Skip to content

Commit c5a61b8

Browse files
committed
linuxkm: make external wolfentropy.ko opt-in; add arm32 Entropy_TimeHiRes shim
1 parent 78e1c68 commit c5a61b8

9 files changed

Lines changed: 125 additions & 24 deletions

File tree

configure.ac

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6048,10 +6048,20 @@ AC_ARG_ENABLE([pwdbased],
60486048
# MemUse Entropy
60496049
# wolfEntropy Software Jitter SP800-90B certifiable entropy source
60506050

6051+
if test "$KERNEL_MODE_DEFAULTS" = "yes" && \
6052+
test "$ENABLED_AMDRDSEED" != "yes" && \
6053+
test "$ENABLED_INTELRDRAND" != "yes" && \
6054+
test "$ENABLED_INTELRDSEED" != "yes"
6055+
then
6056+
ENABLED_ENTROPY_MEMUSE_DEFAULT=yes
6057+
else
6058+
ENABLED_ENTROPY_MEMUSE_DEFAULT=no
6059+
fi
6060+
60516061
AC_ARG_ENABLE([wolfEntropy],
60526062
[AS_HELP_STRING([--enable-wolfEntropy],[Enable memuse entropy support (default: disabled)])],
60536063
[ ENABLED_ENTROPY_MEMUSE=$enableval ],
6054-
[ ENABLED_ENTROPY_MEMUSE=no ]
6064+
[ ENABLED_ENTROPY_MEMUSE=$ENABLED_ENTROPY_MEMUSE_DEFAULT ]
60556065
)
60566066
AC_ARG_ENABLE([wolfentropy],
60576067
[AS_HELP_STRING([--enable-wolfentropy],[Alias for --enable-wolfEntropy])],
@@ -6062,17 +6072,30 @@ AC_ARG_ENABLE([entropy-memuse],
60626072
[ ENABLED_ENTROPY_MEMUSE=$enableval ]
60636073
)
60646074

6065-
# Default --enable-linuxkm to seed its FIPS DRBG from a separately-built
6066-
# wolfentropy.ko module via WC_LINUXKM_WOLFENTROPY_IN_GLUE_LAYER (resolved
6067-
# at module load time via a weak extern on wc_Entropy_Get). Skipped if
6068-
# wolfEntropy is being compiled into libwolfssl.ko itself, or if Intel/AMD
6069-
# RDSEED/RDRAND was explicitly requested as the entropy source.
6070-
if test "$KERNEL_MODE_DEFAULTS" = "yes" && \
6071-
test "x$ENABLED_ENTROPY_MEMUSE" = "xno" && \
6072-
test "$ENABLED_AMDRDSEED" != "yes" && \
6073-
test "$ENABLED_INTELRDRAND" != "yes" && \
6074-
test "$ENABLED_INTELRDSEED" != "yes"
6075-
then
6075+
# Opt-in: seed libwolfssl.ko's FIPS DRBG from a separately-built wolfentropy.ko
6076+
# module via WC_LINUXKM_WOLFENTROPY_IN_GLUE_LAYER (resolved at module load time
6077+
# via a weak extern on wc_Entropy_Get). When enabled, libwolfssl.ko is no
6078+
# longer free-standing -- wolfentropy.ko must be loaded first. Mutually
6079+
# exclusive with --enable-wolfEntropy (which compiles the entropy source
6080+
# directly into libwolfssl.ko).
6081+
AC_ARG_ENABLE([linuxkm-external-wolfentropy],
6082+
[AS_HELP_STRING([--enable-linuxkm-external-wolfentropy],[Seed libwolfssl.ko's DRBG from a separately-loaded wolfentropy.ko module instead of compiling wolfEntropy into libwolfssl.ko (default: disabled)])],
6083+
[ ENABLED_LINUXKM_EXTERNAL_WOLFENTROPY=$enableval ],
6084+
[ ENABLED_LINUXKM_EXTERNAL_WOLFENTROPY=no ]
6085+
)
6086+
6087+
if test "$ENABLED_LINUXKM_EXTERNAL_WOLFENTROPY" = "yes"; then
6088+
if test "$ENABLED_LINUXKM" != "yes"; then
6089+
AC_MSG_ERROR([--enable-linuxkm-external-wolfentropy requires --enable-linuxkm.])
6090+
fi
6091+
if test "x$enable_wolfEntropy" = "xyes" || test "x$enable_wolfentropy" = "xyes" || \
6092+
test "x$enable_entropy_memuse" = "xyes"; then
6093+
AC_MSG_ERROR([--enable-linuxkm-external-wolfentropy is mutually exclusive with --enable-wolfEntropy; the external wolfentropy.ko provides wc_Entropy_Get.])
6094+
fi
6095+
# Override the KERNEL_MODE_DEFAULTS default of ENABLED_ENTROPY_MEMUSE=yes:
6096+
# in this mode the entropy source is provided by wolfentropy.ko, not
6097+
# compiled into libwolfssl.ko.
6098+
ENABLED_ENTROPY_MEMUSE=no
60766099
AM_CFLAGS="$AM_CFLAGS -DWC_LINUXKM_WOLFENTROPY_IN_GLUE_LAYER"
60776100
fi
60786101

@@ -7217,6 +7240,7 @@ then
72177240
;;
72187241
random_c)
72197242
WOLFENTROPY_IN_RANDOM_C=yes
7243+
AM_CFLAGS="$AM_CFLAGS -DWOLFENTROPY_IN_RANDOM_C"
72207244
;;
72217245
*)
72227246
AC_MSG_ERROR([Invalid MemUse Entropy option. Valid are: thread, nofallback, random_c. Seen: $ENABLED_ENTROPY_MEMUSE.])
@@ -11527,7 +11551,11 @@ AM_CONDITIONAL([BUILD_ECCSI],[test "x$ENABLED_ECCSI" = "xyes" || test "x$ENABLED
1152711551
AM_CONDITIONAL([BUILD_SAKKE],[test "x$ENABLED_SAKKE" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
1152811552
AM_CONDITIONAL([BUILD_MEMORY],[test "x$ENABLED_MEMORY" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
1152911553
AM_CONDITIONAL([BUILD_MEMUSE],[test "x$ENABLED_ENTROPY_MEMUSE" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
11530-
AM_CONDITIONAL([BUILD_WOLFENTROPY_C],[test "x$WOLFENTROPY_IN_RANDOM_C" != "xyes" && test "x$ENABLED_ENTROPY_MEMUSE" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
11554+
# In --enable-wolfentropy=random_c mode, wolfentropy.c is textually #included
11555+
# from random.c rather than compiled as its own TU, so it must be excluded
11556+
# from the library source list. Orthogonal to BUILD_MEMUSE, which still
11557+
# gates all other wolfentropy-dependent code.
11558+
AM_CONDITIONAL([BUILD_WOLFENTROPY_IN_RANDOM_C],[test "x$WOLFENTROPY_IN_RANDOM_C" = "xyes"])
1153111559
AM_CONDITIONAL([BUILD_RNG_BANK],[test "$ENABLED_RNG_BANK" = "yes" || test "$ENABLED_USERSETTINGS" = "yes"])
1153211560
AM_CONDITIONAL([BUILD_RSA],[test "x$ENABLED_RSA" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
1153311561
AM_CONDITIONAL([BUILD_DH],[test "x$ENABLED_DH" != "xno" || test "x$ENABLED_USERSETTINGS" = "xyes"])
@@ -11625,7 +11653,7 @@ AM_CONDITIONAL([BUILD_PKCS7],[test "x$ENABLED_PKCS7" = "xyes" || test "x$ENABLED
1162511653
AM_CONDITIONAL([BUILD_SMIME],[test "x$ENABLED_SMIME" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
1162611654
AM_CONDITIONAL([BUILD_HASHFLAGS],[test "x$ENABLED_HASHFLAGS" = "xyes"])
1162711655
AM_CONDITIONAL([BUILD_LINUXKM],[test "$ENABLED_LINUXKM" = "yes"])
11628-
AM_CONDITIONAL([BUILD_WOLFENTROPY_KO],[test "$ENABLED_LINUXKM" = "yes" && test "x$ENABLED_ENTROPY_MEMUSE" != "xno"])
11656+
AM_CONDITIONAL([BUILD_WOLFENTROPY_KO],[test "$ENABLED_LINUXKM" = "yes" && { test "x$ENABLED_ENTROPY_MEMUSE" != "xno" || test "x$ENABLED_LINUXKM_EXTERNAL_WOLFENTROPY" = "xyes"; }])
1162911657
AM_CONDITIONAL([BUILD_KERNEL_MODE_DEFAULTS],[test "$KERNEL_MODE_DEFAULTS" != "no"])
1163011658
AM_CONDITIONAL([BUILD_BSDKM],[test "$ENABLED_BSDKM" = "yes"])
1163111659
AM_CONDITIONAL([BUILD_KERNEL_MODULE],[test "$ENABLED_BSDKM" = "yes" || test "$ENABLED_LINUXKM" = "yes"])

linuxkm/Kbuild

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ ifndef NO_EXPORTS
368368
$(READELF) --symbols --wide $(filter %.o,$^) |
369369
$(AWK) '/^ *[0-9]+: / {
370370
if ($$8 !~ /^(wc_|wolf|WOLF|TLSX_)/){next;}
371-
if ($$8 ~ /^wc_Entropy_(Get|GetRawEntropy|OnDemandTest)$$/){next;}
371+
$(if $(findstring -DWC_LINUXKM_WOLFENTROPY_IN_GLUE_LAYER,$(WOLFSSL_CFLAGS)),if ($$8 ~ /^wc_Entropy_(Get|GetRawEntropy|OnDemandTest)$$/){next;})
372372
if (($$4 == "FUNC") && ($$5 == "GLOBAL") && ($$6 == "DEFAULT")) {
373373
print "$(EXPORT_SYMBOL)(" $$8 ", '"$$ns"');";
374374
}

linuxkm/Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,18 +483,25 @@ WOLFENTROPY_MODULE_TOP := $(MODULE_TOP)/wolfentropy
483483
# Add wolfcrypt/src/sha3_asm.o here for platforms with SHA3 ASM acceleration.
484484
WOLFENTROPY_OBJ_FILES := \
485485
wolfcrypt/src/random.o \
486+
wolfcrypt/src/wolfentropy.o \
487+
wolfcrypt/src/rng_bank.o \
486488
wolfcrypt/src/sha256.o \
487489
wolfcrypt/src/sha3.o \
488490
wolfcrypt/src/wc_port.o \
489491
wolfcrypt/src/memory.o \
490492
wolfcrypt/src/logging.o \
491493
linuxkm/linuxkm_memory.o
492494

493-
# Inherit the same compiler flags as the main module; configure already added
494-
# -DHAVE_ENTROPY_MEMUSE (and -DWOLFSSL_SHA3) when --enable-wolfentropy was set.
495+
# Inherit the same compiler flags as the main module. -DHAVE_ENTROPY_MEMUSE
496+
# and -DWOLFSSL_SHA3 are forced on here because wolfentropy.ko must always
497+
# provide wc_Entropy_* regardless of whether the main libwolfssl.ko was
498+
# built with in-tree wolfEntropy or with --enable-linuxkm-external-wolfentropy
499+
# (which omits them from the main module).
495500
# WOLFSSL_LINUXKM_USE_MUTEXES: use kernel mutexes directly; wolfentropy.ko
496501
# does not have the PIE redirect table that wc_lkm_LockMutex requires.
497-
WOLFENTROPY_CFLAGS := $(WOLFSSL_CFLAGS) -DWOLFSSL_LINUXKM_USE_MUTEXES
502+
# -Wno-error=type-limits: some cross toolchains (e.g. arm-linux-gnueabihf gcc
503+
# 9.x) emit -Wtype-limits in the entropy pipeline; downgrade to warn-only.
504+
WOLFENTROPY_CFLAGS := $(WOLFSSL_CFLAGS) -DHAVE_ENTROPY_MEMUSE -DWOLFSSL_SHA3 -DWOLFSSL_LINUXKM_USE_MUTEXES -Wno-error=type-limits
498505

499506
export WOLFENTROPY_OBJ_FILES WOLFENTROPY_CFLAGS
500507

linuxkm/linuxkm_entropy_time.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* linuxkm_entropy_time.h
2+
*
3+
* arm32 kernel-space Entropy_TimeHiRes shim. The frozen FIPS wolfentropy.c
4+
* has no __arm__ kernel branch and its POSIX fallback (clock_gettime) is
5+
* user-space only. Selected via -DCUSTOM_ENTROPY_TIMEHIRES=wc_linuxkm_time_hires,
6+
* which takes precedence over every arch branch in wolfentropy.c.
7+
*
8+
* Must be included from linuxkm_wc_port.h AFTER the WC_PIE_INDIRECT_SYM(ktime_get_raw)
9+
* #define. Under FIPS/WC_CONTAINERIZE_THIS, that macro rewrites the ktime_get_raw
10+
* call below into an indirected redirect-table lookup. For wolfentropy.ko
11+
* (non-PIE), the call resolves directly against the kernel symbol.
12+
* <linux/timekeeping.h> is assumed to have been pulled in transitively earlier.
13+
*/
14+
15+
#ifndef LINUXKM_ENTROPY_TIME_H
16+
#define LINUXKM_ENTROPY_TIME_H
17+
18+
static inline u64 wc_linuxkm_time_hires(void)
19+
{
20+
return (u64) ktime_get_raw();
21+
}
22+
23+
#endif /* LINUXKM_ENTROPY_TIME_H */

linuxkm/linuxkm_wc_port.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,10 @@
995995
#else
996996
typeof(ktime_get_coarse_real_ts64) *ktime_get_coarse_real_ts64;
997997
#endif
998+
#ifdef CONFIG_ARM
999+
/* arm32 kernel Entropy_TimeHiRes shim (see linuxkm_entropy_time.h). */
1000+
typeof(ktime_get_raw) *ktime_get_raw;
1001+
#endif
9981002

9991003
struct task_struct *(*get_current)(void);
10001004

@@ -1338,6 +1342,10 @@
13381342
#else
13391343
#define ktime_get_coarse_real_ts64 WC_PIE_INDIRECT_SYM(ktime_get_coarse_real_ts64)
13401344
#endif
1345+
#ifdef CONFIG_ARM
1346+
/* arm32 kernel Entropy_TimeHiRes shim (see linuxkm_entropy_time.h). */
1347+
#define ktime_get_raw WC_PIE_INDIRECT_SYM(ktime_get_raw)
1348+
#endif
13411349

13421350
#undef get_current
13431351
#define get_current WC_PIE_INDIRECT_SYM(get_current)
@@ -1467,6 +1475,22 @@
14671475

14681476
#endif /* WC_SYM_RELOC_TABLES */
14691477

1478+
#ifdef CONFIG_ARM
1479+
/* arm32 kernel-space Entropy_TimeHiRes shim. The frozen FIPS wolfentropy.c
1480+
* has no __arm__ kernel branch and its POSIX fallback uses user-space
1481+
* clock_gettime(). wc_linuxkm_time_hires() reads ktime_get_raw(); when
1482+
* WC_CONTAINERIZE_THIS is active above, the #define routes the call through
1483+
* the PIE redirect table, otherwise it resolves directly.
1484+
*
1485+
* Auto-set CUSTOM_ENTROPY_TIMEHIRES so the arm32 branch is selected without
1486+
* requiring downstream CFLAGS changes. Callers can pre-define it to pick a
1487+
* different implementation. */
1488+
#ifndef CUSTOM_ENTROPY_TIMEHIRES
1489+
#define CUSTOM_ENTROPY_TIMEHIRES wc_linuxkm_time_hires
1490+
#endif
1491+
#include "linuxkm_entropy_time.h"
1492+
#endif /* CONFIG_ARM */
1493+
14701494
#if defined(WOLFSSL_KERNEL_STACK_DEBUG) || defined(WC_LINUXKM_STACK_DEBUG)
14711495

14721496
#ifndef CONFIG_THREAD_INFO_IN_TASK

linuxkm/module_hooks.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,10 @@ static int set_up_wolfssl_linuxkm_pie_redirect_table(void) {
13771377
wolfssl_linuxkm_pie_redirect_table.ktime_get_coarse_real_ts64 =
13781378
ktime_get_coarse_real_ts64;
13791379
#endif
1380+
#ifdef CONFIG_ARM
1381+
/* arm32 kernel Entropy_TimeHiRes shim (see linuxkm_entropy_time.h). */
1382+
wolfssl_linuxkm_pie_redirect_table.ktime_get_raw = ktime_get_raw;
1383+
#endif
13801384

13811385
wolfssl_linuxkm_pie_redirect_table.get_current = my_get_current_thread;
13821386

linuxkm/module_hooks_entropy.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ void wc_ecc_fp_init(void) {}
9292
void wc_ecc_fp_free(void) {}
9393
#endif
9494

95+
/* wolfentropy.c and rng_bank.c call wc_linuxkm_check_for_intr_signals() and
96+
* wc_linuxkm_relax_long_loop() directly (no PIE redirect in wolfentropy.ko).
97+
* Their real implementations live in module_hooks.c (libwolfssl.ko only).
98+
* Provide no-op stubs here so modpost can resolve the references.
99+
*/
100+
int wc_linuxkm_check_for_intr_signals(void) { return 0; }
101+
void wc_linuxkm_relax_long_loop(void) {}
102+
95103
MODULE_LICENSE("GPL");
96104
MODULE_AUTHOR("https://www.wolfssl.com/");
97105
MODULE_DESCRIPTION("wolfEntropy SP 800-90B jitter entropy source for wolfSSL FIPS DRBG");

src/include.am

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,11 @@ src_libwolfssl@LIBSUFFIX@_la_SOURCES += \
187187
wolfcrypt/src/hmac.c \
188188
wolfcrypt/src/random.c
189189

190-
if BUILD_WOLFENTROPY_C
190+
if BUILD_MEMUSE
191+
if !BUILD_WOLFENTROPY_IN_RANDOM_C
191192
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/wolfentropy.c
192193
endif
194+
endif
193195

194196
if BUILD_RNG_BANK
195197
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/rng_bank.c
@@ -441,18 +443,22 @@ src_libwolfssl@LIBSUFFIX@_la_SOURCES += \
441443
wolfcrypt/src/random.c
442444

443445
if BUILD_FIPS_V6
444-
if BUILD_WOLFENTROPY_C
446+
if BUILD_MEMUSE
447+
if !BUILD_WOLFENTROPY_IN_RANDOM_C
445448
if BUILD_KERNEL_MODE_DEFAULTS
446449
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/wolfentropy.c
447450
endif
448451
endif
449452
endif
453+
endif
450454

451455
if !BUILD_FIPS_V6
452-
if BUILD_WOLFENTROPY_C
456+
if BUILD_MEMUSE
457+
if !BUILD_WOLFENTROPY_IN_RANDOM_C
453458
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/wolfentropy.c
454459
endif
455460
endif
461+
endif
456462

457463
if BUILD_RNG_BANK
458464
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/rng_bank.c
@@ -810,9 +816,11 @@ endif !BUILD_FIPS_V5
810816
if !BUILD_FIPS_V2_PLUS
811817
if BUILD_RNG
812818
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/random.c
813-
if BUILD_WOLFENTROPY_C
819+
if BUILD_MEMUSE
820+
if !BUILD_WOLFENTROPY_IN_RANDOM_C
814821
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/wolfentropy.c
815822
endif
823+
endif
816824
if BUILD_RNG_BANK
817825
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/rng_bank.c
818826
endif

tests/api/test_random.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ int test_wc_RNG_TestSeed(void)
346346
XMEMSET(seed, 0xa5, sizeof(seed));
347347
/* Return value is DRBG_CONT_FAILURE which is not public. */
348348
/* Moving forward with the RCT test check LT instead of GT */
349-
#if !defined(HAVE_ENTROPY_MEMUSE) && (!defined(HAVE_FIPS) || \
350-
( defined(HAVE_FIPS) && FIPS_VERSION3_GE(7,0,0)))
349+
#if !defined(HAVE_FIPS) || ( defined(HAVE_FIPS) && FIPS_VERSION3_GE(7,0,0) )
351350
ExpectIntLT(wc_RNG_TestSeed(seed, sizeof(seed)), 0);
352351
#else
353352
ExpectIntGT(wc_RNG_TestSeed(seed, sizeof(seed)), 0);

0 commit comments

Comments
 (0)