8c1518ad9bc8cdec952b77c3f8b20d835ee0b36c
[openssl.git] / crypto / dh / dh_gen.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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
8  */
9
10 /*
11  * NB: These functions have been upgraded - the previous prototypes are in
12  * dh_depr.c as wrappers to these ones.  - Geoff
13  */
14
15 /*
16  * DH low level APIs are deprecated for public use, but still ok for
17  * internal use.
18  *
19  * NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
20  * states that no additional pairwise tests are required (apart from the tests
21  * specified in SP800-56A) when generating keys. Hence DH pairwise tests are
22  * omitted here.
23  */
24 #include "internal/deprecated.h"
25
26 #include <stdio.h>
27 #include "internal/cryptlib.h"
28 #include <openssl/bn.h>
29 #include <openssl/sha.h>
30 #include "crypto/dh.h"
31 #include "dh_local.h"
32
33 #ifndef FIPS_MODULE
34 static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
35                                 BN_GENCB *cb);
36 #endif /* FIPS_MODULE */
37
38 int dh_generate_ffc_parameters(DH *dh, int type, int pbits,
39                                int qbits, EVP_MD *md, BN_GENCB *cb)
40 {
41     int ret, res;
42
43     if (qbits <= 0) {
44         if (md != NULL)
45             qbits = EVP_MD_size(md) * 8;
46         else
47             qbits = (pbits >= 2048 ? SHA256_DIGEST_LENGTH :
48                                      SHA_DIGEST_LENGTH) * 8;
49     }
50 #ifndef FIPS_MODULE
51     if (type == DH_PARAMGEN_TYPE_FIPS_186_2)
52         ret = ffc_params_FIPS186_2_generate(dh->libctx, &dh->params,
53                                             FFC_PARAM_TYPE_DH,
54                                             pbits, qbits, md, &res, cb);
55     else
56 #endif
57         ret = ffc_params_FIPS186_4_generate(dh->libctx, &dh->params,
58                                             FFC_PARAM_TYPE_DH,
59                                             pbits, qbits, md, &res, cb);
60     if (ret > 0)
61         dh->dirty_cnt++;
62     return ret;
63 }
64
65 int dh_get_named_group_uid_from_size(int pbits)
66 {
67     /*
68      * Just choose an approved safe prime group.
69      * The alternative to this is to generate FIPS186-4 domain parameters i.e.
70      * return dh_generate_ffc_parameters(ret, prime_len, 0, NULL, cb);
71      * As the FIPS186-4 generated params are for backwards compatibility,
72      * the safe prime group should be used as the default.
73      */
74     int nid;
75
76     switch (pbits) {
77     case 2048:
78         nid = NID_ffdhe2048;
79         break;
80     case 3072:
81         nid = NID_ffdhe3072;
82         break;
83     case 4096:
84         nid = NID_ffdhe4096;
85         break;
86     case 6144:
87         nid = NID_ffdhe6144;
88         break;
89     case 8192:
90         nid = NID_ffdhe8192;
91         break;
92     /* unsupported prime_len */
93     default:
94         return NID_undef;
95     }
96     return nid;
97 }
98
99 #ifdef FIPS_MODULE
100
101 static int dh_gen_named_group(OPENSSL_CTX *libctx, DH *ret, int prime_len)
102 {
103     DH *dh;
104     int ok = 0;
105     int nid = dh_get_named_group_uid_from_size(prime_len);
106
107     if (nid == NID_undef)
108         return 0;
109
110     dh = dh_new_by_nid_with_libctx(libctx, nid);
111     if (dh != NULL
112         && ffc_params_copy(&ret->params, &dh->params)) {
113         ok = 1;
114         ret->dirty_cnt++;
115     }
116     DH_free(dh);
117     return ok;
118 }
119 #endif /* FIPS_MODULE */
120
121 int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
122                               BN_GENCB *cb)
123 {
124 #ifdef FIPS_MODULE
125     if (generator != 2)
126         return 0;
127     return dh_gen_named_group(ret->libctx, ret, prime_len);
128 #else
129     if (ret->meth->generate_params)
130         return ret->meth->generate_params(ret, prime_len, generator, cb);
131     return dh_builtin_genparams(ret, prime_len, generator, cb);
132 #endif /* FIPS_MODULE */
133 }
134
135 #ifndef FIPS_MODULE
136 /*-
137  * We generate DH parameters as follows
138  * find a prime p which is prime_len bits long,
139  * where q=(p-1)/2 is also prime.
140  * In the following we assume that g is not 0, 1 or p-1, since it
141  * would generate only trivial subgroups.
142  * For this case, g is a generator of the order-q subgroup if
143  * g^q mod p == 1.
144  * Or in terms of the Legendre symbol: (g/p) == 1.
145  *
146  * Having said all that,
147  * there is another special case method for the generators 2, 3 and 5.
148  * Using the quadratic reciprocity law it is possible to solve
149  * (g/p) == 1 for the special values 2, 3, 5:
150  * (2/p) == 1 if p mod 8 == 1 or 7.
151  * (3/p) == 1 if p mod 12 == 1 or 11.
152  * (5/p) == 1 if p mod 5 == 1 or 4.
153  * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol
154  *
155  * Since all safe primes > 7 must satisfy p mod 12 == 11
156  * and all safe primes > 11 must satisfy p mod 5 != 1
157  * we can further improve the condition for g = 2, 3 and 5:
158  * for 2, p mod 24 == 23
159  * for 3, p mod 12 == 11
160  * for 5, p mod 60 == 59
161  */
162 static int dh_builtin_genparams(DH *ret, int prime_len, int generator,
163                                 BN_GENCB *cb)
164 {
165     BIGNUM *t1, *t2;
166     int g, ok = -1;
167     BN_CTX *ctx = NULL;
168
169     if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) {
170         DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_LARGE);
171         return 0;
172     }
173
174     if (prime_len < DH_MIN_MODULUS_BITS) {
175         DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_SMALL);
176         return 0;
177     }
178
179     ctx = BN_CTX_new();
180     if (ctx == NULL)
181         goto err;
182     BN_CTX_start(ctx);
183     t1 = BN_CTX_get(ctx);
184     t2 = BN_CTX_get(ctx);
185     if (t2 == NULL)
186         goto err;
187
188     /* Make sure 'ret' has the necessary elements */
189     if (ret->params.p == NULL && ((ret->params.p = BN_new()) == NULL))
190         goto err;
191     if (ret->params.g == NULL && ((ret->params.g = BN_new()) == NULL))
192         goto err;
193
194     if (generator <= 1) {
195         DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);
196         goto err;
197     }
198     if (generator == DH_GENERATOR_2) {
199         if (!BN_set_word(t1, 24))
200             goto err;
201         if (!BN_set_word(t2, 23))
202             goto err;
203         g = 2;
204     } else if (generator == DH_GENERATOR_5) {
205         if (!BN_set_word(t1, 60))
206             goto err;
207         if (!BN_set_word(t2, 59))
208             goto err;
209         g = 5;
210     } else {
211         /*
212          * in the general case, don't worry if 'generator' is a generator or
213          * not: since we are using safe primes, it will generate either an
214          * order-q or an order-2q group, which both is OK
215          */
216         if (!BN_set_word(t1, 12))
217             goto err;
218         if (!BN_set_word(t2, 11))
219             goto err;
220         g = generator;
221     }
222
223     if (!BN_generate_prime_ex(ret->params.p, prime_len, 1, t1, t2, cb))
224         goto err;
225     if (!BN_GENCB_call(cb, 3, 0))
226         goto err;
227     if (!BN_set_word(ret->params.g, g))
228         goto err;
229     ret->dirty_cnt++;
230     ok = 1;
231  err:
232     if (ok == -1) {
233         DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);
234         ok = 0;
235     }
236
237     BN_CTX_end(ctx);
238     BN_CTX_free(ctx);
239     return ok;
240 }
241 #endif /* FIPS_MODULE */