test_suite_ecp.function 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/ecp.h"
  3. #include "ecp_invasive.h"
  4. #if defined(MBEDTLS_TEST_HOOKS) && \
  5. ( defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
  6. defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
  7. defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) )
  8. #define HAVE_FIX_NEGATIVE
  9. #endif
  10. #define ECP_PF_UNKNOWN -1
  11. #define ECP_PT_RESET( x ) \
  12. mbedtls_ecp_point_free( x ); \
  13. mbedtls_ecp_point_init( x );
  14. /* END_HEADER */
  15. /* BEGIN_DEPENDENCIES
  16. * depends_on:MBEDTLS_ECP_C
  17. * END_DEPENDENCIES
  18. */
  19. /* BEGIN_CASE */
  20. void ecp_valid_param( )
  21. {
  22. TEST_VALID_PARAM( mbedtls_ecp_group_free( NULL ) );
  23. TEST_VALID_PARAM( mbedtls_ecp_keypair_free( NULL ) );
  24. TEST_VALID_PARAM( mbedtls_ecp_point_free( NULL ) );
  25. #if defined(MBEDTLS_ECP_RESTARTABLE)
  26. TEST_VALID_PARAM( mbedtls_ecp_restart_free( NULL ) );
  27. #endif /* MBEDTLS_ECP_RESTARTABLE */
  28. exit:
  29. return;
  30. }
  31. /* END_CASE */
  32. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  33. void ecp_invalid_param( )
  34. {
  35. mbedtls_ecp_group grp;
  36. mbedtls_ecp_keypair kp;
  37. mbedtls_ecp_point P;
  38. mbedtls_mpi m;
  39. const char *x = "deadbeef";
  40. int valid_fmt = MBEDTLS_ECP_PF_UNCOMPRESSED;
  41. int invalid_fmt = 42;
  42. size_t olen;
  43. unsigned char buf[42] = { 0 };
  44. const unsigned char *null_buf = NULL;
  45. mbedtls_ecp_group_id valid_group = MBEDTLS_ECP_DP_SECP192R1;
  46. #if defined(MBEDTLS_ECP_RESTARTABLE)
  47. mbedtls_ecp_restart_ctx restart_ctx;
  48. #endif /* MBEDTLS_ECP_RESTARTABLE */
  49. TEST_INVALID_PARAM( mbedtls_ecp_point_init( NULL ) );
  50. TEST_INVALID_PARAM( mbedtls_ecp_keypair_init( NULL ) );
  51. TEST_INVALID_PARAM( mbedtls_ecp_group_init( NULL ) );
  52. #if defined(MBEDTLS_ECP_RESTARTABLE)
  53. TEST_INVALID_PARAM( mbedtls_ecp_restart_init( NULL ) );
  54. TEST_INVALID_PARAM( mbedtls_ecp_check_budget( NULL, &restart_ctx, 42 ) );
  55. #endif /* MBEDTLS_ECP_RESTARTABLE */
  56. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  57. mbedtls_ecp_copy( NULL, &P ) );
  58. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  59. mbedtls_ecp_copy( &P, NULL ) );
  60. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  61. mbedtls_ecp_group_copy( NULL, &grp ) );
  62. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  63. mbedtls_ecp_group_copy( &grp, NULL ) );
  64. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  65. mbedtls_ecp_gen_privkey( NULL,
  66. &m,
  67. mbedtls_test_rnd_std_rand,
  68. NULL ) );
  69. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  70. mbedtls_ecp_gen_privkey( &grp,
  71. NULL,
  72. mbedtls_test_rnd_std_rand,
  73. NULL ) );
  74. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  75. mbedtls_ecp_gen_privkey( &grp,
  76. &m,
  77. NULL,
  78. NULL ) );
  79. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  80. mbedtls_ecp_set_zero( NULL ) );
  81. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  82. mbedtls_ecp_is_zero( NULL ) );
  83. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  84. mbedtls_ecp_point_cmp( NULL, &P ) );
  85. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  86. mbedtls_ecp_point_cmp( &P, NULL ) );
  87. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  88. mbedtls_ecp_point_read_string( NULL, 2,
  89. x, x ) );
  90. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  91. mbedtls_ecp_point_read_string( &P, 2,
  92. NULL, x ) );
  93. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  94. mbedtls_ecp_point_read_string( &P, 2,
  95. x, NULL ) );
  96. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  97. mbedtls_ecp_point_write_binary( NULL, &P,
  98. valid_fmt,
  99. &olen,
  100. buf, sizeof( buf ) ) );
  101. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  102. mbedtls_ecp_point_write_binary( &grp, NULL,
  103. valid_fmt,
  104. &olen,
  105. buf, sizeof( buf ) ) );
  106. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  107. mbedtls_ecp_point_write_binary( &grp, &P,
  108. invalid_fmt,
  109. &olen,
  110. buf, sizeof( buf ) ) );
  111. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  112. mbedtls_ecp_point_write_binary( &grp, &P,
  113. valid_fmt,
  114. NULL,
  115. buf, sizeof( buf ) ) );
  116. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  117. mbedtls_ecp_point_write_binary( &grp, &P,
  118. valid_fmt,
  119. &olen,
  120. NULL, sizeof( buf ) ) );
  121. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  122. mbedtls_ecp_point_read_binary( NULL, &P, buf,
  123. sizeof( buf ) ) );
  124. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  125. mbedtls_ecp_point_read_binary( &grp, NULL, buf,
  126. sizeof( buf ) ) );
  127. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  128. mbedtls_ecp_point_read_binary( &grp, &P, NULL,
  129. sizeof( buf ) ) );
  130. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  131. mbedtls_ecp_tls_read_point( NULL, &P,
  132. (const unsigned char **) &buf,
  133. sizeof( buf ) ) );
  134. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  135. mbedtls_ecp_tls_read_point( &grp, NULL,
  136. (const unsigned char **) &buf,
  137. sizeof( buf ) ) );
  138. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  139. mbedtls_ecp_tls_read_point( &grp, &P, &null_buf,
  140. sizeof( buf ) ) );
  141. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  142. mbedtls_ecp_tls_read_point( &grp, &P, NULL,
  143. sizeof( buf ) ) );
  144. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  145. mbedtls_ecp_tls_write_point( NULL, &P,
  146. valid_fmt,
  147. &olen,
  148. buf,
  149. sizeof( buf ) ) );
  150. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  151. mbedtls_ecp_tls_write_point( &grp, NULL,
  152. valid_fmt,
  153. &olen,
  154. buf,
  155. sizeof( buf ) ) );
  156. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  157. mbedtls_ecp_tls_write_point( &grp, &P,
  158. invalid_fmt,
  159. &olen,
  160. buf,
  161. sizeof( buf ) ) );
  162. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  163. mbedtls_ecp_tls_write_point( &grp, &P,
  164. valid_fmt,
  165. NULL,
  166. buf,
  167. sizeof( buf ) ) );
  168. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  169. mbedtls_ecp_tls_write_point( &grp, &P,
  170. valid_fmt,
  171. &olen,
  172. NULL,
  173. sizeof( buf ) ) );
  174. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  175. mbedtls_ecp_group_load( NULL, valid_group ) );
  176. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  177. mbedtls_ecp_tls_read_group( NULL,
  178. (const unsigned char **) &buf,
  179. sizeof( buf ) ) );
  180. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  181. mbedtls_ecp_tls_read_group( &grp, NULL,
  182. sizeof( buf ) ) );
  183. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  184. mbedtls_ecp_tls_read_group( &grp, &null_buf,
  185. sizeof( buf ) ) );
  186. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  187. mbedtls_ecp_tls_read_group_id( NULL,
  188. (const unsigned char **) &buf,
  189. sizeof( buf ) ) );
  190. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  191. mbedtls_ecp_tls_read_group_id( &valid_group, NULL,
  192. sizeof( buf ) ) );
  193. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  194. mbedtls_ecp_tls_read_group_id( &valid_group,
  195. &null_buf,
  196. sizeof( buf ) ) );
  197. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  198. mbedtls_ecp_tls_write_group( NULL, &olen,
  199. buf, sizeof( buf ) ) );
  200. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  201. mbedtls_ecp_tls_write_group( &grp, NULL,
  202. buf, sizeof( buf ) ) );
  203. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  204. mbedtls_ecp_tls_write_group( &grp, &olen,
  205. NULL, sizeof( buf ) ) );
  206. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  207. mbedtls_ecp_mul( NULL, &P, &m, &P,
  208. mbedtls_test_rnd_std_rand,
  209. NULL ) );
  210. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  211. mbedtls_ecp_mul( &grp, NULL, &m, &P,
  212. mbedtls_test_rnd_std_rand,
  213. NULL ) );
  214. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  215. mbedtls_ecp_mul( &grp, &P, NULL, &P,
  216. mbedtls_test_rnd_std_rand,
  217. NULL ) );
  218. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  219. mbedtls_ecp_mul( &grp, &P, &m, NULL,
  220. mbedtls_test_rnd_std_rand,
  221. NULL ) );
  222. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  223. mbedtls_ecp_mul_restartable( NULL, &P, &m, &P,
  224. mbedtls_test_rnd_std_rand,
  225. NULL , NULL ) );
  226. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  227. mbedtls_ecp_mul_restartable( &grp, NULL, &m, &P,
  228. mbedtls_test_rnd_std_rand,
  229. NULL , NULL ) );
  230. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  231. mbedtls_ecp_mul_restartable( &grp, &P, NULL, &P,
  232. mbedtls_test_rnd_std_rand,
  233. NULL , NULL ) );
  234. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  235. mbedtls_ecp_mul_restartable( &grp, &P, &m, NULL,
  236. mbedtls_test_rnd_std_rand,
  237. NULL , NULL ) );
  238. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  239. mbedtls_ecp_muladd( NULL, &P, &m, &P,
  240. &m, &P ) );
  241. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  242. mbedtls_ecp_muladd( &grp, NULL, &m, &P,
  243. &m, &P ) );
  244. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  245. mbedtls_ecp_muladd( &grp, &P, NULL, &P,
  246. &m, &P ) );
  247. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  248. mbedtls_ecp_muladd( &grp, &P, &m, NULL,
  249. &m, &P ) );
  250. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  251. mbedtls_ecp_muladd( &grp, &P, &m, &P,
  252. NULL, &P ) );
  253. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  254. mbedtls_ecp_muladd( &grp, &P, &m, &P,
  255. &m, NULL ) );
  256. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  257. mbedtls_ecp_muladd_restartable( NULL, &P, &m, &P,
  258. &m, &P, NULL ) );
  259. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  260. mbedtls_ecp_muladd_restartable( &grp, NULL, &m, &P,
  261. &m, &P, NULL ) );
  262. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  263. mbedtls_ecp_muladd_restartable( &grp, &P, NULL, &P,
  264. &m, &P, NULL ) );
  265. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  266. mbedtls_ecp_muladd_restartable( &grp, &P, &m, NULL,
  267. &m, &P, NULL ) );
  268. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  269. mbedtls_ecp_muladd_restartable( &grp, &P, &m, &P,
  270. NULL, &P, NULL ) );
  271. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  272. mbedtls_ecp_muladd_restartable( &grp, &P, &m, &P,
  273. &m, NULL, NULL ) );
  274. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  275. mbedtls_ecp_check_pubkey( NULL, &P ) );
  276. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  277. mbedtls_ecp_check_pubkey( &grp, NULL ) );
  278. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  279. mbedtls_ecp_check_pub_priv( NULL, &kp ) );
  280. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  281. mbedtls_ecp_check_pub_priv( &kp, NULL ) );
  282. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  283. mbedtls_ecp_check_privkey( NULL, &m ) );
  284. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  285. mbedtls_ecp_check_privkey( &grp, NULL ) );
  286. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  287. mbedtls_ecp_gen_keypair_base( NULL, &P, &m, &P,
  288. mbedtls_test_rnd_std_rand, NULL ) );
  289. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  290. mbedtls_ecp_gen_keypair_base( &grp, NULL, &m, &P,
  291. mbedtls_test_rnd_std_rand, NULL ) );
  292. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  293. mbedtls_ecp_gen_keypair_base( &grp, &P, NULL, &P,
  294. mbedtls_test_rnd_std_rand, NULL ) );
  295. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  296. mbedtls_ecp_gen_keypair_base( &grp, &P, &m, NULL,
  297. mbedtls_test_rnd_std_rand, NULL ) );
  298. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  299. mbedtls_ecp_gen_keypair_base( &grp, &P, &m, &P, NULL, NULL ) );
  300. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  301. mbedtls_ecp_gen_keypair( NULL,
  302. &m, &P,
  303. mbedtls_test_rnd_std_rand,
  304. NULL ) );
  305. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  306. mbedtls_ecp_gen_keypair( &grp,
  307. NULL, &P,
  308. mbedtls_test_rnd_std_rand,
  309. NULL ) );
  310. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  311. mbedtls_ecp_gen_keypair( &grp,
  312. &m, NULL,
  313. mbedtls_test_rnd_std_rand,
  314. NULL ) );
  315. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  316. mbedtls_ecp_gen_keypair( &grp,
  317. &m, &P,
  318. NULL,
  319. NULL ) );
  320. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  321. mbedtls_ecp_gen_key( valid_group, NULL,
  322. mbedtls_test_rnd_std_rand,
  323. NULL ) );
  324. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  325. mbedtls_ecp_gen_key( valid_group, &kp,
  326. NULL, NULL ) );
  327. exit:
  328. return;
  329. }
  330. /* END_CASE */
  331. /* BEGIN_CASE */
  332. void mbedtls_ecp_curve_info( int id, int tls_id, int size, char * name )
  333. {
  334. const mbedtls_ecp_curve_info *by_id, *by_tls, *by_name;
  335. by_id = mbedtls_ecp_curve_info_from_grp_id( id );
  336. by_tls = mbedtls_ecp_curve_info_from_tls_id( tls_id );
  337. by_name = mbedtls_ecp_curve_info_from_name( name );
  338. TEST_ASSERT( by_id != NULL );
  339. TEST_ASSERT( by_tls != NULL );
  340. TEST_ASSERT( by_name != NULL );
  341. TEST_ASSERT( by_id == by_tls );
  342. TEST_ASSERT( by_id == by_name );
  343. TEST_ASSERT( by_id->bit_size == size );
  344. TEST_ASSERT( size <= MBEDTLS_ECP_MAX_BITS );
  345. TEST_ASSERT( size <= MBEDTLS_ECP_MAX_BYTES * 8 );
  346. }
  347. /* END_CASE */
  348. /* BEGIN_CASE */
  349. void ecp_check_pub( int grp_id, char * x_hex, char * y_hex, char * z_hex,
  350. int ret )
  351. {
  352. mbedtls_ecp_group grp;
  353. mbedtls_ecp_point P;
  354. mbedtls_ecp_group_init( &grp );
  355. mbedtls_ecp_point_init( &P );
  356. TEST_ASSERT( mbedtls_ecp_group_load( &grp, grp_id ) == 0 );
  357. TEST_ASSERT( mbedtls_test_read_mpi( &P.X, 16, x_hex ) == 0 );
  358. TEST_ASSERT( mbedtls_test_read_mpi( &P.Y, 16, y_hex ) == 0 );
  359. TEST_ASSERT( mbedtls_test_read_mpi( &P.Z, 16, z_hex ) == 0 );
  360. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &P ) == ret );
  361. exit:
  362. mbedtls_ecp_group_free( &grp );
  363. mbedtls_ecp_point_free( &P );
  364. }
  365. /* END_CASE */
  366. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  367. void ecp_test_vect_restart( int id,
  368. char *dA_str, char *xA_str, char *yA_str,
  369. char *dB_str, char *xZ_str, char *yZ_str,
  370. int max_ops, int min_restarts, int max_restarts )
  371. {
  372. /*
  373. * Test for early restart. Based on test vectors like ecp_test_vect(),
  374. * but for the sake of simplicity only does half of each side. It's
  375. * important to test both base point and random point, though, as memory
  376. * management is different in each case.
  377. *
  378. * Don't try using too precise bounds for restarts as the exact number
  379. * will depend on settings such as MBEDTLS_ECP_FIXED_POINT_OPTIM and
  380. * MBEDTLS_ECP_WINDOW_SIZE, as well as implementation details that may
  381. * change in the future. A factor 2 is a minimum safety margin.
  382. *
  383. * For reference, with mbed TLS 2.4 and default settings, for P-256:
  384. * - Random point mult: ~3250M
  385. * - Cold base point mult: ~3300M
  386. * - Hot base point mult: ~1100M
  387. * With MBEDTLS_ECP_WINDOW_SIZE set to 2 (minimum):
  388. * - Random point mult: ~3850M
  389. */
  390. mbedtls_ecp_restart_ctx ctx;
  391. mbedtls_ecp_group grp;
  392. mbedtls_ecp_point R, P;
  393. mbedtls_mpi dA, xA, yA, dB, xZ, yZ;
  394. int cnt_restarts;
  395. int ret;
  396. mbedtls_ecp_restart_init( &ctx );
  397. mbedtls_ecp_group_init( &grp );
  398. mbedtls_ecp_point_init( &R ); mbedtls_ecp_point_init( &P );
  399. mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA );
  400. mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ );
  401. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  402. TEST_ASSERT( mbedtls_test_read_mpi( &dA, 16, dA_str ) == 0 );
  403. TEST_ASSERT( mbedtls_test_read_mpi( &xA, 16, xA_str ) == 0 );
  404. TEST_ASSERT( mbedtls_test_read_mpi( &yA, 16, yA_str ) == 0 );
  405. TEST_ASSERT( mbedtls_test_read_mpi( &dB, 16, dB_str ) == 0 );
  406. TEST_ASSERT( mbedtls_test_read_mpi( &xZ, 16, xZ_str ) == 0 );
  407. TEST_ASSERT( mbedtls_test_read_mpi( &yZ, 16, yZ_str ) == 0 );
  408. mbedtls_ecp_set_max_ops( (unsigned) max_ops );
  409. /* Base point case */
  410. cnt_restarts = 0;
  411. do {
  412. ECP_PT_RESET( &R );
  413. ret = mbedtls_ecp_mul_restartable( &grp, &R, &dA, &grp.G, NULL, NULL, &ctx );
  414. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
  415. TEST_ASSERT( ret == 0 );
  416. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
  417. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 );
  418. TEST_ASSERT( cnt_restarts >= min_restarts );
  419. TEST_ASSERT( cnt_restarts <= max_restarts );
  420. /* Non-base point case */
  421. mbedtls_ecp_copy( &P, &R );
  422. cnt_restarts = 0;
  423. do {
  424. ECP_PT_RESET( &R );
  425. ret = mbedtls_ecp_mul_restartable( &grp, &R, &dB, &P, NULL, NULL, &ctx );
  426. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
  427. TEST_ASSERT( ret == 0 );
  428. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
  429. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
  430. TEST_ASSERT( cnt_restarts >= min_restarts );
  431. TEST_ASSERT( cnt_restarts <= max_restarts );
  432. /* Do we leak memory when aborting an operation?
  433. * This test only makes sense when we actually restart */
  434. if( min_restarts > 0 )
  435. {
  436. ret = mbedtls_ecp_mul_restartable( &grp, &R, &dB, &P, NULL, NULL, &ctx );
  437. TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
  438. }
  439. exit:
  440. mbedtls_ecp_restart_free( &ctx );
  441. mbedtls_ecp_group_free( &grp );
  442. mbedtls_ecp_point_free( &R ); mbedtls_ecp_point_free( &P );
  443. mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA );
  444. mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ );
  445. }
  446. /* END_CASE */
  447. /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
  448. void ecp_muladd_restart( int id, char *xR_str, char *yR_str,
  449. char *u1_str, char *u2_str,
  450. char *xQ_str, char *yQ_str,
  451. int max_ops, int min_restarts, int max_restarts )
  452. {
  453. /*
  454. * Compute R = u1 * G + u2 * Q
  455. * (test vectors mostly taken from ECDSA intermediate results)
  456. *
  457. * See comments at the top of ecp_test_vect_restart()
  458. */
  459. mbedtls_ecp_restart_ctx ctx;
  460. mbedtls_ecp_group grp;
  461. mbedtls_ecp_point R, Q;
  462. mbedtls_mpi u1, u2, xR, yR;
  463. int cnt_restarts;
  464. int ret;
  465. mbedtls_ecp_restart_init( &ctx );
  466. mbedtls_ecp_group_init( &grp );
  467. mbedtls_ecp_point_init( &R );
  468. mbedtls_ecp_point_init( &Q );
  469. mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
  470. mbedtls_mpi_init( &xR ); mbedtls_mpi_init( &yR );
  471. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  472. TEST_ASSERT( mbedtls_test_read_mpi( &u1, 16, u1_str ) == 0 );
  473. TEST_ASSERT( mbedtls_test_read_mpi( &u2, 16, u2_str ) == 0 );
  474. TEST_ASSERT( mbedtls_test_read_mpi( &xR, 16, xR_str ) == 0 );
  475. TEST_ASSERT( mbedtls_test_read_mpi( &yR, 16, yR_str ) == 0 );
  476. TEST_ASSERT( mbedtls_test_read_mpi( &Q.X, 16, xQ_str ) == 0 );
  477. TEST_ASSERT( mbedtls_test_read_mpi( &Q.Y, 16, yQ_str ) == 0 );
  478. TEST_ASSERT( mbedtls_mpi_lset( &Q.Z, 1 ) == 0 );
  479. mbedtls_ecp_set_max_ops( (unsigned) max_ops );
  480. cnt_restarts = 0;
  481. do {
  482. ECP_PT_RESET( &R );
  483. ret = mbedtls_ecp_muladd_restartable( &grp, &R,
  484. &u1, &grp.G, &u2, &Q, &ctx );
  485. } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restarts );
  486. TEST_ASSERT( ret == 0 );
  487. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xR ) == 0 );
  488. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yR ) == 0 );
  489. TEST_ASSERT( cnt_restarts >= min_restarts );
  490. TEST_ASSERT( cnt_restarts <= max_restarts );
  491. /* Do we leak memory when aborting an operation?
  492. * This test only makes sense when we actually restart */
  493. if( min_restarts > 0 )
  494. {
  495. ret = mbedtls_ecp_muladd_restartable( &grp, &R,
  496. &u1, &grp.G, &u2, &Q, &ctx );
  497. TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS );
  498. }
  499. exit:
  500. mbedtls_ecp_restart_free( &ctx );
  501. mbedtls_ecp_group_free( &grp );
  502. mbedtls_ecp_point_free( &R );
  503. mbedtls_ecp_point_free( &Q );
  504. mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
  505. mbedtls_mpi_free( &xR ); mbedtls_mpi_free( &yR );
  506. }
  507. /* END_CASE */
  508. /* BEGIN_CASE */
  509. void ecp_test_vect( int id, char * dA_str, char * xA_str, char * yA_str,
  510. char * dB_str, char * xB_str, char * yB_str,
  511. char * xZ_str, char * yZ_str )
  512. {
  513. mbedtls_ecp_group grp;
  514. mbedtls_ecp_point R;
  515. mbedtls_mpi dA, xA, yA, dB, xB, yB, xZ, yZ;
  516. mbedtls_test_rnd_pseudo_info rnd_info;
  517. mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
  518. mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA ); mbedtls_mpi_init( &yA ); mbedtls_mpi_init( &dB );
  519. mbedtls_mpi_init( &xB ); mbedtls_mpi_init( &yB ); mbedtls_mpi_init( &xZ ); mbedtls_mpi_init( &yZ );
  520. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  521. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  522. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
  523. TEST_ASSERT( mbedtls_test_read_mpi( &dA, 16, dA_str ) == 0 );
  524. TEST_ASSERT( mbedtls_test_read_mpi( &xA, 16, xA_str ) == 0 );
  525. TEST_ASSERT( mbedtls_test_read_mpi( &yA, 16, yA_str ) == 0 );
  526. TEST_ASSERT( mbedtls_test_read_mpi( &dB, 16, dB_str ) == 0 );
  527. TEST_ASSERT( mbedtls_test_read_mpi( &xB, 16, xB_str ) == 0 );
  528. TEST_ASSERT( mbedtls_test_read_mpi( &yB, 16, yB_str ) == 0 );
  529. TEST_ASSERT( mbedtls_test_read_mpi( &xZ, 16, xZ_str ) == 0 );
  530. TEST_ASSERT( mbedtls_test_read_mpi( &yZ, 16, yZ_str ) == 0 );
  531. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G,
  532. &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
  533. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
  534. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yA ) == 0 );
  535. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
  536. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R, NULL, NULL ) == 0 );
  537. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
  538. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
  539. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
  540. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
  541. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 );
  542. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yB ) == 0 );
  543. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
  544. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R,
  545. &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
  546. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xZ ) == 0 );
  547. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.Y, &yZ ) == 0 );
  548. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
  549. exit:
  550. mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
  551. mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA ); mbedtls_mpi_free( &yA ); mbedtls_mpi_free( &dB );
  552. mbedtls_mpi_free( &xB ); mbedtls_mpi_free( &yB ); mbedtls_mpi_free( &xZ ); mbedtls_mpi_free( &yZ );
  553. }
  554. /* END_CASE */
  555. /* BEGIN_CASE */
  556. void ecp_test_vec_x( int id, char * dA_hex, char * xA_hex, char * dB_hex,
  557. char * xB_hex, char * xS_hex )
  558. {
  559. mbedtls_ecp_group grp;
  560. mbedtls_ecp_point R;
  561. mbedtls_mpi dA, xA, dB, xB, xS;
  562. mbedtls_test_rnd_pseudo_info rnd_info;
  563. mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
  564. mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &xA );
  565. mbedtls_mpi_init( &dB ); mbedtls_mpi_init( &xB );
  566. mbedtls_mpi_init( &xS );
  567. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  568. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  569. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
  570. TEST_ASSERT( mbedtls_test_read_mpi( &dA, 16, dA_hex ) == 0 );
  571. TEST_ASSERT( mbedtls_test_read_mpi( &dB, 16, dB_hex ) == 0 );
  572. TEST_ASSERT( mbedtls_test_read_mpi( &xA, 16, xA_hex ) == 0 );
  573. TEST_ASSERT( mbedtls_test_read_mpi( &xB, 16, xB_hex ) == 0 );
  574. TEST_ASSERT( mbedtls_test_read_mpi( &xS, 16, xS_hex ) == 0 );
  575. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &grp.G,
  576. &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
  577. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
  578. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xA ) == 0 );
  579. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &R,
  580. &mbedtls_test_rnd_pseudo_rand, &rnd_info ) == 0 );
  581. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
  582. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 );
  583. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dB, &grp.G, NULL, NULL ) == 0 );
  584. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
  585. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xB ) == 0 );
  586. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &dA, &R, NULL, NULL ) == 0 );
  587. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &R ) == 0 );
  588. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R.X, &xS ) == 0 );
  589. exit:
  590. mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
  591. mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &xA );
  592. mbedtls_mpi_free( &dB ); mbedtls_mpi_free( &xB );
  593. mbedtls_mpi_free( &xS );
  594. }
  595. /* END_CASE */
  596. /* BEGIN_CASE */
  597. void ecp_test_mul( int id, data_t * n_hex,
  598. data_t * Px_hex, data_t * Py_hex, data_t * Pz_hex,
  599. data_t * nPx_hex, data_t * nPy_hex, data_t * nPz_hex,
  600. int expected_ret )
  601. {
  602. mbedtls_ecp_group grp;
  603. mbedtls_ecp_point P, nP, R;
  604. mbedtls_mpi n;
  605. mbedtls_test_rnd_pseudo_info rnd_info;
  606. mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &R );
  607. mbedtls_ecp_point_init( &P ); mbedtls_ecp_point_init( &nP );
  608. mbedtls_mpi_init( &n );
  609. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  610. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  611. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
  612. TEST_ASSERT( mbedtls_mpi_read_binary( &n, n_hex->x, n_hex->len ) == 0 );
  613. TEST_ASSERT( mbedtls_mpi_read_binary( &P.X, Px_hex->x, Px_hex->len ) == 0 );
  614. TEST_ASSERT( mbedtls_mpi_read_binary( &P.Y, Py_hex->x, Py_hex->len ) == 0 );
  615. TEST_ASSERT( mbedtls_mpi_read_binary( &P.Z, Pz_hex->x, Pz_hex->len ) == 0 );
  616. TEST_ASSERT( mbedtls_mpi_read_binary( &nP.X, nPx_hex->x, nPx_hex->len )
  617. == 0 );
  618. TEST_ASSERT( mbedtls_mpi_read_binary( &nP.Y, nPy_hex->x, nPy_hex->len )
  619. == 0 );
  620. TEST_ASSERT( mbedtls_mpi_read_binary( &nP.Z, nPz_hex->x, nPz_hex->len )
  621. == 0 );
  622. TEST_ASSERT( mbedtls_ecp_mul( &grp, &R, &n, &P,
  623. &mbedtls_test_rnd_pseudo_rand, &rnd_info )
  624. == expected_ret );
  625. if( expected_ret == 0 )
  626. {
  627. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.X, &R.X ) == 0 );
  628. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.Y, &R.Y ) == 0 );
  629. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &nP.Z, &R.Z ) == 0 );
  630. }
  631. exit:
  632. mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &R );
  633. mbedtls_ecp_point_free( &P ); mbedtls_ecp_point_free( &nP );
  634. mbedtls_mpi_free( &n );
  635. }
  636. /* END_CASE */
  637. /* BEGIN_CASE */
  638. void ecp_test_mul_rng( int id, data_t * d_hex)
  639. {
  640. mbedtls_ecp_group grp;
  641. mbedtls_mpi d;
  642. mbedtls_ecp_point Q;
  643. mbedtls_ecp_group_init( &grp ); mbedtls_mpi_init( &d );
  644. mbedtls_ecp_point_init( &Q );
  645. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  646. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &grp.G ) == 0 );
  647. TEST_ASSERT( mbedtls_mpi_read_binary( &d, d_hex->x, d_hex->len ) == 0 );
  648. TEST_ASSERT( mbedtls_ecp_mul( &grp, &Q, &d, &grp.G,
  649. &mbedtls_test_rnd_zero_rand, NULL )
  650. == MBEDTLS_ERR_ECP_RANDOM_FAILED );
  651. exit:
  652. mbedtls_ecp_group_free( &grp ); mbedtls_mpi_free( &d );
  653. mbedtls_ecp_point_free( &Q );
  654. }
  655. /* END_CASE */
  656. /* BEGIN_CASE depends_on:MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
  657. void ecp_muladd( int id,
  658. data_t *u1_bin, data_t *P1_bin,
  659. data_t *u2_bin, data_t *P2_bin,
  660. data_t *expected_result )
  661. {
  662. /* Compute R = u1 * P1 + u2 * P2 */
  663. mbedtls_ecp_group grp;
  664. mbedtls_ecp_point P1, P2, R;
  665. mbedtls_mpi u1, u2;
  666. uint8_t actual_result[MBEDTLS_ECP_MAX_PT_LEN];
  667. size_t len;
  668. mbedtls_ecp_group_init( &grp );
  669. mbedtls_ecp_point_init( &P1 );
  670. mbedtls_ecp_point_init( &P2 );
  671. mbedtls_ecp_point_init( &R );
  672. mbedtls_mpi_init( &u1 );
  673. mbedtls_mpi_init( &u2 );
  674. TEST_EQUAL( 0, mbedtls_ecp_group_load( &grp, id ) );
  675. TEST_EQUAL( 0, mbedtls_mpi_read_binary( &u1, u1_bin->x, u1_bin->len ) );
  676. TEST_EQUAL( 0, mbedtls_mpi_read_binary( &u2, u2_bin->x, u2_bin->len ) );
  677. TEST_EQUAL( 0, mbedtls_ecp_point_read_binary( &grp, &P1,
  678. P1_bin->x, P1_bin->len ) );
  679. TEST_EQUAL( 0, mbedtls_ecp_point_read_binary( &grp, &P2,
  680. P2_bin->x, P2_bin->len ) );
  681. TEST_EQUAL( 0, mbedtls_ecp_muladd( &grp, &R, &u1, &P1, &u2, &P2 ) );
  682. TEST_EQUAL( 0, mbedtls_ecp_point_write_binary(
  683. &grp, &R, MBEDTLS_ECP_PF_UNCOMPRESSED,
  684. &len, actual_result, sizeof( actual_result ) ) );
  685. TEST_ASSERT( len <= MBEDTLS_ECP_MAX_PT_LEN );
  686. ASSERT_COMPARE( expected_result->x, expected_result->len,
  687. actual_result, len );
  688. exit:
  689. mbedtls_ecp_group_free( &grp );
  690. mbedtls_ecp_point_free( &P1 );
  691. mbedtls_ecp_point_free( &P2 );
  692. mbedtls_ecp_point_free( &R );
  693. mbedtls_mpi_free( &u1 );
  694. mbedtls_mpi_free( &u2 );
  695. }
  696. /* END_CASE */
  697. /* BEGIN_CASE */
  698. void ecp_fast_mod( int id, char * N_str )
  699. {
  700. mbedtls_ecp_group grp;
  701. mbedtls_mpi N, R;
  702. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &R );
  703. mbedtls_ecp_group_init( &grp );
  704. TEST_ASSERT( mbedtls_test_read_mpi( &N, 16, N_str ) == 0 );
  705. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  706. TEST_ASSERT( grp.modp != NULL );
  707. /*
  708. * Store correct result before we touch N
  709. */
  710. TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &N, &grp.P ) == 0 );
  711. TEST_ASSERT( grp.modp( &N ) == 0 );
  712. TEST_ASSERT( mbedtls_mpi_bitlen( &N ) <= grp.pbits + 3 );
  713. /*
  714. * Use mod rather than addition/subtraction in case previous test fails
  715. */
  716. TEST_ASSERT( mbedtls_mpi_mod_mpi( &N, &N, &grp.P ) == 0 );
  717. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &R ) == 0 );
  718. exit:
  719. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &R );
  720. mbedtls_ecp_group_free( &grp );
  721. }
  722. /* END_CASE */
  723. /* BEGIN_CASE */
  724. void ecp_write_binary( int id, char * x, char * y, char * z, int format,
  725. data_t * out, int blen, int ret )
  726. {
  727. mbedtls_ecp_group grp;
  728. mbedtls_ecp_point P;
  729. unsigned char buf[256];
  730. size_t olen;
  731. memset( buf, 0, sizeof( buf ) );
  732. mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
  733. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  734. TEST_ASSERT( mbedtls_test_read_mpi( &P.X, 16, x ) == 0 );
  735. TEST_ASSERT( mbedtls_test_read_mpi( &P.Y, 16, y ) == 0 );
  736. TEST_ASSERT( mbedtls_test_read_mpi( &P.Z, 16, z ) == 0 );
  737. TEST_ASSERT( mbedtls_ecp_point_write_binary( &grp, &P, format,
  738. &olen, buf, blen ) == ret );
  739. if( ret == 0 )
  740. {
  741. TEST_ASSERT( olen <= MBEDTLS_ECP_MAX_PT_LEN );
  742. TEST_ASSERT( mbedtls_test_hexcmp( buf, out->x, olen, out->len ) == 0 );
  743. }
  744. exit:
  745. mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
  746. }
  747. /* END_CASE */
  748. /* BEGIN_CASE */
  749. void ecp_read_binary( int id, data_t * buf, char * x, char * y, char * z,
  750. int ret )
  751. {
  752. mbedtls_ecp_group grp;
  753. mbedtls_ecp_point P;
  754. mbedtls_mpi X, Y, Z;
  755. mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
  756. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  757. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  758. TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x ) == 0 );
  759. TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y ) == 0 );
  760. TEST_ASSERT( mbedtls_test_read_mpi( &Z, 16, z ) == 0 );
  761. TEST_ASSERT( mbedtls_ecp_point_read_binary( &grp, &P, buf->x, buf->len ) == ret );
  762. if( ret == 0 )
  763. {
  764. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 );
  765. if( mbedtls_ecp_get_type( &grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
  766. {
  767. TEST_ASSERT( mbedtls_mpi_cmp_int( &Y, 0 ) == 0 );
  768. TEST_ASSERT( P.Y.p == NULL );
  769. TEST_ASSERT( mbedtls_mpi_cmp_int( &Z, 1 ) == 0 );
  770. TEST_ASSERT( mbedtls_mpi_cmp_int( &P.Z, 1 ) == 0 );
  771. }
  772. else
  773. {
  774. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 );
  775. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 );
  776. }
  777. }
  778. exit:
  779. mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
  780. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  781. }
  782. /* END_CASE */
  783. /* BEGIN_CASE */
  784. void mbedtls_ecp_tls_read_point( int id, data_t * buf, char * x, char * y,
  785. char * z, int ret )
  786. {
  787. mbedtls_ecp_group grp;
  788. mbedtls_ecp_point P;
  789. mbedtls_mpi X, Y, Z;
  790. const unsigned char *vbuf = buf->x;
  791. mbedtls_ecp_group_init( &grp ); mbedtls_ecp_point_init( &P );
  792. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  793. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  794. TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, x ) == 0 );
  795. TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, y ) == 0 );
  796. TEST_ASSERT( mbedtls_test_read_mpi( &Z, 16, z ) == 0 );
  797. TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &P, &vbuf, buf->len ) == ret );
  798. if( ret == 0 )
  799. {
  800. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.X, &X ) == 0 );
  801. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Y, &Y ) == 0 );
  802. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P.Z, &Z ) == 0 );
  803. TEST_ASSERT( (uint32_t)( vbuf - buf->x ) == buf->len );
  804. }
  805. exit:
  806. mbedtls_ecp_group_free( &grp ); mbedtls_ecp_point_free( &P );
  807. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  808. }
  809. /* END_CASE */
  810. /* BEGIN_CASE */
  811. void ecp_tls_write_read_point( int id )
  812. {
  813. mbedtls_ecp_group grp;
  814. mbedtls_ecp_point pt;
  815. unsigned char buf[256];
  816. const unsigned char *vbuf;
  817. size_t olen;
  818. mbedtls_ecp_group_init( &grp );
  819. mbedtls_ecp_point_init( &pt );
  820. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  821. memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
  822. TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G,
  823. MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
  824. TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen )
  825. == MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  826. TEST_ASSERT( vbuf == buf + olen );
  827. memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
  828. TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &grp.G,
  829. MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
  830. TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
  831. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 );
  832. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 );
  833. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 );
  834. TEST_ASSERT( vbuf == buf + olen );
  835. memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
  836. TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 );
  837. TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt,
  838. MBEDTLS_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 );
  839. TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
  840. TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) );
  841. TEST_ASSERT( vbuf == buf + olen );
  842. memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
  843. TEST_ASSERT( mbedtls_ecp_set_zero( &pt ) == 0 );
  844. TEST_ASSERT( mbedtls_ecp_tls_write_point( &grp, &pt,
  845. MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 );
  846. TEST_ASSERT( mbedtls_ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 );
  847. TEST_ASSERT( mbedtls_ecp_is_zero( &pt ) );
  848. TEST_ASSERT( vbuf == buf + olen );
  849. exit:
  850. mbedtls_ecp_group_free( &grp );
  851. mbedtls_ecp_point_free( &pt );
  852. }
  853. /* END_CASE */
  854. /* BEGIN_CASE */
  855. void mbedtls_ecp_tls_read_group( data_t * buf, int result, int bits,
  856. int record_len )
  857. {
  858. mbedtls_ecp_group grp;
  859. const unsigned char *vbuf = buf->x;
  860. int ret;
  861. mbedtls_ecp_group_init( &grp );
  862. ret = mbedtls_ecp_tls_read_group( &grp, &vbuf, buf->len );
  863. TEST_ASSERT( ret == result );
  864. if( ret == 0)
  865. {
  866. TEST_ASSERT( mbedtls_mpi_bitlen( &grp.P ) == (size_t) bits );
  867. TEST_ASSERT( vbuf - buf->x == record_len);
  868. }
  869. exit:
  870. mbedtls_ecp_group_free( &grp );
  871. }
  872. /* END_CASE */
  873. /* BEGIN_CASE */
  874. void ecp_tls_write_read_group( int id )
  875. {
  876. mbedtls_ecp_group grp1, grp2;
  877. unsigned char buf[10];
  878. const unsigned char *vbuf = buf;
  879. size_t len;
  880. int ret;
  881. mbedtls_ecp_group_init( &grp1 );
  882. mbedtls_ecp_group_init( &grp2 );
  883. memset( buf, 0x00, sizeof( buf ) );
  884. TEST_ASSERT( mbedtls_ecp_group_load( &grp1, id ) == 0 );
  885. TEST_ASSERT( mbedtls_ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 );
  886. ret = mbedtls_ecp_tls_read_group( &grp2, &vbuf, len );
  887. TEST_ASSERT( ret == 0 );
  888. if( ret == 0 )
  889. {
  890. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 );
  891. TEST_ASSERT( grp1.id == grp2.id );
  892. }
  893. exit:
  894. mbedtls_ecp_group_free( &grp1 );
  895. mbedtls_ecp_group_free( &grp2 );
  896. }
  897. /* END_CASE */
  898. /* BEGIN_CASE */
  899. void mbedtls_ecp_check_privkey( int id, char * key_hex, int ret )
  900. {
  901. mbedtls_ecp_group grp;
  902. mbedtls_mpi d;
  903. mbedtls_ecp_group_init( &grp );
  904. mbedtls_mpi_init( &d );
  905. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  906. TEST_ASSERT( mbedtls_test_read_mpi( &d, 16, key_hex ) == 0 );
  907. TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == ret );
  908. exit:
  909. mbedtls_ecp_group_free( &grp );
  910. mbedtls_mpi_free( &d );
  911. }
  912. /* END_CASE */
  913. /* BEGIN_CASE */
  914. void mbedtls_ecp_check_pub_priv( int id_pub, char * Qx_pub, char * Qy_pub,
  915. int id, char * d, char * Qx, char * Qy,
  916. int ret )
  917. {
  918. mbedtls_ecp_keypair pub, prv;
  919. mbedtls_ecp_keypair_init( &pub );
  920. mbedtls_ecp_keypair_init( &prv );
  921. if( id_pub != MBEDTLS_ECP_DP_NONE )
  922. TEST_ASSERT( mbedtls_ecp_group_load( &pub.grp, id_pub ) == 0 );
  923. TEST_ASSERT( mbedtls_ecp_point_read_string( &pub.Q, 16, Qx_pub, Qy_pub ) == 0 );
  924. if( id != MBEDTLS_ECP_DP_NONE )
  925. TEST_ASSERT( mbedtls_ecp_group_load( &prv.grp, id ) == 0 );
  926. TEST_ASSERT( mbedtls_ecp_point_read_string( &prv.Q, 16, Qx, Qy ) == 0 );
  927. TEST_ASSERT( mbedtls_test_read_mpi( &prv.d, 16, d ) == 0 );
  928. TEST_ASSERT( mbedtls_ecp_check_pub_priv( &pub, &prv ) == ret );
  929. exit:
  930. mbedtls_ecp_keypair_free( &pub );
  931. mbedtls_ecp_keypair_free( &prv );
  932. }
  933. /* END_CASE */
  934. /* BEGIN_CASE */
  935. void mbedtls_ecp_gen_keypair( int id )
  936. {
  937. mbedtls_ecp_group grp;
  938. mbedtls_ecp_point Q;
  939. mbedtls_mpi d;
  940. mbedtls_test_rnd_pseudo_info rnd_info;
  941. mbedtls_ecp_group_init( &grp );
  942. mbedtls_ecp_point_init( &Q );
  943. mbedtls_mpi_init( &d );
  944. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  945. TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
  946. TEST_ASSERT( mbedtls_ecp_gen_keypair( &grp, &d, &Q,
  947. &mbedtls_test_rnd_pseudo_rand,
  948. &rnd_info ) == 0 );
  949. TEST_ASSERT( mbedtls_ecp_check_pubkey( &grp, &Q ) == 0 );
  950. TEST_ASSERT( mbedtls_ecp_check_privkey( &grp, &d ) == 0 );
  951. exit:
  952. mbedtls_ecp_group_free( &grp );
  953. mbedtls_ecp_point_free( &Q );
  954. mbedtls_mpi_free( &d );
  955. }
  956. /* END_CASE */
  957. /* BEGIN_CASE */
  958. void mbedtls_ecp_gen_key( int id )
  959. {
  960. mbedtls_ecp_keypair key;
  961. mbedtls_test_rnd_pseudo_info rnd_info;
  962. mbedtls_ecp_keypair_init( &key );
  963. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  964. TEST_ASSERT( mbedtls_ecp_gen_key( id, &key,
  965. &mbedtls_test_rnd_pseudo_rand,
  966. &rnd_info ) == 0 );
  967. TEST_ASSERT( mbedtls_ecp_check_pubkey( &key.grp, &key.Q ) == 0 );
  968. TEST_ASSERT( mbedtls_ecp_check_privkey( &key.grp, &key.d ) == 0 );
  969. exit:
  970. mbedtls_ecp_keypair_free( &key );
  971. }
  972. /* END_CASE */
  973. /* BEGIN_CASE */
  974. void mbedtls_ecp_read_key( int grp_id, data_t* in_key, int expected, int canonical )
  975. {
  976. int ret = 0;
  977. mbedtls_ecp_keypair key;
  978. mbedtls_ecp_keypair key2;
  979. mbedtls_ecp_keypair_init( &key );
  980. mbedtls_ecp_keypair_init( &key2 );
  981. ret = mbedtls_ecp_read_key( grp_id, &key, in_key->x, in_key->len );
  982. TEST_ASSERT( ret == expected );
  983. if( expected == 0 )
  984. {
  985. ret = mbedtls_ecp_check_privkey( &key.grp, &key.d );
  986. TEST_ASSERT( ret == 0 );
  987. if( canonical )
  988. {
  989. unsigned char buf[MBEDTLS_ECP_MAX_BYTES];
  990. ret = mbedtls_ecp_write_key( &key, buf, in_key->len );
  991. TEST_ASSERT( ret == 0 );
  992. ASSERT_COMPARE( in_key->x, in_key->len,
  993. buf, in_key->len );
  994. }
  995. else
  996. {
  997. unsigned char export1[MBEDTLS_ECP_MAX_BYTES];
  998. unsigned char export2[MBEDTLS_ECP_MAX_BYTES];
  999. ret = mbedtls_ecp_write_key( &key, export1, in_key->len );
  1000. TEST_ASSERT( ret == 0 );
  1001. ret = mbedtls_ecp_read_key( grp_id, &key2, export1, in_key->len );
  1002. TEST_ASSERT( ret == expected );
  1003. ret = mbedtls_ecp_write_key( &key2, export2, in_key->len );
  1004. TEST_ASSERT( ret == 0 );
  1005. ASSERT_COMPARE( export1, in_key->len,
  1006. export2, in_key->len );
  1007. }
  1008. }
  1009. exit:
  1010. mbedtls_ecp_keypair_free( &key );
  1011. mbedtls_ecp_keypair_free( &key2 );
  1012. }
  1013. /* END_CASE */
  1014. /* BEGIN_CASE depends_on:HAVE_FIX_NEGATIVE */
  1015. void fix_negative( data_t *N_bin, int c, int bits )
  1016. {
  1017. mbedtls_mpi C, M, N;
  1018. mbedtls_mpi_init( &C );
  1019. mbedtls_mpi_init( &M );
  1020. mbedtls_mpi_init( &N );
  1021. /* C = - c * 2^bits (positive since c is negative) */
  1022. TEST_EQUAL( 0, mbedtls_mpi_lset( &C, -c ) );
  1023. TEST_EQUAL( 0, mbedtls_mpi_shift_l( &C, bits ) );
  1024. TEST_EQUAL( 0, mbedtls_mpi_read_binary( &N, N_bin->x, N_bin->len ) );
  1025. TEST_EQUAL( 0, mbedtls_mpi_grow( &N, C.n ) );
  1026. /* M = N - C = - ( C - N ) (expected result of fix_negative) */
  1027. TEST_EQUAL( 0, mbedtls_mpi_sub_mpi( &M, &N, &C ) );
  1028. mbedtls_ecp_fix_negative( &N, c, bits );
  1029. TEST_EQUAL( 0, mbedtls_mpi_cmp_mpi( &N, &M ) );
  1030. exit:
  1031. mbedtls_mpi_free( &C );
  1032. mbedtls_mpi_free( &M );
  1033. mbedtls_mpi_free( &N );
  1034. }
  1035. /* END_CASE */
  1036. /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_ECP_MONTGOMERY_ENABLED */
  1037. void genkey_mx_known_answer( int bits, data_t *seed, data_t *expected )
  1038. {
  1039. mbedtls_test_rnd_buf_info rnd_info;
  1040. mbedtls_mpi d;
  1041. int ret;
  1042. uint8_t *actual = NULL;
  1043. mbedtls_mpi_init( &d );
  1044. rnd_info.buf = seed->x;
  1045. rnd_info.length = seed->len;
  1046. rnd_info.fallback_f_rng = NULL;
  1047. rnd_info.fallback_p_rng = NULL;
  1048. ASSERT_ALLOC( actual, expected->len );
  1049. ret = mbedtls_ecp_gen_privkey_mx( bits, &d,
  1050. mbedtls_test_rnd_buffer_rand, &rnd_info );
  1051. if( expected->len == 0 )
  1052. {
  1053. /* Expecting an error (happens if there isn't enough randomness) */
  1054. TEST_ASSERT( ret != 0 );
  1055. }
  1056. else
  1057. {
  1058. TEST_EQUAL( ret, 0 );
  1059. TEST_EQUAL( (size_t) bits + 1, mbedtls_mpi_bitlen( &d ) );
  1060. TEST_EQUAL( 0, mbedtls_mpi_write_binary( &d, actual, expected->len ) );
  1061. /* Test the exact result. This assumes that the output of the
  1062. * RNG is used in a specific way, which is overly constraining.
  1063. * The advantage is that it's easier to test the expected properties
  1064. * of the generated key:
  1065. * - The most significant bit must be at a specific positions
  1066. * (can be enforced by checking the bit-length).
  1067. * - The least significant bits must have specific values
  1068. * (can be enforced by checking these bits).
  1069. * - Other bits must be random (by testing with different RNG outputs,
  1070. * we validate that those bits are indeed influenced by the RNG). */
  1071. ASSERT_COMPARE( expected->x, expected->len,
  1072. actual, expected->len );
  1073. }
  1074. exit:
  1075. mbedtls_free( actual );
  1076. mbedtls_mpi_free( &d );
  1077. }
  1078. /* END_CASE */
  1079. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  1080. void ecp_selftest( )
  1081. {
  1082. TEST_ASSERT( mbedtls_ecp_self_test( 1 ) == 0 );
  1083. }
  1084. /* END_CASE */