Apply %restore_fcommon macro for Address Sanitizer
[platform/upstream/nettle.git] / dsa-gen-params.c
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
46
47 /* Valid sizes, according to FIPS 186-3 are (1024, 160), (2048, 224),
48    (2048, 256), (3072, 256). */
49 int
50 dsa_generate_params(struct dsa_params *params,
51                     void *random_ctx, nettle_random_func *random,
52                     void *progress_ctx, nettle_progress_func *progress,
53                     unsigned p_bits, unsigned q_bits)
54 {
55   mpz_t r;
56   unsigned p0_bits;
57   unsigned a;
58
59   if (q_bits < 30 || p_bits < q_bits + 30)
60     return 0;
61
62   mpz_init (r);
63
64   nettle_random_prime (params->q, q_bits, 0, random_ctx, random,
65                        progress_ctx, progress);
66
67   if (q_bits >= (p_bits + 2)/3)
68     _nettle_generate_pocklington_prime (params->p, r, p_bits, 0,
69                                         random_ctx, random,
70                                         params->q, NULL, params->q);
71   else
72     {
73       mpz_t p0, p0q;
74       mpz_init (p0);
75       mpz_init (p0q);
76
77       p0_bits = (p_bits + 3)/2;
78   
79       nettle_random_prime (p0, p0_bits, 0,
80                            random_ctx, random,
81                            progress_ctx, progress);
82
83       if (progress)
84         progress (progress_ctx, 'q');
85
86       /* Generate p = 2 r q p0 + 1, such that 2^{n-1} < p < 2^n. */
87       mpz_mul (p0q, p0, params->q);
88
89       _nettle_generate_pocklington_prime (params->p, r, p_bits, 0,
90                                           random_ctx, random,
91                                           p0, params->q, p0q);
92
93       mpz_mul (r, r, p0);
94
95       mpz_clear (p0);
96       mpz_clear (p0q);
97     }
98   if (progress)
99     progress (progress_ctx, 'p');
100
101   for (a = 2; ; a++)
102     {
103       mpz_set_ui (params->g, a);
104       mpz_powm (params->g, params->g, r, params->p);
105       if (mpz_cmp_ui (params->g, 1) != 0)
106         break;
107     }
108
109   mpz_clear (r);
110   
111   if (progress)
112     progress (progress_ctx, 'g');
113
114   return 1;
115 }