xmss_fast.c 32 KB

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