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.

302 lines
8.7 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 "wots.h"
  8. #include "xmss_commons.h"
  9. void to_byte(unsigned char *out, unsigned long long in, uint32_t bytes)
  10. {
  11. int i;
  12. for (i = bytes-1; i >= 0; i--) {
  13. out[i] = in & 0xff;
  14. in = in >> 8;
  15. }
  16. }
  17. /**
  18. * Computes the leaf at a given address. First generates the WOTS key pair, then computes leaf using l_tree. As this happens position independent, we only require that addr encodes the right ltree-address.
  19. */
  20. void gen_leaf_wots(const xmss_params *params, unsigned char *leaf,
  21. const unsigned char *sk_seed, const unsigned char *pub_seed,
  22. uint32_t ltree_addr[8], uint32_t ots_addr[8])
  23. {
  24. unsigned char seed[params->n];
  25. unsigned char pk[params->wots_keysize];
  26. get_seed(params, seed, sk_seed, ots_addr);
  27. wots_pkgen(params, pk, seed, pub_seed, ots_addr);
  28. l_tree(params, leaf, pk, pub_seed, ltree_addr);
  29. }
  30. /**
  31. * Used for pseudorandom keygeneration,
  32. * generates the seed for the WOTS keypair at address addr
  33. *
  34. * takes params->n byte sk_seed and returns params->n byte seed using 32 byte address addr.
  35. */
  36. void get_seed(const xmss_params *params, unsigned char *seed,
  37. const unsigned char *sk_seed, uint32_t addr[8])
  38. {
  39. unsigned char bytes[32];
  40. // Make sure that chain addr, hash addr, and key bit are 0!
  41. set_chain_addr(addr, 0);
  42. set_hash_addr(addr, 0);
  43. set_key_and_mask(addr, 0);
  44. // Generate pseudorandom value
  45. addr_to_byte(bytes, addr);
  46. prf(params, seed, bytes, sk_seed, params->n);
  47. }
  48. /**
  49. * Computes a leaf from a WOTS public key using an L-tree.
  50. */
  51. void l_tree(const xmss_params *params, unsigned char *leaf, unsigned char *wots_pk,
  52. const unsigned char *pub_seed, uint32_t addr[8])
  53. {
  54. unsigned int l = params->wots_len;
  55. uint32_t i = 0;
  56. uint32_t height = 0;
  57. uint32_t bound;
  58. set_tree_height(addr, height);
  59. while (l > 1) {
  60. bound = l >> 1;
  61. for (i = 0; i < bound; i++) {
  62. set_tree_index(addr, i);
  63. hash_h(params, wots_pk + i*params->n, wots_pk + i*2*params->n, pub_seed, addr);
  64. }
  65. if (l & 1) {
  66. memcpy(wots_pk + (l >> 1)*params->n, wots_pk + (l - 1)*params->n, params->n);
  67. l = (l >> 1) + 1;
  68. }
  69. else {
  70. l = l >> 1;
  71. }
  72. height++;
  73. set_tree_height(addr, height);
  74. }
  75. memcpy(leaf, wots_pk, params->n);
  76. }
  77. /**
  78. * Computes a root node given a leaf and an authapth
  79. */
  80. static void validate_authpath(const xmss_params *params, unsigned char *root,
  81. const unsigned char *leaf, unsigned long leafidx,
  82. const unsigned char *authpath,
  83. const unsigned char *pub_seed, uint32_t addr[8])
  84. {
  85. uint32_t i, j;
  86. unsigned char buffer[2*params->n];
  87. // If leafidx is odd (last bit = 1), current path element is a right child and authpath has to go to the left.
  88. // Otherwise, it is the other way around
  89. if (leafidx & 1) {
  90. for (j = 0; j < params->n; j++) {
  91. buffer[params->n + j] = leaf[j];
  92. buffer[j] = authpath[j];
  93. }
  94. }
  95. else {
  96. for (j = 0; j < params->n; j++) {
  97. buffer[j] = leaf[j];
  98. buffer[params->n + j] = authpath[j];
  99. }
  100. }
  101. authpath += params->n;
  102. for (i = 0; i < params->tree_height-1; i++) {
  103. set_tree_height(addr, i);
  104. leafidx >>= 1;
  105. set_tree_index(addr, leafidx);
  106. if (leafidx & 1) {
  107. hash_h(params, buffer + params->n, buffer, pub_seed, addr);
  108. for (j = 0; j < params->n; j++) {
  109. buffer[j] = authpath[j];
  110. }
  111. }
  112. else {
  113. hash_h(params, buffer, buffer, pub_seed, addr);
  114. for (j = 0; j < params->n; j++) {
  115. buffer[j + params->n] = authpath[j];
  116. }
  117. }
  118. authpath += params->n;
  119. }
  120. set_tree_height(addr, params->tree_height - 1);
  121. leafidx >>= 1;
  122. set_tree_index(addr, leafidx);
  123. hash_h(params, root, buffer, pub_seed, addr);
  124. }
  125. /**
  126. * Verifies a given message signature pair under a given public key.
  127. */
  128. int xmss_core_sign_open(const xmss_params *params,
  129. unsigned char *m, unsigned long long *mlen,
  130. const unsigned char *sm, unsigned long long smlen,
  131. const unsigned char *pk)
  132. {
  133. unsigned long long i;
  134. unsigned long idx = 0;
  135. unsigned char wots_pk[params->wots_keysize];
  136. unsigned char pkhash[params->n];
  137. unsigned char root[params->n];
  138. unsigned char msg_h[params->n];
  139. unsigned char hash_key[3*params->n];
  140. unsigned char pub_seed[params->n];
  141. memcpy(pub_seed, pk + params->n, params->n);
  142. // Init addresses
  143. uint32_t ots_addr[8] = {0};
  144. uint32_t ltree_addr[8] = {0};
  145. uint32_t node_addr[8] = {0};
  146. set_type(ots_addr, 0);
  147. set_type(ltree_addr, 1);
  148. set_type(node_addr, 2);
  149. *mlen = smlen - params->bytes;
  150. // Extract index
  151. for (i = 0; i < params->index_len; i++) {
  152. idx |= ((unsigned long long)sm[i]) << (8*(params->index_len - 1 - i));
  153. }
  154. // Generate hash key (R || root || idx)
  155. memcpy(hash_key, sm + params->index_len, params->n);
  156. memcpy(hash_key + params->n, pk, params->n);
  157. to_byte(hash_key + 2*params->n, idx, params->n);
  158. // hash message
  159. h_msg(params, msg_h, sm + params->bytes, *mlen, hash_key, 3*params->n);
  160. sm += params->index_len + params->n;
  161. // Prepare Address
  162. set_ots_addr(ots_addr, idx);
  163. // Check WOTS signature
  164. wots_pk_from_sig(params, wots_pk, sm, msg_h, pub_seed, ots_addr);
  165. sm += params->wots_keysize;
  166. // Compute Ltree
  167. set_ltree_addr(ltree_addr, idx);
  168. l_tree(params, pkhash, wots_pk, pub_seed, ltree_addr);
  169. // Compute root
  170. validate_authpath(params, root, pkhash, idx, sm, pub_seed, node_addr);
  171. sm += params->tree_height*params->n;
  172. for (i = 0; i < params->n; i++) {
  173. if (root[i] != pk[i]) {
  174. for (i = 0; i < *mlen; i++) {
  175. m[i] = 0;
  176. }
  177. *mlen = -1;
  178. return -1;
  179. }
  180. }
  181. for (i = 0; i < *mlen; i++) {
  182. m[i] = sm[i];
  183. }
  184. return 0;
  185. }
  186. /**
  187. * Verifies a given message signature pair under a given public key.
  188. */
  189. int xmssmt_core_sign_open(const xmss_params *params,
  190. unsigned char *m, unsigned long long *mlen,
  191. const unsigned char *sm, unsigned long long smlen,
  192. const unsigned char *pk)
  193. {
  194. uint32_t idx_leaf;
  195. unsigned long long i;
  196. unsigned long long idx = 0;
  197. unsigned char wots_pk[params->wots_keysize];
  198. unsigned char pkhash[params->n];
  199. unsigned char root[params->n];
  200. unsigned char *msg_h = root;
  201. unsigned char hash_key[3*params->n];
  202. const unsigned char *pub_seed = pk + params->n;
  203. // Init addresses
  204. uint32_t ots_addr[8] = {0};
  205. uint32_t ltree_addr[8] = {0};
  206. uint32_t node_addr[8] = {0};
  207. set_type(ots_addr, 0);
  208. set_type(ltree_addr, 1);
  209. set_type(node_addr, 2);
  210. *mlen = smlen - params->bytes;
  211. // Extract index
  212. for (i = 0; i < params->index_len; i++) {
  213. idx |= ((unsigned long long)sm[i]) << (8*(params->index_len - 1 - i));
  214. }
  215. // Generate hash key (R || root || idx)
  216. memcpy(hash_key, sm + params->index_len, params->n);
  217. memcpy(hash_key + params->n, pk, params->n);
  218. to_byte(hash_key + 2*params->n, idx, params->n);
  219. // hash message
  220. h_msg(params, msg_h, sm + params->bytes, *mlen, hash_key, 3*params->n);
  221. sm += params->index_len + params->n;
  222. for (i = 0; i < params->d; i++) {
  223. // Prepare Address
  224. idx_leaf = (idx & ((1 << params->tree_height)-1));
  225. idx = idx >> params->tree_height;
  226. set_layer_addr(ots_addr, i);
  227. set_layer_addr(ltree_addr, i);
  228. set_layer_addr(node_addr, i);
  229. set_tree_addr(ltree_addr, idx);
  230. set_tree_addr(ots_addr, idx);
  231. set_tree_addr(node_addr, idx);
  232. set_ots_addr(ots_addr, idx_leaf);
  233. // Check WOTS signature
  234. wots_pk_from_sig(params, wots_pk, sm, root, pub_seed, ots_addr);
  235. sm += params->wots_keysize;
  236. // Compute Ltree
  237. set_ltree_addr(ltree_addr, idx_leaf);
  238. l_tree(params, pkhash, wots_pk, pub_seed, ltree_addr);
  239. // Compute root
  240. validate_authpath(params, root, pkhash, idx_leaf, sm, pub_seed, node_addr);
  241. sm += params->tree_height*params->n;
  242. }
  243. for (i = 0; i < params->n; i++) {
  244. if (root[i] != pk[i]) {
  245. for (i = 0; i < *mlen; i++) {
  246. m[i] = 0;
  247. }
  248. *mlen = -1;
  249. return -1;
  250. }
  251. }
  252. for (i = 0; i < *mlen; i++) {
  253. m[i] = sm[i];
  254. }
  255. return 0;
  256. }