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