Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 1 | /* |
| 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 Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 22 | /* This requires MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) to be defined at |
Hanno Becker | a3a5a4e | 2020-09-08 11:33:48 +0100 | [diff] [blame] | 23 | * the point of use. See e.g. the definition of mbedtls_ssl_tls1_3_labels_union |
| 24 | * below. */ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 25 | #define MBEDTLS_SSL_TLS1_3_LABEL_LIST \ |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 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" ) |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 44 | |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 45 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 46 | const unsigned char name [ sizeof(string) - 1 ]; |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 47 | |
| 48 | union mbedtls_ssl_tls1_3_labels_union |
| 49 | { |
| 50 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 51 | }; |
| 52 | struct mbedtls_ssl_tls1_3_labels_struct |
| 53 | { |
| 54 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 55 | }; |
Hanno Becker | a3a5a4e | 2020-09-08 11:33:48 +0100 | [diff] [blame] | 56 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 57 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 58 | extern const struct mbedtls_ssl_tls1_3_labels_struct mbedtls_ssl_tls1_3_labels; |
| 59 | |
| 60 | #define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \ |
| 61 | mbedtls_ssl_tls1_3_labels.LABEL, \ |
| 62 | sizeof(mbedtls_ssl_tls1_3_labels.LABEL) |
| 63 | |
| 64 | #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \ |
| 65 | sizeof( union mbedtls_ssl_tls1_3_labels_union ) |
| 66 | |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 67 | /* The maximum length of HKDF contexts used in the TLS 1.3 standard. |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 68 | * Since contexts are always hashes of message transcripts, this can |
| 69 | * be approximated from above by the maximum hash size. */ |
| 70 | #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \ |
| 71 | MBEDTLS_MD_MAX_SIZE |
| 72 | |
| 73 | /* Maximum desired length for expanded key material generated |
Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 74 | * by HKDF-Expand-Label. |
| 75 | * |
| 76 | * Warning: If this ever needs to be increased, the implementation |
| 77 | * ssl_tls1_3_hkdf_encode_label() in ssl_tls13_keys.c needs to be |
| 78 | * adjusted since it currently assumes that HKDF key expansion |
| 79 | * is never used with more than 255 Bytes of output. */ |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 80 | #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255 |
| 81 | |
| 82 | /** |
| 83 | * \brief The \c HKDF-Expand-Label function from |
| 84 | * the TLS 1.3 standard RFC 8446. |
| 85 | * |
| 86 | * <tt> |
| 87 | * HKDF-Expand-Label( Secret, Label, Context, Length ) = |
| 88 | * HKDF-Expand( Secret, HkdfLabel, Length ) |
| 89 | * </tt> |
| 90 | * |
| 91 | * \param hash_alg The identifier for the hash algorithm to use. |
| 92 | * \param secret The \c Secret argument to \c HKDF-Expand-Label. |
| 93 | * This must be a readable buffer of length \p slen Bytes. |
| 94 | * \param slen The length of \p secret in Bytes. |
| 95 | * \param label The \c Label argument to \c HKDF-Expand-Label. |
| 96 | * This must be a readable buffer of length \p llen Bytes. |
| 97 | * \param llen The length of \p label in Bytes. |
| 98 | * \param ctx The \c Context argument to \c HKDF-Expand-Label. |
| 99 | * This must be a readable buffer of length \p clen Bytes. |
| 100 | * \param clen The length of \p context in Bytes. |
| 101 | * \param buf The destination buffer to hold the expanded secret. |
Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 102 | * This must be a writable buffer of length \p blen Bytes. |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 103 | * \param blen The desired size of the expanded secret in Bytes. |
| 104 | * |
| 105 | * \returns \c 0 on success. |
| 106 | * \return A negative error code on failure. |
| 107 | */ |
| 108 | |
| 109 | int mbedtls_ssl_tls1_3_hkdf_expand_label( |
| 110 | mbedtls_md_type_t hash_alg, |
| 111 | const unsigned char *secret, size_t slen, |
| 112 | const unsigned char *label, size_t llen, |
| 113 | const unsigned char *ctx, size_t clen, |
| 114 | unsigned char *buf, size_t blen ); |
| 115 | |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 116 | /** |
| 117 | * \brief This function is part of the TLS 1.3 key schedule. |
| 118 | * It extracts key and IV for the actual client/server traffic |
| 119 | * from the client/server traffic secrets. |
| 120 | * |
| 121 | * From RFC 8446: |
| 122 | * |
| 123 | * <tt> |
| 124 | * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length) |
| 125 | * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)* |
| 126 | * </tt> |
| 127 | * |
| 128 | * \param hash_alg The identifier for the hash algorithm to be used |
| 129 | * for the HKDF-based expansion of the secret. |
| 130 | * \param client_secret The client traffic secret. |
| 131 | * This must be a readable buffer of size \p slen Bytes |
| 132 | * \param server_secret The server traffic secret. |
| 133 | * This must be a readable buffer of size \p slen Bytes |
| 134 | * \param slen Length of the secrets \p client_secret and |
| 135 | * \p server_secret in Bytes. |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 136 | * \param key_len The desired length of the key to be extracted in Bytes. |
| 137 | * \param iv_len The desired length of the IV to be extracted in Bytes. |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 138 | * \param keys The address of the structure holding the generated |
| 139 | * keys and IVs. |
| 140 | * |
| 141 | * \returns \c 0 on success. |
| 142 | * \returns A negative error code on failure. |
| 143 | */ |
| 144 | |
| 145 | int mbedtls_ssl_tls1_3_make_traffic_keys( |
| 146 | mbedtls_md_type_t hash_alg, |
| 147 | const unsigned char *client_secret, |
| 148 | const unsigned char *server_secret, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 149 | size_t slen, size_t key_len, size_t iv_len, |
Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 150 | mbedtls_ssl_key_set *keys ); |
| 151 | |
Hanno Becker | 0973ff9 | 2020-09-09 12:56:28 +0100 | [diff] [blame] | 152 | |
| 153 | #define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0 |
| 154 | #define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1 |
| 155 | |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 156 | /** |
| 157 | * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446. |
| 158 | * |
| 159 | * <tt> |
| 160 | * Derive-Secret( Secret, Label, Messages ) = |
| 161 | * HKDF-Expand-Label( Secret, Label, |
| 162 | * Hash( Messages ), |
| 163 | * Hash.Length ) ) |
| 164 | * </tt> |
| 165 | * |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 166 | * \param hash_alg The identifier for the hash function used for the |
| 167 | * applications of HKDF. |
| 168 | * \param secret The \c Secret argument to the \c Derive-Secret function. |
| 169 | * This must be a readable buffer of length \p slen Bytes. |
| 170 | * \param slen The length of \p secret in Bytes. |
| 171 | * \param label The \c Label argument to the \c Derive-Secret function. |
| 172 | * This must be a readable buffer of length \p llen Bytes. |
| 173 | * \param llen The length of \p label in Bytes. |
| 174 | * \param ctx The hash of the \c Messages argument to the |
| 175 | * \c Derive-Secret function, or the \c Messages argument |
| 176 | * itself, depending on \p context_already_hashed. |
| 177 | * \param clen The length of \p hash. |
| 178 | * \param ctx_hashed This indicates whether the \p ctx contains the hash of |
| 179 | * the \c Messages argument in the application of the |
| 180 | * \c Derive-Secret function |
| 181 | * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether |
| 182 | * it is the content of \c Messages itself, in which case |
| 183 | * the function takes care of the hashing |
| 184 | * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED). |
| 185 | * \param dstbuf The target buffer to write the output of |
| 186 | * \c Derive-Secret to. This must be a writable buffer of |
| 187 | * size \p buflen Bytes. |
| 188 | * \param buflen The length of \p dstbuf in Bytes. |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 189 | * |
| 190 | * \returns \c 0 on success. |
| 191 | * \returns A negative error code on failure. |
| 192 | */ |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 193 | int mbedtls_ssl_tls1_3_derive_secret( |
| 194 | mbedtls_md_type_t hash_alg, |
| 195 | const unsigned char *secret, size_t slen, |
| 196 | const unsigned char *label, size_t llen, |
| 197 | const unsigned char *ctx, size_t clen, |
Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 198 | int ctx_hashed, |
Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 199 | unsigned char *dstbuf, size_t buflen ); |
| 200 | |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 201 | /** |
Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 202 | * \brief Derive TLS 1.3 early data key material from early secret. |
| 203 | * |
| 204 | * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret() |
| 205 | * with the appropriate labels. |
| 206 | * |
| 207 | * <tt> |
| 208 | * Early Secret |
| 209 | * | |
| 210 | * +-----> Derive-Secret(., "c e traffic", ClientHello) |
| 211 | * | = client_early_traffic_secret |
| 212 | * | |
| 213 | * +-----> Derive-Secret(., "e exp master", ClientHello) |
| 214 | * . = early_exporter_master_secret |
| 215 | * . |
| 216 | * . |
| 217 | * </tt> |
| 218 | * |
| 219 | * \note To obtain the actual key and IV for the early data traffic, |
| 220 | * the client secret derived by this function need to be |
| 221 | * further processed by mbedtls_ssl_tls1_3_make_traffic_keys(). |
| 222 | * |
| 223 | * \note The binder key, which is also generated from the early secret, |
| 224 | * is omitted here. Its calculation is part of the separate routine |
| 225 | * mbedtls_ssl_tls1_3_create_psk_binder(). |
| 226 | * |
| 227 | * \param md_type The hash algorithm associated with the PSK for which |
| 228 | * early data key material is being derived. |
| 229 | * \param early_secret The early secret from which the early data key material |
| 230 | * should be derived. This must be a readable buffer whose |
| 231 | * length is the digest size of the hash algorithm |
| 232 | * represented by \p md_size. |
| 233 | * \param transcript The transcript of the handshake so far, calculated with |
| 234 | * respect to \p md_type. This must be a readable buffer |
| 235 | * whose length is the digest size of the hash algorithm |
| 236 | * represented by \p md_size. |
| 237 | * \param derived The address of the structure in which to store |
| 238 | * the early data key material. |
| 239 | * |
| 240 | * \returns \c 0 on success. |
| 241 | * \returns A negative error code on failure. |
| 242 | */ |
| 243 | int mbedtls_ssl_tls1_3_derive_early_secrets( |
| 244 | mbedtls_md_type_t md_type, |
| 245 | unsigned char const *early_secret, |
| 246 | unsigned char const *transcript, size_t transcript_len, |
| 247 | mbedtls_ssl_tls1_3_early_secrets *derived ); |
| 248 | |
| 249 | /** |
| 250 | * \brief Derive TLS 1.3 handshake key material from the handshake secret. |
| 251 | * |
| 252 | * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret() |
| 253 | * with the appropriate labels from the standard. |
| 254 | * |
| 255 | * <tt> |
| 256 | * Handshake Secret |
| 257 | * | |
| 258 | * +-----> Derive-Secret( ., "c hs traffic", |
| 259 | * | ClientHello...ServerHello ) |
| 260 | * | = client_handshake_traffic_secret |
| 261 | * | |
| 262 | * +-----> Derive-Secret( ., "s hs traffic", |
| 263 | * . ClientHello...ServerHello ) |
| 264 | * . = server_handshake_traffic_secret |
| 265 | * . |
| 266 | * </tt> |
| 267 | * |
| 268 | * \note To obtain the actual key and IV for the encrypted handshake traffic, |
| 269 | * the client and server secret derived by this function need to be |
| 270 | * further processed by mbedtls_ssl_tls1_3_make_traffic_keys(). |
| 271 | * |
| 272 | * \param md_type The hash algorithm associated with the ciphersuite |
| 273 | * that's being used for the connection. |
| 274 | * \param handshake_secret The handshake secret from which the handshake key |
| 275 | * material should be derived. This must be a readable |
| 276 | * buffer whose length is the digest size of the hash |
| 277 | * algorithm represented by \p md_size. |
| 278 | * \param transcript The transcript of the handshake so far, calculated |
| 279 | * with respect to \p md_type. This must be a readable |
| 280 | * buffer whose length is the digest size of the hash |
| 281 | * algorithm represented by \p md_size. |
| 282 | * \param derived The address of the structure in which to |
| 283 | * store the handshake key material. |
| 284 | * |
| 285 | * \returns \c 0 on success. |
| 286 | * \returns A negative error code on failure. |
| 287 | */ |
| 288 | int mbedtls_ssl_tls1_3_derive_handshake_secrets( |
| 289 | mbedtls_md_type_t md_type, |
| 290 | unsigned char const *handshake_secret, |
| 291 | unsigned char const *transcript, size_t transcript_len, |
| 292 | mbedtls_ssl_tls1_3_handshake_secrets *derived ); |
| 293 | |
| 294 | /** |
| 295 | * \brief Derive TLS 1.3 application key material from the master secret. |
| 296 | * |
| 297 | * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret() |
| 298 | * with the appropriate labels from the standard. |
| 299 | * |
| 300 | * <tt> |
| 301 | * Master Secret |
| 302 | * | |
| 303 | * +-----> Derive-Secret( ., "c ap traffic", |
| 304 | * | ClientHello...server Finished ) |
| 305 | * | = client_application_traffic_secret_0 |
| 306 | * | |
| 307 | * +-----> Derive-Secret( ., "s ap traffic", |
| 308 | * | ClientHello...Server Finished ) |
| 309 | * | = server_application_traffic_secret_0 |
| 310 | * | |
| 311 | * +-----> Derive-Secret( ., "exp master", |
| 312 | * . ClientHello...server Finished) |
| 313 | * . = exporter_master_secret |
| 314 | * . |
| 315 | * </tt> |
| 316 | * |
| 317 | * \note To obtain the actual key and IV for the (0-th) application traffic, |
| 318 | * the client and server secret derived by this function need to be |
| 319 | * further processed by mbedtls_ssl_tls1_3_make_traffic_keys(). |
| 320 | * |
| 321 | * \param md_type The hash algorithm associated with the ciphersuite |
| 322 | * that's being used for the connection. |
| 323 | * \param master_secret The master secret from which the application key |
| 324 | * material should be derived. This must be a readable |
| 325 | * buffer whose length is the digest size of the hash |
| 326 | * algorithm represented by \p md_size. |
| 327 | * \param transcript The transcript of the handshake up to and including |
| 328 | * the ServerFinished message, calculated with respect |
| 329 | * to \p md_type. This must be a readable buffer whose |
| 330 | * length is the digest size of the hash algorithm |
| 331 | * represented by \p md_type. |
| 332 | * \param derived The address of the structure in which to |
| 333 | * store the application key material. |
| 334 | * |
| 335 | * \returns \c 0 on success. |
| 336 | * \returns A negative error code on failure. |
| 337 | */ |
| 338 | int mbedtls_ssl_tls1_3_derive_application_secrets( |
| 339 | mbedtls_md_type_t md_type, |
| 340 | unsigned char const *master_secret, |
| 341 | unsigned char const *transcript, size_t transcript_len, |
| 342 | mbedtls_ssl_tls1_3_application_secrets *derived ); |
| 343 | |
| 344 | /** |
| 345 | * \brief Derive TLS 1.3 resumption master secret from the master secret. |
| 346 | * |
| 347 | * This is a small wrapper invoking mbedtls_ssl_tls1_3_derive_secret() |
| 348 | * with the appropriate labels from the standard. |
| 349 | * |
| 350 | * \param md_type The hash algorithm used in the application for which |
| 351 | * key material is being derived. |
| 352 | * \param application_secret The application secret from which the resumption master |
| 353 | * secret should be derived. This must be a readable |
| 354 | * buffer whose length is the digest size of the hash |
| 355 | * algorithm represented by \p md_size. |
| 356 | * \param transcript The transcript of the handshake up to and including |
| 357 | * the ClientFinished message, calculated with respect |
| 358 | * to \p md_type. This must be a readable buffer whose |
| 359 | * length is the digest size of the hash algorithm |
| 360 | * represented by \p md_type. |
| 361 | * \param transcript_len The length of \p transcript in Bytes. |
| 362 | * \param derived The address of the structure in which to |
| 363 | * store the resumption master secret. |
| 364 | * |
| 365 | * \returns \c 0 on success. |
| 366 | * \returns A negative error code on failure. |
| 367 | */ |
| 368 | int mbedtls_ssl_tls1_3_derive_resumption_master_secret( |
| 369 | mbedtls_md_type_t md_type, |
| 370 | unsigned char const *application_secret, |
| 371 | unsigned char const *transcript, size_t transcript_len, |
| 372 | mbedtls_ssl_tls1_3_application_secrets *derived ); |
| 373 | |
| 374 | /** |
Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 375 | * \brief Compute the next secret in the TLS 1.3 key schedule |
| 376 | * |
| 377 | * The TLS 1.3 key schedule proceeds as follows to compute |
| 378 | * the three main secrets during the handshake: The early |
| 379 | * secret for early data, the handshake secret for all |
| 380 | * other encrypted handshake messages, and the master |
| 381 | * secret for all application traffic. |
| 382 | * |
| 383 | * <tt> |
| 384 | * 0 |
| 385 | * | |
| 386 | * v |
| 387 | * PSK -> HKDF-Extract = Early Secret |
| 388 | * | |
| 389 | * v |
| 390 | * Derive-Secret( ., "derived", "" ) |
| 391 | * | |
| 392 | * v |
| 393 | * (EC)DHE -> HKDF-Extract = Handshake Secret |
| 394 | * | |
| 395 | * v |
| 396 | * Derive-Secret( ., "derived", "" ) |
| 397 | * | |
| 398 | * v |
| 399 | * 0 -> HKDF-Extract = Master Secret |
| 400 | * </tt> |
| 401 | * |
| 402 | * Each of the three secrets in turn is the basis for further |
| 403 | * key derivations, such as the derivation of traffic keys and IVs; |
| 404 | * see e.g. mbedtls_ssl_tls1_3_make_traffic_keys(). |
| 405 | * |
| 406 | * This function implements one step in this evolution of secrets: |
| 407 | * |
| 408 | * <tt> |
| 409 | * old_secret |
| 410 | * | |
| 411 | * v |
| 412 | * Derive-Secret( ., "derived", "" ) |
| 413 | * | |
| 414 | * v |
| 415 | * input -> HKDF-Extract = new_secret |
| 416 | * </tt> |
| 417 | * |
| 418 | * \param hash_alg The identifier for the hash function used for the |
| 419 | * applications of HKDF. |
| 420 | * \param secret_old The address of the buffer holding the old secret |
| 421 | * on function entry. If not \c NULL, this must be a |
| 422 | * readable buffer whose size matches the output size |
| 423 | * of the hash function represented by \p hash_alg. |
| 424 | * If \c NULL, an all \c 0 array will be used instead. |
| 425 | * \param input The address of the buffer holding the additional |
| 426 | * input for the key derivation (e.g., the PSK or the |
| 427 | * ephemeral (EC)DH secret). If not \c NULL, this must be |
| 428 | * a readable buffer whose size \p input_len Bytes. |
| 429 | * If \c NULL, an all \c 0 array will be used instead. |
| 430 | * \param input_len The length of \p input in Bytes. |
| 431 | * \param secret_new The address of the buffer holding the new secret |
| 432 | * on function exit. This must be a writable buffer |
| 433 | * whose size matches the output size of the hash |
| 434 | * function represented by \p hash_alg. |
| 435 | * This may be the same as \p secret_old. |
| 436 | * |
| 437 | * \returns \c 0 on success. |
| 438 | * \returns A negative error code on failure. |
| 439 | */ |
| 440 | |
| 441 | int mbedtls_ssl_tls1_3_evolve_secret( |
| 442 | mbedtls_md_type_t hash_alg, |
| 443 | const unsigned char *secret_old, |
| 444 | const unsigned char *input, size_t input_len, |
| 445 | unsigned char *secret_new ); |
| 446 | |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 447 | #define MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL 0 |
| 448 | #define MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION 1 |
| 449 | |
| 450 | /** |
| 451 | * \brief Calculate a TLS 1.3 PSK binder. |
| 452 | * |
| 453 | * \param ssl The SSL context. This is used for debugging only and may |
| 454 | * be \c NULL if MBEDTLS_DEBUG_C is disabled. |
| 455 | * \param md_type The hash algorithm associated to the PSK \p psk. |
| 456 | * \param psk The buffer holding the PSK for which to create a binder. |
| 457 | * \param psk_len The size of \p psk in bytes. |
Hanno Becker | c8d3ccd | 2021-05-26 04:47:29 +0100 | [diff] [blame] | 458 | * \param psk_type This indicates whether the PSK \p psk is externally |
Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 459 | * provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a |
| 460 | * resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION). |
| 461 | * \param transcript The handshake transcript up to the point where the |
| 462 | * PSK binder calculation happens. This must be readable, |
| 463 | * and its size must be equal to the digest size of |
| 464 | * the hash algorithm represented by \p md_type. |
| 465 | * \param result The address at which to store the PSK binder on success. |
| 466 | * This must be writable, and its size must be equal to the |
| 467 | * digest size of the hash algorithm represented by |
| 468 | * \p md_type. |
| 469 | * |
| 470 | * \returns \c 0 on success. |
| 471 | * \returns A negative error code on failure. |
| 472 | */ |
| 473 | int mbedtls_ssl_tls1_3_create_psk_binder( mbedtls_ssl_context *ssl, |
| 474 | const mbedtls_md_type_t md_type, |
| 475 | unsigned char const *psk, size_t psk_len, |
| 476 | int psk_type, |
| 477 | unsigned char const *transcript, |
| 478 | unsigned char *result ); |
| 479 | |
Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 480 | /** |
| 481 | * \bref Setup an SSL transform structure representing the |
| 482 | * record protection mechanism used by TLS 1.3 |
| 483 | * |
| 484 | * \param transform The SSL transform structure to be created. This must have |
| 485 | * been initialized through mbedtls_ssl_transform_init() and |
| 486 | * not used in any other way prior to calling this function. |
| 487 | * In particular, this function does not clean up the |
| 488 | * transform structure prior to installing the new keys. |
| 489 | * \param endpoint Indicates whether the transform is for the client |
| 490 | * (value #MBEDTLS_SSL_IS_CLIENT) or the server |
| 491 | * (value #MBEDTLS_SSL_IS_SERVER). |
| 492 | * \param ciphersuite The numerical identifier for the ciphersuite to use. |
| 493 | * This must be one of the identifiers listed in |
| 494 | * ssl_ciphersuites.h. |
| 495 | * \param traffic_keys The key material to use. No reference is stored in |
| 496 | * the SSL transform being generated, and the caller |
| 497 | * should destroy the key material afterwards. |
| 498 | * \param ssl (Debug-only) The SSL context to use for debug output |
| 499 | * in case of failure. This parameter is only needed if |
| 500 | * #MBEDTLS_DEBUG_C is set, and is ignored otherwise. |
| 501 | * |
| 502 | * \return \c 0 on success. In this case, \p transform is ready to |
| 503 | * be used with mbedtls_ssl_transform_decrypt() and |
| 504 | * mbedtls_ssl_transform_encrypt(). |
| 505 | * \return A negative error code on failure. |
| 506 | */ |
| 507 | int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform, |
| 508 | int endpoint, |
| 509 | int ciphersuite, |
| 510 | mbedtls_ssl_key_set const *traffic_keys, |
| 511 | mbedtls_ssl_context *ssl ); |
| 512 | |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 513 | /* |
| 514 | * TLS 1.3 key schedule evolutions |
| 515 | * |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 516 | * Early -> Handshake -> Application |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 517 | * |
| 518 | * Small wrappers around mbedtls_ssl_tls1_3_evolve_secret(). |
| 519 | */ |
| 520 | |
| 521 | /** |
Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 522 | * \brief Begin TLS 1.3 key schedule by calculating early secret. |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 523 | * |
| 524 | * The TLS 1.3 key schedule can be viewed as a simple state machine |
| 525 | * with states Initial -> Early -> Handshake -> Application, and |
| 526 | * this function represents the Initial -> Early transition. |
| 527 | * |
Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 528 | * \param ssl The SSL context to operate on. |
| 529 | * |
| 530 | * \returns \c 0 on success. |
| 531 | * \returns A negative error code on failure. |
| 532 | */ |
Jerry Yu | 4836952 | 2021-09-18 16:09:01 +0800 | [diff] [blame] | 533 | int mbedtls_ssl_tls1_3_key_schedule_stage_early( mbedtls_ssl_context *ssl ); |
Jerry Yu | 4925ef5 | 2021-09-09 14:42:55 +0800 | [diff] [blame] | 534 | |
Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame^] | 535 | /** |
| 536 | * \brief Compute TLS 1.3 handshake traffic keys. |
| 537 | * |
| 538 | * \param ssl The SSL context to operate on. This must be in |
| 539 | * key schedule stage \c Handshake, see |
| 540 | * mbedtls_ssl_tls13_key_schedule_stage_handshake(). |
| 541 | * \param traffic_keys The address at which to store the handshake traffic key |
| 542 | * keys. This must be writable but may be uninitialized. |
| 543 | * |
| 544 | * \returns \c 0 on success. |
| 545 | * \returns A negative error code on failure. |
| 546 | */ |
| 547 | int mbedtls_ssl_tls1_3_generate_handshake_keys( mbedtls_ssl_context *ssl, |
| 548 | mbedtls_ssl_key_set *traffic_keys ); |
| 549 | |
Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 550 | #endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */ |