Add tests for optionally unsafe code paths
Signed-off-by: Janos Follath <janos.follath@arm.com>
diff --git a/tests/suites/test_suite_bignum_core.function b/tests/suites/test_suite_bignum_core.function
index 373bda4..9c864ac 100644
--- a/tests/suites/test_suite_bignum_core.function
+++ b/tests/suites/test_suite_bignum_core.function
@@ -1178,6 +1178,7 @@
char *input_E, char *input_X)
{
mbedtls_mpi_uint *A = NULL;
+ mbedtls_mpi_uint *A_copy = NULL;
mbedtls_mpi_uint *E = NULL;
mbedtls_mpi_uint *N = NULL;
mbedtls_mpi_uint *X = NULL;
@@ -1229,6 +1230,8 @@
TEST_CALLOC(T, working_limbs);
+ /* Test the safe variant */
+
#if defined(MBEDTLS_TEST_HOOKS)
mbedtls_mpi_optionally_safe_codepath_reset();
#endif
@@ -1236,10 +1239,23 @@
#if defined(MBEDTLS_TEST_HOOKS)
TEST_EQUAL(mbedtls_mpi_optionally_safe_codepath, MBEDTLS_MPI_IS_SECRET);
#endif
-
TEST_EQUAL(0, memcmp(X, Y, N_limbs * sizeof(mbedtls_mpi_uint)));
- /* Check when output aliased to input */
+ /* Test the unsafe variant */
+
+#if defined(MBEDTLS_TEST_HOOKS)
+ mbedtls_mpi_optionally_safe_codepath_reset();
+#endif
+ mbedtls_mpi_core_exp_mod_unsafe(Y, A, N, N_limbs, E, E_limbs, R2, T);
+#if defined(MBEDTLS_TEST_HOOKS)
+ TEST_EQUAL(mbedtls_mpi_optionally_safe_codepath, MBEDTLS_MPI_IS_PUBLIC);
+#endif
+ TEST_EQUAL(0, memcmp(X, Y, N_limbs * sizeof(mbedtls_mpi_uint)));
+
+ /* Check both with output aliased to input */
+
+ TEST_CALLOC(A_copy, A_limbs);
+ memcpy(A_copy, A, sizeof(A_copy) * A_limbs);
#if defined(MBEDTLS_TEST_HOOKS)
mbedtls_mpi_optionally_safe_codepath_reset();
@@ -1248,12 +1264,22 @@
#if defined(MBEDTLS_TEST_HOOKS)
TEST_EQUAL(mbedtls_mpi_optionally_safe_codepath, MBEDTLS_MPI_IS_SECRET);
#endif
+ TEST_EQUAL(0, memcmp(X, A, N_limbs * sizeof(mbedtls_mpi_uint)));
+ memcpy(A, A_copy, sizeof(A) * A_limbs);
+#if defined(MBEDTLS_TEST_HOOKS)
+ mbedtls_mpi_optionally_safe_codepath_reset();
+#endif
+ mbedtls_mpi_core_exp_mod_unsafe(A, A, N, N_limbs, E, E_limbs, R2, T);
+#if defined(MBEDTLS_TEST_HOOKS)
+ TEST_EQUAL(mbedtls_mpi_optionally_safe_codepath, MBEDTLS_MPI_IS_PUBLIC);
+#endif
TEST_EQUAL(0, memcmp(X, A, N_limbs * sizeof(mbedtls_mpi_uint)));
exit:
mbedtls_free(T);
mbedtls_free(A);
+ mbedtls_free(A_copy);
mbedtls_free(E);
mbedtls_free(N);
mbedtls_free(X);