error handling
[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   struct GNUNET_TIME_Absolute start;
48   int ok = GNUNET_OK;
49
50   fprintf (stderr, "%s", "W");
51   GNUNET_CRYPTO_eddsa_key_get_public (key, &pkey);
52   start = GNUNET_TIME_absolute_get ();
53   purp.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
54   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
55
56   for (unsigned int i = 0; i < ITER; i++)
57   {
58     fprintf (stderr, "%s", "."); fflush (stderr);
59     if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_sign (key, &purp, &sig))
60     {
61       fprintf (stderr, "%s", "GNUNET_CRYPTO_eddsa_sign returned SYSERR\n");
62       ok = GNUNET_SYSERR;
63       continue;
64     }
65     if (GNUNET_SYSERR ==
66         GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
67                                     &pkey))
68     {
69       printf ("GNUNET_CRYPTO_eddsa_verify failed!\n");
70       ok = GNUNET_SYSERR;
71       continue;
72     }
73     if (GNUNET_SYSERR !=
74         GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
75                                     &purp, &sig, &pkey))
76     {
77       printf ("GNUNET_CRYPTO_eddsa_verify failed to fail!\n");
78       ok = GNUNET_SYSERR;
79       continue;
80     }
81   }
82   fprintf (stderr, "\n");
83   printf ("%d EdDSA 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 #if PERF
91 static int
92 testSignPerformance ()
93 {
94   struct GNUNET_CRYPTO_EccSignaturePurpose purp;
95   struct GNUNET_CRYPTO_EddsaSignature sig;
96   struct GNUNET_CRYPTO_EddsaPublicKey pkey;
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 (unsigned int 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   fprintf (stderr, "\n");
116   printf ("%d EdDSA sign operations %s\n", ITER,
117           GNUNET_STRINGS_relative_time_to_string (
118             GNUNET_TIME_absolute_get_duration (start),
119             GNUNET_YES));
120   return ok;
121 }
122
123
124 #endif
125
126
127 static int
128 testCreateFromFile ()
129 {
130   struct GNUNET_CRYPTO_EddsaPublicKey p1;
131   struct GNUNET_CRYPTO_EddsaPublicKey p2;
132
133   key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
134   GNUNET_assert (NULL != key);
135   GNUNET_CRYPTO_eddsa_key_get_public (key, &p1);
136   GNUNET_free (key);
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   GNUNET_assert (0 == unlink (KEYFILE));
143   key = GNUNET_CRYPTO_eddsa_key_create_from_file (KEYFILE);
144   GNUNET_assert (NULL != key);
145   GNUNET_CRYPTO_eddsa_key_get_public (key, &p2);
146   GNUNET_assert (0 != memcmp (&p1, &p2, sizeof(p1)));
147   GNUNET_free (key);
148   return GNUNET_OK;
149 }
150
151
152 static void
153 perf_keygen ()
154 {
155   struct GNUNET_TIME_Absolute start;
156   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
157
158   fprintf (stderr, "%s", "W");
159   start = GNUNET_TIME_absolute_get ();
160   for (unsigned int i = 0; i < 10; i++)
161   {
162     fprintf (stderr, "."); fflush (stderr);
163     pk = GNUNET_CRYPTO_eddsa_key_create ();
164     GNUNET_free (pk);
165   }
166   fprintf (stderr, "\n");
167   printf ("10 EdDSA keys created in %s\n",
168           GNUNET_STRINGS_relative_time_to_string (
169             GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
170 }
171
172
173 int
174 main (int argc, char *argv[])
175 {
176   int failure_count = 0;
177
178   if (! gcry_check_version ("1.6.0"))
179   {
180     fprintf (stderr,
181              _ (
182                "libgcrypt has not the expected version (version %s is required).\n"),
183              "1.6.0");
184     return 0;
185   }
186   if (getenv ("GNUNET_GCRYPT_DEBUG"))
187     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
188   GNUNET_log_setup ("test-crypto-eddsa", "WARNING", NULL);
189   key = GNUNET_CRYPTO_eddsa_key_create ();
190 #if PERF
191   if (GNUNET_OK != testSignPerformance ())
192     failure_count++;
193 #endif
194   if (GNUNET_OK != testSignVerify ())
195     failure_count++;
196   GNUNET_free (key);
197   if (GNUNET_OK != testCreateFromFile ())
198     failure_count++;
199   GNUNET_assert (0 == unlink (KEYFILE));
200   perf_keygen ();
201
202   if (0 != failure_count)
203   {
204     fprintf (stderr,
205              "\n\n%d TESTS FAILED!\n\n",
206              failure_count);
207     return -1;
208   }
209   return 0;
210 }
211
212
213 /* end of test_crypto_eddsa.c */