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