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 ); 211exit: 212 mbedtls_aes_xts_free( &ctx ); 213} 214/* END_CASE */ 215 216/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ 217void aes_crypt_xts_keysize( int size, int retval ) 218{ 219 mbedtls_aes_xts_context ctx; 220 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; 221 size_t key_len = size; 222 223 mbedtls_aes_xts_init( &ctx ); 224 225 TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval ); 226 TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval ); 227exit: 228 mbedtls_aes_xts_free( &ctx ); 229} 230/* END_CASE */ 231 232 233/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 234void aes_encrypt_cfb128( data_t * key_str, data_t * iv_str, 235 data_t * src_str, data_t * dst ) 236{ 237 unsigned char output[100]; 238 mbedtls_aes_context ctx; 239 size_t iv_offset = 0; 240 241 memset(output, 0x00, 100); 242 mbedtls_aes_init( &ctx ); 243 244 245 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 246 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 ); 247 248 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 ); 249 250exit: 251 mbedtls_aes_free( &ctx ); 252} 253/* END_CASE */ 254 255/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 256void aes_decrypt_cfb128( data_t * key_str, data_t * iv_str, 257 data_t * src_str, data_t * dst ) 258{ 259 unsigned char output[100]; 260 mbedtls_aes_context ctx; 261 size_t iv_offset = 0; 262 263 memset(output, 0x00, 100); 264 mbedtls_aes_init( &ctx ); 265 266 267 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 268 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 ); 269 270 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 ); 271 272exit: 273 mbedtls_aes_free( &ctx ); 274} 275/* END_CASE */ 276 277/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 278void aes_encrypt_cfb8( data_t * key_str, data_t * iv_str, 279 data_t * src_str, data_t * dst ) 280{ 281 unsigned char output[100]; 282 mbedtls_aes_context ctx; 283 284 memset(output, 0x00, 100); 285 mbedtls_aes_init( &ctx ); 286 287 288 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 289 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 ); 290 291 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 292 src_str->len, dst->len ) == 0 ); 293 294exit: 295 mbedtls_aes_free( &ctx ); 296} 297/* END_CASE */ 298 299/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ 300void aes_decrypt_cfb8( data_t * key_str, data_t * iv_str, 301 data_t * src_str, data_t * dst ) 302{ 303 unsigned char output[100]; 304 mbedtls_aes_context ctx; 305 306 memset(output, 0x00, 100); 307 mbedtls_aes_init( &ctx ); 308 309 310 mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); 311 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 ); 312 313 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 314 src_str->len, dst->len ) == 0 ); 315 316exit: 317 mbedtls_aes_free( &ctx ); 318} 319/* END_CASE */ 320 321/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ 322void aes_encrypt_ofb( int fragment_size, data_t *key_str, 323 data_t *iv_str, data_t *src_str, 324 data_t *expected_output ) 325{ 326 unsigned char output[32]; 327 mbedtls_aes_context ctx; 328 size_t iv_offset = 0; 329 int in_buffer_len; 330 unsigned char* src_str_next; 331 332 memset( output, 0x00, sizeof( output ) ); 333 mbedtls_aes_init( &ctx ); 334 335 TEST_ASSERT( (size_t)fragment_size < sizeof( output ) ); 336 337 TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str->x, 338 key_str->len * 8 ) == 0 ); 339 in_buffer_len = src_str->len; 340 src_str_next = src_str->x; 341 342 while( in_buffer_len > 0 ) 343 { 344 TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset, 345 iv_str->x, src_str_next, output ) == 0 ); 346 347 TEST_ASSERT( memcmp( output, expected_output->x, fragment_size ) == 0 ); 348 349 in_buffer_len -= fragment_size; 350 expected_output->x += fragment_size; 351 src_str_next += fragment_size; 352 353 if( in_buffer_len < fragment_size ) 354 fragment_size = in_buffer_len; 355 } 356 357exit: 358 mbedtls_aes_free( &ctx ); 359} 360/* END_CASE */ 361 362/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ 363void aes_check_params( ) 364{ 365 mbedtls_aes_context aes_ctx; 366#if defined(MBEDTLS_CIPHER_MODE_XTS) 367 mbedtls_aes_xts_context xts_ctx; 368#endif 369 const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; 370 const unsigned char in[16] = { 0 }; 371 unsigned char out[16]; 372 size_t size; 373 const int valid_mode = MBEDTLS_AES_ENCRYPT; 374 const int invalid_mode = 42; 375 376 TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) ); 377#if defined(MBEDTLS_CIPHER_MODE_XTS) 378 TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) ); 379#endif 380 381 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 382 mbedtls_aes_setkey_enc( NULL, key, 128 ) ); 383 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 384 mbedtls_aes_setkey_enc( &aes_ctx, NULL, 128 ) ); 385 386 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 387 mbedtls_aes_setkey_dec( NULL, key, 128 ) ); 388 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 389 mbedtls_aes_setkey_dec( &aes_ctx, NULL, 128 ) ); 390 391#if defined(MBEDTLS_CIPHER_MODE_XTS) 392 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 393 mbedtls_aes_xts_setkey_enc( NULL, key, 128 ) ); 394 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 395 mbedtls_aes_xts_setkey_enc( &xts_ctx, NULL, 128 ) ); 396 397 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 398 mbedtls_aes_xts_setkey_dec( NULL, key, 128 ) ); 399 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 400 mbedtls_aes_xts_setkey_dec( &xts_ctx, NULL, 128 ) ); 401#endif 402 403 404 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 405 mbedtls_aes_crypt_ecb( NULL, 406 valid_mode, in, out ) ); 407 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 408 mbedtls_aes_crypt_ecb( &aes_ctx, 409 invalid_mode, in, out ) ); 410 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 411 mbedtls_aes_crypt_ecb( &aes_ctx, 412 valid_mode, NULL, out ) ); 413 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 414 mbedtls_aes_crypt_ecb( &aes_ctx, 415 valid_mode, in, NULL ) ); 416 417#if defined(MBEDTLS_CIPHER_MODE_CBC) 418 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 419 mbedtls_aes_crypt_cbc( NULL, 420 valid_mode, 16, 421 out, in, out ) ); 422 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 423 mbedtls_aes_crypt_cbc( &aes_ctx, 424 invalid_mode, 16, 425 out, in, out ) ); 426 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 427 mbedtls_aes_crypt_cbc( &aes_ctx, 428 valid_mode, 16, 429 NULL, in, out ) ); 430 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 431 mbedtls_aes_crypt_cbc( &aes_ctx, 432 valid_mode, 16, 433 out, NULL, out ) ); 434 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 435 mbedtls_aes_crypt_cbc( &aes_ctx, 436 valid_mode, 16, 437 out, in, NULL ) ); 438#endif /* MBEDTLS_CIPHER_MODE_CBC */ 439 440#if defined(MBEDTLS_CIPHER_MODE_XTS) 441 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 442 mbedtls_aes_crypt_xts( NULL, 443 valid_mode, 16, 444 in, in, out ) ); 445 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 446 mbedtls_aes_crypt_xts( &xts_ctx, 447 invalid_mode, 16, 448 in, in, out ) ); 449 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 450 mbedtls_aes_crypt_xts( &xts_ctx, 451 valid_mode, 16, 452 NULL, in, out ) ); 453 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 454 mbedtls_aes_crypt_xts( &xts_ctx, 455 valid_mode, 16, 456 in, NULL, out ) ); 457 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 458 mbedtls_aes_crypt_xts( &xts_ctx, 459 valid_mode, 16, 460 in, in, NULL ) ); 461#endif /* MBEDTLS_CIPHER_MODE_XTS */ 462 463#if defined(MBEDTLS_CIPHER_MODE_CFB) 464 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 465 mbedtls_aes_crypt_cfb128( NULL, 466 valid_mode, 16, 467 &size, out, in, out ) ); 468 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 469 mbedtls_aes_crypt_cfb128( &aes_ctx, 470 invalid_mode, 16, 471 &size, out, in, out ) ); 472 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 473 mbedtls_aes_crypt_cfb128( &aes_ctx, 474 valid_mode, 16, 475 NULL, out, in, out ) ); 476 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 477 mbedtls_aes_crypt_cfb128( &aes_ctx, 478 valid_mode, 16, 479 &size, NULL, in, out ) ); 480 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 481 mbedtls_aes_crypt_cfb128( &aes_ctx, 482 valid_mode, 16, 483 &size, out, NULL, out ) ); 484 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 485 mbedtls_aes_crypt_cfb128( &aes_ctx, 486 valid_mode, 16, 487 &size, out, in, NULL ) ); 488 489 490 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 491 mbedtls_aes_crypt_cfb8( NULL, 492 valid_mode, 16, 493 out, in, out ) ); 494 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 495 mbedtls_aes_crypt_cfb8( &aes_ctx, 496 invalid_mode, 16, 497 out, in, out ) ); 498 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 499 mbedtls_aes_crypt_cfb8( &aes_ctx, 500 valid_mode, 16, 501 NULL, in, out ) ); 502 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 503 mbedtls_aes_crypt_cfb8( &aes_ctx, 504 valid_mode, 16, 505 out, NULL, out ) ); 506 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 507 mbedtls_aes_crypt_cfb8( &aes_ctx, 508 valid_mode, 16, 509 out, in, NULL ) ); 510#endif /* MBEDTLS_CIPHER_MODE_CFB */ 511 512#if defined(MBEDTLS_CIPHER_MODE_OFB) 513 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 514 mbedtls_aes_crypt_ofb( NULL, 16, 515 &size, out, in, out ) ); 516 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 517 mbedtls_aes_crypt_ofb( &aes_ctx, 16, 518 NULL, out, in, out ) ); 519 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 520 mbedtls_aes_crypt_ofb( &aes_ctx, 16, 521 &size, NULL, in, out ) ); 522 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 523 mbedtls_aes_crypt_ofb( &aes_ctx, 16, 524 &size, out, NULL, out ) ); 525 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 526 mbedtls_aes_crypt_ofb( &aes_ctx, 16, 527 &size, out, in, NULL ) ); 528#endif /* MBEDTLS_CIPHER_MODE_OFB */ 529 530#if defined(MBEDTLS_CIPHER_MODE_CTR) 531 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 532 mbedtls_aes_crypt_ctr( NULL, 16, &size, out, 533 out, in, out ) ); 534 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 535 mbedtls_aes_crypt_ctr( &aes_ctx, 16, NULL, out, 536 out, in, out ) ); 537 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 538 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, NULL, 539 out, in, out ) ); 540 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 541 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out, 542 NULL, in, out ) ); 543 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 544 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out, 545 out, NULL, out ) ); 546 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, 547 mbedtls_aes_crypt_ctr( &aes_ctx, 16, &size, out, 548 out, in, NULL ) ); 549#endif /* MBEDTLS_CIPHER_MODE_CTR */ 550} 551/* END_CASE */ 552 553/* BEGIN_CASE */ 554void aes_misc_params( ) 555{ 556#if defined(MBEDTLS_CIPHER_MODE_CBC) || \ 557 defined(MBEDTLS_CIPHER_MODE_XTS) || \ 558 defined(MBEDTLS_CIPHER_MODE_CFB) || \ 559 defined(MBEDTLS_CIPHER_MODE_OFB) 560 mbedtls_aes_context aes_ctx; 561 const unsigned char in[16] = { 0 }; 562 unsigned char out[16]; 563#endif 564#if defined(MBEDTLS_CIPHER_MODE_XTS) 565 mbedtls_aes_xts_context xts_ctx; 566#endif 567#if defined(MBEDTLS_CIPHER_MODE_CFB) || \ 568 defined(MBEDTLS_CIPHER_MODE_OFB) 569 size_t size; 570#endif 571 572 /* These calls accept NULL */ 573 TEST_VALID_PARAM( mbedtls_aes_free( NULL ) ); 574#if defined(MBEDTLS_CIPHER_MODE_XTS) 575 TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) ); 576#endif 577 578#if defined(MBEDTLS_CIPHER_MODE_CBC) 579 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, 580 15, 581 out, in, out ) 582 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); 583 TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, 584 17, 585 out, in, out ) 586 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); 587#endif 588 589#if defined(MBEDTLS_CIPHER_MODE_XTS) 590 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT, 591 15, 592 in, in, out ) 593 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); 594 TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT, 595 (1 << 24) + 1, 596 in, in, out ) 597 == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); 598#endif 599 600#if defined(MBEDTLS_CIPHER_MODE_CFB) 601 size = 16; 602 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16, 603 &size, out, in, out ) 604 == MBEDTLS_ERR_AES_BAD_INPUT_DATA ); 605#endif 606 607#if defined(MBEDTLS_CIPHER_MODE_OFB) 608 size = 16; 609 TEST_ASSERT( mbedtls_aes_crypt_ofb( &aes_ctx, 16, &size, out, in, out ) 610 == MBEDTLS_ERR_AES_BAD_INPUT_DATA ); 611#endif 612} 613/* END_CASE */ 614 615/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ 616void aes_selftest( ) 617{ 618 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 ); 619} 620/* END_CASE */ 621