DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
decryptor_source.cc
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/base/decryptor_source.h"
8 
9 #include "packager/base/logging.h"
10 #include "packager/base/stl_util.h"
11 
12 namespace edash_packager {
13 namespace media {
14 
15 DecryptorSource::DecryptorSource(KeySource* key_source)
16  : key_source_(key_source) {
17  CHECK(key_source);
18 }
19 DecryptorSource::~DecryptorSource() {
20  STLDeleteValues(&decryptor_map_);
21 }
22 
23 bool DecryptorSource::DecryptSampleBuffer(const DecryptConfig* decrypt_config,
24  uint8_t* buffer,
25  size_t buffer_size) {
26  DCHECK(decrypt_config);
27  DCHECK(buffer);
28 
29  // Get the decryptor object.
30  AesCtrEncryptor* decryptor;
31  auto found = decryptor_map_.find(decrypt_config->key_id());
32  if (found == decryptor_map_.end()) {
33  // Create new AesCtrEncryptor
34  EncryptionKey key;
35  Status status(key_source_->GetKey(decrypt_config->key_id(), &key));
36  if (!status.ok()) {
37  LOG(ERROR) << "Error retrieving decryption key: " << status;
38  return false;
39  }
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.";
43  return false;
44  }
45  decryptor = aes_ctr_encryptor.release();
46  decryptor_map_[decrypt_config->key_id()] = decryptor;
47  } else {
48  decryptor = found->second;
49  }
50  if (!decryptor->SetIv(decrypt_config->iv())) {
51  LOG(ERROR) << "Invalid initialization vector.";
52  return false;
53  }
54 
55  if (decrypt_config->subsamples().empty()) {
56  // Sample not encrypted using subsample encryption. Decrypt whole.
57  if (!decryptor->Decrypt(buffer, buffer_size, buffer)) {
58  LOG(ERROR) << "Error during bulk sample decryption.";
59  return false;
60  }
61  return true;
62  }
63 
64  // Subsample 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) >
70  buffer_end) {
71  LOG(ERROR) << "Subsamples overflow sample buffer.";
72  return false;
73  }
74  current_ptr += subsample.clear_bytes;
75  if (!decryptor->Decrypt(current_ptr, subsample.cipher_bytes, current_ptr)) {
76  LOG(ERROR) << "Error decrypting subsample buffer.";
77  return false;
78  }
79  current_ptr += subsample.cipher_bytes;
80  }
81  return true;
82 }
83 
84 } // namespace media
85 } // namespace edash_packager
virtual Status GetKey(TrackType track_type, EncryptionKey *key)
Definition: key_source.cc:42