aboutsummaryrefslogtreecommitdiffstats
path: root/t/blake2b.c
blob: a497c3499171b875759104c01c7e466cf41a6458 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* See LICENSE file for copyright and license details. */
#ifdef SUPPORT_BLAKE2B
# define TEST
# include "../common.h"


static struct testcase testcases[] = {
	{1, 0, "",
	 "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419"
	 "d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce"},
	{1, 0, "The quick brown fox jumps over the lazy dog",
	 "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673"
	 "f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918"},
	{1, 0, "The quick brown fox jumps over the lazy dof",
	 "ab6b007747d8068c02e25a6008db8a77c218d94f3b40d2291a7dc8a62090a744"
	 "c082ea27af01521a102e42f480a31e9844053f456b4b41e8aa78bbe5c12957bb"}
};


#else
# define TEST_UNSUPPORTED
# include "../common.h"
#endif


static int
kat_test(void)
{
	TEST_MAIN("BLAKE2b", BLAKE2B, 0);
}


int
main(void)
{
#ifdef SUPPORT_BLAKE2B
	struct libhashsum_hasher hasher1, hasher2;
	char salt1[16], salt2[16], salt3[16], key[64], buf[1024];
	size_t i;
#endif

	int r = kat_test();
	if (r)
		return r;

#ifdef SUPPORT_BLAKE2B
	memset(salt1, 0, sizeof(salt1));
	memset(salt2, 0xAB, sizeof(salt2));
	memset(salt3, 0xCD, sizeof(salt3));

	ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, NULL, NULL, NULL, 0));
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, salt1, NULL, NULL, 0));
	ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b"));
	ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b"));
	ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0));
	ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));

	ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, NULL, NULL, NULL, 0));
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, salt1, NULL, NULL, 0));
	ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0));
	ASSERT(!memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));

	ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, salt1, NULL, NULL, 0));
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, salt2, NULL, NULL, 0));
	ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b"));
	ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[salt=abababababababababababababababab]"));
	ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0));
	ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));

	ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, salt1, NULL, NULL, 0));
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, salt2, NULL, NULL, 0));
	ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0));
	ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));

	ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE2b[n=64,salt=00000000000000000000000000000000]"));
	ASSERT(!libhashsum_init_hasher_from_string(&hasher2, "BLAKE2b[n=64,salt=abababababababababababababababab]"));
	ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b[n=64]"));
	ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[n=64,salt=abababababababababababababababab]"));
	ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0));
	ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));
	ASSERT(hasher1.hash_size == 64U / 8U);

	ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, NULL, salt1, NULL, 0));
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, NULL, salt2, NULL, 0));
	ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b"));
	ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[pepper=abababababababababababababababab]"));
	ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0));
	ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));

	ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, NULL, NULL, NULL, 0));
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, salt2, salt3, NULL, 0));
	ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b"));
	ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[salt=abababababababababababababababab,"
	                                               "pepper=cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd]"));
	ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0));
	ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));

	ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE2b[salt=01010101010101010101010101010101,"
	                                                           "pepper=02020202020202020202020202020202]"));
	ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b[salt=01010101010101010101010101010101,"
	                                               "pepper=02020202020202020202020202020202]"));

	ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, salt2, NULL, NULL, 0));
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, NULL, salt2, NULL, 0));
	ASSERT(!hasher1.finalise_const(&hasher1, "testdata", 8U, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, "testdata", 8U, 0));
	ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));

	for (i = 0; i < sizeof(key); i++)
		key[i] = (char)i;
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher1, 0, NULL, NULL, key, 32 * 8U));
	ASSERT(!libhashsum_init_blake2b_hasher(&hasher2, 0, NULL, NULL, key, 64 * 8U));
	ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b[key=000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f]"));
	ASSERT(!strcmp(hasher2.algorithm_string, "BLAKE2b[key=000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
	                                                     "202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f]"));
	ASSERT(!hasher1.finalise_const(&hasher1, NULL, 0, 0));
	ASSERT(!hasher2.finalise_const(&hasher2, NULL, 0, 0));
	ASSERT(memcmp(hasher1.hash_output, hasher2.hash_output, hasher1.hash_size));
	hex(buf, hasher2.hash_output, hasher2.hash_size);
	ASSERT(!strcmp(buf, "10ebb67700b1868efb4417987acf4690ae9d972fb7a590c2f02871799aaa4786"
	                    "b5e996e8f0f4eb981fc214b005f42d2ff4233499391653df7aefcbc13fc51568"));

	ASSERT(!libhashsum_init_hasher_from_string(&hasher1, "BLAKE2b[pepper=02020202020202020202020202020202,key=0123456789]"));
	ASSERT(!strcmp(hasher1.algorithm_string, "BLAKE2b[pepper=02020202020202020202020202020202,key=0123456789]"));
#endif

	return 0;
}