*/
#include "platform.h"
#include "gnunet_common.h"
-#include "gnunet_crypto_lib.h"
+#include "gnunet_util_lib.h"
#include "gnunet_signatures.h"
-#include "gnunet_time_lib.h"
#define TESTSTRING "Hello World\0"
#define MAX_TESTVAL sizeof(struct GNUNET_CRYPTO_AesSessionKey)
#define PERF GNUNET_YES
+static struct GNUNET_CRYPTO_RsaPrivateKey *key;
+
+
static int
testEncryptDecrypt ()
{
- struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
struct GNUNET_CRYPTO_RsaEncryptedData target;
char result[MAX_TESTVAL];
struct GNUNET_TIME_Absolute start;
int ok;
- fprintf (stderr, "W");
- hostkey = GNUNET_CRYPTO_rsa_key_create ();
- GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
-
+ FPRINTF (stderr, "%s", "W");
+ GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
ok = 0;
start = GNUNET_TIME_absolute_get ();
for (i = 0; i < ITER; i++)
{
- fprintf (stderr, ".");
+ FPRINTF (stderr, "%s", ".");
if (GNUNET_SYSERR ==
GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
&target))
{
- fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
+ FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
ok++;
continue;
}
if (-1 ==
- GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, result,
+ GNUNET_CRYPTO_rsa_decrypt (key, &target, result,
strlen (TESTSTRING) + 1))
{
- fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
+ FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
ok++;
continue;
continue;
}
}
- printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n", ITER,
- (unsigned long long)
- GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
+ printf ("%d RSA encrypt/decrypt operations %s (%d failures)\n",
+ ITER,
+ GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES),
+ ok);
if (ok == 0)
return GNUNET_OK;
- else
- return GNUNET_SYSERR;
+ return GNUNET_SYSERR;
}
+
#if PERF
static int
testEncryptPerformance ()
{
- struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
struct GNUNET_CRYPTO_RsaEncryptedData target;
int i;
struct GNUNET_TIME_Absolute start;
int ok;
- fprintf (stderr, "W");
- hostkey = GNUNET_CRYPTO_rsa_key_create ();
- GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
-
+ FPRINTF (stderr, "%s", "W");
+ GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
ok = 0;
start = GNUNET_TIME_absolute_get ();
for (i = 0; i < ITER; i++)
{
- fprintf (stderr, ".");
+ FPRINTF (stderr, "%s", ".");
if (GNUNET_SYSERR ==
GNUNET_CRYPTO_rsa_encrypt (TESTSTRING, strlen (TESTSTRING) + 1, &pkey,
&target))
{
- fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
+ FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
ok++;
continue;
}
printf ("%d RSA encrypt operations %llu ms (%d failures)\n", ITER,
(unsigned long long)
GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
if (ok != 0)
return GNUNET_SYSERR;
return GNUNET_OK;
static int
testEncryptDecryptSK ()
{
- struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
struct GNUNET_CRYPTO_RsaEncryptedData target;
struct GNUNET_CRYPTO_AesSessionKey insk;
struct GNUNET_TIME_Absolute start;
int ok;
- fprintf (stderr, "W");
- hostkey = GNUNET_CRYPTO_rsa_key_create ();
- GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
-
+ FPRINTF (stderr, "%s", "W");
+ GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
ok = 0;
start = GNUNET_TIME_absolute_get ();
for (i = 0; i < ITER; i++)
{
- fprintf (stderr, ".");
+ FPRINTF (stderr, "%s", ".");
GNUNET_CRYPTO_aes_create_session_key (&insk);
if (GNUNET_SYSERR ==
GNUNET_CRYPTO_rsa_encrypt (&insk,
sizeof (struct GNUNET_CRYPTO_AesSessionKey),
&pkey, &target))
{
- fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
+ FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
ok++;
continue;
}
if (-1 ==
- GNUNET_CRYPTO_rsa_decrypt (hostkey, &target, &outsk,
+ GNUNET_CRYPTO_rsa_decrypt (key, &target, &outsk,
sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
{
- fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
+ FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
ok++;
continue;
}
continue;
}
}
- printf ("%d RSA encrypt/decrypt SK operations %llums (%d failures)\n", ITER,
- (unsigned long long)
- GNUNET_TIME_absolute_get_duration (start).rel_value, ok);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
+ printf ("%d RSA encrypt/decrypt SK operations %s (%d failures)\n",
+ ITER,
+ GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES),
+ ok);
if (ok != 0)
return GNUNET_SYSERR;
return GNUNET_OK;
static int
testSignVerify ()
{
- struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
struct GNUNET_CRYPTO_RsaSignature sig;
struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
struct GNUNET_TIME_Absolute start;
int ok = GNUNET_OK;
- fprintf (stderr, "W");
- hostkey = GNUNET_CRYPTO_rsa_key_create ();
- GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
+ FPRINTF (stderr, "%s", "W");
+ GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
start = GNUNET_TIME_absolute_get ();
purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
for (i = 0; i < ITER; i++)
{
- fprintf (stderr, ".");
- if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
+ FPRINTF (stderr, "%s", ".");
+ if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (key, &purp, &sig))
{
- fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
+ FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
ok = GNUNET_SYSERR;
continue;
}
continue;
}
}
- printf ("%d RSA sign/verify operations %llums\n", ITER,
- (unsigned long long)
- GNUNET_TIME_absolute_get_duration (start).rel_value);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
+ printf ("%d RSA sign/verify operations %s\n", ITER,
+ GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start), GNUNET_YES));
return ok;
}
static int
testSignPerformance ()
{
- struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
struct GNUNET_CRYPTO_RsaSignaturePurpose purp;
struct GNUNET_CRYPTO_RsaSignature sig;
struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
purp.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose));
purp.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TEST);
- fprintf (stderr, "W");
- hostkey = GNUNET_CRYPTO_rsa_key_create ();
- GNUNET_CRYPTO_rsa_key_get_public (hostkey, &pkey);
+ FPRINTF (stderr, "%s", "W");
+ GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
start = GNUNET_TIME_absolute_get ();
for (i = 0; i < ITER; i++)
{
- fprintf (stderr, ".");
- if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (hostkey, &purp, &sig))
+ FPRINTF (stderr, "%s", ".");
+ if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_sign (key, &purp, &sig))
{
- fprintf (stderr, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
+ FPRINTF (stderr, "%s", "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
ok = GNUNET_SYSERR;
continue;
}
printf ("%d RSA sign operations %llu ms\n", ITER,
(unsigned long long)
GNUNET_TIME_absolute_get_duration (start).rel_value);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
return ok;
}
#endif
static int
testCreateFromFile ()
{
- struct GNUNET_CRYPTO_RsaPrivateKey *key;
struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p1;
struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded p2;
GNUNET_assert (NULL != key);
GNUNET_CRYPTO_rsa_key_get_public (key, &p2);
GNUNET_assert (0 != memcmp (&p1, &p2, sizeof (p1)));
- GNUNET_CRYPTO_rsa_key_free (key);
- GNUNET_assert (0 == UNLINK (KEYFILE));
return GNUNET_OK;
}
+static void
+key_cont (void *cls,
+ struct GNUNET_CRYPTO_RsaPrivateKey *pk,
+ const char *emsg)
+{
+ const char *txt = cls;
+ struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub1;
+ struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub2;
+
+ GNUNET_assert (0 == strcmp ("ok", txt));
+ GNUNET_CRYPTO_rsa_key_get_public (pk, &pub1);
+ GNUNET_CRYPTO_rsa_key_get_public (key, &pub2);
+ GNUNET_assert (0 == memcmp (&pub1, &pub2,
+ sizeof (pub1)));
+ GNUNET_CRYPTO_rsa_key_free (pk);
+}
+
+
+static void
+test_async_creation (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc;
+
+ gc = GNUNET_CRYPTO_rsa_key_create_start (KEYFILE,
+ &key_cont,
+ (void*) "bug");
+ GNUNET_CRYPTO_rsa_key_create_stop (gc);
+ gc = GNUNET_CRYPTO_rsa_key_create_start (KEYFILE,
+ &key_cont,
+ (void*) "ok");
+}
+
+
int
main (int argc, char *argv[])
{
int failureCount = 0;
GNUNET_log_setup ("test-crypto-rsa", "WARNING", NULL);
- GNUNET_CRYPTO_random_disable_entropy_gathering ();
if (GNUNET_OK != testCreateFromFile ())
failureCount++;
+ GNUNET_SCHEDULER_run (&test_async_creation, NULL);
#if PERF
if (GNUNET_OK != testEncryptPerformance ())
failureCount++;
failureCount++;
if (GNUNET_OK != testSignVerify ())
failureCount++;
+ GNUNET_CRYPTO_rsa_key_free (key);
+ GNUNET_assert (0 == UNLINK (KEYFILE));
if (failureCount != 0)
{