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.

705 lines
25 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_fast.h"
  11. /**
  12. * Initialize BDS state struct
  13. * parameter names are the same as used in the description of the BDS traversal
  14. */
  15. void xmss_set_bds_state(bds_state *state, unsigned char *stack,
  16. int stackoffset, unsigned char *stacklevels,
  17. unsigned char *auth, unsigned char *keep,
  18. treehash_inst *treehash, unsigned char *retain,
  19. int next_leaf)
  20. {
  21. state->stack = stack;
  22. state->stackoffset = stackoffset;
  23. state->stacklevels = stacklevels;
  24. state->auth = auth;
  25. state->keep = keep;
  26. state->treehash = treehash;
  27. state->retain = retain;
  28. state->next_leaf = next_leaf;
  29. }
  30. static int treehash_minheight_on_stack(const xmss_params *params,
  31. bds_state* state,
  32. const treehash_inst *treehash)
  33. {
  34. unsigned int r = params->tree_height, i;
  35. for (i = 0; i < treehash->stackusage; i++) {
  36. if (state->stacklevels[state->stackoffset - i - 1] < r) {
  37. r = state->stacklevels[state->stackoffset - i - 1];
  38. }
  39. }
  40. return r;
  41. }
  42. /**
  43. * Merkle's TreeHash algorithm. The address only needs to initialize the first 78 bits of addr. Everything else will be set by treehash.
  44. * Currently only used for key generation.
  45. *
  46. */
  47. static void treehash_init(const xmss_params *params,
  48. unsigned char *node, int height, int index,
  49. bds_state *state, const unsigned char *sk_seed,
  50. const unsigned char *pub_seed, const uint32_t addr[8])
  51. {
  52. unsigned int idx = index;
  53. // use three different addresses because at this point we use all three formats in parallel
  54. uint32_t ots_addr[8];
  55. uint32_t ltree_addr[8];
  56. uint32_t node_addr[8];
  57. // only copy layer and tree address parts
  58. memcpy(ots_addr, addr, 12);
  59. // type = ots
  60. set_type(ots_addr, 0);
  61. memcpy(ltree_addr, addr, 12);
  62. set_type(ltree_addr, 1);
  63. memcpy(node_addr, addr, 12);
  64. set_type(node_addr, 2);
  65. uint32_t lastnode, i;
  66. unsigned char stack[(height+1)*params->n];
  67. unsigned int stacklevels[height+1];
  68. unsigned int stackoffset=0;
  69. unsigned int nodeh;
  70. lastnode = idx+(1<<height);
  71. for (i = 0; i < params->tree_height-params->bds_k; i++) {
  72. state->treehash[i].h = i;
  73. state->treehash[i].completed = 1;
  74. state->treehash[i].stackusage = 0;
  75. }
  76. i = 0;
  77. for (; idx < lastnode; idx++) {
  78. set_ltree_addr(ltree_addr, idx);
  79. set_ots_addr(ots_addr, idx);
  80. gen_leaf_wots(params, stack+stackoffset*params->n, sk_seed, pub_seed, ltree_addr, ots_addr);
  81. stacklevels[stackoffset] = 0;
  82. stackoffset++;
  83. if (params->tree_height - params->bds_k > 0 && i == 3) {
  84. memcpy(state->treehash[0].node, stack+stackoffset*params->n, params->n);
  85. }
  86. while (stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2]) {
  87. nodeh = stacklevels[stackoffset-1];
  88. if (i >> nodeh == 1) {
  89. memcpy(state->auth + nodeh*params->n, stack+(stackoffset-1)*params->n, params->n);
  90. }
  91. else {
  92. if (nodeh < params->tree_height - params->bds_k && i >> nodeh == 3) {
  93. memcpy(state->treehash[nodeh].node, stack+(stackoffset-1)*params->n, params->n);
  94. }
  95. else if (nodeh >= params->tree_height - params->bds_k) {
  96. 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);
  97. }
  98. }
  99. set_tree_height(node_addr, stacklevels[stackoffset-1]);
  100. set_tree_index(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
  101. hash_h(params, stack+(stackoffset-2)*params->n, stack+(stackoffset-2)*params->n, pub_seed, node_addr);
  102. stacklevels[stackoffset-2]++;
  103. stackoffset--;
  104. }
  105. i++;
  106. }
  107. for (i = 0; i < params->n; i++) {
  108. node[i] = stack[i];
  109. }
  110. }
  111. static void treehash_update(const xmss_params *params,
  112. treehash_inst *treehash, bds_state *state,
  113. const unsigned char *sk_seed,
  114. const unsigned char *pub_seed,
  115. const uint32_t addr[8])
  116. {
  117. uint32_t ots_addr[8];
  118. uint32_t ltree_addr[8];
  119. uint32_t node_addr[8];
  120. // only copy layer and tree address parts
  121. memcpy(ots_addr, addr, 12);
  122. // type = ots
  123. set_type(ots_addr, 0);
  124. memcpy(ltree_addr, addr, 12);
  125. set_type(ltree_addr, 1);
  126. memcpy(node_addr, addr, 12);
  127. set_type(node_addr, 2);
  128. set_ltree_addr(ltree_addr, treehash->next_idx);
  129. set_ots_addr(ots_addr, treehash->next_idx);
  130. unsigned char nodebuffer[2 * params->n];
  131. unsigned int nodeheight = 0;
  132. gen_leaf_wots(params, nodebuffer, sk_seed, pub_seed, ltree_addr, ots_addr);
  133. while (treehash->stackusage > 0 && state->stacklevels[state->stackoffset-1] == nodeheight) {
  134. memcpy(nodebuffer + params->n, nodebuffer, params->n);
  135. memcpy(nodebuffer, state->stack + (state->stackoffset-1)*params->n, params->n);
  136. set_tree_height(node_addr, nodeheight);
  137. set_tree_index(node_addr, (treehash->next_idx >> (nodeheight+1)));
  138. hash_h(params, nodebuffer, nodebuffer, pub_seed, node_addr);
  139. nodeheight++;
  140. treehash->stackusage--;
  141. state->stackoffset--;
  142. }
  143. if (nodeheight == treehash->h) { // this also implies stackusage == 0
  144. memcpy(treehash->node, nodebuffer, params->n);
  145. treehash->completed = 1;
  146. }
  147. else {
  148. memcpy(state->stack + state->stackoffset*params->n, nodebuffer, params->n);
  149. treehash->stackusage++;
  150. state->stacklevels[state->stackoffset] = nodeheight;
  151. state->stackoffset++;
  152. treehash->next_idx++;
  153. }
  154. }
  155. /**
  156. * Performs one treehash update on the instance that needs it the most.
  157. * Returns 1 if such an instance was not found
  158. **/
  159. static char bds_treehash_update(const xmss_params *params,
  160. bds_state *state, unsigned int updates,
  161. const unsigned char *sk_seed,
  162. unsigned char *pub_seed,
  163. const uint32_t addr[8])
  164. {
  165. uint32_t i, j;
  166. unsigned int level, l_min, low;
  167. unsigned int used = 0;
  168. for (j = 0; j < updates; j++) {
  169. l_min = params->tree_height;
  170. level = params->tree_height - params->bds_k;
  171. for (i = 0; i < params->tree_height - params->bds_k; i++) {
  172. if (state->treehash[i].completed) {
  173. low = params->tree_height;
  174. }
  175. else if (state->treehash[i].stackusage == 0) {
  176. low = i;
  177. }
  178. else {
  179. low = treehash_minheight_on_stack(params, state, &(state->treehash[i]));
  180. }
  181. if (low < l_min) {
  182. level = i;
  183. l_min = low;
  184. }
  185. }
  186. if (level == params->tree_height - params->bds_k) {
  187. break;
  188. }
  189. treehash_update(params, &(state->treehash[level]), state, sk_seed, pub_seed, addr);
  190. used++;
  191. }
  192. return updates - used;
  193. }
  194. /**
  195. * Updates the state (typically NEXT_i) by adding a leaf and updating the stack
  196. * Returns 1 if all leaf nodes have already been processed
  197. **/
  198. static char bds_state_update(const xmss_params *params,
  199. bds_state *state, const unsigned char *sk_seed,
  200. const unsigned char *pub_seed,
  201. const uint32_t addr[8])
  202. {
  203. uint32_t ltree_addr[8];
  204. uint32_t node_addr[8];
  205. uint32_t ots_addr[8];
  206. unsigned int nodeh;
  207. int idx = state->next_leaf;
  208. if (idx == 1 << params->tree_height) {
  209. return 1;
  210. }
  211. // only copy layer and tree address parts
  212. memcpy(ots_addr, addr, 12);
  213. // type = ots
  214. set_type(ots_addr, 0);
  215. memcpy(ltree_addr, addr, 12);
  216. set_type(ltree_addr, 1);
  217. memcpy(node_addr, addr, 12);
  218. set_type(node_addr, 2);
  219. set_ots_addr(ots_addr, idx);
  220. set_ltree_addr(ltree_addr, idx);
  221. gen_leaf_wots(params, state->stack+state->stackoffset*params->n, sk_seed, pub_seed, ltree_addr, ots_addr);
  222. state->stacklevels[state->stackoffset] = 0;
  223. state->stackoffset++;
  224. if (params->tree_height - params->bds_k > 0 && idx == 3) {
  225. memcpy(state->treehash[0].node, state->stack+state->stackoffset*params->n, params->n);
  226. }
  227. while (state->stackoffset>1 && state->stacklevels[state->stackoffset-1] == state->stacklevels[state->stackoffset-2]) {
  228. nodeh = state->stacklevels[state->stackoffset-1];
  229. if (idx >> nodeh == 1) {
  230. memcpy(state->auth + nodeh*params->n, state->stack+(state->stackoffset-1)*params->n, params->n);
  231. }
  232. else {
  233. if (nodeh < params->tree_height - params->bds_k && idx >> nodeh == 3) {
  234. memcpy(state->treehash[nodeh].node, state->stack+(state->stackoffset-1)*params->n, params->n);
  235. }
  236. else if (nodeh >= params->tree_height - params->bds_k) {
  237. 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);
  238. }
  239. }
  240. set_tree_height(node_addr, state->stacklevels[state->stackoffset-1]);
  241. set_tree_index(node_addr, (idx >> (state->stacklevels[state->stackoffset-1]+1)));
  242. hash_h(params, state->stack+(state->stackoffset-2)*params->n, state->stack+(state->stackoffset-2)*params->n, pub_seed, node_addr);
  243. state->stacklevels[state->stackoffset-2]++;
  244. state->stackoffset--;
  245. }
  246. state->next_leaf++;
  247. return 0;
  248. }
  249. /**
  250. * Returns the auth path for node leaf_idx and computes the auth path for the
  251. * next leaf node, using the algorithm described by Buchmann, Dahmen and Szydlo
  252. * in "Post Quantum Cryptography", Springer 2009.
  253. */
  254. static void bds_round(const xmss_params *params,
  255. bds_state *state, const unsigned long leaf_idx,
  256. const unsigned char *sk_seed,
  257. const unsigned char *pub_seed, uint32_t addr[8])
  258. {
  259. unsigned int i;
  260. unsigned int tau = params->tree_height;
  261. unsigned int startidx;
  262. unsigned int offset, rowidx;
  263. unsigned char buf[2 * params->n];
  264. uint32_t ots_addr[8];
  265. uint32_t ltree_addr[8];
  266. uint32_t node_addr[8];
  267. // only copy layer and tree address parts
  268. memcpy(ots_addr, addr, 12);
  269. // type = ots
  270. set_type(ots_addr, 0);
  271. memcpy(ltree_addr, addr, 12);
  272. set_type(ltree_addr, 1);
  273. memcpy(node_addr, addr, 12);
  274. set_type(node_addr, 2);
  275. for (i = 0; i < params->tree_height; i++) {
  276. if (! ((leaf_idx >> i) & 1)) {
  277. tau = i;
  278. break;
  279. }
  280. }
  281. if (tau > 0) {
  282. memcpy(buf, state->auth + (tau-1) * params->n, params->n);
  283. // we need to do this before refreshing state->keep to prevent overwriting
  284. memcpy(buf + params->n, state->keep + ((tau-1) >> 1) * params->n, params->n);
  285. }
  286. if (!((leaf_idx >> (tau + 1)) & 1) && (tau < params->tree_height - 1)) {
  287. memcpy(state->keep + (tau >> 1)*params->n, state->auth + tau*params->n, params->n);
  288. }
  289. if (tau == 0) {
  290. set_ltree_addr(ltree_addr, leaf_idx);
  291. set_ots_addr(ots_addr, leaf_idx);
  292. gen_leaf_wots(params, state->auth, sk_seed, pub_seed, ltree_addr, ots_addr);
  293. }
  294. else {
  295. set_tree_height(node_addr, (tau-1));
  296. set_tree_index(node_addr, leaf_idx >> tau);
  297. hash_h(params, state->auth + tau * params->n, buf, pub_seed, node_addr);
  298. for (i = 0; i < tau; i++) {
  299. if (i < params->tree_height - params->bds_k) {
  300. memcpy(state->auth + i * params->n, state->treehash[i].node, params->n);
  301. }
  302. else {
  303. offset = (1 << (params->tree_height - 1 - i)) + i - params->tree_height;
  304. rowidx = ((leaf_idx >> i) - 1) >> 1;
  305. memcpy(state->auth + i * params->n, state->retain + (offset + rowidx) * params->n, params->n);
  306. }
  307. }
  308. for (i = 0; i < ((tau < params->tree_height - params->bds_k) ? tau : (params->tree_height - params->bds_k)); i++) {
  309. startidx = leaf_idx + 1 + 3 * (1 << i);
  310. if (startidx < 1U << params->tree_height) {
  311. state->treehash[i].h = i;
  312. state->treehash[i].next_idx = startidx;
  313. state->treehash[i].completed = 0;
  314. state->treehash[i].stackusage = 0;
  315. }
  316. }
  317. }
  318. }
  319. /*
  320. * Generates a XMSS key pair for a given parameter set.
  321. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
  322. * Format pk: [root || PUB_SEED] omitting algo oid.
  323. */
  324. int xmss_core_keypair(const xmss_params *params,
  325. unsigned char *pk, unsigned char *sk, bds_state *state)
  326. {
  327. uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  328. // Set idx = 0
  329. sk[0] = 0;
  330. sk[1] = 0;
  331. sk[2] = 0;
  332. sk[3] = 0;
  333. // Init SK_SEED (n byte), SK_PRF (n byte), and PUB_SEED (n byte)
  334. randombytes(sk + params->index_len, 3*params->n);
  335. // Copy PUB_SEED to public key
  336. memcpy(pk + params->n, sk + params->index_len + 2*params->n, params->n);
  337. // Compute root
  338. treehash_init(params, pk, params->tree_height, 0, state, sk + params->index_len, sk + params->index_len + 2*params->n, addr);
  339. // copy root o sk
  340. memcpy(sk + params->index_len + 3*params->n, pk, params->n);
  341. return 0;
  342. }
  343. /**
  344. * Signs a message.
  345. * Returns
  346. * 1. an array containing the signature followed by the message AND
  347. * 2. an updated secret key!
  348. *
  349. */
  350. int xmss_core_sign(const xmss_params *params,
  351. unsigned char *sk, bds_state *state,
  352. unsigned char *sm, unsigned long long *smlen,
  353. const unsigned char *m, unsigned long long mlen)
  354. {
  355. uint16_t i = 0;
  356. // Extract SK
  357. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  358. unsigned char sk_seed[params->n];
  359. memcpy(sk_seed, sk + params->index_len, params->n);
  360. unsigned char sk_prf[params->n];
  361. memcpy(sk_prf, sk + params->index_len + params->n, params->n);
  362. unsigned char pub_seed[params->n];
  363. memcpy(pub_seed, sk + params->index_len + 2*params->n, params->n);
  364. // index as 32 bytes string
  365. unsigned char idx_bytes_32[32];
  366. to_byte(idx_bytes_32, idx, 32);
  367. unsigned char hash_key[3*params->n];
  368. // Update SK
  369. sk[0] = ((idx + 1) >> 24) & 255;
  370. sk[1] = ((idx + 1) >> 16) & 255;
  371. sk[2] = ((idx + 1) >> 8) & 255;
  372. sk[3] = (idx + 1) & 255;
  373. // -- Secret key for this non-forward-secure version is now updated.
  374. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  375. // Init working params
  376. unsigned char R[params->n];
  377. unsigned char msg_h[params->n];
  378. unsigned char ots_seed[params->n];
  379. uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  380. // ---------------------------------
  381. // Message Hashing
  382. // ---------------------------------
  383. // Message Hash:
  384. // First compute pseudorandom value
  385. prf(params, R, idx_bytes_32, sk_prf, params->n);
  386. // Generate hash key (R || root || idx)
  387. memcpy(hash_key, R, params->n);
  388. memcpy(hash_key+params->n, sk+4+3*params->n, params->n);
  389. to_byte(hash_key+2*params->n, idx, params->n);
  390. // Then use it for message digest
  391. h_msg(params, msg_h, m, mlen, hash_key, 3*params->n);
  392. // Start collecting signature
  393. *smlen = 0;
  394. // Copy index to signature
  395. sm[0] = (idx >> 24) & 255;
  396. sm[1] = (idx >> 16) & 255;
  397. sm[2] = (idx >> 8) & 255;
  398. sm[3] = idx & 255;
  399. sm += 4;
  400. *smlen += 4;
  401. // Copy R to signature
  402. for (i = 0; i < params->n; i++) {
  403. sm[i] = R[i];
  404. }
  405. sm += params->n;
  406. *smlen += params->n;
  407. // ----------------------------------
  408. // Now we start to "really sign"
  409. // ----------------------------------
  410. // Prepare Address
  411. set_type(ots_addr, 0);
  412. set_ots_addr(ots_addr, idx);
  413. // Compute seed for OTS key pair
  414. get_seed(params, ots_seed, sk_seed, ots_addr);
  415. // Compute WOTS signature
  416. wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
  417. sm += params->wots_keysize;
  418. *smlen += params->wots_keysize;
  419. // the auth path was already computed during the previous round
  420. memcpy(sm, state->auth, params->tree_height*params->n);
  421. if (idx < (1U << params->tree_height) - 1) {
  422. bds_round(params, state, idx, sk_seed, pub_seed, ots_addr);
  423. bds_treehash_update(params, state, (params->tree_height - params->bds_k) >> 1, sk_seed, pub_seed, ots_addr);
  424. }
  425. sm += params->tree_height*params->n;
  426. *smlen += params->tree_height*params->n;
  427. memcpy(sm, m, mlen);
  428. *smlen += mlen;
  429. return 0;
  430. }
  431. /*
  432. * Generates a XMSSMT key pair for a given parameter set.
  433. * Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
  434. * Format pk: [root || PUB_SEED] omitting algo oid.
  435. */
  436. int xmssmt_core_keypair(const xmss_params *params,
  437. unsigned char *pk, unsigned char *sk,
  438. bds_state *states, unsigned char *wots_sigs)
  439. {
  440. unsigned char ots_seed[params->n];
  441. uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  442. unsigned int i;
  443. // Set idx = 0
  444. for (i = 0; i < params->index_len; i++) {
  445. sk[i] = 0;
  446. }
  447. // Init SK_SEED (params->n byte), SK_PRF (params->n byte), and PUB_SEED (params->n byte)
  448. randombytes(sk+params->index_len, 3*params->n);
  449. // Copy PUB_SEED to public key
  450. memcpy(pk+params->n, sk+params->index_len+2*params->n, params->n);
  451. // Start with the bottom-most layer
  452. set_layer_addr(addr, 0);
  453. // Set up state and compute wots signatures for all but topmost tree root
  454. for (i = 0; i < params->d - 1; i++) {
  455. // Compute seed for OTS key pair
  456. treehash_init(params, pk, params->tree_height, 0, states + i, sk+params->index_len, pk+params->n, addr);
  457. set_layer_addr(addr, (i+1));
  458. get_seed(params, ots_seed, sk + params->index_len, addr);
  459. wots_sign(params, wots_sigs + i*params->wots_keysize, pk, ots_seed, pk+params->n, addr);
  460. }
  461. // Address now points to the single tree on layer d-1
  462. treehash_init(params, pk, params->tree_height, 0, states + i, sk+params->index_len, pk+params->n, addr);
  463. memcpy(sk + params->index_len + 3*params->n, pk, params->n);
  464. return 0;
  465. }
  466. /**
  467. * Signs a message.
  468. * Returns
  469. * 1. an array containing the signature followed by the message AND
  470. * 2. an updated secret key!
  471. *
  472. */
  473. int xmssmt_core_sign(const xmss_params *params,
  474. unsigned char *sk,
  475. bds_state *states, unsigned char *wots_sigs,
  476. unsigned char *sm, unsigned long long *smlen,
  477. const unsigned char *m, unsigned long long mlen)
  478. {
  479. uint64_t idx_tree;
  480. uint32_t idx_leaf;
  481. uint64_t i, j;
  482. int needswap_upto = -1;
  483. unsigned int updates;
  484. unsigned char sk_seed[params->n];
  485. unsigned char sk_prf[params->n];
  486. unsigned char pub_seed[params->n];
  487. // Init working params
  488. unsigned char R[params->n];
  489. unsigned char msg_h[params->n];
  490. unsigned char hash_key[3*params->n];
  491. unsigned char ots_seed[params->n];
  492. uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  493. uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  494. unsigned char idx_bytes_32[32];
  495. bds_state tmp;
  496. // Extract SK
  497. unsigned long long idx = 0;
  498. for (i = 0; i < params->index_len; i++) {
  499. idx |= ((unsigned long long)sk[i]) << 8*(params->index_len - 1 - i);
  500. }
  501. memcpy(sk_seed, sk+params->index_len, params->n);
  502. memcpy(sk_prf, sk+params->index_len+params->n, params->n);
  503. memcpy(pub_seed, sk+params->index_len+2*params->n, params->n);
  504. // Update SK
  505. for (i = 0; i < params->index_len; i++) {
  506. sk[i] = ((idx + 1) >> 8*(params->index_len - 1 - i)) & 255;
  507. }
  508. // -- Secret key for this non-forward-secure version is now updated.
  509. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  510. // ---------------------------------
  511. // Message Hashing
  512. // ---------------------------------
  513. // Message Hash:
  514. // First compute pseudorandom value
  515. to_byte(idx_bytes_32, idx, 32);
  516. prf(params, R, idx_bytes_32, sk_prf, params->n);
  517. // Generate hash key (R || root || idx)
  518. memcpy(hash_key, R, params->n);
  519. memcpy(hash_key+params->n, sk+params->index_len+3*params->n, params->n);
  520. to_byte(hash_key+2*params->n, idx, params->n);
  521. // Then use it for message digest
  522. h_msg(params, msg_h, m, mlen, hash_key, 3*params->n);
  523. // Start collecting signature
  524. *smlen = 0;
  525. // Copy index to signature
  526. for (i = 0; i < params->index_len; i++) {
  527. sm[i] = (idx >> 8*(params->index_len - 1 - i)) & 255;
  528. }
  529. sm += params->index_len;
  530. *smlen += params->index_len;
  531. // Copy R to signature
  532. for (i = 0; i < params->n; i++) {
  533. sm[i] = R[i];
  534. }
  535. sm += params->n;
  536. *smlen += params->n;
  537. // ----------------------------------
  538. // Now we start to "really sign"
  539. // ----------------------------------
  540. // Handle lowest layer separately as it is slightly different...
  541. // Prepare Address
  542. set_type(ots_addr, 0);
  543. idx_tree = idx >> params->tree_height;
  544. idx_leaf = (idx & ((1 << params->tree_height)-1));
  545. set_layer_addr(ots_addr, 0);
  546. set_tree_addr(ots_addr, idx_tree);
  547. set_ots_addr(ots_addr, idx_leaf);
  548. // Compute seed for OTS key pair
  549. get_seed(params, ots_seed, sk_seed, ots_addr);
  550. // Compute WOTS signature
  551. wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
  552. sm += params->wots_keysize;
  553. *smlen += params->wots_keysize;
  554. memcpy(sm, states[0].auth, params->tree_height*params->n);
  555. sm += params->tree_height*params->n;
  556. *smlen += params->tree_height*params->n;
  557. // prepare signature of remaining layers
  558. for (i = 1; i < params->d; i++) {
  559. // put WOTS signature in place
  560. memcpy(sm, wots_sigs + (i-1)*params->wots_keysize, params->wots_keysize);
  561. sm += params->wots_keysize;
  562. *smlen += params->wots_keysize;
  563. // put AUTH nodes in place
  564. memcpy(sm, states[i].auth, params->tree_height*params->n);
  565. sm += params->tree_height*params->n;
  566. *smlen += params->tree_height*params->n;
  567. }
  568. updates = (params->tree_height - params->bds_k) >> 1;
  569. set_tree_addr(addr, (idx_tree + 1));
  570. // mandatory update for NEXT_0 (does not count towards h-k/2) if NEXT_0 exists
  571. if ((1 + idx_tree) * (1 << params->tree_height) + idx_leaf < (1ULL << params->full_height)) {
  572. bds_state_update(params, &states[params->d], sk_seed, pub_seed, addr);
  573. }
  574. for (i = 0; i < params->d; i++) {
  575. // check if we're not at the end of a tree
  576. if (! (((idx + 1) & ((1ULL << ((i+1)*params->tree_height)) - 1)) == 0)) {
  577. idx_leaf = (idx >> (params->tree_height * i)) & ((1 << params->tree_height)-1);
  578. idx_tree = (idx >> (params->tree_height * (i+1)));
  579. set_layer_addr(addr, i);
  580. set_tree_addr(addr, idx_tree);
  581. if (i == (unsigned int) (needswap_upto + 1)) {
  582. bds_round(params, &states[i], idx_leaf, sk_seed, pub_seed, addr);
  583. }
  584. updates = bds_treehash_update(params, &states[i], updates, sk_seed, pub_seed, addr);
  585. set_tree_addr(addr, (idx_tree + 1));
  586. // if a NEXT-tree exists for this level;
  587. if ((1 + idx_tree) * (1 << params->tree_height) + idx_leaf < (1ULL << (params->full_height - params->tree_height * i))) {
  588. if (i > 0 && updates > 0 && states[params->d + i].next_leaf < (1ULL << params->full_height)) {
  589. bds_state_update(params, &states[params->d + i], sk_seed, pub_seed, addr);
  590. updates--;
  591. }
  592. }
  593. }
  594. else if (idx < (1ULL << params->full_height) - 1) {
  595. memcpy(&tmp, states+params->d + i, sizeof(bds_state));
  596. memcpy(states+params->d + i, states + i, sizeof(bds_state));
  597. memcpy(states + i, &tmp, sizeof(bds_state));
  598. set_layer_addr(ots_addr, (i+1));
  599. set_tree_addr(ots_addr, ((idx + 1) >> ((i+2) * params->tree_height)));
  600. set_ots_addr(ots_addr, (((idx >> ((i+1) * params->tree_height)) + 1) & ((1 << params->tree_height)-1)));
  601. get_seed(params, ots_seed, sk+params->index_len, ots_addr);
  602. wots_sign(params, wots_sigs + i*params->wots_keysize, states[i].stack, ots_seed, pub_seed, ots_addr);
  603. states[params->d + i].stackoffset = 0;
  604. states[params->d + i].next_leaf = 0;
  605. updates--; // WOTS-signing counts as one update
  606. needswap_upto = i;
  607. for (j = 0; j < params->tree_height-params->bds_k; j++) {
  608. states[i].treehash[j].completed = 1;
  609. }
  610. }
  611. }
  612. memcpy(sm, m, mlen);
  613. *smlen += mlen;
  614. return 0;
  615. }