Add int return values to SHA-512 function calls

The following function calls are being deprecated to introduce int
return values.
    * mbedtls_sha512()
    * mbedtls_sha512_starts()
    * mbedtls_sha512_update()
    * mbedtls_sha512_finish()
    * mbedtls_sha512_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-05-02 12:07:26 +01:00
parent 72a7f53064
commit 614c689e05
2 changed files with 201 additions and 36 deletions

View File

@ -32,6 +32,11 @@
#include <stddef.h>
#include <stdint.h>
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
!defined(inline) && !defined(__cplusplus)
#define inline __inline
#endif
#if !defined(MBEDTLS_SHA512_ALT)
// Regular implementation
//
@ -80,8 +85,10 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
*
* \param ctx context to be initialized
* \param is384 0 = use SHA512, 1 = use SHA384
*
* \return 0 if successful
*/
void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 );
int mbedtls_sha512_starts_ext( mbedtls_sha512_context *ctx, int is384 );
/**
* \brief SHA-512 process buffer
@ -89,8 +96,11 @@ void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 );
* \param ctx SHA-512 context
* \param input buffer holding the data
* \param ilen length of the input data
*
* \return 0 if successful
*/
void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input,
int mbedtls_sha512_update_ext( mbedtls_sha512_context *ctx,
const unsigned char *input,
size_t ilen );
/**
@ -98,8 +108,93 @@ void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *in
*
* \param ctx SHA-512 context
* \param output SHA-384/512 checksum result
*
* \return 0 if successful
*/
void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64] );
int mbedtls_sha512_finish_ext( mbedtls_sha512_context *ctx,
unsigned char output[64] );
/**
* \brief SHA-512 process data block (internal use only)
*
* \param ctx SHA-512 context
* \param data buffer holding one block of data
*
* \return 0 if successful
*/
int mbedtls_sha512_process_ext( mbedtls_sha512_context *ctx,
const unsigned char data[128] );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief SHA-512 context setup
*
* \deprecated Superseded by mbedtls_sha512_starts_ext() in 2.5.0
*
* \param ctx context to be initialized
* \param is384 0 = use SHA512, 1 = use SHA384
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512_starts(
mbedtls_sha512_context *ctx,
int is384 )
{
mbedtls_sha512_starts_ext( ctx, is384 );
}
/**
* \brief SHA-512 process buffer
*
* \deprecated Superseded by mbedtls_sha512_update_ext() in 2.5.0
*
* \param ctx SHA-512 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512_update(
mbedtls_sha512_context *ctx,
const unsigned char *input,
size_t ilen )
{
mbedtls_sha512_update_ext( ctx, input, ilen );
}
/**
* \brief SHA-512 final digest
*
* \deprecated Superseded by mbedtls_sha512_finish_ext() in 2.5.0
*
* \param ctx SHA-512 context
* \param output SHA-384/512 checksum result
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512_finish(
mbedtls_sha512_context *ctx,
unsigned char output[64] )
{
mbedtls_sha512_finish_ext( ctx, output );
}
/**
* \brief SHA-512 process data block (internal use only)
*
* \deprecated Superseded by mbedtls_sha512_process_ext() in 2.5.0
*
* \param ctx SHA-512 context
* \param data buffer holding one block of data
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512_process(
mbedtls_sha512_context *ctx,
const unsigned char data[128] )
{
mbedtls_sha512_process_ext( ctx, data );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#ifdef __cplusplus
}
@ -120,9 +215,41 @@ extern "C" {
* \param ilen length of the input data
* \param output SHA-384/512 checksum result
* \param is384 0 = use SHA512, 1 = use SHA384
*
* \return 0 if successful
*/
void mbedtls_sha512( const unsigned char *input, size_t ilen,
unsigned char output[64], int is384 );
int mbedtls_sha512_ext( const unsigned char *input,
size_t ilen,
unsigned char output[64],
int is384 );
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
/**
* \brief Output = SHA-512( input buffer )
*
* \deprecated Superseded by mbedtls_sha512_ext() in 2.5.0
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output SHA-384/512 checksum result
* \param is384 0 = use SHA512, 1 = use SHA384
*/
MBEDTLS_DEPRECATED static inline void mbedtls_sha512(
const unsigned char *input,
size_t ilen,
unsigned char output[64],
int is384 )
{
mbedtls_sha512_ext( input, ilen, output, is384 );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
/**
* \brief Checkup routine
@ -131,9 +258,6 @@ void mbedtls_sha512( const unsigned char *input, size_t ilen,
*/
int mbedtls_sha512_self_test( int verbose );
/* Internal use */
void mbedtls_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] );
#ifdef __cplusplus
}
#endif

View File

