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