22e8e25752fb7bd1929a4ff8ba1deb07edc14e90
[strongswan.git] / src / charon / plugins / unit_tester / tests / test_aes.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <library.h>
17 #include <utils/mutex.h>
18
19 #include <unistd.h>
20 #include <sched.h>
21 #include <pthread.h>
22
23 /**
24 * run a test using given values
25 */
26 static bool do_aes_test(u_char *key, int keysize, u_char *iv,
27 u_char *plain, u_char *cipher, int len)
28 {
29 crypter_t *crypter;
30 chunk_t enc, dec;
31 bool good = TRUE;
32
33 crypter = lib->crypto->create_crypter(lib->crypto, ENCR_AES_CBC, keysize);
34 if (!crypter)
35 {
36 return FALSE;
37 }
38 crypter->set_key(crypter, chunk_create(key, keysize));
39 crypter->encrypt(crypter,
40 chunk_create(plain, len), chunk_create(iv, 16), &enc);
41 if (!memeq(enc.ptr, cipher, len))
42 {
43 good = FALSE;
44 }
45 crypter->decrypt(crypter, enc, chunk_create(iv, 16), &dec);
46 if (!memeq(dec.ptr, plain, len))
47 {
48 good = FALSE;
49 }
50 free(enc.ptr);
51 free(dec.ptr);
52 crypter->destroy(crypter);
53 return good;
54 }
55
56 /*******************************************************************************
57 * AES-128 test
58 ******************************************************************************/
59 bool test_aes128()
60 {
61 /*
62 * Test 1 of RFC3602
63 * Key : 0x06a9214036b8a15b512e03d534120006
64 * IV : 0x3dafba429d9eb430b422da802c9fac41
65 * Plaintext : "Single block msg"
66 * Ciphertext: 0xe353779c1079aeb82708942dbe77181a
67 */
68 u_char key1[] = {
69 0x06,0xa9,0x21,0x40,0x36,0xb8,0xa1,0x5b,
70 0x51,0x2e,0x03,0xd5,0x34,0x12,0x00,0x06
71 };
72 u_char iv1[] = {
73 0x3d,0xaf,0xba,0x42,0x9d,0x9e,0xb4,0x30,
74 0xb4,0x22,0xda,0x80,0x2c,0x9f,0xac,0x41
75 };
76 u_char plain1[] = {
77 'S','i','n','g','l','e',' ','b','l','o','c','k',' ','m','s','g'
78 };
79 u_char cipher1[] = {
80 0xe3,0x53,0x77,0x9c,0x10,0x79,0xae,0xb8,
81 0x27,0x08,0x94,0x2d,0xbe,0x77,0x18,0x1a
82 };
83 if (!do_aes_test(key1, 16, iv1, plain1, cipher1, sizeof(plain1)))
84 {
85 return FALSE;
86 }
87
88 /*
89 * Test 2 of RFC3602
90 * Key : 0xc286696d887c9aa0611bbb3e2025a45a
91 * IV : 0x562e17996d093d28ddb3ba695a2e6f58
92 * Plaintext : 0x000102030405060708090a0b0c0d0e0f
93 * 101112131415161718191a1b1c1d1e1f
94 * Ciphertext: 0xd296cd94c2cccf8a3a863028b5e1dc0a
95 * 7586602d253cfff91b8266bea6d61ab1
96 */
97 u_char key2[] = {
98 0xc2,0x86,0x69,0x6d,0x88,0x7c,0x9a,0xa0,
99 0x61,0x1b,0xbb,0x3e,0x20,0x25,0xa4,0x5a
100 };
101 u_char iv2[] = {
102 0x56,0x2e,0x17,0x99,0x6d,0x09,0x3d,0x28,
103 0xdd,0xb3,0xba,0x69,0x5a,0x2e,0x6f,0x58
104 };
105 u_char plain2[] = {
106 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
107 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
108 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
109 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f
110 };
111 u_char cipher2[] = {
112 0xd2,0x96,0xcd,0x94,0xc2,0xcc,0xcf,0x8a,
113 0x3a,0x86,0x30,0x28,0xb5,0xe1,0xdc,0x0a,
114 0x75,0x86,0x60,0x2d,0x25,0x3c,0xff,0xf9,
115 0x1b,0x82,0x66,0xbe,0xa6,0xd6,0x1a,0xb1
116 };
117 if (!do_aes_test(key2, 16, iv2, plain2, cipher2, sizeof(plain2)))
118 {
119 return FALSE;
120 }
121
122 /*
123 * Test 3 of RFC3603
124 * Key : 0x56e47a38c5598974bc46903dba290349
125 * IV : 0x8ce82eefbea0da3c44699ed7db51b7d9
126 * Plaintext : 0xa0a1a2a3a4a5a6a7a8a9aaabacadaeaf
127 * b0b1b2b3b4b5b6b7b8b9babbbcbdbebf
128 * c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
129 * d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
130 * Ciphertext: 0xc30e32ffedc0774e6aff6af0869f71aa
131 * 0f3af07a9a31a9c684db207eb0ef8e4e
132 * 35907aa632c3ffdf868bb7b29d3d46ad
133 * 83ce9f9a102ee99d49a53e87f4c3da55
134 */
135 u_char key3[] = {
136 0x56,0xe4,0x7a,0x38,0xc5,0x59,0x89,0x74,
137 0xbc,0x46,0x90,0x3d,0xba,0x29,0x03,0x49
138 };
139 u_char iv3[] = {
140 0x8c,0xe8,0x2e,0xef,0xbe,0xa0,0xda,0x3c,
141 0x44,0x69,0x9e,0xd7,0xdb,0x51,0xb7,0xd9
142 };
143 u_char plain3[] = {
144 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,
145 0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
146 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,
147 0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
148 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
149 0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
150 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,
151 0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf
152 };
153 u_char cipher3[] = {
154 0xc3,0x0e,0x32,0xff,0xed,0xc0,0x77,0x4e,
155 0x6a,0xff,0x6a,0xf0,0x86,0x9f,0x71,0xaa,
156 0x0f,0x3a,0xf0,0x7a,0x9a,0x31,0xa9,0xc6,
157 0x84,0xdb,0x20,0x7e,0xb0,0xef,0x8e,0x4e,
158 0x35,0x90,0x7a,0xa6,0x32,0xc3,0xff,0xdf,
159 0x86,0x8b,0xb7,0xb2,0x9d,0x3d,0x46,0xad,
160 0x83,0xce,0x9f,0x9a,0x10,0x2e,0xe9,0x9d,
161 0x49,0xa5,0x3e,0x87,0xf4,0xc3,0xda,0x55
162 };
163 if (!do_aes_test(key3, 16, iv3, plain3, cipher3, sizeof(plain3)))
164 {
165 return FALSE;
166 }
167 return TRUE;
168 }