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