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.

942 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 || root || PUB_SEED]
  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) and SK_PRF (n byte)
  488. randombytes(sk + params->index_bytes, 2*params->n);
  489. // Init PUB_SEED (n byte)
  490. randombytes(sk + params->index_bytes + 3*params->n, params->n);
  491. // Copy PUB_SEED to public key
  492. memcpy(pk + params->n, sk + params->index_bytes + 3*params->n, params->n);
  493. // Compute root
  494. treehash_init(params, pk, params->tree_height, 0, &state, sk + params->index_bytes, sk + params->index_bytes + 3*params->n, addr);
  495. // copy root to sk
  496. memcpy(sk + params->index_bytes + 2*params->n, pk, params->n);
  497. /* Write the BDS state into sk. */
  498. xmss_serialize_state(params, sk, &state);
  499. return 0;
  500. }
  501. /**
  502. * Signs a message.
  503. * Returns
  504. * 1. an array containing the signature followed by the message AND
  505. * 2. an updated secret key!
  506. *
  507. */
  508. int xmss_core_sign(const xmss_params *params,
  509. unsigned char *sk,
  510. unsigned char *sm, unsigned long long *smlen,
  511. const unsigned char *m, unsigned long long mlen)
  512. {
  513. const unsigned char *pub_root = sk + params->index_bytes + 2*params->n;
  514. uint16_t i = 0;
  515. // TODO refactor BDS state not to need separate treehash instances
  516. bds_state state;
  517. treehash_inst treehash[params->tree_height - params->bds_k];
  518. state.treehash = treehash;
  519. /* Load the BDS state from sk. */
  520. xmss_deserialize_state(params, &state, sk);
  521. // Extract SK
  522. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  523. unsigned char sk_seed[params->n];
  524. memcpy(sk_seed, sk + params->index_bytes, params->n);
  525. unsigned char sk_prf[params->n];
  526. memcpy(sk_prf, sk + params->index_bytes + params->n, params->n);
  527. unsigned char pub_seed[params->n];
  528. memcpy(pub_seed, sk + params->index_bytes + 3*params->n, params->n);
  529. // index as 32 bytes string
  530. unsigned char idx_bytes_32[32];
  531. ull_to_bytes(idx_bytes_32, 32, idx);
  532. // Update SK
  533. sk[0] = ((idx + 1) >> 24) & 255;
  534. sk[1] = ((idx + 1) >> 16) & 255;
  535. sk[2] = ((idx + 1) >> 8) & 255;
  536. sk[3] = (idx + 1) & 255;
  537. // Secret key for this non-forward-secure version is now updated.
  538. // A production implementation should consider using a file handle instead,
  539. // and write the updated secret key at this point!
  540. // Init working params
  541. unsigned char R[params->n];
  542. unsigned char msg_h[params->n];
  543. uint32_t ots_addr[8] = {0};
  544. // ---------------------------------
  545. // Message Hashing
  546. // ---------------------------------
  547. // Message Hash:
  548. // First compute pseudorandom value
  549. prf(params, R, idx_bytes_32, sk_prf);
  550. /* Already put the message in the right place, to make it easier to prepend
  551. * things when computing the hash over the message. */
  552. memcpy(sm + params->sig_bytes, m, mlen);
  553. /* Compute the message hash. */
  554. hash_message(params, msg_h, R, pub_root, idx,
  555. sm + params->sig_bytes - 4*params->n, mlen);
  556. // Start collecting signature
  557. *smlen = 0;
  558. // Copy index to signature
  559. sm[0] = (idx >> 24) & 255;
  560. sm[1] = (idx >> 16) & 255;
  561. sm[2] = (idx >> 8) & 255;
  562. sm[3] = idx & 255;
  563. sm += 4;
  564. *smlen += 4;
  565. // Copy R to signature
  566. for (i = 0; i < params->n; i++) {
  567. sm[i] = R[i];
  568. }
  569. sm += params->n;
  570. *smlen += params->n;
  571. // ----------------------------------
  572. // Now we start to "really sign"
  573. // ----------------------------------
  574. // Prepare Address
  575. set_type(ots_addr, 0);
  576. set_ots_addr(ots_addr, idx);
  577. // Compute WOTS signature
  578. wots_sign(params, sm, msg_h, sk_seed, pub_seed, ots_addr);
  579. sm += params->wots_sig_bytes;
  580. *smlen += params->wots_sig_bytes;
  581. // the auth path was already computed during the previous round
  582. memcpy(sm, state.auth, params->tree_height*params->n);
  583. if (idx < (1U << params->tree_height) - 1) {
  584. bds_round(params, &state, idx, sk_seed, pub_seed, ots_addr);
  585. bds_treehash_update(params, &state, (params->tree_height - params->bds_k) >> 1, sk_seed, pub_seed, ots_addr);
  586. }
  587. sm += params->tree_height*params->n;
  588. *smlen += params->tree_height*params->n;
  589. memcpy(sm, m, mlen);
  590. *smlen += mlen;
  591. /* Write the updated BDS state back into sk. */
  592. xmss_serialize_state(params, sk, &state);
  593. return 0;
  594. }
  595. /*
  596. * Generates a XMSSMT key pair for a given parameter set.
  597. * Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || root || PUB_SEED]
  598. * Format pk: [root || PUB_SEED] omitting algo oid.
  599. */
  600. int xmssmt_core_keypair(const xmss_params *params,
  601. unsigned char *pk, unsigned char *sk)
  602. {
  603. uint32_t addr[8] = {0};
  604. unsigned int i;
  605. unsigned char *wots_sigs;
  606. // TODO refactor BDS state not to need separate treehash instances
  607. bds_state states[2*params->d - 1];
  608. treehash_inst treehash[(2*params->d - 1) * (params->tree_height - params->bds_k)];
  609. for (i = 0; i < 2*params->d - 1; i++) {
  610. states[i].treehash = treehash + i * (params->tree_height - params->bds_k);
  611. }
  612. xmssmt_deserialize_state(params, states, &wots_sigs, sk);
  613. for (i = 0; i < 2 * params->d - 1; i++) {
  614. states[i].stackoffset = 0;
  615. states[i].next_leaf = 0;
  616. }
  617. // Set idx = 0
  618. for (i = 0; i < params->index_bytes; i++) {
  619. sk[i] = 0;
  620. }
  621. // Init SK_SEED (params->n byte) and SK_PRF (params->n byte)
  622. randombytes(sk+params->index_bytes, 2*params->n);
  623. // Init PUB_SEED (params->n byte)
  624. randombytes(sk+params->index_bytes + 3*params->n, params->n);
  625. // Copy PUB_SEED to public key
  626. memcpy(pk+params->n, sk+params->index_bytes+3*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. wots_sign(params, wots_sigs + i*params->wots_sig_bytes, pk, sk + params->index_bytes, pk+params->n, addr);
  635. }
  636. // Address now points to the single tree on layer d-1
  637. treehash_init(params, pk, params->tree_height, 0, states + i, sk+params->index_bytes, pk+params->n, addr);
  638. memcpy(sk + params->index_bytes + 2*params->n, pk, params->n);
  639. xmssmt_serialize_state(params, sk, states);
  640. return 0;
  641. }
  642. /**
  643. * Signs a message.
  644. * Returns
  645. * 1. an array containing the signature followed by the message AND
  646. * 2. an updated secret key!
  647. *
  648. */
  649. int xmssmt_core_sign(const xmss_params *params,
  650. unsigned char *sk,
  651. unsigned char *sm, unsigned long long *smlen,
  652. const unsigned char *m, unsigned long long mlen)
  653. {
  654. const unsigned char *pub_root = sk + params->index_bytes + 2*params->n;
  655. uint64_t idx_tree;
  656. uint32_t idx_leaf;
  657. uint64_t i, j;
  658. int needswap_upto = -1;
  659. unsigned int updates;
  660. unsigned char sk_seed[params->n];
  661. unsigned char sk_prf[params->n];
  662. unsigned char pub_seed[params->n];
  663. // Init working params
  664. unsigned char R[params->n];
  665. unsigned char msg_h[params->n];
  666. uint32_t addr[8] = {0};
  667. uint32_t ots_addr[8] = {0};
  668. unsigned char idx_bytes_32[32];
  669. unsigned char *wots_sigs;
  670. // TODO refactor BDS state not to need separate treehash instances
  671. bds_state states[2*params->d - 1];
  672. treehash_inst treehash[(2*params->d - 1) * (params->tree_height - params->bds_k)];
  673. for (i = 0; i < 2*params->d - 1; i++) {
  674. states[i].treehash = treehash + i * (params->tree_height - params->bds_k);
  675. }
  676. xmssmt_deserialize_state(params, states, &wots_sigs, sk);
  677. // Extract SK
  678. unsigned long long idx = 0;
  679. for (i = 0; i < params->index_bytes; i++) {
  680. idx |= ((unsigned long long)sk[i]) << 8*(params->index_bytes - 1 - i);
  681. }
  682. memcpy(sk_seed, sk+params->index_bytes, params->n);
  683. memcpy(sk_prf, sk+params->index_bytes+params->n, params->n);
  684. memcpy(pub_seed, sk+params->index_bytes+3*params->n, params->n);
  685. // Update SK
  686. for (i = 0; i < params->index_bytes; i++) {
  687. sk[i] = ((idx + 1) >> 8*(params->index_bytes - 1 - i)) & 255;
  688. }
  689. // Secret key for this non-forward-secure version is now updated.
  690. // A production implementation should consider using a file handle instead,
  691. // and write the updated secret key at this point!
  692. // ---------------------------------
  693. // Message Hashing
  694. // ---------------------------------
  695. // Message Hash:
  696. // First compute pseudorandom value
  697. ull_to_bytes(idx_bytes_32, 32, idx);
  698. prf(params, R, idx_bytes_32, sk_prf);
  699. /* Already put the message in the right place, to make it easier to prepend
  700. * things when computing the hash over the message. */
  701. memcpy(sm + params->sig_bytes, m, mlen);
  702. /* Compute the message hash. */
  703. hash_message(params, msg_h, R, pub_root, idx,
  704. sm + params->sig_bytes - 4*params->n, mlen);
  705. // Start collecting signature
  706. *smlen = 0;
  707. // Copy index to signature
  708. for (i = 0; i < params->index_bytes; i++) {
  709. sm[i] = (idx >> 8*(params->index_bytes - 1 - i)) & 255;
  710. }
  711. sm += params->index_bytes;
  712. *smlen += params->index_bytes;
  713. // Copy R to signature
  714. for (i = 0; i < params->n; i++) {
  715. sm[i] = R[i];
  716. }
  717. sm += params->n;
  718. *smlen += params->n;
  719. // ----------------------------------
  720. // Now we start to "really sign"
  721. // ----------------------------------
  722. // Handle lowest layer separately as it is slightly different...
  723. // Prepare Address
  724. set_type(ots_addr, 0);
  725. idx_tree = idx >> params->tree_height;
  726. idx_leaf = (idx & ((1 << params->tree_height)-1));
  727. set_layer_addr(ots_addr, 0);
  728. set_tree_addr(ots_addr, idx_tree);
  729. set_ots_addr(ots_addr, idx_leaf);
  730. // Compute WOTS signature
  731. wots_sign(params, sm, msg_h, sk_seed, pub_seed, ots_addr);
  732. sm += params->wots_sig_bytes;
  733. *smlen += params->wots_sig_bytes;
  734. memcpy(sm, states[0].auth, params->tree_height*params->n);
  735. sm += params->tree_height*params->n;
  736. *smlen += params->tree_height*params->n;
  737. // prepare signature of remaining layers
  738. for (i = 1; i < params->d; i++) {
  739. // put WOTS signature in place
  740. memcpy(sm, wots_sigs + (i-1)*params->wots_sig_bytes, params->wots_sig_bytes);
  741. sm += params->wots_sig_bytes;
  742. *smlen += params->wots_sig_bytes;
  743. // put AUTH nodes in place
  744. memcpy(sm, states[i].auth, params->tree_height*params->n);
  745. sm += params->tree_height*params->n;
  746. *smlen += params->tree_height*params->n;
  747. }
  748. updates = (params->tree_height - params->bds_k) >> 1;
  749. set_tree_addr(addr, (idx_tree + 1));
  750. // mandatory update for NEXT_0 (does not count towards h-k/2) if NEXT_0 exists
  751. if ((1 + idx_tree) * (1 << params->tree_height) + idx_leaf < (1ULL << params->full_height)) {
  752. bds_state_update(params, &states[params->d], sk_seed, pub_seed, addr);
  753. }
  754. for (i = 0; i < params->d; i++) {
  755. // check if we're not at the end of a tree
  756. if (! (((idx + 1) & ((1ULL << ((i+1)*params->tree_height)) - 1)) == 0)) {
  757. idx_leaf = (idx >> (params->tree_height * i)) & ((1 << params->tree_height)-1);
  758. idx_tree = (idx >> (params->tree_height * (i+1)));
  759. set_layer_addr(addr, i);
  760. set_tree_addr(addr, idx_tree);
  761. if (i == (unsigned int) (needswap_upto + 1)) {
  762. bds_round(params, &states[i], idx_leaf, sk_seed, pub_seed, addr);
  763. }
  764. updates = bds_treehash_update(params, &states[i], updates, sk_seed, pub_seed, addr);
  765. set_tree_addr(addr, (idx_tree + 1));
  766. // if a NEXT-tree exists for this level;
  767. if ((1 + idx_tree) * (1 << params->tree_height) + idx_leaf < (1ULL << (params->full_height - params->tree_height * i))) {
  768. if (i > 0 && updates > 0 && states[params->d + i].next_leaf < (1ULL << params->full_height)) {
  769. bds_state_update(params, &states[params->d + i], sk_seed, pub_seed, addr);
  770. updates--;
  771. }
  772. }
  773. }
  774. else if (idx < (1ULL << params->full_height) - 1) {
  775. deep_state_swap(params, states+params->d + i, states + i);
  776. set_layer_addr(ots_addr, (i+1));
  777. set_tree_addr(ots_addr, ((idx + 1) >> ((i+2) * params->tree_height)));
  778. set_ots_addr(ots_addr, (((idx >> ((i+1) * params->tree_height)) + 1) & ((1 << params->tree_height)-1)));
  779. wots_sign(params, wots_sigs + i*params->wots_sig_bytes, states[i].stack, sk_seed, pub_seed, ots_addr);
  780. states[params->d + i].stackoffset = 0;
  781. states[params->d + i].next_leaf = 0;
  782. updates--; // WOTS-signing counts as one update
  783. needswap_upto = i;
  784. for (j = 0; j < params->tree_height-params->bds_k; j++) {
  785. states[i].treehash[j].completed = 1;
  786. }
  787. }
  788. }
  789. memcpy(sm, m, mlen);
  790. *smlen += mlen;
  791. xmssmt_serialize_state(params, sk, states);
  792. return 0;
  793. }