first batch of license fixes (boring)
[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 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_eddsa.c
18  * @brief testcase for ECC 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 KEYFILE "/tmp/test-gnunet-crypto-eddsa.key"
29
30 #define PERF GNUNET_YES
31
32
33 static struct GNUNET_CRYPTO_EddsaPrivateKey *key;
34
35
36 static int
37 testSignVerify ()
38 {
39   struct GNUNET_CRYPTO_EddsaSignature sig;
40   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
41   struct GNUNET_CRYPTO_EddsaPublicKey pkey;
42   int i;
43   struct GNUNET_TIME_Absolute start;
44   int ok = GNUNET_OK;
45
46   FPRINTF (stderr, "%s",  "W");
47   GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
48   start = GNUNET_TIME_absolute_get ();
49   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
50   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
51
52   for (i = 0; i < ITER; i++)
53   {
54     FPRINTF (stderr, "%s",  "."); fflush (stderr);
55     if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
56     {
57       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
58       ok = GNUNET_SYSERR;
59       continue;
60     }
61     if (GNUNET_SYSERR ==
62         GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
63                                   &pkey))
64     {
65       printf ("GNUNET_CRYPTO_eddsa_verify failed!\n");
66       ok = GNUNET_SYSERR;
67       continue;
68     }
69     if (GNUNET_SYSERR !=
70         GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
71                                   &purp, &sig, &pkey))
72     {
73       printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
74       ok = GNUNET_SYSERR;
75       continue;
76     }
77   }
78   printf ("%d EdDSA 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 #if PERF
85 static int
86 testSignPerformance ()
87 {
88   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
89   struct GNUNET_CRYPTO_EddsaSignature sig;
90   struct GNUNET_CRYPTO_EddsaPublicKey pkey;
91   int i;
92   struct GNUNET_TIME_Absolute start;
93   int ok = GNUNET_OK;
94
95   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
96   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
97   FPRINTF (stderr, "%s",  "W");
98   GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
99   start = GNUNET_TIME_absolute_get ();
100   for (i = 0; i < ITER; i++)
101   {
102     FPRINTF (stderr, "%s",  "."); fflush (stderr);
103     if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
104     {
105       FPRINTF (stderr, "%s",  "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
106       ok = GNUNET_SYSERR;
107       continue;
108     }
109   }
110   printf ("%d EdDSA sign operations %s\n", ITER,
111           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
112                                                   GNUNET_YES));
113   return ok;
114 }
115 #endif
116
117
118 static int
119 testCreateFromFile ()
120 {
121   struct GNUNET_CRYPTO_EddsaPublicKey p1;
122   struct GNUNET_CRYPTO_EddsaPublicKey p2;
123
124   key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
125   GNUNET_assert (NULL != key);
126   GNUNET_CRYPTO_eddsa_key_get_public (key, &p1);
127   GNUNET_free (key);
128   key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
129   GNUNET_assert (NULL != key);
130   GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
131   GNUNET_assert (0 == memcmp (&p1, &p2, sizeof (p1)));
132   GNUNET_free (key);
133   GNUNET_assert (0 == UNLINK (KEYFILE));
134   key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
135   GNUNET_assert (NULL != key);
136   GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
137   GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
138   GNUNET_free (key);
139   return GNUNET_OK;
140 }
141
142
143 static void
144 perf_keygen ()
145 {
146   struct GNUNET_TIME_Absolute start;
147   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
148   int i;
149
150   FPRINTF (stderr, "%s",  "W");
151   start = GNUNET_TIME_absolute_get ();
152   for (i=0;i<10;i++)
153   {
154     fprintf (stderr, "."); fflush (stderr);
155     pk = GNUNET_CRYPTO_eddsa_key_create ();
156     GNUNET_free (pk);
157   }
158   for (;i<25;i++)
159     fprintf (stderr, ".");
160   fflush (stderr);
161   printf ("10 EdDSA keys created in %s\n",
162           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
163 }
164
165
166 int
167 main (int argc, char *argv[])
168 {
169   int failure_count = 0;
170
171   if (! gcry_check_version ("1.6.0"))
172   {
173     FPRINTF (stderr,
174              _("libgcrypt has not the expected version (version %s is required).\n"),
175              "1.6.0");
176     return 0;
177   }
178   if (getenv ("GNUNET_GCRYPT_DEBUG"))
179     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
180   GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL);
181   key = GNUNET_CRYPTO_eddsa_key_create ();
182 #if PERF
183   if (GNUNET_OK != testSignPerformance ())
184     failure_count++;
185 #endif
186   if (GNUNET_OK != testSignVerify ())
187     failure_count++;
188   GNUNET_free (key);
189   if (GNUNET_OK != testCreateFromFile ())
190     failure_count++;
191   GNUNET_assert (0 == UNLINK (KEYFILE));
192   perf_keygen ();
193
194   if (0 != failure_count)
195   {
196     fprintf (stderr,
197              "\n\n%d TESTS FAILED!\n\n",
198              failure_count);
199     return -1;
200   }
201   return 0;
202 }
203
204 /* end of test_crypto_eddsa.c */