@ -114,7 +114,7 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
/*
* SHA-512 context setup
*/
void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 )
int mbedtls_sha512_starts_ext( mbedtls_sha512_context *ctx, int is384 )
{
ctx->total[0] = 0;
ctx->total[1] = 0;
@ -145,6 +145,8 @@ void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 )
}
ctx->is384 = is384;
return( 0 );
}
#if !defined(MBEDTLS_SHA512_PROCESS_ALT)
@ -196,7 +198,8 @@ static const uint64_t K[80] =
UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817)
};
void mbedtls_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
int mbedtls_sha512_process_ext( mbedtls_sha512_context *ctx,
const unsigned char data[128] )
{
int i;
uint64_t temp1, temp2, W[80];
@ -263,20 +266,24 @@ void mbedtls_sha512_process( mbedtls_sha512_context *ctx, const unsigned char da
ctx->state[5] += F;
ctx->state[6] += G;
ctx->state[7] += H;
return( 0 );
}
#endif /* !MBEDTLS_SHA512_PROCESS_ALT */
/*
* SHA-512 process buffer
*/
void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input,
int mbedtls_sha512_update_ext( mbedtls_sha512_context *ctx,
const unsigned char *input,
size_t ilen )
{
int ret;
size_t fill;
unsigned int left;
if( ilen == 0 )
return;
return( 0 );
left = (unsigned int) (ctx->total[0] & 0x7F);
fill = 128 - left;
@ -289,7 +296,10 @@ void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *in
if( left && ilen >= fill )
{
memcpy( (void *) (ctx->buffer + left), input, fill );
mbedtls_sha512_process( ctx, ctx->buffer );
if( ( ret = mbedtls_sha512_process_ext( ctx, ctx->buffer ) ) != 0 )
return( ret );
input += fill;
ilen -= fill;
left = 0;
@ -297,13 +307,17 @@ void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *in
while( ilen >= 128 )
{
mbedtls_sha512_process( ctx, input );
if( ( ret = mbedtls_sha512_process_ext( ctx, input ) ) != 0 )
return( ret );
input += 128;
ilen -= 128;
}
if( ilen > 0 )
memcpy( (void *) (ctx->buffer + left), input, ilen );
return( 0 );
}
static const unsigned char sha512_padding[128] =
@ -321,8 +335,10 @@ static const unsigned char sha512_padding[128] =
/*
* SHA-512 final digest
*/
void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64] )
int mbedtls_sha512_finish_ext( mbedtls_sha512_context *ctx,
unsigned char output[64] )
{
int ret;
size_t last, padn;
uint64_t high, low;
unsigned char msglen[16];
@ -337,8 +353,11 @@ void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64
last = (size_t)( ctx->total[0] & 0x7F );
padn = ( last < 112 ) ? ( 112 - last ) : ( 240 - last );
mbedtls_sha512_update( ctx, sha512_padding, padn );
mbedtls_sha512_update( ctx, msglen, 16 );
if( ( ret = mbedtls_sha512_update_ext( ctx, sha512_padding, padn ) ) != 0 )
return( ret );
if( ( ret = mbedtls_sha512_update_ext( ctx, msglen, 16 ) ) != 0 )
return( ret );
PUT_UINT64_BE( ctx->state[0], output, 0 );
PUT_UINT64_BE( ctx->state[1], output, 8 );
@ -352,6 +371,8 @@ void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64
PUT_UINT64_BE( ctx->state[6], output, 48 );
PUT_UINT64_BE( ctx->state[7], output, 56 );
}
return( 0 );
}
#endif /* !MBEDTLS_SHA512_ALT */
@ -359,16 +380,28 @@ void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64
/*
* output = SHA-512( input buffer )
*/
void mbedtls_sha512( const unsigned char *input, size_t ilen,
unsigned char output[64], int is384 )
int mbedtls_sha512_ext( const unsigned char *input,
size_t ilen,
unsigned char output[64],
int is384 )
{
int ret;
mbedtls_sha512_context ctx;
mbedtls_sha512_init( &ctx );
mbedtls_sha512_starts( &ctx, is384 );
mbedtls_sha512_update( &ctx, input, ilen );
mbedtls_sha512_finish( &ctx, output );
if( ( ret = mbedtls_sha512_starts_ext( &ctx, is384 ) ) != 0 )
return( ret );
if( ( ret = mbedtls_sha512_update_ext( &ctx, input, ilen ) ) != 0 )
return( ret );
if( ( ret = mbedtls_sha512_finish_ext( &ctx, output ) ) != 0 )
return( ret );
mbedtls_sha512_free( &ctx );
return( 0 );
}
#if defined(MBEDTLS_SELF_TEST)
@ -471,29 +504,29 @@ int mbedtls_sha512_self_test( int verbose )
if( verbose != 0 )
mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 );
mbedtls_sha512_starts( &ctx, k );
if( mbedtls_sha512_starts_ext( &ctx, k ) != 0 )
goto fail;
if( j == 2 )
{
memset( buf, 'a', buflen = 1000 );
for( j = 0; j < 1000; j++ )
mbedtls_sha512_update( &ctx, buf, buflen );
if( mbedtls_sha512_update_ext( &ctx, buf, buflen ) != 0 )
goto fail;
}
else
mbedtls_sha512_update( &ctx, sha512_test_buf[j],
sha512_test_buflen[j] );
{
if( mbedtls_sha512_update_ext( &ctx, sha512_test_buf[j],
sha512_test_buflen[j] ) != 0 )
goto fail;
}
mbedtls_sha512_finish( &ctx, sha512sum );
if( mbedtls_sha512_finish_ext( &ctx, sha512sum ) != 0 )
goto fail;
if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 )
{
if( verbose != 0 )
mbedtls_printf( "failed\n" );
ret = 1;
goto exit;
}
goto fail;
if( verbose != 0 )
mbedtls_printf( "passed\n" );
@ -502,6 +535,14 @@ int mbedtls_sha512_self_test( int verbose )
if( verbose != 0 )
mbedtls_printf( "\n" );
goto exit;
fail:
if( verbose != 0 )
mbedtls_printf( "failed\n" );
ret = 1;
exit:
mbedtls_sha512_free( &ctx );
mbedtls_free( buf );