diff --git a/include/polarssl/rmd160.h b/include/polarssl/rmd160.h index 5d6c6b7e9..c8fe119a6 100644 --- a/include/polarssl/rmd160.h +++ b/include/polarssl/rmd160.h @@ -52,6 +52,9 @@ typedef struct uint32_t total[2]; /*!< number of bytes processed */ uint32_t state[5]; /*!< intermediate digest state */ unsigned char buffer[64]; /*!< data block being processed */ + + unsigned char ipad[64]; /*!< HMAC: inner padding */ + unsigned char opad[64]; /*!< HMAC: outer padding */ } rmd160_context; @@ -78,7 +81,7 @@ void rmd160_update( rmd160_context *ctx, * \param ctx RMD160 context * \param output RMD160 checksum result */ -void rmd160_finish( rmd160_context *ctx, unsigned char output[16] ); +void rmd160_finish( rmd160_context *ctx, unsigned char output[20] ); /** * \brief Output = RMD160( input buffer ) @@ -88,7 +91,7 @@ void rmd160_finish( rmd160_context *ctx, unsigned char output[16] ); * \param output RMD160 checksum result */ void rmd160( const unsigned char *input, size_t ilen, - unsigned char output[16] ); + unsigned char output[20] ); #if defined(POLARSSL_FS_IO) /** @@ -99,9 +102,56 @@ void rmd160( const unsigned char *input, size_t ilen, * * \return 0 if successful, or POLARSSL_ERR_RMD160_FILE_IO_ERROR */ -int rmd160_file( const char *path, unsigned char output[16] ); +int rmd160_file( const char *path, unsigned char output[20] ); #endif /* POLARSSL_FS_IO */ +/** + * \brief RMD160 HMAC context setup + * + * \param ctx HMAC context to be initialized + * \param key HMAC secret key + * \param keylen length of the HMAC key + */ +void rmd160_hmac_starts( rmd160_context *ctx, + const unsigned char *key, size_t keylen ); + +/** + * \brief RMD160 HMAC process buffer + * + * \param ctx HMAC context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void rmd160_hmac_update( rmd160_context *ctx, + const unsigned char *input, size_t ilen ); + +/** + * \brief RMD160 HMAC final digest + * + * \param ctx HMAC context + * \param output RMD160 HMAC checksum result + */ +void rmd160_hmac_finish( rmd160_context *ctx, unsigned char output[20] ); + +/** + * \brief RMD160 HMAC context reset + * + * \param ctx HMAC context to be reset + */ +void rmd160_hmac_reset( rmd160_context *ctx ); + +/** + * \brief Output = HMAC-RMD160( hmac key, input buffer ) + * + * \param key HMAC secret key + * \param keylen length of the HMAC key + * \param input buffer holding the data + * \param ilen length of the input data + * \param output HMAC-RMD160 result + */ +void rmd160_hmac( const unsigned char *key, size_t keylen, + const unsigned char *input, size_t ilen, + unsigned char output[20] ); /** * \brief Checkup routine diff --git a/library/rmd160.c b/library/rmd160.c index cdf85dbd2..515ed1c23 100644 --- a/library/rmd160.c +++ b/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, 0xfb, 0x67, 0xd6 }, + { 0xf8, 0x36, 0x62, 0xcc, 0x8d, 0x33, 0x9c, 0x22, 0x7e, 0x60, + 0x0f, 0xcd, 0x63, 0x6c, 0x57, 0xd2, 0x57, 0x1b, 0x1c, 0x34 }, + { 0x84, 0x3d, 0x1c, 0x4e, 0xb8, 0x80, 0xac, 0x8a, 0xc0, 0xc9, + 0xc9, 0x56, 0x96, 0x50, 0x79, 0x57, 0xd0, 0x15, 0x5d, 0xdb }, + { 0x60, 0xf5, 0xef, 0x19, 0x8a, 0x2d, 0xd5, 0x74, 0x55, 0x45, + 0xc1, 0xf0, 0xc4, 0x7a, 0xa3, 0xfb, 0x57, 0x76, 0xf8, 0x81 }, + { 0xe4, 0x9c, 0x13, 0x6a, 0x9e, 0x56, 0x27, 0xe0, 0x68, 0x1b, + 0x80, 0x8a, 0x3b, 0x97, 0xe6, 0xa6, 0xe6, 0x61, 0xae, 0x79 }, + { 0x31, 0xbe, 0x3c, 0xc9, 0x8c, 0xee, 0x37, 0xb7, 0x9b, 0x06, + 0x19, 0xe3, 0xe1, 0xc2, 0xbe, 0x4f, 0x1a, 0xa5, 0x6e, 0x6c }, + }, + { + { 0xfe, 0x69, 0xa6, 0x6c, 0x74, 0x23, 0xee, 0xa9, 0xc8, 0xfa, + 0x2e, 0xff, 0x8d, 0x9d, 0xaf, 0xb4, 0xf1, 0x7a, 0x62, 0xf5 }, + { 0x85, 0x74, 0x3e, 0x89, 0x9b, 0xc8, 0x2d, 0xbf, 0xa3, 0x6f, + 0xaa, 0xa7, 0xa2, 0x5b, 0x7c, 0xfd, 0x37, 0x24, 0x32, 0xcd }, + { 0x6e, 0x4a, 0xfd, 0x50, 0x1f, 0xa6, 0xb4, 0xa1, 0x82, 0x3c, + 0xa3, 0xb1, 0x0b, 0xd9, 0xaa, 0x0b, 0xa9, 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 ); } diff --git a/tests/suites/test_suite_mdx.data b/tests/suites/test_suite_mdx.data index 72325d485..869ae966b 100644 --- a/tests/suites/test_suite_mdx.data +++ b/tests/suites/test_suite_mdx.data @@ -142,6 +142,27 @@ md4_hmac:16:"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":"4869205468657265":"5570c HMAC-MD5 Bouncy Castle test #1 md5_hmac:16:"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":"4869205468657265":"5ccec34ea9656392457fa1ac27f08fbc" +HMAC-RMD160 Test vector RFC 2286 #1 +rmd160_hmac:20:"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b":"4869205468657265":"24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668" + +HMAC-RMD160 Test vector RFC 2286 #2 +rmd160_hmac:20:"4a656665":"7768617420646f2079612077616e7420666f72206e6f7468696e673f":"dda6c0213a485a9e24f4742064a7f033b43c4069" + +HMAC-RMD160 Test vector RFC 2286 #3 +rmd160_hmac:20:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd":"b0b105360de759960ab4f35298e116e295d8e7c1" + +HMAC-RMD160 Test vector RFC 2286 #4 +rmd160_hmac:20:"0102030405060708090a0b0c0d0e0f10111213141516171819":"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd":"d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4" + +HMAC-RMD160 Test vector RFC 2286 #5 +rmd160_hmac:20:"0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c":"546573742057697468205472756e636174696f6e":"7619693978f91d90539ae786500ff3d8e0518e39" + +HMAC-RMD160 Test vector RFC 2286 #6 +rmd160_hmac:20:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":"54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374":"6466ca07ac5eac29e1bd523e5ada7605b791fd8b" + +HMAC-RMD160 Test vector RFC 2286 #7 +rmd160_hmac:20:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":"54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b657920616e64204c6172676572205468616e204f6e6520426c6f636b2d53697a652044617461":"69ea60798d71616cce5fd0871e23754cd75d5a0a" + MD2 Hash file #1 md2_file:"data_files/hash_file_1":"b593c098712d2e21628c8986695451a8" diff --git a/tests/suites/test_suite_mdx.function b/tests/suites/test_suite_mdx.function index 5c0105697..6f43aac2d 100644 --- a/tests/suites/test_suite_mdx.function +++ b/tests/suites/test_suite_mdx.function @@ -160,6 +160,31 @@ void md5_hmac( int trunc_size, char *hex_key_string, char *hex_src_string, } /* END_CASE */ +/* BEGIN_CASE depends_on:POLARSSL_RMD160_C */ +void rmd160_hmac( int trunc_size, char *hex_key_string, char *hex_src_string, + char *hex_hash_string ) +{ + unsigned char src_str[200]; + unsigned char key_str[200]; + unsigned char hash_str[41]; + unsigned char output[20]; + int key_len, src_len; + + memset( src_str, 0x00, sizeof src_str ); + memset( key_str, 0x00, sizeof key_str ); + memset( hash_str, 0x00, sizeof hash_str ); + memset( output, 0x00, sizeof output ); + + key_len = unhexify( key_str, hex_key_string ); + src_len = unhexify( src_str, hex_src_string ); + + rmd160_hmac( key_str, key_len, src_str, src_len, output ); + hexify( hash_str, output, sizeof output ); + + TEST_ASSERT( strncmp( (char *) hash_str, hex_hash_string, trunc_size * 2 ) == 0 ); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:POLARSSL_MD2_C:POLARSSL_FS_IO */ void md2_file( char *filename, char *hex_hash_string ) {