converting to GNUNET_LOG_from*
[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 =
42     "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8c82b755bc02882b44950c4acdc1672ba74c3b94d81a4c1ea3d74e7700ae5594c3a4f3c559e4bff2df6844fac302e4b66175e14dc8bad3ce44281d2fec1a1abef06301010000";
43   GNUNET_HashCode in;
44   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
45   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
46   int i;
47   char out[3];
48
49   fprintf (stderr, "Testing KBlock key correctness");
50   GNUNET_CRYPTO_hash ("X", strlen ("X"), &in);
51   hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
52   if (hostkey == NULL)
53     {
54       GNUNET_break (0);
55       return GNUNET_SYSERR;
56     }
57   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
58   GNUNET_CRYPTO_rsa_key_free (hostkey);
59 #if 0
60   for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
61        i++)
62     printf ("%02x", ((unsigned char *) &pkey)[i]);
63   printf ("\n");
64 #endif
65   for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);
66        i++)
67     {
68       snprintf (out, sizeof (out), "%02x", ((unsigned char *) &pkey)[i]);
69       if (0 != strncmp (out, &want[i * 2], 2))
70         {
71           fprintf (stderr, " Failed! Wanted %.2s but got %2s at %d\n",
72                    &want[i * 2], out, i);
73           return GNUNET_SYSERR;
74         }
75     }
76   fprintf (stderr, " OK\n");
77   return GNUNET_OK;
78 }
79
80
81 static int
82 testMultiKey (const char *word)
83 {
84   GNUNET_HashCode in;
85   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
86   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
87   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey1;
88   int i;
89
90   fprintf (stderr, "Testing KBlock key uniqueness (%s) ", word);
91   GNUNET_CRYPTO_hash (word, strlen (word), &in);
92   hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
93   if (hostkey == NULL)
94     {
95       GNUNET_break (0);
96       return GNUNET_SYSERR;
97     }
98   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
99   /*
100    * for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
101    * printf("%02x", ((unsigned char*) &pkey)[i]);
102    * printf("\n"); */
103   GNUNET_CRYPTO_rsa_key_free (hostkey);
104   for (i = 0; i < UNIQUE_ITER; i++)
105     {
106       fprintf (stderr, ".");
107       hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
108       if (hostkey == NULL)
109         {
110           GNUNET_break (0);
111           fprintf (stderr, " ERROR\n");
112           return GNUNET_SYSERR;
113         }
114       GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1);
115       GNUNET_CRYPTO_rsa_key_free (hostkey);
116       if (0 !=
117           memcmp (&pkey, &pkey1,
118                   sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
119         {
120           GNUNET_break (0);
121           fprintf (stderr, " ERROR\n");
122           return GNUNET_SYSERR;
123         }
124     }
125   fprintf (stderr, " OK\n");
126   return GNUNET_OK;
127 }
128
129
130 static int
131 testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
132 {
133   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
134   struct GNUNET_CRYPTO_RsaEncryptedData target;
135   char result[MAX_TESTVAL];
136   int i;
137   struct GNUNET_TIME_Absolute start;
138   int ok;
139
140   fprintf (stderr, "W");
141   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
142
143   ok = 0;
144   start = GNUNET_TIME_absolute_get ();
145   for (i = 0; i < ITER; i++)
146     {
147       fprintf (stderr, ".");
148       if (GNUNET_SYSERR ==
149           GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1,
150                                      &pkey, &target))
151         {
152           fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
153           ok++;
154           continue;
155         }
156       if (-1 ==
157           GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result,
158                                      strlen (TESTSTRING) + 1))
159         {
160           fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
161           ok++;
162           continue;
163         }
164       if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
165         {
166           printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING,
167                   MAX_TESTVAL, result);
168           ok++;
169           continue;
170         }
171     }
172   printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER,
173           (unsigned long long)
174           GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
175   if (ok == 0)
176     return GNUNET_OK;
177   else
178     return GNUNET_SYSERR;
179 }
180
181 static int
182 testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
183 {
184   struct GNUNET_CRYPTO_RsaSignature sig;
185   struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
186   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
187   int i;
188   struct GNUNET_TIME_Absolute start;
189   int ok = GNUNET_OK;
190
191   fprintf (stderr, "W");
192   GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
193   start = GNUNET_TIME_absolute_get ();
194   purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
195   purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
196   for (i = 0; i < ITER; i++)
197     {
198       fprintf (stderr, ".");
199       if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
200         {
201           fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
202           ok = GNUNET_SYSERR;
203           continue;
204         }
205       if (GNUNET_SYSERR ==
206           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp,
207                                     &sig, &pkey))
208         {
209           printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
210           ok = GNUNET_SYSERR;
211           continue;
212         }
213       if (GNUNET_SYSERR !=
214           GNUNET_CRYPTO_rsa_verify
215           (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
216         {
217           printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
218           ok = GNUNET_SYSERR;
219           continue;
220         }
221     }
222   printf ("%d RSA sign/verify operations %llums\n", ITER,
223           (unsigned long long)
224           GNUNET_TIME_absolute_get_duration (start).rel_value);
225   return ok;
226 }
227
228
229 int
230 main (int argc, char *argv[])
231 {
232   int failureCount = 0;
233   GNUNET_HashCode in;
234   struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
235
236   GNUNET_log_setup ("test-crypto-ksk", "WARNING", NULL);
237   if (GNUNET_OK != testCorrectKey ())
238     failureCount++;
239   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in);
240   hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
241   if (hostkey == NULL)
242     {
243       printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n");
244       return 1;
245     }
246   if (GNUNET_OK != testMultiKey ("foo"))
247     failureCount++;
248   if (GNUNET_OK != testMultiKey ("bar"))
249     failureCount++;
250   if (GNUNET_OK != testEncryptDecrypt (hostkey))
251     failureCount++;
252   if (GNUNET_OK != testSignVerify (hostkey))
253     failureCount++;
254   GNUNET_CRYPTO_rsa_key_free (hostkey);
255
256   if (failureCount != 0)
257     {
258       printf ("\n\n%d TESTS FAILED!\n\n", failureCount);
259       return -1;
260     }
261   return 0;
262 }