blob: 5e1ba6431a6a6ff67a5585a10c014a6abf72b0ef [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnardd1c71502013-01-26 19:09:07 +01002#include <polarssl/ecdsa.h>
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Manuel Pégourié-Gonnardd1c71502013-01-26 19:09:07 +01004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
6 * depends_on:POLARSSL_ECDSA_C:POLARSSL_ECP_C:POLARSSL_BIGNUM_C
7 * END_DEPENDENCIES
8 */
Manuel Pégourié-Gonnardd1c71502013-01-26 19:09:07 +01009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
11void ecdsa_prim_random( int id )
Manuel Pégourié-Gonnardd1c71502013-01-26 19:09:07 +010012{
13 ecp_group grp;
14 ecp_point Q;
15 mpi d, r, s;
16 rnd_pseudo_info rnd_info;
17 unsigned char buf[66];
18
19 ecp_group_init( &grp );
20 ecp_point_init( &Q );
21 mpi_init( &d ); mpi_init( &r ); mpi_init( &s );
22 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Manuel Pégourié-Gonnard450a1632013-01-27 09:08:18 +010023 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnardd1c71502013-01-26 19:09:07 +010024
25 /* prepare material for signature */
26 TEST_ASSERT( rnd_pseudo_rand( &rnd_info, buf, sizeof( buf ) ) == 0 );
Paul Bakker33b43f12013-08-20 11:48:36 +020027 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
Manuel Pégourié-Gonnardd1c71502013-01-26 19:09:07 +010028 TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
29 == 0 );
30
31 TEST_ASSERT( ecdsa_sign( &grp, &r, &s, &d, buf, sizeof( buf ),
32 &rnd_pseudo_rand, &rnd_info ) == 0 );
33 TEST_ASSERT( ecdsa_verify( &grp, buf, sizeof( buf ), &Q, &r, &s ) == 0 );
34
35 ecp_group_free( &grp );
36 ecp_point_free( &Q );
37 mpi_free( &d ); mpi_free( &r ); mpi_free( &s );
38}
Paul Bakker33b43f12013-08-20 11:48:36 +020039/* END_CASE */
Manuel Pégourié-Gonnard602a8972013-01-27 08:10:28 +010040
Paul Bakker33b43f12013-08-20 11:48:36 +020041/* BEGIN_CASE */
42void ecdsa_prim_test_vectors( int id, char *d_str, char *xQ_str, char *yQ_str,
43 char *k_str, char *hash_str, char *r_str,
44 char *s_str )
Manuel Pégourié-Gonnard602a8972013-01-27 08:10:28 +010045{
46 ecp_group grp;
47 ecp_point Q;
48 mpi d, r, s, r_check, s_check;
49 unsigned char buf[66];
50 size_t len;
51
52 ecp_group_init( &grp );
53 ecp_point_init( &Q );
54 mpi_init( &d ); mpi_init( &r ); mpi_init( &s );
55 mpi_init( &r_check ); mpi_init( &s_check );
Manuel Pégourié-Gonnard450a1632013-01-27 09:08:18 +010056 memset( buf, 0, sizeof( buf ) );
Manuel Pégourié-Gonnard602a8972013-01-27 08:10:28 +010057
Paul Bakker33b43f12013-08-20 11:48:36 +020058 TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 );
59 TEST_ASSERT( ecp_point_read_string( &Q, 16, xQ_str, yQ_str ) == 0 );
60 TEST_ASSERT( mpi_read_string( &d, 16, d_str ) == 0 );
61 TEST_ASSERT( mpi_read_string( &r_check, 16, r_str ) == 0 );
62 TEST_ASSERT( mpi_read_string( &s_check, 16, s_str ) == 0 );
63 len = unhexify(buf, hash_str);
Manuel Pégourié-Gonnard602a8972013-01-27 08:10:28 +010064
65 TEST_ASSERT( ecdsa_sign( &grp, &r, &s, &d, buf, len,
Paul Bakker33b43f12013-08-20 11:48:36 +020066 &not_rnd, k_str ) == 0 );
Manuel Pégourié-Gonnard602a8972013-01-27 08:10:28 +010067
68 TEST_ASSERT( mpi_cmp_mpi( &r, &r_check ) == 0 );
69 TEST_ASSERT( mpi_cmp_mpi( &s, &s_check ) == 0 );
70
71 TEST_ASSERT( ecdsa_verify( &grp, buf, len, &Q, &r_check, &s_check ) == 0 );
72
73 ecp_group_free( &grp );
74 ecp_point_free( &Q );
75 mpi_free( &d ); mpi_free( &r ); mpi_free( &s );
76 mpi_free( &r_check ); mpi_free( &s_check );
77}
Paul Bakker33b43f12013-08-20 11:48:36 +020078/* END_CASE */