blob: 0dd4d05645d761b6cd81add3647d00333aa37798 [file] [log] [blame]
Thomas Fossati656864b2016-07-17 08:51:22 +01001/*
2 * HKDF implementation -- RFC 5869
3 *
4 * Copyright (C) 2016-2018, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Thomas Fossati656864b2016-07-17 08:51:22 +010012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Thomas Fossati656864b2016-07-17 08:51:22 +010046 * This file is part of mbed TLS (https://tls.mbed.org)
47 */
48#if !defined(MBEDTLS_CONFIG_FILE)
49#include "mbedtls/config.h"
50#else
51#include MBEDTLS_CONFIG_FILE
52#endif
53
54#if defined(MBEDTLS_HKDF_C)
55
56#include <string.h>
57#include "mbedtls/hkdf.h"
58#include "mbedtls/platform_util.h"
59
60int 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 )
64{
65 int ret;
66 unsigned char prk[MBEDTLS_MD_MAX_SIZE];
67
68 ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk );
69
70 if( ret == 0 )
71 {
72 ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size( md ),
73 info, info_len, okm, okm_len );
74 }
75
76 mbedtls_platform_zeroize( prk, sizeof( prk ) );
77
78 return( ret );
79}
80
81int mbedtls_hkdf_extract( const mbedtls_md_info_t *md,
82 const unsigned char *salt, size_t salt_len,
83 const unsigned char *ikm, size_t ikm_len,
84 unsigned char *prk )
85{
86 unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
87
88 if( salt == NULL )
89 {
90 size_t hash_len;
91
Brian J Murrayca2ea4e2018-07-06 10:03:58 -070092 if( salt_len != 0 )
93 {
94 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
95 }
96
Thomas Fossati656864b2016-07-17 08:51:22 +010097 hash_len = mbedtls_md_get_size( md );
98
99 if( hash_len == 0 )
100 {
101 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
102 }
103
104 salt = null_salt;
105 salt_len = hash_len;
106 }
107
108 return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) );
109}
110
111int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
112 size_t prk_len, const unsigned char *info,
113 size_t info_len, unsigned char *okm, size_t okm_len )
114{
115 size_t hash_len;
116 size_t where = 0;
117 size_t n;
118 size_t t_len = 0;
119 size_t i;
120 int ret = 0;
121 mbedtls_md_context_t ctx;
122 unsigned char t[MBEDTLS_MD_MAX_SIZE];
123
124 if( okm == NULL )
125 {
126 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
127 }
128
129 hash_len = mbedtls_md_get_size( md );
130
131 if( prk_len < hash_len || hash_len == 0 )
132 {
133 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
134 }
135
136 if( info == NULL )
137 {
138 info = (const unsigned char *) "";
139 info_len = 0;
140 }
141
142 n = okm_len / hash_len;
143
144 if( (okm_len % hash_len) != 0 )
145 {
146 n++;
147 }
148
Brian J Murraya61d1232018-07-06 10:02:39 -0700149 /*
150 * Per RFC 5869 Section 2.3, okm_len must not exceed
151 * 255 times the hash length
152 */
Thomas Fossati656864b2016-07-17 08:51:22 +0100153 if( n > 255 )
154 {
155 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
156 }
157
158 mbedtls_md_init( &ctx );
159
160 if( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 )
161 {
162 goto exit;
163 }
164
Brian J Murraya61d1232018-07-06 10:02:39 -0700165 /*
166 * Compute T = T(1) | T(2) | T(3) | ... | T(N)
167 * Where T(N) is defined in RFC 5869 Section 2.3
168 */
Thomas Fossati656864b2016-07-17 08:51:22 +0100169 for( i = 1; i <= n; i++ )
170 {
171 size_t num_to_copy;
172 unsigned char c = i & 0xff;
173
174 ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len );
175 if( ret != 0 )
176 {
177 goto exit;
178 }
179
180 ret = mbedtls_md_hmac_update( &ctx, t, t_len );
181 if( ret != 0 )
182 {
183 goto exit;
184 }
185
186 ret = mbedtls_md_hmac_update( &ctx, info, info_len );
187 if( ret != 0 )
188 {
189 goto exit;
190 }
191
Brian J Murraya61d1232018-07-06 10:02:39 -0700192 /* The constant concatenated to the end of each T(n) is a single octet.
Thomas Fossati656864b2016-07-17 08:51:22 +0100193 * */
194 ret = mbedtls_md_hmac_update( &ctx, &c, 1 );
195 if( ret != 0 )
196 {
197 goto exit;
198 }
199
200 ret = mbedtls_md_hmac_finish( &ctx, t );
201 if( ret != 0 )
202 {
203 goto exit;
204 }
205
206 num_to_copy = i != n ? hash_len : okm_len - where;
207 memcpy( okm + where, t, num_to_copy );
208 where += hash_len;
209 t_len = hash_len;
210 }
211
212exit:
213 mbedtls_md_free( &ctx );
214 mbedtls_platform_zeroize( t, sizeof( t ) );
215
216 return( ret );
217}
218
219#endif /* MBEDTLS_HKDF_C */