test_suite_ecdh.function 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/ecdh.h"
  3. static int load_public_key( int grp_id, data_t *point,
  4. mbedtls_ecp_keypair *ecp )
  5. {
  6. int ok = 0;
  7. TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
  8. TEST_ASSERT( mbedtls_ecp_point_read_binary( &ecp->grp,
  9. &ecp->Q,
  10. point->x,
  11. point->len ) == 0 );
  12. TEST_ASSERT( mbedtls_ecp_check_pubkey( &ecp->grp,
  13. &ecp->Q ) == 0 );
  14. ok = 1;
  15. exit:
  16. return( ok );
  17. }
  18. static int load_private_key( int grp_id, data_t *private_key,
  19. mbedtls_ecp_keypair *ecp,
  20. mbedtls_test_rnd_pseudo_info *rnd_info )
  21. {
  22. int ok = 0;
  23. TEST_ASSERT( mbedtls_ecp_read_key( grp_id, ecp,
  24. private_key->x,
  25. private_key->len ) == 0 );
  26. TEST_ASSERT( mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) == 0 );
  27. /* Calculate the public key from the private key. */
  28. TEST_ASSERT( mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d,
  29. &ecp->grp.G,
  30. &mbedtls_test_rnd_pseudo_rand,
  31. rnd_info ) == 0 );
  32. ok = 1;
  33. exit:
  34. return( ok );
  35. }
  36. /* END_HEADER */
  37. /* BEGIN_DEPENDENCIES
  38. * depends_on:MBEDTLS_ECDH_C
  39. * END_DEPENDENCIES
  40. */
  41. /* BEGIN_CASE */
  42. void ecdh_valid_param( )
  43. {
  44. TEST_VALID_PARAM( mbedtls_ecdh_free( NULL ) );
  45. }
  46. /* END_CASE */
  47. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  48. void ecdh_invalid_param( )
  49. {
  50. mbedtls_ecp_group grp;
  51. mbedtls_ecdh_context ctx;
  52. mbedtls_mpi m;
  53. mbedtls_ecp_point P;
  54. mbedtls_ecp_keypair kp;
  55. size_t olen;
  56. unsigned char buf[42] = { 0 };
  57. const unsigned char *buf_null = NULL;
  58. size_t const buflen = sizeof( buf );
  59. int invalid_side = 42;
  60. mbedtls_ecp_group_id valid_grp = MBEDTLS_ECP_DP_SECP192R1;
  61. TEST_INVALID_PARAM( mbedtls_ecdh_init( NULL ) );
  62. #if defined(MBEDTLS_ECP_RESTARTABLE)
  63. TEST_INVALID_PARAM( mbedtls_ecdh_enable_restart( NULL ) );
  64. #endif /* MBEDTLS_ECP_RESTARTABLE */
  65. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  66. mbedtls_ecdh_gen_public( NULL, &m, &P,
  67. mbedtls_test_rnd_std_rand,
  68. NULL ) );
  69. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  70. mbedtls_ecdh_gen_public( &grp, NULL, &P,
  71. mbedtls_test_rnd_std_rand,
  72. NULL ) );
  73. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  74. mbedtls_ecdh_gen_public( &grp, &m, NULL,
  75. mbedtls_test_rnd_std_rand,
  76. NULL ) );
  77. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  78. mbedtls_ecdh_gen_public( &grp, &m, &P,
  79. NULL, NULL ) );
  80. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  81. mbedtls_ecdh_compute_shared( NULL, &m, &P, &m,
  82. mbedtls_test_rnd_std_rand,
  83. NULL ) );
  84. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  85. mbedtls_ecdh_compute_shared( &grp, NULL, &P, &m,
  86. mbedtls_test_rnd_std_rand,
  87. NULL ) );
  88. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  89. mbedtls_ecdh_compute_shared( &grp, &m, NULL, &m,
  90. mbedtls_test_rnd_std_rand,
  91. NULL ) );
  92. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  93. mbedtls_ecdh_compute_shared( &grp, &m, &P, NULL,
  94. mbedtls_test_rnd_std_rand,
  95. NULL ) );
  96. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  97. mbedtls_ecdh_setup( NULL, valid_grp ) );
  98. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  99. mbedtls_ecdh_make_params( NULL, &olen, buf, buflen,
  100. mbedtls_test_rnd_std_rand, NULL ) );
  101. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  102. mbedtls_ecdh_make_params( &ctx, NULL, buf, buflen,
  103. mbedtls_test_rnd_std_rand, NULL ) );
  104. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  105. mbedtls_ecdh_make_params( &ctx, &olen, NULL, buflen,
  106. mbedtls_test_rnd_std_rand, NULL ) );
  107. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  108. mbedtls_ecdh_make_params( &ctx, &olen, buf, buflen, NULL, NULL ) );
  109. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  110. mbedtls_ecdh_read_params( NULL,
  111. (const unsigned char**) &buf,
  112. buf ) );
  113. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  114. mbedtls_ecdh_read_params( &ctx, &buf_null,
  115. buf ) );
  116. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  117. mbedtls_ecdh_read_params( &ctx, NULL, buf ) );
  118. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  119. mbedtls_ecdh_read_params( &ctx,
  120. (const unsigned char**) &buf,
  121. NULL ) );
  122. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  123. mbedtls_ecdh_get_params( NULL, &kp,
  124. MBEDTLS_ECDH_OURS ) );
  125. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  126. mbedtls_ecdh_get_params( &ctx, NULL,
  127. MBEDTLS_ECDH_OURS ) );
  128. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  129. mbedtls_ecdh_get_params( &ctx, &kp,
  130. invalid_side ) );
  131. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  132. mbedtls_ecdh_make_public( NULL, &olen, buf, buflen,
  133. mbedtls_test_rnd_std_rand, NULL ) );
  134. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  135. mbedtls_ecdh_make_public( &ctx, NULL, buf, buflen,
  136. mbedtls_test_rnd_std_rand, NULL ) );
  137. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  138. mbedtls_ecdh_make_public( &ctx, &olen, NULL, buflen,
  139. mbedtls_test_rnd_std_rand, NULL ) );
  140. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  141. mbedtls_ecdh_make_public( &ctx, &olen, buf, buflen, NULL, NULL ) );
  142. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  143. mbedtls_ecdh_read_public( NULL, buf, buflen ) );
  144. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  145. mbedtls_ecdh_read_public( &ctx, NULL, buflen ) );
  146. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  147. mbedtls_ecdh_calc_secret( NULL, &olen, buf, buflen,
  148. mbedtls_test_rnd_std_rand, NULL ) );
  149. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  150. mbedtls_ecdh_calc_secret( &ctx, NULL, buf, buflen,
  151. mbedtls_test_rnd_std_rand, NULL ) );
  152. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  153. mbedtls_ecdh_calc_secret( &ctx, &olen, NULL, buflen,
  154. mbedtls_test_rnd_std_rand, NULL ) );
  155. exit:
  156. return;
  157. }
  158. /* END_CASE */
  159. /* BEGIN_CASE */
  160. void ecdh_primitive_random( int id )
  161. {
  162. mbedtls_ecp_group grp;
  163. mbedtls_ecp_point qA, qB;
  164. mbedtls_mpi dA, dB, zA, zB;
  165. mbedtls_test_rnd_pseudo_info rnd_info;
  166. mbedtls_ecp_group_init( &grp );
  167. mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
  168. mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
  169. mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB );
  170. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  171. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  172. TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA,
  173. &mbedtls_test_rnd_pseudo_rand,
  174. &rnd_info ) == 0 );
  175. TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB,
  176. &mbedtls_test_rnd_pseudo_rand,
  177. &rnd_info ) == 0 );
  178. TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA,
  179. &mbedtls_test_rnd_pseudo_rand,
  180. &rnd_info ) == 0 );
  181. TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB,
  182. NULL, NULL ) == 0 );
  183. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &zB ) == 0 );
  184. exit:
  185. mbedtls_ecp_group_free( &grp );
  186. mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
  187. mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
  188. mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB );
  189. }
  190. /* END_CASE */
  191. /* BEGIN_CASE */
  192. void ecdh_primitive_testvec( int id, data_t * rnd_buf_A, char * xA_str,
  193. char * yA_str, data_t * rnd_buf_B,
  194. char * xB_str, char * yB_str, char * z_str )
  195. {
  196. mbedtls_ecp_group grp;
  197. mbedtls_ecp_point qA, qB;
  198. mbedtls_mpi dA, dB, zA, zB, check;
  199. mbedtls_test_rnd_buf_info rnd_info_A, rnd_info_B;
  200. mbedtls_ecp_group_init( &grp );
  201. mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
  202. mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
  203. mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB ); mbedtls_mpi_init( &check );
  204. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  205. rnd_info_A.buf = rnd_buf_A->x;
  206. rnd_info_A.length = rnd_buf_A->len;
  207. rnd_info_A.fallback_f_rng = mbedtls_test_rnd_std_rand;
  208. rnd_info_A.fallback_p_rng = NULL;
  209. /* Fix rnd_buf_A->x by shifting it left if necessary */
  210. if( grp.nbits % 8 != 0 )
  211. {
  212. unsigned char shift = 8 - ( grp.nbits % 8 );
  213. size_t i;
  214. for( i = 0; i < rnd_info_A.length - 1; i++ )
  215. rnd_buf_A->x[i] = rnd_buf_A->x[i] << shift
  216. | rnd_buf_A->x[i+1] >> ( 8 - shift );
  217. rnd_buf_A->x[rnd_info_A.length-1] <<= shift;
  218. }
  219. rnd_info_B.buf = rnd_buf_B->x;
  220. rnd_info_B.length = rnd_buf_B->len;
  221. rnd_info_B.fallback_f_rng = mbedtls_test_rnd_std_rand;
  222. rnd_info_B.fallback_p_rng = NULL;
  223. /* Fix rnd_buf_B->x by shifting it left if necessary */
  224. if( grp.nbits % 8 != 0 )
  225. {
  226. unsigned char shift = 8 - ( grp.nbits % 8 );
  227. size_t i;
  228. for( i = 0; i < rnd_info_B.length - 1; i++ )
  229. rnd_buf_B->x[i] = rnd_buf_B->x[i] << shift
  230. | rnd_buf_B->x[i+1] >> ( 8 - shift );
  231. rnd_buf_B->x[rnd_info_B.length-1] <<= shift;
  232. }
  233. TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA,
  234. mbedtls_test_rnd_buffer_rand,
  235. &rnd_info_A ) == 0 );
  236. TEST_ASSERT( ! mbedtls_ecp_is_zero( &qA ) );
  237. TEST_ASSERT( mbedtls_test_read_mpi( &check, 16, xA_str ) == 0 );
  238. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.X, &check ) == 0 );
  239. TEST_ASSERT( mbedtls_test_read_mpi( &check, 16, yA_str ) == 0 );
  240. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.Y, &check ) == 0 );
  241. TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB,
  242. mbedtls_test_rnd_buffer_rand,
  243. &rnd_info_B ) == 0 );
  244. TEST_ASSERT( ! mbedtls_ecp_is_zero( &qB ) );
  245. TEST_ASSERT( mbedtls_test_read_mpi( &check, 16, xB_str ) == 0 );
  246. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.X, &check ) == 0 );
  247. TEST_ASSERT( mbedtls_test_read_mpi( &check, 16, yB_str ) == 0 );
  248. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.Y, &check ) == 0 );
  249. TEST_ASSERT( mbedtls_test_read_mpi( &check, 16, z_str ) == 0 );
  250. TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA, NULL, NULL ) == 0 );
  251. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &check ) == 0 );
  252. TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB, NULL, NULL ) == 0 );
  253. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zB, &check ) == 0 );
  254. exit:
  255. mbedtls_ecp_group_free( &grp );
  256. mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
  257. mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
  258. mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB ); mbedtls_mpi_free( &check );
  259. }
  260. /* END_CASE */
  261. /* BEGIN_CASE */
  262. void ecdh_exchange( int id )
  263. {
  264. mbedtls_ecdh_context srv, cli;
  265. unsigned char buf[1000];
  266. const unsigned char *vbuf;
  267. size_t len;
  268. mbedtls_test_rnd_pseudo_info rnd_info;
  269. unsigned char res_buf[1000];
  270. size_t res_len;
  271. mbedtls_ecdh_init( &srv );
  272. mbedtls_ecdh_init( &cli );
  273. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  274. TEST_ASSERT( mbedtls_ecdh_setup( &srv, id ) == 0 );
  275. memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
  276. TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000,
  277. &mbedtls_test_rnd_pseudo_rand,
  278. &rnd_info ) == 0 );
  279. TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
  280. memset( buf, 0x00, sizeof( buf ) );
  281. TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000,
  282. &mbedtls_test_rnd_pseudo_rand,
  283. &rnd_info ) == 0 );
  284. TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
  285. TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000,
  286. &mbedtls_test_rnd_pseudo_rand,
  287. &rnd_info ) == 0 );
  288. TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &res_len, res_buf, 1000,
  289. NULL, NULL ) == 0 );
  290. TEST_ASSERT( len == res_len );
  291. TEST_ASSERT( memcmp( buf, res_buf, len ) == 0 );
  292. exit:
  293. mbedtls_ecdh_free( &srv );
  294. mbedtls_ecdh_free( &cli );
  295. }
  296. /* END_CASE */
  297. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  298. void ecdh_restart( int id, data_t *dA, data_t *dB, data_t *z,
  299. int enable, int max_ops, int min_restart, int max_restart )
  300. {
  301. int ret;
  302. mbedtls_ecdh_context srv, cli;
  303. unsigned char buf[1000];
  304. const unsigned char *vbuf;
  305. size_t len;
  306. mbedtls_test_rnd_buf_info rnd_info_A, rnd_info_B;
  307. int cnt_restart;
  308. mbedtls_ecp_group grp;
  309. mbedtls_ecp_group_init( &grp );
  310. mbedtls_ecdh_init( &srv );
  311. mbedtls_ecdh_init( &cli );
  312. rnd_info_A.fallback_f_rng = mbedtls_test_rnd_std_rand;
  313. rnd_info_A.fallback_p_rng = NULL;
  314. rnd_info_A.buf = dA->x;
  315. rnd_info_A.length = dA->len;
  316. rnd_info_B.fallback_f_rng = mbedtls_test_rnd_std_rand;
  317. rnd_info_B.fallback_p_rng = NULL;
  318. rnd_info_B.buf = dB->x;
  319. rnd_info_B.length = dB->len;
  320. /* The ECDH context is not guaranteed ot have an mbedtls_ecp_group structure
  321. * in every configuration, therefore we load it separately. */
  322. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  323. /* Otherwise we would have to fix the random buffer,
  324. * as in ecdh_primitive_testvec. */
  325. TEST_ASSERT( grp.nbits % 8 == 0 );
  326. TEST_ASSERT( mbedtls_ecdh_setup( &srv, id ) == 0 );
  327. /* set up restart parameters */
  328. mbedtls_ecp_set_max_ops( max_ops );
  329. if( enable )
  330. {
  331. mbedtls_ecdh_enable_restart( &srv );
  332. mbedtls_ecdh_enable_restart( &cli );
  333. }
  334. /* server writes its parameters */
  335. memset( buf, 0x00, sizeof( buf ) );
  336. len = 0;
  337. cnt_restart = 0;
  338. do {
  339. ret = mbedtls_ecdh_make_params( &srv, &len, buf, sizeof( buf ),
  340. mbedtls_test_rnd_buffer_rand,
  341. &rnd_info_A );
  342. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
  343. TEST_ASSERT( ret == 0 );
  344. TEST_ASSERT( cnt_restart >= min_restart );
  345. TEST_ASSERT( cnt_restart <= max_restart );
  346. /* client read server params */
  347. vbuf = buf;
  348. TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
  349. /* client writes its key share */
  350. memset( buf, 0x00, sizeof( buf ) );
  351. len = 0;
  352. cnt_restart = 0;
  353. do {
  354. ret = mbedtls_ecdh_make_public( &cli, &len, buf, sizeof( buf ),
  355. mbedtls_test_rnd_buffer_rand,
  356. &rnd_info_B );
  357. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
  358. TEST_ASSERT( ret == 0 );
  359. TEST_ASSERT( cnt_restart >= min_restart );
  360. TEST_ASSERT( cnt_restart <= max_restart );
  361. /* server reads client key share */
  362. TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
  363. /* server computes shared secret */
  364. memset( buf, 0, sizeof( buf ) );
  365. len = 0;
  366. cnt_restart = 0;
  367. do {
  368. ret = mbedtls_ecdh_calc_secret( &srv, &len, buf, sizeof( buf ),
  369. NULL, NULL );
  370. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
  371. TEST_ASSERT( ret == 0 );
  372. TEST_ASSERT( cnt_restart >= min_restart );
  373. TEST_ASSERT( cnt_restart <= max_restart );
  374. TEST_ASSERT( len == z->len );
  375. TEST_ASSERT( memcmp( buf, z->x, len ) == 0 );
  376. /* client computes shared secret */
  377. memset( buf, 0, sizeof( buf ) );
  378. len = 0;
  379. cnt_restart = 0;
  380. do {
  381. ret = mbedtls_ecdh_calc_secret( &cli, &len, buf, sizeof( buf ),
  382. NULL, NULL );
  383. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart );
  384. TEST_ASSERT( ret == 0 );
  385. TEST_ASSERT( cnt_restart >= min_restart );
  386. TEST_ASSERT( cnt_restart <= max_restart );
  387. TEST_ASSERT( len == z->len );
  388. TEST_ASSERT( memcmp( buf, z->x, len ) == 0 );
  389. exit:
  390. mbedtls_ecp_group_free( &grp );
  391. mbedtls_ecdh_free( &srv );
  392. mbedtls_ecdh_free( &cli );
  393. }
  394. /* END_CASE */
  395. /* BEGIN_CASE depends_on:MBEDTLS_ECDH_LEGACY_CONTEXT */
  396. void ecdh_exchange_legacy( int id )
  397. {
  398. mbedtls_ecdh_context srv, cli;
  399. unsigned char buf[1000];
  400. const unsigned char *vbuf;
  401. size_t len;
  402. mbedtls_test_rnd_pseudo_info rnd_info;
  403. mbedtls_ecdh_init( &srv );
  404. mbedtls_ecdh_init( &cli );
  405. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  406. TEST_ASSERT( mbedtls_ecp_group_load( &srv.grp, id ) == 0 );
  407. memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
  408. TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000,
  409. &mbedtls_test_rnd_pseudo_rand,
  410. &rnd_info ) == 0 );
  411. TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
  412. memset( buf, 0x00, sizeof( buf ) );
  413. TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000,
  414. &mbedtls_test_rnd_pseudo_rand,
  415. &rnd_info ) == 0 );
  416. TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
  417. TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000,
  418. &mbedtls_test_rnd_pseudo_rand,
  419. &rnd_info ) == 0 );
  420. TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &len, buf, 1000, NULL,
  421. NULL ) == 0 );
  422. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &srv.z, &cli.z ) == 0 );
  423. exit:
  424. mbedtls_ecdh_free( &srv );
  425. mbedtls_ecdh_free( &cli );
  426. }
  427. /* END_CASE */
  428. /* BEGIN_CASE */
  429. void ecdh_exchange_calc_secret( int grp_id,
  430. data_t *our_private_key,
  431. data_t *their_point,
  432. int ours_first,
  433. data_t *expected )
  434. {
  435. mbedtls_test_rnd_pseudo_info rnd_info;
  436. mbedtls_ecp_keypair our_key;
  437. mbedtls_ecp_keypair their_key;
  438. mbedtls_ecdh_context ecdh;
  439. unsigned char shared_secret[MBEDTLS_ECP_MAX_BYTES];
  440. size_t shared_secret_length = 0;
  441. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  442. mbedtls_ecdh_init( &ecdh );
  443. mbedtls_ecp_keypair_init( &our_key );
  444. mbedtls_ecp_keypair_init( &their_key );
  445. if( ! load_private_key( grp_id, our_private_key, &our_key, &rnd_info ) )
  446. goto exit;
  447. if( ! load_public_key( grp_id, their_point, &their_key ) )
  448. goto exit;
  449. /* Import the keys to the ECDH calculation. */
  450. if( ours_first )
  451. {
  452. TEST_ASSERT( mbedtls_ecdh_get_params(
  453. &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
  454. TEST_ASSERT( mbedtls_ecdh_get_params(
  455. &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
  456. }
  457. else
  458. {
  459. TEST_ASSERT( mbedtls_ecdh_get_params(
  460. &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
  461. TEST_ASSERT( mbedtls_ecdh_get_params(
  462. &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
  463. }
  464. /* Perform the ECDH calculation. */
  465. TEST_ASSERT( mbedtls_ecdh_calc_secret(
  466. &ecdh,
  467. &shared_secret_length,
  468. shared_secret, sizeof( shared_secret ),
  469. &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
  470. TEST_ASSERT( shared_secret_length == expected->len );
  471. TEST_ASSERT( memcmp( expected->x, shared_secret,
  472. shared_secret_length ) == 0 );
  473. exit:
  474. mbedtls_ecdh_free( &ecdh );
  475. mbedtls_ecp_keypair_free( &our_key );
  476. mbedtls_ecp_keypair_free( &their_key );
  477. }
  478. /* END_CASE */
  479. /* BEGIN_CASE */
  480. void ecdh_exchange_get_params_fail( int our_grp_id,
  481. data_t *our_private_key,
  482. int their_grp_id,
  483. data_t *their_point,
  484. int ours_first,
  485. int expected_ret )
  486. {
  487. mbedtls_test_rnd_pseudo_info rnd_info;
  488. mbedtls_ecp_keypair our_key;
  489. mbedtls_ecp_keypair their_key;
  490. mbedtls_ecdh_context ecdh;
  491. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  492. mbedtls_ecdh_init( &ecdh );
  493. mbedtls_ecp_keypair_init( &our_key );
  494. mbedtls_ecp_keypair_init( &their_key );
  495. if( ! load_private_key( our_grp_id, our_private_key, &our_key, &rnd_info ) )
  496. goto exit;
  497. if( ! load_public_key( their_grp_id, their_point, &their_key ) )
  498. goto exit;
  499. if( ours_first )
  500. {
  501. TEST_ASSERT( mbedtls_ecdh_get_params(
  502. &ecdh, &our_key, MBEDTLS_ECDH_OURS ) == 0 );
  503. TEST_ASSERT( mbedtls_ecdh_get_params(
  504. &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) ==
  505. expected_ret );
  506. }
  507. else
  508. {
  509. TEST_ASSERT( mbedtls_ecdh_get_params(
  510. &ecdh, &their_key, MBEDTLS_ECDH_THEIRS ) == 0 );
  511. TEST_ASSERT( mbedtls_ecdh_get_params(
  512. &ecdh, &our_key, MBEDTLS_ECDH_OURS ) ==
  513. expected_ret );
  514. }
  515. exit:
  516. mbedtls_ecdh_free( &ecdh );
  517. mbedtls_ecp_keypair_free( &our_key );
  518. mbedtls_ecp_keypair_free( &their_key );
  519. }
  520. /* END_CASE */