| 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" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 } | 103 } |
| 104 | 104 |
| 105 if (output_len != ciphertext_size) { | 105 if (output_len != ciphertext_size) { |
| 106 DVLOG(1) << "Wrong output length"; | 106 DVLOG(1) << "Wrong output length"; |
| 107 return false; | 107 return false; |
| 108 } | 108 } |
| 109 | 109 |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 | 112 |
| 113 QuicData* AeadBaseEncrypter::EncryptPacket( | 113 bool AeadBaseEncrypter::EncryptPacket(QuicPacketSequenceNumber sequence_number, |
| 114 QuicPacketSequenceNumber sequence_number, | 114 StringPiece associated_data, |
| 115 StringPiece associated_data, | 115 StringPiece plaintext, |
| 116 StringPiece plaintext) { | 116 char* output, |
| 117 size_t* output_length, |
| 118 size_t max_output_length) { |
| 117 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); | 119 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); |
| 118 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); | 120 if (max_output_length < ciphertext_size) { |
| 119 | 121 return false; |
| 122 } |
| 120 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the | 123 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the |
| 121 // same sequence number twice. | 124 // same sequence number twice. |
| 122 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; | |
| 123 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); | 125 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); |
| 124 DCHECK_LE(nonce_size, sizeof(nonce)); | 126 memcpy(output, nonce_prefix_, nonce_prefix_size_); |
| 125 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); | 127 memcpy(output + nonce_prefix_size_, &sequence_number, |
| 126 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); | 128 sizeof(sequence_number)); |
| 127 if (!Encrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), | 129 if (!Encrypt(StringPiece(output, nonce_size), associated_data, plaintext, |
| 128 associated_data, plaintext, | 130 reinterpret_cast<unsigned char*>(output))) { |
| 129 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | 131 return false; |
| 130 return nullptr; | |
| 131 } | 132 } |
| 132 | 133 *output_length = ciphertext_size; |
| 133 return new QuicData(ciphertext.release(), ciphertext_size, true); | 134 return true; |
| 134 } | 135 } |
| 135 | 136 |
| 136 size_t AeadBaseEncrypter::GetKeySize() const { return key_size_; } | 137 size_t AeadBaseEncrypter::GetKeySize() const { return key_size_; } |
| 137 | 138 |
| 138 size_t AeadBaseEncrypter::GetNoncePrefixSize() const { | 139 size_t AeadBaseEncrypter::GetNoncePrefixSize() const { |
| 139 return nonce_prefix_size_; | 140 return nonce_prefix_size_; |
| 140 } | 141 } |
| 141 | 142 |
| 142 size_t AeadBaseEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { | 143 size_t AeadBaseEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { |
| 143 return ciphertext_size - auth_tag_size_; | 144 return ciphertext_size - auth_tag_size_; |
| 144 } | 145 } |
| 145 | 146 |
| 146 size_t AeadBaseEncrypter::GetCiphertextSize(size_t plaintext_size) const { | 147 size_t AeadBaseEncrypter::GetCiphertextSize(size_t plaintext_size) const { |
| 147 return plaintext_size + auth_tag_size_; | 148 return plaintext_size + auth_tag_size_; |
| 148 } | 149 } |
| 149 | 150 |
| 150 StringPiece AeadBaseEncrypter::GetKey() const { | 151 StringPiece AeadBaseEncrypter::GetKey() const { |
| 151 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); | 152 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); |
| 152 } | 153 } |
| 153 | 154 |
| 154 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { | 155 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { |
| 155 if (nonce_prefix_size_ == 0) { | 156 if (nonce_prefix_size_ == 0) { |
| 156 return StringPiece(); | 157 return StringPiece(); |
| 157 } | 158 } |
| 158 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 159 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
| 159 nonce_prefix_size_); | 160 nonce_prefix_size_); |
| 160 } | 161 } |
| 161 | 162 |
| 162 } // namespace net | 163 } // namespace net |
| OLD | NEW |