blob: a3f071ecef4609566314458d78ffc8c86948d694 [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
23#include <string.h>
24#include "mbedtls/hkdf.h"
25#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000026#include "mbedtls/error.h"
Thomas Fossati656864b2016-07-17 08:51:22 +010027
David Horstmannceeaeb92023-01-05 15:44:23 +000028int 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 Fossati656864b2016-07-17 08:51:22 +010032{
Janos Follath24eed8d2019-11-22 13:21:35 +000033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Thomas Fossati656864b2016-07-17 08:51:22 +010034 unsigned char prk[MBEDTLS_MD_MAX_SIZE];
35
David Horstmannceeaeb92023-01-05 15:44:23 +000036 ret = mbedtls_hkdf_extract(md, salt, salt_len, ikm, ikm_len, prk);
Thomas Fossati656864b2016-07-17 08:51:22 +010037
David Horstmannceeaeb92023-01-05 15:44:23 +000038 if (ret == 0) {
39 ret = mbedtls_hkdf_expand(md, prk, mbedtls_md_get_size(md),
40 info, info_len, okm, okm_len);
Thomas Fossati656864b2016-07-17 08:51:22 +010041 }
42
David Horstmannceeaeb92023-01-05 15:44:23 +000043 mbedtls_platform_zeroize(prk, sizeof(prk));
Thomas Fossati656864b2016-07-17 08:51:22 +010044
David Horstmannceeaeb92023-01-05 15:44:23 +000045 return ret;
Thomas Fossati656864b2016-07-17 08:51:22 +010046}
47
David Horstmannceeaeb92023-01-05 15:44:23 +000048int 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 Fossati656864b2016-07-17 08:51:22 +010052{
53 unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
54
David Horstmannceeaeb92023-01-05 15:44:23 +000055 if (salt == NULL) {
Thomas Fossati656864b2016-07-17 08:51:22 +010056 size_t hash_len;
57
David Horstmannceeaeb92023-01-05 15:44:23 +000058 if (salt_len != 0) {
Brian J Murrayca2ea4e2018-07-06 10:03:58 -070059 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
60 }
61
David Horstmannceeaeb92023-01-05 15:44:23 +000062 hash_len = mbedtls_md_get_size(md);
Thomas Fossati656864b2016-07-17 08:51:22 +010063
David Horstmannceeaeb92023-01-05 15:44:23 +000064 if (hash_len == 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +010065 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
66 }
67
68 salt = null_salt;
69 salt_len = hash_len;
70 }
71
David Horstmannceeaeb92023-01-05 15:44:23 +000072 return mbedtls_md_hmac(md, salt, salt_len, ikm, ikm_len, prk);
Thomas Fossati656864b2016-07-17 08:51:22 +010073}
74
David Horstmannceeaeb92023-01-05 15:44:23 +000075int 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 Fossati656864b2016-07-17 08:51:22 +010078{
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
David Horstmannceeaeb92023-01-05 15:44:23 +000088 if (okm == NULL) {
89 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
Thomas Fossati656864b2016-07-17 08:51:22 +010090 }
91
David Horstmannceeaeb92023-01-05 15:44:23 +000092 hash_len = mbedtls_md_get_size(md);
Thomas Fossati656864b2016-07-17 08:51:22 +010093
David Horstmannceeaeb92023-01-05 15:44:23 +000094 if (prk_len < hash_len || hash_len == 0) {
95 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
Thomas Fossati656864b2016-07-17 08:51:22 +010096 }
97
David Horstmannceeaeb92023-01-05 15:44:23 +000098 if (info == NULL) {
Thomas Fossati656864b2016-07-17 08:51:22 +010099 info = (const unsigned char *) "";
100 info_len = 0;
101 }
102
103 n = okm_len / hash_len;
104
David Horstmannceeaeb92023-01-05 15:44:23 +0000105 if (okm_len % hash_len != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100106 n++;
107 }
108
Brian J Murraya61d1232018-07-06 10:02:39 -0700109 /*
110 * Per RFC 5869 Section 2.3, okm_len must not exceed
111 * 255 times the hash length
112 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000113 if (n > 255) {
114 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
Thomas Fossati656864b2016-07-17 08:51:22 +0100115 }
116
David Horstmannceeaeb92023-01-05 15:44:23 +0000117 mbedtls_md_init(&ctx);
Thomas Fossati656864b2016-07-17 08:51:22 +0100118
David Horstmannceeaeb92023-01-05 15:44:23 +0000119 if ((ret = mbedtls_md_setup(&ctx, md, 1)) != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100120 goto exit;
121 }
122
David Horstmannceeaeb92023-01-05 15:44:23 +0000123 memset(t, 0, hash_len);
Gilles Peskine3ab121a2020-04-02 19:51:05 +0200124
Brian J Murraya61d1232018-07-06 10:02:39 -0700125 /*
126 * Compute T = T(1) | T(2) | T(3) | ... | T(N)
127 * Where T(N) is defined in RFC 5869 Section 2.3
128 */
David Horstmannceeaeb92023-01-05 15:44:23 +0000129 for (i = 1; i <= n; i++) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100130 size_t num_to_copy;
131 unsigned char c = i & 0xff;
132
David Horstmannceeaeb92023-01-05 15:44:23 +0000133 ret = mbedtls_md_hmac_starts(&ctx, prk, prk_len);
134 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100135 goto exit;
136 }
137
David Horstmannceeaeb92023-01-05 15:44:23 +0000138 ret = mbedtls_md_hmac_update(&ctx, t, t_len);
139 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100140 goto exit;
141 }
142
David Horstmannceeaeb92023-01-05 15:44:23 +0000143 ret = mbedtls_md_hmac_update(&ctx, info, info_len);
144 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100145 goto exit;
146 }
147
Brian J Murraya61d1232018-07-06 10:02:39 -0700148 /* The constant concatenated to the end of each T(n) is a single octet.
Thomas Fossati656864b2016-07-17 08:51:22 +0100149 * */
David Horstmannceeaeb92023-01-05 15:44:23 +0000150 ret = mbedtls_md_hmac_update(&ctx, &c, 1);
151 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100152 goto exit;
153 }
154
David Horstmannceeaeb92023-01-05 15:44:23 +0000155 ret = mbedtls_md_hmac_finish(&ctx, t);
156 if (ret != 0) {
Thomas Fossati656864b2016-07-17 08:51:22 +0100157 goto exit;
158 }
159
160 num_to_copy = i != n ? hash_len : okm_len - where;
David Horstmannceeaeb92023-01-05 15:44:23 +0000161 memcpy(okm + where, t, num_to_copy);
Thomas Fossati656864b2016-07-17 08:51:22 +0100162 where += hash_len;
163 t_len = hash_len;
164 }
165
166exit:
David Horstmannceeaeb92023-01-05 15:44:23 +0000167 mbedtls_md_free(&ctx);
168 mbedtls_platform_zeroize(t, sizeof(t));
Thomas Fossati656864b2016-07-17 08:51:22 +0100169
David Horstmannceeaeb92023-01-05 15:44:23 +0000170 return ret;
Thomas Fossati656864b2016-07-17 08:51:22 +0100171}
172
173#endif /* MBEDTLS_HKDF_C */