diff --git a/include/mbedtls/md.h b/include/mbedtls/md.h index 316478870..cb3e05871 100644 --- a/include/mbedtls/md.h +++ b/include/mbedtls/md.h @@ -247,6 +247,7 @@ int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output ); int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output ); +#if defined(MBEDTLS_FS_IO) /** * \brief Output = message_digest( file contents ) * @@ -259,7 +260,8 @@ int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, si * MBEDTLS_ERR_MD_BAD_INPUT_DATA if md_info was NULL. */ int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, - unsigned char *output ); + unsigned char *output ); +#endif /* MBEDTLS_FS_IO */ /** * \brief Set HMAC key and prepare to authenticate a new message. diff --git a/include/mbedtls/md_internal.h b/include/mbedtls/md_internal.h index d42dc5c0e..8cc8c5bac 100644 --- a/include/mbedtls/md_internal.h +++ b/include/mbedtls/md_internal.h @@ -71,9 +71,6 @@ struct mbedtls_md_info_t void (*digest_func)( const unsigned char *input, size_t ilen, unsigned char *output ); - /** Generic file digest function */ - int (*file_func)( const char *path, unsigned char *output ); - /** Allocate a new context */ void * (*ctx_alloc_func)( void ); diff --git a/library/md.c b/library/md.c index dfa4526dd..737059ae5 100644 --- a/library/md.c +++ b/library/md.c @@ -45,9 +45,8 @@ #include -#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ - !defined(EFI32) -#define strcasecmp _stricmp +#if defined(MBEDTLS_FS_IO) +#include #endif /* Implementation that should never be optimized out by the compiler */ @@ -270,28 +269,49 @@ int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, si return( 0 ); } +#if defined(MBEDTLS_FS_IO) int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output ) { -#if defined(MBEDTLS_FS_IO) int ret; -#endif + FILE *f; + size_t n; + mbedtls_md_context_t ctx; + unsigned char buf[1024]; if( md_info == NULL ) return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); -#if defined(MBEDTLS_FS_IO) - ret = md_info->file_func( path, output ); - if( ret != 0 ) - return( MBEDTLS_ERR_MD_FILE_IO_ERROR + ret ); + mbedtls_md_init( &ctx ); + + if( ( f = fopen( path, "rb" ) ) == NULL ) + { + ret = MBEDTLS_ERR_MD_FILE_IO_ERROR; + goto cleanup; + } + + if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 ) + goto cleanup; + + md_info->starts_func( ctx.md_ctx ); + + while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) + md_info->update_func( ctx.md_ctx, buf, n ); + + if( ferror( f ) != 0 ) + { + ret = MBEDTLS_ERR_MD_FILE_IO_ERROR; + goto cleanup; + } + + md_info->finish_func( ctx.md_ctx, output ); + +cleanup: + fclose( f ); + mbedtls_md_free( &ctx ); return( ret ); -#else - ((void) path); - ((void) output); - - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif /* MBEDTLS_FS_IO */ } +#endif /* MBEDTLS_FS_IO */ int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen ) { diff --git a/library/md_wrap.c b/library/md_wrap.c index 4f12ed6b1..c1140d7f8 100644 --- a/library/md_wrap.c +++ b/library/md_wrap.c @@ -93,17 +93,6 @@ static void md2_finish_wrap( void *ctx, unsigned char *output ) mbedtls_md2_finish( (mbedtls_md2_context *) ctx, output ); } -static int md2_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_md2_file( path, output ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void * md2_ctx_alloc( void ) { return mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) ); @@ -131,7 +120,6 @@ const mbedtls_md_info_t mbedtls_md2_info = { md2_update_wrap, md2_finish_wrap, mbedtls_md2, - md2_file_wrap, md2_ctx_alloc, md2_ctx_free, md2_process_wrap, @@ -157,17 +145,6 @@ static void md4_finish_wrap( void *ctx, unsigned char *output ) mbedtls_md4_finish( (mbedtls_md4_context *) ctx, output ); } -static int md4_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_md4_file( path, output ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void *md4_ctx_alloc( void ) { return mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) ); @@ -193,7 +170,6 @@ const mbedtls_md_info_t mbedtls_md4_info = { md4_update_wrap, md4_finish_wrap, mbedtls_md4, - md4_file_wrap, md4_ctx_alloc, md4_ctx_free, md4_process_wrap, @@ -219,17 +195,6 @@ static void md5_finish_wrap( void *ctx, unsigned char *output ) mbedtls_md5_finish( (mbedtls_md5_context *) ctx, output ); } -static int md5_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_md5_file( path, output ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void * md5_ctx_alloc( void ) { return mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) ); @@ -255,7 +220,6 @@ const mbedtls_md_info_t mbedtls_md5_info = { md5_update_wrap, md5_finish_wrap, mbedtls_md5, - md5_file_wrap, md5_ctx_alloc, md5_ctx_free, md5_process_wrap, @@ -281,17 +245,6 @@ static void ripemd160_finish_wrap( void *ctx, unsigned char *output ) mbedtls_ripemd160_finish( (mbedtls_ripemd160_context *) ctx, output ); } -static int ripemd160_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_ripemd160_file( path, output ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void * ripemd160_ctx_alloc( void ) { mbedtls_ripemd160_context *ctx; @@ -325,7 +278,6 @@ const mbedtls_md_info_t mbedtls_ripemd160_info = { ripemd160_update_wrap, ripemd160_finish_wrap, mbedtls_ripemd160, - ripemd160_file_wrap, ripemd160_ctx_alloc, ripemd160_ctx_free, ripemd160_process_wrap, @@ -351,17 +303,6 @@ static void sha1_finish_wrap( void *ctx, unsigned char *output ) mbedtls_sha1_finish( (mbedtls_sha1_context *) ctx, output ); } -static int sha1_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_sha1_file( path, output ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void * sha1_ctx_alloc( void ) { mbedtls_sha1_context *ctx; @@ -395,7 +336,6 @@ const mbedtls_md_info_t mbedtls_sha1_info = { sha1_update_wrap, sha1_finish_wrap, mbedtls_sha1, - sha1_file_wrap, sha1_ctx_alloc, sha1_ctx_free, sha1_process_wrap, @@ -430,17 +370,6 @@ static void sha224_wrap( const unsigned char *input, size_t ilen, mbedtls_sha256( input, ilen, output, 1 ); } -static int sha224_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_sha256_file( path, output, 1 ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void * sha224_ctx_alloc( void ) { return mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) ); @@ -466,7 +395,6 @@ const mbedtls_md_info_t mbedtls_sha224_info = { sha224_update_wrap, sha224_finish_wrap, sha224_wrap, - sha224_file_wrap, sha224_ctx_alloc, sha224_ctx_free, sha224_process_wrap, @@ -494,17 +422,6 @@ static void sha256_wrap( const unsigned char *input, size_t ilen, mbedtls_sha256( input, ilen, output, 0 ); } -static int sha256_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_sha256_file( path, output, 0 ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void * sha256_ctx_alloc( void ) { mbedtls_sha256_context *ctx; @@ -538,7 +455,6 @@ const mbedtls_md_info_t mbedtls_sha256_info = { sha256_update_wrap, sha256_finish_wrap, sha256_wrap, - sha256_file_wrap, sha256_ctx_alloc, sha256_ctx_free, sha256_process_wrap, @@ -570,17 +486,6 @@ static void sha384_wrap( const unsigned char *input, size_t ilen, mbedtls_sha512( input, ilen, output, 1 ); } -static int sha384_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_sha512_file( path, output, 1 ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void * sha384_ctx_alloc( void ) { return mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) ); @@ -606,7 +511,6 @@ const mbedtls_md_info_t mbedtls_sha384_info = { sha384_update_wrap, sha384_finish_wrap, sha384_wrap, - sha384_file_wrap, sha384_ctx_alloc, sha384_ctx_free, sha384_process_wrap, @@ -634,17 +538,6 @@ static void sha512_wrap( const unsigned char *input, size_t ilen, mbedtls_sha512( input, ilen, output, 0 ); } -static int sha512_file_wrap( const char *path, unsigned char *output ) -{ -#if defined(MBEDTLS_FS_IO) - return mbedtls_sha512_file( path, output, 0 ); -#else - ((void) path); - ((void) output); - return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); -#endif -} - static void * sha512_ctx_alloc( void ) { mbedtls_sha512_context *ctx; @@ -678,7 +571,6 @@ const mbedtls_md_info_t mbedtls_sha512_info = { sha512_update_wrap, sha512_finish_wrap, sha512_wrap, - sha512_file_wrap, sha512_ctx_alloc, sha512_ctx_free, sha512_process_wrap, diff --git a/tests/suites/test_suite_md.function b/tests/suites/test_suite_md.function index a3b87f745..099d109a2 100644 --- a/tests/suites/test_suite_md.function +++ b/tests/suites/test_suite_md.function @@ -68,7 +68,9 @@ void md_null_args( ) TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); +#if defined(MBEDTLS_FS_IO) TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA ); +#endif TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );