aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/testcases/hmac_test.c
blob: ccefb86cf0fca9ca363d86ecae79bc36a8e84d88 (plain)
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
/**
 * @file hmac_test.h
 * 
 * @brief Tests the hmac class 
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include <string.h>
 
#include "hmac_test.h"

#include "../transforms/hmac.h"
#include "../utils/allocator.h"


/* 
 * described in Header-File
 */
void test_hmac_sha1(tester_t *tester)
{
	/*
	 * Test cases from RFC2202
	 * 
	 * test_case =     1
	 * key =           0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
	 * key_len =       20
	 * data =          "Hi There"
	 * data_len =      8
	 * digest =        0xb617318655057264e28bc0b6fb378c8ef146be00
	 * 
	 * test_case =     2
	 * key =           "Jefe"
	 * key_len =       4
	 * data =          "what do ya want for nothing?"
	 * data_len =      28
	 * digest =        0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
	 * 
	 * test_case =     3
	 * key =           0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
	 * key_len =       20
	 * data =          0xdd repeated 50 times
	 * data_len =      50
	 * digest =        0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
	 * 
	 * test_case =     4
	 * key =           0x0102030405060708090a0b0c0d0e0f10111213141516171819
	 * key_len =       25
	 * data =          0xcd repeated 50 times
	 * data_len =      50
	 * digest =        0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
	 * 
	 * test_case =     5
	 * key =           0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
	 * key_len =       20
	 * data =          "Test With Truncation"
	 * data_len =      20
	 * digest =        0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
	 * digest-96 =     0x4c1a03424b55e07fe7f27be1
	 * 
	 * test_case =     6
	 * key =           0xaa repeated 80 times
	 * key_len =       80
	 * data =          "Test Using Larger Than Block-Size Key - Hash Key First"
	 * data_len =      54
	 * digest =        0xaa4ae5e15272d00e95705637ce8a3b55ed402112
	 *  
	 * test_case =     7
	 * key =           0xaa repeated 80 times
	 * key_len =       80
	 * data =          "Test Using Larger Than Block-Size Key and Larger
	 *                 Than One Block-Size Data"
	 * data_len =      73
	 * digest =        0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
	 * 
	 * currently performing test 1, 2 and 7
	 */
	
	chunk_t keys[7];
	chunk_t data[7];
	chunk_t digest[7];
	chunk_t reference[7];
	int i;
	
	/*
	 * values for test 1
	 */
	u_int8_t key1[] = {
		0x0b,0x0b,0x0b,0x0b,
		0x0b,0x0b,0x0b,0x0b,
		0x0b,0x0b,0x0b,0x0b,
		0x0b,0x0b,0x0b,0x0b,
		0x0b,0x0b,0x0b,0x0b
	};
	keys[0].ptr = key1;
	keys[0].len = sizeof(key1);
	data[0].ptr = "Hi There";
	data[0].len = 8; 
	u_int8_t reference1[] = {
		0xb6,0x17,0x31,0x86,
		0x55,0x05,0x72,0x64,
		0xe2,0x8b,0xc0,0xb6,
		0xfb,0x37,0x8c,0x8e,
		0xf1,0x46,0xbe,0x00
	};
	reference[0].ptr = reference1;
	reference[0].len = sizeof(reference1);
	
	/*
	 * values for test 2
	 */
	u_int8_t reference2[] = {
		0xef,0xfc,0xdf,0x6a,
		0xe5,0xeb,0x2f,0xa2,
		0xd2,0x74,0x16,0xd5,
		0xf1,0x84,0xdf,0x9c,
		0x25,0x9a,0x7c,0x79
	};
	keys[1].ptr = "Jefe";
	keys[1].len = 4;
	data[1].ptr = "what do ya want for nothing?";
	data[1].len = 28; 
	reference[1].ptr = reference2;
	reference[1].len = sizeof(reference2);     
	
	/*
	 * values for test 7
	 */
	u_int8_t key7[] = {
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
		0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
	};
	u_int8_t reference7[] = {
		0xe8,0xe9,0x9d,0x0f,
		0x45,0x23,0x7d,0x78,
		0x6d,0x6b,0xba,0xa7,
		0x96,0x5c,0x78,0x08,
		0xbb,0xff,0x1a,0x91
	};
	keys[2].ptr = key7;
	keys[2].len = sizeof(key7);
	data[2].ptr = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
	data[2].len = 73; 
	reference[2].ptr = reference7;
	reference[2].len = sizeof(reference7);
	
	
 	for (i=0; i<3; i++)
 	{
	 	hmac_t *hmac = hmac_create(HASH_SHA1);
	 	hmac->set_key(hmac, keys[i]);
		hmac->allocate_mac(hmac, data[i], &digest[i]);
		hmac->destroy(hmac);
		
		tester->assert_true(tester, digest[i].len == 20, "chunk len");
		tester->assert_false(tester, memcmp(digest[i].ptr, reference[i].ptr, 20), "hmac value");
		allocator_free(digest[i].ptr);
 	}
}