SHARE
TWEET

Untitled

a guest Aug 25th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Key schedule benchmarks
  2.  * cc -O3 bench.c -lgcrypt
  3.  */
  4. #define _POSIX_C_SOURCE 200112L
  5. #include <time.h>
  6. #include <stdio.h>
  7. #include <gcrypt.h>
  8.  
  9. static unsigned long long
  10. utime(void)
  11. {
  12.     struct timespec tv[1];
  13.     clock_gettime(CLOCK_REALTIME, tv);
  14.     return tv->tv_sec * 1000000ULL + tv->tv_nsec / 1000ULL;
  15. }
  16.  
  17. static double
  18. bench(long n, int algo, int keylen, int blocklen)
  19. {
  20.     unsigned char block[16] = {0};
  21.     static const unsigned char key[32] = {
  22.         0x15, 0x86, 0x14, 0xf5, 0x09, 0x79, 0x3e, 0x9b,
  23.         0x52, 0x96, 0x9c, 0xd4, 0x59, 0xa1, 0x4c, 0x7c,
  24.         0xfc, 0x92, 0x13, 0x31, 0x2c, 0xb0, 0x68, 0x73,
  25.         0x6a, 0xc7, 0x05, 0x3e, 0xd8, 0x79, 0x68, 0x13,
  26.     };
  27.     unsigned long long t = utime();
  28.     for (long i = 0; i < n; i++) {
  29.         gcry_cipher_hd_t h;
  30.         gcry_cipher_open(&h, algo, GCRY_CIPHER_MODE_ECB, 0);
  31.         gcry_cipher_setkey(h, key, keylen);
  32.         gcry_cipher_encrypt(h, block, blocklen, 0, 0);
  33.         gcry_cipher_close(h);
  34.     }
  35.     return (utime() - t) / (double)n;
  36. }
  37.  
  38. int
  39. main(void)
  40. {
  41.     static const struct {
  42.         char name[16];
  43.         int exp;
  44.         int algo;
  45.         int keylen;
  46.         int blocklen;
  47.     } table[] = {
  48.         {"AES-256",      20, GCRY_CIPHER_AES256,      32, 16},
  49.         {"Camellia-256", 20, GCRY_CIPHER_CAMELLIA256, 32, 16},
  50.         {"TripleDES",    20, GCRY_CIPHER_3DES,        24,  8},
  51.         {"Twofish",      19, GCRY_CIPHER_TWOFISH,     32, 16},
  52.         {"Blowfish",     16, GCRY_CIPHER_BLOWFISH,    16,  8},
  53.     };
  54.  
  55.     for (size_t i = 0; i < sizeof(table) / sizeof(*table); i++) {
  56.         long n = 1L << table[i].exp;
  57.         int algo = table[i].algo;
  58.         int keylen = table[i].keylen;
  59.         int blocklen = table[i].blocklen;
  60.         double t = bench(n, algo, keylen, blocklen);
  61.         printf("%-16s %7.3f us/op\n", table[i].name, t);
  62.     }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top