diff --git a/tests/.gitignore b/tests/.gitignore index dc17f8729..cac870ca3 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,3 +1,4 @@ /test_suite* data_files/mpi_write data_files/hmac_drbg_seed +data_files/ctr_drbg_seed diff --git a/tests/suites/test_suite_ctr_drbg.data b/tests/suites/test_suite_ctr_drbg.data index 6120bb0c0..53687aee7 100644 --- a/tests/suites/test_suite_ctr_drbg.data +++ b/tests/suites/test_suite_ctr_drbg.data @@ -718,6 +718,15 @@ ctr_drbg_validate_nopr:"898064243e44ff67151736ce8bb6f1c759cab4aaca9b87543a1ac984 CTR_DRBG NIST Validation (AES-256 use df,False,256,128,256,256) #14 ctr_drbg_validate_nopr:"50de72903b9d99764123ffaa0c721e14ad1ab5c46a34c040f25324ba1d937b8ef10467161fcf2978c2a680ac5570c6d2":"5c9954fd0143e62c3bf2d5734052e3c9370f7b9d75c70f58fe33b12e3997ee2c8db84f8467affd7cfd9a9e7ec60da6f31bf9bf32aedf644e4934bd1fc916bc8d":"d5dc4c9fc7171fcbfdaead558a565ffd55d245a58b22ad1666ee05131e33f49e":"ea3114e92e6a19f53b207a0a54cd363a6d053fed0a827f92556f0a8580f7a342":"53686f069b455af4692888d11fac15cf7b4bd38e198de4e62b7098f875198a75":"9fb0df053e0345e5640aa97fedef50a6" +CTR_DRBG entropy usage +ctr_drbg_entropy_usage: + +CTR_DRBG write/update seed file +ctr_drbg_seed_file:"data_files/ctr_drbg_seed":0 + +CTR_DRBG write/update seed file +ctr_drbg_seed_file:"no_such_dir/file":POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR + CTR_DRBG self test ctr_drbg_selftest: diff --git a/tests/suites/test_suite_ctr_drbg.function b/tests/suites/test_suite_ctr_drbg.function index 1ae763d1c..56906750b 100644 --- a/tests/suites/test_suite_ctr_drbg.function +++ b/tests/suites/test_suite_ctr_drbg.function @@ -4,9 +4,9 @@ int test_offset_idx; int entropy_func( void *data, unsigned char *buf, size_t len ) { - unsigned char *p = (unsigned char *) data; + const unsigned char *p = (unsigned char *) data; memcpy( buf, p + test_offset_idx, len ); - test_offset_idx += 32; + test_offset_idx += len; return( 0 ); } /* END_HEADER */ @@ -82,6 +82,88 @@ void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string, } /* END_CASE */ +/* BEGIN_CASE */ +void ctr_drbg_entropy_usage( ) +{ + unsigned char out[16]; + unsigned char add[16]; + unsigned char entropy[1024]; + ctr_drbg_context ctx; + size_t i, reps = 10; + int last_idx; + + test_offset_idx = 0; + memset( entropy, 0, sizeof( entropy ) ); + memset( out, 0, sizeof( out ) ); + memset( add, 0, sizeof( add ) ); + + /* Init must use entropy */ + last_idx = test_offset_idx; + TEST_ASSERT( ctr_drbg_init( &ctx, entropy_func, entropy, NULL, 0 ) == 0 ); + TEST_ASSERT( last_idx < test_offset_idx ); + + /* By default, PR is off and reseed_interval is large, + * so the next few calls should not use entropy */ + last_idx = test_offset_idx; + for( i = 0; i < reps; i++ ) + { + TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 ); + TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4, + add, sizeof( add ) ) == 0 ); + } + TEST_ASSERT( last_idx == test_offset_idx ); + + /* While at it, make sure we didn't write past the requested length */ + TEST_ASSERT( out[sizeof( out ) - 4] == 0 ); + TEST_ASSERT( out[sizeof( out ) - 3] == 0 ); + TEST_ASSERT( out[sizeof( out ) - 2] == 0 ); + TEST_ASSERT( out[sizeof( out ) - 1] == 0 ); + + /* Set reseed_interval to the number of calls done, + * so the next call should reseed */ + ctr_drbg_set_reseed_interval( &ctx, 2 * reps ); + TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); + TEST_ASSERT( last_idx < test_offset_idx ); + + /* The new few calls should not reseed */ + last_idx = test_offset_idx; + for( i = 0; i < reps / 2; i++ ) + { + TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); + TEST_ASSERT( ctr_drbg_random_with_add( &ctx, out, sizeof( out ) , + add, sizeof( add ) ) == 0 ); + } + TEST_ASSERT( last_idx == test_offset_idx ); + + /* Now enable PR, so the next few calls should all reseed */ + ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON ); + TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); + TEST_ASSERT( last_idx < test_offset_idx ); + + /* Finally, check setting entropy_len */ + ctr_drbg_set_entropy_len( &ctx, 42 ); + last_idx = test_offset_idx; + TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); + TEST_ASSERT( test_offset_idx - last_idx == 42 ); + + ctr_drbg_set_entropy_len( &ctx, 13 ); + last_idx = test_offset_idx; + TEST_ASSERT( ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 ); + TEST_ASSERT( test_offset_idx - last_idx == 13 ); +} +/* END_CASE */ + +/* BEGIN_CASE depends_on:POLARSSL_FS_IO */ +void ctr_drbg_seed_file( char *path, int ret ) +{ + ctr_drbg_context ctx; + + TEST_ASSERT( ctr_drbg_init( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 ); + TEST_ASSERT( ctr_drbg_write_seed_file( &ctx, path ) == ret ); + TEST_ASSERT( ctr_drbg_update_seed_file( &ctx, path ) == ret ); +} +/* END_CASE */ + /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST */ void ctr_drbg_selftest( ) {