e5f641b99dd73f06a21a32d7041690db1899dc4b
[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         while (!BN_is_zero(t)) {
96             if (lp - bn_data >= bn_data_num)
97                 goto err;
98             *lp = BN_div_word(t, BN_DEC_CONV);
99             if (*lp == (BN_ULONG)-1)
100                 goto err;
101             lp++;
102         }
103         lp--;
104         /*
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
107          * order.
108          */
109         BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);
110         while (*p)
111             p++;
112         while (lp != bn_data) {
113             lp--;
114             BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);
115             while (*p)
116                 p++;
117         }
118     }
119     ok = 1;
120  err:
121     OPENSSL_free(bn_data);
122     BN_free(t);
123     if (ok)
124         return buf;
125     OPENSSL_free(buf);
126     return NULL;
127 }
128
129 int BN_hex2bn(BIGNUM **bn, const char *a)
130 {
131     BIGNUM *ret = NULL;
132     BN_ULONG l = 0;
133     int neg = 0, h, m, i, j, k, c;
134     int num;
135
136     if ((a == NULL) || (*a == '\0'))
137         return (0);
138
139     if (*a == '-') {
140         neg = 1;
141         a++;
142     }
143
144     for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)
145         continue;
146
147     if (i == 0 || i > INT_MAX/4)
148         goto err;
149
150     num = i + neg;
151     if (bn == NULL)
152         return (num);
153
154     /* a is the start of the hex digits, and it is 'i' long */
155     if (*bn == NULL) {
156         if ((ret = BN_new()) == NULL)
157             return (0);
158     } else {
159         ret = *bn;
160         BN_zero(ret);
161     }
162
163     /* i is the number of hex digits */
164     if (bn_expand(ret, i * 4) == NULL)
165         goto err;
166
167     j = i;                      /* least significant 'hex' */
168     m = 0;
169     h = 0;
170     while (j > 0) {
171         m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;
172         l = 0;
173         for (;;) {
174             c = a[j - m];
175             k = OPENSSL_hexchar2int(c);
176             if (k < 0)
177                 k = 0;          /* paranoia */
178             l = (l << 4) | k;
179
180             if (--m <= 0) {
181                 ret->d[h++] = l;
182                 break;
183             }
184         }
185         j -= (BN_BYTES * 2);
186     }
187     ret->top = h;
188     bn_correct_top(ret);
189     ret->neg = neg;
190
191     *bn = ret;
192     bn_check_top(ret);
193     return (num);
194  err:
195     if (*bn == NULL)
196         BN_free(ret);
197     return (0);
198 }
199
200 int BN_dec2bn(BIGNUM **bn, const char *a)
201 {
202     BIGNUM *ret = NULL;
203     BN_ULONG l = 0;
204     int neg = 0, i, j;
205     int num;
206
207     if ((a == NULL) || (*a == '\0'))
208         return (0);
209     if (*a == '-') {
210         neg = 1;
211         a++;
212     }
213
214     for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)
215         continue;
216
217     if (i == 0 || i > INT_MAX/4)
218         goto err;
219
220     num = i + neg;
221     if (bn == NULL)
222         return (num);
223
224     /*
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
227      */
228     if (*bn == NULL) {
229         if ((ret = BN_new()) == NULL)
230             return (0);
231     } else {
232         ret = *bn;
233         BN_zero(ret);
234     }
235
236     /* i is the number of digits, a bit of an over expand */
237     if (bn_expand(ret, i * 4) == NULL)
238         goto err;
239
240     j = BN_DEC_NUM - (i % BN_DEC_NUM);
241     if (j == BN_DEC_NUM)
242         j = 0;
243     l = 0;
244     while (*a) {
245         l *= 10;
246         l += *a - '0';
247         a++;
248         if (++j == BN_DEC_NUM) {
249             if (!BN_mul_word(ret, BN_DEC_CONV)
250                 || !BN_add_word(ret, l))
251                 goto err;
252             l = 0;
253             j = 0;
254         }
255     }
256     ret->neg = neg;
257
258     bn_correct_top(ret);
259     *bn = ret;
260     bn_check_top(ret);
261     return (num);
262  err:
263     if (*bn == NULL)
264         BN_free(ret);
265     return (0);
266 }
267
268 int BN_asc2bn(BIGNUM **bn, const char *a)
269 {
270     const char *p = a;
271     if (*p == '-')
272         p++;
273
274     if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
275         if (!BN_hex2bn(bn, p + 2))
276             return 0;
277     } else {
278         if (!BN_dec2bn(bn, p))
279             return 0;
280     }
281     if (*a == '-')
282         (*bn)->neg = 1;
283     return 1;
284 }
285
286 # ifndef OPENSSL_NO_STDIO
287 int BN_print_fp(FILE *fp, const BIGNUM *a)
288 {
289     BIO *b;
290     int ret;
291
292     if ((b = BIO_new(BIO_s_file())) == NULL)
293         return (0);
294     BIO_set_fp(b, fp, BIO_NOCLOSE);
295     ret = BN_print(b, a);
296     BIO_free(b);
297     return (ret);
298 }
299 # endif
300
301 int BN_print(BIO *bp, const BIGNUM *a)
302 {
303     int i, j, v, z = 0;
304     int ret = 0;
305
306     if ((a->neg) && (BIO_write(bp, "-", 1) != 1))
307         goto end;
308     if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))
309         goto end;
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;
314             if (z || (v != 0)) {
315                 if (BIO_write(bp, &(Hex[v]), 1) != 1)
316                     goto end;
317                 z = 1;
318             }
319         }
320     }
321     ret = 1;
322  end:
323     return (ret);
324 }
325
326 char *BN_options(void)
327 {
328     static int init = 0;
329     static char data[16];
330
331     if (!init) {
332         init++;
333 #ifdef BN_LLONG
334         BIO_snprintf(data, sizeof data, "bn(%d,%d)",
335                      (int)sizeof(BN_ULLONG) * 8, (int)sizeof(BN_ULONG) * 8);
336 #else
337         BIO_snprintf(data, sizeof data, "bn(%d,%d)",
338                      (int)sizeof(BN_ULONG) * 8, (int)sizeof(BN_ULONG) * 8);
339 #endif
340     }
341     return (data);
342 }