| 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 | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 23 | * the point of use. See e.g. the definition of mbedtls_ssl_tls13_labels_union | 
| Hanno Becker | a3a5a4e | 2020-09-08 11:33:48 +0100 | [diff] [blame] | 24 | * below. */ | 
| Jerry Yu | 0bbb397 | 2021-09-19 20:27:17 +0800 | [diff] [blame] | 25 | #define MBEDTLS_SSL_TLS1_3_LABEL_LIST                                             \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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") \ | 
|  | 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 Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 46 |  | 
| Gabor Mezei | e42d8bf | 2022-03-30 11:33:06 +0200 | [diff] [blame] | 47 | #define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0 | 
|  | 48 | #define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED   1 | 
|  | 49 |  | 
|  | 50 | #define MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL   0 | 
|  | 51 | #define MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION 1 | 
|  | 52 |  | 
|  | 53 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) | 
|  | 54 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string)       \ | 
|  | 56 | const unsigned char name    [sizeof(string) - 1]; | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 57 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | union mbedtls_ssl_tls13_labels_union { | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 59 | MBEDTLS_SSL_TLS1_3_LABEL_LIST | 
|  | 60 | }; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | struct mbedtls_ssl_tls13_labels_struct { | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 62 | MBEDTLS_SSL_TLS1_3_LABEL_LIST | 
|  | 63 | }; | 
| Hanno Becker | a3a5a4e | 2020-09-08 11:33:48 +0100 | [diff] [blame] | 64 | #undef MBEDTLS_SSL_TLS1_3_LABEL | 
| Hanno Becker | e4435ea | 2020-09-08 10:43:52 +0100 | [diff] [blame] | 65 |  | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 66 | extern const struct mbedtls_ssl_tls13_labels_struct mbedtls_ssl_tls13_labels; | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 67 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | #define MBEDTLS_SSL_TLS1_3_LBL_LEN(LABEL)  \ | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 69 | sizeof(mbedtls_ssl_tls13_labels.LABEL) | 
| Jerry Yu | 0bbb397 | 2021-09-19 20:27:17 +0800 | [diff] [blame] | 70 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | #define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(LABEL)  \ | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 72 | mbedtls_ssl_tls13_labels.LABEL,              \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | MBEDTLS_SSL_TLS1_3_LBL_LEN(LABEL) | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 74 |  | 
|  | 75 | #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN  \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | sizeof(union mbedtls_ssl_tls13_labels_union) | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 77 |  | 
| Hanno Becker | 61baae7 | 2020-09-16 09:24:14 +0100 | [diff] [blame] | 78 | /* 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] | 79 | * Since contexts are always hashes of message transcripts, this can | 
|  | 80 | * be approximated from above by the maximum hash size. */ | 
|  | 81 | #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN  \ | 
| Przemyslaw Stekiel | ab9b9d4 | 2022-09-14 13:51:07 +0200 | [diff] [blame] | 82 | PSA_HASH_MAX_SIZE | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 83 |  | 
|  | 84 | /* Maximum desired length for expanded key material generated | 
| Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 85 | * by HKDF-Expand-Label. | 
|  | 86 | * | 
|  | 87 | * Warning: If this ever needs to be increased, the implementation | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 88 | * ssl_tls13_hkdf_encode_label() in ssl_tls13_keys.c needs to be | 
| Hanno Becker | 531fe30 | 2020-09-16 09:45:27 +0100 | [diff] [blame] | 89 | * adjusted since it currently assumes that HKDF key expansion | 
|  | 90 | * is never used with more than 255 Bytes of output. */ | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 91 | #define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255 | 
|  | 92 |  | 
|  | 93 | /** | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 94 | * \brief            The \c HKDF-Expand-Label function from | 
|  | 95 | *                   the TLS 1.3 standard RFC 8446. | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 96 | * | 
|  | 97 | * <tt> | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 98 | *                   HKDF-Expand-Label( Secret, Label, Context, Length ) = | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 99 | *                       HKDF-Expand( Secret, HkdfLabel, Length ) | 
|  | 100 | * </tt> | 
|  | 101 | * | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 102 | * \param hash_alg   The identifier for the hash algorithm to use. | 
|  | 103 | * \param secret     The \c Secret argument to \c HKDF-Expand-Label. | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 104 | *                   This must be a readable buffer of length | 
|  | 105 | *                   \p secret_len Bytes. | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 106 | * \param secret_len The length of \p secret in Bytes. | 
|  | 107 | * \param label      The \c Label argument to \c HKDF-Expand-Label. | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 108 | *                   This must be a readable buffer of length | 
|  | 109 | *                   \p label_len Bytes. | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 110 | * \param label_len  The length of \p label in Bytes. | 
|  | 111 | * \param ctx        The \c Context argument to \c HKDF-Expand-Label. | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 112 | *                   This must be a readable buffer of length \p ctx_len Bytes. | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 113 | * \param ctx_len    The length of \p context in Bytes. | 
|  | 114 | * \param buf        The destination buffer to hold the expanded secret. | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 115 | *                   This must be a writable buffer of length \p buf_len Bytes. | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 116 | * \param buf_len    The desired size of the expanded secret in Bytes. | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 117 | * | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 118 | * \returns          \c 0 on success. | 
|  | 119 | * \return           A negative error code on failure. | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 120 | */ | 
|  | 121 |  | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 122 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 123 | int mbedtls_ssl_tls13_hkdf_expand_label( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | psa_algorithm_t hash_alg, | 
|  | 125 | const unsigned char *secret, size_t secret_len, | 
|  | 126 | const unsigned char *label, size_t label_len, | 
|  | 127 | const unsigned char *ctx, size_t ctx_len, | 
|  | 128 | unsigned char *buf, size_t buf_len); | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 129 |  | 
| Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 130 | /** | 
|  | 131 | * \brief           This function is part of the TLS 1.3 key schedule. | 
|  | 132 | *                  It extracts key and IV for the actual client/server traffic | 
|  | 133 | *                  from the client/server traffic secrets. | 
|  | 134 | * | 
|  | 135 | * From RFC 8446: | 
|  | 136 | * | 
|  | 137 | * <tt> | 
|  | 138 | *   [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length) | 
|  | 139 | *   [sender]_write_iv  = HKDF-Expand-Label(Secret, "iv", "", iv_length)* | 
|  | 140 | * </tt> | 
|  | 141 | * | 
|  | 142 | * \param hash_alg      The identifier for the hash algorithm to be used | 
|  | 143 | *                      for the HKDF-based expansion of the secret. | 
|  | 144 | * \param client_secret The client traffic secret. | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 145 | *                      This must be a readable buffer of size | 
|  | 146 | *                      \p secret_len Bytes | 
| Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 147 | * \param server_secret The server traffic secret. | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 148 | *                      This must be a readable buffer of size | 
|  | 149 | *                      \p secret_len Bytes | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 150 | * \param secret_len    Length of the secrets \p client_secret and | 
| Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 151 | *                      \p server_secret in Bytes. | 
| Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 152 | * \param key_len       The desired length of the key to be extracted in Bytes. | 
|  | 153 | * \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] | 154 | * \param keys          The address of the structure holding the generated | 
|  | 155 | *                      keys and IVs. | 
|  | 156 | * | 
|  | 157 | * \returns             \c 0 on success. | 
|  | 158 | * \returns             A negative error code on failure. | 
|  | 159 | */ | 
|  | 160 |  | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 161 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 162 | int mbedtls_ssl_tls13_make_traffic_keys( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | psa_algorithm_t hash_alg, | 
|  | 164 | const unsigned char *client_secret, | 
|  | 165 | const unsigned char *server_secret, size_t secret_len, | 
|  | 166 | size_t key_len, size_t iv_len, | 
|  | 167 | mbedtls_ssl_key_set *keys); | 
| Hanno Becker | 3385a4d | 2020-08-21 13:03:34 +0100 | [diff] [blame] | 168 |  | 
| Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 169 | /** | 
|  | 170 | * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446. | 
|  | 171 | * | 
|  | 172 | * <tt> | 
|  | 173 | *   Derive-Secret( Secret, Label, Messages ) = | 
|  | 174 | *      HKDF-Expand-Label( Secret, Label, | 
|  | 175 | *                         Hash( Messages ), | 
|  | 176 | *                         Hash.Length ) ) | 
|  | 177 | * </tt> | 
|  | 178 | * | 
| Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 179 | * \param hash_alg   The identifier for the hash function used for the | 
|  | 180 | *                   applications of HKDF. | 
|  | 181 | * \param secret     The \c Secret argument to the \c Derive-Secret function. | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 182 | *                   This must be a readable buffer of length | 
|  | 183 | *                   \p secret_len Bytes. | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 184 | * \param secret_len The length of \p secret in Bytes. | 
| Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 185 | * \param label      The \c Label argument to the \c Derive-Secret function. | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 186 | *                   This must be a readable buffer of length | 
|  | 187 | *                   \p label_len Bytes. | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 188 | * \param label_len  The length of \p label in Bytes. | 
| Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 189 | * \param ctx        The hash of the \c Messages argument to the | 
|  | 190 | *                   \c Derive-Secret function, or the \c Messages argument | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 191 | *                   itself, depending on \p ctx_hashed. | 
|  | 192 | * \param ctx_len    The length of \p ctx in Bytes. | 
| Hanno Becker | 0c42fd9 | 2020-09-09 12:58:29 +0100 | [diff] [blame] | 193 | * \param ctx_hashed This indicates whether the \p ctx contains the hash of | 
|  | 194 | *                   the \c Messages argument in the application of the | 
|  | 195 | *                   \c Derive-Secret function | 
|  | 196 | *                   (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether | 
|  | 197 | *                   it is the content of \c Messages itself, in which case | 
|  | 198 | *                   the function takes care of the hashing | 
|  | 199 | *                   (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED). | 
|  | 200 | * \param dstbuf     The target buffer to write the output of | 
|  | 201 | *                   \c Derive-Secret to. This must be a writable buffer of | 
| Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 202 | *                   size \p dtsbuf_len Bytes. | 
| Xiaofei Bai | 89b526d | 2021-11-23 06:31:16 +0000 | [diff] [blame] | 203 | * \param dstbuf_len The length of \p dstbuf in Bytes. | 
| Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 204 | * | 
|  | 205 | * \returns        \c 0 on success. | 
|  | 206 | * \returns        A negative error code on failure. | 
|  | 207 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 208 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 209 | int mbedtls_ssl_tls13_derive_secret( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | psa_algorithm_t hash_alg, | 
|  | 211 | const unsigned char *secret, size_t secret_len, | 
|  | 212 | const unsigned char *label, size_t label_len, | 
|  | 213 | const unsigned char *ctx, size_t ctx_len, | 
|  | 214 | int ctx_hashed, | 
|  | 215 | unsigned char *dstbuf, size_t dstbuf_len); | 
| Hanno Becker | b35d522 | 2020-08-21 13:27:44 +0100 | [diff] [blame] | 216 |  | 
| Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 217 | /** | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 218 | * \brief Derive TLS 1.3 early data key material from early secret. | 
|  | 219 | * | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 220 | *        This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret() | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 221 | *        with the appropriate labels. | 
|  | 222 | * | 
|  | 223 | * <tt> | 
|  | 224 | *        Early Secret | 
|  | 225 | *             | | 
|  | 226 | *             +-----> Derive-Secret(., "c e traffic", ClientHello) | 
|  | 227 | *             |                      = client_early_traffic_secret | 
|  | 228 | *             | | 
|  | 229 | *             +-----> Derive-Secret(., "e exp master", ClientHello) | 
|  | 230 | *             .                      = early_exporter_master_secret | 
|  | 231 | *             . | 
|  | 232 | *             . | 
|  | 233 | * </tt> | 
|  | 234 | * | 
|  | 235 | * \note  To obtain the actual key and IV for the early data traffic, | 
|  | 236 | *        the client secret derived by this function need to be | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 237 | *        further processed by mbedtls_ssl_tls13_make_traffic_keys(). | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 238 | * | 
|  | 239 | * \note  The binder key, which is also generated from the early secret, | 
|  | 240 | *        is omitted here. Its calculation is part of the separate routine | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 241 | *        mbedtls_ssl_tls13_create_psk_binder(). | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 242 | * | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 243 | * \param hash_alg     The hash algorithm associated with the PSK for which | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 244 | *                     early data key material is being derived. | 
|  | 245 | * \param early_secret The early secret from which the early data key material | 
|  | 246 | *                     should be derived. This must be a readable buffer whose | 
|  | 247 | *                     length is the digest size of the hash algorithm | 
|  | 248 | *                     represented by \p md_size. | 
|  | 249 | * \param transcript   The transcript of the handshake so far, calculated with | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 250 | *                     respect to \p hash_alg. This must be a readable buffer | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 251 | *                     whose length is the digest size of the hash algorithm | 
|  | 252 | *                     represented by \p md_size. | 
|  | 253 | * \param derived      The address of the structure in which to store | 
|  | 254 | *                     the early data key material. | 
|  | 255 | * | 
|  | 256 | * \returns        \c 0 on success. | 
|  | 257 | * \returns        A negative error code on failure. | 
|  | 258 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 259 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 260 | int mbedtls_ssl_tls13_derive_early_secrets( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | psa_algorithm_t hash_alg, | 
|  | 262 | unsigned char const *early_secret, | 
|  | 263 | unsigned char const *transcript, size_t transcript_len, | 
|  | 264 | mbedtls_ssl_tls13_early_secrets *derived); | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 265 |  | 
|  | 266 | /** | 
|  | 267 | * \brief Derive TLS 1.3 handshake key material from the handshake secret. | 
|  | 268 | * | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 269 | *        This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret() | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 270 | *        with the appropriate labels from the standard. | 
|  | 271 | * | 
|  | 272 | * <tt> | 
|  | 273 | *        Handshake Secret | 
|  | 274 | *              | | 
|  | 275 | *              +-----> Derive-Secret( ., "c hs traffic", | 
|  | 276 | *              |                      ClientHello...ServerHello ) | 
|  | 277 | *              |                      = client_handshake_traffic_secret | 
|  | 278 | *              | | 
|  | 279 | *              +-----> Derive-Secret( ., "s hs traffic", | 
|  | 280 | *              .                      ClientHello...ServerHello ) | 
|  | 281 | *              .                      = server_handshake_traffic_secret | 
|  | 282 | *              . | 
|  | 283 | * </tt> | 
|  | 284 | * | 
|  | 285 | * \note  To obtain the actual key and IV for the encrypted handshake traffic, | 
|  | 286 | *        the client and server secret derived by this function need to be | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 287 | *        further processed by mbedtls_ssl_tls13_make_traffic_keys(). | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 288 | * | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 289 | * \param hash_alg          The hash algorithm associated with the ciphersuite | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 290 | *                          that's being used for the connection. | 
|  | 291 | * \param handshake_secret  The handshake secret from which the handshake key | 
|  | 292 | *                          material should be derived. This must be a readable | 
|  | 293 | *                          buffer whose length is the digest size of the hash | 
|  | 294 | *                          algorithm represented by \p md_size. | 
|  | 295 | * \param transcript        The transcript of the handshake so far, calculated | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 296 | *                          with respect to \p hash_alg. This must be a readable | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 297 | *                          buffer whose length is the digest size of the hash | 
|  | 298 | *                          algorithm represented by \p md_size. | 
|  | 299 | * \param derived           The address of the structure in which to | 
|  | 300 | *                          store the handshake key material. | 
|  | 301 | * | 
|  | 302 | * \returns        \c 0 on success. | 
|  | 303 | * \returns        A negative error code on failure. | 
|  | 304 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 305 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 306 | int mbedtls_ssl_tls13_derive_handshake_secrets( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | psa_algorithm_t hash_alg, | 
|  | 308 | unsigned char const *handshake_secret, | 
|  | 309 | unsigned char const *transcript, size_t transcript_len, | 
|  | 310 | mbedtls_ssl_tls13_handshake_secrets *derived); | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 311 |  | 
|  | 312 | /** | 
|  | 313 | * \brief Derive TLS 1.3 application key material from the master secret. | 
|  | 314 | * | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 315 | *        This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret() | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 316 | *        with the appropriate labels from the standard. | 
|  | 317 | * | 
|  | 318 | * <tt> | 
|  | 319 | *        Master Secret | 
|  | 320 | *              | | 
|  | 321 | *              +-----> Derive-Secret( ., "c ap traffic", | 
|  | 322 | *              |                      ClientHello...server Finished ) | 
|  | 323 | *              |                      = client_application_traffic_secret_0 | 
|  | 324 | *              | | 
|  | 325 | *              +-----> Derive-Secret( ., "s ap traffic", | 
|  | 326 | *              |                      ClientHello...Server Finished ) | 
|  | 327 | *              |                      = server_application_traffic_secret_0 | 
|  | 328 | *              | | 
|  | 329 | *              +-----> Derive-Secret( ., "exp master", | 
|  | 330 | *              .                      ClientHello...server Finished) | 
|  | 331 | *              .                      = exporter_master_secret | 
|  | 332 | *              . | 
|  | 333 | * </tt> | 
|  | 334 | * | 
|  | 335 | * \note  To obtain the actual key and IV for the (0-th) application traffic, | 
|  | 336 | *        the client and server secret derived by this function need to be | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 337 | *        further processed by mbedtls_ssl_tls13_make_traffic_keys(). | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 338 | * | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 339 | * \param hash_alg          The hash algorithm associated with the ciphersuite | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 340 | *                          that's being used for the connection. | 
|  | 341 | * \param master_secret     The master secret from which the application key | 
|  | 342 | *                          material should be derived. This must be a readable | 
|  | 343 | *                          buffer whose length is the digest size of the hash | 
|  | 344 | *                          algorithm represented by \p md_size. | 
|  | 345 | * \param transcript        The transcript of the handshake up to and including | 
|  | 346 | *                          the ServerFinished message, calculated with respect | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 347 | *                          to \p hash_alg. This must be a readable buffer whose | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 348 | *                          length is the digest size of the hash algorithm | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 349 | *                          represented by \p hash_alg. | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 350 | * \param derived           The address of the structure in which to | 
|  | 351 | *                          store the application key material. | 
|  | 352 | * | 
|  | 353 | * \returns        \c 0 on success. | 
|  | 354 | * \returns        A negative error code on failure. | 
|  | 355 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 356 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 357 | int mbedtls_ssl_tls13_derive_application_secrets( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | psa_algorithm_t hash_alg, | 
|  | 359 | unsigned char const *master_secret, | 
|  | 360 | unsigned char const *transcript, size_t transcript_len, | 
|  | 361 | mbedtls_ssl_tls13_application_secrets *derived); | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 362 |  | 
|  | 363 | /** | 
|  | 364 | * \brief Derive TLS 1.3 resumption master secret from the master secret. | 
|  | 365 | * | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 366 | *        This is a small wrapper invoking mbedtls_ssl_tls13_derive_secret() | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 367 | *        with the appropriate labels from the standard. | 
|  | 368 | * | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 369 | * \param hash_alg          The hash algorithm used in the application for which | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 370 | *                          key material is being derived. | 
|  | 371 | * \param application_secret The application secret from which the resumption master | 
|  | 372 | *                          secret should be derived. This must be a readable | 
|  | 373 | *                          buffer whose length is the digest size of the hash | 
|  | 374 | *                          algorithm represented by \p md_size. | 
|  | 375 | * \param transcript        The transcript of the handshake up to and including | 
|  | 376 | *                          the ClientFinished message, calculated with respect | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 377 | *                          to \p hash_alg. This must be a readable buffer whose | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 378 | *                          length is the digest size of the hash algorithm | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 379 | *                          represented by \p hash_alg. | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 380 | * \param transcript_len    The length of \p transcript in Bytes. | 
|  | 381 | * \param derived           The address of the structure in which to | 
|  | 382 | *                          store the resumption master secret. | 
|  | 383 | * | 
|  | 384 | * \returns        \c 0 on success. | 
|  | 385 | * \returns        A negative error code on failure. | 
|  | 386 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 387 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 388 | int mbedtls_ssl_tls13_derive_resumption_master_secret( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | psa_algorithm_t hash_alg, | 
|  | 390 | unsigned char const *application_secret, | 
|  | 391 | unsigned char const *transcript, size_t transcript_len, | 
|  | 392 | mbedtls_ssl_tls13_application_secrets *derived); | 
| Hanno Becker | ef5235b | 2021-05-24 06:39:41 +0100 | [diff] [blame] | 393 |  | 
|  | 394 | /** | 
| Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 395 | * \brief Compute the next secret in the TLS 1.3 key schedule | 
|  | 396 | * | 
|  | 397 | * The TLS 1.3 key schedule proceeds as follows to compute | 
|  | 398 | * the three main secrets during the handshake: The early | 
|  | 399 | * secret for early data, the handshake secret for all | 
|  | 400 | * other encrypted handshake messages, and the master | 
|  | 401 | * secret for all application traffic. | 
|  | 402 | * | 
|  | 403 | * <tt> | 
|  | 404 | *                    0 | 
|  | 405 | *                    | | 
|  | 406 | *                    v | 
|  | 407 | *     PSK ->  HKDF-Extract = Early Secret | 
|  | 408 | *                    | | 
|  | 409 | *                    v | 
|  | 410 | *     Derive-Secret( ., "derived", "" ) | 
|  | 411 | *                    | | 
|  | 412 | *                    v | 
|  | 413 | *  (EC)DHE -> HKDF-Extract = Handshake Secret | 
|  | 414 | *                    | | 
|  | 415 | *                    v | 
|  | 416 | *     Derive-Secret( ., "derived", "" ) | 
|  | 417 | *                    | | 
|  | 418 | *                    v | 
|  | 419 | *     0 -> HKDF-Extract = Master Secret | 
|  | 420 | * </tt> | 
|  | 421 | * | 
|  | 422 | * Each of the three secrets in turn is the basis for further | 
|  | 423 | * key derivations, such as the derivation of traffic keys and IVs; | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 424 | * see e.g. mbedtls_ssl_tls13_make_traffic_keys(). | 
| Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 425 | * | 
|  | 426 | * This function implements one step in this evolution of secrets: | 
|  | 427 | * | 
|  | 428 | * <tt> | 
|  | 429 | *                old_secret | 
|  | 430 | *                    | | 
|  | 431 | *                    v | 
|  | 432 | *     Derive-Secret( ., "derived", "" ) | 
|  | 433 | *                    | | 
|  | 434 | *                    v | 
|  | 435 | *     input -> HKDF-Extract = new_secret | 
|  | 436 | * </tt> | 
|  | 437 | * | 
|  | 438 | * \param hash_alg    The identifier for the hash function used for the | 
|  | 439 | *                    applications of HKDF. | 
|  | 440 | * \param secret_old  The address of the buffer holding the old secret | 
|  | 441 | *                    on function entry. If not \c NULL, this must be a | 
|  | 442 | *                    readable buffer whose size matches the output size | 
|  | 443 | *                    of the hash function represented by \p hash_alg. | 
|  | 444 | *                    If \c NULL, an all \c 0 array will be used instead. | 
|  | 445 | * \param input       The address of the buffer holding the additional | 
|  | 446 | *                    input for the key derivation (e.g., the PSK or the | 
|  | 447 | *                    ephemeral (EC)DH secret). If not \c NULL, this must be | 
|  | 448 | *                    a readable buffer whose size \p input_len Bytes. | 
|  | 449 | *                    If \c NULL, an all \c 0 array will be used instead. | 
|  | 450 | * \param input_len   The length of \p input in Bytes. | 
|  | 451 | * \param secret_new  The address of the buffer holding the new secret | 
|  | 452 | *                    on function exit. This must be a writable buffer | 
|  | 453 | *                    whose size matches the output size of the hash | 
|  | 454 | *                    function represented by \p hash_alg. | 
|  | 455 | *                    This may be the same as \p secret_old. | 
|  | 456 | * | 
|  | 457 | * \returns           \c 0 on success. | 
|  | 458 | * \returns           A negative error code on failure. | 
|  | 459 | */ | 
|  | 460 |  | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 461 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 462 | int mbedtls_ssl_tls13_evolve_secret( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | psa_algorithm_t hash_alg, | 
|  | 464 | const unsigned char *secret_old, | 
|  | 465 | const unsigned char *input, size_t input_len, | 
|  | 466 | unsigned char *secret_new); | 
| Hanno Becker | e9cccb4 | 2020-08-20 13:42:46 +0100 | [diff] [blame] | 467 |  | 
| Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 468 | /** | 
|  | 469 | * \brief             Calculate a TLS 1.3 PSK binder. | 
|  | 470 | * | 
|  | 471 | * \param ssl         The SSL context. This is used for debugging only and may | 
|  | 472 | *                    be \c NULL if MBEDTLS_DEBUG_C is disabled. | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 473 | * \param hash_alg    The hash algorithm associated to the PSK \p psk. | 
| Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 474 | * \param psk         The buffer holding the PSK for which to create a binder. | 
|  | 475 | * \param psk_len     The size of \p psk in bytes. | 
| Hanno Becker | c8d3ccd | 2021-05-26 04:47:29 +0100 | [diff] [blame] | 476 | * \param psk_type    This indicates whether the PSK \p psk is externally | 
| Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 477 | *                    provisioned (#MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL) or a | 
|  | 478 | *                    resumption PSK (#MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION). | 
|  | 479 | * \param transcript  The handshake transcript up to the point where the | 
|  | 480 | *                    PSK binder calculation happens. This must be readable, | 
|  | 481 | *                    and its size must be equal to the digest size of | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 482 | *                    the hash algorithm represented by \p hash_alg. | 
| Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 483 | * \param result      The address at which to store the PSK binder on success. | 
|  | 484 | *                    This must be writable, and its size must be equal to the | 
|  | 485 | *                    digest size of  the hash algorithm represented by | 
| Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 486 | *                    \p hash_alg. | 
| Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 487 | * | 
|  | 488 | * \returns           \c 0 on success. | 
|  | 489 | * \returns           A negative error code on failure. | 
|  | 490 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 491 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | int mbedtls_ssl_tls13_create_psk_binder(mbedtls_ssl_context *ssl, | 
|  | 493 | const psa_algorithm_t hash_alg, | 
|  | 494 | unsigned char const *psk, size_t psk_len, | 
|  | 495 | int psk_type, | 
|  | 496 | unsigned char const *transcript, | 
|  | 497 | unsigned char *result); | 
| Hanno Becker | b7d9bad | 2021-05-24 06:44:14 +0100 | [diff] [blame] | 498 |  | 
| Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 499 | /** | 
|  | 500 | * \bref Setup an SSL transform structure representing the | 
|  | 501 | *       record protection mechanism used by TLS 1.3 | 
|  | 502 | * | 
|  | 503 | * \param transform    The SSL transform structure to be created. This must have | 
|  | 504 | *                     been initialized through mbedtls_ssl_transform_init() and | 
|  | 505 | *                     not used in any other way prior to calling this function. | 
|  | 506 | *                     In particular, this function does not clean up the | 
|  | 507 | *                     transform structure prior to installing the new keys. | 
|  | 508 | * \param endpoint     Indicates whether the transform is for the client | 
|  | 509 | *                     (value #MBEDTLS_SSL_IS_CLIENT) or the server | 
|  | 510 | *                     (value #MBEDTLS_SSL_IS_SERVER). | 
|  | 511 | * \param ciphersuite  The numerical identifier for the ciphersuite to use. | 
|  | 512 | *                     This must be one of the identifiers listed in | 
|  | 513 | *                     ssl_ciphersuites.h. | 
|  | 514 | * \param traffic_keys The key material to use. No reference is stored in | 
|  | 515 | *                     the SSL transform being generated, and the caller | 
|  | 516 | *                     should destroy the key material afterwards. | 
|  | 517 | * \param ssl          (Debug-only) The SSL context to use for debug output | 
|  | 518 | *                     in case of failure. This parameter is only needed if | 
|  | 519 | *                     #MBEDTLS_DEBUG_C is set, and is ignored otherwise. | 
|  | 520 | * | 
|  | 521 | * \return             \c 0 on success. In this case, \p transform is ready to | 
|  | 522 | *                     be used with mbedtls_ssl_transform_decrypt() and | 
|  | 523 | *                     mbedtls_ssl_transform_encrypt(). | 
|  | 524 | * \return             A negative error code on failure. | 
|  | 525 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 526 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | int mbedtls_ssl_tls13_populate_transform(mbedtls_ssl_transform *transform, | 
|  | 528 | int endpoint, | 
|  | 529 | int ciphersuite, | 
|  | 530 | mbedtls_ssl_key_set const *traffic_keys, | 
|  | 531 | mbedtls_ssl_context *ssl); | 
| Hanno Becker | c94060c | 2021-03-22 07:50:44 +0000 | [diff] [blame] | 532 |  | 
| Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 533 | /* | 
|  | 534 | * TLS 1.3 key schedule evolutions | 
|  | 535 | * | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 536 | *   Early -> Handshake -> Application | 
| Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 537 | * | 
| Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 538 | * Small wrappers around mbedtls_ssl_tls13_evolve_secret(). | 
| Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 539 | */ | 
|  | 540 |  | 
|  | 541 | /** | 
| Jerry Yu | c1ddeef | 2021-10-08 15:14:45 +0800 | [diff] [blame] | 542 | * \brief Begin TLS 1.3 key schedule by calculating early secret. | 
| Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 543 | * | 
|  | 544 | *        The TLS 1.3 key schedule can be viewed as a simple state machine | 
|  | 545 | *        with states Initial -> Early -> Handshake -> Application, and | 
|  | 546 | *        this function represents the Initial -> Early transition. | 
|  | 547 | * | 
| Jerry Yu | 89ea321 | 2021-09-09 14:31:24 +0800 | [diff] [blame] | 548 | * \param ssl  The SSL context to operate on. | 
|  | 549 | * | 
|  | 550 | * \returns    \c 0 on success. | 
|  | 551 | * \returns    A negative error code on failure. | 
|  | 552 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 553 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl); | 
| Jerry Yu | 4925ef5 | 2021-09-09 14:42:55 +0800 | [diff] [blame] | 555 |  | 
| Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 556 | /** | 
| Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 557 | * \brief Transition into handshake stage of TLS 1.3 key schedule. | 
|  | 558 | * | 
|  | 559 | *        The TLS 1.3 key schedule can be viewed as a simple state machine | 
|  | 560 | *        with states Initial -> Early -> Handshake -> Application, and | 
|  | 561 | *        this function represents the Early -> Handshake transition. | 
|  | 562 | * | 
| Jerry Yu | c068b66 | 2021-10-11 22:30:19 +0800 | [diff] [blame] | 563 | *        In the handshake stage, mbedtls_ssl_tls13_generate_handshake_keys() | 
| Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 564 | *        can be used to derive the handshake traffic keys. | 
|  | 565 | * | 
|  | 566 | * \param ssl  The SSL context to operate on. This must be in key schedule | 
|  | 567 | *             stage \c Early. | 
|  | 568 | * | 
|  | 569 | * \returns    \c 0 on success. | 
|  | 570 | * \returns    A negative error code on failure. | 
|  | 571 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 572 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | int mbedtls_ssl_tls13_key_schedule_stage_handshake(mbedtls_ssl_context *ssl); | 
| Jerry Yu | a0650eb | 2021-09-09 17:14:45 +0800 | [diff] [blame] | 574 |  | 
|  | 575 | /** | 
| Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 576 | * \brief Compute TLS 1.3 handshake traffic keys. | 
|  | 577 | * | 
|  | 578 | * \param ssl  The SSL context to operate on. This must be in | 
|  | 579 | *             key schedule stage \c Handshake, see | 
| Jerry Yu | f0ac235 | 2021-10-11 17:47:07 +0800 | [diff] [blame] | 580 | *             mbedtls_ssl_tls13_key_schedule_stage_handshake(). | 
| Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 581 | * \param traffic_keys The address at which to store the handshake traffic key | 
|  | 582 | *                     keys. This must be writable but may be uninitialized. | 
|  | 583 | * | 
|  | 584 | * \returns    \c 0 on success. | 
|  | 585 | * \returns    A negative error code on failure. | 
|  | 586 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 587 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | int mbedtls_ssl_tls13_generate_handshake_keys(mbedtls_ssl_context *ssl, | 
|  | 589 | mbedtls_ssl_key_set *traffic_keys); | 
| Jerry Yu | 61e35e0 | 2021-09-16 18:59:08 +0800 | [diff] [blame] | 590 |  | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 591 | /** | 
|  | 592 | * \brief Transition into application stage of TLS 1.3 key schedule. | 
|  | 593 | * | 
|  | 594 | *        The TLS 1.3 key schedule can be viewed as a simple state machine | 
|  | 595 | *        with states Initial -> Early -> Handshake -> Application, and | 
|  | 596 | *        this function represents the Handshake -> Application transition. | 
|  | 597 | * | 
| XiaokangQian | d0aa3e9 | 2021-11-10 06:17:40 +0000 | [diff] [blame] | 598 | *        In the handshake stage, mbedtls_ssl_tls13_generate_application_keys() | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 599 | *        can be used to derive the handshake traffic keys. | 
|  | 600 | * | 
|  | 601 | * \param ssl  The SSL context to operate on. This must be in key schedule | 
|  | 602 | *             stage \c Handshake. | 
|  | 603 | * | 
|  | 604 | * \returns    \c 0 on success. | 
|  | 605 | * \returns    A negative error code on failure. | 
|  | 606 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 607 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | int mbedtls_ssl_tls13_key_schedule_stage_application(mbedtls_ssl_context *ssl); | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 609 |  | 
|  | 610 | /** | 
|  | 611 | * \brief Compute TLS 1.3 application traffic keys. | 
|  | 612 | * | 
|  | 613 | * \param ssl  The SSL context to operate on. This must be in | 
|  | 614 | *             key schedule stage \c Application, see | 
| XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 615 | *             mbedtls_ssl_tls13_key_schedule_stage_application(). | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 616 | * \param traffic_keys The address at which to store the application traffic key | 
|  | 617 | *                     keys. This must be writable but may be uninitialized. | 
|  | 618 | * | 
|  | 619 | * \returns    \c 0 on success. | 
|  | 620 | * \returns    A negative error code on failure. | 
|  | 621 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 622 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| XiaokangQian | d0aa3e9 | 2021-11-10 06:17:40 +0000 | [diff] [blame] | 623 | int mbedtls_ssl_tls13_generate_application_keys( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | mbedtls_ssl_context *ssl, mbedtls_ssl_key_set *traffic_keys); | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 625 |  | 
|  | 626 | /** | 
| Jerry Yu | ff22698 | 2022-04-16 16:52:57 +0800 | [diff] [blame] | 627 | * \brief Compute TLS 1.3 resumption master secret. | 
|  | 628 | * | 
|  | 629 | * \param ssl  The SSL context to operate on. This must be in | 
|  | 630 | *             key schedule stage \c Application, see | 
|  | 631 | *             mbedtls_ssl_tls13_key_schedule_stage_application(). | 
|  | 632 | * | 
|  | 633 | * \returns    \c 0 on success. | 
|  | 634 | * \returns    A negative error code on failure. | 
|  | 635 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 636 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | int mbedtls_ssl_tls13_compute_resumption_master_secret(mbedtls_ssl_context *ssl); | 
| Jerry Yu | ff22698 | 2022-04-16 16:52:57 +0800 | [diff] [blame] | 638 |  | 
|  | 639 | /** | 
| XiaokangQian | c5c39d5 | 2021-11-09 11:55:10 +0000 | [diff] [blame] | 640 | * \brief Calculate the verify_data value for the client or server TLS 1.3 | 
|  | 641 | * Finished message. | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 642 | * | 
|  | 643 | * \param ssl  The SSL context to operate on. This must be in | 
|  | 644 | *             key schedule stage \c Handshake, see | 
| XiaokangQian | 4cab024 | 2021-10-12 08:43:37 +0000 | [diff] [blame] | 645 | *             mbedtls_ssl_tls13_key_schedule_stage_application(). | 
| XiaokangQian | c5c39d5 | 2021-11-09 11:55:10 +0000 | [diff] [blame] | 646 | * \param dst        The address at which to write the verify_data value. | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 647 | * \param dst_len    The size of \p dst in bytes. | 
|  | 648 | * \param actual_len The address at which to store the amount of data | 
|  | 649 | *                   actually written to \p dst upon success. | 
| XiaokangQian | aaa0e19 | 2021-11-10 03:07:04 +0000 | [diff] [blame] | 650 | * \param which      The message to calculate the `verify_data` for: | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 651 | *                   - #MBEDTLS_SSL_IS_CLIENT for the Client's Finished message | 
|  | 652 | *                   - #MBEDTLS_SSL_IS_SERVER for the Server's Finished message | 
|  | 653 | * | 
|  | 654 | * \note       Both client and server call this function twice, once to | 
|  | 655 | *             generate their own Finished message, and once to verify the | 
|  | 656 | *             peer's Finished message. | 
|  | 657 |  | 
|  | 658 | * \returns    \c 0 on success. | 
|  | 659 | * \returns    A negative error code on failure. | 
|  | 660 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 661 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 662 | int mbedtls_ssl_tls13_calculate_verify_data(mbedtls_ssl_context *ssl, | 
|  | 663 | unsigned char *dst, | 
|  | 664 | size_t dst_len, | 
|  | 665 | size_t *actual_len, | 
|  | 666 | int which); | 
| XiaokangQian | aa5f5c1 | 2021-09-18 06:20:25 +0000 | [diff] [blame] | 667 |  | 
| Jerry Yu | 91b560f | 2022-11-04 14:10:34 +0800 | [diff] [blame] | 668 | #if defined(MBEDTLS_SSL_EARLY_DATA) | 
|  | 669 | /** | 
|  | 670 | * \brief Compute TLS 1.3 early transform | 
|  | 671 | * | 
| Jerry Yu | b094e12 | 2022-11-21 13:03:47 +0800 | [diff] [blame] | 672 | * \param ssl  The SSL context to operate on. | 
| Jerry Yu | 91b560f | 2022-11-04 14:10:34 +0800 | [diff] [blame] | 673 | * | 
|  | 674 | * \returns    \c 0 on success. | 
|  | 675 | * \returns    A negative error code on failure. | 
|  | 676 | * | 
| Jerry Yu | 3ce61ff | 2022-11-21 22:45:58 +0800 | [diff] [blame] | 677 | * \warning    The function does not compute the early master secret. Call | 
|  | 678 | *             mbedtls_ssl_tls13_key_schedule_stage_early() before to | 
|  | 679 | *             call this function to generate the early master secret. | 
|  | 680 | * \note       For a client/server endpoint, the function computes only the | 
|  | 681 | *             encryption/decryption part of the transform as the decryption/ | 
|  | 682 | *             encryption part is not defined by the specification (no early | 
|  | 683 | *             traffic from the server to the client). | 
| Jerry Yu | 91b560f | 2022-11-04 14:10:34 +0800 | [diff] [blame] | 684 | */ | 
|  | 685 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | int mbedtls_ssl_tls13_compute_early_transform(mbedtls_ssl_context *ssl); | 
| Jerry Yu | 91b560f | 2022-11-04 14:10:34 +0800 | [diff] [blame] | 687 | #endif /* MBEDTLS_SSL_EARLY_DATA */ | 
|  | 688 |  | 
| Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 689 | /** | 
|  | 690 | * \brief Compute TLS 1.3 handshake transform | 
|  | 691 | * | 
| Jerry Yu | f86eb75 | 2022-05-06 11:16:55 +0800 | [diff] [blame] | 692 | * \param ssl  The SSL context to operate on. The early secret must have been | 
| Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 693 | *             computed. | 
|  | 694 | * | 
|  | 695 | * \returns    \c 0 on success. | 
|  | 696 | * \returns    A negative error code on failure. | 
|  | 697 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 698 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | int mbedtls_ssl_tls13_compute_handshake_transform(mbedtls_ssl_context *ssl); | 
| Jerry Yu | e110d25 | 2022-05-05 10:19:22 +0800 | [diff] [blame] | 700 |  | 
| Jerry Yu | fd5ea04 | 2022-05-19 14:29:48 +0800 | [diff] [blame] | 701 | /** | 
|  | 702 | * \brief Compute TLS 1.3 application transform | 
|  | 703 | * | 
|  | 704 | * \param ssl  The SSL context to operate on. The early secret must have been | 
|  | 705 | *             computed. | 
|  | 706 | * | 
|  | 707 | * \returns    \c 0 on success. | 
|  | 708 | * \returns    A negative error code on failure. | 
|  | 709 | */ | 
| Manuel Pégourié-Gonnard | a82a8b9 | 2022-06-17 10:53:58 +0200 | [diff] [blame] | 710 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | int mbedtls_ssl_tls13_compute_application_transform(mbedtls_ssl_context *ssl); | 
| Jerry Yu | fd5ea04 | 2022-05-19 14:29:48 +0800 | [diff] [blame] | 712 |  | 
| Ronald Cron | 41a443a | 2022-10-04 16:38:25 +0200 | [diff] [blame] | 713 | #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) | 
| Jerry Yu | 40f3771 | 2022-07-26 16:58:57 +0800 | [diff] [blame] | 714 | /** | 
| Jerry Yu | 6cf6b47 | 2022-08-16 14:50:28 +0800 | [diff] [blame] | 715 | * \brief Export TLS 1.3 PSK from handshake context | 
| Jerry Yu | 40f3771 | 2022-07-26 16:58:57 +0800 | [diff] [blame] | 716 | * | 
| Jerry Yu | 01e42d2 | 2022-08-21 12:55:51 +0800 | [diff] [blame] | 717 | * \param[in]   ssl  The SSL context to operate on. | 
|  | 718 | * \param[out]  psk  PSK output pointer. | 
|  | 719 | * \param[out]  psk_len Length of PSK. | 
| Jerry Yu | 40f3771 | 2022-07-26 16:58:57 +0800 | [diff] [blame] | 720 | * | 
| Jerry Yu | 29d9faa | 2022-08-23 17:52:45 +0800 | [diff] [blame] | 721 | * \returns     \c 0 if there is a configured PSK and it was exported | 
|  | 722 | *              successfully. | 
|  | 723 | * \returns     A negative error code on failure. | 
| Jerry Yu | 40f3771 | 2022-07-26 16:58:57 +0800 | [diff] [blame] | 724 | */ | 
|  | 725 | MBEDTLS_CHECK_RETURN_CRITICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 726 | int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl, | 
|  | 727 | unsigned char **psk, | 
|  | 728 | size_t *psk_len); | 
| Ronald Cron | 41a443a | 2022-10-04 16:38:25 +0200 | [diff] [blame] | 729 | #endif | 
| Jerry Yu | 40f3771 | 2022-07-26 16:58:57 +0800 | [diff] [blame] | 730 |  | 
| Gabor Mezei | e42d8bf | 2022-03-30 11:33:06 +0200 | [diff] [blame] | 731 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ | 
|  | 732 |  | 
| Hanno Becker | be9d664 | 2020-08-21 13:20:06 +0100 | [diff] [blame] | 733 | #endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */ |