diff options
Diffstat (limited to 'src/util/test_crypto_edx25519.c')
-rw-r--r-- | src/util/test_crypto_edx25519.c | 326 |
1 files changed, 326 insertions, 0 deletions
diff --git a/src/util/test_crypto_edx25519.c b/src/util/test_crypto_edx25519.c new file mode 100644 index 000000000..ead6f0bb9 --- /dev/null +++ b/src/util/test_crypto_edx25519.c | |||
@@ -0,0 +1,326 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2022 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | |||
20 | */ | ||
21 | /** | ||
22 | * @file util/test_crypto_edx25519.c | ||
23 | * @brief testcase for ECC public key crypto for edx25519 | ||
24 | * @author Özgür Kesim | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_signatures.h" | ||
29 | #include <gcrypt.h> | ||
30 | |||
31 | #define ITER 25 | ||
32 | |||
33 | #define KEYFILE "/tmp/test-gnunet-crypto-edx25519.key" | ||
34 | |||
35 | #define PERF GNUNET_YES | ||
36 | |||
37 | |||
38 | static struct GNUNET_CRYPTO_Edx25519PrivateKey key; | ||
39 | |||
40 | |||
41 | static int | ||
42 | testSignVerify (void) | ||
43 | { | ||
44 | struct GNUNET_CRYPTO_Edx25519Signature sig; | ||
45 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | ||
46 | struct GNUNET_CRYPTO_Edx25519PublicKey pkey; | ||
47 | struct GNUNET_TIME_Absolute start; | ||
48 | int ok = GNUNET_OK; | ||
49 | |||
50 | fprintf (stderr, "%s", "W"); | ||
51 | GNUNET_CRYPTO_edx25519_key_get_public (&key, | ||
52 | &pkey); | ||
53 | start = GNUNET_TIME_absolute_get (); | ||
54 | purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); | ||
55 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | ||
56 | |||
57 | for (unsigned int i = 0; i < ITER; i++) | ||
58 | { | ||
59 | fprintf (stderr, "%s", "."); fflush (stderr); | ||
60 | if (GNUNET_SYSERR == GNUNET_CRYPTO_edx25519_sign_ (&key, | ||
61 | &purp, | ||
62 | &sig)) | ||
63 | { | ||
64 | fprintf (stderr, | ||
65 | "GNUNET_CRYPTO_edx25519_sign returned SYSERR\n"); | ||
66 | ok = GNUNET_SYSERR; | ||
67 | continue; | ||
68 | } | ||
69 | if (GNUNET_SYSERR == | ||
70 | GNUNET_CRYPTO_edx25519_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST, | ||
71 | &purp, | ||
72 | &sig, | ||
73 | &pkey)) | ||
74 | { | ||
75 | fprintf (stderr, | ||
76 | "GNUNET_CRYPTO_edx25519_verify failed!\n"); | ||
77 | ok = GNUNET_SYSERR; | ||
78 | continue; | ||
79 | } | ||
80 | if (GNUNET_SYSERR != | ||
81 | GNUNET_CRYPTO_edx25519_verify_ ( | ||
82 | GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, | ||
83 | &purp, | ||
84 | &sig, | ||
85 | &pkey)) | ||
86 | { | ||
87 | fprintf (stderr, | ||
88 | "GNUNET_CRYPTO_edx25519_verify failed to fail!\n"); | ||
89 | ok = GNUNET_SYSERR; | ||
90 | continue; | ||
91 | } | ||
92 | } | ||
93 | fprintf (stderr, "\n"); | ||
94 | printf ("%d EdDSA sign/verify operations %s\n", | ||
95 | ITER, | ||
96 | GNUNET_STRINGS_relative_time_to_string ( | ||
97 | GNUNET_TIME_absolute_get_duration (start), | ||
98 | GNUNET_YES)); | ||
99 | return ok; | ||
100 | } | ||
101 | |||
102 | |||
103 | static int | ||
104 | testDeriveSignVerify (void) | ||
105 | { | ||
106 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | ||
107 | struct GNUNET_CRYPTO_Edx25519Signature sig; | ||
108 | struct GNUNET_CRYPTO_Edx25519PrivateKey dkey; | ||
109 | struct GNUNET_CRYPTO_Edx25519PublicKey pub; | ||
110 | struct GNUNET_CRYPTO_Edx25519PublicKey dpub; | ||
111 | struct GNUNET_CRYPTO_Edx25519PublicKey dpub2; | ||
112 | |||
113 | GNUNET_CRYPTO_edx25519_key_get_public (&key, &pub); | ||
114 | GNUNET_CRYPTO_edx25519_private_key_derive (&key, | ||
115 | "test-derive", | ||
116 | sizeof("test-derive"), | ||
117 | &dkey); | ||
118 | GNUNET_CRYPTO_edx25519_public_key_derive (&pub, | ||
119 | "test-derive", | ||
120 | sizeof("test-derive"), | ||
121 | &dpub); | ||
122 | GNUNET_CRYPTO_edx25519_key_get_public (&dkey, &dpub2); | ||
123 | |||
124 | if (0 != GNUNET_memcmp (&dpub.q_y, &dpub2.q_y)) | ||
125 | { | ||
126 | fprintf (stderr, "key deriviation failed\n"); | ||
127 | return GNUNET_SYSERR; | ||
128 | } | ||
129 | |||
130 | purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); | ||
131 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | ||
132 | |||
133 | GNUNET_CRYPTO_edx25519_sign_ (&dkey, | ||
134 | &purp, | ||
135 | &sig); | ||
136 | |||
137 | if (GNUNET_SYSERR == | ||
138 | GNUNET_CRYPTO_edx25519_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST, | ||
139 | &purp, | ||
140 | &sig, | ||
141 | &dpub)) | ||
142 | { | ||
143 | fprintf (stderr, | ||
144 | "GNUNET_CRYPTO_edx25519_verify failed after derivation!\n"); | ||
145 | return GNUNET_SYSERR; | ||
146 | } | ||
147 | |||
148 | if (GNUNET_SYSERR != | ||
149 | GNUNET_CRYPTO_edx25519_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST, | ||
150 | &purp, | ||
151 | &sig, | ||
152 | &pub)) | ||
153 | { | ||
154 | fprintf (stderr, | ||
155 | "GNUNET_CRYPTO_edx25519_verify failed to fail after derivation!\n"); | ||
156 | return GNUNET_SYSERR; | ||
157 | } | ||
158 | |||
159 | if (GNUNET_SYSERR != | ||
160 | GNUNET_CRYPTO_edx25519_verify_ ( | ||
161 | GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, | ||
162 | &purp, | ||
163 | &sig, | ||
164 | &dpub)) | ||
165 | { | ||
166 | fprintf (stderr, | ||
167 | "GNUNET_CRYPTO_edx25519_verify failed to fail after derivation!\n"); | ||
168 | return GNUNET_SYSERR; | ||
169 | } | ||
170 | return GNUNET_OK; | ||
171 | } | ||
172 | |||
173 | |||
174 | #if PERF | ||
175 | static int | ||
176 | testSignPerformance () | ||
177 | { | ||
178 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | ||
179 | struct GNUNET_CRYPTO_Edx25519Signature sig; | ||
180 | struct GNUNET_CRYPTO_Edx25519PublicKey pkey; | ||
181 | struct GNUNET_TIME_Absolute start; | ||
182 | int ok = GNUNET_OK; | ||
183 | |||
184 | purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); | ||
185 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | ||
186 | fprintf (stderr, "%s", "W"); | ||
187 | GNUNET_CRYPTO_edx25519_key_get_public (&key, | ||
188 | &pkey); | ||
189 | start = GNUNET_TIME_absolute_get (); | ||
190 | for (unsigned int i = 0; i < ITER; i++) | ||
191 | { | ||
192 | fprintf (stderr, "%s", "."); | ||
193 | fflush (stderr); | ||
194 | if (GNUNET_SYSERR == | ||
195 | GNUNET_CRYPTO_edx25519_sign_ (&key, | ||
196 | &purp, | ||
197 | &sig)) | ||
198 | { | ||
199 | fprintf (stderr, "%s", "GNUNET_CRYPTO_edx25519_sign returned SYSERR\n"); | ||
200 | ok = GNUNET_SYSERR; | ||
201 | continue; | ||
202 | } | ||
203 | } | ||
204 | fprintf (stderr, "\n"); | ||
205 | printf ("%d EdDSA sign operations %s\n", | ||
206 | ITER, | ||
207 | GNUNET_STRINGS_relative_time_to_string ( | ||
208 | GNUNET_TIME_absolute_get_duration (start), | ||
209 | GNUNET_YES)); | ||
210 | return ok; | ||
211 | } | ||
212 | |||
213 | |||
214 | #endif | ||
215 | |||
216 | |||
217 | #if 0 /* not implemented */ | ||
218 | static int | ||
219 | testCreateFromFile (void) | ||
220 | { | ||
221 | struct GNUNET_CRYPTO_Edx25519PublicKey p1; | ||
222 | struct GNUNET_CRYPTO_Edx25519PublicKey p2; | ||
223 | |||
224 | /* do_create == GNUNET_YES and non-existing file MUST return GNUNET_YES */ | ||
225 | GNUNET_assert (0 == unlink (KEYFILE) || ENOENT == errno); | ||
226 | GNUNET_assert (GNUNET_YES == | ||
227 | GNUNET_CRYPTO_edx25519_key_from_file (KEYFILE, | ||
228 | GNUNET_YES, | ||
229 | &key)); | ||
230 | GNUNET_CRYPTO_edx25519_key_get_public (&key, | ||
231 | &p1); | ||
232 | |||
233 | /* do_create == GNUNET_YES and _existing_ file MUST return GNUNET_NO */ | ||
234 | GNUNET_assert (GNUNET_NO == | ||
235 | GNUNET_CRYPTO_edx25519_key_from_file (KEYFILE, | ||
236 | GNUNET_YES, | ||
237 | &key)); | ||
238 | GNUNET_CRYPTO_edx25519_key_get_public (&key, | ||
239 | &p2); | ||
240 | GNUNET_assert (0 == | ||
241 | GNUNET_memcmp (&p1, | ||
242 | &p2)); | ||
243 | |||
244 | /* do_create == GNUNET_NO and non-existing file MUST return GNUNET_SYSERR */ | ||
245 | GNUNET_assert (0 == unlink (KEYFILE)); | ||
246 | GNUNET_assert (GNUNET_SYSERR == | ||
247 | GNUNET_CRYPTO_edx25519_key_from_file (KEYFILE, | ||
248 | GNUNET_NO, | ||
249 | &key)); | ||
250 | return GNUNET_OK; | ||
251 | } | ||
252 | |||
253 | |||
254 | #endif | ||
255 | |||
256 | |||
257 | static void | ||
258 | perf_keygen (void) | ||
259 | { | ||
260 | struct GNUNET_TIME_Absolute start; | ||
261 | struct GNUNET_CRYPTO_Edx25519PrivateKey pk; | ||
262 | |||
263 | fprintf (stderr, "%s", "W"); | ||
264 | start = GNUNET_TIME_absolute_get (); | ||
265 | for (unsigned int i = 0; i < 10; i++) | ||
266 | { | ||
267 | fprintf (stderr, "."); | ||
268 | fflush (stderr); | ||
269 | GNUNET_CRYPTO_edx25519_key_create (&pk); | ||
270 | } | ||
271 | fprintf (stderr, "\n"); | ||
272 | printf ("10 EdDSA keys created in %s\n", | ||
273 | GNUNET_STRINGS_relative_time_to_string ( | ||
274 | GNUNET_TIME_absolute_get_duration (start), GNUNET_YES)); | ||
275 | } | ||
276 | |||
277 | |||
278 | int | ||
279 | main (int argc, char *argv[]) | ||
280 | { | ||
281 | int failure_count = 0; | ||
282 | |||
283 | if (! gcry_check_version ("1.6.0")) | ||
284 | { | ||
285 | fprintf (stderr, | ||
286 | "libgcrypt has not the expected version (version %s is required).\n", | ||
287 | "1.6.0"); | ||
288 | return 0; | ||
289 | } | ||
290 | if (getenv ("GNUNET_GCRYPT_DEBUG")) | ||
291 | gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); | ||
292 | GNUNET_log_setup ("test-crypto-edx25519", | ||
293 | "WARNING", | ||
294 | NULL); | ||
295 | GNUNET_CRYPTO_edx25519_key_create (&key); | ||
296 | if (GNUNET_OK != testDeriveSignVerify ()) | ||
297 | { | ||
298 | failure_count++; | ||
299 | fprintf (stderr, | ||
300 | "\n\n%d TESTS FAILED!\n\n", failure_count); | ||
301 | return -1; | ||
302 | } | ||
303 | #if PERF | ||
304 | if (GNUNET_OK != testSignPerformance ()) | ||
305 | failure_count++; | ||
306 | #endif | ||
307 | if (GNUNET_OK != testSignVerify ()) | ||
308 | failure_count++; | ||
309 | #if 0 /* not implemented */ | ||
310 | if (GNUNET_OK != testCreateFromFile ()) | ||
311 | failure_count++; | ||
312 | #endif | ||
313 | perf_keygen (); | ||
314 | |||
315 | if (0 != failure_count) | ||
316 | { | ||
317 | fprintf (stderr, | ||
318 | "\n\n%d TESTS FAILED!\n\n", | ||
319 | failure_count); | ||
320 | return -1; | ||
321 | } | ||
322 | return 0; | ||
323 | } | ||
324 | |||
325 | |||
326 | /* end of test_crypto_edx25519.c */ | ||