/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 | 0 | if (!ossl_prov_is_running() || key == NULL) |
297 | 0 | return 0; |
298 | | |
299 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
300 | 0 | return 0; |
301 | | |
302 | 0 | include_priv = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0); |
303 | 0 | res = ml_dsa_key_fromdata(key, params, include_priv); |
304 | | #ifdef FIPS_MODULE |
305 | | if (res > 0) { |
306 | | res = ml_dsa_pairwise_test(key); |
307 | | if (!res) { |
308 | | ossl_ml_dsa_key_reset(key); |
309 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT_IMPORT); |
310 | | } |
311 | | } |
312 | | #endif /* FIPS_MODULE */ |
313 | 0 | return res; |
314 | 0 | } |
315 | | |
316 | | static const OSSL_PARAM *ml_dsa_import_types(int selection) |
317 | 0 | { |
318 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
319 | 0 | return NULL; |
320 | 0 | return ml_dsa_import_params_list; |
321 | 0 | } |
322 | | |
323 | | static const OSSL_PARAM *ml_dsa_export_types(int selection) |
324 | 0 | { |
325 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
326 | 0 | return NULL; |
327 | 0 | return ml_dsa_export_params_list; |
328 | 0 | } |
329 | | |
330 | | static const OSSL_PARAM *ml_dsa_gettable_params(void *provctx) |
331 | 0 | { |
332 | 0 | return ml_dsa_get_params_list; |
333 | 0 | } |
334 | | |
335 | | static int ml_dsa_get_params(void *keydata, OSSL_PARAM params[]) |
336 | 0 | { |
337 | 0 | ML_DSA_KEY *key = keydata; |
338 | 0 | const uint8_t *d; |
339 | 0 | size_t len; |
340 | 0 | struct ml_dsa_get_params_st p; |
341 | |
|
342 | 0 | if (key == NULL || !ml_dsa_get_params_decoder(params, &p)) |
343 | 0 | return 0; |
344 | | |
345 | 0 | if (p.bits != NULL |
346 | 0 | && !OSSL_PARAM_set_size_t(p.bits, 8 * ossl_ml_dsa_key_get_pub_len(key))) |
347 | 0 | return 0; |
348 | | |
349 | 0 | if (p.secbits != NULL |
350 | 0 | && !OSSL_PARAM_set_size_t(p.secbits, ossl_ml_dsa_key_get_collision_strength_bits(key))) |
351 | 0 | return 0; |
352 | | |
353 | 0 | if (p.maxsize != NULL |
354 | 0 | && !OSSL_PARAM_set_size_t(p.maxsize, ossl_ml_dsa_key_get_sig_len(key))) |
355 | 0 | return 0; |
356 | | |
357 | 0 | if (p.seccat != NULL |
358 | 0 | && !OSSL_PARAM_set_int(p.seccat, ossl_ml_dsa_key_get_security_category(key))) |
359 | 0 | return 0; |
360 | | |
361 | 0 | if (p.seed != NULL) { |
362 | 0 | d = ossl_ml_dsa_key_get_seed(key); |
363 | 0 | if (d != NULL && !OSSL_PARAM_set_octet_string(p.seed, d, ML_DSA_SEED_BYTES)) |
364 | 0 | return 0; |
365 | 0 | } |
366 | | |
367 | 0 | if (p.privkey != NULL) { |
368 | 0 | d = ossl_ml_dsa_key_get_priv(key); |
369 | 0 | if (d != NULL) { |
370 | 0 | len = ossl_ml_dsa_key_get_priv_len(key); |
371 | 0 | if (!OSSL_PARAM_set_octet_string(p.privkey, d, len)) |
372 | 0 | return 0; |
373 | 0 | } |
374 | 0 | } |
375 | | |
376 | 0 | if (p.pubkey != NULL) { |
377 | 0 | d = ossl_ml_dsa_key_get_pub(key); |
378 | 0 | if (d != NULL) { |
379 | 0 | len = ossl_ml_dsa_key_get_pub_len(key); |
380 | 0 | if (!OSSL_PARAM_set_octet_string(p.pubkey, d, len)) |
381 | 0 | return 0; |
382 | 0 | } |
383 | 0 | } |
384 | | |
385 | | /* |
386 | | * This allows apps to use an empty digest, so that the old API |
387 | | * for digest signing can be used. |
388 | | */ |
389 | 0 | if (p.dgstp != NULL && !OSSL_PARAM_set_utf8_string(p.dgstp, "")) |
390 | 0 | return 0; |
391 | 0 | return 1; |
392 | 0 | } |
393 | | |
394 | | static int ml_dsa_export(void *keydata, int selection, |
395 | | OSSL_CALLBACK *param_cb, void *cbarg) |
396 | 0 | { |
397 | 0 | ML_DSA_KEY *key = keydata; |
398 | 0 | OSSL_PARAM params[4]; |
399 | 0 | const uint8_t *buf; |
400 | 0 | int include_private, pnum = 0; |
401 | |
|
402 | 0 | if (!ossl_prov_is_running() || key == NULL) |
403 | 0 | return 0; |
404 | | |
405 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
406 | 0 | return 0; |
407 | | |
408 | 0 | include_private = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0); |
409 | | |
410 | | /* |
411 | | * Note that if the seed is present, both the seed and the private key are |
412 | | * exported. The recipient will have a choice. |
413 | | */ |
414 | 0 | if (include_private) { |
415 | 0 | if ((buf = ossl_ml_dsa_key_get_seed(key)) != NULL) { |
416 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, (void *)buf, ML_DSA_SEED_BYTES); |
417 | 0 | } |
418 | 0 | if ((buf = ossl_ml_dsa_key_get_priv(key)) != NULL) { |
419 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, (void *)buf, |
420 | 0 | ossl_ml_dsa_key_get_priv_len(key)); |
421 | 0 | } |
422 | 0 | } |
423 | 0 | if (((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
424 | 0 | && ((buf = ossl_ml_dsa_key_get_pub(key)) != NULL)) { |
425 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY, (void *)buf, |
426 | 0 | ossl_ml_dsa_key_get_pub_len(key)); |
427 | 0 | } |
428 | 0 | if (pnum == 0) |
429 | 0 | return 0; |
430 | 0 | params[pnum] = OSSL_PARAM_construct_end(); |
431 | 0 | return param_cb(params, cbarg); |
432 | 0 | } |
433 | | |
434 | | #ifndef FIPS_MODULE |
435 | | static void *ml_dsa_load(const void *reference, size_t reference_sz) |
436 | 0 | { |
437 | 0 | ML_DSA_KEY *key = NULL; |
438 | 0 | const ML_DSA_PARAMS *key_params; |
439 | 0 | const uint8_t *sk, *seed; |
440 | |
|
441 | 0 | if (ossl_prov_is_running() && reference_sz == sizeof(key)) { |
442 | | /* The contents of the reference is the address to our object */ |
443 | 0 | key = *(ML_DSA_KEY **)reference; |
444 | | /* We grabbed, so we detach it */ |
445 | 0 | *(ML_DSA_KEY **)reference = NULL; |
446 | | /* All done, if the pubkey is present. */ |
447 | 0 | if (key == NULL || ossl_ml_dsa_key_get_pub(key) != NULL) |
448 | 0 | return key; |
449 | | /* Handle private prekey inputs. */ |
450 | 0 | sk = ossl_ml_dsa_key_get_priv(key); |
451 | 0 | seed = ossl_ml_dsa_key_get_seed(key); |
452 | 0 | if (seed != NULL |
453 | 0 | && (sk == NULL || (ossl_ml_dsa_key_get_prov_flags(key) & ML_DSA_KEY_PREFER_SEED))) { |
454 | 0 | if (ossl_ml_dsa_generate_key(key)) |
455 | 0 | return key; |
456 | 0 | } else if (sk != NULL) { |
457 | 0 | if (ossl_ml_dsa_sk_decode(key, sk, |
458 | 0 | ossl_ml_dsa_key_get_priv_len(key))) |
459 | 0 | return key; |
460 | 0 | key_params = ossl_ml_dsa_key_params(key); |
461 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
462 | 0 | "error parsing %s private key", |
463 | 0 | key_params->alg); |
464 | 0 | } else { |
465 | 0 | return key; |
466 | 0 | } |
467 | 0 | } |
468 | | |
469 | 0 | ossl_ml_dsa_key_free(key); |
470 | 0 | return NULL; |
471 | 0 | } |
472 | | #endif |
473 | | |
474 | | static void *ml_dsa_gen_init(void *provctx, int selection, |
475 | | const OSSL_PARAM params[]) |
476 | 0 | { |
477 | 0 | struct ml_dsa_gen_ctx *gctx = NULL; |
478 | |
|
479 | 0 | if (!ossl_prov_is_running()) |
480 | 0 | return NULL; |
481 | | |
482 | 0 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
483 | 0 | gctx->provctx = provctx; |
484 | 0 | if (!ml_dsa_gen_set_params(gctx, params)) { |
485 | 0 | OPENSSL_free(gctx); |
486 | 0 | gctx = NULL; |
487 | 0 | } |
488 | 0 | } |
489 | 0 | return gctx; |
490 | 0 | } |
491 | | |
492 | | static void *ml_dsa_gen(void *genctx, int evp_type) |
493 | 0 | { |
494 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
495 | 0 | ML_DSA_KEY *key = NULL; |
496 | |
|
497 | 0 | if (!ossl_prov_is_running()) |
498 | 0 | return NULL; |
499 | 0 | key = ossl_prov_ml_dsa_new(gctx->provctx, gctx->propq, evp_type); |
500 | 0 | if (key == NULL) |
501 | 0 | return NULL; |
502 | 0 | if (gctx->entropy_len != 0 |
503 | 0 | && !ossl_ml_dsa_set_prekey(key, 0, 0, |
504 | 0 | gctx->entropy, gctx->entropy_len, NULL, 0)) |
505 | 0 | goto err; |
506 | 0 | if (!ossl_ml_dsa_generate_key(key)) { |
507 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); |
508 | 0 | goto err; |
509 | 0 | } |
510 | | #ifdef FIPS_MODULE |
511 | | if (!ml_dsa_pairwise_test(key)) { |
512 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); |
513 | | goto err; |
514 | | } |
515 | | #endif |
516 | 0 | return key; |
517 | 0 | err: |
518 | 0 | ossl_ml_dsa_key_free(key); |
519 | 0 | return NULL; |
520 | 0 | } |
521 | | |
522 | | static int ml_dsa_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
523 | 0 | { |
524 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
525 | 0 | struct ml_dsa_gen_set_params_st p; |
526 | |
|
527 | 0 | if (gctx == NULL || !ml_dsa_gen_set_params_decoder(params, &p)) |
528 | 0 | return 0; |
529 | | |
530 | 0 | if (p.seed != NULL) { |
531 | 0 | void *vp = gctx->entropy; |
532 | 0 | size_t len = sizeof(gctx->entropy); |
533 | |
|
534 | 0 | if (!OSSL_PARAM_get_octet_string(p.seed, &vp, len, &(gctx->entropy_len))) { |
535 | 0 | gctx->entropy_len = 0; |
536 | 0 | return 0; |
537 | 0 | } |
538 | 0 | } |
539 | | |
540 | 0 | if (p.propq != NULL) { |
541 | 0 | OPENSSL_free(gctx->propq); |
542 | 0 | gctx->propq = NULL; |
543 | 0 | if (!OSSL_PARAM_get_utf8_string(p.propq, &gctx->propq, 0)) |
544 | 0 | return 0; |
545 | 0 | } |
546 | 0 | return 1; |
547 | 0 | } |
548 | | |
549 | | static const OSSL_PARAM *ml_dsa_gen_settable_params(ossl_unused void *genctx, |
550 | | ossl_unused void *provctx) |
551 | 0 | { |
552 | 0 | return ml_dsa_gen_set_params_list; |
553 | 0 | } |
554 | | |
555 | | static void ml_dsa_gen_cleanup(void *genctx) |
556 | 0 | { |
557 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
558 | |
|
559 | 0 | if (gctx == NULL) |
560 | 0 | return; |
561 | | |
562 | 0 | OPENSSL_cleanse(gctx->entropy, gctx->entropy_len); |
563 | 0 | OPENSSL_free(gctx->propq); |
564 | 0 | OPENSSL_free(gctx); |
565 | 0 | } |
566 | | |
567 | | #ifndef FIPS_MODULE |
568 | | #define DISPATCH_LOAD_FN \ |
569 | | { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC)ml_dsa_load }, |
570 | | #else |
571 | | #define DISPATCH_LOAD_FN /* Non-FIPS only */ |
572 | | #endif |
573 | | |
574 | | #define MAKE_KEYMGMT_FUNCTIONS(alg) \ |
575 | | static OSSL_FUNC_keymgmt_new_fn ml_dsa_##alg##_new_key; \ |
576 | | static OSSL_FUNC_keymgmt_gen_fn ml_dsa_##alg##_gen; \ |
577 | | static void *ml_dsa_##alg##_new_key(void *provctx) \ |
578 | 0 | { \ |
579 | 0 | return ossl_prov_ml_dsa_new(provctx, NULL, EVP_PKEY_ML_DSA_##alg); \ |
580 | 0 | } \ |
581 | | static void *ml_dsa_##alg##_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg) \ |
582 | 0 | { \ |
583 | 0 | return ml_dsa_gen(genctx, EVP_PKEY_ML_DSA_##alg); \ |
584 | 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 |
585 | | const OSSL_DISPATCH ossl_ml_dsa_##alg##_keymgmt_functions[] = { \ |
586 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ml_dsa_##alg##_new_key }, \ |
587 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ml_dsa_free_key }, \ |
588 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ml_dsa_has }, \ |
589 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ml_dsa_match }, \ |
590 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ml_dsa_import }, \ |
591 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ml_dsa_import_types }, \ |
592 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ml_dsa_export }, \ |
593 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ml_dsa_export_types }, \ |
594 | | DISPATCH_LOAD_FN { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))ml_dsa_get_params }, \ |
595 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))ml_dsa_gettable_params }, \ |
596 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ml_dsa_validate }, \ |
597 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ml_dsa_gen_init }, \ |
598 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ml_dsa_##alg##_gen }, \ |
599 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ml_dsa_gen_cleanup }, \ |
600 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, \ |
601 | | (void (*)(void))ml_dsa_gen_set_params }, \ |
602 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, \ |
603 | | (void (*)(void))ml_dsa_gen_settable_params }, \ |
604 | | { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ml_dsa_dup_key }, \ |
605 | | OSSL_DISPATCH_END \ |
606 | | } |
607 | | |
608 | 0 | MAKE_KEYMGMT_FUNCTIONS(44); |
609 | 0 | MAKE_KEYMGMT_FUNCTIONS(65); |
610 | | MAKE_KEYMGMT_FUNCTIONS(87); |