Add int return values to SHA1 function calls

The following function calls are being deprecated to introduce int
return values.
    * mbedtls_sha1()
    * mbedtls_sha1_starts()
    * mbedtls_sha1_update()
    * mbedtls_sha1_finish()
    * mbedtls_sha1_process()
The return codes can be used to return error values. This is important
when using hardware accelerators.
This commit is contained in:
Andres Amaya Garcia 2017-04-28 15:14:50 +01:00
parent f2a597fa3d
commit 034ea7e754
2 changed files with 192 additions and 30 deletions

View File

@ -32,6 +32,11 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#if !defined(MBEDTLS_SHA1_ALT) #if !defined(MBEDTLS_SHA1_ALT)
// Regular implementation // Regular implementation
// //
@ -78,8 +83,10 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
* \brief SHA-1 context setup * \brief SHA-1 context setup
* *
* \param ctx context to be initialized * \param ctx context to be initialized
*
* \return 0 if successful
*/ */
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); int mbedtls_sha1_starts_ext( mbedtls_sha1_context *ctx );
/** /**
* \brief SHA-1 process buffer * \brief SHA-1 process buffer
@ -87,19 +94,103 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
* \param ctx SHA-1 context * \param ctx SHA-1 context
* \param input buffer holding the data * \param input buffer holding the data
* \param ilen length of the input data * \param ilen length of the input data
*
* \return 0 if successful
*/ */
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); int mbedtls_sha1_update_ext( mbedtls_sha1_context *ctx,
const unsigned char *input,
size_t ilen );
/** /**
* \brief SHA-1 final digest * \brief SHA-1 final digest
* *
* \param ctx SHA-1 context * \param ctx SHA-1 context
* \param output SHA-1 checksum result * \param output SHA-1 checksum result
*
* \return 0 if successful
*/ */
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ); int mbedtls_sha1_finish_ext( mbedtls_sha1_context *ctx,
unsigned char output[20] );
/* Internal use */ /**
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ); * \brief SHA-1 process data block (internal use only)
*
* \param ctx SHA-1 context
* \param data buffer holding one block of data
*
* \return 0 if successful
*/
int mbedtls_sha1_process_ext( mbedtls_sha1_context *ctx,
const unsigned char data[64] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief SHA-1 context setup
*
* \deprecated Superseded by mbedtls_sha1_starts_ext() in 2.5.0
*
* \param ctx context to be initialized
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha1_starts(
mbedtls_sha1_context *ctx )
{
mbedtls_sha1_starts_ext( ctx );
}
/**
* \brief SHA-1 process buffer
*
* \deprecated Superseded by mbedtls_sha1_update_ext() in 2.5.0
*
* \param ctx SHA-1 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha1_update(
mbedtls_sha1_context *ctx,
const unsigned char *input,
size_t ilen )
{
mbedtls_sha1_update_ext( ctx, input, ilen );
}
/**
* \brief SHA-1 final digest
*
* \deprecated Superseded by mbedtls_sha1_finish_ext() in 2.5.0
*
* \param ctx SHA-1 context
* \param output SHA-1 checksum result
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha1_finish(
mbedtls_sha1_context *ctx,
unsigned char output[20] )
{
mbedtls_sha1_finish_ext( ctx, output );
}
/**
* \brief SHA-1 process data block (internal use only)
*
* \deprecated Superseded by mbedtls_sha1_process_ext() in 2.5.0
*
* \param ctx SHA-1 context
* \param data buffer holding one block of data
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha1_process(
mbedtls_sha1_context *ctx,
const unsigned char data[64] )
{
mbedtls_sha1_process_ext( ctx, data );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -119,8 +210,37 @@ extern "C" {
* \param input buffer holding the data * \param input buffer holding the data
* \param ilen length of the input data * \param ilen length of the input data
* \param output SHA-1 checksum result * \param output SHA-1 checksum result
*
* \return 0 if successful
*/ */
void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ); int mbedtls_sha1_ext( const unsigned char *input,
size_t ilen,
unsigned char output[20] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief Output = SHA-1( input buffer )
*
* \deprecated Superseded by mbedtls_sha1_ext() in 2.5.0
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-1 checksum result
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha1( const unsigned char *input,
size_t ilen,
unsigned char output[20] )
{
mbedtls_sha1_ext( input, ilen, output );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
/** /**
* \brief Checkup routine * \brief Checkup routine

View File

@ -97,7 +97,7 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
/* /*
* SHA-1 context setup * SHA-1 context setup
*/ */
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) int mbedtls_sha1_starts_ext( mbedtls_sha1_context *ctx )
{ {
ctx->total[0] = 0; ctx->total[0] = 0;
ctx->total[1] = 0; ctx->total[1] = 0;
@ -107,10 +107,13 @@ void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
ctx->state[2] = 0x98BADCFE; ctx->state[2] = 0x98BADCFE;
ctx->state[3] = 0x10325476; ctx->state[3] = 0x10325476;
ctx->state[4] = 0xC3D2E1F0; ctx->state[4] = 0xC3D2E1F0;
return( 0 );
} }
#if !defined(MBEDTLS_SHA1_PROCESS_ALT) #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ) int mbedtls_sha1_process_ext( mbedtls_sha1_context *ctx,
const unsigned char data[64] )
{ {
uint32_t temp, W[16], A, B, C, D, E; uint32_t temp, W[16], A, B, C, D, E;
@ -264,19 +267,24 @@ void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[6
ctx->state[2] += C; ctx->state[2] += C;
ctx->state[3] += D; ctx->state[3] += D;
ctx->state[4] += E; ctx->state[4] += E;
return( 0 );
} }
#endif /* !MBEDTLS_SHA1_PROCESS_ALT */ #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
/* /*
* SHA-1 process buffer * SHA-1 process buffer
*/ */
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ) int mbedtls_sha1_update_ext( mbedtls_sha1_context *ctx,
const unsigned char *input,
size_t ilen )
{ {
int ret;
size_t fill; size_t fill;
uint32_t left; uint32_t left;
if( ilen == 0 ) if( ilen == 0 )
return; return( 0 );
left = ctx->total[0] & 0x3F; left = ctx->total[0] & 0x3F;
fill = 64 - left; fill = 64 - left;
@ -290,7 +298,10 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
if( left && ilen >= fill ) if( left && ilen >= fill )
{ {
memcpy( (void *) (ctx->buffer + left), input, fill ); memcpy( (void *) (ctx->buffer + left), input, fill );
mbedtls_sha1_process( ctx, ctx->buffer );
if( ( ret = mbedtls_sha1_process_ext( ctx, ctx->buffer ) ) != 0 )
return( ret );
input += fill; input += fill;
ilen -= fill; ilen -= fill;
left = 0; left = 0;
@ -298,13 +309,17 @@ void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input,
while( ilen >= 64 ) while( ilen >= 64 )
{ {
mbedtls_sha1_process( ctx, input ); if( ( ret = mbedtls_sha1_process_ext( ctx, input ) ) != 0 )
return( ret );
input += 64; input += 64;
ilen -= 64; ilen -= 64;
} }
if( ilen > 0 ) if( ilen > 0 )
memcpy( (void *) (ctx->buffer + left), input, ilen ); memcpy( (void *) (ctx->buffer + left), input, ilen );
return( 0 );
} }
static const unsigned char sha1_padding[64] = static const unsigned char sha1_padding[64] =
@ -318,8 +333,10 @@ static const unsigned char sha1_padding[64] =
/* /*
* SHA-1 final digest * SHA-1 final digest
*/ */
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ) int mbedtls_sha1_finish_ext( mbedtls_sha1_context *ctx,
unsigned char output[20] )
{ {
int ret;
uint32_t last, padn; uint32_t last, padn;
uint32_t high, low; uint32_t high, low;
unsigned char msglen[8]; unsigned char msglen[8];
@ -334,14 +351,18 @@ void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
last = ctx->total[0] & 0x3F; last = ctx->total[0] & 0x3F;
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
mbedtls_sha1_update( ctx, sha1_padding, padn ); if( ( ret = mbedtls_sha1_update_ext( ctx, sha1_padding, padn ) ) != 0 )
mbedtls_sha1_update( ctx, msglen, 8 ); return( ret );
if( ( ret = mbedtls_sha1_update_ext( ctx, msglen, 8 ) ) != 0 )
return( ret );
PUT_UINT32_BE( ctx->state[0], output, 0 ); PUT_UINT32_BE( ctx->state[0], output, 0 );
PUT_UINT32_BE( ctx->state[1], output, 4 ); PUT_UINT32_BE( ctx->state[1], output, 4 );
PUT_UINT32_BE( ctx->state[2], output, 8 ); PUT_UINT32_BE( ctx->state[2], output, 8 );
PUT_UINT32_BE( ctx->state[3], output, 12 ); PUT_UINT32_BE( ctx->state[3], output, 12 );
PUT_UINT32_BE( ctx->state[4], output, 16 ); PUT_UINT32_BE( ctx->state[4], output, 16 );
return( 0 );
} }
#endif /* !MBEDTLS_SHA1_ALT */ #endif /* !MBEDTLS_SHA1_ALT */
@ -349,15 +370,27 @@ void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
/* /*
* output = SHA-1( input buffer ) * output = SHA-1( input buffer )
*/ */
void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ) int mbedtls_sha1_ext( const unsigned char *input,
size_t ilen,
unsigned char output[20] )
{ {
int ret;
mbedtls_sha1_context ctx; mbedtls_sha1_context ctx;
mbedtls_sha1_init( &ctx ); mbedtls_sha1_init( &ctx );
mbedtls_sha1_starts( &ctx );
mbedtls_sha1_update( &ctx, input, ilen ); if( ( ret = mbedtls_sha1_starts_ext( &ctx ) ) != 0 )
mbedtls_sha1_finish( &ctx, output ); return( ret );
if( ( ret = mbedtls_sha1_update_ext( &ctx, input, ilen ) ) != 0 )
return( ret );
if( ( ret = mbedtls_sha1_finish_ext( &ctx, output ) ) != 0 )
return( ret );
mbedtls_sha1_free( &ctx ); mbedtls_sha1_free( &ctx );
return( 0 );
} }
#if defined(MBEDTLS_SELF_TEST) #if defined(MBEDTLS_SELF_TEST)
@ -406,29 +439,30 @@ int mbedtls_sha1_self_test( int verbose )
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( " SHA-1 test #%d: ", i + 1 ); mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
mbedtls_sha1_starts( &ctx ); if( mbedtls_sha1_starts_ext( &ctx ) != 0 )
goto fail;
if( i == 2 ) if( i == 2 )
{ {
memset( buf, 'a', buflen = 1000 ); memset( buf, 'a', buflen = 1000 );
for( j = 0; j < 1000; j++ ) for( j = 0; j < 1000; j++ )
mbedtls_sha1_update( &ctx, buf, buflen ); {
if( mbedtls_sha1_update_ext( &ctx, buf, buflen ) != 0 )
goto fail;
}
} }
else else
mbedtls_sha1_update( &ctx, sha1_test_buf[i], {
sha1_test_buflen[i] ); if( mbedtls_sha1_update_ext( &ctx, sha1_test_buf[i],
sha1_test_buflen[i] ) != 0 )
goto fail;
}
mbedtls_sha1_finish( &ctx, sha1sum ); mbedtls_sha1_finish_ext( &ctx, sha1sum );
if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
{
if( verbose != 0 )
mbedtls_printf( "failed\n" );
ret = 1;
goto exit; goto exit;
}
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
@ -437,6 +471,14 @@ int mbedtls_sha1_self_test( int verbose )
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "\n" ); mbedtls_printf( "\n" );
goto exit;
fail:
if( verbose != 0 )
mbedtls_printf( "failed\n" );
ret = 1;
exit: exit:
mbedtls_sha1_free( &ctx ); mbedtls_sha1_free( &ctx );