| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | *  HKDF implementation -- RFC 5869 | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 18 | */ | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 19 | #include "common.h" | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 20 |  | 
|  | 21 | #if defined(MBEDTLS_HKDF_C) | 
|  | 22 |  | 
|  | 23 | #include <string.h> | 
|  | 24 | #include "mbedtls/hkdf.h" | 
|  | 25 | #include "mbedtls/platform_util.h" | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 26 | #include "mbedtls/error.h" | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 27 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 28 | int mbedtls_hkdf(const mbedtls_md_info_t *md, const unsigned char *salt, | 
|  | 29 | size_t salt_len, const unsigned char *ikm, size_t ikm_len, | 
|  | 30 | const unsigned char *info, size_t info_len, | 
|  | 31 | unsigned char *okm, size_t okm_len) | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 32 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 33 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 34 | unsigned char prk[MBEDTLS_MD_MAX_SIZE]; | 
|  | 35 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 37 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | if (ret == 0) { | 
|  | 39 | ret = mbedtls_hkdf_expand(md, prk, mbedtls_md_get_size(md), | 
|  | 40 | info, info_len, okm, okm_len); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | mbedtls_platform_zeroize(prk, sizeof(prk)); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 44 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | return ret; | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | int mbedtls_hkdf_extract(const mbedtls_md_info_t *md, | 
|  | 49 | const unsigned char *salt, size_t salt_len, | 
|  | 50 | const unsigned char *ikm, size_t ikm_len, | 
|  | 51 | unsigned char *prk) | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 52 | { | 
|  | 53 | unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' }; | 
|  | 54 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | if (salt == NULL) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 56 | size_t hash_len; | 
|  | 57 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | if (salt_len != 0) { | 
| Brian J Murray | ca2ea4e | 2018-07-06 10:03:58 -0700 | [diff] [blame] | 59 | return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA; | 
|  | 60 | } | 
|  | 61 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | hash_len = mbedtls_md_get_size(md); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 63 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | if (hash_len == 0) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 65 | return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | salt = null_salt; | 
|  | 69 | salt_len = hash_len; | 
|  | 70 | } | 
|  | 71 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | return mbedtls_md_hmac(md, salt, salt_len, ikm, ikm_len, prk); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | int mbedtls_hkdf_expand(const mbedtls_md_info_t *md, const unsigned char *prk, | 
|  | 76 | size_t prk_len, const unsigned char *info, | 
|  | 77 | size_t info_len, unsigned char *okm, size_t okm_len) | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 78 | { | 
|  | 79 | size_t hash_len; | 
|  | 80 | size_t where = 0; | 
|  | 81 | size_t n; | 
|  | 82 | size_t t_len = 0; | 
|  | 83 | size_t i; | 
|  | 84 | int ret = 0; | 
|  | 85 | mbedtls_md_context_t ctx; | 
|  | 86 | unsigned char t[MBEDTLS_MD_MAX_SIZE]; | 
|  | 87 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | if (okm == NULL) { | 
|  | 89 | return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA; | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | hash_len = mbedtls_md_get_size(md); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 93 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | if (prk_len < hash_len || hash_len == 0) { | 
|  | 95 | return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA; | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | if (info == NULL) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 99 | info = (const unsigned char *) ""; | 
|  | 100 | info_len = 0; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | n = okm_len / hash_len; | 
|  | 104 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | if (okm_len % hash_len != 0) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 106 | n++; | 
|  | 107 | } | 
|  | 108 |  | 
| Brian J Murray | a61d123 | 2018-07-06 10:02:39 -0700 | [diff] [blame] | 109 | /* | 
|  | 110 | * Per RFC 5869 Section 2.3, okm_len must not exceed | 
|  | 111 | * 255 times the hash length | 
|  | 112 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | if (n > 255) { | 
|  | 114 | return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA; | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | mbedtls_md_init(&ctx); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 118 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | if ((ret = mbedtls_md_setup(&ctx, md, 1)) != 0) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 120 | goto exit; | 
|  | 121 | } | 
|  | 122 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | memset(t, 0, hash_len); | 
| Gilles Peskine | 3ab121a | 2020-04-02 19:51:05 +0200 | [diff] [blame] | 124 |  | 
| Brian J Murray | a61d123 | 2018-07-06 10:02:39 -0700 | [diff] [blame] | 125 | /* | 
|  | 126 | * Compute T = T(1) | T(2) | T(3) | ... | T(N) | 
|  | 127 | * Where T(N) is defined in RFC 5869 Section 2.3 | 
|  | 128 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | for (i = 1; i <= n; i++) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 130 | size_t num_to_copy; | 
|  | 131 | unsigned char c = i & 0xff; | 
|  | 132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | ret = mbedtls_md_hmac_starts(&ctx, prk, prk_len); | 
|  | 134 | if (ret != 0) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 135 | goto exit; | 
|  | 136 | } | 
|  | 137 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | ret = mbedtls_md_hmac_update(&ctx, t, t_len); | 
|  | 139 | if (ret != 0) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 140 | goto exit; | 
|  | 141 | } | 
|  | 142 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | ret = mbedtls_md_hmac_update(&ctx, info, info_len); | 
|  | 144 | if (ret != 0) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 145 | goto exit; | 
|  | 146 | } | 
|  | 147 |  | 
| Brian J Murray | a61d123 | 2018-07-06 10:02:39 -0700 | [diff] [blame] | 148 | /* The constant concatenated to the end of each T(n) is a single octet. | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 149 | * */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | ret = mbedtls_md_hmac_update(&ctx, &c, 1); | 
|  | 151 | if (ret != 0) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 152 | goto exit; | 
|  | 153 | } | 
|  | 154 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | ret = mbedtls_md_hmac_finish(&ctx, t); | 
|  | 156 | if (ret != 0) { | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 157 | goto exit; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | num_to_copy = i != n ? hash_len : okm_len - where; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | memcpy(okm + where, t, num_to_copy); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 162 | where += hash_len; | 
|  | 163 | t_len = hash_len; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | mbedtls_md_free(&ctx); | 
|  | 168 | mbedtls_platform_zeroize(t, sizeof(t)); | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 169 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | return ret; | 
| Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
|  | 173 | #endif /* MBEDTLS_HKDF_C */ |