Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Key schedule benchmarks
- * cc -O3 bench.c -lgcrypt
- */
- #define _POSIX_C_SOURCE 200112L
- #include <time.h>
- #include <stdio.h>
- #include <gcrypt.h>
- static unsigned long long
- utime(void)
- {
- struct timespec tv[1];
- clock_gettime(CLOCK_REALTIME, tv);
- return tv->tv_sec * 1000000ULL + tv->tv_nsec / 1000ULL;
- }
- static double
- bench(long n, int algo, int keylen, int blocklen)
- {
- unsigned char block[16] = {0};
- static const unsigned char key[32] = {
- 0x15, 0x86, 0x14, 0xf5, 0x09, 0x79, 0x3e, 0x9b,
- 0x52, 0x96, 0x9c, 0xd4, 0x59, 0xa1, 0x4c, 0x7c,
- 0xfc, 0x92, 0x13, 0x31, 0x2c, 0xb0, 0x68, 0x73,
- 0x6a, 0xc7, 0x05, 0x3e, 0xd8, 0x79, 0x68, 0x13,
- };
- unsigned long long t = utime();
- for (long i = 0; i < n; i++) {
- gcry_cipher_hd_t h;
- gcry_cipher_open(&h, algo, GCRY_CIPHER_MODE_ECB, 0);
- gcry_cipher_setkey(h, key, keylen);
- gcry_cipher_encrypt(h, block, blocklen, 0, 0);
- gcry_cipher_close(h);
- }
- return (utime() - t) / (double)n;
- }
- int
- main(void)
- {
- static const struct {
- char name[16];
- int exp;
- int algo;
- int keylen;
- int blocklen;
- } table[] = {
- {"AES-256", 20, GCRY_CIPHER_AES256, 32, 16},
- {"Camellia-256", 20, GCRY_CIPHER_CAMELLIA256, 32, 16},
- {"TripleDES", 20, GCRY_CIPHER_3DES, 24, 8},
- {"Twofish", 19, GCRY_CIPHER_TWOFISH, 32, 16},
- {"Blowfish", 16, GCRY_CIPHER_BLOWFISH, 16, 8},
- };
- for (size_t i = 0; i < sizeof(table) / sizeof(*table); i++) {
- long n = 1L << table[i].exp;
- int algo = table[i].algo;
- int keylen = table[i].keylen;
- int blocklen = table[i].blocklen;
- double t = bench(n, algo, keylen, blocklen);
- printf("%-16s %7.3f us/op\n", table[i].name, t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement