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