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