diff options
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_gen.c')
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_gen.c | 109 |
1 files changed, 57 insertions, 52 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c index d7d30bf90a..b5e5ec06e5 100644 --- a/src/lib/libcrypto/dsa/dsa_gen.c +++ b/src/lib/libcrypto/dsa/dsa_gen.c | |||
@@ -64,23 +64,18 @@ | |||
64 | #define HASH SHA1 | 64 | #define HASH SHA1 |
65 | #endif | 65 | #endif |
66 | 66 | ||
67 | #ifndef NO_SHA | ||
67 | #include <stdio.h> | 68 | #include <stdio.h> |
68 | #include <time.h> | 69 | #include <time.h> |
69 | #include "cryptlib.h" | 70 | #include "cryptlib.h" |
70 | #include "sha.h" | 71 | #include <openssl/sha.h> |
71 | #include "bn.h" | 72 | #include <openssl/bn.h> |
72 | #include "dsa.h" | 73 | #include <openssl/dsa.h> |
73 | #include "rand.h" | 74 | #include <openssl/rand.h> |
74 | 75 | ||
75 | DSA *DSA_generate_parameters(bits,seed_in,seed_len,counter_ret,h_ret,callback, | 76 | DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len, |
76 | cb_arg) | 77 | int *counter_ret, unsigned long *h_ret, void (*callback)(), |
77 | int bits; | 78 | char *cb_arg) |
78 | unsigned char *seed_in; | ||
79 | int seed_len; | ||
80 | int *counter_ret; | ||
81 | unsigned long *h_ret; | ||
82 | void (*callback)(); | ||
83 | char *cb_arg; | ||
84 | { | 79 | { |
85 | int ok=0; | 80 | int ok=0; |
86 | unsigned char seed[SHA_DIGEST_LENGTH]; | 81 | unsigned char seed[SHA_DIGEST_LENGTH]; |
@@ -88,6 +83,7 @@ char *cb_arg; | |||
88 | unsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH]; | 83 | unsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH]; |
89 | BIGNUM *r0,*W,*X,*c,*test; | 84 | BIGNUM *r0,*W,*X,*c,*test; |
90 | BIGNUM *g=NULL,*q=NULL,*p=NULL; | 85 | BIGNUM *g=NULL,*q=NULL,*p=NULL; |
86 | BN_MONT_CTX *mont=NULL; | ||
91 | int k,n=0,i,b,m=0; | 87 | int k,n=0,i,b,m=0; |
92 | int counter=0; | 88 | int counter=0; |
93 | BN_CTX *ctx=NULL,*ctx2=NULL; | 89 | BN_CTX *ctx=NULL,*ctx2=NULL; |
@@ -100,20 +96,20 @@ char *cb_arg; | |||
100 | if ((seed_in != NULL) && (seed_len == 20)) | 96 | if ((seed_in != NULL) && (seed_len == 20)) |
101 | memcpy(seed,seed_in,seed_len); | 97 | memcpy(seed,seed_in,seed_len); |
102 | 98 | ||
103 | ctx=BN_CTX_new(); | 99 | if ((ctx=BN_CTX_new()) == NULL) goto err; |
104 | if (ctx == NULL) goto err; | 100 | if ((ctx2=BN_CTX_new()) == NULL) goto err; |
105 | ctx2=BN_CTX_new(); | 101 | if ((ret=DSA_new()) == NULL) goto err; |
106 | if (ctx2 == NULL) goto err; | 102 | |
107 | ret=DSA_new(); | 103 | if ((mont=BN_MONT_CTX_new()) == NULL) goto err; |
108 | if (ret == NULL) goto err; | 104 | |
109 | r0=ctx2->bn[0]; | 105 | r0= &(ctx2->bn[0]); |
110 | g=ctx2->bn[1]; | 106 | g= &(ctx2->bn[1]); |
111 | W=ctx2->bn[2]; | 107 | W= &(ctx2->bn[2]); |
112 | q=ctx2->bn[3]; | 108 | q= &(ctx2->bn[3]); |
113 | X=ctx2->bn[4]; | 109 | X= &(ctx2->bn[4]); |
114 | c=ctx2->bn[5]; | 110 | c= &(ctx2->bn[5]); |
115 | p=ctx2->bn[6]; | 111 | p= &(ctx2->bn[6]); |
116 | test=ctx2->bn[7]; | 112 | test= &(ctx2->bn[7]); |
117 | 113 | ||
118 | BN_lshift(test,BN_value_one(),bits-1); | 114 | BN_lshift(test,BN_value_one(),bits-1); |
119 | 115 | ||
@@ -216,14 +212,16 @@ end: | |||
216 | 212 | ||
217 | /* We now need to gernerate g */ | 213 | /* We now need to gernerate g */ |
218 | /* Set r0=(p-1)/q */ | 214 | /* Set r0=(p-1)/q */ |
219 | BN_sub(test,p,BN_value_one()); | 215 | BN_sub(test,p,BN_value_one()); |
220 | BN_div(r0,NULL,test,q,ctx); | 216 | BN_div(r0,NULL,test,q,ctx); |
221 | 217 | ||
222 | BN_set_word(test,h); | 218 | BN_set_word(test,h); |
219 | BN_MONT_CTX_set(mont,p,ctx); | ||
220 | |||
223 | for (;;) | 221 | for (;;) |
224 | { | 222 | { |
225 | /* g=test^r0%p */ | 223 | /* g=test^r0%p */ |
226 | BN_mod_exp(g,test,r0,p,ctx); | 224 | BN_mod_exp_mont(g,test,r0,p,ctx,mont); |
227 | if (!BN_is_one(g)) break; | 225 | if (!BN_is_one(g)) break; |
228 | BN_add(test,test,BN_value_one()); | 226 | BN_add(test,test,BN_value_one()); |
229 | h++; | 227 | h++; |
@@ -246,32 +244,32 @@ err: | |||
246 | if (counter_ret != NULL) *counter_ret=counter; | 244 | if (counter_ret != NULL) *counter_ret=counter; |
247 | if (h_ret != NULL) *h_ret=h; | 245 | if (h_ret != NULL) *h_ret=h; |
248 | } | 246 | } |
249 | BN_CTX_free(ctx); | 247 | if (ctx != NULL) BN_CTX_free(ctx); |
250 | BN_CTX_free(ctx2); | 248 | if (ctx != NULL) BN_CTX_free(ctx2); |
249 | if (mont != NULL) BN_MONT_CTX_free(mont); | ||
251 | return(ok?ret:NULL); | 250 | return(ok?ret:NULL); |
252 | } | 251 | } |
253 | 252 | ||
254 | int DSA_is_prime(w, callback,cb_arg) | 253 | int DSA_is_prime(BIGNUM *w, void (*callback)(), char *cb_arg) |
255 | BIGNUM *w; | ||
256 | void (*callback)(); | ||
257 | char *cb_arg; | ||
258 | { | 254 | { |
259 | int ok= -1,j,i,n; | 255 | int ok= -1,j,i,n; |
260 | BN_CTX *ctx=NULL,*ctx2=NULL; | 256 | BN_CTX *ctx=NULL,*ctx2=NULL; |
261 | BIGNUM *w_1,*b,*m,*z; | 257 | BIGNUM *w_1,*b,*m,*z,*tmp,*mont_1; |
262 | int a; | 258 | int a; |
259 | BN_MONT_CTX *mont=NULL; | ||
263 | 260 | ||
264 | if (!BN_is_bit_set(w,0)) return(0); | 261 | if (!BN_is_bit_set(w,0)) return(0); |
265 | 262 | ||
266 | ctx=BN_CTX_new(); | 263 | if ((ctx=BN_CTX_new()) == NULL) goto err; |
267 | if (ctx == NULL) goto err; | 264 | if ((ctx2=BN_CTX_new()) == NULL) goto err; |
268 | ctx2=BN_CTX_new(); | 265 | if ((mont=BN_MONT_CTX_new()) == NULL) goto err; |
269 | if (ctx2 == NULL) goto err; | ||
270 | 266 | ||
271 | m= ctx2->bn[2]; | 267 | m= &(ctx2->bn[2]); |
272 | b= ctx2->bn[3]; | 268 | b= &(ctx2->bn[3]); |
273 | z= ctx2->bn[4]; | 269 | z= &(ctx2->bn[4]); |
274 | w_1=ctx2->bn[5]; | 270 | w_1= &(ctx2->bn[5]); |
271 | tmp= &(ctx2->bn[6]); | ||
272 | mont_1= &(ctx2->bn[7]); | ||
275 | 273 | ||
276 | /* step 1 */ | 274 | /* step 1 */ |
277 | n=50; | 275 | n=50; |
@@ -282,24 +280,30 @@ char *cb_arg; | |||
282 | ; | 280 | ; |
283 | if (!BN_rshift(m,w_1,a)) goto err; | 281 | if (!BN_rshift(m,w_1,a)) goto err; |
284 | 282 | ||
283 | BN_MONT_CTX_set(mont,w,ctx); | ||
284 | BN_to_montgomery(mont_1,BN_value_one(),mont,ctx); | ||
285 | BN_to_montgomery(w_1,w_1,mont,ctx); | ||
285 | for (i=1; i < n; i++) | 286 | for (i=1; i < n; i++) |
286 | { | 287 | { |
287 | /* step 3 */ | 288 | /* step 3 */ |
288 | BN_rand(b,BN_num_bits(w)-2/*-1*/,0,0); | 289 | BN_rand(b,BN_num_bits(w)-2/*-1*/,0,0); |
289 | BN_set_word(b,0x10001L); | 290 | /* BN_set_word(b,0x10001L); */ |
290 | 291 | ||
291 | /* step 4 */ | 292 | /* step 4 */ |
292 | j=0; | 293 | j=0; |
293 | if (!BN_mod_exp(z,b,m,w,ctx)) goto err; | 294 | if (!BN_mod_exp_mont(z,b,m,w,ctx,mont)) goto err; |
295 | |||
296 | if (!BN_to_montgomery(z,z,mont,ctx)) goto err; | ||
294 | 297 | ||
295 | /* step 5 */ | 298 | /* step 5 */ |
296 | for (;;) | 299 | for (;;) |
297 | { | 300 | { |
298 | if (((j == 0) && BN_is_one(z)) || (BN_cmp(z,w_1) == 0)) | 301 | if (((j == 0) && (BN_cmp(z,mont_1) == 0)) || |
302 | (BN_cmp(z,w_1) == 0)) | ||
299 | break; | 303 | break; |
300 | 304 | ||
301 | /* step 6 */ | 305 | /* step 6 */ |
302 | if ((j > 0) && BN_is_one(z)) | 306 | if ((j > 0) && (BN_cmp(z,mont_1) == 0)) |
303 | { | 307 | { |
304 | ok=0; | 308 | ok=0; |
305 | goto err; | 309 | goto err; |
@@ -312,7 +316,7 @@ char *cb_arg; | |||
312 | goto err; | 316 | goto err; |
313 | } | 317 | } |
314 | 318 | ||
315 | if (!BN_mod_mul(z,z,z,w,ctx)) goto err; | 319 | if (!BN_mod_mul_montgomery(z,z,z,mont,ctx)) goto err; |
316 | if (callback != NULL) callback(1,j,cb_arg); | 320 | if (callback != NULL) callback(1,j,cb_arg); |
317 | } | 321 | } |
318 | } | 322 | } |
@@ -322,7 +326,8 @@ err: | |||
322 | if (ok == -1) DSAerr(DSA_F_DSA_IS_PRIME,ERR_R_BN_LIB); | 326 | if (ok == -1) DSAerr(DSA_F_DSA_IS_PRIME,ERR_R_BN_LIB); |
323 | BN_CTX_free(ctx); | 327 | BN_CTX_free(ctx); |
324 | BN_CTX_free(ctx2); | 328 | BN_CTX_free(ctx2); |
329 | BN_MONT_CTX_free(mont); | ||
325 | 330 | ||
326 | return(ok); | 331 | return(ok); |
327 | } | 332 | } |
328 | 333 | #endif | |