blob: 6aeffd2e5ccfd9c85c51bbbe6076ccfda51e888d [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
Jaeden Amero184d0692018-04-28 17:26:25 +010010#if 0
11/* BEGIN_SUITE_HELPERS depends_on:MBEDTLS_CIPHER_MODE_XTS */
12static void aes_crypt_xts( char *hex_key_string
13 char *hex_data_unit_string,
14 char *hex_src_string, char *hex_dst_string,
15 int mode)
16{
17 /* XXX Code Review: is this possible? (having a shared helper function for
18 * a test, enabled only when that test is). Would line numbers and test
19 * results look appropriate still or would it be too much indirection? */
20}
21/* END_SUITE_HELPERS */
22#endif
23
Paul Bakker33b43f12013-08-20 11:48:36 +020024/* BEGIN_CASE */
25void aes_encrypt_ecb( char *hex_key_string, char *hex_src_string,
26 char *hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000027{
28 unsigned char key_str[100];
29 unsigned char src_str[100];
30 unsigned char dst_str[100];
31 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000033 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000034
35 memset(key_str, 0x00, 100);
36 memset(src_str, 0x00, 100);
37 memset(dst_str, 0x00, 100);
38 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000040
Paul Bakker33b43f12013-08-20 11:48:36 +020041 key_len = unhexify( key_str, hex_key_string );
42 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020045 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000048 hexify( dst_str, output, 16 );
Paul Bakker367dae42009-06-28 21:50:27 +000049
Paul Bakker33b43f12013-08-20 11:48:36 +020050 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000051 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020052
Paul Bakkerbd51b262014-07-10 15:26:12 +020053exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000055}
Paul Bakker33b43f12013-08-20 11:48:36 +020056/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000057
Paul Bakker33b43f12013-08-20 11:48:36 +020058/* BEGIN_CASE */
59void aes_decrypt_ecb( char *hex_key_string, char *hex_src_string,
60 char *hex_dst_string, int setkey_result )
Paul Bakker367dae42009-06-28 21:50:27 +000061{
62 unsigned char key_str[100];
63 unsigned char src_str[100];
64 unsigned char dst_str[100];
65 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_aes_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000067 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +000068
69 memset(key_str, 0x00, 100);
70 memset(src_str, 0x00, 100);
71 memset(dst_str, 0x00, 100);
72 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000074
Paul Bakker33b43f12013-08-20 11:48:36 +020075 key_len = unhexify( key_str, hex_key_string );
76 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020079 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000082 hexify( dst_str, output, 16 );
Paul Bakker367dae42009-06-28 21:50:27 +000083
Paul Bakker33b43f12013-08-20 11:48:36 +020084 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000085 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020086
Paul Bakkerbd51b262014-07-10 15:26:12 +020087exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +000089}
Paul Bakker33b43f12013-08-20 11:48:36 +020090/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +000091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020093void aes_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
94 char *hex_src_string, char *hex_dst_string,
95 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +000096{
97 unsigned char key_str[100];
98 unsigned char iv_str[100];
99 unsigned char src_str[100];
100 unsigned char dst_str[100];
101 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_aes_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000103 int key_len, data_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000104
105 memset(key_str, 0x00, 100);
106 memset(iv_str, 0x00, 100);
107 memset(src_str, 0x00, 100);
108 memset(dst_str, 0x00, 100);
109 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000111
Paul Bakker33b43f12013-08-20 11:48:36 +0200112 key_len = unhexify( key_str, hex_key_string );
113 unhexify( iv_str, hex_iv_string );
114 data_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
117 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 +0200118 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000119 {
120 hexify( dst_str, output, data_len );
Paul Bakker367dae42009-06-28 21:50:27 +0000121
Paul Bakker33b43f12013-08-20 11:48:36 +0200122 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000123 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200124
Paul Bakkerbd51b262014-07-10 15:26:12 +0200125exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000127}
Paul Bakker33b43f12013-08-20 11:48:36 +0200128/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200131void aes_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
132 char *hex_src_string, char *hex_dst_string,
133 int cbc_result )
Paul Bakker367dae42009-06-28 21:50:27 +0000134{
135 unsigned char key_str[100];
136 unsigned char iv_str[100];
137 unsigned char src_str[100];
138 unsigned char dst_str[100];
139 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_aes_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000141 int key_len, data_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000142
143 memset(key_str, 0x00, 100);
144 memset(iv_str, 0x00, 100);
145 memset(src_str, 0x00, 100);
146 memset(dst_str, 0x00, 100);
147 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000149
Paul Bakker33b43f12013-08-20 11:48:36 +0200150 key_len = unhexify( key_str, hex_key_string );
151 unhexify( iv_str, hex_iv_string );
152 data_len = unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 );
155 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 +0200156 if( cbc_result == 0)
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000157 {
158 hexify( dst_str, output, data_len );
Paul Bakker367dae42009-06-28 21:50:27 +0000159
Paul Bakker33b43f12013-08-20 11:48:36 +0200160 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000161 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200162
Paul Bakkerbd51b262014-07-10 15:26:12 +0200163exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200169void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
170 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000171{
172 unsigned char key_str[100];
173 unsigned char iv_str[100];
174 unsigned char src_str[100];
175 unsigned char dst_str[100];
176 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000178 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000179 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000180
181 memset(key_str, 0x00, 100);
182 memset(iv_str, 0x00, 100);
183 memset(src_str, 0x00, 100);
184 memset(dst_str, 0x00, 100);
185 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000187
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
193 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 +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 Bakker8cfd9d82014-06-18 11:16:11 +0200197
Paul Bakkerbd51b262014-07-10 15:26:12 +0200198exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000200}
Paul Bakker33b43f12013-08-20 11:48:36 +0200201/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200204void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
205 char *hex_src_string, char *hex_dst_string )
Paul Bakker367dae42009-06-28 21:50:27 +0000206{
207 unsigned char key_str[100];
208 unsigned char iv_str[100];
209 unsigned char src_str[100];
210 unsigned char dst_str[100];
211 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_aes_context ctx;
Paul Bakkercd43a0b2011-06-09 13:55:44 +0000213 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000214 int key_len;
Paul Bakker367dae42009-06-28 21:50:27 +0000215
216 memset(key_str, 0x00, 100);
217 memset(iv_str, 0x00, 100);
218 memset(src_str, 0x00, 100);
219 memset(dst_str, 0x00, 100);
220 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_aes_init( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000222
Paul Bakker33b43f12013-08-20 11:48:36 +0200223 key_len = unhexify( key_str, hex_key_string );
224 unhexify( iv_str, hex_iv_string );
225 unhexify( src_str, hex_src_string );
Paul Bakker367dae42009-06-28 21:50:27 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
228 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 +0000229 hexify( dst_str, output, 16 );
230
Paul Bakker33b43f12013-08-20 11:48:36 +0200231 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200232
Paul Bakkerbd51b262014-07-10 15:26:12 +0200233exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_aes_free( &ctx );
Paul Bakker367dae42009-06-28 21:50:27 +0000235}
Paul Bakker33b43f12013-08-20 11:48:36 +0200236/* END_CASE */
Paul Bakker367dae42009-06-28 21:50:27 +0000237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100239void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string,
240 char *hex_src_string, char *hex_dst_string )
241{
242 unsigned char key_str[100];
243 unsigned char iv_str[100];
244 unsigned char src_str[100];
245 unsigned char dst_str[100];
246 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100248 int key_len, src_len;
249
250 memset(key_str, 0x00, 100);
251 memset(iv_str, 0x00, 100);
252 memset(src_str, 0x00, 100);
253 memset(dst_str, 0x00, 100);
254 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100256
257 key_len = unhexify( key_str, hex_key_string );
258 unhexify( iv_str, hex_iv_string );
259 src_len = unhexify( src_str, hex_src_string );
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
262 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 +0100263 hexify( dst_str, output, src_len );
264
265 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200266
Paul Bakkerbd51b262014-07-10 15:26:12 +0200267exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100269}
270/* END_CASE */
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100273void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string,
274 char *hex_src_string, char *hex_dst_string )
275{
276 unsigned char key_str[100];
277 unsigned char iv_str[100];
278 unsigned char src_str[100];
279 unsigned char dst_str[100];
280 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_aes_context ctx;
Paul Bakker556efba2014-01-24 15:38:12 +0100282 int key_len, src_len;
283
284 memset(key_str, 0x00, 100);
285 memset(iv_str, 0x00, 100);
286 memset(src_str, 0x00, 100);
287 memset(dst_str, 0x00, 100);
288 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_aes_init( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100290
291 key_len = unhexify( key_str, hex_key_string );
292 unhexify( iv_str, hex_iv_string );
293 src_len = unhexify( src_str, hex_src_string );
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
296 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 +0100297 hexify( dst_str, output, src_len );
298
299 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200300
Paul Bakkerbd51b262014-07-10 15:26:12 +0200301exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_aes_free( &ctx );
Paul Bakker556efba2014-01-24 15:38:12 +0100303}
304/* END_CASE */
305
Jaeden Amero184d0692018-04-28 17:26:25 +0100306/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
307void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string,
308 char *hex_src_string, char *hex_dst_string )
309{
310 enum { AES_BLOCK_SIZE = 16 };
311 unsigned char *key;
312 unsigned char *data_unit;
313 unsigned char *src;
314 unsigned char *dst;
315 unsigned char *output;
316 mbedtls_aes_xts_context ctx;
317 size_t key_len, src_len, dst_len, data_unit_len;
318
319 mbedtls_aes_xts_init( &ctx );
320
321 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
322 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
323
324 key = unhexify_alloc( hex_key_string, &key_len );
325 TEST_ASSERT( key_len % 2 == 0 );
326
327 src = unhexify_alloc( hex_src_string, &src_len );
328 dst = unhexify_alloc( hex_dst_string, &dst_len );
329 TEST_ASSERT( src_len == dst_len );
330
331 output = zero_alloc(dst_len);
332
333 mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 );
334 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len, data_unit, src, output ) == 0 ); // XXX Code Review: must this be one line?
335
336 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
337
338exit:
339 mbedtls_aes_xts_free( &ctx );
340}
341/* END_CASE */
342
343/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
344void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
345 char *hex_dst_string, char *hex_src_string )
346{
347 /* XXX Code Review: I don't really like all this duplicated code between
348 * encrypt and decrypt. The only thing different is the mode and key
349 * initialization. */
350 enum { AES_BLOCK_SIZE = 16 };
351 unsigned char *key;
352 unsigned char *data_unit;
353 unsigned char *src;
354 unsigned char *dst;
355 unsigned char *output;
356 mbedtls_aes_xts_context ctx;
357 size_t key_len, src_len, dst_len, data_unit_len;
358
359 mbedtls_aes_xts_init( &ctx );
360
361 data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
362 TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
363
364 key = unhexify_alloc( hex_key_string, &key_len );
365 TEST_ASSERT( key_len % 2 == 0 );
366 src = unhexify_alloc( hex_src_string, &src_len );
367 dst = unhexify_alloc( hex_dst_string, &dst_len );
368 TEST_ASSERT( src_len == dst_len );
369
370 output = zero_alloc(dst_len);
371
372 mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 );
373 TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len, data_unit, src, output ) == 0 ); // XXX Code Review: must this be one line?
374
375 TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
376
377exit:
378 mbedtls_aes_xts_free( &ctx );
379}
380/* END_CASE */
381
382/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
383void aes_crypt_xts_size( int size, int retval )
384{
385 size_t length = size;
386 /* Note that this function will most likely crash on failure, as NULL
387 * parameters will be used. In the passing case, the length check in
388 * mbedtls_aes_crypt_xts() will prevent any accesses to parameters by
389 * exiting the function early. XXX <-- Hey look, Mr. Codereviewer. What do
390 * you think? Any ideas how to be safe and not have to actually allocate a
391 * 16 MiB buffer nor add NULL checks to mbedtls_aes_crypt_xts()? */
392 TEST_ASSERT( mbedtls_aes_crypt_xts( NULL, MBEDTLS_AES_ENCRYPT, length, NULL, NULL, NULL ) == retval );
393}
394/* END_CASE */
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200397void aes_selftest()
Paul Bakker3d360822009-07-05 11:29:38 +0000398{
Andres AG93012e82016-09-09 09:10:28 +0100399 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
Paul Bakker3d360822009-07-05 11:29:38 +0000400}
Paul Bakker33b43f12013-08-20 11:48:36 +0200401/* END_CASE */