2 * Copyright 1995-2019 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"
17 static int generate_key(DH *dh);
18 #endif /* FIPS_MODE */
20 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
21 const BIGNUM *a, const BIGNUM *p,
22 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
23 static int dh_init(DH *dh);
24 static int dh_finish(DH *dh);
26 int dh_compute_key(OPENSSL_CTX *libctx, unsigned char *key,
27 const BIGNUM *pub_key, DH *dh)
30 BN_MONT_CTX *mont = NULL;
37 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
38 DHerr(0, DH_R_MODULUS_TOO_LARGE);
42 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
43 DHerr(0, DH_R_MODULUS_TOO_SMALL);
47 ctx = BN_CTX_new_ex(libctx);
51 tmp = BN_CTX_get(ctx);
55 if (dh->priv_key == NULL) {
56 DHerr(0, DH_R_NO_PRIVATE_VALUE);
60 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
61 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
62 dh->lock, dh->params.p, ctx);
63 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
67 /* TODO(3.0) : Solve in a PR related to Key validation for DH */
69 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
70 DHerr(0, DH_R_INVALID_PUBKEY);
74 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->params.p, ctx,
76 DHerr(0, ERR_R_BN_LIB);
80 ret = BN_bn2bin(tmp, key);
87 static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
89 return dh_compute_key(NULL, key, pub_key, dh);
92 int dh_compute_key_padded(OPENSSL_CTX *libctx, unsigned char *key,
93 const BIGNUM *pub_key, DH *dh)
98 rv = dh_compute_key(libctx, key, pub_key, dh);
100 rv = dh->meth->compute_key(key, pub_key, dh);
104 pad = BN_num_bytes(dh->params.p) - rv;
106 memmove(key + pad, key, rv);
113 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
115 return dh->meth->compute_key(key, pub_key, dh);
118 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
120 return dh_compute_key_padded(NULL, key, pub_key, dh);
124 static DH_METHOD dh_ossl = {
129 NULL, /* TODO(3.0) : solve this in a keygen related PR */
140 static const DH_METHOD *default_DH_method = &dh_ossl;
142 const DH_METHOD *DH_OpenSSL(void)
147 const DH_METHOD *DH_get_default_method(void)
149 return default_DH_method;
152 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
153 const BIGNUM *a, const BIGNUM *p,
154 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
156 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
159 static int dh_init(DH *dh)
161 dh->flags |= DH_FLAG_CACHE_MONT_P;
162 ffc_params_init(&dh->params);
166 static int dh_finish(DH *dh)
168 BN_MONT_CTX_free(dh->method_mont_p);
174 void DH_set_default_method(const DH_METHOD *meth)
176 default_DH_method = meth;
179 int DH_generate_key(DH *dh)
181 return dh->meth->generate_key(dh);
184 static int generate_key(DH *dh)
187 int generate_new_key = 0;
190 BN_MONT_CTX *mont = NULL;
191 BIGNUM *pub_key = NULL, *priv_key = NULL;
193 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
194 DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_LARGE);
198 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
199 DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_SMALL);
207 if (dh->priv_key == NULL) {
208 priv_key = BN_secure_new();
209 if (priv_key == NULL)
211 generate_new_key = 1;
213 priv_key = dh->priv_key;
215 if (dh->pub_key == NULL) {
220 pub_key = dh->pub_key;
222 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
223 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
224 dh->lock, dh->params.p, ctx);
229 if (generate_new_key) {
230 if (dh->params.q != NULL) {
232 if (!BN_priv_rand_range(priv_key, dh->params.q))
235 while (BN_is_zero(priv_key) || BN_is_one(priv_key));
237 /* secret exponent length */
238 l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1;
239 if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
242 * We handle just one known case where g is a quadratic non-residue:
243 * for g = 2: p % 8 == 3
245 if (BN_is_word(dh->params.g, DH_GENERATOR_2)
246 && !BN_is_bit_set(dh->params.p, 2)) {
247 /* clear bit 0, since it won't be a secret anyway */
248 if (!BN_clear_bit(priv_key, 0))
255 BIGNUM *prk = BN_new();
259 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
261 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
266 /* We MUST free prk before any further use of priv_key */
270 dh->pub_key = pub_key;
271 dh->priv_key = priv_key;
276 DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
278 if (pub_key != dh->pub_key)
280 if (priv_key != dh->priv_key)
287 int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
289 int err_reason = DH_R_BN_ERROR;
290 BIGNUM *pubkey = NULL;
294 if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
296 DH_get0_pqg(dh, &p, NULL, NULL);
297 if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
298 err_reason = DH_R_NO_PARAMETERS_SET;
302 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
303 * public key is of size not equal to size of p
305 if (BN_is_zero(pubkey) || p_size != len) {
306 err_reason = DH_R_INVALID_PUBKEY;
309 if (DH_set0_key(dh, pubkey, NULL) != 1)
313 DHerr(DH_F_DH_BUF2KEY, err_reason);
318 size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
320 const BIGNUM *pubkey;
325 DH_get0_pqg(dh, &p, NULL, NULL);
326 DH_get0_key(dh, &pubkey, NULL);
327 if (p == NULL || pubkey == NULL
328 || (p_size = BN_num_bytes(p)) == 0
329 || BN_num_bytes(pubkey) == 0) {
330 DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY);
333 if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
334 DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
338 * As per Section 4.2.8.1 of RFC 8446 left pad public
339 * key with zeros to the size of p
341 if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
343 DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);
349 #endif /* FIPS_MODE */