| 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/aead_base_encrypter.h" | 5 #include "net/quic/core/crypto/aead_base_encrypter.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "net/quic/core/quic_utils.h" | 9 #include "net/quic/core/quic_utils.h" |
| 10 #include "net/quic/platform/api/quic_aligned.h" | 10 #include "net/quic/platform/api/quic_aligned.h" |
| 11 #include "net/quic/platform/api/quic_logging.h" | 11 #include "net/quic/platform/api/quic_logging.h" |
| 12 #include "third_party/boringssl/src/include/openssl/err.h" | 12 #include "third_party/boringssl/src/include/openssl/err.h" |
| 13 #include "third_party/boringssl/src/include/openssl/evp.h" | 13 #include "third_party/boringssl/src/include/openssl/evp.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // The maximum size in bytes of the nonce, including 8 bytes of sequence number. | 19 // The maximum size in bytes of the nonce, including 8 bytes of sequence number. |
| 20 // ChaCha20 uses only the 8 byte sequence number and AES-GCM uses 12 bytes. | 20 // ChaCha20 uses only the 8 byte sequence number and AES-GCM uses 12 bytes. |
| 21 const size_t kMaxNonceSize = 12; | 21 const size_t kMaxNonceSize = 12; |
| 22 | 22 |
| 23 // In debug builds only, log OpenSSL error stack. Then clear OpenSSL error | 23 // In debug builds only, log OpenSSL error stack. Then clear OpenSSL error |
| 24 // stack. | 24 // stack. |
| 25 void DLogOpenSslErrors() { | 25 void DLogOpenSslErrors2() { |
| 26 #ifdef NDEBUG | 26 #ifdef NDEBUG |
| 27 while (ERR_get_error()) { | 27 while (ERR_get_error()) { |
| 28 } | 28 } |
| 29 #else | 29 #else |
| 30 while (unsigned long error = ERR_get_error()) { | 30 while (unsigned long error = ERR_get_error()) { |
| 31 char buf[120]; | 31 char buf[120]; |
| 32 ERR_error_string_n(error, buf, arraysize(buf)); | 32 ERR_error_string_n(error, buf, arraysize(buf)); |
| 33 QUIC_DLOG(ERROR) << "OpenSSL error: " << buf; | 33 QUIC_DLOG(ERROR) << "OpenSSL error: " << buf; |
| 34 } | 34 } |
| 35 #endif | 35 #endif |
| (...skipping 20 matching lines...) Expand all Loading... |
| 56 DCHECK_EQ(key.size(), key_size_); | 56 DCHECK_EQ(key.size(), key_size_); |
| 57 if (key.size() != key_size_) { | 57 if (key.size() != key_size_) { |
| 58 return false; | 58 return false; |
| 59 } | 59 } |
| 60 memcpy(key_, key.data(), key.size()); | 60 memcpy(key_, key.data(), key.size()); |
| 61 | 61 |
| 62 EVP_AEAD_CTX_cleanup(ctx_.get()); | 62 EVP_AEAD_CTX_cleanup(ctx_.get()); |
| 63 | 63 |
| 64 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, auth_tag_size_, | 64 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, auth_tag_size_, |
| 65 nullptr)) { | 65 nullptr)) { |
| 66 DLogOpenSslErrors(); | 66 DLogOpenSslErrors2(); |
| 67 return false; | 67 return false; |
| 68 } | 68 } |
| 69 | 69 |
| 70 return true; | 70 return true; |
| 71 } | 71 } |
| 72 | 72 |
| 73 bool AeadBaseEncrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) { | 73 bool AeadBaseEncrypter::SetNoncePrefix(QuicStringPiece nonce_prefix) { |
| 74 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); | 74 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); |
| 75 if (nonce_prefix.size() != nonce_prefix_size_) { | 75 if (nonce_prefix.size() != nonce_prefix_size_) { |
| 76 return false; | 76 return false; |
| 77 } | 77 } |
| 78 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); | 78 memcpy(nonce_prefix_, nonce_prefix.data(), nonce_prefix.size()); |
| 79 return true; | 79 return true; |
| 80 } | 80 } |
| 81 | 81 |
| 82 bool AeadBaseEncrypter::Encrypt(QuicStringPiece nonce, | 82 bool AeadBaseEncrypter::Encrypt(QuicStringPiece nonce, |
| 83 QuicStringPiece associated_data, | 83 QuicStringPiece associated_data, |
| 84 QuicStringPiece plaintext, | 84 QuicStringPiece plaintext, |
| 85 unsigned char* output) { | 85 unsigned char* output) { |
| 86 DCHECK_EQ(nonce.size(), nonce_prefix_size_ + sizeof(QuicPacketNumber)); | 86 DCHECK_EQ(nonce.size(), nonce_prefix_size_ + sizeof(QuicPacketNumber)); |
| 87 | 87 |
| 88 size_t ciphertext_len; | 88 size_t ciphertext_len; |
| 89 if (!EVP_AEAD_CTX_seal( | 89 if (!EVP_AEAD_CTX_seal( |
| 90 ctx_.get(), output, &ciphertext_len, | 90 ctx_.get(), output, &ciphertext_len, |
| 91 plaintext.size() + auth_tag_size_, | 91 plaintext.size() + auth_tag_size_, |
| 92 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), | 92 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), |
| 93 reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size(), | 93 reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size(), |
| 94 reinterpret_cast<const uint8_t*>(associated_data.data()), | 94 reinterpret_cast<const uint8_t*>(associated_data.data()), |
| 95 associated_data.size())) { | 95 associated_data.size())) { |
| 96 DLogOpenSslErrors(); | 96 DLogOpenSslErrors2(); |
| 97 return false; | 97 return false; |
| 98 } | 98 } |
| 99 | 99 |
| 100 return true; | 100 return true; |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool AeadBaseEncrypter::EncryptPacket(QuicVersion /*version*/, | 103 bool AeadBaseEncrypter::EncryptPacket(QuicVersion /*version*/, |
| 104 QuicPacketNumber packet_number, | 104 QuicPacketNumber packet_number, |
| 105 QuicStringPiece associated_data, | 105 QuicStringPiece associated_data, |
| 106 QuicStringPiece plaintext, | 106 QuicStringPiece plaintext, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 149 |
| 150 QuicStringPiece AeadBaseEncrypter::GetNoncePrefix() const { | 150 QuicStringPiece AeadBaseEncrypter::GetNoncePrefix() const { |
| 151 if (nonce_prefix_size_ == 0) { | 151 if (nonce_prefix_size_ == 0) { |
| 152 return QuicStringPiece(); | 152 return QuicStringPiece(); |
| 153 } | 153 } |
| 154 return QuicStringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 154 return QuicStringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
| 155 nonce_prefix_size_); | 155 nonce_prefix_size_); |
| 156 } | 156 } |
| 157 | 157 |
| 158 } // namespace net | 158 } // namespace net |
| OLD | NEW |