Add HMAC support to RIPEMD-160
This commit is contained in:
parent
1744d72902
commit
ff40c3ac34
4 changed files with 268 additions and 17 deletions
183
library/rmd160.c
183
library/rmd160.c
|
@ -312,7 +312,7 @@ static const unsigned char rmd160_padding[64] =
|
|||
/*
|
||||
* RMD160 final digest
|
||||
*/
|
||||
void rmd160_finish( rmd160_context *ctx, unsigned char output[16] )
|
||||
void rmd160_finish( rmd160_context *ctx, unsigned char output[20] )
|
||||
{
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
|
@ -341,7 +341,7 @@ void rmd160_finish( rmd160_context *ctx, unsigned char output[16] )
|
|||
/*
|
||||
* output = RMD160( input buffer )
|
||||
*/
|
||||
void rmd160( const unsigned char *input, size_t ilen, unsigned char output[16] )
|
||||
void rmd160( const unsigned char *input, size_t ilen, unsigned char output[20] )
|
||||
{
|
||||
rmd160_context ctx;
|
||||
|
||||
|
@ -356,7 +356,7 @@ void rmd160( const unsigned char *input, size_t ilen, unsigned char output[16] )
|
|||
/*
|
||||
* output = RMD160( file contents )
|
||||
*/
|
||||
int rmd160_file( const char *path, unsigned char output[16] )
|
||||
int rmd160_file( const char *path, unsigned char output[20] )
|
||||
{
|
||||
FILE *f;
|
||||
size_t n;
|
||||
|
@ -386,12 +386,97 @@ int rmd160_file( const char *path, unsigned char output[16] )
|
|||
}
|
||||
#endif /* POLARSSL_FS_IO */
|
||||
|
||||
|
||||
/*
|
||||
* RMD160 HMAC context setup
|
||||
*/
|
||||
void rmd160_hmac_starts( rmd160_context *ctx,
|
||||
const unsigned char *key, size_t keylen )
|
||||
{
|
||||
size_t i;
|
||||
unsigned char sum[20];
|
||||
|
||||
if( keylen > 64 )
|
||||
{
|
||||
rmd160( key, keylen, sum );
|
||||
keylen = 20;
|
||||
key = sum;
|
||||
}
|
||||
|
||||
memset( ctx->ipad, 0x36, 64 );
|
||||
memset( ctx->opad, 0x5C, 64 );
|
||||
|
||||
for( i = 0; i < keylen; i++ )
|
||||
{
|
||||
ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
|
||||
ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
|
||||
}
|
||||
|
||||
rmd160_starts( ctx );
|
||||
rmd160_update( ctx, ctx->ipad, 64 );
|
||||
|
||||
memset( sum, 0, sizeof( sum ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* RMD160 HMAC process buffer
|
||||
*/
|
||||
void rmd160_hmac_update( rmd160_context *ctx,
|
||||
const unsigned char *input, size_t ilen )
|
||||
{
|
||||
rmd160_update( ctx, input, ilen );
|
||||
}
|
||||
|
||||
/*
|
||||
* RMD160 HMAC final digest
|
||||
*/
|
||||
void rmd160_hmac_finish( rmd160_context *ctx, unsigned char output[20] )
|
||||
{
|
||||
unsigned char tmpbuf[20];
|
||||
|
||||
rmd160_finish( ctx, tmpbuf );
|
||||
rmd160_starts( ctx );
|
||||
rmd160_update( ctx, ctx->opad, 64 );
|
||||
rmd160_update( ctx, tmpbuf, 20 );
|
||||
rmd160_finish( ctx, output );
|
||||
|
||||
memset( tmpbuf, 0, sizeof( tmpbuf ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* RMD160 HMAC context reset
|
||||
*/
|
||||
void rmd160_hmac_reset( rmd160_context *ctx )
|
||||
{
|
||||
rmd160_starts( ctx );
|
||||
rmd160_update( ctx, ctx->ipad, 64 );
|
||||
}
|
||||
|
||||
/*
|
||||
* output = HMAC-RMD160( hmac key, input buffer )
|
||||
*/
|
||||
void rmd160_hmac( const unsigned char *key, size_t keylen,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char output[20] )
|
||||
{
|
||||
rmd160_context ctx;
|
||||
|
||||
rmd160_hmac_starts( &ctx, key, keylen );
|
||||
rmd160_hmac_update( &ctx, input, ilen );
|
||||
rmd160_hmac_finish( &ctx, output );
|
||||
|
||||
memset( &ctx, 0, sizeof( rmd160_context ) );
|
||||
}
|
||||
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
/*
|
||||
* Test vectors from the RIPEMD-160 paper
|
||||
* Test vectors from the RIPEMD-160 paper and
|
||||
* http://homes.esat.kuleuven.be/~bosselae/ripemd160.html#HMAC
|
||||
*/
|
||||
#define MD_TESTS 8
|
||||
static const char *rmd160_test_input[MD_TESTS] =
|
||||
#define TESTS 8
|
||||
#define KEYS 2
|
||||
static const char *rmd160_test_input[TESTS] =
|
||||
{
|
||||
"",
|
||||
"a",
|
||||
|
@ -404,7 +489,7 @@ static const char *rmd160_test_input[MD_TESTS] =
|
|||
"1234567890123456789012345678901234567890",
|
||||
};
|
||||
|
||||
static const unsigned char rmd160_test_output[MD_TESTS][20] =
|
||||
static const unsigned char rmd160_test_md[TESTS][20] =
|
||||
{
|
||||
{ 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, 0x61, 0x28,
|
||||
0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, 0xb2, 0x25, 0x8d, 0x31 },
|
||||
|
@ -424,26 +509,74 @@ static const unsigned char rmd160_test_output[MD_TESTS][20] =
|
|||
0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, 0x63, 0x32, 0x6b, 0xfb },
|
||||
};
|
||||
|
||||
static const unsigned char rmd160_test_hmac[KEYS][TESTS][20] =
|
||||
{
|
||||
{
|
||||
{ 0xcf, 0x38, 0x76, 0x77, 0xbf, 0xda, 0x84, 0x83, 0xe6, 0x3b,
|
||||
0x57, 0xe0, 0x6c, 0x3b, 0x5e, 0xcd, 0x8b, 0x7f, 0xc0, 0x55 },
|
||||
{ 0x0d, 0x35, 0x1d, 0x71, 0xb7, 0x8e, 0x36, 0xdb, 0xb7, 0x39,
|
||||
0x1c, 0x81, 0x0a, 0x0d, 0x2b, 0x62, 0x40, 0xdd, 0xba, 0xfc },
|
||||
{ 0xf7, 0xef, 0x28, 0x8c, 0xb1, 0xbb, 0xcc, 0x61, 0x60, 0xd7,
|
||||
0x65, 0x07, 0xe0, 0xa3, 0xbb, 0xf7, 0x12,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a9, 0x7b, 0xa1, 0x82 },
|
||||
{ 0x2e, 0x06, 0x6e, 0x62, 0x4b, 0xad, 0xb7, 0x6a, 0x18, 0x4c,
|
||||
0x8f, 0x90, 0xfb, 0xa0, 0x53, 0x33, 0x0e, 0x65, 0x0e, 0x92 },
|
||||
{ 0x07, 0xe9, 0x42, 0xaa, 0x4e, 0x3c, 0xd7, 0xc0, 0x4d, 0xed,
|
||||
0xc1, 0xd4, 0x6e, 0x2e, 0x8c, 0xc4, 0xc7, 0x41, 0xb3, 0xd9 },
|
||||
{ 0xb6, 0x58, 0x23, 0x18, 0xdd, 0xcf, 0xb6, 0x7a, 0x53, 0xa6,
|
||||
0x7d, 0x67, 0x6b, 0x8a, 0xd8, 0x69, 0xad, 0xed, 0x62, 0x9a },
|
||||
{ 0xf1, 0xbe, 0x3e, 0xe8, 0x77, 0x70, 0x31, 0x40, 0xd3, 0x4f,
|
||||
0x97, 0xea, 0x1a, 0xb3, 0xa0, 0x7c, 0x14, 0x13, 0x33, 0xe2 },
|
||||
{ 0x85, 0xf1, 0x64, 0x70, 0x3e, 0x61, 0xa6, 0x31, 0x31, 0xbe,
|
||||
0x7e, 0x45, 0x95, 0x8e, 0x07, 0x94, 0x12, 0x39, 0x04, 0xf9 },
|
||||
},
|
||||
};
|
||||
|
||||
static const unsigned char rmd160_test_key[KEYS][20] =
|
||||
{
|
||||
{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
|
||||
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x23, 0x45, 0x67 },
|
||||
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc,
|
||||
0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33 },
|
||||
};
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int rmd160_self_test( int verbose )
|
||||
{
|
||||
int i;
|
||||
int i, j;
|
||||
unsigned char output[20];
|
||||
|
||||
memset( output, 0, sizeof output );
|
||||
|
||||
for( i = 0; i < MD_TESTS; i++ )
|
||||
for( i = 0; i < TESTS; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( " RMD160 test #%d: ", i + 1 );
|
||||
|
||||
rmd160( (const unsigned char *)rmd160_test_input[i],
|
||||
rmd160( (const unsigned char *) rmd160_test_input[i],
|
||||
strlen( rmd160_test_input[i] ),
|
||||
output );
|
||||
|
||||
if( memcmp( output, rmd160_test_output[i], 20 ) != 0 )
|
||||
if( memcmp( output, rmd160_test_md[i], 20 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
|
@ -453,10 +586,32 @@ int rmd160_self_test( int verbose )
|
|||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
for( j = 0; j < KEYS; j++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( " HMAC-RMD160 test #%d, key #%d: ", i + 1, j + 1 );
|
||||
|
||||
rmd160_hmac( rmd160_test_key[j], 20,
|
||||
(const unsigned char *) rmd160_test_input[i],
|
||||
strlen( rmd160_test_input[i] ),
|
||||
output );
|
||||
|
||||
if( memcmp( output, rmd160_test_hmac[j][i], 20 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue