Skip to content

Commit d04ab37

Browse files
committed
New build option WIN_REUSE_CRYPT_HANDLE to allow reuse of the windows crypt provider handle. Seeding happens on any new RNG or after WC_RESEED_INTERVAL. If using threads make sure wolfSSL_Init() or wolfCrypt_Init() is called before spinning up threads. ZD 19754. Fixed minor implicit cast warnings in internal.c. Add missing hpke.c to wolfssl VS project.
1 parent 1c0e5af commit d04ab37

File tree

6 files changed

+77
-15
lines changed

6 files changed

+77
-15
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ WC_STRICT_SIG
583583
WC_WANT_FLAG_DONT_USE_AESNI
584584
WC_XMSS_FULL_HASH
585585
WIFI_AVAILABLE
586+
WIN_REUSE_CRYPT_HANDLE
586587
WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
587588
WOLFSENTRY_H
588589
WOLFSENTRY_NO_JSON

src/internal.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19059,7 +19059,7 @@ static int Poly1305TagOld(WOLFSSL* ssl, byte* additional, int additionalSz,
1905919059

1906019060
/* length of additional input plus padding */
1906119061
XMEMSET(padding, 0, sizeof(padding));
19062-
padding[0] = additionalSz;
19062+
padding[0] = (byte)additionalSz;
1906319063
if ((ret = wc_Poly1305Update(ssl->auth.poly1305, padding,
1906419064
sizeof(padding))) != 0)
1906519065
return ret;
@@ -19141,7 +19141,8 @@ int ChachaAEADEncrypt(WOLFSSL* ssl, byte* out, const byte* input,
1914119141
}
1914219142
#endif
1914319143

19144-
addSz = writeAeadAuthData(ssl, msgLen, type, add, 0, &seq, verifyOrder);
19144+
addSz = writeAeadAuthData(ssl, (word16)msgLen, type, add, 0, &seq,
19145+
verifyOrder);
1914519146
if (addSz < 0)
1914619147
return addSz;
1914719148

@@ -19336,7 +19337,8 @@ int ChachaAEADDecrypt(WOLFSSL* ssl, byte* plain, const byte* input,
1933619337
#endif
1933719338

1933819339

19339-
addSz = writeAeadAuthData(ssl, msgLen, no_type, add, 1, &seq, PEER_ORDER);
19340+
addSz = writeAeadAuthData(ssl, (word16)msgLen, no_type, add, 1, &seq,
19341+
PEER_ORDER);
1934019342
if (addSz < 0)
1934119343
return addSz;
1934219344

wolfcrypt/src/random.c

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,6 +2711,34 @@ 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+
/* shared crypt handle for RNG use */
2716+
static ProviderHandle gHandle = 0;
2717+
2718+
int wc_WinCryptHandleInit(void)
2719+
{
2720+
int ret = 0;
2721+
if (gHandle == 0) {
2722+
if(!CryptAcquireContext(&gHandle, 0, 0, PROV_RSA_FULL,
2723+
CRYPT_VERIFYCONTEXT)) {
2724+
DWORD dw = GetLastError();
2725+
WOLFSSL_MSG("CryptAcquireContext failed!");
2726+
WOLFSSL_ERROR((int)dw);
2727+
ret = WINCRYPT_E;
2728+
}
2729+
}
2730+
return ret;
2731+
}
2732+
2733+
void wc_WinCryptHandleCleanup(void)
2734+
{
2735+
if (gHandle != 0) {
2736+
CryptReleaseContext(gHandle, 0);
2737+
gHandle = 0;
2738+
}
2739+
}
2740+
#endif /* WIN_REUSE_CRYPT_HANDLE */
2741+
27142742
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
27152743
{
27162744
#ifdef WOLF_CRYPTO_CB
@@ -2741,14 +2769,27 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
27412769
}
27422770
#endif /* HAVE_INTEL_RDSEED */
27432771

2744-
if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
2745-
CRYPT_VERIFYCONTEXT))
2772+
#ifdef WIN_REUSE_CRYPT_HANDLE
2773+
/* Check that handle was initialized.
2774+
* Note: initialization should be done through:
2775+
* wolfSSL_Init -> wolfCrypt_Init -> wc_WinCryptHandleInit
2776+
*/
2777+
if (wc_WinCryptHandleInit() != 0) {
27462778
return WINCRYPT_E;
2747-
2748-
if (!CryptGenRandom(os->handle, sz, output))
2779+
}
2780+
if (!CryptGenRandom(gHandle, sz, output))
27492781
return CRYPTGEN_E;
2750-
2782+
#else
2783+
if (!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
2784+
CRYPT_VERIFYCONTEXT)) {
2785+
return WINCRYPT_E;
2786+
}
2787+
if (!CryptGenRandom(os->handle, sz, output)) {
2788+
return CRYPTGEN_E;
2789+
}
27512790
CryptReleaseContext(os->handle, 0);
2791+
os->handle = 0;
2792+
#endif
27522793

