Fix AES-CBC for in-place operation

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman 2023-05-22 23:13:45 +01:00
parent b19b63a639
commit 262d8ced79

View File

@ -1071,26 +1071,21 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
const unsigned char *ivp = iv; const unsigned char *ivp = iv;
if (mode == MBEDTLS_AES_DECRYPT) { if (mode == MBEDTLS_AES_DECRYPT) {
if (length >= 16) { unsigned char temp2[16];
unsigned char temp2[16]; while (length > 0) {
memcpy(temp, input + length - 16, 16); memcpy(temp, input, 16);
ret = mbedtls_aes_crypt_ecb(ctx, mode, input, temp2);
while (length > 0) { if (ret != 0) {
ret = mbedtls_aes_crypt_ecb(ctx, mode, input, temp2); goto exit;
if (ret != 0) {
goto exit;
}
mbedtls_xor(output, temp2, ivp, 16);
ivp = input;
input += 16;
output += 16;
length -= 16;
} }
mbedtls_xor(output, temp2, iv, 16);
memcpy(iv, temp, 16); memcpy(iv, temp, 16);
input += 16;
output += 16;
length -= 16;
} }
} else { } else {
while (length > 0) { while (length > 0) {