From 79afaa055194fba5e8e810472caa30dc883afc7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 31 Jan 2014 11:12:09 +0100 Subject: [PATCH] Add hmac_drbg_selftest() --- library/hmac_drbg.c | 111 +++++++++++++++++++- programs/test/selftest.c | 8 +- tests/suites/test_suite_hmac_drbg.function | 6 ++ tests/suites/test_suite_hmac_drbg.misc.data | 2 + 4 files changed, 123 insertions(+), 4 deletions(-) diff --git a/library/hmac_drbg.c b/library/hmac_drbg.c index cfeb8a4c8..3495d4979 100644 --- a/library/hmac_drbg.c +++ b/library/hmac_drbg.c @@ -352,9 +352,8 @@ int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path ) #include -/* - * Checkup routine - */ +#if !defined(POLARSSL_SHA1_C) +/* Dummy checkup routine */ int hmac_drbg_self_test( int verbose ) { @@ -363,6 +362,112 @@ int hmac_drbg_self_test( int verbose ) return( 0 ); } +#else + +#define OUTPUT_LEN 80 + +/* From a NIST PR=true test vector */ +static unsigned char entropy_pr[] = { + 0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f, + 0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11, + 0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42, + 0x17, 0x60, 0x99, 0xd4, 0x5e, 0x13, 0x97, 0xdc, 0x40, 0x4d, 0x86, 0xa3, + 0x7b, 0xf5, 0x59, 0x54, 0x75, 0x69, 0x51, 0xe4 }; +static const unsigned char result_pr[OUTPUT_LEN] = { + 0x9a, 0x00, 0xa2, 0xd0, 0x0e, 0xd5, 0x9b, 0xfe, 0x31, 0xec, 0xb1, 0x39, + 0x9b, 0x60, 0x81, 0x48, 0xd1, 0x96, 0x9d, 0x25, 0x0d, 0x3c, 0x1e, 0x94, + 0x10, 0x10, 0x98, 0x12, 0x93, 0x25, 0xca, 0xb8, 0xfc, 0xcc, 0x2d, 0x54, + 0x73, 0x19, 0x70, 0xc0, 0x10, 0x7a, 0xa4, 0x89, 0x25, 0x19, 0x95, 0x5e, + 0x4b, 0xc6, 0x00, 0x1d, 0x7f, 0x4e, 0x6a, 0x2b, 0xf8, 0xa3, 0x01, 0xab, + 0x46, 0x05, 0x5c, 0x09, 0xa6, 0x71, 0x88, 0xf1, 0xa7, 0x40, 0xee, 0xf3, + 0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44 }; + +/* From a NIST PR=false test vector */ +static unsigned char entropy_nopr[] = { + 0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66, + 0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8, + 0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3, + 0xe9, 0x9d, 0xfe, 0xdf }; +static const unsigned char result_nopr[OUTPUT_LEN] = { + 0xc6, 0xa1, 0x6a, 0xb8, 0xd4, 0x20, 0x70, 0x6f, 0x0f, 0x34, 0xab, 0x7f, + 0xec, 0x5a, 0xdc, 0xa9, 0xd8, 0xca, 0x3a, 0x13, 0x3e, 0x15, 0x9c, 0xa6, + 0xac, 0x43, 0xc6, 0xf8, 0xa2, 0xbe, 0x22, 0x83, 0x4a, 0x4c, 0x0a, 0x0a, + 0xff, 0xb1, 0x0d, 0x71, 0x94, 0xf1, 0xc1, 0xa5, 0xcf, 0x73, 0x22, 0xec, + 0x1a, 0xe0, 0x96, 0x4e, 0xd4, 0xbf, 0x12, 0x27, 0x46, 0xe0, 0x87, 0xfd, + 0xb5, 0xb3, 0xe9, 0x1b, 0x34, 0x93, 0xd5, 0xbb, 0x98, 0xfa, 0xed, 0x49, + 0xe8, 0x5f, 0x13, 0x0f, 0xc8, 0xa4, 0x59, 0xb7 }; + +/* "Entropy" from buffer */ +static int test_offset; +static int hmac_drbg_self_test_entropy( void *data, + unsigned char *buf, size_t len ) +{ + const unsigned char *p = data; + memcpy( buf, p + test_offset, len ); + test_offset += len; + return( 0 ); +} + +#define CHK( c ) if( (c) != 0 ) \ + { \ + if( verbose != 0 ) \ + printf( "failed\n" ); \ + return( 1 ); \ + } + +/* + * Checkup routine for HMAC_DRBG with SHA-1 + */ +int hmac_drbg_self_test( int verbose ) +{ + hmac_drbg_context ctx; + unsigned char buf[OUTPUT_LEN]; + const md_info_t *md_info = md_info_from_type( POLARSSL_MD_SHA1 ); + + /* + * PR = True + */ + if( verbose != 0 ) + printf( " HMAC_DRBG (PR = True) : " ); + + test_offset = 0; + CHK( hmac_drbg_init( &ctx, md_info, + hmac_drbg_self_test_entropy, entropy_pr, + NULL, 0 ) ); + hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON ); + CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); + CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); + CHK( memcmp( buf, result_pr, OUTPUT_LEN ) ); + hmac_drbg_free( &ctx ); + + if( verbose != 0 ) + printf( "passed\n" ); + + /* + * PR = False + */ + if( verbose != 0 ) + printf( " HMAC_DRBG (PR = False) : " ); + + test_offset = 0; + CHK( hmac_drbg_init( &ctx, md_info, + hmac_drbg_self_test_entropy, entropy_nopr, + NULL, 0 ) ); + CHK( hmac_drbg_reseed( &ctx, NULL, 0 ) ); + CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); + CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); + CHK( memcmp( buf, result_nopr, OUTPUT_LEN ) ); + hmac_drbg_free( &ctx ); + + if( verbose != 0 ) + printf( "passed\n" ); + + if( verbose != 0 ) + printf( "\n" ); + + return( 0 ); +} +#endif /* POLARSSL_SHA1_C */ #endif /* POLARSSL_SELF_TEST */ #endif /* POLARSSL_HMAC_DRBG_C */ diff --git a/programs/test/selftest.c b/programs/test/selftest.c index 8a8a82004..eff7f1b65 100644 --- a/programs/test/selftest.c +++ b/programs/test/selftest.c @@ -28,6 +28,7 @@ #include #include +#include "polarssl/hmac_drbg.h" #include "polarssl/ctr_drbg.h" #include "polarssl/dhm.h" #include "polarssl/gcm.h" @@ -140,7 +141,7 @@ int main( int argc, char *argv[] ) return( ret ); #endif -#if defined(POLARSSL_RSA_C) && defined(POLARSSL_BIGNUM_C) +#if defined(POLARSSL_RSA_C) if( ( ret = rsa_self_test( v ) ) != 0 ) return( ret ); #endif @@ -165,6 +166,11 @@ int main( int argc, char *argv[] ) return( ret ); #endif +#if defined(POLARSSL_HMAC_DRBG_C) + if( ( ret = hmac_drbg_self_test( v ) ) != 0 ) + return( ret ); +#endif + #if defined(POLARSSL_PBKDF2_C) if( ( ret = pbkdf2_self_test( v ) ) != 0 ) return( ret ); diff --git a/tests/suites/test_suite_hmac_drbg.function b/tests/suites/test_suite_hmac_drbg.function index 0699f082c..0882696d0 100644 --- a/tests/suites/test_suite_hmac_drbg.function +++ b/tests/suites/test_suite_hmac_drbg.function @@ -269,3 +269,9 @@ void hmac_drbg_pr( int md_alg, } /* END_CASE */ +/* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ +void hmac_drbg_selftest( ) +{ + TEST_ASSERT( hmac_drbg_self_test( 0 ) == 0 ); +} +/* END_CASE */ diff --git a/tests/suites/test_suite_hmac_drbg.misc.data b/tests/suites/test_suite_hmac_drbg.misc.data index 63758d910..340a47a6e 100644 --- a/tests/suites/test_suite_hmac_drbg.misc.data +++ b/tests/suites/test_suite_hmac_drbg.misc.data @@ -78,3 +78,5 @@ HMAC_DRBG from buffer SHA-512 depends_on:POLARSSL_SHA512_C hmac_drbg_buf:POLARSSL_MD_SHA512 +HMAC_DRBG self test +hmac_drbg_selftest: