Add int return values to RIPEMD-160 function calls

The following function calls are being deprecated to introduce int
return values.
    * mbedtls_ripemd160()
    * mbedtls_ripemd160_starts()
    * mbedtls_ripemd160_update()
    * mbedtls_ripemd160_finish()
    * mbedtls_ripemd160_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 10:59:46 +01:00
parent 2cfd7a982c
commit b1a8bf9725
2 changed files with 189 additions and 33 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_RIPEMD160_ALT) #if !defined(MBEDTLS_RIPEMD160_ALT)
// Regular implementation // Regular implementation
// //
@ -78,8 +83,10 @@ void mbedtls_ripemd160_clone( mbedtls_ripemd160_context *dst,
* \brief RIPEMD-160 context setup * \brief RIPEMD-160 context setup
* *
* \param ctx context to be initialized * \param ctx context to be initialized
*
* \return 0 if successful
*/ */
void mbedtls_ripemd160_starts( mbedtls_ripemd160_context *ctx ); int mbedtls_ripemd160_starts_ext( mbedtls_ripemd160_context *ctx );
/** /**
* \brief RIPEMD-160 process buffer * \brief RIPEMD-160 process buffer
@ -87,20 +94,103 @@ void mbedtls_ripemd160_starts( mbedtls_ripemd160_context *ctx );
* \param ctx RIPEMD-160 context * \param ctx RIPEMD-160 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_ripemd160_update( mbedtls_ripemd160_context *ctx, int mbedtls_ripemd160_update_ext( mbedtls_ripemd160_context *ctx,
const unsigned char *input, size_t ilen ); const unsigned char *input,
size_t ilen );
/** /**
* \brief RIPEMD-160 final digest * \brief RIPEMD-160 final digest
* *
* \param ctx RIPEMD-160 context * \param ctx RIPEMD-160 context
* \param output RIPEMD-160 checksum result * \param output RIPEMD-160 checksum result
*
* \return 0 if successful
*/ */
void mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx, unsigned char output[20] ); int mbedtls_ripemd160_finish_ext( mbedtls_ripemd160_context *ctx,
unsigned char output[20] );
/* Internal use */ /**
void mbedtls_ripemd160_process( mbedtls_ripemd160_context *ctx, const unsigned char data[64] ); * \brief RIPEMD-160 process data block (internal use only)
*
* \param ctx RIPEMD-160 context
* \param data buffer holding one block of data
*
* \return 0 if successful
*/
int mbedtls_ripemd160_process_ext( mbedtls_ripemd160_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 RIPEMD-160 context setup
*
* \deprecated Superseded by mbedtls_ripemd160_starts_ext() in 2.5.0
*
* \param ctx context to be initialized
*/
MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160_starts(
mbedtls_ripemd160_context *ctx )
{
mbedtls_ripemd160_starts_ext( ctx );
}
/**
* \brief RIPEMD-160 process buffer
*
* \deprecated Superseded by mbedtls_ripemd160_update_ext() in 2.5.0
*
* \param ctx RIPEMD-160 context
* \param input buffer holding the data
* \param ilen length of the input data
*/
MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160_update(
mbedtls_ripemd160_context *ctx,
const unsigned char *input,
size_t ilen )
{
mbedtls_ripemd160_update_ext( ctx, input, ilen );
}
/**
* \brief RIPEMD-160 final digest
*
* \deprecated Superseded by mbedtls_ripemd160_finish_ext() in 2.5.0
*
* \param ctx RIPEMD-160 context
* \param output RIPEMD-160 checksum result
*/
MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160_finish(
mbedtls_ripemd160_context *ctx,
unsigned char output[20] )
{
mbedtls_ripemd160_finish_ext( ctx, output );
}
/**
* \brief RIPEMD-160 process data block (internal use only)
*
* \deprecated Superseded by mbedtls_ripemd160_process_ext() in 2.5.0
*
* \param ctx RIPEMD-160 context
* \param data buffer holding one block of data
*/
MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160_process(
mbedtls_ripemd160_context *ctx,
const unsigned char data[64] )
{
mbedtls_ripemd160_process_ext( ctx, data );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -120,9 +210,38 @@ 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 RIPEMD-160 checksum result * \param output RIPEMD-160 checksum result
*
* \return 0 if successful
*/ */
void mbedtls_ripemd160( const unsigned char *input, size_t ilen, int mbedtls_ripemd160_ext( const unsigned char *input,
unsigned char output[20] ); 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 = RIPEMD-160( input buffer )
*
* \deprecated Superseded by mbedtls_ripemd160_ext() in 2.5.0
*
* \param input buffer holding the data
* \param ilen length of the input data
* \param output RIPEMD-160 checksum result
*/
MBEDTLS_DEPRECATED static inline void mbedtls_ripemd160(
const unsigned char *input,
size_t ilen,
unsigned char output[20] )
{
mbedtls_ripemd160_ext( input, ilen, output );
}
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
/** /**
* \brief Checkup routine * \brief Checkup routine

View file

@ -96,7 +96,7 @@ void mbedtls_ripemd160_clone( mbedtls_ripemd160_context *dst,
/* /*
* RIPEMD-160 context setup * RIPEMD-160 context setup
*/ */
void mbedtls_ripemd160_starts( mbedtls_ripemd160_context *ctx ) int mbedtls_ripemd160_starts_ext( mbedtls_ripemd160_context *ctx )
{ {
ctx->total[0] = 0; ctx->total[0] = 0;
ctx->total[1] = 0; ctx->total[1] = 0;
@ -106,13 +106,16 @@ void mbedtls_ripemd160_starts( mbedtls_ripemd160_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_RIPEMD160_PROCESS_ALT) #if !defined(MBEDTLS_RIPEMD160_PROCESS_ALT)
/* /*
* Process one block * Process one block
*/ */
void mbedtls_ripemd160_process( mbedtls_ripemd160_context *ctx, const unsigned char data[64] ) int mbedtls_ripemd160_process_ext( mbedtls_ripemd160_context *ctx,
const unsigned char data[64] )
{ {
uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16]; uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16];
@ -287,20 +290,24 @@ void mbedtls_ripemd160_process( mbedtls_ripemd160_context *ctx, const unsigned c
ctx->state[3] = ctx->state[4] + A + Bp; ctx->state[3] = ctx->state[4] + A + Bp;
ctx->state[4] = ctx->state[0] + B + Cp; ctx->state[4] = ctx->state[0] + B + Cp;
ctx->state[0] = C; ctx->state[0] = C;
return( 0 );
} }
#endif /* !MBEDTLS_RIPEMD160_PROCESS_ALT */ #endif /* !MBEDTLS_RIPEMD160_PROCESS_ALT */
/* /*
* RIPEMD-160 process buffer * RIPEMD-160 process buffer
*/ */
void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx, int mbedtls_ripemd160_update_ext( mbedtls_ripemd160_context *ctx,
const unsigned char *input, size_t ilen ) 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;
@ -314,7 +321,10 @@ void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx,
if( left && ilen >= fill ) if( left && ilen >= fill )
{ {
memcpy( (void *) (ctx->buffer + left), input, fill ); memcpy( (void *) (ctx->buffer + left), input, fill );
mbedtls_ripemd160_process( ctx, ctx->buffer );
if( ( ret = mbedtls_ripemd160_process_ext( ctx, ctx->buffer ) ) != 0 )
return( ret );
input += fill; input += fill;
ilen -= fill; ilen -= fill;
left = 0; left = 0;
@ -322,7 +332,9 @@ void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx,
while( ilen >= 64 ) while( ilen >= 64 )
{ {
mbedtls_ripemd160_process( ctx, input ); if( ( ret = mbedtls_ripemd160_process_ext( ctx, input ) ) != 0 )
return( ret );
input += 64; input += 64;
ilen -= 64; ilen -= 64;
} }
@ -331,6 +343,8 @@ void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx,
{ {
memcpy( (void *) (ctx->buffer + left), input, ilen ); memcpy( (void *) (ctx->buffer + left), input, ilen );
} }
return( 0 );
} }
static const unsigned char ripemd160_padding[64] = static const unsigned char ripemd160_padding[64] =
@ -344,8 +358,10 @@ static const unsigned char ripemd160_padding[64] =
/* /*
* RIPEMD-160 final digest * RIPEMD-160 final digest
*/ */
void mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx, unsigned char output[20] ) int mbedtls_ripemd160_finish_ext( mbedtls_ripemd160_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];
@ -360,29 +376,47 @@ void mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx, unsigned char out
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_ripemd160_update( ctx, ripemd160_padding, padn ); ret = mbedtls_ripemd160_update_ext( ctx, ripemd160_padding, padn );
mbedtls_ripemd160_update( ctx, msglen, 8 ); if( ret != 0 )
return( ret );
ret = mbedtls_ripemd160_update_ext( ctx, msglen, 8 );
if( ret != 0 )
return( ret );
PUT_UINT32_LE( ctx->state[0], output, 0 ); PUT_UINT32_LE( ctx->state[0], output, 0 );
PUT_UINT32_LE( ctx->state[1], output, 4 ); PUT_UINT32_LE( ctx->state[1], output, 4 );
PUT_UINT32_LE( ctx->state[2], output, 8 ); PUT_UINT32_LE( ctx->state[2], output, 8 );
PUT_UINT32_LE( ctx->state[3], output, 12 ); PUT_UINT32_LE( ctx->state[3], output, 12 );
PUT_UINT32_LE( ctx->state[4], output, 16 ); PUT_UINT32_LE( ctx->state[4], output, 16 );
return( 0 );
} }
/* /*
* output = RIPEMD-160( input buffer ) * output = RIPEMD-160( input buffer )
*/ */
void mbedtls_ripemd160( const unsigned char *input, size_t ilen, int mbedtls_ripemd160_ext( const unsigned char *input,
unsigned char output[20] ) size_t ilen,
unsigned char output[20] )
{ {
int ret;
mbedtls_ripemd160_context ctx; mbedtls_ripemd160_context ctx;
mbedtls_ripemd160_init( &ctx ); mbedtls_ripemd160_init( &ctx );
mbedtls_ripemd160_starts( &ctx );
mbedtls_ripemd160_update( &ctx, input, ilen ); if( ( ret = mbedtls_ripemd160_starts_ext( &ctx ) ) != 0 )
mbedtls_ripemd160_finish( &ctx, output ); return( ret );
if( ( ret = mbedtls_ripemd160_update_ext( &ctx, input, ilen ) ) != 0 )
return( ret );
if( ( ret = mbedtls_ripemd160_finish_ext( &ctx, output ) ) != 0 )
return( ret );
mbedtls_ripemd160_free( &ctx ); mbedtls_ripemd160_free( &ctx );
return( 0 );
} }
#if defined(MBEDTLS_SELF_TEST) #if defined(MBEDTLS_SELF_TEST)
@ -430,7 +464,7 @@ static const unsigned char ripemd160_test_md[TESTS][20] =
*/ */
int mbedtls_ripemd160_self_test( int verbose ) int mbedtls_ripemd160_self_test( int verbose )
{ {
int i; int i, ret;
unsigned char output[20]; unsigned char output[20];
memset( output, 0, sizeof output ); memset( output, 0, sizeof output );
@ -440,17 +474,14 @@ int mbedtls_ripemd160_self_test( int verbose )
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( " RIPEMD-160 test #%d: ", i + 1 ); mbedtls_printf( " RIPEMD-160 test #%d: ", i + 1 );
mbedtls_ripemd160( (const unsigned char *) ripemd160_test_input[i], ret = mbedtls_ripemd160_ext(
strlen( ripemd160_test_input[i] ), (const unsigned char *)ripemd160_test_input[i],
output ); strlen( ripemd160_test_input[i] ), output );
if( ret != 0 )
goto fail;
if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 ) if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 )
{ goto fail;
if( verbose != 0 )
mbedtls_printf( "failed\n" );
return( 1 );
}
if( verbose != 0 ) if( verbose != 0 )
mbedtls_printf( "passed\n" ); mbedtls_printf( "passed\n" );
@ -460,6 +491,12 @@ int mbedtls_ripemd160_self_test( int verbose )
mbedtls_printf( "\n" ); mbedtls_printf( "\n" );
return( 0 ); return( 0 );
fail:
if( verbose != 0 )
mbedtls_printf( "failed\n" );
return( 1 );
} }
#endif /* MBEDTLS_SELF_TEST */ #endif /* MBEDTLS_SELF_TEST */