Merge pull request #6732 from wernerlewis/bignum_6019_mod_add
Bignum: Implement mbedtls_mpi_mod_add()
This commit is contained in:
commit
50faa55e4d
5 changed files with 179 additions and 3 deletions
|
@ -210,7 +210,110 @@ exit:
|
|||
/* END MERGE SLOT 4 */
|
||||
|
||||
/* BEGIN MERGE SLOT 5 */
|
||||
/* BEGIN_CASE */
|
||||
void mpi_mod_add( char * input_N,
|
||||
char * input_A, char * input_B,
|
||||
char * input_S, int expected_ret )
|
||||
{
|
||||
mbedtls_mpi_mod_residue a = { NULL, 0 };
|
||||
mbedtls_mpi_mod_residue b = { NULL, 0 };
|
||||
mbedtls_mpi_mod_residue s = { NULL, 0 };
|
||||
mbedtls_mpi_mod_residue x = { NULL, 0 };
|
||||
mbedtls_mpi_uint *X_raw = NULL;
|
||||
|
||||
mbedtls_mpi_mod_modulus m;
|
||||
mbedtls_mpi_mod_modulus_init( &m );
|
||||
|
||||
TEST_EQUAL( 0,
|
||||
test_read_modulus( &m, MBEDTLS_MPI_MOD_REP_MONTGOMERY, input_N ) );
|
||||
|
||||
/* test_read_residue() normally checks that inputs have the same number of
|
||||
* limbs as the modulus. For negative testing we can ask it to skip this
|
||||
* with a non-zero final parameter. */
|
||||
TEST_EQUAL( 0, test_read_residue( &a, &m, input_A, expected_ret != 0 ) );
|
||||
TEST_EQUAL( 0, test_read_residue( &b, &m, input_B, expected_ret != 0 ) );
|
||||
TEST_EQUAL( 0, test_read_residue( &s, &m, input_S, expected_ret != 0 ) );
|
||||
|
||||
size_t limbs = m.limbs;
|
||||
size_t bytes = limbs * sizeof( *X_raw );
|
||||
|
||||
if( expected_ret == 0 )
|
||||
{
|
||||
/* Negative test with too many limbs in output */
|
||||
ASSERT_ALLOC( X_raw, limbs + 1 );
|
||||
|
||||
x.p = X_raw;
|
||||
x.limbs = limbs + 1;
|
||||
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
||||
mbedtls_mpi_mod_add( &x, &a, &b, &m ) );
|
||||
|
||||
mbedtls_free( X_raw );
|
||||
X_raw = NULL;
|
||||
|
||||
/* Negative test with too few limbs in output */
|
||||
if( limbs > 1 )
|
||||
{
|
||||
ASSERT_ALLOC( X_raw, limbs - 1 );
|
||||
|
||||
x.p = X_raw;
|
||||
x.limbs = limbs - 1;
|
||||
TEST_EQUAL( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
||||
mbedtls_mpi_mod_add( &x, &a, &b, &m ) );
|
||||
|
||||
mbedtls_free( X_raw );
|
||||
X_raw = NULL;
|
||||
}
|
||||
|
||||
/* Negative testing with too many/too few limbs in a and b is covered by
|
||||
* manually-written test cases with oret != 0. */
|
||||
}
|
||||
|
||||
/* Allocate correct number of limbs for X_raw */
|
||||
ASSERT_ALLOC( X_raw, limbs );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_mpi_mod_residue_setup( &x, &m, X_raw, limbs ) );
|
||||
|
||||
/* A + B => Correct result or expected error */
|
||||
TEST_EQUAL( expected_ret, mbedtls_mpi_mod_add( &x, &a, &b, &m ) );
|
||||
if( expected_ret != 0 )
|
||||
goto exit;
|
||||
|
||||
TEST_COMPARE_MPI_RESIDUES( x, s );
|
||||
|
||||
/* a + b: alias x to a => Correct result */
|
||||
memcpy( x.p, a.p, bytes );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &x, &b, &m ) );
|
||||
TEST_COMPARE_MPI_RESIDUES( x, s );
|
||||
|
||||
/* a + b: alias x to b => Correct result */
|
||||
memcpy( x.p, b.p, bytes );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &a, &x, &m ) );
|
||||
TEST_COMPARE_MPI_RESIDUES( x, s );
|
||||
|
||||
if ( memcmp( a.p, b.p, bytes ) == 0 )
|
||||
{
|
||||
/* a == b: alias a and b */
|
||||
|
||||
/* a + a => Correct result */
|
||||
TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &a, &a, &m ) );
|
||||
TEST_COMPARE_MPI_RESIDUES( x, s );
|
||||
|
||||
/* a + a: x, a, b all aliased together => Correct result */
|
||||
memcpy( x.p, a.p, bytes );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_mod_add( &x, &x, &x, &m ) );
|
||||
TEST_COMPARE_MPI_RESIDUES( x, s );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free( (void *)m.p ); /* mbedtls_mpi_mod_modulus_free() sets m.p = NULL */
|
||||
mbedtls_mpi_mod_modulus_free( &m );
|
||||
|
||||
mbedtls_free( a.p );
|
||||
mbedtls_free( b.p );
|
||||
mbedtls_free( s.p );
|
||||
mbedtls_free( X_raw );
|
||||
}
|
||||
/* END_CASE */
|
||||
/* END MERGE SLOT 5 */
|
||||
|
||||
/* BEGIN MERGE SLOT 6 */
|
||||
|
|
|
@ -45,7 +45,26 @@ mpi_mod_sub:"014320a022ccb75bdf470ddf25":"000000025a55a46e5da99c71c7":"e8000f93"
|
|||
# END MERGE SLOT 4
|
||||
|
||||
# BEGIN MERGE SLOT 5
|
||||
mpi_mod_add base case for negative testing (N, a, b all >= 1 limb)
|
||||
mpi_mod_add:"014320a022ccb75bdf470ddf25":"000000025a55a46e5da99c71c7":"00033b2e3c9fd0803ce8000f93":"00033b3096f574ee9a919c815a":0
|
||||
|
||||
mpi_mod_add with modulus too long/both inputs too short
|
||||
mpi_mod_add:"0000000014320a022ccb75bdf470ddf25":"000000025a55a46e5da99c71c7":"00033b2e3c9fd0803ce8000f93":"00":MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
|
||||
mpi_mod_add with first input too long
|
||||
mpi_mod_add:"014320a022ccb75bdf470ddf25":"0000000000000025a55a46e5da99c71c7":"00033b2e3c9fd0803ce8000f93":"00":MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
|
||||
mpi_mod_add with second input too long
|
||||
mpi_mod_add:"014320a022ccb75bdf470ddf25":"000000025a55a46e5da99c71c7":"000000000033b2e3c9fd0803ce8000f93":"00":MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
|
||||
mpi_mod_add with both inputs too long
|
||||
mpi_mod_add:"014320a022ccb75bdf470ddf25":"0000000000000025a55a46e5da99c71c7":"000000000033b2e3c9fd0803ce8000f93":"00":MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
|
||||
mpi_mod_add with first input too short
|
||||
mpi_mod_add:"014320a022ccb75bdf470ddf25":"a99c71c7":"00033b2e3c9fd0803ce8000f93":"00":MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
|
||||
mpi_mod_add with second input too short
|
||||
mpi_mod_add:"014320a022ccb75bdf470ddf25":"000000025a55a46e5da99c71c7":"e8000f93":"00":MBEDTLS_ERR_MPI_BAD_INPUT_DATA
|
||||
# END MERGE SLOT 5
|
||||
|
||||
# BEGIN MERGE SLOT 6
|
Loading…
Add table
Add a link
Reference in a new issue