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