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