From 336a16227b9d8ec20339f0984e9507969cf82bf9 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Wed, 16 Nov 2005 13:52:34 +0000 Subject: [PATCH] updates. --- doc/silcalgs.conf | 10 +++ lib/silccrypt/tests/Makefile.am | 2 + lib/silccrypt/tests/test_aes.c | 4 +- lib/silccrypt/tests/test_hmacmd5.c | 4 +- lib/silccrypt/tests/test_hmacsha1.c | 4 +- lib/silccrypt/tests/test_hmacsha256.c | 123 ++++++++++++++++++++++++++ lib/silccrypt/tests/test_md5.c | 4 +- lib/silccrypt/tests/test_sha1.c | 4 +- lib/silccrypt/tests/test_sha256.c | 73 +++++++++++++++ lib/silccrypt/tests/test_silcpkcs.c | 4 +- 10 files changed, 220 insertions(+), 12 deletions(-) create mode 100644 lib/silccrypt/tests/test_hmacsha256.c create mode 100644 lib/silccrypt/tests/test_sha256.c diff --git a/doc/silcalgs.conf b/doc/silcalgs.conf index f0140e5c..55f9774c 100644 --- a/doc/silcalgs.conf +++ b/doc/silcalgs.conf @@ -72,6 +72,11 @@ hash { blocklength = 64; digestlength = 20; }; +hash { + name = "sha256"; + blocklength = 64; + digestlength = 32; +}; hash { name = "md5"; blocklength = 64; @@ -87,6 +92,11 @@ hmac { hash = "sha1"; maclength = 12; }; +hmac { + name = "hmac-sha256-96"; + hash = "sha256"; + maclength = 12; +}; hmac { name = "hmac-md5-96"; hash = "md5"; diff --git a/lib/silccrypt/tests/Makefile.am b/lib/silccrypt/tests/Makefile.am index aeb0724e..84e1e024 100644 --- a/lib/silccrypt/tests/Makefile.am +++ b/lib/silccrypt/tests/Makefile.am @@ -21,6 +21,7 @@ bin_PROGRAMS = test_sha1 \ test_sha256 \ test_md5 \ test_hmacsha1 \ + test_hmacsha256 \ test_hmacmd5 \ test_aes \ test_twofish \ @@ -30,6 +31,7 @@ test_sha1_SOURCES = test_sha1.c test_sha256_SOURCES = test_sha256.c test_md5_SOURCES = test_md5.c test_hmacsha1_SOURCES = test_hmacsha1.c +test_hmacsha256_SOURCES = test_hmacsha256.c test_hmacmd5_SOURCES = test_hmacmd5.c test_aes_SOURCES = test_aes.c diff --git a/lib/silccrypt/tests/test_aes.c b/lib/silccrypt/tests/test_aes.c index 11f8076c..6a802ece 100644 --- a/lib/silccrypt/tests/test_aes.c +++ b/lib/silccrypt/tests/test_aes.c @@ -28,8 +28,8 @@ int main(int argc, char **argv) int i; if (argc > 1 && !strcmp(argv[1], "-d")) { - silc_debug = 1; - silc_debug_hexdump = 1; + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*aes*,*cipher*"); } diff --git a/lib/silccrypt/tests/test_hmacmd5.c b/lib/silccrypt/tests/test_hmacmd5.c index 94f77952..b35bec6f 100644 --- a/lib/silccrypt/tests/test_hmacmd5.c +++ b/lib/silccrypt/tests/test_hmacmd5.c @@ -34,8 +34,8 @@ int main(int argc, char **argv) SilcHmac hmac; if (argc > 1 && !strcmp(argv[1], "-d")) { - silc_debug = 1; - silc_debug_hexdump = 1; + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*hash*,*md5*,*hmac*"); } diff --git a/lib/silccrypt/tests/test_hmacsha1.c b/lib/silccrypt/tests/test_hmacsha1.c index a5a67b61..b1950cd7 100644 --- a/lib/silccrypt/tests/test_hmacsha1.c +++ b/lib/silccrypt/tests/test_hmacsha1.c @@ -34,8 +34,8 @@ int main(int argc, char **argv) SilcHmac hmac; if (argc > 1 && !strcmp(argv[1], "-d")) { - silc_debug = 1; - silc_debug_hexdump = 1; + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*hash*,*sha1*,*hmac*"); } diff --git a/lib/silccrypt/tests/test_hmacsha256.c b/lib/silccrypt/tests/test_hmacsha256.c new file mode 100644 index 00000000..1b5cfbb3 --- /dev/null +++ b/lib/silccrypt/tests/test_hmacsha256.c @@ -0,0 +1,123 @@ +#include "silcincludes.h" + +/* Test vectors from draft-ietf-ipsec-ciph-sha-256-01.txt */ + +/* First test vector */ +const unsigned char key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x00"; +int key1_len = 32; +const unsigned char data1[] = "abc"; +const unsigned char data1_digest[] = "\xa2\x1b\x1f\x5d\x4c\xf4\xf7\x3a\x4d\xd9\x39\x75\x0f\x7a\x06\x6a\x7f\x98\xcc\x13\x1c\xb1\x6a\x66\x92\x75\x90\x21\xcf\xab\x81\x81"; + +/* Second test vector */ +const unsigned char key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x00"; +int key2_len = 32; +const unsigned char data2[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; +const unsigned char data2_digest[] = "\x10\x4f\xdc\x12\x57\x32\x8f\x08\x18\x4b\xa7\x31\x31\xc5\x3c\xae\xe6\x98\xe3\x61\x19\x42\x11\x49\xea\x8c\x71\x24\x56\x69\x7d\x30"; + +/* Third test vector */ +const unsigned char key3[] = "Jefe"; +int key3_len = 4; +unsigned char data3[] = "what do ya want for nothing?"; +const unsigned char data3_digest[] = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43"; + +/* Fourth test vector, key 0xaa 80 times */ +unsigned char key4[80]; +int key4_len = 80; +const unsigned char data4[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; +const unsigned char data4_digest[] = "\x63\x55\xac\x22\xe8\x90\xd0\xa3\xc8\x48\x1a\x5c\xa4\x82\x5b\xc8\x84\xd3\xe7\xa1\xff\x98\xa2\xfc\x2a\xc7\xd8\xe0\x64\xc3\xb2\xe6"; + +int main(int argc, char **argv) +{ + bool success = FALSE; + unsigned char digest[20]; + SilcUInt32 len; + SilcHmac hmac; + + if (argc > 1 && !strcmp(argv[1], "-d")) { + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); + silc_log_set_debug_string("*crypt*,*hash*,*sha256*,*hmac*"); + } + + SILC_LOG_DEBUG(("Registering builtin hash functions")); + silc_hash_register_default(); + silc_hmac_register_default(); + + SILC_LOG_DEBUG(("Allocating sha256 HMAC")); + if (!silc_hmac_alloc("hmac-sha256", NULL, &hmac)) { + SILC_LOG_DEBUG(("Allocating sha256 HMAC failed")); + goto err; + } + + /* First test vector */ + SILC_LOG_DEBUG(("First test vector")); + silc_hmac_init_with_key(hmac, key1, key1_len); + silc_hmac_update(hmac, data1, strlen(data1)); + silc_hmac_final(hmac, digest, &len); + SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key1, key1_len); + SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data1, strlen(data1)); + SILC_LOG_HEXDUMP(("Digest"), digest, len); + SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data1_digest, len); + if (memcmp(digest, data1_digest, len)) { + SILC_LOG_DEBUG(("HMAC failed")); + goto err; + } + SILC_LOG_DEBUG(("HMAC is successful")); + + /* Second test vector */ + SILC_LOG_DEBUG(("Second test vector")); + silc_hmac_init_with_key(hmac, key2, key2_len); + silc_hmac_update(hmac, data2, strlen(data2)); + silc_hmac_final(hmac, digest, &len); + SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key2, key2_len); + SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data2, strlen(data2)); + SILC_LOG_HEXDUMP(("Digest"), digest, len); + SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data2_digest, len); + if (memcmp(digest, data2_digest, len)) { + SILC_LOG_DEBUG(("HMAC failed")); + goto err; + } + SILC_LOG_DEBUG(("HMAC is successful")); + + /* Third test vector */ + SILC_LOG_DEBUG(("Third test vector")); + silc_hmac_init_with_key(hmac, key3, key3_len); + silc_hmac_update(hmac, data3, strlen(data3)); + silc_hmac_final(hmac, digest, &len); + SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key3, key3_len); + SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data3, strlen(data3)); + SILC_LOG_HEXDUMP(("Digest"), digest, len); + SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data3_digest, len); + if (memcmp(digest, data3_digest, len)) { + SILC_LOG_DEBUG(("HMAC failed")); + goto err; + } + SILC_LOG_DEBUG(("HMAC is successful")); + + /* Fourth test vector */ + SILC_LOG_DEBUG(("Fourth test vector")); + memset(key4, '\xaa', key4_len); + silc_hmac_init_with_key(hmac, key4, key4_len); + silc_hmac_update(hmac, data4, strlen(data4)); + silc_hmac_final(hmac, digest, &len); + SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key4, key4_len); + SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data4, sizeof(data4)); + SILC_LOG_HEXDUMP(("Digest"), digest, len); + SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data4_digest, len); + if (memcmp(digest, data4_digest, len)) { + SILC_LOG_DEBUG(("HMAC failed")); + goto err; + } + SILC_LOG_DEBUG(("HMAC is successful")); + + success = TRUE; + + err: + SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); + fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); + + silc_hmac_free(hmac); + silc_hash_unregister_all(); + silc_hmac_unregister_all(); + return success; +} diff --git a/lib/silccrypt/tests/test_md5.c b/lib/silccrypt/tests/test_md5.c index 5f9ac5c1..cb6db76c 100644 --- a/lib/silccrypt/tests/test_md5.c +++ b/lib/silccrypt/tests/test_md5.c @@ -21,8 +21,8 @@ int main(int argc, char **argv) SilcHash md5; if (argc > 1 && !strcmp(argv[1], "-d")) { - silc_debug = 1; - silc_debug_hexdump = 1; + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*hash*,*md5*"); } diff --git a/lib/silccrypt/tests/test_sha1.c b/lib/silccrypt/tests/test_sha1.c index 93efb3d0..b0bcf058 100644 --- a/lib/silccrypt/tests/test_sha1.c +++ b/lib/silccrypt/tests/test_sha1.c @@ -18,8 +18,8 @@ int main(int argc, char **argv) SilcHash sha1; if (argc > 1 && !strcmp(argv[1], "-d")) { - silc_debug = 1; - silc_debug_hexdump = 1; + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*hash*,*sha1*"); } diff --git a/lib/silccrypt/tests/test_sha256.c b/lib/silccrypt/tests/test_sha256.c new file mode 100644 index 00000000..e89ce57f --- /dev/null +++ b/lib/silccrypt/tests/test_sha256.c @@ -0,0 +1,73 @@ +#include "silcincludes.h" + +/* Test vectors from NIST secure hashing definition for SHA-256 */ + +/* First test vector */ +const unsigned char data1[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; +const unsigned char data1_digest[] = "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1"; + +/* Second test vector */ +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"; + +int main(int argc, char **argv) +{ + bool success = FALSE; + unsigned char digest[20]; + SilcHash sha256; + + if (argc > 1 && !strcmp(argv[1], "-d")) { + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); + silc_log_set_debug_string("*crypt*,*hash*,*sha256*"); + } + + SILC_LOG_DEBUG(("Registering builtin hash functions")); + silc_hash_register_default(); + + SILC_LOG_DEBUG(("Allocating sha256 hash function")); + if (!silc_hash_alloc("sha256", &sha256)) { + SILC_LOG_DEBUG(("Allocating sha256 hash function failed")); + goto err; + } + + /* First test vector */ + SILC_LOG_DEBUG(("First test vector")); + silc_hash_init(sha256); + silc_hash_update(sha256, data1, strlen(data1)); + silc_hash_final(sha256, digest); + 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, + sizeof(digest)); + if (memcmp(digest, data1_digest, sizeof(digest))) { + SILC_LOG_DEBUG(("Hash failed")); + goto err; + } + SILC_LOG_DEBUG(("Hash is successful")); + + /* Second test vector */ + SILC_LOG_DEBUG(("First test vector")); + silc_hash_init(sha256); + silc_hash_update(sha256, data2, strlen(data2)); + silc_hash_final(sha256, digest); + SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data2, strlen(data2)); + SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest)); + SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data2_digest, + sizeof(digest)); + if (memcmp(digest, data2_digest, sizeof(digest))) { + SILC_LOG_DEBUG(("Hash failed")); + goto err; + } + SILC_LOG_DEBUG(("Hash is successful")); + + success = TRUE; + + err: + SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); + fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); + + silc_hash_free(sha256); + silc_hash_unregister_all(); + return success; +} diff --git a/lib/silccrypt/tests/test_silcpkcs.c b/lib/silccrypt/tests/test_silcpkcs.c index b1c2354f..0afbdd8e 100644 --- a/lib/silccrypt/tests/test_silcpkcs.c +++ b/lib/silccrypt/tests/test_silcpkcs.c @@ -283,8 +283,8 @@ int main(int argc, char **argv) int i; if (argc > 1 && !strcmp(argv[1], "-d")) { - silc_debug = 1; - silc_debug_hexdump = 1; + silc_log_debug(TRUE); + silc_log_debug_hexdump(TRUE); silc_log_set_debug_string("*crypt*,*pkcs*,*rsa*,*primegen*"); } silc_hash_register_default(); -- 2.24.0