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.

1005 lines
28 KiB

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