Chromium Code Reviews| 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/buffers.h" | |
| 13 #include "media/base/data_buffer.h" | |
| 14 #include "media/base/decrypt_config.h" | 12 #include "media/base/decrypt_config.h" |
| 15 | 13 |
| 16 namespace media { | 14 namespace media { |
| 17 | 15 |
| 18 // TODO(xhwang): Get real IV from frames. | 16 // TODO(xhwang): Get real IV from frames. |
| 19 static const char kInitialCounter[] = "0000000000000000"; | 17 static const char kInitialCounter[] = "0000000000000000"; |
| 20 | 18 |
| 21 // Decrypt |input| using |key|. | 19 // Decrypt |input| using |key|. |
| 22 // Return a scoped_refptr to a Buffer object with the decrypted data on success. | 20 // Return a scoped_refptr to a Buffer object with the decrypted data on success. |
|
scherkus (not reviewing)
2012/05/26 01:36:32
docs
also I'd drop the "scoped_reptr" mentions
DaleCurtis
2012/05/29 21:17:01
Done.
| |
| 23 // Return a scoped_refptr to NULL if the data could not be decrypted. | 21 // Return a scoped_refptr to NULL if the data could not be decrypted. |
| 24 static scoped_refptr<Buffer> DecryptData(const Buffer& input, | 22 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, |
| 25 crypto::SymmetricKey* key) { | 23 crypto::SymmetricKey* key) { |
| 26 CHECK(input.GetDataSize()); | 24 CHECK(input.GetDataSize()); |
| 27 CHECK(key); | 25 CHECK(key); |
| 28 | 26 |
| 29 // Initialize encryption data. | 27 // Initialize encryption data. |
| 30 // The IV must be exactly as long as the cipher block size. | 28 // The IV must be exactly as long as the cipher block size. |
| 31 crypto::Encryptor encryptor; | 29 crypto::Encryptor encryptor; |
| 32 if (!encryptor.Init(key, crypto::Encryptor::CBC, kInitialCounter)) { | 30 if (!encryptor.Init(key, crypto::Encryptor::CBC, kInitialCounter)) { |
| 33 DVLOG(1) << "Could not initialize encryptor."; | 31 DVLOG(1) << "Could not initialize encryptor."; |
| 34 return NULL; | 32 return NULL; |
| 35 } | 33 } |
| 36 | 34 |
| 37 std::string decrypted_text; | 35 std::string decrypted_text; |
| 38 base::StringPiece encrypted_text( | 36 base::StringPiece encrypted_text( |
| 39 reinterpret_cast<const char*>(input.GetData()), | 37 reinterpret_cast<const char*>(input.GetData()), |
| 40 input.GetDataSize()); | 38 input.GetDataSize()); |
| 41 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | 39 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
| 42 DVLOG(1) << "Could not decrypt data."; | 40 DVLOG(1) << "Could not decrypt data."; |
| 43 return NULL; | 41 return NULL; |
| 44 } | 42 } |
| 45 | 43 |
| 46 // TODO(xhwang): Implement a string based Buffer implementation to avoid | 44 return DecoderBuffer::CopyFrom( |
| 47 // data copying. | |
| 48 return DataBuffer::CopyFrom( | |
| 49 reinterpret_cast<const uint8*>(decrypted_text.data()), | 45 reinterpret_cast<const uint8*>(decrypted_text.data()), |
| 50 decrypted_text.size()); | 46 decrypted_text.size()); |
| 51 } | 47 } |
| 52 | 48 |
| 53 AesDecryptor::AesDecryptor() {} | 49 AesDecryptor::AesDecryptor() {} |
| 54 | 50 |
| 55 AesDecryptor::~AesDecryptor() { | 51 AesDecryptor::~AesDecryptor() { |
| 56 STLDeleteValues(&key_map_); | 52 STLDeleteValues(&key_map_); |
| 57 } | 53 } |
| 58 | 54 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 74 | 70 |
| 75 base::AutoLock auto_lock(lock_); | 71 base::AutoLock auto_lock(lock_); |
| 76 KeyMap::iterator found = key_map_.find(key_id_string); | 72 KeyMap::iterator found = key_map_.find(key_id_string); |
| 77 if (found != key_map_.end()) { | 73 if (found != key_map_.end()) { |
| 78 delete found->second; | 74 delete found->second; |
| 79 key_map_.erase(found); | 75 key_map_.erase(found); |
| 80 } | 76 } |
| 81 key_map_[key_id_string] = symmetric_key; | 77 key_map_[key_id_string] = symmetric_key; |
| 82 } | 78 } |
| 83 | 79 |
| 84 scoped_refptr<Buffer> AesDecryptor::Decrypt( | 80 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( |
| 85 const scoped_refptr<Buffer>& encrypted) { | 81 const scoped_refptr<DecoderBuffer>& encrypted) { |
| 86 CHECK(encrypted->GetDecryptConfig()); | 82 CHECK(encrypted->GetDecryptConfig()); |
| 87 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 83 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); |
| 88 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); | 84 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); |
| 89 | 85 |
| 90 // TODO(xhwang): Avoid always constructing a string with StringPiece? | 86 // TODO(xhwang): Avoid always constructing a string with StringPiece? |
| 91 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | 87 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); |
| 92 | 88 |
| 93 crypto::SymmetricKey* key = NULL; | 89 crypto::SymmetricKey* key = NULL; |
| 94 { | 90 { |
| 95 base::AutoLock auto_lock(lock_); | 91 base::AutoLock auto_lock(lock_); |
| 96 KeyMap::const_iterator found = key_map_.find(key_id_string); | 92 KeyMap::const_iterator found = key_map_.find(key_id_string); |
| 97 if (found == key_map_.end()) { | 93 if (found == key_map_.end()) { |
| 98 DVLOG(1) << "Could not find a matching key for given key ID."; | 94 DVLOG(1) << "Could not find a matching key for given key ID."; |
| 99 return NULL; | 95 return NULL; |
| 100 } | 96 } |
| 101 key = found->second; | 97 key = found->second; |
| 102 } | 98 } |
| 103 | 99 |
| 104 scoped_refptr<Buffer> decrypted = DecryptData(*encrypted, key); | 100 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key); |
| 105 | 101 |
| 106 if (decrypted) { | 102 if (decrypted) { |
| 107 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 103 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
| 108 decrypted->SetDuration(encrypted->GetDuration()); | 104 decrypted->SetDuration(encrypted->GetDuration()); |
| 109 } | 105 } |
| 110 | 106 |
| 111 return decrypted; | 107 return decrypted; |
| 112 } | 108 } |
| 113 | 109 |
| 114 } // namespace media | 110 } // namespace media |
| OLD | NEW |