[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 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. |
| 4 | |
rtenneti | 33106d1c | 2015-09-16 16:53:24 | [diff] [blame] | 5 | #include "net/quic/crypto/null_decrypter.h" |
| 6 | |
tfarina | 3723da2 | 2015-06-03 21:39:30 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 9 | #include "net/quic/quic_utils.h" |
| 10 | #include "net/quic/quic_data_reader.h" |
| 11 | |
| 12 | using base::StringPiece; |
| 13 | using std::string; |
| 14 | |
| 15 | namespace net { |
| 16 | |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 17 | NullDecrypter::NullDecrypter() {} |
[email protected] | 752fbe5 | 2013-10-14 08:35:32 | [diff] [blame] | 18 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 19 | bool NullDecrypter::SetKey(StringPiece key) { return key.empty(); } |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 20 | |
| 21 | bool NullDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { |
| 22 | return nonce_prefix.empty(); |
| 23 | } |
| 24 | |
rtenneti | a004d33 | 2015-08-28 06:44:57 | [diff] [blame] | 25 | bool NullDecrypter::DecryptPacket(QuicPacketNumber /*packet_number*/, |
rtenneti | fb8763f2 | 2015-02-11 18:05:58 | [diff] [blame] | 26 | const StringPiece& associated_data, |
| 27 | const StringPiece& ciphertext, |
| 28 | char* output, |
| 29 | size_t* output_length, |
| 30 | size_t max_output_length) { |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 31 | QuicDataReader reader(ciphertext.data(), ciphertext.length()); |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 32 | uint128 hash; |
rtenneti | fb8763f2 | 2015-02-11 18:05:58 | [diff] [blame] | 33 | |
[email protected] | 752fbe5 | 2013-10-14 08:35:32 | [diff] [blame] | 34 | if (!ReadHash(&reader, &hash)) { |
rtenneti | fb8763f2 | 2015-02-11 18:05:58 | [diff] [blame] | 35 | return false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | StringPiece plaintext = reader.ReadRemainingPayload(); |
rtenneti | fb8763f2 | 2015-02-11 18:05:58 | [diff] [blame] | 39 | if (plaintext.length() > max_output_length) { |
| 40 | LOG(DFATAL) << "Output buffer must be larger than the plaintext."; |
| 41 | return false; |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 42 | } |
rtenneti | fb8763f2 | 2015-02-11 18:05:58 | [diff] [blame] | 43 | 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] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 52 | StringPiece NullDecrypter::GetKey() const { return StringPiece(); } |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 53 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 54 | StringPiece NullDecrypter::GetNoncePrefix() const { return StringPiece(); } |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 55 | |
rtenneti | 06c3870 | 2015-07-14 04:44:59 | [diff] [blame] | 56 | const char* NullDecrypter::cipher_name() const { |
| 57 | return "NULL"; |
| 58 | } |
| 59 | |
| 60 | uint32 NullDecrypter::cipher_id() const { |
| 61 | return 0; |
| 62 | } |
| 63 | |
[email protected] | 752fbe5 | 2013-10-14 08:35:32 | [diff] [blame] | 64 | bool NullDecrypter::ReadHash(QuicDataReader* reader, uint128* hash) { |
[email protected] | 752fbe5 | 2013-10-14 08:35:32 | [diff] [blame] | 65 | 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 | |
rtenneti | fb8763f2 | 2015-02-11 18:05:58 | [diff] [blame] | 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()); |
tfarina | 3723da2 | 2015-06-03 21:39:30 | [diff] [blame] | 81 | uint128 mask(UINT64_C(0x0), UINT64_C(0xffffffff)); |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 82 | mask <<= 96; |
| 83 | correct_hash &= ~mask; |
[email protected] | 752fbe5 | 2013-10-14 08:35:32 | [diff] [blame] | 84 | return correct_hash; |
| 85 | } |
| 86 | |
[email protected] | 8b37a09 | 2012-10-18 21:53:49 | [diff] [blame] | 87 | } // namespace net |