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"
15 static int generate_key(DH *dh);
16 static int compute_key(unsigned char *key, const BIGNUM *pub_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 int DH_generate_key(DH *dh)
25 return dh->meth->generate_key(dh);
28 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
30 return dh->meth->compute_key(key, pub_key, dh);
33 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
36 rv = dh->meth->compute_key(key, pub_key, dh);
39 pad = BN_num_bytes(dh->p) - rv;
41 memmove(key + pad, key, rv);
47 static DH_METHOD dh_ossl = {
59 static const DH_METHOD *default_DH_method = &dh_ossl;
61 const DH_METHOD *DH_OpenSSL(void)
66 void DH_set_default_method(const DH_METHOD *meth)
68 default_DH_method = meth;
71 const DH_METHOD *DH_get_default_method(void)
73 return default_DH_method;
76 static int generate_key(DH *dh)
79 int generate_new_key = 0;
82 BN_MONT_CTX *mont = NULL;
83 BIGNUM *pub_key = NULL, *priv_key = NULL;
85 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
86 DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_LARGE);
90 if (BN_num_bits(dh->p) < DH_MIN_MODULUS_BITS) {
91 DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_SMALL);
99 if (dh->priv_key == NULL) {
100 priv_key = BN_secure_new();
101 if (priv_key == NULL)
103 generate_new_key = 1;
105 priv_key = dh->priv_key;
107 if (dh->pub_key == NULL) {
112 pub_key = dh->pub_key;
114 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
115 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
116 dh->lock, dh->p, ctx);
121 if (generate_new_key) {
124 if (!BN_priv_rand_range(priv_key, dh->q))
127 while (BN_is_zero(priv_key) || BN_is_one(priv_key));
129 /* secret exponent length */
130 l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
131 if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
134 * We handle just one known case where g is a quadratic non-residue:
135 * for g = 2: p % 8 == 3
137 if (BN_is_word(dh->g, DH_GENERATOR_2) && !BN_is_bit_set(dh->p, 2)) {
138 /* clear bit 0, since it won't be a secret anyway */
139 if (!BN_clear_bit(priv_key, 0))
146 BIGNUM *prk = BN_new();
150 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
152 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
156 /* We MUST free prk before any further use of priv_key */
160 dh->pub_key = pub_key;
161 dh->priv_key = priv_key;
166 DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
168 if (pub_key != dh->pub_key)
170 if (priv_key != dh->priv_key)
176 static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
179 BN_MONT_CTX *mont = NULL;
184 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
185 DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
189 if (BN_num_bits(dh->p) < DH_MIN_MODULUS_BITS) {
190 DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_SMALL);
198 tmp = BN_CTX_get(ctx);
202 if (dh->priv_key == NULL) {
203 DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
207 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
208 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
209 dh->lock, dh->p, ctx);
210 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
215 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
216 DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
221 meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
222 DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
226 ret = BN_bn2bin(tmp, key);
233 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
234 const BIGNUM *a, const BIGNUM *p,
235 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
237 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
240 static int dh_init(DH *dh)
242 dh->flags |= DH_FLAG_CACHE_MONT_P;
246 static int dh_finish(DH *dh)
248 BN_MONT_CTX_free(dh->method_mont_p);
252 int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
254 int err_reason = DH_R_BN_ERROR;
255 BIGNUM *pubkey = NULL;
259 if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
261 DH_get0_pqg(dh, &p, NULL, NULL);
262 if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
263 err_reason = DH_R_NO_PARAMETERS_SET;
267 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
268 * public key is of size not equal to size of p
270 if (BN_is_zero(pubkey) || p_size != len) {
271 err_reason = DH_R_INVALID_PUBKEY;
274 if (DH_set0_key(dh, pubkey, NULL) != 1)
278 DHerr(DH_F_DH_BUF2KEY, err_reason);
283 size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
285 const BIGNUM *pubkey;
290 DH_get0_pqg(dh, &p, NULL, NULL);
291 DH_get0_key(dh, &pubkey, NULL);
292 if (p == NULL || pubkey == NULL
293 || (p_size = BN_num_bytes(p)) == 0
294 || BN_num_bytes(pubkey) == 0) {
295 DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY);
298 if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
299 DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
303 * As per Section 4.2.8.1 of RFC 8446 left pad public
304 * key with zeros to the size of p
306 if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
308 DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);