-remove async ecc key generation, not needed
[oweals/gnunet.git] / src / util / test_crypto_ecc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19
20 */
21 /**
22  * @file util/test_crypto_ecc.c
23  * @brief testcase for ECC public key crypto
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_signatures.h"
30 #include <gcrypt.h>
31
32 #define TESTSTRING "Hello World\0"
33 #define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
34 #define ITER 25
35 #define KEYFILE "/tmp/test-gnunet-crypto-ecc.key"
36
37 #define PERF GNUNET_YES
38
39 static struct GNUNET_CRYPTO_EccPrivateKey *key;
40
41
42 static int
43 testSignVerify ()
44 {
45   struct GNUNET_CRYPTO_EccSignature sig;
46   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
47   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
48   int i;
49   struct GNUNET_TIME_Absolute start;
50   int ok = GNUNET_OK;
51
52   FPRINTF (stderr, "%s",  "W");
53   GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
54   start = GNUNET_TIME_absolute_get ();
55   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
56   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
57
58   for (i = 0; i < ITER; i++)
59   {
60     FPRINTF (stderr, "%s",  ".");
61     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
62     {
63       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
64       ok = GNUNET_SYSERR;
65       continue;
66     }
67     if (GNUNET_SYSERR ==
68         GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
69                                   &pkey))
70     {
71       printf ("GNUNET_CRYPTO_ecc_verify failed!\n");
72       ok = GNUNET_SYSERR;
73       continue;
74     }
75     if (GNUNET_SYSERR !=
76         GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
77                                   &purp, &sig, &pkey))
78     {
79       printf ("GNUNET_CRYPTO_ecc_verify failed to fail!\n");
80       ok = GNUNET_SYSERR;
81       continue;
82     }
83   }
84   printf ("%d ECC sign/verify operations %s\n", ITER,
85           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
86   return ok;
87 }
88
89
90 #if PERF
91 static int
92 testSignPerformance ()
93 {
94   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
95   struct GNUNET_CRYPTO_EccSignature sig;
96   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pkey;
97   int i;
98   struct GNUNET_TIME_Absolute start;
99   int ok = GNUNET_OK;
100
101   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
102   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
103   FPRINTF (stderr, "%s",  "W");
104   GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
105   start = GNUNET_TIME_absolute_get ();
106   for (i = 0; i < ITER; i++)
107   {
108     FPRINTF (stderr, "%s",  ".");
109     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_sign (key, &purp, &sig))
110     {
111       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_ecc_sign returned SYSERR\n");
112       ok = GNUNET_SYSERR;
113       continue;
114     }
115   }
116   printf ("%d ECC sign operations %llu ms\n", ITER,
117           (unsigned long long)
118           GNUNET_TIME_absolute_get_duration (start).rel_value);
119   return ok;
120 }
121 #endif
122
123
124 static int
125 testCreateFromFile ()
126 {
127   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p1;
128   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded p2;
129
130   key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
131   GNUNET_assert (NULL != key);
132   GNUNET_CRYPTO_ecc_key_get_public (key, &p1);
133   GNUNET_CRYPTO_ecc_key_free (key);
134   key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
135   GNUNET_assert (NULL != key);
136   GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
137   GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
138   GNUNET_CRYPTO_ecc_key_free (key);
139   GNUNET_assert (0 == UNLINK (KEYFILE));
140   key = GNUNET_CRYPTO_ecc_key_create_from_file (KEYFILE);
141   GNUNET_assert (NULL != key);
142   GNUNET_CRYPTO_ecc_key_get_public (key, &p2);
143   GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
144   return GNUNET_OK;
145 }
146
147
148 static void
149 test_ecdh ()
150 {
151   struct GNUNET_CRYPTO_EccPrivateKey *priv1;
152   struct GNUNET_CRYPTO_EccPrivateKey *priv2;
153   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub1;
154   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub2;
155   struct GNUNET_HashCode ecdh1;
156   struct GNUNET_HashCode ecdh2;
157
158   priv1 = GNUNET_CRYPTO_ecc_key_create ();
159   priv2 = GNUNET_CRYPTO_ecc_key_create ();
160   GNUNET_CRYPTO_ecc_key_get_public (priv1, &pub1);
161   GNUNET_CRYPTO_ecc_key_get_public (priv2, &pub2);
162   GNUNET_CRYPTO_ecc_ecdh (priv1, &pub2, &ecdh1);
163   GNUNET_CRYPTO_ecc_ecdh (priv2, &pub1, &ecdh2);
164   GNUNET_assert (0 == memcmp (&ecdh1, &ecdh2,
165                               sizeof (struct GNUNET_HashCode)));
166   GNUNET_CRYPTO_ecc_key_free (priv1);
167   GNUNET_CRYPTO_ecc_key_free (priv2);
168 }
169
170
171 static void
172 perf_keygen ()
173 {
174   struct GNUNET_TIME_Absolute start;
175   struct GNUNET_CRYPTO_EccPrivateKey *pk;
176   int i;
177
178   start = GNUNET_TIME_absolute_get ();
179   for (i=0;i<10;i++)
180   {
181     fprintf (stderr, ".");
182     pk = GNUNET_CRYPTO_ecc_key_create ();
183     GNUNET_CRYPTO_ecc_key_free (pk);
184   }
185   fprintf (stderr, "\n");
186   printf ("Creating 10 ECC keys took %s\n",
187           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
188 }
189
190
191 int
192 main (int argc, char *argv[])
193 {
194   int failureCount = 0;
195
196   if (!gcry_check_version ("1.5.0"))
197   {
198     FPRINTF (stderr,
199              _
200              ("libgcrypt has not the expected version (version %s is required).\n"),
201              "1.5.0");
202     return 0;
203   }
204   GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
205   if (GNUNET_OK != testCreateFromFile ())
206     failureCount++;
207 #if PERF
208   if (GNUNET_OK != testSignPerformance ())
209     failureCount++;
210 #endif
211   if (GNUNET_OK != testSignVerify ())
212     failureCount++;
213   GNUNET_CRYPTO_ecc_key_free (key);
214   GNUNET_assert (0 == UNLINK (KEYFILE));
215   test_ecdh ();
216   perf_keygen ();
217
218   if (failureCount != 0)
219   {
220     printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
221     return -1;
222   }
223   return 0;
224 }
225
226 /* end of test_crypto_ecc.c */