blob: 103f329b8fa193f5634e59850b48874b82deface [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
Dave Rodgman7ff79652023-11-03 12:04:52 +000011 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Thomas Fossati656864b2016-07-17 08:51:22 +010012 */
13#ifndef MBEDTLS_HKDF_H
14#define MBEDTLS_HKDF_H
15
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050016#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010017#include "mbedtls/config.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050018#else
19#include MBEDTLS_CONFIG_FILE
20#endif
21
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010022#include "mbedtls/md.h"
Thomas Fossati656864b2016-07-17 08:51:22 +010023
24/**
25 * \name HKDF Error codes
26 * \{
27 */
Gilles Peskinea3974432021-07-26 18:48:10 +020028/** Bad input parameters to function. */
29#define MBEDTLS_ERR_HKDF_BAD_INPUT_DATA -0x5F80
Andrzej Kurek73afe272022-01-24 10:31:06 -050030/** \} name */
Thomas Fossati656864b2016-07-17 08:51:22 +010031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/**
37 * \brief This is the HMAC-based Extract-and-Expand Key Derivation Function
38 * (HKDF).
39 *
40 * \param md A hash function; md.size denotes the length of the hash
41 * function output in bytes.
42 * \param salt An optional salt value (a non-secret random value);
43 * if the salt is not provided, a string of all zeros of
44 * md.size length is used as the salt.
45 * \param salt_len The length in bytes of the optional \p salt.
46 * \param ikm The input keying material.
47 * \param ikm_len The length in bytes of \p ikm.
48 * \param info An optional context and application specific information
49 * string. This can be a zero-length string.
50 * \param info_len The length of \p info in bytes.
51 * \param okm The output keying material of \p okm_len bytes.
52 * \param okm_len The length of the output keying material in bytes. This
53 * must be less than or equal to 255 * md.size bytes.
54 *
55 * \return 0 on success.
56 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
57 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
58 * MD layer.
59 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060int mbedtls_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt,
61 size_t salt_len, const unsigned char *ikm, size_t ikm_len,
62 const unsigned char *info, size_t info_len,
63 unsigned char *okm, size_t okm_len);
Thomas Fossati656864b2016-07-17 08:51:22 +010064
65/**
66 * \brief Take the input keying material \p ikm and extract from it a
67 * fixed-length pseudorandom key \p prk.
68 *
Janos Follath08a4aeb2018-08-06 14:20:15 +010069 * \warning This function should only be used if the security of it has been
70 * studied and established in that particular context (eg. TLS 1.3
71 * key schedule). For standard HKDF security guarantees use
72 * \c mbedtls_hkdf instead.
73 *
Thomas Fossati656864b2016-07-17 08:51:22 +010074 * \param md A hash function; md.size denotes the length of the
75 * hash function output in bytes.
76 * \param salt An optional salt value (a non-secret random value);
77 * if the salt is not provided, a string of all zeros
78 * of md.size length is used as the salt.
79 * \param salt_len The length in bytes of the optional \p salt.
80 * \param ikm The input keying material.
81 * \param ikm_len The length in bytes of \p ikm.
82 * \param[out] prk A pseudorandom key of at least md.size bytes.
83 *
84 * \return 0 on success.
85 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
86 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
87 * MD layer.
88 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089int mbedtls_hkdf_extract(const mbedtls_md_info_t *md,
90 const unsigned char *salt, size_t salt_len,
91 const unsigned char *ikm, size_t ikm_len,
92 unsigned char *prk);
Thomas Fossati656864b2016-07-17 08:51:22 +010093
94/**
95 * \brief Expand the supplied \p prk into several additional pseudorandom
96 * keys, which is the output of the HKDF.
97 *
Janos Follath08a4aeb2018-08-06 14:20:15 +010098 * \warning This function should only be used if the security of it has been
99 * studied and established in that particular context (eg. TLS 1.3
100 * key schedule). For standard HKDF security guarantees use
101 * \c mbedtls_hkdf instead.
102 *
Thomas Fossati656864b2016-07-17 08:51:22 +0100103 * \param md A hash function; md.size denotes the length of the hash
104 * function output in bytes.
Janos Follathd0a78e92018-08-06 13:55:46 +0100105 * \param prk A pseudorandom key of at least md.size bytes. \p prk is
106 * usually the output from the HKDF extract step.
Thomas Fossati656864b2016-07-17 08:51:22 +0100107 * \param prk_len The length in bytes of \p prk.
108 * \param info An optional context and application specific information
109 * string. This can be a zero-length string.
110 * \param info_len The length of \p info in bytes.
111 * \param okm The output keying material of \p okm_len bytes.
112 * \param okm_len The length of the output keying material in bytes. This
113 * must be less than or equal to 255 * md.size bytes.
114 *
115 * \return 0 on success.
116 * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid.
117 * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying
118 * MD layer.
119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120int mbedtls_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk,
121 size_t prk_len, const unsigned char *info,
122 size_t info_len, unsigned char *okm, size_t okm_len);
Thomas Fossati656864b2016-07-17 08:51:22 +0100123
124#ifdef __cplusplus
125}
126#endif
127
128#endif /* hkdf.h */