summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-08 02:53:09 +0000
committerrsleevi@chromium.org <rsleevi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-08 02:53:09 +0000
commit44a016a843efffc53256c862c792e53d7909c802 (patch)
tree57a31ad697342085bbdb29b2f9bcd3e5f5d58cce
parent98bc449977534905d2254d76686249e18603819e (diff)
downloadchromium_src-44a016a843efffc53256c862c792e53d7909c802.zip
chromium_src-44a016a843efffc53256c862c792e53d7909c802.tar.gz
chromium_src-44a016a843efffc53256c862c792e53d7909c802.tar.bz2
Use base::StringPiece for input parameters in Encryptor, rather than std::string
R=wtc BUG=none TEST=crypto_unittests Review URL: http://codereview.chromium.org/7230037 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91800 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--crypto/encryptor.cc4
-rw-r--r--crypto/encryptor.h19
-rw-r--r--crypto/encryptor_mac.cc14
-rw-r--r--crypto/encryptor_nss.cc16
-rw-r--r--crypto/encryptor_openssl.cc14
-rw-r--r--crypto/encryptor_win.cc10
6 files changed, 48 insertions, 29 deletions
diff --git a/crypto/encryptor.cc b/crypto/encryptor.cc
index 59988f8..53e88f9 100644
--- a/crypto/encryptor.cc
+++ b/crypto/encryptor.cc
@@ -34,7 +34,7 @@ namespace crypto {
/////////////////////////////////////////////////////////////////////////////
// Encyptor::Counter Implementation.
-Encryptor::Counter::Counter(const std::string& counter) {
+Encryptor::Counter::Counter(const base::StringPiece& counter) {
CHECK(sizeof(counter_) == counter.length());
memcpy(&counter_, counter.data(), sizeof(counter_));
@@ -70,7 +70,7 @@ size_t Encryptor::Counter::GetLengthInBytes() const {
/////////////////////////////////////////////////////////////////////////////
// Partial Encryptor Implementation.
-bool Encryptor::SetCounter(const std::string& counter) {
+bool Encryptor::SetCounter(const base::StringPiece& counter) {
if (mode_ != CTR)
return false;
if (counter.length() != 16u)
diff --git a/crypto/encryptor.h b/crypto/encryptor.h
index 712a19c..cfab6f28 100644
--- a/crypto/encryptor.h
+++ b/crypto/encryptor.h
@@ -10,6 +10,7 @@
#include "base/basictypes.h"
#include "base/scoped_ptr.h"
+#include "base/string_piece.h"
#include "build/build_config.h"
#include "crypto/crypto_api.h"
@@ -34,7 +35,7 @@ class CRYPTO_API Encryptor {
// Only 128-bits counter is supported in this class.
class Counter {
public:
- Counter(const std::string& counter);
+ Counter(const base::StringPiece& counter);
~Counter();
// Increment the counter value.
@@ -61,19 +62,19 @@ class CRYPTO_API Encryptor {
// 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);
+ bool Init(SymmetricKey* key, Mode mode, const base::StringPiece& iv);
// Encrypts |plaintext| into |ciphertext|.
- bool Encrypt(const std::string& plaintext, std::string* ciphertext);
+ bool Encrypt(const base::StringPiece& plaintext, std::string* ciphertext);
// Decrypts |ciphertext| into |plaintext|.
- bool Decrypt(const std::string& ciphertext, std::string* plaintext);
+ bool Decrypt(const base::StringPiece& ciphertext, std::string* plaintext);
// Sets the counter value when in CTR mode. Currently only 128-bits
// counter value is supported.
//
// Returns true only if update was successful.
- bool SetCounter(const std::string& counter);
+ bool SetCounter(const base::StringPiece& counter);
// TODO(albertb): Support streaming encryption.
@@ -107,21 +108,21 @@ class CRYPTO_API Encryptor {
#if defined(USE_OPENSSL)
bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt.
- const std::string& input,
+ const base::StringPiece& input,
std::string* output);
std::string iv_;
#elif defined(USE_NSS)
bool Crypt(PK11Context* context,
- const std::string& input,
+ const base::StringPiece& input,
std::string* output);
bool CryptCTR(PK11Context* context,
- const std::string& input,
+ const base::StringPiece& input,
std::string* output);
ScopedPK11Slot slot_;
ScopedSECItem param_;
#elif defined(OS_MACOSX)
bool Crypt(int /*CCOperation*/ op,
- const std::string& input,
+ const base::StringPiece& input,
std::string* output);
std::string iv_;
diff --git a/crypto/encryptor_mac.cc b/crypto/encryptor_mac.cc
index ff6e019..a08d09e 100644
--- a/crypto/encryptor_mac.cc
+++ b/crypto/encryptor_mac.cc
@@ -20,7 +20,9 @@ Encryptor::Encryptor()
Encryptor::~Encryptor() {
}
-bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
+bool Encryptor::Init(SymmetricKey* key,
+ Mode mode,
+ const base::StringPiece& iv) {
DCHECK(key);
DCHECK_EQ(CBC, mode) << "Unsupported mode of operation";
CSSM_DATA raw_key = key->cssm_data();
@@ -33,12 +35,12 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
key_ = key;
mode_ = mode;
- iv_ = iv;
+ iv.CopyToString(&iv_);
return true;
}
bool Encryptor::Crypt(int /*CCOperation*/ op,
- const std::string& input,
+ const base::StringPiece& input,
std::string* output) {
DCHECK(key_);
CSSM_DATA raw_key = key_->cssm_data();
@@ -65,11 +67,13 @@ bool Encryptor::Crypt(int /*CCOperation*/ op,
return true;
}
-bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
+bool Encryptor::Encrypt(const base::StringPiece& plaintext,
+ std::string* ciphertext) {
return Crypt(kCCEncrypt, plaintext, ciphertext);
}
-bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
+bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
+ std::string* plaintext) {
return Crypt(kCCDecrypt, ciphertext, plaintext);
}
diff --git a/crypto/encryptor_nss.cc b/crypto/encryptor_nss.cc
index c53fc10..c4610903 100644
--- a/crypto/encryptor_nss.cc
+++ b/crypto/encryptor_nss.cc
@@ -41,7 +41,9 @@ Encryptor::Encryptor()
Encryptor::~Encryptor() {
}
-bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
+bool Encryptor::Init(SymmetricKey* key,
+ Mode mode,
+ const base::StringPiece& iv) {
DCHECK(key);
DCHECK(CBC == mode || CTR == mode) << "Unsupported mode of operation";
@@ -75,7 +77,8 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
return true;
}
-bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
+bool Encryptor::Encrypt(const base::StringPiece& plaintext,
+ std::string* ciphertext) {
ScopedPK11Context context(PK11_CreateContextBySymKey(GetMechanism(mode_),
CKA_ENCRYPT,
key_->key(),
@@ -89,7 +92,8 @@ bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
return Crypt(context.get(), plaintext, ciphertext);
}
-bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
+bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
+ std::string* plaintext) {
if (ciphertext.empty())
return false;
@@ -105,7 +109,8 @@ bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
return Crypt(context.get(), ciphertext, plaintext);
}
-bool Encryptor::Crypt(PK11Context* context, const std::string& input,
+bool Encryptor::Crypt(PK11Context* context,
+ const base::StringPiece& input,
std::string* output) {
size_t output_len = input.size() + AES_BLOCK_SIZE;
CHECK(output_len > input.size()) << "Output size overflow";
@@ -145,7 +150,8 @@ bool Encryptor::Crypt(PK11Context* context, const std::string& input,
return true;
}
-bool Encryptor::CryptCTR(PK11Context* context, const std::string& input,
+bool Encryptor::CryptCTR(PK11Context* context,
+ const base::StringPiece& input,
std::string* output) {
if (!counter_.get()) {
LOG(ERROR) << "Counter value not set in CTR mode.";
diff --git a/crypto/encryptor_openssl.cc b/crypto/encryptor_openssl.cc
index 7b1e13f..6513181 100644
--- a/crypto/encryptor_openssl.cc
+++ b/crypto/encryptor_openssl.cc
@@ -52,7 +52,9 @@ Encryptor::Encryptor()
Encryptor::~Encryptor() {
}
-bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
+bool Encryptor::Init(SymmetricKey* key,
+ Mode mode,
+ const base::StringPiece& iv) {
DCHECK(key);
DCHECK_EQ(CBC, mode);
@@ -65,20 +67,22 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
key_ = key;
mode_ = mode;
- iv_ = iv;
+ iv.CopyToString(&iv_);
return true;
}
-bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
+bool Encryptor::Encrypt(const base::StringPiece& plaintext,
+ std::string* ciphertext) {
return Crypt(true, plaintext, ciphertext);
}
-bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
+bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
+ std::string* plaintext) {
return Crypt(false, ciphertext, plaintext);
}
bool Encryptor::Crypt(bool do_encrypt,
- const std::string& input,
+ const base::StringPiece& input,
std::string* output) {
DCHECK(key_); // Must call Init() before En/De-crypt.
// Work on the result in a local variable, and then only transfer it to
diff --git a/crypto/encryptor_win.cc b/crypto/encryptor_win.cc
index 8bbd6b8..088c9e5 100644
--- a/crypto/encryptor_win.cc
+++ b/crypto/encryptor_win.cc
@@ -37,7 +37,9 @@ Encryptor::Encryptor()
Encryptor::~Encryptor() {
}
-bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
+bool Encryptor::Init(SymmetricKey* key,
+ Mode mode,
+ const base::StringPiece& iv) {
DCHECK(key);
DCHECK_EQ(CBC, mode) << "Unsupported mode of operation";
@@ -77,7 +79,8 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
return true;
}
-bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
+bool Encryptor::Encrypt(const base::StringPiece& plaintext,
+ std::string* ciphertext) {
DWORD data_len = plaintext.size();
DWORD total_len = data_len + block_size_;
@@ -94,7 +97,8 @@ bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
return true;
}
-bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
+bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
+ std::string* plaintext) {
DWORD data_len = ciphertext.size();
if (data_len == 0)
return false;