Skip to content

Commit 2ac4be3

Browse files
authored
Merge pull request #8773 from douzzer/20250514-linuxkm-and-sp-tweaks
20250514-linuxkm-and-sp-tweaks
2 parents baaab4c + c201006 commit 2ac4be3

4 files changed

Lines changed: 67 additions & 53 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ CONFIG_CRYPTO_AES
5656
CONFIG_CRYPTO_CBC
5757
CONFIG_CRYPTO_CTR
5858
CONFIG_CRYPTO_DH
59+
CONFIG_CRYPTO_DH_RFC7919_GROUPS
5960
CONFIG_CRYPTO_ECB
6061
CONFIG_CRYPTO_ECDH
6162
CONFIG_CRYPTO_ECDSA

linuxkm/lkcapi_dh_glue.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,24 @@
5656
#undef LINUXKM_LKCAPI_REGISTER_DH
5757
#endif
5858

59+
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
60+
/* Support for FFDHE was added in kernel 5.18, and generic DH support
61+
* pre-5.18 used a different binary format for the secret (an additional
62+
* slot for q).
63+
*
64+
* LTS backports of FFDHE go as far back as 4.14.236, using the pre-5.18
65+
* binary layout, but other backports, e.g. RHEL 9.5 on kernel
66+
* 5.14.0-503.40.1, have the 5.18+ layout. Best to disable on all pre-5.18
67+
* and triage as/if necessary.
68+
*/
69+
#undef LINUXKM_LKCAPI_REGISTER_DH
70+
#endif
71+
5972
#if defined(LINUXKM_LKCAPI_REGISTER_ALL_KCONFIG) && \
60-
defined(CONFIG_CRYPTO_DH) && \
73+
(defined(CONFIG_CRYPTO_DH) || defined(CONFIG_CRYPTO_DH_RFC7919_GROUPS)) && \
6174
!defined(LINUXKM_LKCAPI_REGISTER_DH)
62-
#error Config conflict: target kernel has CONFIG_CRYPTO_DH, but module is missing LINUXKM_LKCAPI_REGISTER_DH.
75+
#error Config conflict: target kernel has CONFIG_CRYPTO_DH and/or \
76+
_DH_RFC7919_GROUPS, but module is missing LINUXKM_LKCAPI_REGISTER_DH.
6377
#endif
6478

6579
#if defined(LINUXKM_LKCAPI_REGISTER_DH)

wolfcrypt/src/sp_int.c

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,6 @@ This library provides single precision (SP) integer math functions.
3131

3232
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
3333

34-
#if (!defined(WOLFSSL_SMALL_STACK) && !defined(SP_ALLOC)) || \
35-
defined(WOLFSSL_SP_NO_MALLOC)
36-
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
37-
!defined(WOLFSSL_SP_NO_DYN_STACK)
38-
PRAGMA_GCC_DIAG_PUSH
39-
/* We are statically declaring a variable smaller than sp_int.
40-
* We track available memory in the 'size' field.
41-
* Disable warnings of sp_int being partly outside array bounds of variable.
42-
*/
43-
PRAGMA_GCC("GCC diagnostic ignored \"-Warray-bounds\"")
44-
#endif
45-
#endif
46-
4734
#ifdef NO_INLINE
4835
#include <wolfssl/wolfcrypt/misc.h>
4936
#else
@@ -112,6 +99,15 @@ PRAGMA_GCC("GCC diagnostic ignored \"-Warray-bounds\"")
11299

113100
#include <wolfssl/wolfcrypt/sp_int.h>
114101

