blob: 74b269e6aa049a3c661ec5f3bdfc9c1acf2bb649 [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"
Jerry Yue110d252022-05-05 10:19:22 +080030#include "mbedtls/platform.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080031
32#include "ssl_misc.h"
33#include "ssl_tls13_keys.h"
Gabor Mezeia3eecd22022-02-09 16:57:26 +010034#include "ssl_tls13_invasive.h"
35
36#include "psa/crypto.h"
Jerry Yue3131ef2021-09-16 13:14:15 +080037
Hanno Becker1413bd82020-09-09 12:46:09 +010038#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010039 .name = string,
40
Xiaofei Bai746f9482021-11-12 08:53:56 +000041struct mbedtls_ssl_tls13_labels_struct const mbedtls_ssl_tls13_labels =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010042{
43 /* This seems to work in C, despite the string literal being one
44 * character too long due to the 0-termination. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010045 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Beckerbe9d6642020-08-21 13:20:06 +010046};
47
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010048#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010049
Hanno Beckerbe9d6642020-08-21 13:20:06 +010050/*
51 * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
52 *
53 * The HkdfLabel is specified in RFC 8446 as follows:
54 *
55 * struct HkdfLabel {
56 * uint16 length; // Length of expanded key material
57 * opaque label<7..255>; // Always prefixed by "tls13 "
58 * opaque context<0..255>; // Usually a communication transcript hash
59 * };
60 *
61 * Parameters:
62 * - desired_length: Length of expanded key material
63 * Even though the standard allows expansion to up to
64 * 2**16 Bytes, TLS 1.3 never uses expansion to more than
65 * 255 Bytes, so we require `desired_length` to be at most
66 * 255. This allows us to save a few Bytes of code by
67 * hardcoding the writing of the high bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000068 * - (label, label_len): label + label length, without "tls13 " prefix
69 * The label length MUST be less than or equal to
70 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
71 * It is the caller's responsibility to ensure this.
72 * All (label, label length) pairs used in TLS 1.3
73 * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
74 * - (ctx, ctx_len): context + context length
75 * The context length MUST be less than or equal to
76 * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
77 * It is the caller's responsibility to ensure this.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010078 * - dst: Target buffer for HkdfLabel structure,
79 * This MUST be a writable buffer of size
80 * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
Xiaofei Baifeecbbb2021-11-23 07:24:58 +000081 * - dst_len: Pointer at which to store the actual length of
82 * the HkdfLabel structure on success.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010083 */
84
Xiaofei Baid25fab62021-12-02 06:36:27 +000085static const char tls13_label_prefix[6] = "tls13 ";
Hanno Becker2dfe1322020-09-10 09:23:12 +010086
Hanno Becker9cb0a142020-09-08 10:48:14 +010087#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( label_len, context_len ) \
Hanno Beckerbe9d6642020-08-21 13:20:06 +010088 ( 2 /* expansion length */ \
89 + 1 /* label length */ \
Hanno Becker9cb0a142020-09-08 10:48:14 +010090 + label_len \
Hanno Beckerbe9d6642020-08-21 13:20:06 +010091 + 1 /* context length */ \
Hanno Becker9cb0a142020-09-08 10:48:14 +010092 + context_len )
93
94#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
95 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Xiaofei Baid25fab62021-12-02 06:36:27 +000096 sizeof(tls13_label_prefix) + \
Hanno Becker9cb0a142020-09-08 10:48:14 +010097 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
98 MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010099
Xiaofei Bai746f9482021-11-12 08:53:56 +0000100static void ssl_tls13_hkdf_encode_label(
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100101 size_t desired_length,
Xiaofei Baib7972842021-11-18 07:29:56 +0000102 const unsigned char *label, size_t label_len,
103 const unsigned char *ctx, size_t ctx_len,
104 unsigned char *dst, size_t *dst_len )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100105{
Hanno Becker2dfe1322020-09-10 09:23:12 +0100106 size_t total_label_len =
Xiaofei Baid25fab62021-12-02 06:36:27 +0000107 sizeof(tls13_label_prefix) + label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100108 size_t total_hkdf_lbl_len =
Xiaofei Baib7972842021-11-18 07:29:56 +0000109 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, ctx_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100110
111 unsigned char *p = dst;
112
Hanno Becker531fe302020-09-16 09:45:27 +0100113 /* Add the size of the expanded key material.
114 * We're hardcoding the high byte to 0 here assuming that we never use
115 * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
116#if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
Xiaofei Bai746f9482021-11-12 08:53:56 +0000117#error "The implementation of ssl_tls13_hkdf_encode_label() is not fit for the \
Hanno Becker531fe302020-09-16 09:45:27 +0100118 value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
119#endif
120
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100121 *p++ = 0;
Joe Subbiani2194dc42021-07-14 12:31:31 +0100122 *p++ = MBEDTLS_BYTE_0( desired_length );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100123
124 /* Add label incl. prefix */
Joe Subbiani2194dc42021-07-14 12:31:31 +0100125 *p++ = MBEDTLS_BYTE_0( total_label_len );
Xiaofei Baid25fab62021-12-02 06:36:27 +0000126 memcpy( p, tls13_label_prefix, sizeof(tls13_label_prefix) );
127 p += sizeof(tls13_label_prefix);
Xiaofei Baib7972842021-11-18 07:29:56 +0000128 memcpy( p, label, label_len );
129 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100130
131 /* Add context value */
Xiaofei Baib7972842021-11-18 07:29:56 +0000132 *p++ = MBEDTLS_BYTE_0( ctx_len );
133 if( ctx_len != 0 )
134 memcpy( p, ctx, ctx_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100135
136 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000137 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100138}
139
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100140MBEDTLS_STATIC_TESTABLE
Gabor Mezeied6d6582022-03-26 17:28:06 +0100141psa_status_t mbedtls_psa_hkdf_extract( psa_algorithm_t hash_alg,
Gabor Mezei62bf0242022-02-07 18:12:07 +0100142 const unsigned char *salt, size_t salt_len,
143 const unsigned char *ikm, size_t ikm_len,
144 unsigned char *prk, size_t prk_size,
145 size_t *prk_len )
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100146{
147 unsigned char null_salt[PSA_MAC_MAX_SIZE] = { '\0' };
148 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gabor Mezei26c67412022-02-15 15:46:17 +0100150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei0e7c6f42022-02-15 15:47:54 +0100151 psa_status_t destroy_status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100152 psa_algorithm_t alg = PSA_ALG_HMAC( hash_alg );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100153
154 if( salt == NULL || salt_len == 0 )
155 {
156 size_t hash_len;
157
158 if( salt_len != 0 )
159 {
Gabor Mezeic5efb8e2022-02-08 13:15:45 +0100160 return( PSA_ERROR_INVALID_ARGUMENT );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100161 }
162
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100163 hash_len = PSA_HASH_LENGTH( alg );
164
165 if( hash_len == 0 )
166 {
Gabor Mezeic5efb8e2022-02-08 13:15:45 +0100167 return( PSA_ERROR_INVALID_ARGUMENT );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100168 }
169
Gabor Mezeid860e0f2022-02-15 16:02:59 +0100170 /* salt_len <= sizeof( salt ) because
171 PSA_HASH_LENGTH( alg ) <= PSA_MAC_MAX_SIZE. */
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100172 salt = null_salt;
173 salt_len = hash_len;
174 }
175
176 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
177 psa_set_key_algorithm( &attributes, alg );
178 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
179
Gabor Mezei26c67412022-02-15 15:46:17 +0100180 status = psa_import_key( &attributes, salt, salt_len, &key );
181 if( status != PSA_SUCCESS )
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100182 {
183 goto cleanup;
184 }
185
Gabor Mezei26c67412022-02-15 15:46:17 +0100186 status = psa_mac_compute( key, alg, ikm, ikm_len, prk, prk_size, prk_len );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100187
188cleanup:
Gabor Mezei0e7c6f42022-02-15 15:47:54 +0100189 destroy_status = psa_destroy_key( key );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100190
Gabor Mezei0e7c6f42022-02-15 15:47:54 +0100191 return( ( status == PSA_SUCCESS ) ? destroy_status : status );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100192}
193
194MBEDTLS_STATIC_TESTABLE
Gabor Mezeied6d6582022-03-26 17:28:06 +0100195psa_status_t mbedtls_psa_hkdf_expand( psa_algorithm_t hash_alg,
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100196 const unsigned char *prk, size_t prk_len,
197 const unsigned char *info, size_t info_len,
198 unsigned char *okm, size_t okm_len )
199{
200 size_t hash_len;
201 size_t where = 0;
202 size_t n;
203 size_t t_len = 0;
204 size_t i;
205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
207 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gabor Mezei833713c2022-02-15 16:16:08 +0100208 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100209 psa_status_t destroy_status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100210 unsigned char t[PSA_MAC_MAX_SIZE];
Gabor Mezeied6d6582022-03-26 17:28:06 +0100211 psa_algorithm_t alg = PSA_ALG_HMAC( hash_alg );
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100212
213 if( okm == NULL )
214 {
215 return( PSA_ERROR_INVALID_ARGUMENT );
216 }
217
218 hash_len = PSA_HASH_LENGTH( alg );
219
220 if( prk_len < hash_len || hash_len == 0 )
221 {
222 return( PSA_ERROR_INVALID_ARGUMENT );
223 }
224
225 if( info == NULL )
226 {
227 info = (const unsigned char *) "";
228 info_len = 0;
229 }
230
231 n = okm_len / hash_len;
232
233 if( okm_len % hash_len != 0 )
234 {
235 n++;
236 }
237
238 /*
239 * Per RFC 5869 Section 2.3, okm_len must not exceed
240 * 255 times the hash length
241 */
242 if( n > 255 )
243 {
244 return( PSA_ERROR_INVALID_ARGUMENT );
245 }
246
247 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
248 psa_set_key_algorithm( &attributes, alg );
249 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
250
Gabor Mezei833713c2022-02-15 16:16:08 +0100251 status = psa_import_key( &attributes, prk, prk_len, &key );
252 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100253 {
254 goto cleanup;
255 }
256
257 memset( t, 0, hash_len );
258
259 /*
260 * Compute T = T(1) | T(2) | T(3) | ... | T(N)
261 * Where T(N) is defined in RFC 5869 Section 2.3
262 */
263 for( i = 1; i <= n; i++ )
264 {
265 size_t num_to_copy;
266 unsigned char c = i & 0xff;
267 size_t len;
268
Gabor Mezei833713c2022-02-15 16:16:08 +0100269 status = psa_mac_sign_setup( &operation, key, alg );
270 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100271 {
272 goto cleanup;
273 }
274
Gabor Mezei833713c2022-02-15 16:16:08 +0100275 status = psa_mac_update( &operation, t, t_len );
276 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100277 {
278 goto cleanup;
279 }
280
Gabor Mezei833713c2022-02-15 16:16:08 +0100281 status = psa_mac_update( &operation, info, info_len );
282 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100283 {
284 goto cleanup;
285 }
286
Gabor Mezei8e360252022-02-17 11:50:02 +0100287 /* The constant concatenated to the end of each T(n) is a single octet. */
Gabor Mezei833713c2022-02-15 16:16:08 +0100288 status = psa_mac_update( &operation, &c, 1 );
289 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100290 {
291 goto cleanup;
292 }
293
Gabor Mezei833713c2022-02-15 16:16:08 +0100294 status = psa_mac_sign_finish( &operation, t, PSA_MAC_MAX_SIZE, &len );
295 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100296 {
297 goto cleanup;
298 }
299
300 num_to_copy = i != n ? hash_len : okm_len - where;
301 memcpy( okm + where, t, num_to_copy );
302 where += hash_len;
303 t_len = hash_len;
304 }
305
306cleanup:
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100307 if( status != PSA_SUCCESS )
308 psa_mac_abort( &operation );
309 destroy_status = psa_destroy_key( key );
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100310
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100311 mbedtls_platform_zeroize( t, sizeof( t ) );
312
313 return( ( status == PSA_SUCCESS ) ? destroy_status : status );
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100314}
315
Xiaofei Bai746f9482021-11-12 08:53:56 +0000316int mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100317 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000318 const unsigned char *secret, size_t secret_len,
319 const unsigned char *label, size_t label_len,
320 const unsigned char *ctx, size_t ctx_len,
321 unsigned char *buf, size_t buf_len )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100322{
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100323 unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ];
324 size_t hkdf_label_len;
325
Xiaofei Baib7972842021-11-18 07:29:56 +0000326 if( label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100327 {
328 /* Should never happen since this is an internal
329 * function, and we know statically which labels
330 * are allowed. */
331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
332 }
333
Xiaofei Baib7972842021-11-18 07:29:56 +0000334 if( ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100335 {
336 /* Should not happen, as above. */
337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
338 }
339
Xiaofei Baib7972842021-11-18 07:29:56 +0000340 if( buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100341 {
342 /* Should not happen, as above. */
343 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
344 }
345
Gabor Mezei07732f72022-03-26 17:04:19 +0100346 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
348
Xiaofei Baib7972842021-11-18 07:29:56 +0000349 ssl_tls13_hkdf_encode_label( buf_len,
350 label, label_len,
351 ctx, ctx_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000352 hkdf_label,
353 &hkdf_label_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100354
Gabor Mezei58db6532022-03-21 12:12:37 +0100355 return( psa_ssl_status_to_mbedtls(
Gabor Mezeied6d6582022-03-26 17:28:06 +0100356 mbedtls_psa_hkdf_expand( hash_alg,
Gabor Mezei58db6532022-03-21 12:12:37 +0100357 secret, secret_len,
358 hkdf_label, hkdf_label_len,
359 buf, buf_len ) ) );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100360}
361
Hanno Becker3385a4d2020-08-21 13:03:34 +0100362/*
363 * The traffic keying material is generated from the following inputs:
364 *
365 * - One secret value per sender.
366 * - A purpose value indicating the specific value being generated
367 * - The desired lengths of key and IV.
368 *
369 * The expansion itself is based on HKDF:
370 *
371 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
372 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
373 *
374 * [sender] denotes the sending side and the Secret value is provided
375 * by the function caller. Note that we generate server and client side
376 * keys in a single function call.
377 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000378int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100379 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100380 const unsigned char *client_secret,
Xiaofei Baib7972842021-11-18 07:29:56 +0000381 const unsigned char *server_secret, size_t secret_len,
382 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100383 mbedtls_ssl_key_set *keys )
384{
385 int ret = 0;
386
Xiaofei Bai746f9482021-11-12 08:53:56 +0000387 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000388 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100389 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
390 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100391 keys->client_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100392 if( ret != 0 )
393 return( ret );
394
Xiaofei Bai746f9482021-11-12 08:53:56 +0000395 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000396 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100397 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
398 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100399 keys->server_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100400 if( ret != 0 )
401 return( ret );
402
Xiaofei Bai746f9482021-11-12 08:53:56 +0000403 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000404 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100405 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
406 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100407 keys->client_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100408 if( ret != 0 )
409 return( ret );
410
Xiaofei Bai746f9482021-11-12 08:53:56 +0000411 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000412 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100413 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
414 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100415 keys->server_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100416 if( ret != 0 )
417 return( ret );
418
Hanno Becker493ea7f2020-09-08 11:01:00 +0100419 keys->key_len = key_len;
420 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100421
422 return( 0 );
423}
424
Xiaofei Bai746f9482021-11-12 08:53:56 +0000425int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100426 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000427 const unsigned char *secret, size_t secret_len,
428 const unsigned char *label, size_t label_len,
429 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100430 int ctx_hashed,
Xiaofei Baib7972842021-11-18 07:29:56 +0000431 unsigned char *dstbuf, size_t dstbuf_len )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100432{
433 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100434 unsigned char hashed_context[ PSA_HASH_MAX_SIZE ];
Hanno Becker0c42fd92020-09-09 12:58:29 +0100435 if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100436 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100437 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
438
439 status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context,
440 PSA_HASH_LENGTH( hash_alg ), &ctx_len );
441 if( status != PSA_SUCCESS )
442 {
443 ret = psa_ssl_status_to_mbedtls( status );
444 return ret;
445 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100446 }
447 else
448 {
Xiaofei Baib7972842021-11-18 07:29:56 +0000449 if( ctx_len > sizeof(hashed_context) )
Hanno Becker97a21562020-09-09 12:57:16 +0100450 {
451 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100452 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100453 * Let's double-check nonetheless to not run at the risk
454 * of getting a stack overflow. */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Becker97a21562020-09-09 12:57:16 +0100456 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100457
Xiaofei Baib7972842021-11-18 07:29:56 +0000458 memcpy( hashed_context, ctx, ctx_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100459 }
460
Xiaofei Bai746f9482021-11-12 08:53:56 +0000461 return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000462 secret, secret_len,
463 label, label_len,
464 hashed_context, ctx_len,
465 dstbuf, dstbuf_len ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100466
Hanno Beckerb35d5222020-08-21 13:27:44 +0100467}
468
Xiaofei Bai746f9482021-11-12 08:53:56 +0000469int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100470 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100471 const unsigned char *secret_old,
472 const unsigned char *input, size_t input_len,
473 unsigned char *secret_new )
474{
475 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
476 size_t hlen, ilen;
Gabor Mezei07732f72022-03-26 17:04:19 +0100477 unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 };
Jerry Yu6eaa41c2021-11-22 18:16:45 +0800478 unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 };
Gabor Mezei58db6532022-03-21 12:12:37 +0100479 size_t secret_len;
Gabor Mezei07732f72022-03-26 17:04:19 +0100480
481 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100482 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
483
Gabor Mezei07732f72022-03-26 17:04:19 +0100484 hlen = PSA_HASH_LENGTH( hash_alg );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100485
486 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100487 * on the old secret. */
Hanno Beckere9cccb42020-08-20 13:42:46 +0100488 if( secret_old != NULL )
489 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000490 ret = mbedtls_ssl_tls13_derive_secret(
Hanno Beckere9cccb42020-08-20 13:42:46 +0100491 hash_alg,
492 secret_old, hlen,
493 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
494 NULL, 0, /* context */
495 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Hanno Becker59b50a12020-09-09 10:56:56 +0100496 tmp_secret, hlen );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100497 if( ret != 0 )
498 goto cleanup;
499 }
500
501 if( input != NULL )
502 {
Hanno Becker59b50a12020-09-09 10:56:56 +0100503 memcpy( tmp_input, input, input_len );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100504 ilen = input_len;
505 }
506 else
507 {
508 ilen = hlen;
509 }
510
511 /* HKDF-Extract takes a salt and input key material.
512 * The salt is the old secret, and the input key material
513 * is the input secret (PSK / ECDHE). */
Gabor Mezei58db6532022-03-21 12:12:37 +0100514 ret = psa_ssl_status_to_mbedtls(
Gabor Mezeied6d6582022-03-26 17:28:06 +0100515 mbedtls_psa_hkdf_extract( hash_alg,
Gabor Mezei58db6532022-03-21 12:12:37 +0100516 tmp_secret, hlen,
517 tmp_input, ilen,
518 secret_new, hlen, &secret_len ) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100519
520 cleanup:
521
Hanno Becker59b50a12020-09-09 10:56:56 +0100522 mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
523 mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100524 return( ret );
525}
526
Xiaofei Bai746f9482021-11-12 08:53:56 +0000527int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100528 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100529 unsigned char const *early_secret,
530 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000531 mbedtls_ssl_tls13_early_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100532{
533 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100534 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100535
536 /* We should never call this function with an unknown hash,
537 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100538 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100539 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
540
541 /*
542 * 0
543 * |
544 * v
545 * PSK -> HKDF-Extract = Early Secret
546 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100547 * +-----> Derive-Secret(., "c e traffic", ClientHello)
548 * | = client_early_traffic_secret
549 * |
550 * +-----> Derive-Secret(., "e exp master", ClientHello)
551 * | = early_exporter_master_secret
552 * v
553 */
554
555 /* Create client_early_traffic_secret */
Gabor Mezei07732f72022-03-26 17:04:19 +0100556 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
557 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100558 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ),
559 transcript, transcript_len,
560 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
561 derived->client_early_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100562 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100563 if( ret != 0 )
564 return( ret );
565
566 /* Create early exporter */
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( e_exp_master ),
570 transcript, transcript_len,
571 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
572 derived->early_exporter_master_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 return( 0 );
578}
579
Xiaofei Bai746f9482021-11-12 08:53:56 +0000580int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100581 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100582 unsigned char const *handshake_secret,
583 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000584 mbedtls_ssl_tls13_handshake_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100585{
586 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100587 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100588
589 /* We should never call this function with an unknown hash,
590 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100591 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
593
594 /*
595 *
596 * Handshake Secret
597 * |
598 * +-----> Derive-Secret( ., "c hs traffic",
599 * | ClientHello...ServerHello )
600 * | = client_handshake_traffic_secret
601 * |
602 * +-----> Derive-Secret( ., "s hs traffic",
603 * | ClientHello...ServerHello )
604 * | = server_handshake_traffic_secret
605 *
606 */
607
608 /*
609 * Compute client_handshake_traffic_secret with
610 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
611 */
612
Gabor Mezei07732f72022-03-26 17:04:19 +0100613 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
614 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100615 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ),
616 transcript, transcript_len,
617 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
618 derived->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100619 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100620 if( ret != 0 )
621 return( ret );
622
623 /*
624 * Compute server_handshake_traffic_secret with
625 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
626 */
627
Gabor Mezei07732f72022-03-26 17:04:19 +0100628 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
629 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100630 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ),
631 transcript, transcript_len,
632 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
633 derived->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100634 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100635 if( ret != 0 )
636 return( ret );
637
638 return( 0 );
639}
640
Xiaofei Bai746f9482021-11-12 08:53:56 +0000641int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100642 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100643 unsigned char const *application_secret,
644 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000645 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100646{
647 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100648 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100649
650 /* We should never call this function with an unknown hash,
651 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100652 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100653 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
654
655 /* Generate {client,server}_application_traffic_secret_0
656 *
657 * Master Secret
658 * |
659 * +-----> Derive-Secret( ., "c ap traffic",
660 * | ClientHello...server Finished )
661 * | = client_application_traffic_secret_0
662 * |
663 * +-----> Derive-Secret( ., "s ap traffic",
664 * | ClientHello...Server Finished )
665 * | = server_application_traffic_secret_0
666 * |
667 * +-----> Derive-Secret( ., "exp master",
668 * | ClientHello...server Finished)
669 * | = exporter_master_secret
670 *
671 */
672
Gabor Mezei07732f72022-03-26 17:04:19 +0100673 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
674 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100675 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ),
676 transcript, transcript_len,
677 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
678 derived->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100679 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100680 if( ret != 0 )
681 return( ret );
682
Gabor Mezei07732f72022-03-26 17:04:19 +0100683 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
684 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100685 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ),
686 transcript, transcript_len,
687 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
688 derived->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100689 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100690 if( ret != 0 )
691 return( ret );
692
Gabor Mezei07732f72022-03-26 17:04:19 +0100693 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
694 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100695 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ),
696 transcript, transcript_len,
697 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
698 derived->exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100699 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100700 if( ret != 0 )
701 return( ret );
702
703 return( 0 );
704}
705
706/* Generate resumption_master_secret for use with the ticket exchange.
707 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000708 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100709 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000710int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100711 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100712 unsigned char const *application_secret,
713 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000714 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100715{
716 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100717 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100718
719 /* We should never call this function with an unknown hash,
720 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100721 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100722 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
723
Gabor Mezei07732f72022-03-26 17:04:19 +0100724 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
725 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100726 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ),
727 transcript, transcript_len,
728 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
729 derived->resumption_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100730 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100731
732 if( ret != 0 )
733 return( ret );
734
735 return( 0 );
736}
737
XiaokangQiana4c99f22021-11-11 06:46:35 +0000738int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000739{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000741 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +0100742 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
743 handshake->ciphersuite_info->mac );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000744
745 /*
746 * Compute MasterSecret
747 */
Gabor Mezei07732f72022-03-26 17:04:19 +0100748 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000749 handshake->tls13_master_secrets.handshake,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000750 NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000751 handshake->tls13_master_secrets.app );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000752 if( ret != 0 )
753 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000754 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000755 return( ret );
756 }
757
758 MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret",
Gabor Mezei07732f72022-03-26 17:04:19 +0100759 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000760
761 return( 0 );
762}
763
Gabor Mezei07732f72022-03-26 17:04:19 +0100764static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000765 unsigned char const *base_key,
766 unsigned char const *transcript,
Gabor Mezei07732f72022-03-26 17:04:19 +0100767 unsigned char *dst,
768 size_t *dst_len )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100769{
Gabor Mezei07732f72022-03-26 17:04:19 +0100770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
772 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
773 size_t hash_len = PSA_HASH_LENGTH( hash_alg );
774 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100775 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100776 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100777
778 /* We should never call this function with an unknown hash,
779 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100780 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
782
783 /* TLS 1.3 Finished message
784 *
785 * struct {
786 * opaque verify_data[Hash.length];
787 * } Finished;
788 *
789 * verify_data =
790 * HMAC( finished_key,
791 * Hash( Handshake Context +
792 * Certificate* +
793 * CertificateVerify* )
794 * )
795 *
796 * finished_key =
797 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
798 */
799
Xiaofei Bai746f9482021-11-12 08:53:56 +0000800 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100801 hash_alg, base_key, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100802 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ),
803 NULL, 0,
Gabor Mezei07732f72022-03-26 17:04:19 +0100804 finished_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100805 if( ret != 0 )
806 goto exit;
807
Gabor Mezei07732f72022-03-26 17:04:19 +0100808 alg = PSA_ALG_HMAC( hash_alg );
809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
810 psa_set_key_algorithm( &attributes, alg );
811 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
812
813 status = psa_import_key( &attributes, finished_key, hash_len, &key );
814 if( status != PSA_SUCCESS )
815 {
816 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100817 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100818 }
819
820 status = psa_mac_compute( key, alg, transcript, hash_len,
821 dst, hash_len, dst_len );
822 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100823
824exit:
825
Gabor Mezei07732f72022-03-26 17:04:19 +0100826 status = psa_destroy_key( key );
827 if( ret == 0 )
828 ret = psa_ssl_status_to_mbedtls( status );
829
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100830 mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100831
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100832 return( ret );
833}
834
XiaokangQianc5c39d52021-11-09 11:55:10 +0000835int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000836 unsigned char* dst,
837 size_t dst_len,
838 size_t *actual_len,
839 int from )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000840{
XiaokangQiana7634982021-10-22 06:32:32 +0000841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000842
XiaokangQianaaa0e192021-11-10 03:07:04 +0000843 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000844 size_t transcript_len;
845
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800846 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800847 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800848 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
849 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800850
851 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100852
853 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(
854 ssl->handshake->ciphersuite_info->mac );
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200855 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yua5563f62021-12-10 18:14:36 +0800856
857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) );
858
Jerry Yub737f6a2021-12-10 17:55:23 +0800859 if( from == MBEDTLS_SSL_IS_CLIENT )
860 {
861 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
862 base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret );
863 }
864 else
865 {
866 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
867 base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret );
868 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000869
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200870 if( dst_len < hash_len )
Jerry Yu9c074732021-12-10 17:12:43 +0800871 {
872 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
873 goto exit;
874 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000875
876 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
877 transcript, sizeof( transcript ),
878 &transcript_len );
879 if( ret != 0 )
880 {
881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000882 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000883 }
884 MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len );
885
Gabor Mezei07732f72022-03-26 17:04:19 +0100886 ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000887 if( ret != 0 )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000888 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000889
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200890 MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000892
893exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800894 /* Erase handshake secrets */
Jerry Yub737f6a2021-12-10 17:55:23 +0800895 mbedtls_platform_zeroize( base_key, base_key_len );
XiaokangQian33062842021-11-11 03:37:45 +0000896 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000897 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000898}
899
Xiaofei Bai746f9482021-11-12 08:53:56 +0000900int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100901 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100902 unsigned char const *psk, size_t psk_len,
903 int psk_type,
904 unsigned char const *transcript,
905 unsigned char *result )
906{
907 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100908 unsigned char binder_key[PSA_MAC_MAX_SIZE];
909 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gabor Mezei07732f72022-03-26 17:04:19 +0100910 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
911 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100912
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100913#if !defined(MBEDTLS_DEBUG_C)
914 ssl = NULL; /* make sure we don't use it except for debug */
915 ((void) ssl);
916#endif
917
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100918 /* We should never call this function with an unknown hash,
919 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100920 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100921 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
922
923 /*
924 * 0
925 * |
926 * v
927 * PSK -> HKDF-Extract = Early Secret
928 * |
929 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
930 * | = binder_key
931 * v
932 */
933
Gabor Mezei07732f72022-03-26 17:04:19 +0100934 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000935 NULL, /* Old secret */
936 psk, psk_len, /* Input */
937 early_secret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100938 if( ret != 0 )
939 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000940 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100941 goto exit;
942 }
943
944 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
945 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100946 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
947 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100948 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ),
949 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100950 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100951 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) );
952 }
953 else
954 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100955 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
956 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100957 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ),
958 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100959 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100960 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) );
961 }
962
963 if( ret != 0 )
964 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000965 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100966 goto exit;
967 }
968
969 /*
970 * The binding_value is computed in the same way as the Finished message
971 * but with the BaseKey being the binder_key.
972 */
973
Gabor Mezei07732f72022-03-26 17:04:19 +0100974 ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript,
975 result, &actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100976 if( ret != 0 )
977 goto exit;
978
Gabor Mezei07732f72022-03-26 17:04:19 +0100979 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100980
981exit:
982
983 mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) );
984 mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) );
985 return( ret );
986}
987
Hanno Beckerc94060c2021-03-22 07:50:44 +0000988int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
989 int endpoint,
990 int ciphersuite,
991 mbedtls_ssl_key_set const *traffic_keys,
992 mbedtls_ssl_context *ssl /* DEBUG ONLY */ )
993{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100994#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000995 int ret;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100996#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000997 mbedtls_cipher_info_t const *cipher_info;
998 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
999 unsigned char const *key_enc;
1000 unsigned char const *iv_enc;
1001 unsigned char const *key_dec;
1002 unsigned char const *iv_dec;
1003
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001004#if defined(MBEDTLS_USE_PSA_CRYPTO)
1005 psa_key_type_t key_type;
1006 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1007 psa_algorithm_t alg;
1008 size_t key_bits;
1009 psa_status_t status = PSA_SUCCESS;
1010#endif
1011
Hanno Beckerc94060c2021-03-22 07:50:44 +00001012#if !defined(MBEDTLS_DEBUG_C)
1013 ssl = NULL; /* make sure we don't use it except for those cases */
1014 (void) ssl;
1015#endif
1016
1017 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker7887a772021-04-20 05:27:57 +01001018 if( ciphersuite_info == NULL )
1019 {
1020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
1021 ciphersuite ) );
1022 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1023 }
Hanno Beckerc94060c2021-03-22 07:50:44 +00001024
1025 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
1026 if( cipher_info == NULL )
1027 {
Hanno Becker7887a772021-04-20 05:27:57 +01001028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
1029 ciphersuite_info->cipher ) );
Hanno Beckerf62a7302021-04-21 05:21:28 +01001030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerc94060c2021-03-22 07:50:44 +00001031 }
1032
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001033#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001034 /*
1035 * Setup cipher contexts in target transform
1036 */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001037 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
1038 cipher_info ) ) != 0 )
1039 {
1040 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1041 return( ret );
1042 }
1043
1044 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
1045 cipher_info ) ) != 0 )
1046 {
1047 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1048 return( ret );
1049 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001051
1052#if defined(MBEDTLS_SSL_SRV_C)
1053 if( endpoint == MBEDTLS_SSL_IS_SERVER )
1054 {
1055 key_enc = traffic_keys->server_write_key;
1056 key_dec = traffic_keys->client_write_key;
1057 iv_enc = traffic_keys->server_write_iv;
1058 iv_dec = traffic_keys->client_write_iv;
1059 }
1060 else
1061#endif /* MBEDTLS_SSL_SRV_C */
1062#if defined(MBEDTLS_SSL_CLI_C)
1063 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
1064 {
1065 key_enc = traffic_keys->client_write_key;
1066 key_dec = traffic_keys->server_write_key;
1067 iv_enc = traffic_keys->client_write_iv;
1068 iv_dec = traffic_keys->server_write_iv;
1069 }
1070 else
1071#endif /* MBEDTLS_SSL_CLI_C */
1072 {
1073 /* should not happen */
1074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1075 }
1076
1077 memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len );
1078 memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len );
1079
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001080#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001081 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc,
1082 key_enc, cipher_info->key_bitlen,
1083 MBEDTLS_ENCRYPT ) ) != 0 )
1084 {
1085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1086 return( ret );
1087 }
1088
1089 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec,
1090 key_dec, cipher_info->key_bitlen,
1091 MBEDTLS_DECRYPT ) ) != 0 )
1092 {
1093 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1094 return( ret );
1095 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001096#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001097
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001098 /*
1099 * Setup other fields in SSL transform
1100 */
1101
1102 if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 )
1103 transform->taglen = 8;
1104 else
1105 transform->taglen = 16;
1106
1107 transform->ivlen = traffic_keys->iv_len;
1108 transform->maclen = 0;
1109 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001110 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001111
1112 /* We add the true record content type (1 Byte) to the plaintext and
1113 * then pad to the configured granularity. The mimimum length of the
1114 * type-extended and padded plaintext is therefore the padding
1115 * granularity. */
1116 transform->minlen =
1117 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1118
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001119#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001120 /*
1121 * Setup psa keys and alg
1122 */
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001123 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001124 transform->taglen,
1125 &alg,
1126 &key_type,
1127 &key_bits ) ) != PSA_SUCCESS )
1128 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) );
1130 return( psa_ssl_status_to_mbedtls( status ) );
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001131 }
1132
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001133 transform->psa_alg = alg;
1134
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001135 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001136 {
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1138 psa_set_key_algorithm( &attributes, alg );
1139 psa_set_key_type( &attributes, key_type );
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001140
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001141 if( ( status = psa_import_key( &attributes,
1142 key_enc,
1143 PSA_BITS_TO_BYTES( key_bits ),
1144 &transform->psa_key_enc ) ) != PSA_SUCCESS )
1145 {
1146 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1147 return( psa_ssl_status_to_mbedtls( status ) );
1148 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001149
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001150 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1151
1152 if( ( status = psa_import_key( &attributes,
1153 key_dec,
1154 PSA_BITS_TO_BYTES( key_bits ),
1155 &transform->psa_key_dec ) ) != 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 Stekielae77b0a2022-01-12 10:29:03 +01001160 }
1161#endif /* MBEDTLS_USE_PSA_CRYPTO */
1162
Hanno Beckerc94060c2021-03-22 07:50:44 +00001163 return( 0 );
1164}
1165
Xiaofei Bai746f9482021-11-12 08:53:56 +00001166int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl )
Jerry Yu89ea3212021-09-09 14:31:24 +08001167{
Jerry Yue3131ef2021-09-16 13:14:15 +08001168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001169 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001170 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001171
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001172 if( handshake->ciphersuite_info == NULL )
Jerry Yu89ea3212021-09-09 14:31:24 +08001173 {
1174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) );
1175 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1176 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001177
Gabor Mezei07732f72022-03-26 17:04:19 +01001178 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
Jerry Yue06f4532021-09-23 18:35:07 +08001179
Gabor Mezei07732f72022-03-26 17:04:19 +01001180 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001181 handshake->tls13_master_secrets.early );
Jerry Yu89ea3212021-09-09 14:31:24 +08001182 if( ret != 0 )
1183 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yu89ea3212021-09-09 14:31:24 +08001185 return( ret );
1186 }
1187
1188 return( 0 );
1189}
1190
Jerry Yuc068b662021-10-11 22:30:19 +08001191/* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
Jerry Yu61e35e02021-09-16 18:59:08 +08001192 * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
Jerry Yuc068b662021-10-11 22:30:19 +08001193int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
1194 mbedtls_ssl_key_set *traffic_keys )
Jerry Yu61e35e02021-09-16 18:59:08 +08001195{
1196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1197
1198 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001199
1200 psa_algorithm_t hash_alg;
1201 size_t hash_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001202
Jerry Yu435208a2021-10-13 11:22:16 +08001203 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001204 size_t transcript_len;
1205
1206 mbedtls_cipher_info_t const *cipher_info;
Xiaofei Baib7972842021-11-18 07:29:56 +00001207 size_t key_len, iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001208
Jerry Yu435208a2021-10-13 11:22:16 +08001209 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1210 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001211 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001212
Jerry Yuc068b662021-10-11 22:30:19 +08001213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001214
Jerry Yu435208a2021-10-13 11:22:16 +08001215 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Xiaofei Baib7972842021-11-18 07:29:56 +00001216 key_len = cipher_info->key_bitlen >> 3;
1217 iv_len = cipher_info->iv_size;
Jerry Yu61e35e02021-09-16 18:59:08 +08001218
Jerry Yu435208a2021-10-13 11:22:16 +08001219 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001220
1221 hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
1222 hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yu61e35e02021-09-16 18:59:08 +08001223
1224 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1225 transcript,
1226 sizeof( transcript ),
1227 &transcript_len );
1228 if( ret != 0 )
1229 {
1230 MBEDTLS_SSL_DEBUG_RET( 1,
1231 "mbedtls_ssl_get_handshake_transcript",
1232 ret );
1233 return( ret );
1234 }
1235
Gabor Mezei07732f72022-03-26 17:04:19 +01001236 ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001237 handshake->tls13_master_secrets.handshake,
Jerry Yu435208a2021-10-13 11:22:16 +08001238 transcript, transcript_len, tls13_hs_secrets );
Jerry Yu61e35e02021-09-16 18:59:08 +08001239 if( ret != 0 )
1240 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001241 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets",
Jerry Yu61e35e02021-09-16 18:59:08 +08001242 ret );
1243 return( ret );
1244 }
1245
1246 MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001247 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001248 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001249 MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001250 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001251 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001252
1253 /*
1254 * Export client handshake traffic secret
1255 */
Jerry Yu61e35e02021-09-16 18:59:08 +08001256 if( ssl->f_export_keys != NULL )
1257 {
1258 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001259 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001260 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001261 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001262 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001263 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001264 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1265
1266 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001267 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001268 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001269 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001270 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001271 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001272 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1273 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001274
Gabor Mezei07732f72022-03-26 17:04:19 +01001275 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
Jerry Yu435208a2021-10-13 11:22:16 +08001276 tls13_hs_secrets->client_handshake_traffic_secret,
1277 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001278 hash_len, key_len, iv_len, traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +08001279 if( ret != 0 )
1280 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
Jerry Yu61e35e02021-09-16 18:59:08 +08001282 goto exit;
1283 }
1284
1285 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key",
1286 traffic_keys->client_write_key,
1287 traffic_keys->key_len);
1288
1289 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key",
1290 traffic_keys->server_write_key,
1291 traffic_keys->key_len);
1292
1293 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv",
1294 traffic_keys->client_write_iv,
1295 traffic_keys->iv_len);
1296
1297 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv",
1298 traffic_keys->server_write_iv,
1299 traffic_keys->iv_len);
1300
Jerry Yuc068b662021-10-11 22:30:19 +08001301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001302
1303exit:
1304
1305 return( ret );
1306}
1307
Jerry Yuf0ac2352021-10-11 17:47:07 +08001308int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl )
Jerry Yua0650eb2021-09-09 17:14:45 +08001309{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001311#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C)
1312 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1313#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001314 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +01001315 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
1316 handshake->ciphersuite_info->mac );
Jerry Yua0650eb2021-09-09 17:14:45 +08001317
Jerry Yuf0ac2352021-10-11 17:47:07 +08001318#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
1319 /*
1320 * Compute ECDHE secret used to compute the handshake secret from which
1321 * client_handshake_traffic_secret and server_handshake_traffic_secret
1322 * are derived in the handshake secret derivation stage.
1323 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001324 if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) )
Jerry Yuf0ac2352021-10-11 17:47:07 +08001325 {
1326 if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) )
1327 {
1328#if defined(MBEDTLS_ECDH_C)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001329 /* Compute ECDH shared secret. */
1330 status = psa_raw_key_agreement(
1331 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1332 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1333 handshake->premaster, sizeof( handshake->premaster ),
1334 &handshake->pmslen );
1335 if( status != PSA_SUCCESS )
1336 {
1337 ret = psa_ssl_status_to_mbedtls( status );
1338 MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret );
1339 return( ret );
1340 }
1341
1342 status = psa_destroy_key( handshake->ecdh_psa_privkey );
1343 if( status != PSA_SUCCESS )
1344 {
1345 ret = psa_ssl_status_to_mbedtls( status );
1346 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
1347 return( ret );
1348 }
1349
1350 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001351#endif /* MBEDTLS_ECDH_C */
1352 }
1353 else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) )
1354 {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) );
1356 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1357 }
1358 }
1359#else
1360 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1361#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001362
1363 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001364 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001365 */
Gabor Mezei07732f72022-03-26 17:04:19 +01001366 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001367 handshake->tls13_master_secrets.early,
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001368 handshake->premaster, handshake->pmslen,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001369 handshake->tls13_master_secrets.handshake );
Jerry Yua0650eb2021-09-09 17:14:45 +08001370 if( ret != 0 )
1371 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yua0650eb2021-09-09 17:14:45 +08001373 return( ret );
1374 }
1375
1376 MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret",
Gabor Mezei07732f72022-03-26 17:04:19 +01001377 handshake->tls13_master_secrets.handshake,
1378 PSA_HASH_LENGTH( hash_alg ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001379
1380#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001381 mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001382#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
1383 return( 0 );
1384}
1385
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001386/* Generate application traffic keys since any records following a 1-RTT Finished message
1387 * MUST be encrypted under the application traffic key.
1388 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001389int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001390 mbedtls_ssl_context *ssl,
1391 mbedtls_ssl_key_set *traffic_keys )
1392{
XiaokangQiana7634982021-10-22 06:32:32 +00001393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001394 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001395
1396 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001397 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001398 &ssl->session_negotiate->app_secrets;
1399
1400 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001401 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001402 size_t transcript_len;
1403
1404 /* Variables relating to the hash for the chosen ciphersuite. */
1405 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001406
1407 psa_algorithm_t hash_alg;
1408 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001409
1410 /* Variables relating to the cipher for the chosen ciphersuite. */
1411 mbedtls_cipher_info_t const *cipher_info;
Xiaofei Baib7972842021-11-18 07:29:56 +00001412 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001413
1414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) );
1415
1416 /* Extract basic information about hash and ciphersuite */
1417
1418 cipher_info = mbedtls_cipher_info_from_type(
XiaokangQian33062842021-11-11 03:37:45 +00001419 handshake->ciphersuite_info->cipher );
Xiaofei Baib7972842021-11-18 07:29:56 +00001420 key_len = cipher_info->key_bitlen / 8;
1421 iv_len = cipher_info->iv_size;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001422
XiaokangQian33062842021-11-11 03:37:45 +00001423 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001424
1425 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
1426 hash_len = PSA_HASH_LENGTH( hash_alg );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001427
1428 /* Compute current handshake transcript. It's the caller's responsiblity
1429 * to call this at the right time, that is, after the ServerFinished. */
1430
1431 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1432 transcript, sizeof( transcript ),
1433 &transcript_len );
1434 if( ret != 0 )
XiaokangQian4cab0242021-10-12 08:43:37 +00001435 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001436
1437 /* Compute application secrets from master secret and transcript hash. */
1438
Gabor Mezei07732f72022-03-26 17:04:19 +01001439 ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001440 handshake->tls13_master_secrets.app,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001441 transcript, transcript_len,
1442 app_secrets );
Jerry Yu4a2fa5d2021-12-10 10:19:34 +08001443 /* Erase master secrets */
Jerry Yu23ab7a42021-12-08 14:34:10 +08001444 mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets,
Jerry Yu27224f52021-12-09 10:56:50 +08001445 sizeof( ssl->handshake->tls13_master_secrets ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001446 if( ret != 0 )
1447 {
1448 MBEDTLS_SSL_DEBUG_RET( 1,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001449 "mbedtls_ssl_tls13_derive_application_secrets", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001450 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001451 }
1452
1453 /* Derive first epoch of IV + Key for application traffic. */
1454
Gabor Mezei07732f72022-03-26 17:04:19 +01001455 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001456 app_secrets->client_application_traffic_secret_N,
1457 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001458 hash_len, key_len, iv_len, traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001459 if( ret != 0 )
1460 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001462 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001463 }
1464
1465 MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret",
1466 app_secrets->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001467 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001468
1469 MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret",
1470 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001471 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001472
XiaokangQianac0385c2021-11-03 06:40:11 +00001473 /*
1474 * Export client/server application traffic secret 0
1475 */
1476 if( ssl->f_export_keys != NULL )
1477 {
1478 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001479 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001480 app_secrets->client_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001481 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001482 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001483 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1484 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001485
1486 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001487 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001488 app_secrets->server_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001489 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001490 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001491 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1492 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001493 }
1494
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001495 MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:",
Xiaofei Baib7972842021-11-18 07:29:56 +00001496 traffic_keys->client_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001497 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key",
Xiaofei Baib7972842021-11-18 07:29:56 +00001498 traffic_keys->server_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001499 MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001500 traffic_keys->client_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001501 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001502 traffic_keys->server_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001503
1504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001505
1506 cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001507 /* randbytes is not used again */
1508 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1509 sizeof( ssl->handshake->randbytes ) );
XiaokangQian33062842021-11-11 03:37:45 +00001510 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001511 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001512}
1513
Jerry Yuf86eb752022-05-06 11:16:55 +08001514int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl )
Jerry Yue110d252022-05-05 10:19:22 +08001515{
1516 int ret;
1517 mbedtls_ssl_key_set traffic_keys;
1518 mbedtls_ssl_transform *transform_handshake = NULL;
1519 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1520
1521 /* Compute handshake secret */
1522 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
1523 if( ret != 0 )
1524 {
1525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
1526 goto cleanup;
1527 }
1528
1529 /* Next evolution in key schedule: Establish handshake secret and
1530 * key material. */
1531 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
1532 if( ret != 0 )
1533 {
1534 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1535 ret );
1536 goto cleanup;
1537 }
1538
1539 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1540 if( transform_handshake == NULL )
1541 {
1542 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1543 goto cleanup;
1544 }
1545
1546 ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
1547 ssl->conf->endpoint,
1548 ssl->session_negotiate->ciphersuite,
1549 &traffic_keys,
1550 ssl );
1551 if( ret != 0 )
1552 {
1553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
1554 goto cleanup;
1555 }
1556 handshake->transform_handshake = transform_handshake;
1557
1558cleanup:
1559 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
1560 if( ret != 0 )
Jerry Yue110d252022-05-05 10:19:22 +08001561 mbedtls_free( transform_handshake );
Jerry Yue110d252022-05-05 10:19:22 +08001562
1563 return( ret );
1564}
1565
Ronald Cron6f135e12021-12-08 16:57:54 +01001566#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */