2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/err.h>
11 #include "crypto/ctype.h"
14 static const char Hex[] = "0123456789ABCDEF";
16 /* Must 'OPENSSL_free' the returned data */
17 char *BN_bn2hex(const BIGNUM *a)
24 return OPENSSL_strdup("0");
25 buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
27 BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
33 for (i = a->top - 1; i >= 0; i--) {
34 for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
35 /* strip leading zeros */
36 v = (int)((a->d[i] >> j) & 0xff);
50 /* No BIO_snprintf in FIPS_MODE */
51 /* Must 'OPENSSL_free' the returned data */
52 char *BN_bn2dec(const BIGNUM *a)
54 int i = 0, num, ok = 0, n, tbytes;
58 BN_ULONG *bn_data = NULL, *lp;
62 * get an upper bound for the length of the decimal integer
63 * num <= (BN_num_bits(a) + 1) * log(2)
64 * <= 3 * BN_num_bits(a) * 0.101 + log(2) + 1 (rounding error)
65 * <= 3 * BN_num_bits(a) / 10 + 3 * BN_num_bits / 1000 + 1 + 1
67 i = BN_num_bits(a) * 3;
68 num = (i / 10 + i / 1000 + 1) + 1;
69 tbytes = num + 3; /* negative and terminator and one spare? */
70 bn_data_num = num / BN_DEC_NUM + 1;
71 bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));
72 buf = OPENSSL_malloc(tbytes);
73 if (buf == NULL || bn_data == NULL) {
74 BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
77 if ((t = BN_dup(a)) == NULL)
86 if (BN_is_negative(t))
89 while (!BN_is_zero(t)) {
90 if (lp - bn_data >= bn_data_num)
92 *lp = BN_div_word(t, BN_DEC_CONV);
93 if (*lp == (BN_ULONG)-1)
99 * We now have a series of blocks, BN_DEC_NUM chars in length, where
100 * the last one needs truncation. The blocks need to be reversed in
103 n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT1, *lp);
107 while (lp != bn_data) {
109 n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT2, *lp);
117 OPENSSL_free(bn_data);
126 int BN_hex2bn(BIGNUM **bn, const char *a)
130 int neg = 0, h, m, i, j, k, c;
133 if (a == NULL || *a == '\0')
141 for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)
144 if (i == 0 || i > INT_MAX / 4)
151 /* a is the start of the hex digits, and it is 'i' long */
153 if ((ret = BN_new()) == NULL)
160 /* i is the number of hex digits */
161 if (bn_expand(ret, i * 4) == NULL)
164 j = i; /* least significant 'hex' */
168 m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;
172 k = OPENSSL_hexchar2int(c);
174 k = 0; /* paranoia */
189 /* Don't set the negative flag if it's zero. */
199 int BN_dec2bn(BIGNUM **bn, const char *a)
206 if (a == NULL || *a == '\0')
213 for (i = 0; i <= INT_MAX / 4 && ossl_isdigit(a[i]); i++)
216 if (i == 0 || i > INT_MAX / 4)
224 * a is the start of the digits, and it is 'i' long. We chop it into
225 * BN_DEC_NUM digits at a time
228 if ((ret = BN_new()) == NULL)
235 /* i is the number of digits, a bit of an over expand */
236 if (bn_expand(ret, i * 4) == NULL)
239 j = BN_DEC_NUM - i % BN_DEC_NUM;
247 if (++j == BN_DEC_NUM) {
248 if (!BN_mul_word(ret, BN_DEC_CONV)
249 || !BN_add_word(ret, l))
259 /* Don't set the negative flag if it's zero. */
269 int BN_asc2bn(BIGNUM **bn, const char *a)
276 if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
277 if (!BN_hex2bn(bn, p + 2))
280 if (!BN_dec2bn(bn, p))
283 /* Don't set the negative flag if it's zero. */
284 if (*a == '-' && (*bn)->top != 0)