blob: d4a8e46431272da0399b4c683e9809ef878f650f [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 ];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200148 size_t hkdf_label_len = 0;
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 Stekiel1b0ebdf2022-06-23 09:22:49 +0200214 mbedtls_platform_zeroize( hkdf_label, hkdf_label_len );
Przemek Stekield5ae3652022-05-13 12:10:08 +0200215 return( psa_ssl_status_to_mbedtls ( status ) );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100216}
217
Hanno Becker3385a4d2020-08-21 13:03:34 +0100218/*
219 * The traffic keying material is generated from the following inputs:
220 *
221 * - One secret value per sender.
222 * - A purpose value indicating the specific value being generated
223 * - The desired lengths of key and IV.
224 *
225 * The expansion itself is based on HKDF:
226 *
227 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
228 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
229 *
230 * [sender] denotes the sending side and the Secret value is provided
231 * by the function caller. Note that we generate server and client side
232 * keys in a single function call.
233 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000234int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100235 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100236 const unsigned char *client_secret,
Xiaofei Baib7972842021-11-18 07:29:56 +0000237 const unsigned char *server_secret, size_t secret_len,
238 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100239 mbedtls_ssl_key_set *keys )
240{
241 int ret = 0;
242
Xiaofei Bai746f9482021-11-12 08:53:56 +0000243 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000244 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100245 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
246 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100247 keys->client_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100248 if( ret != 0 )
249 return( ret );
250
Xiaofei Bai746f9482021-11-12 08:53:56 +0000251 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000252 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100253 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
254 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100255 keys->server_write_key, key_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100256 if( ret != 0 )
257 return( ret );
258
Xiaofei Bai746f9482021-11-12 08:53:56 +0000259 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000260 client_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100261 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
262 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100263 keys->client_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100264 if( ret != 0 )
265 return( ret );
266
Xiaofei Bai746f9482021-11-12 08:53:56 +0000267 ret = mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000268 server_secret, secret_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100269 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
270 NULL, 0,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100271 keys->server_write_iv, iv_len );
Hanno Becker3385a4d2020-08-21 13:03:34 +0100272 if( ret != 0 )
273 return( ret );
274
Hanno Becker493ea7f2020-09-08 11:01:00 +0100275 keys->key_len = key_len;
276 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100277
278 return( 0 );
279}
280
Xiaofei Bai746f9482021-11-12 08:53:56 +0000281int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100282 psa_algorithm_t hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000283 const unsigned char *secret, size_t secret_len,
284 const unsigned char *label, size_t label_len,
285 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100286 int ctx_hashed,
Xiaofei Baib7972842021-11-18 07:29:56 +0000287 unsigned char *dstbuf, size_t dstbuf_len )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100288{
289 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100290 unsigned char hashed_context[ PSA_HASH_MAX_SIZE ];
Hanno Becker0c42fd92020-09-09 12:58:29 +0100291 if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
Hanno Beckerb35d5222020-08-21 13:27:44 +0100292 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100293 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
294
295 status = psa_hash_compute( hash_alg, ctx, ctx_len, hashed_context,
296 PSA_HASH_LENGTH( hash_alg ), &ctx_len );
297 if( status != PSA_SUCCESS )
298 {
299 ret = psa_ssl_status_to_mbedtls( status );
300 return ret;
301 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100302 }
303 else
304 {
Xiaofei Baib7972842021-11-18 07:29:56 +0000305 if( ctx_len > sizeof(hashed_context) )
Hanno Becker97a21562020-09-09 12:57:16 +0100306 {
307 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100308 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100309 * Let's double-check nonetheless to not run at the risk
310 * of getting a stack overflow. */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100311 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Hanno Becker97a21562020-09-09 12:57:16 +0100312 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100313
Xiaofei Baib7972842021-11-18 07:29:56 +0000314 memcpy( hashed_context, ctx, ctx_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100315 }
316
Xiaofei Bai746f9482021-11-12 08:53:56 +0000317 return( mbedtls_ssl_tls13_hkdf_expand_label( hash_alg,
Xiaofei Baib7972842021-11-18 07:29:56 +0000318 secret, secret_len,
319 label, label_len,
320 hashed_context, ctx_len,
321 dstbuf, dstbuf_len ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100322
Hanno Beckerb35d5222020-08-21 13:27:44 +0100323}
324
Xiaofei Bai746f9482021-11-12 08:53:56 +0000325int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100326 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100327 const unsigned char *secret_old,
328 const unsigned char *input, size_t input_len,
329 unsigned char *secret_new )
330{
331 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
333 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100334 size_t hlen, ilen;
Gabor Mezei07732f72022-03-26 17:04:19 +0100335 unsigned char tmp_secret[ PSA_MAC_MAX_SIZE ] = { 0 };
Jerry Yu6eaa41c2021-11-22 18:16:45 +0800336 unsigned char tmp_input [ MBEDTLS_ECP_MAX_BYTES ] = { 0 };
Przemek Stekield5ae3652022-05-13 12:10:08 +0200337 psa_key_derivation_operation_t operation =
338 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100339
340 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Gabor Mezei58db6532022-03-21 12:12:37 +0100341 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
342
Gabor Mezei07732f72022-03-26 17:04:19 +0100343 hlen = PSA_HASH_LENGTH( hash_alg );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100344
345 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100346 * on the old secret. */
Hanno Beckere9cccb42020-08-20 13:42:46 +0100347 if( secret_old != NULL )
348 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000349 ret = mbedtls_ssl_tls13_derive_secret(
Hanno Beckere9cccb42020-08-20 13:42:46 +0100350 hash_alg,
351 secret_old, hlen,
352 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
353 NULL, 0, /* context */
354 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Hanno Becker59b50a12020-09-09 10:56:56 +0100355 tmp_secret, hlen );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100356 if( ret != 0 )
357 goto cleanup;
358 }
359
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200360 ret = 0;
361
Hanno Beckere9cccb42020-08-20 13:42:46 +0100362 if( input != NULL )
363 {
Hanno Becker59b50a12020-09-09 10:56:56 +0100364 memcpy( tmp_input, input, input_len );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100365 ilen = input_len;
366 }
367 else
368 {
369 ilen = hlen;
370 }
371
Przemek Stekield5ae3652022-05-13 12:10:08 +0200372 status = psa_key_derivation_setup( &operation,
373 PSA_ALG_HKDF_EXTRACT( hash_alg ) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100374
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200375 if( status != PSA_SUCCESS )
376 goto cleanup;
377
378 status = psa_key_derivation_input_bytes( &operation,
379 PSA_KEY_DERIVATION_INPUT_SALT,
380 tmp_secret,
381 hlen );
382
383 if( status != PSA_SUCCESS )
384 goto cleanup;
385
386 status = psa_key_derivation_input_bytes( &operation,
387 PSA_KEY_DERIVATION_INPUT_SECRET,
388 tmp_input,
389 ilen );
390
391 if( status != PSA_SUCCESS )
392 goto cleanup;
393
394 status = psa_key_derivation_output_bytes( &operation,
395 secret_new,
396 PSA_HASH_LENGTH( hash_alg ) );
397
398 if( status != PSA_SUCCESS )
399 goto cleanup;
400
Hanno Beckere9cccb42020-08-20 13:42:46 +0100401 cleanup:
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200402 abort_status = psa_key_derivation_abort( &operation );
403 status = ( status == PSA_SUCCESS ? abort_status : status );
404 ret = ( ret == 0 ? psa_ssl_status_to_mbedtls ( status ) : ret );
Hanno Becker59b50a12020-09-09 10:56:56 +0100405 mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
406 mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
Hanno Beckere9cccb42020-08-20 13:42:46 +0100407 return( ret );
408}
409
Xiaofei Bai746f9482021-11-12 08:53:56 +0000410int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100411 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100412 unsigned char const *early_secret,
413 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000414 mbedtls_ssl_tls13_early_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100415{
416 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100417 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100418
419 /* We should never call this function with an unknown hash,
420 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100421 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
423
424 /*
425 * 0
426 * |
427 * v
428 * PSK -> HKDF-Extract = Early Secret
429 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100430 * +-----> Derive-Secret(., "c e traffic", ClientHello)
431 * | = client_early_traffic_secret
432 * |
433 * +-----> Derive-Secret(., "e exp master", ClientHello)
434 * | = early_exporter_master_secret
435 * v
436 */
437
438 /* Create client_early_traffic_secret */
Gabor Mezei07732f72022-03-26 17:04:19 +0100439 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
440 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100441 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ),
442 transcript, transcript_len,
443 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
444 derived->client_early_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100445 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100446 if( ret != 0 )
447 return( ret );
448
449 /* Create early exporter */
Gabor Mezei07732f72022-03-26 17:04:19 +0100450 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
451 early_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100452 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ),
453 transcript, transcript_len,
454 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
455 derived->early_exporter_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100456 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100457 if( ret != 0 )
458 return( ret );
459
460 return( 0 );
461}
462
Xiaofei Bai746f9482021-11-12 08:53:56 +0000463int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100464 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100465 unsigned char const *handshake_secret,
466 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000467 mbedtls_ssl_tls13_handshake_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100468{
469 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100470 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100471
472 /* We should never call this function with an unknown hash,
473 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100474 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
476
477 /*
478 *
479 * Handshake Secret
480 * |
481 * +-----> Derive-Secret( ., "c hs traffic",
482 * | ClientHello...ServerHello )
483 * | = client_handshake_traffic_secret
484 * |
485 * +-----> Derive-Secret( ., "s hs traffic",
486 * | ClientHello...ServerHello )
487 * | = server_handshake_traffic_secret
488 *
489 */
490
491 /*
492 * Compute client_handshake_traffic_secret with
493 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
494 */
495
Gabor Mezei07732f72022-03-26 17:04:19 +0100496 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
497 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100498 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ),
499 transcript, transcript_len,
500 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
501 derived->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100502 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100503 if( ret != 0 )
504 return( ret );
505
506 /*
507 * Compute server_handshake_traffic_secret with
508 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
509 */
510
Gabor Mezei07732f72022-03-26 17:04:19 +0100511 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
512 handshake_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100513 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ),
514 transcript, transcript_len,
515 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
516 derived->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100517 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100518 if( ret != 0 )
519 return( ret );
520
521 return( 0 );
522}
523
Xiaofei Bai746f9482021-11-12 08:53:56 +0000524int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100525 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100526 unsigned char const *application_secret,
527 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000528 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100529{
530 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100531 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100532
533 /* We should never call this function with an unknown hash,
534 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100535 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
537
538 /* Generate {client,server}_application_traffic_secret_0
539 *
540 * Master Secret
541 * |
542 * +-----> Derive-Secret( ., "c ap traffic",
543 * | ClientHello...server Finished )
544 * | = client_application_traffic_secret_0
545 * |
546 * +-----> Derive-Secret( ., "s ap traffic",
547 * | ClientHello...Server Finished )
548 * | = server_application_traffic_secret_0
549 * |
550 * +-----> Derive-Secret( ., "exp master",
551 * | ClientHello...server Finished)
552 * | = exporter_master_secret
553 *
554 */
555
Gabor Mezei07732f72022-03-26 17:04:19 +0100556 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
557 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100558 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ),
559 transcript, transcript_len,
560 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
561 derived->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100562 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100563 if( ret != 0 )
564 return( ret );
565
Gabor Mezei07732f72022-03-26 17:04:19 +0100566 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
567 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100568 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ),
569 transcript, transcript_len,
570 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
571 derived->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +0100572 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100573 if( ret != 0 )
574 return( ret );
575
Gabor Mezei07732f72022-03-26 17:04:19 +0100576 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
577 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100578 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ),
579 transcript, transcript_len,
580 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
581 derived->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
589/* Generate resumption_master_secret for use with the ticket exchange.
590 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000591 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100592 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000593int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100594 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100595 unsigned char const *application_secret,
596 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000597 mbedtls_ssl_tls13_application_secrets *derived )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100598{
599 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100600 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100601
602 /* We should never call this function with an unknown hash,
603 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100604 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckeref5235b2021-05-24 06:39:41 +0100605 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
606
Gabor Mezei07732f72022-03-26 17:04:19 +0100607 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
608 application_secret, hash_len,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100609 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ),
610 transcript, transcript_len,
611 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
612 derived->resumption_master_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +0100613 hash_len );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100614
615 if( ret != 0 )
616 return( ret );
617
618 return( 0 );
619}
620
XiaokangQiana4c99f22021-11-11 06:46:35 +0000621int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000622{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000624 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +0100625 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
626 handshake->ciphersuite_info->mac );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000627
628 /*
629 * Compute MasterSecret
630 */
Gabor Mezei07732f72022-03-26 17:04:19 +0100631 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000632 handshake->tls13_master_secrets.handshake,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000633 NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +0000634 handshake->tls13_master_secrets.app );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000635 if( ret != 0 )
636 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000637 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000638 return( ret );
639 }
640
641 MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret",
Gabor Mezei07732f72022-03-26 17:04:19 +0100642 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH( hash_alg ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000643
644 return( 0 );
645}
646
Gabor Mezei07732f72022-03-26 17:04:19 +0100647static int ssl_tls13_calc_finished_core( psa_algorithm_t hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000648 unsigned char const *base_key,
649 unsigned char const *transcript,
Gabor Mezei07732f72022-03-26 17:04:19 +0100650 unsigned char *dst,
651 size_t *dst_len )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100652{
Gabor Mezei07732f72022-03-26 17:04:19 +0100653 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
655 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
656 size_t hash_len = PSA_HASH_LENGTH( hash_alg );
657 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100658 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100659 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100660
661 /* We should never call this function with an unknown hash,
662 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100663 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100664 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
665
666 /* TLS 1.3 Finished message
667 *
668 * struct {
669 * opaque verify_data[Hash.length];
670 * } Finished;
671 *
672 * verify_data =
673 * HMAC( finished_key,
674 * Hash( Handshake Context +
675 * Certificate* +
676 * CertificateVerify* )
677 * )
678 *
679 * finished_key =
680 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
681 */
682
Xiaofei Bai746f9482021-11-12 08:53:56 +0000683 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100684 hash_alg, base_key, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100685 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ),
686 NULL, 0,
Gabor Mezei07732f72022-03-26 17:04:19 +0100687 finished_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100688 if( ret != 0 )
689 goto exit;
690
Gabor Mezei07732f72022-03-26 17:04:19 +0100691 alg = PSA_ALG_HMAC( hash_alg );
692 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
693 psa_set_key_algorithm( &attributes, alg );
694 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
695
696 status = psa_import_key( &attributes, finished_key, hash_len, &key );
697 if( status != PSA_SUCCESS )
698 {
699 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100700 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100701 }
702
703 status = psa_mac_compute( key, alg, transcript, hash_len,
704 dst, hash_len, dst_len );
705 ret = psa_ssl_status_to_mbedtls( status );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100706
707exit:
708
Gabor Mezei07732f72022-03-26 17:04:19 +0100709 status = psa_destroy_key( key );
710 if( ret == 0 )
711 ret = psa_ssl_status_to_mbedtls( status );
712
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100713 mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) );
Gabor Mezei07732f72022-03-26 17:04:19 +0100714
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100715 return( ret );
716}
717
XiaokangQianc5c39d52021-11-09 11:55:10 +0000718int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000719 unsigned char* dst,
720 size_t dst_len,
721 size_t *actual_len,
722 int from )
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000723{
XiaokangQiana7634982021-10-22 06:32:32 +0000724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000725
XiaokangQianaaa0e192021-11-10 03:07:04 +0000726 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000727 size_t transcript_len;
728
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800729 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800730 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800731 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
732 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800733
734 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100735
736 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(
737 ssl->handshake->ciphersuite_info->mac );
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200738 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yua5563f62021-12-10 18:14:36 +0800739
740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) );
741
Jerry Yub737f6a2021-12-10 17:55:23 +0800742 if( from == MBEDTLS_SSL_IS_CLIENT )
743 {
744 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
745 base_key_len = sizeof( tls13_hs_secrets->client_handshake_traffic_secret );
746 }
747 else
748 {
749 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
750 base_key_len = sizeof( tls13_hs_secrets->server_handshake_traffic_secret );
751 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000752
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200753 if( dst_len < hash_len )
Jerry Yu9c074732021-12-10 17:12:43 +0800754 {
755 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
756 goto exit;
757 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000758
759 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
760 transcript, sizeof( transcript ),
761 &transcript_len );
762 if( ret != 0 )
763 {
764 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000765 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000766 }
767 MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len );
768
Gabor Mezei07732f72022-03-26 17:04:19 +0100769 ret = ssl_tls13_calc_finished_core( hash_alg, base_key, transcript, dst, actual_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000770 if( ret != 0 )
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000771 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000772
Gabor Mezei29e7ca82022-03-29 17:08:49 +0200773 MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, hash_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +0000774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000775
776exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800777 /* Erase handshake secrets */
Jerry Yub737f6a2021-12-10 17:55:23 +0800778 mbedtls_platform_zeroize( base_key, base_key_len );
XiaokangQian33062842021-11-11 03:37:45 +0000779 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000780 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000781}
782
Xiaofei Bai746f9482021-11-12 08:53:56 +0000783int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100784 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100785 unsigned char const *psk, size_t psk_len,
786 int psk_type,
787 unsigned char const *transcript,
788 unsigned char *result )
789{
790 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100791 unsigned char binder_key[PSA_MAC_MAX_SIZE];
792 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gabor Mezei07732f72022-03-26 17:04:19 +0100793 size_t const hash_len = PSA_HASH_LENGTH( hash_alg );
794 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100795
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100796#if !defined(MBEDTLS_DEBUG_C)
797 ssl = NULL; /* make sure we don't use it except for debug */
798 ((void) ssl);
799#endif
800
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100801 /* We should never call this function with an unknown hash,
802 * but add an assertion anyway. */
Gabor Mezei07732f72022-03-26 17:04:19 +0100803 if( ! PSA_ALG_IS_HASH( hash_alg ) )
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100804 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
805
806 /*
807 * 0
808 * |
809 * v
810 * PSK -> HKDF-Extract = Early Secret
811 * |
812 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
813 * | = binder_key
814 * v
815 */
816
Gabor Mezei07732f72022-03-26 17:04:19 +0100817 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000818 NULL, /* Old secret */
819 psk, psk_len, /* Input */
820 early_secret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100821 if( ret != 0 )
822 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100824 goto exit;
825 }
826
827 if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
828 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100829 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
830 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100831 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ),
832 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100833 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100834 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) );
835 }
836 else
837 {
Gabor Mezei07732f72022-03-26 17:04:19 +0100838 ret = mbedtls_ssl_tls13_derive_secret( hash_alg,
839 early_secret, hash_len,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100840 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ),
841 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
Gabor Mezei07732f72022-03-26 17:04:19 +0100842 binder_key, hash_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100843 MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) );
844 }
845
846 if( ret != 0 )
847 {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000848 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_secret", ret );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100849 goto exit;
850 }
851
852 /*
853 * The binding_value is computed in the same way as the Finished message
854 * but with the BaseKey being the binder_key.
855 */
856
Gabor Mezei07732f72022-03-26 17:04:19 +0100857 ret = ssl_tls13_calc_finished_core( hash_alg, binder_key, transcript,
858 result, &actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100859 if( ret != 0 )
860 goto exit;
861
Gabor Mezei07732f72022-03-26 17:04:19 +0100862 MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, actual_len );
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100863
864exit:
865
866 mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) );
867 mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) );
868 return( ret );
869}
870
Hanno Beckerc94060c2021-03-22 07:50:44 +0000871int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
872 int endpoint,
873 int ciphersuite,
874 mbedtls_ssl_key_set const *traffic_keys,
875 mbedtls_ssl_context *ssl /* DEBUG ONLY */ )
876{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100877#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000878 int ret;
879 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200880#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000881 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
882 unsigned char const *key_enc;
883 unsigned char const *iv_enc;
884 unsigned char const *key_dec;
885 unsigned char const *iv_dec;
886
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100887#if defined(MBEDTLS_USE_PSA_CRYPTO)
888 psa_key_type_t key_type;
889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
890 psa_algorithm_t alg;
891 size_t key_bits;
892 psa_status_t status = PSA_SUCCESS;
893#endif
894
Hanno Beckerc94060c2021-03-22 07:50:44 +0000895#if !defined(MBEDTLS_DEBUG_C)
896 ssl = NULL; /* make sure we don't use it except for those cases */
897 (void) ssl;
898#endif
899
900 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Hanno Becker7887a772021-04-20 05:27:57 +0100901 if( ciphersuite_info == NULL )
902 {
903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
904 ciphersuite ) );
905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
906 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000907
Neil Armstronga8093f52022-05-04 17:44:05 +0200908#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000909 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
910 if( cipher_info == NULL )
911 {
Hanno Becker7887a772021-04-20 05:27:57 +0100912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
913 ciphersuite_info->cipher ) );
Hanno Beckerf62a7302021-04-21 05:21:28 +0100914 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerc94060c2021-03-22 07:50:44 +0000915 }
916
917 /*
918 * Setup cipher contexts in target transform
919 */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000920 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
921 cipher_info ) ) != 0 )
922 {
923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
924 return( ret );
925 }
926
927 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
928 cipher_info ) ) != 0 )
929 {
930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
931 return( ret );
932 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100933#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000934
935#if defined(MBEDTLS_SSL_SRV_C)
936 if( endpoint == MBEDTLS_SSL_IS_SERVER )
937 {
938 key_enc = traffic_keys->server_write_key;
939 key_dec = traffic_keys->client_write_key;
940 iv_enc = traffic_keys->server_write_iv;
941 iv_dec = traffic_keys->client_write_iv;
942 }
943 else
944#endif /* MBEDTLS_SSL_SRV_C */
945#if defined(MBEDTLS_SSL_CLI_C)
946 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
947 {
948 key_enc = traffic_keys->client_write_key;
949 key_dec = traffic_keys->server_write_key;
950 iv_enc = traffic_keys->client_write_iv;
951 iv_dec = traffic_keys->server_write_iv;
952 }
953 else
954#endif /* MBEDTLS_SSL_CLI_C */
955 {
956 /* should not happen */
957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
958 }
959
960 memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len );
961 memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len );
962
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100963#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000964 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc,
965 key_enc, cipher_info->key_bitlen,
966 MBEDTLS_ENCRYPT ) ) != 0 )
967 {
968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
969 return( ret );
970 }
971
972 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec,
973 key_dec, cipher_info->key_bitlen,
974 MBEDTLS_DECRYPT ) ) != 0 )
975 {
976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
977 return( ret );
978 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100979#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000980
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100981 /*
982 * Setup other fields in SSL transform
983 */
984
985 if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 )
986 transform->taglen = 8;
987 else
988 transform->taglen = 16;
989
990 transform->ivlen = traffic_keys->iv_len;
991 transform->maclen = 0;
992 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -0400993 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100994
995 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800996 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100997 * type-extended and padded plaintext is therefore the padding
998 * granularity. */
999 transform->minlen =
1000 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1001
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001002#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001003 /*
1004 * Setup psa keys and alg
1005 */
Neil Armstronga8093f52022-05-04 17:44:05 +02001006 if( ( status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher,
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001007 transform->taglen,
1008 &alg,
1009 &key_type,
1010 &key_bits ) ) != PSA_SUCCESS )
1011 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls( status ) );
1013 return( psa_ssl_status_to_mbedtls( status ) );
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001014 }
1015
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001016 transform->psa_alg = alg;
1017
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001018 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001019 {
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001020 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1021 psa_set_key_algorithm( &attributes, alg );
1022 psa_set_key_type( &attributes, key_type );
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001023
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001024 if( ( status = psa_import_key( &attributes,
1025 key_enc,
1026 PSA_BITS_TO_BYTES( key_bits ),
1027 &transform->psa_key_enc ) ) != PSA_SUCCESS )
1028 {
1029 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1030 return( psa_ssl_status_to_mbedtls( status ) );
1031 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001032
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1034
1035 if( ( status = psa_import_key( &attributes,
1036 key_dec,
1037 PSA_BITS_TO_BYTES( key_bits ),
1038 &transform->psa_key_dec ) ) != PSA_SUCCESS )
1039 {
1040 MBEDTLS_SSL_DEBUG_RET( 1, "psa_import_key", psa_ssl_status_to_mbedtls( status ) );
1041 return( psa_ssl_status_to_mbedtls( status ) );
1042 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001043 }
1044#endif /* MBEDTLS_USE_PSA_CRYPTO */
1045
Hanno Beckerc94060c2021-03-22 07:50:44 +00001046 return( 0 );
1047}
1048
Xiaofei Bai746f9482021-11-12 08:53:56 +00001049int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl )
Jerry Yu89ea3212021-09-09 14:31:24 +08001050{
Jerry Yue3131ef2021-09-16 13:14:15 +08001051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001052 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001053 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001054
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001055 if( handshake->ciphersuite_info == NULL )
Jerry Yu89ea3212021-09-09 14:31:24 +08001056 {
1057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) );
1058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1059 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001060
Gabor Mezei07732f72022-03-26 17:04:19 +01001061 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
Jerry Yue06f4532021-09-23 18:35:07 +08001062
Gabor Mezei07732f72022-03-26 17:04:19 +01001063 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg, NULL, NULL, 0,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001064 handshake->tls13_master_secrets.early );
Jerry Yu89ea3212021-09-09 14:31:24 +08001065 if( ret != 0 )
1066 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yu89ea3212021-09-09 14:31:24 +08001068 return( ret );
1069 }
1070
1071 return( 0 );
1072}
1073
Neil Armstrongb818e162022-05-17 09:24:52 +02001074static int mbedtls_ssl_tls13_get_cipher_key_info(
1075 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1076 size_t *key_len, size_t *iv_len )
1077{
1078 psa_key_type_t key_type;
1079 psa_algorithm_t alg;
1080 size_t taglen;
1081 size_t key_bits;
1082 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1083
1084 if( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG )
1085 taglen = 8;
1086 else
1087 taglen = 16;
1088
1089 status = mbedtls_ssl_cipher_to_psa( ciphersuite_info->cipher, taglen,
1090 &alg, &key_type, &key_bits );
1091 if( status != PSA_SUCCESS )
1092 return psa_ssl_status_to_mbedtls( status );
1093
1094 *key_len = PSA_BITS_TO_BYTES( key_bits );
1095
Neil Armstrong0fa8ce32022-05-17 14:42:57 +02001096 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1097 *iv_len = 12;
Neil Armstrongb818e162022-05-17 09:24:52 +02001098
1099 return 0;
1100}
1101
Jerry Yuc068b662021-10-11 22:30:19 +08001102/* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
Jerry Yu61e35e02021-09-16 18:59:08 +08001103 * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
Jerry Yuc068b662021-10-11 22:30:19 +08001104int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
1105 mbedtls_ssl_key_set *traffic_keys )
Jerry Yu61e35e02021-09-16 18:59:08 +08001106{
1107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1108
1109 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001110
1111 psa_algorithm_t hash_alg;
1112 size_t hash_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001113
Jerry Yu435208a2021-10-13 11:22:16 +08001114 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001115 size_t transcript_len;
1116
Xiaofei Baib7972842021-11-18 07:29:56 +00001117 size_t key_len, iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001118
Jerry Yu435208a2021-10-13 11:22:16 +08001119 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1120 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001121 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001122
Jerry Yuc068b662021-10-11 22:30:19 +08001123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001124
Neil Armstrongb818e162022-05-17 09:24:52 +02001125 ret = mbedtls_ssl_tls13_get_cipher_key_info( ciphersuite_info,
1126 &key_len, &iv_len );
1127 if( ret != 0 )
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001128 {
Neil Armstrongb818e162022-05-17 09:24:52 +02001129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_get_cipher_key_info", ret );
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001130 return ret;
1131 }
1132
Jerry Yu435208a2021-10-13 11:22:16 +08001133 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001134
1135 hash_alg = mbedtls_psa_translate_md( ciphersuite_info->mac );
1136 hash_len = PSA_HASH_LENGTH( hash_alg );
Jerry Yu61e35e02021-09-16 18:59:08 +08001137
1138 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1139 transcript,
1140 sizeof( transcript ),
1141 &transcript_len );
1142 if( ret != 0 )
1143 {
1144 MBEDTLS_SSL_DEBUG_RET( 1,
1145 "mbedtls_ssl_get_handshake_transcript",
1146 ret );
1147 return( ret );
1148 }
1149
Gabor Mezei07732f72022-03-26 17:04:19 +01001150 ret = mbedtls_ssl_tls13_derive_handshake_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001151 handshake->tls13_master_secrets.handshake,
Jerry Yu435208a2021-10-13 11:22:16 +08001152 transcript, transcript_len, tls13_hs_secrets );
Jerry Yu61e35e02021-09-16 18:59:08 +08001153 if( ret != 0 )
1154 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_handshake_secrets",
Jerry Yu61e35e02021-09-16 18:59:08 +08001156 ret );
1157 return( ret );
1158 }
1159
1160 MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001161 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001162 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001163 MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret",
Jerry Yu435208a2021-10-13 11:22:16 +08001164 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001165 hash_len );
Jerry Yu61e35e02021-09-16 18:59:08 +08001166
1167 /*
1168 * Export client handshake traffic secret
1169 */
Jerry Yu61e35e02021-09-16 18:59:08 +08001170 if( ssl->f_export_keys != NULL )
1171 {
1172 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001173 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001174 tls13_hs_secrets->client_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001175 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001176 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001177 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001178 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1179
1180 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001181 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
Jerry Yu435208a2021-10-13 11:22:16 +08001182 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001183 hash_len,
Jerry Yu435208a2021-10-13 11:22:16 +08001184 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001185 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Jerry Yu61e35e02021-09-16 18:59:08 +08001186 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
1187 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001188
Gabor Mezei07732f72022-03-26 17:04:19 +01001189 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
Jerry Yu435208a2021-10-13 11:22:16 +08001190 tls13_hs_secrets->client_handshake_traffic_secret,
1191 tls13_hs_secrets->server_handshake_traffic_secret,
Gabor Mezei07732f72022-03-26 17:04:19 +01001192 hash_len, key_len, iv_len, traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +08001193 if( ret != 0 )
1194 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
Jerry Yu61e35e02021-09-16 18:59:08 +08001196 goto exit;
1197 }
1198
1199 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key",
1200 traffic_keys->client_write_key,
1201 traffic_keys->key_len);
1202
1203 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key",
1204 traffic_keys->server_write_key,
1205 traffic_keys->key_len);
1206
1207 MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv",
1208 traffic_keys->client_write_iv,
1209 traffic_keys->iv_len);
1210
1211 MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv",
1212 traffic_keys->server_write_iv,
1213 traffic_keys->iv_len);
1214
Jerry Yuc068b662021-10-11 22:30:19 +08001215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) );
Jerry Yu61e35e02021-09-16 18:59:08 +08001216
1217exit:
1218
1219 return( ret );
1220}
1221
Jerry Yuf0ac2352021-10-11 17:47:07 +08001222int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl )
Jerry Yua0650eb2021-09-09 17:14:45 +08001223{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001225#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED) && defined(MBEDTLS_ECDH_C)
1226 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1227#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED && MBEDTLS_ECDH_C */
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001228 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Gabor Mezei07732f72022-03-26 17:04:19 +01001229 psa_algorithm_t const hash_alg = mbedtls_psa_translate_md(
1230 handshake->ciphersuite_info->mac );
Jerry Yua0650eb2021-09-09 17:14:45 +08001231
Jerry Yuf0ac2352021-10-11 17:47:07 +08001232#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
1233 /*
1234 * Compute ECDHE secret used to compute the handshake secret from which
1235 * client_handshake_traffic_secret and server_handshake_traffic_secret
1236 * are derived in the handshake secret derivation stage.
1237 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001238 if( mbedtls_ssl_tls13_ephemeral_enabled( ssl ) )
Jerry Yuf0ac2352021-10-11 17:47:07 +08001239 {
1240 if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) )
1241 {
1242#if defined(MBEDTLS_ECDH_C)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001243 /* Compute ECDH shared secret. */
1244 status = psa_raw_key_agreement(
1245 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1246 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1247 handshake->premaster, sizeof( handshake->premaster ),
1248 &handshake->pmslen );
1249 if( status != PSA_SUCCESS )
1250 {
1251 ret = psa_ssl_status_to_mbedtls( status );
1252 MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret );
1253 return( ret );
1254 }
1255
1256 status = psa_destroy_key( handshake->ecdh_psa_privkey );
1257 if( status != PSA_SUCCESS )
1258 {
1259 ret = psa_ssl_status_to_mbedtls( status );
1260 MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret );
1261 return( ret );
1262 }
1263
1264 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001265#endif /* MBEDTLS_ECDH_C */
1266 }
1267 else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) )
1268 {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) );
1270 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1271 }
1272 }
1273#else
1274 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
1275#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001276
1277 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001278 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001279 */
Gabor Mezei07732f72022-03-26 17:04:19 +01001280 ret = mbedtls_ssl_tls13_evolve_secret( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001281 handshake->tls13_master_secrets.early,
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001282 handshake->premaster, handshake->pmslen,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001283 handshake->tls13_master_secrets.handshake );
Jerry Yua0650eb2021-09-09 17:14:45 +08001284 if( ret != 0 )
1285 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_evolve_secret", ret );
Jerry Yua0650eb2021-09-09 17:14:45 +08001287 return( ret );
1288 }
1289
1290 MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret",
Gabor Mezei07732f72022-03-26 17:04:19 +01001291 handshake->tls13_master_secrets.handshake,
1292 PSA_HASH_LENGTH( hash_alg ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001293
1294#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001295 mbedtls_platform_zeroize( handshake->premaster, sizeof( handshake->premaster ) );
Jerry Yua0650eb2021-09-09 17:14:45 +08001296#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
1297 return( 0 );
1298}
1299
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001300/* Generate application traffic keys since any records following a 1-RTT Finished message
1301 * MUST be encrypted under the application traffic key.
1302 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001303int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001304 mbedtls_ssl_context *ssl,
1305 mbedtls_ssl_key_set *traffic_keys )
1306{
XiaokangQiana7634982021-10-22 06:32:32 +00001307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001308 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001309
1310 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001311 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001312 &ssl->session_negotiate->app_secrets;
1313
1314 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001315 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001316 size_t transcript_len;
1317
1318 /* Variables relating to the hash for the chosen ciphersuite. */
1319 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001320
1321 psa_algorithm_t hash_alg;
1322 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001323
1324 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001325 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001326
1327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) );
1328
1329 /* Extract basic information about hash and ciphersuite */
1330
Neil Armstrongb818e162022-05-17 09:24:52 +02001331 ret = mbedtls_ssl_tls13_get_cipher_key_info( handshake->ciphersuite_info,
1332 &key_len, &iv_len );
1333 if( ret != 0 )
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001334 {
Neil Armstrongb818e162022-05-17 09:24:52 +02001335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_get_cipher_key_info", ret );
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001336 goto cleanup;
1337 }
1338
XiaokangQian33062842021-11-11 03:37:45 +00001339 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001340
1341 hash_alg = mbedtls_psa_translate_md( handshake->ciphersuite_info->mac );
1342 hash_len = PSA_HASH_LENGTH( hash_alg );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001343
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001344 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001345 * to call this at the right time, that is, after the ServerFinished. */
1346
1347 ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
1348 transcript, sizeof( transcript ),
1349 &transcript_len );
1350 if( ret != 0 )
XiaokangQian4cab0242021-10-12 08:43:37 +00001351 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001352
1353 /* Compute application secrets from master secret and transcript hash. */
1354
Gabor Mezei07732f72022-03-26 17:04:19 +01001355 ret = mbedtls_ssl_tls13_derive_application_secrets( hash_alg,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001356 handshake->tls13_master_secrets.app,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001357 transcript, transcript_len,
1358 app_secrets );
1359 if( ret != 0 )
1360 {
1361 MBEDTLS_SSL_DEBUG_RET( 1,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001362 "mbedtls_ssl_tls13_derive_application_secrets", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001363 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001364 }
1365
1366 /* Derive first epoch of IV + Key for application traffic. */
1367
Gabor Mezei07732f72022-03-26 17:04:19 +01001368 ret = mbedtls_ssl_tls13_make_traffic_keys( hash_alg,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001369 app_secrets->client_application_traffic_secret_N,
1370 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001371 hash_len, key_len, iv_len, traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001372 if( ret != 0 )
1373 {
Xiaofei Bai746f9482021-11-12 08:53:56 +00001374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_make_traffic_keys", ret );
XiaokangQian4cab0242021-10-12 08:43:37 +00001375 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001376 }
1377
1378 MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret",
1379 app_secrets->client_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001380 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001381
1382 MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret",
1383 app_secrets->server_application_traffic_secret_N,
Gabor Mezei07732f72022-03-26 17:04:19 +01001384 hash_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001385
XiaokangQianac0385c2021-11-03 06:40:11 +00001386 /*
1387 * Export client/server application traffic secret 0
1388 */
1389 if( ssl->f_export_keys != NULL )
1390 {
1391 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001392 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001393 app_secrets->client_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001394 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001395 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001396 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1397 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001398
1399 ssl->f_export_keys( ssl->p_export_keys,
Xiaofei Bai746f9482021-11-12 08:53:56 +00001400 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
Gabor Mezei07732f72022-03-26 17:04:19 +01001401 app_secrets->server_application_traffic_secret_N, hash_len,
XiaokangQian33062842021-11-11 03:37:45 +00001402 handshake->randbytes,
lhuang04a3890a32022-01-04 09:47:20 -08001403 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
XiaokangQianb51f8842021-11-04 03:02:47 +00001404 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1405 a new constant for TLS 1.3! */ );
XiaokangQianac0385c2021-11-03 06:40:11 +00001406 }
1407
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001408 MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:",
Xiaofei Baib7972842021-11-18 07:29:56 +00001409 traffic_keys->client_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001410 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key",
Xiaofei Baib7972842021-11-18 07:29:56 +00001411 traffic_keys->server_write_key, key_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001412 MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001413 traffic_keys->client_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001414 MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV",
Xiaofei Baib7972842021-11-18 07:29:56 +00001415 traffic_keys->server_write_iv, iv_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001416
1417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001418
1419 cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001420 /* randbytes is not used again */
1421 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1422 sizeof( ssl->handshake->randbytes ) );
Jerry Yuef2b98a2022-05-06 16:40:05 +08001423
XiaokangQian33062842021-11-11 03:37:45 +00001424 mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
XiaokangQian4cab0242021-10-12 08:43:37 +00001425 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001426}
1427
Jerry Yuf86eb752022-05-06 11:16:55 +08001428int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl )
Jerry Yue110d252022-05-05 10:19:22 +08001429{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001431 mbedtls_ssl_key_set traffic_keys;
1432 mbedtls_ssl_transform *transform_handshake = NULL;
1433 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1434
1435 /* Compute handshake secret */
1436 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
1437 if( ret != 0 )
1438 {
1439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_derive_master_secret", ret );
1440 goto cleanup;
1441 }
1442
1443 /* Next evolution in key schedule: Establish handshake secret and
1444 * key material. */
1445 ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
1446 if( ret != 0 )
1447 {
1448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
1449 ret );
1450 goto cleanup;
1451 }
1452
1453 transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1454 if( transform_handshake == NULL )
1455 {
1456 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1457 goto cleanup;
1458 }
1459
Jerry Yuef2b98a2022-05-06 16:40:05 +08001460 ret = mbedtls_ssl_tls13_populate_transform(
1461 transform_handshake,
1462 ssl->conf->endpoint,
1463 ssl->session_negotiate->ciphersuite,
1464 &traffic_keys,
1465 ssl );
Jerry Yue110d252022-05-05 10:19:22 +08001466 if( ret != 0 )
1467 {
1468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
1469 goto cleanup;
1470 }
1471 handshake->transform_handshake = transform_handshake;
1472
1473cleanup:
1474 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
1475 if( ret != 0 )
Jerry Yue110d252022-05-05 10:19:22 +08001476 mbedtls_free( transform_handshake );
Jerry Yue110d252022-05-05 10:19:22 +08001477
1478 return( ret );
1479}
1480
Jerry Yuff226982022-04-16 16:52:57 +08001481int mbedtls_ssl_tls13_generate_resumption_master_secret(
1482 mbedtls_ssl_context *ssl )
1483{
1484 /* Erase master secrets */
1485 mbedtls_platform_zeroize( &ssl->handshake->tls13_master_secrets,
1486 sizeof( ssl->handshake->tls13_master_secrets ) );
1487 return( 0 );
1488}
1489
Jerry Yufd5ea042022-05-19 14:29:48 +08001490int mbedtls_ssl_tls13_compute_application_transform( mbedtls_ssl_context *ssl )
1491{
1492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1493 mbedtls_ssl_key_set traffic_keys;
1494 mbedtls_ssl_transform *transform_application = NULL;
1495
1496 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
1497 if( ret != 0 )
1498 {
1499 MBEDTLS_SSL_DEBUG_RET( 1,
1500 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
1501 goto cleanup;
1502 }
1503
1504 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
1505 if( ret != 0 )
1506 {
1507 MBEDTLS_SSL_DEBUG_RET( 1,
1508 "mbedtls_ssl_tls13_generate_application_keys", ret );
1509 goto cleanup;
1510 }
1511
1512 transform_application =
1513 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1514 if( transform_application == NULL )
1515 {
1516 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1517 goto cleanup;
1518 }
1519
1520 ret = mbedtls_ssl_tls13_populate_transform(
1521 transform_application,
1522 ssl->conf->endpoint,
1523 ssl->session_negotiate->ciphersuite,
1524 &traffic_keys,
1525 ssl );
1526 if( ret != 0 )
1527 {
1528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
1529 goto cleanup;
1530 }
1531
1532 ssl->transform_application = transform_application;
1533
1534cleanup:
1535
1536 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
1537 if( ret != 0 )
1538 {
1539 mbedtls_free( transform_application );
1540 }
1541 return( ret );
1542}
1543
Ronald Cron6f135e12021-12-08 16:57:54 +01001544#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */