test_suite_dhm.function 3.43 KB
Newer Older
1
/* BEGIN_HEADER */
2
#include <polarssl/dhm.h>
3
/* END_HEADER */
4

5 6 7 8
/* BEGIN_DEPENDENCIES
 * depends_on:POLARSSL_DHM_C:POLARSSL_BIGNUM_C
 * END_DEPENDENCIES
 */
9

10
/* BEGIN_CASE */
11
void dhm_do_dhm( int radix_P, char *input_P,
12
                 int radix_G, char *input_G )
13 14 15 16 17 18 19 20
{
    dhm_context ctx_srv;
    dhm_context ctx_cli;
    unsigned char ske[1000];
    unsigned char *p = ske;
    unsigned char pub_cli[1000];
    unsigned char sec_srv[1000];
    unsigned char sec_cli[1000];
21 22 23 24
    size_t ske_len = 0;
    size_t pub_cli_len = 0;
    size_t sec_srv_len = 1000;
    size_t sec_cli_len = 1000;
25
    int x_size, i;
26
    rnd_pseudo_info rnd_info;
27 28 29 30 31 32 33

    memset( &ctx_srv, 0x00, sizeof( dhm_context ) );
    memset( &ctx_cli, 0x00, sizeof( dhm_context ) );
    memset( ske, 0x00, 1000 );
    memset( pub_cli, 0x00, 1000 );
    memset( sec_srv, 0x00, 1000 );
    memset( sec_cli, 0x00, 1000 );
34
    memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
35

36 37 38
    /*
     * Set params
     */
39 40
    TEST_ASSERT( mpi_read_string( &ctx_srv.P, radix_P, input_P ) == 0 );
    TEST_ASSERT( mpi_read_string( &ctx_srv.G, radix_G, input_G ) == 0 );
41
    x_size = mpi_size( &ctx_srv.P );
42
    pub_cli_len = x_size;
43

44 45 46
    /*
     * First key exchange
     */
47
    TEST_ASSERT( dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
48 49 50 51
    ske[ske_len++] = 0;
    ske[ske_len++] = 0;
    TEST_ASSERT( dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );

52
    TEST_ASSERT( dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
53 54 55 56 57 58 59 60 61
    TEST_ASSERT( dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );

    TEST_ASSERT( dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
    TEST_ASSERT( dhm_calc_secret( &ctx_cli, sec_cli, &sec_cli_len, NULL, NULL ) == 0 );

    TEST_ASSERT( sec_srv_len == sec_cli_len );
    TEST_ASSERT( sec_srv_len != 0 );
    TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );

62 63 64 65 66
    /* Re-do calc_secret on server a few times to test update of blinding values */
    for( i = 0; i < 3; i++ )
    {
        sec_srv_len = 1000;
        TEST_ASSERT( dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
67

68 69 70 71
        TEST_ASSERT( sec_srv_len == sec_cli_len );
        TEST_ASSERT( sec_srv_len != 0 );
        TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
    }
72 73 74 75 76 77 78 79 80 81 82 83 84 85

    /*
     * Second key exchange to test change of blinding values on server
     */
    sec_cli_len = 1000;
    sec_srv_len = 1000;
    p = ske;

    TEST_ASSERT( dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
    ske[ske_len++] = 0;
    ske[ske_len++] = 0;
    TEST_ASSERT( dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );

    TEST_ASSERT( dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
86 87
    TEST_ASSERT( dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );

88 89
    TEST_ASSERT( dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
    TEST_ASSERT( dhm_calc_secret( &ctx_cli, sec_cli, &sec_cli_len, NULL, NULL ) == 0 );
90 91 92 93

    TEST_ASSERT( sec_srv_len == sec_cli_len );
    TEST_ASSERT( sec_srv_len != 0 );
    TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
Paul Bakker's avatar
Paul Bakker committed
94 95 96

    dhm_free( &ctx_srv );
    dhm_free( &ctx_cli );
97
}
98
/* END_CASE */
99

100
/* BEGIN_CASE depends_on:POLARSSL_SELFTEST_C */
101 102 103 104 105
void dhm_selftest()
{
    TEST_ASSERT( dhm_self_test( 0 ) == 0 );
}
/* END_CASE */