Debian Codegraph @

crt/s2n/tests/unit/s2n_tls13_key_schedule_rfc8448_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 /* Needed to set up X25519 key shares */
17 #include <openssl/evp.h>
18
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_cipher_suites.h"
22 #include "tls/s2n_tls13_key_schedule.h"
23 #include "tls/s2n_tls13_secrets.h"
24
25 const s2n_mode modes[] = { S2N_SERVER, S2N_CLIENT };
26
27 static uint8_t test_send_key[S2N_TLS_AES_256_GCM_KEY_LEN] = { 0 };
28 static S2N_RESULT s2n_test_set_send_key(struct s2n_session_key *key, struct s2n_blob *in)
29 {
30 RESULT_ENSURE_REF(key);
31 RESULT_ENSURE_REF(in);
32 RESULT_CHECKED_MEMCPY(test_send_key, in->data, in->size);
33 return S2N_RESULT_OK;
34 }
35
36 static uint8_t test_recv_key[S2N_TLS_AES_256_GCM_KEY_LEN] = { 0 };
37 static S2N_RESULT s2n_test_set_recv_key(struct s2n_session_key *key, struct s2n_blob *in)
38 {
39 RESULT_ENSURE_REF(key);
40 RESULT_ENSURE_REF(in);
41 RESULT_CHECKED_MEMCPY(test_recv_key, in->data, in->size);
42 return S2N_RESULT_OK;
43 }
44
45 #define EXPECT_IVS_EQUAL(conn, iv, iv_mode) \
46 if ((iv_mode) == S2N_CLIENT) { \
47 EXPECT_BYTEARRAY_EQUAL((conn)->secure->client_implicit_iv, (iv).data, (iv).size); \
48 } else { \
49 EXPECT_BYTEARRAY_EQUAL((conn)->secure->server_implicit_iv, (iv).data, (iv).size); \
50 }
51
52 #define EXPECT_KEYS_EQUAL(conn, key, key_mode) \
53 if ((conn)->mode == (key_mode)) { \
54 EXPECT_BYTEARRAY_EQUAL(test_send_key, (key).data, (key).size); \
55 } else { \
56 EXPECT_BYTEARRAY_EQUAL(test_recv_key, (key).data, (key).size); \
57 }
58
59 static S2N_RESULT s2n_set_test_secret(struct s2n_connection *conn, uint8_t *secret_bytes, const struct s2n_blob secret)
60 {
61 RESULT_ENSURE_REF(conn);
62 RESULT_ENSURE_REF(secret_bytes);
63 RESULT_CHECKED_MEMCPY(secret_bytes, secret.data, secret.size);
64 /*
65 * Mark the last secret extracted as the master secret to
66 * indicate that all secrets have already been derived.
67 * This test is interested in keys, not secrets.
68 */
69 conn->secrets.extract_secret_type = S2N_MASTER_SECRET;
70 return S2N_RESULT_OK;
71 }
72
73 int main(int argc, char **argv)
74 {
75 BEGIN_TEST();
76
77 /* The RFC values use x25519,
78 * which is only supported via EVP APIs.
79 */
80 if (!s2n_is_evp_apis_supported()) {
81 END_TEST();
82 }
83
84 /* Once a key is set via the standard ciphers, we are unable to retrieve it.
85 * So use a custom cipher to store the keys for later verification.
86 */
87 struct s2n_cipher_suite test_cipher_suite = s2n_tls13_aes_128_gcm_sha256;
88 struct s2n_record_algorithm test_record_alg = *(test_cipher_suite.record_alg);
89 struct s2n_cipher test_cipher = *(test_record_alg.cipher);
90 test_cipher.set_decryption_key = &s2n_test_set_recv_key;
91 test_cipher.set_encryption_key = &s2n_test_set_send_key;
92 test_record_alg.cipher = &test_cipher;
93 test_cipher_suite.record_alg = &test_record_alg;
94 struct s2n_cipher_suite *cipher_suite = &test_cipher_suite;
95
96 /*
97 * Simple 1-RTT Handshake
98 */
99 {
100 const uint32_t one_rtt_handshake_type = NEGOTIATED | FULL_HANDSHAKE;
101 const int one_rtt_message_nums[] = {
102 [SERVER_HELLO] = 1,
103 [SERVER_FINISHED] = 5,
104 [CLIENT_FINISHED] = 6,
105 };
106
107 /* Derive server handshake traffic keys */
108 {
109 /**
110 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
111 *= type=test
112 *# {client} derive read traffic keys for handshake data (same as server
113 *# handshake data write traffic keys)
114 *
115 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
116 *= type=test
117 *# {server} derive write traffic keys for handshake data:
118 *#
119 *# PRK (32 octets): b6 7b 7d 69 0c c1 6c 4e 75 e5 42 13 cb 2d 37 b4
120 *# e9 c9 12 bc de d9 10 5d 42 be fd 59 d3 91 ad 38
121 *#
122 *# key info (13 octets): 00 10 09 74 6c 73 31 33 20 6b 65 79 00
123 *#
124 *# key expanded (16 octets): 3f ce 51 60 09 c2 17 27 d0 f2 e4 e8 6e
125 *# e4 03 bc
126 *#
127 *# iv info (12 octets): 00 0c 08 74 6c 73 31 33 20 69 76 00
128 *#
129 *# iv expanded (12 octets): 5d 31 3e b2 67 12 76 ee 13 00 0b 30
130 */
131 S2N_BLOB_FROM_HEX(secret, "b6 7b 7d 69 0c c1 6c 4e 75 e5 42 13 cb 2d 37 b4 \
132 e9 c9 12 bc de d9 10 5d 42 be fd 59 d3 91 ad 38");
133 S2N_BLOB_FROM_HEX(key, "3f ce 51 60 09 c2 17 27 d0 f2 e4 e8 6e \
134 e4 03 bc");
135 S2N_BLOB_FROM_HEX(iv, "5d 31 3e b2 67 12 76 ee 13 00 0b 30");
136
137 for (size_t i = 0; i < s2n_array_len(modes); i++) {
138 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(modes[i]), s2n_connection_ptr_free);
139 conn->secure->cipher_suite = cipher_suite;
140 conn->actual_protocol_version = S2N_TLS13;
141 EXPECT_OK(s2n_set_test_secret(conn, conn->secrets.version.tls13.server_handshake_secret, secret));
142
143 conn->handshake.handshake_type = one_rtt_handshake_type;
144 conn->handshake.message_number = one_rtt_message_nums[SERVER_HELLO];
145 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), SERVER_HELLO);
146 EXPECT_OK(s2n_tls13_key_schedule_update(conn));
147
148 EXPECT_IVS_EQUAL(conn, iv, S2N_SERVER);
149 EXPECT_KEYS_EQUAL(conn, key, S2N_SERVER);
150 }
151 };
152
153 /* Derive client handshake traffic keys */
154 {
155 /**
156 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
157 *= type=test
158 *# {client} derive write traffic keys for handshake data (same as
159 *# server handshake data read traffic keys)
160 *
161 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
162 *= type=test
163 *# {server} derive read traffic keys for handshake data:
164 *#
165 *# PRK (32 octets): b3 ed db 12 6e 06 7f 35 a7 80 b3 ab f4 5e 2d 8f
166 *# 3b 1a 95 07 38 f5 2e 96 00 74 6a 0e 27 a5 5a 21
167 *#
168 *# key info (13 octets): 00 10 09 74 6c 73 31 33 20 6b 65 79 00
169 *#
170 *# key expanded (16 octets): db fa a6 93 d1 76 2c 5b 66 6a f5 d9 50
171 *# 25 8d 01
172 *#
173 *# iv info (12 octets): 00 0c 08 74 6c 73 31 33 20 69 76 00
174 *#
175 *# iv expanded (12 octets): 5b d3 c7 1b 83 6e 0b 76 bb 73 26 5f
176 */
177 S2N_BLOB_FROM_HEX(secret, "b3 ed db 12 6e 06 7f 35 a7 80 b3 ab f4 5e 2d 8f \
178 3b 1a 95 07 38 f5 2e 96 00 74 6a 0e 27 a5 5a 21");
179 S2N_BLOB_FROM_HEX(key, "db fa a6 93 d1 76 2c 5b 66 6a f5 d9 50 \
180 25 8d 01");
181 S2N_BLOB_FROM_HEX(iv, "5b d3 c7 1b 83 6e 0b 76 bb 73 26 5f");
182
183 for (size_t i = 0; i < s2n_array_len(modes); i++) {
184 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(modes[i]), s2n_connection_ptr_free);
185 conn->secure->cipher_suite = cipher_suite;
186 conn->actual_protocol_version = S2N_TLS13;
187 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
188 EXPECT_OK(s2n_set_test_secret(conn, conn->secrets.version.tls13.client_handshake_secret, secret));
189
190 conn->handshake.handshake_type = one_rtt_handshake_type;
191 conn->handshake.message_number = one_rtt_message_nums[SERVER_FINISHED];
192 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), SERVER_FINISHED);
193 EXPECT_OK(s2n_tls13_key_schedule_update(conn));
194
195 EXPECT_IVS_EQUAL(conn, iv, S2N_CLIENT);
196 EXPECT_KEYS_EQUAL(conn, key, S2N_CLIENT);
197 }
198 };
199
200 /* Derive server application traffic keys */
201 {
202 /**
203 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
204 *= type=test
205 *# {client} derive read traffic keys for application data (same as
206 *# server application data write traffic keys)
207 *
208 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
209 *= type=test
210 *# {server} derive write traffic keys for application data:
211 *#
212 *# PRK (32 octets): a1 1a f9 f0 55 31 f8 56 ad 47 11 6b 45 a9 50 32
213 *# 82 04 b4 f4 4b fb 6b 3a 4b 4f 1f 3f cb 63 16 43
214 *#
215 *# key info (13 octets): 00 10 09 74 6c 73 31 33 20 6b 65 79 00
216 *#
217 *# key expanded (16 octets): 9f 02 28 3b 6c 9c 07 ef c2 6b b9 f2 ac
218 *# 92 e3 56
219 *#
220 *# iv info (12 octets): 00 0c 08 74 6c 73 31 33 20 69 76 00
221 *#
222 *# iv expanded (12 octets): cf 78 2b 88 dd 83 54 9a ad f1 e9 84
223 */
224 S2N_BLOB_FROM_HEX(secret, "a1 1a f9 f0 55 31 f8 56 ad 47 11 6b 45 a9 50 32 \
225 82 04 b4 f4 4b fb 6b 3a 4b 4f 1f 3f cb 63 16 43");
226 S2N_BLOB_FROM_HEX(key, "9f 02 28 3b 6c 9c 07 ef c2 6b b9 f2 ac \
227 92 e3 56");
228 S2N_BLOB_FROM_HEX(iv, "cf 78 2b 88 dd 83 54 9a ad f1 e9 84");
229
230 const message_type_t trigger_messages[] = {
231 [S2N_CLIENT] = CLIENT_FINISHED,
232 [S2N_SERVER] = SERVER_FINISHED,
233 };
234
235 for (size_t i = 0; i < s2n_array_len(modes); i++) {
236 message_type_t trigger_message = trigger_messages[modes[i]];
237 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(modes[i]), s2n_connection_ptr_free);
238 conn->secure->cipher_suite = cipher_suite;
239 conn->actual_protocol_version = S2N_TLS13;
240 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
241 EXPECT_OK(s2n_set_test_secret(conn, conn->secrets.version.tls13.server_app_secret, secret));
242
243 conn->handshake.handshake_type = one_rtt_handshake_type;
244 conn->handshake.message_number = one_rtt_message_nums[trigger_message];
245 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), trigger_message);
246 EXPECT_OK(s2n_tls13_key_schedule_update(conn));
247
248 EXPECT_IVS_EQUAL(conn, iv, S2N_SERVER);
249 EXPECT_KEYS_EQUAL(conn, key, S2N_SERVER);
250 }
251 };
252
253 /* Derive client application traffic keys */
254 {
255 /**
256 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
257 *= type=test
258 *# {server} derive read traffic keys for application data (same as
259 *# client application data write traffic keys)
260 *
261 *= https://www.rfc-editor.org/rfc/rfc8448#section-3
262 *= type=test
263 *# {client} derive write traffic keys for application data:
264 *#
265 *# PRK (32 octets): 9e 40 64 6c e7 9a 7f 9d c0 5a f8 88 9b ce 65 52
266 *# 87 5a fa 0b 06 df 00 87 f7 92 eb b7 c1 75 04 a5
267 *#
268 *# key info (13 octets): 00 10 09 74 6c 73 31 33 20 6b 65 79 00
269 *#
270 *# key expanded (16 octets): 17 42 2d da 59 6e d5 d9 ac d8 90 e3 c6
271 *# 3f 50 51
272 *#
273 *# iv info (12 octets): 00 0c 08 74 6c 73 31 33 20 69 76 00
274 *#
275 *# iv expanded (12 octets): 5b 78 92 3d ee 08 57 90 33 e5 23 d9
276 */
277 S2N_BLOB_FROM_HEX(secret, "9e 40 64 6c e7 9a 7f 9d c0 5a f8 88 9b ce 65 52 \
278 87 5a fa 0b 06 df 00 87 f7 92 eb b7 c1 75 04 a5");
279 S2N_BLOB_FROM_HEX(key, "17 42 2d da 59 6e d5 d9 ac d8 90 e3 c6 \
280 3f 50 51");
281 S2N_BLOB_FROM_HEX(iv, "5b 78 92 3d ee 08 57 90 33 e5 23 d9");
282
283 for (size_t i = 0; i < s2n_array_len(modes); i++) {
284 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(modes[i]), s2n_connection_ptr_free);
285 conn->secure->cipher_suite = cipher_suite;
286 conn->actual_protocol_version = S2N_TLS13;
287 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
288 EXPECT_OK(s2n_set_test_secret(conn, conn->secrets.version.tls13.client_app_secret, secret));
289
290 conn->handshake.handshake_type = one_rtt_handshake_type;
291 conn->handshake.message_number = one_rtt_message_nums[CLIENT_FINISHED];
292 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), CLIENT_FINISHED);
293 EXPECT_OK(s2n_tls13_key_schedule_update(conn));
294
295 EXPECT_IVS_EQUAL(conn, iv, S2N_CLIENT);
296 EXPECT_KEYS_EQUAL(conn, key, S2N_CLIENT);
297 }
298 };
299 };
300
301 /* Resumed 0-RTT Handshake */
302 {
303 const uint32_t resumed_handshake_type = NEGOTIATED | WITH_EARLY_DATA;
304 const int resumed_message_nums[] = {
305 [CLIENT_HELLO] = 0,
306 [SERVER_FINISHED] = 3,
307 };
308
309 /* Derive early application traffic keys */
310 {
311 /**
312 *= https://www.rfc-editor.org/rfc/rfc8448#section-4
313 *= type=test
314 *# {server} derive read traffic keys for early application data (same
315 *# as client early application data write traffic keys)
316 *
317 *= https://www.rfc-editor.org/rfc/rfc8448#section-4
318 *= type=test
319 *# {client} derive write traffic keys for early application data:
320 *#
321 *# PRK (32 octets): 3f bb e6 a6 0d eb 66 c3 0a 32 79 5a ba 0e ff 7e
322 *# aa 10 10 55 86 e7 be 5c 09 67 8d 63 b6 ca ab 62
323 *#
324 *# key info (13 octets): 00 10 09 74 6c 73 31 33 20 6b 65 79 00
325 *#
326 *# key expanded (16 octets): 92 02 05 a5 b7 bf 21 15 e6 fc 5c 29 42
327 *# 83 4f 54
328 *#
329 *# iv info (12 octets): 00 0c 08 74 6c 73 31 33 20 69 76 00
330 *#
331 *# iv expanded (12 octets): 6d 47 5f 09 93 c8 e5 64 61 0d b2 b9
332 */
333 S2N_BLOB_FROM_HEX(secret, "3f bb e6 a6 0d eb 66 c3 0a 32 79 5a ba 0e ff 7e \
334 aa 10 10 55 86 e7 be 5c 09 67 8d 63 b6 ca ab 62");
335 S2N_BLOB_FROM_HEX(key, "92 02 05 a5 b7 bf 21 15 e6 fc 5c 29 42 \
336 83 4f 54");
337 S2N_BLOB_FROM_HEX(iv, "6d 47 5f 09 93 c8 e5 64 61 0d b2 b9");
338
339 const message_type_t trigger_messages[] = {
340 [S2N_CLIENT] = CLIENT_HELLO,
341 [S2N_SERVER] = SERVER_FINISHED,
342 };
343
344 for (size_t i = 0; i < s2n_array_len(modes); i++) {
345 message_type_t trigger_message = trigger_messages[modes[i]];
346 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(modes[i]), s2n_connection_ptr_free);
347 conn->secure->cipher_suite = cipher_suite;
348 conn->actual_protocol_version = S2N_TLS13;
349 EXPECT_OK(s2n_conn_choose_state_machine(conn, S2N_TLS13));
350 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
351 EXPECT_OK(s2n_set_test_secret(conn, conn->secrets.version.tls13.client_early_secret, secret));
352
353 conn->handshake.handshake_type = resumed_handshake_type;
354 conn->handshake.message_number = resumed_message_nums[trigger_message];
355 EXPECT_EQUAL(s2n_conn_get_current_message_type(conn), trigger_message);
356 EXPECT_OK(s2n_tls13_key_schedule_update(conn));
357
358 EXPECT_IVS_EQUAL(conn, iv, S2N_CLIENT);
359 EXPECT_KEYS_EQUAL(conn, key, S2N_CLIENT);
360 }
361 };
362 };
363
364 END_TEST();
365 }