7 #include <packager/media/base/aes_decryptor.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
14 #include <packager/macros/crypto.h>
23 ConstantIvFlag constant_iv_flag)
24 :
AesCryptor(constant_iv_flag), padding_scheme_(padding_scheme) {
25 if (padding_scheme_ != kNoPadding) {
26 CHECK_EQ(constant_iv_flag, kUseConstantIv)
27 <<
"non-constant iv (cipher block chain across calls) only makes sense "
28 "if the padding_scheme is kNoPadding.";
32 AesCbcDecryptor::~AesCbcDecryptor() {}
35 const std::vector<uint8_t>& iv) {
36 if (!SetupCipher(key.size(), kCbcMode)) {
40 if (mbedtls_cipher_setkey(&cipher_ctx_, key.data(),
41 static_cast<int>(8 * key.size()),
42 MBEDTLS_DECRYPT) != 0) {
43 LOG(ERROR) <<
"Failed to set CBC decryption key";
50 size_t AesCbcDecryptor::RequiredOutputSize(
size_t plaintext_size) {
51 return plaintext_size;
54 bool AesCbcDecryptor::CryptInternal(
const uint8_t* ciphertext,
55 size_t ciphertext_size,
57 size_t* plaintext_size) {
58 DCHECK(plaintext_size);
62 if (*plaintext_size < ciphertext_size) {
63 LOG(ERROR) <<
"Expecting output size of at least " << ciphertext_size
67 *plaintext_size = ciphertext_size;
71 if (ciphertext_size == 0) {
72 if (padding_scheme_ == kPkcs5Padding) {
73 LOG(ERROR) <<
"Expected ciphertext to be at least " << AES_BLOCK_SIZE
74 <<
" bytes with Pkcs5 padding.";
81 const size_t residual_block_size = ciphertext_size % AES_BLOCK_SIZE;
82 const size_t cbc_size = ciphertext_size - residual_block_size;
83 if (residual_block_size == 0) {
84 CbcDecryptBlocks(ciphertext, ciphertext_size, plaintext,
86 if (padding_scheme_ != kPkcs5Padding)
90 const uint8_t num_padding_bytes = plaintext[ciphertext_size - 1];
91 if (num_padding_bytes > AES_BLOCK_SIZE) {
92 LOG(ERROR) <<
"Padding length is too large : "
93 <<
static_cast<int>(num_padding_bytes);
96 *plaintext_size -= num_padding_bytes;
98 }
else if (padding_scheme_ == kNoPadding) {
100 CbcDecryptBlocks(ciphertext, cbc_size, plaintext, internal_iv_.data());
103 memcpy(plaintext + cbc_size, ciphertext + cbc_size, residual_block_size);
105 }
else if (padding_scheme_ != kCtsPadding) {
106 LOG(ERROR) <<
"Expecting cipher text size to be multiple of "
107 << AES_BLOCK_SIZE <<
", got " << ciphertext_size;
111 DCHECK_EQ(padding_scheme_, kCtsPadding);
112 if (ciphertext_size < AES_BLOCK_SIZE) {
114 memcpy(plaintext, ciphertext, ciphertext_size);
119 if (cbc_size > AES_BLOCK_SIZE) {
120 CbcDecryptBlocks(ciphertext, cbc_size - AES_BLOCK_SIZE, plaintext,
121 internal_iv_.data());
124 const uint8_t* next_to_last_ciphertext_block =
125 ciphertext + ciphertext_size - residual_block_size - AES_BLOCK_SIZE;
126 uint8_t* next_to_last_plaintext_block =
127 plaintext + ciphertext_size - residual_block_size - AES_BLOCK_SIZE;
131 std::vector<uint8_t> last_iv(
132 ciphertext + ciphertext_size - residual_block_size,
133 ciphertext + ciphertext_size);
134 last_iv.resize(AES_BLOCK_SIZE, 0);
138 CbcDecryptBlocks(next_to_last_ciphertext_block, AES_BLOCK_SIZE,
139 next_to_last_plaintext_block, last_iv.data());
142 if (plaintext == ciphertext) {
143 std::swap_ranges(next_to_last_plaintext_block,
144 next_to_last_plaintext_block + residual_block_size,
145 next_to_last_plaintext_block + AES_BLOCK_SIZE);
147 memcpy(next_to_last_plaintext_block + AES_BLOCK_SIZE,
148 next_to_last_plaintext_block, residual_block_size);
149 memcpy(next_to_last_plaintext_block,
150 next_to_last_ciphertext_block + AES_BLOCK_SIZE, residual_block_size);
154 CbcDecryptBlocks(next_to_last_plaintext_block, AES_BLOCK_SIZE,
155 next_to_last_plaintext_block, internal_iv_.data());
159 void AesCbcDecryptor::SetIvInternal() {
161 internal_iv_.resize(AES_BLOCK_SIZE, 0);
164 void AesCbcDecryptor::CbcDecryptBlocks(
const uint8_t* ciphertext,
165 size_t ciphertext_size,
168 CHECK_EQ(ciphertext_size % AES_BLOCK_SIZE, 0u);
169 CHECK_GT(ciphertext_size, 0u);
173 const uint8_t* last_block = ciphertext + ciphertext_size - AES_BLOCK_SIZE;
174 std::vector<uint8_t> next_iv(last_block, last_block + AES_BLOCK_SIZE);
176 size_t output_size = 0;
177 CHECK_EQ(mbedtls_cipher_crypt(&cipher_ctx_,
iv, AES_BLOCK_SIZE, ciphertext,
178 ciphertext_size, plaintext, &output_size),
180 DCHECK_EQ(output_size % AES_BLOCK_SIZE, 0u);
182 memcpy(
iv, next_iv.data(), next_iv.size());
All the methods that are virtual are virtual for mocking.