Skip to content

Commit 13c73a9

Browse files
committed
linuxkm/lkcapi_glue.c: add LINUXKM_LKCAPI_NEED_AES_COMMON_FUNCS and
LINUXKM_LKCAPI_NEED_AES_SKCIPHER_COMMON_FUNCS helper macros (peer review suggestion). wolfcrypt/src/aes.c: add lengthy comment in software wc_AesSetKeyLocal() explaining the dynamics of aes->use_aesni (peer review suggestion), and in the !haveAESNI && WC_C_DYNAMIC_FALLBACK case, return with immediate success rather than following through to the redundant AesSetKey_C().
1 parent 8705d28 commit 13c73a9

3 files changed

Lines changed: 66 additions & 26 deletions

File tree

linuxkm/lkcapi_glue.c

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,29 @@ static int disable_setkey_warnings = 0;
210210
static int linuxkm_test_aesecb(void);
211211
#endif
212212

213+
#if defined(LINUXKM_LKCAPI_REGISTER_AESCBC) || \
214+
defined(LINUXKM_LKCAPI_REGISTER_AESCFB) || \
215+
defined(LINUXKM_LKCAPI_REGISTER_AESCTR) || \
216+
defined(LINUXKM_LKCAPI_REGISTER_AESOFB) || \
217+
defined(LINUXKM_LKCAPI_REGISTER_AESECB) || \
218+
defined(LINUXKM_LKCAPI_REGISTER_AESGCM) || \
219+
defined(LINUXKM_LKCAPI_REGISTER_AESGCM_RFC4106)
220+
#define LINUXKM_LKCAPI_NEED_AES_COMMON_FUNCS
221+
#endif
222+
223+
#if defined(LINUXKM_LKCAPI_REGISTER_AESCBC) || \
224+
defined(LINUXKM_LKCAPI_REGISTER_AESCFB) || \
225+
defined(LINUXKM_LKCAPI_REGISTER_AESCTR) || \
226+
defined(LINUXKM_LKCAPI_REGISTER_AESOFB) || \
227+
defined(LINUXKM_LKCAPI_REGISTER_AESECB)
228+
#define LINUXKM_LKCAPI_NEED_AES_SKCIPHER_COMMON_FUNCS
229+
#endif
230+
231+
#if defined(LINUXKM_LKCAPI_REGISTER_AESGCM) || \
232+
defined(LINUXKM_LKCAPI_REGISTER_AESGCM_RFC4106)
233+
#define LINUXKM_LKCAPI_REGISTER_AEADS
234+
#endif
235+
213236
/* km_AesX(): wrappers to wolfcrypt wc_AesX functions and
214237
* structures. */
215238

@@ -312,13 +335,7 @@ struct km_AesCtx {
312335
#endif
313336
};
314337

315-
#if defined(LINUXKM_LKCAPI_REGISTER_AESCBC) || \
316-
defined(LINUXKM_LKCAPI_REGISTER_AESCFB) || \
317-
defined(LINUXKM_LKCAPI_REGISTER_AESGCM) || \
318-
defined(LINUXKM_LKCAPI_REGISTER_AESGCM_RFC4106) || \
319-
defined(LINUXKM_LKCAPI_REGISTER_AESCTR) || \
320-
defined(LINUXKM_LKCAPI_REGISTER_AESOFB) || \
321-
defined(LINUXKM_LKCAPI_REGISTER_AESECB)
338+
#ifdef LINUXKM_LKCAPI_NEED_AES_COMMON_FUNCS
322339

323340
static void km_AesExitCommon(struct km_AesCtx * ctx);
324341

@@ -518,11 +535,7 @@ static void km_AesExitCommon(struct km_AesCtx * ctx)
518535
#endif
519536
}
520537

521-
#if defined(LINUXKM_LKCAPI_REGISTER_AESCBC) || \
522-
defined(LINUXKM_LKCAPI_REGISTER_AESCFB) || \
523-
defined(LINUXKM_LKCAPI_REGISTER_AESCTR) || \
524-
defined(LINUXKM_LKCAPI_REGISTER_AESOFB) || \
525-
defined(LINUXKM_LKCAPI_REGISTER_AESECB)
538+
#ifdef LINUXKM_LKCAPI_NEED_AES_SKCIPHER_COMMON_FUNCS
526539

527540
static int km_AesSetKeyCommon(struct km_AesCtx * ctx, const u8 *in_key,
528541
unsigned int key_len, const char * name)
@@ -595,19 +608,9 @@ static void km_AesExit(struct crypto_skcipher *tfm)
595608
km_AesExitCommon(ctx);
596609
}
597610

