- moved hasher_*_t to *_hasher_t
[strongswan.git] / Source / charon / testcases / hasher_test.c
1 /**
2 * @file md5_hasher_test.h
3 *
4 * @brief Tests the md5 hasher
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "hasher_test.h"
26
27 #include <utils/allocator.h>
28
29
30 /*
31 * described in Header-File
32 */
33 void test_md5_hasher(tester_t *tester)
34 {
35 /*
36 * Test vectors from RFC1321:
37 * MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
38 * MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
39 * MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
40 * MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
41 * MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
42 *
43 * currently testing "", "abc", "abcdefghijklmnopqrstuvwxyz"
44 */
45 hasher_t *hasher = hasher_create(HASH_MD5);
46 u_int8_t hash_buffer[16];
47 chunk_t empty, abc, abcd, hash_chunk;
48
49 u_int8_t hash_empty[] = {
50 0xd4,0x1d,0x8c,0xd9,
51 0x8f,0x00,0xb2,0x04,
52 0xe9,0x80,0x09,0x98,
53 0xec,0xf8,0x42,0x7e
54 };
55
56 u_int8_t hash_abc[] = {
57 0x90,0x01,0x50,0x98,
58 0x3c,0xd2,0x4f,0xb0,
59 0xd6,0x96,0x3f,0x7d,
60 0x28,0xe1,0x7f,0x72
61 };
62
63 u_int8_t hash_abcd[] = {
64 0xc3,0xfc,0xd3,0xd7,
65 0x61,0x92,0xe4,0x00,
66 0x7d,0xfb,0x49,0x6c,
67 0xca,0x67,0xe1,0x3b
68 };
69
70 empty.ptr = "";
71 empty.len = 0;
72 abc.ptr = "abc";
73 abc.len = 3;
74 abcd.ptr = "abcdefghijklmnopqrstuvwxyz";
75 abcd.len = strlen(abcd.ptr);
76
77 tester->assert_true(tester, hasher->get_block_size(hasher) == 16, "block size");
78
79 /* simple hashing, using empty */
80 hasher->get_hash(hasher, empty, hash_buffer);
81 tester->assert_false(tester, memcmp(hash_buffer, hash_empty, 16), "hash for empty");
82
83 /* simple hashing, using "abc" */
84 hasher->get_hash(hasher, abc, hash_buffer);
85 tester->assert_false(tester, memcmp(hash_buffer, hash_abc, 16), "hash for abc");
86
87 /* with allocation, using "abcdb..." */
88 hasher->reset(hasher);
89 hasher->allocate_hash(hasher, abcd, &hash_chunk);
90 tester->assert_true(tester, hash_chunk.len == 16, "hash len");
91 tester->assert_false(tester, memcmp(hash_chunk.ptr, hash_abcd, hash_chunk.len), "hash for abcd...");
92 allocator_free(hash_chunk.ptr);
93 tester->assert_true(tester, (hasher->destroy(hasher) == SUCCESS), "destroy call test");
94 }
95
96 /*
97 * described in Header-File
98 */
99 void test_sha1_hasher(tester_t *tester)
100 {
101 /*
102 * Test Vectors (from FIPS PUB 180-1)
103 * "abc"
104 * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
105 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
106 * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
107 * A million repetitions of "a"
108 * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
109 */
110 hasher_t *hasher = hasher_create(HASH_SHA1);
111 u_int8_t hash_buffer[20];
112 chunk_t abc, abcdb, aaa, hash_chunk;
113 u_int32_t i;
114 u_int8_t hash_abc[] = {
115 0xA9,0x99,0x3E,0x36,
116 0x47,0x06,0x81,0x6A,
117 0xBA,0x3E,0x25,0x71,
118 0x78,0x50,0xC2,0x6C,
119 0x9C,0xD0,0xD8,0x9D
120 };
121 u_int8_t hash_abcdb[] = {
122 0x84,0x98,0x3E,0x44,
123 0x1C,0x3B,0xD2,0x6E,
124 0xBA,0xAE,0x4A,0xA1,
125 0xF9,0x51,0x29,0xE5,
126 0xE5,0x46,0x70,0xF1
127 };
128 u_int8_t hash_aaa[] = {
129 0x34,0xAA,0x97,0x3C,
130 0xD4,0xC4,0xDA,0xA4,
131 0xF6,0x1E,0xEB,0x2B,
132 0xDB,0xAD,0x27,0x31,
133 0x65,0x34,0x01,0x6F
134 };
135 abc.ptr = "abc";
136 abc.len = 3;
137 abcdb.ptr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
138 abcdb.len = strlen(abcdb.ptr);
139 aaa.ptr = "aaaaaaaaaa"; /* 10 a's */
140 aaa.len = 10;
141
142 tester->assert_true(tester, hasher->get_block_size(hasher) == 20, "block size");
143
144 /* simple hashing, using "abc" */
145 hasher->get_hash(hasher, abc, hash_buffer);
146 tester->assert_false(tester, memcmp(hash_buffer, hash_abc, 20), "hash for abc");
147
148 /* with allocation, using "abcdb..." */
149 hasher->reset(hasher);
150 hasher->allocate_hash(hasher, abcdb, &hash_chunk);
151 tester->assert_true(tester, hash_chunk.len == 20, "chunk len");
152 tester->assert_false(tester, memcmp(hash_chunk.ptr, hash_abcdb, hash_chunk.len), "hash for abcdb...");
153 allocator_free(hash_chunk.ptr);
154
155 /* updating, using "aaaaaaa..." */
156 hasher->reset(hasher);
157 for(i=0; i<100000; i++)
158 {
159 if (i != 99999)
160 {
161 hasher->get_hash(hasher, aaa, NULL);
162 }
163 else
164 {
165 hasher->get_hash(hasher, aaa, hash_buffer);
166 }
167 }
168 tester->assert_false(tester, memcmp(hash_buffer, hash_aaa, 20), "hash for aaa...");
169
170
171 tester->assert_true(tester, (hasher->destroy(hasher) == SUCCESS), "destroy call test");
172 }