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