blob: 41d7d87641f16edcc3ba6579fbcf020ef7d62614 [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"
32
33int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt,
34 size_t salt_len, const unsigned char *ikm, size_t ikm_len,
35 const unsigned char *info, size_t info_len,
36 unsigned char *okm, size_t okm_len )
37{
38 int ret;
39 unsigned char prk[MBEDTLS_MD_MAX_SIZE];
40
41 ret = mbedtls_hkdf_extract( md, salt, salt_len, ikm, ikm_len, prk );
42
43 if( ret == 0 )
44 {
45 ret = mbedtls_hkdf_expand( md, prk, mbedtls_md_get_size( md ),
46 info, info_len, okm, okm_len );
47 }
48
49 mbedtls_platform_zeroize( prk, sizeof( prk ) );
50
51 return( ret );
52}
53
54int mbedtls_hkdf_extract( const mbedtls_md_info_t *md,
55 const unsigned char *salt, size_t salt_len,
56 const unsigned char *ikm, size_t ikm_len,
57 unsigned char *prk )
58{
59 unsigned char null_salt[MBEDTLS_MD_MAX_SIZE] = { '\0' };
60
61 if( salt == NULL )
62 {
63 size_t hash_len;
64
65 hash_len = mbedtls_md_get_size( md );
66
67 if( hash_len == 0 )
68 {
69 return MBEDTLS_ERR_HKDF_BAD_INPUT_DATA;
70 }
71
72 salt = null_salt;
73 salt_len = hash_len;
74 }
75
76 return( mbedtls_md_hmac( md, salt, salt_len, ikm, ikm_len, prk ) );
77}
78
79int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk,
80 size_t prk_len, const unsigned char *info,
81 size_t info_len, unsigned char *okm, size_t okm_len )
82{
83 size_t hash_len;
84 size_t where = 0;
85 size_t n;
86 size_t t_len = 0;
87 size_t i;
88 int ret = 0;
89 mbedtls_md_context_t ctx;
90 unsigned char t[MBEDTLS_MD_MAX_SIZE];
91
92 if( okm == NULL )
93 {
94 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
95 }
96
97 hash_len = mbedtls_md_get_size( md );
98
99 if( prk_len < hash_len || hash_len == 0 )
100 {
101 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
102 }
103
104 if( info == NULL )
105 {
106 info = (const unsigned char *) "";
107 info_len = 0;
108 }
109
110 n = okm_len / hash_len;
111
112 if( (okm_len % hash_len) != 0 )
113 {
114 n++;
115 }
116
Brian J Murraya61d1232018-07-06 10:02:39 -0700117 /*
118 * Per RFC 5869 Section 2.3, okm_len must not exceed
119 * 255 times the hash length
120 */
Thomas Fossati656864b2016-07-17 08:51:22 +0100121 if( n > 255 )
122 {
123 return( MBEDTLS_ERR_HKDF_BAD_INPUT_DATA );
124 }
125
126 mbedtls_md_init( &ctx );
127
128 if( (ret = mbedtls_md_setup( &ctx, md, 1) ) != 0 )
129 {
130 goto exit;
131 }
132
Brian J Murraya61d1232018-07-06 10:02:39 -0700133 /*
134 * Compute T = T(1) | T(2) | T(3) | ... | T(N)
135 * Where T(N) is defined in RFC 5869 Section 2.3
136 */
Thomas Fossati656864b2016-07-17 08:51:22 +0100137 for( i = 1; i <= n; i++ )
138 {
139 size_t num_to_copy;
140 unsigned char c = i & 0xff;
141
142 ret = mbedtls_md_hmac_starts( &ctx, prk, prk_len );
143 if( ret != 0 )
144 {
145 goto exit;
146 }
147
148 ret = mbedtls_md_hmac_update( &ctx, t, t_len );
149 if( ret != 0 )
150 {
151 goto exit;
152 }
153
154 ret = mbedtls_md_hmac_update( &ctx, info, info_len );
155 if( ret != 0 )
156 {
157 goto exit;
158 }
159
Brian J Murraya61d1232018-07-06 10:02:39 -0700160 /* The constant concatenated to the end of each T(n) is a single octet.
Thomas Fossati656864b2016-07-17 08:51:22 +0100161 * */
162 ret = mbedtls_md_hmac_update( &ctx, &c, 1 );
163 if( ret != 0 )
164 {
165 goto exit;
166 }
167
168 ret = mbedtls_md_hmac_finish( &ctx, t );
169 if( ret != 0 )
170 {
171 goto exit;
172 }
173
174 num_to_copy = i != n ? hash_len : okm_len - where;
175 memcpy( okm + where, t, num_to_copy );
176 where += hash_len;
177 t_len = hash_len;
178 }
179
180exit:
181 mbedtls_md_free( &ctx );
182 mbedtls_platform_zeroize( t, sizeof( t ) );
183
184 return( ret );
185}
186
187#endif /* MBEDTLS_HKDF_C */