Debian Codegraph @

crt/s2n/tests/unit/s2n_prf_key_material_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 "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18 #include "tls/s2n_prf.h"
19 #include "utils/s2n_random.h"
20
21 static S2N_RESULT s2n_test_validate_key_material(struct s2n_key_material *key_material,
22 struct s2n_blob *test_data_blob, uint8_t mac_size, uint8_t key_size, uint8_t iv_size)
23 {
24 /* confirm that the data is copied to key_material */
25 RESULT_ENSURE_EQ(test_data_blob->size, sizeof(key_material->key_block));
26 RESULT_ENSURE_EQ(memcmp(test_data_blob->data, key_material->key_block, test_data_blob->size), 0);
27
28 struct s2n_stuffer test_data_stuffer = { 0 };
29 RESULT_GUARD_POSIX(s2n_stuffer_init_written(&test_data_stuffer, test_data_blob));
30
31 /* test that its possible to access data from s2n_key_material */
32 /* client MAC */
33 uint8_t *test_ptr = s2n_stuffer_raw_read(&test_data_stuffer, mac_size);
34 RESULT_ENSURE_REF(test_ptr);
35 RESULT_ENSURE_EQ(memcmp(test_ptr, key_material->client_mac.data, mac_size), 0);
36 /* server MAC */
37 test_ptr = s2n_stuffer_raw_read(&test_data_stuffer, mac_size);
38 RESULT_ENSURE_REF(test_ptr);
39 RESULT_ENSURE_EQ(memcmp(test_ptr, key_material->server_mac.data, mac_size), 0);
40
41 /* client KEY */
42 test_ptr = s2n_stuffer_raw_read(&test_data_stuffer, key_size);
43 RESULT_ENSURE_REF(test_ptr);
44 RESULT_ENSURE_EQ(memcmp(test_ptr, key_material->client_key.data, key_size), 0);
45 /* server KEY */
46 test_ptr = s2n_stuffer_raw_read(&test_data_stuffer, key_size);
47 RESULT_ENSURE_REF(test_ptr);
48 RESULT_ENSURE_EQ(memcmp(test_ptr, key_material->server_key.data, key_size), 0);
49
50 /* client IV */
51 test_ptr = s2n_stuffer_raw_read(&test_data_stuffer, iv_size);
52 RESULT_ENSURE_REF(test_ptr);
53 RESULT_ENSURE_EQ(memcmp(test_ptr, key_material->client_iv.data, iv_size), 0);
54 /* server IV */
55 test_ptr = s2n_stuffer_raw_read(&test_data_stuffer, iv_size);
56 RESULT_ENSURE_REF(test_ptr);
57 RESULT_ENSURE_EQ(memcmp(test_ptr, key_material->server_iv.data, iv_size), 0);
58
59 return S2N_RESULT_OK;
60 }
61
62 int main(int argc, char **argv)
63 {
64 BEGIN_TEST();
65
66 /* prepare test data */
67 uint8_t test_data[S2N_MAX_KEY_BLOCK_LEN] = { 0 };
68 struct s2n_blob test_data_blob = { 0 };
69 EXPECT_SUCCESS(s2n_blob_init(&test_data_blob, test_data, sizeof(test_data)));
70 EXPECT_OK(s2n_get_public_random_data(&test_data_blob));
71
72 /* fuzz s2n_key_material_init with different mac, key, iv sizes */
73 {
74 for (uint8_t mac_size = 0; mac_size < SHA512_DIGEST_LENGTH; mac_size++) {
75 for (uint8_t key_size = 0; key_size < S2N_TLS_AES_256_GCM_KEY_LEN; key_size++) {
76 for (uint8_t iv_size = 0; iv_size < S2N_TLS_MAX_IV_LEN; iv_size++) {
77 if ((mac_size * 2 + key_size * 2 + iv_size * 2 > S2N_MAX_KEY_BLOCK_LEN)) {
78 continue;
79 }
80
81 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
82 s2n_connection_ptr_free);
83
84 /* test varying size of mac, key and iv */
85 conn->actual_protocol_version = S2N_TLS10;
86 struct s2n_cipher temp_cipher = {
87 .type = S2N_COMPOSITE,
88 .key_material_size = key_size,
89 .io.comp = {
90 /* interpreted as iv size for composite ciphers.. which makes
91 * it easy for testing */
92 .block_size = iv_size,
93 .mac_key_size = mac_size,
94 },
95 };
96 struct s2n_record_algorithm temp_record_alg = {
97 .cipher = &temp_cipher,
98 };
99 struct s2n_cipher_suite temp_cipher_suite = {
100 .record_alg = &temp_record_alg,
101 };
102 conn->secure->cipher_suite = &temp_cipher_suite;
103
104 /* init s2n_key_material */
105 struct s2n_key_material key_material = { 0 };
106 EXPECT_OK(s2n_key_material_init(&key_material, conn));
107
108 /* assert that sizes match */
109 EXPECT_EQUAL(key_material.client_mac.size, mac_size);
110 EXPECT_EQUAL(key_material.client_key.size, key_size);
111 EXPECT_EQUAL(key_material.client_iv.size, iv_size);
112 EXPECT_EQUAL(key_material.server_mac.size, mac_size);
113 EXPECT_EQUAL(key_material.server_key.size, key_size);
114 EXPECT_EQUAL(key_material.server_iv.size, iv_size);
115
116 /* copy data into key_material and validate accessing key_material is sound */
117 EXPECT_EQUAL(sizeof(key_material.key_block), sizeof(test_data));
118 POSIX_CHECKED_MEMCPY(key_material.key_block, test_data, sizeof(key_material.key_block));
119 EXPECT_OK(s2n_test_validate_key_material(&key_material, &test_data_blob, mac_size, key_size, iv_size));
120 }
121 }
122 }
123 }
124
125 /* confirm that s2n_key_material can correctly handle all cipher suites */
126 {
127 for (size_t i = 0; i < cipher_preferences_test_all.count; i++) {
128 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
129 s2n_connection_ptr_free);
130
131 struct s2n_cipher_suite *cipher_suite = cipher_preferences_test_all.suites[i];
132 if (!cipher_suite->available) {
133 continue;
134 }
135 conn->secure->cipher_suite = cipher_suite;
136
137 /* init s2n_key_material */
138 struct s2n_key_material key_material = { 0 };
139 EXPECT_OK(s2n_key_material_init(&key_material, conn));
140
141 /* copy data into key_material and validate accessing key_material is sound */
142 EXPECT_EQUAL(sizeof(key_material.key_block), sizeof(test_data));
143 POSIX_CHECKED_MEMCPY(key_material.key_block, test_data, sizeof(key_material.key_block));
144 /* test that its possible to access mac, key and iv correctly from s2n_key_material */
145 EXPECT_OK(s2n_test_validate_key_material(
146 &key_material,
147 &test_data_blob,
148 key_material.client_mac.size,
149 key_material.client_key.size,
150 key_material.client_iv.size));
151 }
152 }
153
154 /* AEAD cipher
155 * IV size should be the same regardless of protocol version
156 */
157 {
158 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
159 s2n_connection_ptr_free);
160 conn->secure->cipher_suite = &s2n_tls13_aes_128_gcm_sha256;
161 EXPECT_EQUAL(conn->secure->cipher_suite->record_alg->cipher->type, S2N_AEAD);
162
163 struct s2n_key_material key_material = { 0 };
164
165 uint32_t mac = 0;
166 uint32_t key = S2N_TLS_AES_128_GCM_KEY_LEN;
167 uint32_t iv = S2N_TLS13_FIXED_IV_LEN;
168
169 /* initialize s2n_key_material */
170 conn->actual_protocol_version = S2N_TLS10;
171 EXPECT_OK(s2n_key_material_init(&key_material, conn));
172
173 EXPECT_EQUAL(key_material.client_mac.size, mac);
174 EXPECT_EQUAL(key_material.client_key.size, key);
175 EXPECT_EQUAL(key_material.client_iv.size, iv);
176 EXPECT_EQUAL(key_material.server_mac.size, mac);
177 EXPECT_EQUAL(key_material.server_key.size, key);
178 EXPECT_EQUAL(key_material.server_iv.size, iv);
179
180 /* re-initialize s2n_key_material */
181 conn->actual_protocol_version = S2N_TLS11;
182 EXPECT_OK(s2n_key_material_init(&key_material, conn));
183 /* assert same IV size regardless of protocol version */
184 EXPECT_EQUAL(key_material.client_mac.size, mac);
185 EXPECT_EQUAL(key_material.client_key.size, key);
186 EXPECT_EQUAL(key_material.client_iv.size, iv);
187 EXPECT_EQUAL(key_material.server_mac.size, mac);
188 EXPECT_EQUAL(key_material.server_key.size, key);
189 EXPECT_EQUAL(key_material.server_iv.size, iv);
190 }
191
192 /* NON AEAD cipher
193 * IV size depends on protocol version
194 */
195 {
196 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT),
197 s2n_connection_ptr_free);
198
199 conn->secure->cipher_suite = &s2n_rsa_with_aes_128_cbc_sha256;
200 const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
201 /* assert that the cipher chosen is non AEAD */
202 EXPECT_TRUE(cipher->type == S2N_COMPOSITE || cipher->type == S2N_CBC);
203
204 struct s2n_key_material key_material = { 0 };
205
206 uint32_t mac = SHA256_DIGEST_LENGTH;
207 uint32_t key = S2N_TLS_AES_128_GCM_KEY_LEN;
208 uint32_t iv = 16;
209
210 /* initialize s2n_key_material */
211 conn->actual_protocol_version = S2N_TLS10;
212 EXPECT_OK(s2n_key_material_init(&key_material, conn));
213 /* assert IV of non 0 if protocol version <= S2N_TLS10 */
214 EXPECT_EQUAL(key_material.client_mac.size, mac);
215 EXPECT_EQUAL(key_material.client_key.size, key);
216 EXPECT_EQUAL(key_material.client_iv.size, iv);
217 EXPECT_EQUAL(key_material.server_mac.size, mac);
218 EXPECT_EQUAL(key_material.server_key.size, key);
219 EXPECT_EQUAL(key_material.server_iv.size, iv);
220
221 /* re-initialize s2n_key_material */
222 conn->actual_protocol_version = S2N_TLS11;
223 EXPECT_OK(s2n_key_material_init(&key_material, conn));
224 /* assert IV of size == 0 if protocol version > S2N_TLS10 */
225 iv = 0;
226 EXPECT_EQUAL(key_material.client_mac.size, mac);
227 EXPECT_EQUAL(key_material.client_key.size, key);
228 EXPECT_EQUAL(key_material.client_iv.size, iv);
229 EXPECT_EQUAL(key_material.server_mac.size, mac);
230 EXPECT_EQUAL(key_material.server_key.size, key);
231 EXPECT_EQUAL(key_material.server_iv.size, iv);
232 }
233
234 END_TEST();
235 }