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 * 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);
36 static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
39 BN_MONT_CTX *mont = NULL;
46 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
47 DHerr(0, DH_R_MODULUS_TOO_LARGE);
51 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
52 DHerr(0, DH_R_MODULUS_TOO_SMALL);
56 ctx = BN_CTX_new_ex(dh->libctx);
60 tmp = BN_CTX_get(ctx);
64 if (dh->priv_key == NULL) {
65 DHerr(0, DH_R_NO_PRIVATE_VALUE);
69 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
70 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
71 dh->lock, dh->params.p, ctx);
72 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
76 /* TODO(3.0) : Solve in a PR related to Key validation for DH */
78 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
79 DHerr(0, DH_R_INVALID_PUBKEY);
83 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->params.p, ctx,
85 DHerr(0, ERR_R_BN_LIB);
89 ret = BN_bn2bin(tmp, key);
96 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
99 return compute_key(key, pub_key, dh);
101 return dh->meth->compute_key(key, pub_key, dh);
105 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
110 rv = compute_key(key, pub_key, dh);
112 rv = dh->meth->compute_key(key, pub_key, dh);
116 pad = BN_num_bytes(dh->params.p) - rv;
118 memmove(key + pad, key, rv);
124 static DH_METHOD dh_ossl = {
136 static const DH_METHOD *default_DH_method = &dh_ossl;
138 const DH_METHOD *DH_OpenSSL(void)
143 const DH_METHOD *DH_get_default_method(void)
145 return default_DH_method;
148 static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
149 const BIGNUM *a, const BIGNUM *p,
150 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
152 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
155 static int dh_init(DH *dh)
157 dh->flags |= DH_FLAG_CACHE_MONT_P;
158 ffc_params_init(&dh->params);
163 static int dh_finish(DH *dh)
165 BN_MONT_CTX_free(dh->method_mont_p);
170 void DH_set_default_method(const DH_METHOD *meth)
172 default_DH_method = meth;
174 #endif /* FIPS_MODE */
176 int DH_generate_key(DH *dh)
179 return generate_key(dh);
181 return dh->meth->generate_key(dh);
185 int dh_generate_public_key(BN_CTX *ctx, DH *dh, const BIGNUM *priv_key,
189 BIGNUM *prk = BN_new();
190 BN_MONT_CTX *mont = NULL;
195 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
196 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
197 dh->lock, dh->params.p, ctx);
201 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
203 /* pub_key = g^priv_key mod p */
204 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
213 static int generate_key(DH *dh)
216 int generate_new_key = 0;
221 BIGNUM *pub_key = NULL, *priv_key = NULL;
223 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
224 DHerr(0, DH_R_MODULUS_TOO_LARGE);
228 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
229 DHerr(0, DH_R_MODULUS_TOO_SMALL);
233 ctx = BN_CTX_new_ex(dh->libctx);
237 if (dh->priv_key == NULL) {
238 priv_key = BN_secure_new();
239 if (priv_key == NULL)
241 generate_new_key = 1;
243 priv_key = dh->priv_key;
246 if (dh->pub_key == NULL) {
251 pub_key = dh->pub_key;
253 if (generate_new_key) {
254 /* Is it an approved safe prime ?*/
255 if (DH_get_nid(dh) != NID_undef) {
257 ifc_ffc_compute_security_bits(BN_num_bits(dh->params.p));
259 if (dh->params.q == NULL
260 || dh->length > BN_num_bits(dh->params.q))
262 /* dh->length = maximum bit length of generated private key */
263 if (!ffc_generate_private_key(ctx, &dh->params, dh->length,
264 max_strength, priv_key))
268 if (dh->params.q == NULL)
271 if (dh->params.q == NULL) {
272 /* secret exponent length */
273 l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1;
274 if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE,
275 BN_RAND_BOTTOM_ANY, ctx))
278 * We handle just one known case where g is a quadratic non-residue:
279 * for g = 2: p % 8 == 3
281 if (BN_is_word(dh->params.g, DH_GENERATOR_2)
282 && !BN_is_bit_set(dh->params.p, 2)) {
283 /* clear bit 0, since it won't be a secret anyway */
284 if (!BN_clear_bit(priv_key, 0))
291 * For FFC FIPS 186-4 keygen
292 * security strength s = 112,
293 * Max Private key size N = len(q)
295 if (!ffc_generate_private_key(ctx, &dh->params,
296 BN_num_bits(dh->params.q),
304 if (!dh_generate_public_key(ctx, dh, priv_key, pub_key))
307 dh->pub_key = pub_key;
308 dh->priv_key = priv_key;
313 DHerr(0, ERR_R_BN_LIB);
315 if (pub_key != dh->pub_key)
317 if (priv_key != dh->priv_key)
323 int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
325 int err_reason = DH_R_BN_ERROR;
326 BIGNUM *pubkey = NULL;
330 if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
332 DH_get0_pqg(dh, &p, NULL, NULL);
333 if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
334 err_reason = DH_R_NO_PARAMETERS_SET;
338 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
339 * public key is of size not equal to size of p
341 if (BN_is_zero(pubkey) || p_size != len) {
342 err_reason = DH_R_INVALID_PUBKEY;
345 if (DH_set0_key(dh, pubkey, NULL) != 1)
349 DHerr(DH_F_DH_BUF2KEY, err_reason);
354 size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
356 const BIGNUM *pubkey;
361 DH_get0_pqg(dh, &p, NULL, NULL);
362 DH_get0_key(dh, &pubkey, NULL);
363 if (p == NULL || pubkey == NULL
364 || (p_size = BN_num_bytes(p)) == 0
365 || BN_num_bytes(pubkey) == 0) {
366 DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY);
369 if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
370 DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
374 * As per Section 4.2.8.1 of RFC 8446 left pad public
375 * key with zeros to the size of p
377 if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
379 DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);