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.

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