]>
Commit | Line | Data |
---|---|---|
4f22f405 | 1 | /* |
b4df712a | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
4f22f405 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
a1df06b3 | 11 | #include "internal/ctype.h" |
99ba9fd0 | 12 | #include <limits.h> |
b39fc560 | 13 | #include "internal/cryptlib.h" |
ec577822 | 14 | #include <openssl/buffer.h> |
d02b48c6 RE |
15 | #include "bn_lcl.h" |
16 | ||
0f113f3e | 17 | static const char Hex[] = "0123456789ABCDEF"; |
d02b48c6 | 18 | |
26a3a48d | 19 | /* Must 'OPENSSL_free' the returned data */ |
8623f693 | 20 | char *BN_bn2hex(const BIGNUM *a) |
0f113f3e MC |
21 | { |
22 | int i, j, v, z = 0; | |
23 | char *buf; | |
24 | char *p; | |
25 | ||
01c09f9f RS |
26 | if (BN_is_zero(a)) |
27 | return OPENSSL_strdup("0"); | |
28 | buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2); | |
0f113f3e MC |
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) | |
27c6d63d | 35 | *p++ = '-'; |
0f113f3e MC |
36 | for (i = a->top - 1; i >= 0; i--) { |
37 | for (j = BN_BITS2 - 8; j >= 0; j -= 8) { | |
38 | /* strip leading zeros */ | |
27c6d63d P |
39 | v = (int)((a->d[i] >> j) & 0xff); |
40 | if (z || v != 0) { | |
41 | *p++ = Hex[v >> 4]; | |
42 | *p++ = Hex[v & 0x0f]; | |
0f113f3e MC |
43 | z = 1; |
44 | } | |
45 | } | |
46 | } | |
47 | *p = '\0'; | |
48 | err: | |
b4df712a | 49 | return buf; |
0f113f3e | 50 | } |
d02b48c6 | 51 | |
26a3a48d | 52 | /* Must 'OPENSSL_free' the returned data */ |
8623f693 | 53 | char *BN_bn2dec(const BIGNUM *a) |
0f113f3e | 54 | { |
86ba26c8 | 55 | int i = 0, num, ok = 0, n, tbytes; |
0f113f3e MC |
56 | char *buf = NULL; |
57 | char *p; | |
58 | BIGNUM *t = NULL; | |
59 | BN_ULONG *bn_data = NULL, *lp; | |
07bed46f | 60 | int bn_data_num; |
0f113f3e | 61 | |
35a1cc90 MC |
62 | /*- |
63 | * get an upper bound for the length of the decimal integer | |
64 | * num <= (BN_num_bits(a) + 1) * log(2) | |
2338ad88 DB |
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 | |
35a1cc90 | 67 | */ |
0f113f3e MC |
68 | i = BN_num_bits(a) * 3; |
69 | num = (i / 10 + i / 1000 + 1) + 1; | |
86ba26c8 | 70 | tbytes = num + 3; /* negative and terminator and one spare? */ |
07bed46f DSH |
71 | bn_data_num = num / BN_DEC_NUM + 1; |
72 | bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG)); | |
86ba26c8 | 73 | buf = OPENSSL_malloc(tbytes); |
27c6d63d | 74 | if (buf == NULL || bn_data == NULL) { |
0f113f3e MC |
75 | BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE); |
76 | goto err; | |
77 | } | |
78 | if ((t = BN_dup(a)) == NULL) | |
79 | goto err; | |
58964a49 | 80 | |
0f113f3e MC |
81 | p = buf; |
82 | lp = bn_data; | |
83 | if (BN_is_zero(t)) { | |
27c6d63d P |
84 | *p++ = '0'; |
85 | *p++ = '\0'; | |
0f113f3e MC |
86 | } else { |
87 | if (BN_is_negative(t)) | |
88 | *p++ = '-'; | |
89 | ||
0f113f3e | 90 | while (!BN_is_zero(t)) { |
099e2968 KY |
91 | if (lp - bn_data >= bn_data_num) |
92 | goto err; | |
0f113f3e | 93 | *lp = BN_div_word(t, BN_DEC_CONV); |
07bed46f DSH |
94 | if (*lp == (BN_ULONG)-1) |
95 | goto err; | |
0f113f3e MC |
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 | */ | |
86ba26c8 P |
104 | n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT1, *lp); |
105 | if (n < 0) | |
106 | goto err; | |
107 | p += n; | |
0f113f3e MC |
108 | while (lp != bn_data) { |
109 | lp--; | |
86ba26c8 P |
110 | n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT2, *lp); |
111 | if (n < 0) | |
112 | goto err; | |
113 | p += n; | |
0f113f3e MC |
114 | } |
115 | } | |
116 | ok = 1; | |
117 | err: | |
b548a1f1 | 118 | OPENSSL_free(bn_data); |
23a1d5e9 | 119 | BN_free(t); |
b548a1f1 RS |
120 | if (ok) |
121 | return buf; | |
122 | OPENSSL_free(buf); | |
123 | return NULL; | |
0f113f3e | 124 | } |
58964a49 | 125 | |
8623f693 | 126 | int BN_hex2bn(BIGNUM **bn, const char *a) |
0f113f3e MC |
127 | { |
128 | BIGNUM *ret = NULL; | |
129 | BN_ULONG l = 0; | |
130 | int neg = 0, h, m, i, j, k, c; | |
131 | int num; | |
132 | ||
27c6d63d | 133 | if (a == NULL || *a == '\0') |
b4df712a | 134 | return 0; |
0f113f3e MC |
135 | |
136 | if (*a == '-') { | |
137 | neg = 1; | |
138 | a++; | |
139 | } | |
140 | ||
27c6d63d | 141 | for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++) |
99ba9fd0 MC |
142 | continue; |
143 | ||
27c6d63d | 144 | if (i == 0 || i > INT_MAX / 4) |
99ba9fd0 | 145 | goto err; |
0f113f3e MC |
146 | |
147 | num = i + neg; | |
148 | if (bn == NULL) | |
b4df712a | 149 | return num; |
0f113f3e MC |
150 | |
151 | /* a is the start of the hex digits, and it is 'i' long */ | |
152 | if (*bn == NULL) { | |
153 | if ((ret = BN_new()) == NULL) | |
b4df712a | 154 | return 0; |
0f113f3e MC |
155 | } else { |
156 | ret = *bn; | |
157 | BN_zero(ret); | |
158 | } | |
159 | ||
99ba9fd0 | 160 | /* i is the number of hex digits */ |
0f113f3e MC |
161 | if (bn_expand(ret, i * 4) == NULL) |
162 | goto err; | |
163 | ||
164 | j = i; /* least significant 'hex' */ | |
165 | m = 0; | |
166 | h = 0; | |
167 | while (j > 0) { | |
27c6d63d | 168 | m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j; |
0f113f3e MC |
169 | l = 0; |
170 | for (;;) { | |
171 | c = a[j - m]; | |
49445f21 RS |
172 | k = OPENSSL_hexchar2int(c); |
173 | if (k < 0) | |
0f113f3e MC |
174 | k = 0; /* paranoia */ |
175 | l = (l << 4) | k; | |
176 | ||
177 | if (--m <= 0) { | |
178 | ret->d[h++] = l; | |
179 | break; | |
180 | } | |
181 | } | |
27c6d63d | 182 | j -= BN_BYTES * 2; |
0f113f3e MC |
183 | } |
184 | ret->top = h; | |
185 | bn_correct_top(ret); | |
0f113f3e MC |
186 | |
187 | *bn = ret; | |
188 | bn_check_top(ret); | |
01c09f9f RS |
189 | /* Don't set the negative flag if it's zero. */ |
190 | if (ret->top != 0) | |
191 | ret->neg = neg; | |
b4df712a | 192 | return num; |
0f113f3e MC |
193 | err: |
194 | if (*bn == NULL) | |
195 | BN_free(ret); | |
b4df712a | 196 | return 0; |
0f113f3e | 197 | } |
d02b48c6 | 198 | |
8623f693 | 199 | int BN_dec2bn(BIGNUM **bn, const char *a) |
0f113f3e MC |
200 | { |
201 | BIGNUM *ret = NULL; | |
202 | BN_ULONG l = 0; | |
203 | int neg = 0, i, j; | |
204 | int num; | |
205 | ||
27c6d63d | 206 | if (a == NULL || *a == '\0') |
b4df712a | 207 | return 0; |
0f113f3e MC |
208 | if (*a == '-') { |
209 | neg = 1; | |
210 | a++; | |
211 | } | |
212 | ||
27c6d63d | 213 | for (i = 0; i <= INT_MAX / 4 && ossl_isdigit(a[i]); i++) |
99ba9fd0 MC |
214 | continue; |
215 | ||
27c6d63d | 216 | if (i == 0 || i > INT_MAX / 4) |
99ba9fd0 | 217 | goto err; |
0f113f3e MC |
218 | |
219 | num = i + neg; | |
220 | if (bn == NULL) | |
b4df712a | 221 | return num; |
0f113f3e MC |
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 | if (*bn == NULL) { | |
228 | if ((ret = BN_new()) == NULL) | |
b4df712a | 229 | return 0; |
0f113f3e MC |
230 | } else { |
231 | ret = *bn; | |
232 | BN_zero(ret); | |
233 | } | |
234 | ||
99ba9fd0 | 235 | /* i is the number of digits, a bit of an over expand */ |
0f113f3e MC |
236 | if (bn_expand(ret, i * 4) == NULL) |
237 | goto err; | |
238 | ||
27c6d63d | 239 | j = BN_DEC_NUM - i % BN_DEC_NUM; |
0f113f3e MC |
240 | if (j == BN_DEC_NUM) |
241 | j = 0; | |
242 | l = 0; | |
01c09f9f | 243 | while (--i >= 0) { |
0f113f3e MC |
244 | l *= 10; |
245 | l += *a - '0'; | |
246 | a++; | |
247 | if (++j == BN_DEC_NUM) { | |
d356dc56 MC |
248 | if (!BN_mul_word(ret, BN_DEC_CONV) |
249 | || !BN_add_word(ret, l)) | |
250 | goto err; | |
0f113f3e MC |
251 | l = 0; |
252 | j = 0; | |
253 | } | |
254 | } | |
0f113f3e MC |
255 | |
256 | bn_correct_top(ret); | |
257 | *bn = ret; | |
258 | bn_check_top(ret); | |
01c09f9f RS |
259 | /* Don't set the negative flag if it's zero. */ |
260 | if (ret->top != 0) | |
261 | ret->neg = neg; | |
b4df712a | 262 | return num; |
0f113f3e MC |
263 | err: |
264 | if (*bn == NULL) | |
265 | BN_free(ret); | |
b4df712a | 266 | return 0; |
0f113f3e | 267 | } |
58964a49 | 268 | |
54d853eb | 269 | int BN_asc2bn(BIGNUM **bn, const char *a) |
0f113f3e MC |
270 | { |
271 | const char *p = a; | |
01c09f9f | 272 | |
0f113f3e MC |
273 | if (*p == '-') |
274 | p++; | |
275 | ||
276 | if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) { | |
277 | if (!BN_hex2bn(bn, p + 2)) | |
278 | return 0; | |
279 | } else { | |
280 | if (!BN_dec2bn(bn, p)) | |
281 | return 0; | |
282 | } | |
01c09f9f RS |
283 | /* Don't set the negative flag if it's zero. */ |
284 | if (*a == '-' && (*bn)->top != 0) | |
0f113f3e MC |
285 | (*bn)->neg = 1; |
286 | return 1; | |
287 | } | |
54d853eb | 288 | |
0f113f3e | 289 | # ifndef OPENSSL_NO_STDIO |
e93f9a32 | 290 | int BN_print_fp(FILE *fp, const BIGNUM *a) |
0f113f3e MC |
291 | { |
292 | BIO *b; | |
293 | int ret; | |
294 | ||
295 | if ((b = BIO_new(BIO_s_file())) == NULL) | |
b4df712a | 296 | return 0; |
0f113f3e MC |
297 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
298 | ret = BN_print(b, a); | |
299 | BIO_free(b); | |
b4df712a | 300 | return ret; |
0f113f3e MC |
301 | } |
302 | # endif | |
d02b48c6 | 303 | |
8d8a8041 | 304 | int BN_print(BIO *bp, const BIGNUM *a) |
0f113f3e MC |
305 | { |
306 | int i, j, v, z = 0; | |
307 | int ret = 0; | |
308 | ||
27c6d63d | 309 | if ((a->neg) && BIO_write(bp, "-", 1) != 1) |
0f113f3e | 310 | goto end; |
27c6d63d | 311 | if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1) |
0f113f3e MC |
312 | goto end; |
313 | for (i = a->top - 1; i >= 0; i--) { | |
314 | for (j = BN_BITS2 - 4; j >= 0; j -= 4) { | |
315 | /* strip leading zeros */ | |
c9b820aa | 316 | v = (int)((a->d[i] >> j) & 0x0f); |
27c6d63d P |
317 | if (z || v != 0) { |
318 | if (BIO_write(bp, &Hex[v], 1) != 1) | |
0f113f3e MC |
319 | goto end; |
320 | z = 1; | |
321 | } | |
322 | } | |
323 | } | |
324 | ret = 1; | |
325 | end: | |
b4df712a | 326 | return ret; |
0f113f3e | 327 | } |
13a55192 DSH |
328 | |
329 | char *BN_options(void) | |
0f113f3e MC |
330 | { |
331 | static int init = 0; | |
332 | static char data[16]; | |
13a55192 | 333 | |
0f113f3e MC |
334 | if (!init) { |
335 | init++; | |
13a55192 | 336 | #ifdef BN_LLONG |
86ba26c8 P |
337 | BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)", |
338 | sizeof(BN_ULLONG) * 8, sizeof(BN_ULONG) * 8); | |
13a55192 | 339 | #else |
86ba26c8 P |
340 | BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)", |
341 | sizeof(BN_ULONG) * 8, sizeof(BN_ULONG) * 8); | |
13a55192 | 342 | #endif |
0f113f3e | 343 | } |
b4df712a | 344 | return data; |
0f113f3e | 345 | } |