blob: 7f4d93d323a6654aca49830e70412fc97ccec940 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2012 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_QUIC_DECRYPTER_H_
6#define QUICHE_QUIC_CORE_CRYPTO_QUIC_DECRYPTER_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -070011#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050012
13#include "net/third_party/quiche/src/quic/core/crypto/quic_crypter.h"
QUICHE team2d187972019-03-19 16:23:47 -070014#include "net/third_party/quiche/src/quic/core/quic_data_reader.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015#include "net/third_party/quiche/src/quic/core/quic_packets.h"
16#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
dmcardle904ef182019-12-13 08:34:33 -080017#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050018
19namespace quic {
20
21class QUIC_EXPORT_PRIVATE QuicDecrypter : public QuicCrypter {
22 public:
23 virtual ~QuicDecrypter() {}
24
nharperc1bbfe62019-09-27 16:48:40 -070025 static std::unique_ptr<QuicDecrypter> Create(const ParsedQuicVersion& version,
26 QuicTag algorithm);
QUICHE teama6ef0a62019-03-07 20:34:33 -050027
28 // Creates an IETF QuicDecrypter based on |cipher_suite| which must be an id
29 // returned by SSL_CIPHER_get_id. The caller is responsible for taking
30 // ownership of the new QuicDecrypter.
31 static std::unique_ptr<QuicDecrypter> CreateFromCipherSuite(
32 uint32_t cipher_suite);
33
34 // Sets the encryption key. Returns true on success, false on failure.
35 // |DecryptPacket| may not be called until |SetDiversificationNonce| is
36 // called and the preliminary keying material will be combined with that
37 // nonce in order to create the actual key and nonce-prefix.
38 //
39 // If this function is called, neither |SetKey| nor |SetNoncePrefix| may be
40 // called.
dmcardle904ef182019-12-13 08:34:33 -080041 virtual bool SetPreliminaryKey(quiche::QuicheStringPiece key) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050042
43 // SetDiversificationNonce uses |nonce| to derive final keys based on the
44 // input keying material given by calling |SetPreliminaryKey|.
45 //
46 // Calling this function is a no-op if |SetPreliminaryKey| hasn't been
47 // called.
48 virtual bool SetDiversificationNonce(const DiversificationNonce& nonce) = 0;
49
50 // Populates |output| with the decrypted |ciphertext| and populates
51 // |output_length| with the length. Returns 0 if there is an error.
52 // |output| size is specified by |max_output_length| and must be
53 // at least as large as the ciphertext. |packet_number| is
54 // appended to the |nonce_prefix| value provided in SetNoncePrefix()
55 // to form the nonce.
56 // TODO(wtc): add a way for DecryptPacket to report decryption failure due
57 // to non-authentic inputs, as opposed to other reasons for failure.
58 virtual bool DecryptPacket(uint64_t packet_number,
dmcardle904ef182019-12-13 08:34:33 -080059 quiche::QuicheStringPiece associated_data,
60 quiche::QuicheStringPiece ciphertext,
QUICHE teama6ef0a62019-03-07 20:34:33 -050061 char* output,
62 size_t* output_length,
63 size_t max_output_length) = 0;
64
QUICHE team2d187972019-03-19 16:23:47 -070065 // Reads a sample of ciphertext from |sample_reader| and uses the header
66 // protection key to generate a mask to use for header protection. If
67 // successful, this function returns this mask, which is at least 5 bytes
68 // long. Callers can detect failure by checking if the output string is empty.
69 virtual std::string GenerateHeaderProtectionMask(
70 QuicDataReader* sample_reader) = 0;
71
QUICHE teama6ef0a62019-03-07 20:34:33 -050072 // The ID of the cipher. Return 0x03000000 ORed with the 'cryptographic suite
73 // selector'.
74 virtual uint32_t cipher_id() const = 0;
75
76 // For use by unit tests only.
dmcardle904ef182019-12-13 08:34:33 -080077 virtual quiche::QuicheStringPiece GetKey() const = 0;
78 virtual quiche::QuicheStringPiece GetNoncePrefix() const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050079
dmcardle904ef182019-12-13 08:34:33 -080080 static void DiversifyPreliminaryKey(quiche::QuicheStringPiece preliminary_key,
81 quiche::QuicheStringPiece nonce_prefix,
QUICHE teama6ef0a62019-03-07 20:34:33 -050082 const DiversificationNonce& nonce,
83 size_t key_size,
84 size_t nonce_prefix_size,
vasilvvc48c8712019-03-11 13:38:16 -070085 std::string* out_key,
86 std::string* out_nonce_prefix);
QUICHE teama6ef0a62019-03-07 20:34:33 -050087};
88
89} // namespace quic
90
91#endif // QUICHE_QUIC_CORE_CRYPTO_QUIC_DECRYPTER_H_