test_suite_ecdh.function 23 KB

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