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
39 testMultiKey (const char *word)
42 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
43 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
44 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey1;
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);
55 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
57 for (i=0;i<sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded);i++)
58 printf("%02x", ((unsigned char*) &pkey)[i]);
60 GNUNET_CRYPTO_rsa_key_free (hostkey);
61 for (i = 0; i < UNIQUE_ITER; i++)
63 fprintf (stderr, ".");
64 hostkey = GNUNET_CRYPTO_rsa_key_create_from_hash (&in);
68 fprintf (stderr, " ERROR\n");
71 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey1);
72 GNUNET_CRYPTO_rsa_key_free (hostkey);
74 memcmp (&pkey, &pkey1,
75 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
78 fprintf (stderr, " ERROR\n");
82 fprintf (stderr, " OK\n");
88 testEncryptDecrypt (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
90 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
91 struct GNUNET_CRYPTO_RsaEncryptedData target;
92 char result[MAX_TESTVAL];
94 struct GNUNET_TIME_Absolute start;
97 fprintf (stderr, "W");
98 GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
101 start = GNUNET_TIME_absolute_get ();
102 for (i = 0; i < ITER; i++)
104 fprintf (stderr, ".");
105 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
106 strlen (TESTSTRING) + 1,
109 fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
113 if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
115 strlen (TESTSTRING) + 1))
117 fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
121 if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
123 printf ("%s != %.*s - testEncryptDecrypt failed!\n",
124 TESTSTRING, MAX_TESTVAL, result);
129 printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
132 GNUNET_TIME_absolute_get_duration (start).value, ok);
136 return GNUNET_SYSERR;
140 testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
142 struct GNUNET_CRYPTO_RsaSignature sig;
143 struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
144 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
146 struct GNUNET_TIME_Absolute start;
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++)
156 fprintf (stderr, ".");
157 if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
159 fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
164 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
167 printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
172 GNUNET_CRYPTO_rsa_verify
173 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
175 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
180 printf ("%d RSA sign/verify operations %llums\n",
183 GNUNET_TIME_absolute_get_duration (start).value);
189 main (int argc, char *argv[])
191 int failureCount = 0;
193 struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
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);
200 printf ("\nGNUNET_CRYPTO_rsa_key_create_from_hash failed!\n");
204 if (GNUNET_OK != testMultiKey ("foo"))
206 if (GNUNET_OK != testMultiKey ("bar"))
208 if (GNUNET_OK != testEncryptDecrypt (hostkey))
210 if (GNUNET_OK != testSignVerify (hostkey))
212 GNUNET_CRYPTO_rsa_key_free (hostkey);
214 if (failureCount != 0)
216 printf ("\n\n%d TESTS FAILED!\n\n", failureCount);