blob: d37472266efc676e29b75832b1499659e2f0a331 [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
10#include "base/macros.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#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
15
16namespace quic {
17
18// AeadBaseDecrypter is the base class of AEAD QuicDecrypter subclasses.
19class QUIC_EXPORT_PRIVATE AeadBaseDecrypter : public QuicDecrypter {
20 public:
21 // This takes the function pointer rather than the EVP_AEAD itself so
22 // subclasses do not need to call CRYPTO_library_init.
23 AeadBaseDecrypter(const EVP_AEAD* (*aead_getter)(),
24 size_t key_size,
25 size_t auth_tag_size,
26 size_t nonce_size,
27 bool use_ietf_nonce_construction);
28 AeadBaseDecrypter(const AeadBaseDecrypter&) = delete;
29 AeadBaseDecrypter& operator=(const AeadBaseDecrypter&) = delete;
30 ~AeadBaseDecrypter() override;
31
32 // QuicDecrypter implementation
33 bool SetKey(QuicStringPiece key) override;
34 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override;
35 bool SetIV(QuicStringPiece iv) override;
36 bool SetPreliminaryKey(QuicStringPiece key) override;
37 bool SetDiversificationNonce(const DiversificationNonce& nonce) override;
38 bool DecryptPacket(uint64_t packet_number,
39 QuicStringPiece associated_data,
40 QuicStringPiece ciphertext,
41 char* output,
42 size_t* output_length,
43 size_t max_output_length) override;
44 size_t GetKeySize() const override;
45 size_t GetIVSize() const override;
46 QuicStringPiece GetKey() const override;
47 QuicStringPiece 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_