Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / net / quic / crypto / null_decrypter.cc
blob7ca704b44b60fcad5279ca181dabd45dabd01b7f
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 <stdint.h>
7 #include "net/quic/crypto/null_decrypter.h"
8 #include "net/quic/quic_utils.h"
9 #include "net/quic/quic_data_reader.h"
11 using base::StringPiece;
12 using std::string;
14 namespace net {
16 NullDecrypter::NullDecrypter() {}
18 bool NullDecrypter::SetKey(StringPiece key) { return key.empty(); }
20 bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) {
21 return nonce_prefix.empty();
24 bool NullDecrypter::DecryptPacket(QuicPacketNumber /*packet_number*/,
25 const StringPiece& associated_data,
26 const StringPiece& ciphertext,
27 char* output,
28 size_t* output_length,
29 size_t max_output_length) {
30 QuicDataReader reader(ciphertext.data(), ciphertext.length());
31 uint128 hash;
33 if (!ReadHash(&reader, &hash)) {
34 return false;
37 StringPiece plaintext = reader.ReadRemainingPayload();
38 if (plaintext.length() > max_output_length) {
39 LOG(DFATAL) << "Output buffer must be larger than the plaintext.";
40 return false;
42 if (hash != ComputeHash(associated_data, plaintext)) {
43 return false;
45 // Copy the plaintext to output.
46 memcpy(output, plaintext.data(), plaintext.length());
47 *output_length = plaintext.length();
48 return true;
51 StringPiece NullDecrypter::GetKey() const { return StringPiece(); }
53 StringPiece NullDecrypter::GetNoncePrefix() const { return StringPiece(); }
55 const char* NullDecrypter::cipher_name() const {
56 return "NULL";
59 uint32 NullDecrypter::cipher_id() const {
60 return 0;
63 bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) {
64 uint64 lo;
65 uint32 hi;
66 if (!reader->ReadUInt64(&lo) ||
67 !reader->ReadUInt32(&hi)) {
68 return false;
70 *hash = hi;
71 *hash <<= 64;
72 *hash += lo;
73 return true;
76 uint128 NullDecrypter::ComputeHash(const StringPiece& data1,
77 const StringPiece& data2) const {
78 uint128 correct_hash = QuicUtils::FNV1a_128_Hash_Two(
79 data1.data(), data1.length(), data2.data(), data2.length());
80 uint128 mask(UINT64_C(0x0), UINT64_C(0xffffffff));
81 mask <<= 96;
82 correct_hash &= ~mask;
83 return correct_hash;
86 } // namespace net