fixed aes code, we support now aes128, aes192, aes256 in IKE
[strongswan.git] / src / charon / testing / aes_cbc_crypter_test.c
1 /**
2 * @file aes_cbc_crypter_test.c
3 *
4 * @brief Tests for the aes_cbc_crypter_t 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 "aes_cbc_crypter_test.h"
26
27 #include <daemon.h>
28
29 void test_aes_cbc_crypter(protected_tester_t *tester)
30 {
31 /*
32 * Test 1 of RFC3602
33 * Key : 0x06a9214036b8a15b512e03d534120006
34 * IV : 0x3dafba429d9eb430b422da802c9fac41
35 * Plaintext : "Single block msg"
36 * Ciphertext: 0xe353779c1079aeb82708942dbe77181a
37 */
38 crypter_t *crypter;
39 u_int8_t key1[] = {0x06,0xa9,0x21,0x40,0x36,0xb8,0xa1,0x5b,
40 0x51,0x2e,0x03,0xd5,0x34,0x12,0x00,0x06};
41 chunk_t key1_chunk = {ptr: key1, len : 16};
42 u_int8_t iv1[] = {0x3d,0xaf,0xba,0x42,0x9d,0x9e,0xb4,0x30,
43 0xb4,0x22,0xda,0x80,0x2c,0x9f,0xac,0x41};
44 chunk_t iv1_chunk = {ptr: iv1, len : 16};
45 u_int8_t ciphertext1[] = { 0xe3,0x53,0x77,0x9c,0x10,0x79,0xae,0xb8,
46 0x27,0x08,0x94,0x2d,0xbe,0x77,0x18,0x1a};
47
48 chunk_t expected_encrypted1 = {ptr: ciphertext1, len : 16};
49 char * plaintext1 = "Single block msg";
50 chunk_t data1 = {ptr: plaintext1, len : 16};
51 chunk_t encrypted1;
52 chunk_t decrypted1;
53 logger_t *logger;
54
55 logger = logger_manager->get_logger(logger_manager,TESTER);
56
57 crypter = (crypter_t *) aes_cbc_crypter_create(16);
58 tester->assert_true(tester, (crypter != NULL), "create call test");
59
60 tester->assert_true(tester, (crypter->set_key(crypter,key1_chunk) == SUCCESS), "set_key call test");
61
62 tester->assert_true(tester, (crypter->encrypt(crypter,data1,iv1_chunk,&encrypted1) == SUCCESS), "encrypt call test");
63
64 tester->assert_true(tester, (memcmp(encrypted1.ptr, expected_encrypted1.ptr, 16) == 0), "Encrypted value");
65
66 logger->log_chunk(logger,RAW,"exptected encrypted :", expected_encrypted1);
67 logger->log_chunk(logger,RAW,"encrypted :", encrypted1);
68
69 tester->assert_true(tester, (crypter->decrypt(crypter,encrypted1,iv1_chunk,&decrypted1) == SUCCESS), "decrypt call test");
70 chunk_free(&encrypted1);
71
72 tester->assert_true(tester, (memcmp(decrypted1.ptr, plaintext1, 16) == 0), "decrypted value");
73
74 logger->log_chunk(logger,RAW,"expected decrypted :", data1);
75 logger->log_chunk(logger,RAW,"decrypted :", decrypted1);
76
77 chunk_free(&decrypted1);
78
79 crypter->destroy(crypter);
80
81
82 /*
83 * Test 2 of RFC3602
84 * Key : 0xc286696d887c9aa0611bbb3e2025a45a
85 * IV : 0x562e17996d093d28ddb3ba695a2e6f58
86 * Plaintext : 0x000102030405060708090a0b0c0d0e0f
87 * 101112131415161718191a1b1c1d1e1f
88 * Ciphertext: 0xd296cd94c2cccf8a3a863028b5e1dc0a
89 * 7586602d253cfff91b8266bea6d61ab1
90 */
91 u_int8_t key2[] = {0xc2,0x86,0x69,0x6d,0x88,0x7c,0x9a,0xa0,
92 0x61,0x1b,0xbb,0x3e,0x20,0x25,0xa4,0x5a};
93 chunk_t key2_chunk = {ptr: key2, len : 16};
94 u_int8_t iv2[] = {0x56,0x2e,0x17,0x99,0x6d,0x09,0x3d,0x28,
95 0xdd,0xb3,0xba,0x69,0x5a,0x2e,0x6f,0x58};
96 chunk_t iv2_chunk = {ptr: iv2, len : 16};
97 u_int8_t ciphertext2[] = { 0xd2,0x96,0xcd,0x94,0xc2,0xcc,0xcf,0x8a,
98 0x3a,0x86,0x30,0x28,0xb5,0xe1,0xdc,0x0a,
99 0x75,0x86,0x60,0x2d,0x25,0x3c,0xff,0xf9,
100 0x1b,0x82,0x66,0xbe,0xa6,0xd6,0x1a,0xb1};
101
102 chunk_t expected_encrypted2 = {ptr: ciphertext2, len : 32};
103 u_int8_t plaintext2[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
104 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
105 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
106 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f};
107 chunk_t data2 = {ptr: plaintext2, len : 32};
108 chunk_t encrypted2;
109 chunk_t decrypted2;
110
111
112 crypter = (crypter_t *) aes_cbc_crypter_create(16);
113 tester->assert_true(tester, (crypter != NULL), "create call test");
114
115 tester->assert_true(tester, (crypter->set_key(crypter,key2_chunk) == SUCCESS), "set_key call test");
116
117 tester->assert_true(tester, (crypter->encrypt(crypter,data2,iv2_chunk,&encrypted2) == SUCCESS), "encrypt call test");
118
119 tester->assert_true(tester, (memcmp(encrypted2.ptr, expected_encrypted2.ptr, 32) == 0), "Encrypted value");
120
121 logger->log_chunk(logger,RAW,"exptected encrypted :", expected_encrypted2);
122 logger->log_chunk(logger,RAW,"encrypted :", encrypted2);
123
124 tester->assert_true(tester, (crypter->decrypt(crypter,encrypted2,iv2_chunk,&decrypted2) == SUCCESS), "decrypt call test");
125 chunk_free(&encrypted2);
126
127 tester->assert_true(tester, (memcmp(decrypted2.ptr, plaintext2, 32) == 0), "decrypted value");
128
129 logger->log_chunk(logger,RAW,"expected decrypted :", data2);
130 logger->log_chunk(logger,RAW,"decrypted :", decrypted2);
131
132 chunk_free(&decrypted2);
133
134 crypter->destroy(crypter);
135
136 /*
137 * Test 3 of RFC3603
138 * Key : 0x56e47a38c5598974bc46903dba290349
139 * IV : 0x8ce82eefbea0da3c44699ed7db51b7d9
140 * Plaintext : 0xa0a1a2a3a4a5a6a7a8a9aaabacadaeaf
141 * b0b1b2b3b4b5b6b7b8b9babbbcbdbebf
142 * c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
143 * d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
144 * Ciphertext: 0xc30e32ffedc0774e6aff6af0869f71aa
145 * 0f3af07a9a31a9c684db207eb0ef8e4e
146 * 35907aa632c3ffdf868bb7b29d3d46ad
147 * 83ce9f9a102ee99d49a53e87f4c3da55
148 */
149 u_int8_t key3[] = {0x56,0xe4,0x7a,0x38,0xc5,0x59,0x89,0x74,
150 0xbc,0x46,0x90,0x3d,0xba,0x29,0x03,0x49};
151 chunk_t key3_chunk = {ptr: key3, len : 16};
152 u_int8_t iv3[] = {0x8c,0xe8,0x2e,0xef,0xbe,0xa0,0xda,0x3c,
153 0x44,0x69,0x9e,0xd7,0xdb,0x51,0xb7,0xd9};
154 chunk_t iv3_chunk = {ptr: iv3, len : 16};
155 u_int8_t ciphertext3[] = { 0xc3,0x0e,0x32,0xff,0xed,0xc0,0x77,0x4e,
156 0x6a,0xff,0x6a,0xf0,0x86,0x9f,0x71,0xaa,
157 0x0f,0x3a,0xf0,0x7a,0x9a,0x31,0xa9,0xc6,
158 0x84,0xdb,0x20,0x7e,0xb0,0xef,0x8e,0x4e,
159 0x35,0x90,0x7a,0xa6,0x32,0xc3,0xff,0xdf,
160 0x86,0x8b,0xb7,0xb2,0x9d,0x3d,0x46,0xad,
161 0x83,0xce,0x9f,0x9a,0x10,0x2e,0xe9,0x9d,
162 0x49,0xa5,0x3e,0x87,0xf4,0xc3,0xda,0x55};
163
164 chunk_t expected_encrypted3 = {ptr: ciphertext3, len : 64};
165 u_int8_t plaintext3[] = {0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,
166 0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
167 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,
168 0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
169 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
170 0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
171 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,
172 0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf};
173 chunk_t data3 = {ptr: plaintext3, len : 64};
174 chunk_t encrypted3;
175 chunk_t decrypted3;
176
177 crypter = (crypter_t *) aes_cbc_crypter_create(16);
178 tester->assert_true(tester, (crypter != NULL), "create call test");
179
180 tester->assert_true(tester, (crypter->set_key(crypter,key3_chunk) == SUCCESS), "set_key call test");
181
182 tester->assert_true(tester, (crypter->encrypt(crypter,data3,iv3_chunk,&encrypted3) == SUCCESS), "encrypt call test");
183
184 tester->assert_true(tester, (memcmp(encrypted3.ptr, expected_encrypted3.ptr, 64) == 0), "Encrypted value");
185
186 logger->log_chunk(logger,RAW,"exptected encrypted :", expected_encrypted3);
187 logger->log_chunk(logger,RAW,"encrypted :", encrypted3);
188
189 tester->assert_true(tester, (crypter->decrypt(crypter,encrypted3,iv3_chunk,&decrypted3) == SUCCESS), "decrypt call test");
190 chunk_free(&encrypted3);
191
192 tester->assert_true(tester, (memcmp(decrypted3.ptr, plaintext3, 64) == 0), "decrypted value");
193
194 logger->log_chunk(logger,RAW,"expected decrypted :", data3);
195 logger->log_chunk(logger,RAW,"decrypted :", decrypted3);
196
197 chunk_free(&decrypted3);
198
199 crypter->destroy(crypter);
200
201 /**
202 * Test4: Own en-/decrypt test using AES-256
203 *
204 * PLAINTEXT: 00112233445566778899aabbccddeeff
205 * KEY: 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
206 * INPUT: 00112233445566778899aabbccddeeff
207 * OUTPUT: 8ea2b7ca516745bfeafc49904b496089
208 */
209 u_int8_t key4[] = {
210 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
211 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
212 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
213 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f
214 };
215 chunk_t key4_chunk = {ptr: key4, len : 32};
216 u_int8_t iv4[] = {
217 0x8c,0xe8,0x2e,0xef,0xbe,0xa0,0xda,0x3c,
218 0x44,0x69,0x9e,0xd7,0xdb,0x51,0xb7,0xd9
219 };
220 chunk_t iv4_chunk = {ptr: iv4, len : 16};
221 u_int8_t plaintext4[] = {
222 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,
223 0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
224 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,
225 0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
226 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
227 0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
228 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,
229 0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf
230 };
231 chunk_t data4 = {ptr: plaintext4, len : 64};
232 chunk_t encrypted4;
233 chunk_t decrypted4;
234
235 crypter = (crypter_t *) aes_cbc_crypter_create(32);
236 tester->assert_true(tester, (crypter != NULL), "create call test");
237
238 tester->assert_true(tester, (crypter->set_key(crypter,key4_chunk) == SUCCESS), "set_key call test");
239
240 tester->assert_true(tester, (crypter->encrypt(crypter,data4,iv4_chunk,&encrypted4) == SUCCESS), "encrypt call test");
241
242 tester->assert_true(tester, (crypter->decrypt(crypter,encrypted4,iv4_chunk,&decrypted4) == SUCCESS), "decrypt call test");
243 chunk_free(&encrypted4);
244
245 logger->log_chunk(logger,RAW,"expected decrypted :", data4);
246 logger->log_chunk(logger,RAW,"decrypted :", decrypted4);
247 tester->assert_true(tester, (memcmp(decrypted4.ptr, plaintext4, 64) == 0), "decrypted value");
248
249 chunk_free(&decrypted4);
250 crypter->destroy(crypter);
251
252 /**
253 * Test4: Own en-/decrypt test using AES-192
254 *
255 * PLAINTEXT: 00112233445566778899aabbccddeeff
256 * KEY: 000102030405060708090a0b0c0d0e0f1011121314151617
257 * INPUT: 00112233445566778899aabbccddeeff
258 * OUTPUT: 8ea2b7ca516745bfeafc49904b496089
259 */
260 u_int8_t key5[] = {
261 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
262 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
263 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17
264 };
265 chunk_t key5_chunk = {ptr: key5, len : 24};
266 u_int8_t iv5[] = {
267 0x8c,0xe8,0x2e,0xef,0xbe,0xa0,0xda,0x3c,
268 0x44,0x69,0x9e,0xd7,0xdb,0x51,0xb7,0xd9
269 };
270 chunk_t iv5_chunk = {ptr: iv5, len : 16};
271 u_int8_t plaintext5[] = {
272 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,
273 0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
274 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,
275 0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
276 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
277 0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
278 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,
279 0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf
280 };
281 chunk_t data5 = {ptr: plaintext5, len : 64};
282 chunk_t encrypted5;
283 chunk_t decrypted5;
284
285 crypter = (crypter_t *) aes_cbc_crypter_create(24);
286 tester->assert_true(tester, (crypter != NULL), "create call test");
287
288 tester->assert_true(tester, (crypter->set_key(crypter,key5_chunk) == SUCCESS), "set_key call test");
289
290 tester->assert_true(tester, (crypter->encrypt(crypter,data5,iv5_chunk,&encrypted5) == SUCCESS), "encrypt call test");
291
292 tester->assert_true(tester, (crypter->decrypt(crypter,encrypted5,iv5_chunk,&decrypted5) == SUCCESS), "decrypt call test");
293 chunk_free(&encrypted4);
294
295 logger->log_chunk(logger,RAW,"expected decrypted :", data5);
296 logger->log_chunk(logger,RAW,"decrypted :", decrypted5);
297 tester->assert_true(tester, (memcmp(decrypted5.ptr, plaintext5, 64) == 0), "decrypted value");
298
299 chunk_free(&decrypted5);
300 crypter->destroy(crypter);
301 }
302