f6030ff14cc6cb4cacad7b95229e565b7a95acd0
[openssl.git] / crypto / bn / bn_print.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <limits.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/buffer.h>
15 #include "bn_lcl.h"
16
17 static const char Hex[] = "0123456789ABCDEF";
18
19 /* Must 'OPENSSL_free' the returned data */
20 char *BN_bn2hex(const BIGNUM *a)
21 {
22     int i, j, v, z = 0;
23     char *buf;
24     char *p;
25
26     if (a->neg && BN_is_zero(a)) {
27         /* "-0" == 3 bytes including NULL terminator */
28         buf = OPENSSL_malloc(3);
29     } else {
30         buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
31     }
32     if (buf == NULL) {
33         BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
34         goto err;
35     }
36     p = buf;
37     if (a->neg)
38         *(p++) = '-';
39     if (BN_is_zero(a))
40         *(p++) = '0';
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;
45             if (z || (v != 0)) {
46                 *(p++) = Hex[v >> 4];
47                 *(p++) = Hex[v & 0x0f];
48                 z = 1;
49             }
50         }
51     }
52     *p = '\0';
53  err:
54     return (buf);
55 }
56
57 /* Must 'OPENSSL_free' the returned data */
58 char *BN_bn2dec(const BIGNUM *a)
59 {
60     int i = 0, num, ok = 0;
61     char *buf = NULL;
62     char *p;
63     BIGNUM *t = NULL;
64     BN_ULONG *bn_data = NULL, *lp;
65     int bn_data_num;
66
67     /*-
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
72      */
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);
80         goto err;
81     }
82     if ((t = BN_dup(a)) == NULL)
83         goto err;
84
85 #define BUF_REMAIN (num+3 - (size_t)(p - buf))
86     p = buf;
87     lp = bn_data;
88     if (BN_is_zero(t)) {
89         *(p++) = '0';
90         *(p++) = '\0';
91     } else {
92         if (BN_is_negative(t))
93             *p++ = '-';
94
95         i = 0;
96         while (!BN_is_zero(t)) {
97             *lp = BN_div_word(t, BN_DEC_CONV);
98             if (*lp == (BN_ULONG)-1)
99                 goto err;
100             lp++;
101             if (lp - bn_data >= bn_data_num)
102                 goto err;
103         }
104         lp--;
105         /*
106          * We now have a series of blocks, BN_DEC_NUM chars in length, where
107          * the last one needs truncation. The blocks need to be reversed in
108          * order.
109          */
110         BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);
111         while (*p)
112             p++;
113         while (lp != bn_data) {
114             lp--;
115             BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);
116             while (*p)
117                 p++;
118         }
119     }
120     ok = 1;
121  err:
122     OPENSSL_free(bn_data);
123     BN_free(t);
124     if (ok)
125         return buf;
126     OPENSSL_free(buf);
127     return NULL;
128 }
129
130 int BN_hex2bn(BIGNUM **bn, const char *a)
131 {
132     BIGNUM *ret = NULL;
133     BN_ULONG l = 0;
134     int neg = 0, h, m, i, j, k, c;
135     int num;
136
137     if ((a == NULL) || (*a == '\0'))
138         return (0);
139
140     if (*a == '-') {
141         neg = 1;
142         a++;
143     }
144
145     for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)
146         continue;
147
148     if (i == 0 || i > INT_MAX/4)
149         goto err;
150
151     num = i + neg;
152     if (bn == NULL)
153         return (num);
154
155     /* a is the start of the hex digits, and it is 'i' long */
156     if (*bn == NULL) {
157         if ((ret = BN_new()) == NULL)
158             return (0);
159     } else {
160         ret = *bn;
161         BN_zero(ret);
162     }
163
164     /* i is the number of hex digits */
165     if (bn_expand(ret, i * 4) == NULL)
166         goto err;
167
168     j = i;                      /* least significant 'hex' */
169     m = 0;
170     h = 0;
171     while (j > 0) {
172         m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;
173         l = 0;
174         for (;;) {
175             c = a[j - m];
176             k = OPENSSL_hexchar2int(c);
177             if (k < 0)
178                 k = 0;          /* paranoia */
179             l = (l << 4) | k;
180
181             if (--m <= 0) {
182                 ret->d[h++] = l;
183                 break;
184             }
185         }
186         j -= (BN_BYTES * 2);
187     }
188     ret->top = h;
189     bn_correct_top(ret);
190     ret->neg = neg;
191
192     *bn = ret;
193     bn_check_top(ret);
194     return (num);
195  err:
196     if (*bn == NULL)
197         BN_free(ret);
198     return (0);
199 }
200
201 int BN_dec2bn(BIGNUM **bn, const char *a)
202 {
203     BIGNUM *ret = NULL;
204     BN_ULONG l = 0;
205     int neg = 0, i, j;
206     int num;
207
208     if ((a == NULL) || (*a == '\0'))
209         return (0);
210     if (*a == '-') {
211         neg = 1;
212         a++;
213     }
214
215     for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)
216         continue;
217
218     if (i == 0 || i > INT_MAX/4)
219         goto err;
220
221     num = i + neg;
222     if (bn == NULL)
223         return (num);
224
225     /*
226      * a is the start of the digits, and it is 'i' long. We chop it into
227      * BN_DEC_NUM digits at a time
228      */
229     if (*bn == NULL) {
230         if ((ret = BN_new()) == NULL)
231             return (0);
232     } else {
233         ret = *bn;
234         BN_zero(ret);
235     }
236
237     /* i is the number of digits, a bit of an over expand */
238     if (bn_expand(ret, i * 4) == NULL)
239         goto err;
240
241     j = BN_DEC_NUM - (i % BN_DEC_NUM);
242     if (j == BN_DEC_NUM)
243         j = 0;
244     l = 0;
245     while (*a) {
246         l *= 10;
247         l += *a - '0';
248         a++;
249         if (++j == BN_DEC_NUM) {
250             if (!BN_mul_word(ret, BN_DEC_CONV)
251                 || !BN_add_word(ret, l))
252                 goto err;
253             l = 0;
254             j = 0;
255         }
256     }
257     ret->neg = neg;
258
259     bn_correct_top(ret);
260     *bn = ret;
261     bn_check_top(ret);
262     return (num);
263  err:
264     if (*bn == NULL)
265         BN_free(ret);
266     return (0);
267 }
268
269 int BN_asc2bn(BIGNUM **bn, const char *a)
270 {
271     const char *p = a;
272     if (*p == '-')
273         p++;
274
275     if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
276         if (!BN_hex2bn(bn, p + 2))
277             return 0;
278     } else {
279         if (!BN_dec2bn(bn, p))
280             return 0;
281     }
282     if (*a == '-')
283         (*bn)->neg = 1;
284     return 1;
285 }
286
287 # ifndef OPENSSL_NO_STDIO
288 int BN_print_fp(FILE *fp, const BIGNUM *a)
289 {
290     BIO *b;
291     int ret;
292
293     if ((b = BIO_new(BIO_s_file())) == NULL)
294         return (0);
295     BIO_set_fp(b, fp, BIO_NOCLOSE);
296     ret = BN_print(b, a);
297     BIO_free(b);
298     return (ret);
299 }
300 # endif
301
302 int BN_print(BIO *bp, const BIGNUM *a)
303 {
304     int i, j, v, z = 0;
305     int ret = 0;
306
307     if ((a->neg) && (BIO_write(bp, "-", 1) != 1))
308         goto end;
309     if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))
310         goto end;
311     for (i = a->top - 1; i >= 0; i--) {
312         for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
313             /* strip leading zeros */
314             v = ((int)(a->d[i] >> (long)j)) & 0x0f;
315             if (z || (v != 0)) {
316                 if (BIO_write(bp, &(Hex[v]), 1) != 1)
317                     goto end;
318                 z = 1;
319             }
320         }
321     }
322     ret = 1;
323  end:
324     return (ret);
325 }
326
327 char *BN_options(void)
328 {
329     static int init = 0;
330     static char data[16];
331
332     if (!init) {
333         init++;
334 #ifdef BN_LLONG
335         BIO_snprintf(data, sizeof data, "bn(%d,%d)",
336                      (int)sizeof(BN_ULLONG) * 8, (int)sizeof(BN_ULONG) * 8);
337 #else
338         BIO_snprintf(data, sizeof data, "bn(%d,%d)",
339                      (int)sizeof(BN_ULONG) * 8, (int)sizeof(BN_ULONG) * 8);
340 #endif
341     }
342     return (data);
343 }