test_suite_ecp.function 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
BEGIN_HEADER
#include <polarssl/ecp.h>
END_HEADER

BEGIN_DEPENDENCIES
depends_on:POLARSSL_ECP_C:POLARSSL_BIGNUM_C
END_DEPENDENCIES

BEGIN_CASE
ecp_small_add:a_zero:x_a:y_a:b_zero:x_b:y_b:c_zero:x_c:y_c
{
    ecp_group grp;
    ecp_point A, B, C;

    ecp_group_init( &grp );
    ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );

    TEST_ASSERT( ecp_group_read_string( &grp, 10,
                "47", "4", "17", "42", "13" ) == 0 );

    if( {a_zero} )
        ecp_set_zero( &A );
    else
        TEST_ASSERT( ecp_point_read_string( &A, 10, {x_a}, {y_a} ) == 0 );

    if( {b_zero} )
        ecp_set_zero( &B );
    else
        TEST_ASSERT( ecp_point_read_string( &B, 10, {x_b}, {y_b} ) == 0 );

    TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 );

    if( {c_zero} )
34
        TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
35 36 37 38 39 40 41 42 43
    else
    {
        TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
        TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
    }

    TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 );

    if( {c_zero} )
44
        TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
45 46 47 48 49 50 51 52 53 54 55
    else
    {
        TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
        TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
    }

    ecp_group_free( &grp );
    ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
}
END_CASE

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
BEGIN_CASE
ecp_small_sub:a_zero:x_a:y_a:b_zero:x_b:y_b:c_zero:x_c:y_c
{
    ecp_group grp;
    ecp_point A, B, C;

    ecp_group_init( &grp );
    ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C );

    TEST_ASSERT( ecp_group_read_string( &grp, 10,
                "47", "4", "17", "42", "13" ) == 0 );

    if( {a_zero} )
        ecp_set_zero( &A );
    else
        TEST_ASSERT( ecp_point_read_string( &A, 10, {x_a}, {y_a} ) == 0 );

    if( {b_zero} )
        ecp_set_zero( &B );
    else
        TEST_ASSERT( ecp_point_read_string( &B, 10, {x_b}, {y_b} ) == 0 );

    TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 );

    if( {c_zero} )
        TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 );
    else
    {
        TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 );
        TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 );
    }

    ecp_group_free( &grp );
    ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C );
}
END_CASE

93
BEGIN_CASE
94
ecp_small_mul:m:r_zero:x_r:y_r:ret
95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
    ecp_group grp;
    ecp_point R;
    mpi m;

    ecp_group_init( &grp );
    ecp_point_init( &R );
    mpi_init( &m );

    TEST_ASSERT( ecp_group_read_string( &grp, 10,
                "47", "4", "17", "42", "13" ) == 0 );

    TEST_ASSERT( mpi_lset( &m, {m} ) == 0 );

109
    TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G ) == {ret} );
110 111

    if( {r_zero} )
112
        TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 );
113 114 115 116 117 118 119 120 121 122 123 124
    else
    {
        TEST_ASSERT( mpi_cmp_int( &R.X, {x_r} ) == 0 );
        TEST_ASSERT( mpi_cmp_int( &R.Y, {y_r} ) == 0 );
    }

    ecp_group_free( &grp );
    ecp_point_free( &R );
    mpi_free( &m );
}
END_CASE

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
BEGIN_CASE
ecp_small_check_pub:x:y:z:ret
{
    ecp_group grp;
    ecp_point P;

    ecp_group_init( &grp );
    ecp_point_init( &P );

    TEST_ASSERT( ecp_group_read_string( &grp, 10,
                "47", "4", "17", "42", "13" ) == 0 );

    TEST_ASSERT( mpi_lset( &P.X, {x} ) == 0 );
    TEST_ASSERT( mpi_lset( &P.Y, {y} ) == 0 );
    TEST_ASSERT( mpi_lset( &P.Z, {z} ) == 0 );

    TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == {ret} );

    ecp_group_free( &grp );
    ecp_point_free( &P );
}
END_CASE

148
BEGIN_CASE
149
ecp_test_vect:id:dA:xA:yA:dB:xB:yB:xZ:yZ
150 151
{
    ecp_group grp;
152 153
    ecp_point R;
    mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
154

155 156 157
    ecp_group_init( &grp ); ecp_point_init( &R );
    mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB );
    mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ );
158 159 160

    TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );

161 162
    TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 );

