You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
8.8 KiB

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include "hash.h"
  5. #include "hash_address.h"
  6. #include "params.h"
  7. #include "randombytes.h"
  8. #include "wots.h"
  9. #include "utils.h"
  10. #include "xmss_commons.h"
  11. #include "xmss_core.h"
  12. /**
  13. * For a given leaf index, computes the authentication path and the resulting
  14. * root node using Merkle's TreeHash algorithm.
  15. * Expects the layer and tree parts of subtree_addr to be set.
  16. */
  17. static void treehash(const xmss_params *params,
  18. unsigned char *root, unsigned char *auth_path,
  19. const unsigned char *sk_seed,
  20. const unsigned char *pub_seed,
  21. uint32_t leaf_idx, const uint32_t subtree_addr[8])
  22. {
  23. unsigned char stack[(params->tree_height+1)*params->n];
  24. unsigned int heights[params->tree_height+1];
  25. unsigned int offset = 0;
  26. /* The subtree has at most 2^20 leafs, so uint32_t suffices. */
  27. uint32_t idx;
  28. uint32_t tree_idx;
  29. /* We need all three types of addresses in parallel. */
  30. uint32_t ots_addr[8] = {0};
  31. uint32_t ltree_addr[8] = {0};
  32. uint32_t node_addr[8] = {0};
  33. /* Select the required subtree. */
  34. copy_subtree_addr(ots_addr, subtree_addr);
  35. copy_subtree_addr(ltree_addr, subtree_addr);
  36. copy_subtree_addr(node_addr, subtree_addr);
  37. set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
  38. set_type(ltree_addr, XMSS_ADDR_TYPE_LTREE);
  39. set_type(node_addr, XMSS_ADDR_TYPE_HASHTREE);
  40. for (idx = 0; idx < (uint32_t)(1 << params->tree_height); idx++) {
  41. /* Add the next leaf node to the stack. */
  42. set_ltree_addr(ltree_addr, idx);
  43. set_ots_addr(ots_addr, idx);
  44. gen_leaf_wots(params, stack + offset*params->n,
  45. sk_seed, pub_seed, ltree_addr, ots_addr);
  46. offset++;
  47. heights[offset - 1] = 0;
  48. /* If this is a node we need for the auth path.. */
  49. if ((leaf_idx ^ 0x1) == idx) {
  50. memcpy(auth_path, stack + (offset - 1)*params->n, params->n);
  51. }
  52. /* While the top-most nodes are of equal height.. */
  53. while (offset >= 2 && heights[offset - 1] == heights[offset - 2]) {
  54. /* Compute index of the new node, in the next layer. */
  55. tree_idx = (idx >> (heights[offset - 1] + 1));
  56. /* Hash the top-most nodes from the stack together. */
  57. /* Note that tree height is the 'lower' layer, even though we use
  58. the index of the new node on the 'higher' layer. This follows
  59. from the fact that we address the hash function calls. */
  60. set_tree_height(node_addr, heights[offset - 1]);
  61. set_tree_index(node_addr, tree_idx);
  62. thash_h(params, stack + (offset-2)*params->n,
  63. stack + (offset-2)*params->n, pub_seed, node_addr);
  64. offset--;
  65. /* Note that the top-most node is now one layer higher. */
  66. heights[offset - 1]++;
  67. /* If this is a node we need for the auth path.. */
  68. if (((leaf_idx >> heights[offset - 1]) ^ 0x1) == tree_idx) {
  69. memcpy(auth_path + heights[offset - 1]*params->n,
  70. stack + (offset - 1)*params->n, params->n);
  71. }
  72. }
  73. }
  74. memcpy(root, stack, params->n);
  75. }
  76. /**
  77. * Given a set of parameters, this function returns the size of the secret key.
  78. * This is implementation specific, as varying choices in tree traversal will
  79. * result in varying requirements for state storage.
  80. */
  81. unsigned long long xmss_xmssmt_core_sk_bytes(const xmss_params *params)
  82. {
  83. return params->index_bytes + 4 * params->n;
  84. }
  85. /*
  86. * Generates a XMSS key pair for a given parameter set.
  87. * Format sk: [(32bit) index || SK_SEED || SK_PRF || root || PUB_SEED]
  88. * Format pk: [root || PUB_SEED], omitting algorithm OID.
  89. */
  90. int xmss_core_keypair(const xmss_params *params,
  91. unsigned char *pk, unsigned char *sk)
  92. {
  93. /* The key generation procedure of XMSS and XMSSMT is exactly the same.
  94. The only important detail is that the right subtree must be selected;
  95. this requires us to correctly set the d=1 parameter for XMSS. */
  96. return xmssmt_core_keypair(params, pk, sk);
  97. }
  98. /**
  99. * Signs a message. Returns an array containing the signature followed by the
  100. * message and an updated secret key.
  101. */
  102. int xmss_core_sign(const xmss_params *params,
  103. unsigned char *sk,
  104. unsigned char *sm, unsigned long long *smlen,
  105. const unsigned char *m, unsigned long long mlen)
  106. {
  107. /* XMSS signatures are fundamentally an instance of XMSSMT signatures.
  108. For d=1, as is the case with XMSS, some of the calls in the XMSSMT
  109. routine become vacuous (i.e. the loop only iterates once, and address
  110. management can be simplified a bit).*/
  111. return xmssmt_core_sign(params, sk, sm, smlen, m, mlen);
  112. }
  113. /*
  114. * Generates a XMSSMT key pair for a given parameter set.
  115. * Format sk: [(ceil(h/8) bit) index || SK_SEED || SK_PRF || root || PUB_SEED]
  116. * Format pk: [root || PUB_SEED] omitting algorithm OID.
  117. */
  118. int xmssmt_core_keypair(const xmss_params *params,
  119. unsigned char *pk, unsigned char *sk)
  120. {
  121. /* We do not need the auth path in key generation, but it simplifies the
  122. code to have just one treehash routine that computes both root and path
  123. in one function. */
  124. unsigned char auth_path[params->tree_height * params->n];
  125. uint32_t top_tree_addr[8] = {0};
  126. set_layer_addr(top_tree_addr, params->d - 1);
  127. /* Initialize index to 0. */
  128. memset(sk, 0, params->index_bytes);
  129. sk += params->index_bytes;
  130. /* Initialize SK_SEED and SK_PRF. */
  131. randombytes(sk, 2 * params->n);
  132. /* Initialize PUB_SEED. */
  133. randombytes(sk + 3 * params->n, params->n);
  134. memcpy(pk + params->n, sk + 3*params->n, params->n);
  135. /* Compute root node of the top-most subtree. */
  136. treehash(params, pk, auth_path, sk, pk + params->n, 0, top_tree_addr);
  137. memcpy(sk + 2*params->n, pk, params->n);
  138. return 0;
  139. }
  140. /**
  141. * Signs a message. Returns an array containing the signature followed by the
  142. * message and an updated secret key.
  143. */
  144. int xmssmt_core_sign(const xmss_params *params,
  145. unsigned char *sk,
  146. unsigned char *sm, unsigned long long *smlen,
  147. const unsigned char *m, unsigned long long mlen)
  148. {
  149. const unsigned char *sk_seed = sk + params->index_bytes;
  150. const unsigned char *sk_prf = sk + params->index_bytes + params->n;
  151. const unsigned char *pub_root = sk + params->index_bytes + 2*params->n;
  152. const unsigned char *pub_seed = sk + params->index_bytes + 3*params->n;
  153. unsigned char root[params->n];
  154. unsigned char *mhash = root;
  155. unsigned char ots_seed[params->n];
  156. unsigned long long idx;
  157. unsigned char idx_bytes_32[32];
  158. unsigned int i;
  159. uint32_t idx_leaf;
  160. uint32_t ots_addr[8] = {0};
  161. set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
  162. /* Already put the message in the right place, to make it easier to prepend
  163. * things when computing the hash over the message. */
  164. memcpy(sm + params->sig_bytes, m, mlen);
  165. *smlen = params->sig_bytes + mlen;
  166. /* Read and use the current index from the secret key. */
  167. idx = (unsigned long)bytes_to_ull(sk, params->index_bytes);
  168. memcpy(sm, sk, params->index_bytes);
  169. /*************************************************************************
  170. * THIS IS WHERE PRODUCTION IMPLEMENTATIONS WOULD UPDATE THE SECRET KEY. *
  171. *************************************************************************/
  172. /* Increment the index in the secret key. */
  173. ull_to_bytes(sk, params->index_bytes, idx + 1);
  174. /* Compute the digest randomization value. */
  175. ull_to_bytes(idx_bytes_32, 32, idx);
  176. prf(params, sm + params->index_bytes, idx_bytes_32, sk_prf);
  177. /* Compute the message hash. */
  178. hash_message(params, mhash, sm + params->index_bytes, pub_root, idx,
  179. sm + params->sig_bytes - 4*params->n, mlen);
  180. sm += params->index_bytes + params->n;
  181. set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
  182. for (i = 0; i < params->d; i++) {
  183. idx_leaf = (idx & ((1 << params->tree_height)-1));
  184. idx = idx >> params->tree_height;
  185. set_layer_addr(ots_addr, i);
  186. set_tree_addr(ots_addr, idx);
  187. set_ots_addr(ots_addr, idx_leaf);
  188. /* Get a seed for the WOTS keypair. */
  189. get_seed(params, ots_seed, sk_seed, ots_addr);
  190. /* Compute a WOTS signature. */
  191. /* Initially, root = mhash, but on subsequent iterations it is the root
  192. of the subtree below the currently processed subtree. */
  193. wots_sign(params, sm, root, ots_seed, pub_seed, ots_addr);
  194. sm += params->wots_sig_bytes;
  195. /* Compute the authentication path for the used WOTS leaf. */
  196. treehash(params, root, sm, sk_seed, pub_seed, idx_leaf, ots_addr);
  197. sm += params->tree_height*params->n;
  198. }
  199. return 0;
  200. }