blob: 7a9e5c94ac25d57496fa4fcccf0d4a1bdf906947 [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
Hanno Becker70d7fb02020-09-09 10:11:21 +010022/* This requires MBEDTLS_SSL_TLS1_3_LABEL( idx, name, string ) to be defined at
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010023 * the point of use. See e.g. the definition of mbedtls_ssl_tls1_3_labels_union
24 * below. */
Hanno Beckere4435ea2020-09-08 10:43:52 +010025#define MBEDTLS_SSL_TLS1_3_LABEL_LIST \
Hanno Becker1413bd82020-09-09 12:46:09 +010026 MBEDTLS_SSL_TLS1_3_LABEL( finished , "finished" ) \
27 MBEDTLS_SSL_TLS1_3_LABEL( resumption , "resumption" ) \
28 MBEDTLS_SSL_TLS1_3_LABEL( traffic_upd , "traffic upd" ) \
29 MBEDTLS_SSL_TLS1_3_LABEL( exporter , "exporter" ) \
30 MBEDTLS_SSL_TLS1_3_LABEL( key , "key" ) \
31 MBEDTLS_SSL_TLS1_3_LABEL( iv , "iv" ) \
32 MBEDTLS_SSL_TLS1_3_LABEL( c_hs_traffic, "c hs traffic" ) \
33 MBEDTLS_SSL_TLS1_3_LABEL( c_ap_traffic, "c ap traffic" ) \
34 MBEDTLS_SSL_TLS1_3_LABEL( c_e_traffic , "c e traffic" ) \
35 MBEDTLS_SSL_TLS1_3_LABEL( s_hs_traffic, "s hs traffic" ) \
36 MBEDTLS_SSL_TLS1_3_LABEL( s_ap_traffic, "s ap traffic" ) \
37 MBEDTLS_SSL_TLS1_3_LABEL( s_e_traffic , "s e traffic" ) \
38 MBEDTLS_SSL_TLS1_3_LABEL( e_exp_master, "e exp master" ) \
39 MBEDTLS_SSL_TLS1_3_LABEL( res_master , "res master" ) \
40 MBEDTLS_SSL_TLS1_3_LABEL( exp_master , "exp master" ) \
41 MBEDTLS_SSL_TLS1_3_LABEL( ext_binder , "ext binder" ) \
42 MBEDTLS_SSL_TLS1_3_LABEL( res_binder , "res binder" ) \
43 MBEDTLS_SSL_TLS1_3_LABEL( derived , "derived" )
Hanno Beckere4435ea2020-09-08 10:43:52 +010044
Hanno Becker1413bd82020-09-09 12:46:09 +010045#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Hanno Beckere4435ea2020-09-08 10:43:52 +010046 const unsigned char name [ sizeof(string) - 1 ];
Hanno Beckerbe9d6642020-08-21 13:20:06 +010047
48union mbedtls_ssl_tls1_3_labels_union
49{
50 MBEDTLS_SSL_TLS1_3_LABEL_LIST
51};
52struct mbedtls_ssl_tls1_3_labels_struct
53{
54 MBEDTLS_SSL_TLS1_3_LABEL_LIST
55};
Hanno Beckera3a5a4e2020-09-08 11:33:48 +010056#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Beckere4435ea2020-09-08 10:43:52 +010057
Hanno Beckerbe9d6642020-08-21 13:20:06 +010058extern const struct mbedtls_ssl_tls1_3_labels_struct mbedtls_ssl_tls1_3_labels;
59
60#define MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( LABEL ) \
61 mbedtls_ssl_tls1_3_labels.LABEL, \
62 sizeof(mbedtls_ssl_tls1_3_labels.LABEL)
63
64#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN \
65 sizeof( union mbedtls_ssl_tls1_3_labels_union )
66
67/* The maximum length of HKDF contexts used in the TLS 1.3 standad.
68 * Since contexts are always hashes of message transcripts, this can
69 * be approximated from above by the maximum hash size. */
70#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN \
71 MBEDTLS_MD_MAX_SIZE
72
73/* Maximum desired length for expanded key material generated
74 * by HKDF-Expand-Label. */
75#define MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN 255
76
77/**
78 * \brief The \c HKDF-Expand-Label function from
79 * the TLS 1.3 standard RFC 8446.
80 *
81 * <tt>
82 * HKDF-Expand-Label( Secret, Label, Context, Length ) =
83 * HKDF-Expand( Secret, HkdfLabel, Length )
84 * </tt>
85 *
86 * \param hash_alg The identifier for the hash algorithm to use.
87 * \param secret The \c Secret argument to \c HKDF-Expand-Label.
88 * This must be a readable buffer of length \p slen Bytes.
89 * \param slen The length of \p secret in Bytes.
90 * \param label The \c Label argument to \c HKDF-Expand-Label.
91 * This must be a readable buffer of length \p llen Bytes.
92 * \param llen The length of \p label in Bytes.
93 * \param ctx The \c Context argument to \c HKDF-Expand-Label.
94 * This must be a readable buffer of length \p clen Bytes.
95 * \param clen The length of \p context in Bytes.
96 * \param buf The destination buffer to hold the expanded secret.
97 * This must be a writable buffe of length \p blen Bytes.
98 * \param blen The desired size of the expanded secret in Bytes.
99 *
100 * \returns \c 0 on success.
101 * \return A negative error code on failure.
102 */
103
104int mbedtls_ssl_tls1_3_hkdf_expand_label(
105 mbedtls_md_type_t hash_alg,
106 const unsigned char *secret, size_t slen,
107 const unsigned char *label, size_t llen,
108 const unsigned char *ctx, size_t clen,
109 unsigned char *buf, size_t blen );
110
Hanno Becker3385a4d2020-08-21 13:03:34 +0100111/**
112 * \brief This function is part of the TLS 1.3 key schedule.
113 * It extracts key and IV for the actual client/server traffic
114 * from the client/server traffic secrets.
115 *
116 * From RFC 8446:
117 *
118 * <tt>
119 * [sender]_write_key = HKDF-Expand-Label(Secret, "key", "", key_length)
120 * [sender]_write_iv = HKDF-Expand-Label(Secret, "iv", "", iv_length)*
121 * </tt>
122 *
123 * \param hash_alg The identifier for the hash algorithm to be used
124 * for the HKDF-based expansion of the secret.
125 * \param client_secret The client traffic secret.
126 * This must be a readable buffer of size \p slen Bytes
127 * \param server_secret The server traffic secret.
128 * This must be a readable buffer of size \p slen Bytes
129 * \param slen Length of the secrets \p client_secret and
130 * \p server_secret in Bytes.
Hanno Becker493ea7f2020-09-08 11:01:00 +0100131 * \param key_len The desired length of the key to be extracted in Bytes.
132 * \param iv_len The desired length of the IV to be extracted in Bytes.
Hanno Becker3385a4d2020-08-21 13:03:34 +0100133 * \param keys The address of the structure holding the generated
134 * keys and IVs.
135 *
136 * \returns \c 0 on success.
137 * \returns A negative error code on failure.
138 */
139
140int mbedtls_ssl_tls1_3_make_traffic_keys(
141 mbedtls_md_type_t hash_alg,
142 const unsigned char *client_secret,
143 const unsigned char *server_secret,
Hanno Becker493ea7f2020-09-08 11:01:00 +0100144 size_t slen, size_t key_len, size_t iv_len,
Hanno Becker3385a4d2020-08-21 13:03:34 +0100145 mbedtls_ssl_key_set *keys );
146
Hanno Becker0973ff92020-09-09 12:56:28 +0100147
148#define MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED 0
149#define MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED 1
150
Hanno Beckerb35d5222020-08-21 13:27:44 +0100151/**
152 * \brief The \c Derive-Secret function from the TLS 1.3 standard RFC 8446.
153 *
154 * <tt>
155 * Derive-Secret( Secret, Label, Messages ) =
156 * HKDF-Expand-Label( Secret, Label,
157 * Hash( Messages ),
158 * Hash.Length ) )
159 * </tt>
160 *
Hanno Becker0c42fd92020-09-09 12:58:29 +0100161 * \param hash_alg The identifier for the hash function used for the
162 * applications of HKDF.
163 * \param secret The \c Secret argument to the \c Derive-Secret function.
164 * This must be a readable buffer of length \p slen Bytes.
165 * \param slen The length of \p secret in Bytes.
166 * \param label The \c Label argument to the \c Derive-Secret function.
167 * This must be a readable buffer of length \p llen Bytes.
168 * \param llen The length of \p label in Bytes.
169 * \param ctx The hash of the \c Messages argument to the
170 * \c Derive-Secret function, or the \c Messages argument
171 * itself, depending on \p context_already_hashed.
172 * \param clen The length of \p hash.
173 * \param ctx_hashed This indicates whether the \p ctx contains the hash of
174 * the \c Messages argument in the application of the
175 * \c Derive-Secret function
176 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED), or whether
177 * it is the content of \c Messages itself, in which case
178 * the function takes care of the hashing
179 * (value MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED).
180 * \param dstbuf The target buffer to write the output of
181 * \c Derive-Secret to. This must be a writable buffer of
182 * size \p buflen Bytes.
183 * \param buflen The length of \p dstbuf in Bytes.
Hanno Beckerb35d5222020-08-21 13:27:44 +0100184 *
185 * \returns \c 0 on success.
186 * \returns A negative error code on failure.
187 */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100188int mbedtls_ssl_tls1_3_derive_secret(
189 mbedtls_md_type_t hash_alg,
190 const unsigned char *secret, size_t slen,
191 const unsigned char *label, size_t llen,
192 const unsigned char *ctx, size_t clen,
Hanno Becker0c42fd92020-09-09 12:58:29 +0100193 int ctx_hashed,
Hanno Beckerb35d5222020-08-21 13:27:44 +0100194 unsigned char *dstbuf, size_t buflen );
195
Hanno Beckere9cccb42020-08-20 13:42:46 +0100196/**
197 * \brief Compute the next secret in the TLS 1.3 key schedule
198 *
199 * The TLS 1.3 key schedule proceeds as follows to compute
200 * the three main secrets during the handshake: The early
201 * secret for early data, the handshake secret for all
202 * other encrypted handshake messages, and the master
203 * secret for all application traffic.
204 *
205 * <tt>
206 * 0
207 * |
208 * v
209 * PSK -> HKDF-Extract = Early Secret
210 * |
211 * v
212 * Derive-Secret( ., "derived", "" )
213 * |
214 * v
215 * (EC)DHE -> HKDF-Extract = Handshake Secret
216 * |
217 * v
218 * Derive-Secret( ., "derived", "" )
219 * |
220 * v
221 * 0 -> HKDF-Extract = Master Secret
222 * </tt>
223 *
224 * Each of the three secrets in turn is the basis for further
225 * key derivations, such as the derivation of traffic keys and IVs;
226 * see e.g. mbedtls_ssl_tls1_3_make_traffic_keys().
227 *
228 * This function implements one step in this evolution of secrets:
229 *
230 * <tt>
231 * old_secret
232 * |
233 * v
234 * Derive-Secret( ., "derived", "" )
235 * |
236 * v
237 * input -> HKDF-Extract = new_secret
238 * </tt>
239 *
240 * \param hash_alg The identifier for the hash function used for the
241 * applications of HKDF.
242 * \param secret_old The address of the buffer holding the old secret
243 * on function entry. If not \c NULL, this must be a
244 * readable buffer whose size matches the output size
245 * of the hash function represented by \p hash_alg.
246 * If \c NULL, an all \c 0 array will be used instead.
247 * \param input The address of the buffer holding the additional
248 * input for the key derivation (e.g., the PSK or the
249 * ephemeral (EC)DH secret). If not \c NULL, this must be
250 * a readable buffer whose size \p input_len Bytes.
251 * If \c NULL, an all \c 0 array will be used instead.
252 * \param input_len The length of \p input in Bytes.
253 * \param secret_new The address of the buffer holding the new secret
254 * on function exit. This must be a writable buffer
255 * whose size matches the output size of the hash
256 * function represented by \p hash_alg.
257 * This may be the same as \p secret_old.
258 *
259 * \returns \c 0 on success.
260 * \returns A negative error code on failure.
261 */
262
263int mbedtls_ssl_tls1_3_evolve_secret(
264 mbedtls_md_type_t hash_alg,
265 const unsigned char *secret_old,
266 const unsigned char *input, size_t input_len,
267 unsigned char *secret_new );
268
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100269#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */