1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| int main( int argc, char *argv[] ) { int ret = 1; int exit_code = MBEDTLS_EXIT_FAILURE; size_t olen; char buf[65]; mbedtls_ecp_group grp; mbedtls_mpi cli_secret, srv_secret; mbedtls_mpi cli_pri, srv_pri; mbedtls_ecp_point cli_pub, srv_pub;
mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; const char pers[] = "ecdh"; ((void) argc); ((void) argv);
clock_t start_time, end_time;
start_time = clock();
for (int i = 0; i < 100; ++i) {
mbedtls_mpi_init(&cli_pri ); mbedtls_mpi_init(&srv_pri); mbedtls_mpi_init(&cli_secret); mbedtls_mpi_init(&srv_secret);
mbedtls_ecp_group_init(&grp); mbedtls_ecp_point_init(&cli_pub); mbedtls_ecp_point_init(&srv_pub);
mbedtls_entropy_init(&entropy); mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_printf( " . Seeding the random number generator..." ); fflush( stdout ); if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, sizeof pers ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); goto exit; }
mbedtls_printf( " ok\n" );
mbedtls_printf( " . Setting up client context..." ); fflush( stdout );
ret = mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP256R1); if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret ); goto exit; }
ret = mbedtls_ecdh_gen_public(&grp, &cli_pri, &cli_pub, mbedtls_ctr_drbg_random, &ctr_drbg ); if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret ); goto exit; }
ret = mbedtls_ecp_point_write_binary(&grp, &cli_pub, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, sizeof(buf) ); if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_ecp_point_write_binary returned %d\n", ret ); goto exit; }
mbedtls_printf( " ok\n" );
mbedtls_printf( " . Setting up server context..." ); fflush( stdout );
ret = mbedtls_ecdh_gen_public(&grp, &srv_pri, &srv_pub, mbedtls_ctr_drbg_random, &ctr_drbg); if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret ); goto exit; }
ret = mbedtls_ecp_point_write_binary(&grp, &srv_pub, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, sizeof(buf)); if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret ); goto exit; }
mbedtls_printf( " ok\n" );
mbedtls_printf( " . Server reading client key and computing secret..." ); fflush( stdout );
ret = mbedtls_ecdh_compute_shared( &grp, &cli_secret, &srv_pub, &cli_pri, mbedtls_ctr_drbg_random, &ctr_drbg ); if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret ); goto exit; }
mbedtls_printf( " ok\n" );
mbedtls_mpi_write_binary(&cli_secret, buf, mbedtls_mpi_size(&cli_secret));
mbedtls_printf( " . Client reading server key and computing secret..." ); fflush( stdout );
ret = mbedtls_ecdh_compute_shared(&grp, &srv_secret, &cli_pub, &srv_pri, mbedtls_ctr_drbg_random, &ctr_drbg); if (ret != 0) { mbedtls_printf(" failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret); goto exit; }
mbedtls_printf(" ok\n");
mbedtls_mpi_write_binary(&srv_secret, buf, mbedtls_mpi_size(&srv_secret));
mbedtls_printf( " . Checking if both computed secrets are equal..." ); fflush( stdout );
ret = mbedtls_mpi_cmp_mpi( &cli_secret, &srv_secret); if( ret != 0 ) { mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret ); goto exit; }
mbedtls_printf( " ok\n" );
exit_code = MBEDTLS_EXIT_SUCCESS;
exit:
mbedtls_mpi_free(&cli_pri); mbedtls_mpi_free(&srv_pri); mbedtls_mpi_free(&cli_secret); mbedtls_mpi_free(&srv_secret); mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&cli_pub); mbedtls_ecp_point_free(&srv_pub);
mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy );
}
end_time = clock();
double time_cost = (double)(end_time - start_time) / CLOCKS_PER_SEC;
mbedtls_printf("ecdh %f seconds with uecc.\n", time_cost);
#if defined(_WIN32) mbedtls_printf(" + Press Enter to exit this program.\n"); fflush(stdout); getchar(); #endif
return( exit_code ); }
|