mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2026-01-03 21:25:31 +01:00
Combine hex parameters in a struct
This commit is contained in:
parent
5cfc06832e
commit
d30ca130e8
36 changed files with 756 additions and 1014 deletions
|
|
@ -116,34 +116,31 @@ exit:
|
|||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mbedtls_ccm_encrypt_and_tag( int cipher_id, uint8_t * key,
|
||||
uint32_t key_len, uint8_t * msg,
|
||||
uint32_t msg_len, uint8_t * iv,
|
||||
uint32_t iv_len, uint8_t * add,
|
||||
uint32_t add_len, uint8_t * result,
|
||||
uint32_t result_len )
|
||||
void mbedtls_ccm_encrypt_and_tag( int cipher_id, HexParam_t * key,
|
||||
HexParam_t * msg, HexParam_t * iv,
|
||||
HexParam_t * add, HexParam_t * result )
|
||||
{
|
||||
mbedtls_ccm_context ctx;
|
||||
size_t tag_len;
|
||||
uint8_t * msg_n_tag = (uint8_t *)malloc( result_len + 2 );
|
||||
uint8_t * msg_n_tag = (uint8_t *)malloc( result->len + 2 );
|
||||
|
||||
mbedtls_ccm_init( &ctx );
|
||||
|
||||
memset( msg_n_tag, 0, result_len + 2 );
|
||||
memcpy( msg_n_tag, msg, msg_len );
|
||||
memset( msg_n_tag, 0, result->len + 2 );
|
||||
memcpy( msg_n_tag, msg->x, msg->len );
|
||||
|
||||
tag_len = result_len - msg_len;
|
||||
tag_len = result->len - msg->len;
|
||||
|
||||
TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 );
|
||||
|
||||
/* Test with input == output */
|
||||
TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
|
||||
msg_n_tag, msg_n_tag, msg_n_tag + msg_len, tag_len ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg->len, iv->x, iv->len, add->x, add->len,
|
||||
msg_n_tag, msg_n_tag, msg_n_tag + msg->len, tag_len ) == 0 );
|
||||
|
||||
TEST_ASSERT( memcmp( msg_n_tag, result, result_len ) == 0 );
|
||||
TEST_ASSERT( memcmp( msg_n_tag, result->x, result->len ) == 0 );
|
||||
|
||||
/* Check we didn't write past the end */
|
||||
TEST_ASSERT( msg_n_tag[result_len] == 0 && msg_n_tag[result_len + 1] == 0 );
|
||||
TEST_ASSERT( msg_n_tag[result->len] == 0 && msg_n_tag[result->len + 1] == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_ccm_free( &ctx );
|
||||
|
|
@ -152,12 +149,10 @@ exit:
|
|||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mbedtls_ccm_auth_decrypt( int cipher_id, uint8_t * key, uint32_t key_len,
|
||||
uint8_t * msg, uint32_t msg_len, uint8_t * iv,
|
||||
uint32_t iv_len, uint8_t * add,
|
||||
uint32_t add_len, int tag_len,
|
||||
char * result, uint8_t * hex_msg,
|
||||
uint32_t hex_msg_len )
|
||||
void mbedtls_ccm_auth_decrypt( int cipher_id, HexParam_t * key,
|
||||
HexParam_t * msg, HexParam_t * iv,
|
||||
HexParam_t * add, int tag_len, char * result,
|
||||
HexParam_t * hex_msg )
|
||||
{
|
||||
unsigned char tag[16];
|
||||
mbedtls_ccm_context ctx;
|
||||
|
|
@ -167,8 +162,8 @@ void mbedtls_ccm_auth_decrypt( int cipher_id, uint8_t * key, uint32_t key_len,
|
|||
|
||||
memset( tag, 0x00, sizeof( tag ) );
|
||||
|
||||
msg_len -= tag_len;
|
||||
memcpy( tag, msg + msg_len, tag_len );
|
||||
msg->len -= tag_len;
|
||||
memcpy( tag, msg->x + msg->len, tag_len );
|
||||
|
||||
if( strcmp( "FAIL", result ) == 0 )
|
||||
{
|
||||
|
|
@ -179,26 +174,26 @@ void mbedtls_ccm_auth_decrypt( int cipher_id, uint8_t * key, uint32_t key_len,
|
|||
ret = 0;
|
||||
}
|
||||
|
||||
TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key->x, key->len * 8 ) == 0 );
|
||||
|
||||
/* Test with input == output */
|
||||
TEST_ASSERT( mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
|
||||
msg, msg, msg + msg_len, tag_len ) == ret );
|
||||
TEST_ASSERT( mbedtls_ccm_auth_decrypt( &ctx, msg->len, iv->x, iv->len, add->x, add->len,
|
||||
msg->x, msg->x, msg->x + msg->len, tag_len ) == ret );
|
||||
|
||||
if( ret == 0 )
|
||||
{
|
||||
TEST_ASSERT( memcmp( msg, hex_msg, hex_msg_len ) == 0 );
|
||||
TEST_ASSERT( memcmp( msg->x, hex_msg->x, hex_msg->len ) == 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for( i = 0; i < msg_len; i++ )
|
||||
TEST_ASSERT( msg[i] == 0 );
|
||||
for( i = 0; i < msg->len; i++ )
|
||||
TEST_ASSERT( msg->x[i] == 0 );
|
||||
}
|
||||
|
||||
/* Check we didn't write past the end (where the original tag is) */
|
||||
TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 );
|
||||
TEST_ASSERT( memcmp( msg->x + msg->len, tag, tag_len ) == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_ccm_free( &ctx );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue