blob: 48143fa66d48f40b08356556e10c1c720ef593cb [file] [log] [blame]
Thomas Fossati656864b2016-07-17 08:51:22 +01001/*
2 * HKDF implementation -- RFC 5869
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Thomas Fossati656864b2016-07-17 08:51:22 +01005 * 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 Fossati656864b2016-07-17 08:51:22 +010018 */
Gilles Peskinedb09ef62020-06-03 01:43:33 +020019#include "common.h"
Thomas Fossati656864b2016-07-17 08:51:22 +010020
21#if defined(MBEDTLS_HKDF_C)
22
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023# include <string.h>
24# include "mbedtls/hkdf.h"
25# include "mbedtls/platform_util.h"
26# include "mbedtls/error.h"
Thomas Fossati656864b2016-07-17 08:51:22 +010027
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020028int mbedtls_hkdf(const mbedtls_md_info_t *md,
29 const unsigned char *salt,
30 size_t salt_len,
31 const unsigned char *ikm,
32 size_t ikm_len,
33 const unsigned char *info,
34 size_t info_len,
35 unsigned char *okm,
36 size_t okm_len)
Thomas Fossati656864b2016-07-17 08:51:22 +010037{
Janos Follath24eed8d2019-11-22 13:21:35 +000038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Thomas Fossati656864b2016-07-17 08:51:22 +010039 unsigned char prk[MBEDTLS_MD_MAX_SIZE];
40
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020041 ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk);
Thomas Fossati656864b2016-07-17 08:51:22 +010042
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020043 if (ret == 0) {
44 ret = mbedtls_hkdf_expand(md, prk, mbedtls_md_get_size(md), info,
45 info_len, okm, okm_len);
Thomas Fossati656864b2016-07-17 08:51:22 +010046 }
47
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048 mbedtls_platform_zeroize(prk, sizeof(prk));
Thomas Fossati656864b2016-07-17 08:51:22 +010049
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050 return ret;
Thomas Fossati656864b2016-07-17 08:51:22 +010051}
52
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053int mbedtls_hkdf_extract(const mbedtls_md_info_t *md,
54 const unsigned char *salt,
55 size_t salt_len,
56 const unsigned char *ikm,
57 size_t ikm_len,
58 unsigned char *prk)
Thomas Fossati656864b2016-07-17 08:51:22 +010059{
60 unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
61
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020062 if (salt == NULL) {
Thomas Fossati656864b2016-07-17 08:51:22 +010063 size_t hash_len;
64
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065 if (salt_len != 0) {
Brian J Murrayca2ea4e2018-07-06 10:03:58 -070066 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
67 }
68
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069 hash_len = mbedtls_md_get_size(md);
Thomas Fossati656864b2016-07-17 08:51:22 +010070
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020071 if (hash_len == 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +010072 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
73 }
74
75 salt = null_salt;
76 salt_len = hash_len;
77 }
78
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020079 return mbedtls_md_hmac(md, salt, salt_len, ikm, ikm_len, prk);
Thomas Fossati656864b2016-07-17 08:51:22 +010080}
81
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082int mbedtls_hkdf_expand(const mbedtls_md_info_t *md,
83 const unsigned char *prk,
84 size_t prk_len,
85 const unsigned char *info,
86 size_t info_len,
87 unsigned char *okm,
88 size_t okm_len)
Thomas Fossati656864b2016-07-17 08:51:22 +010089{
90 size_t hash_len;
91 size_t where = 0;
92 size_t n;
93 size_t t_len = 0;
94 size_t i;
95 int ret = 0;
96 mbedtls_md_context_t ctx;
97 unsigned char t[MBEDTLS_MD_MAX_SIZE];
98
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020099 if (okm == NULL) {
100 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
Thomas Fossati656864b2016-07-17 08:51:22 +0100101 }
102
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200103 hash_len = mbedtls_md_get_size(md);
Thomas Fossati656864b2016-07-17 08:51:22 +0100104
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105 if (prk_len < hash_len || hash_len == 0) {
106 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
Thomas Fossati656864b2016-07-17 08:51:22 +0100107 }
108
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 if (info == NULL) {
110 info = (const unsigned char *)"";
Thomas Fossati656864b2016-07-17 08:51:22 +0100111 info_len = 0;
112 }
113
114 n = okm_len / hash_len;
115
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200116 if (okm_len % hash_len != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100117 n++;
118 }
119
Brian J Murraya61d1232018-07-06 10:02:39 -0700120 /*
121 * Per RFC 5869 Section 2.3, okm_len must not exceed
122 * 255 times the hash length
123 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124 if (n > 255) {
125 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
Thomas Fossati656864b2016-07-17 08:51:22 +0100126 }
127
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128 mbedtls_md_init(&ctx);
Thomas Fossati656864b2016-07-17 08:51:22 +0100129
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130 if ((ret = mbedtls_md_setup(&ctx, md, 1)) != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100131 goto exit;
132 }
133
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134 memset(t, 0, hash_len);
Gilles Peskine3ab121a2020-04-02 19:51:05 +0200135
Brian J Murraya61d1232018-07-06 10:02:39 -0700136 /*
137 * Compute T = T(1) | T(2) | T(3) | ... | T(N)
138 * Where T(N) is defined in RFC 5869 Section 2.3
139 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200140 for (i = 1; i <= n; i++) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100141 size_t num_to_copy;
142 unsigned char c = i & 0xff;
143
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 ret = mbedtls_md_hmac_starts(&ctx, prk, prk_len);
145 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100146 goto exit;
147 }
148
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149 ret = mbedtls_md_hmac_update(&ctx, t, t_len);
150 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100151 goto exit;
152 }
153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 ret = mbedtls_md_hmac_update(&ctx, info, info_len);
155 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100156 goto exit;
157 }
158
Brian J Murraya61d1232018-07-06 10:02:39 -0700159 /* The constant concatenated to the end of each T(n) is a single octet.
Thomas Fossati656864b2016-07-17 08:51:22 +0100160 * */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200161 ret = mbedtls_md_hmac_update(&ctx, &c, 1);
162 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100163 goto exit;
164 }
165
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200166 ret = mbedtls_md_hmac_finish(&ctx, t);
167 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100168 goto exit;
169 }
170
171 num_to_copy = i != n ? hash_len : okm_len - where;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200172 memcpy(okm + where, t, num_to_copy);
Thomas Fossati656864b2016-07-17 08:51:22 +0100173 where += hash_len;
174 t_len = hash_len;
175 }
176
177exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200178 mbedtls_md_free(&ctx);
179 mbedtls_platform_zeroize(t, sizeof(t));
Thomas Fossati656864b2016-07-17 08:51:22 +0100180
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181 return ret;
Thomas Fossati656864b2016-07-17 08:51:22 +0100182}
183
184#endif /* MBEDTLS_HKDF_C */