blob: 5a6204eda52bf373aa216c9d033cd77e32fb71b8 [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
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
Hanno Becker58c5cea2020-09-08 10:31:33 +010020#include "common.h"
Hanno Beckerbe9d6642020-08-21 13:20:06 +010021
22#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
23
24#include "mbedtls/hkdf.h"
Hanno Becker3385a4d2020-08-21 13:03:34 +010025#include "mbedtls/ssl_internal.h"
Hanno Beckerbe9d6642020-08-21 13:20:06 +010026#include "ssl_tls13_keys.h"
27
28#include <stdint.h>
29#include <string.h>
30
Hanno Becker1413bd82020-09-09 12:46:09 +010031#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010032 .name = string,
33
Hanno Beckerbe9d6642020-08-21 13:20:06 +010034struct mbedtls_ssl_tls1_3_labels_struct const mbedtls_ssl_tls1_3_labels =
35{
36 /* This seems to work in C, despite the string literal being one
37 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010038 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010039};
40
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010041#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010042
Hanno Beckerbe9d6642020-08-21 13:20:06 +010043/*
44 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
45 *
46 * The HkdfLabel is specified in RFC 8446 as follows:
47 *
48 * struct HkdfLabel {
49 * uint16 length; // Length of expanded key material
50 * opaque label<7..255>; // Always prefixed by "tls13 "
51 * opaque context<0..255>; // Usually a communication transcript hash
52 * };
53 *
54 * Parameters:
55 * - desired_length: Length of expanded key material
56 * Even though the standard allows expansion to up to
57 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
58 * 255 Bytes, so we require `desired_length` to be at most
59 * 255. This allows us to save a few Bytes of code by
60 * hardcoding the writing of the high bytes.
61 * - (label, llen): label + label length, without "tls13 " prefix
62 * The label length MUST be
63 * <= MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
64 * It is the caller's responsiblity to ensure this.
Hanno Becker815869a2020-09-08 11:16:16 +010065 * All (label, label length) pairs used in TLS 1.3
66 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
Hanno Beckerbe9d6642020-08-21 13:20:06 +010067 * - (ctx, clen): context + context length
68 * The context length MUST be
69 * <= MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
70 * It is the caller's responsiblity to ensure this.
71 * - dst: Target buffer for HkdfLabel structure,
72 * This MUST be a writable buffer of size
73 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
74 * - dlen: Pointer at which to store the actual length of
75 * the HkdfLabel structure on success.
76 */
77
Hanno Becker2dfe1322020-09-10 09:23:12 +010078static const char tls1_3_label_prefix[6] = "tls13 ";
79
Hanno Becker9cb0a142020-09-08 10:48:14 +010080#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( label_len, context_len ) \
Hanno Beckerbe9d6642020-08-21 13:20:06 +010081 ( 2 /* expansion length */ \
82 + 1 /* label length */ \
Hanno Becker9cb0a142020-09-08 10:48:14 +010083 + label_len \
Hanno Beckerbe9d6642020-08-21 13:20:06 +010084 + 1 /* context length */ \
Hanno Becker9cb0a142020-09-08 10:48:14 +010085 + context_len )
86
87#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
88 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Hanno Becker2dfe1322020-09-10 09:23:12 +010089 sizeof(tls1_3_label_prefix) + \
Hanno Becker9cb0a142020-09-08 10:48:14 +010090 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
91 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010092
93static void ssl_tls1_3_hkdf_encode_label(
94 size_t desired_length,
95 const unsigned char *label, size_t llen,
96 const unsigned char *ctx, size_t clen,
97 unsigned char *dst, size_t *dlen )
98{
Hanno Becker2dfe1322020-09-10 09:23:12 +010099 size_t total_label_len =
100 sizeof(tls1_3_label_prefix) + llen;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100101 size_t total_hkdf_lbl_len =
Hanno Becker9cb0a142020-09-08 10:48:14 +0100102 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, clen );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100103
104 unsigned char *p = dst;
105
106 /* Add total length. */
107 *p++ = 0;
108 *p++ = (unsigned char)( ( desired_length >> 0 ) & 0xFF );
109
110 /* Add label incl. prefix */
111 *p++ = (unsigned char)( total_label_len & 0xFF );
Hanno Becker2dfe1322020-09-10 09:23:12 +0100112 memcpy( p, tls1_3_label_prefix, sizeof(tls1_3_label_prefix) );
113 p += sizeof(tls1_3_label_prefix);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100114 memcpy( p, label, llen );
115 p += llen;
116
117 /* Add context value */
118 *p++ = (unsigned char)( clen & 0xFF );
Hanno Becker00debc72020-09-08 11:12:24 +0100119 if( clen != 0 )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100120 memcpy( p, ctx, clen );
121
122 /* Return total length to the caller. */
123 *dlen = total_hkdf_lbl_len;
124}
125
126int mbedtls_ssl_tls1_3_hkdf_expand_label(
127 mbedtls_md_type_t hash_alg,
128 const unsigned char *secret, size_t slen,
129 const unsigned char *label, size_t llen,
130 const unsigned char *ctx, size_t clen,
131 unsigned char *buf, size_t blen )
132{
133 const mbedtls_md_info_t *md;
134 unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ];
135 size_t hkdf_label_len;
136
137 if( llen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN )
138 {
139 /* Should never happen since this is an internal
140 * function, and we know statically which labels
141 * are allowed. */
142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
143 }
144
145 if( clen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
146 {
147 /* Should not happen, as above. */
148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
149 }
150
151 if( blen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN )
152 {
153 /* Should not happen, as above. */
154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
155 }
156
157 md = mbedtls_md_info_from_type( hash_alg );
158 if( md == NULL )
159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
160
161 ssl_tls1_3_hkdf_encode_label( blen,
162 label, llen,
163 ctx, clen,
164 hkdf_label,
165 &hkdf_label_len );
166
167 return( mbedtls_hkdf_expand( md,
168 secret, slen,
169 hkdf_label, hkdf_label_len,
170 buf, blen ) );
171}
172
Hanno Becker3385a4d2020-08-21 13:03:34 +0100173/*
174 * The traffic keying material is generated from the following inputs:
175 *
176 * - One secret value per sender.
177 * - A purpose value indicating the specific value being generated
178 * - The desired lengths of key and IV.
179 *
180 * The expansion itself is based on HKDF:
181 *
182 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
183 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
184 *
185 * [sender] denotes the sending side and the Secret value is provided
186 * by the function caller. Note that we generate server and client side
187 * keys in a single function call.
188 */
189int mbedtls_ssl_tls1_3_make_traffic_keys(
190 mbedtls_md_type_t hash_alg,
191 const unsigned char *client_secret,
192 const unsigned char *server_secret,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100193 size_t slen, size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100194 mbedtls_ssl_key_set *keys )
195{
196 int ret = 0;
197
198 ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
199 client_secret, slen,
200 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
201 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100202 keys->client_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100203 if( ret != 0 )
204 return( ret );
205
206 ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
207 server_secret, slen,
208 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
209 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100210 keys->server_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100211 if( ret != 0 )
212 return( ret );
213
214 ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
215 client_secret, slen,
216 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
217 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100218 keys->client_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100219 if( ret != 0 )
220 return( ret );
221
222 ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
223 server_secret, slen,
224 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
225 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100226 keys->server_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100227 if( ret != 0 )
228 return( ret );
229
Hanno Becker493ea7f2020-09-08 11:01:00 +0100230 keys->key_len = key_len;
231 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100232
233 return( 0 );
234}
235
Hanno Beckerb35d5222020-08-21 13:27:44 +0100236int mbedtls_ssl_tls1_3_derive_secret(
237 mbedtls_md_type_t hash_alg,
238 const unsigned char *secret, size_t slen,
239 const unsigned char *label, size_t llen,
240 const unsigned char *ctx, size_t clen,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100241 int ctx_hashed,
Hanno Beckerb35d5222020-08-21 13:27:44 +0100242 unsigned char *dstbuf, size_t buflen )
243{
244 int ret;
245 unsigned char hashed_context[ MBEDTLS_MD_MAX_SIZE ];
246
247 const mbedtls_md_info_t *md;
248 md = mbedtls_md_info_from_type( hash_alg );
249 if( md == NULL )
250 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
251
Hanno Becker0c42fd92020-09-09 12:58:29 +0100252 if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100253 {
254 ret = mbedtls_md( md, ctx, clen, hashed_context );
255 if( ret != 0 )
256 return( ret );
257 clen = mbedtls_md_get_size( md );
258 }
259 else
260 {
Hanno Beckerb35d5222020-08-21 13:27:44 +0100261 if( clen > sizeof(hashed_context) )
Hanno Becker97a21562020-09-09 12:57:16 +0100262 {
263 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100264 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100265 * Let's double-check nonetheless to not run at the risk
266 * of getting a stack overflow. */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Becker97a21562020-09-09 12:57:16 +0100268 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100269
270 memcpy( hashed_context, ctx, clen );
271 }
272
273 return( mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
274 secret, slen,
275 label, llen,
276 hashed_context, clen,
277 dstbuf, buflen ) );
278}
279
Hanno Beckere9cccb42020-08-20 13:42:46 +0100280int mbedtls_ssl_tls1_3_evolve_secret(
281 mbedtls_md_type_t hash_alg,
282 const unsigned char *secret_old,
283 const unsigned char *input, size_t input_len,
284 unsigned char *secret_new )
285{
286 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
287 size_t hlen, ilen;
Hanno Becker59b50a12020-09-09 10:56:56 +0100288 unsigned char tmp_secret[ MBEDTLS_MD_MAX_SIZE ] = { 0 };
289 unsigned char tmp_input [ MBEDTLS_MD_MAX_SIZE ] = { 0 };
Hanno Beckere9cccb42020-08-20 13:42:46 +0100290
291 const mbedtls_md_info_t *md;
292 md = mbedtls_md_info_from_type( hash_alg );
293 if( md == NULL )
294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
295
296 hlen = mbedtls_md_get_size( md );
297
298 /* For non-initial runs, call Derive-Secret( ., "derived", "")
299 * on the old secreet. */
300 if( secret_old != NULL )
301 {
302 ret = mbedtls_ssl_tls1_3_derive_secret(
303 hash_alg,
304 secret_old, hlen,
305 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
306 NULL, 0, /* context */
307 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Hanno Becker59b50a12020-09-09 10:56:56 +0100308 tmp_secret, hlen );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100309 if( ret != 0 )
310 goto cleanup;
311 }
312
313 if( input != NULL )
314 {
Hanno Becker59b50a12020-09-09 10:56:56 +0100315 memcpy( tmp_input, input, input_len );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100316 ilen = input_len;
317 }
318 else
319 {
320 ilen = hlen;
321 }
322
323 /* HKDF-Extract takes a salt and input key material.
324 * The salt is the old secret, and the input key material
325 * is the input secret (PSK / ECDHE). */
326 ret = mbedtls_hkdf_extract( md,
Hanno Becker59b50a12020-09-09 10:56:56 +0100327 tmp_secret, hlen,
328 tmp_input, ilen,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100329 secret_new );
330 if( ret != 0 )
331 goto cleanup;
332
333 ret = 0;
334
335 cleanup:
336
Hanno Becker59b50a12020-09-09 10:56:56 +0100337 mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
338 mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100339 return( ret );
340}
341
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100342#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */