blob: 2ce654ba85fece78f5d401ef43d9717102042dc9 [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 Mezei62bf0242022-02-07 18:12:07 +0100140psa_status_t mbedtls_psa_hkdf_extract( psa_algorithm_t alg,
141 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 Mezei9f4bb312022-01-31 16:33:47 +0100151
152 if( salt == NULL || salt_len == 0 )
153 {
154 size_t hash_len;
155
156 if( salt_len != 0 )
157 {
Gabor Mezeic5efb8e2022-02-08 13:15:45 +0100158 return( PSA_ERROR_INVALID_ARGUMENT );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100159 }
160
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100161 hash_len = PSA_HASH_LENGTH( alg );
162
163 if( hash_len == 0 )
164 {
Gabor Mezeic5efb8e2022-02-08 13:15:45 +0100165 return( PSA_ERROR_INVALID_ARGUMENT );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100166 }
167
Gabor Mezeid860e0f2022-02-15 16:02:59 +0100168 /* salt_len <= sizeof( salt ) because
169 PSA_HASH_LENGTH( alg ) <= PSA_MAC_MAX_SIZE. */
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100170 salt = null_salt;
171 salt_len = hash_len;
172 }
173
174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
175 psa_set_key_algorithm( &attributes, alg );
176 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
177
Gabor Mezei26c67412022-02-15 15:46:17 +0100178 status = psa_import_key( &attributes, salt, salt_len, &key );
179 if( status != PSA_SUCCESS )
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100180 {
181 goto cleanup;
182 }
183
Gabor Mezei26c67412022-02-15 15:46:17 +0100184 status = psa_mac_compute( key, alg, ikm, ikm_len, prk, prk_size, prk_len );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100185
186cleanup:
Gabor Mezei0e7c6f42022-02-15 15:47:54 +0100187 destroy_status = psa_destroy_key( key );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100188
Gabor Mezei0e7c6f42022-02-15 15:47:54 +0100189 return( ( status == PSA_SUCCESS ) ? destroy_status : status );
Gabor Mezei9f4bb312022-01-31 16:33:47 +0100190}
191
192MBEDTLS_STATIC_TESTABLE
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100193psa_status_t mbedtls_psa_hkdf_expand( psa_algorithm_t alg,
194 const unsigned char *prk, size_t prk_len,
195 const unsigned char *info, size_t info_len,
196 unsigned char *okm, size_t okm_len )
197{
198 size_t hash_len;
199 size_t where = 0;
200 size_t n;
201 size_t t_len = 0;
202 size_t i;
203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
205 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gabor Mezei833713c2022-02-15 16:16:08 +0100206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100207 psa_status_t destroy_status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100208 unsigned char t[PSA_MAC_MAX_SIZE];
209
210 if( okm == NULL )
211 {
212 return( PSA_ERROR_INVALID_ARGUMENT );
213 }
214
215 hash_len = PSA_HASH_LENGTH( alg );
216
217 if( prk_len < hash_len || hash_len == 0 )
218 {
219 return( PSA_ERROR_INVALID_ARGUMENT );
220 }
221
222 if( info == NULL )
223 {
224 info = (const unsigned char *) "";
225 info_len = 0;
226 }
227
228 n = okm_len / hash_len;
229
230 if( okm_len % hash_len != 0 )
231 {
232 n++;
233 }
234
235 /*
236 * Per RFC 5869 Section 2.3, okm_len must not exceed
237 * 255 times the hash length
238 */
239 if( n > 255 )
240 {
241 return( PSA_ERROR_INVALID_ARGUMENT );
242 }
243
244 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
245 psa_set_key_algorithm( &attributes, alg );
246 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
247
Gabor Mezei833713c2022-02-15 16:16:08 +0100248 status = psa_import_key( &attributes, prk, prk_len, &key );
249 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100250 {
251 goto cleanup;
252 }
253
254 memset( t, 0, hash_len );
255
256 /*
257 * Compute T = T(1) | T(2) | T(3) | ... | T(N)
258 * Where T(N) is defined in RFC 5869 Section 2.3
259 */
260 for( i = 1; i <= n; i++ )
261 {
262 size_t num_to_copy;
263 unsigned char c = i & 0xff;
264 size_t len;
265
Gabor Mezei833713c2022-02-15 16:16:08 +0100266 status = psa_mac_sign_setup( &operation, key, alg );
267 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100268 {
269 goto cleanup;
270 }
271
Gabor Mezei833713c2022-02-15 16:16:08 +0100272 status = psa_mac_update( &operation, t, t_len );
273 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100274 {
275 goto cleanup;
276 }
277
Gabor Mezei833713c2022-02-15 16:16:08 +0100278 status = psa_mac_update( &operation, info, info_len );
279 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100280 {
281 goto cleanup;
282 }
283
Gabor Mezei8e360252022-02-17 11:50:02 +0100284 /* The constant concatenated to the end of each T(n) is a single octet. */
Gabor Mezei833713c2022-02-15 16:16:08 +0100285 status = psa_mac_update( &operation, &c, 1 );
286 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100287 {
288 goto cleanup;
289 }
290
Gabor Mezei833713c2022-02-15 16:16:08 +0100291 status = psa_mac_sign_finish( &operation, t, PSA_MAC_MAX_SIZE, &len );
292 if( status != PSA_SUCCESS )
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100293 {
294 goto cleanup;
295 }
296
297 num_to_copy = i != n ? hash_len : okm_len - where;
298 memcpy( okm + where, t, num_to_copy );
299 where += hash_len;
300 t_len = hash_len;
301 }
302
303cleanup:
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100304 if( status != PSA_SUCCESS )
305 psa_mac_abort( &operation );
306 destroy_status = psa_destroy_key( key );
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100307
Gabor Mezei8d5a4cb2022-02-15 16:23:17 +0100308 mbedtls_platform_zeroize( t, sizeof( t ) );
309
310 return( ( status == PSA_SUCCESS ) ? destroy_status : status );
Gabor Mezeia3eecd22022-02-09 16:57:26 +0100311}
312
Xiaofei Bai746f9482021-11-12 08:53:56 +0000313int mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100314 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000315 const unsigned char *secret, size_t secret_len,
316 const unsigned char *label, size_t label_len,
317 const unsigned char *ctx, size_t ctx_len,
318 unsigned char *buf, size_t buf_len )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100319{
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100320 unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ];
321 size_t hkdf_label_len;
322
Xiaofei Baib7972842021-11-18 07:29:56 +0000323 if( label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100324 {
325 /* Should never happen since this is an internal
326 * function, and we know statically which labels
327 * are allowed. */
328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
329 }
330
Xiaofei Baib7972842021-11-18 07:29:56 +0000331 if( ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100332 {
333 /* Should not happen, as above. */
334 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
335 }
336
Xiaofei Baib7972842021-11-18 07:29:56 +0000337 if( buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100338 {
339 /* Should not happen, as above. */
340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
341 }
342
Gabor Mezei07732f72022-03-26 17:04:19 +0100343 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
345
Xiaofei Baib7972842021-11-18 07:29:56 +0000346 ssl_tls13_hkdf_encode_label( buf_len,
347 label, label_len,
348 ctx, ctx_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000349 hkdf_label,
350 &hkdf_label_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100351
Gabor Mezei58db6532022-03-21 12:12:37 +0100352 return( psa_ssl_status_to_mbedtls(
Gabor Mezei07732f72022-03-26 17:04:19 +0100353 mbedtls_psa_hkdf_expand( PSA_ALG_HMAC( hash_alg ),
Gabor Mezei58db6532022-03-21 12:12:37 +0100354 secret, secret_len,
355 hkdf_label, hkdf_label_len,
356 buf, buf_len ) ) );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100357}
358
Hanno Becker3385a4d2020-08-21 13:03:34 +0100359/*
360 * The traffic keying material is generated from the following inputs:
361 *
362 * - One secret value per sender.
363 * - A purpose value indicating the specific value being generated
364 * - The desired lengths of key and IV.
365 *
366 * The expansion itself is based on HKDF:
367 *
368 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
369 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
370 *
371 * [sender] denotes the sending side and the Secret value is provided
372 * by the function caller. Note that we generate server and client side
373 * keys in a single function call.
374 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000375int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100376 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100377 const unsigned char *client_secret,
Xiaofei Baib7972842021-11-18 07:29:56 +0000378 const unsigned char *server_secret, size_t secret_len,
379 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100380 mbedtls_ssl_key_set *keys )
381{
382 int ret = 0;
383
Xiaofei Bai746f9482021-11-12 08:53:56 +0000384 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000385 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100386 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
387 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100388 keys->client_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100389 if( ret != 0 )
390 return( ret );
391
Xiaofei Bai746f9482021-11-12 08:53:56 +0000392 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000393 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100394 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
395 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100396 keys->server_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100397 if( ret != 0 )
398 return( ret );
399
Xiaofei Bai746f9482021-11-12 08:53:56 +0000400 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000401 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100402 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
403 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100404 keys->client_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100405 if( ret != 0 )
406 return( ret );
407
Xiaofei Bai746f9482021-11-12 08:53:56 +0000408 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000409 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100410 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
411 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100412 keys->server_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100413 if( ret != 0 )
414 return( ret );
415
Hanno Becker493ea7f2020-09-08 11:01:00 +0100416 keys->key_len = key_len;
417 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100418
419 return( 0 );
420}
421
Xiaofei Bai746f9482021-11-12 08:53:56 +0000422int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100423 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000424 const unsigned char *secret, size_t secret_len,
425 const unsigned char *label, size_t label_len,
426 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100427 int ctx_hashed,
Xiaofei Baib7972842021-11-18 07:29:56 +0000428 unsigned char *dstbuf, size_t dstbuf_len )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100429{
430 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100431 unsigned char hashed_context[ PSA_HASH_MAX_SIZE ];
432/*
Xiaofei Baib7972842021-11-18 07:29:56 +0000433 const mbedtls_md_info_t *md_info;
434 md_info = mbedtls_md_info_from_type( hash_alg );
435 if( md_info == NULL )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Gabor Mezei07732f72022-03-26 17:04:19 +0100437*/
Hanno Becker0c42fd92020-09-09 12:58:29 +0100438 if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100439 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100440/*
Xiaofei Baib7972842021-11-18 07:29:56 +0000441 ret = mbedtls_md( md_info, ctx, ctx_len, hashed_context );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100442 if( ret != 0 )
443 return( ret );
Xiaofei Baib7972842021-11-18 07:29:56 +0000444 ctx_len = mbedtls_md_get_size( md_info );
Gabor Mezei07732f72022-03-26 17:04:19 +0100445*/
446 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
447
448 status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context,
449 PSA_HASH_LENGTH( hash_alg ), &ctx_len );
450 if( status != PSA_SUCCESS )
451 {
452 ret = psa_ssl_status_to_mbedtls( status );
453 return ret;
454 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100455 }
456 else
457 {
Xiaofei Baib7972842021-11-18 07:29:56 +0000458 if( ctx_len > sizeof(hashed_context) )
Hanno Becker97a21562020-09-09 12:57:16 +0100459 {
460 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100461 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100462 * Let's double-check nonetheless to not run at the risk
463 * of getting a stack overflow. */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100464 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Becker97a21562020-09-09 12:57:16 +0100465 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100466
Xiaofei Baib7972842021-11-18 07:29:56 +0000467 memcpy( hashed_context, ctx, ctx_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100468 }
469
Xiaofei Bai746f9482021-11-12 08:53:56 +0000470 return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000471 secret, secret_len,
472 label, label_len,
473 hashed_context, ctx_len,
474 dstbuf, dstbuf_len ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100475
Hanno Beckerb35d5222020-08-21 13:27:44 +0100476}
477
Xiaofei Bai746f9482021-11-12 08:53:56 +0000478int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100479 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100480 const unsigned char *secret_old,
481 const unsigned char *input, size_t input_len,
482 unsigned char *secret_new )
483{
484 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
485 size_t hlen, ilen;
Gabor Mezei07732f72022-03-26 17:04:19 +0100486 unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 };
Jerry Yu6eaa41c2021-11-22 18:16:45 +0800487 unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 };
Gabor Mezei58db6532022-03-21 12:12:37 +0100488 size_t secret_len;
Gabor Mezei07732f72022-03-26 17:04:19 +0100489
490 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100491 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
492
Gabor Mezei07732f72022-03-26 17:04:19 +0100493 hlen = PSA_HASH_LENGTH( hash_alg );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100494
495 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100496 * on the old secret. */
Hanno Beckere9cccb42020-08-20 13:42:46 +0100497 if( secret_old != NULL )
498 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000499 ret = mbedtls_ssl_tls13_derive_secret(
Hanno Beckere9cccb42020-08-20 13:42:46 +0100500 hash_alg,
501 secret_old, hlen,
502 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
503 NULL, 0, /* context */
504 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Hanno Becker59b50a12020-09-09 10:56:56 +0100505 tmp_secret, hlen );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100506 if( ret != 0 )
507 goto cleanup;
508 }
509
510 if( input != NULL )
511 {
Hanno Becker59b50a12020-09-09 10:56:56 +0100512 memcpy( tmp_input, input, input_len );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100513 ilen = input_len;
514 }
515 else
516 {
517 ilen = hlen;
518 }
519
520 /* HKDF-Extract takes a salt and input key material.
521 * The salt is the old secret, and the input key material
522 * is the input secret (PSK / ECDHE). */
Gabor Mezei58db6532022-03-21 12:12:37 +0100523 ret = psa_ssl_status_to_mbedtls(
Gabor Mezei07732f72022-03-26 17:04:19 +0100524 mbedtls_psa_hkdf_extract( PSA_ALG_HMAC( hash_alg ),
Gabor Mezei58db6532022-03-21 12:12:37 +0100525 tmp_secret, hlen,
526 tmp_input, ilen,
527 secret_new, hlen, &secret_len ) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100528
529 cleanup:
530
Hanno Becker59b50a12020-09-09 10:56:56 +0100531 mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
532 mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100533 return( ret );
534}
535
Xiaofei Bai746f9482021-11-12 08:53:56 +0000536int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100537 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100538 unsigned char const *early_secret,
539 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000540 mbedtls_ssl_tls13_early_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100541{
542 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100543 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100544
545 /* We should never call this function with an unknown hash,
546 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100547 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
549
550 /*
551 * 0
552 * |
553 * v
554 * PSK -> HKDF-Extract = Early Secret
555 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100556 * +-----> Derive-Secret(., "c e traffic", ClientHello)
557 * | = client_early_traffic_secret
558 * |
559 * +-----> Derive-Secret(., "e exp master", ClientHello)
560 * | = early_exporter_master_secret
561 * v
562 */
563
564 /* Create client_early_traffic_secret */
Gabor Mezei07732f72022-03-26 17:04:19 +0100565 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
566 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100567 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ),
568 transcript, transcript_len,
569 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
570 derived->client_early_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100571 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100572 if( ret != 0 )
573 return( ret );
574
575 /* Create early exporter */
Gabor Mezei07732f72022-03-26 17:04:19 +0100576 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
577 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100578 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ),
579 transcript, transcript_len,
580 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
581 derived->early_exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100582 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100583 if( ret != 0 )
584 return( ret );
585
586 return( 0 );
587}
588
Xiaofei Bai746f9482021-11-12 08:53:56 +0000589int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100590 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100591 unsigned char const *handshake_secret,
592 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000593 mbedtls_ssl_tls13_handshake_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100594{
595 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100596 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100597
598 /* We should never call this function with an unknown hash,
599 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100600 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
602
603 /*
604 *
605 * Handshake Secret
606 * |
607 * +-----> Derive-Secret( ., "c hs traffic",
608 * | ClientHello...ServerHello )
609 * | = client_handshake_traffic_secret
610 * |
611 * +-----> Derive-Secret( ., "s hs traffic",
612 * | ClientHello...ServerHello )
613 * | = server_handshake_traffic_secret
614 *
615 */
616
617 /*
618 * Compute client_handshake_traffic_secret with
619 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
620 */
621
Gabor Mezei07732f72022-03-26 17:04:19 +0100622 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
623 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100624 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ),
625 transcript, transcript_len,
626 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
627 derived->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100628 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100629 if( ret != 0 )
630 return( ret );
631
632 /*
633 * Compute server_handshake_traffic_secret with
634 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
635 */
636
Gabor Mezei07732f72022-03-26 17:04:19 +0100637 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
638 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100639 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ),
640 transcript, transcript_len,
641 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
642 derived->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100643 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100644 if( ret != 0 )
645 return( ret );
646
647 return( 0 );
648}
649
Xiaofei Bai746f9482021-11-12 08:53:56 +0000650int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100651 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100652 unsigned char const *application_secret,
653 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000654 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100655{
656 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100657 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100658
659 /* We should never call this function with an unknown hash,
660 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100661 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
663
664 /* Generate {client,server}_application_traffic_secret_0
665 *
666 * Master Secret
667 * |
668 * +-----> Derive-Secret( ., "c ap traffic",
669 * | ClientHello...server Finished )
670 * | = client_application_traffic_secret_0
671 * |
672 * +-----> Derive-Secret( ., "s ap traffic",
673 * | ClientHello...Server Finished )
674 * | = server_application_traffic_secret_0
675 * |
676 * +-----> Derive-Secret( ., "exp master",
677 * | ClientHello...server Finished)
678 * | = exporter_master_secret
679 *
680 */
681
Gabor Mezei07732f72022-03-26 17:04:19 +0100682 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
683 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100684 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ),
685 transcript, transcript_len,
686 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
687 derived->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100688 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100689 if( ret != 0 )
690 return( ret );
691
Gabor Mezei07732f72022-03-26 17:04:19 +0100692 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
693 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100694 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ),
695 transcript, transcript_len,
696 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
697 derived->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100698 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100699 if( ret != 0 )
700 return( ret );
701
Gabor Mezei07732f72022-03-26 17:04:19 +0100702 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
703 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100704 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ),
705 transcript, transcript_len,
706 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
707 derived->exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100708 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100709 if( ret != 0 )
710 return( ret );
711
712 return( 0 );
713}
714
715/* Generate resumption_master_secret for use with the ticket exchange.
716 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000717 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100718 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000719int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100720 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100721 unsigned char const *application_secret,
722 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000723 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100724{
725 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100726 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100727
728 /* We should never call this function with an unknown hash,
729 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100730 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100731 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
732
Gabor Mezei07732f72022-03-26 17:04:19 +0100733 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
734 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100735 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ),
736 transcript, transcript_len,
737 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
738 derived->resumption_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100739 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100740
741 if( ret != 0 )
742 return( ret );
743
744 return( 0 );
745}
746
XiaokangQiana4c99f22021-11-11 06:46:35 +0000747int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000748{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000750 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +0100751 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
752 handshake->ciphersuite_info->mac );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000753
754 /*
755 * Compute MasterSecret
756 */
Gabor Mezei07732f72022-03-26 17:04:19 +0100757 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000758 handshake->tls13_master_secrets.handshake,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000759 NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000760 handshake->tls13_master_secrets.app );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000761 if( ret != 0 )
762 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000763 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000764 return( ret );
765 }
766
767 MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret",
Gabor Mezei07732f72022-03-26 17:04:19 +0100768 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000769
770 return( 0 );
771}
772
Gabor Mezei07732f72022-03-26 17:04:19 +0100773static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000774 unsigned char const *base_key,
775 unsigned char const *transcript,
Gabor Mezei07732f72022-03-26 17:04:19 +0100776 unsigned char *dst,
777 size_t *dst_len )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100778{
Gabor Mezei07732f72022-03-26 17:04:19 +0100779 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
780 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
781 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
782 size_t hash_len = PSA_HASH_LENGTH( hash_alg );
783 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100784 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100785 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100786
787 /* We should never call this function with an unknown hash,
788 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100789 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100790 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
791
792 /* TLS 1.3 Finished message
793 *
794 * struct {
795 * opaque verify_data[Hash.length];
796 * } Finished;
797 *
798 * verify_data =
799 * HMAC( finished_key,
800 * Hash( Handshake Context +
801 * Certificate* +
802 * CertificateVerify* )
803 * )
804 *
805 * finished_key =
806 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
807 */
808
Xiaofei Bai746f9482021-11-12 08:53:56 +0000809 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100810 hash_alg, base_key, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100811 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ),
812 NULL, 0,
Gabor Mezei07732f72022-03-26 17:04:19 +0100813 finished_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100814 if( ret != 0 )
815 goto exit;
816
Gabor Mezei07732f72022-03-26 17:04:19 +0100817 alg = PSA_ALG_HMAC( hash_alg );
818 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
819 psa_set_key_algorithm( &attributes, alg );
820 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
821
822 status = psa_import_key( &attributes, finished_key, hash_len, &key );
823 if( status != PSA_SUCCESS )
824 {
825 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100826 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100827 }
828
829 status = psa_mac_compute( key, alg, transcript, hash_len,
830 dst, hash_len, dst_len );
831 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100832
833exit:
834
Gabor Mezei07732f72022-03-26 17:04:19 +0100835 status = psa_destroy_key( key );
836 if( ret == 0 )
837 ret = psa_ssl_status_to_mbedtls( status );
838
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100839 mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100840
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100841 return( ret );
842}
843
XiaokangQianc5c39d52021-11-09 11:55:10 +0000844int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000845 unsigned char* dst,
846 size_t dst_len,
847 size_t *actual_len,
848 int from )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000849{
XiaokangQiana7634982021-10-22 06:32:32 +0000850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000851
XiaokangQianaaa0e192021-11-10 03:07:04 +0000852 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000853 size_t transcript_len;
854
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800855 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800856 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800857 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
858 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800859
860 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100861
862 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(
863 ssl->handshake->ciphersuite_info->mac );
864 size_t const hash_lem = PSA_HASH_LENGTH( hash_alg );
Jerry Yua5563f62021-12-10 18:14:36 +0800865
866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) );
867
Jerry Yub737f6a2021-12-10 17:55:23 +0800868 if( from == MBEDTLS_SSL_IS_CLIENT )
869 {
870 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
871 base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret );
872 }
873 else
874 {
875 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
876 base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret );
877 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000878
Gabor Mezei07732f72022-03-26 17:04:19 +0100879 if( dst_len < hash_lem )
Jerry Yu9c074732021-12-10 17:12:43 +0800880 {
881 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
882 goto exit;
883 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000884
885 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
886 transcript, sizeof( transcript ),
887 &transcript_len );
888 if( ret != 0 )
889 {
890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000891 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000892 }
893 MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len );
894
Gabor Mezei07732f72022-03-26 17:04:19 +0100895 ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000896 if( ret != 0 )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000897 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000898
Gabor Mezei07732f72022-03-26 17:04:19 +0100899 MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_lem );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000901
902exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800903 /* Erase handshake secrets */
Jerry Yub737f6a2021-12-10 17:55:23 +0800904 mbedtls_platform_zeroize( base_key, base_key_len );
XiaokangQian33062842021-11-11 03:37:45 +0000905 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000906 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000907}
908
Xiaofei Bai746f9482021-11-12 08:53:56 +0000909int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100910 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100911 unsigned char const *psk, size_t psk_len,
912 int psk_type,
913 unsigned char const *transcript,
914 unsigned char *result )
915{
916 int ret = 0;
917 unsigned char binder_key[MBEDTLS_MD_MAX_SIZE];
918 unsigned char early_secret[MBEDTLS_MD_MAX_SIZE];
Gabor Mezei07732f72022-03-26 17:04:19 +0100919 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
920 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100921
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100922#if !defined(MBEDTLS_DEBUG_C)
923 ssl = NULL; /* make sure we don't use it except for debug */
924 ((void) ssl);
925#endif
926
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100927 /* We should never call this function with an unknown hash,
928 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100929 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
931
932 /*
933 * 0
934 * |
935 * v
936 * PSK -> HKDF-Extract = Early Secret
937 * |
938 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
939 * | = binder_key
940 * v
941 */
942
Gabor Mezei07732f72022-03-26 17:04:19 +0100943 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000944 NULL, /* Old secret */
945 psk, psk_len, /* Input */
946 early_secret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100947 if( ret != 0 )
948 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100950 goto exit;
951 }
952
953 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
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( res_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 'res binder'" ) );
961 }
962 else
963 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100964 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
965 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100966 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ),
967 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100968 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100969 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) );
970 }
971
972 if( ret != 0 )
973 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100975 goto exit;
976 }
977
978 /*
979 * The binding_value is computed in the same way as the Finished message
980 * but with the BaseKey being the binder_key.
981 */
982
Gabor Mezei07732f72022-03-26 17:04:19 +0100983 ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript,
984 result, &actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100985 if( ret != 0 )
986 goto exit;
987
Gabor Mezei07732f72022-03-26 17:04:19 +0100988 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100989
990exit:
991
992 mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) );
993 mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) );
994 return( ret );
995}
996
Hanno Beckerc94060c2021-03-22 07:50:44 +0000997int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
998 int endpoint,
999 int ciphersuite,
1000 mbedtls_ssl_key_set const *traffic_keys,
1001 mbedtls_ssl_context *ssl /* DEBUG ONLY */ )
1002{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001003#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001004 int ret;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001005#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001006 mbedtls_cipher_info_t const *cipher_info;
1007 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1008 unsigned char const *key_enc;
1009 unsigned char const *iv_enc;
1010 unsigned char const *key_dec;
1011 unsigned char const *iv_dec;
1012
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001013#if defined(MBEDTLS_USE_PSA_CRYPTO)
1014 psa_key_type_t key_type;
1015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1016 psa_algorithm_t alg;
1017 size_t key_bits;
1018 psa_status_t status = PSA_SUCCESS;
1019#endif
1020
Hanno Beckerc94060c2021-03-22 07:50:44 +00001021#if !defined(MBEDTLS_DEBUG_C)
1022 ssl = NULL; /* make sure we don't use it except for those cases */
1023 (void) ssl;
1024#endif
1025
1026 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker7887a772021-04-20 05:27:57 +01001027 if( ciphersuite_info == NULL )
1028 {
1029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
1030 ciphersuite ) );
1031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1032 }
Hanno Beckerc94060c2021-03-22 07:50:44 +00001033
1034 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
1035 if( cipher_info == NULL )
1036 {
Hanno Becker7887a772021-04-20 05:27:57 +01001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
1038 ciphersuite_info->cipher ) );
Hanno Beckerf62a7302021-04-21 05:21:28 +01001039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerc94060c2021-03-22 07:50:44 +00001040 }
1041
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001042#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001043 /*
1044 * Setup cipher contexts in target transform
1045 */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001046 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
1047 cipher_info ) ) != 0 )
1048 {
1049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1050 return( ret );
1051 }
1052
1053 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
1054 cipher_info ) ) != 0 )
1055 {
1056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
1057 return( ret );
1058 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001059#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001060
1061#if defined(MBEDTLS_SSL_SRV_C)
1062 if( endpoint == MBEDTLS_SSL_IS_SERVER )
1063 {
1064 key_enc = traffic_keys->server_write_key;
1065 key_dec = traffic_keys->client_write_key;
1066 iv_enc = traffic_keys->server_write_iv;
1067 iv_dec = traffic_keys->client_write_iv;
1068 }
1069 else
1070#endif /* MBEDTLS_SSL_SRV_C */
1071#if defined(MBEDTLS_SSL_CLI_C)
1072 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
1073 {
1074 key_enc = traffic_keys->client_write_key;
1075 key_dec = traffic_keys->server_write_key;
1076 iv_enc = traffic_keys->client_write_iv;
1077 iv_dec = traffic_keys->server_write_iv;
1078 }
1079 else
1080#endif /* MBEDTLS_SSL_CLI_C */
1081 {
1082 /* should not happen */
1083 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1084 }
1085
1086 memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len );
1087 memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len );
1088
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001089#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +00001090 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc,
1091 key_enc, cipher_info->key_bitlen,
1092 MBEDTLS_ENCRYPT ) ) != 0 )
1093 {
1094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1095 return( ret );
1096 }
1097
1098 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec,
1099 key_dec, cipher_info->key_bitlen,
1100 MBEDTLS_DECRYPT ) ) != 0 )
1101 {
1102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
1103 return( ret );
1104 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001105#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +00001106
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001107 /*
1108 * Setup other fields in SSL transform
1109 */
1110
1111 if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 )
1112 transform->taglen = 8;
1113 else
1114 transform->taglen = 16;
1115
1116 transform->ivlen = traffic_keys->iv_len;
1117 transform->maclen = 0;
1118 transform->fixed_ivlen = transform->ivlen;
1119 transform->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4;
1120
1121 /* We add the true record content type (1 Byte) to the plaintext and
1122 * then pad to the configured granularity. The mimimum length of the
1123 * type-extended and padded plaintext is therefore the padding
1124 * granularity. */
1125 transform->minlen =
1126 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1127
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001129 /*
1130 * Setup psa keys and alg
1131 */
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001132 if( ( status = mbedtls_ssl_cipher_to_psa( cipher_info->type,
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001133 transform->taglen,
1134 &alg,
1135 &key_type,
1136 &key_bits ) ) != PSA_SUCCESS )
1137 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) );
1139 return( psa_ssl_status_to_mbedtls( status ) );
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001140 }
1141
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001142 transform->psa_alg = alg;
1143
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001144 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001145 {
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1147 psa_set_key_algorithm( &attributes, alg );
1148 psa_set_key_type( &attributes, key_type );
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001149
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001150 if( ( status = psa_import_key( &attributes,
1151 key_enc,
1152 PSA_BITS_TO_BYTES( key_bits ),
1153 &transform->psa_key_enc ) ) != PSA_SUCCESS )
1154 {
1155 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1156 return( psa_ssl_status_to_mbedtls( status ) );
1157 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001158
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001159 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1160
1161 if( ( status = psa_import_key( &attributes,
1162 key_dec,
1163 PSA_BITS_TO_BYTES( key_bits ),
1164 &transform->psa_key_dec ) ) != PSA_SUCCESS )
1165 {
1166 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1167 return( psa_ssl_status_to_mbedtls( status ) );
1168 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001169 }
1170#endif /* MBEDTLS_USE_PSA_CRYPTO */
1171
Hanno Beckerc94060c2021-03-22 07:50:44 +00001172 return( 0 );
1173}
1174
Xiaofei Bai746f9482021-11-12 08:53:56 +00001175int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl )
Jerry Yu89ea3212021-09-09 14:31:24 +08001176{
Jerry Yue3131ef2021-09-16 13:14:15 +08001177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001178 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001179 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001180
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001181 if( handshake->ciphersuite_info == NULL )
Jerry Yu89ea3212021-09-09 14:31:24 +08001182 {
1183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) );
1184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1185 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001186
Gabor Mezei07732f72022-03-26 17:04:19 +01001187 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
Jerry Yue06f4532021-09-23 18:35:07 +08001188
Gabor Mezei07732f72022-03-26 17:04:19 +01001189 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001190 handshake->tls13_master_secrets.early );
Jerry Yu89ea3212021-09-09 14:31:24 +08001191 if( ret != 0 )
1192 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yu89ea3212021-09-09 14:31:24 +08001194 return( ret );
1195 }
1196
1197 return( 0 );
1198}
1199
Jerry Yuc068b662021-10-11 22:30:19 +08001200/* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
Jerry Yu61e35e02021-09-16 18:59:08 +08001201 * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
Jerry Yuc068b662021-10-11 22:30:19 +08001202int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
1203 mbedtls_ssl_key_set *traffic_keys )
Jerry Yu61e35e02021-09-16 18:59:08 +08001204{
1205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1206
1207 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001208
1209 psa_algorithm_t hash_alg;
1210 size_t hash_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001211
Jerry Yu435208a2021-10-13 11:22:16 +08001212 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001213 size_t transcript_len;
1214
1215 mbedtls_cipher_info_t const *cipher_info;
Xiaofei Baib7972842021-11-18 07:29:56 +00001216 size_t key_len, iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001217
Jerry Yu435208a2021-10-13 11:22:16 +08001218 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1219 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001220 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001221
Jerry Yuc068b662021-10-11 22:30:19 +08001222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001223
Jerry Yu435208a2021-10-13 11:22:16 +08001224 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Xiaofei Baib7972842021-11-18 07:29:56 +00001225 key_len = cipher_info->key_bitlen >> 3;
1226 iv_len = cipher_info->iv_size;
Jerry Yu61e35e02021-09-16 18:59:08 +08001227
Jerry Yu435208a2021-10-13 11:22:16 +08001228 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001229
1230 hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
1231 hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yu61e35e02021-09-16 18:59:08 +08001232
1233 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1234 transcript,
1235 sizeof( transcript ),
1236 &transcript_len );
1237 if( ret != 0 )
1238 {
1239 MBEDTLS_SSL_DEBUG_RET( 1,
1240 "mbedtls_ssl_get_handshake_transcript",
1241 ret );
1242 return( ret );
1243 }
1244
Gabor Mezei07732f72022-03-26 17:04:19 +01001245 ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001246 handshake->tls13_master_secrets.handshake,
Jerry Yu435208a2021-10-13 11:22:16 +08001247 transcript, transcript_len, tls13_hs_secrets );
Jerry Yu61e35e02021-09-16 18:59:08 +08001248 if( ret != 0 )
1249 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets",
Jerry Yu61e35e02021-09-16 18:59:08 +08001251 ret );
1252 return( ret );
1253 }
1254
1255 MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001256 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001257 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001258 MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001259 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001260 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001261
1262 /*
1263 * Export client handshake traffic secret
1264 */
Jerry Yu61e35e02021-09-16 18:59:08 +08001265 if( ssl->f_export_keys != NULL )
1266 {
1267 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001268 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001269 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001270 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001271 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001272 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001273 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1274
1275 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001276 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001277 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001278 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001279 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001280 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001281 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1282 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001283
Gabor Mezei07732f72022-03-26 17:04:19 +01001284 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
Jerry Yu435208a2021-10-13 11:22:16 +08001285 tls13_hs_secrets->client_handshake_traffic_secret,
1286 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001287 hash_len, key_len, iv_len, traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +08001288 if( ret != 0 )
1289 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
Jerry Yu61e35e02021-09-16 18:59:08 +08001291 goto exit;
1292 }
1293
1294 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key",
1295 traffic_keys->client_write_key,
1296 traffic_keys->key_len);
1297
1298 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key",
1299 traffic_keys->server_write_key,
1300 traffic_keys->key_len);
1301
1302 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv",
1303 traffic_keys->client_write_iv,
1304 traffic_keys->iv_len);
1305
1306 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv",
1307 traffic_keys->server_write_iv,
1308 traffic_keys->iv_len);
1309
Jerry Yuc068b662021-10-11 22:30:19 +08001310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001311
1312exit:
1313
1314 return( ret );
1315}
1316
Jerry Yuf0ac2352021-10-11 17:47:07 +08001317int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl )
Jerry Yua0650eb2021-09-09 17:14:45 +08001318{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001320#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C)
1321 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1322#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001323 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +01001324 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
1325 handshake->ciphersuite_info->mac );
Jerry Yua0650eb2021-09-09 17:14:45 +08001326
Jerry Yuf0ac2352021-10-11 17:47:07 +08001327#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
1328 /*
1329 * Compute ECDHE secret used to compute the handshake secret from which
1330 * client_handshake_traffic_secret and server_handshake_traffic_secret
1331 * are derived in the handshake secret derivation stage.
1332 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001333 if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) )
Jerry Yuf0ac2352021-10-11 17:47:07 +08001334 {
1335 if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) )
1336 {
1337#if defined(MBEDTLS_ECDH_C)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001338 /* Compute ECDH shared secret. */
1339 status = psa_raw_key_agreement(
1340 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1341 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1342 handshake->premaster, sizeof( handshake->premaster ),
1343 &handshake->pmslen );
1344 if( status != PSA_SUCCESS )
1345 {
1346 ret = psa_ssl_status_to_mbedtls( status );
1347 MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret );
1348 return( ret );
1349 }
1350
1351 status = psa_destroy_key( handshake->ecdh_psa_privkey );
1352 if( status != PSA_SUCCESS )
1353 {
1354 ret = psa_ssl_status_to_mbedtls( status );
1355 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
1356 return( ret );
1357 }
1358
1359 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001360#endif /* MBEDTLS_ECDH_C */
1361 }
1362 else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) )
1363 {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) );
1365 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1366 }
1367 }
1368#else
1369 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1370#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001371
1372 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001373 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001374 */
Gabor Mezei07732f72022-03-26 17:04:19 +01001375 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001376 handshake->tls13_master_secrets.early,
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001377 handshake->premaster, handshake->pmslen,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001378 handshake->tls13_master_secrets.handshake );
Jerry Yua0650eb2021-09-09 17:14:45 +08001379 if( ret != 0 )
1380 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yua0650eb2021-09-09 17:14:45 +08001382 return( ret );
1383 }
1384
1385 MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret",
Gabor Mezei07732f72022-03-26 17:04:19 +01001386 handshake->tls13_master_secrets.handshake,
1387 PSA_HASH_LENGTH( hash_alg ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001388
1389#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001390 mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001391#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
1392 return( 0 );
1393}
1394
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001395/* Generate application traffic keys since any records following a 1-RTT Finished message
1396 * MUST be encrypted under the application traffic key.
1397 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001398int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001399 mbedtls_ssl_context *ssl,
1400 mbedtls_ssl_key_set *traffic_keys )
1401{
XiaokangQiana7634982021-10-22 06:32:32 +00001402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001403 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001404
1405 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001406 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001407 &ssl->session_negotiate->app_secrets;
1408
1409 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001410 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001411 size_t transcript_len;
1412
1413 /* Variables relating to the hash for the chosen ciphersuite. */
1414 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001415
1416 psa_algorithm_t hash_alg;
1417 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001418
1419 /* Variables relating to the cipher for the chosen ciphersuite. */
1420 mbedtls_cipher_info_t const *cipher_info;
Xiaofei Baib7972842021-11-18 07:29:56 +00001421 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001422
1423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) );
1424
1425 /* Extract basic information about hash and ciphersuite */
1426
1427 cipher_info = mbedtls_cipher_info_from_type(
XiaokangQian33062842021-11-11 03:37:45 +00001428 handshake->ciphersuite_info->cipher );
Xiaofei Baib7972842021-11-18 07:29:56 +00001429 key_len = cipher_info->key_bitlen / 8;
1430 iv_len = cipher_info->iv_size;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001431
XiaokangQian33062842021-11-11 03:37:45 +00001432 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001433
1434 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
1435 hash_len = PSA_HASH_LENGTH( hash_alg );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001436
1437 /* Compute current handshake transcript. It's the caller's responsiblity
1438 * to call this at the right time, that is, after the ServerFinished. */
1439
1440 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1441 transcript, sizeof( transcript ),
1442 &transcript_len );
1443 if( ret != 0 )
XiaokangQian4cab0242021-10-12 08:43:37 +00001444 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001445
1446 /* Compute application secrets from master secret and transcript hash. */
1447
Gabor Mezei07732f72022-03-26 17:04:19 +01001448 ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001449 handshake->tls13_master_secrets.app,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001450 transcript, transcript_len,
1451 app_secrets );
Jerry Yu4a2fa5d2021-12-10 10:19:34 +08001452 /* Erase master secrets */
Jerry Yu23ab7a42021-12-08 14:34:10 +08001453 mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets,
Jerry Yu27224f52021-12-09 10:56:50 +08001454 sizeof( ssl->handshake->tls13_master_secrets ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001455 if( ret != 0 )
1456 {
1457 MBEDTLS_SSL_DEBUG_RET( 1,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001458 "mbedtls_ssl_tls13_derive_application_secrets", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001459 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001460 }
1461
1462 /* Derive first epoch of IV + Key for application traffic. */
1463
Gabor Mezei07732f72022-03-26 17:04:19 +01001464 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001465 app_secrets->client_application_traffic_secret_N,
1466 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001467 hash_len, key_len, iv_len, traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001468 if( ret != 0 )
1469 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001471 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001472 }
1473
1474 MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret",
1475 app_secrets->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001476 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001477
1478 MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret",
1479 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001480 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001481
XiaokangQianac0385c2021-11-03 06:40:11 +00001482 /*
1483 * Export client/server application traffic secret 0
1484 */
1485 if( ssl->f_export_keys != NULL )
1486 {
1487 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001488 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001489 app_secrets->client_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001490 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001491 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001492 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1493 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001494
1495 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001496 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001497 app_secrets->server_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001498 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001499 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001500 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1501 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001502 }
1503
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001504 MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:",
Xiaofei Baib7972842021-11-18 07:29:56 +00001505 traffic_keys->client_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001506 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key",
Xiaofei Baib7972842021-11-18 07:29:56 +00001507 traffic_keys->server_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001508 MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001509 traffic_keys->client_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001510 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001511 traffic_keys->server_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001512
1513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001514
1515 cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001516 /* randbytes is not used again */
1517 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1518 sizeof( ssl->handshake->randbytes ) );
XiaokangQian33062842021-11-11 03:37:45 +00001519 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001520 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001521}
1522
Ronald Cron6f135e12021-12-08 16:57:54 +01001523#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */