Source and destination buffers may point to same buffer
Merge from Widevine repo of http://go/wvgerrit/23581 This CL adds some unit tests to oemcrypto to verify that DecryptCENC and the generic encrypt and decrypt functions behave correctly when the input and output buffer is the same. i.e. decrypt in place. The mock and haystack are also updated to pass the tests. b/34080119 Change-Id: Ie295bdaddbb8058bebb36f6dab092d307f249ecd
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user