Commit 3c053313 authored by rtenneti@chromium.org's avatar rtenneti@chromium.org

CryptoSecretBoxer cleanup changes.

+ Keep the .h in sync with the internal source tree.
+ Moved encrypter_ and decrypter_ back to local variables in the Box and
  Unbox methods.
+ Fixed comments from wtc in https://codereview.chromium.org/213473003/

R=wtc@chromium.org

Review URL: https://codereview.chromium.org/257123002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266830 0039d316-1c4b-4281-b951-d872f2087c98
parent 65cf8d7d
...@@ -5,7 +5,10 @@ ...@@ -5,7 +5,10 @@
#include "net/quic/crypto/crypto_secret_boxer.h" #include "net/quic/crypto/crypto_secret_boxer.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "net/quic/crypto/crypto_protocol.h" #include "net/quic/crypto/crypto_protocol.h"
#include "net/quic/crypto/quic_decrypter.h"
#include "net/quic/crypto/quic_encrypter.h"
#include "net/quic/crypto/quic_random.h" #include "net/quic/crypto/quic_random.h"
using base::StringPiece; using base::StringPiece;
...@@ -29,33 +32,21 @@ static const size_t kKeySize = 16; ...@@ -29,33 +32,21 @@ static const size_t kKeySize = 16;
// It's not terrible, but it's not a "forget about it" margin. // It's not terrible, but it's not a "forget about it" margin.
static const size_t kBoxNonceSize = 12; static const size_t kBoxNonceSize = 12;
CryptoSecretBoxer::CryptoSecretBoxer()
: encrypter_(QuicEncrypter::Create(kAESG)),
decrypter_(QuicDecrypter::Create(kAESG)) {
}
CryptoSecretBoxer::~CryptoSecretBoxer() {}
// static // static
size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; } size_t CryptoSecretBoxer::GetKeySize() { return kKeySize; }
bool CryptoSecretBoxer::SetKey(StringPiece key) { void CryptoSecretBoxer::SetKey(StringPiece key) {
DCHECK_EQ(static_cast<size_t>(kKeySize), key.size()); DCHECK_EQ(kKeySize, key.size());
string key_string = key.as_string(); key_ = key.as_string();
if (!encrypter_->SetKey(key_string)) {
DLOG(DFATAL) << "CryptoSecretBoxer's encrypter_->SetKey failed.";
return false;
}
if (!decrypter_->SetKey(key_string)) {
DLOG(DFATAL) << "CryptoSecretBoxer's decrypter_->SetKey failed.";
return false;
}
return true;
} }
string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const { string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const {
DCHECK_EQ(kKeySize, encrypter_->GetKeySize()); scoped_ptr<QuicEncrypter> encrypter(QuicEncrypter::Create(kAESG));
size_t ciphertext_size = encrypter_->GetCiphertextSize(plaintext.length()); if (!encrypter->SetKey(key_)) {
DLOG(DFATAL) << "CryptoSecretBoxer's encrypter->SetKey failed.";
return string();
}
size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length());
string ret; string ret;
const size_t len = kBoxNonceSize + ciphertext_size; const size_t len = kBoxNonceSize + ciphertext_size;
...@@ -66,9 +57,9 @@ string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const { ...@@ -66,9 +57,9 @@ string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const {
rand->RandBytes(data, kBoxNonceSize); rand->RandBytes(data, kBoxNonceSize);
memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size()); memcpy(data + kBoxNonceSize, plaintext.data(), plaintext.size());
if (!encrypter_->Encrypt(StringPiece(data, kBoxNonceSize), StringPiece(), if (!encrypter->Encrypt(StringPiece(data, kBoxNonceSize), StringPiece(),
plaintext, reinterpret_cast<unsigned char*>( plaintext, reinterpret_cast<unsigned char*>(
data + kBoxNonceSize))) { data + kBoxNonceSize))) {
DLOG(DFATAL) << "CryptoSecretBoxer's Encrypt failed."; DLOG(DFATAL) << "CryptoSecretBoxer's Encrypt failed.";
return string(); return string();
} }
...@@ -91,9 +82,14 @@ bool CryptoSecretBoxer::Unbox(StringPiece ciphertext, ...@@ -91,9 +82,14 @@ bool CryptoSecretBoxer::Unbox(StringPiece ciphertext,
out_storage->resize(len); out_storage->resize(len);
char* data = const_cast<char*>(out_storage->data()); char* data = const_cast<char*>(out_storage->data());
if (!decrypter_->Decrypt(StringPiece(nonce, kBoxNonceSize), StringPiece(), scoped_ptr<QuicDecrypter> decrypter(QuicDecrypter::Create(kAESG));
ciphertext, reinterpret_cast<unsigned char*>(data), if (!decrypter->SetKey(key_)) {
&len)) { DLOG(DFATAL) << "CryptoSecretBoxer's decrypter->SetKey failed.";
return false;
}
if (!decrypter->Decrypt(StringPiece(nonce, kBoxNonceSize), StringPiece(),
ciphertext, reinterpret_cast<unsigned char*>(data),
&len)) {
return false; return false;
} }
......
...@@ -7,11 +7,8 @@ ...@@ -7,11 +7,8 @@
#include <string> #include <string>
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
#include "net/quic/crypto/quic_decrypter.h"
#include "net/quic/crypto/quic_encrypter.h"
namespace net { namespace net {
...@@ -22,17 +19,14 @@ class QuicRandom; ...@@ -22,17 +19,14 @@ class QuicRandom;
// thread-safe. // thread-safe.
class NET_EXPORT_PRIVATE CryptoSecretBoxer { class NET_EXPORT_PRIVATE CryptoSecretBoxer {
public: public:
// Initializes |encrypter_| and |decrypter_| data members. CryptoSecretBoxer() {}
CryptoSecretBoxer();
~CryptoSecretBoxer();
// GetKeySize returns the number of bytes in a key. // GetKeySize returns the number of bytes in a key.
static size_t GetKeySize(); static size_t GetKeySize();
// SetKey sets the key for this object. This must be done before |Box| or // SetKey sets the key for this object. This must be done before |Box| or
// |Unbox| are called. |key| must be |GetKeySize()| bytes long. Returns false // |Unbox| are called. |key| must be |GetKeySize()| bytes long.
// if |encrypter_| or |decrypter_|'s SetKey method fails. void SetKey(base::StringPiece key);
bool SetKey(base::StringPiece key);
// Box encrypts |plaintext| using a random nonce generated from |rand| and // Box encrypts |plaintext| using a random nonce generated from |rand| and
// returns the resulting ciphertext. Since an authenticator and nonce are // returns the resulting ciphertext. Since an authenticator and nonce are
...@@ -49,8 +43,7 @@ class NET_EXPORT_PRIVATE CryptoSecretBoxer { ...@@ -49,8 +43,7 @@ class NET_EXPORT_PRIVATE CryptoSecretBoxer {
base::StringPiece* out) const; base::StringPiece* out) const;
private: private:
scoped_ptr<QuicEncrypter> encrypter_; std::string key_;
scoped_ptr<QuicDecrypter> decrypter_;
DISALLOW_COPY_AND_ASSIGN(CryptoSecretBoxer); DISALLOW_COPY_AND_ASSIGN(CryptoSecretBoxer);
}; };
......
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