Skip to content

Instantly share code, notes, and snippets.

@rblaze
Last active August 29, 2015 14:03
Show Gist options
  • Save rblaze/fe469a8cca74697353d8 to your computer and use it in GitHub Desktop.
Save rblaze/fe469a8cca74697353d8 to your computer and use it in GitHub Desktop.
AES perf test
#define CHECK(x) \
do { \
NTSTATUS __l_status = (x); \
if (! BCRYPT_SUCCESS(__l_status)) { \
printf("ERROR: `%s` returned %x\n", #x, __l_status); \
return 1; \
} \
} while (0)
UCHAR masterSecret[128];
int BulkTest()
{
BCRYPT_ALG_HANDLE encAlgHandle;
BCRYPT_KEY_HANDLE keyHandle;
LARGE_INTEGER start, end, rate;
UCHAR iv[16];
QueryPerformanceFrequency(&rate);
CHECK(BCryptOpenAlgorithmProvider(&encAlgHandle, BCRYPT_AES_ALGORITHM, NULL, 0));
CHECK(BCryptGenerateSymmetricKey(encAlgHandle, &keyHandle, NULL, 0, masterSecret, 16, 0));
ULONG ciphertextsize;
LONG size = 128*1024*1024;
UCHAR *pt = (UCHAR *)malloc(size);
UCHAR *ct = (UCHAR *)malloc(size);
memset(iv, 0, sizeof(iv));
QueryPerformanceCounter(&start);
CHECK(BCryptEncrypt(keyHandle, pt, size, NULL, iv, sizeof(iv), ct, size, &ciphertextsize, 0));
QueryPerformanceCounter(&end);
printf("encryption %d bytes time %I64d ticks, %I64d bps\n", ciphertextsize, end.QuadPart - start.QuadPart, rate.QuadPart * size / (end.QuadPart - start.QuadPart));
memset(iv, 0, sizeof(iv));
QueryPerformanceCounter(&start);
CHECK(BCryptDecrypt(keyHandle, ct, ciphertextsize, NULL, iv, sizeof(iv), pt, size, &ciphertextsize, 0));
QueryPerformanceCounter(&end);
printf("decryption %d bytes time %I64d ticks, %I64d bps\n", ciphertextsize, end.QuadPart - start.QuadPart, rate.QuadPart * size / (end.QuadPart - start.QuadPart));
free(pt);
free(ct);
CHECK(BCryptDestroyKey(keyHandle));
CHECK(BCryptCloseAlgorithmProvider(encAlgHandle, 0));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment