Improve NXP MMCAU/LTC AES CBC handling for unaligned sizes. Cleanup formatting in a few places.

This commit is contained in:
David Garske
2017-05-18 15:04:01 -07:00
parent 30db8e95a7
commit cbb2c73828

View File

@@ -2516,6 +2516,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
uint32_t keySize;
status_t status;
byte *iv, *enc_key;
word32 blocks = (sz / AES_BLOCK_SIZE);
iv = (byte*)aes->reg;
enc_key = (byte*)aes->key;
@@ -2525,7 +2526,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
return status;
}
status = LTC_AES_EncryptCbc(LTC_BASE, in, out, sz,
status = LTC_AES_EncryptCbc(LTC_BASE, in, out, blocks * AES_BLOCK_SIZE,
iv, enc_key, keySize);
return (status == kStatus_Success) ? 0 : -1;
}
@@ -2536,6 +2537,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
uint32_t keySize;
status_t status;
byte* iv, *dec_key;
word32 blocks = (sz / AES_BLOCK_SIZE);
iv = (byte*)aes->reg;
dec_key = (byte*)aes->key;
@@ -2545,7 +2547,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
return status;
}
status = LTC_AES_DecryptCbc(LTC_BASE, in, out, sz,
status = LTC_AES_DecryptCbc(LTC_BASE, in, out, blocks * AES_BLOCK_SIZE,
iv, dec_key, keySize, kLTC_EncryptKey);
return (status == kStatus_Success) ? 0 : -1;
}
@@ -2556,15 +2558,13 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
{
int i;
int offset = 0;
int len = sz;
word32 blocks = (sz / AES_BLOCK_SIZE);
byte *iv;
byte temp_block[AES_BLOCK_SIZE];
iv = (byte*)aes->reg;
while (len > 0)
{
while (blocks--) {
XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
/* XOR block with IV for CBC */
@@ -2573,7 +2573,6 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
wc_AesEncrypt(aes, temp_block, out + offset);
len -= AES_BLOCK_SIZE;
offset += AES_BLOCK_SIZE;
/* store IV for next block */
@@ -2587,16 +2586,13 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
{
int i;
int offset = 0;
int len = sz;
word32 blocks = (sz / AES_BLOCK_SIZE);
byte* iv;
byte temp_block[AES_BLOCK_SIZE];
iv = (byte*)aes->reg;
while (len > 0)
{
while (blocks--) {
XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
wc_AesDecrypt(aes, in + offset, out + offset);
@@ -2608,7 +2604,6 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
/* store IV for next block */
XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
len -= AES_BLOCK_SIZE;
offset += AES_BLOCK_SIZE;
}
@@ -2623,7 +2618,6 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
{
securityAssociation *sa_p;
bufferDescriptor *bd_p;
volatile securityAssociation sa __attribute__((aligned (8)));
volatile bufferDescriptor bd __attribute__((aligned (8)));
volatile int k;
@@ -2730,7 +2724,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
#else
int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{
word32 blocks = sz / AES_BLOCK_SIZE;
word32 blocks = (sz / AES_BLOCK_SIZE);
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
/* if async and byte count above threshold */