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(bool use_short_hash)
15 : use_short_hash_(use_short_hash) {}
17 bool NullDecrypter::SetKey(StringPiece key) { return key.empty(); }
19 bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) {
20 return nonce_prefix.empty();
23 bool NullDecrypter::Decrypt(StringPiece /*nonce*/,
24 StringPiece associated_data,
25 StringPiece ciphertext,
26 unsigned char* output,
27 size_t* output_length) {
28 QuicDataReader reader(ciphertext.data(), ciphertext.length());
31 if (!ReadHash(&reader, &hash)) {
35 StringPiece plaintext = reader.ReadRemainingPayload();
37 // TODO(rch): avoid buffer copy here
38 string buffer = associated_data.as_string();
39 plaintext.AppendToString(&buffer);
40 if (hash != ComputeHash(buffer)) {
43 memcpy(output, plaintext.data(), plaintext.length());
44 *output_length = plaintext.length();
48 QuicData* NullDecrypter::DecryptPacket(QuicPacketSequenceNumber /*seq_number*/,
49 StringPiece associated_data,
50 StringPiece ciphertext) {
51 // It's worth duplicating |Decrypt|, above, in order to save a copy by using
52 // the shared-data QuicData constructor directly.
53 QuicDataReader reader(ciphertext.data(), ciphertext.length());
56 if (!ReadHash(&reader, &hash)) {
60 StringPiece plaintext = reader.ReadRemainingPayload();
62 // TODO(rch): avoid buffer copy here
63 string buffer = associated_data.as_string();
64 plaintext.AppendToString(&buffer);
66 if (hash != ComputeHash(buffer)) {
69 return new QuicData(plaintext.data(), plaintext.length());
72 StringPiece NullDecrypter::GetKey() const { return StringPiece(); }
74 StringPiece NullDecrypter::GetNoncePrefix() const { return StringPiece(); }
76 bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) {
77 if (!use_short_hash_) {
78 return reader->ReadUInt128(hash);
83 if (!reader->ReadUInt64(&lo) ||
84 !reader->ReadUInt32(&hi)) {
93 uint128 NullDecrypter::ComputeHash(const string& data) const {
94 uint128 correct_hash = QuicUtils::FNV1a_128_Hash(data.data(), data.length());
95 if (use_short_hash_) {
96 uint128 mask(GG_UINT64_C(0x0), GG_UINT64_C(0xffffffff));
98 correct_hash &= ~mask;