blob: 479bb4e27b27045023432f58b6305e062ea5b206 [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
Xiaofei Bai746f9482021-11-12 08:53:56 +0000124int mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100125 psa_algorithm_t hash_alg,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000126 const unsigned char *secret, size_t secret_len,
127 const unsigned char *label, size_t label_len,
128 const unsigned char *ctx, size_t ctx_len,
129 unsigned char *buf, size_t buf_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100130
Hanno Becker3385a4d2020-08-21 13:03:34 +0100131/**
132 * \brief This function is part of the TLS 1.3 key schedule.
133 * It extracts key and IV for the actual client/server traffic
134 * from the client/server traffic secrets.
135 *
136 * From RFC 8446:
137 *
138 * <tt>
139 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
140 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
141 * </tt>
142 *
143 * \param hash_alg The identifier for the hash algorithm to be used
144 * for the HKDF-based expansion of the secret.
145 * \param client_secret The client traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000146 * This must be a readable buffer of size
147 * \p secret_len Bytes
Hanno Becker3385a4d2020-08-21 13:03:34 +0100148 * \param server_secret The server traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000149 * This must be a readable buffer of size
150 * \p secret_len Bytes
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000151 * \param secret_len Length of the secrets \p client_secret and
Hanno Becker3385a4d2020-08-21 13:03:34 +0100152 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100153 * \param key_len The desired length of the key to be extracted in Bytes.
154 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100155 * \param keys The address of the structure holding the generated
156 * keys and IVs.
157 *
158 * \returns \c 0 on success.
159 * \returns A negative error code on failure.
160 */
161
Xiaofei Bai746f9482021-11-12 08:53:56 +0000162int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100163 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100164 const unsigned char *client_secret,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000165 const unsigned char *server_secret, size_t secret_len,
166 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100167 mbedtls_ssl_key_set *keys );
168
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 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000208int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100209 psa_algorithm_t hash_alg,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000210 const unsigned char *secret, size_t secret_len,
211 const unsigned char *label, size_t label_len,
212 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100213 int ctx_hashed,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000214 unsigned char *dstbuf, size_t dstbuf_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100215
Hanno Beckere9cccb42020-08-20 13:42:46 +0100216/**
Hanno Beckeref5235b2021-05-24 06:39:41 +0100217 * \brief Derive TLS 1.3 early data key material from early secret.
218 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000219 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100220 * with the appropriate labels.
221 *
222 * <tt>
223 * Early Secret
224 * |
225 * +-----> Derive-Secret(., "c e traffic", ClientHello)
226 * | = client_early_traffic_secret
227 * |
228 * +-----> Derive-Secret(., "e exp master", ClientHello)
229 * . = early_exporter_master_secret
230 * .
231 * .
232 * </tt>
233 *
234 * \note To obtain the actual key and IV for the early data traffic,
235 * the client secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000236 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100237 *
238 * \note The binder key, which is also generated from the early secret,
239 * is omitted here. Its calculation is part of the separate routine
Xiaofei Bai746f9482021-11-12 08:53:56 +0000240 * mbedtls_ssl_tls13_create_psk_binder().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100241 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100242 * \param hash_alg The hash algorithm associated with the PSK for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100243 * early data key material is being derived.
244 * \param early_secret The early secret from which the early data key material
245 * should be derived. This must be a readable buffer whose
246 * length is the digest size of the hash algorithm
247 * represented by \p md_size.
248 * \param transcript The transcript of the handshake so far, calculated with
Gabor Mezei07732f72022-03-26 17:04:19 +0100249 * respect to \p hash_alg. This must be a readable buffer
Hanno Beckeref5235b2021-05-24 06:39:41 +0100250 * whose length is the digest size of the hash algorithm
251 * represented by \p md_size.
252 * \param derived The address of the structure in which to store
253 * the early data key material.
254 *
255 * \returns \c 0 on success.
256 * \returns A negative error code on failure.
257 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000258int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100259 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100260 unsigned char const *early_secret,
261 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000262 mbedtls_ssl_tls13_early_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100263
264/**
265 * \brief Derive TLS 1.3 handshake key material from the handshake secret.
266 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000267 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100268 * with the appropriate labels from the standard.
269 *
270 * <tt>
271 * Handshake Secret
272 * |
273 * +-----> Derive-Secret( ., "c hs traffic",
274 * | ClientHello...ServerHello )
275 * | = client_handshake_traffic_secret
276 * |
277 * +-----> Derive-Secret( ., "s hs traffic",
278 * . ClientHello...ServerHello )
279 * . = server_handshake_traffic_secret
280 * .
281 * </tt>
282 *
283 * \note To obtain the actual key and IV for the encrypted handshake traffic,
284 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000285 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100286 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100287 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100288 * that's being used for the connection.
289 * \param handshake_secret The handshake secret from which the handshake key
290 * material should be derived. This must be a readable
291 * buffer whose length is the digest size of the hash
292 * algorithm represented by \p md_size.
293 * \param transcript The transcript of the handshake so far, calculated
Gabor Mezei07732f72022-03-26 17:04:19 +0100294 * with respect to \p hash_alg. This must be a readable
Hanno Beckeref5235b2021-05-24 06:39:41 +0100295 * buffer whose length is the digest size of the hash
296 * algorithm represented by \p md_size.
297 * \param derived The address of the structure in which to
298 * store the handshake key material.
299 *
300 * \returns \c 0 on success.
301 * \returns A negative error code on failure.
302 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000303int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100304 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100305 unsigned char const *handshake_secret,
306 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000307 mbedtls_ssl_tls13_handshake_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100308
309/**
310 * \brief Derive TLS 1.3 application key material from the master secret.
311 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000312 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100313 * with the appropriate labels from the standard.
314 *
315 * <tt>
316 * Master Secret
317 * |
318 * +-----> Derive-Secret( ., "c ap traffic",
319 * | ClientHello...server Finished )
320 * | = client_application_traffic_secret_0
321 * |
322 * +-----> Derive-Secret( ., "s ap traffic",
323 * | ClientHello...Server Finished )
324 * | = server_application_traffic_secret_0
325 * |
326 * +-----> Derive-Secret( ., "exp master",
327 * . ClientHello...server Finished)
328 * . = exporter_master_secret
329 * .
330 * </tt>
331 *
332 * \note To obtain the actual key and IV for the (0-th) application traffic,
333 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000334 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100335 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100336 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100337 * that's being used for the connection.
338 * \param master_secret The master secret from which the application key
339 * material should be derived. This must be a readable
340 * buffer whose length is the digest size of the hash
341 * algorithm represented by \p md_size.
342 * \param transcript The transcript of the handshake up to and including
343 * the ServerFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100344 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100345 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100346 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100347 * \param derived The address of the structure in which to
348 * store the application key material.
349 *
350 * \returns \c 0 on success.
351 * \returns A negative error code on failure.
352 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000353int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100354 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100355 unsigned char const *master_secret,
356 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000357 mbedtls_ssl_tls13_application_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100358
359/**
360 * \brief Derive TLS 1.3 resumption master secret from the master secret.
361 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000362 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100363 * with the appropriate labels from the standard.
364 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100365 * \param hash_alg The hash algorithm used in the application for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100366 * key material is being derived.
367 * \param application_secret The application secret from which the resumption master
368 * secret should be derived. This must be a readable
369 * buffer whose length is the digest size of the hash
370 * algorithm represented by \p md_size.
371 * \param transcript The transcript of the handshake up to and including
372 * the ClientFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100373 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100374 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100375 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100376 * \param transcript_len The length of \p transcript in Bytes.
377 * \param derived The address of the structure in which to
378 * store the resumption master secret.
379 *
380 * \returns \c 0 on success.
381 * \returns A negative error code on failure.
382 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000383int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100384 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100385 unsigned char const *application_secret,
386 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000387 mbedtls_ssl_tls13_application_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100388
389/**
Hanno Beckere9cccb42020-08-20 13:42:46 +0100390 * \brief Compute the next secret in the TLS 1.3 key schedule
391 *
392 * The TLS 1.3 key schedule proceeds as follows to compute
393 * the three main secrets during the handshake: The early
394 * secret for early data, the handshake secret for all
395 * other encrypted handshake messages, and the master
396 * secret for all application traffic.
397 *
398 * <tt>
399 * 0
400 * |
401 * v
402 * PSK -> HKDF-Extract = Early Secret
403 * |
404 * v
405 * Derive-Secret( ., "derived", "" )
406 * |
407 * v
408 * (EC)DHE -> HKDF-Extract = Handshake Secret
409 * |
410 * v
411 * Derive-Secret( ., "derived", "" )
412 * |
413 * v
414 * 0 -> HKDF-Extract = Master Secret
415 * </tt>
416 *
417 * Each of the three secrets in turn is the basis for further
418 * key derivations, such as the derivation of traffic keys and IVs;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000419 * see e.g. mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckere9cccb42020-08-20 13:42:46 +0100420 *
421 * This function implements one step in this evolution of secrets:
422 *
423 * <tt>
424 * old_secret
425 * |
426 * v
427 * Derive-Secret( ., "derived", "" )
428 * |
429 * v
430 * input -> HKDF-Extract = new_secret
431 * </tt>
432 *
433 * \param hash_alg The identifier for the hash function used for the
434 * applications of HKDF.
435 * \param secret_old The address of the buffer holding the old secret
436 * on function entry. If not \c NULL, this must be a
437 * readable buffer whose size matches the output size
438 * of the hash function represented by \p hash_alg.
439 * If \c NULL, an all \c 0 array will be used instead.
440 * \param input The address of the buffer holding the additional
441 * input for the key derivation (e.g., the PSK or the
442 * ephemeral (EC)DH secret). If not \c NULL, this must be
443 * a readable buffer whose size \p input_len Bytes.
444 * If \c NULL, an all \c 0 array will be used instead.
445 * \param input_len The length of \p input in Bytes.
446 * \param secret_new The address of the buffer holding the new secret
447 * on function exit. This must be a writable buffer
448 * whose size matches the output size of the hash
449 * function represented by \p hash_alg.
450 * This may be the same as \p secret_old.
451 *
452 * \returns \c 0 on success.
453 * \returns A negative error code on failure.
454 */
455
Xiaofei Bai746f9482021-11-12 08:53:56 +0000456int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100457 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100458 const unsigned char *secret_old,
459 const unsigned char *input, size_t input_len,
460 unsigned char *secret_new );
461
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100462/**
463 * \brief Calculate a TLS 1.3 PSK binder.
464 *
465 * \param ssl The SSL context. This is used for debugging only and may
466 * be \c NULL if MBEDTLS_DEBUG_C is disabled.
Gabor Mezei07732f72022-03-26 17:04:19 +0100467 * \param hash_alg The hash algorithm associated to the PSK \p psk.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100468 * \param psk The buffer holding the PSK for which to create a binder.
469 * \param psk_len The size of \p psk in bytes.
Hanno Beckerc8d3ccd2021-05-26 04:47:29 +0100470 * \param psk_type This indicates whether the PSK \p psk is externally
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100471 * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
472 * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
473 * \param transcript The handshake transcript up to the point where the
474 * PSK binder calculation happens. This must be readable,
475 * and its size must be equal to the digest size of
Gabor Mezei07732f72022-03-26 17:04:19 +0100476 * the hash algorithm represented by \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100477 * \param result The address at which to store the PSK binder on success.
478 * This must be writable, and its size must be equal to the
479 * digest size of the hash algorithm represented by
Gabor Mezei07732f72022-03-26 17:04:19 +0100480 * \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100481 *
482 * \returns \c 0 on success.
483 * \returns A negative error code on failure.
484 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000485int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100486 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100487 unsigned char const *psk, size_t psk_len,
488 int psk_type,
489 unsigned char const *transcript,
490 unsigned char *result );
491
Hanno Beckerc94060c2021-03-22 07:50:44 +0000492/**
493 * \bref Setup an SSL transform structure representing the
494 * record protection mechanism used by TLS 1.3
495 *
496 * \param transform The SSL transform structure to be created. This must have
497 * been initialized through mbedtls_ssl_transform_init() and
498 * not used in any other way prior to calling this function.
499 * In particular, this function does not clean up the
500 * transform structure prior to installing the new keys.
501 * \param endpoint Indicates whether the transform is for the client
502 * (value #MBEDTLS_SSL_IS_CLIENT) or the server
503 * (value #MBEDTLS_SSL_IS_SERVER).
504 * \param ciphersuite The numerical identifier for the ciphersuite to use.
505 * This must be one of the identifiers listed in
506 * ssl_ciphersuites.h.
507 * \param traffic_keys The key material to use. No reference is stored in
508 * the SSL transform being generated, and the caller
509 * should destroy the key material afterwards.
510 * \param ssl (Debug-only) The SSL context to use for debug output
511 * in case of failure. This parameter is only needed if
512 * #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
513 *
514 * \return \c 0 on success. In this case, \p transform is ready to
515 * be used with mbedtls_ssl_transform_decrypt() and
516 * mbedtls_ssl_transform_encrypt().
517 * \return A negative error code on failure.
518 */
519int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
520 int endpoint,
521 int ciphersuite,
522 mbedtls_ssl_key_set const *traffic_keys,
523 mbedtls_ssl_context *ssl );
524
Jerry Yu89ea3212021-09-09 14:31:24 +0800525/*
526 * TLS 1.3 key schedule evolutions
527 *
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800528 * Early -> Handshake -> Application
Jerry Yu89ea3212021-09-09 14:31:24 +0800529 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000530 * Small wrappers around mbedtls_ssl_tls13_evolve_secret().
Jerry Yu89ea3212021-09-09 14:31:24 +0800531 */
532
533/**
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800534 * \brief Begin TLS 1.3 key schedule by calculating early secret.
Jerry Yu89ea3212021-09-09 14:31:24 +0800535 *
536 * The TLS 1.3 key schedule can be viewed as a simple state machine
537 * with states Initial -> Early -> Handshake -> Application, and
538 * this function represents the Initial -> Early transition.
539 *
Jerry Yu89ea3212021-09-09 14:31:24 +0800540 * \param ssl The SSL context to operate on.
541 *
542 * \returns \c 0 on success.
543 * \returns A negative error code on failure.
544 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000545int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl );
Jerry Yu4925ef52021-09-09 14:42:55 +0800546
Jerry Yu61e35e02021-09-16 18:59:08 +0800547/**
Jerry Yua0650eb2021-09-09 17:14:45 +0800548 * \brief Transition into handshake stage of TLS 1.3 key schedule.
549 *
550 * The TLS 1.3 key schedule can be viewed as a simple state machine
551 * with states Initial -> Early -> Handshake -> Application, and
552 * this function represents the Early -> Handshake transition.
553 *
Jerry Yuc068b662021-10-11 22:30:19 +0800554 * In the handshake stage, mbedtls_ssl_tls13_generate_handshake_keys()
Jerry Yua0650eb2021-09-09 17:14:45 +0800555 * can be used to derive the handshake traffic keys.
556 *
557 * \param ssl The SSL context to operate on. This must be in key schedule
558 * stage \c Early.
559 *
560 * \returns \c 0 on success.
561 * \returns A negative error code on failure.
562 */
Jerry Yuf0ac2352021-10-11 17:47:07 +0800563int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl );
Jerry Yua0650eb2021-09-09 17:14:45 +0800564
565/**
Jerry Yu61e35e02021-09-16 18:59:08 +0800566 * \brief Compute TLS 1.3 handshake traffic keys.
567 *
568 * \param ssl The SSL context to operate on. This must be in
569 * key schedule stage \c Handshake, see
Jerry Yuf0ac2352021-10-11 17:47:07 +0800570 * mbedtls_ssl_tls13_key_schedule_stage_handshake().
Jerry Yu61e35e02021-09-16 18:59:08 +0800571 * \param traffic_keys The address at which to store the handshake traffic key
572 * keys. This must be writable but may be uninitialized.
573 *
574 * \returns \c 0 on success.
575 * \returns A negative error code on failure.
576 */
Jerry Yuc068b662021-10-11 22:30:19 +0800577int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
578 mbedtls_ssl_key_set *traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +0800579
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000580/**
581 * \brief Transition into application stage of TLS 1.3 key schedule.
582 *
583 * The TLS 1.3 key schedule can be viewed as a simple state machine
584 * with states Initial -> Early -> Handshake -> Application, and
585 * this function represents the Handshake -> Application transition.
586 *
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000587 * In the handshake stage, mbedtls_ssl_tls13_generate_application_keys()
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000588 * can be used to derive the handshake traffic keys.
589 *
590 * \param ssl The SSL context to operate on. This must be in key schedule
591 * stage \c Handshake.
592 *
593 * \returns \c 0 on success.
594 * \returns A negative error code on failure.
595 */
XiaokangQiana4c99f22021-11-11 06:46:35 +0000596int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000597
598/**
599 * \brief Compute TLS 1.3 application traffic keys.
600 *
601 * \param ssl The SSL context to operate on. This must be in
602 * key schedule stage \c Application, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000603 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000604 * \param traffic_keys The address at which to store the application traffic key
605 * keys. This must be writable but may be uninitialized.
606 *
607 * \returns \c 0 on success.
608 * \returns A negative error code on failure.
609 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000610int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000611 mbedtls_ssl_context* ssl, mbedtls_ssl_key_set *traffic_keys );
612
613/**
Jerry Yuff226982022-04-16 16:52:57 +0800614 * \brief Compute TLS 1.3 resumption master secret.
615 *
616 * \param ssl The SSL context to operate on. This must be in
617 * key schedule stage \c Application, see
618 * mbedtls_ssl_tls13_key_schedule_stage_application().
619 *
620 * \returns \c 0 on success.
621 * \returns A negative error code on failure.
622 */
623int mbedtls_ssl_tls13_generate_resumption_master_secret(
624 mbedtls_ssl_context* ssl );
625
626/**
XiaokangQianc5c39d52021-11-09 11:55:10 +0000627 * \brief Calculate the verify_data value for the client or server TLS 1.3
628 * Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000629 *
630 * \param ssl The SSL context to operate on. This must be in
631 * key schedule stage \c Handshake, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000632 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianc5c39d52021-11-09 11:55:10 +0000633 * \param dst The address at which to write the verify_data value.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000634 * \param dst_len The size of \p dst in bytes.
635 * \param actual_len The address at which to store the amount of data
636 * actually written to \p dst upon success.
XiaokangQianaaa0e192021-11-10 03:07:04 +0000637 * \param which The message to calculate the `verify_data` for:
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000638 * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
639 * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
640 *
641 * \note Both client and server call this function twice, once to
642 * generate their own Finished message, and once to verify the
643 * peer's Finished message.
644
645 * \returns \c 0 on success.
646 * \returns A negative error code on failure.
647 */
XiaokangQianc5c39d52021-11-09 11:55:10 +0000648int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context *ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000649 unsigned char *dst,
650 size_t dst_len,
651 size_t *actual_len,
652 int which );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000653
Jerry Yue110d252022-05-05 10:19:22 +0800654/**
655 * \brief Compute TLS 1.3 handshake transform
656 *
Jerry Yuf86eb752022-05-06 11:16:55 +0800657 * \param ssl The SSL context to operate on. The early secret must have been
Jerry Yue110d252022-05-05 10:19:22 +0800658 * computed.
659 *
660 * \returns \c 0 on success.
661 * \returns A negative error code on failure.
662 */
Jerry Yuf86eb752022-05-06 11:16:55 +0800663int mbedtls_ssl_tls13_compute_handshake_transform( mbedtls_ssl_context *ssl );
Jerry Yue110d252022-05-05 10:19:22 +0800664
Gabor Mezeie42d8bf2022-03-30 11:33:06 +0200665#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
666
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100667#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */