7#include <packager/media/base/aes_pattern_cryptor.h>
17#include <absl/log/check.h>
18#include <absl/log/log.h>
20#include <packager/macros/crypto.h>
21#include <packager/media/base/aes_cryptor.h>
27 uint8_t skip_byte_block,
29 ConstantIvFlag constant_iv_flag,
30 std::unique_ptr<AesCryptor> cryptor)
32 crypt_byte_block_(crypt_byte_block),
33 skip_byte_block_(skip_byte_block),
34 encryption_mode_(encryption_mode),
35 cryptor_(std::move(cryptor)) {
37 if (crypt_byte_block_ == 0 && skip_byte_block_ == 0)
38 crypt_byte_block_ = 1;
40 DCHECK(!cryptor_->use_constant_iv());
43AesPatternCryptor::~AesPatternCryptor() {}
46 const std::vector<uint8_t>& iv) {
47 return SetIv(
iv) && cryptor_->InitializeWithIv(key,
iv);
50bool AesPatternCryptor::CryptInternal(
const uint8_t* text,
53 size_t* crypt_text_size) {
55 if (*crypt_text_size < text_size) {
56 LOG(ERROR) <<
"Expecting output size of at least " << text_size
60 *crypt_text_size = text_size;
62 while (text_size > 0) {
63 const size_t crypt_byte_size = crypt_byte_block_ * AES_BLOCK_SIZE;
65 if (text_size <= crypt_byte_size) {
66 const bool need_encrypt =
68 text_size >= AES_BLOCK_SIZE;
72 const size_t aligned_crypt_byte_size =
73 text_size / AES_BLOCK_SIZE * AES_BLOCK_SIZE;
74 if (!cryptor_->Crypt(text, aligned_crypt_byte_size, crypt_text))
76 text += aligned_crypt_byte_size;
77 text_size -= aligned_crypt_byte_size;
78 crypt_text += aligned_crypt_byte_size;
82 memcpy(crypt_text, text, text_size);
86 if (!cryptor_->Crypt(text, crypt_byte_size, crypt_text))
88 text += crypt_byte_size;
89 text_size -= crypt_byte_size;
90 crypt_text += crypt_byte_size;
92 const size_t skip_byte_size = std::min(
93 static_cast<size_t>(skip_byte_block_ * AES_BLOCK_SIZE), text_size);
94 memcpy(crypt_text, text, skip_byte_size);
95 text += skip_byte_size;
96 text_size -= skip_byte_size;
97 crypt_text += skip_byte_size;
102void AesPatternCryptor::SetIvInternal() {
103 CHECK(cryptor_->SetIv(
iv()));
All the methods that are virtual are virtual for mocking.