diff options
author | David Phillips <david@sighup.nz> | 2017-05-29 10:31:47 +1200 |
---|---|---|
committer | David Phillips <david@sighup.nz> | 2017-05-29 10:31:47 +1200 |
commit | f9ddbae280128266f332d4ad7022ee6ff8c281b3 (patch) | |
tree | 2e9226fb20614806550e5472ea32ddf533095a06 | |
parent | d96698e4a16b24cf9c87b8e4d8c0d15f92e497fb (diff) | |
download | sand-leek-f9ddbae280128266f332d4ad7022ee6ff8c281b3.tar.xz |
Remove bitmask and raw_len computation out of tight loop
-rw-r--r-- | sand-leek.c | 59 |
1 files changed, 27 insertions, 32 deletions
diff --git a/sand-leek.c b/sand-leek.c index 8de21fc..7f94126 100644 --- a/sand-leek.c +++ b/sand-leek.c @@ -23,27 +23,17 @@ #define RSA_KEY_BITS 1024 -static char bitmasks[] = { - [1] = 0xF8, /* 5 MSB */ - [2] = 0xC0, /* 2 MSB */ - [3] = 0xFE, /* 7 MSB */ - [4] = 0xF0, /* 4 MSB */ - [5] = 0x80, /* 1 MSB */ - [6] = 0xFC, /* 6 MSB */ - [7] = 0xE0 /* 3 MSB */ -}; - - static char *search; static char search_pad[16]; static unsigned char search_raw[10]; static size_t search_len; +static int raw_len; +static char bitmask; sem_t working; void* work(void *arg) { char onion[17]; - char bitmask; unsigned char sha[20]; unsigned long e = EXPONENT_MIN; unsigned int e_big_endian = 0; @@ -115,25 +105,6 @@ work(void *arg) { goto STOP; } - int raw_len = 0; - switch (search_len) { - case 1: raw_len = 0; break; - case 2: - case 3: raw_len = 1; break; - case 4: raw_len = 2; break; - case 5: - case 6: raw_len = 3; break; - case 7: raw_len = 4; break; - case 8: - case 9: raw_len = 5; break; - case 10: raw_len = 6; break; - case 11: raw_len = 6; break; - case 12: raw_len = 7; break; - case 13: - case 14: raw_len = 8; break; - case 15: raw_len = 9; break; - case 16: raw_len = 10; break; - } if (memcmp(sha, search_raw, raw_len) == 0) { /* check the remaining partial byte */ switch (search_len) { @@ -142,7 +113,6 @@ work(void *arg) { /* nothing to do; already a raw byte boundary */ break; default: - bitmask = bitmasks[search_len % 8]; if ((search_raw[raw_len] & bitmask) != (sha[raw_len] & bitmask)) { e += 2; continue; @@ -205,6 +175,27 @@ STOP: return NULL; } +int +set_raw_params(void) { + /* bitmasks to be used to compare remainder bits */ + static char bitmasks[] = { + [1] = 0xF8, /* 5 MSB */ + [2] = 0xC0, /* 2 MSB */ + [3] = 0xFE, /* 7 MSB */ + [4] = 0xF0, /* 4 MSB */ + [5] = 0x80, /* 1 MSB */ + [6] = 0xFC, /* 6 MSB */ + [7] = 0xE0 /* 3 MSB */ + }; + + /* number of whole bytes of raw hash to compare: + * 10 is the size of the data a full onion address covers + * 16 is the size of the base32-encoded onion address */ + raw_len = (search_len*10)/16; + bitmask = bitmasks[search_len % 8]; + return 0; +} + void die_usage(const char *argv0) { fprintf(stderr, @@ -290,6 +281,10 @@ main(int argc, char **argv) { return 1; } + if (set_raw_params()) { + fprintf(stderr, "Search string of poor length\n"); + return 1; + } memset(search_pad, 0, sizeof(search_pad)); strncpy(search_pad, search, sizeof(search_pad)); |