diff options
Diffstat (limited to 'src/util/test_crypto_eddsa.c')
-rw-r--r-- | src/util/test_crypto_eddsa.c | 206 |
1 files changed, 103 insertions, 103 deletions
diff --git a/src/util/test_crypto_eddsa.c b/src/util/test_crypto_eddsa.c index acb5fbdf5..2119e3080 100644 --- a/src/util/test_crypto_eddsa.c +++ b/src/util/test_crypto_eddsa.c | |||
@@ -11,13 +11,13 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | 19 | ||
20 | */ | 20 | */ |
21 | /** | 21 | /** |
22 | * @file util/test_crypto_eddsa.c | 22 | * @file util/test_crypto_eddsa.c |
23 | * @brief testcase for ECC public key crypto | 23 | * @brief testcase for ECC public key crypto |
@@ -39,7 +39,7 @@ static struct GNUNET_CRYPTO_EddsaPrivateKey *key; | |||
39 | 39 | ||
40 | 40 | ||
41 | static int | 41 | static int |
42 | testSignVerify () | 42 | testSignVerify() |
43 | { | 43 | { |
44 | struct GNUNET_CRYPTO_EddsaSignature sig; | 44 | struct GNUNET_CRYPTO_EddsaSignature sig; |
45 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | 45 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; |
@@ -48,47 +48,47 @@ testSignVerify () | |||
48 | struct GNUNET_TIME_Absolute start; | 48 | struct GNUNET_TIME_Absolute start; |
49 | int ok = GNUNET_OK; | 49 | int ok = GNUNET_OK; |
50 | 50 | ||
51 | fprintf (stderr, "%s", "W"); | 51 | fprintf(stderr, "%s", "W"); |
52 | GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey); | 52 | GNUNET_CRYPTO_eddsa_key_get_public(key, &pkey); |
53 | start = GNUNET_TIME_absolute_get (); | 53 | start = GNUNET_TIME_absolute_get(); |
54 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); | 54 | purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); |
55 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | 55 | purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST); |
56 | 56 | ||
57 | for (i = 0; i < ITER; i++) | 57 | for (i = 0; i < ITER; i++) |
58 | { | ||
59 | fprintf (stderr, "%s", "."); fflush (stderr); | ||
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 | { | 58 | { |
78 | printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n"); | 59 | fprintf(stderr, "%s", "."); fflush(stderr); |
79 | ok = GNUNET_SYSERR; | 60 | if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign(key, &purp, &sig)) |
80 | continue; | 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 | } | ||
81 | } | 82 | } |
82 | } | 83 | printf("%d EdDSA sign/verify operations %s\n", ITER, |
83 | printf ("%d EdDSA sign/verify operations %s\n", ITER, | 84 | GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES)); |
84 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES)); | ||
85 | return ok; | 85 | return ok; |
86 | } | 86 | } |
87 | 87 | ||
88 | 88 | ||
89 | #if PERF | 89 | #if PERF |
90 | static int | 90 | static int |
91 | testSignPerformance () | 91 | testSignPerformance() |
92 | { | 92 | { |
93 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; | 93 | struct GNUNET_CRYPTO_EccSignaturePurpose purp; |
94 | struct GNUNET_CRYPTO_EddsaSignature sig; | 94 | struct GNUNET_CRYPTO_EddsaSignature sig; |
@@ -97,112 +97,112 @@ testSignPerformance () | |||
97 | struct GNUNET_TIME_Absolute start; | 97 | struct GNUNET_TIME_Absolute start; |
98 | int ok = GNUNET_OK; | 98 | int ok = GNUNET_OK; |
99 | 99 | ||
100 | purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)); | 100 | purp.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); |
101 | purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST); | 101 | purp.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_TEST); |
102 | fprintf (stderr, "%s", "W"); | 102 | fprintf(stderr, "%s", "W"); |
103 | GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey); | 103 | GNUNET_CRYPTO_eddsa_key_get_public(key, &pkey); |
104 | start = GNUNET_TIME_absolute_get (); | 104 | start = GNUNET_TIME_absolute_get(); |
105 | for (i = 0; i < ITER; i++) | 105 | for (i = 0; i < ITER; i++) |
106 | { | ||
107 | fprintf (stderr, "%s", "."); fflush (stderr); | ||
108 | if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig)) | ||
109 | { | 106 | { |
110 | fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n"); | 107 | fprintf(stderr, "%s", "."); fflush(stderr); |
111 | ok = GNUNET_SYSERR; | 108 | if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign(key, &purp, &sig)) |
112 | continue; | 109 | { |
110 | fprintf(stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n"); | ||
111 | ok = GNUNET_SYSERR; | ||
112 | continue; | ||
113 | } | ||
113 | } | 114 | } |
114 | } | 115 | printf("%d EdDSA sign operations %s\n", ITER, |
115 | printf ("%d EdDSA sign operations %s\n", ITER, | 116 | GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), |
116 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), | 117 | GNUNET_YES)); |
117 | GNUNET_YES)); | ||
118 | return ok; | 118 | return ok; |
119 | } | 119 | } |
120 | #endif | 120 | #endif |
121 | 121 | ||
122 | 122 | ||
123 | static int | 123 | static int |
124 | testCreateFromFile () | 124 | testCreateFromFile() |
125 | { | 125 | { |
126 | struct GNUNET_CRYPTO_EddsaPublicKey p1; | 126 | struct GNUNET_CRYPTO_EddsaPublicKey p1; |
127 | struct GNUNET_CRYPTO_EddsaPublicKey p2; | 127 | struct GNUNET_CRYPTO_EddsaPublicKey p2; |
128 | 128 | ||
129 | key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE); | 129 | key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE); |
130 | GNUNET_assert (NULL != key); | 130 | GNUNET_assert(NULL != key); |
131 | GNUNET_CRYPTO_eddsa_key_get_public (key, &p1); | 131 | GNUNET_CRYPTO_eddsa_key_get_public(key, &p1); |
132 | GNUNET_free (key); | 132 | GNUNET_free(key); |
133 | key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE); | 133 | key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE); |
134 | GNUNET_assert (NULL != key); | 134 | GNUNET_assert(NULL != key); |
135 | GNUNET_CRYPTO_eddsa_key_get_public (key, &p2); | 135 | GNUNET_CRYPTO_eddsa_key_get_public(key, &p2); |
136 | GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1))); | 136 | GNUNET_assert(0 == memcmp(&p1, &p2, sizeof(p1))); |
137 | GNUNET_free (key); | 137 | GNUNET_free(key); |
138 | GNUNET_assert (0 == unlink (KEYFILE)); | 138 | GNUNET_assert(0 == unlink(KEYFILE)); |
139 | key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE); | 139 | key = GNUNET_CRYPTO_eddsa_key_create_from_file(KEYFILE); |
140 | GNUNET_assert (NULL != key); | 140 | GNUNET_assert(NULL != key); |
141 | GNUNET_CRYPTO_eddsa_key_get_public (key, &p2); | 141 | GNUNET_CRYPTO_eddsa_key_get_public(key, &p2); |
142 | GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1))); | 142 | GNUNET_assert(0 != memcmp(&p1, &p2, sizeof(p1))); |
143 | GNUNET_free (key); | 143 | GNUNET_free(key); |
144 | return GNUNET_OK; | 144 | return GNUNET_OK; |
145 | } | 145 | } |
146 | 146 | ||
147 | 147 | ||
148 | static void | 148 | static void |
149 | perf_keygen () | 149 | perf_keygen() |
150 | { | 150 | { |
151 | struct GNUNET_TIME_Absolute start; | 151 | struct GNUNET_TIME_Absolute start; |
152 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; | 152 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; |
153 | int i; | 153 | int i; |
154 | 154 | ||
155 | fprintf (stderr, "%s", "W"); | 155 | fprintf(stderr, "%s", "W"); |
156 | start = GNUNET_TIME_absolute_get (); | 156 | start = GNUNET_TIME_absolute_get(); |
157 | for (i=0;i<10;i++) | 157 | for (i = 0; i < 10; i++) |
158 | { | 158 | { |
159 | fprintf (stderr, "."); fflush (stderr); | 159 | fprintf(stderr, "."); fflush(stderr); |
160 | pk = GNUNET_CRYPTO_eddsa_key_create (); | 160 | pk = GNUNET_CRYPTO_eddsa_key_create(); |
161 | GNUNET_free (pk); | 161 | GNUNET_free(pk); |
162 | } | 162 | } |
163 | for (;i<25;i++) | 163 | for (; i < 25; i++) |
164 | fprintf (stderr, "."); | 164 | fprintf(stderr, "."); |
165 | fflush (stderr); | 165 | fflush(stderr); |
166 | printf ("10 EdDSA keys created in %s\n", | 166 | printf("10 EdDSA keys created in %s\n", |
167 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES)); | 167 | GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(start), GNUNET_YES)); |
168 | } | 168 | } |
169 | 169 | ||
170 | 170 | ||
171 | int | 171 | int |
172 | main (int argc, char *argv[]) | 172 | main(int argc, char *argv[]) |
173 | { | 173 | { |
174 | int failure_count = 0; | 174 | int failure_count = 0; |
175 | 175 | ||
176 | if (! gcry_check_version ("1.6.0")) | 176 | if (!gcry_check_version("1.6.0")) |
177 | { | 177 | { |
178 | fprintf (stderr, | 178 | fprintf(stderr, |
179 | _("libgcrypt has not the expected version (version %s is required).\n"), | 179 | _("libgcrypt has not the expected version (version %s is required).\n"), |
180 | "1.6.0"); | 180 | "1.6.0"); |
181 | return 0; | 181 | return 0; |
182 | } | 182 | } |
183 | if (getenv ("GNUNET_GCRYPT_DEBUG")) | 183 | if (getenv("GNUNET_GCRYPT_DEBUG")) |
184 | gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0); | 184 | gcry_control(GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); |
185 | GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL); | 185 | GNUNET_log_setup("test-crypto-eddsa", "WARNING", NULL); |
186 | key = GNUNET_CRYPTO_eddsa_key_create (); | 186 | key = GNUNET_CRYPTO_eddsa_key_create(); |
187 | #if PERF | 187 | #if PERF |
188 | if (GNUNET_OK != testSignPerformance ()) | 188 | if (GNUNET_OK != testSignPerformance()) |
189 | failure_count++; | 189 | failure_count++; |
190 | #endif | 190 | #endif |
191 | if (GNUNET_OK != testSignVerify ()) | 191 | if (GNUNET_OK != testSignVerify()) |
192 | failure_count++; | 192 | failure_count++; |
193 | GNUNET_free (key); | 193 | GNUNET_free(key); |
194 | if (GNUNET_OK != testCreateFromFile ()) | 194 | if (GNUNET_OK != testCreateFromFile()) |
195 | failure_count++; | 195 | failure_count++; |
196 | GNUNET_assert (0 == unlink (KEYFILE)); | 196 | GNUNET_assert(0 == unlink(KEYFILE)); |
197 | perf_keygen (); | 197 | perf_keygen(); |
198 | 198 | ||
199 | if (0 != failure_count) | 199 | if (0 != failure_count) |
200 | { | 200 | { |
201 | fprintf (stderr, | 201 | fprintf(stderr, |
202 | "\n\n%d TESTS FAILED!\n\n", | 202 | "\n\n%d TESTS FAILED!\n\n", |
203 | failure_count); | 203 | failure_count); |
204 | return -1; | 204 | return -1; |
205 | } | 205 | } |
206 | return 0; | 206 | return 0; |
207 | } | 207 | } |
208 | 208 | ||