From a59488b2992132e49ad9706a2e1d25dc322d2899 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 3 Apr 2019 15:02:00 -0700 Subject: [PATCH] AES-CTR In Place Added a temp AES block in case in and out are the same pointer. --- wolfcrypt/src/aes.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index bf80d4850..7a73c7882 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -3267,6 +3267,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv) int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) { byte* tmp; + byte scratch[AES_BLOCK_SIZE]; if (aes == NULL || out == NULL || in == NULL) { return BAD_FUNC_ARG; @@ -3285,8 +3286,9 @@ int wc_AesSetIV(Aes* aes, const byte* iv) #ifdef XTRANSFORM_AESCTRBLOCK XTRANSFORM_AESCTRBLOCK(aes, out, in); #else - wc_AesEncrypt(aes, (byte*)aes->reg, out); - xorbuf(out, in, AES_BLOCK_SIZE); + wc_AesEncrypt(aes, (byte*)aes->reg, scratch); + xorbuf(scratch, in, AES_BLOCK_SIZE); + XMEMCPY(out, scratch, AES_BLOCK_SIZE); #endif IncrementAesCounter((byte*)aes->reg); @@ -3295,6 +3297,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv) sz -= AES_BLOCK_SIZE; aes->left = 0; } + ForceZero(scratch, AES_BLOCK_SIZE); /* handle non block size remaining and store unused byte count in left */ if (sz) {