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.c316
1 files changed, 0 insertions, 316 deletions
diff --git a/src/util/test_crypto_eddsa.c b/src/util/test_crypto_eddsa.c
deleted file mode 100644
index 10d6a4e91..000000000
--- a/src/util/test_crypto_eddsa.c
+++ /dev/null
@@ -1,316 +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_with_scalar (&dpriv,
134 &purp,
135 &sig);
136 if (GNUNET_SYSERR ==
137 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
138 &purp,
139 &sig,
140 &dpub))
141 {
142 fprintf (stderr,
143 "GNUNET_CRYPTO_eddsa_verify failed!\n");
144 return GNUNET_SYSERR;
145 }
146 if (GNUNET_SYSERR !=
147 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
148 &purp,
149 &sig,
150 &pkey))
151 {
152 fprintf (stderr,
153 "GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
154 return GNUNET_SYSERR;
155 }
156 if (GNUNET_SYSERR !=
157 GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
158 &purp,
159 &sig,
160 &dpub))
161 {
162 fprintf (stderr,
163 "GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
164 return GNUNET_SYSERR;
165 }
166 return GNUNET_OK;
167}
168
169
170#if PERF
171static int
172testSignPerformance ()
173{
174 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
175 struct GNUNET_CRYPTO_EddsaSignature sig;
176 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
177 struct GNUNET_TIME_Absolute start;
178 int ok = GNUNET_OK;
179
180 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
181 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
182 fprintf (stderr, "%s", "W");
183 GNUNET_CRYPTO_eddsa_key_get_public (&key,
184 &pkey);
185 start = GNUNET_TIME_absolute_get ();
186 for (unsigned int i = 0; i < ITER; i++)
187 {
188 fprintf (stderr, "%s", ".");
189 fflush (stderr);
190 if (GNUNET_SYSERR ==
191 GNUNET_CRYPTO_eddsa_sign_ (&key,
192 &purp,
193 &sig))
194 {
195 fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
196 ok = GNUNET_SYSERR;
197 continue;
198 }
199 }
200 fprintf (stderr, "\n");
201 printf ("%d EdDSA sign operations %s\n",
202 ITER,
203 GNUNET_STRINGS_relative_time_to_string (
204 GNUNET_TIME_absolute_get_duration (start),
205 GNUNET_YES));
206 return ok;
207}
208
209
210#endif
211
212
213static int
214testCreateFromFile (void)
215{
216 struct GNUNET_CRYPTO_EddsaPublicKey p1;
217 struct GNUNET_CRYPTO_EddsaPublicKey p2;
218
219 GNUNET_assert (0 <=
220 GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
221 GNUNET_YES,
222 &key));
223 GNUNET_CRYPTO_eddsa_key_get_public (&key,
224 &p1);
225 GNUNET_assert (GNUNET_NO ==
226 GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
227 GNUNET_YES,
228 &key));
229 GNUNET_CRYPTO_eddsa_key_get_public (&key,
230 &p2);
231 GNUNET_assert (0 ==
232 GNUNET_memcmp (&p1,
233 &p2));
234 GNUNET_assert (0 == unlink (KEYFILE));
235 GNUNET_assert (GNUNET_OK ==
236 GNUNET_CRYPTO_eddsa_key_from_file (KEYFILE,
237 GNUNET_NO,
238 &key));
239 GNUNET_CRYPTO_eddsa_key_get_public (&key,
240 &p2);
241 GNUNET_assert (0 !=
242 GNUNET_memcmp (&p1,
243 &p2));
244 return GNUNET_OK;
245}
246
247
248static void
249perf_keygen (void)
250{
251 struct GNUNET_TIME_Absolute start;
252 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
253
254 fprintf (stderr, "%s", "W");
255 start = GNUNET_TIME_absolute_get ();
256 for (unsigned int i = 0; i < 10; i++)
257 {
258 fprintf (stderr, ".");
259 fflush (stderr);
260 GNUNET_CRYPTO_eddsa_key_create (&pk);
261 }
262 fprintf (stderr, "\n");
263 printf ("10 EdDSA keys created in %s\n",
264 GNUNET_STRINGS_relative_time_to_string (
265 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
266}
267
268
269int
270main (int argc, char *argv[])
271{
272 int failure_count = 0;
273
274 if (! gcry_check_version ("1.6.0"))
275 {
276 fprintf (stderr,
277 "libgcrypt has not the expected version (version %s is required).\n",
278 "1.6.0");
279 return 0;
280 }
281 if (getenv ("GNUNET_GCRYPT_DEBUG"))
282 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
283 GNUNET_log_setup ("test-crypto-eddsa",
284 "WARNING",
285 NULL);
286 GNUNET_CRYPTO_eddsa_key_create (&key);
287 if (GNUNET_OK != testDeriveSignVerify ())
288 {
289 failure_count++;
290 fprintf (stderr,
291 "\n\n%d TESTS FAILED!\n\n", failure_count);
292 return -1;
293 }
294#if PERF
295 if (GNUNET_OK != testSignPerformance ())
296 failure_count++;
297#endif
298 if (GNUNET_OK != testSignVerify ())
299 failure_count++;
300 if (GNUNET_OK != testCreateFromFile ())
301 failure_count++;
302 GNUNET_assert (0 == unlink (KEYFILE));
303 perf_keygen ();
304
305 if (0 != failure_count)
306 {
307 fprintf (stderr,
308 "\n\n%d TESTS FAILED!\n\n",
309 failure_count);
310 return -1;
311 }
312 return 0;
313}
314
315
316/* end of test_crypto_eddsa.c */