QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 11 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | |
| 13 | #include "net/third_party/quiche/src/quic/core/crypto/quic_crypter.h" |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/quic_data_reader.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | |
| 19 | namespace quic { |
| 20 | |
| 21 | class QUIC_EXPORT_PRIVATE QuicDecrypter : public QuicCrypter { |
| 22 | public: |
| 23 | virtual ~QuicDecrypter() {} |
| 24 | |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 25 | static std::unique_ptr<QuicDecrypter> Create(const ParsedQuicVersion& version, |
| 26 | QuicTag algorithm); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | |
| 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. |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 41 | virtual bool SetPreliminaryKey(quiche::QuicheStringPiece key) = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 42 | |
| 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, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 59 | quiche::QuicheStringPiece associated_data, |
| 60 | quiche::QuicheStringPiece ciphertext, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 61 | char* output, |
| 62 | size_t* output_length, |
| 63 | size_t max_output_length) = 0; |
| 64 | |
QUICHE team | 2d18797 | 2019-03-19 16:23:47 -0700 | [diff] [blame] | 65 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 72 | // 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. |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 77 | virtual quiche::QuicheStringPiece GetKey() const = 0; |
| 78 | virtual quiche::QuicheStringPiece GetNoncePrefix() const = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 80 | static void DiversifyPreliminaryKey(quiche::QuicheStringPiece preliminary_key, |
| 81 | quiche::QuicheStringPiece nonce_prefix, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | const DiversificationNonce& nonce, |
| 83 | size_t key_size, |
| 84 | size_t nonce_prefix_size, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 85 | std::string* out_key, |
| 86 | std::string* out_nonce_prefix); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace quic |
| 90 | |
| 91 | #endif // QUICHE_QUIC_CORE_CRYPTO_QUIC_DECRYPTER_H_ |