mlx_kmgmt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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/err.h>
  12. #include <openssl/param_build.h>
  13. #include <openssl/params.h>
  14. #include <openssl/proverr.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/self_test.h>
  17. #include "internal/nelem.h"
  18. #include "internal/param_build_set.h"
  19. #include "prov/implementations.h"
  20. #include "prov/mlx_kem.h"
  21. #include "prov/provider_ctx.h"
  22. #include "prov/providercommon.h"
  23. #include "prov/securitycheck.h"
  24. static OSSL_FUNC_keymgmt_gen_fn mlx_kem_gen;
  25. static OSSL_FUNC_keymgmt_gen_cleanup_fn mlx_kem_gen_cleanup;
  26. static OSSL_FUNC_keymgmt_gen_set_params_fn mlx_kem_gen_set_params;
  27. static OSSL_FUNC_keymgmt_gen_settable_params_fn mlx_kem_gen_settable_params;
  28. static OSSL_FUNC_keymgmt_get_params_fn mlx_kem_get_params;
  29. static OSSL_FUNC_keymgmt_gettable_params_fn mlx_kem_gettable_params;
  30. static OSSL_FUNC_keymgmt_set_params_fn mlx_kem_set_params;
  31. static OSSL_FUNC_keymgmt_settable_params_fn mlx_kem_settable_params;
  32. static OSSL_FUNC_keymgmt_has_fn mlx_kem_has;
  33. static OSSL_FUNC_keymgmt_match_fn mlx_kem_match;
  34. static OSSL_FUNC_keymgmt_import_fn mlx_kem_import;
  35. static OSSL_FUNC_keymgmt_export_fn mlx_kem_export;
  36. static OSSL_FUNC_keymgmt_import_types_fn mlx_kem_imexport_types;
  37. static OSSL_FUNC_keymgmt_export_types_fn mlx_kem_imexport_types;
  38. static OSSL_FUNC_keymgmt_dup_fn mlx_kem_dup;
  39. static const int minimal_selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
  40. | OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  41. /* Must match DECLARE_DISPATCH invocations at the end of the file */
  42. static const ECDH_VINFO hybrid_vtable[] = {
  43. { "EC", "P-256", 65, 32, 32, 1, EVP_PKEY_ML_KEM_768 },
  44. { "EC", "P-384", 97, 48, 48, 1, EVP_PKEY_ML_KEM_1024 },
  45. #if !defined(OPENSSL_NO_ECX)
  46. { "X25519", NULL, 32, 32, 32, 0, EVP_PKEY_ML_KEM_768 },
  47. { "X448", NULL, 56, 56, 56, 0, EVP_PKEY_ML_KEM_1024 },
  48. #endif
  49. };
  50. typedef struct mlx_kem_gen_ctx_st {
  51. OSSL_LIB_CTX *libctx;
  52. char *propq;
  53. int selection;
  54. unsigned int evp_type;
  55. } PROV_ML_KEM_GEN_CTX;
  56. static void mlx_kem_key_free(void *vkey)
  57. {
  58. MLX_KEY *key = vkey;
  59. if (key == NULL)
  60. return;
  61. OPENSSL_free(key->propq);
  62. EVP_PKEY_free(key->mkey);
  63. EVP_PKEY_free(key->xkey);
  64. OPENSSL_free(key);
  65. }
  66. /* Takes ownership of propq */
  67. static void *
  68. mlx_kem_key_new(unsigned int v, OSSL_LIB_CTX *libctx, char *propq)
  69. {
  70. MLX_KEY *key = NULL;
  71. unsigned int ml_kem_variant;
  72. if (!ossl_prov_is_running()
  73. || v >= OSSL_NELEM(hybrid_vtable)
  74. || (key = OPENSSL_malloc(sizeof(*key))) == NULL)
  75. goto err;
  76. ml_kem_variant = hybrid_vtable[v].ml_kem_variant;
  77. key->libctx = libctx;
  78. key->minfo = ossl_ml_kem_get_vinfo(ml_kem_variant);
  79. key->xinfo = &hybrid_vtable[v];
  80. key->xkey = key->mkey = NULL;
  81. key->state = MLX_HAVE_NOKEYS;
  82. key->propq = propq;
  83. return key;
  84. err:
  85. OPENSSL_free(propq);
  86. return NULL;
  87. }
  88. static int mlx_kem_has(const void *vkey, int selection)
  89. {
  90. const MLX_KEY *key = vkey;
  91. /* A NULL key MUST fail to have anything */
  92. if (!ossl_prov_is_running() || key == NULL)
  93. return 0;
  94. switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
  95. case 0:
  96. return 1;
  97. case OSSL_KEYMGMT_SELECT_PUBLIC_KEY:
  98. return mlx_kem_have_pubkey(key);
  99. default:
  100. return mlx_kem_have_prvkey(key);
  101. }
  102. }
  103. static int mlx_kem_match(const void *vkey1, const void *vkey2, int selection)
  104. {
  105. const MLX_KEY *key1 = vkey1;
  106. const MLX_KEY *key2 = vkey2;
  107. int have_pub1 = mlx_kem_have_pubkey(key1);
  108. int have_pub2 = mlx_kem_have_pubkey(key2);
  109. if (!ossl_prov_is_running())
  110. return 0;
  111. /* Compare domain parameters */
  112. if (key1->xinfo != key2->xinfo)
  113. return 0;
  114. if (!(selection & OSSL_KEYMGMT_SELECT_KEYPAIR))
  115. return 1;
  116. if (have_pub1 ^ have_pub2)
  117. return 0;
  118. /* As in other providers, equal when both have no key material. */
  119. if (!have_pub1)
  120. return 1;
  121. return EVP_PKEY_eq(key1->mkey, key2->mkey)
  122. && EVP_PKEY_eq(key1->xkey, key2->xkey);
  123. }
  124. typedef struct export_cb_arg_st {
  125. const char *algorithm_name;
  126. uint8_t *pubenc;
  127. uint8_t *prvenc;
  128. int pubcount;
  129. int prvcount;
  130. size_t puboff;
  131. size_t prvoff;
  132. size_t publen;
  133. size_t prvlen;
  134. } EXPORT_CB_ARG;
  135. /* Copy any exported key material into its storage slot */
  136. static int export_sub_cb(const OSSL_PARAM *params, void *varg)
  137. {
  138. EXPORT_CB_ARG *sub_arg = varg;
  139. const OSSL_PARAM *p = NULL;
  140. size_t len;
  141. /*
  142. * The caller will decide whether anything essential is missing, but, if
  143. * some key material was returned, it should have the right (parameter)
  144. * data type and length.
  145. */
  146. if (ossl_param_is_empty(params))
  147. return 1;
  148. if (sub_arg->pubenc != NULL
  149. && (p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY)) != NULL) {
  150. void *pub = sub_arg->pubenc + sub_arg->puboff;
  151. if (OSSL_PARAM_get_octet_string(p, &pub, sub_arg->publen, &len) != 1)
  152. return 0;
  153. if (len != sub_arg->publen) {
  154. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  155. "Unexpected %s public key length %lu != %lu",
  156. sub_arg->algorithm_name, (unsigned long) len,
  157. sub_arg->publen);
  158. return 0;
  159. }
  160. ++sub_arg->pubcount;
  161. }
  162. if (sub_arg->prvenc != NULL
  163. && (p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY)) != NULL) {
  164. void *prv = sub_arg->prvenc + sub_arg->prvoff;
  165. if (OSSL_PARAM_get_octet_string(p, &prv, sub_arg->prvlen, &len) != 1)
  166. return 0;
  167. if (len != sub_arg->prvlen) {
  168. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  169. "Unexpected %s private key length %lu != %lu",
  170. sub_arg->algorithm_name, (unsigned long) len,
  171. (unsigned long) sub_arg->publen);
  172. return 0;
  173. }
  174. ++sub_arg->prvcount;
  175. }
  176. return 1;
  177. }
  178. static int
  179. export_sub(EXPORT_CB_ARG *sub_arg, int selection, MLX_KEY *key)
  180. {
  181. int slot;
  182. /*
  183. * The caller is responsible for initialising only the pubenc and prvenc
  184. * pointer fields, the rest are set here or in the callback.
  185. */
  186. sub_arg->pubcount = 0;
  187. sub_arg->prvcount = 0;
  188. for (slot = 0; slot < 2; ++slot) {
  189. int ml_kem_slot = key->xinfo->ml_kem_slot;
  190. EVP_PKEY *pkey;
  191. /* Export the parts of each component into its storage slot */
  192. if (slot == ml_kem_slot) {
  193. pkey = key->mkey;
  194. sub_arg->algorithm_name = key->minfo->algorithm_name;
  195. sub_arg->puboff = slot * key->xinfo->pubkey_bytes;
  196. sub_arg->prvoff = slot * key->xinfo->prvkey_bytes;
  197. sub_arg->publen = key->minfo->pubkey_bytes;
  198. sub_arg->prvlen = key->minfo->prvkey_bytes;
  199. } else {
  200. pkey = key->xkey;
  201. sub_arg->algorithm_name = key->xinfo->algorithm_name;
  202. sub_arg->puboff = (1 - ml_kem_slot) * key->minfo->pubkey_bytes;
  203. sub_arg->prvoff = (1 - ml_kem_slot) * key->minfo->prvkey_bytes;
  204. sub_arg->publen = key->xinfo->pubkey_bytes;
  205. sub_arg->prvlen = key->xinfo->prvkey_bytes;
  206. }
  207. if (!EVP_PKEY_export(pkey, selection, export_sub_cb, (void *)sub_arg))
  208. return 0;
  209. }
  210. return 1;
  211. }
  212. static int mlx_kem_export(void *vkey, int selection, OSSL_CALLBACK *param_cb,
  213. void *cbarg)
  214. {
  215. MLX_KEY *key = vkey;
  216. OSSL_PARAM_BLD *tmpl = NULL;
  217. OSSL_PARAM *params = NULL;
  218. size_t publen;
  219. size_t prvlen;
  220. int ret = 0;
  221. EXPORT_CB_ARG sub_arg;
  222. if (!ossl_prov_is_running() || key == NULL)
  223. return 0;
  224. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  225. return 0;
  226. /* Fail when no key material has yet been provided */
  227. if (!mlx_kem_have_pubkey(key)) {
  228. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  229. return 0;
  230. }
  231. publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
  232. prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
  233. memset(&sub_arg, 0, sizeof(sub_arg));
  234. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  235. sub_arg.pubenc = OPENSSL_malloc(publen);
  236. if (sub_arg.pubenc == NULL)
  237. goto err;
  238. }
  239. if (mlx_kem_have_prvkey(key)
  240. && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  241. /*
  242. * Allocated on the secure heap if configured, this is detected in
  243. * ossl_param_build_set_octet_string(), which will then also use the
  244. * secure heap.
  245. */
  246. sub_arg.prvenc = OPENSSL_secure_zalloc(prvlen);
  247. if (sub_arg.prvenc == NULL)
  248. goto err;
  249. }
  250. tmpl = OSSL_PARAM_BLD_new();
  251. if (tmpl == NULL)
  252. goto err;
  253. /* Extract sub-component key material */
  254. if (!export_sub(&sub_arg, selection, key))
  255. goto err;
  256. if (sub_arg.pubenc != NULL && sub_arg.pubcount == 2
  257. && !ossl_param_build_set_octet_string(
  258. tmpl, NULL, OSSL_PKEY_PARAM_PUB_KEY, sub_arg.pubenc, publen))
  259. goto err;
  260. if (sub_arg.prvenc != NULL && sub_arg.prvcount == 2
  261. && !ossl_param_build_set_octet_string(
  262. tmpl, NULL, OSSL_PKEY_PARAM_PRIV_KEY, sub_arg.prvenc, prvlen))
  263. goto err;
  264. params = OSSL_PARAM_BLD_to_param(tmpl);
  265. if (params == NULL)
  266. goto err;
  267. ret = param_cb(params, cbarg);
  268. OSSL_PARAM_free(params);
  269. err:
  270. OSSL_PARAM_BLD_free(tmpl);
  271. OPENSSL_secure_clear_free(sub_arg.prvenc, prvlen);
  272. OPENSSL_free(sub_arg.pubenc);
  273. return ret;
  274. }
  275. static const OSSL_PARAM *mlx_kem_imexport_types(int selection)
  276. {
  277. static const OSSL_PARAM key_types[] = {
  278. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
  279. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
  280. OSSL_PARAM_END
  281. };
  282. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  283. return key_types;
  284. return NULL;
  285. }
  286. static int
  287. load_slot(OSSL_LIB_CTX *libctx, const char *propq, const char *pname,
  288. int selection, MLX_KEY *key, int slot, const uint8_t *in,
  289. int mbytes, int xbytes)
  290. {
  291. EVP_PKEY_CTX *ctx;
  292. EVP_PKEY **ppkey;
  293. OSSL_PARAM parr[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  294. const char *alg;
  295. char *group = NULL;
  296. size_t off, len;
  297. void *val;
  298. int ml_kem_slot = key->xinfo->ml_kem_slot;
  299. int ret = 0;
  300. if (slot == ml_kem_slot) {
  301. alg = key->minfo->algorithm_name;
  302. ppkey = &key->mkey;
  303. off = slot * xbytes;
  304. len = mbytes;
  305. } else {
  306. alg = key->xinfo->algorithm_name;
  307. group = (char *) key->xinfo->group_name;
  308. ppkey = &key->xkey;
  309. off = (1 - ml_kem_slot) * mbytes;
  310. len = xbytes;
  311. }
  312. val = (void *)(in + off);
  313. if ((ctx = EVP_PKEY_CTX_new_from_name(libctx, alg, propq)) == NULL
  314. || EVP_PKEY_fromdata_init(ctx) <= 0)
  315. goto err;
  316. parr[0] = OSSL_PARAM_construct_octet_string(pname, val, len);
  317. if (group != NULL)
  318. parr[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  319. group, 0);
  320. if (EVP_PKEY_fromdata(ctx, ppkey, selection, parr) > 0)
  321. ret = 1;
  322. err:
  323. EVP_PKEY_CTX_free(ctx);
  324. return ret;
  325. }
  326. static int
  327. load_keys(MLX_KEY *key,
  328. const uint8_t *pubenc, size_t publen,
  329. const uint8_t *prvenc, size_t prvlen)
  330. {
  331. int slot;
  332. for (slot = 0; slot < 2; ++slot) {
  333. if (prvlen) {
  334. /* Ignore public keys when private provided */
  335. if (!load_slot(key->libctx, key->propq, OSSL_PKEY_PARAM_PRIV_KEY,
  336. minimal_selection, key, slot, prvenc,
  337. key->minfo->prvkey_bytes, key->xinfo->prvkey_bytes))
  338. goto err;
  339. } else if (publen) {
  340. /* Absent private key data, import public keys */
  341. if (!load_slot(key->libctx, key->propq, OSSL_PKEY_PARAM_PUB_KEY,
  342. minimal_selection, key, slot, pubenc,
  343. key->minfo->pubkey_bytes, key->xinfo->pubkey_bytes))
  344. goto err;
  345. }
  346. }
  347. key->state = prvlen ? MLX_HAVE_PRVKEY : MLX_HAVE_PUBKEY;
  348. return 1;
  349. err:
  350. EVP_PKEY_free(key->mkey);
  351. EVP_PKEY_free(key->xkey);
  352. key->xkey = key->mkey = NULL;
  353. key->state = MLX_HAVE_NOKEYS;
  354. return 0;
  355. }
  356. static int mlx_kem_key_fromdata(MLX_KEY *key,
  357. const OSSL_PARAM params[],
  358. int include_private)
  359. {
  360. const OSSL_PARAM *param_prv_key = NULL, *param_pub_key;
  361. const void *pubenc = NULL, *prvenc = NULL;
  362. size_t pubkey_bytes, prvkey_bytes;
  363. size_t publen = 0, prvlen = 0;
  364. /* Invalid attempt to mutate a key, what is the right error to report? */
  365. if (key == NULL || mlx_kem_have_pubkey(key))
  366. return 0;
  367. pubkey_bytes = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
  368. prvkey_bytes = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
  369. /* What does the caller want to set? */
  370. param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  371. if (param_pub_key != NULL &&
  372. OSSL_PARAM_get_octet_string_ptr(param_pub_key, &pubenc, &publen) != 1)
  373. return 0;
  374. if (include_private)
  375. param_prv_key = OSSL_PARAM_locate_const(params,
  376. OSSL_PKEY_PARAM_PRIV_KEY);
  377. if (param_prv_key != NULL &&
  378. OSSL_PARAM_get_octet_string_ptr(param_prv_key, &prvenc, &prvlen) != 1)
  379. return 0;
  380. /* The caller MUST specify at least one of the public or private keys. */
  381. if (publen == 0 && prvlen == 0) {
  382. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  383. return 0;
  384. }
  385. /*
  386. * When a pubkey is provided, its length MUST be correct, if a private key
  387. * is also provided, the public key will be otherwise ignored. We could
  388. * look for a matching encoded block, but unclear this is useful.
  389. */
  390. if (publen != 0 && publen != pubkey_bytes) {
  391. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  392. return 0;
  393. }
  394. if (prvlen != 0 && prvlen != prvkey_bytes) {
  395. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  396. return 0;
  397. }
  398. return load_keys(key, pubenc, publen, prvenc, prvlen);
  399. }
  400. static int mlx_kem_import(void *vkey, int selection, const OSSL_PARAM params[])
  401. {
  402. MLX_KEY *key = vkey;
  403. int include_private;
  404. if (!ossl_prov_is_running() || key == NULL)
  405. return 0;
  406. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  407. return 0;
  408. include_private = selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  409. return mlx_kem_key_fromdata(key, params, include_private);
  410. }
  411. static const OSSL_PARAM *mlx_kem_gettable_params(void *provctx)
  412. {
  413. static const OSSL_PARAM arr[] = {
  414. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  415. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  416. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  417. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  418. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
  419. OSSL_PARAM_END
  420. };
  421. return arr;
  422. }
  423. /*
  424. * It is assumed the key is guaranteed non-NULL here, and is from this provider
  425. */
  426. static int mlx_kem_get_params(void *vkey, OSSL_PARAM params[])
  427. {
  428. MLX_KEY *key = vkey;
  429. OSSL_PARAM *p, *pub, *prv = NULL;
  430. EXPORT_CB_ARG sub_arg;
  431. int selection;
  432. size_t publen = key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes;
  433. size_t prvlen = key->minfo->prvkey_bytes + key->xinfo->prvkey_bytes;
  434. /* The reported "bit" count is those of the ML-KEM key */
  435. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS);
  436. if (p != NULL)
  437. if (!OSSL_PARAM_set_int(p, key->minfo->bits))
  438. return 0;
  439. /* The reported security bits are those of the ML-KEM key */
  440. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS);
  441. if (p != NULL)
  442. if (!OSSL_PARAM_set_int(p, key->minfo->secbits))
  443. return 0;
  444. /* The ciphertext sizes are additive */
  445. p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE);
  446. if (p != NULL)
  447. if (!OSSL_PARAM_set_int(p, key->minfo->ctext_bytes + key->xinfo->pubkey_bytes))
  448. return 0;
  449. if (!mlx_kem_have_pubkey(key))
  450. return 1;
  451. memset(&sub_arg, 0, sizeof(sub_arg));
  452. pub = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
  453. if (pub != NULL) {
  454. if (pub->data_type != OSSL_PARAM_OCTET_STRING)
  455. return 0;
  456. pub->return_size = publen;
  457. if (pub->data == NULL) {
  458. pub = NULL;
  459. } else if (pub->data_size < publen) {
  460. ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
  461. "public key output buffer too short: %lu < %lu",
  462. (unsigned long) pub->data_size,
  463. (unsigned long) publen);
  464. return 0;
  465. } else {
  466. sub_arg.pubenc = pub->data;
  467. }
  468. }
  469. if (mlx_kem_have_prvkey(key)) {
  470. prv = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_PRIV_KEY);
  471. if (prv != NULL) {
  472. if (prv->data_type != OSSL_PARAM_OCTET_STRING)
  473. return 0;
  474. prv->return_size = prvlen;
  475. if (prv->data == NULL) {
  476. prv = NULL;
  477. } else if (prv->data_size < prvlen) {
  478. ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
  479. "private key output buffer too short: %lu < %lu",
  480. (unsigned long) prv->data_size,
  481. (unsigned long) prvlen);
  482. return 0;
  483. } else {
  484. sub_arg.prvenc = prv->data;
  485. }
  486. }
  487. }
  488. if (pub == NULL && prv == NULL)
  489. return 1;
  490. selection = prv == NULL ? 0 : OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  491. selection |= pub == NULL ? 0 : OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  492. if (key->xinfo->group_name != NULL)
  493. selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
  494. /* Extract sub-component key material */
  495. if (!export_sub(&sub_arg, selection, key))
  496. return 0;
  497. if ((pub != NULL && sub_arg.pubcount != 2)
  498. || (prv != NULL && sub_arg.prvcount != 2))
  499. return 0;
  500. return 1;
  501. }
  502. static const OSSL_PARAM *mlx_kem_settable_params(void *provctx)
  503. {
  504. static const OSSL_PARAM arr[] = {
  505. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  506. OSSL_PARAM_END
  507. };
  508. return arr;
  509. }
  510. static int mlx_kem_set_params(void *vkey, const OSSL_PARAM params[])
  511. {
  512. MLX_KEY *key = vkey;
  513. const OSSL_PARAM *p;
  514. const void *pubenc = NULL;
  515. size_t publen = 0;
  516. if (ossl_param_is_empty(params))
  517. return 1;
  518. /* Only one settable parameter is supported */
  519. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
  520. if (p == NULL)
  521. return 1;
  522. /* Key mutation is reportedly generally not allowed */
  523. if (mlx_kem_have_pubkey(key)) {
  524. ERR_raise_data(ERR_LIB_PROV,
  525. PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE,
  526. "keys cannot be mutated");
  527. return 0;
  528. }
  529. /* An unlikely failure mode is the parameter having some unexpected type */
  530. if (!OSSL_PARAM_get_octet_string_ptr(p, &pubenc, &publen))
  531. return 0;
  532. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
  533. if (p != NULL) {
  534. OPENSSL_free(key->propq);
  535. key->propq = NULL;
  536. if (!OSSL_PARAM_get_utf8_string(p, &key->propq, 0))
  537. return 0;
  538. }
  539. if (publen != key->minfo->pubkey_bytes + key->xinfo->pubkey_bytes) {
  540. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
  541. return 0;
  542. }
  543. return load_keys(key, pubenc, publen, NULL, 0);
  544. }
  545. static int mlx_kem_gen_set_params(void *vgctx, const OSSL_PARAM params[])
  546. {
  547. PROV_ML_KEM_GEN_CTX *gctx = vgctx;
  548. const OSSL_PARAM *p;
  549. if (gctx == NULL)
  550. return 0;
  551. if (ossl_param_is_empty(params))
  552. return 1;
  553. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PROPERTIES);
  554. if (p != NULL) {
  555. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  556. return 0;
  557. OPENSSL_free(gctx->propq);
  558. if ((gctx->propq = OPENSSL_strdup(p->data)) == NULL)
  559. return 0;
  560. }
  561. return 1;
  562. }
  563. static void *mlx_kem_gen_init(int evp_type, OSSL_LIB_CTX *libctx,
  564. int selection, const OSSL_PARAM params[])
  565. {
  566. PROV_ML_KEM_GEN_CTX *gctx = NULL;
  567. /*
  568. * We can only generate private keys, check that the selection is
  569. * appropriate.
  570. */
  571. if (!ossl_prov_is_running()
  572. || (selection & minimal_selection) == 0
  573. || (gctx = OPENSSL_zalloc(sizeof(*gctx))) == NULL)
  574. return NULL;
  575. gctx->evp_type = evp_type;
  576. gctx->libctx = libctx;
  577. gctx->selection = selection;
  578. if (mlx_kem_gen_set_params(gctx, params))
  579. return gctx;
  580. mlx_kem_gen_cleanup(gctx);
  581. return NULL;
  582. }
  583. static const OSSL_PARAM *mlx_kem_gen_settable_params(ossl_unused void *vgctx,
  584. ossl_unused void *provctx)
  585. {
  586. static OSSL_PARAM settable[] = {
  587. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PROPERTIES, NULL, 0),
  588. OSSL_PARAM_END
  589. };
  590. return settable;
  591. }
  592. static void *mlx_kem_gen(void *vgctx, OSSL_CALLBACK *osslcb, void *cbarg)
  593. {
  594. PROV_ML_KEM_GEN_CTX *gctx = vgctx;
  595. MLX_KEY *key;
  596. char *propq;
  597. if (gctx == NULL
  598. || (gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) ==
  599. OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
  600. return NULL;
  601. /* Lose ownership of propq */
  602. propq = gctx->propq;
  603. gctx->propq = NULL;
  604. if ((key = mlx_kem_key_new(gctx->evp_type, gctx->libctx, propq)) == NULL)
  605. return NULL;
  606. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  607. return key;
  608. /* For now, using the same "propq" for all components */
  609. key->mkey = EVP_PKEY_Q_keygen(key->libctx, key->propq,
  610. key->minfo->algorithm_name);
  611. key->xkey = EVP_PKEY_Q_keygen(key->libctx, key->propq,
  612. key->xinfo->algorithm_name,
  613. key->xinfo->group_name);
  614. if (key->mkey != NULL && key->xkey != NULL) {
  615. key->state = MLX_HAVE_PRVKEY;
  616. return key;
  617. }
  618. mlx_kem_key_free(key);
  619. return NULL;
  620. }
  621. static void mlx_kem_gen_cleanup(void *vgctx)
  622. {
  623. PROV_ML_KEM_GEN_CTX *gctx = vgctx;
  624. if (gctx == NULL)
  625. return;
  626. OPENSSL_free(gctx->propq);
  627. OPENSSL_free(gctx);
  628. }
  629. static void *mlx_kem_dup(const void *vkey, int selection)
  630. {
  631. const MLX_KEY *key = vkey;
  632. MLX_KEY *ret;
  633. if (!ossl_prov_is_running()
  634. || (ret = OPENSSL_memdup(key, sizeof(*ret))) == NULL)
  635. return NULL;
  636. if (ret->propq != NULL
  637. && (ret->propq = OPENSSL_strdup(ret->propq)) == NULL) {
  638. OPENSSL_free(ret);
  639. return NULL;
  640. }
  641. /* Absent key material, nothing left to do */
  642. if (ret->mkey == NULL) {
  643. if (ret->xkey == NULL)
  644. return ret;
  645. /* Fail if the source key is an inconsistent state */
  646. OPENSSL_free(ret);
  647. return NULL;
  648. }
  649. switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
  650. case 0:
  651. ret->xkey = ret->mkey = NULL;
  652. return ret;
  653. case OSSL_KEYMGMT_SELECT_KEYPAIR:
  654. ret->mkey = EVP_PKEY_dup(key->mkey);
  655. ret->xkey = EVP_PKEY_dup(key->xkey);
  656. if (ret->xkey != NULL && ret->mkey != NULL)
  657. return ret;
  658. break;
  659. default:
  660. ERR_raise_data(ERR_LIB_PROV, PROV_R_UNSUPPORTED_SELECTION,
  661. "duplication of partial key material not supported");
  662. break;
  663. }
  664. mlx_kem_key_free(ret);
  665. return NULL;
  666. }
  667. #define DECLARE_DISPATCH(name, variant) \
  668. static OSSL_FUNC_keymgmt_new_fn mlx_##name##_kem_new; \
  669. static void *mlx_##name##_kem_new(void *provctx) \
  670. { \
  671. OSSL_LIB_CTX *libctx; \
  672. \
  673. libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx); \
  674. return mlx_kem_key_new(variant, libctx, NULL); \
  675. } \
  676. static OSSL_FUNC_keymgmt_gen_init_fn mlx_##name##_kem_gen_init; \
  677. static void *mlx_##name##_kem_gen_init(void *provctx, int selection, \
  678. const OSSL_PARAM params[]) \
  679. { \
  680. OSSL_LIB_CTX *libctx; \
  681. \
  682. libctx = provctx == NULL ? NULL : PROV_LIBCTX_OF(provctx); \
  683. return mlx_kem_gen_init(variant, libctx, selection, params); \
  684. } \
  685. const OSSL_DISPATCH ossl_mlx_##name##_kem_kmgmt_functions[] = { \
  686. { OSSL_FUNC_KEYMGMT_NEW, (OSSL_FUNC) mlx_##name##_kem_new }, \
  687. { OSSL_FUNC_KEYMGMT_FREE, (OSSL_FUNC) mlx_kem_key_free }, \
  688. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (OSSL_FUNC) mlx_kem_get_params }, \
  689. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_gettable_params }, \
  690. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (OSSL_FUNC) mlx_kem_set_params }, \
  691. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_settable_params }, \
  692. { OSSL_FUNC_KEYMGMT_HAS, (OSSL_FUNC) mlx_kem_has }, \
  693. { OSSL_FUNC_KEYMGMT_MATCH, (OSSL_FUNC) mlx_kem_match }, \
  694. { OSSL_FUNC_KEYMGMT_GEN_INIT, (OSSL_FUNC) mlx_##name##_kem_gen_init }, \
  695. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (OSSL_FUNC) mlx_kem_gen_set_params }, \
  696. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, (OSSL_FUNC) mlx_kem_gen_settable_params }, \
  697. { OSSL_FUNC_KEYMGMT_GEN, (OSSL_FUNC) mlx_kem_gen }, \
  698. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (OSSL_FUNC) mlx_kem_gen_cleanup }, \
  699. { OSSL_FUNC_KEYMGMT_DUP, (OSSL_FUNC) mlx_kem_dup }, \
  700. { OSSL_FUNC_KEYMGMT_IMPORT, (OSSL_FUNC) mlx_kem_import }, \
  701. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (OSSL_FUNC) mlx_kem_imexport_types }, \
  702. { OSSL_FUNC_KEYMGMT_EXPORT, (OSSL_FUNC) mlx_kem_export }, \
  703. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (OSSL_FUNC) mlx_kem_imexport_types }, \
  704. OSSL_DISPATCH_END \
  705. }
  706. /* See |hybrid_vtable| above */
  707. DECLARE_DISPATCH(p256, 0);
  708. DECLARE_DISPATCH(p384, 1);
  709. #if !defined(OPENSSL_NO_ECX)
  710. DECLARE_DISPATCH(x25519, 2);
  711. DECLARE_DISPATCH(x448, 3);
  712. #endif