2011-01-06 16:37:30 +01:00
|
|
|
BEGIN_HEADER
|
|
|
|
#include <polarssl/cipher.h>
|
|
|
|
END_HEADER
|
|
|
|
|
2011-05-26 15:16:06 +02:00
|
|
|
BEGIN_DEPENDENCIES
|
|
|
|
depends_on:POLARSSL_CIPHER_C
|
|
|
|
END_DEPENDENCIES
|
|
|
|
|
2011-01-06 16:37:30 +01:00
|
|
|
BEGIN_CASE
|
|
|
|
enc_dec_buf:cipher_id:cipher_string:key_len:length:
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t length = {length};
|
2011-01-06 16:37:30 +01:00
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char iv[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];
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t outlen = 0;
|
|
|
|
size_t enclen = 0;
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
/* 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 ) );
|
|
|
|
|
|
|
|
TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) );
|
|
|
|
TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
|
|
|
|
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
enclen = cipher_get_block_size( &ctx_enc )
|
2011-01-06 16:37:30 +01:00
|
|
|
* ( 1 + length / cipher_get_block_size( &ctx_enc ) );
|
2011-06-09 16:27:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enclen = length;
|
|
|
|
}
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
/* encode length number of bytes from inbuf */
|
|
|
|
TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( outlen == enclen - cipher_get_block_size ( &ctx_enc ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( outlen == enclen );
|
|
|
|
}
|
|
|
|
|
2011-01-06 16:37:30 +01:00
|
|
|
TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( outlen == cipher_get_block_size ( &ctx_enc ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( outlen == 0 );
|
|
|
|
}
|
|
|
|
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
/* decode the previously encoded string */
|
|
|
|
TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( enclen == outlen );
|
|
|
|
}
|
|
|
|
|
2011-01-06 16:37:30 +01:00
|
|
|
TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( outlen == 0 );
|
|
|
|
}
|
|
|
|
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
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];
|
|
|
|
|
2011-04-24 17:53:29 +02:00
|
|
|
size_t outlen = 0;
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
memset( key, 0, 32 );
|
|
|
|
memset( iv , 0, 16 );
|
|
|
|
|
|
|
|
memset( &ctx_dec, 0, sizeof( ctx_dec ) );
|
|
|
|
|
|
|
|
memset( encbuf, 0, 64 );
|
|
|
|
memset( decbuf, 0, 64 );
|
|
|
|
|
|
|
|
/* Initialise enc and dec contexts */
|
|
|
|
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_reset( &ctx_dec, iv ) );
|
|
|
|
|
|
|
|
/* decode 0-byte string */
|
|
|
|
TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
|
|
|
|
TEST_ASSERT( 0 == outlen );
|
2011-06-09 17:44:37 +02:00
|
|
|
TEST_ASSERT( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
|
2011-01-06 16:37:30 +01:00
|
|
|
TEST_ASSERT( 0 == outlen );
|
|
|
|
|
|
|
|
TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
|
|
|
|
END_CASE
|
|
|
|
|
|
|
|
BEGIN_CASE
|
|
|
|
enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length:
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t first_length = {first_length};
|
|
|
|
size_t second_length = {second_length};
|
|
|
|
size_t length = first_length + second_length;
|
2011-01-06 16:37:30 +01:00
|
|
|
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];
|
|
|
|
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t outlen = 0;
|
|
|
|
size_t totaloutlen = 0;
|
|
|
|
size_t enclen = 0;
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
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_reset( &ctx_dec, iv ) );
|
|
|
|
TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) );
|
|
|
|
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
enclen = cipher_get_block_size(&ctx_enc )
|
2011-01-06 16:37:30 +01:00
|
|
|
* ( 1 + length / cipher_get_block_size( &ctx_enc ) );
|
2011-06-09 16:27:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enclen = length;
|
|
|
|
}
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
/* 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;
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( totaloutlen == enclen );
|
|
|
|
}
|
2011-01-06 16:37:30 +01:00
|
|
|
TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
|
|
|
|
totaloutlen += outlen;
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( outlen == cipher_get_block_size ( &ctx_enc ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( outlen == 0 );
|
|
|
|
}
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
/* decode the previously encoded string */
|
|
|
|
TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) );
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( enclen - cipher_get_block_size ( &ctx_enc ) == outlen );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( enclen == outlen );
|
|
|
|
}
|
2011-01-06 16:37:30 +01:00
|
|
|
TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
|
2011-06-09 16:27:58 +02:00
|
|
|
if( POLARSSL_MODE_CBC == cipher_info->mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( outlen == 0 );
|
|
|
|
}
|
2011-01-06 16:37:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
cipher_selftest:
|
|
|
|
{
|
|
|
|
TEST_ASSERT( cipher_self_test( 0 ) == 0 );
|
|
|
|
}
|
|
|
|
END_CASE
|