mbedtls/tests/suites/test_suite_ccm.function

190 lines
5.1 KiB
Plaintext
Raw Normal View History

2014-05-02 13:17:29 +00:00
/* BEGIN_HEADER */
2015-03-09 17:05:11 +00:00
#include "mbedtls/ccm.h"
2014-05-02 13:17:29 +00:00
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_CCM_C
2014-05-02 13:17:29 +00:00
* END_DEPENDENCIES
*/
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
void mbedtls_ccm_self_test( )
2014-05-02 13:17:29 +00:00
{
TEST_ASSERT( mbedtls_ccm_self_test( 0 ) == 0 );
2014-05-02 13:17:29 +00:00
}
/* END_CASE */
2014-05-06 10:12:45 +00:00
/* BEGIN_CASE */
2015-04-28 16:02:54 +00:00
void mbedtls_ccm_setkey( int cipher_id, int key_size, int result )
2014-05-06 10:12:45 +00:00
{
mbedtls_ccm_context ctx;
2014-05-06 10:12:45 +00:00
unsigned char key[32];
int ret;
2015-04-28 16:02:54 +00:00
mbedtls_ccm_init( &ctx );
2014-05-06 10:12:45 +00:00
memset( key, 0x2A, sizeof( key ) );
TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );
2015-04-28 16:02:54 +00:00
ret = mbedtls_ccm_setkey( &ctx, cipher_id, key, key_size );
2014-05-06 10:12:45 +00:00
TEST_ASSERT( ret == result );
exit:
mbedtls_ccm_free( &ctx );
2014-05-06 10:12:45 +00:00
}
/* END_CASE */
2014-05-06 10:13:09 +00:00
/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
2014-05-06 16:07:24 +00:00
void ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res )
{
mbedtls_ccm_context ctx;
2014-05-06 16:07:24 +00:00
unsigned char key[16];
unsigned char msg[10];
unsigned char iv[14];
unsigned char add[10];
unsigned char out[10];
unsigned char tag[18];
int decrypt_ret;
2015-04-28 16:02:54 +00:00
mbedtls_ccm_init( &ctx );
2014-05-06 16:07:24 +00:00
memset( key, 0, sizeof( key ) );
memset( msg, 0, sizeof( msg ) );
memset( iv, 0, sizeof( iv ) );
memset( add, 0, sizeof( add ) );
memset( out, 0, sizeof( out ) );
memset( tag, 0, sizeof( tag ) );
2015-04-28 16:02:54 +00:00
TEST_ASSERT( mbedtls_ccm_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
2014-05-06 16:07:24 +00:00
key, 8 * sizeof( key ) ) == 0 );
TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
2014-05-06 16:07:24 +00:00
msg, out, tag, tag_len ) == res );
decrypt_ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
2014-05-06 16:07:24 +00:00
msg, out, tag, tag_len );
if( res == 0 )
TEST_ASSERT( decrypt_ret == MBEDTLS_ERR_CCM_AUTH_FAILED );
2014-05-06 16:07:24 +00:00
else
TEST_ASSERT( decrypt_ret == res );
exit:
mbedtls_ccm_free( &ctx );
2014-05-06 16:07:24 +00:00
}
/* END_CASE */
2014-05-06 10:13:09 +00:00
/* BEGIN_CASE */
void mbedtls_ccm_encrypt_and_tag( int cipher_id,
2014-05-06 10:13:09 +00:00
char *key_hex, char *msg_hex,
char *iv_hex, char *add_hex,
char *result_hex )
{
2014-05-06 16:19:06 +00:00
unsigned char key[32];
unsigned char msg[50];
unsigned char iv[13];
unsigned char add[32];
unsigned char result[50];
mbedtls_ccm_context ctx;
2014-05-06 10:13:09 +00:00
size_t key_len, msg_len, iv_len, add_len, tag_len, result_len;
2015-04-28 16:02:54 +00:00
mbedtls_ccm_init( &ctx );
2014-05-06 10:13:09 +00:00
memset( key, 0x00, sizeof( key ) );
memset( msg, 0x00, sizeof( msg ) );
memset( iv, 0x00, sizeof( iv ) );
memset( add, 0x00, sizeof( add ) );
memset( result, 0x00, sizeof( result ) );
key_len = unhexify( key, key_hex );
msg_len = unhexify( msg, msg_hex );
iv_len = unhexify( iv, iv_hex );
add_len = unhexify( add, add_hex );
result_len = unhexify( result, result_hex );
tag_len = result_len - msg_len;
2015-04-28 16:02:54 +00:00
TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
2014-05-06 10:13:09 +00:00
2014-05-07 12:43:46 +00:00
/* Test with input == output */
TEST_ASSERT( mbedtls_ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
2014-05-07 12:43:46 +00:00
msg, msg, msg + msg_len, tag_len ) == 0 );
2014-05-06 10:13:09 +00:00
2014-05-07 12:43:46 +00:00
TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
2014-05-06 10:13:09 +00:00
/* Check we didn't write past the end */
2014-05-07 12:43:46 +00:00
TEST_ASSERT( msg[result_len] == 0 && msg[result_len + 1] == 0 );
2014-05-06 10:13:09 +00:00
exit:
mbedtls_ccm_free( &ctx );
2014-05-06 10:13:09 +00:00
}
/* END_CASE */
2014-05-06 16:06:52 +00:00
/* BEGIN_CASE */
void mbedtls_ccm_auth_decrypt( int cipher_id,
2014-05-06 16:06:52 +00:00
char *key_hex, char *msg_hex,
char *iv_hex, char *add_hex,
int tag_len, char *result_hex )
{
2014-05-06 16:19:06 +00:00
unsigned char key[32];
unsigned char msg[50];
unsigned char iv[13];
unsigned char add[32];
2014-05-07 12:43:46 +00:00
unsigned char tag[16];
2014-05-06 16:19:06 +00:00
unsigned char result[50];
mbedtls_ccm_context ctx;
2014-05-06 16:06:52 +00:00
size_t key_len, msg_len, iv_len, add_len, result_len;
int ret;
2015-04-28 16:02:54 +00:00
mbedtls_ccm_init( &ctx );
2014-05-06 16:06:52 +00:00
memset( key, 0x00, sizeof( key ) );
memset( msg, 0x00, sizeof( msg ) );
memset( iv, 0x00, sizeof( iv ) );
memset( add, 0x00, sizeof( add ) );
2014-05-07 12:43:46 +00:00
memset( tag, 0x00, sizeof( tag ) );
2014-05-06 16:06:52 +00:00
memset( result, 0x00, sizeof( result ) );
key_len = unhexify( key, key_hex );
msg_len = unhexify( msg, msg_hex );
iv_len = unhexify( iv, iv_hex );
add_len = unhexify( add, add_hex );
msg_len -= tag_len;
2014-05-07 12:43:46 +00:00
memcpy( tag, msg + msg_len, tag_len );
2014-05-06 16:06:52 +00:00
if( strcmp( "FAIL", result_hex ) == 0 )
{
ret = MBEDTLS_ERR_CCM_AUTH_FAILED;
result_len = -1;
2014-05-06 16:06:52 +00:00
}
else
{
ret = 0;
result_len = unhexify( result, result_hex );
}
2015-04-28 16:02:54 +00:00
TEST_ASSERT( mbedtls_ccm_setkey( &ctx, cipher_id, key, key_len * 8 ) == 0 );
2014-05-06 16:06:52 +00:00
2014-05-07 12:43:46 +00:00
/* Test with input == output */
TEST_ASSERT( mbedtls_ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
2014-05-07 12:43:46 +00:00
msg, msg, msg + msg_len, tag_len ) == ret );
2014-05-06 16:06:52 +00:00
if( ret == 0 )
{
2014-05-07 12:43:46 +00:00
TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
2014-05-06 16:06:52 +00:00
}
else
{
size_t i;
for( i = 0; i < msg_len; i++ )
2014-05-07 12:43:46 +00:00
TEST_ASSERT( msg[i] == 0 );
2014-05-06 16:06:52 +00:00
}
2014-05-07 12:43:46 +00:00
/* Check we didn't write past the end (where the original tag is) */
TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 );
2014-05-06 16:06:52 +00:00
exit:
mbedtls_ccm_free( &ctx );
2014-05-06 16:06:52 +00:00
}
/* END_CASE */