]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_print.c
Copyright consolidation 06/10
[thirdparty/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
66 /*-
67 * get an upper bound for the length of the decimal integer
68 * num <= (BN_num_bits(a) + 1) * log(2)
69 * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error)
70 * <= BN_num_bits(a)/10 + BN_num_bits/1000 + 1 + 1
71 */
72 i = BN_num_bits(a) * 3;
73 num = (i / 10 + i / 1000 + 1) + 1;
74 bn_data = OPENSSL_malloc((num / BN_DEC_NUM + 1) * sizeof(BN_ULONG));
75 buf = OPENSSL_malloc(num + 3);
76 if ((buf == NULL) || (bn_data == NULL)) {
77 BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
78 goto err;
79 }
80 if ((t = BN_dup(a)) == NULL)
81 goto err;
82
83 #define BUF_REMAIN (num+3 - (size_t)(p - buf))
84 p = buf;
85 lp = bn_data;
86 if (BN_is_zero(t)) {
87 *(p++) = '0';
88 *(p++) = '\0';
89 } else {
90 if (BN_is_negative(t))
91 *p++ = '-';
92
93 i = 0;
94 while (!BN_is_zero(t)) {
95 *lp = BN_div_word(t, BN_DEC_CONV);
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 > 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 ret->neg = neg;
185
186 *bn = ret;
187 bn_check_top(ret);
188 return (num);
189 err:
190 if (*bn == NULL)
191 BN_free(ret);
192 return (0);
193 }
194
195 int BN_dec2bn(BIGNUM **bn, const char *a)
196 {
197 BIGNUM *ret = NULL;
198 BN_ULONG l = 0;
199 int neg = 0, i, j;
200 int num;
201
202 if ((a == NULL) || (*a == '\0'))
203 return (0);
204 if (*a == '-') {
205 neg = 1;
206 a++;
207 }
208
209 for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)
210 continue;
211
212 if (i > INT_MAX/4)
213 goto err;
214
215 num = i + neg;
216 if (bn == NULL)
217 return (num);
218
219 /*
220 * a is the start of the digits, and it is 'i' long. We chop it into
221 * BN_DEC_NUM digits at a time
222 */
223 if (*bn == NULL) {
224 if ((ret = BN_new()) == NULL)
225 return (0);
226 } else {
227 ret = *bn;
228 BN_zero(ret);
229 }
230
231 /* i is the number of digits, a bit of an over expand */
232 if (bn_expand(ret, i * 4) == NULL)
233 goto err;
234
235 j = BN_DEC_NUM - (i % BN_DEC_NUM);
236 if (j == BN_DEC_NUM)
237 j = 0;
238 l = 0;
239 while (*a) {
240 l *= 10;
241 l += *a - '0';
242 a++;
243 if (++j == BN_DEC_NUM) {
244 BN_mul_word(ret, BN_DEC_CONV);
245 BN_add_word(ret, l);
246 l = 0;
247 j = 0;
248 }
249 }
250 ret->neg = neg;
251
252 bn_correct_top(ret);
253 *bn = ret;
254 bn_check_top(ret);
255 return (num);
256 err:
257 if (*bn == NULL)
258 BN_free(ret);
259 return (0);
260 }
261
262 int BN_asc2bn(BIGNUM **bn, const char *a)
263 {
264 const char *p = a;
265 if (*p == '-')
266 p++;
267
268 if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
269 if (!BN_hex2bn(bn, p + 2))
270 return 0;
271 } else {
272 if (!BN_dec2bn(bn, p))
273 return 0;
274 }
275 if (*a == '-')
276 (*bn)->neg = 1;
277 return 1;
278 }
279
280 # ifndef OPENSSL_NO_STDIO
281 int BN_print_fp(FILE *fp, const BIGNUM *a)
282 {
283 BIO *b;
284 int ret;
285
286 if ((b = BIO_new(BIO_s_file())) == NULL)
287 return (0);
288 BIO_set_fp(b, fp, BIO_NOCLOSE);
289 ret = BN_print(b, a);
290 BIO_free(b);
291 return (ret);
292 }
293 # endif
294
295 int BN_print(BIO *bp, const BIGNUM *a)
296 {
297 int i, j, v, z = 0;
298 int ret = 0;
299
300 if ((a->neg) && (BIO_write(bp, "-", 1) != 1))
301 goto end;
302 if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))
303 goto end;
304 for (i = a->top - 1; i >= 0; i--) {
305 for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
306 /* strip leading zeros */
307 v = ((int)(a->d[i] >> (long)j)) & 0x0f;
308 if (z || (v != 0)) {
309 if (BIO_write(bp, &(Hex[v]), 1) != 1)
310 goto end;
311 z = 1;
312 }
313 }
314 }
315 ret = 1;
316 end:
317 return (ret);
318 }
319
320 char *BN_options(void)
321 {
322 static int init = 0;
323 static char data[16];
324
325 if (!init) {
326 init++;
327 #ifdef BN_LLONG
328 BIO_snprintf(data, sizeof data, "bn(%d,%d)",
329 (int)sizeof(BN_ULLONG) * 8, (int)sizeof(BN_ULONG) * 8);
330 #else
331 BIO_snprintf(data, sizeof data, "bn(%d,%d)",
332 (int)sizeof(BN_ULONG) * 8, (int)sizeof(BN_ULONG) * 8);
333 #endif
334 }
335 return (data);
336 }