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.

365 lines
12 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 "xmss_commons.h"
  10. #include "xmss_core.h"
  11. /**
  12. * Merkle's TreeHash algorithm. Currently only used for key generation.
  13. * Computes the root node of the top-most subtree.
  14. */
  15. static void treehash(const xmss_params *params,
  16. unsigned char *root, unsigned char *auth_path,
  17. const unsigned char *sk_seed,
  18. const unsigned char *pub_seed,
  19. uint32_t leaf_idx, const uint32_t subtree_addr[8])
  20. {
  21. unsigned char stack[(params->tree_height+1)*params->n];
  22. unsigned int heights[params->tree_height+1];
  23. unsigned int offset = 0;
  24. /* The subtree has at most 2^20 leafs, so uint32_t suffices. */
  25. uint32_t idx;
  26. uint32_t tree_idx;
  27. /* We need all three types of addresses in parallel. */
  28. uint32_t ots_addr[8] = {0};
  29. uint32_t ltree_addr[8] = {0};
  30. uint32_t node_addr[8] = {0};
  31. /* Select the required subtree. */
  32. copy_subtree_addr(ots_addr, subtree_addr);
  33. copy_subtree_addr(ltree_addr, subtree_addr);
  34. copy_subtree_addr(node_addr, subtree_addr);
  35. set_type(ots_addr, 0);
  36. set_type(ltree_addr, 1);
  37. set_type(node_addr, 2);
  38. for (idx = 0; idx < (uint32_t)(1 << params->tree_height); idx++) {
  39. /* Add the next leaf node to the stack. */
  40. set_ltree_addr(ltree_addr, idx);
  41. set_ots_addr(ots_addr, idx);
  42. gen_leaf_wots(params, stack + offset*params->n,
  43. sk_seed, pub_seed, ltree_addr, ots_addr);
  44. offset++;
  45. heights[offset - 1] = 0;
  46. /* If this is a node we need for the auth path.. */
  47. if ((leaf_idx ^ 0x1) == idx) {
  48. memcpy(auth_path, stack + (offset - 1)*params->n, params->n);
  49. }
  50. /* While the top-most nodes are of equal height.. */
  51. while (offset >= 2 && heights[offset - 1] == heights[offset - 2]) {
  52. /* Compute index of the new node, in the next layer. */
  53. tree_idx = (idx >> (heights[offset - 1] + 1));
  54. /* Hash the top-most nodes from the stack together. */
  55. /* Note that tree height is the 'lower' layer, even though we use
  56. the index of the new node on the 'higher' layer. This follows
  57. from the fact that we address the hash function calls. */
  58. set_tree_height(node_addr, heights[offset - 1]);
  59. set_tree_index(node_addr, tree_idx);
  60. hash_h(params, stack + (offset-2)*params->n,
  61. stack + (offset-2)*params->n, pub_seed, node_addr);
  62. offset--;
  63. /* Note that the top-most node is now one layer higher. */
  64. heights[offset - 1]++;
  65. /* If this is a node we need for the auth path.. */
  66. if (((leaf_idx >> heights[offset - 1]) ^ 0x1) == tree_idx) {
  67. memcpy(auth_path + heights[offset - 1]*params->n,
  68. stack + (offset - 1)*params->n, params->n);
  69. }
  70. }
  71. }
  72. memcpy(root, stack, params->n);
  73. }
  74. /*
  75. * Generates a XMSS key pair for a given parameter set.
  76. * Format sk: [(32bit) index || SK_SEED || SK_PRF || PUB_SEED || root]
  77. * Format pk: [root || PUB_SEED], omitting algorithm OID.
  78. */
  79. int xmss_core_keypair(const xmss_params *params,
  80. unsigned char *pk, unsigned char *sk)
  81. {
  82. /* The key generation procedure of XMSS and XMSSMT is exactly the same.
  83. The only important detail is that the right subtree must be selected;
  84. this requires us to correctly set the d=1 parameter for XMSS. */
  85. return xmssmt_core_keypair(params, pk, sk);
  86. }
  87. /**
  88. * Signs a message.
  89. * Returns
  90. * 1. an array containing the signature followed by the message AND
  91. * 2. an updated secret key!
  92. *
  93. */
  94. int xmss_core_sign(const xmss_params *params, unsigned char *sk, unsigned char *sm, unsigned long long *smlen, const unsigned char *m, unsigned long long mlen)
  95. {
  96. uint16_t i = 0;
  97. // Extract SK
  98. uint32_t idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  99. unsigned char sk_seed[params->n];
  100. unsigned char sk_prf[params->n];
  101. unsigned char pub_seed[params->n];
  102. unsigned char hash_key[3*params->n];
  103. // index as 32 bytes string
  104. unsigned char idx_bytes_32[32];
  105. ull_to_bytes(idx_bytes_32, 32, idx);
  106. memcpy(sk_seed, sk+4, params->n);
  107. memcpy(sk_prf, sk+4+params->n, params->n);
  108. memcpy(pub_seed, sk+4+2*params->n, params->n);
  109. // Update SK
  110. sk[0] = ((idx + 1) >> 24) & 255;
  111. sk[1] = ((idx + 1) >> 16) & 255;
  112. sk[2] = ((idx + 1) >> 8) & 255;
  113. sk[3] = (idx + 1) & 255;
  114. // Secret key for this non-forward-secure version is now updated.
  115. // A production implementation should consider using a file handle instead,
  116. // and write the updated secret key at this point!
  117. // Init working params
  118. unsigned char R[params->n];
  119. unsigned char msg_h[params->n];
  120. unsigned char root[params->n];
  121. unsigned char ots_seed[params->n];
  122. uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  123. // ---------------------------------
  124. // Message Hashing
  125. // ---------------------------------
  126. // Message Hash:
  127. // First compute pseudorandom value
  128. prf(params, R, idx_bytes_32, sk_prf, params->n);
  129. // Generate hash key (R || root || idx)
  130. memcpy(hash_key, R, params->n);
  131. memcpy(hash_key+params->n, sk+4+3*params->n, params->n);
  132. ull_to_bytes(hash_key+2*params->n, params->n, idx);
  133. // Then use it for message digest
  134. h_msg(params, msg_h, m, mlen, hash_key, 3*params->n);
  135. // Start collecting signature
  136. *smlen = 0;
  137. // Copy index to signature
  138. sm[0] = (idx >> 24) & 255;
  139. sm[1] = (idx >> 16) & 255;
  140. sm[2] = (idx >> 8) & 255;
  141. sm[3] = idx & 255;
  142. sm += 4;
  143. *smlen += 4;
  144. // Copy R to signature
  145. for (i = 0; i < params->n; i++)
  146. sm[i] = R[i];
  147. sm += params->n;
  148. *smlen += params->n;
  149. // ----------------------------------
  150. // Now we start to "really sign"
  151. // ----------------------------------
  152. // Prepare Address
  153. set_type(ots_addr, 0);
  154. set_ots_addr(ots_addr, idx);
  155. // Compute seed for OTS key pair
  156. get_seed(params, ots_seed, sk_seed, ots_addr);
  157. // Compute WOTS signature
  158. wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
  159. sm += params->wots_keysize;
  160. *smlen += params->wots_keysize;
  161. treehash(params, root, sm, sk_seed, pub_seed, idx, ots_addr);
  162. sm += params->tree_height*params->n;
  163. *smlen += params->tree_height*params->n;
  164. memcpy(sm, m, mlen);
  165. *smlen += mlen;
  166. return 0;
  167. }
  168. /*
  169. * Generates a XMSSMT key pair for a given parameter set.
  170. * Format sk: [(ceil(h/8) bit) index || SK_SEED || SK_PRF || PUB_SEED]
  171. * Format pk: [root || PUB_SEED] omitting algorithm OID.
  172. */
  173. int xmssmt_core_keypair(const xmss_params *params, unsigned char *pk, unsigned char *sk)
  174. {
  175. /* We do not need the auth path in key generation, but it simplifies the
  176. code to have just one treehash routine that computes both root and path
  177. in one function. */
  178. unsigned char auth_path[params->tree_height * params->n];
  179. uint32_t top_tree_addr[8] = {0};
  180. set_layer_addr(top_tree_addr, params->d - 1);
  181. /* Initialize index to 0. */
  182. memset(sk, 0, params->index_len);
  183. sk += 4;
  184. /* Initialize SK_SEED, SK_PRF and PUB_SEED. */
  185. randombytes(sk, 3 * params->n);
  186. memcpy(pk + params->n, sk + 2*params->n, params->n);
  187. /* Compute root node of the top-most subtree. */
  188. treehash(params, pk, auth_path, sk, pk + params->n, 0, top_tree_addr);
  189. memcpy(sk + 3*params->n, pk, params->n);
  190. return 0;
  191. }
  192. /**
  193. * Signs a message.
  194. * Returns
  195. * 1. an array containing the signature followed by the message AND
  196. * 2. an updated secret key!
  197. *
  198. */
  199. int xmssmt_core_sign(const xmss_params *params, unsigned char *sk, unsigned char *sm, unsigned long long *smlen, const unsigned char *m, unsigned long long mlen)
  200. {
  201. uint64_t idx_tree;
  202. uint32_t idx_leaf;
  203. uint64_t i;
  204. unsigned char sk_seed[params->n];
  205. unsigned char sk_prf[params->n];
  206. unsigned char pub_seed[params->n];
  207. // Init working params
  208. unsigned char R[params->n];
  209. unsigned char hash_key[3*params->n];
  210. unsigned char msg_h[params->n];
  211. unsigned char root[params->n];
  212. unsigned char ots_seed[params->n];
  213. uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  214. unsigned char idx_bytes_32[32];
  215. // Extract SK
  216. unsigned long long idx = 0;
  217. for (i = 0; i < params->index_len; i++) {
  218. idx |= ((unsigned long long)sk[i]) << 8*(params->index_len - 1 - i);
  219. }
  220. memcpy(sk_seed, sk+params->index_len, params->n);
  221. memcpy(sk_prf, sk+params->index_len+params->n, params->n);
  222. memcpy(pub_seed, sk+params->index_len+2*params->n, params->n);
  223. // Update SK
  224. for (i = 0; i < params->index_len; i++) {
  225. sk[i] = ((idx + 1) >> 8*(params->index_len - 1 - i)) & 255;
  226. }
  227. // Secret key for this non-forward-secure version is now updated.
  228. // A production implementation should consider using a file handle instead,
  229. // and write the updated secret key at this point!
  230. // ---------------------------------
  231. // Message Hashing
  232. // ---------------------------------
  233. // Message Hash:
  234. // First compute pseudorandom value
  235. ull_to_bytes(idx_bytes_32, 32, idx);
  236. prf(params, R, idx_bytes_32, sk_prf, params->n);
  237. // Generate hash key (R || root || idx)
  238. memcpy(hash_key, R, params->n);
  239. memcpy(hash_key+params->n, sk+params->index_len+3*params->n, params->n);
  240. ull_to_bytes(hash_key+2*params->n, params->n, idx);
  241. // Then use it for message digest
  242. h_msg(params, msg_h, m, mlen, hash_key, 3*params->n);
  243. // Start collecting signature
  244. *smlen = 0;
  245. // Copy index to signature
  246. for (i = 0; i < params->index_len; i++) {
  247. sm[i] = (idx >> 8*(params->index_len - 1 - i)) & 255;
  248. }
  249. sm += params->index_len;
  250. *smlen += params->index_len;
  251. // Copy R to signature
  252. for (i = 0; i < params->n; i++) {
  253. sm[i] = R[i];
  254. }
  255. sm += params->n;
  256. *smlen += params->n;
  257. // ----------------------------------
  258. // Now we start to "really sign"
  259. // ----------------------------------
  260. // Handle lowest layer separately as it is slightly different...
  261. // Prepare Address
  262. set_type(ots_addr, 0);
  263. idx_tree = idx >> params->tree_height;
  264. idx_leaf = (idx & ((1 << params->tree_height)-1));
  265. set_layer_addr(ots_addr, 0);
  266. set_tree_addr(ots_addr, idx_tree);
  267. set_ots_addr(ots_addr, idx_leaf);
  268. // Compute seed for OTS key pair
  269. get_seed(params, ots_seed, sk_seed, ots_addr);
  270. // Compute WOTS signature
  271. wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
  272. sm += params->wots_keysize;
  273. *smlen += params->wots_keysize;
  274. treehash(params, root, sm, sk_seed, pub_seed, idx_leaf, ots_addr);
  275. sm += params->tree_height*params->n;
  276. *smlen += params->tree_height*params->n;
  277. // Now loop over remaining layers...
  278. unsigned int j;
  279. for (j = 1; j < params->d; j++) {
  280. // Prepare Address
  281. idx_leaf = (idx_tree & ((1 << params->tree_height)-1));
  282. idx_tree = idx_tree >> params->tree_height;
  283. set_layer_addr(ots_addr, j);
  284. set_tree_addr(ots_addr, idx_tree);
  285. set_ots_addr(ots_addr, idx_leaf);
  286. // Compute seed for OTS key pair
  287. get_seed(params, ots_seed, sk_seed, ots_addr);
  288. // Compute WOTS signature
  289. wots_sign(params, sm, root, ots_seed, pub_seed, ots_addr);
  290. sm += params->wots_keysize;
  291. *smlen += params->wots_keysize;
  292. treehash(params, root, sm, sk_seed, pub_seed, idx_leaf, ots_addr);
  293. sm += params->tree_height*params->n;
  294. *smlen += params->tree_height*params->n;
  295. }
  296. memcpy(sm, m, mlen);
  297. *smlen += mlen;
  298. return 0;
  299. }