7 #include <packager/media/base/aes_pattern_cryptor.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
14 #include <packager/macros/crypto.h>
20 uint8_t skip_byte_block,
22 ConstantIvFlag constant_iv_flag,
23 std::unique_ptr<AesCryptor> cryptor)
25 crypt_byte_block_(crypt_byte_block),
26 skip_byte_block_(skip_byte_block),
27 encryption_mode_(encryption_mode),
28 cryptor_(std::move(cryptor)) {
30 if (crypt_byte_block_ == 0 && skip_byte_block_ == 0)
31 crypt_byte_block_ = 1;
33 DCHECK(!cryptor_->use_constant_iv());
36 AesPatternCryptor::~AesPatternCryptor() {}
39 const std::vector<uint8_t>& iv) {
40 return SetIv(
iv) && cryptor_->InitializeWithIv(key,
iv);
43 bool AesPatternCryptor::CryptInternal(
const uint8_t* text,
46 size_t* crypt_text_size) {
48 if (*crypt_text_size < text_size) {
49 LOG(ERROR) <<
"Expecting output size of at least " << text_size
53 *crypt_text_size = text_size;
55 while (text_size > 0) {
56 const size_t crypt_byte_size = crypt_byte_block_ * AES_BLOCK_SIZE;
58 if (text_size <= crypt_byte_size) {
59 const bool need_encrypt =
61 text_size >= AES_BLOCK_SIZE;
65 const size_t aligned_crypt_byte_size =
66 text_size / AES_BLOCK_SIZE * AES_BLOCK_SIZE;
67 if (!cryptor_->Crypt(text, aligned_crypt_byte_size, crypt_text))
69 text += aligned_crypt_byte_size;
70 text_size -= aligned_crypt_byte_size;
71 crypt_text += aligned_crypt_byte_size;
75 memcpy(crypt_text, text, text_size);
79 if (!cryptor_->Crypt(text, crypt_byte_size, crypt_text))
81 text += crypt_byte_size;
82 text_size -= crypt_byte_size;
83 crypt_text += crypt_byte_size;
85 const size_t skip_byte_size = std::min(
86 static_cast<size_t>(skip_byte_block_ * AES_BLOCK_SIZE), text_size);
87 memcpy(crypt_text, text, skip_byte_size);
88 text += skip_byte_size;
89 text_size -= skip_byte_size;
90 crypt_text += skip_byte_size;
95 void AesPatternCryptor::SetIvInternal() {
96 CHECK(cryptor_->SetIv(
iv()));
All the methods that are virtual are virtual for mocking.