diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_prime.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_prime.c | 459 |
1 files changed, 226 insertions, 233 deletions
diff --git a/src/lib/libcrypto/bn/bn_prime.c b/src/lib/libcrypto/bn/bn_prime.c index 0c85f70b59..918b9237c6 100644 --- a/src/lib/libcrypto/bn/bn_prime.c +++ b/src/lib/libcrypto/bn/bn_prime.c | |||
@@ -55,53 +55,100 @@ | |||
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
57 | */ | 57 | */ |
58 | /* ==================================================================== | ||
59 | * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. | ||
60 | * | ||
61 | * Redistribution and use in source and binary forms, with or without | ||
62 | * modification, are permitted provided that the following conditions | ||
63 | * are met: | ||
64 | * | ||
65 | * 1. Redistributions of source code must retain the above copyright | ||
66 | * notice, this list of conditions and the following disclaimer. | ||
67 | * | ||
68 | * 2. Redistributions in binary form must reproduce the above copyright | ||
69 | * notice, this list of conditions and the following disclaimer in | ||
70 | * the documentation and/or other materials provided with the | ||
71 | * distribution. | ||
72 | * | ||
73 | * 3. All advertising materials mentioning features or use of this | ||
74 | * software must display the following acknowledgment: | ||
75 | * "This product includes software developed by the OpenSSL Project | ||
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
77 | * | ||
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
79 | * endorse or promote products derived from this software without | ||
80 | * prior written permission. For written permission, please contact | ||
81 | * openssl-core@openssl.org. | ||
82 | * | ||
83 | * 5. Products derived from this software may not be called "OpenSSL" | ||
84 | * nor may "OpenSSL" appear in their names without prior written | ||
85 | * permission of the OpenSSL Project. | ||
86 | * | ||
87 | * 6. Redistributions of any form whatsoever must retain the following | ||
88 | * acknowledgment: | ||
89 | * "This product includes software developed by the OpenSSL Project | ||
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
91 | * | ||
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
104 | * ==================================================================== | ||
105 | * | ||
106 | * This product includes cryptographic software written by Eric Young | ||
107 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
108 | * Hudson (tjh@cryptsoft.com). | ||
109 | * | ||
110 | */ | ||
58 | 111 | ||
59 | #include <stdio.h> | 112 | #include <stdio.h> |
60 | #include <time.h> | 113 | #include <time.h> |
61 | #include "cryptlib.h" | 114 | #include "cryptlib.h" |
62 | #include "bn_lcl.h" | 115 | #include "bn_lcl.h" |
63 | #include "rand.h" | 116 | #include <openssl/rand.h> |
64 | 117 | ||
65 | /* The quick seive algorithm approach to weeding out primes is | 118 | /* The quick sieve algorithm approach to weeding out primes is |
66 | * Philip Zimmermann's, as implemented in PGP. I have had a read of | 119 | * Philip Zimmermann's, as implemented in PGP. I have had a read of |
67 | * his comments and implemented my own version. | 120 | * his comments and implemented my own version. |
68 | */ | 121 | */ |
69 | #include "bn_prime.h" | 122 | #include "bn_prime.h" |
70 | 123 | ||
71 | #ifndef NOPROTO | 124 | static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1, |
72 | static int witness(BIGNUM *a, BIGNUM *n, BN_CTX *ctx,BN_CTX *ctx2, | 125 | const BIGNUM *a1_odd, int k, BN_CTX *ctx, BN_MONT_CTX *mont); |
73 | BN_MONT_CTX *mont); | ||
74 | static int probable_prime(BIGNUM *rnd, int bits); | 126 | static int probable_prime(BIGNUM *rnd, int bits); |
75 | static int probable_prime_dh(BIGNUM *rnd, int bits, | 127 | static int probable_prime_dh(BIGNUM *rnd, int bits, |
76 | BIGNUM *add, BIGNUM *rem, BN_CTX *ctx); | 128 | const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx); |
77 | static int probable_prime_dh_strong(BIGNUM *rnd, int bits, | 129 | static int probable_prime_dh_safe(BIGNUM *rnd, int bits, |
78 | BIGNUM *add, BIGNUM *rem, BN_CTX *ctx); | 130 | const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx); |
79 | #else | 131 | |
80 | static int witness(); | 132 | BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe, |
81 | static int probable_prime(); | 133 | const BIGNUM *add, const BIGNUM *rem, |
82 | static int probable_prime_dh(); | 134 | void (*callback)(int,int,void *), void *cb_arg) |
83 | static int probable_prime_dh_strong(); | ||
84 | #endif | ||
85 | |||
86 | BIGNUM *BN_generate_prime(bits,strong,add,rem,callback,cb_arg) | ||
87 | int bits; | ||
88 | int strong; | ||
89 | BIGNUM *add; | ||
90 | BIGNUM *rem; | ||
91 | void (*callback)(P_I_I_P); | ||
92 | char *cb_arg; | ||
93 | { | 135 | { |
94 | BIGNUM *rnd=NULL; | 136 | BIGNUM *rnd=NULL; |
95 | BIGNUM *ret=NULL; | 137 | BIGNUM t; |
96 | BIGNUM *t=NULL; | 138 | int found=0; |
97 | int i,j,c1=0; | 139 | int i,j,c1=0; |
98 | BN_CTX *ctx; | 140 | BN_CTX *ctx; |
141 | int checks = BN_prime_checks_for_size(bits); | ||
99 | 142 | ||
100 | ctx=BN_CTX_new(); | 143 | ctx=BN_CTX_new(); |
101 | if (ctx == NULL) goto err; | 144 | if (ctx == NULL) goto err; |
102 | if ((rnd=BN_new()) == NULL) goto err; | 145 | if (ret == NULL) |
103 | if (strong) | 146 | { |
104 | if ((t=BN_new()) == NULL) goto err; | 147 | if ((rnd=BN_new()) == NULL) goto err; |
148 | } | ||
149 | else | ||
150 | rnd=ret; | ||
151 | BN_init(&t); | ||
105 | loop: | 152 | loop: |
106 | /* make a random number and set the top and bottom bits */ | 153 | /* make a random number and set the top and bottom bits */ |
107 | if (add == NULL) | 154 | if (add == NULL) |
@@ -110,9 +157,9 @@ loop: | |||
110 | } | 157 | } |
111 | else | 158 | else |
112 | { | 159 | { |
113 | if (strong) | 160 | if (safe) |
114 | { | 161 | { |
115 | if (!probable_prime_dh_strong(rnd,bits,add,rem,ctx)) | 162 | if (!probable_prime_dh_safe(rnd,bits,add,rem,ctx)) |
116 | goto err; | 163 | goto err; |
117 | } | 164 | } |
118 | else | 165 | else |
@@ -124,171 +171,188 @@ loop: | |||
124 | /* if (BN_mod_word(rnd,(BN_ULONG)3) == 1) goto loop; */ | 171 | /* if (BN_mod_word(rnd,(BN_ULONG)3) == 1) goto loop; */ |
125 | if (callback != NULL) callback(0,c1++,cb_arg); | 172 | if (callback != NULL) callback(0,c1++,cb_arg); |
126 | 173 | ||
127 | if (!strong) | 174 | if (!safe) |
128 | { | 175 | { |
129 | i=BN_is_prime(rnd,BN_prime_checks,callback,ctx,cb_arg); | 176 | i=BN_is_prime_fasttest(rnd,checks,callback,ctx,cb_arg,0); |
130 | if (i == -1) goto err; | 177 | if (i == -1) goto err; |
131 | if (i == 0) goto loop; | 178 | if (i == 0) goto loop; |
132 | } | 179 | } |
133 | else | 180 | else |
134 | { | 181 | { |
135 | /* for a strong prime generation, | 182 | /* for "safe prime" generation, |
136 | * check that (p-1)/2 is prime. | 183 | * check that (p-1)/2 is prime. |
137 | * Since a prime is odd, We just | 184 | * Since a prime is odd, We just |
138 | * need to divide by 2 */ | 185 | * need to divide by 2 */ |
139 | if (!BN_rshift1(t,rnd)) goto err; | 186 | if (!BN_rshift1(&t,rnd)) goto err; |
140 | 187 | ||
141 | for (i=0; i<BN_prime_checks; i++) | 188 | for (i=0; i<checks; i++) |
142 | { | 189 | { |
143 | j=BN_is_prime(rnd,1,callback,ctx,cb_arg); | 190 | j=BN_is_prime_fasttest(rnd,1,callback,ctx,cb_arg,0); |
144 | if (j == -1) goto err; | 191 | if (j == -1) goto err; |
145 | if (j == 0) goto loop; | 192 | if (j == 0) goto loop; |
146 | 193 | ||
147 | j=BN_is_prime(t,1,callback,ctx,cb_arg); | 194 | j=BN_is_prime_fasttest(&t,1,callback,ctx,cb_arg,0); |
148 | if (j == -1) goto err; | 195 | if (j == -1) goto err; |
149 | if (j == 0) goto loop; | 196 | if (j == 0) goto loop; |
150 | 197 | ||
151 | if (callback != NULL) callback(2,c1-1,cb_arg); | 198 | if (callback != NULL) callback(2,c1-1,cb_arg); |
152 | /* We have a strong prime test pass */ | 199 | /* We have a safe prime test pass */ |
153 | } | 200 | } |
154 | } | 201 | } |
155 | /* we have a prime :-) */ | 202 | /* we have a prime :-) */ |
156 | ret=rnd; | 203 | found = 1; |
157 | err: | 204 | err: |
158 | if ((ret == NULL) && (rnd != NULL)) BN_free(rnd); | 205 | if (!found && (ret == NULL) && (rnd != NULL)) BN_free(rnd); |
159 | if (t != NULL) BN_free(t); | 206 | BN_free(&t); |
160 | if (ctx != NULL) BN_CTX_free(ctx); | 207 | if (ctx != NULL) BN_CTX_free(ctx); |
161 | return(ret); | 208 | return(found ? rnd : NULL); |
162 | } | 209 | } |
163 | 210 | ||
164 | int BN_is_prime(a,checks,callback,ctx_passed,cb_arg) | 211 | int BN_is_prime(const BIGNUM *a, int checks, void (*callback)(int,int,void *), |
165 | BIGNUM *a; | 212 | BN_CTX *ctx_passed, void *cb_arg) |
166 | int checks; | ||
167 | void (*callback)(P_I_I_P); | ||
168 | BN_CTX *ctx_passed; | ||
169 | char *cb_arg; | ||
170 | { | 213 | { |
171 | int i,j,c2=0,ret= -1; | 214 | return BN_is_prime_fasttest(a, checks, callback, ctx_passed, cb_arg, 0); |
172 | BIGNUM *check; | 215 | } |
173 | BN_CTX *ctx=NULL,*ctx2=NULL; | ||
174 | BN_MONT_CTX *mont=NULL; | ||
175 | 216 | ||
217 | int BN_is_prime_fasttest(const BIGNUM *a, int checks, | ||
218 | void (*callback)(int,int,void *), | ||
219 | BN_CTX *ctx_passed, void *cb_arg, | ||
220 | int do_trial_division) | ||
221 | { | ||
222 | int i, j, ret = -1; | ||
223 | int k; | ||
224 | BN_CTX *ctx = NULL; | ||
225 | BIGNUM *A1, *A1_odd, *check; /* taken from ctx */ | ||
226 | BN_MONT_CTX *mont = NULL; | ||
227 | const BIGNUM *A = NULL; | ||
228 | |||
229 | if (BN_cmp(a, BN_value_one()) <= 0) | ||
230 | return 0; | ||
231 | |||
232 | if (checks == BN_prime_checks) | ||
233 | checks = BN_prime_checks_for_size(BN_num_bits(a)); | ||
234 | |||
235 | /* first look for small factors */ | ||
176 | if (!BN_is_odd(a)) | 236 | if (!BN_is_odd(a)) |
177 | return(0); | 237 | return 0; |
238 | if (do_trial_division) | ||
239 | { | ||
240 | for (i = 1; i < NUMPRIMES; i++) | ||
241 | if (BN_mod_word(a, primes[i]) == 0) | ||
242 | return 0; | ||
243 | if (callback != NULL) callback(1, -1, cb_arg); | ||
244 | } | ||
245 | |||
178 | if (ctx_passed != NULL) | 246 | if (ctx_passed != NULL) |
179 | ctx=ctx_passed; | 247 | ctx = ctx_passed; |
180 | else | 248 | else |
181 | if ((ctx=BN_CTX_new()) == NULL) goto err; | 249 | if ((ctx=BN_CTX_new()) == NULL) |
182 | 250 | goto err; | |
183 | if ((ctx2=BN_CTX_new()) == NULL) goto err; | 251 | BN_CTX_start(ctx); |
184 | if ((mont=BN_MONT_CTX_new()) == NULL) goto err; | ||
185 | |||
186 | check=ctx->bn[ctx->tos++]; | ||
187 | 252 | ||
188 | /* Setup the montgomery structure */ | 253 | /* A := abs(a) */ |
189 | if (!BN_MONT_CTX_set(mont,a,ctx2)) goto err; | 254 | if (a->neg) |
255 | { | ||
256 | BIGNUM *t; | ||
257 | if ((t = BN_CTX_get(ctx)) == NULL) goto err; | ||
258 | BN_copy(t, a); | ||
259 | t->neg = 0; | ||
260 | A = t; | ||
261 | } | ||
262 | else | ||
263 | A = a; | ||
264 | A1 = BN_CTX_get(ctx); | ||
265 | A1_odd = BN_CTX_get(ctx); | ||
266 | check = BN_CTX_get(ctx); | ||
267 | if (check == NULL) goto err; | ||
268 | |||
269 | /* compute A1 := A - 1 */ | ||
270 | if (!BN_copy(A1, A)) | ||
271 | goto err; | ||
272 | if (!BN_sub_word(A1, 1)) | ||
273 | goto err; | ||
274 | if (BN_is_zero(A1)) | ||
275 | { | ||
276 | ret = 0; | ||
277 | goto err; | ||
278 | } | ||
190 | 279 | ||
191 | for (i=0; i<checks; i++) | 280 | /* write A1 as A1_odd * 2^k */ |
281 | k = 1; | ||
282 | while (!BN_is_bit_set(A1, k)) | ||
283 | k++; | ||
284 | if (!BN_rshift(A1_odd, A1, k)) | ||
285 | goto err; | ||
286 | |||
287 | /* Montgomery setup for computations mod A */ | ||
288 | mont = BN_MONT_CTX_new(); | ||
289 | if (mont == NULL) | ||
290 | goto err; | ||
291 | if (!BN_MONT_CTX_set(mont, A, ctx)) | ||
292 | goto err; | ||
293 | |||
294 | for (i = 0; i < checks; i++) | ||
192 | { | 295 | { |
193 | if (!BN_rand(check,BN_num_bits(a)-1,0,0)) goto err; | 296 | if (!BN_pseudo_rand_range(check, A1)) |
194 | j=witness(check,a,ctx,ctx2,mont); | 297 | goto err; |
298 | if (!BN_add_word(check, 1)) | ||
299 | goto err; | ||
300 | /* now 1 <= check < A */ | ||
301 | |||
302 | j = witness(check, A, A1, A1_odd, k, ctx, mont); | ||
195 | if (j == -1) goto err; | 303 | if (j == -1) goto err; |
196 | if (j) | 304 | if (j) |
197 | { | 305 | { |
198 | ret=0; | 306 | ret=0; |
199 | goto err; | 307 | goto err; |
200 | } | 308 | } |
201 | if (callback != NULL) callback(1,c2++,cb_arg); | 309 | if (callback != NULL) callback(1,i,cb_arg); |
202 | } | 310 | } |
203 | ret=1; | 311 | ret=1; |
204 | err: | 312 | err: |
205 | ctx->tos--; | 313 | if (ctx != NULL) |
206 | if ((ctx_passed == NULL) && (ctx != NULL)) | 314 | { |
207 | BN_CTX_free(ctx); | 315 | BN_CTX_end(ctx); |
208 | if (ctx2 != NULL) | 316 | if (ctx_passed == NULL) |
209 | BN_CTX_free(ctx2); | 317 | BN_CTX_free(ctx); |
210 | if (mont != NULL) BN_MONT_CTX_free(mont); | 318 | } |
211 | 319 | if (mont != NULL) | |
320 | BN_MONT_CTX_free(mont); | ||
321 | |||
212 | return(ret); | 322 | return(ret); |
213 | } | 323 | } |
214 | 324 | ||
215 | #define RECP_MUL_MOD | 325 | static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1, |
216 | 326 | const BIGNUM *a1_odd, int k, BN_CTX *ctx, BN_MONT_CTX *mont) | |
217 | static int witness(a,n,ctx,ctx2,mont) | ||
218 | BIGNUM *a; | ||
219 | BIGNUM *n; | ||
220 | BN_CTX *ctx,*ctx2; | ||
221 | BN_MONT_CTX *mont; | ||
222 | { | 327 | { |
223 | int k,i,ret= -1,good; | 328 | if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) /* w := w^a1_odd mod a */ |
224 | BIGNUM *d,*dd,*tmp,*d1,*d2,*n1; | 329 | return -1; |
225 | BIGNUM *mont_one,*mont_n1,*mont_a; | 330 | if (BN_is_one(w)) |
226 | 331 | return 0; /* probably prime */ | |
227 | d1=ctx->bn[ctx->tos]; | 332 | if (BN_cmp(w, a1) == 0) |
228 | d2=ctx->bn[ctx->tos+1]; | 333 | return 0; /* w == -1 (mod a), 'a' is probably prime */ |
229 | n1=ctx->bn[ctx->tos+2]; | 334 | while (--k) |
230 | ctx->tos+=3; | ||
231 | |||
232 | mont_one=ctx2->bn[ctx2->tos]; | ||
233 | mont_n1=ctx2->bn[ctx2->tos+1]; | ||
234 | mont_a=ctx2->bn[ctx2->tos+2]; | ||
235 | ctx2->tos+=3; | ||
236 | |||
237 | d=d1; | ||
238 | dd=d2; | ||
239 | if (!BN_one(d)) goto err; | ||
240 | if (!BN_sub(n1,n,d)) goto err; /* n1=n-1; */ | ||
241 | k=BN_num_bits(n1); | ||
242 | |||
243 | if (!BN_to_montgomery(mont_one,BN_value_one(),mont,ctx2)) goto err; | ||
244 | if (!BN_to_montgomery(mont_n1,n1,mont,ctx2)) goto err; | ||
245 | if (!BN_to_montgomery(mont_a,a,mont,ctx2)) goto err; | ||
246 | |||
247 | BN_copy(d,mont_one); | ||
248 | for (i=k-1; i>=0; i--) | ||
249 | { | 335 | { |
250 | if ( (BN_cmp(d,mont_one) != 0) && | 336 | if (!BN_mod_mul(w, w, w, a, ctx)) /* w := w^2 mod a */ |
251 | (BN_cmp(d,mont_n1) != 0)) | 337 | return -1; |
252 | good=1; | 338 | if (BN_is_one(w)) |
253 | else | 339 | return 1; /* 'a' is composite, otherwise a previous 'w' would |
254 | good=0; | 340 | * have been == -1 (mod 'a') */ |
255 | 341 | if (BN_cmp(w, a1) == 0) | |
256 | BN_mod_mul_montgomery(dd,d,d,mont,ctx2); | 342 | return 0; /* w == -1 (mod a), 'a' is probably prime */ |
257 | |||
258 | if (good && (BN_cmp(dd,mont_one) == 0)) | ||
259 | { | ||
260 | ret=1; | ||
261 | goto err; | ||
262 | } | ||
263 | if (BN_is_bit_set(n1,i)) | ||
264 | { | ||
265 | BN_mod_mul_montgomery(d,dd,mont_a,mont,ctx2); | ||
266 | } | ||
267 | else | ||
268 | { | ||
269 | tmp=d; | ||
270 | d=dd; | ||
271 | dd=tmp; | ||
272 | } | ||
273 | } | 343 | } |
274 | if (BN_cmp(d,mont_one) == 0) | 344 | /* If we get here, 'w' is the (a-1)/2-th power of the original 'w', |
275 | i=0; | 345 | * and it is neither -1 nor +1 -- so 'a' cannot be prime */ |
276 | else i=1; | 346 | return 1; |
277 | ret=i; | ||
278 | err: | ||
279 | ctx->tos-=3; | ||
280 | ctx2->tos-=3; | ||
281 | return(ret); | ||
282 | } | 347 | } |
283 | 348 | ||
284 | static int probable_prime(rnd, bits) | 349 | static int probable_prime(BIGNUM *rnd, int bits) |
285 | BIGNUM *rnd; | ||
286 | int bits; | ||
287 | { | 350 | { |
288 | int i; | 351 | int i; |
289 | MS_STATIC BN_ULONG mods[NUMPRIMES]; | 352 | BN_ULONG mods[NUMPRIMES]; |
290 | BN_ULONG delta; | 353 | BN_ULONG delta,d; |
291 | 354 | ||
355 | again: | ||
292 | if (!BN_rand(rnd,bits,1,1)) return(0); | 356 | if (!BN_rand(rnd,bits,1,1)) return(0); |
293 | /* we now have a random number 'rand' to test. */ | 357 | /* we now have a random number 'rand' to test. */ |
294 | for (i=1; i<NUMPRIMES; i++) | 358 | for (i=1; i<NUMPRIMES; i++) |
@@ -300,9 +364,12 @@ int bits; | |||
300 | * that gcd(rnd-1,primes) == 1 (except for 2) */ | 364 | * that gcd(rnd-1,primes) == 1 (except for 2) */ |
301 | if (((mods[i]+delta)%primes[i]) <= 1) | 365 | if (((mods[i]+delta)%primes[i]) <= 1) |
302 | { | 366 | { |
367 | d=delta; | ||
303 | delta+=2; | 368 | delta+=2; |
304 | /* perhaps need to check for overflow of | 369 | /* perhaps need to check for overflow of |
305 | * delta (but delta can be upto 2^32) */ | 370 | * delta (but delta can be up to 2^32) |
371 | * 21-May-98 eay - added overflow check */ | ||
372 | if (delta < d) goto again; | ||
306 | goto loop; | 373 | goto loop; |
307 | } | 374 | } |
308 | } | 375 | } |
@@ -310,17 +377,14 @@ int bits; | |||
310 | return(1); | 377 | return(1); |
311 | } | 378 | } |
312 | 379 | ||
313 | static int probable_prime_dh(rnd, bits, add, rem,ctx) | 380 | static int probable_prime_dh(BIGNUM *rnd, int bits, |
314 | BIGNUM *rnd; | 381 | const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx) |
315 | int bits; | ||
316 | BIGNUM *add; | ||
317 | BIGNUM *rem; | ||
318 | BN_CTX *ctx; | ||
319 | { | 382 | { |
320 | int i,ret=0; | 383 | int i,ret=0; |
321 | BIGNUM *t1; | 384 | BIGNUM *t1; |
322 | 385 | ||
323 | t1=ctx->bn[ctx->tos++]; | 386 | BN_CTX_start(ctx); |
387 | if ((t1 = BN_CTX_get(ctx)) == NULL) goto err; | ||
324 | 388 | ||
325 | if (!BN_rand(rnd,bits,0,1)) goto err; | 389 | if (!BN_rand(rnd,bits,0,1)) goto err; |
326 | 390 | ||
@@ -338,7 +402,7 @@ BN_CTX *ctx; | |||
338 | loop: for (i=1; i<NUMPRIMES; i++) | 402 | loop: for (i=1; i<NUMPRIMES; i++) |
339 | { | 403 | { |
340 | /* check that rnd is a prime */ | 404 | /* check that rnd is a prime */ |
341 | if (BN_mod_word(rnd,(BN_LONG)primes[i]) <= 1) | 405 | if (BN_mod_word(rnd,(BN_ULONG)primes[i]) <= 1) |
342 | { | 406 | { |
343 | if (!BN_add(rnd,rnd,add)) goto err; | 407 | if (!BN_add(rnd,rnd,add)) goto err; |
344 | goto loop; | 408 | goto loop; |
@@ -346,24 +410,22 @@ BN_CTX *ctx; | |||
346 | } | 410 | } |
347 | ret=1; | 411 | ret=1; |
348 | err: | 412 | err: |
349 | ctx->tos--; | 413 | BN_CTX_end(ctx); |
350 | return(ret); | 414 | return(ret); |
351 | } | 415 | } |
352 | 416 | ||
353 | static int probable_prime_dh_strong(p, bits, padd, rem,ctx) | 417 | static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd, |
354 | BIGNUM *p; | 418 | const BIGNUM *rem, BN_CTX *ctx) |
355 | int bits; | ||
356 | BIGNUM *padd; | ||
357 | BIGNUM *rem; | ||
358 | BN_CTX *ctx; | ||
359 | { | 419 | { |
360 | int i,ret=0; | 420 | int i,ret=0; |
361 | BIGNUM *t1,*qadd=NULL,*q=NULL; | 421 | BIGNUM *t1,*qadd,*q; |
362 | 422 | ||
363 | bits--; | 423 | bits--; |
364 | t1=ctx->bn[ctx->tos++]; | 424 | BN_CTX_start(ctx); |
365 | q=ctx->bn[ctx->tos++]; | 425 | t1 = BN_CTX_get(ctx); |
366 | qadd=ctx->bn[ctx->tos++]; | 426 | q = BN_CTX_get(ctx); |
427 | qadd = BN_CTX_get(ctx); | ||
428 | if (qadd == NULL) goto err; | ||
367 | 429 | ||
368 | if (!BN_rshift1(qadd,padd)) goto err; | 430 | if (!BN_rshift1(qadd,padd)) goto err; |
369 | 431 | ||
@@ -389,8 +451,8 @@ BN_CTX *ctx; | |||
389 | /* check that p and q are prime */ | 451 | /* check that p and q are prime */ |
390 | /* check that for p and q | 452 | /* check that for p and q |
391 | * gcd(p-1,primes) == 1 (except for 2) */ | 453 | * gcd(p-1,primes) == 1 (except for 2) */ |
392 | if ( (BN_mod_word(p,(BN_LONG)primes[i]) == 0) || | 454 | if ( (BN_mod_word(p,(BN_ULONG)primes[i]) == 0) || |
393 | (BN_mod_word(q,(BN_LONG)primes[i]) == 0)) | 455 | (BN_mod_word(q,(BN_ULONG)primes[i]) == 0)) |
394 | { | 456 | { |
395 | if (!BN_add(p,p,padd)) goto err; | 457 | if (!BN_add(p,p,padd)) goto err; |
396 | if (!BN_add(q,q,qadd)) goto err; | 458 | if (!BN_add(q,q,qadd)) goto err; |
@@ -399,75 +461,6 @@ BN_CTX *ctx; | |||
399 | } | 461 | } |
400 | ret=1; | 462 | ret=1; |
401 | err: | 463 | err: |
402 | ctx->tos-=3; | 464 | BN_CTX_end(ctx); |
403 | return(ret); | ||
404 | } | ||
405 | |||
406 | #if 0 | ||
407 | static int witness(a, n,ctx) | ||
408 | BIGNUM *a; | ||
409 | BIGNUM *n; | ||
410 | BN_CTX *ctx; | ||
411 | { | ||
412 | int k,i,nb,ret= -1; | ||
413 | BIGNUM *d,*dd,*tmp; | ||
414 | BIGNUM *d1,*d2,*x,*n1,*inv; | ||
415 | |||
416 | d1=ctx->bn[ctx->tos]; | ||
417 | d2=ctx->bn[ctx->tos+1]; | ||
418 | x=ctx->bn[ctx->tos+2]; | ||
419 | n1=ctx->bn[ctx->tos+3]; | ||
420 | inv=ctx->bn[ctx->tos+4]; | ||
421 | ctx->tos+=5; | ||
422 | |||
423 | d=d1; | ||
424 | dd=d2; | ||
425 | if (!BN_one(d)) goto err; | ||
426 | if (!BN_sub(n1,n,d)) goto err; /* n1=n-1; */ | ||
427 | k=BN_num_bits(n1); | ||
428 | |||
429 | /* i=BN_num_bits(n); */ | ||
430 | #ifdef RECP_MUL_MOD | ||
431 | nb=BN_reciprocal(inv,n,ctx); /**/ | ||
432 | if (nb == -1) goto err; | ||
433 | #endif | ||
434 | |||
435 | for (i=k-1; i>=0; i--) | ||
436 | { | ||
437 | if (BN_copy(x,d) == NULL) goto err; | ||
438 | #ifndef RECP_MUL_MOD | ||
439 | if (!BN_mod_mul(dd,d,d,n,ctx)) goto err; | ||
440 | #else | ||
441 | if (!BN_mod_mul_reciprocal(dd,d,d,n,inv,nb,ctx)) goto err; | ||
442 | #endif | ||
443 | if ( BN_is_one(dd) && | ||
444 | !BN_is_one(x) && | ||
445 | (BN_cmp(x,n1) != 0)) | ||
446 | { | ||
447 | ret=1; | ||
448 | goto err; | ||
449 | } | ||
450 | if (BN_is_bit_set(n1,i)) | ||
451 | { | ||
452 | #ifndef RECP_MUL_MOD | ||
453 | if (!BN_mod_mul(d,dd,a,n,ctx)) goto err; | ||
454 | #else | ||
455 | if (!BN_mod_mul_reciprocal(d,dd,a,n,inv,nb,ctx)) goto err; | ||
456 | #endif | ||
457 | } | ||
458 | else | ||
459 | { | ||
460 | tmp=d; | ||
461 | d=dd; | ||
462 | dd=tmp; | ||
463 | } | ||
464 | } | ||
465 | if (BN_is_one(d)) | ||
466 | i=0; | ||
467 | else i=1; | ||
468 | ret=i; | ||
469 | err: | ||
470 | ctx->tos-=5; | ||
471 | return(ret); | 465 | return(ret); |
472 | } | 466 | } |
473 | #endif | ||