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