vanitygen.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #include "vanity.hpp"
  2. #include <regex>
  3. #include <mutex>
  4. #include <getopt.h>
  5. static struct
  6. {
  7. bool reg=false;
  8. int threads=-1;
  9. i2p::data::SigningKeyType signature;
  10. std::string outputpath="";
  11. std::regex regex;
  12. bool sig_type = true;
  13. } options;
  14. void check_sig_type()
  15. {
  16. if (SigTypeToName(options.signature).find("unknown") != std::string::npos)
  17. {
  18. std::cerr << "Incorrect signature type" << std::endl;
  19. options.sig_type = false;
  20. }
  21. }
  22. static void inline CalculateW (const uint8_t block[64], uint32_t W[64])
  23. {
  24. /**
  25. * implementation of orignal
  26. */
  27. for (int i = 0; i < 16; i++)
  28. #ifdef _WIN32
  29. W[i] = htobe32(((uint32_t *)(block))[i]);
  30. #else // from big endian to little endian ( swap )
  31. W[i] = be32toh(((uint32_t *)(block))[i]);
  32. #endif
  33. for (int i = 16; i < 64; i++)
  34. W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
  35. }
  36. static void inline TransformBlock (uint32_t state[8], const uint32_t W[64])
  37. {
  38. /**
  39. * implementation of orignal
  40. */
  41. uint32_t S[8];
  42. memcpy(S, state, 32);
  43. uint32_t t0, t1;
  44. RNDr(S, W, 0, 0x428a2f98); RNDr(S, W, 1, 0x71374491); RNDr(S, W, 2, 0xb5c0fbcf); RNDr(S, W, 3, 0xe9b5dba5);
  45. RNDr(S, W, 4, 0x3956c25b); RNDr(S, W, 5, 0x59f111f1); RNDr(S, W, 6, 0x923f82a4); RNDr(S, W, 7, 0xab1c5ed5);
  46. RNDr(S, W, 8, 0xd807aa98); RNDr(S, W, 9, 0x12835b01); RNDr(S, W, 10, 0x243185be); RNDr(S, W, 11, 0x550c7dc3);
  47. RNDr(S, W, 12, 0x72be5d74); RNDr(S, W, 13, 0x80deb1fe); RNDr(S, W, 14, 0x9bdc06a7); RNDr(S, W, 15, 0xc19bf174);
  48. RNDr(S, W, 16, 0xe49b69c1); RNDr(S, W, 17, 0xefbe4786); RNDr(S, W, 18, 0x0fc19dc6); RNDr(S, W, 19, 0x240ca1cc);
  49. RNDr(S, W, 20, 0x2de92c6f); RNDr(S, W, 21, 0x4a7484aa); RNDr(S, W, 22, 0x5cb0a9dc); RNDr(S, W, 23, 0x76f988da);
  50. RNDr(S, W, 24, 0x983e5152); RNDr(S, W, 25, 0xa831c66d); RNDr(S, W, 26, 0xb00327c8); RNDr(S, W, 27, 0xbf597fc7);
  51. RNDr(S, W, 28, 0xc6e00bf3); RNDr(S, W, 29, 0xd5a79147); RNDr(S, W, 30, 0x06ca6351); RNDr(S, W, 31, 0x14292967);
  52. RNDr(S, W, 32, 0x27b70a85); RNDr(S, W, 33, 0x2e1b2138); RNDr(S, W, 34, 0x4d2c6dfc); RNDr(S, W, 35, 0x53380d13);
  53. RNDr(S, W, 36, 0x650a7354); RNDr(S, W, 37, 0x766a0abb); RNDr(S, W, 38, 0x81c2c92e); RNDr(S, W, 39, 0x92722c85);
  54. RNDr(S, W, 40, 0xa2bfe8a1); RNDr(S, W, 41, 0xa81a664b); RNDr(S, W, 42, 0xc24b8b70); RNDr(S, W, 43, 0xc76c51a3);
  55. RNDr(S, W, 44, 0xd192e819); RNDr(S, W, 45, 0xd6990624); RNDr(S, W, 46, 0xf40e3585); RNDr(S, W, 47, 0x106aa070);
  56. RNDr(S, W, 48, 0x19a4c116); RNDr(S, W, 49, 0x1e376c08); RNDr(S, W, 50, 0x2748774c); RNDr(S, W, 51, 0x34b0bcb5);
  57. RNDr(S, W, 52, 0x391c0cb3); RNDr(S, W, 53, 0x4ed8aa4a); RNDr(S, W, 54, 0x5b9cca4f); RNDr(S, W, 55, 0x682e6ff3);
  58. RNDr(S, W, 56, 0x748f82ee); RNDr(S, W, 57, 0x78a5636f); RNDr(S, W, 58, 0x84c87814); RNDr(S, W, 59, 0x8cc70208);
  59. RNDr(S, W, 60, 0x90befffa); RNDr(S, W, 61, 0xa4506ceb); RNDr(S, W, 62, 0xbef9a3f7); RNDr(S, W, 63, 0xc67178f2);
  60. for (int i = 0; i < 8; i++) state[i] += S[i];
  61. }
  62. void inline HashNextBlock (uint32_t state[8], const uint8_t * block)
  63. {
  64. /**
  65. * implementation of orignal
  66. */
  67. uint32_t W[64];
  68. CalculateW (block, W);
  69. TransformBlock (state, W);
  70. }
  71. static bool check_prefix(const char * buf)
  72. {
  73. unsigned short size_str=0;
  74. while(*buf)
  75. {
  76. if(!((*buf > 49 && *buf < 56) || (*buf > 96 && *buf < 123)) || size_str > 52)
  77. return false;
  78. size_str++;
  79. buf++;
  80. }
  81. return true;
  82. }
  83. static inline size_t ByteStreamToBase32 (const uint8_t * inBuf, size_t len, char * outBuf, size_t outLen)
  84. {
  85. size_t ret = 0, pos = 1;
  86. int bits = 8, tmp = inBuf[0];
  87. while (ret < outLen && (bits > 0 || pos < len))
  88. {
  89. if (bits < 5)
  90. {
  91. if (pos < len)
  92. {
  93. tmp <<= 8;
  94. tmp |= inBuf[pos] & 0xFF;
  95. pos++;
  96. bits += 8;
  97. }
  98. else // last byte
  99. {
  100. tmp <<= (5 - bits);
  101. bits = 5;
  102. }
  103. }
  104. bits -= 5;
  105. int ind = (tmp >> bits) & 0x1F;
  106. outBuf[ret] = (ind < 26) ? (ind + 'a') : ((ind - 26) + '2');
  107. ret++;
  108. }
  109. outBuf[ret]='\0';
  110. return ret;
  111. }
  112. static inline bool NotThat(const char * what, const std::regex & reg){
  113. return std::regex_match(what,reg) == 1 ? false : true;
  114. }
  115. static inline bool NotThat(const char * a, const char *b)
  116. {
  117. while(*b)
  118. if(*a++!=*b++)
  119. return true;
  120. return false;
  121. }
  122. std::mutex mtx;
  123. static inline bool thread_find(uint8_t * buf, const char * prefix, int id_thread, unsigned long long throughput)
  124. {
  125. /**
  126. * Thanks to orignal ^-^
  127. * For idea and example ^-^
  128. * Orignal is sensei of crypto ;)
  129. */
  130. mtx.lock();
  131. std::cout << "[+] thread " << id_thread << " bound" << std::endl;
  132. mtx.unlock();
  133. /*
  134. union
  135. {
  136. uint8_t b[391];
  137. uint32_t ll;
  138. } local;
  139. union
  140. {
  141. uint8_t b[32];
  142. uint32_t ll[8];
  143. } hash;
  144. */
  145. uint8_t b[391];
  146. uint32_t hash[8];
  147. memcpy (b, buf, 391);
  148. size_t len = 52;
  149. if (!options.reg)
  150. len = strlen(prefix);
  151. // precalculate first 5 blocks (320 bytes)
  152. uint32_t state[8] = { 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 };
  153. HashNextBlock (state, b);
  154. HashNextBlock (state, b + 64);
  155. HashNextBlock (state, b + 128);
  156. HashNextBlock (state, b + 192);
  157. HashNextBlock (state, b + 256);
  158. // pre-calculate last W
  159. uint32_t lastW[64];
  160. CalculateW (lastBlock, lastW);
  161. uint32_t * nonce = (uint32_t *)(b+320);
  162. (*nonce) += id_thread*throughput;
  163. char addr[53];
  164. uint32_t state1[8];
  165. while(throughput-- and !found)
  166. {
  167. memcpy (state1, state, 32);
  168. // calculate hash of block with nonce
  169. HashNextBlock (state1, b + 320);
  170. // apply last block
  171. TransformBlock (state1, lastW);
  172. // get final hash
  173. for (int j = 8; j--;)
  174. hash[j] = htobe32(state1[j]);
  175. ByteStreamToBase32 ((uint8_t*)hash, 32, addr, len);
  176. // std::cout << addr << std::endl;
  177. // bool result = options.reg ? !NotThat(addr, &options.regex) : !NotThat(addr,prefix);
  178. if( ( options.reg ? !NotThat(addr, options.regex) : !NotThat(addr,prefix) ) )
  179. // if(result)
  180. {
  181. ByteStreamToBase32 ((uint8_t*)hash, 32, addr, 52);
  182. std::cout << "\nFound address: " << addr << std::endl;
  183. found=true;
  184. FoundNonce=*nonce;
  185. // free(hash);
  186. // free(b);
  187. return true;
  188. }
  189. (*nonce)++;
  190. hashescounter++;
  191. if (found)
  192. {
  193. // free(hash);
  194. // free(b);
  195. break;
  196. }
  197. } // while
  198. return true;
  199. }
  200. void usage(void){
  201. const constexpr char * help="Usage:\n"
  202. " vain [text-pattern|regex-pattern] [options]\n\n"
  203. "OPTIONS:\n"
  204. " -h --help show this help (same as --usage)\n"
  205. " -r --reg use regexp instead of simple text pattern, ex.: vain '(one|two).*' -r\n"
  206. " -t --threads number of threads to use (default: one per processor)\n"
  207. // " -s --signature (signature type)\n" // NOT IMPLEMENTED FUCKING PLAZ!
  208. " -o --output privkey output file name (default: ./" DEF_OUTNAME ")\n"
  209. "";
  210. puts(help);
  211. }
  212. void parsing(int argc, char ** args){
  213. int option_index;
  214. static struct option long_options[]={
  215. {"help",no_argument,0,'h'},
  216. {"reg", no_argument,0,'r'},
  217. {"threads", required_argument, 0, 't'},
  218. {"signature", required_argument,0,'s'},
  219. {"output", required_argument,0,'o'},
  220. {"usage", no_argument,0,0},
  221. {0,0,0,0}
  222. };
  223. int c;
  224. while( (c=getopt_long(argc,args, "hrt:s:o:", long_options, &option_index))!=-1){
  225. switch(c){
  226. case 0:
  227. if ( std::string(long_options[option_index].name) == std::string("usage") ){
  228. usage();
  229. exit(1);
  230. }
  231. case 'h':
  232. usage();
  233. exit(0);
  234. break;
  235. case 'r':
  236. options.reg=true;
  237. break;
  238. case 't':
  239. options.threads=atoi(optarg);
  240. break;
  241. case 's':
  242. options.signature = NameToSigType(std::string(optarg));
  243. check_sig_type();
  244. break;
  245. case 'o':
  246. options.outputpath=optarg;
  247. break;
  248. case '?':
  249. std::cerr << "Undefined argument" << std::endl;
  250. default:
  251. std::cerr << args[0] << " --usage / --help" << std::endl;
  252. exit(1);
  253. break;
  254. }
  255. }
  256. }
  257. int main (int argc, char * argv[])
  258. {
  259. if ( argc < 2 )
  260. {
  261. usage();
  262. return 0;
  263. }
  264. parsing( argc > 2 ? argc-1 : argc, argc > 2 ? argv+1 : argv);
  265. //
  266. if(!options.reg && !check_prefix( argv[1] ))
  267. {
  268. std::cout << "Invalid pattern." << std::endl;
  269. usage();
  270. return 1;
  271. }else{
  272. options.regex=std::regex(argv[1]);
  273. // int ret = regcomp( &options.regex, argv[1], REG_EXTENDED );
  274. // if( ret != 0 ){
  275. // std::cerr << "Can't create regexp pattern from " << argv[1] << std::endl;
  276. // return 1;
  277. // }
  278. }
  279. i2p::crypto::InitCrypto (false, true, true, false);
  280. options.signature = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519;
  281. ///////////////
  282. //For while
  283. if(options.signature != i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519)
  284. {
  285. std::cout << "ED25519-SHA512 are currently the only signing keys supported." << std::endl;
  286. return 0;
  287. }
  288. ///////////////
  289. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  290. if (!options.sig_type) return -2;
  291. auto keys = i2p::data::PrivateKeys::CreateRandomKeys (options.signature);
  292. switch(options.signature)
  293. {
  294. case i2p::data::SIGNING_KEY_TYPE_DSA_SHA1:
  295. case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
  296. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048:
  297. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072:
  298. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096:
  299. case i2p::data::SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
  300. std::cout << "Sorry, selected signature type is not supported for address generation." << std::endl;
  301. return 0;
  302. break;
  303. }
  304. //TODO: for other types.
  305. switch(options.signature)
  306. {
  307. case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
  308. break;
  309. case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
  310. break;
  311. case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
  312. break;
  313. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048:
  314. break;
  315. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072:
  316. break;
  317. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096:
  318. break;
  319. case i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
  320. MutateByte=320;
  321. break;
  322. case i2p::data::SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
  323. break;
  324. }
  325. KeyBuf = new uint8_t[keys.GetFullLen()];
  326. keys.ToBuffer (KeyBuf, keys.GetFullLen ());
  327. if(options.threads <= 0)
  328. {
  329. #if defined(WIN32)
  330. SYSTEM_INFO siSysInfo;
  331. GetSystemInfo(&siSysInfo);
  332. options.threads = siSysInfo.dwNumberOfProcessors;
  333. #elif defined(_SC_NPROCESSORS_CONF)
  334. options.threads = sysconf(_SC_NPROCESSORS_CONF);
  335. #elif defined(HW_NCPU)
  336. int req[] = { CTL_HW, HW_NCPU };
  337. size_t len = sizeof(options.threads);
  338. v = sysctl(req, 2, &options.threads, &len, NULL, 0);
  339. #else
  340. options.threads = 1;
  341. #endif
  342. }
  343. std::cout << "Initializing vanity generator (" << options.threads << " threads)\n" << std::endl;
  344. unsigned short attempts = 0;
  345. while(!found)
  346. { // while
  347. { // stack(for destructors(vector/thread))
  348. std::vector<std::thread> threads(options.threads);
  349. unsigned long long thoughtput = 0x4F4B5A37;
  350. std::cout << "Starting attempt #" << ++attempts << ":" << std::endl;
  351. for (unsigned int j = options.threads;j--;)
  352. {
  353. threads[j] = std::thread(thread_find,KeyBuf,argv[1],j,thoughtput);
  354. thoughtput+=1000;
  355. } // for
  356. for (unsigned int j = 0; j < (unsigned int)options.threads;j++)
  357. threads[j].join();
  358. if (FoundNonce == 0)
  359. {
  360. RAND_bytes( KeyBuf+MutateByte , 90 );
  361. }
  362. } // stack
  363. } // while
  364. memcpy (KeyBuf + MutateByte, &FoundNonce, 4);
  365. std::cout << "Hashes calculated: " << hashescounter << std::endl;
  366. if(options.outputpath.size() == 0) options.outputpath.assign(DEF_OUTNAME);
  367. std::ofstream f (options.outputpath, std::ofstream::binary | std::ofstream::out);
  368. if (f)
  369. {
  370. f.write ((char *)KeyBuf, keys.GetFullLen ());
  371. delete [] KeyBuf;
  372. }
  373. else
  374. std::cout << "Can't create output file: " << options.outputpath << std::endl;
  375. i2p::crypto::TerminateCrypto ();
  376. return 0;
  377. }