Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: media/crypto/aes_decryptor.cc

Issue 17408005: Refactored DecoderBuffer to use unix_hacker_style naming. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@localrefactor
Patch Set: fixed remaining style issues Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
98 } 98 }
99 99
100 // The encrypted portions of all subsamples must form a contiguous block, 100 // The encrypted portions of all subsamples must form a contiguous block,
101 // such that an encrypted subsample that ends away from a block boundary is 101 // such that an encrypted subsample that ends away from a block boundary is
102 // immediately followed by the start of the next encrypted subsample. We 102 // immediately followed by the start of the next encrypted subsample. We
103 // copy all encrypted subsamples to a contiguous buffer, decrypt them, then 103 // copy all encrypted subsamples to a contiguous buffer, decrypt them, then
104 // copy the decrypted bytes over the encrypted bytes in the output. 104 // copy the decrypted bytes over the encrypted bytes in the output.
105 // TODO(strobe): attempt to reduce number of memory copies 105 // TODO(strobe): attempt to reduce number of memory copies
106 scoped_ptr<uint8[]> encrypted_bytes(new uint8[total_encrypted_size]); 106 scoped_ptr<uint8[]> encrypted_bytes(new uint8[total_encrypted_size]);
107 CopySubsamples(subsamples, kSrcContainsClearBytes, 107 CopySubsamples(subsamples,
108 reinterpret_cast<const uint8*>(sample), encrypted_bytes.get()); 108 kSrcContainsClearBytes,
109 reinterpret_cast<const uint8*>(sample),
110 encrypted_bytes.get());
109 111
110 base::StringPiece encrypted_text( 112 base::StringPiece encrypted_text(
111 reinterpret_cast<const char*>(encrypted_bytes.get()), 113 reinterpret_cast<const char*>(encrypted_bytes.get()),
112 total_encrypted_size); 114 total_encrypted_size);
113 std::string decrypted_text; 115 std::string decrypted_text;
114 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { 116 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) {
115 DVLOG(1) << "Could not decrypt data."; 117 DVLOG(1) << "Could not decrypt data.";
116 return NULL; 118 return NULL;
117 } 119 }
118 120
119 scoped_refptr<DecoderBuffer> output = DecoderBuffer::CopyFrom( 121 scoped_refptr<DecoderBuffer> output = DecoderBuffer::CopyFrom(
120 reinterpret_cast<const uint8*>(sample), sample_size); 122 reinterpret_cast<const uint8*>(sample), sample_size);
121 CopySubsamples(subsamples, kDstContainsClearBytes, 123 CopySubsamples(subsamples,
124 kDstContainsClearBytes,
122 reinterpret_cast<const uint8*>(decrypted_text.data()), 125 reinterpret_cast<const uint8*>(decrypted_text.data()),
123 output->GetWritableData()); 126 output->writable_data());
124 return output; 127 return output;
125 } 128 }
126 129
127 AesDecryptor::AesDecryptor(const KeyAddedCB& key_added_cb, 130 AesDecryptor::AesDecryptor(const KeyAddedCB& key_added_cb,
128 const KeyErrorCB& key_error_cb, 131 const KeyErrorCB& key_error_cb,
129 const KeyMessageCB& key_message_cb, 132 const KeyMessageCB& key_message_cb,
130 const NeedKeyCB& need_key_cb) 133 const NeedKeyCB& need_key_cb)
131 : key_added_cb_(key_added_cb), 134 : key_added_cb_(key_added_cb),
132 key_error_cb_(key_error_cb), 135 key_error_cb_(key_error_cb),
133 key_message_cb_(key_message_cb), 136 key_message_cb_(key_message_cb),
134 need_key_cb_(need_key_cb) { 137 need_key_cb_(need_key_cb) {}
135 }
136 138
137 AesDecryptor::~AesDecryptor() { 139 AesDecryptor::~AesDecryptor() { STLDeleteValues(&key_map_); }
138 STLDeleteValues(&key_map_);
139 }
140 140
141 bool AesDecryptor::GenerateKeyRequest(const std::string& type, 141 bool AesDecryptor::GenerateKeyRequest(const std::string& type,
142 const uint8* init_data, 142 const uint8* init_data,
143 int init_data_length) { 143 int init_data_length) {
144 std::string session_id_string(base::UintToString(next_session_id_++)); 144 std::string session_id_string(base::UintToString(next_session_id_++));
145 145
146 // For now, the AesDecryptor does not care about |type|; 146 // For now, the AesDecryptor does not care about |type|;
147 // just fire the event with the |init_data| as the request. 147 // just fire the event with the |init_data| as the request.
148 std::string message; 148 std::string message;
149 if (init_data && init_data_length) { 149 if (init_data && init_data_length) {
150 message = std::string(reinterpret_cast<const char*>(init_data), 150 message =
151 init_data_length); 151 std::string(reinterpret_cast<const char*>(init_data), init_data_length);
152 } 152 }
153 153
154 key_message_cb_.Run(session_id_string, message, std::string()); 154 key_message_cb_.Run(session_id_string, message, std::string());
155 return true; 155 return true;
156 } 156 }
157 157
158 void AesDecryptor::AddKey(const uint8* key, 158 void AesDecryptor::AddKey(const uint8* key,
159 int key_length, 159 int key_length,
160 const uint8* init_data, 160 const uint8* init_data,
161 int init_data_length, 161 int init_data_length,
162 const std::string& session_id) { 162 const std::string& session_id) {
163 CHECK(key); 163 CHECK(key);
164 CHECK_GT(key_length, 0); 164 CHECK_GT(key_length, 0);
165 165
166 // TODO(xhwang): Add |session_id| check after we figure out how: 166 // TODO(xhwang): Add |session_id| check after we figure out how:
167 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550 167 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550
168 if (key_length != DecryptConfig::kDecryptionKeySize) { 168 if (key_length != DecryptConfig::kDecryptionKeySize) {
169 DVLOG(1) << "Invalid key length: " << key_length; 169 DVLOG(1) << "Invalid key length: " << key_length;
170 key_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0); 170 key_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0);
171 return; 171 return;
172 } 172 }
173 173
174 // TODO(xhwang): Fix the decryptor to accept no |init_data|. See 174 // TODO(xhwang): Fix the decryptor to accept no |init_data|. See
175 // http://crbug.com/123265. Until then, ensure a non-empty value is passed. 175 // http://crbug.com/123265. Until then, ensure a non-empty value is passed.
176 static const uint8 kDummyInitData[1] = { 0 }; 176 static const uint8 kDummyInitData[1] = {0};
177 if (!init_data) { 177 if (!init_data) {
178 init_data = kDummyInitData; 178 init_data = kDummyInitData;
179 init_data_length = arraysize(kDummyInitData); 179 init_data_length = arraysize(kDummyInitData);
180 } 180 }
181 181
182 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec 182 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec
183 // compliant later (http://crbug.com/123262, http://crbug.com/123265). 183 // compliant later (http://crbug.com/123262, http://crbug.com/123265).
184 std::string key_id_string(reinterpret_cast<const char*>(init_data), 184 std::string key_id_string(reinterpret_cast<const char*>(init_data),
185 init_data_length); 185 init_data_length);
186 std::string key_string(reinterpret_cast<const char*>(key) , key_length); 186 std::string key_string(reinterpret_cast<const char*>(key), key_length);
187 scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string)); 187 scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string));
188 if (!decryption_key) { 188 if (!decryption_key) {
189 DVLOG(1) << "Could not create key."; 189 DVLOG(1) << "Could not create key.";
190 key_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0); 190 key_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0);
191 return; 191 return;
192 } 192 }
193 193
194 if (!decryption_key->Init()) { 194 if (!decryption_key->Init()) {
195 DVLOG(1) << "Could not initialize decryption key."; 195 DVLOG(1) << "Could not initialize decryption key.";
196 key_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0); 196 key_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0);
197 return; 197 return;
198 } 198 }
199 199
200 SetKey(key_id_string, decryption_key.Pass()); 200 SetKey(key_id_string, decryption_key.Pass());
201 201
202 if (!new_audio_key_cb_.is_null()) 202 if (!new_audio_key_cb_.is_null())
203 new_audio_key_cb_.Run(); 203 new_audio_key_cb_.Run();
204 204
205 if (!new_video_key_cb_.is_null()) 205 if (!new_video_key_cb_.is_null())
206 new_video_key_cb_.Run(); 206 new_video_key_cb_.Run();
207 207
208 key_added_cb_.Run(session_id); 208 key_added_cb_.Run(session_id);
209 } 209 }
210 210
211 void AesDecryptor::CancelKeyRequest(const std::string& session_id) { 211 void AesDecryptor::CancelKeyRequest(const std::string& session_id) {}
212 }
213 212
214 Decryptor* AesDecryptor::GetDecryptor() { 213 Decryptor* AesDecryptor::GetDecryptor() { return this; }
215 return this;
216 }
217 214
218 void AesDecryptor::RegisterNewKeyCB(StreamType stream_type, 215 void AesDecryptor::RegisterNewKeyCB(StreamType stream_type,
219 const NewKeyCB& new_key_cb) { 216 const NewKeyCB& new_key_cb) {
220 switch (stream_type) { 217 switch (stream_type) {
221 case kAudio: 218 case kAudio:
222 new_audio_key_cb_ = new_key_cb; 219 new_audio_key_cb_ = new_key_cb;
223 break; 220 break;
224 case kVideo: 221 case kVideo:
225 new_video_key_cb_ = new_key_cb; 222 new_video_key_cb_ = new_key_cb;
226 break; 223 break;
227 default: 224 default:
228 NOTREACHED(); 225 NOTREACHED();
229 } 226 }
230 } 227 }
231 228
232 void AesDecryptor::Decrypt(StreamType stream_type, 229 void AesDecryptor::Decrypt(StreamType stream_type,
233 const scoped_refptr<DecoderBuffer>& encrypted, 230 const scoped_refptr<DecoderBuffer>& encrypted,
234 const DecryptCB& decrypt_cb) { 231 const DecryptCB& decrypt_cb) {
235 CHECK(encrypted->GetDecryptConfig()); 232 CHECK(encrypted->decrypt_config());
236 233
237 scoped_refptr<DecoderBuffer> decrypted; 234 scoped_refptr<DecoderBuffer> decrypted;
238 // An empty iv string signals that the frame is unencrypted. 235 // An empty iv string signals that the frame is unencrypted.
239 if (encrypted->GetDecryptConfig()->iv().empty()) { 236 if (encrypted->decrypt_config()->iv().empty()) {
240 int data_offset = encrypted->GetDecryptConfig()->data_offset(); 237 int data_offset = encrypted->decrypt_config()->data_offset();
241 decrypted = DecoderBuffer::CopyFrom(encrypted->GetData() + data_offset, 238 decrypted = DecoderBuffer::CopyFrom(encrypted->data() + data_offset,
242 encrypted->GetDataSize() - data_offset); 239 encrypted->data_size() - data_offset);
243 } else { 240 } else {
244 const std::string& key_id = encrypted->GetDecryptConfig()->key_id(); 241 const std::string& key_id = encrypted->decrypt_config()->key_id();
245 DecryptionKey* key = GetKey(key_id); 242 DecryptionKey* key = GetKey(key_id);
246 if (!key) { 243 if (!key) {
247 DVLOG(1) << "Could not find a matching key for the given key ID."; 244 DVLOG(1) << "Could not find a matching key for the given key ID.";
248 decrypt_cb.Run(kNoKey, NULL); 245 decrypt_cb.Run(kNoKey, NULL);
249 return; 246 return;
250 } 247 }
251 248
252 crypto::SymmetricKey* decryption_key = key->decryption_key(); 249 crypto::SymmetricKey* decryption_key = key->decryption_key();
253 decrypted = DecryptData(*encrypted.get(), decryption_key); 250 decrypted = DecryptData(*encrypted.get(), decryption_key);
254 if (!decrypted.get()) { 251 if (!decrypted.get()) {
255 DVLOG(1) << "Decryption failed."; 252 DVLOG(1) << "Decryption failed.";
256 decrypt_cb.Run(kError, NULL); 253 decrypt_cb.Run(kError, NULL);
257 return; 254 return;
258 } 255 }
259 } 256 }
260 257
261 decrypted->SetTimestamp(encrypted->GetTimestamp()); 258 decrypted->set_timestamp(encrypted->timestamp());
262 decrypted->SetDuration(encrypted->GetDuration()); 259 decrypted->set_duration(encrypted->duration());
263 decrypt_cb.Run(kSuccess, decrypted); 260 decrypt_cb.Run(kSuccess, decrypted);
264 } 261 }
265 262
266 void AesDecryptor::CancelDecrypt(StreamType stream_type) { 263 void AesDecryptor::CancelDecrypt(StreamType stream_type) {
267 // Decrypt() calls the DecryptCB synchronously so there's nothing to cancel. 264 // Decrypt() calls the DecryptCB synchronously so there's nothing to cancel.
268 } 265 }
269 266
270 void AesDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config, 267 void AesDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config,
271 const DecoderInitCB& init_cb) { 268 const DecoderInitCB& init_cb) {
272 // AesDecryptor does not support audio decoding. 269 // AesDecryptor does not support audio decoding.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 const std::string& key_id) const { 311 const std::string& key_id) const {
315 base::AutoLock auto_lock(key_map_lock_); 312 base::AutoLock auto_lock(key_map_lock_);
316 KeyMap::const_iterator found = key_map_.find(key_id); 313 KeyMap::const_iterator found = key_map_.find(key_id);
317 if (found == key_map_.end()) 314 if (found == key_map_.end())
318 return NULL; 315 return NULL;
319 316
320 return found->second; 317 return found->second;
321 } 318 }
322 319
323 AesDecryptor::DecryptionKey::DecryptionKey(const std::string& secret) 320 AesDecryptor::DecryptionKey::DecryptionKey(const std::string& secret)
324 : secret_(secret) { 321 : secret_(secret) {}
325 }
326 322
327 AesDecryptor::DecryptionKey::~DecryptionKey() {} 323 AesDecryptor::DecryptionKey::~DecryptionKey() {}
328 324
329 bool AesDecryptor::DecryptionKey::Init() { 325 bool AesDecryptor::DecryptionKey::Init() {
330 CHECK(!secret_.empty()); 326 CHECK(!secret_.empty());
331 decryption_key_.reset(crypto::SymmetricKey::Import( 327 decryption_key_.reset(
332 crypto::SymmetricKey::AES, secret_)); 328 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, secret_));
333 if (!decryption_key_) 329 if (!decryption_key_)
334 return false; 330 return false;
335 return true; 331 return true;
336 } 332 }
337 333
338 } // namespace media 334 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698