Skip to content

Commit

Permalink
Use base::StringPiece for input parameters in Encryptor, rather than …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
rsleevi@chromium.org committed Jul 8, 2011
1 parent 98bc449 commit 44a016a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 29 deletions.
4 changes: 2 additions & 2 deletions crypto/encryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_));
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 10 additions & 9 deletions crypto/encryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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.
Expand All @@ -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.

Expand Down Expand Up @@ -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_;
Expand Down
14 changes: 9 additions & 5 deletions crypto/encryptor_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}

Expand Down
16 changes: 11 additions & 5 deletions crypto/encryptor_nss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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(),
Expand All @@ -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;

Expand All @@ -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";
Expand Down Expand Up @@ -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.";
Expand Down
14 changes: 9 additions & 5 deletions crypto/encryptor_openssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions crypto/encryptor_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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_;

Expand All @@ -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;
Expand Down

0 comments on commit 44a016a

Please sign in to comment.