Merge "Source and destination buffers may point to same buffer"

This commit is contained in:
Fred Gylys-Colwell
2017-03-02 04:44:11 +00:00
committed by Android (Google) Code Review
4 changed files with 122 additions and 58 deletions

View File

@@ -1134,6 +1134,7 @@ OEMCryptoResult SessionContext::DecryptCBC(
AES_KEY aes_key;
AES_set_decrypt_key(&key[0], AES_BLOCK_SIZE * 8, &aes_key);
uint8_t iv[AES_BLOCK_SIZE];
uint8_t next_iv[AES_BLOCK_SIZE];
memcpy(iv, &initial_iv[0], AES_BLOCK_SIZE);
size_t l = 0;
@@ -1151,11 +1152,14 @@ OEMCryptoResult SessionContext::DecryptCBC(
memcpy(&clear_data[l], &cipher_data[l], size);
} else {
uint8_t aes_output[AES_BLOCK_SIZE];
// Save the iv for the next block, in case cipher_data is in the same
// buffer as clear_data.
memcpy(next_iv, &cipher_data[l], AES_BLOCK_SIZE);
AES_decrypt(&cipher_data[l], aes_output, &aes_key);
for (size_t n = 0; n < AES_BLOCK_SIZE; n++) {
clear_data[l + n] = aes_output[n] ^ iv[n];
}
memcpy(iv, &cipher_data[l], AES_BLOCK_SIZE);
memcpy(iv, next_iv, AES_BLOCK_SIZE);
}
l += size;
}