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