/src/openssl/providers/implementations/keymgmt/ml_dsa_kmgmt.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2025 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 | | #include <openssl/core_dispatch.h> |
11 | | #include <openssl/core_names.h> |
12 | | #include <openssl/evp.h> |
13 | | #include <openssl/param_build.h> |
14 | | #include <openssl/proverr.h> |
15 | | #include <openssl/self_test.h> |
16 | | #include "crypto/ml_dsa.h" |
17 | | #include "internal/fips.h" |
18 | | #include "internal/param_build_set.h" |
19 | | #include "prov/implementations.h" |
20 | | #include "prov/providercommon.h" |
21 | | #include "prov/provider_ctx.h" |
22 | | #include "prov/ml_dsa.h" |
23 | | #include "providers/implementations/keymgmt/ml_dsa_kmgmt.inc" |
24 | | |
25 | | static OSSL_FUNC_keymgmt_free_fn ml_dsa_free_key; |
26 | | static OSSL_FUNC_keymgmt_has_fn ml_dsa_has; |
27 | | static OSSL_FUNC_keymgmt_match_fn ml_dsa_match; |
28 | | static OSSL_FUNC_keymgmt_import_fn ml_dsa_import; |
29 | | static OSSL_FUNC_keymgmt_export_fn ml_dsa_export; |
30 | | static OSSL_FUNC_keymgmt_import_types_fn ml_dsa_imexport_types; |
31 | | static OSSL_FUNC_keymgmt_export_types_fn ml_dsa_imexport_types; |
32 | | static OSSL_FUNC_keymgmt_dup_fn ml_dsa_dup_key; |
33 | | static OSSL_FUNC_keymgmt_gettable_params_fn ml_dsa_gettable_params; |
34 | | static OSSL_FUNC_keymgmt_validate_fn ml_dsa_validate; |
35 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_dsa_gen_init; |
36 | | static OSSL_FUNC_keymgmt_gen_cleanup_fn ml_dsa_gen_cleanup; |
37 | | static OSSL_FUNC_keymgmt_gen_set_params_fn ml_dsa_gen_set_params; |
38 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn ml_dsa_gen_settable_params; |
39 | | #ifndef FIPS_MODULE |
40 | | static OSSL_FUNC_keymgmt_load_fn ml_dsa_load; |
41 | | #endif |
42 | | |
43 | | struct ml_dsa_gen_ctx { |
44 | | PROV_CTX *provctx; |
45 | | char *propq; |
46 | | uint8_t entropy[32]; |
47 | | size_t entropy_len; |
48 | | }; |
49 | | |
50 | | #ifdef FIPS_MODULE |
51 | | static int ml_dsa_pairwise_test(const ML_DSA_KEY *key) |
52 | | { |
53 | | OSSL_SELF_TEST *st = NULL; |
54 | | OSSL_CALLBACK *cb = NULL; |
55 | | OSSL_LIB_CTX *ctx; |
56 | | void *cbarg = NULL; |
57 | | static const uint8_t msg[] = { 80, 108, 117, 103, 104 }; |
58 | | uint8_t rnd[ML_DSA_ENTROPY_LEN]; |
59 | | uint8_t sig[ML_DSA_87_SIG_LEN]; |
60 | | size_t sig_len = 0; |
61 | | int ret = 0; |
62 | | |
63 | | if (!ml_dsa_has(key, OSSL_KEYMGMT_SELECT_KEYPAIR) |
64 | | || ossl_fips_self_testing()) |
65 | | return 1; |
66 | | |
67 | | /* |
68 | | * The functions `OSSL_SELF_TEST_*` will return directly if parameter `st` |
69 | | * is NULL. |
70 | | */ |
71 | | ctx = ossl_ml_dsa_key_get0_libctx(key); |
72 | | OSSL_SELF_TEST_get_callback(ctx, &cb, &cbarg); |
73 | | |
74 | | if ((st = OSSL_SELF_TEST_new(cb, cbarg)) == NULL) |
75 | | return 0; |
76 | | |
77 | | OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT, |
78 | | OSSL_SELF_TEST_DESC_PCT_ML_DSA); |
79 | | |
80 | | memset(rnd, 0, sizeof(rnd)); |
81 | | memset(sig, 0, sizeof(sig)); |
82 | | |
83 | | if (ossl_ml_dsa_sign(key, 0, msg, sizeof(msg), NULL, 0, rnd, sizeof(rnd), 0, |
84 | | sig, &sig_len, sizeof(sig)) <= 0) |
85 | | goto err; |
86 | | |
87 | | OSSL_SELF_TEST_oncorrupt_byte(st, sig); |
88 | | |
89 | | if (ossl_ml_dsa_verify(key, 0, msg, sizeof(msg), NULL, 0, 0, |
90 | | sig, sig_len) <= 0) |
91 | | goto err; |
92 | | |
93 | | ret = 1; |
94 | | err: |
95 | | OSSL_SELF_TEST_onend(st, ret); |
96 | | OSSL_SELF_TEST_free(st); |
97 | | return ret; |
98 | | } |
99 | | #endif |
100 | | |
101 | | ML_DSA_KEY *ossl_prov_ml_dsa_new(PROV_CTX *ctx, const char *propq, int evp_type) |
102 | 0 | { |
103 | 0 | ML_DSA_KEY *key; |
104 | |
|
105 | 0 | if (!ossl_prov_is_running()) |
106 | 0 | return 0; |
107 | | |
108 | 0 | key = ossl_ml_dsa_key_new(PROV_LIBCTX_OF(ctx), propq, evp_type); |
109 | | /* |
110 | | * When decoding, if the key ends up "loaded" into the same provider, these |
111 | | * are the correct config settings, otherwise, new values will be assigned |
112 | | * on import into a different provider. The "load" API does not pass along |
113 | | * the provider context. |
114 | | */ |
115 | 0 | if (key != NULL) { |
116 | 0 | int flags_set = 0, flags_clr = 0; |
117 | |
|
118 | 0 | if (ossl_prov_ctx_get_bool_param( |
119 | 0 | ctx, OSSL_PKEY_PARAM_ML_DSA_RETAIN_SEED, 1)) |
120 | 0 | flags_set |= ML_DSA_KEY_RETAIN_SEED; |
121 | 0 | else |
122 | 0 | flags_clr = ML_DSA_KEY_RETAIN_SEED; |
123 | |
|
124 | 0 | if (ossl_prov_ctx_get_bool_param( |
125 | 0 | ctx, OSSL_PKEY_PARAM_ML_DSA_PREFER_SEED, 1)) |
126 | 0 | flags_set |= ML_DSA_KEY_PREFER_SEED; |
127 | 0 | else |
128 | 0 | flags_clr |= ML_DSA_KEY_PREFER_SEED; |
129 | |
|
130 | 0 | ossl_ml_dsa_set_prekey(key, flags_set, flags_clr, NULL, 0, NULL, 0); |
131 | 0 | } |
132 | 0 | return key; |
133 | 0 | } |
134 | | |
135 | | static void ml_dsa_free_key(void *keydata) |
136 | 0 | { |
137 | 0 | ossl_ml_dsa_key_free((ML_DSA_KEY *)keydata); |
138 | 0 | } |
139 | | |
140 | | static void *ml_dsa_dup_key(const void *keydata_from, int selection) |
141 | 0 | { |
142 | 0 | if (ossl_prov_is_running()) |
143 | 0 | return ossl_ml_dsa_key_dup(keydata_from, selection); |
144 | 0 | return NULL; |
145 | 0 | } |
146 | | |
147 | | static int ml_dsa_has(const void *keydata, int selection) |
148 | 0 | { |
149 | 0 | const ML_DSA_KEY *key = keydata; |
150 | |
|
151 | 0 | if (!ossl_prov_is_running() || key == NULL) |
152 | 0 | return 0; |
153 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
154 | 0 | return 1; /* the selection is not missing */ |
155 | | |
156 | 0 | return ossl_ml_dsa_key_has(key, selection); |
157 | 0 | } |
158 | | |
159 | | static int ml_dsa_match(const void *keydata1, const void *keydata2, int selection) |
160 | 0 | { |
161 | 0 | const ML_DSA_KEY *key1 = keydata1; |
162 | 0 | const ML_DSA_KEY *key2 = keydata2; |
163 | |
|
164 | 0 | if (!ossl_prov_is_running()) |
165 | 0 | return 0; |
166 | 0 | if (key1 == NULL || key2 == NULL) |
167 | 0 | return 0; |
168 | 0 | return ossl_ml_dsa_key_equal(key1, key2, selection); |
169 | 0 | } |
170 | | |
171 | | static int ml_dsa_validate(const void *key_data, int selection, int check_type) |
172 | 0 | { |
173 | 0 | const ML_DSA_KEY *key = key_data; |
174 | |
|
175 | 0 | if (!ml_dsa_has(key, selection)) |
176 | 0 | return 0; |
177 | | |
178 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR) |
179 | 0 | return ossl_ml_dsa_key_pairwise_check(key); |
180 | 0 | return 1; |
181 | 0 | } |
182 | | |
183 | | /** |
184 | | * @brief Load a ML_DSA key from raw data. |
185 | | * |
186 | | * @param key An ML_DSA key to load into |
187 | | * @param params An array of parameters containing key data. |
188 | | * @param include_private Set to 1 to optionally include the private key data |
189 | | * if it exists. |
190 | | * @returns 1 on success, or 0 on failure. |
191 | | */ |
192 | | static int ml_dsa_key_fromdata(ML_DSA_KEY *key, const OSSL_PARAM params[], |
193 | | int include_private) |
194 | 0 | { |
195 | 0 | const ML_DSA_PARAMS *key_params = ossl_ml_dsa_key_params(key); |
196 | 0 | const uint8_t *pk = NULL, *sk = NULL, *seed = NULL; |
197 | 0 | size_t pk_len = 0, sk_len = 0, seed_len = 0; |
198 | 0 | struct ml_dsa_key_type_params_st p; |
199 | |
|
200 | 0 | if (!ml_dsa_key_type_params_decoder(params, &p)) |
201 | 0 | return 0; |
202 | | |
203 | 0 | if (p.pubkey != NULL) { |
204 | 0 | if (!OSSL_PARAM_get_octet_string_ptr(p.pubkey, (const void **)&pk, &pk_len)) |
205 | 0 | return 0; |
206 | 0 | if (pk != NULL && pk_len != key_params->pk_len) { |
207 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH, |
208 | 0 | "Invalid %s public key length", key_params->alg); |
209 | 0 | return 0; |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | /* Private key seed is optional */ |
214 | 0 | if (p.seed != NULL && include_private) { |
215 | 0 | if (!OSSL_PARAM_get_octet_string_ptr(p.seed, (const void **)&seed, |
216 | 0 | &seed_len)) |
217 | 0 | return 0; |
218 | 0 | if (seed != NULL && seed_len != ML_DSA_SEED_BYTES) { |
219 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 0 | } |
223 | | |
224 | | /* Private key is optional */ |
225 | 0 | if (p.privkey != NULL && include_private) { |
226 | 0 | if (!OSSL_PARAM_get_octet_string_ptr(p.privkey, (const void **)&sk, |
227 | 0 | &sk_len)) |
228 | 0 | return 0; |
229 | 0 | if (sk != NULL && sk_len != key_params->sk_len) { |
230 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH, |
231 | 0 | "Invalid %s private key length", |
232 | 0 | key_params->alg); |
233 | 0 | return 0; |
234 | 0 | } |
235 | 0 | } |
236 | | |
237 | | /* The caller MUST specify at least one of seed, private or public keys. */ |
238 | 0 | if (seed_len == 0 && pk_len == 0 && sk_len == 0) { |
239 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
240 | 0 | return 0; |
241 | 0 | } |
242 | | |
243 | 0 | if (seed_len != 0 |
244 | 0 | && (sk_len == 0 |
245 | 0 | || (ossl_ml_dsa_key_get_prov_flags(key) & ML_DSA_KEY_PREFER_SEED))) { |
246 | 0 | if (!ossl_ml_dsa_set_prekey(key, 0, 0, seed, seed_len, sk, sk_len)) |
247 | 0 | return 0; |
248 | 0 | if (!ossl_ml_dsa_generate_key(key)) { |
249 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); |
250 | 0 | return 0; |
251 | 0 | } |
252 | 0 | } else if (sk_len > 0) { |
253 | 0 | if (!ossl_ml_dsa_sk_decode(key, sk, sk_len)) |
254 | 0 | return 0; |
255 | 0 | } else if (pk_len > 0) { |
256 | 0 | if (!ossl_ml_dsa_pk_decode(key, pk, pk_len)) |
257 | 0 | return 0; |
258 | 0 | } |
259 | | |
260 | | /* Error if the supplied public key does not match the generated key */ |
261 | 0 | if (pk_len == 0 |
262 | 0 | || seed_len + sk_len == 0 |
263 | 0 | || memcmp(ossl_ml_dsa_key_get_pub(key), pk, pk_len) == 0) |
264 | 0 | return 1; |
265 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
266 | 0 | "explicit %s public key does not match private", |
267 | 0 | key_params->alg); |
268 | 0 | ossl_ml_dsa_key_reset(key); |
269 | 0 | return 0; |
270 | 0 | } |
271 | | |
272 | | static int ml_dsa_import(void *keydata, int selection, const OSSL_PARAM params[]) |
273 | 0 | { |
274 | 0 | ML_DSA_KEY *key = keydata; |
275 | 0 | int include_priv; |
276 | 0 | int res; |
277 | |
|
278 | 0 | if (!ossl_prov_is_running() || key == NULL) |
279 | 0 | return 0; |
280 | | |
281 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
282 | 0 | return 0; |
283 | | |
284 | 0 | include_priv = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0); |
285 | 0 | res = ml_dsa_key_fromdata(key, params, include_priv); |
286 | | #ifdef FIPS_MODULE |
287 | | if (res > 0) { |
288 | | res = ml_dsa_pairwise_test(key); |
289 | | if (!res) { |
290 | | ossl_ml_dsa_key_reset(key); |
291 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT_IMPORT); |
292 | | } |
293 | | } |
294 | | #endif /* FIPS_MODULE */ |
295 | 0 | return res; |
296 | 0 | } |
297 | | |
298 | | static const OSSL_PARAM *ml_dsa_imexport_types(int selection) |
299 | 0 | { |
300 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
301 | 0 | return NULL; |
302 | 0 | return ml_dsa_key_type_params_list; |
303 | 0 | } |
304 | | |
305 | | static const OSSL_PARAM *ml_dsa_gettable_params(void *provctx) |
306 | 0 | { |
307 | 0 | return ml_dsa_get_params_list; |
308 | 0 | } |
309 | | |
310 | | static int ml_dsa_get_params(void *keydata, OSSL_PARAM params[]) |
311 | 0 | { |
312 | 0 | ML_DSA_KEY *key = keydata; |
313 | 0 | const uint8_t *d; |
314 | 0 | size_t len; |
315 | 0 | struct ml_dsa_get_params_st p; |
316 | |
|
317 | 0 | if (key == NULL || !ml_dsa_get_params_decoder(params, &p)) |
318 | 0 | return 0; |
319 | | |
320 | 0 | if (p.bits != NULL |
321 | 0 | && !OSSL_PARAM_set_size_t(p.bits, 8 * ossl_ml_dsa_key_get_pub_len(key))) |
322 | 0 | return 0; |
323 | | |
324 | 0 | if (p.secbits != NULL |
325 | 0 | && !OSSL_PARAM_set_size_t(p.secbits, ossl_ml_dsa_key_get_collision_strength_bits(key))) |
326 | 0 | return 0; |
327 | | |
328 | 0 | if (p.maxsize != NULL |
329 | 0 | && !OSSL_PARAM_set_size_t(p.maxsize, ossl_ml_dsa_key_get_sig_len(key))) |
330 | 0 | return 0; |
331 | | |
332 | 0 | if (p.seccat != NULL |
333 | 0 | && !OSSL_PARAM_set_int(p.seccat, ossl_ml_dsa_key_get_security_category(key))) |
334 | 0 | return 0; |
335 | | |
336 | 0 | if (p.seed != NULL) { |
337 | 0 | d = ossl_ml_dsa_key_get_seed(key); |
338 | 0 | if (d != NULL && !OSSL_PARAM_set_octet_string(p.seed, d, |
339 | 0 | ML_DSA_SEED_BYTES)) |
340 | 0 | return 0; |
341 | 0 | } |
342 | | |
343 | 0 | if (p.privkey != NULL) { |
344 | 0 | d = ossl_ml_dsa_key_get_priv(key); |
345 | 0 | if (d != NULL) { |
346 | 0 | len = ossl_ml_dsa_key_get_priv_len(key); |
347 | 0 | if (!OSSL_PARAM_set_octet_string(p.privkey, d, len)) |
348 | 0 | return 0; |
349 | 0 | } |
350 | 0 | } |
351 | | |
352 | 0 | if (p.pubkey != NULL) { |
353 | 0 | d = ossl_ml_dsa_key_get_pub(key); |
354 | 0 | if (d != NULL) { |
355 | 0 | len = ossl_ml_dsa_key_get_pub_len(key); |
356 | 0 | if (!OSSL_PARAM_set_octet_string(p.pubkey, d, len)) |
357 | 0 | return 0; |
358 | 0 | } |
359 | 0 | } |
360 | | |
361 | | /* |
362 | | * This allows apps to use an empty digest, so that the old API |
363 | | * for digest signing can be used. |
364 | | */ |
365 | 0 | if (p.dgstp != NULL && !OSSL_PARAM_set_utf8_string(p.dgstp, "")) |
366 | 0 | return 0; |
367 | 0 | return 1; |
368 | 0 | } |
369 | | |
370 | | static int ml_dsa_export(void *keydata, int selection, |
371 | | OSSL_CALLBACK *param_cb, void *cbarg) |
372 | 0 | { |
373 | 0 | ML_DSA_KEY *key = keydata; |
374 | 0 | OSSL_PARAM params[4]; |
375 | 0 | const uint8_t *buf; |
376 | 0 | int include_private, pnum = 0; |
377 | |
|
378 | 0 | if (!ossl_prov_is_running() || key == NULL) |
379 | 0 | return 0; |
380 | | |
381 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
382 | 0 | return 0; |
383 | | |
384 | 0 | include_private = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0); |
385 | | |
386 | | /* |
387 | | * Note that if the seed is present, both the seed and the private key are |
388 | | * exported. The recipient will have a choice. |
389 | | */ |
390 | 0 | if (include_private) { |
391 | 0 | if ((buf = ossl_ml_dsa_key_get_seed(key)) != NULL) { |
392 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string |
393 | 0 | (OSSL_PKEY_PARAM_ML_DSA_SEED, (void *)buf, ML_DSA_SEED_BYTES); |
394 | 0 | } |
395 | 0 | if ((buf = ossl_ml_dsa_key_get_priv(key)) != NULL) { |
396 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string |
397 | 0 | (OSSL_PKEY_PARAM_PRIV_KEY, (void *)buf, |
398 | 0 | ossl_ml_dsa_key_get_priv_len(key)); |
399 | 0 | } |
400 | 0 | } |
401 | 0 | if (((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
402 | 0 | && ((buf = ossl_ml_dsa_key_get_pub(key)) != NULL)) { |
403 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string |
404 | 0 | (OSSL_PKEY_PARAM_PUB_KEY, (void *)buf, |
405 | 0 | ossl_ml_dsa_key_get_pub_len(key)); |
406 | 0 | } |
407 | 0 | if (pnum == 0) |
408 | 0 | return 0; |
409 | 0 | params[pnum] = OSSL_PARAM_construct_end(); |
410 | 0 | return param_cb(params, cbarg); |
411 | 0 | } |
412 | | |
413 | | #ifndef FIPS_MODULE |
414 | | static void *ml_dsa_load(const void *reference, size_t reference_sz) |
415 | 0 | { |
416 | 0 | ML_DSA_KEY *key = NULL; |
417 | 0 | const ML_DSA_PARAMS *key_params; |
418 | 0 | const uint8_t *sk, *seed; |
419 | |
|
420 | 0 | if (ossl_prov_is_running() && reference_sz == sizeof(key)) { |
421 | | /* The contents of the reference is the address to our object */ |
422 | 0 | key = *(ML_DSA_KEY **)reference; |
423 | | /* We grabbed, so we detach it */ |
424 | 0 | *(ML_DSA_KEY **)reference = NULL; |
425 | | /* All done, if the pubkey is present. */ |
426 | 0 | if (key == NULL || ossl_ml_dsa_key_get_pub(key) != NULL) |
427 | 0 | return key; |
428 | | /* Handle private prekey inputs. */ |
429 | 0 | sk = ossl_ml_dsa_key_get_priv(key); |
430 | 0 | seed = ossl_ml_dsa_key_get_seed(key); |
431 | 0 | if (seed != NULL |
432 | 0 | && (sk == NULL || (ossl_ml_dsa_key_get_prov_flags(key) |
433 | 0 | & ML_DSA_KEY_PREFER_SEED))) { |
434 | 0 | if (ossl_ml_dsa_generate_key(key)) |
435 | 0 | return key; |
436 | 0 | } else if (sk != NULL) { |
437 | 0 | if (ossl_ml_dsa_sk_decode(key, sk, |
438 | 0 | ossl_ml_dsa_key_get_priv_len(key))) |
439 | 0 | return key; |
440 | 0 | key_params = ossl_ml_dsa_key_params(key); |
441 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
442 | 0 | "error parsing %s private key", |
443 | 0 | key_params->alg); |
444 | 0 | } else { |
445 | 0 | return key; |
446 | 0 | } |
447 | 0 | } |
448 | | |
449 | 0 | ossl_ml_dsa_key_free(key); |
450 | 0 | return NULL; |
451 | 0 | } |
452 | | #endif |
453 | | |
454 | | static void *ml_dsa_gen_init(void *provctx, int selection, |
455 | | const OSSL_PARAM params[]) |
456 | 0 | { |
457 | 0 | struct ml_dsa_gen_ctx *gctx = NULL; |
458 | |
|
459 | 0 | if (!ossl_prov_is_running()) |
460 | 0 | return NULL; |
461 | | |
462 | 0 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
463 | 0 | gctx->provctx = provctx; |
464 | 0 | if (!ml_dsa_gen_set_params(gctx, params)) { |
465 | 0 | OPENSSL_free(gctx); |
466 | 0 | gctx = NULL; |
467 | 0 | } |
468 | 0 | } |
469 | 0 | return gctx; |
470 | 0 | } |
471 | | |
472 | | static void *ml_dsa_gen(void *genctx, int evp_type) |
473 | 0 | { |
474 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
475 | 0 | ML_DSA_KEY *key = NULL; |
476 | |
|
477 | 0 | if (!ossl_prov_is_running()) |
478 | 0 | return NULL; |
479 | 0 | key = ossl_prov_ml_dsa_new(gctx->provctx, gctx->propq, evp_type); |
480 | 0 | if (key == NULL) |
481 | 0 | return NULL; |
482 | 0 | if (gctx->entropy_len != 0 |
483 | 0 | && !ossl_ml_dsa_set_prekey(key, 0, 0, |
484 | 0 | gctx->entropy, gctx->entropy_len, NULL, 0)) |
485 | 0 | goto err; |
486 | 0 | if (!ossl_ml_dsa_generate_key(key)) { |
487 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); |
488 | 0 | goto err; |
489 | 0 | } |
490 | | #ifdef FIPS_MODULE |
491 | | if (!ml_dsa_pairwise_test(key)) { |
492 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); |
493 | | goto err; |
494 | | } |
495 | | #endif |
496 | 0 | return key; |
497 | 0 | err: |
498 | 0 | ossl_ml_dsa_key_free(key); |
499 | 0 | return NULL; |
500 | 0 | } |
501 | | |
502 | | static int ml_dsa_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
503 | 0 | { |
504 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
505 | 0 | struct ml_dsa_gen_set_params_st p; |
506 | |
|
507 | 0 | if (gctx == NULL || !ml_dsa_gen_set_params_decoder(params, &p)) |
508 | 0 | return 0; |
509 | | |
510 | 0 | if (p.seed != NULL) { |
511 | 0 | void *vp = gctx->entropy; |
512 | 0 | size_t len = sizeof(gctx->entropy); |
513 | |
|
514 | 0 | if (!OSSL_PARAM_get_octet_string(p.seed, &vp, len, &(gctx->entropy_len))) { |
515 | 0 | gctx->entropy_len = 0; |
516 | 0 | return 0; |
517 | 0 | } |
518 | 0 | } |
519 | | |
520 | 0 | if (p.propq != NULL) { |
521 | 0 | OPENSSL_free(gctx->propq); |
522 | 0 | gctx->propq = NULL; |
523 | 0 | if (!OSSL_PARAM_get_utf8_string(p.propq, &gctx->propq, 0)) |
524 | 0 | return 0; |
525 | 0 | } |
526 | 0 | return 1; |
527 | 0 | } |
528 | | |
529 | | static const OSSL_PARAM *ml_dsa_gen_settable_params(ossl_unused void *genctx, |
530 | | ossl_unused void *provctx) |
531 | 0 | { |
532 | 0 | return ml_dsa_gen_set_params_list; |
533 | 0 | } |
534 | | |
535 | | static void ml_dsa_gen_cleanup(void *genctx) |
536 | 0 | { |
537 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
538 | |
|
539 | 0 | if (gctx == NULL) |
540 | 0 | return; |
541 | | |
542 | 0 | OPENSSL_cleanse(gctx->entropy, gctx->entropy_len); |
543 | 0 | OPENSSL_free(gctx->propq); |
544 | 0 | OPENSSL_free(gctx); |
545 | 0 | } |
546 | | |
547 | | #ifndef FIPS_MODULE |
548 | | # define DISPATCH_LOAD_FN \ |
549 | | { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC) ml_dsa_load }, |
550 | | #else |
551 | | # define DISPATCH_LOAD_FN /* Non-FIPS only */ |
552 | | #endif |
553 | | |
554 | | #define MAKE_KEYMGMT_FUNCTIONS(alg) \ |
555 | | static OSSL_FUNC_keymgmt_new_fn ml_dsa_##alg##_new_key; \ |
556 | | static OSSL_FUNC_keymgmt_gen_fn ml_dsa_##alg##_gen; \ |
557 | | static void *ml_dsa_##alg##_new_key(void *provctx) \ |
558 | 0 | { \ |
559 | 0 | return ossl_prov_ml_dsa_new(provctx, NULL, EVP_PKEY_ML_DSA_##alg); \ |
560 | 0 | } \ |
561 | | static void *ml_dsa_##alg##_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)\ |
562 | 0 | { \ |
563 | 0 | return ml_dsa_gen(genctx, EVP_PKEY_ML_DSA_##alg); \ |
564 | 0 | } \ Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_44_gen Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_65_gen Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_87_gen |
565 | | const OSSL_DISPATCH ossl_ml_dsa_##alg##_keymgmt_functions[] = { \ |
566 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ml_dsa_##alg##_new_key }, \ |
567 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ml_dsa_free_key }, \ |
568 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ml_dsa_has }, \ |
569 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ml_dsa_match }, \ |
570 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ml_dsa_import }, \ |
571 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ml_dsa_imexport_types },\ |
572 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ml_dsa_export }, \ |
573 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ml_dsa_imexport_types },\ |
574 | | DISPATCH_LOAD_FN \ |
575 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ml_dsa_get_params }, \ |
576 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ml_dsa_gettable_params },\ |
577 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ml_dsa_validate }, \ |
578 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ml_dsa_gen_init }, \ |
579 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ml_dsa_##alg##_gen }, \ |
580 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ml_dsa_gen_cleanup }, \ |
581 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, \ |
582 | | (void (*)(void))ml_dsa_gen_set_params }, \ |
583 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, \ |
584 | | (void (*)(void))ml_dsa_gen_settable_params }, \ |
585 | | { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ml_dsa_dup_key }, \ |
586 | | OSSL_DISPATCH_END \ |
587 | | } |
588 | | |
589 | 0 | MAKE_KEYMGMT_FUNCTIONS(44); |
590 | 0 | MAKE_KEYMGMT_FUNCTIONS(65); |
591 | | MAKE_KEYMGMT_FUNCTIONS(87); |