ecp.c 66 KB

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