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.

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