-#include "gnunet_common.h"
-#include "gnunet_crypto_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 ITER 25
-#define KEYFILE "/tmp/test-gnunet-crypto-rsa.key"
-
-#define PERF GNUNET_YES
-
-static int
-testEncryptDecrypt ()
-{
- struct GNUNET_CRYPTO_RsaPrivateKey *hostkey;
- struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
- struct GNUNET_CRYPTO_RsaEncryptedData target;
- char result[MAX_TESTVAL];
- 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);
-
- ok = 0;
- start = GNUNET_TIME_absolute_get ();
- for (i = 0; i < ITER; i++)
- {
- fprintf (stderr, ".");
- if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
- strlen (TESTSTRING) + 1,
- &pkey, &target))
- {
- fprintf (stderr, "GNUNET_CRYPTO_rsa_encrypt returned SYSERR\n");
- ok++;
- continue;
- }
- if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
- &target, result,
- strlen (TESTSTRING) + 1))
- {
- fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
- ok++;
- continue;
-
- }
- if (strncmp (TESTSTRING, result, strlen (TESTSTRING)) != 0)
- {
- printf ("%s != %.*s - testEncryptDecrypt failed!\n",
- TESTSTRING, MAX_TESTVAL, result);
- ok++;
- continue;
- }
- }
- printf ("%d RSA encrypt/decrypt operations %llums (%d failures)\n",
- ITER,
- (unsigned long long) GNUNET_TIME_absolute_get_duration (start).
- value, ok);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
- if (ok == 0)
- return GNUNET_OK;
- else
- 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);
-
- ok = 0;
- start = GNUNET_TIME_absolute_get ();
- for (i = 0; i < ITER; i++)
- {
- fprintf (stderr, ".");
- if (GNUNET_SYSERR == GNUNET_CRYPTO_rsa_encrypt (TESTSTRING,
- strlen (TESTSTRING) + 1,
- &pkey, &target))
- {
- fprintf (stderr, "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).
- value, ok);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
- if (ok != 0)
- return GNUNET_SYSERR;
- return GNUNET_OK;
-}
-#endif
-
-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_CRYPTO_AesSessionKey outsk;
- 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);
-
- ok = 0;
- start = GNUNET_TIME_absolute_get ();
- for (i = 0; i < ITER; i++)
- {
- fprintf (stderr, ".");
- 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");
- ok++;
- continue;
- }
- if (-1 == GNUNET_CRYPTO_rsa_decrypt (hostkey,
- &target, &outsk,
- sizeof (struct
- GNUNET_CRYPTO_AesSessionKey)))
- {
- fprintf (stderr, "GNUNET_CRYPTO_rsa_decrypt returned SYSERR\n");
- ok++;
- continue;
- }
- if (0 !=
- memcmp (&insk, &outsk, sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
- {
- printf ("testEncryptDecryptSK failed!\n");
- ok++;
- continue;
- }
- }
- printf ("%d RSA encrypt/decrypt SK operations %llus (%d failures)\n",
- ITER,
- (unsigned long long) GNUNET_TIME_absolute_get_duration (start).
- value, ok);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
- 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;
- int i;
- 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);
- 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, "GNUNET_CRYPTO_rsa_sign returned SYSERR\n");
- ok = GNUNET_SYSERR;
- continue;
- }
- if (GNUNET_SYSERR ==
- GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TEST,
- &purp, &sig, &pkey))
- {
- printf ("GNUNET_CRYPTO_rsa_verify failed!\n");
- ok = GNUNET_SYSERR;
- continue;
- }
- if (GNUNET_SYSERR !=
- GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_HELLO,
- &purp, &sig, &pkey))
- {
- printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
- ok = GNUNET_SYSERR;
- continue;
- }
- }
- printf ("%d RSA sign/verify operations %llums\n",
- ITER,
- (unsigned long long) GNUNET_TIME_absolute_get_duration (start).
- value);
- GNUNET_CRYPTO_rsa_key_free (hostkey);
- return ok;
-}