2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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
15 #define xxxHASH EVP_sha1()
17 #include <openssl/opensslconf.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>
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,
31 if (ret->meth->dsa_paramgen)
32 return ret->meth->dsa_paramgen(ret, bits, seed_in, seed_len,
33 counter_ret, h_ret, cb);
35 const EVP_MD *evpmd = bits >= 2048 ? EVP_sha256() : EVP_sha1();
36 size_t qbits = EVP_MD_size(evpmd) * 8;
38 return dsa_builtin_paramgen(ret, bits, qbits, evpmd,
39 seed_in, seed_len, NULL, counter_ret,
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)
50 unsigned char seed[SHA256_DIGEST_LENGTH];
51 unsigned char md[SHA256_DIGEST_LENGTH];
52 unsigned char buf[SHA256_DIGEST_LENGTH], buf2[SHA256_DIGEST_LENGTH];
53 BIGNUM *r0, *W, *X, *c, *test;
54 BIGNUM *g = NULL, *q = NULL, *p = NULL;
55 BN_MONT_CTX *mont = NULL;
56 int i, k, n = 0, m = 0, qsize = qbits >> 3;
62 if (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&
63 qsize != SHA256_DIGEST_LENGTH)
68 /* use SHA1 as default */
74 bits = (bits + 63) / 64 * 64;
76 if (seed_in != NULL) {
77 if (seed_len < (size_t)qsize) {
78 DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN, DSA_R_SEED_LEN_SMALL);
81 if (seed_len > (size_t)qsize) {
82 /* Only consume as much seed as is expected. */
85 memcpy(seed, seed_in, seed_len);
88 if ((mont = BN_MONT_CTX_new()) == NULL)
91 if ((ctx = BN_CTX_new()) == NULL)
103 test = BN_CTX_get(ctx);
108 if (!BN_lshift(test, BN_value_one(), bits - 1))
112 for (;;) { /* find q */
113 int use_random_seed = (seed_in == NULL);
116 if (!BN_GENCB_call(cb, 0, m++))
119 if (use_random_seed) {
120 if (RAND_bytes(seed, qsize) <= 0)
123 /* If we come back through, use random seed next time. */
126 memcpy(buf, seed, qsize);
127 memcpy(buf2, seed, qsize);
128 /* precompute "SEED + 1" for step 7: */
129 for (i = qsize - 1; i >= 0; i--) {
136 if (!EVP_Digest(seed, qsize, md, NULL, evpmd, NULL))
138 if (!EVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL))
140 for (i = 0; i < qsize; i++)
145 md[qsize - 1] |= 0x01;
146 if (!BN_bin2bn(md, qsize, q))
150 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
151 use_random_seed, cb);
157 /* do a callback call */
161 if (!BN_GENCB_call(cb, 2, 0))
163 if (!BN_GENCB_call(cb, 3, 0))
170 n = (bits - 1) / 160;
173 if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
178 /* now 'buf' contains "SEED + offset - 1" */
179 for (k = 0; k <= n; k++) {
181 * obtain "SEED + offset + k" by incrementing:
183 for (i = qsize - 1; i >= 0; i--) {
189 if (!EVP_Digest(buf, qsize, md, NULL, evpmd, NULL))
193 if (!BN_bin2bn(md, qsize, r0))
195 if (!BN_lshift(r0, r0, (qsize << 3) * k))
197 if (!BN_add(W, W, r0))
202 if (!BN_mask_bits(W, bits - 1))
206 if (!BN_add(X, X, test))
210 if (!BN_lshift1(r0, q))
212 if (!BN_mod(c, X, r0, ctx))
214 if (!BN_sub(r0, c, BN_value_one()))
216 if (!BN_sub(p, X, r0))
220 if (BN_cmp(p, test) >= 0) {
222 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
224 goto end; /* found it */
231 /* "offset = offset + n + 1" */
239 if (!BN_GENCB_call(cb, 2, 1))
242 /* We now need to generate g */
244 if (!BN_sub(test, p, BN_value_one()))
246 if (!BN_div(r0, NULL, test, q, ctx))
249 if (!BN_set_word(test, h))
251 if (!BN_MONT_CTX_set(mont, p, ctx))
256 if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
260 if (!BN_add(test, test, BN_value_one()))
265 if (!BN_GENCB_call(cb, 3, 1))
277 if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
281 if (counter_ret != NULL)
282 *counter_ret = counter;
286 memcpy(seed_out, seed, qsize);
291 BN_MONT_CTX_free(mont);
296 * This is a parameter generation algorithm for the DSA2 algorithm as
297 * described in FIPS 186-3.
300 int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
301 const EVP_MD *evpmd, const unsigned char *seed_in,
302 size_t seed_len, int idx, unsigned char *seed_out,
303 int *counter_ret, unsigned long *h_ret,
307 unsigned char *seed = NULL, *seed_tmp = NULL;
308 unsigned char md[EVP_MAX_MD_SIZE];
310 BIGNUM *r0, *W, *X, *c, *test;
311 BIGNUM *g = NULL, *q = NULL, *p = NULL;
312 BN_MONT_CTX *mont = NULL;
313 int i, k, n = 0, m = 0, qsize = N >> 3;
317 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
327 evpmd = EVP_sha224();
329 evpmd = EVP_sha256();
332 mdsize = EVP_MD_size(evpmd);
333 /* If unverifiable g generation only don't need seed */
334 if (!ret->p || !ret->q || idx >= 0) {
338 seed = OPENSSL_malloc(seed_len);
343 seed_tmp = OPENSSL_malloc(seed_len);
345 if (seed == NULL || seed_tmp == NULL)
349 memcpy(seed, seed_in, seed_len);
353 if ((ctx = BN_CTX_new()) == NULL)
356 if ((mont = BN_MONT_CTX_new()) == NULL)
360 r0 = BN_CTX_get(ctx);
365 test = BN_CTX_get(ctx);
369 /* if p, q already supplied generate g only */
370 if (ret->p && ret->q) {
374 memcpy(seed_tmp, seed, seed_len);
383 if (!BN_lshift(test, BN_value_one(), L - 1))
386 for (;;) { /* find q */
389 if (!BN_GENCB_call(cb, 0, m++))
393 if (RAND_bytes(seed, seed_len) <= 0)
397 if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
399 /* Take least significant bits of md */
401 pmd = md + mdsize - qsize;
406 memset(md + mdsize, 0, qsize - mdsize);
410 pmd[qsize - 1] |= 0x01;
411 if (!BN_bin2bn(pmd, qsize, q))
415 r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,
416 seed_in ? 1 : 0, cb);
421 /* Provided seed didn't produce a prime: error */
424 DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);
428 /* do a callback call */
431 /* Copy seed to seed_out before we mess with it */
433 memcpy(seed_out, seed, seed_len);
435 if (!BN_GENCB_call(cb, 2, 0))
437 if (!BN_GENCB_call(cb, 3, 0))
444 n = (L - 1) / (mdsize << 3);
447 if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))
452 /* now 'buf' contains "SEED + offset - 1" */
453 for (k = 0; k <= n; k++) {
455 * obtain "SEED + offset + k" by incrementing:
457 for (i = seed_len - 1; i >= 0; i--) {
463 if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))
467 if (!BN_bin2bn(md, mdsize, r0))
469 if (!BN_lshift(r0, r0, (mdsize << 3) * k))
471 if (!BN_add(W, W, r0))
476 if (!BN_mask_bits(W, L - 1))
480 if (!BN_add(X, X, test))
484 if (!BN_lshift1(r0, q))
486 if (!BN_mod(c, X, r0, ctx))
488 if (!BN_sub(r0, c, BN_value_one()))
490 if (!BN_sub(p, X, r0))
494 if (BN_cmp(p, test) >= 0) {
496 r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);
498 goto end; /* found it */
505 /* "offset = offset + n + 1" */
508 if (counter >= (int)(4 * L))
513 DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);
518 if (!BN_GENCB_call(cb, 2, 1))
523 /* We now need to generate g */
525 if (!BN_sub(test, p, BN_value_one()))
527 if (!BN_div(r0, NULL, test, q, ctx))
531 if (!BN_set_word(test, h))
535 if (!BN_MONT_CTX_set(mont, p, ctx))
539 static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };
542 md[1] = (h >> 8) & 0xff;
544 if (!EVP_DigestInit_ex(mctx, evpmd, NULL))
546 if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))
548 if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))
550 if (!EVP_DigestUpdate(mctx, md, 3))
552 if (!EVP_DigestFinal_ex(mctx, md, NULL))
554 if (!BN_bin2bn(md, mdsize, test))
558 if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))
562 if (idx < 0 && !BN_add(test, test, BN_value_one()))
565 if (idx >= 0 && h > 0xffff)
569 if (!BN_GENCB_call(cb, 3, 1))
585 if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {
589 if (counter_ret != NULL)
590 *counter_ret = counter;
595 if (seed_out != seed_tmp)
596 OPENSSL_free(seed_tmp);
600 BN_MONT_CTX_free(mont);
601 EVP_MD_CTX_free(mctx);