2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
13 #include "internal/cryptlib.h"
14 #include <openssl/buffer.h>
17 static const char Hex[] = "0123456789ABCDEF";
19 /* Must 'OPENSSL_free' the returned data */
20 char *BN_bn2hex(const BIGNUM *a)
26 if (a->neg && BN_is_zero(a)) {
27 /* "-0" == 3 bytes including NULL terminator */
28 buf = OPENSSL_malloc(3);
30 buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
33 BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
41 for (i = a->top - 1; i >= 0; i--) {
42 for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
43 /* strip leading zeros */
44 v = ((int)(a->d[i] >> (long)j)) & 0xff;
47 *(p++) = Hex[v & 0x0f];
57 /* Must 'OPENSSL_free' the returned data */
58 char *BN_bn2dec(const BIGNUM *a)
60 int i = 0, num, ok = 0;
64 BN_ULONG *bn_data = NULL, *lp;
68 * get an upper bound for the length of the decimal integer
69 * num <= (BN_num_bits(a) + 1) * log(2)
70 * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error)
71 * <= BN_num_bits(a)/10 + BN_num_bits/1000 + 1 + 1
73 i = BN_num_bits(a) * 3;
74 num = (i / 10 + i / 1000 + 1) + 1;
75 bn_data_num = num / BN_DEC_NUM + 1;
76 bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));
77 buf = OPENSSL_malloc(num + 3);
78 if ((buf == NULL) || (bn_data == NULL)) {
79 BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
82 if ((t = BN_dup(a)) == NULL)
85 #define BUF_REMAIN (num+3 - (size_t)(p - buf))
92 if (BN_is_negative(t))
95 while (!BN_is_zero(t)) {
96 if (lp - bn_data >= bn_data_num)
98 *lp = BN_div_word(t, BN_DEC_CONV);
99 if (*lp == (BN_ULONG)-1)
105 * We now have a series of blocks, BN_DEC_NUM chars in length, where
106 * the last one needs truncation. The blocks need to be reversed in
109 BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);
112 while (lp != bn_data) {
114 BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);
121 OPENSSL_free(bn_data);
129 int BN_hex2bn(BIGNUM **bn, const char *a)
133 int neg = 0, h, m, i, j, k, c;
136 if ((a == NULL) || (*a == '\0'))
144 for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)
147 if (i == 0 || i > INT_MAX/4)
154 /* a is the start of the hex digits, and it is 'i' long */
156 if ((ret = BN_new()) == NULL)
163 /* i is the number of hex digits */
164 if (bn_expand(ret, i * 4) == NULL)
167 j = i; /* least significant 'hex' */
171 m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;
175 k = OPENSSL_hexchar2int(c);
177 k = 0; /* paranoia */
200 int BN_dec2bn(BIGNUM **bn, const char *a)
207 if ((a == NULL) || (*a == '\0'))
214 for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)
217 if (i == 0 || i > INT_MAX/4)
225 * a is the start of the digits, and it is 'i' long. We chop it into
226 * BN_DEC_NUM digits at a time
229 if ((ret = BN_new()) == NULL)
236 /* i is the number of digits, a bit of an over expand */
237 if (bn_expand(ret, i * 4) == NULL)
240 j = BN_DEC_NUM - (i % BN_DEC_NUM);
248 if (++j == BN_DEC_NUM) {
249 if (!BN_mul_word(ret, BN_DEC_CONV)
250 || !BN_add_word(ret, l))
268 int BN_asc2bn(BIGNUM **bn, const char *a)
274 if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
275 if (!BN_hex2bn(bn, p + 2))
278 if (!BN_dec2bn(bn, p))
286 # ifndef OPENSSL_NO_STDIO
287 int BN_print_fp(FILE *fp, const BIGNUM *a)
292 if ((b = BIO_new(BIO_s_file())) == NULL)
294 BIO_set_fp(b, fp, BIO_NOCLOSE);
295 ret = BN_print(b, a);
301 int BN_print(BIO *bp, const BIGNUM *a)
306 if ((a->neg) && (BIO_write(bp, "-", 1) != 1))
308 if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))
310 for (i = a->top - 1; i >= 0; i--) {
311 for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
312 /* strip leading zeros */
313 v = ((int)(a->d[i] >> (long)j)) & 0x0f;
315 if (BIO_write(bp, &(Hex[v]), 1) != 1)
326 char *BN_options(void)
329 static char data[16];
334 BIO_snprintf(data, sizeof data, "bn(%d,%d)",
335 (int)sizeof(BN_ULLONG) * 8, (int)sizeof(BN_ULONG) * 8);
337 BIO_snprintf(data, sizeof data, "bn(%d,%d)",
338 (int)sizeof(BN_ULONG) * 8, (int)sizeof(BN_ULONG) * 8);