2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-03-17 17:03:19 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/base/aes_decryptor.h>
|
2016-03-17 17:03:19 +00:00
|
|
|
|
2016-08-19 22:32:27 +00:00
|
|
|
#include <algorithm>
|
2016-03-17 17:03:19 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
2016-03-17 17:03:19 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/macros/crypto.h>
|
2016-03-17 17:03:19 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-17 17:03:19 +00:00
|
|
|
namespace media {
|
|
|
|
|
2016-04-13 17:52:41 +00:00
|
|
|
AesCbcDecryptor::AesCbcDecryptor(CbcPaddingScheme padding_scheme)
|
|
|
|
: AesCbcDecryptor(padding_scheme, kDontUseConstantIv) {}
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
AesCbcDecryptor::AesCbcDecryptor(CbcPaddingScheme padding_scheme,
|
2016-04-13 17:52:41 +00:00
|
|
|
ConstantIvFlag constant_iv_flag)
|
|
|
|
: AesCryptor(constant_iv_flag), padding_scheme_(padding_scheme) {
|
2016-03-25 18:02:43 +00:00
|
|
|
if (padding_scheme_ != kNoPadding) {
|
2016-04-13 17:52:41 +00:00
|
|
|
CHECK_EQ(constant_iv_flag, kUseConstantIv)
|
|
|
|
<< "non-constant iv (cipher block chain across calls) only makes sense "
|
|
|
|
"if the padding_scheme is kNoPadding.";
|
2016-03-25 18:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-06 00:19:16 +00:00
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
AesCbcDecryptor::~AesCbcDecryptor() {}
|
2016-03-17 17:03:19 +00:00
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
bool AesCbcDecryptor::InitializeWithIv(const std::vector<uint8_t>& key,
|
|
|
|
const std::vector<uint8_t>& iv) {
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!SetupCipher(key.size(), kCbcMode)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mbedtls_cipher_setkey(&cipher_ctx_, key.data(),
|
|
|
|
static_cast<int>(8 * key.size()),
|
|
|
|
MBEDTLS_DECRYPT) != 0) {
|
|
|
|
LOG(ERROR) << "Failed to set CBC decryption key";
|
2016-03-17 17:03:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
return SetIv(iv);
|
2016-03-17 17:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
size_t AesCbcDecryptor::RequiredOutputSize(size_t plaintext_size) {
|
|
|
|
// mbedtls requires a buffer large enough for one extra block.
|
|
|
|
return plaintext_size + AES_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:19:16 +00:00
|
|
|
bool AesCbcDecryptor::CryptInternal(const uint8_t* ciphertext,
|
|
|
|
size_t ciphertext_size,
|
|
|
|
uint8_t* plaintext,
|
|
|
|
size_t* plaintext_size) {
|
2016-03-25 18:02:43 +00:00
|
|
|
DCHECK(plaintext_size);
|
|
|
|
// Plaintext size is the same as ciphertext size except for pkcs5 padding.
|
2016-04-06 00:19:16 +00:00
|
|
|
// Will update later if using pkcs5 padding. For pkcs5 padding, we still
|
|
|
|
// need at least |ciphertext_size| bytes for intermediate operation.
|
2023-12-01 17:32:19 +00:00
|
|
|
// mbedtls requires a buffer large enough for one extra block.
|
|
|
|
const size_t required_plaintext_size = ciphertext_size + AES_BLOCK_SIZE;
|
|
|
|
if (*plaintext_size < required_plaintext_size) {
|
|
|
|
LOG(ERROR) << "Expecting output size of at least "
|
|
|
|
<< required_plaintext_size << " bytes.";
|
2016-04-06 00:19:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-12-01 17:32:19 +00:00
|
|
|
*plaintext_size = required_plaintext_size - AES_BLOCK_SIZE;
|
2016-04-06 00:19:16 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
// If the ciphertext size is 0, this can be a no-op decrypt, so long as the
|
|
|
|
// padding mode isn't PKCS5.
|
2016-03-25 18:02:43 +00:00
|
|
|
if (ciphertext_size == 0) {
|
|
|
|
if (padding_scheme_ == kPkcs5Padding) {
|
|
|
|
LOG(ERROR) << "Expected ciphertext to be at least " << AES_BLOCK_SIZE
|
2016-04-06 00:19:16 +00:00
|
|
|
<< " bytes with Pkcs5 padding.";
|
2016-03-25 18:02:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-03-17 17:03:19 +00:00
|
|
|
}
|
2016-03-25 18:02:43 +00:00
|
|
|
DCHECK(plaintext);
|
|
|
|
|
|
|
|
const size_t residual_block_size = ciphertext_size % AES_BLOCK_SIZE;
|
2016-04-13 23:43:55 +00:00
|
|
|
const size_t cbc_size = ciphertext_size - residual_block_size;
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
// Copy the residual block early, since mbedtls may overwrite one extra block
|
|
|
|
// of the output, and input and output may be the same buffer.
|
|
|
|
std::vector<uint8_t> residual_block(ciphertext + cbc_size,
|
|
|
|
ciphertext + ciphertext_size);
|
|
|
|
DCHECK_EQ(residual_block.size(), residual_block_size);
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
if (residual_block_size == 0) {
|
2023-12-01 17:32:19 +00:00
|
|
|
CbcDecryptBlocks(ciphertext, ciphertext_size, plaintext);
|
2016-03-25 18:02:43 +00:00
|
|
|
if (padding_scheme_ != kPkcs5Padding)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Strip off PKCS5 padding bytes.
|
|
|
|
const uint8_t num_padding_bytes = plaintext[ciphertext_size - 1];
|
|
|
|
if (num_padding_bytes > AES_BLOCK_SIZE) {
|
|
|
|
LOG(ERROR) << "Padding length is too large : "
|
|
|
|
<< static_cast<int>(num_padding_bytes);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*plaintext_size -= num_padding_bytes;
|
|
|
|
return true;
|
2016-04-13 23:43:55 +00:00
|
|
|
} else if (padding_scheme_ == kNoPadding) {
|
2023-12-01 17:32:19 +00:00
|
|
|
CbcDecryptBlocks(ciphertext, cbc_size, plaintext);
|
2016-04-13 23:43:55 +00:00
|
|
|
|
|
|
|
// The residual block is not encrypted.
|
2023-12-01 17:32:19 +00:00
|
|
|
memcpy(plaintext + cbc_size, residual_block.data(), residual_block_size);
|
2016-04-13 23:43:55 +00:00
|
|
|
return true;
|
2016-03-25 18:02:43 +00:00
|
|
|
} else if (padding_scheme_ != kCtsPadding) {
|
|
|
|
LOG(ERROR) << "Expecting cipher text size to be multiple of "
|
|
|
|
<< AES_BLOCK_SIZE << ", got " << ciphertext_size;
|
2016-03-17 17:03:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-25 18:02:43 +00:00
|
|
|
DCHECK_EQ(padding_scheme_, kCtsPadding);
|
2016-03-17 17:03:19 +00:00
|
|
|
if (ciphertext_size < AES_BLOCK_SIZE) {
|
|
|
|
// Don't have a full block, leave unencrypted.
|
|
|
|
memcpy(plaintext, ciphertext, ciphertext_size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
// Copy the next-to-last block early, since mbedtls may overwrite one extra
|
|
|
|
// block of the output, and input and output may be the same buffer.
|
|
|
|
// NOTE: Before this point, there may not be such a block. Here, we know
|
|
|
|
// this is safe.
|
|
|
|
std::vector<uint8_t> next_to_last_block(
|
|
|
|
ciphertext + cbc_size - AES_BLOCK_SIZE, ciphertext + cbc_size);
|
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
// AES-CBC decrypt everything up to the next-to-last full block.
|
|
|
|
if (cbc_size > AES_BLOCK_SIZE) {
|
2023-12-01 17:32:19 +00:00
|
|
|
CbcDecryptBlocks(ciphertext, cbc_size - AES_BLOCK_SIZE, plaintext);
|
2016-03-17 17:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
uint8_t* next_to_last_plaintext_block = plaintext + cbc_size - AES_BLOCK_SIZE;
|
|
|
|
|
|
|
|
// The next-to-last block should be decrypted first in ECB mode, which is
|
|
|
|
// effectively what you get with an IV of all zeroes.
|
|
|
|
std::vector<uint8_t> backup_iv(internal_iv_);
|
|
|
|
internal_iv_.assign(AES_BLOCK_SIZE, 0);
|
|
|
|
// mbedtls requires a buffer large enough for one extra block.
|
|
|
|
std::vector<uint8_t> stolen_bits(AES_BLOCK_SIZE * 2);
|
|
|
|
CbcDecryptBlocks(next_to_last_block.data(), AES_BLOCK_SIZE,
|
|
|
|
stolen_bits.data());
|
|
|
|
|
|
|
|
// Reconstruct the final two blocks of ciphertext.
|
|
|
|
std::vector<uint8_t> reconstructed_blocks(AES_BLOCK_SIZE * 2);
|
|
|
|
memcpy(reconstructed_blocks.data(), residual_block.data(),
|
|
|
|
residual_block_size);
|
|
|
|
memcpy(reconstructed_blocks.data() + residual_block_size,
|
|
|
|
stolen_bits.data() + residual_block_size,
|
|
|
|
AES_BLOCK_SIZE - residual_block_size);
|
|
|
|
memcpy(reconstructed_blocks.data() + AES_BLOCK_SIZE,
|
|
|
|
next_to_last_block.data(), AES_BLOCK_SIZE);
|
|
|
|
|
|
|
|
// Decrypt the last two blocks.
|
|
|
|
internal_iv_ = backup_iv;
|
|
|
|
// mbedtls requires a buffer large enough for one extra block.
|
|
|
|
std::vector<uint8_t> final_output_blocks(AES_BLOCK_SIZE * 3);
|
|
|
|
CbcDecryptBlocks(reconstructed_blocks.data(), AES_BLOCK_SIZE * 2,
|
|
|
|
final_output_blocks.data());
|
|
|
|
|
|
|
|
// Copy the final output.
|
|
|
|
memcpy(next_to_last_plaintext_block, final_output_blocks.data(),
|
|
|
|
AES_BLOCK_SIZE + residual_block_size);
|
2016-03-17 17:03:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-13 17:52:41 +00:00
|
|
|
void AesCbcDecryptor::SetIvInternal() {
|
|
|
|
internal_iv_ = iv();
|
|
|
|
internal_iv_.resize(AES_BLOCK_SIZE, 0);
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
void AesCbcDecryptor::CbcDecryptBlocks(const uint8_t* ciphertext,
|
|
|
|
size_t ciphertext_size,
|
|
|
|
uint8_t* plaintext) {
|
|
|
|
CHECK_EQ(ciphertext_size % AES_BLOCK_SIZE, 0u);
|
|
|
|
CHECK_GT(ciphertext_size, 0u);
|
|
|
|
|
|
|
|
// Copy the final block of ciphertext before decryption, since we could be
|
|
|
|
// decrypting in-place.
|
|
|
|
const uint8_t* last_block = ciphertext + ciphertext_size - AES_BLOCK_SIZE;
|
|
|
|
std::vector<uint8_t> next_iv(last_block, last_block + AES_BLOCK_SIZE);
|
|
|
|
|
|
|
|
size_t output_size = 0;
|
|
|
|
CHECK_EQ(mbedtls_cipher_crypt(&cipher_ctx_, internal_iv_.data(),
|
|
|
|
AES_BLOCK_SIZE, ciphertext, ciphertext_size,
|
|
|
|
plaintext, &output_size),
|
|
|
|
0);
|
|
|
|
DCHECK_EQ(output_size % AES_BLOCK_SIZE, 0u);
|
|
|
|
|
|
|
|
// Update the internal IV.
|
|
|
|
internal_iv_ = next_iv;
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:03:19 +00:00
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|