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