/* BEGIN_HEADER */ #include /* END_HEADER */ /* BEGIN_DEPENDENCIES * depends_on:POLARSSL_CIPHER_C * END_DEPENDENCIES */ /* 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; unsigned char key[32]; unsigned char iv[16]; unsigned char ad[13]; unsigned char tag[16]; const cipher_info_t *cipher_info; cipher_context_t ctx_dec; cipher_context_t ctx_enc; unsigned char inbuf[64]; unsigned char encbuf[64]; unsigned char decbuf[64]; size_t outlen = 0; size_t total_len = 0; memset( key, 0, 32 ); memset( iv , 0, 16 ); memset( &ctx_dec, 0, sizeof( ctx_dec ) ); memset( &ctx_enc, 0, sizeof( ctx_enc ) ); memset( inbuf, 5, 64 ); memset( encbuf, 0, 64 ); memset( decbuf, 0, 64 ); memset( tag, 0, 16 ); memset( ad, 0x2a, 13 ); /* Check and get info structures */ cipher_info = cipher_info_from_type( cipher_id ); TEST_ASSERT( NULL != cipher_info ); TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info ); /* Initialise enc and dec contexts */ TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); if( -1 != pad_mode ) { TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) ); TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) ); } TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_dec, ad, 13 ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_enc, ad, 13 ) ); /* encode length number of bytes from inbuf */ TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); total_len = outlen; TEST_ASSERT( total_len == length || ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && total_len < length && total_len + cipher_get_block_size( &ctx_enc ) > length ) ); TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen, tag, 16 ) ); total_len += outlen; TEST_ASSERT( total_len == length || ( total_len % cipher_get_block_size( &ctx_enc ) == 0 && total_len > length && total_len <= length + cipher_get_block_size( &ctx_enc ) ) ); /* decode the previously encoded string */ TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) ); total_len = outlen; TEST_ASSERT( total_len == length || ( total_len % cipher_get_block_size( &ctx_dec ) == 0 && total_len < length && total_len + cipher_get_block_size( &ctx_dec ) >= length ) ); TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen, tag, 16 ) ); total_len += outlen; TEST_ASSERT( total_len == length ); TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); TEST_ASSERT( 0 == cipher_free_ctx( &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 cipher_info_t *cipher_info; cipher_context_t ctx; unsigned char inbuf[64]; unsigned char encbuf[64]; size_t outlen = 0; memset( key, 0, 32 ); memset( iv , 0, 16 ); memset( &ctx, 0, sizeof( ctx ) ); memset( inbuf, 5, 64 ); memset( encbuf, 0, 64 ); /* Check and get info structures */ cipher_info = cipher_info_from_type( cipher_id ); TEST_ASSERT( NULL != cipher_info ); /* Initialise context */ TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) ); TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) ); TEST_ASSERT( 0 == cipher_reset( &ctx, NULL, 0 ) ); /* encode length number of bytes from inbuf */ TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) ); TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen, NULL, 0 ) ); /* done */ TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); } /* END_CASE */ /* BEGIN_CASE */ void dec_empty_buf() { unsigned char key[32]; unsigned char iv[16]; cipher_context_t ctx_dec; const 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 ); memset( &ctx_dec, 0, sizeof( ctx_dec ) ); memset( encbuf, 0, 64 ); memset( decbuf, 0, 64 ); /* Initialise context */ cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); TEST_ASSERT( NULL != cipher_info); TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_dec, NULL, 0 ) ); /* decode 0-byte string */ TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); TEST_ASSERT( 0 == outlen ); TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen, NULL, 0 ) ); TEST_ASSERT( 0 == outlen ); TEST_ASSERT( 0 == cipher_free_ctx( &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 ) { size_t first_length = first_length_val; size_t second_length = second_length_val; size_t length = first_length + second_length; unsigned char key[32]; unsigned char iv[16]; cipher_context_t ctx_dec; cipher_context_t ctx_enc; const 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 ); memset( &ctx_dec, 0, sizeof( ctx_dec ) ); memset( &ctx_enc, 0, sizeof( ctx_enc ) ); memset( inbuf, 5, 64 ); memset( encbuf, 0, 64 ); memset( decbuf, 0, 64 ); /* Initialise enc and dec contexts */ cipher_info = cipher_info_from_type( cipher_id ); TEST_ASSERT( NULL != cipher_info); TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) ); TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) ); TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) ); TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_dec, NULL, 0 ) ); TEST_ASSERT( 0 == cipher_reset( &ctx_enc, NULL, 0 ) ); /* encode length number of bytes from inbuf */ TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); totaloutlen = outlen; TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); totaloutlen += outlen; TEST_ASSERT( totaloutlen == length || ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && totaloutlen < length && totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) ); TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen, NULL, 0 ) ); totaloutlen += outlen; TEST_ASSERT( totaloutlen == length || ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 && totaloutlen > length && totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) ); /* decode the previously encoded string */ TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) ); totaloutlen = outlen; TEST_ASSERT( totaloutlen == length || ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 && totaloutlen < length && totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) ); TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen, NULL, 0 ) ); totaloutlen += outlen; TEST_ASSERT( totaloutlen == length ); TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); } /* END_CASE */ /* BEGIN_CASE */ void set_padding( int cipher_id, int pad_mode, int ret ) { const cipher_info_t *cipher_info; cipher_context_t ctx; cipher_info = cipher_info_from_type( cipher_id ); TEST_ASSERT( NULL != cipher_info ); TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) ); TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) ); TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) ); } /* END_CASE */ /* BEGIN_CASE */ void check_padding( int pad_mode, char *input_str, int ret, int dlen_check ) { cipher_info_t cipher_info; cipher_context_t ctx; unsigned char input[16]; size_t ilen, dlen; /* build a fake context just for getting access to get_padding */ memset( &ctx, 0, sizeof( ctx ) ); cipher_info.mode = POLARSSL_MODE_CBC; ctx.cipher_info = &cipher_info; TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) ); ilen = unhexify( input, input_str ); TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) ); if( 0 == ret ) TEST_ASSERT( dlen == (size_t) dlen_check ); } /* END_CASE */ /* BEGIN_CASE */ void cipher_selftest() { TEST_ASSERT( cipher_self_test( 0 ) == 0 ); } /* END_CASE */