Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple performance test of NaCl (requires CZMQ) - placed into the public domain
// Exploring functionality of NaCl
#include <czmq.h>
#include <crypto_box.h>
#include <crypto_secretbox.h>
#include <randombytes.h>
int main (void)
{
// Generate public+secret key pair
byte public_key [crypto_box_PUBLICKEYBYTES];
byte secret_key [crypto_box_SECRETKEYBYTES];
int rc = crypto_box_keypair (public_key, secret_key);
assert (rc == 0);
byte precomputed [crypto_box_BEFORENMBYTES];
rc == crypto_box_beforenm (precomputed, public_key, secret_key);
assert (rc == 0);
byte nonce [crypto_box_NONCEBYTES] = { 0 };
byte plaintext [crypto_box_ZEROBYTES + 256];
byte encrypted [crypto_box_ZEROBYTES + 256];
memset (plaintext, 0, crypto_box_ZEROBYTES);
memcpy (plaintext + crypto_box_ZEROBYTES, (byte *) "Hello World", 11);
unsigned long long length = crypto_box_ZEROBYTES + 11;
// How often can we encrypt and decrypt in 1 second?
int64_t end_at = zclock_time () + 1000;
int iteration;
int cycles = 0;
end_at = zclock_time () + 1000;
cycles = 0;
while (true) {
for (iteration = 0; iteration < 100; iteration++) {
rc = crypto_box_afternm (encrypted, plaintext, length, nonce, precomputed);
assert (rc == 0);
}
cycles += 100;
if (zclock_time () >= end_at)
break;
}
printf ("Encryptions: %d/second\n", cycles);
end_at = zclock_time () + 1000;
cycles = 0;
while (true) {
for (iteration = 0; iteration < 100; iteration++) {
rc = crypto_box_open_afternm (plaintext, encrypted, length, nonce, precomputed);
assert (rc == 0);
}
cycles += 100;
if (zclock_time () >= end_at)
break;
}
printf ("Decryptions: %d/second\n", cycles);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment