ecp.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * Elliptic curves over GF(p): generic functions
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * References:
  25. *
  26. * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  27. * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
  28. * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
  29. * RFC 4492 for the related TLS structures and constants
  30. *
  31. * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
  32. *
  33. * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
  34. * for elliptic curve cryptosystems. In : Cryptographic Hardware and
  35. * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
  36. * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
  37. *
  38. * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
  39. * render ECC resistant against Side Channel Attacks. IACR Cryptology
  40. * ePrint Archive, 2004, vol. 2004, p. 342.
  41. * <http://eprint.iacr.org/2004/342.pdf>
  42. */
  43. #if !defined(MBEDTLS_CONFIG_FILE)
  44. #include "mbedtls/config.h"
  45. #else
  46. #include MBEDTLS_CONFIG_FILE
  47. #endif
  48. #if defined(MBEDTLS_ECP_C)
  49. #include "mbedtls/ecp.h"
  50. #include <string.h>
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdlib.h>
  55. #include <stdio.h>
  56. #define mbedtls_printf printf
  57. #define mbedtls_calloc calloc
  58. #define mbedtls_free free
  59. #endif
  60. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  61. !defined(inline) && !defined(__cplusplus)
  62. #define inline __inline
  63. #endif
  64. /* Implementation that should never be optimized out by the compiler */
  65. static void mbedtls_zeroize( void *v, size_t n ) {
  66. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  67. }
  68. #if defined(MBEDTLS_SELF_TEST)
  69. /*
  70. * Counts of point addition and doubling, and field multiplications.
  71. * Used to test resistance of point multiplication to simple timing attacks.
  72. */
  73. static unsigned long add_count, dbl_count, mul_count;
  74. #endif
  75. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
  76. defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
  77. defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
  78. defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
  79. defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
  80. defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
  81. defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
  82. defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
  83. defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
  84. defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
  85. defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  86. #define ECP_SHORTWEIERSTRASS
  87. #endif
  88. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  89. #define ECP_MONTGOMERY
  90. #endif
  91. /*
  92. * Curve types: internal for now, might be exposed later
  93. */
  94. typedef enum
  95. {
  96. ECP_TYPE_NONE = 0,
  97. ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
  98. ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
  99. } ecp_curve_type;
  100. /*
  101. * List of supported curves:
  102. * - internal ID
  103. * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
  104. * - size in bits
  105. * - readable name
  106. *
  107. * Curves are listed in order: largest curves first, and for a given size,
  108. * fastest curves first. This provides the default order for the SSL module.
  109. *
  110. * Reminder: update profiles in x509_crt.c when adding a new curves!
  111. */
  112. static const mbedtls_ecp_curve_info ecp_supported_curves[] =
  113. {
  114. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  115. { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
  116. #endif
  117. #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  118. { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
  119. #endif
  120. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  121. { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
  122. #endif
  123. #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  124. { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
  125. #endif
  126. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  127. { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
  128. #endif
  129. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  130. { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
  131. #endif
  132. #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  133. { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
  134. #endif
  135. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  136. { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
  137. #endif
  138. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  139. { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
  140. #endif
  141. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  142. { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
  143. #endif
  144. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  145. { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
  146. #endif
  147. { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
  148. };
  149. #define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
  150. sizeof( ecp_supported_curves[0] )
  151. static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
  152. /*
  153. * List of supported curves and associated info
  154. */
  155. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
  156. {
  157. return( ecp_supported_curves );
  158. }
  159. /*
  160. * List of supported curves, group ID only
  161. */
  162. const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
  163. {
  164. static int init_done = 0;
  165. if( ! init_done )
  166. {
  167. size_t i = 0;
  168. const mbedtls_ecp_curve_info *curve_info;
  169. for( curve_info = mbedtls_ecp_curve_list();
  170. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  171. curve_info++ )
  172. {
  173. ecp_supported_grp_id[i++] = curve_info->grp_id;
  174. }
  175. ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
  176. init_done = 1;
  177. }
  178. return( ecp_supported_grp_id );
  179. }
  180. /*
  181. * Get the curve info for the internal identifier
  182. */
  183. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id )
  184. {
  185. const mbedtls_ecp_curve_info *curve_info;
  186. for( curve_info = mbedtls_ecp_curve_list();
  187. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  188. curve_info++ )
  189. {
  190. if( curve_info->grp_id == grp_id )
  191. return( curve_info );
  192. }
  193. return( NULL );
  194. }
  195. /*
  196. * Get the curve info from the TLS identifier
  197. */
  198. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
  199. {
  200. const mbedtls_ecp_curve_info *curve_info;
  201. for( curve_info = mbedtls_ecp_curve_list();
  202. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  203. curve_info++ )
  204. {
  205. if( curve_info->tls_id == tls_id )
  206. return( curve_info );
  207. }
  208. return( NULL );
  209. }
  210. /*
  211. * Get the curve info from the name
  212. */
  213. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
  214. {
  215. const mbedtls_ecp_curve_info *curve_info;
  216. for( curve_info = mbedtls_ecp_curve_list();
  217. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  218. curve_info++ )
  219. {
  220. if( strcmp( curve_info->name, name ) == 0 )
  221. return( curve_info );
  222. }
  223. return( NULL );
  224. }
  225. /*
  226. * Get the type of a curve
  227. */
  228. static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
  229. {
  230. if( grp->G.X.p == NULL )
  231. return( ECP_TYPE_NONE );
  232. if( grp->G.Y.p == NULL )
  233. return( ECP_TYPE_MONTGOMERY );
  234. else
  235. return( ECP_TYPE_SHORT_WEIERSTRASS );
  236. }
  237. /*
  238. * Initialize (the components of) a point
  239. */
  240. void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
  241. {
  242. if( pt == NULL )
  243. return;
  244. mbedtls_mpi_init( &pt->X );
  245. mbedtls_mpi_init( &pt->Y );
  246. mbedtls_mpi_init( &pt->Z );
  247. }
  248. /*
  249. * Initialize (the components of) a group
  250. */
  251. void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
  252. {
  253. if( grp == NULL )
  254. return;
  255. memset( grp, 0, sizeof( mbedtls_ecp_group ) );
  256. }
  257. /*
  258. * Initialize (the components of) a key pair
  259. */
  260. void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
  261. {
  262. if( key == NULL )
  263. return;
  264. mbedtls_ecp_group_init( &key->grp );
  265. mbedtls_mpi_init( &key->d );
  266. mbedtls_ecp_point_init( &key->Q );
  267. }
  268. /*
  269. * Unallocate (the components of) a point
  270. */
  271. void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
  272. {
  273. if( pt == NULL )
  274. return;
  275. mbedtls_mpi_free( &( pt->X ) );
  276. mbedtls_mpi_free( &( pt->Y ) );
  277. mbedtls_mpi_free( &( pt->Z ) );
  278. }
  279. /*
  280. * Unallocate (the components of) a group
  281. */
  282. void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
  283. {
  284. size_t i;
  285. if( grp == NULL )
  286. return;
  287. if( grp->h != 1 )
  288. {
  289. mbedtls_mpi_free( &grp->P );
  290. mbedtls_mpi_free( &grp->A );
  291. mbedtls_mpi_free( &grp->B );
  292. mbedtls_ecp_point_free( &grp->G );
  293. mbedtls_mpi_free( &grp->N );
  294. }
  295. if( grp->T != NULL )
  296. {
  297. for( i = 0; i < grp->T_size; i++ )
  298. mbedtls_ecp_point_free( &grp->T[i] );
  299. mbedtls_free( grp->T );
  300. }
  301. mbedtls_zeroize( grp, sizeof( mbedtls_ecp_group ) );
  302. }
  303. /*
  304. * Unallocate (the components of) a key pair
  305. */
  306. void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
  307. {
  308. if( key == NULL )
  309. return;
  310. mbedtls_ecp_group_free( &key->grp );
  311. mbedtls_mpi_free( &key->d );
  312. mbedtls_ecp_point_free( &key->Q );
  313. }
  314. /*
  315. * Copy the contents of a point
  316. */
  317. int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
  318. {
  319. int ret;
  320. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
  321. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
  322. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
  323. cleanup:
  324. return( ret );
  325. }
  326. /*
  327. * Copy the contents of a group object
  328. */
  329. int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
  330. {
  331. return mbedtls_ecp_group_load( dst, src->id );
  332. }
  333. /*
  334. * Set point to zero
  335. */
  336. int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
  337. {
  338. int ret;
  339. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
  340. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
  341. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
  342. cleanup:
  343. return( ret );
  344. }
  345. /*
  346. * Tell if a point is zero
  347. */
  348. int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
  349. {
  350. return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
  351. }
  352. /*
  353. * Compare two points lazyly
  354. */
  355. int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
  356. const mbedtls_ecp_point *Q )
  357. {
  358. if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
  359. mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
  360. mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
  361. {
  362. return( 0 );
  363. }
  364. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  365. }
  366. /*
  367. * Import a non-zero point from ASCII strings
  368. */
  369. int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
  370. const char *x, const char *y )
  371. {
  372. int ret;
  373. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
  374. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
  375. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
  376. cleanup:
  377. return( ret );
  378. }
  379. /*
  380. * Export a point into unsigned binary data (SEC1 2.3.3)
  381. */
  382. int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
  383. int format, size_t *olen,
  384. unsigned char *buf, size_t buflen )
  385. {
  386. int ret = 0;
  387. size_t plen;
  388. if( format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
  389. format != MBEDTLS_ECP_PF_COMPRESSED )
  390. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  391. /*
  392. * Common case: P == 0
  393. */
  394. if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
  395. {
  396. if( buflen < 1 )
  397. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  398. buf[0] = 0x00;
  399. *olen = 1;
  400. return( 0 );
  401. }
  402. plen = mbedtls_mpi_size( &grp->P );
  403. if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
  404. {
  405. *olen = 2 * plen + 1;
  406. if( buflen < *olen )
  407. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  408. buf[0] = 0x04;
  409. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
  410. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
  411. }
  412. else if( format == MBEDTLS_ECP_PF_COMPRESSED )
  413. {
  414. *olen = plen + 1;
  415. if( buflen < *olen )
  416. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  417. buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
  418. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
  419. }
  420. cleanup:
  421. return( ret );
  422. }
  423. /*
  424. * Import a point from unsigned binary data (SEC1 2.3.4)
  425. */
  426. int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  427. const unsigned char *buf, size_t ilen )
  428. {
  429. int ret;
  430. size_t plen;
  431. if( ilen < 1 )
  432. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  433. if( buf[0] == 0x00 )
  434. {
  435. if( ilen == 1 )
  436. return( mbedtls_ecp_set_zero( pt ) );
  437. else
  438. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  439. }
  440. plen = mbedtls_mpi_size( &grp->P );
  441. if( buf[0] != 0x04 )
  442. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  443. if( ilen != 2 * plen + 1 )
  444. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  445. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
  446. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
  447. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
  448. cleanup:
  449. return( ret );
  450. }
  451. /*
  452. * Import a point from a TLS ECPoint record (RFC 4492)
  453. * struct {
  454. * opaque point <1..2^8-1>;
  455. * } ECPoint;
  456. */
  457. int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  458. const unsigned char **buf, size_t buf_len )
  459. {
  460. unsigned char data_len;
  461. const unsigned char *buf_start;
  462. /*
  463. * We must have at least two bytes (1 for length, at least one for data)
  464. */
  465. if( buf_len < 2 )
  466. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  467. data_len = *(*buf)++;
  468. if( data_len < 1 || data_len > buf_len - 1 )
  469. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  470. /*
  471. * Save buffer start for read_binary and update buf
  472. */
  473. buf_start = *buf;
  474. *buf += data_len;
  475. return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len );
  476. }
  477. /*
  478. * Export a point as a TLS ECPoint record (RFC 4492)
  479. * struct {
  480. * opaque point <1..2^8-1>;
  481. * } ECPoint;
  482. */
  483. int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
  484. int format, size_t *olen,
  485. unsigned char *buf, size_t blen )
  486. {
  487. int ret;
  488. /*
  489. * buffer length must be at least one, for our length byte
  490. */
  491. if( blen < 1 )
  492. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  493. if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
  494. olen, buf + 1, blen - 1) ) != 0 )
  495. return( ret );
  496. /*
  497. * write length to the first byte and update total length
  498. */
  499. buf[0] = (unsigned char) *olen;
  500. ++*olen;
  501. return( 0 );
  502. }
  503. /*
  504. * Set a group from an ECParameters record (RFC 4492)
  505. */
  506. int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len )
  507. {
  508. uint16_t tls_id;
  509. const mbedtls_ecp_curve_info *curve_info;
  510. /*
  511. * We expect at least three bytes (see below)
  512. */
  513. if( len < 3 )
  514. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  515. /*
  516. * First byte is curve_type; only named_curve is handled
  517. */
  518. if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
  519. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  520. /*
  521. * Next two bytes are the namedcurve value
  522. */
  523. tls_id = *(*buf)++;
  524. tls_id <<= 8;
  525. tls_id |= *(*buf)++;
  526. if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
  527. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  528. return mbedtls_ecp_group_load( grp, curve_info->grp_id );
  529. }
  530. /*
  531. * Write the ECParameters record corresponding to a group (RFC 4492)
  532. */
  533. int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
  534. unsigned char *buf, size_t blen )
  535. {
  536. const mbedtls_ecp_curve_info *curve_info;
  537. if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
  538. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  539. /*
  540. * We are going to write 3 bytes (see below)
  541. */
  542. *olen = 3;
  543. if( blen < *olen )
  544. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  545. /*
  546. * First byte is curve_type, always named_curve
  547. */
  548. *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
  549. /*
  550. * Next two bytes are the namedcurve value
  551. */
  552. buf[0] = curve_info->tls_id >> 8;
  553. buf[1] = curve_info->tls_id & 0xFF;
  554. return( 0 );
  555. }
  556. /*
  557. * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
  558. * See the documentation of struct mbedtls_ecp_group.
  559. *
  560. * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
  561. */
  562. static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
  563. {
  564. int ret;
  565. if( grp->modp == NULL )
  566. return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
  567. /* N->s < 0 is a much faster test, which fails only if N is 0 */
  568. if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
  569. mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
  570. {
  571. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  572. }
  573. MBEDTLS_MPI_CHK( grp->modp( N ) );
  574. /* N->s < 0 is a much faster test, which fails only if N is 0 */
  575. while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
  576. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
  577. while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
  578. /* we known P, N and the result are positive */
  579. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
  580. cleanup:
  581. return( ret );
  582. }
  583. /*
  584. * Fast mod-p functions expect their argument to be in the 0..p^2 range.
  585. *
  586. * In order to guarantee that, we need to ensure that operands of
  587. * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
  588. * bring the result back to this range.
  589. *
  590. * The following macros are shortcuts for doing that.
  591. */
  592. /*
  593. * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
  594. */
  595. #if defined(MBEDTLS_SELF_TEST)
  596. #define INC_MUL_COUNT mul_count++;
  597. #else
  598. #define INC_MUL_COUNT
  599. #endif
  600. #define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
  601. while( 0 )
  602. /*
  603. * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
  604. * N->s < 0 is a very fast test, which fails only if N is 0
  605. */
  606. #define MOD_SUB( N ) \
  607. while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \
  608. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
  609. /*
  610. * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
  611. * We known P, N and the result are positive, so sub_abs is correct, and
  612. * a bit faster.
  613. */
  614. #define MOD_ADD( N ) \
  615. while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
  616. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
  617. #if defined(ECP_SHORTWEIERSTRASS)
  618. /*
  619. * For curves in short Weierstrass form, we do all the internal operations in
  620. * Jacobian coordinates.
  621. *
  622. * For multiplication, we'll use a comb method with coutermeasueres against
  623. * SPA, hence timing attacks.
  624. */
  625. /*
  626. * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
  627. * Cost: 1N := 1I + 3M + 1S
  628. */
  629. static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
  630. {
  631. int ret;
  632. mbedtls_mpi Zi, ZZi;
  633. if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
  634. return( 0 );
  635. mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
  636. /*
  637. * X = X / Z^2 mod p
  638. */
  639. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
  640. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
  641. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
  642. /*
  643. * Y = Y / Z^3 mod p
  644. */
  645. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
  646. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
  647. /*
  648. * Z = 1
  649. */
  650. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
  651. cleanup:
  652. mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
  653. return( ret );
  654. }
  655. /*
  656. * Normalize jacobian coordinates of an array of (pointers to) points,
  657. * using Montgomery's trick to perform only one inversion mod P.
  658. * (See for example Cohen's "A Course in Computational Algebraic Number
  659. * Theory", Algorithm 10.3.4.)
  660. *
  661. * Warning: fails (returning an error) if one of the points is zero!
  662. * This should never happen, see choice of w in ecp_mul_comb().
  663. *
  664. * Cost: 1N(t) := 1I + (6t - 3)M + 1S
  665. */
  666. static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
  667. mbedtls_ecp_point *T[], size_t t_len )
  668. {
  669. int ret;
  670. size_t i;
  671. mbedtls_mpi *c, u, Zi, ZZi;
  672. if( t_len < 2 )
  673. return( ecp_normalize_jac( grp, *T ) );
  674. if( ( c = mbedtls_calloc( t_len, sizeof( mbedtls_mpi ) ) ) == NULL )
  675. return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
  676. mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
  677. /*
  678. * c[i] = Z_0 * ... * Z_i
  679. */
  680. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
  681. for( i = 1; i < t_len; i++ )
  682. {
  683. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
  684. MOD_MUL( c[i] );
  685. }
  686. /*
  687. * u = 1 / (Z_0 * ... * Z_n) mod P
  688. */
  689. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
  690. for( i = t_len - 1; ; i-- )
  691. {
  692. /*
  693. * Zi = 1 / Z_i mod p
  694. * u = 1 / (Z_0 * ... * Z_i) mod P
  695. */
  696. if( i == 0 ) {
  697. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
  698. }
  699. else
  700. {
  701. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
  702. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
  703. }
  704. /*
  705. * proceed as in normalize()
  706. */
  707. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
  708. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
  709. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
  710. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
  711. /*
  712. * Post-precessing: reclaim some memory by shrinking coordinates
  713. * - not storing Z (always 1)
  714. * - shrinking other coordinates, but still keeping the same number of
  715. * limbs as P, as otherwise it will too likely be regrown too fast.
  716. */
  717. MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
  718. MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
  719. mbedtls_mpi_free( &T[i]->Z );
  720. if( i == 0 )
  721. break;
  722. }
  723. cleanup:
  724. mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
  725. for( i = 0; i < t_len; i++ )
  726. mbedtls_mpi_free( &c[i] );
  727. mbedtls_free( c );
  728. return( ret );
  729. }
  730. /*
  731. * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
  732. * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
  733. */
  734. static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
  735. mbedtls_ecp_point *Q,
  736. unsigned char inv )
  737. {
  738. int ret;
  739. unsigned char nonzero;
  740. mbedtls_mpi mQY;
  741. mbedtls_mpi_init( &mQY );
  742. /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
  743. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
  744. nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
  745. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
  746. cleanup:
  747. mbedtls_mpi_free( &mQY );
  748. return( ret );
  749. }
  750. /*
  751. * Point doubling R = 2 P, Jacobian coordinates
  752. *
  753. * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
  754. *
  755. * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
  756. * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
  757. *
  758. * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
  759. *
  760. * Cost: 1D := 3M + 4S (A == 0)
  761. * 4M + 4S (A == -3)
  762. * 3M + 6S + 1a otherwise
  763. */
  764. static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  765. const mbedtls_ecp_point *P )
  766. {
  767. int ret;
  768. mbedtls_mpi M, S, T, U;
  769. #if defined(MBEDTLS_SELF_TEST)
  770. dbl_count++;
  771. #endif
  772. mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U );
  773. /* Special case for A = -3 */
  774. if( grp->A.p == NULL )
  775. {
  776. /* M = 3(X + Z^2)(X - Z^2) */
  777. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
  778. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
  779. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
  780. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
  781. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
  782. }
  783. else
  784. {
  785. /* M = 3.X^2 */
  786. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
  787. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
  788. /* Optimize away for "koblitz" curves with A = 0 */
  789. if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
  790. {
  791. /* M += A.Z^4 */
  792. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
  793. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
  794. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
  795. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
  796. }
  797. }
  798. /* S = 4.X.Y^2 */
  799. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
  800. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
  801. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
  802. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
  803. /* U = 8.Y^4 */
  804. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
  805. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
  806. /* T = M^2 - 2.S */
  807. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
  808. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
  809. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
  810. /* S = M(S - T) - U */
  811. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
  812. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
  813. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
  814. /* U = 2.Y.Z */
  815. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
  816. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
  817. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
  818. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
  819. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
  820. cleanup:
  821. mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U );
  822. return( ret );
  823. }
  824. /*
  825. * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
  826. *
  827. * The coordinates of Q must be normalized (= affine),
  828. * but those of P don't need to. R is not normalized.
  829. *
  830. * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
  831. * None of these cases can happen as intermediate step in ecp_mul_comb():
  832. * - at each step, P, Q and R are multiples of the base point, the factor
  833. * being less than its order, so none of them is zero;
  834. * - Q is an odd multiple of the base point, P an even multiple,
  835. * due to the choice of precomputed points in the modified comb method.
  836. * So branches for these cases do not leak secret information.
  837. *
  838. * We accept Q->Z being unset (saving memory in tables) as meaning 1.
  839. *
  840. * Cost: 1A := 8M + 3S
  841. */
  842. static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  843. const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
  844. {
  845. int ret;
  846. mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
  847. #if defined(MBEDTLS_SELF_TEST)
  848. add_count++;
  849. #endif
  850. /*
  851. * Trivial cases: P == 0 or Q == 0 (case 1)
  852. */
  853. if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
  854. return( mbedtls_ecp_copy( R, Q ) );
  855. if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
  856. return( mbedtls_ecp_copy( R, P ) );
  857. /*
  858. * Make sure Q coordinates are normalized
  859. */
  860. if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
  861. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  862. mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
  863. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  864. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
  865. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
  866. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
  867. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
  868. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
  869. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
  870. /* Special cases (2) and (3) */
  871. if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
  872. {
  873. if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
  874. {
  875. ret = ecp_double_jac( grp, R, P );
  876. goto cleanup;
  877. }
  878. else
  879. {
  880. ret = mbedtls_ecp_set_zero( R );
  881. goto cleanup;
  882. }
  883. }
  884. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
  885. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
  886. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
  887. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
  888. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
  889. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
  890. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
  891. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
  892. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
  893. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
  894. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
  895. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
  896. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
  897. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
  898. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
  899. cleanup:
  900. mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
  901. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  902. return( ret );
  903. }
  904. /*
  905. * Randomize jacobian coordinates:
  906. * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
  907. * This is sort of the reverse operation of ecp_normalize_jac().
  908. *
  909. * This countermeasure was first suggested in [2].
  910. */
  911. static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  912. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  913. {
  914. int ret;
  915. mbedtls_mpi l, ll;
  916. size_t p_size = ( grp->pbits + 7 ) / 8;
  917. int count = 0;
  918. mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
  919. /* Generate l such that 1 < l < p */
  920. do
  921. {
  922. mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng );
  923. while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
  924. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
  925. if( count++ > 10 )
  926. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  927. }
  928. while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
  929. /* Z = l * Z */
  930. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
  931. /* X = l^2 * X */
  932. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
  933. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
  934. /* Y = l^3 * Y */
  935. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
  936. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
  937. cleanup:
  938. mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
  939. return( ret );
  940. }
  941. /*
  942. * Check and define parameters used by the comb method (see below for details)
  943. */
  944. #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
  945. #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
  946. #endif
  947. /* d = ceil( n / w ) */
  948. #define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
  949. /* number of precomputed points */
  950. #define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  951. /*
  952. * Compute the representation of m that will be used with our comb method.
  953. *
  954. * The basic comb method is described in GECC 3.44 for example. We use a
  955. * modified version that provides resistance to SPA by avoiding zero
  956. * digits in the representation as in [3]. We modify the method further by
  957. * requiring that all K_i be odd, which has the small cost that our
  958. * representation uses one more K_i, due to carries.
  959. *
  960. * Also, for the sake of compactness, only the seven low-order bits of x[i]
  961. * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
  962. * the paper): it is set if and only if if s_i == -1;
  963. *
  964. * Calling conventions:
  965. * - x is an array of size d + 1
  966. * - w is the size, ie number of teeth, of the comb, and must be between
  967. * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
  968. * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
  969. * (the result will be incorrect if these assumptions are not satisfied)
  970. */
  971. static void ecp_comb_fixed( unsigned char x[], size_t d,
  972. unsigned char w, const mbedtls_mpi *m )
  973. {
  974. size_t i, j;
  975. unsigned char c, cc, adjust;
  976. memset( x, 0, d+1 );
  977. /* First get the classical comb values (except for x_d = 0) */
  978. for( i = 0; i < d; i++ )
  979. for( j = 0; j < w; j++ )
  980. x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
  981. /* Now make sure x_1 .. x_d are odd */
  982. c = 0;
  983. for( i = 1; i <= d; i++ )
  984. {
  985. /* Add carry and update it */
  986. cc = x[i] & c;
  987. x[i] = x[i] ^ c;
  988. c = cc;
  989. /* Adjust if needed, avoiding branches */
  990. adjust = 1 - ( x[i] & 0x01 );
  991. c |= x[i] & ( x[i-1] * adjust );
  992. x[i] = x[i] ^ ( x[i-1] * adjust );
  993. x[i-1] |= adjust << 7;
  994. }
  995. }
  996. /*
  997. * Precompute points for the comb method
  998. *
  999. * If i = i_{w-1} ... i_1 is the binary representation of i, then
  1000. * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P
  1001. *
  1002. * T must be able to hold 2^{w - 1} elements
  1003. *
  1004. * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
  1005. */
  1006. static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
  1007. mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
  1008. unsigned char w, size_t d )
  1009. {
  1010. int ret;
  1011. unsigned char i, k;
  1012. size_t j;
  1013. mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
  1014. /*
  1015. * Set T[0] = P and
  1016. * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
  1017. */
  1018. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
  1019. k = 0;
  1020. for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
  1021. {
  1022. cur = T + i;
  1023. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
  1024. for( j = 0; j < d; j++ )
  1025. MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
  1026. TT[k++] = cur;
  1027. }
  1028. MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
  1029. /*
  1030. * Compute the remaining ones using the minimal number of additions
  1031. * Be careful to update T[2^l] only after using it!
  1032. */
  1033. k = 0;
  1034. for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
  1035. {
  1036. j = i;
  1037. while( j-- )
  1038. {
  1039. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
  1040. TT[k++] = &T[i + j];
  1041. }
  1042. }
  1043. MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
  1044. cleanup:
  1045. return( ret );
  1046. }
  1047. /*
  1048. * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
  1049. */
  1050. static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1051. const mbedtls_ecp_point T[], unsigned char t_len,
  1052. unsigned char i )
  1053. {
  1054. int ret;
  1055. unsigned char ii, j;
  1056. /* Ignore the "sign" bit and scale down */
  1057. ii = ( i & 0x7Fu ) >> 1;
  1058. /* Read the whole table to thwart cache-based timing attacks */
  1059. for( j = 0; j < t_len; j++ )
  1060. {
  1061. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
  1062. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
  1063. }
  1064. /* Safely invert result if i is "negative" */
  1065. MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
  1066. cleanup:
  1067. return( ret );
  1068. }
  1069. /*
  1070. * Core multiplication algorithm for the (modified) comb method.
  1071. * This part is actually common with the basic comb method (GECC 3.44)
  1072. *
  1073. * Cost: d A + d D + 1 R
  1074. */
  1075. static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1076. const mbedtls_ecp_point T[], unsigned char t_len,
  1077. const unsigned char x[], size_t d,
  1078. int (*f_rng)(void *, unsigned char *, size_t),
  1079. void *p_rng )
  1080. {
  1081. int ret;
  1082. mbedtls_ecp_point Txi;
  1083. size_t i;
  1084. mbedtls_ecp_point_init( &Txi );
  1085. /* Start with a non-zero point and randomize its coordinates */
  1086. i = d;
  1087. MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
  1088. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
  1089. if( f_rng != 0 )
  1090. MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
  1091. while( i-- != 0 )
  1092. {
  1093. MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
  1094. MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
  1095. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
  1096. }
  1097. cleanup:
  1098. mbedtls_ecp_point_free( &Txi );
  1099. return( ret );
  1100. }
  1101. /*
  1102. * Multiplication using the comb method,
  1103. * for curves in short Weierstrass form
  1104. */
  1105. static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1106. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1107. int (*f_rng)(void *, unsigned char *, size_t),
  1108. void *p_rng )
  1109. {
  1110. int ret;
  1111. unsigned char w, m_is_odd, p_eq_g, pre_len, i;
  1112. size_t d;
  1113. unsigned char k[COMB_MAX_D + 1];
  1114. mbedtls_ecp_point *T;
  1115. mbedtls_mpi M, mm;
  1116. mbedtls_mpi_init( &M );
  1117. mbedtls_mpi_init( &mm );
  1118. /* we need N to be odd to trnaform m in an odd number, check now */
  1119. if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
  1120. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1121. /*
  1122. * Minimize the number of multiplications, that is minimize
  1123. * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
  1124. * (see costs of the various parts, with 1S = 1M)
  1125. */
  1126. w = grp->nbits >= 384 ? 5 : 4;
  1127. /*
  1128. * If P == G, pre-compute a bit more, since this may be re-used later.
  1129. * Just adding one avoids upping the cost of the first mul too much,
  1130. * and the memory cost too.
  1131. */
  1132. #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
  1133. p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
  1134. mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
  1135. if( p_eq_g )
  1136. w++;
  1137. #else
  1138. p_eq_g = 0;
  1139. #endif
  1140. /*
  1141. * Make sure w is within bounds.
  1142. * (The last test is useful only for very small curves in the test suite.)
  1143. */
  1144. if( w > MBEDTLS_ECP_WINDOW_SIZE )
  1145. w = MBEDTLS_ECP_WINDOW_SIZE;
  1146. if( w >= grp->nbits )
  1147. w = 2;
  1148. /* Other sizes that depend on w */
  1149. pre_len = 1U << ( w - 1 );
  1150. d = ( grp->nbits + w - 1 ) / w;
  1151. /*
  1152. * Prepare precomputed points: if P == G we want to
  1153. * use grp->T if already initialized, or initialize it.
  1154. */
  1155. T = p_eq_g ? grp->T : NULL;
  1156. if( T == NULL )
  1157. {
  1158. T = mbedtls_calloc( pre_len, sizeof( mbedtls_ecp_point ) );
  1159. if( T == NULL )
  1160. {
  1161. ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
  1162. goto cleanup;
  1163. }
  1164. MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
  1165. if( p_eq_g )
  1166. {
  1167. grp->T = T;
  1168. grp->T_size = pre_len;
  1169. }
  1170. }
  1171. /*
  1172. * Make sure M is odd (M = m or M = N - m, since N is odd)
  1173. * using the fact that m * P = - (N - m) * P
  1174. */
  1175. m_is_odd = ( mbedtls_mpi_get_bit( m, 0 ) == 1 );
  1176. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
  1177. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
  1178. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
  1179. /*
  1180. * Go for comb multiplication, R = M * P
  1181. */
  1182. ecp_comb_fixed( k, d, w, &M );
  1183. MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
  1184. /*
  1185. * Now get m * P from M * P and normalize it
  1186. */
  1187. MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
  1188. MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
  1189. cleanup:
  1190. if( T != NULL && ! p_eq_g )
  1191. {
  1192. for( i = 0; i < pre_len; i++ )
  1193. mbedtls_ecp_point_free( &T[i] );
  1194. mbedtls_free( T );
  1195. }
  1196. mbedtls_mpi_free( &M );
  1197. mbedtls_mpi_free( &mm );
  1198. if( ret != 0 )
  1199. mbedtls_ecp_point_free( R );
  1200. return( ret );
  1201. }
  1202. #endif /* ECP_SHORTWEIERSTRASS */
  1203. #if defined(ECP_MONTGOMERY)
  1204. /*
  1205. * For Montgomery curves, we do all the internal arithmetic in projective
  1206. * coordinates. Import/export of points uses only the x coordinates, which is
  1207. * internaly represented as X / Z.
  1208. *
  1209. * For scalar multiplication, we'll use a Montgomery ladder.
  1210. */
  1211. /*
  1212. * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
  1213. * Cost: 1M + 1I
  1214. */
  1215. static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
  1216. {
  1217. int ret;
  1218. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
  1219. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
  1220. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
  1221. cleanup:
  1222. return( ret );
  1223. }
  1224. /*
  1225. * Randomize projective x/z coordinates:
  1226. * (X, Z) -> (l X, l Z) for random l
  1227. * This is sort of the reverse operation of ecp_normalize_mxz().
  1228. *
  1229. * This countermeasure was first suggested in [2].
  1230. * Cost: 2M
  1231. */
  1232. static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
  1233. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  1234. {
  1235. int ret;
  1236. mbedtls_mpi l;
  1237. size_t p_size = ( grp->pbits + 7 ) / 8;
  1238. int count = 0;
  1239. mbedtls_mpi_init( &l );
  1240. /* Generate l such that 1 < l < p */
  1241. do
  1242. {
  1243. mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng );
  1244. while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
  1245. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
  1246. if( count++ > 10 )
  1247. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  1248. }
  1249. while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
  1250. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
  1251. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
  1252. cleanup:
  1253. mbedtls_mpi_free( &l );
  1254. return( ret );
  1255. }
  1256. /*
  1257. * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
  1258. * for Montgomery curves in x/z coordinates.
  1259. *
  1260. * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
  1261. * with
  1262. * d = X1
  1263. * P = (X2, Z2)
  1264. * Q = (X3, Z3)
  1265. * R = (X4, Z4)
  1266. * S = (X5, Z5)
  1267. * and eliminating temporary variables tO, ..., t4.
  1268. *
  1269. * Cost: 5M + 4S
  1270. */
  1271. static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
  1272. mbedtls_ecp_point *R, mbedtls_ecp_point *S,
  1273. const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
  1274. const mbedtls_mpi *d )
  1275. {
  1276. int ret;
  1277. mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
  1278. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
  1279. mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
  1280. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
  1281. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
  1282. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
  1283. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
  1284. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
  1285. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
  1286. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
  1287. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
  1288. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
  1289. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
  1290. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
  1291. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
  1292. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
  1293. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
  1294. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
  1295. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
  1296. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
  1297. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
  1298. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
  1299. cleanup:
  1300. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
  1301. mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
  1302. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
  1303. return( ret );
  1304. }
  1305. /*
  1306. * Multiplication with Montgomery ladder in x/z coordinates,
  1307. * for curves in Montgomery form
  1308. */
  1309. static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1310. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1311. int (*f_rng)(void *, unsigned char *, size_t),
  1312. void *p_rng )
  1313. {
  1314. int ret;
  1315. size_t i;
  1316. unsigned char b;
  1317. mbedtls_ecp_point RP;
  1318. mbedtls_mpi PX;
  1319. mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
  1320. /* Save PX and read from P before writing to R, in case P == R */
  1321. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
  1322. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
  1323. /* Set R to zero in modified x/z coordinates */
  1324. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
  1325. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
  1326. mbedtls_mpi_free( &R->Y );
  1327. /* RP.X might be sligtly larger than P, so reduce it */
  1328. MOD_ADD( RP.X );
  1329. /* Randomize coordinates of the starting point */
  1330. if( f_rng != NULL )
  1331. MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
  1332. /* Loop invariant: R = result so far, RP = R + P */
  1333. i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
  1334. while( i-- > 0 )
  1335. {
  1336. b = mbedtls_mpi_get_bit( m, i );
  1337. /*
  1338. * if (b) R = 2R + P else R = 2R,
  1339. * which is:
  1340. * if (b) double_add( RP, R, RP, R )
  1341. * else double_add( R, RP, R, RP )
  1342. * but using safe conditional swaps to avoid leaks
  1343. */
  1344. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
  1345. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
  1346. MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
  1347. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
  1348. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
  1349. }
  1350. MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
  1351. cleanup:
  1352. mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
  1353. return( ret );
  1354. }
  1355. #endif /* ECP_MONTGOMERY */
  1356. /*
  1357. * Multiplication R = m * P
  1358. */
  1359. int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1360. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1361. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  1362. {
  1363. int ret;
  1364. /* Common sanity checks */
  1365. if( mbedtls_mpi_cmp_int( &P->Z, 1 ) != 0 )
  1366. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1367. if( ( ret = mbedtls_ecp_check_privkey( grp, m ) ) != 0 ||
  1368. ( ret = mbedtls_ecp_check_pubkey( grp, P ) ) != 0 )
  1369. return( ret );
  1370. #if defined(ECP_MONTGOMERY)
  1371. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  1372. return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
  1373. #endif
  1374. #if defined(ECP_SHORTWEIERSTRASS)
  1375. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  1376. return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
  1377. #endif
  1378. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1379. }
  1380. #if defined(ECP_SHORTWEIERSTRASS)
  1381. /*
  1382. * Check that an affine point is valid as a public key,
  1383. * short weierstrass curves (SEC1 3.2.3.1)
  1384. */
  1385. static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  1386. {
  1387. int ret;
  1388. mbedtls_mpi YY, RHS;
  1389. /* pt coordinates must be normalized for our checks */
  1390. if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
  1391. mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
  1392. mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
  1393. mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
  1394. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  1395. mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
  1396. /*
  1397. * YY = Y^2
  1398. * RHS = X (X^2 + A) + B = X^3 + A X + B
  1399. */
  1400. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
  1401. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
  1402. /* Special case for A = -3 */
  1403. if( grp->A.p == NULL )
  1404. {
  1405. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
  1406. }
  1407. else
  1408. {
  1409. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
  1410. }
  1411. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
  1412. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
  1413. if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
  1414. ret = MBEDTLS_ERR_ECP_INVALID_KEY;
  1415. cleanup:
  1416. mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
  1417. return( ret );
  1418. }
  1419. #endif /* ECP_SHORTWEIERSTRASS */
  1420. /*
  1421. * R = m * P with shortcuts for m == 1 and m == -1
  1422. * NOT constant-time - ONLY for short Weierstrass!
  1423. */
  1424. static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
  1425. mbedtls_ecp_point *R,
  1426. const mbedtls_mpi *m,
  1427. const mbedtls_ecp_point *P )
  1428. {
  1429. int ret;
  1430. if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
  1431. {
  1432. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
  1433. }
  1434. else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
  1435. {
  1436. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
  1437. if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
  1438. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
  1439. }
  1440. else
  1441. {
  1442. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, NULL, NULL ) );
  1443. }
  1444. cleanup:
  1445. return( ret );
  1446. }
  1447. /*
  1448. * Linear combination
  1449. * NOT constant-time
  1450. */
  1451. int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1452. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1453. const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
  1454. {
  1455. int ret;
  1456. mbedtls_ecp_point mP;
  1457. if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
  1458. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  1459. mbedtls_ecp_point_init( &mP );
  1460. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, &mP, m, P ) );
  1461. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, R, n, Q ) );
  1462. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, &mP, R ) );
  1463. MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
  1464. cleanup:
  1465. mbedtls_ecp_point_free( &mP );
  1466. return( ret );
  1467. }
  1468. #if defined(ECP_MONTGOMERY)
  1469. /*
  1470. * Check validity of a public key for Montgomery curves with x-only schemes
  1471. */
  1472. static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  1473. {
  1474. /* [Curve25519 p. 5] Just check X is the correct number of bytes */
  1475. if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
  1476. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  1477. return( 0 );
  1478. }
  1479. #endif /* ECP_MONTGOMERY */
  1480. /*
  1481. * Check that a point is valid as a public key
  1482. */
  1483. int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  1484. {
  1485. /* Must use affine coordinates */
  1486. if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
  1487. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  1488. #if defined(ECP_MONTGOMERY)
  1489. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  1490. return( ecp_check_pubkey_mx( grp, pt ) );
  1491. #endif
  1492. #if defined(ECP_SHORTWEIERSTRASS)
  1493. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  1494. return( ecp_check_pubkey_sw( grp, pt ) );
  1495. #endif
  1496. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1497. }
  1498. /*
  1499. * Check that an mbedtls_mpi is valid as a private key
  1500. */
  1501. int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d )
  1502. {
  1503. #if defined(ECP_MONTGOMERY)
  1504. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  1505. {
  1506. /* see [Curve25519] page 5 */
  1507. if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
  1508. mbedtls_mpi_get_bit( d, 1 ) != 0 ||
  1509. mbedtls_mpi_get_bit( d, 2 ) != 0 ||
  1510. mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
  1511. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  1512. else
  1513. return( 0 );
  1514. }
  1515. #endif /* ECP_MONTGOMERY */
  1516. #if defined(ECP_SHORTWEIERSTRASS)
  1517. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  1518. {
  1519. /* see SEC1 3.2 */
  1520. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
  1521. mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  1522. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  1523. else
  1524. return( 0 );
  1525. }
  1526. #endif /* ECP_SHORTWEIERSTRASS */
  1527. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1528. }
  1529. /*
  1530. * Generate a keypair with configurable base point
  1531. */
  1532. int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  1533. const mbedtls_ecp_point *G,
  1534. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  1535. int (*f_rng)(void *, unsigned char *, size_t),
  1536. void *p_rng )
  1537. {
  1538. int ret;
  1539. size_t n_size = ( grp->nbits + 7 ) / 8;
  1540. #if defined(ECP_MONTGOMERY)
  1541. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  1542. {
  1543. /* [M225] page 5 */
  1544. size_t b;
  1545. do {
  1546. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
  1547. } while( mbedtls_mpi_bitlen( d ) == 0);
  1548. /* Make sure the most significant bit is nbits */
  1549. b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
  1550. if( b > grp->nbits )
  1551. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
  1552. else
  1553. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
  1554. /* Make sure the last three bits are unset */
  1555. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
  1556. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
  1557. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
  1558. }
  1559. else
  1560. #endif /* ECP_MONTGOMERY */
  1561. #if defined(ECP_SHORTWEIERSTRASS)
  1562. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  1563. {
  1564. /* SEC1 3.2.1: Generate d such that 1 <= n < N */
  1565. int count = 0;
  1566. unsigned char rnd[MBEDTLS_ECP_MAX_BYTES];
  1567. /*
  1568. * Match the procedure given in RFC 6979 (deterministic ECDSA):
  1569. * - use the same byte ordering;
  1570. * - keep the leftmost nbits bits of the generated octet string;
  1571. * - try until result is in the desired range.
  1572. * This also avoids any biais, which is especially important for ECDSA.
  1573. */
  1574. do
  1575. {
  1576. MBEDTLS_MPI_CHK( f_rng( p_rng, rnd, n_size ) );
  1577. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( d, rnd, n_size ) );
  1578. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
  1579. /*
  1580. * Each try has at worst a probability 1/2 of failing (the msb has
  1581. * a probability 1/2 of being 0, and then the result will be < N),
  1582. * so after 30 tries failure probability is a most 2**(-30).
  1583. *
  1584. * For most curves, 1 try is enough with overwhelming probability,
  1585. * since N starts with a lot of 1s in binary, but some curves
  1586. * such as secp224k1 are actually very close to the worst case.
  1587. */
  1588. if( ++count > 30 )
  1589. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  1590. }
  1591. while( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
  1592. mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 );
  1593. }
  1594. else
  1595. #endif /* ECP_SHORTWEIERSTRASS */
  1596. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1597. cleanup:
  1598. if( ret != 0 )
  1599. return( ret );
  1600. return( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
  1601. }
  1602. /*
  1603. * Generate key pair, wrapper for conventional base point
  1604. */
  1605. int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
  1606. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  1607. int (*f_rng)(void *, unsigned char *, size_t),
  1608. void *p_rng )
  1609. {
  1610. return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
  1611. }
  1612. /*
  1613. * Generate a keypair, prettier wrapper
  1614. */
  1615. int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  1616. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  1617. {
  1618. int ret;
  1619. if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
  1620. return( ret );
  1621. return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
  1622. }
  1623. /*
  1624. * Check a public-private key pair
  1625. */
  1626. int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
  1627. {
  1628. int ret;
  1629. mbedtls_ecp_point Q;
  1630. mbedtls_ecp_group grp;
  1631. if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
  1632. pub->grp.id != prv->grp.id ||
  1633. mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
  1634. mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
  1635. mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
  1636. {
  1637. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1638. }
  1639. mbedtls_ecp_point_init( &Q );
  1640. mbedtls_ecp_group_init( &grp );
  1641. /* mbedtls_ecp_mul() needs a non-const group... */
  1642. mbedtls_ecp_group_copy( &grp, &prv->grp );
  1643. /* Also checks d is valid */
  1644. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
  1645. if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
  1646. mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
  1647. mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
  1648. {
  1649. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  1650. goto cleanup;
  1651. }
  1652. cleanup:
  1653. mbedtls_ecp_point_free( &Q );
  1654. mbedtls_ecp_group_free( &grp );
  1655. return( ret );
  1656. }
  1657. #if defined(MBEDTLS_SELF_TEST)
  1658. /*
  1659. * Checkup routine
  1660. */
  1661. int mbedtls_ecp_self_test( int verbose )
  1662. {
  1663. int ret;
  1664. size_t i;
  1665. mbedtls_ecp_group grp;
  1666. mbedtls_ecp_point R, P;
  1667. mbedtls_mpi m;
  1668. unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
  1669. /* exponents especially adapted for secp192r1 */
  1670. const char *exponents[] =
  1671. {
  1672. "000000000000000000000000000000000000000000000001", /* one */
  1673. "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
  1674. "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
  1675. "400000000000000000000000000000000000000000000000", /* one and zeros */
  1676. "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
  1677. "555555555555555555555555555555555555555555555555", /* 101010... */
  1678. };
  1679. mbedtls_ecp_group_init( &grp );
  1680. mbedtls_ecp_point_init( &R );
  1681. mbedtls_ecp_point_init( &P );
  1682. mbedtls_mpi_init( &m );
  1683. /* Use secp192r1 if available, or any available curve */
  1684. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  1685. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
  1686. #else
  1687. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
  1688. #endif
  1689. if( verbose != 0 )
  1690. mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
  1691. /* Do a dummy multiplication first to trigger precomputation */
  1692. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
  1693. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
  1694. add_count = 0;
  1695. dbl_count = 0;
  1696. mul_count = 0;
  1697. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
  1698. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
  1699. for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
  1700. {
  1701. add_c_prev = add_count;
  1702. dbl_c_prev = dbl_count;
  1703. mul_c_prev = mul_count;
  1704. add_count = 0;
  1705. dbl_count = 0;
  1706. mul_count = 0;
  1707. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
  1708. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
  1709. if( add_count != add_c_prev ||
  1710. dbl_count != dbl_c_prev ||
  1711. mul_count != mul_c_prev )
  1712. {
  1713. if( verbose != 0 )
  1714. mbedtls_printf( "failed (%u)\n", (unsigned int) i );
  1715. ret = 1;
  1716. goto cleanup;
  1717. }
  1718. }
  1719. if( verbose != 0 )
  1720. mbedtls_printf( "passed\n" );
  1721. if( verbose != 0 )
  1722. mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
  1723. /* We computed P = 2G last time, use it */
  1724. add_count = 0;
  1725. dbl_count = 0;
  1726. mul_count = 0;
  1727. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
  1728. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
  1729. for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
  1730. {
  1731. add_c_prev = add_count;
  1732. dbl_c_prev = dbl_count;
  1733. mul_c_prev = mul_count;
  1734. add_count = 0;
  1735. dbl_count = 0;
  1736. mul_count = 0;
  1737. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
  1738. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
  1739. if( add_count != add_c_prev ||
  1740. dbl_count != dbl_c_prev ||
  1741. mul_count != mul_c_prev )
  1742. {
  1743. if( verbose != 0 )
  1744. mbedtls_printf( "failed (%u)\n", (unsigned int) i );
  1745. ret = 1;
  1746. goto cleanup;
  1747. }
  1748. }
  1749. if( verbose != 0 )
  1750. mbedtls_printf( "passed\n" );
  1751. cleanup:
  1752. if( ret < 0 && verbose != 0 )
  1753. mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
  1754. mbedtls_ecp_group_free( &grp );
  1755. mbedtls_ecp_point_free( &R );
  1756. mbedtls_ecp_point_free( &P );
  1757. mbedtls_mpi_free( &m );
  1758. if( verbose != 0 )
  1759. mbedtls_printf( "\n" );
  1760. return( ret );
  1761. }
  1762. #endif /* MBEDTLS_SELF_TEST */
  1763. #endif /* MBEDTLS_ECP_C */