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