Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 1 | /** |
| 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 | /* |
Ron Eldor | 420f358 | 2019-07-31 13:58:29 +0300 | [diff] [blame] | 10 | * Copyright (C) 2016-2019, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 11 | * 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 Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 18 | * |
Ron Eldor | 420f358 | 2019-07-31 13:58:29 +0300 | [diff] [blame] | 19 | * 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 Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 22 | * |
Ron Eldor | 420f358 | 2019-07-31 13:58:29 +0300 | [diff] [blame] | 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 24 | * |
Ron Eldor | 420f358 | 2019-07-31 13:58:29 +0300 | [diff] [blame] | 25 | * 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 Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 30 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 31 | * ********** |
| 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 | * ********** |
| 51 | * |
Ron Eldor | 420f358 | 2019-07-31 13:58:29 +0300 | [diff] [blame] | 52 | * This file is part of mbed TLS (https://tls.mbed.org) |
Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 53 | */ |
| 54 | #ifndef MBEDTLS_HKDF_H |
| 55 | #define MBEDTLS_HKDF_H |
| 56 | |
Ron Eldor | 9cbd1b2 | 2018-12-16 12:14:37 +0200 | [diff] [blame] | 57 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 58 | #include "config.h" |
| 59 | #else |
| 60 | #include MBEDTLS_CONFIG_FILE |
| 61 | #endif |
| 62 | |
Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 63 | #include "md.h" |
| 64 | |
| 65 | /** |
| 66 | * \name HKDF Error codes |
| 67 | * \{ |
| 68 | */ |
| 69 | #define MBEDTLS_ERR_HKDF_BAD_INPUT_DATA -0x5F80 /**< Bad input parameters to function. */ |
| 70 | /* \} name */ |
| 71 | |
| 72 | #ifdef __cplusplus |
| 73 | extern "C" { |
| 74 | #endif |
| 75 | |
| 76 | /** |
| 77 | * \brief This is the HMAC-based Extract-and-Expand Key Derivation Function |
| 78 | * (HKDF). |
| 79 | * |
| 80 | * \param md A hash function; md.size denotes the length of the hash |
| 81 | * function output in bytes. |
| 82 | * \param salt An optional salt value (a non-secret random value); |
| 83 | * if the salt is not provided, a string of all zeros of |
| 84 | * md.size length is used as the salt. |
| 85 | * \param salt_len The length in bytes of the optional \p salt. |
| 86 | * \param ikm The input keying material. |
| 87 | * \param ikm_len The length in bytes of \p ikm. |
| 88 | * \param info An optional context and application specific information |
| 89 | * string. This can be a zero-length string. |
| 90 | * \param info_len The length of \p info in bytes. |
| 91 | * \param okm The output keying material of \p okm_len bytes. |
| 92 | * \param okm_len The length of the output keying material in bytes. This |
| 93 | * must be less than or equal to 255 * md.size bytes. |
| 94 | * |
| 95 | * \return 0 on success. |
| 96 | * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid. |
| 97 | * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying |
| 98 | * MD layer. |
| 99 | */ |
| 100 | int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt, |
| 101 | size_t salt_len, const unsigned char *ikm, size_t ikm_len, |
| 102 | const unsigned char *info, size_t info_len, |
| 103 | unsigned char *okm, size_t okm_len ); |
| 104 | |
| 105 | /** |
| 106 | * \brief Take the input keying material \p ikm and extract from it a |
| 107 | * fixed-length pseudorandom key \p prk. |
| 108 | * |
Janos Follath | 08a4aeb | 2018-08-06 14:20:15 +0100 | [diff] [blame] | 109 | * \warning This function should only be used if the security of it has been |
| 110 | * studied and established in that particular context (eg. TLS 1.3 |
| 111 | * key schedule). For standard HKDF security guarantees use |
| 112 | * \c mbedtls_hkdf instead. |
| 113 | * |
Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 114 | * \param md A hash function; md.size denotes the length of the |
| 115 | * hash function output in bytes. |
| 116 | * \param salt An optional salt value (a non-secret random value); |
| 117 | * if the salt is not provided, a string of all zeros |
| 118 | * of md.size length is used as the salt. |
| 119 | * \param salt_len The length in bytes of the optional \p salt. |
| 120 | * \param ikm The input keying material. |
| 121 | * \param ikm_len The length in bytes of \p ikm. |
| 122 | * \param[out] prk A pseudorandom key of at least md.size bytes. |
| 123 | * |
| 124 | * \return 0 on success. |
| 125 | * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid. |
| 126 | * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying |
| 127 | * MD layer. |
| 128 | */ |
| 129 | int mbedtls_hkdf_extract( const mbedtls_md_info_t *md, |
| 130 | const unsigned char *salt, size_t salt_len, |
| 131 | const unsigned char *ikm, size_t ikm_len, |
| 132 | unsigned char *prk ); |
| 133 | |
| 134 | /** |
| 135 | * \brief Expand the supplied \p prk into several additional pseudorandom |
| 136 | * keys, which is the output of the HKDF. |
| 137 | * |
Janos Follath | 08a4aeb | 2018-08-06 14:20:15 +0100 | [diff] [blame] | 138 | * \warning This function should only be used if the security of it has been |
| 139 | * studied and established in that particular context (eg. TLS 1.3 |
| 140 | * key schedule). For standard HKDF security guarantees use |
| 141 | * \c mbedtls_hkdf instead. |
| 142 | * |
Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 143 | * \param md A hash function; md.size denotes the length of the hash |
| 144 | * function output in bytes. |
Janos Follath | d0a78e9 | 2018-08-06 13:55:46 +0100 | [diff] [blame] | 145 | * \param prk A pseudorandom key of at least md.size bytes. \p prk is |
| 146 | * usually the output from the HKDF extract step. |
Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 147 | * \param prk_len The length in bytes of \p prk. |
| 148 | * \param info An optional context and application specific information |
| 149 | * string. This can be a zero-length string. |
| 150 | * \param info_len The length of \p info in bytes. |
| 151 | * \param okm The output keying material of \p okm_len bytes. |
| 152 | * \param okm_len The length of the output keying material in bytes. This |
| 153 | * must be less than or equal to 255 * md.size bytes. |
| 154 | * |
| 155 | * \return 0 on success. |
| 156 | * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid. |
| 157 | * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying |
| 158 | * MD layer. |
| 159 | */ |
| 160 | int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk, |
| 161 | size_t prk_len, const unsigned char *info, |
| 162 | size_t info_len, unsigned char *okm, size_t okm_len ); |
| 163 | |
| 164 | #ifdef __cplusplus |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | #endif /* hkdf.h */ |