Skip to content

Commit 6146485

Browse files
committed
linuxkm/linuxkm_wc_port.h:
* add support for DEBUG_LINUXKM_FORTIFY_OVERLAY to allow KASAN analysis of the overlay without actually enabling CONFIG_FORTIFY_SOURCE (which is buggy in combination with KASAN). * make SAVE_VECTOR_REGISTERS2 definition conditional on !defined(SAVE_VECTOR_REGISTERS2). wolfssl/wolfcrypt/memory.h: fix the DEBUG_VECTOR_REGISTER_ACCESS definition for SAVE_VECTOR_REGISTERS to properly omit the on-success bookkeeping code even if the supplied fail_clause doesn't return. wolfcrypt/src/rsa.c: in wc_MakeRsaKey() primality loop, invoke RESTORE_VECTOR_REGISTERS() SAVE_VECTOR_REGISTERS() to prevent lengthy kernel lockups. wolfcrypt/src/dh.c: in wc_DhGenerateParams() primality loop, invoke RESTORE_VECTOR_REGISTERS() SAVE_VECTOR_REGISTERS() to prevent lengthy kernel lockups. wolfcrypt/src/{curve25519.c,dh.c,dsa.c,ecc.c,eccsi.c,rsa.c,sakke.c,sp_int.c}: when WOLFSSL_LINUXKM, force {SAVE,RESTORE}_VECTOR_REGISTERS() to WC_DO_NOTHING if settings gate out applicable asm.
1 parent 91681f3 commit 6146485

10 files changed

Lines changed: 103 additions & 22 deletions

File tree

linuxkm/linuxkm_wc_port.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
#include <linux/kernel.h>
121121
#include <linux/ctype.h>
122122

123-
#ifdef CONFIG_FORTIFY_SOURCE
123+
#if defined(CONFIG_FORTIFY_SOURCE) || defined(DEBUG_LINUXKM_FORTIFY_OVERLAY)
124124
#ifdef __PIE__
125125
/* the inline definitions in fortify-string.h use non-inline
126126
* fortify_panic().
@@ -345,6 +345,8 @@
345345
fail_clause \
346346
} \
347347
}
348+
#endif
349+
#ifndef SAVE_VECTOR_REGISTERS2
348350
#ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
349351
#define SAVE_VECTOR_REGISTERS2() ({ \
350352
int _fuzzer_ret = SAVE_VECTOR_REGISTERS2_fuzzer(); \
@@ -363,6 +365,8 @@
363365
#include <asm/fpsimd.h>
364366
#ifndef SAVE_VECTOR_REGISTERS
365367
#define SAVE_VECTOR_REGISTERS(fail_clause) { int _svr_ret = save_vector_registers_arm(); if (_svr_ret != 0) { fail_clause } }
368+
#endif
369+
#ifndef SAVE_VECTOR_REGISTERS2
366370
#define SAVE_VECTOR_REGISTERS2() save_vector_registers_arm()
367371
#endif
368372
#ifndef RESTORE_VECTOR_REGISTERS

wolfcrypt/src/curve25519.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
#include <wolfssl/wolfcrypt/cryptocb.h>
5252
#endif
5353

54+
#if defined(WOLFSSL_LINUXKM) && !defined(USE_INTEL_SPEEDUP)
55+
/* force off unneeded vector register save/restore. */
56+
#undef SAVE_VECTOR_REGISTERS
57+
#define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
58+
#undef RESTORE_VECTOR_REGISTERS
59+
#define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
60+
#endif
61+
5462
const curve25519_set_type curve25519_sets[] = {
5563
{
5664
CURVE25519_KEYSIZE,

wolfcrypt/src/dh.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
#include <wolfcrypt/src/misc.c>
5656
#endif
5757

58+
#if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
59+
/* force off unneeded vector register save/restore. */
60+
#undef SAVE_VECTOR_REGISTERS
61+
#define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
62+
#undef RESTORE_VECTOR_REGISTERS
63+
#define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
64+
#endif
5865

5966
/*
6067
Possible DH enable options:
@@ -3003,7 +3010,7 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh)
30033010

30043011
/* loop until p is prime */
30053012
if (ret == 0) {
3006-
do {
3013+
for (;;) {
30073014
if (mp_prime_is_prime_ex(&dh->p, 8, &primeCheck, rng) != MP_OKAY)
30083015
ret = PRIME_GEN_E;
30093016

@@ -3014,7 +3021,14 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh)
30143021
else
30153022
primeCheckCount++;
30163023
}
3017-
} while (ret == 0 && primeCheck == MP_NO);
3024+
3025+
if (ret != 0 || primeCheck == MP_YES)
3026+
break;
3027+
3028+
/* linuxkm: release the kernel for a moment before iterating. */
3029+
RESTORE_VECTOR_REGISTERS();
3030+
SAVE_VECTOR_REGISTERS(ret = _svr_ret; break;);
3031+
};
30183032
}
30193033

