2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/bignum.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 15:24:33 +01:00
|
|
|
#if MBEDTLS_MPI_MAX_BITS > 256
|
|
|
|
#define MPI_MAX_BITS_LARGER_THAN_256
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
typedef struct mbedtls_test_mpi_random
|
|
|
|
{
|
|
|
|
uint8_t *data;
|
|
|
|
uint32_t data_len;
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
|
|
|
|
unsigned char* buf,
|
|
|
|
size_t len )
|
|
|
|
{
|
|
|
|
mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
|
|
|
|
|
|
|
|
if( random == NULL || random->data == NULL || buf == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
if( random->pos + random->chunk_len > random->data_len
|
|
|
|
|| random->chunk_len > len )
|
|
|
|
{
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( buf, 0, len );
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
memcpy( buf + len - random->chunk_len, &random->data[random->pos],
|
|
|
|
random->chunk_len );
|
|
|
|
|
|
|
|
random->pos += random->chunk_len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
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 */
|
|
|
|
void mpi_null( )
|
|
|
|
{
|
2015-04-30 10:28:51 +02:00
|
|
|
mbedtls_mpi X, Y, Z;
|
2015-04-29 17:02:01 +02:00
|
|
|
|
2015-04-30 10:28:51 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
mbedtls_mpi_init( &Y );
|
|
|
|
mbedtls_mpi_init( &Z );
|
2015-04-29 17:02:01 +02:00
|
|
|
|
2015-04-30 10:28:51 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
|
2015-06-18 16:47:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
|
2015-04-30 10:28:51 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
|
2015-04-29 17:02:01 +02:00
|
|
|
|
|
|
|
exit:
|
2015-04-30 10:28:51 +02: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 */
|
|
|
|
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
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2019-03-06 13:29:37 +01:00
|
|
|
memset( str, '!', sizeof( str ) );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result_read == 0 )
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2015-06-02 16:41:48 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result_write == 0 )
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
|
2019-03-06 13:29:37 +01:00
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_read_binary( char *input_X, int radix_A, char *input_A )
|
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 str[1000];
|
|
|
|
unsigned char buf[1000];
|
2015-06-02 16:41:48 +02:00
|
|
|
size_t len;
|
2011-04-24 17:53:29 +02:00
|
|
|
size_t input_len;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
input_len = unhexify( buf, input_X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf, input_len ) == 0 );
|
2015-06-02 16:41:48 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
|
2013-08-20 11:48:36 +02:00
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_write_binary( int radix_X, char *input_X, char *input_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
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 str[1000];
|
|
|
|
unsigned char buf[1000];
|
2011-04-24 17:53:29 +02:00
|
|
|
size_t buflen;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
|
|
|
memset( buf, 0x00, 1000 );
|
|
|
|
memset( str, 0x00, 1000 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2015-10-30 09:23:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
buflen = mbedtls_mpi_size( &X );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( buflen > (size_t) output_size )
|
|
|
|
buflen = (size_t) output_size;
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result == 0)
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
|
|
|
hexify( str, buf, buflen );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 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:
|
2015-04-08 12:49:31 +02: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 */
|
|
|
|
void mbedtls_mpi_read_file( int radix_X, char *input_file, char *input_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
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 str[1000];
|
|
|
|
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
|
|
|
|
|
|
|
memset( buf, 0x00, 1000 );
|
|
|
|
memset( str, 0x00, 1000 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
file = fopen( input_file, "r" );
|
2014-04-17 16:08:20 +02:00
|
|
|
TEST_ASSERT( file != NULL );
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_mpi_read_file( &X, radix_X, file );
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file);
|
2015-02-14 17:01:34 +01:00
|
|
|
TEST_ASSERT( ret == result );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( result == 0 )
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
buflen = mbedtls_mpi_size( &X );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2009-07-12 13:01:32 +02:00
|
|
|
hexify( str, buf, buflen );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( strcasecmp( (char *) str, input_A ) == 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:
|
2015-04-08 12:49:31 +02: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 */
|
|
|
|
void mbedtls_mpi_write_file( int radix_X, char *input_X, int output_radix,
|
2013-08-20 11:48:36 +02:00
|
|
|
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
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
file_out = fopen( output_file, "w" );
|
2011-05-26 15:16:06 +02:00
|
|
|
TEST_ASSERT( file_out != NULL );
|
2015-06-24 01:08:09 +02:00
|
|
|
ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file_out);
|
2015-06-24 01:08:09 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
file_in = fopen( output_file, "r" );
|
2011-05-26 15:16:06 +02:00
|
|
|
TEST_ASSERT( file_in != NULL );
|
2015-06-24 01:08:09 +02:00
|
|
|
ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
|
2009-07-06 08:40:23 +02:00
|
|
|
fclose(file_in);
|
2015-06-24 01:08:09 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_get_bit( int radix_X, 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;
|
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_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:
|
2015-04-08 12:49:31 +02: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 */
|
2016-07-14 13:47:07 +02:00
|
|
|
void mbedtls_mpi_set_bit( int radix_X, char *input_X, int pos, int val,
|
|
|
|
int radix_Y, 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;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, output_Y ) == 0 );
|
2016-07-14 13:47:07 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
|
|
|
|
|
|
|
|
if( result == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
|
|
|
|
}
|
2011-05-18 17:47:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_lsb( int radix_X, 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;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-06-18 16:47:17 +02:00
|
|
|
void mbedtls_mpi_bitlen( int radix_X, 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;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2015-06-18 16:47:17 +02:00
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_gcd( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, 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;
|
|
|
|
mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_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;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02: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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_cmp_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_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:
|
2015-04-08 12:49:31 +02: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 */
|
2019-10-14 10:21:49 +02:00
|
|
|
void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
|
|
|
|
int size_Y, char * input_Y,
|
2019-10-11 15:21:53 +02:00
|
|
|
int input_ret, int input_err )
|
2019-09-11 17:07:14 +02:00
|
|
|
{
|
2019-10-11 15:21:53 +02:00
|
|
|
unsigned ret;
|
|
|
|
unsigned input_uret = input_ret;
|
2019-09-11 17:07:14 +02:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
|
|
|
|
2019-10-14 10:21:49 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
|
2019-09-11 17:07:14 +02:00
|
|
|
|
2020-01-21 16:30:53 +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
|
|
|
|
2019-10-11 15:21:53 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
|
2019-09-11 17:07:14 +02:00
|
|
|
if( input_err == 0 )
|
2019-10-11 15:21:53 +02:00
|
|
|
TEST_ASSERT( ret == input_uret );
|
2019-09-11 17:07:14 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_cmp_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_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:
|
2015-04-08 12:49:31 +02: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 */
|
2020-01-20 21:01:51 +01:00
|
|
|
void mbedtls_mpi_copy_sint( int input_X, int input_Y )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2020-01-20 21:01:51 +01:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
|
2020-01-20 21:01:51 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
|
2020-01-20 21:01:51 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_mpi_copy_binary( char *input_X, char *input_Y )
|
|
|
|
{
|
|
|
|
mbedtls_mpi X, Y, X0;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &X0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_copy( &Y, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2020-01-20 21:01:51 +01:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &X0 );
|
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 */
|
|
|
|
void mpi_copy_self( int input_X )
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-07-06 08:40:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02: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-11-21 10:39:37 +01:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_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;
|
|
|
|
mbedtls_mpi_init( &X );
|
2013-11-21 10:39:37 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
|
2013-11-21 10:39:37 +01:00
|
|
|
TEST_ASSERT( used <= before );
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( X.p, 0x2a, used * sizeof( mbedtls_mpi_uint ) );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
|
2013-11-21 10:39:37 +01:00
|
|
|
TEST_ASSERT( X.n == (size_t) after );
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X );
|
2013-11-21 10:39:37 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-11-21 16:56:39 +01:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_safe_cond_assign( int x_sign, char *x_str,
|
2013-11-21 21:12:26 +01:00
|
|
|
int y_sign, char *y_str )
|
2013-11-21 16:56:39 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, XX;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &XX );
|
2013-11-21 16:56:39 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
|
2013-11-21 21:12:26 +01:00
|
|
|
X.s = x_sign;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
|
2013-11-21 21:12:26 +01:00
|
|
|
Y.s = y_sign;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
|
2013-11-21 16:56:39 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
|
2013-11-21 16:56:39 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &X, &Y, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
|
2013-11-21 16:56:39 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &XX );
|
2013-11-21 16:56:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-12-04 21:41:50 +01:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_safe_cond_swap( int x_sign, char *x_str,
|
2013-12-04 21:41:50 +01:00
|
|
|
int y_sign, char *y_str )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, XX, YY;
|
2013-12-04 21:41:50 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
|
|
|
mbedtls_mpi_init( &XX ); mbedtls_mpi_init( &YY );
|
2013-12-04 21:41:50 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x_str ) == 0 );
|
2013-12-04 21:41:50 +01:00
|
|
|
X.s = x_sign;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y_str ) == 0 );
|
2013-12-04 21:41:50 +01:00
|
|
|
Y.s = y_sign;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_copy( &XX, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_copy( &YY, &Y ) == 0 );
|
2013-12-04 21:41:50 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &XX ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &YY ) == 0 );
|
2013-12-04 21:41:50 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &XX ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &YY ) == 0 );
|
2013-12-04 21:41:50 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
|
|
|
mbedtls_mpi_free( &XX ); mbedtls_mpi_free( &YY );
|
2013-12-04 21:41:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2020-01-20 21:01:51 +01:00
|
|
|
void mbedtls_mpi_swap_sint( int input_X, int input_Y )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2020-01-20 21:01:51 +01:00
|
|
|
mbedtls_mpi X, Y;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &Y, input_Y ) == 0 );
|
2020-01-20 21:01:51 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_Y ) == 0 );
|
|
|
|
|
|
|
|
mbedtls_mpi_swap( &X, &Y );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, input_X ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mbedtls_mpi_swap_binary( char *input_X, char *input_Y )
|
|
|
|
{
|
|
|
|
mbedtls_mpi X, Y, X0, Y0;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
|
|
|
mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y0, 16, input_Y ) == 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_swap( &X, &Y );
|
2020-01-20 21:01:51 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
|
|
|
mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_swap_self( char *input_X )
|
|
|
|
{
|
|
|
|
mbedtls_mpi X, X0;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X0, 16, input_X ) == 0 );
|
|
|
|
|
|
|
|
mbedtls_mpi_swap( &X, &X );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2020-01-20 21:01:51 +01:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
|
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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_add_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, 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;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
|
|
|
void mbedtls_mpi_add_mpi_inplace( int radix_X, char *input_X, int radix_A, char *input_A )
|
|
|
|
{
|
|
|
|
mbedtls_mpi X, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2015-10-25 12:29:13 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_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( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2015-10-25 10:58:03 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_add_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, 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;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
|
|
|
void mpi_add_abs_add_first( int radix_X, char *input_X, int radix_Y,
|
|
|
|
char *input_Y, int radix_A, char *input_A )
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
|
2009-07-12 13:01:32 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 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 */
|
|
|
|
void mpi_add_abs_add_second( int radix_X, char *input_X, int radix_Y,
|
|
|
|
char *input_Y, int radix_A, char *input_A )
|
2009-07-12 13:01:32 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Y, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
|
2009-07-12 13:01:32 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_add_int( int radix_X, char *input_X, int input_Y, int radix_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_sub_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, 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;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_sub_abs( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, 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;
|
2015-04-08 12:49:31 +02: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
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
2015-10-30 09:23:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == sub_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_sub_int( int radix_X, char *input_X, int input_Y, int radix_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, Z, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_mul_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, 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;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_mul_int( int radix_X, char *input_X, int input_Y, int radix_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
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;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
|
2013-08-16 13:38:47 +02:00
|
|
|
if( strcmp( result_comparison, "==" ) == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
|
2013-08-16 13:38:47 +02:00
|
|
|
else if( strcmp( result_comparison, "!=" ) == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
|
2013-08-16 13:38:47 +02:00
|
|
|
else
|
|
|
|
TEST_ASSERT( "unknown operator" == 0 );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_div_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, char *input_A, int radix_B, 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;
|
2015-04-08 12:49:31 +02: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_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
|
|
|
|
res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_div_int( int radix_X, char *input_X, int input_Y, int radix_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *input_A, int radix_B, 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;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
|
|
|
|
mbedtls_mpi_init( &B );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &B, radix_B, input_B ) == 0 );
|
|
|
|
res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
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:
|
2015-04-08 12:49:31 +02: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
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_mod_mpi( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, 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, A;
|
2009-06-28 23:50:27 +02:00
|
|
|
int res;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &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:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); 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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_mod_int( int radix_X, char *input_X, int input_Y, int input_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
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;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_uint r;
|
|
|
|
mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
res = mbedtls_mpi_mod_int( &r, &X, input_Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
|
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:
|
2015-04-08 12:49:31 +02: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
|
|
|
|
2020-12-03 15:24:33 +01:00
|
|
|
/* BEGIN_CASE depends_on:MPI_MAX_BITS_LARGER_THAN_256 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_exp_mod( int radix_A, char *input_A, int radix_E, char *input_E,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_N, char *input_N, int radix_RR, char *input_RR,
|
|
|
|
int radix_X, char *input_X, int div_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;
|
2015-04-08 12:49:31 +02: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 );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( strlen( input_RR ) )
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
|
|
|
void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
|
2020-12-03 12:35:41 +01:00
|
|
|
int radix_RR, char * input_RR, int exp_result )
|
2020-12-02 11:41:50 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi A, E, N, RR, Z;
|
|
|
|
mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
|
|
|
|
mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
|
|
|
|
|
2020-12-03 12:35:41 +01:00
|
|
|
/* Set A to 2^(A_bytes - 1) + 1 */
|
2020-12-02 11:41:50 +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 */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
|
2020-12-02 11:41:50 +01:00
|
|
|
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 */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
|
2020-12-02 11:41:50 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
|
|
|
|
|
|
|
|
if( strlen( input_RR ) )
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &RR, radix_RR, input_RR ) == 0 );
|
|
|
|
|
2020-12-03 12:35:41 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
|
2020-12-02 11:41:50 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
|
|
|
|
mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_inv_mod( int radix_X, char *input_X, int radix_Y, char *input_Y,
|
2013-08-20 11:48:36 +02:00
|
|
|
int radix_A, 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;
|
2015-04-08 12:49:31 +02: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
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &Y, radix_Y, input_Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2009-06-28 23:50:27 +02:00
|
|
|
if( res == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
|
|
|
void mbedtls_mpi_is_prime( int radix_X, 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;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
res = mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL );
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( res == div_result );
|
2011-05-05 13:49:20 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02: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
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
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
|
|
|
void mbedtls_mpi_is_prime_det( char *input_X, char *witnesses,
|
|
|
|
int chunk_len, int div_result )
|
|
|
|
{
|
|
|
|
mbedtls_mpi X;
|
|
|
|
int res;
|
|
|
|
mbedtls_test_mpi_random rand;
|
2018-11-05 16:37:06 +01:00
|
|
|
uint8_t *witness_buf = NULL;
|
|
|
|
uint8_t *input_buf = NULL;
|
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
|
|
|
size_t witness_len;
|
|
|
|
size_t input_len;
|
|
|
|
|
2018-11-05 16:37:06 +01:00
|
|
|
witness_buf = unhexify_alloc( witnesses, &witness_len );
|
|
|
|
input_buf = unhexify_alloc( input_X, &input_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
|
|
|
|
|
|
|
mbedtls_mpi_init( &X );
|
|
|
|
rand.data = witness_buf;
|
|
|
|
rand.data_len = witness_len;
|
|
|
|
rand.pos = 0;
|
|
|
|
rand.chunk_len = chunk_len;
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_buf, input_len ) == 0 );
|
|
|
|
res = mbedtls_mpi_is_prime( &X, mbedtls_test_mpi_miller_rabin_determinizer,
|
|
|
|
&rand );
|
|
|
|
TEST_ASSERT( res == div_result );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &X );
|
2018-11-05 16:37:06 +01:00
|
|
|
mbedtls_free( witness_buf );
|
|
|
|
mbedtls_free( input_buf );
|
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 */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_gen_prime( int bits, int safe, 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;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &X );
|
2014-06-16 17:12:40 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
my_ret = mbedtls_mpi_gen_prime( &X, bits, safe, rnd_std_rand, NULL );
|
2014-06-16 17:12:40 +02:00
|
|
|
TEST_ASSERT( my_ret == ref_ret );
|
|
|
|
|
|
|
|
if( ref_ret == 0 )
|
|
|
|
{
|
2015-06-18 16:47:17 +02:00
|
|
|
size_t actual_bits = mbedtls_mpi_bitlen( &X );
|
2014-06-16 17:12:40 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( actual_bits >= (size_t) bits );
|
|
|
|
TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
|
2014-06-16 17:12:40 +02:00
|
|
|
if( safe )
|
|
|
|
{
|
2018-01-10 08:12:01 +01:00
|
|
|
/* X = ( X - 1 ) / 2 */
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_is_prime( &X, rnd_std_rand, NULL ) == 0 );
|
2014-06-16 17:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_shift_l( int radix_X, char *input_X, int shift_X, int radix_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *input_A)
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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 */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_mpi_shift_r( int radix_X, char *input_X, int shift_X, int radix_A,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *input_A )
|
2009-06-28 23:50:27 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi X, A;
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
|
2009-06-28 23:50:27 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &A, radix_A, input_A ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
|
|
|
|
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:
|
2015-04-08 12:49:31 +02: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
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2013-08-20 11:48:36 +02:00
|
|
|
void mpi_selftest()
|
2009-07-06 08:40:23 +02:00
|
|
|
{
|
2016-09-09 10:10:28 +02: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 */
|