Debian Codegraph @

crt/s2n/tests/unit/s2n_aead_chacha20_poly1305_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 <stdio.h>
17 #include <string.h>
18
19 #include "api/s2n.h"
20 #include "crypto/s2n_cipher.h"
21 #include "crypto/s2n_hmac.h"
22 #include "s2n_test.h"
23 #include "stuffer/s2n_stuffer.h"
24 #include "testlib/s2n_testlib.h"
25 #include "tls/s2n_cipher_suites.h"
26 #include "tls/s2n_crypto.h"
27 #include "tls/s2n_prf.h"
28 #include "tls/s2n_record.h"
29 #include "utils/s2n_random.h"
30 #include "utils/s2n_safety.h"
31
32 static int destroy_server_keys(struct s2n_connection *server_conn)
33 {
34 POSIX_GUARD_RESULT(server_conn->initial->cipher_suite->record_alg->cipher->destroy_key(&server_conn->initial->server_key));
35 POSIX_GUARD_RESULT(server_conn->initial->cipher_suite->record_alg->cipher->destroy_key(&server_conn->initial->client_key));
36 return 0;
37 }
38
39 static int setup_server_keys(struct s2n_connection *server_conn, struct s2n_blob *key)
40 {
41 POSIX_GUARD_RESULT(server_conn->initial->cipher_suite->record_alg->cipher->init(&server_conn->initial->server_key));
42 POSIX_GUARD_RESULT(server_conn->initial->cipher_suite->record_alg->cipher->init(&server_conn->initial->client_key));
43 POSIX_GUARD_RESULT(server_conn->initial->cipher_suite->record_alg->cipher->set_encryption_key(&server_conn->initial->server_key, key));
44 POSIX_GUARD_RESULT(server_conn->initial->cipher_suite->record_alg->cipher->set_decryption_key(&server_conn->initial->client_key, key));
45
46 return 0;
47 }
48
49 int main(int argc, char **argv)
50 {
51 struct s2n_connection *conn = NULL;
52 uint8_t random_data[S2N_SMALL_FRAGMENT_LENGTH + 1];
53 uint8_t chacha20_poly1305_key_data[] = "1234567890123456789012345678901";
54 struct s2n_blob chacha20_poly1305_key = { 0 };
55 EXPECT_SUCCESS(s2n_blob_init(&chacha20_poly1305_key, chacha20_poly1305_key_data, sizeof(chacha20_poly1305_key_data)));
56 struct s2n_blob r = { 0 };
57 EXPECT_SUCCESS(s2n_blob_init(&r, random_data, sizeof(random_data)));
58
59 BEGIN_TEST();
60 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
61
62 /* Skip test if librcrypto doesn't support the cipher */
63 if (!s2n_chacha20_poly1305.is_available()) {
64 END_TEST();
65 }
66
67 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
68 EXPECT_OK(s2n_get_public_random_data(&r));
69
70 /* Peer and we are in sync */
71 conn->server = conn->initial;
72 conn->client = conn->initial;
73
74 /* test the chacha20_poly1305 cipher */
75 conn->initial->cipher_suite->record_alg = &s2n_record_alg_chacha20_poly1305;
76 POSIX_GUARD(setup_server_keys(conn, &chacha20_poly1305_key));
77
78 int max_fragment = S2N_SMALL_FRAGMENT_LENGTH;
79 for (size_t i = 0; i <= max_fragment + 1; i++) {
80 struct s2n_blob in = { 0 };
81 EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i));
82 int bytes_written = 0;
83
84 /* TLS packet on the wire using ChaCha20-Poly1305:
85 * https://tools.ietf.org/html/rfc5246#section-6.2.3.3
86 * https://tools.ietf.org/html/rfc7905#section-2
87 * ----------------------------------
88 * |TLS header|encrypted payload|TAG|
89 * ----------------------------------
90 * Length:
91 * S2N_TLS_RECORD_HEADER_LENGTH + i + S2N_TLS_CHACHA20_POLY1305_TAG_LEN
92 */
93
94 EXPECT_SUCCESS(s2n_connection_wipe(conn));
95 EXPECT_SUCCESS(s2n_connection_prefer_low_latency(conn));
96 conn->actual_protocol_version_established = 1;
97 conn->server_protocol_version = S2N_TLS12;
98 conn->client_protocol_version = S2N_TLS12;
99 conn->actual_protocol_version = S2N_TLS12;
100 EXPECT_SUCCESS(destroy_server_keys(conn));
101 EXPECT_SUCCESS(setup_server_keys(conn, &chacha20_poly1305_key));
102
103 s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in);
104 if (i <= max_fragment) {
105 EXPECT_OK(result);
106 bytes_written = i;
107 } else {
108 EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
109 bytes_written = max_fragment;
110 }
111
112 static const int overhead = S2N_TLS_CHACHA20_POLY1305_EXPLICIT_IV_LEN /* Should be 0 */
113 + S2N_TLS_CHACHA20_POLY1305_TAG_LEN; /* TAG */
114
115 uint16_t predicted_length = bytes_written;
116 predicted_length += conn->initial->cipher_suite->record_alg->cipher->io.aead.record_iv_size;
117 predicted_length += conn->initial->cipher_suite->record_alg->cipher->io.aead.tag_size;
118 EXPECT_EQUAL(predicted_length, bytes_written + overhead);
119
120 EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA);
121 EXPECT_EQUAL(conn->out.blob.data[1], 3);
122 EXPECT_EQUAL(conn->out.blob.data[2], 3);
123 EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff);
124 EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff);
125
126 /* The data should be encrypted */
127 if (bytes_written > 10) {
128 EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + 5, random_data, bytes_written), 0);
129 }
130
131 /* Copy the encrypted out data to the in data */
132 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
133 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
134 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, 5));
135 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
136
137 /* Let's decrypt it */
138 uint8_t content_type = 0;
139 uint16_t fragment_length = 0;
140 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
141 EXPECT_SUCCESS(s2n_record_parse(conn));
142 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA);
143 EXPECT_EQUAL(fragment_length, predicted_length);
144
145 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
146 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
147
148 /* Start over */
149 EXPECT_SUCCESS(s2n_connection_wipe(conn));
150 conn->actual_protocol_version_established = 1;
151 conn->server_protocol_version = S2N_TLS12;
152 conn->client_protocol_version = S2N_TLS12;
153 conn->actual_protocol_version = S2N_TLS12;
154 EXPECT_SUCCESS(destroy_server_keys(conn));
155 EXPECT_SUCCESS(setup_server_keys(conn, &chacha20_poly1305_key));
156 EXPECT_OK(s2n_record_write(conn, TLS_APPLICATION_DATA, &in));
157
158 /* Now lets corrupt some data and ensure the tests pass */
159 /* Copy the encrypted out data to the in data */
160 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
161 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
162 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, 5));
163 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
164
165 /* Tamper the protocol version in the header, and ensure decryption fails, as we use this in the AAD */
166 EXPECT_EQUAL(conn->header_in.blob.data[0], TLS_APPLICATION_DATA);
167 conn->header_in.blob.data[0] ^= 1; /* Flip a bit in the content_type of the TLS Record Header */
168
169 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
170 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA ^ 1);
171
172 /**
173 * We are trying to test the case when the Additional Authenticated Data in AEAD ciphers is tampered with.
174 *
175 * AEAD Ciphers authenticate several fields, including the TLS Record content_type, so this should fail since
176 * we flipped a bit. See s2n_aead_aad_init() for which fields are added to the additional authenticated data.
177 *
178 * We can't flip the TLS Protocol Version bits here because s2n_record_header_parse() will error before we
179 * attempt decryption with ChaCha because the Protocol version doesn't match "conn->actual_protocol_version".
180 */
181 EXPECT_FAILURE(s2n_record_parse(conn));
182
183 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
184 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
185 POSIX_GUARD_RESULT(conn->initial->cipher_suite->record_alg->cipher->destroy_key(&conn->initial->server_key));
186 POSIX_GUARD_RESULT(conn->initial->cipher_suite->record_alg->cipher->destroy_key(&conn->initial->client_key));
187
188 /* Tamper with the TAG and ensure decryption fails */
189 for (size_t j = 0; j < S2N_TLS_CHACHA20_POLY1305_TAG_LEN; j++) {
190 EXPECT_SUCCESS(s2n_connection_wipe(conn));
191 conn->actual_protocol_version_established = 1;
192 conn->server_protocol_version = S2N_TLS12;
193 conn->client_protocol_version = S2N_TLS12;
194 conn->actual_protocol_version = S2N_TLS12;
195 EXPECT_SUCCESS(destroy_server_keys(conn));
196 EXPECT_SUCCESS(setup_server_keys(conn, &chacha20_poly1305_key));
197 EXPECT_OK(s2n_record_write(conn, TLS_APPLICATION_DATA, &in));
198
199 /* Copy the encrypted out data to the in data */
200 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
201 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
202 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, 5));
203 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
204 conn->in.blob.data[s2n_stuffer_data_available(&conn->in) - j - 1]++;
205 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
206 EXPECT_FAILURE(s2n_record_parse(conn));
207 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA);
208
209 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
210 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
211 POSIX_GUARD_RESULT(conn->initial->cipher_suite->record_alg->cipher->destroy_key(&conn->initial->server_key));
212 POSIX_GUARD_RESULT(conn->initial->cipher_suite->record_alg->cipher->destroy_key(&conn->initial->client_key));
213 }
214
215 /* Tamper with the encrypted payload in the ciphertext and ensure decryption fails */
216 for (size_t j = 0; j < i; j++) {
217 EXPECT_SUCCESS(s2n_connection_wipe(conn));
218 conn->actual_protocol_version_established = 1;
219 conn->server_protocol_version = S2N_TLS12;
220 conn->client_protocol_version = S2N_TLS12;
221 conn->actual_protocol_version = S2N_TLS12;
222 EXPECT_SUCCESS(destroy_server_keys(conn));
223 EXPECT_SUCCESS(setup_server_keys(conn, &chacha20_poly1305_key));
224 EXPECT_OK(s2n_record_write(conn, TLS_APPLICATION_DATA, &in));
225
226 /* Copy the encrypted out data to the in data */
227 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
228 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
229 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, 5));
230 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
231 conn->in.blob.data[j]++;
232 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
233 EXPECT_FAILURE(s2n_record_parse(conn));
234 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA);
235
236 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
237 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
238 POSIX_GUARD_RESULT(conn->initial->cipher_suite->record_alg->cipher->destroy_key(&conn->initial->server_key));
239 POSIX_GUARD_RESULT(conn->initial->cipher_suite->record_alg->cipher->destroy_key(&conn->initial->client_key));
240 }
241 }
242
243 EXPECT_SUCCESS(destroy_server_keys(conn));
244 EXPECT_SUCCESS(s2n_connection_free(conn));
245 END_TEST();
246 }