blob: ae814d352c6bb32f5f8337b15b9d5abace69fe07 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/dhm.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakker5c60de22009-07-08 19:47:36 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_DHM_C:MBEDTLS_BIGNUM_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +020011void dhm_do_dhm( int radix_P, char *input_P,
Paul Bakker33b43f12013-08-20 11:48:36 +020012 int radix_G, char *input_G )
Paul Bakker5c60de22009-07-08 19:47:36 +000013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014 mbedtls_dhm_context ctx_srv;
15 mbedtls_dhm_context ctx_cli;
Paul Bakker5c60de22009-07-08 19:47:36 +000016 unsigned char ske[1000];
17 unsigned char *p = ske;
18 unsigned char pub_cli[1000];
19 unsigned char sec_srv[1000];
20 unsigned char sec_cli[1000];
Paul Bakkerf4a3f302011-04-24 15:53:29 +000021 size_t ske_len = 0;
22 size_t pub_cli_len = 0;
23 size_t sec_srv_len = 1000;
24 size_t sec_cli_len = 1000;
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +020025 int x_size, i;
Paul Bakker997bbd12011-03-13 15:45:42 +000026 rnd_pseudo_info rnd_info;
Paul Bakker5c60de22009-07-08 19:47:36 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 mbedtls_dhm_init( &ctx_srv );
29 mbedtls_dhm_init( &ctx_cli );
Paul Bakker5c60de22009-07-08 19:47:36 +000030 memset( ske, 0x00, 1000 );
31 memset( pub_cli, 0x00, 1000 );
32 memset( sec_srv, 0x00, 1000 );
33 memset( sec_cli, 0x00, 1000 );
Paul Bakker997bbd12011-03-13 15:45:42 +000034 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
Paul Bakker5c60de22009-07-08 19:47:36 +000035
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +020036 /*
37 * Set params
38 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 TEST_ASSERT( mbedtls_mpi_read_string( &ctx_srv.P, radix_P, input_P ) == 0 );
40 TEST_ASSERT( mbedtls_mpi_read_string( &ctx_srv.G, radix_G, input_G ) == 0 );
41 x_size = mbedtls_mpi_size( &ctx_srv.P );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +020042 pub_cli_len = x_size;
43
44 /*
45 * First key exchange
46 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +020048 ske[ske_len++] = 0;
49 ske[ske_len++] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +020051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 TEST_ASSERT( mbedtls_dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
53 TEST_ASSERT( mbedtls_dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
56 TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_cli, sec_cli, &sec_cli_len, NULL, NULL ) == 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +020057
58 TEST_ASSERT( sec_srv_len == sec_cli_len );
59 TEST_ASSERT( sec_srv_len != 0 );
60 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
61
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +020062 /* Re-do calc_secret on server a few times to test update of blinding values */
63 for( i = 0; i < 3; i++ )
64 {
65 sec_srv_len = 1000;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +020067
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +020068 TEST_ASSERT( sec_srv_len == sec_cli_len );
69 TEST_ASSERT( sec_srv_len != 0 );
70 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
71 }
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +020072
73 /*
74 * Second key exchange to test change of blinding values on server
75 */
76 sec_cli_len = 1000;
77 sec_srv_len = 1000;
78 p = ske;
Paul Bakker5c60de22009-07-08 19:47:36 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
Paul Bakker5c60de22009-07-08 19:47:36 +000081 ske[ske_len++] = 0;
82 ske[ske_len++] = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
Paul Bakker5c60de22009-07-08 19:47:36 +000084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 TEST_ASSERT( mbedtls_dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
86 TEST_ASSERT( mbedtls_dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );
Paul Bakker5c60de22009-07-08 19:47:36 +000087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 );
89 TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_cli, sec_cli, &sec_cli_len, NULL, NULL ) == 0 );
Paul Bakker5c60de22009-07-08 19:47:36 +000090
91 TEST_ASSERT( sec_srv_len == sec_cli_len );
92 TEST_ASSERT( sec_srv_len != 0 );
93 TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
Paul Bakkerc43481a2011-02-20 16:34:26 +000094
Paul Bakkerbd51b262014-07-10 15:26:12 +020095exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_dhm_free( &ctx_srv );
97 mbedtls_dhm_free( &ctx_cli );
Paul Bakker5c60de22009-07-08 19:47:36 +000098}
Paul Bakker33b43f12013-08-20 11:48:36 +020099/* END_CASE */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100102void dhm_file( char *filename, char *p, char *g, int len )
103{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_dhm_context ctx;
105 mbedtls_mpi P, G;
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_dhm_init( &ctx );
108 mbedtls_mpi_init( &P ); mbedtls_mpi_init( &G );
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 TEST_ASSERT( mbedtls_mpi_read_string( &P, 16, p ) == 0 );
111 TEST_ASSERT( mbedtls_mpi_read_string( &G, 16, g ) == 0 );
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 TEST_ASSERT( mbedtls_dhm_parse_dhmfile( &ctx, filename ) == 0 );
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100114
115 TEST_ASSERT( ctx.len == (size_t) len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &P ) == 0 );
117 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.G, &G ) == 0 );
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100118
Paul Bakkerbd51b262014-07-10 15:26:12 +0200119exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_mpi_free( &P ); mbedtls_mpi_free( &G );
121 mbedtls_dhm_free( &ctx );
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100122}
123/* END_CASE */
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200126void dhm_selftest()
127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 TEST_ASSERT( mbedtls_dhm_self_test( 0 ) == 0 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200129}
130/* END_CASE */