7 #include "packager/media/base/aes_decryptor.h"
9 #include <openssl/aes.h>
11 #include "packager/base/logging.h"
16 bool IsKeySizeValidForAes(
size_t key_size) {
17 return key_size == 16 || key_size == 24 || key_size == 32;
22 namespace edash_packager {
26 bool chain_across_calls)
27 : padding_scheme_(padding_scheme),
28 chain_across_calls_(chain_across_calls) {
29 if (padding_scheme_ != kNoPadding) {
30 CHECK(!chain_across_calls) <<
"cipher block chain across calls only makes "
31 "sense if the padding_scheme is kNoPadding.";
35 AesCbcDecryptor::~AesCbcDecryptor() {}
38 const std::vector<uint8_t>& iv) {
39 if (!IsKeySizeValidForAes(key.size())) {
40 LOG(ERROR) <<
"Invalid AES key size: " << key.size();
44 CHECK_EQ(AES_set_decrypt_key(key.data(), key.size() * 8, mutable_aes_key()),
50 if (iv.size() != AES_BLOCK_SIZE) {
51 LOG(ERROR) <<
"Invalid IV size: " << iv.size();
59 bool AesCbcDecryptor::CryptInternal(
const uint8_t* ciphertext,
60 size_t ciphertext_size,
62 size_t* plaintext_size) {
63 DCHECK(plaintext_size);
68 if (*plaintext_size < ciphertext_size) {
69 LOG(ERROR) <<
"Expecting output size of at least " << ciphertext_size
73 *plaintext_size = ciphertext_size;
75 if (ciphertext_size == 0) {
76 if (padding_scheme_ == kPkcs5Padding) {
77 LOG(ERROR) <<
"Expected ciphertext to be at least " << AES_BLOCK_SIZE
78 <<
" bytes with Pkcs5 padding.";
85 std::vector<uint8_t> local_iv(
iv());
86 const size_t residual_block_size = ciphertext_size % AES_BLOCK_SIZE;
87 if (residual_block_size == 0) {
88 AES_cbc_encrypt(ciphertext, plaintext, ciphertext_size, aes_key(),
89 local_iv.data(), AES_DECRYPT);
90 if (chain_across_calls_)
92 if (padding_scheme_ != kPkcs5Padding)
96 const uint8_t num_padding_bytes = plaintext[ciphertext_size - 1];
97 if (num_padding_bytes > AES_BLOCK_SIZE) {
98 LOG(ERROR) <<
"Padding length is too large : "
99 <<
static_cast<int>(num_padding_bytes);
102 *plaintext_size -= num_padding_bytes;
104 }
else if (padding_scheme_ != kCtsPadding) {
105 LOG(ERROR) <<
"Expecting cipher text size to be multiple of "
106 << AES_BLOCK_SIZE <<
", got " << ciphertext_size;
110 DCHECK(!chain_across_calls_);
111 DCHECK_EQ(padding_scheme_, kCtsPadding);
112 if (ciphertext_size < AES_BLOCK_SIZE) {
114 memcpy(plaintext, ciphertext, ciphertext_size);
119 const size_t cbc_size = ciphertext_size - residual_block_size;
120 if (cbc_size > AES_BLOCK_SIZE) {
121 AES_cbc_encrypt(ciphertext, plaintext, cbc_size - AES_BLOCK_SIZE, aes_key(),
122 local_iv.data(), AES_DECRYPT);
125 const uint8_t* next_to_last_ciphertext_block =
126 ciphertext + ciphertext_size - residual_block_size - AES_BLOCK_SIZE;
127 uint8_t* next_to_last_plaintext_block =
128 plaintext + ciphertext_size - residual_block_size - AES_BLOCK_SIZE;
132 std::vector<uint8_t> last_iv(
133 ciphertext + ciphertext_size - residual_block_size,
134 ciphertext + ciphertext_size);
135 last_iv.resize(AES_BLOCK_SIZE, 0);
139 AES_cbc_encrypt(next_to_last_ciphertext_block, next_to_last_plaintext_block,
140 AES_BLOCK_SIZE, aes_key(), last_iv.data(), AES_DECRYPT);
143 if (plaintext == ciphertext) {
144 std::swap_ranges(next_to_last_plaintext_block,
145 next_to_last_plaintext_block + residual_block_size,
146 next_to_last_plaintext_block + AES_BLOCK_SIZE);
148 memcpy(next_to_last_plaintext_block + AES_BLOCK_SIZE,
149 next_to_last_plaintext_block, residual_block_size);
150 memcpy(next_to_last_plaintext_block,
151 next_to_last_ciphertext_block + AES_BLOCK_SIZE, residual_block_size);
155 AES_cbc_encrypt(next_to_last_plaintext_block, next_to_last_plaintext_block,
156 AES_BLOCK_SIZE, aes_key(), local_iv.data(), AES_DECRYPT);