2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DH low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
19 #include "crypto/bn.h"
20 #include "crypto/dh.h"
21 #include "crypto/security_bits.h"
24 # define MIN_STRENGTH 112
26 # define MIN_STRENGTH 80
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);
37 * See SP800-56Ar3 Section 5.7.1.1
38 * Finite Field Cryptography Diffie-Hellman (FFC DH) Primitive
40 int ossl_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
43 BN_MONT_CTX *mont = NULL;
44 BIGNUM *z = NULL, *pminus1;
47 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
48 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
52 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
53 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
57 ctx = BN_CTX_new_ex(dh->libctx);
61 pminus1 = BN_CTX_get(ctx);
66 if (dh->priv_key == NULL) {
67 ERR_raise(ERR_LIB_DH, DH_R_NO_PRIVATE_VALUE);
71 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
72 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
73 dh->lock, dh->params.p, ctx);
74 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
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,
82 ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
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);
95 /* return the padded key, i.e. same number of bytes as the modulus */
96 ret = BN_bn2binpad(z, key, BN_num_bytes(dh->params.p));
98 BN_clear(z); /* (Step 2) destroy intermediate values */
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.
108 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
111 volatile size_t npad = 0, mask = 1;
113 /* compute the key; ret is constant unless compute_key is external */
115 ret = ossl_dh_compute_key(key, pub_key, dh);
117 ret = dh->meth->compute_key(key, pub_key, dh);
122 /* count leading zero bytes, yet still touch all bytes */
123 for (i = 0; i < ret; i++) {
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);
138 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
142 /* rv is constant unless compute_key is external */
144 rv = ossl_dh_compute_key(key, pub_key, dh);
146 rv = dh->meth->compute_key(key, pub_key, dh);
150 pad = BN_num_bytes(dh->params.p) - rv;
151 /* pad is constant (zero) unless compute_key is external */
153 memmove(key + pad, key, rv);
159 static DH_METHOD dh_ossl = {
171 static const DH_METHOD *default_DH_method = &dh_ossl;
173 const DH_METHOD *DH_OpenSSL(void)
178 const DH_METHOD *DH_get_default_method(void)
180 return default_DH_method;
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)
187 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
190 static int dh_init(DH *dh)
192 dh->flags |= DH_FLAG_CACHE_MONT_P;
193 ossl_ffc_params_init(&dh->params);
198 static int dh_finish(DH *dh)
200 BN_MONT_CTX_free(dh->method_mont_p);
205 void DH_set_default_method(const DH_METHOD *meth)
207 default_DH_method = meth;
209 #endif /* FIPS_MODULE */
211 int DH_generate_key(DH *dh)
214 return generate_key(dh);
216 return dh->meth->generate_key(dh);
220 int ossl_dh_generate_public_key(BN_CTX *ctx, const DH *dh,
221 const BIGNUM *priv_key, BIGNUM *pub_key)
224 BIGNUM *prk = BN_new();
225 BN_MONT_CTX *mont = NULL;
230 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
232 * We take the input DH as const, but we lie, because in some cases we
233 * want to get a hold of its Montgomery context.
235 * We cast to remove the const qualifier in this case, it should be
238 BN_MONT_CTX **pmont = (BN_MONT_CTX **)&dh->method_mont_p;
240 mont = BN_MONT_CTX_set_locked(pmont, dh->lock, dh->params.p, ctx);
244 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
246 /* pub_key = g^priv_key mod p */
247 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
256 static int generate_key(DH *dh)
259 int generate_new_key = 0;
264 BIGNUM *pub_key = NULL, *priv_key = NULL;
266 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
267 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
271 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
272 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
276 ctx = BN_CTX_new_ex(dh->libctx);
280 if (dh->priv_key == NULL) {
281 priv_key = BN_secure_new();
282 if (priv_key == NULL)
284 generate_new_key = 1;
286 priv_key = dh->priv_key;
289 if (dh->pub_key == NULL) {
294 pub_key = dh->pub_key;
296 if (generate_new_key) {
297 /* Is it an approved safe prime ?*/
298 if (DH_get_nid(dh) != NID_undef) {
300 ossl_ifc_ffc_compute_security_bits(BN_num_bits(dh->params.p));
302 if (dh->params.q == NULL
303 || dh->length > BN_num_bits(dh->params.q))
305 /* dh->length = maximum bit length of generated private key */
306 if (!ossl_ffc_generate_private_key(ctx, &dh->params, dh->length,
307 max_strength, priv_key))
311 if (dh->params.q == NULL)
314 if (dh->params.q == NULL) {
315 /* secret exponent length, must satisfy 2^(l-1) <= p */
317 && dh->length >= BN_num_bits(dh->params.p))
319 l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1;
320 if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE,
321 BN_RAND_BOTTOM_ANY, 0, ctx))
324 * We handle just one known case where g is a quadratic non-residue:
325 * for g = 2: p % 8 == 3
327 if (BN_is_word(dh->params.g, DH_GENERATOR_2)
328 && !BN_is_bit_set(dh->params.p, 2)) {
329 /* clear bit 0, since it won't be a secret anyway */
330 if (!BN_clear_bit(priv_key, 0))
336 /* Do a partial check for invalid p, q, g */
337 if (!ossl_ffc_params_simple_validate(dh->libctx, &dh->params,
338 FFC_PARAM_TYPE_DH, NULL))
341 * For FFC FIPS 186-4 keygen
342 * security strength s = 112,
343 * Max Private key size N = len(q)
345 if (!ossl_ffc_generate_private_key(ctx, &dh->params,
346 BN_num_bits(dh->params.q),
354 if (!ossl_dh_generate_public_key(ctx, dh, priv_key, pub_key))
357 dh->pub_key = pub_key;
358 dh->priv_key = priv_key;
363 ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
365 if (pub_key != dh->pub_key)
367 if (priv_key != dh->priv_key)
373 int ossl_dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
375 int err_reason = DH_R_BN_ERROR;
376 BIGNUM *pubkey = NULL;
380 if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
382 DH_get0_pqg(dh, &p, NULL, NULL);
383 if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
384 err_reason = DH_R_NO_PARAMETERS_SET;
388 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
389 * public key is of size not equal to size of p
391 if (BN_is_zero(pubkey) || p_size != len) {
392 err_reason = DH_R_INVALID_PUBKEY;
395 if (DH_set0_key(dh, pubkey, NULL) != 1)
399 ERR_raise(ERR_LIB_DH, err_reason);
404 size_t ossl_dh_key2buf(const DH *dh, unsigned char **pbuf_out, size_t size,
407 const BIGNUM *pubkey;
408 unsigned char *pbuf = NULL;
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) {
417 ERR_raise(ERR_LIB_DH, DH_R_INVALID_PUBKEY);
420 if (pbuf_out != NULL && (alloc || *pbuf_out != NULL)) {
422 if (size >= (size_t)p_size)
425 pbuf = OPENSSL_malloc(p_size);
429 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
433 * As per Section 4.2.8.1 of RFC 8446 left pad public
434 * key with zeros to the size of p
436 if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
439 ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);