Any of the four NTRU parameter sets can be selected
[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 /**
19 * NTRU parameter sets to test
20 */
21 static struct {
22 diffie_hellman_group_t group;
23 char *group_name;
24 } params[] = {
25 { NTRU_112_BIT, "NTRU_112" },
26 { NTRU_128_BIT, "NTRU_128" },
27 { NTRU_192_BIT, "NTRU_192" },
28 { NTRU_256_BIT, "NTRU_256" }
29 };
30
31 /**
32 * NTRU parameter set selection
33 */
34 char *parameter_sets[] = {
35 "x9_98_speed", "x9_98_bandwidth", "x9_98_balance", "optimum"
36 };
37
38 START_TEST(test_ntru_ke)
39 {
40 chunk_t pub_key, cipher_text, i_shared_secret, r_shared_secret;
41 diffie_hellman_t *i_ntru, *r_ntru;
42 char buf[10];
43 int n, len;
44 status_t status;
45
46 len = snprintf(buf, sizeof(buf), "%N", diffie_hellman_group_names,
47 params[_i].group);
48 ck_assert(len == 8);
49 ck_assert(streq(buf, params[_i].group_name));
50
51 for (n = 0; n < countof(parameter_sets); n++)
52 {
53 lib->settings->set_str(lib->settings,
54 "libstrongswan.plugins.ntru.parameter_set",
55 parameter_sets[n]);
56
57 i_ntru = lib->crypto->create_dh(lib->crypto, params[_i].group);
58 ck_assert(i_ntru != NULL);
59 ck_assert(i_ntru->get_dh_group(i_ntru) == params[_i].group);
60
61 i_ntru->get_my_public_value(i_ntru, &pub_key);
62 ck_assert(pub_key.len > 0);
63
64 r_ntru = lib->crypto->create_dh(lib->crypto, params[_i].group);
65 ck_assert(r_ntru != NULL);
66
67 r_ntru->set_other_public_value(r_ntru, pub_key);
68 r_ntru->get_my_public_value(r_ntru, &cipher_text);
69 ck_assert(cipher_text.len > 0);
70
71 status = r_ntru->get_shared_secret(r_ntru, &r_shared_secret);
72 ck_assert(status == SUCCESS);
73 ck_assert(r_shared_secret.len > 0);
74
75 i_ntru->set_other_public_value(i_ntru, cipher_text);
76 status = i_ntru->get_shared_secret(i_ntru, &i_shared_secret);
77
78 if (status == SUCCESS)
79 {
80 ck_assert(chunk_equals(i_shared_secret, r_shared_secret));
81 }
82 else
83 {
84 ck_assert(i_shared_secret.len == 0);
85 }
86
87 chunk_clear(&i_shared_secret);
88 chunk_clear(&r_shared_secret);
89 chunk_free(&pub_key);
90 chunk_free(&cipher_text);
91 i_ntru->destroy(i_ntru);
92 r_ntru->destroy(r_ntru);
93 }
94 }
95 END_TEST
96
97 START_TEST(test_ntru_pubkey_oid)
98 {
99 chunk_t test[] = {
100 chunk_empty,
101 chunk_from_chars(0x00),
102 chunk_from_chars(0x01),
103 chunk_from_chars(0x02),
104 chunk_from_chars(0x02, 0x03, 0x00, 0x03, 0x10),
105 chunk_from_chars(0x01, 0x04, 0x00, 0x03, 0x10),
106 chunk_from_chars(0x01, 0x03, 0x00, 0x03, 0x10),
107 chunk_from_chars(0x01, 0x03, 0xff, 0x03, 0x10),
108 };
109
110 diffie_hellman_t *r_ntru;
111 chunk_t cipher_text;
112 int i;
113
114 for (i = 0; i < countof(test); i++)
115 {
116 r_ntru = lib->crypto->create_dh(lib->crypto, NTRU_128_BIT);
117 r_ntru->set_other_public_value(r_ntru, test[i]);
118 r_ntru->get_my_public_value(r_ntru, &cipher_text);
119 ck_assert(cipher_text.len == 0);
120 r_ntru->destroy(r_ntru);
121 }
122 }
123 END_TEST
124
125 START_TEST(test_ntru_wrong_set)
126 {
127 diffie_hellman_t *i_ntru, *r_ntru;
128 chunk_t pub_key, cipher_text;
129
130 lib->settings->set_str(lib->settings,
131 "libstrongswan.plugins.ntru.parameter_set",
132 "x9_98_bandwidth");
133 i_ntru = lib->crypto->create_dh(lib->crypto, NTRU_112_BIT);
134 i_ntru->get_my_public_value(i_ntru, &pub_key);
135
136 lib->settings->set_str(lib->settings,
137 "libstrongswan.plugins.ntru.parameter_set",
138 "optimum");
139 r_ntru = lib->crypto->create_dh(lib->crypto, NTRU_112_BIT);
140 r_ntru->set_other_public_value(r_ntru, pub_key);
141 r_ntru->get_my_public_value(r_ntru, &cipher_text);
142 ck_assert(cipher_text.len == 0);
143
144 chunk_free(&pub_key);
145 chunk_free(&cipher_text);
146 i_ntru->destroy(i_ntru);
147 r_ntru->destroy(r_ntru);
148 }
149 END_TEST
150
151 Suite *ntru_suite_create()
152 {
153 Suite *s;
154 TCase *tc;
155
156 s = suite_create("ntru");
157
158 tc = tcase_create("ke");
159 tcase_add_loop_test(tc, test_ntru_ke, 0, countof(params));
160 suite_add_tcase(s, tc);
161
162 tc = tcase_create("pubkey_oid");
163 tcase_add_test(tc, test_ntru_pubkey_oid);
164 suite_add_tcase(s, tc);
165
166 tc = tcase_create("wrong_set");
167 tcase_add_test(tc, test_ntru_wrong_set);
168 suite_add_tcase(s, tc);
169
170 return s;
171 }