Skip to content

Commit b91272c

Browse files
committed
wolfcrypt/src/random.c: add sanity check in wc_GenerateSeed_IntelRD() to work around buggy RDSEED by disabling it if it generates three identical 64 bit words consecutively;
wolfssl/wolfcrypt/settings.h: if DEBUG_WOLFSSL && !WC_NO_VERBOSE_RNG, set WC_VERBOSE_RNG, and add WOLFSSL_NO_DEBUG_CERTS to allow inhibition of WOLFSSL_DEBUG_CERTS.
1 parent ba53051 commit b91272c

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ WOLFSSL_NO_COPY_KEY
802802
WOLFSSL_NO_CRL_DATE_CHECK
803803
WOLFSSL_NO_CRL_NEXT_DATE
804804
WOLFSSL_NO_CT_MAX_MIN
805+
WOLFSSL_NO_DEBUG_CERTS
805806
WOLFSSL_NO_DECODE_EXTRA
806807
WOLFSSL_NO_DER_TO_PEM
807808
WOLFSSL_NO_DH186

wolfcrypt/src/random.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,12 +1935,46 @@ static int wc_GenerateSeed_IntelRD(OS_Seed* os, byte* output, word32 sz)
19351935
{
19361936
int ret;
19371937
word64 rndTmp;
1938+
static int rdseed_sanity_status = 0;
19381939

19391940
(void)os;
19401941

19411942
if (!IS_INTEL_RDSEED(intel_flags))
19421943
return -1;
19431944

1945+
if (rdseed_sanity_status == 0) {
1946+
static word64 sanity_words[2] = {0, 0};
1947+
1948+
ret = IntelRDseed64_r(&sanity_words[0]);
1949+
if (ret != 0)
1950+
return ret;
1951+
1952+
ret = IntelRDseed64_r(&sanity_words[1]);
1953+
if (ret != 0)
1954+
return ret;
1955+
1956+
if (sanity_words[0] == sanity_words[1]) {
1957+
ret = IntelRDseed64_r(&sanity_words[0]);
1958+
if (ret != 0)
1959+
return ret;
1960+
1961+
if (sanity_words[0] == sanity_words[1]) {
1962+
rdseed_sanity_status = -1;
1963+
#ifdef WC_VERBOSE_RNG
1964+
WOLFSSL_DEBUG_PRINTF(
1965+
"WARNING: RDSEED disabled due to repeating word 0x%lx -- "
1966+
"check CPU microcode version.", sanity_words[1]);
1967+
#endif
1968+
return -1;
1969+
}
1970+
}
1971+
1972+
rdseed_sanity_status = 1;
1973+
}
1974+
else if (rdseed_sanity_status < 0) {
1975+
return -1;
1976+
}
1977+
19441978
for (; (sz / sizeof(word64)) > 0; sz -= sizeof(word64),
19451979
output += sizeof(word64)) {
19461980
ret = IntelRDseed64_r((word64*)output);

wolfssl/wolfcrypt/settings.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,22 @@
369369
#warning "No configuration for wolfSSL detected, check header order"
370370
#endif
371371

372-
/* Ensure WOLFSSL_DEBUG_CERTS is always set when DEBUG_WOLFSSL is enabled */
373-
#ifdef DEBUG_WOLFSSL
374-
#undef WOLFSSL_DEBUG_CERTS
372+
/* Ensure WOLFSSL_DEBUG_CERTS is set when DEBUG_WOLFSSL is enabled, unless
373+
* expressly requested otherwise.
374+
*/
375+
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_NO_DEBUG_CERTS) && \
376+
!defined(WOLFSSL_DEBUG_CERTS)
375377
#define WOLFSSL_DEBUG_CERTS
376378
#endif
377379

380+
/* Ensure WC_VERBOSE_RNG is set when DEBUG_WOLFSSL is enabled, unless expressly
381+
* requested otherwise.
382+
*/
383+
#if defined(DEBUG_WOLFSSL) && !defined(WC_NO_VERBOSE_RNG) && \
384+
!defined(WC_VERBOSE_RNG)
385+
#define WC_VERBOSE_RNG
386+
#endif
387+
378388
#include <wolfssl/wolfcrypt/visibility.h>
379389

380390
/*------------------------------------------------------------*/

0 commit comments

Comments
 (0)