ml_dsa_key.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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/params.h>
  13. #include <openssl/proverr.h>
  14. #include <openssl/rand.h>
  15. #include "ml_dsa_key.h"
  16. #include "ml_dsa_matrix.h"
  17. #include "ml_dsa_hash.h"
  18. #include "internal/encoder.h"
  19. const ML_DSA_PARAMS *ossl_ml_dsa_key_params(const ML_DSA_KEY *key)
  20. {
  21. return key->params;
  22. }
  23. /* Returns the seed data or NULL if there is no seed */
  24. const uint8_t *ossl_ml_dsa_key_get_seed(const ML_DSA_KEY *key)
  25. {
  26. return key->seed;
  27. }
  28. int ossl_ml_dsa_key_get_prov_flags(const ML_DSA_KEY *key)
  29. {
  30. return key->prov_flags;
  31. }
  32. int ossl_ml_dsa_set_prekey(ML_DSA_KEY *key, int flags_set, int flags_clr,
  33. const uint8_t *seed, size_t seed_len,
  34. const uint8_t *sk, size_t sk_len)
  35. {
  36. int ret = 0;
  37. if (key == NULL
  38. || key->pub_encoding != NULL
  39. || key->priv_encoding != NULL
  40. || (sk != NULL && sk_len != key->params->sk_len)
  41. || (seed != NULL && seed_len != ML_DSA_SEED_BYTES)
  42. || key->seed != NULL)
  43. return 0;
  44. if (sk != NULL
  45. && (key->priv_encoding = OPENSSL_memdup(sk, sk_len)) == NULL)
  46. goto end;
  47. if (seed != NULL
  48. && (key->seed = OPENSSL_memdup(seed, seed_len)) == NULL)
  49. goto end;
  50. key->prov_flags |= flags_set;
  51. key->prov_flags &= ~flags_clr;
  52. ret = 1;
  53. end:
  54. if (!ret) {
  55. OPENSSL_free(key->priv_encoding);
  56. OPENSSL_free(key->seed);
  57. key->priv_encoding = key->seed = NULL;
  58. }
  59. return ret;
  60. }
  61. /**
  62. * @brief Create a new ML_DSA_KEY object
  63. *
  64. * @param libctx A OSSL_LIB_CTX object used for fetching algorithms.
  65. * @param propq The property query used for fetching algorithms
  66. * @param alg The algorithm name associated with the key type
  67. * @returns The new ML_DSA_KEY object on success, or NULL on malloc failure
  68. */
  69. ML_DSA_KEY *ossl_ml_dsa_key_new(OSSL_LIB_CTX *libctx, const char *propq,
  70. int evp_type)
  71. {
  72. ML_DSA_KEY *ret;
  73. const ML_DSA_PARAMS *params = ossl_ml_dsa_params_get(evp_type);
  74. if (params == NULL)
  75. return NULL;
  76. ret = OPENSSL_zalloc(sizeof(*ret));
  77. if (ret != NULL) {
  78. ret->libctx = libctx;
  79. ret->params = params;
  80. ret->prov_flags = ML_DSA_KEY_PROV_FLAGS_DEFAULT;
  81. ret->shake128_md = EVP_MD_fetch(libctx, "SHAKE-128", propq);
  82. ret->shake256_md = EVP_MD_fetch(libctx, "SHAKE-256", propq);
  83. if (ret->shake128_md == NULL || ret->shake256_md == NULL)
  84. goto err;
  85. }
  86. return ret;
  87. err:
  88. ossl_ml_dsa_key_free(ret);
  89. return NULL;
  90. }
  91. int ossl_ml_dsa_key_pub_alloc(ML_DSA_KEY *key)
  92. {
  93. if (key->t1.poly != NULL)
  94. return 0;
  95. return vector_alloc(&key->t1, key->params->k);
  96. }
  97. int ossl_ml_dsa_key_priv_alloc(ML_DSA_KEY *key)
  98. {
  99. size_t k = key->params->k, l = key->params->l;
  100. POLY *poly;
  101. if (key->s1.poly != NULL)
  102. return 0;
  103. if (!vector_alloc(&key->s1, l + 2 * k))
  104. return 0;
  105. poly = key->s1.poly;
  106. key->s1.num_poly = l;
  107. vector_init(&key->s2, poly + l, k);
  108. vector_init(&key->t0, poly + l + k, k);
  109. return 1;
  110. }
  111. /**
  112. * @brief Destroy an ML_DSA_KEY object
  113. */
  114. void ossl_ml_dsa_key_free(ML_DSA_KEY *key)
  115. {
  116. if (key == NULL)
  117. return;
  118. EVP_MD_free(key->shake128_md);
  119. EVP_MD_free(key->shake256_md);
  120. ossl_ml_dsa_key_reset(key);
  121. OPENSSL_free(key);
  122. }
  123. /**
  124. * @brief Factory reset an ML_DSA_KEY object
  125. */
  126. void ossl_ml_dsa_key_reset(ML_DSA_KEY *key)
  127. {
  128. /*
  129. * The allocation for |s1.poly| subsumes those for |s2| and |t0|, which we
  130. * must not access after |s1|'s poly is freed.
  131. */
  132. if (key->s1.poly != NULL) {
  133. vector_zero(&key->s1);
  134. vector_zero(&key->s2);
  135. vector_zero(&key->t0);
  136. vector_free(&key->s1);
  137. key->s2.poly = NULL;
  138. key->t0.poly = NULL;
  139. }
  140. /* The |t1| vector is public and allocated separately */
  141. vector_free(&key->t1);
  142. OPENSSL_cleanse(key->K, sizeof(key->K));
  143. OPENSSL_free(key->pub_encoding);
  144. key->pub_encoding = NULL;
  145. if (key->priv_encoding != NULL)
  146. OPENSSL_clear_free(key->priv_encoding, key->params->sk_len);
  147. key->priv_encoding = NULL;
  148. if (key->seed != NULL)
  149. OPENSSL_clear_free(key->seed, ML_DSA_SEED_BYTES);
  150. key->seed = NULL;
  151. }
  152. /**
  153. * @brief Duplicate a key
  154. *
  155. * @param src A ML_DSA_KEY object to copy
  156. * @param selection to select public and/or private components. Selecting the
  157. * private key will also select the public key
  158. * @returns The duplicated key, or NULL on failure.
  159. */
  160. ML_DSA_KEY *ossl_ml_dsa_key_dup(const ML_DSA_KEY *src, int selection)
  161. {
  162. ML_DSA_KEY *ret = NULL;
  163. if (src == NULL)
  164. return NULL;
  165. /* Prekeys with just a seed or private key are not dupable */
  166. if (src->pub_encoding == NULL
  167. && (src->priv_encoding != NULL || src->seed != NULL))
  168. return NULL;
  169. ret = OPENSSL_zalloc(sizeof(*ret));
  170. if (ret != NULL) {
  171. ret->libctx = src->libctx;
  172. ret->params = src->params;
  173. ret->prov_flags = src->prov_flags;
  174. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  175. if (src->pub_encoding != NULL) {
  176. /* The public components are present if the private key is present */
  177. memcpy(ret->rho, src->rho, sizeof(src->rho));
  178. memcpy(ret->tr, src->tr, sizeof(src->tr));
  179. if (src->t1.poly != NULL) {
  180. if (!ossl_ml_dsa_key_pub_alloc(ret))
  181. goto err;
  182. vector_copy(&ret->t1, &src->t1);
  183. }
  184. if ((ret->pub_encoding = OPENSSL_memdup(src->pub_encoding,
  185. src->params->pk_len)) == NULL)
  186. goto err;
  187. }
  188. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  189. if (src->priv_encoding != NULL) {
  190. memcpy(ret->K, src->K, sizeof(src->K));
  191. if (src->s1.poly != NULL) {
  192. if (!ossl_ml_dsa_key_priv_alloc(ret))
  193. goto err;
  194. vector_copy(&ret->s1, &src->s1);
  195. vector_copy(&ret->s2, &src->s2);
  196. vector_copy(&ret->t0, &src->t0);
  197. }
  198. if ((ret->priv_encoding =
  199. OPENSSL_memdup(src->priv_encoding,
  200. src->params->sk_len)) == NULL)
  201. goto err;
  202. }
  203. if (src->seed != NULL
  204. && (ret->seed = OPENSSL_memdup(src->seed,
  205. ML_DSA_SEED_BYTES)) == NULL)
  206. goto err;
  207. }
  208. }
  209. EVP_MD_up_ref(src->shake128_md);
  210. EVP_MD_up_ref(src->shake256_md);
  211. ret->shake128_md = src->shake128_md;
  212. ret->shake256_md = src->shake256_md;
  213. }
  214. return ret;
  215. err:
  216. ossl_ml_dsa_key_free(ret);
  217. return NULL;
  218. }
  219. /**
  220. * @brief Are 2 keys equal?
  221. *
  222. * To be equal the keys must have matching public or private key data and
  223. * contain the same parameters.
  224. * (Note that in OpenSSL that the private key always has a public key component).
  225. *
  226. * @param key1 A ML_DSA_KEY object
  227. * @param key2 A ML_DSA_KEY object
  228. * @param selection to select public and/or private component comparison.
  229. * @returns 1 if the keys are equal otherwise it returns 0.
  230. */
  231. int ossl_ml_dsa_key_equal(const ML_DSA_KEY *key1, const ML_DSA_KEY *key2,
  232. int selection)
  233. {
  234. int key_checked = 0;
  235. if (key1->params != key2->params)
  236. return 0;
  237. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  238. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  239. if (key1->pub_encoding != NULL && key2->pub_encoding != NULL) {
  240. if (memcmp(key1->pub_encoding, key2->pub_encoding,
  241. key1->params->pk_len) != 0)
  242. return 0;
  243. key_checked = 1;
  244. }
  245. }
  246. if (!key_checked
  247. && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  248. if (key1->priv_encoding != NULL && key2->priv_encoding != NULL) {
  249. if (memcmp(key1->priv_encoding, key2->priv_encoding,
  250. key1->params->sk_len) != 0)
  251. return 0;
  252. key_checked = 1;
  253. }
  254. }
  255. return key_checked;
  256. }
  257. return 1;
  258. }
  259. int ossl_ml_dsa_key_has(const ML_DSA_KEY *key, int selection)
  260. {
  261. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  262. /* Note that the public key always exists if there is a private key */
  263. if (ossl_ml_dsa_key_get_pub(key) == NULL)
  264. return 0; /* No public key */
  265. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  266. && ossl_ml_dsa_key_get_priv(key) == NULL)
  267. return 0; /* No private key */
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. /*
  273. * @brief Given a key containing private key values for rho, s1 & s2
  274. * generate the public value t and return the compressed values t1, t0.
  275. *
  276. * @param key A private key containing params, rh0, s1 & s2.
  277. * @param md_ctx A EVP_MD_CTX used for sampling.
  278. * @param t1 The returned polynomial encoding of the 10 MSB of each coefficient
  279. * of the uncompressed public key polynomial t.
  280. * @param t0 The returned polynomial encoding of the 13 LSB of each coefficient
  281. * of the uncompressed public key polynomial t.
  282. * @returns 1 on success, or 0 on failure.
  283. */
  284. static int public_from_private(const ML_DSA_KEY *key, EVP_MD_CTX *md_ctx,
  285. VECTOR *t1, VECTOR *t0)
  286. {
  287. int ret = 0;
  288. const ML_DSA_PARAMS *params = key->params;
  289. uint32_t k = params->k, l = params->l;
  290. POLY *polys;
  291. MATRIX a_ntt;
  292. VECTOR s1_ntt;
  293. VECTOR t;
  294. polys = OPENSSL_malloc(sizeof(*polys) * (k + l + k * l));
  295. if (polys == NULL)
  296. return 0;
  297. vector_init(&t, polys, k);
  298. vector_init(&s1_ntt, t.poly + k, l);
  299. matrix_init(&a_ntt, s1_ntt.poly + l, k, l);
  300. /* Using rho generate A' = A in NTT form */
  301. if (!matrix_expand_A(md_ctx, key->shake128_md, key->rho, &a_ntt))
  302. goto err;
  303. /* t = NTT_inv(A' * NTT(s1)) + s2 */
  304. vector_copy(&s1_ntt, &key->s1);
  305. vector_ntt(&s1_ntt);
  306. matrix_mult_vector(&a_ntt, &s1_ntt, &t);
  307. vector_ntt_inverse(&t);
  308. vector_add(&t, &key->s2, &t);
  309. /* Compress t */
  310. vector_power2_round(&t, t1, t0);
  311. /* Zeroize secret */
  312. vector_zero(&s1_ntt);
  313. ret = 1;
  314. err:
  315. OPENSSL_free(polys);
  316. return ret;
  317. }
  318. int ossl_ml_dsa_key_public_from_private(ML_DSA_KEY *key)
  319. {
  320. int ret = 0;
  321. VECTOR t0;
  322. EVP_MD_CTX *md_ctx = NULL;
  323. if (!vector_alloc(&t0, key->params->k)) /* t0 is already in the private key */
  324. return 0;
  325. ret = ((md_ctx = EVP_MD_CTX_new())!= NULL)
  326. && ossl_ml_dsa_key_pub_alloc(key) /* allocate space for t1 */
  327. && public_from_private(key, md_ctx, &key->t1, &t0)
  328. && vector_equal(&t0, &key->t0) /* compare the generated t0 to the expected */
  329. && ossl_ml_dsa_pk_encode(key)
  330. && shake_xof(md_ctx, key->shake256_md,
  331. key->pub_encoding, key->params->pk_len,
  332. key->tr, sizeof(key->tr));
  333. vector_free(&t0);
  334. EVP_MD_CTX_free(md_ctx);
  335. return ret;
  336. }
  337. int ossl_ml_dsa_key_pairwise_check(const ML_DSA_KEY *key)
  338. {
  339. int ret = 0;
  340. VECTOR t1, t0;
  341. POLY *polys = NULL;
  342. uint32_t k = key->params->k;
  343. EVP_MD_CTX *md_ctx = NULL;
  344. if (key->pub_encoding == NULL || key->priv_encoding == 0)
  345. return 0;
  346. polys = OPENSSL_malloc(sizeof(*polys) * (2 * k));
  347. if (polys == NULL)
  348. return 0;
  349. md_ctx = EVP_MD_CTX_new();
  350. if (md_ctx == NULL)
  351. goto err;
  352. vector_init(&t1, polys, k);
  353. vector_init(&t0, polys + k, k);
  354. if (!public_from_private(key, md_ctx, &t1, &t0))
  355. goto err;
  356. ret = vector_equal(&t1, &key->t1) && vector_equal(&t0, &key->t0);
  357. err:
  358. EVP_MD_CTX_free(md_ctx);
  359. OPENSSL_free(polys);
  360. return ret;
  361. }
  362. /*
  363. * @brief Generate a public-private key pair from a seed.
  364. * See FIPS 204, Algorithm 6 ML-DSA.KeyGen_internal().
  365. *
  366. * @param out The generated key (which contains params on input)
  367. *
  368. * @returns 1 on success or 0 on failure.
  369. */
  370. static int keygen_internal(ML_DSA_KEY *out)
  371. {
  372. int ret = 0;
  373. uint8_t augmented_seed[ML_DSA_SEED_BYTES + 2];
  374. uint8_t expanded_seed[ML_DSA_RHO_BYTES + ML_DSA_PRIV_SEED_BYTES + ML_DSA_K_BYTES];
  375. const uint8_t *const rho = expanded_seed; /* p = Public Random Seed */
  376. const uint8_t *const priv_seed = expanded_seed + ML_DSA_RHO_BYTES;
  377. const uint8_t *const K = priv_seed + ML_DSA_PRIV_SEED_BYTES;
  378. const ML_DSA_PARAMS *params = out->params;
  379. EVP_MD_CTX *md_ctx = NULL;
  380. if (out->seed == NULL
  381. || (md_ctx = EVP_MD_CTX_new()) == NULL
  382. || !ossl_ml_dsa_key_pub_alloc(out)
  383. || !ossl_ml_dsa_key_priv_alloc(out))
  384. goto err;
  385. /* augmented_seed = seed || k || l */
  386. memcpy(augmented_seed, out->seed, ML_DSA_SEED_BYTES);
  387. augmented_seed[ML_DSA_SEED_BYTES] = (uint8_t)params->k;
  388. augmented_seed[ML_DSA_SEED_BYTES + 1] = (uint8_t)params->l;
  389. /* Expand the seed into p[32], p'[64], K[32] */
  390. if (!shake_xof(md_ctx, out->shake256_md, augmented_seed, sizeof(augmented_seed),
  391. expanded_seed, sizeof(expanded_seed)))
  392. goto err;
  393. memcpy(out->rho, rho, sizeof(out->rho));
  394. memcpy(out->K, K, sizeof(out->K));
  395. ret = vector_expand_S(md_ctx, out->shake256_md, params->eta, priv_seed, &out->s1, &out->s2)
  396. && public_from_private(out, md_ctx, &out->t1, &out->t0)
  397. && ossl_ml_dsa_pk_encode(out)
  398. && shake_xof(md_ctx, out->shake256_md, out->pub_encoding, out->params->pk_len,
  399. out->tr, sizeof(out->tr))
  400. && ossl_ml_dsa_sk_encode(out);
  401. err:
  402. if (out->seed != NULL && (out->prov_flags & ML_DSA_KEY_RETAIN_SEED) == 0) {
  403. OPENSSL_clear_free(out->seed, ML_DSA_SEED_BYTES);
  404. out->seed = NULL;
  405. }
  406. EVP_MD_CTX_free(md_ctx);
  407. OPENSSL_cleanse(augmented_seed, sizeof(augmented_seed));
  408. OPENSSL_cleanse(expanded_seed, sizeof(expanded_seed));
  409. return ret;
  410. }
  411. int ossl_ml_dsa_generate_key(ML_DSA_KEY *out)
  412. {
  413. size_t seed_len = ML_DSA_SEED_BYTES;
  414. uint8_t *sk;
  415. int ret;
  416. if (out->seed == NULL) {
  417. if ((out->seed = OPENSSL_malloc(seed_len)) == NULL)
  418. return 0;
  419. if (RAND_priv_bytes_ex(out->libctx, out->seed, seed_len, 0) <= 0) {
  420. OPENSSL_free(out->seed);
  421. out->seed = NULL;
  422. return 0;
  423. }
  424. }
  425. /* We're generating from a seed, drop private prekey encoding */
  426. sk = out->priv_encoding;
  427. out->priv_encoding = NULL;
  428. if (sk == NULL) {
  429. ret = keygen_internal(out);
  430. } else {
  431. if ((ret = keygen_internal(out)) != 0
  432. && memcmp(out->priv_encoding, sk, out->params->sk_len) != 0) {
  433. ret = 0;
  434. ossl_ml_dsa_key_reset(out);
  435. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
  436. "explicit %s private key does not match seed",
  437. out->params->alg);
  438. }
  439. OPENSSL_free(sk);
  440. }
  441. return ret;
  442. }
  443. /**
  444. * @brief This is used when a ML DSA key is used for an operation.
  445. * This checks that the algorithm is the same (i.e. uses the same parameters)
  446. *
  447. * @param key A ML_DSA key to use for an operation.
  448. * @param evp_type The algorithm nid associated with an operation
  449. *
  450. * @returns 1 if the algorithm matches, or 0 otherwise.
  451. */
  452. int ossl_ml_dsa_key_matches(const ML_DSA_KEY *key, int evp_type)
  453. {
  454. return (key->params->evp_type == evp_type);
  455. }
  456. /* Returns the public key data or NULL if there is no public key */
  457. const uint8_t *ossl_ml_dsa_key_get_pub(const ML_DSA_KEY *key)
  458. {
  459. return key->pub_encoding;
  460. }
  461. /* Returns the encoded public key size */
  462. size_t ossl_ml_dsa_key_get_pub_len(const ML_DSA_KEY *key)
  463. {
  464. return key->params->pk_len;
  465. }
  466. size_t ossl_ml_dsa_key_get_collision_strength_bits(const ML_DSA_KEY *key)
  467. {
  468. return key->params->bit_strength;
  469. }
  470. /* Returns the private key data or NULL if there is no private key */
  471. const uint8_t *ossl_ml_dsa_key_get_priv(const ML_DSA_KEY *key)
  472. {
  473. return key->priv_encoding;
  474. }
  475. size_t ossl_ml_dsa_key_get_priv_len(const ML_DSA_KEY *key)
  476. {
  477. return key->params->sk_len;
  478. }
  479. size_t ossl_ml_dsa_key_get_sig_len(const ML_DSA_KEY *key)
  480. {
  481. return key->params->sig_len;
  482. }
  483. OSSL_LIB_CTX *ossl_ml_dsa_key_get0_libctx(const ML_DSA_KEY *key)
  484. {
  485. return key != NULL ? key->libctx : NULL;
  486. }
  487. const char *ossl_ml_dsa_key_get_name(const ML_DSA_KEY *key)
  488. {
  489. return key->params->alg;
  490. }