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