logging fixes, nicer comments
[oweals/gnunet.git] / src / util / test_crypto_ecdsa.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2002-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19
20 */
21 /**
22  * @file util/test_crypto_ecdsa.c
23  * @brief testcase for ECC ECDSA public key crypto
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
29 #include <gcrypt.h>
30
31 #define ITER 25
32
33 #define PERF GNUNET_YES
34
35
36 static struct GNUNET_CRYPTO_EcdsaPrivateKey *key;
37
38
39 static int
40 testSignVerify ()
41 {
42   struct GNUNET_CRYPTO_EcdsaSignature sig;
43   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
44   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
45   int i;
46   struct GNUNET_TIME_Absolute start;
47   int ok = GNUNET_OK;
48
49   FPRINTF (stderr, "%s",  "W");
50   GNUNET_CRYPTO_ecdsa_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);
54
55   for (i = 0; i < ITER; i++)
56   {
57     FPRINTF (stderr, "%s",  "."); fflush (stderr);
58     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
59     {
60       FPRINTF (stderr,
61                "%s",
62                "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
63       ok = GNUNET_SYSERR;
64       continue;
65     }
66     if (GNUNET_SYSERR ==
67         GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
68                                     &pkey))
69     {
70       printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
71       ok = GNUNET_SYSERR;
72       continue;
73     }
74     if (GNUNET_SYSERR !=
75         GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
76                                     &purp, &sig, &pkey))
77     {
78       printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
79       ok = GNUNET_SYSERR;
80       continue;
81     }
82   }
83   printf ("%d ECDSA sign/verify operations %s\n", ITER,
84           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
85   return ok;
86 }
87
88
89 static int
90 testDeriveSignVerify ()
91 {
92   struct GNUNET_CRYPTO_EcdsaSignature sig;
93   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
94   struct GNUNET_CRYPTO_EcdsaPrivateKey *dpriv;
95   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
96   struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
97
98   dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive (key, "test-derive", "test-CTX");
99   GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
100   GNUNET_CRYPTO_ecdsa_public_key_derive (&pkey, "test-derive", "test-CTX", &dpub);
101   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
102   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
103
104   if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig))
105   {
106     FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
107     GNUNET_free (dpriv);
108     return GNUNET_SYSERR;
109   }
110   if (GNUNET_SYSERR ==
111       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
112                                   &purp, &sig,
113                                   &dpub))
114   {
115     printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
116     GNUNET_free (dpriv);
117     return GNUNET_SYSERR;
118   }
119   if (GNUNET_SYSERR !=
120       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
121                                   &purp, &sig,
122                                   &pkey))
123   {
124     printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
125     GNUNET_free (dpriv);
126     return GNUNET_SYSERR;
127   }
128   if (GNUNET_SYSERR !=
129       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
130                                   &purp, &sig, &dpub))
131   {
132     printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
133     GNUNET_free (dpriv);
134     return GNUNET_SYSERR;
135   }
136   GNUNET_free (dpriv);
137   return GNUNET_OK;
138 }
139
140
141 #if PERF
142 static int
143 testSignPerformance ()
144 {
145   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
146   struct GNUNET_CRYPTO_EcdsaSignature sig;
147   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
148   int i;
149   struct GNUNET_TIME_Absolute start;
150   int ok = GNUNET_OK;
151
152   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
153   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
154   FPRINTF (stderr, "%s",  "W");
155   GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
156   start = GNUNET_TIME_absolute_get ();
157   for (i = 0; i < ITER; i++)
158   {
159     FPRINTF (stderr, "%s",  "."); fflush (stderr);
160     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
161     {
162       FPRINTF (stderr, "%s",
163                "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
164       ok = GNUNET_SYSERR;
165       continue;
166     }
167   }
168   printf ("%d ECC sign operations %s\n", ITER,
169           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
170                                                   GNUNET_YES));
171   return ok;
172 }
173 #endif
174
175
176 static void
177 perf_keygen ()
178 {
179   struct GNUNET_TIME_Absolute start;
180   struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
181   int i;
182
183   FPRINTF (stderr, "%s",  "W");
184   start = GNUNET_TIME_absolute_get ();
185   for (i=0;i<10;i++)
186   {
187     fprintf (stderr, "."); fflush (stderr);
188     pk = GNUNET_CRYPTO_ecdsa_key_create ();
189     GNUNET_free (pk);
190   }
191   for (;i<25;i++)
192     fprintf (stderr, ".");
193   fflush (stderr);
194   printf ("10 ECDSA keys created in %s\n",
195           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
196 }
197
198
199 int
200 main (int argc, char *argv[])
201 {
202   int failure_count = 0;
203
204   if (! gcry_check_version ("1.6.0"))
205   {
206     FPRINTF (stderr,
207              _
208              ("libgcrypt has not the expected version (version %s is required).\n"),
209              "1.6.0");
210     return 0;
211   }
212   if (getenv ("GNUNET_GCRYPT_DEBUG"))
213     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
214   GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
215   key = GNUNET_CRYPTO_ecdsa_key_create ();
216   if (GNUNET_OK != testDeriveSignVerify ())
217   {
218     failure_count++;
219     fprintf (stderr,
220              "\n\n%d TESTS FAILED!\n\n", failure_count);
221     return -1;
222   }
223 #if PERF
224   if (GNUNET_OK != testSignPerformance ())
225     failure_count++;
226 #endif
227   if (GNUNET_OK != testSignVerify ())
228     failure_count++;
229   GNUNET_free (key);
230   perf_keygen ();
231
232   if (0 != failure_count)
233   {
234     fprintf (stderr,
235              "\n\n%d TESTS FAILED!\n\n",
236              failure_count);
237     return -1;
238   }
239   return 0;
240 }
241
242 /* end of test_crypto_ecdsa.c */