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