ecp.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999
  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 ) \
  898. do \
  899. { \
  900. MBEDTLS_MPI_CHK( ecp_modp( &(N), grp ) ); \
  901. INC_MUL_COUNT \
  902. } while( 0 )
  903. /*
  904. * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
  905. * N->s < 0 is a very fast test, which fails only if N is 0
  906. */
  907. #define MOD_SUB( N ) \
  908. while( (N).s < 0 && mbedtls_mpi_cmp_int( &(N), 0 ) != 0 ) \
  909. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &(N), &(N), &grp->P ) )
  910. /*
  911. * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
  912. * We known P, N and the result are positive, so sub_abs is correct, and
  913. * a bit faster.
  914. */
  915. #define MOD_ADD( N ) \
  916. while( mbedtls_mpi_cmp_mpi( &(N), &grp->P ) >= 0 ) \
  917. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &(N), &(N), &grp->P ) )
  918. #if defined(ECP_SHORTWEIERSTRASS)
  919. /*
  920. * For curves in short Weierstrass form, we do all the internal operations in
  921. * Jacobian coordinates.
  922. *
  923. * For multiplication, we'll use a comb method with coutermeasueres against
  924. * SPA, hence timing attacks.
  925. */
  926. /*
  927. * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
  928. * Cost: 1N := 1I + 3M + 1S
  929. */
  930. static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
  931. {
  932. int ret;
  933. mbedtls_mpi Zi, ZZi;
  934. if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
  935. return( 0 );
  936. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
  937. if( mbedtls_internal_ecp_grp_capable( grp ) )
  938. return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
  939. #endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
  940. mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
  941. /*
  942. * X = X / Z^2 mod p
  943. */
  944. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
  945. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
  946. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
  947. /*
  948. * Y = Y / Z^3 mod p
  949. */
  950. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
  951. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
  952. /*
  953. * Z = 1
  954. */
  955. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
  956. cleanup:
  957. mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
  958. return( ret );
  959. }
  960. /*
  961. * Normalize jacobian coordinates of an array of (pointers to) points,
  962. * using Montgomery's trick to perform only one inversion mod P.
  963. * (See for example Cohen's "A Course in Computational Algebraic Number
  964. * Theory", Algorithm 10.3.4.)
  965. *
  966. * Warning: fails (returning an error) if one of the points is zero!
  967. * This should never happen, see choice of w in ecp_mul_comb().
  968. *
  969. * Cost: 1N(t) := 1I + (6t - 3)M + 1S
  970. */
  971. static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
  972. mbedtls_ecp_point *T[], size_t T_size )
  973. {
  974. int ret;
  975. size_t i;
  976. mbedtls_mpi *c, u, Zi, ZZi;
  977. if( T_size < 2 )
  978. return( ecp_normalize_jac( grp, *T ) );
  979. #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
  980. if( mbedtls_internal_ecp_grp_capable( grp ) )
  981. return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
  982. #endif
  983. if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
  984. return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
  985. for( i = 0; i < T_size; i++ )
  986. mbedtls_mpi_init( &c[i] );
  987. mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
  988. /*
  989. * c[i] = Z_0 * ... * Z_i
  990. */
  991. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
  992. for( i = 1; i < T_size; i++ )
  993. {
  994. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
  995. MOD_MUL( c[i] );
  996. }
  997. /*
  998. * u = 1 / (Z_0 * ... * Z_n) mod P
  999. */
  1000. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
  1001. for( i = T_size - 1; ; i-- )
  1002. {
  1003. /*
  1004. * Zi = 1 / Z_i mod p
  1005. * u = 1 / (Z_0 * ... * Z_i) mod P
  1006. */
  1007. if( i == 0 ) {
  1008. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
  1009. }
  1010. else
  1011. {
  1012. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
  1013. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
  1014. }
  1015. /*
  1016. * proceed as in normalize()
  1017. */
  1018. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
  1019. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
  1020. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
  1021. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
  1022. /*
  1023. * Post-precessing: reclaim some memory by shrinking coordinates
  1024. * - not storing Z (always 1)
  1025. * - shrinking other coordinates, but still keeping the same number of
  1026. * limbs as P, as otherwise it will too likely be regrown too fast.
  1027. */
  1028. MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
  1029. MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
  1030. mbedtls_mpi_free( &T[i]->Z );
  1031. if( i == 0 )
  1032. break;
  1033. }
  1034. cleanup:
  1035. mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
  1036. for( i = 0; i < T_size; i++ )
  1037. mbedtls_mpi_free( &c[i] );
  1038. mbedtls_free( c );
  1039. return( ret );
  1040. }
  1041. /*
  1042. * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
  1043. * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
  1044. */
  1045. static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
  1046. mbedtls_ecp_point *Q,
  1047. unsigned char inv )
  1048. {
  1049. int ret;
  1050. unsigned char nonzero;
  1051. mbedtls_mpi mQY;
  1052. mbedtls_mpi_init( &mQY );
  1053. /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
  1054. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
  1055. nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
  1056. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
  1057. cleanup:
  1058. mbedtls_mpi_free( &mQY );
  1059. return( ret );
  1060. }
  1061. /*
  1062. * Point doubling R = 2 P, Jacobian coordinates
  1063. *
  1064. * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
  1065. *
  1066. * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
  1067. * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
  1068. *
  1069. * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
  1070. *
  1071. * Cost: 1D := 3M + 4S (A == 0)
  1072. * 4M + 4S (A == -3)
  1073. * 3M + 6S + 1a otherwise
  1074. */
  1075. static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1076. const mbedtls_ecp_point *P )
  1077. {
  1078. int ret;
  1079. mbedtls_mpi M, S, T, U;
  1080. #if defined(MBEDTLS_SELF_TEST)
  1081. dbl_count++;
  1082. #endif
  1083. #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
  1084. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1085. return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
  1086. #endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
  1087. mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U );
  1088. /* Special case for A = -3 */
  1089. if( grp->A.p == NULL )
  1090. {
  1091. /* M = 3(X + Z^2)(X - Z^2) */
  1092. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
  1093. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
  1094. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
  1095. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
  1096. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
  1097. }
  1098. else
  1099. {
  1100. /* M = 3.X^2 */
  1101. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
  1102. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
  1103. /* Optimize away for "koblitz" curves with A = 0 */
  1104. if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
  1105. {
  1106. /* M += A.Z^4 */
  1107. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
  1108. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
  1109. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
  1110. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
  1111. }
  1112. }
  1113. /* S = 4.X.Y^2 */
  1114. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
  1115. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
  1116. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
  1117. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
  1118. /* U = 8.Y^4 */
  1119. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
  1120. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
  1121. /* T = M^2 - 2.S */
  1122. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
  1123. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
  1124. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
  1125. /* S = M(S - T) - U */
  1126. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
  1127. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
  1128. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
  1129. /* U = 2.Y.Z */
  1130. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
  1131. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
  1132. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
  1133. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
  1134. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
  1135. cleanup:
  1136. mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U );
  1137. return( ret );
  1138. }
  1139. /*
  1140. * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
  1141. *
  1142. * The coordinates of Q must be normalized (= affine),
  1143. * but those of P don't need to. R is not normalized.
  1144. *
  1145. * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
  1146. * None of these cases can happen as intermediate step in ecp_mul_comb():
  1147. * - at each step, P, Q and R are multiples of the base point, the factor
  1148. * being less than its order, so none of them is zero;
  1149. * - Q is an odd multiple of the base point, P an even multiple,
  1150. * due to the choice of precomputed points in the modified comb method.
  1151. * So branches for these cases do not leak secret information.
  1152. *
  1153. * We accept Q->Z being unset (saving memory in tables) as meaning 1.
  1154. *
  1155. * Cost: 1A := 8M + 3S
  1156. */
  1157. static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1158. const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
  1159. {
  1160. int ret;
  1161. mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
  1162. #if defined(MBEDTLS_SELF_TEST)
  1163. add_count++;
  1164. #endif
  1165. #if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
  1166. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1167. return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
  1168. #endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
  1169. /*
  1170. * Trivial cases: P == 0 or Q == 0 (case 1)
  1171. */
  1172. if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
  1173. return( mbedtls_ecp_copy( R, Q ) );
  1174. if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
  1175. return( mbedtls_ecp_copy( R, P ) );
  1176. /*
  1177. * Make sure Q coordinates are normalized
  1178. */
  1179. if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
  1180. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1181. mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
  1182. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  1183. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
  1184. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
  1185. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
  1186. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
  1187. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
  1188. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
  1189. /* Special cases (2) and (3) */
  1190. if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
  1191. {
  1192. if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
  1193. {
  1194. ret = ecp_double_jac( grp, R, P );
  1195. goto cleanup;
  1196. }
  1197. else
  1198. {
  1199. ret = mbedtls_ecp_set_zero( R );
  1200. goto cleanup;
  1201. }
  1202. }
  1203. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
  1204. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
  1205. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
  1206. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
  1207. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
  1208. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
  1209. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
  1210. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
  1211. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
  1212. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
  1213. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
  1214. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
  1215. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
  1216. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
  1217. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
  1218. cleanup:
  1219. mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
  1220. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  1221. return( ret );
  1222. }
  1223. /*
  1224. * Randomize jacobian coordinates:
  1225. * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
  1226. * This is sort of the reverse operation of ecp_normalize_jac().
  1227. *
  1228. * This countermeasure was first suggested in [2].
  1229. */
  1230. static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  1231. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  1232. {
  1233. int ret;
  1234. mbedtls_mpi l, ll;
  1235. size_t p_size;
  1236. int count = 0;
  1237. #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
  1238. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1239. return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
  1240. #endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
  1241. p_size = ( grp->pbits + 7 ) / 8;
  1242. mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
  1243. /* Generate l such that 1 < l < p */
  1244. do
  1245. {
  1246. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
  1247. while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
  1248. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
  1249. if( count++ > 10 )
  1250. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  1251. }
  1252. while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
  1253. /* Z = l * Z */
  1254. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
  1255. /* X = l^2 * X */
  1256. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
  1257. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
  1258. /* Y = l^3 * Y */
  1259. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
  1260. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
  1261. cleanup:
  1262. mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
  1263. return( ret );
  1264. }
  1265. /*
  1266. * Check and define parameters used by the comb method (see below for details)
  1267. */
  1268. #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
  1269. #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
  1270. #endif
  1271. /* d = ceil( n / w ) */
  1272. #define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
  1273. /* number of precomputed points */
  1274. #define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  1275. /*
  1276. * Compute the representation of m that will be used with our comb method.
  1277. *
  1278. * The basic comb method is described in GECC 3.44 for example. We use a
  1279. * modified version that provides resistance to SPA by avoiding zero
  1280. * digits in the representation as in [3]. We modify the method further by
  1281. * requiring that all K_i be odd, which has the small cost that our
  1282. * representation uses one more K_i, due to carries, but saves on the size of
  1283. * the precomputed table.
  1284. *
  1285. * Summary of the comb method and its modifications:
  1286. *
  1287. * - The goal is to compute m*P for some w*d-bit integer m.
  1288. *
  1289. * - The basic comb method splits m into the w-bit integers
  1290. * x[0] .. x[d-1] where x[i] consists of the bits in m whose
  1291. * index has residue i modulo d, and computes m * P as
  1292. * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
  1293. * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
  1294. *
  1295. * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
  1296. * .. + 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]] ..,
  1297. * thereby successively converting it into a form where all summands
  1298. * are nonzero, at the cost of negative summands. This is the basic idea of [3].
  1299. *
  1300. * - More generally, even if x[i+1] != 0, we can first transform the sum as
  1301. * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
  1302. * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
  1303. * Performing and iterating this procedure for those x[i] that are even
  1304. * (keeping track of carry), we can transform the original sum into one of the form
  1305. * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
  1306. * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
  1307. * which is why we are only computing half of it in the first place in
  1308. * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
  1309. *
  1310. * - For the sake of compactness, only the seven low-order bits of x[i]
  1311. * are used to represent its absolute value (K_i in the paper), and the msb
  1312. * of x[i] encodes the sign (s_i in the paper): it is set if and only if
  1313. * if s_i == -1;
  1314. *
  1315. * Calling conventions:
  1316. * - x is an array of size d + 1
  1317. * - w is the size, ie number of teeth, of the comb, and must be between
  1318. * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
  1319. * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
  1320. * (the result will be incorrect if these assumptions are not satisfied)
  1321. */
  1322. static void ecp_comb_recode_core( unsigned char x[], size_t d,
  1323. unsigned char w, const mbedtls_mpi *m )
  1324. {
  1325. size_t i, j;
  1326. unsigned char c, cc, adjust;
  1327. memset( x, 0, d+1 );
  1328. /* First get the classical comb values (except for x_d = 0) */
  1329. for( i = 0; i < d; i++ )
  1330. for( j = 0; j < w; j++ )
  1331. x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
  1332. /* Now make sure x_1 .. x_d are odd */
  1333. c = 0;
  1334. for( i = 1; i <= d; i++ )
  1335. {
  1336. /* Add carry and update it */
  1337. cc = x[i] & c;
  1338. x[i] = x[i] ^ c;
  1339. c = cc;
  1340. /* Adjust if needed, avoiding branches */
  1341. adjust = 1 - ( x[i] & 0x01 );
  1342. c |= x[i] & ( x[i-1] * adjust );
  1343. x[i] = x[i] ^ ( x[i-1] * adjust );
  1344. x[i-1] |= adjust << 7;
  1345. }
  1346. }
  1347. /*
  1348. * Precompute points for the adapted comb method
  1349. *
  1350. * Assumption: T must be able to hold 2^{w - 1} elements.
  1351. *
  1352. * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
  1353. * sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P.
  1354. *
  1355. * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
  1356. *
  1357. * Note: Even comb values (those where P would be omitted from the
  1358. * sum defining T[i] above) are not needed in our adaption
  1359. * the comb method. See ecp_comb_recode_core().
  1360. *
  1361. * This function currently works in four steps:
  1362. * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
  1363. * (2) [norm_dbl] Normalization of coordinates of these T[i]
  1364. * (3) [add] Computation of all T[i]
  1365. * (4) [norm_add] Normalization of all T[i]
  1366. *
  1367. * Step 1 can be interrupted but not the others; together with the final
  1368. * coordinate normalization they are the largest steps done at once, depending
  1369. * on the window size. Here are operation counts for P-256:
  1370. *
  1371. * step (2) (3) (4)
  1372. * w = 5 142 165 208
  1373. * w = 4 136 77 160
  1374. * w = 3 130 33 136
  1375. * w = 2 124 11 124
  1376. *
  1377. * So if ECC operations are blocking for too long even with a low max_ops
  1378. * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
  1379. * to minimize maximum blocking time.
  1380. */
  1381. static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
  1382. mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
  1383. unsigned char w, size_t d,
  1384. mbedtls_ecp_restart_ctx *rs_ctx )
  1385. {
  1386. int ret;
  1387. unsigned char i;
  1388. size_t j = 0;
  1389. const unsigned char T_size = 1U << ( w - 1 );
  1390. mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
  1391. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1392. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1393. {
  1394. if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
  1395. goto dbl;
  1396. if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
  1397. goto norm_dbl;
  1398. if( rs_ctx->rsm->state == ecp_rsm_pre_add )
  1399. goto add;
  1400. if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
  1401. goto norm_add;
  1402. }
  1403. #else
  1404. (void) rs_ctx;
  1405. #endif
  1406. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1407. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1408. {
  1409. rs_ctx->rsm->state = ecp_rsm_pre_dbl;
  1410. /* initial state for the loop */
  1411. rs_ctx->rsm->i = 0;
  1412. }
  1413. dbl:
  1414. #endif
  1415. /*
  1416. * Set T[0] = P and
  1417. * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
  1418. */
  1419. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
  1420. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1421. if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
  1422. j = rs_ctx->rsm->i;
  1423. else
  1424. #endif
  1425. j = 0;
  1426. for( ; j < d * ( w - 1 ); j++ )
  1427. {
  1428. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
  1429. i = 1U << ( j / d );
  1430. cur = T + i;
  1431. if( j % d == 0 )
  1432. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
  1433. MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
  1434. }
  1435. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1436. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1437. rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
  1438. norm_dbl:
  1439. #endif
  1440. /*
  1441. * Normalize current elements in T. As T has holes,
  1442. * use an auxiliary array of pointers to elements in T.
  1443. */
  1444. j = 0;
  1445. for( i = 1; i < T_size; i <<= 1 )
  1446. TT[j++] = T + i;
  1447. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
  1448. MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
  1449. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1450. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1451. rs_ctx->rsm->state = ecp_rsm_pre_add;
  1452. add:
  1453. #endif
  1454. /*
  1455. * Compute the remaining ones using the minimal number of additions
  1456. * Be careful to update T[2^l] only after using it!
  1457. */
  1458. MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
  1459. for( i = 1; i < T_size; i <<= 1 )
  1460. {
  1461. j = i;
  1462. while( j-- )
  1463. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
  1464. }
  1465. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1466. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1467. rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
  1468. norm_add:
  1469. #endif
  1470. /*
  1471. * Normalize final elements in T. Even though there are no holes now, we
  1472. * still need the auxiliary array for homogeneity with the previous
  1473. * call. Also, skip T[0] which is already normalised, being a copy of P.
  1474. */
  1475. for( j = 0; j + 1 < T_size; j++ )
  1476. TT[j] = T + j + 1;
  1477. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
  1478. MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
  1479. cleanup:
  1480. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1481. if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
  1482. ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  1483. {
  1484. if( rs_ctx->rsm->state == ecp_rsm_pre_dbl )
  1485. rs_ctx->rsm->i = j;
  1486. }
  1487. #endif
  1488. return( ret );
  1489. }
  1490. /*
  1491. * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
  1492. *
  1493. * See ecp_comb_recode_core() for background
  1494. */
  1495. static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1496. const mbedtls_ecp_point T[], unsigned char T_size,
  1497. unsigned char i )
  1498. {
  1499. int ret;
  1500. unsigned char ii, j;
  1501. /* Ignore the "sign" bit and scale down */
  1502. ii = ( i & 0x7Fu ) >> 1;
  1503. /* Read the whole table to thwart cache-based timing attacks */
  1504. for( j = 0; j < T_size; j++ )
  1505. {
  1506. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
  1507. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
  1508. }
  1509. /* Safely invert result if i is "negative" */
  1510. MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
  1511. cleanup:
  1512. return( ret );
  1513. }
  1514. /*
  1515. * Core multiplication algorithm for the (modified) comb method.
  1516. * This part is actually common with the basic comb method (GECC 3.44)
  1517. *
  1518. * Cost: d A + d D + 1 R
  1519. */
  1520. static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1521. const mbedtls_ecp_point T[], unsigned char T_size,
  1522. const unsigned char x[], size_t d,
  1523. int (*f_rng)(void *, unsigned char *, size_t),
  1524. void *p_rng,
  1525. mbedtls_ecp_restart_ctx *rs_ctx )
  1526. {
  1527. int ret;
  1528. mbedtls_ecp_point Txi;
  1529. size_t i;
  1530. mbedtls_ecp_point_init( &Txi );
  1531. #if !defined(MBEDTLS_ECP_RESTARTABLE)
  1532. (void) rs_ctx;
  1533. #endif
  1534. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1535. if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
  1536. rs_ctx->rsm->state != ecp_rsm_comb_core )
  1537. {
  1538. rs_ctx->rsm->i = 0;
  1539. rs_ctx->rsm->state = ecp_rsm_comb_core;
  1540. }
  1541. /* new 'if' instead of nested for the sake of the 'else' branch */
  1542. if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
  1543. {
  1544. /* restore current index (R already pointing to rs_ctx->rsm->R) */
  1545. i = rs_ctx->rsm->i;
  1546. }
  1547. else
  1548. #endif
  1549. {
  1550. /* Start with a non-zero point and randomize its coordinates */
  1551. i = d;
  1552. MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
  1553. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
  1554. if( f_rng != 0 )
  1555. MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
  1556. }
  1557. while( i != 0 )
  1558. {
  1559. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
  1560. --i;
  1561. MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
  1562. MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
  1563. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
  1564. }
  1565. cleanup:
  1566. mbedtls_ecp_point_free( &Txi );
  1567. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1568. if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
  1569. ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  1570. {
  1571. rs_ctx->rsm->i = i;
  1572. /* no need to save R, already pointing to rs_ctx->rsm->R */
  1573. }
  1574. #endif
  1575. return( ret );
  1576. }
  1577. /*
  1578. * Recode the scalar to get constant-time comb multiplication
  1579. *
  1580. * As the actual scalar recoding needs an odd scalar as a starting point,
  1581. * this wrapper ensures that by replacing m by N - m if necessary, and
  1582. * informs the caller that the result of multiplication will be negated.
  1583. *
  1584. * This works because we only support large prime order for Short Weierstrass
  1585. * curves, so N is always odd hence either m or N - m is.
  1586. *
  1587. * See ecp_comb_recode_core() for background.
  1588. */
  1589. static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
  1590. const mbedtls_mpi *m,
  1591. unsigned char k[COMB_MAX_D + 1],
  1592. size_t d,
  1593. unsigned char w,
  1594. unsigned char *parity_trick )
  1595. {
  1596. int ret;
  1597. mbedtls_mpi M, mm;
  1598. mbedtls_mpi_init( &M );
  1599. mbedtls_mpi_init( &mm );
  1600. /* N is always odd (see above), just make extra sure */
  1601. if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
  1602. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  1603. /* do we need the parity trick? */
  1604. *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
  1605. /* execute parity fix in constant time */
  1606. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
  1607. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
  1608. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
  1609. /* actual scalar recoding */
  1610. ecp_comb_recode_core( k, d, w, &M );
  1611. cleanup:
  1612. mbedtls_mpi_free( &mm );
  1613. mbedtls_mpi_free( &M );
  1614. return( ret );
  1615. }
  1616. /*
  1617. * Perform comb multiplication (for short Weierstrass curves)
  1618. * once the auxiliary table has been pre-computed.
  1619. *
  1620. * Scalar recoding may use a parity trick that makes us compute -m * P,
  1621. * if that is the case we'll need to recover m * P at the end.
  1622. */
  1623. static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
  1624. mbedtls_ecp_point *R,
  1625. const mbedtls_mpi *m,
  1626. const mbedtls_ecp_point *T,
  1627. unsigned char T_size,
  1628. unsigned char w,
  1629. size_t d,
  1630. int (*f_rng)(void *, unsigned char *, size_t),
  1631. void *p_rng,
  1632. mbedtls_ecp_restart_ctx *rs_ctx )
  1633. {
  1634. int ret;
  1635. unsigned char parity_trick;
  1636. unsigned char k[COMB_MAX_D + 1];
  1637. mbedtls_ecp_point *RR = R;
  1638. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1639. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1640. {
  1641. RR = &rs_ctx->rsm->R;
  1642. if( rs_ctx->rsm->state == ecp_rsm_final_norm )
  1643. goto final_norm;
  1644. }
  1645. #endif
  1646. MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
  1647. &parity_trick ) );
  1648. MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
  1649. f_rng, p_rng, rs_ctx ) );
  1650. MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
  1651. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1652. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1653. rs_ctx->rsm->state = ecp_rsm_final_norm;
  1654. final_norm:
  1655. #endif
  1656. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
  1657. MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
  1658. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1659. if( rs_ctx != NULL && rs_ctx->rsm != NULL )
  1660. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
  1661. #endif
  1662. cleanup:
  1663. return( ret );
  1664. }
  1665. /*
  1666. * Pick window size based on curve size and whether we optimize for base point
  1667. */
  1668. static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
  1669. unsigned char p_eq_g )
  1670. {
  1671. unsigned char w;
  1672. /*
  1673. * Minimize the number of multiplications, that is minimize
  1674. * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
  1675. * (see costs of the various parts, with 1S = 1M)
  1676. */
  1677. w = grp->nbits >= 384 ? 5 : 4;
  1678. /*
  1679. * If P == G, pre-compute a bit more, since this may be re-used later.
  1680. * Just adding one avoids upping the cost of the first mul too much,
  1681. * and the memory cost too.
  1682. */
  1683. if( p_eq_g )
  1684. w++;
  1685. /*
  1686. * Make sure w is within bounds.
  1687. * (The last test is useful only for very small curves in the test suite.)
  1688. */
  1689. if( w > MBEDTLS_ECP_WINDOW_SIZE )
  1690. w = MBEDTLS_ECP_WINDOW_SIZE;
  1691. if( w >= grp->nbits )
  1692. w = 2;
  1693. return( w );
  1694. }
  1695. /*
  1696. * Multiplication using the comb method - for curves in short Weierstrass form
  1697. *
  1698. * This function is mainly responsible for administrative work:
  1699. * - managing the restart context if enabled
  1700. * - managing the table of precomputed points (passed between the below two
  1701. * functions): allocation, computation, ownership tranfer, freeing.
  1702. *
  1703. * It delegates the actual arithmetic work to:
  1704. * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
  1705. *
  1706. * See comments on ecp_comb_recode_core() regarding the computation strategy.
  1707. */
  1708. static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1709. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1710. int (*f_rng)(void *, unsigned char *, size_t),
  1711. void *p_rng,
  1712. mbedtls_ecp_restart_ctx *rs_ctx )
  1713. {
  1714. int ret;
  1715. unsigned char w, p_eq_g, i;
  1716. size_t d;
  1717. unsigned char T_size, T_ok;
  1718. mbedtls_ecp_point *T;
  1719. ECP_RS_ENTER( rsm );
  1720. /* Is P the base point ? */
  1721. #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
  1722. p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
  1723. mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
  1724. #else
  1725. p_eq_g = 0;
  1726. #endif
  1727. /* Pick window size and deduce related sizes */
  1728. w = ecp_pick_window_size( grp, p_eq_g );
  1729. T_size = 1U << ( w - 1 );
  1730. d = ( grp->nbits + w - 1 ) / w;
  1731. /* Pre-computed table: do we have it already for the base point? */
  1732. if( p_eq_g && grp->T != NULL )
  1733. {
  1734. /* second pointer to the same table, will be deleted on exit */
  1735. T = grp->T;
  1736. T_ok = 1;
  1737. }
  1738. else
  1739. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1740. /* Pre-computed table: do we have one in progress? complete? */
  1741. if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL )
  1742. {
  1743. /* transfer ownership of T from rsm to local function */
  1744. T = rs_ctx->rsm->T;
  1745. rs_ctx->rsm->T = NULL;
  1746. rs_ctx->rsm->T_size = 0;
  1747. /* This effectively jumps to the call to mul_comb_after_precomp() */
  1748. T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
  1749. }
  1750. else
  1751. #endif
  1752. /* Allocate table if we didn't have any */
  1753. {
  1754. T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
  1755. if( T == NULL )
  1756. {
  1757. ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
  1758. goto cleanup;
  1759. }
  1760. for( i = 0; i < T_size; i++ )
  1761. mbedtls_ecp_point_init( &T[i] );
  1762. T_ok = 0;
  1763. }
  1764. /* Compute table (or finish computing it) if not done already */
  1765. if( !T_ok )
  1766. {
  1767. MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
  1768. if( p_eq_g )
  1769. {
  1770. /* almost transfer ownership of T to the group, but keep a copy of
  1771. * the pointer to use for calling the next function more easily */
  1772. grp->T = T;
  1773. grp->T_size = T_size;
  1774. }
  1775. }
  1776. /* Actual comb multiplication using precomputed points */
  1777. MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
  1778. T, T_size, w, d,
  1779. f_rng, p_rng, rs_ctx ) );
  1780. cleanup:
  1781. /* does T belong to the group? */
  1782. if( T == grp->T )
  1783. T = NULL;
  1784. /* does T belong to the restart context? */
  1785. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1786. if( rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL )
  1787. {
  1788. /* transfer ownership of T from local function to rsm */
  1789. rs_ctx->rsm->T_size = T_size;
  1790. rs_ctx->rsm->T = T;
  1791. T = NULL;
  1792. }
  1793. #endif
  1794. /* did T belong to us? then let's destroy it! */
  1795. if( T != NULL )
  1796. {
  1797. for( i = 0; i < T_size; i++ )
  1798. mbedtls_ecp_point_free( &T[i] );
  1799. mbedtls_free( T );
  1800. }
  1801. /* don't free R while in progress in case R == P */
  1802. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1803. if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  1804. #endif
  1805. /* prevent caller from using invalid value */
  1806. if( ret != 0 )
  1807. mbedtls_ecp_point_free( R );
  1808. ECP_RS_LEAVE( rsm );
  1809. return( ret );
  1810. }
  1811. #endif /* ECP_SHORTWEIERSTRASS */
  1812. #if defined(ECP_MONTGOMERY)
  1813. /*
  1814. * For Montgomery curves, we do all the internal arithmetic in projective
  1815. * coordinates. Import/export of points uses only the x coordinates, which is
  1816. * internaly represented as X / Z.
  1817. *
  1818. * For scalar multiplication, we'll use a Montgomery ladder.
  1819. */
  1820. /*
  1821. * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
  1822. * Cost: 1M + 1I
  1823. */
  1824. static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
  1825. {
  1826. int ret;
  1827. #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
  1828. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1829. return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
  1830. #endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
  1831. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
  1832. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
  1833. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
  1834. cleanup:
  1835. return( ret );
  1836. }
  1837. /*
  1838. * Randomize projective x/z coordinates:
  1839. * (X, Z) -> (l X, l Z) for random l
  1840. * This is sort of the reverse operation of ecp_normalize_mxz().
  1841. *
  1842. * This countermeasure was first suggested in [2].
  1843. * Cost: 2M
  1844. */
  1845. static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
  1846. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  1847. {
  1848. int ret;
  1849. mbedtls_mpi l;
  1850. size_t p_size;
  1851. int count = 0;
  1852. #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
  1853. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1854. return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
  1855. #endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
  1856. p_size = ( grp->pbits + 7 ) / 8;
  1857. mbedtls_mpi_init( &l );
  1858. /* Generate l such that 1 < l < p */
  1859. do
  1860. {
  1861. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
  1862. while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
  1863. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
  1864. if( count++ > 10 )
  1865. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  1866. }
  1867. while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
  1868. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
  1869. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
  1870. cleanup:
  1871. mbedtls_mpi_free( &l );
  1872. return( ret );
  1873. }
  1874. /*
  1875. * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
  1876. * for Montgomery curves in x/z coordinates.
  1877. *
  1878. * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
  1879. * with
  1880. * d = X1
  1881. * P = (X2, Z2)
  1882. * Q = (X3, Z3)
  1883. * R = (X4, Z4)
  1884. * S = (X5, Z5)
  1885. * and eliminating temporary variables tO, ..., t4.
  1886. *
  1887. * Cost: 5M + 4S
  1888. */
  1889. static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
  1890. mbedtls_ecp_point *R, mbedtls_ecp_point *S,
  1891. const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
  1892. const mbedtls_mpi *d )
  1893. {
  1894. int ret;
  1895. mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
  1896. #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
  1897. if( mbedtls_internal_ecp_grp_capable( grp ) )
  1898. return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
  1899. #endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
  1900. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
  1901. mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
  1902. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
  1903. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
  1904. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
  1905. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
  1906. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
  1907. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
  1908. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
  1909. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
  1910. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
  1911. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
  1912. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
  1913. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
  1914. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
  1915. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
  1916. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
  1917. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
  1918. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
  1919. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
  1920. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
  1921. cleanup:
  1922. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
  1923. mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
  1924. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
  1925. return( ret );
  1926. }
  1927. /*
  1928. * Multiplication with Montgomery ladder in x/z coordinates,
  1929. * for curves in Montgomery form
  1930. */
  1931. static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1932. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1933. int (*f_rng)(void *, unsigned char *, size_t),
  1934. void *p_rng )
  1935. {
  1936. int ret;
  1937. size_t i;
  1938. unsigned char b;
  1939. mbedtls_ecp_point RP;
  1940. mbedtls_mpi PX;
  1941. mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
  1942. /* Save PX and read from P before writing to R, in case P == R */
  1943. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
  1944. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
  1945. /* Set R to zero in modified x/z coordinates */
  1946. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
  1947. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
  1948. mbedtls_mpi_free( &R->Y );
  1949. /* RP.X might be sligtly larger than P, so reduce it */
  1950. MOD_ADD( RP.X );
  1951. /* Randomize coordinates of the starting point */
  1952. if( f_rng != NULL )
  1953. MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
  1954. /* Loop invariant: R = result so far, RP = R + P */
  1955. i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
  1956. while( i-- > 0 )
  1957. {
  1958. b = mbedtls_mpi_get_bit( m, i );
  1959. /*
  1960. * if (b) R = 2R + P else R = 2R,
  1961. * which is:
  1962. * if (b) double_add( RP, R, RP, R )
  1963. * else double_add( R, RP, R, RP )
  1964. * but using safe conditional swaps to avoid leaks
  1965. */
  1966. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
  1967. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
  1968. MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
  1969. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
  1970. MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
  1971. }
  1972. MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
  1973. cleanup:
  1974. mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
  1975. return( ret );
  1976. }
  1977. #endif /* ECP_MONTGOMERY */
  1978. /*
  1979. * Restartable multiplication R = m * P
  1980. */
  1981. int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1982. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1983. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  1984. mbedtls_ecp_restart_ctx *rs_ctx )
  1985. {
  1986. int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  1987. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  1988. char is_grp_capable = 0;
  1989. #endif
  1990. ECP_VALIDATE_RET( grp != NULL );
  1991. ECP_VALIDATE_RET( R != NULL );
  1992. ECP_VALIDATE_RET( m != NULL );
  1993. ECP_VALIDATE_RET( P != NULL );
  1994. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1995. /* reset ops count for this call if top-level */
  1996. if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
  1997. rs_ctx->ops_done = 0;
  1998. #endif
  1999. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2000. if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
  2001. MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
  2002. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  2003. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2004. /* skip argument check when restarting */
  2005. if( rs_ctx == NULL || rs_ctx->rsm == NULL )
  2006. #endif
  2007. {
  2008. /* check_privkey is free */
  2009. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
  2010. /* Common sanity checks */
  2011. MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
  2012. MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
  2013. }
  2014. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  2015. #if defined(ECP_MONTGOMERY)
  2016. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  2017. MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
  2018. #endif
  2019. #if defined(ECP_SHORTWEIERSTRASS)
  2020. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  2021. MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
  2022. #endif
  2023. cleanup:
  2024. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2025. if( is_grp_capable )
  2026. mbedtls_internal_ecp_free( grp );
  2027. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  2028. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2029. if( rs_ctx != NULL )
  2030. rs_ctx->depth--;
  2031. #endif
  2032. return( ret );
  2033. }
  2034. /*
  2035. * Multiplication R = m * P
  2036. */
  2037. int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  2038. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  2039. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  2040. {
  2041. ECP_VALIDATE_RET( grp != NULL );
  2042. ECP_VALIDATE_RET( R != NULL );
  2043. ECP_VALIDATE_RET( m != NULL );
  2044. ECP_VALIDATE_RET( P != NULL );
  2045. return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
  2046. }
  2047. #if defined(ECP_SHORTWEIERSTRASS)
  2048. /*
  2049. * Check that an affine point is valid as a public key,
  2050. * short weierstrass curves (SEC1 3.2.3.1)
  2051. */
  2052. static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  2053. {
  2054. int ret;
  2055. mbedtls_mpi YY, RHS;
  2056. /* pt coordinates must be normalized for our checks */
  2057. if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
  2058. mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
  2059. mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
  2060. mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
  2061. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2062. mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
  2063. /*
  2064. * YY = Y^2
  2065. * RHS = X (X^2 + A) + B = X^3 + A X + B
  2066. */
  2067. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
  2068. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
  2069. /* Special case for A = -3 */
  2070. if( grp->A.p == NULL )
  2071. {
  2072. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
  2073. }
  2074. else
  2075. {
  2076. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
  2077. }
  2078. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
  2079. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
  2080. if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
  2081. ret = MBEDTLS_ERR_ECP_INVALID_KEY;
  2082. cleanup:
  2083. mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
  2084. return( ret );
  2085. }
  2086. #endif /* ECP_SHORTWEIERSTRASS */
  2087. /*
  2088. * R = m * P with shortcuts for m == 1 and m == -1
  2089. * NOT constant-time - ONLY for short Weierstrass!
  2090. */
  2091. static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
  2092. mbedtls_ecp_point *R,
  2093. const mbedtls_mpi *m,
  2094. const mbedtls_ecp_point *P,
  2095. mbedtls_ecp_restart_ctx *rs_ctx )
  2096. {
  2097. int ret;
  2098. if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
  2099. {
  2100. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
  2101. }
  2102. else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
  2103. {
  2104. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
  2105. if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
  2106. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
  2107. }
  2108. else
  2109. {
  2110. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
  2111. NULL, NULL, rs_ctx ) );
  2112. }
  2113. cleanup:
  2114. return( ret );
  2115. }
  2116. /*
  2117. * Restartable linear combination
  2118. * NOT constant-time
  2119. */
  2120. int mbedtls_ecp_muladd_restartable(
  2121. mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  2122. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  2123. const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
  2124. mbedtls_ecp_restart_ctx *rs_ctx )
  2125. {
  2126. int ret;
  2127. mbedtls_ecp_point mP;
  2128. mbedtls_ecp_point *pmP = &mP;
  2129. mbedtls_ecp_point *pR = R;
  2130. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2131. char is_grp_capable = 0;
  2132. #endif
  2133. ECP_VALIDATE_RET( grp != NULL );
  2134. ECP_VALIDATE_RET( R != NULL );
  2135. ECP_VALIDATE_RET( m != NULL );
  2136. ECP_VALIDATE_RET( P != NULL );
  2137. ECP_VALIDATE_RET( n != NULL );
  2138. ECP_VALIDATE_RET( Q != NULL );
  2139. if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
  2140. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  2141. mbedtls_ecp_point_init( &mP );
  2142. ECP_RS_ENTER( ma );
  2143. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2144. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2145. {
  2146. /* redirect intermediate results to restart context */
  2147. pmP = &rs_ctx->ma->mP;
  2148. pR = &rs_ctx->ma->R;
  2149. /* jump to next operation */
  2150. if( rs_ctx->ma->state == ecp_rsma_mul2 )
  2151. goto mul2;
  2152. if( rs_ctx->ma->state == ecp_rsma_add )
  2153. goto add;
  2154. if( rs_ctx->ma->state == ecp_rsma_norm )
  2155. goto norm;
  2156. }
  2157. #endif /* MBEDTLS_ECP_RESTARTABLE */
  2158. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
  2159. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2160. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2161. rs_ctx->ma->state = ecp_rsma_mul2;
  2162. mul2:
  2163. #endif
  2164. MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
  2165. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2166. if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
  2167. MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
  2168. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  2169. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2170. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2171. rs_ctx->ma->state = ecp_rsma_add;
  2172. add:
  2173. #endif
  2174. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
  2175. MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
  2176. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2177. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2178. rs_ctx->ma->state = ecp_rsma_norm;
  2179. norm:
  2180. #endif
  2181. MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
  2182. MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
  2183. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2184. if( rs_ctx != NULL && rs_ctx->ma != NULL )
  2185. MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
  2186. #endif
  2187. cleanup:
  2188. #if defined(MBEDTLS_ECP_INTERNAL_ALT)
  2189. if( is_grp_capable )
  2190. mbedtls_internal_ecp_free( grp );
  2191. #endif /* MBEDTLS_ECP_INTERNAL_ALT */
  2192. mbedtls_ecp_point_free( &mP );
  2193. ECP_RS_LEAVE( ma );
  2194. return( ret );
  2195. }
  2196. /*
  2197. * Linear combination
  2198. * NOT constant-time
  2199. */
  2200. int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  2201. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  2202. const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
  2203. {
  2204. ECP_VALIDATE_RET( grp != NULL );
  2205. ECP_VALIDATE_RET( R != NULL );
  2206. ECP_VALIDATE_RET( m != NULL );
  2207. ECP_VALIDATE_RET( P != NULL );
  2208. ECP_VALIDATE_RET( n != NULL );
  2209. ECP_VALIDATE_RET( Q != NULL );
  2210. return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
  2211. }
  2212. #if defined(ECP_MONTGOMERY)
  2213. /*
  2214. * Check validity of a public key for Montgomery curves with x-only schemes
  2215. */
  2216. static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
  2217. {
  2218. /* [Curve25519 p. 5] Just check X is the correct number of bytes */
  2219. /* Allow any public value, if it's too big then we'll just reduce it mod p
  2220. * (RFC 7748 sec. 5 para. 3). */
  2221. if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
  2222. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2223. return( 0 );
  2224. }
  2225. #endif /* ECP_MONTGOMERY */
  2226. /*
  2227. * Check that a point is valid as a public key
  2228. */
  2229. int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
  2230. const mbedtls_ecp_point *pt )
  2231. {
  2232. ECP_VALIDATE_RET( grp != NULL );
  2233. ECP_VALIDATE_RET( pt != NULL );
  2234. /* Must use affine coordinates */
  2235. if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
  2236. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2237. #if defined(ECP_MONTGOMERY)
  2238. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  2239. return( ecp_check_pubkey_mx( grp, pt ) );
  2240. #endif
  2241. #if defined(ECP_SHORTWEIERSTRASS)
  2242. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  2243. return( ecp_check_pubkey_sw( grp, pt ) );
  2244. #endif
  2245. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  2246. }
  2247. /*
  2248. * Check that an mbedtls_mpi is valid as a private key
  2249. */
  2250. int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
  2251. const mbedtls_mpi *d )
  2252. {
  2253. ECP_VALIDATE_RET( grp != NULL );
  2254. ECP_VALIDATE_RET( d != NULL );
  2255. #if defined(ECP_MONTGOMERY)
  2256. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  2257. {
  2258. /* see RFC 7748 sec. 5 para. 5 */
  2259. if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
  2260. mbedtls_mpi_get_bit( d, 1 ) != 0 ||
  2261. mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
  2262. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2263. /* see [Curve25519] page 5 */
  2264. if( grp->nbits == 254 && mbedtls_mpi_get_bit( d, 2 ) != 0 )
  2265. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2266. return( 0 );
  2267. }
  2268. #endif /* ECP_MONTGOMERY */
  2269. #if defined(ECP_SHORTWEIERSTRASS)
  2270. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  2271. {
  2272. /* see SEC1 3.2 */
  2273. if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
  2274. mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
  2275. return( MBEDTLS_ERR_ECP_INVALID_KEY );
  2276. else
  2277. return( 0 );
  2278. }
  2279. #endif /* ECP_SHORTWEIERSTRASS */
  2280. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  2281. }
  2282. /*
  2283. * Generate a private key
  2284. */
  2285. int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
  2286. mbedtls_mpi *d,
  2287. int (*f_rng)(void *, unsigned char *, size_t),
  2288. void *p_rng )
  2289. {
  2290. int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  2291. size_t n_size;
  2292. ECP_VALIDATE_RET( grp != NULL );
  2293. ECP_VALIDATE_RET( d != NULL );
  2294. ECP_VALIDATE_RET( f_rng != NULL );
  2295. n_size = ( grp->nbits + 7 ) / 8;
  2296. #if defined(ECP_MONTGOMERY)
  2297. if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
  2298. {
  2299. /* [M225] page 5 */
  2300. size_t b;
  2301. do {
  2302. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
  2303. } while( mbedtls_mpi_bitlen( d ) == 0);
  2304. /* Make sure the most significant bit is nbits */
  2305. b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
  2306. if( b > grp->nbits )
  2307. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
  2308. else
  2309. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
  2310. /* Make sure the last two bits are unset for Curve448, three bits for
  2311. Curve25519 */
  2312. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
  2313. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
  2314. if( grp->nbits == 254 )
  2315. {
  2316. MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
  2317. }
  2318. }
  2319. #endif /* ECP_MONTGOMERY */
  2320. #if defined(ECP_SHORTWEIERSTRASS)
  2321. if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
  2322. {
  2323. /* SEC1 3.2.1: Generate d such that 1 <= n < N */
  2324. int count = 0;
  2325. unsigned cmp = 0;
  2326. /*
  2327. * Match the procedure given in RFC 6979 (deterministic ECDSA):
  2328. * - use the same byte ordering;
  2329. * - keep the leftmost nbits bits of the generated octet string;
  2330. * - try until result is in the desired range.
  2331. * This also avoids any biais, which is especially important for ECDSA.
  2332. */
  2333. do
  2334. {
  2335. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
  2336. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
  2337. /*
  2338. * Each try has at worst a probability 1/2 of failing (the msb has
  2339. * a probability 1/2 of being 0, and then the result will be < N),
  2340. * so after 30 tries failure probability is a most 2**(-30).
  2341. *
  2342. * For most curves, 1 try is enough with overwhelming probability,
  2343. * since N starts with a lot of 1s in binary, but some curves
  2344. * such as secp224k1 are actually very close to the worst case.
  2345. */
  2346. if( ++count > 30 )
  2347. return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
  2348. ret = mbedtls_mpi_lt_mpi_ct( d, &grp->N, &cmp );
  2349. if( ret != 0 )
  2350. {
  2351. goto cleanup;
  2352. }
  2353. }
  2354. while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
  2355. }
  2356. #endif /* ECP_SHORTWEIERSTRASS */
  2357. cleanup:
  2358. return( ret );
  2359. }
  2360. /*
  2361. * Generate a keypair with configurable base point
  2362. */
  2363. int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  2364. const mbedtls_ecp_point *G,
  2365. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  2366. int (*f_rng)(void *, unsigned char *, size_t),
  2367. void *p_rng )
  2368. {
  2369. int ret;
  2370. ECP_VALIDATE_RET( grp != NULL );
  2371. ECP_VALIDATE_RET( d != NULL );
  2372. ECP_VALIDATE_RET( G != NULL );
  2373. ECP_VALIDATE_RET( Q != NULL );
  2374. ECP_VALIDATE_RET( f_rng != NULL );
  2375. MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
  2376. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
  2377. cleanup:
  2378. return( ret );
  2379. }
  2380. /*
  2381. * Generate key pair, wrapper for conventional base point
  2382. */
  2383. int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
  2384. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  2385. int (*f_rng)(void *, unsigned char *, size_t),
  2386. void *p_rng )
  2387. {
  2388. ECP_VALIDATE_RET( grp != NULL );
  2389. ECP_VALIDATE_RET( d != NULL );
  2390. ECP_VALIDATE_RET( Q != NULL );
  2391. ECP_VALIDATE_RET( f_rng != NULL );
  2392. return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
  2393. }
  2394. /*
  2395. * Generate a keypair, prettier wrapper
  2396. */
  2397. int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  2398. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  2399. {
  2400. int ret;
  2401. ECP_VALIDATE_RET( key != NULL );
  2402. ECP_VALIDATE_RET( f_rng != NULL );
  2403. if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
  2404. return( ret );
  2405. return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
  2406. }
  2407. /*
  2408. * Check a public-private key pair
  2409. */
  2410. int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
  2411. {
  2412. int ret;
  2413. mbedtls_ecp_point Q;
  2414. mbedtls_ecp_group grp;
  2415. ECP_VALIDATE_RET( pub != NULL );
  2416. ECP_VALIDATE_RET( prv != NULL );
  2417. if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
  2418. pub->grp.id != prv->grp.id ||
  2419. mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
  2420. mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
  2421. mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
  2422. {
  2423. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  2424. }
  2425. mbedtls_ecp_point_init( &Q );
  2426. mbedtls_ecp_group_init( &grp );
  2427. /* mbedtls_ecp_mul() needs a non-const group... */
  2428. mbedtls_ecp_group_copy( &grp, &prv->grp );
  2429. /* Also checks d is valid */
  2430. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
  2431. if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
  2432. mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
  2433. mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
  2434. {
  2435. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  2436. goto cleanup;
  2437. }
  2438. cleanup:
  2439. mbedtls_ecp_point_free( &Q );
  2440. mbedtls_ecp_group_free( &grp );
  2441. return( ret );
  2442. }
  2443. #if defined(MBEDTLS_SELF_TEST)
  2444. /*
  2445. * Checkup routine
  2446. */
  2447. int mbedtls_ecp_self_test( int verbose )
  2448. {
  2449. int ret;
  2450. size_t i;
  2451. mbedtls_ecp_group grp;
  2452. mbedtls_ecp_point R, P;
  2453. mbedtls_mpi m;
  2454. unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
  2455. /* exponents especially adapted for secp192r1 */
  2456. const char *exponents[] =
  2457. {
  2458. "000000000000000000000000000000000000000000000001", /* one */
  2459. "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
  2460. "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
  2461. "400000000000000000000000000000000000000000000000", /* one and zeros */
  2462. "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
  2463. "555555555555555555555555555555555555555555555555", /* 101010... */
  2464. };
  2465. mbedtls_ecp_group_init( &grp );
  2466. mbedtls_ecp_point_init( &R );
  2467. mbedtls_ecp_point_init( &P );
  2468. mbedtls_mpi_init( &m );
  2469. /* Use secp192r1 if available, or any available curve */
  2470. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  2471. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
  2472. #else
  2473. MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
  2474. #endif
  2475. if( verbose != 0 )
  2476. mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
  2477. /* Do a dummy multiplication first to trigger precomputation */
  2478. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
  2479. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
  2480. add_count = 0;
  2481. dbl_count = 0;
  2482. mul_count = 0;
  2483. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
  2484. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
  2485. for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
  2486. {
  2487. add_c_prev = add_count;
  2488. dbl_c_prev = dbl_count;
  2489. mul_c_prev = mul_count;
  2490. add_count = 0;
  2491. dbl_count = 0;
  2492. mul_count = 0;
  2493. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
  2494. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
  2495. if( add_count != add_c_prev ||
  2496. dbl_count != dbl_c_prev ||
  2497. mul_count != mul_c_prev )
  2498. {
  2499. if( verbose != 0 )
  2500. mbedtls_printf( "failed (%u)\n", (unsigned int) i );
  2501. ret = 1;
  2502. goto cleanup;
  2503. }
  2504. }
  2505. if( verbose != 0 )
  2506. mbedtls_printf( "passed\n" );
  2507. if( verbose != 0 )
  2508. mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
  2509. /* We computed P = 2G last time, use it */
  2510. add_count = 0;
  2511. dbl_count = 0;
  2512. mul_count = 0;
  2513. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
  2514. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
  2515. for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
  2516. {
  2517. add_c_prev = add_count;
  2518. dbl_c_prev = dbl_count;
  2519. mul_c_prev = mul_count;
  2520. add_count = 0;
  2521. dbl_count = 0;
  2522. mul_count = 0;
  2523. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
  2524. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
  2525. if( add_count != add_c_prev ||
  2526. dbl_count != dbl_c_prev ||
  2527. mul_count != mul_c_prev )
  2528. {
  2529. if( verbose != 0 )
  2530. mbedtls_printf( "failed (%u)\n", (unsigned int) i );
  2531. ret = 1;
  2532. goto cleanup;
  2533. }
  2534. }
  2535. if( verbose != 0 )
  2536. mbedtls_printf( "passed\n" );
  2537. cleanup:
  2538. if( ret < 0 && verbose != 0 )
  2539. mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
  2540. mbedtls_ecp_group_free( &grp );
  2541. mbedtls_ecp_point_free( &R );
  2542. mbedtls_ecp_point_free( &P );
  2543. mbedtls_mpi_free( &m );
  2544. if( verbose != 0 )
  2545. mbedtls_printf( "\n" );
  2546. return( ret );
  2547. }
  2548. #endif /* MBEDTLS_SELF_TEST */
  2549. #endif /* !MBEDTLS_ECP_ALT */
  2550. #endif /* MBEDTLS_ECP_C */