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

Unified Diff: crypto/encryptor.cc

Issue 7056026: Implement AES-CTR for NSS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: align byets Created 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « crypto/encryptor.h ('k') | crypto/encryptor_nss.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: crypto/encryptor.cc
diff --git a/crypto/encryptor.cc b/crypto/encryptor.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0da18d415ebdfc6c57996070ab519fe2cf13329f
--- /dev/null
+++ b/crypto/encryptor.cc
@@ -0,0 +1,113 @@
+// Copyright (c) 2011 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 "crypto/encryptor.h"
+
+#include "base/logging.h"
+#include "build/build_config.h"
+
+// Include headers to provide bswap for all platforms.
+#if defined(COMPILER_MSVC)
+#include <stdlib.h>
+#define bswap_16(x) _byteswap_ushort(x)
+#define bswap_32(x) _byteswap_ulong(x)
+#define bswap_64(x) _byteswap_uint64(x)
+#elif defined(OS_MACOSX)
+#include <libkern/OSByteOrder.h>
+#define bswap_16(x) OSSwapInt16(x)
+#define bswap_32(x) OSSwapInt32(x)
+#define bswap_64(x) OSSwapInt64(x)
+#else
+#include <byteswap.h>
+#endif
wtc 2011/06/23 18:10:24 To make your code correct for big-endian systems,
+
+namespace crypto {
wtc 2011/06/23 18:10:24 Add a blank line after this line.
+namespace {
+const size_t kCounterLength = 16u;
+} // namespace
+
+/////////////////////////////////////////////////////////////////////////////
+// Encyptor::Counter Implementation.
+Encryptor::Counter::Counter(const std::string& counter)
+ : counter_bits_(kCounterLength * 8) {
+ // Check the endianness at runtime and complain if this is not little
+ // endian.
+ int endian_check = 1;
+ CHECK_EQ(*reinterpret_cast<int8*>(&endian_check), 1)
+ << "This code only runs in little endian system";
+ CHECK_EQ(kCounterLength, counter.length());
+
+ memcpy(counter_.buf, counter.data(), kCounterLength);
+}
+
+Encryptor::Counter::~Counter() {
+}
+
+void Encryptor::Counter::Increment() {
+ uint64* counter = reinterpret_cast<uint64*>(counter_.buf);
+ uint64 low_num = bswap_64(*(counter + 1));
+ uint64 new_low_num = low_num + 1;
+ *(counter + 1) = bswap_64(new_low_num);
+
+ // Overflow occured.
+ if (new_low_num < low_num)
+ *counter = bswap_64(bswap_64(*counter) + 1);
+}
+
+void Encryptor::Counter::Write(void* buf) {
+ uint8* buf_ptr = reinterpret_cast<uint8*>(buf);
+ memcpy(buf_ptr, counter_.buf, kCounterLength);
+}
+
+size_t Encryptor::Counter::GetLengthInBytes() const {
+ return counter_bits_ / 8;
+}
+
+/////////////////////////////////////////////////////////////////////////////
+// Partial Encryptor Implementation.
+
+bool Encryptor::UpdateCounter(const std::string& counter) {
+ if (mode_ != CTR)
+ return false;
+ if (counter.length() != kCounterLength)
+ return false;
+
+ counter_.reset(new Counter(counter));
+ return true;
+}
+
+void Encryptor::GenerateCounterMask(size_t plaintext_len,
+ uint8* mask,
+ size_t* mask_len) {
+ DCHECK_EQ(CTR, mode_);
+ CHECK(mask);
+ CHECK(mask_len);
+
+ const size_t kBlockLength = counter_->GetLengthInBytes();
+ size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength;
+ CHECK(blocks);
+
+ *mask_len = blocks * kBlockLength;
+
+ for (size_t i = 0; i < blocks; ++i) {
+ counter_->Write(mask);
+ mask += kBlockLength;
+ counter_->Increment();
+ }
+}
+
+void Encryptor::MaskMessage(const void* plaintext,
+ size_t plaintext_len,
+ const void* mask,
+ void* ciphertext) const {
+ DCHECK_EQ(CTR, mode_);
+ const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext);
+ const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask);
+ uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext);
+
+ for (size_t i = 0; i < plaintext_len; ++i)
+ ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i];
+}
+
+} // namespace crypto
« no previous file with comments | « crypto/encryptor.h ('k') | crypto/encryptor_nss.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698