Skip to content

Commit 66703cc

Browse files
authored
Run the FIPS CAST tests under lock during wolfprovider init (#319)
* Run the FIPS CAST tests under lock during wolfprovider init * Add error messages on FIPS CAST errors
1 parent 0e6e7e7 commit 66703cc

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

include/wolfprovider/internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ WC_RNG* wp_provctx_get_rng(WOLFPROV_CTX* provCtx);
160160
#ifndef WP_SINGLE_THREADED
161161
int wp_provctx_lock_rng(WOLFPROV_CTX* provCtx);
162162
void wp_provctx_unlock_rng(WOLFPROV_CTX* provCtx);
163+
164+
#ifdef HAVE_FIPS
165+
wolfSSL_Mutex *wp_get_cast_mutex(void);
166+
#endif
163167
#endif
164168

165169
int wolfssl_prov_get_capabilities(void *provctx, const char *capability,

src/wp_internal.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@
3030
#include <wolfssl/wolfcrypt/rsa.h>
3131
#include <wolfssl/wolfcrypt/pwdbased.h>
3232

33+
#if defined(HAVE_FIPS) && (!defined(WP_SINGLE_THREADED))
34+
static wolfSSL_Mutex castMutex;
35+
36+
/**
37+
* Initialize the cast mutex on library load.
38+
*
39+
* This constructor runs when libwolfprov.so is loaded via dlopen() or at
40+
* program startup. It ensures the castMutex is initialized under lock.
41+
*/
42+
__attribute__((constructor))
43+
static void wolfprov_init_cast_mutex(void)
44+
{
45+
wc_InitMutex(&castMutex);
46+
}
47+
48+
wolfSSL_Mutex *wp_get_cast_mutex()
49+
{
50+
return &castMutex;
51+
}
52+
#endif
53+
3354
/**
3455
* Get the wolfSSL random number generator from the provider context.
3556
*

src/wp_wolfprov.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ static WOLFPROV_CTX* wolfssl_prov_ctx_new(void)
214214
{
215215
WOLFPROV_CTX* ctx;
216216

217-
#ifdef WC_RNG_SEED_CB
218-
wc_SetSeed_Cb(wc_GenerateSeed);
219-
#endif
220-
221217
ctx = (WOLFPROV_CTX*)OPENSSL_zalloc(sizeof(WOLFPROV_CTX));
222218
if ((ctx != NULL) && (wc_InitRng(&ctx->rng) != 0)) {
223219
OPENSSL_free(ctx);
@@ -1312,6 +1308,31 @@ int wolfssl_provider_init(const OSSL_CORE_HANDLE* handle,
13121308
}
13131309
}
13141310

1311+
if (ok) {
1312+
#ifdef WC_RNG_SEED_CB
1313+
wc_SetSeed_Cb(wc_GenerateSeed);
1314+
#endif
1315+
#if defined(HAVE_FIPS) && (!defined(WP_SINGLE_THREADED))
1316+
/* To avoid multi-threading issues in FIPS CAST tests, run all tests
1317+
* under a lock now */
1318+
if (wp_lock(wp_get_cast_mutex()) != 1) {
1319+
WOLFPROV_ERROR_MSG(WP_LOG_COMP_PROVIDER,
1320+
"Fatal Error: unable to acquire FIPS CAST lock");
1321+
ok = 0;
1322+
}
1323+
if (ok) {
1324+
if (wc_RunAllCast_fips() != 0) {
1325+
WOLFPROV_ERROR_MSG(WP_LOG_COMP_PROVIDER,
1326+
"Fatal Error: FIPS algo selftest failure");
1327+
ok = 0;
1328+
}
1329+
if (wp_unlock(wp_get_cast_mutex()) != 1) {
1330+
ok = 0;
1331+
}
1332+
}
1333+
#endif
1334+
}
1335+
13151336
if (ok) {
13161337
/* Create a new provider context. */
13171338
*provCtx = wolfssl_prov_ctx_new();

0 commit comments

Comments
 (0)