blob: 0e75b71f277fa337690e798dd59f2090c5202958 [file] [log] [blame]
Thomas Fossati656864b2016-07-17 08:51:22 +01001/**
2 * \file hkdf.h
3 *
4 * \brief This file contains the HKDF interface.
5 *
6 * The HMAC-based Extract-and-Expand Key Derivation Function (HKDF) is
7 * specified by RFC 5869.
8 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Ron Eldor40244bc2019-07-31 13:58:29 +030011 * SPDX-License-Identifier: Apache-2.0
Thomas Fossati656864b2016-07-17 08:51:22 +010012 *
Ron Eldor40244bc2019-07-31 13:58:29 +030013 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
Thomas Fossati656864b2016-07-17 08:51:22 +010016 *
Ron Eldor40244bc2019-07-31 13:58:29 +030017 * http://www.apache.org/licenses/LICENSE-2.0
Thomas Fossati656864b2016-07-17 08:51:22 +010018 *
Ron Eldor40244bc2019-07-31 13:58:29 +030019 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Thomas Fossati656864b2016-07-17 08:51:22 +010024 */
25#ifndef MBEDTLS_HKDF_H
26#define MBEDTLS_HKDF_H
27
Bence Szépkútic662b362021-05-27 11:25:03 +020028#include "mbedtls/build_info.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050029
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/md.h"
Thomas Fossati656864b2016-07-17 08:51:22 +010031
32/**
33 * \name HKDF Error codes
34 * \{
35 */
Gilles Peskined2971572021-07-26 18:48:10 +020036/** Bad input parameters to function. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020037#define MBEDTLS_ERR_HKDF_BAD_INPUT_DATA -0x5F80
Thomas Fossati656864b2016-07-17 08:51:22 +010038/* \} name */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/**
45 * \brief This is the HMAC-based Extract-and-Expand Key Derivation Function
46 * (HKDF).
47 *
48 * \param md A hash function; md.size denotes the length of the hash
49 * function output in bytes.
50 * \param salt An optional salt value (a non-secret random value);
51 * if the salt is not provided, a string of all zeros of
52 * md.size length is used as the salt.
53 * \param salt_len The length in bytes of the optional \p salt.
54 * \param ikm The input keying material.
55 * \param ikm_len The length in bytes of \p ikm.
56 * \param info An optional context and application specific information
57 * string. This can be a zero-length string.
58 * \param info_len The length of \p info in bytes.
59 * \param okm The output keying material of \p okm_len bytes.
60 * \param okm_len The length of the output keying material in bytes. This
61 * must be less than or equal to 255 * md.size bytes.
62 *
63 * \return 0 on success.
64 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
65 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
66 * MD layer.
67 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068int mbedtls_hkdf(const mbedtls_md_info_t *md,
69 const unsigned char *salt,
70 size_t salt_len,
71 const unsigned char *ikm,
72 size_t ikm_len,
73 const unsigned char *info,
74 size_t info_len,
75 unsigned char *okm,
76 size_t okm_len);
Thomas Fossati656864b2016-07-17 08:51:22 +010077
78/**
79 * \brief Take the input keying material \p ikm and extract from it a
80 * fixed-length pseudorandom key \p prk.
81 *
Janos Follath08a4aeb2018-08-06 14:20:15 +010082 * \warning This function should only be used if the security of it has been
83 * studied and established in that particular context (eg. TLS 1.3
84 * key schedule). For standard HKDF security guarantees use
85 * \c mbedtls_hkdf instead.
86 *
Thomas Fossati656864b2016-07-17 08:51:22 +010087 * \param md A hash function; md.size denotes the length of the
88 * hash function output in bytes.
89 * \param salt An optional salt value (a non-secret random value);
90 * if the salt is not provided, a string of all zeros
91 * of md.size length is used as the salt.
92 * \param salt_len The length in bytes of the optional \p salt.
93 * \param ikm The input keying material.
94 * \param ikm_len The length in bytes of \p ikm.
95 * \param[out] prk A pseudorandom key of at least md.size bytes.
96 *
97 * \return 0 on success.
98 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
99 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
100 * MD layer.
101 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102int mbedtls_hkdf_extract(const mbedtls_md_info_t *md,
103 const unsigned char *salt,
104 size_t salt_len,
105 const unsigned char *ikm,
106 size_t ikm_len,
107 unsigned char *prk);
Thomas Fossati656864b2016-07-17 08:51:22 +0100108
109/**
110 * \brief Expand the supplied \p prk into several additional pseudorandom
111 * keys, which is the output of the HKDF.
112 *
Janos Follath08a4aeb2018-08-06 14:20:15 +0100113 * \warning This function should only be used if the security of it has been
114 * studied and established in that particular context (eg. TLS 1.3
115 * key schedule). For standard HKDF security guarantees use
116 * \c mbedtls_hkdf instead.
117 *
Thomas Fossati656864b2016-07-17 08:51:22 +0100118 * \param md A hash function; md.size denotes the length of the hash
119 * function output in bytes.
Janos Follathd0a78e92018-08-06 13:55:46 +0100120 * \param prk A pseudorandom key of at least md.size bytes. \p prk is
121 * usually the output from the HKDF extract step.
Thomas Fossati656864b2016-07-17 08:51:22 +0100122 * \param prk_len The length in bytes of \p prk.
123 * \param info An optional context and application specific information
124 * string. This can be a zero-length string.
125 * \param info_len The length of \p info in bytes.
126 * \param okm The output keying material of \p okm_len bytes.
127 * \param okm_len The length of the output keying material in bytes. This
128 * must be less than or equal to 255 * md.size bytes.
129 *
130 * \return 0 on success.
131 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
132 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
133 * MD layer.
134 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200135int mbedtls_hkdf_expand(const mbedtls_md_info_t *md,
136 const unsigned char *prk,
137 size_t prk_len,
138 const unsigned char *info,
139 size_t info_len,
140 unsigned char *okm,
141 size_t okm_len);
Thomas Fossati656864b2016-07-17 08:51:22 +0100142
143#ifdef __cplusplus
144}
145#endif
146
147#endif /* hkdf.h */