選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

250 行
9.2 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. * Derives a XMSSMT key pair for a given parameter set.
  115. * Seed must be 3*n long.
  116. * Format sk: [(ceil(h/8) bit) index || SK_SEED || SK_PRF || root || PUB_SEED]
  117. * Format pk: [root || PUB_SEED] omitting algorithm OID.
  118. */
  119. int xmssmt_core_seed_keypair(const xmss_params *params,
  120. unsigned char *pk, unsigned char *sk,
  121. unsigned char *seed)
  122. {
  123. /* We do not need the auth path in key generation, but it simplifies the
  124. code to have just one treehash routine that computes both root and path
  125. in one function. */
  126. unsigned char auth_path[params->tree_height * params->n];
  127. uint32_t top_tree_addr[8] = {0};
  128. set_layer_addr(top_tree_addr, params->d - 1);
  129. /* Initialize index to 0. */
  130. memset(sk, 0, params->index_bytes);
  131. sk += params->index_bytes;
  132. /* Initialize SK_SEED and SK_PRF. */
  133. memcpy(sk, seed, 2 * params->n);
  134. /* Initialize PUB_SEED. */
  135. memcpy(sk + 3 * params->n, seed + 2 * params->n, params->n);
  136. memcpy(pk + params->n, sk + 3*params->n, params->n);
  137. /* Compute root node of the top-most subtree. */
  138. treehash(params, pk, auth_path, sk, pk + params->n, 0, top_tree_addr);
  139. memcpy(sk + 2*params->n, pk, params->n);
  140. return 0;
  141. }
  142. /*
  143. * Generates a XMSSMT key pair for a given parameter set.
  144. * Format sk: [(ceil(h/8) bit) index || SK_SEED || SK_PRF || root || PUB_SEED]
  145. * Format pk: [root || PUB_SEED] omitting algorithm OID.
  146. */
  147. int xmssmt_core_keypair(const xmss_params *params,
  148. unsigned char *pk, unsigned char *sk)
  149. {
  150. unsigned char seed[3 * params->n];
  151. randombytes(seed, 3 * params->n);
  152. xmssmt_core_seed_keypair(params, pk, sk, seed);
  153. return 0;
  154. }
  155. /**
  156. * Signs a message. Returns an array containing the signature followed by the
  157. * message and an updated secret key.
  158. */
  159. int xmssmt_core_sign(const xmss_params *params,
  160. unsigned char *sk,
  161. unsigned char *sm, unsigned long long *smlen,
  162. const unsigned char *m, unsigned long long mlen)
  163. {
  164. const unsigned char *sk_seed = sk + params->index_bytes;
  165. const unsigned char *sk_prf = sk + params->index_bytes + params->n;
  166. const unsigned char *pub_root = sk + params->index_bytes + 2*params->n;
  167. const unsigned char *pub_seed = sk + params->index_bytes + 3*params->n;
  168. unsigned char root[params->n];
  169. unsigned char *mhash = root;
  170. unsigned long long idx;
  171. unsigned char idx_bytes_32[32];
  172. unsigned int i;
  173. uint32_t idx_leaf;
  174. uint32_t ots_addr[8] = {0};
  175. set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
  176. /* Already put the message in the right place, to make it easier to prepend
  177. * things when computing the hash over the message. */
  178. memcpy(sm + params->sig_bytes, m, mlen);
  179. *smlen = params->sig_bytes + mlen;
  180. /* Read and use the current index from the secret key. */
  181. idx = (unsigned long)bytes_to_ull(sk, params->index_bytes);
  182. memcpy(sm, sk, params->index_bytes);
  183. /*************************************************************************
  184. * THIS IS WHERE PRODUCTION IMPLEMENTATIONS WOULD UPDATE THE SECRET KEY. *
  185. *************************************************************************/
  186. /* Increment the index in the secret key. */
  187. ull_to_bytes(sk, params->index_bytes, idx + 1);
  188. /* Compute the digest randomization value. */
  189. ull_to_bytes(idx_bytes_32, 32, idx);
  190. prf(params, sm + params->index_bytes, idx_bytes_32, sk_prf);
  191. /* Compute the message hash. */
  192. hash_message(params, mhash, sm + params->index_bytes, pub_root, idx,
  193. sm + params->sig_bytes - 4*params->n, mlen);
  194. sm += params->index_bytes + params->n;
  195. set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
  196. for (i = 0; i < params->d; i++) {
  197. idx_leaf = (idx & ((1 << params->tree_height)-1));
  198. idx = idx >> params->tree_height;
  199. set_layer_addr(ots_addr, i);
  200. set_tree_addr(ots_addr, idx);
  201. set_ots_addr(ots_addr, idx_leaf);
  202. /* Compute a WOTS signature. */
  203. /* Initially, root = mhash, but on subsequent iterations it is the root
  204. of the subtree below the currently processed subtree. */
  205. wots_sign(params, sm, root, sk_seed, pub_seed, ots_addr);
  206. sm += params->wots_sig_bytes;
  207. /* Compute the authentication path for the used WOTS leaf. */
  208. treehash(params, root, sm, sk_seed, pub_seed, idx_leaf, ots_addr);
  209. sm += params->tree_height*params->n;
  210. }
  211. return 0;
  212. }