@@ -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,
1238912411int 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