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 DecryptorSource::DecryptorSource(KeySource* key_source)
17 : key_source_(key_source) {
20 DecryptorSource::~DecryptorSource() {}
22 bool DecryptorSource::DecryptSampleBuffer(
const DecryptConfig* decrypt_config,
25 DCHECK(decrypt_config);
29 AesCryptor* decryptor =
nullptr;
30 auto found = decryptor_map_.find(decrypt_config->key_id());
31 if (found == decryptor_map_.end()) {
34 Status status(key_source_->
GetKey(decrypt_config->key_id(), &key));
36 LOG(ERROR) <<
"Error retrieving decryption key: " << status;
40 std::unique_ptr<AesCryptor> aes_decryptor;
41 switch (decrypt_config->protection_scheme()) {
43 aes_decryptor.reset(
new AesCtrDecryptor);
46 aes_decryptor.reset(
new AesCbcDecryptor(kNoPadding));
49 aes_decryptor.reset(
new AesPatternCryptor(
50 decrypt_config->crypt_byte_block(),
51 decrypt_config->skip_byte_block(),
53 AesCryptor::kDontUseConstantIv,
54 std::unique_ptr<AesCryptor>(
new AesCtrDecryptor())));
57 aes_decryptor.reset(
new AesPatternCryptor(
58 decrypt_config->crypt_byte_block(),
59 decrypt_config->skip_byte_block(),
61 AesCryptor::kUseConstantIv,
62 std::unique_ptr<AesCryptor>(
new AesCbcDecryptor(kNoPadding))));
65 LOG(ERROR) <<
"Unsupported protection scheme: "
66 << decrypt_config->protection_scheme();
70 if (!aes_decryptor->InitializeWithIv(key.key, decrypt_config->iv())) {
71 LOG(ERROR) <<
"Failed to initialize AesDecryptor for decryption.";
74 decryptor = aes_decryptor.get();
75 decryptor_map_[decrypt_config->key_id()] = std::move(aes_decryptor);
77 decryptor = found->second.get();
79 if (!decryptor->SetIv(decrypt_config->iv())) {
80 LOG(ERROR) <<
"Invalid initialization vector.";
84 if (decrypt_config->subsamples().empty()) {
86 if (!decryptor->Crypt(buffer, buffer_size, buffer)) {
87 LOG(ERROR) <<
"Error during bulk sample decryption.";
94 const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
95 uint8_t* current_ptr = buffer;
96 const uint8_t*
const buffer_end = buffer + buffer_size;
97 for (
const auto& subsample : subsamples) {
98 if ((current_ptr + subsample.clear_bytes + subsample.cipher_bytes) >
100 LOG(ERROR) <<
"Subsamples overflow sample buffer.";
103 current_ptr += subsample.clear_bytes;
104 if (!decryptor->Crypt(current_ptr, subsample.cipher_bytes, current_ptr)) {
105 LOG(ERROR) <<
"Error decrypting subsample buffer.";
108 current_ptr += subsample.cipher_bytes;