50 const uint8_t* encrypted_buffer,
52 uint8_t* decrypted_buffer) {
53 DCHECK(decrypt_config);
54 DCHECK(encrypted_buffer);
55 DCHECK(decrypted_buffer);
57 if (CheckMemoryOverlap(encrypted_buffer, buffer_size, decrypted_buffer)) {
58 LOG(ERROR) <<
"Encrypted buffer and decrypted buffer cannot overlap.";
64 auto found = decryptor_map_.find(decrypt_config->key_id());
65 if (found == decryptor_map_.end()) {
68 Status status(key_source_->
GetKey(decrypt_config->key_id(), &key));
70 LOG(ERROR) <<
"Error retrieving decryption key: " << status;
74 std::unique_ptr<AesCryptor> aes_decryptor;
75 switch (decrypt_config->protection_scheme()) {
84 decrypt_config->crypt_byte_block(),
85 decrypt_config->skip_byte_block(),
87 AesCryptor::kDontUseConstantIv,
92 decrypt_config->crypt_byte_block(),
93 decrypt_config->skip_byte_block(),
95 AesCryptor::kUseConstantIv,
99 LOG(ERROR) <<
"Unsupported protection scheme: "
100 << decrypt_config->protection_scheme();
104 if (!aes_decryptor->InitializeWithIv(key.key, decrypt_config->iv())) {
105 LOG(ERROR) <<
"Failed to initialize AesDecryptor for decryption.";
108 decryptor = aes_decryptor.get();
109 decryptor_map_[decrypt_config->key_id()] = std::move(aes_decryptor);
111 decryptor = found->second.get();
113 if (!decryptor->
SetIv(decrypt_config->iv())) {
114 LOG(ERROR) <<
"Invalid initialization vector.";
118 if (decrypt_config->subsamples().empty()) {
120 if (!decryptor->Crypt(encrypted_buffer, buffer_size, decrypted_buffer)) {
121 LOG(ERROR) <<
"Error during bulk sample decryption.";
128 const std::vector<SubsampleEntry>& subsamples = decrypt_config->subsamples();
129 const uint8_t* current_ptr = encrypted_buffer;
130 const uint8_t*
const buffer_end = encrypted_buffer + buffer_size;
131 for (
const auto& subsample : subsamples) {
132 if ((current_ptr + subsample.clear_bytes + subsample.cipher_bytes) >
134 LOG(ERROR) <<
"Subsamples overflow sample buffer.";
137 memcpy(decrypted_buffer, current_ptr, subsample.clear_bytes);
138 current_ptr += subsample.clear_bytes;
139 decrypted_buffer += subsample.clear_bytes;
140 if (!decryptor->Crypt(current_ptr, subsample.cipher_bytes,
142 LOG(ERROR) <<
"Error decrypting subsample buffer.";
145 current_ptr += subsample.cipher_bytes;
146 decrypted_buffer += subsample.cipher_bytes;