blob: 14f6e4876c64b7562eef7de589f92c1fd84b190a [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Hanno Beckerbe9d6642020-08-21 13:20:06 +01006 */
7#if !defined(MBEDTLS_SSL_TLS1_3_KEYS_H)
8#define MBEDTLS_SSL_TLS1_3_KEYS_H
9
Hanno Becker70d7fb02020-09-09 10:11:21 +010010/* This requires MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) to be defined at
Xiaofei Bai746f9482021-11-12 08:53:56 +000011 * the point of use. See e.g. the definition of mbedtls_ssl_tls13_labels_union
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010012 * below. */
Jerry Yu0bbb3972021-09-19 20:27:17 +080013#define MBEDTLS_SSL_TLS1_3_LABEL_LIST \
Gilles Peskine449bd832023-01-11 14:50:10 +010014 MBEDTLS_SSL_TLS1_3_LABEL(finished, "finished") \
15 MBEDTLS_SSL_TLS1_3_LABEL(resumption, "resumption") \
16 MBEDTLS_SSL_TLS1_3_LABEL(traffic_upd, "traffic upd") \
17 MBEDTLS_SSL_TLS1_3_LABEL(exporter, "exporter") \
18 MBEDTLS_SSL_TLS1_3_LABEL(key, "key") \
19 MBEDTLS_SSL_TLS1_3_LABEL(iv, "iv") \
20 MBEDTLS_SSL_TLS1_3_LABEL(c_hs_traffic, "c hs traffic") \
21 MBEDTLS_SSL_TLS1_3_LABEL(c_ap_traffic, "c ap traffic") \
22 MBEDTLS_SSL_TLS1_3_LABEL(c_e_traffic, "c e traffic") \
23 MBEDTLS_SSL_TLS1_3_LABEL(s_hs_traffic, "s hs traffic") \
24 MBEDTLS_SSL_TLS1_3_LABEL(s_ap_traffic, "s ap traffic") \
25 MBEDTLS_SSL_TLS1_3_LABEL(s_e_traffic, "s e traffic") \
26 MBEDTLS_SSL_TLS1_3_LABEL(e_exp_master, "e exp master") \
27 MBEDTLS_SSL_TLS1_3_LABEL(res_master, "res master") \
28 MBEDTLS_SSL_TLS1_3_LABEL(exp_master, "exp master") \
29 MBEDTLS_SSL_TLS1_3_LABEL(ext_binder, "ext binder") \
30 MBEDTLS_SSL_TLS1_3_LABEL(res_binder, "res binder") \
31 MBEDTLS_SSL_TLS1_3_LABEL(derived, "derived") \
32 MBEDTLS_SSL_TLS1_3_LABEL(client_cv, "TLS 1.3, client CertificateVerify") \
33 MBEDTLS_SSL_TLS1_3_LABEL(server_cv, "TLS 1.3, server CertificateVerify")
Hanno Beckere4435ea2020-09-08 10:43:52 +010034
Gabor Mezeie42d8bf2022-03-30 11:33:06 +020035#define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
36#define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
37
38#define MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL 0
39#define MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION 1
40
41#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
42
Gilles Peskine449bd832023-01-11 14:50:10 +010043#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
44 const unsigned char name [sizeof(string) - 1];
Hanno Beckerbe9d6642020-08-21 13:20:06 +010045
Gilles Peskine449bd832023-01-11 14:50:10 +010046union mbedtls_ssl_tls13_labels_union {
Hanno Beckerbe9d6642020-08-21 13:20:06 +010047 MBEDTLS_SSL_TLS1_3_LABEL_LIST
48};
Gilles Peskine449bd832023-01-11 14:50:10 +010049struct mbedtls_ssl_tls13_labels_struct {
Hanno Beckerbe9d6642020-08-21 13:20:06 +010050 MBEDTLS_SSL_TLS1_3_LABEL_LIST
51};
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010052#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010053
Xiaofei Bai746f9482021-11-12 08:53:56 +000054extern const struct mbedtls_ssl_tls13_labels_struct mbedtls_ssl_tls13_labels;
Hanno Beckerbe9d6642020-08-21 13:20:06 +010055
Gilles Peskine449bd832023-01-11 14:50:10 +010056#define MBEDTLS_SSL_TLS1_3_LBL_LEN(LABEL) \
Xiaofei Bai746f9482021-11-12 08:53:56 +000057 sizeof(mbedtls_ssl_tls13_labels.LABEL)
Jerry Yu0bbb3972021-09-19 20:27:17 +080058
Gilles Peskine449bd832023-01-11 14:50:10 +010059#define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(LABEL) \
Xiaofei Bai746f9482021-11-12 08:53:56 +000060 mbedtls_ssl_tls13_labels.LABEL, \
Gilles Peskine449bd832023-01-11 14:50:10 +010061 MBEDTLS_SSL_TLS1_3_LBL_LEN(LABEL)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010062
Max Fillinger58268832024-11-25 20:38:04 +010063/* Maximum length of the label field in the HkdfLabel struct defined in
64 * RFC 8446, Section 7.1, excluding the "tls13 " prefix. */
65#define MBEDTLS_SSL_TLS1_3_HKDF_LABEL_MAX_LABEL_LEN 249
Hanno Beckerbe9d6642020-08-21 13:20:06 +010066
Hanno Becker61baae72020-09-16 09:24:14 +010067/* The maximum length of HKDF contexts used in the TLS 1.3 standard.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010068 * Since contexts are always hashes of message transcripts, this can
69 * be approximated from above by the maximum hash size. */
70#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
Przemyslaw Stekielab9b9d42022-09-14 13:51:07 +020071 PSA_HASH_MAX_SIZE
Hanno Beckerbe9d6642020-08-21 13:20:06 +010072
73/* Maximum desired length for expanded key material generated
Max Fillinger28916ac2024-10-29 18:49:30 +010074 * by HKDF-Expand-Label. This algorithm can output up to 255 * hash_size
75 * bytes of key material where hash_size is the output size of the
76 * underlying hash function. */
77#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN \
78 (255 * MBEDTLS_TLS1_3_MD_MAX_SIZE)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010079
80/**
Xiaofei Bai89b526d2021-11-23 06:31:16 +000081 * \brief The \c HKDF-Expand-Label function from
82 * the TLS 1.3 standard RFC 8446.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010083 *
84 * <tt>
Xiaofei Bai89b526d2021-11-23 06:31:16 +000085 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010086 * HKDF-Expand( Secret, HkdfLabel, Length )
87 * </tt>
88 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +000089 * \param hash_alg The identifier for the hash algorithm to use.
90 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000091 * This must be a readable buffer of length
92 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +000093 * \param secret_len The length of \p secret in Bytes.
94 * \param label The \c Label argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000095 * This must be a readable buffer of length
96 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +000097 * \param label_len The length of \p label in Bytes.
98 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000099 * This must be a readable buffer of length \p ctx_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000100 * \param ctx_len The length of \p context in Bytes.
101 * \param buf The destination buffer to hold the expanded secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000102 * This must be a writable buffer of length \p buf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000103 * \param buf_len The desired size of the expanded secret in Bytes.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100104 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000105 * \returns \c 0 on success.
106 * \return A negative error code on failure.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100107 */
108
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200109MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000110int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 psa_algorithm_t hash_alg,
112 const unsigned char *secret, size_t secret_len,
113 const unsigned char *label, size_t label_len,
114 const unsigned char *ctx, size_t ctx_len,
115 unsigned char *buf, size_t buf_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100116
Hanno Becker3385a4d2020-08-21 13:03:34 +0100117/**
118 * \brief This function is part of the TLS 1.3 key schedule.
119 * It extracts key and IV for the actual client/server traffic
120 * from the client/server traffic secrets.
121 *
122 * From RFC 8446:
123 *
124 * <tt>
125 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
126 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
127 * </tt>
128 *
129 * \param hash_alg The identifier for the hash algorithm to be used
130 * for the HKDF-based expansion of the secret.
131 * \param client_secret The client traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000132 * This must be a readable buffer of size
133 * \p secret_len Bytes
Hanno Becker3385a4d2020-08-21 13:03:34 +0100134 * \param server_secret The server traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000135 * This must be a readable buffer of size
136 * \p secret_len Bytes
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000137 * \param secret_len Length of the secrets \p client_secret and
Hanno Becker3385a4d2020-08-21 13:03:34 +0100138 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100139 * \param key_len The desired length of the key to be extracted in Bytes.
140 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100141 * \param keys The address of the structure holding the generated
142 * keys and IVs.
143 *
144 * \returns \c 0 on success.
145 * \returns A negative error code on failure.
146 */
147
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200148MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000149int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 psa_algorithm_t hash_alg,
151 const unsigned char *client_secret,
152 const unsigned char *server_secret, size_t secret_len,
153 size_t key_len, size_t iv_len,
154 mbedtls_ssl_key_set *keys);
Hanno Becker3385a4d2020-08-21 13:03:34 +0100155
Hanno Beckerb35d5222020-08-21 13:27:44 +0100156/**
157 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
158 *
159 * <tt>
160 * Derive-Secret( Secret, Label, Messages ) =
161 * HKDF-Expand-Label( Secret, Label,
162 * Hash( Messages ),
163 * Hash.Length ) )
164 * </tt>
165 *
Hanno Becker0c42fd92020-09-09 12:58:29 +0100166 * \param hash_alg The identifier for the hash function used for the
167 * applications of HKDF.
168 * \param secret The \c Secret argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000169 * This must be a readable buffer of length
170 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000171 * \param secret_len The length of \p secret in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100172 * \param label The \c Label argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000173 * This must be a readable buffer of length
174 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000175 * \param label_len The length of \p label in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100176 * \param ctx The hash of the \c Messages argument to the
177 * \c Derive-Secret function, or the \c Messages argument
Xiaofei Baid25fab62021-12-02 06:36:27 +0000178 * itself, depending on \p ctx_hashed.
179 * \param ctx_len The length of \p ctx in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100180 * \param ctx_hashed This indicates whether the \p ctx contains the hash of
181 * the \c Messages argument in the application of the
182 * \c Derive-Secret function
183 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
184 * it is the content of \c Messages itself, in which case
185 * the function takes care of the hashing
186 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
187 * \param dstbuf The target buffer to write the output of
188 * \c Derive-Secret to. This must be a writable buffer of
Xiaofei Baid25fab62021-12-02 06:36:27 +0000189 * size \p dtsbuf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000190 * \param dstbuf_len The length of \p dstbuf in Bytes.
Hanno Beckerb35d5222020-08-21 13:27:44 +0100191 *
192 * \returns \c 0 on success.
193 * \returns A negative error code on failure.
194 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200195MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000196int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 psa_algorithm_t hash_alg,
198 const unsigned char *secret, size_t secret_len,
199 const unsigned char *label, size_t label_len,
200 const unsigned char *ctx, size_t ctx_len,
201 int ctx_hashed,
202 unsigned char *dstbuf, size_t dstbuf_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100203
Hanno Beckere9cccb42020-08-20 13:42:46 +0100204/**
Hanno Beckeref5235b2021-05-24 06:39:41 +0100205 * \brief Derive TLS 1.3 early data key material from early secret.
206 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000207 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100208 * with the appropriate labels.
209 *
210 * <tt>
211 * Early Secret
212 * |
213 * +-----> Derive-Secret(., "c e traffic", ClientHello)
214 * | = client_early_traffic_secret
215 * |
216 * +-----> Derive-Secret(., "e exp master", ClientHello)
217 * . = early_exporter_master_secret
218 * .
219 * .
220 * </tt>
221 *
222 * \note To obtain the actual key and IV for the early data traffic,
223 * the client secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000224 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100225 *
226 * \note The binder key, which is also generated from the early secret,
227 * is omitted here. Its calculation is part of the separate routine
Xiaofei Bai746f9482021-11-12 08:53:56 +0000228 * mbedtls_ssl_tls13_create_psk_binder().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100229 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100230 * \param hash_alg The hash algorithm associated with the PSK for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100231 * early data key material is being derived.
232 * \param early_secret The early secret from which the early data key material
233 * should be derived. This must be a readable buffer whose
234 * length is the digest size of the hash algorithm
235 * represented by \p md_size.
236 * \param transcript The transcript of the handshake so far, calculated with
Gabor Mezei07732f72022-03-26 17:04:19 +0100237 * respect to \p hash_alg. This must be a readable buffer
Hanno Beckeref5235b2021-05-24 06:39:41 +0100238 * whose length is the digest size of the hash algorithm
239 * represented by \p md_size.
240 * \param derived The address of the structure in which to store
241 * the early data key material.
242 *
243 * \returns \c 0 on success.
244 * \returns A negative error code on failure.
245 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200246MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000247int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 psa_algorithm_t hash_alg,
249 unsigned char const *early_secret,
250 unsigned char const *transcript, size_t transcript_len,
251 mbedtls_ssl_tls13_early_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100252
253/**
254 * \brief Derive TLS 1.3 handshake key material from the handshake secret.
255 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000256 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100257 * with the appropriate labels from the standard.
258 *
259 * <tt>
260 * Handshake Secret
261 * |
262 * +-----> Derive-Secret( ., "c hs traffic",
263 * | ClientHello...ServerHello )
264 * | = client_handshake_traffic_secret
265 * |
266 * +-----> Derive-Secret( ., "s hs traffic",
267 * . ClientHello...ServerHello )
268 * . = server_handshake_traffic_secret
269 * .
270 * </tt>
271 *
272 * \note To obtain the actual key and IV for the encrypted handshake traffic,
273 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000274 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100275 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100276 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100277 * that's being used for the connection.
278 * \param handshake_secret The handshake secret from which the handshake key
279 * material should be derived. This must be a readable
280 * buffer whose length is the digest size of the hash
281 * algorithm represented by \p md_size.
282 * \param transcript The transcript of the handshake so far, calculated
Gabor Mezei07732f72022-03-26 17:04:19 +0100283 * with respect to \p hash_alg. This must be a readable
Hanno Beckeref5235b2021-05-24 06:39:41 +0100284 * buffer whose length is the digest size of the hash
285 * algorithm represented by \p md_size.
286 * \param derived The address of the structure in which to
287 * store the handshake key material.
288 *
289 * \returns \c 0 on success.
290 * \returns A negative error code on failure.
291 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200292MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000293int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 psa_algorithm_t hash_alg,
295 unsigned char const *handshake_secret,
296 unsigned char const *transcript, size_t transcript_len,
297 mbedtls_ssl_tls13_handshake_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100298
299/**
300 * \brief Derive TLS 1.3 application key material from the master secret.
301 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000302 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100303 * with the appropriate labels from the standard.
304 *
305 * <tt>
306 * Master Secret
307 * |
308 * +-----> Derive-Secret( ., "c ap traffic",
309 * | ClientHello...server Finished )
310 * | = client_application_traffic_secret_0
311 * |
312 * +-----> Derive-Secret( ., "s ap traffic",
313 * | ClientHello...Server Finished )
314 * | = server_application_traffic_secret_0
315 * |
316 * +-----> Derive-Secret( ., "exp master",
317 * . ClientHello...server Finished)
318 * . = exporter_master_secret
319 * .
320 * </tt>
321 *
322 * \note To obtain the actual key and IV for the (0-th) application traffic,
323 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000324 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100325 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100326 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100327 * that's being used for the connection.
328 * \param master_secret The master secret from which the application key
329 * material should be derived. This must be a readable
330 * buffer whose length is the digest size of the hash
331 * algorithm represented by \p md_size.
332 * \param transcript The transcript of the handshake up to and including
333 * the ServerFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100334 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100335 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100336 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100337 * \param derived The address of the structure in which to
338 * store the application key material.
339 *
340 * \returns \c 0 on success.
341 * \returns A negative error code on failure.
342 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200343MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000344int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 psa_algorithm_t hash_alg,
346 unsigned char const *master_secret,
347 unsigned char const *transcript, size_t transcript_len,
348 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 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000353 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100354 * with the appropriate labels from the standard.
355 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100356 * \param hash_alg The hash algorithm used in the application for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100357 * 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
Gabor Mezei07732f72022-03-26 17:04:19 +0100364 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100365 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100366 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100367 * \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 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200374MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000375int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 psa_algorithm_t hash_alg,
377 unsigned char const *application_secret,
378 unsigned char const *transcript, size_t transcript_len,
379 mbedtls_ssl_tls13_application_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100380
381/**
Hanno Beckere9cccb42020-08-20 13:42:46 +0100382 * \brief Compute the next secret in the TLS 1.3 key schedule
383 *
384 * The TLS 1.3 key schedule proceeds as follows to compute
385 * the three main secrets during the handshake: The early
386 * secret for early data, the handshake secret for all
387 * other encrypted handshake messages, and the master
388 * secret for all application traffic.
389 *
390 * <tt>
391 * 0
392 * |
393 * v
394 * PSK -> HKDF-Extract = Early Secret
395 * |
396 * v
397 * Derive-Secret( ., "derived", "" )
398 * |
399 * v
400 * (EC)DHE -> HKDF-Extract = Handshake Secret
401 * |
402 * v
403 * Derive-Secret( ., "derived", "" )
404 * |
405 * v
406 * 0 -> HKDF-Extract = Master Secret
407 * </tt>
408 *
409 * Each of the three secrets in turn is the basis for further
410 * key derivations, such as the derivation of traffic keys and IVs;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000411 * see e.g. mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckere9cccb42020-08-20 13:42:46 +0100412 *
413 * This function implements one step in this evolution of secrets:
414 *
415 * <tt>
416 * old_secret
417 * |
418 * v
419 * Derive-Secret( ., "derived", "" )
420 * |
421 * v
422 * input -> HKDF-Extract = new_secret
423 * </tt>
424 *
425 * \param hash_alg The identifier for the hash function used for the
426 * applications of HKDF.
427 * \param secret_old The address of the buffer holding the old secret
428 * on function entry. If not \c NULL, this must be a
429 * readable buffer whose size matches the output size
430 * of the hash function represented by \p hash_alg.
431 * If \c NULL, an all \c 0 array will be used instead.
432 * \param input The address of the buffer holding the additional
433 * input for the key derivation (e.g., the PSK or the
434 * ephemeral (EC)DH secret). If not \c NULL, this must be
435 * a readable buffer whose size \p input_len Bytes.
436 * If \c NULL, an all \c 0 array will be used instead.
437 * \param input_len The length of \p input in Bytes.
438 * \param secret_new The address of the buffer holding the new secret
439 * on function exit. This must be a writable buffer
440 * whose size matches the output size of the hash
441 * function represented by \p hash_alg.
442 * This may be the same as \p secret_old.
443 *
444 * \returns \c 0 on success.
445 * \returns A negative error code on failure.
446 */
447
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200448MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000449int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 psa_algorithm_t hash_alg,
451 const unsigned char *secret_old,
452 const unsigned char *input, size_t input_len,
453 unsigned char *secret_new);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100454
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100455/**
456 * \brief Calculate a TLS 1.3 PSK binder.
457 *
458 * \param ssl The SSL context. This is used for debugging only and may
459 * be \c NULL if MBEDTLS_DEBUG_C is disabled.
Gabor Mezei07732f72022-03-26 17:04:19 +0100460 * \param hash_alg The hash algorithm associated to the PSK \p psk.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100461 * \param psk The buffer holding the PSK for which to create a binder.
462 * \param psk_len The size of \p psk in bytes.
Hanno Beckerc8d3ccd2021-05-26 04:47:29 +0100463 * \param psk_type This indicates whether the PSK \p psk is externally
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100464 * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
465 * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
466 * \param transcript The handshake transcript up to the point where the
467 * PSK binder calculation happens. This must be readable,
468 * and its size must be equal to the digest size of
Gabor Mezei07732f72022-03-26 17:04:19 +0100469 * the hash algorithm represented by \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100470 * \param result The address at which to store the PSK binder on success.
471 * This must be writable, and its size must be equal to the
472 * digest size of the hash algorithm represented by
Gabor Mezei07732f72022-03-26 17:04:19 +0100473 * \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100474 *
475 * \returns \c 0 on success.
476 * \returns A negative error code on failure.
477 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200478MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100479int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
480 const psa_algorithm_t hash_alg,
481 unsigned char const *psk, size_t psk_len,
482 int psk_type,
483 unsigned char const *transcript,
484 unsigned char *result);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100485
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 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200513MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100514int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
515 int endpoint,
516 int ciphersuite,
517 mbedtls_ssl_key_set const *traffic_keys,
518 mbedtls_ssl_context *ssl);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000519
Jerry Yu89ea3212021-09-09 14:31:24 +0800520/*
521 * TLS 1.3 key schedule evolutions
522 *
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800523 * Early -> Handshake -> Application
Jerry Yu89ea3212021-09-09 14:31:24 +0800524 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000525 * Small wrappers around mbedtls_ssl_tls13_evolve_secret().
Jerry Yu89ea3212021-09-09 14:31:24 +0800526 */
527
528/**
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800529 * \brief Begin TLS 1.3 key schedule by calculating early secret.
Jerry Yu89ea3212021-09-09 14:31:24 +0800530 *
531 * The TLS 1.3 key schedule can be viewed as a simple state machine
532 * with states Initial -> Early -> Handshake -> Application, and
533 * this function represents the Initial -> Early transition.
534 *
Jerry Yu89ea3212021-09-09 14:31:24 +0800535 * \param ssl The SSL context to operate on.
536 *
537 * \returns \c 0 on success.
538 * \returns A negative error code on failure.
539 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200540MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100541int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl);
Jerry Yu4925ef52021-09-09 14:42:55 +0800542
Jerry Yu61e35e02021-09-16 18:59:08 +0800543/**
Jerry Yuff226982022-04-16 16:52:57 +0800544 * \brief Compute TLS 1.3 resumption master secret.
545 *
546 * \param ssl The SSL context to operate on. This must be in
547 * key schedule stage \c Application, see
548 * mbedtls_ssl_tls13_key_schedule_stage_application().
549 *
550 * \returns \c 0 on success.
551 * \returns A negative error code on failure.
552 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200553MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100554int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl);
Jerry Yuff226982022-04-16 16:52:57 +0800555
556/**
XiaokangQianc5c39d52021-11-09 11:55:10 +0000557 * \brief Calculate the verify_data value for the client or server TLS 1.3
558 * Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000559 *
560 * \param ssl The SSL context to operate on. This must be in
561 * key schedule stage \c Handshake, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000562 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianc5c39d52021-11-09 11:55:10 +0000563 * \param dst The address at which to write the verify_data value.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000564 * \param dst_len The size of \p dst in bytes.
565 * \param actual_len The address at which to store the amount of data
566 * actually written to \p dst upon success.
XiaokangQianaaa0e192021-11-10 03:07:04 +0000567 * \param which The message to calculate the `verify_data` for:
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000568 * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
569 * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
570 *
571 * \note Both client and server call this function twice, once to
572 * generate their own Finished message, and once to verify the
573 * peer's Finished message.
574
575 * \returns \c 0 on success.
576 * \returns A negative error code on failure.
577 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200578MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100579int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
580 unsigned char *dst,
581 size_t dst_len,
582 size_t *actual_len,
583 int which);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000584
Jerry Yu91b560f2022-11-04 14:10:34 +0800585#if defined(MBEDTLS_SSL_EARLY_DATA)
586/**
587 * \brief Compute TLS 1.3 early transform
588 *
Jerry Yub094e122022-11-21 13:03:47 +0800589 * \param ssl The SSL context to operate on.
Jerry Yu91b560f2022-11-04 14:10:34 +0800590 *
591 * \returns \c 0 on success.
592 * \returns A negative error code on failure.
593 *
Jerry Yu3ce61ff2022-11-21 22:45:58 +0800594 * \warning The function does not compute the early master secret. Call
595 * mbedtls_ssl_tls13_key_schedule_stage_early() before to
596 * call this function to generate the early master secret.
597 * \note For a client/server endpoint, the function computes only the
598 * encryption/decryption part of the transform as the decryption/
599 * encryption part is not defined by the specification (no early
600 * traffic from the server to the client).
Jerry Yu91b560f2022-11-04 14:10:34 +0800601 */
602MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100603int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl);
Jerry Yu91b560f2022-11-04 14:10:34 +0800604#endif /* MBEDTLS_SSL_EARLY_DATA */
605
Jerry Yue110d252022-05-05 10:19:22 +0800606/**
607 * \brief Compute TLS 1.3 handshake transform
608 *
Jerry Yuf86eb752022-05-06 11:16:55 +0800609 * \param ssl The SSL context to operate on. The early secret must have been
Jerry Yue110d252022-05-05 10:19:22 +0800610 * computed.
611 *
612 * \returns \c 0 on success.
613 * \returns A negative error code on failure.
614 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200615MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100616int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl);
Jerry Yue110d252022-05-05 10:19:22 +0800617
Jerry Yufd5ea042022-05-19 14:29:48 +0800618/**
619 * \brief Compute TLS 1.3 application transform
620 *
621 * \param ssl The SSL context to operate on. The early secret must have been
622 * computed.
623 *
624 * \returns \c 0 on success.
625 * \returns A negative error code on failure.
626 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200627MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100628int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl);
Jerry Yufd5ea042022-05-19 14:29:48 +0800629
Ronald Cron41a443a2022-10-04 16:38:25 +0200630#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu40f37712022-07-26 16:58:57 +0800631/**
Jerry Yu6cf6b472022-08-16 14:50:28 +0800632 * \brief Export TLS 1.3 PSK from handshake context
Jerry Yu40f37712022-07-26 16:58:57 +0800633 *
Jerry Yu01e42d22022-08-21 12:55:51 +0800634 * \param[in] ssl The SSL context to operate on.
635 * \param[out] psk PSK output pointer.
636 * \param[out] psk_len Length of PSK.
Jerry Yu40f37712022-07-26 16:58:57 +0800637 *
Jerry Yu29d9faa2022-08-23 17:52:45 +0800638 * \returns \c 0 if there is a configured PSK and it was exported
639 * successfully.
640 * \returns A negative error code on failure.
Jerry Yu40f37712022-07-26 16:58:57 +0800641 */
642MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100643int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
644 unsigned char **psk,
645 size_t *psk_len);
Ronald Cron41a443a2022-10-04 16:38:25 +0200646#endif
Jerry Yu40f37712022-07-26 16:58:57 +0800647
Max Fillingerbd81c9d2024-07-22 14:43:56 +0200648/**
649 * \brief Calculate TLS-Exporter function as defined in RFC 8446, Section 7.5.
650 *
651 * \param[in] hash_alg The hash algorithm.
652 * \param[in] secret The secret to use. (Should be the exporter master secret.)
653 * \param[in] secret_len Length of secret.
654 * \param[in] label The label of the exported key.
655 * \param[in] label_len The length of label.
656 * \param[out] out The output buffer for the exported key. Must have room for at least out_len bytes.
657 * \param[in] out_len Length of the key to generate.
Max Fillinger136fe9e2024-08-09 18:54:36 +0200658 */
Max Fillinger9c9989f2024-08-14 16:44:50 +0200659int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
660 const unsigned char *secret, const size_t secret_len,
661 const unsigned char *label, const size_t label_len,
662 const unsigned char *context_value, const size_t context_len,
663 uint8_t *out, const size_t out_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +0200664
Gabor Mezeie42d8bf2022-03-30 11:33:06 +0200665#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
666
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100667#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */