/src/nettle/dsa-gen-params.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* dsa-gen-params.c |
2 | | |
3 | | Generation of DSA parameters |
4 | | |
5 | | Copyright (C) 2002, 2013, 2014 Niels Möller |
6 | | |
7 | | This file is part of GNU Nettle. |
8 | | |
9 | | GNU Nettle is free software: you can redistribute it and/or |
10 | | modify it under the terms of either: |
11 | | |
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. |
15 | | |
16 | | or |
17 | | |
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. |
21 | | |
22 | | or both in parallel, as here. |
23 | | |
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. |
28 | | |
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/. |
32 | | */ |
33 | | |
34 | | #if HAVE_CONFIG_H |
35 | | # include "config.h" |
36 | | #endif |
37 | | |
38 | | #include <assert.h> |
39 | | #include <stdlib.h> |
40 | | |
41 | | #include "dsa.h" |
42 | | |
43 | | #include "bignum.h" |
44 | | #include "nettle-internal.h" |
45 | | #include "hogweed-internal.h" |
46 | | |
47 | | |
48 | | /* Valid sizes, according to FIPS 186-3 are (1024, 160), (2048, 224), |
49 | | (2048, 256), (3072, 256). */ |
50 | | int |
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) |
55 | 0 | { |
56 | 0 | mpz_t r; |
57 | 0 | unsigned p0_bits; |
58 | 0 | unsigned a; |
59 | |
|
60 | 0 | if (q_bits < 30 || p_bits < q_bits + 30) |
61 | 0 | return 0; |
62 | | |
63 | 0 | mpz_init (r); |
64 | |
|
65 | 0 | nettle_random_prime (params->q, q_bits, 0, random_ctx, random, |
66 | 0 | progress_ctx, progress); |
67 | |
|
68 | 0 | if (q_bits >= (p_bits + 2)/3) |
69 | 0 | _nettle_generate_pocklington_prime (params->p, r, p_bits, 0, |
70 | 0 | random_ctx, random, |
71 | 0 | params->q, NULL, params->q); |
72 | 0 | else |
73 | 0 | { |
74 | 0 | mpz_t p0, p0q; |
75 | 0 | mpz_init (p0); |
76 | 0 | mpz_init (p0q); |
77 | |
|
78 | 0 | p0_bits = (p_bits + 3)/2; |
79 | | |
80 | 0 | nettle_random_prime (p0, p0_bits, 0, |
81 | 0 | random_ctx, random, |
82 | 0 | progress_ctx, progress); |
83 | |
|
84 | 0 | if (progress) |
85 | 0 | progress (progress_ctx, 'q'); |
86 | | |
87 | | /* Generate p = 2 r q p0 + 1, such that 2^{n-1} < p < 2^n. */ |
88 | 0 | mpz_mul (p0q, p0, params->q); |
89 | |
|
90 | 0 | _nettle_generate_pocklington_prime (params->p, r, p_bits, 0, |
91 | 0 | random_ctx, random, |
92 | 0 | p0, params->q, p0q); |
93 | |
|
94 | 0 | mpz_mul (r, r, p0); |
95 | |
|
96 | 0 | mpz_clear (p0); |
97 | 0 | mpz_clear (p0q); |
98 | 0 | } |
99 | 0 | if (progress) |
100 | 0 | progress (progress_ctx, 'p'); |
101 | |
|
102 | 0 | for (a = 2; ; a++) |
103 | 0 | { |
104 | 0 | mpz_set_ui (params->g, a); |
105 | 0 | mpz_powm (params->g, params->g, r, params->p); |
106 | 0 | if (mpz_cmp_ui (params->g, 1) != 0) |
107 | 0 | break; |
108 | 0 | } |
109 | |
|
110 | 0 | mpz_clear (r); |
111 | | |
112 | 0 | if (progress) |
113 | 0 | progress (progress_ctx, 'g'); |
114 | |
|
115 | 0 | return 1; |
116 | 0 | } |