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