Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

153 рядки
3.7 KiB

  1. /*
  2. wots.c version 20160722
  3. Andreas Hülsing
  4. Joost Rijneveld
  5. Public domain.
  6. */
  7. #include "math.h"
  8. #include "stdio.h"
  9. #include "stdint.h"
  10. #include "xmss_commons.h"
  11. //#include "params.h"
  12. //#include "prg.h"
  13. #include "hash.h"
  14. #include "wots.h"
  15. #include "hash_address.h"
  16. #include "params.h"
  17. /**
  18. * Helper method for pseudorandom key generation
  19. * Expands an n-byte array into a len*n byte array
  20. * this is done using PRF
  21. */
  22. static void expand_seed(unsigned char *outseeds, const unsigned char *inseed)
  23. {
  24. uint32_t i = 0;
  25. unsigned char ctr[32];
  26. for(i = 0; i < XMSS_WOTS_LEN; i++){
  27. to_byte(ctr, i, 32);
  28. prf(outseeds + i*XMSS_N, ctr, inseed, XMSS_N);
  29. }
  30. }
  31. /**
  32. * Computes the chaining function.
  33. * out and in have to be n-byte arrays
  34. *
  35. * interpretes in as start-th value of the chain
  36. * addr has to contain the address of the chain
  37. */
  38. static void gen_chain(unsigned char *out, const unsigned char *in, unsigned int start, unsigned int steps, const unsigned char *pub_seed, uint32_t addr[8])
  39. {
  40. uint32_t i, j;
  41. for (j = 0; j < XMSS_N; j++)
  42. out[j] = in[j];
  43. for (i = start; i < (start+steps) && i < XMSS_WOTS_W; i++) {
  44. setHashADRS(addr, i);
  45. hash_f(out, out, pub_seed, addr, XMSS_N);
  46. }
  47. }
  48. /**
  49. * base_w algorithm as described in draft.
  50. *
  51. *
  52. */
  53. static void base_w(int *output, const int out_len, const unsigned char *input)
  54. {
  55. int in = 0;
  56. int out = 0;
  57. uint32_t total = 0;
  58. int bits = 0;
  59. int consumed = 0;
  60. for (consumed = 0; consumed < out_len; consumed++) {
  61. if (bits == 0) {
  62. total = input[in];
  63. in++;
  64. bits += 8;
  65. }
  66. bits -= XMSS_WOTS_LOG_W;
  67. output[out] = (total >> bits) & (XMSS_WOTS_W - 1);
  68. out++;
  69. }
  70. }
  71. void wots_pkgen(unsigned char *pk, const unsigned char *sk, const unsigned char *pub_seed, uint32_t addr[8])
  72. {
  73. uint32_t i;
  74. expand_seed(pk, sk);
  75. for (i=0; i < XMSS_WOTS_LEN; i++) {
  76. setChainADRS(addr, i);
  77. gen_chain(pk+i*XMSS_N, pk+i*XMSS_N, 0, XMSS_WOTS_W-1, pub_seed, addr);
  78. }
  79. }
  80. void wots_sign(unsigned char *sig, const unsigned char *msg, const unsigned char *sk, const unsigned char *pub_seed, uint32_t addr[8])
  81. {
  82. int basew[XMSS_WOTS_LEN];
  83. int csum = 0;
  84. uint32_t i = 0;
  85. base_w(basew, XMSS_WOTS_LEN1, msg);
  86. for (i=0; i < XMSS_WOTS_LEN1; i++) {
  87. csum += XMSS_WOTS_W - 1 - basew[i];
  88. }
  89. csum = csum << (8 - ((XMSS_WOTS_LEN2 * XMSS_WOTS_LOG_W) % 8));
  90. int len_2_bytes = ((XMSS_WOTS_LEN2 * XMSS_WOTS_LOG_W) + 7) / 8;
  91. unsigned char csum_bytes[len_2_bytes];
  92. to_byte(csum_bytes, csum, len_2_bytes);
  93. int csum_basew[len_2_bytes / XMSS_WOTS_LOG_W];
  94. base_w(csum_basew, XMSS_WOTS_LEN2, csum_bytes);
  95. for (i = 0; i < XMSS_WOTS_LEN2; i++) {
  96. basew[XMSS_WOTS_LEN1 + i] = csum_basew[i];
  97. }
  98. expand_seed(sig, sk);
  99. for (i = 0; i < XMSS_WOTS_LEN; i++) {
  100. setChainADRS(addr, i);
  101. gen_chain(sig+i*XMSS_N, sig+i*XMSS_N, 0, basew[i], pub_seed, addr);
  102. }
  103. }
  104. void wots_pkFromSig(unsigned char *pk, const unsigned char *sig, const unsigned char *msg, const unsigned char *pub_seed, uint32_t addr[8])
  105. {
  106. int basew[XMSS_WOTS_LEN];
  107. int csum = 0;
  108. uint32_t i = 0;
  109. base_w(basew, XMSS_WOTS_LEN1, msg);
  110. for (i=0; i < XMSS_WOTS_LEN1; i++) {
  111. csum += XMSS_WOTS_W - 1 - basew[i];
  112. }
  113. csum = csum << (8 - ((XMSS_WOTS_LEN2 * XMSS_WOTS_LOG_W) % 8));
  114. int len_2_bytes = ((XMSS_WOTS_LEN2 * XMSS_WOTS_LOG_W) + 7) / 8;
  115. unsigned char csum_bytes[len_2_bytes];
  116. to_byte(csum_bytes, csum, len_2_bytes);
  117. int csum_basew[len_2_bytes / XMSS_WOTS_LOG_W];
  118. base_w(csum_basew, XMSS_WOTS_LEN2, csum_bytes);
  119. for (i = 0; i < XMSS_WOTS_LEN2; i++) {
  120. basew[XMSS_WOTS_LEN1 + i] = csum_basew[i];
  121. }
  122. for (i=0; i < XMSS_WOTS_LEN; i++) {
  123. setChainADRS(addr, i);
  124. gen_chain(pk+i*XMSS_N, sig+i*XMSS_N, basew[i], XMSS_WOTS_W-1-basew[i], pub_seed, addr);
  125. }
  126. }