• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/aes.h"
3
4/* Test AES with a copied context.
5 *
6 * enc and dec must be AES context objects. They don't need to
7 * be initialized, and are left freed.
8 */
9static int test_ctx_alignment(const data_t *key,
10                              mbedtls_aes_context *enc,
11                              mbedtls_aes_context *dec)
12{
13    unsigned char plaintext[16] = {
14        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
15        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
16    };
17    unsigned char ciphertext[16];
18    unsigned char output[16];
19
20    // Set key and encrypt with original context
21    mbedtls_aes_init(enc);
22    TEST_ASSERT(mbedtls_aes_setkey_enc(enc, key->x, key->len * 8) == 0);
23    TEST_ASSERT(mbedtls_aes_crypt_ecb(enc, MBEDTLS_AES_ENCRYPT,
24                                      plaintext, ciphertext) == 0);
25
26    // Set key for decryption with original context
27    mbedtls_aes_init(dec);
28    TEST_ASSERT(mbedtls_aes_setkey_dec(dec, key->x, key->len * 8) == 0);
29
30    // Wipe the original context to make sure nothing from it is used
31    memset(enc, 0, sizeof(*enc));
32    mbedtls_aes_free(enc);
33
34    // Decrypt
35    TEST_ASSERT(mbedtls_aes_crypt_ecb(dec, MBEDTLS_AES_DECRYPT,
36                                      ciphertext, output) == 0);
37    TEST_MEMORY_COMPARE(plaintext, 16, output, 16);
38
39    mbedtls_aes_free(dec);
40
41    return 1;
42
43exit:
44    /* Bug: we may be leaving something unfreed. This is harmless
45     * in our built-in implementations, but might cause a memory leak
46     * with alternative implementations. */
47    return 0;
48}
49
50/* END_HEADER */
51
52/* BEGIN_DEPENDENCIES
53 * depends_on:MBEDTLS_AES_C
54 * END_DEPENDENCIES
55 */
56
57/* BEGIN_CASE */
58void aes_encrypt_ecb(data_t *key_str, data_t *src_str,
59                     data_t *dst, int setkey_result)
60{
61    unsigned char output[100];
62    mbedtls_aes_context ctx;
63
64    memset(output, 0x00, 100);
65
66    mbedtls_aes_init(&ctx);
67
68    TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == setkey_result);
69    if (setkey_result == 0) {
70        TEST_ASSERT(mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT, src_str->x, output) == 0);
71
72        TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
73    }
74
75exit:
76    mbedtls_aes_free(&ctx);
77}
78/* END_CASE */
79
80/* BEGIN_CASE */
81void aes_decrypt_ecb(data_t *key_str, data_t *src_str,
82                     data_t *dst, int setkey_result)
83{
84    unsigned char output[100];
85    mbedtls_aes_context ctx;
86
87    memset(output, 0x00, 100);
88
89    mbedtls_aes_init(&ctx);
90
91    TEST_ASSERT(mbedtls_aes_setkey_dec(&ctx, key_str->x, key_str->len * 8) == setkey_result);
92    if (setkey_result == 0) {
93        TEST_ASSERT(mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_DECRYPT, src_str->x, output) == 0);
94
95        TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
96    }
97
98exit:
99    mbedtls_aes_free(&ctx);
100}
101/* END_CASE */
102
103/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
104void aes_encrypt_cbc(data_t *key_str, data_t *iv_str,
105                     data_t *src_str, data_t *dst,
106                     int cbc_result)
107{
108    unsigned char output[100];
109    mbedtls_aes_context ctx;
110
111    memset(output, 0x00, 100);
112
113    mbedtls_aes_init(&ctx);
114
115    TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
116    TEST_ASSERT(mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x,
117                                      src_str->x, output) == cbc_result);
118    if (cbc_result == 0) {
119
120        TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
121                                        src_str->len, dst->len) == 0);
122    }
123
124exit:
125    mbedtls_aes_free(&ctx);
126}
127/* END_CASE */
128
129/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
130void aes_decrypt_cbc(data_t *key_str, data_t *iv_str,
131                     data_t *src_str, data_t *dst,
132                     int cbc_result)
133{
134    unsigned char output[100];
135    mbedtls_aes_context ctx;
136
137    memset(output, 0x00, 100);
138    mbedtls_aes_init(&ctx);
139
140    TEST_ASSERT(mbedtls_aes_setkey_dec(&ctx, key_str->x, key_str->len * 8) == 0);
141    TEST_ASSERT(mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x,
142                                      src_str->x, output) == cbc_result);
143    if (cbc_result == 0) {
144
145        TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
146                                        src_str->len, dst->len) == 0);
147    }
148
149exit:
150    mbedtls_aes_free(&ctx);
151}
152/* END_CASE */
153
154/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
155void aes_encrypt_xts(char *hex_key_string, char *hex_data_unit_string,
156                     char *hex_src_string, char *hex_dst_string)
157{
158    enum { AES_BLOCK_SIZE = 16 };
159    unsigned char *data_unit = NULL;
160    unsigned char *key = NULL;
161    unsigned char *src = NULL;
162    unsigned char *dst = NULL;
163    unsigned char *output = NULL;
164    mbedtls_aes_xts_context ctx;
165    size_t key_len, src_len, dst_len, data_unit_len;
166
167    mbedtls_aes_xts_init(&ctx);
168
169    data_unit = mbedtls_test_unhexify_alloc(hex_data_unit_string,
170                                            &data_unit_len);
171    TEST_ASSERT(data_unit_len == AES_BLOCK_SIZE);
172
173    key = mbedtls_test_unhexify_alloc(hex_key_string, &key_len);
174    TEST_ASSERT(key_len % 2 == 0);
175
176    src = mbedtls_test_unhexify_alloc(hex_src_string, &src_len);
177    dst = mbedtls_test_unhexify_alloc(hex_dst_string, &dst_len);
178    TEST_ASSERT(src_len == dst_len);
179
180    output = mbedtls_test_zero_alloc(dst_len);
181
182    TEST_ASSERT(mbedtls_aes_xts_setkey_enc(&ctx, key, key_len * 8) == 0);
183    TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, src_len,
184                                      data_unit, src, output) == 0);
185
186    TEST_ASSERT(memcmp(output, dst, dst_len) == 0);
187
188exit:
189    mbedtls_aes_xts_free(&ctx);
190    mbedtls_free(data_unit);
191    mbedtls_free(key);
192    mbedtls_free(src);
193    mbedtls_free(dst);
194    mbedtls_free(output);
195}
196/* END_CASE */
197
198/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
199void aes_decrypt_xts(char *hex_key_string, char *hex_data_unit_string,
200                     char *hex_dst_string, char *hex_src_string)
201{
202    enum { AES_BLOCK_SIZE = 16 };
203    unsigned char *data_unit = NULL;
204    unsigned char *key = NULL;
205    unsigned char *src = NULL;
206    unsigned char *dst = NULL;
207    unsigned char *output = NULL;
208    mbedtls_aes_xts_context ctx;
209    size_t key_len, src_len, dst_len, data_unit_len;
210
211    mbedtls_aes_xts_init(&ctx);
212
213    data_unit = mbedtls_test_unhexify_alloc(hex_data_unit_string,
214                                            &data_unit_len);
215    TEST_ASSERT(data_unit_len == AES_BLOCK_SIZE);
216
217    key = mbedtls_test_unhexify_alloc(hex_key_string, &key_len);
218    TEST_ASSERT(key_len % 2 == 0);
219
220    src = mbedtls_test_unhexify_alloc(hex_src_string, &src_len);
221    dst = mbedtls_test_unhexify_alloc(hex_dst_string, &dst_len);
222    TEST_ASSERT(src_len == dst_len);
223
224    output = mbedtls_test_zero_alloc(dst_len);
225
226    TEST_ASSERT(mbedtls_aes_xts_setkey_dec(&ctx, key, key_len * 8) == 0);
227    TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_DECRYPT, src_len,
228                                      data_unit, src, output) == 0);
229
230    TEST_ASSERT(memcmp(output, dst, dst_len) == 0);
231
232exit:
233    mbedtls_aes_xts_free(&ctx);
234    mbedtls_free(data_unit);
235    mbedtls_free(key);
236    mbedtls_free(src);
237    mbedtls_free(dst);
238    mbedtls_free(output);
239}
240/* END_CASE */
241
242/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
243void aes_crypt_xts_size(int size, int retval)
244{
245    mbedtls_aes_xts_context ctx;
246    const unsigned char src[16] = { 0 };
247    unsigned char output[16];
248    unsigned char data_unit[16];
249    size_t length = size;
250
251    mbedtls_aes_xts_init(&ctx);
252    memset(data_unit, 0x00, sizeof(data_unit));
253
254
255    /* Valid pointers are passed for builds with MBEDTLS_CHECK_PARAMS, as
256     * otherwise we wouldn't get to the size check we're interested in. */
257    TEST_ASSERT(mbedtls_aes_crypt_xts(&ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src,
258                                      output) == retval);
259exit:
260    mbedtls_aes_xts_free(&ctx);
261}
262/* END_CASE */
263
264/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */
265void aes_crypt_xts_keysize(int size, int retval)
266{
267    mbedtls_aes_xts_context ctx;
268    const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
269    size_t key_len = size;
270
271    mbedtls_aes_xts_init(&ctx);
272
273    TEST_ASSERT(mbedtls_aes_xts_setkey_enc(&ctx, key, key_len * 8) == retval);
274    TEST_ASSERT(mbedtls_aes_xts_setkey_dec(&ctx, key, key_len * 8) == retval);
275exit:
276    mbedtls_aes_xts_free(&ctx);
277}
278/* END_CASE */
279
280
281/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
282void aes_encrypt_cfb128(data_t *key_str, data_t *iv_str,
283                        data_t *src_str, data_t *dst)
284{
285    unsigned char output[100];
286    mbedtls_aes_context ctx;
287    size_t iv_offset = 0;
288
289    memset(output, 0x00, 100);
290    mbedtls_aes_init(&ctx);
291
292
293    TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
294    TEST_ASSERT(mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x,
295                                         src_str->x, output) == 0);
296
297    TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
298
299exit:
300    mbedtls_aes_free(&ctx);
301}
302/* END_CASE */
303
304/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
305void aes_decrypt_cfb128(data_t *key_str, data_t *iv_str,
306                        data_t *src_str, data_t *dst)
307{
308    unsigned char output[100];
309    mbedtls_aes_context ctx;
310    size_t iv_offset = 0;
311
312    memset(output, 0x00, 100);
313    mbedtls_aes_init(&ctx);
314
315
316    TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
317    TEST_ASSERT(mbedtls_aes_crypt_cfb128(&ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x,
318                                         src_str->x, output) == 0);
319
320    TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
321
322exit:
323    mbedtls_aes_free(&ctx);
324}
325/* END_CASE */
326
327/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
328void aes_encrypt_cfb8(data_t *key_str, data_t *iv_str,
329                      data_t *src_str, data_t *dst)
330{
331    unsigned char output[100];
332    mbedtls_aes_context ctx;
333
334    memset(output, 0x00, 100);
335    mbedtls_aes_init(&ctx);
336
337
338    TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
339    TEST_ASSERT(mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x,
340                                       src_str->x, output) == 0);
341
342    TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
343                                    src_str->len, dst->len) == 0);
344
345exit:
346    mbedtls_aes_free(&ctx);
347}
348/* END_CASE */
349
350/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
351void aes_decrypt_cfb8(data_t *key_str, data_t *iv_str,
352                      data_t *src_str, data_t *dst)
353{
354    unsigned char output[100];
355    mbedtls_aes_context ctx;
356
357    memset(output, 0x00, 100);
358    mbedtls_aes_init(&ctx);
359
360
361    TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x, key_str->len * 8) == 0);
362    TEST_ASSERT(mbedtls_aes_crypt_cfb8(&ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x,
363                                       src_str->x, output) == 0);
364
365    TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
366                                    src_str->len, dst->len) == 0);
367
368exit:
369    mbedtls_aes_free(&ctx);
370}
371/* END_CASE */
372
373/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */
374void aes_encrypt_ofb(int fragment_size, data_t *key_str,
375                     data_t *iv_str, data_t *src_str,
376                     data_t *expected_output)
377{
378    unsigned char output[32];
379    mbedtls_aes_context ctx;
380    size_t iv_offset = 0;
381    int in_buffer_len;
382    unsigned char *src_str_next;
383
384    memset(output, 0x00, sizeof(output));
385    mbedtls_aes_init(&ctx);
386
387    TEST_ASSERT((size_t) fragment_size < sizeof(output));
388
389    TEST_ASSERT(mbedtls_aes_setkey_enc(&ctx, key_str->x,
390                                       key_str->len * 8) == 0);
391    in_buffer_len = src_str->len;
392    src_str_next = src_str->x;
393
394    while (in_buffer_len > 0) {
395        TEST_ASSERT(mbedtls_aes_crypt_ofb(&ctx, fragment_size, &iv_offset,
396                                          iv_str->x, src_str_next, output) == 0);
397
398        TEST_ASSERT(memcmp(output, expected_output->x, fragment_size) == 0);
399
400        in_buffer_len -= fragment_size;
401        expected_output->x += fragment_size;
402        src_str_next += fragment_size;
403
404        if (in_buffer_len < fragment_size) {
405            fragment_size = in_buffer_len;
406        }
407    }
408
409exit:
410    mbedtls_aes_free(&ctx);
411}
412/* END_CASE */
413
414/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
415void aes_check_params()
416{
417    mbedtls_aes_context aes_ctx;
418#if defined(MBEDTLS_CIPHER_MODE_XTS)
419    mbedtls_aes_xts_context xts_ctx;
420#endif
421    const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
422    const unsigned char in[16] = { 0 };
423    unsigned char out[16];
424    size_t size;
425    const int valid_mode = MBEDTLS_AES_ENCRYPT;
426    const int invalid_mode = 42;
427
428    TEST_INVALID_PARAM(mbedtls_aes_init(NULL));
429#if defined(MBEDTLS_CIPHER_MODE_XTS)
430    TEST_INVALID_PARAM(mbedtls_aes_xts_init(NULL));
431#endif
432
433    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
434                           mbedtls_aes_setkey_enc(NULL, key, 128));
435    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
436                           mbedtls_aes_setkey_enc(&aes_ctx, NULL, 128));
437
438    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
439                           mbedtls_aes_setkey_dec(NULL, key, 128));
440    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
441                           mbedtls_aes_setkey_dec(&aes_ctx, NULL, 128));
442
443#if defined(MBEDTLS_CIPHER_MODE_XTS)
444    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
445                           mbedtls_aes_xts_setkey_enc(NULL, key, 128));
446    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
447                           mbedtls_aes_xts_setkey_enc(&xts_ctx, NULL, 128));
448
449    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
450                           mbedtls_aes_xts_setkey_dec(NULL, key, 128));
451    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
452                           mbedtls_aes_xts_setkey_dec(&xts_ctx, NULL, 128));
453#endif
454
455
456    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
457                           mbedtls_aes_crypt_ecb(NULL,
458                                                 valid_mode, in, out));
459    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
460                           mbedtls_aes_crypt_ecb(&aes_ctx,
461                                                 invalid_mode, in, out));
462    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
463                           mbedtls_aes_crypt_ecb(&aes_ctx,
464                                                 valid_mode, NULL, out));
465    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
466                           mbedtls_aes_crypt_ecb(&aes_ctx,
467                                                 valid_mode, in, NULL));
468
469#if defined(MBEDTLS_CIPHER_MODE_CBC)
470    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
471                           mbedtls_aes_crypt_cbc(NULL,
472                                                 valid_mode, 16,
473                                                 out, in, out));
474    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
475                           mbedtls_aes_crypt_cbc(&aes_ctx,
476                                                 invalid_mode, 16,
477                                                 out, in, out));
478    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
479                           mbedtls_aes_crypt_cbc(&aes_ctx,
480                                                 valid_mode, 16,
481                                                 NULL, in, out));
482    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
483                           mbedtls_aes_crypt_cbc(&aes_ctx,
484                                                 valid_mode, 16,
485                                                 out, NULL, out));
486    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
487                           mbedtls_aes_crypt_cbc(&aes_ctx,
488                                                 valid_mode, 16,
489                                                 out, in, NULL));
490#endif /* MBEDTLS_CIPHER_MODE_CBC */
491
492#if defined(MBEDTLS_CIPHER_MODE_XTS)
493    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
494                           mbedtls_aes_crypt_xts(NULL,
495                                                 valid_mode, 16,
496                                                 in, in, out));
497    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
498                           mbedtls_aes_crypt_xts(&xts_ctx,
499                                                 invalid_mode, 16,
500                                                 in, in, out));
501    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
502                           mbedtls_aes_crypt_xts(&xts_ctx,
503                                                 valid_mode, 16,
504                                                 NULL, in, out));
505    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
506                           mbedtls_aes_crypt_xts(&xts_ctx,
507                                                 valid_mode, 16,
508                                                 in, NULL, out));
509    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
510                           mbedtls_aes_crypt_xts(&xts_ctx,
511                                                 valid_mode, 16,
512                                                 in, in, NULL));
513#endif /* MBEDTLS_CIPHER_MODE_XTS */
514
515#if defined(MBEDTLS_CIPHER_MODE_CFB)
516    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
517                           mbedtls_aes_crypt_cfb128(NULL,
518                                                    valid_mode, 16,
519                                                    &size, out, in, out));
520    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
521                           mbedtls_aes_crypt_cfb128(&aes_ctx,
522                                                    invalid_mode, 16,
523                                                    &size, out, in, out));
524    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
525                           mbedtls_aes_crypt_cfb128(&aes_ctx,
526                                                    valid_mode, 16,
527                                                    NULL, out, in, out));
528    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
529                           mbedtls_aes_crypt_cfb128(&aes_ctx,
530                                                    valid_mode, 16,
531                                                    &size, NULL, in, out));
532    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
533                           mbedtls_aes_crypt_cfb128(&aes_ctx,
534                                                    valid_mode, 16,
535                                                    &size, out, NULL, out));
536    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
537                           mbedtls_aes_crypt_cfb128(&aes_ctx,
538                                                    valid_mode, 16,
539                                                    &size, out, in, NULL));
540
541
542    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
543                           mbedtls_aes_crypt_cfb8(NULL,
544                                                  valid_mode, 16,
545                                                  out, in, out));
546    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
547                           mbedtls_aes_crypt_cfb8(&aes_ctx,
548                                                  invalid_mode, 16,
549                                                  out, in, out));
550    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
551                           mbedtls_aes_crypt_cfb8(&aes_ctx,
552                                                  valid_mode, 16,
553                                                  NULL, in, out));
554    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
555                           mbedtls_aes_crypt_cfb8(&aes_ctx,
556                                                  valid_mode, 16,
557                                                  out, NULL, out));
558    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
559                           mbedtls_aes_crypt_cfb8(&aes_ctx,
560                                                  valid_mode, 16,
561                                                  out, in, NULL));
562#endif /* MBEDTLS_CIPHER_MODE_CFB */
563
564#if defined(MBEDTLS_CIPHER_MODE_OFB)
565    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
566                           mbedtls_aes_crypt_ofb(NULL, 16,
567                                                 &size, out, in, out));
568    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
569                           mbedtls_aes_crypt_ofb(&aes_ctx, 16,
570                                                 NULL, out, in, out));
571    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
572                           mbedtls_aes_crypt_ofb(&aes_ctx, 16,
573                                                 &size, NULL, in, out));
574    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
575                           mbedtls_aes_crypt_ofb(&aes_ctx, 16,
576                                                 &size, out, NULL, out));
577    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
578                           mbedtls_aes_crypt_ofb(&aes_ctx, 16,
579                                                 &size, out, in, NULL));
580#endif /* MBEDTLS_CIPHER_MODE_OFB */
581
582#if defined(MBEDTLS_CIPHER_MODE_CTR)
583    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
584                           mbedtls_aes_crypt_ctr(NULL, 16, &size, out,
585                                                 out, in, out));
586    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
587                           mbedtls_aes_crypt_ctr(&aes_ctx, 16, NULL, out,
588                                                 out, in, out));
589    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
590                           mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, NULL,
591                                                 out, in, out));
592    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
593                           mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out,
594                                                 NULL, in, out));
595    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
596                           mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out,
597                                                 out, NULL, out));
598    TEST_INVALID_PARAM_RET(MBEDTLS_ERR_AES_BAD_INPUT_DATA,
599                           mbedtls_aes_crypt_ctr(&aes_ctx, 16, &size, out,
600                                                 out, in, NULL));
601#endif /* MBEDTLS_CIPHER_MODE_CTR */
602}
603/* END_CASE */
604
605/* BEGIN_CASE */
606void aes_misc_params()
607{
608#if defined(MBEDTLS_CIPHER_MODE_CBC) || \
609    defined(MBEDTLS_CIPHER_MODE_XTS) || \
610    defined(MBEDTLS_CIPHER_MODE_CFB) || \
611    defined(MBEDTLS_CIPHER_MODE_OFB)
612    const unsigned char in[16] = { 0 };
613    unsigned char out[16];
614#endif
615#if defined(MBEDTLS_CIPHER_MODE_CBC) || \
616    defined(MBEDTLS_CIPHER_MODE_CFB) || \
617    defined(MBEDTLS_CIPHER_MODE_OFB)
618    mbedtls_aes_context aes_ctx;
619#endif
620#if defined(MBEDTLS_CIPHER_MODE_XTS)
621    mbedtls_aes_xts_context xts_ctx;
622#endif
623#if defined(MBEDTLS_CIPHER_MODE_CFB) || \
624    defined(MBEDTLS_CIPHER_MODE_OFB)
625    size_t size;
626#endif
627
628    /* These calls accept NULL */
629    TEST_VALID_PARAM(mbedtls_aes_free(NULL));
630#if defined(MBEDTLS_CIPHER_MODE_XTS)
631    TEST_VALID_PARAM(mbedtls_aes_xts_free(NULL));
632#endif
633
634#if defined(MBEDTLS_CIPHER_MODE_CBC)
635    TEST_ASSERT(mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT,
636                                      15,
637                                      out, in, out)
638                == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
639    TEST_ASSERT(mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT,
640                                      17,
641                                      out, in, out)
642                == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
643#endif
644
645#if defined(MBEDTLS_CIPHER_MODE_XTS)
646    TEST_ASSERT(mbedtls_aes_crypt_xts(&xts_ctx, MBEDTLS_AES_ENCRYPT,
647                                      15,
648                                      in, in, out)
649                == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
650    TEST_ASSERT(mbedtls_aes_crypt_xts(&xts_ctx, MBEDTLS_AES_ENCRYPT,
651                                      (1 << 24) + 1,
652                                      in, in, out)
653                == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH);
654#endif
655
656#if defined(MBEDTLS_CIPHER_MODE_CFB)
657    size = 16;
658    TEST_ASSERT(mbedtls_aes_crypt_cfb128(&aes_ctx, MBEDTLS_AES_ENCRYPT, 16,
659                                         &size, out, in, out)
660                == MBEDTLS_ERR_AES_BAD_INPUT_DATA);
661#endif
662
663#if defined(MBEDTLS_CIPHER_MODE_OFB)
664    size = 16;
665    TEST_ASSERT(mbedtls_aes_crypt_ofb(&aes_ctx, 16, &size, out, in, out)
666                == MBEDTLS_ERR_AES_BAD_INPUT_DATA);
667#endif
668}
669/* END_CASE */
670
671/* BEGIN_CASE */
672void aes_ecb_context_alignment(data_t *key)
673{
674    /* We test alignment multiple times, with different alignments
675     * of the context and of the plaintext/ciphertext. */
676
677    struct align0 {
678        mbedtls_aes_context ctx;
679    };
680    struct align0 *enc0 = NULL;
681    struct align0 *dec0 = NULL;
682
683    struct align1 {
684        char bump;
685        mbedtls_aes_context ctx;
686    };
687    struct align1 *enc1 = NULL;
688    struct align1 *dec1 = NULL;
689
690    /* All peak alignment */
691    TEST_CALLOC(enc0, 1);
692    TEST_CALLOC(dec0, 1);
693    if (!test_ctx_alignment(key, &enc0->ctx, &dec0->ctx)) {
694        goto exit;
695    }
696    mbedtls_free(enc0);
697    enc0 = NULL;
698    mbedtls_free(dec0);
699    dec0 = NULL;
700
701    /* Enc aligned, dec not */
702    TEST_CALLOC(enc0, 1);
703    TEST_CALLOC(dec1, 1);
704    if (!test_ctx_alignment(key, &enc0->ctx, &dec1->ctx)) {
705        goto exit;
706    }
707    mbedtls_free(enc0);
708    enc0 = NULL;
709    mbedtls_free(dec1);
710    dec1 = NULL;
711
712    /* Dec aligned, enc not */
713    TEST_CALLOC(enc1, 1);
714    TEST_CALLOC(dec0, 1);
715    if (!test_ctx_alignment(key, &enc1->ctx, &dec0->ctx)) {
716        goto exit;
717    }
718    mbedtls_free(enc1);
719    enc1 = NULL;
720    mbedtls_free(dec0);
721    dec0 = NULL;
722
723    /* Both shifted */
724    TEST_CALLOC(enc1, 1);
725    TEST_CALLOC(dec1, 1);
726    if (!test_ctx_alignment(key, &enc1->ctx, &dec1->ctx)) {
727        goto exit;
728    }
729    mbedtls_free(enc1);
730    enc1 = NULL;
731    mbedtls_free(dec1);
732    dec1 = NULL;
733
734exit:
735    mbedtls_free(enc0);
736    mbedtls_free(dec0);
737    mbedtls_free(enc1);
738    mbedtls_free(dec1);
739}
740/* END_CASE */
741
742/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
743void aes_selftest()
744{
745    TEST_ASSERT(mbedtls_aes_self_test(1) == 0);
746}
747/* END_CASE */
748