Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [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 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "absl/strings/string_view.h" |
| 11 | #include "quiche/quic/platform/api/quic_export.h" |
| 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. |
| 18 | class QUIC_EXPORT_PRIVATE QuicHKDF { |
| 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. |
bnc | b91850e | 2022-04-13 08:34:05 -0700 | [diff] [blame] | 34 | QuicHKDF(absl::string_view secret, absl::string_view salt, |
| 35 | absl::string_view info, size_t key_bytes_to_generate, |
| 36 | size_t iv_bytes_to_generate, size_t subkey_secret_bytes_to_generate); |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 37 | |
| 38 | // An alternative constructor that allows the client and server key/IV |
| 39 | // lengths to be different. |
bnc | b91850e | 2022-04-13 08:34:05 -0700 | [diff] [blame] | 40 | QuicHKDF(absl::string_view secret, absl::string_view salt, |
| 41 | absl::string_view info, size_t client_key_bytes_to_generate, |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 42 | size_t server_key_bytes_to_generate, |
| 43 | size_t client_iv_bytes_to_generate, |
| 44 | size_t server_iv_bytes_to_generate, |
| 45 | size_t subkey_secret_bytes_to_generate); |
| 46 | |
| 47 | ~QuicHKDF(); |
| 48 | |
| 49 | absl::string_view client_write_key() const { return client_write_key_; } |
| 50 | absl::string_view client_write_iv() const { return client_write_iv_; } |
| 51 | absl::string_view server_write_key() const { return server_write_key_; } |
| 52 | absl::string_view server_write_iv() const { return server_write_iv_; } |
| 53 | absl::string_view subkey_secret() const { return subkey_secret_; } |
| 54 | absl::string_view client_hp_key() const { return client_hp_key_; } |
| 55 | absl::string_view server_hp_key() const { return server_hp_key_; } |
| 56 | |
| 57 | private: |
| 58 | std::vector<uint8_t> output_; |
| 59 | |
| 60 | absl::string_view client_write_key_; |
| 61 | absl::string_view server_write_key_; |
| 62 | absl::string_view client_write_iv_; |
| 63 | absl::string_view server_write_iv_; |
| 64 | absl::string_view subkey_secret_; |
| 65 | absl::string_view client_hp_key_; |
| 66 | absl::string_view server_hp_key_; |
| 67 | }; |
| 68 | |
| 69 | } // namespace quic |
| 70 | |
| 71 | #endif // QUICHE_QUIC_CORE_CRYPTO_QUIC_HKDF_H_ |