mbedtls/tests/suites/test_suite_dhm.function

269 lines
11 KiB
Plaintext
Raw Normal View History

/* BEGIN_HEADER */
2015-03-09 18:05:11 +01:00
#include "mbedtls/dhm.h"
/* END_HEADER */
2009-07-08 21:47:36 +02:00
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_DHM_C:MBEDTLS_BIGNUM_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
void dhm_invalid_params( )
{
mbedtls_dhm_context ctx;
unsigned char buf[42] = { 0 };
unsigned char *buf_null = NULL;
mbedtls_mpi X;
2018-12-17 10:34:36 +01:00
size_t const buflen = sizeof( buf );
size_t len;
TEST_INVALID_PARAM( mbedtls_dhm_init( NULL ) );
TEST_VALID_PARAM( mbedtls_dhm_free( NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_read_params( NULL,
(unsigned char**) &buf,
buf ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_read_params( &ctx, &buf_null, buf ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_read_params( &ctx, NULL, buf ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_read_params( &ctx,
(unsigned char**) &buf,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_make_params( NULL, buflen,
buf, &len,
rnd_std_rand,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_make_params( &ctx, buflen,
NULL, &len,
rnd_std_rand,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_make_params( &ctx, buflen,
buf, NULL,
rnd_std_rand,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_make_params( &ctx, buflen,
buf, &len,
NULL,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_set_group( NULL, &X, &X ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_set_group( &ctx, NULL, &X ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_set_group( &ctx, &X, NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_read_public( NULL, buf, buflen ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_read_public( &ctx, NULL, buflen ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_make_public( NULL, buflen,
buf, buflen,
rnd_std_rand,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_make_public( &ctx, buflen,
NULL, buflen,
rnd_std_rand,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_make_public( &ctx, buflen,
buf, buflen,
NULL,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_calc_secret( NULL, buf, buflen,
&len, rnd_std_rand,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_calc_secret( &ctx, NULL, buflen,
&len, rnd_std_rand,
NULL ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_calc_secret( &ctx, buf, buflen,
NULL, rnd_std_rand,
NULL ) );
#if defined(MBEDTLS_ASN1_PARSE_C)
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_parse_dhm( NULL, buf, buflen ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
2018-12-17 10:34:36 +01:00
mbedtls_dhm_parse_dhm( &ctx, NULL, buflen ) );
#if defined(MBEDTLS_FS_IO)
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_parse_dhmfile( NULL, "" ) );
TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
mbedtls_dhm_parse_dhmfile( &ctx, NULL ) );
#endif /* MBEDTLS_FS_IO */
#endif /* MBEDTLS_ASN1_PARSE_C */
exit:
return;
}
/* END_CASE */
/* BEGIN_CASE */
2013-09-04 16:39:03 +02:00
void dhm_do_dhm( int radix_P, char *input_P,
int radix_G, char *input_G, int result )
2009-07-08 21:47:36 +02:00
{
mbedtls_dhm_context ctx_srv;
mbedtls_dhm_context ctx_cli;
2009-07-08 21:47:36 +02:00
unsigned char ske[1000];
unsigned char *p = ske;
unsigned char pub_cli[1000];
unsigned char sec_srv[1000];
unsigned char sec_cli[1000];
2011-04-24 17:53:29 +02:00
size_t ske_len = 0;
size_t pub_cli_len = 0;
size_t sec_srv_len;
size_t sec_cli_len;
2013-09-17 11:34:11 +02:00
int x_size, i;
rnd_pseudo_info rnd_info;
2009-07-08 21:47:36 +02:00
mbedtls_dhm_init( &ctx_srv );
mbedtls_dhm_init( &ctx_cli );
2009-07-08 21:47:36 +02:00
memset( ske, 0x00, 1000 );
memset( pub_cli, 0x00, 1000 );
memset( sec_srv, 0x00, 1000 );
memset( sec_cli, 0x00, 1000 );
memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
2009-07-08 21:47:36 +02:00
2013-09-04 16:29:59 +02:00
/*
* Set params
*/
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 );
2013-09-04 16:29:59 +02:00
pub_cli_len = x_size;
2009-07-08 21:47:36 +02:00
2013-09-04 16:29:59 +02:00
/*
* First key exchange
*/
TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == result );
if ( result != 0 )
goto exit;
2009-07-08 21:47:36 +02:00
ske[ske_len++] = 0;
ske[ske_len++] = 0;
TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
2009-07-08 21:47:36 +02:00
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 );
2013-09-04 16:29:59 +02:00
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 );
2013-09-04 16:29:59 +02:00
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 );
2013-09-17 11:34:11 +02:00
/* 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( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( sec_srv ), &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
2009-07-08 21:47:36 +02:00
2013-09-17 11:34:11 +02:00
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 );
}
2013-09-04 16:29:59 +02:00
/*
* Second key exchange to test change of blinding values on server
*/
p = ske;
TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
2013-09-04 16:29:59 +02:00
ske[ske_len++] = 0;
ske[ske_len++] = 0;
TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
2013-09-04 16:29:59 +02:00
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 );
2009-07-08 21:47:36 +02:00
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 );
2009-07-08 21:47:36 +02:00
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 );
2011-02-20 17:34:26 +01:00
exit:
mbedtls_dhm_free( &ctx_srv );
mbedtls_dhm_free( &ctx_cli );
2009-07-08 21:47:36 +02:00
}
/* END_CASE */
/* BEGIN_CASE */
void dhm_make_public( int P_bytes, int radix_G, char *input_G, int result )
{
mbedtls_mpi P, G;
mbedtls_dhm_context ctx;
unsigned char output[MBEDTLS_MPI_MAX_SIZE];
mbedtls_mpi_init( &P );
mbedtls_mpi_init( &G );
mbedtls_dhm_init( &ctx );
TEST_ASSERT( mbedtls_mpi_lset( &P, 1 ) == 0 );
TEST_ASSERT( mbedtls_mpi_shift_l( &P, ( P_bytes * 8 ) - 1 ) == 0 );
TEST_ASSERT( mbedtls_mpi_set_bit( &P, 0, 1 ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_string( &G, radix_G, input_G ) == 0 );
TEST_ASSERT( mbedtls_dhm_set_group( &ctx, &P, &G ) == 0 );
TEST_ASSERT( mbedtls_dhm_make_public( &ctx, (int) mbedtls_mpi_size( &P ),
output, sizeof(output),
&rnd_pseudo_rand,
NULL ) == result );
exit:
mbedtls_mpi_free( &P );
mbedtls_mpi_free( &G );
mbedtls_dhm_free( &ctx );
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
2017-05-30 15:23:15 +02:00
void dhm_file( char * filename, char * p, char * g, int len )
2014-03-29 16:42:38 +01:00
{
mbedtls_dhm_context ctx;
mbedtls_mpi P, G;
2014-03-29 16:42:38 +01:00
mbedtls_dhm_init( &ctx );
mbedtls_mpi_init( &P ); mbedtls_mpi_init( &G );
2014-03-29 16:42:38 +01:00
TEST_ASSERT( mbedtls_mpi_read_string( &P, 16, p ) == 0 );
TEST_ASSERT( mbedtls_mpi_read_string( &G, 16, g ) == 0 );
2014-03-29 16:42:38 +01:00
TEST_ASSERT( mbedtls_dhm_parse_dhmfile( &ctx, filename ) == 0 );
2014-03-29 16:42:38 +01:00
TEST_ASSERT( ctx.len == (size_t) len );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &P ) == 0 );
TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.G, &G ) == 0 );
2014-03-29 16:42:38 +01:00
exit:
mbedtls_mpi_free( &P ); mbedtls_mpi_free( &G );
mbedtls_dhm_free( &ctx );
2014-03-29 16:42:38 +01:00
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
2017-05-30 15:23:15 +02:00
void dhm_selftest( )
{
TEST_ASSERT( mbedtls_dhm_self_test( 1 ) == 0 );
}
/* END_CASE */