Added test_hash.
authorPekka Riikonen <priikone@silcnet.org>
Sat, 1 Sep 2007 10:12:08 +0000 (10:12 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 1 Sep 2007 10:12:08 +0000 (10:12 +0000)
lib/silccrypt/tests/Makefile.am
lib/silccrypt/tests/test_hash.c [new file with mode: 0644]
lib/silccrypt/tests/test_sha256.c

index 96479a2c5ecb5bc51598da3cd351cb6598218ad3..838a57c3c10112f06b44ab1f0d95edf571d80f53 100644 (file)
@@ -28,8 +28,10 @@ bin_PROGRAMS =       test_sha1       \
                test_cast5      \
                test_des        \
                test_silcpkcs   \
-               test_dsa
+               test_dsa        \
+               test_hash
 
+test_hash_SOURCES = test_hash.c
 test_sha1_SOURCES = test_sha1.c
 test_sha256_SOURCES = test_sha256.c
 test_md5_SOURCES = test_md5.c
diff --git a/lib/silccrypt/tests/test_hash.c b/lib/silccrypt/tests/test_hash.c
new file mode 100644 (file)
index 0000000..dd8f983
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+
+  test_hash.c
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 2007 Pekka Riikonen
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; version 2 of the License.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+*/
+
+#include "silc.h"
+
+#define HASH_LEN 0x0002ffff    /* hash data len (at least) */
+#define HASH_ROUND 256         /* hash rounds (at least) */
+#define HASH_MIN_TIME 2.0       /* seconds to run the test (at least) */
+
+SilcTimerStruct timer;
+SilcHash hash;
+
+int main(int argc, char **argv)
+{
+  SilcUInt64 sec;
+  SilcUInt32 usec;
+  double totsec;
+  unsigned char *data;
+  SilcUInt32 rounds;
+  unsigned char digest[SILC_HASH_MAXLEN];
+  int i, k;
+
+  data = malloc(HASH_LEN * sizeof(*data));
+  if (!data)
+    exit(1);
+
+  for (i = 0; i < HASH_LEN; i++)
+    data[i] = i % 255;
+
+  silc_timer_synchronize(&timer);
+
+  for (i = 0; silc_default_hash[i].name; i++) {
+    if (!silc_hash_alloc(silc_default_hash[i].name, &hash))
+      exit(1);
+    silc_hash_init(hash);
+
+    rounds = HASH_ROUND;
+
+  retry:
+    silc_timer_start(&timer);
+    for (k = 0; k < rounds; k++)
+      silc_hash_update(hash, data, HASH_LEN);
+    silc_timer_stop(&timer);
+    silc_hash_final(hash, digest);
+
+    silc_timer_value(&timer, &sec, &usec);
+    totsec = (double)sec;
+    totsec += ((double)usec / (double)(1000 * 1000));
+    if (totsec < HASH_MIN_TIME) {
+      rounds *= 2;
+      goto retry;
+    }
+
+    printf("%s:\t%.2f KB (%.2f MB) / sec (total test time %.2f secs)\n",
+          silc_default_hash[i].name,
+          (((double)(HASH_LEN * rounds) / 1024.0) / totsec),
+          (((double)(HASH_LEN * rounds) / (1024.0 * 1024.0)) / totsec),
+          totsec);
+
+    silc_hash_free(hash);
+  }
+
+  return 0;
+}
index ef0945b6dd40675464df1cd402ec577184f0b69e..f818af617d90ae2fad4eb95480ec34bc63081630 100644 (file)
@@ -10,10 +10,17 @@ const unsigned char data1_digest[] = "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x
 const unsigned char data2[] = "abc";
 const unsigned char data2_digest[] = "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad";
 
+/* Third test vector */
+const unsigned char data3[] ="ebaccc34d6d6d3d21ed0ad2ba7c07c21d253c4814f4ad89d32369237497f47a1adabfa2398ddd09d769cc46d3fd69c9303251c13c750799b8f151166bc2658609871168b30a4d0a162f183fb360f99b172811503681a11f813c16a446272ba6fd48586344533b9280856519c357059c344ef1718dbaf86fae5c10799e46b5316886fb4e68090757890539617e403c511a4f78a19c818c2ea2e9d4e2de9190c9dddb806";
+const unsigned char data3_digest[] ="c907180443dee3cbccb4c31328e625158527a593b878de1b8e4ba37f1d69fb66";
+
+SilcTimerStruct timer;
+
 int main(int argc, char **argv)
 {
   SilcBool success = FALSE;
-  unsigned char digest[20];
+  unsigned char digest[32], tmp[4096], digest2[32];
+  SilcUInt32 tmp_len;
   SilcHash sha256;
   
   if (argc > 1 && !strcmp(argv[1], "-d")) {
@@ -31,11 +38,18 @@ int main(int argc, char **argv)
     goto err;
   }
 
+  SilcUInt64 t1, t2;
+  silc_timer_synchronize(&timer);
+
   /* First test vector */
   SILC_LOG_DEBUG(("First test vector"));
   silc_hash_init(sha256);
   silc_hash_update(sha256, data1, strlen(data1));
+  memset(digest, 0, sizeof(digest));
+  t1 = silc_timer_tick(&timer, FALSE);
   silc_hash_final(sha256, digest);
+  t2 = silc_timer_tick(&timer, TRUE);
+  SILC_LOG_DEBUG(("cycles: %d", t2 - t1));
   SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data1, strlen(data1));
   SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest));
   SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data1_digest,
@@ -47,7 +61,7 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Hash is successful"));
   
   /* Second test vector */
-  SILC_LOG_DEBUG(("First test vector"));
+  SILC_LOG_DEBUG(("Second test vector"));
   silc_hash_init(sha256);
   silc_hash_update(sha256, data2, strlen(data2));
   silc_hash_final(sha256, digest);
@@ -61,6 +75,23 @@ int main(int argc, char **argv)
   }
   SILC_LOG_DEBUG(("Hash is successful"));
   
+  /* Third test vector */
+  SILC_LOG_DEBUG(("Third test vector"));
+  silc_hash_init(sha256);
+  silc_hex2data(data3, tmp, sizeof(tmp), &tmp_len);
+  silc_hash_update(sha256, tmp, tmp_len);
+  silc_hash_final(sha256, digest);
+  SILC_LOG_HEXDUMP(("Message"), tmp, tmp_len);
+  SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest));
+  silc_hex2data(data3_digest, digest2, sizeof(digest2), NULL);
+  SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)digest2,
+                  sizeof(digest));
+  if (memcmp(digest, digest2, sizeof(digest))) {
+    SILC_LOG_DEBUG(("Hash failed"));
+    goto err;
+  }
+  SILC_LOG_DEBUG(("Hash is successful"));
+  
   success = TRUE;
   
  err: