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