Refactor test code for CTR DRBG to clarify test functions
previously a single function was used for most test cases (ctr_drbg_validate) making it harder to understand what the exact scenario is as a result it was split into easier to understand functions.
diff --git a/tests/suites/test_suite_ctr_drbg.function b/tests/suites/test_suite_ctr_drbg.function
index f0465d3..f6b0915 100644
--- a/tests/suites/test_suite_ctr_drbg.function
+++ b/tests/suites/test_suite_ctr_drbg.function
@@ -12,8 +12,8 @@
RESEED_ALWAYS /* prediction resistance, no explicit reseed */
};
-static size_t test_offset_idx;
-static size_t test_max_idx;
+static size_t test_offset_idx = 0;
+static size_t test_max_idx = 0;
static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
{
const unsigned char *p = (unsigned char *) data;
@@ -24,7 +24,7 @@
return( 0 );
}
-static int ctr_drbg_validate_internal( int reseed_mode, data_t * nonce,
+static void ctr_drbg_validate_internal( int reseed_mode, data_t * nonce,
int entropy_len_arg, data_t * entropy,
data_t * reseed,
data_t * add1, data_t * add2,
@@ -35,13 +35,15 @@
size_t entropy_chunk_len = (size_t) entropy_len_arg;
+ TEST_ASSERT( entropy_chunk_len <= sizeof( buf ) );
+
test_offset_idx = 0;
mbedtls_ctr_drbg_init( &ctx );
test_max_idx = entropy->len;
- /* CTR_DRBG_Instantiate(entropy[:entropy_len], nonce, perso, <ignored>)
- * where nonce||perso = nonce[nonce_len] */
+ /* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>)
+ * where nonce||perso = nonce[nonce->len] */
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len(
&ctx,
mbedtls_test_entropy_func, entropy->x,
@@ -54,14 +56,14 @@
if( reseed_mode == RESEED_FIRST )
{
- /* CTR_DRBG_Reseed(entropy[idx:idx+entropy_len],
- * reseed[:reseed_len]) */
+ /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
+ * reseed[:reseed->len]) */
TEST_ASSERT( mbedtls_ctr_drbg_reseed(
&ctx,
reseed->x, reseed->len ) == 0 );
}
- /* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1_len]) -> buf */
+ /* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1->len]) -> buf */
/* Then reseed if prediction resistance is enabled. */
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add(
&ctx,
@@ -71,8 +73,8 @@
if( reseed_mode == RESEED_SECOND )
{
- /* CTR_DRBG_Reseed(entropy[idx:idx+entropy_len],
- * reseed[:reseed_len]) */
+ /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
+ * reseed[:reseed->len]) */
TEST_ASSERT( mbedtls_ctr_drbg_reseed(
&ctx,
reseed->x, reseed->len ) == 0 );
@@ -88,7 +90,6 @@
exit:
mbedtls_ctr_drbg_free( &ctx );
- return 0;
}
/* END_HEADER */
@@ -131,19 +132,6 @@
}
/* END_CASE */
-/* BEGIN_CASE */
-void ctr_drbg_validate( int reseed_mode, data_t * nonce,
- int entropy_len_arg, data_t * entropy,
- data_t * reseed,
- data_t * add1, data_t * add2,
- data_t * result_string )
-{
- TEST_ASSERT( ctr_drbg_validate_internal( reseed_mode, nonce,
- entropy_len_arg, entropy,
- reseed, add1,
- add2, result_string) == 0 );
-}
-/* END_CASE */
/* BEGIN_CASE */
void ctr_drbg_validate_no_reseed( data_t * add_init, data_t * entropy,
@@ -151,10 +139,11 @@
data_t * result_string )
{
data_t empty = {0};
- TEST_ASSERT( ctr_drbg_validate_internal( RESEED_NEVER, add_init,
- entropy->len, entropy,
- &empty, add1, add2,
- result_string ) == 0);
+ ctr_drbg_validate_internal( RESEED_NEVER, add_init,
+ entropy->len, entropy,
+ &empty, add1, add2,
+ result_string );
+ goto exit; // goto is needed to avoid warning ( no test assertions in func)
}
/* END_CASE */
@@ -164,26 +153,43 @@
data_t * result_string )
{
data_t empty = {0};
- TEST_ASSERT( ctr_drbg_validate_internal( RESEED_ALWAYS, add_init,
- entropy->len / 3, entropy,
- &empty, add1, add2,
- result_string ) == 0);
+ ctr_drbg_validate_internal( RESEED_ALWAYS, add_init,
+ entropy->len / 3, entropy,
+ &empty, add1, add2,
+ result_string );
+ goto exit; // goto is needed to avoid warning ( no test assertions in func)
}
/* END_CASE */
/* BEGIN_CASE */
-void ctr_drbg_validate_nopr( data_t * add_init, data_t * entropy,
+void ctr_drbg_validate_reseed_between( data_t * add_init, data_t * entropy,
data_t * add1, data_t * add_reseed,
data_t * add2, data_t * result_string )
{
- TEST_ASSERT( ctr_drbg_validate_internal( RESEED_SECOND, add_init,
- entropy->len / 2, entropy,
- add_reseed, add1, add2,
- result_string ) == 0);
+ ctr_drbg_validate_internal( RESEED_SECOND, add_init,
+ entropy->len / 2, entropy,
+ add_reseed, add1, add2,
+ result_string );
+ goto exit; // goto is needed to avoid warning ( no test assertions in func)
}
/* END_CASE */
/* BEGIN_CASE */
+void ctr_drbg_validate_reseed_first( data_t * add_init, data_t * entropy,
+ data_t * add1, data_t * add_reseed,
+ data_t * add2, data_t * result_string )
+{
+ ctr_drbg_validate_internal( RESEED_FIRST, add_init,
+ entropy->len / 2, entropy,
+ add_reseed, add1, add2,
+ result_string );
+ goto exit; // goto is needed to avoid warning ( no test assertions in func)
+}
+/* END_CASE */
+
+
+
+/* BEGIN_CASE */
void ctr_drbg_entropy_usage( )
{
unsigned char out[16];