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.

999 lines
28 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) & 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. static void get_seed(unsigned char seed[32], const unsigned char *sk_seed, unsigned char addr[16])
  70. {
  71. // Make sure that chain addr, hash addr, and key bit are 0!
  72. ZEROISE_OTS_ADDR(addr);
  73. // Generate pseudorandom value
  74. prg_with_counter(seed, 32, sk_seed, 32, addr);
  75. }
  76. /**
  77. * Initialize xmss params struct
  78. * parameter names are the same as in the draft
  79. * parameter k is K as used in the BDS algorithm
  80. */
  81. void xmss_set_params(xmss_params *params, int m, int n, int h, int w, int k)
  82. {
  83. params->h = h;
  84. params->m = m;
  85. params->n = n;
  86. params->k = k;
  87. wots_params wots_par;
  88. wots_set_params(&wots_par, m, n, w);
  89. params->wots_par = wots_par;
  90. }
  91. /**
  92. * Initialize BDS state struct
  93. * parameter names are the same as used in the description of the BDS traversal
  94. */
  95. 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)
  96. {
  97. state->stack = stack;
  98. state->stackoffset = stackoffset;
  99. state->stacklevels = stacklevels;
  100. state->auth = auth;
  101. state->keep = keep;
  102. state->treehash = treehash;
  103. state->retain = retain;
  104. }
  105. /**
  106. * Initialize xmssmt_params struct
  107. * parameter names are the same as in the draft
  108. *
  109. * Especially h is the total tree height, i.e. the XMSS trees have height h/d
  110. */
  111. void xmssmt_set_params(xmssmt_params *params, int m, int n, int h, int d, int w, int k)
  112. {
  113. if(h % d){
  114. fprintf(stderr, "d must devide h without remainder!\n");
  115. return;
  116. }
  117. params->h = h;
  118. params->d = d;
  119. params->m = m;
  120. params->n = n;
  121. params->index_len = (h + 7) / 8;
  122. xmss_params xmss_par;
  123. xmss_set_params(&xmss_par, m, n, (h/d), w, k);
  124. params->xmss_par = xmss_par;
  125. }
  126. /**
  127. * Computes a leaf from a WOTS public key using an L-tree.
  128. */
  129. static void l_tree(unsigned char *leaf, unsigned char *wots_pk, const xmss_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  130. {
  131. unsigned int l = params->wots_par.len;
  132. unsigned int n = params->n;
  133. unsigned long i = 0;
  134. unsigned int height = 0;
  135. //ADRS.setTreeHeight(0);
  136. SET_LTREE_TREE_HEIGHT(addr,height);
  137. unsigned long bound;
  138. while ( l > 1 )
  139. {
  140. bound = l >> 1; //floor(l / 2);
  141. for ( i = 0; i < bound; i = i + 1 ) {
  142. //ADRS.setTreeIndex(i);
  143. SET_LTREE_TREE_INDEX(addr,i);
  144. //wots_pk[i] = RAND_HASH(pk[2i], pk[2i + 1], SEED, ADRS);
  145. hash_2n_n(wots_pk+i*n,wots_pk+i*2*n, pub_seed, addr, n);
  146. }
  147. //if ( l % 2 == 1 ) {
  148. if(l&1)
  149. {
  150. //pk[floor(l / 2) + 1] = pk[l];
  151. memcpy(wots_pk+(l>>1)*n,wots_pk+(l-1)*n, n);
  152. //l = ceil(l / 2);
  153. l=(l>>1)+1;
  154. }
  155. else
  156. {
  157. //l = ceil(l / 2);
  158. l=(l>>1);
  159. }
  160. //ADRS.setTreeHeight(ADRS.getTreeHeight() + 1);
  161. height++;
  162. SET_LTREE_TREE_HEIGHT(addr,height);
  163. }
  164. //return pk[0];
  165. memcpy(leaf,wots_pk,n);
  166. }
  167. /**
  168. * 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.
  169. */
  170. 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])
  171. {
  172. unsigned char seed[32];
  173. unsigned char pk[params->wots_par.keysize];
  174. get_seed(seed, sk_seed, ots_addr);
  175. wots_pkgen(pk, seed, &(params->wots_par), pub_seed, ots_addr);
  176. l_tree(leaf, pk, params, pub_seed, ltree_addr);
  177. }
  178. static int treehash_minheight_on_stack(bds_state* state, const xmss_params *params, const treehash_inst *treehash) {
  179. int r = params->h, i;
  180. for (i = 0; i < treehash->stackusage; i++) {
  181. if (state->stacklevels[state->stackoffset - i - 1] < r) {
  182. r = state->stacklevels[state->stackoffset - i - 1];
  183. }
  184. }
  185. return r;
  186. }
  187. /**
  188. * Merkle's TreeHash algorithm. The address only needs to initialize the first 78 bits of addr. Everything else will be set by treehash.
  189. * Currently only used for key generation.
  190. *
  191. */
  192. 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])
  193. {
  194. unsigned int idx = index;
  195. unsigned int n = params->n;
  196. unsigned int h = params->h;
  197. unsigned int k = params->k;
  198. // use three different addresses because at this point we use all three formats in parallel
  199. unsigned char ots_addr[16];
  200. unsigned char ltree_addr[16];
  201. unsigned char node_addr[16];
  202. memcpy(ots_addr, addr, 10);
  203. SET_OTS_BIT(ots_addr, 1);
  204. memcpy(ltree_addr, addr, 10);
  205. SET_OTS_BIT(ltree_addr, 0);
  206. SET_LTREE_BIT(ltree_addr, 1);
  207. memcpy(node_addr, ltree_addr, 10);
  208. SET_LTREE_BIT(node_addr, 0);
  209. SET_NODE_PADDING(node_addr);
  210. int lastnode,i;
  211. unsigned char stack[(height+1)*n];
  212. unsigned int stacklevels[height+1];
  213. unsigned int stackoffset=0;
  214. int nodeh;
  215. lastnode = idx+(1<<height);
  216. for(i = 0; i < h-k; i++) {
  217. state->treehash[i].h = i;
  218. state->treehash[i].completed = 1;
  219. state->treehash[i].stackusage = 0;
  220. }
  221. i = 0;
  222. for(;idx<lastnode;idx++)
  223. {
  224. SET_LTREE_ADDRESS(ltree_addr,idx);
  225. SET_OTS_ADDRESS(ots_addr,idx);
  226. gen_leaf_wots(stack+stackoffset*n,sk_seed,params, pub_seed, ltree_addr, ots_addr);
  227. stacklevels[stackoffset] = 0;
  228. stackoffset++;
  229. if (h - k > 0 && i == 3) {
  230. memcpy(state->treehash[0].node, stack+stackoffset*n, n);
  231. }
  232. while(stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2])
  233. {
  234. nodeh = stacklevels[stackoffset-1];
  235. if (i >> nodeh == 1) {
  236. memcpy(state->auth + nodeh*n, stack+(stackoffset-1)*n, n);
  237. }
  238. else {
  239. if (nodeh < h - k && i >> nodeh == 3) {
  240. memcpy(state->treehash[nodeh].node, stack+(stackoffset-1)*n, n);
  241. }
  242. else if (nodeh >= h - k) {
  243. memcpy(state->retain + ((1 << (h - 1 - nodeh)) + nodeh - h + (((i >> nodeh) - 3) >> 1)) * n, stack+(stackoffset-1)*n, n);
  244. }
  245. }
  246. SET_NODE_TREE_HEIGHT(node_addr,stacklevels[stackoffset-1]);
  247. SET_NODE_TREE_INDEX(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
  248. hash_2n_n(stack+(stackoffset-2)*n,stack+(stackoffset-2)*n, pub_seed,
  249. node_addr, n);
  250. stacklevels[stackoffset-2]++;
  251. stackoffset--;
  252. }
  253. i++;
  254. }
  255. for(i=0;i<n;i++)
  256. node[i] = stack[i];
  257. }
  258. 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]) {
  259. int n = params->n;
  260. unsigned char ots_addr[16];
  261. unsigned char ltree_addr[16];
  262. unsigned char node_addr[16];
  263. memcpy(ots_addr, addr, 10);
  264. SET_OTS_BIT(ots_addr, 1);
  265. memcpy(ltree_addr, addr, 10);
  266. SET_OTS_BIT(ltree_addr, 0);
  267. SET_LTREE_BIT(ltree_addr, 1);
  268. memcpy(node_addr, ltree_addr, 10);
  269. SET_LTREE_BIT(node_addr, 0);
  270. SET_NODE_PADDING(node_addr);
  271. SET_LTREE_ADDRESS(ltree_addr, treehash->next_idx);
  272. SET_OTS_ADDRESS(ots_addr, treehash->next_idx);
  273. unsigned char nodebuffer[2 * n];
  274. unsigned int nodeheight = 0;
  275. gen_leaf_wots(nodebuffer, sk_seed, params, pub_seed, ltree_addr, ots_addr);
  276. while (treehash->stackusage > 0 && state->stacklevels[state->stackoffset-1] == nodeheight) {
  277. memcpy(nodebuffer + n, nodebuffer, n);
  278. memcpy(nodebuffer, state->stack + (state->stackoffset-1)*n, n);
  279. SET_NODE_TREE_HEIGHT(node_addr, nodeheight);
  280. SET_NODE_TREE_INDEX(node_addr, (treehash->next_idx >> (nodeheight+1)));
  281. hash_2n_n(nodebuffer, nodebuffer, pub_seed, node_addr, n);
  282. nodeheight++;
  283. treehash->stackusage--;
  284. state->stackoffset--;
  285. }
  286. if (nodeheight == treehash->h) { // this also implies stackusage == 0
  287. memcpy(treehash->node, nodebuffer, n);
  288. treehash->completed = 1;
  289. }
  290. else {
  291. memcpy(state->stack + state->stackoffset*n, nodebuffer, n);
  292. treehash->stackusage++;
  293. state->stacklevels[state->stackoffset] = nodeheight;
  294. state->stackoffset++;
  295. treehash->next_idx++;
  296. }
  297. }
  298. /**
  299. * Computes a root node given a leaf and an authapth
  300. */
  301. 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])
  302. {
  303. unsigned int n = params->n;
  304. int i,j;
  305. unsigned char buffer[2*n];
  306. // If leafidx is odd (last bit = 1), current path element is a right child and authpath has to go to the left.
  307. // Otherwise, it is the other way around
  308. if(leafidx&1)
  309. {
  310. for(j=0;j<n;j++)
  311. buffer[n+j] = leaf[j];
  312. for(j=0;j<n;j++)
  313. buffer[j] = authpath[j];
  314. }
  315. else
  316. {
  317. for(j=0;j<n;j++)
  318. buffer[j] = leaf[j];
  319. for(j=0;j<n;j++)
  320. buffer[n+j] = authpath[j];
  321. }
  322. authpath += n;
  323. for(i=0;i<params->h-1;i++)
  324. {
  325. SET_NODE_TREE_HEIGHT(addr,i);
  326. leafidx >>= 1;
  327. SET_NODE_TREE_INDEX(addr, leafidx);
  328. if(leafidx&1)
  329. {
  330. hash_2n_n(buffer+n,buffer,pub_seed, addr, n);
  331. for(j=0;j<n;j++)
  332. buffer[j] = authpath[j];
  333. }
  334. else
  335. {
  336. hash_2n_n(buffer,buffer,pub_seed, addr, n);
  337. for(j=0;j<n;j++)
  338. buffer[j+n] = authpath[j];
  339. }
  340. authpath += n;
  341. }
  342. SET_NODE_TREE_HEIGHT(addr, (params->h-1));
  343. leafidx >>= 1;
  344. SET_NODE_TREE_INDEX(addr, leafidx);
  345. hash_2n_n(root,buffer,pub_seed,addr,n);
  346. }
  347. /**
  348. * Returns the auth path for node leaf_idx and computes the auth path for the
  349. * next leaf node, using the algorithm described by Buchmann, Dahmen and Szydlo
  350. * in "Post Quantum Cryptography", Springer 2009.
  351. */
  352. static void compute_authpath_wots_fast(unsigned char *root, unsigned char *authpath, unsigned long leaf_idx, bds_state *state, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, unsigned char addr[16])
  353. {
  354. unsigned int i, j;
  355. int n = params->n;
  356. int h = params->h;
  357. int k = params->k;
  358. // the auth path was already computed during the previous round
  359. memcpy(authpath, state->auth, h*n);
  360. // TODO but we don't have the root handy yet.
  361. // memcpy(root, ???, n);
  362. int tau = h;
  363. int startidx;
  364. int offset, rowidx;
  365. int level, l_min, low;
  366. unsigned char buf[2 * n];
  367. unsigned char ots_addr[16];
  368. unsigned char ltree_addr[16];
  369. unsigned char node_addr[16];
  370. memcpy(ots_addr, addr, 10);
  371. SET_OTS_BIT(ots_addr, 1);
  372. memcpy(ltree_addr, addr, 10);
  373. SET_OTS_BIT(ltree_addr, 0);
  374. SET_LTREE_BIT(ltree_addr, 1);
  375. memcpy(node_addr, ltree_addr, 10);
  376. SET_LTREE_BIT(node_addr, 0);
  377. SET_NODE_PADDING(node_addr);
  378. for (i = 0; i < h; i++) {
  379. if (! ((leaf_idx >> i) & 1)) {
  380. tau = i;
  381. break;
  382. }
  383. }
  384. if (tau > 0) {
  385. memcpy(buf, state->auth + (tau-1) * n, n);
  386. // we need to do this before refreshing state->keep to prevent overwriting
  387. memcpy(buf + n, state->keep + ((tau-1) >> 1) * n, n);
  388. }
  389. if (!((leaf_idx >> (tau + 1)) & 1) && (tau < h - 1)) {
  390. memcpy(state->keep + (tau >> 1)*n, state->auth + tau*n, n);
  391. }
  392. if (tau == 0) {
  393. SET_LTREE_ADDRESS(ltree_addr,leaf_idx);
  394. SET_OTS_ADDRESS(ots_addr,leaf_idx);
  395. gen_leaf_wots(state->auth, sk_seed, params, pub_seed, ltree_addr, ots_addr);
  396. }
  397. else {
  398. SET_NODE_TREE_HEIGHT(node_addr, (tau-1));
  399. SET_NODE_TREE_INDEX(node_addr, leaf_idx >> tau);
  400. hash_2n_n(state->auth + tau * n, buf, pub_seed, node_addr, n);
  401. for (i = 0; i < tau; i++) {
  402. if (i < h - k) {
  403. memcpy(state->auth + i * n, state->treehash[i].node, n);
  404. }
  405. else {
  406. offset = (1 << (h - 1 - i)) + i - h;
  407. rowidx = ((leaf_idx >> i) - 1) >> 1;
  408. memcpy(state->auth + i * n, state->retain + (offset + rowidx) * n, n);
  409. }
  410. }
  411. for (i = 0; i < ((tau < h - k) ? tau : (h - k)); i++) {
  412. startidx = leaf_idx + 1 + 3 * (1 << i);
  413. if (startidx < 1 << h) {
  414. state->treehash[i].h = i;
  415. state->treehash[i].next_idx = startidx;
  416. state->treehash[i].completed = 0;
  417. }
  418. }
  419. }
  420. for (i = 0; i < (h - k) >> 1; i++) {
  421. l_min = h;
  422. level = h - k;
  423. for (j = 0; j < h - k; j++) {
  424. if (state->treehash[j].completed) {
  425. low = h;
  426. }
  427. else if (state->treehash[j].stackusage == 0) {
  428. low = j;
  429. }
  430. else {
  431. low = treehash_minheight_on_stack(state, params, &(state->treehash[j]));
  432. }
  433. if (low < l_min) {
  434. level = j;
  435. l_min = low;
  436. }
  437. }
  438. if (level != h - k) {
  439. treehash_update(&(state->treehash[level]), state, sk_seed, params, pub_seed, addr);
  440. }
  441. }
  442. }
  443. /*
  444. * Generates a XMSS key pair for a given parameter set.
  445. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED]
  446. * Format pk: [root || PUB_SEED] omitting algo oid.
  447. */
  448. int xmss_keypair(unsigned char *pk, unsigned char *sk, bds_state *state, xmss_params *params)
  449. {
  450. unsigned int n = params->n;
  451. unsigned int m = params->m;
  452. // Set idx = 0
  453. sk[0] = 0;
  454. sk[1] = 0;
  455. sk[2] = 0;
  456. sk[3] = 0;
  457. // Init SK_SEED (n byte), SK_PRF (m byte), and PUB_SEED (n byte)
  458. randombytes(sk+4,2*n+m);
  459. // Copy PUB_SEED to public key
  460. memcpy(pk+n, sk+4+n+m,n);
  461. unsigned char addr[16] = {0,0,0,0};
  462. // Compute root
  463. treehash_setup(pk, params->h, 0, state, sk+4, params, sk+4+n+m, addr);
  464. return 0;
  465. }
  466. /**
  467. * Signs a message.
  468. * Returns
  469. * 1. an array containing the signature followed by the message AND
  470. * 2. an updated secret key!
  471. *
  472. */
  473. 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)
  474. {
  475. unsigned int n = params->n;
  476. unsigned int m = params->m;
  477. // Extract SK
  478. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  479. unsigned char sk_seed[n];
  480. memcpy(sk_seed,sk+4,n);
  481. unsigned char sk_prf[m];
  482. memcpy(sk_prf,sk+4+n,m);
  483. unsigned char pub_seed[n];
  484. memcpy(pub_seed,sk+4+n+m,n);
  485. // Update SK
  486. sk[0] = ((idx + 1) >> 24) & 255;
  487. sk[1] = ((idx + 1) >> 16) & 255;
  488. sk[2] = ((idx + 1) >> 8) & 255;
  489. sk[3] = (idx + 1) & 255;
  490. // -- Secret key for this non-forward-secure version is now updated.
  491. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  492. // Init working params
  493. unsigned long long i;
  494. unsigned char R[m];
  495. unsigned char msg_h[m];
  496. unsigned char root[n];
  497. unsigned char ots_seed[n];
  498. unsigned char ots_addr[16] = {0,0,0,0};
  499. // ---------------------------------
  500. // Message Hashing
  501. // ---------------------------------
  502. // Message Hash:
  503. // First compute pseudorandom key
  504. prf_m(R, msg, msglen, sk_prf, m);
  505. // Then use it for message digest
  506. hash_m(msg_h, msg, msglen, R, m, m);
  507. // Start collecting signature
  508. *sig_msg_len = 0;
  509. // Copy index to signature
  510. sig_msg[0] = (idx >> 24) & 255;
  511. sig_msg[1] = (idx >> 16) & 255;
  512. sig_msg[2] = (idx >> 8) & 255;
  513. sig_msg[3] = idx & 255;
  514. sig_msg += 4;
  515. *sig_msg_len += 4;
  516. // Copy R to signature
  517. for(i=0; i<m; i++)
  518. sig_msg[i] = R[i];
  519. sig_msg += m;
  520. *sig_msg_len += m;
  521. // ----------------------------------
  522. // Now we start to "really sign"
  523. // ----------------------------------
  524. // Prepare Address
  525. SET_OTS_BIT(ots_addr,1);
  526. SET_OTS_ADDRESS(ots_addr,idx);
  527. // Compute seed for OTS key pair
  528. get_seed(ots_seed, sk_seed, ots_addr);
  529. // Compute WOTS signature
  530. wots_sign(sig_msg, msg_h, ots_seed, &(params->wots_par), pub_seed, ots_addr);
  531. sig_msg += params->wots_par.keysize;
  532. *sig_msg_len += params->wots_par.keysize;
  533. compute_authpath_wots_fast(root, sig_msg, idx, state, sk_seed, params, pub_seed, ots_addr);
  534. sig_msg += params->h*n;
  535. *sig_msg_len += params->h*n;
  536. //Whipe secret elements?
  537. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  538. memcpy(sig_msg,msg,msglen);
  539. *sig_msg_len += msglen;
  540. return 0;
  541. }
  542. /**
  543. * Verifies a given message signature pair under a given public key.
  544. */
  545. 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)
  546. {
  547. unsigned int n = params->n;
  548. unsigned int m = params->m;
  549. unsigned long long i, m_len;
  550. unsigned long idx=0;
  551. unsigned char wots_pk[params->wots_par.keysize];
  552. unsigned char pkhash[n];
  553. unsigned char root[n];
  554. unsigned char msg_h[m];
  555. unsigned char pub_seed[n];
  556. memcpy(pub_seed,pk+n,n);
  557. // Init addresses
  558. unsigned char ots_addr[16] = {0,0,0,0};
  559. unsigned char ltree_addr[16];
  560. unsigned char node_addr[16];
  561. SET_OTS_BIT(ots_addr, 1);
  562. memcpy(ltree_addr, ots_addr, 10);
  563. SET_OTS_BIT(ltree_addr, 0);
  564. SET_LTREE_BIT(ltree_addr, 1);
  565. memcpy(node_addr, ltree_addr, 10);
  566. SET_LTREE_BIT(node_addr, 0);
  567. SET_NODE_PADDING(node_addr);
  568. // Extract index
  569. idx = ((unsigned long)sig_msg[0] << 24) | ((unsigned long)sig_msg[1] << 16) | ((unsigned long)sig_msg[2] << 8) | sig_msg[3];
  570. printf("verify:: idx = %lu\n",idx);
  571. sig_msg += 4;
  572. sig_msg_len -= 4;
  573. // hash message (recall, R is now on pole position at sig_msg
  574. unsigned long long tmp_sig_len = m+params->wots_par.keysize+params->h*n;
  575. m_len = sig_msg_len - tmp_sig_len;
  576. hash_m(msg_h, sig_msg + tmp_sig_len, m_len, sig_msg, m, m);
  577. sig_msg += m;
  578. sig_msg_len -= m;
  579. //-----------------------
  580. // Verify signature
  581. //-----------------------
  582. // Prepare Address
  583. SET_OTS_ADDRESS(ots_addr,idx);
  584. // Check WOTS signature
  585. wots_pkFromSig(wots_pk, sig_msg, msg_h, &(params->wots_par), pub_seed, ots_addr);
  586. sig_msg += params->wots_par.keysize;
  587. sig_msg_len -= params->wots_par.keysize;
  588. // Compute Ltree
  589. SET_LTREE_ADDRESS(ltree_addr, idx);
  590. l_tree(pkhash, wots_pk, params, pub_seed, ltree_addr);
  591. // Compute root
  592. validate_authpath(root, pkhash, idx, sig_msg, params, pub_seed, node_addr);
  593. sig_msg += params->h*n;
  594. sig_msg_len -= params->h*n;
  595. for(i=0;i<n;i++)
  596. if(root[i] != pk[i])
  597. goto fail;
  598. *msglen = sig_msg_len;
  599. for(i=0;i<*msglen;i++)
  600. msg[i] = sig_msg[i];
  601. return 0;
  602. fail:
  603. *msglen = sig_msg_len;
  604. for(i=0;i<*msglen;i++)
  605. msg[i] = 0;
  606. *msglen = -1;
  607. return -1;
  608. }
  609. /*
  610. * Generates a XMSSMT key pair for a given parameter set.
  611. * Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || PUB_SEED]
  612. * Format pk: [root || PUB_SEED] omitting algo oid.
  613. */
  614. int xmssmt_keypair(unsigned char *pk, unsigned char *sk, bds_state *state, xmssmt_params *params)
  615. {
  616. unsigned int n = params->n;
  617. unsigned int m = params->m;
  618. unsigned int i;
  619. // Set idx = 0
  620. for (i = 0; i < params->index_len; i++){
  621. sk[i] = 0;
  622. }
  623. // Init SK_SEED (n byte), SK_PRF (m byte), and PUB_SEED (n byte)
  624. randombytes(sk+params->index_len,2*n+m);
  625. // Copy PUB_SEED to public key
  626. memcpy(pk+n, sk+params->index_len+n+m,n);
  627. // Set address to point on the single tree on layer d-1
  628. unsigned char addr[16] = {0,0,0,0};
  629. SET_LAYER_ADDRESS(addr, (params->d-1));
  630. // Compute root
  631. treehash_setup(pk, params->xmss_par.h, 0, state, sk+params->index_len, &(params->xmss_par), pk+n, addr);
  632. return 0;
  633. }
  634. /**
  635. * Signs a message.
  636. * Returns
  637. * 1. an array containing the signature followed by the message AND
  638. * 2. an updated secret key!
  639. *
  640. */
  641. int xmssmt_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 xmssmt_params *params)
  642. {
  643. unsigned int n = params->n;
  644. unsigned int m = params->m;
  645. unsigned int tree_h = params->xmss_par.h;
  646. unsigned int idx_len = params->index_len;
  647. unsigned long long idx_tree;
  648. unsigned long long idx_leaf;
  649. unsigned long long i;
  650. unsigned char sk_seed[n];
  651. unsigned char sk_prf[m];
  652. unsigned char pub_seed[n];
  653. // Init working params
  654. unsigned char R[m];
  655. unsigned char msg_h[m];
  656. unsigned char root[n];
  657. unsigned char ots_seed[n];
  658. unsigned char ots_addr[16] = {0,0,0,0};
  659. // Extract SK
  660. unsigned long long idx = 0;
  661. for(i = 0; i < idx_len; i++){
  662. idx |= ((unsigned long long)sk[i]) << 8*(idx_len - 1 - i);
  663. }
  664. memcpy(sk_seed,sk+idx_len,n);
  665. memcpy(sk_prf,sk+idx_len+n,m);
  666. memcpy(pub_seed,sk+idx_len+n+m,n);
  667. // Update SK
  668. for(i = 0; i < idx_len; i++){
  669. sk[i] = ((idx + 1) >> 8*(idx_len - 1 - i)) & 255;
  670. }
  671. // -- Secret key for this non-forward-secure version is now updated.
  672. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  673. // ---------------------------------
  674. // Message Hashing
  675. // ---------------------------------
  676. // Message Hash:
  677. // First compute pseudorandom key
  678. prf_m(R, msg, msglen, sk_prf, m);
  679. // Then use it for message digest
  680. hash_m(msg_h, msg, msglen, R, m, m);
  681. // Start collecting signature
  682. *sig_msg_len = 0;
  683. // Copy index to signature
  684. for(i = 0; i < idx_len; i++){
  685. sig_msg[i] = (idx >> 8*(idx_len - 1 - i)) & 255;
  686. }
  687. sig_msg += idx_len;
  688. *sig_msg_len += idx_len;
  689. // Copy R to signature
  690. for(i=0; i<m; i++)
  691. sig_msg[i] = R[i];
  692. sig_msg += m;
  693. *sig_msg_len += m;
  694. // ----------------------------------
  695. // Now we start to "really sign"
  696. // ----------------------------------
  697. // Handle lowest layer separately as it is slightly different...
  698. // Prepare Address
  699. SET_OTS_BIT(ots_addr,1);
  700. idx_tree = idx >> tree_h;
  701. idx_leaf = (idx & ((1 << tree_h)-1));
  702. SET_LAYER_ADDRESS(ots_addr,0);
  703. SET_TREE_ADDRESS(ots_addr, idx_tree);
  704. SET_OTS_ADDRESS(ots_addr, idx_leaf);
  705. // Compute seed for OTS key pair
  706. get_seed(ots_seed, sk_seed, ots_addr);
  707. // Compute WOTS signature
  708. wots_sign(sig_msg, msg_h, ots_seed, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  709. sig_msg += params->xmss_par.wots_par.keysize;
  710. *sig_msg_len += params->xmss_par.wots_par.keysize;
  711. compute_authpath_wots_fast(root, sig_msg, idx_leaf, state, sk_seed, &(params->xmss_par), pub_seed, ots_addr);
  712. sig_msg += tree_h*n;
  713. *sig_msg_len += tree_h*n;
  714. // Now loop over remaining layers...
  715. unsigned int j;
  716. for(j = 1; j < params->d; j++){
  717. // Prepare Address
  718. idx_leaf = (idx_tree & ((1 << tree_h)-1));
  719. idx_tree = idx_tree >> tree_h;
  720. SET_LAYER_ADDRESS(ots_addr,j);
  721. SET_TREE_ADDRESS(ots_addr, idx_tree);
  722. SET_OTS_ADDRESS(ots_addr, idx_leaf);
  723. // Compute seed for OTS key pair
  724. get_seed(ots_seed, sk_seed, ots_addr);
  725. // Compute WOTS signature
  726. wots_sign(sig_msg, root, ots_seed, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  727. sig_msg += params->xmss_par.wots_par.keysize;
  728. *sig_msg_len += params->xmss_par.wots_par.keysize;
  729. compute_authpath_wots_fast(root, sig_msg, idx_leaf, state, sk_seed, &(params->xmss_par), pub_seed, ots_addr);
  730. sig_msg += tree_h*n;
  731. *sig_msg_len += tree_h*n;
  732. }
  733. //Whipe secret elements?
  734. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  735. memcpy(sig_msg,msg,msglen);
  736. *sig_msg_len += msglen;
  737. return 0;
  738. }
  739. /**
  740. * Verifies a given message signature pair under a given public key.
  741. */
  742. 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)
  743. {
  744. unsigned int n = params->n;
  745. unsigned int m = params->m;
  746. unsigned int tree_h = params->xmss_par.h;
  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, m_len;
  751. unsigned long long idx=0;
  752. unsigned char wots_pk[params->xmss_par.wots_par.keysize];
  753. unsigned char pkhash[n];
  754. unsigned char root[n];
  755. unsigned char msg_h[m];
  756. unsigned char pub_seed[n];
  757. memcpy(pub_seed,pk+n,n);
  758. // Init addresses
  759. unsigned char ots_addr[16] = {0,0,0,0};
  760. unsigned char ltree_addr[16];
  761. unsigned char node_addr[16];
  762. // Extract index
  763. for(i = 0; i < idx_len; i++){
  764. idx |= ((unsigned long long)sig_msg[i]) << (8*(idx_len - 1 - i));
  765. }
  766. printf("verify:: idx = %llu\n",idx);
  767. sig_msg += idx_len;
  768. sig_msg_len -= idx_len;
  769. // hash message (recall, R is now on pole position at sig_msg
  770. unsigned long long tmp_sig_len = m+ (params->d * params->xmss_par.wots_par.keysize) + (params->h * n);
  771. m_len = sig_msg_len - tmp_sig_len;
  772. hash_m(msg_h, sig_msg + tmp_sig_len, m_len, sig_msg, m, m);
  773. sig_msg += m;
  774. sig_msg_len -= m;
  775. //-----------------------
  776. // Verify signature
  777. //-----------------------
  778. // Prepare Address
  779. idx_tree = idx >> tree_h;
  780. idx_leaf = (idx & ((1 << tree_h)-1));
  781. SET_LAYER_ADDRESS(ots_addr,0);
  782. SET_TREE_ADDRESS(ots_addr, idx_tree);
  783. SET_OTS_BIT(ots_addr, 1);
  784. memcpy(ltree_addr, ots_addr, 10);
  785. SET_OTS_BIT(ltree_addr, 0);
  786. SET_LTREE_BIT(ltree_addr, 1);
  787. memcpy(node_addr, ltree_addr, 10);
  788. SET_LTREE_BIT(node_addr, 0);
  789. SET_NODE_PADDING(node_addr);
  790. SET_OTS_ADDRESS(ots_addr,idx_leaf);
  791. // Check WOTS signature
  792. wots_pkFromSig(wots_pk, sig_msg, msg_h, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  793. sig_msg += params->xmss_par.wots_par.keysize;
  794. sig_msg_len -= params->xmss_par.wots_par.keysize;
  795. // Compute Ltree
  796. SET_LTREE_ADDRESS(ltree_addr, idx_leaf);
  797. l_tree(pkhash, wots_pk, &(params->xmss_par), pub_seed, ltree_addr);
  798. // Compute root
  799. validate_authpath(root, pkhash, idx_leaf, sig_msg, &(params->xmss_par), pub_seed, node_addr);
  800. sig_msg += tree_h*n;
  801. sig_msg_len -= tree_h*n;
  802. for(i = 1; i < params->d; i++){
  803. // Prepare Address
  804. idx_leaf = (idx_tree & ((1 << tree_h)-1));
  805. idx_tree = idx_tree >> tree_h;
  806. SET_LAYER_ADDRESS(ots_addr,i);
  807. SET_TREE_ADDRESS(ots_addr, idx_tree);
  808. SET_OTS_BIT(ots_addr, 1);
  809. memcpy(ltree_addr, ots_addr, 10);
  810. SET_OTS_BIT(ltree_addr, 0);
  811. SET_LTREE_BIT(ltree_addr, 1);
  812. memcpy(node_addr, ltree_addr, 10);
  813. SET_LTREE_BIT(node_addr, 0);
  814. SET_NODE_PADDING(node_addr);
  815. SET_OTS_ADDRESS(ots_addr,idx_leaf);
  816. // Check WOTS signature
  817. wots_pkFromSig(wots_pk, sig_msg, root, &(params->xmss_par.wots_par), pub_seed, ots_addr);
  818. sig_msg += params->xmss_par.wots_par.keysize;
  819. sig_msg_len -= params->xmss_par.wots_par.keysize;
  820. // Compute Ltree
  821. SET_LTREE_ADDRESS(ltree_addr, idx_leaf);
  822. l_tree(pkhash, wots_pk, &(params->xmss_par), pub_seed, ltree_addr);
  823. // Compute root
  824. validate_authpath(root, pkhash, idx_leaf, sig_msg, &(params->xmss_par), pub_seed, node_addr);
  825. sig_msg += tree_h*n;
  826. sig_msg_len -= tree_h*n;
  827. }
  828. for(i=0;i<n;i++)
  829. if(root[i] != pk[i])
  830. goto fail;
  831. *msglen = sig_msg_len;
  832. for(i=0;i<*msglen;i++)
  833. msg[i] = sig_msg[i];
  834. return 0;
  835. fail:
  836. *msglen = sig_msg_len;
  837. for(i=0;i<*msglen;i++)
  838. msg[i] = 0;
  839. *msglen = -1;
  840. return -1;
  841. }