]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_key.c
Add a range check (from SP800-56Ar3) to DH key derivation.
[thirdparty/openssl.git] / crypto / dh / dh_key.c
CommitLineData
aa6bb135 1/*
4333b89f 2 * Copyright 1995-2021 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
ada66e78
P
10/*
11 * DH low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
d02b48c6 16#include <stdio.h>
b39fc560 17#include "internal/cryptlib.h"
706457b7 18#include "dh_local.h"
25f2138b 19#include "crypto/bn.h"
62f49b90 20#include "crypto/dh.h"
738ee181 21#include "crypto/security_bits.h"
d02b48c6 22
f844f9eb 23#ifdef FIPS_MODULE
b03ec3b5
SL
24# define MIN_STRENGTH 112
25#else
26# define MIN_STRENGTH 80
27#endif
28
13066cee 29static int generate_key(DH *dh);
f971ccb2 30static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
0f113f3e
MC
31 const BIGNUM *a, const BIGNUM *p,
32 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
13066cee
DSH
33static int dh_init(DH *dh);
34static int dh_finish(DH *dh);
35
e454a393
SL
36/*
37 * See SP800-56Ar3 Section 5.7.1.1
38 * Finite Field Cryptography Diffie-Hellman (FFC DH) Primitive
39 */
40int ossl_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
0f113f3e 41{
62f49b90
SL
42 BN_CTX *ctx = NULL;
43 BN_MONT_CTX *mont = NULL;
e454a393 44 BIGNUM *z = NULL, *pminus1;
62f49b90 45 int ret = -1;
62f49b90 46
dc8de3e6 47 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
9311d0c4 48 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
62f49b90
SL
49 goto err;
50 }
51
dc8de3e6 52 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
9311d0c4 53 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
62f49b90
SL
54 return 0;
55 }
56
8083fd3a 57 ctx = BN_CTX_new_ex(dh->libctx);
62f49b90
SL
58 if (ctx == NULL)
59 goto err;
60 BN_CTX_start(ctx);
e454a393
SL
61 pminus1 = BN_CTX_get(ctx);
62 z = BN_CTX_get(ctx);
63 if (z == NULL)
62f49b90
SL
64 goto err;
65
66 if (dh->priv_key == NULL) {
9311d0c4 67 ERR_raise(ERR_LIB_DH, DH_R_NO_PRIVATE_VALUE);
62f49b90
SL
68 goto err;
69 }
70
71 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
72 mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,
dc8de3e6 73 dh->lock, dh->params.p, ctx);
62f49b90
SL
74 BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);
75 if (!mont)
76 goto err;
77 }
e454a393
SL
78
79 /* (Step 1) Z = pub_key^priv_key mod p */
80 if (!dh->meth->bn_mod_exp(dh, z, pub_key, dh->priv_key, dh->params.p, ctx,
62f49b90 81 mont)) {
9311d0c4 82 ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
62f49b90
SL
83 goto err;
84 }
85
e454a393
SL
86 /* (Step 2) Error if z <= 1 or z = p - 1 */
87 if (BN_copy(pminus1, dh->params.p) == NULL
88 || !BN_sub_word(pminus1, 1)
89 || BN_cmp(z, BN_value_one()) <= 0
90 || BN_cmp(z, pminus1) == 0) {
91 ERR_raise(ERR_LIB_DH, DH_R_INVALID_SECRET);
92 goto err;
93 }
94
22aa4a3a 95 /* return the padded key, i.e. same number of bytes as the modulus */
e454a393 96 ret = BN_bn2binpad(z, key, BN_num_bytes(dh->params.p));
62f49b90 97 err:
e454a393 98 BN_clear(z); /* (Step 2) destroy intermediate values */
62f49b90
SL
99 BN_CTX_end(ctx);
100 BN_CTX_free(ctx);
101 return ret;
0f113f3e 102}
13066cee 103
22aa4a3a
BB
104/*-
105 * NB: This function is inherently not constant time due to the
106 * RFC 5246 (8.1.2) padding style that strips leading zero bytes.
107 */
8083fd3a 108int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
0f113f3e 109{
22aa4a3a
BB
110 int ret = 0, i;
111 volatile size_t npad = 0, mask = 1;
112
113 /* compute the key; ret is constant unless compute_key is external */
f844f9eb 114#ifdef FIPS_MODULE
e454a393 115 ret = ossl_dh_compute_key(key, pub_key, dh);
8083fd3a 116#else
22aa4a3a 117 ret = dh->meth->compute_key(key, pub_key, dh);
8083fd3a 118#endif
22aa4a3a
BB
119 if (ret <= 0)
120 return ret;
121
122 /* count leading zero bytes, yet still touch all bytes */
123 for (i = 0; i < ret; i++) {
124 mask &= !key[i];
125 npad += mask;
126 }
127
128 /* unpad key */
129 ret -= npad;
130 /* key-dependent memory access, potentially leaking npad / ret */
131 memmove(key, key + npad, ret);
132 /* key-dependent memory access, potentially leaking npad / ret */
133 memset(key + ret, 0, npad);
134
135 return ret;
0f113f3e 136}
13066cee 137
8083fd3a 138int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh)
0f113f3e
MC
139{
140 int rv, pad;
62f49b90 141
22aa4a3a 142 /* rv is constant unless compute_key is external */
f844f9eb 143#ifdef FIPS_MODULE
e454a393 144 rv = ossl_dh_compute_key(key, pub_key, dh);
62f49b90 145#else
0f113f3e 146 rv = dh->meth->compute_key(key, pub_key, dh);
62f49b90 147#endif
0f113f3e
MC
148 if (rv <= 0)
149 return rv;
dc8de3e6 150 pad = BN_num_bytes(dh->params.p) - rv;
22aa4a3a 151 /* pad is constant (zero) unless compute_key is external */
0f113f3e
MC
152 if (pad > 0) {
153 memmove(key + pad, key, rv);
154 memset(key, 0, pad);
155 }
156 return rv + pad;
157}
bc91494e 158
13066cee 159static DH_METHOD dh_ossl = {
0f113f3e
MC
160 "OpenSSL DH Method",
161 generate_key,
e454a393 162 ossl_dh_compute_key,
0f113f3e
MC
163 dh_bn_mod_exp,
164 dh_init,
165 dh_finish,
166 DH_FLAG_FIPS_METHOD,
167 NULL,
168 NULL
13066cee
DSH
169};
170
076fc555
RS
171static const DH_METHOD *default_DH_method = &dh_ossl;
172
f971ccb2 173const DH_METHOD *DH_OpenSSL(void)
13066cee 174{
0f113f3e 175 return &dh_ossl;
13066cee
DSH
176}
177
62f49b90
SL
178const DH_METHOD *DH_get_default_method(void)
179{
180 return default_DH_method;
181}
182
183static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
184 const BIGNUM *a, const BIGNUM *p,
185 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
186{
187 return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx);
188}
189
190static int dh_init(DH *dh)
191{
192 dh->flags |= DH_FLAG_CACHE_MONT_P;
5357c106 193 ossl_ffc_params_init(&dh->params);
f11f86f6 194 dh->dirty_cnt++;
62f49b90
SL
195 return 1;
196}
197
198static int dh_finish(DH *dh)
199{
200 BN_MONT_CTX_free(dh->method_mont_p);
201 return 1;
202}
203
f844f9eb 204#ifndef FIPS_MODULE
076fc555
RS
205void DH_set_default_method(const DH_METHOD *meth)
206{
207 default_DH_method = meth;
208}
f844f9eb 209#endif /* FIPS_MODULE */
076fc555 210
62f49b90 211int DH_generate_key(DH *dh)
076fc555 212{
f844f9eb 213#ifdef FIPS_MODULE
8083fd3a
SL
214 return generate_key(dh);
215#else
62f49b90 216 return dh->meth->generate_key(dh);
8083fd3a 217#endif
076fc555
RS
218}
219
19dbb742
SL
220int ossl_dh_generate_public_key(BN_CTX *ctx, const DH *dh,
221 const BIGNUM *priv_key, BIGNUM *pub_key)
8083fd3a
SL
222{
223 int ret = 0;
224 BIGNUM *prk = BN_new();
225 BN_MONT_CTX *mont = NULL;
226
227 if (prk == NULL)
228 return 0;
229
230 if (dh->flags & DH_FLAG_CACHE_MONT_P) {
d1fb6b48
NT
231 /*
232 * We take the input DH as const, but we lie, because in some cases we
233 * want to get a hold of its Montgomery context.
234 *
235 * We cast to remove the const qualifier in this case, it should be
236 * fine...
237 */
238 BN_MONT_CTX **pmont = (BN_MONT_CTX **)&dh->method_mont_p;
239
240 mont = BN_MONT_CTX_set_locked(pmont, dh->lock, dh->params.p, ctx);
8083fd3a
SL
241 if (mont == NULL)
242 goto err;
243 }
244 BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);
245
246 /* pub_key = g^priv_key mod p */
247 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->params.g, prk, dh->params.p,
248 ctx, mont))
249 goto err;
250 ret = 1;
251err:
252 BN_clear_free(prk);
253 return ret;
254}
255
256static int generate_key(DH *dh)
0f113f3e
MC
257{
258 int ok = 0;
259 int generate_new_key = 0;
f844f9eb 260#ifndef FIPS_MODULE
0f113f3e 261 unsigned l;
f11f86f6 262#endif
91f7361f 263 BN_CTX *ctx = NULL;
0f113f3e 264 BIGNUM *pub_key = NULL, *priv_key = NULL;
d02b48c6 265
dc8de3e6 266 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
9311d0c4 267 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
91f7361f
GV
268 return 0;
269 }
270
dc8de3e6 271 if (BN_num_bits(dh->params.p) < DH_MIN_MODULUS_BITS) {
9311d0c4 272 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_SMALL);
6de1fe90
BE
273 return 0;
274 }
275
8083fd3a 276 ctx = BN_CTX_new_ex(dh->libctx);
0f113f3e
MC
277 if (ctx == NULL)
278 goto err;
d02b48c6 279
0f113f3e 280 if (dh->priv_key == NULL) {
74924dcb 281 priv_key = BN_secure_new();
0f113f3e
MC
282 if (priv_key == NULL)
283 goto err;
284 generate_new_key = 1;
8083fd3a 285 } else {
0f113f3e 286 priv_key = dh->priv_key;
8083fd3a 287 }
d02b48c6 288
0f113f3e
MC
289 if (dh->pub_key == NULL) {
290 pub_key = BN_new();
291 if (pub_key == NULL)
292 goto err;
8083fd3a 293 } else {
0f113f3e 294 pub_key = dh->pub_key;
0f113f3e 295 }
0f113f3e 296 if (generate_new_key) {
f11f86f6
SL
297 /* Is it an approved safe prime ?*/
298 if (DH_get_nid(dh) != NID_undef) {
738ee181 299 int max_strength =
9500c823 300 ossl_ifc_ffc_compute_security_bits(BN_num_bits(dh->params.p));
738ee181 301
55f02cb6 302 if (dh->params.q == NULL
f11f86f6
SL
303 || dh->length > BN_num_bits(dh->params.q))
304 goto err;
738ee181 305 /* dh->length = maximum bit length of generated private key */
5357c106
P
306 if (!ossl_ffc_generate_private_key(ctx, &dh->params, dh->length,
307 max_strength, priv_key))
f11f86f6
SL
308 goto err;
309 } else {
f844f9eb 310#ifdef FIPS_MODULE
f11f86f6
SL
311 if (dh->params.q == NULL)
312 goto err;
313#else
314 if (dh->params.q == NULL) {
28e1d588
RL
315 /* secret exponent length, must satisfy 2^(l-1) <= p */
316 if (dh->length != 0
317 && dh->length >= BN_num_bits(dh->params.p))
318 goto err;
f11f86f6
SL
319 l = dh->length ? dh->length : BN_num_bits(dh->params.p) - 1;
320 if (!BN_priv_rand_ex(priv_key, l, BN_RAND_TOP_ONE,
321 BN_RAND_BOTTOM_ANY, ctx))
322 goto err;
323 /*
324 * We handle just one known case where g is a quadratic non-residue:
325 * for g = 2: p % 8 == 3
326 */
327 if (BN_is_word(dh->params.g, DH_GENERATOR_2)
328 && !BN_is_bit_set(dh->params.p, 2)) {
329 /* clear bit 0, since it won't be a secret anyway */
330 if (!BN_clear_bit(priv_key, 0))
331 goto err;
332 }
333 } else
334#endif
335 {
63794b04 336 /* Do a partial check for invalid p, q, g */
5357c106 337 if (!ossl_ffc_params_simple_validate(dh->libctx, &dh->params,
ba37b820 338 FFC_PARAM_TYPE_DH, NULL))
63794b04 339 goto err;
f11f86f6
SL
340 /*
341 * For FFC FIPS 186-4 keygen
342 * security strength s = 112,
343 * Max Private key size N = len(q)
344 */
5357c106
P
345 if (!ossl_ffc_generate_private_key(ctx, &dh->params,
346 BN_num_bits(dh->params.q),
347 MIN_STRENGTH,
348 priv_key))
a38c878c
BE
349 goto err;
350 }
0f113f3e
MC
351 }
352 }
dfeab068 353
19dbb742 354 if (!ossl_dh_generate_public_key(ctx, dh, priv_key, pub_key))
8083fd3a 355 goto err;
46a64376 356
0f113f3e
MC
357 dh->pub_key = pub_key;
358 dh->priv_key = priv_key;
8b84b075 359 dh->dirty_cnt++;
0f113f3e
MC
360 ok = 1;
361 err:
362 if (ok != 1)
9311d0c4 363 ERR_raise(ERR_LIB_DH, ERR_R_BN_LIB);
d02b48c6 364
23a1d5e9 365 if (pub_key != dh->pub_key)
0f113f3e 366 BN_free(pub_key);
23a1d5e9 367 if (priv_key != dh->priv_key)
0f113f3e
MC
368 BN_free(priv_key);
369 BN_CTX_free(ctx);
26a7d938 370 return ok;
0f113f3e 371}
d02b48c6 372
19dbb742 373int ossl_dh_buf2key(DH *dh, const unsigned char *buf, size_t len)
9aaecbfc 374{
375 int err_reason = DH_R_BN_ERROR;
376 BIGNUM *pubkey = NULL;
377 const BIGNUM *p;
378 size_t p_size;
379
380 if ((pubkey = BN_bin2bn(buf, len, NULL)) == NULL)
381 goto err;
382 DH_get0_pqg(dh, &p, NULL, NULL);
383 if (p == NULL || (p_size = BN_num_bytes(p)) == 0) {
384 err_reason = DH_R_NO_PARAMETERS_SET;
385 goto err;
386 }
387 /*
388 * As per Section 4.2.8.1 of RFC 8446 fail if DHE's
389 * public key is of size not equal to size of p
390 */
391 if (BN_is_zero(pubkey) || p_size != len) {
392 err_reason = DH_R_INVALID_PUBKEY;
393 goto err;
394 }
395 if (DH_set0_key(dh, pubkey, NULL) != 1)
396 goto err;
397 return 1;
398err:
9311d0c4 399 ERR_raise(ERR_LIB_DH, err_reason);
9aaecbfc 400 BN_free(pubkey);
401 return 0;
402}
403
19dbb742
SL
404size_t ossl_dh_key2buf(const DH *dh, unsigned char **pbuf_out, size_t size,
405 int alloc)
9aaecbfc 406{
407 const BIGNUM *pubkey;
6a9bd929 408 unsigned char *pbuf = NULL;
9aaecbfc 409 const BIGNUM *p;
410 int p_size;
411
412 DH_get0_pqg(dh, &p, NULL, NULL);
413 DH_get0_key(dh, &pubkey, NULL);
414 if (p == NULL || pubkey == NULL
415 || (p_size = BN_num_bytes(p)) == 0
416 || BN_num_bytes(pubkey) == 0) {
9311d0c4 417 ERR_raise(ERR_LIB_DH, DH_R_INVALID_PUBKEY);
9aaecbfc 418 return 0;
419 }
6a9bd929
MC
420 if (pbuf_out != NULL && (alloc || *pbuf_out != NULL)) {
421 if (!alloc) {
422 if (size >= (size_t)p_size)
423 pbuf = *pbuf_out;
424 } else {
425 pbuf = OPENSSL_malloc(p_size);
426 }
427
428 if (pbuf == NULL) {
9311d0c4 429 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
6a9bd929
MC
430 return 0;
431 }
432 /*
433 * As per Section 4.2.8.1 of RFC 8446 left pad public
434 * key with zeros to the size of p
435 */
436 if (BN_bn2binpad(pubkey, pbuf, p_size) < 0) {
437 if (alloc)
438 OPENSSL_free(pbuf);
9311d0c4 439 ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
6a9bd929
MC
440 return 0;
441 }
442 *pbuf_out = pbuf;
9aaecbfc 443 }
9aaecbfc 444 return p_size;
445}