OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/crypto/aes_decryptor.h" | 5 #include "media/crypto/aes_decryptor.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 memcpy(dst, src, subsample.cypher_bytes); | 40 memcpy(dst, src, subsample.cypher_bytes); |
41 src += subsample.cypher_bytes; | 41 src += subsample.cypher_bytes; |
42 dst += subsample.cypher_bytes; | 42 dst += subsample.cypher_bytes; |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 // Decrypts |input| using |key|. Returns a DecoderBuffer with the decrypted | 46 // Decrypts |input| using |key|. Returns a DecoderBuffer with the decrypted |
47 // data if decryption succeeded or NULL if decryption failed. | 47 // data if decryption succeeded or NULL if decryption failed. |
48 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, | 48 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, |
49 crypto::SymmetricKey* key) { | 49 crypto::SymmetricKey* key) { |
50 CHECK(input.GetDataSize()); | 50 CHECK(input.data_size()); |
51 CHECK(input.GetDecryptConfig()); | 51 CHECK(input.decrypt_config()); |
52 CHECK(key); | 52 CHECK(key); |
53 | 53 |
54 crypto::Encryptor encryptor; | 54 crypto::Encryptor encryptor; |
55 if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) { | 55 if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) { |
56 DVLOG(1) << "Could not initialize decryptor."; | 56 DVLOG(1) << "Could not initialize decryptor."; |
57 return NULL; | 57 return NULL; |
58 } | 58 } |
59 | 59 |
60 DCHECK_EQ(input.GetDecryptConfig()->iv().size(), | 60 DCHECK_EQ(input.decrypt_config()->iv().size(), |
61 static_cast<size_t>(DecryptConfig::kDecryptionKeySize)); | 61 static_cast<size_t>(DecryptConfig::kDecryptionKeySize)); |
62 if (!encryptor.SetCounter(input.GetDecryptConfig()->iv())) { | 62 if (!encryptor.SetCounter(input.decrypt_config()->iv())) { |
63 DVLOG(1) << "Could not set counter block."; | 63 DVLOG(1) << "Could not set counter block."; |
64 return NULL; | 64 return NULL; |
65 } | 65 } |
66 | 66 |
67 const int data_offset = input.GetDecryptConfig()->data_offset(); | 67 const int data_offset = input.decrypt_config()->data_offset(); |
68 const char* sample = | 68 const char* sample = |
69 reinterpret_cast<const char*>(input.GetData() + data_offset); | 69 reinterpret_cast<const char*>(input.data() + data_offset); |
70 int sample_size = input.GetDataSize() - data_offset; | 70 int sample_size = input.data_size() - data_offset; |
71 | 71 |
72 if (input.GetDecryptConfig()->subsamples().empty()) { | 72 if (input.decrypt_config()->subsamples().empty()) { |
73 std::string decrypted_text; | 73 std::string decrypted_text; |
74 base::StringPiece encrypted_text(sample, sample_size); | 74 base::StringPiece encrypted_text(sample, sample_size); |
75 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | 75 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
76 DVLOG(1) << "Could not decrypt data."; | 76 DVLOG(1) << "Could not decrypt data."; |
77 return NULL; | 77 return NULL; |
78 } | 78 } |
79 | 79 |
80 // TODO(xhwang): Find a way to avoid this data copy. | 80 // TODO(xhwang): Find a way to avoid this data copy. |
81 return DecoderBuffer::CopyFrom( | 81 return DecoderBuffer::CopyFrom( |
82 reinterpret_cast<const uint8*>(decrypted_text.data()), | 82 reinterpret_cast<const uint8*>(decrypted_text.data()), |
83 decrypted_text.size()); | 83 decrypted_text.size()); |
84 } | 84 } |
85 | 85 |
86 const std::vector<SubsampleEntry>& subsamples = | 86 const std::vector<SubsampleEntry>& subsamples = |
87 input.GetDecryptConfig()->subsamples(); | 87 input.decrypt_config()->subsamples(); |
88 | 88 |
89 int total_clear_size = 0; | 89 int total_clear_size = 0; |
90 int total_encrypted_size = 0; | 90 int total_encrypted_size = 0; |
91 for (size_t i = 0; i < subsamples.size(); i++) { | 91 for (size_t i = 0; i < subsamples.size(); i++) { |
92 total_clear_size += subsamples[i].clear_bytes; | 92 total_clear_size += subsamples[i].clear_bytes; |
93 total_encrypted_size += subsamples[i].cypher_bytes; | 93 total_encrypted_size += subsamples[i].cypher_bytes; |
94 } | 94 } |
95 if (total_clear_size + total_encrypted_size != sample_size) { | 95 if (total_clear_size + total_encrypted_size != sample_size) { |
96 DVLOG(1) << "Subsample sizes do not equal input size"; | 96 DVLOG(1) << "Subsample sizes do not equal input size"; |
97 return NULL; | 97 return NULL; |
(...skipping 15 matching lines...) Expand all Loading... |
113 std::string decrypted_text; | 113 std::string decrypted_text; |
114 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | 114 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
115 DVLOG(1) << "Could not decrypt data."; | 115 DVLOG(1) << "Could not decrypt data."; |
116 return NULL; | 116 return NULL; |
117 } | 117 } |
118 | 118 |
119 scoped_refptr<DecoderBuffer> output = DecoderBuffer::CopyFrom( | 119 scoped_refptr<DecoderBuffer> output = DecoderBuffer::CopyFrom( |
120 reinterpret_cast<const uint8*>(sample), sample_size); | 120 reinterpret_cast<const uint8*>(sample), sample_size); |
121 CopySubsamples(subsamples, kDstContainsClearBytes, | 121 CopySubsamples(subsamples, kDstContainsClearBytes, |
122 reinterpret_cast<const uint8*>(decrypted_text.data()), | 122 reinterpret_cast<const uint8*>(decrypted_text.data()), |
123 output->GetWritableData()); | 123 output->writable_data()); |
124 return output; | 124 return output; |
125 } | 125 } |
126 | 126 |
127 AesDecryptor::AesDecryptor(const KeyAddedCB& key_added_cb, | 127 AesDecryptor::AesDecryptor(const KeyAddedCB& key_added_cb, |
128 const KeyErrorCB& key_error_cb, | 128 const KeyErrorCB& key_error_cb, |
129 const KeyMessageCB& key_message_cb, | 129 const KeyMessageCB& key_message_cb, |
130 const NeedKeyCB& need_key_cb) | 130 const NeedKeyCB& need_key_cb) |
131 : key_added_cb_(key_added_cb), | 131 : key_added_cb_(key_added_cb), |
132 key_error_cb_(key_error_cb), | 132 key_error_cb_(key_error_cb), |
133 key_message_cb_(key_message_cb), | 133 key_message_cb_(key_message_cb), |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 new_video_key_cb_ = new_key_cb; | 225 new_video_key_cb_ = new_key_cb; |
226 break; | 226 break; |
227 default: | 227 default: |
228 NOTREACHED(); | 228 NOTREACHED(); |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
232 void AesDecryptor::Decrypt(StreamType stream_type, | 232 void AesDecryptor::Decrypt(StreamType stream_type, |
233 const scoped_refptr<DecoderBuffer>& encrypted, | 233 const scoped_refptr<DecoderBuffer>& encrypted, |
234 const DecryptCB& decrypt_cb) { | 234 const DecryptCB& decrypt_cb) { |
235 CHECK(encrypted->GetDecryptConfig()); | 235 CHECK(encrypted->decrypt_config()); |
236 | 236 |
237 scoped_refptr<DecoderBuffer> decrypted; | 237 scoped_refptr<DecoderBuffer> decrypted; |
238 // An empty iv string signals that the frame is unencrypted. | 238 // An empty iv string signals that the frame is unencrypted. |
239 if (encrypted->GetDecryptConfig()->iv().empty()) { | 239 if (encrypted->decrypt_config()->iv().empty()) { |
240 int data_offset = encrypted->GetDecryptConfig()->data_offset(); | 240 int data_offset = encrypted->decrypt_config()->data_offset(); |
241 decrypted = DecoderBuffer::CopyFrom(encrypted->GetData() + data_offset, | 241 decrypted = DecoderBuffer::CopyFrom(encrypted->data() + data_offset, |
242 encrypted->GetDataSize() - data_offset); | 242 encrypted->data_size() - data_offset); |
243 } else { | 243 } else { |
244 const std::string& key_id = encrypted->GetDecryptConfig()->key_id(); | 244 const std::string& key_id = encrypted->decrypt_config()->key_id(); |
245 DecryptionKey* key = GetKey(key_id); | 245 DecryptionKey* key = GetKey(key_id); |
246 if (!key) { | 246 if (!key) { |
247 DVLOG(1) << "Could not find a matching key for the given key ID."; | 247 DVLOG(1) << "Could not find a matching key for the given key ID."; |
248 decrypt_cb.Run(kNoKey, NULL); | 248 decrypt_cb.Run(kNoKey, NULL); |
249 return; | 249 return; |
250 } | 250 } |
251 | 251 |
252 crypto::SymmetricKey* decryption_key = key->decryption_key(); | 252 crypto::SymmetricKey* decryption_key = key->decryption_key(); |
253 decrypted = DecryptData(*encrypted.get(), decryption_key); | 253 decrypted = DecryptData(*encrypted.get(), decryption_key); |
254 if (!decrypted.get()) { | 254 if (!decrypted.get()) { |
255 DVLOG(1) << "Decryption failed."; | 255 DVLOG(1) << "Decryption failed."; |
256 decrypt_cb.Run(kError, NULL); | 256 decrypt_cb.Run(kError, NULL); |
257 return; | 257 return; |
258 } | 258 } |
259 } | 259 } |
260 | 260 |
261 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 261 decrypted->set_timestamp(encrypted->timestamp()); |
262 decrypted->SetDuration(encrypted->GetDuration()); | 262 decrypted->set_duration(encrypted->duration()); |
263 decrypt_cb.Run(kSuccess, decrypted); | 263 decrypt_cb.Run(kSuccess, decrypted); |
264 } | 264 } |
265 | 265 |
266 void AesDecryptor::CancelDecrypt(StreamType stream_type) { | 266 void AesDecryptor::CancelDecrypt(StreamType stream_type) { |
267 // Decrypt() calls the DecryptCB synchronously so there's nothing to cancel. | 267 // Decrypt() calls the DecryptCB synchronously so there's nothing to cancel. |
268 } | 268 } |
269 | 269 |
270 void AesDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config, | 270 void AesDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config, |
271 const DecoderInitCB& init_cb) { | 271 const DecoderInitCB& init_cb) { |
272 // AesDecryptor does not support audio decoding. | 272 // AesDecryptor does not support audio decoding. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 bool AesDecryptor::DecryptionKey::Init() { | 329 bool AesDecryptor::DecryptionKey::Init() { |
330 CHECK(!secret_.empty()); | 330 CHECK(!secret_.empty()); |
331 decryption_key_.reset(crypto::SymmetricKey::Import( | 331 decryption_key_.reset(crypto::SymmetricKey::Import( |
332 crypto::SymmetricKey::AES, secret_)); | 332 crypto::SymmetricKey::AES, secret_)); |
333 if (!decryption_key_) | 333 if (!decryption_key_) |
334 return false; | 334 return false; |
335 return true; | 335 return true; |
336 } | 336 } |
337 | 337 |
338 } // namespace media | 338 } // namespace media |
OLD | NEW |