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