blob: 5209a023caa553815599ca5353bcf596d5b69438 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 teama6ef0a62019-03-07 20:34:33 -050010#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"
dmcardle904ef182019-12-13 08:34:33 -080013#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050014
15namespace quic {
16
17// AeadBaseEncrypter is the base class of AEAD QuicEncrypter subclasses.
18class 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
dmcardle904ef182019-12-13 08:34:33 -080032 bool SetKey(quiche::QuicheStringPiece key) override;
33 bool SetNoncePrefix(quiche::QuicheStringPiece nonce_prefix) override;
34 bool SetIV(quiche::QuicheStringPiece iv) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050035 bool EncryptPacket(uint64_t packet_number,
dmcardle904ef182019-12-13 08:34:33 -080036 quiche::QuicheStringPiece associated_data,
37 quiche::QuicheStringPiece plaintext,
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 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;
dmcardle904ef182019-12-13 08:34:33 -080046 quiche::QuicheStringPiece GetKey() const override;
47 quiche::QuicheStringPiece GetNoncePrefix() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050048
49 // Necessary so unit tests can explicitly specify a nonce, instead of an IV
50 // (or nonce prefix) and packet number.
dmcardle904ef182019-12-13 08:34:33 -080051 bool Encrypt(quiche::QuicheStringPiece nonce,
52 quiche::QuicheStringPiece associated_data,
53 quiche::QuicheStringPiece plaintext,
QUICHE teama6ef0a62019-03-07 20:34:33 -050054 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_