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