Fix representation of mod-random output

mbedtls_mpi_mod_raw_random() and mbedtls_mpi_mod_random() were producing
output in the Montgomery representation, instead of obeying the
representation chosen in the modulus structure. Fix this.

Duplicate the test cases for mod-random output to have separate test cases
for each representation.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/library/bignum_mod_raw.c b/library/bignum_mod_raw.c
index 721efc8..d811740 100644
--- a/library/bignum_mod_raw.c
+++ b/library/bignum_mod_raw.c
@@ -215,7 +215,7 @@
     int ret = mbedtls_mpi_core_random( X, min, N->p, N->limbs, f_rng, p_rng );
     if( ret != 0 )
         return( ret );
-    return( mbedtls_mpi_mod_raw_to_mont_rep( X, N ) );
+    return( mbedtls_mpi_mod_raw_canonical_to_modulus_rep( X, N ) );
 }
 
 /* END MERGE SLOT 6 */
diff --git a/tests/suites/test_suite_bignum_random.data b/tests/suites/test_suite_bignum_random.data
index b486f38..ee5e397 100644
--- a/tests/suites/test_suite_bignum_random.data
+++ b/tests/suites/test_suite_bignum_random.data
@@ -25,8 +25,11 @@
 MPI random legacy=core: 2^28-1..2^28+1 (NOT_ACCEPTABLE)
 mpi_legacy_random_values:0x0fffffff:"10000001"
 
-MPI random mod=core: 2^28-1..2^28+1 (NOT_ACCEPTABLE)
-mpi_mod_random_values:0x0fffffff:"10000001"
+MPI random mod=core: 2^28-1..2^28+1 (NOT_ACCEPTABLE) (Mont)
+mpi_mod_random_values:0x0fffffff:"10000001":MBEDTLS_MPI_MOD_REP_MONTGOMERY
+
+MPI random mod=core: 2^28-1..2^28+1 (NOT_ACCEPTABLE) (canon)
+mpi_mod_random_values:0x0fffffff:"10000001":MBEDTLS_MPI_MOD_REP_OPT_RED
 
 MPI core random basic: 2^29-1..2^29+1 (NOT_ACCEPTABLE)
 mpi_core_random_basic:0x1fffffff:"20000001":MBEDTLS_ERR_MPI_NOT_ACCEPTABLE
@@ -34,8 +37,11 @@
 MPI random legacy=core: 2^29-1..2^29+1 (NOT_ACCEPTABLE)
 mpi_legacy_random_values:0x1fffffff:"20000001"
 
-MPI random mod=core: 2^29-1..2^29+1 (NOT_ACCEPTABLE)
-mpi_mod_random_values:0x1fffffff:"20000001"
+MPI random mod=core: 2^29-1..2^29+1 (NOT_ACCEPTABLE) (Mont)
+mpi_mod_random_values:0x1fffffff:"20000001":MBEDTLS_MPI_MOD_REP_MONTGOMERY
+
+MPI random mod=core: 2^29-1..2^29+1 (NOT_ACCEPTABLE) (canon)
+mpi_mod_random_values:0x1fffffff:"20000001":MBEDTLS_MPI_MOD_REP_OPT_RED
 
 MPI core random basic: 2^30-1..2^30+1 (NOT_ACCEPTABLE)
 mpi_core_random_basic:0x3fffffff:"40000001":MBEDTLS_ERR_MPI_NOT_ACCEPTABLE
@@ -43,8 +49,11 @@
 MPI random legacy=core: 2^30-1..2^30+1 (NOT_ACCEPTABLE)
 mpi_legacy_random_values:0x3fffffff:"40000001"
 
-MPI random mod=core: 2^30-1..2^30+1 (NOT_ACCEPTABLE)
-mpi_mod_random_values:0x3fffffff:"40000001"
+MPI random mod=core: 2^30-1..2^30+1 (NOT_ACCEPTABLE) (Mont)
+mpi_mod_random_values:0x3fffffff:"40000001":MBEDTLS_MPI_MOD_REP_MONTGOMERY
+
+MPI random mod=core: 2^30-1..2^30+1 (NOT_ACCEPTABLE) (canon)
+mpi_mod_random_values:0x3fffffff:"40000001":MBEDTLS_MPI_MOD_REP_OPT_RED
 
 MPI core random basic: 2^31-1..2^31+1 (NOT_ACCEPTABLE)
 mpi_core_random_basic:0x7fffffff:"80000001":MBEDTLS_ERR_MPI_NOT_ACCEPTABLE
