aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_crypto_eddsa.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_crypto_eddsa.c')
-rw-r--r--src/lib/util/test_crypto_eddsa.c319
1 files changed, 319 insertions, 0 deletions
diff --git a/src/lib/util/test_crypto_eddsa.c b/src/lib/util/test_crypto_eddsa.c
new file mode 100644
index 000000000..820230fd2
--- /dev/null
+++ b/src/lib/util/test_crypto_eddsa.c
@@ -0,0 +1,319 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2002-2013 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_eddsa.c
23 * @brief testcase for ECC public key crypto
24 * @author Christian Grothoff
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-eddsa.key"
35
36#define PERF GNUNET_YES
37
38
39static struct GNUNET_CRYPTO_EddsaPrivateKey key;
40
41
42static int
43testSignVerify (void)
44{
45 struct GNUNET_CRYPTO_EddsaSignature sig;
46 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
47 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
48 struct GNUNET_TIME_Absolute start;
49 int ok = GNUNET_OK;
50
51 fprintf (stderr, "%s", "W");
52 GNUNET_CRYPTO_eddsa_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_eddsa_sign_ (&key,
62 &purp,
63 &sig))
64 {
65 fprintf (stderr,
66 "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
67 ok = GNUNET_SYSERR;
68 continue;
69 }
70 if (GNUNET_SYSERR ==
71 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
72 &purp,
73 &sig,
74 &pkey))
75 {
76 fprintf (stderr,
77 "GNUNET_CRYPTO_eddsa_verify failed!\n");
78 ok = GNUNET_SYSERR;
79 continue;
80 }
81 if (GNUNET_SYSERR !=
82 GNUNET_CRYPTO_eddsa_verify_ (
83 GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
84 &purp,
85 &sig,
86 &pkey))
87 {
88 fprintf (stderr,
89 "GNUNET_CRYPTO_eddsa_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
104static int
105testDeriveSignVerify (void)
106{
107 struct GNUNET_CRYPTO_EddsaSignature sig;
108 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
109 struct GNUNET_CRYPTO_EddsaPrivateScalar dpriv;
110 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
111 struct GNUNET_CRYPTO_EddsaPublicKey dpub;
112 struct GNUNET_CRYPTO_EddsaPublicKey dpub2;
113
114 GNUNET_CRYPTO_eddsa_private_key_derive (&key,
115 "test-derive",
116 "test-CTX",
117 &dpriv);
118 GNUNET_CRYPTO_eddsa_key_get_public (&key,
119 &pkey);
120 GNUNET_CRYPTO_eddsa_public_key_derive (&pkey,
121 "test-derive",
122 "test-CTX",
123 &dpub);
124 GNUNET_CRYPTO_eddsa_key_get_public_from_scalar (&dpriv, &dpub2);
125 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
126 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
127
128 if (0 != GNUNET_memcmp (&dpub.q_y, &dpub2.q_y))
129 {
130 fprintf (stderr, "%s", "key derivation failed\n");
131 return GNUNET_SYSERR;
132 }
133
134 GNUNET_CRYPTO_eddsa_sign_derived (&key,
135 "test-derive",
136 "test-CTX",
137 &purp,
138 &sig);
139 if (GNUNET_SYSERR ==
140 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
141 &purp,
142 &sig,
143 &dpub))
144 {
145 fprintf (stderr,
146 "GNUNET_CRYPTO_eddsa_verify failed!\n");
147 return GNUNET_SYSERR;
148 }
149 if (GNUNET_SYSERR !=
150 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
151 &purp,
152 &sig,
153 &pkey))
154 {
155 fprintf (stderr,
156 "GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
157 return GNUNET_SYSERR;
158 }
159 if (GNUNET_SYSERR !=
160 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
161 &purp,
162 &sig,
163 &dpub))
164 {
165 fprintf (stderr,
166 "GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
167 return GNUNET_SYSERR;
168 }
169 return GNUNET_OK;
170}
171
172
173#if PERF
174static int
175testSignPerformance ()
176{
177 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
178 struct GNUNET_CRYPTO_EddsaSignature sig;
179 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
180 struct GNUNET_TIME_Absolute start;
181 int ok = GNUNET_OK;
182
183 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
184 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
185 fprintf (stderr, "%s", "W");
186 GNUNET_CRYPTO_eddsa_key_get_public (&key,
187 &pkey);
188 start = GNUNET_TIME_absolute_get ();
189 for (unsigned int i = 0; i < ITER; i++)
190 {
191 fprintf (stderr, "%s", ".");
192 fflush (stderr);
193 if (GNUNET_SYSERR ==
194 GNUNET_CRYPTO_eddsa_sign_ (&key,
195 &purp,
196 &sig))
197 {
198 fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
199 ok = GNUNET_SYSERR;
200 continue;
201 }
202 }
203 fprintf (stderr, "\n");
204 printf ("%d EdDSA sign operations %s\n",
205 ITER,
206 GNUNET_STRINGS_relative_time_to_string (
207 GNUNET_TIME_absolute_get_duration (start),
208 GNUNET_YES));
209 return ok;
210}
211
212
213#endif
214
215
216static int
217testCreateFromFile (void)
218{
219 struct GNUNET_CRYPTO_EddsaPublicKey p1;
220 struct GNUNET_CRYPTO_EddsaPublicKey p2;
221
222 /* do_create == GNUNET_YES and non-existing file MUST return GNUNET_YES */
223 GNUNET_assert (0 == unlink (KEYFILE) || ENOENT == errno);
224 GNUNET_assert (GNUNET_YES ==
225 GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
226 GNUNET_YES,
227 &key));
228 GNUNET_CRYPTO_eddsa_key_get_public (&key,
229 &p1);
230
231 /* do_create == GNUNET_YES and _existing_ file MUST return GNUNET_NO */
232 GNUNET_assert (GNUNET_NO ==
233 GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
234 GNUNET_YES,
235 &key));
236 GNUNET_CRYPTO_eddsa_key_get_public (&key,
237 &p2);
238 GNUNET_assert (0 ==
239 GNUNET_memcmp (&p1,
240 &p2));
241
242 /* do_create == GNUNET_NO and non-existing file MUST return GNUNET_SYSERR */
243 GNUNET_assert (0 == unlink (KEYFILE));
244 GNUNET_assert (GNUNET_SYSERR ==
245 GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
246 GNUNET_NO,
247 &key));
248 return GNUNET_OK;
249}
250
251
252static void
253perf_keygen (void)
254{
255 struct GNUNET_TIME_Absolute start;
256 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
257
258 fprintf (stderr, "%s", "W");
259 start = GNUNET_TIME_absolute_get ();
260 for (unsigned int i = 0; i < 10; i++)
261 {
262 fprintf (stderr, ".");
263 fflush (stderr);
264 GNUNET_CRYPTO_eddsa_key_create (&pk);
265 }
266 fprintf (stderr, "\n");
267 printf ("10 EdDSA keys created in %s\n",
268 GNUNET_STRINGS_relative_time_to_string (
269 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
270}
271
272
273int
274main (int argc, char *argv[])
275{
276 int failure_count = 0;
277
278 if (! gcry_check_version ("1.6.0"))
279 {
280 fprintf (stderr,
281 "libgcrypt has not the expected version (version %s is required).\n",
282 "1.6.0");
283 return 0;
284 }
285 if (getenv ("GNUNET_GCRYPT_DEBUG"))
286 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
287 GNUNET_log_setup ("test-crypto-eddsa",
288 "WARNING",
289 NULL);
290 GNUNET_CRYPTO_eddsa_key_create (&key);
291 if (GNUNET_OK != testDeriveSignVerify ())
292 {
293 failure_count++;
294 fprintf (stderr,
295 "\n\n%d TESTS FAILED!\n\n", failure_count);
296 return -1;
297 }
298#if PERF
299 if (GNUNET_OK != testSignPerformance ())
300 failure_count++;
301#endif
302 if (GNUNET_OK != testSignVerify ())
303 failure_count++;
304 if (GNUNET_OK != testCreateFromFile ())
305 failure_count++;
306 perf_keygen ();
307
308 if (0 != failure_count)
309 {
310 fprintf (stderr,
311 "\n\n%d TESTS FAILED!\n\n",
312 failure_count);
313 return -1;
314 }
315 return 0;
316}
317
318
319/* end of test_crypto_eddsa.c */