xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/crypto/aes_base_encrypter.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2013 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 
5 #include "quiche/quic/core/crypto/aes_base_encrypter.h"
6 
7 #include "absl/strings/string_view.h"
8 #include "openssl/aes.h"
9 #include "quiche/quic/platform/api/quic_bug_tracker.h"
10 
11 namespace quic {
12 
SetHeaderProtectionKey(absl::string_view key)13 bool AesBaseEncrypter::SetHeaderProtectionKey(absl::string_view key) {
14   if (key.size() != GetKeySize()) {
15     QUIC_BUG(quic_bug_10726_1)
16         << "Invalid key size for header protection: " << key.size();
17     return false;
18   }
19   if (AES_set_encrypt_key(reinterpret_cast<const uint8_t*>(key.data()),
20                           key.size() * 8, &pne_key_) != 0) {
21     QUIC_BUG(quic_bug_10726_2) << "Unexpected failure of AES_set_encrypt_key";
22     return false;
23   }
24   return true;
25 }
26 
GenerateHeaderProtectionMask(absl::string_view sample)27 std::string AesBaseEncrypter::GenerateHeaderProtectionMask(
28     absl::string_view sample) {
29   if (sample.size() != AES_BLOCK_SIZE) {
30     return std::string();
31   }
32   std::string out(AES_BLOCK_SIZE, 0);
33   AES_encrypt(reinterpret_cast<const uint8_t*>(sample.data()),
34               reinterpret_cast<uint8_t*>(const_cast<char*>(out.data())),
35               &pne_key_);
36   return out;
37 }
38 
GetConfidentialityLimit() const39 QuicPacketCount AesBaseEncrypter::GetConfidentialityLimit() const {
40   // For AEAD_AES_128_GCM and AEAD_AES_256_GCM ... endpoints that do not send
41   // packets larger than 2^11 bytes cannot protect more than 2^28 packets.
42   // https://quicwg.org/base-drafts/draft-ietf-quic-tls.html#name-confidentiality-limit
43   static_assert(kMaxOutgoingPacketSize <= 2048,
44                 "This key limit requires limits on encryption payload sizes");
45   return 268435456U;
46 }
47 
48 }  // namespace quic
49