25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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