2 * Copyright 1995-2020 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 #include "internal/cryptlib.h"
13 #include "crypto/bn.h"
14 #include "crypto/dh.h"
16 static int generate_key(DH *dh);
17 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
18 const BIGNUM *a, const BIGNUM *p,
19 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
20 static int dh_init(DH *dh);
21 static int dh_finish(DH *dh);
23 static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
26 BN_MONT_CTX *mont = NULL;
33 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
34 DHerr(0, DH_R_MODULUS_TOO_LARGE);
38 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
39 DHerr(0, DH_R_MODULUS_TOO_SMALL);
43 ctx = BN_CTX_new_ex(dh->libctx);
47 tmp = BN_CTX_get(ctx);
51 if (dh->priv_key == NULL) {
52 DHerr(0, DH_R_NO_PRIVATE_VALUE);
56 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
57 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
58 dh->lock, dh->params.p, ctx);
59 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
63 /* TODO(3.0) : Solve in a PR related to Key validation for DH */
65 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
66 DHerr(0, DH_R_INVALID_PUBKEY);
70 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->params.p, ctx,
72 DHerr(0, ERR_R_BN_LIB);
76 ret = BN_bn2bin(tmp, key);
83 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
86 return compute_key(key, pub_key, dh);
88 return dh->meth->compute_key(key, pub_key, dh);
92 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
97 rv = compute_key(key, pub_key, dh);
99 rv = dh->meth->compute_key(key, pub_key, dh);
103 pad = BN_num_bytes(dh->params.p) - rv;
105 memmove(key + pad, key, rv);
111 static DH_METHOD dh_ossl = {
123 static const DH_METHOD *default_DH_method = &dh_ossl;
125 const DH_METHOD *DH_OpenSSL(void)
130 const DH_METHOD *DH_get_default_method(void)
132 return default_DH_method;
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)
139 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
142 static int dh_init(DH *dh)
144 dh->flags |= DH_FLAG_CACHE_MONT_P;
145 ffc_params_init(&dh->params);
150 static int dh_finish(DH *dh)
152 BN_MONT_CTX_free(dh->method_mont_p);
157 void DH_set_default_method(const DH_METHOD *meth)
159 default_DH_method = meth;
161 #endif /* FIPS_MODE */
163 int DH_generate_key(DH *dh)
166 return generate_key(dh);
168 return dh->meth->generate_key(dh);
172 int dh_generate_public_key(BN_CTX *ctx, DH *dh, const BIGNUM *priv_key,
176 BIGNUM *prk = BN_new();
177 BN_MONT_CTX *mont = NULL;
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);
188 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
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,
200 static int generate_key(DH *dh)
203 int generate_new_key = 0;
208 BIGNUM *pub_key = NULL, *priv_key = NULL;
210 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
211 DHerr(0, DH_R_MODULUS_TOO_LARGE);
215 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
216 DHerr(0, DH_R_MODULUS_TOO_SMALL);
220 ctx = BN_CTX_new_ex(dh->libctx);
224 if (dh->priv_key == NULL) {
225 priv_key = BN_secure_new();
226 if (priv_key == NULL)
228 generate_new_key = 1;
230 priv_key = dh->priv_key;
233 if (dh->pub_key == NULL) {
238 pub_key = dh->pub_key;
240 if (generate_new_key) {
241 /* Is it an approved safe prime ?*/
242 if (DH_get_nid(dh) != NID_undef) {
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)
249 || dh->params.q == NULL
250 || dh->length > BN_num_bits(dh->params.q))
252 if (!ffc_generate_private_key(ctx, &dh->params, dh->length,
253 dh->length / 2, priv_key))
257 if (dh->params.q == NULL)
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))
267 * We handle just one known case where g is a quadratic non-residue:
268 * for g = 2: p % 8 == 3
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))
280 * For FFC FIPS 186-4 keygen
281 * security strength s = 112,
282 * Max Private key size N = len(q)
284 if (!ffc_generate_private_key(ctx, &dh->params,
285 BN_num_bits(dh->params.q), 112,
292 if (!dh_generate_public_key(ctx, dh, priv_key, pub_key))
295 dh->pub_key = pub_key;
296 dh->priv_key = priv_key;
301 DHerr(0, ERR_R_BN_LIB);
303 if (pub_key != dh->pub_key)
305 if (priv_key != dh->priv_key)
311 int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
313 int err_reason = DH_R_BN_ERROR;
314 BIGNUM *pubkey = NULL;
318 if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
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;
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
329 if (BN_is_zero(pubkey) || p_size != len) {
330 err_reason = DH_R_INVALID_PUBKEY;
333 if (DH_set0_key(dh, pubkey, NULL) != 1)
337 DHerr(DH_F_DH_BUF2KEY, err_reason);
342 size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
344 const BIGNUM *pubkey;
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);
357 if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
358 DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
362 * As per Section 4.2.8.1 of RFC 8446 left pad public
363 * key with zeros to the size of p
365 if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
367 DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);