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/crypto/aead_base_decrypter.h" | 5 #include "net/quic/crypto/aead_base_decrypter.h" |
6 | 6 |
7 #include <openssl/err.h> | 7 #include <openssl/err.h> |
8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "net/quic/quic_flags.h" | 11 #include "net/quic/quic_flags.h" |
| 12 #include "net/quic/quic_utils.h" |
12 | 13 |
13 using base::StringPiece; | 14 using base::StringPiece; |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Clear OpenSSL error stack. | 20 // Clear OpenSSL error stack. |
20 void ClearOpenSslErrors() { | 21 void ClearOpenSslErrors() { |
21 while (ERR_get_error()) { | 22 while (ERR_get_error()) { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 size_t* output_length, | 87 size_t* output_length, |
87 size_t max_output_length) { | 88 size_t max_output_length) { |
88 if (ciphertext.length() < auth_tag_size_) { | 89 if (ciphertext.length() < auth_tag_size_) { |
89 return false; | 90 return false; |
90 } | 91 } |
91 | 92 |
92 uint8_t nonce[sizeof(nonce_prefix_) + sizeof(packet_number)]; | 93 uint8_t nonce[sizeof(nonce_prefix_) + sizeof(packet_number)]; |
93 const size_t nonce_size = nonce_prefix_size_ + sizeof(packet_number); | 94 const size_t nonce_size = nonce_prefix_size_ + sizeof(packet_number); |
94 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); | 95 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); |
95 if (FLAGS_quic_include_path_id_in_iv) { | 96 if (FLAGS_quic_include_path_id_in_iv) { |
96 // Setting the nonce below relies on QuicPathId and QuicPacketNumber being | |
97 // specific sizes. | |
98 static_assert(sizeof(path_id) == 1, "Size of QuicPathId changed."); | |
99 static_assert(sizeof(packet_number) == 8, | |
100 "Size of QuicPacketNumber changed."); | |
101 // Use path_id and lower 7 bytes of packet_number as lower 8 bytes of nonce. | |
102 uint64_t path_id_packet_number = | 97 uint64_t path_id_packet_number = |
103 (static_cast<uint64_t>(path_id) << 56) | packet_number; | 98 QuicUtils::PackPathIdAndPacketNumber(path_id, packet_number); |
104 DCHECK(path_id != kDefaultPathId || path_id_packet_number == packet_number); | |
105 memcpy(nonce + nonce_prefix_size_, &path_id_packet_number, | 99 memcpy(nonce + nonce_prefix_size_, &path_id_packet_number, |
106 sizeof(path_id_packet_number)); | 100 sizeof(path_id_packet_number)); |
107 } else { | 101 } else { |
108 memcpy(nonce + nonce_prefix_size_, &packet_number, sizeof(packet_number)); | 102 memcpy(nonce + nonce_prefix_size_, &packet_number, sizeof(packet_number)); |
109 } | 103 } |
110 if (!EVP_AEAD_CTX_open( | 104 if (!EVP_AEAD_CTX_open( |
111 ctx_.get(), reinterpret_cast<uint8_t*>(output), output_length, | 105 ctx_.get(), reinterpret_cast<uint8_t*>(output), output_length, |
112 max_output_length, reinterpret_cast<const uint8_t*>(nonce), | 106 max_output_length, reinterpret_cast<const uint8_t*>(nonce), |
113 nonce_size, reinterpret_cast<const uint8_t*>(ciphertext.data()), | 107 nonce_size, reinterpret_cast<const uint8_t*>(ciphertext.data()), |
114 ciphertext.size(), | 108 ciphertext.size(), |
(...skipping 13 matching lines...) Expand all Loading... |
128 | 122 |
129 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { | 123 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { |
130 if (nonce_prefix_size_ == 0) { | 124 if (nonce_prefix_size_ == 0) { |
131 return StringPiece(); | 125 return StringPiece(); |
132 } | 126 } |
133 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 127 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
134 nonce_prefix_size_); | 128 nonce_prefix_size_); |
135 } | 129 } |
136 | 130 |
137 } // namespace net | 131 } // namespace net |
OLD | NEW |