Clean up details in ctr_drbg_selftest()
diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c
index c2f78a4..99d120c 100644
--- a/library/ctr_drbg.c
+++ b/library/ctr_drbg.c
@@ -410,7 +410,7 @@
#include <stdio.h>
-unsigned char entropy_source_pr[96] =
+static unsigned char entropy_source_pr[96] =
{ 0xc1, 0x80, 0x81, 0xa6, 0x5d, 0x44, 0x02, 0x16,
0x19, 0xb3, 0xf1, 0x80, 0xb1, 0xc9, 0x20, 0x02,
0x6a, 0x54, 0x6f, 0x0c, 0x70, 0x81, 0x49, 0x8b,
@@ -424,7 +424,7 @@
0x93, 0x92, 0xcf, 0xc5, 0x23, 0x12, 0xd5, 0x56,
0x2c, 0x4a, 0x6e, 0xff, 0xdc, 0x10, 0xd0, 0x68 };
-unsigned char entropy_source_nopr[64] =
+static unsigned char entropy_source_nopr[64] =
{ 0x5a, 0x19, 0x4d, 0x5e, 0x2b, 0x31, 0x58, 0x14,
0x54, 0xde, 0xf6, 0x75, 0xfb, 0x79, 0x58, 0xfe,
0xc7, 0xdb, 0x87, 0x3e, 0x56, 0x89, 0xfc, 0x9d,
@@ -434,32 +434,39 @@
0xf5, 0x98, 0x3d, 0x77, 0x1c, 0x1b, 0x13, 0x7e,
0x4e, 0x0f, 0x9d, 0x8e, 0xf4, 0x09, 0xf9, 0x2e };
-unsigned char nonce_pers_pr[16] =
+static const unsigned char nonce_pers_pr[16] =
{ 0xd2, 0x54, 0xfc, 0xff, 0x02, 0x1e, 0x69, 0xd2,
0x29, 0xc9, 0xcf, 0xad, 0x85, 0xfa, 0x48, 0x6c };
-unsigned char nonce_pers_nopr[16] =
+static const unsigned char nonce_pers_nopr[16] =
{ 0x1b, 0x54, 0xb8, 0xff, 0x06, 0x42, 0xbf, 0xf5,
0x21, 0xf1, 0x5c, 0x1c, 0x0b, 0x66, 0x5f, 0x3f };
-unsigned char result_pr[16] =
+static const unsigned char result_pr[16] =
{ 0x34, 0x01, 0x16, 0x56, 0xb4, 0x29, 0x00, 0x8f,
0x35, 0x63, 0xec, 0xb5, 0xf2, 0x59, 0x07, 0x23 };
-unsigned char result_nopr[16] =
+static const unsigned char result_nopr[16] =
{ 0xa0, 0x54, 0x30, 0x3d, 0x8a, 0x7e, 0xa9, 0x88,
0x9d, 0x90, 0x3e, 0x07, 0x7c, 0x6f, 0x21, 0x8f };
-int test_offset;
+static int test_offset;
static int ctr_drbg_self_test_entropy( void *data, unsigned char *buf,
size_t len )
{
- unsigned char *p = data;
+ const unsigned char *p = data;
memcpy( buf, p + test_offset, len );
- test_offset += 32;
+ test_offset += len;
return( 0 );
}
+#define CHK( c ) if( (c) != 0 ) \
+ { \
+ if( verbose != 0 ) \
+ printf( "failed\n" ); \
+ return( 1 ); \
+ }
+
/*
* Checkup routine
*/
@@ -475,39 +482,13 @@
printf( " CTR_DRBG (PR = TRUE) : " );
test_offset = 0;
- if( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy, entropy_source_pr, nonce_pers_pr, 16, 32 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
+ CHK( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy,
+ entropy_source_pr, nonce_pers_pr, 16, 32 ) );
ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
+ CHK( ctr_drbg_random( &ctx, buf, CTR_DRBG_BLOCKSIZE ) );
+ CHK( ctr_drbg_random( &ctx, buf, CTR_DRBG_BLOCKSIZE ) );
+ CHK( memcmp( buf, result_pr, CTR_DRBG_BLOCKSIZE ) );
- if( ctr_drbg_random( &ctx, buf, CTR_DRBG_BLOCKSIZE ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( ctr_drbg_random( &ctx, buf, CTR_DRBG_BLOCKSIZE ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( memcmp( buf, result_pr, CTR_DRBG_BLOCKSIZE ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
if( verbose != 0 )
printf( "passed\n" );
@@ -518,46 +499,13 @@
printf( " CTR_DRBG (PR = FALSE): " );
test_offset = 0;
- if( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy, entropy_source_nopr, nonce_pers_nopr, 16, 32 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
+ CHK( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy,
+ entropy_source_nopr, nonce_pers_nopr, 16, 32 ) );
+ CHK( ctr_drbg_random( &ctx, buf, 16 ) );
+ CHK( ctr_drbg_reseed( &ctx, NULL, 0 ) );
+ CHK( ctr_drbg_random( &ctx, buf, 16 ) );
+ CHK( memcmp( buf, result_nopr, 16 ) );
- return( 1 );
- }
-
- if( ctr_drbg_random( &ctx, buf, 16 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( ctr_drbg_reseed( &ctx, NULL, 0 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( ctr_drbg_random( &ctx, buf, 16 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
- if( memcmp( buf, result_nopr, 16 ) != 0 )
- {
- if( verbose != 0 )
- printf( "failed\n" );
-
- return( 1 );
- }
-
if( verbose != 0 )
printf( "passed\n" );