blob: e346dc7c32e5d4281503dbb33988c61102cbaf71 [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 */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100155void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100156 char *hex_src_string, char *hex_dst_string )
Aorimn5f778012016-06-09 23:22:58 +0200157{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100158 enum { AES_BLOCK_SIZE = 16 };
159 unsigned char *data_unit = NULL;
160 unsigned char *key = NULL;
161 unsigned char *src = NULL;
162 unsigned char *dst = NULL;
163 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100164 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100165 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200166
Jaeden Amero9366feb2018-05-29 18:55:17 +0100167 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200168
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100169 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
170 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200171
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100172 key = unhexify_alloc( hex_key_string, &key_len );
173 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200174
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100175 src = unhexify_alloc( hex_src_string, &src_len );
176 dst = unhexify_alloc( hex_dst_string, &dst_len );
177 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200178
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100179 output = zero_alloc( dst_len );
180
181 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 );
182 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len,
183 data_unit, src, output ) == 0 );
184
185 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200186
187exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100188 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100189 mbedtls_free( data_unit );
190 mbedtls_free( key );
191 mbedtls_free( src );
192 mbedtls_free( dst );
193 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200194}
195/* END_CASE */
196
197/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100198void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100199 char *hex_dst_string, char *hex_src_string )
Aorimn5f778012016-06-09 23:22:58 +0200200{
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100201 enum { AES_BLOCK_SIZE = 16 };
202 unsigned char *data_unit = NULL;
203 unsigned char *key = NULL;
204 unsigned char *src = NULL;
205 unsigned char *dst = NULL;
206 unsigned char *output = NULL;
Jaeden Amero9366feb2018-05-29 18:55:17 +0100207 mbedtls_aes_xts_context ctx;
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100208 size_t key_len, src_len, dst_len, data_unit_len;
Aorimn5f778012016-06-09 23:22:58 +0200209
Jaeden Amero9366feb2018-05-29 18:55:17 +0100210 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200211
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100212 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
213 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
Aorimn5f778012016-06-09 23:22:58 +0200214
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100215 key = unhexify_alloc( hex_key_string, &key_len );
216 TEST_ASSERT( key_len % 2 == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200217
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100218 src = unhexify_alloc( hex_src_string, &src_len );
219 dst = unhexify_alloc( hex_dst_string, &dst_len );
220 TEST_ASSERT( src_len == dst_len );
Aorimn5f778012016-06-09 23:22:58 +0200221
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100222 output = zero_alloc( dst_len );
223
224 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 );
225 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len,
226 data_unit, src, output ) == 0 );
227
228 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
Aorimn5f778012016-06-09 23:22:58 +0200229
230exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100231 mbedtls_aes_xts_free( &ctx );
Jaeden Ameroe5c4b072018-04-28 17:26:25 +0100232 mbedtls_free( data_unit );
233 mbedtls_free( key );
234 mbedtls_free( src );
235 mbedtls_free( dst );
236 mbedtls_free( output );
Aorimn5f778012016-06-09 23:22:58 +0200237}
238/* END_CASE */
239
Jaeden Amero425382d2018-04-28 17:26:25 +0100240/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
241void aes_crypt_xts_size( int size, int retval )
242{
243 mbedtls_aes_xts_context ctx;
244 const unsigned char *src = NULL;
245 unsigned char *output = NULL;
246 unsigned char data_unit[16];
247 size_t length = size;
248
249 mbedtls_aes_xts_init( &ctx );
250 memset( data_unit, 0x00, sizeof( data_unit ) );
251
252
253 /* Note that this function will most likely crash on failure, as NULL
254 * parameters will be used. In the passing case, the length check in
255 * mbedtls_aes_crypt_xts() will prevent any accesses to parameters by
256 * exiting the function early. */
257 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval );
258}
259/* END_CASE */
260
Jaeden Amero142383e2018-05-31 10:40:34 +0100261/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
262void aes_crypt_xts_keysize( int size, int retval )
263{
264 mbedtls_aes_xts_context ctx;
265 const unsigned char *key = NULL;
266 size_t key_len = size;
267
268 mbedtls_aes_xts_init( &ctx );
269
270 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval );
271 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval );
272exit:
273 mbedtls_aes_xts_free( &ctx );
274}
275/* END_CASE */
Jaeden Amero425382d2018-04-28 17:26:25 +0100276
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200279void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
280 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000281{
282 unsigned char key_str[100];
283 unsigned char iv_str[100];
284 unsigned char src_str[100];
285 unsigned char dst_str[100];
286 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000288 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000289 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000290
291 memset(key_str, 0x00, 100);
292 memset(iv_str, 0x00, 100);
293 memset(src_str, 0x00, 100);
294 memset(dst_str, 0x00, 100);
295 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000297
Paul Bakker33b43f12013-08-20 11:48:36 +0200298 key_len = unhexify( key_str, hex_key_string );
299 unhexify( iv_str, hex_iv_string );
300 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
303 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 +0000304 hexify( dst_str, output, 16 );
305
Paul Bakker33b43f12013-08-20 11:48:36 +0200306 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200307
Paul Bakkerbd51b262014-07-10 15:26:12 +0200308exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000310}
Paul Bakker33b43f12013-08-20 11:48:36 +0200311/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200314void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
315 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000316{
317 unsigned char key_str[100];
318 unsigned char iv_str[100];
319 unsigned char src_str[100];
320 unsigned char dst_str[100];
321 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000323 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000324 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000325
326 memset(key_str, 0x00, 100);
327 memset(iv_str, 0x00, 100);
328 memset(src_str, 0x00, 100);
329 memset(dst_str, 0x00, 100);
330 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000332
Paul Bakker33b43f12013-08-20 11:48:36 +0200333 key_len = unhexify( key_str, hex_key_string );
334 unhexify( iv_str, hex_iv_string );
335 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
338 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 +0000339 hexify( dst_str, output, 16 );
340
Paul Bakker33b43f12013-08-20 11:48:36 +0200341 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200342
Paul Bakkerbd51b262014-07-10 15:26:12 +0200343exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000345}
Paul Bakker33b43f12013-08-20 11:48:36 +0200346/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100349void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string,
350 char *hex_src_string, char *hex_dst_string )
351{
352 unsigned char key_str[100];
353 unsigned char iv_str[100];
354 unsigned char src_str[100];
355 unsigned char dst_str[100];
356 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100358 int key_len, src_len;
359
360 memset(key_str, 0x00, 100);
361 memset(iv_str, 0x00, 100);
362 memset(src_str, 0x00, 100);
363 memset(dst_str, 0x00, 100);
364 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100366
367 key_len = unhexify( key_str, hex_key_string );
368 unhexify( iv_str, hex_iv_string );
369 src_len = unhexify( src_str, hex_src_string );
370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
372 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 +0100373 hexify( dst_str, output, src_len );
374
375 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200376
Paul Bakkerbd51b262014-07-10 15:26:12 +0200377exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100379}
380/* END_CASE */
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100383void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string,
384 char *hex_src_string, char *hex_dst_string )
385{
386 unsigned char key_str[100];
387 unsigned char iv_str[100];
388 unsigned char src_str[100];
389 unsigned char dst_str[100];
390 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100392 int key_len, src_len;
393
394 memset(key_str, 0x00, 100);
395 memset(iv_str, 0x00, 100);
396 memset(src_str, 0x00, 100);
397 memset(dst_str, 0x00, 100);
398 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100400
401 key_len = unhexify( key_str, hex_key_string );
402 unhexify( iv_str, hex_iv_string );
403 src_len = unhexify( src_str, hex_src_string );
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
406 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 +0100407 hexify( dst_str, output, src_len );
408
409 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200410
Paul Bakkerbd51b262014-07-10 15:26:12 +0200411exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100413}
414/* END_CASE */
415
Simon Butcher03018842018-04-22 22:57:58 +0100416/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
417void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
Simon Butcher00131442018-05-22 22:40:36 +0100418 char *hex_iv_string, char *hex_src_string,
419 char *hex_dst_string )
Simon Butcher03018842018-04-22 22:57:58 +0100420{
Simon Butchere416bf92018-06-02 18:28:32 +0100421 unsigned char key_str[32];
422 unsigned char iv_str[16];
423 unsigned char src_str[64];
424 unsigned char dst_str[64];
425 unsigned char output[32];
Simon Butcher03018842018-04-22 22:57:58 +0100426 mbedtls_aes_context ctx;
427 size_t iv_offset = 0;
428 int in_buffer_len;
429 unsigned char* src_str_next;
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100430 int key_len;
Simon Butcher03018842018-04-22 22:57:58 +0100431
Simon Butcherb7836e12018-06-02 18:36:49 +0100432 memset( key_str, 0x00, sizeof( key_str ) );
433 memset( iv_str, 0x00, sizeof( iv_str ) );
434 memset( src_str, 0x00, sizeof( src_str ) );
435 memset( dst_str, 0x00, sizeof( dst_str ) );
436 memset( output, 0x00, sizeof( output ) );
Simon Butcher03018842018-04-22 22:57:58 +0100437 mbedtls_aes_init( &ctx );
438
Simon Butchere416bf92018-06-02 18:28:32 +0100439 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
440 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
441 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
442 TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
443
Simon Butcher03018842018-04-22 22:57:58 +0100444 key_len = unhexify( key_str, hex_key_string );
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100445 unhexify( iv_str, hex_iv_string );
Simon Butcher03018842018-04-22 22:57:58 +0100446 in_buffer_len = unhexify( src_str, hex_src_string );
447
Simon Butcherad4e4932018-04-29 00:43:47 +0100448 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100449 src_str_next = src_str;
450
451 while( in_buffer_len > 0 )
452 {
453 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
454 iv_str, src_str_next, output ) == 0 );
455
456 hexify( dst_str, output, fragment_size );
457 TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
Simon Butcher00131442018-05-22 22:40:36 +0100458 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100459
460 in_buffer_len -= fragment_size;
461 hex_dst_string += ( fragment_size * 2 );
462 src_str_next += fragment_size;
463
464 if( in_buffer_len < fragment_size )
465 fragment_size = in_buffer_len;
466 }
467
468exit:
469 mbedtls_aes_free( &ctx );
470}
471/* END_CASE */
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200474void aes_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000475{
Andres AG93012e82016-09-09 09:10:28 +0100476 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000477}
Paul Bakker33b43f12013-08-20 11:48:36 +0200478/* END_CASE */