27532794
return 0;
27542795
}

wolfcrypt/src/wc_port.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,20 @@ int wolfCrypt_Init(void)
339339
return ret;
340340
#endif
341341

342-
#ifdef HAVE_ENTROPY_MEMUSE
343-
ret = Entropy_Init();
344-
if (ret != 0) {
345-
WOLFSSL_MSG("Error initializing entropy");
346-
return ret;
347-
}
348-
#endif
342+
#if defined(USE_WINDOWS_API) && defined(WIN_REUSE_CRYPT_HANDLE)
343+
/* A failure here should not happen, but if it does the actual RNG seed
344+
* call will fail. This init is for a shared crypt provider handle for
345+
* RNG */
346+
(void)wc_WinCryptHandleInit();
347+
#endif
348+
349+
#ifdef HAVE_ENTROPY_MEMUSE
350+
ret = Entropy_Init();
351+
if (ret != 0) {
352+
WOLFSSL_MSG("Error initializing entropy");
353+
return ret;
354+
}
355+
#endif
349356

350357
#ifdef HAVE_ECC
351358
#ifdef FP_ECC
@@ -516,6 +523,10 @@ int wolfCrypt_Cleanup(void)
516523
Entropy_Final();
517524
#endif
518525

526+
#if defined(USE_WINDOWS_API) && defined(WIN_REUSE_CRYPT_HANDLE)
527+
wc_WinCryptHandleCleanup();
528+
#endif
529+
519530
#ifdef WOLF_CRYPTO_CB
520531
wc_CryptoCb_Cleanup();
521532
#endif

wolfssl.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@
437437
<ClCompile Include="wolfcrypt\src\ge_operations.c" />
438438
<ClCompile Include="wolfcrypt\src\hash.c" />
439439
<ClCompile Include="wolfcrypt\src\hmac.c" />
440+
<ClCompile Include="wolfcrypt\src\hpke.c" />
440441
<ClCompile Include="wolfcrypt\src\integer.c" />
441442
<ClCompile Include="wolfcrypt\src\kdf.c" />
442443
<ClCompile Include="wolfcrypt\src\wc_mlkem.c" />

wolfssl/wolfcrypt/random.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@
133133
#else
134134
typedef unsigned long ProviderHandle;
135135
#endif
136+
137+
#ifdef WIN_REUSE_CRYPT_HANDLE
138+
/* called from wolfCrypt_Init() and wolfCrypt_Cleanup() */
139+
WOLFSSL_LOCAL int wc_WinCryptHandleInit(void);
140+
WOLFSSL_LOCAL void wc_WinCryptHandleCleanup(void);
141+
#endif
136142
#endif
137143

138144
#ifndef WC_RNG_TYPE_DEFINED /* guard on redeclaration */

0 commit comments

Comments
 (0)