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