From: tb Date: Sun, 9 Jul 2023 18:37:58 +0000 (+0000) Subject: Reimplement BN_print() and BN_print_fp() X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c38a592a1b12eb01d4ec5b18d4798399a59acc4b;p=openbsd 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 --- 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 @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -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);