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 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 } | 74 } |
75 | 75 |
76 bool AeadBaseEncrypter::Encrypt(StringPiece nonce, | 76 bool AeadBaseEncrypter::Encrypt(StringPiece nonce, |
77 StringPiece associated_data, | 77 StringPiece associated_data, |
78 StringPiece plaintext, | 78 StringPiece plaintext, |
79 unsigned char* output) { | 79 unsigned char* output) { |
80 if (nonce.size() != nonce_prefix_size_ + sizeof(QuicPacketSequenceNumber)) { | 80 if (nonce.size() != nonce_prefix_size_ + sizeof(QuicPacketSequenceNumber)) { |
81 return false; | 81 return false; |
82 } | 82 } |
83 | 83 |
84 size_t len; | 84 size_t ciphertext_len; |
85 if (!EVP_AEAD_CTX_seal( | 85 if (!EVP_AEAD_CTX_seal( |
86 ctx_.get(), | 86 ctx_.get(), output, &ciphertext_len, |
87 output, | |
88 &len, | |
89 plaintext.size() + auth_tag_size_, | 87 plaintext.size() + auth_tag_size_, |
90 reinterpret_cast<const uint8_t*>(nonce.data()), | 88 reinterpret_cast<const uint8_t*>(nonce.data()), nonce.size(), |
91 nonce.size(), | 89 reinterpret_cast<const uint8_t*>(plaintext.data()), plaintext.size(), |
92 reinterpret_cast<const uint8_t*>(plaintext.data()), | |
93 plaintext.size(), | |
94 reinterpret_cast<const uint8_t*>(associated_data.data()), | 90 reinterpret_cast<const uint8_t*>(associated_data.data()), |
95 associated_data.size())) { | 91 associated_data.size())) { |
96 DLogOpenSslErrors(); | 92 DLogOpenSslErrors(); |
97 return false; | 93 return false; |
98 } | 94 } |
99 | 95 |
100 return true; | 96 return true; |
101 } | 97 } |
102 | 98 |
103 QuicData* AeadBaseEncrypter::EncryptPacket( | 99 bool AeadBaseEncrypter::EncryptPacket(QuicPacketSequenceNumber sequence_number, |
104 QuicPacketSequenceNumber sequence_number, | 100 StringPiece associated_data, |
105 StringPiece associated_data, | 101 StringPiece plaintext, |
106 StringPiece plaintext) { | 102 char* output, |
| 103 size_t* output_length, |
| 104 size_t max_output_length) { |
107 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); | 105 size_t ciphertext_size = GetCiphertextSize(plaintext.length()); |
108 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); | 106 if (max_output_length < ciphertext_size) { |
109 | 107 return false; |
| 108 } |
110 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the | 109 // TODO(ianswett): Introduce a check to ensure that we don't encrypt with the |
111 // same sequence number twice. | 110 // same sequence number twice. |
112 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; | |
113 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); | 111 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); |
114 DCHECK_LE(nonce_size, sizeof(nonce)); | 112 memcpy(output, nonce_prefix_, nonce_prefix_size_); |
115 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); | 113 memcpy(output + nonce_prefix_size_, &sequence_number, |
116 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); | 114 sizeof(sequence_number)); |
117 if (!Encrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), | 115 if (!Encrypt(StringPiece(output, nonce_size), associated_data, plaintext, |
118 associated_data, plaintext, | 116 reinterpret_cast<unsigned char*>(output))) { |
119 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | 117 return false; |
120 return nullptr; | |
121 } | 118 } |
122 | 119 *output_length = ciphertext_size; |
123 return new QuicData(ciphertext.release(), ciphertext_size, true); | 120 return true; |
124 } | 121 } |
125 | 122 |
126 size_t AeadBaseEncrypter::GetKeySize() const { return key_size_; } | 123 size_t AeadBaseEncrypter::GetKeySize() const { return key_size_; } |
127 | 124 |
128 size_t AeadBaseEncrypter::GetNoncePrefixSize() const { | 125 size_t AeadBaseEncrypter::GetNoncePrefixSize() const { |
129 return nonce_prefix_size_; | 126 return nonce_prefix_size_; |
130 } | 127 } |
131 | 128 |
132 size_t AeadBaseEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { | 129 size_t AeadBaseEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { |
133 return ciphertext_size - auth_tag_size_; | 130 return ciphertext_size - auth_tag_size_; |
134 } | 131 } |
135 | 132 |
136 size_t AeadBaseEncrypter::GetCiphertextSize(size_t plaintext_size) const { | 133 size_t AeadBaseEncrypter::GetCiphertextSize(size_t plaintext_size) const { |
137 return plaintext_size + auth_tag_size_; | 134 return plaintext_size + auth_tag_size_; |
138 } | 135 } |
139 | 136 |
140 StringPiece AeadBaseEncrypter::GetKey() const { | 137 StringPiece AeadBaseEncrypter::GetKey() const { |
141 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); | 138 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); |
142 } | 139 } |
143 | 140 |
144 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { | 141 StringPiece AeadBaseEncrypter::GetNoncePrefix() const { |
145 if (nonce_prefix_size_ == 0) { | 142 if (nonce_prefix_size_ == 0) { |
146 return StringPiece(); | 143 return StringPiece(); |
147 } | 144 } |
148 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 145 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
149 nonce_prefix_size_); | 146 nonce_prefix_size_); |
150 } | 147 } |
151 | 148 |
152 } // namespace net | 149 } // namespace net |
OLD | NEW |