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

Unified Diff: crypto/encryptor.cc

Issue 7056026: Implement AES-CTR for NSS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: revised 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
Index: crypto/encryptor.cc
diff --git a/crypto/encryptor.cc b/crypto/encryptor.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0cee9b4dd1a8d2b29c163ff467175df347fea6cf
--- /dev/null
+++ b/crypto/encryptor.cc
@@ -0,0 +1,116 @@
+// 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"
+
+namespace crypto {
+
+namespace {
+
+const size_t kCounterLength = 16u;
+
+inline void Set8(void* memory, size_t offset, uint8 v) {
+ static_cast<uint8*>(memory)[offset] = v;
+}
+
+inline uint8 Get8(const void* memory, size_t offset) {
+ return static_cast<const uint8*>(memory)[offset];
+}
+
+inline void SetBE64(void* memory, uint64 v) {
+ Set8(memory, 0, static_cast<uint8>(v >> 56));
+ Set8(memory, 1, static_cast<uint8>(v >> 48));
+ Set8(memory, 2, static_cast<uint8>(v >> 40));
+ Set8(memory, 3, static_cast<uint8>(v >> 32));
+ Set8(memory, 4, static_cast<uint8>(v >> 24));
+ Set8(memory, 5, static_cast<uint8>(v >> 16));
+ Set8(memory, 6, static_cast<uint8>(v >> 8));
+ Set8(memory, 7, static_cast<uint8>(v >> 0));
+}
+
+inline uint64 GetBE64(const void* memory) {
+ return (static_cast<uint64>(Get8(memory, 0)) << 56)
+ | (static_cast<uint64>(Get8(memory, 1)) << 48)
+ | (static_cast<uint64>(Get8(memory, 2)) << 40)
+ | (static_cast<uint64>(Get8(memory, 3)) << 32)
+ | (static_cast<uint64>(Get8(memory, 4)) << 24)
+ | (static_cast<uint64>(Get8(memory, 5)) << 16)
+ | (static_cast<uint64>(Get8(memory, 6)) << 8)
+ | (static_cast<uint64>(Get8(memory, 7)) << 0);
+}
Ryan Sleevi 2011/06/08 01:29:23 style nit: The | operator should go at the end of
Alpha Left Google 2011/06/13 23:32:45 Done.
+
+} // namespace
+
+/////////////////////////////////////////////////////////////////////////////
+// Encyptor::Counter Implementation.
+Encryptor::Counter::Counter(const std::string& counter) {
+ CHECK_EQ(kCounterLength, counter.length());
+
+ high_num_ = GetBE64(counter.data());
+ low_num_ = GetBE64(counter.data() + sizeof(high_num_));
+}
+
+Encryptor::Counter::~Counter() {
+}
+
+void Encryptor::Counter::Increment() {
+ uint64 old_num = low_num_;
+ ++low_num_;
+
+ // Overflow occurs.
+ if (low_num_ < old_num)
+ ++high_num_;
+}
+
+void Encryptor::Counter::Write(uint8* buf) {
+ SetBE64(buf, high_num_);
+ SetBE64(buf + sizeof(high_num_), low_num_);
+}
+
+const int Encryptor::Counter::GetLengthInBytes() const {
+ return kCounterLength;
+}
+
+/////////////////////////////////////////////////////////////////////////////
+// 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(int plaintext_len,
+ scoped_array<uint8>* mask,
+ int* mask_len) {
+ DCHECK_EQ(CTR, mode_);
+
+ const int kBlockLength = counter_->GetLengthInBytes();
+ int blocks = (plaintext_len + kBlockLength - 1) / kBlockLength;
+
Ryan Sleevi 2011/06/08 01:29:23 nit: Given that it's crypto code, should there be
Alpha Left Google 2011/06/13 23:32:45 I've changed the types here to size_t and check th
+ *mask_len = blocks * kBlockLength;
+ mask->reset(new uint8[*mask_len]);
+
+ uint8* buf = mask->get();
+ for (int i = 0; i < blocks; ++i) {
+ counter_->Write(buf);
+ buf += kBlockLength;
+ counter_->Increment();
+ }
Ryan Sleevi 2011/06/08 01:29:23 DESIGN: This API requires that the entire mask be
Alpha Left Google 2011/06/13 23:32:45 My first attempt was a combined version of Generat
+}
+
+void Encryptor::MaskMessage(const uint8* plaintext, int plaintext_len,
+ const uint8* mask, uint8* ciphertext) const {
+ DCHECK_EQ(CTR, mode_);
+
+ for (int i = 0; i < plaintext_len; ++i)
+ ciphertext[i] = plaintext[i] ^ mask[i];
+}
+
+} // namespace crypto

Powered by Google App Engine
This is Rietveld 408576698