2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file util/test_crypto_ksk.c
23 * @brief testcase for util/crypto_ksk.c
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_time_lib.h"
32 #define TESTSTRING "Hello World\0"
33 #define MAX_TESTVAL 20
41 const char *want = "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8c82b755bc02882b44950c4acdc1672ba74c3b94d81a4c1ea3d74e7700ae5594c3a4f3c559e4bff2df6844fac302e4b66175e14dc8bad3ce44281d2fec1a1abef06301010000";
43 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
44 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
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);
56 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
57 GNUNET_CRYPTO_rsa_key_free (hostkey);
59 for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
60 printf("%02x", ((unsigned char*) &pkey)[i]);
63 for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
65 snprintf(out, sizeof (out), "%02x", ((unsigned char*) &pkey)[i]);
66 if (0 != strncmp (out, &want[i*2], 2))
69 " Failed! Wanted %.2s but got %2s at %d\n",
75 fprintf (stderr, " OK\n");
81 testMultiKey (const char *word)
84 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
85 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
86 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey1;
89 fprintf (stderr, "Testing KBlock key uniqueness (%s) ", word);
90 GNUNET_CRYPTO_hash (word, strlen (word), &in);
91 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
97 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
99 for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
100 printf("%02x", ((unsigned char*) &pkey)[i]);
102 GNUNET_CRYPTO_rsa_key_free (hostkey);
103 for (i = 0; i < UNIQUE_ITER; i++)
105 fprintf (stderr, ".");
106 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
110 fprintf (stderr, " ERROR\n");
111 return GNUNET_SYSERR;
113 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1);
114 GNUNET_CRYPTO_rsa_key_free (hostkey);
116 memcmp (&pkey, &pkey1,
117 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
120 fprintf (stderr, " ERROR\n");
121 return GNUNET_SYSERR;
124 fprintf (stderr, " OK\n");
130 testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
132 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
133 struct GNUNET_CRYPTO_RsaEncryptedData target;
134 char result[MAX_TESTVAL];
136 struct GNUNET_TIME_Absolute start;
139 fprintf (stderr, "W");
140 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
143 start = GNUNET_TIME_absolute_get ();
144 for (i = 0; i < ITER; i++)
146 fprintf (stderr, ".");
147 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
148 strlen (TESTSTRING) + 1,
151 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
155 if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
157 strlen (TESTSTRING) + 1))
159 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
163 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
165 printf ("%s != %.*s - testEncryptDecrypt failed!\n",
166 TESTSTRING, MAX_TESTVAL, result);
171 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
174 GNUNET_TIME_absolute_get_duration (start).value, ok);
178 return GNUNET_SYSERR;
182 testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
184 struct GNUNET_CRYPTO_RsaSignature sig;
185 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
186 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
188 struct GNUNET_TIME_Absolute start;
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++)
198 fprintf (stderr, ".");
199 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
201 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
206 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
209 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
214 GNUNET_CRYPTO_rsa_verify
215 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
217 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
222 printf ("%d RSA sign/verify operations %llums\n",
225 GNUNET_TIME_absolute_get_duration (start).value);
231 main (int argc, char *argv[])
233 int failureCount = 0;
235 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
237 GNUNET_log_setup ("test-crypto-ksk", "WARNING", NULL);
238 if (GNUNET_OK != testCorrectKey ())
240 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in);
241 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
244 printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n");
247 if (GNUNET_OK != testMultiKey ("foo"))
249 if (GNUNET_OK != testMultiKey ("bar"))
251 if (GNUNET_OK != testEncryptDecrypt (hostkey))
253 if (GNUNET_OK != testSignVerify (hostkey))
255 GNUNET_CRYPTO_rsa_key_free (hostkey);
257 if (failureCount != 0)
259 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);