2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecp.h"
|
2013-08-16 13:38:47 +02:00
|
|
|
|
2021-04-03 21:47:53 +02:00
|
|
|
#include "ecp_invasive.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_TEST_HOOKS) && \
|
|
|
|
( defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
|
|
|
|
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) )
|
|
|
|
#define HAVE_FIX_NEGATIVE
|
|
|
|
#endif
|
|
|
|
|
2015-04-03 16:41:52 +02:00
|
|
|
#define ECP_PF_UNKNOWN -1
|
2018-10-16 11:22:45 +02:00
|
|
|
|
|
|
|
#define ECP_PT_RESET( x ) \
|
|
|
|
mbedtls_ecp_point_free( x ); \
|
|
|
|
mbedtls_ecp_point_init( x );
|
2021-03-29 21:32:16 +02:00
|
|
|
|
2022-03-18 13:58:26 +01:00
|
|
|
/* Auxiliary function to compare two mbedtls_ecp_group objects. */
|
2022-03-28 07:25:12 +02:00
|
|
|
inline static int mbedtls_ecp_group_cmp( mbedtls_ecp_group *grp1,
|
|
|
|
mbedtls_ecp_group *grp2 )
|
2022-03-18 13:58:26 +01:00
|
|
|
{
|
2022-03-28 07:25:12 +02:00
|
|
|
if( mbedtls_mpi_cmp_mpi( &grp1->P, &grp2->P ) != 0 )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( mbedtls_mpi_cmp_mpi( &grp1->A, &grp2->A ) != 0 )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( mbedtls_mpi_cmp_mpi( &grp1->B, &grp2->B ) != 0 )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( mbedtls_mpi_cmp_mpi( &grp1->N, &grp2->N ) != 0 )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( mbedtls_ecp_point_cmp( &grp1->G, &grp2->G ) != 0 )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->id != grp2->id )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->pbits != grp2->pbits )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->nbits != grp2->nbits )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->h != grp2->h )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->modp != grp2->modp )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->t_pre != grp2->t_pre )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->t_post != grp2->t_post )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->t_data != grp2->t_data )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->T_size != grp2->T_size )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
2022-03-28 07:25:12 +02:00
|
|
|
if( grp1->T != grp2->T )
|
2022-03-18 13:58:26 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_ECP_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2022-07-27 16:23:02 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-12-14 16:08:13 +01:00
|
|
|
void ecp_invalid_param( )
|
|
|
|
{
|
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point P;
|
|
|
|
int invalid_fmt = 42;
|
|
|
|
size_t olen;
|
|
|
|
unsigned char buf[42] = { 0 };
|
|
|
|
|
2021-05-21 08:50:00 +02:00
|
|
|
TEST_EQUAL( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
|
2018-12-14 16:08:13 +01:00
|
|
|
mbedtls_ecp_point_write_binary( &grp, &P,
|
|
|
|
invalid_fmt,
|
|
|
|
&olen,
|
|
|
|
buf, sizeof( buf ) ) );
|
2021-05-21 08:50:00 +02:00
|
|
|
TEST_EQUAL( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
|
2018-12-14 16:08:13 +01:00
|
|
|
mbedtls_ecp_tls_write_point( &grp, &P,
|
|
|
|
invalid_fmt,
|
|
|
|
&olen,
|
|
|
|
buf,
|
|
|
|
sizeof( buf ) ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-11-30 15:10:14 +01:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_ecp_curve_info( int id, int tls_id, int size, char * name )
|
2013-11-30 15:10:14 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_curve_info *by_id, *by_tls, *by_name;
|
2013-11-30 15:10:14 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
by_id = mbedtls_ecp_curve_info_from_grp_id( id );
|
|
|
|
by_tls = mbedtls_ecp_curve_info_from_tls_id( tls_id );
|
|
|
|
by_name = mbedtls_ecp_curve_info_from_name( name );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( by_id != NULL );
|
|
|
|
TEST_ASSERT( by_tls != NULL );
|
|
|
|
TEST_ASSERT( by_name != NULL );
|
2013-11-30 15:10:14 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( by_id == by_tls );
|
|
|
|
TEST_ASSERT( by_id == by_name );
|
|
|
|
|
2015-06-18 15:45:05 +02:00
|
|
|
TEST_ASSERT( by_id->bit_size == size );
|
2021-05-31 21:44:25 +02:00
|
|
|
TEST_ASSERT( size <= MBEDTLS_ECP_MAX_BITS );
|
|
|
|
TEST_ASSERT( size <= MBEDTLS_ECP_MAX_BYTES * 8 );
|
2013-11-30 15:10:14 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-12-04 11:08:01 +01:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void ecp_check_pub( int grp_id, char * x_hex, char * y_hex, char * z_hex,
|
|
|
|
int ret )
|
2013-12-04 11:08:01 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point P;
|
2013-12-04 11:08:01 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &P );
|
2013-12-04 11:08:01 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, grp_id ) == 0 );
|
2013-12-04 11:08:01 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &P.X, x_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &P.Y, y_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &P.Z, z_hex ) == 0 );
|
2013-12-04 11:08:01 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &P ) == ret );
|
2013-12-04 11:08:01 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &P );
|
2013-12-04 11:08:01 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-04-20 15:50:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
|
2017-03-08 11:41:47 +01:00
|
|
|
void ecp_test_vect_restart( int id,
|
|
|
|
char *dA_str, char *xA_str, char *yA_str,
|
|
|
|
char *dB_str, char *xZ_str, char *yZ_str,
|
|
|
|
int max_ops, int min_restarts, int max_restarts )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Test for early restart. Based on test vectors like ecp_test_vect(),
|
|
|
|
* but for the sake of simplicity only does half of each side. It's
|
|
|
|
* important to test both base point and random point, though, as memory
|
|
|
|
* management is different in each case.
|
|
|
|
*
|
|
|
|
* Don't try using too precise bounds for restarts as the exact number
|
|
|
|
* will depend on settings such as MBEDTLS_ECP_FIXED_POINT_OPTIM and
|
|
|
|
* MBEDTLS_ECP_WINDOW_SIZE, as well as implementation details that may
|
|
|
|
* change in the future. A factor 2 is a minimum safety margin.
|
|
|
|
*
|
|
|
|
* For reference, with mbed TLS 2.4 and default settings, for P-256:
|
2017-03-20 14:13:07 +01:00
|
|
|
* - Random point mult: ~3250M
|
|
|
|
* - Cold base point mult: ~3300M
|
|
|
|
* - Hot base point mult: ~1100M
|
2017-03-08 11:41:47 +01:00
|
|
|
* With MBEDTLS_ECP_WINDOW_SIZE set to 2 (minimum):
|
2017-03-20 14:13:07 +01:00
|
|
|
* - Random point mult: ~3850M
|
2017-03-08 11:41:47 +01:00
|
|
|
*/
|
2017-04-19 10:11:56 +02:00
|
|
|
mbedtls_ecp_restart_ctx ctx;
|
2017-03-08 11:41:47 +01:00
|
|
|
mbedtls_ecp_group grp;
|
2018-10-16 11:22:45 +02:00
|
|
|
mbedtls_ecp_point R, P;
|
2017-03-08 11:41:47 +01:00
|
|
|
mbedtls_mpi dA, xA, yA, dB, xZ, yZ;
|
|
|
|
int cnt_restarts;
|
|
|
|
int ret;
|
2021-06-15 11:29:26 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2017-03-08 11:41:47 +01:00
|
|
|
|
2017-04-19 10:11:56 +02:00
|
|
|
mbedtls_ecp_restart_init( &ctx );
|
2018-10-16 11:22:45 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &R ); mbedtls_ecp_point_init( &P );
|
2017-03-08 11:41:47 +01:00
|
|
|
mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA );
|
|
|
|
mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ );
|
2021-06-15 11:29:26 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2017-03-08 11:41:47 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dA, dA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xA, xA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &yA, yA_str ) == 0 );
|
2017-03-08 11:41:47 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dB, dB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xZ, xZ_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &yZ, yZ_str ) == 0 );
|
2017-03-08 11:41:47 +01:00
|
|
|
|
|
|
|
mbedtls_ecp_set_max_ops( (unsigned) max_ops );
|
|
|
|
|
|
|
|
/* Base point case */
|
|
|
|
cnt_restarts = 0;
|
|
|
|
do {
|
2018-10-16 11:22:45 +02:00
|
|
|
ECP_PT_RESET( &R );
|
2021-06-15 11:29:26 +02:00
|
|
|
ret = mbedtls_ecp_mul_restartable( &grp, &R, &dA, &grp.G,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info, &ctx );
|
2017-08-28 12:20:39 +02:00
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
|
2017-03-08 11:41:47 +01:00
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2017-03-08 11:41:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( cnt_restarts >= min_restarts );
|
|
|
|
TEST_ASSERT( cnt_restarts <= max_restarts );
|
|
|
|
|
|
|
|
/* Non-base point case */
|
2018-10-16 11:22:45 +02:00
|
|
|
mbedtls_ecp_copy( &P, &R );
|
2017-03-08 11:41:47 +01:00
|
|
|
cnt_restarts = 0;
|
|
|
|
do {
|
2018-10-16 11:22:45 +02:00
|
|
|
ECP_PT_RESET( &R );
|
2021-06-15 11:29:26 +02:00
|
|
|
ret = mbedtls_ecp_mul_restartable( &grp, &R, &dB, &P,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info, &ctx );
|
2017-08-28 12:20:39 +02:00
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
|
2017-03-08 11:41:47 +01:00
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2017-03-08 11:41:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( cnt_restarts >= min_restarts );
|
|
|
|
TEST_ASSERT( cnt_restarts <= max_restarts );
|
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
/* Do we leak memory when aborting an operation?
|
|
|
|
* This test only makes sense when we actually restart */
|
|
|
|
if( min_restarts > 0 )
|
|
|
|
{
|
2021-06-15 11:29:26 +02:00
|
|
|
ret = mbedtls_ecp_mul_restartable( &grp, &R, &dB, &P,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info, &ctx );
|
2017-08-28 12:20:39 +02:00
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
|
|
|
|
}
|
2017-03-14 10:58:00 +01:00
|
|
|
|
2017-03-08 11:41:47 +01:00
|
|
|
exit:
|
2017-04-19 10:11:56 +02:00
|
|
|
mbedtls_ecp_restart_free( &ctx );
|
2018-10-16 11:22:45 +02:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &R ); mbedtls_ecp_point_free( &P );
|
2017-03-08 11:41:47 +01:00
|
|
|
mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA );
|
|
|
|
mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2017-04-20 15:50:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
|
2017-04-20 13:36:18 +02:00
|
|
|
void ecp_muladd_restart( int id, char *xR_str, char *yR_str,
|
|
|
|
char *u1_str, char *u2_str,
|
|
|
|
char *xQ_str, char *yQ_str,
|
|
|
|
int max_ops, int min_restarts, int max_restarts )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Compute R = u1 * G + u2 * Q
|
|
|
|
* (test vectors mostly taken from ECDSA intermediate results)
|
|
|
|
*
|
|
|
|
* See comments at the top of ecp_test_vect_restart()
|
|
|
|
*/
|
|
|
|
mbedtls_ecp_restart_ctx ctx;
|
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point R, Q;
|
|
|
|
mbedtls_mpi u1, u2, xR, yR;
|
|
|
|
int cnt_restarts;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
mbedtls_ecp_restart_init( &ctx );
|
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &R );
|
|
|
|
mbedtls_ecp_point_init( &Q );
|
|
|
|
mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
|
|
|
|
mbedtls_mpi_init( &xR ); mbedtls_mpi_init( &yR );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &u1, u1_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &u2, u2_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xR, xR_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &yR, yR_str ) == 0 );
|
2017-04-20 13:36:18 +02:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Q.X, xQ_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Q.Y, yQ_str ) == 0 );
|
2017-04-20 13:36:18 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_lset( &Q.Z, 1 ) == 0 );
|
|
|
|
|
|
|
|
mbedtls_ecp_set_max_ops( (unsigned) max_ops );
|
|
|
|
|
|
|
|
cnt_restarts = 0;
|
|
|
|
do {
|
2018-10-16 11:22:45 +02:00
|
|
|
ECP_PT_RESET( &R );
|
2017-04-20 13:36:18 +02:00
|
|
|
ret = mbedtls_ecp_muladd_restartable( &grp, &R,
|
|
|
|
&u1, &grp.G, &u2, &Q, &ctx );
|
2017-08-28 12:20:39 +02:00
|
|
|
} while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
|
2017-04-20 13:36:18 +02:00
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2017-04-20 13:36:18 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xR ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yR ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( cnt_restarts >= min_restarts );
|
|
|
|
TEST_ASSERT( cnt_restarts <= max_restarts );
|
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
/* Do we leak memory when aborting an operation?
|
|
|
|
* This test only makes sense when we actually restart */
|
|
|
|
if( min_restarts > 0 )
|
|
|
|
{
|
|
|
|
ret = mbedtls_ecp_muladd_restartable( &grp, &R,
|
|
|
|
&u1, &grp.G, &u2, &Q, &ctx );
|
|
|
|
TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
|
|
|
|
}
|
2017-04-20 13:36:18 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecp_restart_free( &ctx );
|
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &R );
|
|
|
|
mbedtls_ecp_point_free( &Q );
|
|
|
|
mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
|
|
|
|
mbedtls_mpi_free( &xR ); mbedtls_mpi_free( &yR );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void ecp_test_vect( int id, char * dA_str, char * xA_str, char * yA_str,
|
|
|
|
char * dB_str, char * xB_str, char * yB_str,
|
|
|
|
char * xZ_str, char * yZ_str )
|
2012-11-07 21:39:45 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point R;
|
|
|
|
mbedtls_mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
|
|
|
|
mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA ); mbedtls_mpi_init( &dB );
|
|
|
|
mbedtls_mpi_init( &xB ); mbedtls_mpi_init( &yB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ );
|
2020-06-10 12:12:18 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2012-11-07 21:39:45 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
|
2012-11-24 12:05:44 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dA, dA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xA, xA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &yA, yA_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dB, dB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xB, xB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &yB, yB_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xZ, xZ_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &yZ, yZ_str ) == 0 );
|
2012-11-07 12:24:22 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G,
|
2020-06-10 12:12:18 +02:00
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
|
2021-06-15 11:29:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
|
2021-06-15 11:29:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yB ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R,
|
2020-06-10 12:12:18 +02:00
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
|
2012-11-07 12:24:22 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
|
|
|
|
mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA ); mbedtls_mpi_free( &dB );
|
|
|
|
mbedtls_mpi_free( &xB ); mbedtls_mpi_free( &yB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ );
|
2012-11-07 21:39:45 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-11 20:45:18 +01:00
|
|
|
|
2013-12-04 11:49:20 +01:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void ecp_test_vec_x( int id, char * dA_hex, char * xA_hex, char * dB_hex,
|
|
|
|
char * xB_hex, char * xS_hex )
|
2013-12-04 11:49:20 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point R;
|
|
|
|
mbedtls_mpi dA, xA, dB, xB, xS;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
|
|
|
|
mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA );
|
|
|
|
mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xB );
|
|
|
|
mbedtls_mpi_init( &xS );
|
2020-06-10 12:12:18 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dA, dA_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &dB, dB_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xA, xA_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xB, xB_hex ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &xS, xS_hex ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G,
|
2020-06-10 12:12:18 +02:00
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R,
|
2020-06-10 12:12:18 +02:00
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2021-06-15 11:29:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2021-06-15 11:29:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 );
|
2013-12-04 11:49:20 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
|
|
|
|
mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA );
|
|
|
|
mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xB );
|
|
|
|
mbedtls_mpi_free( &xS );
|
2013-12-04 11:49:20 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-04-26 15:28:19 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_test_mul( int id, data_t * n_hex,
|
|
|
|
data_t * Px_hex, data_t * Py_hex, data_t * Pz_hex,
|
|
|
|
data_t * nPx_hex, data_t * nPy_hex, data_t * nPz_hex,
|
|
|
|
int expected_ret )
|
|
|
|
{
|
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point P, nP, R;
|
|
|
|
mbedtls_mpi n;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2019-04-26 15:28:19 +02:00
|
|
|
|
|
|
|
mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
|
|
|
|
mbedtls_ecp_point_init( &P ); mbedtls_ecp_point_init( &nP );
|
|
|
|
mbedtls_mpi_init( &n );
|
2020-06-10 12:12:18 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2019-04-26 15:28:19 +02:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &n, n_hex->x, n_hex->len ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &P.X, Px_hex->x, Px_hex->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &P.Y, Py_hex->x, Py_hex->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &P.Z, Pz_hex->x, Pz_hex->len ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &nP.X, nPx_hex->x, nPx_hex->len )
|
|
|
|
== 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &nP.Y, nPy_hex->x, nPy_hex->len )
|
|
|
|
== 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &nP.Z, nPz_hex->x, nPz_hex->len )
|
|
|
|
== 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &n, &P,
|
2020-06-10 12:12:18 +02:00
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info )
|
2019-04-26 15:28:19 +02:00
|
|
|
== expected_ret );
|
|
|
|
|
|
|
|
if( expected_ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.X, &R.X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.Y, &R.Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.Z, &R.Z ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
|
|
|
|
mbedtls_ecp_point_free( &P ); mbedtls_ecp_point_free( &nP );
|
|
|
|
mbedtls_mpi_free( &n );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-05-13 07:22:45 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_test_mul_rng( int id, data_t * d_hex)
|
|
|
|
{
|
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_mpi d;
|
|
|
|
mbedtls_ecp_point Q;
|
|
|
|
|
|
|
|
mbedtls_ecp_group_init( &grp ); mbedtls_mpi_init( &d );
|
|
|
|
mbedtls_ecp_point_init( &Q );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_binary( &d, d_hex->x, d_hex->len ) == 0 );
|
|
|
|
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_mul( &grp, &Q, &d, &grp.G,
|
|
|
|
&mbedtls_test_rnd_zero_rand, NULL )
|
2020-05-13 07:22:45 +02:00
|
|
|
== MBEDTLS_ERR_ECP_RANDOM_FAILED );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecp_group_free( &grp ); mbedtls_mpi_free( &d );
|
|
|
|
mbedtls_ecp_point_free( &Q );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-03 18:31:01 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
|
|
|
void ecp_muladd( int id,
|
|
|
|
data_t *u1_bin, data_t *P1_bin,
|
|
|
|
data_t *u2_bin, data_t *P2_bin,
|
|
|
|
data_t *expected_result )
|
|
|
|
{
|
|
|
|
/* Compute R = u1 * P1 + u2 * P2 */
|
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point P1, P2, R;
|
|
|
|
mbedtls_mpi u1, u2;
|
|
|
|
uint8_t actual_result[MBEDTLS_ECP_MAX_PT_LEN];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &P1 );
|
|
|
|
mbedtls_ecp_point_init( &P2 );
|
|
|
|
mbedtls_ecp_point_init( &R );
|
|
|
|
mbedtls_mpi_init( &u1 );
|
|
|
|
mbedtls_mpi_init( &u2 );
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_ecp_group_load( &grp, id ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_read_binary( &u1, u1_bin->x, u1_bin->len ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_read_binary( &u2, u2_bin->x, u2_bin->len ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_ecp_point_read_binary( &grp, &P1,
|
|
|
|
P1_bin->x, P1_bin->len ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_ecp_point_read_binary( &grp, &P2,
|
|
|
|
P2_bin->x, P2_bin->len ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_ecp_muladd( &grp, &R, &u1, &P1, &u2, &P2 ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_ecp_point_write_binary(
|
|
|
|
&grp, &R, MBEDTLS_ECP_PF_UNCOMPRESSED,
|
|
|
|
&len, actual_result, sizeof( actual_result ) ) );
|
2021-05-31 21:44:25 +02:00
|
|
|
TEST_ASSERT( len <= MBEDTLS_ECP_MAX_PT_LEN );
|
2021-04-03 18:31:01 +02:00
|
|
|
|
|
|
|
ASSERT_COMPARE( expected_result->x, expected_result->len,
|
|
|
|
actual_result, len );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &P1 );
|
|
|
|
mbedtls_ecp_point_free( &P2 );
|
|
|
|
mbedtls_ecp_point_free( &R );
|
|
|
|
mbedtls_mpi_free( &u1 );
|
|
|
|
mbedtls_mpi_free( &u2 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void ecp_fast_mod( int id, char * N_str )
|
2012-11-11 20:45:18 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_mpi N, R;
|
2012-11-11 20:45:18 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &R );
|
|
|
|
mbedtls_ecp_group_init( &grp );
|
2012-11-11 20:45:18 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &N, N_str ) == 0 );
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-10-21 14:52:21 +02:00
|
|
|
TEST_ASSERT( grp.modp != NULL );
|
2012-11-11 20:45:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Store correct result before we touch N
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
|
2012-11-11 20:45:18 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( grp.modp( &N ) == 0 );
|
2015-06-18 16:47:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_bitlen( &N ) <= grp.pbits + 3 );
|
2012-11-11 20:45:18 +01:00
|
|
|
|
|
|
|
/*
|
2014-04-11 15:31:33 +02:00
|
|
|
* Use mod rather than addition/subtraction in case previous test fails
|
2012-11-11 20:45:18 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &R ) == 0 );
|
2012-11-11 20:45:18 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &R );
|
|
|
|
mbedtls_ecp_group_free( &grp );
|
2012-11-11 20:45:18 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-13 20:57:00 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void ecp_write_binary( int id, char * x, char * y, char * z, int format,
|
2018-06-29 12:05:32 +02:00
|
|
|
data_t * out, int blen, int ret )
|
2012-11-24 14:10:14 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point P;
|
2017-05-30 15:23:15 +02:00
|
|
|
unsigned char buf[256];
|
2013-02-10 12:22:46 +01:00
|
|
|
size_t olen;
|
2012-11-24 14:10:14 +01:00
|
|
|
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &P.X, x ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &P.Y, y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &P.Z, z ) == 0 );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_point_write_binary( &grp, &P, format,
|
2013-08-20 11:48:36 +02:00
|
|
|
&olen, buf, blen ) == ret );
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ret == 0 )
|
2012-11-24 14:10:14 +01:00
|
|
|
{
|
2021-05-31 21:44:25 +02:00
|
|
|
TEST_ASSERT( olen <= MBEDTLS_ECP_MAX_PT_LEN );
|
2020-06-10 11:03:08 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_hexcmp( buf, out->x, olen, out->len ) == 0 );
|
2012-11-24 14:10:14 +01:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
|
2012-11-24 14:10:14 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-24 14:10:14 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void ecp_read_binary( int id, data_t * buf, char * x, char * y, char * z,
|
2013-08-20 11:48:36 +02:00
|
|
|
int ret )
|
2012-11-24 16:19:42 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point P;
|
|
|
|
mbedtls_mpi X, Y, Z;
|
2012-11-24 16:19:42 +01:00
|
|
|
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, x ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Z, z ) == 0 );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_point_read_binary( &grp, &P, buf->x, buf->len ) == ret );
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ret == 0 )
|
2012-11-24 16:19:42 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 );
|
2019-02-26 13:36:52 +01:00
|
|
|
if( mbedtls_ecp_get_type( &grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
|
2019-02-13 11:44:06 +01:00
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, 0 ) == 0 );
|
|
|
|
TEST_ASSERT( P.Y.p == NULL );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &Z, 1 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_int( &P.Z, 1 ) == 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 );
|
|
|
|
}
|
2012-11-24 16:19:42 +01:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
|
2012-11-24 16:19:42 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2012-11-24 16:19:42 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void mbedtls_ecp_tls_read_point( int id, data_t * buf, char * x, char * y,
|
2017-06-09 05:32:58 +02:00
|
|
|
char * z, int ret )
|
2013-02-10 13:00:20 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point P;
|
|
|
|
mbedtls_mpi X, Y, Z;
|
2017-06-09 05:32:58 +02:00
|
|
|
const unsigned char *vbuf = buf->x;
|
2013-02-10 13:00:20 +01:00
|
|
|
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
|
|
|
|
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &X, x ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Y, y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &Z, z ) == 0 );
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &P, &vbuf, buf->len ) == ret );
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
if( ret == 0 )
|
2013-02-10 13:00:20 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 );
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( (uint32_t)( vbuf - buf->x ) == buf->len );
|
2013-02-10 13:00:20 +01:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
|
|
|
|
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
|
2013-02-10 13:00:20 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-10 13:00:20 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_tls_write_read_point( int id )
|
2013-01-26 14:42:45 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point pt;
|
2013-02-10 11:15:11 +01:00
|
|
|
unsigned char buf[256];
|
2013-02-10 13:38:29 +01:00
|
|
|
const unsigned char *vbuf;
|
2013-02-10 12:22:46 +01:00
|
|
|
size_t olen;
|
2013-01-26 14:42:45 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &pt );
|
2013-01-26 14:42:45 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-01-26 14:42:45 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G,
|
|
|
|
MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen )
|
|
|
|
== MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( vbuf == buf + olen );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G,
|
|
|
|
MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( vbuf == buf + olen );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt,
|
|
|
|
MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( vbuf == buf + olen );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2013-02-10 13:38:29 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt,
|
|
|
|
MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) );
|
2013-02-10 13:38:29 +01:00
|
|
|
TEST_ASSERT( vbuf == buf + olen );
|
2013-01-26 14:42:45 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &pt );
|
2013-01-26 14:42:45 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-01-26 14:42:45 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2018-06-29 12:05:32 +02:00
|
|
|
void mbedtls_ecp_tls_read_group( data_t * buf, int result, int bits,
|
2017-06-09 05:32:58 +02:00
|
|
|
int record_len )
|
2013-02-09 17:03:58 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
2017-06-09 05:32:58 +02:00
|
|
|
const unsigned char *vbuf = buf->x;
|
2017-05-30 15:23:15 +02:00
|
|
|
int ret;
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
ret = mbedtls_ecp_tls_read_group( &grp, &vbuf, buf->len );
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
TEST_ASSERT( ret == result );
|
2013-02-09 17:03:58 +01:00
|
|
|
if( ret == 0)
|
2013-02-10 13:20:52 +01:00
|
|
|
{
|
2015-06-18 16:47:17 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_bitlen( &grp.P ) == (size_t) bits );
|
2017-06-09 05:32:58 +02:00
|
|
|
TEST_ASSERT( vbuf - buf->x == record_len);
|
2013-02-10 13:20:52 +01:00
|
|
|
}
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
2013-02-09 17:03:58 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-09 17:03:58 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_tls_write_read_group( int id )
|
2013-02-10 12:51:17 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp1, grp2;
|
2013-02-10 12:51:17 +01:00
|
|
|
unsigned char buf[10];
|
2013-02-10 13:20:52 +01:00
|
|
|
const unsigned char *vbuf = buf;
|
2013-02-10 12:51:17 +01:00
|
|
|
size_t len;
|
|
|
|
int ret;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp1 );
|
|
|
|
mbedtls_ecp_group_init( &grp2 );
|
2013-02-10 12:51:17 +01:00
|
|
|
memset( buf, 0x00, sizeof( buf ) );
|
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp1, id ) == 0 );
|
2013-02-10 12:51:17 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
|
|
|
|
ret = mbedtls_ecp_tls_read_group( &grp2, &vbuf, len );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
2013-02-10 12:51:17 +01:00
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
|
2013-02-10 12:51:17 +01:00
|
|
|
TEST_ASSERT( grp1.id == grp2.id );
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp1 );
|
|
|
|
mbedtls_ecp_group_free( &grp2 );
|
2013-02-10 12:51:17 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-10 12:51:17 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_ecp_check_privkey( int id, char * key_hex, int ret )
|
2013-07-01 14:06:13 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_mpi d;
|
2013-07-01 14:06:13 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_mpi_init( &d );
|
2013-07-01 14:06:13 +02:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &d, key_hex ) == 0 );
|
2013-07-01 14:06:13 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == ret );
|
2013-07-01 14:06:13 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_mpi_free( &d );
|
2013-07-01 14:06:13 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-07-01 14:06:13 +02:00
|
|
|
|
2014-11-06 15:25:32 +01:00
|
|
|
/* BEGIN_CASE */
|
2017-05-30 15:23:15 +02:00
|
|
|
void mbedtls_ecp_check_pub_priv( int id_pub, char * Qx_pub, char * Qy_pub,
|
|
|
|
int id, char * d, char * Qx, char * Qy,
|
|
|
|
int ret )
|
2014-11-06 15:25:32 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair pub, prv;
|
2021-06-15 11:29:26 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2014-11-06 15:25:32 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair_init( &pub );
|
|
|
|
mbedtls_ecp_keypair_init( &prv );
|
2021-06-15 11:29:26 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2014-11-06 15:25:32 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( id_pub != MBEDTLS_ECP_DP_NONE )
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &pub.grp, id_pub ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_point_read_string( &pub.Q, 16, Qx_pub, Qy_pub ) == 0 );
|
2014-11-06 15:25:32 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( id != MBEDTLS_ECP_DP_NONE )
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &prv.grp, id ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_point_read_string( &prv.Q, 16, Qx, Qy ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &prv.d, d ) == 0 );
|
2014-11-06 15:25:32 +01:00
|
|
|
|
2021-06-15 11:29:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pub_priv( &pub, &prv,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret );
|
2014-11-06 15:25:32 +01:00
|
|
|
|
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair_free( &pub );
|
|
|
|
mbedtls_ecp_keypair_free( &prv );
|
2014-11-06 15:25:32 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ecp_gen_keypair( int id )
|
2013-02-10 11:15:11 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point Q;
|
|
|
|
mbedtls_mpi d;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &Q );
|
|
|
|
mbedtls_mpi_init( &d );
|
2020-06-10 12:12:18 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info ) == 0 );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &Q ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == 0 );
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_ecp_point_free( &Q );
|
|
|
|
mbedtls_mpi_free( &d );
|
2013-02-10 11:15:11 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-02-10 11:15:11 +01:00
|
|
|
|
2013-11-30 14:13:16 +01:00
|
|
|
/* BEGIN_CASE */
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ecp_gen_key( int id )
|
2013-11-30 14:13:16 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair key;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2013-11-30 14:13:16 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair_init( &key );
|
2020-06-10 12:12:18 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
2013-11-30 14:13:16 +01:00
|
|
|
|
2020-06-10 14:08:26 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_gen_key( id, &key,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info ) == 0 );
|
2013-11-30 14:13:16 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_check_privkey( &key.grp, &key.d ) == 0 );
|
2013-11-30 14:13:16 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair_free( &key );
|
2013-11-30 14:13:16 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-02-15 17:17:45 +01:00
|
|
|
/* BEGIN_CASE */
|
2020-06-09 19:55:26 +02:00
|
|
|
void mbedtls_ecp_read_key( int grp_id, data_t* in_key, int expected, int canonical )
|
2019-02-15 17:17:45 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_ecp_keypair key;
|
2020-06-09 19:55:26 +02:00
|
|
|
mbedtls_ecp_keypair key2;
|
2019-02-15 17:17:45 +01:00
|
|
|
|
|
|
|
mbedtls_ecp_keypair_init( &key );
|
2020-06-09 19:55:26 +02:00
|
|
|
mbedtls_ecp_keypair_init( &key2 );
|
2019-02-15 17:17:45 +01:00
|
|
|
|
|
|
|
ret = mbedtls_ecp_read_key( grp_id, &key, in_key->x, in_key->len );
|
|
|
|
TEST_ASSERT( ret == expected );
|
|
|
|
|
|
|
|
if( expected == 0 )
|
|
|
|
{
|
|
|
|
ret = mbedtls_ecp_check_privkey( &key.grp, &key.d );
|
|
|
|
TEST_ASSERT( ret == 0 );
|
2020-06-09 19:55:26 +02:00
|
|
|
|
|
|
|
if( canonical )
|
|
|
|
{
|
|
|
|
unsigned char buf[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
|
2020-07-13 10:59:40 +02:00
|
|
|
ret = mbedtls_ecp_write_key( &key, buf, in_key->len );
|
2020-06-09 19:55:26 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
|
|
|
|
ASSERT_COMPARE( in_key->x, in_key->len,
|
2020-06-11 17:00:36 +02:00
|
|
|
buf, in_key->len );
|
2020-06-09 19:55:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned char export1[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
unsigned char export2[MBEDTLS_ECP_MAX_BYTES];
|
|
|
|
|
2020-07-13 10:59:40 +02:00
|
|
|
ret = mbedtls_ecp_write_key( &key, export1, in_key->len );
|
2020-06-09 19:55:26 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
|
|
|
|
ret = mbedtls_ecp_read_key( grp_id, &key2, export1, in_key->len );
|
|
|
|
TEST_ASSERT( ret == expected );
|
|
|
|
|
2020-07-13 10:59:40 +02:00
|
|
|
ret = mbedtls_ecp_write_key( &key2, export2, in_key->len );
|
2020-06-09 19:55:26 +02:00
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
|
2020-06-11 17:00:36 +02:00
|
|
|
ASSERT_COMPARE( export1, in_key->len,
|
|
|
|
export2, in_key->len );
|
2020-06-09 19:55:26 +02:00
|
|
|
}
|
2019-02-15 17:17:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecp_keypair_free( &key );
|
2020-06-09 19:55:26 +02:00
|
|
|
mbedtls_ecp_keypair_free( &key2 );
|
2019-02-15 17:17:45 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-03 21:47:53 +02:00
|
|
|
/* BEGIN_CASE depends_on:HAVE_FIX_NEGATIVE */
|
|
|
|
void fix_negative( data_t *N_bin, int c, int bits )
|
|
|
|
{
|
|
|
|
mbedtls_mpi C, M, N;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &C );
|
|
|
|
mbedtls_mpi_init( &M );
|
|
|
|
mbedtls_mpi_init( &N );
|
|
|
|
|
2021-04-09 15:46:51 +02:00
|
|
|
/* C = - c * 2^bits (positive since c is negative) */
|
2021-04-03 21:47:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_lset( &C, -c ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_shift_l( &C, bits ) );
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_read_binary( &N, N_bin->x, N_bin->len ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_grow( &N, C.n ) );
|
|
|
|
|
2021-04-09 15:46:51 +02:00
|
|
|
/* M = N - C = - ( C - N ) (expected result of fix_negative) */
|
2021-04-03 21:47:53 +02:00
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_sub_mpi( &M, &N, &C ) );
|
|
|
|
|
|
|
|
mbedtls_ecp_fix_negative( &N, c, bits );
|
|
|
|
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_cmp_mpi( &N, &M ) );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_mpi_free( &C );
|
|
|
|
mbedtls_mpi_free( &M );
|
|
|
|
mbedtls_mpi_free( &N );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-03-24 12:01:02 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
|
|
|
void genkey_mx_known_answer( int bits, data_t *seed, data_t *expected )
|
|
|
|
{
|
|
|
|
mbedtls_test_rnd_buf_info rnd_info;
|
|
|
|
mbedtls_mpi d;
|
|
|
|
int ret;
|
|
|
|
uint8_t *actual = NULL;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &d );
|
|
|
|
rnd_info.buf = seed->x;
|
|
|
|
rnd_info.length = seed->len;
|
|
|
|
rnd_info.fallback_f_rng = NULL;
|
|
|
|
rnd_info.fallback_p_rng = NULL;
|
|
|
|
|
|
|
|
ASSERT_ALLOC( actual, expected->len );
|
|
|
|
|
|
|
|
ret = mbedtls_ecp_gen_privkey_mx( bits, &d,
|
|
|
|
mbedtls_test_rnd_buffer_rand, &rnd_info );
|
|
|
|
|
|
|
|
if( expected->len == 0 )
|
|
|
|
{
|
|
|
|
/* Expecting an error (happens if there isn't enough randomness) */
|
|
|
|
TEST_ASSERT( ret != 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_EQUAL( ret, 0 );
|
|
|
|
TEST_EQUAL( (size_t) bits + 1, mbedtls_mpi_bitlen( &d ) );
|
|
|
|
TEST_EQUAL( 0, mbedtls_mpi_write_binary( &d, actual, expected->len ) );
|
|
|
|
/* Test the exact result. This assumes that the output of the
|
|
|
|
* RNG is used in a specific way, which is overly constraining.
|
|
|
|
* The advantage is that it's easier to test the expected properties
|
|
|
|
* of the generated key:
|
|
|
|
* - The most significant bit must be at a specific positions
|
|
|
|
* (can be enforced by checking the bit-length).
|
|
|
|
* - The least significant bits must have specific values
|
|
|
|
* (can be enforced by checking these bits).
|
|
|
|
* - Other bits must be random (by testing with different RNG outputs,
|
|
|
|
* we validate that those bits are indeed influenced by the RNG). */
|
|
|
|
ASSERT_COMPARE( expected->x, expected->len,
|
|
|
|
actual, expected->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( actual );
|
|
|
|
mbedtls_mpi_free( &d );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2017-05-30 15:23:15 +02:00
|
|
|
void ecp_selftest( )
|
2012-11-13 20:57:00 +01:00
|
|
|
{
|
2016-09-09 10:10:28 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_self_test( 1 ) == 0 );
|
2012-11-13 20:57:00 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2022-03-18 13:58:26 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_export( int id, char * Qx, char * Qy,char * d, int expected_ret, int invalid_grp )
|
|
|
|
{
|
|
|
|
mbedtls_ecp_keypair key;
|
|
|
|
mbedtls_ecp_group export_grp;
|
|
|
|
mbedtls_mpi export_d;
|
|
|
|
mbedtls_ecp_point export_Q;
|
|
|
|
|
|
|
|
mbedtls_ecp_group_init( &export_grp );
|
|
|
|
mbedtls_ecp_group_init( &key.grp );
|
|
|
|
mbedtls_mpi_init( &export_d );
|
|
|
|
mbedtls_ecp_point_init( &export_Q );
|
|
|
|
|
|
|
|
mbedtls_ecp_keypair_init( &key );
|
|
|
|
if( invalid_grp == 0 )
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &key.grp, id ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ecp_point_read_string( &key.Q, 16, Qx, Qy ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &key.d, d ) == 0 );
|
2022-03-18 13:58:26 +01:00
|
|
|
|
|
|
|
TEST_EQUAL( mbedtls_ecp_export( &key, &export_grp,
|
|
|
|
&export_d, &export_Q ), expected_ret );
|
|
|
|
|
|
|
|
if( expected_ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_EQUAL( mbedtls_ecp_point_cmp( &key.Q, &export_Q ), 0 );
|
|
|
|
TEST_EQUAL( mbedtls_mpi_cmp_mpi( &key.d, &export_d ), 0 );
|
|
|
|
TEST_EQUAL( mbedtls_ecp_group_cmp( &key.grp, &export_grp ), 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecp_keypair_free( &key );
|
|
|
|
mbedtls_ecp_group_free( &export_grp );
|
|
|
|
mbedtls_mpi_free( &export_d );
|
|
|
|
mbedtls_ecp_point_free( &export_Q );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-06-17 14:41:18 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecp_check_order( int id, char * expected_order_hex )
|
|
|
|
{
|
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_mpi expected_n;
|
|
|
|
|
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_mpi_init( &expected_n );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2022-07-07 12:02:27 +02:00
|
|
|
TEST_ASSERT( mbedtls_test_read_mpi( &expected_n, expected_order_hex ) == 0);
|
2022-06-17 14:41:18 +02:00
|
|
|
|
|
|
|
// check sign bits are well-formed (i.e. 1 or -1) - see #5810
|
|
|
|
TEST_ASSERT( grp.N.s == -1 || grp.N.s == 1);
|
|
|
|
TEST_ASSERT( expected_n.s == -1 || expected_n.s == 1);
|
2022-06-17 14:48:29 +02:00
|
|
|
|
2022-06-17 14:41:18 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.N, &expected_n ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ecp_group_free( &grp );
|
|
|
|
mbedtls_mpi_free( &expected_n );
|
|
|
|
}
|
2022-06-17 15:59:36 +02:00
|
|
|
/* END_CASE */
|