/src/openssl111/crypto/dsa/dsa_gen.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* |
11 | | * Parameter generation follows the updated Appendix 2.2 for FIPS PUB 186, |
12 | | * also Appendix 2.2 of FIPS PUB 186-1 (i.e. use SHA as defined in FIPS PUB |
13 | | * 180-1) |
14 | | */ |
15 | | #define xxxHASH EVP_sha1() |
16 | | |
17 | | #include <openssl/opensslconf.h> |
18 | | #include <stdio.h> |
19 | | #include "internal/cryptlib.h" |
20 | | #include <openssl/evp.h> |
21 | | #include <openssl/bn.h> |
22 | | #include <openssl/rand.h> |
23 | | #include <openssl/sha.h> |
24 | | #include "dsa_local.h" |
25 | | |
26 | | int DSA_generate_parameters_ex(DSA *ret, int bits, |
27 | | const unsigned char *seed_in, int seed_len, |
28 | | int *counter_ret, unsigned long *h_ret, |
29 | | BN_GENCB *cb) |
30 | 0 | { |
31 | 0 | if (ret->meth->dsa_paramgen) |
32 | 0 | return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len, |
33 | 0 | counter_ret, h_ret, cb); |
34 | 0 | else { |
35 | 0 | const EVP_MD *evpmd = bits >= 2048 ? EVP_sha256() : EVP_sha1(); |
36 | 0 | size_t qbits = EVP_MD_size(evpmd) * 8; |
37 | |
|
38 | 0 | return dsa_builtin_paramgen(ret, bits, qbits, evpmd, |
39 | 0 | seed_in, seed_len, NULL, counter_ret, |
40 | 0 | h_ret, cb); |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | | int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits, |
45 | | const EVP_MD *evpmd, const unsigned char *seed_in, |
46 | | size_t seed_len, unsigned char *seed_out, |
47 | | int *counter_ret, unsigned long *h_ret, BN_GENCB *cb) |
48 | 0 | { |
49 | 0 | int ok = 0; |
50 | 0 | unsigned char seed[SHA256_DIGEST_LENGTH]; |
51 | 0 | unsigned char md[SHA256_DIGEST_LENGTH]; |
52 | 0 | unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH]; |
53 | 0 | BIGNUM *r0, *W, *X, *c, *test; |
54 | 0 | BIGNUM *g = NULL, *q = NULL, *p = NULL; |
55 | 0 | BN_MONT_CTX *mont = NULL; |
56 | 0 | int i, k, n = 0, m = 0, qsize = qbits >> 3; |
57 | 0 | int counter = 0; |
58 | 0 | int r = 0; |
59 | 0 | BN_CTX *ctx = NULL; |
60 | 0 | unsigned int h = 2; |
61 | |
|
62 | 0 | if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH && |
63 | 0 | qsize != SHA256_DIGEST_LENGTH) |
64 | | /* invalid q size */ |
65 | 0 | return 0; |
66 | | |
67 | 0 | if (evpmd == NULL) { |
68 | 0 | if (qsize == SHA_DIGEST_LENGTH) |
69 | 0 | evpmd = EVP_sha1(); |
70 | 0 | else if (qsize == SHA224_DIGEST_LENGTH) |
71 | 0 | evpmd = EVP_sha224(); |
72 | 0 | else |
73 | 0 | evpmd = EVP_sha256(); |
74 | 0 | } else { |
75 | 0 | qsize = EVP_MD_size(evpmd); |
76 | 0 | } |
77 | |
|
78 | 0 | if (bits < 512) |
79 | 0 | bits = 512; |
80 | |
|
81 | 0 | bits = (bits + 63) / 64 * 64; |
82 | |
|
83 | 0 | if (seed_in != NULL) { |
84 | 0 | if (seed_len < (size_t)qsize) { |
85 | 0 | DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN, DSA_R_SEED_LEN_SMALL); |
86 | 0 | return 0; |
87 | 0 | } |
88 | 0 | if (seed_len > (size_t)qsize) { |
89 | | /* Only consume as much seed as is expected. */ |
90 | 0 | seed_len = qsize; |
91 | 0 | } |
92 | 0 | memcpy(seed, seed_in, seed_len); |
93 | 0 | } |
94 | | |
95 | 0 | if ((mont = BN_MONT_CTX_new()) == NULL) |
96 | 0 | goto err; |
97 | | |
98 | 0 | if ((ctx = BN_CTX_new()) == NULL) |
99 | 0 | goto err; |
100 | | |
101 | 0 | BN_CTX_start(ctx); |
102 | |
|
103 | 0 | r0 = BN_CTX_get(ctx); |
104 | 0 | g = BN_CTX_get(ctx); |
105 | 0 | W = BN_CTX_get(ctx); |
106 | 0 | q = BN_CTX_get(ctx); |
107 | 0 | X = BN_CTX_get(ctx); |
108 | 0 | c = BN_CTX_get(ctx); |
109 | 0 | p = BN_CTX_get(ctx); |
110 | 0 | test = BN_CTX_get(ctx); |
111 | |
|
112 | 0 | if (test == NULL) |
113 | 0 | goto err; |
114 | | |
115 | 0 | if (!BN_lshift(test, BN_value_one(), bits - 1)) |
116 | 0 | goto err; |
117 | | |
118 | 0 | for (;;) { |
119 | 0 | for (;;) { /* find q */ |
120 | 0 | int use_random_seed = (seed_in == NULL); |
121 | | |
122 | | /* step 1 */ |
123 | 0 | if (!BN_GENCB_call(cb, 0, m++)) |
124 | 0 | goto err; |
125 | | |
126 | 0 | if (use_random_seed) { |
127 | 0 | if (RAND_bytes(seed, qsize) <= 0) |
128 | 0 | goto err; |
129 | 0 | } else { |
130 | | /* If we come back through, use random seed next time. */ |
131 | 0 | seed_in = NULL; |
132 | 0 | } |
133 | 0 | memcpy(buf, seed, qsize); |
134 | 0 | memcpy(buf2, seed, qsize); |
135 | | /* precompute "SEED + 1" for step 7: */ |
136 | 0 | for (i = qsize - 1; i >= 0; i--) { |
137 | 0 | buf[i]++; |
138 | 0 | if (buf[i] != 0) |
139 | 0 | break; |
140 | 0 | } |
141 | | |
142 | | /* step 2 */ |
143 | 0 | if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL)) |
144 | 0 | goto err; |
145 | 0 | if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL)) |
146 | 0 | goto err; |
147 | 0 | for (i = 0; i < qsize; i++) |
148 | 0 | md[i] ^= buf2[i]; |
149 | | |
150 | | /* step 3 */ |
151 | 0 | md[0] |= 0x80; |
152 | 0 | md[qsize - 1] |= 0x01; |
153 | 0 | if (!BN_bin2bn(md, qsize, q)) |
154 | 0 | goto err; |
155 | | |
156 | | /* step 4 */ |
157 | 0 | r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, |
158 | 0 | use_random_seed, cb); |
159 | 0 | if (r > 0) |
160 | 0 | break; |
161 | 0 | if (r != 0) |
162 | 0 | goto err; |
163 | | |
164 | | /* do a callback call */ |
165 | | /* step 5 */ |
166 | 0 | } |
167 | | |
168 | 0 | if (!BN_GENCB_call(cb, 2, 0)) |
169 | 0 | goto err; |
170 | 0 | if (!BN_GENCB_call(cb, 3, 0)) |
171 | 0 | goto err; |
172 | | |
173 | | /* step 6 */ |
174 | 0 | counter = 0; |
175 | | /* "offset = 2" */ |
176 | |
|
177 | 0 | n = (bits - 1) / 160; |
178 | |
|
179 | 0 | for (;;) { |
180 | 0 | if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) |
181 | 0 | goto err; |
182 | | |
183 | | /* step 7 */ |
184 | 0 | BN_zero(W); |
185 | | /* now 'buf' contains "SEED + offset - 1" */ |
186 | 0 | for (k = 0; k <= n; k++) { |
187 | | /* |
188 | | * obtain "SEED + offset + k" by incrementing: |
189 | | */ |
190 | 0 | for (i = qsize - 1; i >= 0; i--) { |
191 | 0 | buf[i]++; |
192 | 0 | if (buf[i] != 0) |
193 | 0 | break; |
194 | 0 | } |
195 | |
|
196 | 0 | if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL)) |
197 | 0 | goto err; |
198 | | |
199 | | /* step 8 */ |
200 | 0 | if (!BN_bin2bn(md, qsize, r0)) |
201 | 0 | goto err; |
202 | 0 | if (!BN_lshift(r0, r0, (qsize << 3) * k)) |
203 | 0 | goto err; |
204 | 0 | if (!BN_add(W, W, r0)) |
205 | 0 | goto err; |
206 | 0 | } |
207 | | |
208 | | /* more of step 8 */ |
209 | 0 | if (!BN_mask_bits(W, bits - 1)) |
210 | 0 | goto err; |
211 | 0 | if (!BN_copy(X, W)) |
212 | 0 | goto err; |
213 | 0 | if (!BN_add(X, X, test)) |
214 | 0 | goto err; |
215 | | |
216 | | /* step 9 */ |
217 | 0 | if (!BN_lshift1(r0, q)) |
218 | 0 | goto err; |
219 | 0 | if (!BN_mod(c, X, r0, ctx)) |
220 | 0 | goto err; |
221 | 0 | if (!BN_sub(r0, c, BN_value_one())) |
222 | 0 | goto err; |
223 | 0 | if (!BN_sub(p, X, r0)) |
224 | 0 | goto err; |
225 | | |
226 | | /* step 10 */ |
227 | 0 | if (BN_cmp(p, test) >= 0) { |
228 | | /* step 11 */ |
229 | 0 | r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb); |
230 | 0 | if (r > 0) |
231 | 0 | goto end; /* found it */ |
232 | 0 | if (r != 0) |
233 | 0 | goto err; |
234 | 0 | } |
235 | | |
236 | | /* step 13 */ |
237 | 0 | counter++; |
238 | | /* "offset = offset + n + 1" */ |
239 | | |
240 | | /* step 14 */ |
241 | 0 | if (counter >= 4096) |
242 | 0 | break; |
243 | 0 | } |
244 | 0 | } |
245 | 0 | end: |
246 | 0 | if (!BN_GENCB_call(cb, 2, 1)) |
247 | 0 | goto err; |
248 | | |
249 | | /* We now need to generate g */ |
250 | | /* Set r0=(p-1)/q */ |
251 | 0 | if (!BN_sub(test, p, BN_value_one())) |
252 | 0 | goto err; |
253 | 0 | if (!BN_div(r0, NULL, test, q, ctx)) |
254 | 0 | goto err; |
255 | | |
256 | 0 | if (!BN_set_word(test, h)) |
257 | 0 | goto err; |
258 | 0 | if (!BN_MONT_CTX_set(mont, p, ctx)) |
259 | 0 | goto err; |
260 | | |
261 | 0 | for (;;) { |
262 | | /* g=test^r0%p */ |
263 | 0 | if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) |
264 | 0 | goto err; |
265 | 0 | if (!BN_is_one(g)) |
266 | 0 | break; |
267 | 0 | if (!BN_add(test, test, BN_value_one())) |
268 | 0 | goto err; |
269 | 0 | h++; |
270 | 0 | } |
271 | | |
272 | 0 | if (!BN_GENCB_call(cb, 3, 1)) |
273 | 0 | goto err; |
274 | | |
275 | 0 | ok = 1; |
276 | 0 | err: |
277 | 0 | if (ok) { |
278 | 0 | BN_free(ret->p); |
279 | 0 | BN_free(ret->q); |
280 | 0 | BN_free(ret->g); |
281 | 0 | ret->p = BN_dup(p); |
282 | 0 | ret->q = BN_dup(q); |
283 | 0 | ret->g = BN_dup(g); |
284 | 0 | if (ret->p == NULL || ret->q == NULL || ret->g == NULL) { |
285 | 0 | ok = 0; |
286 | 0 | goto err; |
287 | 0 | } |
288 | 0 | if (counter_ret != NULL) |
289 | 0 | *counter_ret = counter; |
290 | 0 | if (h_ret != NULL) |
291 | 0 | *h_ret = h; |
292 | 0 | if (seed_out) |
293 | 0 | memcpy(seed_out, seed, qsize); |
294 | 0 | } |
295 | 0 | BN_CTX_end(ctx); |
296 | 0 | BN_CTX_free(ctx); |
297 | 0 | BN_MONT_CTX_free(mont); |
298 | 0 | return ok; |
299 | 0 | } |
300 | | |
301 | | /* |
302 | | * This is a parameter generation algorithm for the DSA2 algorithm as |
303 | | * described in FIPS 186-3. |
304 | | */ |
305 | | |
306 | | int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N, |
307 | | const EVP_MD *evpmd, const unsigned char *seed_in, |
308 | | size_t seed_len, int idx, unsigned char *seed_out, |
309 | | int *counter_ret, unsigned long *h_ret, |
310 | | BN_GENCB *cb) |
311 | 0 | { |
312 | 0 | int ok = -1; |
313 | 0 | unsigned char *seed = NULL, *seed_tmp = NULL; |
314 | 0 | unsigned char md[EVP_MAX_MD_SIZE]; |
315 | 0 | int mdsize; |
316 | 0 | BIGNUM *r0, *W, *X, *c, *test; |
317 | 0 | BIGNUM *g = NULL, *q = NULL, *p = NULL; |
318 | 0 | BN_MONT_CTX *mont = NULL; |
319 | 0 | int i, k, n = 0, m = 0, qsize = N >> 3; |
320 | 0 | int counter = 0; |
321 | 0 | int r = 0; |
322 | 0 | BN_CTX *ctx = NULL; |
323 | 0 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
324 | 0 | unsigned int h = 2; |
325 | |
|
326 | 0 | if (mctx == NULL) |
327 | 0 | goto err; |
328 | | |
329 | | /* make sure L > N, otherwise we'll get trapped in an infinite loop */ |
330 | 0 | if (L <= N) { |
331 | 0 | DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS); |
332 | 0 | goto err; |
333 | 0 | } |
334 | | |
335 | 0 | if (evpmd == NULL) { |
336 | 0 | if (N == 160) |
337 | 0 | evpmd = EVP_sha1(); |
338 | 0 | else if (N == 224) |
339 | 0 | evpmd = EVP_sha224(); |
340 | 0 | else |
341 | 0 | evpmd = EVP_sha256(); |
342 | 0 | } |
343 | |
|
344 | 0 | mdsize = EVP_MD_size(evpmd); |
345 | | /* If unverifiable g generation only don't need seed */ |
346 | 0 | if (!ret->p || !ret->q || idx >= 0) { |
347 | 0 | if (seed_len == 0) |
348 | 0 | seed_len = mdsize; |
349 | |
|
350 | 0 | seed = OPENSSL_malloc(seed_len); |
351 | |
|
352 | 0 | if (seed_out) |
353 | 0 | seed_tmp = seed_out; |
354 | 0 | else |
355 | 0 | seed_tmp = OPENSSL_malloc(seed_len); |
356 | |
|
357 | 0 | if (seed == NULL || seed_tmp == NULL) |
358 | 0 | goto err; |
359 | | |
360 | 0 | if (seed_in) |
361 | 0 | memcpy(seed, seed_in, seed_len); |
362 | |
|
363 | 0 | } |
364 | | |
365 | 0 | if ((ctx = BN_CTX_new()) == NULL) |
366 | 0 | goto err; |
367 | | |
368 | 0 | if ((mont = BN_MONT_CTX_new()) == NULL) |
369 | 0 | goto err; |
370 | | |
371 | 0 | BN_CTX_start(ctx); |
372 | 0 | r0 = BN_CTX_get(ctx); |
373 | 0 | g = BN_CTX_get(ctx); |
374 | 0 | W = BN_CTX_get(ctx); |
375 | 0 | X = BN_CTX_get(ctx); |
376 | 0 | c = BN_CTX_get(ctx); |
377 | 0 | test = BN_CTX_get(ctx); |
378 | 0 | if (test == NULL) |
379 | 0 | goto err; |
380 | | |
381 | | /* if p, q already supplied generate g only */ |
382 | 0 | if (ret->p && ret->q) { |
383 | 0 | p = ret->p; |
384 | 0 | q = ret->q; |
385 | 0 | if (idx >= 0) |
386 | 0 | memcpy(seed_tmp, seed, seed_len); |
387 | 0 | goto g_only; |
388 | 0 | } else { |
389 | 0 | p = BN_CTX_get(ctx); |
390 | 0 | q = BN_CTX_get(ctx); |
391 | 0 | if (q == NULL) |
392 | 0 | goto err; |
393 | 0 | } |
394 | | |
395 | 0 | if (!BN_lshift(test, BN_value_one(), L - 1)) |
396 | 0 | goto err; |
397 | 0 | for (;;) { |
398 | 0 | for (;;) { /* find q */ |
399 | 0 | unsigned char *pmd; |
400 | | /* step 1 */ |
401 | 0 | if (!BN_GENCB_call(cb, 0, m++)) |
402 | 0 | goto err; |
403 | | |
404 | 0 | if (!seed_in) { |
405 | 0 | if (RAND_bytes(seed, seed_len) <= 0) |
406 | 0 | goto err; |
407 | 0 | } |
408 | | /* step 2 */ |
409 | 0 | if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL)) |
410 | 0 | goto err; |
411 | | /* Take least significant bits of md */ |
412 | 0 | if (mdsize > qsize) |
413 | 0 | pmd = md + mdsize - qsize; |
414 | 0 | else |
415 | 0 | pmd = md; |
416 | |
|
417 | 0 | if (mdsize < qsize) |
418 | 0 | memset(md + mdsize, 0, qsize - mdsize); |
419 | | |
420 | | /* step 3 */ |
421 | 0 | pmd[0] |= 0x80; |
422 | 0 | pmd[qsize - 1] |= 0x01; |
423 | 0 | if (!BN_bin2bn(pmd, qsize, q)) |
424 | 0 | goto err; |
425 | | |
426 | | /* step 4 */ |
427 | 0 | r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx, |
428 | 0 | seed_in ? 1 : 0, cb); |
429 | 0 | if (r > 0) |
430 | 0 | break; |
431 | 0 | if (r != 0) |
432 | 0 | goto err; |
433 | | /* Provided seed didn't produce a prime: error */ |
434 | 0 | if (seed_in) { |
435 | 0 | ok = 0; |
436 | 0 | DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME); |
437 | 0 | goto err; |
438 | 0 | } |
439 | | |
440 | | /* do a callback call */ |
441 | | /* step 5 */ |
442 | 0 | } |
443 | | /* Copy seed to seed_out before we mess with it */ |
444 | 0 | if (seed_out) |
445 | 0 | memcpy(seed_out, seed, seed_len); |
446 | |
|
447 | 0 | if (!BN_GENCB_call(cb, 2, 0)) |
448 | 0 | goto err; |
449 | 0 | if (!BN_GENCB_call(cb, 3, 0)) |
450 | 0 | goto err; |
451 | | |
452 | | /* step 6 */ |
453 | 0 | counter = 0; |
454 | | /* "offset = 1" */ |
455 | |
|
456 | 0 | n = (L - 1) / (mdsize << 3); |
457 | |
|
458 | 0 | for (;;) { |
459 | 0 | if ((counter != 0) && !BN_GENCB_call(cb, 0, counter)) |
460 | 0 | goto err; |
461 | | |
462 | | /* step 7 */ |
463 | 0 | BN_zero(W); |
464 | | /* now 'buf' contains "SEED + offset - 1" */ |
465 | 0 | for (k = 0; k <= n; k++) { |
466 | | /* |
467 | | * obtain "SEED + offset + k" by incrementing: |
468 | | */ |
469 | 0 | for (i = seed_len - 1; i >= 0; i--) { |
470 | 0 | seed[i]++; |
471 | 0 | if (seed[i] != 0) |
472 | 0 | break; |
473 | 0 | } |
474 | |
|
475 | 0 | if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL)) |
476 | 0 | goto err; |
477 | | |
478 | | /* step 8 */ |
479 | 0 | if (!BN_bin2bn(md, mdsize, r0)) |
480 | 0 | goto err; |
481 | 0 | if (!BN_lshift(r0, r0, (mdsize << 3) * k)) |
482 | 0 | goto err; |
483 | 0 | if (!BN_add(W, W, r0)) |
484 | 0 | goto err; |
485 | 0 | } |
486 | | |
487 | | /* more of step 8 */ |
488 | 0 | if (!BN_mask_bits(W, L - 1)) |
489 | 0 | goto err; |
490 | 0 | if (!BN_copy(X, W)) |
491 | 0 | goto err; |
492 | 0 | if (!BN_add(X, X, test)) |
493 | 0 | goto err; |
494 | | |
495 | | /* step 9 */ |
496 | 0 | if (!BN_lshift1(r0, q)) |
497 | 0 | goto err; |
498 | 0 | if (!BN_mod(c, X, r0, ctx)) |
499 | 0 | goto err; |
500 | 0 | if (!BN_sub(r0, c, BN_value_one())) |
501 | 0 | goto err; |
502 | 0 | if (!BN_sub(p, X, r0)) |
503 | 0 | goto err; |
504 | | |
505 | | /* step 10 */ |
506 | 0 | if (BN_cmp(p, test) >= 0) { |
507 | | /* step 11 */ |
508 | 0 | r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb); |
509 | 0 | if (r > 0) |
510 | 0 | goto end; /* found it */ |
511 | 0 | if (r != 0) |
512 | 0 | goto err; |
513 | 0 | } |
514 | | |
515 | | /* step 13 */ |
516 | 0 | counter++; |
517 | | /* "offset = offset + n + 1" */ |
518 | | |
519 | | /* step 14 */ |
520 | 0 | if (counter >= (int)(4 * L)) |
521 | 0 | break; |
522 | 0 | } |
523 | 0 | if (seed_in) { |
524 | 0 | ok = 0; |
525 | 0 | DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS); |
526 | 0 | goto err; |
527 | 0 | } |
528 | 0 | } |
529 | 0 | end: |
530 | 0 | if (!BN_GENCB_call(cb, 2, 1)) |
531 | 0 | goto err; |
532 | | |
533 | 0 | g_only: |
534 | | |
535 | | /* We now need to generate g */ |
536 | | /* Set r0=(p-1)/q */ |
537 | 0 | if (!BN_sub(test, p, BN_value_one())) |
538 | 0 | goto err; |
539 | 0 | if (!BN_div(r0, NULL, test, q, ctx)) |
540 | 0 | goto err; |
541 | | |
542 | 0 | if (idx < 0) { |
543 | 0 | if (!BN_set_word(test, h)) |
544 | 0 | goto err; |
545 | 0 | } else |
546 | 0 | h = 1; |
547 | 0 | if (!BN_MONT_CTX_set(mont, p, ctx)) |
548 | 0 | goto err; |
549 | | |
550 | 0 | for (;;) { |
551 | 0 | static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e }; |
552 | 0 | if (idx >= 0) { |
553 | 0 | md[0] = idx & 0xff; |
554 | 0 | md[1] = (h >> 8) & 0xff; |
555 | 0 | md[2] = h & 0xff; |
556 | 0 | if (!EVP_DigestInit_ex(mctx, evpmd, NULL)) |
557 | 0 | goto err; |
558 | 0 | if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len)) |
559 | 0 | goto err; |
560 | 0 | if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen))) |
561 | 0 | goto err; |
562 | 0 | if (!EVP_DigestUpdate(mctx, md, 3)) |
563 | 0 | goto err; |
564 | 0 | if (!EVP_DigestFinal_ex(mctx, md, NULL)) |
565 | 0 | goto err; |
566 | 0 | if (!BN_bin2bn(md, mdsize, test)) |
567 | 0 | goto err; |
568 | 0 | } |
569 | | /* g=test^r0%p */ |
570 | 0 | if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) |
571 | 0 | goto err; |
572 | 0 | if (!BN_is_one(g)) |
573 | 0 | break; |
574 | 0 | if (idx < 0 && !BN_add(test, test, BN_value_one())) |
575 | 0 | goto err; |
576 | 0 | h++; |
577 | 0 | if (idx >= 0 && h > 0xffff) |
578 | 0 | goto err; |
579 | 0 | } |
580 | | |
581 | 0 | if (!BN_GENCB_call(cb, 3, 1)) |
582 | 0 | goto err; |
583 | | |
584 | 0 | ok = 1; |
585 | 0 | err: |
586 | 0 | if (ok == 1) { |
587 | 0 | if (p != ret->p) { |
588 | 0 | BN_free(ret->p); |
589 | 0 | ret->p = BN_dup(p); |
590 | 0 | } |
591 | 0 | if (q != ret->q) { |
592 | 0 | BN_free(ret->q); |
593 | 0 | ret->q = BN_dup(q); |
594 | 0 | } |
595 | 0 | BN_free(ret->g); |
596 | 0 | ret->g = BN_dup(g); |
597 | 0 | if (ret->p == NULL || ret->q == NULL || ret->g == NULL) { |
598 | 0 | ok = -1; |
599 | 0 | goto err; |
600 | 0 | } |
601 | 0 | if (counter_ret != NULL) |
602 | 0 | *counter_ret = counter; |
603 | 0 | if (h_ret != NULL) |
604 | 0 | *h_ret = h; |
605 | 0 | } |
606 | 0 | OPENSSL_free(seed); |
607 | 0 | if (seed_out != seed_tmp) |
608 | 0 | OPENSSL_free(seed_tmp); |
609 | 0 | BN_CTX_end(ctx); |
610 | 0 | BN_CTX_free(ctx); |
611 | 0 | BN_MONT_CTX_free(mont); |
612 | 0 | EVP_MD_CTX_free(mctx); |
613 | 0 | return ok; |
614 | 0 | } |