ecp.c 66 KB

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