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 : key_added_cb_(key_added_cb), | 130 : key_added_cb_(key_added_cb), |
131 key_error_cb_(key_error_cb), | 131 key_error_cb_(key_error_cb), |
132 key_message_cb_(key_message_cb) { | 132 key_message_cb_(key_message_cb) { |
133 } | 133 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 new_video_key_cb_ = new_key_cb; | 221 new_video_key_cb_ = new_key_cb; |
222 break; | 222 break; |
223 default: | 223 default: |
224 NOTREACHED(); | 224 NOTREACHED(); |
225 } | 225 } |
226 } | 226 } |
227 | 227 |
228 void AesDecryptor::Decrypt(StreamType stream_type, | 228 void AesDecryptor::Decrypt(StreamType stream_type, |
229 const scoped_refptr<DecoderBuffer>& encrypted, | 229 const scoped_refptr<DecoderBuffer>& encrypted, |
230 const DecryptCB& decrypt_cb) { | 230 const DecryptCB& decrypt_cb) { |
231 CHECK(encrypted->GetDecryptConfig()); | 231 CHECK(encrypted->decrypt_config()); |
232 | 232 |
233 scoped_refptr<DecoderBuffer> decrypted; | 233 scoped_refptr<DecoderBuffer> decrypted; |
234 // An empty iv string signals that the frame is unencrypted. | 234 // An empty iv string signals that the frame is unencrypted. |
235 if (encrypted->GetDecryptConfig()->iv().empty()) { | 235 if (encrypted->decrypt_config()->iv().empty()) { |
236 int data_offset = encrypted->GetDecryptConfig()->data_offset(); | 236 int data_offset = encrypted->decrypt_config()->data_offset(); |
237 decrypted = DecoderBuffer::CopyFrom(encrypted->GetData() + data_offset, | 237 decrypted = DecoderBuffer::CopyFrom(encrypted->data() + data_offset, |
238 encrypted->GetDataSize() - data_offset); | 238 encrypted->data_size() - data_offset); |
239 } else { | 239 } else { |
240 const std::string& key_id = encrypted->GetDecryptConfig()->key_id(); | 240 const std::string& key_id = encrypted->decrypt_config()->key_id(); |
241 DecryptionKey* key = GetKey(key_id); | 241 DecryptionKey* key = GetKey(key_id); |
242 if (!key) { | 242 if (!key) { |
243 DVLOG(1) << "Could not find a matching key for the given key ID."; | 243 DVLOG(1) << "Could not find a matching key for the given key ID."; |
244 decrypt_cb.Run(kNoKey, NULL); | 244 decrypt_cb.Run(kNoKey, NULL); |
245 return; | 245 return; |
246 } | 246 } |
247 | 247 |
248 crypto::SymmetricKey* decryption_key = key->decryption_key(); | 248 crypto::SymmetricKey* decryption_key = key->decryption_key(); |
249 decrypted = DecryptData(*encrypted.get(), decryption_key); | 249 decrypted = DecryptData(*encrypted.get(), decryption_key); |
250 if (!decrypted.get()) { | 250 if (!decrypted.get()) { |
251 DVLOG(1) << "Decryption failed."; | 251 DVLOG(1) << "Decryption failed."; |
252 decrypt_cb.Run(kError, NULL); | 252 decrypt_cb.Run(kError, NULL); |
253 return; | 253 return; |
254 } | 254 } |
255 } | 255 } |
256 | 256 |
257 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 257 decrypted->set_timestamp(encrypted->timestamp()); |
258 decrypted->SetDuration(encrypted->GetDuration()); | 258 decrypted->set_duration(encrypted->duration()); |
259 decrypt_cb.Run(kSuccess, decrypted); | 259 decrypt_cb.Run(kSuccess, decrypted); |
260 } | 260 } |
261 | 261 |
262 void AesDecryptor::CancelDecrypt(StreamType stream_type) { | 262 void AesDecryptor::CancelDecrypt(StreamType stream_type) { |
263 // Decrypt() calls the DecryptCB synchronously so there's nothing to cancel. | 263 // Decrypt() calls the DecryptCB synchronously so there's nothing to cancel. |
264 } | 264 } |
265 | 265 |
266 void AesDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config, | 266 void AesDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config, |
267 const DecoderInitCB& init_cb) { | 267 const DecoderInitCB& init_cb) { |
268 // AesDecryptor does not support audio decoding. | 268 // AesDecryptor does not support audio decoding. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 bool AesDecryptor::DecryptionKey::Init() { | 325 bool AesDecryptor::DecryptionKey::Init() { |
326 CHECK(!secret_.empty()); | 326 CHECK(!secret_.empty()); |
327 decryption_key_.reset(crypto::SymmetricKey::Import( | 327 decryption_key_.reset(crypto::SymmetricKey::Import( |
328 crypto::SymmetricKey::AES, secret_)); | 328 crypto::SymmetricKey::AES, secret_)); |
329 if (!decryption_key_) | 329 if (!decryption_key_) |
330 return false; | 330 return false; |
331 return true; | 331 return true; |
332 } | 332 } |
333 | 333 |
334 } // namespace media | 334 } // namespace media |
OLD | NEW |