1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-08-08 17:42:09 +03:00

Add tests for CCM via cipher layer

This commit is contained in:
Manuel Pégourié-Gonnard
2014-05-15 16:03:07 +02:00
parent 2e5ee32033
commit 542eac5aba
4 changed files with 577 additions and 1 deletions

View File

@@ -411,6 +411,92 @@ void decrypt_test_vec( int cipher_id, int pad_mode,
}
/* END_CASE */
/* BEGIN_CASE depends_on:POLARSSL_CIPHER_MODE_AEAD */
void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
char *hex_ad, char *hex_cipher,
char *hex_tag, char *hex_clear )
{
int ret;
unsigned char key[50];
unsigned char iv[50];
unsigned char cipher[200];
unsigned char clear[200];
unsigned char ad[200];
unsigned char tag[20];
unsigned char my_tag[20];
size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
cipher_context_t ctx;
unsigned char output[200];
size_t outlen;
memset( key, 0x00, sizeof( key ) );
memset( iv, 0x00, sizeof( iv ) );
memset( cipher, 0x00, sizeof( cipher ) );
memset( clear, 0x00, sizeof( clear ) );
memset( ad, 0x00, sizeof( ad ) );
memset( tag, 0x00, sizeof( tag ) );
memset( my_tag, 0xFF, sizeof( my_tag ) );
memset( output, 0xFF, sizeof( output ) );
key_len = unhexify( key, hex_key );
iv_len = unhexify( iv, hex_iv );
cipher_len = unhexify( cipher, hex_cipher );
ad_len = unhexify( ad, hex_ad );
tag_len = unhexify( tag, hex_tag );
/* Prepare context */
TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
cipher_info_from_type( cipher_id ) ) );
TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
/* decode buffer and check tag */
ret = cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
cipher, cipher_len, output, &outlen,
tag, tag_len );
/* make sure we didn't overwrite */
TEST_ASSERT( output[outlen + 0] == 0xFF );
TEST_ASSERT( output[outlen + 1] == 0xFF );
/* make sure the message is rejected if it should be */
if( strcmp( hex_clear, "FAIL" ) == 0 )
{
TEST_ASSERT( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED );
goto cleanup;
}
/* otherwise, make sure it was decrypted properly */
TEST_ASSERT( ret == 0 );
clear_len = unhexify( clear, hex_clear );
TEST_ASSERT( outlen == clear_len );
TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
/* then encrypt the clear and make sure we get the same ciphertext and tag */
memset( output, 0xFF, sizeof( output ) );
outlen = 0;
ret = cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
clear, clear_len, output, &outlen,
my_tag, tag_len );
TEST_ASSERT( ret == 0 );
TEST_ASSERT( outlen == clear_len );
TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
/* make sure we didn't overwrite */
TEST_ASSERT( output[outlen + 0] == 0xFF );
TEST_ASSERT( output[outlen + 1] == 0xFF );
TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
cleanup:
cipher_free_ctx( &ctx );
}
/* END_CASE */
/* BEGIN_CASE */
void test_vec_ecb( int cipher_id, int operation, char *hex_key,
char *hex_input, char *hex_result,