/* Public Domain */ #include #include #include #include #include #define MOT_KEY_SHA1_HASH "\x1d\x3f\xb6\x62\x79\x4d\x8c\x70\xfb\x57\xb4\xcb\x49\x2e\x27\xf6\x6f\x15\x2e\x4f" #define MOT_KEY_EXPONENT 0x10001 #define MOT_KEY_SIZE 2048 #ifdef USE_BITS static int max_bit_count = 0; #endif char diff[20]; struct sha1_hash { unsigned char* hex; short bits[160]; int bit_count; }; struct keys_pool { RSA* key; void* next; }; /* Function for init SHA1 hash fast-access structure * ------------------------------------------------------------------ * Take empty structure with fullfilled only string of hexadecimal * chars and fill other fields, e.g. bits[] array and bits_count * accumulator */ void sha1_hash_init(struct sha1_hash *hash) { int i, pos = 0; unsigned int *input= (unsigned int *)hash->hex; hash->bit_count = 0; for(i = 31; i >= 0; i--) { if (((*input >> i) & 1)) { hash->bits[pos] = 1; hash->bit_count++; } else hash->bits[pos] = 0; pos++; } } /* Function for compare two sha1 hashes. * ------------------------------------------------------------------- * Return 1 if hashes identical * Return 0 if hashes differs */ int sha1_hash_compare_hex(struct sha1_hash *hash1, struct sha1_hash *hash2) { int i; for (i = 0; hash1->hex[i]; i++ ) if ( (hash1->hex[i] & hash2->hex[i]) != hash1->hex[i] ) { diff[i] = hash1->hex[i] ^ hash2->hex[i]; printf("out %d: %x %x\n", i, hash1->hex[i], hash2->hex[i]); printf("differs in %d byte: %s\n", i + 1, diff); return 0; } return 1; } unsigned char *DER_encode_RSA_public(RSA *rsa, int *len) { unsigned char *buf, *next; next = NULL; *len = i2d_RSAPublicKey(rsa, &next); return next; } // TODO: Make keys generation as different thread, // which can generate pool of the RSA keys. void generate_keys(struct keys_pool *pool) { pool->key = RSA_generate_key(MOT_KEY_EXPONENT, MOT_KEY_SIZE, NULL, NULL); } int main(void) { RSA* key; const unsigned char* key_data = NULL; struct sha1_hash *origin; struct sha1_hash *hash; struct keys_pool *pool; struct diff_buffer *diff; FILE *fp; int i = 0; int key_size = 0; int n = 0; hash = malloc(sizeof(*hash)); origin = malloc(sizeof(*origin)); pool = malloc(sizeof(*pool)); diff = malloc(sizeof(diff)); origin->hex = MOT_KEY_SHA1_HASH; sha1_hash_init(origin); while (1) { //generate_keys(pool); pool->key = RSA_generate_key(MOT_KEY_SIZE,MOT_KEY_EXPONENT, NULL, NULL); key_size = RSA_size(pool->key); key_data = DER_encode_RSA_public(pool->key, &key_size); hash->hex = SHA1(key_data, key_size, NULL); if (sha1_hash_compare_hex(origin, hash) == 1) { printf("key found!\n"); RSA_print_fp(stdout, key, 3); return EXIT_SUCCESS; } RSA_free(pool->key); n++; if ( !(n % 10) ) { printf("%d keys done!\n", n); } } return EXIT_SUCCESS; }