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.

530 lines
14 KiB

  1. /*
  2. xmss.c version 20150811
  3. Andreas Hülsing
  4. Public domain.
  5. */
  6. #include "xmss.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <math.h>
  11. #include "randombytes.h"
  12. #include "wots.h"
  13. #include "hash.h"
  14. #include "prg.h"
  15. #include "xmss_commons.h"
  16. // For testing
  17. #include "stdio.h"
  18. /**
  19. * Macros used to manipulate the respective fields
  20. * in the 16byte hash address
  21. */
  22. #define SET_OTS_BIT(a, b) {\
  23. a[9] = (a[9] & 253) | (b << 1);}
  24. #define SET_OTS_ADDRESS(a, v) {\
  25. a[12] = (a[12] & 1) | ((v << 1) & 255);\
  26. a[11] = (v >> 7) & 255;\
  27. a[10] = (v >> 15) & 255;\
  28. a[9] = (a[9] & 254) | ((v >> 23) & 1);}
  29. #define ZEROISE_OTS_ADDR(a) {\
  30. a[12] = (a[12] & 254);\
  31. a[13] = 0;\
  32. a[14] = 0;\
  33. a[15] = 0;}
  34. #define SET_LTREE_BIT(a, b) {\
  35. a[9] = (a[9] & 254) | b;}
  36. #define SET_LTREE_ADDRESS(a, v) {\
  37. a[12] = v & 255;\
  38. a[11] = (v >> 8) & 255;\
  39. a[10] = (v >> 16) & 255;}
  40. #define SET_LTREE_TREE_HEIGHT(a, v) {\
  41. a[13] = (a[13] & 3) | ((v << 2) & 255);}
  42. #define SET_LTREE_TREE_INDEX(a, v) {\
  43. a[15] = (a[15] & 3) | ((v << 2) & 255);\
  44. a[14] = (v >> 6) & 255;\
  45. a[13] = (a[13] & 252) | ((v >> 14) & 3);}
  46. #define SET_NODE_PADDING(a) {\
  47. a[10] = 0;\
  48. a[11] = a[11] & 3;}
  49. #define SET_NODE_TREE_HEIGHT(a, v) {\
  50. a[12] = (a[12] & 3) | ((v << 2) & 255);\
  51. a[11] = (a[11] & 252) | ((v >> 6) & 3);}
  52. #define SET_NODE_TREE_INDEX(a, v) {\
  53. a[15] = (a[15] & 3) | ((v << 2) & 255);\
  54. a[14] = (v >> 6) & 255;\
  55. a[13] = (v >> 14) & 255;\
  56. a[12] = (a[12] & 252) | ((v >> 22) & 3);}
  57. /**
  58. * Used for pseudorandom keygeneration,
  59. * generates the seed for the WOTS keypair at address addr
  60. */
  61. static void get_seed(unsigned char seed[32], const unsigned char *sk_seed, unsigned char addr[16])
  62. {
  63. // Make sure that chain addr, hash addr, and key bit are 0!
  64. ZEROISE_OTS_ADDR(addr);
  65. // Generate pseudorandom value
  66. prg_with_counter(seed, 32, sk_seed, 32, addr);
  67. }
  68. /**
  69. * Initialize xmss params struct
  70. * parameter names are the same as in the draft
  71. */
  72. void xmss_set_params(xmss_params *params, int m, int n, int h, int w)
  73. {
  74. params->h = h;
  75. params->m = m;
  76. params->n = n;
  77. wots_params wots_par;
  78. wots_set_params(&wots_par, m, n, w);
  79. params->wots_par = &wots_par;
  80. }
  81. /**
  82. * Computes a leaf from a WOTS public key using an L-tree.
  83. */
  84. static void l_tree(unsigned char *leaf, unsigned char *wots_pk, const xmss_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  85. {
  86. uint l = params->wots_par->len;
  87. uint n = params->n;
  88. unsigned long i = 0;
  89. uint height = 0;
  90. //ADRS.setTreeHeight(0);
  91. SET_LTREE_TREE_HEIGHT(addr,height);
  92. unsigned long bound;
  93. while ( l > 1 )
  94. {
  95. bound = l >> 1; //floor(l / 2);
  96. for ( i = 0; i < bound; i = i + 1 ) {
  97. //ADRS.setTreeIndex(i);
  98. SET_LTREE_TREE_INDEX(addr,i);
  99. //wots_pk[i] = RAND_HASH(pk[2i], pk[2i + 1], SEED, ADRS);
  100. hash_2n_n(wots_pk+i*n,wots_pk+i*2*n, pub_seed, addr, n);
  101. }
  102. //if ( l % 2 == 1 ) {
  103. if(l&1)
  104. {
  105. //pk[floor(l / 2) + 1] = pk[l];
  106. memcpy(wots_pk+(l>>1)*n,wots_pk+(l-1)*n, n);
  107. //l = ceil(l / 2);
  108. l=(l>>1)+1;
  109. }
  110. else
  111. {
  112. //l = ceil(l / 2);
  113. l=(l>>1);
  114. }
  115. //ADRS.setTreeHeight(ADRS.getTreeHeight() + 1);
  116. height++;
  117. SET_LTREE_TREE_HEIGHT(addr,height);
  118. }
  119. //return pk[0];
  120. memcpy(leaf,wots_pk,n);
  121. }
  122. /**
  123. * 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.
  124. */
  125. static void gen_leaf_wots(unsigned char *leaf, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, unsigned char ltree_addr[16], unsigned char ots_addr[16])
  126. {
  127. unsigned char seed[32];
  128. unsigned char pk[params->wots_par->keysize];
  129. get_seed(seed, sk_seed, ots_addr);
  130. wots_pkgen(pk, seed, params->wots_par, pub_seed, ots_addr);
  131. l_tree(leaf, pk, params, pub_seed, ltree_addr);
  132. }
  133. /**
  134. * Merkle's TreeHash algorithm. The address only needs to initialize the first 78 bits of addr. Everything else will be set by treehash.
  135. * Currently only used for key generation.
  136. *
  137. */
  138. static void treehash(unsigned char *node, int height, int index, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, const unsigned char addr[16])
  139. {
  140. uint idx = index;
  141. uint n = params->n;
  142. // use three different addresses because at this point we use all three formats in parallel
  143. unsigned char ots_addr[16];
  144. unsigned char ltree_addr[16];
  145. unsigned char node_addr[16];
  146. memcpy(ots_addr, addr, 10);
  147. SET_OTS_BIT(ots_addr, 1);
  148. memcpy(ltree_addr, addr, 10);
  149. SET_OTS_BIT(ltree_addr, 0);
  150. SET_LTREE_BIT(ltree_addr, 1);
  151. memcpy(node_addr, ltree_addr, 10);
  152. SET_LTREE_BIT(node_addr, 0);
  153. SET_NODE_PADDING(node_addr);
  154. int lastnode,i;
  155. unsigned char stack[(height+1)*n];
  156. unsigned int stacklevels[height+1];
  157. unsigned int stackoffset=0;
  158. lastnode = idx+(1<<height);
  159. for(;idx<lastnode;idx++)
  160. {
  161. SET_LTREE_ADDRESS(ltree_addr,idx);
  162. SET_OTS_ADDRESS(ots_addr,idx);
  163. gen_leaf_wots(stack+stackoffset*n,sk_seed,params, pub_seed, ltree_addr, ots_addr);
  164. stacklevels[stackoffset] = 0;
  165. stackoffset++;
  166. while(stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2])
  167. {
  168. SET_NODE_TREE_HEIGHT(node_addr,stacklevels[stackoffset-1]);
  169. SET_NODE_TREE_INDEX(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
  170. hash_2n_n(stack+(stackoffset-2)*n,stack+(stackoffset-2)*n, pub_seed,
  171. node_addr, n);
  172. stacklevels[stackoffset-2]++;
  173. stackoffset--;
  174. }
  175. }
  176. for(i=0;i<n;i++)
  177. node[i] = stack[i];
  178. }
  179. /**
  180. * Computes a root node given a leaf and an authapth
  181. */
  182. static void validate_authpath(unsigned char *root, const unsigned char *leaf, unsigned long leafidx, const unsigned char *authpath, const xmss_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  183. {
  184. uint n = params->n;
  185. int i,j;
  186. unsigned char buffer[2*n];
  187. // If leafidx is odd (last bit = 1), current path element is a right child and authpath has to go to the left.
  188. // Otherwise, it is the other way around
  189. if(leafidx&1)
  190. {
  191. for(j=0;j<n;j++)
  192. buffer[n+j] = leaf[j];
  193. for(j=0;j<n;j++)
  194. buffer[j] = authpath[j];
  195. }
  196. else
  197. {
  198. for(j=0;j<n;j++)
  199. buffer[j] = leaf[j];
  200. for(j=0;j<n;j++)
  201. buffer[n+j] = authpath[j];
  202. }
  203. authpath += n;
  204. for(i=0;i<params->h-1;i++)
  205. {
  206. SET_NODE_TREE_HEIGHT(addr,i);
  207. leafidx >>= 1;
  208. SET_NODE_TREE_INDEX(addr, leafidx);
  209. if(leafidx&1)
  210. {
  211. hash_2n_n(buffer+n,buffer,pub_seed, addr, n);
  212. for(j=0;j<n;j++)
  213. buffer[j] = authpath[j];
  214. }
  215. else
  216. {
  217. hash_2n_n(buffer,buffer,pub_seed, addr, n);
  218. for(j=0;j<n;j++)
  219. buffer[j+n] = authpath[j];
  220. }
  221. authpath += n;
  222. }
  223. SET_NODE_TREE_HEIGHT(addr, (params->h-1));
  224. leafidx >>= 1;
  225. SET_NODE_TREE_INDEX(addr, leafidx);
  226. hash_2n_n(root,buffer,pub_seed,addr,n);
  227. }
  228. /**
  229. * 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.
  230. * For more efficient algorithms see e.g. the chapter on hash-based signatures in Bernstein, Buchmann, Dahmen. "Post-quantum Cryptography", Springer 2009.
  231. * It returns the authpath in "authpath" with the node on level 0 at index 0.
  232. */
  233. static void compute_authpath_wots(unsigned char *root, unsigned char *authpath, unsigned long leaf_idx, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, unsigned char addr[16])
  234. {
  235. uint i, j, level;
  236. int n = params->n;
  237. int h = params->h;
  238. unsigned char tree[2*(1<<h)*n];
  239. unsigned char ots_addr[16];
  240. unsigned char ltree_addr[16];
  241. unsigned char node_addr[16];
  242. memcpy(ots_addr, addr, 10);
  243. SET_OTS_BIT(ots_addr, 1);
  244. memcpy(ltree_addr, addr, 10);
  245. SET_OTS_BIT(ltree_addr, 0);
  246. SET_LTREE_BIT(ltree_addr, 1);
  247. memcpy(node_addr, ltree_addr, 10);
  248. SET_LTREE_BIT(node_addr, 0);
  249. SET_NODE_PADDING(node_addr);
  250. // Compute all leaves
  251. for(i = 0; i < (1<<h); i++)
  252. {
  253. SET_LTREE_ADDRESS(ltree_addr,i);
  254. SET_OTS_ADDRESS(ots_addr,i);
  255. gen_leaf_wots(tree+((1<<h)*n + i*n), sk_seed, params, pub_seed, ltree_addr, ots_addr);
  256. }
  257. level = 0;
  258. // Compute tree:
  259. // Outer loop: For each inner layer
  260. for (i = (1<<h); i > 0; i>>=1)
  261. {
  262. SET_NODE_TREE_HEIGHT(node_addr, level);
  263. // Inner loop: for each pair of sibling nodes
  264. for (j = 0; j < i; j+=2)
  265. {
  266. SET_NODE_TREE_INDEX(node_addr, j>>1);
  267. hash_2n_n(tree + (i>>1)*n + (j>>1) * n, tree + i*n + j*n, pub_seed, node_addr, n);
  268. }
  269. level++;
  270. }
  271. // copy authpath
  272. for(i=0;i<h;i++)
  273. memcpy(authpath + i*n, tree + ((1<<h)>>i)*n + ((leaf_idx >> i) ^ 1) * n, n);
  274. // copy root
  275. memcpy(root, tree+n, n);
  276. }
  277. /*
  278. * Generates a XMSS key pair for a given parameter set.
  279. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED]
  280. * Format pk: [root || PUB_SEED] omitting algo oid.
  281. */
  282. int xmss_keypair(unsigned char *pk, unsigned char *sk, xmss_params *params)
  283. {
  284. uint n = params->n;
  285. uint m = params->m;
  286. // Set idx = 0
  287. sk[0] = 0;
  288. sk[1] = 0;
  289. sk[2] = 0;
  290. sk[3] = 0;
  291. // Init SK_SEED (n byte), SK_PRF (m byte), and PUB_SEED (n byte)
  292. randombytes(sk+4,2*n+m);
  293. // Copy PUB_SEED to public key
  294. memcpy(pk+n, sk+4+n+m,n);
  295. unsigned char addr[16] = {0,0,0,0};
  296. // Compute root
  297. treehash(pk, params->h, 0, sk+4, params, sk+4+n+m, addr);
  298. return 0;
  299. }
  300. /**
  301. * Signs a message.
  302. * Returns
  303. * 1. an array containing the signature followed by the message AND
  304. * 2. an updated secret key!
  305. *
  306. */
  307. int xmss_sign(unsigned char *sk, unsigned char *sig_msg, unsigned long long *sig_msg_len, const unsigned char *msg, unsigned long long msglen, const xmss_params *params, unsigned char* pk)
  308. {
  309. uint n = params->n;
  310. uint m = params->m;
  311. // Extract SK
  312. unsigned long idx = (sk[0] << 24) | (sk[1] << 16) | (sk[2] << 8) || sk[3];
  313. unsigned char sk_seed[n];
  314. memcpy(sk_seed,sk+4,n);
  315. unsigned char sk_prf[m];
  316. memcpy(sk_prf,sk+4+n,m);
  317. unsigned char pub_seed[n];
  318. memcpy(pub_seed,sk+4+n+m,n);
  319. // Update SK
  320. sk[0] = ((idx + 1) >> 24) & 255;
  321. sk[1] = ((idx + 1) >> 16) & 255;
  322. sk[2] = ((idx + 1) >> 8) & 255;
  323. sk[3] = (idx + 1) & 255;
  324. // -- Secret key for this non-forward-secure version is now updated.
  325. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  326. // Init working params
  327. unsigned long long i;
  328. unsigned char R[m];
  329. unsigned char msg_h[m];
  330. unsigned char root[n];
  331. unsigned char ots_seed[n];
  332. unsigned char ots_addr[16] = {0,0,0,0};
  333. // ---------------------------------
  334. // Message Hashing
  335. // ---------------------------------
  336. // Message Hash:
  337. // First compute pseudorandom key
  338. prf_m(R, msg, msglen, sk_prf, m);
  339. // Then use it for message digest
  340. hash_m(msg_h, msg, msglen, R, m, m);
  341. // Start collecting signature
  342. *sig_msg_len = 0;
  343. // Copy index to signature
  344. sig_msg[0] = (idx >> 24) & 255;
  345. sig_msg[1] = (idx >> 16) & 255;
  346. sig_msg[2] = (idx >> 8) & 255;
  347. sig_msg[3] = idx & 255;
  348. sig_msg += 4;
  349. *sig_msg_len += 4;
  350. // Copy R to signature
  351. for(i=0; i<m; i++)
  352. sig_msg[i] = R[i];
  353. sig_msg += m;
  354. *sig_msg_len += m;
  355. // ----------------------------------
  356. // Now we start to "really sign"
  357. // ----------------------------------
  358. // Prepare Address
  359. SET_OTS_BIT(ots_addr,1);
  360. SET_OTS_ADDRESS(ots_addr,idx);
  361. // Compute seed for OTS key pair
  362. get_seed(ots_seed, sk_seed, ots_addr);
  363. // Compute WOTS signature
  364. wots_sign(sig_msg, msg_h, ots_seed, params->wots_par, pub_seed, ots_addr);
  365. sig_msg += params->wots_par->keysize;
  366. *sig_msg_len += params->wots_par->keysize;
  367. compute_authpath_wots(root, sig_msg, idx, sk_seed, params, pub_seed, ots_addr);
  368. sig_msg += params->h*n;
  369. *sig_msg_len += params->h*n;
  370. //DEBUG
  371. for(i=0;i<n;i++)
  372. if(root[i] != pk[i])
  373. printf("Different PK's %llu",i);
  374. //Whipe secret elements?
  375. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  376. memcpy(sig_msg,msg,msglen);
  377. *sig_msg_len += msglen;
  378. return 0;
  379. }
  380. /**
  381. * Verifies a given message signature pair under a given public key.
  382. */
  383. int xmss_sign_open(unsigned char *msg, unsigned long long *msglen, const unsigned char *sig_msg, unsigned long long sig_msg_len, const unsigned char *pk, const xmss_params *params)
  384. {
  385. uint n = params->n;
  386. uint m = params->m;
  387. unsigned long long i, m_len;
  388. unsigned long idx=0;
  389. unsigned char wots_pk[params->wots_par->keysize];
  390. unsigned char pkhash[n];
  391. unsigned char root[n];
  392. unsigned char msg_h[m];
  393. unsigned char pub_seed[n];
  394. memcpy(pub_seed,pk+n,n);
  395. // Init addresses
  396. unsigned char ots_addr[16] = {0,0,0,0};
  397. unsigned char ltree_addr[16];
  398. unsigned char node_addr[16];
  399. SET_OTS_BIT(ots_addr, 1);
  400. memcpy(ltree_addr, ots_addr, 10);
  401. SET_OTS_BIT(ltree_addr, 0);
  402. SET_LTREE_BIT(ltree_addr, 1);
  403. memcpy(node_addr, ltree_addr, 10);
  404. SET_LTREE_BIT(node_addr, 0);
  405. SET_NODE_PADDING(node_addr);
  406. // Extract index
  407. idx = (sig_msg[0] << 24) | (sig_msg[1] << 16) | (sig_msg[2] << 8) || sig_msg[3];
  408. sig_msg += 4;
  409. sig_msg_len -= 4;
  410. // hash message (recall, R is now on pole position at sig_msg
  411. unsigned long long tmp_sig_len = m+params->wots_par->keysize+params->h*n;
  412. m_len = sig_msg_len - tmp_sig_len;
  413. hash_m(msg_h, sig_msg + tmp_sig_len, m_len, sig_msg, m, m);
  414. sig_msg += m;
  415. sig_msg_len -= m;
  416. //-----------------------
  417. // Verify signature
  418. //-----------------------
  419. // Prepare Address
  420. SET_OTS_ADDRESS(ots_addr,idx);
  421. // Check WOTS signature
  422. wots_pkFromSig(wots_pk, sig_msg, msg_h, params->wots_par, pub_seed, ots_addr);
  423. sig_msg += params->wots_par->keysize;
  424. sig_msg_len -= params->wots_par->keysize;
  425. // Compute Ltree
  426. SET_LTREE_ADDRESS(ltree_addr, idx);
  427. l_tree(pkhash, wots_pk, params, pub_seed, ltree_addr);
  428. // Compute root
  429. validate_authpath(root, pkhash, idx, sig_msg, params, pub_seed, node_addr);
  430. sig_msg += params->h*n;
  431. sig_msg_len -= params->h*n;
  432. for(i=0;i<n;i++)
  433. if(root[i] != pk[i])
  434. goto fail;
  435. *msglen = sig_msg_len;
  436. for(i=0;i<*msglen;i++)
  437. msg[i] = sig_msg[i];
  438. return 0;
  439. fail:
  440. *msglen = sig_msg_len;
  441. for(i=0;i<*msglen;i++)
  442. msg[i] = 0;
  443. *msglen = -1;
  444. return -1;
  445. }