Não pode escolher mais do que 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.

164 linhas
4.2 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. void wots_set_params(wots_params *params, int n, int w)
  17. {
  18. params->n = n;
  19. params->w = w;
  20. params->log_w = (int) log2(w);
  21. params->len_1 = (int) ceil(((8*n) / params->log_w));
  22. params->len_2 = (int) floor(log2(params->len_1*(w-1)) / params->log_w) + 1;
  23. params->len = params->len_1 + params->len_2;
  24. params->keysize = params->len*params->n;
  25. }
  26. /**
  27. * Helper method for pseudorandom key generation
  28. * Expands an n-byte array into a len*n byte array
  29. * this is done using PRF
  30. */
  31. static void expand_seed(unsigned char *outseeds, const unsigned char *inseed, const wots_params *params)
  32. {
  33. uint32_t i = 0;
  34. unsigned char ctr[32];
  35. for(i = 0; i < params->len; i++){
  36. to_byte(ctr, i, 32);
  37. prf((outseeds + (i*params->n)), ctr, inseed, params->n);
  38. }
  39. }
  40. /**
  41. * Computes the chaining function.
  42. * out and in have to be n-byte arrays
  43. *
  44. * interpretes in as start-th value of the chain
  45. * addr has to contain the address of the chain
  46. */
  47. static void gen_chain(unsigned char *out, const unsigned char *in, unsigned int start, unsigned int steps, const wots_params *params, const unsigned char *pub_seed, uint32_t addr[8])
  48. {
  49. uint32_t i, j;
  50. for (j = 0; j < params->n; j++)
  51. out[j] = in[j];
  52. for (i = start; i < (start+steps) && i < params->w; i++) {
  53. setHashADRS(addr, i);
  54. hash_f(out, out, pub_seed, addr, params->n);
  55. }
  56. }
  57. /**
  58. * base_w algorithm as described in draft.
  59. *
  60. *
  61. */
  62. static void base_w(int *output, const int out_len, const unsigned char *input, const wots_params *params)
  63. {
  64. int in = 0;
  65. int out = 0;
  66. uint8_t total = 0;
  67. int bits = 0;
  68. int consumed = 0;
  69. for (consumed = 0; consumed < out_len; consumed++) {
  70. if (bits == 0) {
  71. total = input[in];
  72. in++;
  73. bits += 8;
  74. }
  75. bits -= params->log_w;
  76. output[out] = (total >> bits) & (params->w - 1);
  77. out++;
  78. }
  79. }
  80. void wots_pkgen(unsigned char *pk, const unsigned char *sk, const wots_params *params, const unsigned char *pub_seed, uint32_t addr[8])
  81. {
  82. uint32_t i;
  83. expand_seed(pk, sk, params);
  84. for (i=0; i < params->len; i++) {
  85. setChainADRS(addr, i);
  86. gen_chain(pk+i*params->n, pk+i*params->n, 0, params->w-1, params, pub_seed, addr);
  87. }
  88. }
  89. void wots_sign(unsigned char *sig, const unsigned char *msg, const unsigned char *sk, const wots_params *params, const unsigned char *pub_seed, uint32_t addr[8])
  90. {
  91. int basew[params->len];
  92. int csum = 0;
  93. uint32_t i = 0;
  94. base_w(basew, params->len_1, msg, params);
  95. for (i=0; i < params->len_1; i++) {
  96. csum += params->w - 1 - basew[i];
  97. }
  98. csum = csum << (8 - ((params->len_2 * params->log_w) % 8));
  99. int len_2_bytes = ((params->len_2 * params->log_w) + 7) / 8;
  100. unsigned char csum_bytes[len_2_bytes];
  101. to_byte(csum_bytes, csum, len_2_bytes);
  102. int csum_basew[params->len_2];
  103. base_w(csum_basew, params->len_2, csum_bytes, params);
  104. for (i = 0; i < params->len_2; i++) {
  105. basew[params->len_1 + i] = csum_basew[i];
  106. }
  107. expand_seed(sig, sk, params);
  108. for (i = 0; i < params->len; i++) {
  109. setChainADRS(addr, i);
  110. gen_chain(sig+i*params->n, sig+i*params->n, 0, basew[i], params, pub_seed, addr);
  111. }
  112. }
  113. void wots_pkFromSig(unsigned char *pk, const unsigned char *sig, const unsigned char *msg, const wots_params *params, const unsigned char *pub_seed, uint32_t addr[8])
  114. {
  115. int basew[params->len];
  116. int csum = 0;
  117. uint32_t i = 0;
  118. base_w(basew, params->len_1, msg, params);
  119. for (i=0; i < params->len_1; i++) {
  120. csum += params->w - 1 - basew[i];
  121. }
  122. csum = csum << (8 - ((params->len_2 * params->log_w) % 8));
  123. int len_2_bytes = ((params->len_2 * params->log_w) + 7) / 8;
  124. unsigned char csum_bytes[len_2_bytes];
  125. to_byte(csum_bytes, csum, len_2_bytes);
  126. int csum_basew[params->len_2];
  127. base_w(csum_basew, params->len_2, csum_bytes, params);
  128. for (i = 0; i < params->len_2; i++) {
  129. basew[params->len_1 + i] = csum_basew[i];
  130. }
  131. for (i=0; i < params->len; i++) {
  132. setChainADRS(addr, i);
  133. gen_chain(pk+i*params->n, sig+i*params->n, basew[i], params->w-1-basew[i], params, pub_seed, addr);
  134. }
  135. }