slh_dsa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <stddef.h>
  10. #include <string.h>
  11. #include <openssl/err.h>
  12. #include <openssl/proverr.h>
  13. #include "slh_dsa_local.h"
  14. #include "slh_dsa_key.h"
  15. #define SLH_MAX_M 49 /* See slh_params.c */
  16. /* The size of md is (21..40 bytes) - since a is in bits round up to nearest byte */
  17. #define MD_LEN(params) (((params)->k * (params)->a + 7) >> 3)
  18. static int get_tree_ids(PACKET *pkt, const SLH_DSA_PARAMS *params,
  19. uint64_t *tree_id, uint32_t *leaf_id);
  20. /**
  21. * @brief SLH-DSA Signature generation
  22. * See FIPS 205 Section 9.2 Algorithm 19
  23. *
  24. * A signature consists of
  25. * r[n] random bytes
  26. * [k]*[1+a][n] FORS signature bytes
  27. * [h + d*len][n] Hyper tree signature bytes
  28. *
  29. * @param ctx Contains SLH_DSA algorithm functions and constants, and the
  30. * private SLH_DSA key to use for signing.
  31. * @param msg The message to sign. This may be encoded beforehand.
  32. * @param msg_len The size of |msg|
  33. * @param sig The returned signature
  34. * @param sig_len The size of the returned |sig|
  35. * @param sig_size The maximum size of |sig|
  36. * @param opt_rand An optional random value to use of size |n|. It can be NULL.
  37. * @returns 1 if the signature generation succeeded or 0 otherwise.
  38. */
  39. static int slh_sign_internal(SLH_DSA_HASH_CTX *hctx,
  40. const uint8_t *msg, size_t msg_len,
  41. uint8_t *sig, size_t *sig_len, size_t sig_size,
  42. const uint8_t *opt_rand)
  43. {
  44. int ret = 0;
  45. const SLH_DSA_KEY *priv = hctx->key;
  46. const SLH_DSA_PARAMS *params = priv->params;
  47. size_t sig_len_expected = params->sig_len;
  48. uint8_t m_digest[SLH_MAX_M];
  49. const uint8_t *md; /* The first md_len bytes of m_digest */
  50. size_t md_len = MD_LEN(params); /* The size of the digest |md| */
  51. /* Points to |m_digest| buffer, it is also reused to point to |sig_fors| */
  52. PACKET r_packet, *rpkt = &r_packet;
  53. uint8_t *r, *sig_fors; /* Pointers into buffer inside |wpkt| */
  54. WPACKET w_packet, *wpkt = &w_packet; /* Points to output |sig| buffer */
  55. const uint8_t *pk_seed, *sk_seed; /* pointers to elements within |priv| */
  56. uint8_t pk_fors[SLH_MAX_N];
  57. uint64_t tree_id;
  58. uint32_t leaf_id;
  59. SLH_ADRS_DECLARE(adrs);
  60. SLH_HASH_FUNC_DECLARE(priv, hashf);
  61. SLH_ADRS_FUNC_DECLARE(priv, adrsf);
  62. if (sig == NULL) {
  63. *sig_len = sig_len_expected;
  64. return 1;
  65. }
  66. if (sig_size < sig_len_expected) {
  67. ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE,
  68. "is %zu, should be at least %zu", sig_size, sig_len_expected);
  69. return 0;
  70. }
  71. /* Exit if private key is not set */
  72. if (priv->has_priv == 0) {
  73. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  74. return 0;
  75. }
  76. if (!WPACKET_init_static_len(wpkt, sig, sig_len_expected, 0))
  77. return 0;
  78. if (!PACKET_buf_init(rpkt, m_digest, params->m))
  79. return 0;
  80. pk_seed = SLH_DSA_PK_SEED(priv);
  81. sk_seed = SLH_DSA_SK_SEED(priv);
  82. if (opt_rand == NULL)
  83. opt_rand = pk_seed;
  84. adrsf->zero(adrs);
  85. /* calculate Randomness value r, and output to the SLH-DSA signature */
  86. r = WPACKET_get_curr(wpkt);
  87. if (!hashf->PRF_MSG(hctx, SLH_DSA_SK_PRF(priv), opt_rand, msg, msg_len, wpkt)
  88. /* generate a digest of size |params->m| bytes where m is (30..49) */
  89. || !hashf->H_MSG(hctx, r, pk_seed, SLH_DSA_PK_ROOT(priv), msg, msg_len,
  90. m_digest, sizeof(m_digest))
  91. /* Grab the first md_len bytes of m_digest to use in fors_sign() */
  92. || !PACKET_get_bytes(rpkt, &md, md_len)
  93. /* Grab remaining bytes from m_digest to select tree and leaf id's */
  94. || !get_tree_ids(rpkt, params, &tree_id, &leaf_id))
  95. goto err;
  96. adrsf->set_tree_address(adrs, tree_id);
  97. adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_FORS_TREE);
  98. adrsf->set_keypair_address(adrs, leaf_id);
  99. sig_fors = WPACKET_get_curr(wpkt);
  100. /* generate the FORS signature and append it to the SLH-DSA signature */
  101. ret = ossl_slh_fors_sign(hctx, md, sk_seed, pk_seed, adrs, wpkt)
  102. /* Reuse rpkt to point to the FORS signature that was just generated */
  103. && PACKET_buf_init(rpkt, sig_fors, WPACKET_get_curr(wpkt) - sig_fors)
  104. /* Calculate the FORS public key using the generated FORS signature */
  105. && ossl_slh_fors_pk_from_sig(hctx, rpkt, md, pk_seed, adrs,
  106. pk_fors, sizeof(pk_fors))
  107. /* Generate ht signature and append to the SLH-DSA signature */
  108. && ossl_slh_ht_sign(hctx, pk_fors, sk_seed, pk_seed, tree_id, leaf_id,
  109. wpkt);
  110. *sig_len = sig_len_expected;
  111. ret = 1;
  112. err:
  113. if (!WPACKET_finish(wpkt))
  114. ret = 0;
  115. return ret;
  116. }
  117. /**
  118. * @brief SLH-DSA Signature verification
  119. * See FIPS 205 Section 9.3 Algorithm 20
  120. *
  121. * A signature consists of
  122. * r[n] random bytes
  123. * [k]*[1+a][n] FORS signature bytes
  124. * [h + d*len][n] Hyper tree signature bytes
  125. *
  126. * @param hctx Contains SLH_DSA algorithm functions and constants and the
  127. * public SLH_DSA key to use for verification.
  128. * @param msg The message to verify. This may be encoded beforehand.
  129. * @param msg_len The size of |msg|
  130. * @param sig A signature to verify
  131. * @param sig_len The size of |sig|
  132. * @returns 1 if the signature verification succeeded or 0 otherwise.
  133. */
  134. static int slh_verify_internal(SLH_DSA_HASH_CTX *hctx,
  135. const uint8_t *msg, size_t msg_len,
  136. const uint8_t *sig, size_t sig_len)
  137. {
  138. const SLH_DSA_KEY *pub = hctx->key;
  139. SLH_HASH_FUNC_DECLARE(pub, hashf);
  140. SLH_ADRS_FUNC_DECLARE(pub, adrsf);
  141. SLH_ADRS_DECLARE(adrs);
  142. const SLH_DSA_PARAMS *params = pub->params;
  143. uint32_t n = params->n;
  144. const uint8_t *pk_seed, *pk_root; /* Pointers to elements in |pub| */
  145. PACKET pkt, *sig_rpkt = &pkt; /* Points to the |sig| buffer */
  146. uint8_t m_digest[SLH_MAX_M];
  147. const uint8_t *md; /* This is a pointer into the buffer in m_digest_rpkt */
  148. size_t md_len = MD_LEN(params); /* 21..40 bytes */
  149. PACKET pkt2, *m_digest_rpkt = &pkt2; /* Points to m_digest buffer */
  150. const uint8_t *r; /* Pointer to |sig_rpkt| buffer */
  151. uint8_t pk_fors[SLH_MAX_N];
  152. uint64_t tree_id;
  153. uint32_t leaf_id;
  154. /* Exit if public key is not set */
  155. if (pub->pub == NULL) {
  156. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  157. return 0;
  158. }
  159. /* Exit if signature is invalid size */
  160. if (sig_len != params->sig_len
  161. || !PACKET_buf_init(sig_rpkt, sig, sig_len))
  162. return 0;
  163. if (!PACKET_get_bytes(sig_rpkt, &r, n))
  164. return 0;
  165. adrsf->zero(adrs);
  166. pk_seed = SLH_DSA_PK_SEED(pub);
  167. pk_root = SLH_DSA_PK_ROOT(pub);
  168. if (!hashf->H_MSG(hctx, r, pk_seed, pk_root, msg, msg_len,
  169. m_digest, sizeof(m_digest)))
  170. return 0;
  171. /*
  172. * Get md (the first md_len bytes of m_digest to use in
  173. * ossl_slh_fors_pk_from_sig(), and then retrieve the tree id and leaf id
  174. * from the remaining bytes in m_digest.
  175. */
  176. if (!PACKET_buf_init(m_digest_rpkt, m_digest, sizeof(m_digest))
  177. || !PACKET_get_bytes(m_digest_rpkt, &md, md_len)
  178. || !get_tree_ids(m_digest_rpkt, params, &tree_id, &leaf_id))
  179. return 0;
  180. adrsf->set_tree_address(adrs, tree_id);
  181. adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_FORS_TREE);
  182. adrsf->set_keypair_address(adrs, leaf_id);
  183. return ossl_slh_fors_pk_from_sig(hctx, sig_rpkt, md, pk_seed, adrs,
  184. pk_fors, sizeof(pk_fors))
  185. && ossl_slh_ht_verify(hctx, pk_fors, sig_rpkt, pk_seed,
  186. tree_id, leaf_id, pk_root)
  187. && PACKET_remaining(sig_rpkt) == 0;
  188. }
  189. /**
  190. * @brief Encode a message
  191. * See FIPS 205 Algorithm 22 Step 8 (and algorithm 24 Step 4).
  192. *
  193. * SLH_DSA pure signatures are encoded as M' = 00 || ctx_len || ctx || msg
  194. * Where ctx is the empty string by default and ctx_len <= 255.
  195. *
  196. * @param msg A message to encode
  197. * @param msg_len The size of |msg|
  198. * @param ctx An optional context to add to the message encoding.
  199. * @param ctx_len The size of |ctx|. It must be in the range 0..255
  200. * @param encode Use the Pure signature encoding if this is 1, and dont encode
  201. * if this value is 0.
  202. * @param tmp A small buffer that may be used if the message is small.
  203. * @param tmp_len The size of |tmp|
  204. * @param out_len The size of the returned encoded buffer.
  205. * @returns A buffer containing the encoded message. If the passed in
  206. * |tmp| buffer is big enough to hold the encoded message then it returns |tmp|
  207. * otherwise it allocates memory which must be freed by the caller. If |encode|
  208. * is 0 then it returns |msg|. NULL is returned if there is a failure.
  209. */
  210. static uint8_t *msg_encode(const uint8_t *msg, size_t msg_len,
  211. const uint8_t *ctx, size_t ctx_len, int encode,
  212. uint8_t *tmp, size_t tmp_len, size_t *out_len)
  213. {
  214. uint8_t *encoded = NULL;
  215. size_t encoded_len;
  216. if (encode == 0) {
  217. /* Raw message */
  218. *out_len = msg_len;
  219. return (uint8_t *)msg;
  220. }
  221. if (ctx_len > SLH_DSA_MAX_CONTEXT_STRING_LEN)
  222. return NULL;
  223. /* Pure encoding */
  224. encoded_len = 1 + 1 + ctx_len + msg_len;
  225. *out_len = encoded_len;
  226. if (encoded_len <= tmp_len) {
  227. encoded = tmp;
  228. } else {
  229. encoded = OPENSSL_zalloc(encoded_len);
  230. if (encoded == NULL)
  231. return NULL;
  232. }
  233. encoded[0] = 0;
  234. encoded[1] = (uint8_t)ctx_len;
  235. memcpy(&encoded[2], ctx, ctx_len);
  236. memcpy(&encoded[2 + ctx_len], msg, msg_len);
  237. return encoded;
  238. }
  239. /**
  240. * See FIPS 205 Section 10.2.1 Algorithm 22
  241. * @returns 1 on success, or 0 on error.
  242. */
  243. int ossl_slh_dsa_sign(SLH_DSA_HASH_CTX *slh_ctx,
  244. const uint8_t *msg, size_t msg_len,
  245. const uint8_t *ctx, size_t ctx_len,
  246. const uint8_t *add_rand, int encode,
  247. unsigned char *sig, size_t *siglen, size_t sigsize)
  248. {
  249. uint8_t m_tmp[1024], *m = m_tmp;
  250. size_t m_len = 0;
  251. int ret = 0;
  252. if (sig != NULL) {
  253. m = msg_encode(msg, msg_len, ctx, ctx_len, encode, m_tmp, sizeof(m_tmp),
  254. &m_len);
  255. if (m == NULL)
  256. return 0;
  257. }
  258. ret = slh_sign_internal(slh_ctx, m, m_len, sig, siglen, sigsize, add_rand);
  259. if (m != msg && m != m_tmp)
  260. OPENSSL_free(m);
  261. return ret;
  262. }
  263. /**
  264. * See FIPS 205 Section 10.3 Algorithm 24
  265. * @returns 1 on success, or 0 on error.
  266. */
  267. int ossl_slh_dsa_verify(SLH_DSA_HASH_CTX *slh_ctx,
  268. const uint8_t *msg, size_t msg_len,
  269. const uint8_t *ctx, size_t ctx_len, int encode,
  270. const uint8_t *sig, size_t sig_len)
  271. {
  272. uint8_t *m;
  273. size_t m_len;
  274. uint8_t m_tmp[1024];
  275. int ret = 0;
  276. m = msg_encode(msg, msg_len, ctx, ctx_len, encode, m_tmp, sizeof(m_tmp),
  277. &m_len);
  278. if (m == NULL)
  279. return 0;
  280. ret = slh_verify_internal(slh_ctx, m, m_len, sig, sig_len);
  281. if (m != msg && m != m_tmp)
  282. OPENSSL_free(m);
  283. return ret;
  284. }
  285. /*
  286. * See FIPS 205 Algorithm 2 toInt(X, n)
  287. * OPENSSL_load_u64_be() cant be used here as the |in_len| may be < 8
  288. */
  289. static uint64_t bytes_to_u64_be(const uint8_t *in, size_t in_len)
  290. {
  291. size_t i;
  292. uint64_t total = 0;
  293. for (i = 0; i < in_len; i++)
  294. total = (total << 8) + *in++;
  295. return total;
  296. }
  297. /*
  298. * See Algorithm 19 Steps 7..10 (also Algorithm 20 Step 10..13).
  299. * Converts digested bytes into a tree index, and leaf index within the tree.
  300. * The sizes are determined by the |params| parameter set.
  301. */
  302. static int get_tree_ids(PACKET *rpkt, const SLH_DSA_PARAMS *params,
  303. uint64_t *tree_id, uint32_t *leaf_id)
  304. {
  305. const uint8_t *tree_id_bytes, *leaf_id_bytes;
  306. uint32_t tree_id_len, leaf_id_len;
  307. uint64_t tree_id_mask, leaf_id_mask;
  308. tree_id_len = ((params->h - params->hm + 7) >> 3); /* 7 or 8 bytes */
  309. leaf_id_len = ((params->hm + 7) >> 3); /* 1 or 2 bytes */
  310. if (!PACKET_get_bytes(rpkt, &tree_id_bytes, tree_id_len)
  311. || !PACKET_get_bytes(rpkt, &leaf_id_bytes, leaf_id_len))
  312. return 0;
  313. /*
  314. * In order to calculate A mod (2^X) where X is in the range of (54..64)
  315. * This is equivalent to A & (2^x - 1) which is just a sequence of X ones
  316. * that must fit into a 64 bit value.
  317. * e.g when X = 64 it would be A & (0xFFFF_FFFF_FFFF_FFFF)
  318. * when X = 54 it would be A & (0x3F_FFFF_FFFF_FFFF)
  319. * i.e. A & (0xFFFF_FFFF_FFFF_FFFF >> (64 - X))
  320. */
  321. tree_id_mask = (~(uint64_t)0) >> (64 - (params->h - params->hm));
  322. leaf_id_mask = ((uint64_t)1 << params->hm) - 1; /* max value is 0x1FF when hm = 9 */
  323. *tree_id = bytes_to_u64_be(tree_id_bytes, tree_id_len) & tree_id_mask;
  324. *leaf_id = (uint32_t)(bytes_to_u64_be(leaf_id_bytes, leaf_id_len) & leaf_id_mask);
  325. return 1;
  326. }