30203034
/* tmp2 += (2*loop_check_prime)

wolfcrypt/src/dsa.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
#include <wolfcrypt/src/misc.c>
4343
#endif
4444

45+
#if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
46+
/* force off unneeded vector register save/restore. */
47+
#undef SAVE_VECTOR_REGISTERS
48+
#define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
49+
#undef RESTORE_VECTOR_REGISTERS
50+
#define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
51+
#endif
52+
4553
#ifdef _MSC_VER
4654
/* disable for while(0) cases (MSVC bug) */
4755
#pragma warning(disable:4127)

wolfcrypt/src/ecc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ ECC Curve Sizes:
213213
#include <wolfssl/wolfcrypt/hmac.h>
214214
#endif
215215

216+
#if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
217+
/* force off unneeded vector register save/restore. */
218+
#undef SAVE_VECTOR_REGISTERS
219+
#define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
220+
#undef RESTORE_VECTOR_REGISTERS
221+
#define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
222+
#endif
223+
216224
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
217225
#define GEN_MEM_ERR MP_MEM
218226
#elif defined(USE_FAST_MATH)

wolfcrypt/src/eccsi.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
#include <wolfssl/wolfcrypt/sp.h>
4444
#endif
4545

46+
#if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
47+
/* force off unneeded vector register save/restore. */
48+
#undef SAVE_VECTOR_REGISTERS
49+
#define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
50+
#undef RESTORE_VECTOR_REGISTERS
51+
#define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
52+
#endif
53+
4654
#ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
4755
/* FIPS build has replaced ecc.h. */
4856
#define wc_ecc_key_get_priv(key) (&((key)->k))

wolfcrypt/src/rsa.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ RSA keys can be used to encrypt, decrypt, sign and verify data.
6262
#include <wolfssl/wolfcrypt/sp.h>
6363
#endif
6464

65+
#if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
66+
/* force off unneeded vector register save/restore. */
67+
#undef SAVE_VECTOR_REGISTERS
68+
#define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
69+
#undef RESTORE_VECTOR_REGISTERS
70+
#define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
71+
#endif
72+
6573
/*
6674
Possible RSA enable options:
6775
* NO_RSA: Overall control of RSA default: on
@@ -712,8 +720,7 @@ int wc_CheckRsaKey(RsaKey* key)
712720

713721
ret = wc_InitRng(rng);
714722

715-
if (ret == 0)
716-
SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
723+
SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
717724

718725
if (ret == 0) {
719726
if (INIT_MP_INT_SIZE(tmp, mp_bitsused(&key->n)) != MP_OKAY)
@@ -4830,7 +4837,7 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
48304837
#endif
48314838
isPrime = 0;
48324839
i = 0;
4833-
do {
4840+
for (;;) {
48344841
#ifdef SHOW_GEN
48354842
printf(".");
48364843
fflush(stdout);
@@ -4853,9 +4860,15 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
48534860
i++;
48544861
#else
48554862
/* Keep the old retry behavior in non-FIPS build. */
4856-
(void)i;
48574863
#endif
4858-
} while (err == MP_OKAY && !isPrime && i < failCount);
4864+
4865+
if (err != MP_OKAY || isPrime || i >= failCount)
4866+
break;
4867+
4868+
/* linuxkm: release the kernel for a moment before iterating. */
4869+
RESTORE_VECTOR_REGISTERS();
4870+
SAVE_VECTOR_REGISTERS(err = _svr_ret; break;);
4871+
};
48594872
}
48604873

