From da9746e8cec20b043459ccdc85de567eb7611a36 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 27 Sep 2022 22:38:38 +0200 Subject: [PATCH] Test NOT_ACCEPTABLE cases for MPI get-random Test some cases where mbedtls_mpi_core_random() or mbedtls_mpi_random() should return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE. These test cases use a very small range that makes the NOT_ACCEPTABLE case likely. The test code uses a deterministic RNG whose implementation is in the test framework, so we know that the tests will pass reproducibly unless the implementation the test framework changes. Signed-off-by: Gilles Peskine --- tests/suites/test_suite_bignum.function | 37 +++++++++++++++++++ tests/suites/test_suite_bignum.misc.data | 45 ++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/tests/suites/test_suite_bignum.function b/tests/suites/test_suite_bignum.function index 6fd0b1b5fe..9dc5e595df 100644 --- a/tests/suites/test_suite_bignum.function +++ b/tests/suites/test_suite_bignum.function @@ -1295,9 +1295,46 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void mpi_core_random_basic( int min, char *bound_bytes, int expected_ret ) +{ + /* Same RNG as in mpi_random_values */ + mbedtls_test_rnd_pseudo_info rnd = { + {'T', 'h', 'i', 's', ' ', 'i', ',', 'a', + 's', 'e', 'e', 'd', '!', 0}, + 0, 0}; + size_t limbs; + mbedtls_mpi_uint *lower_bound = NULL; + mbedtls_mpi_uint *upper_bound = NULL; + mbedtls_mpi_uint *result = NULL; + + TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &upper_bound, &limbs, + bound_bytes ) ); + ASSERT_ALLOC( lower_bound, limbs * ciL ); + lower_bound[0] = min; + ASSERT_ALLOC( result, limbs * ciL ); + + TEST_EQUAL( expected_ret, + mbedtls_mpi_core_random( result, min, upper_bound, limbs, + mbedtls_test_rnd_pseudo_rand, &rnd ) ); + + if( expected_ret == 0 ) + { + TEST_EQUAL( 0, mbedtls_mpi_core_lt_ct( result, lower_bound, limbs ) ); + TEST_EQUAL( 1, mbedtls_mpi_core_lt_ct( result, upper_bound, limbs ) ); + } + +exit: + mbedtls_free( lower_bound ); + mbedtls_free( upper_bound ); + mbedtls_free( result ); +} +/* END_CASE */ + /* BEGIN_CASE */ void mpi_random_values( int min, char *max_hex ) { + /* Same RNG as in mpi_core_random_basic */ mbedtls_test_rnd_pseudo_info rnd_core = { {'T', 'h', 'i', 's', ' ', 'i', ',', 'a', 's', 'e', 'e', 'd', '!', 0}, diff --git a/tests/suites/test_suite_bignum.misc.data b/tests/suites/test_suite_bignum.misc.data index bc659c1af5..fa4c5bce41 100644 --- a/tests/suites/test_suite_bignum.misc.data +++ b/tests/suites/test_suite_bignum.misc.data @@ -1788,6 +1788,51 @@ mpi_fill_random:16:15:0:MBEDTLS_ERR_ENTROPY_SOURCE_FAILED Fill random: MAX_SIZE bytes, RNG failure after MAX_SIZE-1 bytes mpi_fill_random:MBEDTLS_MPI_MAX_SIZE:MBEDTLS_MPI_MAX_SIZE-1:0:MBEDTLS_ERR_ENTROPY_SOURCE_FAILED +MPI core random basic: 0..1 +mpi_core_random_basic:0:"01":0 + +MPI core random basic: 0..2 +mpi_core_random_basic:0:"02":0 + +MPI core random basic: 1..2 +mpi_core_random_basic:1:"02":0 + +MPI core random basic: 2^30..2^31 +mpi_core_random_basic:0x40000000:"80000000":0 + +MPI core random basic: 0..2^128 +mpi_core_random_basic:0x40000000:"0100000000000000000000000000000000":0 + +MPI core random basic: 2^30..2^129 +mpi_core_random_basic:0x40000000:"0200000000000000000000000000000000":0 + +# Use the same data values for mpi_core_random_basic->NOT_ACCEPTABLE +# and for mpi_random_values where we want to return NOT_ACCEPTABLE but +# this isn't checked at runtime. +MPI core random basic: 2^28-1..2^28 (NOT_ACCEPTABLE) +mpi_core_random_basic:0x0fffffff:"10000000":MBEDTLS_ERR_MPI_NOT_ACCEPTABLE + +MPI random legacy=core: 2^28-1..2^28 (NOT_ACCEPTABLE) +mpi_random_values:0x0fffffff:"10000000" + +MPI core random basic: 2^29-1..2^29 (NOT_ACCEPTABLE) +mpi_core_random_basic:0x1fffffff:"20000000":MBEDTLS_ERR_MPI_NOT_ACCEPTABLE + +MPI random legacy=core: 2^29-1..2^29 (NOT_ACCEPTABLE) +mpi_random_values:0x1fffffff:"20000000" + +MPI core random basic: 2^30-1..2^30 (NOT_ACCEPTABLE) +mpi_core_random_basic:0x3fffffff:"40000000":MBEDTLS_ERR_MPI_NOT_ACCEPTABLE + +MPI random legacy=core: 2^30-1..2^30 (NOT_ACCEPTABLE) +mpi_random_values:0x3fffffff:"40000000" + +MPI core random basic: 2^31-1..2^31 (NOT_ACCEPTABLE) +mpi_core_random_basic:0x7fffffff:"80000000":MBEDTLS_ERR_MPI_NOT_ACCEPTABLE + +MPI random legacy=core: 2^31-1..2^31 (NOT_ACCEPTABLE) +mpi_random_values:0x7fffffff:"80000000" + MPI random in range: 1..2 mpi_random_many:1:"02":1000