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/>.
20 * @file util/test_crypto_eddsa.c
21 * @brief testcase for ECC public key crypto
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_signatures.h"
31 #define KEYFILE "/tmp/test-gnunet-crypto-eddsa.key"
33 #define PERF GNUNET_YES
36 static struct GNUNET_CRYPTO_EddsaPrivateKey *key;
42 struct GNUNET_CRYPTO_EddsaSignature sig;
43 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
44 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
46 struct GNUNET_TIME_Absolute start;
49 FPRINTF (stderr, "%s", "W");
50 GNUNET_CRYPTO_eddsa_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);
55 for (i = 0; i < ITER; i++)
57 FPRINTF (stderr, "%s", "."); fflush (stderr);
58 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
60 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
65 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
68 printf ("GNUNET_CRYPTO_eddsa_verify failed!\n");
73 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
76 printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
81 printf ("%d EdDSA sign/verify operations %s\n", ITER,
82 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
89 testSignPerformance ()
91 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
92 struct GNUNET_CRYPTO_EddsaSignature sig;
93 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
95 struct GNUNET_TIME_Absolute start;
98 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
99 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
100 FPRINTF (stderr, "%s", "W");
101 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
102 start = GNUNET_TIME_absolute_get ();
103 for (i = 0; i < ITER; i++)
105 FPRINTF (stderr, "%s", "."); fflush (stderr);
106 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
108 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
113 printf ("%d EdDSA sign operations %s\n", ITER,
114 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
122 testCreateFromFile ()
124 struct GNUNET_CRYPTO_EddsaPublicKey p1;
125 struct GNUNET_CRYPTO_EddsaPublicKey p2;
127 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
128 GNUNET_assert (NULL != key);
129 GNUNET_CRYPTO_eddsa_key_get_public (key, &p1);
131 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
132 GNUNET_assert (NULL != key);
133 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
134 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
136 GNUNET_assert (0 == UNLINK (KEYFILE));
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)));
149 struct GNUNET_TIME_Absolute start;
150 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
153 FPRINTF (stderr, "%s", "W");
154 start = GNUNET_TIME_absolute_get ();
157 fprintf (stderr, "."); fflush (stderr);
158 pk = GNUNET_CRYPTO_eddsa_key_create ();
162 fprintf (stderr, ".");
164 printf ("10 EdDSA keys created in %s\n",
165 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
170 main (int argc, char *argv[])
172 int failure_count = 0;
174 if (! gcry_check_version ("1.6.0"))
177 _("libgcrypt has not the expected version (version %s is required).\n"),
181 if (getenv ("GNUNET_GCRYPT_DEBUG"))
182 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
183 GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL);
184 key = GNUNET_CRYPTO_eddsa_key_create ();
186 if (GNUNET_OK != testSignPerformance ())
189 if (GNUNET_OK != testSignVerify ())
192 if (GNUNET_OK != testCreateFromFile ())
194 GNUNET_assert (0 == UNLINK (KEYFILE));
197 if (0 != failure_count)
200 "\n\n%d TESTS FAILED!\n\n",
207 /* end of test_crypto_eddsa.c */