/* * aesbrute.c - A simple brute-force benchmark for demonstration purposes. * be@bjrn.se 12 June 2008. Public domain. */ #include #include #include #include "aes.h" /* http://fp.gladman.plus.com/AES/index.htm */ #define TEST_KEYS (1ULL << 24ULL) #define TEST_HIT 0x3fb8b8b8 #define TEST_NUM_THREADS 8 #define SECONDS_PER_DAY (60 * 60 * 24) void print_test_info() { printf("aesbrute.c - A simple brute-force benchmark "); printf("for demonstration purposes.\n"); printf("be@bjrn.se 12 June 2008. Public domain.\n\n"); printf("Brian Gladman AES-implementation 04-03-08.\n"); #if defined(ASM_X86_V1C) || defined(ASM_X86_V2) || defined(ASM_X86_V2C) || defined(ASM_AMD64_C) printf("Asm implementation.\n"); #else printf("Non-asm implementation.\n"); #endif printf("Built with gcc -03 GCC 3.4.2 (mingw-special) on Pentium 4.\n"); printf("%d threads.\n", TEST_NUM_THREADS); printf("\n"); } void print_result(unsigned long long cnt, double interval) { double kps = (double)cnt / interval; printf("%llu keys tested\n", cnt); printf("Time %f seconds.\n", interval); printf("%f keys/s.\n", kps); printf("%f keys/day.\n", kps * SECONDS_PER_DAY); printf("%f days to brute-force 2^64 keys.\n", 18446744073709551615ULL / (kps * SECONDS_PER_DAY)); printf("%f days to brute-force 2^56 keys.\n", 72057594037927935ULL / (kps * SECONDS_PER_DAY)); /* printf("your computer is approx %f times slower than built for 10 000 USD spring 2007.\n", 65156244608.0 / kps); */ } unsigned long long total_cnt = 0; static void *brute(void *args) { aes_decrypt_ctx ctx; unsigned char in[16], out[16], key[16]; int i, hit = 0; long long cnt = 0; unsigned long long *int_key = (unsigned long long *)key; unsigned int *int_out = (unsigned int *)out; for (i = 0; i < 16; i++) { key[i] = 0; in[i] = 'a'; } for (int_key[0] = 0; int_key[0] <= TEST_KEYS;) { /* unrolled. */ aes_decrypt_key128(key, &ctx); aes_decrypt(in, out, &ctx); if (int_out[0] == TEST_HIT) hit++; int_key[0]++; cnt++; aes_decrypt_key128(key, &ctx); aes_decrypt(in, out, &ctx); if (int_out[0] == TEST_HIT) hit++; int_key[0]++; cnt++; aes_decrypt_key128(key, &ctx); aes_decrypt(in, out, &ctx); if (int_out[0] == TEST_HIT) hit++; int_key[0]++; cnt++; aes_decrypt_key128(key, &ctx); aes_decrypt(in, out, &ctx); if (int_out[0] == TEST_HIT) hit++; int_key[0]++; cnt++; } total_cnt += cnt; return NULL; } int main() { pthread_t thread[TEST_NUM_THREADS]; clock_t start, stop; double interval; int i; print_test_info(); aes_init(); start = clock(); for (i = 0; i < TEST_NUM_THREADS-1; i++) { if (pthread_create(&thread[i], NULL, brute, NULL) != 0) return 1; } brute(NULL); /* Wait. */ for (i = 0; i < TEST_NUM_THREADS-1; i++) { //printf("Waiting for thread %d.\n", i); if (pthread_join(thread[i], NULL) != 0) return 1; } stop = clock(); interval = (double)(stop - start) / (double)CLOCKS_PER_SEC; print_result(TEST_KEYS * TEST_NUM_THREADS, interval); return 0; }