OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/quic/crypto/null_encrypter.h" | |
6 | |
7 #include "net/quic/quic_data_writer.h" | |
8 #include "net/quic/quic_utils.h" | |
9 | |
10 using base::StringPiece; | |
11 using std::string; | |
12 | |
13 namespace net { | |
14 | |
15 const size_t kHashSizeShort = 12; // size of uint128 serialized short | |
16 | |
17 NullEncrypter::NullEncrypter() {} | |
18 | |
19 bool NullEncrypter::SetKey(StringPiece key) { | |
20 return key.empty(); | |
21 } | |
22 | |
23 bool NullEncrypter::SetNoncePrefix(StringPiece nonce_prefix) { | |
24 return nonce_prefix.empty(); | |
25 } | |
26 | |
27 bool NullEncrypter::EncryptPacket(QuicPathId /*path_id*/, | |
28 QuicPacketNumber /*packet_number*/, | |
29 StringPiece associated_data, | |
30 StringPiece plaintext, | |
31 char* output, | |
32 size_t* output_length, | |
33 size_t max_output_length) { | |
34 const size_t len = plaintext.size() + GetHashLength(); | |
35 if (max_output_length < len) { | |
36 return false; | |
37 } | |
38 uint128 hash = QuicUtils::FNV1a_128_Hash_Two( | |
39 associated_data.data(), associated_data.size(), plaintext.data(), | |
40 plaintext.size()); | |
41 // TODO(ianswett): memmove required for in place encryption. Placing the | |
42 // hash at the end would allow use of memcpy, doing nothing for in place. | |
43 memmove(output + GetHashLength(), plaintext.data(), plaintext.length()); | |
44 QuicUtils::SerializeUint128Short(hash, | |
45 reinterpret_cast<unsigned char*>(output)); | |
46 *output_length = len; | |
47 return true; | |
48 } | |
49 | |
50 size_t NullEncrypter::GetKeySize() const { | |
51 return 0; | |
52 } | |
53 | |
54 size_t NullEncrypter::GetNoncePrefixSize() const { | |
55 return 0; | |
56 } | |
57 | |
58 size_t NullEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { | |
59 return ciphertext_size - GetHashLength(); | |
60 } | |
61 | |
62 size_t NullEncrypter::GetCiphertextSize(size_t plaintext_size) const { | |
63 return plaintext_size + GetHashLength(); | |
64 } | |
65 | |
66 StringPiece NullEncrypter::GetKey() const { | |
67 return StringPiece(); | |
68 } | |
69 | |
70 StringPiece NullEncrypter::GetNoncePrefix() const { | |
71 return StringPiece(); | |
72 } | |
73 | |
74 size_t NullEncrypter::GetHashLength() const { | |
75 return kHashSizeShort; | |
76 } | |
77 | |
78 } // namespace net | |
OLD | NEW |