@@ -12631,90 +12631,21 @@ static WARN_UNUSED_RESULT int _AesXtsHelper(
1263112631 * returns 0 on success
1263212632 */
1263312633/* Software AES - XTS Encrypt */
12634+
12635+ static int AesXtsEncryptUpdate_sw (XtsAes * xaes , byte * out , const byte * in ,
12636+ word32 sz ,
12637+ byte * i );
1263412638static int AesXtsEncrypt_sw (XtsAes * xaes , byte * out , const byte * in , word32 sz ,
1263512639 const byte * i )
1263612640{
1263712641 int ret ;
12638- word32 blocks = (sz / AES_BLOCK_SIZE );
12639- Aes * aes = & xaes - > aes ;
1264012642 byte tweak_block [AES_BLOCK_SIZE ];
1264112643
1264212644 ret = wc_AesEncryptDirect (& xaes - > tweak , tweak_block , i );
1264312645 if (ret != 0 )
1264412646 return ret ;
1264512647
12646- #ifdef HAVE_AES_ECB
12647- /* encrypt all of buffer at once when possible */
12648- if (in != out ) { /* can not handle inline */
12649- XMEMCPY (out , tweak_block , AES_BLOCK_SIZE );
12650- if ((ret = _AesXtsHelper (& xaes - > aes , out , in , sz , AES_ENCRYPTION )) != 0 )
12651- return ret ;
12652- }
12653- #endif
12654-
12655- while (blocks > 0 ) {
12656- word32 j ;
12657- byte carry = 0 ;
12658-
12659- #ifdef HAVE_AES_ECB
12660- if (in == out )
12661- #endif
12662- { /* check for if inline */
12663- byte buf [AES_BLOCK_SIZE ];
12664-
12665- XMEMCPY (buf , in , AES_BLOCK_SIZE );
12666- xorbuf (buf , tweak_block , AES_BLOCK_SIZE );
12667- ret = wc_AesEncryptDirect (aes , out , buf );
12668- if (ret != 0 )
12669- return ret ;
12670- }
12671- xorbuf (out , tweak_block , AES_BLOCK_SIZE );
12672-
12673- /* multiply by shift left and propagate carry */
12674- for (j = 0 ; j < AES_BLOCK_SIZE ; j + + ) {
12675- byte tmpC ;
12676-
12677- tmpC = (tweak_block [j ] >> 7 ) & 0x01 ;
12678- tweak_block [j ] = (byte )((tweak_block [j ] << 1 ) + carry );
12679- carry = tmpC ;
12680- }
12681- if (carry ) {
12682- tweak_block [0 ] ^= GF_XTS ;
12683- }
12684-
12685- in + = AES_BLOCK_SIZE ;
12686- out + = AES_BLOCK_SIZE ;
12687- sz - = AES_BLOCK_SIZE ;
12688- blocks - - ;
12689- }
12690-
12691- /* stealing operation of XTS to handle left overs */
12692- if (sz > 0 ) {
12693- byte buf [AES_BLOCK_SIZE ];
12694-
12695- XMEMCPY (buf , out - AES_BLOCK_SIZE , AES_BLOCK_SIZE );
12696- if (sz >= AES_BLOCK_SIZE ) { /* extra sanity check before copy */
12697- return BUFFER_E ;
12698- }
12699- if (in != out ) {
12700- XMEMCPY (out , buf , sz );
12701- XMEMCPY (buf , in , sz );
12702- }
12703- else {
12704- byte buf2 [AES_BLOCK_SIZE ];
12705-
12706- XMEMCPY (buf2 , buf , sz );
12707- XMEMCPY (buf , in , sz );
12708- XMEMCPY (out , buf2 , sz );
12709- }
12710-
12711- xorbuf (buf , tweak_block , AES_BLOCK_SIZE );
12712- ret = wc_AesEncryptDirect (aes , out - AES_BLOCK_SIZE , buf );
12713- if (ret == 0 )
12714- xorbuf (out - AES_BLOCK_SIZE , tweak_block , AES_BLOCK_SIZE );
12715- }
12716-
12717- return ret ;
12648+ return AesXtsEncryptUpdate_sw (xaes , out , in , sz , tweak_block );
1271812649}
1271912650
1272012651#ifdef WOLFSSL_AESXTS_STREAM
@@ -12726,10 +12657,12 @@ static int AesXtsEncrypt_sw(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1272612657 *
1272712658 * returns 0 on success
1272812659 */
12729- static int AesXtsInit_sw (XtsAes * xaes , byte * i ) {
12660+ static int AesXtsInitTweak_sw (XtsAes * xaes , byte * i ) {
1273012661 return wc_AesEncryptDirect (& xaes - > tweak , i , i );
1273112662}
1273212663
12664+ #endif /* WOLFSSL_AESXTS_STREAM */
12665+
1273312666/* Block-streaming AES-XTS.
1273412667 *
1273512668 * Supply block-aligned input data with successive calls. Final call need not
@@ -12825,8 +12758,6 @@ static int AesXtsEncryptUpdate_sw(XtsAes* xaes, byte* out, const byte* in,
1282512758 return ret ;
1282612759}
1282712760
12828- #endif /* WOLFSSL_AESXTS_STREAM */
12829-
1283012761/* AES with XTS mode. (XTS) XEX encryption with Tweak and cipher text Stealing.
1283112762 *
1283212763 * xaes AES keys to use for block encrypt/decrypt
@@ -12956,7 +12887,7 @@ int wc_AesXtsEncryptInit(XtsAes* xaes, byte* i, word32 iSz)
1295612887 else
1295712888#endif /* WOLFSSL_AESNI */
1295812889 {
12959- ret = AesXtsInit_sw (xaes , i );
12890+ ret = AesXtsInitTweak_sw (xaes , i );
1296012891 }
1296112892 }
1296212893
@@ -13032,6 +12963,7 @@ int wc_AesXtsEncryptUpdate(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1303212963
1303312964#endif /* WOLFSSL_AESXTS_STREAM */
1303412965
12966+
1303512967/* Same process as encryption but use aes_decrypt key.
1303612968 *
1303712969 * xaes AES keys to use for block encrypt/decrypt
@@ -13043,125 +12975,23 @@ int wc_AesXtsEncryptUpdate(XtsAes* xaes, byte* out, const byte* in, word32 sz,
1304312975 * returns 0 on success
1304412976 */
1304512977/* Software AES - XTS Decrypt */
12978+
12979+ static int AesXtsDecryptUpdate_sw (XtsAes * xaes , byte * out , const byte * in ,
12980+ word32 sz , byte * i );
12981+
1304612982static int AesXtsDecrypt_sw (XtsAes * xaes , byte * out , const byte * in , word32 sz ,
1304712983 const byte * i )
1304812984{
13049- int ret = 0 ;
13050- word32 blocks = (sz / AES_BLOCK_SIZE );
13051- #ifdef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
13052- Aes * aes = & xaes -> aes_decrypt ;
13053- #else
13054- Aes * aes = & xaes -> aes ;
13055- #endif
13056- word32 j ;
13057- byte carry = 0 ;
12985+ int ret ;
1305812986 byte tweak_block [AES_BLOCK_SIZE ];
13059- byte stl = (sz % AES_BLOCK_SIZE );
1306012987
1306112988 ret = wc_AesEncryptDirect (& xaes -> tweak , tweak_block , i );
1306212989 if (ret != 0 )
1306312990 return ret ;
1306412991
13065- /* if Stealing then break out of loop one block early to handle special
13066- * case */
13067- if (stl > 0 ) {
13068- blocks -- ;
13069- }
13070-
13071- #ifdef HAVE_AES_ECB
13072- /* decrypt all of buffer at once when possible */
13073- if (in != out ) { /* can not handle inline */
13074- XMEMCPY (out , tweak_block , AES_BLOCK_SIZE );
13075- if ((ret = _AesXtsHelper (aes , out , in , sz , AES_DECRYPTION )) != 0 )
13076- return ret ;
13077- }
13078- #endif
13079-
13080- while (blocks > 0 ) {
13081- #ifdef HAVE_AES_ECB
13082- if (in == out )
13083- #endif
13084- { /* check for if inline */
13085- byte buf [AES_BLOCK_SIZE ];
13086-
13087- XMEMCPY (buf , in , AES_BLOCK_SIZE );
13088- xorbuf (buf , tweak_block , AES_BLOCK_SIZE );
13089- ret = wc_AesDecryptDirect (aes , out , buf );
13090- if (ret != 0 )
13091- return ret ;
13092- }
13093- xorbuf (out , tweak_block , AES_BLOCK_SIZE );
13094-
13095- /* multiply by shift left and propagate carry */
13096- for (j = 0 ; j < AES_BLOCK_SIZE ; j ++ ) {
13097- byte tmpC ;
13098-
13099- tmpC = (tweak_block [j ] >> 7 ) & 0x01 ;
13100- tweak_block [j ] = (byte )((tweak_block [j ] << 1 ) + carry );
13101- carry = tmpC ;
13102- }
13103- if (carry ) {
13104- tweak_block [0 ] ^= GF_XTS ;
13105- }
13106- carry = 0 ;
13107-
13108- in += AES_BLOCK_SIZE ;
13109- out += AES_BLOCK_SIZE ;
13110- sz -= AES_BLOCK_SIZE ;
13111- blocks -- ;
13112- }
13113-
13114- /* stealing operation of XTS to handle left overs */
13115- if (sz >= AES_BLOCK_SIZE ) {
13116- byte buf [AES_BLOCK_SIZE ];
13117- byte tmp2 [AES_BLOCK_SIZE ];
13118-
13119- /* multiply by shift left and propagate carry */
13120- for (j = 0 ; j < AES_BLOCK_SIZE ; j ++ ) {
13121- byte tmpC ;
13122-
13123- tmpC = (tweak_block [j ] >> 7 ) & 0x01 ;
13124- tmp2 [j ] = (byte )((tweak_block [j ] << 1 ) + carry );
13125- carry = tmpC ;
13126- }
13127- if (carry ) {
13128- tmp2 [0 ] ^= GF_XTS ;
13129- }
13130-
13131- XMEMCPY (buf , in , AES_BLOCK_SIZE );
13132- xorbuf (buf , tmp2 , AES_BLOCK_SIZE );
13133- ret = wc_AesDecryptDirect (aes , out , buf );
13134- if (ret != 0 )
13135- return ret ;
13136- xorbuf (out , tmp2 , AES_BLOCK_SIZE );
13137-
13138- /* tmp2 holds partial | last */
13139- XMEMCPY (tmp2 , out , AES_BLOCK_SIZE );
13140- in += AES_BLOCK_SIZE ;
13141- out += AES_BLOCK_SIZE ;
13142- sz -= AES_BLOCK_SIZE ;
13143-
13144- /* Make buffer with end of cipher text | last */
13145- XMEMCPY (buf , tmp2 , AES_BLOCK_SIZE );
13146- if (sz >= AES_BLOCK_SIZE ) { /* extra sanity check before copy */
13147- return BUFFER_E ;
13148- }
13149- XMEMCPY (buf , in , sz );
13150- XMEMCPY (out , tmp2 , sz );
13151-
13152- xorbuf (buf , tweak_block , AES_BLOCK_SIZE );
13153- ret = wc_AesDecryptDirect (aes , tmp2 , buf );
13154- if (ret != 0 )
13155- return ret ;
13156- xorbuf (tmp2 , tweak_block , AES_BLOCK_SIZE );
13157- XMEMCPY (out - AES_BLOCK_SIZE , tmp2 , AES_BLOCK_SIZE );
13158- }
13159-
13160- return ret ;
12992+ return AesXtsDecryptUpdate_sw (xaes , out , in , sz , tweak_block );
1316112993}
1316212994
13163- #ifdef WOLFSSL_AESXTS_STREAM
13164-
1316512995/* Block-streaming AES-XTS.
1316612996 *
1316712997 * Same process as encryption but use decrypt key.
@@ -13290,8 +13120,6 @@ static int AesXtsDecryptUpdate_sw(XtsAes* xaes, byte* out, const byte* in,
1329013120 return ret ;
1329113121}
1329213122
13293- #endif /* WOLFSSL_AESXTS_STREAM */
13294-
1329513123/* Same process as encryption but Aes key is AES_DECRYPTION type.
1329613124 *
1329713125 * xaes AES keys to use for block encrypt/decrypt
@@ -13433,7 +13261,7 @@ int wc_AesXtsDecryptInit(XtsAes* xaes, byte* i, word32 iSz)
1343313261 else
1343413262#endif /* WOLFSSL_AESNI */
1343513263 {
13436- ret = AesXtsInit_sw (xaes , i );
13264+ ret = AesXtsInitTweak_sw (xaes , i );
1343713265 }
1343813266
1343913267 }
0 commit comments