2 This file is part of GNUnet.
3 Copyright (C) 2002-2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/test_crypto_eddsa.c
23 * @brief testcase for ECC public key crypto
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
33 #define KEYFILE "/tmp/test-gnunet-crypto-eddsa.key"
35 #define PERF GNUNET_YES
38 static struct GNUNET_CRYPTO_EddsaPrivateKey *key;
44 struct GNUNET_CRYPTO_EddsaSignature sig;
45 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
46 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
47 struct GNUNET_TIME_Absolute start;
50 fprintf (stderr, "%s", "W");
51 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
52 start = GNUNET_TIME_absolute_get ();
53 purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
54 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
56 for (unsigned int i = 0; i < ITER; i++)
58 fprintf (stderr, "%s", "."); fflush (stderr);
59 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
61 fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
66 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
69 printf ("GNUNET_CRYPTO_eddsa_verify failed!\n");
74 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
77 printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
82 fprintf (stderr, "\n");
83 printf ("%d EdDSA sign/verify operations %s\n", ITER,
84 GNUNET_STRINGS_relative_time_to_string (
85 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
92 testSignPerformance ()
94 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
95 struct GNUNET_CRYPTO_EddsaSignature sig;
96 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
97 struct GNUNET_TIME_Absolute start;
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 (unsigned int i = 0; i < ITER; i++)
107 fprintf (stderr, "%s", "."); fflush (stderr);
108 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
110 fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
115 fprintf (stderr, "\n");
116 printf ("%d EdDSA sign operations %s\n", ITER,
117 GNUNET_STRINGS_relative_time_to_string (
118 GNUNET_TIME_absolute_get_duration (start),
128 testCreateFromFile ()
130 struct GNUNET_CRYPTO_EddsaPublicKey p1;
131 struct GNUNET_CRYPTO_EddsaPublicKey p2;
133 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
134 GNUNET_assert (NULL != key);
135 GNUNET_CRYPTO_eddsa_key_get_public (key, &p1);
137 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
138 GNUNET_assert (NULL != key);
139 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
140 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof(p1)));
142 GNUNET_assert (0 == unlink (KEYFILE));
143 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
144 GNUNET_assert (NULL != key);
145 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
146 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof(p1)));
155 struct GNUNET_TIME_Absolute start;
156 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
158 fprintf (stderr, "%s", "W");
159 start = GNUNET_TIME_absolute_get ();
160 for (unsigned int i = 0; i < 10; i++)
162 fprintf (stderr, "."); fflush (stderr);
163 pk = GNUNET_CRYPTO_eddsa_key_create ();
166 fprintf (stderr, "\n");
167 printf ("10 EdDSA keys created in %s\n",
168 GNUNET_STRINGS_relative_time_to_string (
169 GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
174 main (int argc, char *argv[])
176 int failure_count = 0;
178 if (! gcry_check_version ("1.6.0"))
182 "libgcrypt has not the expected version (version %s is required).\n"),
186 if (getenv ("GNUNET_GCRYPT_DEBUG"))
187 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
188 GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL);
189 key = GNUNET_CRYPTO_eddsa_key_create ();
191 if (GNUNET_OK != testSignPerformance ())
194 if (GNUNET_OK != testSignVerify ())
197 if (GNUNET_OK != testCreateFromFile ())
199 GNUNET_assert (0 == unlink (KEYFILE));
202 if (0 != failure_count)
205 "\n\n%d TESTS FAILED!\n\n",
213 /* end of test_crypto_eddsa.c */