7 #include <packager/media/base/decryptor_source.h>
9 #include <absl/log/check.h>
10 #include <absl/log/log.h>
12 #include <packager/media/base/aes_decryptor.h>
13 #include <packager/media/base/aes_pattern_cryptor.h>
18 bool CheckMemoryOverlap(
const uint8_t* encrypted_buffer,
20 uint8_t* decrypted_buffer) {
21 return (decrypted_buffer < encrypted_buffer)
22 ? (encrypted_buffer < decrypted_buffer + buffer_size)
23 : (decrypted_buffer < encrypted_buffer + buffer_size);
31 : key_source_(key_source) {
34 DecryptorSource::~DecryptorSource() {}
37 const uint8_t* encrypted_buffer,
39 uint8_t* decrypted_buffer) {
40 DCHECK(decrypt_config);
41 DCHECK(encrypted_buffer);
42 DCHECK(decrypted_buffer);
44 if (CheckMemoryOverlap(encrypted_buffer, buffer_size, decrypted_buffer)) {
45 LOG(ERROR) <<
"Encrypted buffer and decrypted buffer cannot overlap.";
51 auto found = decryptor_map_.find(decrypt_config->key_id());
52 if (found == decryptor_map_.end()) {
55 Status status(key_source_->
GetKey(decrypt_config->key_id(), &key));
57 LOG(ERROR) <<
"Error retrieving decryption key: " << status;
61 std::unique_ptr<AesCryptor> aes_decryptor;
62 switch (decrypt_config->protection_scheme()) {
71 decrypt_config->crypt_byte_block(),
72 decrypt_config->skip_byte_block(),
74 AesCryptor::kDontUseConstantIv,
79 decrypt_config->crypt_byte_block(),
80 decrypt_config->skip_byte_block(),
82 AesCryptor::kUseConstantIv,
86 LOG(ERROR) <<
"Unsupported protection scheme: "
87 << decrypt_config->protection_scheme();
91 if (!aes_decryptor->InitializeWithIv(key.key, decrypt_config->iv())) {
92 LOG(ERROR) <<
"Failed to initialize AesDecryptor for decryption.";
95 decryptor = aes_decryptor.get();
96 decryptor_map_[decrypt_config->key_id()] = std::move(aes_decryptor);
98 decryptor = found->second.get();
100 if (!decryptor->
SetIv(decrypt_config->iv())) {
101 LOG(ERROR) <<
"Invalid initialization vector.";
105 if (decrypt_config->subsamples().empty()) {
107 if (!decryptor->Crypt(encrypted_buffer, buffer_size, decrypted_buffer)) {
108 LOG(ERROR) <<
"Error during bulk sample decryption.";
115 const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
116 const uint8_t* current_ptr = encrypted_buffer;
117 const uint8_t*
const buffer_end = encrypted_buffer + buffer_size;
118 for (
const auto& subsample : subsamples) {
119 if ((current_ptr + subsample.clear_bytes + subsample.cipher_bytes) >
121 LOG(ERROR) <<
"Subsamples overflow sample buffer.";
124 memcpy(decrypted_buffer, current_ptr, subsample.clear_bytes);
125 current_ptr += subsample.clear_bytes;
126 decrypted_buffer += subsample.clear_bytes;
127 if (!decryptor->Crypt(current_ptr, subsample.cipher_bytes,
129 LOG(ERROR) <<
"Error decrypting subsample buffer.";
132 current_ptr += subsample.cipher_bytes;
133 decrypted_buffer += subsample.cipher_bytes;
All the methods that are virtual are virtual for mocking.