Skip to content

Commit 28e4fe3

Browse files
committed
bsdkm: initial wolfcrypt FreeBSD kernel module support.
1 parent 46a7719 commit 28e4fe3

File tree

20 files changed

+769
-47
lines changed

20 files changed

+769
-47
lines changed

.codespellexcludelines

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ rsource "Kconfig.tls-generic"
1616
\pagenumbering{alph}
1717
DES3_KEY_SIZE = 24, /* 3 des ede */
1818
/* functions added to support above needed, removed TOOM and KARATSUBA */
19+
#include <sys/systm.h>
20+
* extern global version from /usr/src/sys/sys/systm.h */

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ linuxkm/linuxkm
249249
linuxkm/src
250250
linuxkm/patches/src
251251
*.nds
252+
bsdkm/export_syms
253+
bsdkm/i386
254+
bsdkm/libwolfssl.ko
255+
bsdkm/machine
256+
bsdkm/opt_global.h
257+
bsdkm/x86
252258

253259
# autotools generated
254260
scripts/unit.test

.wolfssl_known_macro_extras

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ BASE64_NO_TABLE
3535
BLAKE2B_SELFTEST
3636
BLAKE2S_SELFTEST
3737
BLOCKING
38+
BSDKM_EXPORT_SYMS
3839
BSP_DEFAULT_IO_CHANNEL_DEFINED
3940
BSP_LED_0
4041
BSP_LED_1
@@ -305,6 +306,7 @@ INTIMEVER
305306
IOTSAFE_NO_GETDATA
306307
IOTSAFE_SIG_8BIT_LENGTH
307308
KCAPI_USE_XMALLOC
309+
KERNEL_ROOT
308310
K_SERIES
309311
LIBWOLFSSL_VERSION_GIT_BRANCH
310312
LIBWOLFSSL_VERSION_GIT_HASH

Makefile.am

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ include sslSniffer/sslSnifferTest/include.am
179179
include debian/include.am
180180
include rpm/include.am
181181
include linuxkm/include.am
182+
include bsdkm/include.am
182183
include zephyr/include.am
183184
include RTOS/nuttx/include.am
184185

@@ -220,7 +221,7 @@ if BUILD_LINUXKM
220221
CFLAGS_FPU_DISABLE CFLAGS_FPU_ENABLE CFLAGS_SIMD_DISABLE CFLAGS_SIMD_ENABLE \
221222
CFLAGS_AUTO_VECTORIZE_DISABLE CFLAGS_AUTO_VECTORIZE_ENABLE \
222223
ASFLAGS_FPU_DISABLE_SIMD_ENABLE ASFLAGS_FPU_ENABLE_SIMD_DISABLE \
223-
ASFLAGS_FPUSIMD_DISABLE ASFLAGS_FPUSIMD_ENABLE ENABLED_LINUXKM_BENCHMARKS
224+
ASFLAGS_FPUSIMD_DISABLE ASFLAGS_FPUSIMD_ENABLE ENABLED_KERNEL_BENCHMARKS
224225

225226
module:
226227
+$(MAKE) -C linuxkm libwolfssl.ko
@@ -236,6 +237,20 @@ install_module modules_install:
236237

237238
endif
238239

240+
if BUILD_BSDKM
241+
SUBDIRS_OPT += bsdkm
242+
DIST_SUBDIRS_OPT += bsdkm
243+
244+
.MAKE.EXPORTED = build_triplet host_triplet CC AS LD \
245+
KERNEL_ROOT BSDKM_EXPORT_SYMS KERNEL_EXTRA_CFLAGS \
246+
EXTRA_CFLAGS EXTRA_CPPFLAGS EXTRA_CCASFLAGS EXTRA_LDFLAGS \
247+
AM_CPPFLAGS CPPFLAGS AM_CFLAGS CFLAGS \
248+
AM_CCASFLAGS CCASFLAGS \
249+
src_libwolfssl_la_OBJECTS ENABLED_CRYPT_TESTS
250+
251+
endif
252+
253+
239254
if USE_VALGRIND
240255
TESTS_ENVIRONMENT=./valgrind-error.sh
241256
endif

bsdkm/Makefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# wolfssl module name and source.
2+
KMOD=libwolfssl
3+
SRCS=wolfkmod.c
4+
WOLFSSL_DIR=../
5+
6+
CFLAGS+=-I${WOLFSSL_DIR}
7+
CFLAGS+=-DWOLFSSL_IGNORE_FILE_WARN -DHAVE_CONFIG_H
8+
CFLAGS+=-DNO_MAIN_DRIVER
9+
# debug mode
10+
CFLAGS+=-DWOLFSSL_BSDKM_VERBOSE_DEBUG
11+
CFLAGS+=$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS)
12+
13+
# FreeBSD make does not support GNU make's patsubst and related. Filter
14+
# through sed instead.
15+
OBJS != echo ${src_libwolfssl_la_OBJECTS} | sed 's|src_libwolfssl_la-||g' | \
16+
sed 's|\.lo|.o|g' | sed 's|wolfcrypt/src/|${WOLFSSL_DIR}/wolfcrypt/src/|g'
17+
18+
.if ${ENABLED_CRYPT_TESTS} == "yes"
19+
OBJS += ${WOLFSSL_DIR}/wolfcrypt/test/test.o
20+
.else
21+
CFLAGS+=-DNO_CRYPT_TEST
22+
.endif
23+
24+
# Export no public symbols by default.
25+
.if !defined(BSDKM_EXPORT_SYMS)
26+
EXPORT_SYMS=NO
27+
.else
28+
EXPORT_SYMS=${BSDKM_EXPORT_SYMS}
29+
.endif
30+
31+
# Default to live kernel src tree makefile at
32+
# /usr/src/sys/conf/kmod.mk
33+
.if !defined(KERNEL_ROOT)
34+
SYSDIR?= /usr/src/sys
35+
.else
36+
SYSDIR?= ${KERNEL_ROOT}
37+
.endif
38+
.include "${SYSDIR}/conf/kmod.mk"
39+
40+
# Smooth out a few inconsistencies between FreeBSD default compiler flags
41+
# in /usr/src/sys/conf/kern.mk, vs wolfssl harden flags in
42+
# m4/ax_harden_compiler_flags.m4. E.g. some FreeBSD header files shorten
43+
# 64 to 32 bit, and some wolfcrypt functions cast away const.
44+
CFLAGS+= -Wno-unused-function
45+
CFLAGS+= -Wno-cast-qual
46+
CFLAGS+= -Wno-error=cast-qual
47+
CFLAGS+= -Wno-shorten-64-to-32
48+
CFLAGS+= -DLIBWOLFSSL_GLOBAL_EXTRA_CFLAGS="\" $(KERNEL_EXTRA_CFLAGS)\""

