|
| 1 | +/* strkey -- String in Public Key |
| 2 | + * |
| 3 | + * Generates Tox's key pairs, checking if a certain string is in the public key. |
| 4 | + * |
| 5 | + * Requires sodium or nacl library. |
| 6 | + * |
| 7 | + * There seem to be some problems with the code working on Windows -- it works |
| 8 | + * when built in debug mode with MinGW 4.8, but it doesn't work correctly when |
| 9 | + * built in release. |
| 10 | + * |
| 11 | + * Usage: strkey <offset> <string> |
| 12 | + * |
| 13 | + * Offset - an integer specifying exact byte offset position of the string you |
| 14 | + * are looking for within a public key. When offset is negative, the program |
| 15 | + * just looks for the desired string being somewhere, doesn't matter where, in |
| 16 | + * the public key. |
| 17 | + * |
| 18 | + * String - a hex string that you want to have in your public key. It must have |
| 19 | + * an even number of letters, since every two hexes map to a single byte of |
| 20 | + * the public key. |
| 21 | + * |
| 22 | + * Examples: |
| 23 | + * strkey 0 0123 |
| 24 | + * Looks for a public key that begins with "0123". |
| 25 | + * |
| 26 | + * strkey 1 0123 |
| 27 | + * Looks for a public key that has "0123" starting at its second byte, i.e. "XX0123...". |
| 28 | + * |
| 29 | + * strkey 2 0123 |
| 30 | + * Looks for a public key that has "0123" starting at its third byte, i.e. "XXXX0123...". |
| 31 | + * (each two hexes represent a single byte of a public key) |
| 32 | + * |
| 33 | + * strkey -1 AF57CC |
| 34 | + * Looks for a public key that contains "AF57CC", regardless of its position. |
| 35 | + * |
| 36 | + * To compile with gcc and sodium: gcc strkey.c -o strkey -lsodium |
| 37 | +*/ |
| 38 | + |
| 39 | +#include <stdio.h> |
| 40 | +#include <string.h> |
| 41 | + |
| 42 | +#include <sodium.h> |
| 43 | + |
| 44 | +#define PRINT_TRIES_COUNT |
| 45 | + |
| 46 | +void print_key(unsigned char *key) |
| 47 | +{ |
| 48 | + size_t i; |
| 49 | + for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++) { |
| 50 | + if (key[i] < 16) { |
| 51 | + fprintf(stdout, "0"); |
| 52 | + } |
| 53 | + |
| 54 | + fprintf(stdout, "%hhX", key[i]); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +int main(int argc, char *argv[]) |
| 59 | +{ |
| 60 | + unsigned char public_key[crypto_box_PUBLICKEYBYTES]; // null terminator |
| 61 | + unsigned char secret_key[crypto_box_SECRETKEYBYTES]; |
| 62 | + int offset = 0; |
| 63 | + size_t len; |
| 64 | + unsigned char desired_bin[crypto_box_PUBLICKEYBYTES]; // null terminator |
| 65 | + |
| 66 | + if (argc == 3) { |
| 67 | + offset = atoi(argv[1]); |
| 68 | + char *desired_hex = argv[2]; |
| 69 | + len = strlen(desired_hex); |
| 70 | + if (len % 2 != 0) { |
| 71 | + fprintf(stderr, "Desired key should have an even number of letters\n"); |
| 72 | + exit(1); |
| 73 | + } |
| 74 | + size_t block_length = (offset < 0 ? 0 : offset) + len/2; |
| 75 | + if (block_length > crypto_box_PUBLICKEYBYTES) { |
| 76 | + fprintf(stderr, "The given key with the given offset exceed public key's length\n"); |
| 77 | + exit(1); |
| 78 | + } |
| 79 | + |
| 80 | + // convert hex to bin |
| 81 | + char *pos = desired_hex; |
| 82 | + size_t i; |
| 83 | + for (i = 0; i < len; pos += 2) { |
| 84 | + sscanf(pos, "%2hhx", &desired_bin[i]); |
| 85 | + ++i; |
| 86 | + } |
| 87 | + } else { |
| 88 | + fprintf(stdout, "Usage: executable <byte offset> <desired hex string with even number of letters>\n"); |
| 89 | + exit(1); |
| 90 | + } |
| 91 | + |
| 92 | + len /= 2; |
| 93 | + |
| 94 | +#ifdef PRINT_TRIES_COUNT |
| 95 | + long long unsigned int tries = 0; |
| 96 | +#endif |
| 97 | + |
| 98 | + if (offset < 0) { |
| 99 | + int found = 0; |
| 100 | + do { |
| 101 | +#ifdef PRINT_TRIES_COUNT |
| 102 | + tries ++; |
| 103 | +#endif |
| 104 | + crypto_box_keypair(public_key, secret_key); |
| 105 | + int i; |
| 106 | + for (i = 0; i <= crypto_box_PUBLICKEYBYTES - len; i ++) { |
| 107 | + if (memcmp(public_key + i, desired_bin, len) == 0) { |
| 108 | + found = 1; |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + } while (!found); |
| 113 | + } else { |
| 114 | + unsigned char *p = public_key + offset; |
| 115 | + |
| 116 | + do { |
| 117 | +#ifdef PRINT_TRIES_COUNT |
| 118 | + tries ++; |
| 119 | +#endif |
| 120 | + crypto_box_keypair(public_key, secret_key); |
| 121 | + } while (memcmp(p, desired_bin, len) != 0); |
| 122 | + } |
| 123 | + |
| 124 | + fprintf(stdout, "Public key: "); |
| 125 | + print_key(public_key); |
| 126 | + fprintf(stdout, "\n"); |
| 127 | + |
| 128 | + fprintf(stdout, "Private key: "); |
| 129 | + print_key(secret_key); |
| 130 | + fprintf(stdout, "\n"); |
| 131 | + |
| 132 | +#ifdef PRINT_TRIES_COUNT |
| 133 | + fprintf(stdout, "Found the key pair on %llu try.\n", tries); |
| 134 | +#endif |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments