Overhaul testing of mbedtls_mpi_copy

Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.

Also generalize mpi_copy_self to support arbitrary inputs.

Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:

```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
    "zero (null)" => "",
    "zero (1 limb)" => "0",
    "small positive" => "bead",
    "large positive" => "ca5cadedb01dfaceacc01ade",
    "small negative" => "-bead",
    "large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
    foreach $d (sort keys %v) {
        printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
               $s, $d, $v{$s}, rhs($v{$d});
    }
}
foreach $s (sort keys %v) {
    printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2021-06-10 15:17:30 +02:00
parent b53b218bf2
commit 77f55c9b00
2 changed files with 133 additions and 60 deletions

View file

@ -316,44 +316,131 @@ mbedtls_mpi_cmp_abs:10:"2":10:"-3":-1
Base test mbedtls_mpi_cmp_abs (Mix values) #3 Base test mbedtls_mpi_cmp_abs (Mix values) #3
mbedtls_mpi_cmp_abs:10:"-2":10:"1":1 mbedtls_mpi_cmp_abs:10:"-2":10:"1":1
Copy zero (1 limb) to positive (1 limb) Copy large negative to large negative
mbedtls_mpi_copy_sint:0:1500 mbedtls_mpi_copy:"-ca5cadedb01dfaceacc01ade":"-face1e55ca11ab1ecab005e5"
Copy zero (1 limb) to negative (1 limb) Copy large negative to large positive
mbedtls_mpi_copy_sint:0:-1500 mbedtls_mpi_copy:"-ca5cadedb01dfaceacc01ade":"face1e55ca11ab1ecab005e5"
Copy positive (1 limb) to zero (1 limb) Copy large negative to small negative
mbedtls_mpi_copy_sint:1500:0 mbedtls_mpi_copy:"-ca5cadedb01dfaceacc01ade":"-beef"
Copy negative (1 limb) to zero (1 limb) Copy large negative to small positive
mbedtls_mpi_copy_sint:-1500:0 mbedtls_mpi_copy:"-ca5cadedb01dfaceacc01ade":"beef"
Copy positive (1 limb) to negative (1 limb) Copy large negative to zero (1 limb)
mbedtls_mpi_copy_sint:1500:-42 mbedtls_mpi_copy:"-ca5cadedb01dfaceacc01ade":"0"
Copy negative (1 limb) to positive (1 limb) Copy large negative to zero (null)
mbedtls_mpi_copy_sint:-42:1500 mbedtls_mpi_copy:"-ca5cadedb01dfaceacc01ade":""
Copy large positive to large negative
mbedtls_mpi_copy:"ca5cadedb01dfaceacc01ade":"-face1e55ca11ab1ecab005e5"
Copy large positive to large positive
mbedtls_mpi_copy:"ca5cadedb01dfaceacc01ade":"face1e55ca11ab1ecab005e5"
Copy large positive to small negative
mbedtls_mpi_copy:"ca5cadedb01dfaceacc01ade":"-beef"
Copy large positive to small positive
mbedtls_mpi_copy:"ca5cadedb01dfaceacc01ade":"beef"
Copy large positive to zero (1 limb)
mbedtls_mpi_copy:"ca5cadedb01dfaceacc01ade":"0"
Copy large positive to zero (null)
mbedtls_mpi_copy:"ca5cadedb01dfaceacc01ade":""
Copy small negative to large negative
mbedtls_mpi_copy:"-bead":"-face1e55ca11ab1ecab005e5"
Copy small negative to large positive
mbedtls_mpi_copy:"-bead":"face1e55ca11ab1ecab005e5"
Copy small negative to small negative
mbedtls_mpi_copy:"-bead":"-beef"
Copy small negative to small positive
mbedtls_mpi_copy:"-bead":"beef"
Copy small negative to zero (1 limb)
mbedtls_mpi_copy:"-bead":"0"
Copy small negative to zero (null)
mbedtls_mpi_copy:"-bead":""
Copy small positive to large negative
mbedtls_mpi_copy:"bead":"-face1e55ca11ab1ecab005e5"
Copy small positive to large positive
mbedtls_mpi_copy:"bead":"face1e55ca11ab1ecab005e5"
Copy small positive to small negative
mbedtls_mpi_copy:"bead":"-beef"
Copy small positive to small positive
mbedtls_mpi_copy:"bead":"beef"
Copy small positive to zero (1 limb)
mbedtls_mpi_copy:"bead":"0"
Copy small positive to zero (null)
mbedtls_mpi_copy:"bead":""
Copy zero (1 limb) to large negative
mbedtls_mpi_copy:"0":"-face1e55ca11ab1ecab005e5"
Copy zero (1 limb) to large positive
mbedtls_mpi_copy:"0":"face1e55ca11ab1ecab005e5"
Copy zero (1 limb) to small negative
mbedtls_mpi_copy:"0":"-beef"
Copy zero (1 limb) to small positive
mbedtls_mpi_copy:"0":"beef"
Copy zero (1 limb) to zero (1 limb)
mbedtls_mpi_copy:"0":"0"
Copy zero (1 limb) to zero (null)
mbedtls_mpi_copy:"0":""
Copy zero (null) to large negative
mbedtls_mpi_copy:"":"-face1e55ca11ab1ecab005e5"
Copy zero (null) to large positive
mbedtls_mpi_copy:"":"face1e55ca11ab1ecab005e5"
Copy zero (null) to small negative
mbedtls_mpi_copy:"":"-beef"
Copy zero (null) to small positive
mbedtls_mpi_copy:"":"beef"
Copy zero (null) to zero (1 limb)
mbedtls_mpi_copy:"":"0"
Copy zero (null) to zero (null) Copy zero (null) to zero (null)
mbedtls_mpi_copy_binary:"":"" mbedtls_mpi_copy:"":""
Copy zero (null) to positive (1 limb) Copy self: large negative
mbedtls_mpi_copy_binary:"":"1234" mpi_copy_self:"-ca5cadedb01dfaceacc01ade"
Copy positive (1 limb) to zero (null) Copy self: large positive
mbedtls_mpi_copy_binary:"1234":"" mpi_copy_self:"ca5cadedb01dfaceacc01ade"
Copy positive to larger Copy self: small negative
mbedtls_mpi_copy_binary:"bead":"ca5cadedb01dfaceacc01ade" mpi_copy_self:"-bead"
Copy positive to smaller Copy self: small positive
mbedtls_mpi_copy_binary:"ca5cadedb01dfaceacc01ade":"bead" mpi_copy_self:"bead"
Copy self: positive (1 limb)
mpi_copy_self:14
Copy self: zero (1 limb) Copy self: zero (1 limb)
mpi_copy_self:0 mpi_copy_self:"0"
Copy self: zero (null)
mpi_copy_self:""
Swap zero (1 limb) with positive (1 limb) Swap zero (1 limb) with positive (1 limb)
mbedtls_mpi_swap_sint:0:1500 mbedtls_mpi_swap_sint:0:1500

View file

@ -716,56 +716,42 @@ exit:
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mbedtls_mpi_copy_sint( int input_X, int input_Y ) void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
{ {
mbedtls_mpi X, Y; mbedtls_mpi src, dst;
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &src );
mbedtls_mpi_init( &dst );
TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 ); TEST_ASSERT( mbedtls_test_read_mpi( &src, 16, src_hex ) == 0 );
TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 ); TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 ); TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
TEST_ASSERT( sign_is_valid( &Y ) );
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 ); TEST_ASSERT( sign_is_valid( &dst ) );
TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 ); TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
exit: exit:
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &src );
mbedtls_mpi_free( &dst );
} }
/* END_CASE */ /* END_CASE */
/* BEGIN_CASE */ /* BEGIN_CASE */
void mbedtls_mpi_copy_binary( data_t *input_X, data_t *input_Y ) void mpi_copy_self( char *input_X )
{ {
mbedtls_mpi X, Y, X0; mbedtls_mpi X, A;
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &A );
TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_binary( &Y, input_Y->x, input_Y->len ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_binary( &X0, input_X->x, input_X->len ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
TEST_ASSERT( sign_is_valid( &Y ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
exit:
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
}
/* END_CASE */
/* BEGIN_CASE */
void mpi_copy_self( int input_X )
{
mbedtls_mpi X;
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X );
TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 ); TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 ); TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
TEST_ASSERT( mbedtls_test_read_mpi( &A, 16, input_X ) == 0 );
TEST_ASSERT( sign_is_valid( &X ) );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
exit: exit:
mbedtls_mpi_free( &A );
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X );
} }
/* END_CASE */ /* END_CASE */