2 * Copyright 1995-2021 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 ERR_raise(ERR_LIB_DH
, DH_R_MODULUS_TOO_LARGE
);
51 if (BN_num_bits(dh
->params
.p
) < DH_MIN_MODULUS_BITS
) {
52 ERR_raise(ERR_LIB_DH
, 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 ERR_raise(ERR_LIB_DH
, 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 ERR_raise(ERR_LIB_DH
, DH_R_INVALID_PUBKEY
);
83 if (!dh
->meth
->bn_mod_exp(dh
, tmp
, pub_key
, dh
->priv_key
, dh
->params
.p
, ctx
,
85 ERR_raise(ERR_LIB_DH
, ERR_R_BN_LIB
);
89 /* return the padded key, i.e. same number of bytes as the modulus */
90 ret
= BN_bn2binpad(tmp
, key
, BN_num_bytes(dh
->params
.p
));
98 * NB: This function is inherently not constant time due to the
99 * RFC 5246 (8.1.2) padding style that strips leading zero bytes.
101 int DH_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
104 volatile size_t npad
= 0, mask
= 1;
106 /* compute the key; ret is constant unless compute_key is external */
108 ret
= compute_key(key
, pub_key
, dh
);
110 ret
= dh
->meth
->compute_key(key
, pub_key
, dh
);
115 /* count leading zero bytes, yet still touch all bytes */
116 for (i
= 0; i
< ret
; i
++) {
123 /* key-dependent memory access, potentially leaking npad / ret */
124 memmove(key
, key
+ npad
, ret
);
125 /* key-dependent memory access, potentially leaking npad / ret */
126 memset(key
+ ret
, 0, npad
);
131 int DH_compute_key_padded(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
135 /* rv is constant unless compute_key is external */
137 rv
= compute_key(key
, pub_key
, dh
);
139 rv
= dh
->meth
->compute_key(key
, pub_key
, dh
);
143 pad
= BN_num_bytes(dh
->params
.p
) - rv
;
144 /* pad is constant (zero) unless compute_key is external */
146 memmove(key
+ pad
, key
, rv
);
152 static DH_METHOD dh_ossl
= {
164 static const DH_METHOD
*default_DH_method
= &dh_ossl
;
166 const DH_METHOD
*DH_OpenSSL(void)
171 const DH_METHOD
*DH_get_default_method(void)
173 return default_DH_method
;
176 static int dh_bn_mod_exp(const DH
*dh
, BIGNUM
*r
,
177 const BIGNUM
*a
, const BIGNUM
*p
,
178 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
180 return BN_mod_exp_mont(r
, a
, p
, m
, ctx
, m_ctx
);
183 static int dh_init(DH
*dh
)
185 dh
->flags
|= DH_FLAG_CACHE_MONT_P
;
186 ossl_ffc_params_init(&dh
->params
);
191 static int dh_finish(DH
*dh
)
193 BN_MONT_CTX_free(dh
->method_mont_p
);
198 void DH_set_default_method(const DH_METHOD
*meth
)
200 default_DH_method
= meth
;
202 #endif /* FIPS_MODULE */
204 int DH_generate_key(DH
*dh
)
207 return generate_key(dh
);
209 return dh
->meth
->generate_key(dh
);
213 int dh_generate_public_key(BN_CTX
*ctx
, const DH
*dh
, const BIGNUM
*priv_key
,
217 BIGNUM
*prk
= BN_new();
218 BN_MONT_CTX
*mont
= NULL
;
223 if (dh
->flags
& DH_FLAG_CACHE_MONT_P
) {
225 * We take the input DH as const, but we lie, because in some cases we
226 * want to get a hold of its Montgomery context.
228 * We cast to remove the const qualifier in this case, it should be
231 BN_MONT_CTX
**pmont
= (BN_MONT_CTX
**)&dh
->method_mont_p
;
233 mont
= BN_MONT_CTX_set_locked(pmont
, dh
->lock
, dh
->params
.p
, ctx
);
237 BN_with_flags(prk
, priv_key
, BN_FLG_CONSTTIME
);
239 /* pub_key = g^priv_key mod p */
240 if (!dh
->meth
->bn_mod_exp(dh
, pub_key
, dh
->params
.g
, prk
, dh
->params
.p
,
249 static int generate_key(DH
*dh
)
252 int generate_new_key
= 0;
257 BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
259 if (BN_num_bits(dh
->params
.p
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
260 ERR_raise(ERR_LIB_DH
, DH_R_MODULUS_TOO_LARGE
);
264 if (BN_num_bits(dh
->params
.p
) < DH_MIN_MODULUS_BITS
) {
265 ERR_raise(ERR_LIB_DH
, DH_R_MODULUS_TOO_SMALL
);
269 ctx
= BN_CTX_new_ex(dh
->libctx
);
273 if (dh
->priv_key
== NULL
) {
274 priv_key
= BN_secure_new();
275 if (priv_key
== NULL
)
277 generate_new_key
= 1;
279 priv_key
= dh
->priv_key
;
282 if (dh
->pub_key
== NULL
) {
287 pub_key
= dh
->pub_key
;
289 if (generate_new_key
) {
290 /* Is it an approved safe prime ?*/
291 if (DH_get_nid(dh
) != NID_undef
) {
293 ifc_ffc_compute_security_bits(BN_num_bits(dh
->params
.p
));
295 if (dh
->params
.q
== NULL
296 || dh
->length
> BN_num_bits(dh
->params
.q
))
298 /* dh->length = maximum bit length of generated private key */
299 if (!ossl_ffc_generate_private_key(ctx
, &dh
->params
, dh
->length
,
300 max_strength
, priv_key
))
304 if (dh
->params
.q
== NULL
)
307 if (dh
->params
.q
== NULL
) {
308 /* secret exponent length, must satisfy 2^(l-1) <= p */
310 && dh
->length
>= BN_num_bits(dh
->params
.p
))
312 l
= dh
->length
? dh
->length
: BN_num_bits(dh
->params
.p
) - 1;
313 if (!BN_priv_rand_ex(priv_key
, l
, BN_RAND_TOP_ONE
,
314 BN_RAND_BOTTOM_ANY
, ctx
))
317 * We handle just one known case where g is a quadratic non-residue:
318 * for g = 2: p % 8 == 3
320 if (BN_is_word(dh
->params
.g
, DH_GENERATOR_2
)
321 && !BN_is_bit_set(dh
->params
.p
, 2)) {
322 /* clear bit 0, since it won't be a secret anyway */
323 if (!BN_clear_bit(priv_key
, 0))
329 /* Do a partial check for invalid p, q, g */
330 if (!ossl_ffc_params_simple_validate(dh
->libctx
, &dh
->params
,
331 FFC_PARAM_TYPE_DH
, NULL
))
334 * For FFC FIPS 186-4 keygen
335 * security strength s = 112,
336 * Max Private key size N = len(q)
338 if (!ossl_ffc_generate_private_key(ctx
, &dh
->params
,
339 BN_num_bits(dh
->params
.q
),
347 if (!dh_generate_public_key(ctx
, dh
, priv_key
, pub_key
))
350 dh
->pub_key
= pub_key
;
351 dh
->priv_key
= priv_key
;
356 ERR_raise(ERR_LIB_DH
, ERR_R_BN_LIB
);
358 if (pub_key
!= dh
->pub_key
)
360 if (priv_key
!= dh
->priv_key
)
366 int dh_buf2key(DH
*dh
, const unsigned char *buf
, size_t len
)
368 int err_reason
= DH_R_BN_ERROR
;
369 BIGNUM
*pubkey
= NULL
;
373 if ((pubkey
= BN_bin2bn(buf
, len
, NULL
)) == NULL
)
375 DH_get0_pqg(dh
, &p
, NULL
, NULL
);
376 if (p
== NULL
|| (p_size
= BN_num_bytes(p
)) == 0) {
377 err_reason
= DH_R_NO_PARAMETERS_SET
;
381 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
382 * public key is of size not equal to size of p
384 if (BN_is_zero(pubkey
) || p_size
!= len
) {
385 err_reason
= DH_R_INVALID_PUBKEY
;
388 if (DH_set0_key(dh
, pubkey
, NULL
) != 1)
392 ERR_raise(ERR_LIB_DH
, err_reason
);
397 size_t dh_key2buf(const DH
*dh
, unsigned char **pbuf_out
, size_t size
, int alloc
)
399 const BIGNUM
*pubkey
;
400 unsigned char *pbuf
= NULL
;
404 DH_get0_pqg(dh
, &p
, NULL
, NULL
);
405 DH_get0_key(dh
, &pubkey
, NULL
);
406 if (p
== NULL
|| pubkey
== NULL
407 || (p_size
= BN_num_bytes(p
)) == 0
408 || BN_num_bytes(pubkey
) == 0) {
409 ERR_raise(ERR_LIB_DH
, DH_R_INVALID_PUBKEY
);
412 if (pbuf_out
!= NULL
&& (alloc
|| *pbuf_out
!= NULL
)) {
414 if (size
>= (size_t)p_size
)
417 pbuf
= OPENSSL_malloc(p_size
);
421 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
425 * As per Section 4.2.8.1 of RFC 8446 left pad public
426 * key with zeros to the size of p
428 if (BN_bn2binpad(pubkey
, pbuf
, p_size
) < 0) {
431 ERR_raise(ERR_LIB_DH
, DH_R_BN_ERROR
);