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