summaryrefslogtreecommitdiff
path: root/pjlib-util/src/pjlib-util-test
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2007-02-25 15:38:32 +0000
committerBenny Prijono <bennylp@teluu.com>2007-02-25 15:38:32 +0000
commita34fbb64e27b5075a4207318ba356349669b4f08 (patch)
treefb9705d9e0164747e4a17ba0fc723abb4abba791 /pjlib-util/src/pjlib-util-test
parent3692bfe973fa350b52445119e3270199b88dfa7e (diff)
Updated VS8 and EVC4 project with new STUN files and added SHA1, HMAC-MD5, and HMAC-SHA1 encryption
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1001 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib-util/src/pjlib-util-test')
-rw-r--r--pjlib-util/src/pjlib-util-test/encryption.c409
-rw-r--r--pjlib-util/src/pjlib-util-test/main.c2
-rw-r--r--pjlib-util/src/pjlib-util-test/test.c10
-rw-r--r--pjlib-util/src/pjlib-util-test/test.h6
4 files changed, 423 insertions, 4 deletions
diff --git a/pjlib-util/src/pjlib-util-test/encryption.c b/pjlib-util/src/pjlib-util-test/encryption.c
new file mode 100644
index 00000000..0c5de154
--- /dev/null
+++ b/pjlib-util/src/pjlib-util-test/encryption.c
@@ -0,0 +1,409 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include "test.h"
+#include <pjlib-util.h>
+#include <pjlib.h>
+
+
+#if INCLUDE_ENCRYPTION_TEST
+
+/*
+ * Encryption algorithm tests.
+ */
+#define THIS_FILE "encryption.c"
+
+
+/*
+ * SHA1 test from the original sha1.c source file.
+ */
+static char *sha1_test_data[] = {
+ "abc",
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ "A million repetitions of 'a'"
+};
+static char *sha1_test_results[] = {
+ "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
+ "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
+ "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"
+};
+
+
+static void digest_to_hex(const pj_uint8_t digest[PJ_SHA1_DIGEST_SIZE],
+ char *output)
+{
+ int i,j;
+ char *c = output;
+
+ for (i = 0; i < PJ_SHA1_DIGEST_SIZE/4; i++) {
+ for (j = 0; j < 4; j++) {
+ sprintf(c,"%02X", digest[i*4+j]);
+ c += 2;
+ }
+ sprintf(c, " ");
+ c += 1;
+ }
+ *(c - 1) = '\0';
+}
+
+static int sha1_test1(void)
+{
+ int k;
+ pj_sha1_context context;
+ pj_uint8_t digest[20];
+ char output[80];
+
+ PJ_LOG(3, (THIS_FILE, " SHA1 test vector 1 from sha1.c.."));
+
+ for (k = 0; k < 2; k++){
+ pj_sha1_init(&context);
+ pj_sha1_update(&context, (pj_uint8_t*)sha1_test_data[k],
+ pj_ansi_strlen(sha1_test_data[k]));
+ pj_sha1_final(&context, digest);
+ digest_to_hex(digest, output);
+
+ if (pj_ansi_strcmp(output, sha1_test_results[k])) {
+ PJ_LOG(3, (THIS_FILE, " incorrect hash result on k=%d", k));
+ return -10;
+ }
+ }
+
+ /* million 'a' vector we feed separately */
+ pj_sha1_init(&context);
+ for (k = 0; k < 1000000; k++)
+ pj_sha1_update(&context, (pj_uint8_t*)"a", 1);
+ pj_sha1_final(&context, digest);
+ digest_to_hex(digest, output);
+ if (strcmp(output, sha1_test_results[2])) {
+ PJ_LOG(3, (THIS_FILE, " incorrect hash result!"));
+ return -20;
+ }
+
+ /* success */
+ return(0);
+}
+
+
+/*
+ * SHA1 test from RFC 3174
+ */
+/*
+ * Define patterns for testing
+ */
+#define TEST1 "abc"
+#define TEST2a "abcdbcdecdefdefgefghfghighijhi"
+#define TEST2b "jkijkljklmklmnlmnomnopnopq"
+#define TEST2 TEST2a TEST2b
+#define TEST3 "a"
+#define TEST4a "01234567012345670123456701234567"
+#define TEST4b "01234567012345670123456701234567"
+ /* an exact multiple of 512 bits */
+#define TEST4 TEST4a TEST4b
+
+static char *testarray[4] =
+{
+ TEST1,
+ TEST2,
+ TEST3,
+ TEST4
+};
+static int repeatcount[4] = { 1, 1, 1000000, 10 };
+static char *resultarray[4] =
+{
+ "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
+ "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
+ "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F",
+ "DEA356A2 CDDD90C7 A7ECEDC5 EBB56393 4F460452"
+};
+
+static int sha1_test2(void)
+{
+ pj_sha1_context sha;
+ int i;
+ pj_uint8_t digest[20];
+ char char_digest[64];
+
+ PJ_LOG(3, (THIS_FILE, " SHA1 test vector 2 from rfc 3174.."));
+
+ for(i = 0; i < 4; ++i) {
+ int j;
+
+ pj_sha1_init(&sha);
+
+ for(j = 0; j < repeatcount[i]; ++j) {
+ pj_sha1_update(&sha,
+ (const pj_uint8_t *) testarray[i],
+ pj_ansi_strlen(testarray[i]));
+ }
+
+ pj_sha1_final(&sha, digest);
+
+ digest_to_hex(digest, char_digest);
+ if (pj_ansi_strcmp(char_digest, resultarray[i])) {
+ PJ_LOG(3, (THIS_FILE, " digest mismatch in test %d", i));
+ return -40;
+ }
+ }
+
+ return 0;
+}
+
+
+/*
+ * HMAC-MD5 and HMAC-SHA1 test vectors from RFC 2202
+ */
+struct rfc2202_test
+{
+ char *key;
+ unsigned key_len;
+ char *input;
+ unsigned input_len;
+ char *md5_digest;
+ char *sha1_digest;
+};
+
+
+stru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a\xaa\xaa\xaa\xaa\xaa\xaa"
+ "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
+ 80,
+ "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
+ 73,
+ "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e",
+ "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"
+ }
+};
+
+
+static int rfc2202_test(void)
+{
+ unsigned i;
+
+ PJ_LOG(3, (THIS_FILE, " verifying test vectors from rfc 2202.."));
+
+ /* Verify that test vectors are valid */
+ for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
+ PJ_ASSERT_RETURN(pj_ansi_strlen(rfc2202_test_vector[i].input) ==
+ rfc2202_test_vector[i].input_len, -50);
+ PJ_ASSERT_RETURN(pj_ansi_strlen(rfc2202_test_vector[i].key) ==
+ rfc2202_test_vector[i].key_len, -52);
+ PJ_ASSERT_RETURN(rfc2202_test_vector[i].md5_digest==NULL ||
+ pj_ansi_strlen(rfc2202_test_vector[i].md5_digest)<=16,
+ -54);
+ PJ_ASSERT_RETURN(rfc2202_test_vector[i].sha1_digest==NULL ||
+ pj_ansi_strlen(rfc2202_test_vector[i].sha1_digest)<=20,
+ -56);
+ }
+
+ /* Test HMAC-MD5 */
+ PJ_LOG(3, (THIS_FILE, " HMAC-MD5 test vectors from rfc 2202.."));
+ for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
+ pj_uint8_t digest_buf[18], *digest;
+
+ if (rfc2202_test_vector[i].md5_digest == NULL)
+ continue;
+
+ digest_buf[0] = '\0';
+ digest_buf[17] = '\0';
+
+ digest = digest_buf+1;
+
+ pj_hmac_md5((pj_uint8_t*)rfc2202_test_vector[i].input,
+ rfc2202_test_vector[i].input_len,
+ (pj_uint8_t*)rfc2202_test_vector[i].key,
+ rfc2202_test_vector[i].key_len,
+ digest);
+
+ /* Check for overwrites */
+ if (digest_buf[0] != '\0' || digest_buf[17] != '\0') {
+ PJ_LOG(3, (THIS_FILE, " error: overwriting outside buffer on test %d", i));
+ return -60;
+ }
+
+ /* Compare digest */
+ if (pj_memcmp(rfc2202_test_vector[i].md5_digest, digest, 16)) {
+ PJ_LOG(3, (THIS_FILE, " error: digest mismatch on test %d", i));
+ return -65;
+ }
+ }
+
+ /* Test HMAC-SHA1 */
+ PJ_LOG(3, (THIS_FILE, " HMAC-SHA1 test vectors from rfc 2202.."));
+ for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
+ pj_uint8_t digest_buf[22], *digest;
+
+ if (rfc2202_test_vector[i].sha1_digest == NULL)
+ continue;
+
+ digest_buf[0] = '\0';
+ digest_buf[21] = '\0';
+
+ digest = digest_buf+1;
+
+ pj_hmac_sha1((pj_uint8_t*)rfc2202_test_vector[i].input,
+ rfc2202_test_vector[i].input_len,
+ (pj_uint8_t*)rfc2202_test_vector[i].key,
+ rfc2202_test_vector[i].key_len,
+ digest);
+
+ /* Check for overwrites */
+ if (digest_buf[0] != '\0' || digest_buf[21] != '\0') {
+ PJ_LOG(3, (THIS_FILE, " error: overwriting outside buffer on test %d", i));
+ return -70;
+ }
+
+ /* Compare digest */
+ if (pj_memcmp(rfc2202_test_vector[i].sha1_digest, digest, 20)) {
+ PJ_LOG(3, (THIS_FILE, " error: digest mismatch on test %d", i));
+ return -75;
+ }
+ }
+
+
+ /* Success */
+ return 0;
+}
+
+
+int encryption_test()
+{
+ int rc;
+
+ rc = sha1_test1();
+ if (rc != 0)
+ return rc;
+
+ rc = sha1_test2();
+ if (rc != 0)
+ return rc;
+
+ rc = rfc2202_test();
+ if (rc != 0)
+ return rc;
+
+ return 0;
+}
+
+
+
+#endif /* INCLUDE_ENCRYPTION_TEST */
+
diff --git a/pjlib-util/src/pjlib-util-test/main.c b/pjlib-util/src/pjlib-util-test/main.c
index ac48b8a4..33583e30 100644
--- a/pjlib-util/src/pjlib-util-test/main.c
+++ b/pjlib-util/src/pjlib-util-test/main.c
@@ -1,4 +1,4 @@
-/* $Id */
+/* $Id$ */
/*
* Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
*
diff --git a/pjlib-util/src/pjlib-util-test/test.c b/pjlib-util/src/pjlib-util-test/test.c
index 0d1b4117..b6cc7290 100644
--- a/pjlib-util/src/pjlib-util-test/test.c
+++ b/pjlib-util/src/pjlib-util-test/test.c
@@ -1,4 +1,4 @@
-/* $Id */
+/* $Id$ */
/*
* Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
*
@@ -61,7 +61,15 @@ static int test_inner(void)
pj_dump_config();
pj_caching_pool_init( &caching_pool, &pj_pool_factory_default_policy, 0 );
+
+#if INCLUDE_XML_TEST
DO_TEST(xml_test());
+#endif
+
+#if INCLUDE_ENCRYPTION_TEST
+ DO_TEST(encryption_test());
+#endif
+
on_return:
return rc;
diff --git a/pjlib-util/src/pjlib-util-test/test.h b/pjlib-util/src/pjlib-util-test/test.h
index 9532bbd3..252b3399 100644
--- a/pjlib-util/src/pjlib-util-test/test.h
+++ b/pjlib-util/src/pjlib-util-test/test.h
@@ -1,4 +1,4 @@
-/* $Id */
+/* $Id$ */
/*
* Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
*
@@ -18,9 +18,11 @@
*/
#include <pj/types.h>
-#define INCLUDE_XML_TEST 1
+#define INCLUDE_XML_TEST 1
+#define INCLUDE_ENCRYPTION_TEST 1
extern int xml_test(void);
+extern int encryption_test();
extern int test_main(void);
extern void app_perror(const char *title, pj_status_t rc);