OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/crypto/decryptor.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "crypto/encryptor.h" |
| 9 #include "media/base/decoder_buffer.h" |
| 10 #include "media/base/decrypt_config.h" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 Decryptor::Decryptor() {} |
| 15 |
| 16 Decryptor::~Decryptor() {} |
| 17 |
| 18 scoped_refptr<DecoderBuffer> Decryptor::DecryptData(const DecoderBuffer& input, |
| 19 crypto::SymmetricKey* key, |
| 20 int offset) { |
| 21 CHECK(input.GetDataSize()); |
| 22 CHECK(input.GetDecryptConfig()); |
| 23 CHECK(key); |
| 24 |
| 25 // Initialize encryption data. |
| 26 crypto::Encryptor encryptor; |
| 27 if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) { |
| 28 DVLOG(1) << "Could not initialize encryptor."; |
| 29 return NULL; |
| 30 } |
| 31 |
| 32 // Set the counter block. |
| 33 std::string counter_block = |
| 34 Decryptor::GenerateCounterBlock(input.GetDecryptConfig()->iv()); |
| 35 if (counter_block.empty()) { |
| 36 DVLOG(1) << "Could not generate counter block."; |
| 37 return NULL; |
| 38 } |
| 39 if (!encryptor.SetCounter(counter_block)) { |
| 40 DVLOG(1) << "Could not set counter block."; |
| 41 return NULL; |
| 42 } |
| 43 |
| 44 std::string decrypted_text; |
| 45 const char* frame = reinterpret_cast<const char*>(input.GetData() + offset); |
| 46 int frame_size = input.GetDataSize() - offset; |
| 47 base::StringPiece encrypted_text(frame, frame_size); |
| 48 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
| 49 DVLOG(1) << "Could not decrypt data."; |
| 50 return NULL; |
| 51 } |
| 52 |
| 53 // TODO(xhwang): Find a way to avoid this data copy. |
| 54 return DecoderBuffer::CopyFrom( |
| 55 reinterpret_cast<const uint8*>(decrypted_text.data()), |
| 56 decrypted_text.size()); |
| 57 } |
| 58 |
| 59 std::string Decryptor::GenerateCounterBlock(uint64 iv) { |
| 60 char counter_block_data[DecryptConfig::kKeySize]; |
| 61 |
| 62 // Set the IV. |
| 63 memcpy(counter_block_data, &iv, sizeof(iv)); |
| 64 |
| 65 // Set block counter to all 0's. |
| 66 memset(counter_block_data + sizeof(iv), |
| 67 0, |
| 68 DecryptConfig::kKeySize - sizeof(iv)); |
| 69 |
| 70 std::string counter_block(counter_block_data, DecryptConfig::kKeySize); |
| 71 return counter_block; |
| 72 } |
| 73 |
| 74 } // namespace media |
OLD | NEW |