mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
williequesada
Date:
Tue Jun 04 16:03:38 2019 +0000
Revision:
1:1a219dea6cb5
Parent:
0:cdf462088d13
compartir a Pablo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/ecdsa.h"
markrad 0:cdf462088d13 3 /* END_HEADER */
markrad 0:cdf462088d13 4
markrad 0:cdf462088d13 5 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 6 * depends_on:MBEDTLS_ECDSA_C
markrad 0:cdf462088d13 7 * END_DEPENDENCIES
markrad 0:cdf462088d13 8 */
markrad 0:cdf462088d13 9
markrad 0:cdf462088d13 10 /* BEGIN_CASE */
markrad 0:cdf462088d13 11 void ecdsa_prim_random( int id )
markrad 0:cdf462088d13 12 {
markrad 0:cdf462088d13 13 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 14 mbedtls_ecp_point Q;
markrad 0:cdf462088d13 15 mbedtls_mpi d, r, s;
markrad 0:cdf462088d13 16 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 17 unsigned char buf[66];
markrad 0:cdf462088d13 18
markrad 0:cdf462088d13 19 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 20 mbedtls_ecp_point_init( &Q );
markrad 0:cdf462088d13 21 mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
markrad 0:cdf462088d13 22 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 23 memset( buf, 0, sizeof( buf ) );
markrad 0:cdf462088d13 24
markrad 0:cdf462088d13 25 /* prepare material for signature */
markrad 0:cdf462088d13 26 TEST_ASSERT( rnd_pseudo_rand( &rnd_info, buf, sizeof( buf ) ) == 0 );
markrad 0:cdf462088d13 27 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 28 TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
markrad 0:cdf462088d13 29 == 0 );
markrad 0:cdf462088d13 30
markrad 0:cdf462088d13 31 TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, buf, sizeof( buf ),
markrad 0:cdf462088d13 32 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 33 TEST_ASSERT( mbedtls_ecdsa_verify( &grp, buf, sizeof( buf ), &Q, &r, &s ) == 0 );
markrad 0:cdf462088d13 34
markrad 0:cdf462088d13 35 exit:
markrad 0:cdf462088d13 36 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 37 mbedtls_ecp_point_free( &Q );
markrad 0:cdf462088d13 38 mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
markrad 0:cdf462088d13 39 }
markrad 0:cdf462088d13 40 /* END_CASE */
markrad 0:cdf462088d13 41
markrad 0:cdf462088d13 42 /* BEGIN_CASE */
markrad 0:cdf462088d13 43 void ecdsa_prim_test_vectors( int id, char *d_str, char *xQ_str, char *yQ_str,
markrad 0:cdf462088d13 44 char *k_str, char *hash_str, char *r_str,
markrad 0:cdf462088d13 45 char *s_str )
markrad 0:cdf462088d13 46 {
markrad 0:cdf462088d13 47 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 48 mbedtls_ecp_point Q;
markrad 0:cdf462088d13 49 mbedtls_mpi d, r, s, r_check, s_check;
markrad 0:cdf462088d13 50 unsigned char hash[66], rnd_buf[66];
markrad 0:cdf462088d13 51 size_t hlen;
markrad 0:cdf462088d13 52 rnd_buf_info rnd_info;
markrad 0:cdf462088d13 53
markrad 0:cdf462088d13 54 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 55 mbedtls_ecp_point_init( &Q );
markrad 0:cdf462088d13 56 mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
markrad 0:cdf462088d13 57 mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check );
markrad 0:cdf462088d13 58 memset( hash, 0, sizeof( hash ) );
markrad 0:cdf462088d13 59 memset( rnd_buf, 0, sizeof( rnd_buf ) );
markrad 0:cdf462088d13 60
markrad 0:cdf462088d13 61 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 62 TEST_ASSERT( mbedtls_ecp_point_read_string( &Q, 16, xQ_str, yQ_str ) == 0 );
markrad 0:cdf462088d13 63 TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, d_str ) == 0 );
markrad 0:cdf462088d13 64 TEST_ASSERT( mbedtls_mpi_read_string( &r_check, 16, r_str ) == 0 );
markrad 0:cdf462088d13 65 TEST_ASSERT( mbedtls_mpi_read_string( &s_check, 16, s_str ) == 0 );
markrad 0:cdf462088d13 66 hlen = unhexify(hash, hash_str);
markrad 0:cdf462088d13 67 rnd_info.buf = rnd_buf;
markrad 0:cdf462088d13 68 rnd_info.length = unhexify( rnd_buf, k_str );
markrad 0:cdf462088d13 69
markrad 0:cdf462088d13 70 /* Fix rnd_buf by shifting it left if necessary */
markrad 0:cdf462088d13 71 if( grp.nbits % 8 != 0 )
markrad 0:cdf462088d13 72 {
markrad 0:cdf462088d13 73 unsigned char shift = 8 - ( grp.nbits % 8 );
markrad 0:cdf462088d13 74 size_t i;
markrad 0:cdf462088d13 75
markrad 0:cdf462088d13 76 for( i = 0; i < rnd_info.length - 1; i++ )
markrad 0:cdf462088d13 77 rnd_buf[i] = rnd_buf[i] << shift | rnd_buf[i+1] >> ( 8 - shift );
markrad 0:cdf462088d13 78
markrad 0:cdf462088d13 79 rnd_buf[rnd_info.length-1] <<= shift;
markrad 0:cdf462088d13 80 }
markrad 0:cdf462088d13 81
markrad 0:cdf462088d13 82 TEST_ASSERT( mbedtls_ecdsa_sign( &grp, &r, &s, &d, hash, hlen,
markrad 0:cdf462088d13 83 rnd_buffer_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 84
markrad 0:cdf462088d13 85 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &r, &r_check ) == 0 );
markrad 0:cdf462088d13 86 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &s, &s_check ) == 0 );
markrad 0:cdf462088d13 87
markrad 0:cdf462088d13 88 TEST_ASSERT( mbedtls_ecdsa_verify( &grp, hash, hlen, &Q, &r_check, &s_check ) == 0 );
markrad 0:cdf462088d13 89
markrad 0:cdf462088d13 90 exit:
markrad 0:cdf462088d13 91 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 92 mbedtls_ecp_point_free( &Q );
markrad 0:cdf462088d13 93 mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
markrad 0:cdf462088d13 94 mbedtls_mpi_free( &r_check ); mbedtls_mpi_free( &s_check );
markrad 0:cdf462088d13 95 }
markrad 0:cdf462088d13 96 /* END_CASE */
markrad 0:cdf462088d13 97
markrad 0:cdf462088d13 98 /* BEGIN_CASE depends_on:MBEDTLS_ECDSA_DETERMINISTIC */
markrad 0:cdf462088d13 99 void ecdsa_det_test_vectors( int id, char *d_str, int md_alg,
markrad 0:cdf462088d13 100 char *msg, char *r_str, char *s_str )
markrad 0:cdf462088d13 101 {
markrad 0:cdf462088d13 102 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 103 mbedtls_mpi d, r, s, r_check, s_check;
markrad 0:cdf462088d13 104 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
markrad 0:cdf462088d13 105 size_t hlen;
markrad 0:cdf462088d13 106 const mbedtls_md_info_t *md_info;
markrad 0:cdf462088d13 107
markrad 0:cdf462088d13 108 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 109 mbedtls_mpi_init( &d ); mbedtls_mpi_init( &r ); mbedtls_mpi_init( &s );
markrad 0:cdf462088d13 110 mbedtls_mpi_init( &r_check ); mbedtls_mpi_init( &s_check );
markrad 0:cdf462088d13 111 memset( hash, 0, sizeof( hash ) );
markrad 0:cdf462088d13 112
markrad 0:cdf462088d13 113 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 114 TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, d_str ) == 0 );
markrad 0:cdf462088d13 115 TEST_ASSERT( mbedtls_mpi_read_string( &r_check, 16, r_str ) == 0 );
markrad 0:cdf462088d13 116 TEST_ASSERT( mbedtls_mpi_read_string( &s_check, 16, s_str ) == 0 );
markrad 0:cdf462088d13 117
markrad 0:cdf462088d13 118 md_info = mbedtls_md_info_from_type( md_alg );
markrad 0:cdf462088d13 119 TEST_ASSERT( md_info != NULL );
markrad 0:cdf462088d13 120 hlen = mbedtls_md_get_size( md_info );
markrad 0:cdf462088d13 121 mbedtls_md( md_info, (const unsigned char *) msg, strlen( msg ), hash );
markrad 0:cdf462088d13 122
markrad 0:cdf462088d13 123 TEST_ASSERT( mbedtls_ecdsa_sign_det( &grp, &r, &s, &d, hash, hlen, md_alg ) == 0 );
markrad 0:cdf462088d13 124
markrad 0:cdf462088d13 125 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &r, &r_check ) == 0 );
markrad 0:cdf462088d13 126 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &s, &s_check ) == 0 );
markrad 0:cdf462088d13 127
markrad 0:cdf462088d13 128 exit:
markrad 0:cdf462088d13 129 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 130 mbedtls_mpi_free( &d ); mbedtls_mpi_free( &r ); mbedtls_mpi_free( &s );
markrad 0:cdf462088d13 131 mbedtls_mpi_free( &r_check ); mbedtls_mpi_free( &s_check );
markrad 0:cdf462088d13 132 }
markrad 0:cdf462088d13 133 /* END_CASE */
markrad 0:cdf462088d13 134
markrad 0:cdf462088d13 135 /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
markrad 0:cdf462088d13 136 void ecdsa_write_read_random( int id )
markrad 0:cdf462088d13 137 {
markrad 0:cdf462088d13 138 mbedtls_ecdsa_context ctx;
markrad 0:cdf462088d13 139 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 140 unsigned char hash[32];
markrad 0:cdf462088d13 141 unsigned char sig[200];
markrad 0:cdf462088d13 142 size_t sig_len, i;
markrad 0:cdf462088d13 143
markrad 0:cdf462088d13 144 mbedtls_ecdsa_init( &ctx );
markrad 0:cdf462088d13 145 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 146 memset( hash, 0, sizeof( hash ) );
markrad 0:cdf462088d13 147 memset( sig, 0x2a, sizeof( sig ) );
markrad 0:cdf462088d13 148
markrad 0:cdf462088d13 149 /* prepare material for signature */
markrad 0:cdf462088d13 150 TEST_ASSERT( rnd_pseudo_rand( &rnd_info, hash, sizeof( hash ) ) == 0 );
markrad 0:cdf462088d13 151
markrad 0:cdf462088d13 152 /* generate signing key */
markrad 0:cdf462088d13 153 TEST_ASSERT( mbedtls_ecdsa_genkey( &ctx, id, &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 154
markrad 0:cdf462088d13 155 /* generate and write signature, then read and verify it */
markrad 0:cdf462088d13 156 TEST_ASSERT( mbedtls_ecdsa_write_signature( &ctx, MBEDTLS_MD_SHA256,
markrad 0:cdf462088d13 157 hash, sizeof( hash ),
markrad 0:cdf462088d13 158 sig, &sig_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 159 TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
markrad 0:cdf462088d13 160 sig, sig_len ) == 0 );
markrad 0:cdf462088d13 161
markrad 0:cdf462088d13 162 /* check we didn't write past the announced length */
markrad 0:cdf462088d13 163 for( i = sig_len; i < sizeof( sig ); i++ )
markrad 0:cdf462088d13 164 TEST_ASSERT( sig[i] == 0x2a );
markrad 0:cdf462088d13 165
markrad 0:cdf462088d13 166 /* try verification with invalid length */
markrad 0:cdf462088d13 167 TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
markrad 0:cdf462088d13 168 sig, sig_len - 1 ) != 0 );
markrad 0:cdf462088d13 169 TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
markrad 0:cdf462088d13 170 sig, sig_len + 1 ) != 0 );
markrad 0:cdf462088d13 171
markrad 0:cdf462088d13 172 /* try invalid sequence tag */
markrad 0:cdf462088d13 173 sig[0]++;
markrad 0:cdf462088d13 174 TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
markrad 0:cdf462088d13 175 sig, sig_len ) != 0 );
markrad 0:cdf462088d13 176 sig[0]--;
markrad 0:cdf462088d13 177
markrad 0:cdf462088d13 178 /* try modifying r */
markrad 0:cdf462088d13 179 sig[10]++;
markrad 0:cdf462088d13 180 TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
markrad 0:cdf462088d13 181 sig, sig_len ) != 0 );
markrad 0:cdf462088d13 182 sig[10]--;
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 /* try modifying s */
markrad 0:cdf462088d13 185 sig[sig_len - 1]++;
markrad 0:cdf462088d13 186 TEST_ASSERT( mbedtls_ecdsa_read_signature( &ctx, hash, sizeof( hash ),
markrad 0:cdf462088d13 187 sig, sig_len ) != 0 );
markrad 0:cdf462088d13 188 sig[sig_len - 1]--;
markrad 0:cdf462088d13 189
markrad 0:cdf462088d13 190 exit:
markrad 0:cdf462088d13 191 mbedtls_ecdsa_free( &ctx );
markrad 0:cdf462088d13 192 }
markrad 0:cdf462088d13 193 /* END_CASE */
markrad 0:cdf462088d13 194