2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/bignum.h"
|
2020-11-25 15:37:20 +01:00
|
|
|
#include "mbedtls/entropy.h"
|
2022-07-22 19:24:06 +02:00
|
|
|
#include "constant_time_internal.h"
|
2022-09-27 22:04:51 +02:00
|
|
|
#include "bignum_core.h"
|
2022-07-22 19:24:06 +02:00
|
|
|
#include "test/constant_flow.h"
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
|
2020-12-03 18:44:03 +01:00
|
|
|
#if MBEDTLS_MPI_MAX_BITS > 792
|
|
|
|
#define MPI_MAX_BITS_LARGER_THAN_792
|
2020-12-03 15:24:33 +01:00
|
|
|
#endif
|
2022-08-12 15:36:56 +02:00
|
|
|
|
2021-06-10 15:34:15 +02:00
|
|
|
/* Check the validity of the sign bit in an MPI object. Reject representations
|
|
|
|
* that are not supported by the rest of the library and indicate a bug when
|
|
|
|
* constructing the value. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static int sign_is_valid(const mbedtls_mpi *X)
|
2021-06-10 15:34:15 +02:00
|
|
|
{
|
2022-11-09 21:08:44 +01:00
|
|
|
/* Only +1 and -1 are valid sign bits, not e.g. 0 */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (X->s != 1 && X->s != -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-11-09 21:08:44 +01:00
|
|
|
|
|
|
|
/* The value 0 must be represented with the sign +1. A "negative zero"
|
|
|
|
* with s=-1 is an invalid representation. Forbid that. As an exception,
|
|
|
|
* we sometimes test the robustness of library functions when given
|
|
|
|
* a negative zero input. If a test case has a negative zero as input,
|
|
|
|
* we don't mind if the function has a negative zero output. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_test_case_uses_negative_0 &&
|
|
|
|
mbedtls_mpi_bitlen(X) == 0 && X->s != 1) {
|
|
|
|
return 0;
|
2022-11-09 21:08:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 1;
|
2021-06-10 15:34:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct mbedtls_test_mpi_random {
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
data_t *data;
|
|
|
|
size_t pos;
|
|
|
|
size_t chunk_len;
|
|
|
|
} mbedtls_test_mpi_random;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called by the Miller-Rabin primality test each time it
|
|
|
|
* chooses a random witness. The witnesses (or non-witnesses as provided by the
|
|
|
|
* test) are stored in the data member of the state structure. Each number is in
|
|
|
|
* the format that mbedtls_mpi_read_string understands and is chunk_len long.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_test_mpi_miller_rabin_determinizer(void *state,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t len)
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random *) state;
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (random == NULL || random->data->x == NULL || buf == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (random->pos + random->chunk_len > random->data->len
|
|
|
|
|| random->chunk_len > len) {
|
|
|
|
return -1;
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, len);
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
|
|
|
|
/* The witness is written to the end of the buffer, since the buffer is
|
|
|
|
* used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
|
|
|
|
* Writing the witness to the start of the buffer would result in the
|
|
|
|
* buffer being 'witness 000...000', which would be treated as
|
|
|
|
* witness * 2^n for some n. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(buf + len - random->chunk_len, &random->data->x[random->pos],
|
|
|
|
random->chunk_len);
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
|
|
|
|
random->pos += random->chunk_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
}
|
2020-11-25 15:37:20 +01:00
|
|
|
|
|
|
|
/* Random generator that is told how many bytes to return. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static int f_rng_bytes_left(void *state, unsigned char *buf, size_t len)
|
2020-11-25 15:37:20 +01:00
|
|
|
{
|
|
|
|
size_t *bytes_left = state;
|
|
|
|
size_t i;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (*bytes_left == 0) {
|
|
|
|
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
|
|
|
}
|
2020-11-25 15:37:20 +01:00
|
|
|
buf[i] = *bytes_left & 0xff;
|
2023-01-11 14:50:10 +01:00
|
|
|
--(*bytes_left);
|
2020-11-25 15:37:20 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-11-25 15:37:20 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_BIGNUM_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2015-04-29 17:02:01 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_null()
|
2015-04-29 17:02:01 +02:00
|
|
|
{
|
2015-04-30 10:28:51 +02:00
|
|
|
mbedtls_mpi X, Y, Z;
|
2015-04-29 17:02:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
|
|
|
mbedtls_mpi_init(&Y);
|
|
|
|
mbedtls_mpi_init(&Z);
|
2015-04-29 17:02:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_get_bit(&X, 42) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_lsb(&X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_bitlen(&X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_size(&X) == 0);
|
2015-04-29 17:02:01 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2015-04-29 17:02:01 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_read_write_string(int radix_X, char *input_X, int radix_A,
|
|
|
|
char *input_A, int output_size, int result_read,
|
|
|
|
int result_write)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-06-28 23:50:27 +02:00
|
|
|
char str[1000];
|
2015-06-02 16:41:48 +02:00
|
|
|
size_t len;
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(str, '!', sizeof(str));
|
2019-03-06 13:29:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_read_string(&X, radix_X, input_X) == result_read);
|
|
|
|
if (result_read == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_write_string(&X, radix_A, str, output_size, &len) == result_write);
|
|
|
|
if (result_write == 0) {
|
|
|
|
TEST_ASSERT(strcasecmp(str, input_A) == 0);
|
|
|
|
TEST_ASSERT(str[len] == '!');
|
2009-07-12 13:01:32 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_read_binary(data_t *buf, char *input_A)
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2019-02-25 17:11:58 +01:00
|
|
|
char str[1000];
|
2015-06-02 16:41:48 +02:00
|
|
|
size_t len;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_read_binary(&X, buf->x, buf->len) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_write_string(&X, 16, str, sizeof(str), &len) == 0);
|
|
|
|
TEST_ASSERT(strcmp((char *) str, input_A) == 0);
|
2019-02-13 11:28:28 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2019-02-13 11:28:28 +01:00
|
|
|
}
|
2022-07-21 10:29:32 +02:00
|
|
|
/* END_CASE */
|
|
|
|
|
2019-02-13 11:28:28 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_read_binary_le(data_t *buf, char *input_A)
|
2019-02-13 11:28:28 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
2019-02-25 17:11:58 +01:00
|
|
|
char str[1000];
|
2019-02-13 11:28:28 +01:00
|
|
|
size_t len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2019-02-13 11:28:28 +01:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_read_binary_le(&X, buf->x, buf->len) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_write_string(&X, 16, str, sizeof(str), &len) == 0);
|
|
|
|
TEST_ASSERT(strcmp((char *) str, input_A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_write_binary(char *input_X, data_t *input_A,
|
|
|
|
int output_size, int result)
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-07-06 08:40:23 +02:00
|
|
|
unsigned char buf[1000];
|
2011-04-24 17:53:29 +02:00
|
|
|
size_t buflen;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0x00, 1000);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
2015-10-30 09:23:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
buflen = mbedtls_mpi_size(&X);
|
|
|
|
if (buflen > (size_t) output_size) {
|
2013-08-20 11:48:36 +02:00
|
|
|
buflen = (size_t) output_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_write_binary(&X, buf, buflen) == result);
|
|
|
|
if (result == 0) {
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_hexcmp(buf, input_A->x,
|
|
|
|
buflen, input_A->len) == 0);
|
2009-07-12 13:01:32 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2019-02-19 17:17:40 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_write_binary_le(char *input_X, data_t *input_A,
|
|
|
|
int output_size, int result)
|
2019-02-19 17:17:40 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
|
|
|
unsigned char buf[1000];
|
|
|
|
size_t buflen;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0x00, 1000);
|
2019-02-19 17:17:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2019-02-19 17:17:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
2019-02-19 17:17:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
buflen = mbedtls_mpi_size(&X);
|
|
|
|
if (buflen > (size_t) output_size) {
|
2019-02-19 17:17:40 +01:00
|
|
|
buflen = (size_t) output_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-02-19 17:17:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_write_binary_le(&X, buf, buflen) == result);
|
|
|
|
if (result == 0) {
|
2019-02-19 17:17:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_hexcmp(buf, input_A->x,
|
|
|
|
buflen, input_A->len) == 0);
|
2019-02-19 17:17:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2019-02-19 17:17:40 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_read_file(char *input_file, data_t *input_A, int result)
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-07-06 08:40:23 +02:00
|
|
|
unsigned char buf[1000];
|
2011-04-24 17:53:29 +02:00
|
|
|
size_t buflen;
|
2009-07-11 21:15:20 +02:00
|
|
|
FILE *file;
|
2015-02-14 17:01:34 +01:00
|
|
|
int ret;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0x00, 1000);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
file = fopen(input_file, "r");
|
|
|
|
TEST_ASSERT(file != NULL);
|
|
|
|
ret = mbedtls_mpi_read_file(&X, 16, file);
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == result);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (result == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
buflen = mbedtls_mpi_size(&X);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_write_binary(&X, buf, buflen) == 0);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_hexcmp(buf, input_A->x,
|
|
|
|
buflen, input_A->len) == 0);
|
2009-07-12 13:01:32 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_write_file(char *input_X, char *output_file)
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
2009-07-11 21:15:20 +02:00
|
|
|
FILE *file_out, *file_in;
|
2015-06-24 01:08:09 +02:00
|
|
|
int ret;
|
2009-07-11 21:15:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
file_out = fopen(output_file, "w");
|
|
|
|
TEST_ASSERT(file_out != NULL);
|
|
|
|
ret = mbedtls_mpi_write_file(NULL, &X, 16, file_out);
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file_out);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
file_in = fopen(output_file, "r");
|
|
|
|
TEST_ASSERT(file_in != NULL);
|
|
|
|
ret = mbedtls_mpi_read_file(&Y, 16, file_in);
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file_in);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &Y) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_get_bit(char *input_X, int pos, int val)
|
2011-05-18 17:47:11 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_get_bit(&X, pos) == val);
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2011-05-18 17:47:11 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_set_bit(char *input_X, int pos, int val,
|
|
|
|
char *output_Y, int result)
|
2011-05-18 17:47:11 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, output_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_set_bit(&X, pos, val) == result);
|
2016-07-14 13:47:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (result == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &Y) == 0);
|
2016-07-14 13:47:07 +02:00
|
|
|
}
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
|
2011-05-18 17:47:11 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_lsb(char *input_X, int nr_bits)
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_lsb(&X) == (size_t) nr_bits);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_bitlen(char *input_X, int nr_bits)
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_bitlen(&X) == (size_t) nr_bits);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_gcd(char *input_X, char *input_Y,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi A, X, Y, Z;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A); mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_gcd(&Z, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&A); mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_cmp_int(int input_X, int input_A, int result_CMP)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_lset(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_int(&X, input_A) == result_CMP);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_cmp_mpi(char *input_X, char *input_Y,
|
|
|
|
int input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &Y) == input_A);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2019-09-11 17:07:14 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_lt_mpi_ct(int size_X, char *input_X,
|
|
|
|
int size_Y, char *input_Y,
|
|
|
|
int input_ret, int input_err)
|
2019-09-11 17:07:14 +02:00
|
|
|
{
|
2020-09-02 15:18:07 +02:00
|
|
|
unsigned ret = -1;
|
2019-10-11 15:21:53 +02:00
|
|
|
unsigned input_uret = input_ret;
|
2019-09-11 17:07:14 +02:00
|
|
|
mbedtls_mpi X, Y;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
|
2019-09-11 17:07:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
2019-09-11 17:07:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_grow(&X, size_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_grow(&Y, size_Y) == 0);
|
2019-09-11 17:07:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_lt_mpi_ct(&X, &Y, &ret) == input_err);
|
|
|
|
if (input_err == 0) {
|
|
|
|
TEST_ASSERT(ret == input_uret);
|
|
|
|
}
|
2019-09-11 17:07:14 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
|
2019-09-11 17:07:14 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_cmp_abs(char *input_X, char *input_Y,
|
|
|
|
int input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_abs(&X, &Y) == input_A);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_copy(char *src_hex, char *dst_hex)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2021-06-10 23:00:33 +02:00
|
|
|
mbedtls_mpi src, dst, ref;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&src);
|
|
|
|
mbedtls_mpi_init(&dst);
|
|
|
|
mbedtls_mpi_init(&ref);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&src, src_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&ref, dst_hex) == 0);
|
2020-01-20 21:01:51 +01:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_copy() */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&dst, dst_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_copy(&dst, &src) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&dst));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&dst, &src) == 0);
|
2020-01-20 21:01:51 +01:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_safe_cond_assign(), assignment done */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&dst);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&dst, dst_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_safe_cond_assign(&dst, &src, 1) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&dst));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&dst, &src) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_safe_cond_assign(), assignment not done */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&dst);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&dst, dst_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_safe_cond_assign(&dst, &src, 0) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&dst));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&dst, &ref) == 0);
|
2021-06-10 23:00:33 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&src);
|
|
|
|
mbedtls_mpi_free(&dst);
|
|
|
|
mbedtls_mpi_free(&ref);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_copy_self(char *input_X)
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
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>
2021-06-10 15:17:30 +02:00
|
|
|
mbedtls_mpi X, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A);
|
|
|
|
mbedtls_mpi_init(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_copy(&X, &X) == 0);
|
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>
2021-06-10 15:17:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_X) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&A);
|
|
|
|
mbedtls_mpi_free(&X);
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2021-06-10 22:29:57 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_swap(char *X_hex, char *Y_hex)
|
2021-06-10 22:29:57 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi X, Y, X0, Y0;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
|
|
|
|
mbedtls_mpi_init(&X0); mbedtls_mpi_init(&Y0);
|
2021-06-10 22:29:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X0, X_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y0, Y_hex) == 0);
|
2021-06-10 22:29:57 +02:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_swap() */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, X_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, Y_hex) == 0);
|
|
|
|
mbedtls_mpi_swap(&X, &Y);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(sign_is_valid(&Y));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &Y0) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Y, &X0) == 0);
|
2021-06-10 22:29:57 +02:00
|
|
|
|
2021-06-10 23:00:33 +02:00
|
|
|
/* mbedtls_mpi_safe_cond_swap(), swap done */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
|
|
|
mbedtls_mpi_free(&Y);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, X_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, Y_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_safe_cond_swap(&X, &Y, 1) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(sign_is_valid(&Y));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &Y0) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Y, &X0) == 0);
|
2021-06-10 23:00:33 +02:00
|
|
|
|
|
|
|
/* mbedtls_mpi_safe_cond_swap(), swap not done */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
|
|
|
mbedtls_mpi_free(&Y);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, X_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, Y_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_safe_cond_swap(&X, &Y, 0) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(sign_is_valid(&Y));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &X0) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Y, &Y0) == 0);
|
2021-06-10 23:00:33 +02:00
|
|
|
|
2021-06-10 22:29:57 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
|
|
|
|
mbedtls_mpi_free(&X0); mbedtls_mpi_free(&Y0);
|
2021-06-10 22:29:57 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_swap_self(char *X_hex)
|
2021-06-10 22:29:57 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi X, X0;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&X0);
|
2021-06-10 22:29:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, X_hex) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X0, X_hex) == 0);
|
2021-06-10 22:29:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_swap(&X, &X);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &X0) == 0);
|
2021-06-10 22:29:57 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&X0);
|
2021-06-10 22:29:57 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-11-21 10:39:37 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_shrink(int before, int used, int min, int after)
|
2013-11-21 10:39:37 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_grow(&X, before) == 0);
|
|
|
|
if (used > 0) {
|
|
|
|
size_t used_bit_count = used * 8 * sizeof(mbedtls_mpi_uint);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_set_bit(&X, used_bit_count - 1, 1) == 0);
|
2021-06-15 21:19:18 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(X.n, (size_t) before);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shrink(&X, min) == 0);
|
|
|
|
TEST_EQUAL(X.n, (size_t) after);
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2013-11-21 10:39:37 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_add_mpi(char *input_X, char *input_Y,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_add_mpi(&Z, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2020-07-23 01:18:11 +02:00
|
|
|
/* result == first operand */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_add_mpi(&X, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
2020-07-23 01:18:11 +02:00
|
|
|
|
|
|
|
/* result == second operand */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_add_mpi(&Y, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Y));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Y, &A) == 0);
|
2020-07-23 01:18:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-10-25 10:58:03 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_add_mpi_inplace(char *input_X, char *input_A)
|
2015-10-25 10:58:03 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi X, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&A);
|
2015-10-25 10:58:03 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
2015-10-25 12:29:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_sub_abs(&X, &X, &X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_int(&X, 0) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
2015-10-25 12:29:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_add_abs(&X, &X, &X) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
2015-10-25 12:29:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_add_mpi(&X, &X, &X) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
2015-10-25 10:58:03 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&A);
|
2015-10-25 10:58:03 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_add_abs(char *input_X, char *input_Y,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_add_abs(&Z, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2020-07-23 01:18:11 +02:00
|
|
|
/* result == first operand */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_add_abs(&X, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
2020-07-23 01:18:11 +02:00
|
|
|
|
|
|
|
/* result == second operand */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_add_abs(&Y, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Y));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Y, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-07-12 13:01:32 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-07-12 13:01:32 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_add_int(char *input_X, int input_Y,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Z, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_add_int(&Z, &X, input_Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_sub_mpi(char *input_X, char *input_Y,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_sub_mpi(&Z, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2020-07-23 01:18:11 +02:00
|
|
|
/* result == first operand */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_sub_mpi(&X, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
2020-07-23 01:18:11 +02:00
|
|
|
|
|
|
|
/* result == second operand */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_sub_mpi(&Y, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Y));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Y, &A) == 0);
|
2020-07-23 01:18:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_sub_abs(char *input_X, char *input_Y,
|
|
|
|
char *input_A, int sub_result)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
2015-10-30 09:23:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_mpi_sub_abs(&Z, &X, &Y);
|
|
|
|
TEST_ASSERT(res == sub_result);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2020-07-23 01:18:11 +02:00
|
|
|
/* result == first operand */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_sub_abs(&X, &X, &Y) == sub_result);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
if (sub_result == 0) {
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
|
|
|
}
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
2020-07-23 01:18:11 +02:00
|
|
|
|
|
|
|
/* result == second operand */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_sub_abs(&Y, &X, &Y) == sub_result);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Y));
|
|
|
|
if (sub_result == 0) {
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Y, &A) == 0);
|
|
|
|
}
|
2020-07-23 01:18:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_sub_int(char *input_X, int input_Y,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Z, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_sub_int(&Z, &X, input_Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mul_mpi(char *input_X, char *input_Y,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_mul_mpi(&Z, &X, &Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mul_int(char *input_X, int input_Y,
|
|
|
|
char *input_A, char *result_comparison)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Z, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_mul_int(&Z, &X, input_Y) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
if (strcmp(result_comparison, "==") == 0) {
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
|
|
|
} else if (strcmp(result_comparison, "!=") == 0) {
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) != 0);
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT("unknown operator" == 0);
|
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_div_mpi(char *input_X, char *input_Y,
|
|
|
|
char *input_A, char *input_B,
|
|
|
|
int div_result)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Q, R, A, B;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Q); mbedtls_mpi_init(&R);
|
|
|
|
mbedtls_mpi_init(&A); mbedtls_mpi_init(&B);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&B, input_B) == 0);
|
|
|
|
res = mbedtls_mpi_div_mpi(&Q, &R, &X, &Y);
|
|
|
|
TEST_ASSERT(res == div_result);
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&Q));
|
|
|
|
TEST_ASSERT(sign_is_valid(&R));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Q, &A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &B) == 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Q); mbedtls_mpi_free(&R);
|
|
|
|
mbedtls_mpi_free(&A); mbedtls_mpi_free(&B);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_div_int(char *input_X, int input_Y,
|
|
|
|
char *input_A, char *input_B,
|
|
|
|
int div_result)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Q, R, A, B;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Q); mbedtls_mpi_init(&R); mbedtls_mpi_init(&A);
|
|
|
|
mbedtls_mpi_init(&B);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&B, input_B) == 0);
|
|
|
|
res = mbedtls_mpi_div_int(&Q, &R, &X, input_Y);
|
|
|
|
TEST_ASSERT(res == div_result);
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&Q));
|
|
|
|
TEST_ASSERT(sign_is_valid(&R));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Q, &A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &B) == 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Q); mbedtls_mpi_free(&R); mbedtls_mpi_free(&A);
|
|
|
|
mbedtls_mpi_free(&B);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2022-10-19 13:46:35 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_mpi(char *input_X, char *input_Y,
|
|
|
|
char *input_A, int div_result)
|
2022-10-19 13:46:35 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi X, Y, A;
|
|
|
|
int res;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&A);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
res = mbedtls_mpi_mod_mpi(&X, &X, &Y);
|
|
|
|
TEST_ASSERT(res == div_result);
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
2022-10-19 13:46:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&A);
|
2022-10-19 13:46:35 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_int(char *input_X, char *input_Y,
|
|
|
|
char *input_A, int mod_result)
|
2022-10-19 13:46:35 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
2022-11-09 12:45:29 +01:00
|
|
|
mbedtls_mpi Y;
|
|
|
|
mbedtls_mpi A;
|
2022-10-19 13:46:35 +02:00
|
|
|
int res;
|
|
|
|
mbedtls_mpi_uint r;
|
2022-11-09 12:45:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
|
|
|
mbedtls_mpi_init(&Y);
|
|
|
|
mbedtls_mpi_init(&A);
|
2022-10-19 13:46:35 +02:00
|
|
|
|
2022-11-09 12:45:29 +01:00
|
|
|
/* We use MPIs to read Y and A since the test framework limits us to
|
|
|
|
* ints, so we can't have 64-bit values */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&X, input_X), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&Y, input_Y), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&A, input_A), 0);
|
2022-11-09 12:45:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(Y.n, 1);
|
|
|
|
TEST_EQUAL(A.n, 1);
|
2022-11-09 12:45:29 +01:00
|
|
|
|
2022-11-10 13:05:55 +01:00
|
|
|
/* Convert the MPIs for Y and A to (signed) mbedtls_mpi_sints */
|
|
|
|
|
|
|
|
/* Since we're converting sign+magnitude to two's complement, we lose one
|
|
|
|
* bit of value in the output. This means there are some values we can't
|
|
|
|
* represent, e.g. (hex) -A0000000 on 32-bit systems. These are technically
|
|
|
|
* invalid test cases, so could be considered "won't happen", but they are
|
|
|
|
* easy to test for, and this helps guard against human error. */
|
|
|
|
|
|
|
|
mbedtls_mpi_sint y = (mbedtls_mpi_sint) Y.p[0];
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(y >= 0); /* If y < 0 here, we can't make negative y */
|
|
|
|
if (Y.s == -1) {
|
2022-11-09 12:45:29 +01:00
|
|
|
y = -y;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-10 13:05:55 +01:00
|
|
|
|
|
|
|
mbedtls_mpi_sint a = (mbedtls_mpi_sint) A.p[0];
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(a >= 0); /* Same goes for a */
|
|
|
|
if (A.s == -1) {
|
2022-11-09 12:45:29 +01:00
|
|
|
a = -a;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-09 12:45:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_mpi_mod_int(&r, &X, y);
|
|
|
|
TEST_EQUAL(res, mod_result);
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_EQUAL(r, a);
|
2022-10-19 13:46:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
|
|
|
mbedtls_mpi_free(&Y);
|
|
|
|
mbedtls_mpi_free(&A);
|
2022-10-19 13:46:35 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_exp_mod(char *input_A, char *input_E,
|
|
|
|
char *input_N, char *input_X,
|
|
|
|
int exp_result)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi A, E, N, RR, Z, X;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N);
|
|
|
|
mbedtls_mpi_init(&RR); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&X);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
|
|
|
|
res = mbedtls_mpi_exp_mod(&Z, &A, &E, &N, NULL);
|
|
|
|
TEST_ASSERT(res == exp_result);
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &X) == 0);
|
2021-06-09 18:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now test again with the speed-up parameter supplied as an output. */
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_mpi_exp_mod(&Z, &A, &E, &N, &RR);
|
|
|
|
TEST_ASSERT(res == exp_result);
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &X) == 0);
|
2021-06-09 18:31:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now test again with the speed-up parameter supplied in calculated form. */
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_mpi_exp_mod(&Z, &A, &E, &N, &RR);
|
|
|
|
TEST_ASSERT(res == exp_result);
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &X) == 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N);
|
|
|
|
mbedtls_mpi_free(&RR); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&X);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2020-12-02 11:41:50 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_exp_mod_size(int A_bytes, int E_bytes, int N_bytes,
|
|
|
|
char *input_RR, int exp_result)
|
2020-12-02 11:41:50 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi A, E, N, RR, Z;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A); mbedtls_mpi_init(&E); mbedtls_mpi_init(&N);
|
|
|
|
mbedtls_mpi_init(&RR); mbedtls_mpi_init(&Z);
|
2020-12-02 11:41:50 +01:00
|
|
|
|
2020-12-03 12:35:41 +01:00
|
|
|
/* Set A to 2^(A_bytes - 1) + 1 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_lset(&A, 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_l(&A, (A_bytes * 8) - 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_set_bit(&A, 0, 1) == 0);
|
2020-12-03 12:35:41 +01:00
|
|
|
|
|
|
|
/* Set E to 2^(E_bytes - 1) + 1 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_lset(&E, 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_l(&E, (E_bytes * 8) - 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_set_bit(&E, 0, 1) == 0);
|
2020-12-03 12:35:41 +01:00
|
|
|
|
|
|
|
/* Set N to 2^(N_bytes - 1) + 1 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_lset(&N, 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_l(&N, (N_bytes * 8) - 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_set_bit(&N, 0, 1) == 0);
|
2020-12-02 11:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strlen(input_RR)) {
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&RR, input_RR) == 0);
|
|
|
|
}
|
2020-12-02 11:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_exp_mod(&Z, &A, &E, &N, &RR) == exp_result);
|
2020-12-02 11:41:50 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&A); mbedtls_mpi_free(&E); mbedtls_mpi_free(&N);
|
|
|
|
mbedtls_mpi_free(&RR); mbedtls_mpi_free(&Z);
|
2020-12-02 11:41:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_inv_mod(char *input_X, char *input_Y,
|
|
|
|
char *input_A, int div_result)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, Z, A;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z); mbedtls_mpi_init(&A);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&Y, input_Y) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
res = mbedtls_mpi_inv_mod(&Z, &X, &Y);
|
|
|
|
TEST_ASSERT(res == div_result);
|
|
|
|
if (res == 0) {
|
|
|
|
TEST_ASSERT(sign_is_valid(&Z));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Z, &A) == 0);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_is_prime(char *input_X, int div_result)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
res = mbedtls_mpi_is_prime_ext(&X, 40, mbedtls_test_rnd_std_rand, NULL);
|
|
|
|
TEST_ASSERT(res == div_result);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_is_prime_det(data_t *input_X, data_t *witnesses,
|
|
|
|
int chunk_len, int rounds)
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
|
|
|
int res;
|
|
|
|
mbedtls_test_mpi_random rand;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
rand.data = witnesses;
|
|
|
|
rand.pos = 0;
|
|
|
|
rand.chunk_len = chunk_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_read_binary(&X, input_X->x, input_X->len) == 0);
|
|
|
|
res = mbedtls_mpi_is_prime_ext(&X, rounds - 1,
|
|
|
|
mbedtls_test_mpi_miller_rabin_determinizer,
|
|
|
|
&rand);
|
|
|
|
TEST_ASSERT(res == 0);
|
2018-10-02 16:30:39 +02:00
|
|
|
|
|
|
|
rand.data = witnesses;
|
|
|
|
rand.pos = 0;
|
|
|
|
rand.chunk_len = chunk_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
res = mbedtls_mpi_is_prime_ext(&X, rounds,
|
|
|
|
mbedtls_test_mpi_miller_rabin_determinizer,
|
|
|
|
&rand);
|
|
|
|
TEST_ASSERT(res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
Bignum: Add tests for primality testing
Primality tests have to deal with different distribution when generating
primes and when validating primes.
These new tests are testing if mbedtls_mpi_is_prime() is working
properly in the latter setting.
The new tests involve pseudoprimes with maximum number of
non-witnesses. The non-witnesses were generated by printing them
from mpi_miller_rabin(). The pseudoprimes were generated by the
following function:
void gen_monier( mbedtls_mpi* res, int nbits )
{
mbedtls_mpi p_2x_plus_1, p_4x_plus_1, x, tmp;
mbedtls_mpi_init( &p_2x_plus_1 );
mbedtls_mpi_init( &p_4x_plus_1 );
mbedtls_mpi_init( &x ); mbedtls_mpi_init( &tmp );
do
{
mbedtls_mpi_gen_prime( &p_2x_plus_1, nbits >> 1, 0,
rnd_std_rand, NULL );
mbedtls_mpi_sub_int( &x, &p_2x_plus_1, 1 );
mbedtls_mpi_div_int( &x, &tmp, &x, 2 );
if( mbedtls_mpi_get_bit( &x, 0 ) == 0 )
continue;
mbedtls_mpi_mul_int( &p_4x_plus_1, &x, 4 );
mbedtls_mpi_add_int( &p_4x_plus_1, &p_4x_plus_1, 1 );
if( mbedtls_mpi_is_prime( &p_4x_plus_1, rnd_std_rand,
NULL ) == 0 )
break;
} while( 1 );
mbedtls_mpi_mul_mpi( res, &p_2x_plus_1, &p_4x_plus_1 );
}
2018-09-05 18:04:49 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_gen_prime(int bits, int flags, int ref_ret)
|
2014-06-16 17:12:40 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
2014-06-16 17:12:40 +02:00
|
|
|
int my_ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2014-06-16 17:12:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
my_ret = mbedtls_mpi_gen_prime(&X, bits, flags,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL);
|
|
|
|
TEST_ASSERT(my_ret == ref_ret);
|
2014-06-16 17:12:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ref_ret == 0) {
|
|
|
|
size_t actual_bits = mbedtls_mpi_bitlen(&X);
|
2014-06-16 17:12:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(actual_bits >= (size_t) bits);
|
|
|
|
TEST_ASSERT(actual_bits <= (size_t) bits + 1);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
2014-06-16 17:12:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_is_prime_ext(&X, 40,
|
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL) == 0);
|
|
|
|
if (flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH) {
|
2018-01-10 08:12:01 +01:00
|
|
|
/* X = ( X - 1 ) / 2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_r(&X, 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_is_prime_ext(&X, 40,
|
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL) == 0);
|
2014-06-16 17:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2014-06-16 17:12:40 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_shift_l(char *input_X, int shift_X,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_l(&X, shift_X) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_shift_r(char *input_X, int shift_X,
|
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, A;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X); mbedtls_mpi_init(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&X, input_X) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&A, input_A) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_r(&X, shift_X) == 0);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&X, &A) == 0);
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X); mbedtls_mpi_free(&A);
|
2009-06-28 23:50:27 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2020-11-25 15:37:20 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_fill_random(int wanted_bytes, int rng_bytes,
|
|
|
|
int before, int expected_ret)
|
2020-11-25 15:37:20 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
|
|
|
int ret;
|
|
|
|
size_t bytes_left = rng_bytes;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&X);
|
2020-11-25 15:37:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (before != 0) {
|
2021-04-02 00:02:27 +02:00
|
|
|
/* Set X to sign(before) * 2^(|before|-1) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_lset(&X, before > 0 ? 1 : -1) == 0);
|
|
|
|
if (before < 0) {
|
|
|
|
before = -before;
|
|
|
|
}
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_l(&X, before - 1) == 0);
|
2021-04-02 00:02:27 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mpi_fill_random(&X, wanted_bytes,
|
|
|
|
f_rng_bytes_left, &bytes_left);
|
|
|
|
TEST_ASSERT(ret == expected_ret);
|
2020-11-25 15:37:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_ret == 0) {
|
2020-11-25 15:37:20 +01:00
|
|
|
/* mbedtls_mpi_fill_random is documented to use bytes from the RNG
|
|
|
|
* as a big-endian representation of the number. We know when
|
|
|
|
* our RNG function returns null bytes, so we know how many
|
|
|
|
* leading zero bytes the number has. */
|
|
|
|
size_t leading_zeros = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (wanted_bytes > 0 && rng_bytes % 256 == 0) {
|
2020-11-25 15:37:20 +01:00
|
|
|
leading_zeros = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
TEST_ASSERT(mbedtls_mpi_size(&X) + leading_zeros ==
|
|
|
|
(size_t) wanted_bytes);
|
|
|
|
TEST_ASSERT((int) bytes_left == rng_bytes - wanted_bytes);
|
|
|
|
TEST_ASSERT(sign_is_valid(&X));
|
2020-11-25 15:37:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
2020-11-25 15:37:20 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-11-15 23:02:14 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void most_negative_mpi_sint()
|
2022-11-15 23:02:14 +01:00
|
|
|
{
|
|
|
|
/* Ad hoc tests for n = -p = -2^(biL-1) as a mbedtls_mpi_sint. We
|
|
|
|
* guarantee that mbedtls_mpi_sint is a two's complement type, so this
|
|
|
|
* is a valid value. However, negating it (`-n`) has undefined behavior
|
|
|
|
* (although in practice `-n` evaluates to the value n).
|
|
|
|
*
|
|
|
|
* This function has ad hoc tests for this value. It's separated from other
|
|
|
|
* functions because the test framework makes it hard to pass this value
|
|
|
|
* into test cases.
|
|
|
|
*
|
|
|
|
* In the comments here:
|
|
|
|
* - biL = number of bits in limbs
|
|
|
|
* - p = 2^(biL-1) (smallest positive value not in mbedtls_mpi_sint range)
|
|
|
|
* - n = -2^(biL-1) (largest negative value in mbedtls_mpi_sint range)
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_mpi A, R, X;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A);
|
|
|
|
mbedtls_mpi_init(&R);
|
|
|
|
mbedtls_mpi_init(&X);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint most_positive_plus_1 = (mbedtls_mpi_uint) 1 << (biL - 1);
|
2022-11-15 23:02:14 +01:00
|
|
|
const mbedtls_mpi_sint most_positive = most_positive_plus_1 - 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_mpi_sint most_negative = -most_positive - 1;
|
|
|
|
TEST_EQUAL((mbedtls_mpi_uint) most_negative,
|
|
|
|
(mbedtls_mpi_uint) 1 << (biL - 1));
|
|
|
|
TEST_EQUAL((mbedtls_mpi_uint) most_negative << 1, 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_lset() */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, most_negative), 0);
|
|
|
|
TEST_EQUAL(A.s, -1);
|
|
|
|
TEST_EQUAL(A.n, 1);
|
|
|
|
TEST_EQUAL(A.p[0], most_positive_plus_1);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_cmp_int(): -p == -p */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&A, most_negative), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_cmp_int(): -(p+1) < -p */
|
|
|
|
A.p[0] = most_positive_plus_1 + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&A, most_negative), -1);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_cmp_int(): -(p-1) > -p */
|
|
|
|
A.p[0] = most_positive_plus_1 - 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&A, most_negative), 1);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_add_int(): (p-1) + (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, most_positive), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_add_int(&X, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, -1), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_add_int(): (0) + (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, 0), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_add_int(&X, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, most_negative), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_add_int(): (-p) + (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_add_int(&X, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(X.s, -1);
|
|
|
|
TEST_EQUAL(X.n, 2);
|
|
|
|
TEST_EQUAL(X.p[0], 0);
|
|
|
|
TEST_EQUAL(X.p[1], 1);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_sub_int(): (p) - (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&X);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, most_positive), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_sub_int(&X, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(X.s, 1);
|
|
|
|
TEST_EQUAL(X.n, 1);
|
|
|
|
TEST_EQUAL(X.p[0], ~(mbedtls_mpi_uint) 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_sub_int(): (0) - (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, 0), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_sub_int(&X, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(X.s, 1);
|
|
|
|
TEST_EQUAL(X.n, 1);
|
|
|
|
TEST_EQUAL(X.p[0], most_positive_plus_1);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_sub_int(): (-p) - (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_sub_int(&X, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, 0), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_div_int(): (-p+1) / (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, -most_positive), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_div_int(&X, &R, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, 0), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&R, -most_positive), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_div_int(): (-p) / (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_div_int(&X, &R, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, 1), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&R, 0), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_div_int(): (-2*p) / (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_shift_l(&A, 1), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_div_int(&X, &R, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, 2), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&R, 0), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_div_int(): (-2*p+1) / (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_add_int(&A, &A, 1), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_div_int(&X, &R, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, 1), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&R, -most_positive), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_div_int(): (p-1) / (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&A, most_positive), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_div_int(&X, &R, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, 0), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&R, most_positive), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_div_int(): (p) / (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_add_int(&A, &A, 1), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_div_int(&X, &R, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, -1), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&R, 0), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_div_int(): (2*p) / (-p) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_shift_l(&A, 1), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_div_int(&X, &R, &A, most_negative), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&X, -2), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_int(&R, 0), 0);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_mod_int(): never valid */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_int(X.p, &A, most_negative),
|
|
|
|
MBEDTLS_ERR_MPI_NEGATIVE_VALUE);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
/* Test mbedtls_mpi_random(): never valid */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_random(&X, most_negative, &A,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL),
|
|
|
|
MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
|
2022-11-15 23:02:14 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&A);
|
|
|
|
mbedtls_mpi_free(&R);
|
|
|
|
mbedtls_mpi_free(&X);
|
2022-11-15 23:02:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_selftest()
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_self_test(1) == 0);
|
2009-07-06 08:40:23 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|