ecp.c 63 KB

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