diff --git a/Makefile b/Makefile index 7f334ee27310..39fc342bfc69 100644 --- a/Makefile +++ b/Makefile @@ -73,7 +73,7 @@ clean: $(MAKE) -C programs clean $(MAKE) -C tests clean ifndef WINDOWS - find . \( -name \*.gcno -o -name \*.gcda -o -name *.info \) -exec rm {} + + find . \( -name \*.gcno -o -name \*.gcda -o -name \*.info \) -exec rm {} + endif check: lib diff --git a/library/asn1write.c b/library/asn1write.c index 027c858e7a9b..69b61b205f13 100644 --- a/library/asn1write.c +++ b/library/asn1write.c @@ -60,16 +60,43 @@ int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len return( 2 ); } - if( *p - start < 3 ) - return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); + if( len <= 0xFFFF ) + { + if( *p - start < 3 ) + return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); - // We assume we never have lengths larger than 65535 bytes - // - *--(*p) = len % 256; - *--(*p) = ( len / 256 ) % 256; - *--(*p) = 0x82; + *--(*p) = ( len ) & 0xFF; + *--(*p) = ( len >> 8 ) & 0xFF; + *--(*p) = 0x82; + return( 3 ); + } + + if( len <= 0xFFFFFF ) + { + if( *p - start < 4 ) + return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); + + *--(*p) = ( len ) & 0xFF; + *--(*p) = ( len >> 8 ) & 0xFF; + *--(*p) = ( len >> 16 ) & 0xFF; + *--(*p) = 0x83; + return( 4 ); + } + + if( len <= 0xFFFFFFFF ) + { + if( *p - start < 5 ) + return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); + + *--(*p) = ( len ) & 0xFF; + *--(*p) = ( len >> 8 ) & 0xFF; + *--(*p) = ( len >> 16 ) & 0xFF; + *--(*p) = ( len >> 24 ) & 0xFF; + *--(*p) = 0x84; + return( 5 ); + } - return( 3 ); + return( MBEDTLS_ERR_ASN1_INVALID_LENGTH ); } int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag ) diff --git a/library/entropy.c b/library/entropy.c index 45c894b1df6d..8040638d0c01 100644 --- a/library/entropy.c +++ b/library/entropy.c @@ -42,6 +42,10 @@ #include #endif +#if defined(MBEDTLS_ENTROPY_NV_SEED) +#include "mbedtls/platform.h" +#endif + #if defined(MBEDTLS_SELF_TEST) #if defined(MBEDTLS_PLATFORM_C) #include "mbedtls/platform.h" diff --git a/programs/test/selftest.c b/programs/test/selftest.c index 6923453251bd..89c66169e584 100644 --- a/programs/test/selftest.c +++ b/programs/test/selftest.c @@ -106,8 +106,8 @@ static int run_test_snprintf( void ) * self-test. If this fails, we attempt the test anyway, so no error is passed * back. */ -#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_ENTROPY_NV_SEED) && \ - !defined(MBEDTLS_NO_PLATFORM_ENTROPY) +#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C) && \ + defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY) static void create_entropy_seed_file( void ) { int result; @@ -138,7 +138,7 @@ static void create_entropy_seed_file( void ) int main( int argc, char *argv[] ) { int v, suites_tested = 0, suites_failed = 0; -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) +#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST) unsigned char buf[1000000]; #endif void *pointer; @@ -396,10 +396,6 @@ int main( int argc, char *argv[] ) suites_tested++; #endif -#else - mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" ); -#endif - if( v != 0 ) { #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG) @@ -416,6 +412,10 @@ int main( int argc, char *argv[] ) suites_tested++; #endif +#else + mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" ); +#endif + if( v != 0 ) { mbedtls_printf( " Executed %d test suites\n\n", suites_tested ); diff --git a/tests/suites/test_suite_asn1write.data b/tests/suites/test_suite_asn1write.data index 61934f556814..c2a78b1afd42 100644 --- a/tests/suites/test_suite_asn1write.data +++ b/tests/suites/test_suite_asn1write.data @@ -48,3 +48,45 @@ mbedtls_asn1_write_ia5_string:"ABC":"":3:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ASN.1 Write IA5 String #5 (Buffer too small for string) mbedtls_asn1_write_ia5_string:"ABC":"":2:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write / Read Length #0 (Len = 0, short form) +mbedtls_asn1_write_len:0:"00":1:1 + +ASN.1 Write / Read Length #1 (Len = 127, short form) +mbedtls_asn1_write_len:127:"7F":1:1 + +ASN.1 Write / Read Length #2 (Len = 127, buffer too small) +mbedtls_asn1_write_len:127:"7F":0:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write / Read Length #3 (Len = 128, long form) +mbedtls_asn1_write_len:128:"8180":2:2 + +ASN.1 Write / Read Length #4 (Len = 255, long form) +mbedtls_asn1_write_len:255:"81FF":2:2 + +ASN.1 Write / Read Length #5 (Len = 255, buffer too small) +mbedtls_asn1_write_len:255:"81FF":1:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write / Read Length #6 (Len = 258, byte order) +mbedtls_asn1_write_len:258:"820102":3:3 + +ASN.1 Write / Read Length #7 (Len = 65535, long form) +mbedtls_asn1_write_len:65535:"82FFFF":3:3 + +ASN.1 Write / Read Length #8 (Len = 65535, buffer too small) +mbedtls_asn1_write_len:65535:"82FFFF":2:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write / Read Length #9 (Len = 66051, byte order) +mbedtls_asn1_write_len:66051:"83010203":4:4 + +ASN.1 Write / Read Length #10 (Len = 16777215, long form) +mbedtls_asn1_write_len:16777215:"83FFFFFF":4:4 + +ASN.1 Write / Read Length #11 (Len = 16777215, buffer too small) +mbedtls_asn1_write_len:16777215:"83FFFFFF":3:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL + +ASN.1 Write / Read Length #12 (Len = 16909060, byte order) +mbedtls_asn1_write_len:16909060:"8401020304":5:5 + +ASN.1 Write / Read Length #12 (Len = 16909060, buffer too small) +mbedtls_asn1_write_len:16909060:"8401020304":4:MBEDTLS_ERR_ASN1_BUF_TOO_SMALL diff --git a/tests/suites/test_suite_asn1write.function b/tests/suites/test_suite_asn1write.function index a15c8216dced..fc5fd8a2ebba 100644 --- a/tests/suites/test_suite_asn1write.function +++ b/tests/suites/test_suite_asn1write.function @@ -82,3 +82,57 @@ void mbedtls_asn1_write_ia5_string( char *str, char *hex_asn1, } } /* END_CASE */ + +/* BEGIN_CASE */ +void mbedtls_asn1_write_len( int len, char *check_str, int buf_len, + int result ) +{ + int ret; + unsigned char buf[150]; + unsigned char asn1[150]; + unsigned char *p; + size_t asn1_len, i, read_len; + + memset( buf, GUARD_VAL, sizeof( buf ) ); + memset( asn1, 0, sizeof( asn1 ) ); + asn1_len = unhexify( asn1, check_str ); + + p = buf + GUARD_LEN + buf_len; + + ret = mbedtls_asn1_write_len( &p, buf + GUARD_LEN, (size_t) len ); + + TEST_ASSERT( ret == result ); + + /* Check for buffer overwrite on both sides */ + for( i = 0; i < GUARD_LEN; i++ ) + { + TEST_ASSERT( buf[i] == GUARD_VAL ); + TEST_ASSERT( buf[GUARD_LEN + buf_len + i] == GUARD_VAL ); + } + + if( result >= 0 ) + { + TEST_ASSERT( (size_t) ret == asn1_len ); + TEST_ASSERT( p + asn1_len == buf + GUARD_LEN + buf_len ); + + TEST_ASSERT( memcmp( p, asn1, asn1_len ) == 0 ); + + /* Read back with mbedtls_asn1_get_len() to check */ + ret = mbedtls_asn1_get_len( &p, buf + GUARD_LEN + buf_len, &read_len ); + + if( len == 0 ) + { + TEST_ASSERT( ret == 0 ); + } + else + { + /* Return will be MBEDTLS_ERR_ASN1_OUT_OF_DATA because the rest of + * the buffer is missing + */ + TEST_ASSERT( ret == MBEDTLS_ERR_ASN1_OUT_OF_DATA ); + } + TEST_ASSERT( read_len == (size_t) len ); + TEST_ASSERT( p == buf + GUARD_LEN + buf_len ); + } +} +/* END_CASE */ diff --git a/tests/suites/test_suite_cipher.aes.data b/tests/suites/test_suite_cipher.aes.data index 9c21cfcc5960..9b39bc9a8aab 100644 --- a/tests/suites/test_suite_cipher.aes.data +++ b/tests/suites/test_suite_cipher.aes.data @@ -1097,3 +1097,6 @@ test_vec_ecb:MBEDTLS_CIPHER_AES_256_ECB:MBEDTLS_DECRYPT:"00000000000000000000000 AES-256-ECB Decrypt NIST KAT #12 depends_on:MBEDTLS_AES_C test_vec_ecb:MBEDTLS_CIPHER_AES_256_ECB:MBEDTLS_DECRYPT:"0000000000000000000000000000000000000000000000000000000000000000":"9b80eefb7ebe2d2b16247aa0efc72f5d":"e0000000000000000000000000000000":0 + +Cipher Corner Case behaviours +cipher_special_behaviours: diff --git a/tests/suites/test_suite_cipher.function b/tests/suites/test_suite_cipher.function index 967a6a0a1c66..107352438318 100644 --- a/tests/suites/test_suite_cipher.function +++ b/tests/suites/test_suite_cipher.function @@ -91,6 +91,44 @@ void cipher_null_args( ) } /* END_CASE */ +/* 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 */ + /* BEGIN_CASE */ void enc_dec_buf( int cipher_id, char *cipher_string, int key_len, int length_val, int pad_mode ) diff --git a/tests/suites/test_suite_ctr_drbg.data b/tests/suites/test_suite_ctr_drbg.data index 5c69d20cba7e..666165851e49 100644 --- a/tests/suites/test_suite_ctr_drbg.data +++ b/tests/suites/test_suite_ctr_drbg.data @@ -727,6 +727,9 @@ ctr_drbg_seed_file:"data_files/ctr_drbg_seed":0 CTR_DRBG write/update seed file ctr_drbg_seed_file:"no_such_dir/file":MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR +CTR_DRBG Special Behaviours +ctr_drbg_special_behaviours: + CTR_DRBG self test ctr_drbg_selftest: diff --git a/tests/suites/test_suite_ctr_drbg.function b/tests/suites/test_suite_ctr_drbg.function index b7d2bece9d59..3c7873b3182a 100644 --- a/tests/suites/test_suite_ctr_drbg.function +++ b/tests/suites/test_suite_ctr_drbg.function @@ -16,6 +16,34 @@ int mbedtls_entropy_func( void *data, unsigned char *buf, size_t len ) * END_DEPENDENCIES */ +/* BEGIN_CASE */ +void ctr_drbg_special_behaviours( ) +{ + mbedtls_ctr_drbg_context ctx; + unsigned char output[512]; + unsigned char additional[512]; + + mbedtls_ctr_drbg_init( &ctx ); + memset( output, 0, sizeof( output ) ); + memset( additional, 0, sizeof( additional ) ); + + TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, + output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1, + additional, 16 ) == + MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG ); + TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, + output, 16, + additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1 ) == + MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); + + TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional, + MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1 ) == + MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); +exit: + mbedtls_ctr_drbg_free( &ctx ); +} +/* END_CASE */ + /* BEGIN_CASE */ void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string, char *add1_string, char *add2_string, diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function index 0fcaa388f9c8..5c453e48ec2b 100644 --- a/tests/suites/test_suite_md.function +++ b/tests/suites/test_suite_md.function @@ -134,18 +134,18 @@ void md_text( char *text_md_name, char *text_src_string, char *hex_hash_string ) unsigned char output[100]; const mbedtls_md_info_t *md_info = NULL; - memset(md_name, 0x00, 100); - memset(src_str, 0x00, 1000); - memset(hash_str, 0x00, 1000); - memset(output, 0x00, 100); + memset( md_name, 0x00, 100 ); + memset( src_str, 0x00, 1000 ); + memset( hash_str, 0x00, 1000 ); + memset( output, 0x00, 100 ); - strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 ); - strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); + strncpy( (char *) src_str, text_src_string, sizeof( src_str ) - 1 ); + strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); md_info = mbedtls_md_info_from_string(md_name); TEST_ASSERT( md_info != NULL ); TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, strlen( (char *) src_str ), output ) ); - hexify( hash_str, output, mbedtls_md_get_size(md_info) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); } @@ -161,19 +161,19 @@ void md_hex( char *text_md_name, char *hex_src_string, char *hex_hash_string ) int src_len; const mbedtls_md_info_t *md_info = NULL; - memset(md_name, 0x00, 100); - memset(src_str, 0x00, 10000); - memset(hash_str, 0x00, 10000); - memset(output, 0x00, 100); + memset( md_name, 0x00, 100 ); + memset( src_str, 0x00, 10000 ); + memset( hash_str, 0x00, 10000 ); + memset( output, 0x00, 100 ); - strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); - md_info = mbedtls_md_info_from_string(md_name); + strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); + md_info = mbedtls_md_info_from_string( md_name ); TEST_ASSERT( md_info != NULL ); src_len = unhexify( src_str, hex_src_string ); TEST_ASSERT ( 0 == mbedtls_md( md_info, src_str, src_len, output ) ); - hexify( hash_str, output, mbedtls_md_get_size(md_info) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); } @@ -187,30 +187,46 @@ void md_text_multi( char *text_md_name, char *text_src_string, unsigned char src_str[1000]; unsigned char hash_str[1000]; unsigned char output[100]; + int halfway, len; const mbedtls_md_info_t *md_info = NULL; - mbedtls_md_context_t ctx; + mbedtls_md_context_t ctx, ctx_copy; mbedtls_md_init( &ctx ); + mbedtls_md_init( &ctx_copy ); - memset(md_name, 0x00, 100); - memset(src_str, 0x00, 1000); - memset(hash_str, 0x00, 1000); - memset(output, 0x00, 100); + memset( md_name, 0x00, 100 ); + memset( src_str, 0x00, 1000 ); + memset( hash_str, 0x00, 1000 ); + memset( output, 0x00, 100 ); strncpy( (char *) src_str, text_src_string, sizeof(src_str) - 1 ); strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); + len = strlen( (char *) src_str ); + halfway = len / 2; + md_info = mbedtls_md_info_from_string(md_name); TEST_ASSERT( md_info != NULL ); TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) ); + TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) ); TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) ); TEST_ASSERT ( ctx.md_ctx != NULL ); - TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, strlen( (char *) src_str ) ) ); + TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) ); + TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) ); + + TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, len - halfway ) ); TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); + TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); - hexify( hash_str, output, mbedtls_md_get_size(md_info) ); + /* Test clone */ + memset( hash_str, 0x00, 1000 ); + memset( output, 0x00, 100 ); + TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, len - halfway ) ); + TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); exit: @@ -226,31 +242,44 @@ void md_hex_multi( char *text_md_name, char *hex_src_string, unsigned char src_str[10000]; unsigned char hash_str[10000]; unsigned char output[100]; - int src_len; + int src_len, halfway; const mbedtls_md_info_t *md_info = NULL; - mbedtls_md_context_t ctx; + mbedtls_md_context_t ctx, ctx_copy; mbedtls_md_init( &ctx ); + mbedtls_md_init( &ctx_copy ); - memset(md_name, 0x00, 100); - memset(src_str, 0x00, 10000); - memset(hash_str, 0x00, 10000); - memset(output, 0x00, 100); + memset( md_name, 0x00, 100 ); + memset( src_str, 0x00, 10000 ); + memset( hash_str, 0x00, 10000 ); + memset( output, 0x00, 100 ); - strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); + strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); md_info = mbedtls_md_info_from_string(md_name); TEST_ASSERT( md_info != NULL ); TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 0 ) ); + TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx_copy, md_info, 0 ) ); src_len = unhexify( src_str, hex_src_string ); + halfway = src_len / 2; TEST_ASSERT ( 0 == mbedtls_md_starts( &ctx ) ); TEST_ASSERT ( ctx.md_ctx != NULL ); - TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, src_len ) ); + TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str, halfway ) ); + TEST_ASSERT ( 0 == mbedtls_md_clone( &ctx_copy, &ctx ) ); + + TEST_ASSERT ( 0 == mbedtls_md_update( &ctx, src_str + halfway, src_len - halfway) ); TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx, output ) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); + TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); - hexify( hash_str, output, mbedtls_md_get_size(md_info) ); + /* Test clone */ + memset( hash_str, 0x00, 10000 ); + memset( output, 0x00, 100 ); + TEST_ASSERT ( 0 == mbedtls_md_update( &ctx_copy, src_str + halfway, src_len - halfway ) ); + TEST_ASSERT ( 0 == mbedtls_md_finish( &ctx_copy, output ) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); exit: @@ -270,13 +299,13 @@ void mbedtls_md_hmac( char *text_md_name, int trunc_size, char *hex_key_string, int key_len, src_len; const mbedtls_md_info_t *md_info = NULL; - memset(md_name, 0x00, 100); - memset(src_str, 0x00, 10000); - memset(key_str, 0x00, 10000); - memset(hash_str, 0x00, 10000); - memset(output, 0x00, 100); + memset( md_name, 0x00, 100 ); + memset( src_str, 0x00, 10000 ); + memset( key_str, 0x00, 10000 ); + memset( hash_str, 0x00, 10000 ); + memset( output, 0x00, 100 ); - strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); + strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); md_info = mbedtls_md_info_from_string( md_name ); TEST_ASSERT( md_info != NULL ); @@ -284,7 +313,7 @@ void mbedtls_md_hmac( char *text_md_name, int trunc_size, char *hex_key_string, src_len = unhexify( src_str, hex_src_string ); TEST_ASSERT ( mbedtls_md_hmac( md_info, key_str, key_len, src_str, src_len, output ) == 0 ); - hexify( hash_str, output, mbedtls_md_get_size(md_info) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); } @@ -299,43 +328,46 @@ void md_hmac_multi( char *text_md_name, int trunc_size, char *hex_key_string, unsigned char key_str[10000]; unsigned char hash_str[10000]; unsigned char output[100]; - int key_len, src_len; + int key_len, src_len, halfway; const mbedtls_md_info_t *md_info = NULL; mbedtls_md_context_t ctx; mbedtls_md_init( &ctx ); - memset(md_name, 0x00, 100); - memset(src_str, 0x00, 10000); - memset(key_str, 0x00, 10000); - memset(hash_str, 0x00, 10000); - memset(output, 0x00, 100); + memset( md_name, 0x00, 100 ); + memset( src_str, 0x00, 10000 ); + memset( key_str, 0x00, 10000 ); + memset( hash_str, 0x00, 10000 ); + memset( output, 0x00, 100 ); - strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); + strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); md_info = mbedtls_md_info_from_string( md_name ); TEST_ASSERT( md_info != NULL ); TEST_ASSERT ( 0 == mbedtls_md_setup( &ctx, md_info, 1 ) ); key_len = unhexify( key_str, hex_key_string ); src_len = unhexify( src_str, hex_src_string ); + halfway = src_len / 2; TEST_ASSERT ( 0 == mbedtls_md_hmac_starts( &ctx, key_str, key_len ) ); TEST_ASSERT ( ctx.md_ctx != NULL ); - TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, src_len ) ); + TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, halfway ) ); + TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str + halfway, src_len - halfway ) ); TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) ); - hexify( hash_str, output, mbedtls_md_get_size(md_info) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); /* Test again, for reset() */ - memset(hash_str, 0x00, 10000); - memset(output, 0x00, 100); + memset( hash_str, 0x00, 10000 ); + memset( output, 0x00, 100 ); TEST_ASSERT ( 0 == mbedtls_md_hmac_reset( &ctx ) ); - TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, src_len ) ); + TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str, halfway ) ); + TEST_ASSERT ( 0 == mbedtls_md_hmac_update( &ctx, src_str + halfway, src_len - halfway ) ); TEST_ASSERT ( 0 == mbedtls_md_hmac_finish( &ctx, output ) ); - hexify( hash_str, output, mbedtls_md_get_size(md_info) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); exit: @@ -351,16 +383,16 @@ void mbedtls_md_file( char *text_md_name, char *filename, char *hex_hash_string unsigned char output[100]; const mbedtls_md_info_t *md_info = NULL; - memset(md_name, 0x00, 100); - memset(hash_str, 0x00, 1000); - memset(output, 0x00, 100); + memset( md_name, 0x00, 100 ); + memset( hash_str, 0x00, 1000 ); + memset( output, 0x00, 100 ); - strncpy( (char *) md_name, text_md_name, sizeof(md_name) - 1 ); + strncpy( (char *) md_name, text_md_name, sizeof( md_name ) - 1 ); md_info = mbedtls_md_info_from_string( md_name ); TEST_ASSERT( md_info != NULL ); TEST_ASSERT( mbedtls_md_file( md_info, filename, output ) == 0 ); - hexify( hash_str, output, mbedtls_md_get_size(md_info) ); + hexify( hash_str, output, mbedtls_md_get_size( md_info ) ); TEST_ASSERT( strcmp( (char *) hash_str, hex_hash_string ) == 0 ); } diff --git a/tests/suites/test_suite_mpi.data b/tests/suites/test_suite_mpi.data index 3fd7f2d1b65c..fa65b5f2a1c6 100644 --- a/tests/suites/test_suite_mpi.data +++ b/tests/suites/test_suite_mpi.data @@ -706,22 +706,25 @@ Test bit getting (Value bit 23) mbedtls_mpi_get_bit:10:"49979687":23:1 Test bit set (Change existing value with a 1) -mbedtls_mpi_set_bit:10:"49979687":24:1:10:"66756903" +mbedtls_mpi_set_bit:10:"49979687":24:1:10:"66756903":0 Test bit set (Change existing value with a 0) -mbedtls_mpi_set_bit:10:"49979687":25:0:10:"16425255" +mbedtls_mpi_set_bit:10:"49979687":25:0:10:"16425255":0 Test bit set (Add above existing limbs with a 0) -mbedtls_mpi_set_bit:10:"49979687":80:0:10:"49979687" +mbedtls_mpi_set_bit:10:"49979687":80:0:10:"49979687":0 Test bit set (Add above existing limbs with a 1) -mbedtls_mpi_set_bit:10:"49979687":80:1:10:"1208925819614629224685863" +mbedtls_mpi_set_bit:10:"49979687":80:1:10:"1208925819614629224685863":0 Test bit set (Bit index larger than 31 with a 0) -mbedtls_mpi_set_bit:16:"FFFFFFFFFFFFFFFF":32:0:16:"FFFFFFFEFFFFFFFF" +mbedtls_mpi_set_bit:16:"FFFFFFFFFFFFFFFF":32:0:16:"FFFFFFFEFFFFFFFF":0 Test bit set (Bit index larger than 31 with a 1) -mbedtls_mpi_set_bit:16:"00":32:1:16:"0100000000" +mbedtls_mpi_set_bit:16:"00":32:1:16:"0100000000":0 + +Test bit set (Invalid bit value) +mbedtls_mpi_set_bit:16:"00":5:2:16:"00":MBEDTLS_ERR_MPI_BAD_INPUT_DATA MPI Selftest depends_on:MBEDTLS_SELF_TEST diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index c41ca69375ae..e5d0850f194d 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -189,16 +189,20 @@ exit: /* END_CASE */ /* BEGIN_CASE */ -void mbedtls_mpi_set_bit( int radix_X, char *input_X, int pos, int val, int radix_Y, - char *output_Y ) +void mbedtls_mpi_set_bit( int radix_X, char *input_X, int pos, int val, + int radix_Y, char *output_Y, int result ) { mbedtls_mpi X, Y; mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 ); TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 ); - TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == 0 ); - TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); + TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result ); + + if( result == 0 ) + { + TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 ); + } exit: mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );