1/* BEGIN_HEADER */ 2#include "mbedtls/ecdh.h" 3/* END_HEADER */ 4 5/* BEGIN_DEPENDENCIES 6 * depends_on:MBEDTLS_ECDH_C 7 * END_DEPENDENCIES 8 */ 9 10/* BEGIN_CASE */ 11void ecdh_primitive_random( int id ) 12{ 13 mbedtls_ecp_group grp; 14 mbedtls_ecp_point qA, qB; 15 mbedtls_mpi dA, dB, zA, zB; 16 rnd_pseudo_info rnd_info; 17 18 mbedtls_ecp_group_init( &grp ); 19 mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB ); 20 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB ); 21 mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB ); 22 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); 23 24 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); 25 26 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA, &rnd_pseudo_rand, &rnd_info ) 27 == 0 ); 28 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB, &rnd_pseudo_rand, &rnd_info ) 29 == 0 ); 30 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA, 31 &rnd_pseudo_rand, &rnd_info ) == 0 ); 32 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB, 33 NULL, NULL ) == 0 ); 34 35 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &zB ) == 0 ); 36 37exit: 38 mbedtls_ecp_group_free( &grp ); 39 mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB ); 40 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB ); 41 mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB ); 42} 43/* END_CASE */ 44 45/* BEGIN_CASE */ 46void ecdh_primitive_testvec( int id, char *dA_str, char *xA_str, char *yA_str, 47 char *dB_str, char *xB_str, char *yB_str, 48 char *z_str ) 49{ 50 mbedtls_ecp_group grp; 51 mbedtls_ecp_point qA, qB; 52 mbedtls_mpi dA, dB, zA, zB, check; 53 unsigned char rnd_buf_A[MBEDTLS_ECP_MAX_BYTES]; 54 unsigned char rnd_buf_B[MBEDTLS_ECP_MAX_BYTES]; 55 rnd_buf_info rnd_info_A, rnd_info_B; 56 57 mbedtls_ecp_group_init( &grp ); 58 mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB ); 59 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB ); 60 mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB ); mbedtls_mpi_init( &check ); 61 62 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 ); 63 64 rnd_info_A.buf = rnd_buf_A; 65 rnd_info_A.length = unhexify( rnd_buf_A, dA_str ); 66 67 /* Fix rnd_buf_A by shifting it left if necessary */ 68 if( grp.nbits % 8 != 0 ) 69 { 70 unsigned char shift = 8 - ( grp.nbits % 8 ); 71 size_t i; 72 73 for( i = 0; i < rnd_info_A.length - 1; i++ ) 74 rnd_buf_A[i] = rnd_buf_A[i] << shift 75 | rnd_buf_A[i+1] >> ( 8 - shift ); 76 77 rnd_buf_A[rnd_info_A.length-1] <<= shift; 78 } 79 80 rnd_info_B.buf = rnd_buf_B; 81 rnd_info_B.length = unhexify( rnd_buf_B, dB_str ); 82 83 /* Fix rnd_buf_B by shifting it left if necessary */ 84 if( grp.nbits % 8 != 0 ) 85 { 86 unsigned char shift = 8 - ( grp.nbits % 8 ); 87 size_t i; 88 89 for( i = 0; i < rnd_info_B.length - 1; i++ ) 90 rnd_buf_B[i] = rnd_buf_B[i] << shift 91 | rnd_buf_B[i+1] >> ( 8 - shift ); 92 93 rnd_buf_B[rnd_info_B.length-1] <<= shift; 94 } 95 96 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA, 97 rnd_buffer_rand, &rnd_info_A ) == 0 ); 98 TEST_ASSERT( ! mbedtls_ecp_is_zero( &qA ) ); 99 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xA_str ) == 0 ); 100 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.X, &check ) == 0 ); 101 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yA_str ) == 0 ); 102 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.Y, &check ) == 0 ); 103 104 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB, 105 rnd_buffer_rand, &rnd_info_B ) == 0 ); 106 TEST_ASSERT( ! mbedtls_ecp_is_zero( &qB ) ); 107 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xB_str ) == 0 ); 108 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.X, &check ) == 0 ); 109 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yB_str ) == 0 ); 110 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.Y, &check ) == 0 ); 111 112 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, z_str ) == 0 ); 113 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA, NULL, NULL ) == 0 ); 114 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &check ) == 0 ); 115 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB, NULL, NULL ) == 0 ); 116 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zB, &check ) == 0 ); 117 118exit: 119 mbedtls_ecp_group_free( &grp ); 120 mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB ); 121 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB ); 122 mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB ); mbedtls_mpi_free( &check ); 123} 124/* END_CASE */ 125 126/* BEGIN_CASE */ 127void ecdh_exchange( int id ) 128{ 129 mbedtls_ecdh_context srv, cli; 130 unsigned char buf[1000]; 131 const unsigned char *vbuf; 132 size_t len; 133 rnd_pseudo_info rnd_info; 134 135 mbedtls_ecdh_init( &srv ); 136 mbedtls_ecdh_init( &cli ); 137 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); 138 139 TEST_ASSERT( mbedtls_ecp_group_load( &srv.grp, id ) == 0 ); 140 141 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; 142 TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000, 143 &rnd_pseudo_rand, &rnd_info ) == 0 ); 144 TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 ); 145 146 memset( buf, 0x00, sizeof( buf ) ); 147 TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000, 148 &rnd_pseudo_rand, &rnd_info ) == 0 ); 149 TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 ); 150 151 TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000, 152 &rnd_pseudo_rand, &rnd_info ) == 0 ); 153 TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &len, buf, 1000, NULL, NULL ) == 0 ); 154 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &srv.z, &cli.z ) == 0 ); 155 156exit: 157 mbedtls_ecdh_free( &srv ); 158 mbedtls_ecdh_free( &cli ); 159} 160/* END_CASE */ 161