]>
Commit | Line | Data |
---|---|---|
aae95101 | 1 | /* |
d9c9b7b8 | 2 | * Copyright (C) 2016-2018 Tobias Brunner |
aae95101 TB |
3 | * HSR Hochschule fuer Technik Rapperswil |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2 of the License, or (at your | |
8 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | * for more details. | |
14 | */ | |
15 | ||
16 | #include "test_suite.h" | |
17 | ||
2307bffe | 18 | #include <crypto/proposal/proposal.h> |
aae95101 | 19 | |
9e5065d8 TB |
20 | static struct { |
21 | protocol_id_t proto; | |
22 | char *proposal; | |
23 | char *expected; | |
24 | } create_data[] = { | |
25 | { PROTO_IKE, "", NULL }, | |
26 | { PROTO_IKE, "sha256", NULL }, | |
27 | { PROTO_IKE, "sha256-modp3072", NULL }, | |
28 | { PROTO_IKE, "null-sha256-modp3072", "IKE:NULL/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_3072" }, | |
29 | { PROTO_IKE, "aes128", NULL }, | |
30 | { PROTO_IKE, "aes128-sha256", NULL }, | |
31 | { PROTO_IKE, "aes128-sha256-modpnone", NULL }, | |
ee019ab3 TB |
32 | { PROTO_IKE, "aes128-prfsha256", NULL }, |
33 | { PROTO_IKE, "aes128-prfsha256-modp2048", NULL }, | |
9e5065d8 TB |
34 | { PROTO_IKE, "aes128-sha256-modp3072", "IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_3072" }, |
35 | { PROTO_IKE, "aes128-sha256-prfsha384-modp3072", "IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_384/MODP_3072" }, | |
36 | { PROTO_IKE, "aes128gcm16-modp3072", NULL }, | |
37 | { PROTO_IKE, "aes128gcm16-prfsha256-modp3072", "IKE:AES_GCM_16_128/PRF_HMAC_SHA2_256/MODP_3072" }, | |
38 | { PROTO_IKE, "aes128gcm16-sha256-modp3072", "IKE:AES_GCM_16_128/PRF_HMAC_SHA2_256/MODP_3072" }, | |
39 | { PROTO_IKE, "aes128gcm16-aes128-modp3072", NULL }, | |
40 | { PROTO_IKE, "aes128gcm16-aes128-sha256-modp3072", NULL }, | |
41 | { PROTO_ESP, "", NULL }, | |
42 | { PROTO_ESP, "sha256", NULL }, | |
43 | { PROTO_ESP, "aes128-sha256", "ESP:AES_CBC_128/HMAC_SHA2_256_128/NO_EXT_SEQ" }, | |
44 | { PROTO_ESP, "aes128-sha256-esn", "ESP:AES_CBC_128/HMAC_SHA2_256_128/EXT_SEQ" }, | |
45 | { PROTO_ESP, "aes128-sha256-noesn", "ESP:AES_CBC_128/HMAC_SHA2_256_128/NO_EXT_SEQ" }, | |
46 | { PROTO_ESP, "aes128-sha256-esn-noesn", "ESP:AES_CBC_128/HMAC_SHA2_256_128/EXT_SEQ/NO_EXT_SEQ" }, | |
47 | { PROTO_ESP, "aes128-sha256-prfsha256-modp3072", "ESP:AES_CBC_128/HMAC_SHA2_256_128/MODP_3072/NO_EXT_SEQ" }, | |
48 | { PROTO_ESP, "aes128gcm16-aes128-sha256-modp3072", NULL }, | |
49 | { PROTO_ESP, "aes128gmac", "ESP:NULL_AES_GMAC_128/NO_EXT_SEQ" }, | |
50 | { PROTO_AH, "", NULL }, | |
51 | { PROTO_AH, "aes128", NULL }, | |
52 | { PROTO_AH, "aes128-sha256", "AH:HMAC_SHA2_256_128/NO_EXT_SEQ" }, | |
53 | { PROTO_AH, "sha256-sha1", "AH:HMAC_SHA2_256_128/HMAC_SHA1_96/NO_EXT_SEQ" }, | |
54 | { PROTO_AH, "aes128gmac-sha256", "AH:AES_128_GMAC/HMAC_SHA2_256_128/NO_EXT_SEQ" }, | |
55 | { PROTO_AH, "aes128gmac-sha256-prfsha256", "AH:AES_128_GMAC/HMAC_SHA2_256_128/NO_EXT_SEQ" }, | |
56 | { PROTO_AH, "aes128gmac-aes256gmac-aes128-sha256", "AH:AES_128_GMAC/AES_256_GMAC/HMAC_SHA2_256_128/NO_EXT_SEQ" }, | |
57 | { PROTO_AH, "sha256-esn", "AH:HMAC_SHA2_256_128/EXT_SEQ" }, | |
58 | { PROTO_AH, "sha256-noesn", "AH:HMAC_SHA2_256_128/NO_EXT_SEQ" }, | |
59 | { PROTO_AH, "sha256-esn-noesn", "AH:HMAC_SHA2_256_128/EXT_SEQ/NO_EXT_SEQ" }, | |
60 | }; | |
61 | ||
d9c9b7b8 | 62 | static void assert_proposal_eq(proposal_t *proposal, char *expected) |
9e5065d8 | 63 | { |
9e5065d8 TB |
64 | char str[BUF_LEN]; |
65 | ||
d9c9b7b8 | 66 | if (!expected) |
9e5065d8 TB |
67 | { |
68 | ck_assert(!proposal); | |
69 | return; | |
70 | } | |
71 | snprintf(str, sizeof(str), "%P", proposal); | |
d9c9b7b8 TB |
72 | ck_assert_str_eq(expected, str); |
73 | } | |
74 | ||
75 | START_TEST(test_create_from_string) | |
76 | { | |
77 | proposal_t *proposal; | |
78 | ||
79 | proposal = proposal_create_from_string(create_data[_i].proto, | |
80 | create_data[_i].proposal); | |
81 | assert_proposal_eq(proposal, create_data[_i].expected); | |
82 | DESTROY_IF(proposal); | |
9e5065d8 TB |
83 | } |
84 | END_TEST | |
85 | ||
aae95101 | 86 | static struct { |
9b191d59 | 87 | protocol_id_t proto; |
aae95101 TB |
88 | char *self; |
89 | char *other; | |
90 | char *expected; | |
91 | } select_data[] = { | |
9b191d59 TB |
92 | { PROTO_ESP, "aes128", "aes128", "aes128" }, |
93 | { PROTO_ESP, "aes128", "aes256", NULL }, | |
94 | { PROTO_ESP, "aes128-aes256", "aes256-aes128", "aes128" }, | |
95 | { PROTO_ESP, "aes256-aes128", "aes128-aes256", "aes256" }, | |
96 | { PROTO_ESP, "aes128-aes256-sha1-sha256", "aes256-aes128-sha256-sha1", "aes128-sha1" }, | |
97 | { PROTO_ESP, "aes256-aes128-sha256-sha1", "aes128-aes256-sha1-sha256", "aes256-sha256" }, | |
98 | { PROTO_ESP, "aes128-sha256-modp3072", "aes128-sha256", NULL }, | |
99 | { PROTO_ESP, "aes128-sha256", "aes128-sha256-modp3072", NULL }, | |
100 | { PROTO_ESP, "aes128-sha256-modp3072", "aes128-sha256-modpnone", NULL }, | |
101 | { PROTO_ESP, "aes128-sha256-modpnone", "aes128-sha256-modp3072", NULL }, | |
102 | { PROTO_ESP, "aes128-sha256-modp3072-modpnone", "aes128-sha256", "aes128-sha256" }, | |
103 | { PROTO_ESP, "aes128-sha256", "aes128-sha256-modp3072-modpnone", "aes128-sha256" }, | |
104 | { PROTO_ESP, "aes128-sha256-modp3072-modpnone", "aes128-sha256-modpnone-modp3072", "aes128-sha256-modp3072" }, | |
105 | { PROTO_ESP, "aes128-sha256-modpnone-modp3072", "aes128-sha256-modp3072-modpnone", "aes128-sha256-modpnone" }, | |
9b191d59 TB |
106 | { PROTO_IKE, "aes128-sha256-modp3072", "aes128-sha256-modp3072", "aes128-sha256-modp3072" }, |
107 | { PROTO_IKE, "aes128-sha256-modp3072", "aes128-sha256-modp3072-modpnone", "aes128-sha256-modp3072" }, | |
108 | { PROTO_IKE, "aes128-sha256-modp3072-modpnone", "aes128-sha256-modp3072", "aes128-sha256-modp3072" }, | |
aae95101 TB |
109 | }; |
110 | ||
111 | START_TEST(test_select) | |
112 | { | |
113 | proposal_t *self, *other, *selected, *expected; | |
114 | ||
9b191d59 | 115 | self = proposal_create_from_string(select_data[_i].proto, |
aae95101 | 116 | select_data[_i].self); |
9b191d59 | 117 | other = proposal_create_from_string(select_data[_i].proto, |
aae95101 | 118 | select_data[_i].other); |
22f13dce | 119 | selected = self->select(self, other, TRUE, FALSE); |
aae95101 TB |
120 | if (select_data[_i].expected) |
121 | { | |
9b191d59 | 122 | expected = proposal_create_from_string(select_data[_i].proto, |
aae95101 TB |
123 | select_data[_i].expected); |
124 | ck_assert(selected); | |
125 | ck_assert_msg(expected->equals(expected, selected), "proposal %P does " | |
126 | "not match expected %P", selected, expected); | |
127 | expected->destroy(expected); | |
128 | } | |
129 | else | |
130 | { | |
131 | ck_assert(!selected); | |
132 | } | |
133 | DESTROY_IF(selected); | |
134 | other->destroy(other); | |
135 | self->destroy(self); | |
136 | } | |
137 | END_TEST | |
138 | ||
22f13dce TB |
139 | START_TEST(test_select_spi) |
140 | { | |
141 | proposal_t *self, *other, *selected; | |
142 | ||
143 | self = proposal_create_from_string(PROTO_ESP, "aes128-sha256-modp3072"); | |
144 | other = proposal_create_from_string(PROTO_ESP, "aes128-sha256-modp3072"); | |
145 | other->set_spi(other, 0x12345678); | |
146 | ||
147 | selected = self->select(self, other, TRUE, FALSE); | |
148 | ck_assert(selected); | |
149 | ck_assert_int_eq(selected->get_spi(selected), other->get_spi(other)); | |
150 | selected->destroy(selected); | |
151 | ||
152 | selected = self->select(self, other, FALSE, FALSE); | |
153 | ck_assert(selected); | |
154 | ck_assert_int_eq(selected->get_spi(selected), self->get_spi(self)); | |
155 | selected->destroy(selected); | |
156 | ||
157 | other->destroy(other); | |
158 | self->destroy(self); | |
159 | } | |
160 | END_TEST | |
161 | ||
d9c9b7b8 TB |
162 | START_TEST(test_promote_dh_group) |
163 | { | |
164 | proposal_t *proposal; | |
165 | ||
166 | proposal = proposal_create_from_string(PROTO_IKE, | |
167 | "aes128-sha256-modp3072-ecp256"); | |
168 | ck_assert(proposal->promote_dh_group(proposal, ECP_256_BIT)); | |
169 | assert_proposal_eq(proposal, "IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/ECP_256/MODP_3072"); | |
170 | proposal->destroy(proposal); | |
171 | } | |
172 | END_TEST | |
173 | ||
174 | START_TEST(test_promote_dh_group_already_front) | |
175 | { | |
176 | proposal_t *proposal; | |
177 | ||
178 | proposal = proposal_create_from_string(PROTO_IKE, | |
179 | "aes128-sha256-modp3072-ecp256"); | |
180 | ck_assert(proposal->promote_dh_group(proposal, MODP_3072_BIT)); | |
181 | assert_proposal_eq(proposal, "IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_3072/ECP_256"); | |
182 | proposal->destroy(proposal); | |
183 | } | |
184 | END_TEST | |
185 | ||
186 | START_TEST(test_promote_dh_group_not_contained) | |
187 | { | |
188 | proposal_t *proposal; | |
189 | ||
190 | proposal = proposal_create_from_string(PROTO_IKE, | |
191 | "aes128-sha256-modp3072-ecp256"); | |
192 | ||
193 | ck_assert(!proposal->promote_dh_group(proposal, MODP_2048_BIT)); | |
194 | assert_proposal_eq(proposal, "IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_3072/ECP_256"); | |
195 | proposal->destroy(proposal); | |
196 | } | |
197 | END_TEST | |
198 | ||
5eb094df TB |
199 | START_TEST(test_unknown_transform_types_print) |
200 | { | |
201 | proposal_t *proposal; | |
202 | ||
203 | proposal = proposal_create(PROTO_IKE, 0); | |
204 | proposal->add_algorithm(proposal, 242, 42, 128); | |
205 | assert_proposal_eq(proposal, "IKE:UNKNOWN_242_42_128"); | |
206 | proposal->destroy(proposal); | |
207 | ||
208 | proposal = proposal_create_from_string(PROTO_IKE, | |
76c7c951 | 209 | "aes128-sha256-ecp256"); |
5eb094df TB |
210 | proposal->add_algorithm(proposal, 242, 42, 128); |
211 | proposal->add_algorithm(proposal, 243, 1, 0); | |
76c7c951 | 212 | assert_proposal_eq(proposal, "IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/ECP_256/UNKNOWN_242_42_128/UNKNOWN_243_1"); |
5eb094df TB |
213 | proposal->destroy(proposal); |
214 | } | |
215 | END_TEST | |
216 | ||
6b8749ab TB |
217 | START_TEST(test_unknown_transform_types_equals) |
218 | { | |
219 | proposal_t *self, *other; | |
220 | ||
221 | self = proposal_create_from_string(PROTO_IKE, "aes128-sha256-ecp256"); | |
222 | other = proposal_create_from_string(PROTO_IKE, "aes128-sha256-ecp256"); | |
223 | other->add_algorithm(other, 242, 42, 0); | |
224 | ck_assert(!self->equals(self, other)); | |
225 | ck_assert(!other->equals(other, self)); | |
226 | self->add_algorithm(self, 242, 42, 0); | |
227 | ck_assert(self->equals(self, other)); | |
228 | ck_assert(other->equals(other, self)); | |
229 | other->destroy(other); | |
230 | self->destroy(self); | |
231 | } | |
232 | END_TEST | |
233 | ||
76c7c951 TB |
234 | START_TEST(test_unknown_transform_types_select_fail) |
235 | { | |
236 | proposal_t *self, *other, *selected; | |
237 | ||
238 | self = proposal_create_from_string(PROTO_IKE, "aes128-sha256-ecp256"); | |
239 | other = proposal_create_from_string(PROTO_IKE, "aes128-sha256-ecp256"); | |
240 | other->add_algorithm(other, 242, 42, 0); | |
241 | ||
242 | selected = self->select(self, other, TRUE, FALSE); | |
243 | ck_assert(!selected); | |
244 | other->destroy(other); | |
245 | self->destroy(self); | |
246 | } | |
247 | END_TEST | |
248 | ||
249 | START_TEST(test_unknown_transform_types_select_fail_subtype) | |
250 | { | |
251 | proposal_t *self, *other, *selected; | |
252 | ||
253 | self = proposal_create_from_string(PROTO_IKE, "aes128-sha256-ecp256"); | |
254 | self->add_algorithm(self, 242, 8, 0); | |
255 | other = proposal_create_from_string(PROTO_IKE, "aes128-sha256-ecp256"); | |
256 | other->add_algorithm(other, 242, 42, 0); | |
257 | ||
258 | selected = self->select(self, other, TRUE, FALSE); | |
259 | ck_assert(!selected); | |
260 | other->destroy(other); | |
261 | self->destroy(self); | |
262 | } | |
263 | END_TEST | |
264 | ||
265 | START_TEST(test_unknown_transform_types_select_success) | |
266 | { | |
267 | proposal_t *self, *other, *selected; | |
268 | ||
269 | self = proposal_create_from_string(PROTO_IKE, "aes128-sha256-ecp256"); | |
270 | self->add_algorithm(self, 242, 42, 128); | |
271 | other = proposal_create_from_string(PROTO_IKE, "aes128-sha256-ecp256"); | |
272 | other->add_algorithm(other, 242, 42, 128); | |
273 | other->add_algorithm(other, 242, 1, 0); | |
274 | ||
275 | selected = self->select(self, other, TRUE, FALSE); | |
276 | ck_assert(selected); | |
277 | assert_proposal_eq(selected, "IKE:AES_CBC_128/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/ECP_256/UNKNOWN_242_42_128"); | |
278 | selected->destroy(selected); | |
279 | other->destroy(other); | |
280 | self->destroy(self); | |
281 | } | |
282 | END_TEST | |
283 | ||
5a7b0be2 TB |
284 | START_TEST(test_chacha20_poly1305_key_length) |
285 | { | |
286 | proposal_t *proposal; | |
287 | uint16_t alg, ks; | |
288 | ||
289 | proposal = proposal_create_from_string(PROTO_IKE, "chacha20poly1305-prfsha256-ecp256"); | |
290 | proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &alg, &ks); | |
291 | ck_assert_int_eq(alg, ENCR_CHACHA20_POLY1305); | |
292 | ck_assert_int_eq(ks, 0); | |
293 | assert_proposal_eq(proposal, "IKE:CHACHA20_POLY1305/PRF_HMAC_SHA2_256/ECP_256"); | |
294 | proposal->destroy(proposal); | |
295 | } | |
296 | END_TEST | |
76c7c951 TB |
297 | |
298 | ||
aae95101 TB |
299 | Suite *proposal_suite_create() |
300 | { | |
301 | Suite *s; | |
302 | TCase *tc; | |
303 | ||
304 | s = suite_create("proposal"); | |
305 | ||
9e5065d8 TB |
306 | tc = tcase_create("create_from_string"); |
307 | tcase_add_loop_test(tc, test_create_from_string, 0, countof(create_data)); | |
308 | suite_add_tcase(s, tc); | |
309 | ||
aae95101 TB |
310 | tc = tcase_create("select"); |
311 | tcase_add_loop_test(tc, test_select, 0, countof(select_data)); | |
22f13dce | 312 | tcase_add_test(tc, test_select_spi); |
aae95101 TB |
313 | suite_add_tcase(s, tc); |
314 | ||
d9c9b7b8 TB |
315 | tc = tcase_create("promote_dh_group"); |
316 | tcase_add_test(tc, test_promote_dh_group); | |
317 | tcase_add_test(tc, test_promote_dh_group_already_front); | |
318 | tcase_add_test(tc, test_promote_dh_group_not_contained); | |
319 | suite_add_tcase(s, tc); | |
320 | ||
5eb094df TB |
321 | tc = tcase_create("unknown transform types"); |
322 | tcase_add_test(tc, test_unknown_transform_types_print); | |
6b8749ab | 323 | tcase_add_test(tc, test_unknown_transform_types_equals); |
76c7c951 TB |
324 | tcase_add_test(tc, test_unknown_transform_types_select_fail); |
325 | tcase_add_test(tc, test_unknown_transform_types_select_fail_subtype); | |
326 | tcase_add_test(tc, test_unknown_transform_types_select_success); | |
5eb094df TB |
327 | suite_add_tcase(s, tc); |
328 | ||
5a7b0be2 TB |
329 | tc = tcase_create("chacha20/poly1305"); |
330 | tcase_add_test(tc, test_chacha20_poly1305_key_length); | |
331 | suite_add_tcase(s, tc); | |
332 | ||
aae95101 TB |
333 | return s; |
334 | } |