blob: f6793503ea7cda78b73f2b00a1a0aa56dad8c495 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00002#include <polarssl/aes.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker367dae42009-06-28 21:50:27 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
6 * depends_on:POLARSSL_AES_C
7 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
11void aes_encrypt_ecb( char *hex_key_string, char *hex_src_string,
12 char *hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000013{
14 unsigned char key_str[100];
15 unsigned char src_str[100];
16 unsigned char dst_str[100];
17 unsigned char output[100];
18 aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000019 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000020
21 memset(key_str, 0x00, 100);
22 memset(src_str, 0x00, 100);
23 memset(dst_str, 0x00, 100);
24 memset(output, 0x00, 100);
25
Paul Bakker33b43f12013-08-20 11:48:36 +020026 key_len = unhexify( key_str, hex_key_string );
27 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000028
Paul Bakker33b43f12013-08-20 11:48:36 +020029 TEST_ASSERT( aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
30 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000031 {
Paul Bakkerf3ccc682010-03-18 21:21:02 +000032 TEST_ASSERT( aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000033 hexify( dst_str, output, 16 );
Paul Bakker367dae42009-06-28 21:50:27 +000034
Paul Bakker33b43f12013-08-20 11:48:36 +020035 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000036 }
Paul Bakker367dae42009-06-28 21:50:27 +000037}
Paul Bakker33b43f12013-08-20 11:48:36 +020038/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000039
Paul Bakker33b43f12013-08-20 11:48:36 +020040/* BEGIN_CASE */
41void aes_decrypt_ecb( char *hex_key_string, char *hex_src_string,
42 char *hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000043{
44 unsigned char key_str[100];
45 unsigned char src_str[100];
46 unsigned char dst_str[100];
47 unsigned char output[100];
48 aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000049 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000050
51 memset(key_str, 0x00, 100);
52 memset(src_str, 0x00, 100);
53 memset(dst_str, 0x00, 100);
54 memset(output, 0x00, 100);
55
Paul Bakker33b43f12013-08-20 11:48:36 +020056 key_len = unhexify( key_str, hex_key_string );
57 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000058
Paul Bakker33b43f12013-08-20 11:48:36 +020059 TEST_ASSERT( aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
60 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000061 {
Paul Bakkerf3ccc682010-03-18 21:21:02 +000062 TEST_ASSERT( aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000063 hexify( dst_str, output, 16 );
Paul Bakker367dae42009-06-28 21:50:27 +000064
Paul Bakker33b43f12013-08-20 11:48:36 +020065 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000066 }
Paul Bakker367dae42009-06-28 21:50:27 +000067}
Paul Bakker33b43f12013-08-20 11:48:36 +020068/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000069
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020070/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020071void aes_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
72 char *hex_src_string, char *hex_dst_string,
73 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000074{
75 unsigned char key_str[100];
76 unsigned char iv_str[100];
77 unsigned char src_str[100];
78 unsigned char dst_str[100];
79 unsigned char output[100];
80 aes_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +000081 int key_len, data_len;
Paul Bakker367dae42009-06-28 21:50:27 +000082
83 memset(key_str, 0x00, 100);
84 memset(iv_str, 0x00, 100);
85 memset(src_str, 0x00, 100);
86 memset(dst_str, 0x00, 100);
87 memset(output, 0x00, 100);
88
Paul Bakker33b43f12013-08-20 11:48:36 +020089 key_len = unhexify( key_str, hex_key_string );
90 unhexify( iv_str, hex_iv_string );
91 data_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000092
93 aes_setkey_enc( &ctx, key_str, key_len * 8 );
Paul Bakker33b43f12013-08-20 11:48:36 +020094 TEST_ASSERT( aes_crypt_cbc( &ctx, AES_ENCRYPT, data_len, iv_str, src_str, output ) == cbc_result );
95 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +000096 {
97 hexify( dst_str, output, data_len );
Paul Bakker367dae42009-06-28 21:50:27 +000098
Paul Bakker33b43f12013-08-20 11:48:36 +020099 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000100 }
Paul Bakker367dae42009-06-28 21:50:27 +0000101}
Paul Bakker33b43f12013-08-20 11:48:36 +0200102/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000103
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200104/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200105void aes_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
106 char *hex_src_string, char *hex_dst_string,
107 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000108{
109 unsigned char key_str[100];
110 unsigned char iv_str[100];
111 unsigned char src_str[100];
112 unsigned char dst_str[100];
113 unsigned char output[100];
114 aes_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000115 int key_len, data_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000116
117 memset(key_str, 0x00, 100);
118 memset(iv_str, 0x00, 100);
119 memset(src_str, 0x00, 100);
120 memset(dst_str, 0x00, 100);
121 memset(output, 0x00, 100);
122
Paul Bakker33b43f12013-08-20 11:48:36 +0200123 key_len = unhexify( key_str, hex_key_string );
124 unhexify( iv_str, hex_iv_string );
125 data_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000126
127 aes_setkey_dec( &ctx, key_str, key_len * 8 );
Paul Bakker33b43f12013-08-20 11:48:36 +0200128 TEST_ASSERT( aes_crypt_cbc( &ctx, AES_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
129 if( cbc_result == 0)
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000130 {
131 hexify( dst_str, output, data_len );
Paul Bakker367dae42009-06-28 21:50:27 +0000132
Paul Bakker33b43f12013-08-20 11:48:36 +0200133 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000134 }
Paul Bakker367dae42009-06-28 21:50:27 +0000135}
Paul Bakker33b43f12013-08-20 11:48:36 +0200136/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000137
Paul Bakker33b43f12013-08-20 11:48:36 +0200138/* BEGIN_CASE */
139void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
140 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000141{
142 unsigned char key_str[100];
143 unsigned char iv_str[100];
144 unsigned char src_str[100];
145 unsigned char dst_str[100];
146 unsigned char output[100];
147 aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000148 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000149 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000150
151 memset(key_str, 0x00, 100);
152 memset(iv_str, 0x00, 100);
153 memset(src_str, 0x00, 100);
154 memset(dst_str, 0x00, 100);
155 memset(output, 0x00, 100);
156
Paul Bakker33b43f12013-08-20 11:48:36 +0200157 key_len = unhexify( key_str, hex_key_string );
158 unhexify( iv_str, hex_iv_string );
159 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000160
161 aes_setkey_enc( &ctx, key_str, key_len * 8 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000162 TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000163 hexify( dst_str, output, 16 );
164
Paul Bakker33b43f12013-08-20 11:48:36 +0200165 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000166}
Paul Bakker33b43f12013-08-20 11:48:36 +0200167/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000168
Paul Bakker33b43f12013-08-20 11:48:36 +0200169/* BEGIN_CASE */
170void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
171 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000172{
173 unsigned char key_str[100];
174 unsigned char iv_str[100];
175 unsigned char src_str[100];
176 unsigned char dst_str[100];
177 unsigned char output[100];
178 aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000179 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000180 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000181
182 memset(key_str, 0x00, 100);
183 memset(iv_str, 0x00, 100);
184 memset(src_str, 0x00, 100);
185 memset(dst_str, 0x00, 100);
186 memset(output, 0x00, 100);
187
Paul Bakker33b43f12013-08-20 11:48:36 +0200188 key_len = unhexify( key_str, hex_key_string );
189 unhexify( iv_str, hex_iv_string );
190 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000191
192 aes_setkey_enc( &ctx, key_str, key_len * 8 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000193 TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000194 hexify( dst_str, output, 16 );
195
Paul Bakker33b43f12013-08-20 11:48:36 +0200196 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000197}
Paul Bakker33b43f12013-08-20 11:48:36 +0200198/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000199
Paul Bakker33b43f12013-08-20 11:48:36 +0200200/* BEGIN_CASE */
201void aes_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000202{
203 TEST_ASSERT( aes_self_test( 0 ) == 0 );
204}
Paul Bakker33b43f12013-08-20 11:48:36 +0200205/* END_CASE */