Skip to content

Commit 9fa8388

Browse files
bigbrettdgarske
authored andcommitted
fixed bug where tls_bench failed KATs for FIPS builds in a multithreaded environment
1 parent c73e433 commit 9fa8388

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

examples/benchmark/tls_bench.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,31 @@ char* myoptarg = NULL;
383383
int DoneHandShake = 0;
384384
#endif
385385

386+
387+
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
388+
static int run_all_CAST(void)
389+
{
390+
int ret = 0;
391+
392+
for (int i=0; i<FIPS_CAST_COUNT; i++) {
393+
if ((ret = wc_RunCast_fips(i)) != 0) {
394+
#ifdef NO_ERROR_STRINGS
395+
fprintf(stderr,
396+
"ERROR: FIPS CAST failed with return code: %d\n", ret);
397+
#else
398+
fprintf(stderr,
399+
"ERROR: FIPS CAST failed for algorithm: %s\n",
400+
wc_GetErrorString(ret));
401+
#endif
402+
return ret;
403+
}
404+
}
405+
406+
return ret;
407+
}
408+
#endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
409+
410+
386411
static double gettime_secs(int reset)
387412
{
388413
struct timeval tv;
@@ -1863,6 +1888,23 @@ int bench_tls(void* args)
18631888
/* Initialize wolfSSL */
18641889
wolfSSL_Init();
18651890

1891+
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
1892+
/*
1893+
* When running benchmarks on FIPS builds, we need to run ALL CASTs up
1894+
* front before spawning client/server threads, otherwise there is the
1895+
* possibility that both threads try to run a CAST at the same time during
1896+
* the handshake. In this scenario, the thread that doesn't win the race
1897+
* will not be able to run the CAST, since it returns "busy", which is treated
1898+
* as a failure. Running the CASTs up front is a simpler solution than
1899+
* implementing an additional layer of synchronization.
1900+
*/
1901+
if ((ret = run_all_CAST()) != 0)
1902+
{
1903+
fprintf(stderr, "CAST failed. Exiting benchmark\n");
1904+
goto exit;
1905+
}
1906+
#endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
1907+
18661908
/* Parse command line arguments */
18671909
while ((ch = mygetopt(argc, argv, "?" "udeil:p:t:vT:sch:P:mS:g")) != -1) {
18681910
switch (ch) {

0 commit comments

Comments
 (0)