Commit 44a016a8 authored by rsleevi@chromium.org's avatar rsleevi@chromium.org

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
parent 98bc4499
...@@ -34,7 +34,7 @@ namespace crypto { ...@@ -34,7 +34,7 @@ namespace crypto {
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Encyptor::Counter Implementation. // Encyptor::Counter Implementation.
Encryptor::Counter::Counter(const std::string& counter) { Encryptor::Counter::Counter(const base::StringPiece& counter) {
CHECK(sizeof(counter_) == counter.length()); CHECK(sizeof(counter_) == counter.length());
memcpy(&counter_, counter.data(), sizeof(counter_)); memcpy(&counter_, counter.data(), sizeof(counter_));
...@@ -70,7 +70,7 @@ size_t Encryptor::Counter::GetLengthInBytes() const { ...@@ -70,7 +70,7 @@ size_t Encryptor::Counter::GetLengthInBytes() const {
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Partial Encryptor Implementation. // Partial Encryptor Implementation.
bool Encryptor::SetCounter(const std::string& counter) { bool Encryptor::SetCounter(const base::StringPiece& counter) {
if (mode_ != CTR) if (mode_ != CTR)
return false; return false;
if (counter.length() != 16u) if (counter.length() != 16u)
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/scoped_ptr.h" #include "base/scoped_ptr.h"
#include "base/string_piece.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "crypto/crypto_api.h" #include "crypto/crypto_api.h"
...@@ -34,7 +35,7 @@ class CRYPTO_API Encryptor { ...@@ -34,7 +35,7 @@ class CRYPTO_API Encryptor {
// Only 128-bits counter is supported in this class. // Only 128-bits counter is supported in this class.
class Counter { class Counter {
public: public:
Counter(const std::string& counter); Counter(const base::StringPiece& counter);
~Counter(); ~Counter();
// Increment the counter value. // Increment the counter value.
...@@ -61,19 +62,19 @@ class CRYPTO_API Encryptor { ...@@ -61,19 +62,19 @@ class CRYPTO_API Encryptor {
// key or the initialization vector cannot be used. // key or the initialization vector cannot be used.
// //
// When |mode| is CTR then |iv| should be empty. // 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|. // 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|. // 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 // Sets the counter value when in CTR mode. Currently only 128-bits
// counter value is supported. // counter value is supported.
// //
// Returns true only if update was successful. // Returns true only if update was successful.
bool SetCounter(const std::string& counter); bool SetCounter(const base::StringPiece& counter);
// TODO(albertb): Support streaming encryption. // TODO(albertb): Support streaming encryption.
...@@ -107,21 +108,21 @@ class CRYPTO_API Encryptor { ...@@ -107,21 +108,21 @@ class CRYPTO_API Encryptor {
#if defined(USE_OPENSSL) #if defined(USE_OPENSSL)
bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt.
const std::string& input, const base::StringPiece& input,
std::string* output); std::string* output);
std::string iv_; std::string iv_;
#elif defined(USE_NSS) #elif defined(USE_NSS)
bool Crypt(PK11Context* context, bool Crypt(PK11Context* context,
const std::string& input, const base::StringPiece& input,
std::string* output); std::string* output);
bool CryptCTR(PK11Context* context, bool CryptCTR(PK11Context* context,
const std::string& input, const base::StringPiece& input,
std::string* output); std::string* output);
ScopedPK11Slot slot_; ScopedPK11Slot slot_;
ScopedSECItem param_; ScopedSECItem param_;
#elif defined(OS_MACOSX) #elif defined(OS_MACOSX)
bool Crypt(int /*CCOperation*/ op, bool Crypt(int /*CCOperation*/ op,
const std::string& input, const base::StringPiece& input,
std::string* output); std::string* output);
std::string iv_; std::string iv_;
......
...@@ -20,7 +20,9 @@ Encryptor::Encryptor() ...@@ -20,7 +20,9 @@ Encryptor::Encryptor()
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(key);
DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; DCHECK_EQ(CBC, mode) << "Unsupported mode of operation";
CSSM_DATA raw_key = key->cssm_data(); CSSM_DATA raw_key = key->cssm_data();
...@@ -33,12 +35,12 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { ...@@ -33,12 +35,12 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
key_ = key; key_ = key;
mode_ = mode; mode_ = mode;
iv_ = iv; iv.CopyToString(&iv_);
return true; return true;
} }
bool Encryptor::Crypt(int /*CCOperation*/ op, bool Encryptor::Crypt(int /*CCOperation*/ op,
const std::string& input, const base::StringPiece& input,
std::string* output) { std::string* output) {
DCHECK(key_); DCHECK(key_);
CSSM_DATA raw_key = key_->cssm_data(); CSSM_DATA raw_key = key_->cssm_data();
...@@ -65,11 +67,13 @@ bool Encryptor::Crypt(int /*CCOperation*/ op, ...@@ -65,11 +67,13 @@ bool Encryptor::Crypt(int /*CCOperation*/ op,
return true; 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); 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); return Crypt(kCCDecrypt, ciphertext, plaintext);
} }
......
...@@ -41,7 +41,9 @@ Encryptor::Encryptor() ...@@ -41,7 +41,9 @@ Encryptor::Encryptor()
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(key);
DCHECK(CBC == mode || CTR == mode) << "Unsupported mode of operation"; DCHECK(CBC == mode || CTR == mode) << "Unsupported mode of operation";
...@@ -75,7 +77,8 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { ...@@ -75,7 +77,8 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
return true; 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_), ScopedPK11Context context(PK11_CreateContextBySymKey(GetMechanism(mode_),
CKA_ENCRYPT, CKA_ENCRYPT,
key_->key(), key_->key(),
...@@ -89,7 +92,8 @@ bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { ...@@ -89,7 +92,8 @@ bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
return Crypt(context.get(), plaintext, 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()) if (ciphertext.empty())
return false; return false;
...@@ -105,7 +109,8 @@ bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { ...@@ -105,7 +109,8 @@ bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
return Crypt(context.get(), ciphertext, 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) { std::string* output) {
size_t output_len = input.size() + AES_BLOCK_SIZE; size_t output_len = input.size() + AES_BLOCK_SIZE;
CHECK(output_len > input.size()) << "Output size overflow"; CHECK(output_len > input.size()) << "Output size overflow";
...@@ -145,7 +150,8 @@ bool Encryptor::Crypt(PK11Context* context, const std::string& input, ...@@ -145,7 +150,8 @@ bool Encryptor::Crypt(PK11Context* context, const std::string& input,
return true; return true;
} }
bool Encryptor::CryptCTR(PK11Context* context, const std::string& input, bool Encryptor::CryptCTR(PK11Context* context,
const base::StringPiece& input,
std::string* output) { std::string* output) {
if (!counter_.get()) { if (!counter_.get()) {
LOG(ERROR) << "Counter value not set in CTR mode."; LOG(ERROR) << "Counter value not set in CTR mode.";
......
...@@ -52,7 +52,9 @@ Encryptor::Encryptor() ...@@ -52,7 +52,9 @@ Encryptor::Encryptor()
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(key);
DCHECK_EQ(CBC, mode); DCHECK_EQ(CBC, mode);
...@@ -65,20 +67,22 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { ...@@ -65,20 +67,22 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
key_ = key; key_ = key;
mode_ = mode; mode_ = mode;
iv_ = iv; iv.CopyToString(&iv_);
return true; 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); 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); return Crypt(false, ciphertext, plaintext);
} }
bool Encryptor::Crypt(bool do_encrypt, bool Encryptor::Crypt(bool do_encrypt,
const std::string& input, const base::StringPiece& input,
std::string* output) { std::string* output) {
DCHECK(key_); // Must call Init() before En/De-crypt. DCHECK(key_); // Must call Init() before En/De-crypt.
// Work on the result in a local variable, and then only transfer it to // Work on the result in a local variable, and then only transfer it to
......
...@@ -37,7 +37,9 @@ Encryptor::Encryptor() ...@@ -37,7 +37,9 @@ Encryptor::Encryptor()
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(key);
DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; DCHECK_EQ(CBC, mode) << "Unsupported mode of operation";
...@@ -77,7 +79,8 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { ...@@ -77,7 +79,8 @@ bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
return true; 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 data_len = plaintext.size();
DWORD total_len = data_len + block_size_; DWORD total_len = data_len + block_size_;
...@@ -94,7 +97,8 @@ bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { ...@@ -94,7 +97,8 @@ bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
return true; 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(); DWORD data_len = ciphertext.size();
if (data_len == 0) if (data_len == 0)
return false; return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment