blob: b8c07c6c6694f57d009cf7ea93c9c5772b25184e [file] [log] [blame]
[email protected]48b581d2013-02-23 06:47:221// 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 CRYPTO_HKDF_H_
6#define CRYPTO_HKDF_H_
7
avidd373b8b2015-12-21 21:34:438#include <stddef.h>
9#include <stdint.h>
10
[email protected]48b581d2013-02-23 06:47:2211#include <vector>
12
[email protected]daf079a2013-04-17 21:42:4013#include "base/strings/string_piece.h"
[email protected]48b581d2013-02-23 06:47:2214#include "crypto/crypto_export.h"
15
16namespace crypto {
17
18// HKDF implements the key derivation function specified in RFC 5869 (using
19// SHA-256) and outputs key material, as needed by QUIC.
20// See https://tools.ietf.org/html/rfc5869 for details.
21class CRYPTO_EXPORT HKDF {
22 public:
[email protected]2fe8b632014-07-31 11:36:3723 // |secret|: the input shared secret (or, from RFC 5869, the IKM).
[email protected]48b581d2013-02-23 06:47:2224 // |salt|: an (optional) public salt / non-secret random value. While
25 // optional, callers are strongly recommended to provide a salt. There is no
26 // added security value in making this larger than the SHA-256 block size of
27 // 64 bytes.
28 // |info|: an (optional) label to distinguish different uses of HKDF. It is
29 // optional context and application specific information (can be a zero-length
30 // string).
[email protected]2fe8b632014-07-31 11:36:3731 // |key_bytes_to_generate|: the number of bytes of key material to generate
32 // for both client and server.
33 // |iv_bytes_to_generate|: the number of bytes of IV to generate for both
34 // client and server.
35 // |subkey_secret_bytes_to_generate|: the number of bytes of subkey secret to
36 // generate, shared between client and server.
[email protected]48b581d2013-02-23 06:47:2237 HKDF(const base::StringPiece& secret,
38 const base::StringPiece& salt,
39 const base::StringPiece& info,
40 size_t key_bytes_to_generate,
[email protected]2fe8b632014-07-31 11:36:3741 size_t iv_bytes_to_generate,
42 size_t subkey_secret_bytes_to_generate);
rch0db8686b2016-04-21 21:51:3743
44 // An alternative constructor that allows the client and server key/IV
45 // lengths to be different.
46 HKDF(const base::StringPiece& secret,
47 const base::StringPiece& salt,
48 const base::StringPiece& info,
49 size_t client_key_bytes_to_generate,
50 size_t server_key_bytes_to_generate,
51 size_t client_iv_bytes_to_generate,
52 size_t server_iv_bytes_to_generate,
53 size_t subkey_secret_bytes_to_generate);
[email protected]acb66cb2013-02-27 02:13:1354 ~HKDF();
[email protected]48b581d2013-02-23 06:47:2255
56 base::StringPiece client_write_key() const {
57 return client_write_key_;
58 }
59 base::StringPiece client_write_iv() const {
60 return client_write_iv_;
61 }
62 base::StringPiece server_write_key() const {
63 return server_write_key_;
64 }
65 base::StringPiece server_write_iv() const {
66 return server_write_iv_;
67 }
[email protected]2fe8b632014-07-31 11:36:3768 base::StringPiece subkey_secret() const {
69 return subkey_secret_;
70 }
[email protected]48b581d2013-02-23 06:47:2271
72 private:
avidd373b8b2015-12-21 21:34:4373 std::vector<uint8_t> output_;
[email protected]48b581d2013-02-23 06:47:2274
75 base::StringPiece client_write_key_;
76 base::StringPiece server_write_key_;
77 base::StringPiece client_write_iv_;
78 base::StringPiece server_write_iv_;
[email protected]2fe8b632014-07-31 11:36:3779 base::StringPiece subkey_secret_;
[email protected]48b581d2013-02-23 06:47:2280};
81
82} // namespace crypto
83
84#endif // CRYPTO_HKDF_H_