@@ -52,8 +61,11 @@
 MPI random legacy=core: 2^31-1..2^31+1 (NOT_ACCEPTABLE)
 mpi_legacy_random_values:0x7fffffff:"80000001"
 
-MPI random mod=core: 2^31-1..2^31+1 (NOT_ACCEPTABLE)
-mpi_mod_random_values:0x7fffffff:"80000001"
+MPI random mod=core: 2^31-1..2^31+1 (NOT_ACCEPTABLE) (Mont)
+mpi_mod_random_values:0x7fffffff:"80000001":MBEDTLS_MPI_MOD_REP_MONTGOMERY
+
+MPI random mod=core: 2^31-1..2^31+1 (NOT_ACCEPTABLE) (canon)
+mpi_mod_random_values:0x7fffffff:"80000001":MBEDTLS_MPI_MOD_REP_OPT_RED
 
 MPI random in range: 1..2
 mpi_random_many:1:"02":1000
@@ -246,23 +258,41 @@
 MPI random legacy=core: 0..2^256+1
 mpi_legacy_random_values:0:"010000000000000000000000000000000000000000000000000000000000000001"
 
-MPI random mod=core: 0..1
-mpi_mod_random_values:0:"01"
+MPI random mod=core: 0..1 (Mont)
+mpi_mod_random_values:0:"01":MBEDTLS_MPI_MOD_REP_MONTGOMERY
 
-MPI random mod=core: 0..3
-mpi_mod_random_values:0:"03"
+MPI random mod=core: 0..1 (canon)
+mpi_mod_random_values:0:"01":MBEDTLS_MPI_MOD_REP_OPT_RED
 
-MPI random mod=core: 1..3
-mpi_mod_random_values:1:"03"
+MPI random mod=core: 0..3 (Mont)
+mpi_mod_random_values:0:"03":MBEDTLS_MPI_MOD_REP_MONTGOMERY
 
-MPI random mod=core: 2^30..2^31-1
-mpi_mod_random_values:0x40000000:"7fffffff"
+MPI random mod=core: 0..3 (canon)
+mpi_mod_random_values:0:"03":MBEDTLS_MPI_MOD_REP_OPT_RED
 
-MPI random mod=core: 2^31-1..2^32-1
-mpi_mod_random_values:0x7fffffff:"ffffffff"
+MPI random mod=core: 1..3 (Mont)
+mpi_mod_random_values:1:"03":MBEDTLS_MPI_MOD_REP_MONTGOMERY
 
-MPI random mod=core: 0..2^256+1
-mpi_mod_random_values:0:"010000000000000000000000000000000000000000000000000000000000000001"
+MPI random mod=core: 1..3 (canon)
+mpi_mod_random_values:1:"03":MBEDTLS_MPI_MOD_REP_OPT_RED
+
+MPI random mod=core: 2^30..2^31-1 (Mont)
+mpi_mod_random_values:0x40000000:"7fffffff":MBEDTLS_MPI_MOD_REP_MONTGOMERY
+
+MPI random mod=core: 2^30..2^31-1 (canon)
+mpi_mod_random_values:0x40000000:"7fffffff":MBEDTLS_MPI_MOD_REP_OPT_RED
+
+MPI random mod=core: 2^31-1..2^32-1 (Mont)
+mpi_mod_random_values:0x7fffffff:"ffffffff":MBEDTLS_MPI_MOD_REP_MONTGOMERY
+
+MPI random mod=core: 2^31-1..2^32-1 (canon)
+mpi_mod_random_values:0x7fffffff:"ffffffff":MBEDTLS_MPI_MOD_REP_OPT_RED
+
+MPI random mod=core: 0..2^256+1 (Mont)
+mpi_mod_random_values:0:"010000000000000000000000000000000000000000000000000000000000000001":MBEDTLS_MPI_MOD_REP_MONTGOMERY
+
+MPI random mod=core: 0..2^256+1 (canon)
+mpi_mod_random_values:0:"010000000000000000000000000000000000000000000000000000000000000001":MBEDTLS_MPI_MOD_REP_OPT_RED
 
 MPI random mod validation: 1 limb, good, 0..1
 mpi_mod_random_validation:0:"1":0:0
