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.

417 lines
12 KiB

  1. /*
  2. xmss.c version 20160722
  3. Andreas Hülsing
  4. Joost Rijneveld
  5. Public domain.
  6. */
  7. #include "xmss.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdint.h>
  11. #include <math.h>
  12. #include "randombytes.h"
  13. #include "wots.h"
  14. #include "hash.h"
  15. //#include "prg.h"
  16. #include "xmss_commons.h"
  17. #include "hash_address.h"
  18. #include "params.h"
  19. // For testing
  20. #include "stdio.h"
  21. /**
  22. * Merkle's TreeHash algorithm. The address only needs to initialize the first 78 bits of addr. Everything else will be set by treehash.
  23. * Currently only used for key generation.
  24. *
  25. */
  26. static void treehash(unsigned char *node, uint16_t height, uint32_t index, const unsigned char *sk_seed, const unsigned char *pub_seed, const uint32_t addr[8])
  27. {
  28. uint32_t idx = index;
  29. // use three different addresses because at this point we use all three formats in parallel
  30. uint32_t ots_addr[8];
  31. uint32_t ltree_addr[8];
  32. uint32_t node_addr[8];
  33. // only copy layer and tree address parts
  34. memcpy(ots_addr, addr, 12);
  35. // type = ots
  36. setType(ots_addr, 0);
  37. memcpy(ltree_addr, addr, 12);
  38. setType(ltree_addr, 1);
  39. memcpy(node_addr, addr, 12);
  40. setType(node_addr, 2);
  41. uint32_t lastnode, i;
  42. unsigned char stack[(height+1)*XMSS_N];
  43. uint16_t stacklevels[height+1];
  44. unsigned int stackoffset=0;
  45. lastnode = idx+(1 << height);
  46. for (; idx < lastnode; idx++) {
  47. setLtreeADRS(ltree_addr, idx);
  48. setOTSADRS(ots_addr, idx);
  49. gen_leaf_wots(stack+stackoffset*XMSS_N, sk_seed, pub_seed, ltree_addr, ots_addr);
  50. stacklevels[stackoffset] = 0;
  51. stackoffset++;
  52. while (stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2]) {
  53. setTreeHeight(node_addr, stacklevels[stackoffset-1]);
  54. setTreeIndex(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
  55. hash_h(stack+(stackoffset-2)*XMSS_N, stack+(stackoffset-2)*XMSS_N, pub_seed,
  56. node_addr, XMSS_N);
  57. stacklevels[stackoffset-2]++;
  58. stackoffset--;
  59. }
  60. }
  61. for (i=0; i < XMSS_N; i++)
  62. node[i] = stack[i];
  63. }
  64. /**
  65. * Computes the authpath and the root. This method is using a lot of space as we build the whole tree and then select the authpath nodes.
  66. * For more efficient algorithms see e.g. the chapter on hash-based signatures in Bernstein, Buchmann, Dahmen. "Post-quantum Cryptography", Springer 2009.
  67. * It returns the authpath in "authpath" with the node on level 0 at index 0.
  68. */
  69. static void compute_authpath_wots(unsigned char *root, unsigned char *authpath, unsigned long leaf_idx, const unsigned char *sk_seed, unsigned char *pub_seed, uint32_t addr[8])
  70. {
  71. uint32_t i, j, level;
  72. unsigned char tree[2*(1<<XMSS_TREEHEIGHT)*XMSS_N];
  73. uint32_t ots_addr[8];
  74. uint32_t ltree_addr[8];
  75. uint32_t node_addr[8];
  76. memcpy(ots_addr, addr, 12);
  77. setType(ots_addr, 0);
  78. memcpy(ltree_addr, addr, 12);
  79. setType(ltree_addr, 1);
  80. memcpy(node_addr, addr, 12);
  81. setType(node_addr, 2);
  82. // Compute all leaves
  83. for (i = 0; i < (1U << XMSS_TREEHEIGHT); i++) {
  84. setLtreeADRS(ltree_addr, i);
  85. setOTSADRS(ots_addr, i);
  86. gen_leaf_wots(tree+((1<<XMSS_TREEHEIGHT)*XMSS_N + i*XMSS_N), sk_seed, pub_seed, ltree_addr, ots_addr);
  87. }
  88. level = 0;
  89. // Compute tree:
  90. // Outer loop: For each inner layer
  91. for (i = (1<<XMSS_TREEHEIGHT); i > 1; i>>=1) {
  92. setTreeHeight(node_addr, level);
  93. // Inner loop: for each pair of sibling nodes
  94. for (j = 0; j < i; j+=2) {
  95. setTreeIndex(node_addr, j>>1);
  96. hash_h(tree + (i>>1)*XMSS_N + (j>>1) * XMSS_N, tree + i*XMSS_N + j*XMSS_N, pub_seed, node_addr, XMSS_N);
  97. }
  98. level++;
  99. }
  100. // copy authpath
  101. for (i=0; i < XMSS_TREEHEIGHT; i++)
  102. memcpy(authpath + i*XMSS_N, tree + ((1<<XMSS_TREEHEIGHT)>>i)*XMSS_N + ((leaf_idx >> i) ^ 1) * XMSS_N, XMSS_N);
  103. // copy root
  104. memcpy(root, tree+XMSS_N, XMSS_N);
  105. }
  106. /*
  107. * Generates a XMSS key pair for a given parameter set.
  108. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
  109. * Format pk: [root || PUB_SEED] omitting algo oid.
  110. */
  111. int xmss_keypair(unsigned char *pk, unsigned char *sk)
  112. {
  113. // Set idx = 0
  114. sk[0] = 0;
  115. sk[1] = 0;
  116. sk[2] = 0;
  117. sk[3] = 0;
  118. // Init SK_SEED (XMSS_N byte), SK_PRF (XMSS_N byte), and PUB_SEED (XMSS_N byte)
  119. randombytes(sk+4, 3*XMSS_N);
  120. // Copy PUB_SEED to public key
  121. memcpy(pk+XMSS_N, sk+4+2*XMSS_N, XMSS_N);
  122. uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  123. // Compute root
  124. treehash(pk, XMSS_TREEHEIGHT, 0, sk+4, sk+4+2*XMSS_N, addr);
  125. // copy root to sk
  126. memcpy(sk+4+3*XMSS_N, pk, XMSS_N);
  127. return 0;
  128. }
  129. /**
  130. * Signs a message.
  131. * Returns
  132. * 1. an array containing the signature followed by the message AND
  133. * 2. an updated secret key!
  134. *
  135. */
  136. int xmss_sign(unsigned char *sk, unsigned char *sig_msg, unsigned long long *sig_msg_len, const unsigned char *msg, unsigned long long msglen)
  137. {
  138. uint16_t i = 0;
  139. // Extract SK
  140. uint32_t idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  141. unsigned char sk_seed[XMSS_N];
  142. memcpy(sk_seed, sk+4, XMSS_N);
  143. unsigned char sk_prf[XMSS_N];
  144. memcpy(sk_prf, sk+4+XMSS_N, XMSS_N);
  145. unsigned char pub_seed[XMSS_N];
  146. memcpy(pub_seed, sk+4+2*XMSS_N, XMSS_N);
  147. // index as 32 bytes string
  148. unsigned char idx_bytes_32[32];
  149. to_byte(idx_bytes_32, idx, 32);
  150. unsigned char hash_key[3*XMSS_N];
  151. // Update SK
  152. sk[0] = ((idx + 1) >> 24) & 255;
  153. sk[1] = ((idx + 1) >> 16) & 255;
  154. sk[2] = ((idx + 1) >> 8) & 255;
  155. sk[3] = (idx + 1) & 255;
  156. // -- Secret key for this non-forward-secure version is now updated.
  157. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  158. // Init working params
  159. unsigned char R[XMSS_N];
  160. unsigned char msg_h[XMSS_N];
  161. unsigned char root[XMSS_N];
  162. unsigned char ots_seed[XMSS_N];
  163. uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  164. // ---------------------------------
  165. // Message Hashing
  166. // ---------------------------------
  167. // Message Hash:
  168. // First compute pseudorandom value
  169. prf(R, idx_bytes_32, sk_prf, XMSS_N);
  170. // Generate hash key (R || root || idx)
  171. memcpy(hash_key, R, XMSS_N);
  172. memcpy(hash_key+XMSS_N, sk+4+3*XMSS_N, XMSS_N);
  173. to_byte(hash_key+2*XMSS_N, idx, XMSS_N);
  174. // Then use it for message digest
  175. h_msg(msg_h, msg, msglen, hash_key, 3*XMSS_N, XMSS_N);
  176. // Start collecting signature
  177. *sig_msg_len = 0;
  178. // Copy index to signature
  179. sig_msg[0] = (idx >> 24) & 255;
  180. sig_msg[1] = (idx >> 16) & 255;
  181. sig_msg[2] = (idx >> 8) & 255;
  182. sig_msg[3] = idx & 255;
  183. sig_msg += 4;
  184. *sig_msg_len += 4;
  185. // Copy R to signature
  186. for (i = 0; i < XMSS_N; i++)
  187. sig_msg[i] = R[i];
  188. sig_msg += XMSS_N;
  189. *sig_msg_len += XMSS_N;
  190. // ----------------------------------
  191. // Now we start to "really sign"
  192. // ----------------------------------
  193. // Prepare Address
  194. setType(ots_addr, 0);
  195. setOTSADRS(ots_addr, idx);
  196. // Compute seed for OTS key pair
  197. get_seed(ots_seed, sk_seed, ots_addr);
  198. // Compute WOTS signature
  199. wots_sign(sig_msg, msg_h, ots_seed, pub_seed, ots_addr);
  200. sig_msg += XMSS_WOTS_KEYSIZE;
  201. *sig_msg_len += XMSS_WOTS_KEYSIZE;
  202. compute_authpath_wots(root, sig_msg, idx, sk_seed, pub_seed, ots_addr);
  203. sig_msg += XMSS_TREEHEIGHT*XMSS_N;
  204. *sig_msg_len += XMSS_TREEHEIGHT*XMSS_N;
  205. //Whipe secret elements?
  206. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  207. memcpy(sig_msg, msg, msglen);
  208. *sig_msg_len += msglen;
  209. return 0;
  210. }
  211. /*
  212. * Generates a XMSSMT key pair for a given parameter set.
  213. * Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || PUB_SEED]
  214. * Format pk: [root || PUB_SEED] omitting algo oid.
  215. */
  216. int xmssmt_keypair(unsigned char *pk, unsigned char *sk)
  217. {
  218. uint16_t i;
  219. // Set idx = 0
  220. for (i = 0; i < XMSS_INDEX_LEN; i++) {
  221. sk[i] = 0;
  222. }
  223. // Init SK_SEED (XMSS_N byte), SK_PRF (XMSS_N byte), and PUB_SEED (XMSS_N byte)
  224. randombytes(sk+XMSS_INDEX_LEN, 3*XMSS_N);
  225. // Copy PUB_SEED to public key
  226. memcpy(pk+XMSS_N, sk+XMSS_INDEX_LEN+2*XMSS_N, XMSS_N);
  227. // Set address to point on the single tree on layer d-1
  228. uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  229. setLayerADRS(addr, (XMSS_D-1));
  230. // Compute root
  231. treehash(pk, XMSS_TREEHEIGHT, 0, sk+XMSS_INDEX_LEN, pk+XMSS_N, addr);
  232. memcpy(sk+XMSS_INDEX_LEN+3*XMSS_N, pk, XMSS_N);
  233. return 0;
  234. }
  235. /**
  236. * Signs a message.
  237. * Returns
  238. * 1. an array containing the signature followed by the message AND
  239. * 2. an updated secret key!
  240. *
  241. */
  242. int xmssmt_sign(unsigned char *sk, unsigned char *sig_msg, unsigned long long *sig_msg_len, const unsigned char *msg, unsigned long long msglen)
  243. {
  244. uint64_t idx_tree;
  245. uint32_t idx_leaf;
  246. uint64_t i;
  247. unsigned char sk_seed[XMSS_N];
  248. unsigned char sk_prf[XMSS_N];
  249. unsigned char pub_seed[XMSS_N];
  250. // Init working params
  251. unsigned char R[XMSS_N];
  252. unsigned char hash_key[3*XMSS_N];
  253. unsigned char msg_h[XMSS_N];
  254. unsigned char root[XMSS_N];
  255. unsigned char ots_seed[XMSS_N];
  256. uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  257. unsigned char idx_bytes_32[32];
  258. // Extract SK
  259. unsigned long long idx = 0;
  260. for (i = 0; i < XMSS_INDEX_LEN; i++) {
  261. idx |= ((unsigned long long)sk[i]) << 8*(XMSS_INDEX_LEN - 1 - i);
  262. }
  263. memcpy(sk_seed, sk+XMSS_INDEX_LEN, XMSS_N);
  264. memcpy(sk_prf, sk+XMSS_INDEX_LEN+XMSS_N, XMSS_N);
  265. memcpy(pub_seed, sk+XMSS_INDEX_LEN+2*XMSS_N, XMSS_N);
  266. // Update SK
  267. for (i = 0; i < XMSS_INDEX_LEN; i++) {
  268. sk[i] = ((idx + 1) >> 8*(XMSS_INDEX_LEN - 1 - i)) & 255;
  269. }
  270. // -- Secret key for this non-forward-secure version is now updated.
  271. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  272. // ---------------------------------
  273. // Message Hashing
  274. // ---------------------------------
  275. // Message Hash:
  276. // First compute pseudorandom value
  277. to_byte(idx_bytes_32, idx, 32);
  278. prf(R, idx_bytes_32, sk_prf, XMSS_N);
  279. // Generate hash key (R || root || idx)
  280. memcpy(hash_key, R, XMSS_N);
  281. memcpy(hash_key+XMSS_N, sk+XMSS_INDEX_LEN+3*XMSS_N, XMSS_N);
  282. to_byte(hash_key+2*XMSS_N, idx, XMSS_N);
  283. // Then use it for message digest
  284. h_msg(msg_h, msg, msglen, hash_key, 3*XMSS_N, XMSS_N);
  285. // Start collecting signature
  286. *sig_msg_len = 0;
  287. // Copy index to signature
  288. for (i = 0; i < XMSS_INDEX_LEN; i++) {
  289. sig_msg[i] = (idx >> 8*(XMSS_INDEX_LEN - 1 - i)) & 255;
  290. }
  291. sig_msg += XMSS_INDEX_LEN;
  292. *sig_msg_len += XMSS_INDEX_LEN;
  293. // Copy R to signature
  294. for (i=0; i < XMSS_N; i++)
  295. sig_msg[i] = R[i];
  296. sig_msg += XMSS_N;
  297. *sig_msg_len += XMSS_N;
  298. // ----------------------------------
  299. // Now we start to "really sign"
  300. // ----------------------------------
  301. // Handle lowest layer separately as it is slightly different...
  302. // Prepare Address
  303. setType(ots_addr, 0);
  304. idx_tree = idx >> XMSS_TREEHEIGHT;
  305. idx_leaf = (idx & ((1 << XMSS_TREEHEIGHT)-1));
  306. setLayerADRS(ots_addr, 0);
  307. setTreeADRS(ots_addr, idx_tree);
  308. setOTSADRS(ots_addr, idx_leaf);
  309. // Compute seed for OTS key pair
  310. get_seed(ots_seed, sk_seed, ots_addr);
  311. // Compute WOTS signature
  312. wots_sign(sig_msg, msg_h, ots_seed, pub_seed, ots_addr);
  313. sig_msg += XMSS_WOTS_KEYSIZE;
  314. *sig_msg_len += XMSS_WOTS_KEYSIZE;
  315. compute_authpath_wots(root, sig_msg, idx_leaf, sk_seed, pub_seed, ots_addr);
  316. sig_msg += XMSS_TREEHEIGHT*XMSS_N;
  317. *sig_msg_len += XMSS_TREEHEIGHT*XMSS_N;
  318. // Now loop over remaining layers...
  319. unsigned int j;
  320. for (j = 1; j < XMSS_D; j++) {
  321. // Prepare Address
  322. idx_leaf = (idx_tree & ((1 << XMSS_TREEHEIGHT)-1));
  323. idx_tree = idx_tree >> XMSS_TREEHEIGHT;
  324. setLayerADRS(ots_addr, j);
  325. setTreeADRS(ots_addr, idx_tree);
  326. setOTSADRS(ots_addr, idx_leaf);
  327. // Compute seed for OTS key pair
  328. get_seed(ots_seed, sk_seed, ots_addr);
  329. // Compute WOTS signature
  330. wots_sign(sig_msg, root, ots_seed, pub_seed, ots_addr);
  331. sig_msg += XMSS_WOTS_KEYSIZE;
  332. *sig_msg_len += XMSS_WOTS_KEYSIZE;
  333. compute_authpath_wots(root, sig_msg, idx_leaf, sk_seed, pub_seed, ots_addr);
  334. sig_msg += XMSS_TREEHEIGHT*XMSS_N;
  335. *sig_msg_len += XMSS_TREEHEIGHT*XMSS_N;
  336. }
  337. //Whipe secret elements?
  338. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  339. memcpy(sig_msg, msg, msglen);
  340. *sig_msg_len += msglen;
  341. return 0;
  342. }