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.

947 lines
33 KiB

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include "hash.h"
  5. #include "hash_address.h"
  6. #include "params.h"
  7. #include "randombytes.h"
  8. #include "wots.h"
  9. #include "utils.h"
  10. #include "xmss_commons.h"
  11. #include "xmss_core.h"
  12. typedef struct{
  13. unsigned char h;
  14. unsigned long next_idx;
  15. unsigned char stackusage;
  16. unsigned char completed;
  17. unsigned char *node;
  18. } treehash_inst;
  19. typedef struct {
  20. unsigned char *stack;
  21. unsigned int stackoffset;
  22. unsigned char *stacklevels;
  23. unsigned char *auth;
  24. unsigned char *keep;
  25. treehash_inst *treehash;
  26. unsigned char *retain;
  27. unsigned int next_leaf;
  28. } bds_state;
  29. /* These serialization functions provide a transition between the current
  30. way of storing the state in an exposed struct, and storing it as part of the
  31. byte array that is the secret key.
  32. They will probably be refactored in a non-backwards-compatible way, soon. */
  33. static void xmssmt_serialize_state(const xmss_params *params,
  34. unsigned char *sk, bds_state *states)
  35. {
  36. unsigned int i, j;
  37. /* Skip past the 'regular' sk */
  38. sk += params->index_bytes + 4*params->n;
  39. for (i = 0; i < 2*params->d - 1; i++) {
  40. sk += (params->tree_height + 1) * params->n; /* stack */
  41. ull_to_bytes(sk, 4, states[i].stackoffset);
  42. sk += 4;
  43. sk += params->tree_height + 1; /* stacklevels */
  44. sk += params->tree_height * params->n; /* auth */
  45. sk += (params->tree_height >> 1) * params->n; /* keep */
  46. for (j = 0; j < params->tree_height - params->bds_k; j++) {
  47. ull_to_bytes(sk, 1, states[i].treehash[j].h);
  48. sk += 1;
  49. ull_to_bytes(sk, 4, states[i].treehash[j].next_idx);
  50. sk += 4;
  51. ull_to_bytes(sk, 1, states[i].treehash[j].stackusage);
  52. sk += 1;
  53. ull_to_bytes(sk, 1, states[i].treehash[j].completed);
  54. sk += 1;
  55. sk += params->n; /* node */
  56. }
  57. /* retain */
  58. sk += ((1 << params->bds_k) - params->bds_k - 1) * params->n;
  59. ull_to_bytes(sk, 4, states[i].next_leaf);
  60. sk += 4;
  61. }
  62. }
  63. static void xmssmt_deserialize_state(const xmss_params *params,
  64. bds_state *states,
  65. unsigned char **wots_sigs,
  66. unsigned char *sk)
  67. {
  68. unsigned int i, j;
  69. /* Skip past the 'regular' sk */
  70. sk += params->index_bytes + 4*params->n;
  71. // TODO These data sizes follow from the (former) test xmss_core_fast.c
  72. // TODO They should be reconsidered / motivated more explicitly
  73. for (i = 0; i < 2*params->d - 1; i++) {
  74. states[i].stack = sk;
  75. sk += (params->tree_height + 1) * params->n;
  76. states[i].stackoffset = bytes_to_ull(sk, 4);
  77. sk += 4;
  78. states[i].stacklevels = sk;
  79. sk += params->tree_height + 1;
  80. states[i].auth = sk;
  81. sk += params->tree_height * params->n;
  82. states[i].keep = sk;
  83. sk += (params->tree_height >> 1) * params->n;
  84. for (j = 0; j < params->tree_height - params->bds_k; j++) {
  85. states[i].treehash[j].h = bytes_to_ull(sk, 1);
  86. sk += 1;
  87. states[i].treehash[j].next_idx = bytes_to_ull(sk, 4);
  88. sk += 4;
  89. states[i].treehash[j].stackusage = bytes_to_ull(sk, 1);
  90. sk += 1;
  91. states[i].treehash[j].completed = bytes_to_ull(sk, 1);
  92. sk += 1;
  93. states[i].treehash[j].node = sk;
  94. sk += params->n;
  95. }
  96. states[i].retain = sk;
  97. sk += ((1 << params->bds_k) - params->bds_k - 1) * params->n;
  98. states[i].next_leaf = bytes_to_ull(sk, 4);
  99. sk += 4;
  100. }
  101. if (params->d > 1) {
  102. *wots_sigs = sk;
  103. }
  104. }
  105. static void xmss_serialize_state(const xmss_params *params,
  106. unsigned char *sk, bds_state *state)
  107. {
  108. xmssmt_serialize_state(params, sk, state);
  109. }
  110. static void xmss_deserialize_state(const xmss_params *params,
  111. bds_state *state, unsigned char *sk)
  112. {
  113. xmssmt_deserialize_state(params, state, NULL, sk);
  114. }
  115. static void memswap(void *a, void *b, void *t, unsigned long long len)
  116. {
  117. memcpy(t, a, len);
  118. memcpy(a, b, len);
  119. memcpy(b, t, len);
  120. }
  121. /**
  122. * Swaps the content of two bds_state objects, swapping actual memory rather
  123. * than pointers.
  124. * As we're mapping memory chunks in the secret key to bds state objects,
  125. * it is now necessary to make swaps 'real swaps'. This could be done in the
  126. * serialization function as well, but that causes more overhead
  127. */
  128. // TODO this should not be necessary if we keep better track of the states
  129. static void deep_state_swap(const xmss_params *params,
  130. bds_state *a, bds_state *b)
  131. {
  132. // TODO this is extremely ugly and should be refactored
  133. // TODO right now, this ensures that both 'stack' and 'retain' fit
  134. unsigned char t[
  135. ((params->tree_height + 1) > ((1 << params->bds_k) - params->bds_k - 1)
  136. ? (params->tree_height + 1)
  137. : ((1 << params->bds_k) - params->bds_k - 1))
  138. * params->n];
  139. unsigned int i;
  140. memswap(a->stack, b->stack, t, (params->tree_height + 1) * params->n);
  141. memswap(&a->stackoffset, &b->stackoffset, t, sizeof(a->stackoffset));
  142. memswap(a->stacklevels, b->stacklevels, t, params->tree_height + 1);
  143. memswap(a->auth, b->auth, t, params->tree_height * params->n);
  144. memswap(a->keep, b->keep, t, (params->tree_height >> 1) * params->n);
  145. for (i = 0; i < params->tree_height - params->bds_k; i++) {
  146. memswap(&a->treehash[i].h, &b->treehash[i].h, t, sizeof(a->treehash[i].h));
  147. memswap(&a->treehash[i].next_idx, &b->treehash[i].next_idx, t, sizeof(a->treehash[i].next_idx));
  148. memswap(&a->treehash[i].stackusage, &b->treehash[i].stackusage, t, sizeof(a->treehash[i].stackusage));
  149. memswap(&a->treehash[i].completed, &b->treehash[i].completed, t, sizeof(a->treehash[i].completed));
  150. memswap(a->treehash[i].node, b->treehash[i].node, t, params->n);
  151. }
  152. memswap(a->retain, b->retain, t, ((1 << params->bds_k) - params->bds_k - 1) * params->n);
  153. memswap(&a->next_leaf, &b->next_leaf, t, sizeof(a->next_leaf));
  154. }
  155. static int treehash_minheight_on_stack(const xmss_params *params,
  156. bds_state *state,
  157. const treehash_inst *treehash)
  158. {
  159. unsigned int r = params->tree_height, i;
  160. for (i = 0; i < treehash->stackusage; i++) {
  161. if (state->stacklevels[state->stackoffset - i - 1] < r) {
  162. r = state->stacklevels[state->stackoffset - i - 1];
  163. }
  164. }
  165. return r;
  166. }
  167. /**
  168. * Merkle's TreeHash algorithm. The address only needs to initialize the first 78 bits of addr. Everything else will be set by treehash.
  169. * Currently only used for key generation.
  170. *
  171. */
  172. static void treehash_init(const xmss_params *params,
  173. unsigned char *node, int height, int index,
  174. bds_state *state, const unsigned char *sk_seed,
  175. const unsigned char *pub_seed, const uint32_t addr[8])
  176. {
  177. unsigned int idx = index;
  178. // use three different addresses because at this point we use all three formats in parallel
  179. uint32_t ots_addr[8] = {0};
  180. uint32_t ltree_addr[8] = {0};
  181. uint32_t node_addr[8] = {0};
  182. // only copy layer and tree address parts
  183. copy_subtree_addr(ots_addr, addr);
  184. // type = ots
  185. set_type(ots_addr, 0);
  186. copy_subtree_addr(ltree_addr, addr);
  187. set_type(ltree_addr, 1);
  188. copy_subtree_addr(node_addr, addr);
  189. set_type(node_addr, 2);
  190. uint32_t lastnode, i;
  191. unsigned char stack[(height+1)*params->n];
  192. unsigned int stacklevels[height+1];
  193. unsigned int stackoffset=0;
  194. unsigned int nodeh;
  195. lastnode = idx+(1<<height);
  196. for (i = 0; i < params->tree_height-params->bds_k; i++) {
  197. state->treehash[i].h = i;
  198. state->treehash[i].completed = 1;
  199. state->treehash[i].stackusage = 0;
  200. }
  201. i = 0;
  202. for (; idx < lastnode; idx++) {
  203. set_ltree_addr(ltree_addr, idx);
  204. set_ots_addr(ots_addr, idx);
  205. gen_leaf_wots(params, stack+stackoffset*params->n, sk_seed, pub_seed, ltree_addr, ots_addr);
  206. stacklevels[stackoffset] = 0;
  207. stackoffset++;
  208. if (params->tree_height - params->bds_k > 0 && i == 3) {
  209. memcpy(state->treehash[0].node, stack+stackoffset*params->n, params->n);
  210. }
  211. while (stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2]) {
  212. nodeh = stacklevels[stackoffset-1];
  213. if (i >> nodeh == 1) {
  214. memcpy(state->auth + nodeh*params->n, stack+(stackoffset-1)*params->n, params->n);
  215. }
  216. else {
  217. if (nodeh < params->tree_height - params->bds_k && i >> nodeh == 3) {
  218. memcpy(state->treehash[nodeh].node, stack+(stackoffset-1)*params->n, params->n);
  219. }
  220. else if (nodeh >= params->tree_height - params->bds_k) {
  221. memcpy(state->retain + ((1 << (params->tree_height - 1 - nodeh)) + nodeh - params->tree_height + (((i >> nodeh) - 3) >> 1)) * params->n, stack+(stackoffset-1)*params->n, params->n);
  222. }
  223. }
  224. set_tree_height(node_addr, stacklevels[stackoffset-1]);
  225. set_tree_index(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
  226. thash_h(params, stack+(stackoffset-2)*params->n, stack+(stackoffset-2)*params->n, pub_seed, node_addr);
  227. stacklevels[stackoffset-2]++;
  228. stackoffset--;
  229. }
  230. i++;
  231. }
  232. for (i = 0; i < params->n; i++) {
  233. node[i] = stack[i];
  234. }
  235. }
  236. static void treehash_update(const xmss_params *params,
  237. treehash_inst *treehash, bds_state *state,
  238. const unsigned char *sk_seed,
  239. const unsigned char *pub_seed,
  240. const uint32_t addr[8])
  241. {
  242. uint32_t ots_addr[8] = {0};
  243. uint32_t ltree_addr[8] = {0};
  244. uint32_t node_addr[8] = {0};
  245. // only copy layer and tree address parts
  246. copy_subtree_addr(ots_addr, addr);
  247. // type = ots
  248. set_type(ots_addr, 0);
  249. copy_subtree_addr(ltree_addr, addr);
  250. set_type(ltree_addr, 1);
  251. copy_subtree_addr(node_addr, addr);
  252. set_type(node_addr, 2);
  253. set_ltree_addr(ltree_addr, treehash->next_idx);
  254. set_ots_addr(ots_addr, treehash->next_idx);
  255. unsigned char nodebuffer[2 * params->n];
  256. unsigned int nodeheight = 0;
  257. gen_leaf_wots(params, nodebuffer, sk_seed, pub_seed, ltree_addr, ots_addr);
  258. while (treehash->stackusage > 0 && state->stacklevels[state->stackoffset-1] == nodeheight) {
  259. memcpy(nodebuffer + params->n, nodebuffer, params->n);
  260. memcpy(nodebuffer, state->stack + (state->stackoffset-1)*params->n, params->n);
  261. set_tree_height(node_addr, nodeheight);
  262. set_tree_index(node_addr, (treehash->next_idx >> (nodeheight+1)));
  263. thash_h(params, nodebuffer, nodebuffer, pub_seed, node_addr);
  264. nodeheight++;
  265. treehash->stackusage--;
  266. state->stackoffset--;
  267. }
  268. if (nodeheight == treehash->h) { // this also implies stackusage == 0
  269. memcpy(treehash->node, nodebuffer, params->n);
  270. treehash->completed = 1;
  271. }
  272. else {
  273. memcpy(state->stack + state->stackoffset*params->n, nodebuffer, params->n);
  274. treehash->stackusage++;
  275. state->stacklevels[state->stackoffset] = nodeheight;
  276. state->stackoffset++;
  277. treehash->next_idx++;
  278. }
  279. }
  280. /**
  281. * Performs treehash updates on the instance that needs it the most.
  282. * Returns the updated number of available updates.
  283. **/
  284. static char bds_treehash_update(const xmss_params *params,
  285. bds_state *state, unsigned int updates,
  286. const unsigned char *sk_seed,
  287. unsigned char *pub_seed,
  288. const uint32_t addr[8])
  289. {
  290. uint32_t i, j;
  291. unsigned int level, l_min, low;
  292. unsigned int used = 0;
  293. for (j = 0; j < updates; j++) {
  294. l_min = params->tree_height;
  295. level = params->tree_height - params->bds_k;
  296. for (i = 0; i < params->tree_height - params->bds_k; i++) {
  297. if (state->treehash[i].completed) {
  298. low = params->tree_height;
  299. }
  300. else if (state->treehash[i].stackusage == 0) {
  301. low = i;
  302. }
  303. else {
  304. low = treehash_minheight_on_stack(params, state, &(state->treehash[i]));
  305. }
  306. if (low < l_min) {
  307. level = i;
  308. l_min = low;
  309. }
  310. }
  311. if (level == params->tree_height - params->bds_k) {
  312. break;
  313. }
  314. treehash_update(params, &(state->treehash[level]), state, sk_seed, pub_seed, addr);
  315. used++;
  316. }
  317. return updates - used;
  318. }
  319. /**
  320. * Updates the state (typically NEXT_i) by adding a leaf and updating the stack
  321. * Returns -1 if all leaf nodes have already been processed
  322. **/
  323. static char bds_state_update(const xmss_params *params,
  324. bds_state *state, const unsigned char *sk_seed,
  325. const unsigned char *pub_seed,
  326. const uint32_t addr[8])
  327. {
  328. uint32_t ltree_addr[8] = {0};
  329. uint32_t node_addr[8] = {0};
  330. uint32_t ots_addr[8] = {0};
  331. unsigned int nodeh;
  332. int idx = state->next_leaf;
  333. if (idx == 1 << params->tree_height) {
  334. return -1;
  335. }
  336. // only copy layer and tree address parts
  337. copy_subtree_addr(ots_addr, addr);
  338. // type = ots
  339. set_type(ots_addr, 0);
  340. copy_subtree_addr(ltree_addr, addr);
  341. set_type(ltree_addr, 1);
  342. copy_subtree_addr(node_addr, addr);
  343. set_type(node_addr, 2);
  344. set_ots_addr(ots_addr, idx);
  345. set_ltree_addr(ltree_addr, idx);
  346. gen_leaf_wots(params, state->stack+state->stackoffset*params->n, sk_seed, pub_seed, ltree_addr, ots_addr);
  347. state->stacklevels[state->stackoffset] = 0;
  348. state->stackoffset++;
  349. if (params->tree_height - params->bds_k > 0 && idx == 3) {
  350. memcpy(state->treehash[0].node, state->stack+state->stackoffset*params->n, params->n);
  351. }
  352. while (state->stackoffset>1 && state->stacklevels[state->stackoffset-1] == state->stacklevels[state->stackoffset-2]) {
  353. nodeh = state->stacklevels[state->stackoffset-1];
  354. if (idx >> nodeh == 1) {
  355. memcpy(state->auth + nodeh*params->n, state->stack+(state->stackoffset-1)*params->n, params->n);
  356. }
  357. else {
  358. if (nodeh < params->tree_height - params->bds_k && idx >> nodeh == 3) {
  359. memcpy(state->treehash[nodeh].node, state->stack+(state->stackoffset-1)*params->n, params->n);
  360. }
  361. else if (nodeh >= params->tree_height - params->bds_k) {
  362. memcpy(state->retain + ((1 << (params->tree_height - 1 - nodeh)) + nodeh - params->tree_height + (((idx >> nodeh) - 3) >> 1)) * params->n, state->stack+(state->stackoffset-1)*params->n, params->n);
  363. }
  364. }
  365. set_tree_height(node_addr, state->stacklevels[state->stackoffset-1]);
  366. set_tree_index(node_addr, (idx >> (state->stacklevels[state->stackoffset-1]+1)));
  367. thash_h(params, state->stack+(state->stackoffset-2)*params->n, state->stack+(state->stackoffset-2)*params->n, pub_seed, node_addr);
  368. state->stacklevels[state->stackoffset-2]++;
  369. state->stackoffset--;
  370. }
  371. state->next_leaf++;
  372. return 0;
  373. }
  374. /**
  375. * Returns the auth path for node leaf_idx and computes the auth path for the
  376. * next leaf node, using the algorithm described by Buchmann, Dahmen and Szydlo
  377. * in "Post Quantum Cryptography", Springer 2009.
  378. */
  379. static void bds_round(const xmss_params *params,
  380. bds_state *state, const unsigned long leaf_idx,
  381. const unsigned char *sk_seed,
  382. const unsigned char *pub_seed, uint32_t addr[8])
  383. {
  384. unsigned int i;
  385. unsigned int tau = params->tree_height;
  386. unsigned int startidx;
  387. unsigned int offset, rowidx;
  388. unsigned char buf[2 * params->n];
  389. uint32_t ots_addr[8] = {0};
  390. uint32_t ltree_addr[8] = {0};
  391. uint32_t node_addr[8] = {0};
  392. // only copy layer and tree address parts
  393. copy_subtree_addr(ots_addr, addr);
  394. // type = ots
  395. set_type(ots_addr, 0);
  396. copy_subtree_addr(ltree_addr, addr);
  397. set_type(ltree_addr, 1);
  398. copy_subtree_addr(node_addr, addr);
  399. set_type(node_addr, 2);
  400. for (i = 0; i < params->tree_height; i++) {
  401. if (! ((leaf_idx >> i) & 1)) {
  402. tau = i;
  403. break;
  404. }
  405. }
  406. if (tau > 0) {
  407. memcpy(buf, state->auth + (tau-1) * params->n, params->n);
  408. // we need to do this before refreshing state->keep to prevent overwriting
  409. memcpy(buf + params->n, state->keep + ((tau-1) >> 1) * params->n, params->n);
  410. }
  411. if (!((leaf_idx >> (tau + 1)) & 1) && (tau < params->tree_height - 1)) {
  412. memcpy(state->keep + (tau >> 1)*params->n, state->auth + tau*params->n, params->n);
  413. }
  414. if (tau == 0) {
  415. set_ltree_addr(ltree_addr, leaf_idx);
  416. set_ots_addr(ots_addr, leaf_idx);
  417. gen_leaf_wots(params, state->auth, sk_seed, pub_seed, ltree_addr, ots_addr);
  418. }
  419. else {
  420. set_tree_height(node_addr, (tau-1));
  421. set_tree_index(node_addr, leaf_idx >> tau);
  422. thash_h(params, state->auth + tau * params->n, buf, pub_seed, node_addr);
  423. for (i = 0; i < tau; i++) {
  424. if (i < params->tree_height - params->bds_k) {
  425. memcpy(state->auth + i * params->n, state->treehash[i].node, params->n);
  426. }
  427. else {
  428. offset = (1 << (params->tree_height - 1 - i)) + i - params->tree_height;
  429. rowidx = ((leaf_idx >> i) - 1) >> 1;
  430. memcpy(state->auth + i * params->n, state->retain + (offset + rowidx) * params->n, params->n);
  431. }
  432. }
  433. for (i = 0; i < ((tau < params->tree_height - params->bds_k) ? tau : (params->tree_height - params->bds_k)); i++) {
  434. startidx = leaf_idx + 1 + 3 * (1 << i);
  435. if (startidx < 1U << params->tree_height) {
  436. state->treehash[i].h = i;
  437. state->treehash[i].next_idx = startidx;
  438. state->treehash[i].completed = 0;
  439. state->treehash[i].stackusage = 0;
  440. }
  441. }
  442. }
  443. }
  444. /**
  445. * Given a set of parameters, this function returns the size of the secret key.
  446. * This is implementation specific, as varying choices in tree traversal will
  447. * result in varying requirements for state storage.
  448. *
  449. * This function handles both XMSS and XMSSMT parameter sets.
  450. */
  451. unsigned long long xmss_xmssmt_core_sk_bytes(const xmss_params *params)
  452. {
  453. return params->index_bytes + 4 * params->n
  454. + (2 * params->d - 1) * (
  455. (params->tree_height + 1) * params->n
  456. + 4
  457. + params->tree_height + 1
  458. + params->tree_height * params->n
  459. + (params->tree_height >> 1) * params->n
  460. + (params->tree_height - params->bds_k) * (7 + params->n)
  461. + ((1 << params->bds_k) - params->bds_k - 1) * params->n
  462. + 4
  463. )
  464. + (params->d - 1) * params->wots_sig_bytes;
  465. }
  466. /*
  467. * Generates a XMSS key pair for a given parameter set.
  468. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
  469. * Format pk: [root || PUB_SEED] omitting algo oid.
  470. */
  471. int xmss_core_keypair(const xmss_params *params,
  472. unsigned char *pk, unsigned char *sk)
  473. {
  474. uint32_t addr[8] = {0};
  475. // TODO refactor BDS state not to need separate treehash instances
  476. bds_state state;
  477. treehash_inst treehash[params->tree_height - params->bds_k];
  478. state.treehash = treehash;
  479. xmss_deserialize_state(params, &state, sk);
  480. state.stackoffset = 0;
  481. state.next_leaf = 0;
  482. // Set idx = 0
  483. sk[0] = 0;
  484. sk[1] = 0;
  485. sk[2] = 0;
  486. sk[3] = 0;
  487. // Init SK_SEED (n byte), SK_PRF (n byte), and PUB_SEED (n byte)
  488. randombytes(sk + params->index_bytes, 3*params->n);
  489. // Copy PUB_SEED to public key
  490. memcpy(pk + params->n, sk + params->index_bytes + 2*params->n, params->n);
  491. // Compute root
  492. treehash_init(params, pk, params->tree_height, 0, &state, sk + params->index_bytes, sk + params->index_bytes + 2*params->n, addr);
  493. // copy root o sk
  494. memcpy(sk + params->index_bytes + 3*params->n, pk, params->n);
  495. /* Write the BDS state into sk. */
  496. xmss_serialize_state(params, sk, &state);
  497. return 0;
  498. }
  499. /**
  500. * Signs a message.
  501. * Returns
  502. * 1. an array containing the signature followed by the message AND
  503. * 2. an updated secret key!
  504. *
  505. */
  506. int xmss_core_sign(const xmss_params *params,
  507. unsigned char *sk,
  508. unsigned char *sm, unsigned long long *smlen,
  509. const unsigned char *m, unsigned long long mlen)
  510. {
  511. const unsigned char *pub_root = sk + params->index_bytes + 3*params->n;
  512. uint16_t i = 0;
  513. // TODO refactor BDS state not to need separate treehash instances
  514. bds_state state;
  515. treehash_inst treehash[params->tree_height - params->bds_k];
  516. state.treehash = treehash;
  517. /* Load the BDS state from sk. */
  518. xmss_deserialize_state(params, &state, sk);
  519. // Extract SK
  520. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  521. unsigned char sk_seed[params->n];
  522. memcpy(sk_seed, sk + params->index_bytes, params->n);
  523. unsigned char sk_prf[params->n];
  524. memcpy(sk_prf, sk + params->index_bytes + params->n, params->n);
  525. unsigned char pub_seed[params->n];
  526. memcpy(pub_seed, sk + params->index_bytes + 2*params->n, params->n);
  527. // index as 32 bytes string
  528. unsigned char idx_bytes_32[32];
  529. ull_to_bytes(idx_bytes_32, 32, idx);
  530. // Update SK
  531. sk[0] = ((idx + 1) >> 24) & 255;
  532. sk[1] = ((idx + 1) >> 16) & 255;
  533. sk[2] = ((idx + 1) >> 8) & 255;
  534. sk[3] = (idx + 1) & 255;
  535. // Secret key for this non-forward-secure version is now updated.
  536. // A production implementation should consider using a file handle instead,
  537. // and write the updated secret key at this point!
  538. // Init working params
  539. unsigned char R[params->n];
  540. unsigned char msg_h[params->n];
  541. unsigned char ots_seed[params->n];
  542. uint32_t ots_addr[8] = {0};
  543. // ---------------------------------
  544. // Message Hashing
  545. // ---------------------------------
  546. // Message Hash:
  547. // First compute pseudorandom value
  548. prf(params, R, idx_bytes_32, sk_prf);
  549. /* Already put the message in the right place, to make it easier to prepend
  550. * things when computing the hash over the message. */
  551. memcpy(sm + params->sig_bytes, m, mlen);
  552. /* Compute the message hash. */
  553. hash_message(params, msg_h, R, pub_root, idx,
  554. sm + params->sig_bytes - 4*params->n, mlen);
  555. // Start collecting signature
  556. *smlen = 0;
  557. // Copy index to signature
  558. sm[0] = (idx >> 24) & 255;
  559. sm[1] = (idx >> 16) & 255;
  560. sm[2] = (idx >> 8) & 255;
  561. sm[3] = idx & 255;
  562. sm += 4;
  563. *smlen += 4;
  564. // Copy R to signature
  565. for (i = 0; i < params->n; i++) {
  566. sm[i] = R[i];
  567. }
  568. sm += params->n;
  569. *smlen += params->n;
  570. // ----------------------------------
  571. // Now we start to "really sign"
  572. // ----------------------------------
  573. // Prepare Address
  574. set_type(ots_addr, 0);
  575. set_ots_addr(ots_addr, idx);
  576. // Compute seed for OTS key pair
  577. get_seed(params, ots_seed, sk_seed, ots_addr);
  578. // Compute WOTS signature
  579. wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
  580. sm += params->wots_sig_bytes;
  581. *smlen += params->wots_sig_bytes;
  582. // the auth path was already computed during the previous round
  583. memcpy(sm, state.auth, params->tree_height*params->n);
  584. if (idx < (1U << params->tree_height) - 1) {
  585. bds_round(params, &state, idx, sk_seed, pub_seed, ots_addr);
  586. bds_treehash_update(params, &state, (params->tree_height - params->bds_k) >> 1, sk_seed, pub_seed, ots_addr);
  587. }
  588. sm += params->tree_height*params->n;
  589. *smlen += params->tree_height*params->n;
  590. memcpy(sm, m, mlen);
  591. *smlen += mlen;
  592. /* Write the updated BDS state back into sk. */
  593. xmss_serialize_state(params, sk, &state);
  594. return 0;
  595. }
  596. /*
  597. * Generates a XMSSMT key pair for a given parameter set.
  598. * Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
  599. * Format pk: [root || PUB_SEED] omitting algo oid.
  600. */
  601. int xmssmt_core_keypair(const xmss_params *params,
  602. unsigned char *pk, unsigned char *sk)
  603. {
  604. unsigned char ots_seed[params->n];
  605. uint32_t addr[8] = {0};
  606. unsigned int i;
  607. unsigned char *wots_sigs;
  608. // TODO refactor BDS state not to need separate treehash instances
  609. bds_state states[2*params->d - 1];
  610. treehash_inst treehash[(2*params->d - 1) * (params->tree_height - params->bds_k)];
  611. for (i = 0; i < 2*params->d - 1; i++) {
  612. states[i].treehash = treehash + i * (params->tree_height - params->bds_k);
  613. }
  614. xmssmt_deserialize_state(params, states, &wots_sigs, sk);
  615. for (i = 0; i < 2 * params->d - 1; i++) {
  616. states[i].stackoffset = 0;
  617. states[i].next_leaf = 0;
  618. }
  619. // Set idx = 0
  620. for (i = 0; i < params->index_bytes; i++) {
  621. sk[i] = 0;
  622. }
  623. // Init SK_SEED (params->n byte), SK_PRF (params->n byte), and PUB_SEED (params->n byte)
  624. randombytes(sk+params->index_bytes, 3*params->n);
  625. // Copy PUB_SEED to public key
  626. memcpy(pk+params->n, sk+params->index_bytes+2*params->n, params->n);
  627. // Start with the bottom-most layer
  628. set_layer_addr(addr, 0);
  629. // Set up state and compute wots signatures for all but topmost tree root
  630. for (i = 0; i < params->d - 1; i++) {
  631. // Compute seed for OTS key pair
  632. treehash_init(params, pk, params->tree_height, 0, states + i, sk+params->index_bytes, pk+params->n, addr);
  633. set_layer_addr(addr, (i+1));
  634. get_seed(params, ots_seed, sk + params->index_bytes, addr);
  635. wots_sign(params, wots_sigs + i*params->wots_sig_bytes, pk, ots_seed, pk+params->n, addr);
  636. }
  637. // Address now points to the single tree on layer d-1
  638. treehash_init(params, pk, params->tree_height, 0, states + i, sk+params->index_bytes, pk+params->n, addr);
  639. memcpy(sk + params->index_bytes + 3*params->n, pk, params->n);
  640. xmssmt_serialize_state(params, sk, states);
  641. return 0;
  642. }
  643. /**
  644. * Signs a message.
  645. * Returns
  646. * 1. an array containing the signature followed by the message AND
  647. * 2. an updated secret key!
  648. *
  649. */
  650. int xmssmt_core_sign(const xmss_params *params,
  651. unsigned char *sk,
  652. unsigned char *sm, unsigned long long *smlen,
  653. const unsigned char *m, unsigned long long mlen)
  654. {
  655. const unsigned char *pub_root = sk + params->index_bytes + 3*params->n;
  656. uint64_t idx_tree;
  657. uint32_t idx_leaf;
  658. uint64_t i, j;
  659. int needswap_upto = -1;
  660. unsigned int updates;
  661. unsigned char sk_seed[params->n];
  662. unsigned char sk_prf[params->n];
  663. unsigned char pub_seed[params->n];
  664. // Init working params
  665. unsigned char R[params->n];
  666. unsigned char msg_h[params->n];
  667. unsigned char ots_seed[params->n];
  668. uint32_t addr[8] = {0};
  669. uint32_t ots_addr[8] = {0};
  670. unsigned char idx_bytes_32[32];
  671. unsigned char *wots_sigs;
  672. // TODO refactor BDS state not to need separate treehash instances
  673. bds_state states[2*params->d - 1];
  674. treehash_inst treehash[(2*params->d - 1) * (params->tree_height - params->bds_k)];
  675. for (i = 0; i < 2*params->d - 1; i++) {
  676. states[i].treehash = treehash + i * (params->tree_height - params->bds_k);
  677. }
  678. xmssmt_deserialize_state(params, states, &wots_sigs, sk);
  679. // Extract SK
  680. unsigned long long idx = 0;
  681. for (i = 0; i < params->index_bytes; i++) {
  682. idx |= ((unsigned long long)sk[i]) << 8*(params->index_bytes - 1 - i);
  683. }
  684. memcpy(sk_seed, sk+params->index_bytes, params->n);
  685. memcpy(sk_prf, sk+params->index_bytes+params->n, params->n);
  686. memcpy(pub_seed, sk+params->index_bytes+2*params->n, params->n);
  687. // Update SK
  688. for (i = 0; i < params->index_bytes; i++) {
  689. sk[i] = ((idx + 1) >> 8*(params->index_bytes - 1 - i)) & 255;
  690. }
  691. // Secret key for this non-forward-secure version is now updated.
  692. // A production implementation should consider using a file handle instead,
  693. // and write the updated secret key at this point!
  694. // ---------------------------------
  695. // Message Hashing
  696. // ---------------------------------
  697. // Message Hash:
  698. // First compute pseudorandom value
  699. ull_to_bytes(idx_bytes_32, 32, idx);
  700. prf(params, R, idx_bytes_32, sk_prf);
  701. /* Already put the message in the right place, to make it easier to prepend
  702. * things when computing the hash over the message. */
  703. memcpy(sm + params->sig_bytes, m, mlen);
  704. /* Compute the message hash. */
  705. hash_message(params, msg_h, R, pub_root, idx,
  706. sm + params->sig_bytes - 4*params->n, mlen);
  707. // Start collecting signature
  708. *smlen = 0;
  709. // Copy index to signature
  710. for (i = 0; i < params->index_bytes; i++) {
  711. sm[i] = (idx >> 8*(params->index_bytes - 1 - i)) & 255;
  712. }
  713. sm += params->index_bytes;
  714. *smlen += params->index_bytes;
  715. // Copy R to signature
  716. for (i = 0; i < params->n; i++) {
  717. sm[i] = R[i];
  718. }
  719. sm += params->n;
  720. *smlen += params->n;
  721. // ----------------------------------
  722. // Now we start to "really sign"
  723. // ----------------------------------
  724. // Handle lowest layer separately as it is slightly different...
  725. // Prepare Address
  726. set_type(ots_addr, 0);
  727. idx_tree = idx >> params->tree_height;
  728. idx_leaf = (idx & ((1 << params->tree_height)-1));
  729. set_layer_addr(ots_addr, 0);
  730. set_tree_addr(ots_addr, idx_tree);
  731. set_ots_addr(ots_addr, idx_leaf);
  732. // Compute seed for OTS key pair
  733. get_seed(params, ots_seed, sk_seed, ots_addr);
  734. // Compute WOTS signature
  735. wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
  736. sm += params->wots_sig_bytes;
  737. *smlen += params->wots_sig_bytes;
  738. memcpy(sm, states[0].auth, params->tree_height*params->n);
  739. sm += params->tree_height*params->n;
  740. *smlen += params->tree_height*params->n;
  741. // prepare signature of remaining layers
  742. for (i = 1; i < params->d; i++) {
  743. // put WOTS signature in place
  744. memcpy(sm, wots_sigs + (i-1)*params->wots_sig_bytes, params->wots_sig_bytes);
  745. sm += params->wots_sig_bytes;
  746. *smlen += params->wots_sig_bytes;
  747. // put AUTH nodes in place
  748. memcpy(sm, states[i].auth, params->tree_height*params->n);
  749. sm += params->tree_height*params->n;
  750. *smlen += params->tree_height*params->n;
  751. }
  752. updates = (params->tree_height - params->bds_k) >> 1;
  753. set_tree_addr(addr, (idx_tree + 1));
  754. // mandatory update for NEXT_0 (does not count towards h-k/2) if NEXT_0 exists
  755. if ((1 + idx_tree) * (1 << params->tree_height) + idx_leaf < (1ULL << params->full_height)) {
  756. bds_state_update(params, &states[params->d], sk_seed, pub_seed, addr);
  757. }
  758. for (i = 0; i < params->d; i++) {
  759. // check if we're not at the end of a tree
  760. if (! (((idx + 1) & ((1ULL << ((i+1)*params->tree_height)) - 1)) == 0)) {
  761. idx_leaf = (idx >> (params->tree_height * i)) & ((1 << params->tree_height)-1);
  762. idx_tree = (idx >> (params->tree_height * (i+1)));
  763. set_layer_addr(addr, i);
  764. set_tree_addr(addr, idx_tree);
  765. if (i == (unsigned int) (needswap_upto + 1)) {
  766. bds_round(params, &states[i], idx_leaf, sk_seed, pub_seed, addr);
  767. }
  768. updates = bds_treehash_update(params, &states[i], updates, sk_seed, pub_seed, addr);
  769. set_tree_addr(addr, (idx_tree + 1));
  770. // if a NEXT-tree exists for this level;
  771. if ((1 + idx_tree) * (1 << params->tree_height) + idx_leaf < (1ULL << (params->full_height - params->tree_height * i))) {
  772. if (i > 0 && updates > 0 && states[params->d + i].next_leaf < (1ULL << params->full_height)) {
  773. bds_state_update(params, &states[params->d + i], sk_seed, pub_seed, addr);
  774. updates--;
  775. }
  776. }
  777. }
  778. else if (idx < (1ULL << params->full_height) - 1) {
  779. deep_state_swap(params, states+params->d + i, states + i);
  780. set_layer_addr(ots_addr, (i+1));
  781. set_tree_addr(ots_addr, ((idx + 1) >> ((i+2) * params->tree_height)));
  782. set_ots_addr(ots_addr, (((idx >> ((i+1) * params->tree_height)) + 1) & ((1 << params->tree_height)-1)));
  783. get_seed(params, ots_seed, sk+params->index_bytes, ots_addr);
  784. wots_sign(params, wots_sigs + i*params->wots_sig_bytes, states[i].stack, ots_seed, pub_seed, ots_addr);
  785. states[params->d + i].stackoffset = 0;
  786. states[params->d + i].next_leaf = 0;
  787. updates--; // WOTS-signing counts as one update
  788. needswap_upto = i;
  789. for (j = 0; j < params->tree_height-params->bds_k; j++) {
  790. states[i].treehash[j].completed = 1;
  791. }
  792. }
  793. }
  794. memcpy(sm, m, mlen);
  795. *smlen += mlen;
  796. xmssmt_serialize_state(params, sk, states);
  797. return 0;
  798. }