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