blob: e503f9870603c4646f605b077db37c43e989986c [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
Xiaofei Bai746f9482021-11-12 08:53:56 +0000140int mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100141 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000142 const unsigned char *secret, size_t secret_len,
143 const unsigned char *label, size_t label_len,
144 const unsigned char *ctx, size_t ctx_len,
145 unsigned char *buf, size_t buf_len )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100146{
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100147 unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ];
148 size_t hkdf_label_len;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200149 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
150 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200151 psa_key_derivation_operation_t operation =
152 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100153
Xiaofei Baib7972842021-11-18 07:29:56 +0000154 if( label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100155 {
156 /* Should never happen since this is an internal
157 * function, and we know statically which labels
158 * are allowed. */
159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
160 }
161
Xiaofei Baib7972842021-11-18 07:29:56 +0000162 if( ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100163 {
164 /* Should not happen, as above. */
165 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
166 }
167
Xiaofei Baib7972842021-11-18 07:29:56 +0000168 if( buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN )
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100169 {
170 /* Should not happen, as above. */
171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
172 }
173
Gabor Mezei07732f72022-03-26 17:04:19 +0100174 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
176
Xiaofei Baib7972842021-11-18 07:29:56 +0000177 ssl_tls13_hkdf_encode_label( buf_len,
178 label, label_len,
179 ctx, ctx_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000180 hkdf_label,
181 &hkdf_label_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100182
Przemek Stekield5ae3652022-05-13 12:10:08 +0200183 status = psa_key_derivation_setup( &operation, PSA_ALG_HKDF_EXPAND( hash_alg ) );
Przemek Stekield5ae3652022-05-13 12:10:08 +0200184
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200185 if( status != PSA_SUCCESS )
186 goto cleanup;
187
188 status = psa_key_derivation_input_bytes( &operation,
189 PSA_KEY_DERIVATION_INPUT_SECRET,
190 secret,
191 secret_len );
192
193 if( status != PSA_SUCCESS )
194 goto cleanup;
195
196 status = psa_key_derivation_input_bytes( &operation,
197 PSA_KEY_DERIVATION_INPUT_INFO,
198 hkdf_label,
199 hkdf_label_len );
200
201 if( status != PSA_SUCCESS )
202 goto cleanup;
203
204 status = psa_key_derivation_output_bytes( &operation,
205 buf,
206 buf_len );
207
208 if( status != PSA_SUCCESS )
209 goto cleanup;
210
211cleanup:
212 abort_status = psa_key_derivation_abort( &operation );
213 status = ( status == PSA_SUCCESS ? abort_status : status );
Przemek Stekield5ae3652022-05-13 12:10:08 +0200214 return( psa_ssl_status_to_mbedtls ( status ) );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100215}
216
Hanno Becker3385a4d2020-08-21 13:03:34 +0100217/*
218 * The traffic keying material is generated from the following inputs:
219 *
220 * - One secret value per sender.
221 * - A purpose value indicating the specific value being generated
222 * - The desired lengths of key and IV.
223 *
224 * The expansion itself is based on HKDF:
225 *
226 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
227 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
228 *
229 * [sender] denotes the sending side and the Secret value is provided
230 * by the function caller. Note that we generate server and client side
231 * keys in a single function call.
232 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000233int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100234 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100235 const unsigned char *client_secret,
Xiaofei Baib7972842021-11-18 07:29:56 +0000236 const unsigned char *server_secret, size_t secret_len,
237 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100238 mbedtls_ssl_key_set *keys )
239{
240 int ret = 0;
241
Xiaofei Bai746f9482021-11-12 08:53:56 +0000242 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000243 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100244 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
245 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100246 keys->client_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100247 if( ret != 0 )
248 return( ret );
249
Xiaofei Bai746f9482021-11-12 08:53:56 +0000250 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000251 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100252 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
253 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100254 keys->server_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100255 if( ret != 0 )
256 return( ret );
257
Xiaofei Bai746f9482021-11-12 08:53:56 +0000258 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000259 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100260 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
261 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100262 keys->client_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100263 if( ret != 0 )
264 return( ret );
265
Xiaofei Bai746f9482021-11-12 08:53:56 +0000266 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000267 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100268 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
269 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100270 keys->server_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100271 if( ret != 0 )
272 return( ret );
273
Hanno Becker493ea7f2020-09-08 11:01:00 +0100274 keys->key_len = key_len;
275 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100276
277 return( 0 );
278}
279
Xiaofei Bai746f9482021-11-12 08:53:56 +0000280int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100281 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000282 const unsigned char *secret, size_t secret_len,
283 const unsigned char *label, size_t label_len,
284 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100285 int ctx_hashed,
Xiaofei Baib7972842021-11-18 07:29:56 +0000286 unsigned char *dstbuf, size_t dstbuf_len )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100287{
288 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100289 unsigned char hashed_context[ PSA_HASH_MAX_SIZE ];
Hanno Becker0c42fd92020-09-09 12:58:29 +0100290 if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100291 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
293
294 status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context,
295 PSA_HASH_LENGTH( hash_alg ), &ctx_len );
296 if( status != PSA_SUCCESS )
297 {
298 ret = psa_ssl_status_to_mbedtls( status );
299 return ret;
300 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100301 }
302 else
303 {
Xiaofei Baib7972842021-11-18 07:29:56 +0000304 if( ctx_len > sizeof(hashed_context) )
Hanno Becker97a21562020-09-09 12:57:16 +0100305 {
306 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100307 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100308 * Let's double-check nonetheless to not run at the risk
309 * of getting a stack overflow. */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Becker97a21562020-09-09 12:57:16 +0100311 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100312
Xiaofei Baib7972842021-11-18 07:29:56 +0000313 memcpy( hashed_context, ctx, ctx_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100314 }
315
Xiaofei Bai746f9482021-11-12 08:53:56 +0000316 return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000317 secret, secret_len,
318 label, label_len,
319 hashed_context, ctx_len,
320 dstbuf, dstbuf_len ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100321
Hanno Beckerb35d5222020-08-21 13:27:44 +0100322}
323
Xiaofei Bai746f9482021-11-12 08:53:56 +0000324int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100325 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100326 const unsigned char *secret_old,
327 const unsigned char *input, size_t input_len,
328 unsigned char *secret_new )
329{
330 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
332 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100333 size_t hlen, ilen;
Gabor Mezei07732f72022-03-26 17:04:19 +0100334 unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 };
Jerry Yu6eaa41c2021-11-22 18:16:45 +0800335 unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 };
Przemek Stekield5ae3652022-05-13 12:10:08 +0200336 psa_key_derivation_operation_t operation =
337 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100338
339 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100340 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
341
Gabor Mezei07732f72022-03-26 17:04:19 +0100342 hlen = PSA_HASH_LENGTH( hash_alg );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100343
344 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100345 * on the old secret. */
Hanno Beckere9cccb42020-08-20 13:42:46 +0100346 if( secret_old != NULL )
347 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000348 ret = mbedtls_ssl_tls13_derive_secret(
Hanno Beckere9cccb42020-08-20 13:42:46 +0100349 hash_alg,
350 secret_old, hlen,
351 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
352 NULL, 0, /* context */
353 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Hanno Becker59b50a12020-09-09 10:56:56 +0100354 tmp_secret, hlen );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100355 if( ret != 0 )
356 goto cleanup;
357 }
358
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200359 ret = 0;
360
Hanno Beckere9cccb42020-08-20 13:42:46 +0100361 if( input != NULL )
362 {
Hanno Becker59b50a12020-09-09 10:56:56 +0100363 memcpy( tmp_input, input, input_len );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100364 ilen = input_len;
365 }
366 else
367 {
368 ilen = hlen;
369 }
370
Przemek Stekield5ae3652022-05-13 12:10:08 +0200371 status = psa_key_derivation_setup( &operation,
372 PSA_ALG_HKDF_EXTRACT( hash_alg ) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100373
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200374 if( status != PSA_SUCCESS )
375 goto cleanup;
376
377 status = psa_key_derivation_input_bytes( &operation,
378 PSA_KEY_DERIVATION_INPUT_SALT,
379 tmp_secret,
380 hlen );
381
382 if( status != PSA_SUCCESS )
383 goto cleanup;
384
385 status = psa_key_derivation_input_bytes( &operation,
386 PSA_KEY_DERIVATION_INPUT_SECRET,
387 tmp_input,
388 ilen );
389
390 if( status != PSA_SUCCESS )
391 goto cleanup;
392
393 status = psa_key_derivation_output_bytes( &operation,
394 secret_new,
395 PSA_HASH_LENGTH( hash_alg ) );
396
397 if( status != PSA_SUCCESS )
398 goto cleanup;
399
Hanno Beckere9cccb42020-08-20 13:42:46 +0100400 cleanup:
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200401 abort_status = psa_key_derivation_abort( &operation );
402 status = ( status == PSA_SUCCESS ? abort_status : status );
403 ret = ( ret == 0 ? psa_ssl_status_to_mbedtls ( status ) : ret );
Hanno Becker59b50a12020-09-09 10:56:56 +0100404 mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
405 mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100406 return( ret );
407}
408
Xiaofei Bai746f9482021-11-12 08:53:56 +0000409int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100410 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100411 unsigned char const *early_secret,
412 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000413 mbedtls_ssl_tls13_early_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100414{
415 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100416 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100417
418 /* We should never call this function with an unknown hash,
419 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100420 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100421 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
422
423 /*
424 * 0
425 * |
426 * v
427 * PSK -> HKDF-Extract = Early Secret
428 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100429 * +-----> Derive-Secret(., "c e traffic", ClientHello)
430 * | = client_early_traffic_secret
431 * |
432 * +-----> Derive-Secret(., "e exp master", ClientHello)
433 * | = early_exporter_master_secret
434 * v
435 */
436
437 /* Create client_early_traffic_secret */
Gabor Mezei07732f72022-03-26 17:04:19 +0100438 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
439 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100440 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ),
441 transcript, transcript_len,
442 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
443 derived->client_early_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100444 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100445 if( ret != 0 )
446 return( ret );
447
448 /* Create early exporter */
Gabor Mezei07732f72022-03-26 17:04:19 +0100449 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
450 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100451 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ),
452 transcript, transcript_len,
453 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
454 derived->early_exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100455 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100456 if( ret != 0 )
457 return( ret );
458
459 return( 0 );
460}
461
Xiaofei Bai746f9482021-11-12 08:53:56 +0000462int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100463 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100464 unsigned char const *handshake_secret,
465 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000466 mbedtls_ssl_tls13_handshake_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100467{
468 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100469 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100470
471 /* We should never call this function with an unknown hash,
472 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100473 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
475
476 /*
477 *
478 * Handshake Secret
479 * |
480 * +-----> Derive-Secret( ., "c hs traffic",
481 * | ClientHello...ServerHello )
482 * | = client_handshake_traffic_secret
483 * |
484 * +-----> Derive-Secret( ., "s hs traffic",
485 * | ClientHello...ServerHello )
486 * | = server_handshake_traffic_secret
487 *
488 */
489
490 /*
491 * Compute client_handshake_traffic_secret with
492 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
493 */
494
Gabor Mezei07732f72022-03-26 17:04:19 +0100495 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
496 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100497 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ),
498 transcript, transcript_len,
499 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
500 derived->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100501 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100502 if( ret != 0 )
503 return( ret );
504
505 /*
506 * Compute server_handshake_traffic_secret with
507 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
508 */
509
Gabor Mezei07732f72022-03-26 17:04:19 +0100510 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
511 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100512 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ),
513 transcript, transcript_len,
514 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
515 derived->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100516 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100517 if( ret != 0 )
518 return( ret );
519
520 return( 0 );
521}
522
Xiaofei Bai746f9482021-11-12 08:53:56 +0000523int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100524 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100525 unsigned char const *application_secret,
526 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000527 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100528{
529 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100530 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100531
532 /* We should never call this function with an unknown hash,
533 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100534 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
536
537 /* Generate {client,server}_application_traffic_secret_0
538 *
539 * Master Secret
540 * |
541 * +-----> Derive-Secret( ., "c ap traffic",
542 * | ClientHello...server Finished )
543 * | = client_application_traffic_secret_0
544 * |
545 * +-----> Derive-Secret( ., "s ap traffic",
546 * | ClientHello...Server Finished )
547 * | = server_application_traffic_secret_0
548 * |
549 * +-----> Derive-Secret( ., "exp master",
550 * | ClientHello...server Finished)
551 * | = exporter_master_secret
552 *
553 */
554
Gabor Mezei07732f72022-03-26 17:04:19 +0100555 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
556 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100557 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ),
558 transcript, transcript_len,
559 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
560 derived->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100561 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100562 if( ret != 0 )
563 return( ret );
564
Gabor Mezei07732f72022-03-26 17:04:19 +0100565 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
566 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100567 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ),
568 transcript, transcript_len,
569 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
570 derived->server_application_traffic_secret_N,
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
Gabor Mezei07732f72022-03-26 17:04:19 +0100575 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
576 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100577 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ),
578 transcript, transcript_len,
579 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
580 derived->exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100581 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100582 if( ret != 0 )
583 return( ret );
584
585 return( 0 );
586}
587
588/* Generate resumption_master_secret for use with the ticket exchange.
589 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000590 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100591 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000592int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100593 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100594 unsigned char const *application_secret,
595 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000596 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100597{
598 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100599 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100600
601 /* We should never call this function with an unknown hash,
602 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100603 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
605
Gabor Mezei07732f72022-03-26 17:04:19 +0100606 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
607 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100608 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ),
609 transcript, transcript_len,
610 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
611 derived->resumption_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100612 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100613
614 if( ret != 0 )
615 return( ret );
616
617 return( 0 );
618}
619
XiaokangQiana4c99f22021-11-11 06:46:35 +0000620int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000621{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000623 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +0100624 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
625 handshake->ciphersuite_info->mac );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000626
627 /*
628 * Compute MasterSecret
629 */
Gabor Mezei07732f72022-03-26 17:04:19 +0100630 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000631 handshake->tls13_master_secrets.handshake,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000632 NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000633 handshake->tls13_master_secrets.app );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000634 if( ret != 0 )
635 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000636 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000637 return( ret );
638 }
639
640 MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret",
Gabor Mezei07732f72022-03-26 17:04:19 +0100641 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000642
643 return( 0 );
644}
645
Gabor Mezei07732f72022-03-26 17:04:19 +0100646static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000647 unsigned char const *base_key,
648 unsigned char const *transcript,
Gabor Mezei07732f72022-03-26 17:04:19 +0100649 unsigned char *dst,
650 size_t *dst_len )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100651{
Gabor Mezei07732f72022-03-26 17:04:19 +0100652 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
655 size_t hash_len = PSA_HASH_LENGTH( hash_alg );
656 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100657 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100658 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100659
660 /* We should never call this function with an unknown hash,
661 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100662 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
664
665 /* TLS 1.3 Finished message
666 *
667 * struct {
668 * opaque verify_data[Hash.length];
669 * } Finished;
670 *
671 * verify_data =
672 * HMAC( finished_key,
673 * Hash( Handshake Context +
674 * Certificate* +
675 * CertificateVerify* )
676 * )
677 *
678 * finished_key =
679 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
680 */
681
Xiaofei Bai746f9482021-11-12 08:53:56 +0000682 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100683 hash_alg, base_key, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100684 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ),
685 NULL, 0,
Gabor Mezei07732f72022-03-26 17:04:19 +0100686 finished_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100687 if( ret != 0 )
688 goto exit;
689
Gabor Mezei07732f72022-03-26 17:04:19 +0100690 alg = PSA_ALG_HMAC( hash_alg );
691 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
692 psa_set_key_algorithm( &attributes, alg );
693 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
694
695 status = psa_import_key( &attributes, finished_key, hash_len, &key );
696 if( status != PSA_SUCCESS )
697 {
698 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100699 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100700 }
701
702 status = psa_mac_compute( key, alg, transcript, hash_len,
703 dst, hash_len, dst_len );
704 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100705
706exit:
707
Gabor Mezei07732f72022-03-26 17:04:19 +0100708 status = psa_destroy_key( key );
709 if( ret == 0 )
710 ret = psa_ssl_status_to_mbedtls( status );
711
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100712 mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100713
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100714 return( ret );
715}
716
XiaokangQianc5c39d52021-11-09 11:55:10 +0000717int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000718 unsigned char* dst,
719 size_t dst_len,
720 size_t *actual_len,
721 int from )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000722{
XiaokangQiana7634982021-10-22 06:32:32 +0000723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000724
XiaokangQianaaa0e192021-11-10 03:07:04 +0000725 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000726 size_t transcript_len;
727
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800728 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800729 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800730 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
731 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800732
733 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100734
735 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(
736 ssl->handshake->ciphersuite_info->mac );
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200737 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yua5563f62021-12-10 18:14:36 +0800738
739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) );
740
Jerry Yub737f6a2021-12-10 17:55:23 +0800741 if( from == MBEDTLS_SSL_IS_CLIENT )
742 {
743 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
744 base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret );
745 }
746 else
747 {
748 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
749 base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret );
750 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000751
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200752 if( dst_len < hash_len )
Jerry Yu9c074732021-12-10 17:12:43 +0800753 {
754 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
755 goto exit;
756 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000757
758 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
759 transcript, sizeof( transcript ),
760 &transcript_len );
761 if( ret != 0 )
762 {
763 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000764 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000765 }
766 MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len );
767
Gabor Mezei07732f72022-03-26 17:04:19 +0100768 ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000769 if( ret != 0 )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000770 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000771
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200772 MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000774
775exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800776 /* Erase handshake secrets */
Jerry Yub737f6a2021-12-10 17:55:23 +0800777 mbedtls_platform_zeroize( base_key, base_key_len );
XiaokangQian33062842021-11-11 03:37:45 +0000778 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000779 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000780}
781
Xiaofei Bai746f9482021-11-12 08:53:56 +0000782int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100783 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100784 unsigned char const *psk, size_t psk_len,
785 int psk_type,
786 unsigned char const *transcript,
787 unsigned char *result )
788{
789 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100790 unsigned char binder_key[PSA_MAC_MAX_SIZE];
791 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gabor Mezei07732f72022-03-26 17:04:19 +0100792 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
793 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100794
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100795#if !defined(MBEDTLS_DEBUG_C)
796 ssl = NULL; /* make sure we don't use it except for debug */
797 ((void) ssl);
798#endif
799
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100800 /* We should never call this function with an unknown hash,
801 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100802 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
804
805 /*
806 * 0
807 * |
808 * v
809 * PSK -> HKDF-Extract = Early Secret
810 * |
811 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
812 * | = binder_key
813 * v
814 */
815
Gabor Mezei07732f72022-03-26 17:04:19 +0100816 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000817 NULL, /* Old secret */
818 psk, psk_len, /* Input */
819 early_secret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100820 if( ret != 0 )
821 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100823 goto exit;
824 }
825
826 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
827 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100828 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
829 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100830 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ),
831 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100832 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100833 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) );
834 }
835 else
836 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100837 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
838 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100839 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ),
840 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100841 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100842 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) );
843 }
844
845 if( ret != 0 )
846 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100848 goto exit;
849 }
850
851 /*
852 * The binding_value is computed in the same way as the Finished message
853 * but with the BaseKey being the binder_key.
854 */
855
Gabor Mezei07732f72022-03-26 17:04:19 +0100856 ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript,
857 result, &actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100858 if( ret != 0 )
859 goto exit;
860
Gabor Mezei07732f72022-03-26 17:04:19 +0100861 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100862
863exit:
864
865 mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) );
866 mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) );
867 return( ret );
868}
869
Hanno Beckerc94060c2021-03-22 07:50:44 +0000870int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
871 int endpoint,
872 int ciphersuite,
873 mbedtls_ssl_key_set const *traffic_keys,
874 mbedtls_ssl_context *ssl /* DEBUG ONLY */ )
875{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100876#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000877 int ret;
878 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200879#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000880 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
881 unsigned char const *key_enc;
882 unsigned char const *iv_enc;
883 unsigned char const *key_dec;
884 unsigned char const *iv_dec;
885
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100886#if defined(MBEDTLS_USE_PSA_CRYPTO)
887 psa_key_type_t key_type;
888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
889 psa_algorithm_t alg;
890 size_t key_bits;
891 psa_status_t status = PSA_SUCCESS;
892#endif
893
Hanno Beckerc94060c2021-03-22 07:50:44 +0000894#if !defined(MBEDTLS_DEBUG_C)
895 ssl = NULL; /* make sure we don't use it except for those cases */
896 (void) ssl;
897#endif
898
899 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker7887a772021-04-20 05:27:57 +0100900 if( ciphersuite_info == NULL )
901 {
902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
903 ciphersuite ) );
904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
905 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000906
Neil Armstronga8093f52022-05-04 17:44:05 +0200907#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000908 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
909 if( cipher_info == NULL )
910 {
Hanno Becker7887a772021-04-20 05:27:57 +0100911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
912 ciphersuite_info->cipher ) );
Hanno Beckerf62a7302021-04-21 05:21:28 +0100913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerc94060c2021-03-22 07:50:44 +0000914 }
915
916 /*
917 * Setup cipher contexts in target transform
918 */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000919 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
920 cipher_info ) ) != 0 )
921 {
922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
923 return( ret );
924 }
925
926 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
927 cipher_info ) ) != 0 )
928 {
929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
930 return( ret );
931 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100932#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000933
934#if defined(MBEDTLS_SSL_SRV_C)
935 if( endpoint == MBEDTLS_SSL_IS_SERVER )
936 {
937 key_enc = traffic_keys->server_write_key;
938 key_dec = traffic_keys->client_write_key;
939 iv_enc = traffic_keys->server_write_iv;
940 iv_dec = traffic_keys->client_write_iv;
941 }
942 else
943#endif /* MBEDTLS_SSL_SRV_C */
944#if defined(MBEDTLS_SSL_CLI_C)
945 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
946 {
947 key_enc = traffic_keys->client_write_key;
948 key_dec = traffic_keys->server_write_key;
949 iv_enc = traffic_keys->client_write_iv;
950 iv_dec = traffic_keys->server_write_iv;
951 }
952 else
953#endif /* MBEDTLS_SSL_CLI_C */
954 {
955 /* should not happen */
956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
957 }
958
959 memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len );
960 memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len );
961
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100962#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000963 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc,
964 key_enc, cipher_info->key_bitlen,
965 MBEDTLS_ENCRYPT ) ) != 0 )
966 {
967 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
968 return( ret );
969 }
970
971 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec,
972 key_dec, cipher_info->key_bitlen,
973 MBEDTLS_DECRYPT ) ) != 0 )
974 {
975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
976 return( ret );
977 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100978#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000979
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100980 /*
981 * Setup other fields in SSL transform
982 */
983
984 if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 )
985 transform->taglen = 8;
986 else
987 transform->taglen = 16;
988
989 transform->ivlen = traffic_keys->iv_len;
990 transform->maclen = 0;
991 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -0400992 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100993
994 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800995 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100996 * type-extended and padded plaintext is therefore the padding
997 * granularity. */
998 transform->minlen =
999 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1000
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001001#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001002 /*
1003 * Setup psa keys and alg
1004 */
Neil Armstronga8093f52022-05-04 17:44:05 +02001005 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001006 transform->taglen,
1007 &alg,
1008 &key_type,
1009 &key_bits ) ) != PSA_SUCCESS )
1010 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) );
1012 return( psa_ssl_status_to_mbedtls( status ) );
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001013 }
1014
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001015 transform->psa_alg = alg;
1016
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001017 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001018 {
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001019 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1020 psa_set_key_algorithm( &attributes, alg );
1021 psa_set_key_type( &attributes, key_type );
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001022
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001023 if( ( status = psa_import_key( &attributes,
1024 key_enc,
1025 PSA_BITS_TO_BYTES( key_bits ),
1026 &transform->psa_key_enc ) ) != PSA_SUCCESS )
1027 {
1028 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1029 return( psa_ssl_status_to_mbedtls( status ) );
1030 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001031
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001032 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1033
1034 if( ( status = psa_import_key( &attributes,
1035 key_dec,
1036 PSA_BITS_TO_BYTES( key_bits ),
1037 &transform->psa_key_dec ) ) != PSA_SUCCESS )
1038 {
1039 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1040 return( psa_ssl_status_to_mbedtls( status ) );
1041 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001042 }
1043#endif /* MBEDTLS_USE_PSA_CRYPTO */
1044
Hanno Beckerc94060c2021-03-22 07:50:44 +00001045 return( 0 );
1046}
1047
Xiaofei Bai746f9482021-11-12 08:53:56 +00001048int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl )
Jerry Yu89ea3212021-09-09 14:31:24 +08001049{
Jerry Yue3131ef2021-09-16 13:14:15 +08001050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001051 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001052 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001053
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001054 if( handshake->ciphersuite_info == NULL )
Jerry Yu89ea3212021-09-09 14:31:24 +08001055 {
1056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) );
1057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1058 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001059
Gabor Mezei07732f72022-03-26 17:04:19 +01001060 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
Jerry Yue06f4532021-09-23 18:35:07 +08001061
Gabor Mezei07732f72022-03-26 17:04:19 +01001062 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001063 handshake->tls13_master_secrets.early );
Jerry Yu89ea3212021-09-09 14:31:24 +08001064 if( ret != 0 )
1065 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001066 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yu89ea3212021-09-09 14:31:24 +08001067 return( ret );
1068 }
1069
1070 return( 0 );
1071}
1072
Neil Armstrongb818e162022-05-17 09:24:52 +02001073static int mbedtls_ssl_tls13_get_cipher_key_info(
1074 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1075 size_t *key_len, size_t *iv_len )
1076{
1077 psa_key_type_t key_type;
1078 psa_algorithm_t alg;
1079 size_t taglen;
1080 size_t key_bits;
1081 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1082
1083 if( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG )
1084 taglen = 8;
1085 else
1086 taglen = 16;
1087
1088 status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, taglen,
1089 &alg, &key_type, &key_bits );
1090 if( status != PSA_SUCCESS )
1091 return psa_ssl_status_to_mbedtls( status );
1092
1093 *key_len = PSA_BITS_TO_BYTES( key_bits );
1094
Neil Armstrong0fa8ce32022-05-17 14:42:57 +02001095 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1096 *iv_len = 12;
Neil Armstrongb818e162022-05-17 09:24:52 +02001097
1098 return 0;
1099}
1100
Jerry Yuc068b662021-10-11 22:30:19 +08001101/* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
Jerry Yu61e35e02021-09-16 18:59:08 +08001102 * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
Jerry Yuc068b662021-10-11 22:30:19 +08001103int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
1104 mbedtls_ssl_key_set *traffic_keys )
Jerry Yu61e35e02021-09-16 18:59:08 +08001105{
1106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1107
1108 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001109
1110 psa_algorithm_t hash_alg;
1111 size_t hash_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001112
Jerry Yu435208a2021-10-13 11:22:16 +08001113 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001114 size_t transcript_len;
1115
Xiaofei Baib7972842021-11-18 07:29:56 +00001116 size_t key_len, iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001117
Jerry Yu435208a2021-10-13 11:22:16 +08001118 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1119 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001120 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001121
Jerry Yuc068b662021-10-11 22:30:19 +08001122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001123
Neil Armstrongb818e162022-05-17 09:24:52 +02001124 ret = mbedtls_ssl_tls13_get_cipher_key_info( ciphersuite_info,
1125 &key_len, &iv_len );
1126 if( ret != 0 )
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001127 {
Neil Armstrongb818e162022-05-17 09:24:52 +02001128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_get_cipher_key_info", ret );
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001129 return ret;
1130 }
1131
Jerry Yu435208a2021-10-13 11:22:16 +08001132 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001133
1134 hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
1135 hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yu61e35e02021-09-16 18:59:08 +08001136
1137 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1138 transcript,
1139 sizeof( transcript ),
1140 &transcript_len );
1141 if( ret != 0 )
1142 {
1143 MBEDTLS_SSL_DEBUG_RET( 1,
1144 "mbedtls_ssl_get_handshake_transcript",
1145 ret );
1146 return( ret );
1147 }
1148
Gabor Mezei07732f72022-03-26 17:04:19 +01001149 ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001150 handshake->tls13_master_secrets.handshake,
Jerry Yu435208a2021-10-13 11:22:16 +08001151 transcript, transcript_len, tls13_hs_secrets );
Jerry Yu61e35e02021-09-16 18:59:08 +08001152 if( ret != 0 )
1153 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001154 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets",
Jerry Yu61e35e02021-09-16 18:59:08 +08001155 ret );
1156 return( ret );
1157 }
1158
1159 MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001160 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001161 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001162 MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001163 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001164 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001165
1166 /*
1167 * Export client handshake traffic secret
1168 */
Jerry Yu61e35e02021-09-16 18:59:08 +08001169 if( ssl->f_export_keys != NULL )
1170 {
1171 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001172 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001173 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001174 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001175 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001176 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001177 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1178
1179 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001180 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001181 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001182 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001183 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001184 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001185 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1186 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001187
Gabor Mezei07732f72022-03-26 17:04:19 +01001188 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
Jerry Yu435208a2021-10-13 11:22:16 +08001189 tls13_hs_secrets->client_handshake_traffic_secret,
1190 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001191 hash_len, key_len, iv_len, traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +08001192 if( ret != 0 )
1193 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
Jerry Yu61e35e02021-09-16 18:59:08 +08001195 goto exit;
1196 }
1197
1198 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key",
1199 traffic_keys->client_write_key,
1200 traffic_keys->key_len);
1201
1202 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key",
1203 traffic_keys->server_write_key,
1204 traffic_keys->key_len);
1205
1206 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv",
1207 traffic_keys->client_write_iv,
1208 traffic_keys->iv_len);
1209
1210 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv",
1211 traffic_keys->server_write_iv,
1212 traffic_keys->iv_len);
1213
Jerry Yuc068b662021-10-11 22:30:19 +08001214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001215
1216exit:
1217
1218 return( ret );
1219}
1220
Jerry Yuf0ac2352021-10-11 17:47:07 +08001221int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl )
Jerry Yua0650eb2021-09-09 17:14:45 +08001222{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001224#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C)
1225 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1226#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001227 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +01001228 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
1229 handshake->ciphersuite_info->mac );
Jerry Yua0650eb2021-09-09 17:14:45 +08001230
Jerry Yuf0ac2352021-10-11 17:47:07 +08001231#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
1232 /*
1233 * Compute ECDHE secret used to compute the handshake secret from which
1234 * client_handshake_traffic_secret and server_handshake_traffic_secret
1235 * are derived in the handshake secret derivation stage.
1236 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001237 if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) )
Jerry Yuf0ac2352021-10-11 17:47:07 +08001238 {
1239 if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) )
1240 {
1241#if defined(MBEDTLS_ECDH_C)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001242 /* Compute ECDH shared secret. */
1243 status = psa_raw_key_agreement(
1244 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1245 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1246 handshake->premaster, sizeof( handshake->premaster ),
1247 &handshake->pmslen );
1248 if( status != PSA_SUCCESS )
1249 {
1250 ret = psa_ssl_status_to_mbedtls( status );
1251 MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret );
1252 return( ret );
1253 }
1254
1255 status = psa_destroy_key( handshake->ecdh_psa_privkey );
1256 if( status != PSA_SUCCESS )
1257 {
1258 ret = psa_ssl_status_to_mbedtls( status );
1259 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
1260 return( ret );
1261 }
1262
1263 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001264#endif /* MBEDTLS_ECDH_C */
1265 }
1266 else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) )
1267 {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) );
1269 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1270 }
1271 }
1272#else
1273 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1274#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001275
1276 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001277 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001278 */
Gabor Mezei07732f72022-03-26 17:04:19 +01001279 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001280 handshake->tls13_master_secrets.early,
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001281 handshake->premaster, handshake->pmslen,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001282 handshake->tls13_master_secrets.handshake );
Jerry Yua0650eb2021-09-09 17:14:45 +08001283 if( ret != 0 )
1284 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001285 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yua0650eb2021-09-09 17:14:45 +08001286 return( ret );
1287 }
1288
1289 MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret",
Gabor Mezei07732f72022-03-26 17:04:19 +01001290 handshake->tls13_master_secrets.handshake,
1291 PSA_HASH_LENGTH( hash_alg ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001292
1293#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001294 mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001295#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
1296 return( 0 );
1297}
1298
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001299/* Generate application traffic keys since any records following a 1-RTT Finished message
1300 * MUST be encrypted under the application traffic key.
1301 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001302int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001303 mbedtls_ssl_context *ssl,
1304 mbedtls_ssl_key_set *traffic_keys )
1305{
XiaokangQiana7634982021-10-22 06:32:32 +00001306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001307 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001308
1309 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001310 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001311 &ssl->session_negotiate->app_secrets;
1312
1313 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001314 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001315 size_t transcript_len;
1316
1317 /* Variables relating to the hash for the chosen ciphersuite. */
1318 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001319
1320 psa_algorithm_t hash_alg;
1321 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001322
1323 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001324 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001325
1326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) );
1327
1328 /* Extract basic information about hash and ciphersuite */
1329
Neil Armstrongb818e162022-05-17 09:24:52 +02001330 ret = mbedtls_ssl_tls13_get_cipher_key_info( handshake->ciphersuite_info,
1331 &key_len, &iv_len );
1332 if( ret != 0 )
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001333 {
Neil Armstrongb818e162022-05-17 09:24:52 +02001334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_get_cipher_key_info", ret );
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001335 goto cleanup;
1336 }
1337
XiaokangQian33062842021-11-11 03:37:45 +00001338 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001339
1340 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
1341 hash_len = PSA_HASH_LENGTH( hash_alg );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001342
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001343 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001344 * to call this at the right time, that is, after the ServerFinished. */
1345
1346 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1347 transcript, sizeof( transcript ),
1348 &transcript_len );
1349 if( ret != 0 )
XiaokangQian4cab0242021-10-12 08:43:37 +00001350 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001351
1352 /* Compute application secrets from master secret and transcript hash. */
1353
Gabor Mezei07732f72022-03-26 17:04:19 +01001354 ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001355 handshake->tls13_master_secrets.app,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001356 transcript, transcript_len,
1357 app_secrets );
1358 if( ret != 0 )
1359 {
1360 MBEDTLS_SSL_DEBUG_RET( 1,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001361 "mbedtls_ssl_tls13_derive_application_secrets", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001362 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001363 }
1364
1365 /* Derive first epoch of IV + Key for application traffic. */
1366
Gabor Mezei07732f72022-03-26 17:04:19 +01001367 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001368 app_secrets->client_application_traffic_secret_N,
1369 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001370 hash_len, key_len, iv_len, traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001371 if( ret != 0 )
1372 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001374 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001375 }
1376
1377 MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret",
1378 app_secrets->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001379 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001380
1381 MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret",
1382 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001383 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001384
XiaokangQianac0385c2021-11-03 06:40:11 +00001385 /*
1386 * Export client/server application traffic secret 0
1387 */
1388 if( ssl->f_export_keys != NULL )
1389 {
1390 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001391 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001392 app_secrets->client_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001393 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001394 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001395 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1396 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001397
1398 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001399 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001400 app_secrets->server_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001401 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001402 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001403 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1404 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001405 }
1406
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001407 MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:",
Xiaofei Baib7972842021-11-18 07:29:56 +00001408 traffic_keys->client_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001409 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key",
Xiaofei Baib7972842021-11-18 07:29:56 +00001410 traffic_keys->server_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001411 MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001412 traffic_keys->client_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001413 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001414 traffic_keys->server_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001415
1416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001417
1418 cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001419 /* randbytes is not used again */
1420 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1421 sizeof( ssl->handshake->randbytes ) );
Jerry Yuef2b98a2022-05-06 16:40:05 +08001422
XiaokangQian33062842021-11-11 03:37:45 +00001423 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001424 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001425}
1426
Jerry Yuf86eb752022-05-06 11:16:55 +08001427int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl )
Jerry Yue110d252022-05-05 10:19:22 +08001428{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001430 mbedtls_ssl_key_set traffic_keys;
1431 mbedtls_ssl_transform *transform_handshake = NULL;
1432 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1433
1434 /* Compute handshake secret */
1435 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
1436 if( ret != 0 )
1437 {
1438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
1439 goto cleanup;
1440 }
1441
1442 /* Next evolution in key schedule: Establish handshake secret and
1443 * key material. */
1444 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
1445 if( ret != 0 )
1446 {
1447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1448 ret );
1449 goto cleanup;
1450 }
1451
1452 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1453 if( transform_handshake == NULL )
1454 {
1455 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1456 goto cleanup;
1457 }
1458
Jerry Yuef2b98a2022-05-06 16:40:05 +08001459 ret = mbedtls_ssl_tls13_populate_transform(
1460 transform_handshake,
1461 ssl->conf->endpoint,
1462 ssl->session_negotiate->ciphersuite,
1463 &traffic_keys,
1464 ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001465 if( ret != 0 )
1466 {
1467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
1468 goto cleanup;
1469 }
1470 handshake->transform_handshake = transform_handshake;
1471
1472cleanup:
1473 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
1474 if( ret != 0 )
Jerry Yue110d252022-05-05 10:19:22 +08001475 mbedtls_free( transform_handshake );
Jerry Yue110d252022-05-05 10:19:22 +08001476
1477 return( ret );
1478}
1479
Jerry Yuff226982022-04-16 16:52:57 +08001480int mbedtls_ssl_tls13_generate_resumption_master_secret(
1481 mbedtls_ssl_context *ssl )
1482{
1483 /* Erase master secrets */
1484 mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets,
1485 sizeof( ssl->handshake->tls13_master_secrets ) );
1486 return( 0 );
1487}
1488
Jerry Yufd5ea042022-05-19 14:29:48 +08001489int mbedtls_ssl_tls13_compute_application_transform( mbedtls_ssl_context *ssl )
1490{
1491 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1492 mbedtls_ssl_key_set traffic_keys;
1493 mbedtls_ssl_transform *transform_application = NULL;
1494
1495 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
1496 if( ret != 0 )
1497 {
1498 MBEDTLS_SSL_DEBUG_RET( 1,
1499 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
1500 goto cleanup;
1501 }
1502
1503 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
1504 if( ret != 0 )
1505 {
1506 MBEDTLS_SSL_DEBUG_RET( 1,
1507 "mbedtls_ssl_tls13_generate_application_keys", ret );
1508 goto cleanup;
1509 }
1510
1511 transform_application =
1512 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1513 if( transform_application == NULL )
1514 {
1515 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1516 goto cleanup;
1517 }
1518
1519 ret = mbedtls_ssl_tls13_populate_transform(
1520 transform_application,
1521 ssl->conf->endpoint,
1522 ssl->session_negotiate->ciphersuite,
1523 &traffic_keys,
1524 ssl );
1525 if( ret != 0 )
1526 {
1527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
1528 goto cleanup;
1529 }
1530
1531 ssl->transform_application = transform_application;
1532
1533cleanup:
1534
1535 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
1536 if( ret != 0 )
1537 {
1538 mbedtls_free( transform_application );
1539 }
1540 return( ret );
1541}
1542
Ronald Cron6f135e12021-12-08 16:57:54 +01001543#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */