Skip to content

Instantly share code, notes, and snippets.

@kkm000
Created September 11, 2015 05:05
Show Gist options
  • Save kkm000/8fc78dd6861354d96e8c to your computer and use it in GitHub Desktop.
Save kkm000/8fc78dd6861354d96e8c to your computer and use it in GitHub Desktop.
Benchmarking key store enumeration
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
static void failerr(const char *funName) {
fprintf(stderr, "%s() failed: 0x%08lX\n", funName, GetLastError());
exit(1);
}
void enum_keys(HCRYPTPROV hprov) {
BYTE buf[1024]; // Max key name length.
for (DWORD first_next = CRYPT_FIRST; 1; first_next = CRYPT_NEXT) {
DWORD buf_len = sizeof buf;
if (!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, buf, &buf_len, first_next)) {
if (GetLastError() == ERROR_NO_MORE_ITEMS)
break;
else
failerr("CryptGetProvParam");
}
}
}
const char *csp = MS_ENH_RSA_AES_PROV_A;
const DWORD csptype = PROV_RSA_AES;
void do_benchmark(DWORD enum_flags) {
enum_flags |= CRYPT_VERIFYCONTEXT;
HCRYPTPROV hprov;
if (!CryptAcquireContext(&hprov, NULL, csp, csptype, enum_flags)) {
failerr("CryptAcquireContext");
}
int K = 100;
LARGE_INTEGER perfreq, timein, timeout;
QueryPerformanceFrequency(&perfreq);
QueryPerformanceCounter(&timein);
for (int i = 0; i < K; ++i)
enum_keys (hprov);
QueryPerformanceCounter(&timeout);
printf(" %f ms per pass\n", (double)(timeout.QuadPart-timein.QuadPart)/perfreq.QuadPart * 1000.0 / K);
CryptReleaseContext(hprov, 0);
}
void main() {
printf("--- User key store access performance test... ");
do_benchmark(0);
printf("--- Machine key store access performance test... ");
do_benchmark(CRYPT_MACHINE_KEYSET);
}
@kkm000
Copy link
Author

kkm000 commented Sep 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment