4a36dcb9c128e4bb663ba2ae2cf6367012de71e2
[strongswan.git] / Source / charon / testcases / hmac_test.c
1 /**
2 * @file hmac_test.h
3 *
4 * @brief Tests the hmac class
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 "hmac_test.h"
26
27 #include "../transforms/hmac.h"
28 #include "../utils/allocator.h"
29
30
31 /*
32 * described in Header-File
33 */
34 void test_hmac_sha1(tester_t *tester)
35 {
36 /*
37 * Test cases from RFC2202
38 *
39 * test_case = 1
40 * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
41 * key_len = 20
42 * data = "Hi There"
43 * data_len = 8
44 * digest = 0xb617318655057264e28bc0b6fb378c8ef146be00
45 *
46 * test_case = 2
47 * key = "Jefe"
48 * key_len = 4
49 * data = "what do ya want for nothing?"
50 * data_len = 28
51 * digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
52 *
53 * test_case = 3
54 * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
55 * key_len = 20
56 * data = 0xdd repeated 50 times
57 * data_len = 50
58 * digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
59 *
60 * test_case = 4
61 * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
62 * key_len = 25
63 * data = 0xcd repeated 50 times
64 * data_len = 50
65 * digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
66 *
67 * test_case = 5
68 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
69 * key_len = 20
70 * data = "Test With Truncation"
71 * data_len = 20
72 * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
73 * digest-96 = 0x4c1a03424b55e07fe7f27be1
74 *
75 * test_case = 6
76 * key = 0xaa repeated 80 times
77 * key_len = 80
78 * data = "Test Using Larger Than Block-Size Key - Hash Key First"
79 * data_len = 54
80 * digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
81 *
82 * test_case = 7
83 * key = 0xaa repeated 80 times
84 * key_len = 80
85 * data = "Test Using Larger Than Block-Size Key and Larger
86 * Than One Block-Size Data"
87 * data_len = 73
88 * digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
89 *
90 * currently performing test 1, 2 and 7
91 */
92
93 chunk_t keys[7];
94 chunk_t data[7];
95 chunk_t digest[7];
96 chunk_t reference[7];
97 int i;
98
99 /*
100 * values for test 1
101 */
102 u_int8_t key1[] = {
103 0x0b,0x0b,0x0b,0x0b,
104 0x0b,0x0b,0x0b,0x0b,
105 0x0b,0x0b,0x0b,0x0b,
106 0x0b,0x0b,0x0b,0x0b,
107 0x0b,0x0b,0x0b,0x0b
108 };
109 keys[0].ptr = key1;
110 keys[0].len = sizeof(key1);
111 data[0].ptr = "Hi There";
112 data[0].len = 8;
113 u_int8_t reference1[] = {
114 0xb6,0x17,0x31,0x86,
115 0x55,0x05,0x72,0x64,
116 0xe2,0x8b,0xc0,0xb6,
117 0xfb,0x37,0x8c,0x8e,
118 0xf1,0x46,0xbe,0x00
119 };
120 reference[0].ptr = reference1;
121 reference[0].len = sizeof(reference1);
122
123 /*
124 * values for test 2
125 */
126 u_int8_t reference2[] = {
127 0xef,0xfc,0xdf,0x6a,
128 0xe5,0xeb,0x2f,0xa2,
129 0xd2,0x74,0x16,0xd5,
130 0xf1,0x84,0xdf,0x9c,
131 0x25,0x9a,0x7c,0x79
132 };
133 keys[1].ptr = "Jefe";
134 keys[1].len = 4;
135 data[1].ptr = "what do ya want for nothing?";
136 data[1].len = 28;
137 reference[1].ptr = reference2;
138 reference[1].len = sizeof(reference2);
139
140 /*
141 * values for test 7
142 */
143 u_int8_t key7[] = {
144 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
145 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
146 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
147 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
148 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
149 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
150 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
151 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
152 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
153 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
154 };
155 u_int8_t reference7[] = {
156 0xe8,0xe9,0x9d,0x0f,
157 0x45,0x23,0x7d,0x78,
158 0x6d,0x6b,0xba,0xa7,
159 0x96,0x5c,0x78,0x08,
160 0xbb,0xff,0x1a,0x91
161 };
162 keys[2].ptr = key7;
163 keys[2].len = sizeof(key7);
164 data[2].ptr = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
165 data[2].len = 73;
166 reference[2].ptr = reference7;
167 reference[2].len = sizeof(reference7);
168
169
170 for (i=0; i<3; i++)
171 {
172 hmac_t *hmac = hmac_create(HASH_SHA1, keys[i]);
173 hmac->allocate_mac(hmac, data[i], &digest[i]);
174 hmac->destroy(hmac);
175
176 tester->assert_true(tester, digest[i].len == 20, "chunk len");
177 tester->assert_false(tester, memcmp(digest[i].ptr, reference[i].ptr, 20), "hmac value");
178 allocator_free(digest[i].ptr);
179 }
180 }