Skip to content

Commit 6cf1bb1

Browse files
committed
Improvements to thread safety. Fix minor implicit cast warnings. Add missing hpke.c to wolfssl VS project.
1 parent 8e83640 commit 6cf1bb1

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

src/internal.c

100644100755
+5-3
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

100644100755
+39-14
Original file line numberDiff line numberDiff line change
@@ -2712,8 +2712,32 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
27122712
#elif defined(USE_WINDOWS_API)
27132713

27142714
#ifdef WIN_REUSE_CRYPT_HANDLE
2715-
static ProviderHandle gHandle;
2716-
#endif
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 */
27172741

27182742
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
27192743
{
@@ -2746,22 +2770,23 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
27462770
#endif /* HAVE_INTEL_RDSEED */
27472771

27482772
#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;
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) {
2778+
return WINCRYPT_E;
27532779
}
2754-
os->handle = gHandle;
2780+
if (!CryptGenRandom(gHandle, sz, output))
2781+
return CRYPTGEN_E;
27552782
#else
2756-
if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
2757-
CRYPT_VERIFYCONTEXT))
2783+
if (!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
2784+
CRYPT_VERIFYCONTEXT)) {
27582785
return WINCRYPT_E;
2759-
#endif
2760-
2761-
if (!CryptGenRandom(os->handle, sz, output))
2786+
}
2787+
if (!CryptGenRandom(os->handle, sz, output)) {
27622788
return CRYPTGEN_E;
2763-
2764-
#ifndef WIN_REUSE_CRYPT_HANDLE
2789+
}
27652790
CryptReleaseContext(os->handle, 0);
27662791
os->handle = 0;
27672792
#endif

wolfcrypt/src/wc_port.c

+18-7
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

+1
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

+6
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)