2 * Copyright (C) 2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "test_suite.h"
18 #include <crypto/crypto_factory.h>
20 static rng_t
*rng_create(rng_quality_t quality
)
22 rng_quality_t
*q
= malloc_thing(rng_quality_t
);
27 static rng_t
*rng_create_weak(rng_quality_t quality
)
29 ck_assert(quality
== RNG_WEAK
);
30 return rng_create(RNG_WEAK
);
33 static rng_t
*rng_create_strong(rng_quality_t quality
)
35 ck_assert(quality
<= RNG_STRONG
);
36 return rng_create(RNG_STRONG
);
39 static rng_t
*rng_create_true(rng_quality_t quality
)
41 ck_assert(quality
<= RNG_TRUE
);
42 return rng_create(RNG_TRUE
);
45 static rng_t
*rng_create_true_second(rng_quality_t quality
)
47 fail("should never be called");
48 return rng_create(RNG_TRUE
);
51 static rng_quality_t rng_weak
= RNG_WEAK
;
52 static rng_quality_t rng_strong
= RNG_STRONG
;
53 static rng_quality_t rng_true
= RNG_TRUE
;
56 rng_quality_t
*exp_weak
;
57 rng_quality_t
*exp_strong
;
58 rng_quality_t
*exp_true
;
61 rng_constructor_t create
;
67 { &rng_weak
, NULL
, NULL
, {
68 { &rng_weak
, rng_create_weak
},
71 { &rng_strong
, &rng_strong
, NULL
, {
72 { &rng_strong
, rng_create_strong
},
75 { &rng_true
, &rng_true
, &rng_true
, {
76 { &rng_true
, rng_create_true
},
79 { &rng_true
, &rng_true
, &rng_true
, {
80 { &rng_true
, rng_create_true
},
81 { &rng_true
, rng_create_true_second
},
84 { &rng_weak
, &rng_true
, &rng_true
, {
85 { &rng_weak
, rng_create_weak
},
86 { &rng_true
, rng_create_true
},
89 { &rng_weak
, &rng_strong
, &rng_true
, {
90 { &rng_true
, rng_create_true
},
91 { &rng_strong
, rng_create_strong
},
92 { &rng_weak
, rng_create_weak
},
95 { &rng_weak
, &rng_strong
, &rng_true
, {
96 { &rng_weak
, rng_create_weak
},
97 { &rng_strong
, rng_create_strong
},
98 { &rng_true
, rng_create_true
},
103 static void verify_rng(crypto_factory_t
*factory
, rng_quality_t request
,
104 rng_quality_t
*expected
)
108 res
= (rng_quality_t
*)factory
->create_rng(factory
, request
);
116 ck_assert_int_eq(*expected
, *res
);
121 START_TEST(test_create_rng
)
123 crypto_factory_t
*factory
;
126 factory
= crypto_factory_create();
127 for (i
= 0; rng_data
[_i
].data
[i
].q
; i
++)
129 ck_assert(factory
->add_rng(factory
, *rng_data
[_i
].data
[i
].q
, "test",
130 rng_data
[_i
].data
[i
].create
));
132 verify_rng(factory
, RNG_WEAK
, rng_data
[_i
].exp_weak
);
133 verify_rng(factory
, RNG_STRONG
, rng_data
[_i
].exp_strong
);
134 verify_rng(factory
, RNG_TRUE
, rng_data
[_i
].exp_true
);
135 for (i
= 0; rng_data
[_i
].data
[i
].q
; i
++)
137 factory
->remove_rng(factory
, rng_data
[_i
].data
[i
].create
);
139 factory
->destroy(factory
);
143 static diffie_hellman_t
*dh_create(char *plugin
)
145 return (diffie_hellman_t
*)plugin
;
148 static diffie_hellman_t
*dh_create_modp1024(diffie_hellman_group_t group
, ...)
150 ck_assert(group
== MODP_1024_BIT
);
151 return dh_create("plugin1");
154 static diffie_hellman_t
*dh_create_modp1024_second(diffie_hellman_group_t group
,
157 ck_assert(group
== MODP_1024_BIT
);
158 return dh_create("plugin2");
161 static diffie_hellman_t
*dh_create_modp2048(diffie_hellman_group_t group
, ...)
163 ck_assert(group
== MODP_2048_BIT
);
164 return dh_create("plugin1");
167 static diffie_hellman_t
*dh_create_modp2048_second(diffie_hellman_group_t group
,
170 ck_assert(group
== MODP_2048_BIT
);
171 return dh_create("plugin2");
178 diffie_hellman_group_t g
;
179 dh_constructor_t create
;
184 { MODP_NONE
, NULL
, NULL
}
187 { MODP_1024_BIT
, dh_create_modp1024
, "plugin1" },
188 { MODP_NONE
, NULL
, NULL
}
191 { MODP_1024_BIT
, dh_create_modp1024
, "plugin1" },
192 { MODP_1024_BIT
, dh_create_modp1024_second
, "plugin2" },
193 { MODP_NONE
, NULL
, NULL
}
196 { MODP_1024_BIT
, dh_create_modp1024_second
, "plugin2" },
197 { MODP_1024_BIT
, dh_create_modp1024
, "plugin1" },
198 { MODP_NONE
, NULL
, NULL
}
200 { "plugin1", "plugin1", {
201 { MODP_1024_BIT
, dh_create_modp1024
, "plugin1" },
202 { MODP_2048_BIT
, dh_create_modp2048
, "plugin1" },
205 { "plugin1", "plugin1", {
206 { MODP_2048_BIT
, dh_create_modp2048
, "plugin1" },
207 { MODP_1024_BIT
, dh_create_modp1024
, "plugin1" },
210 { "plugin1", "plugin1", {
211 { MODP_2048_BIT
, dh_create_modp2048
, "plugin1" },
212 { MODP_2048_BIT
, dh_create_modp2048_second
, "plugin2" },
213 { MODP_1024_BIT
, dh_create_modp1024
, "plugin1" },
216 { "plugin1", "plugin2", {
217 { MODP_2048_BIT
, dh_create_modp2048_second
, "plugin2" },
218 { MODP_2048_BIT
, dh_create_modp2048
, "plugin1" },
219 { MODP_1024_BIT
, dh_create_modp1024
, "plugin1" },
224 static void verify_dh(crypto_factory_t
*factory
, diffie_hellman_group_t request
,
229 plugin
= (char*)factory
->create_dh(factory
, request
);
237 ck_assert_str_eq(expected
, plugin
);
241 START_TEST(test_create_dh
)
243 enumerator_t
*enumerator
;
244 crypto_factory_t
*factory
;
245 diffie_hellman_group_t group
;
250 factory
= crypto_factory_create();
251 for (i
= 0; dh_data
[_i
].data
[i
].g
!= MODP_NONE
; i
++)
253 ck_assert(factory
->add_dh(factory
, dh_data
[_i
].data
[i
].g
,
254 dh_data
[_i
].data
[i
].plugin
,
255 dh_data
[_i
].data
[i
].create
));
257 verify_dh(factory
, MODP_1024_BIT
, dh_data
[_i
].exp1024
);
258 verify_dh(factory
, MODP_2048_BIT
, dh_data
[_i
].exp2048
);
260 len
= countof(dh_data
[_i
].data
);
261 enumerator
= factory
->create_dh_enumerator(factory
);
262 for (i
= 0; enumerator
->enumerate(enumerator
, &group
, &plugin
) && i
< len
;)
264 ck_assert_int_eq(dh_data
[_i
].data
[i
].g
, group
);
265 while (dh_data
[_i
].data
[i
].g
== group
)
266 { /* skip other entries by the same group */
272 ck_assert(dh_data
[_i
].exp1024
);
273 ck_assert_str_eq(dh_data
[_i
].exp1024
, plugin
);
276 ck_assert(dh_data
[_i
].exp2048
);
277 ck_assert_str_eq(dh_data
[_i
].exp2048
, plugin
);
280 fail("unexpected DH group");
284 ck_assert(!enumerator
->enumerate(enumerator
));
285 ck_assert_int_eq(dh_data
[_i
].data
[i
].g
, MODP_NONE
);
286 enumerator
->destroy(enumerator
);
288 for (i
= 0; dh_data
[_i
].data
[i
].g
!= MODP_NONE
; i
++)
290 factory
->remove_dh(factory
, dh_data
[_i
].data
[i
].create
);
292 factory
->destroy(factory
);
296 Suite
*crypto_factory_suite_create()
301 s
= suite_create("crypto-factory");
303 tc
= tcase_create("create_rng");
304 tcase_add_loop_test(tc
, test_create_rng
, 0, countof(rng_data
));
305 suite_add_tcase(s
, tc
);
307 tc
= tcase_create("create_dh");
308 tcase_add_loop_test(tc
, test_create_dh
, 0, countof(dh_data
));
309 suite_add_tcase(s
, tc
);