ml_kem_kmgmt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. #include <openssl/core_dispatch.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include <openssl/err.h>
  13. #include <openssl/proverr.h>
  14. #include <openssl/provider.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/self_test.h>
  17. #include <openssl/param_build.h>
  18. #include "crypto/ml_kem.h"
  19. #include "internal/fips.h"
  20. #include "internal/param_build_set.h"
  21. #include "prov/implementations.h"
  22. #include "prov/providercommon.h"
  23. #include "prov/provider_ctx.h"
  24. #include "prov/securitycheck.h"
  25. #include "prov/ml_kem.h"
  26. static OSSL_FUNC_keymgmt_new_fn ml_kem_512_new;
  27. static OSSL_FUNC_keymgmt_new_fn ml_kem_768_new;
  28. static OSSL_FUNC_keymgmt_new_fn ml_kem_1024_new;
  29. static OSSL_FUNC_keymgmt_gen_fn ml_kem_gen;
  30. static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_512_gen_init;
  31. static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_768_gen_init;
  32. static OSSL_FUNC_keymgmt_gen_init_fn ml_kem_1024_gen_init;
  33. static OSSL_FUNC_keymgmt_gen_cleanup_fn ml_kem_gen_cleanup;
  34. static OSSL_FUNC_keymgmt_gen_set_params_fn ml_kem_gen_set_params;
  35. static OSSL_FUNC_keymgmt_gen_settable_params_fn ml_kem_gen_settable_params;
  36. #ifndef FIPS_MODULE
  37. static OSSL_FUNC_keymgmt_load_fn ml_kem_load;
  38. #endif
  39. static OSSL_FUNC_keymgmt_get_params_fn ml_kem_get_params;
  40. static OSSL_FUNC_keymgmt_gettable_params_fn ml_kem_gettable_params;
  41. static OSSL_FUNC_keymgmt_set_params_fn ml_kem_set_params;
  42. static OSSL_FUNC_keymgmt_settable_params_fn ml_kem_settable_params;
  43. static OSSL_FUNC_keymgmt_has_fn ml_kem_has;
  44. static OSSL_FUNC_keymgmt_match_fn ml_kem_match;
  45. static OSSL_FUNC_keymgmt_validate_fn ml_kem_validate;
  46. static OSSL_FUNC_keymgmt_import_fn ml_kem_import;
  47. static OSSL_FUNC_keymgmt_export_fn ml_kem_export;
  48. static OSSL_FUNC_keymgmt_import_types_fn ml_kem_imexport_types;
  49. static OSSL_FUNC_keymgmt_export_types_fn ml_kem_imexport_types;
  50. static OSSL_FUNC_keymgmt_dup_fn ml_kem_dup;
  51. static const int minimal_selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
  52. | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  53. typedef struct ml_kem_gen_ctx_st {
  54. PROV_CTX *provctx;
  55. char *propq;
  56. int selection;
  57. int evp_type;
  58. uint8_t seedbuf[ML_KEM_SEED_BYTES];
  59. uint8_t *seed;
  60. } PROV_ML_KEM_GEN_CTX;
  61. static int ml_kem_pairwise_test(const ML_KEM_KEY *key, int key_flags)
  62. {
  63. #ifdef FIPS_MODULE
  64. OSSL_SELF_TEST *st = NULL;
  65. OSSL_CALLBACK *cb = NULL;
  66. void *cbarg = NULL;
  67. #endif
  68. unsigned char entropy[ML_KEM_RANDOM_BYTES];
  69. unsigned char secret[ML_KEM_SHARED_SECRET_BYTES];
  70. unsigned char out[ML_KEM_SHARED_SECRET_BYTES];
  71. unsigned char *ctext = NULL;
  72. const ML_KEM_VINFO *v = ossl_ml_kem_key_vinfo(key);
  73. int operation_result = 0;
  74. int ret = 0;
  75. /* Unless we have both a public and private key, we can't do the test */
  76. if (!ossl_ml_kem_have_prvkey(key)
  77. || !ossl_ml_kem_have_pubkey(key)
  78. || (key_flags & ML_KEM_KEY_PCT_TYPE) == 0)
  79. return 1;
  80. #ifdef FIPS_MODULE
  81. /* During self test, it is a waste to do this test */
  82. if (ossl_fips_self_testing())
  83. return 1;
  84. /*
  85. * The functions `OSSL_SELF_TEST_*` will return directly if parameter `st`
  86. * is NULL.
  87. */
  88. OSSL_SELF_TEST_get_callback(key->libctx, &cb, &cbarg);
  89. st = OSSL_SELF_TEST_new(cb, cbarg);
  90. if (st == NULL)
  91. return 0;
  92. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
  93. OSSL_SELF_TEST_DESC_PCT_ML_KEM);
  94. #endif /* FIPS_MODULE */
  95. ctext = OPENSSL_malloc(v->ctext_bytes);
  96. if (ctext == NULL)
  97. goto err;
  98. memset(out, 0, sizeof(out));
  99. /*
  100. * The pairwise test is skipped unless either RANDOM or FIXED entropy PCTs
  101. * are enabled.
  102. */
  103. if (key_flags & ML_KEM_KEY_RANDOM_PCT) {
  104. operation_result = ossl_ml_kem_encap_rand(ctext, v->ctext_bytes,
  105. secret, sizeof(secret), key);
  106. } else {
  107. memset(entropy, 0125, sizeof(entropy));
  108. operation_result = ossl_ml_kem_encap_seed(ctext, v->ctext_bytes,
  109. secret, sizeof(secret),
  110. entropy, sizeof(entropy),
  111. key);
  112. }
  113. if (operation_result != 1)
  114. goto err;
  115. #ifdef FIPS_MODULE
  116. OSSL_SELF_TEST_oncorrupt_byte(st, ctext);
  117. #endif
  118. operation_result = ossl_ml_kem_decap(out, sizeof(out), ctext, v->ctext_bytes,
  119. key);
  120. if (operation_result != 1 || memcmp(out, secret, sizeof(out)) != 0)
  121. goto err;
  122. ret = 1;
  123. err:
  124. #ifdef FIPS_MODULE
  125. OSSL_SELF_TEST_onend(st, ret);
  126. OSSL_SELF_TEST_free(st);
  127. #else
  128. if (ret == 0) {
  129. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
  130. "public part of %s private key fails to match private",
  131. v->algorithm_name);
  132. }
  133. #endif
  134. OPENSSL_free(ctext);
  135. return ret;
  136. }
  137. ML_KEM_KEY *ossl_prov_ml_kem_new(PROV_CTX *ctx, const char *propq, int evp_type)
  138. {
  139. ML_KEM_KEY *key;
  140. if (!ossl_prov_is_running())
  141. return NULL;
  142. /*
  143. * When decoding, if the key ends up "loaded" into the same provider, these
  144. * are the correct config settings, otherwise, new values will be assigned
  145. * on import into a different provider. The "load" API does not pass along
  146. * the provider context.
  147. */
  148. if ((key = ossl_ml_kem_key_new(PROV_LIBCTX_OF(ctx), propq, evp_type)) != NULL) {
  149. const char *pct_type = ossl_prov_ctx_get_param(
  150. ctx, OSSL_PKEY_PARAM_ML_KEM_IMPORT_PCT_TYPE, "random");
  151. if (ossl_prov_ctx_get_bool_param(
  152. ctx, OSSL_PKEY_PARAM_ML_KEM_RETAIN_SEED, 1))
  153. key->prov_flags |= ML_KEM_KEY_RETAIN_SEED;
  154. else
  155. key->prov_flags &= ~ML_KEM_KEY_RETAIN_SEED;
  156. if (ossl_prov_ctx_get_bool_param(
  157. ctx, OSSL_PKEY_PARAM_ML_KEM_PREFER_SEED, 1))
  158. key->prov_flags |= ML_KEM_KEY_PREFER_SEED;
  159. else
  160. key->prov_flags &= ~ML_KEM_KEY_PREFER_SEED;
  161. if (OPENSSL_strcasecmp(pct_type, "random") == 0)
  162. key->prov_flags |= ML_KEM_KEY_RANDOM_PCT;
  163. else if (OPENSSL_strcasecmp(pct_type, "fixed") == 0)
  164. key->prov_flags |= ML_KEM_KEY_FIXED_PCT;
  165. else
  166. key->prov_flags &= ~ML_KEM_KEY_PCT_TYPE;
  167. }
  168. return key;
  169. }
  170. static int ml_kem_has(const void *vkey, int selection)
  171. {
  172. const ML_KEM_KEY *key = vkey;
  173. /* A NULL key MUST fail to have anything */
  174. if (!ossl_prov_is_running() || key == NULL)
  175. return 0;
  176. switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
  177. case 0:
  178. return 1;
  179. case OSSL_KEYMGMT_SELECT_PUBLIC_KEY:
  180. return ossl_ml_kem_have_pubkey(key);
  181. default:
  182. return ossl_ml_kem_have_prvkey(key);
  183. }
  184. }
  185. static int ml_kem_match(const void *vkey1, const void *vkey2, int selection)
  186. {
  187. const ML_KEM_KEY *key1 = vkey1;
  188. const ML_KEM_KEY *key2 = vkey2;
  189. if (!ossl_prov_is_running())
  190. return 0;
  191. /* All we have that can be compared is key material */
  192. if (!(selection & OSSL_KEYMGMT_SELECT_KEYPAIR))
  193. return 1;
  194. return ossl_ml_kem_pubkey_cmp(key1, key2);
  195. }
  196. static int ml_kem_validate(const void *vkey, int selection, int check_type)
  197. {
  198. const ML_KEM_KEY *key = vkey;
  199. if (!ml_kem_has(key, selection))
  200. return 0;
  201. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
  202. return ml_kem_pairwise_test(key, ML_KEM_KEY_RANDOM_PCT);
  203. return 1;
  204. }
  205. static int ml_kem_export(void *vkey, int selection, OSSL_CALLBACK *param_cb,
  206. void *cbarg)
  207. {
  208. ML_KEM_KEY *key = vkey;
  209. OSSL_PARAM_BLD *tmpl = NULL;
  210. OSSL_PARAM *params = NULL;
  211. const ML_KEM_VINFO *v;
  212. uint8_t *pubenc = NULL, *prvenc = NULL, *seedenc = NULL;
  213. size_t prvlen = 0, seedlen = 0;
  214. int ret = 0;
  215. if (!ossl_prov_is_running() || key == NULL)
  216. return 0;
  217. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  218. return 0;
  219. v = ossl_ml_kem_key_vinfo(key);
  220. if (!ossl_ml_kem_have_pubkey(key)) {
  221. /* Fail when no key material can be returned */
  222. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0
  223. || !ossl_ml_kem_decoded_key(key)) {
  224. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  225. return 0;
  226. }
  227. } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  228. pubenc = OPENSSL_malloc(v->pubkey_bytes);
  229. if (pubenc == NULL
  230. || !ossl_ml_kem_encode_public_key(pubenc, v->pubkey_bytes, key))
  231. goto err;
  232. }
  233. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  234. /*
  235. * The seed and/or private key material are allocated on the secure
  236. * heap if configured, ossl_param_build_set_octet_string(), will then
  237. * also use the secure heap.
  238. */
  239. if (ossl_ml_kem_have_seed(key)) {
  240. seedlen = ML_KEM_SEED_BYTES;
  241. if ((seedenc = OPENSSL_secure_zalloc(seedlen)) == NULL
  242. || !ossl_ml_kem_encode_seed(seedenc, seedlen, key))
  243. goto err;
  244. }
  245. if (ossl_ml_kem_have_prvkey(key)) {
  246. prvlen = v->prvkey_bytes;
  247. if ((prvenc = OPENSSL_secure_zalloc(prvlen)) == NULL
  248. || !ossl_ml_kem_encode_private_key(prvenc, prvlen, key))
  249. goto err;
  250. } else if (ossl_ml_kem_have_dkenc(key)) {
  251. prvlen = v->prvkey_bytes;
  252. if ((prvenc = OPENSSL_secure_zalloc(prvlen)) == NULL)
  253. goto err;
  254. memcpy(prvenc, key->encoded_dk, prvlen);
  255. }
  256. }
  257. tmpl = OSSL_PARAM_BLD_new();
  258. if (tmpl == NULL)
  259. goto err;
  260. /* The (d, z) seed, when available and private keys are requested. */
  261. if (seedenc != NULL
  262. && !ossl_param_build_set_octet_string(
  263. tmpl, params, OSSL_PKEY_PARAM_ML_KEM_SEED, seedenc, seedlen))
  264. goto err;
  265. /* The private key in the FIPS 203 |dk| format, when requested. */
  266. if (prvenc != NULL
  267. && !ossl_param_build_set_octet_string(
  268. tmpl, params, OSSL_PKEY_PARAM_PRIV_KEY, prvenc, prvlen))
  269. goto err;
  270. /* The public key on request; it is always available when either is */
  271. if (pubenc != NULL
  272. && !ossl_param_build_set_octet_string(
  273. tmpl, params, OSSL_PKEY_PARAM_PUB_KEY, pubenc, v->pubkey_bytes))
  274. goto err;
  275. params = OSSL_PARAM_BLD_to_param(tmpl);
  276. if (params == NULL)
  277. goto err;
  278. ret = param_cb(params, cbarg);
  279. OSSL_PARAM_free(params);
  280. err:
  281. OSSL_PARAM_BLD_free(tmpl);
  282. OPENSSL_secure_clear_free(seedenc, seedlen);
  283. OPENSSL_secure_clear_free(prvenc, prvlen);
  284. OPENSSL_free(pubenc);
  285. return ret;
  286. }
  287. static const OSSL_PARAM *ml_kem_imexport_types(int selection)
  288. {
  289. static const OSSL_PARAM key_types[] = {
  290. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0),
  291. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
  292. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
  293. OSSL_PARAM_END
  294. };
  295. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  296. return key_types;
  297. return NULL;
  298. }
  299. static int check_seed(const uint8_t *seed, const uint8_t *prvenc,
  300. ML_KEM_KEY *key)
  301. {
  302. size_t zlen = ML_KEM_RANDOM_BYTES;
  303. if (memcmp(seed + ML_KEM_SEED_BYTES - zlen,
  304. prvenc + key->vinfo->prvkey_bytes - zlen, zlen) == 0)
  305. return 1;
  306. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
  307. "private %s key implicit rejection secret does"
  308. " not match seed", key->vinfo->algorithm_name);
  309. return 0;
  310. }
  311. static int check_prvenc(const uint8_t *prvenc, ML_KEM_KEY *key)
  312. {
  313. size_t len = key->vinfo->prvkey_bytes;
  314. uint8_t *buf = OPENSSL_malloc(len);
  315. int ret = 0;
  316. if (buf != NULL
  317. && ossl_ml_kem_encode_private_key(buf, len, key))
  318. ret = memcmp(buf, prvenc, len) == 0;
  319. OPENSSL_clear_free(buf, len);
  320. if (ret)
  321. return 1;
  322. if (buf != NULL)
  323. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
  324. "explicit %s private key does not match seed",
  325. key->vinfo->algorithm_name);
  326. ossl_ml_kem_key_reset(key);
  327. return 0;
  328. }
  329. static int ml_kem_key_fromdata(ML_KEM_KEY *key,
  330. const OSSL_PARAM params[],
  331. int include_private)
  332. {
  333. const OSSL_PARAM *p = NULL;
  334. const void *pubenc = NULL, *prvenc = NULL, *seedenc = NULL;
  335. size_t publen = 0, prvlen = 0, seedlen = 0, puboff;
  336. const ML_KEM_VINFO *v;
  337. /* Invalid attempt to mutate a key, what is the right error to report? */
  338. if (key == NULL || ossl_ml_kem_have_pubkey(key))
  339. return 0;
  340. v = ossl_ml_kem_key_vinfo(key);
  341. /*
  342. * When a private key is provided, without a seed, any public key also
  343. * provided will be ignored (apart from length), just as with the seed.
  344. */
  345. if (include_private) {
  346. /*
  347. * When a seed is provided, the private and public keys may be ignored,
  348. * after validating just their lengths. Comparing encodings or hashes
  349. * when applicable is possible, but not currently implemented.
  350. */
  351. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ML_KEM_SEED);
  352. if (p != NULL
  353. && OSSL_PARAM_get_octet_string_ptr(p, &seedenc, &seedlen) != 1)
  354. return 0;
  355. if (seedlen != 0 && seedlen != ML_KEM_SEED_BYTES) {
  356. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
  357. return 0;
  358. }
  359. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  360. if (p != NULL
  361. && OSSL_PARAM_get_octet_string_ptr(p, &prvenc, &prvlen) != 1)
  362. return 0;
  363. if (prvlen != 0 && prvlen != v->prvkey_bytes) {
  364. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  365. return 0;
  366. }
  367. }
  368. /* Used only when no seed or private key is provided. */
  369. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  370. if (p != NULL
  371. && OSSL_PARAM_get_octet_string_ptr(p, &pubenc, &publen) != 1)
  372. return 0;
  373. if (publen != 0 && publen != v->pubkey_bytes) {
  374. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  375. return 0;
  376. }
  377. /* The caller MUST specify at least one of seed, private or public keys. */
  378. if (seedlen == 0 && publen == 0 && prvlen == 0) {
  379. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  380. return 0;
  381. }
  382. /* Check any explicit public key against embedded value in private key */
  383. if (publen > 0 && prvlen > 0) {
  384. /* point to the ek offset in dk = DKpke||ek||H(ek)||z */
  385. puboff = prvlen - ML_KEM_RANDOM_BYTES - ML_KEM_PKHASH_BYTES - publen;
  386. if (memcmp(pubenc, (unsigned char *)prvenc + puboff, publen) != 0) {
  387. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
  388. "explicit %s public key does not match private",
  389. v->algorithm_name);
  390. return 0;
  391. }
  392. }
  393. if (seedlen != 0
  394. && (prvlen == 0 || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) {
  395. if (prvlen != 0 && !check_seed(seedenc, prvenc, key))
  396. return 0;
  397. if (!ossl_ml_kem_set_seed(seedenc, seedlen, key)
  398. || !ossl_ml_kem_genkey(NULL, 0, key))
  399. return 0;
  400. return prvlen == 0 || check_prvenc(prvenc, key);
  401. } else if (prvlen != 0) {
  402. return ossl_ml_kem_parse_private_key(prvenc, prvlen, key);
  403. }
  404. return ossl_ml_kem_parse_public_key(pubenc, publen, key);
  405. }
  406. static int ml_kem_import(void *vkey, int selection, const OSSL_PARAM params[])
  407. {
  408. ML_KEM_KEY *key = vkey;
  409. int include_private;
  410. int res;
  411. if (!ossl_prov_is_running() || key == NULL)
  412. return 0;
  413. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  414. return 0;
  415. include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  416. res = ml_kem_key_fromdata(key, params, include_private);
  417. if (res > 0 && include_private
  418. && !ml_kem_pairwise_test(key, key->prov_flags)) {
  419. #ifdef FIPS_MODULE
  420. ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT_IMPORT);
  421. #endif
  422. ossl_ml_kem_key_reset(key);
  423. res = 0;
  424. }
  425. return res;
  426. }
  427. static const OSSL_PARAM *ml_kem_gettable_params(void *provctx)
  428. {
  429. static const OSSL_PARAM arr[] = {
  430. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  431. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  432. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  433. /* Exported for import */
  434. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0),
  435. /* Exported to EVP_PKEY_get_raw_private_key() */
  436. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
  437. /* Exported to EVP_PKEY_get_raw_public_key() */
  438. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
  439. /* Needed by EVP_PKEY_get1_encoded_public_key() */
  440. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  441. OSSL_PARAM_END
  442. };
  443. return arr;
  444. }
  445. #ifndef FIPS_MODULE
  446. static void *ml_kem_load(const void *reference, size_t reference_sz)
  447. {
  448. ML_KEM_KEY *key = NULL;
  449. uint8_t *encoded_dk = NULL;
  450. uint8_t seed[ML_KEM_SEED_BYTES];
  451. if (ossl_prov_is_running() && reference_sz == sizeof(key)) {
  452. /* The contents of the reference is the address to our object */
  453. key = *(ML_KEM_KEY **)reference;
  454. encoded_dk = key->encoded_dk;
  455. key->encoded_dk = NULL;
  456. /* We grabbed, so we detach it */
  457. *(ML_KEM_KEY **)reference = NULL;
  458. if (encoded_dk != NULL
  459. && ossl_ml_kem_encode_seed(seed, sizeof(seed), key)
  460. && !check_seed(seed, encoded_dk, key))
  461. goto err;
  462. /* Generate the key now, if it holds only a stashed seed. */
  463. if (ossl_ml_kem_have_seed(key)
  464. && (encoded_dk == NULL
  465. || (key->prov_flags & ML_KEM_KEY_PREFER_SEED))) {
  466. if (!ossl_ml_kem_genkey(NULL, 0, key)
  467. || (encoded_dk != NULL && !check_prvenc(encoded_dk, key)))
  468. goto err;
  469. } else if (encoded_dk != NULL) {
  470. if (!ossl_ml_kem_parse_private_key(encoded_dk,
  471. key->vinfo->prvkey_bytes, key)) {
  472. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
  473. "error parsing %s private key",
  474. key->vinfo->algorithm_name);
  475. goto err;
  476. }
  477. if (!ml_kem_pairwise_test(key, key->prov_flags))
  478. goto err;
  479. }
  480. OPENSSL_free(encoded_dk);
  481. return key;
  482. }
  483. err:
  484. OPENSSL_free(encoded_dk);
  485. ossl_ml_kem_key_free(key);
  486. return NULL;
  487. }
  488. #endif
  489. /*
  490. * It is assumed the key is guaranteed non-NULL here, and is from this provider
  491. */
  492. static int ml_kem_get_params(void *vkey, OSSL_PARAM params[])
  493. {
  494. ML_KEM_KEY *key = vkey;
  495. const ML_KEM_VINFO *v = ossl_ml_kem_key_vinfo(key);
  496. OSSL_PARAM *p;
  497. const char *pubparams[] = {
  498. OSSL_PKEY_PARAM_PUB_KEY,
  499. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
  500. };
  501. int i;
  502. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS);
  503. if (p != NULL)
  504. if (!OSSL_PARAM_set_int(p, v->bits))
  505. return 0;
  506. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS);
  507. if (p != NULL)
  508. if (!OSSL_PARAM_set_int(p, v->secbits))
  509. return 0;
  510. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE);
  511. if (p != NULL)
  512. if (!OSSL_PARAM_set_int(p, v->ctext_bytes))
  513. return 0;
  514. if (ossl_ml_kem_have_pubkey(key)) {
  515. uint8_t *pubenc = NULL;
  516. for (i = 0; i < 2; ++i) {
  517. p = OSSL_PARAM_locate(params, pubparams[i]);
  518. if (p == NULL)
  519. continue;
  520. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  521. return 0;
  522. p->return_size = v->pubkey_bytes;
  523. if (p->data == NULL)
  524. continue;
  525. if (p->data_size < p->return_size)
  526. return 0;
  527. if (pubenc != NULL) {
  528. memcpy(p->data, pubenc, p->return_size);
  529. continue;
  530. }
  531. if (!ossl_ml_kem_encode_public_key(p->data, p->return_size, key))
  532. return 0;
  533. pubenc = p->data;
  534. }
  535. }
  536. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY);
  537. if (p != NULL && ossl_ml_kem_have_prvkey(key)) {
  538. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  539. return 0;
  540. p->return_size = v->prvkey_bytes;
  541. if (p->data != NULL) {
  542. if (p->data_size < p->return_size)
  543. return 0;
  544. if (!ossl_ml_kem_encode_private_key(p->data, p->return_size, key))
  545. return 0;
  546. }
  547. }
  548. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ML_KEM_SEED);
  549. if (p != NULL && ossl_ml_kem_have_seed(key)) {
  550. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  551. return 0;
  552. p->return_size = ML_KEM_SEED_BYTES;
  553. if (p->data != NULL) {
  554. if (p->data_size < p->return_size)
  555. return 0;
  556. if (!ossl_ml_kem_encode_seed(p->data, p->return_size, key))
  557. return 0;
  558. }
  559. }
  560. return 1;
  561. }
  562. static const OSSL_PARAM *ml_kem_settable_params(void *provctx)
  563. {
  564. static const OSSL_PARAM arr[] = {
  565. /* Used in TLS via EVP_PKEY_set1_encoded_public_key(). */
  566. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  567. OSSL_PARAM_END
  568. };
  569. return arr;
  570. }
  571. static int ml_kem_set_params(void *vkey, const OSSL_PARAM params[])
  572. {
  573. ML_KEM_KEY *key = vkey;
  574. const OSSL_PARAM *p;
  575. const void *pubenc = NULL;
  576. size_t publen = 0;
  577. if (ossl_param_is_empty(params))
  578. return 1;
  579. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
  580. if (p != NULL
  581. && (OSSL_PARAM_get_octet_string_ptr(p, &pubenc, &publen) != 1
  582. || publen != key->vinfo->pubkey_bytes)) {
  583. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  584. return 0;
  585. }
  586. if (publen == 0)
  587. return 1;
  588. /* Key mutation is reportedly generally not allowed */
  589. if (ossl_ml_kem_have_pubkey(key)) {
  590. ERR_raise_data(ERR_LIB_PROV,
  591. PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
  592. "ML-KEM keys cannot be mutated");
  593. return 0;
  594. }
  595. return ossl_ml_kem_parse_public_key(pubenc, publen, key);
  596. }
  597. static int ml_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[])
  598. {
  599. PROV_ML_KEM_GEN_CTX *gctx = vgctx;
  600. const OSSL_PARAM *p;
  601. if (gctx == NULL)
  602. return 0;
  603. if (ossl_param_is_empty(params))
  604. return 1;
  605. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
  606. if (p != NULL) {
  607. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  608. return 0;
  609. OPENSSL_free(gctx->propq);
  610. if ((gctx->propq = OPENSSL_strdup(p->data)) == NULL)
  611. return 0;
  612. }
  613. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ML_KEM_SEED);
  614. if (p != NULL) {
  615. size_t len = ML_KEM_SEED_BYTES;
  616. gctx->seed = gctx->seedbuf;
  617. if (OSSL_PARAM_get_octet_string(p, (void **)&gctx->seed, len, &len)
  618. && len == ML_KEM_SEED_BYTES)
  619. return 1;
  620. /* Possibly, but less likely wrong data type */
  621. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
  622. gctx->seed = NULL;
  623. return 0;
  624. }
  625. return 1;
  626. }
  627. static void *ml_kem_gen_init(void *provctx, int selection,
  628. const OSSL_PARAM params[], int evp_type)
  629. {
  630. PROV_ML_KEM_GEN_CTX *gctx = NULL;
  631. /*
  632. * We can only generate private keys, check that the selection is
  633. * appropriate.
  634. */
  635. if (!ossl_prov_is_running()
  636. || (selection & minimal_selection) == 0
  637. || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL)
  638. return NULL;
  639. gctx->selection = selection;
  640. gctx->evp_type = evp_type;
  641. gctx->provctx = provctx;
  642. if (ml_kem_gen_set_params(gctx, params))
  643. return gctx;
  644. ml_kem_gen_cleanup(gctx);
  645. return NULL;
  646. }
  647. static const OSSL_PARAM *ml_kem_gen_settable_params(ossl_unused void *vgctx,
  648. ossl_unused void *provctx)
  649. {
  650. static OSSL_PARAM settable[] = {
  651. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ML_KEM_SEED, NULL, 0),
  652. OSSL_PARAM_END
  653. };
  654. return settable;
  655. }
  656. static void *ml_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg)
  657. {
  658. PROV_ML_KEM_GEN_CTX *gctx = vgctx;
  659. ML_KEM_KEY *key;
  660. uint8_t *nopub = NULL;
  661. uint8_t *seed;
  662. int genok = 0;
  663. if (gctx == NULL
  664. || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) ==
  665. OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
  666. return NULL;
  667. seed = gctx->seed;
  668. key = ossl_prov_ml_kem_new(gctx->provctx, gctx->propq, gctx->evp_type);
  669. if (key == NULL)
  670. return NULL;
  671. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  672. return key;
  673. if (seed != NULL && !ossl_ml_kem_set_seed(seed, ML_KEM_SEED_BYTES, key))
  674. return NULL;
  675. genok = ossl_ml_kem_genkey(nopub, 0, key);
  676. /* Erase the single-use seed */
  677. if (seed != NULL)
  678. OPENSSL_cleanse(seed, ML_KEM_SEED_BYTES);
  679. gctx->seed = NULL;
  680. if (genok) {
  681. #ifdef FIPS_MODULE
  682. if (!ml_kem_pairwise_test(key, ML_KEM_KEY_FIXED_PCT)) {
  683. ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
  684. ossl_ml_kem_key_free(key);
  685. return NULL;
  686. }
  687. #endif /* FIPS_MODULE */
  688. return key;
  689. }
  690. ossl_ml_kem_key_free(key);
  691. return NULL;
  692. }
  693. static void ml_kem_gen_cleanup(void *vgctx)
  694. {
  695. PROV_ML_KEM_GEN_CTX *gctx = vgctx;
  696. if (gctx == NULL)
  697. return;
  698. if (gctx->seed != NULL)
  699. OPENSSL_cleanse(gctx->seed, ML_KEM_RANDOM_BYTES);
  700. OPENSSL_free(gctx->propq);
  701. OPENSSL_free(gctx);
  702. }
  703. static void *ml_kem_dup(const void *vkey, int selection)
  704. {
  705. const ML_KEM_KEY *key = vkey;
  706. if (!ossl_prov_is_running())
  707. return NULL;
  708. return ossl_ml_kem_key_dup(key, selection);
  709. }
  710. #ifndef FIPS_MODULE
  711. # define DISPATCH_LOAD_FN \
  712. { OSSL_FUNC_KEYMGMT_LOAD, (OSSL_FUNC) ml_kem_load },
  713. #else
  714. # define DISPATCH_LOAD_FN /* Non-FIPS only */
  715. #endif
  716. #define DECLARE_VARIANT(bits) \
  717. static void *ml_kem_##bits##_new(void *provctx) \
  718. { \
  719. return ossl_prov_ml_kem_new(provctx, NULL, EVP_PKEY_ML_KEM_##bits); \
  720. } \
  721. static void *ml_kem_##bits##_gen_init(void *provctx, int selection, \
  722. const OSSL_PARAM params[]) \
  723. { \
  724. return ml_kem_gen_init(provctx, selection, params, \
  725. EVP_PKEY_ML_KEM_##bits); \
  726. } \
  727. const OSSL_DISPATCH ossl_ml_kem_##bits##_keymgmt_functions[] = { \
  728. { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC) ml_kem_##bits##_new }, \
  729. { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC) ossl_ml_kem_key_free }, \
  730. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC) ml_kem_get_params }, \
  731. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gettable_params }, \
  732. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC) ml_kem_set_params }, \
  733. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_settable_params }, \
  734. { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC) ml_kem_has }, \
  735. { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC) ml_kem_match }, \
  736. { OSSL_FUNC_KEYMGMT_VALIDATE, (OSSL_FUNC) ml_kem_validate }, \
  737. { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC) ml_kem_##bits##_gen_init }, \
  738. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC) ml_kem_gen_set_params }, \
  739. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC) ml_kem_gen_settable_params }, \
  740. { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC) ml_kem_gen }, \
  741. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC) ml_kem_gen_cleanup }, \
  742. DISPATCH_LOAD_FN \
  743. { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC) ml_kem_dup }, \
  744. { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC) ml_kem_import }, \
  745. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \
  746. { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC) ml_kem_export }, \
  747. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC) ml_kem_imexport_types }, \
  748. OSSL_DISPATCH_END \
  749. }
  750. DECLARE_VARIANT(512);
  751. DECLARE_VARIANT(768);
  752. DECLARE_VARIANT(1024);