7 #include "packager/media/base/decryptor_source.h"
9 #include "packager/base/logging.h"
10 #include "packager/base/stl_util.h"
11 #include "packager/media/base/aes_decryptor.h"
12 #include "packager/media/base/aes_pattern_cryptor.h"
14 namespace edash_packager {
17 DecryptorSource::DecryptorSource(KeySource* key_source)
18 : key_source_(key_source) {
21 DecryptorSource::~DecryptorSource() {
22 STLDeleteValues(&decryptor_map_);
25 bool DecryptorSource::DecryptSampleBuffer(
const DecryptConfig* decrypt_config,
28 DCHECK(decrypt_config);
32 AesCryptor* decryptor;
33 auto found = decryptor_map_.find(decrypt_config->key_id());
34 if (found == decryptor_map_.end()) {
37 Status status(key_source_->
GetKey(decrypt_config->key_id(), &key));
39 LOG(ERROR) <<
"Error retrieving decryption key: " << status;
43 scoped_ptr<AesCryptor> aes_decryptor;
44 switch (decrypt_config->protection_scheme()) {
46 aes_decryptor.reset(
new AesCtrDecryptor);
49 aes_decryptor.reset(
new AesCbcDecryptor(kNoPadding));
52 aes_decryptor.reset(
new AesPatternCryptor(
53 decrypt_config->crypt_byte_block(),
54 decrypt_config->skip_byte_block(),
56 AesCryptor::kDontUseConstantIv,
57 scoped_ptr<AesCryptor>(
new AesCtrDecryptor())));
60 aes_decryptor.reset(
new AesPatternCryptor(
61 decrypt_config->crypt_byte_block(),
62 decrypt_config->skip_byte_block(),
64 AesCryptor::kUseConstantIv,
65 scoped_ptr<AesCryptor>(
new AesCbcDecryptor(kNoPadding))));
68 LOG(ERROR) <<
"Unsupported protection scheme: "
69 << decrypt_config->protection_scheme();
73 if (!aes_decryptor->InitializeWithIv(key.key, decrypt_config->iv())) {
74 LOG(ERROR) <<
"Failed to initialize AesDecryptor for decryption.";
77 decryptor = aes_decryptor.release();
78 decryptor_map_[decrypt_config->key_id()] = decryptor;
80 decryptor = found->second;
82 if (!decryptor->SetIv(decrypt_config->iv())) {
83 LOG(ERROR) <<
"Invalid initialization vector.";
87 if (decrypt_config->subsamples().empty()) {
89 if (!decryptor->Crypt(buffer, buffer_size, buffer)) {
90 LOG(ERROR) <<
"Error during bulk sample decryption.";
97 const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
98 uint8_t* current_ptr = buffer;
99 const uint8_t*
const buffer_end = buffer + buffer_size;
100 for (
const auto& subsample : subsamples) {
101 if ((current_ptr + subsample.clear_bytes + subsample.cipher_bytes) >
103 LOG(ERROR) <<
"Subsamples overflow sample buffer.";
106 current_ptr += subsample.clear_bytes;
107 if (!decryptor->Crypt(current_ptr, subsample.cipher_bytes, current_ptr)) {
108 LOG(ERROR) <<
"Error decrypting subsample buffer.";
111 current_ptr += subsample.cipher_bytes;