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.

1153 lines
34 KiB

  1. /*
  2. xmss.c version 20150811
  3. Andreas Hülsing
  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) & 252);\
  24. a[5] = (a[5] & 252) | ((v >> 6) & 3);}
  25. #define SET_TREE_ADDRESS(a, v) {\
  26. a[9] = (a[9] & 3) | ((v << 2) & 252);\
  27. a[8] = (v >> 6) & 255;\
  28. a[7] = (v >> 14) & 255;\
  29. a[6] = (a[6] & 252) | ((v >> 22) & 3);}
  30. #define SET_OTS_BIT(a, b) {\
  31. a[9] = (a[9] & 253) | ((b << 1) & 2);}
  32. #define SET_OTS_ADDRESS(a, v) {\
  33. a[12] = (a[12] & 1) | ((v << 1) & 254);\
  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 & 1);}
  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) & 252);}
  50. #define SET_LTREE_TREE_INDEX(a, v) {\
  51. a[15] = (a[15] & 3) | ((v << 2) & 252);\
  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) & 252);\
  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) & 252);\
  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, unsigned int updates, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, const unsigned char addr[16]) {
  363. unsigned int i, j;
  364. int level, l_min, low;
  365. int h = params->h;
  366. int k = params->k;
  367. int used = 0;
  368. for (j = 0; j < updates; j++) {
  369. l_min = h;
  370. level = h - k;
  371. for (i = 0; i < h - k; i++) {
  372. if (state->treehash[i].completed) {
  373. low = h;
  374. }
  375. else if (state->treehash[i].stackusage == 0) {
  376. low = i;
  377. }
  378. else {
  379. low = treehash_minheight_on_stack(state, params, &(state->treehash[i]));
  380. }
  381. if (low < l_min) {
  382. level = i;
  383. l_min = low;
  384. }
  385. }
  386. if (level == h - k) {
  387. break;
  388. }
  389. // printf("Updated treehash instance on level %d\n", level);
  390. treehash_update(&(state->treehash[level]), state, sk_seed, params, pub_seed, addr);
  391. used++;
  392. }
  393. return updates - used;
  394. }
  395. /**
  396. * Updates the state (typically NEXT_i) by adding a leaf and updating the stack
  397. * Returns 1 if all leaf nodes have already been processed
  398. **/
  399. 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]) {
  400. unsigned char ltree_addr[16];
  401. unsigned char node_addr[16];
  402. unsigned char ots_addr[16];
  403. int n = params->n;
  404. int h = params->h;
  405. int k = params->k;
  406. int nodeh;
  407. int idx = state->next_leaf;
  408. if (idx == 1 << h) {
  409. return 1;
  410. }
  411. memcpy(ots_addr, addr, 10);
  412. SET_OTS_BIT(ots_addr, 1);
  413. SET_OTS_ADDRESS(ots_addr, idx);
  414. memcpy(ltree_addr, addr, 10);
  415. SET_OTS_BIT(ltree_addr, 0);
  416. SET_LTREE_BIT(ltree_addr, 1);
  417. SET_LTREE_ADDRESS(ltree_addr, idx);
  418. memcpy(node_addr, addr, 10);
  419. SET_LTREE_BIT(node_addr, 0);
  420. SET_OTS_BIT(node_addr, 0);
  421. SET_NODE_PADDING(node_addr);
  422. gen_leaf_wots(state->stack+state->stackoffset*n,sk_seed,params, pub_seed, ltree_addr, ots_addr);
  423. state->stacklevels[state->stackoffset] = 0;
  424. state->stackoffset++;
  425. if (h - k > 0 && idx == 3) {
  426. memcpy(state->treehash[0].node, state->stack+state->stackoffset*n, n);
  427. }
  428. while(state->stackoffset>1 && state->stacklevels[state->stackoffset-1] == state->stacklevels[state->stackoffset-2])
  429. {
  430. nodeh = state->stacklevels[state->stackoffset-1];
  431. if (idx >> nodeh == 1) {
  432. memcpy(state->auth + nodeh*n, state->stack+(state->stackoffset-1)*n, n);
  433. }
  434. else {
  435. if (nodeh < h - k && idx >> nodeh == 3) {
  436. memcpy(state->treehash[nodeh].node, state->stack+(state->stackoffset-1)*n, n);
  437. }
  438. else if (nodeh >= h - k) {
  439. memcpy(state->retain + ((1 << (h - 1 - nodeh)) + nodeh - h + (((idx >> nodeh) - 3) >> 1)) * n, state->stack+(state->stackoffset-1)*n, n);
  440. }
  441. }
  442. SET_NODE_TREE_HEIGHT(node_addr, state->stacklevels[state->stackoffset-1]);
  443. SET_NODE_TREE_INDEX(node_addr, (idx >> (state->stacklevels[state->stackoffset-1]+1)));
  444. hash_2n_n(state->stack+(state->stackoffset-2)*n,state->stack+(state->stackoffset-2)*n, pub_seed, node_addr, n);
  445. state->stacklevels[state->stackoffset-2]++;
  446. state->stackoffset--;
  447. }
  448. state->next_leaf++;
  449. return 0;
  450. }
  451. /**
  452. * Returns the auth path for node leaf_idx and computes the auth path for the
  453. * next leaf node, using the algorithm described by Buchmann, Dahmen and Szydlo
  454. * in "Post Quantum Cryptography", Springer 2009.
  455. */
  456. static void bds_round(bds_state *state, const unsigned long leaf_idx, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, unsigned char addr[16])
  457. {
  458. // printf("COMPUTING %llu\n", leaf_idx);
  459. unsigned int i;
  460. int n = params->n;
  461. int h = params->h;
  462. int k = params->k;
  463. int tau = h;
  464. int startidx;
  465. int offset, rowidx;
  466. unsigned char buf[2 * n];
  467. unsigned char ots_addr[16];
  468. unsigned char ltree_addr[16];
  469. unsigned char node_addr[16];
  470. memcpy(ots_addr, addr, 10);
  471. SET_OTS_BIT(ots_addr, 1);
  472. memcpy(ltree_addr, addr, 10);
  473. SET_OTS_BIT(ltree_addr, 0);
  474. SET_LTREE_BIT(ltree_addr, 1);
  475. memcpy(node_addr, ltree_addr, 10);
  476. SET_LTREE_BIT(node_addr, 0);
  477. SET_NODE_PADDING(node_addr);
  478. for (i = 0; i < h; i++) {
  479. if (! ((leaf_idx >> i) & 1)) {
  480. tau = i;
  481. break;
  482. }
  483. }
  484. if (tau > 0) {
  485. memcpy(buf, state->auth + (tau-1) * n, n);
  486. // we need to do this before refreshing state->keep to prevent overwriting
  487. memcpy(buf + n, state->keep + ((tau-1) >> 1) * n, n);
  488. }
  489. if (!((leaf_idx >> (tau + 1)) & 1) && (tau < h - 1)) {
  490. memcpy(state->keep + (tau >> 1)*n, state->auth + tau*n, n);
  491. }
  492. if (tau == 0) {
  493. SET_LTREE_ADDRESS(ltree_addr,leaf_idx);
  494. SET_OTS_ADDRESS(ots_addr,leaf_idx);
  495. gen_leaf_wots(state->auth, sk_seed, params, pub_seed, ltree_addr, ots_addr);
  496. }
  497. else {
  498. SET_NODE_TREE_HEIGHT(node_addr, (tau-1));
  499. SET_NODE_TREE_INDEX(node_addr, leaf_idx >> tau);
  500. hash_2n_n(state->auth + tau * n, buf, pub_seed, node_addr, n);
  501. for (i = 0; i < tau; i++) {
  502. if (i < h - k) {
  503. memcpy(state->auth + i * n, state->treehash[i].node, n);
  504. }
  505. else {
  506. offset = (1 << (h - 1 - i)) + i - h;
  507. rowidx = ((leaf_idx >> i) - 1) >> 1;
  508. memcpy(state->auth + i * n, state->retain + (offset + rowidx) * n, n);
  509. }
  510. }
  511. for (i = 0; i < ((tau < h - k) ? tau : (h - k)); i++) {
  512. startidx = leaf_idx + 1 + 3 * (1 << i);
  513. if (startidx < 1 << h) {
  514. state->treehash[i].h = i;
  515. state->treehash[i].next_idx = startidx;
  516. state->treehash[i].completed = 0;
  517. state->treehash[i].stackusage = 0;
  518. }
  519. }
  520. }
  521. }
  522. /*
  523. * Generates a XMSS key pair for a given parameter set.
  524. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED]
  525. * Format pk: [root || PUB_SEED] omitting algo oid.
  526. */
  527. int xmss_keypair(unsigned char *pk, unsigned char *sk, bds_state *state, xmss_params *params)
  528. {
  529. unsigned int n = params->n;
  530. unsigned int m = params->m;
  531. // Set idx = 0
  532. sk[0] = 0;
  533. sk[1] = 0;
  534. sk[2] = 0;
  535. sk[3] = 0;
  536. // Init SK_SEED (n byte), SK_PRF (m byte), and PUB_SEED (n byte)
  537. randombytes(sk+4,2*n+m);
  538. // Copy PUB_SEED to public key
  539. memcpy(pk+n, sk+4+n+m,n);
  540. unsigned char addr[16] = {0,0,0,0};
  541. // Compute root
  542. treehash_setup(pk, params->h, 0, state, sk+4, params, sk+4+n+m, addr);
  543. return 0;
  544. }
  545. /**
  546. * Signs a message.
  547. * Returns
  548. * 1. an array containing the signature followed by the message AND
  549. * 2. an updated secret key!
  550. *
  551. */
  552. 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)
  553. {
  554. unsigned int h = params->h;
  555. unsigned int n = params->n;
  556. unsigned int m = params->m;
  557. unsigned int k = params->k;
  558. // Extract SK
  559. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  560. unsigned char sk_seed[n];
  561. memcpy(sk_seed,sk+4,n);
  562. unsigned char sk_prf[m];
  563. memcpy(sk_prf,sk+4+n,m);
  564. unsigned char pub_seed[n];
  565. memcpy(pub_seed,sk+4+n+m,n);
  566. // Update SK
  567. sk[0] = ((idx + 1) >> 24) & 255;
  568. sk[1] = ((idx + 1) >> 16) & 255;
  569. sk[2] = ((idx + 1) >> 8) & 255;
  570. sk[3] = (idx + 1) & 255;
  571. // -- Secret key for this non-forward-secure version is now updated.
  572. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  573. // Init working params
  574. unsigned long long i;
  575. unsigned char R[m];
  576. unsigned char msg_h[m];
  577. unsigned char ots_seed[n];
  578. unsigned char ots_addr[16] = {0,0,0,0};
  579. // ---------------------------------
  580. // Message Hashing
  581. // ---------------------------------
  582. // Message Hash:
  583. // First compute pseudorandom key
  584. prf_m(R, msg, msglen, sk_prf, m);
  585. // Then use it for message digest
  586. hash_m(msg_h, msg, msglen, R, m, m);
  587. // Start collecting signature
  588. *sig_msg_len = 0;
  589. // Copy index to signature
  590. sig_msg[0] = (idx >> 24) & 255;
  591. sig_msg[1] = (idx >> 16) & 255;
  592. sig_msg[2] = (idx >> 8) & 255;
  593. sig_msg[3] = idx & 255;
  594. sig_msg += 4;
  595. *sig_msg_len += 4;
  596. // Copy R to signature
  597. for(i=0; i<m; i++)
  598. sig_msg[i] = R[i];
  599. sig_msg += m;
  600. *sig_msg_len += m;
  601. // ----------------------------------
  602. // Now we start to "really sign"
  603. // ----------------------------------
  604. // Prepare Address
  605. SET_OTS_BIT(ots_addr,1);
  606. SET_OTS_ADDRESS(ots_addr,idx);
  607. // Compute seed for OTS key pair
  608. get_seed(ots_seed, sk_seed, n, ots_addr);
  609. // Compute WOTS signature
  610. wots_sign(sig_msg, msg_h, ots_seed, &(params->wots_par), pub_seed, ots_addr);
  611. sig_msg += params->wots_par.keysize;
  612. *sig_msg_len += params->wots_par.keysize;
  613. // the auth path was already computed during the previous round
  614. memcpy(sig_msg, state->auth, h*n);
  615. if (idx < (1 << h) - 1) {
  616. bds_round(state, idx, sk_seed, params, pub_seed, ots_addr);
  617. bds_treehash_update(state, (h - k) >> 1, sk_seed, params, 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. int needswap_upto = -1;
  752. unsigned int updates;
  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 addr[16] = {0,0,0,0};
  761. unsigned char ots_addr[16] = {0,0,0,0};
  762. bds_state tmp;
  763. // Extract SK
  764. unsigned long long idx = 0;
  765. for(i = 0; i < idx_len; i++){
  766. idx |= ((unsigned long long)sk[i]) << 8*(idx_len - 1 - i);
  767. }
  768. memcpy(sk_seed,sk+idx_len,n);
  769. memcpy(sk_prf,sk+idx_len+n,m);
  770. memcpy(pub_seed,sk+idx_len+n+m,n);
  771. // Update SK
  772. for(i = 0; i < idx_len; i++){
  773. sk[i] = ((idx + 1) >> 8*(idx_len - 1 - i)) & 255;
  774. }
  775. // -- Secret key for this non-forward-secure version is now updated.
  776. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  777. // ---------------------------------
  778. // Message Hashing
  779. // ---------------------------------
  780. // Message Hash:
  781. // First compute pseudorandom key
  782. prf_m(R, msg, msglen, sk_prf, m);
  783. // Then use it for message digest
  784. hash_m(msg_h, msg, msglen, R, m, m);
  785. // Start collecting signature
  786. *sig_msg_len = 0;
  787. // Copy index to signature
  788. for(i = 0; i < idx_len; i++){
  789. sig_msg[i] = (idx >> 8*(idx_len - 1 - i)) & 255;
  790. }
  791. sig_msg += idx_len;
  792. *sig_msg_len += idx_len;
  793. // Copy R to signature
  794. for(i=0; i<m; i++)
  795. sig_msg[i] = R[i];
  796. sig_msg += m;
  797. *sig_msg_len += m;
  798. // ----------------------------------
  799. // Now we start to "really sign"
  800. // ----------------------------------
  801. // Handle lowest layer separately as it is slightly different...
  802. // Prepare Address
  803. SET_OTS_BIT(ots_addr,1);
  804. idx_tree = idx >> tree_h;
  805. idx_leaf = (idx & ((1 << tree_h)-1));
  806. SET_LAYER_ADDRESS(ots_addr,0);
  807. SET_TREE_ADDRESS(ots_addr, idx_tree);
  808. SET_OTS_ADDRESS(ots_addr, idx_leaf);
  809. // Compute seed for OTS key pair
  810. get_seed(ots_seed, sk_seed, n, ots_addr);
  811. // Compute WOTS signature
  812. wots_sign(sig_msg, msg_h, ots_seed, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  813. sig_msg += params->xmss_par.wots_par.keysize;
  814. *sig_msg_len += params->xmss_par.wots_par.keysize;
  815. memcpy(sig_msg, states[0].auth, tree_h*n);
  816. sig_msg += tree_h*n;
  817. *sig_msg_len += tree_h*n;
  818. // prepare signature of remaining layers
  819. for(i = 1; i < params->d; i++){
  820. // put WOTS signature in place
  821. memcpy(sig_msg, wots_sigs + (i-1)*params->xmss_par.wots_par.keysize, params->xmss_par.wots_par.keysize);
  822. sig_msg += params->xmss_par.wots_par.keysize;
  823. *sig_msg_len += params->xmss_par.wots_par.keysize;
  824. // put AUTH nodes in place
  825. memcpy(sig_msg, states[i].auth, tree_h*n);
  826. sig_msg += tree_h*n;
  827. *sig_msg_len += tree_h*n;
  828. }
  829. updates = (tree_h - k) >> 1;
  830. SET_LAYER_ADDRESS(addr, 0);
  831. SET_TREE_ADDRESS(addr, (idx_tree + 1));
  832. // mandatory update for NEXT_0 (does not count towards h-k/2)
  833. bds_state_update(&states[params->d], sk_seed, &(params->xmss_par), pub_seed, addr);
  834. for (i = 0; i < params->d; i++) {
  835. // check if we're not at the end of a tree
  836. if (! (((idx + 1) & ((1UL << ((i+1)*tree_h)) - 1)) == 0)) {
  837. idx_leaf = (idx >> (tree_h * i)) & ((1 << tree_h)-1);
  838. idx_tree = (idx >> (tree_h * (i+1)));
  839. SET_LAYER_ADDRESS(addr, i);
  840. SET_TREE_ADDRESS(addr, idx_tree);
  841. if (i == needswap_upto+1) {
  842. bds_round(&states[i], idx_leaf, sk_seed, &(params->xmss_par), pub_seed, addr);
  843. }
  844. updates = bds_treehash_update(&states[i], updates, sk_seed, &(params->xmss_par), pub_seed, addr);
  845. SET_TREE_ADDRESS(addr, (idx_tree + 1));
  846. // if a NEXT-tree exists for this level;
  847. if ((1 + idx_tree) * (1 << tree_h) + idx_leaf < (1UL << (h - tree_h * i))) {
  848. if (i > 0 && updates > 0 && states[params->d + i].next_leaf < (1UL << h)) {
  849. bds_state_update(&states[params->d + i], sk_seed, &(params->xmss_par), pub_seed, addr);
  850. updates--;
  851. }
  852. }
  853. }
  854. else if (idx < (1UL << h) - 1) {
  855. memcpy(&tmp, states+params->d + i, sizeof(bds_state));
  856. memcpy(states+params->d + i, states + i, sizeof(bds_state));
  857. memcpy(states + i, &tmp, sizeof(bds_state));
  858. SET_LAYER_ADDRESS(ots_addr, (i+1));
  859. SET_TREE_ADDRESS(ots_addr, ((idx + 1) >> ((i+2) * tree_h)));
  860. SET_OTS_ADDRESS(ots_addr, (((idx >> ((i+1) * tree_h)) + 1) & ((1 << tree_h)-1)));
  861. get_seed(ots_seed, sk+params->index_len, n, ots_addr);
  862. 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);
  863. states[params->d + i].stackoffset = 0;
  864. states[params->d + i].next_leaf = 0;
  865. updates--; // WOTS-signing counts as one update
  866. needswap_upto = i;
  867. for (j = 0; j < tree_h-k; j++) {
  868. states[i].treehash[j].completed = 1;
  869. }
  870. }
  871. }
  872. //Whipe secret elements?
  873. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  874. memcpy(sig_msg,msg,msglen);
  875. *sig_msg_len += msglen;
  876. return 0;
  877. }
  878. /**
  879. * Verifies a given message signature pair under a given public key.
  880. */
  881. 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)
  882. {
  883. unsigned int n = params->n;
  884. unsigned int m = params->m;
  885. unsigned int tree_h = params->xmss_par.h;
  886. unsigned int idx_len = params->index_len;
  887. unsigned long long idx_tree;
  888. unsigned long long idx_leaf;
  889. unsigned long long i, m_len;
  890. unsigned long long idx=0;
  891. unsigned char wots_pk[params->xmss_par.wots_par.keysize];
  892. unsigned char pkhash[n];
  893. unsigned char root[n];
  894. unsigned char msg_h[m];
  895. unsigned char pub_seed[n];
  896. memcpy(pub_seed,pk+n,n);
  897. // Init addresses
  898. unsigned char ots_addr[16] = {0,0,0,0};
  899. unsigned char ltree_addr[16];
  900. unsigned char node_addr[16];
  901. // Extract index
  902. for(i = 0; i < idx_len; i++){
  903. idx |= ((unsigned long long)sig_msg[i]) << (8*(idx_len - 1 - i));
  904. }
  905. printf("verify:: idx = %llu\n",idx);
  906. sig_msg += idx_len;
  907. sig_msg_len -= idx_len;
  908. // hash message (recall, R is now on pole position at sig_msg
  909. unsigned long long tmp_sig_len = m+ (params->d * params->xmss_par.wots_par.keysize) + (params->h * n);
  910. m_len = sig_msg_len - tmp_sig_len;
  911. hash_m(msg_h, sig_msg + tmp_sig_len, m_len, sig_msg, m, m);
  912. sig_msg += m;
  913. sig_msg_len -= m;
  914. //-----------------------
  915. // Verify signature
  916. //-----------------------
  917. // Prepare Address
  918. idx_tree = idx >> tree_h;
  919. idx_leaf = (idx & ((1 << tree_h)-1));
  920. SET_LAYER_ADDRESS(ots_addr,0);
  921. SET_TREE_ADDRESS(ots_addr, idx_tree);
  922. SET_OTS_BIT(ots_addr, 1);
  923. memcpy(ltree_addr, ots_addr, 10);
  924. SET_OTS_BIT(ltree_addr, 0);
  925. SET_LTREE_BIT(ltree_addr, 1);
  926. memcpy(node_addr, ltree_addr, 10);
  927. SET_LTREE_BIT(node_addr, 0);
  928. SET_NODE_PADDING(node_addr);
  929. SET_OTS_ADDRESS(ots_addr,idx_leaf);
  930. // Check WOTS signature
  931. wots_pkFromSig(wots_pk, sig_msg, msg_h, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  932. sig_msg += params->xmss_par.wots_par.keysize;
  933. sig_msg_len -= params->xmss_par.wots_par.keysize;
  934. // Compute Ltree
  935. SET_LTREE_ADDRESS(ltree_addr, idx_leaf);
  936. l_tree(pkhash, wots_pk, &(params->xmss_par), pub_seed, ltree_addr);
  937. // Compute root
  938. validate_authpath(root, pkhash, idx_leaf, sig_msg, &(params->xmss_par), pub_seed, node_addr);
  939. sig_msg += tree_h*n;
  940. sig_msg_len -= tree_h*n;
  941. for(i = 1; i < params->d; i++){
  942. // Prepare Address
  943. idx_leaf = (idx_tree & ((1 << tree_h)-1));
  944. idx_tree = idx_tree >> tree_h;
  945. SET_LAYER_ADDRESS(ots_addr,i);
  946. SET_TREE_ADDRESS(ots_addr, idx_tree);
  947. SET_OTS_BIT(ots_addr, 1);
  948. memcpy(ltree_addr, ots_addr, 10);
  949. SET_OTS_BIT(ltree_addr, 0);
  950. SET_LTREE_BIT(ltree_addr, 1);
  951. memcpy(node_addr, ltree_addr, 10);
  952. SET_LTREE_BIT(node_addr, 0);
  953. SET_NODE_PADDING(node_addr);
  954. SET_OTS_ADDRESS(ots_addr,idx_leaf);
  955. // Check WOTS signature
  956. wots_pkFromSig(wots_pk, sig_msg, root, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  957. sig_msg += params->xmss_par.wots_par.keysize;
  958. sig_msg_len -= params->xmss_par.wots_par.keysize;
  959. // Compute Ltree
  960. SET_LTREE_ADDRESS(ltree_addr, idx_leaf);
  961. l_tree(pkhash, wots_pk, &(params->xmss_par), pub_seed, ltree_addr);
  962. // Compute root
  963. validate_authpath(root, pkhash, idx_leaf, sig_msg, &(params->xmss_par), pub_seed, node_addr);
  964. sig_msg += tree_h*n;
  965. sig_msg_len -= tree_h*n;
  966. }
  967. for(i=0;i<n;i++)
  968. if(root[i] != pk[i])
  969. goto fail;
  970. *msglen = sig_msg_len;
  971. for(i=0;i<*msglen;i++)
  972. msg[i] = sig_msg[i];
  973. return 0;
  974. fail:
  975. *msglen = sig_msg_len;
  976. for(i=0;i<*msglen;i++)
  977. msg[i] = 0;
  978. *msglen = -1;
  979. return -1;
  980. }