Skip to content

Commit 85b51b3

Browse files
committed
fixes for fips#379 and related:
linuxkm/Makefile, linuxkm/linuxkm-fips-hash-wrapper.sh, linuxkm/linuxkm_memory.c: refactor coreKey extraction to use ELF tools rather than WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE and user_settings.h. linuxkm/module_hooks.c: add stack measurement for wc_RunAllCast_fips(). tests/api/test_slhdsa.c: frivolous initialization to work around a false positive -Wmaybe-uninitialized in slhdsa_der_roundtrip_one(). wolfcrypt/src/wc_slhdsa.c, wolfssl/wolfcrypt/wc_slhdsa.h: * refactor lifecycle management for SHA-2 objects to fix a leak via wc_SlhDsaKey_CheckKey(). * add support for WC_SLHDSA_NO_ASM. * add WOLFSSL_SLHDSA_VERIFY_ONLY gates around prototypes, to get compile-time failures for misuse. wolfcrypt/test/test.c: * clean up myFipsCb() and restore usability of TEST_ALWAYS_RUN_TO_END with bad FIPS hash (useful test coverage). * add wc_RunAllCast_fips() to wolfcrypt_test(). * when WOLFSSL_KERNEL_MODE or BENCH_EMBEDDED, force on WOLFSSL_SLHDSA_VERIFY_ONLY unless WOLFSSL_SLHDSA_FORCE_FULL_TESTS is defined. wolfssl/wolfcrypt/settings.h: * add WC_MLKEM_NO_ASM to WOLFSSL_LINUXKM section to work around asm bug. * remove clause in WOLFSSL_KERNEL_MODE section that forced on WOLFSSL_SLHDSA_VERIFY_ONLY.
1 parent 02dfd12 commit 85b51b3

11 files changed

Lines changed: 211 additions & 112 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ WC_FORCE_LINUXKM_FORTIFY_SOURCE
656656
WC_HASH_CUSTOM_MAX_BLOCK_SIZE
657657
WC_HASH_CUSTOM_MAX_DIGEST_SIZE
658658
WC_HASH_CUSTOM_MIN_DIGEST_SIZE
659+
WC_MLKEM_KERNEL_ASM
659660
WC_NO_ASYNC_SLEEP
660661
WC_NO_RNG_SIMPLE
661662
WC_NO_STATIC_ASSERT
@@ -671,6 +672,8 @@ WC_RSA_NONBLOCK_TIME
671672
WC_RSA_NO_FERMAT_CHECK
672673
WC_RWLOCK_OPS_INLINE
673674
WC_SKIP_INCLUDED_C_FILES
675+
WC_SLHDSA_KERNEL_ASM
676+
WC_SLHDSA_NO_ASM
674677
WC_SLHDSA_VERBOSE_DEBUG
675678
WC_SSIZE_TYPE
676679
WC_STRICT_SIG

linuxkm/Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,11 @@ $(MODULE_TOP)/libwolfssl-user-build/src/.libs/libwolfssl.so: $(LIBWOLFSSL_NAME).
401401
@ for file in "$${srcfiles[@]}"; do if [[ ! -e "$$file" ]]; then mkdir -p "$$(dirname "$$file")" && cp --no-dereference --symbolic-link --no-clobber '$(SRC_TOP)'/"$$file" "$$file"; fi; done
402402
@ echo ' done.'
403403
@fi
404-
@if [[ ! -f user_settings.h ]]; then
405-
@ echo '__attribute__ ((visibility("default"))) extern const char coreKey[];' > user_settings.h
406-
@ echo > user_settings_asm.h
407-
@fi
408404
@if [[ -f Makefile ]]; then
409405
@ echo 'Using existing Makefile for libwolfssl.so.'
410406
@else
411407
@ echo -n 'Configuring user libwolfssl.so...'
412-
@ $(FRESH_ENV) ./configure $(QFLAG) $(VFLAG) --disable-jobserver --enable-cryptonly --enable-fips="$$FIPS_FLAVOR" CFLAGS='-DWC_SYM_RELOC_TABLES_SUPPORT -DWOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE -DWOLFSSL_USER_SETTINGS -DWOLFSSL_USER_SETTINGS_ASM -DDEBUG_LINUXKM_PIE_SUPPORT' $(if $(HOSTCC),CC='$(HOSTCC)')
408+
@ $(FRESH_ENV) ./configure $(QFLAG) $(VFLAG) --disable-jobserver --enable-cryptonly --enable-fips="$$FIPS_FLAVOR" CFLAGS='-DWC_SYM_RELOC_TABLES_SUPPORT -DDEBUG_LINUXKM_PIE_SUPPORT -DWOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE_SUPPORT' $(if $(HOSTCC),CC='$(HOSTCC)')
413409
@ echo ' done.'
414410
@fi
415411
@echo -n 'Building user libwolfssl.so...'

linuxkm/linuxkm-fips-hash-wrapper.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ if ! "$AWK" --version 2>&1 | grep -F -q 'GNU Awk'; then
4545
exit 1
4646
fi
4747

48+
if [[ ! -v COREKEY ]]; then
49+
if [[ ! -v LIBWOLFSSL ]]; then
50+
LIBWOLFSSL=./libwolfssl-user-build/src/.libs/libwolfssl.so
51+
fi
52+
read -a coreKey_a < <("${READELF-readelf}" --symbols --wide "$LIBWOLFSSL" | grep --max-count=1 -E -e '[[:space:]]coreKey$') || exit $?
53+
if [[ ${#coreKey_a[@]} != 8 || "${coreKey_a[2]}" != "65" ]]; then
54+
echo "unexpected readelf output: \"${coreKey_a[*]}\" (${#coreKey_a[@]})" >&2
55+
exit 1
56+
fi
57+
coreKey_offset=$((0x${coreKey_a[1]}))
58+
COREKEY=$(dd if="$LIBWOLFSSL" bs=64 iflag=skip_bytes,count_bytes skip="$coreKey_offset" count=64 status=none) || exit $?
59+
if [[ "$COREKEY" =~ ^[0-9A-Fa-f]{64}$ ]]; then
60+
:
61+
else
62+
echo "unexpected value for coreKey \"${COREKEY}\"." >&2
63+
exit 1
64+
fi
65+
fi
66+
4867
# shellcheck disable=SC2016 # using $AWK instead of awk confuses shellcheck.
4968
readarray -t fenceposts < <(readelf --wide --sections --symbols "$mod_path" | "$AWK" '
5069
BEGIN {
@@ -110,4 +129,4 @@ BEGIN {
110129
}
111130
}')
112131

113-
./linuxkm-fips-hash "${fenceposts[@]}" --mod-path "$mod_path" --in-place "$@"
132+
./linuxkm-fips-hash "${fenceposts[@]}" --mod-path "$mod_path" --in-place --core-key="$COREKEY" "$@"

linuxkm/linuxkm_memory.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ ssize_t wc_reloc_normalize_segment(
505505

506506
#ifdef HAVE_FIPS
507507

508-
#ifdef WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE
508+
#if defined(WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE) || \
509+
defined(WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE_SUPPORT)
509510

510511
#include <wolfssl/wolfcrypt/fips_test.h>
511512
#ifndef MAX_FIPS_DATA_SZ
@@ -969,6 +970,7 @@ int wc_fips_generate_hash(
969970
return ret;
970971
}
971972

972-
#endif /* WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE */
973+
#endif /* WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE || */
974+
/* WOLFCRYPT_FIPS_CORE_DYNAMIC_HASH_VALUE_SUPPORT */
973975

974976
#endif /* HAVE_FIPS */

linuxkm/module_hooks.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,25 @@ static int wolfssl_init(void)
882882
#endif
883883

884884
#if defined(HAVE_FIPS) && FIPS_VERSION3_GT(5,2,0)
885+
886+
#ifdef WC_LINUXKM_HAVE_STACK_DEBUG
887+
{
888+
unsigned long stack_usage;
889+
stack_usage = wc_linuxkm_stack_current();
890+
pr_info("STACK INFO: usage at call to wc_RunAllCast_fips(): %lu of %lu total\n", stack_usage, THREAD_SIZE);
891+
wc_linuxkm_stack_hwm_prepare(0xee);
892+
#endif
893+
885894
ret = wc_RunAllCast_fips();
895+
896+
#ifdef WC_LINUXKM_HAVE_STACK_DEBUG
897+
stack_usage = wc_linuxkm_stack_hwm_measure_rel(0xee);
898+
pr_info("STACK INFO: rel usage by wc_RunAllCast_fips(): %lu\n", stack_usage);
899+
/* shush up false stack HWM reading by kernel: */
900+
wc_linuxkm_stack_hwm_prepare(0);
901+
}
902+
#endif
903+
886904
if (ret != 0) {
887905
pr_err("ERROR: wc_RunAllCast_fips() failed with return value %d\n", ret);
888906
return -ECANCELED;

tests/api/test_slhdsa.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ static int slhdsa_der_roundtrip_one(enum SlhDsaParam param)
13511351
byte* derBuf = NULL;
13521352
byte* sig = NULL;
13531353
const word32 derBufSz = 16 * 1024;
1354-
word32 derLen;
1354+
word32 derLen = 0; /* initialize to suppress false -Wmaybe-uninitialized */
13551355
word32 idx;
13561356
word32 sigLen;
13571357
enum SlhDsaParam placeholder = param;

0 commit comments

Comments
 (0)