blob: b75c5cea3798128b0c389e388f419be96e6fe813 [file] [log] [blame]
Hanno Beckerbe9d6642020-08-21 13:20:06 +01001/*
2 * TLS 1.3 key schedule
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 ( the "License" ); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19#if !defined(MBEDTLS_SSL_TLS1_3_KEYS_H)
20#define MBEDTLS_SSL_TLS1_3_KEYS_H
21
22#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Beckere4435ea2020-09-08 10:43:52 +010023
Hanno Becker70d7fb02020-09-09 10:11:21 +010024/* This requires MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) to be defined at
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010025 * the point of use. See e.g. the definition of mbedtls_ssl_tls1_3_labels_union
26 * below. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010027#define MBEDTLS_SSL_TLS1_3_LABEL_LIST \
Hanno Becker70d7fb02020-09-09 10:11:21 +010028 MBEDTLS_SSL_TLS1_3_LABEL( 0, finished , "finished" ) \
29 MBEDTLS_SSL_TLS1_3_LABEL( 1, resumption , "resumption" ) \
30 MBEDTLS_SSL_TLS1_3_LABEL( 2, traffic_upd , "traffic upd" ) \
31 MBEDTLS_SSL_TLS1_3_LABEL( 3, exporter , "exporter" ) \
32 MBEDTLS_SSL_TLS1_3_LABEL( 4, key , "key" ) \
33 MBEDTLS_SSL_TLS1_3_LABEL( 5, iv , "iv" ) \
34 MBEDTLS_SSL_TLS1_3_LABEL( 6, sn , "sn" ) \
35 MBEDTLS_SSL_TLS1_3_LABEL( 7, c_hs_traffic, "c hs traffic" ) \
36 MBEDTLS_SSL_TLS1_3_LABEL( 8, c_ap_traffic, "c ap traffic" ) \
37 MBEDTLS_SSL_TLS1_3_LABEL( 9, c_e_traffic , "c e traffic" ) \
38 MBEDTLS_SSL_TLS1_3_LABEL( 10, s_hs_traffic, "s hs traffic" ) \
39 MBEDTLS_SSL_TLS1_3_LABEL( 11, s_ap_traffic, "s ap traffic" ) \
40 MBEDTLS_SSL_TLS1_3_LABEL( 12, s_e_traffic , "s e traffic" ) \
Hanno Becker00cfc1c2020-09-09 10:41:23 +010041 MBEDTLS_SSL_TLS1_3_LABEL( 13, e_exp_master, "e exp master" ) \
Hanno Becker70d7fb02020-09-09 10:11:21 +010042 MBEDTLS_SSL_TLS1_3_LABEL( 14, res_master , "res master" ) \
Hanno Becker00cfc1c2020-09-09 10:41:23 +010043 MBEDTLS_SSL_TLS1_3_LABEL( 15, exp_master , "exp master" ) \
44 MBEDTLS_SSL_TLS1_3_LABEL( 16, ext_binder , "ext binder" ) \
45 MBEDTLS_SSL_TLS1_3_LABEL( 17, res_binder , "res binder" ) \
46 MBEDTLS_SSL_TLS1_3_LABEL( 18, derived , "derived" )
Hanno Beckere4435ea2020-09-08 10:43:52 +010047
Hanno Becker70d7fb02020-09-09 10:11:21 +010048#define MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010049 const unsigned char name [ sizeof(string) - 1 ];
Hanno Beckerbe9d6642020-08-21 13:20:06 +010050
51union mbedtls_ssl_tls1_3_labels_union
52{
53 MBEDTLS_SSL_TLS1_3_LABEL_LIST
54};
55struct mbedtls_ssl_tls1_3_labels_struct
56{
57 MBEDTLS_SSL_TLS1_3_LABEL_LIST
58};
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010059#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010060
Hanno Beckerbe9d6642020-08-21 13:20:06 +010061extern const struct mbedtls_ssl_tls1_3_labels_struct mbedtls_ssl_tls1_3_labels;
62
63#define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \
64 mbedtls_ssl_tls1_3_labels.LABEL, \
65 sizeof(mbedtls_ssl_tls1_3_labels.LABEL)
66
67#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
68 sizeof( union mbedtls_ssl_tls1_3_labels_union )
69
70/* The maximum length of HKDF contexts used in the TLS 1.3 standad.
71 * Since contexts are always hashes of message transcripts, this can
72 * be approximated from above by the maximum hash size. */
73#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
74 MBEDTLS_MD_MAX_SIZE
75
76/* Maximum desired length for expanded key material generated
77 * by HKDF-Expand-Label. */
78#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
79
80/**
81 * \brief The \c HKDF-Expand-Label function from
82 * the TLS 1.3 standard RFC 8446.
83 *
84 * <tt>
85 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
86 * HKDF-Expand( Secret, HkdfLabel, Length )
87 * </tt>
88 *
89 * \param hash_alg The identifier for the hash algorithm to use.
90 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
91 * This must be a readable buffer of length \p slen Bytes.
92 * \param slen The length of \p secret in Bytes.
93 * \param label The \c Label argument to \c HKDF-Expand-Label.
94 * This must be a readable buffer of length \p llen Bytes.
95 * \param llen The length of \p label in Bytes.
96 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
97 * This must be a readable buffer of length \p clen Bytes.
98 * \param clen The length of \p context in Bytes.
99 * \param buf The destination buffer to hold the expanded secret.
100 * This must be a writable buffe of length \p blen Bytes.
101 * \param blen The desired size of the expanded secret in Bytes.
102 *
103 * \returns \c 0 on success.
104 * \return A negative error code on failure.
105 */
106
107int mbedtls_ssl_tls1_3_hkdf_expand_label(
108 mbedtls_md_type_t hash_alg,
109 const unsigned char *secret, size_t slen,
110 const unsigned char *label, size_t llen,
111 const unsigned char *ctx, size_t clen,
112 unsigned char *buf, size_t blen );
113
Hanno Becker3385a4d2020-08-21 13:03:34 +0100114/**
115 * \brief This function is part of the TLS 1.3 key schedule.
116 * It extracts key and IV for the actual client/server traffic
117 * from the client/server traffic secrets.
118 *
119 * From RFC 8446:
120 *
121 * <tt>
122 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
123 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
124 * </tt>
125 *
126 * \param hash_alg The identifier for the hash algorithm to be used
127 * for the HKDF-based expansion of the secret.
128 * \param client_secret The client traffic secret.
129 * This must be a readable buffer of size \p slen Bytes
130 * \param server_secret The server traffic secret.
131 * This must be a readable buffer of size \p slen Bytes
132 * \param slen Length of the secrets \p client_secret and
133 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100134 * \param key_len The desired length of the key to be extracted in Bytes.
135 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100136 * \param keys The address of the structure holding the generated
137 * keys and IVs.
138 *
139 * \returns \c 0 on success.
140 * \returns A negative error code on failure.
141 */
142
143int mbedtls_ssl_tls1_3_make_traffic_keys(
144 mbedtls_md_type_t hash_alg,
145 const unsigned char *client_secret,
146 const unsigned char *server_secret,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100147 size_t slen, size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100148 mbedtls_ssl_key_set *keys );
149
Hanno Beckerb35d5222020-08-21 13:27:44 +0100150/**
151 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
152 *
153 * <tt>
154 * Derive-Secret( Secret, Label, Messages ) =
155 * HKDF-Expand-Label( Secret, Label,
156 * Hash( Messages ),
157 * Hash.Length ) )
158 * </tt>
159 *
160 * Note: In this implementation of the function we assume that
161 * the parameter message contains the already hashed value and
162 * the Derive-Secret function does not need to hash it again.
163 *
164 * \param hash_alg The identifier for the hash function used for the
165 * applications of HKDF.
166 * \param secret The \c Secret argument to the \c Derive-Secret function.
167 * This must be a readable buffer of length \p slen Bytes.
168 * \param slen The length of \p secret in Bytes.
169 * \param label The \c Label argument to the \c Derive-Secret function.
170 * This must be a readable buffer of length \p llen Bytes.
171 * \param llen The length of \p label in Bytes.
172 * \param hash The hash of the \c Messages argument to the \c Derive-Secret
173 * function. This must be a readable buffer of length \p mlen
174 * hlen Bytes.
175 * \param hlen The length of \p hash.
176 * \param dstbuf The target buffer to write the output of \c Derive-Secret to.
177 * This must be a writable buffer of size \p buflen Bytes.
178 * \param buflen The length of \p dstbuf in Bytes.
179 *
180 * \returns \c 0 on success.
181 * \returns A negative error code on failure.
182 */
183
184#define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
185#define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
186
187int mbedtls_ssl_tls1_3_derive_secret(
188 mbedtls_md_type_t hash_alg,
189 const unsigned char *secret, size_t slen,
190 const unsigned char *label, size_t llen,
191 const unsigned char *ctx, size_t clen,
192 int context_already_hashed,
193 unsigned char *dstbuf, size_t buflen );
194
Hanno Beckere9cccb42020-08-20 13:42:46 +0100195/**
196 * \brief Compute the next secret in the TLS 1.3 key schedule
197 *
198 * The TLS 1.3 key schedule proceeds as follows to compute
199 * the three main secrets during the handshake: The early
200 * secret for early data, the handshake secret for all
201 * other encrypted handshake messages, and the master
202 * secret for all application traffic.
203 *
204 * <tt>
205 * 0
206 * |
207 * v
208 * PSK -> HKDF-Extract = Early Secret
209 * |
210 * v
211 * Derive-Secret( ., "derived", "" )
212 * |
213 * v
214 * (EC)DHE -> HKDF-Extract = Handshake Secret
215 * |
216 * v
217 * Derive-Secret( ., "derived", "" )
218 * |
219 * v
220 * 0 -> HKDF-Extract = Master Secret
221 * </tt>
222 *
223 * Each of the three secrets in turn is the basis for further
224 * key derivations, such as the derivation of traffic keys and IVs;
225 * see e.g. mbedtls_ssl_tls1_3_make_traffic_keys().
226 *
227 * This function implements one step in this evolution of secrets:
228 *
229 * <tt>
230 * old_secret
231 * |
232 * v
233 * Derive-Secret( ., "derived", "" )
234 * |
235 * v
236 * input -> HKDF-Extract = new_secret
237 * </tt>
238 *
239 * \param hash_alg The identifier for the hash function used for the
240 * applications of HKDF.
241 * \param secret_old The address of the buffer holding the old secret
242 * on function entry. If not \c NULL, this must be a
243 * readable buffer whose size matches the output size
244 * of the hash function represented by \p hash_alg.
245 * If \c NULL, an all \c 0 array will be used instead.
246 * \param input The address of the buffer holding the additional
247 * input for the key derivation (e.g., the PSK or the
248 * ephemeral (EC)DH secret). If not \c NULL, this must be
249 * a readable buffer whose size \p input_len Bytes.
250 * If \c NULL, an all \c 0 array will be used instead.
251 * \param input_len The length of \p input in Bytes.
252 * \param secret_new The address of the buffer holding the new secret
253 * on function exit. This must be a writable buffer
254 * whose size matches the output size of the hash
255 * function represented by \p hash_alg.
256 * This may be the same as \p secret_old.
257 *
258 * \returns \c 0 on success.
259 * \returns A negative error code on failure.
260 */
261
262int mbedtls_ssl_tls1_3_evolve_secret(
263 mbedtls_md_type_t hash_alg,
264 const unsigned char *secret_old,
265 const unsigned char *input, size_t input_len,
266 unsigned char *secret_new );
267
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100268#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
269
270#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */