- AUTH_HMAC_MD5_96 and AUTH_HMAC_SHA1_96 implemented and tested...
[strongswan.git] / Source / charon / testcases / hmac_signer_test.c
1 /**
2 * @file hmac_signer_test.c
3 *
4 * @brief Tests the hmac SHA1 and MD5 signer class hmac_signer_t
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
24 #include <string.h>
25
26 #include "hmac_signer_test.h"
27
28 #include <transforms/signers/signer.h>
29 #include <utils/allocator.h>
30 #include <globals.h>
31
32
33 /*
34 * Described in header.
35 */
36 void test_hmac_md5_signer(tester_t *tester)
37 {
38 /* Test cases from RFC2202
39 *
40 * test_case = 5
41 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
42 * key_len = 16
43 * data = "Test With Truncation"
44 * data_len = 20
45 * digest = 0x56461ef2342edc00f9bab995690efd4c
46 * digest-96 0x56461ef2342edc00f9bab995
47 *
48 * currently only this test 5 gets performed!
49 */
50 chunk_t keys[4];
51 chunk_t data[4];
52 chunk_t signature[4];
53 chunk_t reference[4];
54 chunk_t wrong_reference[4];
55 int i;
56 logger_t *logger;
57 bool valid;
58
59 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"HMAC MD5 96");
60
61 signer_t *signer = (signer_t *) signer_create(AUTH_HMAC_MD5_96);
62 tester->assert_true(tester, (signer != NULL), "signer create call check");
63
64
65 /*
66 * values for test 5
67 */
68 u_int8_t key1[] = {
69 0x0c,0x0c,0x0c,0x0c,
70 0x0c,0x0c,0x0c,0x0c,
71 0x0c,0x0c,0x0c,0x0c,
72 0x0c,0x0c,0x0c,0x0c,
73 };
74 keys[0].ptr = key1;
75 keys[0].len = sizeof(key1);
76 data[0].ptr = "Test With Truncation";
77 data[0].len = 20;
78 u_int8_t reference1[] = {
79 0x56,0x46,0x1e,0xf2,0x34,0x2e,
80 0xdc,0x00,0xf9,0xba,0xb9,0x95
81 };
82 reference[0].ptr = reference1;
83 reference[0].len = sizeof(reference1);
84
85 u_int8_t wrong_reference1[] = {
86 0x56,0x46,0x1e,0xa2,0x34,0x2e,
87 0xdc,0x00,0xf9,0xba,0xb9,0x95
88 };
89
90 wrong_reference[0].ptr = wrong_reference1;
91 wrong_reference[0].len = sizeof(wrong_reference1);
92
93 for (i=0; i<1; i++)
94 {
95 signer->set_key(signer, keys[i]);
96 signer->allocate_signature(signer, data[i], &signature[i]);
97 tester->assert_true(tester, signature[i].len == 12, "chunk len");
98 tester->assert_true(tester, (memcmp(signature[i].ptr, reference[i].ptr, 12) == 0), "hmac value");
99 logger->log_chunk(logger,RAW,"expected signature:",&reference[i]);
100 logger->log_chunk(logger,RAW,"signature:",&signature[i]);
101 allocator_free(signature[i].ptr);
102 signer->verify_signature(signer, data[i],reference[i], &valid);
103 tester->assert_true(tester, (valid == TRUE), "Signature valid check");
104
105 signer->verify_signature(signer, data[i],wrong_reference[i], &valid);
106 tester->assert_true(tester, (valid == FALSE), "Signature not valid check");
107 }
108
109
110
111 tester->assert_true(tester, (signer->destroy(signer) == SUCCESS), "signer destroy call check");
112
113 global_logger_manager->destroy_logger(global_logger_manager,logger);
114 }
115
116
117 /*
118 * Described in header.
119 */
120 void test_hmac_sha1_signer(tester_t *tester)
121 {
122 /*
123 * test_case = 7
124 * key = 0xaa repeated 80 times
125 * key_len = 80
126 * data = "Test Using Larger Than Block-Size Key and Larger
127 * Than One Block-Size Data"
128 * data_len = 73
129 * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
130 * digest-96 = 0x4c1a03424b55e07fe7f27be1
131 */
132
133 chunk_t keys[4];
134 chunk_t data[4];
135 chunk_t signature[4];
136 chunk_t reference[4];
137 chunk_t wrong_reference[4];
138 int i;
139 logger_t *logger;
140 bool valid;
141
142 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"HMAC SHA1 96");
143
144 signer_t *signer = (signer_t *) signer_create(AUTH_HMAC_SHA1_96);
145 tester->assert_true(tester, (signer != NULL), "signer create call check");
146
147
148 /*
149 * values for test 5
150 */
151 u_int8_t key1[] = {
152 0xaa,0xaa,0xaa,0xaa,
153 0xaa,0xaa,0xaa,0xaa,
154 0xaa,0xaa,0xaa,0xaa,
155 0xaa,0xaa,0xaa,0xaa,
156 0xaa,0xaa,0xaa,0xaa,
157 0xaa,0xaa,0xaa,0xaa,
158 0xaa,0xaa,0xaa,0xaa,
159 0xaa,0xaa,0xaa,0xaa,
160 0xaa,0xaa,0xaa,0xaa,
161 0xaa,0xaa,0xaa,0xaa,
162 0xaa,0xaa,0xaa,0xaa,
163 0xaa,0xaa,0xaa,0xaa,
164 0xaa,0xaa,0xaa,0xaa,
165 0xaa,0xaa,0xaa,0xaa,
166 0xaa,0xaa,0xaa,0xaa,
167 0xaa,0xaa,0xaa,0xaa,
168 0xaa,0xaa,0xaa,0xaa,
169 0xaa,0xaa,0xaa,0xaa,
170 0xaa,0xaa,0xaa,0xaa,
171 0xaa,0xaa,0xaa,0xaa,
172 };
173 keys[0].ptr = key1;
174 keys[0].len = sizeof(key1);
175 data[0].ptr = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
176 data[0].len = 73;
177 u_int8_t reference1[] = {
178 0xe8,0xe9,0x9d,0x0f,0x45,0x23,
179 0x7d,0x78,0x6d,0x6b,0xba,0xa7
180 };
181 reference[0].ptr = reference1;
182 reference[0].len = sizeof(reference1);
183
184 u_int8_t wrong_reference1[] = {
185 0xe8,0xe9,0x9d,0x0f,0x46,0x23,
186 0x7d,0x71,0x6d,0x6b,0xba,0xa7
187 };
188
189 wrong_reference[0].ptr = wrong_reference1;
190 wrong_reference[0].len = sizeof(wrong_reference1);
191
192 for (i=0; i<1; i++)
193 {
194 signer->set_key(signer, keys[i]);
195 signer->allocate_signature(signer, data[i], &signature[i]);
196 tester->assert_true(tester, signature[i].len == 12, "chunk len");
197 tester->assert_true(tester, (memcmp(signature[i].ptr, reference[i].ptr, 12) == 0), "hmac value");
198 logger->log_chunk(logger,RAW,"expected signature:",&reference[i]);
199 logger->log_chunk(logger,RAW,"signature:",&signature[i]);
200 allocator_free(signature[i].ptr);
201 signer->verify_signature(signer, data[i],reference[i], &valid);
202 tester->assert_true(tester, (valid == TRUE), "Signature valid check");
203
204 signer->verify_signature(signer, data[i],wrong_reference[i], &valid);
205 tester->assert_true(tester, (valid == FALSE), "Signature not valid check");
206 }
207
208
209
210 tester->assert_true(tester, (signer->destroy(signer) == SUCCESS), "signer destroy call check");
211
212 global_logger_manager->destroy_logger(global_logger_manager,logger);
213
214 }