diff options
author | rtenneti@chromium.org <rtenneti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-29 10:24:02 +0000 |
---|---|---|
committer | rtenneti@chromium.org <rtenneti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-29 10:24:02 +0000 |
commit | 3c05331313cf4d8569b656ced2e09ed2ed3cdbb4 (patch) | |
tree | ac5d7451dde2df26d23b06d79b077169f35240b9 | |
parent | 65cf8d7d110d827e5079a647dbf520e755252324 (diff) | |
download | chromium_src-3c05331313cf4d8569b656ced2e09ed2ed3cdbb4.zip chromium_src-3c05331313cf4d8569b656ced2e09ed2ed3cdbb4.tar.gz chromium_src-3c05331313cf4d8569b656ced2e09ed2ed3cdbb4.tar.bz2 |
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
-rw-r--r-- | net/quic/crypto/crypto_secret_boxer.cc | 50 | ||||
-rw-r--r-- | net/quic/crypto/crypto_secret_boxer.h | 15 |
2 files changed, 27 insertions, 38 deletions
diff --git a/net/quic/crypto/crypto_secret_boxer.cc b/net/quic/crypto/crypto_secret_boxer.cc index 365b5d3..5d8a11b 100644 --- a/net/quic/crypto/crypto_secret_boxer.cc +++ b/net/quic/crypto/crypto_secret_boxer.cc @@ -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,9 +57,9 @@ 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(), - plaintext, reinterpret_cast<unsigned char*>( - data + kBoxNonceSize))) { + if (!encrypter->Encrypt(StringPiece(data, kBoxNonceSize), StringPiece(), + plaintext, reinterpret_cast<unsigned char*>( + data + kBoxNonceSize))) { DLOG(DFATAL) << "CryptoSecretBoxer's Encrypt failed."; return string(); } @@ -91,9 +82,14 @@ bool CryptoSecretBoxer::Unbox(StringPiece ciphertext, out_storage->resize(len); char* data = const_cast<char*>(out_storage->data()); - if (!decrypter_->Decrypt(StringPiece(nonce, kBoxNonceSize), StringPiece(), - ciphertext, reinterpret_cast<unsigned char*>(data), - &len)) { + 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; } diff --git a/net/quic/crypto/crypto_secret_boxer.h b/net/quic/crypto/crypto_secret_boxer.h index 6deb771..38b8fb3 100644 --- a/net/quic/crypto/crypto_secret_boxer.h +++ b/net/quic/crypto/crypto_secret_boxer.h @@ -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); }; |