summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_print.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_print.c')
-rw-r--r--src/lib/libcrypto/bn/bn_print.c436
1 files changed, 225 insertions, 211 deletions
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c
index 49efbcb9a9..c09c64db25 100644
--- a/src/lib/libcrypto/bn/bn_print.c
+++ b/src/lib/libcrypto/bn/bn_print.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -65,314 +65,328 @@
65static const char Hex[]="0123456789ABCDEF"; 65static const char Hex[]="0123456789ABCDEF";
66 66
67/* Must 'free' the returned data */ 67/* Must 'free' the returned data */
68char *BN_bn2hex(const BIGNUM *a) 68char *
69 { 69BN_bn2hex(const BIGNUM *a)
70 int i,j,v,z=0; 70{
71 int i, j, v, z = 0;
71 char *buf; 72 char *buf;
72 char *p; 73 char *p;
73 74
74 buf=(char *)malloc(a->top*BN_BYTES*2+2); 75 buf = (char *)malloc(a->top * BN_BYTES * 2 + 2);
75 if (buf == NULL) 76 if (buf == NULL) {
76 { 77 BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
77 BNerr(BN_F_BN_BN2HEX,ERR_R_MALLOC_FAILURE);
78 goto err; 78 goto err;
79 } 79 }
80 p=buf; 80 p = buf;
81 if (a->neg) *(p++)='-'; 81 if (a->neg)
82 if (BN_is_zero(a)) *(p++)='0'; 82 *(p++) = '-';
83 for (i=a->top-1; i >=0; i--) 83 if (BN_is_zero(a))
84 { 84 *(p++) = '0';
85 for (j=BN_BITS2-8; j >= 0; j-=8) 85 for (i = a->top - 1; i >=0; i--) {
86 { 86 for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
87 /* strip leading zeros */ 87 /* strip leading zeros */
88 v=((int)(a->d[i]>>(long)j))&0xff; 88 v = ((int)(a->d[i] >> (long)j)) & 0xff;
89 if (z || (v != 0)) 89 if (z || (v != 0)) {
90 { 90 *(p++) = Hex[v >> 4];
91 *(p++)=Hex[v>>4]; 91 *(p++) = Hex[v & 0x0f];
92 *(p++)=Hex[v&0x0f]; 92 z = 1;
93 z=1;
94 }
95 } 93 }
96 } 94 }
97 *p='\0';
98err:
99 return(buf);
100 } 95 }
96 *p = '\0';
97
98err:
99 return (buf);
100}
101 101
102/* Must 'free' the returned data */ 102/* Must 'free' the returned data */
103char *BN_bn2dec(const BIGNUM *a) 103char *
104 { 104BN_bn2dec(const BIGNUM *a)
105 int i=0,num, ok = 0; 105{
106 char *buf=NULL; 106 int i = 0, num, ok = 0;
107 char *buf = NULL;
107 char *p; 108 char *p;
108 BIGNUM *t=NULL; 109 BIGNUM *t = NULL;
109 BN_ULONG *bn_data=NULL,*lp; 110 BN_ULONG *bn_data = NULL, *lp;
110 111
111 /* get an upper bound for the length of the decimal integer 112 /* get an upper bound for the length of the decimal integer
112 * num <= (BN_num_bits(a) + 1) * log(2) 113 * num <= (BN_num_bits(a) + 1) * log(2)
113 * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error) 114 * <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1 (rounding error)
114 * <= BN_num_bits(a)/10 + BN_num_bits/1000 + 1 + 1 115 * <= BN_num_bits(a)/10 + BN_num_bits/1000 + 1 + 1
115 */ 116 */
116 i=BN_num_bits(a)*3; 117 i = BN_num_bits(a) * 3;
117 num=(i/10+i/1000+1)+1; 118 num = (i / 10 + i / 1000 + 1) + 1;
118 bn_data=(BN_ULONG *)malloc((num/BN_DEC_NUM+1)*sizeof(BN_ULONG)); 119 bn_data = (BN_ULONG *)malloc((num / BN_DEC_NUM + 1) * sizeof(BN_ULONG));
119 buf=(char *)malloc(num+3); 120 buf = (char *)malloc(num + 3);
120 if ((buf == NULL) || (bn_data == NULL)) 121 if ((buf == NULL) || (bn_data == NULL)) {
121 { 122 BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
122 BNerr(BN_F_BN_BN2DEC,ERR_R_MALLOC_FAILURE); 123 goto err;
124 }
125 if ((t = BN_dup(a)) == NULL)
123 goto err; 126 goto err;
124 }
125 if ((t=BN_dup(a)) == NULL) goto err;
126 127
127#define BUF_REMAIN (num+3 - (size_t)(p - buf)) 128#define BUF_REMAIN (num+3 - (size_t)(p - buf))
128 p=buf; 129 p = buf;
129 lp=bn_data; 130 lp = bn_data;
130 if (BN_is_zero(t)) 131 if (BN_is_zero(t)) {
131 { 132 *(p++) = '0';
132 *(p++)='0'; 133 *(p++) = '\0';
133 *(p++)='\0'; 134 } else {
134 }
135 else
136 {
137 if (BN_is_negative(t)) 135 if (BN_is_negative(t))
138 *p++ = '-'; 136 *p++ = '-';
139 137
140 i=0; 138 i = 0;
141 while (!BN_is_zero(t)) 139 while (!BN_is_zero(t)) {
142 { 140 *lp = BN_div_word(t, BN_DEC_CONV);
143 *lp=BN_div_word(t,BN_DEC_CONV);
144 lp++; 141 lp++;
145 } 142 }
146 lp--; 143 lp--;
147 /* We now have a series of blocks, BN_DEC_NUM chars 144 /* We now have a series of blocks, BN_DEC_NUM chars
148 * in length, where the last one needs truncation. 145 * in length, where the last one needs truncation.
149 * The blocks need to be reversed in order. */ 146 * The blocks need to be reversed in order. */
150 snprintf(p,BUF_REMAIN,BN_DEC_FMT1,*lp); 147 snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);
151 while (*p) p++; 148 while (*p)
152 while (lp != bn_data) 149 p++;
153 { 150 while (lp != bn_data) {
154 lp--; 151 lp--;
155 snprintf(p,BUF_REMAIN,BN_DEC_FMT2,*lp); 152 snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);
156 while (*p) p++; 153 while (*p)
157 } 154 p++;
158 } 155 }
156 }
159 ok = 1; 157 ok = 1;
158
160err: 159err:
161 if (bn_data != NULL) free(bn_data); 160 if (bn_data != NULL)
162 if (t != NULL) BN_free(t); 161 free(bn_data);
163 if (!ok && buf) 162 if (t != NULL)
164 { 163 BN_free(t);
164 if (!ok && buf) {
165 free(buf); 165 free(buf);
166 buf = NULL; 166 buf = NULL;
167 }
168
169 return(buf);
170 } 167 }
171 168
172int BN_hex2bn(BIGNUM **bn, const char *a) 169 return (buf);
173 { 170}
174 BIGNUM *ret=NULL; 171
175 BN_ULONG l=0; 172int
176 int neg=0,h,m,i,j,k,c; 173BN_hex2bn(BIGNUM **bn, const char *a)
174{
175 BIGNUM *ret = NULL;
176 BN_ULONG l = 0;
177 int neg = 0, h, m, i,j, k, c;
177 int num; 178 int num;
178 179
179 if ((a == NULL) || (*a == '\0')) return(0); 180 if ((a == NULL) || (*a == '\0'))
181 return (0);
180 182
181 if (*a == '-') { neg=1; a++; } 183 if (*a == '-') {
184 neg = 1;
185 a++;
186 }
182 187
183 for (i=0; isxdigit((unsigned char) a[i]); i++) 188 for (i = 0; isxdigit((unsigned char)a[i]); i++)
184 ; 189 ;
185 190
186 num=i+neg; 191 num = i + neg;
187 if (bn == NULL) return(num); 192 if (bn == NULL)
193 return (num);
188 194
189 /* a is the start of the hex digits, and it is 'i' long */ 195 /* a is the start of the hex digits, and it is 'i' long */
190 if (*bn == NULL) 196 if (*bn == NULL) {
191 { 197 if ((ret = BN_new()) == NULL)
192 if ((ret=BN_new()) == NULL) return(0); 198 return (0);
193 } 199 } else {
194 else
195 {
196 ret= *bn; 200 ret= *bn;
197 BN_zero(ret); 201 BN_zero(ret);
198 } 202 }
199 203
200 /* i is the number of hex digests; */ 204 /* i is the number of hex digests; */
201 if (bn_expand(ret,i*4) == NULL) goto err; 205 if (bn_expand(ret, i * 4) == NULL)
202 206 goto err;
203 j=i; /* least significant 'hex' */ 207
204 m=0; 208 j = i; /* least significant 'hex' */
205 h=0; 209 m = 0;
206 while (j > 0) 210 h = 0;
207 { 211 while (j > 0) {
208 m=((BN_BYTES*2) <= j)?(BN_BYTES*2):j; 212 m = ((BN_BYTES*2) <= j) ? (BN_BYTES * 2) : j;
209 l=0; 213 l = 0;
210 for (;;) 214 for (;;) {
211 { 215 c = a[j - m];
212 c=a[j-m]; 216 if ((c >= '0') && (c <= '9'))
213 if ((c >= '0') && (c <= '9')) k=c-'0'; 217 k = c - '0';
214 else if ((c >= 'a') && (c <= 'f')) k=c-'a'+10; 218 else if ((c >= 'a') && (c <= 'f'))
215 else if ((c >= 'A') && (c <= 'F')) k=c-'A'+10; 219 k = c - 'a' + 10;
216 else k=0; /* paranoia */ 220 else if ((c >= 'A') && (c <= 'F'))
217 l=(l<<4)|k; 221 k = c - 'A' + 10;
218 222 else
219 if (--m <= 0) 223 k = 0; /* paranoia */
220 { 224 l = (l << 4) | k;
221 ret->d[h++]=l; 225
226 if (--m <= 0) {
227 ret->d[h++] = l;
222 break; 228 break;
223 }
224 } 229 }
225 j-=(BN_BYTES*2);
226 } 230 }
227 ret->top=h; 231 j -= (BN_BYTES * 2);
232 }
233 ret->top = h;
228 bn_correct_top(ret); 234 bn_correct_top(ret);
229 ret->neg=neg; 235 ret->neg = neg;
230 236
231 *bn=ret; 237 *bn = ret;
232 bn_check_top(ret); 238 bn_check_top(ret);
233 return(num); 239 return (num);
234err:
235 if (*bn == NULL) BN_free(ret);
236 return(0);
237 }
238 240
239int BN_dec2bn(BIGNUM **bn, const char *a) 241err:
240 { 242 if (*bn == NULL)
241 BIGNUM *ret=NULL; 243 BN_free(ret);
242 BN_ULONG l=0; 244 return (0);
243 int neg=0,i,j; 245}
246
247int
248BN_dec2bn(BIGNUM **bn, const char *a)
249{
250 BIGNUM *ret = NULL;
251 BN_ULONG l = 0;
252 int neg = 0, i, j;
244 int num; 253 int num;
245 254
246 if ((a == NULL) || (*a == '\0')) return(0); 255 if ((a == NULL) || (*a == '\0'))
247 if (*a == '-') { neg=1; a++; } 256 return (0);
257 if (*a == '-') {
258 neg = 1;
259 a++;
260 }
248 261
249 for (i=0; isdigit((unsigned char) a[i]); i++) 262 for (i = 0; isdigit((unsigned char)a[i]); i++)
250 ; 263 ;
251 264
252 num=i+neg; 265 num = i + neg;
253 if (bn == NULL) return(num); 266 if (bn == NULL)
267 return (num);
254 268
255 /* a is the start of the digits, and it is 'i' long. 269 /* a is the start of the digits, and it is 'i' long.
256 * We chop it into BN_DEC_NUM digits at a time */ 270 * We chop it into BN_DEC_NUM digits at a time */
257 if (*bn == NULL) 271 if (*bn == NULL) {
258 { 272 if ((ret = BN_new()) == NULL)
259 if ((ret=BN_new()) == NULL) return(0); 273 return (0);
260 } 274 } else {
261 else 275 ret = *bn;
262 {
263 ret= *bn;
264 BN_zero(ret); 276 BN_zero(ret);
265 } 277 }
266 278
267 /* i is the number of digests, a bit of an over expand; */ 279 /* i is the number of digests, a bit of an over expand; */
268 if (bn_expand(ret,i*4) == NULL) goto err; 280 if (bn_expand(ret, i * 4) == NULL)
269 281 goto err;
270 j=BN_DEC_NUM-(i%BN_DEC_NUM); 282
271 if (j == BN_DEC_NUM) j=0; 283 j = BN_DEC_NUM - (i % BN_DEC_NUM);
272 l=0; 284 if (j == BN_DEC_NUM)
273 while (*a) 285 j = 0;
274 { 286 l = 0;
275 l*=10; 287 while (*a) {
276 l+= *a-'0'; 288 l *= 10;
289 l += *a - '0';
277 a++; 290 a++;
278 if (++j == BN_DEC_NUM) 291 if (++j == BN_DEC_NUM) {
279 { 292 BN_mul_word(ret, BN_DEC_CONV);
280 BN_mul_word(ret,BN_DEC_CONV); 293 BN_add_word(ret, l);
281 BN_add_word(ret,l); 294 l = 0;
282 l=0; 295 j = 0;
283 j=0;
284 }
285 } 296 }
286 ret->neg=neg; 297 }
298 ret->neg = neg;
287 299
288 bn_correct_top(ret); 300 bn_correct_top(ret);
289 *bn=ret; 301 *bn = ret;
290 bn_check_top(ret); 302 bn_check_top(ret);
291 return(num); 303 return (num);
304
292err: 305err:
293 if (*bn == NULL) BN_free(ret); 306 if (*bn == NULL)
294 return(0); 307 BN_free(ret);
295 } 308 return (0);
309}
296 310
297int BN_asc2bn(BIGNUM **bn, const char *a) 311int
298 { 312BN_asc2bn(BIGNUM **bn, const char *a)
313{
299 const char *p = a; 314 const char *p = a;
300 if (*p == '-') 315 if (*p == '-')
301 p++; 316 p++;
302 317
303 if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) 318 if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
304 {
305 if (!BN_hex2bn(bn, p + 2)) 319 if (!BN_hex2bn(bn, p + 2))
306 return 0; 320 return 0;
307 } 321 } else {
308 else
309 {
310 if (!BN_dec2bn(bn, p)) 322 if (!BN_dec2bn(bn, p))
311 return 0; 323 return 0;
312 } 324 }
313 if (*a == '-') 325 if (*a == '-')
314 (*bn)->neg = 1; 326 (*bn)->neg = 1;
315 return 1; 327 return 1;
316 } 328}
317 329
318#ifndef OPENSSL_NO_BIO 330#ifndef OPENSSL_NO_BIO
319#ifndef OPENSSL_NO_FP_API 331#ifndef OPENSSL_NO_FP_API
320int BN_print_fp(FILE *fp, const BIGNUM *a) 332int
321 { 333BN_print_fp(FILE *fp, const BIGNUM *a)
334{
322 BIO *b; 335 BIO *b;
323 int ret; 336 int ret;
324 337
325 if ((b=BIO_new(BIO_s_file())) == NULL) 338 if ((b = BIO_new(BIO_s_file())) == NULL)
326 return(0); 339 return (0);
327 BIO_set_fp(b,fp,BIO_NOCLOSE); 340 BIO_set_fp(b, fp, BIO_NOCLOSE);
328 ret=BN_print(b,a); 341 ret = BN_print(b, a);
329 BIO_free(b); 342 BIO_free(b);
330 return(ret); 343 return (ret);
331 } 344}
332#endif 345#endif
333 346
334int BN_print(BIO *bp, const BIGNUM *a) 347int
335 { 348BN_print(BIO *bp, const BIGNUM *a)
336 int i,j,v,z=0; 349{
337 int ret=0; 350 int i, j, v, z = 0;
338 351 int ret = 0;
339 if ((a->neg) && (BIO_write(bp,"-",1) != 1)) goto end; 352
340 if (BN_is_zero(a) && (BIO_write(bp,"0",1) != 1)) goto end; 353 if ((a->neg) && (BIO_write(bp, "-", 1) != 1))
341 for (i=a->top-1; i >=0; i--) 354 goto end;
342 { 355 if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))
343 for (j=BN_BITS2-4; j >= 0; j-=4) 356 goto end;
344 { 357 for (i = a->top - 1; i >= 0; i--) {
358 for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
345 /* strip leading zeros */ 359 /* strip leading zeros */
346 v=((int)(a->d[i]>>(long)j))&0x0f; 360 v = ((int)(a->d[i] >> (long)j)) & 0x0f;
347 if (z || (v != 0)) 361 if (z || (v != 0)) {
348 { 362 if (BIO_write(bp, &(Hex[v]), 1) != 1)
349 if (BIO_write(bp,&(Hex[v]),1) != 1)
350 goto end; 363 goto end;
351 z=1; 364 z = 1;
352 }
353 } 365 }
354 } 366 }
355 ret=1;
356end:
357 return(ret);
358 } 367 }
368 ret = 1;
369
370end:
371 return (ret);
372}
359#endif 373#endif
360 374
361char *BN_options(void) 375char *
362 { 376BN_options(void)
363 static int init=0; 377{
378 static int init = 0;
364 static char data[16]; 379 static char data[16];
365 380
366 if (!init) 381 if (!init) {
367 {
368 init++; 382 init++;
369#ifdef BN_LLONG 383#ifdef BN_LLONG
370 snprintf(data,sizeof data,"bn(%d,%d)", 384 snprintf(data,sizeof data, "bn(%d,%d)",
371 (int)sizeof(BN_ULLONG)*8,(int)sizeof(BN_ULONG)*8); 385 (int)sizeof(BN_ULLONG) * 8, (int)sizeof(BN_ULONG) * 8);
372#else 386#else
373 snprintf(data,sizeof data,"bn(%d,%d)", 387 snprintf(data,sizeof data, "bn(%d,%d)",
374 (int)sizeof(BN_ULONG)*8,(int)sizeof(BN_ULONG)*8); 388 (int)sizeof(BN_ULONG) * 8, (int)sizeof(BN_ULONG) * 8);
375#endif 389#endif
376 }
377 return(data);
378 } 390 }
391 return (data);
392}