Skip to content

Commit 785c174

Browse files
committed
New build option to allow reuse of the windows crypt provider handle (WIN_REUSE_CRYPT_HANDLE). ZD 19754.
1 parent c22505a commit 785c174

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

.wolfssl_known_macro_extras

+1
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ WC_STRICT_SIG
567567
WC_WANT_FLAG_DONT_USE_AESNI
568568
WC_XMSS_FULL_HASH
569569
WIFI_AVAILABLE
570+
WIN_REUSE_CRYPT_HANDLE
570571
WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
571572
WOLFSENTRY_H
572573
WOLFSENTRY_NO_JSON

wolfcrypt/src/random.c

+16
Original file line numberDiff line numberDiff line change
@@ -2711,6 +2711,10 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
27112711

27122712
#elif defined(USE_WINDOWS_API)
27132713

2714+
#ifdef WIN_REUSE_CRYPT_HANDLE
2715+
static ProviderHandle gHandle;
2716+
#endif
2717+
27142718
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
27152719
{
27162720
#ifdef WOLF_CRYPTO_CB
@@ -2741,14 +2745,26 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
27412745
}
27422746
#endif /* HAVE_INTEL_RDSEED */
27432747

2748+
#ifdef WIN_REUSE_CRYPT_HANDLE
2749+
if (gHandle == 0) {
2750+
if(!CryptAcquireContext(&gHandle, 0, 0, PROV_RSA_FULL,
2751+
CRYPT_VERIFYCONTEXT))
2752+
return WINCRYPT_E;
2753+
}
2754+
os->handle = gHandle;
2755+
#else
27442756
if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
27452757
CRYPT_VERIFYCONTEXT))
27462758
return WINCRYPT_E;
2759+
#endif
27472760

27482761
if (!CryptGenRandom(os->handle, sz, output))
27492762
return CRYPTGEN_E;
27502763

2764+
#ifndef WIN_REUSE_CRYPT_HANDLE
27512765
CryptReleaseContext(os->handle, 0);
2766+
os->handle = 0;
2767+
#endif
27522768

27532769
return 0;
27542770
}

0 commit comments

Comments
 (0)