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