blob: 161f0a114e121bab8007245a9434d14c91888e13 [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 *
161 * Note: In this implementation of the function we assume that
162 * the parameter message contains the already hashed value and
163 * the Derive-Secret function does not need to hash it again.
164 *
165 * \param hash_alg The identifier for the hash function used for the
166 * applications of HKDF.
167 * \param secret The \c Secret argument to the \c Derive-Secret function.
168 * This must be a readable buffer of length \p slen Bytes.
169 * \param slen The length of \p secret in Bytes.
170 * \param label The \c Label argument to the \c Derive-Secret function.
171 * This must be a readable buffer of length \p llen Bytes.
172 * \param llen The length of \p label in Bytes.
173 * \param hash The hash of the \c Messages argument to the \c Derive-Secret
174 * function. This must be a readable buffer of length \p mlen
175 * hlen Bytes.
176 * \param hlen The length of \p hash.
177 * \param dstbuf The target buffer to write the output of \c Derive-Secret to.
178 * This must be a writable buffer of size \p buflen Bytes.
179 * \param buflen The length of \p dstbuf in Bytes.
180 *
181 * \returns \c 0 on success.
182 * \returns A negative error code on failure.
183 */
Hanno Beckerb35d5222020-08-21 13:27:44 +0100184int mbedtls_ssl_tls1_3_derive_secret(
185 mbedtls_md_type_t hash_alg,
186 const unsigned char *secret, size_t slen,
187 const unsigned char *label, size_t llen,
188 const unsigned char *ctx, size_t clen,
189 int context_already_hashed,
190 unsigned char *dstbuf, size_t buflen );
191
Hanno Beckere9cccb42020-08-20 13:42:46 +0100192/**
193 * \brief Compute the next secret in the TLS 1.3 key schedule
194 *
195 * The TLS 1.3 key schedule proceeds as follows to compute
196 * the three main secrets during the handshake: The early
197 * secret for early data, the handshake secret for all
198 * other encrypted handshake messages, and the master
199 * secret for all application traffic.
200 *
201 * <tt>
202 * 0
203 * |
204 * v
205 * PSK -> HKDF-Extract = Early Secret
206 * |
207 * v
208 * Derive-Secret( ., "derived", "" )
209 * |
210 * v
211 * (EC)DHE -> HKDF-Extract = Handshake Secret
212 * |
213 * v
214 * Derive-Secret( ., "derived", "" )
215 * |
216 * v
217 * 0 -> HKDF-Extract = Master Secret
218 * </tt>
219 *
220 * Each of the three secrets in turn is the basis for further
221 * key derivations, such as the derivation of traffic keys and IVs;
222 * see e.g. mbedtls_ssl_tls1_3_make_traffic_keys().
223 *
224 * This function implements one step in this evolution of secrets:
225 *
226 * <tt>
227 * old_secret
228 * |
229 * v
230 * Derive-Secret( ., "derived", "" )
231 * |
232 * v
233 * input -> HKDF-Extract = new_secret
234 * </tt>
235 *
236 * \param hash_alg The identifier for the hash function used for the
237 * applications of HKDF.
238 * \param secret_old The address of the buffer holding the old secret
239 * on function entry. If not \c NULL, this must be a
240 * readable buffer whose size matches the output size
241 * of the hash function represented by \p hash_alg.
242 * If \c NULL, an all \c 0 array will be used instead.
243 * \param input The address of the buffer holding the additional
244 * input for the key derivation (e.g., the PSK or the
245 * ephemeral (EC)DH secret). If not \c NULL, this must be
246 * a readable buffer whose size \p input_len Bytes.
247 * If \c NULL, an all \c 0 array will be used instead.
248 * \param input_len The length of \p input in Bytes.
249 * \param secret_new The address of the buffer holding the new secret
250 * on function exit. This must be a writable buffer
251 * whose size matches the output size of the hash
252 * function represented by \p hash_alg.
253 * This may be the same as \p secret_old.
254 *
255 * \returns \c 0 on success.
256 * \returns A negative error code on failure.
257 */
258
259int mbedtls_ssl_tls1_3_evolve_secret(
260 mbedtls_md_type_t hash_alg,
261 const unsigned char *secret_old,
262 const unsigned char *input, size_t input_len,
263 unsigned char *secret_new );
264
Hanno Beckerbe9d6642020-08-21 13:20:06 +0100265#endif /* MBEDTLS_SSL_TLS1_3_KEYS_H */