blob: 928fa5850301057cfda93b9d005649f3af5886ef [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
Hanno Becker1413bd82020-09-09 12:46:09 +010047#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010048 const unsigned char name [ sizeof(string) - 1 ];
Hanno Beckerbe9d6642020-08-21 13:20:06 +010049
Xiaofei Bai746f9482021-11-12 08:53:56 +000050union mbedtls_ssl_tls13_labels_union
Hanno Beckerbe9d6642020-08-21 13:20:06 +010051{
52 MBEDTLS_SSL_TLS1_3_LABEL_LIST
53};
Xiaofei Bai746f9482021-11-12 08:53:56 +000054struct mbedtls_ssl_tls13_labels_struct
Hanno Beckerbe9d6642020-08-21 13:20:06 +010055{
56 MBEDTLS_SSL_TLS1_3_LABEL_LIST
57};
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010058#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010059
Xiaofei Bai746f9482021-11-12 08:53:56 +000060extern const struct mbedtls_ssl_tls13_labels_struct mbedtls_ssl_tls13_labels;
Hanno Beckerbe9d6642020-08-21 13:20:06 +010061
Jerry Yu0bbb3972021-09-19 20:27:17 +080062#define MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL ) \
Xiaofei Bai746f9482021-11-12 08:53:56 +000063 sizeof(mbedtls_ssl_tls13_labels.LABEL)
Jerry Yu0bbb3972021-09-19 20:27:17 +080064
Hanno Beckerbe9d6642020-08-21 13:20:06 +010065#define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \
Xiaofei Bai746f9482021-11-12 08:53:56 +000066 mbedtls_ssl_tls13_labels.LABEL, \
Jerry Yu0bbb3972021-09-19 20:27:17 +080067 MBEDTLS_SSL_TLS1_3_LBL_LEN( LABEL )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010068
69#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
Xiaofei Bai746f9482021-11-12 08:53:56 +000070 sizeof( union mbedtls_ssl_tls13_labels_union )
Hanno Beckerbe9d6642020-08-21 13:20:06 +010071
Hanno Becker61baae72020-09-16 09:24:14 +010072/* The maximum length of HKDF contexts used in the TLS 1.3 standard.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010073 * Since contexts are always hashes of message transcripts, this can
74 * be approximated from above by the maximum hash size. */
75#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
76 MBEDTLS_MD_MAX_SIZE
77
78/* Maximum desired length for expanded key material generated
Hanno Becker531fe302020-09-16 09:45:27 +010079 * by HKDF-Expand-Label.
80 *
81 * Warning: If this ever needs to be increased, the implementation
Xiaofei Bai746f9482021-11-12 08:53:56 +000082 * ssl_tls13_hkdf_encode_label() in ssl_tls13_keys.c needs to be
Hanno Becker531fe302020-09-16 09:45:27 +010083 * adjusted since it currently assumes that HKDF key expansion
84 * is never used with more than 255 Bytes of output. */
Hanno Beckerbe9d6642020-08-21 13:20:06 +010085#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
86
87/**
Xiaofei Bai89b526d2021-11-23 06:31:16 +000088 * \brief The \c HKDF-Expand-Label function from
89 * the TLS 1.3 standard RFC 8446.
Hanno Beckerbe9d6642020-08-21 13:20:06 +010090 *
91 * <tt>
Xiaofei Bai89b526d2021-11-23 06:31:16 +000092 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
Hanno Beckerbe9d6642020-08-21 13:20:06 +010093 * HKDF-Expand( Secret, HkdfLabel, Length )
94 * </tt>
95 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +000096 * \param hash_alg The identifier for the hash algorithm to use.
97 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +000098 * This must be a readable buffer of length
99 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000100 * \param secret_len The length of \p secret in Bytes.
101 * \param label The \c Label argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000102 * This must be a readable buffer of length
103 * \p label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000104 * \param label_len The length of \p label in Bytes.
105 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000106 * This must be a readable buffer of length \p ctx_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000107 * \param ctx_len The length of \p context in Bytes.
108 * \param buf The destination buffer to hold the expanded secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000109 * This must be a writable buffer of length \p buf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000110 * \param buf_len The desired size of the expanded secret in Bytes.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100111 *
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000112 * \returns \c 0 on success.
113 * \return A negative error code on failure.
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100114 */
115
Xiaofei Bai746f9482021-11-12 08:53:56 +0000116int mbedtls_ssl_tls13_hkdf_expand_label(
Gabor Mezei07732f72022-03-26 17:04:19 +0100117 psa_algorithm_t hash_alg,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000118 const unsigned char *secret, size_t secret_len,
119 const unsigned char *label, size_t label_len,
120 const unsigned char *ctx, size_t ctx_len,
121 unsigned char *buf, size_t buf_len );
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100122
Hanno Becker3385a4d2020-08-21 13:03:34 +0100123/**
124 * \brief This function is part of the TLS 1.3 key schedule.
125 * It extracts key and IV for the actual client/server traffic
126 * from the client/server traffic secrets.
127 *
128 * From RFC 8446:
129 *
130 * <tt>
131 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
132 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
133 * </tt>
134 *
135 * \param hash_alg The identifier for the hash algorithm to be used
136 * for the HKDF-based expansion of the secret.
137 * \param client_secret The client traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000138 * This must be a readable buffer of size
139 * \p secret_len Bytes
Hanno Becker3385a4d2020-08-21 13:03:34 +0100140 * \param server_secret The server traffic secret.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000141 * This must be a readable buffer of size
142 * \p secret_len Bytes
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000143 * \param secret_len Length of the secrets \p client_secret and
Hanno Becker3385a4d2020-08-21 13:03:34 +0100144 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100145 * \param key_len The desired length of the key to be extracted in Bytes.
146 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100147 * \param keys The address of the structure holding the generated
148 * keys and IVs.
149 *
150 * \returns \c 0 on success.
151 * \returns A negative error code on failure.
152 */
153
Xiaofei Bai746f9482021-11-12 08:53:56 +0000154int mbedtls_ssl_tls13_make_traffic_keys(
Gabor Mezei07732f72022-03-26 17:04:19 +0100155 psa_algorithm_t hash_alg,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100156 const unsigned char *client_secret,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000157 const unsigned char *server_secret, size_t secret_len,
158 size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100159 mbedtls_ssl_key_set *keys );
160
Hanno Becker0973ff92020-09-09 12:56:28 +0100161
162#define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
163#define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
164
Hanno Beckerb35d5222020-08-21 13:27:44 +0100165/**
166 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
167 *
168 * <tt>
169 * Derive-Secret( Secret, Label, Messages ) =
170 * HKDF-Expand-Label( Secret, Label,
171 * Hash( Messages ),
172 * Hash.Length ) )
173 * </tt>
174 *
Hanno Becker0c42fd92020-09-09 12:58:29 +0100175 * \param hash_alg The identifier for the hash function used for the
176 * applications of HKDF.
177 * \param secret The \c Secret argument to the \c Derive-Secret function.
Xiaofei Baid25fab62021-12-02 06:36:27 +0000178 * This must be a readable buffer of length
179 * \p secret_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000180 * \param secret_len The length of \p secret in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100181 * \param label The \c Label 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 label_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000184 * \param label_len The length of \p label in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100185 * \param ctx The hash of the \c Messages argument to the
186 * \c Derive-Secret function, or the \c Messages argument
Xiaofei Baid25fab62021-12-02 06:36:27 +0000187 * itself, depending on \p ctx_hashed.
188 * \param ctx_len The length of \p ctx in Bytes.
Hanno Becker0c42fd92020-09-09 12:58:29 +0100189 * \param ctx_hashed This indicates whether the \p ctx contains the hash of
190 * the \c Messages argument in the application of the
191 * \c Derive-Secret function
192 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
193 * it is the content of \c Messages itself, in which case
194 * the function takes care of the hashing
195 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
196 * \param dstbuf The target buffer to write the output of
197 * \c Derive-Secret to. This must be a writable buffer of
Xiaofei Baid25fab62021-12-02 06:36:27 +0000198 * size \p dtsbuf_len Bytes.
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000199 * \param dstbuf_len The length of \p dstbuf in Bytes.
Hanno Beckerb35d5222020-08-21 13:27:44 +0100200 *
201 * \returns \c 0 on success.
202 * \returns A negative error code on failure.
203 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000204int mbedtls_ssl_tls13_derive_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100205 psa_algorithm_t hash_alg,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000206 const unsigned char *secret, size_t secret_len,
207 const unsigned char *label, size_t label_len,
208 const unsigned char *ctx, size_t ctx_len,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100209 int ctx_hashed,
Xiaofei Bai89b526d2021-11-23 06:31:16 +0000210 unsigned char *dstbuf, size_t dstbuf_len );
Hanno Beckerb35d5222020-08-21 13:27:44 +0100211
Hanno Beckere9cccb42020-08-20 13:42:46 +0100212/**
Hanno Beckeref5235b2021-05-24 06:39:41 +0100213 * \brief Derive TLS 1.3 early data key material from early secret.
214 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000215 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100216 * with the appropriate labels.
217 *
218 * <tt>
219 * Early Secret
220 * |
221 * +-----> Derive-Secret(., "c e traffic", ClientHello)
222 * | = client_early_traffic_secret
223 * |
224 * +-----> Derive-Secret(., "e exp master", ClientHello)
225 * . = early_exporter_master_secret
226 * .
227 * .
228 * </tt>
229 *
230 * \note To obtain the actual key and IV for the early data traffic,
231 * the client secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000232 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100233 *
234 * \note The binder key, which is also generated from the early secret,
235 * is omitted here. Its calculation is part of the separate routine
Xiaofei Bai746f9482021-11-12 08:53:56 +0000236 * mbedtls_ssl_tls13_create_psk_binder().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100237 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100238 * \param hash_alg The hash algorithm associated with the PSK for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100239 * early data key material is being derived.
240 * \param early_secret The early secret from which the early data key material
241 * should be derived. This must be a readable buffer whose
242 * length is the digest size of the hash algorithm
243 * represented by \p md_size.
244 * \param transcript The transcript of the handshake so far, calculated with
Gabor Mezei07732f72022-03-26 17:04:19 +0100245 * respect to \p hash_alg. This must be a readable buffer
Hanno Beckeref5235b2021-05-24 06:39:41 +0100246 * whose length is the digest size of the hash algorithm
247 * represented by \p md_size.
248 * \param derived The address of the structure in which to store
249 * the early data key material.
250 *
251 * \returns \c 0 on success.
252 * \returns A negative error code on failure.
253 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000254int mbedtls_ssl_tls13_derive_early_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100255 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100256 unsigned char const *early_secret,
257 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000258 mbedtls_ssl_tls13_early_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100259
260/**
261 * \brief Derive TLS 1.3 handshake key material from the handshake secret.
262 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000263 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100264 * with the appropriate labels from the standard.
265 *
266 * <tt>
267 * Handshake Secret
268 * |
269 * +-----> Derive-Secret( ., "c hs traffic",
270 * | ClientHello...ServerHello )
271 * | = client_handshake_traffic_secret
272 * |
273 * +-----> Derive-Secret( ., "s hs traffic",
274 * . ClientHello...ServerHello )
275 * . = server_handshake_traffic_secret
276 * .
277 * </tt>
278 *
279 * \note To obtain the actual key and IV for the encrypted handshake traffic,
280 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000281 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100282 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100283 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100284 * that's being used for the connection.
285 * \param handshake_secret The handshake secret from which the handshake key
286 * material should be derived. This must be a readable
287 * buffer whose length is the digest size of the hash
288 * algorithm represented by \p md_size.
289 * \param transcript The transcript of the handshake so far, calculated
Gabor Mezei07732f72022-03-26 17:04:19 +0100290 * with respect to \p hash_alg. This must be a readable
Hanno Beckeref5235b2021-05-24 06:39:41 +0100291 * buffer whose length is the digest size of the hash
292 * algorithm represented by \p md_size.
293 * \param derived The address of the structure in which to
294 * store the handshake key material.
295 *
296 * \returns \c 0 on success.
297 * \returns A negative error code on failure.
298 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000299int mbedtls_ssl_tls13_derive_handshake_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100300 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100301 unsigned char const *handshake_secret,
302 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000303 mbedtls_ssl_tls13_handshake_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100304
305/**
306 * \brief Derive TLS 1.3 application key material from the master secret.
307 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000308 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100309 * with the appropriate labels from the standard.
310 *
311 * <tt>
312 * Master Secret
313 * |
314 * +-----> Derive-Secret( ., "c ap traffic",
315 * | ClientHello...server Finished )
316 * | = client_application_traffic_secret_0
317 * |
318 * +-----> Derive-Secret( ., "s ap traffic",
319 * | ClientHello...Server Finished )
320 * | = server_application_traffic_secret_0
321 * |
322 * +-----> Derive-Secret( ., "exp master",
323 * . ClientHello...server Finished)
324 * . = exporter_master_secret
325 * .
326 * </tt>
327 *
328 * \note To obtain the actual key and IV for the (0-th) application traffic,
329 * the client and server secret derived by this function need to be
Xiaofei Bai746f9482021-11-12 08:53:56 +0000330 * further processed by mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckeref5235b2021-05-24 06:39:41 +0100331 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100332 * \param hash_alg The hash algorithm associated with the ciphersuite
Hanno Beckeref5235b2021-05-24 06:39:41 +0100333 * that's being used for the connection.
334 * \param master_secret The master secret from which the application key
335 * material should be derived. This must be a readable
336 * buffer whose length is the digest size of the hash
337 * algorithm represented by \p md_size.
338 * \param transcript The transcript of the handshake up to and including
339 * the ServerFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100340 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100341 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100342 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100343 * \param derived The address of the structure in which to
344 * store the application key material.
345 *
346 * \returns \c 0 on success.
347 * \returns A negative error code on failure.
348 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000349int mbedtls_ssl_tls13_derive_application_secrets(
Gabor Mezei07732f72022-03-26 17:04:19 +0100350 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100351 unsigned char const *master_secret,
352 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000353 mbedtls_ssl_tls13_application_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100354
355/**
356 * \brief Derive TLS 1.3 resumption master secret from the master secret.
357 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000358 * This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret()
Hanno Beckeref5235b2021-05-24 06:39:41 +0100359 * with the appropriate labels from the standard.
360 *
Gabor Mezei07732f72022-03-26 17:04:19 +0100361 * \param hash_alg The hash algorithm used in the application for which
Hanno Beckeref5235b2021-05-24 06:39:41 +0100362 * key material is being derived.
363 * \param application_secret The application secret from which the resumption master
364 * secret should be derived. This must be a readable
365 * buffer whose length is the digest size of the hash
366 * algorithm represented by \p md_size.
367 * \param transcript The transcript of the handshake up to and including
368 * the ClientFinished message, calculated with respect
Gabor Mezei07732f72022-03-26 17:04:19 +0100369 * to \p hash_alg. This must be a readable buffer whose
Hanno Beckeref5235b2021-05-24 06:39:41 +0100370 * length is the digest size of the hash algorithm
Gabor Mezei07732f72022-03-26 17:04:19 +0100371 * represented by \p hash_alg.
Hanno Beckeref5235b2021-05-24 06:39:41 +0100372 * \param transcript_len The length of \p transcript in Bytes.
373 * \param derived The address of the structure in which to
374 * store the resumption master secret.
375 *
376 * \returns \c 0 on success.
377 * \returns A negative error code on failure.
378 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000379int mbedtls_ssl_tls13_derive_resumption_master_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100380 psa_algorithm_t hash_alg,
Hanno Beckeref5235b2021-05-24 06:39:41 +0100381 unsigned char const *application_secret,
382 unsigned char const *transcript, size_t transcript_len,
Xiaofei Bai746f9482021-11-12 08:53:56 +0000383 mbedtls_ssl_tls13_application_secrets *derived );
Hanno Beckeref5235b2021-05-24 06:39:41 +0100384
385/**
Hanno Beckere9cccb42020-08-20 13:42:46 +0100386 * \brief Compute the next secret in the TLS 1.3 key schedule
387 *
388 * The TLS 1.3 key schedule proceeds as follows to compute
389 * the three main secrets during the handshake: The early
390 * secret for early data, the handshake secret for all
391 * other encrypted handshake messages, and the master
392 * secret for all application traffic.
393 *
394 * <tt>
395 * 0
396 * |
397 * v
398 * PSK -> HKDF-Extract = Early Secret
399 * |
400 * v
401 * Derive-Secret( ., "derived", "" )
402 * |
403 * v
404 * (EC)DHE -> HKDF-Extract = Handshake Secret
405 * |
406 * v
407 * Derive-Secret( ., "derived", "" )
408 * |
409 * v
410 * 0 -> HKDF-Extract = Master Secret
411 * </tt>
412 *
413 * Each of the three secrets in turn is the basis for further
414 * key derivations, such as the derivation of traffic keys and IVs;
Xiaofei Bai746f9482021-11-12 08:53:56 +0000415 * see e.g. mbedtls_ssl_tls13_make_traffic_keys().
Hanno Beckere9cccb42020-08-20 13:42:46 +0100416 *
417 * This function implements one step in this evolution of secrets:
418 *
419 * <tt>
420 * old_secret
421 * |
422 * v
423 * Derive-Secret( ., "derived", "" )
424 * |
425 * v
426 * input -> HKDF-Extract = new_secret
427 * </tt>
428 *
429 * \param hash_alg The identifier for the hash function used for the
430 * applications of HKDF.
431 * \param secret_old The address of the buffer holding the old secret
432 * on function entry. If not \c NULL, this must be a
433 * readable buffer whose size matches the output size
434 * of the hash function represented by \p hash_alg.
435 * If \c NULL, an all \c 0 array will be used instead.
436 * \param input The address of the buffer holding the additional
437 * input for the key derivation (e.g., the PSK or the
438 * ephemeral (EC)DH secret). If not \c NULL, this must be
439 * a readable buffer whose size \p input_len Bytes.
440 * If \c NULL, an all \c 0 array will be used instead.
441 * \param input_len The length of \p input in Bytes.
442 * \param secret_new The address of the buffer holding the new secret
443 * on function exit. This must be a writable buffer
444 * whose size matches the output size of the hash
445 * function represented by \p hash_alg.
446 * This may be the same as \p secret_old.
447 *
448 * \returns \c 0 on success.
449 * \returns A negative error code on failure.
450 */
451
Xiaofei Bai746f9482021-11-12 08:53:56 +0000452int mbedtls_ssl_tls13_evolve_secret(
Gabor Mezei07732f72022-03-26 17:04:19 +0100453 psa_algorithm_t hash_alg,
Hanno Beckere9cccb42020-08-20 13:42:46 +0100454 const unsigned char *secret_old,
455 const unsigned char *input, size_t input_len,
456 unsigned char *secret_new );
457
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100458#define MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL 0
459#define MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION 1
460
461/**
462 * \brief Calculate a TLS 1.3 PSK binder.
463 *
464 * \param ssl The SSL context. This is used for debugging only and may
465 * be \c NULL if MBEDTLS_DEBUG_C is disabled.
Gabor Mezei07732f72022-03-26 17:04:19 +0100466 * \param hash_alg The hash algorithm associated to the PSK \p psk.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100467 * \param psk The buffer holding the PSK for which to create a binder.
468 * \param psk_len The size of \p psk in bytes.
Hanno Beckerc8d3ccd2021-05-26 04:47:29 +0100469 * \param psk_type This indicates whether the PSK \p psk is externally
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100470 * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a
471 * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION).
472 * \param transcript The handshake transcript up to the point where the
473 * PSK binder calculation happens. This must be readable,
474 * and its size must be equal to the digest size of
Gabor Mezei07732f72022-03-26 17:04:19 +0100475 * the hash algorithm represented by \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100476 * \param result The address at which to store the PSK binder on success.
477 * This must be writable, and its size must be equal to the
478 * digest size of the hash algorithm represented by
Gabor Mezei07732f72022-03-26 17:04:19 +0100479 * \p hash_alg.
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100480 *
481 * \returns \c 0 on success.
482 * \returns A negative error code on failure.
483 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000484int mbedtls_ssl_tls13_create_psk_binder( mbedtls_ssl_context *ssl,
Gabor Mezei07732f72022-03-26 17:04:19 +0100485 const psa_algorithm_t hash_alg,
Hanno Beckerb7d9bad2021-05-24 06:44:14 +0100486 unsigned char const *psk, size_t psk_len,
487 int psk_type,
488 unsigned char const *transcript,
489 unsigned char *result );
490
Hanno Beckerc94060c2021-03-22 07:50:44 +0000491/**
492 * \bref Setup an SSL transform structure representing the
493 * record protection mechanism used by TLS 1.3
494 *
495 * \param transform The SSL transform structure to be created. This must have
496 * been initialized through mbedtls_ssl_transform_init() and
497 * not used in any other way prior to calling this function.
498 * In particular, this function does not clean up the
499 * transform structure prior to installing the new keys.
500 * \param endpoint Indicates whether the transform is for the client
501 * (value #MBEDTLS_SSL_IS_CLIENT) or the server
502 * (value #MBEDTLS_SSL_IS_SERVER).
503 * \param ciphersuite The numerical identifier for the ciphersuite to use.
504 * This must be one of the identifiers listed in
505 * ssl_ciphersuites.h.
506 * \param traffic_keys The key material to use. No reference is stored in
507 * the SSL transform being generated, and the caller
508 * should destroy the key material afterwards.
509 * \param ssl (Debug-only) The SSL context to use for debug output
510 * in case of failure. This parameter is only needed if
511 * #MBEDTLS_DEBUG_C is set, and is ignored otherwise.
512 *
513 * \return \c 0 on success. In this case, \p transform is ready to
514 * be used with mbedtls_ssl_transform_decrypt() and
515 * mbedtls_ssl_transform_encrypt().
516 * \return A negative error code on failure.
517 */
518int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
519 int endpoint,
520 int ciphersuite,
521 mbedtls_ssl_key_set const *traffic_keys,
522 mbedtls_ssl_context *ssl );
523
Jerry Yu89ea3212021-09-09 14:31:24 +0800524/*
525 * TLS 1.3 key schedule evolutions
526 *
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800527 * Early -> Handshake -> Application
Jerry Yu89ea3212021-09-09 14:31:24 +0800528 *
Xiaofei Bai746f9482021-11-12 08:53:56 +0000529 * Small wrappers around mbedtls_ssl_tls13_evolve_secret().
Jerry Yu89ea3212021-09-09 14:31:24 +0800530 */
531
532/**
Jerry Yuc1ddeef2021-10-08 15:14:45 +0800533 * \brief Begin TLS 1.3 key schedule by calculating early secret.
Jerry Yu89ea3212021-09-09 14:31:24 +0800534 *
535 * The TLS 1.3 key schedule can be viewed as a simple state machine
536 * with states Initial -> Early -> Handshake -> Application, and
537 * this function represents the Initial -> Early transition.
538 *
Jerry Yu89ea3212021-09-09 14:31:24 +0800539 * \param ssl The SSL context to operate on.
540 *
541 * \returns \c 0 on success.
542 * \returns A negative error code on failure.
543 */
Xiaofei Bai746f9482021-11-12 08:53:56 +0000544int mbedtls_ssl_tls13_key_schedule_stage_early( mbedtls_ssl_context *ssl );
Jerry Yu4925ef52021-09-09 14:42:55 +0800545
Jerry Yu61e35e02021-09-16 18:59:08 +0800546/**
Jerry Yua0650eb2021-09-09 17:14:45 +0800547 * \brief Transition into handshake stage of TLS 1.3 key schedule.
548 *
549 * The TLS 1.3 key schedule can be viewed as a simple state machine
550 * with states Initial -> Early -> Handshake -> Application, and
551 * this function represents the Early -> Handshake transition.
552 *
Jerry Yuc068b662021-10-11 22:30:19 +0800553 * In the handshake stage, mbedtls_ssl_tls13_generate_handshake_keys()
Jerry Yua0650eb2021-09-09 17:14:45 +0800554 * can be used to derive the handshake traffic keys.
555 *
556 * \param ssl The SSL context to operate on. This must be in key schedule
557 * stage \c Early.
558 *
559 * \returns \c 0 on success.
560 * \returns A negative error code on failure.
561 */
Jerry Yuf0ac2352021-10-11 17:47:07 +0800562int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl );
Jerry Yua0650eb2021-09-09 17:14:45 +0800563
564/**
Jerry Yu61e35e02021-09-16 18:59:08 +0800565 * \brief Compute TLS 1.3 handshake traffic keys.
566 *
567 * \param ssl The SSL context to operate on. This must be in
568 * key schedule stage \c Handshake, see
Jerry Yuf0ac2352021-10-11 17:47:07 +0800569 * mbedtls_ssl_tls13_key_schedule_stage_handshake().
Jerry Yu61e35e02021-09-16 18:59:08 +0800570 * \param traffic_keys The address at which to store the handshake traffic key
571 * keys. This must be writable but may be uninitialized.
572 *
573 * \returns \c 0 on success.
574 * \returns A negative error code on failure.
575 */
Jerry Yuc068b662021-10-11 22:30:19 +0800576int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
577 mbedtls_ssl_key_set *traffic_keys );
Jerry Yu61e35e02021-09-16 18:59:08 +0800578
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000579/**
580 * \brief Transition into application stage of TLS 1.3 key schedule.
581 *
582 * The TLS 1.3 key schedule can be viewed as a simple state machine
583 * with states Initial -> Early -> Handshake -> Application, and
584 * this function represents the Handshake -> Application transition.
585 *
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000586 * In the handshake stage, mbedtls_ssl_tls13_generate_application_keys()
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000587 * can be used to derive the handshake traffic keys.
588 *
589 * \param ssl The SSL context to operate on. This must be in key schedule
590 * stage \c Handshake.
591 *
592 * \returns \c 0 on success.
593 * \returns A negative error code on failure.
594 */
XiaokangQiana4c99f22021-11-11 06:46:35 +0000595int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000596
597/**
598 * \brief Compute TLS 1.3 application traffic keys.
599 *
600 * \param ssl The SSL context to operate on. This must be in
601 * key schedule stage \c Application, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000602 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000603 * \param traffic_keys The address at which to store the application traffic key
604 * keys. This must be writable but may be uninitialized.
605 *
606 * \returns \c 0 on success.
607 * \returns A negative error code on failure.
608 */
XiaokangQiand0aa3e92021-11-10 06:17:40 +0000609int mbedtls_ssl_tls13_generate_application_keys(
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000610 mbedtls_ssl_context* ssl, mbedtls_ssl_key_set *traffic_keys );
611
612/**
XiaokangQianc5c39d52021-11-09 11:55:10 +0000613 * \brief Calculate the verify_data value for the client or server TLS 1.3
614 * Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000615 *
616 * \param ssl The SSL context to operate on. This must be in
617 * key schedule stage \c Handshake, see
XiaokangQian4cab0242021-10-12 08:43:37 +0000618 * mbedtls_ssl_tls13_key_schedule_stage_application().
XiaokangQianc5c39d52021-11-09 11:55:10 +0000619 * \param dst The address at which to write the verify_data value.
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000620 * \param dst_len The size of \p dst in bytes.
621 * \param actual_len The address at which to store the amount of data
622 * actually written to \p dst upon success.
XiaokangQianaaa0e192021-11-10 03:07:04 +0000623 * \param which The message to calculate the `verify_data` for:
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000624 * - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message
625 * - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message
626 *
627 * \note Both client and server call this function twice, once to
628 * generate their own Finished message, and once to verify the
629 * peer's Finished message.
630
631 * \returns \c 0 on success.
632 * \returns A negative error code on failure.
633 */
XiaokangQianc5c39d52021-11-09 11:55:10 +0000634int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context *ssl,
XiaokangQianaaa0e192021-11-10 03:07:04 +0000635 unsigned char *dst,
636 size_t dst_len,
637 size_t *actual_len,
638 int which );
XiaokangQianaa5f5c12021-09-18 06:20:25 +0000639
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100640#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */