diff --git a/tests/suites/test_suite_bignum_core.data b/tests/suites/test_suite_bignum_core.data index 030015154..cb6fe424b 100644 --- a/tests/suites/test_suite_bignum_core.data +++ b/tests/suites/test_suite_bignum_core.data @@ -245,6 +245,45 @@ mpi_core_lt_ct:"11FFFFFFFFFFFFFFFF":"FF1111111111111111":1 mbedtls_mpi_core_lt_ct: x>y (alternating limbs) mpi_core_lt_ct:"FF1111111111111111":"11FFFFFFFFFFFFFFFF":0 +mbedtls_mpi_core_cond_assign: 1 limb +mpi_core_cond_assign:"FFFFFFFF":"11111111":4 + +mbedtls_mpi_core_cond_assign: more limbs #1 +mpi_core_cond_assign:"00000000FFFFFFFF55555555AAAAAAAA":"0123456789ABCDEF0123456789ABCDEF":16 + +mbedtls_mpi_core_cond_assign: more limbs #2 +mpi_core_cond_assign:"11111111EEEEEEEE77777777CCCCCCCC":"FEDCBA9876543210FEDCBA9876543210":16 + +mbedtls_mpi_core_cond_assign: more limbs #3 +mpi_core_cond_assign:"562D2B7E83BDC6FF783CEC0D6F46EAE7":"4C314E3B5CEB009C25F3300D5ECF670A":16 + +mbedtls_mpi_core_cond_assign: copy 256 bytes of limbs +mpi_core_cond_assign:"00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF":"6E3173EEAC8D68A5AB53D259F32D9E9C298FD2C4FAD3BEE9151DC103EA2382F5480C7D11F451C060A1E3D887E05A620EF6395763CB7A40FC473DD0771456A018E18635EA971C36DCAD09D60E8BD0E2E0CCD1AECB8BE0ABA881DBE60163F6C45947EC0B05FDAAA3DF944627DD4FACBAD3FF2AB4B99D91E548C06A4AF320A9CA0D2FD0CB19B90B9D6A8BF59CB631DD925B6DEA621FE962099D3D0BED6B13C0C546DC6B563A7FC63B1B77D277897DD7B9DF28C4C9213A183B83D982964C6AD8192CE7354B11ED727EDEF85074C46E4E2E6C1728FB7980385CDB36512F927847C6A14A118624ABC12B09DBEE60D651B5431AAD982228C61655EABB80C263871AE1CF":256 + +mbedtls_mpi_core_cond_assign: copy half of the limbs +mpi_core_cond_assign:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 + +mbedtls_mpi_core_cond_swap: same value +mpi_core_cond_swap:"FFFFFFFF":"FFFFFFFF":4 + +mbedtls_mpi_core_cond_swap: 1 limb +mpi_core_cond_swap:"FFFFFFFF":"11111111":4 + +mbedtls_mpi_core_cond_swap: more limbs #1 +mpi_core_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"0123456789ABCDEF0123456789ABCDEF":16 + +mbedtls_mpi_core_cond_swap: more limbs #2 +mpi_core_cond_swap:"11111111EEEEEEEE77777777CCCCCCCC":"FEDCBA9876543210FEDCBA9876543210":16 + +mbedtls_mpi_core_cond_swap: more limbs #3 +mpi_core_cond_swap:"562D2B7E83BDC6FF783CEC0D6F46EAE7":"4C314E3B5CEB009C25F3300D5ECF670A":16 + +mbedtls_mpi_core_cond_swap: copy 256 bytes of limbs +mpi_core_cond_swap:"00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF":"9FBBA284460D8EAB5E0D66B634BD18FBA58C0C25417DD637526A7622C6425B46E09AEFBB8C2340AC823DFE990A62C85DB23BCDBEA734134606CEEB4BCF7444569D5EC4E32341ED09D7A5D0BB8B11D7B726BAECCF37D4FC1BEBD892CADD7BE9E093343C1A68D7A188DFE145C1EDBD8048B24E20A076F981D75ABE44318ADC40ED316C444774B6A90D3EE49557315AA6FAB162A498C0B2E2C15BD94186A665E12DDC39211583FA5F21218A3B46999FEBA4DFF454FB6ED35B8F3AE5F8EA840838BD76006DA112F85EDAA2CC07518FFC9724D5695BAF74F16C8D1A3A06029D2F5C1023D9E8A84D1267BD9AF82D1F5F77092D34BE4E8C4D1EA8C58F90B094DCFD6920":256 + +mbedtls_mpi_core_cond_swap: copy half of the limbs +mpi_core_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 + mbedtls_mpi_montg_init #1 mpi_montg_init:"000000000000001d":"cb08d3dcb08d3dcb" diff --git a/tests/suites/test_suite_bignum_core.function b/tests/suites/test_suite_bignum_core.function index 2bd8aac28..de8b7f194 100644 --- a/tests/suites/test_suite_bignum_core.function +++ b/tests/suites/test_suite_bignum_core.function @@ -196,6 +196,148 @@ exit: } /* END_CASE */ +/* BEGIN_CASE */ +void mpi_core_cond_assign( data_t * input_X, + data_t * input_Y, + int input_bytes ) +{ + mbedtls_mpi_uint *X = NULL; + mbedtls_mpi_uint *Y = NULL; + size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); + size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); + size_t limbs = limbs_X; + size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); + size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); + size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint ); + + TEST_EQUAL( limbs_X, limbs_Y ); + TEST_ASSERT( copy_limbs <= limbs ); + + ASSERT_ALLOC( X, limbs ); + ASSERT_ALLOC( Y, limbs ); + + TEST_ASSERT( mbedtls_mpi_core_read_be( X, limbs, input_X->x, input_X->len ) + == 0 ); + + TEST_ASSERT( mbedtls_mpi_core_read_be( Y, limbs, input_Y->x, input_Y->len ) + == 0 ); + + /* condition is false */ + TEST_CF_SECRET( X, bytes ); + TEST_CF_SECRET( Y, bytes ); + + mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 0 ); + + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + + TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); + + /* condition is true */ + TEST_CF_SECRET( X, bytes ); + TEST_CF_SECRET( Y, bytes ); + + mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 1 ); + + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + + /* Check if the given length is copied even it is smaller + than the length of the given MPIs. */ + if( copy_limbs < limbs ) + { + ASSERT_COMPARE( X, copy_bytes, Y, copy_bytes ); + TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); + } + else + ASSERT_COMPARE( X, bytes, Y, bytes ); + +exit: + mbedtls_free( X ); + mbedtls_free( Y ); +} +/* END_CASE */ + +/* BEGIN_CASE */ +void mpi_core_cond_swap( data_t * input_X, + data_t * input_Y, + int input_bytes ) +{ + mbedtls_mpi_uint *tmp_X = NULL; + mbedtls_mpi_uint *tmp_Y = NULL; + mbedtls_mpi_uint *X = NULL; + mbedtls_mpi_uint *Y = NULL; + size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); + size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); + size_t limbs = limbs_X; + size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); + size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); + size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint ); + + TEST_EQUAL( limbs_X, limbs_Y ); + TEST_ASSERT( copy_limbs <= limbs ); + + ASSERT_ALLOC( tmp_X, limbs ); + ASSERT_ALLOC( tmp_Y, limbs ); + + TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, limbs, + input_X->x, input_X->len ) + == 0 ); + ASSERT_ALLOC( X, limbs ); + memcpy( X, tmp_X, bytes ); + + TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, limbs, + input_Y->x, input_Y->len ) + == 0 ); + ASSERT_ALLOC( Y, limbs ); + memcpy( Y, tmp_Y, bytes ); + + /* condition is false */ + TEST_CF_SECRET( X, bytes ); + TEST_CF_SECRET( Y, bytes ); + + mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 0 ); + + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + + ASSERT_COMPARE( X, bytes, tmp_X, bytes ); + ASSERT_COMPARE( Y, bytes, tmp_Y, bytes ); + + /* condition is true */ + TEST_CF_SECRET( X, bytes ); + TEST_CF_SECRET( Y, bytes ); + + mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 1 ); + + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + + /* Check if the given length is copied even it is smaller + than the length of the given MPIs. */ + if( copy_limbs < limbs ) + { + ASSERT_COMPARE( X, copy_bytes, tmp_Y, copy_bytes ); + ASSERT_COMPARE( Y, copy_bytes, tmp_X, copy_bytes ); + TEST_ASSERT( memcmp( X, tmp_X, bytes ) != 0 ); + TEST_ASSERT( memcmp( X, tmp_Y, bytes ) != 0 ); + TEST_ASSERT( memcmp( Y, tmp_X, bytes ) != 0 ); + TEST_ASSERT( memcmp( Y, tmp_Y, bytes ) != 0 ); + } + else + { + ASSERT_COMPARE( X, bytes, tmp_Y, bytes ); + ASSERT_COMPARE( Y, bytes, tmp_X, bytes ); + } + +exit: + mbedtls_free( tmp_X ); + mbedtls_free( tmp_Y ); + mbedtls_free( X ); + mbedtls_free( Y ); +} +/* END_CASE */ + /* BEGIN_CASE */ void mpi_core_add_if( char * input_A, char * input_B, char * input_S4, int carry4, diff --git a/tests/suites/test_suite_bignum_mod_raw.data b/tests/suites/test_suite_bignum_mod_raw.data index 6b5bb645e..788165d1a 100644 --- a/tests/suites/test_suite_bignum_mod_raw.data +++ b/tests/suites/test_suite_bignum_mod_raw.data @@ -139,3 +139,42 @@ mpi_mod_raw_io:"":1:1:MBEDTLS_MPI_MOD_EXT_REP_INVALID:MBEDTLS_ERR_MPI_BAD_INPUT_ Test mbedtls_mpi_mod_raw_io #22 (writing with invalid endianness) mpi_mod_raw_io:"":1:1:MBEDTLS_MPI_MOD_EXT_REP_INVALID:0:MBEDTLS_ERR_MPI_BAD_INPUT_DATA + +mbedtls_mpi_mod_raw_cond_assign: 1 limb +mpi_mod_raw_cond_assign:"FFFFFFFF":"11111111":4 + +mbedtls_mpi_mod_raw_cond_assign: more limbs #1 +mpi_mod_raw_cond_assign:"00000000FFFFFFFF55555555AAAAAAAA":"0123456789ABCDEF0123456789ABCDEF":16 + +mbedtls_mpi_mod_raw_cond_assign: more limbs #2 +mpi_mod_raw_cond_assign:"11111111EEEEEEEE77777777CCCCCCCC":"FEDCBA9876543210FEDCBA9876543210":16 + +mbedtls_mpi_mod_raw_cond_assign: more limbs #3 +mpi_mod_raw_cond_assign:"562D2B7E83BDC6FF783CEC0D6F46EAE7":"4C314E3B5CEB009C25F3300D5ECF670A":16 + +mbedtls_mpi_mod_raw_cond_assign: copy 256 bytes of limbs +mpi_mod_raw_cond_assign:"00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF":"38AAE958A11CCE9F4B4D0EDF64D31DF6FEA451D56B3BAB3DB796739B96C5C9C619DAE2AD5394D41FC3746A4F35BA186F02D908E900A2BA5AC95D494378A6A6FC619801DA3F3B4C5BAAA49B16DD3B2EF9AFB4B82449F90628E1438405D321ACA8476EA3E7124D46D5F25CD57143F2C6A022F7D6EBF7C726AEF56D97E3944EACD627A814D2F546F5A659DD316AE57584BC46F0D976D537E78AB09D2599BB0F073366C541CEF4B6D76FE72D000A01167FCE506B805B295C12BFCAA3B768D8E6AA50D939BDB519A94000B7B18B2D36AE888EDC33DC933E9612D112BB7A95D0748F175E69F00B9D8470C64E27B4FF39551B09FF046F864E9FC22A3E1F73570DF31214":256 + +mbedtls_mpi_mod_raw_cond_assign: copy half of the limbs +mpi_mod_raw_cond_assign:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 + +mbedtls_mpi_mod_raw_cond_swap: same value +mpi_mod_raw_cond_swap:"FFFFFFFF":"FFFFFFFF":4 + +mbedtls_mpi_mod_raw_cond_swap: 1 limb +mpi_mod_raw_cond_swap:"FFFFFFFF":"11111111":4 + +mbedtls_mpi_mod_raw_cond_swap: more limbs #1 +mpi_mod_raw_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"0123456789ABCDEF0123456789ABCDEF":16 + +mbedtls_mpi_mod_raw_cond_swap: more limbs #2 +mpi_mod_raw_cond_swap:"11111111EEEEEEEE77777777CCCCCCCC":"FEDCBA9876543210FEDCBA9876543210":16 + +mbedtls_mpi_mod_raw_cond_swap: more limbs #3 +mpi_mod_raw_cond_swap:"562D2B7E83BDC6FF783CEC0D6F46EAE7":"4C314E3B5CEB009C25F3300D5ECF670A":16 + +mbedtls_mpi_mod_raw_cond_swap: copy 256 bytes of limbs +mpi_mod_raw_cond_swap:"00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF":"A6154F6146E5AE06C705EF228DB022B4C0C908D9FF11E83CB0836A37DD11D72B5AE0D053B6D0B376B015E3B09DCC04644F12CBCBD0407300DEDA1D817E99A030BA128A57494F6C6BC650A840B70B3A1CDF8A75A387184C93DEF02DD538C60E8764AD2E4600952E09D24DC5524AB34070D25D204174D23B2BFDACBC67F8973C47FAF01CE9810130934AB6A94C5E6D223EDEAE904FD18368231AC3113C2CD73E879124CE21FC379C695C41D6861F5FB957570C3E6EF61F4FBA5D9945FB42931FC8E68370177DB313BEB7AF79B1F0F872368946FF35BD059E60677342EB3A0431714044AD508CD244B7CE200C5DC75B3BC7846A4CF0436C9288F2153124C9283171":256 + +mbedtls_mpi_mod_raw_cond_swap: copy half of the limbs +mpi_mod_raw_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 diff --git a/tests/suites/test_suite_bignum_mod_raw.function b/tests/suites/test_suite_bignum_mod_raw.function index 76574752b..099c94432 100644 --- a/tests/suites/test_suite_bignum_mod_raw.function +++ b/tests/suites/test_suite_bignum_mod_raw.function @@ -106,3 +106,171 @@ exit: mbedtls_mpi_mod_modulus_free( &m ); } /* END_CASE */ + +/* BEGIN_CASE */ +void mpi_mod_raw_cond_assign( data_t * input_X, + data_t * input_Y, + int input_bytes ) +{ + mbedtls_mpi_uint *X = NULL; + mbedtls_mpi_uint *Y = NULL; + mbedtls_mpi_uint *buff_m = NULL; + mbedtls_mpi_mod_modulus m; + size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); + size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); + size_t limbs = limbs_X; + size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); + size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); + size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint ); + + TEST_EQUAL( limbs_X, limbs_Y ); + TEST_ASSERT( copy_limbs <= limbs ); + + ASSERT_ALLOC( X, limbs ); + ASSERT_ALLOC( Y, limbs ); + + ASSERT_ALLOC( buff_m, limbs ); + mbedtls_mpi_mod_modulus_init( &m ); + TEST_ASSERT( mbedtls_mpi_mod_modulus_setup( + &m, buff_m, copy_limbs, + MBEDTLS_MPI_MOD_EXT_REP_BE, + MBEDTLS_MPI_MOD_REP_MONTGOMERY ) + == 0 ); + + TEST_ASSERT( mbedtls_mpi_core_read_be( X, limbs, + input_X->x, input_X->len ) + == 0 ); + + TEST_ASSERT( mbedtls_mpi_core_read_be( Y, limbs, + input_Y->x, input_Y->len ) + == 0 ); + + /* condition is false */ + TEST_CF_SECRET( X, bytes ); + TEST_CF_SECRET( Y, bytes ); + + mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 0 ); + + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + + TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); + + /* condition is true */ + TEST_CF_SECRET( X, bytes ); + TEST_CF_SECRET( Y, bytes ); + + mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 1 ); + + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + + /* Check if the given length is copied even it is smaller + than the length of the given MPIs. */ + if( copy_limbs len ); + size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); + size_t limbs = limbs_X; + size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); + size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); + size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint ); + + TEST_EQUAL( limbs_X, limbs_Y ); + TEST_ASSERT( copy_limbs <= limbs ); + + ASSERT_ALLOC( tmp_X, limbs ); + ASSERT_ALLOC( tmp_Y, limbs ); + + ASSERT_ALLOC( buff_m, copy_limbs ); + mbedtls_mpi_mod_modulus_init( &m ); + TEST_ASSERT( mbedtls_mpi_mod_modulus_setup( + &m, buff_m, copy_limbs, + MBEDTLS_MPI_MOD_EXT_REP_BE, + MBEDTLS_MPI_MOD_REP_MONTGOMERY ) + == 0 ); + + TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, limbs, input_X->x, input_X->len ) + == 0 ); + ASSERT_ALLOC( X, limbs ); + memcpy( X, tmp_X, bytes ); + + TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, limbs, input_Y->x, input_Y->len ) + == 0 ); + ASSERT_ALLOC( Y, bytes ); + memcpy( Y, tmp_Y, bytes ); + + /* condition is false */ + TEST_CF_SECRET( X, bytes ); + TEST_CF_SECRET( Y, bytes ); + + mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 0 ); + + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + + ASSERT_COMPARE( X, bytes, tmp_X, bytes ); + ASSERT_COMPARE( Y, bytes, tmp_Y, bytes ); + + /* condition is true */ + TEST_CF_SECRET( X, bytes ); + TEST_CF_SECRET( Y, bytes ); + + mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 1 ); + + TEST_CF_PUBLIC( X, bytes ); + TEST_CF_PUBLIC( Y, bytes ); + + /* Check if the given length is copied even it is smaller + than the length of the given MPIs. */ + if( copy_limbs < limbs ) + { + ASSERT_COMPARE( X, copy_bytes, tmp_Y, copy_bytes ); + ASSERT_COMPARE( Y, copy_bytes, tmp_X, copy_bytes ); + TEST_ASSERT( memcmp( X, tmp_X, bytes ) != 0 ); + TEST_ASSERT( memcmp( X, tmp_Y, bytes ) != 0 ); + TEST_ASSERT( memcmp( Y, tmp_X, bytes ) != 0 ); + TEST_ASSERT( memcmp( Y, tmp_Y, bytes ) != 0 ); + } + else + { + ASSERT_COMPARE( X, bytes, tmp_Y, bytes ); + ASSERT_COMPARE( Y, bytes, tmp_X, bytes ); + } + +exit: + mbedtls_free( tmp_X ); + mbedtls_free( tmp_Y ); + mbedtls_free( X ); + mbedtls_free( Y ); + + mbedtls_mpi_mod_modulus_free( &m ); + mbedtls_free( buff_m ); +} +/* END_CASE */ diff --git a/tests/suites/test_suite_mpi.data b/tests/suites/test_suite_mpi.data index 794031901..78afcb64c 100644 --- a/tests/suites/test_suite_mpi.data +++ b/tests/suites/test_suite_mpi.data @@ -301,84 +301,6 @@ mpi_cmp_mpi:"-1230000000000000000":"":-1 Test mbedtls_mpi_cmp_mpi: large negative < 0 (1 limb) mpi_cmp_mpi:"-1230000000000000000":"0":-1 -mbedtls_mpi_core_cond_assign: 1 limb -mpi_core_cond_assign:"FFFFFFFF":"11111111":4 - -mbedtls_mpi_core_cond_assign: more limbs #1 -mpi_core_cond_assign:"00000000FFFFFFFF55555555AAAAAAAA":"0123456789ABCDEF0123456789ABCDEF":16 - -mbedtls_mpi_core_cond_assign: more limbs #2 -mpi_core_cond_assign:"11111111EEEEEEEE77777777CCCCCCCC":"FEDCBA9876543210FEDCBA9876543210":16 - -mbedtls_mpi_core_cond_assign: more limbs #3 -mpi_core_cond_assign:"562D2B7E83BDC6FF783CEC0D6F46EAE7":"4C314E3B5CEB009C25F3300D5ECF670A":16 - -mbedtls_mpi_core_cond_assign: copy 256 bytes of limbs -mpi_core_cond_assign:"00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF":"6E3173EEAC8D68A5AB53D259F32D9E9C298FD2C4FAD3BEE9151DC103EA2382F5480C7D11F451C060A1E3D887E05A620EF6395763CB7A40FC473DD0771456A018E18635EA971C36DCAD09D60E8BD0E2E0CCD1AECB8BE0ABA881DBE60163F6C45947EC0B05FDAAA3DF944627DD4FACBAD3FF2AB4B99D91E548C06A4AF320A9CA0D2FD0CB19B90B9D6A8BF59CB631DD925B6DEA621FE962099D3D0BED6B13C0C546DC6B563A7FC63B1B77D277897DD7B9DF28C4C9213A183B83D982964C6AD8192CE7354B11ED727EDEF85074C46E4E2E6C1728FB7980385CDB36512F927847C6A14A118624ABC12B09DBEE60D651B5431AAD982228C61655EABB80C263871AE1CF":256 - -mbedtls_mpi_core_cond_assign: copy half of the limbs -mpi_core_cond_assign:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 - -mbedtls_mpi_core_cond_swap: same value -mpi_core_cond_swap:"FFFFFFFF":"FFFFFFFF":4 - -mbedtls_mpi_core_cond_swap: 1 limb -mpi_core_cond_swap:"FFFFFFFF":"11111111":4 - -mbedtls_mpi_core_cond_swap: more limbs #1 -mpi_core_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"0123456789ABCDEF0123456789ABCDEF":16 - -mbedtls_mpi_core_cond_swap: more limbs #2 -mpi_core_cond_swap:"11111111EEEEEEEE77777777CCCCCCCC":"FEDCBA9876543210FEDCBA9876543210":16 - -mbedtls_mpi_core_cond_swap: more limbs #3 -mpi_core_cond_swap:"562D2B7E83BDC6FF783CEC0D6F46EAE7":"4C314E3B5CEB009C25F3300D5ECF670A":16 - -mbedtls_mpi_core_cond_swap: copy 256 bytes of limbs -mpi_core_cond_swap:"00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF":"9FBBA284460D8EAB5E0D66B634BD18FBA58C0C25417DD637526A7622C6425B46E09AEFBB8C2340AC823DFE990A62C85DB23BCDBEA734134606CEEB4BCF7444569D5EC4E32341ED09D7A5D0BB8B11D7B726BAECCF37D4FC1BEBD892CADD7BE9E093343C1A68D7A188DFE145C1EDBD8048B24E20A076F981D75ABE44318ADC40ED316C444774B6A90D3EE49557315AA6FAB162A498C0B2E2C15BD94186A665E12DDC39211583FA5F21218A3B46999FEBA4DFF454FB6ED35B8F3AE5F8EA840838BD76006DA112F85EDAA2CC07518FFC9724D5695BAF74F16C8D1A3A06029D2F5C1023D9E8A84D1267BD9AF82D1F5F77092D34BE4E8C4D1EA8C58F90B094DCFD6920":256 - -mbedtls_mpi_core_cond_swap: copy half of the limbs -mpi_core_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 - -mbedtls_mpi_mod_raw_cond_assign: 1 limb -mpi_mod_raw_cond_assign:"FFFFFFFF":"11111111":4 - -mbedtls_mpi_mod_raw_cond_assign: more limbs #1 -mpi_mod_raw_cond_assign:"00000000FFFFFFFF55555555AAAAAAAA":"0123456789ABCDEF0123456789ABCDEF":16 - -mbedtls_mpi_mod_raw_cond_assign: more limbs #2 -mpi_mod_raw_cond_assign:"11111111EEEEEEEE77777777CCCCCCCC":"FEDCBA9876543210FEDCBA9876543210":16 - -mbedtls_mpi_mod_raw_cond_assign: more limbs #3 -mpi_mod_raw_cond_assign:"562D2B7E83BDC6FF783CEC0D6F46EAE7":"4C314E3B5CEB009C25F3300D5ECF670A":16 - -mbedtls_mpi_mod_raw_cond_assign: copy 256 bytes of limbs -mpi_mod_raw_cond_assign:"00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF":"38AAE958A11CCE9F4B4D0EDF64D31DF6FEA451D56B3BAB3DB796739B96C5C9C619DAE2AD5394D41FC3746A4F35BA186F02D908E900A2BA5AC95D494378A6A6FC619801DA3F3B4C5BAAA49B16DD3B2EF9AFB4B82449F90628E1438405D321ACA8476EA3E7124D46D5F25CD57143F2C6A022F7D6EBF7C726AEF56D97E3944EACD627A814D2F546F5A659DD316AE57584BC46F0D976D537E78AB09D2599BB0F073366C541CEF4B6D76FE72D000A01167FCE506B805B295C12BFCAA3B768D8E6AA50D939BDB519A94000B7B18B2D36AE888EDC33DC933E9612D112BB7A95D0748F175E69F00B9D8470C64E27B4FF39551B09FF046F864E9FC22A3E1F73570DF31214":256 - -mbedtls_mpi_mod_raw_cond_assign: copy half of the limbs -mpi_mod_raw_cond_assign:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 - -mbedtls_mpi_mod_raw_cond_swap: same value -mpi_mod_raw_cond_swap:"FFFFFFFF":"FFFFFFFF":4 - -mbedtls_mpi_mod_raw_cond_swap: 1 limb -mpi_mod_raw_cond_swap:"FFFFFFFF":"11111111":4 - -mbedtls_mpi_mod_raw_cond_swap: more limbs #1 -mpi_mod_raw_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"0123456789ABCDEF0123456789ABCDEF":16 - -mbedtls_mpi_mod_raw_cond_swap: more limbs #2 -mpi_mod_raw_cond_swap:"11111111EEEEEEEE77777777CCCCCCCC":"FEDCBA9876543210FEDCBA9876543210":16 - -mbedtls_mpi_mod_raw_cond_swap: more limbs #3 -mpi_mod_raw_cond_swap:"562D2B7E83BDC6FF783CEC0D6F46EAE7":"4C314E3B5CEB009C25F3300D5ECF670A":16 - -mbedtls_mpi_mod_raw_cond_swap: copy 256 bytes of limbs -mpi_mod_raw_cond_swap:"00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF00000000111111112222222233333333444444445555555566666666777777778888888899999999AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDEEEEEEEEFFFFFFFF":"A6154F6146E5AE06C705EF228DB022B4C0C908D9FF11E83CB0836A37DD11D72B5AE0D053B6D0B376B015E3B09DCC04644F12CBCBD0407300DEDA1D817E99A030BA128A57494F6C6BC650A840B70B3A1CDF8A75A387184C93DEF02DD538C60E8764AD2E4600952E09D24DC5524AB34070D25D204174D23B2BFDACBC67F8973C47FAF01CE9810130934AB6A94C5E6D223EDEAE904FD18368231AC3113C2CD73E879124CE21FC379C695C41D6861F5FB957570C3E6EF61F4FBA5D9945FB42931FC8E68370177DB313BEB7AF79B1F0F872368946FF35BD059E60677342EB3A0431714044AD508CD244B7CE200C5DC75B3BC7846A4CF0436C9288F2153124C9283171":256 - -mbedtls_mpi_mod_raw_cond_swap: copy half of the limbs -mpi_mod_raw_cond_swap:"00000000FFFFFFFF55555555AAAAAAAA":"FEDCBA9876543210FEDCBA9876543210":8 - Base test mbedtls_mpi_lt_mpi_ct #1 mpi_lt_mpi_ct:1:"2B5":1:"2B5":0:0 diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index db274100a..4cec0a7c7 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -457,316 +457,6 @@ exit: } /* END_CASE */ -/* BEGIN_CASE */ -void mpi_core_cond_assign( data_t * input_X, - data_t * input_Y, - int input_bytes ) -{ - mbedtls_mpi_uint *X = NULL; - mbedtls_mpi_uint *Y = NULL; - size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); - size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); - size_t limbs = limbs_X; - size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); - size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); - size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint ); - - TEST_EQUAL( limbs_X, limbs_Y ); - TEST_ASSERT( copy_limbs <= limbs ); - - ASSERT_ALLOC( X, limbs ); - ASSERT_ALLOC( Y, limbs ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( X, limbs, input_X->x, input_X->len ) - == 0 ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( Y, limbs, input_Y->x, input_Y->len ) - == 0 ); - - /* condition is false */ - TEST_CF_SECRET( X, bytes ); - TEST_CF_SECRET( Y, bytes ); - - mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 0 ); - - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - - TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); - - /* condition is true */ - TEST_CF_SECRET( X, bytes ); - TEST_CF_SECRET( Y, bytes ); - - mbedtls_mpi_core_cond_assign( X, Y, copy_limbs, 1 ); - - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - - /* Check if the given length is copied even it is smaller - than the length of the given MPIs. */ - if( copy_limbs < limbs ) - { - ASSERT_COMPARE( X, copy_bytes, Y, copy_bytes ); - TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); - } - else - ASSERT_COMPARE( X, bytes, Y, bytes ); - -exit: - mbedtls_free( X ); - mbedtls_free( Y ); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void mpi_core_cond_swap( data_t * input_X, - data_t * input_Y, - int input_bytes ) -{ - mbedtls_mpi_uint *tmp_X = NULL; - mbedtls_mpi_uint *tmp_Y = NULL; - mbedtls_mpi_uint *X = NULL; - mbedtls_mpi_uint *Y = NULL; - size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); - size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); - size_t limbs = limbs_X; - size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); - size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); - size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint ); - - TEST_EQUAL( limbs_X, limbs_Y ); - TEST_ASSERT( copy_limbs <= limbs ); - - ASSERT_ALLOC( tmp_X, limbs ); - ASSERT_ALLOC( tmp_Y, limbs ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, limbs, - input_X->x, input_X->len ) - == 0 ); - ASSERT_ALLOC( X, limbs ); - memcpy( X, tmp_X, bytes ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, limbs, - input_Y->x, input_Y->len ) - == 0 ); - ASSERT_ALLOC( Y, limbs ); - memcpy( Y, tmp_Y, bytes ); - - /* condition is false */ - TEST_CF_SECRET( X, bytes ); - TEST_CF_SECRET( Y, bytes ); - - mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 0 ); - - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - - ASSERT_COMPARE( X, bytes, tmp_X, bytes ); - ASSERT_COMPARE( Y, bytes, tmp_Y, bytes ); - - /* condition is true */ - TEST_CF_SECRET( X, bytes ); - TEST_CF_SECRET( Y, bytes ); - - mbedtls_mpi_core_cond_swap( X, Y, copy_limbs, 1 ); - - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - - /* Check if the given length is copied even it is smaller - than the length of the given MPIs. */ - if( copy_limbs < limbs ) - { - ASSERT_COMPARE( X, copy_bytes, tmp_Y, copy_bytes ); - ASSERT_COMPARE( Y, copy_bytes, tmp_X, copy_bytes ); - TEST_ASSERT( memcmp( X, tmp_X, bytes ) != 0 ); - TEST_ASSERT( memcmp( X, tmp_Y, bytes ) != 0 ); - TEST_ASSERT( memcmp( Y, tmp_X, bytes ) != 0 ); - TEST_ASSERT( memcmp( Y, tmp_Y, bytes ) != 0 ); - } - else - { - ASSERT_COMPARE( X, bytes, tmp_Y, bytes ); - ASSERT_COMPARE( Y, bytes, tmp_X, bytes ); - } - -exit: - mbedtls_free( tmp_X ); - mbedtls_free( tmp_Y ); - mbedtls_free( X ); - mbedtls_free( Y ); -} -/* END_CASE */ - -/* BEGIN_CASE */ -void mpi_mod_raw_cond_assign( data_t * input_X, - data_t * input_Y, - int input_bytes ) -{ - mbedtls_mpi_uint *X = NULL; - mbedtls_mpi_uint *Y = NULL; - mbedtls_mpi_uint *buff_m = NULL; - mbedtls_mpi_mod_modulus m; - size_t limbs_X = CHARS_TO_LIMBS( input_X->len ); - size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); - size_t limbs = limbs_X; - size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); - size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); - size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint ); - - TEST_EQUAL( limbs_X, limbs_Y ); - TEST_ASSERT( copy_limbs <= limbs ); - - ASSERT_ALLOC( X, limbs ); - ASSERT_ALLOC( Y, limbs ); - - ASSERT_ALLOC( buff_m, limbs ); - mbedtls_mpi_mod_modulus_init( &m ); - TEST_ASSERT( mbedtls_mpi_mod_modulus_setup( - &m, buff_m, copy_limbs, - MBEDTLS_MPI_MOD_EXT_REP_BE, - MBEDTLS_MPI_MOD_REP_MONTGOMERY ) - == 0 ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( X, limbs, - input_X->x, input_X->len ) - == 0 ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( Y, limbs, - input_Y->x, input_Y->len ) - == 0 ); - - /* condition is false */ - TEST_CF_SECRET( X, bytes ); - TEST_CF_SECRET( Y, bytes ); - - mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 0 ); - - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - - TEST_ASSERT( memcmp( X, Y, bytes ) != 0 ); - - /* condition is true */ - TEST_CF_SECRET( X, bytes ); - TEST_CF_SECRET( Y, bytes ); - - mbedtls_mpi_mod_raw_cond_assign( X, Y, &m, 1 ); - - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - - /* Check if the given length is copied even it is smaller - than the length of the given MPIs. */ - if( copy_limbs len ); - size_t limbs_Y = CHARS_TO_LIMBS( input_Y->len ); - size_t limbs = limbs_X; - size_t copy_limbs = CHARS_TO_LIMBS( input_bytes ); - size_t bytes = limbs * sizeof( mbedtls_mpi_uint ); - size_t copy_bytes = copy_limbs * sizeof( mbedtls_mpi_uint ); - - TEST_EQUAL( limbs_X, limbs_Y ); - TEST_ASSERT( copy_limbs <= limbs ); - - ASSERT_ALLOC( tmp_X, limbs ); - ASSERT_ALLOC( tmp_Y, limbs ); - - ASSERT_ALLOC( buff_m, copy_limbs ); - mbedtls_mpi_mod_modulus_init( &m ); - TEST_ASSERT( mbedtls_mpi_mod_modulus_setup( - &m, buff_m, copy_limbs, - MBEDTLS_MPI_MOD_EXT_REP_BE, - MBEDTLS_MPI_MOD_REP_MONTGOMERY ) - == 0 ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_X, limbs, input_X->x, input_X->len ) - == 0 ); - ASSERT_ALLOC( X, limbs ); - memcpy( X, tmp_X, bytes ); - - TEST_ASSERT( mbedtls_mpi_core_read_be( tmp_Y, limbs, input_Y->x, input_Y->len ) - == 0 ); - ASSERT_ALLOC( Y, bytes ); - memcpy( Y, tmp_Y, bytes ); - - /* condition is false */ - TEST_CF_SECRET( X, bytes ); - TEST_CF_SECRET( Y, bytes ); - - mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 0 ); - - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - - ASSERT_COMPARE( X, bytes, tmp_X, bytes ); - ASSERT_COMPARE( Y, bytes, tmp_Y, bytes ); - - /* condition is true */ - TEST_CF_SECRET( X, bytes ); - TEST_CF_SECRET( Y, bytes ); - - mbedtls_mpi_mod_raw_cond_swap( X, Y, &m, 1 ); - - TEST_CF_PUBLIC( X, bytes ); - TEST_CF_PUBLIC( Y, bytes ); - - /* Check if the given length is copied even it is smaller - than the length of the given MPIs. */ - if( copy_limbs < limbs ) - { - ASSERT_COMPARE( X, copy_bytes, tmp_Y, copy_bytes ); - ASSERT_COMPARE( Y, copy_bytes, tmp_X, copy_bytes ); - TEST_ASSERT( memcmp( X, tmp_X, bytes ) != 0 ); - TEST_ASSERT( memcmp( X, tmp_Y, bytes ) != 0 ); - TEST_ASSERT( memcmp( Y, tmp_X, bytes ) != 0 ); - TEST_ASSERT( memcmp( Y, tmp_Y, bytes ) != 0 ); - } - else - { - ASSERT_COMPARE( X, bytes, tmp_Y, bytes ); - ASSERT_COMPARE( Y, bytes, tmp_X, bytes ); - } - -exit: - mbedtls_free( tmp_X ); - mbedtls_free( tmp_Y ); - mbedtls_free( X ); - mbedtls_free( Y ); - - mbedtls_mpi_mod_modulus_free( &m ); - mbedtls_free( buff_m ); -} -/* END_CASE */ - /* BEGIN_CASE */ void mpi_lt_mpi_ct( int size_X, char * input_X, int size_Y, char * input_Y,