1/* BEGIN_HEADER */
2#include "mbedtls/aes.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_AES_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void aes_encrypt_ecb( data_t * key_str, data_t * src_str,
12                      data_t * hex_dst_string, int setkey_result )
13{
14    unsigned char output[100];
15    mbedtls_aes_context ctx;
16
17    memset(output, 0x00, 100);
18
19    mbedtls_aes_init( &ctx );
20
21    TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
22    if( setkey_result == 0 )
23    {
24        TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str->x, output ) == 0 );
25
26        TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
27    }
28
29exit:
30    mbedtls_aes_free( &ctx );
31}
32/* END_CASE */
33
34/* BEGIN_CASE */
35void aes_decrypt_ecb( data_t * key_str, data_t * src_str,
36                      data_t * hex_dst_string, int setkey_result )
37{
38    unsigned char output[100];
39    mbedtls_aes_context ctx;
40
41    memset(output, 0x00, 100);
42
43    mbedtls_aes_init( &ctx );
44
45    TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
46    if( setkey_result == 0 )
47    {
48        TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str->x, output ) == 0 );
49
50        TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
51    }
52
53exit:
54    mbedtls_aes_free( &ctx );
55}
56/* END_CASE */
57
58/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
59void aes_encrypt_cbc( data_t * key_str, data_t * iv_str,
60                      data_t * src_str, data_t * hex_dst_string,
61                      int cbc_result )
62{
63    unsigned char output[100];
64    mbedtls_aes_context ctx;
65
66    memset(output, 0x00, 100);
67
68    mbedtls_aes_init( &ctx );
69
70    mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
71    TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
72    if( cbc_result == 0 )
73    {
74
75        TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
76    }
77
78exit:
79    mbedtls_aes_free( &ctx );
80}
81/* END_CASE */
82
83/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
84void aes_decrypt_cbc( data_t * key_str, data_t * iv_str,
85                      data_t * src_str, data_t * hex_dst_string,
86                      int cbc_result )
87{
88    unsigned char output[100];
89    mbedtls_aes_context ctx;
90
91    memset(output, 0x00, 100);
92    mbedtls_aes_init( &ctx );
93
94    mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
95    TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
96    if( cbc_result == 0)
97    {
98
99        TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
100    }
101
102exit:
103    mbedtls_aes_free( &ctx );
104}
105/* END_CASE */
106
107/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
108void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string,
109                      char *hex_src_string, char *hex_dst_string )
110{
111    enum { AES_BLOCK_SIZE = 16 };
112    unsigned char *data_unit = NULL;
113    unsigned char *key = NULL;
114    unsigned char *src = NULL;
115    unsigned char *dst = NULL;
116    unsigned char *output = NULL;
117    mbedtls_aes_xts_context ctx;
118    size_t key_len, src_len, dst_len, data_unit_len;
119
120    mbedtls_aes_xts_init( &ctx );
121
122    data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
123    TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
124
125    key = unhexify_alloc( hex_key_string, &key_len );
126    TEST_ASSERT( key_len % 2 == 0 );
127
128    src = unhexify_alloc( hex_src_string, &src_len );
129    dst = unhexify_alloc( hex_dst_string, &dst_len );
130    TEST_ASSERT( src_len == dst_len );
131
132    output = zero_alloc( dst_len );
133
134    TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 );
135    TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len,
136                                        data_unit, src, output ) == 0 );
137
138    TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
139
140exit:
141    mbedtls_aes_xts_free( &ctx );
142    mbedtls_free( data_unit );
143    mbedtls_free( key );
144    mbedtls_free( src );
145    mbedtls_free( dst );
146    mbedtls_free( output );
147}
148/* END_CASE */
149
150/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
151void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string,
152                      char *hex_dst_string, char *hex_src_string )
153{
154    enum { AES_BLOCK_SIZE = 16 };
155    unsigned char *data_unit = NULL;
156    unsigned char *key = NULL;
157    unsigned char *src = NULL;
158    unsigned char *dst = NULL;
159    unsigned char *output = NULL;
160    mbedtls_aes_xts_context ctx;
161    size_t key_len, src_len, dst_len, data_unit_len;
162
163    mbedtls_aes_xts_init( &ctx );
164
165    data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len );
166    TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE );
167
168    key = unhexify_alloc( hex_key_string, &key_len );
169    TEST_ASSERT( key_len % 2 == 0 );
170
171    src = unhexify_alloc( hex_src_string, &src_len );
172    dst = unhexify_alloc( hex_dst_string, &dst_len );
173    TEST_ASSERT( src_len == dst_len );
174
175    output = zero_alloc( dst_len );
176
177    TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 );
178    TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len,
179                                        data_unit, src, output ) == 0 );
180
181    TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 );
182
183exit:
184    mbedtls_aes_xts_free( &ctx );
185    mbedtls_free( data_unit );
186    mbedtls_free( key );
187    mbedtls_free( src );
188    mbedtls_free( dst );
189    mbedtls_free( output );
190}
191/* END_CASE */
192
193/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
194void aes_crypt_xts_size( int size, int retval )
195{
196    mbedtls_aes_xts_context ctx;
197    const unsigned char src[16] = { 0 };
198    unsigned char output[16];
199    unsigned char data_unit[16];
200    size_t length = size;
201
202    mbedtls_aes_xts_init( &ctx );
203    memset( data_unit, 0x00, sizeof( data_unit ) );
204
205
206    /* Valid pointers are passed for builds with MBEDTLS_CHECK_PARAMS, as
207     * otherwise we wouldn't get to the size check we're interested in. */
208    TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval );
209}
210/* END_CASE */
211
212/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
213void aes_crypt_xts_keysize( int size, int retval )
214{
215    mbedtls_aes_xts_context ctx;
216    const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
217    size_t key_len = size;
218
219    mbedtls_aes_xts_init( &ctx );
220
221    TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval );
222    TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval );
223exit:
224    mbedtls_aes_xts_free( &ctx );
225}
226/* END_CASE */
227
228
229/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
230void aes_encrypt_cfb128( data_t * key_str, data_t * iv_str,
231                         data_t * src_str, data_t * hex_dst_string )
232{
233    unsigned char output[100];
234    mbedtls_aes_context ctx;
235    size_t iv_offset = 0;
236
237    memset(output, 0x00, 100);
238    mbedtls_aes_init( &ctx );
239
240
241    mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
242    TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
243
244    TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
245
246exit:
247    mbedtls_aes_free( &ctx );
248}
249/* END_CASE */
250
251/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
252void aes_decrypt_cfb128( data_t * key_str, data_t * iv_str,
253                         data_t * src_str, data_t * hex_dst_string )
254{
255    unsigned char output[100];
256    mbedtls_aes_context ctx;
257    size_t iv_offset = 0;
258
259    memset(output, 0x00, 100);
260    mbedtls_aes_init( &ctx );
261
262
263    mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
264    TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
265
266    TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 );
267
268exit:
269    mbedtls_aes_free( &ctx );
270}
271/* END_CASE */
272
273/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
274void aes_encrypt_cfb8( data_t * key_str, data_t * iv_str,
275                       data_t * src_str, data_t * hex_dst_string )
276{
277    unsigned char output[100];
278    mbedtls_aes_context ctx;
279
280    memset(output, 0x00, 100);
281    mbedtls_aes_init( &ctx );
282
283
284    mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
285    TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 );
286
287    TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
288
289exit:
290    mbedtls_aes_free( &ctx );
291}
292/* END_CASE */
293
294/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
295void aes_decrypt_cfb8( data_t * key_str, data_t * iv_str,
296                       data_t * src_str, data_t * hex_dst_string )
297{
298    unsigned char output[100];
299    mbedtls_aes_context ctx;
300
301    memset(output, 0x00, 100);
302    mbedtls_aes_init( &ctx );
303
304
305    mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
306    TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 );
307
308    TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 );
309
310exit:
311    mbedtls_aes_free( &ctx );
312}
313/* END_CASE */
314
315/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
316void aes_encrypt_ofb( int fragment_size, char *hex_key_string,
317                      char *hex_iv_string, char *hex_src_string,
318                      char *hex_dst_string )
319{
320    unsigned char key_str[32];
321    unsigned char iv_str[16];
322    unsigned char src_str[64];
323    unsigned char dst_str[64];
324    unsigned char output[32];
325    mbedtls_aes_context ctx;
326    size_t iv_offset = 0;
327    int in_buffer_len;
328    unsigned char* src_str_next;
329    int key_len;
330
331    memset( key_str, 0x00, sizeof( key_str ) );
332    memset( iv_str, 0x00, sizeof( iv_str ) );
333    memset( src_str, 0x00, sizeof( src_str ) );
334    memset( dst_str, 0x00, sizeof( dst_str ) );
335    memset( output, 0x00, sizeof( output ) );
336    mbedtls_aes_init( &ctx );
337
338    TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) );
339    TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) );
340    TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) );
341    TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) );
342
343    key_len = unhexify( key_str, hex_key_string );
344    unhexify( iv_str, hex_iv_string );
345    in_buffer_len = unhexify( src_str, hex_src_string );
346
347    TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 );
348    src_str_next = src_str;
349
350    while( in_buffer_len > 0 )
351    {
352        TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset,
353                                            iv_str, src_str_next, output ) == 0 );
354
355        hexify( dst_str, output, fragment_size );
356        TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string,
357                              ( 2 * fragment_size ) ) == 0 );
358
359        in_buffer_len -= fragment_size;
360        hex_dst_string += ( fragment_size * 2 );
361        src_str_next += fragment_size;
362
363        if( in_buffer_len < fragment_size )
364            fragment_size = in_buffer_len;
365    }
366
367exit:
368    mbedtls_aes_free( &ctx );
369}
370/* END_CASE */
371
372/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
373void aes_check_params( )
374{
375    mbedtls_aes_context aes_ctx;
376#if defined(MBEDTLS_CIPHER_MODE_XTS)
377    mbedtls_aes_xts_context xts_ctx;
378#endif
379    const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
380    const unsigned char in[16] = { 0 };
381    unsigned char out[16];
382    size_t size;
383    const int valid_mode = MBEDTLS_AES_ENCRYPT;
384    const int invalid_mode = 42;
385
386    TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) );
387#if defined(MBEDTLS_CIPHER_MODE_XTS)
388    TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) );
389#endif
390
391    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
392                            mbedtls_aes_setkey_enc( NULL, key, 128 ) );
393    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
394                            mbedtls_aes_setkey_enc( &aes_ctx, NULL, 128 ) );
395
396    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
397                            mbedtls_aes_setkey_dec( NULL, key, 128 ) );
398    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
399                            mbedtls_aes_setkey_dec( &aes_ctx, NULL, 128 ) );
400
401#if defined(MBEDTLS_CIPHER_MODE_XTS)
402    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
403                            mbedtls_aes_xts_setkey_enc( NULL, key, 128 ) );
404    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
405                            mbedtls_aes_xts_setkey_enc( &xts_ctx, NULL, 128 ) );
406
407    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
408                            mbedtls_aes_xts_setkey_dec( NULL, key, 128 ) );
409    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
410                            mbedtls_aes_xts_setkey_dec( &xts_ctx, NULL, 128 ) );
411#endif
412
413
414    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
415                            mbedtls_aes_crypt_ecb( NULL,
416                                                   valid_mode, in, out ) );
417    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
418                            mbedtls_aes_crypt_ecb( &aes_ctx,
419                                                   invalid_mode, in, out ) );
420    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
421                            mbedtls_aes_crypt_ecb( &aes_ctx,
422                                                   valid_mode, NULL, out ) );
423    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
424                            mbedtls_aes_crypt_ecb( &aes_ctx,
425                                                   valid_mode, in, NULL ) );
426
427#if defined(MBEDTLS_CIPHER_MODE_CBC)
428    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
429                            mbedtls_aes_crypt_cbc( NULL,
430                                                   valid_mode, 16,
431                                                   out, in, out ) );
432    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
433                            mbedtls_aes_crypt_cbc( &aes_ctx,
434                                                   invalid_mode, 16,
435                                                   out, in, out ) );
436    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
437                            mbedtls_aes_crypt_cbc( &aes_ctx,
438                                                   valid_mode, 16,
439                                                   NULL, in, out ) );
440    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
441                            mbedtls_aes_crypt_cbc( &aes_ctx,
442                                                   valid_mode, 16,
443                                                   out, NULL, out ) );
444    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
445                            mbedtls_aes_crypt_cbc( &aes_ctx,
446                                                   valid_mode, 16,
447                                                   out, in, NULL ) );
448#endif /* MBEDTLS_CIPHER_MODE_CBC */
449
450#if defined(MBEDTLS_CIPHER_MODE_XTS)
451    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
452                            mbedtls_aes_crypt_xts( NULL,
453                                                   valid_mode, 16,
454                                                   in, in, out ) );
455    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
456                            mbedtls_aes_crypt_xts( &xts_ctx,
457                                                   invalid_mode, 16,
458                                                   in, in, out ) );
459    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
460                            mbedtls_aes_crypt_xts( &xts_ctx,
461                                                   valid_mode, 16,
462                                                   NULL, in, out ) );
463    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
464                            mbedtls_aes_crypt_xts( &xts_ctx,
465                                                   valid_mode, 16,
466                                                   in, NULL, out ) );
467    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
468                            mbedtls_aes_crypt_xts( &xts_ctx,
469                                                   valid_mode, 16,
470                                                   in, in, NULL ) );
471#endif /* MBEDTLS_CIPHER_MODE_XTS */
472
473#if defined(MBEDTLS_CIPHER_MODE_CFB)
474    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
475                            mbedtls_aes_crypt_cfb128( NULL,
476                                                      valid_mode, 16,
477                                                      &size, out, in, out ) );
478    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
479                            mbedtls_aes_crypt_cfb128( &aes_ctx,
480                                                      invalid_mode, 16,
481                                                      &size, out, in, out ) );
482    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
483                            mbedtls_aes_crypt_cfb128( &aes_ctx,
484                                                      valid_mode, 16,
485                                                      NULL, out, in, out ) );
486    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
487                            mbedtls_aes_crypt_cfb128( &aes_ctx,
488                                                      valid_mode, 16,
489                                                      &size, NULL, in, out ) );
490    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
491                            mbedtls_aes_crypt_cfb128( &aes_ctx,
492                                                      valid_mode, 16,
493                                                      &size, out, NULL, out ) );
494    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
495                            mbedtls_aes_crypt_cfb128( &aes_ctx,
496                                                      valid_mode, 16,
497                                                      &size, out, in, NULL ) );
498
499
500    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
501                            mbedtls_aes_crypt_cfb8( NULL,
502                                                    valid_mode, 16,
503                                                    out, in, out ) );
504    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
505                            mbedtls_aes_crypt_cfb8( &aes_ctx,
506                                                    invalid_mode, 16,
507                                                    out, in, out ) );
508    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
509                            mbedtls_aes_crypt_cfb8( &aes_ctx,
510                                                    valid_mode, 16,
511                                                    NULL, in, out ) );
512    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
513                            mbedtls_aes_crypt_cfb8( &aes_ctx,
514                                                    valid_mode, 16,
515                                                    out, NULL, out ) );
516    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
517                            mbedtls_aes_crypt_cfb8( &aes_ctx,
518                                                    valid_mode, 16,
519                                                    out, in, NULL ) );
520#endif /* MBEDTLS_CIPHER_MODE_CFB */
521
522#if defined(MBEDTLS_CIPHER_MODE_OFB)
523    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
524                            mbedtls_aes_crypt_ofb( NULL, 16,
525                                                   &size, out, in, out ) );
526    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
527                            mbedtls_aes_crypt_ofb( &aes_ctx, 16,
528                                                   NULL, out, in, out ) );
529    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
530                            mbedtls_aes_crypt_ofb( &aes_ctx, 16,
531                                                   &size, NULL, in, out ) );
532    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
533                            mbedtls_aes_crypt_ofb( &aes_ctx, 16,
534                                                   &size, out, NULL, out ) );
535    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
536                            mbedtls_aes_crypt_ofb( &aes_ctx, 16,
537                                                   &size, out, in, NULL ) );
538#endif /* MBEDTLS_CIPHER_MODE_OFB */
539
540#if defined(MBEDTLS_CIPHER_MODE_CTR)
541    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
542                            mbedtls_aes_crypt_ctr( NULL, 16, &size, out,
543                                                   out, in, out ) );
544    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
545                            mbedtls_aes_crypt_ctr( &aes_ctx, 16, NULL, out,
546                                                   out, in, out ) );
547    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
548                            mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, NULL,
549                                                   out, in, out ) );
550    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
551                            mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
552                                                   NULL, in, out ) );
553    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
554                            mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
555                                                   out, NULL, out ) );
556    TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA,
557                            mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out,
558                                                   out, in, NULL ) );
559#endif /* MBEDTLS_CIPHER_MODE_CTR */
560}
561/* END_CASE */
562
563/* BEGIN_CASE */
564void aes_misc_params( )
565{
566#if defined(MBEDTLS_CIPHER_MODE_CBC) || \
567    defined(MBEDTLS_CIPHER_MODE_XTS) || \
568    defined(MBEDTLS_CIPHER_MODE_CFB) || \
569    defined(MBEDTLS_CIPHER_MODE_OFB)
570    mbedtls_aes_context aes_ctx;
571    const unsigned char in[16] = { 0 };
572    unsigned char out[16];
573#endif
574#if defined(MBEDTLS_CIPHER_MODE_XTS)
575    mbedtls_aes_xts_context xts_ctx;
576#endif
577#if defined(MBEDTLS_CIPHER_MODE_CFB) || \
578    defined(MBEDTLS_CIPHER_MODE_OFB)
579    size_t size;
580#endif
581
582    /* These calls accept NULL */
583    TEST_VALID_PARAM( mbedtls_aes_free( NULL ) );
584#if defined(MBEDTLS_CIPHER_MODE_XTS)
585    TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) );
586#endif
587
588#if defined(MBEDTLS_CIPHER_MODE_CBC)
589    TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
590                                        15,
591                                        out, in, out )
592                 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
593    TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT,
594                                        17,
595                                        out, in, out )
596                 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
597#endif
598
599#if defined(MBEDTLS_CIPHER_MODE_XTS)
600    TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
601                                        15,
602                                        in, in, out )
603                 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
604    TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT,
605                                        (1 << 24) + 1,
606                                        in, in, out )
607                 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
608#endif
609
610#if defined(MBEDTLS_CIPHER_MODE_CFB)
611    size = 16;
612    TEST_ASSERT( mbedtls_aes_crypt_cfb128( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16,
613                                           &size, out, in, out )
614                 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
615#endif
616
617#if defined(MBEDTLS_CIPHER_MODE_OFB)
618    size = 16;
619    TEST_ASSERT( mbedtls_aes_crypt_ofb( &aes_ctx, 16, &size, out, in, out )
620                 == MBEDTLS_ERR_AES_BAD_INPUT_DATA );
621#endif
622}
623/* END_CASE */
624
625/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
626void aes_selftest(  )
627{
628    TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
629}
630/* END_CASE */
631