123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581 |
- /* BEGIN_HEADER */
- #include "mbedtls/cipher.h"
- #if defined(MBEDTLS_AES_C)
- #include "mbedtls/aes.h"
- #endif
- #if defined(MBEDTLS_GCM_C)
- #include "mbedtls/gcm.h"
- #endif
- #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
- #define MBEDTLS_CIPHER_AUTH_CRYPT
- #endif
- #if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
- /* Helper for resetting key/direction
- *
- * The documentation doesn't explicitly say whether calling
- * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
- * the default software implementation, but only by accident. It isn't
- * guaranteed to work with new ciphers or with alternative implementations of
- * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
- * it, and instead start with a fresh context.
- */
- static int cipher_reset_key( mbedtls_cipher_context_t *ctx, int cipher_id,
- int use_psa, size_t tag_len, const data_t *key, int direction )
- {
- mbedtls_cipher_free( ctx );
- mbedtls_cipher_init( ctx );
- #if !defined(MBEDTLS_USE_PSA_CRYPTO)
- (void) use_psa;
- (void) tag_len;
- #else
- if( use_psa == 1 )
- {
- TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
- mbedtls_cipher_info_from_type( cipher_id ),
- tag_len ) );
- }
- else
- #endif /* MBEDTLS_USE_PSA_CRYPTO */
- {
- TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
- mbedtls_cipher_info_from_type( cipher_id ) ) );
- }
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
- direction ) );
- return( 1 );
- exit:
- return( 0 );
- }
- /*
- * Check if a buffer is all-0 bytes:
- * return 1 if it is,
- * 0 if it isn't.
- */
- int buffer_is_all_zero( const uint8_t *buf, size_t size )
- {
- for( size_t i = 0; i < size; i++ )
- if( buf[i] != 0 )
- return 0;
- return 1;
- }
- #endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
- /* END_HEADER */
- /* BEGIN_DEPENDENCIES
- * depends_on:MBEDTLS_CIPHER_C
- * END_DEPENDENCIES
- */
- /* BEGIN_CASE */
- void mbedtls_cipher_list( )
- {
- const int *cipher_type;
- for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
- TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void cipher_invalid_param_unconditional( )
- {
- mbedtls_cipher_context_t valid_ctx;
- mbedtls_cipher_context_t invalid_ctx;
- mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
- mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
- unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
- int valid_size = sizeof(valid_buffer);
- int valid_bitlen = valid_size * 8;
- const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
- *( mbedtls_cipher_list() ) );
- size_t size_t_var;
- (void)valid_mode; /* In some configurations this is unused */
- mbedtls_cipher_init( &valid_ctx );
- mbedtls_cipher_init( &invalid_ctx );
- TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, valid_info ) == 0 );
- /* mbedtls_cipher_setup() */
- TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- /* mbedtls_cipher_get_block_size() */
- TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
- /* mbedtls_cipher_get_cipher_mode() */
- TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
- MBEDTLS_MODE_NONE );
- /* mbedtls_cipher_get_iv_size() */
- TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
- /* mbedtls_cipher_get_type() */
- TEST_ASSERT(
- mbedtls_cipher_get_type( &invalid_ctx ) ==
- MBEDTLS_CIPHER_NONE);
- /* mbedtls_cipher_get_name() */
- TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
- /* mbedtls_cipher_get_key_bitlen() */
- TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
- MBEDTLS_KEY_LENGTH_NONE );
- /* mbedtls_cipher_get_operation() */
- TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
- MBEDTLS_OPERATION_NONE );
- /* mbedtls_cipher_setkey() */
- TEST_ASSERT(
- mbedtls_cipher_setkey( &invalid_ctx,
- valid_buffer,
- valid_bitlen,
- valid_operation ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- /* mbedtls_cipher_set_iv() */
- TEST_ASSERT(
- mbedtls_cipher_set_iv( &invalid_ctx,
- valid_buffer,
- valid_size ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- /* mbedtls_cipher_reset() */
- TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- /* mbedtls_cipher_update_ad() */
- TEST_ASSERT(
- mbedtls_cipher_update_ad( &invalid_ctx,
- valid_buffer,
- valid_size ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
- #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
- /* mbedtls_cipher_set_padding_mode() */
- TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- #endif
- /* mbedtls_cipher_update() */
- TEST_ASSERT(
- mbedtls_cipher_update( &invalid_ctx,
- valid_buffer,
- valid_size,
- valid_buffer,
- &size_t_var ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- /* mbedtls_cipher_finish() */
- TEST_ASSERT(
- mbedtls_cipher_finish( &invalid_ctx,
- valid_buffer,
- &size_t_var ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- /* mbedtls_cipher_write_tag() */
- TEST_ASSERT(
- mbedtls_cipher_write_tag( &invalid_ctx,
- valid_buffer,
- valid_size ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- /* mbedtls_cipher_check_tag() */
- TEST_ASSERT(
- mbedtls_cipher_check_tag( &invalid_ctx,
- valid_buffer,
- valid_size ) ==
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
- exit:
- mbedtls_cipher_free( &invalid_ctx );
- mbedtls_cipher_free( &valid_ctx );
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
- void cipher_invalid_param_conditional( )
- {
- mbedtls_cipher_context_t valid_ctx;
- mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
- mbedtls_operation_t invalid_operation = 100;
- mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
- unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
- int valid_size = sizeof(valid_buffer);
- int valid_bitlen = valid_size * 8;
- const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
- *( mbedtls_cipher_list() ) );
- size_t size_t_var;
- (void)valid_mode; /* In some configurations this is unused */
- /* mbedtls_cipher_init() */
- TEST_VALID_PARAM( mbedtls_cipher_init( &valid_ctx ) );
- TEST_INVALID_PARAM( mbedtls_cipher_init( NULL ) );
- /* mbedtls_cipher_setup() */
- TEST_VALID_PARAM( mbedtls_cipher_setup( &valid_ctx, valid_info ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_setup( NULL, valid_info ) );
- /* mbedtls_cipher_get_block_size() */
- TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_block_size( NULL ) );
- /* mbedtls_cipher_get_cipher_mode() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_MODE_NONE,
- mbedtls_cipher_get_cipher_mode( NULL ) );
- /* mbedtls_cipher_get_iv_size() */
- TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_iv_size( NULL ) );
- /* mbedtls_cipher_get_type() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_CIPHER_NONE,
- mbedtls_cipher_get_type( NULL ) );
- /* mbedtls_cipher_get_name() */
- TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_name( NULL ) );
- /* mbedtls_cipher_get_key_bitlen() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_KEY_LENGTH_NONE,
- mbedtls_cipher_get_key_bitlen( NULL ) );
- /* mbedtls_cipher_get_operation() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_OPERATION_NONE,
- mbedtls_cipher_get_operation( NULL ) );
- /* mbedtls_cipher_setkey() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_setkey( NULL,
- valid_buffer,
- valid_bitlen,
- valid_operation ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_setkey( &valid_ctx,
- NULL,
- valid_bitlen,
- valid_operation ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_setkey( &valid_ctx,
- valid_buffer,
- valid_bitlen,
- invalid_operation ) );
- /* mbedtls_cipher_set_iv() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_set_iv( NULL,
- valid_buffer,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_set_iv( &valid_ctx,
- NULL,
- valid_size ) );
- /* mbedtls_cipher_reset() */
- TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_reset( NULL ) );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- /* mbedtls_cipher_update_ad() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_update_ad( NULL,
- valid_buffer,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_update_ad( &valid_ctx,
- NULL,
- valid_size ) );
- #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
- #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
- /* mbedtls_cipher_set_padding_mode() */
- TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_set_padding_mode( NULL, valid_mode ) );
- #endif
- /* mbedtls_cipher_update() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_update( NULL,
- valid_buffer,
- valid_size,
- valid_buffer,
- &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_update( &valid_ctx,
- NULL, valid_size,
- valid_buffer,
- &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_update( &valid_ctx,
- valid_buffer, valid_size,
- NULL,
- &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_update( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer,
- NULL ) );
- /* mbedtls_cipher_finish() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_finish( NULL,
- valid_buffer,
- &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_finish( &valid_ctx,
- NULL,
- &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_finish( &valid_ctx,
- valid_buffer,
- NULL ) );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- /* mbedtls_cipher_write_tag() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_write_tag( NULL,
- valid_buffer,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_write_tag( &valid_ctx,
- NULL,
- valid_size ) );
- /* mbedtls_cipher_check_tag() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_check_tag( NULL,
- valid_buffer,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_check_tag( &valid_ctx,
- NULL,
- valid_size ) );
- #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
- /* mbedtls_cipher_crypt() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_crypt( NULL,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_crypt( &valid_ctx,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_crypt( &valid_ctx,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_crypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, &size_t_var ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_crypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, NULL ) );
- #if defined(MBEDTLS_CIPHER_MODE_AEAD)
- /* mbedtls_cipher_auth_encrypt() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt( NULL,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt( &valid_ctx,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt( &valid_ctx,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, NULL,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var,
- NULL, valid_size ) );
- /* mbedtls_cipher_auth_decrypt() */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt( NULL,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt( &valid_ctx,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt( &valid_ctx,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, &size_t_var,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, NULL,
- valid_buffer, valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, &size_t_var,
- NULL, valid_size ) );
- #endif /* defined(MBEDTLS_CIPHER_MODE_AEAD) */
- #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
- /* mbedtls_cipher_auth_encrypt_ext */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt_ext( NULL,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_encrypt_ext( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size, NULL,
- valid_size ) );
- /* mbedtls_cipher_auth_decrypt_ext */
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt_ext( NULL,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, valid_size,
- valid_buffer, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- NULL, valid_size, &size_t_var,
- valid_size ) );
- TEST_INVALID_PARAM_RET(
- MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
- mbedtls_cipher_auth_decrypt_ext( &valid_ctx,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size,
- valid_buffer, valid_size, NULL,
- valid_size ) );
- #endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
- /* mbedtls_cipher_free() */
- TEST_VALID_PARAM( mbedtls_cipher_free( NULL ) );
- exit:
- TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) );
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
- void cipher_special_behaviours( )
- {
- const mbedtls_cipher_info_t *cipher_info;
- mbedtls_cipher_context_t ctx;
- unsigned char input[32];
- unsigned char output[32];
- #if defined (MBEDTLS_CIPHER_MODE_CBC)
- unsigned char iv[32];
- #endif
- size_t olen = 0;
- mbedtls_cipher_init( &ctx );
- memset( input, 0, sizeof( input ) );
- memset( output, 0, sizeof( output ) );
- #if defined(MBEDTLS_CIPHER_MODE_CBC)
- memset( iv, 0, sizeof( iv ) );
- /* Check and get info structures */
- cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
- TEST_ASSERT( NULL != cipher_info );
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
- /* IV too big */
- TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
- == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
- /* IV too small */
- TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
- == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- mbedtls_cipher_free( &ctx );
- mbedtls_cipher_init( &ctx );
- #endif /* MBEDTLS_CIPHER_MODE_CBC */
- cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
- TEST_ASSERT( NULL != cipher_info );
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
- /* Update ECB with partial block */
- TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
- == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
- exit:
- mbedtls_cipher_free( &ctx );
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
- int length_val, int pad_mode )
- {
- size_t length = length_val, outlen, total_len, i, block_size;
- unsigned char key[64];
- unsigned char iv[16];
- unsigned char ad[13];
- unsigned char tag[16];
- unsigned char inbuf[64];
- unsigned char encbuf[64];
- unsigned char decbuf[64];
- const mbedtls_cipher_info_t *cipher_info;
- mbedtls_cipher_context_t ctx_dec;
- mbedtls_cipher_context_t ctx_enc;
- /*
- * Prepare contexts
- */
- mbedtls_cipher_init( &ctx_dec );
- mbedtls_cipher_init( &ctx_enc );
- memset( key, 0x2a, sizeof( key ) );
- /* Check and get info structures */
- cipher_info = mbedtls_cipher_info_from_type( cipher_id );
- TEST_ASSERT( NULL != cipher_info );
- TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
- /* Initialise enc and dec contexts */
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
- #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
- if( -1 != pad_mode )
- {
- TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
- TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
- }
- #else
- (void) pad_mode;
- #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
- /*
- * Do a few encode/decode cycles
- */
- for( i = 0; i < 3; i++ )
- {
- memset( iv , 0x00 + i, sizeof( iv ) );
- memset( ad, 0x10 + i, sizeof( ad ) );
- memset( inbuf, 0x20 + i, sizeof( inbuf ) );
- memset( encbuf, 0, sizeof( encbuf ) );
- memset( decbuf, 0, sizeof( decbuf ) );
- memset( tag, 0, sizeof( tag ) );
- TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
- TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
- TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
- TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
- TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
- #endif
- block_size = mbedtls_cipher_get_block_size( &ctx_enc );
- TEST_ASSERT( block_size != 0 );
- /* encode length number of bytes from inbuf */
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
- total_len = outlen;
- TEST_ASSERT( total_len == length ||
- ( total_len % block_size == 0 &&
- total_len < length &&
- total_len + block_size > length ) );
- TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
- total_len += outlen;
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
- #endif
- TEST_ASSERT( total_len == length ||
- ( total_len % block_size == 0 &&
- total_len > length &&
- total_len <= length + block_size ) );
- /* decode the previously encoded string */
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
- total_len = outlen;
- TEST_ASSERT( total_len == length ||
- ( total_len % block_size == 0 &&
- total_len < length &&
- total_len + block_size >= length ) );
- TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
- total_len += outlen;
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
- #endif
- /* check result */
- TEST_ASSERT( total_len == length );
- TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
- }
- /*
- * Done
- */
- exit:
- mbedtls_cipher_free( &ctx_dec );
- mbedtls_cipher_free( &ctx_enc );
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
- int ret )
- {
- size_t length = length_val;
- unsigned char key[32];
- unsigned char iv[16];
- const mbedtls_cipher_info_t *cipher_info;
- mbedtls_cipher_context_t ctx;
- unsigned char inbuf[64];
- unsigned char encbuf[64];
- size_t outlen = 0;
- memset( key, 0, 32 );
- memset( iv , 0, 16 );
- mbedtls_cipher_init( &ctx );
- memset( inbuf, 5, 64 );
- memset( encbuf, 0, 64 );
- /* Check and get info structures */
- cipher_info = mbedtls_cipher_info_from_type( cipher_id );
- TEST_ASSERT( NULL != cipher_info );
- /* Initialise context */
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
- #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
- TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
- #else
- (void) pad_mode;
- #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
- TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
- TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
- #endif
- /* encode length number of bytes from inbuf */
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
- TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
- /* done */
- exit:
- mbedtls_cipher_free( &ctx );
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void dec_empty_buf( int cipher,
- int expected_update_ret,
- int expected_finish_ret )
- {
- unsigned char key[32];
- unsigned char iv[16];
- mbedtls_cipher_context_t ctx_dec;
- const mbedtls_cipher_info_t *cipher_info;
- unsigned char encbuf[64];
- unsigned char decbuf[64];
- size_t outlen = 0;
- memset( key, 0, 32 );
- memset( iv , 0, 16 );
- mbedtls_cipher_init( &ctx_dec );
- memset( encbuf, 0, 64 );
- memset( decbuf, 0, 64 );
- /* Initialise context */
- cipher_info = mbedtls_cipher_info_from_type( cipher );
- TEST_ASSERT( NULL != cipher_info);
- TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
- key, cipher_info->key_bitlen,
- MBEDTLS_DECRYPT ) );
- TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
- TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
- #endif
- /* decode 0-byte string */
- TEST_ASSERT( expected_update_ret ==
- mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
- TEST_ASSERT( 0 == outlen );
- if ( expected_finish_ret == 0 &&
- ( cipher_info->mode == MBEDTLS_MODE_CBC ||
- cipher_info->mode == MBEDTLS_MODE_ECB ) )
- {
- /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
- * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
- * decrypting an empty buffer.
- * On the other hand, CBC and ECB ciphers need a full block of input.
- */
- expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
- }
- TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
- &ctx_dec, decbuf + outlen, &outlen ) );
- TEST_ASSERT( 0 == outlen );
- exit:
- mbedtls_cipher_free( &ctx_dec );
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
- int second_length_val, int pad_mode,
- int first_encrypt_output_len, int second_encrypt_output_len,
- int first_decrypt_output_len, int second_decrypt_output_len )
- {
- size_t first_length = first_length_val;
- size_t second_length = second_length_val;
- size_t length = first_length + second_length;
- size_t block_size;
- unsigned char key[32];
- unsigned char iv[16];
- mbedtls_cipher_context_t ctx_dec;
- mbedtls_cipher_context_t ctx_enc;
- const mbedtls_cipher_info_t *cipher_info;
- unsigned char inbuf[64];
- unsigned char encbuf[64];
- unsigned char decbuf[64];
- size_t outlen = 0;
- size_t totaloutlen = 0;
- memset( key, 0, 32 );
- memset( iv , 0, 16 );
- mbedtls_cipher_init( &ctx_dec );
- mbedtls_cipher_init( &ctx_enc );
- memset( inbuf, 5, 64 );
- memset( encbuf, 0, 64 );
- memset( decbuf, 0, 64 );
- /* Initialise enc and dec contexts */
- cipher_info = mbedtls_cipher_info_from_type( cipher_id );
- TEST_ASSERT( NULL != cipher_info);
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
- #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
- if( -1 != pad_mode )
- {
- TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
- TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
- }
- #else
- (void) pad_mode;
- #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
- TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
- TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
- TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
- TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
- TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
- #endif
- block_size = mbedtls_cipher_get_block_size( &ctx_enc );
- TEST_ASSERT( block_size != 0 );
- /* encode length number of bytes from inbuf */
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
- TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
- totaloutlen = outlen;
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
- TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
- totaloutlen += outlen;
- TEST_ASSERT( totaloutlen == length ||
- ( totaloutlen % block_size == 0 &&
- totaloutlen < length &&
- totaloutlen + block_size > length ) );
- TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
- totaloutlen += outlen;
- TEST_ASSERT( totaloutlen == length ||
- ( totaloutlen % block_size == 0 &&
- totaloutlen > length &&
- totaloutlen <= length + block_size ) );
- /* decode the previously encoded string */
- second_length = totaloutlen - first_length;
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
- TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
- totaloutlen = outlen;
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
- TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
- totaloutlen += outlen;
- TEST_ASSERT( totaloutlen == length ||
- ( totaloutlen % block_size == 0 &&
- totaloutlen < length &&
- totaloutlen + block_size >= length ) );
- TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
- totaloutlen += outlen;
- TEST_ASSERT( totaloutlen == length );
- TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
- exit:
- mbedtls_cipher_free( &ctx_dec );
- mbedtls_cipher_free( &ctx_enc );
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
- data_t * iv, data_t * cipher,
- data_t * clear, data_t * ad, data_t * tag,
- int finish_result, int tag_result )
- {
- unsigned char output[265];
- mbedtls_cipher_context_t ctx;
- size_t outlen, total_len;
- mbedtls_cipher_init( &ctx );
- memset( output, 0x00, sizeof( output ) );
- #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
- ((void) ad);
- ((void) tag);
- #endif
- /* Prepare context */
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
- mbedtls_cipher_info_from_type( cipher_id ) ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
- #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
- if( pad_mode != -1 )
- TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
- #else
- (void) pad_mode;
- #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
- TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
- TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
- #endif
- /* decode buffer and check tag->x */
- total_len = 0;
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
- total_len += outlen;
- TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
- &outlen ) );
- total_len += outlen;
- #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
- TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
- #endif
- /* check plaintext only if everything went fine */
- if( 0 == finish_result && 0 == tag_result )
- {
- TEST_ASSERT( total_len == clear->len );
- TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
- }
- exit:
- mbedtls_cipher_free( &ctx );
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
- void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
- data_t * ad, data_t * cipher, data_t * tag,
- char * result, data_t * clear, int use_psa )
- {
- /*
- * Take an AEAD ciphertext + tag and perform a pair
- * of AEAD decryption and AEAD encryption. Check that
- * this results in the expected plaintext, and that
- * decryption and encryption are inverse to one another.
- *
- * Do that twice:
- * - once with legacy functions auth_decrypt/auth_encrypt
- * - once with new functions auth_decrypt_ext/auth_encrypt_ext
- * This allows testing both without duplicating test cases.
- */
- int ret;
- int using_nist_kw, using_nist_kw_padding;
- mbedtls_cipher_context_t ctx;
- size_t outlen;
- unsigned char *cipher_plus_tag = NULL;
- size_t cipher_plus_tag_len;
- unsigned char *decrypt_buf = NULL;
- size_t decrypt_buf_len = 0;
- unsigned char *encrypt_buf = NULL;
- size_t encrypt_buf_len = 0;
- #if !defined(MBEDTLS_DEPRECATED_WARNING) && \
- !defined(MBEDTLS_DEPRECATED_REMOVED)
- unsigned char *tmp_tag = NULL;
- unsigned char *tmp_cipher = NULL;
- unsigned char *tag_buf = NULL;
- #endif /* !MBEDTLS_DEPRECATED_WARNING && !MBEDTLS_DEPRECATED_REMOVED */
- /* Null pointers are documented as valid for inputs of length 0.
- * The test framework passes non-null pointers, so set them to NULL.
- * key, cipher and tag can't be empty. */
- if( iv->len == 0 )
- iv->x = NULL;
- if( ad->len == 0 )
- ad->x = NULL;
- if( clear->len == 0 )
- clear->x = NULL;
- mbedtls_cipher_init( &ctx );
- /* Initialize PSA Crypto */
- #if defined(MBEDTLS_USE_PSA_CRYPTO)
- if( use_psa == 1 )
- PSA_ASSERT( psa_crypto_init( ) );
- #else
- (void) use_psa;
- #endif
- /*
- * Are we using NIST_KW? with padding?
- */
- using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
- cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
- cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
- using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
- cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
- cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
- using_nist_kw_padding;
- /****************************************************************
- * *
- * Part 1: non-deprecated API *
- * *
- ****************************************************************/
- /*
- * Prepare context for decryption
- */
- if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
- MBEDTLS_DECRYPT ) )
- goto exit;
- /*
- * prepare buffer for decryption
- * (we need the tag appended to the ciphertext)
- */
- cipher_plus_tag_len = cipher->len + tag->len;
- ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
- memcpy( cipher_plus_tag, cipher->x, cipher->len );
- memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
- /*
- * Compute length of output buffer according to the documentation
- */
- if( using_nist_kw )
- decrypt_buf_len = cipher_plus_tag_len - 8;
- else
- decrypt_buf_len = cipher_plus_tag_len - tag->len;
- /*
- * Try decrypting to a buffer that's 1B too small
- */
- if( decrypt_buf_len != 0 )
- {
- ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
- outlen = 0;
- ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
- ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
- decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
- TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
- mbedtls_free( decrypt_buf );
- decrypt_buf = NULL;
- }
- /*
- * Authenticate and decrypt, and check result
- */
- ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
- outlen = 0;
- ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
- ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
- decrypt_buf, decrypt_buf_len, &outlen, tag->len );
- if( strcmp( result, "FAIL" ) == 0 )
- {
- TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
- TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
- }
- else
- {
- TEST_ASSERT( ret == 0 );
- ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
- }
- /* Free this, but keep cipher_plus_tag for deprecated function with PSA */
- mbedtls_free( decrypt_buf );
- decrypt_buf = NULL;
- /*
- * Encrypt back if test data was authentic
- */
- if( strcmp( result, "FAIL" ) != 0 )
- {
- /* prepare context for encryption */
- if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
- MBEDTLS_ENCRYPT ) )
- goto exit;
- /*
- * Compute size of output buffer according to documentation
- */
- if( using_nist_kw )
- {
- encrypt_buf_len = clear->len + 8;
- if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
- encrypt_buf_len += 8 - encrypt_buf_len % 8;
- }
- else
- {
- encrypt_buf_len = clear->len + tag->len;
- }
- /*
- * Try encrypting with an output buffer that's 1B too small
- */
- ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
- outlen = 0;
- ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
- ad->x, ad->len, clear->x, clear->len,
- encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
- TEST_ASSERT( ret != 0 );
- mbedtls_free( encrypt_buf );
- encrypt_buf = NULL;
- /*
- * Encrypt and check the result
- */
- ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
- outlen = 0;
- ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
- ad->x, ad->len, clear->x, clear->len,
- encrypt_buf, encrypt_buf_len, &outlen, tag->len );
- TEST_ASSERT( ret == 0 );
- TEST_ASSERT( outlen == cipher->len + tag->len );
- TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
- TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
- tag->x, tag->len ) == 0 );
- mbedtls_free( encrypt_buf );
- encrypt_buf = NULL;
- }
- /****************************************************************
- * *
- * Part 2: deprecated API *
- * *
- ****************************************************************/
- #if !defined(MBEDTLS_DEPRECATED_WARNING) && \
- !defined(MBEDTLS_DEPRECATED_REMOVED)
- /*
- * Prepare context for decryption
- */
- if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
- MBEDTLS_DECRYPT ) )
- goto exit;
- /*
- * Prepare pointers for decryption
- */
- #if defined(MBEDTLS_USE_PSA_CRYPTO)
- if( use_psa == 1 )
- {
- /* PSA requires that the tag immediately follows the ciphertext.
- * Fortunately, we already have that from testing the new API. */
- tmp_cipher = cipher_plus_tag;
- tmp_tag = tmp_cipher + cipher->len;
- }
- else
- #endif /* MBEDTLS_USE_PSA_CRYPTO */
- {
- tmp_cipher = cipher->x;
- tmp_tag = tag->x;
- }
- /*
- * Authenticate and decrypt, and check result
- */
- ASSERT_ALLOC( decrypt_buf, cipher->len );
- outlen = 0;
- ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
- tmp_cipher, cipher->len, decrypt_buf, &outlen,
- tmp_tag, tag->len );
- if( using_nist_kw )
- {
- /* NIST_KW with legacy API */
- TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
- }
- else if( strcmp( result, "FAIL" ) == 0 )
- {
- /* unauthentic message */
- TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
- TEST_ASSERT( buffer_is_all_zero( decrypt_buf, cipher->len ) );
- }
- else
- {
- /* authentic message: is the plaintext correct? */
- TEST_ASSERT( ret == 0 );
- ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
- }
- mbedtls_free( decrypt_buf );
- decrypt_buf = NULL;
- mbedtls_free( cipher_plus_tag );
- cipher_plus_tag = NULL;
- /*
- * Encrypt back if test data was authentic
- */
- if( strcmp( result, "FAIL" ) != 0 )
- {
- /* prepare context for encryption */
- if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
- MBEDTLS_ENCRYPT ) )
- goto exit;
- /* prepare buffers for encryption */
- #if defined(MBEDTLS_USE_PSA_CRYPTO)
- if( use_psa )
- {
- ASSERT_ALLOC( cipher_plus_tag, cipher->len + tag->len );
- tmp_cipher = cipher_plus_tag;
- tmp_tag = cipher_plus_tag + cipher->len;
- }
- else
- #endif /* MBEDTLS_USE_PSA_CRYPTO */
- {
- ASSERT_ALLOC( encrypt_buf, cipher->len );
- ASSERT_ALLOC( tag_buf, tag->len );
- tmp_cipher = encrypt_buf;
- tmp_tag = tag_buf;
- }
- /*
- * Encrypt and check the result
- */
- outlen = 0;
- ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
- clear->x, clear->len, tmp_cipher, &outlen,
- tmp_tag, tag->len );
- if( using_nist_kw )
- {
- TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
- }
- else
- {
- TEST_ASSERT( ret == 0 );
- TEST_ASSERT( outlen == cipher->len );
- if( cipher->len != 0 )
- TEST_ASSERT( memcmp( tmp_cipher, cipher->x, cipher->len ) == 0 );
- TEST_ASSERT( memcmp( tmp_tag, tag->x, tag->len ) == 0 );
- }
- }
- #endif /* !MBEDTLS_DEPRECATED_WARNING && !MBEDTLS_DEPRECATED_REMOVED */
- exit:
- mbedtls_cipher_free( &ctx );
- mbedtls_free( decrypt_buf );
- mbedtls_free( encrypt_buf );
- mbedtls_free( cipher_plus_tag );
- #if !defined(MBEDTLS_DEPRECATED_WARNING) && \
- !defined(MBEDTLS_DEPRECATED_REMOVED)
- mbedtls_free( tag_buf );
- #endif /* !MBEDTLS_DEPRECATED_WARNING && !MBEDTLS_DEPRECATED_REMOVED */
- #if defined(MBEDTLS_USE_PSA_CRYPTO)
- if( use_psa == 1 )
- PSA_DONE( );
- #endif /* MBEDTLS_USE_PSA_CRYPTO */
- }
- /* END_CASE */
- /* BEGIN_CASE */
- void test_vec_ecb( int cipher_id, int operation, data_t * key,
- data_t * input, data_t * result, int finish_result
- )
- {
- mbedtls_cipher_context_t ctx;
- unsigned char output[32];
- size_t outlen;
- mbedtls_cipher_init( &ctx );
- memset( output, 0x00, sizeof( output ) );
- /* Prepare context */
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
- mbedtls_cipher_info_from_type( cipher_id ) ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
- TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
- mbedtls_cipher_get_block_size( &ctx ),
- output, &outlen ) );
- TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
- TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
- &outlen ) );
- TEST_ASSERT( 0 == outlen );
- /* check plaintext only if everything went fine */
- if( 0 == finish_result )
- TEST_ASSERT( 0 == memcmp( output, result->x,
- mbedtls_cipher_get_block_size( &ctx ) ) );
- exit:
- mbedtls_cipher_free( &ctx );
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
- void test_vec_crypt( int cipher_id, int operation, data_t *key,
- data_t *iv, data_t *input, data_t *result,
- int finish_result, int use_psa )
- {
- mbedtls_cipher_context_t ctx;
- unsigned char output[32];
- size_t outlen;
- mbedtls_cipher_init( &ctx );
- memset( output, 0x00, sizeof( output ) );
- /* Prepare context */
- #if !defined(MBEDTLS_USE_PSA_CRYPTO)
- (void) use_psa;
- #else
- if( use_psa == 1 )
- {
- PSA_ASSERT( psa_crypto_init( ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
- mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
- }
- else
- #endif /* MBEDTLS_USE_PSA_CRYPTO */
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
- mbedtls_cipher_info_from_type( cipher_id ) ) );
- TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
- if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
- TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
- TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
- iv->len, input->x, input->len,
- output, &outlen ) );
- TEST_ASSERT( result->len == outlen );
- /* check plaintext only if everything went fine */
- if( 0 == finish_result )
- TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
- exit:
- mbedtls_cipher_free( &ctx );
- #if defined(MBEDTLS_USE_PSA_CRYPTO)
- PSA_DONE( );
- #endif /* MBEDTLS_USE_PSA_CRYPTO */
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
- void set_padding( int cipher_id, int pad_mode, int ret )
- {
- const mbedtls_cipher_info_t *cipher_info;
- mbedtls_cipher_context_t ctx;
- mbedtls_cipher_init( &ctx );
- cipher_info = mbedtls_cipher_info_from_type( cipher_id );
- TEST_ASSERT( NULL != cipher_info );
- TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
- TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
- exit:
- mbedtls_cipher_free( &ctx );
- }
- /* END_CASE */
- /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
- void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
- )
- {
- mbedtls_cipher_info_t cipher_info;
- mbedtls_cipher_context_t ctx;
- size_t dlen;
- /* build a fake context just for getting access to get_padding */
- mbedtls_cipher_init( &ctx );
- cipher_info.mode = MBEDTLS_MODE_CBC;
- ctx.cipher_info = &cipher_info;
- TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
- TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
- if( 0 == ret )
- TEST_ASSERT( dlen == (size_t) dlen_check );
- }
- /* END_CASE */
|