diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-10-10 14:40:06 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-10-10 14:40:06 +0000 |
commit | 4add4c6a6302ff01539e246f68aaf13299414fd6 (patch) | |
tree | 2f5343a10726d165c35c98c48eb59247a5ab3a63 /src/util/test_crypto_ecdsa.c | |
parent | 9351b1e9bdf2b067b6db06562c26ba658cff42b8 (diff) | |
download | gnunet-4add4c6a6302ff01539e246f68aaf13299414fd6.tar.gz gnunet-4add4c6a6302ff01539e246f68aaf13299414fd6.zip |
-updated tests
Diffstat (limited to 'src/util/test_crypto_ecdsa.c')
-rw-r--r-- | src/util/test_crypto_ecdsa.c | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/src/util/test_crypto_ecdsa.c b/src/util/test_crypto_ecdsa.c new file mode 100644 index 000000000..27c0fb137 --- /dev/null +++ b/src/util/test_crypto_ecdsa.c | |||
@@ -0,0 +1,237 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2002-2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | 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 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
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 | |||
36 | static struct GNUNET_CRYPTO_EcdsaPrivateKey *key; | ||
37 | |||
38 | |||
39 | static int | ||
40 | testSignVerify () | ||
41 | { | ||
42 | struct GNUNET_CRYPTO_EcdsaSignature sig; | ||
43 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | ||
44 | struct GNUNET_CRYPTO_EcdsaPublicKey pkey; | ||
45 | int i; | ||
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, &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 (i = 0; i < ITER; i++) | ||
56 | { | ||
57 | FPRINTF (stderr, "%s", "."); | ||
58 | if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig)) | ||
59 | { | ||
60 | FPRINTF (stderr, | ||
61 | "%s", | ||
62 | "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n"); | ||
63 | ok = GNUNET_SYSERR; | ||
64 | continue; | ||
65 | } | ||
66 | if (GNUNET_SYSERR == | ||
67 | GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig, | ||
68 | &pkey)) | ||
69 | { | ||
70 | printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n"); | ||
71 | ok = GNUNET_SYSERR; | ||
72 | continue; | ||
73 | } | ||
74 | if (GNUNET_SYSERR != | ||
75 | GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, | ||
76 | &purp, &sig, &pkey)) | ||
77 | { | ||
78 | printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n"); | ||
79 | ok = GNUNET_SYSERR; | ||
80 | continue; | ||
81 | } | ||
82 | } | ||
83 | printf ("%d ECDSA sign/verify operations %s\n", ITER, | ||
84 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES)); | ||
85 | return ok; | ||
86 | } | ||
87 | |||
88 | |||
89 | static int | ||
90 | testDeriveSignVerify () | ||
91 | { | ||
92 | struct GNUNET_CRYPTO_EcdsaSignature sig; | ||
93 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | ||
94 | struct GNUNET_CRYPTO_EcdsaPrivateKey *dpriv; | ||
95 | struct GNUNET_CRYPTO_EcdsaPublicKey pkey; | ||
96 | struct GNUNET_CRYPTO_EcdsaPublicKey dpub; | ||
97 | |||
98 | dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive (key, "test-derive", "test-CTX"); | ||
99 | GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey); | ||
100 | GNUNET_CRYPTO_ecdsa_public_key_derive (&pkey, "test-derive", "test-CTX", &dpub); | ||
101 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); | ||
102 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | ||
103 | |||
104 | if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig)) | ||
105 | { | ||
106 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n"); | ||
107 | GNUNET_free (dpriv); | ||
108 | return GNUNET_SYSERR; | ||
109 | } | ||
110 | if (GNUNET_SYSERR == | ||
111 | GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, | ||
112 | &purp, &sig, | ||
113 | &dpub)) | ||
114 | { | ||
115 | printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n"); | ||
116 | GNUNET_free (dpriv); | ||
117 | return GNUNET_SYSERR; | ||
118 | } | ||
119 | if (GNUNET_SYSERR != | ||
120 | GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, | ||
121 | &purp, &sig, | ||
122 | &pkey)) | ||
123 | { | ||
124 | printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n"); | ||
125 | GNUNET_free (dpriv); | ||
126 | return GNUNET_SYSERR; | ||
127 | } | ||
128 | if (GNUNET_SYSERR != | ||
129 | GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, | ||
130 | &purp, &sig, &dpub)) | ||
131 | { | ||
132 | printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n"); | ||
133 | GNUNET_free (dpriv); | ||
134 | return GNUNET_SYSERR; | ||
135 | } | ||
136 | GNUNET_free (dpriv); | ||
137 | return GNUNET_OK; | ||
138 | } | ||
139 | |||
140 | |||
141 | #if PERF | ||
142 | static int | ||
143 | testSignPerformance () | ||
144 | { | ||
145 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | ||
146 | struct GNUNET_CRYPTO_EcdsaSignature sig; | ||
147 | struct GNUNET_CRYPTO_EcdsaPublicKey pkey; | ||
148 | int i; | ||
149 | struct GNUNET_TIME_Absolute start; | ||
150 | int ok = GNUNET_OK; | ||
151 | |||
152 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); | ||
153 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | ||
154 | FPRINTF (stderr, "%s", "W"); | ||
155 | GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey); | ||
156 | start = GNUNET_TIME_absolute_get (); | ||
157 | for (i = 0; i < ITER; i++) | ||
158 | { | ||
159 | FPRINTF (stderr, "%s", "."); | ||
160 | if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig)) | ||
161 | { | ||
162 | FPRINTF (stderr, "%s", | ||
163 | "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n"); | ||
164 | ok = GNUNET_SYSERR; | ||
165 | continue; | ||
166 | } | ||
167 | } | ||
168 | printf ("%d ECC sign operations %s\n", ITER, | ||
169 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), | ||
170 | GNUNET_YES)); | ||
171 | return ok; | ||
172 | } | ||
173 | #endif | ||
174 | |||
175 | |||
176 | static void | ||
177 | perf_keygen () | ||
178 | { | ||
179 | struct GNUNET_TIME_Absolute start; | ||
180 | struct GNUNET_CRYPTO_EcdsaPrivateKey *pk; | ||
181 | int i; | ||
182 | |||
183 | start = GNUNET_TIME_absolute_get (); | ||
184 | for (i=0;i<10;i++) | ||
185 | { | ||
186 | fprintf (stderr, "."); | ||
187 | pk = GNUNET_CRYPTO_ecdsa_key_create (); | ||
188 | GNUNET_free (pk); | ||
189 | } | ||
190 | fprintf (stderr, "\n"); | ||
191 | printf ("Creating 10 ECDSA keys took %s\n", | ||
192 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES)); | ||
193 | } | ||
194 | |||
195 | |||
196 | int | ||
197 | main (int argc, char *argv[]) | ||
198 | { | ||
199 | int failure_count = 0; | ||
200 | |||
201 | if (! gcry_check_version ("1.5.0")) | ||
202 | { | ||
203 | FPRINTF (stderr, | ||
204 | _ | ||
205 | ("libgcrypt has not the expected version (version %s is required).\n"), | ||
206 | "1.5.0"); | ||
207 | return 0; | ||
208 | } | ||
209 | GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL); | ||
210 | key = GNUNET_CRYPTO_ecdsa_key_create (); | ||
211 | if (GNUNET_OK != testDeriveSignVerify ()) | ||
212 | { | ||
213 | failure_count++; | ||
214 | fprintf (stderr, | ||
215 | "\n\n%d TESTS FAILED!\n\n", failure_count); | ||
216 | return -1; | ||
217 | } | ||
218 | #if PERF | ||
219 | if (GNUNET_OK != testSignPerformance ()) | ||
220 | failure_count++; | ||
221 | #endif | ||
222 | if (GNUNET_OK != testSignVerify ()) | ||
223 | failure_count++; | ||
224 | GNUNET_free (key); | ||
225 | perf_keygen (); | ||
226 | |||
227 | if (0 != failure_count) | ||
228 | { | ||
229 | fprintf (stderr, | ||
230 | "\n\n%d TESTS FAILED!\n\n", | ||
231 | failure_count); | ||
232 | return -1; | ||
233 | } | ||
234 | return 0; | ||
235 | } | ||
236 | |||
237 | /* end of test_crypto_ecdsa.c */ | ||