Skip to content

Commit 5c03ec3

Browse files
authored
Merge pull request #6575 from tom-cosgrove-arm/convert-mpi_mod_int-test-cases-to-hex-228
Backport 2.28: Enable mpi_mod_int test case to take full-range MPI integers
2 parents 4637ed2 + 020ab7f commit 5c03ec3

File tree

2 files changed

+83
-18
lines changed

2 files changed

+83
-18
lines changed

tests/suites/test_suite_bignum.function

+39-6
Original file line numberDiff line numberDiff line change
@@ -1172,24 +1172,57 @@ exit:
11721172
/* END_CASE */
11731173

11741174
/* BEGIN_CASE */
1175-
void mbedtls_mpi_mod_int( char * input_X, int input_Y,
1176-
int input_A, int div_result )
1175+
void mbedtls_mpi_mod_int( char * input_X, char * input_Y,
1176+
char * input_A, int mod_result )
11771177
{
11781178
mbedtls_mpi X;
1179+
mbedtls_mpi Y;
1180+
mbedtls_mpi A;
11791181
int res;
11801182
mbedtls_mpi_uint r;
1183+
11811184
mbedtls_mpi_init( &X );
1185+
mbedtls_mpi_init( &Y );
1186+
mbedtls_mpi_init( &A );
11821187

1183-
TEST_ASSERT( mbedtls_test_read_mpi( &X, input_X ) == 0 );
1184-
res = mbedtls_mpi_mod_int( &r, &X, input_Y );
1185-
TEST_ASSERT( res == div_result );
1188+
/* We use MPIs to read Y and A since the test framework limits us to
1189+
* ints, so we can't have 64-bit values */
1190+
TEST_EQUAL( mbedtls_test_read_mpi( &X, input_X ), 0 );
1191+
TEST_EQUAL( mbedtls_test_read_mpi( &Y, input_Y ), 0 );
1192+
TEST_EQUAL( mbedtls_test_read_mpi( &A, input_A ), 0 );
1193+
1194+
TEST_EQUAL( Y.n, 1 );
1195+
TEST_EQUAL( A.n, 1 );
1196+
1197+
/* Convert the MPIs for Y and A to (signed) mbedtls_mpi_sints */
1198+
1199+
/* Since we're converting sign+magnitude to two's complement, we lose one
1200+
* bit of value in the output. This means there are some values we can't
1201+
* represent, e.g. (hex) -A0000000 on 32-bit systems. These are technically
1202+
* invalid test cases, so could be considered "won't happen", but they are
1203+
* easy to test for, and this helps guard against human error. */
1204+
1205+
mbedtls_mpi_sint y = (mbedtls_mpi_sint) Y.p[0];
1206+
TEST_ASSERT( y >= 0 ); /* If y < 0 here, we can't make negative y */
1207+
if( Y.s == -1 )
1208+
y = -y;
1209+
1210+
mbedtls_mpi_sint a = (mbedtls_mpi_sint) A.p[0];
1211+
TEST_ASSERT( a >= 0 ); /* Same goes for a */
1212+
if( A.s == -1 )
1213+
a = -a;
1214+
1215+
res = mbedtls_mpi_mod_int( &r, &X, y );
1216+
TEST_EQUAL( res, mod_result );
11861217
if( res == 0 )
11871218
{
1188-
TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
1219+
TEST_EQUAL( r, a );
11891220
}
11901221

11911222
exit:
11921223
mbedtls_mpi_free( &X );
1224+
mbedtls_mpi_free( &Y );
1225+
mbedtls_mpi_free( &A );
11931226
}
11941227
/* END_CASE */
11951228

tests/suites/test_suite_bignum.misc.data

+44-12
Original file line numberDiff line numberDiff line change
@@ -1202,40 +1202,72 @@ Test mbedtls_mpi_mod_mpi: 0 (null) % -1
12021202
mbedtls_mpi_mod_mpi:"":"-1":"":MBEDTLS_ERR_MPI_NEGATIVE_VALUE
12031203

12041204
Base test mbedtls_mpi_mod_int #1
1205-
mbedtls_mpi_mod_int:"3e8":13:12:0
1205+
mbedtls_mpi_mod_int:"3e8":"d":"c":0
12061206

12071207
Base test mbedtls_mpi_mod_int #2 (Divide by zero)
1208-
mbedtls_mpi_mod_int:"3e8":0:0:MBEDTLS_ERR_MPI_DIVISION_BY_ZERO
1208+
mbedtls_mpi_mod_int:"3e8":"0":"0":MBEDTLS_ERR_MPI_DIVISION_BY_ZERO
12091209

12101210
Base test mbedtls_mpi_mod_int #3
1211-
mbedtls_mpi_mod_int:"-3e8":13:1:0
1211+
mbedtls_mpi_mod_int:"-3e8":"d":"1":0
12121212

12131213
Base test mbedtls_mpi_mod_int #4 (Negative modulo)
1214-
mbedtls_mpi_mod_int:"3e8":-13:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE
1214+
mbedtls_mpi_mod_int:"3e8":"-d":"0":MBEDTLS_ERR_MPI_NEGATIVE_VALUE
12151215

12161216
Base test mbedtls_mpi_mod_int #5 (Negative modulo)
1217-
mbedtls_mpi_mod_int:"-3e8":-13:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE
1217+
mbedtls_mpi_mod_int:"-3e8":"-d":"0":MBEDTLS_ERR_MPI_NEGATIVE_VALUE
12181218

