diff options
Diffstat (limited to 'src/lib/libcrypto/bn')
-rw-r--r-- | src/lib/libcrypto/bn/Makefile.ssl | 3 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/asm/mips3.s | 8 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/asm/pa-risc2.s | 2 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/asm/pa-risc2W.s | 2 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn.h | 8 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_div.c | 7 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_err.c | 3 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_lib.c | 7 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_rand.c | 107 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_shift.c | 5 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bntest.c | 64 |
11 files changed, 158 insertions, 58 deletions
diff --git a/src/lib/libcrypto/bn/Makefile.ssl b/src/lib/libcrypto/bn/Makefile.ssl index 17b72d577f..526d7adb5c 100644 --- a/src/lib/libcrypto/bn/Makefile.ssl +++ b/src/lib/libcrypto/bn/Makefile.ssl | |||
@@ -68,7 +68,8 @@ bnbug: bnbug.c ../../libcrypto.a top | |||
68 | 68 | ||
69 | lib: $(LIBOBJ) | 69 | lib: $(LIBOBJ) |
70 | $(AR) $(LIB) $(LIBOBJ) | 70 | $(AR) $(LIB) $(LIBOBJ) |
71 | $(RANLIB) $(LIB) | 71 | @echo You may get an error following this line. Please ignore. |
72 | - $(RANLIB) $(LIB) | ||
72 | @touch lib | 73 | @touch lib |
73 | 74 | ||
74 | # elf | 75 | # elf |
diff --git a/src/lib/libcrypto/bn/asm/mips3.s b/src/lib/libcrypto/bn/asm/mips3.s index 2df4dcd4b0..45786c00a5 100644 --- a/src/lib/libcrypto/bn/asm/mips3.s +++ b/src/lib/libcrypto/bn/asm/mips3.s | |||
@@ -586,13 +586,13 @@ LEAF(bn_div_3_words) | |||
586 | ld a0,(a3) | 586 | ld a0,(a3) |
587 | move ta2,a1 | 587 | move ta2,a1 |
588 | ld a1,-8(a3) | 588 | ld a1,-8(a3) |
589 | move ta3,ra | 589 | bne a0,a2,.L_bn_div_3_words_proceed |
590 | move v1,zero | ||
591 | li v0,-1 | 590 | li v0,-1 |
592 | beq a0,a2,.L_bn_div_3_words_skip_div | 591 | jr ra |
592 | .L_bn_div_3_words_proceed: | ||
593 | move ta3,ra | ||
593 | bal bn_div_words | 594 | bal bn_div_words |
594 | move ra,ta3 | 595 | move ra,ta3 |
595 | .L_bn_div_3_words_skip_div: | ||
596 | dmultu ta2,v0 | 596 | dmultu ta2,v0 |
597 | ld t2,-16(a3) | 597 | ld t2,-16(a3) |
598 | move ta0,zero | 598 | move ta0,zero |
diff --git a/src/lib/libcrypto/bn/asm/pa-risc2.s b/src/lib/libcrypto/bn/asm/pa-risc2.s index 7239aa2c76..af9730d062 100644 --- a/src/lib/libcrypto/bn/asm/pa-risc2.s +++ b/src/lib/libcrypto/bn/asm/pa-risc2.s | |||
@@ -1611,7 +1611,7 @@ bn_mul_comba4 | |||
1611 | .IMPORT $global$,DATA | 1611 | .IMPORT $global$,DATA |
1612 | .SPACE $TEXT$ | 1612 | .SPACE $TEXT$ |
1613 | .SUBSPA $CODE$ | 1613 | .SUBSPA $CODE$ |
1614 | .SUBSPA $LIT$,QUAD=0,ALIGN=8,ACCESS=0x2c,SORT=16 | 1614 | .SUBSPA $LIT$,ACCESS=0x2c |
1615 | C$7 | 1615 | C$7 |
1616 | .ALIGN 8 | 1616 | .ALIGN 8 |
1617 | .STRINGZ "Division would overflow (%d)\n" | 1617 | .STRINGZ "Division would overflow (%d)\n" |
diff --git a/src/lib/libcrypto/bn/asm/pa-risc2W.s b/src/lib/libcrypto/bn/asm/pa-risc2W.s index 54b6606252..a99545754d 100644 --- a/src/lib/libcrypto/bn/asm/pa-risc2W.s +++ b/src/lib/libcrypto/bn/asm/pa-risc2W.s | |||
@@ -1598,7 +1598,7 @@ bn_mul_comba4 | |||
1598 | .IMPORT $global$,DATA | 1598 | .IMPORT $global$,DATA |
1599 | .SPACE $TEXT$ | 1599 | .SPACE $TEXT$ |
1600 | .SUBSPA $CODE$ | 1600 | .SUBSPA $CODE$ |
1601 | .SUBSPA $LIT$,QUAD=0,ALIGN=8,ACCESS=0x2c,SORT=16 | 1601 | .SUBSPA $LIT$,ACCESS=0x2c |
1602 | C$4 | 1602 | C$4 |
1603 | .ALIGN 8 | 1603 | .ALIGN 8 |
1604 | .STRINGZ "Division would overflow (%d)\n" | 1604 | .STRINGZ "Division would overflow (%d)\n" |
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h index 1eb8395b25..b232c2ceae 100644 --- a/src/lib/libcrypto/bn/bn.h +++ b/src/lib/libcrypto/bn/bn.h | |||
@@ -239,7 +239,7 @@ typedef struct bignum_st | |||
239 | } BIGNUM; | 239 | } BIGNUM; |
240 | 240 | ||
241 | /* Used for temp variables */ | 241 | /* Used for temp variables */ |
242 | #define BN_CTX_NUM 12 | 242 | #define BN_CTX_NUM 16 |
243 | #define BN_CTX_NUM_POS 12 | 243 | #define BN_CTX_NUM_POS 12 |
244 | typedef struct bignum_ctx | 244 | typedef struct bignum_ctx |
245 | { | 245 | { |
@@ -328,6 +328,7 @@ BIGNUM *BN_CTX_get(BN_CTX *ctx); | |||
328 | void BN_CTX_end(BN_CTX *ctx); | 328 | void BN_CTX_end(BN_CTX *ctx); |
329 | int BN_rand(BIGNUM *rnd, int bits, int top,int bottom); | 329 | int BN_rand(BIGNUM *rnd, int bits, int top,int bottom); |
330 | int BN_pseudo_rand(BIGNUM *rnd, int bits, int top,int bottom); | 330 | int BN_pseudo_rand(BIGNUM *rnd, int bits, int top,int bottom); |
331 | int BN_rand_range(BIGNUM *rnd, BIGNUM *range); | ||
331 | int BN_num_bits(const BIGNUM *a); | 332 | int BN_num_bits(const BIGNUM *a); |
332 | int BN_num_bits_word(BN_ULONG); | 333 | int BN_num_bits_word(BN_ULONG); |
333 | BIGNUM *BN_new(void); | 334 | BIGNUM *BN_new(void); |
@@ -467,6 +468,8 @@ BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num); | |||
467 | # define bn_dump(a,b) | 468 | # define bn_dump(a,b) |
468 | #endif | 469 | #endif |
469 | 470 | ||
471 | int BN_bntest_rand(BIGNUM *rnd, int bits, int top,int bottom); | ||
472 | |||
470 | /* BEGIN ERROR CODES */ | 473 | /* BEGIN ERROR CODES */ |
471 | /* The following lines are auto generated by the script mkerr.pl. Any changes | 474 | /* The following lines are auto generated by the script mkerr.pl. Any changes |
472 | * made after this point may be overwritten when the script is next run. | 475 | * made after this point may be overwritten when the script is next run. |
@@ -493,16 +496,19 @@ BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num); | |||
493 | #define BN_F_BN_MPI2BN 112 | 496 | #define BN_F_BN_MPI2BN 112 |
494 | #define BN_F_BN_NEW 113 | 497 | #define BN_F_BN_NEW 113 |
495 | #define BN_F_BN_RAND 114 | 498 | #define BN_F_BN_RAND 114 |
499 | #define BN_F_BN_RAND_RANGE 122 | ||
496 | #define BN_F_BN_USUB 115 | 500 | #define BN_F_BN_USUB 115 |
497 | 501 | ||
498 | /* Reason codes. */ | 502 | /* Reason codes. */ |
499 | #define BN_R_ARG2_LT_ARG3 100 | 503 | #define BN_R_ARG2_LT_ARG3 100 |
500 | #define BN_R_BAD_RECIPROCAL 101 | 504 | #define BN_R_BAD_RECIPROCAL 101 |
505 | #define BN_R_BIGNUM_TOO_LONG 114 | ||
501 | #define BN_R_CALLED_WITH_EVEN_MODULUS 102 | 506 | #define BN_R_CALLED_WITH_EVEN_MODULUS 102 |
502 | #define BN_R_DIV_BY_ZERO 103 | 507 | #define BN_R_DIV_BY_ZERO 103 |
503 | #define BN_R_ENCODING_ERROR 104 | 508 | #define BN_R_ENCODING_ERROR 104 |
504 | #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105 | 509 | #define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105 |
505 | #define BN_R_INVALID_LENGTH 106 | 510 | #define BN_R_INVALID_LENGTH 106 |
511 | #define BN_R_INVALID_RANGE 115 | ||
506 | #define BN_R_NOT_INITIALIZED 107 | 512 | #define BN_R_NOT_INITIALIZED 107 |
507 | #define BN_R_NO_INVERSE 108 | 513 | #define BN_R_NO_INVERSE 108 |
508 | #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 | 514 | #define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109 |
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c index c3772c243b..c328b5b411 100644 --- a/src/lib/libcrypto/bn/bn_div.c +++ b/src/lib/libcrypto/bn/bn_div.c | |||
@@ -180,13 +180,13 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor, | |||
180 | 180 | ||
181 | BN_CTX_start(ctx); | 181 | BN_CTX_start(ctx); |
182 | tmp=BN_CTX_get(ctx); | 182 | tmp=BN_CTX_get(ctx); |
183 | tmp->neg=0; | ||
184 | snum=BN_CTX_get(ctx); | 183 | snum=BN_CTX_get(ctx); |
185 | sdiv=BN_CTX_get(ctx); | 184 | sdiv=BN_CTX_get(ctx); |
186 | if (dv == NULL) | 185 | if (dv == NULL) |
187 | res=BN_CTX_get(ctx); | 186 | res=BN_CTX_get(ctx); |
188 | else res=dv; | 187 | else res=dv; |
189 | if (res == NULL) goto err; | 188 | if (sdiv==NULL || res == NULL) goto err; |
189 | tmp->neg=0; | ||
190 | 190 | ||
191 | /* First we normalise the numbers */ | 191 | /* First we normalise the numbers */ |
192 | norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2); | 192 | norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2); |
@@ -237,7 +237,8 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor, | |||
237 | for (i=0; i<loop-1; i++) | 237 | for (i=0; i<loop-1; i++) |
238 | { | 238 | { |
239 | BN_ULONG q,l0; | 239 | BN_ULONG q,l0; |
240 | #ifdef BN_DIV3W | 240 | #if defined(BN_DIV3W) && !defined(NO_ASM) |
241 | BN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG); | ||
241 | q=bn_div_3_words(wnump,d1,d0); | 242 | q=bn_div_3_words(wnump,d1,d0); |
242 | #else | 243 | #else |
243 | BN_ULONG n0,n1,rem=0; | 244 | BN_ULONG n0,n1,rem=0; |
diff --git a/src/lib/libcrypto/bn/bn_err.c b/src/lib/libcrypto/bn/bn_err.c index 86550c4c21..adc6a214fc 100644 --- a/src/lib/libcrypto/bn/bn_err.c +++ b/src/lib/libcrypto/bn/bn_err.c | |||
@@ -84,6 +84,7 @@ static ERR_STRING_DATA BN_str_functs[]= | |||
84 | {ERR_PACK(0,BN_F_BN_MPI2BN,0), "BN_mpi2bn"}, | 84 | {ERR_PACK(0,BN_F_BN_MPI2BN,0), "BN_mpi2bn"}, |
85 | {ERR_PACK(0,BN_F_BN_NEW,0), "BN_new"}, | 85 | {ERR_PACK(0,BN_F_BN_NEW,0), "BN_new"}, |
86 | {ERR_PACK(0,BN_F_BN_RAND,0), "BN_rand"}, | 86 | {ERR_PACK(0,BN_F_BN_RAND,0), "BN_rand"}, |
87 | {ERR_PACK(0,BN_F_BN_RAND_RANGE,0), "BN_rand_range"}, | ||
87 | {ERR_PACK(0,BN_F_BN_USUB,0), "BN_usub"}, | 88 | {ERR_PACK(0,BN_F_BN_USUB,0), "BN_usub"}, |
88 | {0,NULL} | 89 | {0,NULL} |
89 | }; | 90 | }; |
@@ -92,11 +93,13 @@ static ERR_STRING_DATA BN_str_reasons[]= | |||
92 | { | 93 | { |
93 | {BN_R_ARG2_LT_ARG3 ,"arg2 lt arg3"}, | 94 | {BN_R_ARG2_LT_ARG3 ,"arg2 lt arg3"}, |
94 | {BN_R_BAD_RECIPROCAL ,"bad reciprocal"}, | 95 | {BN_R_BAD_RECIPROCAL ,"bad reciprocal"}, |
96 | {BN_R_BIGNUM_TOO_LONG ,"bignum too long"}, | ||
95 | {BN_R_CALLED_WITH_EVEN_MODULUS ,"called with even modulus"}, | 97 | {BN_R_CALLED_WITH_EVEN_MODULUS ,"called with even modulus"}, |
96 | {BN_R_DIV_BY_ZERO ,"div by zero"}, | 98 | {BN_R_DIV_BY_ZERO ,"div by zero"}, |
97 | {BN_R_ENCODING_ERROR ,"encoding error"}, | 99 | {BN_R_ENCODING_ERROR ,"encoding error"}, |
98 | {BN_R_EXPAND_ON_STATIC_BIGNUM_DATA ,"expand on static bignum data"}, | 100 | {BN_R_EXPAND_ON_STATIC_BIGNUM_DATA ,"expand on static bignum data"}, |
99 | {BN_R_INVALID_LENGTH ,"invalid length"}, | 101 | {BN_R_INVALID_LENGTH ,"invalid length"}, |
102 | {BN_R_INVALID_RANGE ,"invalid range"}, | ||
100 | {BN_R_NOT_INITIALIZED ,"not initialized"}, | 103 | {BN_R_NOT_INITIALIZED ,"not initialized"}, |
101 | {BN_R_NO_INVERSE ,"no inverse"}, | 104 | {BN_R_NO_INVERSE ,"no inverse"}, |
102 | {BN_R_TOO_MANY_TEMPORARY_VARIABLES ,"too many temporary variables"}, | 105 | {BN_R_TOO_MANY_TEMPORARY_VARIABLES ,"too many temporary variables"}, |
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c index b6b0ce4b3c..7767d65170 100644 --- a/src/lib/libcrypto/bn/bn_lib.c +++ b/src/lib/libcrypto/bn/bn_lib.c | |||
@@ -62,6 +62,7 @@ | |||
62 | #endif | 62 | #endif |
63 | 63 | ||
64 | #include <assert.h> | 64 | #include <assert.h> |
65 | #include <limits.h> | ||
65 | #include <stdio.h> | 66 | #include <stdio.h> |
66 | #include "cryptlib.h" | 67 | #include "cryptlib.h" |
67 | #include "bn_lcl.h" | 68 | #include "bn_lcl.h" |
@@ -319,6 +320,12 @@ BIGNUM *bn_expand2(BIGNUM *b, int words) | |||
319 | 320 | ||
320 | if (words > b->dmax) | 321 | if (words > b->dmax) |
321 | { | 322 | { |
323 | if (words > (INT_MAX/(4*BN_BITS2))) | ||
324 | { | ||
325 | BNerr(BN_F_BN_EXPAND2,BN_R_BIGNUM_TOO_LONG); | ||
326 | return NULL; | ||
327 | } | ||
328 | |||
322 | bn_check_top(b); | 329 | bn_check_top(b); |
323 | if (BN_get_flags(b,BN_FLG_STATIC_DATA)) | 330 | if (BN_get_flags(b,BN_FLG_STATIC_DATA)) |
324 | { | 331 | { |
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c index 21ecbc04ed..acd0619921 100644 --- a/src/lib/libcrypto/bn/bn_rand.c +++ b/src/lib/libcrypto/bn/bn_rand.c | |||
@@ -76,7 +76,7 @@ static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) | |||
76 | 76 | ||
77 | bytes=(bits+7)/8; | 77 | bytes=(bits+7)/8; |
78 | bit=(bits-1)%8; | 78 | bit=(bits-1)%8; |
79 | mask=0xff<<bit; | 79 | mask=0xff<<(bit+1); |
80 | 80 | ||
81 | buf=(unsigned char *)OPENSSL_malloc(bytes); | 81 | buf=(unsigned char *)OPENSSL_malloc(bytes); |
82 | if (buf == NULL) | 82 | if (buf == NULL) |
@@ -100,25 +100,48 @@ static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) | |||
100 | goto err; | 100 | goto err; |
101 | } | 101 | } |
102 | 102 | ||
103 | if (top) | 103 | #if 1 |
104 | if (pseudorand == 2) | ||
104 | { | 105 | { |
105 | if (bit == 0) | 106 | /* generate patterns that are more likely to trigger BN |
107 | library bugs */ | ||
108 | int i; | ||
109 | unsigned char c; | ||
110 | |||
111 | for (i = 0; i < bytes; i++) | ||
112 | { | ||
113 | RAND_pseudo_bytes(&c, 1); | ||
114 | if (c >= 128 && i > 0) | ||
115 | buf[i] = buf[i-1]; | ||
116 | else if (c < 42) | ||
117 | buf[i] = 0; | ||
118 | else if (c < 84) | ||
119 | buf[i] = 255; | ||
120 | } | ||
121 | } | ||
122 | #endif | ||
123 | |||
124 | if (top != -1) | ||
125 | { | ||
126 | if (top) | ||
106 | { | 127 | { |
107 | buf[0]=1; | 128 | if (bit == 0) |
108 | buf[1]|=0x80; | 129 | { |
130 | buf[0]=1; | ||
131 | buf[1]|=0x80; | ||
132 | } | ||
133 | else | ||
134 | { | ||
135 | buf[0]|=(3<<(bit-1)); | ||
136 | } | ||
109 | } | 137 | } |
110 | else | 138 | else |
111 | { | 139 | { |
112 | buf[0]|=(3<<(bit-1)); | 140 | buf[0]|=(1<<bit); |
113 | buf[0]&= ~(mask<<1); | ||
114 | } | 141 | } |
115 | } | 142 | } |
116 | else | 143 | buf[0] &= ~mask; |
117 | { | 144 | if (bottom) /* set bottom bit if requested */ |
118 | buf[0]|=(1<<bit); | ||
119 | buf[0]&= ~(mask<<1); | ||
120 | } | ||
121 | if (bottom) /* set bottom bits to whatever odd is */ | ||
122 | buf[bytes-1]|=1; | 145 | buf[bytes-1]|=1; |
123 | if (!BN_bin2bn(buf,bytes,rnd)) goto err; | 146 | if (!BN_bin2bn(buf,bytes,rnd)) goto err; |
124 | ret=1; | 147 | ret=1; |
@@ -140,3 +163,61 @@ int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) | |||
140 | { | 163 | { |
141 | return bnrand(1, rnd, bits, top, bottom); | 164 | return bnrand(1, rnd, bits, top, bottom); |
142 | } | 165 | } |
166 | |||
167 | #if 1 | ||
168 | int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom) | ||
169 | { | ||
170 | return bnrand(2, rnd, bits, top, bottom); | ||
171 | } | ||
172 | #endif | ||
173 | |||
174 | /* random number r: 0 <= r < range */ | ||
175 | int BN_rand_range(BIGNUM *r, BIGNUM *range) | ||
176 | { | ||
177 | int n; | ||
178 | |||
179 | if (range->neg || BN_is_zero(range)) | ||
180 | { | ||
181 | BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE); | ||
182 | return 0; | ||
183 | } | ||
184 | |||
185 | n = BN_num_bits(range); /* n > 0 */ | ||
186 | |||
187 | if (n == 1) | ||
188 | { | ||
189 | if (!BN_zero(r)) return 0; | ||
190 | } | ||
191 | else if (BN_is_bit_set(range, n - 2)) | ||
192 | { | ||
193 | do | ||
194 | { | ||
195 | /* range = 11..._2, so each iteration succeeds with probability >= .75 */ | ||
196 | if (!BN_rand(r, n, -1, 0)) return 0; | ||
197 | } | ||
198 | while (BN_cmp(r, range) >= 0); | ||
199 | } | ||
200 | else | ||
201 | { | ||
202 | /* range = 10..._2, | ||
203 | * so 3*range (= 11..._2) is exactly one bit longer than range */ | ||
204 | do | ||
205 | { | ||
206 | if (!BN_rand(r, n + 1, -1, 0)) return 0; | ||
207 | /* If r < 3*range, use r := r MOD range | ||
208 | * (which is either r, r - range, or r - 2*range). | ||
209 | * Otherwise, iterate once more. | ||
210 | * Since 3*range = 11..._2, each iteration succeeds with | ||
211 | * probability >= .75. */ | ||
212 | if (BN_cmp(r ,range) >= 0) | ||
213 | { | ||
214 | if (!BN_sub(r, r, range)) return 0; | ||
215 | if (BN_cmp(r, range) >= 0) | ||
216 | if (!BN_sub(r, r, range)) return 0; | ||
217 | } | ||
218 | } | ||
219 | while (BN_cmp(r, range) >= 0); | ||
220 | } | ||
221 | |||
222 | return 1; | ||
223 | } | ||
diff --git a/src/lib/libcrypto/bn/bn_shift.c b/src/lib/libcrypto/bn/bn_shift.c index 0883247384..c2608f9f4a 100644 --- a/src/lib/libcrypto/bn/bn_shift.c +++ b/src/lib/libcrypto/bn/bn_shift.c | |||
@@ -172,6 +172,11 @@ int BN_rshift(BIGNUM *r, BIGNUM *a, int n) | |||
172 | r->neg=a->neg; | 172 | r->neg=a->neg; |
173 | if (bn_wexpand(r,a->top-nw+1) == NULL) return(0); | 173 | if (bn_wexpand(r,a->top-nw+1) == NULL) return(0); |
174 | } | 174 | } |
175 | else | ||
176 | { | ||
177 | if (n == 0) | ||
178 | return 1; /* or the copying loop will go berserk */ | ||
179 | } | ||
175 | 180 | ||
176 | f= &(a->d[nw]); | 181 | f= &(a->d[nw]); |
177 | t=r->d; | 182 | t=r->d; |
diff --git a/src/lib/libcrypto/bn/bntest.c b/src/lib/libcrypto/bn/bntest.c index 0a97af69c5..af0c2629e8 100644 --- a/src/lib/libcrypto/bn/bntest.c +++ b/src/lib/libcrypto/bn/bntest.c | |||
@@ -107,11 +107,9 @@ static const char rnd_seed[] = "string to make the random number generator think | |||
107 | static void message(BIO *out, char *m) | 107 | static void message(BIO *out, char *m) |
108 | { | 108 | { |
109 | fprintf(stderr, "test %s\n", m); | 109 | fprintf(stderr, "test %s\n", m); |
110 | #if defined(linux) || defined(__FreeBSD__) /* can we use GNU bc features? */ | ||
111 | BIO_puts(out, "print \"test "); | 110 | BIO_puts(out, "print \"test "); |
112 | BIO_puts(out, m); | 111 | BIO_puts(out, m); |
113 | BIO_puts(out, "\\n\"\n"); | 112 | BIO_puts(out, "\\n\"\n"); |
114 | #endif | ||
115 | } | 113 | } |
116 | 114 | ||
117 | int main(int argc, char *argv[]) | 115 | int main(int argc, char *argv[]) |
@@ -122,9 +120,7 @@ int main(int argc, char *argv[]) | |||
122 | 120 | ||
123 | results = 0; | 121 | results = 0; |
124 | 122 | ||
125 | RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't | 123 | RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */ |
126 | * even check its return value | ||
127 | * (which we should) */ | ||
128 | 124 | ||
129 | argc--; | 125 | argc--; |
130 | argv++; | 126 | argv++; |
@@ -253,10 +249,10 @@ int test_add(BIO *bp) | |||
253 | BN_init(&b); | 249 | BN_init(&b); |
254 | BN_init(&c); | 250 | BN_init(&c); |
255 | 251 | ||
256 | BN_rand(&a,512,0,0); | 252 | BN_bntest_rand(&a,512,0,0); |
257 | for (i=0; i<num0; i++) | 253 | for (i=0; i<num0; i++) |
258 | { | 254 | { |
259 | BN_rand(&b,450+i,0,0); | 255 | BN_bntest_rand(&b,450+i,0,0); |
260 | a.neg=rand_neg(); | 256 | a.neg=rand_neg(); |
261 | b.neg=rand_neg(); | 257 | b.neg=rand_neg(); |
262 | if (bp == NULL) | 258 | if (bp == NULL) |
@@ -305,14 +301,14 @@ int test_sub(BIO *bp) | |||
305 | { | 301 | { |
306 | if (i < num1) | 302 | if (i < num1) |
307 | { | 303 | { |
308 | BN_rand(&a,512,0,0); | 304 | BN_bntest_rand(&a,512,0,0); |
309 | BN_copy(&b,&a); | 305 | BN_copy(&b,&a); |
310 | if (BN_set_bit(&a,i)==0) return(0); | 306 | if (BN_set_bit(&a,i)==0) return(0); |
311 | BN_add_word(&b,i); | 307 | BN_add_word(&b,i); |
312 | } | 308 | } |
313 | else | 309 | else |
314 | { | 310 | { |
315 | BN_rand(&b,400+i-num1,0,0); | 311 | BN_bntest_rand(&b,400+i-num1,0,0); |
316 | a.neg=rand_neg(); | 312 | a.neg=rand_neg(); |
317 | b.neg=rand_neg(); | 313 | b.neg=rand_neg(); |
318 | } | 314 | } |
@@ -362,13 +358,13 @@ int test_div(BIO *bp, BN_CTX *ctx) | |||
362 | { | 358 | { |
363 | if (i < num1) | 359 | if (i < num1) |
364 | { | 360 | { |
365 | BN_rand(&a,400,0,0); | 361 | BN_bntest_rand(&a,400,0,0); |
366 | BN_copy(&b,&a); | 362 | BN_copy(&b,&a); |
367 | BN_lshift(&a,&a,i); | 363 | BN_lshift(&a,&a,i); |
368 | BN_add_word(&a,i); | 364 | BN_add_word(&a,i); |
369 | } | 365 | } |
370 | else | 366 | else |
371 | BN_rand(&b,50+3*(i-num1),0,0); | 367 | BN_bntest_rand(&b,50+3*(i-num1),0,0); |
372 | a.neg=rand_neg(); | 368 | a.neg=rand_neg(); |
373 | b.neg=rand_neg(); | 369 | b.neg=rand_neg(); |
374 | if (bp == NULL) | 370 | if (bp == NULL) |
@@ -432,13 +428,13 @@ int test_div_recp(BIO *bp, BN_CTX *ctx) | |||
432 | { | 428 | { |
433 | if (i < num1) | 429 | if (i < num1) |
434 | { | 430 | { |
435 | BN_rand(&a,400,0,0); | 431 | BN_bntest_rand(&a,400,0,0); |
436 | BN_copy(&b,&a); | 432 | BN_copy(&b,&a); |
437 | BN_lshift(&a,&a,i); | 433 | BN_lshift(&a,&a,i); |
438 | BN_add_word(&a,i); | 434 | BN_add_word(&a,i); |
439 | } | 435 | } |
440 | else | 436 | else |
441 | BN_rand(&b,50+3*(i-num1),0,0); | 437 | BN_bntest_rand(&b,50+3*(i-num1),0,0); |
442 | a.neg=rand_neg(); | 438 | a.neg=rand_neg(); |
443 | b.neg=rand_neg(); | 439 | b.neg=rand_neg(); |
444 | BN_RECP_CTX_set(&recp,&b,ctx); | 440 | BN_RECP_CTX_set(&recp,&b,ctx); |
@@ -509,11 +505,11 @@ int test_mul(BIO *bp) | |||
509 | { | 505 | { |
510 | if (i <= num1) | 506 | if (i <= num1) |
511 | { | 507 | { |
512 | BN_rand(&a,100,0,0); | 508 | BN_bntest_rand(&a,100,0,0); |
513 | BN_rand(&b,100,0,0); | 509 | BN_bntest_rand(&b,100,0,0); |
514 | } | 510 | } |
515 | else | 511 | else |
516 | BN_rand(&b,i-num1,0,0); | 512 | BN_bntest_rand(&b,i-num1,0,0); |
517 | a.neg=rand_neg(); | 513 | a.neg=rand_neg(); |
518 | b.neg=rand_neg(); | 514 | b.neg=rand_neg(); |
519 | if (bp == NULL) | 515 | if (bp == NULL) |
@@ -562,7 +558,7 @@ int test_sqr(BIO *bp, BN_CTX *ctx) | |||
562 | 558 | ||
563 | for (i=0; i<num0; i++) | 559 | for (i=0; i<num0; i++) |
564 | { | 560 | { |
565 | BN_rand(&a,40+i*10,0,0); | 561 | BN_bntest_rand(&a,40+i*10,0,0); |
566 | a.neg=rand_neg(); | 562 | a.neg=rand_neg(); |
567 | if (bp == NULL) | 563 | if (bp == NULL) |
568 | for (j=0; j<100; j++) | 564 | for (j=0; j<100; j++) |
@@ -613,15 +609,15 @@ int test_mont(BIO *bp, BN_CTX *ctx) | |||
613 | 609 | ||
614 | mont=BN_MONT_CTX_new(); | 610 | mont=BN_MONT_CTX_new(); |
615 | 611 | ||
616 | BN_rand(&a,100,0,0); /**/ | 612 | BN_bntest_rand(&a,100,0,0); /**/ |
617 | BN_rand(&b,100,0,0); /**/ | 613 | BN_bntest_rand(&b,100,0,0); /**/ |
618 | for (i=0; i<num2; i++) | 614 | for (i=0; i<num2; i++) |
619 | { | 615 | { |
620 | int bits = (200*(i+1))/num2; | 616 | int bits = (200*(i+1))/num2; |
621 | 617 | ||
622 | if (bits == 0) | 618 | if (bits == 0) |
623 | continue; | 619 | continue; |
624 | BN_rand(&n,bits,0,1); | 620 | BN_bntest_rand(&n,bits,0,1); |
625 | BN_MONT_CTX_set(mont,&n,ctx); | 621 | BN_MONT_CTX_set(mont,&n,ctx); |
626 | 622 | ||
627 | BN_to_montgomery(&A,&a,mont,ctx); | 623 | BN_to_montgomery(&A,&a,mont,ctx); |
@@ -683,10 +679,10 @@ int test_mod(BIO *bp, BN_CTX *ctx) | |||
683 | d=BN_new(); | 679 | d=BN_new(); |
684 | e=BN_new(); | 680 | e=BN_new(); |
685 | 681 | ||
686 | BN_rand(a,1024,0,0); /**/ | 682 | BN_bntest_rand(a,1024,0,0); /**/ |
687 | for (i=0; i<num0; i++) | 683 | for (i=0; i<num0; i++) |
688 | { | 684 | { |
689 | BN_rand(b,450+i*10,0,0); /**/ | 685 | BN_bntest_rand(b,450+i*10,0,0); /**/ |
690 | a->neg=rand_neg(); | 686 | a->neg=rand_neg(); |
691 | b->neg=rand_neg(); | 687 | b->neg=rand_neg(); |
692 | if (bp == NULL) | 688 | if (bp == NULL) |
@@ -732,11 +728,11 @@ int test_mod_mul(BIO *bp, BN_CTX *ctx) | |||
732 | d=BN_new(); | 728 | d=BN_new(); |
733 | e=BN_new(); | 729 | e=BN_new(); |
734 | 730 | ||
735 | BN_rand(c,1024,0,0); /**/ | 731 | BN_bntest_rand(c,1024,0,0); /**/ |
736 | for (i=0; i<num0; i++) | 732 | for (i=0; i<num0; i++) |
737 | { | 733 | { |
738 | BN_rand(a,475+i*10,0,0); /**/ | 734 | BN_bntest_rand(a,475+i*10,0,0); /**/ |
739 | BN_rand(b,425+i*11,0,0); /**/ | 735 | BN_bntest_rand(b,425+i*11,0,0); /**/ |
740 | a->neg=rand_neg(); | 736 | a->neg=rand_neg(); |
741 | b->neg=rand_neg(); | 737 | b->neg=rand_neg(); |
742 | /* if (bp == NULL) | 738 | /* if (bp == NULL) |
@@ -794,11 +790,11 @@ int test_mod_exp(BIO *bp, BN_CTX *ctx) | |||
794 | d=BN_new(); | 790 | d=BN_new(); |
795 | e=BN_new(); | 791 | e=BN_new(); |
796 | 792 | ||
797 | BN_rand(c,30,0,1); /* must be odd for montgomery */ | 793 | BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */ |
798 | for (i=0; i<num2; i++) | 794 | for (i=0; i<num2; i++) |
799 | { | 795 | { |
800 | BN_rand(a,20+i*5,0,0); /**/ | 796 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
801 | BN_rand(b,2+i,0,0); /**/ | 797 | BN_bntest_rand(b,2+i,0,0); /**/ |
802 | 798 | ||
803 | if (!BN_mod_exp(d,a,b,c,ctx)) | 799 | if (!BN_mod_exp(d,a,b,c,ctx)) |
804 | return(00); | 800 | return(00); |
@@ -848,8 +844,8 @@ int test_exp(BIO *bp, BN_CTX *ctx) | |||
848 | 844 | ||
849 | for (i=0; i<num2; i++) | 845 | for (i=0; i<num2; i++) |
850 | { | 846 | { |
851 | BN_rand(a,20+i*5,0,0); /**/ | 847 | BN_bntest_rand(a,20+i*5,0,0); /**/ |
852 | BN_rand(b,2+i,0,0); /**/ | 848 | BN_bntest_rand(b,2+i,0,0); /**/ |
853 | 849 | ||
854 | if (!BN_exp(d,a,b,ctx)) | 850 | if (!BN_exp(d,a,b,ctx)) |
855 | return(00); | 851 | return(00); |
@@ -899,7 +895,7 @@ int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_) | |||
899 | else | 895 | else |
900 | { | 896 | { |
901 | a=BN_new(); | 897 | a=BN_new(); |
902 | BN_rand(a,200,0,0); /**/ | 898 | BN_bntest_rand(a,200,0,0); /**/ |
903 | a->neg=rand_neg(); | 899 | a->neg=rand_neg(); |
904 | } | 900 | } |
905 | for (i=0; i<num0; i++) | 901 | for (i=0; i<num0; i++) |
@@ -951,7 +947,7 @@ int test_lshift1(BIO *bp) | |||
951 | b=BN_new(); | 947 | b=BN_new(); |
952 | c=BN_new(); | 948 | c=BN_new(); |
953 | 949 | ||
954 | BN_rand(a,200,0,0); /**/ | 950 | BN_bntest_rand(a,200,0,0); /**/ |
955 | a->neg=rand_neg(); | 951 | a->neg=rand_neg(); |
956 | for (i=0; i<num0; i++) | 952 | for (i=0; i<num0; i++) |
957 | { | 953 | { |
@@ -995,7 +991,7 @@ int test_rshift(BIO *bp,BN_CTX *ctx) | |||
995 | e=BN_new(); | 991 | e=BN_new(); |
996 | BN_one(c); | 992 | BN_one(c); |
997 | 993 | ||
998 | BN_rand(a,200,0,0); /**/ | 994 | BN_bntest_rand(a,200,0,0); /**/ |
999 | a->neg=rand_neg(); | 995 | a->neg=rand_neg(); |
1000 | for (i=0; i<num0; i++) | 996 | for (i=0; i<num0; i++) |
1001 | { | 997 | { |
@@ -1038,7 +1034,7 @@ int test_rshift1(BIO *bp) | |||
1038 | b=BN_new(); | 1034 | b=BN_new(); |
1039 | c=BN_new(); | 1035 | c=BN_new(); |
1040 | 1036 | ||
1041 | BN_rand(a,200,0,0); /**/ | 1037 | BN_bntest_rand(a,200,0,0); /**/ |
1042 | a->neg=rand_neg(); | 1038 | a->neg=rand_neg(); |
1043 | for (i=0; i<num0; i++) | 1039 | for (i=0; i<num0; i++) |
1044 | { | 1040 | { |