]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/keymgmt/dh_kmgmt.c
Reduce optimization in hppa builds
[thirdparty/openssl.git] / providers / implementations / keymgmt / dh_kmgmt.c
CommitLineData
8b84b075 1/*
da1c088f 2 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
8b84b075
RL
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
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"
b697864c 15#include "internal/common.h"
ada66e78 16
7165593c 17#include <string.h> /* strcmp */
23c48d94 18#include <openssl/core_dispatch.h>
8b84b075
RL
19#include <openssl/core_names.h>
20#include <openssl/bn.h>
7165593c 21#include <openssl/err.h>
af3e7e1b 22#include "prov/implementations.h"
1640d48c 23#include "prov/providercommon.h"
8083fd3a
SL
24#include "prov/provider_ctx.h"
25#include "crypto/dh.h"
7165593c 26#include "internal/sizes.h"
8b84b075 27
363b1e5d
DMSP
28static OSSL_FUNC_keymgmt_new_fn dh_newdata;
29static OSSL_FUNC_keymgmt_free_fn dh_freedata;
30static OSSL_FUNC_keymgmt_gen_init_fn dh_gen_init;
627c2203 31static OSSL_FUNC_keymgmt_gen_init_fn dhx_gen_init;
363b1e5d
DMSP
32static OSSL_FUNC_keymgmt_gen_set_template_fn dh_gen_set_template;
33static OSSL_FUNC_keymgmt_gen_set_params_fn dh_gen_set_params;
34static OSSL_FUNC_keymgmt_gen_settable_params_fn dh_gen_settable_params;
35static OSSL_FUNC_keymgmt_gen_fn dh_gen;
36static OSSL_FUNC_keymgmt_gen_cleanup_fn dh_gen_cleanup;
7c664b1f 37static OSSL_FUNC_keymgmt_load_fn dh_load;
363b1e5d
DMSP
38static OSSL_FUNC_keymgmt_get_params_fn dh_get_params;
39static OSSL_FUNC_keymgmt_gettable_params_fn dh_gettable_params;
40static OSSL_FUNC_keymgmt_set_params_fn dh_set_params;
41static OSSL_FUNC_keymgmt_settable_params_fn dh_settable_params;
42static OSSL_FUNC_keymgmt_has_fn dh_has;
43static OSSL_FUNC_keymgmt_match_fn dh_match;
44static OSSL_FUNC_keymgmt_validate_fn dh_validate;
45static OSSL_FUNC_keymgmt_import_fn dh_import;
46static OSSL_FUNC_keymgmt_import_types_fn dh_import_types;
47static OSSL_FUNC_keymgmt_export_fn dh_export;
48static OSSL_FUNC_keymgmt_export_types_fn dh_export_types;
4a9fe33c 49static OSSL_FUNC_keymgmt_dup_fn dh_dup;
8dd5c603 50
7165593c 51#define DH_POSSIBLE_SELECTIONS \
273a67e3 52 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
8b84b075 53
7165593c 54struct dh_gen_ctx {
b4250010 55 OSSL_LIB_CTX *libctx;
7165593c
SL
56
57 FFC_PARAMS *ffc_params;
58 int selection;
59 /* All these parameters are used for parameter generation only */
60 /* If there is a group name then the remaining parameters are not needed */
61 int group_nid;
62 size_t pbits;
63 size_t qbits;
7165593c
SL
64 unsigned char *seed; /* optional FIPS186-4 param for testing */
65 size_t seedlen;
66 int gindex; /* optional FIPS186-4 generator index (ignored if -1) */
67 int gen_type; /* see dhtype2id */
68 int generator; /* Used by DH_PARAMGEN_TYPE_GENERATOR in non fips mode only */
69 int pcounter;
70 int hindex;
738ee181 71 int priv_len;
7165593c 72
f2a71518
SL
73 char *mdname;
74 char *mdprops;
7165593c
SL
75 OSSL_CALLBACK *cb;
76 void *cbarg;
627c2203 77 int dh_type;
7165593c
SL
78};
79
c829c23b 80static int dh_gen_type_name2id_w_default(const char *name, int type)
7165593c 81{
c2bd8d27
MC
82 if (strcmp(name, "default") == 0) {
83#ifdef FIPS_MODULE
84 if (type == DH_FLAG_TYPE_DHX)
85 return DH_PARAMGEN_TYPE_FIPS_186_4;
86
87 return DH_PARAMGEN_TYPE_GROUP;
88#else
89 if (type == DH_FLAG_TYPE_DHX)
90 return DH_PARAMGEN_TYPE_FIPS_186_2;
91
92 return DH_PARAMGEN_TYPE_GENERATOR;
93#endif
94 }
95
f1ffaaee 96 return ossl_dh_gen_type_name2id(name, type);
c8f23016
RL
97}
98
8dd5c603 99static void *dh_newdata(void *provctx)
14e3e00f 100{
627c2203
SL
101 DH *dh = NULL;
102
422cbcee 103 if (ossl_prov_is_running()) {
19dbb742 104 dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx));
422cbcee
P
105 if (dh != NULL) {
106 DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
107 DH_set_flags(dh, DH_FLAG_TYPE_DH);
108 }
627c2203
SL
109 }
110 return dh;
111}
112
113static void *dhx_newdata(void *provctx)
114{
115 DH *dh = NULL;
116
19dbb742 117 dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx));
627c2203
SL
118 if (dh != NULL) {
119 DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
120 DH_set_flags(dh, DH_FLAG_TYPE_DHX);
121 }
122 return dh;
14e3e00f
RL
123}
124
8dd5c603 125static void dh_freedata(void *keydata)
c8f23016 126{
8dd5c603
RL
127 DH_free(keydata);
128}
1640d48c 129
3d914185 130static int dh_has(const void *keydata, int selection)
8dd5c603 131{
3d914185 132 const DH *dh = keydata;
9a485440
TM
133 int ok = 1;
134
135 if (!ossl_prov_is_running() || dh == NULL)
136 return 0;
137 if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
138 return 1; /* the selection is not missing */
139
140 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
141 ok = ok && (DH_get0_pub_key(dh) != NULL);
142 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
143 ok = ok && (DH_get0_priv_key(dh) != NULL);
144 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
145 ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
8dd5c603 146 return ok;
c8f23016
RL
147}
148
2888fc15
RL
149static int dh_match(const void *keydata1, const void *keydata2, int selection)
150{
151 const DH *dh1 = keydata1;
152 const DH *dh2 = keydata2;
153 int ok = 1;
154
422cbcee
P
155 if (!ossl_prov_is_running())
156 return 0;
157
ee22a374
RL
158 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
159 int key_checked = 0;
160
161 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
162 const BIGNUM *pa = DH_get0_pub_key(dh1);
163 const BIGNUM *pb = DH_get0_pub_key(dh2);
164
165 if (pa != NULL && pb != NULL) {
166 ok = ok && BN_cmp(pa, pb) == 0;
167 key_checked = 1;
168 }
169 }
170 if (!key_checked
171 && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
172 const BIGNUM *pa = DH_get0_priv_key(dh1);
173 const BIGNUM *pb = DH_get0_priv_key(dh2);
174
175 if (pa != NULL && pb != NULL) {
176 ok = ok && BN_cmp(pa, pb) == 0;
177 key_checked = 1;
178 }
179 }
180 ok = ok && key_checked;
181 }
2888fc15 182 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
19dbb742
SL
183 FFC_PARAMS *dhparams1 = ossl_dh_get0_params((DH *)dh1);
184 FFC_PARAMS *dhparams2 = ossl_dh_get0_params((DH *)dh2);
2888fc15 185
5357c106 186 ok = ok && ossl_ffc_params_cmp(dhparams1, dhparams2, 1);
2888fc15
RL
187 }
188 return ok;
189}
190
8dd5c603 191static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
8b84b075 192{
8dd5c603 193 DH *dh = keydata;
7165593c 194 int ok = 1;
8dd5c603 195
422cbcee 196 if (!ossl_prov_is_running() || dh == NULL)
8dd5c603
RL
197 return 0;
198
7165593c
SL
199 if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
200 return 0;
8dd5c603 201
9ac82e2e
TM
202 /* a key without parameters is meaningless */
203 ok = ok && ossl_dh_params_fromdata(dh, params);
7165593c 204
944f822a 205 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
206 int include_private =
207 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
208
209 ok = ok && ossl_dh_key_fromdata(dh, params, include_private);
210 }
8dd5c603
RL
211
212 return ok;
8b84b075
RL
213}
214
8dd5c603
RL
215static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
216 void *cbarg)
c8f23016 217{
8dd5c603 218 DH *dh = keydata;
b03ec3b5 219 OSSL_PARAM_BLD *tmpl = NULL;
1640d48c 220 OSSL_PARAM *params = NULL;
8dd5c603
RL
221 int ok = 1;
222
422cbcee 223 if (!ossl_prov_is_running() || dh == NULL)
8dd5c603 224 return 0;
1640d48c 225
1ae4678c
TM
226 if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
227 return 0;
228
6d4e6009
P
229 tmpl = OSSL_PARAM_BLD_new();
230 if (tmpl == NULL)
231 return 0;
8dd5c603
RL
232
233 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
19dbb742 234 ok = ok && ossl_dh_params_todata(dh, tmpl, NULL);
944f822a 235
236 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
237 int include_private =
238 selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
239
240 ok = ok && ossl_dh_key_todata(dh, tmpl, NULL, include_private);
241 }
8dd5c603 242
46c1c2d7 243 if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
b03ec3b5
SL
244 ok = 0;
245 goto err;
6d4e6009 246 }
46c1c2d7 247
8dd5c603 248 ok = param_cb(params, cbarg);
3f883c7c 249 OSSL_PARAM_free(params);
b03ec3b5
SL
250err:
251 OSSL_PARAM_BLD_free(tmpl);
8dd5c603 252 return ok;
c8f23016
RL
253}
254
273a67e3
RL
255/* IMEXPORT = IMPORT + EXPORT */
256
7165593c
SL
257# define DH_IMEXPORTABLE_PARAMETERS \
258 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
259 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
260 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \
261 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \
262 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \
263 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \
264 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \
8f81e3a1 265 OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL), \
b8086652 266 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), \
8f81e3a1 267 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0)
7165593c 268# define DH_IMEXPORTABLE_PUBLIC_KEY \
90d3cb57 269 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
7165593c 270# define DH_IMEXPORTABLE_PRIVATE_KEY \
90d3cb57 271 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
273a67e3
RL
272static const OSSL_PARAM dh_all_types[] = {
273 DH_IMEXPORTABLE_PARAMETERS,
274 DH_IMEXPORTABLE_PUBLIC_KEY,
275 DH_IMEXPORTABLE_PRIVATE_KEY,
276 OSSL_PARAM_END
277};
278static const OSSL_PARAM dh_parameter_types[] = {
279 DH_IMEXPORTABLE_PARAMETERS,
280 OSSL_PARAM_END
281};
282static const OSSL_PARAM dh_key_types[] = {
283 DH_IMEXPORTABLE_PUBLIC_KEY,
284 DH_IMEXPORTABLE_PRIVATE_KEY,
285 OSSL_PARAM_END
286};
287static const OSSL_PARAM *dh_types[] = {
288 NULL, /* Index 0 = none of them */
289 dh_parameter_types, /* Index 1 = parameter types */
290 dh_key_types, /* Index 2 = key types */
291 dh_all_types /* Index 3 = 1 + 2 */
292};
293
294static const OSSL_PARAM *dh_imexport_types(int selection)
295{
296 int type_select = 0;
297
298 if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
299 type_select += 1;
300 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
301 type_select += 2;
302 return dh_types[type_select];
303}
304
305static const OSSL_PARAM *dh_import_types(int selection)
306{
307 return dh_imexport_types(selection);
308}
309
310static const OSSL_PARAM *dh_export_types(int selection)
311{
312 return dh_imexport_types(selection);
313}
314
8dd5c603 315static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
9e5aaf78
RL
316{
317 DH *dh = key;
318 OSSL_PARAM *p;
319
320 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
321 && !OSSL_PARAM_set_int(p, DH_bits(dh)))
322 return 0;
323 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
324 && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
325 return 0;
326 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
327 && !OSSL_PARAM_set_int(p, DH_size(dh)))
328 return 0;
5ac8fb58 329 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
6a9bd929
MC
330 if (p->data_type != OSSL_PARAM_OCTET_STRING)
331 return 0;
19dbb742
SL
332 p->return_size = ossl_dh_key2buf(dh, (unsigned char **)&p->data,
333 p->data_size, 0);
6a9bd929
MC
334 if (p->return_size == 0)
335 return 0;
336 }
337
19dbb742 338 return ossl_dh_params_todata(dh, NULL, params)
944f822a 339 && ossl_dh_key_todata(dh, NULL, params, 1);
9e5aaf78
RL
340}
341
273a67e3
RL
342static const OSSL_PARAM dh_params[] = {
343 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
344 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
345 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
5ac8fb58 346 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
7165593c
SL
347 DH_IMEXPORTABLE_PARAMETERS,
348 DH_IMEXPORTABLE_PUBLIC_KEY,
349 DH_IMEXPORTABLE_PRIVATE_KEY,
273a67e3
RL
350 OSSL_PARAM_END
351};
352
af5e1e85 353static const OSSL_PARAM *dh_gettable_params(void *provctx)
273a67e3
RL
354{
355 return dh_params;
356}
357
6a9bd929 358static const OSSL_PARAM dh_known_settable_params[] = {
5ac8fb58 359 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
6a9bd929
MC
360 OSSL_PARAM_END
361};
362
af5e1e85 363static const OSSL_PARAM *dh_settable_params(void *provctx)
6a9bd929
MC
364{
365 return dh_known_settable_params;
366}
367
368static int dh_set_params(void *key, const OSSL_PARAM params[])
369{
370 DH *dh = key;
371 const OSSL_PARAM *p;
372
5ac8fb58 373 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
6a9bd929
MC
374 if (p != NULL
375 && (p->data_type != OSSL_PARAM_OCTET_STRING
19dbb742 376 || !ossl_dh_buf2key(dh, p->data, p->data_size)))
6a9bd929
MC
377 return 0;
378
379 return 1;
380}
381
4718326a 382static int dh_validate_public(const DH *dh, int checktype)
a54ff473
SL
383{
384 const BIGNUM *pub_key = NULL;
4718326a 385 int res = 0;
a54ff473
SL
386
387 DH_get0_key(dh, &pub_key, NULL);
2fc2e37b
MB
388 if (pub_key == NULL)
389 return 0;
4718326a
SL
390
391 /* The partial test is only valid for named group's with q = (p - 1) / 2 */
392 if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK
393 && ossl_dh_is_named_safe_prime_group(dh))
19dbb742 394 return ossl_dh_check_pub_key_partial(dh, pub_key, &res);
4718326a 395
eaee1765 396 return DH_check_pub_key_ex(dh, pub_key);
a54ff473
SL
397}
398
d1fb6b48 399static int dh_validate_private(const DH *dh)
a54ff473
SL
400{
401 int status = 0;
402 const BIGNUM *priv_key = NULL;
403
404 DH_get0_key(dh, NULL, &priv_key);
2fc2e37b
MB
405 if (priv_key == NULL)
406 return 0;
d715dbd8 407 return ossl_dh_check_priv_key(dh, priv_key, &status);
a54ff473
SL
408}
409
899e2564 410static int dh_validate(const void *keydata, int selection, int checktype)
a54ff473 411{
d1fb6b48 412 const DH *dh = keydata;
9a485440 413 int ok = 1;
a54ff473 414
422cbcee
P
415 if (!ossl_prov_is_running())
416 return 0;
417
9a485440
TM
418 if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
419 return 1; /* nothing to validate */
a54ff473 420
899e2564
MC
421 if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
422 /*
423 * Both of these functions check parameters. DH_check_params_ex()
424 * performs a lightweight check (e.g. it does not check that p is a
425 * safe prime)
426 */
427 if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
428 ok = ok && DH_check_params_ex(dh);
429 else
430 ok = ok && DH_check_ex(dh);
431 }
a54ff473
SL
432
433 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
4718326a 434 ok = ok && dh_validate_public(dh, checktype);
a54ff473
SL
435
436 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
437 ok = ok && dh_validate_private(dh);
438
439 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
440 == OSSL_KEYMGMT_SELECT_KEYPAIR)
19dbb742 441 ok = ok && ossl_dh_check_pairwise(dh);
a54ff473
SL
442 return ok;
443}
444
f9562909
P
445static void *dh_gen_init_base(void *provctx, int selection,
446 const OSSL_PARAM params[], int type)
7165593c 447{
a829b735 448 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
7165593c
SL
449 struct dh_gen_ctx *gctx = NULL;
450
422cbcee
P
451 if (!ossl_prov_is_running())
452 return NULL;
453
7165593c
SL
454 if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
455 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
456 return NULL;
457
458 if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
459 gctx->selection = selection;
460 gctx->libctx = libctx;
461 gctx->pbits = 2048;
462 gctx->qbits = 224;
4f2271d5 463 gctx->mdname = NULL;
c2bd8d27
MC
464#ifdef FIPS_MODULE
465 gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
466 ? DH_PARAMGEN_TYPE_FIPS_186_4
467 : DH_PARAMGEN_TYPE_GROUP;
468#else
469 gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
470 ? DH_PARAMGEN_TYPE_FIPS_186_2
471 : DH_PARAMGEN_TYPE_GENERATOR;
472#endif
7165593c
SL
473 gctx->gindex = -1;
474 gctx->hindex = 0;
475 gctx->pcounter = -1;
476 gctx->generator = DH_GENERATOR_2;
627c2203 477 gctx->dh_type = type;
7165593c 478 }
f9562909
P
479 if (!dh_gen_set_params(gctx, params)) {
480 OPENSSL_free(gctx);
481 gctx = NULL;
482 }
7165593c
SL
483 return gctx;
484}
485
f9562909
P
486static void *dh_gen_init(void *provctx, int selection,
487 const OSSL_PARAM params[])
627c2203 488{
f9562909 489 return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DH);
627c2203
SL
490}
491
f9562909
P
492static void *dhx_gen_init(void *provctx, int selection,
493 const OSSL_PARAM params[])
627c2203 494{
f9562909 495 return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DHX);
627c2203
SL
496}
497
7165593c
SL
498static int dh_gen_set_template(void *genctx, void *templ)
499{
500 struct dh_gen_ctx *gctx = genctx;
501 DH *dh = templ;
502
422cbcee 503 if (!ossl_prov_is_running() || gctx == NULL || dh == NULL)
7165593c 504 return 0;
19dbb742 505 gctx->ffc_params = ossl_dh_get0_params(dh);
7165593c
SL
506 return 1;
507}
508
509static int dh_set_gen_seed(struct dh_gen_ctx *gctx, unsigned char *seed,
510 size_t seedlen)
511{
512 OPENSSL_clear_free(gctx->seed, gctx->seedlen);
513 gctx->seed = NULL;
514 gctx->seedlen = 0;
515 if (seed != NULL && seedlen > 0) {
516 gctx->seed = OPENSSL_memdup(seed, seedlen);
517 if (gctx->seed == NULL)
518 return 0;
519 gctx->seedlen = seedlen;
520 }
521 return 1;
522}
523
f1ffaaee 524static int dh_gen_common_set_params(void *genctx, const OSSL_PARAM params[])
7165593c
SL
525{
526 struct dh_gen_ctx *gctx = genctx;
527 const OSSL_PARAM *p;
b697864c 528 int gen_type = -1;
7165593c
SL
529
530 if (gctx == NULL)
531 return 0;
f9562909
P
532 if (params == NULL)
533 return 1;
534
7165593c
SL
535 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
536 if (p != NULL) {
537 if (p->data_type != OSSL_PARAM_UTF8_STRING
b697864c 538 || ((gen_type =
19dbb742 539 dh_gen_type_name2id_w_default(p->data, gctx->dh_type)) == -1)) {
7165593c
SL
540 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
541 return 0;
542 }
b697864c
NH
543 if (gen_type != -1)
544 gctx->gen_type = gen_type;
7165593c 545 }
023b188c 546 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
7165593c 547 if (p != NULL) {
c829c23b
RL
548 const DH_NAMED_GROUP *group = NULL;
549
7165593c 550 if (p->data_type != OSSL_PARAM_UTF8_STRING
3b53f88c 551 || p->data == NULL
c829c23b
RL
552 || (group = ossl_ffc_name_to_dh_named_group(p->data)) == NULL
553 || ((gctx->group_nid =
554 ossl_ffc_named_group_get_uid(group)) == NID_undef)) {
7165593c
SL
555 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
556 return 0;
557 }
7165593c 558 }
f1ffaaee
SL
559 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
560 && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
561 return 0;
562 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
563 if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->priv_len))
564 return 0;
565 return 1;
566}
567
568static const OSSL_PARAM *dh_gen_settable_params(ossl_unused void *genctx,
569 ossl_unused void *provctx)
570{
571 static const OSSL_PARAM dh_gen_settable[] = {
572 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
573 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
574 OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
575 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
576 OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_GENERATOR, NULL),
577 OSSL_PARAM_END
578 };
579 return dh_gen_settable;
580}
581
582static const OSSL_PARAM *dhx_gen_settable_params(ossl_unused void *genctx,
583 ossl_unused void *provctx)
584{
585 static const OSSL_PARAM dhx_gen_settable[] = {
586 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
587 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
588 OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
589 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
590 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
591 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
592 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
593 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
594 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
595 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
596 OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
597 OSSL_PARAM_END
598 };
599 return dhx_gen_settable;
600}
601
602static int dhx_gen_set_params(void *genctx, const OSSL_PARAM params[])
603{
604 struct dh_gen_ctx *gctx = genctx;
605 const OSSL_PARAM *p;
606
607 if (!dh_gen_common_set_params(genctx, params))
7165593c 608 return 0;
f1ffaaee
SL
609
610 /* Parameters related to fips186-4 and fips186-2 */
7165593c 611 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
738ee181 612 if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->gindex))
7165593c
SL
613 return 0;
614 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
738ee181 615 if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->pcounter))
7165593c
SL
616 return 0;
617 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
738ee181 618 if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->hindex))
7165593c
SL
619 return 0;
620 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
621 if (p != NULL
622 && (p->data_type != OSSL_PARAM_OCTET_STRING
623 || !dh_set_gen_seed(gctx, p->data, p->data_size)))
624 return 0;
7165593c
SL
625 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
626 && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
627 return 0;
628 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
629 if (p != NULL) {
7165593c
SL
630 if (p->data_type != OSSL_PARAM_UTF8_STRING)
631 return 0;
f2a71518
SL
632 OPENSSL_free(gctx->mdname);
633 gctx->mdname = OPENSSL_strdup(p->data);
634 if (gctx->mdname == NULL)
635 return 0;
4f2271d5
SL
636 }
637 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
638 if (p != NULL) {
639 if (p->data_type != OSSL_PARAM_UTF8_STRING)
7165593c 640 return 0;
f2a71518
SL
641 OPENSSL_free(gctx->mdprops);
642 gctx->mdprops = OPENSSL_strdup(p->data);
643 if (gctx->mdprops == NULL)
644 return 0;
7165593c 645 }
f1ffaaee
SL
646
647 /* Parameters that are not allowed for DHX */
648 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR);
649 if (p != NULL) {
650 ERR_raise(ERR_LIB_PROV, ERR_R_UNSUPPORTED);
738ee181 651 return 0;
f1ffaaee 652 }
7165593c
SL
653 return 1;
654}
655
f1ffaaee 656static int dh_gen_set_params(void *genctx, const OSSL_PARAM params[])
7165593c 657{
f1ffaaee
SL
658 struct dh_gen_ctx *gctx = genctx;
659 const OSSL_PARAM *p;
660
661 if (!dh_gen_common_set_params(genctx, params))
662 return 0;
663
664 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR);
665 if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->generator))
666 return 0;
667
668 /* Parameters that are not allowed for DH */
669 if (OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX) != NULL
670 || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER) != NULL
671 || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H) != NULL
672 || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED) != NULL
673 || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS) != NULL
674 || OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST) != NULL
675 || OSSL_PARAM_locate_const(params,
676 OSSL_PKEY_PARAM_FFC_DIGEST_PROPS) != NULL) {
677 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
678 return 0;
679 }
680 return 1;
7165593c
SL
681}
682
683static int dh_gencb(int p, int n, BN_GENCB *cb)
684{
685 struct dh_gen_ctx *gctx = BN_GENCB_get_arg(cb);
686 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
687
688 params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
689 params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
690
691 return gctx->cb(params, gctx->cbarg);
692}
693
694static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
695{
696 int ret = 0;
697 struct dh_gen_ctx *gctx = genctx;
698 DH *dh = NULL;
699 BN_GENCB *gencb = NULL;
700 FFC_PARAMS *ffc;
701
422cbcee 702 if (!ossl_prov_is_running() || gctx == NULL)
7165593c
SL
703 return NULL;
704
f1ffaaee 705 /*
ad31628c 706 * If a group name is selected then the type is group regardless of what
f1ffaaee
SL
707 * the user selected. This overrides rather than errors for backwards
708 * compatibility.
709 */
710 if (gctx->group_nid != NID_undef)
711 gctx->gen_type = DH_PARAMGEN_TYPE_GROUP;
712
5056133c
NH
713 /*
714 * Do a bounds check on context gen_type. Must be in range:
715 * DH_PARAMGEN_TYPE_GENERATOR <= gen_type <= DH_PARAMGEN_TYPE_GROUP
716 * Noted here as this needs to be adjusted if a new group type is
717 * added.
718 */
b697864c
NH
719 if (!ossl_assert((gctx->gen_type >= DH_PARAMGEN_TYPE_GENERATOR)
720 && (gctx->gen_type <= DH_PARAMGEN_TYPE_GROUP))) {
721 ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
722 "gen_type set to unsupported value %d", gctx->gen_type);
723 return NULL;
724 }
725
7165593c 726 /* For parameter generation - If there is a group name just create it */
c2bd8d27
MC
727 if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP
728 && gctx->ffc_params == NULL) {
7165593c
SL
729 /* Select a named group if there is not one already */
730 if (gctx->group_nid == NID_undef)
19dbb742 731 gctx->group_nid = ossl_dh_get_named_group_uid_from_size(gctx->pbits);
7165593c
SL
732 if (gctx->group_nid == NID_undef)
733 return NULL;
19dbb742 734 dh = ossl_dh_new_by_nid_ex(gctx->libctx, gctx->group_nid);
7165593c
SL
735 if (dh == NULL)
736 return NULL;
19dbb742 737 ffc = ossl_dh_get0_params(dh);
7165593c 738 } else {
19dbb742 739 dh = ossl_dh_new_ex(gctx->libctx);
7165593c
SL
740 if (dh == NULL)
741 return NULL;
19dbb742 742 ffc = ossl_dh_get0_params(dh);
7165593c
SL
743
744 /* Copy the template value if one was passed */
745 if (gctx->ffc_params != NULL
5357c106 746 && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
7165593c
SL
747 goto end;
748
5357c106 749 if (!ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
7165593c
SL
750 goto end;
751 if (gctx->gindex != -1) {
5357c106 752 ossl_ffc_params_set_gindex(ffc, gctx->gindex);
7165593c 753 if (gctx->pcounter != -1)
5357c106 754 ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
7165593c 755 } else if (gctx->hindex != 0) {
5357c106 756 ossl_ffc_params_set_h(ffc, gctx->hindex);
7165593c 757 }
a76ccb9d 758 if (gctx->mdname != NULL)
759 ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops);
7165593c
SL
760 gctx->cb = osslcb;
761 gctx->cbarg = cbarg;
762 gencb = BN_GENCB_new();
763 if (gencb != NULL)
764 BN_GENCB_set(gencb, dh_gencb, genctx);
765
766 if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
767 /*
768 * NOTE: The old safe prime generator code is not used in fips mode,
769 * (i.e internally it ignores the generator and chooses a named
770 * group based on pbits.
771 */
772 if (gctx->gen_type == DH_PARAMGEN_TYPE_GENERATOR)
773 ret = DH_generate_parameters_ex(dh, gctx->pbits,
774 gctx->generator, gencb);
775 else
19dbb742
SL
776 ret = ossl_dh_generate_ffc_parameters(dh, gctx->gen_type,
777 gctx->pbits, gctx->qbits,
778 gencb);
7165593c
SL
779 if (ret <= 0)
780 goto end;
781 }
782 }
783
784 if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
785 if (ffc->p == NULL || ffc->g == NULL)
786 goto end;
738ee181
SL
787 if (gctx->priv_len > 0)
788 DH_set_length(dh, (long)gctx->priv_len);
5357c106
P
789 ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
790 gctx->gen_type == DH_PARAMGEN_TYPE_FIPS_186_2);
7165593c
SL
791 if (DH_generate_key(dh) <= 0)
792 goto end;
793 }
627c2203
SL
794 DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
795 DH_set_flags(dh, gctx->dh_type);
796
7165593c
SL
797 ret = 1;
798end:
799 if (ret <= 0) {
800 DH_free(dh);
801 dh = NULL;
802 }
803 BN_GENCB_free(gencb);
804 return dh;
805}
806
807static void dh_gen_cleanup(void *genctx)
808{
809 struct dh_gen_ctx *gctx = genctx;
810
811 if (gctx == NULL)
812 return;
813
f2a71518
SL
814 OPENSSL_free(gctx->mdname);
815 OPENSSL_free(gctx->mdprops);
7165593c 816 OPENSSL_clear_free(gctx->seed, gctx->seedlen);
7165593c
SL
817 OPENSSL_free(gctx);
818}
819
4a9fe33c 820static void *dh_load(const void *reference, size_t reference_sz)
7c664b1f
RL
821{
822 DH *dh = NULL;
823
422cbcee 824 if (ossl_prov_is_running() && reference_sz == sizeof(dh)) {
7c664b1f
RL
825 /* The contents of the reference is the address to our object */
826 dh = *(DH **)reference;
827 /* We grabbed, so we detach it */
828 *(DH **)reference = NULL;
829 return dh;
830 }
831 return NULL;
832}
833
b4f447c0 834static void *dh_dup(const void *keydata_from, int selection)
4a9fe33c
TM
835{
836 if (ossl_prov_is_running())
b4f447c0 837 return ossl_dh_dup(keydata_from, selection);
4a9fe33c
TM
838 return NULL;
839}
840
1be63951 841const OSSL_DISPATCH ossl_dh_keymgmt_functions[] = {
8dd5c603 842 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
7165593c
SL
843 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dh_gen_init },
844 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
845 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params },
846 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
847 (void (*)(void))dh_gen_settable_params },
848 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
849 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
7c664b1f 850 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
8dd5c603 851 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
273a67e3
RL
852 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
853 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
6a9bd929
MC
854 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
855 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
8dd5c603 856 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
2888fc15 857 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
a54ff473 858 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
8dd5c603 859 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
273a67e3 860 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
8dd5c603 861 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
273a67e3 862 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
4a9fe33c 863 { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup },
1e6bd31e 864 OSSL_DISPATCH_END
8b84b075 865};
627c2203
SL
866
867/* For any DH key, we use the "DH" algorithms regardless of sub-type. */
868static const char *dhx_query_operation_name(int operation_id)
869{
870 return "DH";
871}
872
1be63951 873const OSSL_DISPATCH ossl_dhx_keymgmt_functions[] = {
627c2203
SL
874 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dhx_newdata },
875 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dhx_gen_init },
876 { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
f1ffaaee 877 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dhx_gen_set_params },
627c2203 878 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
f1ffaaee 879 (void (*)(void))dhx_gen_settable_params },
627c2203
SL
880 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
881 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
882 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
883 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
884 { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
885 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
886 { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
887 { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
888 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
889 { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
890 { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
891 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
892 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
893 { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
894 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
895 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
896 (void (*)(void))dhx_query_operation_name },
4a9fe33c 897 { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup },
1e6bd31e 898 OSSL_DISPATCH_END
627c2203 899};