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.

1171 lines
34 KiB

  1. /*
  2. xmss_fast.c version 20151120
  3. Andreas Hülsing and Joost Rijneveld
  4. Public domain.
  5. */
  6. #include "xmss_fast.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_LAYER_ADDRESS(a, v) {\
  23. a[6] = (a[6] & 3) | ((v << 2) & 255);\
  24. a[5] = (a[5] & 252) | ((v >> 6) & 255);}
  25. #define SET_TREE_ADDRESS(a, v) {\
  26. a[9] = (a[9] & 3) | ((v << 2) & 255);\
  27. a[8] = (v >> 6) & 255;\
  28. a[7] = (v >> 14) & 255;\
  29. a[6] = (a[6] & 252) | ((v >> 22) & 255);}
  30. #define SET_OTS_BIT(a, b) {\
  31. a[9] = (a[9] & 253) | (b << 1);}
  32. #define SET_OTS_ADDRESS(a, v) {\
  33. a[12] = (a[12] & 1) | ((v << 1) & 255);\
  34. a[11] = (v >> 7) & 255;\
  35. a[10] = (v >> 15) & 255;\
  36. a[9] = (a[9] & 254) | ((v >> 23) & 1);}
  37. #define ZEROISE_OTS_ADDR(a) {\
  38. a[12] = (a[12] & 254);\
  39. a[13] = 0;\
  40. a[14] = 0;\
  41. a[15] = 0;}
  42. #define SET_LTREE_BIT(a, b) {\
  43. a[9] = (a[9] & 254) | b;}
  44. #define SET_LTREE_ADDRESS(a, v) {\
  45. a[12] = v & 255;\
  46. a[11] = (v >> 8) & 255;\
  47. a[10] = (v >> 16) & 255;}
  48. #define SET_LTREE_TREE_HEIGHT(a, v) {\
  49. a[13] = (a[13] & 3) | ((v << 2) & 255);}
  50. #define SET_LTREE_TREE_INDEX(a, v) {\
  51. a[15] = (a[15] & 3) | ((v << 2) & 255);\
  52. a[14] = (v >> 6) & 255;\
  53. a[13] = (a[13] & 252) | ((v >> 14) & 3);}
  54. #define SET_NODE_PADDING(a) {\
  55. a[10] = 0;\
  56. a[11] = a[11] & 3;}
  57. #define SET_NODE_TREE_HEIGHT(a, v) {\
  58. a[12] = (a[12] & 3) | ((v << 2) & 255);\
  59. a[11] = (a[11] & 252) | ((v >> 6) & 3);}
  60. #define SET_NODE_TREE_INDEX(a, v) {\
  61. a[15] = (a[15] & 3) | ((v << 2) & 255);\
  62. a[14] = (v >> 6) & 255;\
  63. a[13] = (v >> 14) & 255;\
  64. a[12] = (a[12] & 252) | ((v >> 22) & 3);}
  65. /**
  66. * Used for pseudorandom keygeneration,
  67. * generates the seed for the WOTS keypair at address addr
  68. *
  69. * takes n byte sk_seed and returns n byte seed using 16 byte address addr.
  70. */
  71. static void get_seed(unsigned char *seed, const unsigned char *sk_seed, int n, unsigned char addr[16])
  72. {
  73. // Make sure that chain addr, hash addr, and key bit are 0!
  74. ZEROISE_OTS_ADDR(addr);
  75. // Generate pseudorandom value
  76. prg_with_counter(seed, sk_seed, n, addr);
  77. }
  78. /**
  79. * Initialize xmss params struct
  80. * parameter names are the same as in the draft
  81. * parameter k is K as used in the BDS algorithm
  82. */
  83. int xmss_set_params(xmss_params *params, int m, int n, int h, int w, int k)
  84. {
  85. if(k >= h || k < 2 || (h - k) % 2){
  86. fprintf(stderr, "For BDS traversal, H - K must be even, with H > K >= 2!\n");
  87. return 1;
  88. }
  89. params->h = h;
  90. params->m = m;
  91. params->n = n;
  92. params->k = k;
  93. wots_params wots_par;
  94. wots_set_params(&wots_par, m, n, w);
  95. params->wots_par = wots_par;
  96. return 0;
  97. }
  98. /**
  99. * Initialize BDS state struct
  100. * parameter names are the same as used in the description of the BDS traversal
  101. */
  102. void xmss_set_bds_state(bds_state *state, unsigned char *stack, int stackoffset, unsigned char *stacklevels, unsigned char *auth, unsigned char *keep, treehash_inst *treehash, unsigned char *retain, int next_leaf)
  103. {
  104. state->stack = stack;
  105. state->stackoffset = stackoffset;
  106. state->stacklevels = stacklevels;
  107. state->auth = auth;
  108. state->keep = keep;
  109. state->treehash = treehash;
  110. state->retain = retain;
  111. state->next_leaf = next_leaf;
  112. }
  113. /**
  114. * Initialize xmssmt_params struct
  115. * parameter names are the same as in the draft
  116. *
  117. * Especially h is the total tree height, i.e. the XMSS trees have height h/d
  118. */
  119. int xmssmt_set_params(xmssmt_params *params, int m, int n, int h, int d, int w, int k)
  120. {
  121. if(h % d){
  122. fprintf(stderr, "d must divide h without remainder!\n");
  123. return 1;
  124. }
  125. params->h = h;
  126. params->d = d;
  127. params->m = m;
  128. params->n = n;
  129. params->index_len = (h + 7) / 8;
  130. xmss_params xmss_par;
  131. if (xmss_set_params(&xmss_par, m, n, (h/d), w, k)) {
  132. return 1;
  133. }
  134. params->xmss_par = xmss_par;
  135. return 0;
  136. }
  137. /**
  138. * Computes a leaf from a WOTS public key using an L-tree.
  139. */
  140. static void l_tree(unsigned char *leaf, unsigned char *wots_pk, const xmss_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  141. {
  142. unsigned int l = params->wots_par.len;
  143. unsigned int n = params->n;
  144. unsigned long i = 0;
  145. unsigned int height = 0;
  146. //ADRS.setTreeHeight(0);
  147. SET_LTREE_TREE_HEIGHT(addr,height);
  148. unsigned long bound;
  149. while ( l > 1 )
  150. {
  151. bound = l >> 1; //floor(l / 2);
  152. for ( i = 0; i < bound; i = i + 1 ) {
  153. //ADRS.setTreeIndex(i);
  154. SET_LTREE_TREE_INDEX(addr,i);
  155. //wots_pk[i] = RAND_HASH(pk[2i], pk[2i + 1], SEED, ADRS);
  156. hash_2n_n(wots_pk+i*n,wots_pk+i*2*n, pub_seed, addr, n);
  157. }
  158. //if ( l % 2 == 1 ) {
  159. if(l&1)
  160. {
  161. //pk[floor(l / 2) + 1] = pk[l];
  162. memcpy(wots_pk+(l>>1)*n,wots_pk+(l-1)*n, n);
  163. //l = ceil(l / 2);
  164. l=(l>>1)+1;
  165. }
  166. else
  167. {
  168. //l = ceil(l / 2);
  169. l=(l>>1);
  170. }
  171. //ADRS.setTreeHeight(ADRS.getTreeHeight() + 1);
  172. height++;
  173. SET_LTREE_TREE_HEIGHT(addr,height);
  174. }
  175. //return pk[0];
  176. memcpy(leaf,wots_pk,n);
  177. }
  178. /**
  179. * 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.
  180. */
  181. 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])
  182. {
  183. unsigned char seed[params->n];
  184. unsigned char pk[params->wots_par.keysize];
  185. get_seed(seed, sk_seed, params->n, ots_addr);
  186. wots_pkgen(pk, seed, &(params->wots_par), pub_seed, ots_addr);
  187. l_tree(leaf, pk, params, pub_seed, ltree_addr);
  188. }
  189. static int treehash_minheight_on_stack(bds_state* state, const xmss_params *params, const treehash_inst *treehash) {
  190. int r = params->h, i;
  191. for (i = 0; i < treehash->stackusage; i++) {
  192. if (state->stacklevels[state->stackoffset - i - 1] < r) {
  193. r = state->stacklevels[state->stackoffset - i - 1];
  194. }
  195. }
  196. return r;
  197. }
  198. /**
  199. * Merkle's TreeHash algorithm. The address only needs to initialize the first 78 bits of addr. Everything else will be set by treehash.
  200. * Currently only used for key generation.
  201. *
  202. */
  203. static void treehash_setup(unsigned char *node, int height, int index, bds_state *state, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, const unsigned char addr[16])
  204. {
  205. unsigned int idx = index;
  206. unsigned int n = params->n;
  207. unsigned int h = params->h;
  208. unsigned int k = params->k;
  209. // use three different addresses because at this point we use all three formats in parallel
  210. unsigned char ots_addr[16];
  211. unsigned char ltree_addr[16];
  212. unsigned char node_addr[16];
  213. memcpy(ots_addr, addr, 10);
  214. SET_OTS_BIT(ots_addr, 1);
  215. memcpy(ltree_addr, addr, 10);
  216. SET_OTS_BIT(ltree_addr, 0);
  217. SET_LTREE_BIT(ltree_addr, 1);
  218. memcpy(node_addr, ltree_addr, 10);
  219. SET_LTREE_BIT(node_addr, 0);
  220. SET_NODE_PADDING(node_addr);
  221. int lastnode,i;
  222. unsigned char stack[(height+1)*n];
  223. unsigned int stacklevels[height+1];
  224. unsigned int stackoffset=0;
  225. int nodeh;
  226. lastnode = idx+(1<<height);
  227. for(i = 0; i < h-k; i++) {
  228. state->treehash[i].h = i;
  229. state->treehash[i].completed = 1;
  230. state->treehash[i].stackusage = 0;
  231. }
  232. i = 0;
  233. for(;idx<lastnode;idx++)
  234. {
  235. SET_LTREE_ADDRESS(ltree_addr,idx);
  236. SET_OTS_ADDRESS(ots_addr,idx);
  237. gen_leaf_wots(stack+stackoffset*n,sk_seed,params, pub_seed, ltree_addr, ots_addr);
  238. stacklevels[stackoffset] = 0;
  239. stackoffset++;
  240. if (h - k > 0 && i == 3) {
  241. memcpy(state->treehash[0].node, stack+stackoffset*n, n);
  242. }
  243. while(stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2])
  244. {
  245. nodeh = stacklevels[stackoffset-1];
  246. if (i >> nodeh == 1) {
  247. memcpy(state->auth + nodeh*n, stack+(stackoffset-1)*n, n);
  248. }
  249. else {
  250. if (nodeh < h - k && i >> nodeh == 3) {
  251. memcpy(state->treehash[nodeh].node, stack+(stackoffset-1)*n, n);
  252. }
  253. else if (nodeh >= h - k) {
  254. memcpy(state->retain + ((1 << (h - 1 - nodeh)) + nodeh - h + (((i >> nodeh) - 3) >> 1)) * n, stack+(stackoffset-1)*n, n);
  255. }
  256. }
  257. SET_NODE_TREE_HEIGHT(node_addr,stacklevels[stackoffset-1]);
  258. SET_NODE_TREE_INDEX(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
  259. hash_2n_n(stack+(stackoffset-2)*n,stack+(stackoffset-2)*n, pub_seed,
  260. node_addr, n);
  261. stacklevels[stackoffset-2]++;
  262. stackoffset--;
  263. }
  264. i++;
  265. }
  266. for(i=0;i<n;i++)
  267. node[i] = stack[i];
  268. }
  269. static void treehash_update(treehash_inst *treehash, bds_state *state, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, const unsigned char addr[16]) {
  270. int n = params->n;
  271. unsigned char ots_addr[16];
  272. unsigned char ltree_addr[16];
  273. unsigned char node_addr[16];
  274. memcpy(ots_addr, addr, 10);
  275. SET_OTS_BIT(ots_addr, 1);
  276. memcpy(ltree_addr, addr, 10);
  277. SET_OTS_BIT(ltree_addr, 0);
  278. SET_LTREE_BIT(ltree_addr, 1);
  279. memcpy(node_addr, ltree_addr, 10);
  280. SET_LTREE_BIT(node_addr, 0);
  281. SET_NODE_PADDING(node_addr);
  282. SET_LTREE_ADDRESS(ltree_addr, treehash->next_idx);
  283. SET_OTS_ADDRESS(ots_addr, treehash->next_idx);
  284. unsigned char nodebuffer[2 * n];
  285. unsigned int nodeheight = 0;
  286. gen_leaf_wots(nodebuffer, sk_seed, params, pub_seed, ltree_addr, ots_addr);
  287. while (treehash->stackusage > 0 && state->stacklevels[state->stackoffset-1] == nodeheight) {
  288. memcpy(nodebuffer + n, nodebuffer, n);
  289. memcpy(nodebuffer, state->stack + (state->stackoffset-1)*n, n);
  290. SET_NODE_TREE_HEIGHT(node_addr, nodeheight);
  291. SET_NODE_TREE_INDEX(node_addr, (treehash->next_idx >> (nodeheight+1)));
  292. hash_2n_n(nodebuffer, nodebuffer, pub_seed, node_addr, n);
  293. nodeheight++;
  294. treehash->stackusage--;
  295. state->stackoffset--;
  296. }
  297. if (nodeheight == treehash->h) { // this also implies stackusage == 0
  298. memcpy(treehash->node, nodebuffer, n);
  299. treehash->completed = 1;
  300. }
  301. else {
  302. memcpy(state->stack + state->stackoffset*n, nodebuffer, n);
  303. treehash->stackusage++;
  304. state->stacklevels[state->stackoffset] = nodeheight;
  305. state->stackoffset++;
  306. treehash->next_idx++;
  307. }
  308. }
  309. /**
  310. * Computes a root node given a leaf and an authapth
  311. */
  312. 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])
  313. {
  314. unsigned int n = params->n;
  315. int i,j;
  316. unsigned char buffer[2*n];
  317. // If leafidx is odd (last bit = 1), current path element is a right child and authpath has to go to the left.
  318. // Otherwise, it is the other way around
  319. if(leafidx&1)
  320. {
  321. for(j=0;j<n;j++)
  322. buffer[n+j] = leaf[j];
  323. for(j=0;j<n;j++)
  324. buffer[j] = authpath[j];
  325. }
  326. else
  327. {
  328. for(j=0;j<n;j++)
  329. buffer[j] = leaf[j];
  330. for(j=0;j<n;j++)
  331. buffer[n+j] = authpath[j];
  332. }
  333. authpath += n;
  334. for(i=0;i<params->h-1;i++)
  335. {
  336. SET_NODE_TREE_HEIGHT(addr,i);
  337. leafidx >>= 1;
  338. SET_NODE_TREE_INDEX(addr, leafidx);
  339. if(leafidx&1)
  340. {
  341. hash_2n_n(buffer+n,buffer,pub_seed, addr, n);
  342. for(j=0;j<n;j++)
  343. buffer[j] = authpath[j];
  344. }
  345. else
  346. {
  347. hash_2n_n(buffer,buffer,pub_seed, addr, n);
  348. for(j=0;j<n;j++)
  349. buffer[j+n] = authpath[j];
  350. }
  351. authpath += n;
  352. }
  353. SET_NODE_TREE_HEIGHT(addr, (params->h-1));
  354. leafidx >>= 1;
  355. SET_NODE_TREE_INDEX(addr, leafidx);
  356. hash_2n_n(root,buffer,pub_seed,addr,n);
  357. }
  358. /**
  359. * Performs one treehash update on the instance that needs it the most.
  360. * Returns 1 if such an instance was not found
  361. **/
  362. static char bds_treehash_update(bds_state *state, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, const unsigned char addr[16]) {
  363. unsigned int i;
  364. int level, l_min, low;
  365. int h = params->h;
  366. int k = params->k;
  367. l_min = h;
  368. level = h - k;
  369. for (i = 0; i < h - k; i++) {
  370. if (state->treehash[i].completed) {
  371. low = h;
  372. }
  373. else if (state->treehash[i].stackusage == 0) {
  374. low = i;
  375. }
  376. else {
  377. low = treehash_minheight_on_stack(state, params, &(state->treehash[i]));
  378. }
  379. if (low < l_min) {
  380. level = i;
  381. l_min = low;
  382. }
  383. }
  384. if (level == h - k) {
  385. return 1;
  386. }
  387. treehash_update(&(state->treehash[level]), state, sk_seed, params, pub_seed, addr);
  388. return 0;
  389. }
  390. /**
  391. * Updates the state (typically NEXT_i) by adding a leaf and updating the stack
  392. * Returns 1 if all leaf nodes have already been processed
  393. **/
  394. static char bds_state_update(bds_state *state, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, const unsigned char addr[16]) {
  395. unsigned char ltree_addr[16];
  396. unsigned char node_addr[16];
  397. unsigned char ots_addr[16];
  398. int n = params->n;
  399. int h = params->h;
  400. int k = params->k;
  401. int nodeh;
  402. int idx = state->next_leaf;
  403. if (idx == 1 << h) {
  404. return 1;
  405. }
  406. memcpy(ots_addr, addr, 10);
  407. SET_OTS_BIT(ots_addr, 1);
  408. SET_OTS_ADDRESS(ots_addr, idx);
  409. memcpy(ltree_addr, addr, 10);
  410. SET_OTS_BIT(ltree_addr, 0);
  411. SET_LTREE_BIT(ltree_addr, 1);
  412. SET_LTREE_ADDRESS(ltree_addr, idx);
  413. memcpy(node_addr, addr, 10);
  414. SET_LTREE_BIT(node_addr, 0);
  415. SET_OTS_BIT(node_addr, 0);
  416. SET_NODE_PADDING(node_addr);
  417. gen_leaf_wots(state->stack+state->stackoffset*n,sk_seed,params, pub_seed, ltree_addr, ots_addr);
  418. state->stacklevels[state->stackoffset] = 0;
  419. state->stackoffset++;
  420. if (h - k > 0 && idx == 3) {
  421. memcpy(state->treehash[0].node, state->stack+state->stackoffset*n, n);
  422. }
  423. while(state->stackoffset>1 && state->stacklevels[state->stackoffset-1] == state->stacklevels[state->stackoffset-2])
  424. {
  425. nodeh = state->stacklevels[state->stackoffset-1];
  426. if (idx >> nodeh == 1) {
  427. memcpy(state->auth + nodeh*n, state->stack+(state->stackoffset-1)*n, n);
  428. }
  429. else {
  430. if (nodeh < h - k && idx >> nodeh == 3) {
  431. memcpy(state->treehash[nodeh].node, state->stack+(state->stackoffset-1)*n, n);
  432. }
  433. else if (nodeh >= h - k) {
  434. memcpy(state->retain + ((1 << (h - 1 - nodeh)) + nodeh - h + (((idx >> nodeh) - 3) >> 1)) * n, state->stack+(state->stackoffset-1)*n, n);
  435. }
  436. }
  437. SET_NODE_TREE_HEIGHT(node_addr, state->stacklevels[state->stackoffset-1]);
  438. SET_NODE_TREE_INDEX(node_addr, (idx >> (state->stacklevels[state->stackoffset-1]+1)));
  439. hash_2n_n(state->stack+(state->stackoffset-2)*n,state->stack+(state->stackoffset-2)*n, pub_seed, node_addr, n);
  440. state->stacklevels[state->stackoffset-2]++;
  441. state->stackoffset--;
  442. }
  443. state->next_leaf++;
  444. return 0;
  445. }
  446. /**
  447. * Returns the auth path for node leaf_idx and computes the auth path for the
  448. * next leaf node, using the algorithm described by Buchmann, Dahmen and Szydlo
  449. * in "Post Quantum Cryptography", Springer 2009.
  450. */
  451. static int bds_round(bds_state *state, const unsigned long leaf_idx, const unsigned char *sk_seed, const xmss_params *params, const int updates, unsigned char *pub_seed, unsigned char addr[16])
  452. {
  453. unsigned int i;
  454. int n = params->n;
  455. int h = params->h;
  456. int k = params->k;
  457. int tau = h;
  458. int startidx;
  459. int offset, rowidx;
  460. unsigned char buf[2 * n];
  461. unsigned char ots_addr[16];
  462. unsigned char ltree_addr[16];
  463. unsigned char node_addr[16];
  464. memcpy(ots_addr, addr, 10);
  465. SET_OTS_BIT(ots_addr, 1);
  466. memcpy(ltree_addr, addr, 10);
  467. SET_OTS_BIT(ltree_addr, 0);
  468. SET_LTREE_BIT(ltree_addr, 1);
  469. memcpy(node_addr, ltree_addr, 10);
  470. SET_LTREE_BIT(node_addr, 0);
  471. SET_NODE_PADDING(node_addr);
  472. for (i = 0; i < h; i++) {
  473. if (! ((leaf_idx >> i) & 1)) {
  474. tau = i;
  475. break;
  476. }
  477. }
  478. if (tau > 0) {
  479. memcpy(buf, state->auth + (tau-1) * n, n);
  480. // we need to do this before refreshing state->keep to prevent overwriting
  481. memcpy(buf + n, state->keep + ((tau-1) >> 1) * n, n);
  482. }
  483. if (!((leaf_idx >> (tau + 1)) & 1) && (tau < h - 1)) {
  484. memcpy(state->keep + (tau >> 1)*n, state->auth + tau*n, n);
  485. }
  486. if (tau == 0) {
  487. SET_LTREE_ADDRESS(ltree_addr,leaf_idx);
  488. SET_OTS_ADDRESS(ots_addr,leaf_idx);
  489. gen_leaf_wots(state->auth, sk_seed, params, pub_seed, ltree_addr, ots_addr);
  490. }
  491. else {
  492. SET_NODE_TREE_HEIGHT(node_addr, (tau-1));
  493. SET_NODE_TREE_INDEX(node_addr, leaf_idx >> tau);
  494. hash_2n_n(state->auth + tau * n, buf, pub_seed, node_addr, n);
  495. for (i = 0; i < tau; i++) {
  496. if (i < h - k) {
  497. memcpy(state->auth + i * n, state->treehash[i].node, n);
  498. }
  499. else {
  500. offset = (1 << (h - 1 - i)) + i - h;
  501. rowidx = ((leaf_idx >> i) - 1) >> 1;
  502. memcpy(state->auth + i * n, state->retain + (offset + rowidx) * n, n);
  503. }
  504. }
  505. for (i = 0; i < ((tau < h - k) ? tau : (h - k)); i++) {
  506. startidx = leaf_idx + 1 + 3 * (1 << i);
  507. if (startidx < 1 << h) {
  508. state->treehash[i].h = i;
  509. state->treehash[i].next_idx = startidx;
  510. state->treehash[i].completed = 0;
  511. state->treehash[i].stackusage = 0;
  512. }
  513. }
  514. }
  515. int remaining = 0;
  516. for (i = 0; i < updates; i++) {
  517. if (bds_treehash_update(state, sk_seed, params, pub_seed, addr)) {
  518. remaining++;
  519. }
  520. }
  521. return remaining;
  522. }
  523. /*
  524. * Generates a XMSS key pair for a given parameter set.
  525. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED]
  526. * Format pk: [root || PUB_SEED] omitting algo oid.
  527. */
  528. int xmss_keypair(unsigned char *pk, unsigned char *sk, bds_state *state, xmss_params *params)
  529. {
  530. unsigned int n = params->n;
  531. unsigned int m = params->m;
  532. // Set idx = 0
  533. sk[0] = 0;
  534. sk[1] = 0;
  535. sk[2] = 0;
  536. sk[3] = 0;
  537. // Init SK_SEED (n byte), SK_PRF (m byte), and PUB_SEED (n byte)
  538. randombytes(sk+4,2*n+m);
  539. // Copy PUB_SEED to public key
  540. memcpy(pk+n, sk+4+n+m,n);
  541. unsigned char addr[16] = {0,0,0,0};
  542. // Compute root
  543. treehash_setup(pk, params->h, 0, state, sk+4, params, sk+4+n+m, addr);
  544. return 0;
  545. }
  546. /**
  547. * Signs a message.
  548. * Returns
  549. * 1. an array containing the signature followed by the message AND
  550. * 2. an updated secret key!
  551. *
  552. */
  553. int xmss_sign(unsigned char *sk, bds_state *state, unsigned char *sig_msg, unsigned long long *sig_msg_len, const unsigned char *msg, unsigned long long msglen, const xmss_params *params)
  554. {
  555. unsigned int h = params->h;
  556. unsigned int n = params->n;
  557. unsigned int m = params->m;
  558. unsigned int k = params->k;
  559. // Extract SK
  560. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  561. unsigned char sk_seed[n];
  562. memcpy(sk_seed,sk+4,n);
  563. unsigned char sk_prf[m];
  564. memcpy(sk_prf,sk+4+n,m);
  565. unsigned char pub_seed[n];
  566. memcpy(pub_seed,sk+4+n+m,n);
  567. // Update SK
  568. sk[0] = ((idx + 1) >> 24) & 255;
  569. sk[1] = ((idx + 1) >> 16) & 255;
  570. sk[2] = ((idx + 1) >> 8) & 255;
  571. sk[3] = (idx + 1) & 255;
  572. // -- Secret key for this non-forward-secure version is now updated.
  573. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  574. // Init working params
  575. unsigned long long i;
  576. unsigned char R[m];
  577. unsigned char msg_h[m];
  578. unsigned char ots_seed[n];
  579. unsigned char ots_addr[16] = {0,0,0,0};
  580. // ---------------------------------
  581. // Message Hashing
  582. // ---------------------------------
  583. // Message Hash:
  584. // First compute pseudorandom key
  585. prf_m(R, msg, msglen, sk_prf, m);
  586. // Then use it for message digest
  587. hash_m(msg_h, msg, msglen, R, m, m);
  588. // Start collecting signature
  589. *sig_msg_len = 0;
  590. // Copy index to signature
  591. sig_msg[0] = (idx >> 24) & 255;
  592. sig_msg[1] = (idx >> 16) & 255;
  593. sig_msg[2] = (idx >> 8) & 255;
  594. sig_msg[3] = idx & 255;
  595. sig_msg += 4;
  596. *sig_msg_len += 4;
  597. // Copy R to signature
  598. for(i=0; i<m; i++)
  599. sig_msg[i] = R[i];
  600. sig_msg += m;
  601. *sig_msg_len += m;
  602. // ----------------------------------
  603. // Now we start to "really sign"
  604. // ----------------------------------
  605. // Prepare Address
  606. SET_OTS_BIT(ots_addr,1);
  607. SET_OTS_ADDRESS(ots_addr,idx);
  608. // Compute seed for OTS key pair
  609. get_seed(ots_seed, sk_seed, n, ots_addr);
  610. // Compute WOTS signature
  611. wots_sign(sig_msg, msg_h, ots_seed, &(params->wots_par), pub_seed, ots_addr);
  612. sig_msg += params->wots_par.keysize;
  613. *sig_msg_len += params->wots_par.keysize;
  614. // the auth path was already computed during the previous round
  615. memcpy(sig_msg, state->auth, h*n);
  616. if (idx < (1 << h) - 1) {
  617. bds_round(state, idx, sk_seed, params, (h - k) >> 1, pub_seed, ots_addr);
  618. }
  619. sig_msg += params->h*n;
  620. *sig_msg_len += params->h*n;
  621. //Whipe secret elements?
  622. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  623. memcpy(sig_msg,msg,msglen);
  624. *sig_msg_len += msglen;
  625. return 0;
  626. }
  627. /**
  628. * Verifies a given message signature pair under a given public key.
  629. */
  630. 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)
  631. {
  632. unsigned int n = params->n;
  633. unsigned int m = params->m;
  634. unsigned long long i, m_len;
  635. unsigned long idx=0;
  636. unsigned char wots_pk[params->wots_par.keysize];
  637. unsigned char pkhash[n];
  638. unsigned char root[n];
  639. unsigned char msg_h[m];
  640. unsigned char pub_seed[n];
  641. memcpy(pub_seed,pk+n,n);
  642. // Init addresses
  643. unsigned char ots_addr[16] = {0,0,0,0};
  644. unsigned char ltree_addr[16];
  645. unsigned char node_addr[16];
  646. SET_OTS_BIT(ots_addr, 1);
  647. memcpy(ltree_addr, ots_addr, 10);
  648. SET_OTS_BIT(ltree_addr, 0);
  649. SET_LTREE_BIT(ltree_addr, 1);
  650. memcpy(node_addr, ltree_addr, 10);
  651. SET_LTREE_BIT(node_addr, 0);
  652. SET_NODE_PADDING(node_addr);
  653. // Extract index
  654. idx = ((unsigned long)sig_msg[0] << 24) | ((unsigned long)sig_msg[1] << 16) | ((unsigned long)sig_msg[2] << 8) | sig_msg[3];
  655. printf("verify:: idx = %lu\n",idx);
  656. sig_msg += 4;
  657. sig_msg_len -= 4;
  658. // hash message (recall, R is now on pole position at sig_msg
  659. unsigned long long tmp_sig_len = m+params->wots_par.keysize+params->h*n;
  660. m_len = sig_msg_len - tmp_sig_len;
  661. hash_m(msg_h, sig_msg + tmp_sig_len, m_len, sig_msg, m, m);
  662. sig_msg += m;
  663. sig_msg_len -= m;
  664. //-----------------------
  665. // Verify signature
  666. //-----------------------
  667. // Prepare Address
  668. SET_OTS_ADDRESS(ots_addr,idx);
  669. // Check WOTS signature
  670. wots_pkFromSig(wots_pk, sig_msg, msg_h, &(params->wots_par), pub_seed, ots_addr);
  671. sig_msg += params->wots_par.keysize;
  672. sig_msg_len -= params->wots_par.keysize;
  673. // Compute Ltree
  674. SET_LTREE_ADDRESS(ltree_addr, idx);
  675. l_tree(pkhash, wots_pk, params, pub_seed, ltree_addr);
  676. // Compute root
  677. validate_authpath(root, pkhash, idx, sig_msg, params, pub_seed, node_addr);
  678. sig_msg += params->h*n;
  679. sig_msg_len -= params->h*n;
  680. for(i=0;i<n;i++)
  681. if(root[i] != pk[i])
  682. goto fail;
  683. *msglen = sig_msg_len;
  684. for(i=0;i<*msglen;i++)
  685. msg[i] = sig_msg[i];
  686. return 0;
  687. fail:
  688. *msglen = sig_msg_len;
  689. for(i=0;i<*msglen;i++)
  690. msg[i] = 0;
  691. *msglen = -1;
  692. return -1;
  693. }
  694. /*
  695. * Generates a XMSSMT key pair for a given parameter set.
  696. * Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || PUB_SEED]
  697. * Format pk: [root || PUB_SEED] omitting algo oid.
  698. */
  699. int xmssmt_keypair(unsigned char *pk, unsigned char *sk, bds_state *states, unsigned char *wots_sigs, xmssmt_params *params)
  700. {
  701. unsigned int n = params->n;
  702. unsigned int m = params->m;
  703. unsigned int i;
  704. unsigned char ots_seed[params->n];
  705. // Set idx = 0
  706. for (i = 0; i < params->index_len; i++){
  707. sk[i] = 0;
  708. }
  709. // Init SK_SEED (n byte), SK_PRF (m byte), and PUB_SEED (n byte)
  710. randombytes(sk+params->index_len,2*n+m);
  711. // Copy PUB_SEED to public key
  712. memcpy(pk+n, sk+params->index_len+n+m,n);
  713. // Set address to point on the single tree on layer d-1
  714. unsigned char addr[16] = {0,0,0,0};
  715. SET_OTS_BIT(addr,1);
  716. SET_TREE_ADDRESS(addr, 0);
  717. SET_OTS_ADDRESS(addr, 0);
  718. SET_LAYER_ADDRESS(addr, 0);
  719. // Set up state and compute wots signatures for all but topmost tree root
  720. for (i = 0; i < params->d - 1; i++) {
  721. SET_LAYER_ADDRESS(addr, i);
  722. SET_TREE_ADDRESS(addr, 0);
  723. SET_OTS_ADDRESS(addr, 0);
  724. // Compute seed for OTS key pair
  725. treehash_setup(pk, params->xmss_par.h, 0, states + i, sk+params->index_len, &(params->xmss_par), pk+n, addr);
  726. SET_LAYER_ADDRESS(addr, (i+1));
  727. get_seed(ots_seed, sk+params->index_len, n, addr);
  728. wots_sign(wots_sigs + i*params->xmss_par.wots_par.keysize, pk, ots_seed, &(params->xmss_par.wots_par), pk+n, addr);
  729. }
  730. treehash_setup(pk, params->xmss_par.h, 0, states + i, sk+params->index_len, &(params->xmss_par), pk+n, addr);
  731. return 0;
  732. }
  733. /**
  734. * Signs a message.
  735. * Returns
  736. * 1. an array containing the signature followed by the message AND
  737. * 2. an updated secret key!
  738. *
  739. */
  740. int xmssmt_sign(unsigned char *sk, bds_state *states, unsigned char *wots_sigs, unsigned char *sig_msg, unsigned long long *sig_msg_len, const unsigned char *msg, unsigned long long msglen, const xmssmt_params *params)
  741. {
  742. unsigned int n = params->n;
  743. unsigned int m = params->m;
  744. unsigned int tree_h = params->xmss_par.h;
  745. unsigned int h = params->h;
  746. unsigned int k = params->xmss_par.k;
  747. unsigned int idx_len = params->index_len;
  748. unsigned long long idx_tree;
  749. unsigned long long idx_leaf;
  750. unsigned long long i, j;
  751. unsigned int updates;
  752. unsigned int first_nonwots;
  753. unsigned char sk_seed[n];
  754. unsigned char sk_prf[m];
  755. unsigned char pub_seed[n];
  756. // Init working params
  757. unsigned char R[m];
  758. unsigned char msg_h[m];
  759. unsigned char ots_seed[n];
  760. unsigned char ots_addr[16] = {0,0,0,0};
  761. bds_state tmp;
  762. // Extract SK
  763. unsigned long long idx = 0;
  764. for(i = 0; i < idx_len; i++){
  765. idx |= ((unsigned long long)sk[i]) << 8*(idx_len - 1 - i);
  766. }
  767. memcpy(sk_seed,sk+idx_len,n);
  768. memcpy(sk_prf,sk+idx_len+n,m);
  769. memcpy(pub_seed,sk+idx_len+n+m,n);
  770. // Update SK
  771. for(i = 0; i < idx_len; i++){
  772. sk[i] = ((idx + 1) >> 8*(idx_len - 1 - i)) & 255;
  773. }
  774. // -- Secret key for this non-forward-secure version is now updated.
  775. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  776. // ---------------------------------
  777. // Message Hashing
  778. // ---------------------------------
  779. // Message Hash:
  780. // First compute pseudorandom key
  781. prf_m(R, msg, msglen, sk_prf, m);
  782. // Then use it for message digest
  783. hash_m(msg_h, msg, msglen, R, m, m);
  784. // Start collecting signature
  785. *sig_msg_len = 0;
  786. // Copy index to signature
  787. for(i = 0; i < idx_len; i++){
  788. sig_msg[i] = (idx >> 8*(idx_len - 1 - i)) & 255;
  789. }
  790. sig_msg += idx_len;
  791. *sig_msg_len += idx_len;
  792. // Copy R to signature
  793. for(i=0; i<m; i++)
  794. sig_msg[i] = R[i];
  795. sig_msg += m;
  796. *sig_msg_len += m;
  797. // ----------------------------------
  798. // Now we start to "really sign"
  799. // ----------------------------------
  800. // Handle lowest layer separately as it is slightly different...
  801. // Prepare Address
  802. SET_OTS_BIT(ots_addr,1);
  803. idx_tree = idx >> tree_h;
  804. idx_leaf = (idx & ((1 << tree_h)-1));
  805. SET_LAYER_ADDRESS(ots_addr,0);
  806. SET_TREE_ADDRESS(ots_addr, idx_tree);
  807. SET_OTS_ADDRESS(ots_addr, idx_leaf);
  808. // Compute seed for OTS key pair
  809. get_seed(ots_seed, sk_seed, n, ots_addr);
  810. // Compute WOTS signature
  811. wots_sign(sig_msg, msg_h, ots_seed, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  812. sig_msg += params->xmss_par.wots_par.keysize;
  813. *sig_msg_len += params->xmss_par.wots_par.keysize;
  814. memcpy(sig_msg, states[0].auth, tree_h*n);
  815. sig_msg += tree_h*n;
  816. *sig_msg_len += tree_h*n;
  817. // prepare signature of remaining layers
  818. for(i = 1; i < params->d; i++){
  819. // put WOTS signature in place
  820. memcpy(sig_msg, wots_sigs + (i-1)*params->xmss_par.wots_par.keysize, params->xmss_par.wots_par.keysize);
  821. sig_msg += params->xmss_par.wots_par.keysize;
  822. *sig_msg_len += params->xmss_par.wots_par.keysize;
  823. // put AUTH nodes in place
  824. memcpy(sig_msg, states[i].auth, tree_h*n);
  825. sig_msg += tree_h*n;
  826. *sig_msg_len += tree_h*n;
  827. }
  828. SET_LAYER_ADDRESS(ots_addr, 0);
  829. SET_TREE_ADDRESS(ots_addr, (idx_tree + 1));
  830. updates = tree_h - k;
  831. // if a NEXT-tree exists within the hypertree
  832. if ((1 + idx_tree) * (1 << tree_h) + idx_leaf < (1 << h)) {
  833. // mandatory update for NEXT_0 (does not count towards h-k)
  834. bds_state_update(&states[params->d], sk_seed, &(params->xmss_par), pub_seed, ots_addr);
  835. // check if we're at the end of a tree
  836. for (i = 0; i < params->d; i++) {
  837. if (((idx + 1) & ((1 << ((i+1)*tree_h)) - 1)) == 0) {
  838. memcpy(&tmp, states+params->d + i, sizeof(bds_state));
  839. memcpy(states+params->d + i, states + i, sizeof(bds_state));
  840. memcpy(states + i, &tmp, sizeof(bds_state));
  841. SET_TREE_ADDRESS(ots_addr, ((idx + 1) >> ((i+2) * tree_h)));
  842. SET_OTS_ADDRESS(ots_addr, (((idx >> ((i+1) * tree_h)) + 1) & ((1 << tree_h)-1)));
  843. SET_LAYER_ADDRESS(ots_addr, (i+1));
  844. get_seed(ots_seed, sk+params->index_len, n, ots_addr);
  845. wots_sign(wots_sigs + i*params->xmss_par.wots_par.keysize, states[i].stack, ots_seed, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  846. states[params->d + i].stackoffset = 0;
  847. states[params->d + i].next_leaf = 0;
  848. updates--; // WOTS-signing counts as one update
  849. // this bds_round is needed to initialise the state, but should not perform updates
  850. // note that one should still pass the (reduced) current idx, as bds_round sets up for idx+1
  851. bds_round(&states[i+1], ((idx >> ((i+1)*tree_h))) & ((1 << tree_h)-1), sk_seed, &(params->xmss_par), 0, pub_seed, ots_addr);
  852. for (j = 0; j < tree_h-k; j++) {
  853. states[i].treehash[j].completed = 1;
  854. }
  855. }
  856. }
  857. }
  858. SET_LAYER_ADDRESS(ots_addr, 0);
  859. SET_TREE_ADDRESS(ots_addr, idx_tree);
  860. first_nonwots = (tree_h - k) - updates;
  861. if (first_nonwots == 0) {
  862. updates = bds_round(&states[0], idx_leaf, sk_seed, &(params->xmss_par), (tree_h - k) >> 1, pub_seed, ots_addr);
  863. }
  864. for (i = 1; updates > 0 && i < params->d; i++) {
  865. idx_leaf = (idx_tree & ((1 << tree_h)-1));
  866. idx_tree = idx_tree >> tree_h;
  867. if (first_nonwots > i) {
  868. continue;
  869. }
  870. SET_LAYER_ADDRESS(ots_addr, i);
  871. SET_TREE_ADDRESS(ots_addr, idx_tree);
  872. SET_OTS_ADDRESS(ots_addr, idx_leaf);
  873. while (updates > 0 && !bds_treehash_update(&states[i], sk_seed, &(params->xmss_par), pub_seed, ots_addr)) {
  874. updates--;
  875. }
  876. SET_TREE_ADDRESS(ots_addr, (idx_tree + 1));
  877. // if a NEXT-tree exists for this level;
  878. if ((1 + idx_tree) * (1 << tree_h) + idx_leaf < (1 << (h - tree_h * i))) {
  879. while (updates > 0 && !bds_state_update(&states[params->d + i], sk_seed, &(params->xmss_par), pub_seed, ots_addr)) {
  880. updates--;
  881. }
  882. }
  883. }
  884. //Whipe secret elements?
  885. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  886. memcpy(sig_msg,msg,msglen);
  887. *sig_msg_len += msglen;
  888. return 0;
  889. }
  890. /**
  891. * Verifies a given message signature pair under a given public key.
  892. */
  893. int xmssmt_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 xmssmt_params *params)
  894. {
  895. unsigned int n = params->n;
  896. unsigned int m = params->m;
  897. unsigned int tree_h = params->xmss_par.h;
  898. unsigned int idx_len = params->index_len;
  899. unsigned long long idx_tree;
  900. unsigned long long idx_leaf;
  901. unsigned long long i, m_len;
  902. unsigned long long idx=0;
  903. unsigned char wots_pk[params->xmss_par.wots_par.keysize];
  904. unsigned char pkhash[n];
  905. unsigned char root[n];
  906. unsigned char msg_h[m];
  907. unsigned char pub_seed[n];
  908. memcpy(pub_seed,pk+n,n);
  909. // Init addresses
  910. unsigned char ots_addr[16] = {0,0,0,0};
  911. unsigned char ltree_addr[16];
  912. unsigned char node_addr[16];
  913. // Extract index
  914. for(i = 0; i < idx_len; i++){
  915. idx |= ((unsigned long long)sig_msg[i]) << (8*(idx_len - 1 - i));
  916. }
  917. printf("verify:: idx = %llu\n",idx);
  918. sig_msg += idx_len;
  919. sig_msg_len -= idx_len;
  920. // hash message (recall, R is now on pole position at sig_msg
  921. unsigned long long tmp_sig_len = m+ (params->d * params->xmss_par.wots_par.keysize) + (params->h * n);
  922. m_len = sig_msg_len - tmp_sig_len;
  923. hash_m(msg_h, sig_msg + tmp_sig_len, m_len, sig_msg, m, m);
  924. sig_msg += m;
  925. sig_msg_len -= m;
  926. //-----------------------
  927. // Verify signature
  928. //-----------------------
  929. // Prepare Address
  930. idx_tree = idx >> tree_h;
  931. idx_leaf = (idx & ((1 << tree_h)-1));
  932. SET_LAYER_ADDRESS(ots_addr,0);
  933. SET_TREE_ADDRESS(ots_addr, idx_tree);
  934. SET_OTS_BIT(ots_addr, 1);
  935. memcpy(ltree_addr, ots_addr, 10);
  936. SET_OTS_BIT(ltree_addr, 0);
  937. SET_LTREE_BIT(ltree_addr, 1);
  938. memcpy(node_addr, ltree_addr, 10);
  939. SET_LTREE_BIT(node_addr, 0);
  940. SET_NODE_PADDING(node_addr);
  941. SET_OTS_ADDRESS(ots_addr,idx_leaf);
  942. // Check WOTS signature
  943. wots_pkFromSig(wots_pk, sig_msg, msg_h, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  944. sig_msg += params->xmss_par.wots_par.keysize;
  945. sig_msg_len -= params->xmss_par.wots_par.keysize;
  946. // Compute Ltree
  947. SET_LTREE_ADDRESS(ltree_addr, idx_leaf);
  948. l_tree(pkhash, wots_pk, &(params->xmss_par), pub_seed, ltree_addr);
  949. // Compute root
  950. validate_authpath(root, pkhash, idx_leaf, sig_msg, &(params->xmss_par), pub_seed, node_addr);
  951. sig_msg += tree_h*n;
  952. sig_msg_len -= tree_h*n;
  953. for(i = 1; i < params->d; i++){
  954. // Prepare Address
  955. idx_leaf = (idx_tree & ((1 << tree_h)-1));
  956. idx_tree = idx_tree >> tree_h;
  957. SET_LAYER_ADDRESS(ots_addr,i);
  958. SET_TREE_ADDRESS(ots_addr, idx_tree);
  959. SET_OTS_BIT(ots_addr, 1);
  960. memcpy(ltree_addr, ots_addr, 10);
  961. SET_OTS_BIT(ltree_addr, 0);
  962. SET_LTREE_BIT(ltree_addr, 1);
  963. memcpy(node_addr, ltree_addr, 10);
  964. SET_LTREE_BIT(node_addr, 0);
  965. SET_NODE_PADDING(node_addr);
  966. SET_OTS_ADDRESS(ots_addr,idx_leaf);
  967. // Check WOTS signature
  968. wots_pkFromSig(wots_pk, sig_msg, root, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  969. sig_msg += params->xmss_par.wots_par.keysize;
  970. sig_msg_len -= params->xmss_par.wots_par.keysize;
  971. // Compute Ltree
  972. SET_LTREE_ADDRESS(ltree_addr, idx_leaf);
  973. l_tree(pkhash, wots_pk, &(params->xmss_par), pub_seed, ltree_addr);
  974. // Compute root
  975. validate_authpath(root, pkhash, idx_leaf, sig_msg, &(params->xmss_par), pub_seed, node_addr);
  976. sig_msg += tree_h*n;
  977. sig_msg_len -= tree_h*n;
  978. }
  979. for(i=0;i<n;i++)
  980. if(root[i] != pk[i])
  981. goto fail;
  982. *msglen = sig_msg_len;
  983. for(i=0;i<*msglen;i++)
  984. msg[i] = sig_msg[i];
  985. return 0;
  986. fail:
  987. *msglen = sig_msg_len;
  988. for(i=0;i<*msglen;i++)
  989. msg[i] = 0;
  990. *msglen = -1;
  991. return -1;
  992. }