]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
f11f86f6 | 2 | * Copyright 1995-2020 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" |
706457b7 | 12 | #include "dh_local.h" |
25f2138b | 13 | #include "crypto/bn.h" |
62f49b90 | 14 | #include "crypto/dh.h" |
d02b48c6 | 15 | |
13066cee | 16 | static int generate_key(DH *dh); |
f971ccb2 | 17 | static 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 |
20 | static int dh_init(DH *dh); |
21 | static int dh_finish(DH *dh); | |
22 | ||
62f49b90 SL |
23 | int dh_compute_key(OPENSSL_CTX *libctx, unsigned char *key, |
24 | const BIGNUM *pub_key, DH *dh) | |
0f113f3e | 25 | { |
62f49b90 SL |
26 | BN_CTX *ctx = NULL; |
27 | BN_MONT_CTX *mont = NULL; | |
28 | BIGNUM *tmp; | |
29 | int ret = -1; | |
30 | #ifndef FIPS_MODE | |
31 | int check_result; | |
32 | #endif | |
33 | ||
dc8de3e6 | 34 | if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) { |
62f49b90 SL |
35 | DHerr(0, DH_R_MODULUS_TOO_LARGE); |
36 | goto err; | |
37 | } | |
38 | ||
dc8de3e6 | 39 | if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) { |
62f49b90 SL |
40 | DHerr(0, DH_R_MODULUS_TOO_SMALL); |
41 | return 0; | |
42 | } | |
43 | ||
44 | ctx = BN_CTX_new_ex(libctx); | |
45 | if (ctx == NULL) | |
46 | goto err; | |
47 | BN_CTX_start(ctx); | |
48 | tmp = BN_CTX_get(ctx); | |
49 | if (tmp == NULL) | |
50 | goto err; | |
51 | ||
52 | if (dh->priv_key == NULL) { | |
53 | DHerr(0, DH_R_NO_PRIVATE_VALUE); | |
54 | goto err; | |
55 | } | |
56 | ||
57 | if (dh->flags & DH_FLAG_CACHE_MONT_P) { | |
58 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, | |
dc8de3e6 | 59 | dh->lock, dh->params.p, ctx); |
62f49b90 SL |
60 | BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME); |
61 | if (!mont) | |
62 | goto err; | |
63 | } | |
64 | /* TODO(3.0) : Solve in a PR related to Key validation for DH */ | |
65 | #ifndef FIPS_MODE | |
66 | if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) { | |
67 | DHerr(0, DH_R_INVALID_PUBKEY); | |
68 | goto err; | |
69 | } | |
70 | #endif | |
dc8de3e6 | 71 | if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->params.p, ctx, |
62f49b90 SL |
72 | mont)) { |
73 | DHerr(0, ERR_R_BN_LIB); | |
74 | goto err; | |
75 | } | |
76 | ||
77 | ret = BN_bn2bin(tmp, key); | |
78 | err: | |
79 | BN_CTX_end(ctx); | |
80 | BN_CTX_free(ctx); | |
81 | return ret; | |
0f113f3e | 82 | } |
13066cee | 83 | |
62f49b90 | 84 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) |
0f113f3e | 85 | { |
62f49b90 | 86 | return dh_compute_key(NULL, key, pub_key, dh); |
0f113f3e | 87 | } |
13066cee | 88 | |
62f49b90 SL |
89 | int dh_compute_key_padded(OPENSSL_CTX *libctx, unsigned char *key, |
90 | const BIGNUM *pub_key, DH *dh) | |
0f113f3e MC |
91 | { |
92 | int rv, pad; | |
62f49b90 SL |
93 | |
94 | #ifdef FIPS_MODE | |
95 | rv = dh_compute_key(libctx, key, pub_key, dh); | |
96 | #else | |
0f113f3e | 97 | rv = dh->meth->compute_key(key, pub_key, dh); |
62f49b90 | 98 | #endif |
0f113f3e MC |
99 | if (rv <= 0) |
100 | return rv; | |
dc8de3e6 | 101 | pad = BN_num_bytes(dh->params.p) - rv; |
0f113f3e MC |
102 | if (pad > 0) { |
103 | memmove(key + pad, key, rv); | |
104 | memset(key, 0, pad); | |
105 | } | |
106 | return rv + pad; | |
107 | } | |
bc91494e | 108 | |
62f49b90 SL |
109 | #ifndef FIPS_MODE |
110 | int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |
111 | { | |
112 | return dh->meth->compute_key(key, pub_key, dh); | |
113 | } | |
114 | ||
115 | int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |
116 | { | |
117 | return dh_compute_key_padded(NULL, key, pub_key, dh); | |
118 | } | |
119 | #endif | |
120 | ||
13066cee | 121 | static DH_METHOD dh_ossl = { |
0f113f3e MC |
122 | "OpenSSL DH Method", |
123 | generate_key, | |
124 | compute_key, | |
125 | dh_bn_mod_exp, | |
126 | dh_init, | |
127 | dh_finish, | |
128 | DH_FLAG_FIPS_METHOD, | |
129 | NULL, | |
130 | NULL | |
13066cee DSH |
131 | }; |
132 | ||
076fc555 RS |
133 | static const DH_METHOD *default_DH_method = &dh_ossl; |
134 | ||
f971ccb2 | 135 | const DH_METHOD *DH_OpenSSL(void) |
13066cee | 136 | { |
0f113f3e | 137 | return &dh_ossl; |
13066cee DSH |
138 | } |
139 | ||
62f49b90 SL |
140 | const DH_METHOD *DH_get_default_method(void) |
141 | { | |
142 | return default_DH_method; | |
143 | } | |
144 | ||
145 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, | |
146 | const BIGNUM *a, const BIGNUM *p, | |
147 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | |
148 | { | |
149 | return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); | |
150 | } | |
151 | ||
152 | static int dh_init(DH *dh) | |
153 | { | |
154 | dh->flags |= DH_FLAG_CACHE_MONT_P; | |
dc8de3e6 | 155 | ffc_params_init(&dh->params); |
f11f86f6 | 156 | dh->dirty_cnt++; |
62f49b90 SL |
157 | return 1; |
158 | } | |
159 | ||
160 | static int dh_finish(DH *dh) | |
161 | { | |
162 | BN_MONT_CTX_free(dh->method_mont_p); | |
163 | return 1; | |
164 | } | |
165 | ||
166 | #ifndef FIPS_MODE | |
076fc555 RS |
167 | void DH_set_default_method(const DH_METHOD *meth) |
168 | { | |
169 | default_DH_method = meth; | |
170 | } | |
171 | ||
62f49b90 | 172 | int DH_generate_key(DH *dh) |
076fc555 | 173 | { |
62f49b90 | 174 | return dh->meth->generate_key(dh); |
076fc555 | 175 | } |
f11f86f6 | 176 | #endif /* FIPS_MODE */ |
076fc555 | 177 | |
f11f86f6 | 178 | static int dh_generate_key(OPENSSL_CTX *libctx, DH *dh) |
0f113f3e MC |
179 | { |
180 | int ok = 0; | |
181 | int generate_new_key = 0; | |
f11f86f6 | 182 | #ifndef FIPS_MODE |
0f113f3e | 183 | unsigned l; |
f11f86f6 | 184 | #endif |
91f7361f | 185 | BN_CTX *ctx = NULL; |
0f113f3e MC |
186 | BN_MONT_CTX *mont = NULL; |
187 | BIGNUM *pub_key = NULL, *priv_key = NULL; | |
d02b48c6 | 188 | |
dc8de3e6 | 189 | if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) { |
f11f86f6 | 190 | DHerr(0, DH_R_MODULUS_TOO_LARGE); |
91f7361f GV |
191 | return 0; |
192 | } | |
193 | ||
dc8de3e6 | 194 | if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) { |
f11f86f6 | 195 | DHerr(0, DH_R_MODULUS_TOO_SMALL); |
6de1fe90 BE |
196 | return 0; |
197 | } | |
198 | ||
f11f86f6 | 199 | ctx = BN_CTX_new_ex(libctx); |
0f113f3e MC |
200 | if (ctx == NULL) |
201 | goto err; | |
d02b48c6 | 202 | |
0f113f3e | 203 | if (dh->priv_key == NULL) { |
74924dcb | 204 | priv_key = BN_secure_new(); |
0f113f3e MC |
205 | if (priv_key == NULL) |
206 | goto err; | |
207 | generate_new_key = 1; | |
208 | } else | |
209 | priv_key = dh->priv_key; | |
d02b48c6 | 210 | |
0f113f3e MC |
211 | if (dh->pub_key == NULL) { |
212 | pub_key = BN_new(); | |
213 | if (pub_key == NULL) | |
214 | goto err; | |
215 | } else | |
216 | pub_key = dh->pub_key; | |
d02b48c6 | 217 | |
0f113f3e MC |
218 | if (dh->flags & DH_FLAG_CACHE_MONT_P) { |
219 | mont = BN_MONT_CTX_set_locked(&dh->method_mont_p, | |
dc8de3e6 | 220 | dh->lock, dh->params.p, ctx); |
0f113f3e MC |
221 | if (!mont) |
222 | goto err; | |
223 | } | |
6ec8e63a | 224 | |
0f113f3e | 225 | if (generate_new_key) { |
f11f86f6 SL |
226 | /* Is it an approved safe prime ?*/ |
227 | if (DH_get_nid(dh) != NID_undef) { | |
a38c878c | 228 | /* |
f11f86f6 SL |
229 | * The safe prime group code sets N = 2*s |
230 | * (where s = max security strength supported). | |
231 | * N = dh->length (N = maximum bit length of private key) | |
a38c878c | 232 | */ |
f11f86f6 SL |
233 | if (dh->length == 0 |
234 | || dh->params.q == NULL | |
235 | || dh->length > BN_num_bits(dh->params.q)) | |
236 | goto err; | |
237 | if (!ffc_generate_private_key(ctx, &dh->params, dh->length, | |
238 | dh->length / 2, priv_key)) | |
239 | goto err; | |
240 | } else { | |
241 | #ifdef FIPS_MODE | |
242 | if (dh->params.q == NULL) | |
243 | goto err; | |
244 | #else | |
245 | if (dh->params.q == NULL) { | |
246 | /* secret exponent length */ | |
247 | l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1; | |
248 | if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE, | |
249 | BN_RAND_BOTTOM_ANY, ctx)) | |
250 | goto err; | |
251 | /* | |
252 | * We handle just one known case where g is a quadratic non-residue: | |
253 | * for g = 2: p % 8 == 3 | |
254 | */ | |
255 | if (BN_is_word(dh->params.g, DH_GENERATOR_2) | |
256 | && !BN_is_bit_set(dh->params.p, 2)) { | |
257 | /* clear bit 0, since it won't be a secret anyway */ | |
258 | if (!BN_clear_bit(priv_key, 0)) | |
259 | goto err; | |
260 | } | |
261 | } else | |
262 | #endif | |
263 | { | |
264 | /* | |
265 | * For FFC FIPS 186-4 keygen | |
266 | * security strength s = 112, | |
267 | * Max Private key size N = len(q) | |
268 | */ | |
269 | if (!ffc_generate_private_key(ctx, &dh->params, | |
270 | BN_num_bits(dh->params.q), 112, | |
271 | priv_key)) | |
a38c878c BE |
272 | goto err; |
273 | } | |
0f113f3e MC |
274 | } |
275 | } | |
dfeab068 | 276 | |
0f113f3e | 277 | { |
5584f65a | 278 | BIGNUM *prk = BN_new(); |
46a64376 | 279 | |
5584f65a MC |
280 | if (prk == NULL) |
281 | goto err; | |
282 | BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME); | |
46a64376 | 283 | |
f11f86f6 | 284 | /* pub_key = g^priv_key mod p */ |
dc8de3e6 SL |
285 | if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p, |
286 | ctx, mont)) { | |
a38c878c | 287 | BN_clear_free(prk); |
0f113f3e MC |
288 | goto err; |
289 | } | |
5584f65a | 290 | /* We MUST free prk before any further use of priv_key */ |
a38c878c | 291 | BN_clear_free(prk); |
0f113f3e | 292 | } |
46a64376 | 293 | |
0f113f3e MC |
294 | dh->pub_key = pub_key; |
295 | dh->priv_key = priv_key; | |
8b84b075 | 296 | dh->dirty_cnt++; |
0f113f3e MC |
297 | ok = 1; |
298 | err: | |
299 | if (ok != 1) | |
f11f86f6 | 300 | DHerr(0, ERR_R_BN_LIB); |
d02b48c6 | 301 | |
23a1d5e9 | 302 | if (pub_key != dh->pub_key) |
0f113f3e | 303 | BN_free(pub_key); |
23a1d5e9 | 304 | if (priv_key != dh->priv_key) |
0f113f3e MC |
305 | BN_free(priv_key); |
306 | BN_CTX_free(ctx); | |
26a7d938 | 307 | return ok; |
0f113f3e | 308 | } |
d02b48c6 | 309 | |
f11f86f6 SL |
310 | static int generate_key(DH *dh) |
311 | { | |
312 | return dh_generate_key(NULL, dh); | |
313 | } | |
dc8de3e6 | 314 | |
9aaecbfc | 315 | int dh_buf2key(DH *dh, const unsigned char *buf, size_t len) |
316 | { | |
317 | int err_reason = DH_R_BN_ERROR; | |
318 | BIGNUM *pubkey = NULL; | |
319 | const BIGNUM *p; | |
320 | size_t p_size; | |
321 | ||
322 | if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL) | |
323 | goto err; | |
324 | DH_get0_pqg(dh, &p, NULL, NULL); | |
325 | if (p == NULL || (p_size = BN_num_bytes(p)) == 0) { | |
326 | err_reason = DH_R_NO_PARAMETERS_SET; | |
327 | goto err; | |
328 | } | |
329 | /* | |
330 | * As per Section 4.2.8.1 of RFC 8446 fail if DHE's | |
331 | * public key is of size not equal to size of p | |
332 | */ | |
333 | if (BN_is_zero(pubkey) || p_size != len) { | |
334 | err_reason = DH_R_INVALID_PUBKEY; | |
335 | goto err; | |
336 | } | |
337 | if (DH_set0_key(dh, pubkey, NULL) != 1) | |
338 | goto err; | |
339 | return 1; | |
340 | err: | |
341 | DHerr(DH_F_DH_BUF2KEY, err_reason); | |
342 | BN_free(pubkey); | |
343 | return 0; | |
344 | } | |
345 | ||
346 | size_t dh_key2buf(const DH *dh, unsigned char **pbuf_out) | |
347 | { | |
348 | const BIGNUM *pubkey; | |
349 | unsigned char *pbuf; | |
350 | const BIGNUM *p; | |
351 | int p_size; | |
352 | ||
353 | DH_get0_pqg(dh, &p, NULL, NULL); | |
354 | DH_get0_key(dh, &pubkey, NULL); | |
355 | if (p == NULL || pubkey == NULL | |
356 | || (p_size = BN_num_bytes(p)) == 0 | |
357 | || BN_num_bytes(pubkey) == 0) { | |
358 | DHerr(DH_F_DH_KEY2BUF, DH_R_INVALID_PUBKEY); | |
359 | return 0; | |
360 | } | |
361 | if ((pbuf = OPENSSL_malloc(p_size)) == NULL) { | |
362 | DHerr(DH_F_DH_KEY2BUF, ERR_R_MALLOC_FAILURE); | |
363 | return 0; | |
364 | } | |
365 | /* | |
366 | * As per Section 4.2.8.1 of RFC 8446 left pad public | |
367 | * key with zeros to the size of p | |
368 | */ | |
369 | if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) { | |
370 | OPENSSL_free(pbuf); | |
371 | DHerr(DH_F_DH_KEY2BUF, DH_R_BN_ERROR); | |
372 | return 0; | |
373 | } | |
374 | *pbuf_out = pbuf; | |
375 | return p_size; | |
376 | } |