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