2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/cipher.h"
|
2013-09-03 18:17:35 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/gcm.h"
|
2013-09-03 18:17:35 +00:00
|
|
|
#endif
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_HEADER */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 10:49:31 +00:00
|
|
|
* depends_on:MBEDTLS_CIPHER_C
|
2013-08-20 09:48:36 +00:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 13:16:06 +00:00
|
|
|
|
2014-03-29 15:10:55 +00:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_cipher_list( )
|
2014-03-29 15:10:55 +00:00
|
|
|
{
|
|
|
|
const int *cipher_type;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
|
|
|
|
TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
|
2014-03-29 15:10:55 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2014-06-13 14:08:07 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void cipher_null_args( )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_context_t ctx;
|
|
|
|
const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type( *( mbedtls_cipher_list() ) );
|
2014-06-13 14:08:07 +00:00
|
|
|
unsigned char buf[1] = { 0 };
|
|
|
|
size_t olen;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_get_block_size( NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_get_block_size( &ctx ) == 0 );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_get_cipher_mode( NULL ) == MBEDTLS_MODE_NONE );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &ctx ) == MBEDTLS_MODE_NONE );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_get_iv_size( NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_get_iv_size( &ctx ) == 0 );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_info_from_string( NULL ) == NULL );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_setup( &ctx, NULL )
|
2015-04-08 10:49:31 +00:00
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_setup( NULL, info )
|
2015-04-08 10:49:31 +00:00
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_setkey( NULL, buf, 0, MBEDTLS_ENCRYPT )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_setkey( &ctx, buf, 0, MBEDTLS_ENCRYPT )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_set_iv( NULL, buf, 0 )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, buf, 0 )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_reset( NULL ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_reset( &ctx ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2014-06-13 14:08:07 +00:00
|
|
|
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_update_ad( NULL, buf, 0 )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_update_ad( &ctx, buf, 0 )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2014-06-13 14:08:07 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_update( NULL, buf, 0, buf, &olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_update( &ctx, buf, 0, buf, &olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_finish( NULL, buf, &olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_finish( &ctx, buf, &olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_write_tag( NULL, buf, olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_write_tag( &ctx, buf, olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_cipher_check_tag( NULL, buf, olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
TEST_ASSERT( mbedtls_cipher_check_tag( &ctx, buf, olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
2014-06-13 14:08:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2016-07-14 12:46:10 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
|
|
|
|
void cipher_special_behaviours( )
|
|
|
|
{
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_context_t ctx;
|
|
|
|
unsigned char input[32];
|
|
|
|
unsigned char output[32];
|
|
|
|
unsigned char iv[32];
|
|
|
|
size_t olen = 0;
|
|
|
|
|
|
|
|
mbedtls_cipher_init( &ctx );
|
|
|
|
memset( input, 0, sizeof( input ) );
|
|
|
|
memset( output, 0, sizeof( output ) );
|
|
|
|
memset( iv, 0, sizeof( iv ) );
|
|
|
|
|
|
|
|
/* Check and get info structures */
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
|
|
|
|
TEST_ASSERT( NULL != cipher_info );
|
|
|
|
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
|
|
|
|
|
|
|
|
/* IV too big */
|
|
|
|
TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
|
|
|
|
== MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
|
|
|
|
|
|
|
|
/* IV too small */
|
|
|
|
TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
|
|
|
|
== MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
/* Update ECB with partial block */
|
|
|
|
TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
|
|
|
|
== MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_cipher_free( &ctx );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
|
|
|
|
int length_val, int pad_mode )
|
2013-08-16 11:38:47 +00:00
|
|
|
{
|
2015-06-23 23:08:09 +00:00
|
|
|
size_t length = length_val, outlen, total_len, i, block_size;
|
2011-01-06 15:37:30 +00:00
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char iv[16];
|
2013-08-31 15:31:03 +00:00
|
|
|
unsigned char ad[13];
|
|
|
|
unsigned char tag[16];
|
2011-01-06 15:37:30 +00:00
|
|
|
unsigned char inbuf[64];
|
|
|
|
unsigned char encbuf[64];
|
|
|
|
unsigned char decbuf[64];
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_context_t ctx_dec;
|
|
|
|
mbedtls_cipher_context_t ctx_enc;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-09-05 08:30:32 +00:00
|
|
|
/*
|
|
|
|
* Prepare contexts
|
|
|
|
*/
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx_dec );
|
|
|
|
mbedtls_cipher_init( &ctx_enc );
|
2013-09-05 08:30:32 +00:00
|
|
|
|
|
|
|
memset( key, 0x2a, sizeof( key ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
/* Check and get info structures */
|
2015-04-08 10:49:31 +00:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_id );
|
2011-01-06 15:37:30 +00:00
|
|
|
TEST_ASSERT( NULL != cipher_info );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
/* Initialise enc and dec contexts */
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
|
2013-09-05 08:30:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
2013-08-20 09:48:36 +00:00
|
|
|
if( -1 != pad_mode )
|
2013-07-26 11:20:42 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
|
2013-07-26 11:20:42 +00:00
|
|
|
}
|
2013-09-13 12:41:45 +00:00
|
|
|
#else
|
|
|
|
(void) pad_mode;
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
2013-07-26 11:20:42 +00:00
|
|
|
|
2013-09-05 08:30:32 +00:00
|
|
|
/*
|
|
|
|
* Do a few encode/decode cycles
|
|
|
|
*/
|
|
|
|
for( i = 0; i < 3; i++ )
|
|
|
|
{
|
|
|
|
memset( iv , 0x00 + i, sizeof( iv ) );
|
|
|
|
memset( ad, 0x10 + i, sizeof( ad ) );
|
|
|
|
memset( inbuf, 0x20 + i, sizeof( inbuf ) );
|
|
|
|
|
|
|
|
memset( encbuf, 0, sizeof( encbuf ) );
|
|
|
|
memset( decbuf, 0, sizeof( decbuf ) );
|
|
|
|
memset( tag, 0, sizeof( tag ) );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
|
2013-09-03 11:04:44 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
|
2013-09-03 11:54:12 +00:00
|
|
|
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-06-23 23:08:09 +00:00
|
|
|
block_size = mbedtls_cipher_get_block_size( &ctx_enc );
|
|
|
|
TEST_ASSERT( block_size != 0 );
|
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
/* encode length number of bytes from inbuf */
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
|
2013-07-25 13:26:54 +00:00
|
|
|
total_len = outlen;
|
|
|
|
|
|
|
|
TEST_ASSERT( total_len == length ||
|
2015-06-23 23:08:09 +00:00
|
|
|
( total_len % block_size == 0 &&
|
2013-07-25 13:26:54 +00:00
|
|
|
total_len < length &&
|
2015-06-23 23:08:09 +00:00
|
|
|
total_len + block_size > length ) );
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
|
2013-07-25 13:26:54 +00:00
|
|
|
total_len += outlen;
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-07-25 13:26:54 +00:00
|
|
|
TEST_ASSERT( total_len == length ||
|
2015-06-23 23:08:09 +00:00
|
|
|
( total_len % block_size == 0 &&
|
2013-07-25 13:26:54 +00:00
|
|
|
total_len > length &&
|
2015-06-23 23:08:09 +00:00
|
|
|
total_len <= length + block_size ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
/* decode the previously encoded string */
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
|
2013-07-25 13:26:54 +00:00
|
|
|
total_len = outlen;
|
|
|
|
|
|
|
|
TEST_ASSERT( total_len == length ||
|
2015-06-23 23:08:09 +00:00
|
|
|
( total_len % block_size == 0 &&
|
2013-07-25 13:26:54 +00:00
|
|
|
total_len < length &&
|
2015-06-23 23:08:09 +00:00
|
|
|
total_len + block_size >= length ) );
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
|
2013-07-25 13:26:54 +00:00
|
|
|
total_len += outlen;
|
2011-06-09 14:27:58 +00:00
|
|
|
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif
|
2013-09-03 14:19:22 +00:00
|
|
|
|
2013-09-05 08:30:32 +00:00
|
|
|
/* check result */
|
2013-07-25 13:26:54 +00:00
|
|
|
TEST_ASSERT( total_len == length );
|
2011-01-06 15:37:30 +00:00
|
|
|
TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
|
2013-09-05 08:30:32 +00:00
|
|
|
}
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-09-05 08:30:32 +00:00
|
|
|
/*
|
|
|
|
* Done
|
|
|
|
*/
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_free( &ctx_dec );
|
|
|
|
mbedtls_cipher_free( &ctx_enc );
|
2013-08-16 11:38:47 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void enc_fail( int cipher_id, int pad_mode, int key_len,
|
|
|
|
int length_val, int ret )
|
2013-08-16 11:38:47 +00:00
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
size_t length = length_val;
|
2013-07-26 14:50:44 +00:00
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char iv[16];
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_context_t ctx;
|
2013-07-26 14:50:44 +00:00
|
|
|
|
|
|
|
unsigned char inbuf[64];
|
|
|
|
unsigned char encbuf[64];
|
|
|
|
|
|
|
|
size_t outlen = 0;
|
|
|
|
|
|
|
|
memset( key, 0, 32 );
|
|
|
|
memset( iv , 0, 16 );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx );
|
2013-07-26 14:50:44 +00:00
|
|
|
|
|
|
|
memset( inbuf, 5, 64 );
|
|
|
|
memset( encbuf, 0, 64 );
|
|
|
|
|
|
|
|
/* Check and get info structures */
|
2015-04-08 10:49:31 +00:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_id );
|
2013-07-26 14:50:44 +00:00
|
|
|
TEST_ASSERT( NULL != cipher_info );
|
|
|
|
|
|
|
|
/* Initialise context */
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
|
2013-09-13 12:41:45 +00:00
|
|
|
#else
|
|
|
|
(void) pad_mode;
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif
|
2013-07-26 14:50:44 +00:00
|
|
|
|
|
|
|
/* encode length number of bytes from inbuf */
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
|
|
|
|
TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
|
2013-07-26 14:50:44 +00:00
|
|
|
|
|
|
|
/* done */
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_free( &ctx );
|
2013-08-16 11:38:47 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2013-07-26 14:50:44 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void dec_empty_buf()
|
2013-08-16 11:38:47 +00:00
|
|
|
{
|
2011-01-06 15:37:30 +00:00
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char iv[16];
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_context_t ctx_dec;
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
unsigned char encbuf[64];
|
|
|
|
unsigned char decbuf[64];
|
|
|
|
|
2011-04-24 15:53:29 +00:00
|
|
|
size_t outlen = 0;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
memset( key, 0, 32 );
|
|
|
|
memset( iv , 0, 16 );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx_dec );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
memset( encbuf, 0, 64 );
|
|
|
|
memset( decbuf, 0, 64 );
|
|
|
|
|
2013-09-03 11:04:44 +00:00
|
|
|
/* Initialise context */
|
2015-04-08 10:49:31 +00:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
|
2011-01-06 15:37:30 +00:00
|
|
|
TEST_ASSERT( NULL != cipher_info);
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
|
2013-09-03 11:04:44 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
|
2013-09-03 11:54:12 +00:00
|
|
|
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
/* decode 0-byte string */
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
TEST_ASSERT( 0 == outlen );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED == mbedtls_cipher_finish(
|
2013-09-03 14:19:22 +00:00
|
|
|
&ctx_dec, decbuf + outlen, &outlen ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
TEST_ASSERT( 0 == outlen );
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_free( &ctx_dec );
|
2013-08-16 11:38:47 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
|
2018-03-28 02:16:17 +00:00
|
|
|
int second_length_val, int pad_mode,
|
|
|
|
int first_encrypt_output_len, int second_encrypt_output_len,
|
|
|
|
int first_decrypt_output_len, int second_decrypt_output_len )
|
2013-08-16 11:38:47 +00:00
|
|
|
{
|
2013-08-20 09:48:36 +00:00
|
|
|
size_t first_length = first_length_val;
|
|
|
|
size_t second_length = second_length_val;
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t length = first_length + second_length;
|
2015-06-23 23:08:09 +00:00
|
|
|
size_t block_size;
|
2011-01-06 15:37:30 +00:00
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char iv[16];
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_context_t ctx_dec;
|
|
|
|
mbedtls_cipher_context_t ctx_enc;
|
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
unsigned char inbuf[64];
|
|
|
|
unsigned char encbuf[64];
|
|
|
|
unsigned char decbuf[64];
|
|
|
|
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t outlen = 0;
|
|
|
|
size_t totaloutlen = 0;
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
memset( key, 0, 32 );
|
|
|
|
memset( iv , 0, 16 );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx_dec );
|
|
|
|
mbedtls_cipher_init( &ctx_enc );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
memset( inbuf, 5, 64 );
|
|
|
|
memset( encbuf, 0, 64 );
|
|
|
|
memset( decbuf, 0, 64 );
|
|
|
|
|
|
|
|
/* Initialise enc and dec contexts */
|
2015-04-08 10:49:31 +00:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_id );
|
2011-01-06 15:37:30 +00:00
|
|
|
TEST_ASSERT( NULL != cipher_info);
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2018-03-28 02:16:17 +00:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
|
|
|
if( -1 != pad_mode )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) pad_mode;
|
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
|
2013-09-03 11:04:44 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
|
2013-09-03 11:54:12 +00:00
|
|
|
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-06-23 23:08:09 +00:00
|
|
|
block_size = mbedtls_cipher_get_block_size( &ctx_enc );
|
|
|
|
TEST_ASSERT( block_size != 0 );
|
|
|
|
|
2011-01-06 15:37:30 +00:00
|
|
|
/* encode length number of bytes from inbuf */
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
|
2018-03-28 02:16:17 +00:00
|
|
|
TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
|
2011-01-06 15:37:30 +00:00
|
|
|
totaloutlen = outlen;
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
|
2018-03-28 02:16:17 +00:00
|
|
|
TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
|
2011-01-06 15:37:30 +00:00
|
|
|
totaloutlen += outlen;
|
2013-07-25 13:26:54 +00:00
|
|
|
TEST_ASSERT( totaloutlen == length ||
|
2015-06-23 23:08:09 +00:00
|
|
|
( totaloutlen % block_size == 0 &&
|
2013-07-25 13:26:54 +00:00
|
|
|
totaloutlen < length &&
|
2015-06-23 23:08:09 +00:00
|
|
|
totaloutlen + block_size > length ) );
|
2013-07-25 13:26:54 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
totaloutlen += outlen;
|
2013-07-25 13:26:54 +00:00
|
|
|
TEST_ASSERT( totaloutlen == length ||
|
2015-06-23 23:08:09 +00:00
|
|
|
( totaloutlen % block_size == 0 &&
|
2013-07-25 13:26:54 +00:00
|
|
|
totaloutlen > length &&
|
2015-06-23 23:08:09 +00:00
|
|
|
totaloutlen <= length + block_size ) );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
/* decode the previously encoded string */
|
2018-03-28 02:16:17 +00:00
|
|
|
second_length = totaloutlen - first_length;
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
|
|
|
|
TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
|
2013-07-25 13:26:54 +00:00
|
|
|
totaloutlen = outlen;
|
2018-03-28 02:16:17 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
|
|
|
|
TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
|
|
|
|
totaloutlen += outlen;
|
2013-07-25 13:26:54 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( totaloutlen == length ||
|
2015-06-23 23:08:09 +00:00
|
|
|
( totaloutlen % block_size == 0 &&
|
2013-07-25 13:26:54 +00:00
|
|
|
totaloutlen < length &&
|
2015-06-23 23:08:09 +00:00
|
|
|
totaloutlen + block_size >= length ) );
|
2013-07-25 13:26:54 +00:00
|
|
|
|
2018-03-28 02:16:17 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
|
2013-07-25 13:26:54 +00:00
|
|
|
totaloutlen += outlen;
|
|
|
|
|
|
|
|
TEST_ASSERT( totaloutlen == length );
|
2011-01-06 15:37:30 +00:00
|
|
|
|
|
|
|
TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_free( &ctx_dec );
|
|
|
|
mbedtls_cipher_free( &ctx_enc );
|
2013-08-16 11:38:47 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2013-09-03 16:31:25 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void decrypt_test_vec( int cipher_id, int pad_mode,
|
|
|
|
char *hex_key, char *hex_iv,
|
|
|
|
char *hex_cipher, char *hex_clear,
|
|
|
|
char *hex_ad, char *hex_tag,
|
|
|
|
int finish_result, int tag_result )
|
|
|
|
{
|
2013-09-03 18:17:35 +00:00
|
|
|
unsigned char key[50];
|
|
|
|
unsigned char iv[50];
|
|
|
|
unsigned char cipher[200];
|
|
|
|
unsigned char clear[200];
|
|
|
|
unsigned char ad[200];
|
|
|
|
unsigned char tag[20];
|
2013-09-20 09:29:59 +00:00
|
|
|
size_t key_len, iv_len, cipher_len, clear_len;
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2013-09-20 09:29:59 +00:00
|
|
|
size_t ad_len, tag_len;
|
|
|
|
#endif
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_context_t ctx;
|
2013-09-03 18:17:35 +00:00
|
|
|
unsigned char output[200];
|
2013-09-03 16:31:25 +00:00
|
|
|
size_t outlen, total_len;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2013-09-03 16:31:25 +00:00
|
|
|
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( output, 0x00, sizeof( output ) );
|
|
|
|
|
|
|
|
key_len = unhexify( key, hex_key );
|
|
|
|
iv_len = unhexify( iv, hex_iv );
|
|
|
|
cipher_len = unhexify( cipher, hex_cipher );
|
|
|
|
clear_len = unhexify( clear, hex_clear );
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2013-09-03 16:31:25 +00:00
|
|
|
ad_len = unhexify( ad, hex_ad );
|
|
|
|
tag_len = unhexify( tag, hex_tag );
|
2013-09-20 09:29:59 +00:00
|
|
|
#else
|
|
|
|
((void) hex_ad);
|
|
|
|
((void) hex_tag);
|
|
|
|
#endif
|
2013-09-03 16:31:25 +00:00
|
|
|
|
|
|
|
/* Prepare context */
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_info_from_type( cipher_id ) ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
2013-09-03 16:31:25 +00:00
|
|
|
if( pad_mode != -1 )
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
|
2013-09-13 12:41:45 +00:00
|
|
|
#else
|
|
|
|
(void) pad_mode;
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, iv_len ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad, ad_len ) );
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif
|
2013-09-03 16:31:25 +00:00
|
|
|
|
|
|
|
/* decode buffer and check tag */
|
|
|
|
total_len = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
|
2013-09-03 16:31:25 +00:00
|
|
|
total_len += outlen;
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
|
2013-09-03 16:31:25 +00:00
|
|
|
&outlen ) );
|
|
|
|
total_len += outlen;
|
2016-05-18 15:04:41 +00:00
|
|
|
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_AEAD_CHACHA20_POLY1305_C)
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag, tag_len ) );
|
2014-06-24 13:26:28 +00:00
|
|
|
#endif
|
2013-09-03 16:31:25 +00:00
|
|
|
|
|
|
|
/* check plaintext only if everything went fine */
|
|
|
|
if( 0 == finish_result && 0 == tag_result )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( total_len == clear_len );
|
|
|
|
TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
|
|
|
|
}
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_free( &ctx );
|
2013-09-03 16:31:25 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
|
2014-05-15 14:03:07 +00:00
|
|
|
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;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_context_t ctx;
|
2014-05-15 14:03:07 +00:00
|
|
|
unsigned char output[200];
|
|
|
|
size_t outlen;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2014-05-15 14:03:07 +00:00
|
|
|
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 */
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_info_from_type( cipher_id ) ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
|
2014-05-15 14:03:07 +00:00
|
|
|
|
|
|
|
/* decode buffer and check tag */
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
|
2014-05-15 14:03:07 +00:00
|
|
|
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 )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
|
2014-07-10 13:26:12 +00:00
|
|
|
goto exit;
|
2014-05-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
|
2014-05-15 14:03:07 +00:00
|
|
|
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 );
|
|
|
|
|
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_free( &ctx );
|
2014-05-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-09-08 21:04:04 +00:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void test_vec_ecb( int cipher_id, int operation, char *hex_key,
|
|
|
|
char *hex_input, char *hex_result,
|
|
|
|
int finish_result )
|
|
|
|
{
|
|
|
|
unsigned char key[50];
|
|
|
|
unsigned char input[16];
|
|
|
|
unsigned char result[16];
|
|
|
|
size_t key_len;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_context_t ctx;
|
2013-09-08 21:04:04 +00:00
|
|
|
unsigned char output[32];
|
|
|
|
size_t outlen;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2013-09-08 21:04:04 +00:00
|
|
|
memset( key, 0x00, sizeof( key ) );
|
|
|
|
memset( input, 0x00, sizeof( input ) );
|
|
|
|
memset( result, 0x00, sizeof( result ) );
|
|
|
|
memset( output, 0x00, sizeof( output ) );
|
|
|
|
|
|
|
|
/* Prepare context */
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_info_from_type( cipher_id ) ) );
|
2013-09-08 21:04:04 +00:00
|
|
|
|
|
|
|
key_len = unhexify( key, hex_key );
|
|
|
|
TEST_ASSERT( unhexify( input, hex_input ) ==
|
2015-04-08 10:49:31 +00:00
|
|
|
(int) mbedtls_cipher_get_block_size( &ctx ) );
|
2013-09-08 21:04:04 +00:00
|
|
|
TEST_ASSERT( unhexify( result, hex_result ) ==
|
2015-04-08 10:49:31 +00:00
|
|
|
(int) mbedtls_cipher_get_block_size( &ctx ) );
|
2013-09-08 21:04:04 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
|
2013-09-08 21:04:04 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input,
|
|
|
|
mbedtls_cipher_get_block_size( &ctx ),
|
2013-09-08 21:04:04 +00:00
|
|
|
output, &outlen ) );
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
|
|
|
|
TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
|
2013-09-08 21:04:04 +00:00
|
|
|
&outlen ) );
|
|
|
|
TEST_ASSERT( 0 == outlen );
|
|
|
|
|
|
|
|
/* check plaintext only if everything went fine */
|
|
|
|
if( 0 == finish_result )
|
|
|
|
TEST_ASSERT( 0 == memcmp( output, result,
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_get_block_size( &ctx ) ) );
|
2013-09-08 21:04:04 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_free( &ctx );
|
2013-09-08 21:04:04 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
2013-08-20 09:48:36 +00:00
|
|
|
void set_padding( int cipher_id, int pad_mode, int ret )
|
2013-08-16 11:38:47 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
mbedtls_cipher_context_t ctx;
|
2013-07-24 16:05:00 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx );
|
2014-07-01 13:45:49 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_id );
|
2013-07-24 16:05:00 +00:00
|
|
|
TEST_ASSERT( NULL != cipher_info );
|
2015-05-14 11:51:45 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
|
2013-07-24 16:05:00 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
|
2013-07-24 16:05:00 +00:00
|
|
|
|
2014-07-10 13:26:12 +00:00
|
|
|
exit:
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_free( &ctx );
|
2013-08-16 11:38:47 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|
2011-01-06 15:37:30 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
|
2013-08-20 09:48:36 +00:00
|
|
|
void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
|
2013-08-16 11:38:47 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_info_t cipher_info;
|
|
|
|
mbedtls_cipher_context_t ctx;
|
2013-07-26 08:55:02 +00:00
|
|
|
unsigned char input[16];
|
|
|
|
size_t ilen, dlen;
|
|
|
|
|
|
|
|
/* build a fake context just for getting access to get_padding */
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_cipher_init( &ctx );
|
|
|
|
cipher_info.mode = MBEDTLS_MODE_CBC;
|
2013-07-26 08:55:02 +00:00
|
|
|
ctx.cipher_info = &cipher_info;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
|
2013-07-26 08:55:02 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
ilen = unhexify( input, input_str );
|
2013-07-26 08:55:02 +00:00
|
|
|
|
2013-08-20 09:48:36 +00:00
|
|
|
TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
|
|
|
|
if( 0 == ret )
|
|
|
|
TEST_ASSERT( dlen == (size_t) dlen_check );
|
2013-08-16 11:38:47 +00:00
|
|
|
}
|
2013-08-20 09:48:36 +00:00
|
|
|
/* END_CASE */
|