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_encrypter.h" | 5 #include "net/quic/crypto/aead_base_encrypter.h" |
6 | 6 |
7 #include <openssl/err.h> | 7 #include <openssl/err.h> |
8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "net/quic/quic_flags.h" | 12 #include "net/quic/quic_flags.h" |
| 13 #include "net/quic/quic_utils.h" |
13 | 14 |
14 using base::StringPiece; | 15 using base::StringPiece; |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 // The maximum size in bytes of the nonce, including 8 bytes of sequence number. | 21 // The maximum size in bytes of the nonce, including 8 bytes of sequence number. |
21 // ChaCha20 uses only the 8 byte sequence number and AES-GCM uses 12 bytes. | 22 // ChaCha20 uses only the 8 byte sequence number and AES-GCM uses 12 bytes. |
22 const size_t kMaxNonceSize = 12; | 23 const size_t kMaxNonceSize = 12; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); | 114 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); |
114 if (max_output_length < ciphertext_size) { | 115 if (max_output_length < ciphertext_size) { |
115 return false; | 116 return false; |
116 } | 117 } |
117 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the | 118 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the |
118 // same packet number twice. | 119 // same packet number twice. |
119 const size_t nonce_size = nonce_prefix_size_ + sizeof(packet_number); | 120 const size_t nonce_size = nonce_prefix_size_ + sizeof(packet_number); |
120 ALIGNAS(4) char nonce_buffer[kMaxNonceSize]; | 121 ALIGNAS(4) char nonce_buffer[kMaxNonceSize]; |
121 memcpy(nonce_buffer, nonce_prefix_, nonce_prefix_size_); | 122 memcpy(nonce_buffer, nonce_prefix_, nonce_prefix_size_); |
122 if (FLAGS_quic_include_path_id_in_iv) { | 123 if (FLAGS_quic_include_path_id_in_iv) { |
123 // Setting the nonce below relies on QuicPathId and QuicPacketNumber being | |
124 // specific sizes. | |
125 static_assert(sizeof(path_id) == 1, "Size of QuicPathId changed."); | |
126 static_assert(sizeof(packet_number) == 8, | |
127 "Size of QuicPacketNumber changed."); | |
128 // Use path_id and lower 7 bytes of packet_number as lower 8 bytes of nonce. | |
129 uint64_t path_id_packet_number = | 124 uint64_t path_id_packet_number = |
130 (static_cast<uint64_t>(path_id) << 56) | packet_number; | 125 QuicUtils::PackPathIdAndPacketNumber(path_id, packet_number); |
131 DCHECK(path_id != kDefaultPathId || path_id_packet_number == packet_number); | |
132 memcpy(nonce_buffer + nonce_prefix_size_, &path_id_packet_number, | 126 memcpy(nonce_buffer + nonce_prefix_size_, &path_id_packet_number, |
133 sizeof(path_id_packet_number)); | 127 sizeof(path_id_packet_number)); |
134 } else { | 128 } else { |
135 memcpy(nonce_buffer + nonce_prefix_size_, &packet_number, | 129 memcpy(nonce_buffer + nonce_prefix_size_, &packet_number, |
136 sizeof(packet_number)); | 130 sizeof(packet_number)); |
137 } | 131 } |
138 | 132 |
139 if (!Encrypt(StringPiece(nonce_buffer, nonce_size), associated_data, | 133 if (!Encrypt(StringPiece(nonce_buffer, nonce_size), associated_data, |
140 plaintext, reinterpret_cast<unsigned char*>(output))) { | 134 plaintext, reinterpret_cast<unsigned char*>(output))) { |
141 return false; | 135 return false; |
(...skipping 24 matching lines...) Expand all Loading... |
166 | 160 |
167 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { | 161 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { |
168 if (nonce_prefix_size_ == 0) { | 162 if (nonce_prefix_size_ == 0) { |
169 return StringPiece(); | 163 return StringPiece(); |
170 } | 164 } |
171 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 165 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
172 nonce_prefix_size_); | 166 nonce_prefix_size_); |
173 } | 167 } |
174 | 168 |
175 } // namespace net | 169 } // namespace net |
OLD | NEW |