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 int dh_compute_key(OPENSSL_CTX *libctx, unsigned char *key,
24 const BIGNUM *pub_key, DH *dh)
27 BN_MONT_CTX *mont = NULL;
34 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
35 DHerr(0, DH_R_MODULUS_TOO_LARGE);
39 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
40 DHerr(0, DH_R_MODULUS_TOO_SMALL);
44 ctx = BN_CTX_new_ex(libctx);
48 tmp = BN_CTX_get(ctx);
52 if (dh->priv_key == NULL) {
53 DHerr(0, DH_R_NO_PRIVATE_VALUE);
57 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
58 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
59 dh->lock, dh->params.p, ctx);
60 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
64 /* TODO(3.0) : Solve in a PR related to Key validation for DH */
66 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
67 DHerr(0, DH_R_INVALID_PUBKEY);
71 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->params.p, ctx,
73 DHerr(0, ERR_R_BN_LIB);
77 ret = BN_bn2bin(tmp, key);
84 static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
86 return dh_compute_key(NULL, key, pub_key, dh);
89 int dh_compute_key_padded(OPENSSL_CTX *libctx, unsigned char *key,
90 const BIGNUM *pub_key, DH *dh)
95 rv = dh_compute_key(libctx, key, pub_key, dh);
97 rv = dh->meth->compute_key(key, pub_key, dh);
101 pad = BN_num_bytes(dh->params.p) - rv;
103 memmove(key + pad, key, rv);
110 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
112 return dh->meth->compute_key(key, pub_key, dh);
115 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
117 return dh_compute_key_padded(NULL, key, pub_key, dh);
121 static DH_METHOD dh_ossl = {
133 static const DH_METHOD *default_DH_method = &dh_ossl;
135 const DH_METHOD *DH_OpenSSL(void)
140 const DH_METHOD *DH_get_default_method(void)
142 return default_DH_method;
145 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
146 const BIGNUM *a, const BIGNUM *p,
147 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
149 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
152 static int dh_init(DH *dh)
154 dh->flags |= DH_FLAG_CACHE_MONT_P;
155 ffc_params_init(&dh->params);
160 static int dh_finish(DH *dh)
162 BN_MONT_CTX_free(dh->method_mont_p);
167 void DH_set_default_method(const DH_METHOD *meth)
169 default_DH_method = meth;
172 int DH_generate_key(DH *dh)
174 return dh->meth->generate_key(dh);
176 #endif /* FIPS_MODE */
178 static int dh_generate_key(OPENSSL_CTX *libctx, DH *dh)
181 int generate_new_key = 0;
186 BN_MONT_CTX *mont = NULL;
187 BIGNUM *pub_key = NULL, *priv_key = NULL;
189 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
190 DHerr(0, DH_R_MODULUS_TOO_LARGE);
194 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
195 DHerr(0, DH_R_MODULUS_TOO_SMALL);
199 ctx = BN_CTX_new_ex(libctx);
203 if (dh->priv_key == NULL) {
204 priv_key = BN_secure_new();
205 if (priv_key == NULL)
207 generate_new_key = 1;
209 priv_key = dh->priv_key;
211 if (dh->pub_key == NULL) {
216 pub_key = dh->pub_key;
218 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
219 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
220 dh->lock, dh->params.p, ctx);
225 if (generate_new_key) {
226 /* Is it an approved safe prime ?*/
227 if (DH_get_nid(dh) != NID_undef) {
229 * The safe prime group code sets N = 2*s
230 * (where s = max security strength supported).
231 * N = dh->length (N = maximum bit length of private key)
234 || dh->params.q == NULL
235 || dh->length > BN_num_bits(dh->params.q))
237 if (!ffc_generate_private_key(ctx, &dh->params, dh->length,
238 dh->length / 2, priv_key))
242 if (dh->params.q == NULL)
245 if (dh->params.q == NULL) {
246 /* secret exponent length */
247 l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1;
248 if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE,
249 BN_RAND_BOTTOM_ANY, ctx))
252 * We handle just one known case where g is a quadratic non-residue:
253 * for g = 2: p % 8 == 3
255 if (BN_is_word(dh->params.g, DH_GENERATOR_2)
256 && !BN_is_bit_set(dh->params.p, 2)) {
257 /* clear bit 0, since it won't be a secret anyway */
258 if (!BN_clear_bit(priv_key, 0))
265 * For FFC FIPS 186-4 keygen
266 * security strength s = 112,
267 * Max Private key size N = len(q)
269 if (!ffc_generate_private_key(ctx, &dh->params,
270 BN_num_bits(dh->params.q), 112,
278 BIGNUM *prk = BN_new();
282 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
284 /* pub_key = g^priv_key mod p */
285 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
290 /* We MUST free prk before any further use of priv_key */
294 dh->pub_key = pub_key;
295 dh->priv_key = priv_key;
300 DHerr(0, ERR_R_BN_LIB);
302 if (pub_key != dh->pub_key)
304 if (priv_key != dh->priv_key)
310 static int generate_key(DH *dh)
312 return dh_generate_key(NULL, dh);
315 int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
317 int err_reason = DH_R_BN_ERROR;
318 BIGNUM *pubkey = NULL;
322 if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
324 DH_get0_pqg(dh, &p, NULL, NULL);
325 if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
326 err_reason = DH_R_NO_PARAMETERS_SET;
330 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
331 * public key is of size not equal to size of p
333 if (BN_is_zero(pubkey) || p_size != len) {
334 err_reason = DH_R_INVALID_PUBKEY;
337 if (DH_set0_key(dh, pubkey, NULL) != 1)
341 DHerr(DH_F_DH_BUF2KEY, err_reason);
346 size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
348 const BIGNUM *pubkey;
353 DH_get0_pqg(dh, &p, NULL, NULL);
354 DH_get0_key(dh, &pubkey, NULL);
355 if (p == NULL || pubkey == NULL
356 || (p_size = BN_num_bytes(p)) == 0
357 || BN_num_bytes(pubkey) == 0) {
358 DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY);
361 if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
362 DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
366 * As per Section 4.2.8.1 of RFC 8446 left pad public
367 * key with zeros to the size of p
369 if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
371 DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);