| 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 "net/quic/core/crypto/aes_128_gcm_12_decrypter.h" | 10 #include "net/quic/core/crypto/aes_128_gcm_12_decrypter.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 size_t CryptoSecretBoxer::GetKeySize() { | 43 size_t CryptoSecretBoxer::GetKeySize() { |
| 44 return kKeySize; | 44 return kKeySize; |
| 45 } | 45 } |
| 46 | 46 |
| 47 void CryptoSecretBoxer::SetKeys(const std::vector<string>& keys) { | 47 void CryptoSecretBoxer::SetKeys(const std::vector<string>& keys) { |
| 48 DCHECK(!keys.empty()); | 48 DCHECK(!keys.empty()); |
| 49 std::vector<string> copy = keys; | 49 std::vector<string> copy = keys; |
| 50 for (const string& key : keys) { | 50 for (const string& key : keys) { |
| 51 DCHECK_EQ(kKeySize, key.size()); | 51 DCHECK_EQ(kKeySize, key.size()); |
| 52 } | 52 } |
| 53 base::AutoLock l(lock_); | 53 QuicWriterMutexLock l(&lock_); |
| 54 keys_.swap(copy); | 54 keys_.swap(copy); |
| 55 } | 55 } |
| 56 | 56 |
| 57 string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const { | 57 string CryptoSecretBoxer::Box(QuicRandom* rand, StringPiece plaintext) const { |
| 58 std::unique_ptr<Aes128Gcm12Encrypter> encrypter(new Aes128Gcm12Encrypter()); | 58 std::unique_ptr<Aes128Gcm12Encrypter> encrypter(new Aes128Gcm12Encrypter()); |
| 59 { | 59 { |
| 60 base::AutoLock l(lock_); | 60 QuicReaderMutexLock l(&lock_); |
| 61 DCHECK_EQ(kKeySize, keys_[0].size()); | 61 DCHECK_EQ(kKeySize, keys_[0].size()); |
| 62 if (!encrypter->SetKey(keys_[0])) { | 62 if (!encrypter->SetKey(keys_[0])) { |
| 63 DLOG(DFATAL) << "CryptoSecretBoxer's encrypter->SetKey failed."; | 63 DLOG(DFATAL) << "CryptoSecretBoxer's encrypter->SetKey failed."; |
| 64 return string(); | 64 return string(); |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); | 67 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); |
| 68 | 68 |
| 69 string ret; | 69 string ret; |
| 70 const size_t len = kBoxNonceSize + ciphertext_size; | 70 const size_t len = kBoxNonceSize + ciphertext_size; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 97 QuicPacketNumber packet_number; | 97 QuicPacketNumber packet_number; |
| 98 StringPiece nonce_prefix(nonce.data(), nonce.size() - sizeof(packet_number)); | 98 StringPiece nonce_prefix(nonce.data(), nonce.size() - sizeof(packet_number)); |
| 99 memcpy(&packet_number, nonce.data() + nonce_prefix.size(), | 99 memcpy(&packet_number, nonce.data() + nonce_prefix.size(), |
| 100 sizeof(packet_number)); | 100 sizeof(packet_number)); |
| 101 | 101 |
| 102 std::unique_ptr<Aes128Gcm12Decrypter> decrypter(new Aes128Gcm12Decrypter()); | 102 std::unique_ptr<Aes128Gcm12Decrypter> decrypter(new Aes128Gcm12Decrypter()); |
| 103 char plaintext[kMaxPacketSize]; | 103 char plaintext[kMaxPacketSize]; |
| 104 size_t plaintext_length = 0; | 104 size_t plaintext_length = 0; |
| 105 bool ok = false; | 105 bool ok = false; |
| 106 { | 106 { |
| 107 base::AutoLock l(lock_); | 107 QuicReaderMutexLock l(&lock_); |
| 108 for (const string& key : keys_) { | 108 for (const string& key : keys_) { |
| 109 if (decrypter->SetKey(key)) { | 109 if (decrypter->SetKey(key)) { |
| 110 decrypter->SetNoncePrefix(nonce_prefix); | 110 decrypter->SetNoncePrefix(nonce_prefix); |
| 111 if (decrypter->DecryptPacket( | 111 if (decrypter->DecryptPacket( |
| 112 /*path_id=*/0u, packet_number, | 112 /*path_id=*/0u, packet_number, |
| 113 /*associated data=*/StringPiece(), ciphertext, plaintext, | 113 /*associated data=*/StringPiece(), ciphertext, plaintext, |
| 114 &plaintext_length, kMaxPacketSize)) { | 114 &plaintext_length, kMaxPacketSize)) { |
| 115 ok = true; | 115 ok = true; |
| 116 break; | 116 break; |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 if (!ok) { | 121 if (!ok) { |
| 122 return false; | 122 return false; |
| 123 } | 123 } |
| 124 | 124 |
| 125 out_storage->resize(plaintext_length); | 125 out_storage->resize(plaintext_length); |
| 126 out_storage->assign(plaintext, plaintext_length); | 126 out_storage->assign(plaintext, plaintext_length); |
| 127 out->set(out_storage->data(), plaintext_length); | 127 out->set(out_storage->data(), plaintext_length); |
| 128 return true; | 128 return true; |
| 129 } | 129 } |
| 130 | 130 |
| 131 } // namespace net | 131 } // namespace net |
| OLD | NEW |