Debian Codegraph @

crt/s2n/tests/unit/s2n_aes_sha_composite_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 #include <openssl/evp.h>
17 #include <string.h>
18
19 #include "api/s2n.h"
20 #include "crypto/s2n_cipher.h"
21 #include "crypto/s2n_hash.h"
22 #include "crypto/s2n_hmac.h"
23 #include "s2n_test.h"
24 #include "stuffer/s2n_stuffer.h"
25 #include "testlib/s2n_testlib.h"
26 #include "tls/s2n_cipher_suites.h"
27 #include "tls/s2n_record.h"
28 #include "utils/s2n_random.h"
29
30 /* Explicit IV starts after the TLS record header. */
31 #define EXPLICIT_IV_OFFSET S2N_TLS_RECORD_HEADER_LENGTH
32
33 /* IVs should never be repeated with the same session key. */
34 static int ensure_explicit_iv_is_unique(uint8_t existing_explicit_ivs[S2N_DEFAULT_FRAGMENT_LENGTH][S2N_TLS_MAX_IV_LEN],
35 size_t num_existing_ivs,
36 const uint8_t *candidate_iv,
37 uint16_t iv_len)
38 {
39 for (size_t i = 0; i < num_existing_ivs; i++) {
40 if (memcmp(existing_explicit_ivs[i], candidate_iv, iv_len) == 0) {
41 return S2N_FAILURE;
42 }
43 }
44
45 return S2N_SUCCESS;
46 }
47
48 int main(int argc, char **argv)
49 {
50 struct s2n_connection *conn = NULL;
51 uint8_t random_data[S2N_DEFAULT_FRAGMENT_LENGTH + 1];
52 uint8_t mac_key_sha[20] = "server key shaserve";
53 uint8_t mac_key_sha256[32] = "server key sha256server key sha";
54 uint8_t aes128_key[] = "123456789012345";
55 uint8_t aes256_key[] = "1234567890123456789012345678901";
56 struct s2n_blob aes128 = { 0 };
57 EXPECT_SUCCESS(s2n_blob_init(&aes128, aes128_key, sizeof(aes128_key)));
58 struct s2n_blob aes256 = { 0 };
59 EXPECT_SUCCESS(s2n_blob_init(&aes256, aes256_key, sizeof(aes256_key)));
60 struct s2n_blob r = { 0 };
61 EXPECT_SUCCESS(s2n_blob_init(&r, random_data, sizeof(random_data)));
62 /* Stores explicit IVs used in each test case to validate uniqueness. */
63 uint8_t existing_explicit_ivs[S2N_DEFAULT_FRAGMENT_LENGTH + 2][S2N_TLS_MAX_IV_LEN];
64
65 BEGIN_TEST();
66 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
67
68 /* Skip test if we can't use the ciphers */
69 if (!s2n_aes128_sha.is_available()
70 || !s2n_aes256_sha.is_available()
71 || !s2n_aes128_sha256.is_available()
72 || !s2n_aes256_sha256.is_available()) {
73 END_TEST();
74 }
75
76 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
77 EXPECT_OK(s2n_get_public_random_data(&r));
78
79 /* Peer and we are in sync */
80 conn->server = conn->initial;
81 conn->client = conn->initial;
82
83 const int max_aligned_fragment = S2N_DEFAULT_FRAGMENT_LENGTH;
84 const uint8_t proto_versions[3] = { S2N_TLS10, S2N_TLS11, S2N_TLS12 };
85
86 /* test the composite AES128_SHA1 cipher */
87 conn->initial->cipher_suite->record_alg = &s2n_record_alg_aes128_sha_composite;
88
89 /* It's important to verify all TLS versions for the composite implementation.
90 * There are a few gotchas with respect to explicit IV length and payload length
91 */
92 for (int j = 0; j < 3; j++) {
93 for (size_t i = 0; i <= max_aligned_fragment + 1; i++) {
94 struct s2n_blob in = { 0 };
95 EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i));
96 int bytes_written = 0;
97
98 EXPECT_SUCCESS(s2n_connection_wipe(conn));
99
100 EXPECT_OK(conn->initial->cipher_suite->record_alg->cipher->set_encryption_key(&conn->initial->server_key, &aes128));
101 EXPECT_OK(conn->initial->cipher_suite->record_alg->cipher->set_decryption_key(&conn->initial->client_key, &aes128));
102 EXPECT_SUCCESS(conn->initial->cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->initial->server_key, mac_key_sha, sizeof(mac_key_sha)));
103 EXPECT_SUCCESS(conn->initial->cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->initial->client_key, mac_key_sha, sizeof(mac_key_sha)));
104
105 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out));
106 conn->actual_protocol_version = proto_versions[j];
107
108 int explicit_iv_len = 0;
109 if (conn->actual_protocol_version > S2N_TLS10) {
110 explicit_iv_len = 16;
111 } else {
112 explicit_iv_len = 0;
113 }
114
115 s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in);
116 if (i <= max_aligned_fragment) {
117 EXPECT_OK(result);
118 bytes_written = i;
119 } else {
120 EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
121 bytes_written = max_aligned_fragment;
122 }
123
124 uint16_t predicted_length = bytes_written + 1 + SHA_DIGEST_LENGTH + explicit_iv_len;
125 if (predicted_length % 16) {
126 predicted_length += (16 - (predicted_length % 16));
127 }
128 EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA);
129 uint8_t record_version = conn->out.blob.data[1] * 10 + conn->out.blob.data[2];
130 EXPECT_EQUAL(record_version, conn->actual_protocol_version);
131 EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff);
132 EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff);
133
134 /* The data should be encrypted */
135 if (bytes_written > 10) {
136 EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + EXPLICIT_IV_OFFSET + explicit_iv_len, random_data, bytes_written), 0);
137 }
138
139 if (explicit_iv_len > 0) {
140 /* The explicit IV for every record written should be random */
141 uint8_t *explicit_iv = conn->out.blob.data + EXPLICIT_IV_OFFSET;
142 EXPECT_SUCCESS(ensure_explicit_iv_is_unique(existing_explicit_ivs, i, explicit_iv, explicit_iv_len));
143 /* Record this IV */
144 EXPECT_MEMCPY_SUCCESS(existing_explicit_ivs[i], explicit_iv, explicit_iv_len);
145 }
146
147 /* Copy the encrypted out data to the in data */
148 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
149 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
150 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH));
151 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
152
153 /* Let's decrypt it */
154 uint8_t content_type = 0;
155 uint16_t fragment_length = 0;
156 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
157 EXPECT_SUCCESS(s2n_record_parse(conn));
158 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA);
159 EXPECT_EQUAL(fragment_length, predicted_length);
160
161 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
162 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
163 }
164 }
165
166 /* test the composite AES256_SHA1 cipher */
167 conn->initial->cipher_suite->record_alg = &s2n_record_alg_aes256_sha_composite;
168 for (int j = 0; j < 3; j++) {
169 for (int i = 0; i <= max_aligned_fragment + 1; i++) {
170 struct s2n_blob in = { 0 };
171 EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i));
172 int bytes_written = 0;
173
174 EXPECT_SUCCESS(s2n_connection_wipe(conn));
175
176 EXPECT_OK(conn->initial->cipher_suite->record_alg->cipher->set_encryption_key(&conn->initial->server_key, &aes256));
177 EXPECT_OK(conn->initial->cipher_suite->record_alg->cipher->set_decryption_key(&conn->initial->client_key, &aes256));
178 EXPECT_SUCCESS(conn->initial->cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->initial->server_key, mac_key_sha, sizeof(mac_key_sha)));
179 EXPECT_SUCCESS(conn->initial->cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->initial->client_key, mac_key_sha, sizeof(mac_key_sha)));
180
181 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out));
182 conn->actual_protocol_version = proto_versions[j];
183
184 int explicit_iv_len = 0;
185 if (conn->actual_protocol_version > S2N_TLS10) {
186 explicit_iv_len = 16;
187 } else {
188 explicit_iv_len = 0;
189 }
190
191 s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in);
192 if (i <= max_aligned_fragment) {
193 EXPECT_OK(result);
194 bytes_written = i;
195 } else {
196 EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
197 bytes_written = max_aligned_fragment;
198 }
199
200 uint16_t predicted_length = bytes_written + 1 + SHA_DIGEST_LENGTH + explicit_iv_len;
201 if (predicted_length % 16) {
202 predicted_length += (16 - (predicted_length % 16));
203 }
204 EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA);
205 uint8_t record_version = conn->out.blob.data[1] * 10 + conn->out.blob.data[2];
206 EXPECT_EQUAL(record_version, conn->actual_protocol_version);
207 EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff);
208 EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff);
209
210 /* The data should be encrypted */
211 if (bytes_written > 10) {
212 EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + EXPLICIT_IV_OFFSET + explicit_iv_len, random_data, bytes_written), 0);
213 }
214
215 if (explicit_iv_len > 0) {
216 /* The explicit IV for every record written should be random */
217 uint8_t *explicit_iv = conn->out.blob.data + EXPLICIT_IV_OFFSET;
218 EXPECT_SUCCESS(ensure_explicit_iv_is_unique(existing_explicit_ivs, i, explicit_iv, explicit_iv_len));
219 /* Record this IV */
220 EXPECT_MEMCPY_SUCCESS(existing_explicit_ivs[i], explicit_iv, explicit_iv_len);
221 }
222
223 /* Copy the encrypted out data to the in data */
224 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
225 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
226 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH));
227 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
228
229 /* Let's decrypt it */
230 uint8_t content_type = 0;
231 uint16_t fragment_length = 0;
232 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
233 EXPECT_SUCCESS(s2n_record_parse(conn));
234 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA);
235 EXPECT_EQUAL(fragment_length, predicted_length);
236
237 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
238 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
239 }
240 }
241
242 /* test the composite AES128_SHA256 cipher */
243 conn->initial->cipher_suite->record_alg = &s2n_record_alg_aes128_sha256_composite;
244 for (int j = 0; j < 3; j++) {
245 for (int i = 0; i < max_aligned_fragment + 1; i++) {
246 struct s2n_blob in = { 0 };
247 EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i));
248 int bytes_written = 0;
249
250 EXPECT_SUCCESS(s2n_connection_wipe(conn));
251
252 EXPECT_OK(conn->initial->cipher_suite->record_alg->cipher->set_encryption_key(&conn->initial->server_key, &aes128));
253 EXPECT_OK(conn->initial->cipher_suite->record_alg->cipher->set_decryption_key(&conn->initial->client_key, &aes128));
254 EXPECT_SUCCESS(conn->initial->cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->initial->server_key, mac_key_sha256, sizeof(mac_key_sha256)));
255 EXPECT_SUCCESS(conn->initial->cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->initial->client_key, mac_key_sha256, sizeof(mac_key_sha256)));
256
257 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out));
258 conn->actual_protocol_version = proto_versions[j];
259
260 int explicit_iv_len = 0;
261 if (conn->actual_protocol_version > S2N_TLS10) {
262 explicit_iv_len = 16;
263 } else {
264 explicit_iv_len = 0;
265 }
266
267 s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in);
268 if (i <= max_aligned_fragment) {
269 EXPECT_OK(result);
270 bytes_written = i;
271 } else {
272 EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
273 bytes_written = max_aligned_fragment;
274 }
275
276 uint16_t predicted_length = bytes_written + 1 + SHA256_DIGEST_LENGTH + explicit_iv_len;
277 if (predicted_length % 16) {
278 predicted_length += (16 - (predicted_length % 16));
279 }
280 EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA);
281 uint8_t record_version = conn->out.blob.data[1] * 10 + conn->out.blob.data[2];
282 EXPECT_EQUAL(record_version, conn->actual_protocol_version);
283 EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff);
284 EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff);
285
286 /* The data should be encrypted */
287 if (bytes_written > 10) {
288 EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + EXPLICIT_IV_OFFSET + explicit_iv_len, random_data, bytes_written), 0);
289 }
290
291 if (explicit_iv_len > 0) {
292 /* The explicit IV for every record written should be random */
293 uint8_t *explicit_iv = conn->out.blob.data + EXPLICIT_IV_OFFSET;
294 EXPECT_SUCCESS(ensure_explicit_iv_is_unique(existing_explicit_ivs, i, explicit_iv, explicit_iv_len));
295 /* Record this IV */
296 EXPECT_MEMCPY_SUCCESS(existing_explicit_ivs[i], explicit_iv, explicit_iv_len);
297 }
298
299 /* Copy the encrypted out data to the in data */
300 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
301 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
302 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH));
303 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
304
305 /* Let's decrypt it */
306 uint8_t content_type = 0;
307 uint16_t fragment_length = 0;
308 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
309 EXPECT_SUCCESS(s2n_record_parse(conn));
310 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA);
311 EXPECT_EQUAL(fragment_length, predicted_length);
312
313 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
314 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
315 }
316 }
317
318 /* test the composite AES256_SHA256 cipher */
319 conn->initial->cipher_suite->record_alg = &s2n_record_alg_aes256_sha256_composite;
320 for (int j = 0; j < 3; j++) {
321 for (int i = 0; i <= max_aligned_fragment + 1; i++) {
322 struct s2n_blob in = { 0 };
323 EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i));
324 int bytes_written = 0;
325
326 EXPECT_SUCCESS(s2n_connection_wipe(conn));
327
328 EXPECT_OK(conn->initial->cipher_suite->record_alg->cipher->set_encryption_key(&conn->initial->server_key, &aes256));
329 EXPECT_OK(conn->initial->cipher_suite->record_alg->cipher->set_decryption_key(&conn->initial->client_key, &aes256));
330 EXPECT_SUCCESS(conn->initial->cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->initial->server_key, mac_key_sha256, sizeof(mac_key_sha256)));
331 EXPECT_SUCCESS(conn->initial->cipher_suite->record_alg->cipher->io.comp.set_mac_write_key(&conn->initial->client_key, mac_key_sha256, sizeof(mac_key_sha256)));
332
333 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out));
334 conn->actual_protocol_version = proto_versions[j];
335
336 int explicit_iv_len = 0;
337 if (conn->actual_protocol_version > S2N_TLS10) {
338 explicit_iv_len = 16;
339 } else {
340 explicit_iv_len = 0;
341 }
342
343 s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in);
344 if (i <= max_aligned_fragment) {
345 EXPECT_OK(result);
346 bytes_written = i;
347 } else {
348 EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
349 bytes_written = max_aligned_fragment;
350 }
351
352 uint16_t predicted_length = bytes_written + 1 + SHA256_DIGEST_LENGTH + explicit_iv_len;
353 if (predicted_length % 16) {
354 predicted_length += (16 - (predicted_length % 16));
355 }
356 EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA);
357 uint8_t record_version = conn->out.blob.data[1] * 10 + conn->out.blob.data[2];
358 EXPECT_EQUAL(record_version, conn->actual_protocol_version);
359 EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff);
360 EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff);
361
362 /* The data should be encrypted */
363 if (bytes_written > 10) {
364 EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + EXPLICIT_IV_OFFSET + explicit_iv_len, random_data, bytes_written), 0);
365 }
366
367 if (explicit_iv_len > 0) {
368 /* The explicit IV for every record written should be random */
369 uint8_t *explicit_iv = conn->out.blob.data + EXPLICIT_IV_OFFSET;
370 EXPECT_SUCCESS(ensure_explicit_iv_is_unique(existing_explicit_ivs, i, explicit_iv, explicit_iv_len));
371 /* Record this IV */
372 EXPECT_MEMCPY_SUCCESS(existing_explicit_ivs[i], explicit_iv, explicit_iv_len);
373 }
374
375 /* Copy the encrypted out data to the in data */
376 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
377 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
378 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, S2N_TLS_RECORD_HEADER_LENGTH));
379 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
380
381 /* Let's decrypt it */
382 uint8_t content_type = 0;
383 uint16_t fragment_length = 0;
384 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
385 EXPECT_SUCCESS(s2n_record_parse(conn));
386 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA);
387 EXPECT_EQUAL(fragment_length, predicted_length);
388
389 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
390 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
391 }
392 }
393
394 EXPECT_SUCCESS(s2n_connection_free(conn));
395
396 END_TEST();
397 }