AES-CTR In Place

Added a temp AES block in case in and out are the same pointer.
This commit is contained in:
John Safranek
2019-04-03 15:02:00 -07:00
parent e13c903ad2
commit a59488b299

View File

@@ -3267,6 +3267,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
{ {
byte* tmp; byte* tmp;
byte scratch[AES_BLOCK_SIZE];
if (aes == NULL || out == NULL || in == NULL) { if (aes == NULL || out == NULL || in == NULL) {
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
@@ -3285,8 +3286,9 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
#ifdef XTRANSFORM_AESCTRBLOCK #ifdef XTRANSFORM_AESCTRBLOCK
XTRANSFORM_AESCTRBLOCK(aes, out, in); XTRANSFORM_AESCTRBLOCK(aes, out, in);
#else #else
wc_AesEncrypt(aes, (byte*)aes->reg, out); wc_AesEncrypt(aes, (byte*)aes->reg, scratch);
xorbuf(out, in, AES_BLOCK_SIZE); xorbuf(scratch, in, AES_BLOCK_SIZE);
XMEMCPY(out, scratch, AES_BLOCK_SIZE);
#endif #endif
IncrementAesCounter((byte*)aes->reg); IncrementAesCounter((byte*)aes->reg);
@@ -3295,6 +3297,7 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
sz -= AES_BLOCK_SIZE; sz -= AES_BLOCK_SIZE;
aes->left = 0; aes->left = 0;
} }
ForceZero(scratch, AES_BLOCK_SIZE);
/* handle non block size remaining and store unused byte count in left */ /* handle non block size remaining and store unused byte count in left */
if (sz) { if (sz) {