diff --git a/tests/suites/test_suite_bignum_random.function b/tests/suites/test_suite_bignum_random.function
index 0f431cd..f2cb206 100644
--- a/tests/suites/test_suite_bignum_random.function
+++ b/tests/suites/test_suite_bignum_random.function
@@ -192,7 +192,7 @@
 /* END_CASE */
 
 /* BEGIN_CASE */
-void mpi_mod_random_values( int min, char *max_hex )
+void mpi_mod_random_values( int min, char *max_hex, int rep )
 {
     /* Same RNG as in mpi_core_random_basic */
     mbedtls_test_rnd_pseudo_info rnd_core = {
@@ -210,9 +210,7 @@
     mbedtls_mpi_mod_modulus N;
     mbedtls_mpi_mod_modulus_init( &N );
 
-    TEST_EQUAL( mbedtls_test_read_mpi_modulus( &N, max_hex,
-                                               MBEDTLS_MPI_MOD_REP_MONTGOMERY ),
-                0 );
+    TEST_EQUAL( mbedtls_test_read_mpi_modulus( &N, max_hex, rep ), 0 );
     ASSERT_ALLOC( R_core, N.limbs );
     ASSERT_ALLOC( R_mod_raw, N.limbs );
     ASSERT_ALLOC( R_mod_digits, N.limbs );
@@ -240,10 +238,12 @@
     TEST_EQUAL( core_ret, mod_ret );
     if( core_ret == 0 )
     {
-        TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( R_mod_raw, &N ), 0 );
+        TEST_EQUAL( mbedtls_mpi_mod_raw_modulus_to_canonical_rep( R_mod_raw, &N ),
+                    0 );
         ASSERT_COMPARE( R_core, N.limbs * ciL,
                         R_mod_raw, N.limbs * ciL );
-        TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( R_mod_digits, &N ), 0 );
+        TEST_EQUAL( mbedtls_mpi_mod_raw_modulus_to_canonical_rep( R_mod_digits, &N ),
+                    0 );
         ASSERT_COMPARE( R_core, N.limbs * ciL,
                         R_mod_digits, N.limbs * ciL );
     }
@@ -428,7 +428,7 @@
     mbedtls_mpi_mod_modulus_init( &N );
 
     TEST_EQUAL( mbedtls_test_read_mpi_modulus( &N, bound_hex,
-                                               MBEDTLS_MPI_MOD_REP_MONTGOMERY ),
+                                               MBEDTLS_MPI_MOD_REP_OPT_RED ),
                 0 );
     size_t result_limbs = N.limbs + result_limbs_delta;
     ASSERT_ALLOC( result_digits, result_limbs );
@@ -445,7 +445,6 @@
          * size as the modulus, otherwise it's a mistake in the test data. */
         TEST_EQUAL( result_limbs, N.limbs );
         /* Sanity check: check that the result is in range */
-        TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( result_digits, &N ), 0 );
         TEST_EQUAL( mbedtls_mpi_core_lt_ct( result_digits, N.p, N.limbs ),
                     1 );
         /* Check result >= min (changes result) */
@@ -463,7 +462,6 @@
                     expected_ret );
         if( expected_ret == 0 )
         {
-            TEST_EQUAL( mbedtls_mpi_mod_raw_from_mont_rep( result_digits, &N ), 0 );
             TEST_EQUAL( mbedtls_mpi_core_lt_ct( result_digits, N.p, N.limbs ),
                         1 );
             TEST_EQUAL( mbedtls_mpi_core_sub_int( result_digits, result.p, min,