blob: ecfdab318d36c26eb51195435c15be4372d13058 [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
Gilles Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN(label_len, context_len) \
88 (2 /* expansion length */ \
89 + 1 /* label length */ \
90 + label_len \
91 + 1 /* context length */ \
92 + context_len)
Hanno Becker9cb0a142020-09-08 10:48:14 +010093
94#define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
95 SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
Gilles Peskine449bd832023-01-11 14:50:10 +010096 sizeof(tls13_label_prefix) + \
97 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(
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 size_t desired_length,
102 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 =
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 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 \
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
Hanno Becker531fe302020-09-16 09:45:27 +0100119#endif
120
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100121 *p++ = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 *p++ = MBEDTLS_BYTE_0(desired_length);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100123
124 /* Add label incl. prefix */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 *p++ = MBEDTLS_BYTE_0(total_label_len);
126 memcpy(p, tls13_label_prefix, sizeof(tls13_label_prefix));
Xiaofei Baid25fab62021-12-02 06:36:27 +0000127 p += sizeof(tls13_label_prefix);
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 memcpy(p, label, label_len);
Xiaofei Baib7972842021-11-18 07:29:56 +0000129 p += label_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100130
131 /* Add context value */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 *p++ = MBEDTLS_BYTE_0(ctx_len);
133 if (ctx_len != 0) {
134 memcpy(p, ctx, ctx_len);
135 }
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100136
137 /* Return total length to the caller. */
Xiaofei Baib7972842021-11-18 07:29:56 +0000138 *dst_len = total_hkdf_lbl_len;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100139}
140
Xiaofei Bai746f9482021-11-12 08:53:56 +0000141int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 psa_algorithm_t hash_alg,
143 const unsigned char *secret, size_t secret_len,
144 const unsigned char *label, size_t label_len,
145 const unsigned char *ctx, size_t ctx_len,
146 unsigned char *buf, size_t buf_len)
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100147{
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 unsigned char hkdf_label[SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN];
Przemek Stekiel1b0ebdf2022-06-23 09:22:49 +0200149 size_t hkdf_label_len = 0;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200150 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
151 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekield5ae3652022-05-13 12:10:08 +0200152 psa_key_derivation_operation_t operation =
153 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (label_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100156 /* Should never happen since this is an internal
157 * function, and we know statically which labels
158 * are allowed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (ctx_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100163 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (buf_len > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN) {
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100168 /* Should not happen, as above. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100170 }
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (!PSA_ALG_IS_HASH(hash_alg)) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
174 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 ssl_tls13_hkdf_encode_label(buf_len,
177 label, label_len,
178 ctx, ctx_len,
179 hkdf_label,
180 &hkdf_label_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF_EXPAND(hash_alg));
Przemek Stekield5ae3652022-05-13 12:10:08 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (status != PSA_SUCCESS) {
185 goto cleanup;
186 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 status = psa_key_derivation_input_bytes(&operation,
189 PSA_KEY_DERIVATION_INPUT_SECRET,
190 secret,
191 secret_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (status != PSA_SUCCESS) {
194 goto cleanup;
195 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 status = psa_key_derivation_input_bytes(&operation,
198 PSA_KEY_DERIVATION_INPUT_INFO,
199 hkdf_label,
200 hkdf_label_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (status != PSA_SUCCESS) {
203 goto cleanup;
204 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 status = psa_key_derivation_output_bytes(&operation,
207 buf,
208 buf_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (status != PSA_SUCCESS) {
211 goto cleanup;
212 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200213
214cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 abort_status = psa_key_derivation_abort(&operation);
216 status = (status == PSA_SUCCESS ? abort_status : status);
217 mbedtls_platform_zeroize(hkdf_label, hkdf_label_len);
218 return psa_ssl_status_to_mbedtls(status);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100219}
220
Jerry Yua5db6c02022-11-23 18:08:04 +0800221MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yua8771832022-11-21 23:16:54 +0800222static int ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 psa_algorithm_t hash_alg,
224 const unsigned char *secret, size_t secret_len,
225 unsigned char *key, size_t key_len,
226 unsigned char *iv, size_t iv_len)
Jerry Yua8771832022-11-21 23:16:54 +0800227{
228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
229
Jerry Yuaec08b32022-11-29 15:19:27 +0800230 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 hash_alg,
232 secret, secret_len,
233 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(key),
234 NULL, 0,
235 key, key_len);
236 if (ret != 0) {
237 return ret;
238 }
Jerry Yua8771832022-11-21 23:16:54 +0800239
Jerry Yuaec08b32022-11-29 15:19:27 +0800240 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 hash_alg,
242 secret, secret_len,
243 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(iv),
244 NULL, 0,
245 iv, iv_len);
246 return ret;
Jerry Yua8771832022-11-21 23:16:54 +0800247}
248
Hanno Becker3385a4d2020-08-21 13:03:34 +0100249/*
250 * The traffic keying material is generated from the following inputs:
251 *
252 * - One secret value per sender.
253 * - A purpose value indicating the specific value being generated
254 * - The desired lengths of key and IV.
255 *
256 * The expansion itself is based on HKDF:
257 *
258 * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
259 * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
260 *
261 * [sender] denotes the sending side and the Secret value is provided
262 * by the function caller. Note that we generate server and client side
263 * keys in a single function call.
264 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000265int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 psa_algorithm_t hash_alg,
267 const unsigned char *client_secret,
268 const unsigned char *server_secret, size_t secret_len,
269 size_t key_len, size_t iv_len,
270 mbedtls_ssl_key_set *keys)
Hanno Becker3385a4d2020-08-21 13:03:34 +0100271{
272 int ret = 0;
273
Jerry Yua8771832022-11-21 23:16:54 +0800274 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 hash_alg, client_secret, secret_len,
276 keys->client_write_key, key_len,
277 keys->client_write_iv, iv_len);
278 if (ret != 0) {
279 return ret;
280 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100281
Jerry Yua8771832022-11-21 23:16:54 +0800282 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 hash_alg, server_secret, secret_len,
284 keys->server_write_key, key_len,
285 keys->server_write_iv, iv_len);
286 if (ret != 0) {
287 return ret;
288 }
Hanno Becker3385a4d2020-08-21 13:03:34 +0100289
Hanno Becker493ea7f2020-09-08 11:01:00 +0100290 keys->key_len = key_len;
291 keys->iv_len = iv_len;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return 0;
Hanno Becker3385a4d2020-08-21 13:03:34 +0100294}
295
Xiaofei Bai746f9482021-11-12 08:53:56 +0000296int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 psa_algorithm_t hash_alg,
298 const unsigned char *secret, size_t secret_len,
299 const unsigned char *label, size_t label_len,
300 const unsigned char *ctx, size_t ctx_len,
301 int ctx_hashed,
302 unsigned char *dstbuf, size_t dstbuf_len)
Hanno Beckerb35d5222020-08-21 13:27:44 +0100303{
304 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 unsigned char hashed_context[PSA_HASH_MAX_SIZE];
306 if (ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED) {
Gabor Mezei07732f72022-03-26 17:04:19 +0100307 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 status = psa_hash_compute(hash_alg, ctx, ctx_len, hashed_context,
310 PSA_HASH_LENGTH(hash_alg), &ctx_len);
311 if (status != PSA_SUCCESS) {
312 ret = psa_ssl_status_to_mbedtls(status);
Gabor Mezei07732f72022-03-26 17:04:19 +0100313 return ret;
314 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 } else {
316 if (ctx_len > sizeof(hashed_context)) {
Hanno Becker97a21562020-09-09 12:57:16 +0100317 /* This should never happen since this function is internal
Hanno Becker0c42fd92020-09-09 12:58:29 +0100318 * and the code sets `ctx_hashed` correctly.
Hanno Becker97a21562020-09-09 12:57:16 +0100319 * Let's double-check nonetheless to not run at the risk
320 * of getting a stack overflow. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker97a21562020-09-09 12:57:16 +0100322 }
Hanno Beckerb35d5222020-08-21 13:27:44 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 memcpy(hashed_context, ctx, ctx_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100325 }
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return mbedtls_ssl_tls13_hkdf_expand_label(hash_alg,
328 secret, secret_len,
329 label, label_len,
330 hashed_context, ctx_len,
331 dstbuf, dstbuf_len);
Gabor Mezei07732f72022-03-26 17:04:19 +0100332
Hanno Beckerb35d5222020-08-21 13:27:44 +0100333}
334
Xiaofei Bai746f9482021-11-12 08:53:56 +0000335int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 psa_algorithm_t hash_alg,
337 const unsigned char *secret_old,
338 const unsigned char *input, size_t input_len,
339 unsigned char *secret_new)
Hanno Beckere9cccb42020-08-20 13:42:46 +0100340{
341 int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
343 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron831fee62022-10-05 16:22:59 +0200344 size_t hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 unsigned char tmp_secret[PSA_MAC_MAX_SIZE] = { 0 };
346 const unsigned char all_zeroes_input[MBEDTLS_TLS1_3_MD_MAX_SIZE] = { 0 };
Ronald Cron831fee62022-10-05 16:22:59 +0200347 const unsigned char *l_input = NULL;
348 size_t l_input_len;
349
Przemek Stekield5ae3652022-05-13 12:10:08 +0200350 psa_key_derivation_operation_t operation =
351 PSA_KEY_DERIVATION_OPERATION_INIT;
Gabor Mezei07732f72022-03-26 17:04:19 +0100352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (!PSA_ALG_IS_HASH(hash_alg)) {
354 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
355 }
Gabor Mezei58db6532022-03-21 12:12:37 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 hlen = PSA_HASH_LENGTH(hash_alg);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100358
359 /* For non-initial runs, call Derive-Secret( ., "derived", "")
Hanno Becker61baae72020-09-16 09:24:14 +0100360 * on the old secret. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (secret_old != NULL) {
Xiaofei Bai746f9482021-11-12 08:53:56 +0000362 ret = mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 hash_alg,
364 secret_old, hlen,
365 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(derived),
366 NULL, 0, /* context */
367 MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
368 tmp_secret, hlen);
369 if (ret != 0) {
Hanno Beckere9cccb42020-08-20 13:42:46 +0100370 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 }
Hanno Beckere9cccb42020-08-20 13:42:46 +0100372 }
373
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200374 ret = 0;
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (input != NULL && input_len != 0) {
Ronald Cron831fee62022-10-05 16:22:59 +0200377 l_input = input;
378 l_input_len = input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 } else {
Ronald Cron831fee62022-10-05 16:22:59 +0200380 l_input = all_zeroes_input;
381 l_input_len = hlen;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100382 }
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 status = psa_key_derivation_setup(&operation,
385 PSA_ALG_HKDF_EXTRACT(hash_alg));
Hanno Beckere9cccb42020-08-20 13:42:46 +0100386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (status != PSA_SUCCESS) {
388 goto cleanup;
389 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 status = psa_key_derivation_input_bytes(&operation,
392 PSA_KEY_DERIVATION_INPUT_SALT,
393 tmp_secret,
394 hlen);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (status != PSA_SUCCESS) {
397 goto cleanup;
398 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 status = psa_key_derivation_input_bytes(&operation,
401 PSA_KEY_DERIVATION_INPUT_SECRET,
402 l_input, l_input_len);
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (status != PSA_SUCCESS) {
405 goto cleanup;
406 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 status = psa_key_derivation_output_bytes(&operation,
409 secret_new,
410 PSA_HASH_LENGTH(hash_alg));
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (status != PSA_SUCCESS) {
413 goto cleanup;
414 }
Przemek Stekiel38ab4002022-06-23 09:05:40 +0200415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416cleanup:
417 abort_status = psa_key_derivation_abort(&operation);
418 status = (status == PSA_SUCCESS ? abort_status : status);
419 ret = (ret == 0 ? psa_ssl_status_to_mbedtls(status) : ret);
420 mbedtls_platform_zeroize(tmp_secret, sizeof(tmp_secret));
421 return ret;
Hanno Beckere9cccb42020-08-20 13:42:46 +0100422}
423
Xiaofei Bai746f9482021-11-12 08:53:56 +0000424int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 psa_algorithm_t hash_alg,
426 unsigned char const *early_secret,
427 unsigned char const *transcript, size_t transcript_len,
428 mbedtls_ssl_tls13_early_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100429{
430 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100432
433 /* We should never call this function with an unknown hash,
434 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (!PSA_ALG_IS_HASH(hash_alg)) {
436 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
437 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100438
439 /*
440 * 0
441 * |
442 * v
443 * PSK -> HKDF-Extract = Early Secret
444 * |
Hanno Beckeref5235b2021-05-24 06:39:41 +0100445 * +-----> Derive-Secret(., "c e traffic", ClientHello)
446 * | = client_early_traffic_secret
447 * |
448 * +-----> Derive-Secret(., "e exp master", ClientHello)
449 * | = early_exporter_master_secret
450 * v
451 */
452
453 /* Create client_early_traffic_secret */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
455 early_secret, hash_len,
456 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_e_traffic),
457 transcript, transcript_len,
458 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
459 derived->client_early_traffic_secret,
460 hash_len);
461 if (ret != 0) {
462 return ret;
463 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100464
465 /* Create early exporter */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
467 early_secret, hash_len,
468 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(e_exp_master),
469 transcript, transcript_len,
470 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
471 derived->early_exporter_master_secret,
472 hash_len);
473 if (ret != 0) {
474 return ret;
475 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100478}
479
Xiaofei Bai746f9482021-11-12 08:53:56 +0000480int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 psa_algorithm_t hash_alg,
482 unsigned char const *handshake_secret,
483 unsigned char const *transcript, size_t transcript_len,
484 mbedtls_ssl_tls13_handshake_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100485{
486 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100488
489 /* We should never call this function with an unknown hash,
490 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (!PSA_ALG_IS_HASH(hash_alg)) {
492 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
493 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100494
495 /*
496 *
497 * Handshake Secret
498 * |
499 * +-----> Derive-Secret( ., "c hs traffic",
500 * | ClientHello...ServerHello )
501 * | = client_handshake_traffic_secret
502 * |
503 * +-----> Derive-Secret( ., "s hs traffic",
504 * | ClientHello...ServerHello )
505 * | = server_handshake_traffic_secret
506 *
507 */
508
509 /*
510 * Compute client_handshake_traffic_secret with
511 * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
512 */
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
515 handshake_secret, hash_len,
516 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_hs_traffic),
517 transcript, transcript_len,
518 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
519 derived->client_handshake_traffic_secret,
520 hash_len);
521 if (ret != 0) {
522 return ret;
523 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100524
525 /*
526 * Compute server_handshake_traffic_secret with
527 * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
528 */
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
531 handshake_secret, hash_len,
532 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_hs_traffic),
533 transcript, transcript_len,
534 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
535 derived->server_handshake_traffic_secret,
536 hash_len);
537 if (ret != 0) {
538 return ret;
539 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100542}
543
Xiaofei Bai746f9482021-11-12 08:53:56 +0000544int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 psa_algorithm_t hash_alg,
546 unsigned char const *application_secret,
547 unsigned char const *transcript, size_t transcript_len,
548 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100549{
550 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100552
553 /* We should never call this function with an unknown hash,
554 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (!PSA_ALG_IS_HASH(hash_alg)) {
556 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
557 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100558
559 /* Generate {client,server}_application_traffic_secret_0
560 *
561 * Master Secret
562 * |
563 * +-----> Derive-Secret( ., "c ap traffic",
564 * | ClientHello...server Finished )
565 * | = client_application_traffic_secret_0
566 * |
567 * +-----> Derive-Secret( ., "s ap traffic",
568 * | ClientHello...Server Finished )
569 * | = server_application_traffic_secret_0
570 * |
571 * +-----> Derive-Secret( ., "exp master",
572 * | ClientHello...server Finished)
573 * | = exporter_master_secret
574 *
575 */
576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
578 application_secret, hash_len,
579 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(c_ap_traffic),
580 transcript, transcript_len,
581 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
582 derived->client_application_traffic_secret_N,
583 hash_len);
584 if (ret != 0) {
585 return ret;
586 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
589 application_secret, hash_len,
590 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(s_ap_traffic),
591 transcript, transcript_len,
592 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
593 derived->server_application_traffic_secret_N,
594 hash_len);
595 if (ret != 0) {
596 return ret;
597 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
600 application_secret, hash_len,
601 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(exp_master),
602 transcript, transcript_len,
603 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
604 derived->exporter_master_secret,
605 hash_len);
606 if (ret != 0) {
607 return ret;
608 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100611}
612
613/* Generate resumption_master_secret for use with the ticket exchange.
614 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000615 * This is not integrated with mbedtls_ssl_tls13_derive_application_secrets()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100616 * because it uses the transcript hash up to and including ClientFinished. */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000617int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 psa_algorithm_t hash_alg,
619 unsigned char const *application_secret,
620 unsigned char const *transcript, size_t transcript_len,
621 mbedtls_ssl_tls13_application_secrets *derived)
Hanno Beckeref5235b2021-05-24 06:39:41 +0100622{
623 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100625
626 /* We should never call this function with an unknown hash,
627 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (!PSA_ALG_IS_HASH(hash_alg)) {
629 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
630 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
633 application_secret, hash_len,
634 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_master),
635 transcript, transcript_len,
636 MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
637 derived->resumption_master_secret,
638 hash_len);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (ret != 0) {
641 return ret;
642 }
Hanno Beckeref5235b2021-05-24 06:39:41 +0100643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 return 0;
Hanno Beckeref5235b2021-05-24 06:39:41 +0100645}
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647int mbedtls_ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000648{
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +0000650 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200651 psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 handshake->ciphersuite_info->mac);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000653
654 /*
655 * Compute MasterSecret
656 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
658 handshake->tls13_master_secrets.handshake,
659 NULL, 0,
660 handshake->tls13_master_secrets.app);
661 if (ret != 0) {
662 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
663 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000664 }
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 MBEDTLS_SSL_DEBUG_BUF(4, "Master secret",
667 handshake->tls13_master_secrets.app, PSA_HASH_LENGTH(hash_alg));
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000670}
671
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200672MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100673static int ssl_tls13_calc_finished_core(psa_algorithm_t hash_alg,
674 unsigned char const *base_key,
675 unsigned char const *transcript,
676 unsigned char *dst,
677 size_t *dst_len)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100678{
Gabor Mezei07732f72022-03-26 17:04:19 +0100679 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
681 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 size_t hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100683 unsigned char finished_key[PSA_MAC_MAX_SIZE];
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100684 int ret;
Gabor Mezei07732f72022-03-26 17:04:19 +0100685 psa_algorithm_t alg;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100686
687 /* We should never call this function with an unknown hash,
688 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (!PSA_ALG_IS_HASH(hash_alg)) {
690 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
691 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100692
693 /* TLS 1.3 Finished message
694 *
695 * struct {
696 * opaque verify_data[Hash.length];
697 * } Finished;
698 *
699 * verify_data =
700 * HMAC( finished_key,
701 * Hash( Handshake Context +
702 * Certificate* +
703 * CertificateVerify* )
704 * )
705 *
706 * finished_key =
707 * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
708 */
709
Xiaofei Bai746f9482021-11-12 08:53:56 +0000710 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 hash_alg, base_key, hash_len,
712 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(finished),
713 NULL, 0,
714 finished_key, hash_len);
715 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100716 goto exit;
Gabor Mezei07732f72022-03-26 17:04:19 +0100717 }
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 alg = PSA_ALG_HMAC(hash_alg);
720 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
721 psa_set_key_algorithm(&attributes, alg);
722 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
723
724 status = psa_import_key(&attributes, finished_key, hash_len, &key);
725 if (status != PSA_SUCCESS) {
726 ret = psa_ssl_status_to_mbedtls(status);
727 goto exit;
728 }
729
730 status = psa_mac_compute(key, alg, transcript, hash_len,
731 dst, hash_len, dst_len);
732 ret = psa_ssl_status_to_mbedtls(status);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100733
734exit:
735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 status = psa_destroy_key(key);
737 if (ret == 0) {
738 ret = psa_ssl_status_to_mbedtls(status);
739 }
Gabor Mezei07732f72022-03-26 17:04:19 +0100740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 mbedtls_platform_zeroize(finished_key, sizeof(finished_key));
Gabor Mezei07732f72022-03-26 17:04:19 +0100742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100744}
745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
747 unsigned char *dst,
748 size_t dst_len,
749 size_t *actual_len,
750 int from)
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000751{
XiaokangQiana7634982021-10-22 06:32:32 +0000752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000753
XiaokangQianaaa0e192021-11-10 03:07:04 +0000754 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000755 size_t transcript_len;
756
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800757 unsigned char *base_key = NULL;
Jerry Yub737f6a2021-12-10 17:55:23 +0800758 size_t base_key_len = 0;
Jerry Yu9c074732021-12-10 17:12:43 +0800759 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets =
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 &ssl->handshake->tls13_hs_secrets;
Jerry Yua5563f62021-12-10 18:14:36 +0800761
762 mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +0100763
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200764 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 ssl->handshake->ciphersuite_info->mac);
766 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yua5563f62021-12-10 18:14:36 +0800767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_calculate_verify_data"));
Jerry Yua5563f62021-12-10 18:14:36 +0800769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (from == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yub737f6a2021-12-10 17:55:23 +0800771 base_key = tls13_hs_secrets->client_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 base_key_len = sizeof(tls13_hs_secrets->client_handshake_traffic_secret);
773 } else {
Jerry Yub737f6a2021-12-10 17:55:23 +0800774 base_key = tls13_hs_secrets->server_handshake_traffic_secret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 base_key_len = sizeof(tls13_hs_secrets->server_handshake_traffic_secret);
Jerry Yub737f6a2021-12-10 17:55:23 +0800776 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (dst_len < hash_len) {
Jerry Yu9c074732021-12-10 17:12:43 +0800779 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
780 goto exit;
781 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
784 transcript, sizeof(transcript),
785 &transcript_len);
786 if (ret != 0) {
787 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000788 goto exit;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000789 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 MBEDTLS_SSL_DEBUG_BUF(4, "handshake hash", transcript, transcript_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 ret = ssl_tls13_calc_finished_core(hash_alg, base_key, transcript, dst, actual_len);
793 if (ret != 0) {
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000794 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 MBEDTLS_SSL_DEBUG_BUF(3, "verify_data for finished message", dst, hash_len);
798 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_calculate_verify_data"));
XiaokangQian61bdbbc2021-10-28 08:03:38 +0000799
800exit:
Jerry Yu4a2fa5d2021-12-10 10:19:34 +0800801 /* Erase handshake secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 mbedtls_platform_zeroize(base_key, base_key_len);
803 mbedtls_platform_zeroize(transcript, sizeof(transcript));
804 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000805}
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
808 const psa_algorithm_t hash_alg,
809 unsigned char const *psk, size_t psk_len,
810 int psk_type,
811 unsigned char const *transcript,
812 unsigned char *result)
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100813{
814 int ret = 0;
Gabor Mezeied6d6582022-03-26 17:28:06 +0100815 unsigned char binder_key[PSA_MAC_MAX_SIZE];
816 unsigned char early_secret[PSA_MAC_MAX_SIZE];
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 size_t const hash_len = PSA_HASH_LENGTH(hash_alg);
Gabor Mezei07732f72022-03-26 17:04:19 +0100818 size_t actual_len;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100819
Hanno Becker28e5f1e2021-05-26 09:29:49 +0100820#if !defined(MBEDTLS_DEBUG_C)
821 ssl = NULL; /* make sure we don't use it except for debug */
822 ((void) ssl);
823#endif
824
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100825 /* We should never call this function with an unknown hash,
826 * but add an assertion anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (!PSA_ALG_IS_HASH(hash_alg)) {
828 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
829 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100830
831 /*
832 * 0
833 * |
834 * v
835 * PSK -> HKDF-Extract = Early Secret
836 * |
837 * +-----> Derive-Secret(., "ext binder" | "res binder", "")
838 * | = binder_key
839 * v
840 */
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
843 NULL, /* Old secret */
844 psk, psk_len, /* Input */
845 early_secret);
846 if (ret != 0) {
847 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100848 goto exit;
849 }
850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_create_psk_binder",
852 early_secret, hash_len);
Ronald Cron295d93e2022-07-19 08:21:29 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
855 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
856 early_secret, hash_len,
857 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(res_binder),
858 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
859 binder_key, hash_len);
860 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'res binder'"));
861 } else {
862 ret = mbedtls_ssl_tls13_derive_secret(hash_alg,
863 early_secret, hash_len,
864 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(ext_binder),
865 NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
866 binder_key, hash_len);
867 MBEDTLS_SSL_DEBUG_MSG(4, ("Derive Early Secret with 'ext binder'"));
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100868 }
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (ret != 0) {
871 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_secret", ret);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100872 goto exit;
873 }
874
875 /*
876 * The binding_value is computed in the same way as the Finished message
877 * but with the BaseKey being the binder_key.
878 */
879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 ret = ssl_tls13_calc_finished_core(hash_alg, binder_key, transcript,
881 result, &actual_len);
882 if (ret != 0) {
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100883 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 }
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder", result, actual_len);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100887
888exit:
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 mbedtls_platform_zeroize(early_secret, sizeof(early_secret));
891 mbedtls_platform_zeroize(binder_key, sizeof(binder_key));
892 return ret;
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100893}
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
896 int endpoint,
897 int ciphersuite,
898 mbedtls_ssl_key_set const *traffic_keys,
899 mbedtls_ssl_context *ssl /* DEBUG ONLY */)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000900{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100901#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckerc94060c2021-03-22 07:50:44 +0000902 int ret;
903 mbedtls_cipher_info_t const *cipher_info;
Neil Armstronga8093f52022-05-04 17:44:05 +0200904#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000905 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
906 unsigned char const *key_enc;
907 unsigned char const *iv_enc;
908 unsigned char const *key_dec;
909 unsigned char const *iv_dec;
910
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +0100911#if defined(MBEDTLS_USE_PSA_CRYPTO)
912 psa_key_type_t key_type;
913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
914 psa_algorithm_t alg;
915 size_t key_bits;
916 psa_status_t status = PSA_SUCCESS;
917#endif
918
Hanno Beckerc94060c2021-03-22 07:50:44 +0000919#if !defined(MBEDTLS_DEBUG_C)
920 ssl = NULL; /* make sure we don't use it except for those cases */
921 (void) ssl;
922#endif
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
925 if (ciphersuite_info == NULL) {
926 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
927 ciphersuite));
928 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker7887a772021-04-20 05:27:57 +0100929 }
Hanno Beckerc94060c2021-03-22 07:50:44 +0000930
Neil Armstronga8093f52022-05-04 17:44:05 +0200931#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
933 if (cipher_info == NULL) {
934 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
935 ciphersuite_info->cipher));
936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000937 }
938
939 /*
940 * Setup cipher contexts in target transform
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
943 cipher_info)) != 0) {
944 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
945 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000946 }
947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
949 cipher_info)) != 0) {
950 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
951 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000952 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100953#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000954
955#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000957 key_enc = traffic_keys->server_write_key;
958 key_dec = traffic_keys->client_write_key;
959 iv_enc = traffic_keys->server_write_iv;
960 iv_dec = traffic_keys->client_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000962#endif /* MBEDTLS_SSL_SRV_C */
963#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerc94060c2021-03-22 07:50:44 +0000965 key_enc = traffic_keys->client_write_key;
966 key_dec = traffic_keys->server_write_key;
967 iv_enc = traffic_keys->client_write_iv;
968 iv_dec = traffic_keys->server_write_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 } else
Hanno Beckerc94060c2021-03-22 07:50:44 +0000970#endif /* MBEDTLS_SSL_CLI_C */
971 {
972 /* should not happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000974 }
975
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
977 memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000978
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100979#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
981 key_enc, cipher_info->key_bitlen,
982 MBEDTLS_ENCRYPT)) != 0) {
983 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
984 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000985 }
986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
988 key_dec, cipher_info->key_bitlen,
989 MBEDTLS_DECRYPT)) != 0) {
990 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
991 return ret;
Hanno Beckerc94060c2021-03-22 07:50:44 +0000992 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100993#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerc94060c2021-03-22 07:50:44 +0000994
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +0100995 /*
996 * Setup other fields in SSL transform
997 */
998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if ((ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) != 0) {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001000 transform->taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 } else {
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001002 transform->taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 }
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001004
1005 transform->ivlen = traffic_keys->iv_len;
1006 transform->maclen = 0;
1007 transform->fixed_ivlen = transform->ivlen;
Glenn Strauss07c64162022-03-14 12:34:51 -04001008 transform->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001009
1010 /* We add the true record content type (1 Byte) to the plaintext and
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001011 * then pad to the configured granularity. The minimum length of the
Przemyslaw Stekieldd7b5012022-01-17 15:28:57 +01001012 * type-extended and padded plaintext is therefore the padding
1013 * granularity. */
1014 transform->minlen =
1015 transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
1016
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001017#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001018 /*
1019 * Setup psa keys and alg
1020 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
1022 transform->taglen,
1023 &alg,
1024 &key_type,
1025 &key_bits)) != PSA_SUCCESS) {
1026 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", psa_ssl_status_to_mbedtls(status));
1027 return psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001028 }
1029
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001030 transform->psa_alg = alg;
1031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1033 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1034 psa_set_key_algorithm(&attributes, alg);
1035 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if ((status = psa_import_key(&attributes,
1038 key_enc,
1039 PSA_BITS_TO_BYTES(key_bits),
1040 &transform->psa_key_enc)) != PSA_SUCCESS) {
1041 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", psa_ssl_status_to_mbedtls(status));
1042 return psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001043 }
Przemyslaw Stekielfe7397d2022-01-17 15:47:07 +01001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if ((status = psa_import_key(&attributes,
1048 key_dec,
1049 PSA_BITS_TO_BYTES(key_bits),
1050 &transform->psa_key_dec)) != PSA_SUCCESS) {
1051 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", psa_ssl_status_to_mbedtls(status));
1052 return psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekielf9cd6082022-02-01 11:25:55 +01001053 }
Przemyslaw Stekielae77b0a2022-01-12 10:29:03 +01001054 }
1055#endif /* MBEDTLS_USE_PSA_CRYPTO */
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return 0;
Hanno Beckerc94060c2021-03-22 07:50:44 +00001058}
1059
Jerry Yu84a6eda2022-11-04 11:17:35 +08001060MBEDTLS_CHECK_RETURN_CRITICAL
1061static int ssl_tls13_get_cipher_key_info(
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 const mbedtls_ssl_ciphersuite_t *ciphersuite_info,
1063 size_t *key_len, size_t *iv_len)
Jerry Yu84a6eda2022-11-04 11:17:35 +08001064{
1065 psa_key_type_t key_type;
1066 psa_algorithm_t alg;
1067 size_t taglen;
1068 size_t key_bits;
1069 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG) {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001072 taglen = 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 } else {
Jerry Yu84a6eda2022-11-04 11:17:35 +08001074 taglen = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher, taglen,
1078 &alg, &key_type, &key_bits);
1079 if (status != PSA_SUCCESS) {
1080 return psa_ssl_status_to_mbedtls(status);
1081 }
Jerry Yu84a6eda2022-11-04 11:17:35 +08001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 *key_len = PSA_BITS_TO_BYTES(key_bits);
Jerry Yu84a6eda2022-11-04 11:17:35 +08001084
1085 /* TLS 1.3 only have AEAD ciphers, IV length is unconditionally 12 bytes */
1086 *iv_len = 12;
1087
1088 return 0;
1089}
1090
Jerry Yu91b560f2022-11-04 14:10:34 +08001091#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001092/*
1093 * ssl_tls13_generate_early_key() generates the key necessary for protecting
Jerry Yue31688b2022-11-22 21:55:56 +08001094 * the early application data and handshake messages as described in section 7
1095 * of RFC 8446.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001096 *
Jerry Yue31688b2022-11-22 21:55:56 +08001097 * NOTE: Only one key is generated, the key for the traffic from the client to
1098 * the server. The TLS 1.3 specification does not define a secret and thus
1099 * a key for server early traffic.
Jerry Yu3ce61ff2022-11-21 22:45:58 +08001100 */
Jerry Yu91b560f2022-11-04 14:10:34 +08001101MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001102static int ssl_tls13_generate_early_key(mbedtls_ssl_context *ssl,
1103 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu91b560f2022-11-04 14:10:34 +08001104{
1105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu91b560f2022-11-04 14:10:34 +08001106 mbedtls_md_type_t md_type;
Jerry Yu91b560f2022-11-04 14:10:34 +08001107 psa_algorithm_t hash_alg;
1108 size_t hash_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001109 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
1110 size_t transcript_len;
Jerry Yuaec08b32022-11-29 15:19:27 +08001111 size_t key_len;
1112 size_t iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001113
1114 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1115 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
1116 mbedtls_ssl_tls13_early_secrets *tls13_early_secrets = &handshake->tls13_early_secrets;
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1121 if (ret != 0) {
1122 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001123 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001124 }
1125
1126 md_type = ciphersuite_info->mac;
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
1129 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu91b560f2022-11-04 14:10:34 +08001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1132 transcript,
1133 sizeof(transcript),
1134 &transcript_len);
1135 if (ret != 0) {
1136 MBEDTLS_SSL_DEBUG_RET(1,
1137 "mbedtls_ssl_get_handshake_transcript",
1138 ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001139 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001140 }
1141
Jerry Yub094e122022-11-21 13:03:47 +08001142 ret = mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 hash_alg, handshake->tls13_master_secrets.early,
1144 transcript, transcript_len, tls13_early_secrets);
1145 if (ret != 0) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001146 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 1, "mbedtls_ssl_tls13_derive_early_secrets", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001148 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001149 }
1150
1151 MBEDTLS_SSL_DEBUG_BUF(
1152 4, "Client early traffic secret",
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 tls13_early_secrets->client_early_traffic_secret, hash_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001154
1155 /*
1156 * Export client handshake traffic secret
1157 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (ssl->f_export_keys != NULL) {
Jerry Yub094e122022-11-21 13:03:47 +08001159 ssl->f_export_keys(
1160 ssl->p_export_keys,
1161 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_EARLY_SECRET,
1162 tls13_early_secrets->client_early_traffic_secret,
1163 hash_len,
1164 handshake->randbytes,
1165 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu91b560f2022-11-04 14:10:34 +08001167 }
1168
Jerry Yua8771832022-11-21 23:16:54 +08001169 ret = ssl_tls13_make_traffic_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 hash_alg,
1171 tls13_early_secrets->client_early_traffic_secret,
1172 hash_len, traffic_keys->client_write_key, key_len,
1173 traffic_keys->client_write_iv, iv_len);
1174 if (ret != 0) {
1175 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_make_traffic_key", ret);
Jerry Yu3d78e082022-11-23 18:26:20 +08001176 goto cleanup;
Jerry Yu91b560f2022-11-04 14:10:34 +08001177 }
Jerry Yua8771832022-11-21 23:16:54 +08001178 traffic_keys->key_len = key_len;
1179 traffic_keys->iv_len = iv_len;
Jerry Yu91b560f2022-11-04 14:10:34 +08001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_key",
1182 traffic_keys->client_write_key,
1183 traffic_keys->key_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 MBEDTLS_SSL_DEBUG_BUF(4, "client early write_iv",
1186 traffic_keys->client_write_iv,
1187 traffic_keys->iv_len);
Jerry Yu91b560f2022-11-04 14:10:34 +08001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_generate_early_key"));
Jerry Yu91b560f2022-11-04 14:10:34 +08001190
Jerry Yu3d78e082022-11-23 18:26:20 +08001191cleanup:
1192 /* Erase secret and transcript */
1193 mbedtls_platform_zeroize(
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 tls13_early_secrets, sizeof(mbedtls_ssl_tls13_early_secrets));
1195 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1196 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001197}
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl)
Jerry Yu91b560f2022-11-04 14:10:34 +08001200{
1201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1202 mbedtls_ssl_key_set traffic_keys;
1203 mbedtls_ssl_transform *transform_earlydata = NULL;
1204 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1205
1206 /* Next evolution in key schedule: Establish early_data secret and
1207 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 ret = ssl_tls13_generate_early_key(ssl, &traffic_keys);
1209 if (ret != 0) {
1210 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_generate_early_key",
1211 ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001212 goto cleanup;
1213 }
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 transform_earlydata = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1216 if (transform_earlydata == NULL) {
Jerry Yu91b560f2022-11-04 14:10:34 +08001217 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1218 goto cleanup;
1219 }
1220
1221 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 transform_earlydata,
1223 ssl->conf->endpoint,
1224 ssl->session_negotiate->ciphersuite,
1225 &traffic_keys,
1226 ssl);
1227 if (ret != 0) {
1228 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yu91b560f2022-11-04 14:10:34 +08001229 goto cleanup;
1230 }
1231 handshake->transform_earlydata = transform_earlydata;
1232
1233cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1235 if (ret != 0) {
1236 mbedtls_free(transform_earlydata);
1237 }
Jerry Yu91b560f2022-11-04 14:10:34 +08001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 return ret;
Jerry Yu91b560f2022-11-04 14:10:34 +08001240}
1241#endif /* MBEDTLS_SSL_EARLY_DATA */
1242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
Jerry Yu89ea3212021-09-09 14:31:24 +08001244{
Jerry Yue3131ef2021-09-16 13:14:15 +08001245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gabor Mezei07732f72022-03-26 17:04:19 +01001246 psa_algorithm_t hash_alg;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001247 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Ronald Cron295d93e2022-07-19 08:21:29 +02001248 unsigned char *psk = NULL;
1249 size_t psk_len = 0;
Jerry Yu6ca7c7f2021-09-28 18:51:40 +08001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (handshake->ciphersuite_info == NULL) {
1252 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher suite info not found"));
1253 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu89ea3212021-09-09 14:31:24 +08001254 }
Jerry Yue3131ef2021-09-16 13:14:15 +08001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
Ronald Cron41a443a2022-10-04 16:38:25 +02001257#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
1259 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
1260 if (ret != 0) {
1261 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_export_handshake_psk",
1262 ret);
1263 return ret;
Jerry Yu5d01c052022-08-17 10:18:10 +08001264 }
Ronald Cron295d93e2022-07-19 08:21:29 +02001265 }
Jerry Yu5d01c052022-08-17 10:18:10 +08001266#endif
Ronald Cron295d93e2022-07-19 08:21:29 +02001267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
1269 handshake->tls13_master_secrets.early);
Ronald Cron295d93e2022-07-19 08:21:29 +02001270#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Ronald Cron41a443a2022-10-04 16:38:25 +02001271 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 mbedtls_free((void *) psk);
Ronald Cron295d93e2022-07-19 08:21:29 +02001273#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (ret != 0) {
1275 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
1276 return ret;
Jerry Yu89ea3212021-09-09 14:31:24 +08001277 }
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 MBEDTLS_SSL_DEBUG_BUF(4, "mbedtls_ssl_tls13_key_schedule_stage_early",
1280 handshake->tls13_master_secrets.early,
1281 PSA_HASH_LENGTH(hash_alg));
1282 return 0;
Jerry Yu89ea3212021-09-09 14:31:24 +08001283}
1284
Jerry Yuc068b662021-10-11 22:30:19 +08001285/* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
Jerry Yu61e35e02021-09-16 18:59:08 +08001286 * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287int mbedtls_ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl,
1288 mbedtls_ssl_key_set *traffic_keys)
Jerry Yu61e35e02021-09-16 18:59:08 +08001289{
1290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu61e35e02021-09-16 18:59:08 +08001291 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001292 psa_algorithm_t hash_alg;
1293 size_t hash_len;
Jerry Yu435208a2021-10-13 11:22:16 +08001294 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu61e35e02021-09-16 18:59:08 +08001295 size_t transcript_len;
Jerry Yub094e122022-11-21 13:03:47 +08001296 size_t key_len;
1297 size_t iv_len;
Jerry Yu61e35e02021-09-16 18:59:08 +08001298
Jerry Yu435208a2021-10-13 11:22:16 +08001299 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1300 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
Xiaofei Bai746f9482021-11-12 08:53:56 +00001301 mbedtls_ssl_tls13_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
Jerry Yu435208a2021-10-13 11:22:16 +08001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 ret = ssl_tls13_get_cipher_key_info(ciphersuite_info, &key_len, &iv_len);
1306 if (ret != 0) {
1307 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001308 return ret;
1309 }
1310
Jerry Yu435208a2021-10-13 11:22:16 +08001311 md_type = ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 hash_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
1314 hash_len = PSA_HASH_LENGTH(hash_alg);
Jerry Yu61e35e02021-09-16 18:59:08 +08001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1317 transcript,
1318 sizeof(transcript),
1319 &transcript_len);
1320 if (ret != 0) {
1321 MBEDTLS_SSL_DEBUG_RET(1,
1322 "mbedtls_ssl_get_handshake_transcript",
1323 ret);
1324 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001325 }
1326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 ret = mbedtls_ssl_tls13_derive_handshake_secrets(hash_alg,
1328 handshake->tls13_master_secrets.handshake,
1329 transcript, transcript_len, tls13_hs_secrets);
1330 if (ret != 0) {
1331 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_handshake_secrets",
1332 ret);
1333 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001334 }
1335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 MBEDTLS_SSL_DEBUG_BUF(4, "Client handshake traffic secret",
1337 tls13_hs_secrets->client_handshake_traffic_secret,
1338 hash_len);
1339 MBEDTLS_SSL_DEBUG_BUF(4, "Server handshake traffic secret",
1340 tls13_hs_secrets->server_handshake_traffic_secret,
1341 hash_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001342
1343 /*
1344 * Export client handshake traffic secret
1345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (ssl->f_export_keys != NULL) {
1347 ssl->f_export_keys(ssl->p_export_keys,
1348 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
1349 tls13_hs_secrets->client_handshake_traffic_secret,
1350 hash_len,
1351 handshake->randbytes,
1352 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1353 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 ssl->f_export_keys(ssl->p_export_keys,
1356 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_HANDSHAKE_TRAFFIC_SECRET,
1357 tls13_hs_secrets->server_handshake_traffic_secret,
1358 hash_len,
1359 handshake->randbytes,
1360 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1361 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */);
Jerry Yu61e35e02021-09-16 18:59:08 +08001362 }
Jerry Yu61e35e02021-09-16 18:59:08 +08001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 ret = mbedtls_ssl_tls13_make_traffic_keys(hash_alg,
1365 tls13_hs_secrets->client_handshake_traffic_secret,
1366 tls13_hs_secrets->server_handshake_traffic_secret,
1367 hash_len, key_len, iv_len, traffic_keys);
1368 if (ret != 0) {
1369 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
Jerry Yu61e35e02021-09-16 18:59:08 +08001370 goto exit;
1371 }
1372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_key",
1374 traffic_keys->client_write_key,
1375 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_key",
1378 traffic_keys->server_write_key,
1379 traffic_keys->key_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 MBEDTLS_SSL_DEBUG_BUF(4, "client_handshake write_iv",
1382 traffic_keys->client_write_iv,
1383 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 MBEDTLS_SSL_DEBUG_BUF(4, "server_handshake write_iv",
1386 traffic_keys->server_write_iv,
1387 traffic_keys->iv_len);
Jerry Yu61e35e02021-09-16 18:59:08 +08001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_tls13_generate_handshake_keys"));
Jerry Yu61e35e02021-09-16 18:59:08 +08001390
1391exit:
1392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 return ret;
Jerry Yu61e35e02021-09-16 18:59:08 +08001394}
1395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396int mbedtls_ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl)
Jerry Yua0650eb2021-09-09 17:14:45 +08001397{
Jerry Yuf0ac2352021-10-11 17:47:07 +08001398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5ccfcd42021-10-11 16:39:29 +08001399 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001400 psa_algorithm_t const hash_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 handshake->ciphersuite_info->mac);
Ronald Cron3b056202022-10-05 17:20:21 +02001402 unsigned char *shared_secret = NULL;
1403 size_t shared_secret_len = 0;
Jerry Yua0650eb2021-09-09 17:14:45 +08001404
Ronald Crona2900bc2022-10-20 14:37:35 +02001405#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Jerry Yuf0ac2352021-10-11 17:47:07 +08001406 /*
1407 * Compute ECDHE secret used to compute the handshake secret from which
1408 * client_handshake_traffic_secret and server_handshake_traffic_secret
1409 * are derived in the handshake secret derivation stage.
1410 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
1412 if (mbedtls_ssl_tls13_named_group_is_ecdhe(handshake->offered_group_id)) {
Jerry Yuf0ac2352021-10-11 17:47:07 +08001413#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 /* Compute ECDH shared secret. */
Ronald Cron4c7edb22022-10-05 15:37:11 +02001415 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Ronald Cron3b056202022-10-05 17:20:21 +02001416 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 status = psa_get_key_attributes(handshake->ecdh_psa_privkey,
1419 &key_attributes);
1420 if (status != PSA_SUCCESS) {
1421 ret = psa_ssl_status_to_mbedtls(status);
1422 }
Ronald Cron3b056202022-10-05 17:20:21 +02001423
1424 shared_secret_len = PSA_BITS_TO_BYTES(
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 psa_get_key_bits(&key_attributes));
1426 shared_secret = mbedtls_calloc(1, shared_secret_len);
1427 if (shared_secret == NULL) {
1428 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1429 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001430
Ronald Cron4c7edb22022-10-05 15:37:11 +02001431 status = psa_raw_key_agreement(
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
1433 handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
1434 shared_secret, shared_secret_len, &shared_secret_len);
1435 if (status != PSA_SUCCESS) {
1436 ret = psa_ssl_status_to_mbedtls(status);
1437 MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001438 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001439 }
Przemyslaw Stekielc0824bf2022-02-10 10:37:15 +01001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 status = psa_destroy_key(handshake->ecdh_psa_privkey);
1442 if (status != PSA_SUCCESS) {
1443 ret = psa_ssl_status_to_mbedtls(status);
1444 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001445 goto cleanup;
Ronald Cron4c7edb22022-10-05 15:37:11 +02001446 }
1447
1448 handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001449#endif /* MBEDTLS_ECDH_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 } else {
1451 MBEDTLS_SSL_DEBUG_MSG(1, ("Group not supported."));
1452 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yuf0ac2352021-10-11 17:47:07 +08001453 }
1454 }
Ronald Crona2900bc2022-10-20 14:37:35 +02001455#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Jerry Yua0650eb2021-09-09 17:14:45 +08001456
1457 /*
Jerry Yuf0ac2352021-10-11 17:47:07 +08001458 * Compute the Handshake Secret
Jerry Yua0650eb2021-09-09 17:14:45 +08001459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 ret = mbedtls_ssl_tls13_evolve_secret(hash_alg,
1461 handshake->tls13_master_secrets.early,
1462 shared_secret, shared_secret_len,
1463 handshake->tls13_master_secrets.handshake);
1464 if (ret != 0) {
1465 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_evolve_secret", ret);
Ronald Cron3b056202022-10-05 17:20:21 +02001466 goto cleanup;
Jerry Yua0650eb2021-09-09 17:14:45 +08001467 }
1468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 MBEDTLS_SSL_DEBUG_BUF(4, "Handshake secret",
1470 handshake->tls13_master_secrets.handshake,
1471 PSA_HASH_LENGTH(hash_alg));
Jerry Yua0650eb2021-09-09 17:14:45 +08001472
Ronald Cron3b056202022-10-05 17:20:21 +02001473cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (shared_secret != NULL) {
1475 mbedtls_platform_zeroize(shared_secret, shared_secret_len);
1476 mbedtls_free(shared_secret);
Ronald Cron3b056202022-10-05 17:20:21 +02001477 }
1478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return ret;
Jerry Yua0650eb2021-09-09 17:14:45 +08001480}
1481
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001482/* Generate application traffic keys since any records following a 1-RTT Finished message
1483 * MUST be encrypted under the application traffic key.
1484 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001485int mbedtls_ssl_tls13_generate_application_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 mbedtls_ssl_context *ssl,
1487 mbedtls_ssl_key_set *traffic_keys)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001488{
XiaokangQiana7634982021-10-22 06:32:32 +00001489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian33062842021-11-11 03:37:45 +00001490 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001491
1492 /* Address at which to store the application secrets */
Xiaofei Bai746f9482021-11-12 08:53:56 +00001493 mbedtls_ssl_tls13_application_secrets * const app_secrets =
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001494 &ssl->session_negotiate->app_secrets;
1495
1496 /* Holding the transcript up to and including the ServerFinished */
XiaokangQian33062842021-11-11 03:37:45 +00001497 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001498 size_t transcript_len;
1499
1500 /* Variables relating to the hash for the chosen ciphersuite. */
1501 mbedtls_md_type_t md_type;
Gabor Mezei07732f72022-03-26 17:04:19 +01001502
1503 psa_algorithm_t hash_alg;
1504 size_t hash_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001505
1506 /* Variables relating to the cipher for the chosen ciphersuite. */
Xiaofei Baib7972842021-11-18 07:29:56 +00001507 size_t key_len, iv_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive application traffic keys"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001510
1511 /* Extract basic information about hash and ciphersuite */
1512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 ret = ssl_tls13_get_cipher_key_info(handshake->ciphersuite_info,
1514 &key_len, &iv_len);
1515 if (ret != 0) {
1516 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_get_cipher_key_info", ret);
Neil Armstrong4f4f2712022-05-05 15:34:39 +02001517 goto cleanup;
1518 }
1519
XiaokangQian33062842021-11-11 03:37:45 +00001520 md_type = handshake->ciphersuite_info->mac;
Gabor Mezei07732f72022-03-26 17:04:19 +01001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 hash_alg = mbedtls_hash_info_psa_from_md(handshake->ciphersuite_info->mac);
1523 hash_len = PSA_HASH_LENGTH(hash_alg);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001524
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001525 /* Compute current handshake transcript. It's the caller's responsibility
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001526 * to call this at the right time, that is, after the ServerFinished. */
1527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1529 transcript, sizeof(transcript),
1530 &transcript_len);
1531 if (ret != 0) {
XiaokangQian4cab0242021-10-12 08:43:37 +00001532 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001534
1535 /* Compute application secrets from master secret and transcript hash. */
1536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 ret = mbedtls_ssl_tls13_derive_application_secrets(hash_alg,
1538 handshake->tls13_master_secrets.app,
1539 transcript, transcript_len,
1540 app_secrets);
1541 if (ret != 0) {
1542 MBEDTLS_SSL_DEBUG_RET(1,
1543 "mbedtls_ssl_tls13_derive_application_secrets", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001544 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001545 }
1546
1547 /* Derive first epoch of IV + Key for application traffic. */
1548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 ret = mbedtls_ssl_tls13_make_traffic_keys(hash_alg,
1550 app_secrets->client_application_traffic_secret_N,
1551 app_secrets->server_application_traffic_secret_N,
1552 hash_len, key_len, iv_len, traffic_keys);
1553 if (ret != 0) {
1554 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_make_traffic_keys", ret);
XiaokangQian4cab0242021-10-12 08:43:37 +00001555 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001556 }
1557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 MBEDTLS_SSL_DEBUG_BUF(4, "Client application traffic secret",
1559 app_secrets->client_application_traffic_secret_N,
1560 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 MBEDTLS_SSL_DEBUG_BUF(4, "Server application traffic secret",
1563 app_secrets->server_application_traffic_secret_N,
1564 hash_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001565
XiaokangQianac0385c2021-11-03 06:40:11 +00001566 /*
1567 * Export client/server application traffic secret 0
1568 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (ssl->f_export_keys != NULL) {
1570 ssl->f_export_keys(ssl->p_export_keys,
1571 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_CLIENT_APPLICATION_TRAFFIC_SECRET,
1572 app_secrets->client_application_traffic_secret_N, hash_len,
1573 handshake->randbytes,
1574 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1575 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1576 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 ssl->f_export_keys(ssl->p_export_keys,
1579 MBEDTLS_SSL_KEY_EXPORT_TLS1_3_SERVER_APPLICATION_TRAFFIC_SECRET,
1580 app_secrets->server_application_traffic_secret_N, hash_len,
1581 handshake->randbytes,
1582 handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN,
1583 MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
1584 a new constant for TLS 1.3! */);
XiaokangQianac0385c2021-11-03 06:40:11 +00001585 }
1586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 MBEDTLS_SSL_DEBUG_BUF(4, "client application_write_key:",
1588 traffic_keys->client_write_key, key_len);
1589 MBEDTLS_SSL_DEBUG_BUF(4, "server application write key",
1590 traffic_keys->server_write_key, key_len);
1591 MBEDTLS_SSL_DEBUG_BUF(4, "client application write IV",
1592 traffic_keys->client_write_iv, iv_len);
1593 MBEDTLS_SSL_DEBUG_BUF(4, "server application write IV",
1594 traffic_keys->server_write_iv, iv_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive application traffic keys"));
XiaokangQian4cab0242021-10-12 08:43:37 +00001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598cleanup:
Jerry Yu2c70a392021-12-08 13:28:49 +08001599 /* randbytes is not used again */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 mbedtls_platform_zeroize(ssl->handshake->randbytes,
1601 sizeof(ssl->handshake->randbytes));
Jerry Yuef2b98a2022-05-06 16:40:05 +08001602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 mbedtls_platform_zeroize(transcript, sizeof(transcript));
1604 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001605}
1606
Gilles Peskine449bd832023-01-11 14:50:10 +01001607int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08001608{
Jerry Yuef2b98a2022-05-06 16:40:05 +08001609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yue110d252022-05-05 10:19:22 +08001610 mbedtls_ssl_key_set traffic_keys;
1611 mbedtls_ssl_transform *transform_handshake = NULL;
1612 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1613
1614 /* Compute handshake secret */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 ret = mbedtls_ssl_tls13_key_schedule_stage_handshake(ssl);
1616 if (ret != 0) {
1617 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_derive_master_secret", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001618 goto cleanup;
1619 }
1620
1621 /* Next evolution in key schedule: Establish handshake secret and
1622 * key material. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 ret = mbedtls_ssl_tls13_generate_handshake_keys(ssl, &traffic_keys);
1624 if (ret != 0) {
1625 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_generate_handshake_keys",
1626 ret);
Jerry Yue110d252022-05-05 10:19:22 +08001627 goto cleanup;
1628 }
1629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 transform_handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1631 if (transform_handshake == NULL) {
Jerry Yue110d252022-05-05 10:19:22 +08001632 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1633 goto cleanup;
1634 }
1635
Jerry Yuef2b98a2022-05-06 16:40:05 +08001636 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 transform_handshake,
1638 ssl->conf->endpoint,
1639 ssl->session_negotiate->ciphersuite,
1640 &traffic_keys,
1641 ssl);
1642 if (ret != 0) {
1643 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yue110d252022-05-05 10:19:22 +08001644 goto cleanup;
1645 }
1646 handshake->transform_handshake = transform_handshake;
1647
1648cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1650 if (ret != 0) {
1651 mbedtls_free(transform_handshake);
1652 }
Jerry Yue110d252022-05-05 10:19:22 +08001653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08001655}
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl)
Jerry Yuff226982022-04-16 16:52:57 +08001658{
Jerry Yu46bffe02022-09-13 11:25:28 +08001659 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu49d63f82022-08-03 12:28:08 +08001660 mbedtls_md_type_t md_type;
Jerry Yu46bffe02022-09-13 11:25:28 +08001661 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1662 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu49d63f82022-08-03 12:28:08 +08001663 size_t transcript_len;
1664
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 MBEDTLS_SSL_DEBUG_MSG(2,
1666 ("=> mbedtls_ssl_tls13_compute_resumption_master_secret"));
Jerry Yu49d63f82022-08-03 12:28:08 +08001667
Jerry Yu46bffe02022-09-13 11:25:28 +08001668 md_type = handshake->ciphersuite_info->mac;
Jerry Yu49d63f82022-08-03 12:28:08 +08001669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 ret = mbedtls_ssl_get_handshake_transcript(ssl, md_type,
1671 transcript, sizeof(transcript),
1672 &transcript_len);
1673 if (ret != 0) {
1674 return ret;
1675 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001676
1677 ret = mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 mbedtls_psa_translate_md(md_type),
1679 handshake->tls13_master_secrets.app,
1680 transcript, transcript_len,
1681 &ssl->session_negotiate->app_secrets);
1682 if (ret != 0) {
1683 return ret;
1684 }
Jerry Yu49d63f82022-08-03 12:28:08 +08001685
Jerry Yuff226982022-04-16 16:52:57 +08001686 /* Erase master secrets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 mbedtls_platform_zeroize(&handshake->tls13_master_secrets,
1688 sizeof(handshake->tls13_master_secrets));
Jerry Yu49d63f82022-08-03 12:28:08 +08001689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 MBEDTLS_SSL_DEBUG_BUF(4, "Resumption master secret",
1691 ssl->session_negotiate->app_secrets.resumption_master_secret,
1692 PSA_HASH_LENGTH(mbedtls_psa_translate_md(md_type)));
Jerry Yu46bffe02022-09-13 11:25:28 +08001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 MBEDTLS_SSL_DEBUG_MSG(2,
1695 ("<= mbedtls_ssl_tls13_compute_resumption_master_secret"));
1696 return 0;
Jerry Yuff226982022-04-16 16:52:57 +08001697}
1698
Gilles Peskine449bd832023-01-11 14:50:10 +01001699int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl)
Jerry Yufd5ea042022-05-19 14:29:48 +08001700{
1701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1702 mbedtls_ssl_key_set traffic_keys;
1703 mbedtls_ssl_transform *transform_application = NULL;
1704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 ret = mbedtls_ssl_tls13_key_schedule_stage_application(ssl);
1706 if (ret != 0) {
1707 MBEDTLS_SSL_DEBUG_RET(1,
1708 "mbedtls_ssl_tls13_key_schedule_stage_application", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001709 goto cleanup;
1710 }
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 ret = mbedtls_ssl_tls13_generate_application_keys(ssl, &traffic_keys);
1713 if (ret != 0) {
1714 MBEDTLS_SSL_DEBUG_RET(1,
1715 "mbedtls_ssl_tls13_generate_application_keys", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001716 goto cleanup;
1717 }
1718
1719 transform_application =
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
1721 if (transform_application == NULL) {
Jerry Yufd5ea042022-05-19 14:29:48 +08001722 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1723 goto cleanup;
1724 }
1725
1726 ret = mbedtls_ssl_tls13_populate_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 transform_application,
1728 ssl->conf->endpoint,
1729 ssl->session_negotiate->ciphersuite,
1730 &traffic_keys,
1731 ssl);
1732 if (ret != 0) {
1733 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_populate_transform", ret);
Jerry Yufd5ea042022-05-19 14:29:48 +08001734 goto cleanup;
1735 }
1736
1737 ssl->transform_application = transform_application;
1738
1739cleanup:
1740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 mbedtls_platform_zeroize(&traffic_keys, sizeof(traffic_keys));
1742 if (ret != 0) {
1743 mbedtls_free(transform_application);
Jerry Yufd5ea042022-05-19 14:29:48 +08001744 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 return ret;
Jerry Yufd5ea042022-05-19 14:29:48 +08001746}
1747
Ronald Cron41a443a2022-10-04 16:38:25 +02001748#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001749int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
1750 unsigned char **psk,
1751 size_t *psk_len)
Jerry Yu40f37712022-07-26 16:58:57 +08001752{
Jerry Yu40f37712022-07-26 16:58:57 +08001753#if defined(MBEDTLS_USE_PSA_CRYPTO)
1754 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
Jerry Yuc5a23a02022-08-25 10:51:44 +08001755 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu40f37712022-07-26 16:58:57 +08001756
1757 *psk_len = 0;
1758 *psk = NULL;
1759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 if (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
1761 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu40f37712022-07-26 16:58:57 +08001762 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001763
1764 status = psa_get_key_attributes(ssl->handshake->psk_opaque, &key_attributes);
1765 if (status != PSA_SUCCESS) {
1766 return psa_ssl_status_to_mbedtls(status);
1767 }
1768
1769 *psk_len = PSA_BITS_TO_BYTES(psa_get_key_bits(&key_attributes));
1770 *psk = mbedtls_calloc(1, *psk_len);
1771 if (*psk == NULL) {
1772 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1773 }
1774
1775 status = psa_export_key(ssl->handshake->psk_opaque,
1776 (uint8_t *) *psk, *psk_len, psk_len);
1777 if (status != PSA_SUCCESS) {
1778 mbedtls_free((void *) *psk);
1779 *psk = NULL;
1780 return psa_ssl_status_to_mbedtls(status);
1781 }
1782 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001783#else
1784 *psk = ssl->handshake->psk;
1785 *psk_len = ssl->handshake->psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (*psk == NULL) {
1787 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1788 }
1789 return 0;
Jerry Yu40f37712022-07-26 16:58:57 +08001790#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu40f37712022-07-26 16:58:57 +08001791}
Ronald Cron41a443a2022-10-04 16:38:25 +02001792#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +08001793
Ronald Cron6f135e12021-12-08 16:57:54 +01001794#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */