diff options
author | Theo Buehler <tb@cvs.openbsd.org> | 2023-07-09 18:37:59 +0000 |
---|---|---|
committer | Theo Buehler <tb@cvs.openbsd.org> | 2023-07-09 18:37:59 +0000 |
commit | 1615e73ca25c1479beb77832196944e752e71bd2 (patch) | |
tree | cd1bb034115e7a846c9df0f40250433561edb444 /lib | |
parent | acb359cc9a912ee1cb109bb207ea1116aa2bcbe8 (diff) |
Reimplement BN_print() and BN_print_fp()
These can now use the internal version of BN_bn2hex() and be direct
wrappers of BIO_printf() and fprintf() as they should have been all
along.
ok jsing
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libcrypto/bn/bn_convert.c | 47 | ||||
-rw-r--r-- | lib/libcrypto/bn/bn_print.c | 45 |
2 files changed, 45 insertions, 47 deletions
diff --git a/lib/libcrypto/bn/bn_convert.c b/lib/libcrypto/bn/bn_convert.c index 788e90cc8d8..f09c9091e71 100644 --- a/lib/libcrypto/bn/bn_convert.c +++ b/lib/libcrypto/bn/bn_convert.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_convert.c,v 1.14 2023/07/09 18:27:22 tb Exp $ */ +/* $OpenBSD: bn_convert.c,v 1.15 2023/07/09 18:37:58 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -771,48 +771,3 @@ BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain) return (a); } LCRYPTO_ALIAS(BN_mpi2bn); - -#ifndef OPENSSL_NO_BIO -int -BN_print_fp(FILE *fp, const BIGNUM *a) -{ - BIO *b; - int ret; - - if ((b = BIO_new(BIO_s_file())) == NULL) - return (0); - BIO_set_fp(b, fp, BIO_NOCLOSE); - ret = BN_print(b, a); - BIO_free(b); - return (ret); -} -LCRYPTO_ALIAS(BN_print_fp); - -int -BN_print(BIO *bp, const BIGNUM *a) -{ - int i, j, v, z = 0; - int ret = 0; - - if ((a->neg) && (BIO_write(bp, "-", 1) != 1)) - goto end; - if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1)) - goto end; - for (i = a->top - 1; i >= 0; i--) { - for (j = BN_BITS2 - 4; j >= 0; j -= 4) { - /* strip leading zeros */ - v = ((int)(a->d[i] >> (long)j)) & 0x0f; - if (z || (v != 0)) { - if (BIO_write(bp, &hex_digits[v], 1) != 1) - goto end; - z = 1; - } - } - } - ret = 1; - -end: - return (ret); -} -LCRYPTO_ALIAS(BN_print); -#endif diff --git a/lib/libcrypto/bn/bn_print.c b/lib/libcrypto/bn/bn_print.c index c76d077324d..666bbf43322 100644 --- a/lib/libcrypto/bn/bn_print.c +++ b/lib/libcrypto/bn/bn_print.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_print.c,v 1.43 2023/07/09 18:35:52 tb Exp $ */ +/* $OpenBSD: bn_print.c,v 1.44 2023/07/09 18:37:58 tb Exp $ */ /* * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> @@ -19,6 +19,7 @@ #include <ctype.h> #include <limits.h> #include <stdarg.h> +#include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h> @@ -149,3 +150,45 @@ bn_printf(BIO *bio, const BIGNUM *bn, int indent, const char *fmt, ...) return bn_print_bignum(bio, bn, indent); } + +int +BN_print(BIO *bio, const BIGNUM *bn) +{ + char *hex = NULL; + size_t hex_len = 0; + int ret = 0; + + if (!bn_bn2hex_nibbles(bn, &hex, &hex_len)) + goto err; + if (BIO_printf(bio, "%s", hex) <= 0) + goto err; + + ret = 1; + + err: + freezero(hex, hex_len); + + return ret; +} +LCRYPTO_ALIAS(BN_print); + +int +BN_print_fp(FILE *fp, const BIGNUM *bn) +{ + char *hex = NULL; + size_t hex_len = 0; + int ret = 0; + + if (!bn_bn2hex_nibbles(bn, &hex, &hex_len)) + goto err; + if (fprintf(fp, "%s", hex) < 0) + goto err; + + ret = 1; + + err: + freezero(hex, hex_len); + + return ret; +} +LCRYPTO_ALIAS(BN_print_fp); |