Skip to content

Commit 1fc6718

Browse files
committed
linuxkm: address peer review:
* support AES_ENCRYPTION_AND_DECRYPTION only if WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS is defined, and define it in linuxkm_wc_port.h if LINUXKM_LKCAPI_REGISTER. * fix a typo in km_AesInitCommon(). * remove #if 0 code in lkcapi_glue.c.
1 parent 957fc74 commit 1fc6718

5 files changed

Lines changed: 165 additions & 64 deletions

File tree

linuxkm/linuxkm_wc_port.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@
269269
#include <crypto/scatterwalk.h>
270270
#include <crypto/internal/aead.h>
271271
#include <crypto/internal/skcipher.h>
272+
273+
#ifndef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
274+
#define WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
275+
#endif
272276
#endif
273277

274278
#if defined(WOLFSSL_AESNI) || defined(USE_INTEL_SPEEDUP) || defined(WOLFSSL_SP_X86_64_ASM)

linuxkm/lkcapi_glue.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static int km_AesInitCommon(struct km_AesCtx * ctx, const char * name, int need_
134134

135135
ctx->aes_decrypt = (Aes *)malloc(sizeof(*ctx->aes_decrypt));
136136

137-
if (! ctx->aes_encrypt) {
137+
if (! ctx->aes_decrypt) {
138138
pr_err("error: km_AesInitCommon %s failed: %d\n", name, MEMORY_E);
139139
km_AesExitCommon(ctx);
140140
return MEMORY_E;
@@ -239,7 +239,7 @@ static int km_AesCbcEncrypt(struct skcipher_request *req)
239239

240240
err = skcipher_walk_virt(&walk, req, false);
241241

242-
while ((nbytes = walk.nbytes)) {
242+
while ((nbytes = walk.nbytes) != 0) {
243243
err = wc_AesSetIV(ctx->aes_encrypt, walk.iv);
244244

245245
if (unlikely(err)) {
@@ -274,7 +274,7 @@ static int km_AesCbcDecrypt(struct skcipher_request *req)
274274

275275
err = skcipher_walk_virt(&walk, req, false);
276276

277-
while ((nbytes = walk.nbytes)) {
277+
while ((nbytes = walk.nbytes) != 0) {
278278
err = wc_AesSetIV(ctx->aes_decrypt, walk.iv);
279279

280280
if (unlikely(err)) {
@@ -347,7 +347,7 @@ static int km_AesCfbEncrypt(struct skcipher_request *req)
347347

348348
err = skcipher_walk_virt(&walk, req, false);
349349

350-
while ((nbytes = walk.nbytes)) {
350+
while ((nbytes = walk.nbytes) != 0) {
351351
err = wc_AesSetIV(ctx->aes_encrypt, walk.iv);
352352

353353
if (unlikely(err)) {
@@ -382,7 +382,7 @@ static int km_AesCfbDecrypt(struct skcipher_request *req)
382382

383383
err = skcipher_walk_virt(&walk, req, false);
384384

385-
while ((nbytes = walk.nbytes)) {
385+
while ((nbytes = walk.nbytes) != 0) {
386386
err = wc_AesSetIV(ctx->aes_encrypt, walk.iv);
387387

388388
if (unlikely(err)) {
@@ -527,7 +527,7 @@ static int km_AesGcmEncrypt(struct aead_request *req)
527527
return err;
528528
}
529529

530-
while ((nbytes = walk.nbytes)) {
530+
while ((nbytes = walk.nbytes) != 0) {
531531
int n = nbytes;
532532

533533
if (likely(cryptLeft && nbytes)) {
@@ -615,7 +615,7 @@ static int km_AesGcmDecrypt(struct aead_request *req)
615615
return err;
616616
}
617617

618-
while ((nbytes = walk.nbytes)) {
618+
while ((nbytes = walk.nbytes) != 0) {
619619
int n = nbytes;
620620

621621
if (likely(cryptLeft && nbytes)) {
@@ -712,9 +712,6 @@ static void km_AesXtsExit(struct crypto_skcipher *tfm)
712712
wc_AesXtsFree(ctx->aesXts);
713713
free(ctx->aesXts);
714714
ctx->aesXts = NULL;
715-
#if 0
716-
km_ForceZeroXts(ctx);
717-
#endif
718715
}
719716

720717
static int km_AesXtsSetKey(struct crypto_skcipher *tfm, const u8 *in_key,
@@ -730,11 +727,6 @@ static int km_AesXtsSetKey(struct crypto_skcipher *tfm, const u8 *in_key,
730727
return err;
731728
}
732729

733-
#if 0
734-
XMEMCPY(ctx->key, in_key, key_len);
735-
ctx->keylen = key_len;
736-
#endif
737-
738730
return 0;
739731
}
740732

@@ -759,7 +751,7 @@ static int km_AesXtsEncrypt(struct skcipher_request *req)
759751
return err;
760752
}
761753

762-
while ((nbytes = walk.nbytes)) {
754+
while ((nbytes = walk.nbytes) != 0) {
763755
err = wc_AesXtsEncrypt(ctx->aesXts, walk.dst.virt.addr,
764756
walk.src.virt.addr, nbytes,
765757
walk.iv, walk.ivsize);
@@ -798,7 +790,7 @@ static int km_AesXtsDecrypt(struct skcipher_request *req)
798790
return err;
799791
}
800792

801-
while ((nbytes = walk.nbytes)) {
793+
while ((nbytes = walk.nbytes) != 0) {
802794
err = wc_AesXtsDecrypt(ctx->aesXts, walk.dst.virt.addr,
803795
walk.src.virt.addr, nbytes,
804796
walk.iv, walk.ivsize);

wolfcrypt/src/aes.c

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12271,15 +12271,17 @@ int wc_AesXtsInit(XtsAes* aes, void* heap, int devId)
1227112271
if ((ret = wc_AesInit(&aes->tweak, heap, devId)) != 0) {
1227212272
return ret;
1227312273
}
12274-
if ((ret = wc_AesInit(&aes->aes_encrypt, heap, devId)) != 0) {
12274+
if ((ret = wc_AesInit(&aes->aes, heap, devId)) != 0) {
1227512275
(void)wc_AesFree(&aes->tweak);
1227612276
return ret;
1227712277
}
12278+
#ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
1227812279
if ((ret = wc_AesInit(&aes->aes_decrypt, heap, devId)) != 0) {
1227912280
(void)wc_AesFree(&aes->tweak);
12280-
(void)wc_AesFree(&aes->aes_encrypt);
12281+
(void)wc_AesFree(&aes->aes);
1228112282
return ret;
1228212283
}
12284+
#endif
1228312285

1228412286
return 0;
1228512287
}
@@ -12304,6 +12306,15 @@ int wc_AesXtsSetKeyNoInit(XtsAes* aes, const byte* key, word32 len, int dir)
1230412306
return BAD_FUNC_ARG;
1230512307
}
1230612308

12309+
if ((dir != AES_ENCRYPTION) && (dir != AES_DECRYPTION)
12310+
#ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
12311+
&& (dir != AES_ENCRYPTION_AND_DECRYPTION)
12312+
#endif
12313+
)
12314+
{
12315+
return BAD_FUNC_ARG;
12316+
}
12317+
1230712318
keySz = len/2;
1230812319
if (keySz != AES_128_KEY_SIZE && keySz != AES_256_KEY_SIZE) {
1230912320
WOLFSSL_MSG("Unsupported key size");
@@ -12318,10 +12329,15 @@ int wc_AesXtsSetKeyNoInit(XtsAes* aes, const byte* key, word32 len, int dir)
1231812329
#endif
1231912330

1232012331
if ((dir == AES_ENCRYPTION) || (dir == AES_ENCRYPTION_AND_DECRYPTION))
12321-
ret = wc_AesSetKey(&aes->aes_encrypt, key, keySz, NULL, AES_ENCRYPTION);
12332+
ret = wc_AesSetKey(&aes->aes, key, keySz, NULL, AES_ENCRYPTION);
1232212333

12334+
#ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
1232312335
if ((ret == 0) && ((dir == AES_DECRYPTION) || (dir == AES_ENCRYPTION_AND_DECRYPTION)))
1232412336
ret = wc_AesSetKey(&aes->aes_decrypt, key, keySz, NULL, AES_DECRYPTION);
12337+
#else
12338+
if (dir == AES_DECRYPTION)
12339+
ret = wc_AesSetKey(&aes->aes, key, keySz, NULL, AES_DECRYPTION);
12340+
#endif
1232512341

1232612342
if (ret == 0)
1232712343
ret = wc_AesSetKey(&aes->tweak, key + keySz, keySz, NULL,
@@ -12334,13 +12350,19 @@ int wc_AesXtsSetKeyNoInit(XtsAes* aes, const byte* key, word32 len, int dir)
1233412350
* them to all be AESNI. If any aren't, disable AESNI on all.
1233512351
*/
1233612352
if ((((dir == AES_ENCRYPTION) || (dir == AES_ENCRYPTION_AND_DECRYPTION)) &&
12337-
(aes->aes_encrypt.use_aesni != aes->tweak.use_aesni)) ||
12353+
(aes->aes.use_aesni != aes->tweak.use_aesni))
12354+
#ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
12355+
||
1233812356
(((dir == AES_DECRYPTION) || (dir == AES_ENCRYPTION_AND_DECRYPTION)) &&
12339-
(aes->aes_decrypt.use_aesni != aes->tweak.use_aesni)))
12357+
(aes->aes_decrypt.use_aesni != aes->tweak.use_aesni))
12358+
#endif
12359+
)
1234012360
{
1234112361
#ifdef WC_AES_C_DYNAMIC_FALLBACK
12342-
aes->aes_encrypt.use_aesni = 0;
12362+
aes->aes.use_aesni = 0;
12363+
#ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
1234312364
aes->aes_decrypt.use_aesni = 0;
12365+
#endif
1234412366
aes->tweak.use_aesni = 0;
1234512367
#else
1234612368
ret = SYSLIB_FAILED_E;
@@ -12389,8 +12411,10 @@ int wc_AesXtsSetKey(XtsAes* aes, const byte* key, word32 len, int dir,
1238912411
int wc_AesXtsFree(XtsAes* aes)
1239012412
{
1239112413
if (aes != NULL) {
12392-
wc_AesFree(&aes->aes_encrypt);
12414+
wc_AesFree(&aes->aes);
12415+
#ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
1239312416
wc_AesFree(&aes->aes_decrypt);
12417+
#endif
1239412418
wc_AesFree(&aes->tweak);
1239512419
}
1239612420

@@ -12547,7 +12571,7 @@ static int AesXtsEncrypt_sw(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1254712571
{
1254812572
int ret = 0;
1254912573
word32 blocks = (sz / AES_BLOCK_SIZE);
12550-
Aes *aes = &xaes->aes_encrypt;
12574+
Aes *aes = &xaes->aes;
1255112575
Aes *tweak = &xaes->tweak;
1255212576
byte tmp[AES_BLOCK_SIZE];
1255312577

@@ -12650,11 +12674,15 @@ int wc_AesXtsEncrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1265012674
{
1265112675
int ret;
1265212676

12677+
Aes *aes;
12678+
1265312679
if (xaes == NULL || out == NULL || in == NULL) {
1265412680
return BAD_FUNC_ARG;
1265512681
}
1265612682

12657-
if (xaes->aes_encrypt.keylen == 0) {
12683+
aes = &xaes->aes;
12684+
12685+
if (aes->keylen == 0) {
1265812686
WOLFSSL_MSG("wc_AesXtsEncrypt called with unset encryption key.");
1265912687
return BAD_FUNC_ARG;
1266012688
}
@@ -12671,33 +12699,33 @@ int wc_AesXtsEncrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1267112699
{
1267212700
#ifdef WOLFSSL_AESNI
1267312701
#ifdef WC_AES_C_DYNAMIC_FALLBACK
12674-
int orig_use_aesni = xaes->aes_encrypt.use_aesni;
12702+
int orig_use_aesni = aes->use_aesni;
1267512703
#endif
1267612704

12677-
if (xaes->aes_encrypt.use_aesni && ((ret = SAVE_VECTOR_REGISTERS2()) != 0)) {
12705+
if (aes->use_aesni && ((ret = SAVE_VECTOR_REGISTERS2()) != 0)) {
1267812706
#ifdef WC_AES_C_DYNAMIC_FALLBACK
12679-
xaes->aes_encrypt.use_aesni = 0;
12707+
aes->use_aesni = 0;
1268012708
xaes->tweak.use_aesni = 0;
1268112709
#else
1268212710
return ret;
1268312711
#endif
1268412712
}
12685-
if (xaes->aes_encrypt.use_aesni) {
12713+
if (aes->use_aesni) {
1268612714
#if defined(HAVE_INTEL_AVX1)
1268712715
if (IS_INTEL_AVX1(intel_flags)) {
1268812716
AES_XTS_encrypt_avx1(in, out, sz, i,
12689-
(const byte*)xaes->aes_encrypt.key,
12717+
(const byte*)aes->key,
1269012718
(const byte*)xaes->tweak.key,
12691-
(int)xaes->aes_encrypt.rounds);
12719+
(int)aes->rounds);
1269212720
ret = 0;
1269312721
}
1269412722
else
1269512723
#endif
1269612724
{
1269712725
AES_XTS_encrypt_aesni(in, out, sz, i,
12698-
(const byte*)xaes->aes_encrypt.key,
12726+
(const byte*)aes->key,
1269912727
(const byte*)xaes->tweak.key,
12700-
(int)xaes->aes_encrypt.rounds);
12728+
(int)aes->rounds);
1270112729
ret = 0;
1270212730
}
1270312731
}
@@ -12708,11 +12736,11 @@ int wc_AesXtsEncrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1270812736
}
1270912737

1271012738
#ifdef WOLFSSL_AESNI
12711-
if (xaes->aes_encrypt.use_aesni)
12739+
if (aes->use_aesni)
1271212740
RESTORE_VECTOR_REGISTERS();
1271312741
#ifdef WC_AES_C_DYNAMIC_FALLBACK
1271412742
else if (orig_use_aesni) {
12715-
xaes->aes_encrypt.use_aesni = orig_use_aesni;
12743+
aes->use_aesni = orig_use_aesni;
1271612744
xaes->tweak.use_aesni = orig_use_aesni;
1271712745
}
1271812746
#endif
@@ -12738,7 +12766,11 @@ static int AesXtsDecrypt_sw(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1273812766
{
1273912767
int ret = 0;
1274012768
word32 blocks = (sz / AES_BLOCK_SIZE);
12769+
#ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
1274112770
Aes *aes = &xaes->aes_decrypt;
12771+
#else
12772+
Aes *aes = &xaes->aes;
12773+
#endif
1274212774
Aes *tweak = &xaes->tweak;
1274312775
word32 j;
1274412776
byte carry = 0;
@@ -12866,12 +12898,19 @@ int wc_AesXtsDecrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1286612898
const byte* i, word32 iSz)
1286712899
{
1286812900
int ret;
12901+
Aes *aes;
1286912902

1287012903
if (xaes == NULL || out == NULL || in == NULL) {
1287112904
return BAD_FUNC_ARG;
1287212905
}
1287312906

12874-
if (xaes->aes_decrypt.keylen == 0) {
12907+
#ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
12908+
aes = &xaes->aes_decrypt;
12909+
#else
12910+
aes = &xaes->aes;
12911+
#endif
12912+
12913+
if (aes->keylen == 0) {
1287512914
WOLFSSL_MSG("wc_AesXtsDecrypt called with unset decryption key.");
1287612915
return BAD_FUNC_ARG;
1287712916
}
@@ -12888,33 +12927,33 @@ int wc_AesXtsDecrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1288812927
{
1288912928
#ifdef WOLFSSL_AESNI
1289012929
#ifdef WC_AES_C_DYNAMIC_FALLBACK
12891-
int orig_use_aesni = xaes->aes_decrypt.use_aesni;
12930+
int orig_use_aesni = aes->use_aesni;
1289212931
#endif
1289312932

12894-
if (xaes->aes_decrypt.use_aesni && ((ret = SAVE_VECTOR_REGISTERS2() != 0))) {
12933+
if (aes->use_aesni && ((ret = SAVE_VECTOR_REGISTERS2() != 0))) {
1289512934
#ifdef WC_AES_C_DYNAMIC_FALLBACK
12896-
xaes->aes_decrypt.use_aesni = 0;
12935+
aes->use_aesni = 0;
1289712936
xaes->tweak.use_aesni = 0;
1289812937
#else
1289912938
return ret;
1290012939
#endif
1290112940
}
12902-
if (xaes->aes_decrypt.use_aesni) {
12941+
if (aes->use_aesni) {
1290312942
#if defined(HAVE_INTEL_AVX1)
1290412943
if (IS_INTEL_AVX1(intel_flags)) {
1290512944
AES_XTS_decrypt_avx1(in, out, sz, i,
12906-
(const byte*)xaes->aes_decrypt.key,
12945+
(const byte*)aes->key,
1290712946
(const byte*)xaes->tweak.key,
12908-
(int)xaes->aes_decrypt.rounds);
12947+
(int)aes->rounds);
1290912948
ret = 0;
1291012949
}
1291112950
else
1291212951
#endif
1291312952
{
1291412953
AES_XTS_decrypt_aesni(in, out, sz, i,
12915-
(const byte*)xaes->aes_decrypt.key,
12954+
(const byte*)aes->key,
1291612955
(const byte*)xaes->tweak.key,
12917-
(int)xaes->aes_decrypt.rounds);
12956+
(int)aes->rounds);
1291812957
ret = 0;
1291912958
}
1292012959
}
@@ -12925,11 +12964,11 @@ int wc_AesXtsDecrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1292512964
}
1292612965

1292712966
#ifdef WOLFSSL_AESNI
12928-
if (xaes->aes_decrypt.use_aesni)
12967+
if (aes->use_aesni)
1292912968
RESTORE_VECTOR_REGISTERS();
1293012969
#ifdef WC_AES_C_DYNAMIC_FALLBACK
1293112970
else if (orig_use_aesni) {
12932-
xaes->aes_decrypt.use_aesni = orig_use_aesni;
12971+
aes->use_aesni = orig_use_aesni;
1293312972
xaes->tweak.use_aesni = orig_use_aesni;
1293412973
}
1293512974
#endif

0 commit comments

Comments
 (0)