]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
da1c088f | 2 | * Copyright 1995-2023 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 | ||
ada66e78 P |
10 | /* |
11 | * DH low level APIs are deprecated for public use, but still ok for | |
12 | * internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
d02b48c6 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
706457b7 | 18 | #include "dh_local.h" |
25f2138b | 19 | #include "crypto/bn.h" |
62f49b90 | 20 | #include "crypto/dh.h" |
738ee181 | 21 | #include "crypto/security_bits.h" |
d02b48c6 | 22 | |
f844f9eb | 23 | #ifdef FIPS_MODULE |
b03ec3b5 SL |
24 | # define MIN_STRENGTH 112 |
25 | #else | |
26 | # define MIN_STRENGTH 80 | |
27 | #endif | |
28 | ||
13066cee | 29 | static int generate_key(DH *dh); |
f971ccb2 | 30 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, |
0f113f3e MC |
31 | const BIGNUM *a, const BIGNUM *p, |
32 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | |
13066cee DSH |
33 | static int dh_init(DH *dh); |
34 | static int dh_finish(DH *dh); | |
35 | ||
e454a393 SL |
36 | /* |
37 | * See SP800-56Ar3 Section 5.7.1.1 | |
38 | * Finite Field Cryptography Diffie-Hellman (FFC DH) Primitive | |
39 | */ | |
40 | int ossl_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |
0f113f3e | 41 | { |
62f49b90 SL |
42 | BN_CTX *ctx = NULL; |
43 | BN_MONT_CTX *mont = NULL; | |
e454a393 | 44 | BIGNUM *z = NULL, *pminus1; |
62f49b90 | 45 | int ret = -1; |
62f49b90 | 46 | |
dc8de3e6 | 47 | if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) { |
9311d0c4 | 48 | ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE); |
62f49b90 SL |
49 | goto err; |
50 | } | |
51 | ||
dc8de3e6 | 52 | if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) { |
9311d0c4 | 53 | ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL); |
62f49b90 SL |
54 | return 0; |
55 | } | |
56 | ||
8083fd3a | 57 | ctx = BN_CTX_new_ex(dh->libctx); |
62f49b90 SL |
58 | if (ctx == NULL) |
59 | goto err; | |
60 | BN_CTX_start(ctx); | |
e454a393 SL |
61 | pminus1 = BN_CTX_get(ctx); |
62 | z = BN_CTX_get(ctx); | |
63 | if (z == NULL) | |
62f49b90 SL |
64 | goto err; |
65 | ||
66 | if (dh->priv_key == NULL) { | |
9311d0c4 | 67 | ERR_raise(ERR_LIB_DH, DH_R_NO_PRIVATE_VALUE); |
62f49b90 SL |
68 | goto err; |
69 | } | |
70 | ||
71 | if (dh->flags & DH_FLAG_CACHE_MONT_P) { | |
72 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, | |
dc8de3e6 | 73 | dh->lock, dh->params.p, ctx); |
62f49b90 SL |
74 | BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME); |
75 | if (!mont) | |
76 | goto err; | |
77 | } | |
e454a393 SL |
78 | |
79 | /* (Step 1) Z = pub_key^priv_key mod p */ | |
80 | if (!dh->meth->bn_mod_exp(dh, z, pub_key, dh->priv_key, dh->params.p, ctx, | |
62f49b90 | 81 | mont)) { |
9311d0c4 | 82 | ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB); |
62f49b90 SL |
83 | goto err; |
84 | } | |
85 | ||
e454a393 SL |
86 | /* (Step 2) Error if z <= 1 or z = p - 1 */ |
87 | if (BN_copy(pminus1, dh->params.p) == NULL | |
88 | || !BN_sub_word(pminus1, 1) | |
89 | || BN_cmp(z, BN_value_one()) <= 0 | |
90 | || BN_cmp(z, pminus1) == 0) { | |
91 | ERR_raise(ERR_LIB_DH, DH_R_INVALID_SECRET); | |
92 | goto err; | |
93 | } | |
94 | ||
22aa4a3a | 95 | /* return the padded key, i.e. same number of bytes as the modulus */ |
e454a393 | 96 | ret = BN_bn2binpad(z, key, BN_num_bytes(dh->params.p)); |
62f49b90 | 97 | err: |
e454a393 | 98 | BN_clear(z); /* (Step 2) destroy intermediate values */ |
62f49b90 SL |
99 | BN_CTX_end(ctx); |
100 | BN_CTX_free(ctx); | |
101 | return ret; | |
0f113f3e | 102 | } |
13066cee | 103 | |
22aa4a3a BB |
104 | /*- |
105 | * NB: This function is inherently not constant time due to the | |
106 | * RFC 5246 (8.1.2) padding style that strips leading zero bytes. | |
107 | */ | |
8083fd3a | 108 | int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e | 109 | { |
22aa4a3a BB |
110 | int ret = 0, i; |
111 | volatile size_t npad = 0, mask = 1; | |
112 | ||
113 | /* compute the key; ret is constant unless compute_key is external */ | |
f844f9eb | 114 | #ifdef FIPS_MODULE |
e454a393 | 115 | ret = ossl_dh_compute_key(key, pub_key, dh); |
8083fd3a | 116 | #else |
22aa4a3a | 117 | ret = dh->meth->compute_key(key, pub_key, dh); |
8083fd3a | 118 | #endif |
22aa4a3a BB |
119 | if (ret <= 0) |
120 | return ret; | |
121 | ||
122 | /* count leading zero bytes, yet still touch all bytes */ | |
123 | for (i = 0; i < ret; i++) { | |
124 | mask &= !key[i]; | |
125 | npad += mask; | |
126 | } | |
127 | ||
128 | /* unpad key */ | |
129 | ret -= npad; | |
130 | /* key-dependent memory access, potentially leaking npad / ret */ | |
131 | memmove(key, key + npad, ret); | |
132 | /* key-dependent memory access, potentially leaking npad / ret */ | |
133 | memset(key + ret, 0, npad); | |
134 | ||
135 | return ret; | |
0f113f3e | 136 | } |
13066cee | 137 | |
8083fd3a | 138 | int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e MC |
139 | { |
140 | int rv, pad; | |
62f49b90 | 141 | |
22aa4a3a | 142 | /* rv is constant unless compute_key is external */ |
f844f9eb | 143 | #ifdef FIPS_MODULE |
e454a393 | 144 | rv = ossl_dh_compute_key(key, pub_key, dh); |
62f49b90 | 145 | #else |
0f113f3e | 146 | rv = dh->meth->compute_key(key, pub_key, dh); |
62f49b90 | 147 | #endif |
0f113f3e MC |
148 | if (rv <= 0) |
149 | return rv; | |
dc8de3e6 | 150 | pad = BN_num_bytes(dh->params.p) - rv; |
22aa4a3a | 151 | /* pad is constant (zero) unless compute_key is external */ |
0f113f3e MC |
152 | if (pad > 0) { |
153 | memmove(key + pad, key, rv); | |
154 | memset(key, 0, pad); | |
155 | } | |
156 | return rv + pad; | |
157 | } | |
bc91494e | 158 | |
13066cee | 159 | static DH_METHOD dh_ossl = { |
0f113f3e MC |
160 | "OpenSSL DH Method", |
161 | generate_key, | |
e454a393 | 162 | ossl_dh_compute_key, |
0f113f3e MC |
163 | dh_bn_mod_exp, |
164 | dh_init, | |
165 | dh_finish, | |
166 | DH_FLAG_FIPS_METHOD, | |
167 | NULL, | |
168 | NULL | |
13066cee DSH |
169 | }; |
170 | ||
076fc555 RS |
171 | static const DH_METHOD *default_DH_method = &dh_ossl; |
172 | ||
f971ccb2 | 173 | const DH_METHOD *DH_OpenSSL(void) |
13066cee | 174 | { |
0f113f3e | 175 | return &dh_ossl; |
13066cee DSH |
176 | } |
177 | ||
62f49b90 SL |
178 | const DH_METHOD *DH_get_default_method(void) |
179 | { | |
180 | return default_DH_method; | |
181 | } | |
182 | ||
183 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, | |
184 | const BIGNUM *a, const BIGNUM *p, | |
185 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | |
186 | { | |
79040cf2 JC |
187 | #ifdef S390X_MOD_EXP |
188 | return s390x_mod_exp(r, a, p, m, ctx, m_ctx); | |
189 | #else | |
62f49b90 | 190 | return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); |
79040cf2 | 191 | #endif |
62f49b90 SL |
192 | } |
193 | ||
194 | static int dh_init(DH *dh) | |
195 | { | |
196 | dh->flags |= DH_FLAG_CACHE_MONT_P; | |
f11f86f6 | 197 | dh->dirty_cnt++; |
62f49b90 SL |
198 | return 1; |
199 | } | |
200 | ||
201 | static int dh_finish(DH *dh) | |
202 | { | |
203 | BN_MONT_CTX_free(dh->method_mont_p); | |
204 | return 1; | |
205 | } | |
206 | ||
f844f9eb | 207 | #ifndef FIPS_MODULE |
076fc555 RS |
208 | void DH_set_default_method(const DH_METHOD *meth) |
209 | { | |
210 | default_DH_method = meth; | |
211 | } | |
f844f9eb | 212 | #endif /* FIPS_MODULE */ |
076fc555 | 213 | |
62f49b90 | 214 | int DH_generate_key(DH *dh) |
076fc555 | 215 | { |
f844f9eb | 216 | #ifdef FIPS_MODULE |
8083fd3a SL |
217 | return generate_key(dh); |
218 | #else | |
62f49b90 | 219 | return dh->meth->generate_key(dh); |
8083fd3a | 220 | #endif |
076fc555 RS |
221 | } |
222 | ||
19dbb742 SL |
223 | int ossl_dh_generate_public_key(BN_CTX *ctx, const DH *dh, |
224 | const BIGNUM *priv_key, BIGNUM *pub_key) | |
8083fd3a SL |
225 | { |
226 | int ret = 0; | |
227 | BIGNUM *prk = BN_new(); | |
228 | BN_MONT_CTX *mont = NULL; | |
229 | ||
230 | if (prk == NULL) | |
231 | return 0; | |
232 | ||
233 | if (dh->flags & DH_FLAG_CACHE_MONT_P) { | |
d1fb6b48 NT |
234 | /* |
235 | * We take the input DH as const, but we lie, because in some cases we | |
236 | * want to get a hold of its Montgomery context. | |
237 | * | |
238 | * We cast to remove the const qualifier in this case, it should be | |
239 | * fine... | |
240 | */ | |
241 | BN_MONT_CTX **pmont = (BN_MONT_CTX **)&dh->method_mont_p; | |
242 | ||
243 | mont = BN_MONT_CTX_set_locked(pmont, dh->lock, dh->params.p, ctx); | |
8083fd3a SL |
244 | if (mont == NULL) |
245 | goto err; | |
246 | } | |
247 | BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); | |
248 | ||
249 | /* pub_key = g^priv_key mod p */ | |
250 | if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p, | |
251 | ctx, mont)) | |
252 | goto err; | |
253 | ret = 1; | |
254 | err: | |
255 | BN_clear_free(prk); | |
256 | return ret; | |
257 | } | |
258 | ||
259 | static int generate_key(DH *dh) | |
0f113f3e MC |
260 | { |
261 | int ok = 0; | |
262 | int generate_new_key = 0; | |
f844f9eb | 263 | #ifndef FIPS_MODULE |
0f113f3e | 264 | unsigned l; |
f11f86f6 | 265 | #endif |
91f7361f | 266 | BN_CTX *ctx = NULL; |
0f113f3e | 267 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
d02b48c6 | 268 | |
dc8de3e6 | 269 | if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) { |
9311d0c4 | 270 | ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE); |
91f7361f GV |
271 | return 0; |
272 | } | |
273 | ||
dc8de3e6 | 274 | if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) { |
9311d0c4 | 275 | ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL); |
6de1fe90 BE |
276 | return 0; |
277 | } | |
278 | ||
8083fd3a | 279 | ctx = BN_CTX_new_ex(dh->libctx); |
0f113f3e MC |
280 | if (ctx == NULL) |
281 | goto err; | |
d02b48c6 | 282 | |
0f113f3e | 283 | if (dh->priv_key == NULL) { |
74924dcb | 284 | priv_key = BN_secure_new(); |
0f113f3e MC |
285 | if (priv_key == NULL) |
286 | goto err; | |
287 | generate_new_key = 1; | |
8083fd3a | 288 | } else { |
0f113f3e | 289 | priv_key = dh->priv_key; |
8083fd3a | 290 | } |
d02b48c6 | 291 | |
0f113f3e MC |
292 | if (dh->pub_key == NULL) { |
293 | pub_key = BN_new(); | |
294 | if (pub_key == NULL) | |
295 | goto err; | |
8083fd3a | 296 | } else { |
0f113f3e | 297 | pub_key = dh->pub_key; |
0f113f3e | 298 | } |
0f113f3e | 299 | if (generate_new_key) { |
f11f86f6 SL |
300 | /* Is it an approved safe prime ?*/ |
301 | if (DH_get_nid(dh) != NID_undef) { | |
738ee181 | 302 | int max_strength = |
9500c823 | 303 | ossl_ifc_ffc_compute_security_bits(BN_num_bits(dh->params.p)); |
738ee181 | 304 | |
55f02cb6 | 305 | if (dh->params.q == NULL |
f11f86f6 SL |
306 | || dh->length > BN_num_bits(dh->params.q)) |
307 | goto err; | |
738ee181 | 308 | /* dh->length = maximum bit length of generated private key */ |
5357c106 P |
309 | if (!ossl_ffc_generate_private_key(ctx, &dh->params, dh->length, |
310 | max_strength, priv_key)) | |
f11f86f6 SL |
311 | goto err; |
312 | } else { | |
f844f9eb | 313 | #ifdef FIPS_MODULE |
f11f86f6 SL |
314 | if (dh->params.q == NULL) |
315 | goto err; | |
316 | #else | |
317 | if (dh->params.q == NULL) { | |
28e1d588 RL |
318 | /* secret exponent length, must satisfy 2^(l-1) <= p */ |
319 | if (dh->length != 0 | |
320 | && dh->length >= BN_num_bits(dh->params.p)) | |
321 | goto err; | |
f11f86f6 SL |
322 | l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1; |
323 | if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE, | |
5cbd2ea3 | 324 | BN_RAND_BOTTOM_ANY, 0, ctx)) |
f11f86f6 SL |
325 | goto err; |
326 | /* | |
327 | * We handle just one known case where g is a quadratic non-residue: | |
328 | * for g = 2: p % 8 == 3 | |
329 | */ | |
330 | if (BN_is_word(dh->params.g, DH_GENERATOR_2) | |
331 | && !BN_is_bit_set(dh->params.p, 2)) { | |
332 | /* clear bit 0, since it won't be a secret anyway */ | |
333 | if (!BN_clear_bit(priv_key, 0)) | |
334 | goto err; | |
335 | } | |
336 | } else | |
337 | #endif | |
338 | { | |
63794b04 | 339 | /* Do a partial check for invalid p, q, g */ |
5357c106 | 340 | if (!ossl_ffc_params_simple_validate(dh->libctx, &dh->params, |
ba37b820 | 341 | FFC_PARAM_TYPE_DH, NULL)) |
63794b04 | 342 | goto err; |
f11f86f6 SL |
343 | /* |
344 | * For FFC FIPS 186-4 keygen | |
345 | * security strength s = 112, | |
346 | * Max Private key size N = len(q) | |
347 | */ | |
5357c106 P |
348 | if (!ossl_ffc_generate_private_key(ctx, &dh->params, |
349 | BN_num_bits(dh->params.q), | |
350 | MIN_STRENGTH, | |
351 | priv_key)) | |
a38c878c BE |
352 | goto err; |
353 | } | |
0f113f3e MC |
354 | } |
355 | } | |
dfeab068 | 356 | |
19dbb742 | 357 | if (!ossl_dh_generate_public_key(ctx, dh, priv_key, pub_key)) |
8083fd3a | 358 | goto err; |
46a64376 | 359 | |
0f113f3e MC |
360 | dh->pub_key = pub_key; |
361 | dh->priv_key = priv_key; | |
8b84b075 | 362 | dh->dirty_cnt++; |
0f113f3e MC |
363 | ok = 1; |
364 | err: | |
365 | if (ok != 1) | |
9311d0c4 | 366 | ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB); |
d02b48c6 | 367 | |
23a1d5e9 | 368 | if (pub_key != dh->pub_key) |
0f113f3e | 369 | BN_free(pub_key); |
23a1d5e9 | 370 | if (priv_key != dh->priv_key) |
0f113f3e MC |
371 | BN_free(priv_key); |
372 | BN_CTX_free(ctx); | |
26a7d938 | 373 | return ok; |
0f113f3e | 374 | } |
d02b48c6 | 375 | |
19dbb742 | 376 | int ossl_dh_buf2key(DH *dh, const unsigned char *buf, size_t len) |
9aaecbfc | 377 | { |
378 | int err_reason = DH_R_BN_ERROR; | |
379 | BIGNUM *pubkey = NULL; | |
380 | const BIGNUM *p; | |
2c0f7d46 | 381 | int ret; |
9aaecbfc | 382 | |
383 | if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL) | |
384 | goto err; | |
385 | DH_get0_pqg(dh, &p, NULL, NULL); | |
2c0f7d46 | 386 | if (p == NULL || BN_num_bytes(p) == 0) { |
9aaecbfc | 387 | err_reason = DH_R_NO_PARAMETERS_SET; |
388 | goto err; | |
389 | } | |
2c0f7d46 TM |
390 | /* Prevent small subgroup attacks per RFC 8446 Section 4.2.8.1 */ |
391 | if (!ossl_dh_check_pub_key_partial(dh, pubkey, &ret)) { | |
9aaecbfc | 392 | err_reason = DH_R_INVALID_PUBKEY; |
393 | goto err; | |
394 | } | |
395 | if (DH_set0_key(dh, pubkey, NULL) != 1) | |
396 | goto err; | |
397 | return 1; | |
398 | err: | |
9311d0c4 | 399 | ERR_raise(ERR_LIB_DH, err_reason); |
9aaecbfc | 400 | BN_free(pubkey); |
401 | return 0; | |
402 | } | |
403 | ||
19dbb742 SL |
404 | size_t ossl_dh_key2buf(const DH *dh, unsigned char **pbuf_out, size_t size, |
405 | int alloc) | |
9aaecbfc | 406 | { |
407 | const BIGNUM *pubkey; | |
6a9bd929 | 408 | unsigned char *pbuf = NULL; |
9aaecbfc | 409 | const BIGNUM *p; |
410 | int p_size; | |
411 | ||
412 | DH_get0_pqg(dh, &p, NULL, NULL); | |
413 | DH_get0_key(dh, &pubkey, NULL); | |
414 | if (p == NULL || pubkey == NULL | |
415 | || (p_size = BN_num_bytes(p)) == 0 | |
416 | || BN_num_bytes(pubkey) == 0) { | |
9311d0c4 | 417 | ERR_raise(ERR_LIB_DH, DH_R_INVALID_PUBKEY); |
9aaecbfc | 418 | return 0; |
419 | } | |
6a9bd929 MC |
420 | if (pbuf_out != NULL && (alloc || *pbuf_out != NULL)) { |
421 | if (!alloc) { | |
422 | if (size >= (size_t)p_size) | |
423 | pbuf = *pbuf_out; | |
e077455e RL |
424 | if (pbuf == NULL) |
425 | ERR_raise(ERR_LIB_DH, DH_R_INVALID_SIZE); | |
6a9bd929 MC |
426 | } else { |
427 | pbuf = OPENSSL_malloc(p_size); | |
428 | } | |
429 | ||
e077455e RL |
430 | /* Errors raised above */ |
431 | if (pbuf == NULL) | |
6a9bd929 | 432 | return 0; |
6a9bd929 MC |
433 | /* |
434 | * As per Section 4.2.8.1 of RFC 8446 left pad public | |
435 | * key with zeros to the size of p | |
436 | */ | |
437 | if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) { | |
438 | if (alloc) | |
439 | OPENSSL_free(pbuf); | |
9311d0c4 | 440 | ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR); |
6a9bd929 MC |
441 | return 0; |
442 | } | |
443 | *pbuf_out = pbuf; | |
9aaecbfc | 444 | } |
9aaecbfc | 445 | return p_size; |
446 | } |