ecp.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  1. /*
  2. * Elliptic curves over GF(p): generic functions
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * References:
  23. *
  24. * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  25. * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
  26. * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
  27. * RFC 4492 for the related TLS structures and constants
  28. * RFC 7748 for the Curve448 and Curve25519 curve definitions
  29. *
  30. * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
  31. *
  32. * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
  33. * for elliptic curve cryptosystems. In : Cryptographic Hardware and
  34. * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
  35. * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
  36. *
  37. * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
  38. * render ECC resistant against Side Channel Attacks. IACR Cryptology
  39. * ePrint Archive, 2004, vol. 2004, p. 342.
  40. * <http://eprint.iacr.org/2004/342.pdf>
  41. */
  42. #if !defined(MBEDTLS_CONFIG_FILE)
  43. #include "mbedtls/config.h"
  44. #else
  45. #include MBEDTLS_CONFIG_FILE
  46. #endif
  47. /**
  48. * \brief Function level alternative implementation.
  49. *
  50. * The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to
  51. * replace certain functions in this module. The alternative implementations are
  52. * typically hardware accelerators and need to activate the hardware before the
  53. * computation starts and deactivate it after it finishes. The
  54. * mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve
  55. * this purpose.
  56. *
  57. * To preserve the correct functionality the following conditions must hold:
  58. *
  59. * - The alternative implementation must be activated by
  60. * mbedtls_internal_ecp_init() before any of the replaceable functions is
  61. * called.
  62. * - mbedtls_internal_ecp_free() must \b only be called when the alternative
  63. * implementation is activated.
  64. * - mbedtls_internal_ecp_init() must \b not be called when the alternative
  65. * implementation is activated.
  66. * - Public functions must not return while the alternative implementation is
  67. * activated.
  68. * - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
  69. * before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) )
  70. * \endcode ensures that the alternative implementation supports the current
  71. * group.
  72. */
  73. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  74. #endif
  75. #if defined(MBEDTLS_ECP_C)
  76. #include "mbedtls/ecp.h"
  77. #include "mbedtls/threading.h"
  78. #include "mbedtls/platform_util.h"
  79. #include <string.h>
  80. #if !defined(MBEDTLS_ECP_ALT)
  81. /* Parameter validation macros based on platform_util.h */
  82. #define ECP_VALIDATE_RET( cond ) \
  83. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
  84. #define ECP_VALIDATE( cond ) \
  85. MBEDTLS_INTERNAL_VALIDATE( cond )
  86. #if defined(MBEDTLS_PLATFORM_C)
  87. #include "mbedtls/platform.h"
  88. #else
  89. #include <stdlib.h>
  90. #include <stdio.h>
  91. #define mbedtls_printf printf
  92. #define mbedtls_calloc calloc
  93. #define mbedtls_free free
  94. #endif
  95. #include "mbedtls/ecp_internal.h"
  96. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  97. !defined(inline) && !defined(__cplusplus)
  98. #define inline __inline
  99. #endif
  100. #if defined(MBEDTLS_SELF_TEST)
  101. /*
  102. * Counts of point addition and doubling, and field multiplications.
  103. * Used to test resistance of point multiplication to simple timing attacks.
  104. */
  105. static unsigned long add_count, dbl_count, mul_count;
  106. #endif
  107. #if defined(MBEDTLS_ECP_RESTARTABLE)
  108. /*
  109. * Maximum number of "basic operations" to be done in a row.
  110. *
  111. * Default value 0 means that ECC operations will not yield.
  112. * Note that regardless of the value of ecp_max_ops, always at
  113. * least one step is performed before yielding.
  114. *
  115. * Setting ecp_max_ops=1 can be suitable for testing purposes
  116. * as it will interrupt computation at all possible points.
  117. */
  118. static unsigned ecp_max_ops = 0;
  119. /*
  120. * Set ecp_max_ops
  121. */
  122. void mbedtls_ecp_set_max_ops( unsigned max_ops )
  123. {
  124. ecp_max_ops = max_ops;
  125. }
  126. /*
  127. * Check if restart is enabled
  128. */
  129. int mbedtls_ecp_restart_is_enabled( void )
  130. {
  131. return( ecp_max_ops != 0 );
  132. }
  133. /*
  134. * Restart sub-context for ecp_mul_comb()
  135. */
  136. struct mbedtls_ecp_restart_mul
  137. {
  138. mbedtls_ecp_point R; /* current intermediate result */
  139. size_t i; /* current index in various loops, 0 outside */
  140. mbedtls_ecp_point *T; /* table for precomputed points */
  141. unsigned char T_size; /* number of points in table T */
  142. enum { /* what were we doing last time we returned? */
  143. ecp_rsm_init = 0, /* nothing so far, dummy initial state */
  144. ecp_rsm_pre_dbl, /* precompute 2^n multiples */
  145. ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
  146. ecp_rsm_pre_add, /* precompute remaining points by adding */
  147. ecp_rsm_pre_norm_add, /* normalize all precomputed points */
  148. ecp_rsm_comb_core, /* ecp_mul_comb_core() */
  149. ecp_rsm_final_norm, /* do the final normalization */
  150. } state;
  151. };
  152. /*
  153. * Init restart_mul sub-context
  154. */
  155. static void ecp_restart_rsm_init( mbedtls_ecp_restart_mul_ctx *ctx )
  156. {
  157. mbedtls_ecp_point_init( &ctx->R );
  158. ctx->i = 0;
  159. ctx->T = NULL;
  160. ctx->T_size = 0;
  161. ctx->state = ecp_rsm_init;
  162. }
  163. /*
  164. * Free the components of a restart_mul sub-context
  165. */
  166. static void ecp_restart_rsm_free( mbedtls_ecp_restart_mul_ctx *ctx )
  167. {
  168. unsigned char i;
  169. if( ctx == NULL )
  170. return;
  171. mbedtls_ecp_point_free( &ctx->R );
  172. if( ctx->T != NULL )
  173. {
  174. for( i = 0; i < ctx->T_size; i++ )
  175. mbedtls_ecp_point_free( ctx->T + i );
  176. mbedtls_free( ctx->T );
  177. }
  178. ecp_restart_rsm_init( ctx );
  179. }
  180. /*
  181. * Restart context for ecp_muladd()
  182. */
  183. struct mbedtls_ecp_restart_muladd
  184. {
  185. mbedtls_ecp_point mP; /* mP value */
  186. mbedtls_ecp_point R; /* R intermediate result */
  187. enum { /* what should we do next? */
  188. ecp_rsma_mul1 = 0, /* first multiplication */
  189. ecp_rsma_mul2, /* second multiplication */
  190. ecp_rsma_add, /* addition */
  191. ecp_rsma_norm, /* normalization */
  192. } state;
  193. };
  194. /*
  195. * Init restart_muladd sub-context
  196. */
  197. static void ecp_restart_ma_init( mbedtls_ecp_restart_muladd_ctx *ctx )
  198. {
  199. mbedtls_ecp_point_init( &ctx->mP );
  200. mbedtls_ecp_point_init( &ctx->R );
  201. ctx->state = ecp_rsma_mul1;
  202. }
  203. /*
  204. * Free the components of a restart_muladd sub-context
  205. */
  206. static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx )
  207. {
  208. if( ctx == NULL )
  209. return;
  210. mbedtls_ecp_point_free( &ctx->mP );
  211. mbedtls_ecp_point_free( &ctx->R );
  212. ecp_restart_ma_init( ctx );
  213. }
  214. /*
  215. * Initialize a restart context
  216. */
  217. void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
  218. {
  219. ECP_VALIDATE( ctx != NULL );
  220. ctx->ops_done = 0;
  221. ctx->depth = 0;
  222. ctx->rsm = NULL;
  223. ctx->ma = NULL;
  224. }
  225. /*
  226. * Free the components of a restart context
  227. */
  228. void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
  229. {
  230. if( ctx == NULL )
  231. return;
  232. ecp_restart_rsm_free( ctx->rsm );
  233. mbedtls_free( ctx->rsm );
  234. ecp_restart_ma_free( ctx->ma );
  235. mbedtls_free( ctx->ma );
  236. mbedtls_ecp_restart_init( ctx );
  237. }
  238. /*
  239. * Check if we can do the next step
  240. */
  241. int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
  242. mbedtls_ecp_restart_ctx *rs_ctx,
  243. unsigned ops )
  244. {
  245. ECP_VALIDATE_RET( grp != NULL );
  246. if( rs_ctx != NULL && ecp_max_ops != 0 )
  247. {
  248. /* scale depending on curve size: the chosen reference is 256-bit,
  249. * and multiplication is quadratic. Round to the closest integer. */
  250. if( grp->pbits >= 512 )
  251. ops *= 4;
  252. else if( grp->pbits >= 384 )
  253. ops *= 2;
  254. /* Avoid infinite loops: always allow first step.
  255. * Because of that, however, it's not generally true
  256. * that ops_done <= ecp_max_ops, so the check
  257. * ops_done > ecp_max_ops below is mandatory. */
  258. if( ( rs_ctx->ops_done != 0 ) &&
  259. ( rs_ctx->ops_done > ecp_max_ops ||
  260. ops > ecp_max_ops - rs_ctx->ops_done ) )
  261. {
  262. return( MBEDTLS_ERR_ECP_IN_PROGRESS );
  263. }
  264. /* update running count */
  265. rs_ctx->ops_done += ops;
  266. }
  267. return( 0 );
  268. }
  269. /* Call this when entering a function that needs its own sub-context */
  270. #define ECP_RS_ENTER( SUB ) do { \
  271. /* reset ops count for this call if top-level */ \
  272. if( rs_ctx != NULL && rs_ctx->depth++ == 0 ) \
  273. rs_ctx->ops_done = 0; \
  274. \
  275. /* set up our own sub-context if needed */ \
  276. if( mbedtls_ecp_restart_is_enabled() && \
  277. rs_ctx != NULL && rs_ctx->SUB == NULL ) \
  278. { \
  279. rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
  280. if( rs_ctx->SUB == NULL ) \
  281. return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
  282. \
  283. ecp_restart_## SUB ##_init( rs_ctx->SUB ); \
  284. } \
  285. } while( 0 )
  286. /* Call this when leaving a function that needs its own sub-context */
  287. #define ECP_RS_LEAVE( SUB ) do { \
  288. /* clear our sub-context when not in progress (done or error) */ \
  289. if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
  290. ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
  291. { \
  292. ecp_restart_## SUB ##_free( rs_ctx->SUB ); \
  293. mbedtls_free( rs_ctx->SUB ); \
  294. rs_ctx->SUB = NULL; \
  295. } \
  296. \
  297. if( rs_ctx != NULL ) \
  298. rs_ctx->depth--; \
  299. } while( 0 )
  300. #else /* MBEDTLS_ECP_RESTARTABLE */
  301. #define ECP_RS_ENTER( sub ) (void) rs_ctx;
  302. #define ECP_RS_LEAVE( sub ) (void) rs_ctx;
  303. #endif /* MBEDTLS_ECP_RESTARTABLE */
  304. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
  305. defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
  306. defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
  307. defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
  308. defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
  309. defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
  310. defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
  311. defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
  312. defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
  313. defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
  314. defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  315. #define ECP_SHORTWEIERSTRASS
  316. #endif
  317. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
  318. defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
  319. #define ECP_MONTGOMERY
  320. #endif
  321. /*
  322. * Curve types: internal for now, might be exposed later
  323. */
  324. typedef enum
  325. {
  326. ECP_TYPE_NONE = 0,
  327. ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
  328. ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
  329. } ecp_curve_type;
  330. /*
  331. * List of supported curves:
  332. * - internal ID
  333. * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
  334. * - size in bits
  335. * - readable name
  336. *
  337. * Curves are listed in order: largest curves first, and for a given size,
  338. * fastest curves first. This provides the default order for the SSL module.
  339. *
  340. * Reminder: update profiles in x509_crt.c when adding a new curves!
  341. */
  342. static const mbedtls_ecp_curve_info ecp_supported_curves[] =
  343. {
  344. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  345. { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
  346. #endif
  347. #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  348. { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
  349. #endif
  350. #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  351. { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
  352. #endif
  353. #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  354. { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
  355. #endif
  356. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  357. { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
  358. #endif
  359. #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  360. { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
  361. #endif
  362. #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  363. { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
  364. #endif
  365. #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  366. { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
  367. #endif
  368. #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  369. { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
  370. #endif
  371. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  372. { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
  373. #endif
  374. #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  375. { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
  376. #endif
  377. { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
  378. };
  379. #define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
  380. sizeof( ecp_supported_curves[0] )
  381. static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
  382. /*
  383. * List of supported curves and associated info
  384. */
  385. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
  386. {
  387. return( ecp_supported_curves );
  388. }
  389. /*
  390. * List of supported curves, group ID only
  391. */
  392. const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
  393. {
  394. static int init_done = 0;
  395. if( ! init_done )
  396. {
  397. size_t i = 0;
  398. const mbedtls_ecp_curve_info *curve_info;
  399. for( curve_info = mbedtls_ecp_curve_list();
  400. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  401. curve_info++ )
  402. {
  403. ecp_supported_grp_id[i++] = curve_info->grp_id;
  404. }
  405. ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
  406. init_done = 1;
  407. }
  408. return( ecp_supported_grp_id );
  409. }
  410. /*
  411. * Get the curve info for the internal identifier
  412. */
  413. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id )
  414. {
  415. const mbedtls_ecp_curve_info *curve_info;
  416. for( curve_info = mbedtls_ecp_curve_list();
  417. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  418. curve_info++ )
  419. {
  420. if( curve_info->grp_id == grp_id )
  421. return( curve_info );
  422. }
  423. return( NULL );
  424. }
  425. /*
  426. * Get the curve info from the TLS identifier
  427. */
  428. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
  429. {
  430. const mbedtls_ecp_curve_info *curve_info;
  431. for( curve_info = mbedtls_ecp_curve_list();
  432. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  433. curve_info++ )
  434. {
  435. if( curve_info->tls_id == tls_id )
  436. return( curve_info );
  437. }
  438. return( NULL );
  439. }
  440. /*
  441. * Get the curve info from the name
  442. */
  443. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
  444. {
  445. const mbedtls_ecp_curve_info *curve_info;
  446. if( name == NULL )
  447. return( NULL );
  448. for( curve_info = mbedtls_ecp_curve_list();
  449. curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
  450. curve_info++ )
  451. {
  452. if( strcmp( curve_info->name, name ) == 0 )
  453. return( curve_info );
  454. }
  455. return( NULL );
  456. }
  457. /*
  458. * Get the type of a curve
  459. */
  460. static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
  461. {
  462. if( grp->G.X.p == NULL )
  463. return( ECP_TYPE_NONE );
  464. if( grp->G.Y.p == NULL )
  465. return( ECP_TYPE_MONTGOMERY );
  466. else
  467. return( ECP_TYPE_SHORT_WEIERSTRASS );
  468. }
  469. /*
  470. * Initialize (the components of) a point
  471. */
  472. void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
  473. {
  474. ECP_VALIDATE( pt != NULL );
  475. mbedtls_mpi_init( &pt->X );
  476. mbedtls_mpi_init( &pt->Y );
  477. mbedtls_mpi_init( &pt->Z );
  478. }
  479. /*
  480. * Initialize (the components of) a group
  481. */
  482. void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
  483. {
  484. ECP_VALIDATE( grp != NULL );
  485. grp->id = MBEDTLS_ECP_DP_NONE;
  486. mbedtls_mpi_init( &grp->P );
  487. mbedtls_mpi_init( &grp->A );
  488. mbedtls_mpi_init( &grp->B );
  489. mbedtls_ecp_point_init( &grp->G );
  490. mbedtls_mpi_init( &grp->N );
  491. grp->pbits = 0;
  492. grp->nbits = 0;
  493. grp->h = 0;
  494. grp->modp = NULL;
  495. grp->t_pre = NULL;
  496. grp->t_post = NULL;
  497. grp->t_data = NULL;
  498. grp->T = NULL;
  499. grp->T_size = 0;
  500. }
  501. /*
  502. * Initialize (the components of) a key pair
  503. */
  504. void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
  505. {
  506. ECP_VALIDATE( key != NULL );
  507. mbedtls_ecp_group_init( &key->grp );
  508. mbedtls_mpi_init( &key->d );
  509. mbedtls_ecp_point_init( &key->Q );
  510. }
  511. /*
  512. * Unallocate (the components of) a point
  513. */
  514. void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
  515. {
  516. if( pt == NULL )
  517. return;
  518. mbedtls_mpi_free( &( pt->X ) );
  519. mbedtls_mpi_free( &( pt->Y ) );
  520. mbedtls_mpi_free( &( pt->Z ) );
  521. }
  522. /*
  523. * Unallocate (the components of) a group
  524. */
  525. void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
  526. {
  527. size_t i;
  528. if( grp == NULL )
  529. return;
  530. if( grp->h != 1 )
  531. {
  532. mbedtls_mpi_free( &grp->P );
  533. mbedtls_mpi_free( &grp->A );
  534. mbedtls_mpi_free( &grp->B );
  535. mbedtls_ecp_point_free( &grp->G );
  536. mbedtls_mpi_free( &grp->N );
  537. }
  538. if( grp->T != NULL )
  539. {
  540. for( i = 0; i < grp->T_size; i++ )
  541. mbedtls_ecp_point_free( &grp->T[i] );
  542. mbedtls_free( grp->T );
  543. }
  544. mbedtls_platform_zeroize( grp, sizeof( mbedtls_ecp_group ) );
  545. }
  546. /*
  547. * Unallocate (the components of) a key pair
  548. */
  549. void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
  550. {
  551. if( key == NULL )
  552. return;
  553. mbedtls_ecp_group_free( &key->grp );
  554. mbedtls_mpi_free( &key->d );
  555. mbedtls_ecp_point_free( &key->Q );
  556. }
  557. /*
  558. * Copy the contents of a point
  559. */
  560. int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
  561. {
  562. int ret;
  563. ECP_VALIDATE_RET( P != NULL );
  564. ECP_VALIDATE_RET( Q != NULL );
  565. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
  566. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
  567. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
  568. cleanup:
  569. return( ret );
  570. }
  571. /*
  572. * Copy the contents of a group object
  573. */
  574. int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
  575. {
  576. ECP_VALIDATE_RET( dst != NULL );
  577. ECP_VALIDATE_RET( src != NULL );
  578. return( mbedtls_ecp_group_load( dst, src->id ) );
  579. }
  580. /*
  581. * Set point to zero
  582. */
  583. int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
  584. {
  585. int ret;
  586. ECP_VALIDATE_RET( pt != NULL );
  587. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
  588. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
  589. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
  590. cleanup:
  591. return( ret );
  592. }
  593. /*
  594. * Tell if a point is zero
  595. */
  596. int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
  597. {
  598. ECP_VALIDATE_RET( pt != NULL );
  599. return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
  600. }
  601. /*
  602. * Compare two points lazily
  603. */
  604. int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
  605. const mbedtls_ecp_point *Q )
  606. {
  607. ECP_VALIDATE_RET( P != NULL );
  608. ECP_VALIDATE_RET( Q != NULL );
  609. if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
  610. mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
  611. mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
  612. {
  613. return( 0 );
  614. }
  615. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  616. }
  617. /*
  618. * Import a non-zero point from ASCII strings
  619. */
  620. int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
  621. const char *x, const char *y )
  622. {
  623. int ret;
  624. ECP_VALIDATE_RET( P != NULL );
  625. ECP_VALIDATE_RET( x != NULL );
  626. ECP_VALIDATE_RET( y != NULL );
  627. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
  628. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
  629. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
  630. cleanup:
  631. return( ret );
  632. }
  633. /*
  634. * Export a point into unsigned binary data (SEC1 2.3.3)
  635. */
  636. int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp,
  637. const mbedtls_ecp_point *P,
  638. int format, size_t *olen,
  639. unsigned char *buf, size_t buflen )
  640. {
  641. int ret = 0;
  642. size_t plen;
  643. ECP_VALIDATE_RET( grp != NULL );
  644. ECP_VALIDATE_RET( P != NULL );
  645. ECP_VALIDATE_RET( olen != NULL );
  646. ECP_VALIDATE_RET( buf != NULL );
  647. ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  648. format == MBEDTLS_ECP_PF_COMPRESSED );
  649. /*
  650. * Common case: P == 0
  651. */
  652. if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
  653. {
  654. if( buflen < 1 )
  655. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  656. buf[0] = 0x00;
  657. *olen = 1;
  658. return( 0 );
  659. }
  660. plen = mbedtls_mpi_size( &grp->P );
  661. if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
  662. {
  663. *olen = 2 * plen + 1;
  664. if( buflen < *olen )
  665. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  666. buf[0] = 0x04;
  667. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
  668. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
  669. }
  670. else if( format == MBEDTLS_ECP_PF_COMPRESSED )
  671. {
  672. *olen = plen + 1;
  673. if( buflen < *olen )
  674. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  675. buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
  676. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
  677. }
  678. cleanup:
  679. return( ret );
  680. }
  681. /*
  682. * Import a point from unsigned binary data (SEC1 2.3.4)
  683. */
  684. int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
  685. mbedtls_ecp_point *pt,
  686. const unsigned char *buf, size_t ilen )
  687. {
  688. int ret;
  689. size_t plen;
  690. ECP_VALIDATE_RET( grp != NULL );
  691. ECP_VALIDATE_RET( pt != NULL );
  692. ECP_VALIDATE_RET( buf != NULL );
  693. if( ilen < 1 )
  694. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  695. if( buf[0] == 0x00 )
  696. {
  697. if( ilen == 1 )
  698. return( mbedtls_ecp_set_zero( pt ) );
  699. else
  700. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  701. }
  702. plen = mbedtls_mpi_size( &grp->P );
  703. if( buf[0] != 0x04 )
  704. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  705. if( ilen != 2 * plen + 1 )
  706. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  707. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
  708. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
  709. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
  710. cleanup:
  711. return( ret );
  712. }
  713. /*
  714. * Import a point from a TLS ECPoint record (RFC 4492)
  715. * struct {
  716. * opaque point <1..2^8-1>;
  717. * } ECPoint;
  718. */
  719. int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
  720. mbedtls_ecp_point *pt,
  721. const unsigned char **buf, size_t buf_len )
  722. {
  723. unsigned char data_len;
  724. const unsigned char *buf_start;
  725. ECP_VALIDATE_RET( grp != NULL );
  726. ECP_VALIDATE_RET( pt != NULL );
  727. ECP_VALIDATE_RET( buf != NULL );
  728. ECP_VALIDATE_RET( *buf != NULL );
  729. /*
  730. * We must have at least two bytes (1 for length, at least one for data)
  731. */
  732. if( buf_len < 2 )
  733. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  734. data_len = *(*buf)++;
  735. if( data_len < 1 || data_len > buf_len - 1 )
  736. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  737. /*
  738. * Save buffer start for read_binary and update buf
  739. */
  740. buf_start = *buf;
  741. *buf += data_len;
  742. return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) );
  743. }
  744. /*
  745. * Export a point as a TLS ECPoint record (RFC 4492)
  746. * struct {
  747. * opaque point <1..2^8-1>;
  748. * } ECPoint;
  749. */
  750. int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
  751. int format, size_t *olen,
  752. unsigned char *buf, size_t blen )
  753. {
  754. int ret;
  755. ECP_VALIDATE_RET( grp != NULL );
  756. ECP_VALIDATE_RET( pt != NULL );
  757. ECP_VALIDATE_RET( olen != NULL );
  758. ECP_VALIDATE_RET( buf != NULL );
  759. ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  760. format == MBEDTLS_ECP_PF_COMPRESSED );
  761. /*
  762. * buffer length must be at least one, for our length byte
  763. */
  764. if( blen < 1 )
  765. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  766. if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
  767. olen, buf + 1, blen - 1) ) != 0 )
  768. return( ret );
  769. /*
  770. * write length to the first byte and update total length
  771. */
  772. buf[0] = (unsigned char) *olen;
  773. ++*olen;
  774. return( 0 );
  775. }
  776. /*
  777. * Set a group from an ECParameters record (RFC 4492)
  778. */
  779. int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
  780. const unsigned char **buf, size_t len )
  781. {
  782. int ret;
  783. mbedtls_ecp_group_id grp_id;
  784. ECP_VALIDATE_RET( grp != NULL );
  785. ECP_VALIDATE_RET( buf != NULL );
  786. ECP_VALIDATE_RET( *buf != NULL );
  787. if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, len ) ) != 0 )
  788. return( ret );
  789. return( mbedtls_ecp_group_load( grp, grp_id ) );
  790. }
  791. /*
  792. * Read a group id from an ECParameters record (RFC 4492) and convert it to
  793. * mbedtls_ecp_group_id.
  794. */
  795. int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
  796. const unsigned char **buf, size_t len )
  797. {
  798. uint16_t tls_id;
  799. const mbedtls_ecp_curve_info *curve_info;
  800. ECP_VALIDATE_RET( grp != NULL );
  801. ECP_VALIDATE_RET( buf != NULL );
  802. ECP_VALIDATE_RET( *buf != NULL );
  803. /*
  804. * We expect at least three bytes (see below)
  805. */
  806. if( len < 3 )
  807. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  808. /*
  809. * First byte is curve_type; only named_curve is handled
  810. */
  811. if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
  812. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  813. /*
  814. * Next two bytes are the namedcurve value
  815. */
  816. tls_id = *(*buf)++;
  817. tls_id <<= 8;
  818. tls_id |= *(*buf)++;
  819. if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
  820. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  821. *grp = curve_info->grp_id;
  822. return( 0 );
  823. }
  824. /*
  825. * Write the ECParameters record corresponding to a group (RFC 4492)
  826. */
  827. int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
  828. unsigned char *buf, size_t blen )
  829. {
  830. const mbedtls_ecp_curve_info *curve_info;
  831. ECP_VALIDATE_RET( grp != NULL );
  832. ECP_VALIDATE_RET( buf != NULL );
  833. ECP_VALIDATE_RET( olen != NULL );
  834. if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
  835. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  836. /*
  837. * We are going to write 3 bytes (see below)
  838. */
  839. *olen = 3;
  840. if( blen < *olen )
  841. return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
  842. /*
  843. * First byte is curve_type, always named_curve
  844. */
  845. *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
  846. /*
  847. * Next two bytes are the namedcurve value
  848. */
  849. buf[0] = curve_info->tls_id >> 8;
  850. buf[1] = curve_info->tls_id & 0xFF;
  851. return( 0 );
  852. }
  853. /*
  854. * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
  855. * See the documentation of struct mbedtls_ecp_group.
  856. *
  857. * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
  858. */
  859. static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
  860. {
  861. int ret;
  862. if( grp->modp == NULL )
  863. return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
  864. /* N->s < 0 is a much faster test, which fails only if N is 0 */
  865. if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
  866. mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
  867. {
  868. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  869. }
  870. MBEDTLS_MPI_CHK( grp->modp( N ) );
  871. /* N->s < 0 is a much faster test, which fails only if N is 0 */
  872. while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
  873. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
  874. while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
  875. /* we known P, N and the result are positive */
  876. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
  877. cleanup:
  878. return( ret );
  879. }
  880. /*
  881. * Fast mod-p functions expect their argument to be in the 0..p^2 range.
  882. *
  883. * In order to guarantee that, we need to ensure that operands of
  884. * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
  885. * bring the result back to this range.
  886. *
  887. * The following macros are shortcuts for doing that.
  888. */
  889. /*
  890. * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
  891. */
  892. #if defined(MBEDTLS_SELF_TEST)
  893. #define INC_MUL_COUNT mul_count++;
  894. #else
  895. #define INC_MUL_COUNT
  896. #endif
  897. #define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
  898. while( 0 )
  899. /*
  900. * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
  901. * N->s < 0 is a very fast test, which fails only if N is 0
  902. */
  903. #define MOD_SUB( N ) \
  904. while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \
  905. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
  906. /*
  907. * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
  908. * We known P, N and the result are positive, so sub_abs is correct, and
  909. * a bit faster.
  910. */
  911. #define MOD_ADD( N ) \
  912. while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
  913. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
  914. #if defined(ECP_SHORTWEIERSTRASS)
  915. /*
  916. * For curves in short Weierstrass form, we do all the internal operations in
  917. * Jacobian coordinates.
  918. *
  919. * For multiplication, we'll use a comb method with coutermeasueres against
  920. * SPA, hence timing attacks.
  921. */
  922. /*
  923. * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
  924. * Cost: 1N := 1I + 3M + 1S
  925. */
  926. static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
  927. {
  928. int ret;
  929. mbedtls_mpi Zi, ZZi;
  930. if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
  931. return( 0 );
  932. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
  933. if( mbedtls_internal_ecp_grp_capable( grp ) )
  934. return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
  935. #endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
  936. mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
  937. /*
  938. * X = X / Z^2 mod p
  939. */
  940. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
  941. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
  942. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
  943. /*
  944. * Y = Y / Z^3 mod p
  945. */
  946. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
  947. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
  948. /*
  949. * Z = 1
  950. */
  951. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
  952. cleanup:
  953. mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
  954. return( ret );
  955. }
  956. /*
  957. * Normalize jacobian coordinates of an array of (pointers to) points,
  958. * using Montgomery's trick to perform only one inversion mod P.
  959. * (See for example Cohen's "A Course in Computational Algebraic Number
  960. * Theory", Algorithm 10.3.4.)
  961. *
  962. * Warning: fails (returning an error) if one of the points is zero!
  963. * This should never happen, see choice of w in ecp_mul_comb().
  964. *
  965. * Cost: 1N(t) := 1I + (6t - 3)M + 1S
  966. */
  967. static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
  968. mbedtls_ecp_point *T[], size_t T_size )
  969. {
  970. int ret;
  971. size_t i;
  972. mbedtls_mpi *c, u, Zi, ZZi;
  973. if( T_size < 2 )
  974. return( ecp_normalize_jac( grp, *T ) );
  975. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
  976. if( mbedtls_internal_ecp_grp_capable( grp ) )
  977. return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
  978. #endif
  979. if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
  980. return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
  981. for( i = 0; i < T_size; i++ )
  982. mbedtls_mpi_init( &c[i] );
  983. mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
  984. /*
  985. * c[i] = Z_0 * ... * Z_i
  986. */
  987. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
  988. for( i = 1; i < T_size; i++ )
  989. {
  990. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
  991. MOD_MUL( c[i] );
  992. }
  993. /*
  994. * u = 1 / (Z_0 * ... * Z_n) mod P
  995. */
  996. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
  997. for( i = T_size - 1; ; i-- )
  998. {
  999. /*
  1000. * Zi = 1 / Z_i mod p
  1001. * u = 1 / (Z_0 * ... * Z_i) mod P
  1002. */
  1003. if( i == 0 ) {
  1004. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
  1005. }
  1006. else
  1007. {
  1008. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
  1009. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
  1010. }
  1011. /*
  1012. * proceed as in normalize()
  1013. */
  1014. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
  1015. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
  1016. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
  1017. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
  1018. /*
  1019. * Post-precessing: reclaim some memory by shrinking coordinates
  1020. * - not storing Z (always 1)
  1021. * - shrinking other coordinates, but still keeping the same number of
  1022. * limbs as P, as otherwise it will too likely be regrown too fast.
  1023. */
  1024. MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
  1025. MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
  1026. mbedtls_mpi_free( &T[i]->Z );
  1027. if( i == 0 )
  1028. break;
  1029. }
  1030. cleanup:
  1031. mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
  1032. for( i = 0; i < T_size; i++ )
  1033. mbedtls_mpi_free( &c[i] );
  1034. mbedtls_free( c );
  1035. return( ret );
  1036. }
  1037. /*
  1038. * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
  1039. * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
  1040. */
  1041. static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
  1042. mbedtls_ecp_point *Q,
  1043. unsigned char inv )
  1044. {
  1045. int ret;
  1046. unsigned char nonzero;
  1047. mbedtls_mpi mQY;
  1048. mbedtls_mpi_init( &mQY );
  1049. /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
  1050. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
  1051. nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
  1052. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
  1053. cleanup:
  1054. mbedtls_mpi_free( &mQY );
  1055. return( ret );
  1056. }
  1057. /*
  1058. * Point doubling R = 2 P, Jacobian coordinates
  1059. *
  1060. * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
  1061. *
  1062. * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
  1063. * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
  1064. *
  1065. * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
  1066. *
  1067. * Cost: 1D := 3M + 4S (A == 0)
  1068. * 4M + 4S (A == -3)
  1069. * 3M + 6S + 1a otherwise
  1070. */
  1071. static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1072. const mbedtls_ecp_point *P )
  1073. {
  1074. int ret;
  1075. mbedtls_mpi M, S, T, U;
  1076. #if defined(MBEDTLS_SELF_TEST)
  1077. dbl_count++;
  1078. #endif
  1079. #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
  1080. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1081. return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
  1082. #endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
  1083. mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U );
  1084. /* Special case for A = -3 */
  1085. if( grp->A.p == NULL )
  1086. {
  1087. /* M = 3(X + Z^2)(X - Z^2) */
  1088. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
  1089. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
  1090. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
  1091. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
  1092. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
  1093. }
  1094. else
  1095. {
  1096. /* M = 3.X^2 */
  1097. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
  1098. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
  1099. /* Optimize away for "koblitz" curves with A = 0 */
  1100. if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
  1101. {
  1102. /* M += A.Z^4 */
  1103. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
  1104. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
  1105. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
  1106. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
  1107. }
  1108. }
  1109. /* S = 4.X.Y^2 */
  1110. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
  1111. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
  1112. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
  1113. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
  1114. /* U = 8.Y^4 */
  1115. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
  1116. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
  1117. /* T = M^2 - 2.S */
  1118. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
  1119. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
  1120. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
  1121. /* S = M(S - T) - U */
  1122. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
  1123. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
  1124. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
  1125. /* U = 2.Y.Z */
  1126. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
  1127. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
  1128. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
  1129. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
  1130. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
  1131. cleanup:
  1132. mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U );
  1133. return( ret );
  1134. }
  1135. /*
  1136. * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
  1137. *
  1138. * The coordinates of Q must be normalized (= affine),
  1139. * but those of P don't need to. R is not normalized.
  1140. *
  1141. * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
  1142. * None of these cases can happen as intermediate step in ecp_mul_comb():
  1143. * - at each step, P, Q and R are multiples of the base point, the factor
  1144. * being less than its order, so none of them is zero;
  1145. * - Q is an odd multiple of the base point, P an even multiple,
  1146. * due to the choice of precomputed points in the modified comb method.
  1147. * So branches for these cases do not leak secret information.
  1148. *
  1149. * We accept Q->Z being unset (saving memory in tables) as meaning 1.
  1150. *
  1151. * Cost: 1A := 8M + 3S
  1152. */
  1153. static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1154. const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
  1155. {
  1156. int ret;
  1157. mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
  1158. #if defined(MBEDTLS_SELF_TEST)
  1159. add_count++;
  1160. #endif
  1161. #if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
  1162. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1163. return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
  1164. #endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
  1165. /*
  1166. * Trivial cases: P == 0 or Q == 0 (case 1)
  1167. */
  1168. if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
  1169. return( mbedtls_ecp_copy( R, Q ) );
  1170. if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
  1171. return( mbedtls_ecp_copy( R, P ) );
  1172. /*
  1173. * Make sure Q coordinates are normalized
  1174. */
  1175. if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
  1176. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1177. mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
  1178. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  1179. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
  1180. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
  1181. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
  1182. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
  1183. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
  1184. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
  1185. /* Special cases (2) and (3) */
  1186. if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
  1187. {
  1188. if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
  1189. {
  1190. ret = ecp_double_jac( grp, R, P );
  1191. goto cleanup;
  1192. }
  1193. else
  1194. {
  1195. ret = mbedtls_ecp_set_zero( R );
  1196. goto cleanup;
  1197. }
  1198. }
  1199. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
  1200. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
  1201. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
  1202. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
  1203. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
  1204. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
  1205. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
  1206. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
  1207. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
  1208. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
  1209. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
  1210. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
  1211. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
  1212. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
  1213. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
  1214. cleanup:
  1215. mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
  1216. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  1217. return( ret );
  1218. }
  1219. /*
  1220. * Randomize jacobian coordinates:
  1221. * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
  1222. * This is sort of the reverse operation of ecp_normalize_jac().
  1223. *
  1224. * This countermeasure was first suggested in [2].
  1225. */
  1226. static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  1227. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  1228. {
  1229. int ret;
  1230. mbedtls_mpi l, ll;
  1231. size_t p_size;
  1232. int count = 0;
  1233. #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
  1234. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1235. return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
  1236. #endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
  1237. p_size = ( grp->pbits + 7 ) / 8;
  1238. mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
  1239. /* Generate l such that 1 < l < p */
  1240. do
  1241. {
  1242. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
  1243. while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
  1244. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
  1245. if( count++ > 10 )
  1246. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  1247. }
  1248. while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
  1249. /* Z = l * Z */
  1250. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
  1251. /* X = l^2 * X */
  1252. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
  1253. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
  1254. /* Y = l^3 * Y */
  1255. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
  1256. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
  1257. cleanup:
  1258. mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
  1259. return( ret );
  1260. }
  1261. /*
  1262. * Check and define parameters used by the comb method (see below for details)
  1263. */
  1264. #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
  1265. #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
  1266. #endif
  1267. /* d = ceil( n / w ) */
  1268. #define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
  1269. /* number of precomputed points */
  1270. #define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  1271. /*
  1272. * Compute the representation of m that will be used with our comb method.
  1273. *
  1274. * The basic comb method is described in GECC 3.44 for example. We use a
  1275. * modified version that provides resistance to SPA by avoiding zero
  1276. * digits in the representation as in [3]. We modify the method further by
  1277. * requiring that all K_i be odd, which has the small cost that our
  1278. * representation uses one more K_i, due to carries, but saves on the size of
  1279. * the precomputed table.
  1280. *
  1281. * Summary of the comb method and its modifications:
  1282. *
  1283. * - The goal is to compute m*P for some w*d-bit integer m.
  1284. *
  1285. * - The basic comb method splits m into the w-bit integers
  1286. * x[0] .. x[d-1] where x[i] consists of the bits in m whose
  1287. * index has residue i modulo d, and computes m * P as
  1288. * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
  1289. * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
  1290. *
  1291. * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
  1292. * .. + 2^{i-1} S[x[i-1]] - 2^i S[x[i]] + 2^{i+1} S[x[i]] + 2^{i+2} S[x[i+2]] ..,
  1293. * thereby successively converting it into a form where all summands
  1294. * are nonzero, at the cost of negative summands. This is the basic idea of [3].
  1295. *
  1296. * - More generally, even if x[i+1] != 0, we can first transform the sum as
  1297. * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
  1298. * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
  1299. * Performing and iterating this procedure for those x[i] that are even
  1300. * (keeping track of carry), we can transform the original sum into one of the form
  1301. * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
  1302. * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
  1303. * which is why we are only computing half of it in the first place in
  1304. * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
  1305. *
  1306. * - For the sake of compactness, only the seven low-order bits of x[i]
  1307. * are used to represent its absolute value (K_i in the paper), and the msb
  1308. * of x[i] encodes the sign (s_i in the paper): it is set if and only if
  1309. * if s_i == -1;
  1310. *
  1311. * Calling conventions:
  1312. * - x is an array of size d + 1
  1313. * - w is the size, ie number of teeth, of the comb, and must be between
  1314. * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
  1315. * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
  1316. * (the result will be incorrect if these assumptions are not satisfied)
  1317. */
  1318. static void ecp_comb_recode_core( unsigned char x[], size_t d,
  1319. unsigned char w, const mbedtls_mpi *m )
  1320. {
  1321. size_t i, j;
  1322. unsigned char c, cc, adjust;
  1323. memset( x, 0, d+1 );
  1324. /* First get the classical comb values (except for x_d = 0) */
  1325. for( i = 0; i < d; i++ )
  1326. for( j = 0; j < w; j++ )
  1327. x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
  1328. /* Now make sure x_1 .. x_d are odd */
  1329. c = 0;
  1330. for( i = 1; i <= d; i++ )
  1331. {
  1332. /* Add carry and update it */
  1333. cc = x[i] & c;
  1334. x[i] = x[i] ^ c;
  1335. c = cc;
  1336. /* Adjust if needed, avoiding branches */
  1337. adjust = 1 - ( x[i] & 0x01 );
  1338. c |= x[i] & ( x[i-1] * adjust );
  1339. x[i] = x[i] ^ ( x[i-1] * adjust );
  1340. x[i-1] |= adjust << 7;
  1341. }
  1342. }
  1343. /*
  1344. * Precompute points for the adapted comb method
  1345. *
  1346. * Assumption: T must be able to hold 2^{w - 1} elements.
  1347. *
  1348. * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
  1349. * sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P.
  1350. *
  1351. * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
  1352. *
  1353. * Note: Even comb values (those where P would be omitted from the
  1354. * sum defining T[i] above) are not needed in our adaption
  1355. * the comb method. See ecp_comb_recode_core().
  1356. *
  1357. * This function currently works in four steps:
  1358. * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
  1359. * (2) [norm_dbl] Normalization of coordinates of these T[i]
  1360. * (3) [add] Computation of all T[i]
  1361. * (4) [norm_add] Normalization of all T[i]
  1362. *
  1363. * Step 1 can be interrupted but not the others; together with the final
  1364. * coordinate normalization they are the largest steps done at once, depending
  1365. * on the window size. Here are operation counts for P-256:
  1366. *
  1367. * step (2) (3) (4)
  1368. * w = 5 142 165 208
  1369. * w = 4 136 77 160
  1370. * w = 3 130 33 136
  1371. * w = 2 124 11 124
  1372. *
  1373. * So if ECC operations are blocking for too long even with a low max_ops
  1374. * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
  1375. * to minimize maximum blocking time.
  1376. */
  1377. static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
  1378. mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
  1379. unsigned char w, size_t d,
  1380. mbedtls_ecp_restart_ctx *rs_ctx )
  1381. {
  1382. int ret;
  1383. unsigned char i;
  1384. size_t j = 0;
  1385. const unsigned char T_size = 1U << ( w - 1 );
  1386. mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
  1387. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1388. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1389. {
  1390. if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
  1391. goto dbl;
  1392. if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
  1393. goto norm_dbl;
  1394. if( rs_ctx->rsm->state == ecp_rsm_pre_add )
  1395. goto add;
  1396. if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
  1397. goto norm_add;
  1398. }
  1399. #else
  1400. (void) rs_ctx;
  1401. #endif
  1402. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1403. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1404. {
  1405. rs_ctx->rsm->state = ecp_rsm_pre_dbl;
  1406. /* initial state for the loop */
  1407. rs_ctx->rsm->i = 0;
  1408. }
  1409. dbl:
  1410. #endif
  1411. /*
  1412. * Set T[0] = P and
  1413. * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
  1414. */
  1415. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
  1416. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1417. if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
  1418. j = rs_ctx->rsm->i;
  1419. else
  1420. #endif
  1421. j = 0;
  1422. for( ; j < d * ( w - 1 ); j++ )
  1423. {
  1424. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
  1425. i = 1U << ( j / d );
  1426. cur = T + i;
  1427. if( j % d == 0 )
  1428. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
  1429. MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
  1430. }
  1431. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1432. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1433. rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
  1434. norm_dbl:
  1435. #endif
  1436. /*
  1437. * Normalize current elements in T. As T has holes,
  1438. * use an auxiliary array of pointers to elements in T.
  1439. */
  1440. j = 0;
  1441. for( i = 1; i < T_size; i <<= 1 )
  1442. TT[j++] = T + i;
  1443. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
  1444. MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
  1445. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1446. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1447. rs_ctx->rsm->state = ecp_rsm_pre_add;
  1448. add:
  1449. #endif
  1450. /*
  1451. * Compute the remaining ones using the minimal number of additions
  1452. * Be careful to update T[2^l] only after using it!
  1453. */
  1454. MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
  1455. for( i = 1; i < T_size; i <<= 1 )
  1456. {
  1457. j = i;
  1458. while( j-- )
  1459. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
  1460. }
  1461. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1462. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1463. rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
  1464. norm_add:
  1465. #endif
  1466. /*
  1467. * Normalize final elements in T. Even though there are no holes now, we
  1468. * still need the auxiliary array for homogeneity with the previous
  1469. * call. Also, skip T[0] which is already normalised, being a copy of P.
  1470. */
  1471. for( j = 0; j + 1 < T_size; j++ )
  1472. TT[j] = T + j + 1;
  1473. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
  1474. MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
  1475. cleanup:
  1476. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1477. if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
  1478. ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  1479. {
  1480. if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
  1481. rs_ctx->rsm->i = j;
  1482. }
  1483. #endif
  1484. return( ret );
  1485. }
  1486. /*
  1487. * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
  1488. *
  1489. * See ecp_comb_recode_core() for background
  1490. */
  1491. static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1492. const mbedtls_ecp_point T[], unsigned char T_size,
  1493. unsigned char i )
  1494. {
  1495. int ret;
  1496. unsigned char ii, j;
  1497. /* Ignore the "sign" bit and scale down */
  1498. ii = ( i & 0x7Fu ) >> 1;
  1499. /* Read the whole table to thwart cache-based timing attacks */
  1500. for( j = 0; j < T_size; j++ )
  1501. {
  1502. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
  1503. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
  1504. }
  1505. /* Safely invert result if i is "negative" */
  1506. MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
  1507. cleanup:
  1508. return( ret );
  1509. }
  1510. /*
  1511. * Core multiplication algorithm for the (modified) comb method.
  1512. * This part is actually common with the basic comb method (GECC 3.44)
  1513. *
  1514. * Cost: d A + d D + 1 R
  1515. */
  1516. static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1517. const mbedtls_ecp_point T[], unsigned char T_size,
  1518. const unsigned char x[], size_t d,
  1519. int (*f_rng)(void *, unsigned char *, size_t),
  1520. void *p_rng,
  1521. mbedtls_ecp_restart_ctx *rs_ctx )
  1522. {
  1523. int ret;
  1524. mbedtls_ecp_point Txi;
  1525. size_t i;
  1526. mbedtls_ecp_point_init( &Txi );
  1527. #if !defined(MBEDTLS_ECP_RESTARTABLE)
  1528. (void) rs_ctx;
  1529. #endif
  1530. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1531. if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
  1532. rs_ctx->rsm->state != ecp_rsm_comb_core )
  1533. {
  1534. rs_ctx->rsm->i = 0;
  1535. rs_ctx->rsm->state = ecp_rsm_comb_core;
  1536. }
  1537. /* new 'if' instead of nested for the sake of the 'else' branch */
  1538. if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
  1539. {
  1540. /* restore current index (R already pointing to rs_ctx->rsm->R) */
  1541. i = rs_ctx->rsm->i;
  1542. }
  1543. else
  1544. #endif
  1545. {
  1546. /* Start with a non-zero point and randomize its coordinates */
  1547. i = d;
  1548. MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
  1549. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
  1550. if( f_rng != 0 )
  1551. MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
  1552. }
  1553. while( i != 0 )
  1554. {
  1555. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
  1556. --i;
  1557. MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
  1558. MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
  1559. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
  1560. }
  1561. cleanup:
  1562. mbedtls_ecp_point_free( &Txi );
  1563. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1564. if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
  1565. ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  1566. {
  1567. rs_ctx->rsm->i = i;
  1568. /* no need to save R, already pointing to rs_ctx->rsm->R */
  1569. }
  1570. #endif
  1571. return( ret );
  1572. }
  1573. /*
  1574. * Recode the scalar to get constant-time comb multiplication
  1575. *
  1576. * As the actual scalar recoding needs an odd scalar as a starting point,
  1577. * this wrapper ensures that by replacing m by N - m if necessary, and
  1578. * informs the caller that the result of multiplication will be negated.
  1579. *
  1580. * This works because we only support large prime order for Short Weierstrass
  1581. * curves, so N is always odd hence either m or N - m is.
  1582. *
  1583. * See ecp_comb_recode_core() for background.
  1584. */
  1585. static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
  1586. const mbedtls_mpi *m,
  1587. unsigned char k[COMB_MAX_D + 1],
  1588. size_t d,
  1589. unsigned char w,
  1590. unsigned char *parity_trick )
  1591. {
  1592. int ret;
  1593. mbedtls_mpi M, mm;
  1594. mbedtls_mpi_init( &M );
  1595. mbedtls_mpi_init( &mm );
  1596. /* N is always odd (see above), just make extra sure */
  1597. if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
  1598. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1599. /* do we need the parity trick? */
  1600. *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
  1601. /* execute parity fix in constant time */
  1602. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
  1603. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
  1604. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
  1605. /* actual scalar recoding */
  1606. ecp_comb_recode_core( k, d, w, &M );
  1607. cleanup:
  1608. mbedtls_mpi_free( &mm );
  1609. mbedtls_mpi_free( &M );
  1610. return( ret );
  1611. }
  1612. /*
  1613. * Perform comb multiplication (for short Weierstrass curves)
  1614. * once the auxiliary table has been pre-computed.
  1615. *
  1616. * Scalar recoding may use a parity trick that makes us compute -m * P,
  1617. * if that is the case we'll need to recover m * P at the end.
  1618. */
  1619. static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
  1620. mbedtls_ecp_point *R,
  1621. const mbedtls_mpi *m,
  1622. const mbedtls_ecp_point *T,
  1623. unsigned char T_size,
  1624. unsigned char w,
  1625. size_t d,
  1626. int (*f_rng)(void *, unsigned char *, size_t),
  1627. void *p_rng,
  1628. mbedtls_ecp_restart_ctx *rs_ctx )
  1629. {
  1630. int ret;
  1631. unsigned char parity_trick;
  1632. unsigned char k[COMB_MAX_D + 1];
  1633. mbedtls_ecp_point *RR = R;
  1634. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1635. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1636. {
  1637. RR = &rs_ctx->rsm->R;
  1638. if( rs_ctx->rsm->state == ecp_rsm_final_norm )
  1639. goto final_norm;
  1640. }
  1641. #endif
  1642. MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
  1643. &parity_trick ) );
  1644. MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
  1645. f_rng, p_rng, rs_ctx ) );
  1646. MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
  1647. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1648. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1649. rs_ctx->rsm->state = ecp_rsm_final_norm;
  1650. final_norm:
  1651. #endif
  1652. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
  1653. MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
  1654. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1655. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1656. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
  1657. #endif
  1658. cleanup:
  1659. return( ret );
  1660. }
  1661. /*
  1662. * Pick window size based on curve size and whether we optimize for base point
  1663. */
  1664. static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
  1665. unsigned char p_eq_g )
  1666. {
  1667. unsigned char w;
  1668. /*
  1669. * Minimize the number of multiplications, that is minimize
  1670. * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
  1671. * (see costs of the various parts, with 1S = 1M)
  1672. */
  1673. w = grp->nbits >= 384 ? 5 : 4;
  1674. /*
  1675. * If P == G, pre-compute a bit more, since this may be re-used later.
  1676. * Just adding one avoids upping the cost of the first mul too much,
  1677. * and the memory cost too.
  1678. */
  1679. if( p_eq_g )
  1680. w++;
  1681. /*
  1682. * Make sure w is within bounds.
  1683. * (The last test is useful only for very small curves in the test suite.)
  1684. */
  1685. if( w > MBEDTLS_ECP_WINDOW_SIZE )
  1686. w = MBEDTLS_ECP_WINDOW_SIZE;
  1687. if( w >= grp->nbits )
  1688. w = 2;
  1689. return( w );
  1690. }
  1691. /*
  1692. * Multiplication using the comb method - for curves in short Weierstrass form
  1693. *
  1694. * This function is mainly responsible for administrative work:
  1695. * - managing the restart context if enabled
  1696. * - managing the table of precomputed points (passed between the below two
  1697. * functions): allocation, computation, ownership tranfer, freeing.
  1698. *
  1699. * It delegates the actual arithmetic work to:
  1700. * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
  1701. *
  1702. * See comments on ecp_comb_recode_core() regarding the computation strategy.
  1703. */
  1704. static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1705. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1706. int (*f_rng)(void *, unsigned char *, size_t),
  1707. void *p_rng,
  1708. mbedtls_ecp_restart_ctx *rs_ctx )
  1709. {
  1710. int ret;
  1711. unsigned char w, p_eq_g, i;
  1712. size_t d;
  1713. unsigned char T_size, T_ok;
  1714. mbedtls_ecp_point *T;
  1715. ECP_RS_ENTER( rsm );
  1716. /* Is P the base point ? */
  1717. #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
  1718. p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
  1719. mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
  1720. #else
  1721. p_eq_g = 0;
  1722. #endif
  1723. /* Pick window size and deduce related sizes */
  1724. w = ecp_pick_window_size( grp, p_eq_g );
  1725. T_size = 1U << ( w - 1 );
  1726. d = ( grp->nbits + w - 1 ) / w;
  1727. /* Pre-computed table: do we have it already for the base point? */
  1728. if( p_eq_g && grp->T != NULL )
  1729. {
  1730. /* second pointer to the same table, will be deleted on exit */
  1731. T = grp->T;
  1732. T_ok = 1;
  1733. }
  1734. else
  1735. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1736. /* Pre-computed table: do we have one in progress? complete? */
  1737. if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
  1738. {
  1739. /* transfer ownership of T from rsm to local function */
  1740. T = rs_ctx->rsm->T;
  1741. rs_ctx->rsm->T = NULL;
  1742. rs_ctx->rsm->T_size = 0;
  1743. /* This effectively jumps to the call to mul_comb_after_precomp() */
  1744. T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
  1745. }
  1746. else
  1747. #endif
  1748. /* Allocate table if we didn't have any */
  1749. {
  1750. T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
  1751. if( T == NULL )
  1752. {
  1753. ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
  1754. goto cleanup;
  1755. }
  1756. for( i = 0; i < T_size; i++ )
  1757. mbedtls_ecp_point_init( &T[i] );
  1758. T_ok = 0;
  1759. }
  1760. /* Compute table (or finish computing it) if not done already */
  1761. if( !T_ok )
  1762. {
  1763. MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
  1764. if( p_eq_g )
  1765. {
  1766. /* almost transfer ownership of T to the group, but keep a copy of
  1767. * the pointer to use for calling the next function more easily */
  1768. grp->T = T;
  1769. grp->T_size = T_size;
  1770. }
  1771. }
  1772. /* Actual comb multiplication using precomputed points */
  1773. MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
  1774. T, T_size, w, d,
  1775. f_rng, p_rng, rs_ctx ) );
  1776. cleanup:
  1777. /* does T belong to the group? */
  1778. if( T == grp->T )
  1779. T = NULL;
  1780. /* does T belong to the restart context? */
  1781. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1782. if( rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL )
  1783. {
  1784. /* transfer ownership of T from local function to rsm */
  1785. rs_ctx->rsm->T_size = T_size;
  1786. rs_ctx->rsm->T = T;
  1787. T = NULL;
  1788. }
  1789. #endif
  1790. /* did T belong to us? then let's destroy it! */
  1791. if( T != NULL )
  1792. {
  1793. for( i = 0; i < T_size; i++ )
  1794. mbedtls_ecp_point_free( &T[i] );
  1795. mbedtls_free( T );
  1796. }
  1797. /* don't free R while in progress in case R == P */
  1798. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1799. if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  1800. #endif
  1801. /* prevent caller from using invalid value */
  1802. if( ret != 0 )
  1803. mbedtls_ecp_point_free( R );
  1804. ECP_RS_LEAVE( rsm );
  1805. return( ret );
  1806. }
  1807. #endif /* ECP_SHORTWEIERSTRASS */
  1808. #if defined(ECP_MONTGOMERY)
  1809. /*
  1810. * For Montgomery curves, we do all the internal arithmetic in projective
  1811. * coordinates. Import/export of points uses only the x coordinates, which is
  1812. * internaly represented as X / Z.
  1813. *
  1814. * For scalar multiplication, we'll use a Montgomery ladder.
  1815. */
  1816. /*
  1817. * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
  1818. * Cost: 1M + 1I
  1819. */
  1820. static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
  1821. {
  1822. int ret;
  1823. #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
  1824. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1825. return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
  1826. #endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
  1827. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
  1828. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
  1829. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
  1830. cleanup:
  1831. return( ret );
  1832. }
  1833. /*
  1834. * Randomize projective x/z coordinates:
  1835. * (X, Z) -> (l X, l Z) for random l
  1836. * This is sort of the reverse operation of ecp_normalize_mxz().
  1837. *
  1838. * This countermeasure was first suggested in [2].
  1839. * Cost: 2M
  1840. */
  1841. static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
  1842. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  1843. {
  1844. int ret;
  1845. mbedtls_mpi l;
  1846. size_t p_size;
  1847. int count = 0;
  1848. #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
  1849. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1850. return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
  1851. #endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
  1852. p_size = ( grp->pbits + 7 ) / 8;
  1853. mbedtls_mpi_init( &l );
  1854. /* Generate l such that 1 < l < p */
  1855. do
  1856. {
  1857. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
  1858. while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
  1859. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
  1860. if( count++ > 10 )
  1861. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  1862. }
  1863. while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
  1864. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
  1865. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
  1866. cleanup:
  1867. mbedtls_mpi_free( &l );
  1868. return( ret );
  1869. }
  1870. /*
  1871. * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
  1872. * for Montgomery curves in x/z coordinates.
  1873. *
  1874. * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
  1875. * with
  1876. * d = X1
  1877. * P = (X2, Z2)
  1878. * Q = (X3, Z3)
  1879. * R = (X4, Z4)
  1880. * S = (X5, Z5)
  1881. * and eliminating temporary variables tO, ..., t4.
  1882. *
  1883. * Cost: 5M + 4S
  1884. */
  1885. static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
  1886. mbedtls_ecp_point *R, mbedtls_ecp_point *S,
  1887. const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
  1888. const mbedtls_mpi *d )
  1889. {
  1890. int ret;
  1891. mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
  1892. #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
  1893. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1894. return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
  1895. #endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
  1896. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
  1897. mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
  1898. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
  1899. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
  1900. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
  1901. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
  1902. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
  1903. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
  1904. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
  1905. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
  1906. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
  1907. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
  1908. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
  1909. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
  1910. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
  1911. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
  1912. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
  1913. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
  1914. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
  1915. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
  1916. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
  1917. cleanup:
  1918. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
  1919. mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
  1920. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
  1921. return( ret );
  1922. }
  1923. /*
  1924. * Multiplication with Montgomery ladder in x/z coordinates,
  1925. * for curves in Montgomery form
  1926. */
  1927. static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1928. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1929. int (*f_rng)(void *, unsigned char *, size_t),
  1930. void *p_rng )
  1931. {
  1932. int ret;
  1933. size_t i;
  1934. unsigned char b;
  1935. mbedtls_ecp_point RP;
  1936. mbedtls_mpi PX;
  1937. mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
  1938. /* Save PX and read from P before writing to R, in case P == R */
  1939. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
  1940. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
  1941. /* Set R to zero in modified x/z coordinates */
  1942. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
  1943. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
  1944. mbedtls_mpi_free( &R->Y );
  1945. /* RP.X might be sligtly larger than P, so reduce it */
  1946. MOD_ADD( RP.X );
  1947. /* Randomize coordinates of the starting point */
  1948. if( f_rng != NULL )
  1949. MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
  1950. /* Loop invariant: R = result so far, RP = R + P */
  1951. i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
  1952. while( i-- > 0 )
  1953. {
  1954. b = mbedtls_mpi_get_bit( m, i );
  1955. /*
  1956. * if (b) R = 2R + P else R = 2R,
  1957. * which is:
  1958. * if (b) double_add( RP, R, RP, R )
  1959. * else double_add( R, RP, R, RP )
  1960. * but using safe conditional swaps to avoid leaks
  1961. */
  1962. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
  1963. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
  1964. MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
  1965. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
  1966. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
  1967. }
  1968. MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
  1969. cleanup:
  1970. mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
  1971. return( ret );
  1972. }
  1973. #endif /* ECP_MONTGOMERY */
  1974. /*
  1975. * Restartable multiplication R = m * P
  1976. */
  1977. int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1978. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1979. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  1980. mbedtls_ecp_restart_ctx *rs_ctx )
  1981. {
  1982. int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  1983. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  1984. char is_grp_capable = 0;
  1985. #endif
  1986. ECP_VALIDATE_RET( grp != NULL );
  1987. ECP_VALIDATE_RET( R != NULL );
  1988. ECP_VALIDATE_RET( m != NULL );
  1989. ECP_VALIDATE_RET( P != NULL );
  1990. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1991. /* reset ops count for this call if top-level */
  1992. if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
  1993. rs_ctx->ops_done = 0;
  1994. #endif
  1995. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  1996. if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
  1997. MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
  1998. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  1999. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2000. /* skip argument check when restarting */
  2001. if( rs_ctx == NULL || rs_ctx->rsm == NULL )
  2002. #endif
  2003. {
  2004. /* check_privkey is free */
  2005. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
  2006. /* Common sanity checks */
  2007. MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
  2008. MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
  2009. }
  2010. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  2011. #if defined(ECP_MONTGOMERY)
  2012. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  2013. MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
  2014. #endif
  2015. #if defined(ECP_SHORTWEIERSTRASS)
  2016. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  2017. MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
  2018. #endif
  2019. cleanup:
  2020. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2021. if( is_grp_capable )
  2022. mbedtls_internal_ecp_free( grp );
  2023. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  2024. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2025. if( rs_ctx != NULL )
  2026. rs_ctx->depth--;
  2027. #endif
  2028. return( ret );
  2029. }
  2030. /*
  2031. * Multiplication R = m * P
  2032. */
  2033. int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  2034. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  2035. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  2036. {
  2037. ECP_VALIDATE_RET( grp != NULL );
  2038. ECP_VALIDATE_RET( R != NULL );
  2039. ECP_VALIDATE_RET( m != NULL );
  2040. ECP_VALIDATE_RET( P != NULL );
  2041. return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
  2042. }
  2043. #if defined(ECP_SHORTWEIERSTRASS)
  2044. /*
  2045. * Check that an affine point is valid as a public key,
  2046. * short weierstrass curves (SEC1 3.2.3.1)
  2047. */
  2048. static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  2049. {
  2050. int ret;
  2051. mbedtls_mpi YY, RHS;
  2052. /* pt coordinates must be normalized for our checks */
  2053. if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
  2054. mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
  2055. mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
  2056. mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
  2057. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2058. mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
  2059. /*
  2060. * YY = Y^2
  2061. * RHS = X (X^2 + A) + B = X^3 + A X + B
  2062. */
  2063. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
  2064. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
  2065. /* Special case for A = -3 */
  2066. if( grp->A.p == NULL )
  2067. {
  2068. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
  2069. }
  2070. else
  2071. {
  2072. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
  2073. }
  2074. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
  2075. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
  2076. if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
  2077. ret = MBEDTLS_ERR_ECP_INVALID_KEY;
  2078. cleanup:
  2079. mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
  2080. return( ret );
  2081. }
  2082. #endif /* ECP_SHORTWEIERSTRASS */
  2083. /*
  2084. * R = m * P with shortcuts for m == 1 and m == -1
  2085. * NOT constant-time - ONLY for short Weierstrass!
  2086. */
  2087. static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
  2088. mbedtls_ecp_point *R,
  2089. const mbedtls_mpi *m,
  2090. const mbedtls_ecp_point *P,
  2091. mbedtls_ecp_restart_ctx *rs_ctx )
  2092. {
  2093. int ret;
  2094. if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
  2095. {
  2096. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
  2097. }
  2098. else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
  2099. {
  2100. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
  2101. if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
  2102. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
  2103. }
  2104. else
  2105. {
  2106. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
  2107. NULL, NULL, rs_ctx ) );
  2108. }
  2109. cleanup:
  2110. return( ret );
  2111. }
  2112. /*
  2113. * Restartable linear combination
  2114. * NOT constant-time
  2115. */
  2116. int mbedtls_ecp_muladd_restartable(
  2117. mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  2118. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  2119. const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
  2120. mbedtls_ecp_restart_ctx *rs_ctx )
  2121. {
  2122. int ret;
  2123. mbedtls_ecp_point mP;
  2124. mbedtls_ecp_point *pmP = &mP;
  2125. mbedtls_ecp_point *pR = R;
  2126. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2127. char is_grp_capable = 0;
  2128. #endif
  2129. ECP_VALIDATE_RET( grp != NULL );
  2130. ECP_VALIDATE_RET( R != NULL );
  2131. ECP_VALIDATE_RET( m != NULL );
  2132. ECP_VALIDATE_RET( P != NULL );
  2133. ECP_VALIDATE_RET( n != NULL );
  2134. ECP_VALIDATE_RET( Q != NULL );
  2135. if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
  2136. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  2137. mbedtls_ecp_point_init( &mP );
  2138. ECP_RS_ENTER( ma );
  2139. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2140. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2141. {
  2142. /* redirect intermediate results to restart context */
  2143. pmP = &rs_ctx->ma->mP;
  2144. pR = &rs_ctx->ma->R;
  2145. /* jump to next operation */
  2146. if( rs_ctx->ma->state == ecp_rsma_mul2 )
  2147. goto mul2;
  2148. if( rs_ctx->ma->state == ecp_rsma_add )
  2149. goto add;
  2150. if( rs_ctx->ma->state == ecp_rsma_norm )
  2151. goto norm;
  2152. }
  2153. #endif /* MBEDTLS_ECP_RESTARTABLE */
  2154. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
  2155. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2156. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2157. rs_ctx->ma->state = ecp_rsma_mul2;
  2158. mul2:
  2159. #endif
  2160. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
  2161. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2162. if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
  2163. MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
  2164. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  2165. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2166. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2167. rs_ctx->ma->state = ecp_rsma_add;
  2168. add:
  2169. #endif
  2170. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
  2171. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
  2172. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2173. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2174. rs_ctx->ma->state = ecp_rsma_norm;
  2175. norm:
  2176. #endif
  2177. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
  2178. MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
  2179. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2180. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2181. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
  2182. #endif
  2183. cleanup:
  2184. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2185. if( is_grp_capable )
  2186. mbedtls_internal_ecp_free( grp );
  2187. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  2188. mbedtls_ecp_point_free( &mP );
  2189. ECP_RS_LEAVE( ma );
  2190. return( ret );
  2191. }
  2192. /*
  2193. * Linear combination
  2194. * NOT constant-time
  2195. */
  2196. int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  2197. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  2198. const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
  2199. {
  2200. ECP_VALIDATE_RET( grp != NULL );
  2201. ECP_VALIDATE_RET( R != NULL );
  2202. ECP_VALIDATE_RET( m != NULL );
  2203. ECP_VALIDATE_RET( P != NULL );
  2204. ECP_VALIDATE_RET( n != NULL );
  2205. ECP_VALIDATE_RET( Q != NULL );
  2206. return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
  2207. }
  2208. #if defined(ECP_MONTGOMERY)
  2209. /*
  2210. * Check validity of a public key for Montgomery curves with x-only schemes
  2211. */
  2212. static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  2213. {
  2214. /* [Curve25519 p. 5] Just check X is the correct number of bytes */
  2215. /* Allow any public value, if it's too big then we'll just reduce it mod p
  2216. * (RFC 7748 sec. 5 para. 3). */
  2217. if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
  2218. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2219. return( 0 );
  2220. }
  2221. #endif /* ECP_MONTGOMERY */
  2222. /*
  2223. * Check that a point is valid as a public key
  2224. */
  2225. int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
  2226. const mbedtls_ecp_point *pt )
  2227. {
  2228. ECP_VALIDATE_RET( grp != NULL );
  2229. ECP_VALIDATE_RET( pt != NULL );
  2230. /* Must use affine coordinates */
  2231. if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
  2232. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2233. #if defined(ECP_MONTGOMERY)
  2234. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  2235. return( ecp_check_pubkey_mx( grp, pt ) );
  2236. #endif
  2237. #if defined(ECP_SHORTWEIERSTRASS)
  2238. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  2239. return( ecp_check_pubkey_sw( grp, pt ) );
  2240. #endif
  2241. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  2242. }
  2243. /*
  2244. * Check that an mbedtls_mpi is valid as a private key
  2245. */
  2246. int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
  2247. const mbedtls_mpi *d )
  2248. {
  2249. ECP_VALIDATE_RET( grp != NULL );
  2250. ECP_VALIDATE_RET( d != NULL );
  2251. #if defined(ECP_MONTGOMERY)
  2252. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  2253. {
  2254. /* see RFC 7748 sec. 5 para. 5 */
  2255. if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
  2256. mbedtls_mpi_get_bit( d, 1 ) != 0 ||
  2257. mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
  2258. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2259. /* see [Curve25519] page 5 */
  2260. if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
  2261. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2262. return( 0 );
  2263. }
  2264. #endif /* ECP_MONTGOMERY */
  2265. #if defined(ECP_SHORTWEIERSTRASS)
  2266. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  2267. {
  2268. /* see SEC1 3.2 */
  2269. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
  2270. mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  2271. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2272. else
  2273. return( 0 );
  2274. }
  2275. #endif /* ECP_SHORTWEIERSTRASS */
  2276. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  2277. }
  2278. /*
  2279. * Generate a private key
  2280. */
  2281. int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
  2282. mbedtls_mpi *d,
  2283. int (*f_rng)(void *, unsigned char *, size_t),
  2284. void *p_rng )
  2285. {
  2286. int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  2287. size_t n_size;
  2288. ECP_VALIDATE_RET( grp != NULL );
  2289. ECP_VALIDATE_RET( d != NULL );
  2290. ECP_VALIDATE_RET( f_rng != NULL );
  2291. n_size = ( grp->nbits + 7 ) / 8;
  2292. #if defined(ECP_MONTGOMERY)
  2293. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  2294. {
  2295. /* [M225] page 5 */
  2296. size_t b;
  2297. do {
  2298. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
  2299. } while( mbedtls_mpi_bitlen( d ) == 0);
  2300. /* Make sure the most significant bit is nbits */
  2301. b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
  2302. if( b > grp->nbits )
  2303. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
  2304. else
  2305. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
  2306. /* Make sure the last two bits are unset for Curve448, three bits for
  2307. Curve25519 */
  2308. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
  2309. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
  2310. if( grp->nbits == 254 )
  2311. {
  2312. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
  2313. }
  2314. }
  2315. #endif /* ECP_MONTGOMERY */
  2316. #if defined(ECP_SHORTWEIERSTRASS)
  2317. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  2318. {
  2319. /* SEC1 3.2.1: Generate d such that 1 <= n < N */
  2320. int count = 0;
  2321. /*
  2322. * Match the procedure given in RFC 6979 (deterministic ECDSA):
  2323. * - use the same byte ordering;
  2324. * - keep the leftmost nbits bits of the generated octet string;
  2325. * - try until result is in the desired range.
  2326. * This also avoids any biais, which is especially important for ECDSA.
  2327. */
  2328. do
  2329. {
  2330. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
  2331. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
  2332. /*
  2333. * Each try has at worst a probability 1/2 of failing (the msb has
  2334. * a probability 1/2 of being 0, and then the result will be < N),
  2335. * so after 30 tries failure probability is a most 2**(-30).
  2336. *
  2337. * For most curves, 1 try is enough with overwhelming probability,
  2338. * since N starts with a lot of 1s in binary, but some curves
  2339. * such as secp224k1 are actually very close to the worst case.
  2340. */
  2341. if( ++count > 30 )
  2342. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  2343. }
  2344. while( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
  2345. mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 );
  2346. }
  2347. #endif /* ECP_SHORTWEIERSTRASS */
  2348. cleanup:
  2349. return( ret );
  2350. }
  2351. /*
  2352. * Generate a keypair with configurable base point
  2353. */
  2354. int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  2355. const mbedtls_ecp_point *G,
  2356. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  2357. int (*f_rng)(void *, unsigned char *, size_t),
  2358. void *p_rng )
  2359. {
  2360. int ret;
  2361. ECP_VALIDATE_RET( grp != NULL );
  2362. ECP_VALIDATE_RET( d != NULL );
  2363. ECP_VALIDATE_RET( G != NULL );
  2364. ECP_VALIDATE_RET( Q != NULL );
  2365. ECP_VALIDATE_RET( f_rng != NULL );
  2366. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
  2367. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
  2368. cleanup:
  2369. return( ret );
  2370. }
  2371. /*
  2372. * Generate key pair, wrapper for conventional base point
  2373. */
  2374. int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
  2375. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  2376. int (*f_rng)(void *, unsigned char *, size_t),
  2377. void *p_rng )
  2378. {
  2379. ECP_VALIDATE_RET( grp != NULL );
  2380. ECP_VALIDATE_RET( d != NULL );
  2381. ECP_VALIDATE_RET( Q != NULL );
  2382. ECP_VALIDATE_RET( f_rng != NULL );
  2383. return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
  2384. }
  2385. /*
  2386. * Generate a keypair, prettier wrapper
  2387. */
  2388. int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  2389. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  2390. {
  2391. int ret;
  2392. ECP_VALIDATE_RET( key != NULL );
  2393. ECP_VALIDATE_RET( f_rng != NULL );
  2394. if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
  2395. return( ret );
  2396. return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
  2397. }
  2398. /*
  2399. * Check a public-private key pair
  2400. */
  2401. int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
  2402. {
  2403. int ret;
  2404. mbedtls_ecp_point Q;
  2405. mbedtls_ecp_group grp;
  2406. ECP_VALIDATE_RET( pub != NULL );
  2407. ECP_VALIDATE_RET( prv != NULL );
  2408. if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
  2409. pub->grp.id != prv->grp.id ||
  2410. mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
  2411. mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
  2412. mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
  2413. {
  2414. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  2415. }
  2416. mbedtls_ecp_point_init( &Q );
  2417. mbedtls_ecp_group_init( &grp );
  2418. /* mbedtls_ecp_mul() needs a non-const group... */
  2419. mbedtls_ecp_group_copy( &grp, &prv->grp );
  2420. /* Also checks d is valid */
  2421. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
  2422. if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
  2423. mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
  2424. mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
  2425. {
  2426. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  2427. goto cleanup;
  2428. }
  2429. cleanup:
  2430. mbedtls_ecp_point_free( &Q );
  2431. mbedtls_ecp_group_free( &grp );
  2432. return( ret );
  2433. }
  2434. #if defined(MBEDTLS_SELF_TEST)
  2435. /*
  2436. * Checkup routine
  2437. */
  2438. int mbedtls_ecp_self_test( int verbose )
  2439. {
  2440. int ret;
  2441. size_t i;
  2442. mbedtls_ecp_group grp;
  2443. mbedtls_ecp_point R, P;
  2444. mbedtls_mpi m;
  2445. unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
  2446. /* exponents especially adapted for secp192r1 */
  2447. const char *exponents[] =
  2448. {
  2449. "000000000000000000000000000000000000000000000001", /* one */
  2450. "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
  2451. "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
  2452. "400000000000000000000000000000000000000000000000", /* one and zeros */
  2453. "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
  2454. "555555555555555555555555555555555555555555555555", /* 101010... */
  2455. };
  2456. mbedtls_ecp_group_init( &grp );
  2457. mbedtls_ecp_point_init( &R );
  2458. mbedtls_ecp_point_init( &P );
  2459. mbedtls_mpi_init( &m );
  2460. /* Use secp192r1 if available, or any available curve */
  2461. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  2462. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
  2463. #else
  2464. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
  2465. #endif
  2466. if( verbose != 0 )
  2467. mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
  2468. /* Do a dummy multiplication first to trigger precomputation */
  2469. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
  2470. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
  2471. add_count = 0;
  2472. dbl_count = 0;
  2473. mul_count = 0;
  2474. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
  2475. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
  2476. for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
  2477. {
  2478. add_c_prev = add_count;
  2479. dbl_c_prev = dbl_count;
  2480. mul_c_prev = mul_count;
  2481. add_count = 0;
  2482. dbl_count = 0;
  2483. mul_count = 0;
  2484. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
  2485. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
  2486. if( add_count != add_c_prev ||
  2487. dbl_count != dbl_c_prev ||
  2488. mul_count != mul_c_prev )
  2489. {
  2490. if( verbose != 0 )
  2491. mbedtls_printf( "failed (%u)\n", (unsigned int) i );
  2492. ret = 1;
  2493. goto cleanup;
  2494. }
  2495. }
  2496. if( verbose != 0 )
  2497. mbedtls_printf( "passed\n" );
  2498. if( verbose != 0 )
  2499. mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
  2500. /* We computed P = 2G last time, use it */
  2501. add_count = 0;
  2502. dbl_count = 0;
  2503. mul_count = 0;
  2504. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
  2505. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
  2506. for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
  2507. {
  2508. add_c_prev = add_count;
  2509. dbl_c_prev = dbl_count;
  2510. mul_c_prev = mul_count;
  2511. add_count = 0;
  2512. dbl_count = 0;
  2513. mul_count = 0;
  2514. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
  2515. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
  2516. if( add_count != add_c_prev ||
  2517. dbl_count != dbl_c_prev ||
  2518. mul_count != mul_c_prev )
  2519. {
  2520. if( verbose != 0 )
  2521. mbedtls_printf( "failed (%u)\n", (unsigned int) i );
  2522. ret = 1;
  2523. goto cleanup;
  2524. }
  2525. }
  2526. if( verbose != 0 )
  2527. mbedtls_printf( "passed\n" );
  2528. cleanup:
  2529. if( ret < 0 && verbose != 0 )
  2530. mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
  2531. mbedtls_ecp_group_free( &grp );
  2532. mbedtls_ecp_point_free( &R );
  2533. mbedtls_ecp_point_free( &P );
  2534. mbedtls_mpi_free( &m );
  2535. if( verbose != 0 )
  2536. mbedtls_printf( "\n" );
  2537. return( ret );
  2538. }
  2539. #endif /* MBEDTLS_SELF_TEST */
  2540. #endif /* !MBEDTLS_ECP_ALT */
  2541. #endif /* MBEDTLS_ECP_C */