bsdkm/bsdkm_wc_port.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* bsdkm_wc_port.h
2+
*
3+
* Copyright (C) 2006-2025 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
/* included by wolfssl/wolfcrypt/wc_port.h */
23+
24+
#ifndef BSDKM_WC_PORT_H
25+
#define BSDKM_WC_PORT_H
26+
27+
#ifdef WOLFSSL_BSDKM
28+
29+
#include <sys/ctype.h>
30+
#include <sys/types.h>
31+
#include <sys/malloc.h>
32+
#include <sys/systm.h>
33+
#if !defined(SINGLE_THREADED)
34+
#include <sys/mutex.h>
35+
#endif /* !SINGLE_THREADED */
36+
#ifndef CHAR_BIT
37+
#include <sys/limits.h>
38+
#endif /* !CHAR_BIT*/
39+
40+
/* needed to prevent wolfcrypt/src/asn.c version shadowing
41+
* extern global version from /usr/src/sys/sys/systm.h */
42+
#define version wc_version
43+
44+
#define wc_km_printf printf
45+
46+
/* str and char utility functions */
47+
#define XATOI(s) ({ \
48+
char * endptr = NULL; \
49+
long _xatoi_ret = strtol(s, &endptr, 10); \
50+
if ((s) == endptr || *endptr != '\0') { \
51+
_xatoi_ret = 0; \
52+
} \
53+
(int)_xatoi_ret; \
54+
})
55+
56+
#if !defined(XMALLOC_OVERRIDE)
57+
#error bsdkm requires XMALLOC_OVERRIDE
58+
#endif /* !XMALLOC_OVERRIDE */
59+
60+
/* use malloc and free from /usr/include/sys/malloc.h */
61+
extern struct malloc_type M_WOLFSSL[1];
62+
63+
#define XMALLOC(s, h, t) \
64+
({(void)(h); (void)(t); malloc(s, M_WOLFSSL, M_WAITOK | M_ZERO);})
65+
66+
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
67+
#define XFREE(p, h, t) \
68+
({(void)(h); (void)(t); free(p, M_WOLFSSL);})
69+
#else
70+
#define XFREE(p, h, t) \
71+
({void* _xp; (void)(h); (void)(t); _xp = (p); \
72+
if(_xp) free(_xp, M_WOLFSSL);})
73+
#endif
74+
75+
#if !defined(SINGLE_THREADED)
76+
#define WC_MUTEX_OPS_INLINE
77+
78+
typedef struct wolfSSL_Mutex {
79+
struct mtx lock;
80+
} wolfSSL_Mutex;
81+
82+
static __always_inline int wc_InitMutex(wolfSSL_Mutex * m)
83+
{
84+
mtx_init(&m->lock, "wolfssl spinlock", NULL, MTX_SPIN);
85+
return 0;
86+
}
87+
88+
static __always_inline int wc_FreeMutex(wolfSSL_Mutex * m)
89+
{
90+
mtx_destroy(&m->lock);
91+
return 0;
92+
}
93+
94+
static __always_inline int wc_LockMutex(wolfSSL_Mutex *m)
95+
{
96+
mtx_lock_spin(&m->lock);
97+
return 0;
98+
}
99+
100+
static __always_inline int wc_UnLockMutex(wolfSSL_Mutex* m)
101+
{
102+
mtx_unlock_spin(&m->lock);
103+
return 0;
104+
}
105+
#endif /* !SINGLE_THREADED */
106+
107+
#if defined(WOLFSSL_HAVE_ATOMIC_H) && !defined(WOLFSSL_NO_ATOMICS)
108+
#include <machine/atomic.h>
109+
typedef volatile int wolfSSL_Atomic_Int;
110+
typedef volatile unsigned int wolfSSL_Atomic_Uint;
111+
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
112+
#define WOLFSSL_ATOMIC_LOAD(x) (int)atomic_load_acq_int(&(x))
113+
#define WOLFSSL_ATOMIC_STORE(x, v) atomic_store_rel_int(&(x), (v))
114+
#define WOLFSSL_ATOMIC_OPS
115+
#endif /* WOLFSSL_HAVE_ATOMIC_H && !WOLFSSL_NO_ATOMICS */
116+
117+
#endif /* WOLFSSL_BSDKM */
118+
#endif /* BSDKM_WC_PORT_H */

bsdkm/include.am

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# vim:ft=automake
2+
# included from Top Level Makefile.am
3+
# All paths should be given relative to the root
4+
5+
EXTRA_DIST += m4/ax_bsdkm.m4 \
6+
bsdkm/Makefile \
7+
bsdkm/wolfkmod.c \
8+
bsdkm/bsdkm_wc_port.h

0 commit comments

Comments
 (0)