2 This file is part of GNUnet.
3 (C) 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file util/test_crypto_ecc.c
23 * @brief testcase for ECC public key crypto
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_signatures.h"
31 #define TESTSTRING "Hello World\0"
32 #define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
34 #define KEYFILE "/tmp/test-gnunet-crypto-ecc.key"
36 #define PERF GNUNET_YES
38 static struct GNUNET_CRYPTO_EccPrivateKey *key;
44 struct GNUNET_CRYPTO_EccSignature sig;
45 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
46 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
48 struct GNUNET_TIME_Absolute start;
51 FPRINTF (stderr, "%s", "W");
52 GNUNET_CRYPTO_ecc_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);
57 for (i = 0; i < ITER; i++)
59 FPRINTF (stderr, "%s", ".");
60 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
62 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
67 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
70 printf ("GNUNET_CRYPTO_ecc_verify failed!\n");
75 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
78 printf ("GNUNET_CRYPTO_ecc_verify failed to fail!\n");
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));
91 testSignPerformance ()
93 struct GNUNET_CRYPTO_EccSignaturePurpose purp;
94 struct GNUNET_CRYPTO_EccSignature sig;
95 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded 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_ecc_key_get_public (key, &pkey);
104 start = GNUNET_TIME_absolute_get ();
105 for (i = 0; i < ITER; i++)
107 FPRINTF (stderr, "%s", ".");
108 if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
110 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
115 printf ("%d ECC sign operations %llu ms\n", ITER,
117 GNUNET_TIME_absolute_get_duration (start).rel_value);
124 testCreateFromFile ()
126 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p1;
127 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p2;
129 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
130 GNUNET_assert (NULL != key);
131 GNUNET_CRYPTO_ecc_key_get_public (key, &p1);
132 GNUNET_CRYPTO_ecc_key_free (key);
133 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
134 GNUNET_assert (NULL != key);
135 GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
136 GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
137 GNUNET_CRYPTO_ecc_key_free (key);
138 GNUNET_assert (0 == UNLINK (KEYFILE));
139 key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
140 GNUNET_assert (NULL != key);
141 GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
142 GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
149 struct GNUNET_CRYPTO_EccPrivateKey *pk,
152 const char *txt = cls;
153 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub1;
154 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub2;
156 GNUNET_assert (0 == strcmp ("ok", txt));
157 GNUNET_CRYPTO_ecc_key_get_public (pk, &pub1);
158 GNUNET_CRYPTO_ecc_key_get_public (key, &pub2);
159 GNUNET_assert (0 == memcmp (&pub1, &pub2,
161 GNUNET_CRYPTO_ecc_key_free (pk);
166 test_async_creation (void *cls,
167 const struct GNUNET_SCHEDULER_TaskContext *tc)
169 struct GNUNET_CRYPTO_EccKeyGenerationContext *gc;
171 gc = GNUNET_CRYPTO_ecc_key_create_start (KEYFILE,
174 GNUNET_CRYPTO_ecc_key_create_stop (gc);
175 gc = GNUNET_CRYPTO_ecc_key_create_start (KEYFILE,
182 main (int argc, char *argv[])
184 int failureCount = 0;
186 if (!gcry_check_version ("1.5.0"))
190 ("libgcrypt has not the expected version (version %s is required).\n"),
194 GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
195 GNUNET_CRYPTO_random_disable_entropy_gathering ();
196 if (GNUNET_OK != testCreateFromFile ())
198 GNUNET_SCHEDULER_run (&test_async_creation, NULL);
200 if (GNUNET_OK != testSignPerformance ())
203 if (GNUNET_OK != testSignVerify ())
205 GNUNET_CRYPTO_ecc_key_free (key);
206 GNUNET_assert (0 == UNLINK (KEYFILE));
208 if (failureCount != 0)
210 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
216 /* end of test_crypto_ecc.c */