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/ecp.h"
markrad 0:cdf462088d13 3
markrad 0:cdf462088d13 4 #define ECP_PF_UNKNOWN -1
markrad 0:cdf462088d13 5 /* END_HEADER */
markrad 0:cdf462088d13 6
markrad 0:cdf462088d13 7 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 8 * depends_on:MBEDTLS_ECP_C
markrad 0:cdf462088d13 9 * END_DEPENDENCIES
markrad 0:cdf462088d13 10 */
markrad 0:cdf462088d13 11
markrad 0:cdf462088d13 12 /* BEGIN_CASE */
markrad 0:cdf462088d13 13 void mbedtls_ecp_curve_info( int id, int tls_id, int size, char *name )
markrad 0:cdf462088d13 14 {
markrad 0:cdf462088d13 15 const mbedtls_ecp_curve_info *by_id, *by_tls, *by_name;
markrad 0:cdf462088d13 16
markrad 0:cdf462088d13 17 by_id = mbedtls_ecp_curve_info_from_grp_id( id );
markrad 0:cdf462088d13 18 by_tls = mbedtls_ecp_curve_info_from_tls_id( tls_id );
markrad 0:cdf462088d13 19 by_name = mbedtls_ecp_curve_info_from_name( name );
markrad 0:cdf462088d13 20 TEST_ASSERT( by_id != NULL );
markrad 0:cdf462088d13 21 TEST_ASSERT( by_tls != NULL );
markrad 0:cdf462088d13 22 TEST_ASSERT( by_name != NULL );
markrad 0:cdf462088d13 23
markrad 0:cdf462088d13 24 TEST_ASSERT( by_id == by_tls );
markrad 0:cdf462088d13 25 TEST_ASSERT( by_id == by_name );
markrad 0:cdf462088d13 26
markrad 0:cdf462088d13 27 TEST_ASSERT( by_id->bit_size == size );
markrad 0:cdf462088d13 28 }
markrad 0:cdf462088d13 29 /* END_CASE */
markrad 0:cdf462088d13 30
markrad 0:cdf462088d13 31 /* BEGIN_CASE */
markrad 0:cdf462088d13 32 void ecp_check_pub_mx( int grp_id, char *key_hex, int ret )
markrad 0:cdf462088d13 33 {
markrad 0:cdf462088d13 34 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 35 mbedtls_ecp_point P;
markrad 0:cdf462088d13 36
markrad 0:cdf462088d13 37 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 38 mbedtls_ecp_point_init( &P );
markrad 0:cdf462088d13 39
markrad 0:cdf462088d13 40 TEST_ASSERT( mbedtls_ecp_group_load( &grp, grp_id ) == 0 );
markrad 0:cdf462088d13 41
markrad 0:cdf462088d13 42 TEST_ASSERT( mbedtls_mpi_read_string( &P.X, 16, key_hex ) == 0 );
markrad 0:cdf462088d13 43 TEST_ASSERT( mbedtls_mpi_lset( &P.Z, 1 ) == 0 );
markrad 0:cdf462088d13 44
markrad 0:cdf462088d13 45 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &P ) == ret );
markrad 0:cdf462088d13 46
markrad 0:cdf462088d13 47 exit:
markrad 0:cdf462088d13 48 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 49 mbedtls_ecp_point_free( &P );
markrad 0:cdf462088d13 50 }
markrad 0:cdf462088d13 51 /* END_CASE */
markrad 0:cdf462088d13 52
markrad 0:cdf462088d13 53 /* BEGIN_CASE */
markrad 0:cdf462088d13 54 void ecp_test_vect( int id, char *dA_str, char *xA_str, char *yA_str,
markrad 0:cdf462088d13 55 char *dB_str, char *xB_str, char *yB_str, char *xZ_str,
markrad 0:cdf462088d13 56 char *yZ_str )
markrad 0:cdf462088d13 57 {
markrad 0:cdf462088d13 58 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 59 mbedtls_ecp_point R;
markrad 0:cdf462088d13 60 mbedtls_mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
markrad 0:cdf462088d13 61 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 62
markrad 0:cdf462088d13 63 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
markrad 0:cdf462088d13 64 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA ); mbedtls_mpi_init( &dB );
markrad 0:cdf462088d13 65 mbedtls_mpi_init( &xB ); mbedtls_mpi_init( &yB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ );
markrad 0:cdf462088d13 66 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 67
markrad 0:cdf462088d13 68 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 69
markrad 0:cdf462088d13 70 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
markrad 0:cdf462088d13 71
markrad 0:cdf462088d13 72 TEST_ASSERT( mbedtls_mpi_read_string( &dA, 16, dA_str ) == 0 );
markrad 0:cdf462088d13 73 TEST_ASSERT( mbedtls_mpi_read_string( &xA, 16, xA_str ) == 0 );
markrad 0:cdf462088d13 74 TEST_ASSERT( mbedtls_mpi_read_string( &yA, 16, yA_str ) == 0 );
markrad 0:cdf462088d13 75 TEST_ASSERT( mbedtls_mpi_read_string( &dB, 16, dB_str ) == 0 );
markrad 0:cdf462088d13 76 TEST_ASSERT( mbedtls_mpi_read_string( &xB, 16, xB_str ) == 0 );
markrad 0:cdf462088d13 77 TEST_ASSERT( mbedtls_mpi_read_string( &yB, 16, yB_str ) == 0 );
markrad 0:cdf462088d13 78 TEST_ASSERT( mbedtls_mpi_read_string( &xZ, 16, xZ_str ) == 0 );
markrad 0:cdf462088d13 79 TEST_ASSERT( mbedtls_mpi_read_string( &yZ, 16, yZ_str ) == 0 );
markrad 0:cdf462088d13 80
markrad 0:cdf462088d13 81 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G,
markrad 0:cdf462088d13 82 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 83 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
markrad 0:cdf462088d13 84 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 );
markrad 0:cdf462088d13 85 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
markrad 0:cdf462088d13 86 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
markrad 0:cdf462088d13 87 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
markrad 0:cdf462088d13 88 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
markrad 0:cdf462088d13 89 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
markrad 0:cdf462088d13 90
markrad 0:cdf462088d13 91 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
markrad 0:cdf462088d13 92 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 );
markrad 0:cdf462088d13 93 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yB ) == 0 );
markrad 0:cdf462088d13 94 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
markrad 0:cdf462088d13 95 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R,
markrad 0:cdf462088d13 96 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 97 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
markrad 0:cdf462088d13 98 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
markrad 0:cdf462088d13 99 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
markrad 0:cdf462088d13 100
markrad 0:cdf462088d13 101 exit:
markrad 0:cdf462088d13 102 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
markrad 0:cdf462088d13 103 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA ); mbedtls_mpi_free( &dB );
markrad 0:cdf462088d13 104 mbedtls_mpi_free( &xB ); mbedtls_mpi_free( &yB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ );
markrad 0:cdf462088d13 105 }
markrad 0:cdf462088d13 106 /* END_CASE */
markrad 0:cdf462088d13 107
markrad 0:cdf462088d13 108 /* BEGIN_CASE */
markrad 0:cdf462088d13 109 void ecp_test_vec_x( int id, char *dA_hex, char *xA_hex,
markrad 0:cdf462088d13 110 char *dB_hex, char *xB_hex, char *xS_hex )
markrad 0:cdf462088d13 111 {
markrad 0:cdf462088d13 112 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 113 mbedtls_ecp_point R;
markrad 0:cdf462088d13 114 mbedtls_mpi dA, xA, dB, xB, xS;
markrad 0:cdf462088d13 115 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 116
markrad 0:cdf462088d13 117 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
markrad 0:cdf462088d13 118 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA );
markrad 0:cdf462088d13 119 mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xB );
markrad 0:cdf462088d13 120 mbedtls_mpi_init( &xS );
markrad 0:cdf462088d13 121 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 122
markrad 0:cdf462088d13 123 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 124
markrad 0:cdf462088d13 125 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
markrad 0:cdf462088d13 126
markrad 0:cdf462088d13 127 TEST_ASSERT( mbedtls_mpi_read_string( &dA, 16, dA_hex ) == 0 );
markrad 0:cdf462088d13 128 TEST_ASSERT( mbedtls_mpi_read_string( &dB, 16, dB_hex ) == 0 );
markrad 0:cdf462088d13 129 TEST_ASSERT( mbedtls_mpi_read_string( &xA, 16, xA_hex ) == 0 );
markrad 0:cdf462088d13 130 TEST_ASSERT( mbedtls_mpi_read_string( &xB, 16, xB_hex ) == 0 );
markrad 0:cdf462088d13 131 TEST_ASSERT( mbedtls_mpi_read_string( &xS, 16, xS_hex ) == 0 );
markrad 0:cdf462088d13 132
markrad 0:cdf462088d13 133 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G,
markrad 0:cdf462088d13 134 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 135 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
markrad 0:cdf462088d13 136 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
markrad 0:cdf462088d13 137
markrad 0:cdf462088d13 138 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R,
markrad 0:cdf462088d13 139 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 140 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
markrad 0:cdf462088d13 141 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 );
markrad 0:cdf462088d13 142
markrad 0:cdf462088d13 143 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
markrad 0:cdf462088d13 144 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
markrad 0:cdf462088d13 145 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 );
markrad 0:cdf462088d13 146
markrad 0:cdf462088d13 147 TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R, NULL, NULL ) == 0 );
markrad 0:cdf462088d13 148 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
markrad 0:cdf462088d13 149 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 );
markrad 0:cdf462088d13 150
markrad 0:cdf462088d13 151 exit:
markrad 0:cdf462088d13 152 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
markrad 0:cdf462088d13 153 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA );
markrad 0:cdf462088d13 154 mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xB );
markrad 0:cdf462088d13 155 mbedtls_mpi_free( &xS );
markrad 0:cdf462088d13 156 }
markrad 0:cdf462088d13 157 /* END_CASE */
markrad 0:cdf462088d13 158
markrad 0:cdf462088d13 159 /* BEGIN_CASE */
markrad 0:cdf462088d13 160 void ecp_fast_mod( int id, char *N_str )
markrad 0:cdf462088d13 161 {
markrad 0:cdf462088d13 162 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 163 mbedtls_mpi N, R;
markrad 0:cdf462088d13 164
markrad 0:cdf462088d13 165 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &R );
markrad 0:cdf462088d13 166 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 167
markrad 0:cdf462088d13 168 TEST_ASSERT( mbedtls_mpi_read_string( &N, 16, N_str ) == 0 );
markrad 0:cdf462088d13 169 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 170 TEST_ASSERT( grp.modp != NULL );
markrad 0:cdf462088d13 171
markrad 0:cdf462088d13 172 /*
markrad 0:cdf462088d13 173 * Store correct result before we touch N
markrad 0:cdf462088d13 174 */
markrad 0:cdf462088d13 175 TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
markrad 0:cdf462088d13 176
markrad 0:cdf462088d13 177 TEST_ASSERT( grp.modp( &N ) == 0 );
markrad 0:cdf462088d13 178 TEST_ASSERT( mbedtls_mpi_bitlen( &N ) <= grp.pbits + 3 );
markrad 0:cdf462088d13 179
markrad 0:cdf462088d13 180 /*
markrad 0:cdf462088d13 181 * Use mod rather than addition/subtraction in case previous test fails
markrad 0:cdf462088d13 182 */
markrad 0:cdf462088d13 183 TEST_ASSERT( mbedtls_mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
markrad 0:cdf462088d13 184 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &R ) == 0 );
markrad 0:cdf462088d13 185
markrad 0:cdf462088d13 186 exit:
markrad 0:cdf462088d13 187 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &R );
markrad 0:cdf462088d13 188 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 189 }
markrad 0:cdf462088d13 190 /* END_CASE */
markrad 0:cdf462088d13 191
markrad 0:cdf462088d13 192 /* BEGIN_CASE */
markrad 0:cdf462088d13 193 void ecp_write_binary( int id, char *x, char *y, char *z, int format,
markrad 0:cdf462088d13 194 char *out, int blen, int ret )
markrad 0:cdf462088d13 195 {
markrad 0:cdf462088d13 196 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 197 mbedtls_ecp_point P;
markrad 0:cdf462088d13 198 unsigned char buf[256], str[512];
markrad 0:cdf462088d13 199 size_t olen;
markrad 0:cdf462088d13 200
markrad 0:cdf462088d13 201 memset( buf, 0, sizeof( buf ) );
markrad 0:cdf462088d13 202 memset( str, 0, sizeof( str ) );
markrad 0:cdf462088d13 203
markrad 0:cdf462088d13 204 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
markrad 0:cdf462088d13 205
markrad 0:cdf462088d13 206 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 207
markrad 0:cdf462088d13 208 TEST_ASSERT( mbedtls_mpi_read_string( &P.X, 16, x ) == 0 );
markrad 0:cdf462088d13 209 TEST_ASSERT( mbedtls_mpi_read_string( &P.Y, 16, y ) == 0 );
markrad 0:cdf462088d13 210 TEST_ASSERT( mbedtls_mpi_read_string( &P.Z, 16, z ) == 0 );
markrad 0:cdf462088d13 211
markrad 0:cdf462088d13 212 TEST_ASSERT( mbedtls_ecp_point_write_binary( &grp, &P, format,
markrad 0:cdf462088d13 213 &olen, buf, blen ) == ret );
markrad 0:cdf462088d13 214
markrad 0:cdf462088d13 215 if( ret == 0 )
markrad 0:cdf462088d13 216 {
markrad 0:cdf462088d13 217 hexify( str, buf, olen );
markrad 0:cdf462088d13 218 TEST_ASSERT( strcasecmp( (char *) str, out ) == 0 );
markrad 0:cdf462088d13 219 }
markrad 0:cdf462088d13 220
markrad 0:cdf462088d13 221 exit:
markrad 0:cdf462088d13 222 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
markrad 0:cdf462088d13 223 }
markrad 0:cdf462088d13 224 /* END_CASE */
markrad 0:cdf462088d13 225
markrad 0:cdf462088d13 226 /* BEGIN_CASE */
markrad 0:cdf462088d13 227 void ecp_read_binary( int id, char *input, char *x, char *y, char *z,
markrad 0:cdf462088d13 228 int ret )
markrad 0:cdf462088d13 229 {
markrad 0:cdf462088d13 230 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 231 mbedtls_ecp_point P;
markrad 0:cdf462088d13 232 mbedtls_mpi X, Y, Z;
markrad 0:cdf462088d13 233 int ilen;
markrad 0:cdf462088d13 234 unsigned char buf[256];
markrad 0:cdf462088d13 235
markrad 0:cdf462088d13 236 memset( buf, 0, sizeof( buf ) );
markrad 0:cdf462088d13 237
markrad 0:cdf462088d13 238 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
markrad 0:cdf462088d13 239 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
markrad 0:cdf462088d13 240
markrad 0:cdf462088d13 241 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 242
markrad 0:cdf462088d13 243 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x ) == 0 );
markrad 0:cdf462088d13 244 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y ) == 0 );
markrad 0:cdf462088d13 245 TEST_ASSERT( mbedtls_mpi_read_string( &Z, 16, z ) == 0 );
markrad 0:cdf462088d13 246
markrad 0:cdf462088d13 247 ilen = unhexify( buf, input );
markrad 0:cdf462088d13 248
markrad 0:cdf462088d13 249 TEST_ASSERT( mbedtls_ecp_point_read_binary( &grp, &P, buf, ilen ) == ret );
markrad 0:cdf462088d13 250
markrad 0:cdf462088d13 251 if( ret == 0 )
markrad 0:cdf462088d13 252 {
markrad 0:cdf462088d13 253 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 );
markrad 0:cdf462088d13 254 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 );
markrad 0:cdf462088d13 255 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 );
markrad 0:cdf462088d13 256 }
markrad 0:cdf462088d13 257
markrad 0:cdf462088d13 258 exit:
markrad 0:cdf462088d13 259 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
markrad 0:cdf462088d13 260 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
markrad 0:cdf462088d13 261 }
markrad 0:cdf462088d13 262 /* END_CASE */
markrad 0:cdf462088d13 263
markrad 0:cdf462088d13 264 /* BEGIN_CASE */
markrad 0:cdf462088d13 265 void mbedtls_ecp_tls_read_point( int id, char *input, char *x, char *y, char *z,
markrad 0:cdf462088d13 266 int ret )
markrad 0:cdf462088d13 267 {
markrad 0:cdf462088d13 268 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 269 mbedtls_ecp_point P;
markrad 0:cdf462088d13 270 mbedtls_mpi X, Y, Z;
markrad 0:cdf462088d13 271 size_t ilen;
markrad 0:cdf462088d13 272 unsigned char buf[256];
markrad 0:cdf462088d13 273 const unsigned char *vbuf = buf;
markrad 0:cdf462088d13 274
markrad 0:cdf462088d13 275 memset( buf, 0, sizeof( buf ) );
markrad 0:cdf462088d13 276
markrad 0:cdf462088d13 277 mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
markrad 0:cdf462088d13 278 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
markrad 0:cdf462088d13 279
markrad 0:cdf462088d13 280 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 281
markrad 0:cdf462088d13 282 TEST_ASSERT( mbedtls_mpi_read_string( &X, 16, x ) == 0 );
markrad 0:cdf462088d13 283 TEST_ASSERT( mbedtls_mpi_read_string( &Y, 16, y ) == 0 );
markrad 0:cdf462088d13 284 TEST_ASSERT( mbedtls_mpi_read_string( &Z, 16, z ) == 0 );
markrad 0:cdf462088d13 285
markrad 0:cdf462088d13 286 ilen = unhexify( buf, input );
markrad 0:cdf462088d13 287
markrad 0:cdf462088d13 288 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == ret );
markrad 0:cdf462088d13 289
markrad 0:cdf462088d13 290 if( ret == 0 )
markrad 0:cdf462088d13 291 {
markrad 0:cdf462088d13 292 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 );
markrad 0:cdf462088d13 293 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 );
markrad 0:cdf462088d13 294 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 );
markrad 0:cdf462088d13 295 TEST_ASSERT( *vbuf == 0x00 );
markrad 0:cdf462088d13 296 }
markrad 0:cdf462088d13 297
markrad 0:cdf462088d13 298 exit:
markrad 0:cdf462088d13 299 mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
markrad 0:cdf462088d13 300 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
markrad 0:cdf462088d13 301 }
markrad 0:cdf462088d13 302 /* END_CASE */
markrad 0:cdf462088d13 303
markrad 0:cdf462088d13 304 /* BEGIN_CASE */
markrad 0:cdf462088d13 305 void ecp_tls_write_read_point( int id )
markrad 0:cdf462088d13 306 {
markrad 0:cdf462088d13 307 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 308 mbedtls_ecp_point pt;
markrad 0:cdf462088d13 309 unsigned char buf[256];
markrad 0:cdf462088d13 310 const unsigned char *vbuf;
markrad 0:cdf462088d13 311 size_t olen;
markrad 0:cdf462088d13 312
markrad 0:cdf462088d13 313 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 314 mbedtls_ecp_point_init( &pt );
markrad 0:cdf462088d13 315
markrad 0:cdf462088d13 316 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 317
markrad 0:cdf462088d13 318 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
markrad 0:cdf462088d13 319 TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G,
markrad 0:cdf462088d13 320 MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
markrad 0:cdf462088d13 321 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen )
markrad 0:cdf462088d13 322 == MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 323 TEST_ASSERT( vbuf == buf + olen );
markrad 0:cdf462088d13 324
markrad 0:cdf462088d13 325 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
markrad 0:cdf462088d13 326 TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G,
markrad 0:cdf462088d13 327 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
markrad 0:cdf462088d13 328 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
markrad 0:cdf462088d13 329 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
markrad 0:cdf462088d13 330 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
markrad 0:cdf462088d13 331 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
markrad 0:cdf462088d13 332 TEST_ASSERT( vbuf == buf + olen );
markrad 0:cdf462088d13 333
markrad 0:cdf462088d13 334 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
markrad 0:cdf462088d13 335 TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 );
markrad 0:cdf462088d13 336 TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt,
markrad 0:cdf462088d13 337 MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
markrad 0:cdf462088d13 338 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
markrad 0:cdf462088d13 339 TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) );
markrad 0:cdf462088d13 340 TEST_ASSERT( vbuf == buf + olen );
markrad 0:cdf462088d13 341
markrad 0:cdf462088d13 342 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
markrad 0:cdf462088d13 343 TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 );
markrad 0:cdf462088d13 344 TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt,
markrad 0:cdf462088d13 345 MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
markrad 0:cdf462088d13 346 TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
markrad 0:cdf462088d13 347 TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) );
markrad 0:cdf462088d13 348 TEST_ASSERT( vbuf == buf + olen );
markrad 0:cdf462088d13 349
markrad 0:cdf462088d13 350 exit:
markrad 0:cdf462088d13 351 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 352 mbedtls_ecp_point_free( &pt );
markrad 0:cdf462088d13 353 }
markrad 0:cdf462088d13 354 /* END_CASE */
markrad 0:cdf462088d13 355
markrad 0:cdf462088d13 356 /* BEGIN_CASE */
markrad 0:cdf462088d13 357 void mbedtls_ecp_tls_read_group( char *record, int result, int bits )
markrad 0:cdf462088d13 358 {
markrad 0:cdf462088d13 359 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 360 unsigned char buf[10];
markrad 0:cdf462088d13 361 const unsigned char *vbuf = buf;
markrad 0:cdf462088d13 362 int len, ret;
markrad 0:cdf462088d13 363
markrad 0:cdf462088d13 364 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 365 memset( buf, 0x00, sizeof( buf ) );
markrad 0:cdf462088d13 366
markrad 0:cdf462088d13 367 len = unhexify( buf, record );
markrad 0:cdf462088d13 368
markrad 0:cdf462088d13 369 ret = mbedtls_ecp_tls_read_group( &grp, &vbuf, len );
markrad 0:cdf462088d13 370
markrad 0:cdf462088d13 371 TEST_ASSERT( ret == result );
markrad 0:cdf462088d13 372 if( ret == 0)
markrad 0:cdf462088d13 373 {
markrad 0:cdf462088d13 374 TEST_ASSERT( mbedtls_mpi_bitlen( &grp.P ) == (size_t) bits );
markrad 0:cdf462088d13 375 TEST_ASSERT( *vbuf == 0x00 );
markrad 0:cdf462088d13 376 }
markrad 0:cdf462088d13 377
markrad 0:cdf462088d13 378 exit:
markrad 0:cdf462088d13 379 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 380 }
markrad 0:cdf462088d13 381 /* END_CASE */
markrad 0:cdf462088d13 382
markrad 0:cdf462088d13 383 /* BEGIN_CASE */
markrad 0:cdf462088d13 384 void ecp_tls_write_read_group( int id )
markrad 0:cdf462088d13 385 {
markrad 0:cdf462088d13 386 mbedtls_ecp_group grp1, grp2;
markrad 0:cdf462088d13 387 unsigned char buf[10];
markrad 0:cdf462088d13 388 const unsigned char *vbuf = buf;
markrad 0:cdf462088d13 389 size_t len;
markrad 0:cdf462088d13 390 int ret;
markrad 0:cdf462088d13 391
markrad 0:cdf462088d13 392 mbedtls_ecp_group_init( &grp1 );
markrad 0:cdf462088d13 393 mbedtls_ecp_group_init( &grp2 );
markrad 0:cdf462088d13 394 memset( buf, 0x00, sizeof( buf ) );
markrad 0:cdf462088d13 395
markrad 0:cdf462088d13 396 TEST_ASSERT( mbedtls_ecp_group_load( &grp1, id ) == 0 );
markrad 0:cdf462088d13 397
markrad 0:cdf462088d13 398 TEST_ASSERT( mbedtls_ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
markrad 0:cdf462088d13 399 ret = mbedtls_ecp_tls_read_group( &grp2, &vbuf, len );
markrad 0:cdf462088d13 400 TEST_ASSERT( ret == 0 );
markrad 0:cdf462088d13 401
markrad 0:cdf462088d13 402 if( ret == 0 )
markrad 0:cdf462088d13 403 {
markrad 0:cdf462088d13 404 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
markrad 0:cdf462088d13 405 TEST_ASSERT( grp1.id == grp2.id );
markrad 0:cdf462088d13 406 }
markrad 0:cdf462088d13 407
markrad 0:cdf462088d13 408 exit:
markrad 0:cdf462088d13 409 mbedtls_ecp_group_free( &grp1 );
markrad 0:cdf462088d13 410 mbedtls_ecp_group_free( &grp2 );
markrad 0:cdf462088d13 411 }
markrad 0:cdf462088d13 412 /* END_CASE */
markrad 0:cdf462088d13 413
markrad 0:cdf462088d13 414 /* BEGIN_CASE */
markrad 0:cdf462088d13 415 void mbedtls_ecp_check_privkey( int id, char *key_hex, int ret )
markrad 0:cdf462088d13 416 {
markrad 0:cdf462088d13 417 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 418 mbedtls_mpi d;
markrad 0:cdf462088d13 419
markrad 0:cdf462088d13 420 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 421 mbedtls_mpi_init( &d );
markrad 0:cdf462088d13 422
markrad 0:cdf462088d13 423 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 424 TEST_ASSERT( mbedtls_mpi_read_string( &d, 16, key_hex ) == 0 );
markrad 0:cdf462088d13 425
markrad 0:cdf462088d13 426 TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == ret );
markrad 0:cdf462088d13 427
markrad 0:cdf462088d13 428 exit:
markrad 0:cdf462088d13 429 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 430 mbedtls_mpi_free( &d );
markrad 0:cdf462088d13 431 }
markrad 0:cdf462088d13 432 /* END_CASE */
markrad 0:cdf462088d13 433
markrad 0:cdf462088d13 434 /* BEGIN_CASE */
markrad 0:cdf462088d13 435 void mbedtls_ecp_check_pub_priv( int id_pub, char *Qx_pub, char *Qy_pub,
markrad 0:cdf462088d13 436 int id, char *d, char *Qx, char *Qy, int ret )
markrad 0:cdf462088d13 437 {
markrad 0:cdf462088d13 438 mbedtls_ecp_keypair pub, prv;
markrad 0:cdf462088d13 439
markrad 0:cdf462088d13 440 mbedtls_ecp_keypair_init( &pub );
markrad 0:cdf462088d13 441 mbedtls_ecp_keypair_init( &prv );
markrad 0:cdf462088d13 442
markrad 0:cdf462088d13 443 if( id_pub != MBEDTLS_ECP_DP_NONE )
markrad 0:cdf462088d13 444 TEST_ASSERT( mbedtls_ecp_group_load( &pub.grp, id_pub ) == 0 );
markrad 0:cdf462088d13 445 TEST_ASSERT( mbedtls_ecp_point_read_string( &pub.Q, 16, Qx_pub, Qy_pub ) == 0 );
markrad 0:cdf462088d13 446
markrad 0:cdf462088d13 447 if( id != MBEDTLS_ECP_DP_NONE )
markrad 0:cdf462088d13 448 TEST_ASSERT( mbedtls_ecp_group_load( &prv.grp, id ) == 0 );
markrad 0:cdf462088d13 449 TEST_ASSERT( mbedtls_ecp_point_read_string( &prv.Q, 16, Qx, Qy ) == 0 );
markrad 0:cdf462088d13 450 TEST_ASSERT( mbedtls_mpi_read_string( &prv.d, 16, d ) == 0 );
markrad 0:cdf462088d13 451
markrad 0:cdf462088d13 452 TEST_ASSERT( mbedtls_ecp_check_pub_priv( &pub, &prv ) == ret );
markrad 0:cdf462088d13 453
markrad 0:cdf462088d13 454 exit:
markrad 0:cdf462088d13 455 mbedtls_ecp_keypair_free( &pub );
markrad 0:cdf462088d13 456 mbedtls_ecp_keypair_free( &prv );
markrad 0:cdf462088d13 457 }
markrad 0:cdf462088d13 458 /* END_CASE */
markrad 0:cdf462088d13 459
markrad 0:cdf462088d13 460 /* BEGIN_CASE */
markrad 0:cdf462088d13 461 void mbedtls_ecp_gen_keypair( int id )
markrad 0:cdf462088d13 462 {
markrad 0:cdf462088d13 463 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 464 mbedtls_ecp_point Q;
markrad 0:cdf462088d13 465 mbedtls_mpi d;
markrad 0:cdf462088d13 466 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 467
markrad 0:cdf462088d13 468 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 469 mbedtls_ecp_point_init( &Q );
markrad 0:cdf462088d13 470 mbedtls_mpi_init( &d );
markrad 0:cdf462088d13 471 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 472
markrad 0:cdf462088d13 473 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 474
markrad 0:cdf462088d13 475 TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
markrad 0:cdf462088d13 476 == 0 );
markrad 0:cdf462088d13 477
markrad 0:cdf462088d13 478 TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &Q ) == 0 );
markrad 0:cdf462088d13 479 TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == 0 );
markrad 0:cdf462088d13 480
markrad 0:cdf462088d13 481 exit:
markrad 0:cdf462088d13 482 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 483 mbedtls_ecp_point_free( &Q );
markrad 0:cdf462088d13 484 mbedtls_mpi_free( &d );
markrad 0:cdf462088d13 485 }
markrad 0:cdf462088d13 486 /* END_CASE */
markrad 0:cdf462088d13 487
markrad 0:cdf462088d13 488 /* BEGIN_CASE */
markrad 0:cdf462088d13 489 void mbedtls_ecp_gen_key( int id )
markrad 0:cdf462088d13 490 {
markrad 0:cdf462088d13 491 mbedtls_ecp_keypair key;
markrad 0:cdf462088d13 492 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 493
markrad 0:cdf462088d13 494 mbedtls_ecp_keypair_init( &key );
markrad 0:cdf462088d13 495 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 496
markrad 0:cdf462088d13 497 TEST_ASSERT( mbedtls_ecp_gen_key( id, &key, &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 498
markrad 0:cdf462088d13 499 TEST_ASSERT( mbedtls_ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
markrad 0:cdf462088d13 500 TEST_ASSERT( mbedtls_ecp_check_privkey( &key.grp, &key.d ) == 0 );
markrad 0:cdf462088d13 501
markrad 0:cdf462088d13 502 exit:
markrad 0:cdf462088d13 503 mbedtls_ecp_keypair_free( &key );
markrad 0:cdf462088d13 504 }
markrad 0:cdf462088d13 505 /* END_CASE */
markrad 0:cdf462088d13 506
markrad 0:cdf462088d13 507 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 508 void ecp_selftest()
markrad 0:cdf462088d13 509 {
markrad 0:cdf462088d13 510 TEST_ASSERT( mbedtls_ecp_self_test( 1 ) == 0 );
markrad 0:cdf462088d13 511 }
markrad 0:cdf462088d13 512 /* END_CASE */