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