From 66ea31ccd0d2122015fc20b01fadf8ab27ae0d60 Mon Sep 17 00:00:00 2001 From: Elena Uziunaite Date: Fri, 28 Jun 2024 14:54:09 +0100 Subject: [PATCH] Clean up constant-flow memsan testing Disable asm in memsan constant-flow testing and adjust test_suit_bignum_core.function accordingly Signed-off-by: Elena Uziunaite --- tests/scripts/all.sh | 2 + tests/suites/test_suite_bignum_core.function | 43 ++++++++------------ 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index a1203f7726..3eca325003 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -2202,6 +2202,7 @@ component_test_memsan_constant_flow () { scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO scripts/config.py unset MBEDTLS_AESNI_C # memsan doesn't grok asm + scripts/config.py unset MBEDTLS_HAVE_ASM CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan . make @@ -2220,6 +2221,7 @@ component_test_memsan_constant_flow_psa () { scripts/config.py full scripts/config.py set MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN scripts/config.py unset MBEDTLS_AESNI_C # memsan doesn't grok asm + scripts/config.py unset MBEDTLS_HAVE_ASM CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan . make diff --git a/tests/suites/test_suite_bignum_core.function b/tests/suites/test_suite_bignum_core.function index 89c2282207..6c0bd1e0c4 100644 --- a/tests/suites/test_suite_bignum_core.function +++ b/tests/suites/test_suite_bignum_core.function @@ -787,20 +787,16 @@ void mpi_core_mla(char *input_A, char *input_B, char *input_S, memcpy(a, A.p, A.n * sizeof(mbedtls_mpi_uint)); memcpy(x, X->p, X->n * sizeof(mbedtls_mpi_uint)); -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) TEST_CF_SECRET(a, bytes); TEST_CF_SECRET(B.p, B.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(S.p, sizeof(mbedtls_mpi_uint)); -#endif /* 1a) A += B * s => we should get the correct carry */ TEST_EQUAL(mbedtls_mpi_core_mla(a, limbs, B.p, B.n, *S.p), *cy->p); -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) TEST_CF_PUBLIC(a, bytes); TEST_CF_PUBLIC(B.p, B.n * sizeof(mbedtls_mpi_uint)); TEST_CF_PUBLIC(S.p, sizeof(mbedtls_mpi_uint)); -#endif /* 1b) A += B * s => we should get the correct result */ TEST_MEMORY_COMPARE(a, bytes, x, bytes); @@ -808,15 +804,15 @@ void mpi_core_mla(char *input_A, char *input_B, char *input_S, if (A.n == B.n && memcmp(A.p, B.p, bytes) == 0) { /* Check when A and B are aliased */ memcpy(a, A.p, A.n * sizeof(mbedtls_mpi_uint)); -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) + TEST_CF_SECRET(a, bytes); TEST_CF_SECRET(S.p, sizeof(mbedtls_mpi_uint)); -#endif + TEST_EQUAL(mbedtls_mpi_core_mla(a, limbs, a, limbs, *S.p), *cy->p); -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) + TEST_CF_PUBLIC(a, bytes); TEST_CF_PUBLIC(S.p, sizeof(mbedtls_mpi_uint)); -#endif + TEST_MEMORY_COMPARE(a, bytes, x, bytes); } @@ -919,20 +915,18 @@ void mpi_core_montmul(int limbs_AN4, int limbs_B4, size_t working_limbs = mbedtls_mpi_core_montmul_working_limbs(limbs_AN); TEST_EQUAL(working_limbs, limbs_AN * 2 + 1); TEST_EQUAL(0, mbedtls_mpi_grow(&T, working_limbs)); - /* Temporary because MEMSAN doesn't support assembly implementation see #1243 */ -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) + TEST_CF_SECRET(N.p, N.n * sizeof(mbedtls_mpi_uint)); -#endif + /* Calculate the Montgomery constant (this is unit tested separately) */ mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N.p); TEST_EQUAL(0, mbedtls_mpi_grow(&R, limbs_AN)); /* ensure it's got the right number of limbs */ -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) TEST_CF_SECRET(N.p, N.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(A.p, A.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(B.p, B.n * sizeof(mbedtls_mpi_uint)); -#endif + mbedtls_mpi_core_montmul(R.p, A.p, B.p, B.n, N.p, N.n, mm, T.p); TEST_CF_PUBLIC(R.p, R.n * sizeof(mbedtls_mpi_uint)); @@ -942,11 +936,11 @@ void mpi_core_montmul(int limbs_AN4, int limbs_B4, /* The output (R, above) may be aliased to A - use R to save the value of A */ memcpy(R.p, A.p, bytes); -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) + TEST_CF_SECRET(N.p, N.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(A.p, A.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(B.p, B.n * sizeof(mbedtls_mpi_uint)); -#endif + mbedtls_mpi_core_montmul(A.p, A.p, B.p, B.n, N.p, N.n, mm, T.p); TEST_CF_PUBLIC(A.p, A.n * sizeof(mbedtls_mpi_uint)); @@ -957,11 +951,11 @@ void mpi_core_montmul(int limbs_AN4, int limbs_B4, /* The output may be aliased to N - use R to save the value of N */ memcpy(R.p, N.p, bytes); -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) + TEST_CF_SECRET(N.p, N.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(A.p, A.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(B.p, B.n * sizeof(mbedtls_mpi_uint)); -#endif + mbedtls_mpi_core_montmul(N.p, A.p, B.p, B.n, N.p, N.n, mm, T.p); TEST_CF_PUBLIC(N.p, N.n * sizeof(mbedtls_mpi_uint)); @@ -977,11 +971,11 @@ void mpi_core_montmul(int limbs_AN4, int limbs_B4, if (memcmp(A.p, B.p, bytes) == 0) { /* Test with A aliased to B and output, since this is permitted - * don't bother with yet another test with only A and B aliased */ -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) + TEST_CF_SECRET(N.p, N.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(A.p, A.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(B.p, B.n * sizeof(mbedtls_mpi_uint)); -#endif + mbedtls_mpi_core_montmul(B.p, B.p, B.p, B.n, N.p, N.n, mm, T.p); TEST_CF_PUBLIC(B.p, B.n * sizeof(mbedtls_mpi_uint)); @@ -991,11 +985,11 @@ void mpi_core_montmul(int limbs_AN4, int limbs_B4, } /* The output may be aliased to B - last test, so we don't save B */ -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) + TEST_CF_SECRET(N.p, N.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(A.p, A.n * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(B.p, B.n * sizeof(mbedtls_mpi_uint)); -#endif + mbedtls_mpi_core_montmul(B.p, A.p, B.p, B.n, N.p, N.n, mm, T.p); TEST_CF_PUBLIC(B.p, B.n * sizeof(mbedtls_mpi_uint)); @@ -1303,23 +1297,20 @@ void mpi_core_exp_mod(char *input_N, char *input_A, TEST_CALLOC(T, working_limbs); - /* Temporary because MEMSAN doesn't support assembly implementation see #1243 */ -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) TEST_CF_SECRET(A, A_limbs * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(N, N_limbs * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(E, E_limbs * sizeof(mbedtls_mpi_uint)); -#endif + mbedtls_mpi_core_exp_mod(Y, A, N, N_limbs, E, E_limbs, R2, T); TEST_CF_PUBLIC(Y, N_limbs * sizeof(mbedtls_mpi_uint)); TEST_EQUAL(0, memcmp(X, Y, N_limbs * sizeof(mbedtls_mpi_uint))); -#if !defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) TEST_CF_SECRET(A, A_limbs * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(N, N_limbs * sizeof(mbedtls_mpi_uint)); TEST_CF_SECRET(E, E_limbs * sizeof(mbedtls_mpi_uint)); -#endif + /* Check when output aliased to input */ mbedtls_mpi_core_exp_mod(A, A, N, N_limbs, E, E_limbs, R2, T);