blob: 5c851c712d29354347eb75761c0b98ea10421fbd [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 ];
434/*
Xiaofei Baib7972842021-11-18 07:29:56 +0000435 const mbedtls_md_info_t *md_info;
436 md_info = mbedtls_md_info_from_type( hash_alg );
437 if( md_info == NULL )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100438 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gabor Mezei07732f72022-03-26 17:04:19 +0100439*/
Hanno Becker0c42fd92020-09-09 12:58:29 +0100440 if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100441 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100442/*
Xiaofei Baib7972842021-11-18 07:29:56 +0000443 ret = mbedtls_md( md_info, ctx, ctx_len, hashed_context );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100444 if( ret != 0 )
445 return( ret );
Xiaofei Baib7972842021-11-18 07:29:56 +0000446 ctx_len = mbedtls_md_get_size( md_info );
Gabor Mezei07732f72022-03-26 17:04:19 +0100447*/
448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
449
450 status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context,
451 PSA_HASH_LENGTH( hash_alg ), &ctx_len );
452 if( status != PSA_SUCCESS )
453 {
454 ret = psa_ssl_status_to_mbedtls( status );
455 return ret;
456 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100457 }
458 else
459 {
Xiaofei Baib7972842021-11-18 07:29:56 +0000460 if( ctx_len > sizeof(hashed_context) )
Hanno Becker97a21562020-09-09 12:57:16 +0100461 {
462 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100463 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100464 * Let's double-check nonetheless to not run at the risk
465 * of getting a stack overflow. */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Becker97a21562020-09-09 12:57:16 +0100467 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100468
Xiaofei Baib7972842021-11-18 07:29:56 +0000469 memcpy( hashed_context, ctx, ctx_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100470 }
471
Xiaofei Bai746f9482021-11-12 08:53:56 +0000472 return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000473 secret, secret_len,
474 label, label_len,
475 hashed_context, ctx_len,
476 dstbuf, dstbuf_len ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100477
Hanno Beckerb35d5222020-08-21 13:27:44 +0100478}
479
Xiaofei Bai746f9482021-11-12 08:53:56 +0000480int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100481 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100482 const unsigned char *secret_old,
483 const unsigned char *input, size_t input_len,
484 unsigned char *secret_new )
485{
486 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
487 size_t hlen, ilen;
Gabor Mezei07732f72022-03-26 17:04:19 +0100488 unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 };
Jerry Yu6eaa41c2021-11-22 18:16:45 +0800489 unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 };
Gabor Mezei58db6532022-03-21 12:12:37 +0100490 size_t secret_len;
Gabor Mezei07732f72022-03-26 17:04:19 +0100491
492 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
494
Gabor Mezei07732f72022-03-26 17:04:19 +0100495 hlen = PSA_HASH_LENGTH( hash_alg );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100496
497 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100498 * on the old secret. */
Hanno Beckere9cccb42020-08-20 13:42:46 +0100499 if( secret_old != NULL )
500 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000501 ret = mbedtls_ssl_tls13_derive_secret(
Hanno Beckere9cccb42020-08-20 13:42:46 +0100502 hash_alg,
503 secret_old, hlen,
504 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
505 NULL, 0, /* context */
506 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Hanno Becker59b50a12020-09-09 10:56:56 +0100507 tmp_secret, hlen );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100508 if( ret != 0 )
509 goto cleanup;
510 }
511
512 if( input != NULL )
513 {
Hanno Becker59b50a12020-09-09 10:56:56 +0100514 memcpy( tmp_input, input, input_len );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100515 ilen = input_len;
516 }
517 else
518 {
519 ilen = hlen;
520 }
521
522 /* HKDF-Extract takes a salt and input key material.
523 * The salt is the old secret, and the input key material
524 * is the input secret (PSK / ECDHE). */
Gabor Mezei58db6532022-03-21 12:12:37 +0100525 ret = psa_ssl_status_to_mbedtls(
Gabor Mezeied6d6582022-03-26 17:28:06 +0100526 mbedtls_psa_hkdf_extract( hash_alg,
Gabor Mezei58db6532022-03-21 12:12:37 +0100527 tmp_secret, hlen,
528 tmp_input, ilen,
529 secret_new, hlen, &secret_len ) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100530
531 cleanup:
532
Hanno Becker59b50a12020-09-09 10:56:56 +0100533 mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
534 mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100535 return( ret );
536}
537
Xiaofei Bai746f9482021-11-12 08:53:56 +0000538int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100539 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100540 unsigned char const *early_secret,
541 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000542 mbedtls_ssl_tls13_early_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100543{
544 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100545 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100546
547 /* We should never call this function with an unknown hash,
548 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100549 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
551
552 /*
553 * 0
554 * |
555 * v
556 * PSK -> HKDF-Extract = Early Secret
557 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100558 * +-----> Derive-Secret(., "c e traffic", ClientHello)
559 * | = client_early_traffic_secret
560 * |
561 * +-----> Derive-Secret(., "e exp master", ClientHello)
562 * | = early_exporter_master_secret
563 * v
564 */
565
566 /* Create client_early_traffic_secret */
Gabor Mezei07732f72022-03-26 17:04:19 +0100567 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
568 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100569 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ),
570 transcript, transcript_len,
571 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
572 derived->client_early_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100573 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100574 if( ret != 0 )
575 return( ret );
576
577 /* Create early exporter */
Gabor Mezei07732f72022-03-26 17:04:19 +0100578 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
579 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100580 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ),
581 transcript, transcript_len,
582 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
583 derived->early_exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100584 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100585 if( ret != 0 )
586 return( ret );
587
588 return( 0 );
589}
590
Xiaofei Bai746f9482021-11-12 08:53:56 +0000591int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100592 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100593 unsigned char const *handshake_secret,
594 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000595 mbedtls_ssl_tls13_handshake_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100596{
597 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100598 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100599
600 /* We should never call this function with an unknown hash,
601 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100602 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
604
605 /*
606 *
607 * Handshake Secret
608 * |
609 * +-----> Derive-Secret( ., "c hs traffic",
610 * | ClientHello...ServerHello )
611 * | = client_handshake_traffic_secret
612 * |
613 * +-----> Derive-Secret( ., "s hs traffic",
614 * | ClientHello...ServerHello )
615 * | = server_handshake_traffic_secret
616 *
617 */
618
619 /*
620 * Compute client_handshake_traffic_secret with
621 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
622 */
623
Gabor Mezei07732f72022-03-26 17:04:19 +0100624 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
625 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100626 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ),
627 transcript, transcript_len,
628 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
629 derived->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100630 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100631 if( ret != 0 )
632 return( ret );
633
634 /*
635 * Compute server_handshake_traffic_secret with
636 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
637 */
638
Gabor Mezei07732f72022-03-26 17:04:19 +0100639 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
640 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100641 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ),
642 transcript, transcript_len,
643 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
644 derived->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100645 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100646 if( ret != 0 )
647 return( ret );
648
649 return( 0 );
650}
651
Xiaofei Bai746f9482021-11-12 08:53:56 +0000652int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100653 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100654 unsigned char const *application_secret,
655 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000656 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100657{
658 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100659 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100660
661 /* We should never call this function with an unknown hash,
662 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100663 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100664 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
665
666 /* Generate {client,server}_application_traffic_secret_0
667 *
668 * Master Secret
669 * |
670 * +-----> Derive-Secret( ., "c ap traffic",
671 * | ClientHello...server Finished )
672 * | = client_application_traffic_secret_0
673 * |
674 * +-----> Derive-Secret( ., "s ap traffic",
675 * | ClientHello...Server Finished )
676 * | = server_application_traffic_secret_0
677 * |
678 * +-----> Derive-Secret( ., "exp master",
679 * | ClientHello...server Finished)
680 * | = exporter_master_secret
681 *
682 */
683
Gabor Mezei07732f72022-03-26 17:04:19 +0100684 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
685 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100686 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ),
687 transcript, transcript_len,
688 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
689 derived->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100690 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100691 if( ret != 0 )
692 return( ret );
693
Gabor Mezei07732f72022-03-26 17:04:19 +0100694 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
695 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100696 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ),
697 transcript, transcript_len,
698 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
699 derived->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100700 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100701 if( ret != 0 )
702 return( ret );
703
Gabor Mezei07732f72022-03-26 17:04:19 +0100704 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
705 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100706 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ),
707 transcript, transcript_len,
708 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
709 derived->exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100710 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100711 if( ret != 0 )
712 return( ret );
713
714 return( 0 );
715}
716
717/* Generate resumption_master_secret for use with the ticket exchange.
718 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000719 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100720 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000721int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100722 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100723 unsigned char const *application_secret,
724 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000725 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100726{
727 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100728 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100729
730 /* We should never call this function with an unknown hash,
731 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100732 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
734
Gabor Mezei07732f72022-03-26 17:04:19 +0100735 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
736 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100737 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ),
738 transcript, transcript_len,
739 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
740 derived->resumption_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100741 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100742
743 if( ret != 0 )
744 return( ret );
745
746 return( 0 );
747}
748
XiaokangQiana4c99f22021-11-11 06:46:35 +0000749int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000750{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000752 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +0100753 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
754 handshake->ciphersuite_info->mac );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000755
756 /*
757 * Compute MasterSecret
758 */
Gabor Mezei07732f72022-03-26 17:04:19 +0100759 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000760 handshake->tls13_master_secrets.handshake,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000761 NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000762 handshake->tls13_master_secrets.app );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000763 if( ret != 0 )
764 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000765 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000766 return( ret );
767 }
768
769 MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret",
Gabor Mezei07732f72022-03-26 17:04:19 +0100770 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000771
772 return( 0 );
773}
774
Gabor Mezei07732f72022-03-26 17:04:19 +0100775static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000776 unsigned char const *base_key,
777 unsigned char const *transcript,
Gabor Mezei07732f72022-03-26 17:04:19 +0100778 unsigned char *dst,
779 size_t *dst_len )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100780{
Gabor Mezei07732f72022-03-26 17:04:19 +0100781 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
783 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
784 size_t hash_len = PSA_HASH_LENGTH( hash_alg );
785 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100786 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100787 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100788
789 /* We should never call this function with an unknown hash,
790 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100791 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
793
794 /* TLS 1.3 Finished message
795 *
796 * struct {
797 * opaque verify_data[Hash.length];
798 * } Finished;
799 *
800 * verify_data =
801 * HMAC( finished_key,
802 * Hash( Handshake Context +
803 * Certificate* +
804 * CertificateVerify* )
805 * )
806 *
807 * finished_key =
808 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
809 */
810
Xiaofei Bai746f9482021-11-12 08:53:56 +0000811 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100812 hash_alg, base_key, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100813 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ),
814 NULL, 0,
Gabor Mezei07732f72022-03-26 17:04:19 +0100815 finished_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100816 if( ret != 0 )
817 goto exit;
818
Gabor Mezei07732f72022-03-26 17:04:19 +0100819 alg = PSA_ALG_HMAC( hash_alg );
820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
821 psa_set_key_algorithm( &attributes, alg );
822 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
823
824 status = psa_import_key( &attributes, finished_key, hash_len, &key );
825 if( status != PSA_SUCCESS )
826 {
827 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100828 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100829 }
830
831 status = psa_mac_compute( key, alg, transcript, hash_len,
832 dst, hash_len, dst_len );
833 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100834
835exit:
836
Gabor Mezei07732f72022-03-26 17:04:19 +0100837 status = psa_destroy_key( key );
838 if( ret == 0 )
839 ret = psa_ssl_status_to_mbedtls( status );
840
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100841 mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100842
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100843 return( ret );
844}
845
XiaokangQianc5c39d52021-11-09 11:55:10 +0000846int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000847 unsigned char* dst,
848 size_t dst_len,
849 size_t *actual_len,
850 int from )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000851{
XiaokangQiana7634982021-10-22 06:32:32 +0000852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000853
XiaokangQianaaa0e192021-11-10 03:07:04 +0000854 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000855 size_t transcript_len;
856
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800857 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800858 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800859 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
860 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800861
862 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100863
864 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(
865 ssl->handshake->ciphersuite_info->mac );
866 size_t const hash_lem = PSA_HASH_LENGTH( hash_alg );
Jerry Yua5563f62021-12-10 18:14:36 +0800867
868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) );
869
Jerry Yub737f6a2021-12-10 17:55:23 +0800870 if( from == MBEDTLS_SSL_IS_CLIENT )
871 {
872 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
873 base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret );
874 }
875 else
876 {
877 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
878 base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret );
879 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000880
Gabor Mezei07732f72022-03-26 17:04:19 +0100881 if( dst_len < hash_lem )
Jerry Yu9c074732021-12-10 17:12:43 +0800882 {
883 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
884 goto exit;
885 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000886
887 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
888 transcript, sizeof( transcript ),
889 &transcript_len );
890 if( ret != 0 )
891 {
892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000893 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000894 }
895 MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len );
896
Gabor Mezei07732f72022-03-26 17:04:19 +0100897 ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000898 if( ret != 0 )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000899 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000900
Gabor Mezei07732f72022-03-26 17:04:19 +0100901 MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_lem );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000903
904exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800905 /* Erase handshake secrets */
Jerry Yub737f6a2021-12-10 17:55:23 +0800906 mbedtls_platform_zeroize( base_key, base_key_len );
XiaokangQian33062842021-11-11 03:37:45 +0000907 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000908 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000909}
910
Xiaofei Bai746f9482021-11-12 08:53:56 +0000911int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100912 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100913 unsigned char const *psk, size_t psk_len,
914 int psk_type,
915 unsigned char const *transcript,
916 unsigned char *result )
917{
918 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100919 unsigned char binder_key[PSA_MAC_MAX_SIZE];
920 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gabor Mezei07732f72022-03-26 17:04:19 +0100921 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
922 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100923
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100924#if !defined(MBEDTLS_DEBUG_C)
925 ssl = NULL; /* make sure we don't use it except for debug */
926 ((void) ssl);
927#endif
928
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100929 /* We should never call this function with an unknown hash,
930 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100931 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
933
934 /*
935 * 0
936 * |
937 * v
938 * PSK -> HKDF-Extract = Early Secret
939 * |
940 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
941 * | = binder_key
942 * v
943 */
944
Gabor Mezei07732f72022-03-26 17:04:19 +0100945 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000946 NULL, /* Old secret */
947 psk, psk_len, /* Input */
948 early_secret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100949 if( ret != 0 )
950 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100952 goto exit;
953 }
954
955 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
956 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100957 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
958 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100959 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ),
960 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100961 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100962 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) );
963 }
964 else
965 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100966 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
967 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100968 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ),
969 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100970 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100971 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) );
972 }
973
974 if( ret != 0 )
975 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100977 goto exit;
978 }
979
980 /*
981 * The binding_value is computed in the same way as the Finished message
982 * but with the BaseKey being the binder_key.
983 */
984
Gabor Mezei07732f72022-03-26 17:04:19 +0100985 ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript,
986 result, &actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100987 if( ret != 0 )
988 goto exit;
989
Gabor Mezei07732f72022-03-26 17:04:19 +0100990 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100991
992exit:
993
994 mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) );
995 mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) );
996 return( ret );
997}
998
Hanno Beckerc94060c2021-03-22 07:50:44 +0000999int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
1000 int endpoint,
1001 int ciphersuite,
1002 mbedtls_ssl_key_set const *traffic_keys,
1003 mbedtls_ssl_context *ssl /* DEBUG ONLY */ )
1004{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001005#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001006 int ret;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001007#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001008 mbedtls_cipher_info_t const *cipher_info;
1009 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1010 unsigned char const *key_enc;
1011 unsigned char const *iv_enc;
1012 unsigned char const *key_dec;
1013 unsigned char const *iv_dec;
1014
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001015#if defined(MBEDTLS_USE_PSA_CRYPTO)
1016 psa_key_type_t key_type;
1017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1018 psa_algorithm_t alg;
1019 size_t key_bits;
1020 psa_status_t status = PSA_SUCCESS;
1021#endif
1022
Hanno Beckerc94060c2021-03-22 07:50:44 +00001023#if !defined(MBEDTLS_DEBUG_C)
1024 ssl = NULL; /* make sure we don't use it except for those cases */
1025 (void) ssl;
1026#endif
1027
1028 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker7887a772021-04-20 05:27:57 +01001029 if( ciphersuite_info == NULL )
1030 {
1031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
1032 ciphersuite ) );
1033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1034 }
Hanno Beckerc94060c2021-03-22 07:50:44 +00001035
1036 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
1037 if( cipher_info == NULL )
1038 {
Hanno Becker7887a772021-04-20 05:27:57 +01001039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
1040 ciphersuite_info->cipher ) );
Hanno Beckerf62a7302021-04-21 05:21:28 +01001041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerc94060c2021-03-22 07:50:44 +00001042 }
1043
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001044#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001045 /*
1046 * Setup cipher contexts in target transform
1047 */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001048 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
1049 cipher_info ) ) != 0 )
1050 {
1051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1052 return( ret );
1053 }
1054
1055 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
1056 cipher_info ) ) != 0 )
1057 {
1058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1059 return( ret );
1060 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001061#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001062
1063#if defined(MBEDTLS_SSL_SRV_C)
1064 if( endpoint == MBEDTLS_SSL_IS_SERVER )
1065 {
1066 key_enc = traffic_keys->server_write_key;
1067 key_dec = traffic_keys->client_write_key;
1068 iv_enc = traffic_keys->server_write_iv;
1069 iv_dec = traffic_keys->client_write_iv;
1070 }
1071 else
1072#endif /* MBEDTLS_SSL_SRV_C */
1073#if defined(MBEDTLS_SSL_CLI_C)
1074 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
1075 {
1076 key_enc = traffic_keys->client_write_key;
1077 key_dec = traffic_keys->server_write_key;
1078 iv_enc = traffic_keys->client_write_iv;
1079 iv_dec = traffic_keys->server_write_iv;
1080 }
1081 else
1082#endif /* MBEDTLS_SSL_CLI_C */
1083 {
1084 /* should not happen */
1085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1086 }
1087
1088 memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len );
1089 memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len );
1090
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001091#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001092 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc,
1093 key_enc, cipher_info->key_bitlen,
1094 MBEDTLS_ENCRYPT ) ) != 0 )
1095 {
1096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1097 return( ret );
1098 }
1099
1100 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec,
1101 key_dec, cipher_info->key_bitlen,
1102 MBEDTLS_DECRYPT ) ) != 0 )
1103 {
1104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1105 return( ret );
1106 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001107#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001108
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001109 /*
1110 * Setup other fields in SSL transform
1111 */
1112
1113 if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 )
1114 transform->taglen = 8;
1115 else
1116 transform->taglen = 16;
1117
1118 transform->ivlen = traffic_keys->iv_len;
1119 transform->maclen = 0;
1120 transform->fixed_ivlen = transform->ivlen;
1121 transform->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4;
1122
1123 /* We add the true record content type (1 Byte) to the plaintext and
1124 * then pad to the configured granularity. The mimimum length of the
1125 * type-extended and padded plaintext is therefore the padding
1126 * granularity. */
1127 transform->minlen =
1128 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1129
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001130#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001131 /*
1132 * Setup psa keys and alg
1133 */
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001134 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001135 transform->taglen,
1136 &alg,
1137 &key_type,
1138 &key_bits ) ) != PSA_SUCCESS )
1139 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) );
1141 return( psa_ssl_status_to_mbedtls( status ) );
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001142 }
1143
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001144 transform->psa_alg = alg;
1145
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001146 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001147 {
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001148 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1149 psa_set_key_algorithm( &attributes, alg );
1150 psa_set_key_type( &attributes, key_type );
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001151
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001152 if( ( status = psa_import_key( &attributes,
1153 key_enc,
1154 PSA_BITS_TO_BYTES( key_bits ),
1155 &transform->psa_key_enc ) ) != PSA_SUCCESS )
1156 {
1157 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1158 return( psa_ssl_status_to_mbedtls( status ) );
1159 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001160
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001161 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1162
1163 if( ( status = psa_import_key( &attributes,
1164 key_dec,
1165 PSA_BITS_TO_BYTES( key_bits ),
1166 &transform->psa_key_dec ) ) != PSA_SUCCESS )
1167 {
1168 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1169 return( psa_ssl_status_to_mbedtls( status ) );
1170 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001171 }
1172#endif /* MBEDTLS_USE_PSA_CRYPTO */
1173
Hanno Beckerc94060c2021-03-22 07:50:44 +00001174 return( 0 );
1175}
1176
Xiaofei Bai746f9482021-11-12 08:53:56 +00001177int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl )
Jerry Yu89ea3212021-09-09 14:31:24 +08001178{
Jerry Yue3131ef2021-09-16 13:14:15 +08001179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001180 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001181 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001182
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001183 if( handshake->ciphersuite_info == NULL )
Jerry Yu89ea3212021-09-09 14:31:24 +08001184 {
1185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) );
1186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1187 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001188
Gabor Mezei07732f72022-03-26 17:04:19 +01001189 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
Jerry Yue06f4532021-09-23 18:35:07 +08001190
Gabor Mezei07732f72022-03-26 17:04:19 +01001191 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001192 handshake->tls13_master_secrets.early );
Jerry Yu89ea3212021-09-09 14:31:24 +08001193 if( ret != 0 )
1194 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yu89ea3212021-09-09 14:31:24 +08001196 return( ret );
1197 }
1198
1199 return( 0 );
1200}
1201
Jerry Yuc068b662021-10-11 22:30:19 +08001202/* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
Jerry Yu61e35e02021-09-16 18:59:08 +08001203 * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
Jerry Yuc068b662021-10-11 22:30:19 +08001204int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
1205 mbedtls_ssl_key_set *traffic_keys )
Jerry Yu61e35e02021-09-16 18:59:08 +08001206{
1207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1208
1209 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001210
1211 psa_algorithm_t hash_alg;
1212 size_t hash_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001213
Jerry Yu435208a2021-10-13 11:22:16 +08001214 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001215 size_t transcript_len;
1216
1217 mbedtls_cipher_info_t const *cipher_info;
Xiaofei Baib7972842021-11-18 07:29:56 +00001218 size_t key_len, iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001219
Jerry Yu435208a2021-10-13 11:22:16 +08001220 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1221 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001222 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001223
Jerry Yuc068b662021-10-11 22:30:19 +08001224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001225
Jerry Yu435208a2021-10-13 11:22:16 +08001226 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Xiaofei Baib7972842021-11-18 07:29:56 +00001227 key_len = cipher_info->key_bitlen >> 3;
1228 iv_len = cipher_info->iv_size;
Jerry Yu61e35e02021-09-16 18:59:08 +08001229
Jerry Yu435208a2021-10-13 11:22:16 +08001230 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001231
1232 hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
1233 hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yu61e35e02021-09-16 18:59:08 +08001234
1235 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1236 transcript,
1237 sizeof( transcript ),
1238 &transcript_len );
1239 if( ret != 0 )
1240 {
1241 MBEDTLS_SSL_DEBUG_RET( 1,
1242 "mbedtls_ssl_get_handshake_transcript",
1243 ret );
1244 return( ret );
1245 }
1246
Gabor Mezei07732f72022-03-26 17:04:19 +01001247 ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001248 handshake->tls13_master_secrets.handshake,
Jerry Yu435208a2021-10-13 11:22:16 +08001249 transcript, transcript_len, tls13_hs_secrets );
Jerry Yu61e35e02021-09-16 18:59:08 +08001250 if( ret != 0 )
1251 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets",
Jerry Yu61e35e02021-09-16 18:59:08 +08001253 ret );
1254 return( ret );
1255 }
1256
1257 MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001258 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001259 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001260 MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001261 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001262 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001263
1264 /*
1265 * Export client handshake traffic secret
1266 */
Jerry Yu61e35e02021-09-16 18:59:08 +08001267 if( ssl->f_export_keys != NULL )
1268 {
1269 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001270 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001271 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001272 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001273 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001274 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001275 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1276
1277 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001278 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001279 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001280 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001281 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001282 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001283 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1284 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001285
Gabor Mezei07732f72022-03-26 17:04:19 +01001286 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
Jerry Yu435208a2021-10-13 11:22:16 +08001287 tls13_hs_secrets->client_handshake_traffic_secret,
1288 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001289 hash_len, key_len, iv_len, traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +08001290 if( ret != 0 )
1291 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
Jerry Yu61e35e02021-09-16 18:59:08 +08001293 goto exit;
1294 }
1295
1296 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key",
1297 traffic_keys->client_write_key,
1298 traffic_keys->key_len);
1299
1300 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key",
1301 traffic_keys->server_write_key,
1302 traffic_keys->key_len);
1303
1304 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv",
1305 traffic_keys->client_write_iv,
1306 traffic_keys->iv_len);
1307
1308 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv",
1309 traffic_keys->server_write_iv,
1310 traffic_keys->iv_len);
1311
Jerry Yuc068b662021-10-11 22:30:19 +08001312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001313
1314exit:
1315
1316 return( ret );
1317}
1318
Jerry Yuf0ac2352021-10-11 17:47:07 +08001319int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl )
Jerry Yua0650eb2021-09-09 17:14:45 +08001320{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001322#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C)
1323 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1324#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001325 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +01001326 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
1327 handshake->ciphersuite_info->mac );
Jerry Yua0650eb2021-09-09 17:14:45 +08001328
Jerry Yuf0ac2352021-10-11 17:47:07 +08001329#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
1330 /*
1331 * Compute ECDHE secret used to compute the handshake secret from which
1332 * client_handshake_traffic_secret and server_handshake_traffic_secret
1333 * are derived in the handshake secret derivation stage.
1334 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001335 if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) )
Jerry Yuf0ac2352021-10-11 17:47:07 +08001336 {
1337 if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) )
1338 {
1339#if defined(MBEDTLS_ECDH_C)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001340 /* Compute ECDH shared secret. */
1341 status = psa_raw_key_agreement(
1342 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1343 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1344 handshake->premaster, sizeof( handshake->premaster ),
1345 &handshake->pmslen );
1346 if( status != PSA_SUCCESS )
1347 {
1348 ret = psa_ssl_status_to_mbedtls( status );
1349 MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret );
1350 return( ret );
1351 }
1352
1353 status = psa_destroy_key( handshake->ecdh_psa_privkey );
1354 if( status != PSA_SUCCESS )
1355 {
1356 ret = psa_ssl_status_to_mbedtls( status );
1357 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
1358 return( ret );
1359 }
1360
1361 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001362#endif /* MBEDTLS_ECDH_C */
1363 }
1364 else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) )
1365 {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) );
1367 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1368 }
1369 }
1370#else
1371 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1372#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001373
1374 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001375 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001376 */
Gabor Mezei07732f72022-03-26 17:04:19 +01001377 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001378 handshake->tls13_master_secrets.early,
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001379 handshake->premaster, handshake->pmslen,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001380 handshake->tls13_master_secrets.handshake );
Jerry Yua0650eb2021-09-09 17:14:45 +08001381 if( ret != 0 )
1382 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yua0650eb2021-09-09 17:14:45 +08001384 return( ret );
1385 }
1386
1387 MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret",
Gabor Mezei07732f72022-03-26 17:04:19 +01001388 handshake->tls13_master_secrets.handshake,
1389 PSA_HASH_LENGTH( hash_alg ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001390
1391#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001392 mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001393#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
1394 return( 0 );
1395}
1396
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001397/* Generate application traffic keys since any records following a 1-RTT Finished message
1398 * MUST be encrypted under the application traffic key.
1399 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001400int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001401 mbedtls_ssl_context *ssl,
1402 mbedtls_ssl_key_set *traffic_keys )
1403{
XiaokangQiana7634982021-10-22 06:32:32 +00001404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001405 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001406
1407 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001408 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001409 &ssl->session_negotiate->app_secrets;
1410
1411 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001412 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001413 size_t transcript_len;
1414
1415 /* Variables relating to the hash for the chosen ciphersuite. */
1416 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001417
1418 psa_algorithm_t hash_alg;
1419 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001420
1421 /* Variables relating to the cipher for the chosen ciphersuite. */
1422 mbedtls_cipher_info_t const *cipher_info;
Xiaofei Baib7972842021-11-18 07:29:56 +00001423 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001424
1425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) );
1426
1427 /* Extract basic information about hash and ciphersuite */
1428
1429 cipher_info = mbedtls_cipher_info_from_type(
XiaokangQian33062842021-11-11 03:37:45 +00001430 handshake->ciphersuite_info->cipher );
Xiaofei Baib7972842021-11-18 07:29:56 +00001431 key_len = cipher_info->key_bitlen / 8;
1432 iv_len = cipher_info->iv_size;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001433
XiaokangQian33062842021-11-11 03:37:45 +00001434 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001435
1436 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
1437 hash_len = PSA_HASH_LENGTH( hash_alg );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001438
1439 /* Compute current handshake transcript. It's the caller's responsiblity
1440 * to call this at the right time, that is, after the ServerFinished. */
1441
1442 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1443 transcript, sizeof( transcript ),
1444 &transcript_len );
1445 if( ret != 0 )
XiaokangQian4cab0242021-10-12 08:43:37 +00001446 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001447
1448 /* Compute application secrets from master secret and transcript hash. */
1449
Gabor Mezei07732f72022-03-26 17:04:19 +01001450 ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001451 handshake->tls13_master_secrets.app,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001452 transcript, transcript_len,
1453 app_secrets );
Jerry Yu4a2fa5d2021-12-10 10:19:34 +08001454 /* Erase master secrets */
Jerry Yu23ab7a42021-12-08 14:34:10 +08001455 mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets,
Jerry Yu27224f52021-12-09 10:56:50 +08001456 sizeof( ssl->handshake->tls13_master_secrets ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001457 if( ret != 0 )
1458 {
1459 MBEDTLS_SSL_DEBUG_RET( 1,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001460 "mbedtls_ssl_tls13_derive_application_secrets", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001461 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001462 }
1463
1464 /* Derive first epoch of IV + Key for application traffic. */
1465
Gabor Mezei07732f72022-03-26 17:04:19 +01001466 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001467 app_secrets->client_application_traffic_secret_N,
1468 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001469 hash_len, key_len, iv_len, traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001470 if( ret != 0 )
1471 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001473 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001474 }
1475
1476 MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret",
1477 app_secrets->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001478 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001479
1480 MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret",
1481 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001482 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001483
XiaokangQianac0385c2021-11-03 06:40:11 +00001484 /*
1485 * Export client/server application traffic secret 0
1486 */
1487 if( ssl->f_export_keys != NULL )
1488 {
1489 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001490 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001491 app_secrets->client_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001492 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001493 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001494 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1495 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001496
1497 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001498 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001499 app_secrets->server_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001500 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001501 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001502 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1503 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001504 }
1505
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001506 MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:",
Xiaofei Baib7972842021-11-18 07:29:56 +00001507 traffic_keys->client_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001508 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key",
Xiaofei Baib7972842021-11-18 07:29:56 +00001509 traffic_keys->server_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001510 MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001511 traffic_keys->client_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001512 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001513 traffic_keys->server_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001514
1515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001516
1517 cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001518 /* randbytes is not used again */
1519 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1520 sizeof( ssl->handshake->randbytes ) );
XiaokangQian33062842021-11-11 03:37:45 +00001521 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001522 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001523}
1524
Ronald Cron6f135e12021-12-08 16:57:54 +01001525#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */