Skip to content

Commit b1e5d0f

Browse files
committed
linuxkm: completion and stabilization of LKCAPI integration for AES-CBC, AES-CFB, AES-GCM, and AES-XTS:
linuxkm/lkcapi_glue.c (added in earlier commit): implement linuxkm_lkcapi_register() and linuxkm_lkcapi_unregister() with idempotency. add AES-XTS algorithm glue and self-test implementations. add per-algorithm gating: LINUXKM_LKCAPI_REGISTER_AESCBC, _AESCFB, _AESGCM, and _AESXTS. carry forward philljj's implementations for AES-CBC, AES-CFB, and AES-GCM, with various cleanups. linuxkm/module_hooks.c: print the "wolfCrypt container hashes" message only if DEBUG_LINUXKM_PIE_SUPPORT is set. render the FIPS version for the self-test success message using the HAVE_FIPS_VERSION* macros. add a "skipping full wolfcrypt_test() ..." message for --disable-crypttests builds. add CONFIG_FORTIFY_SOURCE gates. configure.ac: add support for --enable-linuxkm-lkcapi-register; add AES-XTS to output config summary; rename --enable-xts to --enable-aesxts (retaining old option for backward compatibility). linuxkm/linuxkm_wc_port.h: add support for CONFIG_FORTIFY_SOURCE. linuxkm/linuxkm_memory.c: fix retvals in save_vector_registers_x86() (wc-style MEMORY_E, not sys-style ENOMEM). add __my_fortify_panic() implementation. linuxkm/Kbuild: for ENABLED_LINUXKM_PIE in rename-pie-text-and-data-sections recipe, create an .rodata.wolfcrypt section. linuxkm/include.am: add linuxkm/lkcapi_glue.c to EXTRA_DIST. wolfcrypt/test/test.c: when defined(HAVE_FIPS_VERSION), inhibit a test clause in aes_xts_128_test() disallowed by FIPS ("FIPS AES-XTS main and tweak keys must differ"). fix out-of-order user message in ecc_test().
1 parent 39c74a9 commit b1e5d0f

10 files changed

Lines changed: 380 additions & 1319 deletions

File tree

configure.ac

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ then
745745
test "$enable_psk" = "" && enable_psk=yes
746746
test "$enable_cmac" = "" && enable_cmac=yes
747747
test "$enable_siphash" = "" && enable_siphash=yes
748-
test "$enable_xts" = "" && enable_xts=yes
748+
test "$enable_aesxts" = "" && enable_aesxts=yes
749749
test "$enable_ocsp" = "" && enable_ocsp=yes
750750
test "$enable_ocspstapling" = "" && test "$enable_ocsp" != "no" && enable_ocspstapling=yes
751751
test "$enable_ocspstapling2" = "" && test "$enable_ocsp" != "no" && enable_ocspstapling2=yes
@@ -933,7 +933,7 @@ then
933933
test "$enable_psk" = "" && enable_psk=yes
934934
test "$enable_cmac" = "" && enable_cmac=yes
935935
test "$enable_siphash" = "" && enable_siphash=yes
936-
test "$enable_xts" = "" && enable_xts=yes
936+
test "$enable_aesxts" = "" && enable_aesxts=yes
937937
test "$enable_ocsp" = "" && enable_ocsp=yes
938938
test "$enable_ocspstapling" = "" && test "$enable_ocsp" != "no" && enable_ocspstapling=yes
939939
test "$enable_ocspstapling2" = "" && test "$enable_ocsp" != "no" && enable_ocspstapling2=yes
@@ -4836,17 +4836,23 @@ AS_IF([test "x$ENABLED_CMAC" = "xyes"],
48364836

48374837

48384838
# AES-XTS
4839+
AC_ARG_ENABLE([aesxts],
4840+
[AS_HELP_STRING([--enable-aesxts],[Enable AES XTS (default: disabled)])],
4841+
[ ENABLED_AESXTS=$enableval ],
4842+
[ ENABLED_AESXTS=no ]
4843+
)
4844+
4845+
# legacy old option name, for compatibility:
48394846
AC_ARG_ENABLE([xts],
4840-
[AS_HELP_STRING([--enable-xts],[Enable XTS (default: disabled)])],
4841-
[ ENABLED_XTS=$enableval ],
4842-
[ ENABLED_XTS=no ]
4847+
[AS_HELP_STRING([--enable-xts],[Please use --enable-aesxts])],
4848+
[ ENABLED_AESXTS=$enableval ]
48434849
)
48444850

4845-
AS_IF([test "x$ENABLED_XTS" = "xyes"],
4851+
AS_IF([test "x$ENABLED_AESXTS" = "xyes"],
48464852
[AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_AES_XTS -DWOLFSSL_AES_DIRECT"])
4847-
AS_IF([test "x$ENABLED_XTS" = "xyes" && test "x$ENABLED_INTELASM" = "xyes"],
4853+
AS_IF([test "x$ENABLED_AESXTS" = "xyes" && test "x$ENABLED_INTELASM" = "xyes"],
48484854
[AM_CCASFLAGS="$AM_CCASFLAGS -DWOLFSSL_AES_XTS"])
4849-
AS_IF([test "x$ENABLED_XTS" = "xyes" && test "x$ENABLED_AESNI" = "xyes"],
4855+
AS_IF([test "x$ENABLED_AESXTS" = "xyes" && test "x$ENABLED_AESNI" = "xyes"],
48504856
[AM_CCASFLAGS="$AM_CCASFLAGS -DWOLFSSL_AES_XTS"])
48514857

48524858
# Web Server Build
@@ -8028,6 +8034,32 @@ if test -n "$MPI_MAX_KEY_BITS" -o -n "$WITH_MAX_ECC_BITS"; then
80288034
fi
80298035
fi
80308036

8037+
AC_ARG_ENABLE([linuxkm-lkcapi-register],
8038+
[AS_HELP_STRING([--enable-linuxkm-lkcapi-register],[Register wolfCrypt implementations with the Linux Kernel Crypto API backplane. Possible values are "none", "all", "cbc(aes)", "cfb(aes)", "gcm(aes)", and "xts(aes)", or a comma-separate combination. (default: none)])],
8039+
[ENABLED_LINUXKM_LKCAPI_REGISTER=$enableval],
8040+
[ENABLED_LINUXKM_LKCAPI_REGISTER=none]
8041+
)
8042+
if test "$ENABLED_LINUXKM_LKCAPI_REGISTER" != "none"
8043+
then
8044+
AM_CFLAGS="$AM_CFLAGS -DLINUXKM_LKCAPI_REGISTER"
8045+
for lkcapi_alg in $(echo "$ENABLED_LINUXKM_LKCAPI_REGISTER" | tr ',' ' ')
8046+
do
8047+
case "$lkcapi_alg" in
8048+
all) AM_CFLAGS="$AM_CFLAGS -DLINUXKM_LKCAPI_REGISTER_ALL" ;;
8049+
'cbc(aes)') test "$ENABLED_AESCBC" != "no" || AC_MSG_ERROR([linuxkm-lkcapi-register ${lkcapi_alg}: AES-CBC implementation not enabled.])
8050+
AM_CFLAGS="$AM_CFLAGS -DLINUXKM_LKCAPI_REGISTER_AESCBC" ;;
8051+
'cfb(aes)') test "$ENABLED_AESCFB" != "no" || AC_MSG_ERROR([linuxkm-lkcapi-register ${lkcapi_alg}: AES-CFB implementation not enabled.])
8052+
AM_CFLAGS="$AM_CFLAGS -DLINUXKM_LKCAPI_REGISTER_AESCFB" ;;
8053+
'gcm(aes)') test "$ENABLED_AESGCM" != "no" || AC_MSG_ERROR([linuxkm-lkcapi-register ${lkcapi_alg}: AES-GCM implementation not enabled.])
8054+
test "$ENABLED_AESGCM_STREAM" != "no" || AC_MSG_ERROR([linuxkm-lkcapi-register ${lkcapi_alg}: --enable-aesgcm-stream is required for LKCAPI.])
8055+
AM_CFLAGS="$AM_CFLAGS -DLINUXKM_LKCAPI_REGISTER_AESGCM" ;;
8056+
'xts(aes)') test "$ENABLED_AESXTS" != "no" || AC_MSG_ERROR([linuxkm-lkcapi-register ${lkcapi_alg}: AES-XTS implementation not enabled.])
8057+
AM_CFLAGS="$AM_CFLAGS -DLINUXKM_LKCAPI_REGISTER_AESXTS" ;;
8058+
*) AC_MSG_ERROR([Unsupported LKCAPI algorithm "$lkcapi_alg".]) ;;
8059+
esac
8060+
done
8061+
fi
8062+
80318063
# Library Suffix
80328064
LIBSUFFIX=""
80338065
AC_ARG_WITH([libsuffix],
@@ -8958,7 +8990,7 @@ AM_CONDITIONAL([BUILD_SNIFFER], [ test "x$ENABLED_SNIFFER" = "xyes" || test "
89588990
AM_CONDITIONAL([BUILD_SNIFFTEST],[ test "x$ENABLED_SNIFFTEST" = "xyes"])
89598991
AM_CONDITIONAL([BUILD_AESGCM],[test "x$ENABLED_AESGCM" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
89608992
AM_CONDITIONAL([BUILD_AESCCM],[test "x$ENABLED_AESCCM" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
8961-
AM_CONDITIONAL([BUILD_XTS],[test "x$ENABLED_XTS" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
8993+
AM_CONDITIONAL([BUILD_AESXTS],[test "x$ENABLED_AESXTS" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
89628994
AM_CONDITIONAL([BUILD_ARMASM],[test "x$ENABLED_ARMASM" = "xyes"])
89638995
AM_CONDITIONAL([BUILD_ARMASM_INLINE],[test "x$ENABLED_ARMASM_INLINE" = "xyes"])
89648996
AM_CONDITIONAL([BUILD_ARMASM_CRYPTO],[test "x$ENABLED_ARMASM_CRYPTO" = "xyes"])
@@ -9397,6 +9429,7 @@ echo " * AES-CCM: $ENABLED_AESCCM"
93979429
echo " * AES-CTR: $ENABLED_AESCTR"
93989430
echo " * AES-CFB: $ENABLED_AESCFB"
93999431
echo " * AES-OFB: $ENABLED_AESOFB"
9432+
echo " * AES-XTS: $ENABLED_AESXTS"
94009433
echo " * AES-SIV: $ENABLED_AESSIV"
94019434
echo " * AES-EAX: $ENABLED_AESEAX"
94029435
echo " * AES Bitspliced: $ENABLED_AESBS"

linuxkm/Kbuild

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ ifneq "$(quiet)" "silent_"
154154
endif
155155
@cd "$(obj)" || exit $$?; \
156156
for file in $(WOLFCRYPT_PIE_FILES); do \
157-
$(OBJCOPY) --rename-section .text=.text.wolfcrypt --rename-section .data=.data.wolfcrypt "$$file" || exit $$?; \
157+
$(OBJCOPY) --rename-section .text=.text.wolfcrypt --rename-section .data=.data.wolfcrypt --rename-section .rodata=.rodata.wolfcrypt "$$file" || exit $$?; \
158158
done
159159
ifneq "$(quiet)" "silent_"
160-
@echo ' wolfCrypt .{text,data} sections containerized to .{text,data}.wolfcrypt'
160+
@echo ' wolfCrypt .{text,data,rodata} sections containerized to .{text,data,rodata}.wolfcrypt'
161161
endif
162162

163163
$(src)/linuxkm/module_exports.c: rename-pie-text-and-data-sections

linuxkm/include.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ EXTRA_DIST += m4/ax_linuxkm.m4 \
1212
linuxkm/pie_redirect_table.c \
1313
linuxkm/pie_last.c \
1414
linuxkm/linuxkm_memory.c \
15-
linuxkm/linuxkm_wc_port.h
15+
linuxkm/linuxkm_wc_port.h \
16+
linuxkm/lkcapi_glue.c

linuxkm/linuxkm_memory.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ WARN_UNUSED_RESULT int save_vector_registers_x86(void)
275275
{
276276
struct wc_thread_fpu_count_ent *pstate = wc_linuxkm_fpu_state_assoc(1);
277277
if (pstate == NULL)
278-
return ENOMEM;
278+
return MEMORY_E;
279279

280280
/* allow for nested calls */
281281
if (pstate->fpu_state != 0U) {
@@ -314,7 +314,7 @@ WARN_UNUSED_RESULT int save_vector_registers_x86(void)
314314
if (! warned_fpu_forbidden)
315315
pr_err("save_vector_registers_x86 called from IRQ handler.\n");
316316
wc_linuxkm_fpu_state_release(pstate);
317-
return EPERM;
317+
return BAD_STATE_E;
318318
} else {
319319
#if defined(CONFIG_SMP) && !defined(CONFIG_PREEMPT_COUNT) && \
320320
(LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0)) && \
@@ -380,3 +380,11 @@ void my__show_free_areas(
380380
return;
381381
}
382382
#endif
383+
384+
#if defined(__PIE__) && defined(CONFIG_FORTIFY_SOURCE)
385+
/* needed because FORTIFY_SOURCE inline implementations call fortify_panic(). */
386+
void __my_fortify_panic(const char *name) {
387+
pr_emerg("__my_fortify_panic in %s\n", name);
388+
BUG();
389+
}
390+
#endif

linuxkm/linuxkm_wc_port.h

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,87 @@
119119
#include <linux/kconfig.h>
120120
#include <linux/kernel.h>
121121
#include <linux/ctype.h>
122+
123+
#ifdef CONFIG_FORTIFY_SOURCE
124+
#ifdef __PIE__
125+
/* the inline definitions in fortify-string.h use non-inline
126+
* fortify_panic().
127+
*/
128+
extern void __my_fortify_panic(const char *name) __noreturn __cold;
129+
#define fortify_panic __my_fortify_panic
130+
#endif
131+
132+
/* the _FORTIFY_SOURCE macros and implementations for several string
133+
* functions are incompatible with libwolfssl, so just reimplement with
134+
* inlines and remap with macros.
135+
*/
136+
137+
#define __ARCH_STRLEN_NO_REDIRECT
138+
#define __ARCH_MEMCPY_NO_REDIRECT
139+
#define __ARCH_MEMSET_NO_REDIRECT
140+
#define __ARCH_MEMMOVE_NO_REDIRECT
141+
142+
/* the inline definitions in fortify-string.h use non-inline
143+
* strlen().
144+
*/
145+
static inline size_t strlen(const char *s) {
146+
const char *s_start = s;
147+
while (*s)
148+
++s;
149+
return (size_t)s - (size_t)s_start;
150+
}
151+
152+
#include <linux/string.h>
153+
154+
#undef strlen
155+
#define strlen(s) \
156+
((__builtin_constant_p(s) && __builtin_constant_p(*(s))) ? \
157+
(sizeof(s) - 1) : strlen(s))
158+
159+
static inline void *my_memcpy(void *dest, const void *src, size_t n) {
160+
u8 *src_bytes = (u8 *)src,
161+
*dest_bytes = (u8 *)dest,
162+
*endp = src_bytes + n;
163+
while (src_bytes < endp)
164+
*dest_bytes++ = *src_bytes++;
165+
return dest;
166+
}
167+
#undef memcpy
168+
#define memcpy my_memcpy
169+
170+
static inline void *my_memset(void *dest, int c, size_t n) {
171+
u8 *dest_bytes = (u8 *)dest, *endp = dest_bytes + n;
172+
while (dest_bytes < endp)
173+
*dest_bytes++ = (u8)c;
174+
return dest;
175+
}
176+
#undef memset
177+
#define memset my_memset
178+
179+
static inline void *my_memmove(void *dest, const void *src, size_t n) {
180+
u8 *src_bytes = (u8 *)src, *dest_bytes = (u8 *)dest;
181+
if (src_bytes < dest_bytes) {
182+
u8 *startp = src_bytes;
183+
src_bytes += n - 1;
184+
dest_bytes += n - 1;
185+
while (src_bytes >= startp)
186+
*dest_bytes-- = *src_bytes--;
187+
} else if (src_bytes > dest_bytes) {
188+
u8 *endp = src_bytes + n;
189+
while (src_bytes < endp)
190+
*dest_bytes++ = *src_bytes++;
191+
}
192+
return dest;
193+
}
194+
#undef memmove
195+
#define memmove my_memmove
196+
197+
#endif /* CONFIG_FORTIFY_SOURCE */
198+
122199
#include <linux/init.h>
123200
#include <linux/module.h>
201+
#include <linux/delay.h>
202+
124203
#ifdef __PIE__
125204
/* without this, mm.h brings in static, but not inline, pmd_to_page(),
126205
* with direct references to global vmem variables.
@@ -146,7 +225,7 @@
146225
#include <linux/net.h>
147226
#include <linux/slab.h>
148227

149-
#ifdef LINUXKM_REGISTER_ALG
228+
#ifdef LINUXKM_LKCAPI_REGISTER
150229
#include <linux/crypto.h>
151230
#include <linux/scatterlist.h>
152231
#include <crypto/scatterwalk.h>
@@ -303,6 +382,11 @@
303382
#else
304383
typeof(printk) *printk;
305384
#endif
385+
386+
#ifdef CONFIG_FORTIFY_SOURCE
387+
typeof(__warn_printk) *__warn_printk;
388+
#endif
389+
306390
typeof(snprintf) *snprintf;
307391

308392
const unsigned char *_ctype;
@@ -446,6 +530,11 @@
446530
#else
447531
#define printk (wolfssl_linuxkm_get_pie_redirect_table()->printk)
448532
#endif
533+
534+
#ifdef CONFIG_FORTIFY_SOURCE
535+
#define __warn_printk (wolfssl_linuxkm_get_pie_redirect_table()->__warn_printk)
536+
#endif
537+
449538
#define snprintf (wolfssl_linuxkm_get_pie_redirect_table()->snprintf)
450539

451540
#define _ctype (wolfssl_linuxkm_get_pie_redirect_table()->_ctype)

0 commit comments

Comments
 (0)