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"
22 static int generate_key(DH
*dh
);
23 static int dh_bn_mod_exp(const DH
*dh
, BIGNUM
*r
,
24 const BIGNUM
*a
, const BIGNUM
*p
,
25 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
);
26 static int dh_init(DH
*dh
);
27 static int dh_finish(DH
*dh
);
29 static int compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
32 BN_MONT_CTX
*mont
= NULL
;
39 if (BN_num_bits(dh
->params
.p
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
40 DHerr(0, DH_R_MODULUS_TOO_LARGE
);
44 if (BN_num_bits(dh
->params
.p
) < DH_MIN_MODULUS_BITS
) {
45 DHerr(0, DH_R_MODULUS_TOO_SMALL
);
49 ctx
= BN_CTX_new_ex(dh
->libctx
);
53 tmp
= BN_CTX_get(ctx
);
57 if (dh
->priv_key
== NULL
) {
58 DHerr(0, DH_R_NO_PRIVATE_VALUE
);
62 if (dh
->flags
& DH_FLAG_CACHE_MONT_P
) {
63 mont
= BN_MONT_CTX_set_locked(&dh
->method_mont_p
,
64 dh
->lock
, dh
->params
.p
, ctx
);
65 BN_set_flags(dh
->priv_key
, BN_FLG_CONSTTIME
);
69 /* TODO(3.0) : Solve in a PR related to Key validation for DH */
71 if (!DH_check_pub_key(dh
, pub_key
, &check_result
) || check_result
) {
72 DHerr(0, DH_R_INVALID_PUBKEY
);
76 if (!dh
->meth
->bn_mod_exp(dh
, tmp
, pub_key
, dh
->priv_key
, dh
->params
.p
, ctx
,
78 DHerr(0, ERR_R_BN_LIB
);
82 ret
= BN_bn2bin(tmp
, key
);
89 int DH_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
92 return compute_key(key
, pub_key
, dh
);
94 return dh
->meth
->compute_key(key
, pub_key
, dh
);
98 int DH_compute_key_padded(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
103 rv
= compute_key(key
, pub_key
, dh
);
105 rv
= dh
->meth
->compute_key(key
, pub_key
, dh
);
109 pad
= BN_num_bytes(dh
->params
.p
) - rv
;
111 memmove(key
+ pad
, key
, rv
);
117 static DH_METHOD dh_ossl
= {
129 static const DH_METHOD
*default_DH_method
= &dh_ossl
;
131 const DH_METHOD
*DH_OpenSSL(void)
136 const DH_METHOD
*DH_get_default_method(void)
138 return default_DH_method
;
141 static int dh_bn_mod_exp(const DH
*dh
, BIGNUM
*r
,
142 const BIGNUM
*a
, const BIGNUM
*p
,
143 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
145 return BN_mod_exp_mont(r
, a
, p
, m
, ctx
, m_ctx
);
148 static int dh_init(DH
*dh
)
150 dh
->flags
|= DH_FLAG_CACHE_MONT_P
;
151 ffc_params_init(&dh
->params
);
156 static int dh_finish(DH
*dh
)
158 BN_MONT_CTX_free(dh
->method_mont_p
);
163 void DH_set_default_method(const DH_METHOD
*meth
)
165 default_DH_method
= meth
;
167 #endif /* FIPS_MODE */
169 int DH_generate_key(DH
*dh
)
172 return generate_key(dh
);
174 return dh
->meth
->generate_key(dh
);
178 int dh_generate_public_key(BN_CTX
*ctx
, DH
*dh
, const BIGNUM
*priv_key
,
182 BIGNUM
*prk
= BN_new();
183 BN_MONT_CTX
*mont
= NULL
;
188 if (dh
->flags
& DH_FLAG_CACHE_MONT_P
) {
189 mont
= BN_MONT_CTX_set_locked(&dh
->method_mont_p
,
190 dh
->lock
, dh
->params
.p
, ctx
);
194 BN_with_flags(prk
, priv_key
, BN_FLG_CONSTTIME
);
196 /* pub_key = g^priv_key mod p */
197 if (!dh
->meth
->bn_mod_exp(dh
, pub_key
, dh
->params
.g
, prk
, dh
->params
.p
,
206 static int generate_key(DH
*dh
)
209 int generate_new_key
= 0;
214 BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
216 if (BN_num_bits(dh
->params
.p
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
217 DHerr(0, DH_R_MODULUS_TOO_LARGE
);
221 if (BN_num_bits(dh
->params
.p
) < DH_MIN_MODULUS_BITS
) {
222 DHerr(0, DH_R_MODULUS_TOO_SMALL
);
226 ctx
= BN_CTX_new_ex(dh
->libctx
);
230 if (dh
->priv_key
== NULL
) {
231 priv_key
= BN_secure_new();
232 if (priv_key
== NULL
)
234 generate_new_key
= 1;
236 priv_key
= dh
->priv_key
;
239 if (dh
->pub_key
== NULL
) {
244 pub_key
= dh
->pub_key
;
246 if (generate_new_key
) {
247 /* Is it an approved safe prime ?*/
248 if (DH_get_nid(dh
) != NID_undef
) {
250 * The safe prime group code sets N = 2*s
251 * (where s = max security strength supported).
252 * N = dh->length (N = maximum bit length of private key)
255 || dh
->params
.q
== NULL
256 || dh
->length
> BN_num_bits(dh
->params
.q
))
258 if (!ffc_generate_private_key(ctx
, &dh
->params
, dh
->length
,
259 dh
->length
/ 2, priv_key
))
263 if (dh
->params
.q
== NULL
)
266 if (dh
->params
.q
== NULL
) {
267 /* secret exponent length */
268 l
= dh
->length
? dh
->length
: BN_num_bits(dh
->params
.p
) - 1;
269 if (!BN_priv_rand_ex(priv_key
, l
, BN_RAND_TOP_ONE
,
270 BN_RAND_BOTTOM_ANY
, ctx
))
273 * We handle just one known case where g is a quadratic non-residue:
274 * for g = 2: p % 8 == 3
276 if (BN_is_word(dh
->params
.g
, DH_GENERATOR_2
)
277 && !BN_is_bit_set(dh
->params
.p
, 2)) {
278 /* clear bit 0, since it won't be a secret anyway */
279 if (!BN_clear_bit(priv_key
, 0))
286 * For FFC FIPS 186-4 keygen
287 * security strength s = 112,
288 * Max Private key size N = len(q)
290 if (!ffc_generate_private_key(ctx
, &dh
->params
,
291 BN_num_bits(dh
->params
.q
), 112,
298 if (!dh_generate_public_key(ctx
, dh
, priv_key
, pub_key
))
301 dh
->pub_key
= pub_key
;
302 dh
->priv_key
= priv_key
;
307 DHerr(0, ERR_R_BN_LIB
);
309 if (pub_key
!= dh
->pub_key
)
311 if (priv_key
!= dh
->priv_key
)
317 int dh_buf2key(DH
*dh
, const unsigned char *buf
, size_t len
)
319 int err_reason
= DH_R_BN_ERROR
;
320 BIGNUM
*pubkey
= NULL
;
324 if ((pubkey
= BN_bin2bn(buf
, len
, NULL
)) == NULL
)
326 DH_get0_pqg(dh
, &p
, NULL
, NULL
);
327 if (p
== NULL
|| (p_size
= BN_num_bytes(p
)) == 0) {
328 err_reason
= DH_R_NO_PARAMETERS_SET
;
332 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
333 * public key is of size not equal to size of p
335 if (BN_is_zero(pubkey
) || p_size
!= len
) {
336 err_reason
= DH_R_INVALID_PUBKEY
;
339 if (DH_set0_key(dh
, pubkey
, NULL
) != 1)
343 DHerr(DH_F_DH_BUF2KEY
, err_reason
);
348 size_t dh_key2buf(const DH
*dh
, unsigned char **pbuf_out
)
350 const BIGNUM
*pubkey
;
355 DH_get0_pqg(dh
, &p
, NULL
, NULL
);
356 DH_get0_key(dh
, &pubkey
, NULL
);
357 if (p
== NULL
|| pubkey
== NULL
358 || (p_size
= BN_num_bytes(p
)) == 0
359 || BN_num_bytes(pubkey
) == 0) {
360 DHerr(DH_F_DH_KEY2BUF
, DH_R_INVALID_PUBKEY
);
363 if ((pbuf
= OPENSSL_malloc(p_size
)) == NULL
) {
364 DHerr(DH_F_DH_KEY2BUF
, ERR_R_MALLOC_FAILURE
);
368 * As per Section 4.2.8.1 of RFC 8446 left pad public
369 * key with zeros to the size of p
371 if (BN_bn2binpad(pubkey
, pbuf
, p_size
) < 0) {
373 DHerr(DH_F_DH_KEY2BUF
, DH_R_BN_ERROR
);