Add UI functionality to duplicate the user data
[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 (BN_is_zero(a))
27         return OPENSSL_strdup("0");
28     buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
29     if (buf == NULL) {
30         BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
31         goto err;
32     }
33     p = buf;
34     if (a->neg)
35         *(p++) = '-';
36     for (i = a->top - 1; i >= 0; i--) {
37         for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
38             /* strip leading zeros */
39             v = ((int)(a->d[i] >> (long)j)) & 0xff;
40             if (z || (v != 0)) {
41                 *(p++) = Hex[v >> 4];
42                 *(p++) = Hex[v & 0x0f];
43                 z = 1;
44             }
45         }
46     }
47     *p = '\0';
48  err:
49     return (buf);
50 }
51
52 /* Must 'OPENSSL_free' the returned data */
53 char *BN_bn2dec(const BIGNUM *a)
54 {
55     int i = 0, num, ok = 0;
56     char *buf = NULL;
57     char *p;
58     BIGNUM *t = NULL;
59     BN_ULONG *bn_data = NULL, *lp;
60     int bn_data_num;
61
62     /*-
63      * get an upper bound for the length of the decimal integer
64      * num <= (BN_num_bits(a) + 1) * log(2)
65      *     <= 3 * BN_num_bits(a) * 0.101 + log(2) + 1     (rounding error)
66      *     <= 3 * BN_num_bits(a) / 10 + 3 * BN_num_bits / 1000 + 1 + 1
67      */
68     i = BN_num_bits(a) * 3;
69     num = (i / 10 + i / 1000 + 1) + 1;
70     bn_data_num = num / BN_DEC_NUM + 1;
71     bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG));
72     buf = OPENSSL_malloc(num + 3);
73     if ((buf == NULL) || (bn_data == NULL)) {
74         BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
75         goto err;
76     }
77     if ((t = BN_dup(a)) == NULL)
78         goto err;
79
80 #define BUF_REMAIN (num+3 - (size_t)(p - buf))
81     p = buf;
82     lp = bn_data;
83     if (BN_is_zero(t)) {
84         *(p++) = '0';
85         *(p++) = '\0';
86     } else {
87         if (BN_is_negative(t))
88             *p++ = '-';
89
90         while (!BN_is_zero(t)) {
91             if (lp - bn_data >= bn_data_num)
92                 goto err;
93             *lp = BN_div_word(t, BN_DEC_CONV);
94             if (*lp == (BN_ULONG)-1)
95                 goto err;
96             lp++;
97         }
98         lp--;
99         /*
100          * We now have a series of blocks, BN_DEC_NUM chars in length, where
101          * the last one needs truncation. The blocks need to be reversed in
102          * order.
103          */
104         BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);
105         while (*p)
106             p++;
107         while (lp != bn_data) {
108             lp--;
109             BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);
110             while (*p)
111                 p++;
112         }
113     }
114     ok = 1;
115  err:
116     OPENSSL_free(bn_data);
117     BN_free(t);
118     if (ok)
119         return buf;
120     OPENSSL_free(buf);
121     return NULL;
122 }
123
124 int BN_hex2bn(BIGNUM **bn, const char *a)
125 {
126     BIGNUM *ret = NULL;
127     BN_ULONG l = 0;
128     int neg = 0, h, m, i, j, k, c;
129     int num;
130
131     if ((a == NULL) || (*a == '\0'))
132         return (0);
133
134     if (*a == '-') {
135         neg = 1;
136         a++;
137     }
138
139     for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)
140         continue;
141
142     if (i == 0 || i > INT_MAX/4)
143         goto err;
144
145     num = i + neg;
146     if (bn == NULL)
147         return (num);
148
149     /* a is the start of the hex digits, and it is 'i' long */
150     if (*bn == NULL) {
151         if ((ret = BN_new()) == NULL)
152             return (0);
153     } else {
154         ret = *bn;
155         BN_zero(ret);
156     }
157
158     /* i is the number of hex digits */
159     if (bn_expand(ret, i * 4) == NULL)
160         goto err;
161
162     j = i;                      /* least significant 'hex' */
163     m = 0;
164     h = 0;
165     while (j > 0) {
166         m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;
167         l = 0;
168         for (;;) {
169             c = a[j - m];
170             k = OPENSSL_hexchar2int(c);
171             if (k < 0)
172                 k = 0;          /* paranoia */
173             l = (l << 4) | k;
174
175             if (--m <= 0) {
176                 ret->d[h++] = l;
177                 break;
178             }
179         }
180         j -= (BN_BYTES * 2);
181     }
182     ret->top = h;
183     bn_correct_top(ret);
184
185     *bn = ret;
186     bn_check_top(ret);
187     /* Don't set the negative flag if it's zero. */
188     if (ret->top != 0)
189         ret->neg = neg;
190     return (num);
191  err:
192     if (*bn == NULL)
193         BN_free(ret);
194     return (0);
195 }
196
197 int BN_dec2bn(BIGNUM **bn, const char *a)
198 {
199     BIGNUM *ret = NULL;
200     BN_ULONG l = 0;
201     int neg = 0, i, j;
202     int num;
203
204     if ((a == NULL) || (*a == '\0'))
205         return (0);
206     if (*a == '-') {
207         neg = 1;
208         a++;
209     }
210
211     for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)
212         continue;
213
214     if (i == 0 || i > INT_MAX/4)
215         goto err;
216
217     num = i + neg;
218     if (bn == NULL)
219         return (num);
220
221     /*
222      * a is the start of the digits, and it is 'i' long. We chop it into
223      * BN_DEC_NUM digits at a time
224      */
225     if (*bn == NULL) {
226         if ((ret = BN_new()) == NULL)
227             return (0);
228     } else {
229         ret = *bn;
230         BN_zero(ret);
231     }
232
233     /* i is the number of digits, a bit of an over expand */
234     if (bn_expand(ret, i * 4) == NULL)
235         goto err;
236
237     j = BN_DEC_NUM - (i % BN_DEC_NUM);
238     if (j == BN_DEC_NUM)
239         j = 0;
240     l = 0;
241     while (--i >= 0) {
242         l *= 10;
243         l += *a - '0';
244         a++;
245         if (++j == BN_DEC_NUM) {
246             if (!BN_mul_word(ret, BN_DEC_CONV)
247                 || !BN_add_word(ret, l))
248                 goto err;
249             l = 0;
250             j = 0;
251         }
252     }
253
254     bn_correct_top(ret);
255     *bn = ret;
256     bn_check_top(ret);
257     /* Don't set the negative flag if it's zero. */
258     if (ret->top != 0)
259         ret->neg = neg;
260     return (num);
261  err:
262     if (*bn == NULL)
263         BN_free(ret);
264     return (0);
265 }
266
267 int BN_asc2bn(BIGNUM **bn, const char *a)
268 {
269     const char *p = a;
270
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     /* Don't set the negative flag if it's zero. */
282     if (*a == '-' && (*bn)->top != 0)
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 }