選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

730 行
26 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. * Given a set of parameters, this function returns the size of the secret key.
  321. * This is implementation specific, as varying choices in tree traversal will
  322. * result in varying requirements for state storage.
  323. */
  324. unsigned long long xmss_core_sk_bytes(const xmss_params *params)
  325. {
  326. return params->index_bytes + 4 * params->n;
  327. }
  328. /*
  329. * Generates a XMSS key pair for a given parameter set.
  330. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
  331. * Format pk: [root || PUB_SEED] omitting algo oid.
  332. */
  333. int xmss_core_keypair(const xmss_params *params,
  334. unsigned char *pk, unsigned char *sk, bds_state *state)
  335. {
  336. uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  337. // Set idx = 0
  338. sk[0] = 0;
  339. sk[1] = 0;
  340. sk[2] = 0;
  341. sk[3] = 0;
  342. // Init SK_SEED (n byte), SK_PRF (n byte), and PUB_SEED (n byte)
  343. randombytes(sk + params->index_bytes, 3*params->n);
  344. // Copy PUB_SEED to public key
  345. memcpy(pk + params->n, sk + params->index_bytes + 2*params->n, params->n);
  346. // Compute root
  347. treehash_init(params, pk, params->tree_height, 0, state, sk + params->index_bytes, sk + params->index_bytes + 2*params->n, addr);
  348. // copy root o sk
  349. memcpy(sk + params->index_bytes + 3*params->n, pk, params->n);
  350. return 0;
  351. }
  352. /**
  353. * Signs a message.
  354. * Returns
  355. * 1. an array containing the signature followed by the message AND
  356. * 2. an updated secret key!
  357. *
  358. */
  359. int xmss_core_sign(const xmss_params *params,
  360. unsigned char *sk, bds_state *state,
  361. unsigned char *sm, unsigned long long *smlen,
  362. const unsigned char *m, unsigned long long mlen)
  363. {
  364. const unsigned char *pub_root = sk + params->index_bytes + 3*params->n;
  365. uint16_t i = 0;
  366. // Extract SK
  367. unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
  368. unsigned char sk_seed[params->n];
  369. memcpy(sk_seed, sk + params->index_bytes, params->n);
  370. unsigned char sk_prf[params->n];
  371. memcpy(sk_prf, sk + params->index_bytes + params->n, params->n);
  372. unsigned char pub_seed[params->n];
  373. memcpy(pub_seed, sk + params->index_bytes + 2*params->n, params->n);
  374. // index as 32 bytes string
  375. unsigned char idx_bytes_32[32];
  376. ull_to_bytes(idx_bytes_32, 32, idx);
  377. // Update SK
  378. sk[0] = ((idx + 1) >> 24) & 255;
  379. sk[1] = ((idx + 1) >> 16) & 255;
  380. sk[2] = ((idx + 1) >> 8) & 255;
  381. sk[3] = (idx + 1) & 255;
  382. // Secret key for this non-forward-secure version is now updated.
  383. // A production implementation should consider using a file handle instead,
  384. // and write the updated secret key at this point!
  385. // Init working params
  386. unsigned char R[params->n];
  387. unsigned char msg_h[params->n];
  388. unsigned char ots_seed[params->n];
  389. uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  390. // ---------------------------------
  391. // Message Hashing
  392. // ---------------------------------
  393. // Message Hash:
  394. // First compute pseudorandom value
  395. prf(params, R, idx_bytes_32, sk_prf);
  396. /* Already put the message in the right place, to make it easier to prepend
  397. * things when computing the hash over the message. */
  398. memcpy(sm + params->sig_bytes, m, mlen);
  399. /* Compute the message hash. */
  400. hash_message(params, msg_h, R, pub_root, idx,
  401. sm + params->sig_bytes - 4*params->n, mlen);
  402. // Start collecting signature
  403. *smlen = 0;
  404. // Copy index to signature
  405. sm[0] = (idx >> 24) & 255;
  406. sm[1] = (idx >> 16) & 255;
  407. sm[2] = (idx >> 8) & 255;
  408. sm[3] = idx & 255;
  409. sm += 4;
  410. *smlen += 4;
  411. // Copy R to signature
  412. for (i = 0; i < params->n; i++) {
  413. sm[i] = R[i];
  414. }
  415. sm += params->n;
  416. *smlen += params->n;
  417. // ----------------------------------
  418. // Now we start to "really sign"
  419. // ----------------------------------
  420. // Prepare Address
  421. set_type(ots_addr, 0);
  422. set_ots_addr(ots_addr, idx);
  423. // Compute seed for OTS key pair
  424. get_seed(params, ots_seed, sk_seed, ots_addr);
  425. // Compute WOTS signature
  426. wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
  427. sm += params->wots_sig_bytes;
  428. *smlen += params->wots_sig_bytes;
  429. // the auth path was already computed during the previous round
  430. memcpy(sm, state->auth, params->tree_height*params->n);
  431. if (idx < (1U << params->tree_height) - 1) {
  432. bds_round(params, state, idx, sk_seed, pub_seed, ots_addr);
  433. bds_treehash_update(params, state, (params->tree_height - params->bds_k) >> 1, sk_seed, pub_seed, ots_addr);
  434. }
  435. sm += params->tree_height*params->n;
  436. *smlen += params->tree_height*params->n;
  437. memcpy(sm, m, mlen);
  438. *smlen += mlen;
  439. return 0;
  440. }
  441. /**
  442. * Given a set of parameters, this function returns the size of the secret key.
  443. * This is implementation specific, as varying choices in tree traversal will
  444. * result in varying requirements for state storage.
  445. */
  446. unsigned long long xmssmt_core_sk_bytes(const xmss_params *params)
  447. {
  448. return params->index_bytes + 4 * params->n;
  449. }
  450. /*
  451. * Generates a XMSSMT key pair for a given parameter set.
  452. * Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
  453. * Format pk: [root || PUB_SEED] omitting algo oid.
  454. */
  455. int xmssmt_core_keypair(const xmss_params *params,
  456. unsigned char *pk, unsigned char *sk,
  457. bds_state *states, unsigned char *wots_sigs)
  458. {
  459. unsigned char ots_seed[params->n];
  460. uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  461. unsigned int i;
  462. // Set idx = 0
  463. for (i = 0; i < params->index_bytes; i++) {
  464. sk[i] = 0;
  465. }
  466. // Init SK_SEED (params->n byte), SK_PRF (params->n byte), and PUB_SEED (params->n byte)
  467. randombytes(sk+params->index_bytes, 3*params->n);
  468. // Copy PUB_SEED to public key
  469. memcpy(pk+params->n, sk+params->index_bytes+2*params->n, params->n);
  470. // Start with the bottom-most layer
  471. set_layer_addr(addr, 0);
  472. // Set up state and compute wots signatures for all but topmost tree root
  473. for (i = 0; i < params->d - 1; i++) {
  474. // Compute seed for OTS key pair
  475. treehash_init(params, pk, params->tree_height, 0, states + i, sk+params->index_bytes, pk+params->n, addr);
  476. set_layer_addr(addr, (i+1));
  477. get_seed(params, ots_seed, sk + params->index_bytes, addr);
  478. wots_sign(params, wots_sigs + i*params->wots_sig_bytes, pk, ots_seed, pk+params->n, addr);
  479. }
  480. // Address now points to the single tree on layer d-1
  481. treehash_init(params, pk, params->tree_height, 0, states + i, sk+params->index_bytes, pk+params->n, addr);
  482. memcpy(sk + params->index_bytes + 3*params->n, pk, params->n);
  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 xmssmt_core_sign(const xmss_params *params,
  493. unsigned char *sk,
  494. bds_state *states, unsigned char *wots_sigs,
  495. unsigned char *sm, unsigned long long *smlen,
  496. const unsigned char *m, unsigned long long mlen)
  497. {
  498. const unsigned char *pub_root = sk + params->index_bytes + 3*params->n;
  499. uint64_t idx_tree;
  500. uint32_t idx_leaf;
  501. uint64_t i, j;
  502. int needswap_upto = -1;
  503. unsigned int updates;
  504. unsigned char sk_seed[params->n];
  505. unsigned char sk_prf[params->n];
  506. unsigned char pub_seed[params->n];
  507. // Init working params
  508. unsigned char R[params->n];
  509. unsigned char msg_h[params->n];
  510. unsigned char ots_seed[params->n];
  511. uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  512. uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  513. unsigned char idx_bytes_32[32];
  514. bds_state tmp;
  515. // Extract SK
  516. unsigned long long idx = 0;
  517. for (i = 0; i < params->index_bytes; i++) {
  518. idx |= ((unsigned long long)sk[i]) << 8*(params->index_bytes - 1 - i);
  519. }
  520. memcpy(sk_seed, sk+params->index_bytes, params->n);
  521. memcpy(sk_prf, sk+params->index_bytes+params->n, params->n);
  522. memcpy(pub_seed, sk+params->index_bytes+2*params->n, params->n);
  523. // Update SK
  524. for (i = 0; i < params->index_bytes; i++) {
  525. sk[i] = ((idx + 1) >> 8*(params->index_bytes - 1 - i)) & 255;
  526. }
  527. // Secret key for this non-forward-secure version is now updated.
  528. // A production implementation should consider using a file handle instead,
  529. // and write the updated secret key at this point!
  530. // ---------------------------------
  531. // Message Hashing
  532. // ---------------------------------
  533. // Message Hash:
  534. // First compute pseudorandom value
  535. ull_to_bytes(idx_bytes_32, 32, idx);
  536. prf(params, R, idx_bytes_32, sk_prf);
  537. /* Already put the message in the right place, to make it easier to prepend
  538. * things when computing the hash over the message. */
  539. memcpy(sm + params->sig_bytes, m, mlen);
  540. /* Compute the message hash. */
  541. hash_message(params, msg_h, R, pub_root, idx,
  542. sm + params->sig_bytes - 4*params->n, mlen);
  543. // Start collecting signature
  544. *smlen = 0;
  545. // Copy index to signature
  546. for (i = 0; i < params->index_bytes; i++) {
  547. sm[i] = (idx >> 8*(params->index_bytes - 1 - i)) & 255;
  548. }
  549. sm += params->index_bytes;
  550. *smlen += params->index_bytes;
  551. // Copy R to signature
  552. for (i = 0; i < params->n; i++) {
  553. sm[i] = R[i];
  554. }
  555. sm += params->n;
  556. *smlen += params->n;
  557. // ----------------------------------
  558. // Now we start to "really sign"
  559. // ----------------------------------
  560. // Handle lowest layer separately as it is slightly different...
  561. // Prepare Address
  562. set_type(ots_addr, 0);
  563. idx_tree = idx >> params->tree_height;
  564. idx_leaf = (idx & ((1 << params->tree_height)-1));
  565. set_layer_addr(ots_addr, 0);
  566. set_tree_addr(ots_addr, idx_tree);
  567. set_ots_addr(ots_addr, idx_leaf);
  568. // Compute seed for OTS key pair
  569. get_seed(params, ots_seed, sk_seed, ots_addr);
  570. // Compute WOTS signature
  571. wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
  572. sm += params->wots_sig_bytes;
  573. *smlen += params->wots_sig_bytes;
  574. memcpy(sm, states[0].auth, params->tree_height*params->n);
  575. sm += params->tree_height*params->n;
  576. *smlen += params->tree_height*params->n;
  577. // prepare signature of remaining layers
  578. for (i = 1; i < params->d; i++) {
  579. // put WOTS signature in place
  580. memcpy(sm, wots_sigs + (i-1)*params->wots_sig_bytes, params->wots_sig_bytes);
  581. sm += params->wots_sig_bytes;
  582. *smlen += params->wots_sig_bytes;
  583. // put AUTH nodes in place
  584. memcpy(sm, states[i].auth, params->tree_height*params->n);
  585. sm += params->tree_height*params->n;
  586. *smlen += params->tree_height*params->n;
  587. }
  588. updates = (params->tree_height - params->bds_k) >> 1;
  589. set_tree_addr(addr, (idx_tree + 1));
  590. // mandatory update for NEXT_0 (does not count towards h-k/2) if NEXT_0 exists
  591. if ((1 + idx_tree) * (1 << params->tree_height) + idx_leaf < (1ULL << params->full_height)) {
  592. bds_state_update(params, &states[params->d], sk_seed, pub_seed, addr);
  593. }
  594. for (i = 0; i < params->d; i++) {
  595. // check if we're not at the end of a tree
  596. if (! (((idx + 1) & ((1ULL << ((i+1)*params->tree_height)) - 1)) == 0)) {
  597. idx_leaf = (idx >> (params->tree_height * i)) & ((1 << params->tree_height)-1);
  598. idx_tree = (idx >> (params->tree_height * (i+1)));
  599. set_layer_addr(addr, i);
  600. set_tree_addr(addr, idx_tree);
  601. if (i == (unsigned int) (needswap_upto + 1)) {
  602. bds_round(params, &states[i], idx_leaf, sk_seed, pub_seed, addr);
  603. }
  604. updates = bds_treehash_update(params, &states[i], updates, sk_seed, pub_seed, addr);
  605. set_tree_addr(addr, (idx_tree + 1));
  606. // if a NEXT-tree exists for this level;
  607. if ((1 + idx_tree) * (1 << params->tree_height) + idx_leaf < (1ULL << (params->full_height - params->tree_height * i))) {
  608. if (i > 0 && updates > 0 && states[params->d + i].next_leaf < (1ULL << params->full_height)) {
  609. bds_state_update(params, &states[params->d + i], sk_seed, pub_seed, addr);
  610. updates--;
  611. }
  612. }
  613. }
  614. else if (idx < (1ULL << params->full_height) - 1) {
  615. memcpy(&tmp, states+params->d + i, sizeof(bds_state));
  616. memcpy(states+params->d + i, states + i, sizeof(bds_state));
  617. memcpy(states + i, &tmp, sizeof(bds_state));
  618. set_layer_addr(ots_addr, (i+1));
  619. set_tree_addr(ots_addr, ((idx + 1) >> ((i+2) * params->tree_height)));
  620. set_ots_addr(ots_addr, (((idx >> ((i+1) * params->tree_height)) + 1) & ((1 << params->tree_height)-1)));
  621. get_seed(params, ots_seed, sk+params->index_bytes, ots_addr);
  622. wots_sign(params, wots_sigs + i*params->wots_sig_bytes, states[i].stack, ots_seed, pub_seed, ots_addr);
  623. states[params->d + i].stackoffset = 0;
  624. states[params->d + i].next_leaf = 0;
  625. updates--; // WOTS-signing counts as one update
  626. needswap_upto = i;
  627. for (j = 0; j < params->tree_height-params->bds_k; j++) {
  628. states[i].treehash[j].completed = 1;
  629. }
  630. }
  631. }
  632. memcpy(sm, m, mlen);
  633. *smlen += mlen;
  634. return 0;
  635. }