blob: cad2f7e2d282446234a4545041de7cb61d45f2b2 [file] [log] [blame]
[email protected]8b37a092012-10-18 21:53:491// 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
rtenneti33106d1c2015-09-16 16:53:245#include "net/quic/crypto/null_decrypter.h"
6
tfarina3723da22015-06-03 21:39:307#include <stdint.h>
8
[email protected]8b37a092012-10-18 21:53:499#include "net/quic/quic_utils.h"
10#include "net/quic/quic_data_reader.h"
11
12using base::StringPiece;
13using std::string;
14
15namespace net {
16
[email protected]5dafdb62013-11-14 01:24:2617NullDecrypter::NullDecrypter() {}
[email protected]752fbe52013-10-14 08:35:3218
[email protected]2532de12013-05-09 12:29:3319bool NullDecrypter::SetKey(StringPiece key) { return key.empty(); }
[email protected]9db443912013-02-25 05:27:0320
21bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) {
22 return nonce_prefix.empty();
23}
24
rtennetia004d332015-08-28 06:44:5725bool NullDecrypter::DecryptPacket(QuicPacketNumber /*packet_number*/,
rtennetifb8763f22015-02-11 18:05:5826 const StringPiece& associated_data,
27 const StringPiece& ciphertext,
28 char* output,
29 size_t* output_length,
30 size_t max_output_length) {
[email protected]8b37a092012-10-18 21:53:4931 QuicDataReader reader(ciphertext.data(), ciphertext.length());
[email protected]8b37a092012-10-18 21:53:4932 uint128 hash;
rtennetifb8763f22015-02-11 18:05:5833
[email protected]752fbe52013-10-14 08:35:3234 if (!ReadHash(&reader, &hash)) {
rtennetifb8763f22015-02-11 18:05:5835 return false;
[email protected]8b37a092012-10-18 21:53:4936 }
37
38 StringPiece plaintext = reader.ReadRemainingPayload();
rtennetifb8763f22015-02-11 18:05:5839 if (plaintext.length() > max_output_length) {
40 LOG(DFATAL) << "Output buffer must be larger than the plaintext.";
41 return false;
[email protected]8b37a092012-10-18 21:53:4942 }
rtennetifb8763f22015-02-11 18:05:5843 if (hash != ComputeHash(associated_data, plaintext)) {
44 return false;
45 }
46 // Copy the plaintext to output.
47 memcpy(output, plaintext.data(), plaintext.length());
48 *output_length = plaintext.length();
49 return true;
[email protected]8b37a092012-10-18 21:53:4950}
51
[email protected]2532de12013-05-09 12:29:3352StringPiece NullDecrypter::GetKey() const { return StringPiece(); }
[email protected]14e8106c2013-03-14 16:25:3353
[email protected]2532de12013-05-09 12:29:3354StringPiece NullDecrypter::GetNoncePrefix() const { return StringPiece(); }
[email protected]14e8106c2013-03-14 16:25:3355
rtenneti06c38702015-07-14 04:44:5956const char* NullDecrypter::cipher_name() const {
57 return "NULL";
58}
59
60uint32 NullDecrypter::cipher_id() const {
61 return 0;
62}
63
[email protected]752fbe52013-10-14 08:35:3264bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) {
[email protected]752fbe52013-10-14 08:35:3265 uint64 lo;
66 uint32 hi;
67 if (!reader->ReadUInt64(&lo) ||
68 !reader->ReadUInt32(&hi)) {
69 return false;
70 }
71 *hash = hi;
72 *hash <<= 64;
73 *hash += lo;
74 return true;
75}
76
rtennetifb8763f22015-02-11 18:05:5877uint128 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());
tfarina3723da22015-06-03 21:39:3081 uint128 mask(UINT64_C(0x0), UINT64_C(0xffffffff));
[email protected]5dafdb62013-11-14 01:24:2682 mask <<= 96;
83 correct_hash &= ~mask;
[email protected]752fbe52013-10-14 08:35:3284 return correct_hash;
85}
86
[email protected]8b37a092012-10-18 21:53:4987} // namespace net