error handling
[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      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_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 (
85             GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
86   return ok;
87 }
88
89
90 static int
91 testDeriveSignVerify ()
92 {
93   struct GNUNET_CRYPTO_EcdsaSignature sig;
94   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
95   struct GNUNET_CRYPTO_EcdsaPrivateKey *dpriv;
96   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
97   struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
98
99   dpriv = GNUNET_CRYPTO_ecdsa_private_key_derive (key, "test-derive",
100                                                   "test-CTX");
101   GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
102   GNUNET_CRYPTO_ecdsa_public_key_derive (&pkey, "test-derive", "test-CTX",
103                                          &dpub);
104   purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
105   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
106
107   if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (dpriv, &purp, &sig))
108   {
109     fprintf (stderr, "%s", "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
110     GNUNET_free (dpriv);
111     return GNUNET_SYSERR;
112   }
113   if (GNUNET_SYSERR ==
114       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
115                                   &purp, &sig,
116                                   &dpub))
117   {
118     printf ("GNUNET_CRYPTO_ecdsa_verify failed!\n");
119     GNUNET_free (dpriv);
120     return GNUNET_SYSERR;
121   }
122   if (GNUNET_SYSERR !=
123       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
124                                   &purp, &sig,
125                                   &pkey))
126   {
127     printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
128     GNUNET_free (dpriv);
129     return GNUNET_SYSERR;
130   }
131   if (GNUNET_SYSERR !=
132       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
133                                   &purp, &sig, &dpub))
134   {
135     printf ("GNUNET_CRYPTO_ecdsa_verify failed to fail!\n");
136     GNUNET_free (dpriv);
137     return GNUNET_SYSERR;
138   }
139   GNUNET_free (dpriv);
140   return GNUNET_OK;
141 }
142
143
144 #if PERF
145 static int
146 testSignPerformance ()
147 {
148   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
149   struct GNUNET_CRYPTO_EcdsaSignature sig;
150   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
151   int i;
152   struct GNUNET_TIME_Absolute start;
153   int ok = GNUNET_OK;
154
155   purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
156   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
157   fprintf (stderr, "%s", "W");
158   GNUNET_CRYPTO_ecdsa_key_get_public (key, &pkey);
159   start = GNUNET_TIME_absolute_get ();
160   for (i = 0; i < ITER; i++)
161   {
162     fprintf (stderr, "%s", "."); fflush (stderr);
163     if (GNUNET_SYSERR == GNUNET_CRYPTO_ecdsa_sign (key, &purp, &sig))
164     {
165       fprintf (stderr, "%s",
166                "GNUNET_CRYPTO_ecdsa_sign returned SYSERR\n");
167       ok = GNUNET_SYSERR;
168       continue;
169     }
170   }
171   printf ("%d ECC sign operations %s\n", ITER,
172           GNUNET_STRINGS_relative_time_to_string (
173             GNUNET_TIME_absolute_get_duration (start),
174             GNUNET_YES));
175   return ok;
176 }
177
178
179 #endif
180
181
182 static void
183 perf_keygen ()
184 {
185   struct GNUNET_TIME_Absolute start;
186   struct GNUNET_CRYPTO_EcdsaPrivateKey *pk;
187   int i;
188
189   fprintf (stderr, "%s", "W");
190   start = GNUNET_TIME_absolute_get ();
191   for (i = 0; i < 10; i++)
192   {
193     fprintf (stderr, "."); fflush (stderr);
194     pk = GNUNET_CRYPTO_ecdsa_key_create ();
195     GNUNET_free (pk);
196   }
197   for (; i < 25; i++)
198     fprintf (stderr, ".");
199   fflush (stderr);
200   printf ("10 ECDSA keys created in %s\n",
201           GNUNET_STRINGS_relative_time_to_string (
202             GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
203 }
204
205
206 int
207 main (int argc, char *argv[])
208 {
209   int failure_count = 0;
210
211   if (! gcry_check_version ("1.6.0"))
212   {
213     fprintf (stderr,
214              _
215              (
216                "libgcrypt has not the expected version (version %s is required).\n"),
217              "1.6.0");
218     return 0;
219   }
220   if (getenv ("GNUNET_GCRYPT_DEBUG"))
221     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
222   GNUNET_log_setup ("test-crypto-ecc", "WARNING", NULL);
223   key = GNUNET_CRYPTO_ecdsa_key_create ();
224   if (GNUNET_OK != testDeriveSignVerify ())
225   {
226     failure_count++;
227     fprintf (stderr,
228              "\n\n%d TESTS FAILED!\n\n", failure_count);
229     return -1;
230   }
231 #if PERF
232   if (GNUNET_OK != testSignPerformance ())
233     failure_count++;
234 #endif
235   if (GNUNET_OK != testSignVerify ())
236     failure_count++;
237   GNUNET_free (key);
238   perf_keygen ();
239
240   if (0 != failure_count)
241   {
242     fprintf (stderr,
243              "\n\n%d TESTS FAILED!\n\n",
244              failure_count);
245     return -1;
246   }
247   return 0;
248 }
249
250
251 /* end of test_crypto_ecdsa.c */