Add MB configs for the chromium.chrome waterfall.
[chromium-blink-merge.git] / net / quic / crypto / null_decrypter.cc
blobcad2f7e2d282446234a4545041de7cb61d45f2b2
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"
7 #include <stdint.h>
9 #include "net/quic/quic_utils.h"
10 #include "net/quic/quic_data_reader.h"
12 using base::StringPiece;
13 using std::string;
15 namespace net {
17 NullDecrypter::NullDecrypter() {}
19 bool NullDecrypter::SetKey(StringPiece key) { return key.empty(); }
21 bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) {
22 return nonce_prefix.empty();
25 bool NullDecrypter::DecryptPacket(QuicPacketNumber /*packet_number*/,
26 const StringPiece& associated_data,
27 const StringPiece& ciphertext,
28 char* output,
29 size_t* output_length,
30 size_t max_output_length) {
31 QuicDataReader reader(ciphertext.data(), ciphertext.length());
32 uint128 hash;
34 if (!ReadHash(&reader, &hash)) {
35 return false;
38 StringPiece plaintext = reader.ReadRemainingPayload();
39 if (plaintext.length() > max_output_length) {
40 LOG(DFATAL) << "Output buffer must be larger than the plaintext.";
41 return false;
43 if (hash != ComputeHash(associated_data, plaintext)) {
44 return false;
46 // Copy the plaintext to output.
47 memcpy(output, plaintext.data(), plaintext.length());
48 *output_length = plaintext.length();
49 return true;
52 StringPiece NullDecrypter::GetKey() const { return StringPiece(); }
54 StringPiece NullDecrypter::GetNoncePrefix() const { return StringPiece(); }
56 const char* NullDecrypter::cipher_name() const {
57 return "NULL";
60 uint32 NullDecrypter::cipher_id() const {
61 return 0;
64 bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) {
65 uint64 lo;
66 uint32 hi;
67 if (!reader->ReadUInt64(&lo) ||
68 !reader->ReadUInt32(&hi)) {
69 return false;
71 *hash = hi;
72 *hash <<= 64;
73 *hash += lo;
74 return true;
77 uint128 NullDecrypter::ComputeHash(const StringPiece& data1,
78 const StringPiece& data2) const {
79 uint128 correct_hash = QuicUtils::FNV1a_128_Hash_Two(
80 data1.data(), data1.length(), data2.data(), data2.length());
81 uint128 mask(UINT64_C(0x0), UINT64_C(0xffffffff));
82 mask <<= 96;
83 correct_hash &= ~mask;
84 return correct_hash;
87 } // namespace net