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.
5 #include "net/quic/crypto/null_decrypter.h"
6 #include "net/quic/quic_utils.h"
7 #include "net/quic/quic_data_reader.h"
9 using base::StringPiece
;
14 NullDecrypter::NullDecrypter() {}
16 bool NullDecrypter::SetKey(StringPiece key
) { return key
.empty(); }
18 bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix
) {
19 return nonce_prefix
.empty();
22 bool NullDecrypter::Decrypt(StringPiece
/*nonce*/,
23 StringPiece associated_data
,
24 StringPiece ciphertext
,
25 unsigned char* output
,
26 size_t* output_length
) {
27 QuicDataReader
reader(ciphertext
.data(), ciphertext
.length());
30 if (!ReadHash(&reader
, &hash
)) {
34 StringPiece plaintext
= reader
.ReadRemainingPayload();
36 // TODO(rch): avoid buffer copy here
37 string buffer
= associated_data
.as_string();
38 plaintext
.AppendToString(&buffer
);
39 if (hash
!= ComputeHash(buffer
)) {
42 memcpy(output
, plaintext
.data(), plaintext
.length());
43 *output_length
= plaintext
.length();
47 QuicData
* NullDecrypter::DecryptPacket(QuicPacketSequenceNumber
/*seq_number*/,
48 StringPiece associated_data
,
49 StringPiece ciphertext
) {
50 // It's worth duplicating |Decrypt|, above, in order to save a copy by using
51 // the shared-data QuicData constructor directly.
52 QuicDataReader
reader(ciphertext
.data(), ciphertext
.length());
55 if (!ReadHash(&reader
, &hash
)) {
59 StringPiece plaintext
= reader
.ReadRemainingPayload();
61 // TODO(rch): avoid buffer copy here
62 string buffer
= associated_data
.as_string();
63 plaintext
.AppendToString(&buffer
);
65 if (hash
!= ComputeHash(buffer
)) {
68 return new QuicData(plaintext
.data(), plaintext
.length());
71 StringPiece
NullDecrypter::GetKey() const { return StringPiece(); }
73 StringPiece
NullDecrypter::GetNoncePrefix() const { return StringPiece(); }
75 bool NullDecrypter::ReadHash(QuicDataReader
* reader
, uint128
* hash
) {
78 if (!reader
->ReadUInt64(&lo
) ||
79 !reader
->ReadUInt32(&hi
)) {
88 uint128
NullDecrypter::ComputeHash(const string
& data
) const {
89 uint128 correct_hash
= QuicUtils::FNV1a_128_Hash(data
.data(), data
.length());
90 uint128
mask(GG_UINT64_C(0x0), GG_UINT64_C(0xffffffff));
92 correct_hash
&= ~mask
;