2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 "internal/bn_int.h"
15 static int generate_key(DH
*dh
);
16 static int compute_key(unsigned char *key
, const BIGNUM
*pub_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_generate_key(DH
*dh
)
25 return dh
->meth
->generate_key(dh
);
28 int DH_compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
30 return dh
->meth
->compute_key(key
, pub_key
, dh
);
33 int DH_compute_key_padded(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
36 rv
= dh
->meth
->compute_key(key
, pub_key
, dh
);
39 pad
= BN_num_bytes(dh
->p
) - rv
;
41 memmove(key
+ pad
, key
, rv
);
47 static DH_METHOD dh_ossl
= {
59 static const DH_METHOD
*default_DH_method
= &dh_ossl
;
61 const DH_METHOD
*DH_OpenSSL(void)
66 void DH_set_default_method(const DH_METHOD
*meth
)
68 default_DH_method
= meth
;
71 const DH_METHOD
*DH_get_default_method(void)
73 return default_DH_method
;
76 static int generate_key(DH
*dh
)
79 int generate_new_key
= 0;
82 BN_MONT_CTX
*mont
= NULL
;
83 BIGNUM
*pub_key
= NULL
, *priv_key
= NULL
;
85 if (BN_num_bits(dh
->p
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
86 DHerr(DH_F_GENERATE_KEY
, DH_R_MODULUS_TOO_LARGE
);
94 if (dh
->priv_key
== NULL
) {
95 priv_key
= BN_secure_new();
100 priv_key
= dh
->priv_key
;
102 if (dh
->pub_key
== NULL
) {
107 pub_key
= dh
->pub_key
;
109 if (dh
->flags
& DH_FLAG_CACHE_MONT_P
) {
110 mont
= BN_MONT_CTX_set_locked(&dh
->method_mont_p
,
111 dh
->lock
, dh
->p
, ctx
);
116 if (generate_new_key
) {
119 if (!BN_priv_rand_range(priv_key
, dh
->q
))
122 while (BN_is_zero(priv_key
) || BN_is_one(priv_key
));
124 /* secret exponent length */
125 l
= dh
->length
? dh
->length
: BN_num_bits(dh
->p
) - 1;
126 if (!BN_priv_rand(priv_key
, l
, BN_RAND_TOP_ONE
, BN_RAND_BOTTOM_ANY
))
132 BIGNUM
*prk
= BN_new();
136 BN_with_flags(prk
, priv_key
, BN_FLG_CONSTTIME
);
138 if (!dh
->meth
->bn_mod_exp(dh
, pub_key
, dh
->g
, prk
, dh
->p
, ctx
, mont
)) {
142 /* We MUST free prk before any further use of priv_key */
146 dh
->pub_key
= pub_key
;
147 dh
->priv_key
= priv_key
;
151 DHerr(DH_F_GENERATE_KEY
, ERR_R_BN_LIB
);
153 if (pub_key
!= dh
->pub_key
)
155 if (priv_key
!= dh
->priv_key
)
161 static int compute_key(unsigned char *key
, const BIGNUM
*pub_key
, DH
*dh
)
164 BN_MONT_CTX
*mont
= NULL
;
169 if (BN_num_bits(dh
->p
) > OPENSSL_DH_MAX_MODULUS_BITS
) {
170 DHerr(DH_F_COMPUTE_KEY
, DH_R_MODULUS_TOO_LARGE
);
178 tmp
= BN_CTX_get(ctx
);
182 if (dh
->priv_key
== NULL
) {
183 DHerr(DH_F_COMPUTE_KEY
, DH_R_NO_PRIVATE_VALUE
);
187 if (dh
->flags
& DH_FLAG_CACHE_MONT_P
) {
188 mont
= BN_MONT_CTX_set_locked(&dh
->method_mont_p
,
189 dh
->lock
, dh
->p
, ctx
);
190 BN_set_flags(dh
->priv_key
, BN_FLG_CONSTTIME
);
195 if (!DH_check_pub_key(dh
, pub_key
, &check_result
) || check_result
) {
196 DHerr(DH_F_COMPUTE_KEY
, DH_R_INVALID_PUBKEY
);
201 meth
->bn_mod_exp(dh
, tmp
, pub_key
, dh
->priv_key
, dh
->p
, ctx
, mont
)) {
202 DHerr(DH_F_COMPUTE_KEY
, ERR_R_BN_LIB
);
206 ret
= BN_bn2bin(tmp
, key
);
215 static int dh_bn_mod_exp(const DH
*dh
, BIGNUM
*r
,
216 const BIGNUM
*a
, const BIGNUM
*p
,
217 const BIGNUM
*m
, BN_CTX
*ctx
, BN_MONT_CTX
*m_ctx
)
219 return BN_mod_exp_mont(r
, a
, p
, m
, ctx
, m_ctx
);
222 static int dh_init(DH
*dh
)
224 dh
->flags
|= DH_FLAG_CACHE_MONT_P
;
228 static int dh_finish(DH
*dh
)
230 BN_MONT_CTX_free(dh
->method_mont_p
);