Index: media/crypto/decryptor.cc |
diff --git a/media/crypto/decryptor.cc b/media/crypto/decryptor.cc |
new file mode 100755 |
index 0000000000000000000000000000000000000000..f6a2170e0e1d63da71281edb935a8bb8ec50c122 |
--- /dev/null |
+++ b/media/crypto/decryptor.cc |
@@ -0,0 +1,74 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/crypto/decryptor.h" |
+ |
+#include "base/logging.h" |
+#include "crypto/encryptor.h" |
+#include "media/base/decoder_buffer.h" |
+#include "media/base/decrypt_config.h" |
+ |
+namespace media { |
+ |
+Decryptor::Decryptor() {} |
+ |
+Decryptor::~Decryptor() {} |
+ |
+scoped_refptr<DecoderBuffer> Decryptor::DecryptData(const DecoderBuffer& input, |
+ crypto::SymmetricKey* key, |
+ int offset) { |
+ CHECK(input.GetDataSize()); |
+ CHECK(input.GetDecryptConfig()); |
+ CHECK(key); |
+ |
+ // Initialize encryption data. |
+ crypto::Encryptor encryptor; |
+ if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) { |
+ DVLOG(1) << "Could not initialize encryptor."; |
+ return NULL; |
+ } |
+ |
+ // Set the counter block. |
+ std::string counter_block = |
+ Decryptor::GenerateCounterBlock(input.GetDecryptConfig()->iv()); |
+ if (counter_block.empty()) { |
+ DVLOG(1) << "Could not generate counter block."; |
+ return NULL; |
+ } |
+ if (!encryptor.SetCounter(counter_block)) { |
+ DVLOG(1) << "Could not set counter block."; |
+ return NULL; |
+ } |
+ |
+ std::string decrypted_text; |
+ const char* frame = reinterpret_cast<const char*>(input.GetData() + offset); |
+ int frame_size = input.GetDataSize() - offset; |
+ base::StringPiece encrypted_text(frame, frame_size); |
+ if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
+ DVLOG(1) << "Could not decrypt data."; |
+ return NULL; |
+ } |
+ |
+ // TODO(xhwang): Find a way to avoid this data copy. |
+ return DecoderBuffer::CopyFrom( |
+ reinterpret_cast<const uint8*>(decrypted_text.data()), |
+ decrypted_text.size()); |
+} |
+ |
+std::string Decryptor::GenerateCounterBlock(uint64 iv) { |
+ char counter_block_data[DecryptConfig::kKeySize]; |
+ |
+ // Set the IV. |
+ memcpy(counter_block_data, &iv, sizeof(iv)); |
+ |
+ // Set block counter to all 0's. |
+ memset(counter_block_data + sizeof(iv), |
+ 0, |
+ DecryptConfig::kKeySize - sizeof(iv)); |
+ |
+ std::string counter_block(counter_block_data, DecryptConfig::kKeySize); |
+ return counter_block; |
+} |
+ |
+} // namespace media |