blob: 3cfc5aea336aa49c2a6a1c29f0fc65dfb0557aa6 [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útia2947ac2020-08-19 16:37:36 +020010 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020011 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
12 *
13 * This file is provided under the Apache License 2.0, or the
14 * GNU General Public License v2.0 or later.
15 *
16 * **********
17 * Apache License 2.0:
Thomas Fossati656864b2016-07-17 08:51:22 +010018 *
Ron Eldor420f3582019-07-31 13:58:29 +030019 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
Thomas Fossati656864b2016-07-17 08:51:22 +010022 *
Ron Eldor420f3582019-07-31 13:58:29 +030023 * http://www.apache.org/licenses/LICENSE-2.0
Thomas Fossati656864b2016-07-17 08:51:22 +010024 *
Ron Eldor420f3582019-07-31 13:58:29 +030025 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Thomas Fossati656864b2016-07-17 08:51:22 +010030 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020031 * **********
32 *
33 * **********
34 * GNU General Public License v2.0 or later:
35 *
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License along
47 * with this program; if not, write to the Free Software Foundation, Inc.,
48 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
49 *
50 * **********
Thomas Fossati656864b2016-07-17 08:51:22 +010051 */
52#ifndef MBEDTLS_HKDF_H
53#define MBEDTLS_HKDF_H
54
Ron Eldor9cbd1b22018-12-16 12:14:37 +020055#if !defined(MBEDTLS_CONFIG_FILE)
56#include "config.h"
57#else
58#include MBEDTLS_CONFIG_FILE
59#endif
60
Thomas Fossati656864b2016-07-17 08:51:22 +010061#include "md.h"
62
63/**
64 * \name HKDF Error codes
65 * \{
66 */
Gilles Peskine1990fab2021-07-26 18:48:10 +020067/** Bad input parameters to function. */
68#define MBEDTLS_ERR_HKDF_BAD_INPUT_DATA -0x5F80
Thomas Fossati656864b2016-07-17 08:51:22 +010069/* \} name */
70
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75/**
76 * \brief This is the HMAC-based Extract-and-Expand Key Derivation Function
77 * (HKDF).
78 *
79 * \param md A hash function; md.size denotes the length of the hash
80 * function output in bytes.
81 * \param salt An optional salt value (a non-secret random value);
82 * if the salt is not provided, a string of all zeros of
83 * md.size length is used as the salt.
84 * \param salt_len The length in bytes of the optional \p salt.
85 * \param ikm The input keying material.
86 * \param ikm_len The length in bytes of \p ikm.
87 * \param info An optional context and application specific information
88 * string. This can be a zero-length string.
89 * \param info_len The length of \p info in bytes.
90 * \param okm The output keying material of \p okm_len bytes.
91 * \param okm_len The length of the output keying material in bytes. This
92 * must be less than or equal to 255 * md.size bytes.
93 *
94 * \return 0 on success.
95 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
96 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
97 * MD layer.
98 */
99int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt,
100 size_t salt_len, const unsigned char *ikm, size_t ikm_len,
101 const unsigned char *info, size_t info_len,
102 unsigned char *okm, size_t okm_len );
103
104/**
105 * \brief Take the input keying material \p ikm and extract from it a
106 * fixed-length pseudorandom key \p prk.
107 *
Janos Follath08a4aeb2018-08-06 14:20:15 +0100108 * \warning This function should only be used if the security of it has been
109 * studied and established in that particular context (eg. TLS 1.3
110 * key schedule). For standard HKDF security guarantees use
111 * \c mbedtls_hkdf instead.
112 *
Thomas Fossati656864b2016-07-17 08:51:22 +0100113 * \param md A hash function; md.size denotes the length of the
114 * hash function output in bytes.
115 * \param salt An optional salt value (a non-secret random value);
116 * if the salt is not provided, a string of all zeros
117 * of md.size length is used as the salt.
118 * \param salt_len The length in bytes of the optional \p salt.
119 * \param ikm The input keying material.
120 * \param ikm_len The length in bytes of \p ikm.
121 * \param[out] prk A pseudorandom key of at least md.size bytes.
122 *
123 * \return 0 on success.
124 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
125 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
126 * MD layer.
127 */
128int mbedtls_hkdf_extract( const mbedtls_md_info_t *md,
129 const unsigned char *salt, size_t salt_len,
130 const unsigned char *ikm, size_t ikm_len,
131 unsigned char *prk );
132
133/**
134 * \brief Expand the supplied \p prk into several additional pseudorandom
135 * keys, which is the output of the HKDF.
136 *
Janos Follath08a4aeb2018-08-06 14:20:15 +0100137 * \warning This function should only be used if the security of it has been
138 * studied and established in that particular context (eg. TLS 1.3
139 * key schedule). For standard HKDF security guarantees use
140 * \c mbedtls_hkdf instead.
141 *
Thomas Fossati656864b2016-07-17 08:51:22 +0100142 * \param md A hash function; md.size denotes the length of the hash
143 * function output in bytes.
Janos Follathd0a78e92018-08-06 13:55:46 +0100144 * \param prk A pseudorandom key of at least md.size bytes. \p prk is
145 * usually the output from the HKDF extract step.
Thomas Fossati656864b2016-07-17 08:51:22 +0100146 * \param prk_len The length in bytes of \p prk.
147 * \param info An optional context and application specific information
148 * string. This can be a zero-length string.
149 * \param info_len The length of \p info in bytes.
150 * \param okm The output keying material of \p okm_len bytes.
151 * \param okm_len The length of the output keying material in bytes. This
152 * must be less than or equal to 255 * md.size bytes.
153 *
154 * \return 0 on success.
155 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
156 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
157 * MD layer.
158 */
159int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
160 size_t prk_len, const unsigned char *info,
161 size_t info_len, unsigned char *okm, size_t okm_len );
162
163#ifdef __cplusplus
164}
165#endif
166
167#endif /* hkdf.h */