blob: d7deaba8d197498e66918f8800dd3ce895a9343f [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
Ronald Cron6f135e12021-12-08 16:57:54 +010022#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010023
Hanno Beckerbe9d6642020-08-21 13:20:06 +010024#include <stdint.h>
25#include <string.h>
26
Jerry Yue3131ef2021-09-16 13:14:15 +080027#include "mbedtls/hkdf.h"
28#include "mbedtls/debug.h"
29#include "mbedtls/error.h"
30
31#include "ssl_misc.h"
32#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010033#include "ssl_tls13_invasive.h"
34
35#include "psa/crypto.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080036
Hanno Becker1413bd82020-09-09 12:46:09 +010037#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010038 .name = string,
39
Xiaofei Bai746f9482021-11-12 08:53:56 +000040struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010041{
42 /* This seems to work in C, despite the string literal being one
43 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010044 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010045};
46
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010047#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010048
Hanno Beckerbe9d6642020-08-21 13:20:06 +010049/*
50 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
51 *
52 * The HkdfLabel is specified in RFC 8446 as follows:
53 *
54 * struct HkdfLabel {
55 * uint16 length; // Length of expanded key material
56 * opaque label<7..255>; // Always prefixed by "tls13 "
57 * opaque context<0..255>; // Usually a communication transcript hash
58 * };
59 *
60 * Parameters:
61 * - desired_length: Length of expanded key material
62 * Even though the standard allows expansion to up to
63 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
64 * 255 Bytes, so we require `desired_length` to be at most
65 * 255. This allows us to save a few Bytes of code by
66 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000067 * - (label, label_len): label + label length, without "tls13 " prefix
68 * The label length MUST be less than or equal to
69 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
70 * It is the caller's responsibility to ensure this.
71 * All (label, label length) pairs used in TLS 1.3
72 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
73 * - (ctx, ctx_len): context + context length
74 * The context length MUST be less than or equal to
75 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
76 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010077 * - dst: Target buffer for HkdfLabel structure,
78 * This MUST be a writable buffer of size
79 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000080 * - dst_len: Pointer at which to store the actual length of
81 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010082 */
83
Xiaofei Baid25fab62021-12-02 06:36:27 +000084static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010085
Hanno Becker9cb0a142020-09-08 10:48:14 +010086#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( label_len, context_len ) \
Hanno Beckerbe9d6642020-08-21 13:20:06 +010087 ( 2 /* expansion length */ \
88 + 1 /* label length */ \
Hanno Becker9cb0a142020-09-08 10:48:14 +010089 + label_len \
Hanno Beckerbe9d6642020-08-21 13:20:06 +010090 + 1 /* context length */ \
Hanno Becker9cb0a142020-09-08 10:48:14 +010091 + context_len )
92
93#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
94 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Xiaofei Baid25fab62021-12-02 06:36:27 +000095 sizeof(tls13_label_prefix) + \
Hanno Becker9cb0a142020-09-08 10:48:14 +010096 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
97 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010098
Xiaofei Bai746f9482021-11-12 08:53:56 +000099static void ssl_tls13_hkdf_encode_label(
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100100 size_t desired_length,
Xiaofei Baib7972842021-11-18 07:29:56 +0000101 const unsigned char *label, size_t label_len,
102 const unsigned char *ctx, size_t ctx_len,
103 unsigned char *dst, size_t *dst_len )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100104{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100105 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000106 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100107 size_t total_hkdf_lbl_len =
Xiaofei Baib7972842021-11-18 07:29:56 +0000108 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, ctx_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100109
110 unsigned char *p = dst;
111
Hanno Becker531fe302020-09-16 09:45:27 +0100112 /* Add the size of the expanded key material.
113 * We're hardcoding the high byte to 0 here assuming that we never use
114 * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
115#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
Xiaofei Bai746f9482021-11-12 08:53:56 +0000116#error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \
Hanno Becker531fe302020-09-16 09:45:27 +0100117 value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
118#endif
119
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100120 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100121 *p++ = MBEDTLS_BYTE_0( desired_length );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100122
123 /* Add label incl. prefix */
Joe Subbiani2194dc42021-07-14 12:31:31 +0100124 *p++ = MBEDTLS_BYTE_0( total_label_len );
Xiaofei Baid25fab62021-12-02 06:36:27 +0000125 memcpy( p, tls13_label_prefix, sizeof(tls13_label_prefix) );
126 p += sizeof(tls13_label_prefix);
Xiaofei Baib7972842021-11-18 07:29:56 +0000127 memcpy( p, label, label_len );
128 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100129
130 /* Add context value */
Xiaofei Baib7972842021-11-18 07:29:56 +0000131 *p++ = MBEDTLS_BYTE_0( ctx_len );
132 if( ctx_len != 0 )
133 memcpy( p, ctx, ctx_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100134
135 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000136 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100137}
138
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100139MBEDTLS_STATIC_TESTABLE
Gabor Mezeied6d6582022-03-26 17:28:06 +0100140psa_status_t mbedtls_psa_hkdf_extract( psa_algorithm_t hash_alg,
Gabor Mezei62bf0242022-02-07 18:12:07 +0100141 const unsigned char *salt, size_t salt_len,
142 const unsigned char *ikm, size_t ikm_len,
143 unsigned char *prk, size_t prk_size,
144 size_t *prk_len )
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100145{
146 unsigned char null_salt[PSA_MAC_MAX_SIZE] = { '\0' };
147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gabor Mezei26c67412022-02-15 15:46:17 +0100149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei0e7c6f42022-02-15 15:47:54 +0100150 psa_status_t destroy_status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100151 psa_algorithm_t alg = PSA_ALG_HMAC( hash_alg );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100152
153 if( salt == NULL || salt_len == 0 )
154 {
155 size_t hash_len;
156
157 if( salt_len != 0 )
158 {
Gabor Mezeic5efb8e2022-02-08 13:15:45 +0100159 return( PSA_ERROR_INVALID_ARGUMENT );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100160 }
161
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100162 hash_len = PSA_HASH_LENGTH( alg );
163
164 if( hash_len == 0 )
165 {
Gabor Mezeic5efb8e2022-02-08 13:15:45 +0100166 return( PSA_ERROR_INVALID_ARGUMENT );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100167 }
168
Gabor Mezeid860e0f2022-02-15 16:02:59 +0100169 /* salt_len <= sizeof( salt ) because
170 PSA_HASH_LENGTH( alg ) <= PSA_MAC_MAX_SIZE. */
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100171 salt = null_salt;
172 salt_len = hash_len;
173 }
174
175 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
176 psa_set_key_algorithm( &attributes, alg );
177 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
178
Gabor Mezei26c67412022-02-15 15:46:17 +0100179 status = psa_import_key( &attributes, salt, salt_len, &key );
180 if( status != PSA_SUCCESS )
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100181 {
182 goto cleanup;
183 }
184
Gabor Mezei26c67412022-02-15 15:46:17 +0100185 status = psa_mac_compute( key, alg, ikm, ikm_len, prk, prk_size, prk_len );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100186
187cleanup:
Gabor Mezei0e7c6f42022-02-15 15:47:54 +0100188 destroy_status = psa_destroy_key( key );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100189
Gabor Mezei0e7c6f42022-02-15 15:47:54 +0100190 return( ( status == PSA_SUCCESS ) ? destroy_status : status );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100191}
192
193MBEDTLS_STATIC_TESTABLE
Gabor Mezeied6d6582022-03-26 17:28:06 +0100194psa_status_t mbedtls_psa_hkdf_expand( psa_algorithm_t hash_alg,
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100195 const unsigned char *prk, size_t prk_len,
196 const unsigned char *info, size_t info_len,
197 unsigned char *okm, size_t okm_len )
198{
199 size_t hash_len;
200 size_t where = 0;
201 size_t n;
202 size_t t_len = 0;
203 size_t i;
204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
206 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gabor Mezei833713c2022-02-15 16:16:08 +0100207 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100208 psa_status_t destroy_status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100209 unsigned char t[PSA_MAC_MAX_SIZE];
Gabor Mezeied6d6582022-03-26 17:28:06 +0100210 psa_algorithm_t alg = PSA_ALG_HMAC( hash_alg );
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100211
212 if( okm == NULL )
213 {
214 return( PSA_ERROR_INVALID_ARGUMENT );
215 }
216
217 hash_len = PSA_HASH_LENGTH( alg );
218
219 if( prk_len < hash_len || hash_len == 0 )
220 {
221 return( PSA_ERROR_INVALID_ARGUMENT );
222 }
223
224 if( info == NULL )
225 {
226 info = (const unsigned char *) "";
227 info_len = 0;
228 }
229
230 n = okm_len / hash_len;
231
232 if( okm_len % hash_len != 0 )
233 {
234 n++;
235 }
236
237 /*
238 * Per RFC 5869 Section 2.3, okm_len must not exceed
239 * 255 times the hash length
240 */
241 if( n > 255 )
242 {
243 return( PSA_ERROR_INVALID_ARGUMENT );
244 }
245
246 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
247 psa_set_key_algorithm( &attributes, alg );
248 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
249
Gabor Mezei833713c2022-02-15 16:16:08 +0100250 status = psa_import_key( &attributes, prk, prk_len, &key );
251 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100252 {
253 goto cleanup;
254 }
255
256 memset( t, 0, hash_len );
257
258 /*
259 * Compute T = T(1) | T(2) | T(3) | ... | T(N)
260 * Where T(N) is defined in RFC 5869 Section 2.3
261 */
262 for( i = 1; i <= n; i++ )
263 {
264 size_t num_to_copy;
265 unsigned char c = i & 0xff;
266 size_t len;
267
Gabor Mezei833713c2022-02-15 16:16:08 +0100268 status = psa_mac_sign_setup( &operation, key, alg );
269 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100270 {
271 goto cleanup;
272 }
273
Gabor Mezei833713c2022-02-15 16:16:08 +0100274 status = psa_mac_update( &operation, t, t_len );
275 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100276 {
277 goto cleanup;
278 }
279
Gabor Mezei833713c2022-02-15 16:16:08 +0100280 status = psa_mac_update( &operation, info, info_len );
281 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100282 {
283 goto cleanup;
284 }
285
Gabor Mezei8e360252022-02-17 11:50:02 +0100286 /* The constant concatenated to the end of each T(n) is a single octet. */
Gabor Mezei833713c2022-02-15 16:16:08 +0100287 status = psa_mac_update( &operation, &c, 1 );
288 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100289 {
290 goto cleanup;
291 }
292
Gabor Mezei833713c2022-02-15 16:16:08 +0100293 status = psa_mac_sign_finish( &operation, t, PSA_MAC_MAX_SIZE, &len );
294 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100295 {
296 goto cleanup;
297 }
298
299 num_to_copy = i != n ? hash_len : okm_len - where;
300 memcpy( okm + where, t, num_to_copy );
301 where += hash_len;
302 t_len = hash_len;
303 }
304
305cleanup:
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100306 if( status != PSA_SUCCESS )
307 psa_mac_abort( &operation );
308 destroy_status = psa_destroy_key( key );
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100309
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100310 mbedtls_platform_zeroize( t, sizeof( t ) );
311
312 return( ( status == PSA_SUCCESS ) ? destroy_status : status );
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100313}
314
Xiaofei Bai746f9482021-11-12 08:53:56 +0000315int mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100316 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000317 const unsigned char *secret, size_t secret_len,
318 const unsigned char *label, size_t label_len,
319 const unsigned char *ctx, size_t ctx_len,
320 unsigned char *buf, size_t buf_len )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100321{
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100322 unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ];
323 size_t hkdf_label_len;
324
Xiaofei Baib7972842021-11-18 07:29:56 +0000325 if( label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100326 {
327 /* Should never happen since this is an internal
328 * function, and we know statically which labels
329 * are allowed. */
330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
331 }
332
Xiaofei Baib7972842021-11-18 07:29:56 +0000333 if( ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100334 {
335 /* Should not happen, as above. */
336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
337 }
338
Xiaofei Baib7972842021-11-18 07:29:56 +0000339 if( buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100340 {
341 /* Should not happen, as above. */
342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
343 }
344
Gabor Mezei07732f72022-03-26 17:04:19 +0100345 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100346 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
347
Xiaofei Baib7972842021-11-18 07:29:56 +0000348 ssl_tls13_hkdf_encode_label( buf_len,
349 label, label_len,
350 ctx, ctx_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000351 hkdf_label,
352 &hkdf_label_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100353
Gabor Mezei58db6532022-03-21 12:12:37 +0100354 return( psa_ssl_status_to_mbedtls(
Gabor Mezeied6d6582022-03-26 17:28:06 +0100355 mbedtls_psa_hkdf_expand( hash_alg,
Gabor Mezei58db6532022-03-21 12:12:37 +0100356 secret, secret_len,
357 hkdf_label, hkdf_label_len,
358 buf, buf_len ) ) );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100359}
360
Hanno Becker3385a4d2020-08-21 13:03:34 +0100361/*
362 * The traffic keying material is generated from the following inputs:
363 *
364 * - One secret value per sender.
365 * - A purpose value indicating the specific value being generated
366 * - The desired lengths of key and IV.
367 *
368 * The expansion itself is based on HKDF:
369 *
370 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
371 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
372 *
373 * [sender] denotes the sending side and the Secret value is provided
374 * by the function caller. Note that we generate server and client side
375 * keys in a single function call.
376 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000377int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100378 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100379 const unsigned char *client_secret,
Xiaofei Baib7972842021-11-18 07:29:56 +0000380 const unsigned char *server_secret, size_t secret_len,
381 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100382 mbedtls_ssl_key_set *keys )
383{
384 int ret = 0;
385
Xiaofei Bai746f9482021-11-12 08:53:56 +0000386 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000387 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100388 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
389 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100390 keys->client_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100391 if( ret != 0 )
392 return( ret );
393
Xiaofei Bai746f9482021-11-12 08:53:56 +0000394 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000395 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100396 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
397 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100398 keys->server_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100399 if( ret != 0 )
400 return( ret );
401
Xiaofei Bai746f9482021-11-12 08:53:56 +0000402 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000403 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100404 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
405 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100406 keys->client_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100407 if( ret != 0 )
408 return( ret );
409
Xiaofei Bai746f9482021-11-12 08:53:56 +0000410 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000411 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100412 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
413 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100414 keys->server_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100415 if( ret != 0 )
416 return( ret );
417
Hanno Becker493ea7f2020-09-08 11:01:00 +0100418 keys->key_len = key_len;
419 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100420
421 return( 0 );
422}
423
Xiaofei Bai746f9482021-11-12 08:53:56 +0000424int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100425 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000426 const unsigned char *secret, size_t secret_len,
427 const unsigned char *label, size_t label_len,
428 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100429 int ctx_hashed,
Xiaofei Baib7972842021-11-18 07:29:56 +0000430 unsigned char *dstbuf, size_t dstbuf_len )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100431{
432 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100433 unsigned char hashed_context[ PSA_HASH_MAX_SIZE ];
Hanno Becker0c42fd92020-09-09 12:58:29 +0100434 if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100435 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100436 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
437
438 status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context,
439 PSA_HASH_LENGTH( hash_alg ), &ctx_len );
440 if( status != PSA_SUCCESS )
441 {
442 ret = psa_ssl_status_to_mbedtls( status );
443 return ret;
444 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100445 }
446 else
447 {
Xiaofei Baib7972842021-11-18 07:29:56 +0000448 if( ctx_len > sizeof(hashed_context) )
Hanno Becker97a21562020-09-09 12:57:16 +0100449 {
450 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100451 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100452 * Let's double-check nonetheless to not run at the risk
453 * of getting a stack overflow. */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100454 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Becker97a21562020-09-09 12:57:16 +0100455 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100456
Xiaofei Baib7972842021-11-18 07:29:56 +0000457 memcpy( hashed_context, ctx, ctx_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100458 }
459
Xiaofei Bai746f9482021-11-12 08:53:56 +0000460 return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000461 secret, secret_len,
462 label, label_len,
463 hashed_context, ctx_len,
464 dstbuf, dstbuf_len ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100465
Hanno Beckerb35d5222020-08-21 13:27:44 +0100466}
467
Xiaofei Bai746f9482021-11-12 08:53:56 +0000468int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100469 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100470 const unsigned char *secret_old,
471 const unsigned char *input, size_t input_len,
472 unsigned char *secret_new )
473{
474 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
475 size_t hlen, ilen;
Gabor Mezei07732f72022-03-26 17:04:19 +0100476 unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 };
Jerry Yu6eaa41c2021-11-22 18:16:45 +0800477 unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 };
Gabor Mezei58db6532022-03-21 12:12:37 +0100478 size_t secret_len;
Gabor Mezei07732f72022-03-26 17:04:19 +0100479
480 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
482
Gabor Mezei07732f72022-03-26 17:04:19 +0100483 hlen = PSA_HASH_LENGTH( hash_alg );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100484
485 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100486 * on the old secret. */
Hanno Beckere9cccb42020-08-20 13:42:46 +0100487 if( secret_old != NULL )
488 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000489 ret = mbedtls_ssl_tls13_derive_secret(
Hanno Beckere9cccb42020-08-20 13:42:46 +0100490 hash_alg,
491 secret_old, hlen,
492 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
493 NULL, 0, /* context */
494 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Hanno Becker59b50a12020-09-09 10:56:56 +0100495 tmp_secret, hlen );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100496 if( ret != 0 )
497 goto cleanup;
498 }
499
500 if( input != NULL )
501 {
Hanno Becker59b50a12020-09-09 10:56:56 +0100502 memcpy( tmp_input, input, input_len );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100503 ilen = input_len;
504 }
505 else
506 {
507 ilen = hlen;
508 }
509
510 /* HKDF-Extract takes a salt and input key material.
511 * The salt is the old secret, and the input key material
512 * is the input secret (PSK / ECDHE). */
Gabor Mezei58db6532022-03-21 12:12:37 +0100513 ret = psa_ssl_status_to_mbedtls(
Gabor Mezeied6d6582022-03-26 17:28:06 +0100514 mbedtls_psa_hkdf_extract( hash_alg,
Gabor Mezei58db6532022-03-21 12:12:37 +0100515 tmp_secret, hlen,
516 tmp_input, ilen,
517 secret_new, hlen, &secret_len ) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100518
519 cleanup:
520
Hanno Becker59b50a12020-09-09 10:56:56 +0100521 mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
522 mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100523 return( ret );
524}
525
Xiaofei Bai746f9482021-11-12 08:53:56 +0000526int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100527 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100528 unsigned char const *early_secret,
529 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000530 mbedtls_ssl_tls13_early_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100531{
532 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100533 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100534
535 /* We should never call this function with an unknown hash,
536 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100537 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100538 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
539
540 /*
541 * 0
542 * |
543 * v
544 * PSK -> HKDF-Extract = Early Secret
545 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100546 * +-----> Derive-Secret(., "c e traffic", ClientHello)
547 * | = client_early_traffic_secret
548 * |
549 * +-----> Derive-Secret(., "e exp master", ClientHello)
550 * | = early_exporter_master_secret
551 * v
552 */
553
554 /* Create client_early_traffic_secret */
Gabor Mezei07732f72022-03-26 17:04:19 +0100555 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
556 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100557 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ),
558 transcript, transcript_len,
559 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
560 derived->client_early_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100561 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100562 if( ret != 0 )
563 return( ret );
564
565 /* Create early exporter */
Gabor Mezei07732f72022-03-26 17:04:19 +0100566 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
567 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100568 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ),
569 transcript, transcript_len,
570 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
571 derived->early_exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100572 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100573 if( ret != 0 )
574 return( ret );
575
576 return( 0 );
577}
578
Xiaofei Bai746f9482021-11-12 08:53:56 +0000579int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100580 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100581 unsigned char const *handshake_secret,
582 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000583 mbedtls_ssl_tls13_handshake_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100584{
585 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100586 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100587
588 /* We should never call this function with an unknown hash,
589 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100590 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100591 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
592
593 /*
594 *
595 * Handshake Secret
596 * |
597 * +-----> Derive-Secret( ., "c hs traffic",
598 * | ClientHello...ServerHello )
599 * | = client_handshake_traffic_secret
600 * |
601 * +-----> Derive-Secret( ., "s hs traffic",
602 * | ClientHello...ServerHello )
603 * | = server_handshake_traffic_secret
604 *
605 */
606
607 /*
608 * Compute client_handshake_traffic_secret with
609 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
610 */
611
Gabor Mezei07732f72022-03-26 17:04:19 +0100612 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
613 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100614 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ),
615 transcript, transcript_len,
616 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
617 derived->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100618 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100619 if( ret != 0 )
620 return( ret );
621
622 /*
623 * Compute server_handshake_traffic_secret with
624 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
625 */
626
Gabor Mezei07732f72022-03-26 17:04:19 +0100627 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
628 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100629 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ),
630 transcript, transcript_len,
631 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
632 derived->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100633 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100634 if( ret != 0 )
635 return( ret );
636
637 return( 0 );
638}
639
Xiaofei Bai746f9482021-11-12 08:53:56 +0000640int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100641 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100642 unsigned char const *application_secret,
643 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000644 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100645{
646 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100647 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100648
649 /* We should never call this function with an unknown hash,
650 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100651 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100652 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
653
654 /* Generate {client,server}_application_traffic_secret_0
655 *
656 * Master Secret
657 * |
658 * +-----> Derive-Secret( ., "c ap traffic",
659 * | ClientHello...server Finished )
660 * | = client_application_traffic_secret_0
661 * |
662 * +-----> Derive-Secret( ., "s ap traffic",
663 * | ClientHello...Server Finished )
664 * | = server_application_traffic_secret_0
665 * |
666 * +-----> Derive-Secret( ., "exp master",
667 * | ClientHello...server Finished)
668 * | = exporter_master_secret
669 *
670 */
671
Gabor Mezei07732f72022-03-26 17:04:19 +0100672 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
673 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100674 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ),
675 transcript, transcript_len,
676 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
677 derived->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100678 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100679 if( ret != 0 )
680 return( ret );
681
Gabor Mezei07732f72022-03-26 17:04:19 +0100682 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
683 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100684 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ),
685 transcript, transcript_len,
686 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
687 derived->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100688 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100689 if( ret != 0 )
690 return( ret );
691
Gabor Mezei07732f72022-03-26 17:04:19 +0100692 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
693 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100694 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ),
695 transcript, transcript_len,
696 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
697 derived->exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100698 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100699 if( ret != 0 )
700 return( ret );
701
702 return( 0 );
703}
704
705/* Generate resumption_master_secret for use with the ticket exchange.
706 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000707 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100708 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000709int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100710 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100711 unsigned char const *application_secret,
712 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000713 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100714{
715 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100716 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100717
718 /* We should never call this function with an unknown hash,
719 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100720 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
722
Gabor Mezei07732f72022-03-26 17:04:19 +0100723 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
724 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100725 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ),
726 transcript, transcript_len,
727 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
728 derived->resumption_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100729 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100730
731 if( ret != 0 )
732 return( ret );
733
734 return( 0 );
735}
736
XiaokangQiana4c99f22021-11-11 06:46:35 +0000737int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000738{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000740 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +0100741 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
742 handshake->ciphersuite_info->mac );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000743
744 /*
745 * Compute MasterSecret
746 */
Gabor Mezei07732f72022-03-26 17:04:19 +0100747 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000748 handshake->tls13_master_secrets.handshake,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000749 NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000750 handshake->tls13_master_secrets.app );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000751 if( ret != 0 )
752 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000754 return( ret );
755 }
756
757 MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret",
Gabor Mezei07732f72022-03-26 17:04:19 +0100758 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000759
760 return( 0 );
761}
762
Gabor Mezei07732f72022-03-26 17:04:19 +0100763static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000764 unsigned char const *base_key,
765 unsigned char const *transcript,
Gabor Mezei07732f72022-03-26 17:04:19 +0100766 unsigned char *dst,
767 size_t *dst_len )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100768{
Gabor Mezei07732f72022-03-26 17:04:19 +0100769 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
772 size_t hash_len = PSA_HASH_LENGTH( hash_alg );
773 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100774 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100775 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100776
777 /* We should never call this function with an unknown hash,
778 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100779 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
781
782 /* TLS 1.3 Finished message
783 *
784 * struct {
785 * opaque verify_data[Hash.length];
786 * } Finished;
787 *
788 * verify_data =
789 * HMAC( finished_key,
790 * Hash( Handshake Context +
791 * Certificate* +
792 * CertificateVerify* )
793 * )
794 *
795 * finished_key =
796 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
797 */
798
Xiaofei Bai746f9482021-11-12 08:53:56 +0000799 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100800 hash_alg, base_key, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100801 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ),
802 NULL, 0,
Gabor Mezei07732f72022-03-26 17:04:19 +0100803 finished_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100804 if( ret != 0 )
805 goto exit;
806
Gabor Mezei07732f72022-03-26 17:04:19 +0100807 alg = PSA_ALG_HMAC( hash_alg );
808 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
809 psa_set_key_algorithm( &attributes, alg );
810 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
811
812 status = psa_import_key( &attributes, finished_key, hash_len, &key );
813 if( status != PSA_SUCCESS )
814 {
815 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100816 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100817 }
818
819 status = psa_mac_compute( key, alg, transcript, hash_len,
820 dst, hash_len, dst_len );
821 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100822
823exit:
824
Gabor Mezei07732f72022-03-26 17:04:19 +0100825 status = psa_destroy_key( key );
826 if( ret == 0 )
827 ret = psa_ssl_status_to_mbedtls( status );
828
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100829 mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100830
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100831 return( ret );
832}
833
XiaokangQianc5c39d52021-11-09 11:55:10 +0000834int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000835 unsigned char* dst,
836 size_t dst_len,
837 size_t *actual_len,
838 int from )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000839{
XiaokangQiana7634982021-10-22 06:32:32 +0000840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000841
XiaokangQianaaa0e192021-11-10 03:07:04 +0000842 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000843 size_t transcript_len;
844
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800845 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800846 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800847 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
848 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800849
850 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100851
852 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(
853 ssl->handshake->ciphersuite_info->mac );
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200854 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yua5563f62021-12-10 18:14:36 +0800855
856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) );
857
Jerry Yub737f6a2021-12-10 17:55:23 +0800858 if( from == MBEDTLS_SSL_IS_CLIENT )
859 {
860 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
861 base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret );
862 }
863 else
864 {
865 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
866 base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret );
867 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000868
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200869 if( dst_len < hash_len )
Jerry Yu9c074732021-12-10 17:12:43 +0800870 {
871 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
872 goto exit;
873 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000874
875 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
876 transcript, sizeof( transcript ),
877 &transcript_len );
878 if( ret != 0 )
879 {
880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000881 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000882 }
883 MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len );
884
Gabor Mezei07732f72022-03-26 17:04:19 +0100885 ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000886 if( ret != 0 )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000887 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000888
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200889 MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000891
892exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800893 /* Erase handshake secrets */
Jerry Yub737f6a2021-12-10 17:55:23 +0800894 mbedtls_platform_zeroize( base_key, base_key_len );
XiaokangQian33062842021-11-11 03:37:45 +0000895 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000896 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000897}
898
Xiaofei Bai746f9482021-11-12 08:53:56 +0000899int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100900 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100901 unsigned char const *psk, size_t psk_len,
902 int psk_type,
903 unsigned char const *transcript,
904 unsigned char *result )
905{
906 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100907 unsigned char binder_key[PSA_MAC_MAX_SIZE];
908 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gabor Mezei07732f72022-03-26 17:04:19 +0100909 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
910 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100911
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100912#if !defined(MBEDTLS_DEBUG_C)
913 ssl = NULL; /* make sure we don't use it except for debug */
914 ((void) ssl);
915#endif
916
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100917 /* We should never call this function with an unknown hash,
918 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100919 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
921
922 /*
923 * 0
924 * |
925 * v
926 * PSK -> HKDF-Extract = Early Secret
927 * |
928 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
929 * | = binder_key
930 * v
931 */
932
Gabor Mezei07732f72022-03-26 17:04:19 +0100933 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000934 NULL, /* Old secret */
935 psk, psk_len, /* Input */
936 early_secret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100937 if( ret != 0 )
938 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000939 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100940 goto exit;
941 }
942
943 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
944 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100945 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
946 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100947 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ),
948 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100949 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100950 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) );
951 }
952 else
953 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100954 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
955 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100956 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ),
957 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100958 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100959 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) );
960 }
961
962 if( ret != 0 )
963 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000964 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100965 goto exit;
966 }
967
968 /*
969 * The binding_value is computed in the same way as the Finished message
970 * but with the BaseKey being the binder_key.
971 */
972
Gabor Mezei07732f72022-03-26 17:04:19 +0100973 ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript,
974 result, &actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100975 if( ret != 0 )
976 goto exit;
977
Gabor Mezei07732f72022-03-26 17:04:19 +0100978 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100979
980exit:
981
982 mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) );
983 mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) );
984 return( ret );
985}
986
Hanno Beckerc94060c2021-03-22 07:50:44 +0000987int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
988 int endpoint,
989 int ciphersuite,
990 mbedtls_ssl_key_set const *traffic_keys,
991 mbedtls_ssl_context *ssl /* DEBUG ONLY */ )
992{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100993#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000994 int ret;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100995#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000996 mbedtls_cipher_info_t const *cipher_info;
997 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
998 unsigned char const *key_enc;
999 unsigned char const *iv_enc;
1000 unsigned char const *key_dec;
1001 unsigned char const *iv_dec;
1002
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001003#if defined(MBEDTLS_USE_PSA_CRYPTO)
1004 psa_key_type_t key_type;
1005 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1006 psa_algorithm_t alg;
1007 size_t key_bits;
1008 psa_status_t status = PSA_SUCCESS;
1009#endif
1010
Hanno Beckerc94060c2021-03-22 07:50:44 +00001011#if !defined(MBEDTLS_DEBUG_C)
1012 ssl = NULL; /* make sure we don't use it except for those cases */
1013 (void) ssl;
1014#endif
1015
1016 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker7887a772021-04-20 05:27:57 +01001017 if( ciphersuite_info == NULL )
1018 {
1019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
1020 ciphersuite ) );
1021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1022 }
Hanno Beckerc94060c2021-03-22 07:50:44 +00001023
1024 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
1025 if( cipher_info == NULL )
1026 {
Hanno Becker7887a772021-04-20 05:27:57 +01001027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
1028 ciphersuite_info->cipher ) );
Hanno Beckerf62a7302021-04-21 05:21:28 +01001029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerc94060c2021-03-22 07:50:44 +00001030 }
1031
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001032#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001033 /*
1034 * Setup cipher contexts in target transform
1035 */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001036 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
1037 cipher_info ) ) != 0 )
1038 {
1039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1040 return( ret );
1041 }
1042
1043 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
1044 cipher_info ) ) != 0 )
1045 {
1046 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1047 return( ret );
1048 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001049#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001050
1051#if defined(MBEDTLS_SSL_SRV_C)
1052 if( endpoint == MBEDTLS_SSL_IS_SERVER )
1053 {
1054 key_enc = traffic_keys->server_write_key;
1055 key_dec = traffic_keys->client_write_key;
1056 iv_enc = traffic_keys->server_write_iv;
1057 iv_dec = traffic_keys->client_write_iv;
1058 }
1059 else
1060#endif /* MBEDTLS_SSL_SRV_C */
1061#if defined(MBEDTLS_SSL_CLI_C)
1062 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
1063 {
1064 key_enc = traffic_keys->client_write_key;
1065 key_dec = traffic_keys->server_write_key;
1066 iv_enc = traffic_keys->client_write_iv;
1067 iv_dec = traffic_keys->server_write_iv;
1068 }
1069 else
1070#endif /* MBEDTLS_SSL_CLI_C */
1071 {
1072 /* should not happen */
1073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1074 }
1075
1076 memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len );
1077 memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len );
1078
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001079#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001080 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc,
1081 key_enc, cipher_info->key_bitlen,
1082 MBEDTLS_ENCRYPT ) ) != 0 )
1083 {
1084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1085 return( ret );
1086 }
1087
1088 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec,
1089 key_dec, cipher_info->key_bitlen,
1090 MBEDTLS_DECRYPT ) ) != 0 )
1091 {
1092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1093 return( ret );
1094 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001095#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001096
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001097 /*
1098 * Setup other fields in SSL transform
1099 */
1100
1101 if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 )
1102 transform->taglen = 8;
1103 else
1104 transform->taglen = 16;
1105
1106 transform->ivlen = traffic_keys->iv_len;
1107 transform->maclen = 0;
1108 transform->fixed_ivlen = transform->ivlen;
1109 transform->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4;
1110
1111 /* We add the true record content type (1 Byte) to the plaintext and
1112 * then pad to the configured granularity. The mimimum length of the
1113 * type-extended and padded plaintext is therefore the padding
1114 * granularity. */
1115 transform->minlen =
1116 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1117
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001119 /*
1120 * Setup psa keys and alg
1121 */
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001122 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001123 transform->taglen,
1124 &alg,
1125 &key_type,
1126 &key_bits ) ) != PSA_SUCCESS )
1127 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) );
1129 return( psa_ssl_status_to_mbedtls( status ) );
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001130 }
1131
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001132 transform->psa_alg = alg;
1133
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001134 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001135 {
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001136 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1137 psa_set_key_algorithm( &attributes, alg );
1138 psa_set_key_type( &attributes, key_type );
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001139
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001140 if( ( status = psa_import_key( &attributes,
1141 key_enc,
1142 PSA_BITS_TO_BYTES( key_bits ),
1143 &transform->psa_key_enc ) ) != PSA_SUCCESS )
1144 {
1145 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1146 return( psa_ssl_status_to_mbedtls( status ) );
1147 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001148
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001149 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1150
1151 if( ( status = psa_import_key( &attributes,
1152 key_dec,
1153 PSA_BITS_TO_BYTES( key_bits ),
1154 &transform->psa_key_dec ) ) != PSA_SUCCESS )
1155 {
1156 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1157 return( psa_ssl_status_to_mbedtls( status ) );
1158 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001159 }
1160#endif /* MBEDTLS_USE_PSA_CRYPTO */
1161
Hanno Beckerc94060c2021-03-22 07:50:44 +00001162 return( 0 );
1163}
1164
Xiaofei Bai746f9482021-11-12 08:53:56 +00001165int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl )
Jerry Yu89ea3212021-09-09 14:31:24 +08001166{
Jerry Yue3131ef2021-09-16 13:14:15 +08001167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001168 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001169 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001170
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001171 if( handshake->ciphersuite_info == NULL )
Jerry Yu89ea3212021-09-09 14:31:24 +08001172 {
1173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) );
1174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1175 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001176
Gabor Mezei07732f72022-03-26 17:04:19 +01001177 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
Jerry Yue06f4532021-09-23 18:35:07 +08001178
Gabor Mezei07732f72022-03-26 17:04:19 +01001179 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001180 handshake->tls13_master_secrets.early );
Jerry Yu89ea3212021-09-09 14:31:24 +08001181 if( ret != 0 )
1182 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yu89ea3212021-09-09 14:31:24 +08001184 return( ret );
1185 }
1186
1187 return( 0 );
1188}
1189
Jerry Yuc068b662021-10-11 22:30:19 +08001190/* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
Jerry Yu61e35e02021-09-16 18:59:08 +08001191 * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
Jerry Yuc068b662021-10-11 22:30:19 +08001192int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
1193 mbedtls_ssl_key_set *traffic_keys )
Jerry Yu61e35e02021-09-16 18:59:08 +08001194{
1195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1196
1197 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001198
1199 psa_algorithm_t hash_alg;
1200 size_t hash_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001201
Jerry Yu435208a2021-10-13 11:22:16 +08001202 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001203 size_t transcript_len;
1204
1205 mbedtls_cipher_info_t const *cipher_info;
Xiaofei Baib7972842021-11-18 07:29:56 +00001206 size_t key_len, iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001207
Jerry Yu435208a2021-10-13 11:22:16 +08001208 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1209 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001210 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001211
Jerry Yuc068b662021-10-11 22:30:19 +08001212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001213
Jerry Yu435208a2021-10-13 11:22:16 +08001214 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Xiaofei Baib7972842021-11-18 07:29:56 +00001215 key_len = cipher_info->key_bitlen >> 3;
1216 iv_len = cipher_info->iv_size;
Jerry Yu61e35e02021-09-16 18:59:08 +08001217
Jerry Yu435208a2021-10-13 11:22:16 +08001218 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001219
1220 hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
1221 hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yu61e35e02021-09-16 18:59:08 +08001222
1223 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1224 transcript,
1225 sizeof( transcript ),
1226 &transcript_len );
1227 if( ret != 0 )
1228 {
1229 MBEDTLS_SSL_DEBUG_RET( 1,
1230 "mbedtls_ssl_get_handshake_transcript",
1231 ret );
1232 return( ret );
1233 }
1234
Gabor Mezei07732f72022-03-26 17:04:19 +01001235 ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001236 handshake->tls13_master_secrets.handshake,
Jerry Yu435208a2021-10-13 11:22:16 +08001237 transcript, transcript_len, tls13_hs_secrets );
Jerry Yu61e35e02021-09-16 18:59:08 +08001238 if( ret != 0 )
1239 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001240 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets",
Jerry Yu61e35e02021-09-16 18:59:08 +08001241 ret );
1242 return( ret );
1243 }
1244
1245 MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001246 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001247 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001248 MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001249 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001250 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001251
1252 /*
1253 * Export client handshake traffic secret
1254 */
Jerry Yu61e35e02021-09-16 18:59:08 +08001255 if( ssl->f_export_keys != NULL )
1256 {
1257 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001258 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001259 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001260 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001261 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001262 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001263 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1264
1265 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001266 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001267 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001268 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001269 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001270 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001271 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1272 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001273
Gabor Mezei07732f72022-03-26 17:04:19 +01001274 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
Jerry Yu435208a2021-10-13 11:22:16 +08001275 tls13_hs_secrets->client_handshake_traffic_secret,
1276 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001277 hash_len, key_len, iv_len, traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +08001278 if( ret != 0 )
1279 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
Jerry Yu61e35e02021-09-16 18:59:08 +08001281 goto exit;
1282 }
1283
1284 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key",
1285 traffic_keys->client_write_key,
1286 traffic_keys->key_len);
1287
1288 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key",
1289 traffic_keys->server_write_key,
1290 traffic_keys->key_len);
1291
1292 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv",
1293 traffic_keys->client_write_iv,
1294 traffic_keys->iv_len);
1295
1296 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv",
1297 traffic_keys->server_write_iv,
1298 traffic_keys->iv_len);
1299
Jerry Yuc068b662021-10-11 22:30:19 +08001300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001301
1302exit:
1303
1304 return( ret );
1305}
1306
Jerry Yuf0ac2352021-10-11 17:47:07 +08001307int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl )
Jerry Yua0650eb2021-09-09 17:14:45 +08001308{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001310#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C)
1311 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1312#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001313 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +01001314 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
1315 handshake->ciphersuite_info->mac );
Jerry Yua0650eb2021-09-09 17:14:45 +08001316
Jerry Yuf0ac2352021-10-11 17:47:07 +08001317#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
1318 /*
1319 * Compute ECDHE secret used to compute the handshake secret from which
1320 * client_handshake_traffic_secret and server_handshake_traffic_secret
1321 * are derived in the handshake secret derivation stage.
1322 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001323 if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) )
Jerry Yuf0ac2352021-10-11 17:47:07 +08001324 {
1325 if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) )
1326 {
1327#if defined(MBEDTLS_ECDH_C)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001328 /* Compute ECDH shared secret. */
1329 status = psa_raw_key_agreement(
1330 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1331 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1332 handshake->premaster, sizeof( handshake->premaster ),
1333 &handshake->pmslen );
1334 if( status != PSA_SUCCESS )
1335 {
1336 ret = psa_ssl_status_to_mbedtls( status );
1337 MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret );
1338 return( ret );
1339 }
1340
1341 status = psa_destroy_key( handshake->ecdh_psa_privkey );
1342 if( status != PSA_SUCCESS )
1343 {
1344 ret = psa_ssl_status_to_mbedtls( status );
1345 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
1346 return( ret );
1347 }
1348
1349 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001350#endif /* MBEDTLS_ECDH_C */
1351 }
1352 else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) )
1353 {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) );
1355 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1356 }
1357 }
1358#else
1359 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1360#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001361
1362 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001363 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001364 */
Gabor Mezei07732f72022-03-26 17:04:19 +01001365 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001366 handshake->tls13_master_secrets.early,
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001367 handshake->premaster, handshake->pmslen,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001368 handshake->tls13_master_secrets.handshake );
Jerry Yua0650eb2021-09-09 17:14:45 +08001369 if( ret != 0 )
1370 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yua0650eb2021-09-09 17:14:45 +08001372 return( ret );
1373 }
1374
1375 MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret",
Gabor Mezei07732f72022-03-26 17:04:19 +01001376 handshake->tls13_master_secrets.handshake,
1377 PSA_HASH_LENGTH( hash_alg ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001378
1379#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001380 mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001381#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
1382 return( 0 );
1383}
1384
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001385/* Generate application traffic keys since any records following a 1-RTT Finished message
1386 * MUST be encrypted under the application traffic key.
1387 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001388int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001389 mbedtls_ssl_context *ssl,
1390 mbedtls_ssl_key_set *traffic_keys )
1391{
XiaokangQiana7634982021-10-22 06:32:32 +00001392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001393 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001394
1395 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001396 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001397 &ssl->session_negotiate->app_secrets;
1398
1399 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001400 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001401 size_t transcript_len;
1402
1403 /* Variables relating to the hash for the chosen ciphersuite. */
1404 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001405
1406 psa_algorithm_t hash_alg;
1407 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001408
1409 /* Variables relating to the cipher for the chosen ciphersuite. */
1410 mbedtls_cipher_info_t const *cipher_info;
Xiaofei Baib7972842021-11-18 07:29:56 +00001411 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001412
1413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) );
1414
1415 /* Extract basic information about hash and ciphersuite */
1416
1417 cipher_info = mbedtls_cipher_info_from_type(
XiaokangQian33062842021-11-11 03:37:45 +00001418 handshake->ciphersuite_info->cipher );
Xiaofei Baib7972842021-11-18 07:29:56 +00001419 key_len = cipher_info->key_bitlen / 8;
1420 iv_len = cipher_info->iv_size;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001421
XiaokangQian33062842021-11-11 03:37:45 +00001422 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001423
1424 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
1425 hash_len = PSA_HASH_LENGTH( hash_alg );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001426
1427 /* Compute current handshake transcript. It's the caller's responsiblity
1428 * to call this at the right time, that is, after the ServerFinished. */
1429
1430 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1431 transcript, sizeof( transcript ),
1432 &transcript_len );
1433 if( ret != 0 )
XiaokangQian4cab0242021-10-12 08:43:37 +00001434 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001435
1436 /* Compute application secrets from master secret and transcript hash. */
1437
Gabor Mezei07732f72022-03-26 17:04:19 +01001438 ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001439 handshake->tls13_master_secrets.app,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001440 transcript, transcript_len,
1441 app_secrets );
Jerry Yu4a2fa5d2021-12-10 10:19:34 +08001442 /* Erase master secrets */
Jerry Yu23ab7a42021-12-08 14:34:10 +08001443 mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets,
Jerry Yu27224f52021-12-09 10:56:50 +08001444 sizeof( ssl->handshake->tls13_master_secrets ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001445 if( ret != 0 )
1446 {
1447 MBEDTLS_SSL_DEBUG_RET( 1,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001448 "mbedtls_ssl_tls13_derive_application_secrets", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001449 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001450 }
1451
1452 /* Derive first epoch of IV + Key for application traffic. */
1453
Gabor Mezei07732f72022-03-26 17:04:19 +01001454 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001455 app_secrets->client_application_traffic_secret_N,
1456 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001457 hash_len, key_len, iv_len, traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001458 if( ret != 0 )
1459 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001461 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001462 }
1463
1464 MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret",
1465 app_secrets->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001466 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001467
1468 MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret",
1469 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001470 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001471
XiaokangQianac0385c2021-11-03 06:40:11 +00001472 /*
1473 * Export client/server application traffic secret 0
1474 */
1475 if( ssl->f_export_keys != NULL )
1476 {
1477 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001478 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001479 app_secrets->client_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001480 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001481 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001482 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1483 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001484
1485 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001486 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001487 app_secrets->server_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001488 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001489 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001490 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1491 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001492 }
1493
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001494 MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:",
Xiaofei Baib7972842021-11-18 07:29:56 +00001495 traffic_keys->client_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001496 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key",
Xiaofei Baib7972842021-11-18 07:29:56 +00001497 traffic_keys->server_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001498 MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001499 traffic_keys->client_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001500 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001501 traffic_keys->server_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001502
1503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001504
1505 cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001506 /* randbytes is not used again */
1507 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1508 sizeof( ssl->handshake->randbytes ) );
XiaokangQian33062842021-11-11 03:37:45 +00001509 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001510 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001511}
1512
Ronald Cron6f135e12021-12-08 16:57:54 +01001513#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */