diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_print.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_print.c | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c index acba7ed7ee..810dde34e1 100644 --- a/src/lib/libcrypto/bn/bn_print.c +++ b/src/lib/libcrypto/bn/bn_print.c | |||
@@ -62,7 +62,7 @@ | |||
62 | #include <openssl/buffer.h> | 62 | #include <openssl/buffer.h> |
63 | #include "bn_lcl.h" | 63 | #include "bn_lcl.h" |
64 | 64 | ||
65 | static const char *Hex="0123456789ABCDEF"; | 65 | static const char Hex[]="0123456789ABCDEF"; |
66 | 66 | ||
67 | /* Must 'OPENSSL_free' the returned data */ | 67 | /* Must 'OPENSSL_free' the returned data */ |
68 | char *BN_bn2hex(const BIGNUM *a) | 68 | char *BN_bn2hex(const BIGNUM *a) |
@@ -102,14 +102,19 @@ err: | |||
102 | /* Must 'OPENSSL_free' the returned data */ | 102 | /* Must 'OPENSSL_free' the returned data */ |
103 | char *BN_bn2dec(const BIGNUM *a) | 103 | char *BN_bn2dec(const BIGNUM *a) |
104 | { | 104 | { |
105 | int i=0,num; | 105 | int i=0,num, ok = 0; |
106 | char *buf=NULL; | 106 | char *buf=NULL; |
107 | char *p; | 107 | char *p; |
108 | BIGNUM *t=NULL; | 108 | BIGNUM *t=NULL; |
109 | BN_ULONG *bn_data=NULL,*lp; | 109 | BN_ULONG *bn_data=NULL,*lp; |
110 | 110 | ||
111 | /* get an upper bound for the length of the decimal integer | ||
112 | * num <= (BN_num_bits(a) + 1) * log(2) | ||
113 | * <= 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 | */ | ||
111 | i=BN_num_bits(a)*3; | 116 | i=BN_num_bits(a)*3; |
112 | num=(i/10+i/1000+3)+1; | 117 | num=(i/10+i/1000+1)+1; |
113 | bn_data=(BN_ULONG *)OPENSSL_malloc((num/BN_DEC_NUM+1)*sizeof(BN_ULONG)); | 118 | bn_data=(BN_ULONG *)OPENSSL_malloc((num/BN_DEC_NUM+1)*sizeof(BN_ULONG)); |
114 | buf=(char *)OPENSSL_malloc(num+3); | 119 | buf=(char *)OPENSSL_malloc(num+3); |
115 | if ((buf == NULL) || (bn_data == NULL)) | 120 | if ((buf == NULL) || (bn_data == NULL)) |
@@ -122,7 +127,6 @@ char *BN_bn2dec(const BIGNUM *a) | |||
122 | #define BUF_REMAIN (num+3 - (size_t)(p - buf)) | 127 | #define BUF_REMAIN (num+3 - (size_t)(p - buf)) |
123 | p=buf; | 128 | p=buf; |
124 | lp=bn_data; | 129 | lp=bn_data; |
125 | if (t->neg) *(p++)='-'; | ||
126 | if (BN_is_zero(t)) | 130 | if (BN_is_zero(t)) |
127 | { | 131 | { |
128 | *(p++)='0'; | 132 | *(p++)='0'; |
@@ -130,6 +134,9 @@ char *BN_bn2dec(const BIGNUM *a) | |||
130 | } | 134 | } |
131 | else | 135 | else |
132 | { | 136 | { |
137 | if (BN_is_negative(t)) | ||
138 | *p++ = '-'; | ||
139 | |||
133 | i=0; | 140 | i=0; |
134 | while (!BN_is_zero(t)) | 141 | while (!BN_is_zero(t)) |
135 | { | 142 | { |
@@ -149,9 +156,16 @@ char *BN_bn2dec(const BIGNUM *a) | |||
149 | while (*p) p++; | 156 | while (*p) p++; |
150 | } | 157 | } |
151 | } | 158 | } |
159 | ok = 1; | ||
152 | err: | 160 | err: |
153 | if (bn_data != NULL) OPENSSL_free(bn_data); | 161 | if (bn_data != NULL) OPENSSL_free(bn_data); |
154 | if (t != NULL) BN_free(t); | 162 | if (t != NULL) BN_free(t); |
163 | if (!ok && buf) | ||
164 | { | ||
165 | OPENSSL_free(buf); | ||
166 | buf = NULL; | ||
167 | } | ||
168 | |||
155 | return(buf); | 169 | return(buf); |
156 | } | 170 | } |
157 | 171 | ||
@@ -211,10 +225,11 @@ int BN_hex2bn(BIGNUM **bn, const char *a) | |||
211 | j-=(BN_BYTES*2); | 225 | j-=(BN_BYTES*2); |
212 | } | 226 | } |
213 | ret->top=h; | 227 | ret->top=h; |
214 | bn_fix_top(ret); | 228 | bn_correct_top(ret); |
215 | ret->neg=neg; | 229 | ret->neg=neg; |
216 | 230 | ||
217 | *bn=ret; | 231 | *bn=ret; |
232 | bn_check_top(ret); | ||
218 | return(num); | 233 | return(num); |
219 | err: | 234 | err: |
220 | if (*bn == NULL) BN_free(ret); | 235 | if (*bn == NULL) BN_free(ret); |
@@ -270,8 +285,9 @@ int BN_dec2bn(BIGNUM **bn, const char *a) | |||
270 | } | 285 | } |
271 | ret->neg=neg; | 286 | ret->neg=neg; |
272 | 287 | ||
273 | bn_fix_top(ret); | 288 | bn_correct_top(ret); |
274 | *bn=ret; | 289 | *bn=ret; |
290 | bn_check_top(ret); | ||
275 | return(num); | 291 | return(num); |
276 | err: | 292 | err: |
277 | if (*bn == NULL) BN_free(ret); | 293 | if (*bn == NULL) BN_free(ret); |
@@ -300,7 +316,7 @@ int BN_print(BIO *bp, const BIGNUM *a) | |||
300 | int ret=0; | 316 | int ret=0; |
301 | 317 | ||
302 | if ((a->neg) && (BIO_write(bp,"-",1) != 1)) goto end; | 318 | if ((a->neg) && (BIO_write(bp,"-",1) != 1)) goto end; |
303 | if ((BN_is_zero(a)) && (BIO_write(bp,"0",1) != 1)) goto end; | 319 | if (BN_is_zero(a) && (BIO_write(bp,"0",1) != 1)) goto end; |
304 | for (i=a->top-1; i >=0; i--) | 320 | for (i=a->top-1; i >=0; i--) |
305 | { | 321 | { |
306 | for (j=BN_BITS2-4; j >= 0; j-=4) | 322 | for (j=BN_BITS2-4; j >= 0; j-=4) |
@@ -320,14 +336,3 @@ end: | |||
320 | return(ret); | 336 | return(ret); |
321 | } | 337 | } |
322 | #endif | 338 | #endif |
323 | |||
324 | #ifdef BN_DEBUG | ||
325 | void bn_dump1(FILE *o, const char *a, const BN_ULONG *b,int n) | ||
326 | { | ||
327 | int i; | ||
328 | fprintf(o, "%s=", a); | ||
329 | for (i=n-1;i>=0;i--) | ||
330 | fprintf(o, "%08lX", b[i]); /* assumes 32-bit BN_ULONG */ | ||
331 | fprintf(o, "\n"); | ||
332 | } | ||
333 | #endif | ||