blob: 3440202b48d6f52b86936aab47840b8ef1e20d11 [file] [log] [blame]
Paul Bakker367dae42009-06-28 21:50:27 +00001BEGIN_HEADER
2#include <polarssl/aes.h>
3END_HEADER
4
5BEGIN_CASE
6aes_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
7{
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
23 aes_setkey_enc( &ctx, key_str, key_len * 8 );
24 aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output );
25 hexify( dst_str, output, 16 );
26
27 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
28}
29END_CASE
30
31BEGIN_CASE
32aes_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string
33{
34 unsigned char key_str[100];
35 unsigned char src_str[100];
36 unsigned char dst_str[100];
37 unsigned char output[100];
38 aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000039 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000040
41 memset(key_str, 0x00, 100);
42 memset(src_str, 0x00, 100);
43 memset(dst_str, 0x00, 100);
44 memset(output, 0x00, 100);
45
Paul Bakker69998dd2009-07-11 19:15:20 +000046 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +000047 unhexify( src_str, {hex_src_string} );
48
49 aes_setkey_dec( &ctx, key_str, key_len * 8 );
50 aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output );
51 hexify( dst_str, output, 16 );
52
53 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
54}
55END_CASE
56
57BEGIN_CASE
58aes_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
59{
60 unsigned char key_str[100];
61 unsigned char iv_str[100];
62 unsigned char src_str[100];
63 unsigned char dst_str[100];
64 unsigned char output[100];
65 aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000066 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000067
68 memset(key_str, 0x00, 100);
69 memset(iv_str, 0x00, 100);
70 memset(src_str, 0x00, 100);
71 memset(dst_str, 0x00, 100);
72 memset(output, 0x00, 100);
73
Paul Bakker69998dd2009-07-11 19:15:20 +000074 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +000075 unhexify( iv_str, {hex_iv_string} );
76 unhexify( src_str, {hex_src_string} );
77
78 aes_setkey_enc( &ctx, key_str, key_len * 8 );
79 aes_crypt_cbc( &ctx, AES_ENCRYPT, 16, iv_str, src_str, output );
80 hexify( dst_str, output, 16 );
81
82 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
83}
84END_CASE
85
86BEGIN_CASE
87aes_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
88{
89 unsigned char key_str[100];
90 unsigned char iv_str[100];
91 unsigned char src_str[100];
92 unsigned char dst_str[100];
93 unsigned char output[100];
94 aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000095 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000096
97 memset(key_str, 0x00, 100);
98 memset(iv_str, 0x00, 100);
99 memset(src_str, 0x00, 100);
100 memset(dst_str, 0x00, 100);
101 memset(output, 0x00, 100);
102
Paul Bakker69998dd2009-07-11 19:15:20 +0000103 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +0000104 unhexify( iv_str, {hex_iv_string} );
105 unhexify( src_str, {hex_src_string} );
106
107 aes_setkey_dec( &ctx, key_str, key_len * 8 );
108 aes_crypt_cbc( &ctx, AES_DECRYPT, 16, iv_str, src_str, output );
109 hexify( dst_str, output, 16 );
110
111 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
112}
113END_CASE
114
115BEGIN_CASE
116aes_encrypt_cfb128:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
117{
118 unsigned char key_str[100];
119 unsigned char iv_str[100];
120 unsigned char src_str[100];
121 unsigned char dst_str[100];
122 unsigned char output[100];
123 aes_context ctx;
124 int iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000125 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000126
127 memset(key_str, 0x00, 100);
128 memset(iv_str, 0x00, 100);
129 memset(src_str, 0x00, 100);
130 memset(dst_str, 0x00, 100);
131 memset(output, 0x00, 100);
132
Paul Bakker69998dd2009-07-11 19:15:20 +0000133 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +0000134 unhexify( iv_str, {hex_iv_string} );
135 unhexify( src_str, {hex_src_string} );
136
137 aes_setkey_enc( &ctx, key_str, key_len * 8 );
138 aes_crypt_cfb128( &ctx, AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output );
139 hexify( dst_str, output, 16 );
140
141 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
142}
143END_CASE
144
145BEGIN_CASE
146aes_decrypt_cfb128:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string
147{
148 unsigned char key_str[100];
149 unsigned char iv_str[100];
150 unsigned char src_str[100];
151 unsigned char dst_str[100];
152 unsigned char output[100];
153 aes_context ctx;
154 int iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000155 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000156
157 memset(key_str, 0x00, 100);
158 memset(iv_str, 0x00, 100);
159 memset(src_str, 0x00, 100);
160 memset(dst_str, 0x00, 100);
161 memset(output, 0x00, 100);
162
Paul Bakker69998dd2009-07-11 19:15:20 +0000163 key_len = unhexify( key_str, {hex_key_string} );
Paul Bakker367dae42009-06-28 21:50:27 +0000164 unhexify( iv_str, {hex_iv_string} );
165 unhexify( src_str, {hex_src_string} );
166
167 aes_setkey_enc( &ctx, key_str, key_len * 8 );
168 aes_crypt_cfb128( &ctx, AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output );
169 hexify( dst_str, output, 16 );
170
171 TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 );
172}
173END_CASE
174
Paul Bakker3d360822009-07-05 11:29:38 +0000175BEGIN_CASE
176aes_selftest:
177{
178 TEST_ASSERT( aes_self_test( 0 ) == 0 );
179}
180END_CASE