/src/openssl/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 | | |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/evp.h> |
14 | | #include <openssl/param_build.h> |
15 | | #include <openssl/proverr.h> |
16 | | #include <openssl/self_test.h> |
17 | | #include "crypto/ml_dsa.h" |
18 | | #include "internal/fips.h" |
19 | | #include "internal/param_build_set.h" |
20 | | #include "prov/implementations.h" |
21 | | #include "prov/providercommon.h" |
22 | | #include "prov/provider_ctx.h" |
23 | | #include "prov/ml_dsa.h" |
24 | | |
25 | | static OSSL_FUNC_keymgmt_free_fn ml_dsa_free_key; |
26 | | static OSSL_FUNC_keymgmt_has_fn ml_dsa_has; |
27 | | static OSSL_FUNC_keymgmt_match_fn ml_dsa_match; |
28 | | static OSSL_FUNC_keymgmt_import_fn ml_dsa_import; |
29 | | static OSSL_FUNC_keymgmt_export_fn ml_dsa_export; |
30 | | static OSSL_FUNC_keymgmt_import_types_fn ml_dsa_imexport_types; |
31 | | static OSSL_FUNC_keymgmt_export_types_fn ml_dsa_imexport_types; |
32 | | static OSSL_FUNC_keymgmt_dup_fn ml_dsa_dup_key; |
33 | | static OSSL_FUNC_keymgmt_gettable_params_fn ml_dsa_gettable_params; |
34 | | static OSSL_FUNC_keymgmt_validate_fn ml_dsa_validate; |
35 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_dsa_gen_init; |
36 | | static OSSL_FUNC_keymgmt_gen_cleanup_fn ml_dsa_gen_cleanup; |
37 | | static OSSL_FUNC_keymgmt_gen_set_params_fn ml_dsa_gen_set_params; |
38 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn ml_dsa_gen_settable_params; |
39 | | #ifndef FIPS_MODULE |
40 | | static OSSL_FUNC_keymgmt_load_fn ml_dsa_load; |
41 | | #endif |
42 | | |
43 | | struct ml_dsa_gen_ctx { |
44 | | PROV_CTX *provctx; |
45 | | char *propq; |
46 | | uint8_t entropy[32]; |
47 | | size_t entropy_len; |
48 | | }; |
49 | | |
50 | | #ifdef FIPS_MODULE |
51 | | static int ml_dsa_pairwise_test(const ML_DSA_KEY *key) |
52 | | { |
53 | | OSSL_SELF_TEST *st = NULL; |
54 | | OSSL_CALLBACK *cb = NULL; |
55 | | OSSL_LIB_CTX *ctx; |
56 | | void *cbarg = NULL; |
57 | | static const uint8_t msg[] = { 80, 108, 117, 103, 104 }; |
58 | | uint8_t rnd[ML_DSA_ENTROPY_LEN]; |
59 | | uint8_t sig[ML_DSA_87_SIG_LEN]; |
60 | | size_t sig_len = 0; |
61 | | int ret = 0; |
62 | | |
63 | | if (!ml_dsa_has(key, OSSL_KEYMGMT_SELECT_KEYPAIR) |
64 | | || ossl_fips_self_testing()) |
65 | | 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 | 0 | { |
103 | 0 | ML_DSA_KEY *key; |
104 | |
|
105 | 0 | if (!ossl_prov_is_running()) |
106 | 0 | return 0; |
107 | | |
108 | 0 | 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 | 0 | if (key != NULL) { |
116 | 0 | int flags_set = 0, flags_clr = 0; |
117 | |
|
118 | 0 | if (ossl_prov_ctx_get_bool_param( |
119 | 0 | ctx, OSSL_PKEY_PARAM_ML_DSA_RETAIN_SEED, 1)) |
120 | 0 | flags_set |= ML_DSA_KEY_RETAIN_SEED; |
121 | 0 | else |
122 | 0 | flags_clr = ML_DSA_KEY_RETAIN_SEED; |
123 | |
|
124 | 0 | if (ossl_prov_ctx_get_bool_param( |
125 | 0 | ctx, OSSL_PKEY_PARAM_ML_DSA_PREFER_SEED, 1)) |
126 | 0 | flags_set |= ML_DSA_KEY_PREFER_SEED; |
127 | 0 | else |
128 | 0 | flags_clr |= ML_DSA_KEY_PREFER_SEED; |
129 | |
|
130 | 0 | ossl_ml_dsa_set_prekey(key, flags_set, flags_clr, NULL, 0, NULL, 0); |
131 | 0 | } |
132 | 0 | return key; |
133 | 0 | } |
134 | | |
135 | | static void ml_dsa_free_key(void *keydata) |
136 | 0 | { |
137 | 0 | ossl_ml_dsa_key_free((ML_DSA_KEY *)keydata); |
138 | 0 | } |
139 | | |
140 | | static void *ml_dsa_dup_key(const void *keydata_from, int selection) |
141 | 0 | { |
142 | 0 | if (ossl_prov_is_running()) |
143 | 0 | return ossl_ml_dsa_key_dup(keydata_from, selection); |
144 | 0 | return NULL; |
145 | 0 | } |
146 | | |
147 | | static int ml_dsa_has(const void *keydata, int selection) |
148 | 0 | { |
149 | 0 | const ML_DSA_KEY *key = keydata; |
150 | |
|
151 | 0 | if (!ossl_prov_is_running() || key == NULL) |
152 | 0 | return 0; |
153 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
154 | 0 | return 1; /* the selection is not missing */ |
155 | | |
156 | 0 | return ossl_ml_dsa_key_has(key, selection); |
157 | 0 | } |
158 | | |
159 | | static int ml_dsa_match(const void *keydata1, const void *keydata2, int selection) |
160 | 0 | { |
161 | 0 | const ML_DSA_KEY *key1 = keydata1; |
162 | 0 | const ML_DSA_KEY *key2 = keydata2; |
163 | |
|
164 | 0 | if (!ossl_prov_is_running()) |
165 | 0 | return 0; |
166 | 0 | if (key1 == NULL || key2 == NULL) |
167 | 0 | return 0; |
168 | 0 | return ossl_ml_dsa_key_equal(key1, key2, selection); |
169 | 0 | } |
170 | | |
171 | | static int ml_dsa_validate(const void *key_data, int selection, int check_type) |
172 | 0 | { |
173 | 0 | const ML_DSA_KEY *key = key_data; |
174 | |
|
175 | 0 | if (!ml_dsa_has(key, selection)) |
176 | 0 | return 0; |
177 | | |
178 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR) |
179 | 0 | return ossl_ml_dsa_key_pairwise_check(key); |
180 | 0 | return 1; |
181 | 0 | } |
182 | | |
183 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
184 | | #ifndef ml_dsa_key_type_params_list |
185 | | static const OSSL_PARAM ml_dsa_key_type_params_list[] = { |
186 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, NULL, 0), |
187 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), |
188 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), |
189 | | OSSL_PARAM_END |
190 | | }; |
191 | | #endif |
192 | | |
193 | | #ifndef ml_dsa_key_type_params_st |
194 | | struct ml_dsa_key_type_params_st { |
195 | | OSSL_PARAM *privkey; |
196 | | OSSL_PARAM *pubkey; |
197 | | OSSL_PARAM *seed; |
198 | | }; |
199 | | #endif |
200 | | |
201 | | #ifndef ml_dsa_key_type_params_decoder |
202 | 0 | static struct ml_dsa_key_type_params_st ml_dsa_key_type_params_decoder(const OSSL_PARAM params[]) { |
203 | 0 | struct ml_dsa_key_type_params_st r; |
204 | 0 | const OSSL_PARAM *p; |
205 | 0 | const char *s; |
206 | |
|
207 | 0 | memset(&r, 0, sizeof(r)); |
208 | 0 | for (p = params; (s = p->key) != NULL; p++) |
209 | 0 | switch(s[0]) { |
210 | 0 | default: |
211 | 0 | break; |
212 | 0 | case 'p': |
213 | 0 | switch(s[1]) { |
214 | 0 | default: |
215 | 0 | break; |
216 | 0 | case 'r': |
217 | 0 | if (ossl_likely(r.privkey == NULL && strcmp("iv", s + 2) == 0)) |
218 | 0 | r.privkey = (OSSL_PARAM *)p; |
219 | 0 | break; |
220 | 0 | case 'u': |
221 | 0 | if (ossl_likely(r.pubkey == NULL && strcmp("b", s + 2) == 0)) |
222 | 0 | r.pubkey = (OSSL_PARAM *)p; |
223 | 0 | } |
224 | 0 | break; |
225 | 0 | case 's': |
226 | 0 | if (ossl_likely(r.seed == NULL && strcmp("eed", s + 1) == 0)) |
227 | 0 | r.seed = (OSSL_PARAM *)p; |
228 | 0 | } |
229 | 0 | return r; |
230 | 0 | } |
231 | | #endif |
232 | | /* End of machine generated */ |
233 | | |
234 | | /** |
235 | | * @brief Load a ML_DSA key from raw data. |
236 | | * |
237 | | * @param key An ML_DSA key to load into |
238 | | * @param params An array of parameters containing key data. |
239 | | * @param include_private Set to 1 to optionally include the private key data |
240 | | * if it exists. |
241 | | * @returns 1 on success, or 0 on failure. |
242 | | */ |
243 | | static int ml_dsa_key_fromdata(ML_DSA_KEY *key, const OSSL_PARAM params[], |
244 | | int include_private) |
245 | 0 | { |
246 | 0 | const ML_DSA_PARAMS *key_params = ossl_ml_dsa_key_params(key); |
247 | 0 | const uint8_t *pk = NULL, *sk = NULL, *seed = NULL; |
248 | 0 | size_t pk_len = 0, sk_len = 0, seed_len = 0; |
249 | 0 | struct ml_dsa_key_type_params_st p = ml_dsa_key_type_params_decoder(params); |
250 | |
|
251 | 0 | if (p.pubkey != NULL) { |
252 | 0 | if (!OSSL_PARAM_get_octet_string_ptr(p.pubkey, (const void **)&pk, &pk_len)) |
253 | 0 | return 0; |
254 | 0 | if (pk != NULL && pk_len != key_params->pk_len) { |
255 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH, |
256 | 0 | "Invalid %s public key length", key_params->alg); |
257 | 0 | return 0; |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | | /* Private key seed is optional */ |
262 | 0 | if (p.seed != NULL && include_private) { |
263 | 0 | if (!OSSL_PARAM_get_octet_string_ptr(p.seed, (const void **)&seed, |
264 | 0 | &seed_len)) |
265 | 0 | return 0; |
266 | 0 | if (seed != NULL && seed_len != ML_DSA_SEED_BYTES) { |
267 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH); |
268 | 0 | return 0; |
269 | 0 | } |
270 | 0 | } |
271 | | |
272 | | /* Private key is optional */ |
273 | 0 | if (p.privkey != NULL && include_private) { |
274 | 0 | if (!OSSL_PARAM_get_octet_string_ptr(p.privkey, (const void **)&sk, |
275 | 0 | &sk_len)) |
276 | 0 | return 0; |
277 | 0 | if (sk != NULL && sk_len != key_params->sk_len) { |
278 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH, |
279 | 0 | "Invalid %s private key length", |
280 | 0 | key_params->alg); |
281 | 0 | return 0; |
282 | 0 | } |
283 | 0 | } |
284 | | |
285 | | /* The caller MUST specify at least one of seed, private or public keys. */ |
286 | 0 | if (seed_len == 0 && pk_len == 0 && sk_len == 0) { |
287 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
288 | 0 | return 0; |
289 | 0 | } |
290 | | |
291 | 0 | if (seed_len != 0 |
292 | 0 | && (sk_len == 0 |
293 | 0 | || (ossl_ml_dsa_key_get_prov_flags(key) & ML_DSA_KEY_PREFER_SEED))) { |
294 | 0 | if (!ossl_ml_dsa_set_prekey(key, 0, 0, seed, seed_len, sk, sk_len)) |
295 | 0 | return 0; |
296 | 0 | if (!ossl_ml_dsa_generate_key(key)) { |
297 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); |
298 | 0 | return 0; |
299 | 0 | } |
300 | 0 | } else if (sk_len > 0) { |
301 | 0 | if (!ossl_ml_dsa_sk_decode(key, sk, sk_len)) |
302 | 0 | return 0; |
303 | 0 | } else if (pk_len > 0) { |
304 | 0 | if (!ossl_ml_dsa_pk_decode(key, pk, pk_len)) |
305 | 0 | return 0; |
306 | 0 | } |
307 | | |
308 | | /* Error if the supplied public key does not match the generated key */ |
309 | 0 | if (pk_len == 0 |
310 | 0 | || seed_len + sk_len == 0 |
311 | 0 | || memcmp(ossl_ml_dsa_key_get_pub(key), pk, pk_len) == 0) |
312 | 0 | return 1; |
313 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
314 | 0 | "explicit %s public key does not match private", |
315 | 0 | key_params->alg); |
316 | 0 | ossl_ml_dsa_key_reset(key); |
317 | 0 | return 0; |
318 | 0 | } |
319 | | |
320 | | static int ml_dsa_import(void *keydata, int selection, const OSSL_PARAM params[]) |
321 | 0 | { |
322 | 0 | ML_DSA_KEY *key = keydata; |
323 | 0 | int include_priv; |
324 | |
|
325 | 0 | if (!ossl_prov_is_running() || key == NULL) |
326 | 0 | return 0; |
327 | | |
328 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
329 | 0 | return 0; |
330 | | |
331 | 0 | include_priv = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0); |
332 | 0 | return ml_dsa_key_fromdata(key, params, include_priv); |
333 | 0 | } |
334 | | |
335 | | static const OSSL_PARAM *ml_dsa_imexport_types(int selection) |
336 | 0 | { |
337 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
338 | 0 | return NULL; |
339 | 0 | return ml_dsa_key_type_params_list; |
340 | 0 | } |
341 | | |
342 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
343 | | #ifndef ml_dsa_get_params_list |
344 | | static const OSSL_PARAM ml_dsa_get_params_list[] = { |
345 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), |
346 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), |
347 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), |
348 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL), |
349 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST, NULL, 0), |
350 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, NULL, 0), |
351 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), |
352 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), |
353 | | OSSL_PARAM_END |
354 | | }; |
355 | | #endif |
356 | | |
357 | | #ifndef ml_dsa_get_params_st |
358 | | struct ml_dsa_get_params_st { |
359 | | OSSL_PARAM *bits; |
360 | | OSSL_PARAM *dgstp; |
361 | | OSSL_PARAM *maxsize; |
362 | | OSSL_PARAM *privkey; |
363 | | OSSL_PARAM *pubkey; |
364 | | OSSL_PARAM *secbits; |
365 | | OSSL_PARAM *seccat; |
366 | | OSSL_PARAM *seed; |
367 | | }; |
368 | | #endif |
369 | | |
370 | | #ifndef ml_dsa_get_params_decoder |
371 | 0 | static struct ml_dsa_get_params_st ml_dsa_get_params_decoder(const OSSL_PARAM params[]) { |
372 | 0 | struct ml_dsa_get_params_st r; |
373 | 0 | const OSSL_PARAM *p; |
374 | 0 | const char *s; |
375 | |
|
376 | 0 | memset(&r, 0, sizeof(r)); |
377 | 0 | for (p = params; (s = p->key) != NULL; p++) |
378 | 0 | switch(s[0]) { |
379 | 0 | default: |
380 | 0 | break; |
381 | 0 | case 'b': |
382 | 0 | if (ossl_likely(r.bits == NULL && strcmp("its", s + 1) == 0)) |
383 | 0 | r.bits = (OSSL_PARAM *)p; |
384 | 0 | break; |
385 | 0 | case 'm': |
386 | 0 | switch(s[1]) { |
387 | 0 | default: |
388 | 0 | break; |
389 | 0 | case 'a': |
390 | 0 | switch(s[2]) { |
391 | 0 | default: |
392 | 0 | break; |
393 | 0 | case 'n': |
394 | 0 | if (ossl_likely(r.dgstp == NULL && strcmp("datory-digest", s + 3) == 0)) |
395 | 0 | r.dgstp = (OSSL_PARAM *)p; |
396 | 0 | break; |
397 | 0 | case 'x': |
398 | 0 | if (ossl_likely(r.maxsize == NULL && strcmp("-size", s + 3) == 0)) |
399 | 0 | r.maxsize = (OSSL_PARAM *)p; |
400 | 0 | } |
401 | 0 | } |
402 | 0 | break; |
403 | 0 | case 'p': |
404 | 0 | switch(s[1]) { |
405 | 0 | default: |
406 | 0 | break; |
407 | 0 | case 'r': |
408 | 0 | if (ossl_likely(r.privkey == NULL && strcmp("iv", s + 2) == 0)) |
409 | 0 | r.privkey = (OSSL_PARAM *)p; |
410 | 0 | break; |
411 | 0 | case 'u': |
412 | 0 | if (ossl_likely(r.pubkey == NULL && strcmp("b", s + 2) == 0)) |
413 | 0 | r.pubkey = (OSSL_PARAM *)p; |
414 | 0 | } |
415 | 0 | break; |
416 | 0 | case 's': |
417 | 0 | switch(s[1]) { |
418 | 0 | default: |
419 | 0 | break; |
420 | 0 | case 'e': |
421 | 0 | switch(s[2]) { |
422 | 0 | default: |
423 | 0 | break; |
424 | 0 | case 'c': |
425 | 0 | switch(s[3]) { |
426 | 0 | default: |
427 | 0 | break; |
428 | 0 | case 'u': |
429 | 0 | switch(s[4]) { |
430 | 0 | default: |
431 | 0 | break; |
432 | 0 | case 'r': |
433 | 0 | switch(s[5]) { |
434 | 0 | default: |
435 | 0 | break; |
436 | 0 | case 'i': |
437 | 0 | switch(s[6]) { |
438 | 0 | default: |
439 | 0 | break; |
440 | 0 | case 't': |
441 | 0 | switch(s[7]) { |
442 | 0 | default: |
443 | 0 | break; |
444 | 0 | case 'y': |
445 | 0 | switch(s[8]) { |
446 | 0 | default: |
447 | 0 | break; |
448 | 0 | case '-': |
449 | 0 | switch(s[9]) { |
450 | 0 | default: |
451 | 0 | break; |
452 | 0 | case 'b': |
453 | 0 | if (ossl_likely(r.secbits == NULL && strcmp("its", s + 10) == 0)) |
454 | 0 | r.secbits = (OSSL_PARAM *)p; |
455 | 0 | break; |
456 | 0 | case 'c': |
457 | 0 | if (ossl_likely(r.seccat == NULL && strcmp("ategory", s + 10) == 0)) |
458 | 0 | r.seccat = (OSSL_PARAM *)p; |
459 | 0 | } |
460 | 0 | } |
461 | 0 | } |
462 | 0 | } |
463 | 0 | } |
464 | 0 | } |
465 | 0 | } |
466 | 0 | break; |
467 | 0 | case 'e': |
468 | 0 | if (ossl_likely(r.seed == NULL && strcmp("d", s + 3) == 0)) |
469 | 0 | r.seed = (OSSL_PARAM *)p; |
470 | 0 | } |
471 | 0 | } |
472 | 0 | } |
473 | 0 | return r; |
474 | 0 | } |
475 | | #endif |
476 | | /* End of machine generated */ |
477 | | |
478 | | static const OSSL_PARAM *ml_dsa_gettable_params(void *provctx) |
479 | 0 | { |
480 | 0 | return ml_dsa_get_params_list; |
481 | 0 | } |
482 | | |
483 | | static int ml_dsa_get_params(void *keydata, OSSL_PARAM params[]) |
484 | 0 | { |
485 | 0 | ML_DSA_KEY *key = keydata; |
486 | 0 | const uint8_t *d; |
487 | 0 | size_t len; |
488 | 0 | struct ml_dsa_get_params_st p = ml_dsa_get_params_decoder(params); |
489 | |
|
490 | 0 | if (p.bits != NULL |
491 | 0 | && !OSSL_PARAM_set_int(p.bits, 8 * ossl_ml_dsa_key_get_pub_len(key))) |
492 | 0 | return 0; |
493 | | |
494 | 0 | if (p.secbits != NULL |
495 | 0 | && !OSSL_PARAM_set_int(p.secbits, ossl_ml_dsa_key_get_collision_strength_bits(key))) |
496 | 0 | return 0; |
497 | | |
498 | 0 | if (p.maxsize != NULL |
499 | 0 | && !OSSL_PARAM_set_int(p.maxsize, ossl_ml_dsa_key_get_sig_len(key))) |
500 | 0 | return 0; |
501 | | |
502 | 0 | if (p.seccat != NULL |
503 | 0 | && !OSSL_PARAM_set_int(p.seccat, ossl_ml_dsa_key_get_security_category(key))) |
504 | 0 | return 0; |
505 | | |
506 | | |
507 | 0 | if (p.seed != NULL) { |
508 | 0 | d = ossl_ml_dsa_key_get_seed(key); |
509 | 0 | if (d != NULL && !OSSL_PARAM_set_octet_string(p.seed, d, |
510 | 0 | ML_DSA_SEED_BYTES)) |
511 | 0 | return 0; |
512 | 0 | } |
513 | | |
514 | 0 | if (p.privkey != NULL) { |
515 | 0 | d = ossl_ml_dsa_key_get_priv(key); |
516 | 0 | if (d != NULL) { |
517 | 0 | len = ossl_ml_dsa_key_get_priv_len(key); |
518 | 0 | if (!OSSL_PARAM_set_octet_string(p.privkey, d, len)) |
519 | 0 | return 0; |
520 | 0 | } |
521 | 0 | } |
522 | | |
523 | 0 | if (p.pubkey != NULL) { |
524 | 0 | d = ossl_ml_dsa_key_get_pub(key); |
525 | 0 | if (d != NULL) { |
526 | 0 | len = ossl_ml_dsa_key_get_pub_len(key); |
527 | 0 | if (!OSSL_PARAM_set_octet_string(p.pubkey, d, len)) |
528 | 0 | return 0; |
529 | 0 | } |
530 | 0 | } |
531 | | |
532 | | /* |
533 | | * This allows apps to use an empty digest, so that the old API |
534 | | * for digest signing can be used. |
535 | | */ |
536 | 0 | if (p.dgstp != NULL && !OSSL_PARAM_set_utf8_string(p.dgstp, "")) |
537 | 0 | return 0; |
538 | 0 | return 1; |
539 | 0 | } |
540 | | |
541 | | static int ml_dsa_export(void *keydata, int selection, |
542 | | OSSL_CALLBACK *param_cb, void *cbarg) |
543 | 0 | { |
544 | 0 | ML_DSA_KEY *key = keydata; |
545 | 0 | OSSL_PARAM params[4]; |
546 | 0 | const uint8_t *buf; |
547 | 0 | int include_private, pnum = 0; |
548 | |
|
549 | 0 | if (!ossl_prov_is_running() || key == NULL) |
550 | 0 | return 0; |
551 | | |
552 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
553 | 0 | return 0; |
554 | | |
555 | 0 | include_private = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0); |
556 | | |
557 | | /* |
558 | | * Note that if the seed is present, both the seed and the private key are |
559 | | * exported. The recipient will have a choice. |
560 | | */ |
561 | 0 | if (include_private) { |
562 | 0 | if ((buf = ossl_ml_dsa_key_get_seed(key)) != NULL) { |
563 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string |
564 | 0 | (OSSL_PKEY_PARAM_ML_DSA_SEED, (void *)buf, ML_DSA_SEED_BYTES); |
565 | 0 | } |
566 | 0 | if ((buf = ossl_ml_dsa_key_get_priv(key)) != NULL) { |
567 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string |
568 | 0 | (OSSL_PKEY_PARAM_PRIV_KEY, (void *)buf, |
569 | 0 | ossl_ml_dsa_key_get_priv_len(key)); |
570 | 0 | } |
571 | 0 | } |
572 | 0 | if (((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) |
573 | 0 | && ((buf = ossl_ml_dsa_key_get_pub(key)) != NULL)) { |
574 | 0 | params[pnum++] = OSSL_PARAM_construct_octet_string |
575 | 0 | (OSSL_PKEY_PARAM_PUB_KEY, (void *)buf, |
576 | 0 | ossl_ml_dsa_key_get_pub_len(key)); |
577 | 0 | } |
578 | 0 | if (pnum == 0) |
579 | 0 | return 0; |
580 | 0 | params[pnum] = OSSL_PARAM_construct_end(); |
581 | 0 | return param_cb(params, cbarg); |
582 | 0 | } |
583 | | |
584 | | #ifndef FIPS_MODULE |
585 | | static void *ml_dsa_load(const void *reference, size_t reference_sz) |
586 | 0 | { |
587 | 0 | ML_DSA_KEY *key = NULL; |
588 | 0 | const ML_DSA_PARAMS *key_params; |
589 | 0 | const uint8_t *sk, *seed; |
590 | |
|
591 | 0 | if (ossl_prov_is_running() && reference_sz == sizeof(key)) { |
592 | | /* The contents of the reference is the address to our object */ |
593 | 0 | key = *(ML_DSA_KEY **)reference; |
594 | | /* We grabbed, so we detach it */ |
595 | 0 | *(ML_DSA_KEY **)reference = NULL; |
596 | | /* All done, if the pubkey is present. */ |
597 | 0 | if (key == NULL || ossl_ml_dsa_key_get_pub(key) != NULL) |
598 | 0 | return key; |
599 | | /* Handle private prekey inputs. */ |
600 | 0 | sk = ossl_ml_dsa_key_get_priv(key); |
601 | 0 | seed = ossl_ml_dsa_key_get_seed(key); |
602 | 0 | if (seed != NULL |
603 | 0 | && (sk == NULL || (ossl_ml_dsa_key_get_prov_flags(key) |
604 | 0 | & ML_DSA_KEY_PREFER_SEED))) { |
605 | 0 | if (ossl_ml_dsa_generate_key(key)) |
606 | 0 | return key; |
607 | 0 | } else if (sk != NULL) { |
608 | 0 | if (ossl_ml_dsa_sk_decode(key, sk, |
609 | 0 | ossl_ml_dsa_key_get_priv_len(key))) |
610 | 0 | return key; |
611 | 0 | key_params = ossl_ml_dsa_key_params(key); |
612 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
613 | 0 | "error parsing %s private key", |
614 | 0 | key_params->alg); |
615 | 0 | } else { |
616 | 0 | return key; |
617 | 0 | } |
618 | 0 | } |
619 | | |
620 | 0 | ossl_ml_dsa_key_free(key); |
621 | 0 | return NULL; |
622 | 0 | } |
623 | | #endif |
624 | | |
625 | | static void *ml_dsa_gen_init(void *provctx, int selection, |
626 | | const OSSL_PARAM params[]) |
627 | 0 | { |
628 | 0 | struct ml_dsa_gen_ctx *gctx = NULL; |
629 | |
|
630 | 0 | if (!ossl_prov_is_running()) |
631 | 0 | return NULL; |
632 | | |
633 | 0 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
634 | 0 | gctx->provctx = provctx; |
635 | 0 | if (!ml_dsa_gen_set_params(gctx, params)) { |
636 | 0 | OPENSSL_free(gctx); |
637 | 0 | gctx = NULL; |
638 | 0 | } |
639 | 0 | } |
640 | 0 | return gctx; |
641 | 0 | } |
642 | | |
643 | | static void *ml_dsa_gen(void *genctx, int evp_type) |
644 | 0 | { |
645 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
646 | 0 | ML_DSA_KEY *key = NULL; |
647 | |
|
648 | 0 | if (!ossl_prov_is_running()) |
649 | 0 | return NULL; |
650 | 0 | key = ossl_prov_ml_dsa_new(gctx->provctx, gctx->propq, evp_type); |
651 | 0 | if (key == NULL) |
652 | 0 | return NULL; |
653 | 0 | if (gctx->entropy_len != 0 |
654 | 0 | && !ossl_ml_dsa_set_prekey(key, 0, 0, |
655 | 0 | gctx->entropy, gctx->entropy_len, NULL, 0)) |
656 | 0 | goto err; |
657 | 0 | if (!ossl_ml_dsa_generate_key(key)) { |
658 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); |
659 | 0 | goto err; |
660 | 0 | } |
661 | | #ifdef FIPS_MODULE |
662 | | if (!ml_dsa_pairwise_test(key)) { |
663 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); |
664 | | goto err; |
665 | | } |
666 | | #endif |
667 | 0 | return key; |
668 | 0 | err: |
669 | 0 | ossl_ml_dsa_key_free(key); |
670 | 0 | return NULL; |
671 | 0 | } |
672 | | |
673 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
674 | | #ifndef ml_dsa_gen_set_params_list |
675 | | static const OSSL_PARAM ml_dsa_gen_set_params_list[] = { |
676 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, NULL, 0), |
677 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0), |
678 | | OSSL_PARAM_END |
679 | | }; |
680 | | #endif |
681 | | |
682 | | #ifndef ml_dsa_gen_set_params_st |
683 | | struct ml_dsa_gen_set_params_st { |
684 | | OSSL_PARAM *propq; |
685 | | OSSL_PARAM *seed; |
686 | | }; |
687 | | #endif |
688 | | |
689 | | #ifndef ml_dsa_gen_set_params_decoder |
690 | 0 | static struct ml_dsa_gen_set_params_st ml_dsa_gen_set_params_decoder(const OSSL_PARAM params[]) { |
691 | 0 | struct ml_dsa_gen_set_params_st r; |
692 | 0 | const OSSL_PARAM *p; |
693 | 0 | const char *s; |
694 | |
|
695 | 0 | memset(&r, 0, sizeof(r)); |
696 | 0 | for (p = params; (s = p->key) != NULL; p++) |
697 | 0 | switch(s[0]) { |
698 | 0 | default: |
699 | 0 | break; |
700 | 0 | case 'p': |
701 | 0 | if (ossl_likely(r.propq == NULL && strcmp("roperties", s + 1) == 0)) |
702 | 0 | r.propq = (OSSL_PARAM *)p; |
703 | 0 | break; |
704 | 0 | case 's': |
705 | 0 | if (ossl_likely(r.seed == NULL && strcmp("eed", s + 1) == 0)) |
706 | 0 | r.seed = (OSSL_PARAM *)p; |
707 | 0 | } |
708 | 0 | return r; |
709 | 0 | } |
710 | | #endif |
711 | | /* End of machine generated */ |
712 | | |
713 | | static int ml_dsa_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
714 | 0 | { |
715 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
716 | 0 | struct ml_dsa_gen_set_params_st p; |
717 | |
|
718 | 0 | if (gctx == NULL) |
719 | 0 | return 0; |
720 | 0 | if (ossl_param_is_empty(params)) |
721 | 0 | return 1; |
722 | | |
723 | 0 | p = ml_dsa_gen_set_params_decoder(params); |
724 | |
|
725 | 0 | if (p.seed != NULL) { |
726 | 0 | void *vp = gctx->entropy; |
727 | 0 | size_t len = sizeof(gctx->entropy); |
728 | |
|
729 | 0 | if (!OSSL_PARAM_get_octet_string(p.seed, &vp, len, &(gctx->entropy_len))) { |
730 | 0 | gctx->entropy_len = 0; |
731 | 0 | return 0; |
732 | 0 | } |
733 | 0 | } |
734 | | |
735 | 0 | if (p.propq != NULL) { |
736 | 0 | OPENSSL_free(gctx->propq); |
737 | 0 | gctx->propq = NULL; |
738 | 0 | if (!OSSL_PARAM_get_utf8_string(p.propq, &gctx->propq, 0)) |
739 | 0 | return 0; |
740 | 0 | } |
741 | 0 | return 1; |
742 | 0 | } |
743 | | |
744 | | static const OSSL_PARAM *ml_dsa_gen_settable_params(ossl_unused void *genctx, |
745 | | ossl_unused void *provctx) |
746 | 0 | { |
747 | 0 | return ml_dsa_gen_set_params_list; |
748 | 0 | } |
749 | | |
750 | | static void ml_dsa_gen_cleanup(void *genctx) |
751 | 0 | { |
752 | 0 | struct ml_dsa_gen_ctx *gctx = genctx; |
753 | |
|
754 | 0 | if (gctx == NULL) |
755 | 0 | return; |
756 | | |
757 | 0 | OPENSSL_cleanse(gctx->entropy, gctx->entropy_len); |
758 | 0 | OPENSSL_free(gctx->propq); |
759 | 0 | OPENSSL_free(gctx); |
760 | 0 | } |
761 | | |
762 | | #ifndef FIPS_MODULE |
763 | | # define DISPATCH_LOAD_FN \ |
764 | | { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC) ml_dsa_load }, |
765 | | #else |
766 | | # define DISPATCH_LOAD_FN /* Non-FIPS only */ |
767 | | #endif |
768 | | |
769 | | #define MAKE_KEYMGMT_FUNCTIONS(alg) \ |
770 | | static OSSL_FUNC_keymgmt_new_fn ml_dsa_##alg##_new_key; \ |
771 | | static OSSL_FUNC_keymgmt_gen_fn ml_dsa_##alg##_gen; \ |
772 | | static void *ml_dsa_##alg##_new_key(void *provctx) \ |
773 | 0 | { \ |
774 | 0 | return ossl_prov_ml_dsa_new(provctx, NULL, EVP_PKEY_ML_DSA_##alg); \ |
775 | 0 | } \ Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_44_new_key Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_65_new_key Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_87_new_key |
776 | | static void *ml_dsa_##alg##_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)\ |
777 | 0 | { \ |
778 | 0 | return ml_dsa_gen(genctx, EVP_PKEY_ML_DSA_##alg); \ |
779 | 0 | } \ Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_44_gen Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_65_gen Unexecuted instantiation: ml_dsa_kmgmt.c:ml_dsa_87_gen |
780 | | const OSSL_DISPATCH ossl_ml_dsa_##alg##_keymgmt_functions[] = { \ |
781 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ml_dsa_##alg##_new_key }, \ |
782 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ml_dsa_free_key }, \ |
783 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ml_dsa_has }, \ |
784 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ml_dsa_match }, \ |
785 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ml_dsa_import }, \ |
786 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ml_dsa_imexport_types },\ |
787 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ml_dsa_export }, \ |
788 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ml_dsa_imexport_types },\ |
789 | | DISPATCH_LOAD_FN \ |
790 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ml_dsa_get_params }, \ |
791 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ml_dsa_gettable_params },\ |
792 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ml_dsa_validate }, \ |
793 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ml_dsa_gen_init }, \ |
794 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ml_dsa_##alg##_gen }, \ |
795 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ml_dsa_gen_cleanup }, \ |
796 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, \ |
797 | | (void (*)(void))ml_dsa_gen_set_params }, \ |
798 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, \ |
799 | | (void (*)(void))ml_dsa_gen_settable_params }, \ |
800 | | { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))ml_dsa_dup_key }, \ |
801 | | OSSL_DISPATCH_END \ |
802 | | } |
803 | | |
804 | | MAKE_KEYMGMT_FUNCTIONS(44); |
805 | | MAKE_KEYMGMT_FUNCTIONS(65); |
806 | | MAKE_KEYMGMT_FUNCTIONS(87); |