Merge pull request #5336 from SparkiDev/aes_ctr_opt

AES-CTR: improve performance when multiple blocks
This commit is contained in:
David Garske
2022-07-07 21:51:08 -07:00
committed by GitHub

View File

@@ -4470,6 +4470,29 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
#ifdef WOLFSSL_CHECK_MEM_ZERO
wc_MemZero_Add("wc_AesCtrEncrypt scratch", scratch, AES_BLOCK_SIZE);
#endif
#if defined(HAVE_AES_ECB) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
!defined(XTRANSFORM_AESCTRBLOCK)
if (in != out && sz >= AES_BLOCK_SIZE) {
int blocks = sz / AES_BLOCK_SIZE;
byte* counter = (byte*)aes->reg;
byte* c = out;
while (blocks--) {
XMEMCPY(c, counter, AES_BLOCK_SIZE);
c += AES_BLOCK_SIZE;
IncrementAesCounter(counter);
}
/* reset number of blocks and then do encryption */
blocks = sz / AES_BLOCK_SIZE;
wc_AesEcbEncrypt(aes, out, out, AES_BLOCK_SIZE * blocks);
xorbuf(out, in, AES_BLOCK_SIZE * blocks);
in += AES_BLOCK_SIZE * blocks;
out += AES_BLOCK_SIZE * blocks;
sz -= blocks * AES_BLOCK_SIZE;
}
else
#endif
{
/* do as many block size ops as possible */
while (sz >= AES_BLOCK_SIZE) {
#ifdef XTRANSFORM_AESCTRBLOCK
@@ -4494,6 +4517,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
aes->left = 0;
}
ForceZero(scratch, AES_BLOCK_SIZE);
}
/* handle non block size remaining and store unused byte count in left */
if (sz) {