7 #include "packager/media/base/decryptor_source.h" 9 #include "packager/base/logging.h" 10 #include "packager/media/base/aes_decryptor.h" 11 #include "packager/media/base/aes_pattern_cryptor.h" 16 bool CheckMemoryOverlap(
const uint8_t* encrypted_buffer,
18 uint8_t* decrypted_buffer) {
19 return (decrypted_buffer < encrypted_buffer)
20 ? (encrypted_buffer < decrypted_buffer + buffer_size)
21 : (decrypted_buffer < encrypted_buffer + buffer_size);
29 : key_source_(key_source) {
32 DecryptorSource::~DecryptorSource() {}
35 const uint8_t* encrypted_buffer,
37 uint8_t* decrypted_buffer) {
38 DCHECK(decrypt_config);
39 DCHECK(encrypted_buffer);
40 DCHECK(decrypted_buffer);
42 if (CheckMemoryOverlap(encrypted_buffer, buffer_size, decrypted_buffer)) {
43 LOG(ERROR) <<
"Encrypted buffer and decrypted buffer cannot overlap.";
49 auto found = decryptor_map_.find(decrypt_config->key_id());
50 if (found == decryptor_map_.end()) {
53 Status status(key_source_->
GetKey(decrypt_config->key_id(), &key));
55 LOG(ERROR) <<
"Error retrieving decryption key: " << status;
59 std::unique_ptr<AesCryptor> aes_decryptor;
60 switch (decrypt_config->protection_scheme()) {
69 decrypt_config->crypt_byte_block(),
70 decrypt_config->skip_byte_block(),
72 AesCryptor::kDontUseConstantIv,
77 decrypt_config->crypt_byte_block(),
78 decrypt_config->skip_byte_block(),
80 AesCryptor::kUseConstantIv,
84 LOG(ERROR) <<
"Unsupported protection scheme: " 85 << decrypt_config->protection_scheme();
89 if (!aes_decryptor->InitializeWithIv(key.key, decrypt_config->iv())) {
90 LOG(ERROR) <<
"Failed to initialize AesDecryptor for decryption.";
93 decryptor = aes_decryptor.get();
94 decryptor_map_[decrypt_config->key_id()] = std::move(aes_decryptor);
96 decryptor = found->second.get();
98 if (!decryptor->
SetIv(decrypt_config->iv())) {
99 LOG(ERROR) <<
"Invalid initialization vector.";
103 if (decrypt_config->subsamples().empty()) {
105 if (!decryptor->Crypt(encrypted_buffer, buffer_size, decrypted_buffer)) {
106 LOG(ERROR) <<
"Error during bulk sample decryption.";
113 const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
114 const uint8_t* current_ptr = encrypted_buffer;
115 const uint8_t*
const buffer_end = encrypted_buffer + buffer_size;
116 for (
const auto& subsample : subsamples) {
117 if ((current_ptr + subsample.clear_bytes + subsample.cipher_bytes) >
119 LOG(ERROR) <<
"Subsamples overflow sample buffer.";
122 memcpy(decrypted_buffer, current_ptr, subsample.clear_bytes);
123 current_ptr += subsample.clear_bytes;
124 decrypted_buffer += subsample.clear_bytes;
125 if (!decryptor->Crypt(current_ptr, subsample.cipher_bytes,
127 LOG(ERROR) <<
"Error decrypting subsample buffer.";
130 current_ptr += subsample.cipher_bytes;
131 decrypted_buffer += subsample.cipher_bytes;
All the methods that are virtual are virtual for mocking.