blob: 379035ddbb5a3465647dbcb43a56a10a7e71b8c9 [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
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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21#if !defined(MBEDTLS_CONFIG_FILE)
22#include "mbedtls/config.h"
23#else
24#include MBEDTLS_CONFIG_FILE
25#endif
26
27#if defined(MBEDTLS_HKDF_C)
28
29#include <string.h>
30#include "mbedtls/hkdf.h"
31#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000032#include "mbedtls/error.h"
Thomas Fossati656864b2016-07-17 08:51:22 +010033
34int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt,
35 size_t salt_len, const unsigned char *ikm, size_t ikm_len,
36 const unsigned char *info, size_t info_len,
37 unsigned char *okm, size_t okm_len )
38{
Janos Follath24eed8d2019-11-22 13:21:35 +000039 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Thomas Fossati656864b2016-07-17 08:51:22 +010040 unsigned char prk[MBEDTLS_MD_MAX_SIZE];
41
42 ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk );
43
44 if( ret == 0 )
45 {
46 ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size( md ),
47 info, info_len, okm, okm_len );
48 }
49
50 mbedtls_platform_zeroize( prk, sizeof( prk ) );
51
52 return( ret );
53}
54
55int mbedtls_hkdf_extract( const mbedtls_md_info_t *md,
56 const unsigned char *salt, size_t salt_len,
57 const unsigned char *ikm, size_t ikm_len,
58 unsigned char *prk )
59{
60 unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
61
62 if( salt == NULL )
63 {
64 size_t hash_len;
65
Brian J Murrayca2ea4e2018-07-06 10:03:58 -070066 if( salt_len != 0 )
67 {
68 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
69 }
70
Thomas Fossati656864b2016-07-17 08:51:22 +010071 hash_len = mbedtls_md_get_size( md );
72
73 if( hash_len == 0 )
74 {
75 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
76 }
77
78 salt = null_salt;
79 salt_len = hash_len;
80 }
81
82 return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) );
83}
84
85int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
86 size_t prk_len, const unsigned char *info,
87 size_t info_len, unsigned char *okm, size_t okm_len )
88{
89 size_t hash_len;
90 size_t where = 0;
91 size_t n;
92 size_t t_len = 0;
93 size_t i;
94 int ret = 0;
95 mbedtls_md_context_t ctx;
96 unsigned char t[MBEDTLS_MD_MAX_SIZE];
97
98 if( okm == NULL )
99 {
100 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
101 }
102
103 hash_len = mbedtls_md_get_size( md );
104
105 if( prk_len < hash_len || hash_len == 0 )
106 {
107 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
108 }
109
110 if( info == NULL )
111 {
112 info = (const unsigned char *) "";
113 info_len = 0;
114 }
115
116 n = okm_len / hash_len;
117
118 if( (okm_len % hash_len) != 0 )
119 {
120 n++;
121 }
122
Brian J Murraya61d1232018-07-06 10:02:39 -0700123 /*
124 * Per RFC 5869 Section 2.3, okm_len must not exceed
125 * 255 times the hash length
126 */
Thomas Fossati656864b2016-07-17 08:51:22 +0100127 if( n > 255 )
128 {
129 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
130 }
131
132 mbedtls_md_init( &ctx );
133
134 if( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 )
135 {
136 goto exit;
137 }
138
Brian J Murraya61d1232018-07-06 10:02:39 -0700139 /*
140 * Compute T = T(1) | T(2) | T(3) | ... | T(N)
141 * Where T(N) is defined in RFC 5869 Section 2.3
142 */
Thomas Fossati656864b2016-07-17 08:51:22 +0100143 for( i = 1; i <= n; i++ )
144 {
145 size_t num_to_copy;
146 unsigned char c = i & 0xff;
147
148 ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len );
149 if( ret != 0 )
150 {
151 goto exit;
152 }
153
154 ret = mbedtls_md_hmac_update( &ctx, t, t_len );
155 if( ret != 0 )
156 {
157 goto exit;
158 }
159
160 ret = mbedtls_md_hmac_update( &ctx, info, info_len );
161 if( ret != 0 )
162 {
163 goto exit;
164 }
165
Brian J Murraya61d1232018-07-06 10:02:39 -0700166 /* The constant concatenated to the end of each T(n) is a single octet.
Thomas Fossati656864b2016-07-17 08:51:22 +0100167 * */
168 ret = mbedtls_md_hmac_update( &ctx, &c, 1 );
169 if( ret != 0 )
170 {
171 goto exit;
172 }
173
174 ret = mbedtls_md_hmac_finish( &ctx, t );
175 if( ret != 0 )
176 {
177 goto exit;
178 }
179
180 num_to_copy = i != n ? hash_len : okm_len - where;
181 memcpy( okm + where, t, num_to_copy );
182 where += hash_len;
183 t_len = hash_len;
184 }
185
186exit:
187 mbedtls_md_free( &ctx );
188 mbedtls_platform_zeroize( t, sizeof( t ) );
189
190 return( ret );
191}
192
193#endif /* MBEDTLS_HKDF_C */