d5a965d4234b4e456ab95d6c47663dd5889b7286
[oweals/gnunet.git] / src / util / test_crypto_ksk.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/test_crypto_ksk.c
23  * @brief testcase for util/crypto_ksk.c
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_time_lib.h"
31
32 #define TESTSTRING "Hello World\0"
33 #define MAX_TESTVAL 20
34 #define UNIQUE_ITER 6
35 #define ITER 25
36
37
38 static int
39 testCorrectKey ()
40 {
41   const char *want = "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8e5e1396e5e5a4ed694fb1d45e15ae68d8756e525cbaf6ab6ed0269ac402f2a6b8a73627e3797496b43a851271cb7d7b60b6acf4324ba72be5cafcef98dca8d71d1b01010000";
42   GNUNET_HashCode in;
43   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
44   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
45   int i;
46   char out[3];
47
48   fprintf (stderr, "Testing KBlock key correctness");
49   GNUNET_CRYPTO_hash ("X", strlen ("X"), &in);
50   hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
51   if (hostkey == NULL)
52     {
53       GNUNET_break (0);
54       return GNUNET_SYSERR;
55     }
56   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
57   GNUNET_CRYPTO_rsa_key_free (hostkey);
58   for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
59     {
60       snprintf(out, sizeof (out), "%02x", ((unsigned char*) &pkey)[i]);
61       if (0 != strncmp (out, &want[i*2], 2))
62         {
63           fprintf (stderr,
64                    "Wanted %.2s but got %2s\n",
65                    &want[i*2],
66                    out);
67           return GNUNET_SYSERR;
68         }
69     }
70   fprintf (stderr, " OK\n");
71   return GNUNET_OK;
72 }
73
74
75 static int
76 testMultiKey (const char *word)
77 {
78   GNUNET_HashCode in;
79   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
80   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
81   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey1;
82   int i;
83
84   fprintf (stderr, "Testing KBlock key uniqueness (%s) ", word);
85   GNUNET_CRYPTO_hash (word, strlen (word), &in);
86   hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
87   if (hostkey == NULL)
88     {
89       GNUNET_break (0);
90       return GNUNET_SYSERR;
91     }
92   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
93   /*
94      for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
95      printf("%02x", ((unsigned char*) &pkey)[i]);
96      printf("\n"); */
97   GNUNET_CRYPTO_rsa_key_free (hostkey);
98   for (i = 0; i < UNIQUE_ITER; i++)
99     {
100       fprintf (stderr, ".");
101       hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
102       if (hostkey == NULL)
103         {
104           GNUNET_break (0);
105           fprintf (stderr, " ERROR\n");
106           return GNUNET_SYSERR;
107         }
108       GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1);
109       GNUNET_CRYPTO_rsa_key_free (hostkey);
110       if (0 !=
111           memcmp (&pkey, &pkey1,
112                   sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
113         {
114           GNUNET_break (0);
115           fprintf (stderr, " ERROR\n");
116           return GNUNET_SYSERR;
117         }
118     }
119   fprintf (stderr, " OK\n");
120   return GNUNET_OK;
121 }
122
123
124 static int
125 testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
126 {
127   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
128   struct GNUNET_CRYPTO_RsaEncryptedData target;
129   char result[MAX_TESTVAL];
130   int i;
131   struct GNUNET_TIME_Absolute start;
132   int ok;
133
134   fprintf (stderr, "W");
135   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
136
137   ok = 0;
138   start = GNUNET_TIME_absolute_get ();
139   for (i = 0; i < ITER; i++)
140     {
141       fprintf (stderr, ".");
142       if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
143                                                       strlen (TESTSTRING) + 1,
144                                                       &pkey, &target))
145         {
146           fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
147           ok++;
148           continue;
149         }
150       if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
151                                            &target, result,
152                                            strlen (TESTSTRING) + 1))
153         {
154           fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
155           ok++;
156           continue;
157         }
158       if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
159         {
160           printf ("%s != %.*s - testEncryptDecrypt failed!\n",
161                   TESTSTRING, MAX_TESTVAL, result);
162           ok++;
163           continue;
164         }
165     }
166   printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
167           ITER,
168           (unsigned long long)
169           GNUNET_TIME_absolute_get_duration (start).value, ok);
170   if (ok == 0)
171     return GNUNET_OK;
172   else
173     return GNUNET_SYSERR;
174 }
175
176 static int
177 testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
178 {
179   struct GNUNET_CRYPTO_RsaSignature sig;
180   struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
181   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
182   int i;
183   struct GNUNET_TIME_Absolute start;
184   int ok = GNUNET_OK;
185
186   fprintf (stderr, "W");
187   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
188   start = GNUNET_TIME_absolute_get ();
189   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
190   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
191   for (i = 0; i < ITER; i++)
192     {
193       fprintf (stderr, ".");
194       if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
195         {
196           fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
197           ok = GNUNET_SYSERR;
198           continue;
199         }
200       if (GNUNET_SYSERR ==
201           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
202                                     &purp, &sig, &pkey))
203         {
204           printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
205           ok = GNUNET_SYSERR;
206           continue;
207         }
208       if (GNUNET_SYSERR !=
209           GNUNET_CRYPTO_rsa_verify
210           (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
211         {
212           printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
213           ok = GNUNET_SYSERR;
214           continue;
215         }
216     }
217   printf ("%d RSA sign/verify operations %llums\n",
218           ITER,
219           (unsigned long long)
220           GNUNET_TIME_absolute_get_duration (start).value);
221   return ok;
222 }
223
224
225 int
226 main (int argc, char *argv[])
227 {
228   int failureCount = 0;
229   GNUNET_HashCode in;
230   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
231
232   GNUNET_log_setup ("test-crypto-ksk", "WARNING", NULL);
233   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in);
234   hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
235   if (hostkey == NULL)
236     {
237       printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n");
238       return 1;
239     }
240
241   if (GNUNET_OK != testCorrectKey ())
242     failureCount++;
243   if (GNUNET_OK != testMultiKey ("foo"))
244     failureCount++;
245   if (GNUNET_OK != testMultiKey ("bar"))
246     failureCount++;
247   if (GNUNET_OK != testEncryptDecrypt (hostkey))
248     failureCount++;
249   if (GNUNET_OK != testSignVerify (hostkey))
250     failureCount++;
251   GNUNET_CRYPTO_rsa_key_free (hostkey);
252
253   if (failureCount != 0)
254     {
255       printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
256       return -1;
257     }
258   return 0;
259 }