dlopen-test: Use libnettle.dylib on MacOS.
[gd/nettle] / 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 #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 {
56   mpz_t r;
57   unsigned p0_bits;
58   unsigned a;
59
60   if (q_bits < 30 || p_bits < q_bits + 30)
61     return 0;
62
63   mpz_init (r);
64
65   nettle_random_prime (params->q, q_bits, 0, random_ctx, random,
66                        progress_ctx, progress);
67
68   if (q_bits >= (p_bits + 2)/3)
69     _nettle_generate_pocklington_prime (params->p, r, p_bits, 0,
70                                         random_ctx, random,
71                                         params->q, NULL, params->q);
72   else
73     {
74       mpz_t p0, p0q;
75       mpz_init (p0);
76       mpz_init (p0q);
77
78       p0_bits = (p_bits + 3)/2;
79   
80       nettle_random_prime (p0, p0_bits, 0,
81                            random_ctx, random,
82                            progress_ctx, progress);
83
84       if (progress)
85         progress (progress_ctx, 'q');
86
87       /* Generate p = 2 r q p0 + 1, such that 2^{n-1} < p < 2^n. */
88       mpz_mul (p0q, p0, params->q);
89
90       _nettle_generate_pocklington_prime (params->p, r, p_bits, 0,
91                                           random_ctx, random,
92                                           p0, params->q, p0q);
93
94       mpz_mul (r, r, p0);
95
96       mpz_clear (p0);
97       mpz_clear (p0q);
98     }
99   if (progress)
100     progress (progress_ctx, 'p');
101
102   for (a = 2; ; a++)
103     {
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)
107         break;
108     }
109
110   mpz_clear (r);
111   
112   if (progress)
113     progress (progress_ctx, 'g');
114
115   return 1;
116 }