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_util_lib.h"
29 #include "gnunet_signatures.h"
32 #define TESTSTRING "Hello World\0"
33 #define MAX_TESTVAL 20
42 "010601000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b73c215f7a5e6b09bec55713c901786c09324a150980e014bdb0d04426934929c3b4971a9711af5455536cd6eeb8bfa004ee904972a737455f53c752987d8c82b755bc02882b44950c4acdc1672ba74c3b94d81a4c1ea3d74e7700ae5594c3a4f3c559e4bff2df6844fac302e4b66175e14dc8bad3ce44281d2fec1a1abef06301010000";
43 struct GNUNET_HashCode in;
44 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
45 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
49 FPRINTF (stderr, "%s", "Testing KBlock key correctness");
50 GNUNET_CRYPTO_hash ("X", strlen ("X"), &in);
51 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
57 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
58 GNUNET_CRYPTO_rsa_key_free (hostkey);
60 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); i++)
61 printf ("%02x", ((unsigned char *) &pkey)[i]);
64 for (i = 0; i < sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded); i++)
66 snprintf (out, sizeof (out), "%02x", ((unsigned char *) &pkey)[i]);
67 if (0 != strncmp (out, &want[i * 2], 2))
69 FPRINTF (stderr, " Failed! Wanted %.2s but got %2s at %d\n", &want[i * 2],
74 FPRINTF (stderr, "%s", " OK\n");
80 testMultiKey (const char *word)
82 struct GNUNET_HashCode in;
83 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
84 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
85 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey1;
88 FPRINTF (stderr, "Testing KBlock key uniqueness (%s) ", word);
89 GNUNET_CRYPTO_hash (word, strlen (word), &in);
90 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
96 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
98 * for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
99 * printf("%02x", ((unsigned char*) &pkey)[i]);
101 GNUNET_CRYPTO_rsa_key_free (hostkey);
102 for (i = 0; i < UNIQUE_ITER; i++)
104 FPRINTF (stderr, "%s", ".");
105 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
109 FPRINTF (stderr, "%s", " ERROR\n");
110 return GNUNET_SYSERR;
112 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1);
113 GNUNET_CRYPTO_rsa_key_free (hostkey);
115 memcmp (&pkey, &pkey1,
116 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
119 FPRINTF (stderr, "%s", " ERROR\n");
120 return GNUNET_SYSERR;
123 FPRINTF (stderr, "%s", " OK\n");
129 testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
131 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
132 struct GNUNET_CRYPTO_RsaEncryptedData target;
133 char result[MAX_TESTVAL];
135 struct GNUNET_TIME_Absolute start;
138 FPRINTF (stderr, "%s", "W");
139 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
142 start = GNUNET_TIME_absolute_get ();
143 for (i = 0; i < ITER; i++)
145 FPRINTF (stderr, "%s", ".");
147 GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
150 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
155 GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result,
156 strlen (TESTSTRING) + 1))
158 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
162 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
164 printf ("%s != %.*s - testEncryptDecrypt failed!\n", TESTSTRING,
165 MAX_TESTVAL, result);
170 printf ("%d RSA encrypt/decrypt operations %s (%d failures)\n",
172 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES),
177 return GNUNET_SYSERR;
181 testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
183 struct GNUNET_CRYPTO_RsaSignature sig;
184 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
185 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
187 struct GNUNET_TIME_Absolute start;
190 FPRINTF (stderr, "%s", "W");
191 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
192 start = GNUNET_TIME_absolute_get ();
193 purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
194 purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
195 for (i = 0; i < ITER; i++)
197 FPRINTF (stderr, "%s", ".");
198 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
200 FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
205 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST, &purp, &sig,
208 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
213 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN,
216 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
221 printf ("%d RSA sign/verify operations %s\n",
223 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
229 main (int argc, char *argv[])
231 int failureCount = 0;
232 struct GNUNET_HashCode in;
233 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
235 GNUNET_log_setup ("test-crypto-ksk", "WARNING", NULL);
236 if (GNUNET_OK != testCorrectKey ())
238 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &in);
239 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
242 printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n");
245 if (GNUNET_OK != testMultiKey ("foo"))
247 if (GNUNET_OK != testMultiKey ("bar"))
249 if (GNUNET_OK != testEncryptDecrypt (hostkey))
251 if (GNUNET_OK != testSignVerify (hostkey))
253 GNUNET_CRYPTO_rsa_key_free (hostkey);
255 if (failureCount != 0)
257 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);