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

Unified Diff: crypto/encryptor.h

Issue 7056026: Implement AES-CTR for NSS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: done 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/crypto.gyp ('k') | crypto/encryptor.cc » ('j') | crypto/encryptor.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: crypto/encryptor.h
diff --git a/crypto/encryptor.h b/crypto/encryptor.h
index d8250f6bdf48f65ab10f9fb64d400444d1863a9d..f88e8cf226e6f536db2600362988eb31c2b1b70f 100644
--- a/crypto/encryptor.h
+++ b/crypto/encryptor.h
@@ -8,8 +8,9 @@
#include <string>
+#include "base/basictypes.h"
+#include "base/scoped_ptr.h"
#include "build/build_config.h"
-
#if defined(USE_NSS)
#include "crypto/scoped_nss_types.h"
#elif defined(OS_WIN)
@@ -23,13 +24,39 @@ class SymmetricKey;
class Encryptor {
public:
enum Mode {
- CBC
+ CBC,
+ CTR,
+ };
+
+ // This class implements a 128-bits counter to be used in AES-CTR encryption.
+ // Only 128-bits counter is supported in this class.
+ class Counter {
+ public:
+ Counter(const std::string& counter);
+ ~Counter();
+
+ // Increment the counter value.
+ void Increment();
+
+ // Write the content of the counter to |buf|. |buf| should have enough
+ // space for |GetLengthInBytes()|.
+ void Write(void* buf);
+
+ // Return the length of this counter.
+ size_t GetLengthInBytes() const;
+
+ private:
+ uint64 high_num_;
+ uint64 low_num_;
};
+
Encryptor();
virtual ~Encryptor();
// Initializes the encryptor using |key| and |iv|. Returns false if either the
// key or the initialization vector cannot be used.
+ //
+ // When |mode| is CTR then |iv| should be empty.
bool Init(SymmetricKey* key, Mode mode, const std::string& iv);
// Encrypts |plaintext| into |ciphertext|.
@@ -38,11 +65,36 @@ class Encryptor {
// Decrypts |ciphertext| into |plaintext|.
bool Decrypt(const std::string& ciphertext, std::string* plaintext);
+ // Update the counter value when in CTR mode. Currently only 128-bits
+ // counter value is supported.
+ //
+ // Return true only if update was successful.
+ bool UpdateCounter(const std::string& counter);
+
// TODO(albertb): Support streaming encryption.
private:
+ // Generate a mask using |counter_| to be used for encryption in CTR mode.
+ // Resulting mask will be written to |mask| with |mask_len| bytes.
+ //
+ // The generated mask will always have at least |plaintext_len| bytes and
+ // will be a multiple of the counter length.
+ //
+ // This method is used only in CTR mode.
+ void GenerateCounterMask(size_t plaintext_len,
+ scoped_array<uint8>* mask,
+ size_t* mask_len);
+
+ // Mask the |plaintext| message using |mask|. The output will be written to
+ // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes.
+ void MaskMessage(const void* plaintext,
+ size_t plaintext_len,
+ const void* mask,
+ void* ciphertext) const;
+
SymmetricKey* key_;
Mode mode_;
+ scoped_ptr<Counter> counter_;
#if defined(USE_OPENSSL)
bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt.
@@ -50,6 +102,12 @@ class Encryptor {
std::string* output);
std::string iv_;
#elif defined(USE_NSS)
+ bool Crypt(PK11Context* context,
+ const std::string& input,
+ std::string* output);
+ bool CryptCTR(PK11Context* context,
+ const std::string& input,
+ std::string* output);
ScopedPK11Slot slot_;
ScopedSECItem param_;
#elif defined(OS_MACOSX)
« no previous file with comments | « crypto/crypto.gyp ('k') | crypto/encryptor.cc » ('j') | crypto/encryptor.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698