QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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_ENCRYPTER_H_ |
| 6 | #define QUICHE_QUIC_CORE_CRYPTO_AEAD_BASE_ENCRYPTER_H_ |
| 7 | |
| 8 | #include <cstddef> |
| 9 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "third_party/boringssl/src/include/openssl/aead.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | |
| 15 | namespace quic { |
| 16 | |
| 17 | // AeadBaseEncrypter is the base class of AEAD QuicEncrypter subclasses. |
| 18 | class QUIC_EXPORT_PRIVATE AeadBaseEncrypter : public QuicEncrypter { |
| 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 | AeadBaseEncrypter(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 | AeadBaseEncrypter(const AeadBaseEncrypter&) = delete; |
| 28 | AeadBaseEncrypter& operator=(const AeadBaseEncrypter&) = delete; |
| 29 | ~AeadBaseEncrypter() override; |
| 30 | |
| 31 | // QuicEncrypter implementation |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 32 | bool SetKey(quiche::QuicheStringPiece key) override; |
| 33 | bool SetNoncePrefix(quiche::QuicheStringPiece nonce_prefix) override; |
| 34 | bool SetIV(quiche::QuicheStringPiece iv) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | bool EncryptPacket(uint64_t packet_number, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 36 | quiche::QuicheStringPiece associated_data, |
| 37 | quiche::QuicheStringPiece plaintext, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | char* output, |
| 39 | size_t* output_length, |
| 40 | size_t max_output_length) override; |
| 41 | size_t GetKeySize() const override; |
| 42 | size_t GetNoncePrefixSize() const override; |
| 43 | size_t GetIVSize() const override; |
| 44 | size_t GetMaxPlaintextSize(size_t ciphertext_size) const override; |
| 45 | size_t GetCiphertextSize(size_t plaintext_size) const override; |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 46 | quiche::QuicheStringPiece GetKey() const override; |
| 47 | quiche::QuicheStringPiece GetNoncePrefix() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 48 | |
| 49 | // Necessary so unit tests can explicitly specify a nonce, instead of an IV |
| 50 | // (or nonce prefix) and packet number. |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 51 | bool Encrypt(quiche::QuicheStringPiece nonce, |
| 52 | quiche::QuicheStringPiece associated_data, |
| 53 | quiche::QuicheStringPiece plaintext, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | unsigned char* output); |
| 55 | |
| 56 | protected: |
| 57 | // Make these constants available to the subclasses so that the subclasses |
| 58 | // can assert at compile time their key_size_ and nonce_size_ do not |
| 59 | // exceed the maximum. |
| 60 | static const size_t kMaxKeySize = 32; |
| 61 | enum : size_t { kMaxNonceSize = 12 }; |
| 62 | |
| 63 | private: |
| 64 | const EVP_AEAD* const aead_alg_; |
| 65 | const size_t key_size_; |
| 66 | const size_t auth_tag_size_; |
| 67 | const size_t nonce_size_; |
| 68 | const bool use_ietf_nonce_construction_; |
| 69 | |
| 70 | // The key. |
| 71 | unsigned char key_[kMaxKeySize]; |
| 72 | // The IV used to construct the nonce. |
| 73 | unsigned char iv_[kMaxNonceSize]; |
| 74 | |
| 75 | bssl::ScopedEVP_AEAD_CTX ctx_; |
| 76 | }; |
| 77 | |
| 78 | } // namespace quic |
| 79 | |
| 80 | #endif // QUICHE_QUIC_CORE_CRYPTO_AEAD_BASE_ENCRYPTER_H_ |