aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_crypto_ecdsa.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_crypto_ecdsa.c')
-rw-r--r--src/util/test_crypto_ecdsa.c282
1 files changed, 0 insertions, 282 deletions
diff --git a/src/util/test_crypto_ecdsa.c b/src/util/test_crypto_ecdsa.c
deleted file mode 100644
index 9dc1f863d..000000000
--- a/src/util/test_crypto_ecdsa.c
+++ /dev/null
@@ -1,282 +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_ecdsa.c
23 * @brief testcase for ECC ECDSA 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 PERF GNUNET_YES
35
36
37static struct GNUNET_CRYPTO_EcdsaPrivateKey key;
38
39
40static int
41testSignVerify (void)
42{
43 struct GNUNET_CRYPTO_EcdsaSignature sig;
44 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
45 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
46 struct GNUNET_TIME_Absolute start;
47 int ok = GNUNET_OK;
48
49 fprintf (stderr, "%s", "W");
50 GNUNET_CRYPTO_ecdsa_key_get_public (&key,
51 &pkey);
52 start = GNUNET_TIME_absolute_get ();
53 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
54 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
55
56 for (unsigned int i = 0; i < ITER; i++)
57 {
58 fprintf (stderr, "%s", ".");
59 fflush (stderr);
60 if (GNUNET_SYSERR ==
61 GNUNET_CRYPTO_ecdsa_sign_ (&key,
62 &purp,
63 &sig))
64 {
65 fprintf (stderr,
66 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
67 ok = GNUNET_SYSERR;
68 continue;
69 }
70 if (GNUNET_SYSERR ==
71 GNUNET_CRYPTO_ecdsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
72 &purp,
73 &sig,
74 &pkey))
75 {
76 fprintf (stderr,
77 "GNUNET_CRYPTO_ecdsa_verify failed!\n");
78 ok = GNUNET_SYSERR;
79 continue;
80 }
81 if (GNUNET_SYSERR !=
82 GNUNET_CRYPTO_ecdsa_verify_ (
83 GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
84 &purp,
85 &sig,
86 &pkey))
87 {
88 fprintf (stderr,
89 "GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
90 ok = GNUNET_SYSERR;
91 continue;
92 }
93 }
94 printf ("%d ECDSA 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_EcdsaSignature sig;
107 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
108 struct GNUNET_CRYPTO_EcdsaPrivateKey *dpriv;
109 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
110 struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
111 struct GNUNET_CRYPTO_EcdsaPublicKey dpub2;
112
113 dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive (&key,
114 "test-derive",
115 "test-CTX");
116 GNUNET_CRYPTO_ecdsa_key_get_public (&key,
117 &pkey);
118 GNUNET_CRYPTO_ecdsa_public_key_derive (&pkey,
119 "test-derive",
120 "test-CTX",
121 &dpub);
122 GNUNET_CRYPTO_ecdsa_key_get_public (dpriv, &dpub2);
123 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
124 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
125
126 if (0 != GNUNET_memcmp (&dpub.q_y, &dpub2.q_y))
127 {
128 fprintf (stderr, "%s", "key derivation failed\n");
129 GNUNET_free (dpriv);
130 return GNUNET_SYSERR;
131 }
132
133 if (GNUNET_SYSERR ==
134 GNUNET_CRYPTO_ecdsa_sign_ (dpriv,
135 &purp,
136 &sig))
137 {
138 fprintf (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
139 GNUNET_free (dpriv);
140 return GNUNET_SYSERR;
141 }
142 if (GNUNET_SYSERR ==
143 GNUNET_CRYPTO_ecdsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
144 &purp,
145 &sig,
146 &dpub))
147 {
148 fprintf (stderr,
149 "GNUNET_CRYPTO_ecdsa_verify failed!\n");
150 GNUNET_free (dpriv);
151 return GNUNET_SYSERR;
152 }
153 if (GNUNET_SYSERR !=
154 GNUNET_CRYPTO_ecdsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TEST,
155 &purp,
156 &sig,
157 &pkey))
158 {
159 fprintf (stderr,
160 "GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
161 GNUNET_free (dpriv);
162 return GNUNET_SYSERR;
163 }
164 if (GNUNET_SYSERR !=
165 GNUNET_CRYPTO_ecdsa_verify_ (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
166 &purp,
167 &sig,
168 &dpub))
169 {
170 fprintf (stderr,
171 "GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
172 GNUNET_free (dpriv);
173 return GNUNET_SYSERR;
174 }
175 GNUNET_free (dpriv);
176 return GNUNET_OK;
177}
178
179
180#if PERF
181static int
182testSignPerformance (void)
183{
184 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
185 struct GNUNET_CRYPTO_EcdsaSignature sig;
186 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
187 int i;
188 struct GNUNET_TIME_Absolute start;
189 int ok = GNUNET_OK;
190
191 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
192 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
193 fprintf (stderr, "%s", "W");
194 GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
195 start = GNUNET_TIME_absolute_get ();
196 for (i = 0; i < ITER; i++)
197 {
198 fprintf (stderr, "%s", "."); fflush (stderr);
199 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign_ (key, &purp, &sig))
200 {
201 fprintf (stderr, "%s",
202 "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
203 ok = GNUNET_SYSERR;
204 continue;
205 }
206 }
207 printf ("%d ECC sign operations %s\n", 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
218static void
219perf_keygen (void)
220{
221 struct GNUNET_TIME_Absolute start;
222 struct GNUNET_CRYPTO_EcdsaPrivateKey pk;
223
224 fprintf (stderr, "%s", "W");
225 start = GNUNET_TIME_absolute_get ();
226 for (unsigned int i = 0; i < 10; i++)
227 {
228 fprintf (stderr, ".");
229 fflush (stderr);
230 GNUNET_CRYPTO_ecdsa_key_create (&pk);
231 }
232 fflush (stderr);
233 printf ("10 ECDSA keys created in %s\n",
234 GNUNET_STRINGS_relative_time_to_string (
235 GNUNET_TIME_absolute_get_duration (start),
236 GNUNET_YES));
237}
238
239
240int
241main (int argc, char *argv[])
242{
243 int failure_count = 0;
244
245 if (! gcry_check_version ("1.6.0"))
246 {
247 fprintf (stderr,
248 "libgcrypt has not the expected version (version %s is required).\n",
249 "1.6.0");
250 return 0;
251 }
252 if (getenv ("GNUNET_GCRYPT_DEBUG"))
253 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
254 GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
255 GNUNET_CRYPTO_ecdsa_key_create (&key);
256 if (GNUNET_OK != testDeriveSignVerify ())
257 {
258 failure_count++;
259 fprintf (stderr,
260 "\n\n%d TESTS FAILED!\n\n", failure_count);
261 return -1;
262 }
263#if PERF
264 if (GNUNET_OK != testSignPerformance ())
265 failure_count++;
266#endif
267 if (GNUNET_OK != testSignVerify ())
268 failure_count++;
269 perf_keygen ();
270
271 if (0 != failure_count)
272 {
273 fprintf (stderr,
274 "\n\n%d TESTS FAILED!\n\n",
275 failure_count);
276 return -1;
277 }
278 return 0;
279}
280
281
282/* end of test_crypto_ecdsa.c */