2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecdsa.h"
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2013-01-26 19:09:07 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_ECDSA_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2013-01-26 19:09:07 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdsa_prim_random( int id )
|
2013-01-26 19:09:07 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point Q;
|
|
|
|
mbedtls_mpi d, r, s;
|
2013-01-26 19:09:07 +01:00
|
|
|
rnd_pseudo_info rnd_info;
|
|
|
|
unsigned char buf[66];
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &Q );
|
|
|
|
mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
|
2013-01-26 19:09:07 +01:00
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
2013-01-27 09:08:18 +01:00
|
|
|
memset( buf, 0, sizeof( buf ) );
|
2013-01-26 19:09:07 +01:00
|
|
|
|
|
|
|
/* prepare material for signature */
|
|
|
|
TEST_ASSERT( rnd_pseudo_rand( &rnd_info, buf, sizeof( buf ) ) == 0 );
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
|
2013-01-26 19:09:07 +01:00
|
|
|
== 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, buf, sizeof( buf ),
|
2013-01-26 19:09:07 +01:00
|
|
|
&rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_verify( &grp, buf, sizeof( buf ), &Q, &r, &s ) == 0 );
|
2013-01-26 19:09:07 +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 ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
|
2013-01-26 19:09:07 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ecdsa_prim_test_vectors( int id, char *d_str, char *xQ_str, char *yQ_str,
|
|
|
|
char *k_str, char *hash_str, char *r_str,
|
2017-11-17 18:09:31 +01:00
|
|
|
char *s_str, int result )
|
2013-01-27 08:10:28 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point Q;
|
|
|
|
mbedtls_mpi d, r, s, r_check, s_check;
|
2014-01-06 11:00:07 +01:00
|
|
|
unsigned char hash[66], rnd_buf[66];
|
|
|
|
size_t hlen;
|
|
|
|
rnd_buf_info rnd_info;
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_ecp_point_init( &Q );
|
|
|
|
mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
|
|
|
|
mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check );
|
2014-01-06 11:00:07 +01:00
|
|
|
memset( hash, 0, sizeof( hash ) );
|
|
|
|
memset( rnd_buf, 0, sizeof( rnd_buf ) );
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_point_read_string( &Q, 16, xQ_str, yQ_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, d_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &r_check, 16, r_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &s_check, 16, s_str ) == 0 );
|
2014-01-06 11:00:07 +01:00
|
|
|
hlen = unhexify(hash, hash_str);
|
|
|
|
rnd_info.buf = rnd_buf;
|
|
|
|
rnd_info.length = unhexify( rnd_buf, k_str );
|
|
|
|
|
|
|
|
/* Fix rnd_buf by shifting it left if necessary */
|
|
|
|
if( grp.nbits % 8 != 0 )
|
|
|
|
{
|
|
|
|
unsigned char shift = 8 - ( grp.nbits % 8 );
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for( i = 0; i < rnd_info.length - 1; i++ )
|
|
|
|
rnd_buf[i] = rnd_buf[i] << shift | rnd_buf[i+1] >> ( 8 - shift );
|
|
|
|
|
|
|
|
rnd_buf[rnd_info.length-1] <<= shift;
|
|
|
|
}
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, hash, hlen,
|
2017-11-17 18:09:31 +01:00
|
|
|
rnd_buffer_rand, &rnd_info ) == result );
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2017-11-17 18:09:31 +01:00
|
|
|
if ( result == 0)
|
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &r, &r_check ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &s, &s_check ) == 0 );
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2017-11-17 18:09:31 +01:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash, hlen, &Q, &r_check, &s_check ) == 0 );
|
|
|
|
}
|
2013-01-27 08:10:28 +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 ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
|
|
|
|
mbedtls_mpi_free( &r_check ); mbedtls_mpi_free( &s_check );
|
2013-01-27 08:10:28 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_DETERMINISTIC */
|
2014-01-06 14:25:56 +01:00
|
|
|
void ecdsa_det_test_vectors( int id, char *d_str, int md_alg,
|
|
|
|
char *msg, char *r_str, char *s_str )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_mpi d, r, s, r_check, s_check;
|
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
2014-01-06 14:25:56 +01:00
|
|
|
size_t hlen;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
|
|
|
mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
|
|
|
|
mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check );
|
2014-01-06 14:25:56 +01:00
|
|
|
memset( hash, 0, sizeof( hash ) );
|
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, d_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &r_check, 16, r_str ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_read_string( &s_check, 16, s_str ) == 0 );
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_info = mbedtls_md_info_from_type( md_alg );
|
2014-04-17 16:07:20 +02:00
|
|
|
TEST_ASSERT( md_info != NULL );
|
2015-04-08 12:49:31 +02:00
|
|
|
hlen = mbedtls_md_get_size( md_info );
|
|
|
|
mbedtls_md( md_info, (const unsigned char *) msg, strlen( msg ), hash );
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_sign_det( &grp, &r, &s, &d, hash, hlen, md_alg ) == 0 );
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &r, &r_check ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &s, &s_check ) == 0 );
|
2014-01-06 14:25:56 +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_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
|
|
|
|
mbedtls_mpi_free( &r_check ); mbedtls_mpi_free( &s_check );
|
2014-01-06 14:25:56 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
|
2013-08-08 13:30:57 +02:00
|
|
|
void ecdsa_write_read_random( int id )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdsa_context ctx;
|
2013-08-08 13:30:57 +02:00
|
|
|
rnd_pseudo_info rnd_info;
|
2015-03-31 11:41:42 +02:00
|
|
|
unsigned char hash[32];
|
2013-08-08 13:30:57 +02:00
|
|
|
unsigned char sig[200];
|
|
|
|
size_t sig_len, i;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdsa_init( &ctx );
|
2013-08-08 13:30:57 +02:00
|
|
|
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
|
|
|
|
memset( hash, 0, sizeof( hash ) );
|
|
|
|
memset( sig, 0x2a, sizeof( sig ) );
|
|
|
|
|
|
|
|
/* prepare material for signature */
|
|
|
|
TEST_ASSERT( rnd_pseudo_rand( &rnd_info, hash, sizeof( hash ) ) == 0 );
|
|
|
|
|
|
|
|
/* generate signing key */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_genkey( &ctx, id, &rnd_pseudo_rand, &rnd_info ) == 0 );
|
2013-08-08 13:30:57 +02:00
|
|
|
|
|
|
|
/* generate and write signature, then read and verify it */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_write_signature( &ctx, MBEDTLS_MD_SHA256,
|
2015-03-31 11:41:42 +02:00
|
|
|
hash, sizeof( hash ),
|
2013-08-08 13:30:57 +02:00
|
|
|
sig, &sig_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
|
2013-08-08 13:30:57 +02:00
|
|
|
sig, sig_len ) == 0 );
|
|
|
|
|
|
|
|
/* check we didn't write past the announced length */
|
|
|
|
for( i = sig_len; i < sizeof( sig ); i++ )
|
|
|
|
TEST_ASSERT( sig[i] == 0x2a );
|
|
|
|
|
|
|
|
/* try verification with invalid length */
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
|
2013-08-08 13:30:57 +02:00
|
|
|
sig, sig_len - 1 ) != 0 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
|
2013-08-08 13:30:57 +02:00
|
|
|
sig, sig_len + 1 ) != 0 );
|
|
|
|
|
|
|
|
/* try invalid sequence tag */
|
|
|
|
sig[0]++;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
|
2013-08-08 13:30:57 +02:00
|
|
|
sig, sig_len ) != 0 );
|
|
|
|
sig[0]--;
|
|
|
|
|
|
|
|
/* try modifying r */
|
|
|
|
sig[10]++;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
|
2013-08-08 13:30:57 +02:00
|
|
|
sig, sig_len ) != 0 );
|
|
|
|
sig[10]--;
|
|
|
|
|
|
|
|
/* try modifying s */
|
|
|
|
sig[sig_len - 1]++;
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
|
2013-08-08 13:30:57 +02:00
|
|
|
sig, sig_len ) != 0 );
|
|
|
|
sig[sig_len - 1]--;
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdsa_free( &ctx );
|
2013-08-08 13:30:57 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2014-01-06 10:27:16 +01:00
|
|
|
|