QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2018 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_HKDF_H_ |
| 6 | #define QUICHE_QUIC_CORE_CRYPTO_QUIC_HKDF_H_ |
| 7 | |
danzh | 0289317 | 2019-06-05 15:54:11 -0700 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 11 | #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] | 12 | |
| 13 | namespace quic { |
| 14 | |
| 15 | // QuicHKDF implements the key derivation function specified in RFC 5869 |
| 16 | // (using SHA-256) and outputs key material, as needed by QUIC. |
| 17 | // See https://tools.ietf.org/html/rfc5869 for details. |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 18 | class QUIC_EXPORT_PRIVATE QuicHKDF { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 19 | public: |
| 20 | // |secret|: the input shared secret (or, from RFC 5869, the IKM). |
| 21 | // |salt|: an (optional) public salt / non-secret random value. While |
| 22 | // optional, callers are strongly recommended to provide a salt. There is no |
| 23 | // added security value in making this larger than the SHA-256 block size of |
| 24 | // 64 bytes. |
| 25 | // |info|: an (optional) label to distinguish different uses of HKDF. It is |
| 26 | // optional context and application specific information (can be a zero-length |
| 27 | // string). |
| 28 | // |key_bytes_to_generate|: the number of bytes of key material to generate |
| 29 | // for both client and server. |
| 30 | // |iv_bytes_to_generate|: the number of bytes of IV to generate for both |
| 31 | // client and server. |
| 32 | // |subkey_secret_bytes_to_generate|: the number of bytes of subkey secret to |
| 33 | // generate, shared between client and server. |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 34 | QuicHKDF(quiche::QuicheStringPiece secret, |
| 35 | quiche::QuicheStringPiece salt, |
| 36 | quiche::QuicheStringPiece info, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 37 | size_t key_bytes_to_generate, |
| 38 | size_t iv_bytes_to_generate, |
| 39 | size_t subkey_secret_bytes_to_generate); |
| 40 | |
| 41 | // An alternative constructor that allows the client and server key/IV |
| 42 | // lengths to be different. |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 43 | QuicHKDF(quiche::QuicheStringPiece secret, |
| 44 | quiche::QuicheStringPiece salt, |
| 45 | quiche::QuicheStringPiece info, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 46 | size_t client_key_bytes_to_generate, |
| 47 | size_t server_key_bytes_to_generate, |
| 48 | size_t client_iv_bytes_to_generate, |
| 49 | size_t server_iv_bytes_to_generate, |
| 50 | size_t subkey_secret_bytes_to_generate); |
| 51 | |
| 52 | ~QuicHKDF(); |
| 53 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 54 | quiche::QuicheStringPiece client_write_key() const { |
| 55 | return client_write_key_; |
| 56 | } |
| 57 | quiche::QuicheStringPiece client_write_iv() const { return client_write_iv_; } |
| 58 | quiche::QuicheStringPiece server_write_key() const { |
| 59 | return server_write_key_; |
| 60 | } |
| 61 | quiche::QuicheStringPiece server_write_iv() const { return server_write_iv_; } |
| 62 | quiche::QuicheStringPiece subkey_secret() const { return subkey_secret_; } |
| 63 | quiche::QuicheStringPiece client_hp_key() const { return client_hp_key_; } |
| 64 | quiche::QuicheStringPiece server_hp_key() const { return server_hp_key_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | std::vector<uint8_t> output_; |
| 68 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 69 | quiche::QuicheStringPiece client_write_key_; |
| 70 | quiche::QuicheStringPiece server_write_key_; |
| 71 | quiche::QuicheStringPiece client_write_iv_; |
| 72 | quiche::QuicheStringPiece server_write_iv_; |
| 73 | quiche::QuicheStringPiece subkey_secret_; |
| 74 | quiche::QuicheStringPiece client_hp_key_; |
| 75 | quiche::QuicheStringPiece server_hp_key_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace quic |
| 79 | |
| 80 | #endif // QUICHE_QUIC_CORE_CRYPTO_QUIC_HKDF_H_ |