| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ | 
| Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 2 | #include <polarssl/ecdsa.h> | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 3 | /* END_HEADER */ | 
| Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 4 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 5 | /* BEGIN_DEPENDENCIES | 
| Manuel Pégourié-Gonnard | a0f0747 | 2013-08-23 16:35:32 +0200 | [diff] [blame] | 6 | * depends_on:POLARSSL_ECDSA_C | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 7 | * END_DEPENDENCIES | 
|  | 8 | */ | 
| Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 9 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 10 | /* BEGIN_CASE */ | 
|  | 11 | void ecdsa_prim_random( int id ) | 
| Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 12 | { | 
|  | 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é-Gonnard | 450a163 | 2013-01-27 09:08:18 +0100 | [diff] [blame] | 23 | memset( buf, 0, sizeof( buf ) ); | 
| Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 24 |  | 
|  | 25 | /* prepare material for signature */ | 
|  | 26 | TEST_ASSERT( rnd_pseudo_rand( &rnd_info, buf, sizeof( buf ) ) == 0 ); | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 27 | TEST_ASSERT( ecp_use_known_dp( &grp, id ) == 0 ); | 
| Manuel Pégourié-Gonnard | d1c7150 | 2013-01-26 19:09:07 +0100 | [diff] [blame] | 28 | 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 Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 39 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 40 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 41 | /* BEGIN_CASE */ | 
|  | 42 | void 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é-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 45 | { | 
|  | 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é-Gonnard | 450a163 | 2013-01-27 09:08:18 +0100 | [diff] [blame] | 56 | memset( buf, 0, sizeof( buf ) ); | 
| Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 57 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 58 | 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é-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 64 |  | 
|  | 65 | TEST_ASSERT( ecdsa_sign( &grp, &r, &s, &d, buf, len, | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 66 | ¬_rnd, k_str ) == 0 ); | 
| Manuel Pégourié-Gonnard | 602a897 | 2013-01-27 08:10:28 +0100 | [diff] [blame] | 67 |  | 
|  | 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 Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 78 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 79 |  | 
|  | 80 | /* BEGIN_CASE */ | 
|  | 81 | void ecdsa_write_read_random( int id ) | 
|  | 82 | { | 
|  | 83 | ecdsa_context ctx; | 
|  | 84 | rnd_pseudo_info rnd_info; | 
|  | 85 | unsigned char hash[66]; | 
|  | 86 | unsigned char sig[200]; | 
|  | 87 | size_t sig_len, i; | 
|  | 88 |  | 
|  | 89 | ecdsa_init( &ctx ); | 
|  | 90 | memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); | 
|  | 91 | memset( hash, 0, sizeof( hash ) ); | 
|  | 92 | memset( sig, 0x2a, sizeof( sig ) ); | 
|  | 93 |  | 
|  | 94 | /* prepare material for signature */ | 
|  | 95 | TEST_ASSERT( rnd_pseudo_rand( &rnd_info, hash, sizeof( hash ) ) == 0 ); | 
|  | 96 |  | 
|  | 97 | /* generate signing key */ | 
| Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 98 | TEST_ASSERT( ecdsa_genkey( &ctx, id, &rnd_pseudo_rand, &rnd_info ) == 0 ); | 
| Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 99 |  | 
|  | 100 | /* generate and write signature, then read and verify it */ | 
|  | 101 | TEST_ASSERT( ecdsa_write_signature( &ctx, hash, sizeof( hash ), | 
|  | 102 | sig, &sig_len, &rnd_pseudo_rand, &rnd_info ) == 0 ); | 
|  | 103 | TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ), | 
|  | 104 | sig, sig_len ) == 0 ); | 
|  | 105 |  | 
|  | 106 | /* check we didn't write past the announced length */ | 
|  | 107 | for( i = sig_len; i < sizeof( sig ); i++ ) | 
|  | 108 | TEST_ASSERT( sig[i] == 0x2a ); | 
|  | 109 |  | 
|  | 110 | /* try verification with invalid length */ | 
|  | 111 | TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ), | 
|  | 112 | sig, sig_len - 1 ) != 0 ); | 
|  | 113 | TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ), | 
|  | 114 | sig, sig_len + 1 ) != 0 ); | 
|  | 115 |  | 
|  | 116 | /* try invalid sequence tag */ | 
|  | 117 | sig[0]++; | 
|  | 118 | TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ), | 
|  | 119 | sig, sig_len ) != 0 ); | 
|  | 120 | sig[0]--; | 
|  | 121 |  | 
|  | 122 | /* try modifying r */ | 
|  | 123 | sig[10]++; | 
|  | 124 | TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ), | 
|  | 125 | sig, sig_len ) != 0 ); | 
|  | 126 | sig[10]--; | 
|  | 127 |  | 
|  | 128 | /* try modifying s */ | 
|  | 129 | sig[sig_len - 1]++; | 
|  | 130 | TEST_ASSERT( ecdsa_read_signature( &ctx, hash, sizeof( hash ), | 
|  | 131 | sig, sig_len ) != 0 ); | 
|  | 132 | sig[sig_len - 1]--; | 
|  | 133 |  | 
|  | 134 | ecdsa_free( &ctx ); | 
|  | 135 | } | 
|  | 136 | /* END_CASE */ |