test_suite_dhm.function 4.28 KB
Newer Older
1
/* BEGIN_HEADER */
2
#include "mbedtls/dhm.h"
3
/* END_HEADER */
4

5
/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
6
 * depends_on:MBEDTLS_DHM_C:MBEDTLS_BIGNUM_C
7 8
 * 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
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
14 15
    mbedtls_dhm_context ctx_srv;
    mbedtls_dhm_context ctx_cli;
16 17 18 19 20
    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
    size_t ske_len = 0;
    size_t pub_cli_len = 0;
23 24
    size_t sec_srv_len;
    size_t sec_cli_len;
25
    int x_size, i;
26
    rnd_pseudo_info rnd_info;
27

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
28 29
    mbedtls_dhm_init( &ctx_srv );
    mbedtls_dhm_init( &ctx_cli );
30 31 32 33
    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
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
39 40 41
    TEST_ASSERT( mbedtls_mpi_read_string( &ctx_srv.P, radix_P, input_P ) == 0 );
    TEST_ASSERT( mbedtls_mpi_read_string( &ctx_srv.G, radix_G, input_G ) == 0 );
    x_size = mbedtls_mpi_size( &ctx_srv.P );
42
    pub_cli_len = x_size;
43

44 45 46
    /*
     * First key exchange
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
47
    TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
48 49
    ske[ske_len++] = 0;
    ske[ske_len++] = 0;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
50
    TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
51

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
52 53
    TEST_ASSERT( mbedtls_dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
    TEST_ASSERT( mbedtls_dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );
54

55 56
    TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( sec_srv ), &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
    TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_cli, sec_cli, sizeof( sec_cli ), &sec_cli_len, NULL, NULL ) == 0 );
57 58 59 60 61

    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
    /* 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;
66
        TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( 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

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
78
    TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
79 80
    ske[ske_len++] = 0;
    ske[ske_len++] = 0;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
81
    TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
82

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
83 84
    TEST_ASSERT( mbedtls_dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
    TEST_ASSERT( mbedtls_dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );
85

86 87
    TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( sec_srv ), &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
    TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_cli, sec_cli, sizeof( sec_cli ), &sec_cli_len, NULL, NULL ) == 0 );
88 89 90 91

    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
92

93
exit:
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
94 95
    mbedtls_dhm_free( &ctx_srv );
    mbedtls_dhm_free( &ctx_cli );
96
}
97
/* END_CASE */
98

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
99
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
100 101
void dhm_file( char *filename, char *p, char *g, int len )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
102 103
    mbedtls_dhm_context ctx;
    mbedtls_mpi P, G;
104

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
105 106
    mbedtls_dhm_init( &ctx );
    mbedtls_mpi_init( &P ); mbedtls_mpi_init( &G );
107

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
108 109
    TEST_ASSERT( mbedtls_mpi_read_string( &P, 16, p ) == 0 );
    TEST_ASSERT( mbedtls_mpi_read_string( &G, 16, g ) == 0 );
110

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
111
    TEST_ASSERT( mbedtls_dhm_parse_dhmfile( &ctx, filename ) == 0 );
112 113

    TEST_ASSERT( ctx.len == (size_t) len );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
114 115
    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &P ) == 0 );
    TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.G, &G ) == 0 );
116

117
exit:
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
118 119
    mbedtls_mpi_free( &P ); mbedtls_mpi_free( &G );
    mbedtls_dhm_free( &ctx );
120 121 122
}
/* END_CASE */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
123
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
124 125
void dhm_selftest()
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
126
    TEST_ASSERT( mbedtls_dhm_self_test( 0 ) == 0 );
127 128
}
/* END_CASE */