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 #ifndef QUICHE_QUIC_CORE_CRYPTO_AEAD_BASE_DECRYPTER_H_
6 #define QUICHE_QUIC_CORE_CRYPTO_AEAD_BASE_DECRYPTER_H_
7 
8 #include <cstddef>
9 
10 #include "absl/strings/string_view.h"
11 #include "third_party/boringssl/src/include/openssl/aead.h"
12 #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h"
13 #include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
14 
15 namespace quic {
16 
17 // AeadBaseDecrypter is the base class of AEAD QuicDecrypter subclasses.
18 class QUIC_EXPORT_PRIVATE AeadBaseDecrypter : public QuicDecrypter {
19  public:
20   // This takes the function pointer rather than the EVP_AEAD itself so
21   // subclasses do not need to call CRYPTO_library_init.
22   AeadBaseDecrypter(const EVP_AEAD* (*aead_getter)(),
23                     size_t key_size,
24                     size_t auth_tag_size,
25                     size_t nonce_size,
26                     bool use_ietf_nonce_construction);
27   AeadBaseDecrypter(const AeadBaseDecrypter&) = delete;
28   AeadBaseDecrypter& operator=(const AeadBaseDecrypter&) = delete;
29   ~AeadBaseDecrypter() override;
30 
31   // QuicDecrypter implementation
32   bool SetKey(absl::string_view key) override;
33   bool SetNoncePrefix(absl::string_view nonce_prefix) override;
34   bool SetIV(absl::string_view iv) override;
35   bool SetPreliminaryKey(absl::string_view key) override;
36   bool SetDiversificationNonce(const DiversificationNonce& nonce) override;
37   bool DecryptPacket(uint64_t packet_number,
38                      absl::string_view associated_data,
39                      absl::string_view ciphertext,
40                      char* output,
41                      size_t* output_length,
42                      size_t max_output_length) override;
43   size_t GetKeySize() const override;
44   size_t GetNoncePrefixSize() const override;
45   size_t GetIVSize() const override;
46   absl::string_view GetKey() const override;
47   absl::string_view GetNoncePrefix() const override;
48 
49  protected:
50   // Make these constants available to the subclasses so that the subclasses
51   // can assert at compile time their key_size_ and nonce_size_ do not
52   // exceed the maximum.
53   static const size_t kMaxKeySize = 32;
54   static const size_t kMaxNonceSize = 12;
55 
56  private:
57   const EVP_AEAD* const aead_alg_;
58   const size_t key_size_;
59   const size_t auth_tag_size_;
60   const size_t nonce_size_;
61   const bool use_ietf_nonce_construction_;
62   bool have_preliminary_key_;
63 
64   // The key.
65   unsigned char key_[kMaxKeySize];
66   // The IV used to construct the nonce.
67   unsigned char iv_[kMaxNonceSize];
68 
69   bssl::ScopedEVP_AEAD_CTX ctx_;
70 };
71 
72 }  // namespace quic
73 
74 #endif  // QUICHE_QUIC_CORE_CRYPTO_AEAD_BASE_DECRYPTER_H_
75