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 AesCryptor* 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;
41 scoped_ptr<AesCryptor> aes_decryptor;
42 switch (decrypt_config->decryption_mode()) {
43 case kEncryptionModeAesCtr:
44 aes_decryptor.reset(
new AesCtrDecryptor);
46 case kEncryptionModeAesCbc:
47 aes_decryptor.reset(
new AesCbcDecryptor(kNoPadding, kChainAcrossCalls));
50 LOG(ERROR) <<
"Unsupported Decryption Mode: "
51 << decrypt_config->decryption_mode();
55 if (!aes_decryptor->InitializeWithIv(key.key, decrypt_config->iv())) {
56 LOG(ERROR) <<
"Failed to initialize AesDecryptor for decryption.";
59 decryptor = aes_decryptor.release();
60 decryptor_map_[decrypt_config->key_id()] = decryptor;
62 decryptor = found->second;
64 if (!decryptor->SetIv(decrypt_config->iv())) {
65 LOG(ERROR) <<
"Invalid initialization vector.";
69 if (decrypt_config->subsamples().empty()) {
71 if (!decryptor->Crypt(buffer, buffer_size, buffer)) {
72 LOG(ERROR) <<
"Error during bulk sample decryption.";
79 const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
80 uint8_t* current_ptr = buffer;
81 const uint8_t*
const buffer_end = buffer + buffer_size;
82 for (
const auto& subsample : subsamples) {
83 if ((current_ptr + subsample.clear_bytes + subsample.cipher_bytes) >
85 LOG(ERROR) <<
"Subsamples overflow sample buffer.";
88 current_ptr += subsample.clear_bytes;
89 if (!decryptor->Crypt(current_ptr, subsample.cipher_bytes, current_ptr)) {
90 LOG(ERROR) <<
"Error decrypting subsample buffer.";
93 current_ptr += subsample.cipher_bytes;