163 164 165 166 167 168 169 170 171 172 173 174
    TEST_ASSERT( mpi_read_string( &dA, 16, {dA} ) == 0 );
    TEST_ASSERT( mpi_read_string( &xA, 16, {xA} ) == 0 );
    TEST_ASSERT( mpi_read_string( &yA, 16, {yA} ) == 0 );
    TEST_ASSERT( mpi_read_string( &dB, 16, {dB} ) == 0 );
    TEST_ASSERT( mpi_read_string( &xB, 16, {xB} ) == 0 );
    TEST_ASSERT( mpi_read_string( &yB, 16, {yB} ) == 0 );
    TEST_ASSERT( mpi_read_string( &xZ, 16, {xZ} ) == 0 );
    TEST_ASSERT( mpi_read_string( &yZ, 16, {yZ} ) == 0 );

    TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 );
175
    TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
176 177 178
    TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
179
    TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
180 181 182 183

    TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 );
184
    TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
185 186 187
    TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
188
    TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 );
189 190 191 192

    ecp_group_free( &grp ); ecp_point_free( &R );
    mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB );
    mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ );
193 194
}
END_CASE
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

BEGIN_CASE
ecp_fast_mod:id:N
{
    ecp_group grp;
    mpi N, R;

    mpi_init( &N ); mpi_init( &R );
    ecp_group_init( &grp );

    TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );
    TEST_ASSERT( mpi_read_string( &N, 16, {N} ) == 0 );

    /*
     * Store correct result before we touch N
     */
    TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 );

    TEST_ASSERT( grp.modp( &N ) == 0 );
    TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 );

    /*
     * Use mod rather than addition/substraction in case previous test fails
     */
    TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 );

    mpi_free( &N ); mpi_free( &R );
    ecp_group_free( &grp );
}
END_CASE
226

227
BEGIN_CASE
228
ecp_write_binary:id:x:y:z:format:out:blen:ret
229 230 231 232
{
    ecp_group grp;
    ecp_point P;
    unsigned char buf[256], str[512];
233
    size_t olen;
234 235 236 237 238 239 240 241 242 243 244 245

    memset( buf, 0, sizeof( buf ) );
    memset( str, 0, sizeof( str ) );

    ecp_group_init( &grp ); ecp_point_init( &P );

    TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );

    TEST_ASSERT( mpi_read_string( &P.X, 16, {x} ) == 0 );
    TEST_ASSERT( mpi_read_string( &P.Y, 16, {y} ) == 0 );
    TEST_ASSERT( mpi_read_string( &P.Z, 16, {z} ) == 0 );

246
#define POLARSSL_ECP_PF_UNKNOWN     -1
247
    TEST_ASSERT( ecp_point_write_binary( &grp, &P, POLARSSL_ECP_PF_{format},
248
                                   &olen, buf, {blen} ) == {ret} );
249 250 251

    if( {ret} == 0 )
    {
252
        hexify( str, buf, olen );
253 254 255 256 257 258 259
        TEST_ASSERT( strcasecmp( (char *) str, {out} ) == 0 );
    }

    ecp_group_free( &grp ); ecp_point_free( &P );
}
END_CASE

260
BEGIN_CASE
261
ecp_read_binary:id:input:x:y:z:ret
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
{
    ecp_group grp;
    ecp_point P;
    mpi X, Y, Z;
    int ilen;
    unsigned char buf[256];

    memset( buf, 0, sizeof( buf ) );

    ecp_group_init( &grp ); ecp_point_init( &P );
    mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );

    TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );

    TEST_ASSERT( mpi_read_string( &X, 16, {x} ) == 0 );
    TEST_ASSERT( mpi_read_string( &Y, 16, {y} ) == 0 );
    TEST_ASSERT( mpi_read_string( &Z, 16, {z} ) == 0 );

    ilen = unhexify( buf, {input} );

282
    TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == {ret} );
283 284 285 286 287 288 289 290 291 292 293 294 295

    if( {ret} == 0 )
    {
        TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
        TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
        TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
    }

    ecp_group_free( &grp ); ecp_point_free( &P );
    mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
}
END_CASE

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
BEGIN_CASE
ecp_tls_read_point:id:input:x:y:z:ret
{
    ecp_group grp;
    ecp_point P;
    mpi X, Y, Z;
    size_t ilen;
    unsigned char buf[256];

    memset( buf, 0, sizeof( buf ) );

    ecp_group_init( &grp ); ecp_point_init( &P );
    mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );

    TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );

    TEST_ASSERT( mpi_read_string( &X, 16, {x} ) == 0 );
    TEST_ASSERT( mpi_read_string( &Y, 16, {y} ) == 0 );
    TEST_ASSERT( mpi_read_string( &Z, 16, {z} ) == 0 );

    ilen = unhexify( buf, {input} );

    TEST_ASSERT( ecp_tls_read_point( &grp, &P, buf, ilen ) == {ret} );

    if( {ret} == 0 )
    {
        TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 );
        TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 );
        TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 );
    }

    ecp_group_free( &grp ); ecp_point_free( &P );
    mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
}
END_CASE

