-
Notifications
You must be signed in to change notification settings - Fork 82
/
test.c
217 lines (183 loc) · 5.1 KB
/
test.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
SHA1 tests by Philip Woolford <[email protected]>
100% Public Domain
*/
#include "sha1.h"
#include "CUnit/Basic.h"
#include "stdio.h"
#include "string.h"
#define SUCCESS 0
/* The suite initialization function.
* Returns zero on success, non-zero otherwise.
*/
int init_suite(
void
)
{
return 0;
}
/* The suite cleanup function.
* Returns zero on success, non-zero otherwise.
*/
int clean_suite(
void
)
{
return 0;
}
/* Test Vector 1 */
void testvec1(
void
)
{
char const string[] = "abc";
char const expect[] = "a9993e364706816aba3e25717850c26c9cd0d89d";
char result[21];
char hexresult[41];
size_t offset;
/* calculate hash */
SHA1( result, string, strlen(string) );
/* format the hash for comparison */
for( offset = 0; offset < 20; offset++) {
sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
}
CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
}
/* Test Vector 2 */
void testvec2(
void
)
{
char const string[] = "";
char const expect[] = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
char result[21];
char hexresult[41];
size_t offset;
/* calculate hash */
SHA1( result, string, strlen(string) );
/*format the hash for comparison */
for( offset = 0; offset < 20; offset++) {
sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
}
CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
}
/* Test Vector 3 */
void testvec3(
void
)
{
char const string[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
char const expect[] = "84983e441c3bd26ebaae4aa1f95129e5e54670f1";
char result[21];
char hexresult[41];
size_t offset;
/* calculate hash */
SHA1( result, string, strlen(string) );
/* format the hash for comparison */
for( offset = 0; offset < 20; offset++) {
sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
}
CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
}
/* Test Vector 4 */
void testvec4(
void
)
{
char const string1[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghij";
char const string2[] = "klmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
char const expect[] = "a49b2446a02c645bf419f995b67091253a04a259";
unsigned char result[21];
char hexresult[41];
size_t offset;
SHA1_CTX ctx;
/* calculate hash */
SHA1Init(&ctx);
SHA1Update( &ctx, (unsigned char const *)string1, strlen(string1) );
SHA1Update( &ctx, (unsigned char const *)string2, strlen(string2) );
SHA1Final(result, &ctx);
/* format the hash for comparison */
for( offset = 0; offset < 20; offset++) {
sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
}
CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
}
/* Test Vector 5 */
void testvec5(
void
)
{
char string[1000001];
char const expect[] = "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
char result[21];
char hexresult[41];
int iterator;
size_t offset;
/* generate string */
for( iterator = 0; iterator < 1000000; iterator++) {
string[iterator] = 'a';
}
string[1000000] = '\0';
/* calculate hash */
SHA1( result, string, strlen(string) );
/* format the hash for comparison */
for( offset = 0; offset < 20; offset++) {
sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
}
CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
}
/* Test Vector 6 */
void testvec6(
void
)
{
char const string[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno";
char const expect[] = "7789f0c9ef7bfc40d93311143dfbe69e2017f592";
unsigned char result[21];
char hexresult[41];
int iterator;
size_t offset;
SHA1_CTX ctx;
/* calculate hash */
SHA1Init(&ctx);
for ( iterator = 0; iterator < 16777216; iterator++) {
SHA1Update( &ctx, (unsigned char const *)string, strlen(string) );
}
SHA1Final(result, &ctx);
/* format the hash for comparison */
for( offset = 0; offset < 20; offset++) {
sprintf( ( hexresult + (2*offset)), "%02x", result[offset]&0xff);
}
CU_ASSERT( strncmp(hexresult, expect, 40) == SUCCESS );
}
int main(
void
)
{
CU_pSuite pSuite = NULL;
/* initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* add a suite to the registry */
pSuite = CU_add_suite("http://www.di-mgt.com.au/sha_testvectors.html", init_suite, clean_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* add the tests to the suite */
if ((NULL == CU_add_test(pSuite, "Test of Test Vector 1", testvec1)) ||
(NULL == CU_add_test(pSuite, "Test of Test Vector 2", testvec2)) ||
(NULL == CU_add_test(pSuite, "Test of Test Vector 3", testvec3)) ||
(NULL == CU_add_test(pSuite, "Test of Test Vector 4", testvec4)) ||
(NULL == CU_add_test(pSuite, "Test of Test Vector 5", testvec5)) ||
(NULL == CU_add_test(pSuite, "Test of Test Vector 6", testvec6)))
{
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}