blob: 165f702e604d091e56c0df91283b2efe483dcbee [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,
Aorimn5f778012016-06-09 23:22:58 +0200156 char *hex_src_string, char *hex_dst_string,
157 int data_unit_len, int xts_result )
158{
159 unsigned char key_str[100] = { 0, };
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100160 unsigned char data_unit_str[100] = { 0, };
Aorimn5f778012016-06-09 23:22:58 +0200161 unsigned char src_str[100] = { 0, };
162 unsigned char dst_str[100] = { 0, };
163 unsigned char output[100] = { 0, };
Jaeden Amero9366feb2018-05-29 18:55:17 +0100164 mbedtls_aes_xts_context ctx;
Aorimn5f778012016-06-09 23:22:58 +0200165 int key_len, data_len;
166
Jaeden Amero9366feb2018-05-29 18:55:17 +0100167 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200168
169 key_len = unhexify( key_str, hex_key_string );
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100170 unhexify( data_unit_str, hex_data_unit_string );
Aorimn5f778012016-06-09 23:22:58 +0200171 data_len = unhexify( src_str, hex_src_string );
Jaeden Amero5162b932018-05-29 12:55:24 +0100172 TEST_ASSERT( data_len == data_unit_len / 8 );
Aorimn5f778012016-06-09 23:22:58 +0200173
Jaeden Amero9366feb2018-05-29 18:55:17 +0100174 mbedtls_aes_xts_setkey_enc( &ctx, key_str, key_len * 8 );
Aorimn5f778012016-06-09 23:22:58 +0200175
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100176 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, data_len, data_unit_str, src_str, output ) == xts_result );
Aorimn5f778012016-06-09 23:22:58 +0200177 if( xts_result == 0 )
178 {
179 hexify( dst_str, output, data_len );
180
181 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
182 }
183
184exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100185 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200186}
187/* END_CASE */
188
189/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100190void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
Aorimn5f778012016-06-09 23:22:58 +0200191 char *hex_src_string, char *hex_dst_string,
192 int data_unit_len, int xts_result )
193{
194 unsigned char key_str[100] = { 0, };
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100195 unsigned char data_unit_str[100] = { 0, };
Aorimn5f778012016-06-09 23:22:58 +0200196 unsigned char src_str[100] = { 0, };
197 unsigned char dst_str[100] = { 0, };
198 unsigned char output[100] = { 0, };
Jaeden Amero9366feb2018-05-29 18:55:17 +0100199 mbedtls_aes_xts_context ctx;
Aorimn5f778012016-06-09 23:22:58 +0200200 int key_len, data_len;
201
Jaeden Amero9366feb2018-05-29 18:55:17 +0100202 mbedtls_aes_xts_init( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200203
204 key_len = unhexify( key_str, hex_key_string );
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100205 unhexify( data_unit_str, hex_data_unit_string );
Aorimn5f778012016-06-09 23:22:58 +0200206 data_len = unhexify( src_str, hex_src_string );
Jaeden Amero5162b932018-05-29 12:55:24 +0100207 TEST_ASSERT( data_len == data_unit_len / 8 );
Aorimn5f778012016-06-09 23:22:58 +0200208
Jaeden Amero9366feb2018-05-29 18:55:17 +0100209 mbedtls_aes_xts_setkey_dec( &ctx, key_str, key_len * 8 );
Aorimn5f778012016-06-09 23:22:58 +0200210
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100211 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, data_len, data_unit_str, src_str, output ) == xts_result );
Aorimn5f778012016-06-09 23:22:58 +0200212 if( xts_result == 0 )
213 {
214 hexify( dst_str, output, data_len );
215
216 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
217 }
218
219exit:
Jaeden Amero9366feb2018-05-29 18:55:17 +0100220 mbedtls_aes_xts_free( &ctx );
Aorimn5f778012016-06-09 23:22:58 +0200221}
222/* END_CASE */
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200225void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
226 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000227{
228 unsigned char key_str[100];
229 unsigned char iv_str[100];
230 unsigned char src_str[100];
231 unsigned char dst_str[100];
232 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000234 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000235 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000236
237 memset(key_str, 0x00, 100);
238 memset(iv_str, 0x00, 100);
239 memset(src_str, 0x00, 100);
240 memset(dst_str, 0x00, 100);
241 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000243
Paul Bakker33b43f12013-08-20 11:48:36 +0200244 key_len = unhexify( key_str, hex_key_string );
245 unhexify( iv_str, hex_iv_string );
246 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
249 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 +0000250 hexify( dst_str, output, 16 );
251
Paul Bakker33b43f12013-08-20 11:48:36 +0200252 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200253
Paul Bakkerbd51b262014-07-10 15:26:12 +0200254exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000256}
Paul Bakker33b43f12013-08-20 11:48:36 +0200257/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200260void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
261 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000262{
263 unsigned char key_str[100];
264 unsigned char iv_str[100];
265 unsigned char src_str[100];
266 unsigned char dst_str[100];
267 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000269 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000270 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000271
272 memset(key_str, 0x00, 100);
273 memset(iv_str, 0x00, 100);
274 memset(src_str, 0x00, 100);
275 memset(dst_str, 0x00, 100);
276 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000278
Paul Bakker33b43f12013-08-20 11:48:36 +0200279 key_len = unhexify( key_str, hex_key_string );
280 unhexify( iv_str, hex_iv_string );
281 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
284 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 +0000285 hexify( dst_str, output, 16 );
286
Paul Bakker33b43f12013-08-20 11:48:36 +0200287 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200288
Paul Bakkerbd51b262014-07-10 15:26:12 +0200289exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000291}
Paul Bakker33b43f12013-08-20 11:48:36 +0200292/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100295void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string,
296 char *hex_src_string, char *hex_dst_string )
297{
298 unsigned char key_str[100];
299 unsigned char iv_str[100];
300 unsigned char src_str[100];
301 unsigned char dst_str[100];
302 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100304 int key_len, src_len;
305
306 memset(key_str, 0x00, 100);
307 memset(iv_str, 0x00, 100);
308 memset(src_str, 0x00, 100);
309 memset(dst_str, 0x00, 100);
310 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100312
313 key_len = unhexify( key_str, hex_key_string );
314 unhexify( iv_str, hex_iv_string );
315 src_len = unhexify( src_str, hex_src_string );
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
318 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 +0100319 hexify( dst_str, output, src_len );
320
321 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200322
Paul Bakkerbd51b262014-07-10 15:26:12 +0200323exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100325}
326/* END_CASE */
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100329void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string,
330 char *hex_src_string, char *hex_dst_string )
331{
332 unsigned char key_str[100];
333 unsigned char iv_str[100];
334 unsigned char src_str[100];
335 unsigned char dst_str[100];
336 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100338 int key_len, src_len;
339
340 memset(key_str, 0x00, 100);
341 memset(iv_str, 0x00, 100);
342 memset(src_str, 0x00, 100);
343 memset(dst_str, 0x00, 100);
344 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100346
347 key_len = unhexify( key_str, hex_key_string );
348 unhexify( iv_str, hex_iv_string );
349 src_len = unhexify( src_str, hex_src_string );
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
352 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 +0100353 hexify( dst_str, output, src_len );
354
355 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200356
Paul Bakkerbd51b262014-07-10 15:26:12 +0200357exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100359}
360/* END_CASE */
361
Simon Butcher03018842018-04-22 22:57:58 +0100362/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
363void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
Simon Butcher00131442018-05-22 22:40:36 +0100364 char *hex_iv_string, char *hex_src_string,
365 char *hex_dst_string )
Simon Butcher03018842018-04-22 22:57:58 +0100366{
Simon Butchere416bf92018-06-02 18:28:32 +0100367 unsigned char key_str[32];
368 unsigned char iv_str[16];
369 unsigned char src_str[64];
370 unsigned char dst_str[64];
371 unsigned char output[32];
Simon Butcher03018842018-04-22 22:57:58 +0100372 mbedtls_aes_context ctx;
373 size_t iv_offset = 0;
374 int in_buffer_len;
375 unsigned char* src_str_next;
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100376 int key_len;
Simon Butcher03018842018-04-22 22:57:58 +0100377
Simon Butcherb7836e12018-06-02 18:36:49 +0100378 memset( key_str, 0x00, sizeof( key_str ) );
379 memset( iv_str, 0x00, sizeof( iv_str ) );
380 memset( src_str, 0x00, sizeof( src_str ) );
381 memset( dst_str, 0x00, sizeof( dst_str ) );
382 memset( output, 0x00, sizeof( output ) );
Simon Butcher03018842018-04-22 22:57:58 +0100383 mbedtls_aes_init( &ctx );
384
Simon Butchere416bf92018-06-02 18:28:32 +0100385 TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
386 TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
387 TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
388 TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
389
Simon Butcher03018842018-04-22 22:57:58 +0100390 key_len = unhexify( key_str, hex_key_string );
Simon Butcherdbe7fbf2018-04-29 14:51:35 +0100391 unhexify( iv_str, hex_iv_string );
Simon Butcher03018842018-04-22 22:57:58 +0100392 in_buffer_len = unhexify( src_str, hex_src_string );
393
Simon Butcherad4e4932018-04-29 00:43:47 +0100394 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100395 src_str_next = src_str;
396
397 while( in_buffer_len > 0 )
398 {
399 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
400 iv_str, src_str_next, output ) == 0 );
401
402 hexify( dst_str, output, fragment_size );
403 TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
Simon Butcher00131442018-05-22 22:40:36 +0100404 ( 2 * fragment_size ) ) == 0 );
Simon Butcher03018842018-04-22 22:57:58 +0100405
406 in_buffer_len -= fragment_size;
407 hex_dst_string += ( fragment_size * 2 );
408 src_str_next += fragment_size;
409
410 if( in_buffer_len < fragment_size )
411 fragment_size = in_buffer_len;
412 }
413
414exit:
415 mbedtls_aes_free( &ctx );
416}
417/* END_CASE */
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200420void aes_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000421{
Andres AG93012e82016-09-09 09:10:28 +0100422 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000423}
Paul Bakker33b43f12013-08-20 11:48:36 +0200424/* END_CASE */