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