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