worker.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #define _POSIX_C_SOURCE 200112L
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <pthread.h>
  8. #include <sodium/randombytes.h>
  9. #ifdef PASSPHRASE
  10. #include <sodium/crypto_hash_sha256.h>
  11. #endif
  12. #include <sodium/utils.h>
  13. #include "types.h"
  14. #include "likely.h"
  15. #include "vec.h"
  16. #include "base32.h"
  17. #include "keccak.h"
  18. #include "ioutil.h"
  19. #include "common.h"
  20. #include "yaml.h"
  21. #include "worker.h"
  22. #include "filters.h"
  23. #ifndef _WIN32
  24. #define FSZ "%zu"
  25. #else
  26. #define FSZ "%Iu"
  27. #endif
  28. // additional 0 terminator is added by C
  29. const char * const pkprefix = "== ed25519v1-public: type0 ==\0\0";
  30. const char * const skprefix = "== ed25519v1-secret: type0 ==\0\0";
  31. static const char checksumstr[] = ".onion checksum";
  32. #define checksumstrlen (sizeof(checksumstr) - 1) // 15
  33. pthread_mutex_t keysgenerated_mutex;
  34. volatile size_t keysgenerated = 0;
  35. volatile int endwork = 0;
  36. int yamloutput = 0;
  37. int yamlraw = 0;
  38. int numwords = 1;
  39. size_t numneedgenerate = 0;
  40. // output directory
  41. char *workdir = 0;
  42. size_t workdirlen = 0;
  43. #ifdef PASSPHRASE
  44. // How many times we loop before a reseed
  45. #define DETERMINISTIC_LOOP_COUNT (1<<24)
  46. pthread_mutex_t determseed_mutex;
  47. u8 determseed[SEED_LEN];
  48. int pw_skipnear = 0;
  49. int pw_warnnear = 0;
  50. #endif
  51. char *makesname(void)
  52. {
  53. char *sname = (char *) malloc(workdirlen + ONION_LEN + 63 + 1);
  54. if (!sname)
  55. abort();
  56. if (workdir)
  57. memcpy(sname,workdir,workdirlen);
  58. return sname;
  59. }
  60. static void onionready(char *sname,const u8 *secret,const u8 *pubonion,int warnnear)
  61. {
  62. if (endwork)
  63. return;
  64. if (numneedgenerate) {
  65. pthread_mutex_lock(&keysgenerated_mutex);
  66. if (keysgenerated >= numneedgenerate) {
  67. pthread_mutex_unlock(&keysgenerated_mutex);
  68. return;
  69. }
  70. ++keysgenerated;
  71. if (keysgenerated == numneedgenerate)
  72. endwork = 1;
  73. pthread_mutex_unlock(&keysgenerated_mutex);
  74. }
  75. // disabled as this was never ever triggered as far as I'm aware
  76. #if 0
  77. // Sanity check that the public key matches the private one.
  78. ge_p3 ALIGN(16) point;
  79. u8 testpk[PUBLIC_LEN];
  80. ge_scalarmult_base(&point,&secret[SKPREFIX_SIZE]);
  81. ge_p3_tobytes(testpk,&point);
  82. if (memcmp(testpk,&pubonion[PKPREFIX_SIZE],PUBLIC_LEN) != 0)
  83. abort();
  84. #endif
  85. if (!yamloutput) {
  86. if (createdir(sname,1) != 0) {
  87. pthread_mutex_lock(&fout_mutex);
  88. fprintf(stderr,"ERROR: could not create directory \"%s\" for key output\n",sname);
  89. pthread_mutex_unlock(&fout_mutex);
  90. return;
  91. }
  92. strcpy(&sname[onionendpos],"/hs_ed25519_secret_key");
  93. writetofile(sname,secret,FORMATTED_SECRET_LEN,1);
  94. strcpy(&sname[onionendpos],"/hs_ed25519_public_key");
  95. writetofile(sname,pubonion,FORMATTED_PUBLIC_LEN,0);
  96. strcpy(&sname[onionendpos],"/hostname");
  97. FILE *hfile = fopen(sname,"w");
  98. sname[onionendpos] = '\n';
  99. if (hfile) {
  100. fwrite(&sname[direndpos],ONION_LEN + 1,1,hfile);
  101. fclose(hfile);
  102. }
  103. if (fout) {
  104. pthread_mutex_lock(&fout_mutex);
  105. #ifdef PASSPHRASE
  106. const char * const pwarn = " warn:near\n";
  107. if (warnnear)
  108. strcpy(&sname[onionendpos],pwarn);
  109. const size_t oprintlen = printlen;
  110. const size_t printlen = oprintlen + (warnnear ? strlen(pwarn)-1 : 0);
  111. #else
  112. (void) warnnear;
  113. #endif
  114. fwrite(&sname[printstartpos],printlen,1,fout);
  115. fflush(fout);
  116. pthread_mutex_unlock(&fout_mutex);
  117. }
  118. }
  119. else
  120. yamlout_writekeys(&sname[direndpos],pubonion,secret,yamlraw);
  121. }
  122. #include "filters_inc.inc.h"
  123. #include "filters_worker.inc.h"
  124. #ifdef STATISTICS
  125. #define ADDNUMSUCCESS ++st->numsuccess.v
  126. #else
  127. #define ADDNUMSUCCESS do ; while (0)
  128. #endif
  129. union pubonionunion {
  130. u8 raw[PKPREFIX_SIZE + PUBLIC_LEN + 32];
  131. struct {
  132. u64 prefix[4];
  133. u64 key[4];
  134. u64 hash[4];
  135. } i;
  136. } ;
  137. /*
  138. // little endian inc
  139. static void addsk32(u8 *sk)
  140. {
  141. register unsigned int c = 8;
  142. for (size_t i = 0;i < 32;++i) {
  143. c = (unsigned int)sk[i] + c; sk[i] = c & 0xFF; c >>= 8;
  144. // unsure if needed
  145. if (!c) break;
  146. }
  147. }
  148. */
  149. // 0123 4567 xxxx --3--> 3456 7xxx
  150. // 0123 4567 xxxx --1--> 1234 567x
  151. static inline void shiftpk(u8 *dst,const u8 *src,size_t sbits)
  152. {
  153. size_t i,sbytes = sbits / 8;
  154. sbits %= 8;
  155. for (i = 0;i + sbytes < PUBLIC_LEN;++i) {
  156. dst[i] = (u8) ((src[i+sbytes] << sbits) |
  157. (src[i+sbytes+1] >> (8 - sbits)));
  158. }
  159. for(;i < PUBLIC_LEN;++i)
  160. dst[i] = 0;
  161. }
  162. // in little-endian order, 32 bytes aka 256 bits
  163. static void addsztoscalar32(u8 *dst,size_t v)
  164. {
  165. int i;
  166. u32 c = 0;
  167. for (i = 0;i < 32;++i) {
  168. c += *dst + (v & 0xFF); *dst = c & 0xFF; c >>= 8;
  169. v >>= 8;
  170. ++dst;
  171. }
  172. }
  173. #ifdef PASSPHRASE
  174. static void reseedright(u8 sk[SECRET_LEN])
  175. {
  176. crypto_hash_sha256_state state;
  177. crypto_hash_sha256_init(&state);
  178. // old right side
  179. crypto_hash_sha256_update(&state,&sk[32],32);
  180. // new random data
  181. randombytes(&sk[32],32);
  182. crypto_hash_sha256_update(&state,&sk[32],32);
  183. // put result in right side
  184. crypto_hash_sha256_final(&state,&sk[32]);
  185. }
  186. #endif // PASSPHRASE
  187. #if !defined(BATCHNUM)
  188. #define BATCHNUM 2048
  189. #endif
  190. #include "ed25519/ed25519.h"
  191. #include "worker_impl.inc.h"
  192. size_t worker_batch_memuse(void)
  193. {
  194. size_t s = 0,x;
  195. #ifdef ED25519_ref10
  196. x = crypto_sign_ed25519_ref10_worker_batch_memuse();
  197. if (x > s)
  198. s = x;
  199. #endif
  200. #ifdef ED25519_amd64_51_30k
  201. x = crypto_sign_ed25519_amd64_51_30k_worker_batch_memuse();
  202. if (x > s)
  203. s = x;
  204. #endif
  205. #ifdef ED25519_amd64_64_24k
  206. x = crypto_sign_ed25519_amd64_64_24k_worker_batch_memuse();
  207. if (x > s)
  208. s = x;
  209. #endif
  210. #ifdef ED25519_donna
  211. x = crypto_sign_ed25519_donna_worker_batch_memuse();
  212. if (x > s)
  213. s = x;
  214. #endif
  215. return s;
  216. }
  217. void worker_init(void)
  218. {
  219. #ifdef ED25519_ref10
  220. crypto_sign_ed25519_ref10_ge_initeightpoint();
  221. #endif
  222. #ifdef ED25519_amd64_51_30k
  223. crypto_sign_ed25519_amd64_51_30k_ge_initeightpoint();
  224. #endif
  225. #ifdef ED25519_amd64_64_24k
  226. crypto_sign_ed25519_amd64_64_24k_ge_initeightpoint();
  227. #endif
  228. #ifdef ED25519_donna
  229. crypto_sign_ed25519_donna_ge_initeightpoint();
  230. #endif
  231. }