blob: 8c7fa158c8254106a5f17649e881d7e84b6186f9 [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_DECRYPTER_H_
6#define QUICHE_QUIC_CORE_CRYPTO_AEAD_BASE_DECRYPTER_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_decrypter.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
13#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
14
15namespace quic {
16
17// AeadBaseDecrypter is the base class of AEAD QuicDecrypter subclasses.
18class 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(QuicStringPiece key) override;
33 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override;
34 bool SetIV(QuicStringPiece iv) override;
35 bool SetPreliminaryKey(QuicStringPiece key) override;
36 bool SetDiversificationNonce(const DiversificationNonce& nonce) override;
37 bool DecryptPacket(uint64_t packet_number,
38 QuicStringPiece associated_data,
39 QuicStringPiece 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 GetIVSize() const override;
45 QuicStringPiece GetKey() const override;
46 QuicStringPiece GetNoncePrefix() const override;
47
48 protected:
49 // Make these constants available to the subclasses so that the subclasses
50 // can assert at compile time their key_size_ and nonce_size_ do not
51 // exceed the maximum.
52 static const size_t kMaxKeySize = 32;
53 static const size_t kMaxNonceSize = 12;
54
55 private:
56 const EVP_AEAD* const aead_alg_;
57 const size_t key_size_;
58 const size_t auth_tag_size_;
59 const size_t nonce_size_;
60 const bool use_ietf_nonce_construction_;
61 bool have_preliminary_key_;
62
63 // The key.
64 unsigned char key_[kMaxKeySize];
65 // The IV used to construct the nonce.
66 unsigned char iv_[kMaxNonceSize];
67
68 bssl::ScopedEVP_AEAD_CTX ctx_;
69};
70
71} // namespace quic
72
73#endif // QUICHE_QUIC_CORE_CRYPTO_AEAD_BASE_DECRYPTER_H_