12191219
Base test mbedtls_mpi_mod_int #6 (By 1)
1220-
mbedtls_mpi_mod_int:"3e8":1:0:0
1220+
mbedtls_mpi_mod_int:"3e8":"1":"0":0
12211221

12221222
Base test mbedtls_mpi_mod_int #7 (By 2)
1223-
mbedtls_mpi_mod_int:"3e9":2:1:0
1223+
mbedtls_mpi_mod_int:"3e9":"2":"1":0
12241224

12251225
Base test mbedtls_mpi_mod_int #8 (By 2)
1226-
mbedtls_mpi_mod_int:"3e8":2:0:0
1226+
mbedtls_mpi_mod_int:"3e8":"2":"0":0
12271227

12281228
Test mbedtls_mpi_mod_int: 0 (null) % 1
1229-
mbedtls_mpi_mod_int:"":1:0:0
1229+
mbedtls_mpi_mod_int:"":"1":"0":0
12301230

12311231
Test mbedtls_mpi_mod_int: 0 (null) % 2
1232-
mbedtls_mpi_mod_int:"":2:0:0
1232+
mbedtls_mpi_mod_int:"":"2":"0":0
12331233

12341234
Test mbedtls_mpi_mod_int: 0 (null) % -1
1235-
mbedtls_mpi_mod_int:"":-1:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE
1235+
mbedtls_mpi_mod_int:"":"-1":"0":MBEDTLS_ERR_MPI_NEGATIVE_VALUE
12361236

12371237
Test mbedtls_mpi_mod_int: 0 (null) % -2
1238-
mbedtls_mpi_mod_int:"":-2:0:MBEDTLS_ERR_MPI_NEGATIVE_VALUE
1238+
mbedtls_mpi_mod_int:"":"-2":"0":MBEDTLS_ERR_MPI_NEGATIVE_VALUE
1239+
1240+
# CURRENTLY FAILS - SEE GITHUB ISSUE #6540
1241+
#Test mbedtls_mpi_mod_int: 230772460340063000000100500000300000010 % 5178236083361335880 -> 3386266129388798810
1242+
#depends_on:MBEDTLS_HAVE_INT64
1243+
#mbedtls_mpi_mod_int:"AD9D28BF6C4E98FDF156BF0980CEE30A":"47DCCA4847DCCA48":"2EFE6F1A7D28035A":0
1244+
1245+
Test mbedtls_mpi_mod_mpi: 230772460340063000000100500000300000010 % 5178236083361335880 -> 3386266129388798810
1246+
mbedtls_mpi_mod_mpi:"AD9D28BF6C4E98FDF156BF0980CEE30A":"47DCCA4847DCCA48":"2EFE6F1A7D28035A":0
1247+
1248+
# CURRENTLY FAILS - SEE GITHUB ISSUE #6540
1249+
#Test mbedtls_mpi_mod_int: 230772460340062999996714233870911201200 % 5178236083361335880 -> 0
1250+
#depends_on:MBEDTLS_HAVE_INT64
1251+
#mbedtls_mpi_mod_int:"AD9D28BF6C4E98FDC2584FEF03A6DFB0":"47DCCA4847DCCA48":"0":0
1252+
1253+
Test mbedtls_mpi_mod_mpi: 230772460340062999996714233870911201200 % 5178236083361335880 -> 0
1254+
mbedtls_mpi_mod_mpi:"AD9D28BF6C4E98FDC2584FEF03A6DFB0":"47DCCA4847DCCA48":"0":0
1255+
1256+
# CURRENTLY FAILS WHEN MPIS ARE 32-BIT (ISSUE #6450): WHEN FIXED, REMOVE "depends_on" LINE
1257+
Test mbedtls_mpi_mod_int: 230772460340063000000100500000300000010 % 1205652040 -> 3644370
1258+
depends_on:MBEDTLS_HAVE_INT64
1259+
mbedtls_mpi_mod_int:"AD9D28BF6C4E98FDF156BF0980CEE30A":"47DCCA48":"379BD2":0
1260+
1261+
Test mbedtls_mpi_mod_mpi: 230772460340063000000100500000300000010 % 1205652040 -> 3644370
1262+
mbedtls_mpi_mod_mpi:"AD9D28BF6C4E98FDF156BF0980CEE30A":"47DCCA48":"379BD2":0
1263+
1264+
# CURRENTLY FAILS WHEN MPIS ARE 32-BIT (ISSUE #6450): WHEN FIXED, REMOVE "depends_on" LINE
1265+
Test mbedtls_mpi_mod_int: 230772460340063000000100500000296355640 % 1205652040 -> 0
1266+
depends_on:MBEDTLS_HAVE_INT64
1267+
mbedtls_mpi_mod_int:"AD9D28BF6C4E98FDF156BF0980974738":"47DCCA48":"0":0
1268+
1269+
Test mbedtls_mpi_mod_mpi: 230772460340063000000100500000296355640 % 1205652040 -> 0
1270+
mbedtls_mpi_mod_mpi:"AD9D28BF6C4E98FDF156BF0980974738":"47DCCA48":"0":0
12391271

12401272
Base test mbedtls_mpi_exp_mod #1
12411273
mbedtls_mpi_exp_mod:"17":"d":"1d":"18":0

0 commit comments

Comments
 (0)