blob: 1b7049a10034321b123f3bf09c6dfc01a349ef49 [file] [log] [blame]
Paul Bakker367dae42009-06-28 21:50:27 +00001BEGIN_HEADER
2#include <polarssl/aes.h>
3END_HEADER
4
5BEGIN_CASE
Paul Bakker2b222c82009-07-27 21:03:45 +00006aes_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string:setkey_result
Paul Bakker367dae42009-06-28 21:50:27 +00007{
8 unsigned char key_str[100];
9 unsigned char src_str[100];
10 unsigned char dst_str[100];
11 unsigned char output[100];
12 aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000013 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000014
15 memset(key_str, 0x00, 100);
16 memset(src_str, 0x00, 100);
17 memset(dst_str, 0x00, 100);
18 memset(output, 0x00, 100);
19
Paul Bakker69998dd2009-07-11 19:15:20 +000020 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +000021 unhexify( src_str, {hex_src_string} );
22
Paul Bakker2b222c82009-07-27 21:03:45 +000023 TEST_ASSERT( aes_setkey_enc( &ctx, key_str, key_len * 8 ) == {setkey_result} );
24 if( {setkey_result} == 0 )
25 {
Paul Bakkerf3ccc682010-03-18 21:21:02 +000026 TEST_ASSERT( aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000027 hexify( dst_str, output, 16 );
Paul Bakker367dae42009-06-28 21:50:27 +000028
Paul Bakker2b222c82009-07-27 21:03:45 +000029 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
30 }
Paul Bakker367dae42009-06-28 21:50:27 +000031}
32END_CASE
33
34BEGIN_CASE
Paul Bakker2b222c82009-07-27 21:03:45 +000035aes_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string:setkey_result
Paul Bakker367dae42009-06-28 21:50:27 +000036{
37 unsigned char key_str[100];
38 unsigned char src_str[100];
39 unsigned char dst_str[100];
40 unsigned char output[100];
41 aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000042 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000043
44 memset(key_str, 0x00, 100);
45 memset(src_str, 0x00, 100);
46 memset(dst_str, 0x00, 100);
47 memset(output, 0x00, 100);
48
Paul Bakker69998dd2009-07-11 19:15:20 +000049 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +000050 unhexify( src_str, {hex_src_string} );
51
Paul Bakker2b222c82009-07-27 21:03:45 +000052 TEST_ASSERT( aes_setkey_dec( &ctx, key_str, key_len * 8 ) == {setkey_result} );
53 if( {setkey_result} == 0 )
54 {
Paul Bakkerf3ccc682010-03-18 21:21:02 +000055 TEST_ASSERT( aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000056 hexify( dst_str, output, 16 );
Paul Bakker367dae42009-06-28 21:50:27 +000057
Paul Bakker2b222c82009-07-27 21:03:45 +000058 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
59 }
Paul Bakker367dae42009-06-28 21:50:27 +000060}
61END_CASE
62
63BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +000064aes_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakker367dae42009-06-28 21:50:27 +000065{
66 unsigned char key_str[100];
67 unsigned char iv_str[100];
68 unsigned char src_str[100];
69 unsigned char dst_str[100];
70 unsigned char output[100];
71 aes_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +000072 int key_len, data_len;
Paul Bakker367dae42009-06-28 21:50:27 +000073
74 memset(key_str, 0x00, 100);
75 memset(iv_str, 0x00, 100);
76 memset(src_str, 0x00, 100);
77 memset(dst_str, 0x00, 100);
78 memset(output, 0x00, 100);
79
Paul Bakker69998dd2009-07-11 19:15:20 +000080 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +000081 unhexify( iv_str, {hex_iv_string} );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000082 data_len = unhexify( src_str, {hex_src_string} );
Paul Bakker367dae42009-06-28 21:50:27 +000083
84 aes_setkey_enc( &ctx, key_str, key_len * 8 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000085 TEST_ASSERT( aes_crypt_cbc( &ctx, AES_ENCRYPT, data_len, iv_str, src_str, output ) == {cbc_result} );
86 if( {cbc_result} == 0 )
87 {
88 hexify( dst_str, output, data_len );
Paul Bakker367dae42009-06-28 21:50:27 +000089
Paul Bakkerf3ccc682010-03-18 21:21:02 +000090 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
91 }
Paul Bakker367dae42009-06-28 21:50:27 +000092}
93END_CASE
94
95BEGIN_CASE
Paul Bakkerf3ccc682010-03-18 21:21:02 +000096aes_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result
Paul Bakker367dae42009-06-28 21:50:27 +000097{
98 unsigned char key_str[100];
99 unsigned char iv_str[100];
100 unsigned char src_str[100];
101 unsigned char dst_str[100];
102 unsigned char output[100];
103 aes_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000104 int key_len, data_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000105
106 memset(key_str, 0x00, 100);
107 memset(iv_str, 0x00, 100);
108 memset(src_str, 0x00, 100);
109 memset(dst_str, 0x00, 100);
110 memset(output, 0x00, 100);
111
Paul Bakker69998dd2009-07-11 19:15:20 +0000112 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +0000113 unhexify( iv_str, {hex_iv_string} );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000114 data_len = unhexify( src_str, {hex_src_string} );
Paul Bakker367dae42009-06-28 21:50:27 +0000115
116 aes_setkey_dec( &ctx, key_str, key_len * 8 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000117 TEST_ASSERT( aes_crypt_cbc( &ctx, AES_DECRYPT, data_len, iv_str, src_str, output ) == {cbc_result} );
118 if( {cbc_result} == 0)
119 {
120 hexify( dst_str, output, data_len );
Paul Bakker367dae42009-06-28 21:50:27 +0000121
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000122 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
123 }
Paul Bakker367dae42009-06-28 21:50:27 +0000124}
125END_CASE
126
127BEGIN_CASE
128aes_encrypt_cfb128:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
129{
130 unsigned char key_str[100];
131 unsigned char iv_str[100];
132 unsigned char src_str[100];
133 unsigned char dst_str[100];
134 unsigned char output[100];
135 aes_context ctx;
136 int iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000137 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000138
139 memset(key_str, 0x00, 100);
140 memset(iv_str, 0x00, 100);
141 memset(src_str, 0x00, 100);
142 memset(dst_str, 0x00, 100);
143 memset(output, 0x00, 100);
144
Paul Bakker69998dd2009-07-11 19:15:20 +0000145 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +0000146 unhexify( iv_str, {hex_iv_string} );
147 unhexify( src_str, {hex_src_string} );
148
149 aes_setkey_enc( &ctx, key_str, key_len * 8 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000150 TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000151 hexify( dst_str, output, 16 );
152
153 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
154}
155END_CASE
156
157BEGIN_CASE
158aes_decrypt_cfb128:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
159{
160 unsigned char key_str[100];
161 unsigned char iv_str[100];
162 unsigned char src_str[100];
163 unsigned char dst_str[100];
164 unsigned char output[100];
165 aes_context ctx;
166 int iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000167 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000168
169 memset(key_str, 0x00, 100);
170 memset(iv_str, 0x00, 100);
171 memset(src_str, 0x00, 100);
172 memset(dst_str, 0x00, 100);
173 memset(output, 0x00, 100);
174
Paul Bakker69998dd2009-07-11 19:15:20 +0000175 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +0000176 unhexify( iv_str, {hex_iv_string} );
177 unhexify( src_str, {hex_src_string} );
178
179 aes_setkey_enc( &ctx, key_str, key_len * 8 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000180 TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000181 hexify( dst_str, output, 16 );
182
183 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
184}
185END_CASE
186
Paul Bakker3d360822009-07-05 11:29:38 +0000187BEGIN_CASE
188aes_selftest:
189{
190 TEST_ASSERT( aes_self_test( 0 ) == 0 );
191}
192END_CASE