blob: 31ffe4481e0a9ef6a2b778936a05d017c484c417 [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
63#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
Gilles Peskine449bd832023-01-11 14:50:10 +010064 sizeof(union mbedtls_ssl_tls13_labels_union)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010065
Hanno Becker61baae72020-09-16 09:24:14 +010066/* The maximum length of HKDF contexts used in the TLS 1.3 standard.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010067 * Since contexts are always hashes of message transcripts, this can
68 * be approximated from above by the maximum hash size. */
69#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
Przemyslaw Stekielab9b9d42022-09-14 13:51:07 +020070 PSA_HASH_MAX_SIZE
Hanno Beckerbe9d6642020-08-21 13:20:06 +010071
72/* Maximum desired length for expanded key material generated
Max Fillinger28916ac2024-10-29 18:49:30 +010073 * by HKDF-Expand-Label. This algorithm can output up to 255 * hash_size
74 * bytes of key material where hash_size is the output size of the
75 * underlying hash function. */
76#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN \
77 (255 * MBEDTLS_TLS1_3_MD_MAX_SIZE)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010078
79/**
Xiaofei Bai89b526d2021-11-23 06:31:16 +000080 * \brief The \c HKDF-Expand-Label function from
81 * the TLS 1.3 standard RFC 8446.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010082 *
83 * <tt>
Xiaofei Bai89b526d2021-11-23 06:31:16 +000084 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010085 * HKDF-Expand( Secret, HkdfLabel, Length )
86 * </tt>
87 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +000088 * \param hash_alg The identifier for the hash algorithm to use.
89 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000090 * This must be a readable buffer of length
91 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +000092 * \param secret_len The length of \p secret in Bytes.
93 * \param label The \c Label argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000094 * This must be a readable buffer of length
95 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +000096 * \param label_len The length of \p label in Bytes.
97 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000098 * This must be a readable buffer of length \p ctx_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +000099 * \param ctx_len The length of \p context in Bytes.
100 * \param buf The destination buffer to hold the expanded secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000101 * This must be a writable buffer of length \p buf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000102 * \param buf_len The desired size of the expanded secret in Bytes.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100103 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000104 * \returns \c 0 on success.
105 * \return A negative error code on failure.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100106 */
107
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200108MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000109int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 psa_algorithm_t hash_alg,
111 const unsigned char *secret, size_t secret_len,
112 const unsigned char *label, size_t label_len,
113 const unsigned char *ctx, size_t ctx_len,
114 unsigned char *buf, size_t buf_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100115
Hanno Becker3385a4d2020-08-21 13:03:34 +0100116/**
117 * \brief This function is part of the TLS 1.3 key schedule.
118 * It extracts key and IV for the actual client/server traffic
119 * from the client/server traffic secrets.
120 *
121 * From RFC 8446:
122 *
123 * <tt>
124 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
125 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
126 * </tt>
127 *
128 * \param hash_alg The identifier for the hash algorithm to be used
129 * for the HKDF-based expansion of the secret.
130 * \param client_secret The client traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000131 * This must be a readable buffer of size
132 * \p secret_len Bytes
Hanno Becker3385a4d2020-08-21 13:03:34 +0100133 * \param server_secret The server traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000134 * This must be a readable buffer of size
135 * \p secret_len Bytes
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000136 * \param secret_len Length of the secrets \p client_secret and
Hanno Becker3385a4d2020-08-21 13:03:34 +0100137 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100138 * \param key_len The desired length of the key to be extracted in Bytes.
139 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100140 * \param keys The address of the structure holding the generated
141 * keys and IVs.
142 *
143 * \returns \c 0 on success.
144 * \returns A negative error code on failure.
145 */
146
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200147MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000148int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 psa_algorithm_t hash_alg,
150 const unsigned char *client_secret,
151 const unsigned char *server_secret, size_t secret_len,
152 size_t key_len, size_t iv_len,
153 mbedtls_ssl_key_set *keys);
Hanno Becker3385a4d2020-08-21 13:03:34 +0100154
Hanno Beckerb35d5222020-08-21 13:27:44 +0100155/**
156 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
157 *
158 * <tt>
159 * Derive-Secret( Secret, Label, Messages ) =
160 * HKDF-Expand-Label( Secret, Label,
161 * Hash( Messages ),
162 * Hash.Length ) )
163 * </tt>
164 *
Hanno Becker0c42fd92020-09-09 12:58:29 +0100165 * \param hash_alg The identifier for the hash function used for the
166 * applications of HKDF.
167 * \param secret The \c Secret argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000168 * This must be a readable buffer of length
169 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000170 * \param secret_len The length of \p secret in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100171 * \param label The \c Label argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000172 * This must be a readable buffer of length
173 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000174 * \param label_len The length of \p label in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100175 * \param ctx The hash of the \c Messages argument to the
176 * \c Derive-Secret function, or the \c Messages argument
Xiaofei Baid25fab62021-12-02 06:36:27 +0000177 * itself, depending on \p ctx_hashed.
178 * \param ctx_len The length of \p ctx in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100179 * \param ctx_hashed This indicates whether the \p ctx contains the hash of
180 * the \c Messages argument in the application of the
181 * \c Derive-Secret function
182 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
183 * it is the content of \c Messages itself, in which case
184 * the function takes care of the hashing
185 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
186 * \param dstbuf The target buffer to write the output of
187 * \c Derive-Secret to. This must be a writable buffer of
Xiaofei Baid25fab62021-12-02 06:36:27 +0000188 * size \p dtsbuf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000189 * \param dstbuf_len The length of \p dstbuf in Bytes.
Hanno Beckerb35d5222020-08-21 13:27:44 +0100190 *
191 * \returns \c 0 on success.
192 * \returns A negative error code on failure.
193 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200194MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000195int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 psa_algorithm_t hash_alg,
197 const unsigned char *secret, size_t secret_len,
198 const unsigned char *label, size_t label_len,
199 const unsigned char *ctx, size_t ctx_len,
200 int ctx_hashed,
201 unsigned char *dstbuf, size_t dstbuf_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100202
Hanno Beckere9cccb42020-08-20 13:42:46 +0100203/**
Hanno Beckeref5235b2021-05-24 06:39:41 +0100204 * \brief Derive TLS 1.3 early data key material from early secret.
205 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000206 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100207 * with the appropriate labels.
208 *
209 * <tt>
210 * Early Secret
211 * |
212 * +-----> Derive-Secret(., "c e traffic", ClientHello)
213 * | = client_early_traffic_secret
214 * |
215 * +-----> Derive-Secret(., "e exp master", ClientHello)
216 * . = early_exporter_master_secret
217 * .
218 * .
219 * </tt>
220 *
221 * \note To obtain the actual key and IV for the early data traffic,
222 * the client secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000223 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100224 *
225 * \note The binder key, which is also generated from the early secret,
226 * is omitted here. Its calculation is part of the separate routine
Xiaofei Bai746f9482021-11-12 08:53:56 +0000227 * mbedtls_ssl_tls13_create_psk_binder().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100228 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100229 * \param hash_alg The hash algorithm associated with the PSK for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100230 * early data key material is being derived.
231 * \param early_secret The early secret from which the early data key material
232 * should be derived. This must be a readable buffer whose
233 * length is the digest size of the hash algorithm
234 * represented by \p md_size.
235 * \param transcript The transcript of the handshake so far, calculated with
Gabor Mezei07732f72022-03-26 17:04:19 +0100236 * respect to \p hash_alg. This must be a readable buffer
Hanno Beckeref5235b2021-05-24 06:39:41 +0100237 * whose length is the digest size of the hash algorithm
238 * represented by \p md_size.
239 * \param derived The address of the structure in which to store
240 * the early data key material.
241 *
242 * \returns \c 0 on success.
243 * \returns A negative error code on failure.
244 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200245MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000246int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 psa_algorithm_t hash_alg,
248 unsigned char const *early_secret,
249 unsigned char const *transcript, size_t transcript_len,
250 mbedtls_ssl_tls13_early_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100251
252/**
253 * \brief Derive TLS 1.3 handshake key material from the handshake secret.
254 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000255 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100256 * with the appropriate labels from the standard.
257 *
258 * <tt>
259 * Handshake Secret
260 * |
261 * +-----> Derive-Secret( ., "c hs traffic",
262 * | ClientHello...ServerHello )
263 * | = client_handshake_traffic_secret
264 * |
265 * +-----> Derive-Secret( ., "s hs traffic",
266 * . ClientHello...ServerHello )
267 * . = server_handshake_traffic_secret
268 * .
269 * </tt>
270 *
271 * \note To obtain the actual key and IV for the encrypted handshake traffic,
272 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000273 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100274 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100275 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100276 * that's being used for the connection.
277 * \param handshake_secret The handshake secret from which the handshake key
278 * material should be derived. This must be a readable
279 * buffer whose length is the digest size of the hash
280 * algorithm represented by \p md_size.
281 * \param transcript The transcript of the handshake so far, calculated
Gabor Mezei07732f72022-03-26 17:04:19 +0100282 * with respect to \p hash_alg. This must be a readable
Hanno Beckeref5235b2021-05-24 06:39:41 +0100283 * buffer whose length is the digest size of the hash
284 * algorithm represented by \p md_size.
285 * \param derived The address of the structure in which to
286 * store the handshake key material.
287 *
288 * \returns \c 0 on success.
289 * \returns A negative error code on failure.
290 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200291MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000292int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 psa_algorithm_t hash_alg,
294 unsigned char const *handshake_secret,
295 unsigned char const *transcript, size_t transcript_len,
296 mbedtls_ssl_tls13_handshake_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100297
298/**
299 * \brief Derive TLS 1.3 application key material from the master secret.
300 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000301 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100302 * with the appropriate labels from the standard.
303 *
304 * <tt>
305 * Master Secret
306 * |
307 * +-----> Derive-Secret( ., "c ap traffic",
308 * | ClientHello...server Finished )
309 * | = client_application_traffic_secret_0
310 * |
311 * +-----> Derive-Secret( ., "s ap traffic",
312 * | ClientHello...Server Finished )
313 * | = server_application_traffic_secret_0
314 * |
315 * +-----> Derive-Secret( ., "exp master",
316 * . ClientHello...server Finished)
317 * . = exporter_master_secret
318 * .
319 * </tt>
320 *
321 * \note To obtain the actual key and IV for the (0-th) application traffic,
322 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000323 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100324 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100325 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100326 * that's being used for the connection.
327 * \param master_secret The master secret from which the application key
328 * material should be derived. This must be a readable
329 * buffer whose length is the digest size of the hash
330 * algorithm represented by \p md_size.
331 * \param transcript The transcript of the handshake up to and including
332 * the ServerFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100333 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100334 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100335 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100336 * \param derived The address of the structure in which to
337 * store the application key material.
338 *
339 * \returns \c 0 on success.
340 * \returns A negative error code on failure.
341 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200342MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000343int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 psa_algorithm_t hash_alg,
345 unsigned char const *master_secret,
346 unsigned char const *transcript, size_t transcript_len,
347 mbedtls_ssl_tls13_application_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100348
349/**
350 * \brief Derive TLS 1.3 resumption master secret from the master secret.
351 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000352 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100353 * with the appropriate labels from the standard.
354 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100355 * \param hash_alg The hash algorithm used in the application for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100356 * key material is being derived.
357 * \param application_secret The application secret from which the resumption master
358 * secret should be derived. This must be a readable
359 * buffer whose length is the digest size of the hash
360 * algorithm represented by \p md_size.
361 * \param transcript The transcript of the handshake up to and including
362 * the ClientFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100363 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100364 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100365 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100366 * \param transcript_len The length of \p transcript in Bytes.
367 * \param derived The address of the structure in which to
368 * store the resumption master secret.
369 *
370 * \returns \c 0 on success.
371 * \returns A negative error code on failure.
372 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200373MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000374int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 psa_algorithm_t hash_alg,
376 unsigned char const *application_secret,
377 unsigned char const *transcript, size_t transcript_len,
378 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;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000410 * see e.g. mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckere9cccb42020-08-20 13:42:46 +0100411 *
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
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200447MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000448int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 psa_algorithm_t hash_alg,
450 const unsigned char *secret_old,
451 const unsigned char *input, size_t input_len,
452 unsigned char *secret_new);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100453
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100454/**
455 * \brief Calculate a TLS 1.3 PSK binder.
456 *
457 * \param ssl The SSL context. This is used for debugging only and may
458 * be \c NULL if MBEDTLS_DEBUG_C is disabled.
Gabor Mezei07732f72022-03-26 17:04:19 +0100459 * \param hash_alg The hash algorithm associated to the PSK \p psk.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100460 * \param psk The buffer holding the PSK for which to create a binder.
461 * \param psk_len The size of \p psk in bytes.
Hanno Beckerc8d3ccd2021-05-26 04:47:29 +0100462 * \param psk_type This indicates whether the PSK \p psk is externally
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100463 * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
464 * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
465 * \param transcript The handshake transcript up to the point where the
466 * PSK binder calculation happens. This must be readable,
467 * and its size must be equal to the digest size of
Gabor Mezei07732f72022-03-26 17:04:19 +0100468 * the hash algorithm represented by \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100469 * \param result The address at which to store the PSK binder on success.
470 * This must be writable, and its size must be equal to the
471 * digest size of the hash algorithm represented by
Gabor Mezei07732f72022-03-26 17:04:19 +0100472 * \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100473 *
474 * \returns \c 0 on success.
475 * \returns A negative error code on failure.
476 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200477MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100478int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
479 const psa_algorithm_t hash_alg,
480 unsigned char const *psk, size_t psk_len,
481 int psk_type,
482 unsigned char const *transcript,
483 unsigned char *result);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100484
Hanno Beckerc94060c2021-03-22 07:50:44 +0000485/**
486 * \bref Setup an SSL transform structure representing the
487 * record protection mechanism used by TLS 1.3
488 *
489 * \param transform The SSL transform structure to be created. This must have
490 * been initialized through mbedtls_ssl_transform_init() and
491 * not used in any other way prior to calling this function.
492 * In particular, this function does not clean up the
493 * transform structure prior to installing the new keys.
494 * \param endpoint Indicates whether the transform is for the client
495 * (value #MBEDTLS_SSL_IS_CLIENT) or the server
496 * (value #MBEDTLS_SSL_IS_SERVER).
497 * \param ciphersuite The numerical identifier for the ciphersuite to use.
498 * This must be one of the identifiers listed in
499 * ssl_ciphersuites.h.
500 * \param traffic_keys The key material to use. No reference is stored in
501 * the SSL transform being generated, and the caller
502 * should destroy the key material afterwards.
503 * \param ssl (Debug-only) The SSL context to use for debug output
504 * in case of failure. This parameter is only needed if
505 * #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
506 *
507 * \return \c 0 on success. In this case, \p transform is ready to
508 * be used with mbedtls_ssl_transform_decrypt() and
509 * mbedtls_ssl_transform_encrypt().
510 * \return A negative error code on failure.
511 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200512MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100513int 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);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000518
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 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000524 * Small wrappers around mbedtls_ssl_tls13_evolve_secret().
Jerry Yu89ea3212021-09-09 14:31:24 +0800525 */
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 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200539MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100540int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl);
Jerry Yu4925ef52021-09-09 14:42:55 +0800541
Jerry Yu61e35e02021-09-16 18:59:08 +0800542/**
Jerry Yuff226982022-04-16 16:52:57 +0800543 * \brief Compute TLS 1.3 resumption master secret.
544 *
545 * \param ssl The SSL context to operate on. This must be in
546 * key schedule stage \c Application, see
547 * mbedtls_ssl_tls13_key_schedule_stage_application().
548 *
549 * \returns \c 0 on success.
550 * \returns A negative error code on failure.
551 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200552MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100553int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl);
Jerry Yuff226982022-04-16 16:52:57 +0800554
555/**
XiaokangQianc5c39d52021-11-09 11:55:10 +0000556 * \brief Calculate the verify_data value for the client or server TLS 1.3
557 * Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000558 *
559 * \param ssl The SSL context to operate on. This must be in
560 * key schedule stage \c Handshake, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000561 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianc5c39d52021-11-09 11:55:10 +0000562 * \param dst The address at which to write the verify_data value.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000563 * \param dst_len The size of \p dst in bytes.
564 * \param actual_len The address at which to store the amount of data
565 * actually written to \p dst upon success.
XiaokangQianaaa0e192021-11-10 03:07:04 +0000566 * \param which The message to calculate the `verify_data` for:
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000567 * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
568 * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
569 *
570 * \note Both client and server call this function twice, once to
571 * generate their own Finished message, and once to verify the
572 * peer's Finished message.
573
574 * \returns \c 0 on success.
575 * \returns A negative error code on failure.
576 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200577MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100578int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
579 unsigned char *dst,
580 size_t dst_len,
581 size_t *actual_len,
582 int which);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000583
Jerry Yu91b560f2022-11-04 14:10:34 +0800584#if defined(MBEDTLS_SSL_EARLY_DATA)
585/**
586 * \brief Compute TLS 1.3 early transform
587 *
Jerry Yub094e122022-11-21 13:03:47 +0800588 * \param ssl The SSL context to operate on.
Jerry Yu91b560f2022-11-04 14:10:34 +0800589 *
590 * \returns \c 0 on success.
591 * \returns A negative error code on failure.
592 *
Jerry Yu3ce61ff2022-11-21 22:45:58 +0800593 * \warning The function does not compute the early master secret. Call
594 * mbedtls_ssl_tls13_key_schedule_stage_early() before to
595 * call this function to generate the early master secret.
596 * \note For a client/server endpoint, the function computes only the
597 * encryption/decryption part of the transform as the decryption/
598 * encryption part is not defined by the specification (no early
599 * traffic from the server to the client).
Jerry Yu91b560f2022-11-04 14:10:34 +0800600 */
601MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100602int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl);
Jerry Yu91b560f2022-11-04 14:10:34 +0800603#endif /* MBEDTLS_SSL_EARLY_DATA */
604
Jerry Yue110d252022-05-05 10:19:22 +0800605/**
606 * \brief Compute TLS 1.3 handshake transform
607 *
Jerry Yuf86eb752022-05-06 11:16:55 +0800608 * \param ssl The SSL context to operate on. The early secret must have been
Jerry Yue110d252022-05-05 10:19:22 +0800609 * computed.
610 *
611 * \returns \c 0 on success.
612 * \returns A negative error code on failure.
613 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200614MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100615int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl);
Jerry Yue110d252022-05-05 10:19:22 +0800616
Jerry Yufd5ea042022-05-19 14:29:48 +0800617/**
618 * \brief Compute TLS 1.3 application transform
619 *
620 * \param ssl The SSL context to operate on. The early secret must have been
621 * computed.
622 *
623 * \returns \c 0 on success.
624 * \returns A negative error code on failure.
625 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200626MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100627int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl);
Jerry Yufd5ea042022-05-19 14:29:48 +0800628
Ronald Cron41a443a2022-10-04 16:38:25 +0200629#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu40f37712022-07-26 16:58:57 +0800630/**
Jerry Yu6cf6b472022-08-16 14:50:28 +0800631 * \brief Export TLS 1.3 PSK from handshake context
Jerry Yu40f37712022-07-26 16:58:57 +0800632 *
Jerry Yu01e42d22022-08-21 12:55:51 +0800633 * \param[in] ssl The SSL context to operate on.
634 * \param[out] psk PSK output pointer.
635 * \param[out] psk_len Length of PSK.
Jerry Yu40f37712022-07-26 16:58:57 +0800636 *
Jerry Yu29d9faa2022-08-23 17:52:45 +0800637 * \returns \c 0 if there is a configured PSK and it was exported
638 * successfully.
639 * \returns A negative error code on failure.
Jerry Yu40f37712022-07-26 16:58:57 +0800640 */
641MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100642int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
643 unsigned char **psk,
644 size_t *psk_len);
Ronald Cron41a443a2022-10-04 16:38:25 +0200645#endif
Jerry Yu40f37712022-07-26 16:58:57 +0800646
Max Fillingerbd81c9d2024-07-22 14:43:56 +0200647/**
648 * \brief Calculate TLS-Exporter function as defined in RFC 8446, Section 7.5.
649 *
650 * \param[in] hash_alg The hash algorithm.
651 * \param[in] secret The secret to use. (Should be the exporter master secret.)
652 * \param[in] secret_len Length of secret.
653 * \param[in] label The label of the exported key.
654 * \param[in] label_len The length of label.
655 * \param[out] out The output buffer for the exported key. Must have room for at least out_len bytes.
656 * \param[in] out_len Length of the key to generate.
Max Fillinger136fe9e2024-08-09 18:54:36 +0200657 */
Max Fillinger9c9989f2024-08-14 16:44:50 +0200658int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
659 const unsigned char *secret, const size_t secret_len,
660 const unsigned char *label, const size_t label_len,
661 const unsigned char *context_value, const size_t context_len,
662 uint8_t *out, const size_t out_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +0200663
Gabor Mezeie42d8bf2022-03-30 11:33:06 +0200664#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
665
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100666#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */