/* vim: set ts=8 sts=4 sw=4 tw=80 noet: */
/*======================================================================
Copyright (C) 2004,2005,2009 Walter Doekes <walter+tthsum@wjd.nu>
This file is part of tthsum.

tthsum 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 3 of the License, or
(at your option) any later version.

tthsum 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 tthsum.  If not, see <http://www.gnu.org/licenses/>.
======================================================================*/
#include "tiger.h"

#include "base32.h"
#include "test.h"
#include <stdlib.h>
#include <string.h>


static int help_cmp_base32_bin(const char* name, const uint8_t* input,
	unsigned input_len, const char* base32) {
    uint64_t res[3];
    char base32buf[40];
    tiger((uint64_t*)input, input_len, res);
    uint64tobase32(base32buf, res, 3);
    TEST_PASS1(strcmp(base32buf, base32) == 0,
	    "Tiger hash result for \"%s\" is wrong!", name);
    if (input_len != 0) {
	tiger_bp(input[0], input + 1, input_len - 1, res);
	uint64tobase32(base32buf, res, 3);
	TEST_PASS1(strcmp(base32buf, base32) == 0,
		"Tiger BP hash result for \"%s\" is wrong!", name);
    }
    return 0;
}
    
static int help_cmp_base32(const char* input, const char* base32) {
    return help_cmp_base32_bin(input, (const uint8_t*)input, strlen(input),
	    base32);
}

static int test_tiger192_nessie_1a() {
    /* echo -n '3293AC630C13F0245F92BBB1766E16167A4E58492DDE73F3' |
     * perl -e 'use MIME::Base32 "RFC";$bin=<>;$bin=~s/([0-9A-Fa-f]{2})/chr
     *  hex $1/eg;print MIME::Base32::encode($bin)."\n";' */
    return help_cmp_base32("",
	    "GKJ2YYYMCPYCIX4SXOYXM3QWCZ5E4WCJFXPHH4Y")
	 + help_cmp_base32("a",
	    "O67PX3ZOP34KWLWI7E57LB5H7RQT4JD7L4SHQCI")
	 + help_cmp_base32("abc",
	    "FKVRJBHIYFMPFP5YYX7UDNL2KJISSEY4SV5V7EY")
	 + help_cmp_base32("message digest",
	    "3GA7RS3YEANJKDOPGBEHKHSEDRIX7SQ2UVNCT5Q")
	 + help_cmp_base32("abcdefghijklmnopqrstuvwxyz",
	    "C4KKI4XO4V6TABAECK74YVIDFIFRCYBP6N5652I")
	 + help_cmp_base32("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnl"
	    "mnomnopnopq", "B557TIM3TRMPFN3BBX36QTYKYOTRYYY6PNJ7PDQ");
}

static int test_tiger192_nessie_1b() {
    int ret = 0;
    char buf[2048];
    char *p;
    int i = 0;

    p = buf;
    for (i = 'A'; i <= 'Z'; ++i)
	*p++ = (char)i;
    for (i = 'a'; i <= 'z'; ++i)
	*p++ = (char)i;
    for (i = '0'; i <= '9'; ++i)
	*p++ = (char)i;
    *p = '\0';
    ret += help_cmp_base32(buf, "RXHKNAFBOWB64UBLUOFDYNUGKGEQ766M3RE2RTA");

    p = buf;
    for (i = 0; i < 8; ++i) {
	int j;
	for (j = '1'; j <= '9'; ++j)
	    *p++ = (char)j;
	*p++ = (char)'0';
    }
    *p = '\0';
    ret += help_cmp_base32(buf, "DQKHSVJJ7WPSA6UVR6CMKLYR5CD7UDFL37MRX7I");

    p = (char*)malloc(1000000);
    memset(p, 'a', 1000000);
    ret += help_cmp_base32_bin("one million times 'a'", (uint8_t*)p, 1000000,
	    "NWYOE4U4X2WZHVYVY2T5GYYC5GZ45YGSXQYUWQI");
    free(p);

    return ret;
}

static int test_tiger192_ross_and_eli() {
    return help_cmp_base32("",
	    "GKJ2YYYMCPYCIX4SXOYXM3QWCZ5E4WCJFXPHH4Y")
	 + help_cmp_base32("abc",
	    "FKVRJBHIYFMPFP5YYX7UDNL2KJISSEY4SV5V7EY")
	 + help_cmp_base32("Tiger",
	    "3UACGB4Z6UAJ73DN5PEDRO3KE7PSXHLPCEGHSNY")
	 + help_cmp_base32("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr"
	    "stuvwxyz0123456789+-", "64OILA4QFL5YPHW74YIPQLANI6DKHJJUKBCINNI")
	 + help_cmp_base32("ABCDEFGHIJKLMNOPQRSTUVWXYZ=abcdefghijklmnopq"
	    "rstuvwxyz+0123456789", "JDHOWYYIXB6UN2K5MVQRFTPRRWLZCX4XMVSYSVY")
	 + help_cmp_base32("Tiger - A Fast New Hash Function, by Ross An"
	    "derson and Eli Biham", "RKDGQKIEBJAQY4U22I7VVWTRCYB3HTOTK7SMCXQ")
	 + help_cmp_base32("Tiger - A Fast New Hash Function, by Ross An"
	    "derson and Eli Biham, proceedings of Fast Software Encrypti"
	    "on 3, Cambridge, 1996.", "MMNL3UID5OND2JC3NX6U255SK76HIOKQDUKWRXI")
	 + help_cmp_base32("Tiger - A Fast New Hash Function, by Ross An"
	    "derson and Eli Biham, proceedings of Fast Software Encrypti"
	    "on 3, Cambridge.", "ZZK2NL6VSH26XLCUP74E7CJCP6JTDWVQWYI4RCI")
	 + help_cmp_base32("message digest", 
	    "3GA7RS3YEANJKDOPGBEHKHSEDRIX7SQ2UVNCT5Q")
	 + help_cmp_base32("12345678901234567890123456789012345678901234"
	    "567890123456789012345678901234567890",
	    "DQKHSVJJ7WPSA6UVR6CMKLYR5CD7UDFL37MRX7I")
	 + help_cmp_base32("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr"
	    "stuvwxyz0123456789+-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm"
	    "nopqrstuvwxyz0123456789+-",
	    "YVADJZNUH24AAWCIU7QK42VMO3SP6WIK44K72JI");
}


TESTS(tiger_test)
    TEST(test_tiger192_nessie_1a);
    TEST(test_tiger192_nessie_1b);
    TEST(test_tiger192_ross_and_eli);
ENDTESTS
