ml_dsa_codecs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 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 <string.h>
  10. #include <openssl/byteorder.h>
  11. #include <openssl/err.h>
  12. #include <openssl/proverr.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/core_names.h>
  15. #include "internal/encoder.h"
  16. #include "prov/ml_dsa.h"
  17. #include "ml_dsa_codecs.h"
  18. /*-
  19. * Tables describing supported ASN.1 input/output formats.
  20. */
  21. /*-
  22. * ML-DSA-44:
  23. * Public key bytes: 1312 (0x0520)
  24. * Private key bytes: 2560 (0x0a00)
  25. */
  26. static const ML_COMMON_SPKI_FMT ml_dsa_44_spkifmt = {
  27. { 0x30, 0x82, 0x05, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48,
  28. 0x01, 0x65, 0x03, 0x04, 0x03, 0x11, 0x03, 0x82, 0x05, 0x21, 0x00, }
  29. };
  30. static const ML_COMMON_PKCS8_FMT ml_dsa_44_p8fmt[NUM_PKCS8_FORMATS] = {
  31. { "seed-priv", 0x0a2a, 0, 0x30820a26, 0x0420, 6, 0x20, 0x04820a00, 0x2a, 0x0a00, 0, 0, },
  32. { "priv-only", 0x0a04, 0, 0x04820a00, 0, 0, 0, 0, 0x04, 0x0a00, 0, 0, },
  33. { "oqskeypair", 0x0f24, 0, 0x04820f20, 0, 0, 0, 0, 0x04, 0x0a00, 0x0a04, 0x0520 },
  34. { "seed-only", 0x0022, 2, 0x8020, 0, 2, 0x20, 0, 0, 0, 0, 0, },
  35. { "bare-priv", 0x0a00, 4, 0, 0, 0, 0, 0, 0, 0x0a00, 0, 0, },
  36. { "bare-seed", 0x0020, 4, 0, 0, 0, 0x20, 0, 0, 0, 0, 0, },
  37. };
  38. /*
  39. * ML-DSA-65:
  40. * Public key bytes: 1952 (0x07a0)
  41. * Private key bytes: 4032 (0x0fc0)
  42. */
  43. static const ML_COMMON_SPKI_FMT ml_dsa_65_spkifmt = {
  44. { 0x30, 0x82, 0x07, 0xb2, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48,
  45. 0x01, 0x65, 0x03, 0x04, 0x03, 0x12, 0x03, 0x82, 0x07, 0xa1, 0x00, }
  46. };
  47. static const ML_COMMON_PKCS8_FMT ml_dsa_65_p8fmt[NUM_PKCS8_FORMATS] = {
  48. { "seed-priv", 0x0fea, 0, 0x30820fe6, 0x0420, 6, 0x20, 0x04820fc0, 0x2a, 0x0fc0, 0, 0, },
  49. { "priv-only", 0x0fc4, 0, 0x04820fc0, 0, 0, 0, 0, 0x04, 0x0fc0, 0, 0, },
  50. { "oqskeypair", 0x1764, 0, 0x04821760, 0, 0, 0, 0, 0x04, 0x0fc0, 0x0fc4, 0x07a0 },
  51. { "seed-only", 0x0022, 2, 0x8020, 0, 2, 0x20, 0, 0, 0, 0, 0, },
  52. { "bare-priv", 0x0fc0, 4, 0, 0, 0, 0, 0, 0, 0x0fc0, 0, 0, },
  53. { "bare-seed", 0x0020, 4, 0, 0, 0, 0x20, 0, 0, 0, 0, 0, },
  54. };
  55. /*-
  56. * ML-DSA-87:
  57. * Public key bytes: 2592 (0x0a20)
  58. * Private key bytes: 4896 (0x1320)
  59. */
  60. static const ML_COMMON_SPKI_FMT ml_dsa_87_spkifmt = {
  61. { 0x30, 0x82, 0x0a, 0x32, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48,
  62. 0x01, 0x65, 0x03, 0x04, 0x03, 0x13, 0x03, 0x82, 0x0a, 0x21, 0x00, }
  63. };
  64. static const ML_COMMON_PKCS8_FMT ml_dsa_87_p8fmt[NUM_PKCS8_FORMATS] = {
  65. { "seed-priv", 0x134a, 0, 0x30821346, 0x0420, 6, 0x20, 0x04821320, 0x2a, 0x1320, 0, 0, },
  66. { "priv-only", 0x1324, 0, 0x04821320, 0, 0, 0, 0, 0x04, 0x1320, 0, 0, },
  67. { "oqskeypair", 0x1d44, 0, 0x04821d40, 0, 0, 0, 0, 0x04, 0x1320, 0x1324, 0x0a20 },
  68. { "seed-only", 0x0022, 2, 0x8020, 0, 2, 0x20, 0, 0, 0, 0, 0, },
  69. { "bare-priv", 0x1320, 4, 0, 0, 0, 0, 0, 0, 0x1320, 0, 0, },
  70. { "bare-seed", 0x0020, 4, 0, 0, 0, 0x20, 0, 0, 0, 0, 0, },
  71. };
  72. /* Indices of slots in the codec table below */
  73. #define ML_DSA_44_CODEC 0
  74. #define ML_DSA_65_CODEC 1
  75. #define ML_DSA_87_CODEC 2
  76. /*
  77. * Per-variant fixed parameters
  78. */
  79. static const ML_COMMON_CODEC codecs[3] = {
  80. { &ml_dsa_44_spkifmt, ml_dsa_44_p8fmt },
  81. { &ml_dsa_65_spkifmt, ml_dsa_65_p8fmt },
  82. { &ml_dsa_87_spkifmt, ml_dsa_87_p8fmt }
  83. };
  84. /* Retrieve the parameters of one of the ML-DSA variants */
  85. static const ML_COMMON_CODEC *ml_dsa_get_codec(int evp_type)
  86. {
  87. switch (evp_type) {
  88. case EVP_PKEY_ML_DSA_44:
  89. return &codecs[ML_DSA_44_CODEC];
  90. case EVP_PKEY_ML_DSA_65:
  91. return &codecs[ML_DSA_65_CODEC];
  92. case EVP_PKEY_ML_DSA_87:
  93. return &codecs[ML_DSA_87_CODEC];
  94. }
  95. return NULL;
  96. }
  97. ML_DSA_KEY *
  98. ossl_ml_dsa_d2i_PUBKEY(const uint8_t *pk, int pk_len, int evp_type,
  99. PROV_CTX *provctx, const char *propq)
  100. {
  101. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
  102. const ML_COMMON_CODEC *codec;
  103. const ML_DSA_PARAMS *params;
  104. ML_DSA_KEY *ret;
  105. if ((params = ossl_ml_dsa_params_get(evp_type)) == NULL
  106. || (codec = ml_dsa_get_codec(evp_type)) == NULL)
  107. return NULL;
  108. if (pk_len != ML_COMMON_SPKI_OVERHEAD + (ossl_ssize_t) params->pk_len
  109. || memcmp(pk, codec->spkifmt->asn1_prefix, ML_COMMON_SPKI_OVERHEAD) != 0)
  110. return NULL;
  111. pk_len -= ML_COMMON_SPKI_OVERHEAD;
  112. pk += ML_COMMON_SPKI_OVERHEAD;
  113. if ((ret = ossl_ml_dsa_key_new(libctx, propq, evp_type)) == NULL)
  114. return NULL;
  115. if (!ossl_ml_dsa_pk_decode(ret, pk, (size_t) pk_len)) {
  116. ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
  117. "errror parsing %s public key from input SPKI",
  118. params->alg);
  119. ossl_ml_dsa_key_free(ret);
  120. return NULL;
  121. }
  122. return ret;
  123. }
  124. ML_DSA_KEY *
  125. ossl_ml_dsa_d2i_PKCS8(const uint8_t *prvenc, int prvlen,
  126. int evp_type, PROV_CTX *provctx,
  127. const char *propq)
  128. {
  129. const ML_DSA_PARAMS *v;
  130. const ML_COMMON_CODEC *codec;
  131. ML_COMMON_PKCS8_FMT_PREF *fmt_slots = NULL, *slot;
  132. const ML_COMMON_PKCS8_FMT *p8fmt;
  133. ML_DSA_KEY *key = NULL, *ret = NULL;
  134. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  135. const uint8_t *buf, *pos;
  136. const X509_ALGOR *alg = NULL;
  137. const char *formats;
  138. int len, ptype;
  139. uint32_t magic;
  140. uint16_t seed_magic;
  141. const uint8_t *seed = NULL;
  142. const uint8_t *priv = NULL;
  143. /* Which ML-DSA variant? */
  144. if ((v = ossl_ml_dsa_params_get(evp_type)) == NULL
  145. || (codec = ml_dsa_get_codec(evp_type)) == NULL)
  146. return 0;
  147. /* Extract the key OID and any parameters. */
  148. if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &prvenc, prvlen)) == NULL)
  149. return 0;
  150. /* Shortest prefix is 4 bytes: seq tag/len + octet string tag/len */
  151. if (!PKCS8_pkey_get0(NULL, &buf, &len, &alg, p8inf))
  152. goto end;
  153. /* Bail out early if this is some other key type. */
  154. if (OBJ_obj2nid(alg->algorithm) != evp_type)
  155. goto end;
  156. /* Get the list of enabled decoders. Their order is not important here. */
  157. formats = ossl_prov_ctx_get_param(
  158. provctx, OSSL_PKEY_PARAM_ML_DSA_INPUT_FORMATS, NULL);
  159. fmt_slots = ossl_ml_common_pkcs8_fmt_order(v->alg, codec->p8fmt,
  160. "input", formats);
  161. if (fmt_slots == NULL)
  162. goto end;
  163. /* Parameters must be absent. */
  164. X509_ALGOR_get0(NULL, &ptype, NULL, alg);
  165. if (ptype != V_ASN1_UNDEF) {
  166. ERR_raise_data(ERR_LIB_PROV, PROV_R_UNEXPECTED_KEY_PARAMETERS,
  167. "unexpected parameters with a PKCS#8 %s private key",
  168. v->alg);
  169. goto end;
  170. }
  171. if ((ossl_ssize_t)len < (ossl_ssize_t)sizeof(magic))
  172. goto end;
  173. /* Find the matching p8 info slot, that also has the expected length. */
  174. pos = OPENSSL_load_u32_be(&magic, buf);
  175. for (slot = fmt_slots; (p8fmt = slot->fmt) != NULL; ++slot) {
  176. if (len != (ossl_ssize_t)p8fmt->p8_bytes)
  177. continue;
  178. if (p8fmt->p8_shift == sizeof(magic)
  179. || (magic >> (p8fmt->p8_shift * 8)) == p8fmt->p8_magic) {
  180. pos -= p8fmt->p8_shift;
  181. break;
  182. }
  183. }
  184. if (p8fmt == NULL
  185. || (p8fmt->seed_length > 0 && p8fmt->seed_length != ML_DSA_SEED_BYTES)
  186. || (p8fmt->priv_length > 0 && p8fmt->priv_length != v->sk_len)
  187. || (p8fmt->pub_length > 0 && p8fmt->pub_length != v->pk_len)) {
  188. ERR_raise_data(ERR_LIB_PROV, PROV_R_ML_DSA_NO_FORMAT,
  189. "no matching enabled %s private key input formats",
  190. v->alg);
  191. goto end;
  192. }
  193. if (p8fmt->seed_length > 0) {
  194. /* Check |seed| tag/len, if not subsumed by |magic|. */
  195. if (pos + sizeof(uint16_t) == buf + p8fmt->seed_offset) {
  196. pos = OPENSSL_load_u16_be(&seed_magic, pos);
  197. if (seed_magic != p8fmt->seed_magic)
  198. goto end;
  199. } else if (pos != buf + p8fmt->seed_offset) {
  200. goto end;
  201. }
  202. pos += ML_DSA_SEED_BYTES;
  203. }
  204. if (p8fmt->priv_length > 0) {
  205. /* Check |priv| tag/len */
  206. if (pos + sizeof(uint32_t) == buf + p8fmt->priv_offset) {
  207. pos = OPENSSL_load_u32_be(&magic, pos);
  208. if (magic != p8fmt->priv_magic)
  209. goto end;
  210. } else if (pos != buf + p8fmt->priv_offset) {
  211. goto end;
  212. }
  213. pos += v->sk_len;
  214. }
  215. if (p8fmt->pub_length > 0) {
  216. if (pos != buf + p8fmt->pub_offset)
  217. goto end;
  218. pos += v->pk_len;
  219. }
  220. if (pos != buf + len)
  221. goto end;
  222. /*
  223. * Collect the seed and/or key into a "decoded" private key object,
  224. * to be turned into a real key on provider "load" or "import".
  225. */
  226. if ((key = ossl_prov_ml_dsa_new(provctx, propq, evp_type)) == NULL)
  227. goto end;
  228. if (p8fmt->seed_length > 0)
  229. seed = buf + p8fmt->seed_offset;
  230. if (p8fmt->priv_length > 0)
  231. priv = buf + p8fmt->priv_offset;
  232. /* Any OQS public key content is ignored */
  233. if (ossl_ml_dsa_set_prekey(key, 0, 0,
  234. seed, ML_DSA_SEED_BYTES, priv, v->sk_len))
  235. ret = key;
  236. end:
  237. OPENSSL_free(fmt_slots);
  238. PKCS8_PRIV_KEY_INFO_free(p8inf);
  239. if (ret == NULL)
  240. ossl_ml_dsa_key_free(key);
  241. return ret;
  242. }
  243. /* Same as ossl_ml_dsa_encode_pubkey, but allocates the output buffer. */
  244. int ossl_ml_dsa_i2d_pubkey(const ML_DSA_KEY *key, unsigned char **out)
  245. {
  246. const ML_DSA_PARAMS *params = ossl_ml_dsa_key_params(key);
  247. const uint8_t *pk = ossl_ml_dsa_key_get_pub(key);
  248. if (pk == NULL) {
  249. ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY,
  250. "no %s public key data available", params->alg);
  251. return 0;
  252. }
  253. if (out != NULL
  254. && (*out = OPENSSL_memdup(pk, params->pk_len)) == NULL)
  255. return 0;
  256. return (int)params->pk_len;
  257. }
  258. /* Allocate and encode PKCS#8 private key payload. */
  259. int ossl_ml_dsa_i2d_prvkey(const ML_DSA_KEY *key, uint8_t **out,
  260. PROV_CTX *provctx)
  261. {
  262. const ML_DSA_PARAMS *params = ossl_ml_dsa_key_params(key);
  263. const ML_COMMON_CODEC *codec;
  264. ML_COMMON_PKCS8_FMT_PREF *fmt_slots, *slot;
  265. const ML_COMMON_PKCS8_FMT *p8fmt;
  266. uint8_t *buf = NULL, *pos;
  267. const char *formats;
  268. int len = ML_DSA_SEED_BYTES;
  269. int ret = 0;
  270. const uint8_t *seed = ossl_ml_dsa_key_get_seed(key);
  271. const uint8_t *sk = ossl_ml_dsa_key_get_priv(key);
  272. /* Not ours to handle */
  273. if ((codec = ml_dsa_get_codec(params->evp_type)) == NULL)
  274. return 0;
  275. if (sk == NULL) {
  276. ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY,
  277. "no %s private key data available",
  278. params->alg);
  279. return 0;
  280. }
  281. formats = ossl_prov_ctx_get_param(
  282. provctx, OSSL_PKEY_PARAM_ML_DSA_OUTPUT_FORMATS, NULL);
  283. fmt_slots = ossl_ml_common_pkcs8_fmt_order(params->alg, codec->p8fmt,
  284. "output", formats);
  285. if (fmt_slots == NULL)
  286. return 0;
  287. /* If we don't have a seed, skip seedful entries */
  288. for (slot = fmt_slots; (p8fmt = slot->fmt) != NULL; ++slot)
  289. if (seed != NULL || p8fmt->seed_length == 0)
  290. break;
  291. /* No matching table entries, give up */
  292. if (p8fmt == NULL
  293. || (p8fmt->seed_length > 0 && p8fmt->seed_length != ML_DSA_SEED_BYTES)
  294. || (p8fmt->priv_length > 0 && p8fmt->priv_length != params->sk_len)
  295. || (p8fmt->pub_length > 0 && p8fmt->pub_length != params->pk_len)) {
  296. ERR_raise_data(ERR_LIB_PROV, PROV_R_ML_DSA_NO_FORMAT,
  297. "no matching enabled %s private key output formats",
  298. params->alg);
  299. goto end;
  300. }
  301. len = p8fmt->p8_bytes;
  302. if (out == NULL) {
  303. ret = len;
  304. goto end;
  305. }
  306. if ((pos = buf = OPENSSL_malloc((size_t) len)) == NULL)
  307. goto end;
  308. switch (p8fmt->p8_shift) {
  309. case 0:
  310. pos = OPENSSL_store_u32_be(pos, p8fmt->p8_magic);
  311. break;
  312. case 2:
  313. pos = OPENSSL_store_u16_be(pos, (uint16_t)p8fmt->p8_magic);
  314. break;
  315. case 4:
  316. break;
  317. default:
  318. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  319. "error encoding %s private key", params->alg);
  320. goto end;
  321. }
  322. if (p8fmt->seed_length != 0) {
  323. /*
  324. * Either the tag/len were already included in |magic| or they require
  325. * us to write two bytes now.
  326. */
  327. if (pos + sizeof(uint16_t) == buf + p8fmt->seed_offset)
  328. pos = OPENSSL_store_u16_be(pos, p8fmt->seed_magic);
  329. if (pos != buf + p8fmt->seed_offset) {
  330. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  331. "error encoding %s private key", params->alg);
  332. goto end;
  333. }
  334. memcpy(pos, seed, ML_DSA_SEED_BYTES);
  335. pos += ML_DSA_SEED_BYTES;
  336. }
  337. if (p8fmt->priv_length != 0) {
  338. if (pos + sizeof(uint32_t) == buf + p8fmt->priv_offset)
  339. pos = OPENSSL_store_u32_be(pos, p8fmt->priv_magic);
  340. if (pos != buf + p8fmt->priv_offset) {
  341. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  342. "error encoding %s private key", params->alg);
  343. goto end;
  344. }
  345. memcpy(pos, sk, params->sk_len);
  346. pos += params->sk_len;
  347. }
  348. /* OQS form output with tacked-on public key */
  349. if (p8fmt->pub_length != 0) {
  350. /* The OQS pubkey is never separately DER-wrapped */
  351. if (pos != buf + p8fmt->pub_offset) {
  352. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  353. "error encoding %s private key", params->alg);
  354. goto end;
  355. }
  356. memcpy(pos, ossl_ml_dsa_key_get_pub(key), params->pk_len);
  357. pos += params->pk_len;
  358. }
  359. if (pos == buf + len) {
  360. *out = buf;
  361. ret = len;
  362. }
  363. end:
  364. OPENSSL_free(fmt_slots);
  365. if (ret == 0)
  366. OPENSSL_free(buf);
  367. return ret;
  368. }
  369. int ossl_ml_dsa_key_to_text(BIO *out, const ML_DSA_KEY *key, int selection)
  370. {
  371. const ML_DSA_PARAMS *params;
  372. const uint8_t *seed, *sk, *pk;
  373. if (out == NULL || key == NULL) {
  374. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
  375. return 0;
  376. }
  377. params = ossl_ml_dsa_key_params(key);
  378. pk = ossl_ml_dsa_key_get_pub(key);
  379. sk = ossl_ml_dsa_key_get_priv(key);
  380. seed = ossl_ml_dsa_key_get_seed(key);
  381. if (pk == NULL) {
  382. /* Regardless of the |selection|, there must be a public key */
  383. ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_KEY,
  384. "no %s key material available", params->alg);
  385. return 0;
  386. }
  387. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  388. if (sk == NULL) {
  389. ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_KEY,
  390. "no %s key material available", params->alg);
  391. return 0;
  392. }
  393. if (BIO_printf(out, "%s Private-Key:\n", params->alg) <= 0)
  394. return 0;
  395. if (seed != NULL && !ossl_bio_print_labeled_buf(out, "seed:", seed,
  396. ML_DSA_SEED_BYTES))
  397. return 0;
  398. if (!ossl_bio_print_labeled_buf(out, "priv:", sk, params->sk_len))
  399. return 0;
  400. } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  401. if (BIO_printf(out, "%s Public-Key:\n", params->alg) <= 0)
  402. return 0;
  403. }
  404. if (!ossl_bio_print_labeled_buf(out, "pub:", pk, params->pk_len))
  405. return 0;
  406. return 1;
  407. }