test_suite_ecp.function 44 KB

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