xref: /aosp_15_r20/external/cronet/net/quic/mock_decrypter.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/quic/mock_decrypter.h"
6 
7 #include <limits>
8 
9 #include "net/third_party/quiche/src/quiche/quic/core/quic_utils.h"
10 #include "net/third_party/quiche/src/quiche/quic/platform/api/quic_bug_tracker.h"
11 
12 using quic::DiversificationNonce;
13 using quic::Perspective;
14 using quic::QuicPacketNumber;
15 
16 namespace net {
17 
18 namespace {
19 
20 const size_t kPaddingSize = 12;
21 
22 }  // namespace
23 
MockDecrypter(Perspective perspective)24 MockDecrypter::MockDecrypter(Perspective perspective) {}
25 
SetKey(std::string_view key)26 bool MockDecrypter::SetKey(std::string_view key) {
27   return key.empty();
28 }
29 
SetNoncePrefix(std::string_view nonce_prefix)30 bool MockDecrypter::SetNoncePrefix(std::string_view nonce_prefix) {
31   return nonce_prefix.empty();
32 }
33 
SetIV(std::string_view iv)34 bool MockDecrypter::SetIV(std::string_view iv) {
35   return iv.empty();
36 }
37 
SetHeaderProtectionKey(std::string_view key)38 bool MockDecrypter::SetHeaderProtectionKey(std::string_view key) {
39   return key.empty();
40 }
41 
GetKeySize() const42 size_t MockDecrypter::GetKeySize() const {
43   return 0;
44 }
45 
GetIVSize() const46 size_t MockDecrypter::GetIVSize() const {
47   return 0;
48 }
49 
GetNoncePrefixSize() const50 size_t MockDecrypter::GetNoncePrefixSize() const {
51   return 0;
52 }
53 
SetPreliminaryKey(std::string_view key)54 bool MockDecrypter::SetPreliminaryKey(std::string_view key) {
55   LOG(DFATAL) << "Should not be called";
56   return false;
57 }
58 
SetDiversificationNonce(const DiversificationNonce & nonce)59 bool MockDecrypter::SetDiversificationNonce(const DiversificationNonce& nonce) {
60   LOG(DFATAL) << "Should not be called";
61   return true;
62 }
63 
DecryptPacket(uint64_t,std::string_view associated_data,std::string_view ciphertext,char * output,size_t * output_length,size_t max_output_length)64 bool MockDecrypter::DecryptPacket(uint64_t /*packet_number*/,
65                                   std::string_view associated_data,
66                                   std::string_view ciphertext,
67                                   char* output,
68                                   size_t* output_length,
69                                   size_t max_output_length) {
70   if (ciphertext.length() < kPaddingSize) {
71     return false;
72   }
73   size_t plaintext_size = ciphertext.length() - kPaddingSize;
74   if (plaintext_size > max_output_length) {
75     return false;
76   }
77 
78   memcpy(output, ciphertext.data(), plaintext_size);
79   *output_length = plaintext_size;
80   return true;
81 }
82 
GenerateHeaderProtectionMask(quic::QuicDataReader * sample_reader)83 std::string MockDecrypter::GenerateHeaderProtectionMask(
84     quic::QuicDataReader* sample_reader) {
85   return std::string(5, 0);
86 }
87 
cipher_id() const88 uint32_t MockDecrypter::cipher_id() const {
89   return 0;
90 }
91 
GetIntegrityLimit() const92 quic::QuicPacketCount MockDecrypter::GetIntegrityLimit() const {
93   return std::numeric_limits<quic::QuicPacketCount>::max();
94 }
95 
GetKey() const96 std::string_view MockDecrypter::GetKey() const {
97   return std::string_view();
98 }
99 
GetNoncePrefix() const100 std::string_view MockDecrypter::GetNoncePrefix() const {
101   return std::string_view();
102 }
103 
104 }  // namespace net
105