Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

524 linhas
14 KiB

  1. #include "xmss.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <math.h>
  6. #include "randombytes.h"
  7. #include "wots.h"
  8. #include "hash.h"
  9. #include "prg.h"
  10. #include "xmss_commons.h"
  11. // For testing
  12. #include "stdio.h"
  13. /**
  14. * Macros used to manipulate the respective fields
  15. * in the 16byte hash address
  16. */
  17. #define SET_OTS_BIT(a, b) {\
  18. a[9] = (a[9] & 253) | (b << 1);}
  19. #define SET_OTS_ADDRESS(a, v) {\
  20. a[12] = (a[12] & 1) | ((v << 1) & 255);\
  21. a[11] = (v >> 7) & 255;\
  22. a[10] = (v >> 15) & 255;\
  23. a[9] = (a[9] & 254) | ((v >> 23) & 1);}
  24. #define ZEROISE_OTS_ADDR(a) {\
  25. a[12] = (a[12] & 254);\
  26. a[13] = 0;\
  27. a[14] = 0;\
  28. a[15] = 0;}
  29. #define SET_LTREE_BIT(a, b) {\
  30. a[9] = (a[9] & 254) | b;}
  31. #define SET_LTREE_ADDRESS(a, v) {\
  32. a[12] = v & 255;\
  33. a[11] = (v >> 8) & 255;\
  34. a[10] = (v >> 16) & 255;}
  35. #define SET_LTREE_TREE_HEIGHT(a, v) {\
  36. a[13] = (a[13] & 3) | ((v << 2) & 255);}
  37. #define SET_LTREE_TREE_INDEX(a, v) {\
  38. a[15] = (a[15] & 3) | ((v << 2) & 255);\
  39. a[14] = (v >> 6) & 255;\
  40. a[13] = (a[13] & 252) | ((v >> 14) & 3);}
  41. #define SET_NODE_PADDING(a) {\
  42. a[10] = 0;\
  43. a[11] = a[11] & 3;}
  44. #define SET_NODE_TREE_HEIGHT(a, v) {\
  45. a[12] = (a[12] & 3) | ((v << 2) & 255);\
  46. a[11] = (a[11] & 252) | ((v >> 6) & 3);}
  47. #define SET_NODE_TREE_INDEX(a, v) {\
  48. a[15] = (a[15] & 3) | ((v << 2) & 255);\
  49. a[14] = (v >> 6) & 255;\
  50. a[13] = (v >> 14) & 255;\
  51. a[12] = (a[12] & 252) | ((v >> 22) & 3);}
  52. /**
  53. * Used for pseudorandom keygeneration,
  54. * generates the seed for the WOTS keypair at address addr
  55. */
  56. static void get_seed(unsigned char seed[32], const unsigned char *sk_seed, unsigned char addr[16])
  57. {
  58. // Make sure that chain addr, hash addr, and key bit are 0!
  59. ZEROISE_OTS_ADDR(addr);
  60. // Generate pseudorandom value
  61. prg_with_counter(seed, 32, sk_seed, 32, addr);
  62. }
  63. /**
  64. * Initialize xmss params struct
  65. * parameter names are the same as in the draft
  66. */
  67. void xmss_set_params(xmss_params *params, int m, int n, int h, int w)
  68. {
  69. params->h = h;
  70. params->m = m;
  71. params->n = n;
  72. wots_params wots_par;
  73. wots_set_params(&wots_par, m, n, w);
  74. params->wots_par = &wots_par;
  75. }
  76. /**
  77. * Computes a leaf from a WOTS public key using an L-tree.
  78. */
  79. static void l_tree(unsigned char *leaf, unsigned char *wots_pk, const xmss_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  80. {
  81. uint l = params->wots_par->len;
  82. uint n = params->n;
  83. unsigned long i = 0;
  84. uint height = 0;
  85. //ADRS.setTreeHeight(0);
  86. SET_LTREE_TREE_HEIGHT(addr,height);
  87. unsigned long bound;
  88. while ( l > 1 )
  89. {
  90. bound = l >> 1; //floor(l / 2);
  91. for ( i = 0; i < bound; i = i + 1 ) {
  92. //ADRS.setTreeIndex(i);
  93. SET_LTREE_TREE_INDEX(addr,i);
  94. //wots_pk[i] = RAND_HASH(pk[2i], pk[2i + 1], SEED, ADRS);
  95. hash_2n_n(wots_pk+i*n,wots_pk+i*2*n, pub_seed, addr, n);
  96. }
  97. //if ( l % 2 == 1 ) {
  98. if(l&1)
  99. {
  100. //pk[floor(l / 2) + 1] = pk[l];
  101. memcpy(wots_pk+(l>>1)*n,wots_pk+(l-1)*n, n);
  102. //l = ceil(l / 2);
  103. l=(l>>1)+1;
  104. }
  105. else
  106. {
  107. //l = ceil(l / 2);
  108. l=(l>>1);
  109. }
  110. //ADRS.setTreeHeight(ADRS.getTreeHeight() + 1);
  111. height++;
  112. SET_LTREE_TREE_HEIGHT(addr,height);
  113. }
  114. //return pk[0];
  115. memcpy(leaf,wots_pk,n);
  116. }
  117. /**
  118. * Computes the leaf at a given address. First generates the WOTS key pair, then computes leaf using l_tree. As this happens position independent, we only require that addr encodes the right ltree-address.
  119. */
  120. static void gen_leaf_wots(unsigned char *leaf, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, unsigned char ltree_addr[16], unsigned char ots_addr[16])
  121. {
  122. unsigned char seed[32];
  123. unsigned char pk[params->wots_par->keysize];
  124. get_seed(seed, sk_seed, ots_addr);
  125. wots_pkgen(pk, seed, params->wots_par, pub_seed, ots_addr);
  126. l_tree(leaf, pk, params, pub_seed, ltree_addr);
  127. }
  128. /**
  129. * Merkle's TreeHash algorithm. The address only needs to initialize the first 78 bits of addr. Everything else will be set by treehash.
  130. * Currently only used for key generation.
  131. *
  132. */
  133. static void treehash(unsigned char *node, int height, int index, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, const unsigned char addr[16])
  134. {
  135. uint idx = index;
  136. uint n = params->n;
  137. // use three different addresses because at this point we use all three formats in parallel
  138. unsigned char ots_addr[16];
  139. unsigned char ltree_addr[16];
  140. unsigned char node_addr[16];
  141. memcpy(ots_addr, addr, 10);
  142. SET_OTS_BIT(ots_addr, 1);
  143. memcpy(ltree_addr, addr, 10);
  144. SET_OTS_BIT(ltree_addr, 0);
  145. SET_LTREE_BIT(ltree_addr, 1);
  146. memcpy(node_addr, ltree_addr, 10);
  147. SET_LTREE_BIT(node_addr, 0);
  148. SET_NODE_PADDING(node_addr);
  149. int lastnode,i;
  150. unsigned char stack[(height+1)*n];
  151. unsigned int stacklevels[height+1];
  152. unsigned int stackoffset=0;
  153. lastnode = idx+(1<<height);
  154. for(;idx<lastnode;idx++)
  155. {
  156. SET_LTREE_ADDRESS(ltree_addr,idx);
  157. SET_OTS_ADDRESS(ots_addr,idx);
  158. gen_leaf_wots(stack+stackoffset*n,sk_seed,params, pub_seed, ltree_addr, ots_addr);
  159. stacklevels[stackoffset] = 0;
  160. stackoffset++;
  161. while(stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2])
  162. {
  163. SET_NODE_TREE_HEIGHT(node_addr,stacklevels[stackoffset-1]);
  164. SET_NODE_TREE_INDEX(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
  165. hash_2n_n(stack+(stackoffset-2)*n,stack+(stackoffset-2)*n, pub_seed,
  166. node_addr, n);
  167. stacklevels[stackoffset-2]++;
  168. stackoffset--;
  169. }
  170. }
  171. for(i=0;i<n;i++)
  172. node[i] = stack[i];
  173. }
  174. /**
  175. * Computes a root node given a leaf and an authapth
  176. */
  177. static void validate_authpath(unsigned char *root, const unsigned char *leaf, unsigned long leafidx, const unsigned char *authpath, const xmss_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  178. {
  179. uint n = params->n;
  180. int i,j;
  181. unsigned char buffer[2*n];
  182. // If leafidx is odd (last bit = 1), current path element is a right child and authpath has to go to the left.
  183. // Otherwise, it is the other way around
  184. if(leafidx&1)
  185. {
  186. for(j=0;j<n;j++)
  187. buffer[n+j] = leaf[j];
  188. for(j=0;j<n;j++)
  189. buffer[j] = authpath[j];
  190. }
  191. else
  192. {
  193. for(j=0;j<n;j++)
  194. buffer[j] = leaf[j];
  195. for(j=0;j<n;j++)
  196. buffer[n+j] = authpath[j];
  197. }
  198. authpath += n;
  199. for(i=0;i<params->h-1;i++)
  200. {
  201. SET_NODE_TREE_HEIGHT(addr,i);
  202. leafidx >>= 1;
  203. SET_NODE_TREE_INDEX(addr, leafidx);
  204. if(leafidx&1)
  205. {
  206. hash_2n_n(buffer+n,buffer,pub_seed, addr, n);
  207. for(j=0;j<n;j++)
  208. buffer[j] = authpath[j];
  209. }
  210. else
  211. {
  212. hash_2n_n(buffer,buffer,pub_seed, addr, n);
  213. for(j=0;j<n;j++)
  214. buffer[j+n] = authpath[j];
  215. }
  216. authpath += n;
  217. }
  218. SET_NODE_TREE_HEIGHT(addr, (params->h-1));
  219. leafidx >>= 1;
  220. SET_NODE_TREE_INDEX(addr, leafidx);
  221. hash_2n_n(root,buffer,pub_seed,addr,n);
  222. }
  223. /**
  224. * Computes the authpath and the root. This method is using a lot of space as we build the whole tree and then select the authpath nodes.
  225. * For more efficient algorithms see e.g. the chapter on hash-based signatures in Bernstein, Buchmann, Dahmen. "Post-quantum Cryptography", Springer 2009.
  226. * It returns the authpath in "authpath" with the node on level 0 at index 0.
  227. */
  228. static void compute_authpath_wots(unsigned char *root, unsigned char *authpath, unsigned long leaf_idx, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, unsigned char addr[16])
  229. {
  230. uint i, j, level;
  231. int n = params->n;
  232. int h = params->h;
  233. unsigned char tree[2*(1<<h)*n];
  234. unsigned char ots_addr[16];
  235. unsigned char ltree_addr[16];
  236. unsigned char node_addr[16];
  237. memcpy(ots_addr, addr, 10);
  238. SET_OTS_BIT(ots_addr, 1);
  239. memcpy(ltree_addr, addr, 10);
  240. SET_OTS_BIT(ltree_addr, 0);
  241. SET_LTREE_BIT(ltree_addr, 1);
  242. memcpy(node_addr, ltree_addr, 10);
  243. SET_LTREE_BIT(node_addr, 0);
  244. SET_NODE_PADDING(node_addr);
  245. // Compute all leaves
  246. for(i = 0; i < (1<<h); i++)
  247. {
  248. SET_LTREE_ADDRESS(ltree_addr,i);
  249. SET_OTS_ADDRESS(ots_addr,i);
  250. gen_leaf_wots(tree+((1<<h)*n + i*n), sk_seed, params, pub_seed, ltree_addr, ots_addr);
  251. }
  252. level = 0;
  253. // Compute tree:
  254. // Outer loop: For each inner layer
  255. for (i = (1<<h); i > 0; i>>=1)
  256. {
  257. SET_NODE_TREE_HEIGHT(node_addr, level);
  258. // Inner loop: for each pair of sibling nodes
  259. for (j = 0; j < i; j+=2)
  260. {
  261. SET_NODE_TREE_INDEX(node_addr, j>>1);
  262. hash_2n_n(tree + (i>>1)*n + (j>>1) * n, tree + i*n + j*n, pub_seed, node_addr, n);
  263. }
  264. level++;
  265. }
  266. // copy authpath
  267. for(i=0;i<h;i++)
  268. memcpy(authpath + i*n, tree + ((1<<h)>>i)*n + ((leaf_idx >> i) ^ 1) * n, n);
  269. // copy root
  270. memcpy(root, tree+n, n);
  271. }
  272. /*
  273. * Generates a XMSS key pair for a given parameter set.
  274. * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED]
  275. * Format pk: [root || PUB_SEED] omitting algo oid.
  276. */
  277. int xmss_keypair(unsigned char *pk, unsigned char *sk, xmss_params *params)
  278. {
  279. uint n = params->n;
  280. uint m = params->m;
  281. // Set idx = 0
  282. sk[0] = 0;
  283. sk[1] = 0;
  284. sk[2] = 0;
  285. sk[3] = 0;
  286. // Init SK_SEED (n byte), SK_PRF (m byte), and PUB_SEED (n byte)
  287. randombytes(sk+4,2*n+m);
  288. // Copy PUB_SEED to public key
  289. memcpy(pk+n, sk+4+n+m,n);
  290. unsigned char addr[16] = {0,0,0,0};
  291. // Compute root
  292. treehash(pk, params->h, 0, sk+4, params, sk+4+n+m, addr);
  293. return 0;
  294. }
  295. /**
  296. * Signs a message.
  297. * Returns
  298. * 1. an array containing the signature followed by the message AND
  299. * 2. an updated secret key!
  300. *
  301. */
  302. int xmss_sign(unsigned char *sk, unsigned char *sig_msg, unsigned long long *sig_msg_len, const unsigned char *msg, unsigned long long msglen, const xmss_params *params, unsigned char* pk)
  303. {
  304. uint n = params->n;
  305. uint m = params->m;
  306. // Extract SK
  307. unsigned long idx = (sk[0] << 24) | (sk[1] << 16) | (sk[2] << 8) || sk[3];
  308. unsigned char sk_seed[n];
  309. memcpy(sk_seed,sk+4,n);
  310. unsigned char sk_prf[m];
  311. memcpy(sk_prf,sk+4+n,m);
  312. unsigned char pub_seed[n];
  313. memcpy(pub_seed,sk+4+n+m,n);
  314. // Update SK
  315. sk[0] = ((idx + 1) >> 24) & 255;
  316. sk[1] = ((idx + 1) >> 16) & 255;
  317. sk[2] = ((idx + 1) >> 8) & 255;
  318. sk[3] = (idx + 1) & 255;
  319. // -- Secret key for this non-forward-secure version is now updated.
  320. // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
  321. // Init working params
  322. unsigned long long i;
  323. unsigned char R[m];
  324. unsigned char msg_h[m];
  325. unsigned char root[n];
  326. unsigned char ots_seed[n];
  327. unsigned char ots_addr[16] = {0,0,0,0};
  328. // ---------------------------------
  329. // Message Hashing
  330. // ---------------------------------
  331. // Message Hash:
  332. // First compute pseudorandom key
  333. prf_m(R, msg, msglen, sk_prf, m);
  334. // Then use it for message digest
  335. hash_m(msg_h, msg, msglen, R, m, m);
  336. // Start collecting signature
  337. *sig_msg_len = 0;
  338. // Copy index to signature
  339. sig_msg[0] = (idx >> 24) & 255;
  340. sig_msg[1] = (idx >> 16) & 255;
  341. sig_msg[2] = (idx >> 8) & 255;
  342. sig_msg[3] = idx & 255;
  343. sig_msg += 4;
  344. *sig_msg_len += 4;
  345. // Copy R to signature
  346. for(i=0; i<m; i++)
  347. sig_msg[i] = R[i];
  348. sig_msg += m;
  349. *sig_msg_len += m;
  350. // ----------------------------------
  351. // Now we start to "really sign"
  352. // ----------------------------------
  353. // Prepare Address
  354. SET_OTS_BIT(ots_addr,1);
  355. SET_OTS_ADDRESS(ots_addr,idx);
  356. // Compute seed for OTS key pair
  357. get_seed(ots_seed, sk_seed, ots_addr);
  358. // Compute WOTS signature
  359. wots_sign(sig_msg, msg_h, ots_seed, params->wots_par, pub_seed, ots_addr);
  360. sig_msg += params->wots_par->keysize;
  361. *sig_msg_len += params->wots_par->keysize;
  362. compute_authpath_wots(root, sig_msg, idx, sk_seed, params, pub_seed, ots_addr);
  363. sig_msg += params->h*n;
  364. *sig_msg_len += params->h*n;
  365. //DEBUG
  366. for(i=0;i<n;i++)
  367. if(root[i] != pk[i])
  368. printf("Different PK's %llu",i);
  369. //Whipe secret elements?
  370. //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
  371. memcpy(sig_msg,msg,msglen);
  372. *sig_msg_len += msglen;
  373. return 0;
  374. }
  375. /**
  376. * Verifies a given message signature pair under a given public key.
  377. */
  378. int xmss_sign_open(unsigned char *msg, unsigned long long *msglen, const unsigned char *sig_msg, unsigned long long sig_msg_len, const unsigned char *pk, const xmss_params *params)
  379. {
  380. uint n = params->n;
  381. uint m = params->m;
  382. unsigned long long i, m_len;
  383. unsigned long idx=0;
  384. unsigned char wots_pk[params->wots_par->keysize];
  385. unsigned char pkhash[n];
  386. unsigned char root[n];
  387. unsigned char msg_h[m];
  388. unsigned char pub_seed[n];
  389. memcpy(pub_seed,pk+n,n);
  390. // Init addresses
  391. unsigned char ots_addr[16] = {0,0,0,0};
  392. unsigned char ltree_addr[16];
  393. unsigned char node_addr[16];
  394. SET_OTS_BIT(ots_addr, 1);
  395. memcpy(ltree_addr, ots_addr, 10);
  396. SET_OTS_BIT(ltree_addr, 0);
  397. SET_LTREE_BIT(ltree_addr, 1);
  398. memcpy(node_addr, ltree_addr, 10);
  399. SET_LTREE_BIT(node_addr, 0);
  400. SET_NODE_PADDING(node_addr);
  401. // Extract index
  402. idx = (sig_msg[0] << 24) | (sig_msg[1] << 16) | (sig_msg[2] << 8) || sig_msg[3];
  403. sig_msg += 4;
  404. sig_msg_len -= 4;
  405. // hash message (recall, R is now on pole position at sig_msg
  406. unsigned long long tmp_sig_len = m+params->wots_par->keysize+params->h*n;
  407. m_len = sig_msg_len - tmp_sig_len;
  408. hash_m(msg_h, sig_msg + tmp_sig_len, m_len, sig_msg, m, m);
  409. sig_msg += m;
  410. sig_msg_len -= m;
  411. //-----------------------
  412. // Verify signature
  413. //-----------------------
  414. // Prepare Address
  415. SET_OTS_ADDRESS(ots_addr,idx);
  416. // Check WOTS signature
  417. wots_pkFromSig(wots_pk, sig_msg, msg_h, params->wots_par, pub_seed, ots_addr);
  418. sig_msg += params->wots_par->keysize;
  419. sig_msg_len -= params->wots_par->keysize;
  420. // Compute Ltree
  421. SET_LTREE_ADDRESS(ltree_addr, idx);
  422. l_tree(pkhash, wots_pk, params, pub_seed, ltree_addr);
  423. // Compute root
  424. validate_authpath(root, pkhash, idx, sig_msg, params, pub_seed, node_addr);
  425. sig_msg += params->h*n;
  426. sig_msg_len -= params->h*n;
  427. for(i=0;i<n;i++)
  428. if(root[i] != pk[i])
  429. goto fail;
  430. *msglen = sig_msg_len;
  431. for(i=0;i<*msglen;i++)
  432. msg[i] = sig_msg[i];
  433. return 0;
  434. fail:
  435. *msglen = sig_msg_len;
  436. for(i=0;i<*msglen;i++)
  437. msg[i] = 0;
  438. *msglen = -1;
  439. return -1;
  440. }