3 Generation of DSA parameters
5 Copyright (C) 2002, 2013, 2014 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
44 #include "nettle-internal.h"
45 #include "hogweed-internal.h"
48 /* Valid sizes, according to FIPS 186-3 are (1024, 160), (2048, 224),
49 (2048, 256), (3072, 256). */
51 dsa_generate_params(struct dsa_params *params,
52 void *random_ctx, nettle_random_func *random,
53 void *progress_ctx, nettle_progress_func *progress,
54 unsigned p_bits, unsigned q_bits)
60 if (q_bits < 30 || p_bits < q_bits + 30)
65 nettle_random_prime (params->q, q_bits, 0, random_ctx, random,
66 progress_ctx, progress);
68 if (q_bits >= (p_bits + 2)/3)
69 _nettle_generate_pocklington_prime (params->p, r, p_bits, 0,
71 params->q, NULL, params->q);
78 p0_bits = (p_bits + 3)/2;
80 nettle_random_prime (p0, p0_bits, 0,
82 progress_ctx, progress);
85 progress (progress_ctx, 'q');
87 /* Generate p = 2 r q p0 + 1, such that 2^{n-1} < p < 2^n. */
88 mpz_mul (p0q, p0, params->q);
90 _nettle_generate_pocklington_prime (params->p, r, p_bits, 0,
100 progress (progress_ctx, 'p');
104 mpz_set_ui (params->g, a);
105 mpz_powm (params->g, params->g, r, params->p);
106 if (mpz_cmp_ui (params->g, 1) != 0)
113 progress (progress_ctx, 'g');