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