diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2015-09-13 16:02:12 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2015-09-13 16:02:12 +0000 |
commit | 3437b9d40659ded468a83c5a7de7e41236cb50cb (patch) | |
tree | f47fb7244314b2982570b21f3a64b3cdc127dc12 /lib | |
parent | 0036919aa9e53366d1ae9f19c637aeb5a44f0466 (diff) |
Handle negative-zero in BN_bn2dec() too, just like in BN_print().
ok miod
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libcrypto/bn/bn_print.c | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/lib/libcrypto/bn/bn_print.c b/lib/libcrypto/bn/bn_print.c index 6b9f82caafc..a68412c8a82 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.24 2015/09/13 15:59:29 deraadt Exp $ */ +/* $OpenBSD: bn_print.c,v 1.25 2015/09/13 16:02:11 deraadt Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -114,6 +114,20 @@ BN_bn2dec(const BIGNUM *a) BIGNUM *t = NULL; BN_ULONG *bn_data = NULL, *lp; + if (BN_is_zero(t)) { + buf = malloc(BN_is_negative(t) + 2); + if (buf == NULL) { + BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE); + goto err; + } + p = buf; + if (BN_is_negative(t)) + *(p++) = '-'; + *(p++) = '0'; + *(p++) = '\0'; + return (buf); + } + /* get an upper bound for the length of the decimal integer * num <= (BN_num_bits(a) + 1) * log(2) * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error) @@ -133,31 +147,26 @@ BN_bn2dec(const BIGNUM *a) #define BUF_REMAIN (num+3 - (size_t)(p - buf)) p = buf; lp = bn_data; - if (BN_is_zero(t)) { - *(p++) = '0'; - *(p++) = '\0'; - } else { - if (BN_is_negative(t)) - *p++ = '-'; + if (BN_is_negative(t)) + *p++ = '-'; - i = 0; - while (!BN_is_zero(t)) { - *lp = BN_div_word(t, BN_DEC_CONV); - lp++; - } + i = 0; + while (!BN_is_zero(t)) { + *lp = BN_div_word(t, BN_DEC_CONV); + lp++; + } + lp--; + /* We now have a series of blocks, BN_DEC_NUM chars + * in length, where the last one needs truncation. + * The blocks need to be reversed in order. */ + snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp); + while (*p) + p++; + while (lp != bn_data) { lp--; - /* We now have a series of blocks, BN_DEC_NUM chars - * in length, where the last one needs truncation. - * The blocks need to be reversed in order. */ - snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp); + snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp); while (*p) p++; - while (lp != bn_data) { - lp--; - snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp); - while (*p) - p++; - } } ok = 1; |