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