paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / util / test_crypto_eddsa.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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19 /**
20  * @file util/test_crypto_eddsa.c
21  * @brief testcase for ECC public key crypto
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_signatures.h"
27 #include <gcrypt.h>
28
29 #define ITER 25
30
31 #define KEYFILE "/tmp/test-gnunet-crypto-eddsa.key"
32
33 #define PERF GNUNET_YES
34
35
36 static struct GNUNET_CRYPTO_EddsaPrivateKey *key;
37
38
39 static int
40 testSignVerify ()
41 {
42   struct GNUNET_CRYPTO_EddsaSignature sig;
43   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
44   struct GNUNET_CRYPTO_EddsaPublicKey pkey;
45   int i;
46   struct GNUNET_TIME_Absolute start;
47   int ok = GNUNET_OK;
48
49   FPRINTF (stderr, "%s",  "W");
50   GNUNET_CRYPTO_eddsa_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_eddsa_sign (key, &purp, &sig))
59     {
60       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
61       ok = GNUNET_SYSERR;
62       continue;
63     }
64     if (GNUNET_SYSERR ==
65         GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
66                                   &pkey))
67     {
68       printf ("GNUNET_CRYPTO_eddsa_verify failed!\n");
69       ok = GNUNET_SYSERR;
70       continue;
71     }
72     if (GNUNET_SYSERR !=
73         GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
74                                   &purp, &sig, &pkey))
75     {
76       printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
77       ok = GNUNET_SYSERR;
78       continue;
79     }
80   }
81   printf ("%d EdDSA sign/verify operations %s\n", ITER,
82           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
83   return ok;
84 }
85
86
87 #if PERF
88 static int
89 testSignPerformance ()
90 {
91   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
92   struct GNUNET_CRYPTO_EddsaSignature sig;
93   struct GNUNET_CRYPTO_EddsaPublicKey pkey;
94   int i;
95   struct GNUNET_TIME_Absolute start;
96   int ok = GNUNET_OK;
97
98   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
99   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
100   FPRINTF (stderr, "%s",  "W");
101   GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
102   start = GNUNET_TIME_absolute_get ();
103   for (i = 0; i < ITER; i++)
104   {
105     FPRINTF (stderr, "%s",  "."); fflush (stderr);
106     if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
107     {
108       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
109       ok = GNUNET_SYSERR;
110       continue;
111     }
112   }
113   printf ("%d EdDSA sign operations %s\n", ITER,
114           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
115                                                   GNUNET_YES));
116   return ok;
117 }
118 #endif
119
120
121 static int
122 testCreateFromFile ()
123 {
124   struct GNUNET_CRYPTO_EddsaPublicKey p1;
125   struct GNUNET_CRYPTO_EddsaPublicKey p2;
126
127   key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
128   GNUNET_assert (NULL != key);
129   GNUNET_CRYPTO_eddsa_key_get_public (key, &p1);
130   GNUNET_free (key);
131   key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
132   GNUNET_assert (NULL != key);
133   GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
134   GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
135   GNUNET_free (key);
136   GNUNET_assert (0 == UNLINK (KEYFILE));
137   key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
138   GNUNET_assert (NULL != key);
139   GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
140   GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
141   GNUNET_free (key);
142   return GNUNET_OK;
143 }
144
145
146 static void
147 perf_keygen ()
148 {
149   struct GNUNET_TIME_Absolute start;
150   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
151   int i;
152
153   FPRINTF (stderr, "%s",  "W");
154   start = GNUNET_TIME_absolute_get ();
155   for (i=0;i<10;i++)
156   {
157     fprintf (stderr, "."); fflush (stderr);
158     pk = GNUNET_CRYPTO_eddsa_key_create ();
159     GNUNET_free (pk);
160   }
161   for (;i<25;i++)
162     fprintf (stderr, ".");
163   fflush (stderr);
164   printf ("10 EdDSA keys created in %s\n",
165           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
166 }
167
168
169 int
170 main (int argc, char *argv[])
171 {
172   int failure_count = 0;
173
174   if (! gcry_check_version ("1.6.0"))
175   {
176     FPRINTF (stderr,
177              _("libgcrypt has not the expected version (version %s is required).\n"),
178              "1.6.0");
179     return 0;
180   }
181   if (getenv ("GNUNET_GCRYPT_DEBUG"))
182     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
183   GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL);
184   key = GNUNET_CRYPTO_eddsa_key_create ();
185 #if PERF
186   if (GNUNET_OK != testSignPerformance ())
187     failure_count++;
188 #endif
189   if (GNUNET_OK != testSignVerify ())
190     failure_count++;
191   GNUNET_free (key);
192   if (GNUNET_OK != testCreateFromFile ())
193     failure_count++;
194   GNUNET_assert (0 == UNLINK (KEYFILE));
195   perf_keygen ();
196
197   if (0 != failure_count)
198   {
199     fprintf (stderr,
200              "\n\n%d TESTS FAILED!\n\n",
201              failure_count);
202     return -1;
203   }
204   return 0;
205 }
206
207 /* end of test_crypto_eddsa.c */