| 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 <pk11pub.h> | 7 #include <pk11pub.h> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "crypto/scoped_nss_types.h" | 10 #include "crypto/scoped_nss_types.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 // The maximum size in bytes of the nonce, including 8 bytes of sequence number. | 20 // 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. | 21 // ChaCha20 uses only the 8 byte sequence number and AES-GCM uses 12 bytes. |
| 21 const size_t kMaxNonceSize = 12; | 22 const size_t kMaxNonceSize = 12; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); | 129 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); |
| 129 if (max_output_length < ciphertext_size) { | 130 if (max_output_length < ciphertext_size) { |
| 130 return false; | 131 return false; |
| 131 } | 132 } |
| 132 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the | 133 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the |
| 133 // same packet number twice. | 134 // same packet number twice. |
| 134 const size_t nonce_size = nonce_prefix_size_ + sizeof(packet_number); | 135 const size_t nonce_size = nonce_prefix_size_ + sizeof(packet_number); |
| 135 ALIGNAS(4) char nonce_buffer[kMaxNonceSize]; | 136 ALIGNAS(4) char nonce_buffer[kMaxNonceSize]; |
| 136 memcpy(nonce_buffer, nonce_prefix_, nonce_prefix_size_); | 137 memcpy(nonce_buffer, nonce_prefix_, nonce_prefix_size_); |
| 137 if (FLAGS_quic_include_path_id_in_iv) { | 138 if (FLAGS_quic_include_path_id_in_iv) { |
| 138 // Setting the nonce below relies on QuicPathId and QuicPacketNumber being | |
| 139 // specific sizes. | |
| 140 static_assert(sizeof(path_id) == 1, "Size of QuicPathId changed."); | |
| 141 static_assert(sizeof(packet_number) == 8, | |
| 142 "Size of QuicPacketNumber changed."); | |
| 143 // Use path_id and lower 7 bytes of packet_number as lower 8 bytes of nonce. | |
| 144 uint64_t path_id_packet_number = | 139 uint64_t path_id_packet_number = |
| 145 (static_cast<uint64_t>(path_id) << 56) | packet_number; | 140 QuicUtils::PackPathIdAndPacketNumber(path_id, packet_number); |
| 146 DCHECK(path_id != kDefaultPathId || path_id_packet_number == packet_number); | |
| 147 memcpy(nonce_buffer + nonce_prefix_size_, &path_id_packet_number, | 141 memcpy(nonce_buffer + nonce_prefix_size_, &path_id_packet_number, |
| 148 sizeof(path_id_packet_number)); | 142 sizeof(path_id_packet_number)); |
| 149 } else { | 143 } else { |
| 150 memcpy(nonce_buffer + nonce_prefix_size_, &packet_number, | 144 memcpy(nonce_buffer + nonce_prefix_size_, &packet_number, |
| 151 sizeof(packet_number)); | 145 sizeof(packet_number)); |
| 152 } | 146 } |
| 153 if (!Encrypt(StringPiece(nonce_buffer, nonce_size), associated_data, | 147 if (!Encrypt(StringPiece(nonce_buffer, nonce_size), associated_data, |
| 154 plaintext, reinterpret_cast<unsigned char*>(output))) { | 148 plaintext, reinterpret_cast<unsigned char*>(output))) { |
| 155 return false; | 149 return false; |
| 156 } | 150 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 180 | 174 |
| 181 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { | 175 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { |
| 182 if (nonce_prefix_size_ == 0) { | 176 if (nonce_prefix_size_ == 0) { |
| 183 return StringPiece(); | 177 return StringPiece(); |
| 184 } | 178 } |
| 185 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 179 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
| 186 nonce_prefix_size_); | 180 nonce_prefix_size_); |
| 187 } | 181 } |
| 188 | 182 |
| 189 } // namespace net | 183 } // namespace net |
| OLD | NEW |