| OLD | NEW | 
|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "net/quic/core/crypto/crypto_secret_boxer.h" | 5 #include "net/quic/core/crypto/crypto_secret_boxer.h" | 
| 6 | 6 | 
| 7 #include <memory> | 7 #include <memory> | 
| 8 | 8 | 
| 9 #include "base/logging.h" | 9 #include "base/logging.h" | 
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 31 | 31 | 
| 32 CryptoSecretBoxer::CryptoSecretBoxer() {} | 32 CryptoSecretBoxer::CryptoSecretBoxer() {} | 
| 33 | 33 | 
| 34 CryptoSecretBoxer::~CryptoSecretBoxer() {} | 34 CryptoSecretBoxer::~CryptoSecretBoxer() {} | 
| 35 | 35 | 
| 36 // static | 36 // static | 
| 37 size_t CryptoSecretBoxer::GetKeySize() { | 37 size_t CryptoSecretBoxer::GetKeySize() { | 
| 38   return kKeySize; | 38   return kKeySize; | 
| 39 } | 39 } | 
| 40 | 40 | 
| 41 // kAEAD is the AEAD used for boxing: AES-128-GCM-SIV. | 41 // kAEAD_fn is the AEAD used for boxing: AES-128-GCM-SIV. | 
| 42 static const EVP_AEAD* (*const kAEAD)() = EVP_aead_aes_128_gcm_siv; | 42 static const EVP_AEAD* (*const kAEAD_fn)() = EVP_aead_aes_128_gcm_siv; | 
| 43 | 43 | 
| 44 void CryptoSecretBoxer::SetKeys(const std::vector<string>& keys) { | 44 void CryptoSecretBoxer::SetKeys(const std::vector<string>& keys) { | 
| 45   DCHECK(!keys.empty()); | 45   DCHECK(!keys.empty()); | 
| 46   const EVP_AEAD* const aead = kAEAD(); | 46   const EVP_AEAD* const aead = kAEAD_fn(); | 
| 47   std::unique_ptr<State> new_state(new State); | 47   std::unique_ptr<State> new_state(new State); | 
| 48 | 48 | 
| 49   for (const string& key : keys) { | 49   for (const string& key : keys) { | 
| 50     DCHECK_EQ(kKeySize, key.size()); | 50     DCHECK_EQ(kKeySize, key.size()); | 
| 51     bssl::UniquePtr<EVP_AEAD_CTX> ctx( | 51     bssl::UniquePtr<EVP_AEAD_CTX> ctx( | 
| 52         EVP_AEAD_CTX_new(aead, reinterpret_cast<const uint8_t*>(key.data()), | 52         EVP_AEAD_CTX_new(aead, reinterpret_cast<const uint8_t*>(key.data()), | 
| 53                          key.size(), EVP_AEAD_DEFAULT_TAG_LENGTH)); | 53                          key.size(), EVP_AEAD_DEFAULT_TAG_LENGTH)); | 
| 54     if (!ctx) { | 54     if (!ctx) { | 
| 55       LOG(DFATAL) << "EVP_AEAD_CTX_init failed"; | 55       LOG(DFATAL) << "EVP_AEAD_CTX_init failed"; | 
| 56       return; | 56       return; | 
| 57     } | 57     } | 
| 58 | 58 | 
| 59     new_state->ctxs.push_back(std::move(ctx)); | 59     new_state->ctxs.push_back(std::move(ctx)); | 
| 60   } | 60   } | 
| 61 | 61 | 
| 62   QuicWriterMutexLock l(&lock_); | 62   QuicWriterMutexLock l(&lock_); | 
| 63   state_ = std::move(new_state); | 63   state_ = std::move(new_state); | 
| 64 } | 64 } | 
| 65 | 65 | 
| 66 string CryptoSecretBoxer::Box(QuicRandom* rand, | 66 string CryptoSecretBoxer::Box(QuicRandom* rand, | 
| 67                               QuicStringPiece plaintext) const { | 67                               QuicStringPiece plaintext) const { | 
| 68   // The box is formatted as: | 68   // The box is formatted as: | 
| 69   //   12 bytes of random nonce | 69   //   12 bytes of random nonce | 
| 70   //   n bytes of ciphertext | 70   //   n bytes of ciphertext | 
| 71   //   16 bytes of authenticator | 71   //   16 bytes of authenticator | 
| 72   size_t out_len = | 72   size_t out_len = | 
| 73       kBoxNonceSize + plaintext.size() + EVP_AEAD_max_overhead(kAEAD()); | 73       kBoxNonceSize + plaintext.size() + EVP_AEAD_max_overhead(kAEAD_fn()); | 
| 74 | 74 | 
| 75   string ret; | 75   string ret; | 
| 76   uint8_t* out = reinterpret_cast<uint8_t*>(base::WriteInto(&ret, out_len + 1)); | 76   uint8_t* out = reinterpret_cast<uint8_t*>(base::WriteInto(&ret, out_len + 1)); | 
| 77 | 77 | 
| 78   // Write kBoxNonceSize bytes of random nonce to the beginning of the output | 78   // Write kBoxNonceSize bytes of random nonce to the beginning of the output | 
| 79   // buffer. | 79   // buffer. | 
| 80   rand->RandBytes(out, kBoxNonceSize); | 80   rand->RandBytes(out, kBoxNonceSize); | 
| 81   const uint8_t* const nonce = out; | 81   const uint8_t* const nonce = out; | 
| 82   out += kBoxNonceSize; | 82   out += kBoxNonceSize; | 
| 83   out_len -= kBoxNonceSize; | 83   out_len -= kBoxNonceSize; | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 122                           nullptr, 0)) { | 122                           nullptr, 0)) { | 
| 123       *out = QuicStringPiece(out_storage->data(), bytes_written); | 123       *out = QuicStringPiece(out_storage->data(), bytes_written); | 
| 124       return true; | 124       return true; | 
| 125     } | 125     } | 
| 126   } | 126   } | 
| 127 | 127 | 
| 128   return false; | 128   return false; | 
| 129 } | 129 } | 
| 130 | 130 | 
| 131 }  // namespace net | 131 }  // namespace net | 
| OLD | NEW | 
|---|