| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "crypto/encryptor.h" | 10 #include "crypto/encryptor.h" |
| 11 #include "crypto/symmetric_key.h" | 11 #include "crypto/symmetric_key.h" |
| 12 #include "media/base/decoder_buffer.h" | 12 #include "media/base/decoder_buffer.h" |
| 13 #include "media/base/decrypt_config.h" | 13 #include "media/base/decrypt_config.h" |
| 14 | 14 |
| 15 namespace media { | 15 namespace media { |
| 16 | 16 |
| 17 // TODO(xhwang): Get real IV from frames. | |
| 18 static const char kInitialCounter[] = "0000000000000000"; | |
| 19 | |
| 20 // Decrypt |input| using |key|. | |
| 21 // Return a DecoderBuffer with the decrypted data if decryption succeeded. | |
| 22 // Return NULL if decryption failed. | |
| 23 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, | |
| 24 crypto::SymmetricKey* key) { | |
| 25 CHECK(input.GetDataSize()); | |
| 26 CHECK(key); | |
| 27 | |
| 28 // Initialize encryption data. | |
| 29 // The IV must be exactly as long as the cipher block size. | |
| 30 crypto::Encryptor encryptor; | |
| 31 if (!encryptor.Init(key, crypto::Encryptor::CBC, kInitialCounter)) { | |
| 32 DVLOG(1) << "Could not initialize encryptor."; | |
| 33 return NULL; | |
| 34 } | |
| 35 | |
| 36 std::string decrypted_text; | |
| 37 base::StringPiece encrypted_text( | |
| 38 reinterpret_cast<const char*>(input.GetData()), | |
| 39 input.GetDataSize()); | |
| 40 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | |
| 41 DVLOG(1) << "Could not decrypt data."; | |
| 42 return NULL; | |
| 43 } | |
| 44 | |
| 45 // TODO(xhwang): Find a way to avoid this data copy. | |
| 46 return DecoderBuffer::CopyFrom( | |
| 47 reinterpret_cast<const uint8*>(decrypted_text.data()), | |
| 48 decrypted_text.size()); | |
| 49 } | |
| 50 | |
| 51 AesDecryptor::AesDecryptor() {} | 17 AesDecryptor::AesDecryptor() {} |
| 52 | 18 |
| 53 AesDecryptor::~AesDecryptor() { | 19 AesDecryptor::~AesDecryptor() { |
| 54 STLDeleteValues(&key_map_); | 20 STLDeleteValues(&key_map_); |
| 55 } | 21 } |
| 56 | 22 |
| 57 void AesDecryptor::AddKey(const uint8* key_id, int key_id_size, | 23 void AesDecryptor::AddKey(const uint8* key_id, int key_id_size, |
| 58 const uint8* key, int key_size) { | 24 const uint8* key, int key_size) { |
| 59 CHECK(key_id && key); | 25 CHECK(key_id && key); |
| 60 CHECK_GT(key_id_size, 0); | 26 CHECK_GT(key_id_size, 0); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 { | 58 { |
| 93 base::AutoLock auto_lock(lock_); | 59 base::AutoLock auto_lock(lock_); |
| 94 KeyMap::const_iterator found = key_map_.find(key_id_string); | 60 KeyMap::const_iterator found = key_map_.find(key_id_string); |
| 95 if (found == key_map_.end()) { | 61 if (found == key_map_.end()) { |
| 96 DVLOG(1) << "Could not find a matching key for given key ID."; | 62 DVLOG(1) << "Could not find a matching key for given key ID."; |
| 97 return NULL; | 63 return NULL; |
| 98 } | 64 } |
| 99 key = found->second; | 65 key = found->second; |
| 100 } | 66 } |
| 101 | 67 |
| 102 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key); | 68 scoped_refptr<DecoderBuffer> decrypted = |
| 69 Decryptor::DecryptData(*encrypted, key, 0); |
| 103 | 70 |
| 104 if (decrypted) { | 71 if (decrypted) { |
| 105 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 72 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
| 106 decrypted->SetDuration(encrypted->GetDuration()); | 73 decrypted->SetDuration(encrypted->GetDuration()); |
| 107 } | 74 } |
| 108 | 75 |
| 109 return decrypted; | 76 return decrypted; |
| 110 } | 77 } |
| 111 | 78 |
| 112 } // namespace media | 79 } // namespace media |
| OLD | NEW |