blob: 91f5fa2ded658bb8e130400490c4b6a899aefacd [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_AES_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * 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];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 mbedtls_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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000026
Paul Bakker33b43f12013-08-20 11:48:36 +020027 key_len = unhexify( key_str, hex_key_string );
28 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020031 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000034 hexify( dst_str, output, 16 );
Paul Bakker367dae42009-06-28 21:50:27 +000035
Paul Bakker33b43f12013-08-20 11:48:36 +020036 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000037 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020038
Paul Bakkerbd51b262014-07-10 15:26:12 +020039exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000041}
Paul Bakker33b43f12013-08-20 11:48:36 +020042/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000043
Paul Bakker33b43f12013-08-20 11:48:36 +020044/* BEGIN_CASE */
45void aes_decrypt_ecb( char *hex_key_string, char *hex_src_string,
46 char *hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000047{
48 unsigned char key_str[100];
49 unsigned char src_str[100];
50 unsigned char dst_str[100];
51 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000053 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000054
55 memset(key_str, 0x00, 100);
56 memset(src_str, 0x00, 100);
57 memset(dst_str, 0x00, 100);
58 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000060
Paul Bakker33b43f12013-08-20 11:48:36 +020061 key_len = unhexify( key_str, hex_key_string );
62 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020065 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000068 hexify( dst_str, output, 16 );
Paul Bakker367dae42009-06-28 21:50:27 +000069
Paul Bakker33b43f12013-08-20 11:48:36 +020070 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000071 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020072
Paul Bakkerbd51b262014-07-10 15:26:12 +020073exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000075}
Paul Bakker33b43f12013-08-20 11:48:36 +020076/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020079void aes_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
80 char *hex_src_string, char *hex_dst_string,
81 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000082{
83 unsigned char key_str[100];
84 unsigned char iv_str[100];
85 unsigned char src_str[100];
86 unsigned char dst_str[100];
87 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_aes_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +000089 int key_len, data_len;
Paul Bakker367dae42009-06-28 21:50:27 +000090
91 memset(key_str, 0x00, 100);
92 memset(iv_str, 0x00, 100);
93 memset(src_str, 0x00, 100);
94 memset(dst_str, 0x00, 100);
95 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000097
Paul Bakker33b43f12013-08-20 11:48:36 +020098 key_len = unhexify( key_str, hex_key_string );
99 unhexify( iv_str, hex_iv_string );
100 data_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
103 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_ENCRYPT, data_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200104 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000105 {
106 hexify( dst_str, output, data_len );
Paul Bakker367dae42009-06-28 21:50:27 +0000107
Paul Bakker33b43f12013-08-20 11:48:36 +0200108 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000109 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200110
Paul Bakkerbd51b262014-07-10 15:26:12 +0200111exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000113}
Paul Bakker33b43f12013-08-20 11:48:36 +0200114/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200117void aes_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
118 char *hex_src_string, char *hex_dst_string,
119 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000120{
121 unsigned char key_str[100];
122 unsigned char iv_str[100];
123 unsigned char src_str[100];
124 unsigned char dst_str[100];
125 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_aes_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000127 int key_len, data_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000128
129 memset(key_str, 0x00, 100);
130 memset(iv_str, 0x00, 100);
131 memset(src_str, 0x00, 100);
132 memset(dst_str, 0x00, 100);
133 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000135
Paul Bakker33b43f12013-08-20 11:48:36 +0200136 key_len = unhexify( key_str, hex_key_string );
137 unhexify( iv_str, hex_iv_string );
138 data_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 );
141 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200142 if( cbc_result == 0)
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000143 {
144 hexify( dst_str, output, data_len );
Paul Bakker367dae42009-06-28 21:50:27 +0000145
Paul Bakker33b43f12013-08-20 11:48:36 +0200146 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000147 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200148
Paul Bakkerbd51b262014-07-10 15:26:12 +0200149exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000151}
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000153
Aorimn5f778012016-06-09 23:22:58 +0200154/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
155void aes_encrypt_xts( char *hex_key_string, char *hex_iv_string,
156 char *hex_src_string, char *hex_dst_string,
157 int data_unit_len, int xts_result )
158{
159 unsigned char key_str[100] = { 0, };
160 unsigned char iv_str[100] = { 0, };
161 unsigned char src_str[100] = { 0, };
162 unsigned char dst_str[100] = { 0, };
163 unsigned char output[100] = { 0, };
164 mbedtls_aes_context crypt_ctx, tweak_ctx;
165 int key_len, data_len;
166
167 mbedtls_aes_init( &crypt_ctx );
168 mbedtls_aes_init( &tweak_ctx );
169
170 key_len = unhexify( key_str, hex_key_string );
171 unhexify( iv_str, hex_iv_string );
172 data_len = unhexify( src_str, hex_src_string );
173
174 mbedtls_aes_setkey_enc( &crypt_ctx, key_str, ( key_len * 8 ) / 2 );
175 mbedtls_aes_setkey_enc( &tweak_ctx, key_str + key_len / 2, ( key_len * 8 ) / 2 );
176
177 TEST_ASSERT( mbedtls_aes_crypt_xts( &crypt_ctx, &tweak_ctx, MBEDTLS_AES_ENCRYPT, data_unit_len, iv_str, src_str, output ) == xts_result );
178 if( xts_result == 0 )
179 {
180 hexify( dst_str, output, data_len );
181
182 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
183 }
184
185exit:
186 mbedtls_aes_free( &crypt_ctx );
187 mbedtls_aes_free( &tweak_ctx );
188}
189/* END_CASE */
190
191/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
192void aes_decrypt_xts( char *hex_key_string, char *hex_iv_string,
193 char *hex_src_string, char *hex_dst_string,
194 int data_unit_len, int xts_result )
195{
196 unsigned char key_str[100] = { 0, };
197 unsigned char iv_str[100] = { 0, };
198 unsigned char src_str[100] = { 0, };
199 unsigned char dst_str[100] = { 0, };
200 unsigned char output[100] = { 0, };
201 mbedtls_aes_context crypt_ctx, tweak_ctx;
202 int key_len, data_len;
203
204 mbedtls_aes_init( &crypt_ctx );
205 mbedtls_aes_init( &tweak_ctx );
206
207 key_len = unhexify( key_str, hex_key_string );
208 unhexify( iv_str, hex_iv_string );
209 data_len = unhexify( src_str, hex_src_string );
210
211 mbedtls_aes_setkey_dec( &crypt_ctx, key_str, ( key_len * 8 ) / 2 );
212 mbedtls_aes_setkey_enc( &tweak_ctx, key_str + key_len / 2, ( key_len * 8 ) / 2 );
213
214 TEST_ASSERT( mbedtls_aes_crypt_xts( &crypt_ctx, &tweak_ctx, MBEDTLS_AES_DECRYPT, data_unit_len, iv_str, src_str, output ) == xts_result );
215 if( xts_result == 0 )
216 {
217 hexify( dst_str, output, data_len );
218
219 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
220 }
221
222exit:
223 mbedtls_aes_free( &crypt_ctx );
224 mbedtls_aes_free( &tweak_ctx );
225}
226/* END_CASE */
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200229void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
230 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000231{
232 unsigned char key_str[100];
233 unsigned char iv_str[100];
234 unsigned char src_str[100];
235 unsigned char dst_str[100];
236 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000238 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000239 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000240
241 memset(key_str, 0x00, 100);
242 memset(iv_str, 0x00, 100);
243 memset(src_str, 0x00, 100);
244 memset(dst_str, 0x00, 100);
245 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000247
Paul Bakker33b43f12013-08-20 11:48:36 +0200248 key_len = unhexify( key_str, hex_key_string );
249 unhexify( iv_str, hex_iv_string );
250 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
253 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000254 hexify( dst_str, output, 16 );
255
Paul Bakker33b43f12013-08-20 11:48:36 +0200256 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200257
Paul Bakkerbd51b262014-07-10 15:26:12 +0200258exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000260}
Paul Bakker33b43f12013-08-20 11:48:36 +0200261/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200264void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
265 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000266{
267 unsigned char key_str[100];
268 unsigned char iv_str[100];
269 unsigned char src_str[100];
270 unsigned char dst_str[100];
271 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000273 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000274 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000275
276 memset(key_str, 0x00, 100);
277 memset(iv_str, 0x00, 100);
278 memset(src_str, 0x00, 100);
279 memset(dst_str, 0x00, 100);
280 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000282
Paul Bakker33b43f12013-08-20 11:48:36 +0200283 key_len = unhexify( key_str, hex_key_string );
284 unhexify( iv_str, hex_iv_string );
285 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
288 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakker367dae42009-06-28 21:50:27 +0000289 hexify( dst_str, output, 16 );
290
Paul Bakker33b43f12013-08-20 11:48:36 +0200291 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200292
Paul Bakkerbd51b262014-07-10 15:26:12 +0200293exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000295}
Paul Bakker33b43f12013-08-20 11:48:36 +0200296/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100299void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string,
300 char *hex_src_string, char *hex_dst_string )
301{
302 unsigned char key_str[100];
303 unsigned char iv_str[100];
304 unsigned char src_str[100];
305 unsigned char dst_str[100];
306 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100308 int key_len, src_len;
309
310 memset(key_str, 0x00, 100);
311 memset(iv_str, 0x00, 100);
312 memset(src_str, 0x00, 100);
313 memset(dst_str, 0x00, 100);
314 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100316
317 key_len = unhexify( key_str, hex_key_string );
318 unhexify( iv_str, hex_iv_string );
319 src_len = unhexify( src_str, hex_src_string );
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
322 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_len, iv_str, src_str, output ) == 0 );
Paul Bakker556efba2014-01-24 15:38:12 +0100323 hexify( dst_str, output, src_len );
324
325 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200326
Paul Bakkerbd51b262014-07-10 15:26:12 +0200327exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100329}
330/* END_CASE */
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100333void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string,
334 char *hex_src_string, char *hex_dst_string )
335{
336 unsigned char key_str[100];
337 unsigned char iv_str[100];
338 unsigned char src_str[100];
339 unsigned char dst_str[100];
340 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100342 int key_len, src_len;
343
344 memset(key_str, 0x00, 100);
345 memset(iv_str, 0x00, 100);
346 memset(src_str, 0x00, 100);
347 memset(dst_str, 0x00, 100);
348 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100350
351 key_len = unhexify( key_str, hex_key_string );
352 unhexify( iv_str, hex_iv_string );
353 src_len = unhexify( src_str, hex_src_string );
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
356 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_len, iv_str, src_str, output ) == 0 );
Paul Bakker556efba2014-01-24 15:38:12 +0100357 hexify( dst_str, output, src_len );
358
359 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200360
Paul Bakkerbd51b262014-07-10 15:26:12 +0200361exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100363}
364/* END_CASE */
365
Simon Butcher03018842018-04-22 22:57:58 +0100366/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
367void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
Simon Butcher00131442018-05-22 22:40:36 +0100368 char *hex_iv_string, char *hex_src_string,
369 char *hex_dst_string )
Simon Butcher03018842018-04-22 22:57:58 +0100370{
Simon Butchere416bf92018-06-02 18:28:32 +0100371 unsigned char key_str[32];
372 unsigned char iv_str[16];
373 unsigned char src_str[64];
374 unsigned char dst_str[64];
375 unsigned char output[32];
Simon Butcher03018842018-04-22 22:57:58 +0100376 mbedtls_aes_context ctx;
377 size_t iv_offset = 0;
378 int in_buffer_len;
379 unsigned char* src_str_next;
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100380 int key_len;
Simon Butcher03018842018-04-22 22:57:58 +0100381
Simon Butcherb7836e12018-06-02 18:36:49 +0100382 memset( key_str, 0x00, sizeof( key_str ) );
383 memset( iv_str, 0x00, sizeof( iv_str ) );
384 memset( src_str, 0x00, sizeof( src_str ) );
385 memset( dst_str, 0x00, sizeof( dst_str ) );
386 memset( output, 0x00, sizeof( output ) );
Simon Butcher03018842018-04-22 22:57:58 +0100387 mbedtls_aes_init( &ctx );
388
Simon Butchere416bf92018-06-02 18:28:32 +0100389 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
390 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
391 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
392 TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
393
Simon Butcher03018842018-04-22 22:57:58 +0100394 key_len = unhexify( key_str, hex_key_string );
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100395 unhexify( iv_str, hex_iv_string );
Simon Butcher03018842018-04-22 22:57:58 +0100396 in_buffer_len = unhexify( src_str, hex_src_string );
397
Simon Butcherad4e4932018-04-29 00:43:47 +0100398 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100399 src_str_next = src_str;
400
401 while( in_buffer_len > 0 )
402 {
403 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
404 iv_str, src_str_next, output ) == 0 );
405
406 hexify( dst_str, output, fragment_size );
407 TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
Simon Butcher00131442018-05-22 22:40:36 +0100408 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100409
410 in_buffer_len -= fragment_size;
411 hex_dst_string += ( fragment_size * 2 );
412 src_str_next += fragment_size;
413
414 if( in_buffer_len < fragment_size )
415 fragment_size = in_buffer_len;
416 }
417
418exit:
419 mbedtls_aes_free( &ctx );
420}
421/* END_CASE */
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200424void aes_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000425{
Andres AG93012e82016-09-09 09:10:28 +0100426 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000427}
Paul Bakker33b43f12013-08-20 11:48:36 +0200428/* END_CASE */