blob: 21e9b4d734e28f49a35b6c48a0d3ee8fcf361051 [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 ( the "License" ); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19#if !defined(MBEDTLS_SSL_TLS1_3_KEYS_H)
20#define MBEDTLS_SSL_TLS1_3_KEYS_H
21
Hanno Becker70d7fb02020-09-09 10:11:21 +010022/* This requires MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) to be defined at
Xiaofei Bai746f9482021-11-12 08:53:56 +000023 * the point of use. See e.g. the definition of mbedtls_ssl_tls13_labels_union
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010024 * below. */
Jerry Yu0bbb3972021-09-19 20:27:17 +080025#define MBEDTLS_SSL_TLS1_3_LABEL_LIST \
Gilles Peskine449bd832023-01-11 14:50:10 +010026 MBEDTLS_SSL_TLS1_3_LABEL(finished, "finished") \
27 MBEDTLS_SSL_TLS1_3_LABEL(resumption, "resumption") \
28 MBEDTLS_SSL_TLS1_3_LABEL(traffic_upd, "traffic upd") \
29 MBEDTLS_SSL_TLS1_3_LABEL(exporter, "exporter") \
30 MBEDTLS_SSL_TLS1_3_LABEL(key, "key") \
31 MBEDTLS_SSL_TLS1_3_LABEL(iv, "iv") \
32 MBEDTLS_SSL_TLS1_3_LABEL(c_hs_traffic, "c hs traffic") \
33 MBEDTLS_SSL_TLS1_3_LABEL(c_ap_traffic, "c ap traffic") \
34 MBEDTLS_SSL_TLS1_3_LABEL(c_e_traffic, "c e traffic") \
35 MBEDTLS_SSL_TLS1_3_LABEL(s_hs_traffic, "s hs traffic") \
36 MBEDTLS_SSL_TLS1_3_LABEL(s_ap_traffic, "s ap traffic") \
37 MBEDTLS_SSL_TLS1_3_LABEL(s_e_traffic, "s e traffic") \
38 MBEDTLS_SSL_TLS1_3_LABEL(e_exp_master, "e exp master") \
39 MBEDTLS_SSL_TLS1_3_LABEL(res_master, "res master") \
40 MBEDTLS_SSL_TLS1_3_LABEL(exp_master, "exp master") \
41 MBEDTLS_SSL_TLS1_3_LABEL(ext_binder, "ext binder") \
42 MBEDTLS_SSL_TLS1_3_LABEL(res_binder, "res binder") \
43 MBEDTLS_SSL_TLS1_3_LABEL(derived, "derived") \
44 MBEDTLS_SSL_TLS1_3_LABEL(client_cv, "TLS 1.3, client CertificateVerify") \
45 MBEDTLS_SSL_TLS1_3_LABEL(server_cv, "TLS 1.3, server CertificateVerify")
Hanno Beckere4435ea2020-09-08 10:43:52 +010046
Gabor Mezeie42d8bf2022-03-30 11:33:06 +020047#define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
48#define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
49
50#define MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL 0
51#define MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION 1
52
53#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
56 const unsigned char name [sizeof(string) - 1];
Hanno Beckerbe9d6642020-08-21 13:20:06 +010057
Gilles Peskine449bd832023-01-11 14:50:10 +010058union mbedtls_ssl_tls13_labels_union {
Hanno Beckerbe9d6642020-08-21 13:20:06 +010059 MBEDTLS_SSL_TLS1_3_LABEL_LIST
60};
Gilles Peskine449bd832023-01-11 14:50:10 +010061struct mbedtls_ssl_tls13_labels_struct {
Hanno Beckerbe9d6642020-08-21 13:20:06 +010062 MBEDTLS_SSL_TLS1_3_LABEL_LIST
63};
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010064#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010065
Xiaofei Bai746f9482021-11-12 08:53:56 +000066extern const struct mbedtls_ssl_tls13_labels_struct mbedtls_ssl_tls13_labels;
Hanno Beckerbe9d6642020-08-21 13:20:06 +010067
Gilles Peskine449bd832023-01-11 14:50:10 +010068#define MBEDTLS_SSL_TLS1_3_LBL_LEN(LABEL) \
Xiaofei Bai746f9482021-11-12 08:53:56 +000069 sizeof(mbedtls_ssl_tls13_labels.LABEL)
Jerry Yu0bbb3972021-09-19 20:27:17 +080070
Gilles Peskine449bd832023-01-11 14:50:10 +010071#define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(LABEL) \
Xiaofei Bai746f9482021-11-12 08:53:56 +000072 mbedtls_ssl_tls13_labels.LABEL, \
Gilles Peskine449bd832023-01-11 14:50:10 +010073 MBEDTLS_SSL_TLS1_3_LBL_LEN(LABEL)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010074
75#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
Gilles Peskine449bd832023-01-11 14:50:10 +010076 sizeof(union mbedtls_ssl_tls13_labels_union)
Hanno Beckerbe9d6642020-08-21 13:20:06 +010077
Hanno Becker61baae72020-09-16 09:24:14 +010078/* The maximum length of HKDF contexts used in the TLS 1.3 standard.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010079 * Since contexts are always hashes of message transcripts, this can
80 * be approximated from above by the maximum hash size. */
81#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
Przemyslaw Stekielab9b9d42022-09-14 13:51:07 +020082 PSA_HASH_MAX_SIZE
Hanno Beckerbe9d6642020-08-21 13:20:06 +010083
84/* Maximum desired length for expanded key material generated
Hanno Becker531fe302020-09-16 09:45:27 +010085 * by HKDF-Expand-Label.
86 *
87 * Warning: If this ever needs to be increased, the implementation
Xiaofei Bai746f9482021-11-12 08:53:56 +000088 * ssl_tls13_hkdf_encode_label() in ssl_tls13_keys.c needs to be
Hanno Becker531fe302020-09-16 09:45:27 +010089 * adjusted since it currently assumes that HKDF key expansion
90 * is never used with more than 255 Bytes of output. */
Hanno Beckerbe9d6642020-08-21 13:20:06 +010091#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
92
93/**
Xiaofei Bai89b526d2021-11-23 06:31:16 +000094 * \brief The \c HKDF-Expand-Label function from
95 * the TLS 1.3 standard RFC 8446.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010096 *
97 * <tt>
Xiaofei Bai89b526d2021-11-23 06:31:16 +000098 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010099 * HKDF-Expand( Secret, HkdfLabel, Length )
100 * </tt>
101 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000102 * \param hash_alg The identifier for the hash algorithm to use.
103 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000104 * This must be a readable buffer of length
105 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000106 * \param secret_len The length of \p secret in Bytes.
107 * \param label The \c Label argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000108 * This must be a readable buffer of length
109 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000110 * \param label_len The length of \p label in Bytes.
111 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000112 * This must be a readable buffer of length \p ctx_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000113 * \param ctx_len The length of \p context in Bytes.
114 * \param buf The destination buffer to hold the expanded secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000115 * This must be a writable buffer of length \p buf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000116 * \param buf_len The desired size of the expanded secret in Bytes.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100117 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000118 * \returns \c 0 on success.
119 * \return A negative error code on failure.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100120 */
121
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200122MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000123int mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 psa_algorithm_t hash_alg,
125 const unsigned char *secret, size_t secret_len,
126 const unsigned char *label, size_t label_len,
127 const unsigned char *ctx, size_t ctx_len,
128 unsigned char *buf, size_t buf_len);
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100129
Hanno Becker3385a4d2020-08-21 13:03:34 +0100130/**
131 * \brief This function is part of the TLS 1.3 key schedule.
132 * It extracts key and IV for the actual client/server traffic
133 * from the client/server traffic secrets.
134 *
135 * From RFC 8446:
136 *
137 * <tt>
138 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
139 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
140 * </tt>
141 *
142 * \param hash_alg The identifier for the hash algorithm to be used
143 * for the HKDF-based expansion of the secret.
144 * \param client_secret The client traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000145 * This must be a readable buffer of size
146 * \p secret_len Bytes
Hanno Becker3385a4d2020-08-21 13:03:34 +0100147 * \param server_secret The server traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000148 * This must be a readable buffer of size
149 * \p secret_len Bytes
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000150 * \param secret_len Length of the secrets \p client_secret and
Hanno Becker3385a4d2020-08-21 13:03:34 +0100151 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100152 * \param key_len The desired length of the key to be extracted in Bytes.
153 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100154 * \param keys The address of the structure holding the generated
155 * keys and IVs.
156 *
157 * \returns \c 0 on success.
158 * \returns A negative error code on failure.
159 */
160
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200161MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000162int mbedtls_ssl_tls13_make_traffic_keys(
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 psa_algorithm_t hash_alg,
164 const unsigned char *client_secret,
165 const unsigned char *server_secret, size_t secret_len,
166 size_t key_len, size_t iv_len,
167 mbedtls_ssl_key_set *keys);
Hanno Becker3385a4d2020-08-21 13:03:34 +0100168
Hanno Beckerb35d5222020-08-21 13:27:44 +0100169/**
170 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
171 *
172 * <tt>
173 * Derive-Secret( Secret, Label, Messages ) =
174 * HKDF-Expand-Label( Secret, Label,
175 * Hash( Messages ),
176 * Hash.Length ) )
177 * </tt>
178 *
Hanno Becker0c42fd92020-09-09 12:58:29 +0100179 * \param hash_alg The identifier for the hash function used for the
180 * applications of HKDF.
181 * \param secret The \c Secret argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000182 * This must be a readable buffer of length
183 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000184 * \param secret_len The length of \p secret in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100185 * \param label The \c Label argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000186 * This must be a readable buffer of length
187 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000188 * \param label_len The length of \p label in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100189 * \param ctx The hash of the \c Messages argument to the
190 * \c Derive-Secret function, or the \c Messages argument
Xiaofei Baid25fab62021-12-02 06:36:27 +0000191 * itself, depending on \p ctx_hashed.
192 * \param ctx_len The length of \p ctx in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100193 * \param ctx_hashed This indicates whether the \p ctx contains the hash of
194 * the \c Messages argument in the application of the
195 * \c Derive-Secret function
196 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
197 * it is the content of \c Messages itself, in which case
198 * the function takes care of the hashing
199 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
200 * \param dstbuf The target buffer to write the output of
201 * \c Derive-Secret to. This must be a writable buffer of
Xiaofei Baid25fab62021-12-02 06:36:27 +0000202 * size \p dtsbuf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000203 * \param dstbuf_len The length of \p dstbuf in Bytes.
Hanno Beckerb35d5222020-08-21 13:27:44 +0100204 *
205 * \returns \c 0 on success.
206 * \returns A negative error code on failure.
207 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200208MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000209int mbedtls_ssl_tls13_derive_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 psa_algorithm_t hash_alg,
211 const unsigned char *secret, size_t secret_len,
212 const unsigned char *label, size_t label_len,
213 const unsigned char *ctx, size_t ctx_len,
214 int ctx_hashed,
215 unsigned char *dstbuf, size_t dstbuf_len);
Hanno Beckerb35d5222020-08-21 13:27:44 +0100216
Hanno Beckere9cccb42020-08-20 13:42:46 +0100217/**
Hanno Beckeref5235b2021-05-24 06:39:41 +0100218 * \brief Derive TLS 1.3 early data key material from early secret.
219 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000220 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100221 * with the appropriate labels.
222 *
223 * <tt>
224 * Early Secret
225 * |
226 * +-----> Derive-Secret(., "c e traffic", ClientHello)
227 * | = client_early_traffic_secret
228 * |
229 * +-----> Derive-Secret(., "e exp master", ClientHello)
230 * . = early_exporter_master_secret
231 * .
232 * .
233 * </tt>
234 *
235 * \note To obtain the actual key and IV for the early data traffic,
236 * the client secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000237 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100238 *
239 * \note The binder key, which is also generated from the early secret,
240 * is omitted here. Its calculation is part of the separate routine
Xiaofei Bai746f9482021-11-12 08:53:56 +0000241 * mbedtls_ssl_tls13_create_psk_binder().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100242 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100243 * \param hash_alg The hash algorithm associated with the PSK for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100244 * early data key material is being derived.
245 * \param early_secret The early secret from which the early data key material
246 * should be derived. This must be a readable buffer whose
247 * length is the digest size of the hash algorithm
248 * represented by \p md_size.
249 * \param transcript The transcript of the handshake so far, calculated with
Gabor Mezei07732f72022-03-26 17:04:19 +0100250 * respect to \p hash_alg. This must be a readable buffer
Hanno Beckeref5235b2021-05-24 06:39:41 +0100251 * whose length is the digest size of the hash algorithm
252 * represented by \p md_size.
253 * \param derived The address of the structure in which to store
254 * the early data key material.
255 *
256 * \returns \c 0 on success.
257 * \returns A negative error code on failure.
258 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200259MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000260int mbedtls_ssl_tls13_derive_early_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 psa_algorithm_t hash_alg,
262 unsigned char const *early_secret,
263 unsigned char const *transcript, size_t transcript_len,
264 mbedtls_ssl_tls13_early_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100265
266/**
267 * \brief Derive TLS 1.3 handshake key material from the handshake secret.
268 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000269 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100270 * with the appropriate labels from the standard.
271 *
272 * <tt>
273 * Handshake Secret
274 * |
275 * +-----> Derive-Secret( ., "c hs traffic",
276 * | ClientHello...ServerHello )
277 * | = client_handshake_traffic_secret
278 * |
279 * +-----> Derive-Secret( ., "s hs traffic",
280 * . ClientHello...ServerHello )
281 * . = server_handshake_traffic_secret
282 * .
283 * </tt>
284 *
285 * \note To obtain the actual key and IV for the encrypted handshake traffic,
286 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000287 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100288 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100289 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100290 * that's being used for the connection.
291 * \param handshake_secret The handshake secret from which the handshake key
292 * material should be derived. This must be a readable
293 * buffer whose length is the digest size of the hash
294 * algorithm represented by \p md_size.
295 * \param transcript The transcript of the handshake so far, calculated
Gabor Mezei07732f72022-03-26 17:04:19 +0100296 * with respect to \p hash_alg. This must be a readable
Hanno Beckeref5235b2021-05-24 06:39:41 +0100297 * buffer whose length is the digest size of the hash
298 * algorithm represented by \p md_size.
299 * \param derived The address of the structure in which to
300 * store the handshake key material.
301 *
302 * \returns \c 0 on success.
303 * \returns A negative error code on failure.
304 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200305MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000306int mbedtls_ssl_tls13_derive_handshake_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 psa_algorithm_t hash_alg,
308 unsigned char const *handshake_secret,
309 unsigned char const *transcript, size_t transcript_len,
310 mbedtls_ssl_tls13_handshake_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100311
312/**
313 * \brief Derive TLS 1.3 application key material from the master secret.
314 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000315 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100316 * with the appropriate labels from the standard.
317 *
318 * <tt>
319 * Master Secret
320 * |
321 * +-----> Derive-Secret( ., "c ap traffic",
322 * | ClientHello...server Finished )
323 * | = client_application_traffic_secret_0
324 * |
325 * +-----> Derive-Secret( ., "s ap traffic",
326 * | ClientHello...Server Finished )
327 * | = server_application_traffic_secret_0
328 * |
329 * +-----> Derive-Secret( ., "exp master",
330 * . ClientHello...server Finished)
331 * . = exporter_master_secret
332 * .
333 * </tt>
334 *
335 * \note To obtain the actual key and IV for the (0-th) application traffic,
336 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000337 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100338 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100339 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100340 * that's being used for the connection.
341 * \param master_secret The master secret from which the application key
342 * material should be derived. This must be a readable
343 * buffer whose length is the digest size of the hash
344 * algorithm represented by \p md_size.
345 * \param transcript The transcript of the handshake up to and including
346 * the ServerFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100347 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100348 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100349 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100350 * \param derived The address of the structure in which to
351 * store the application key material.
352 *
353 * \returns \c 0 on success.
354 * \returns A negative error code on failure.
355 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200356MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000357int mbedtls_ssl_tls13_derive_application_secrets(
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 psa_algorithm_t hash_alg,
359 unsigned char const *master_secret,
360 unsigned char const *transcript, size_t transcript_len,
361 mbedtls_ssl_tls13_application_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100362
363/**
364 * \brief Derive TLS 1.3 resumption master secret from the master secret.
365 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000366 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100367 * with the appropriate labels from the standard.
368 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100369 * \param hash_alg The hash algorithm used in the application for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100370 * key material is being derived.
371 * \param application_secret The application secret from which the resumption master
372 * secret should be derived. This must be a readable
373 * buffer whose length is the digest size of the hash
374 * algorithm represented by \p md_size.
375 * \param transcript The transcript of the handshake up to and including
376 * the ClientFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100377 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100378 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100379 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100380 * \param transcript_len The length of \p transcript in Bytes.
381 * \param derived The address of the structure in which to
382 * store the resumption master secret.
383 *
384 * \returns \c 0 on success.
385 * \returns A negative error code on failure.
386 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200387MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000388int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 psa_algorithm_t hash_alg,
390 unsigned char const *application_secret,
391 unsigned char const *transcript, size_t transcript_len,
392 mbedtls_ssl_tls13_application_secrets *derived);
Hanno Beckeref5235b2021-05-24 06:39:41 +0100393
394/**
Hanno Beckere9cccb42020-08-20 13:42:46 +0100395 * \brief Compute the next secret in the TLS 1.3 key schedule
396 *
397 * The TLS 1.3 key schedule proceeds as follows to compute
398 * the three main secrets during the handshake: The early
399 * secret for early data, the handshake secret for all
400 * other encrypted handshake messages, and the master
401 * secret for all application traffic.
402 *
403 * <tt>
404 * 0
405 * |
406 * v
407 * PSK -> HKDF-Extract = Early Secret
408 * |
409 * v
410 * Derive-Secret( ., "derived", "" )
411 * |
412 * v
413 * (EC)DHE -> HKDF-Extract = Handshake Secret
414 * |
415 * v
416 * Derive-Secret( ., "derived", "" )
417 * |
418 * v
419 * 0 -> HKDF-Extract = Master Secret
420 * </tt>
421 *
422 * Each of the three secrets in turn is the basis for further
423 * key derivations, such as the derivation of traffic keys and IVs;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000424 * see e.g. mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckere9cccb42020-08-20 13:42:46 +0100425 *
426 * This function implements one step in this evolution of secrets:
427 *
428 * <tt>
429 * old_secret
430 * |
431 * v
432 * Derive-Secret( ., "derived", "" )
433 * |
434 * v
435 * input -> HKDF-Extract = new_secret
436 * </tt>
437 *
438 * \param hash_alg The identifier for the hash function used for the
439 * applications of HKDF.
440 * \param secret_old The address of the buffer holding the old secret
441 * on function entry. If not \c NULL, this must be a
442 * readable buffer whose size matches the output size
443 * of the hash function represented by \p hash_alg.
444 * If \c NULL, an all \c 0 array will be used instead.
445 * \param input The address of the buffer holding the additional
446 * input for the key derivation (e.g., the PSK or the
447 * ephemeral (EC)DH secret). If not \c NULL, this must be
448 * a readable buffer whose size \p input_len Bytes.
449 * If \c NULL, an all \c 0 array will be used instead.
450 * \param input_len The length of \p input in Bytes.
451 * \param secret_new The address of the buffer holding the new secret
452 * on function exit. This must be a writable buffer
453 * whose size matches the output size of the hash
454 * function represented by \p hash_alg.
455 * This may be the same as \p secret_old.
456 *
457 * \returns \c 0 on success.
458 * \returns A negative error code on failure.
459 */
460
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200461MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000462int mbedtls_ssl_tls13_evolve_secret(
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 psa_algorithm_t hash_alg,
464 const unsigned char *secret_old,
465 const unsigned char *input, size_t input_len,
466 unsigned char *secret_new);
Hanno Beckere9cccb42020-08-20 13:42:46 +0100467
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100468/**
469 * \brief Calculate a TLS 1.3 PSK binder.
470 *
471 * \param ssl The SSL context. This is used for debugging only and may
472 * be \c NULL if MBEDTLS_DEBUG_C is disabled.
Gabor Mezei07732f72022-03-26 17:04:19 +0100473 * \param hash_alg The hash algorithm associated to the PSK \p psk.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100474 * \param psk The buffer holding the PSK for which to create a binder.
475 * \param psk_len The size of \p psk in bytes.
Hanno Beckerc8d3ccd2021-05-26 04:47:29 +0100476 * \param psk_type This indicates whether the PSK \p psk is externally
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100477 * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
478 * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
479 * \param transcript The handshake transcript up to the point where the
480 * PSK binder calculation happens. This must be readable,
481 * and its size must be equal to the digest size of
Gabor Mezei07732f72022-03-26 17:04:19 +0100482 * the hash algorithm represented by \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100483 * \param result The address at which to store the PSK binder on success.
484 * This must be writable, and its size must be equal to the
485 * digest size of the hash algorithm represented by
Gabor Mezei07732f72022-03-26 17:04:19 +0100486 * \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100487 *
488 * \returns \c 0 on success.
489 * \returns A negative error code on failure.
490 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200491MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100492int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl,
493 const psa_algorithm_t hash_alg,
494 unsigned char const *psk, size_t psk_len,
495 int psk_type,
496 unsigned char const *transcript,
497 unsigned char *result);
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100498
Hanno Beckerc94060c2021-03-22 07:50:44 +0000499/**
500 * \bref Setup an SSL transform structure representing the
501 * record protection mechanism used by TLS 1.3
502 *
503 * \param transform The SSL transform structure to be created. This must have
504 * been initialized through mbedtls_ssl_transform_init() and
505 * not used in any other way prior to calling this function.
506 * In particular, this function does not clean up the
507 * transform structure prior to installing the new keys.
508 * \param endpoint Indicates whether the transform is for the client
509 * (value #MBEDTLS_SSL_IS_CLIENT) or the server
510 * (value #MBEDTLS_SSL_IS_SERVER).
511 * \param ciphersuite The numerical identifier for the ciphersuite to use.
512 * This must be one of the identifiers listed in
513 * ssl_ciphersuites.h.
514 * \param traffic_keys The key material to use. No reference is stored in
515 * the SSL transform being generated, and the caller
516 * should destroy the key material afterwards.
517 * \param ssl (Debug-only) The SSL context to use for debug output
518 * in case of failure. This parameter is only needed if
519 * #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
520 *
521 * \return \c 0 on success. In this case, \p transform is ready to
522 * be used with mbedtls_ssl_transform_decrypt() and
523 * mbedtls_ssl_transform_encrypt().
524 * \return A negative error code on failure.
525 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200526MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100527int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform,
528 int endpoint,
529 int ciphersuite,
530 mbedtls_ssl_key_set const *traffic_keys,
531 mbedtls_ssl_context *ssl);
Hanno Beckerc94060c2021-03-22 07:50:44 +0000532
Jerry Yu89ea3212021-09-09 14:31:24 +0800533/*
534 * TLS 1.3 key schedule evolutions
535 *
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800536 * Early -> Handshake -> Application
Jerry Yu89ea3212021-09-09 14:31:24 +0800537 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000538 * Small wrappers around mbedtls_ssl_tls13_evolve_secret().
Jerry Yu89ea3212021-09-09 14:31:24 +0800539 */
540
541/**
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800542 * \brief Begin TLS 1.3 key schedule by calculating early secret.
Jerry Yu89ea3212021-09-09 14:31:24 +0800543 *
544 * The TLS 1.3 key schedule can be viewed as a simple state machine
545 * with states Initial -> Early -> Handshake -> Application, and
546 * this function represents the Initial -> Early transition.
547 *
Jerry Yu89ea3212021-09-09 14:31:24 +0800548 * \param ssl The SSL context to operate on.
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_key_schedule_stage_early(mbedtls_ssl_context *ssl);
Jerry Yu4925ef52021-09-09 14:42:55 +0800555
Jerry Yu61e35e02021-09-16 18:59:08 +0800556/**
Jerry Yuff226982022-04-16 16:52:57 +0800557 * \brief Compute TLS 1.3 resumption master secret.
558 *
559 * \param ssl The SSL context to operate on. This must be in
560 * key schedule stage \c Application, see
561 * mbedtls_ssl_tls13_key_schedule_stage_application().
562 *
563 * \returns \c 0 on success.
564 * \returns A negative error code on failure.
565 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200566MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100567int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl);
Jerry Yuff226982022-04-16 16:52:57 +0800568
569/**
XiaokangQianc5c39d52021-11-09 11:55:10 +0000570 * \brief Calculate the verify_data value for the client or server TLS 1.3
571 * Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000572 *
573 * \param ssl The SSL context to operate on. This must be in
574 * key schedule stage \c Handshake, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000575 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianc5c39d52021-11-09 11:55:10 +0000576 * \param dst The address at which to write the verify_data value.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000577 * \param dst_len The size of \p dst in bytes.
578 * \param actual_len The address at which to store the amount of data
579 * actually written to \p dst upon success.
XiaokangQianaaa0e192021-11-10 03:07:04 +0000580 * \param which The message to calculate the `verify_data` for:
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000581 * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
582 * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
583 *
584 * \note Both client and server call this function twice, once to
585 * generate their own Finished message, and once to verify the
586 * peer's Finished message.
587
588 * \returns \c 0 on success.
589 * \returns A negative error code on failure.
590 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200591MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100592int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl,
593 unsigned char *dst,
594 size_t dst_len,
595 size_t *actual_len,
596 int which);
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000597
Jerry Yu91b560f2022-11-04 14:10:34 +0800598#if defined(MBEDTLS_SSL_EARLY_DATA)
599/**
600 * \brief Compute TLS 1.3 early transform
601 *
Jerry Yub094e122022-11-21 13:03:47 +0800602 * \param ssl The SSL context to operate on.
Jerry Yu91b560f2022-11-04 14:10:34 +0800603 *
604 * \returns \c 0 on success.
605 * \returns A negative error code on failure.
606 *
Jerry Yu3ce61ff2022-11-21 22:45:58 +0800607 * \warning The function does not compute the early master secret. Call
608 * mbedtls_ssl_tls13_key_schedule_stage_early() before to
609 * call this function to generate the early master secret.
610 * \note For a client/server endpoint, the function computes only the
611 * encryption/decryption part of the transform as the decryption/
612 * encryption part is not defined by the specification (no early
613 * traffic from the server to the client).
Jerry Yu91b560f2022-11-04 14:10:34 +0800614 */
615MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100616int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl);
Jerry Yu91b560f2022-11-04 14:10:34 +0800617#endif /* MBEDTLS_SSL_EARLY_DATA */
618
Jerry Yue110d252022-05-05 10:19:22 +0800619/**
620 * \brief Compute TLS 1.3 handshake transform
621 *
Jerry Yuf86eb752022-05-06 11:16:55 +0800622 * \param ssl The SSL context to operate on. The early secret must have been
Jerry Yue110d252022-05-05 10:19:22 +0800623 * 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_handshake_transform(mbedtls_ssl_context *ssl);
Jerry Yue110d252022-05-05 10:19:22 +0800630
Jerry Yufd5ea042022-05-19 14:29:48 +0800631/**
632 * \brief Compute TLS 1.3 application transform
633 *
634 * \param ssl The SSL context to operate on. The early secret must have been
635 * computed.
636 *
637 * \returns \c 0 on success.
638 * \returns A negative error code on failure.
639 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200640MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100641int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl);
Jerry Yufd5ea042022-05-19 14:29:48 +0800642
Ronald Cron41a443a2022-10-04 16:38:25 +0200643#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu40f37712022-07-26 16:58:57 +0800644/**
Jerry Yu6cf6b472022-08-16 14:50:28 +0800645 * \brief Export TLS 1.3 PSK from handshake context
Jerry Yu40f37712022-07-26 16:58:57 +0800646 *
Jerry Yu01e42d22022-08-21 12:55:51 +0800647 * \param[in] ssl The SSL context to operate on.
648 * \param[out] psk PSK output pointer.
649 * \param[out] psk_len Length of PSK.
Jerry Yu40f37712022-07-26 16:58:57 +0800650 *
Jerry Yu29d9faa2022-08-23 17:52:45 +0800651 * \returns \c 0 if there is a configured PSK and it was exported
652 * successfully.
653 * \returns A negative error code on failure.
Jerry Yu40f37712022-07-26 16:58:57 +0800654 */
655MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100656int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
657 unsigned char **psk,
658 size_t *psk_len);
Ronald Cron41a443a2022-10-04 16:38:25 +0200659#endif
Jerry Yu40f37712022-07-26 16:58:57 +0800660
Gabor Mezeie42d8bf2022-03-30 11:33:06 +0200661#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
662
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100663#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */