blob: 1e6fff58bfebe4c6ddfdd7fb383b59ad26062a17 [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#if !defined(MBEDTLS_SSL_TLS1_3_KEYS_H)
20#define MBEDTLS_SSL_TLS1_3_KEYS_H
21
XiaokangQian46c6fc72021-10-22 10:20:28 +000022#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker70d7fb02020-09-09 10:11:21 +010023/* This requires MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) to be defined at
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010024 * the point of use. See e.g. the definition of mbedtls_ssl_tls1_3_labels_union
25 * below. */
Jerry Yu0bbb3972021-09-19 20:27:17 +080026#define MBEDTLS_SSL_TLS1_3_LABEL_LIST \
27 MBEDTLS_SSL_TLS1_3_LABEL( finished , "finished" ) \
28 MBEDTLS_SSL_TLS1_3_LABEL( resumption , "resumption" ) \
29 MBEDTLS_SSL_TLS1_3_LABEL( traffic_upd , "traffic upd" ) \
30 MBEDTLS_SSL_TLS1_3_LABEL( exporter , "exporter" ) \
31 MBEDTLS_SSL_TLS1_3_LABEL( key , "key" ) \
32 MBEDTLS_SSL_TLS1_3_LABEL( iv , "iv" ) \
33 MBEDTLS_SSL_TLS1_3_LABEL( c_hs_traffic, "c hs traffic" ) \
34 MBEDTLS_SSL_TLS1_3_LABEL( c_ap_traffic, "c ap traffic" ) \
35 MBEDTLS_SSL_TLS1_3_LABEL( c_e_traffic , "c e traffic" ) \
36 MBEDTLS_SSL_TLS1_3_LABEL( s_hs_traffic, "s hs traffic" ) \
37 MBEDTLS_SSL_TLS1_3_LABEL( s_ap_traffic, "s ap traffic" ) \
38 MBEDTLS_SSL_TLS1_3_LABEL( s_e_traffic , "s e traffic" ) \
39 MBEDTLS_SSL_TLS1_3_LABEL( e_exp_master, "e exp master" ) \
40 MBEDTLS_SSL_TLS1_3_LABEL( res_master , "res master" ) \
41 MBEDTLS_SSL_TLS1_3_LABEL( exp_master , "exp master" ) \
42 MBEDTLS_SSL_TLS1_3_LABEL( ext_binder , "ext binder" ) \
43 MBEDTLS_SSL_TLS1_3_LABEL( res_binder , "res binder" ) \
44 MBEDTLS_SSL_TLS1_3_LABEL( derived , "derived" ) \
45 MBEDTLS_SSL_TLS1_3_LABEL( client_cv , "TLS 1.3, client CertificateVerify" ) \
46 MBEDTLS_SSL_TLS1_3_LABEL( server_cv , "TLS 1.3, server CertificateVerify" )
Hanno Beckere4435ea2020-09-08 10:43:52 +010047
Hanno Becker1413bd82020-09-09 12:46:09 +010048#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010049 const unsigned char name [ sizeof(string) - 1 ];
Hanno Beckerbe9d6642020-08-21 13:20:06 +010050
51union mbedtls_ssl_tls1_3_labels_union
52{
53 MBEDTLS_SSL_TLS1_3_LABEL_LIST
54};
55struct mbedtls_ssl_tls1_3_labels_struct
56{
57 MBEDTLS_SSL_TLS1_3_LABEL_LIST
58};
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010059#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010060
Hanno Beckerbe9d6642020-08-21 13:20:06 +010061extern const struct mbedtls_ssl_tls1_3_labels_struct mbedtls_ssl_tls1_3_labels;
62
Jerry Yu0bbb3972021-09-19 20:27:17 +080063#define MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL ) \
64 sizeof(mbedtls_ssl_tls1_3_labels.LABEL)
65
Hanno Beckerbe9d6642020-08-21 13:20:06 +010066#define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \
67 mbedtls_ssl_tls1_3_labels.LABEL, \
Jerry Yu0bbb3972021-09-19 20:27:17 +080068 MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010069
70#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
71 sizeof( union mbedtls_ssl_tls1_3_labels_union )
72
Hanno Becker61baae72020-09-16 09:24:14 +010073/* The maximum length of HKDF contexts used in the TLS 1.3 standard.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010074 * Since contexts are always hashes of message transcripts, this can
75 * be approximated from above by the maximum hash size. */
76#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
77 MBEDTLS_MD_MAX_SIZE
78
79/* Maximum desired length for expanded key material generated
Hanno Becker531fe302020-09-16 09:45:27 +010080 * by HKDF-Expand-Label.
81 *
82 * Warning: If this ever needs to be increased, the implementation
83 * ssl_tls1_3_hkdf_encode_label() in ssl_tls13_keys.c needs to be
84 * adjusted since it currently assumes that HKDF key expansion
85 * is never used with more than 255 Bytes of output. */
Hanno Beckerbe9d6642020-08-21 13:20:06 +010086#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
87
88/**
89 * \brief The \c HKDF-Expand-Label function from
90 * the TLS 1.3 standard RFC 8446.
91 *
92 * <tt>
93 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
94 * HKDF-Expand( Secret, HkdfLabel, Length )
95 * </tt>
96 *
97 * \param hash_alg The identifier for the hash algorithm to use.
98 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
99 * This must be a readable buffer of length \p slen Bytes.
100 * \param slen The length of \p secret in Bytes.
101 * \param label The \c Label argument to \c HKDF-Expand-Label.
102 * This must be a readable buffer of length \p llen Bytes.
103 * \param llen The length of \p label in Bytes.
104 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
105 * This must be a readable buffer of length \p clen Bytes.
106 * \param clen The length of \p context in Bytes.
107 * \param buf The destination buffer to hold the expanded secret.
Hanno Becker61baae72020-09-16 09:24:14 +0100108 * This must be a writable buffer of length \p blen Bytes.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100109 * \param blen The desired size of the expanded secret in Bytes.
110 *
111 * \returns \c 0 on success.
112 * \return A negative error code on failure.
113 */
114
115int mbedtls_ssl_tls1_3_hkdf_expand_label(
116 mbedtls_md_type_t hash_alg,
117 const unsigned char *secret, size_t slen,
118 const unsigned char *label, size_t llen,
119 const unsigned char *ctx, size_t clen,
120 unsigned char *buf, size_t blen );
121
Hanno Becker3385a4d2020-08-21 13:03:34 +0100122/**
123 * \brief This function is part of the TLS 1.3 key schedule.
124 * It extracts key and IV for the actual client/server traffic
125 * from the client/server traffic secrets.
126 *
127 * From RFC 8446:
128 *
129 * <tt>
130 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
131 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
132 * </tt>
133 *
134 * \param hash_alg The identifier for the hash algorithm to be used
135 * for the HKDF-based expansion of the secret.
136 * \param client_secret The client traffic secret.
137 * This must be a readable buffer of size \p slen Bytes
138 * \param server_secret The server traffic secret.
139 * This must be a readable buffer of size \p slen Bytes
140 * \param slen Length of the secrets \p client_secret and
141 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100142 * \param key_len The desired length of the key to be extracted in Bytes.
143 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100144 * \param keys The address of the structure holding the generated
145 * keys and IVs.
146 *
147 * \returns \c 0 on success.
148 * \returns A negative error code on failure.
149 */
150
151int mbedtls_ssl_tls1_3_make_traffic_keys(
152 mbedtls_md_type_t hash_alg,
153 const unsigned char *client_secret,
154 const unsigned char *server_secret,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100155 size_t slen, size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100156 mbedtls_ssl_key_set *keys );
157
Hanno Becker0973ff92020-09-09 12:56:28 +0100158
159#define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
160#define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
161
Hanno Beckerb35d5222020-08-21 13:27:44 +0100162/**
163 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
164 *
165 * <tt>
166 * Derive-Secret( Secret, Label, Messages ) =
167 * HKDF-Expand-Label( Secret, Label,
168 * Hash( Messages ),
169 * Hash.Length ) )
170 * </tt>
171 *
Hanno Becker0c42fd92020-09-09 12:58:29 +0100172 * \param hash_alg The identifier for the hash function used for the
173 * applications of HKDF.
174 * \param secret The \c Secret argument to the \c Derive-Secret function.
175 * This must be a readable buffer of length \p slen Bytes.
176 * \param slen The length of \p secret in Bytes.
177 * \param label The \c Label argument to the \c Derive-Secret function.
178 * This must be a readable buffer of length \p llen Bytes.
179 * \param llen The length of \p label in Bytes.
180 * \param ctx The hash of the \c Messages argument to the
181 * \c Derive-Secret function, or the \c Messages argument
182 * itself, depending on \p context_already_hashed.
183 * \param clen The length of \p hash.
184 * \param ctx_hashed This indicates whether the \p ctx contains the hash of
185 * the \c Messages argument in the application of the
186 * \c Derive-Secret function
187 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
188 * it is the content of \c Messages itself, in which case
189 * the function takes care of the hashing
190 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
191 * \param dstbuf The target buffer to write the output of
192 * \c Derive-Secret to. This must be a writable buffer of
193 * size \p buflen Bytes.
194 * \param buflen The length of \p dstbuf in Bytes.
Hanno Beckerb35d5222020-08-21 13:27:44 +0100195 *
196 * \returns \c 0 on success.
197 * \returns A negative error code on failure.
198 */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100199int mbedtls_ssl_tls1_3_derive_secret(
200 mbedtls_md_type_t hash_alg,
201 const unsigned char *secret, size_t slen,
202 const unsigned char *label, size_t llen,
203 const unsigned char *ctx, size_t clen,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100204 int ctx_hashed,
Hanno Beckerb35d5222020-08-21 13:27:44 +0100205 unsigned char *dstbuf, size_t buflen );
206
Hanno Beckere9cccb42020-08-20 13:42:46 +0100207/**
Hanno Beckeref5235b2021-05-24 06:39:41 +0100208 * \brief Derive TLS 1.3 early data key material from early secret.
209 *
210 * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret()
211 * with the appropriate labels.
212 *
213 * <tt>
214 * Early Secret
215 * |
216 * +-----> Derive-Secret(., "c e traffic", ClientHello)
217 * | = client_early_traffic_secret
218 * |
219 * +-----> Derive-Secret(., "e exp master", ClientHello)
220 * . = early_exporter_master_secret
221 * .
222 * .
223 * </tt>
224 *
225 * \note To obtain the actual key and IV for the early data traffic,
226 * the client secret derived by this function need to be
227 * further processed by mbedtls_ssl_tls1_3_make_traffic_keys().
228 *
229 * \note The binder key, which is also generated from the early secret,
230 * is omitted here. Its calculation is part of the separate routine
231 * mbedtls_ssl_tls1_3_create_psk_binder().
232 *
233 * \param md_type The hash algorithm associated with the PSK for which
234 * early data key material is being derived.
235 * \param early_secret The early secret from which the early data key material
236 * should be derived. This must be a readable buffer whose
237 * length is the digest size of the hash algorithm
238 * represented by \p md_size.
239 * \param transcript The transcript of the handshake so far, calculated with
240 * respect to \p md_type. This must be a readable buffer
241 * whose length is the digest size of the hash algorithm
242 * represented by \p md_size.
243 * \param derived The address of the structure in which to store
244 * the early data key material.
245 *
246 * \returns \c 0 on success.
247 * \returns A negative error code on failure.
248 */
249int mbedtls_ssl_tls1_3_derive_early_secrets(
250 mbedtls_md_type_t md_type,
251 unsigned char const *early_secret,
252 unsigned char const *transcript, size_t transcript_len,
253 mbedtls_ssl_tls1_3_early_secrets *derived );
254
255/**
256 * \brief Derive TLS 1.3 handshake key material from the handshake secret.
257 *
258 * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret()
259 * with the appropriate labels from the standard.
260 *
261 * <tt>
262 * Handshake Secret
263 * |
264 * +-----> Derive-Secret( ., "c hs traffic",
265 * | ClientHello...ServerHello )
266 * | = client_handshake_traffic_secret
267 * |
268 * +-----> Derive-Secret( ., "s hs traffic",
269 * . ClientHello...ServerHello )
270 * . = server_handshake_traffic_secret
271 * .
272 * </tt>
273 *
274 * \note To obtain the actual key and IV for the encrypted handshake traffic,
275 * the client and server secret derived by this function need to be
276 * further processed by mbedtls_ssl_tls1_3_make_traffic_keys().
277 *
278 * \param md_type The hash algorithm associated with the ciphersuite
279 * that's being used for the connection.
280 * \param handshake_secret The handshake secret from which the handshake key
281 * material should be derived. This must be a readable
282 * buffer whose length is the digest size of the hash
283 * algorithm represented by \p md_size.
284 * \param transcript The transcript of the handshake so far, calculated
285 * with respect to \p md_type. This must be a readable
286 * buffer whose length is the digest size of the hash
287 * algorithm represented by \p md_size.
288 * \param derived The address of the structure in which to
289 * store the handshake key material.
290 *
291 * \returns \c 0 on success.
292 * \returns A negative error code on failure.
293 */
294int mbedtls_ssl_tls1_3_derive_handshake_secrets(
295 mbedtls_md_type_t md_type,
296 unsigned char const *handshake_secret,
297 unsigned char const *transcript, size_t transcript_len,
298 mbedtls_ssl_tls1_3_handshake_secrets *derived );
299
300/**
301 * \brief Derive TLS 1.3 application key material from the master secret.
302 *
303 * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret()
304 * with the appropriate labels from the standard.
305 *
306 * <tt>
307 * Master Secret
308 * |
309 * +-----> Derive-Secret( ., "c ap traffic",
310 * | ClientHello...server Finished )
311 * | = client_application_traffic_secret_0
312 * |
313 * +-----> Derive-Secret( ., "s ap traffic",
314 * | ClientHello...Server Finished )
315 * | = server_application_traffic_secret_0
316 * |
317 * +-----> Derive-Secret( ., "exp master",
318 * . ClientHello...server Finished)
319 * . = exporter_master_secret
320 * .
321 * </tt>
322 *
323 * \note To obtain the actual key and IV for the (0-th) application traffic,
324 * the client and server secret derived by this function need to be
325 * further processed by mbedtls_ssl_tls1_3_make_traffic_keys().
326 *
327 * \param md_type The hash algorithm associated with the ciphersuite
328 * that's being used for the connection.
329 * \param master_secret The master secret from which the application key
330 * material should be derived. This must be a readable
331 * buffer whose length is the digest size of the hash
332 * algorithm represented by \p md_size.
333 * \param transcript The transcript of the handshake up to and including
334 * the ServerFinished message, calculated with respect
335 * to \p md_type. This must be a readable buffer whose
336 * length is the digest size of the hash algorithm
337 * represented by \p md_type.
338 * \param derived The address of the structure in which to
339 * store the application key material.
340 *
341 * \returns \c 0 on success.
342 * \returns A negative error code on failure.
343 */
344int mbedtls_ssl_tls1_3_derive_application_secrets(
345 mbedtls_md_type_t md_type,
346 unsigned char const *master_secret,
347 unsigned char const *transcript, size_t transcript_len,
XiaokangQiana7634982021-10-22 06:32:32 +0000348 mbedtls_ssl_tls13_application_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100349
350/**
351 * \brief Derive TLS 1.3 resumption master secret from the master secret.
352 *
353 * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret()
354 * with the appropriate labels from the standard.
355 *
356 * \param md_type The hash algorithm used in the application for which
357 * key material is being derived.
358 * \param application_secret The application secret from which the resumption master
359 * secret should be derived. This must be a readable
360 * buffer whose length is the digest size of the hash
361 * algorithm represented by \p md_size.
362 * \param transcript The transcript of the handshake up to and including
363 * the ClientFinished message, calculated with respect
364 * to \p md_type. This must be a readable buffer whose
365 * length is the digest size of the hash algorithm
366 * represented by \p md_type.
367 * \param transcript_len The length of \p transcript in Bytes.
368 * \param derived The address of the structure in which to
369 * store the resumption master secret.
370 *
371 * \returns \c 0 on success.
372 * \returns A negative error code on failure.
373 */
374int mbedtls_ssl_tls1_3_derive_resumption_master_secret(
375 mbedtls_md_type_t md_type,
376 unsigned char const *application_secret,
377 unsigned char const *transcript, size_t transcript_len,
XiaokangQiana7634982021-10-22 06:32:32 +0000378 mbedtls_ssl_tls13_application_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100379
380/**
Hanno Beckere9cccb42020-08-20 13:42:46 +0100381 * \brief Compute the next secret in the TLS 1.3 key schedule
382 *
383 * The TLS 1.3 key schedule proceeds as follows to compute
384 * the three main secrets during the handshake: The early
385 * secret for early data, the handshake secret for all
386 * other encrypted handshake messages, and the master
387 * secret for all application traffic.
388 *
389 * <tt>
390 * 0
391 * |
392 * v
393 * PSK -> HKDF-Extract = Early Secret
394 * |
395 * v
396 * Derive-Secret( ., "derived", "" )
397 * |
398 * v
399 * (EC)DHE -> HKDF-Extract = Handshake Secret
400 * |
401 * v
402 * Derive-Secret( ., "derived", "" )
403 * |
404 * v
405 * 0 -> HKDF-Extract = Master Secret
406 * </tt>
407 *
408 * Each of the three secrets in turn is the basis for further
409 * key derivations, such as the derivation of traffic keys and IVs;
410 * see e.g. mbedtls_ssl_tls1_3_make_traffic_keys().
411 *
412 * This function implements one step in this evolution of secrets:
413 *
414 * <tt>
415 * old_secret
416 * |
417 * v
418 * Derive-Secret( ., "derived", "" )
419 * |
420 * v
421 * input -> HKDF-Extract = new_secret
422 * </tt>
423 *
424 * \param hash_alg The identifier for the hash function used for the
425 * applications of HKDF.
426 * \param secret_old The address of the buffer holding the old secret
427 * on function entry. If not \c NULL, this must be a
428 * readable buffer whose size matches the output size
429 * of the hash function represented by \p hash_alg.
430 * If \c NULL, an all \c 0 array will be used instead.
431 * \param input The address of the buffer holding the additional
432 * input for the key derivation (e.g., the PSK or the
433 * ephemeral (EC)DH secret). If not \c NULL, this must be
434 * a readable buffer whose size \p input_len Bytes.
435 * If \c NULL, an all \c 0 array will be used instead.
436 * \param input_len The length of \p input in Bytes.
437 * \param secret_new The address of the buffer holding the new secret
438 * on function exit. This must be a writable buffer
439 * whose size matches the output size of the hash
440 * function represented by \p hash_alg.
441 * This may be the same as \p secret_old.
442 *
443 * \returns \c 0 on success.
444 * \returns A negative error code on failure.
445 */
446
447int mbedtls_ssl_tls1_3_evolve_secret(
448 mbedtls_md_type_t hash_alg,
449 const unsigned char *secret_old,
450 const unsigned char *input, size_t input_len,
451 unsigned char *secret_new );
452
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100453#define MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL 0
454#define MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION 1
455
456/**
457 * \brief Calculate a TLS 1.3 PSK binder.
458 *
459 * \param ssl The SSL context. This is used for debugging only and may
460 * be \c NULL if MBEDTLS_DEBUG_C is disabled.
461 * \param md_type The hash algorithm associated to the PSK \p psk.
462 * \param psk The buffer holding the PSK for which to create a binder.
463 * \param psk_len The size of \p psk in bytes.
Hanno Beckerc8d3ccd2021-05-26 04:47:29 +0100464 * \param psk_type This indicates whether the PSK \p psk is externally
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100465 * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
466 * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
467 * \param transcript The handshake transcript up to the point where the
468 * PSK binder calculation happens. This must be readable,
469 * and its size must be equal to the digest size of
470 * the hash algorithm represented by \p md_type.
471 * \param result The address at which to store the PSK binder on success.
472 * This must be writable, and its size must be equal to the
473 * digest size of the hash algorithm represented by
474 * \p md_type.
475 *
476 * \returns \c 0 on success.
477 * \returns A negative error code on failure.
478 */
479int mbedtls_ssl_tls1_3_create_psk_binder( mbedtls_ssl_context *ssl,
480 const mbedtls_md_type_t md_type,
481 unsigned char const *psk, size_t psk_len,
482 int psk_type,
483 unsigned char const *transcript,
484 unsigned char *result );
485
Hanno Beckerc94060c2021-03-22 07:50:44 +0000486/**
487 * \bref Setup an SSL transform structure representing the
488 * record protection mechanism used by TLS 1.3
489 *
490 * \param transform The SSL transform structure to be created. This must have
491 * been initialized through mbedtls_ssl_transform_init() and
492 * not used in any other way prior to calling this function.
493 * In particular, this function does not clean up the
494 * transform structure prior to installing the new keys.
495 * \param endpoint Indicates whether the transform is for the client
496 * (value #MBEDTLS_SSL_IS_CLIENT) or the server
497 * (value #MBEDTLS_SSL_IS_SERVER).
498 * \param ciphersuite The numerical identifier for the ciphersuite to use.
499 * This must be one of the identifiers listed in
500 * ssl_ciphersuites.h.
501 * \param traffic_keys The key material to use. No reference is stored in
502 * the SSL transform being generated, and the caller
503 * should destroy the key material afterwards.
504 * \param ssl (Debug-only) The SSL context to use for debug output
505 * in case of failure. This parameter is only needed if
506 * #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
507 *
508 * \return \c 0 on success. In this case, \p transform is ready to
509 * be used with mbedtls_ssl_transform_decrypt() and
510 * mbedtls_ssl_transform_encrypt().
511 * \return A negative error code on failure.
512 */
513int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
514 int endpoint,
515 int ciphersuite,
516 mbedtls_ssl_key_set const *traffic_keys,
517 mbedtls_ssl_context *ssl );
518
Jerry Yu89ea3212021-09-09 14:31:24 +0800519/*
520 * TLS 1.3 key schedule evolutions
521 *
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800522 * Early -> Handshake -> Application
Jerry Yu89ea3212021-09-09 14:31:24 +0800523 *
524 * Small wrappers around mbedtls_ssl_tls1_3_evolve_secret().
525 */
526
527/**
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800528 * \brief Begin TLS 1.3 key schedule by calculating early secret.
Jerry Yu89ea3212021-09-09 14:31:24 +0800529 *
530 * The TLS 1.3 key schedule can be viewed as a simple state machine
531 * with states Initial -> Early -> Handshake -> Application, and
532 * this function represents the Initial -> Early transition.
533 *
Jerry Yu89ea3212021-09-09 14:31:24 +0800534 * \param ssl The SSL context to operate on.
535 *
536 * \returns \c 0 on success.
537 * \returns A negative error code on failure.
538 */
Jerry Yu48369522021-09-18 16:09:01 +0800539int mbedtls_ssl_tls1_3_key_schedule_stage_early( mbedtls_ssl_context *ssl );
Jerry Yu4925ef52021-09-09 14:42:55 +0800540
Jerry Yu61e35e02021-09-16 18:59:08 +0800541/**
Jerry Yua0650eb2021-09-09 17:14:45 +0800542 * \brief Transition into handshake stage of TLS 1.3 key schedule.
543 *
544 * The TLS 1.3 key schedule can be viewed as a simple state machine
545 * with states Initial -> Early -> Handshake -> Application, and
546 * this function represents the Early -> Handshake transition.
547 *
Jerry Yuc068b662021-10-11 22:30:19 +0800548 * In the handshake stage, mbedtls_ssl_tls13_generate_handshake_keys()
Jerry Yua0650eb2021-09-09 17:14:45 +0800549 * can be used to derive the handshake traffic keys.
550 *
551 * \param ssl The SSL context to operate on. This must be in key schedule
552 * stage \c Early.
553 *
554 * \returns \c 0 on success.
555 * \returns A negative error code on failure.
556 */
Jerry Yuf0ac2352021-10-11 17:47:07 +0800557int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl );
Jerry Yua0650eb2021-09-09 17:14:45 +0800558
559/**
Jerry Yu61e35e02021-09-16 18:59:08 +0800560 * \brief Compute TLS 1.3 handshake traffic keys.
561 *
562 * \param ssl The SSL context to operate on. This must be in
563 * key schedule stage \c Handshake, see
Jerry Yuf0ac2352021-10-11 17:47:07 +0800564 * mbedtls_ssl_tls13_key_schedule_stage_handshake().
Jerry Yu61e35e02021-09-16 18:59:08 +0800565 * \param traffic_keys The address at which to store the handshake traffic key
566 * keys. This must be writable but may be uninitialized.
567 *
568 * \returns \c 0 on success.
569 * \returns A negative error code on failure.
570 */
Jerry Yuc068b662021-10-11 22:30:19 +0800571int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
572 mbedtls_ssl_key_set *traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +0800573
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000574/**
575 * \brief Transition into application stage of TLS 1.3 key schedule.
576 *
577 * The TLS 1.3 key schedule can be viewed as a simple state machine
578 * with states Initial -> Early -> Handshake -> Application, and
579 * this function represents the Handshake -> Application transition.
580 *
581 * In the handshake stage, mbedtls_ssl_tls1_3_generate_application_keys()
582 * can be used to derive the handshake traffic keys.
583 *
584 * \param ssl The SSL context to operate on. This must be in key schedule
585 * stage \c Handshake.
586 *
587 * \returns \c 0 on success.
588 * \returns A negative error code on failure.
589 */
XiaokangQian4cab0242021-10-12 08:43:37 +0000590int mbedtls_ssl_tls13_key_schedule_stage_application(
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000591 mbedtls_ssl_context *ssl );
592
593/**
594 * \brief Compute TLS 1.3 application traffic keys.
595 *
596 * \param ssl The SSL context to operate on. This must be in
597 * key schedule stage \c Application, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000598 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000599 * \param traffic_keys The address at which to store the application traffic key
600 * keys. This must be writable but may be uninitialized.
601 *
602 * \returns \c 0 on success.
603 * \returns A negative error code on failure.
604 */
605int mbedtls_ssl_tls1_3_generate_application_keys(
606 mbedtls_ssl_context* ssl, mbedtls_ssl_key_set *traffic_keys );
607
608/**
609 * \brief Calculate content of TLS 1.3 Finished message.
610 *
611 * \param ssl The SSL context to operate on. This must be in
612 * key schedule stage \c Handshake, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000613 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000614 * \param dst The address at which to write the Finished content.
615 * \param dst_len The size of \p dst in bytes.
616 * \param actual_len The address at which to store the amount of data
617 * actually written to \p dst upon success.
618 * \param from The endpoint the Finished message originates from:
619 * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
620 * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
621 *
622 * \note Both client and server call this function twice, once to
623 * generate their own Finished message, and once to verify the
624 * peer's Finished message.
625
626 * \returns \c 0 on success.
627 * \returns A negative error code on failure.
628 */
XiaokangQiana7634982021-10-22 06:32:32 +0000629int mbedtls_ssl_tls1_3_calculate_expected_finished( mbedtls_ssl_context *ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000630 unsigned char *dst,
631 size_t dst_len,
632 size_t *actual_len,
633 int from );
634
XiaokangQian46c6fc72021-10-22 10:20:28 +0000635#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100636#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */