Skip to content

Instantly share code, notes, and snippets.

@jedisct1
Created May 2, 2016 04:18
Show Gist options
  • Save jedisct1/63a4df48c7b367b45664ea0a9f5c1d4e to your computer and use it in GitHub Desktop.
Save jedisct1/63a4df48c7b367b45664ea0a9f5c1d4e to your computer and use it in GitHub Desktop.
#include <stddef.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#define crypto_aead_aes256gcm_KEYBYTES 32U
#define crypto_aead_aes256gcm_NPUBBYTES 12U
#define crypto_aead_aes256gcm_ABYTES 16U
#define CRYPTO_ALIGN(x) __attribute__((aligned(x)))
typedef CRYPTO_ALIGN(16) unsigned char crypto_aead_aes256gcm_state[512];
int crypto_aead_aes256gcm_is_available(void);
int sodium_init(void);
void sodium_increment(unsigned char *n, const size_t nlen);
int crypto_aead_aes256gcm_beforenm(crypto_aead_aes256gcm_state *ctx_,
const unsigned char *k);
int crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c,
unsigned long long *clen_p,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *ad,
unsigned long long adlen,
const unsigned char *nsec,
const unsigned char *npub,
const crypto_aead_aes256gcm_state *ctx_);
int main(void)
{
CRYPTO_ALIGN(16) unsigned char block[65536 + crypto_aead_aes256gcm_ABYTES];
crypto_aead_aes256gcm_state ctx;
unsigned char key[crypto_aead_aes256gcm_KEYBYTES];
unsigned char npub[crypto_aead_aes256gcm_NPUBBYTES];
size_t bs = 2;
unsigned long long rounds = 2500000;
unsigned long long i;
unsigned long long elapsed;
struct timeval tv, tv2;
if (sodium_init() != 0 || crypto_aead_aes256gcm_is_available() == 0) {
return 1;
}
memset(key, 42, sizeof key);
crypto_aead_aes256gcm_beforenm(&ctx, key);
printf("Iterations: %llu\n", rounds);
while (bs <= sizeof block) {
printf("Testing with block size: %zu\t", bs);
fflush(stdout);
gettimeofday(&tv, NULL);
for (i = 0; i < rounds; i++) {
crypto_aead_aes256gcm_encrypt_afternm(block, NULL, block, bs,
NULL, 0ULL, NULL, npub,
&ctx);
sodium_increment(npub, sizeof npub);
}
gettimeofday(&tv2, NULL);
elapsed = (tv2.tv_usec + tv2.tv_sec * 1000000) -
(tv.tv_usec + tv.tv_sec * 1000000);
printf("%llu usec (%6llu Mb/s)\t\tblock[0]=%x\n", elapsed,
bs * rounds * 8ULL * 1000000ULL / (elapsed * 1000000ULL),
block[0]);
bs <<= 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment