blob: a4b012f36e30326f9cac0028417da8b7ae3212b8 [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
Hanno Becker531fe302020-09-16 09:45:27 +010073 * by HKDF-Expand-Label.
74 *
75 * Warning: If this ever needs to be increased, the implementation
Xiaofei Bai746f9482021-11-12 08:53:56 +000076 * ssl_tls13_hkdf_encode_label() in ssl_tls13_keys.c needs to be
Hanno Becker531fe302020-09-16 09:45:27 +010077 * adjusted since it currently assumes that HKDF key expansion
78 * is never used with more than 255 Bytes of output. */
Hanno Beckerbe9d6642020-08-21 13:20:06 +010079#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
80
81/**
Xiaofei Bai89b526d2021-11-23 06:31:16 +000082 * \brief The \c HKDF-Expand-Label function from
83 * the TLS 1.3 standard RFC 8446.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010084 *
85 * <tt>
Xiaofei Bai89b526d2021-11-23 06:31:16 +000086 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010087 * HKDF-Expand( Secret, HkdfLabel, Length )
88 * </tt>
89 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +000090 * \param hash_alg The identifier for the hash algorithm to use.
91 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000092 * This must be a readable buffer of length
93 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +000094 * \param secret_len The length of \p secret in Bytes.
95 * \param label The \c Label argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000096 * This must be a readable buffer of length
97 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +000098 * \param label_len The length of \p label in Bytes.
99 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000100 * This must be a readable buffer of length \p ctx_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000101 * \param ctx_len The length of \p context in Bytes.
102 * \param buf The destination buffer to hold the expanded secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000103 * This must be a writable buffer of length \p buf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000104 * \param buf_len The desired size of the expanded secret in Bytes.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100105 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000106 * \returns \c 0 on success.
107 * \return A negative error code on failure.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100108 */
109
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200110MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000111int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 psa_algorithm_t hash_alg,
113 const unsigned char *secret, size_t secret_len,
114 const unsigned char *label, size_t label_len,
115 const unsigned char *ctx, size_t ctx_len,
116 unsigned char *buf, size_t buf_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100117
Hanno Becker3385a4d2020-08-21 13:03:34 +0100118/**
119 * \brief This function is part of the TLS 1.3 key schedule.
120 * It extracts key and IV for the actual client/server traffic
121 * from the client/server traffic secrets.
122 *
123 * From RFC 8446:
124 *
125 * <tt>
126 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
127 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
128 * </tt>
129 *
130 * \param hash_alg The identifier for the hash algorithm to be used
131 * for the HKDF-based expansion of the secret.
132 * \param client_secret The client traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000133 * This must be a readable buffer of size
134 * \p secret_len Bytes
Hanno Becker3385a4d2020-08-21 13:03:34 +0100135 * \param server_secret The server traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000136 * This must be a readable buffer of size
137 * \p secret_len Bytes
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000138 * \param secret_len Length of the secrets \p client_secret and
Hanno Becker3385a4d2020-08-21 13:03:34 +0100139 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100140 * \param key_len The desired length of the key to be extracted in Bytes.
141 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100142 * \param keys The address of the structure holding the generated
143 * keys and IVs.
144 *
145 * \returns \c 0 on success.
146 * \returns A negative error code on failure.
147 */
148
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200149MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000150int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 psa_algorithm_t hash_alg,
152 const unsigned char *client_secret,
153 const unsigned char *server_secret, size_t secret_len,
154 size_t key_len, size_t iv_len,
155 mbedtls_ssl_key_set *keys);
Hanno Becker3385a4d2020-08-21 13:03:34 +0100156
Hanno Beckerb35d5222020-08-21 13:27:44 +0100157/**
158 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
159 *
160 * <tt>
161 * Derive-Secret( Secret, Label, Messages ) =
162 * HKDF-Expand-Label( Secret, Label,
163 * Hash( Messages ),
164 * Hash.Length ) )
165 * </tt>
166 *
Hanno Becker0c42fd92020-09-09 12:58:29 +0100167 * \param hash_alg The identifier for the hash function used for the
168 * applications of HKDF.
169 * \param secret The \c Secret argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000170 * This must be a readable buffer of length
171 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000172 * \param secret_len The length of \p secret in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100173 * \param label The \c Label argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000174 * This must be a readable buffer of length
175 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000176 * \param label_len The length of \p label in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100177 * \param ctx The hash of the \c Messages argument to the
178 * \c Derive-Secret function, or the \c Messages argument
Xiaofei Baid25fab62021-12-02 06:36:27 +0000179 * itself, depending on \p ctx_hashed.
180 * \param ctx_len The length of \p ctx in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100181 * \param ctx_hashed This indicates whether the \p ctx contains the hash of
182 * the \c Messages argument in the application of the
183 * \c Derive-Secret function
184 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
185 * it is the content of \c Messages itself, in which case
186 * the function takes care of the hashing
187 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
188 * \param dstbuf The target buffer to write the output of
189 * \c Derive-Secret to. This must be a writable buffer of
Xiaofei Baid25fab62021-12-02 06:36:27 +0000190 * size \p dtsbuf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000191 * \param dstbuf_len The length of \p dstbuf in Bytes.
Hanno Beckerb35d5222020-08-21 13:27:44 +0100192 *
193 * \returns \c 0 on success.
194 * \returns A negative error code on failure.
195 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200196MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000197int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 psa_algorithm_t hash_alg,
199 const unsigned char *secret, size_t secret_len,
200 const unsigned char *label, size_t label_len,
201 const unsigned char *ctx, size_t ctx_len,
202 int ctx_hashed,
203 unsigned char *dstbuf, size_t dstbuf_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100204
Hanno Beckere9cccb42020-08-20 13:42:46 +0100205/**
Hanno Beckeref5235b2021-05-24 06:39:41 +0100206 * \brief Derive TLS 1.3 early data key material from early secret.
207 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000208 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100209 * with the appropriate labels.
210 *
211 * <tt>
212 * Early Secret
213 * |
214 * +-----> Derive-Secret(., "c e traffic", ClientHello)
215 * | = client_early_traffic_secret
216 * |
217 * +-----> Derive-Secret(., "e exp master", ClientHello)
218 * . = early_exporter_master_secret
219 * .
220 * .
221 * </tt>
222 *
223 * \note To obtain the actual key and IV for the early data traffic,
224 * the client secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000225 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100226 *
227 * \note The binder key, which is also generated from the early secret,
228 * is omitted here. Its calculation is part of the separate routine
Xiaofei Bai746f9482021-11-12 08:53:56 +0000229 * mbedtls_ssl_tls13_create_psk_binder().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100230 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100231 * \param hash_alg The hash algorithm associated with the PSK for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100232 * early data key material is being derived.
233 * \param early_secret The early secret from which the early data key material
234 * should be derived. This must be a readable buffer whose
235 * length is the digest size of the hash algorithm
236 * represented by \p md_size.
237 * \param transcript The transcript of the handshake so far, calculated with
Gabor Mezei07732f72022-03-26 17:04:19 +0100238 * respect to \p hash_alg. This must be a readable buffer
Hanno Beckeref5235b2021-05-24 06:39:41 +0100239 * whose length is the digest size of the hash algorithm
240 * represented by \p md_size.
241 * \param derived The address of the structure in which to store
242 * the early data key material.
243 *
244 * \returns \c 0 on success.
245 * \returns A negative error code on failure.
246 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200247MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000248int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 psa_algorithm_t hash_alg,
250 unsigned char const *early_secret,
251 unsigned char const *transcript, size_t transcript_len,
252 mbedtls_ssl_tls13_early_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100253
254/**
255 * \brief Derive TLS 1.3 handshake key material from the handshake secret.
256 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000257 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100258 * with the appropriate labels from the standard.
259 *
260 * <tt>
261 * Handshake Secret
262 * |
263 * +-----> Derive-Secret( ., "c hs traffic",
264 * | ClientHello...ServerHello )
265 * | = client_handshake_traffic_secret
266 * |
267 * +-----> Derive-Secret( ., "s hs traffic",
268 * . ClientHello...ServerHello )
269 * . = server_handshake_traffic_secret
270 * .
271 * </tt>
272 *
273 * \note To obtain the actual key and IV for the encrypted handshake traffic,
274 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000275 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100276 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100277 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100278 * that's being used for the connection.
279 * \param handshake_secret The handshake secret from which the handshake key
280 * material should be derived. This must be a readable
281 * buffer whose length is the digest size of the hash
282 * algorithm represented by \p md_size.
283 * \param transcript The transcript of the handshake so far, calculated
Gabor Mezei07732f72022-03-26 17:04:19 +0100284 * with respect to \p hash_alg. This must be a readable
Hanno Beckeref5235b2021-05-24 06:39:41 +0100285 * buffer whose length is the digest size of the hash
286 * algorithm represented by \p md_size.
287 * \param derived The address of the structure in which to
288 * store the handshake key material.
289 *
290 * \returns \c 0 on success.
291 * \returns A negative error code on failure.
292 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200293MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000294int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 psa_algorithm_t hash_alg,
296 unsigned char const *handshake_secret,
297 unsigned char const *transcript, size_t transcript_len,
298 mbedtls_ssl_tls13_handshake_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100299
300/**
301 * \brief Derive TLS 1.3 application key material from the master secret.
302 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000303 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100304 * 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
Xiaofei Bai746f9482021-11-12 08:53:56 +0000325 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100326 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100327 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100328 * 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
Gabor Mezei07732f72022-03-26 17:04:19 +0100335 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100336 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100337 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100338 * \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 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200344MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000345int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 psa_algorithm_t hash_alg,
347 unsigned char const *master_secret,
348 unsigned char const *transcript, size_t transcript_len,
349 mbedtls_ssl_tls13_application_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100350
351/**
352 * \brief Derive TLS 1.3 resumption master secret from the master secret.
353 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000354 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100355 * with the appropriate labels from the standard.
356 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100357 * \param hash_alg The hash algorithm used in the application for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100358 * key material is being derived.
359 * \param application_secret The application secret from which the resumption master
360 * secret should be derived. This must be a readable
361 * buffer whose length is the digest size of the hash
362 * algorithm represented by \p md_size.
363 * \param transcript The transcript of the handshake up to and including
364 * the ClientFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100365 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100366 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100367 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100368 * \param transcript_len The length of \p transcript in Bytes.
369 * \param derived The address of the structure in which to
370 * store the resumption master secret.
371 *
372 * \returns \c 0 on success.
373 * \returns A negative error code on failure.
374 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200375MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000376int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 psa_algorithm_t hash_alg,
378 unsigned char const *application_secret,
379 unsigned char const *transcript, size_t transcript_len,
380 mbedtls_ssl_tls13_application_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100381
382/**
Hanno Beckere9cccb42020-08-20 13:42:46 +0100383 * \brief Compute the next secret in the TLS 1.3 key schedule
384 *
385 * The TLS 1.3 key schedule proceeds as follows to compute
386 * the three main secrets during the handshake: The early
387 * secret for early data, the handshake secret for all
388 * other encrypted handshake messages, and the master
389 * secret for all application traffic.
390 *
391 * <tt>
392 * 0
393 * |
394 * v
395 * PSK -> HKDF-Extract = Early Secret
396 * |
397 * v
398 * Derive-Secret( ., "derived", "" )
399 * |
400 * v
401 * (EC)DHE -> HKDF-Extract = Handshake Secret
402 * |
403 * v
404 * Derive-Secret( ., "derived", "" )
405 * |
406 * v
407 * 0 -> HKDF-Extract = Master Secret
408 * </tt>
409 *
410 * Each of the three secrets in turn is the basis for further
411 * key derivations, such as the derivation of traffic keys and IVs;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000412 * see e.g. mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckere9cccb42020-08-20 13:42:46 +0100413 *
414 * This function implements one step in this evolution of secrets:
415 *
416 * <tt>
417 * old_secret
418 * |
419 * v
420 * Derive-Secret( ., "derived", "" )
421 * |
422 * v
423 * input -> HKDF-Extract = new_secret
424 * </tt>
425 *
426 * \param hash_alg The identifier for the hash function used for the
427 * applications of HKDF.
428 * \param secret_old The address of the buffer holding the old secret
429 * on function entry. If not \c NULL, this must be a
430 * readable buffer whose size matches the output size
431 * of the hash function represented by \p hash_alg.
432 * If \c NULL, an all \c 0 array will be used instead.
433 * \param input The address of the buffer holding the additional
434 * input for the key derivation (e.g., the PSK or the
435 * ephemeral (EC)DH secret). If not \c NULL, this must be
436 * a readable buffer whose size \p input_len Bytes.
437 * If \c NULL, an all \c 0 array will be used instead.
438 * \param input_len The length of \p input in Bytes.
439 * \param secret_new The address of the buffer holding the new secret
440 * on function exit. This must be a writable buffer
441 * whose size matches the output size of the hash
442 * function represented by \p hash_alg.
443 * This may be the same as \p secret_old.
444 *
445 * \returns \c 0 on success.
446 * \returns A negative error code on failure.
447 */
448
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200449MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000450int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 psa_algorithm_t hash_alg,
452 const unsigned char *secret_old,
453 const unsigned char *input, size_t input_len,
454 unsigned char *secret_new);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100455
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100456/**
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.
Gabor Mezei07732f72022-03-26 17:04:19 +0100461 * \param hash_alg The hash algorithm associated to the PSK \p psk.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100462 * \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
Gabor Mezei07732f72022-03-26 17:04:19 +0100470 * the hash algorithm represented by \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100471 * \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
Gabor Mezei07732f72022-03-26 17:04:19 +0100474 * \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100475 *
476 * \returns \c 0 on success.
477 * \returns A negative error code on failure.
478 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200479MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100480int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
481 const psa_algorithm_t hash_alg,
482 unsigned char const *psk, size_t psk_len,
483 int psk_type,
484 unsigned char const *transcript,
485 unsigned char *result);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100486
Hanno Beckerc94060c2021-03-22 07:50:44 +0000487/**
488 * \bref Setup an SSL transform structure representing the
489 * record protection mechanism used by TLS 1.3
490 *
491 * \param transform The SSL transform structure to be created. This must have
492 * been initialized through mbedtls_ssl_transform_init() and
493 * not used in any other way prior to calling this function.
494 * In particular, this function does not clean up the
495 * transform structure prior to installing the new keys.
496 * \param endpoint Indicates whether the transform is for the client
497 * (value #MBEDTLS_SSL_IS_CLIENT) or the server
498 * (value #MBEDTLS_SSL_IS_SERVER).
499 * \param ciphersuite The numerical identifier for the ciphersuite to use.
500 * This must be one of the identifiers listed in
501 * ssl_ciphersuites.h.
502 * \param traffic_keys The key material to use. No reference is stored in
503 * the SSL transform being generated, and the caller
504 * should destroy the key material afterwards.
505 * \param ssl (Debug-only) The SSL context to use for debug output
506 * in case of failure. This parameter is only needed if
507 * #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
508 *
509 * \return \c 0 on success. In this case, \p transform is ready to
510 * be used with mbedtls_ssl_transform_decrypt() and
511 * mbedtls_ssl_transform_encrypt().
512 * \return A negative error code on failure.
513 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200514MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100515int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
516 int endpoint,
517 int ciphersuite,
518 mbedtls_ssl_key_set const *traffic_keys,
519 mbedtls_ssl_context *ssl);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000520
Jerry Yu89ea3212021-09-09 14:31:24 +0800521/*
522 * TLS 1.3 key schedule evolutions
523 *
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800524 * Early -> Handshake -> Application
Jerry Yu89ea3212021-09-09 14:31:24 +0800525 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000526 * Small wrappers around mbedtls_ssl_tls13_evolve_secret().
Jerry Yu89ea3212021-09-09 14:31:24 +0800527 */
528
529/**
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800530 * \brief Begin TLS 1.3 key schedule by calculating early secret.
Jerry Yu89ea3212021-09-09 14:31:24 +0800531 *
532 * The TLS 1.3 key schedule can be viewed as a simple state machine
533 * with states Initial -> Early -> Handshake -> Application, and
534 * this function represents the Initial -> Early transition.
535 *
Jerry Yu89ea3212021-09-09 14:31:24 +0800536 * \param ssl The SSL context to operate on.
537 *
538 * \returns \c 0 on success.
539 * \returns A negative error code on failure.
540 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200541MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100542int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl);
Jerry Yu4925ef52021-09-09 14:42:55 +0800543
Jerry Yu61e35e02021-09-16 18:59:08 +0800544/**
Jerry Yuff226982022-04-16 16:52:57 +0800545 * \brief Compute TLS 1.3 resumption master secret.
546 *
547 * \param ssl The SSL context to operate on. This must be in
548 * key schedule stage \c Application, see
549 * mbedtls_ssl_tls13_key_schedule_stage_application().
550 *
551 * \returns \c 0 on success.
552 * \returns A negative error code on failure.
553 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200554MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100555int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl);
Jerry Yuff226982022-04-16 16:52:57 +0800556
557/**
XiaokangQianc5c39d52021-11-09 11:55:10 +0000558 * \brief Calculate the verify_data value for the client or server TLS 1.3
559 * Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000560 *
561 * \param ssl The SSL context to operate on. This must be in
562 * key schedule stage \c Handshake, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000563 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianc5c39d52021-11-09 11:55:10 +0000564 * \param dst The address at which to write the verify_data value.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000565 * \param dst_len The size of \p dst in bytes.
566 * \param actual_len The address at which to store the amount of data
567 * actually written to \p dst upon success.
XiaokangQianaaa0e192021-11-10 03:07:04 +0000568 * \param which The message to calculate the `verify_data` for:
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000569 * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
570 * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
571 *
572 * \note Both client and server call this function twice, once to
573 * generate their own Finished message, and once to verify the
574 * peer's Finished message.
575
576 * \returns \c 0 on success.
577 * \returns A negative error code on failure.
578 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200579MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100580int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
581 unsigned char *dst,
582 size_t dst_len,
583 size_t *actual_len,
584 int which);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000585
Jerry Yu91b560f2022-11-04 14:10:34 +0800586#if defined(MBEDTLS_SSL_EARLY_DATA)
587/**
588 * \brief Compute TLS 1.3 early transform
589 *
Jerry Yub094e122022-11-21 13:03:47 +0800590 * \param ssl The SSL context to operate on.
Jerry Yu91b560f2022-11-04 14:10:34 +0800591 *
592 * \returns \c 0 on success.
593 * \returns A negative error code on failure.
594 *
Jerry Yu3ce61ff2022-11-21 22:45:58 +0800595 * \warning The function does not compute the early master secret. Call
596 * mbedtls_ssl_tls13_key_schedule_stage_early() before to
597 * call this function to generate the early master secret.
598 * \note For a client/server endpoint, the function computes only the
599 * encryption/decryption part of the transform as the decryption/
600 * encryption part is not defined by the specification (no early
601 * traffic from the server to the client).
Jerry Yu91b560f2022-11-04 14:10:34 +0800602 */
603MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100604int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl);
Jerry Yu91b560f2022-11-04 14:10:34 +0800605#endif /* MBEDTLS_SSL_EARLY_DATA */
606
Jerry Yue110d252022-05-05 10:19:22 +0800607/**
608 * \brief Compute TLS 1.3 handshake transform
609 *
Jerry Yuf86eb752022-05-06 11:16:55 +0800610 * \param ssl The SSL context to operate on. The early secret must have been
Jerry Yue110d252022-05-05 10:19:22 +0800611 * computed.
612 *
613 * \returns \c 0 on success.
614 * \returns A negative error code on failure.
615 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200616MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100617int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl);
Jerry Yue110d252022-05-05 10:19:22 +0800618
Jerry Yufd5ea042022-05-19 14:29:48 +0800619/**
620 * \brief Compute TLS 1.3 application transform
621 *
622 * \param ssl The SSL context to operate on. The early secret must have been
623 * computed.
624 *
625 * \returns \c 0 on success.
626 * \returns A negative error code on failure.
627 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200628MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100629int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl);
Jerry Yufd5ea042022-05-19 14:29:48 +0800630
Ronald Cron41a443a2022-10-04 16:38:25 +0200631#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu40f37712022-07-26 16:58:57 +0800632/**
Jerry Yu6cf6b472022-08-16 14:50:28 +0800633 * \brief Export TLS 1.3 PSK from handshake context
Jerry Yu40f37712022-07-26 16:58:57 +0800634 *
Jerry Yu01e42d22022-08-21 12:55:51 +0800635 * \param[in] ssl The SSL context to operate on.
636 * \param[out] psk PSK output pointer.
637 * \param[out] psk_len Length of PSK.
Jerry Yu40f37712022-07-26 16:58:57 +0800638 *
Jerry Yu29d9faa2022-08-23 17:52:45 +0800639 * \returns \c 0 if there is a configured PSK and it was exported
640 * successfully.
641 * \returns A negative error code on failure.
Jerry Yu40f37712022-07-26 16:58:57 +0800642 */
643MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100644int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
645 unsigned char **psk,
646 size_t *psk_len);
Ronald Cron41a443a2022-10-04 16:38:25 +0200647#endif
Jerry Yu40f37712022-07-26 16:58:57 +0800648
Max Fillingerbd81c9d2024-07-22 14:43:56 +0200649/**
650 * \brief Calculate TLS-Exporter function as defined in RFC 8446, Section 7.5.
651 *
652 * \param[in] hash_alg The hash algorithm.
653 * \param[in] secret The secret to use. (Should be the exporter master secret.)
654 * \param[in] secret_len Length of secret.
655 * \param[in] label The label of the exported key.
656 * \param[in] label_len The length of label.
657 * \param[out] out The output buffer for the exported key. Must have room for at least out_len bytes.
658 * \param[in] out_len Length of the key to generate.
Max Fillinger136fe9e2024-08-09 18:54:36 +0200659 */
Max Fillinger9c9989f2024-08-14 16:44:50 +0200660int mbedtls_ssl_tls13_exporter(const psa_algorithm_t hash_alg,
661 const unsigned char *secret, const size_t secret_len,
662 const unsigned char *label, const size_t label_len,
663 const unsigned char *context_value, const size_t context_len,
664 uint8_t *out, const size_t out_len);
Max Fillingerbd81c9d2024-07-22 14:43:56 +0200665
Gabor Mezeie42d8bf2022-03-30 11:33:06 +0200666#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
667
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100668#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */