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_eddsa.c | |
parent | 9351b1e9bdf2b067b6db06562c26ba658cff42b8 (diff) | |
download | gnunet-4add4c6a6302ff01539e246f68aaf13299414fd6.tar.gz gnunet-4add4c6a6302ff01539e246f68aaf13299414fd6.zip |
-updated tests
Diffstat (limited to 'src/util/test_crypto_eddsa.c')
-rw-r--r-- | src/util/test_crypto_eddsa.c | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/src/util/test_crypto_eddsa.c b/src/util/test_crypto_eddsa.c new file mode 100644 index 000000000..209eea5c6 --- /dev/null +++ b/src/util/test_crypto_eddsa.c | |||
@@ -0,0 +1,204 @@ | |||
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_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 | |||
38 | static struct GNUNET_CRYPTO_EddsaPrivateKey *key; | ||
39 | |||
40 | |||
41 | static int | ||
42 | testSignVerify () | ||
43 | { | ||
44 | struct GNUNET_CRYPTO_EddsaSignature sig; | ||
45 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | ||
46 | struct GNUNET_CRYPTO_EddsaPublicKey pkey; | ||
47 | int i; | ||
48 | struct GNUNET_TIME_Absolute start; | ||
49 | int ok = GNUNET_OK; | ||
50 | |||
51 | FPRINTF (stderr, "%s", "W"); | ||
52 | GNUNET_CRYPTO_eddsa_key_get_public (key, &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 (i = 0; i < ITER; i++) | ||
58 | { | ||
59 | FPRINTF (stderr, "%s", "."); | ||
60 | if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig)) | ||
61 | { | ||
62 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n"); | ||
63 | ok = GNUNET_SYSERR; | ||
64 | continue; | ||
65 | } | ||
66 | if (GNUNET_SYSERR == | ||
67 | GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig, | ||
68 | &pkey)) | ||
69 | { | ||
70 | printf ("GNUNET_CRYPTO_eddsa_verify failed!\n"); | ||
71 | ok = GNUNET_SYSERR; | ||
72 | continue; | ||
73 | } | ||
74 | if (GNUNET_SYSERR != | ||
75 | GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, | ||
76 | &purp, &sig, &pkey)) | ||
77 | { | ||
78 | printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n"); | ||
79 | ok = GNUNET_SYSERR; | ||
80 | continue; | ||
81 | } | ||
82 | } | ||
83 | printf ("%d ECC 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 | #if PERF | ||
90 | static int | ||
91 | testSignPerformance () | ||
92 | { | ||
93 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | ||
94 | struct GNUNET_CRYPTO_EddsaSignature sig; | ||
95 | struct GNUNET_CRYPTO_EddsaPublicKey pkey; | ||
96 | int i; | ||
97 | struct GNUNET_TIME_Absolute start; | ||
98 | int ok = GNUNET_OK; | ||
99 | |||
100 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); | ||
101 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | ||
102 | FPRINTF (stderr, "%s", "W"); | ||
103 | GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey); | ||
104 | start = GNUNET_TIME_absolute_get (); | ||
105 | for (i = 0; i < ITER; i++) | ||
106 | { | ||
107 | FPRINTF (stderr, "%s", "."); | ||
108 | if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig)) | ||
109 | { | ||
110 | FPRINTF (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n"); | ||
111 | ok = GNUNET_SYSERR; | ||
112 | continue; | ||
113 | } | ||
114 | } | ||
115 | printf ("%d EdDSA sign operations %s\n", ITER, | ||
116 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), | ||
117 | GNUNET_YES)); | ||
118 | return ok; | ||
119 | } | ||
120 | #endif | ||
121 | |||
122 | |||
123 | static int | ||
124 | testCreateFromFile () | ||
125 | { | ||
126 | struct GNUNET_CRYPTO_EddsaPublicKey p1; | ||
127 | struct GNUNET_CRYPTO_EddsaPublicKey p2; | ||
128 | |||
129 | key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE); | ||
130 | GNUNET_assert (NULL != key); | ||
131 | GNUNET_CRYPTO_eddsa_key_get_public (key, &p1); | ||
132 | GNUNET_free (key); | ||
133 | key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE); | ||
134 | GNUNET_assert (NULL != key); | ||
135 | GNUNET_CRYPTO_eddsa_key_get_public (key, &p2); | ||
136 | GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1))); | ||
137 | GNUNET_free (key); | ||
138 | GNUNET_assert (0 == UNLINK (KEYFILE)); | ||
139 | key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE); | ||
140 | GNUNET_assert (NULL != key); | ||
141 | GNUNET_CRYPTO_eddsa_key_get_public (key, &p2); | ||
142 | GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1))); | ||
143 | GNUNET_free (key); | ||
144 | return GNUNET_OK; | ||
145 | } | ||
146 | |||
147 | |||
148 | static void | ||
149 | perf_keygen () | ||
150 | { | ||
151 | struct GNUNET_TIME_Absolute start; | ||
152 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; | ||
153 | int i; | ||
154 | |||
155 | start = GNUNET_TIME_absolute_get (); | ||
156 | for (i=0;i<10;i++) | ||
157 | { | ||
158 | fprintf (stderr, "."); | ||
159 | pk = GNUNET_CRYPTO_eddsa_key_create (); | ||
160 | GNUNET_free (pk); | ||
161 | } | ||
162 | fprintf (stderr, "\n"); | ||
163 | printf ("Creating 10 EdDSA keys took %s\n", | ||
164 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES)); | ||
165 | } | ||
166 | |||
167 | |||
168 | int | ||
169 | main (int argc, char *argv[]) | ||
170 | { | ||
171 | int failure_count = 0; | ||
172 | |||
173 | if (! gcry_check_version ("1.5.0")) | ||
174 | { | ||
175 | FPRINTF (stderr, | ||
176 | _("libgcrypt has not the expected version (version %s is required).\n"), | ||
177 | "1.5.0"); | ||
178 | return 0; | ||
179 | } | ||
180 | GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL); | ||
181 | key = GNUNET_CRYPTO_eddsa_key_create (); | ||
182 | #if PERF | ||
183 | if (GNUNET_OK != testSignPerformance ()) | ||
184 | failure_count++; | ||
185 | #endif | ||
186 | if (GNUNET_OK != testSignVerify ()) | ||
187 | failure_count++; | ||
188 | GNUNET_free (key); | ||
189 | if (GNUNET_OK != testCreateFromFile ()) | ||
190 | failure_count++; | ||
191 | GNUNET_assert (0 == UNLINK (KEYFILE)); | ||
192 | perf_keygen (); | ||
193 | |||
194 | if (0 != failure_count) | ||
195 | { | ||
196 | fprintf (stderr, | ||
197 | "\n\n%d TESTS FAILED!\n\n", | ||
198 | failure_count); | ||
199 | return -1; | ||
200 | } | ||
201 | return 0; | ||
202 | } | ||
203 | |||
204 | /* end of test_crypto_eddsa.c */ | ||