48614874
if (err == MP_OKAY && !isPrime)

wolfcrypt/src/sakke.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
#include <wolfssl/wolfcrypt/sakke.h>
4545
#include <wolfssl/wolfcrypt/asn_public.h>
4646

47+
#if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
48+
/* force off unneeded vector register save/restore. */
49+
#undef SAVE_VECTOR_REGISTERS
50+
#define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
51+
#undef RESTORE_VECTOR_REGISTERS
52+
#define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
53+
#endif
54+
4755
#ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
4856
/* FIPS build has replaced ecc.h. */
4957
#define wc_ecc_key_get_priv(key) (&((key)->k))

wolfcrypt/src/sp_int.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ This library provides single precision (SP) integer math functions.
115115

116116
#include <wolfssl/wolfcrypt/sp_int.h>
117117

118+
#if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_SP_ASM)
119+
/* force off unneeded vector register save/restore. */
120+
#undef SAVE_VECTOR_REGISTERS
121+
#define SAVE_VECTOR_REGISTERS(...) WC_DO_NOTHING
122+
#undef RESTORE_VECTOR_REGISTERS
123+
#define RESTORE_VECTOR_REGISTERS() WC_DO_NOTHING
124+
#endif
125+
118126
/* DECL_SP_INT: Declare one variable of type 'sp_int'. */
119127
#if (defined(WOLFSSL_SMALL_STACK) || defined(SP_ALLOC)) && \
120128
!defined(WOLFSSL_SP_NO_MALLOC)

wolfssl/wolfcrypt/memory.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,27 +283,29 @@ WOLFSSL_LOCAL int wc_debug_CipherLifecycleFree(void **CipherLifecycleTag,
283283
#define DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE abort();
284284
#elif defined(DEBUG_VECTOR_REGISTERS_EXIT_ON_FAIL)
285285
#define DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE exit(1);
286-
#else
286+
#elif !defined(DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE)
287287
#define DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE
288288
#endif
289289

290290
#define SAVE_VECTOR_REGISTERS(fail_clause) { \
291291
int _svr_ret = wc_debug_vector_registers_retval; \
292292
if (_svr_ret != 0) { fail_clause } \
293-
++wc_svr_count; \
294-
if (wc_svr_count > 5) { \
295-
fprintf(stderr, \
296-
("%s @ L%d : incr : " \
297-
"wc_svr_count %d (last op %s L%d)\n"), \
298-
__FILE__, \
299-
__LINE__, \
300-
wc_svr_count, \
301-
wc_svr_last_file, \
302-
wc_svr_last_line); \
303-
DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE \
293+
else { \
294+
++wc_svr_count; \
295+
if (wc_svr_count > 5) { \
296+
fprintf(stderr, \
297+
("%s @ L%d : incr : " \
298+
"wc_svr_count %d (last op %s L%d)\n"), \
299+
__FILE__, \
300+
__LINE__, \
301+
wc_svr_count, \
302+
wc_svr_last_file, \
303+
wc_svr_last_line); \
304+
DEBUG_VECTOR_REGISTERS_EXTRA_FAIL_CLAUSE \
305+
} \
306+
wc_svr_last_file = __FILE__; \
307+
wc_svr_last_line = __LINE__; \
304308
} \
305-
wc_svr_last_file = __FILE__; \
306-
wc_svr_last_line = __LINE__; \
307309
}
308310

309311
WOLFSSL_API extern THREAD_LS_T int wc_debug_vector_registers_retval;

0 commit comments

Comments
 (0)