summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authortb <>2023-07-09 18:37:58 +0000
committertb <>2023-07-09 18:37:58 +0000
commitc3a5f55cf5c07f2337c53a10e83c3d52de255ed9 (patch)
tree2bfbb422f07c263deb1b21a7811ab2b440e86d7e /src/lib
parent2465d485086b84028dd242e11c351bcee78e18a6 (diff)
downloadopenbsd-c3a5f55cf5c07f2337c53a10e83c3d52de255ed9.tar.gz
openbsd-c3a5f55cf5c07f2337c53a10e83c3d52de255ed9.tar.bz2
openbsd-c3a5f55cf5c07f2337c53a10e83c3d52de255ed9.zip
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 'src/lib')
-rw-r--r--src/lib/libcrypto/bn/bn_convert.c47
-rw-r--r--src/lib/libcrypto/bn/bn_print.c45
2 files changed, 45 insertions, 47 deletions
diff --git a/src/lib/libcrypto/bn/bn_convert.c b/src/lib/libcrypto/bn/bn_convert.c
index 788e90cc8d..f09c9091e7 100644
--- a/src/lib/libcrypto/bn/bn_convert.c
+++ b/src/lib/libcrypto/bn/bn_convert.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_convert.c,v 1.14 2023/07/09 18:27:22 tb Exp $ */ 1/* $OpenBSD: bn_convert.c,v 1.15 2023/07/09 18:37:58 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -771,48 +771,3 @@ BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain)
771 return (a); 771 return (a);
772} 772}
773LCRYPTO_ALIAS(BN_mpi2bn); 773LCRYPTO_ALIAS(BN_mpi2bn);
774
775#ifndef OPENSSL_NO_BIO
776int
777BN_print_fp(FILE *fp, const BIGNUM *a)
778{
779 BIO *b;
780 int ret;
781
782 if ((b = BIO_new(BIO_s_file())) == NULL)
783 return (0);
784 BIO_set_fp(b, fp, BIO_NOCLOSE);
785 ret = BN_print(b, a);
786 BIO_free(b);
787 return (ret);
788}
789LCRYPTO_ALIAS(BN_print_fp);
790
791int
792BN_print(BIO *bp, const BIGNUM *a)
793{
794 int i, j, v, z = 0;
795 int ret = 0;
796
797 if ((a->neg) && (BIO_write(bp, "-", 1) != 1))
798 goto end;
799 if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))
800 goto end;
801 for (i = a->top - 1; i >= 0; i--) {
802 for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
803 /* strip leading zeros */
804 v = ((int)(a->d[i] >> (long)j)) & 0x0f;
805 if (z || (v != 0)) {
806 if (BIO_write(bp, &hex_digits[v], 1) != 1)
807 goto end;
808 z = 1;
809 }
810 }
811 }
812 ret = 1;
813
814end:
815 return (ret);
816}
817LCRYPTO_ALIAS(BN_print);
818#endif
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c
index c76d077324..666bbf4332 100644
--- a/src/lib/libcrypto/bn/bn_print.c
+++ b/src/lib/libcrypto/bn/bn_print.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_print.c,v 1.43 2023/07/09 18:35:52 tb Exp $ */ 1/* $OpenBSD: bn_print.c,v 1.44 2023/07/09 18:37:58 tb Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2023 Theo Buehler <tb@openbsd.org>
@@ -19,6 +19,7 @@
19#include <ctype.h> 19#include <ctype.h>
20#include <limits.h> 20#include <limits.h>
21#include <stdarg.h> 21#include <stdarg.h>
22#include <stdio.h>
22#include <stdint.h> 23#include <stdint.h>
23#include <stdlib.h> 24#include <stdlib.h>
24#include <string.h> 25#include <string.h>
@@ -149,3 +150,45 @@ bn_printf(BIO *bio, const BIGNUM *bn, int indent, const char *fmt, ...)
149 150
150 return bn_print_bignum(bio, bn, indent); 151 return bn_print_bignum(bio, bn, indent);
151} 152}
153
154int
155BN_print(BIO *bio, const BIGNUM *bn)
156{
157 char *hex = NULL;
158 size_t hex_len = 0;
159 int ret = 0;
160
161 if (!bn_bn2hex_nibbles(bn, &hex, &hex_len))
162 goto err;
163 if (BIO_printf(bio, "%s", hex) <= 0)
164 goto err;
165
166 ret = 1;
167
168 err:
169 freezero(hex, hex_len);
170
171 return ret;
172}
173LCRYPTO_ALIAS(BN_print);
174
175int
176BN_print_fp(FILE *fp, const BIGNUM *bn)
177{
178 char *hex = NULL;
179 size_t hex_len = 0;
180 int ret = 0;
181
182 if (!bn_bn2hex_nibbles(bn, &hex, &hex_len))
183 goto err;
184 if (fprintf(fp, "%s", hex) < 0)
185 goto err;
186
187 ret = 1;
188
189 err:
190 freezero(hex, hex_len);
191
192 return ret;
193}
194LCRYPTO_ALIAS(BN_print_fp);