598-
#endif /* LINUXKM_LKCAPI_REGISTER_AESCBC ||
599-
* LINUXKM_LKCAPI_REGISTER_AESCFB ||
600-
* LINUXKM_LKCAPI_REGISTER_AESCTR ||
601-
* LINUXKM_LKCAPI_REGISTER_AESOFB ||
602-
* LINUXKM_LKCAPI_REGISTER_AESECB
603-
*/
604-
605-
#endif /* LINUXKM_LKCAPI_REGISTER_AESCBC ||
606-
* LINUXKM_LKCAPI_REGISTER_AESCFB || LINUXKM_LKCAPI_REGISTER_AESGCM ||
607-
* LINUXKM_LKCAPI_REGISTER_AESGCM_RFC4106 ||
608-
* LINUXKM_LKCAPI_REGISTER_AESCTR || LINUXKM_LKCAPI_REGISTER_AESOFB ||
609-
* LINUXKM_LKCAPI_REGISTER_AESECB
610-
*/
611+
#endif /* LINUXKM_LKCAPI_NEED_AES_SKCIPHER_COMMON_FUNCS */
612+
613+
#endif /* LINUXKM_LKCAPI_NEED_AES_COMMON_FUNCS */
611614

612615
#ifdef LINUXKM_LKCAPI_REGISTER_AESCBC
613616

wolfcrypt/src/aes.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4575,6 +4575,36 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
45754575
#endif /* WC_C_DYNAMIC_FALLBACK */
45764576

45774577
#ifdef WOLFSSL_AESNI
4578+
4579+
/* The dynamics for determining whether AES-NI will be used are tricky.
4580+
*
4581+
* First, we check for CPU support and cache the result -- if AES-NI is
4582+
* missing, we always shortcut to the AesSetKey_C() path.
4583+
*
4584+
* Second, if the CPU supports AES-NI, we confirm on a per-call basis
4585+
* that it's safe to use in the caller context, using
4586+
* SAVE_VECTOR_REGISTERS2(). This is an always-true no-op in user-space
4587+
* builds, but has substantive logic behind it in kernel module builds.
4588+
*
4589+
* The outcome when SAVE_VECTOR_REGISTERS2() fails depends on
4590+
* WC_C_DYNAMIC_FALLBACK -- if that's defined, we return immediately with
4591+
* success but with AES-NI disabled (the earlier AesSetKey_C() allows
4592+
* future encrypt/decrypt calls to succeed), otherwise we fail.
4593+
*
4594+
* Upon successful return, aes->use_aesni will have a zero value if
4595+
* AES-NI is disabled, and a nonzero value if it's enabled.
4596+
*
4597+
* An additional, optional semantic is available via
4598+
* WC_FLAG_DONT_USE_AESNI, and is used in some kernel module builds to
4599+
* let the caller inhibit AES-NI. When this macro is defined,
4600+
* wc_AesInit() before wc_AesSetKey() is imperative, to avoid a read of
4601+
* uninitialized data in aes->use_aesni. That's why support for
4602+
* WC_FLAG_DONT_USE_AESNI must remain optional -- wc_AesInit() was only
4603+
* added in release 3.11.0, so legacy applications inevitably call
4604+
* wc_AesSetKey() on uninitialized Aes contexts. This must continue to
4605+
* function correctly with default build settings.
4606+
*/
4607+
45784608
if (checkedAESNI == 0) {
45794609
haveAESNI = Check_CPU_support_AES();
45804610
checkedAESNI = 1;
@@ -4627,6 +4657,12 @@ static void AesSetKey_C(Aes* aes, const byte* key, word32 keySz, int dir)
46274657
}
46284658
else {
46294659
aes->use_aesni = 0;
4660+
#ifdef WC_C_DYNAMIC_FALLBACK
4661+
/* If WC_C_DYNAMIC_FALLBACK, we already called AesSetKey_C()
4662+
* above.
4663+
*/
4664+
return 0;
4665+
#endif
46304666
}
46314667
#endif /* WOLFSSL_AESNI */
46324668

wolfssl/wolfcrypt/aes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ struct Aes {
306306
#if defined(WOLFSSL_LINUXKM) || defined(WC_WANT_FLAG_DONT_USE_AESNI)
307307
/* Note, we can't support WC_FLAG_DONT_USE_AESNI by default because we
308308
* need to support legacy applications that call wc_AesSetKey() on
309-
* uninited struct Aes.
309+
* uninited struct Aes. For details see the software implementation of
310+
* wc_AesSetKeyLocal() (aes.c).
310311
*/
311312
#define WC_FLAG_DONT_USE_AESNI 2
312313
#endif

0 commit comments

Comments
 (0)