blob: e69b0edddc8bce2d031d9a712ed9d4544ab671fa [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"
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// 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
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;
35 bool SetPreliminaryKey(quiche::QuicheStringPiece key) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050036 bool SetDiversificationNonce(const DiversificationNonce& nonce) override;
37 bool DecryptPacket(uint64_t packet_number,
dmcardle904ef182019-12-13 08:34:33 -080038 quiche::QuicheStringPiece associated_data,
39 quiche::QuicheStringPiece ciphertext,
QUICHE teama6ef0a62019-03-07 20:34:33 -050040 char* output,
41 size_t* output_length,
42 size_t max_output_length) override;
43 size_t GetKeySize() const override;
nharper965e5922019-09-23 22:33:54 -070044 size_t GetNoncePrefixSize() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050045 size_t GetIVSize() 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 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_