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

201 рядки
5.1 KiB

  1. /*
  2. wots.c version 2015xxxx
  3. Andreas Hülsing
  4. Public domain.
  5. */
  6. #include "math.h"
  7. #include "stdio.h"
  8. #include "xmss_commons.h"
  9. //#include "params.h"
  10. #include "prg.h"
  11. #include "hash.h"
  12. #include "wots.h"
  13. /**
  14. * Macros used to manipulate the respective fields
  15. * in the 16byte hash address
  16. */
  17. #define SET_HASH_ADDRESS(a, v) {\
  18. a[15] = (a[15] & 1) | ((v << 1) & 255);\
  19. a[14] = (a[14] & 254) | ((v >> 7) & 1);}
  20. #define SET_CHAIN_ADDRESS(a, v) {\
  21. a[14] = (a[14] & 1) | ((v << 1) & 255);\
  22. a[13] = (v >> 7) & 255;\
  23. a[12] = (a[12] & 254) | ((v >> 15) & 1);}
  24. void wots_set_params(wots_params *params, int m, int n, int w)
  25. {
  26. params->m = m;
  27. params->n = n;
  28. params->w = w;
  29. params->log_w = (int) log2(w);
  30. params->len_1 = (int) ceil(((8*m) / params->log_w));
  31. params->len_2 = (int) floor(log2(params->len_1*(w-1)) / params->log_w) + 1;
  32. params->len = params->len_1 + params->len_2;
  33. params->keysize = params->len*params->n;
  34. }
  35. /**
  36. * Helper method for pseudorandom key generation
  37. * Expands a 32 byte array into a len*n byte array
  38. * this is done using chacha20 with nonce 0 and counter 0
  39. */
  40. static void expand_seed(unsigned char *outseeds, const unsigned char *inseed, wots_params *params)
  41. {
  42. prg(outseeds, params->keysize, inseed, 32);
  43. }
  44. /**
  45. * Computes the chaining function.
  46. * out and in have to be n-byte arrays
  47. *
  48. * interpretes in as start-th value of the chain
  49. * addr has to contain the address of the chain
  50. */
  51. static void gen_chain(unsigned char *out, const unsigned char *in, int start, int steps, const wots_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  52. {
  53. uint i,j;
  54. for(j=0;j<params->n;j++)
  55. out[j] = in[j];
  56. for(i=start;i<(start+steps) && i<params->w;i++){
  57. SET_HASH_ADDRESS(addr,i);
  58. // printf("Hash %d:",i);
  59. // hexdump(addr,16);
  60. // printf("\n");
  61. hash_n_n(out,out, pub_seed, addr,params->n);
  62. }
  63. }
  64. /**
  65. * base_w algorithm as described in draft.
  66. *
  67. *
  68. */
  69. static void base_w(int *output, const unsigned char *input, int in_len, wots_params *params)
  70. {
  71. int in = 0;
  72. int out = 0;
  73. int total = 0;
  74. int bits = 0;
  75. int consumed = 0;
  76. for(consumed = 0; consumed < 8 * in_len; consumed += params->log_w)
  77. {
  78. if(bits == 0){
  79. total = input[in_len - 1 - in];
  80. in++;
  81. bits += 8;
  82. }
  83. bits -= params->log_w;
  84. output[out] = (total >> bits) & (params->w - 1);
  85. out++;
  86. }
  87. }
  88. /**
  89. * Alternative base w algorithm for w = 16 to check...
  90. */
  91. static void base_w_alternative(int *output, unsigned char *input, int in_len, wots_params *params)
  92. {
  93. uint i = 0;
  94. for(i = 0; i < in_len; i += 2)
  95. {
  96. output[i] = input[in_len - 1 - (i / 2)] >> 4;
  97. output[i+1] = input[in_len - 1 - (i / 2)] & 0xf;
  98. }
  99. }
  100. void wots_pkgen(unsigned char *pk, const unsigned char *sk, wots_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  101. {
  102. uint i;
  103. expand_seed(pk, sk, params);
  104. for(i=0;i<params->len;i++){
  105. SET_CHAIN_ADDRESS(addr,i);
  106. // printf("Chain: %d\n",i);
  107. // hexdump(addr,16);
  108. // printf("\n");
  109. gen_chain(pk+i*params->n, pk+i*params->n, 0, params->w-1, params, pub_seed, addr);
  110. }
  111. }
  112. void wots_sign(unsigned char *sig, const unsigned char *msg, const unsigned char *sk, wots_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  113. {
  114. int basew[params->len];
  115. int csum = 0;
  116. uint i=0;
  117. base_w(basew, msg, params->m, params);
  118. for(i=0;i<params->len_1;i++)
  119. {
  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[len_2_bytes / params->log_w];
  127. base_w(csum_basew, csum_bytes, len_2_bytes, params);
  128. for(i = 0; i < params->len_2; i++)
  129. {
  130. basew[params->len_1 + i] = csum_basew[i];
  131. }
  132. expand_seed(sig, sk, params);
  133. for(i=0;i<params->len;i++){
  134. SET_CHAIN_ADDRESS(addr,i);
  135. // printf("Chain: %d\n",i);
  136. // hexdump(addr,16);
  137. // printf("\n");
  138. gen_chain(sig+i*params->n, sig+i*params->n, 0, basew[i], params, pub_seed, addr);
  139. }
  140. }
  141. void wots_pkFromSig(unsigned char *pk, const unsigned char *sig, const unsigned char *msg, wots_params *params, const unsigned char *pub_seed, unsigned char addr[16])
  142. {
  143. int basew[params->len];
  144. int csum = 0;
  145. uint i=0;
  146. base_w(basew, msg, params->m, params);
  147. for(i=0;i<params->len_1;i++)
  148. {
  149. csum += params->w - 1 - basew[i];
  150. }
  151. csum = csum << ( 8 - ( ( params->len_2 * params->log_w ) % 8 ));
  152. int len_2_bytes = ((params->len_2 * params->log_w) + 7) / 8;
  153. unsigned char csum_bytes[len_2_bytes];
  154. to_byte(csum_bytes, csum, len_2_bytes);
  155. int csum_basew[len_2_bytes / params->log_w];
  156. base_w(csum_basew, csum_bytes, len_2_bytes, params);
  157. for(i = 0; i < params->len_2; i++)
  158. {
  159. basew[params->len_1 + i] = csum_basew[i];
  160. }
  161. for(i=0;i<params->len;i++){
  162. SET_CHAIN_ADDRESS(addr,i);
  163. // printf("Chain: %d\n",i);
  164. // hexdump(addr,16);
  165. // printf("\n");
  166. gen_chain(pk+i*params->n, sig+i*params->n, basew[i], params->w-1-basew[i], params, pub_seed, addr);
  167. }
  168. }