332
BEGIN_CASE
333
ecp_tls_write_read_point:id
334 335
{
    ecp_group grp;
336 337
    ecp_point pt;
    unsigned char buf[256];
338
    size_t olen;
339 340

    ecp_group_init( &grp );
341 342
    ecp_point_init( &pt );
    memset( buf, 0x00, sizeof( buf ) );
343 344 345

    TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );

346
    TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
347 348
                    POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
    TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, olen )
349 350 351
                 == POLARSSL_ERR_ECP_BAD_INPUT_DATA );

    TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G,
352 353
                    POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
    TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, olen ) == 0 );
354 355 356 357 358 359
    TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
    TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );

    TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
    TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
360 361
                    POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
    TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, olen ) == 0 );
362 363 364 365
    TEST_ASSERT( ecp_is_zero( &pt ) );

    TEST_ASSERT( ecp_set_zero( &pt ) == 0 );
    TEST_ASSERT( ecp_tls_write_point( &grp, &pt,
366 367
                    POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
    TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, olen ) == 0 );
368
    TEST_ASSERT( ecp_is_zero( &pt ) );
369 370

    ecp_group_free( &grp );
371
    ecp_point_free( &pt );
372 373 374
}
END_CASE

375
BEGIN_CASE
376
ecp_tls_read_group:record:ret:bits
377 378 379
{
    ecp_group grp;
    unsigned char buf[10];
380
    const unsigned char *vbuf = buf;
381 382 383 384 385 386 387
    int len, ret;

    ecp_group_init( &grp );
    memset( buf, 0x00, sizeof( buf ) );

    len = unhexify( buf, {record} );

388
    ret = ecp_tls_read_group( &grp, &vbuf, len );
389 390 391

    TEST_ASSERT( ret == {ret} );
    if( ret == 0)
392
    {
393
        TEST_ASSERT( mpi_msb( &grp.P ) == {bits} );
394 395
        TEST_ASSERT( *vbuf == 0x00 );
    }
396 397 398 399 400

    ecp_group_free( &grp );
}
END_CASE

401 402 403 404 405
BEGIN_CASE
ecp_tls_write_read_group:id
{
    ecp_group grp1, grp2;
    unsigned char buf[10];
406
    const unsigned char *vbuf = buf;
407 408 409 410 411 412 413 414 415 416
    size_t len;
    int ret;

    ecp_group_init( &grp1 );
    ecp_group_init( &grp2 );
    memset( buf, 0x00, sizeof( buf ) );

    TEST_ASSERT( ecp_use_known_dp( &grp1, POLARSSL_ECP_DP_{id} ) == 0 );

    TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
417
    TEST_ASSERT( ( ret = ecp_tls_read_group( &grp2, &vbuf, len ) ) == 0 );
418 419 420 421 422 423 424 425 426 427 428 429

    if( ret == 0 )
    {
        TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
        TEST_ASSERT( grp1.id == grp2.id );
    }

    ecp_group_free( &grp1 );
    ecp_group_free( &grp2 );
}
END_CASE

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
BEGIN_CASE
ecp_gen_keypair:id
{
    ecp_group grp;
    ecp_point Q;
    mpi d;
    rnd_pseudo_info rnd_info;

    ecp_group_init( &grp );
    ecp_point_init( &Q );
    mpi_init( &d );
    memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );

    TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 );

    TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info )
                 == 0 );

    TEST_ASSERT( mpi_cmp_mpi( &d, &grp.N ) < 0 );
    TEST_ASSERT( mpi_cmp_int( &d, 1 ) >= 0 );

    ecp_group_free( &grp );
    ecp_point_free( &Q );
    mpi_free( &d );
}
END_CASE

457 458 459 460 461 462
BEGIN_CASE
ecp_selftest:
{
    TEST_ASSERT( ecp_self_test( 0 ) == 0 );
}
END_CASE