]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_key.c
Remove some utilities from the core to provider interface
[thirdparty/openssl.git] / crypto / dh / dh_key.c
CommitLineData
aa6bb135 1/*
a38c878c 2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
e38873f5 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
0aeddcfa 12#include "dh_locl.h"
829ccf6a 13#include "internal/bn_int.h"
d02b48c6 14
13066cee 15static int generate_key(DH *dh);
f971ccb2
RL
16static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
17static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
0f113f3e
MC
18 const BIGNUM *a, const BIGNUM *p,
19 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
13066cee
DSH
20static int dh_init(DH *dh);
21static int dh_finish(DH *dh);
22
6b691a5c 23int DH_generate_key(DH *dh)
0f113f3e
MC
24{
25 return dh->meth->generate_key(dh);
26}
13066cee 27
f971ccb2 28int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
0f113f3e
MC
29{
30 return dh->meth->compute_key(key, pub_key, dh);
31}
13066cee 32
bc91494e 33int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
0f113f3e
MC
34{
35 int rv, pad;
36 rv = dh->meth->compute_key(key, pub_key, dh);
37 if (rv <= 0)
38 return rv;
39 pad = BN_num_bytes(dh->p) - rv;
40 if (pad > 0) {
41 memmove(key + pad, key, rv);
42 memset(key, 0, pad);
43 }
44 return rv + pad;
45}
bc91494e 46
13066cee 47static DH_METHOD dh_ossl = {
0f113f3e
MC
48 "OpenSSL DH Method",
49 generate_key,
50 compute_key,
51 dh_bn_mod_exp,
52 dh_init,
53 dh_finish,
54 DH_FLAG_FIPS_METHOD,
55 NULL,
56 NULL
13066cee
DSH
57};
58
076fc555
RS
59static const DH_METHOD *default_DH_method = &dh_ossl;
60
f971ccb2 61const DH_METHOD *DH_OpenSSL(void)
13066cee 62{
0f113f3e 63 return &dh_ossl;
13066cee
DSH
64}
65
076fc555
RS
66void DH_set_default_method(const DH_METHOD *meth)
67{
68 default_DH_method = meth;
69}
70
71const DH_METHOD *DH_get_default_method(void)
72{
73 return default_DH_method;
74}
75
13066cee 76static int generate_key(DH *dh)
0f113f3e
MC
77{
78 int ok = 0;
79 int generate_new_key = 0;
80 unsigned l;
91f7361f 81 BN_CTX *ctx = NULL;
0f113f3e
MC
82 BN_MONT_CTX *mont = NULL;
83 BIGNUM *pub_key = NULL, *priv_key = NULL;
d02b48c6 84
91f7361f
GV
85 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
86 DHerr(DH_F_GENERATE_KEY, DH_R_MODULUS_TOO_LARGE);
87 return 0;
88 }
89
0f113f3e
MC
90 ctx = BN_CTX_new();
91 if (ctx == NULL)
92 goto err;
d02b48c6 93
0f113f3e 94 if (dh->priv_key == NULL) {
74924dcb 95 priv_key = BN_secure_new();
0f113f3e
MC
96 if (priv_key == NULL)
97 goto err;
98 generate_new_key = 1;
99 } else
100 priv_key = dh->priv_key;
d02b48c6 101
0f113f3e
MC
102 if (dh->pub_key == NULL) {
103 pub_key = BN_new();
104 if (pub_key == NULL)
105 goto err;
106 } else
107 pub_key = dh->pub_key;
d02b48c6 108
0f113f3e
MC
109 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
110 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
d188a536 111 dh->lock, dh->p, ctx);
0f113f3e
MC
112 if (!mont)
113 goto err;
114 }
6ec8e63a 115
0f113f3e
MC
116 if (generate_new_key) {
117 if (dh->q) {
118 do {
ddc6a5c8 119 if (!BN_priv_rand_range(priv_key, dh->q))
0f113f3e
MC
120 goto err;
121 }
122 while (BN_is_zero(priv_key) || BN_is_one(priv_key));
123 } else {
124 /* secret exponent length */
125 l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;
ddc6a5c8 126 if (!BN_priv_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
0f113f3e 127 goto err;
a38c878c
BE
128 /*
129 * We handle just one known case where g is a quadratic non-residue:
130 * for g = 2: p % 8 == 3
131 */
132 if (BN_is_word(dh->g, DH_GENERATOR_2) && !BN_is_bit_set(dh->p, 2)) {
133 /* clear bit 0, since it won't be a secret anyway */
134 if (!BN_clear_bit(priv_key, 0))
135 goto err;
136 }
0f113f3e
MC
137 }
138 }
dfeab068 139
0f113f3e 140 {
5584f65a 141 BIGNUM *prk = BN_new();
46a64376 142
5584f65a
MC
143 if (prk == NULL)
144 goto err;
145 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
46a64376 146
0f113f3e 147 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {
a38c878c 148 BN_clear_free(prk);
0f113f3e
MC
149 goto err;
150 }
5584f65a 151 /* We MUST free prk before any further use of priv_key */
a38c878c 152 BN_clear_free(prk);
0f113f3e 153 }
46a64376 154
0f113f3e
MC
155 dh->pub_key = pub_key;
156 dh->priv_key = priv_key;
157 ok = 1;
158 err:
159 if (ok != 1)
160 DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);
d02b48c6 161
23a1d5e9 162 if (pub_key != dh->pub_key)
0f113f3e 163 BN_free(pub_key);
23a1d5e9 164 if (priv_key != dh->priv_key)
0f113f3e
MC
165 BN_free(priv_key);
166 BN_CTX_free(ctx);
26a7d938 167 return ok;
0f113f3e 168}
d02b48c6 169
f971ccb2 170static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
0f113f3e
MC
171{
172 BN_CTX *ctx = NULL;
173 BN_MONT_CTX *mont = NULL;
174 BIGNUM *tmp;
175 int ret = -1;
176 int check_result;
d02b48c6 177
0f113f3e
MC
178 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
179 DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);
180 goto err;
181 }
5e3225cc 182
0f113f3e
MC
183 ctx = BN_CTX_new();
184 if (ctx == NULL)
185 goto err;
186 BN_CTX_start(ctx);
187 tmp = BN_CTX_get(ctx);
7928ee4d
BE
188 if (tmp == NULL)
189 goto err;
6ec8e63a 190
0f113f3e
MC
191 if (dh->priv_key == NULL) {
192 DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);
193 goto err;
194 }
dfeab068 195
0f113f3e
MC
196 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
197 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
d188a536 198 dh->lock, dh->p, ctx);
5584f65a 199 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
0f113f3e
MC
200 if (!mont)
201 goto err;
202 }
bf3d6c0c 203
0f113f3e
MC
204 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {
205 DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);
206 goto err;
207 }
d02b48c6 208
0f113f3e
MC
209 if (!dh->
210 meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {
211 DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);
212 goto err;
213 }
13066cee 214
0f113f3e
MC
215 ret = BN_bn2bin(tmp, key);
216 err:
ce1415ed
SL
217 BN_CTX_end(ctx);
218 BN_CTX_free(ctx);
26a7d938 219 return ret;
0f113f3e 220}
6dad7bd6 221
0f113f3e
MC
222static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
223 const BIGNUM *a, const BIGNUM *p,
224 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
225{
5584f65a 226 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
0f113f3e 227}
13066cee
DSH
228
229static int dh_init(DH *dh)
0f113f3e
MC
230{
231 dh->flags |= DH_FLAG_CACHE_MONT_P;
208fb891 232 return 1;
0f113f3e 233}
13066cee
DSH
234
235static int dh_finish(DH *dh)
0f113f3e 236{
23a1d5e9 237 BN_MONT_CTX_free(dh->method_mont_p);
208fb891 238 return 1;
0f113f3e 239}
9aaecbfc 240
241int dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
242{
243 int err_reason = DH_R_BN_ERROR;
244 BIGNUM *pubkey = NULL;
245 const BIGNUM *p;
246 size_t p_size;
247
248 if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
249 goto err;
250 DH_get0_pqg(dh, &p, NULL, NULL);
251 if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
252 err_reason = DH_R_NO_PARAMETERS_SET;
253 goto err;
254 }
255 /*
256 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
257 * public key is of size not equal to size of p
258 */
259 if (BN_is_zero(pubkey) || p_size != len) {
260 err_reason = DH_R_INVALID_PUBKEY;
261 goto err;
262 }
263 if (DH_set0_key(dh, pubkey, NULL) != 1)
264 goto err;
265 return 1;
266err:
267 DHerr(DH_F_DH_BUF2KEY, err_reason);
268 BN_free(pubkey);
269 return 0;
270}
271
272size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out)
273{
274 const BIGNUM *pubkey;
275 unsigned char *pbuf;
276 const BIGNUM *p;
277 int p_size;
278
279 DH_get0_pqg(dh, &p, NULL, NULL);
280 DH_get0_key(dh, &pubkey, NULL);
281 if (p == NULL || pubkey == NULL
282 || (p_size = BN_num_bytes(p)) == 0
283 || BN_num_bytes(pubkey) == 0) {
284 DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY);
285 return 0;
286 }
287 if ((pbuf = OPENSSL_malloc(p_size)) == NULL) {
288 DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE);
289 return 0;
290 }
291 /*
292 * As per Section 4.2.8.1 of RFC 8446 left pad public
293 * key with zeros to the size of p
294 */
295 if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
296 OPENSSL_free(pbuf);
297 DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR);
298 return 0;
299 }
300 *pbuf_out = pbuf;
301 return p_size;
302}