102+
#ifdef WOLFSSL_SP_DYN_STACK
103+
/* We are statically declaring a variable smaller than sp_int.
104+
* We track available memory in the 'size' field.
105+
* Disable warnings of sp_int being partly outside array bounds of variable.
106+
*/
107+
PRAGMA_GCC_DIAG_PUSH
108+
PRAGMA_GCC("GCC diagnostic ignored \"-Warray-bounds\"")
109+
#endif
110+
115111
#if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
116112
/* force off unneeded vector register save/restore. */
117113
#undef SAVE_VECTOR_REGISTERS
@@ -127,8 +123,7 @@ PRAGMA_GCC("GCC diagnostic ignored \"-Warray-bounds\"")
127123
#define DECL_SP_INT(n, s) \
128124
sp_int* n = NULL
129125
#else
130-
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
131-
!defined(WOLFSSL_SP_NO_DYN_STACK)
126+
#ifdef WOLFSSL_SP_DYN_STACK
132127
/* Declare a variable on the stack with the required data size. */
133128
#define DECL_SP_INT(n, s) \
134129
sp_int_digit n##d[MP_INT_SIZEOF_DIGITS(s)]; \
@@ -218,8 +213,7 @@ PRAGMA_GCC("GCC diagnostic ignored \"-Warray-bounds\"")
218213
/* Declare a variable that will be assigned a value on XMALLOC. */
219214
#define DECL_SP_INT_ARRAY(n, s, c) \
220215
DECL_DYN_SP_INT_ARRAY(n, s, c)
221-
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
222-
!defined(WOLFSSL_SP_NO_DYN_STACK)
216+
#elif defined(WOLFSSL_SP_DYN_STACK)
223217
/* Declare a variable on the stack with the required data size. */
224218
#define DECL_SP_INT_ARRAY(n, s, c) \
225219
sp_int_digit n##d[MP_INT_SIZEOF_DIGITS(s) * (c)]; \
@@ -264,8 +258,7 @@ while (0)
264258
!defined(WOLFSSL_SP_NO_MALLOC)
265259
#define ALLOC_SP_INT_ARRAY(n, s, c, err, h) \
266260
ALLOC_DYN_SP_INT_ARRAY(n, s, c, err, h)
267-
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
268-
!defined(WOLFSSL_SP_NO_DYN_STACK)
261+
#elif defined(WOLFSSL_SP_DYN_STACK)
269262
/* Data declared on stack that supports multiple sp_ints of the
270263
* required size. Use pointers into data to make up array and set sizes.
271264
*/
@@ -9175,8 +9168,7 @@ static int _sp_mul_nxn(const sp_int* a, const sp_int* b, sp_int* r)
91759168
unsigned int k;
91769169
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC)
91779170
sp_int_digit* t = NULL;
9178-
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
9179-
!defined(WOLFSSL_SP_NO_DYN_STACK)
9171+
#elif defined(WOLFSSL_SP_DYN_STACK)
91809172
sp_int_digit t[a->used];
91819173
#else
91829174
sp_int_digit t[SP_INT_DIGITS / 2];
@@ -9252,8 +9244,7 @@ static int _sp_mul(const sp_int* a, const sp_int* b, sp_int* r)
92529244
sp_size_t k;
92539245
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC)
92549246
sp_int_digit* t = NULL;
9255-
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
9256-
!defined(WOLFSSL_SP_NO_DYN_STACK)
9247+
#elif defined(WOLFSSL_SP_DYN_STACK)
92579248
sp_int_digit t[a->used + b->used];
92589249
#else
92599250
sp_int_digit t[SP_INT_DIGITS];
@@ -9329,8 +9320,7 @@ static int _sp_mul(const sp_int* a, const sp_int* b, sp_int* r)
93299320
sp_size_t k;
93309321
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC)
93319322
sp_int_digit* t = NULL;
9332-
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
9333-
!defined(WOLFSSL_SP_NO_DYN_STACK)
9323+
#elif defined(WOLFSSL_SP_DYN_STACK)
93349324
sp_int_digit t[a->used + b->used];
93359325
#else
93369326
sp_int_digit t[SP_INT_DIGITS];
@@ -14879,8 +14869,7 @@ static int _sp_sqr(const sp_int* a, sp_int* r)
1487914869
sp_size_t k;
1488014870
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC)
1488114871
sp_int_digit* t = NULL;
14882-
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
14883-
!defined(WOLFSSL_SP_NO_DYN_STACK)
14872+
#elif defined(WOLFSSL_SP_DYN_STACK)
1488414873
sp_int_digit t[((a->used + 1) / 2) * 2 + 1];
1488514874
#else
1488614875
sp_int_digit t[(SP_INT_DIGITS + 1) / 2];
@@ -14994,8 +14983,7 @@ static int _sp_sqr(const sp_int* a, sp_int* r)
1499414983
sp_size_t k;
1499514984
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC)
1499614985
sp_int_digit* t = NULL;
14997-
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
14998-
!defined(WOLFSSL_SP_NO_DYN_STACK)
14986+
#elif defined(WOLFSSL_SP_DYN_STACK)
1499914987
sp_int_digit t[a->used * 2];
1500014988
#else
1500114989
sp_int_digit t[SP_INT_DIGITS];
@@ -19891,12 +19879,8 @@ void sp_memzero_check(sp_int* sp)
1989119879
}
1989219880
#endif /* WOLFSSL_CHECK_MEM_ZERO */
1989319881

