mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-01-10 05:35:34 +00:00
Implement md_file in the MD layer
This commit is contained in:
parent
eb0d8706ce
commit
bfffa908a6
|
@ -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,
|
int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
|
||||||
unsigned char *output );
|
unsigned char *output );
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_FS_IO)
|
||||||
/**
|
/**
|
||||||
* \brief Output = message_digest( file contents )
|
* \brief Output = message_digest( file contents )
|
||||||
*
|
*
|
||||||
|
@ -260,6 +261,7 @@ int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, si
|
||||||
*/
|
*/
|
||||||
int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
|
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.
|
* \brief Set HMAC key and prepare to authenticate a new message.
|
||||||
|
|
|
@ -71,9 +71,6 @@ struct mbedtls_md_info_t
|
||||||
void (*digest_func)( const unsigned char *input, size_t ilen,
|
void (*digest_func)( const unsigned char *input, size_t ilen,
|
||||||
unsigned char *output );
|
unsigned char *output );
|
||||||
|
|
||||||
/** Generic file digest function */
|
|
||||||
int (*file_func)( const char *path, unsigned char *output );
|
|
||||||
|
|
||||||
/** Allocate a new context */
|
/** Allocate a new context */
|
||||||
void * (*ctx_alloc_func)( void );
|
void * (*ctx_alloc_func)( void );
|
||||||
|
|
||||||
|
|
50
library/md.c
50
library/md.c
|
@ -45,9 +45,8 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
|
#if defined(MBEDTLS_FS_IO)
|
||||||
!defined(EFI32)
|
#include <stdio.h>
|
||||||
#define strcasecmp _stricmp
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Implementation that should never be optimized out by the compiler */
|
/* 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 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_FS_IO)
|
||||||
int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
|
int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, unsigned char *output )
|
||||||
{
|
{
|
||||||
#if defined(MBEDTLS_FS_IO)
|
|
||||||
int ret;
|
int ret;
|
||||||
#endif
|
FILE *f;
|
||||||
|
size_t n;
|
||||||
|
mbedtls_md_context_t ctx;
|
||||||
|
unsigned char buf[1024];
|
||||||
|
|
||||||
if( md_info == NULL )
|
if( md_info == NULL )
|
||||||
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
||||||
|
|
||||||
#if defined(MBEDTLS_FS_IO)
|
mbedtls_md_init( &ctx );
|
||||||
ret = md_info->file_func( path, output );
|
|
||||||
if( ret != 0 )
|
if( ( f = fopen( path, "rb" ) ) == NULL )
|
||||||
return( MBEDTLS_ERR_MD_FILE_IO_ERROR + ret );
|
{
|
||||||
|
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 );
|
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 )
|
int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen )
|
||||||
{
|
{
|
||||||
|
|
|
@ -93,17 +93,6 @@ static void md2_finish_wrap( void *ctx, unsigned char *output )
|
||||||
mbedtls_md2_finish( (mbedtls_md2_context *) ctx, 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 )
|
static void * md2_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
return mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
|
return mbedtls_calloc( 1, sizeof( mbedtls_md2_context ) );
|
||||||
|
@ -131,7 +120,6 @@ const mbedtls_md_info_t mbedtls_md2_info = {
|
||||||
md2_update_wrap,
|
md2_update_wrap,
|
||||||
md2_finish_wrap,
|
md2_finish_wrap,
|
||||||
mbedtls_md2,
|
mbedtls_md2,
|
||||||
md2_file_wrap,
|
|
||||||
md2_ctx_alloc,
|
md2_ctx_alloc,
|
||||||
md2_ctx_free,
|
md2_ctx_free,
|
||||||
md2_process_wrap,
|
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 );
|
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 )
|
static void *md4_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
return mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
|
return mbedtls_calloc( 1, sizeof( mbedtls_md4_context ) );
|
||||||
|
@ -193,7 +170,6 @@ const mbedtls_md_info_t mbedtls_md4_info = {
|
||||||
md4_update_wrap,
|
md4_update_wrap,
|
||||||
md4_finish_wrap,
|
md4_finish_wrap,
|
||||||
mbedtls_md4,
|
mbedtls_md4,
|
||||||
md4_file_wrap,
|
|
||||||
md4_ctx_alloc,
|
md4_ctx_alloc,
|
||||||
md4_ctx_free,
|
md4_ctx_free,
|
||||||
md4_process_wrap,
|
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 );
|
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 )
|
static void * md5_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
return mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
|
return mbedtls_calloc( 1, sizeof( mbedtls_md5_context ) );
|
||||||
|
@ -255,7 +220,6 @@ const mbedtls_md_info_t mbedtls_md5_info = {
|
||||||
md5_update_wrap,
|
md5_update_wrap,
|
||||||
md5_finish_wrap,
|
md5_finish_wrap,
|
||||||
mbedtls_md5,
|
mbedtls_md5,
|
||||||
md5_file_wrap,
|
|
||||||
md5_ctx_alloc,
|
md5_ctx_alloc,
|
||||||
md5_ctx_free,
|
md5_ctx_free,
|
||||||
md5_process_wrap,
|
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 );
|
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 )
|
static void * ripemd160_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
mbedtls_ripemd160_context *ctx;
|
mbedtls_ripemd160_context *ctx;
|
||||||
|
@ -325,7 +278,6 @@ const mbedtls_md_info_t mbedtls_ripemd160_info = {
|
||||||
ripemd160_update_wrap,
|
ripemd160_update_wrap,
|
||||||
ripemd160_finish_wrap,
|
ripemd160_finish_wrap,
|
||||||
mbedtls_ripemd160,
|
mbedtls_ripemd160,
|
||||||
ripemd160_file_wrap,
|
|
||||||
ripemd160_ctx_alloc,
|
ripemd160_ctx_alloc,
|
||||||
ripemd160_ctx_free,
|
ripemd160_ctx_free,
|
||||||
ripemd160_process_wrap,
|
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 );
|
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 )
|
static void * sha1_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
mbedtls_sha1_context *ctx;
|
mbedtls_sha1_context *ctx;
|
||||||
|
@ -395,7 +336,6 @@ const mbedtls_md_info_t mbedtls_sha1_info = {
|
||||||
sha1_update_wrap,
|
sha1_update_wrap,
|
||||||
sha1_finish_wrap,
|
sha1_finish_wrap,
|
||||||
mbedtls_sha1,
|
mbedtls_sha1,
|
||||||
sha1_file_wrap,
|
|
||||||
sha1_ctx_alloc,
|
sha1_ctx_alloc,
|
||||||
sha1_ctx_free,
|
sha1_ctx_free,
|
||||||
sha1_process_wrap,
|
sha1_process_wrap,
|
||||||
|
@ -430,17 +370,6 @@ static void sha224_wrap( const unsigned char *input, size_t ilen,
|
||||||
mbedtls_sha256( input, ilen, output, 1 );
|
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 )
|
static void * sha224_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
return mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
|
return mbedtls_calloc( 1, sizeof( mbedtls_sha256_context ) );
|
||||||
|
@ -466,7 +395,6 @@ const mbedtls_md_info_t mbedtls_sha224_info = {
|
||||||
sha224_update_wrap,
|
sha224_update_wrap,
|
||||||
sha224_finish_wrap,
|
sha224_finish_wrap,
|
||||||
sha224_wrap,
|
sha224_wrap,
|
||||||
sha224_file_wrap,
|
|
||||||
sha224_ctx_alloc,
|
sha224_ctx_alloc,
|
||||||
sha224_ctx_free,
|
sha224_ctx_free,
|
||||||
sha224_process_wrap,
|
sha224_process_wrap,
|
||||||
|
@ -494,17 +422,6 @@ static void sha256_wrap( const unsigned char *input, size_t ilen,
|
||||||
mbedtls_sha256( input, ilen, output, 0 );
|
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 )
|
static void * sha256_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
mbedtls_sha256_context *ctx;
|
mbedtls_sha256_context *ctx;
|
||||||
|
@ -538,7 +455,6 @@ const mbedtls_md_info_t mbedtls_sha256_info = {
|
||||||
sha256_update_wrap,
|
sha256_update_wrap,
|
||||||
sha256_finish_wrap,
|
sha256_finish_wrap,
|
||||||
sha256_wrap,
|
sha256_wrap,
|
||||||
sha256_file_wrap,
|
|
||||||
sha256_ctx_alloc,
|
sha256_ctx_alloc,
|
||||||
sha256_ctx_free,
|
sha256_ctx_free,
|
||||||
sha256_process_wrap,
|
sha256_process_wrap,
|
||||||
|
@ -570,17 +486,6 @@ static void sha384_wrap( const unsigned char *input, size_t ilen,
|
||||||
mbedtls_sha512( input, ilen, output, 1 );
|
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 )
|
static void * sha384_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
return mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
|
return mbedtls_calloc( 1, sizeof( mbedtls_sha512_context ) );
|
||||||
|
@ -606,7 +511,6 @@ const mbedtls_md_info_t mbedtls_sha384_info = {
|
||||||
sha384_update_wrap,
|
sha384_update_wrap,
|
||||||
sha384_finish_wrap,
|
sha384_finish_wrap,
|
||||||
sha384_wrap,
|
sha384_wrap,
|
||||||
sha384_file_wrap,
|
|
||||||
sha384_ctx_alloc,
|
sha384_ctx_alloc,
|
||||||
sha384_ctx_free,
|
sha384_ctx_free,
|
||||||
sha384_process_wrap,
|
sha384_process_wrap,
|
||||||
|
@ -634,17 +538,6 @@ static void sha512_wrap( const unsigned char *input, size_t ilen,
|
||||||
mbedtls_sha512( input, ilen, output, 0 );
|
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 )
|
static void * sha512_ctx_alloc( void )
|
||||||
{
|
{
|
||||||
mbedtls_sha512_context *ctx;
|
mbedtls_sha512_context *ctx;
|
||||||
|
@ -678,7 +571,6 @@ const mbedtls_md_info_t mbedtls_sha512_info = {
|
||||||
sha512_update_wrap,
|
sha512_update_wrap,
|
||||||
sha512_finish_wrap,
|
sha512_finish_wrap,
|
||||||
sha512_wrap,
|
sha512_wrap,
|
||||||
sha512_file_wrap,
|
|
||||||
sha512_ctx_alloc,
|
sha512_ctx_alloc,
|
||||||
sha512_ctx_free,
|
sha512_ctx_free,
|
||||||
sha512_process_wrap,
|
sha512_process_wrap,
|
||||||
|
|
|
@ -68,7 +68,9 @@ void md_null_args( )
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_md( NULL, buf, 1, buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
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 );
|
TEST_ASSERT( mbedtls_md_file( NULL, "", buf ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 )
|
TEST_ASSERT( mbedtls_md_hmac_starts( NULL, buf, 1 )
|
||||||
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
== MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
||||||
|
|
Loading…
Reference in a new issue