]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
f11f86f6 | 2 | * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
e38873f5 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
706457b7 | 12 | #include "dh_local.h" |
25f2138b | 13 | #include "crypto/bn.h" |
62f49b90 | 14 | #include "crypto/dh.h" |
d02b48c6 | 15 | |
13066cee | 16 | static int generate_key(DH *dh); |
f971ccb2 | 17 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, |
0f113f3e MC |
18 | const BIGNUM *a, const BIGNUM *p, |
19 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | |
13066cee DSH |
20 | static int dh_init(DH *dh); |
21 | static int dh_finish(DH *dh); | |
22 | ||
8083fd3a | 23 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e | 24 | { |
62f49b90 SL |
25 | BN_CTX *ctx = NULL; |
26 | BN_MONT_CTX *mont = NULL; | |
27 | BIGNUM *tmp; | |
28 | int ret = -1; | |
29 | #ifndef FIPS_MODE | |
30 | int check_result; | |
31 | #endif | |
32 | ||
dc8de3e6 | 33 | if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) { |
62f49b90 SL |
34 | DHerr(0, DH_R_MODULUS_TOO_LARGE); |
35 | goto err; | |
36 | } | |
37 | ||
dc8de3e6 | 38 | if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) { |
62f49b90 SL |
39 | DHerr(0, DH_R_MODULUS_TOO_SMALL); |
40 | return 0; | |
41 | } | |
42 | ||
8083fd3a | 43 | ctx = BN_CTX_new_ex(dh->libctx); |
62f49b90 SL |
44 | if (ctx == NULL) |
45 | goto err; | |
46 | BN_CTX_start(ctx); | |
47 | tmp = BN_CTX_get(ctx); | |
48 | if (tmp == NULL) | |
49 | goto err; | |
50 | ||
51 | if (dh->priv_key == NULL) { | |
52 | DHerr(0, DH_R_NO_PRIVATE_VALUE); | |
53 | goto err; | |
54 | } | |
55 | ||
56 | if (dh->flags & DH_FLAG_CACHE_MONT_P) { | |
57 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, | |
dc8de3e6 | 58 | dh->lock, dh->params.p, ctx); |
62f49b90 SL |
59 | BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME); |
60 | if (!mont) | |
61 | goto err; | |
62 | } | |
63 | /* TODO(3.0) : Solve in a PR related to Key validation for DH */ | |
64 | #ifndef FIPS_MODE | |
65 | if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) { | |
66 | DHerr(0, DH_R_INVALID_PUBKEY); | |
67 | goto err; | |
68 | } | |
69 | #endif | |
dc8de3e6 | 70 | if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->params.p, ctx, |
62f49b90 SL |
71 | mont)) { |
72 | DHerr(0, ERR_R_BN_LIB); | |
73 | goto err; | |
74 | } | |
75 | ||
76 | ret = BN_bn2bin(tmp, key); | |
77 | err: | |
78 | BN_CTX_end(ctx); | |
79 | BN_CTX_free(ctx); | |
80 | return ret; | |
0f113f3e | 81 | } |
13066cee | 82 | |
8083fd3a | 83 | int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e | 84 | { |
8083fd3a SL |
85 | #ifdef FIPS_MODE |
86 | return compute_key(key, pub_key, dh); | |
87 | #else | |
88 | return dh->meth->compute_key(key, pub_key, dh); | |
89 | #endif | |
0f113f3e | 90 | } |
13066cee | 91 | |
8083fd3a | 92 | int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e MC |
93 | { |
94 | int rv, pad; | |
62f49b90 SL |
95 | |
96 | #ifdef FIPS_MODE | |
8083fd3a | 97 | rv = compute_key(key, pub_key, dh); |
62f49b90 | 98 | #else |
0f113f3e | 99 | rv = dh->meth->compute_key(key, pub_key, dh); |
62f49b90 | 100 | #endif |
0f113f3e MC |
101 | if (rv <= 0) |
102 | return rv; | |
dc8de3e6 | 103 | pad = BN_num_bytes(dh->params.p) - rv; |
0f113f3e MC |
104 | if (pad > 0) { |
105 | memmove(key + pad, key, rv); | |
106 | memset(key, 0, pad); | |
107 | } | |
108 | return rv + pad; | |
109 | } | |
bc91494e | 110 | |
13066cee | 111 | static DH_METHOD dh_ossl = { |
0f113f3e MC |
112 | "OpenSSL DH Method", |
113 | generate_key, | |
114 | compute_key, | |
115 | dh_bn_mod_exp, | |
116 | dh_init, | |
117 | dh_finish, | |
118 | DH_FLAG_FIPS_METHOD, | |
119 | NULL, | |
120 | NULL | |
13066cee DSH |
121 | }; |
122 | ||
076fc555 RS |
123 | static const DH_METHOD *default_DH_method = &dh_ossl; |
124 | ||
f971ccb2 | 125 | const DH_METHOD *DH_OpenSSL(void) |
13066cee | 126 | { |
0f113f3e | 127 | return &dh_ossl; |
13066cee DSH |
128 | } |
129 | ||
62f49b90 SL |
130 | const DH_METHOD *DH_get_default_method(void) |
131 | { | |
132 | return default_DH_method; | |
133 | } | |
134 | ||
135 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, | |
136 | const BIGNUM *a, const BIGNUM *p, | |
137 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | |
138 | { | |
139 | return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); | |
140 | } | |
141 | ||
142 | static int dh_init(DH *dh) | |
143 | { | |
144 | dh->flags |= DH_FLAG_CACHE_MONT_P; | |
dc8de3e6 | 145 | ffc_params_init(&dh->params); |
f11f86f6 | 146 | dh->dirty_cnt++; |
62f49b90 SL |
147 | return 1; |
148 | } | |
149 | ||
150 | static int dh_finish(DH *dh) | |
151 | { | |
152 | BN_MONT_CTX_free(dh->method_mont_p); | |
153 | return 1; | |
154 | } | |
155 | ||
156 | #ifndef FIPS_MODE | |
076fc555 RS |
157 | void DH_set_default_method(const DH_METHOD *meth) |
158 | { | |
159 | default_DH_method = meth; | |
160 | } | |
8083fd3a | 161 | #endif /* FIPS_MODE */ |
076fc555 | 162 | |
62f49b90 | 163 | int DH_generate_key(DH *dh) |
076fc555 | 164 | { |
8083fd3a SL |
165 | #ifdef FIPS_MODE |
166 | return generate_key(dh); | |
167 | #else | |
62f49b90 | 168 | return dh->meth->generate_key(dh); |
8083fd3a | 169 | #endif |
076fc555 RS |
170 | } |
171 | ||
8083fd3a SL |
172 | int dh_generate_public_key(BN_CTX *ctx, DH *dh, const BIGNUM *priv_key, |
173 | BIGNUM *pub_key) | |
174 | { | |
175 | int ret = 0; | |
176 | BIGNUM *prk = BN_new(); | |
177 | BN_MONT_CTX *mont = NULL; | |
178 | ||
179 | if (prk == NULL) | |
180 | return 0; | |
181 | ||
182 | if (dh->flags & DH_FLAG_CACHE_MONT_P) { | |
183 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, | |
184 | dh->lock, dh->params.p, ctx); | |
185 | if (mont == NULL) | |
186 | goto err; | |
187 | } | |
188 | BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); | |
189 | ||
190 | /* pub_key = g^priv_key mod p */ | |
191 | if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p, | |
192 | ctx, mont)) | |
193 | goto err; | |
194 | ret = 1; | |
195 | err: | |
196 | BN_clear_free(prk); | |
197 | return ret; | |
198 | } | |
199 | ||
200 | static int generate_key(DH *dh) | |
0f113f3e MC |
201 | { |
202 | int ok = 0; | |
203 | int generate_new_key = 0; | |
f11f86f6 | 204 | #ifndef FIPS_MODE |
0f113f3e | 205 | unsigned l; |
f11f86f6 | 206 | #endif |
91f7361f | 207 | BN_CTX *ctx = NULL; |
0f113f3e | 208 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
d02b48c6 | 209 | |
dc8de3e6 | 210 | if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) { |
f11f86f6 | 211 | DHerr(0, DH_R_MODULUS_TOO_LARGE); |
91f7361f GV |
212 | return 0; |
213 | } | |
214 | ||
dc8de3e6 | 215 | if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) { |
f11f86f6 | 216 | DHerr(0, DH_R_MODULUS_TOO_SMALL); |
6de1fe90 BE |
217 | return 0; |
218 | } | |
219 | ||
8083fd3a | 220 | ctx = BN_CTX_new_ex(dh->libctx); |
0f113f3e MC |
221 | if (ctx == NULL) |
222 | goto err; | |
d02b48c6 | 223 | |
0f113f3e | 224 | if (dh->priv_key == NULL) { |
74924dcb | 225 | priv_key = BN_secure_new(); |
0f113f3e MC |
226 | if (priv_key == NULL) |
227 | goto err; | |
228 | generate_new_key = 1; | |
8083fd3a | 229 | } else { |
0f113f3e | 230 | priv_key = dh->priv_key; |
8083fd3a | 231 | } |
d02b48c6 | 232 | |
0f113f3e MC |
233 | if (dh->pub_key == NULL) { |
234 | pub_key = BN_new(); | |
235 | if (pub_key == NULL) | |
236 | goto err; | |
8083fd3a | 237 | } else { |
0f113f3e | 238 | pub_key = dh->pub_key; |
0f113f3e | 239 | } |
0f113f3e | 240 | if (generate_new_key) { |
f11f86f6 SL |
241 | /* Is it an approved safe prime ?*/ |
242 | if (DH_get_nid(dh) != NID_undef) { | |
a38c878c | 243 | /* |
f11f86f6 SL |
244 | * The safe prime group code sets N = 2*s |
245 | * (where s = max security strength supported). | |
246 | * N = dh->length (N = maximum bit length of private key) | |
a38c878c | 247 | */ |
f11f86f6 SL |
248 | if (dh->length == 0 |
249 | || dh->params.q == NULL | |
250 | || dh->length > BN_num_bits(dh->params.q)) | |
251 | goto err; | |
252 | if (!ffc_generate_private_key(ctx, &dh->params, dh->length, | |
253 | dh->length / 2, priv_key)) | |
254 | goto err; | |
255 | } else { | |
256 | #ifdef FIPS_MODE | |
257 | if (dh->params.q == NULL) | |
258 | goto err; | |
259 | #else | |
260 | if (dh->params.q == NULL) { | |
261 | /* secret exponent length */ | |
262 | l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1; | |
263 | if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE, | |
264 | BN_RAND_BOTTOM_ANY, ctx)) | |
265 | goto err; | |
266 | /* | |
267 | * We handle just one known case where g is a quadratic non-residue: | |
268 | * for g = 2: p % 8 == 3 | |
269 | */ | |
270 | if (BN_is_word(dh->params.g, DH_GENERATOR_2) | |
271 | && !BN_is_bit_set(dh->params.p, 2)) { | |
272 | /* clear bit 0, since it won't be a secret anyway */ | |
273 | if (!BN_clear_bit(priv_key, 0)) | |
274 | goto err; | |
275 | } | |
276 | } else | |
277 | #endif | |
278 | { | |
279 | /* | |
280 | * For FFC FIPS 186-4 keygen | |
281 | * security strength s = 112, | |
282 | * Max Private key size N = len(q) | |
283 | */ | |
284 | if (!ffc_generate_private_key(ctx, &dh->params, | |
285 | BN_num_bits(dh->params.q), 112, | |
286 | priv_key)) | |
a38c878c BE |
287 | goto err; |
288 | } | |
0f113f3e MC |
289 | } |
290 | } | |
dfeab068 | 291 | |
8083fd3a SL |
292 | if (!dh_generate_public_key(ctx, dh, priv_key, pub_key)) |
293 | goto err; | |
46a64376 | 294 | |
0f113f3e MC |
295 | dh->pub_key = pub_key; |
296 | dh->priv_key = priv_key; | |
8b84b075 | 297 | dh->dirty_cnt++; |
0f113f3e MC |
298 | ok = 1; |
299 | err: | |
300 | if (ok != 1) | |
f11f86f6 | 301 | DHerr(0, ERR_R_BN_LIB); |
d02b48c6 | 302 | |
23a1d5e9 | 303 | if (pub_key != dh->pub_key) |
0f113f3e | 304 | BN_free(pub_key); |
23a1d5e9 | 305 | if (priv_key != dh->priv_key) |
0f113f3e MC |
306 | BN_free(priv_key); |
307 | BN_CTX_free(ctx); | |
26a7d938 | 308 | return ok; |
0f113f3e | 309 | } |
d02b48c6 | 310 | |
9aaecbfc | 311 | int dh_buf2key(DH *dh, const unsigned char *buf, size_t len) |
312 | { | |
313 | int err_reason = DH_R_BN_ERROR; | |
314 | BIGNUM *pubkey = NULL; | |
315 | const BIGNUM *p; | |
316 | size_t p_size; | |
317 | ||
318 | if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL) | |
319 | goto err; | |
320 | DH_get0_pqg(dh, &p, NULL, NULL); | |
321 | if (p == NULL || (p_size = BN_num_bytes(p)) == 0) { | |
322 | err_reason = DH_R_NO_PARAMETERS_SET; | |
323 | goto err; | |
324 | } | |
325 | /* | |
326 | * As per Section 4.2.8.1 of RFC 8446 fail if DHE's | |
327 | * public key is of size not equal to size of p | |
328 | */ | |
329 | if (BN_is_zero(pubkey) || p_size != len) { | |
330 | err_reason = DH_R_INVALID_PUBKEY; | |
331 | goto err; | |
332 | } | |
333 | if (DH_set0_key(dh, pubkey, NULL) != 1) | |
334 | goto err; | |
335 | return 1; | |
336 | err: | |
337 | DHerr(DH_F_DH_BUF2KEY, err_reason); | |
338 | BN_free(pubkey); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out) | |
343 | { | |
344 | const BIGNUM *pubkey; | |
345 | unsigned char *pbuf; | |
346 | const BIGNUM *p; | |
347 | int p_size; | |
348 | ||
349 | DH_get0_pqg(dh, &p, NULL, NULL); | |
350 | DH_get0_key(dh, &pubkey, NULL); | |
351 | if (p == NULL || pubkey == NULL | |
352 | || (p_size = BN_num_bytes(p)) == 0 | |
353 | || BN_num_bytes(pubkey) == 0) { | |
354 | DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY); | |
355 | return 0; | |
356 | } | |
357 | if ((pbuf = OPENSSL_malloc(p_size)) == NULL) { | |
358 | DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE); | |
359 | return 0; | |
360 | } | |
361 | /* | |
362 | * As per Section 4.2.8.1 of RFC 8446 left pad public | |
363 | * key with zeros to the size of p | |
364 | */ | |
365 | if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) { | |
366 | OPENSSL_free(pbuf); | |
367 | DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR); | |
368 | return 0; | |
369 | } | |
370 | *pbuf_out = pbuf; | |
371 | return p_size; | |
372 | } |