/* $Id$ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono * * 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 #include #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] & 0xFF); c += 2; } sprintf(c, " "); c += 1; } *(c - 1) = '\0'; } static int sha1_test1(void) { enum { MILLION = 1000000 }; int k; pj_sha1_context context; pj_uint8_t digest[20]; char output[80]; pj_pool_t *pool; pj_uint8_t *block; 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 < MILLION; 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; } /* million 'a' test, using block */ pool = pj_pool_create(mem, "sha1test", 256, 512, NULL); block = (pj_uint8_t*)pj_pool_alloc(pool, MILLION); pj_memset(block, 'a', MILLION); pj_sha1_init(&context); pj_sha1_update(&context, block, MILLION); pj_sha1_final(&context, digest); digest_to_hex(digest, output); if (strcmp(output, sha1_test_results[2])) { pj_pool_release(pool); PJ_LOG(3, (THIS_FILE, " incorrect hash result for block update!")); return -21; } /* verify that original buffer was not modified */ for (k=0; k 0) { pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input + len/2, len - len/2); } crc1 = pj_crc32_final(&ctx); if (crc0 != crc1) { PJ_LOG(3,(THIS_FILE, " error: crc algorithm error on test %d", i)); return -85; } } return 0; } enum { ENCODE = 1, DECODE = 2, ENCODE_DECODE = 3 }; /* * Base64 test vectors (RFC 4648) */ static struct base64_test_vec { const char *base256; const char *base64; unsigned flag; } base64_test_vec[] = { { "", "", ENCODE_DECODE }, { "f", "Zg==", ENCODE_DECODE }, { "fo", "Zm8=", ENCODE_DECODE }, { "foo", "Zm9v", ENCODE_DECODE }, { "foob", "Zm9vYg==", ENCODE_DECODE }, { "fooba", "Zm9vYmE=", ENCODE_DECODE }, { "foobar", "Zm9vYmFy", ENCODE_DECODE }, { "\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+", ENCODE_DECODE }, { "\x14\xfb\x9c\x03\xd9", "FPucA9k=", ENCODE_DECODE }, { "\x14\xfb\x9c\x03", "FPucAw==", ENCODE_DECODE }, /* with whitespaces */ { "foobar", "Zm9v\r\nYmFy", DECODE }, { "foobar", "\nZ\r\nm 9\tv\nYm\nF\ny\n", DECODE }, }; static int base64_test(void) { unsigned i; char output[80]; pj_status_t rc; PJ_LOG(3, (THIS_FILE, " base64 test..")); for (i=0; i