19894-
#if (!defined(WOLFSSL_SMALL_STACK) && !defined(SP_ALLOC)) || \
19895-
defined(WOLFSSL_SP_NO_MALLOC)
19896-
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
19897-
!defined(WOLFSSL_SP_NO_DYN_STACK)
19898-
PRAGMA_GCC_DIAG_POP
19899-
#endif
19882+
#ifdef WOLFSSL_SP_DYN_STACK
19883+
PRAGMA_GCC_DIAG_POP
1990019884
#endif
1990119885

1990219886
#endif /* WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */

wolfssl/wolfcrypt/sp_int.h

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ extern "C" {
260260
#endif
261261
#endif
262262

263-
/* Number of bytes in each word. */
264-
#define SP_WORD_SIZEOF (SP_WORD_SIZE / 8)
265-
266263
/* Define the types used. */
267264
#if defined(HAVE___UINT128_T) && !defined(NO_INT128)
268265
#ifdef __SIZEOF_INT128__
@@ -285,27 +282,35 @@ extern "C" {
285282
#endif
286283

287284
#if SP_WORD_SIZE == 8
285+
#define SP_WORD_SIZEOF 1
286+
288287
typedef sp_uint8 sp_int_digit;
289288
typedef sp_int8 sp_int_sdigit;
290289
typedef sp_uint16 sp_int_word;
291290
typedef sp_int16 sp_int_sword;
292291

293292
#define SP_MASK 0xffU
294293
#elif SP_WORD_SIZE == 16
294+
#define SP_WORD_SIZEOF 2
295+
295296
typedef sp_uint16 sp_int_digit;
296297
typedef sp_int16 sp_int_sdigit;
297298
typedef sp_uint32 sp_int_word;
298299
typedef sp_int32 sp_int_sword;
299300

300301
#define SP_MASK 0xffffU
301302
#elif SP_WORD_SIZE == 32
303+
#define SP_WORD_SIZEOF 4
304+
302305
typedef sp_uint32 sp_int_digit;
303306
typedef sp_int32 sp_int_sdigit;
304307
typedef sp_uint64 sp_int_word;
305308
typedef sp_int64 sp_int_sword;
306309

307310
#define SP_MASK 0xffffffffU
308311
#elif SP_WORD_SIZE == 64
312+
#define SP_WORD_SIZEOF 8
313+
309314
typedef sp_uint64 sp_int_digit;
310315
typedef sp_int64 sp_int_sdigit;
311316
#if (defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
@@ -788,11 +793,19 @@ typedef struct sp_ecc_ctx {
788793
#define MP_INT_NEXT(t, cnt) \
789794
(sp_int*)(((byte*)(t)) + MP_INT_SIZEOF(cnt))
790795

796+
#define MP_INT_SIZEOF_DIGITS(cnt) (MP_INT_SIZEOF(cnt) / sizeof(sp_int_digit))
791797

792798
/* Calculate the number of words required to support a number of bits. */
793799
#define MP_BITS_CNT(bits) \
794800
((unsigned int)(((((bits) + SP_WORD_SIZE - 1) / SP_WORD_SIZE) * 2 + 1)))
795801

802+
#if !defined(WOLFSSL_SP_NO_DYN_STACK) && defined(__STDC_VERSION__) && \
803+
(__STDC_VERSION__ >= 199901L) && \
804+
(defined(WOLFSSL_SP_NO_MALLOC) || \
805+
!(defined(WOLFSSL_SMALL_STACK) || defined(SP_ALLOC)))
806+
#define WOLFSSL_SP_DYN_STACK
807+
#endif
808+
796809
#ifdef WOLFSSL_SMALL_STACK
797810
/*
798811
* Dynamic memory allocation of mp_int.
@@ -823,26 +836,25 @@ while (0)
823836
/*
824837
* Static allocation of mp_int.
825838
*/
826-
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
827-
!defined(WOLFSSL_SP_NO_DYN_STACK)
839+
#ifdef WOLFSSL_SP_DYN_STACK
828840
/* Declare a dynamically allocated mp_int. */
829-
#define DECL_MP_INT_SIZE_DYN(name, bits, max) \
830-
unsigned char name##d[MP_INT_SIZEOF(MP_BITS_CNT(bits))]; \
841+
#define DECL_MP_INT_SIZE_DYN(name, bits, max) \
842+
sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(bits))]; \
831843
sp_int* (name) = (sp_int*)name##d
832844
#elif defined(__cplusplus)
833845
/* C++ doesn't tolerate parentheses around "name" (-Wparentheses) */
834-
#define DECL_MP_INT_SIZE_DYN(name, bits, max) \
835-
unsigned char name##d[MP_INT_SIZEOF(MP_BITS_CNT(max))]; \
846+
#define DECL_MP_INT_SIZE_DYN(name, bits, max) \
847+
sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(max))]; \
836848
sp_int* name = (sp_int*)name##d
837849
#else
838850
/* Declare a dynamically allocated mp_int. */
839-
#define DECL_MP_INT_SIZE_DYN(name, bits, max) \
840-
unsigned char name##d[MP_INT_SIZEOF(MP_BITS_CNT(max))]; \
851+
#define DECL_MP_INT_SIZE_DYN(name, bits, max) \
852+
sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(max))]; \
841853
sp_int* (name) = (sp_int*)name##d
842854
#endif
843855
/* Declare a statically allocated mp_int. */
844-
#define DECL_MP_INT_SIZE(name, bits) \
845-
unsigned char name##d[MP_INT_SIZEOF(MP_BITS_CNT(bits))]; \
856+
#define DECL_MP_INT_SIZE(name, bits) \
857+
sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(bits))]; \
846858
sp_int* (name) = (sp_int*)name##d
847859
/* Zero out mp_int of minimal size. */
848860
#define NEW_MP_INT_SIZE(name, bits, heap, type) \
@@ -910,7 +922,7 @@ typedef struct sp_int {
910922
struct WC_BIGINT raw;
911923
#endif
912924
/** Data of number. */
913-
sp_int_digit dp[SP_INT_DIGITS];
925+
XALIGNED(SP_WORD_SIZEOF) sp_int_digit dp[SP_INT_DIGITS];
914926
} sp_int;
915927

916928
typedef struct sp_int_minimal {
@@ -927,11 +939,14 @@ typedef struct sp_int_minimal {
927939
struct WC_BIGINT raw;
928940
#endif
929941
/** First digit of number. */
930-
sp_int_digit dp[1];
942+
XALIGNED(SP_WORD_SIZEOF) sp_int_digit dp[1];
931943
} sp_int_minimal;
932944

945+
/* MP_INT_SIZEOF_DIGITS() requires that sizeof(sp_int) is a multiple of
946+
* sizeof(sp_int_digit).
947+
*/
948+
wc_static_assert(sizeof(struct sp_int) % sizeof(sp_int_digit) == 0);
933949
wc_static_assert(sizeof(struct sp_int_minimal) % sizeof(sp_int_digit) == 0);
934-
#define MP_INT_SIZEOF_DIGITS(cnt) (MP_INT_SIZEOF(cnt) / sizeof(sp_int_digit))
935950

936951
/* Multi-precision integer type is SP integer type. */
937952
typedef sp_int mp_int;

0 commit comments

Comments
 (0)