blob: b80e02c979803f75fe69fed0e70a3cd6b52ee647 [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 \
26 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
Hanno Becker1413bd82020-09-09 12:46:09 +010055#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010056 const unsigned char name [ sizeof(string) - 1 ];
Hanno Beckerbe9d6642020-08-21 13:20:06 +010057
Xiaofei Bai746f9482021-11-12 08:53:56 +000058union mbedtls_ssl_tls13_labels_union
Hanno Beckerbe9d6642020-08-21 13:20:06 +010059{
60 MBEDTLS_SSL_TLS1_3_LABEL_LIST
61};
Xiaofei Bai746f9482021-11-12 08:53:56 +000062struct mbedtls_ssl_tls13_labels_struct
Hanno Beckerbe9d6642020-08-21 13:20:06 +010063{
64 MBEDTLS_SSL_TLS1_3_LABEL_LIST
65};
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010066#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010067
Xiaofei Bai746f9482021-11-12 08:53:56 +000068extern const struct mbedtls_ssl_tls13_labels_struct mbedtls_ssl_tls13_labels;
Hanno Beckerbe9d6642020-08-21 13:20:06 +010069
Jerry Yu0bbb3972021-09-19 20:27:17 +080070#define MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL ) \
Xiaofei Bai746f9482021-11-12 08:53:56 +000071 sizeof(mbedtls_ssl_tls13_labels.LABEL)
Jerry Yu0bbb3972021-09-19 20:27:17 +080072
Hanno Beckerbe9d6642020-08-21 13:20:06 +010073#define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \
Xiaofei Bai746f9482021-11-12 08:53:56 +000074 mbedtls_ssl_tls13_labels.LABEL, \
Jerry Yu0bbb3972021-09-19 20:27:17 +080075 MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010076
77#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
Xiaofei Bai746f9482021-11-12 08:53:56 +000078 sizeof( union mbedtls_ssl_tls13_labels_union )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010079
Hanno Becker61baae72020-09-16 09:24:14 +010080/* The maximum length of HKDF contexts used in the TLS 1.3 standard.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010081 * Since contexts are always hashes of message transcripts, this can
82 * be approximated from above by the maximum hash size. */
83#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
84 MBEDTLS_MD_MAX_SIZE
85
86/* Maximum desired length for expanded key material generated
Hanno Becker531fe302020-09-16 09:45:27 +010087 * by HKDF-Expand-Label.
88 *
89 * Warning: If this ever needs to be increased, the implementation
Xiaofei Bai746f9482021-11-12 08:53:56 +000090 * ssl_tls13_hkdf_encode_label() in ssl_tls13_keys.c needs to be
Hanno Becker531fe302020-09-16 09:45:27 +010091 * adjusted since it currently assumes that HKDF key expansion
92 * is never used with more than 255 Bytes of output. */
Hanno Beckerbe9d6642020-08-21 13:20:06 +010093#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
94
95/**
Xiaofei Bai89b526d2021-11-23 06:31:16 +000096 * \brief The \c HKDF-Expand-Label function from
97 * the TLS 1.3 standard RFC 8446.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010098 *
99 * <tt>
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000100 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100101 * HKDF-Expand( Secret, HkdfLabel, Length )
102 * </tt>
103 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000104 * \param hash_alg The identifier for the hash algorithm to use.
105 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000106 * This must be a readable buffer of length
107 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000108 * \param secret_len The length of \p secret in Bytes.
109 * \param label The \c Label argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000110 * This must be a readable buffer of length
111 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000112 * \param label_len The length of \p label in Bytes.
113 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000114 * This must be a readable buffer of length \p ctx_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000115 * \param ctx_len The length of \p context in Bytes.
116 * \param buf The destination buffer to hold the expanded secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000117 * This must be a writable buffer of length \p buf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000118 * \param buf_len The desired size of the expanded secret in Bytes.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100119 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000120 * \returns \c 0 on success.
121 * \return A negative error code on failure.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100122 */
123
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200124MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000125int mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100126 psa_algorithm_t hash_alg,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000127 const unsigned char *secret, size_t secret_len,
128 const unsigned char *label, size_t label_len,
129 const unsigned char *ctx, size_t ctx_len,
130 unsigned char *buf, size_t buf_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100131
Hanno Becker3385a4d2020-08-21 13:03:34 +0100132/**
133 * \brief This function is part of the TLS 1.3 key schedule.
134 * It extracts key and IV for the actual client/server traffic
135 * from the client/server traffic secrets.
136 *
137 * From RFC 8446:
138 *
139 * <tt>
140 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
141 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
142 * </tt>
143 *
144 * \param hash_alg The identifier for the hash algorithm to be used
145 * for the HKDF-based expansion of the secret.
146 * \param client_secret The client traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000147 * This must be a readable buffer of size
148 * \p secret_len Bytes
Hanno Becker3385a4d2020-08-21 13:03:34 +0100149 * \param server_secret The server traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000150 * This must be a readable buffer of size
151 * \p secret_len Bytes
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000152 * \param secret_len Length of the secrets \p client_secret and
Hanno Becker3385a4d2020-08-21 13:03:34 +0100153 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100154 * \param key_len The desired length of the key to be extracted in Bytes.
155 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100156 * \param keys The address of the structure holding the generated
157 * keys and IVs.
158 *
159 * \returns \c 0 on success.
160 * \returns A negative error code on failure.
161 */
162
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200163MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000164int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100165 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100166 const unsigned char *client_secret,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000167 const unsigned char *server_secret, size_t secret_len,
168 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100169 mbedtls_ssl_key_set *keys );
170
Hanno Beckerb35d5222020-08-21 13:27:44 +0100171/**
172 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
173 *
174 * <tt>
175 * Derive-Secret( Secret, Label, Messages ) =
176 * HKDF-Expand-Label( Secret, Label,
177 * Hash( Messages ),
178 * Hash.Length ) )
179 * </tt>
180 *
Hanno Becker0c42fd92020-09-09 12:58:29 +0100181 * \param hash_alg The identifier for the hash function used for the
182 * applications of HKDF.
183 * \param secret The \c Secret argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000184 * This must be a readable buffer of length
185 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000186 * \param secret_len The length of \p secret in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100187 * \param label The \c Label argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000188 * This must be a readable buffer of length
189 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000190 * \param label_len The length of \p label in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100191 * \param ctx The hash of the \c Messages argument to the
192 * \c Derive-Secret function, or the \c Messages argument
Xiaofei Baid25fab62021-12-02 06:36:27 +0000193 * itself, depending on \p ctx_hashed.
194 * \param ctx_len The length of \p ctx in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100195 * \param ctx_hashed This indicates whether the \p ctx contains the hash of
196 * the \c Messages argument in the application of the
197 * \c Derive-Secret function
198 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
199 * it is the content of \c Messages itself, in which case
200 * the function takes care of the hashing
201 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
202 * \param dstbuf The target buffer to write the output of
203 * \c Derive-Secret to. This must be a writable buffer of
Xiaofei Baid25fab62021-12-02 06:36:27 +0000204 * size \p dtsbuf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000205 * \param dstbuf_len The length of \p dstbuf in Bytes.
Hanno Beckerb35d5222020-08-21 13:27:44 +0100206 *
207 * \returns \c 0 on success.
208 * \returns A negative error code on failure.
209 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200210MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000211int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100212 psa_algorithm_t hash_alg,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000213 const unsigned char *secret, size_t secret_len,
214 const unsigned char *label, size_t label_len,
215 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100216 int ctx_hashed,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000217 unsigned char *dstbuf, size_t dstbuf_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100218
Hanno Beckere9cccb42020-08-20 13:42:46 +0100219/**
Hanno Beckeref5235b2021-05-24 06:39:41 +0100220 * \brief Derive TLS 1.3 early data key material from early secret.
221 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000222 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100223 * with the appropriate labels.
224 *
225 * <tt>
226 * Early Secret
227 * |
228 * +-----> Derive-Secret(., "c e traffic", ClientHello)
229 * | = client_early_traffic_secret
230 * |
231 * +-----> Derive-Secret(., "e exp master", ClientHello)
232 * . = early_exporter_master_secret
233 * .
234 * .
235 * </tt>
236 *
237 * \note To obtain the actual key and IV for the early data traffic,
238 * the client secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000239 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100240 *
241 * \note The binder key, which is also generated from the early secret,
242 * is omitted here. Its calculation is part of the separate routine
Xiaofei Bai746f9482021-11-12 08:53:56 +0000243 * mbedtls_ssl_tls13_create_psk_binder().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100244 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100245 * \param hash_alg The hash algorithm associated with the PSK for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100246 * early data key material is being derived.
247 * \param early_secret The early secret from which the early data key material
248 * should be derived. This must be a readable buffer whose
249 * length is the digest size of the hash algorithm
250 * represented by \p md_size.
251 * \param transcript The transcript of the handshake so far, calculated with
Gabor Mezei07732f72022-03-26 17:04:19 +0100252 * respect to \p hash_alg. This must be a readable buffer
Hanno Beckeref5235b2021-05-24 06:39:41 +0100253 * whose length is the digest size of the hash algorithm
254 * represented by \p md_size.
255 * \param derived The address of the structure in which to store
256 * the early data key material.
257 *
258 * \returns \c 0 on success.
259 * \returns A negative error code on failure.
260 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200261MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000262int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100263 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100264 unsigned char const *early_secret,
265 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000266 mbedtls_ssl_tls13_early_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100267
268/**
269 * \brief Derive TLS 1.3 handshake key material from the handshake secret.
270 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000271 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100272 * with the appropriate labels from the standard.
273 *
274 * <tt>
275 * Handshake Secret
276 * |
277 * +-----> Derive-Secret( ., "c hs traffic",
278 * | ClientHello...ServerHello )
279 * | = client_handshake_traffic_secret
280 * |
281 * +-----> Derive-Secret( ., "s hs traffic",
282 * . ClientHello...ServerHello )
283 * . = server_handshake_traffic_secret
284 * .
285 * </tt>
286 *
287 * \note To obtain the actual key and IV for the encrypted handshake traffic,
288 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000289 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100290 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100291 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100292 * that's being used for the connection.
293 * \param handshake_secret The handshake secret from which the handshake key
294 * material should be derived. This must be a readable
295 * buffer whose length is the digest size of the hash
296 * algorithm represented by \p md_size.
297 * \param transcript The transcript of the handshake so far, calculated
Gabor Mezei07732f72022-03-26 17:04:19 +0100298 * with respect to \p hash_alg. This must be a readable
Hanno Beckeref5235b2021-05-24 06:39:41 +0100299 * buffer whose length is the digest size of the hash
300 * algorithm represented by \p md_size.
301 * \param derived The address of the structure in which to
302 * store the handshake key material.
303 *
304 * \returns \c 0 on success.
305 * \returns A negative error code on failure.
306 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200307MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000308int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100309 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100310 unsigned char const *handshake_secret,
311 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000312 mbedtls_ssl_tls13_handshake_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100313
314/**
315 * \brief Derive TLS 1.3 application key material from the master secret.
316 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000317 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100318 * with the appropriate labels from the standard.
319 *
320 * <tt>
321 * Master Secret
322 * |
323 * +-----> Derive-Secret( ., "c ap traffic",
324 * | ClientHello...server Finished )
325 * | = client_application_traffic_secret_0
326 * |
327 * +-----> Derive-Secret( ., "s ap traffic",
328 * | ClientHello...Server Finished )
329 * | = server_application_traffic_secret_0
330 * |
331 * +-----> Derive-Secret( ., "exp master",
332 * . ClientHello...server Finished)
333 * . = exporter_master_secret
334 * .
335 * </tt>
336 *
337 * \note To obtain the actual key and IV for the (0-th) application traffic,
338 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000339 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100340 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100341 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100342 * that's being used for the connection.
343 * \param master_secret The master secret from which the application key
344 * material should be derived. This must be a readable
345 * buffer whose length is the digest size of the hash
346 * algorithm represented by \p md_size.
347 * \param transcript The transcript of the handshake up to and including
348 * the ServerFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100349 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100350 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100351 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100352 * \param derived The address of the structure in which to
353 * store the application key material.
354 *
355 * \returns \c 0 on success.
356 * \returns A negative error code on failure.
357 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200358MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000359int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100360 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100361 unsigned char const *master_secret,
362 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000363 mbedtls_ssl_tls13_application_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100364
365/**
366 * \brief Derive TLS 1.3 resumption master secret from the master secret.
367 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000368 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100369 * with the appropriate labels from the standard.
370 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100371 * \param hash_alg The hash algorithm used in the application for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100372 * key material is being derived.
373 * \param application_secret The application secret from which the resumption master
374 * secret should be derived. This must be a readable
375 * buffer whose length is the digest size of the hash
376 * algorithm represented by \p md_size.
377 * \param transcript The transcript of the handshake up to and including
378 * the ClientFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100379 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100380 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100381 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100382 * \param transcript_len The length of \p transcript in Bytes.
383 * \param derived The address of the structure in which to
384 * store the resumption master secret.
385 *
386 * \returns \c 0 on success.
387 * \returns A negative error code on failure.
388 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200389MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000390int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100391 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100392 unsigned char const *application_secret,
393 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000394 mbedtls_ssl_tls13_application_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100395
396/**
Hanno Beckere9cccb42020-08-20 13:42:46 +0100397 * \brief Compute the next secret in the TLS 1.3 key schedule
398 *
399 * The TLS 1.3 key schedule proceeds as follows to compute
400 * the three main secrets during the handshake: The early
401 * secret for early data, the handshake secret for all
402 * other encrypted handshake messages, and the master
403 * secret for all application traffic.
404 *
405 * <tt>
406 * 0
407 * |
408 * v
409 * PSK -> HKDF-Extract = Early Secret
410 * |
411 * v
412 * Derive-Secret( ., "derived", "" )
413 * |
414 * v
415 * (EC)DHE -> HKDF-Extract = Handshake Secret
416 * |
417 * v
418 * Derive-Secret( ., "derived", "" )
419 * |
420 * v
421 * 0 -> HKDF-Extract = Master Secret
422 * </tt>
423 *
424 * Each of the three secrets in turn is the basis for further
425 * key derivations, such as the derivation of traffic keys and IVs;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000426 * see e.g. mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckere9cccb42020-08-20 13:42:46 +0100427 *
428 * This function implements one step in this evolution of secrets:
429 *
430 * <tt>
431 * old_secret
432 * |
433 * v
434 * Derive-Secret( ., "derived", "" )
435 * |
436 * v
437 * input -> HKDF-Extract = new_secret
438 * </tt>
439 *
440 * \param hash_alg The identifier for the hash function used for the
441 * applications of HKDF.
442 * \param secret_old The address of the buffer holding the old secret
443 * on function entry. If not \c NULL, this must be a
444 * readable buffer whose size matches the output size
445 * of the hash function represented by \p hash_alg.
446 * If \c NULL, an all \c 0 array will be used instead.
447 * \param input The address of the buffer holding the additional
448 * input for the key derivation (e.g., the PSK or the
449 * ephemeral (EC)DH secret). If not \c NULL, this must be
450 * a readable buffer whose size \p input_len Bytes.
451 * If \c NULL, an all \c 0 array will be used instead.
452 * \param input_len The length of \p input in Bytes.
453 * \param secret_new The address of the buffer holding the new secret
454 * on function exit. This must be a writable buffer
455 * whose size matches the output size of the hash
456 * function represented by \p hash_alg.
457 * This may be the same as \p secret_old.
458 *
459 * \returns \c 0 on success.
460 * \returns A negative error code on failure.
461 */
462
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200463MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000464int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100465 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100466 const unsigned char *secret_old,
467 const unsigned char *input, size_t input_len,
468 unsigned char *secret_new );
469
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100470/**
471 * \brief Calculate a TLS 1.3 PSK binder.
472 *
473 * \param ssl The SSL context. This is used for debugging only and may
474 * be \c NULL if MBEDTLS_DEBUG_C is disabled.
Gabor Mezei07732f72022-03-26 17:04:19 +0100475 * \param hash_alg The hash algorithm associated to the PSK \p psk.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100476 * \param psk The buffer holding the PSK for which to create a binder.
477 * \param psk_len The size of \p psk in bytes.
Hanno Beckerc8d3ccd2021-05-26 04:47:29 +0100478 * \param psk_type This indicates whether the PSK \p psk is externally
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100479 * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
480 * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
481 * \param transcript The handshake transcript up to the point where the
482 * PSK binder calculation happens. This must be readable,
483 * and its size must be equal to the digest size of
Gabor Mezei07732f72022-03-26 17:04:19 +0100484 * the hash algorithm represented by \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100485 * \param result The address at which to store the PSK binder on success.
486 * This must be writable, and its size must be equal to the
487 * digest size of the hash algorithm represented by
Gabor Mezei07732f72022-03-26 17:04:19 +0100488 * \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100489 *
490 * \returns \c 0 on success.
491 * \returns A negative error code on failure.
492 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200493MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000494int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100495 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100496 unsigned char const *psk, size_t psk_len,
497 int psk_type,
498 unsigned char const *transcript,
499 unsigned char *result );
500
Hanno Beckerc94060c2021-03-22 07:50:44 +0000501/**
502 * \bref Setup an SSL transform structure representing the
503 * record protection mechanism used by TLS 1.3
504 *
505 * \param transform The SSL transform structure to be created. This must have
506 * been initialized through mbedtls_ssl_transform_init() and
507 * not used in any other way prior to calling this function.
508 * In particular, this function does not clean up the
509 * transform structure prior to installing the new keys.
510 * \param endpoint Indicates whether the transform is for the client
511 * (value #MBEDTLS_SSL_IS_CLIENT) or the server
512 * (value #MBEDTLS_SSL_IS_SERVER).
513 * \param ciphersuite The numerical identifier for the ciphersuite to use.
514 * This must be one of the identifiers listed in
515 * ssl_ciphersuites.h.
516 * \param traffic_keys The key material to use. No reference is stored in
517 * the SSL transform being generated, and the caller
518 * should destroy the key material afterwards.
519 * \param ssl (Debug-only) The SSL context to use for debug output
520 * in case of failure. This parameter is only needed if
521 * #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
522 *
523 * \return \c 0 on success. In this case, \p transform is ready to
524 * be used with mbedtls_ssl_transform_decrypt() and
525 * mbedtls_ssl_transform_encrypt().
526 * \return A negative error code on failure.
527 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200528MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerc94060c2021-03-22 07:50:44 +0000529int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
530 int endpoint,
531 int ciphersuite,
532 mbedtls_ssl_key_set const *traffic_keys,
533 mbedtls_ssl_context *ssl );
534
Jerry Yu89ea3212021-09-09 14:31:24 +0800535/*
536 * TLS 1.3 key schedule evolutions
537 *
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800538 * Early -> Handshake -> Application
Jerry Yu89ea3212021-09-09 14:31:24 +0800539 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000540 * Small wrappers around mbedtls_ssl_tls13_evolve_secret().
Jerry Yu89ea3212021-09-09 14:31:24 +0800541 */
542
543/**
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800544 * \brief Begin TLS 1.3 key schedule by calculating early secret.
Jerry Yu89ea3212021-09-09 14:31:24 +0800545 *
546 * The TLS 1.3 key schedule can be viewed as a simple state machine
547 * with states Initial -> Early -> Handshake -> Application, and
548 * this function represents the Initial -> Early transition.
549 *
Jerry Yu89ea3212021-09-09 14:31:24 +0800550 * \param ssl The SSL context to operate on.
551 *
552 * \returns \c 0 on success.
553 * \returns A negative error code on failure.
554 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200555MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai746f9482021-11-12 08:53:56 +0000556int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl );
Jerry Yu4925ef52021-09-09 14:42:55 +0800557
Jerry Yu61e35e02021-09-16 18:59:08 +0800558/**
Jerry Yua0650eb2021-09-09 17:14:45 +0800559 * \brief Transition into handshake stage of TLS 1.3 key schedule.
560 *
561 * The TLS 1.3 key schedule can be viewed as a simple state machine
562 * with states Initial -> Early -> Handshake -> Application, and
563 * this function represents the Early -> Handshake transition.
564 *
Jerry Yuc068b662021-10-11 22:30:19 +0800565 * In the handshake stage, mbedtls_ssl_tls13_generate_handshake_keys()
Jerry Yua0650eb2021-09-09 17:14:45 +0800566 * can be used to derive the handshake traffic keys.
567 *
568 * \param ssl The SSL context to operate on. This must be in key schedule
569 * stage \c Early.
570 *
571 * \returns \c 0 on success.
572 * \returns A negative error code on failure.
573 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200574MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf0ac2352021-10-11 17:47:07 +0800575int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl );
Jerry Yua0650eb2021-09-09 17:14:45 +0800576
577/**
Jerry Yu61e35e02021-09-16 18:59:08 +0800578 * \brief Compute TLS 1.3 handshake traffic keys.
579 *
580 * \param ssl The SSL context to operate on. This must be in
581 * key schedule stage \c Handshake, see
Jerry Yuf0ac2352021-10-11 17:47:07 +0800582 * mbedtls_ssl_tls13_key_schedule_stage_handshake().
Jerry Yu61e35e02021-09-16 18:59:08 +0800583 * \param traffic_keys The address at which to store the handshake traffic key
584 * keys. This must be writable but may be uninitialized.
585 *
586 * \returns \c 0 on success.
587 * \returns A negative error code on failure.
588 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200589MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc068b662021-10-11 22:30:19 +0800590int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
591 mbedtls_ssl_key_set *traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +0800592
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000593/**
594 * \brief Transition into application stage of TLS 1.3 key schedule.
595 *
596 * The TLS 1.3 key schedule can be viewed as a simple state machine
597 * with states Initial -> Early -> Handshake -> Application, and
598 * this function represents the Handshake -> Application transition.
599 *
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000600 * In the handshake stage, mbedtls_ssl_tls13_generate_application_keys()
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000601 * can be used to derive the handshake traffic keys.
602 *
603 * \param ssl The SSL context to operate on. This must be in key schedule
604 * stage \c Handshake.
605 *
606 * \returns \c 0 on success.
607 * \returns A negative error code on failure.
608 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200609MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiana4c99f22021-11-11 06:46:35 +0000610int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000611
612/**
613 * \brief Compute TLS 1.3 application traffic keys.
614 *
615 * \param ssl The SSL context to operate on. This must be in
616 * key schedule stage \c Application, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000617 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000618 * \param traffic_keys The address at which to store the application traffic key
619 * keys. This must be writable but may be uninitialized.
620 *
621 * \returns \c 0 on success.
622 * \returns A negative error code on failure.
623 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200624MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000625int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000626 mbedtls_ssl_context* ssl, mbedtls_ssl_key_set *traffic_keys );
627
628/**
Jerry Yuff226982022-04-16 16:52:57 +0800629 * \brief Compute TLS 1.3 resumption master secret.
630 *
631 * \param ssl The SSL context to operate on. This must be in
632 * key schedule stage \c Application, see
633 * mbedtls_ssl_tls13_key_schedule_stage_application().
634 *
635 * \returns \c 0 on success.
636 * \returns A negative error code on failure.
637 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200638MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu466dda82022-09-13 11:20:20 +0800639int mbedtls_ssl_tls13_compute_resumption_master_secret( mbedtls_ssl_context *ssl );
Jerry Yuff226982022-04-16 16:52:57 +0800640
641/**
XiaokangQianc5c39d52021-11-09 11:55:10 +0000642 * \brief Calculate the verify_data value for the client or server TLS 1.3
643 * Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000644 *
645 * \param ssl The SSL context to operate on. This must be in
646 * key schedule stage \c Handshake, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000647 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianc5c39d52021-11-09 11:55:10 +0000648 * \param dst The address at which to write the verify_data value.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000649 * \param dst_len The size of \p dst in bytes.
650 * \param actual_len The address at which to store the amount of data
651 * actually written to \p dst upon success.
XiaokangQianaaa0e192021-11-10 03:07:04 +0000652 * \param which The message to calculate the `verify_data` for:
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000653 * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
654 * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
655 *
656 * \note Both client and server call this function twice, once to
657 * generate their own Finished message, and once to verify the
658 * peer's Finished message.
659
660 * \returns \c 0 on success.
661 * \returns A negative error code on failure.
662 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200663MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianc5c39d52021-11-09 11:55:10 +0000664int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context *ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000665 unsigned char *dst,
666 size_t dst_len,
667 size_t *actual_len,
668 int which );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000669
Jerry Yue110d252022-05-05 10:19:22 +0800670/**
671 * \brief Compute TLS 1.3 handshake transform
672 *
Jerry Yuf86eb752022-05-06 11:16:55 +0800673 * \param ssl The SSL context to operate on. The early secret must have been
Jerry Yue110d252022-05-05 10:19:22 +0800674 * computed.
675 *
676 * \returns \c 0 on success.
677 * \returns A negative error code on failure.
678 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200679MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf86eb752022-05-06 11:16:55 +0800680int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl );
Jerry Yue110d252022-05-05 10:19:22 +0800681
Jerry Yufd5ea042022-05-19 14:29:48 +0800682/**
683 * \brief Compute TLS 1.3 application transform
684 *
685 * \param ssl The SSL context to operate on. The early secret must have been
686 * computed.
687 *
688 * \returns \c 0 on success.
689 * \returns A negative error code on failure.
690 */
Manuel Pégourié-Gonnarda82a8b92022-06-17 10:53:58 +0200691MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yufd5ea042022-05-19 14:29:48 +0800692int mbedtls_ssl_tls13_compute_application_transform( mbedtls_ssl_context *ssl );
693
Jerry Yu01e42d22022-08-21 12:55:51 +0800694#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Jerry Yu40f37712022-07-26 16:58:57 +0800695/**
Jerry Yu6cf6b472022-08-16 14:50:28 +0800696 * \brief Export TLS 1.3 PSK from handshake context
Jerry Yu40f37712022-07-26 16:58:57 +0800697 *
Jerry Yu01e42d22022-08-21 12:55:51 +0800698 * \param[in] ssl The SSL context to operate on.
699 * \param[out] psk PSK output pointer.
700 * \param[out] psk_len Length of PSK.
Jerry Yu40f37712022-07-26 16:58:57 +0800701 *
Jerry Yu29d9faa2022-08-23 17:52:45 +0800702 * \returns \c 0 if there is a configured PSK and it was exported
703 * successfully.
704 * \returns A negative error code on failure.
Jerry Yu40f37712022-07-26 16:58:57 +0800705 */
706MBEDTLS_CHECK_RETURN_CRITICAL
707int mbedtls_ssl_tls13_export_handshake_psk( mbedtls_ssl_context *ssl,
708 unsigned char **psk,
709 size_t *psk_len );
Jerry Yu01e42d22022-08-21 12:55:51 +0800710#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yu40f37712022-07-26 16:58:57 +0800711
Gabor Mezeie42d8bf2022-03-30 11:33:06 +0200712#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
713
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100714#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */