/src/openssl/providers/implementations/keymgmt/ml_kem_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 | | |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/params.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/proverr.h> |
16 | | #include <openssl/provider.h> |
17 | | #include <openssl/rand.h> |
18 | | #include <openssl/self_test.h> |
19 | | #include <openssl/param_build.h> |
20 | | #include <openssl/cms.h> |
21 | | #include "crypto/ml_kem.h" |
22 | | #include "internal/fips.h" |
23 | | #include "internal/param_build_set.h" |
24 | | #include "internal/sizes.h" |
25 | | #include "prov/der_hkdf.h" |
26 | | #include "prov/der_wrap.h" |
27 | | #include "prov/implementations.h" |
28 | | #include "prov/providercommon.h" |
29 | | #include "prov/provider_ctx.h" |
30 | | #include "prov/securitycheck.h" |
31 | | #include "prov/ml_kem.h" |
32 | | |
33 | | static OSSL_FUNC_keymgmt_new_fn ml_kem_512_new; |
34 | | static OSSL_FUNC_keymgmt_new_fn ml_kem_768_new; |
35 | | static OSSL_FUNC_keymgmt_new_fn ml_kem_1024_new; |
36 | | static OSSL_FUNC_keymgmt_gen_fn ml_kem_gen; |
37 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_512_gen_init; |
38 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_768_gen_init; |
39 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_1024_gen_init; |
40 | | static OSSL_FUNC_keymgmt_gen_cleanup_fn ml_kem_gen_cleanup; |
41 | | static OSSL_FUNC_keymgmt_gen_set_params_fn ml_kem_gen_set_params; |
42 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn ml_kem_gen_settable_params; |
43 | | #ifndef FIPS_MODULE |
44 | | static OSSL_FUNC_keymgmt_load_fn ml_kem_load; |
45 | | #endif |
46 | | static OSSL_FUNC_keymgmt_get_params_fn ml_kem_get_params; |
47 | | static OSSL_FUNC_keymgmt_gettable_params_fn ml_kem_gettable_params; |
48 | | static OSSL_FUNC_keymgmt_set_params_fn ml_kem_set_params; |
49 | | static OSSL_FUNC_keymgmt_settable_params_fn ml_kem_settable_params; |
50 | | static OSSL_FUNC_keymgmt_has_fn ml_kem_has; |
51 | | static OSSL_FUNC_keymgmt_match_fn ml_kem_match; |
52 | | static OSSL_FUNC_keymgmt_validate_fn ml_kem_validate; |
53 | | static OSSL_FUNC_keymgmt_import_fn ml_kem_import; |
54 | | static OSSL_FUNC_keymgmt_export_fn ml_kem_export; |
55 | | static OSSL_FUNC_keymgmt_import_types_fn ml_kem_imexport_types; |
56 | | static OSSL_FUNC_keymgmt_export_types_fn ml_kem_imexport_types; |
57 | | static OSSL_FUNC_keymgmt_dup_fn ml_kem_dup; |
58 | | |
59 | | static const int minimal_selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS |
60 | | | OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
61 | | |
62 | | typedef struct ml_kem_gen_ctx_st { |
63 | | PROV_CTX *provctx; |
64 | | char *propq; |
65 | | int selection; |
66 | | int evp_type; |
67 | | uint8_t seedbuf[ML_KEM_SEED_BYTES]; |
68 | | uint8_t *seed; |
69 | | } PROV_ML_KEM_GEN_CTX; |
70 | | |
71 | | static int ml_kem_pairwise_test(const ML_KEM_KEY *key, int key_flags) |
72 | 0 | { |
73 | | #ifdef FIPS_MODULE |
74 | | OSSL_SELF_TEST *st = NULL; |
75 | | OSSL_CALLBACK *cb = NULL; |
76 | | void *cbarg = NULL; |
77 | | #endif |
78 | 0 | unsigned char entropy[ML_KEM_RANDOM_BYTES]; |
79 | 0 | unsigned char secret[ML_KEM_SHARED_SECRET_BYTES]; |
80 | 0 | unsigned char out[ML_KEM_SHARED_SECRET_BYTES]; |
81 | 0 | unsigned char *ctext = NULL; |
82 | 0 | const ML_KEM_VINFO *v = ossl_ml_kem_key_vinfo(key); |
83 | 0 | int operation_result = 0; |
84 | 0 | int ret = 0; |
85 | | |
86 | | /* Unless we have both a public and private key, we can't do the test */ |
87 | 0 | if (!ossl_ml_kem_have_prvkey(key) |
88 | 0 | || !ossl_ml_kem_have_pubkey(key) |
89 | 0 | || (key_flags & ML_KEM_KEY_PCT_TYPE) == 0) |
90 | 0 | return 1; |
91 | | #ifdef FIPS_MODULE |
92 | | /* During self test, it is a waste to do this test */ |
93 | | if (ossl_fips_self_testing()) |
94 | | return 1; |
95 | | |
96 | | /* |
97 | | * The functions `OSSL_SELF_TEST_*` will return directly if parameter `st` |
98 | | * is NULL. |
99 | | */ |
100 | | OSSL_SELF_TEST_get_callback(key->libctx, &cb, &cbarg); |
101 | | |
102 | | st = OSSL_SELF_TEST_new(cb, cbarg); |
103 | | if (st == NULL) |
104 | | return 0; |
105 | | |
106 | | OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT, |
107 | | OSSL_SELF_TEST_DESC_PCT_ML_KEM); |
108 | | #endif /* FIPS_MODULE */ |
109 | | |
110 | 0 | ctext = OPENSSL_malloc(v->ctext_bytes); |
111 | 0 | if (ctext == NULL) |
112 | 0 | goto err; |
113 | | |
114 | 0 | memset(out, 0, sizeof(out)); |
115 | | |
116 | | /* |
117 | | * The pairwise test is skipped unless either RANDOM or FIXED entropy PCTs |
118 | | * are enabled. |
119 | | */ |
120 | 0 | if (key_flags & ML_KEM_KEY_RANDOM_PCT) { |
121 | 0 | operation_result = ossl_ml_kem_encap_rand(ctext, v->ctext_bytes, |
122 | 0 | secret, sizeof(secret), key); |
123 | 0 | } else { |
124 | 0 | memset(entropy, 0125, sizeof(entropy)); |
125 | 0 | operation_result = ossl_ml_kem_encap_seed(ctext, v->ctext_bytes, |
126 | 0 | secret, sizeof(secret), |
127 | 0 | entropy, sizeof(entropy), |
128 | 0 | key); |
129 | 0 | } |
130 | 0 | if (operation_result != 1) |
131 | 0 | goto err; |
132 | | |
133 | | #ifdef FIPS_MODULE |
134 | | OSSL_SELF_TEST_oncorrupt_byte(st, ctext); |
135 | | #endif |
136 | | |
137 | 0 | operation_result = ossl_ml_kem_decap(out, sizeof(out), ctext, v->ctext_bytes, |
138 | 0 | key); |
139 | 0 | if (operation_result != 1 || memcmp(out, secret, sizeof(out)) != 0) |
140 | 0 | goto err; |
141 | | |
142 | 0 | ret = 1; |
143 | 0 | err: |
144 | | #ifdef FIPS_MODULE |
145 | | OSSL_SELF_TEST_onend(st, ret); |
146 | | OSSL_SELF_TEST_free(st); |
147 | | #else |
148 | 0 | if (ret == 0) { |
149 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
150 | 0 | "public part of %s private key fails to match private", |
151 | 0 | v->algorithm_name); |
152 | 0 | } |
153 | 0 | #endif |
154 | 0 | OPENSSL_free(ctext); |
155 | 0 | return ret; |
156 | 0 | } |
157 | | |
158 | | ML_KEM_KEY *ossl_prov_ml_kem_new(PROV_CTX *ctx, const char *propq, int evp_type) |
159 | 0 | { |
160 | 0 | ML_KEM_KEY *key; |
161 | |
|
162 | 0 | if (!ossl_prov_is_running()) |
163 | 0 | return NULL; |
164 | | /* |
165 | | * When decoding, if the key ends up "loaded" into the same provider, these |
166 | | * are the correct config settings, otherwise, new values will be assigned |
167 | | * on import into a different provider. The "load" API does not pass along |
168 | | * the provider context. |
169 | | */ |
170 | 0 | if ((key = ossl_ml_kem_key_new(PROV_LIBCTX_OF(ctx), propq, evp_type)) != NULL) { |
171 | 0 | const char *pct_type = ossl_prov_ctx_get_param( |
172 | 0 | ctx, OSSL_PKEY_PARAM_ML_KEM_IMPORT_PCT_TYPE, "random"); |
173 | |
|
174 | 0 | if (ossl_prov_ctx_get_bool_param( |
175 | 0 | ctx, OSSL_PKEY_PARAM_ML_KEM_RETAIN_SEED, 1)) |
176 | 0 | key->prov_flags |= ML_KEM_KEY_RETAIN_SEED; |
177 | 0 | else |
178 | 0 | key->prov_flags &= ~ML_KEM_KEY_RETAIN_SEED; |
179 | 0 | if (ossl_prov_ctx_get_bool_param( |
180 | 0 | ctx, OSSL_PKEY_PARAM_ML_KEM_PREFER_SEED, 1)) |
181 | 0 | key->prov_flags |= ML_KEM_KEY_PREFER_SEED; |
182 | 0 | else |
183 | 0 | key->prov_flags &= ~ML_KEM_KEY_PREFER_SEED; |
184 | 0 | if (OPENSSL_strcasecmp(pct_type, "random") == 0) |
185 | 0 | key->prov_flags |= ML_KEM_KEY_RANDOM_PCT; |
186 | 0 | else if (OPENSSL_strcasecmp(pct_type, "fixed") == 0) |
187 | 0 | key->prov_flags |= ML_KEM_KEY_FIXED_PCT; |
188 | 0 | else |
189 | 0 | key->prov_flags &= ~ML_KEM_KEY_PCT_TYPE; |
190 | 0 | } |
191 | 0 | return key; |
192 | 0 | } |
193 | | |
194 | | static int ml_kem_has(const void *vkey, int selection) |
195 | 0 | { |
196 | 0 | const ML_KEM_KEY *key = vkey; |
197 | | |
198 | | /* A NULL key MUST fail to have anything */ |
199 | 0 | if (!ossl_prov_is_running() || key == NULL) |
200 | 0 | return 0; |
201 | | |
202 | 0 | switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) { |
203 | 0 | case 0: |
204 | 0 | return 1; |
205 | 0 | case OSSL_KEYMGMT_SELECT_PUBLIC_KEY: |
206 | 0 | return ossl_ml_kem_have_pubkey(key); |
207 | 0 | default: |
208 | 0 | return ossl_ml_kem_have_prvkey(key); |
209 | 0 | } |
210 | 0 | } |
211 | | |
212 | | static int ml_kem_match(const void *vkey1, const void *vkey2, int selection) |
213 | 0 | { |
214 | 0 | const ML_KEM_KEY *key1 = vkey1; |
215 | 0 | const ML_KEM_KEY *key2 = vkey2; |
216 | |
|
217 | 0 | if (!ossl_prov_is_running()) |
218 | 0 | return 0; |
219 | | |
220 | | /* All we have that can be compared is key material */ |
221 | 0 | if (!(selection & OSSL_KEYMGMT_SELECT_KEYPAIR)) |
222 | 0 | return 1; |
223 | | |
224 | 0 | return ossl_ml_kem_pubkey_cmp(key1, key2); |
225 | 0 | } |
226 | | |
227 | | static int ml_kem_validate(const void *vkey, int selection, int check_type) |
228 | 0 | { |
229 | 0 | const ML_KEM_KEY *key = vkey; |
230 | |
|
231 | 0 | if (!ml_kem_has(key, selection)) |
232 | 0 | return 0; |
233 | | |
234 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR) |
235 | 0 | return ml_kem_pairwise_test(key, ML_KEM_KEY_RANDOM_PCT); |
236 | 0 | return 1; |
237 | 0 | } |
238 | | |
239 | | static int ml_kem_export(void *vkey, int selection, OSSL_CALLBACK *param_cb, |
240 | | void *cbarg) |
241 | 0 | { |
242 | 0 | ML_KEM_KEY *key = vkey; |
243 | 0 | OSSL_PARAM_BLD *tmpl = NULL; |
244 | 0 | OSSL_PARAM *params = NULL; |
245 | 0 | const ML_KEM_VINFO *v; |
246 | 0 | uint8_t *pubenc = NULL, *prvenc = NULL, *seedenc = NULL; |
247 | 0 | size_t prvlen = 0, seedlen = 0; |
248 | 0 | int ret = 0; |
249 | |
|
250 | 0 | if (!ossl_prov_is_running() || key == NULL) |
251 | 0 | return 0; |
252 | | |
253 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
254 | 0 | return 0; |
255 | | |
256 | 0 | v = ossl_ml_kem_key_vinfo(key); |
257 | 0 | if (!ossl_ml_kem_have_pubkey(key)) { |
258 | | /* Fail when no key material can be returned */ |
259 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0 |
260 | 0 | || !ossl_ml_kem_decoded_key(key)) { |
261 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
262 | 0 | return 0; |
263 | 0 | } |
264 | 0 | } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) { |
265 | 0 | pubenc = OPENSSL_malloc(v->pubkey_bytes); |
266 | 0 | if (pubenc == NULL |
267 | 0 | || !ossl_ml_kem_encode_public_key(pubenc, v->pubkey_bytes, key)) |
268 | 0 | goto err; |
269 | 0 | } |
270 | | |
271 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
272 | | /* |
273 | | * The seed and/or private key material are allocated on the secure |
274 | | * heap if configured, ossl_param_build_set_octet_string(), will then |
275 | | * also use the secure heap. |
276 | | */ |
277 | 0 | if (ossl_ml_kem_have_seed(key)) { |
278 | 0 | seedlen = ML_KEM_SEED_BYTES; |
279 | 0 | if ((seedenc = OPENSSL_secure_zalloc(seedlen)) == NULL |
280 | 0 | || !ossl_ml_kem_encode_seed(seedenc, seedlen, key)) |
281 | 0 | goto err; |
282 | 0 | } |
283 | 0 | if (ossl_ml_kem_have_prvkey(key)) { |
284 | 0 | prvlen = v->prvkey_bytes; |
285 | 0 | if ((prvenc = OPENSSL_secure_zalloc(prvlen)) == NULL |
286 | 0 | || !ossl_ml_kem_encode_private_key(prvenc, prvlen, key)) |
287 | 0 | goto err; |
288 | 0 | } else if (ossl_ml_kem_have_dkenc(key)) { |
289 | 0 | prvlen = v->prvkey_bytes; |
290 | 0 | if ((prvenc = OPENSSL_secure_zalloc(prvlen)) == NULL) |
291 | 0 | goto err; |
292 | 0 | memcpy(prvenc, key->encoded_dk, prvlen); |
293 | 0 | } |
294 | 0 | } |
295 | | |
296 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
297 | 0 | if (tmpl == NULL) |
298 | 0 | goto err; |
299 | | |
300 | | /* The (d, z) seed, when available and private keys are requested. */ |
301 | 0 | if (seedenc != NULL |
302 | 0 | && !ossl_param_build_set_octet_string( |
303 | 0 | tmpl, params, OSSL_PKEY_PARAM_ML_KEM_SEED, seedenc, seedlen)) |
304 | 0 | goto err; |
305 | | |
306 | | /* The private key in the FIPS 203 |dk| format, when requested. */ |
307 | 0 | if (prvenc != NULL |
308 | 0 | && !ossl_param_build_set_octet_string( |
309 | 0 | tmpl, params, OSSL_PKEY_PARAM_PRIV_KEY, prvenc, prvlen)) |
310 | 0 | goto err; |
311 | | |
312 | | /* The public key on request; it is always available when either is */ |
313 | 0 | if (pubenc != NULL |
314 | 0 | && !ossl_param_build_set_octet_string( |
315 | 0 | tmpl, params, OSSL_PKEY_PARAM_PUB_KEY, pubenc, v->pubkey_bytes)) |
316 | 0 | goto err; |
317 | | |
318 | 0 | params = OSSL_PARAM_BLD_to_param(tmpl); |
319 | 0 | if (params == NULL) |
320 | 0 | goto err; |
321 | | |
322 | 0 | ret = param_cb(params, cbarg); |
323 | 0 | OSSL_PARAM_free(params); |
324 | |
|
325 | 0 | err: |
326 | 0 | OSSL_PARAM_BLD_free(tmpl); |
327 | 0 | OPENSSL_secure_clear_free(seedenc, seedlen); |
328 | 0 | OPENSSL_secure_clear_free(prvenc, prvlen); |
329 | 0 | OPENSSL_free(pubenc); |
330 | 0 | return ret; |
331 | 0 | } |
332 | | |
333 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
334 | | #ifndef ml_kem_key_type_params_list |
335 | | static const OSSL_PARAM ml_kem_key_type_params_list[] = { |
336 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0), |
337 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), |
338 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), |
339 | | OSSL_PARAM_END |
340 | | }; |
341 | | #endif |
342 | | |
343 | | #ifndef ml_kem_key_type_params_st |
344 | | struct ml_kem_key_type_params_st { |
345 | | OSSL_PARAM *privkey; |
346 | | OSSL_PARAM *pubkey; |
347 | | OSSL_PARAM *seed; |
348 | | }; |
349 | | #endif |
350 | | |
351 | | #ifndef ml_kem_key_type_params_decoder |
352 | | static int ml_kem_key_type_params_decoder |
353 | | (const OSSL_PARAM *p, struct ml_kem_key_type_params_st *r) |
354 | 0 | { |
355 | 0 | const char *s; |
356 | |
|
357 | 0 | memset(r, 0, sizeof(*r)); |
358 | 0 | if (p != NULL) |
359 | 0 | for (; (s = p->key) != NULL; p++) |
360 | 0 | switch(s[0]) { |
361 | 0 | default: |
362 | 0 | break; |
363 | 0 | case 'p': |
364 | 0 | switch(s[1]) { |
365 | 0 | default: |
366 | 0 | break; |
367 | 0 | case 'r': |
368 | 0 | if (ossl_likely(strcmp("iv", s + 2) == 0)) { |
369 | | /* OSSL_PKEY_PARAM_PRIV_KEY */ |
370 | 0 | if (ossl_unlikely(r->privkey != NULL)) { |
371 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
372 | 0 | "param %s is repeated", s); |
373 | 0 | return 0; |
374 | 0 | } |
375 | 0 | r->privkey = (OSSL_PARAM *)p; |
376 | 0 | } |
377 | 0 | break; |
378 | 0 | case 'u': |
379 | 0 | if (ossl_likely(strcmp("b", s + 2) == 0)) { |
380 | | /* OSSL_PKEY_PARAM_PUB_KEY */ |
381 | 0 | if (ossl_unlikely(r->pubkey != NULL)) { |
382 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
383 | 0 | "param %s is repeated", s); |
384 | 0 | return 0; |
385 | 0 | } |
386 | 0 | r->pubkey = (OSSL_PARAM *)p; |
387 | 0 | } |
388 | 0 | } |
389 | 0 | break; |
390 | 0 | case 's': |
391 | 0 | if (ossl_likely(strcmp("eed", s + 1) == 0)) { |
392 | | /* OSSL_PKEY_PARAM_ML_KEM_SEED */ |
393 | 0 | if (ossl_unlikely(r->seed != NULL)) { |
394 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
395 | 0 | "param %s is repeated", s); |
396 | 0 | return 0; |
397 | 0 | } |
398 | 0 | r->seed = (OSSL_PARAM *)p; |
399 | 0 | } |
400 | 0 | } |
401 | 0 | return 1; |
402 | 0 | } |
403 | | #endif |
404 | | /* End of machine generated */ |
405 | | |
406 | | static const OSSL_PARAM *ml_kem_imexport_types(int selection) |
407 | 0 | { |
408 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) |
409 | 0 | return ml_kem_key_type_params_list; |
410 | 0 | return NULL; |
411 | 0 | } |
412 | | |
413 | | static int check_seed(const uint8_t *seed, const uint8_t *prvenc, |
414 | | ML_KEM_KEY *key) |
415 | 0 | { |
416 | 0 | size_t zlen = ML_KEM_RANDOM_BYTES; |
417 | |
|
418 | 0 | if (memcmp(seed + ML_KEM_SEED_BYTES - zlen, |
419 | 0 | prvenc + key->vinfo->prvkey_bytes - zlen, zlen) == 0) |
420 | 0 | return 1; |
421 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
422 | 0 | "private %s key implicit rejection secret does" |
423 | 0 | " not match seed", key->vinfo->algorithm_name); |
424 | 0 | return 0; |
425 | 0 | } |
426 | | |
427 | | static int check_prvenc(const uint8_t *prvenc, ML_KEM_KEY *key) |
428 | 0 | { |
429 | 0 | size_t len = key->vinfo->prvkey_bytes; |
430 | 0 | uint8_t *buf = OPENSSL_malloc(len); |
431 | 0 | int ret = 0; |
432 | |
|
433 | 0 | if (buf != NULL |
434 | 0 | && ossl_ml_kem_encode_private_key(buf, len, key)) |
435 | 0 | ret = memcmp(buf, prvenc, len) == 0; |
436 | 0 | OPENSSL_clear_free(buf, len); |
437 | 0 | if (ret) |
438 | 0 | return 1; |
439 | | |
440 | 0 | if (buf != NULL) |
441 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
442 | 0 | "explicit %s private key does not match seed", |
443 | 0 | key->vinfo->algorithm_name); |
444 | 0 | ossl_ml_kem_key_reset(key); |
445 | 0 | return 0; |
446 | 0 | } |
447 | | |
448 | | static int ml_kem_key_fromdata(ML_KEM_KEY *key, |
449 | | const OSSL_PARAM params[], |
450 | | int include_private) |
451 | 0 | { |
452 | 0 | const void *pubenc = NULL, *prvenc = NULL, *seedenc = NULL; |
453 | 0 | size_t publen = 0, prvlen = 0, seedlen = 0, puboff; |
454 | 0 | const ML_KEM_VINFO *v; |
455 | 0 | struct ml_kem_key_type_params_st p; |
456 | | |
457 | | /* Invalid attempt to mutate a key, what is the right error to report? */ |
458 | 0 | if (key == NULL |
459 | 0 | || ossl_ml_kem_have_pubkey(key) |
460 | 0 | || !ml_kem_key_type_params_decoder(params, &p)) |
461 | 0 | return 0; |
462 | 0 | v = ossl_ml_kem_key_vinfo(key); |
463 | | |
464 | | /* |
465 | | * When a private key is provided, without a seed, any public key also |
466 | | * provided will be ignored (apart from length), just as with the seed. |
467 | | */ |
468 | 0 | if (p.seed != NULL && include_private) { |
469 | | /* |
470 | | * When a seed is provided, the private and public keys may be ignored, |
471 | | * after validating just their lengths. Comparing encodings or hashes |
472 | | * when applicable is possible, but not currently implemented. |
473 | | */ |
474 | 0 | if (OSSL_PARAM_get_octet_string_ptr(p.seed, &seedenc, &seedlen) != 1) |
475 | 0 | return 0; |
476 | 0 | if (seedlen != 0 && seedlen != ML_KEM_SEED_BYTES) { |
477 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH); |
478 | 0 | return 0; |
479 | 0 | } |
480 | 0 | } |
481 | | |
482 | 0 | if (p.privkey != NULL && include_private) { |
483 | 0 | if (OSSL_PARAM_get_octet_string_ptr(p.privkey, &prvenc, &prvlen) != 1) |
484 | 0 | return 0; |
485 | 0 | if (prvlen != 0 && prvlen != v->prvkey_bytes) { |
486 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
487 | 0 | return 0; |
488 | 0 | } |
489 | 0 | } |
490 | | |
491 | | /* Used only when no seed or private key is provided. */ |
492 | 0 | if (p.pubkey != NULL) { |
493 | 0 | if (OSSL_PARAM_get_octet_string_ptr(p.pubkey, &pubenc, &publen) != 1) |
494 | 0 | return 0; |
495 | 0 | if (publen != 0 && publen != v->pubkey_bytes) { |
496 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
497 | 0 | return 0; |
498 | 0 | } |
499 | 0 | } |
500 | | |
501 | | /* The caller MUST specify at least one of seed, private or public keys. */ |
502 | 0 | if (seedlen == 0 && publen == 0 && prvlen == 0) { |
503 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
504 | 0 | return 0; |
505 | 0 | } |
506 | | |
507 | | /* Check any explicit public key against embedded value in private key */ |
508 | 0 | if (publen > 0 && prvlen > 0) { |
509 | | /* point to the ek offset in dk = DKpke||ek||H(ek)||z */ |
510 | 0 | puboff = prvlen - ML_KEM_RANDOM_BYTES - ML_KEM_PKHASH_BYTES - publen; |
511 | 0 | if (memcmp(pubenc, (unsigned char *)prvenc + puboff, publen) != 0) { |
512 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
513 | 0 | "explicit %s public key does not match private", |
514 | 0 | v->algorithm_name); |
515 | 0 | return 0; |
516 | 0 | } |
517 | 0 | } |
518 | | |
519 | 0 | if (seedlen != 0 |
520 | 0 | && (prvlen == 0 || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) { |
521 | 0 | if (prvlen != 0 && !check_seed(seedenc, prvenc, key)) |
522 | 0 | return 0; |
523 | 0 | if (!ossl_ml_kem_set_seed(seedenc, seedlen, key) |
524 | 0 | || !ossl_ml_kem_genkey(NULL, 0, key)) |
525 | 0 | return 0; |
526 | 0 | return prvlen == 0 || check_prvenc(prvenc, key); |
527 | 0 | } else if (prvlen != 0) { |
528 | 0 | return ossl_ml_kem_parse_private_key(prvenc, prvlen, key); |
529 | 0 | } |
530 | 0 | return ossl_ml_kem_parse_public_key(pubenc, publen, key); |
531 | 0 | } |
532 | | |
533 | | static int ml_kem_import(void *vkey, int selection, const OSSL_PARAM params[]) |
534 | 0 | { |
535 | 0 | ML_KEM_KEY *key = vkey; |
536 | 0 | int include_private; |
537 | 0 | int res; |
538 | |
|
539 | 0 | if (!ossl_prov_is_running() || key == NULL) |
540 | 0 | return 0; |
541 | | |
542 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
543 | 0 | return 0; |
544 | | |
545 | 0 | include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0; |
546 | 0 | res = ml_kem_key_fromdata(key, params, include_private); |
547 | 0 | if (res > 0 && include_private |
548 | 0 | && !ml_kem_pairwise_test(key, key->prov_flags)) { |
549 | | #ifdef FIPS_MODULE |
550 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT_IMPORT); |
551 | | #endif |
552 | 0 | ossl_ml_kem_key_reset(key); |
553 | 0 | res = 0; |
554 | 0 | } |
555 | 0 | return res; |
556 | 0 | } |
557 | | |
558 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
559 | | #ifndef ml_kem_get_params_list |
560 | | static const OSSL_PARAM ml_kem_get_params_list[] = { |
561 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL), |
562 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL), |
563 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL), |
564 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_CATEGORY, NULL), |
565 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0), |
566 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0), |
567 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), |
568 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
569 | | OSSL_PARAM_int(OSSL_PKEY_PARAM_CMS_RI_TYPE, NULL), |
570 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_CMS_KEMRI_KDF_ALGORITHM, NULL, 0), |
571 | | OSSL_PARAM_END |
572 | | }; |
573 | | #endif |
574 | | |
575 | | #ifndef ml_kem_get_params_st |
576 | | struct ml_kem_get_params_st { |
577 | | OSSL_PARAM *bits; |
578 | | OSSL_PARAM *encpubkey; |
579 | | OSSL_PARAM *kemri_kdf_alg; |
580 | | OSSL_PARAM *maxsize; |
581 | | OSSL_PARAM *privkey; |
582 | | OSSL_PARAM *pubkey; |
583 | | OSSL_PARAM *ri_type; |
584 | | OSSL_PARAM *secbits; |
585 | | OSSL_PARAM *seccat; |
586 | | OSSL_PARAM *seed; |
587 | | }; |
588 | | #endif |
589 | | |
590 | | #ifndef ml_kem_get_params_decoder |
591 | | static int ml_kem_get_params_decoder |
592 | | (const OSSL_PARAM *p, struct ml_kem_get_params_st *r) |
593 | 0 | { |
594 | 0 | const char *s; |
595 | |
|
596 | 0 | memset(r, 0, sizeof(*r)); |
597 | 0 | if (p != NULL) |
598 | 0 | for (; (s = p->key) != NULL; p++) |
599 | 0 | switch(s[0]) { |
600 | 0 | default: |
601 | 0 | break; |
602 | 0 | case 'b': |
603 | 0 | if (ossl_likely(strcmp("its", s + 1) == 0)) { |
604 | | /* OSSL_PKEY_PARAM_BITS */ |
605 | 0 | if (ossl_unlikely(r->bits != NULL)) { |
606 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
607 | 0 | "param %s is repeated", s); |
608 | 0 | return 0; |
609 | 0 | } |
610 | 0 | r->bits = (OSSL_PARAM *)p; |
611 | 0 | } |
612 | 0 | break; |
613 | 0 | case 'e': |
614 | 0 | if (ossl_likely(strcmp("ncoded-pub-key", s + 1) == 0)) { |
615 | | /* OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY */ |
616 | 0 | if (ossl_unlikely(r->encpubkey != NULL)) { |
617 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
618 | 0 | "param %s is repeated", s); |
619 | 0 | return 0; |
620 | 0 | } |
621 | 0 | r->encpubkey = (OSSL_PARAM *)p; |
622 | 0 | } |
623 | 0 | break; |
624 | 0 | case 'k': |
625 | 0 | if (ossl_likely(strcmp("emri-kdf-alg", s + 1) == 0)) { |
626 | | /* OSSL_PKEY_PARAM_CMS_KEMRI_KDF_ALGORITHM */ |
627 | 0 | if (ossl_unlikely(r->kemri_kdf_alg != NULL)) { |
628 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
629 | 0 | "param %s is repeated", s); |
630 | 0 | return 0; |
631 | 0 | } |
632 | 0 | r->kemri_kdf_alg = (OSSL_PARAM *)p; |
633 | 0 | } |
634 | 0 | break; |
635 | 0 | case 'm': |
636 | 0 | if (ossl_likely(strcmp("ax-size", s + 1) == 0)) { |
637 | | /* OSSL_PKEY_PARAM_MAX_SIZE */ |
638 | 0 | if (ossl_unlikely(r->maxsize != NULL)) { |
639 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
640 | 0 | "param %s is repeated", s); |
641 | 0 | return 0; |
642 | 0 | } |
643 | 0 | r->maxsize = (OSSL_PARAM *)p; |
644 | 0 | } |
645 | 0 | break; |
646 | 0 | case 'p': |
647 | 0 | switch(s[1]) { |
648 | 0 | default: |
649 | 0 | break; |
650 | 0 | case 'r': |
651 | 0 | if (ossl_likely(strcmp("iv", s + 2) == 0)) { |
652 | | /* OSSL_PKEY_PARAM_PRIV_KEY */ |
653 | 0 | if (ossl_unlikely(r->privkey != NULL)) { |
654 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
655 | 0 | "param %s is repeated", s); |
656 | 0 | return 0; |
657 | 0 | } |
658 | 0 | r->privkey = (OSSL_PARAM *)p; |
659 | 0 | } |
660 | 0 | break; |
661 | 0 | case 'u': |
662 | 0 | if (ossl_likely(strcmp("b", s + 2) == 0)) { |
663 | | /* OSSL_PKEY_PARAM_PUB_KEY */ |
664 | 0 | if (ossl_unlikely(r->pubkey != NULL)) { |
665 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
666 | 0 | "param %s is repeated", s); |
667 | 0 | return 0; |
668 | 0 | } |
669 | 0 | r->pubkey = (OSSL_PARAM *)p; |
670 | 0 | } |
671 | 0 | } |
672 | 0 | break; |
673 | 0 | case 'r': |
674 | 0 | if (ossl_likely(strcmp("i-type", s + 1) == 0)) { |
675 | | /* OSSL_PKEY_PARAM_CMS_RI_TYPE */ |
676 | 0 | if (ossl_unlikely(r->ri_type != NULL)) { |
677 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
678 | 0 | "param %s is repeated", s); |
679 | 0 | return 0; |
680 | 0 | } |
681 | 0 | r->ri_type = (OSSL_PARAM *)p; |
682 | 0 | } |
683 | 0 | break; |
684 | 0 | case 's': |
685 | 0 | switch(s[1]) { |
686 | 0 | default: |
687 | 0 | break; |
688 | 0 | case 'e': |
689 | 0 | switch(s[2]) { |
690 | 0 | default: |
691 | 0 | break; |
692 | 0 | case 'c': |
693 | 0 | switch(s[3]) { |
694 | 0 | default: |
695 | 0 | break; |
696 | 0 | case 'u': |
697 | 0 | switch(s[4]) { |
698 | 0 | default: |
699 | 0 | break; |
700 | 0 | case 'r': |
701 | 0 | switch(s[5]) { |
702 | 0 | default: |
703 | 0 | break; |
704 | 0 | case 'i': |
705 | 0 | switch(s[6]) { |
706 | 0 | default: |
707 | 0 | break; |
708 | 0 | case 't': |
709 | 0 | switch(s[7]) { |
710 | 0 | default: |
711 | 0 | break; |
712 | 0 | case 'y': |
713 | 0 | switch(s[8]) { |
714 | 0 | default: |
715 | 0 | break; |
716 | 0 | case '-': |
717 | 0 | switch(s[9]) { |
718 | 0 | default: |
719 | 0 | break; |
720 | 0 | case 'b': |
721 | 0 | if (ossl_likely(strcmp("its", s + 10) == 0)) { |
722 | | /* OSSL_PKEY_PARAM_SECURITY_BITS */ |
723 | 0 | if (ossl_unlikely(r->secbits != NULL)) { |
724 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
725 | 0 | "param %s is repeated", s); |
726 | 0 | return 0; |
727 | 0 | } |
728 | 0 | r->secbits = (OSSL_PARAM *)p; |
729 | 0 | } |
730 | 0 | break; |
731 | 0 | case 'c': |
732 | 0 | if (ossl_likely(strcmp("ategory", s + 10) == 0)) { |
733 | | /* OSSL_PKEY_PARAM_SECURITY_CATEGORY */ |
734 | 0 | if (ossl_unlikely(r->seccat != NULL)) { |
735 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
736 | 0 | "param %s is repeated", s); |
737 | 0 | return 0; |
738 | 0 | } |
739 | 0 | r->seccat = (OSSL_PARAM *)p; |
740 | 0 | } |
741 | 0 | } |
742 | 0 | } |
743 | 0 | } |
744 | 0 | } |
745 | 0 | } |
746 | 0 | } |
747 | 0 | } |
748 | 0 | break; |
749 | 0 | case 'e': |
750 | 0 | if (ossl_likely(strcmp("d", s + 3) == 0)) { |
751 | | /* OSSL_PKEY_PARAM_ML_KEM_SEED */ |
752 | 0 | if (ossl_unlikely(r->seed != NULL)) { |
753 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
754 | 0 | "param %s is repeated", s); |
755 | 0 | return 0; |
756 | 0 | } |
757 | 0 | r->seed = (OSSL_PARAM *)p; |
758 | 0 | } |
759 | 0 | } |
760 | 0 | } |
761 | 0 | } |
762 | 0 | return 1; |
763 | 0 | } |
764 | | #endif |
765 | | /* End of machine generated */ |
766 | | |
767 | | static const OSSL_PARAM *ml_kem_gettable_params(void *provctx) |
768 | 0 | { |
769 | 0 | return ml_kem_get_params_list; |
770 | 0 | } |
771 | | |
772 | | #ifndef FIPS_MODULE |
773 | | static void *ml_kem_load(const void *reference, size_t reference_sz) |
774 | 0 | { |
775 | 0 | ML_KEM_KEY *key = NULL; |
776 | 0 | uint8_t *encoded_dk = NULL; |
777 | 0 | uint8_t seed[ML_KEM_SEED_BYTES]; |
778 | |
|
779 | 0 | if (ossl_prov_is_running() && reference_sz == sizeof(key)) { |
780 | | /* The contents of the reference is the address to our object */ |
781 | 0 | key = *(ML_KEM_KEY **)reference; |
782 | 0 | encoded_dk = key->encoded_dk; |
783 | 0 | key->encoded_dk = NULL; |
784 | | /* We grabbed, so we detach it */ |
785 | 0 | *(ML_KEM_KEY **)reference = NULL; |
786 | 0 | if (encoded_dk != NULL |
787 | 0 | && ossl_ml_kem_encode_seed(seed, sizeof(seed), key) |
788 | 0 | && !check_seed(seed, encoded_dk, key)) |
789 | 0 | goto err; |
790 | | /* Generate the key now, if it holds only a stashed seed. */ |
791 | 0 | if (ossl_ml_kem_have_seed(key) |
792 | 0 | && (encoded_dk == NULL |
793 | 0 | || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) { |
794 | 0 | if (!ossl_ml_kem_genkey(NULL, 0, key) |
795 | 0 | || (encoded_dk != NULL && !check_prvenc(encoded_dk, key))) |
796 | 0 | goto err; |
797 | 0 | } else if (encoded_dk != NULL) { |
798 | 0 | if (!ossl_ml_kem_parse_private_key(encoded_dk, |
799 | 0 | key->vinfo->prvkey_bytes, key)) { |
800 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, |
801 | 0 | "error parsing %s private key", |
802 | 0 | key->vinfo->algorithm_name); |
803 | 0 | goto err; |
804 | 0 | } |
805 | 0 | if (!ml_kem_pairwise_test(key, key->prov_flags)) |
806 | 0 | goto err; |
807 | 0 | } |
808 | 0 | OPENSSL_secure_clear_free(encoded_dk, key->vinfo->prvkey_bytes); |
809 | 0 | return key; |
810 | 0 | } |
811 | | |
812 | 0 | err: |
813 | 0 | if (key != NULL && key->vinfo != NULL) |
814 | 0 | OPENSSL_secure_clear_free(encoded_dk, key->vinfo->prvkey_bytes); |
815 | 0 | ossl_ml_kem_key_free(key); |
816 | 0 | return NULL; |
817 | 0 | } |
818 | | #endif |
819 | | |
820 | | static int ml_kem_get_key_param(const ML_KEM_KEY *key, OSSL_PARAM *p, |
821 | | size_t bytes, |
822 | | int (*get_f)(uint8_t *out, size_t len, |
823 | | const ML_KEM_KEY *key)) |
824 | 0 | { |
825 | 0 | if (p->data_type != OSSL_PARAM_OCTET_STRING) |
826 | 0 | return 0; |
827 | 0 | p->return_size = bytes; |
828 | 0 | if (p->data != NULL) |
829 | 0 | if (p->data_size < p->return_size |
830 | 0 | || !(*get_f)(p->data, p->return_size, key)) |
831 | 0 | return 0; |
832 | 0 | return 1; |
833 | 0 | } |
834 | | |
835 | | /* |
836 | | * It is assumed the key is guaranteed non-NULL here, and is from this provider |
837 | | */ |
838 | | static int ml_kem_get_params(void *vkey, OSSL_PARAM params[]) |
839 | 0 | { |
840 | 0 | ML_KEM_KEY *key = vkey; |
841 | 0 | const ML_KEM_VINFO *v; |
842 | 0 | struct ml_kem_get_params_st p; |
843 | |
|
844 | 0 | if (key == NULL || !ml_kem_get_params_decoder(params, &p)) |
845 | 0 | return 0; |
846 | | |
847 | 0 | v = ossl_ml_kem_key_vinfo(key); |
848 | |
|
849 | 0 | if (p.bits != NULL && !OSSL_PARAM_set_size_t(p.bits, v->bits)) |
850 | 0 | return 0; |
851 | | |
852 | 0 | if (p.secbits != NULL && !OSSL_PARAM_set_size_t(p.secbits, v->secbits)) |
853 | 0 | return 0; |
854 | | |
855 | 0 | if (p.maxsize != NULL && !OSSL_PARAM_set_size_t(p.maxsize, v->ctext_bytes)) |
856 | 0 | return 0; |
857 | | |
858 | 0 | if (p.seccat != NULL && !OSSL_PARAM_set_int(p.seccat, v->security_category)) |
859 | 0 | return 0; |
860 | | |
861 | 0 | if (p.pubkey != NULL && ossl_ml_kem_have_pubkey(key)) { |
862 | | /* Exported to EVP_PKEY_get_raw_public_key() */ |
863 | 0 | if (!ml_kem_get_key_param(key, p.pubkey, v->pubkey_bytes, |
864 | 0 | &ossl_ml_kem_encode_public_key)) |
865 | 0 | return 0; |
866 | 0 | } |
867 | | |
868 | 0 | if (p.encpubkey != NULL && ossl_ml_kem_have_pubkey(key)) { |
869 | | /* Needed by EVP_PKEY_get1_encoded_public_key() */ |
870 | 0 | if (!ml_kem_get_key_param(key, p.encpubkey, v->pubkey_bytes, |
871 | 0 | &ossl_ml_kem_encode_public_key)) |
872 | 0 | return 0; |
873 | 0 | } |
874 | | |
875 | 0 | if (p.privkey != NULL && ossl_ml_kem_have_prvkey(key)) { |
876 | | /* Exported to EVP_PKEY_get_raw_private_key() */ |
877 | 0 | if (!ml_kem_get_key_param(key, p.privkey, v->prvkey_bytes, |
878 | 0 | &ossl_ml_kem_encode_private_key)) |
879 | 0 | return 0; |
880 | 0 | } |
881 | | |
882 | 0 | if (p.seed != NULL && ossl_ml_kem_have_seed(key)) { |
883 | | /* Exported for import */ |
884 | 0 | if (!ml_kem_get_key_param(key, p.seed, ML_KEM_SEED_BYTES, |
885 | 0 | &ossl_ml_kem_encode_seed)) |
886 | 0 | return 0; |
887 | 0 | } |
888 | | |
889 | 0 | #ifndef OPENSSL_NO_CMS |
890 | 0 | if (p.ri_type != NULL && !OSSL_PARAM_set_int(p.ri_type, CMS_RECIPINFO_KEM)) |
891 | 0 | return 0; |
892 | | |
893 | 0 | if (p.kemri_kdf_alg != NULL) { |
894 | 0 | uint8_t aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE]; |
895 | 0 | int ret; |
896 | 0 | size_t aid_len = 0; |
897 | 0 | WPACKET pkt; |
898 | 0 | uint8_t *aid = NULL; |
899 | |
|
900 | 0 | ret = WPACKET_init_der(&pkt, aid_buf, sizeof(aid_buf)); |
901 | 0 | ret &= ossl_DER_w_begin_sequence(&pkt, -1) |
902 | 0 | && ossl_DER_w_precompiled(&pkt, -1, ossl_der_oid_id_alg_hkdf_with_sha256, |
903 | 0 | sizeof(ossl_der_oid_id_alg_hkdf_with_sha256)) |
904 | 0 | && ossl_DER_w_end_sequence(&pkt, -1); |
905 | 0 | if (ret && WPACKET_finish(&pkt)) { |
906 | 0 | WPACKET_get_total_written(&pkt, &aid_len); |
907 | 0 | aid = WPACKET_get_curr(&pkt); |
908 | 0 | } |
909 | 0 | WPACKET_cleanup(&pkt); |
910 | 0 | if (!ret) |
911 | 0 | return 0; |
912 | 0 | if (aid != NULL && aid_len != 0 && |
913 | 0 | !OSSL_PARAM_set_octet_string(p.kemri_kdf_alg, aid, aid_len)) |
914 | 0 | return 0; |
915 | 0 | } |
916 | 0 | #endif |
917 | | |
918 | 0 | return 1; |
919 | 0 | } |
920 | | |
921 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
922 | | #ifndef ml_kem_set_params_list |
923 | | static const OSSL_PARAM ml_kem_set_params_list[] = { |
924 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0), |
925 | | OSSL_PARAM_END |
926 | | }; |
927 | | #endif |
928 | | |
929 | | #ifndef ml_kem_set_params_st |
930 | | struct ml_kem_set_params_st { |
931 | | OSSL_PARAM *pub; |
932 | | }; |
933 | | #endif |
934 | | |
935 | | #ifndef ml_kem_set_params_decoder |
936 | | static int ml_kem_set_params_decoder |
937 | | (const OSSL_PARAM *p, struct ml_kem_set_params_st *r) |
938 | 0 | { |
939 | 0 | const char *s; |
940 | |
|
941 | 0 | memset(r, 0, sizeof(*r)); |
942 | 0 | if (p != NULL) |
943 | 0 | for (; (s = p->key) != NULL; p++) |
944 | 0 | if (ossl_likely(strcmp("encoded-pub-key", s + 0) == 0)) { |
945 | | /* OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY */ |
946 | 0 | if (ossl_unlikely(r->pub != NULL)) { |
947 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
948 | 0 | "param %s is repeated", s); |
949 | 0 | return 0; |
950 | 0 | } |
951 | 0 | r->pub = (OSSL_PARAM *)p; |
952 | 0 | } |
953 | 0 | return 1; |
954 | 0 | } |
955 | | #endif |
956 | | /* End of machine generated */ |
957 | | |
958 | | static const OSSL_PARAM *ml_kem_settable_params(void *provctx) |
959 | 0 | { |
960 | 0 | return ml_kem_set_params_list; |
961 | 0 | } |
962 | | |
963 | | static int ml_kem_set_params(void *vkey, const OSSL_PARAM params[]) |
964 | 0 | { |
965 | 0 | ML_KEM_KEY *key = vkey; |
966 | 0 | const void *pubenc = NULL; |
967 | 0 | size_t publen = 0; |
968 | 0 | struct ml_kem_set_params_st p; |
969 | |
|
970 | 0 | if (key == NULL || !ml_kem_set_params_decoder(params, &p)) |
971 | 0 | return 0; |
972 | | |
973 | | /* Used in TLS via EVP_PKEY_set1_encoded_public_key(). */ |
974 | 0 | if (p.pub != NULL |
975 | 0 | && (OSSL_PARAM_get_octet_string_ptr(p.pub, &pubenc, &publen) != 1 |
976 | 0 | || publen != key->vinfo->pubkey_bytes)) { |
977 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
978 | 0 | return 0; |
979 | 0 | } |
980 | | |
981 | 0 | if (publen == 0) |
982 | 0 | return 1; |
983 | | |
984 | | /* Key mutation is reportedly generally not allowed */ |
985 | 0 | if (ossl_ml_kem_have_pubkey(key)) { |
986 | 0 | ERR_raise_data(ERR_LIB_PROV, |
987 | 0 | PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE, |
988 | 0 | "ML-KEM keys cannot be mutated"); |
989 | 0 | return 0; |
990 | 0 | } |
991 | | |
992 | 0 | return ossl_ml_kem_parse_public_key(pubenc, publen, key); |
993 | 0 | } |
994 | | |
995 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
996 | | #ifndef ml_kem_gen_set_params_list |
997 | | static const OSSL_PARAM ml_kem_gen_set_params_list[] = { |
998 | | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_DSA_SEED, NULL, 0), |
999 | | OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0), |
1000 | | OSSL_PARAM_END |
1001 | | }; |
1002 | | #endif |
1003 | | |
1004 | | #ifndef ml_kem_gen_set_params_st |
1005 | | struct ml_kem_gen_set_params_st { |
1006 | | OSSL_PARAM *propq; |
1007 | | OSSL_PARAM *seed; |
1008 | | }; |
1009 | | #endif |
1010 | | |
1011 | | #ifndef ml_kem_gen_set_params_decoder |
1012 | | static int ml_kem_gen_set_params_decoder |
1013 | | (const OSSL_PARAM *p, struct ml_kem_gen_set_params_st *r) |
1014 | 0 | { |
1015 | 0 | const char *s; |
1016 | |
|
1017 | 0 | memset(r, 0, sizeof(*r)); |
1018 | 0 | if (p != NULL) |
1019 | 0 | for (; (s = p->key) != NULL; p++) |
1020 | 0 | switch(s[0]) { |
1021 | 0 | default: |
1022 | 0 | break; |
1023 | 0 | case 'p': |
1024 | 0 | if (ossl_likely(strcmp("roperties", s + 1) == 0)) { |
1025 | | /* OSSL_PKEY_PARAM_PROPERTIES */ |
1026 | 0 | if (ossl_unlikely(r->propq != NULL)) { |
1027 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
1028 | 0 | "param %s is repeated", s); |
1029 | 0 | return 0; |
1030 | 0 | } |
1031 | 0 | r->propq = (OSSL_PARAM *)p; |
1032 | 0 | } |
1033 | 0 | break; |
1034 | 0 | case 's': |
1035 | 0 | if (ossl_likely(strcmp("eed", s + 1) == 0)) { |
1036 | | /* OSSL_PKEY_PARAM_ML_DSA_SEED */ |
1037 | 0 | if (ossl_unlikely(r->seed != NULL)) { |
1038 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
1039 | 0 | "param %s is repeated", s); |
1040 | 0 | return 0; |
1041 | 0 | } |
1042 | 0 | r->seed = (OSSL_PARAM *)p; |
1043 | 0 | } |
1044 | 0 | } |
1045 | 0 | return 1; |
1046 | 0 | } |
1047 | | #endif |
1048 | | /* End of machine generated */ |
1049 | | |
1050 | | static int ml_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[]) |
1051 | 0 | { |
1052 | 0 | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
1053 | 0 | struct ml_kem_gen_set_params_st p; |
1054 | |
|
1055 | 0 | if (gctx == NULL || !ml_kem_gen_set_params_decoder(params, &p)) |
1056 | 0 | return 0; |
1057 | | |
1058 | 0 | if (p.propq != NULL) { |
1059 | 0 | if (p.propq->data_type != OSSL_PARAM_UTF8_STRING) |
1060 | 0 | return 0; |
1061 | 0 | OPENSSL_free(gctx->propq); |
1062 | 0 | if ((gctx->propq = OPENSSL_strdup(p.propq->data)) == NULL) |
1063 | 0 | return 0; |
1064 | 0 | } |
1065 | | |
1066 | 0 | if (p.seed != NULL) { |
1067 | 0 | size_t len = ML_KEM_SEED_BYTES; |
1068 | |
|
1069 | 0 | gctx->seed = gctx->seedbuf; |
1070 | 0 | if (OSSL_PARAM_get_octet_string(p.seed, (void **)&gctx->seed, len, &len) |
1071 | 0 | && len == ML_KEM_SEED_BYTES) |
1072 | 0 | return 1; |
1073 | | |
1074 | | /* Possibly, but less likely wrong data type */ |
1075 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH); |
1076 | 0 | gctx->seed = NULL; |
1077 | 0 | return 0; |
1078 | 0 | } |
1079 | | |
1080 | 0 | return 1; |
1081 | 0 | } |
1082 | | |
1083 | | static void *ml_kem_gen_init(void *provctx, int selection, |
1084 | | const OSSL_PARAM params[], int evp_type) |
1085 | 0 | { |
1086 | 0 | PROV_ML_KEM_GEN_CTX *gctx = NULL; |
1087 | | |
1088 | | /* |
1089 | | * We can only generate private keys, check that the selection is |
1090 | | * appropriate. |
1091 | | */ |
1092 | 0 | if (!ossl_prov_is_running() |
1093 | 0 | || (selection & minimal_selection) == 0 |
1094 | 0 | || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL) |
1095 | 0 | return NULL; |
1096 | | |
1097 | 0 | gctx->selection = selection; |
1098 | 0 | gctx->evp_type = evp_type; |
1099 | 0 | gctx->provctx = provctx; |
1100 | 0 | if (ml_kem_gen_set_params(gctx, params)) |
1101 | 0 | return gctx; |
1102 | | |
1103 | 0 | ml_kem_gen_cleanup(gctx); |
1104 | 0 | return NULL; |
1105 | 0 | } |
1106 | | |
1107 | | static const OSSL_PARAM *ml_kem_gen_settable_params(ossl_unused void *vgctx, |
1108 | | ossl_unused void *provctx) |
1109 | 0 | { |
1110 | 0 | return ml_kem_gen_set_params_list; |
1111 | 0 | } |
1112 | | |
1113 | | static void *ml_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg) |
1114 | 0 | { |
1115 | 0 | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
1116 | 0 | ML_KEM_KEY *key; |
1117 | 0 | uint8_t *nopub = NULL; |
1118 | 0 | uint8_t *seed; |
1119 | 0 | int genok = 0; |
1120 | |
|
1121 | 0 | if (gctx == NULL |
1122 | 0 | || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == |
1123 | 0 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY) |
1124 | 0 | return NULL; |
1125 | 0 | seed = gctx->seed; |
1126 | 0 | key = ossl_prov_ml_kem_new(gctx->provctx, gctx->propq, gctx->evp_type); |
1127 | 0 | if (key == NULL) |
1128 | 0 | return NULL; |
1129 | | |
1130 | 0 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
1131 | 0 | return key; |
1132 | | |
1133 | 0 | if (seed != NULL && !ossl_ml_kem_set_seed(seed, ML_KEM_SEED_BYTES, key)) |
1134 | 0 | return NULL; |
1135 | 0 | genok = ossl_ml_kem_genkey(nopub, 0, key); |
1136 | | |
1137 | | /* Erase the single-use seed */ |
1138 | 0 | if (seed != NULL) |
1139 | 0 | OPENSSL_cleanse(seed, ML_KEM_SEED_BYTES); |
1140 | 0 | gctx->seed = NULL; |
1141 | |
|
1142 | 0 | if (genok) { |
1143 | | #ifdef FIPS_MODULE |
1144 | | if (!ml_kem_pairwise_test(key, ML_KEM_KEY_FIXED_PCT)) { |
1145 | | ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT); |
1146 | | ossl_ml_kem_key_free(key); |
1147 | | return NULL; |
1148 | | } |
1149 | | #endif /* FIPS_MODULE */ |
1150 | 0 | return key; |
1151 | 0 | } |
1152 | | |
1153 | 0 | ossl_ml_kem_key_free(key); |
1154 | 0 | return NULL; |
1155 | 0 | } |
1156 | | |
1157 | | static void ml_kem_gen_cleanup(void *vgctx) |
1158 | 0 | { |
1159 | 0 | PROV_ML_KEM_GEN_CTX *gctx = vgctx; |
1160 | |
|
1161 | 0 | if (gctx == NULL) |
1162 | 0 | return; |
1163 | | |
1164 | 0 | if (gctx->seed != NULL) |
1165 | 0 | OPENSSL_cleanse(gctx->seed, ML_KEM_RANDOM_BYTES); |
1166 | 0 | OPENSSL_free(gctx->propq); |
1167 | 0 | OPENSSL_free(gctx); |
1168 | 0 | } |
1169 | | |
1170 | | static void *ml_kem_dup(const void *vkey, int selection) |
1171 | 0 | { |
1172 | 0 | const ML_KEM_KEY *key = vkey; |
1173 | |
|
1174 | 0 | if (!ossl_prov_is_running()) |
1175 | 0 | return NULL; |
1176 | | |
1177 | 0 | return ossl_ml_kem_key_dup(key, selection); |
1178 | 0 | } |
1179 | | |
1180 | | #ifndef FIPS_MODULE |
1181 | | # define DISPATCH_LOAD_FN \ |
1182 | | { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC) ml_kem_load }, |
1183 | | #else |
1184 | | # define DISPATCH_LOAD_FN /* Non-FIPS only */ |
1185 | | #endif |
1186 | | |
1187 | | #define DECLARE_VARIANT(bits) \ |
1188 | | static OSSL_FUNC_keymgmt_new_fn ml_kem_##bits##_new; \ |
1189 | | static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_##bits##_gen_init; \ |
1190 | | static void *ml_kem_##bits##_new(void *provctx) \ |
1191 | 0 | { \ |
1192 | 0 | return ossl_prov_ml_kem_new(provctx, NULL, EVP_PKEY_ML_KEM_##bits); \ |
1193 | 0 | } \ |
1194 | | static void *ml_kem_##bits##_gen_init(void *provctx, int selection, \ |
1195 | | const OSSL_PARAM params[]) \ |
1196 | 0 | { \ |
1197 | 0 | return ml_kem_gen_init(provctx, selection, params, \ |
1198 | 0 | EVP_PKEY_ML_KEM_##bits); \ |
1199 | 0 | } \ Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_512_gen_init Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_768_gen_init Unexecuted instantiation: ml_kem_kmgmt.c:ml_kem_1024_gen_init |
1200 | | const OSSL_DISPATCH ossl_ml_kem_##bits##_keymgmt_functions[] = { \ |
1201 | | { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC) ml_kem_##bits##_new }, \ |
1202 | | { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC) ossl_ml_kem_key_free }, \ |
1203 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC) ml_kem_get_params }, \ |
1204 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gettable_params }, \ |
1205 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC) ml_kem_set_params }, \ |
1206 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_settable_params }, \ |
1207 | | { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC) ml_kem_has }, \ |
1208 | | { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC) ml_kem_match }, \ |
1209 | | { OSSL_FUNC_KEYMGMT_VALIDATE, (OSSL_FUNC) ml_kem_validate }, \ |
1210 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC) ml_kem_##bits##_gen_init }, \ |
1211 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC) ml_kem_gen_set_params }, \ |
1212 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gen_settable_params }, \ |
1213 | | { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC) ml_kem_gen }, \ |
1214 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC) ml_kem_gen_cleanup }, \ |
1215 | | DISPATCH_LOAD_FN \ |
1216 | | { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC) ml_kem_dup }, \ |
1217 | | { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC) ml_kem_import }, \ |
1218 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \ |
1219 | | { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC) ml_kem_export }, \ |
1220 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \ |
1221 | | OSSL_DISPATCH_END \ |
1222 | | } |
1223 | 0 | DECLARE_VARIANT(512); |
1224 | 0 | DECLARE_VARIANT(768); |
1225 | | DECLARE_VARIANT(1024); |