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 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.
17 * @file util/test_crypto_eddsa.c
18 * @brief testcase for ECC public key crypto
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_signatures.h"
28 #define KEYFILE "/tmp/test-gnunet-crypto-eddsa.key"
30 #define PERF GNUNET_YES
33 static struct GNUNET_CRYPTO_EddsaPrivateKey *key;
39 struct GNUNET_CRYPTO_EddsaSignature sig;
40 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
41 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
43 struct GNUNET_TIME_Absolute start;
46 FPRINTF (stderr, "%s", "W");
47 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
48 start = GNUNET_TIME_absolute_get ();
49 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
50 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
52 for (i = 0; i < ITER; i++)
54 FPRINTF (stderr, "%s", "."); fflush (stderr);
55 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
57 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
62 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
65 printf ("GNUNET_CRYPTO_eddsa_verify failed!\n");
70 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
73 printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
78 printf ("%d EdDSA sign/verify operations %s\n", ITER,
79 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
86 testSignPerformance ()
88 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
89 struct GNUNET_CRYPTO_EddsaSignature sig;
90 struct GNUNET_CRYPTO_EddsaPublicKey pkey;
92 struct GNUNET_TIME_Absolute start;
95 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
96 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
97 FPRINTF (stderr, "%s", "W");
98 GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
99 start = GNUNET_TIME_absolute_get ();
100 for (i = 0; i < ITER; i++)
102 FPRINTF (stderr, "%s", "."); fflush (stderr);
103 if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
105 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
110 printf ("%d EdDSA sign operations %s\n", ITER,
111 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
119 testCreateFromFile ()
121 struct GNUNET_CRYPTO_EddsaPublicKey p1;
122 struct GNUNET_CRYPTO_EddsaPublicKey p2;
124 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
125 GNUNET_assert (NULL != key);
126 GNUNET_CRYPTO_eddsa_key_get_public (key, &p1);
128 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
129 GNUNET_assert (NULL != key);
130 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
131 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
133 GNUNET_assert (0 == UNLINK (KEYFILE));
134 key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
135 GNUNET_assert (NULL != key);
136 GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
137 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
146 struct GNUNET_TIME_Absolute start;
147 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
150 FPRINTF (stderr, "%s", "W");
151 start = GNUNET_TIME_absolute_get ();
154 fprintf (stderr, "."); fflush (stderr);
155 pk = GNUNET_CRYPTO_eddsa_key_create ();
159 fprintf (stderr, ".");
161 printf ("10 EdDSA keys created in %s\n",
162 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
167 main (int argc, char *argv[])
169 int failure_count = 0;
171 if (! gcry_check_version ("1.6.0"))
174 _("libgcrypt has not the expected version (version %s is required).\n"),
178 if (getenv ("GNUNET_GCRYPT_DEBUG"))
179 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
180 GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL);
181 key = GNUNET_CRYPTO_eddsa_key_create ();
183 if (GNUNET_OK != testSignPerformance ())
186 if (GNUNET_OK != testSignVerify ())
189 if (GNUNET_OK != testCreateFromFile ())
191 GNUNET_assert (0 == UNLINK (KEYFILE));
194 if (0 != failure_count)
197 "\n\n%d TESTS FAILED!\n\n",
204 /* end of test_crypto_eddsa.c */