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.

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