Add tests for optionally unsafe code paths
Signed-off-by: Janos Follath <janos.follath@arm.com>
diff --git a/tf-psa-crypto/tests/suites/test_suite_bignum_core.function b/tf-psa-crypto/tests/suites/test_suite_bignum_core.function
index fe47cc6..accfffd 100644
--- a/tf-psa-crypto/tests/suites/test_suite_bignum_core.function
+++ b/tf-psa-crypto/tests/suites/test_suite_bignum_core.function
@@ -1246,6 +1246,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;
@@ -1297,10 +1298,10 @@
TEST_CALLOC(T, working_limbs);
+ /* Test the safe variant */
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));
-
#if defined(MBEDTLS_TEST_HOOKS)
mbedtls_mpi_optionally_safe_codepath_reset();
#endif
@@ -1308,16 +1309,31 @@
#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)));
TEST_CF_PUBLIC(Y, N_limbs * sizeof(mbedtls_mpi_uint));
TEST_EQUAL(0, memcmp(X, Y, N_limbs * sizeof(mbedtls_mpi_uint)));
+ /* 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);
+
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));
-
- /* Check when output aliased to input */
#if defined(MBEDTLS_TEST_HOOKS)
mbedtls_mpi_optionally_safe_codepath_reset();
#endif
@@ -1325,13 +1341,23 @@
#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)));
TEST_CF_PUBLIC(A, A_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);