]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 1995-2019 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 | #include "internal/deprecated.h" | |
20 | ||
21 | #include <stdio.h> | |
22 | #include "internal/cryptlib.h" | |
23 | #include <openssl/bn.h> | |
24 | #include "crypto/dh.h" | |
25 | #include "dh_local.h" | |
26 | ||
27 | #ifndef FIPS_MODE | |
28 | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, | |
29 | BN_GENCB *cb); | |
30 | #endif /* FIPS_MODE */ | |
31 | ||
32 | /* | |
33 | * TODO(3.0): keygen should be able to use this method to do a FIPS186-4 style | |
34 | * paramgen. | |
35 | */ | |
36 | int dh_generate_ffc_parameters(DH *dh, int bits, | |
37 | int qbits, int gindex, BN_GENCB *cb) | |
38 | { | |
39 | int ret, res; | |
40 | ||
41 | if (qbits <= 0) { | |
42 | const EVP_MD *evpmd = bits >= 2048 ? EVP_sha256() : EVP_sha1(); | |
43 | ||
44 | qbits = EVP_MD_size(evpmd) * 8; | |
45 | } | |
46 | dh->params.gindex = gindex; | |
47 | ret = ffc_params_FIPS186_4_generate(dh->libctx, &dh->params, | |
48 | FFC_PARAM_TYPE_DH, | |
49 | bits, qbits, NULL, &res, cb); | |
50 | if (ret > 0) | |
51 | dh->dirty_cnt++; | |
52 | return ret; | |
53 | } | |
54 | ||
55 | int DH_generate_parameters_ex(DH *ret, int prime_len, int generator, | |
56 | BN_GENCB *cb) | |
57 | { | |
58 | #ifdef FIPS_MODE | |
59 | /* | |
60 | * Just choose an approved safe prime group. | |
61 | * The alternative to this is to generate FIPS186-4 domain parameters i.e. | |
62 | * return dh_generate_ffc_parameters(ret, prime_len, -1, -1, cb); | |
63 | * As the FIPS186-4 generated params are for backwards compatability, | |
64 | * the safe prime group should be used as the default. | |
65 | */ | |
66 | DH *dh = NULL; | |
67 | int ok = 0, nid; | |
68 | ||
69 | if (generator != 2) | |
70 | return 0; | |
71 | ||
72 | switch (prime_len) { | |
73 | case 2048: | |
74 | nid = NID_ffdhe2048; | |
75 | break; | |
76 | case 3072: | |
77 | nid = NID_ffdhe3072; | |
78 | break; | |
79 | case 4096: | |
80 | nid = NID_ffdhe4096; | |
81 | break; | |
82 | case 6144: | |
83 | nid = NID_ffdhe6144; | |
84 | break; | |
85 | case 8192: | |
86 | nid = NID_ffdhe8192; | |
87 | break; | |
88 | /* unsupported prime_len */ | |
89 | default: | |
90 | return 0; | |
91 | } | |
92 | dh = DH_new_by_nid(nid); | |
93 | if (dh != NULL && ffc_params_copy(&ret->params, &dh->params)) { | |
94 | ok = 1; | |
95 | ret->dirty_cnt++; | |
96 | } | |
97 | DH_free(dh); | |
98 | return ok; | |
99 | #else | |
100 | if (ret->meth->generate_params) | |
101 | return ret->meth->generate_params(ret, prime_len, generator, cb); | |
102 | return dh_builtin_genparams(ret, prime_len, generator, cb); | |
103 | #endif /* FIPS_MODE */ | |
104 | } | |
105 | ||
106 | #ifndef FIPS_MODE | |
107 | /*- | |
108 | * We generate DH parameters as follows | |
109 | * find a prime p which is prime_len bits long, | |
110 | * where q=(p-1)/2 is also prime. | |
111 | * In the following we assume that g is not 0, 1 or p-1, since it | |
112 | * would generate only trivial subgroups. | |
113 | * For this case, g is a generator of the order-q subgroup if | |
114 | * g^q mod p == 1. | |
115 | * Or in terms of the Legendre symbol: (g/p) == 1. | |
116 | * | |
117 | * Having said all that, | |
118 | * there is another special case method for the generators 2, 3 and 5. | |
119 | * Using the quadratic reciprocity law it is possible to solve | |
120 | * (g/p) == 1 for the special values 2, 3, 5: | |
121 | * (2/p) == 1 if p mod 8 == 1 or 7. | |
122 | * (3/p) == 1 if p mod 12 == 1 or 11. | |
123 | * (5/p) == 1 if p mod 5 == 1 or 4. | |
124 | * See for instance: https://en.wikipedia.org/wiki/Legendre_symbol | |
125 | * | |
126 | * Since all safe primes > 7 must satisfy p mod 12 == 11 | |
127 | * and all safe primes > 11 must satisfy p mod 5 != 1 | |
128 | * we can further improve the condition for g = 2, 3 and 5: | |
129 | * for 2, p mod 24 == 23 | |
130 | * for 3, p mod 12 == 11 | |
131 | * for 5, p mod 60 == 59 | |
132 | */ | |
133 | static int dh_builtin_genparams(DH *ret, int prime_len, int generator, | |
134 | BN_GENCB *cb) | |
135 | { | |
136 | BIGNUM *t1, *t2; | |
137 | int g, ok = -1; | |
138 | BN_CTX *ctx = NULL; | |
139 | ||
140 | if (prime_len > OPENSSL_DH_MAX_MODULUS_BITS) { | |
141 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_LARGE); | |
142 | return 0; | |
143 | } | |
144 | ||
145 | if (prime_len < DH_MIN_MODULUS_BITS) { | |
146 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_MODULUS_TOO_SMALL); | |
147 | return 0; | |
148 | } | |
149 | ||
150 | ctx = BN_CTX_new(); | |
151 | if (ctx == NULL) | |
152 | goto err; | |
153 | BN_CTX_start(ctx); | |
154 | t1 = BN_CTX_get(ctx); | |
155 | t2 = BN_CTX_get(ctx); | |
156 | if (t2 == NULL) | |
157 | goto err; | |
158 | ||
159 | /* Make sure 'ret' has the necessary elements */ | |
160 | if (ret->params.p == NULL && ((ret->params.p = BN_new()) == NULL)) | |
161 | goto err; | |
162 | if (ret->params.g == NULL && ((ret->params.g = BN_new()) == NULL)) | |
163 | goto err; | |
164 | ||
165 | if (generator <= 1) { | |
166 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR); | |
167 | goto err; | |
168 | } | |
169 | if (generator == DH_GENERATOR_2) { | |
170 | if (!BN_set_word(t1, 24)) | |
171 | goto err; | |
172 | if (!BN_set_word(t2, 23)) | |
173 | goto err; | |
174 | g = 2; | |
175 | } else if (generator == DH_GENERATOR_5) { | |
176 | if (!BN_set_word(t1, 60)) | |
177 | goto err; | |
178 | if (!BN_set_word(t2, 59)) | |
179 | goto err; | |
180 | g = 5; | |
181 | } else { | |
182 | /* | |
183 | * in the general case, don't worry if 'generator' is a generator or | |
184 | * not: since we are using safe primes, it will generate either an | |
185 | * order-q or an order-2q group, which both is OK | |
186 | */ | |
187 | if (!BN_set_word(t1, 12)) | |
188 | goto err; | |
189 | if (!BN_set_word(t2, 11)) | |
190 | goto err; | |
191 | g = generator; | |
192 | } | |
193 | ||
194 | if (!BN_generate_prime_ex(ret->params.p, prime_len, 1, t1, t2, cb)) | |
195 | goto err; | |
196 | if (!BN_GENCB_call(cb, 3, 0)) | |
197 | goto err; | |
198 | if (!BN_set_word(ret->params.g, g)) | |
199 | goto err; | |
200 | ret->dirty_cnt++; | |
201 | ok = 1; | |
202 | err: | |
203 | if (ok == -1) { | |
204 | DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB); | |
205 | ok = 0; | |
206 | } | |
207 | ||
208 | BN_CTX_end(ctx); | |
209 | BN_CTX_free(ctx); | |
210 | return ok; | |
211 | } | |
212 | #endif /* FIPS_MODE */ |