2 * Copyright 1995-2023 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
);
37 * See SP800-56Ar3 Section 5.7.1.1
38 * Finite Field Cryptography Diffie-Hellman (FFC DH) Primitive
40 int ossl_dh_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
43 BN_MONT_CTX
*mont
= NULL
;
44 BIGNUM
*z
= NULL
, *pminus1
;
47 if (BN_num_bits(dh
->params
.p
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
48 ERR_raise(ERR_LIB_DH
, DH_R_MODULUS_TOO_LARGE
);
52 if (dh
->params
.q
!= NULL
53 && BN_num_bits(dh
->params
.q
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
54 ERR_raise(ERR_LIB_DH
, DH_R_Q_TOO_LARGE
);
58 if (BN_num_bits(dh
->params
.p
) < DH_MIN_MODULUS_BITS
) {
59 ERR_raise(ERR_LIB_DH
, DH_R_MODULUS_TOO_SMALL
);
63 ctx
= BN_CTX_new_ex(dh
->libctx
);
67 pminus1
= BN_CTX_get(ctx
);
72 if (dh
->priv_key
== NULL
) {
73 ERR_raise(ERR_LIB_DH
, DH_R_NO_PRIVATE_VALUE
);
77 if (dh
->flags
& DH_FLAG_CACHE_MONT_P
) {
78 mont
= BN_MONT_CTX_set_locked(&dh
->method_mont_p
,
79 dh
->lock
, dh
->params
.p
, ctx
);
80 BN_set_flags(dh
->priv_key
, BN_FLG_CONSTTIME
);
85 /* (Step 1) Z = pub_key^priv_key mod p */
86 if (!dh
->meth
->bn_mod_exp(dh
, z
, pub_key
, dh
->priv_key
, dh
->params
.p
, ctx
,
88 ERR_raise(ERR_LIB_DH
, ERR_R_BN_LIB
);
92 /* (Step 2) Error if z <= 1 or z = p - 1 */
93 if (BN_copy(pminus1
, dh
->params
.p
) == NULL
94 || !BN_sub_word(pminus1
, 1)
95 || BN_cmp(z
, BN_value_one()) <= 0
96 || BN_cmp(z
, pminus1
) == 0) {
97 ERR_raise(ERR_LIB_DH
, DH_R_INVALID_SECRET
);
101 /* return the padded key, i.e. same number of bytes as the modulus */
102 ret
= BN_bn2binpad(z
, key
, BN_num_bytes(dh
->params
.p
));
104 BN_clear(z
); /* (Step 2) destroy intermediate values */
111 * NB: This function is inherently not constant time due to the
112 * RFC 5246 (8.1.2) padding style that strips leading zero bytes.
114 int DH_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
117 volatile size_t npad
= 0, mask
= 1;
119 /* compute the key; ret is constant unless compute_key is external */
121 ret
= ossl_dh_compute_key(key
, pub_key
, dh
);
123 ret
= dh
->meth
->compute_key(key
, pub_key
, dh
);
128 /* count leading zero bytes, yet still touch all bytes */
129 for (i
= 0; i
< ret
; i
++) {
136 /* key-dependent memory access, potentially leaking npad / ret */
137 memmove(key
, key
+ npad
, ret
);
138 /* key-dependent memory access, potentially leaking npad / ret */
139 memset(key
+ ret
, 0, npad
);
144 int DH_compute_key_padded(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
148 /* rv is constant unless compute_key is external */
150 rv
= ossl_dh_compute_key(key
, pub_key
, dh
);
152 rv
= dh
->meth
->compute_key(key
, pub_key
, dh
);
156 pad
= BN_num_bytes(dh
->params
.p
) - rv
;
157 /* pad is constant (zero) unless compute_key is external */
159 memmove(key
+ pad
, key
, rv
);
165 static DH_METHOD dh_ossl
= {
177 static const DH_METHOD
*default_DH_method
= &dh_ossl
;
179 const DH_METHOD
*DH_OpenSSL(void)
184 const DH_METHOD
*DH_get_default_method(void)
186 return default_DH_method
;
189 static int dh_bn_mod_exp(const DH
*dh
, BIGNUM
*r
,
190 const BIGNUM
*a
, const BIGNUM
*p
,
191 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
194 return s390x_mod_exp(r
, a
, p
, m
, ctx
, m_ctx
);
196 return BN_mod_exp_mont(r
, a
, p
, m
, ctx
, m_ctx
);
200 static int dh_init(DH
*dh
)
202 dh
->flags
|= DH_FLAG_CACHE_MONT_P
;
207 static int dh_finish(DH
*dh
)
209 BN_MONT_CTX_free(dh
->method_mont_p
);
214 void DH_set_default_method(const DH_METHOD
*meth
)
216 default_DH_method
= meth
;
218 #endif /* FIPS_MODULE */
220 int DH_generate_key(DH
*dh
)
223 return generate_key(dh
);
225 return dh
->meth
->generate_key(dh
);
229 int ossl_dh_generate_public_key(BN_CTX
*ctx
, const DH
*dh
,
230 const BIGNUM
*priv_key
, BIGNUM
*pub_key
)
233 BIGNUM
*prk
= BN_new();
234 BN_MONT_CTX
*mont
= NULL
;
239 if (dh
->flags
& DH_FLAG_CACHE_MONT_P
) {
241 * We take the input DH as const, but we lie, because in some cases we
242 * want to get a hold of its Montgomery context.
244 * We cast to remove the const qualifier in this case, it should be
247 BN_MONT_CTX
**pmont
= (BN_MONT_CTX
**)&dh
->method_mont_p
;
249 mont
= BN_MONT_CTX_set_locked(pmont
, dh
->lock
, dh
->params
.p
, ctx
);
253 BN_with_flags(prk
, priv_key
, BN_FLG_CONSTTIME
);
255 /* pub_key = g^priv_key mod p */
256 if (!dh
->meth
->bn_mod_exp(dh
, pub_key
, dh
->params
.g
, prk
, dh
->params
.p
,
265 static int generate_key(DH
*dh
)
268 int generate_new_key
= 0;
273 BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
275 if (BN_num_bits(dh
->params
.p
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
276 ERR_raise(ERR_LIB_DH
, DH_R_MODULUS_TOO_LARGE
);
280 if (dh
->params
.q
!= NULL
281 && BN_num_bits(dh
->params
.q
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
282 ERR_raise(ERR_LIB_DH
, DH_R_Q_TOO_LARGE
);
286 if (BN_num_bits(dh
->params
.p
) < DH_MIN_MODULUS_BITS
) {
287 ERR_raise(ERR_LIB_DH
, DH_R_MODULUS_TOO_SMALL
);
291 ctx
= BN_CTX_new_ex(dh
->libctx
);
295 if (dh
->priv_key
== NULL
) {
296 priv_key
= BN_secure_new();
297 if (priv_key
== NULL
)
299 generate_new_key
= 1;
301 priv_key
= dh
->priv_key
;
304 if (dh
->pub_key
== NULL
) {
309 pub_key
= dh
->pub_key
;
311 if (generate_new_key
) {
312 /* Is it an approved safe prime ?*/
313 if (DH_get_nid(dh
) != NID_undef
) {
315 ossl_ifc_ffc_compute_security_bits(BN_num_bits(dh
->params
.p
));
317 if (dh
->params
.q
== NULL
318 || dh
->length
> BN_num_bits(dh
->params
.q
))
320 /* dh->length = maximum bit length of generated private key */
321 if (!ossl_ffc_generate_private_key(ctx
, &dh
->params
, dh
->length
,
322 max_strength
, priv_key
))
326 if (dh
->params
.q
== NULL
)
329 if (dh
->params
.q
== NULL
) {
330 /* secret exponent length, must satisfy 2^(l-1) <= p */
332 && dh
->length
>= BN_num_bits(dh
->params
.p
))
334 l
= dh
->length
? dh
->length
: BN_num_bits(dh
->params
.p
) - 1;
335 if (!BN_priv_rand_ex(priv_key
, l
, BN_RAND_TOP_ONE
,
336 BN_RAND_BOTTOM_ANY
, 0, ctx
))
339 * We handle just one known case where g is a quadratic non-residue:
340 * for g = 2: p % 8 == 3
342 if (BN_is_word(dh
->params
.g
, DH_GENERATOR_2
)
343 && !BN_is_bit_set(dh
->params
.p
, 2)) {
344 /* clear bit 0, since it won't be a secret anyway */
345 if (!BN_clear_bit(priv_key
, 0))
351 /* Do a partial check for invalid p, q, g */
352 if (!ossl_ffc_params_simple_validate(dh
->libctx
, &dh
->params
,
353 FFC_PARAM_TYPE_DH
, NULL
))
356 * For FFC FIPS 186-4 keygen
357 * security strength s = 112,
358 * Max Private key size N = len(q)
360 if (!ossl_ffc_generate_private_key(ctx
, &dh
->params
,
361 BN_num_bits(dh
->params
.q
),
369 if (!ossl_dh_generate_public_key(ctx
, dh
, priv_key
, pub_key
))
372 dh
->pub_key
= pub_key
;
373 dh
->priv_key
= priv_key
;
378 ERR_raise(ERR_LIB_DH
, ERR_R_BN_LIB
);
380 if (pub_key
!= dh
->pub_key
)
382 if (priv_key
!= dh
->priv_key
)
388 int ossl_dh_buf2key(DH
*dh
, const unsigned char *buf
, size_t len
)
390 int err_reason
= DH_R_BN_ERROR
;
391 BIGNUM
*pubkey
= NULL
;
395 if ((pubkey
= BN_bin2bn(buf
, len
, NULL
)) == NULL
)
397 DH_get0_pqg(dh
, &p
, NULL
, NULL
);
398 if (p
== NULL
|| BN_num_bytes(p
) == 0) {
399 err_reason
= DH_R_NO_PARAMETERS_SET
;
402 /* Prevent small subgroup attacks per RFC 8446 Section 4.2.8.1 */
403 if (!ossl_dh_check_pub_key_partial(dh
, pubkey
, &ret
)) {
404 err_reason
= DH_R_INVALID_PUBKEY
;
407 if (DH_set0_key(dh
, pubkey
, NULL
) != 1)
411 ERR_raise(ERR_LIB_DH
, err_reason
);
416 size_t ossl_dh_key2buf(const DH
*dh
, unsigned char **pbuf_out
, size_t size
,
419 const BIGNUM
*pubkey
;
420 unsigned char *pbuf
= NULL
;
424 DH_get0_pqg(dh
, &p
, NULL
, NULL
);
425 DH_get0_key(dh
, &pubkey
, NULL
);
426 if (p
== NULL
|| pubkey
== NULL
427 || (p_size
= BN_num_bytes(p
)) == 0
428 || BN_num_bytes(pubkey
) == 0) {
429 ERR_raise(ERR_LIB_DH
, DH_R_INVALID_PUBKEY
);
432 if (pbuf_out
!= NULL
&& (alloc
|| *pbuf_out
!= NULL
)) {
434 if (size
>= (size_t)p_size
)
437 ERR_raise(ERR_LIB_DH
, DH_R_INVALID_SIZE
);
439 pbuf
= OPENSSL_malloc(p_size
);
442 /* Errors raised above */
446 * As per Section 4.2.8.1 of RFC 8446 left pad public
447 * key with zeros to the size of p
449 if (BN_bn2binpad(pubkey
, pbuf
, p_size
) < 0) {
452 ERR_raise(ERR_LIB_DH
, DH_R_BN_ERROR
);