unit-tests: Added ntru entropy, retransmission and ciphertext tests
[strongswan.git] / src / libstrongswan / tests / suites / test_ntru.c
1 /*
2 * Copyright (C) 2013 Andreas Steffen
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
18 #include <plugins/ntru/ntru_plugin.h>
19
20 /**
21 * NTRU parameter sets to test
22 */
23 static struct {
24 diffie_hellman_group_t group;
25 char *group_name;
26 } params[] = {
27 { NTRU_112_BIT, "NTRU_112" },
28 { NTRU_128_BIT, "NTRU_128" },
29 { NTRU_192_BIT, "NTRU_192" },
30 { NTRU_256_BIT, "NTRU_256" }
31 };
32
33 /**
34 * NTRU parameter set selection
35 */
36 char *parameter_sets[] = {
37 "x9_98_speed", "x9_98_bandwidth", "x9_98_balance", "optimum"
38 };
39
40 START_TEST(test_ntru_entropy)
41 {
42 ck_assert(!ntru_plugin_get_entropy(GET_NUM_BYTES_PER_BYTE_OF_ENTROPY, NULL));
43 ck_assert(!ntru_plugin_get_entropy(GET_BYTE_OF_ENTROPY, NULL));
44 ck_assert(!ntru_plugin_get_entropy(10, NULL));
45 }
46 END_TEST
47
48 START_TEST(test_ntru_ke)
49 {
50 chunk_t pub_key, cipher_text, i_shared_secret, r_shared_secret;
51 diffie_hellman_t *i_ntru, *r_ntru;
52 char buf[10];
53 int n, len;
54 status_t status;
55
56 len = snprintf(buf, sizeof(buf), "%N", diffie_hellman_group_names,
57 params[_i].group);
58 ck_assert(len == 8);
59 ck_assert(streq(buf, params[_i].group_name));
60
61 for (n = 0; n < countof(parameter_sets); n++)
62 {
63 lib->settings->set_str(lib->settings,
64 "libstrongswan.plugins.ntru.parameter_set",
65 parameter_sets[n]);
66
67 i_ntru = lib->crypto->create_dh(lib->crypto, params[_i].group);
68 ck_assert(i_ntru != NULL);
69 ck_assert(i_ntru->get_dh_group(i_ntru) == params[_i].group);
70
71 i_ntru->get_my_public_value(i_ntru, &pub_key);
72 ck_assert(pub_key.len > 0);
73
74 r_ntru = lib->crypto->create_dh(lib->crypto, params[_i].group);
75 ck_assert(r_ntru != NULL);
76
77 r_ntru->set_other_public_value(r_ntru, pub_key);
78 r_ntru->get_my_public_value(r_ntru, &cipher_text);
79 ck_assert(cipher_text.len > 0);
80
81 status = r_ntru->get_shared_secret(r_ntru, &r_shared_secret);
82 ck_assert(status == SUCCESS);
83 ck_assert(r_shared_secret.len > 0);
84
85 i_ntru->set_other_public_value(i_ntru, cipher_text);
86 status = i_ntru->get_shared_secret(i_ntru, &i_shared_secret);
87
88 if (status == SUCCESS)
89 {
90 ck_assert(chunk_equals(i_shared_secret, r_shared_secret));
91 }
92 else
93 {
94 ck_assert(i_shared_secret.len == 0);
95 }
96
97 chunk_clear(&i_shared_secret);
98 chunk_clear(&r_shared_secret);
99 chunk_free(&pub_key);
100 chunk_free(&cipher_text);
101 i_ntru->destroy(i_ntru);
102 r_ntru->destroy(r_ntru);
103 }
104 }
105 END_TEST
106
107 START_TEST(test_ntru_retransmission)
108 {
109 diffie_hellman_t *i_ntru;
110 chunk_t pub_key1, pub_key2;
111
112 i_ntru = lib->crypto->create_dh(lib->crypto, NTRU_256_BIT);
113 i_ntru->get_my_public_value(i_ntru, &pub_key1);
114 i_ntru->get_my_public_value(i_ntru, &pub_key2);
115 ck_assert(chunk_equals(pub_key1, pub_key2));
116
117 chunk_free(&pub_key1);
118 chunk_free(&pub_key2);
119 i_ntru->destroy(i_ntru);
120 }
121 END_TEST
122
123 START_TEST(test_ntru_pubkey_oid)
124 {
125 chunk_t test[] = {
126 chunk_empty,
127 chunk_from_chars(0x00),
128 chunk_from_chars(0x01),
129 chunk_from_chars(0x02),
130 chunk_from_chars(0x02, 0x03, 0x00, 0x03, 0x10),
131 chunk_from_chars(0x01, 0x04, 0x00, 0x03, 0x10),
132 chunk_from_chars(0x01, 0x03, 0x00, 0x03, 0x10),
133 chunk_from_chars(0x01, 0x03, 0xff, 0x03, 0x10),
134 };
135
136 diffie_hellman_t *r_ntru;
137 chunk_t cipher_text;
138 int i;
139
140 for (i = 0; i < countof(test); i++)
141 {
142 r_ntru = lib->crypto->create_dh(lib->crypto, NTRU_128_BIT);
143 r_ntru->set_other_public_value(r_ntru, test[i]);
144 r_ntru->get_my_public_value(r_ntru, &cipher_text);
145 ck_assert(cipher_text.len == 0);
146 r_ntru->destroy(r_ntru);
147 }
148 }
149 END_TEST
150
151 START_TEST(test_ntru_wrong_set)
152 {
153 diffie_hellman_t *i_ntru, *r_ntru;
154 chunk_t pub_key, cipher_text;
155
156 lib->settings->set_str(lib->settings,
157 "libstrongswan.plugins.ntru.parameter_set",
158 "x9_98_bandwidth");
159 i_ntru = lib->crypto->create_dh(lib->crypto, NTRU_112_BIT);
160 i_ntru->get_my_public_value(i_ntru, &pub_key);
161
162 lib->settings->set_str(lib->settings,
163 "libstrongswan.plugins.ntru.parameter_set",
164 "optimum");
165 r_ntru = lib->crypto->create_dh(lib->crypto, NTRU_112_BIT);
166 r_ntru->set_other_public_value(r_ntru, pub_key);
167 r_ntru->get_my_public_value(r_ntru, &cipher_text);
168 ck_assert(cipher_text.len == 0);
169
170 chunk_free(&pub_key);
171 chunk_free(&cipher_text);
172 i_ntru->destroy(i_ntru);
173 r_ntru->destroy(r_ntru);
174 }
175 END_TEST
176
177 START_TEST(test_ntru_ciphertext)
178 {
179 chunk_t test[] = {
180 chunk_empty,
181 chunk_from_chars(0x00),
182 };
183
184 diffie_hellman_t *i_ntru;
185 chunk_t pub_key, shared_secret;
186 int i;
187
188 for (i = 0; i < countof(test); i++)
189 {
190 i_ntru = lib->crypto->create_dh(lib->crypto, NTRU_128_BIT);
191 i_ntru->get_my_public_value(i_ntru, &pub_key);
192 i_ntru->set_other_public_value(i_ntru, test[i]);
193 ck_assert(i_ntru->get_shared_secret(i_ntru, &shared_secret) != SUCCESS);
194 ck_assert(shared_secret.len == 0);
195
196 chunk_free(&pub_key);
197 i_ntru->destroy(i_ntru);
198 }
199 }
200 END_TEST
201
202 Suite *ntru_suite_create()
203 {
204 Suite *s;
205 TCase *tc;
206
207 s = suite_create("ntru");
208
209 tc = tcase_create("entropy");
210 tcase_add_test(tc, test_ntru_entropy);
211 suite_add_tcase(s, tc);
212
213 tc = tcase_create("ke");
214 tcase_add_loop_test(tc, test_ntru_ke, 0, countof(params));
215 suite_add_tcase(s, tc);
216
217 tc = tcase_create("retransmission");
218 tcase_add_test(tc, test_ntru_retransmission);
219 suite_add_tcase(s, tc);
220
221 tc = tcase_create("pubkey_oid");
222 tcase_add_test(tc, test_ntru_pubkey_oid);
223 suite_add_tcase(s, tc);
224
225 tc = tcase_create("wrong_set");
226 tcase_add_test(tc, test_ntru_wrong_set);
227 suite_add_tcase(s, tc);
228
229 tc = tcase_create("ciphertext");
230 tcase_add_test(tc, test_ntru_ciphertext);
231 suite_add_tcase(s, tc);
232
233 return s;
234 }