7 #include "packager/media/base/decryptor_source.h"
9 #include "packager/base/logging.h"
10 #include "packager/base/stl_util.h"
12 namespace edash_packager {
15 DecryptorSource::DecryptorSource(KeySource* key_source)
16 : key_source_(key_source) {
19 DecryptorSource::~DecryptorSource() {
20 STLDeleteValues(&decryptor_map_);
23 bool DecryptorSource::DecryptSampleBuffer(
const DecryptConfig* decrypt_config,
26 DCHECK(decrypt_config);
30 AesCtrEncryptor* decryptor;
31 auto found = decryptor_map_.find(decrypt_config->key_id());
32 if (found == decryptor_map_.end()) {
35 Status status(key_source_->
GetKey(decrypt_config->key_id(), &key));
37 LOG(ERROR) <<
"Error retrieving decryption key: " << status;
40 scoped_ptr<AesCtrEncryptor> aes_ctr_encryptor(
new AesCtrEncryptor);
41 if (!aes_ctr_encryptor->InitializeWithIv(key.key, decrypt_config->iv())) {
42 LOG(ERROR) <<
"Failed to initialize AesCtrEncryptor for decryption.";
45 decryptor = aes_ctr_encryptor.release();
46 decryptor_map_[decrypt_config->key_id()] = decryptor;
48 decryptor = found->second;
50 if (!decryptor->SetIv(decrypt_config->iv())) {
51 LOG(ERROR) <<
"Invalid initialization vector.";
55 if (decrypt_config->subsamples().empty()) {
57 if (!decryptor->Decrypt(buffer, buffer_size, buffer)) {
58 LOG(ERROR) <<
"Error during bulk sample decryption.";
65 const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
66 uint8_t* current_ptr = buffer;
67 const uint8_t*
const buffer_end = buffer + buffer_size;
68 for (
const auto& subsample : subsamples) {
69 if ((current_ptr + subsample.clear_bytes + subsample.cipher_bytes) >
71 LOG(ERROR) <<
"Subsamples overflow sample buffer.";
74 current_ptr += subsample.clear_bytes;
75 if (!decryptor->Decrypt(current_ptr, subsample.cipher_bytes, current_ptr)) {
76 LOG(ERROR) <<
"Error decrypting subsample buffer.";
79 current_ptr += subsample.cipher_bytes;