bip39bruteforce.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include "bip32.h"
  5. #include "bip39.h"
  6. #include "curves.h"
  7. #include "ecdsa.h"
  8. #include "secp256k1.h"
  9. char iter[256];
  10. uint8_t seed[512 / 8];
  11. char addr[MAX_ADDR_SIZE];
  12. int count = 0, found = 0;
  13. HDNode node;
  14. clock_t start;
  15. #define ACCOUNT_LEGACY 0
  16. // around 280 tries per second
  17. // testing data:
  18. //
  19. // mnemonic: "all all all all all all all all all all all all"
  20. // address: legacy: "1JAd7XCBzGudGpJQSDSfpmJhiygtLQWaGL"
  21. // segwit: "3L6TyTisPBmrDAj6RoKmDzNnj4eQi54gD2"
  22. // passphrase: ""
  23. //
  24. // mnemonic: "all all all all all all all all all all all all"
  25. // address: legacy: "1N3uJ5AU3FTYQ1ZQgTMtYmgSvMBmQiGVBS"
  26. // segwit: "3NcXPfbDP4UHSbuHASALJEBtDeAcWYMMcS"
  27. // passphrase: "testing"
  28. int main(int argc, char **argv) {
  29. if (argc != 2 && argc != 3) {
  30. fprintf(stderr, "Usage: bip39bruteforce address [mnemonic]\n");
  31. return 1;
  32. }
  33. const char *address = argv[1];
  34. const char *mnemonic, *item;
  35. if (argc == 3) {
  36. mnemonic = argv[2];
  37. item = "passphrase";
  38. } else {
  39. mnemonic = NULL;
  40. item = "mnemonic";
  41. }
  42. if (mnemonic && !mnemonic_check(mnemonic)) {
  43. fprintf(stderr, "\"%s\" is not a valid mnemonic\n", mnemonic);
  44. return 2;
  45. }
  46. printf("Reading %ss from stdin ...\n", item);
  47. start = clock();
  48. for (;;) {
  49. if (fgets(iter, 256, stdin) == NULL) break;
  50. int len = strlen(iter);
  51. if (len <= 0) {
  52. continue;
  53. }
  54. count++;
  55. iter[len - 1] = 0;
  56. if (mnemonic) {
  57. mnemonic_to_seed(mnemonic, iter, seed, NULL);
  58. } else {
  59. mnemonic_to_seed(iter, "", seed, NULL);
  60. }
  61. hdnode_from_seed(seed, 512 / 8, SECP256K1_NAME, &node);
  62. #if ACCOUNT_LEGACY
  63. hdnode_private_ckd_prime(&node, 44);
  64. #else
  65. hdnode_private_ckd_prime(&node, 49);
  66. #endif
  67. hdnode_private_ckd_prime(&node, 0);
  68. hdnode_private_ckd_prime(&node, 0);
  69. hdnode_private_ckd(&node, 0);
  70. hdnode_private_ckd(&node, 0);
  71. hdnode_fill_public_key(&node);
  72. #if ACCOUNT_LEGACY
  73. // Legacy address
  74. ecdsa_get_address(node.public_key, 0, HASHER_SHA2_RIPEMD, HASHER_SHA2D,
  75. addr, sizeof(addr));
  76. #else
  77. // Segwit-in-P2SH
  78. ecdsa_get_address_segwit_p2sh(node.public_key, 5, HASHER_SHA2_RIPEMD,
  79. HASHER_SHA2D, addr, sizeof(addr));
  80. #endif
  81. if (strcmp(address, addr) == 0) {
  82. found = 1;
  83. break;
  84. }
  85. }
  86. float dur = (float)(clock() - start) / CLOCKS_PER_SEC;
  87. printf("Tried %d %ss in %f seconds = %f tries/second\n", count, item, dur,
  88. (float)count / dur);
  89. if (found) {
  90. printf("Correct %s found! :-)\n\"%s\"\n", item, iter);
  91. return 0;
  92. }
  93. printf("Correct %s not found. :-(\n", item);
  94. return 4;
  95. }