bignum.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957
  1. /*
  2. * Multi-precision integer library
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. /*
  47. * The following sources were referenced in the design of this Multi-precision
  48. * Integer library:
  49. *
  50. * [1] Handbook of Applied Cryptography - 1997
  51. * Menezes, van Oorschot and Vanstone
  52. *
  53. * [2] Multi-Precision Math
  54. * Tom St Denis
  55. * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
  56. *
  57. * [3] GNU Multi-Precision Arithmetic Library
  58. * https://gmplib.org/manual/index.html
  59. *
  60. */
  61. #if !defined(MBEDTLS_CONFIG_FILE)
  62. #include "mbedtls/config.h"
  63. #else
  64. #include MBEDTLS_CONFIG_FILE
  65. #endif
  66. #if defined(MBEDTLS_BIGNUM_C)
  67. #include "mbedtls/bignum.h"
  68. #include "mbedtls/bn_mul.h"
  69. #include "mbedtls/platform_util.h"
  70. #include <string.h>
  71. #if defined(MBEDTLS_PLATFORM_C)
  72. #include "mbedtls/platform.h"
  73. #else
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #define mbedtls_printf printf
  77. #define mbedtls_calloc calloc
  78. #define mbedtls_free free
  79. #endif
  80. #define MPI_VALIDATE_RET( cond ) \
  81. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
  82. #define MPI_VALIDATE( cond ) \
  83. MBEDTLS_INTERNAL_VALIDATE( cond )
  84. #define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
  85. #define biL (ciL << 3) /* bits in limb */
  86. #define biH (ciL << 2) /* half limb size */
  87. #define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
  88. /*
  89. * Convert between bits/chars and number of limbs
  90. * Divide first in order to avoid potential overflows
  91. */
  92. #define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
  93. #define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
  94. /* Implementation that should never be optimized out by the compiler */
  95. static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n )
  96. {
  97. mbedtls_platform_zeroize( v, ciL * n );
  98. }
  99. /*
  100. * Initialize one MPI
  101. */
  102. void mbedtls_mpi_init( mbedtls_mpi *X )
  103. {
  104. MPI_VALIDATE( X != NULL );
  105. X->s = 1;
  106. X->n = 0;
  107. X->p = NULL;
  108. }
  109. /*
  110. * Unallocate one MPI
  111. */
  112. void mbedtls_mpi_free( mbedtls_mpi *X )
  113. {
  114. if( X == NULL )
  115. return;
  116. if( X->p != NULL )
  117. {
  118. mbedtls_mpi_zeroize( X->p, X->n );
  119. mbedtls_free( X->p );
  120. }
  121. X->s = 1;
  122. X->n = 0;
  123. X->p = NULL;
  124. }
  125. /*
  126. * Enlarge to the specified number of limbs
  127. */
  128. int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
  129. {
  130. mbedtls_mpi_uint *p;
  131. MPI_VALIDATE_RET( X != NULL );
  132. if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
  133. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  134. if( X->n < nblimbs )
  135. {
  136. if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
  137. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  138. if( X->p != NULL )
  139. {
  140. memcpy( p, X->p, X->n * ciL );
  141. mbedtls_mpi_zeroize( X->p, X->n );
  142. mbedtls_free( X->p );
  143. }
  144. X->n = nblimbs;
  145. X->p = p;
  146. }
  147. return( 0 );
  148. }
  149. /*
  150. * Resize down as much as possible,
  151. * while keeping at least the specified number of limbs
  152. */
  153. int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
  154. {
  155. mbedtls_mpi_uint *p;
  156. size_t i;
  157. MPI_VALIDATE_RET( X != NULL );
  158. if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
  159. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  160. /* Actually resize up if there are currently fewer than nblimbs limbs. */
  161. if( X->n <= nblimbs )
  162. return( mbedtls_mpi_grow( X, nblimbs ) );
  163. /* After this point, then X->n > nblimbs and in particular X->n > 0. */
  164. for( i = X->n - 1; i > 0; i-- )
  165. if( X->p[i] != 0 )
  166. break;
  167. i++;
  168. if( i < nblimbs )
  169. i = nblimbs;
  170. if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
  171. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  172. if( X->p != NULL )
  173. {
  174. memcpy( p, X->p, i * ciL );
  175. mbedtls_mpi_zeroize( X->p, X->n );
  176. mbedtls_free( X->p );
  177. }
  178. X->n = i;
  179. X->p = p;
  180. return( 0 );
  181. }
  182. /*
  183. * Copy the contents of Y into X
  184. */
  185. int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
  186. {
  187. int ret = 0;
  188. size_t i;
  189. MPI_VALIDATE_RET( X != NULL );
  190. MPI_VALIDATE_RET( Y != NULL );
  191. if( X == Y )
  192. return( 0 );
  193. if( Y->n == 0 )
  194. {
  195. mbedtls_mpi_free( X );
  196. return( 0 );
  197. }
  198. for( i = Y->n - 1; i > 0; i-- )
  199. if( Y->p[i] != 0 )
  200. break;
  201. i++;
  202. X->s = Y->s;
  203. if( X->n < i )
  204. {
  205. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
  206. }
  207. else
  208. {
  209. memset( X->p + i, 0, ( X->n - i ) * ciL );
  210. }
  211. memcpy( X->p, Y->p, i * ciL );
  212. cleanup:
  213. return( ret );
  214. }
  215. /*
  216. * Swap the contents of X and Y
  217. */
  218. void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
  219. {
  220. mbedtls_mpi T;
  221. MPI_VALIDATE( X != NULL );
  222. MPI_VALIDATE( Y != NULL );
  223. memcpy( &T, X, sizeof( mbedtls_mpi ) );
  224. memcpy( X, Y, sizeof( mbedtls_mpi ) );
  225. memcpy( Y, &T, sizeof( mbedtls_mpi ) );
  226. }
  227. /*
  228. * Conditionally assign dest = src, without leaking information
  229. * about whether the assignment was made or not.
  230. * dest and src must be arrays of limbs of size n.
  231. * assign must be 0 or 1.
  232. */
  233. static void mpi_safe_cond_assign( size_t n,
  234. mbedtls_mpi_uint *dest,
  235. const mbedtls_mpi_uint *src,
  236. unsigned char assign )
  237. {
  238. size_t i;
  239. for( i = 0; i < n; i++ )
  240. dest[i] = dest[i] * ( 1 - assign ) + src[i] * assign;
  241. }
  242. /*
  243. * Conditionally assign X = Y, without leaking information
  244. * about whether the assignment was made or not.
  245. * (Leaking information about the respective sizes of X and Y is ok however.)
  246. */
  247. int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
  248. {
  249. int ret = 0;
  250. size_t i;
  251. MPI_VALIDATE_RET( X != NULL );
  252. MPI_VALIDATE_RET( Y != NULL );
  253. /* make sure assign is 0 or 1 in a time-constant manner */
  254. assign = (assign | (unsigned char)-assign) >> 7;
  255. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
  256. X->s = X->s * ( 1 - assign ) + Y->s * assign;
  257. mpi_safe_cond_assign( Y->n, X->p, Y->p, assign );
  258. for( i = Y->n; i < X->n; i++ )
  259. X->p[i] *= ( 1 - assign );
  260. cleanup:
  261. return( ret );
  262. }
  263. /*
  264. * Conditionally swap X and Y, without leaking information
  265. * about whether the swap was made or not.
  266. * Here it is not ok to simply swap the pointers, which whould lead to
  267. * different memory access patterns when X and Y are used afterwards.
  268. */
  269. int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
  270. {
  271. int ret, s;
  272. size_t i;
  273. mbedtls_mpi_uint tmp;
  274. MPI_VALIDATE_RET( X != NULL );
  275. MPI_VALIDATE_RET( Y != NULL );
  276. if( X == Y )
  277. return( 0 );
  278. /* make sure swap is 0 or 1 in a time-constant manner */
  279. swap = (swap | (unsigned char)-swap) >> 7;
  280. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
  281. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
  282. s = X->s;
  283. X->s = X->s * ( 1 - swap ) + Y->s * swap;
  284. Y->s = Y->s * ( 1 - swap ) + s * swap;
  285. for( i = 0; i < X->n; i++ )
  286. {
  287. tmp = X->p[i];
  288. X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
  289. Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
  290. }
  291. cleanup:
  292. return( ret );
  293. }
  294. /*
  295. * Set value from integer
  296. */
  297. int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
  298. {
  299. int ret;
  300. MPI_VALIDATE_RET( X != NULL );
  301. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
  302. memset( X->p, 0, X->n * ciL );
  303. X->p[0] = ( z < 0 ) ? -z : z;
  304. X->s = ( z < 0 ) ? -1 : 1;
  305. cleanup:
  306. return( ret );
  307. }
  308. /*
  309. * Get a specific bit
  310. */
  311. int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
  312. {
  313. MPI_VALIDATE_RET( X != NULL );
  314. if( X->n * biL <= pos )
  315. return( 0 );
  316. return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
  317. }
  318. /* Get a specific byte, without range checks. */
  319. #define GET_BYTE( X, i ) \
  320. ( ( ( X )->p[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
  321. /*
  322. * Set a bit to a specific value of 0 or 1
  323. */
  324. int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
  325. {
  326. int ret = 0;
  327. size_t off = pos / biL;
  328. size_t idx = pos % biL;
  329. MPI_VALIDATE_RET( X != NULL );
  330. if( val != 0 && val != 1 )
  331. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  332. if( X->n * biL <= pos )
  333. {
  334. if( val == 0 )
  335. return( 0 );
  336. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
  337. }
  338. X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
  339. X->p[off] |= (mbedtls_mpi_uint) val << idx;
  340. cleanup:
  341. return( ret );
  342. }
  343. /*
  344. * Return the number of less significant zero-bits
  345. */
  346. size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
  347. {
  348. size_t i, j, count = 0;
  349. MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 );
  350. for( i = 0; i < X->n; i++ )
  351. for( j = 0; j < biL; j++, count++ )
  352. if( ( ( X->p[i] >> j ) & 1 ) != 0 )
  353. return( count );
  354. return( 0 );
  355. }
  356. /*
  357. * Count leading zero bits in a given integer
  358. */
  359. static size_t mbedtls_clz( const mbedtls_mpi_uint x )
  360. {
  361. size_t j;
  362. mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
  363. for( j = 0; j < biL; j++ )
  364. {
  365. if( x & mask ) break;
  366. mask >>= 1;
  367. }
  368. return j;
  369. }
  370. /*
  371. * Return the number of bits
  372. */
  373. size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
  374. {
  375. size_t i, j;
  376. if( X->n == 0 )
  377. return( 0 );
  378. for( i = X->n - 1; i > 0; i-- )
  379. if( X->p[i] != 0 )
  380. break;
  381. j = biL - mbedtls_clz( X->p[i] );
  382. return( ( i * biL ) + j );
  383. }
  384. /*
  385. * Return the total size in bytes
  386. */
  387. size_t mbedtls_mpi_size( const mbedtls_mpi *X )
  388. {
  389. return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
  390. }
  391. /*
  392. * Convert an ASCII character to digit value
  393. */
  394. static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
  395. {
  396. *d = 255;
  397. if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
  398. if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
  399. if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
  400. if( *d >= (mbedtls_mpi_uint) radix )
  401. return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
  402. return( 0 );
  403. }
  404. /*
  405. * Import from an ASCII string
  406. */
  407. int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
  408. {
  409. int ret;
  410. size_t i, j, slen, n;
  411. int sign = 1;
  412. mbedtls_mpi_uint d;
  413. mbedtls_mpi T;
  414. MPI_VALIDATE_RET( X != NULL );
  415. MPI_VALIDATE_RET( s != NULL );
  416. if( radix < 2 || radix > 16 )
  417. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  418. mbedtls_mpi_init( &T );
  419. if( s[0] == '-' )
  420. {
  421. ++s;
  422. sign = -1;
  423. }
  424. slen = strlen( s );
  425. if( radix == 16 )
  426. {
  427. if( slen > MPI_SIZE_T_MAX >> 2 )
  428. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  429. n = BITS_TO_LIMBS( slen << 2 );
  430. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
  431. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  432. for( i = slen, j = 0; i > 0; i--, j++ )
  433. {
  434. MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
  435. X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
  436. }
  437. }
  438. else
  439. {
  440. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  441. for( i = 0; i < slen; i++ )
  442. {
  443. MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
  444. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
  445. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
  446. }
  447. }
  448. if( sign < 0 && mbedtls_mpi_bitlen( X ) != 0 )
  449. X->s = -1;
  450. cleanup:
  451. mbedtls_mpi_free( &T );
  452. return( ret );
  453. }
  454. /*
  455. * Helper to write the digits high-order first.
  456. */
  457. static int mpi_write_hlp( mbedtls_mpi *X, int radix,
  458. char **p, const size_t buflen )
  459. {
  460. int ret;
  461. mbedtls_mpi_uint r;
  462. size_t length = 0;
  463. char *p_end = *p + buflen;
  464. do
  465. {
  466. if( length >= buflen )
  467. {
  468. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  469. }
  470. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
  471. MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
  472. /*
  473. * Write the residue in the current position, as an ASCII character.
  474. */
  475. if( r < 0xA )
  476. *(--p_end) = (char)( '0' + r );
  477. else
  478. *(--p_end) = (char)( 'A' + ( r - 0xA ) );
  479. length++;
  480. } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
  481. memmove( *p, p_end, length );
  482. *p += length;
  483. cleanup:
  484. return( ret );
  485. }
  486. /*
  487. * Export into an ASCII string
  488. */
  489. int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
  490. char *buf, size_t buflen, size_t *olen )
  491. {
  492. int ret = 0;
  493. size_t n;
  494. char *p;
  495. mbedtls_mpi T;
  496. MPI_VALIDATE_RET( X != NULL );
  497. MPI_VALIDATE_RET( olen != NULL );
  498. MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
  499. if( radix < 2 || radix > 16 )
  500. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  501. n = mbedtls_mpi_bitlen( X ); /* Number of bits necessary to present `n`. */
  502. if( radix >= 4 ) n >>= 1; /* Number of 4-adic digits necessary to present
  503. * `n`. If radix > 4, this might be a strict
  504. * overapproximation of the number of
  505. * radix-adic digits needed to present `n`. */
  506. if( radix >= 16 ) n >>= 1; /* Number of hexadecimal digits necessary to
  507. * present `n`. */
  508. n += 1; /* Terminating null byte */
  509. n += 1; /* Compensate for the divisions above, which round down `n`
  510. * in case it's not even. */
  511. n += 1; /* Potential '-'-sign. */
  512. n += ( n & 1 ); /* Make n even to have enough space for hexadecimal writing,
  513. * which always uses an even number of hex-digits. */
  514. if( buflen < n )
  515. {
  516. *olen = n;
  517. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  518. }
  519. p = buf;
  520. mbedtls_mpi_init( &T );
  521. if( X->s == -1 )
  522. {
  523. *p++ = '-';
  524. buflen--;
  525. }
  526. if( radix == 16 )
  527. {
  528. int c;
  529. size_t i, j, k;
  530. for( i = X->n, k = 0; i > 0; i-- )
  531. {
  532. for( j = ciL; j > 0; j-- )
  533. {
  534. c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
  535. if( c == 0 && k == 0 && ( i + j ) != 2 )
  536. continue;
  537. *(p++) = "0123456789ABCDEF" [c / 16];
  538. *(p++) = "0123456789ABCDEF" [c % 16];
  539. k = 1;
  540. }
  541. }
  542. }
  543. else
  544. {
  545. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
  546. if( T.s == -1 )
  547. T.s = 1;
  548. MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
  549. }
  550. *p++ = '\0';
  551. *olen = p - buf;
  552. cleanup:
  553. mbedtls_mpi_free( &T );
  554. return( ret );
  555. }
  556. #if defined(MBEDTLS_FS_IO)
  557. /*
  558. * Read X from an opened file
  559. */
  560. int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
  561. {
  562. mbedtls_mpi_uint d;
  563. size_t slen;
  564. char *p;
  565. /*
  566. * Buffer should have space for (short) label and decimal formatted MPI,
  567. * newline characters and '\0'
  568. */
  569. char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
  570. MPI_VALIDATE_RET( X != NULL );
  571. MPI_VALIDATE_RET( fin != NULL );
  572. if( radix < 2 || radix > 16 )
  573. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  574. memset( s, 0, sizeof( s ) );
  575. if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
  576. return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
  577. slen = strlen( s );
  578. if( slen == sizeof( s ) - 2 )
  579. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  580. if( slen > 0 && s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
  581. if( slen > 0 && s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
  582. p = s + slen;
  583. while( p-- > s )
  584. if( mpi_get_digit( &d, radix, *p ) != 0 )
  585. break;
  586. return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
  587. }
  588. /*
  589. * Write X into an opened file (or stdout if fout == NULL)
  590. */
  591. int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
  592. {
  593. int ret;
  594. size_t n, slen, plen;
  595. /*
  596. * Buffer should have space for (short) label and decimal formatted MPI,
  597. * newline characters and '\0'
  598. */
  599. char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
  600. MPI_VALIDATE_RET( X != NULL );
  601. if( radix < 2 || radix > 16 )
  602. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  603. memset( s, 0, sizeof( s ) );
  604. MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
  605. if( p == NULL ) p = "";
  606. plen = strlen( p );
  607. slen = strlen( s );
  608. s[slen++] = '\r';
  609. s[slen++] = '\n';
  610. if( fout != NULL )
  611. {
  612. if( fwrite( p, 1, plen, fout ) != plen ||
  613. fwrite( s, 1, slen, fout ) != slen )
  614. return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
  615. }
  616. else
  617. mbedtls_printf( "%s%s", p, s );
  618. cleanup:
  619. return( ret );
  620. }
  621. #endif /* MBEDTLS_FS_IO */
  622. /* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
  623. * into the storage form used by mbedtls_mpi. */
  624. static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x )
  625. {
  626. uint8_t i;
  627. unsigned char *x_ptr;
  628. mbedtls_mpi_uint tmp = 0;
  629. for( i = 0, x_ptr = (unsigned char*) &x; i < ciL; i++, x_ptr++ )
  630. {
  631. tmp <<= CHAR_BIT;
  632. tmp |= (mbedtls_mpi_uint) *x_ptr;
  633. }
  634. return( tmp );
  635. }
  636. static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x )
  637. {
  638. #if defined(__BYTE_ORDER__)
  639. /* Nothing to do on bigendian systems. */
  640. #if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
  641. return( x );
  642. #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
  643. #if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
  644. /* For GCC and Clang, have builtins for byte swapping. */
  645. #if defined(__GNUC__) && defined(__GNUC_PREREQ)
  646. #if __GNUC_PREREQ(4,3)
  647. #define have_bswap
  648. #endif
  649. #endif
  650. #if defined(__clang__) && defined(__has_builtin)
  651. #if __has_builtin(__builtin_bswap32) && \
  652. __has_builtin(__builtin_bswap64)
  653. #define have_bswap
  654. #endif
  655. #endif
  656. #if defined(have_bswap)
  657. /* The compiler is hopefully able to statically evaluate this! */
  658. switch( sizeof(mbedtls_mpi_uint) )
  659. {
  660. case 4:
  661. return( __builtin_bswap32(x) );
  662. case 8:
  663. return( __builtin_bswap64(x) );
  664. }
  665. #endif
  666. #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
  667. #endif /* __BYTE_ORDER__ */
  668. /* Fall back to C-based reordering if we don't know the byte order
  669. * or we couldn't use a compiler-specific builtin. */
  670. return( mpi_uint_bigendian_to_host_c( x ) );
  671. }
  672. static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs )
  673. {
  674. mbedtls_mpi_uint *cur_limb_left;
  675. mbedtls_mpi_uint *cur_limb_right;
  676. if( limbs == 0 )
  677. return;
  678. /*
  679. * Traverse limbs and
  680. * - adapt byte-order in each limb
  681. * - swap the limbs themselves.
  682. * For that, simultaneously traverse the limbs from left to right
  683. * and from right to left, as long as the left index is not bigger
  684. * than the right index (it's not a problem if limbs is odd and the
  685. * indices coincide in the last iteration).
  686. */
  687. for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 );
  688. cur_limb_left <= cur_limb_right;
  689. cur_limb_left++, cur_limb_right-- )
  690. {
  691. mbedtls_mpi_uint tmp;
  692. /* Note that if cur_limb_left == cur_limb_right,
  693. * this code effectively swaps the bytes only once. */
  694. tmp = mpi_uint_bigendian_to_host( *cur_limb_left );
  695. *cur_limb_left = mpi_uint_bigendian_to_host( *cur_limb_right );
  696. *cur_limb_right = tmp;
  697. }
  698. }
  699. /*
  700. * Import X from unsigned binary data, big endian
  701. */
  702. int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
  703. {
  704. int ret;
  705. size_t const limbs = CHARS_TO_LIMBS( buflen );
  706. size_t const overhead = ( limbs * ciL ) - buflen;
  707. unsigned char *Xp;
  708. MPI_VALIDATE_RET( X != NULL );
  709. MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
  710. /* Ensure that target MPI has exactly the necessary number of limbs */
  711. if( X->n != limbs )
  712. {
  713. mbedtls_mpi_free( X );
  714. mbedtls_mpi_init( X );
  715. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
  716. }
  717. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  718. /* Avoid calling `memcpy` with NULL source argument,
  719. * even if buflen is 0. */
  720. if( buf != NULL )
  721. {
  722. Xp = (unsigned char*) X->p;
  723. memcpy( Xp + overhead, buf, buflen );
  724. mpi_bigendian_to_host( X->p, limbs );
  725. }
  726. cleanup:
  727. return( ret );
  728. }
  729. /*
  730. * Export X into unsigned binary data, big endian
  731. */
  732. int mbedtls_mpi_write_binary( const mbedtls_mpi *X,
  733. unsigned char *buf, size_t buflen )
  734. {
  735. size_t stored_bytes;
  736. size_t bytes_to_copy;
  737. unsigned char *p;
  738. size_t i;
  739. MPI_VALIDATE_RET( X != NULL );
  740. MPI_VALIDATE_RET( buflen == 0 || buf != NULL );
  741. stored_bytes = X->n * ciL;
  742. if( stored_bytes < buflen )
  743. {
  744. /* There is enough space in the output buffer. Write initial
  745. * null bytes and record the position at which to start
  746. * writing the significant bytes. In this case, the execution
  747. * trace of this function does not depend on the value of the
  748. * number. */
  749. bytes_to_copy = stored_bytes;
  750. p = buf + buflen - stored_bytes;
  751. memset( buf, 0, buflen - stored_bytes );
  752. }
  753. else
  754. {
  755. /* The output buffer is smaller than the allocated size of X.
  756. * However X may fit if its leading bytes are zero. */
  757. bytes_to_copy = buflen;
  758. p = buf;
  759. for( i = bytes_to_copy; i < stored_bytes; i++ )
  760. {
  761. if( GET_BYTE( X, i ) != 0 )
  762. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  763. }
  764. }
  765. for( i = 0; i < bytes_to_copy; i++ )
  766. p[bytes_to_copy - i - 1] = GET_BYTE( X, i );
  767. return( 0 );
  768. }
  769. /*
  770. * Left-shift: X <<= count
  771. */
  772. int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
  773. {
  774. int ret;
  775. size_t i, v0, t1;
  776. mbedtls_mpi_uint r0 = 0, r1;
  777. MPI_VALIDATE_RET( X != NULL );
  778. v0 = count / (biL );
  779. t1 = count & (biL - 1);
  780. i = mbedtls_mpi_bitlen( X ) + count;
  781. if( X->n * biL < i )
  782. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
  783. ret = 0;
  784. /*
  785. * shift by count / limb_size
  786. */
  787. if( v0 > 0 )
  788. {
  789. for( i = X->n; i > v0; i-- )
  790. X->p[i - 1] = X->p[i - v0 - 1];
  791. for( ; i > 0; i-- )
  792. X->p[i - 1] = 0;
  793. }
  794. /*
  795. * shift by count % limb_size
  796. */
  797. if( t1 > 0 )
  798. {
  799. for( i = v0; i < X->n; i++ )
  800. {
  801. r1 = X->p[i] >> (biL - t1);
  802. X->p[i] <<= t1;
  803. X->p[i] |= r0;
  804. r0 = r1;
  805. }
  806. }
  807. cleanup:
  808. return( ret );
  809. }
  810. /*
  811. * Right-shift: X >>= count
  812. */
  813. int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
  814. {
  815. size_t i, v0, v1;
  816. mbedtls_mpi_uint r0 = 0, r1;
  817. MPI_VALIDATE_RET( X != NULL );
  818. v0 = count / biL;
  819. v1 = count & (biL - 1);
  820. if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
  821. return mbedtls_mpi_lset( X, 0 );
  822. /*
  823. * shift by count / limb_size
  824. */
  825. if( v0 > 0 )
  826. {
  827. for( i = 0; i < X->n - v0; i++ )
  828. X->p[i] = X->p[i + v0];
  829. for( ; i < X->n; i++ )
  830. X->p[i] = 0;
  831. }
  832. /*
  833. * shift by count % limb_size
  834. */
  835. if( v1 > 0 )
  836. {
  837. for( i = X->n; i > 0; i-- )
  838. {
  839. r1 = X->p[i - 1] << (biL - v1);
  840. X->p[i - 1] >>= v1;
  841. X->p[i - 1] |= r0;
  842. r0 = r1;
  843. }
  844. }
  845. return( 0 );
  846. }
  847. /*
  848. * Compare unsigned values
  849. */
  850. int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
  851. {
  852. size_t i, j;
  853. MPI_VALIDATE_RET( X != NULL );
  854. MPI_VALIDATE_RET( Y != NULL );
  855. for( i = X->n; i > 0; i-- )
  856. if( X->p[i - 1] != 0 )
  857. break;
  858. for( j = Y->n; j > 0; j-- )
  859. if( Y->p[j - 1] != 0 )
  860. break;
  861. if( i == 0 && j == 0 )
  862. return( 0 );
  863. if( i > j ) return( 1 );
  864. if( j > i ) return( -1 );
  865. for( ; i > 0; i-- )
  866. {
  867. if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
  868. if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
  869. }
  870. return( 0 );
  871. }
  872. /*
  873. * Compare signed values
  874. */
  875. int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
  876. {
  877. size_t i, j;
  878. MPI_VALIDATE_RET( X != NULL );
  879. MPI_VALIDATE_RET( Y != NULL );
  880. for( i = X->n; i > 0; i-- )
  881. if( X->p[i - 1] != 0 )
  882. break;
  883. for( j = Y->n; j > 0; j-- )
  884. if( Y->p[j - 1] != 0 )
  885. break;
  886. if( i == 0 && j == 0 )
  887. return( 0 );
  888. if( i > j ) return( X->s );
  889. if( j > i ) return( -Y->s );
  890. if( X->s > 0 && Y->s < 0 ) return( 1 );
  891. if( Y->s > 0 && X->s < 0 ) return( -1 );
  892. for( ; i > 0; i-- )
  893. {
  894. if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
  895. if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
  896. }
  897. return( 0 );
  898. }
  899. /** Decide if an integer is less than the other, without branches.
  900. *
  901. * \param x First integer.
  902. * \param y Second integer.
  903. *
  904. * \return 1 if \p x is less than \p y, 0 otherwise
  905. */
  906. static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
  907. const mbedtls_mpi_uint y )
  908. {
  909. mbedtls_mpi_uint ret;
  910. mbedtls_mpi_uint cond;
  911. /*
  912. * Check if the most significant bits (MSB) of the operands are different.
  913. */
  914. cond = ( x ^ y );
  915. /*
  916. * If the MSB are the same then the difference x-y will be negative (and
  917. * have its MSB set to 1 during conversion to unsigned) if and only if x<y.
  918. */
  919. ret = ( x - y ) & ~cond;
  920. /*
  921. * If the MSB are different, then the operand with the MSB of 1 is the
  922. * bigger. (That is if y has MSB of 1, then x<y is true and it is false if
  923. * the MSB of y is 0.)
  924. */
  925. ret |= y & cond;
  926. ret = ret >> ( biL - 1 );
  927. return (unsigned) ret;
  928. }
  929. /*
  930. * Compare signed values in constant time
  931. */
  932. int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
  933. unsigned *ret )
  934. {
  935. size_t i;
  936. /* The value of any of these variables is either 0 or 1 at all times. */
  937. unsigned cond, done, X_is_negative, Y_is_negative;
  938. MPI_VALIDATE_RET( X != NULL );
  939. MPI_VALIDATE_RET( Y != NULL );
  940. MPI_VALIDATE_RET( ret != NULL );
  941. if( X->n != Y->n )
  942. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  943. /*
  944. * Set sign_N to 1 if N >= 0, 0 if N < 0.
  945. * We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
  946. */
  947. X_is_negative = ( X->s & 2 ) >> 1;
  948. Y_is_negative = ( Y->s & 2 ) >> 1;
  949. /*
  950. * If the signs are different, then the positive operand is the bigger.
  951. * That is if X is negative (X_is_negative == 1), then X < Y is true and it
  952. * is false if X is positive (X_is_negative == 0).
  953. */
  954. cond = ( X_is_negative ^ Y_is_negative );
  955. *ret = cond & X_is_negative;
  956. /*
  957. * This is a constant-time function. We might have the result, but we still
  958. * need to go through the loop. Record if we have the result already.
  959. */
  960. done = cond;
  961. for( i = X->n; i > 0; i-- )
  962. {
  963. /*
  964. * If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
  965. * X and Y are negative.
  966. *
  967. * Again even if we can make a decision, we just mark the result and
  968. * the fact that we are done and continue looping.
  969. */
  970. cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
  971. *ret |= cond & ( 1 - done ) & X_is_negative;
  972. done |= cond;
  973. /*
  974. * If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
  975. * X and Y are positive.
  976. *
  977. * Again even if we can make a decision, we just mark the result and
  978. * the fact that we are done and continue looping.
  979. */
  980. cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
  981. *ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
  982. done |= cond;
  983. }
  984. return( 0 );
  985. }
  986. /*
  987. * Compare signed values
  988. */
  989. int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
  990. {
  991. mbedtls_mpi Y;
  992. mbedtls_mpi_uint p[1];
  993. MPI_VALIDATE_RET( X != NULL );
  994. *p = ( z < 0 ) ? -z : z;
  995. Y.s = ( z < 0 ) ? -1 : 1;
  996. Y.n = 1;
  997. Y.p = p;
  998. return( mbedtls_mpi_cmp_mpi( X, &Y ) );
  999. }
  1000. /*
  1001. * Unsigned addition: X = |A| + |B| (HAC 14.7)
  1002. */
  1003. int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1004. {
  1005. int ret;
  1006. size_t i, j;
  1007. mbedtls_mpi_uint *o, *p, c, tmp;
  1008. MPI_VALIDATE_RET( X != NULL );
  1009. MPI_VALIDATE_RET( A != NULL );
  1010. MPI_VALIDATE_RET( B != NULL );
  1011. if( X == B )
  1012. {
  1013. const mbedtls_mpi *T = A; A = X; B = T;
  1014. }
  1015. if( X != A )
  1016. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
  1017. /*
  1018. * X should always be positive as a result of unsigned additions.
  1019. */
  1020. X->s = 1;
  1021. for( j = B->n; j > 0; j-- )
  1022. if( B->p[j - 1] != 0 )
  1023. break;
  1024. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
  1025. o = B->p; p = X->p; c = 0;
  1026. /*
  1027. * tmp is used because it might happen that p == o
  1028. */
  1029. for( i = 0; i < j; i++, o++, p++ )
  1030. {
  1031. tmp= *o;
  1032. *p += c; c = ( *p < c );
  1033. *p += tmp; c += ( *p < tmp );
  1034. }
  1035. while( c != 0 )
  1036. {
  1037. if( i >= X->n )
  1038. {
  1039. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
  1040. p = X->p + i;
  1041. }
  1042. *p += c; c = ( *p < c ); i++; p++;
  1043. }
  1044. cleanup:
  1045. return( ret );
  1046. }
  1047. /**
  1048. * Helper for mbedtls_mpi subtraction.
  1049. *
  1050. * Calculate d - s where d and s have the same size.
  1051. * This function operates modulo (2^ciL)^n and returns the carry
  1052. * (1 if there was a wraparound, i.e. if `d < s`, and 0 otherwise).
  1053. *
  1054. * \param n Number of limbs of \p d and \p s.
  1055. * \param[in,out] d On input, the left operand.
  1056. * On output, the result of the subtraction:
  1057. * \param[in] s The right operand.
  1058. *
  1059. * \return 1 if `d < s`.
  1060. * 0 if `d >= s`.
  1061. */
  1062. static mbedtls_mpi_uint mpi_sub_hlp( size_t n,
  1063. mbedtls_mpi_uint *d,
  1064. const mbedtls_mpi_uint *s )
  1065. {
  1066. size_t i;
  1067. mbedtls_mpi_uint c, z;
  1068. for( i = c = 0; i < n; i++, s++, d++ )
  1069. {
  1070. z = ( *d < c ); *d -= c;
  1071. c = ( *d < *s ) + z; *d -= *s;
  1072. }
  1073. return( c );
  1074. }
  1075. /*
  1076. * Unsigned subtraction: X = |A| - |B| (HAC 14.9, 14.10)
  1077. */
  1078. int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1079. {
  1080. mbedtls_mpi TB;
  1081. int ret;
  1082. size_t n;
  1083. mbedtls_mpi_uint carry;
  1084. MPI_VALIDATE_RET( X != NULL );
  1085. MPI_VALIDATE_RET( A != NULL );
  1086. MPI_VALIDATE_RET( B != NULL );
  1087. mbedtls_mpi_init( &TB );
  1088. if( X == B )
  1089. {
  1090. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
  1091. B = &TB;
  1092. }
  1093. if( X != A )
  1094. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
  1095. /*
  1096. * X should always be positive as a result of unsigned subtractions.
  1097. */
  1098. X->s = 1;
  1099. ret = 0;
  1100. for( n = B->n; n > 0; n-- )
  1101. if( B->p[n - 1] != 0 )
  1102. break;
  1103. if( n > A->n )
  1104. {
  1105. /* B >= (2^ciL)^n > A */
  1106. ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
  1107. goto cleanup;
  1108. }
  1109. carry = mpi_sub_hlp( n, X->p, B->p );
  1110. if( carry != 0 )
  1111. {
  1112. /* Propagate the carry to the first nonzero limb of X. */
  1113. for( ; n < X->n && X->p[n] == 0; n++ )
  1114. --X->p[n];
  1115. /* If we ran out of space for the carry, it means that the result
  1116. * is negative. */
  1117. if( n == X->n )
  1118. {
  1119. ret = MBEDTLS_ERR_MPI_NEGATIVE_VALUE;
  1120. goto cleanup;
  1121. }
  1122. --X->p[n];
  1123. }
  1124. cleanup:
  1125. mbedtls_mpi_free( &TB );
  1126. return( ret );
  1127. }
  1128. /*
  1129. * Signed addition: X = A + B
  1130. */
  1131. int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1132. {
  1133. int ret, s;
  1134. MPI_VALIDATE_RET( X != NULL );
  1135. MPI_VALIDATE_RET( A != NULL );
  1136. MPI_VALIDATE_RET( B != NULL );
  1137. s = A->s;
  1138. if( A->s * B->s < 0 )
  1139. {
  1140. if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
  1141. {
  1142. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
  1143. X->s = s;
  1144. }
  1145. else
  1146. {
  1147. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
  1148. X->s = -s;
  1149. }
  1150. }
  1151. else
  1152. {
  1153. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
  1154. X->s = s;
  1155. }
  1156. cleanup:
  1157. return( ret );
  1158. }
  1159. /*
  1160. * Signed subtraction: X = A - B
  1161. */
  1162. int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1163. {
  1164. int ret, s;
  1165. MPI_VALIDATE_RET( X != NULL );
  1166. MPI_VALIDATE_RET( A != NULL );
  1167. MPI_VALIDATE_RET( B != NULL );
  1168. s = A->s;
  1169. if( A->s * B->s > 0 )
  1170. {
  1171. if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
  1172. {
  1173. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
  1174. X->s = s;
  1175. }
  1176. else
  1177. {
  1178. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
  1179. X->s = -s;
  1180. }
  1181. }
  1182. else
  1183. {
  1184. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
  1185. X->s = s;
  1186. }
  1187. cleanup:
  1188. return( ret );
  1189. }
  1190. /*
  1191. * Signed addition: X = A + b
  1192. */
  1193. int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  1194. {
  1195. mbedtls_mpi _B;
  1196. mbedtls_mpi_uint p[1];
  1197. MPI_VALIDATE_RET( X != NULL );
  1198. MPI_VALIDATE_RET( A != NULL );
  1199. p[0] = ( b < 0 ) ? -b : b;
  1200. _B.s = ( b < 0 ) ? -1 : 1;
  1201. _B.n = 1;
  1202. _B.p = p;
  1203. return( mbedtls_mpi_add_mpi( X, A, &_B ) );
  1204. }
  1205. /*
  1206. * Signed subtraction: X = A - b
  1207. */
  1208. int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  1209. {
  1210. mbedtls_mpi _B;
  1211. mbedtls_mpi_uint p[1];
  1212. MPI_VALIDATE_RET( X != NULL );
  1213. MPI_VALIDATE_RET( A != NULL );
  1214. p[0] = ( b < 0 ) ? -b : b;
  1215. _B.s = ( b < 0 ) ? -1 : 1;
  1216. _B.n = 1;
  1217. _B.p = p;
  1218. return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
  1219. }
  1220. /*
  1221. * Helper for mbedtls_mpi multiplication
  1222. */
  1223. static
  1224. #if defined(__APPLE__) && defined(__arm__)
  1225. /*
  1226. * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
  1227. * appears to need this to prevent bad ARM code generation at -O3.
  1228. */
  1229. __attribute__ ((noinline))
  1230. #endif
  1231. void mpi_mul_hlp( size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b )
  1232. {
  1233. mbedtls_mpi_uint c = 0, t = 0;
  1234. #if defined(MULADDC_HUIT)
  1235. for( ; i >= 8; i -= 8 )
  1236. {
  1237. MULADDC_INIT
  1238. MULADDC_HUIT
  1239. MULADDC_STOP
  1240. }
  1241. for( ; i > 0; i-- )
  1242. {
  1243. MULADDC_INIT
  1244. MULADDC_CORE
  1245. MULADDC_STOP
  1246. }
  1247. #else /* MULADDC_HUIT */
  1248. for( ; i >= 16; i -= 16 )
  1249. {
  1250. MULADDC_INIT
  1251. MULADDC_CORE MULADDC_CORE
  1252. MULADDC_CORE MULADDC_CORE
  1253. MULADDC_CORE MULADDC_CORE
  1254. MULADDC_CORE MULADDC_CORE
  1255. MULADDC_CORE MULADDC_CORE
  1256. MULADDC_CORE MULADDC_CORE
  1257. MULADDC_CORE MULADDC_CORE
  1258. MULADDC_CORE MULADDC_CORE
  1259. MULADDC_STOP
  1260. }
  1261. for( ; i >= 8; i -= 8 )
  1262. {
  1263. MULADDC_INIT
  1264. MULADDC_CORE MULADDC_CORE
  1265. MULADDC_CORE MULADDC_CORE
  1266. MULADDC_CORE MULADDC_CORE
  1267. MULADDC_CORE MULADDC_CORE
  1268. MULADDC_STOP
  1269. }
  1270. for( ; i > 0; i-- )
  1271. {
  1272. MULADDC_INIT
  1273. MULADDC_CORE
  1274. MULADDC_STOP
  1275. }
  1276. #endif /* MULADDC_HUIT */
  1277. t++;
  1278. do {
  1279. *d += c; c = ( *d < c ); d++;
  1280. }
  1281. while( c != 0 );
  1282. }
  1283. /*
  1284. * Baseline multiplication: X = A * B (HAC 14.12)
  1285. */
  1286. int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1287. {
  1288. int ret;
  1289. size_t i, j;
  1290. mbedtls_mpi TA, TB;
  1291. MPI_VALIDATE_RET( X != NULL );
  1292. MPI_VALIDATE_RET( A != NULL );
  1293. MPI_VALIDATE_RET( B != NULL );
  1294. mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
  1295. if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
  1296. if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
  1297. for( i = A->n; i > 0; i-- )
  1298. if( A->p[i - 1] != 0 )
  1299. break;
  1300. for( j = B->n; j > 0; j-- )
  1301. if( B->p[j - 1] != 0 )
  1302. break;
  1303. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
  1304. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  1305. for( ; j > 0; j-- )
  1306. mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] );
  1307. X->s = A->s * B->s;
  1308. cleanup:
  1309. mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
  1310. return( ret );
  1311. }
  1312. /*
  1313. * Baseline multiplication: X = A * b
  1314. */
  1315. int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
  1316. {
  1317. mbedtls_mpi _B;
  1318. mbedtls_mpi_uint p[1];
  1319. MPI_VALIDATE_RET( X != NULL );
  1320. MPI_VALIDATE_RET( A != NULL );
  1321. _B.s = 1;
  1322. _B.n = 1;
  1323. _B.p = p;
  1324. p[0] = b;
  1325. return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
  1326. }
  1327. /*
  1328. * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
  1329. * mbedtls_mpi_uint divisor, d
  1330. */
  1331. static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
  1332. mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
  1333. {
  1334. #if defined(MBEDTLS_HAVE_UDBL)
  1335. mbedtls_t_udbl dividend, quotient;
  1336. #else
  1337. const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
  1338. const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
  1339. mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
  1340. mbedtls_mpi_uint u0_msw, u0_lsw;
  1341. size_t s;
  1342. #endif
  1343. /*
  1344. * Check for overflow
  1345. */
  1346. if( 0 == d || u1 >= d )
  1347. {
  1348. if (r != NULL) *r = ~0;
  1349. return ( ~0 );
  1350. }
  1351. #if defined(MBEDTLS_HAVE_UDBL)
  1352. dividend = (mbedtls_t_udbl) u1 << biL;
  1353. dividend |= (mbedtls_t_udbl) u0;
  1354. quotient = dividend / d;
  1355. if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
  1356. quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
  1357. if( r != NULL )
  1358. *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
  1359. return (mbedtls_mpi_uint) quotient;
  1360. #else
  1361. /*
  1362. * Algorithm D, Section 4.3.1 - The Art of Computer Programming
  1363. * Vol. 2 - Seminumerical Algorithms, Knuth
  1364. */
  1365. /*
  1366. * Normalize the divisor, d, and dividend, u0, u1
  1367. */
  1368. s = mbedtls_clz( d );
  1369. d = d << s;
  1370. u1 = u1 << s;
  1371. u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
  1372. u0 = u0 << s;
  1373. d1 = d >> biH;
  1374. d0 = d & uint_halfword_mask;
  1375. u0_msw = u0 >> biH;
  1376. u0_lsw = u0 & uint_halfword_mask;
  1377. /*
  1378. * Find the first quotient and remainder
  1379. */
  1380. q1 = u1 / d1;
  1381. r0 = u1 - d1 * q1;
  1382. while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
  1383. {
  1384. q1 -= 1;
  1385. r0 += d1;
  1386. if ( r0 >= radix ) break;
  1387. }
  1388. rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
  1389. q0 = rAX / d1;
  1390. r0 = rAX - q0 * d1;
  1391. while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
  1392. {
  1393. q0 -= 1;
  1394. r0 += d1;
  1395. if ( r0 >= radix ) break;
  1396. }
  1397. if (r != NULL)
  1398. *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
  1399. quotient = q1 * radix + q0;
  1400. return quotient;
  1401. #endif
  1402. }
  1403. /*
  1404. * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
  1405. */
  1406. int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
  1407. const mbedtls_mpi *B )
  1408. {
  1409. int ret;
  1410. size_t i, n, t, k;
  1411. mbedtls_mpi X, Y, Z, T1, T2;
  1412. MPI_VALIDATE_RET( A != NULL );
  1413. MPI_VALIDATE_RET( B != NULL );
  1414. if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
  1415. return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
  1416. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  1417. mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
  1418. if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
  1419. {
  1420. if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
  1421. if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
  1422. return( 0 );
  1423. }
  1424. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
  1425. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
  1426. X.s = Y.s = 1;
  1427. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
  1428. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
  1429. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
  1430. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) );
  1431. k = mbedtls_mpi_bitlen( &Y ) % biL;
  1432. if( k < biL - 1 )
  1433. {
  1434. k = biL - 1 - k;
  1435. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
  1436. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
  1437. }
  1438. else k = 0;
  1439. n = X.n - 1;
  1440. t = Y.n - 1;
  1441. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
  1442. while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
  1443. {
  1444. Z.p[n - t]++;
  1445. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
  1446. }
  1447. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
  1448. for( i = n; i > t ; i-- )
  1449. {
  1450. if( X.p[i] >= Y.p[t] )
  1451. Z.p[i - t - 1] = ~0;
  1452. else
  1453. {
  1454. Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
  1455. Y.p[t], NULL);
  1456. }
  1457. Z.p[i - t - 1]++;
  1458. do
  1459. {
  1460. Z.p[i - t - 1]--;
  1461. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
  1462. T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
  1463. T1.p[1] = Y.p[t];
  1464. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
  1465. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) );
  1466. T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
  1467. T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
  1468. T2.p[2] = X.p[i];
  1469. }
  1470. while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
  1471. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
  1472. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
  1473. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
  1474. if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
  1475. {
  1476. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
  1477. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
  1478. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
  1479. Z.p[i - t - 1]--;
  1480. }
  1481. }
  1482. if( Q != NULL )
  1483. {
  1484. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
  1485. Q->s = A->s * B->s;
  1486. }
  1487. if( R != NULL )
  1488. {
  1489. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
  1490. X.s = A->s;
  1491. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
  1492. if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
  1493. R->s = 1;
  1494. }
  1495. cleanup:
  1496. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  1497. mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
  1498. return( ret );
  1499. }
  1500. /*
  1501. * Division by int: A = Q * b + R
  1502. */
  1503. int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R,
  1504. const mbedtls_mpi *A,
  1505. mbedtls_mpi_sint b )
  1506. {
  1507. mbedtls_mpi _B;
  1508. mbedtls_mpi_uint p[1];
  1509. MPI_VALIDATE_RET( A != NULL );
  1510. p[0] = ( b < 0 ) ? -b : b;
  1511. _B.s = ( b < 0 ) ? -1 : 1;
  1512. _B.n = 1;
  1513. _B.p = p;
  1514. return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
  1515. }
  1516. /*
  1517. * Modulo: R = A mod B
  1518. */
  1519. int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1520. {
  1521. int ret;
  1522. MPI_VALIDATE_RET( R != NULL );
  1523. MPI_VALIDATE_RET( A != NULL );
  1524. MPI_VALIDATE_RET( B != NULL );
  1525. if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
  1526. return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
  1527. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
  1528. while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
  1529. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
  1530. while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
  1531. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
  1532. cleanup:
  1533. return( ret );
  1534. }
  1535. /*
  1536. * Modulo: r = A mod b
  1537. */
  1538. int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  1539. {
  1540. size_t i;
  1541. mbedtls_mpi_uint x, y, z;
  1542. MPI_VALIDATE_RET( r != NULL );
  1543. MPI_VALIDATE_RET( A != NULL );
  1544. if( b == 0 )
  1545. return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
  1546. if( b < 0 )
  1547. return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
  1548. /*
  1549. * handle trivial cases
  1550. */
  1551. if( b == 1 )
  1552. {
  1553. *r = 0;
  1554. return( 0 );
  1555. }
  1556. if( b == 2 )
  1557. {
  1558. *r = A->p[0] & 1;
  1559. return( 0 );
  1560. }
  1561. /*
  1562. * general case
  1563. */
  1564. for( i = A->n, y = 0; i > 0; i-- )
  1565. {
  1566. x = A->p[i - 1];
  1567. y = ( y << biH ) | ( x >> biH );
  1568. z = y / b;
  1569. y -= z * b;
  1570. x <<= biH;
  1571. y = ( y << biH ) | ( x >> biH );
  1572. z = y / b;
  1573. y -= z * b;
  1574. }
  1575. /*
  1576. * If A is negative, then the current y represents a negative value.
  1577. * Flipping it to the positive side.
  1578. */
  1579. if( A->s < 0 && y != 0 )
  1580. y = b - y;
  1581. *r = y;
  1582. return( 0 );
  1583. }
  1584. /*
  1585. * Fast Montgomery initialization (thanks to Tom St Denis)
  1586. */
  1587. static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
  1588. {
  1589. mbedtls_mpi_uint x, m0 = N->p[0];
  1590. unsigned int i;
  1591. x = m0;
  1592. x += ( ( m0 + 2 ) & 4 ) << 1;
  1593. for( i = biL; i >= 8; i /= 2 )
  1594. x *= ( 2 - ( m0 * x ) );
  1595. *mm = ~x + 1;
  1596. }
  1597. /** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
  1598. *
  1599. * \param[in,out] A One of the numbers to multiply.
  1600. * It must have at least as many limbs as N
  1601. * (A->n >= N->n), and any limbs beyond n are ignored.
  1602. * On successful completion, A contains the result of
  1603. * the multiplication A * B * R^-1 mod N where
  1604. * R = (2^ciL)^n.
  1605. * \param[in] B One of the numbers to multiply.
  1606. * It must be nonzero and must not have more limbs than N
  1607. * (B->n <= N->n).
  1608. * \param[in] N The modulo. N must be odd.
  1609. * \param mm The value calculated by `mpi_montg_init(&mm, N)`.
  1610. * This is -N^-1 mod 2^ciL.
  1611. * \param[in,out] T A bignum for temporary storage.
  1612. * It must be at least twice the limb size of N plus 2
  1613. * (T->n >= 2 * (N->n + 1)).
  1614. * Its initial content is unused and
  1615. * its final content is indeterminate.
  1616. * Note that unlike the usual convention in the library
  1617. * for `const mbedtls_mpi*`, the content of T can change.
  1618. */
  1619. static void mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
  1620. const mbedtls_mpi *T )
  1621. {
  1622. size_t i, n, m;
  1623. mbedtls_mpi_uint u0, u1, *d;
  1624. memset( T->p, 0, T->n * ciL );
  1625. d = T->p;
  1626. n = N->n;
  1627. m = ( B->n < n ) ? B->n : n;
  1628. for( i = 0; i < n; i++ )
  1629. {
  1630. /*
  1631. * T = (T + u0*B + u1*N) / 2^biL
  1632. */
  1633. u0 = A->p[i];
  1634. u1 = ( d[0] + u0 * B->p[0] ) * mm;
  1635. mpi_mul_hlp( m, B->p, d, u0 );
  1636. mpi_mul_hlp( n, N->p, d, u1 );
  1637. *d++ = u0; d[n + 1] = 0;
  1638. }
  1639. /* At this point, d is either the desired result or the desired result
  1640. * plus N. We now potentially subtract N, avoiding leaking whether the
  1641. * subtraction is performed through side channels. */
  1642. /* Copy the n least significant limbs of d to A, so that
  1643. * A = d if d < N (recall that N has n limbs). */
  1644. memcpy( A->p, d, n * ciL );
  1645. /* If d >= N then we want to set A to d - N. To prevent timing attacks,
  1646. * do the calculation without using conditional tests. */
  1647. /* Set d to d0 + (2^biL)^n - N where d0 is the current value of d. */
  1648. d[n] += 1;
  1649. d[n] -= mpi_sub_hlp( n, d, N->p );
  1650. /* If d0 < N then d < (2^biL)^n
  1651. * so d[n] == 0 and we want to keep A as it is.
  1652. * If d0 >= N then d >= (2^biL)^n, and d <= (2^biL)^n + N < 2 * (2^biL)^n
  1653. * so d[n] == 1 and we want to set A to the result of the subtraction
  1654. * which is d - (2^biL)^n, i.e. the n least significant limbs of d.
  1655. * This exactly corresponds to a conditional assignment. */
  1656. mpi_safe_cond_assign( n, A->p, d, (unsigned char) d[n] );
  1657. }
  1658. /*
  1659. * Montgomery reduction: A = A * R^-1 mod N
  1660. *
  1661. * See mpi_montmul() regarding constraints and guarantees on the parameters.
  1662. */
  1663. static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N,
  1664. mbedtls_mpi_uint mm, const mbedtls_mpi *T )
  1665. {
  1666. mbedtls_mpi_uint z = 1;
  1667. mbedtls_mpi U;
  1668. U.n = U.s = (int) z;
  1669. U.p = &z;
  1670. mpi_montmul( A, &U, N, mm, T );
  1671. }
  1672. /*
  1673. * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
  1674. */
  1675. int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
  1676. const mbedtls_mpi *E, const mbedtls_mpi *N,
  1677. mbedtls_mpi *_RR )
  1678. {
  1679. int ret;
  1680. size_t wbits, wsize, one = 1;
  1681. size_t i, j, nblimbs;
  1682. size_t bufsize, nbits;
  1683. mbedtls_mpi_uint ei, mm, state;
  1684. mbedtls_mpi RR, T, W[ 1 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
  1685. int neg;
  1686. MPI_VALIDATE_RET( X != NULL );
  1687. MPI_VALIDATE_RET( A != NULL );
  1688. MPI_VALIDATE_RET( E != NULL );
  1689. MPI_VALIDATE_RET( N != NULL );
  1690. if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 )
  1691. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1692. if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
  1693. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1694. if( mbedtls_mpi_bitlen( E ) > MBEDTLS_MPI_MAX_BITS ||
  1695. mbedtls_mpi_bitlen( N ) > MBEDTLS_MPI_MAX_BITS )
  1696. return ( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1697. /*
  1698. * Init temps and window size
  1699. */
  1700. mpi_montg_init( &mm, N );
  1701. mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
  1702. mbedtls_mpi_init( &Apos );
  1703. memset( W, 0, sizeof( W ) );
  1704. i = mbedtls_mpi_bitlen( E );
  1705. wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
  1706. ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
  1707. #if( MBEDTLS_MPI_WINDOW_SIZE < 6 )
  1708. if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
  1709. wsize = MBEDTLS_MPI_WINDOW_SIZE;
  1710. #endif
  1711. j = N->n + 1;
  1712. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
  1713. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
  1714. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
  1715. /*
  1716. * Compensate for negative A (and correct at the end)
  1717. */
  1718. neg = ( A->s == -1 );
  1719. if( neg )
  1720. {
  1721. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
  1722. Apos.s = 1;
  1723. A = &Apos;
  1724. }
  1725. /*
  1726. * If 1st call, pre-compute R^2 mod N
  1727. */
  1728. if( _RR == NULL || _RR->p == NULL )
  1729. {
  1730. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
  1731. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
  1732. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
  1733. if( _RR != NULL )
  1734. memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
  1735. }
  1736. else
  1737. memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
  1738. /*
  1739. * W[1] = A * R^2 * R^-1 mod N = A * R mod N
  1740. */
  1741. if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
  1742. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
  1743. else
  1744. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
  1745. mpi_montmul( &W[1], &RR, N, mm, &T );
  1746. /*
  1747. * X = R^2 * R^-1 mod N = R mod N
  1748. */
  1749. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
  1750. mpi_montred( X, N, mm, &T );
  1751. if( wsize > 1 )
  1752. {
  1753. /*
  1754. * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
  1755. */
  1756. j = one << ( wsize - 1 );
  1757. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
  1758. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
  1759. for( i = 0; i < wsize - 1; i++ )
  1760. mpi_montmul( &W[j], &W[j], N, mm, &T );
  1761. /*
  1762. * W[i] = W[i - 1] * W[1]
  1763. */
  1764. for( i = j + 1; i < ( one << wsize ); i++ )
  1765. {
  1766. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
  1767. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
  1768. mpi_montmul( &W[i], &W[1], N, mm, &T );
  1769. }
  1770. }
  1771. nblimbs = E->n;
  1772. bufsize = 0;
  1773. nbits = 0;
  1774. wbits = 0;
  1775. state = 0;
  1776. while( 1 )
  1777. {
  1778. if( bufsize == 0 )
  1779. {
  1780. if( nblimbs == 0 )
  1781. break;
  1782. nblimbs--;
  1783. bufsize = sizeof( mbedtls_mpi_uint ) << 3;
  1784. }
  1785. bufsize--;
  1786. ei = (E->p[nblimbs] >> bufsize) & 1;
  1787. /*
  1788. * skip leading 0s
  1789. */
  1790. if( ei == 0 && state == 0 )
  1791. continue;
  1792. if( ei == 0 && state == 1 )
  1793. {
  1794. /*
  1795. * out of window, square X
  1796. */
  1797. mpi_montmul( X, X, N, mm, &T );
  1798. continue;
  1799. }
  1800. /*
  1801. * add ei to current window
  1802. */
  1803. state = 2;
  1804. nbits++;
  1805. wbits |= ( ei << ( wsize - nbits ) );
  1806. if( nbits == wsize )
  1807. {
  1808. /*
  1809. * X = X^wsize R^-1 mod N
  1810. */
  1811. for( i = 0; i < wsize; i++ )
  1812. mpi_montmul( X, X, N, mm, &T );
  1813. /*
  1814. * X = X * W[wbits] R^-1 mod N
  1815. */
  1816. mpi_montmul( X, &W[wbits], N, mm, &T );
  1817. state--;
  1818. nbits = 0;
  1819. wbits = 0;
  1820. }
  1821. }
  1822. /*
  1823. * process the remaining bits
  1824. */
  1825. for( i = 0; i < nbits; i++ )
  1826. {
  1827. mpi_montmul( X, X, N, mm, &T );
  1828. wbits <<= 1;
  1829. if( ( wbits & ( one << wsize ) ) != 0 )
  1830. mpi_montmul( X, &W[1], N, mm, &T );
  1831. }
  1832. /*
  1833. * X = A^E * R * R^-1 mod N = A^E mod N
  1834. */
  1835. mpi_montred( X, N, mm, &T );
  1836. if( neg && E->n != 0 && ( E->p[0] & 1 ) != 0 )
  1837. {
  1838. X->s = -1;
  1839. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
  1840. }
  1841. cleanup:
  1842. for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
  1843. mbedtls_mpi_free( &W[i] );
  1844. mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
  1845. if( _RR == NULL || _RR->p == NULL )
  1846. mbedtls_mpi_free( &RR );
  1847. return( ret );
  1848. }
  1849. /*
  1850. * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
  1851. */
  1852. int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1853. {
  1854. int ret;
  1855. size_t lz, lzt;
  1856. mbedtls_mpi TG, TA, TB;
  1857. MPI_VALIDATE_RET( G != NULL );
  1858. MPI_VALIDATE_RET( A != NULL );
  1859. MPI_VALIDATE_RET( B != NULL );
  1860. mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
  1861. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
  1862. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
  1863. lz = mbedtls_mpi_lsb( &TA );
  1864. lzt = mbedtls_mpi_lsb( &TB );
  1865. if( lzt < lz )
  1866. lz = lzt;
  1867. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
  1868. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
  1869. TA.s = TB.s = 1;
  1870. while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
  1871. {
  1872. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
  1873. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
  1874. if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
  1875. {
  1876. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
  1877. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
  1878. }
  1879. else
  1880. {
  1881. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
  1882. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
  1883. }
  1884. }
  1885. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
  1886. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
  1887. cleanup:
  1888. mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
  1889. return( ret );
  1890. }
  1891. /*
  1892. * Fill X with size bytes of random.
  1893. *
  1894. * Use a temporary bytes representation to make sure the result is the same
  1895. * regardless of the platform endianness (useful when f_rng is actually
  1896. * deterministic, eg for tests).
  1897. */
  1898. int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
  1899. int (*f_rng)(void *, unsigned char *, size_t),
  1900. void *p_rng )
  1901. {
  1902. int ret;
  1903. size_t const limbs = CHARS_TO_LIMBS( size );
  1904. size_t const overhead = ( limbs * ciL ) - size;
  1905. unsigned char *Xp;
  1906. MPI_VALIDATE_RET( X != NULL );
  1907. MPI_VALIDATE_RET( f_rng != NULL );
  1908. /* Ensure that target MPI has exactly the necessary number of limbs */
  1909. if( X->n != limbs )
  1910. {
  1911. mbedtls_mpi_free( X );
  1912. mbedtls_mpi_init( X );
  1913. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) );
  1914. }
  1915. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  1916. Xp = (unsigned char*) X->p;
  1917. MBEDTLS_MPI_CHK( f_rng( p_rng, Xp + overhead, size ) );
  1918. mpi_bigendian_to_host( X->p, limbs );
  1919. cleanup:
  1920. return( ret );
  1921. }
  1922. /*
  1923. * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
  1924. */
  1925. int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
  1926. {
  1927. int ret;
  1928. mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
  1929. MPI_VALIDATE_RET( X != NULL );
  1930. MPI_VALIDATE_RET( A != NULL );
  1931. MPI_VALIDATE_RET( N != NULL );
  1932. if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 )
  1933. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1934. mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
  1935. mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
  1936. mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
  1937. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
  1938. if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
  1939. {
  1940. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  1941. goto cleanup;
  1942. }
  1943. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
  1944. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
  1945. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
  1946. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
  1947. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
  1948. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
  1949. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
  1950. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
  1951. do
  1952. {
  1953. while( ( TU.p[0] & 1 ) == 0 )
  1954. {
  1955. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
  1956. if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
  1957. {
  1958. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
  1959. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
  1960. }
  1961. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
  1962. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
  1963. }
  1964. while( ( TV.p[0] & 1 ) == 0 )
  1965. {
  1966. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
  1967. if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
  1968. {
  1969. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
  1970. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
  1971. }
  1972. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
  1973. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
  1974. }
  1975. if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
  1976. {
  1977. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
  1978. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
  1979. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
  1980. }
  1981. else
  1982. {
  1983. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
  1984. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
  1985. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
  1986. }
  1987. }
  1988. while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
  1989. while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
  1990. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
  1991. while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
  1992. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
  1993. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
  1994. cleanup:
  1995. mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
  1996. mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
  1997. mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
  1998. return( ret );
  1999. }
  2000. #if defined(MBEDTLS_GENPRIME)
  2001. static const int small_prime[] =
  2002. {
  2003. 3, 5, 7, 11, 13, 17, 19, 23,
  2004. 29, 31, 37, 41, 43, 47, 53, 59,
  2005. 61, 67, 71, 73, 79, 83, 89, 97,
  2006. 101, 103, 107, 109, 113, 127, 131, 137,
  2007. 139, 149, 151, 157, 163, 167, 173, 179,
  2008. 181, 191, 193, 197, 199, 211, 223, 227,
  2009. 229, 233, 239, 241, 251, 257, 263, 269,
  2010. 271, 277, 281, 283, 293, 307, 311, 313,
  2011. 317, 331, 337, 347, 349, 353, 359, 367,
  2012. 373, 379, 383, 389, 397, 401, 409, 419,
  2013. 421, 431, 433, 439, 443, 449, 457, 461,
  2014. 463, 467, 479, 487, 491, 499, 503, 509,
  2015. 521, 523, 541, 547, 557, 563, 569, 571,
  2016. 577, 587, 593, 599, 601, 607, 613, 617,
  2017. 619, 631, 641, 643, 647, 653, 659, 661,
  2018. 673, 677, 683, 691, 701, 709, 719, 727,
  2019. 733, 739, 743, 751, 757, 761, 769, 773,
  2020. 787, 797, 809, 811, 821, 823, 827, 829,
  2021. 839, 853, 857, 859, 863, 877, 881, 883,
  2022. 887, 907, 911, 919, 929, 937, 941, 947,
  2023. 953, 967, 971, 977, 983, 991, 997, -103
  2024. };
  2025. /*
  2026. * Small divisors test (X must be positive)
  2027. *
  2028. * Return values:
  2029. * 0: no small factor (possible prime, more tests needed)
  2030. * 1: certain prime
  2031. * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
  2032. * other negative: error
  2033. */
  2034. static int mpi_check_small_factors( const mbedtls_mpi *X )
  2035. {
  2036. int ret = 0;
  2037. size_t i;
  2038. mbedtls_mpi_uint r;
  2039. if( ( X->p[0] & 1 ) == 0 )
  2040. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  2041. for( i = 0; small_prime[i] > 0; i++ )
  2042. {
  2043. if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
  2044. return( 1 );
  2045. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
  2046. if( r == 0 )
  2047. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  2048. }
  2049. cleanup:
  2050. return( ret );
  2051. }
  2052. /*
  2053. * Miller-Rabin pseudo-primality test (HAC 4.24)
  2054. */
  2055. static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds,
  2056. int (*f_rng)(void *, unsigned char *, size_t),
  2057. void *p_rng )
  2058. {
  2059. int ret, count;
  2060. size_t i, j, k, s;
  2061. mbedtls_mpi W, R, T, A, RR;
  2062. MPI_VALIDATE_RET( X != NULL );
  2063. MPI_VALIDATE_RET( f_rng != NULL );
  2064. mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R );
  2065. mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
  2066. mbedtls_mpi_init( &RR );
  2067. /*
  2068. * W = |X| - 1
  2069. * R = W >> lsb( W )
  2070. */
  2071. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
  2072. s = mbedtls_mpi_lsb( &W );
  2073. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
  2074. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
  2075. for( i = 0; i < rounds; i++ )
  2076. {
  2077. /*
  2078. * pick a random A, 1 < A < |X| - 1
  2079. */
  2080. count = 0;
  2081. do {
  2082. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
  2083. j = mbedtls_mpi_bitlen( &A );
  2084. k = mbedtls_mpi_bitlen( &W );
  2085. if (j > k) {
  2086. A.p[A.n - 1] &= ( (mbedtls_mpi_uint) 1 << ( k - ( A.n - 1 ) * biL - 1 ) ) - 1;
  2087. }
  2088. if (count++ > 30) {
  2089. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  2090. goto cleanup;
  2091. }
  2092. } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
  2093. mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
  2094. /*
  2095. * A = A^R mod |X|
  2096. */
  2097. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
  2098. if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
  2099. mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  2100. continue;
  2101. j = 1;
  2102. while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
  2103. {
  2104. /*
  2105. * A = A * A mod |X|
  2106. */
  2107. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
  2108. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
  2109. if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  2110. break;
  2111. j++;
  2112. }
  2113. /*
  2114. * not prime if A != |X| - 1 or A == 1
  2115. */
  2116. if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
  2117. mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  2118. {
  2119. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  2120. break;
  2121. }
  2122. }
  2123. cleanup:
  2124. mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R );
  2125. mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
  2126. mbedtls_mpi_free( &RR );
  2127. return( ret );
  2128. }
  2129. /*
  2130. * Pseudo-primality test: small factors, then Miller-Rabin
  2131. */
  2132. int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
  2133. int (*f_rng)(void *, unsigned char *, size_t),
  2134. void *p_rng )
  2135. {
  2136. int ret;
  2137. mbedtls_mpi XX;
  2138. MPI_VALIDATE_RET( X != NULL );
  2139. MPI_VALIDATE_RET( f_rng != NULL );
  2140. XX.s = 1;
  2141. XX.n = X->n;
  2142. XX.p = X->p;
  2143. if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
  2144. mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
  2145. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  2146. if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
  2147. return( 0 );
  2148. if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
  2149. {
  2150. if( ret == 1 )
  2151. return( 0 );
  2152. return( ret );
  2153. }
  2154. return( mpi_miller_rabin( &XX, rounds, f_rng, p_rng ) );
  2155. }
  2156. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  2157. /*
  2158. * Pseudo-primality test, error probability 2^-80
  2159. */
  2160. int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
  2161. int (*f_rng)(void *, unsigned char *, size_t),
  2162. void *p_rng )
  2163. {
  2164. MPI_VALIDATE_RET( X != NULL );
  2165. MPI_VALIDATE_RET( f_rng != NULL );
  2166. /*
  2167. * In the past our key generation aimed for an error rate of at most
  2168. * 2^-80. Since this function is deprecated, aim for the same certainty
  2169. * here as well.
  2170. */
  2171. return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) );
  2172. }
  2173. #endif
  2174. /*
  2175. * Prime number generation
  2176. *
  2177. * To generate an RSA key in a way recommended by FIPS 186-4, both primes must
  2178. * be either 1024 bits or 1536 bits long, and flags must contain
  2179. * MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR.
  2180. */
  2181. int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
  2182. int (*f_rng)(void *, unsigned char *, size_t),
  2183. void *p_rng )
  2184. {
  2185. #ifdef MBEDTLS_HAVE_INT64
  2186. // ceil(2^63.5)
  2187. #define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
  2188. #else
  2189. // ceil(2^31.5)
  2190. #define CEIL_MAXUINT_DIV_SQRT2 0xb504f334U
  2191. #endif
  2192. int ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  2193. size_t k, n;
  2194. int rounds;
  2195. mbedtls_mpi_uint r;
  2196. mbedtls_mpi Y;
  2197. MPI_VALIDATE_RET( X != NULL );
  2198. MPI_VALIDATE_RET( f_rng != NULL );
  2199. if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
  2200. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  2201. mbedtls_mpi_init( &Y );
  2202. n = BITS_TO_LIMBS( nbits );
  2203. if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR ) == 0 )
  2204. {
  2205. /*
  2206. * 2^-80 error probability, number of rounds chosen per HAC, table 4.4
  2207. */
  2208. rounds = ( ( nbits >= 1300 ) ? 2 : ( nbits >= 850 ) ? 3 :
  2209. ( nbits >= 650 ) ? 4 : ( nbits >= 350 ) ? 8 :
  2210. ( nbits >= 250 ) ? 12 : ( nbits >= 150 ) ? 18 : 27 );
  2211. }
  2212. else
  2213. {
  2214. /*
  2215. * 2^-100 error probability, number of rounds computed based on HAC,
  2216. * fact 4.48
  2217. */
  2218. rounds = ( ( nbits >= 1450 ) ? 4 : ( nbits >= 1150 ) ? 5 :
  2219. ( nbits >= 1000 ) ? 6 : ( nbits >= 850 ) ? 7 :
  2220. ( nbits >= 750 ) ? 8 : ( nbits >= 500 ) ? 13 :
  2221. ( nbits >= 250 ) ? 28 : ( nbits >= 150 ) ? 40 : 51 );
  2222. }
  2223. while( 1 )
  2224. {
  2225. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
  2226. /* make sure generated number is at least (nbits-1)+0.5 bits (FIPS 186-4 §B.3.3 steps 4.4, 5.5) */
  2227. if( X->p[n-1] < CEIL_MAXUINT_DIV_SQRT2 ) continue;
  2228. k = n * biL;
  2229. if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
  2230. X->p[0] |= 1;
  2231. if( ( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH ) == 0 )
  2232. {
  2233. ret = mbedtls_mpi_is_prime_ext( X, rounds, f_rng, p_rng );
  2234. if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
  2235. goto cleanup;
  2236. }
  2237. else
  2238. {
  2239. /*
  2240. * An necessary condition for Y and X = 2Y + 1 to be prime
  2241. * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
  2242. * Make sure it is satisfied, while keeping X = 3 mod 4
  2243. */
  2244. X->p[0] |= 2;
  2245. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
  2246. if( r == 0 )
  2247. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
  2248. else if( r == 1 )
  2249. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
  2250. /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
  2251. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
  2252. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
  2253. while( 1 )
  2254. {
  2255. /*
  2256. * First, check small factors for X and Y
  2257. * before doing Miller-Rabin on any of them
  2258. */
  2259. if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
  2260. ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
  2261. ( ret = mpi_miller_rabin( X, rounds, f_rng, p_rng ) )
  2262. == 0 &&
  2263. ( ret = mpi_miller_rabin( &Y, rounds, f_rng, p_rng ) )
  2264. == 0 )
  2265. goto cleanup;
  2266. if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
  2267. goto cleanup;
  2268. /*
  2269. * Next candidates. We want to preserve Y = (X-1) / 2 and
  2270. * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
  2271. * so up Y by 6 and X by 12.
  2272. */
  2273. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
  2274. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
  2275. }
  2276. }
  2277. }
  2278. cleanup:
  2279. mbedtls_mpi_free( &Y );
  2280. return( ret );
  2281. }
  2282. #endif /* MBEDTLS_GENPRIME */
  2283. #if defined(MBEDTLS_SELF_TEST)
  2284. #define GCD_PAIR_COUNT 3
  2285. static const int gcd_pairs[GCD_PAIR_COUNT][3] =
  2286. {
  2287. { 693, 609, 21 },
  2288. { 1764, 868, 28 },
  2289. { 768454923, 542167814, 1 }
  2290. };
  2291. /*
  2292. * Checkup routine
  2293. */
  2294. int mbedtls_mpi_self_test( int verbose )
  2295. {
  2296. int ret, i;
  2297. mbedtls_mpi A, E, N, X, Y, U, V;
  2298. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
  2299. mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
  2300. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
  2301. "EFE021C2645FD1DC586E69184AF4A31E" \
  2302. "D5F53E93B5F123FA41680867BA110131" \
  2303. "944FE7952E2517337780CB0DB80E61AA" \
  2304. "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
  2305. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
  2306. "B2E7EFD37075B9F03FF989C7C5051C20" \
  2307. "34D2A323810251127E7BF8625A4F49A5" \
  2308. "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
  2309. "5B5C25763222FEFCCFC38B832366C29E" ) );
  2310. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
  2311. "0066A198186C18C10B2F5ED9B522752A" \
  2312. "9830B69916E535C8F047518A889A43A5" \
  2313. "94B6BED27A168D31D4A52F88925AA8F5" ) );
  2314. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
  2315. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  2316. "602AB7ECA597A3D6B56FF9829A5E8B85" \
  2317. "9E857EA95A03512E2BAE7391688D264A" \
  2318. "A5663B0341DB9CCFD2C4C5F421FEC814" \
  2319. "8001B72E848A38CAE1C65F78E56ABDEF" \
  2320. "E12D3C039B8A02D6BE593F0BBBDA56F1" \
  2321. "ECF677152EF804370C1A305CAF3B5BF1" \
  2322. "30879B56C61DE584A0F53A2447A51E" ) );
  2323. if( verbose != 0 )
  2324. mbedtls_printf( " MPI test #1 (mul_mpi): " );
  2325. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  2326. {
  2327. if( verbose != 0 )
  2328. mbedtls_printf( "failed\n" );
  2329. ret = 1;
  2330. goto cleanup;
  2331. }
  2332. if( verbose != 0 )
  2333. mbedtls_printf( "passed\n" );
  2334. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
  2335. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  2336. "256567336059E52CAE22925474705F39A94" ) );
  2337. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
  2338. "6613F26162223DF488E9CD48CC132C7A" \
  2339. "0AC93C701B001B092E4E5B9F73BCD27B" \
  2340. "9EE50D0657C77F374E903CDFA4C642" ) );
  2341. if( verbose != 0 )
  2342. mbedtls_printf( " MPI test #2 (div_mpi): " );
  2343. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
  2344. mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
  2345. {
  2346. if( verbose != 0 )
  2347. mbedtls_printf( "failed\n" );
  2348. ret = 1;
  2349. goto cleanup;
  2350. }
  2351. if( verbose != 0 )
  2352. mbedtls_printf( "passed\n" );
  2353. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
  2354. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  2355. "36E139AEA55215609D2816998ED020BB" \
  2356. "BD96C37890F65171D948E9BC7CBAA4D9" \
  2357. "325D24D6A3C12710F10A09FA08AB87" ) );
  2358. if( verbose != 0 )
  2359. mbedtls_printf( " MPI test #3 (exp_mod): " );
  2360. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  2361. {
  2362. if( verbose != 0 )
  2363. mbedtls_printf( "failed\n" );
  2364. ret = 1;
  2365. goto cleanup;
  2366. }
  2367. if( verbose != 0 )
  2368. mbedtls_printf( "passed\n" );
  2369. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
  2370. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  2371. "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
  2372. "C3DBA76456363A10869622EAC2DD84EC" \
  2373. "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
  2374. if( verbose != 0 )
  2375. mbedtls_printf( " MPI test #4 (inv_mod): " );
  2376. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  2377. {
  2378. if( verbose != 0 )
  2379. mbedtls_printf( "failed\n" );
  2380. ret = 1;
  2381. goto cleanup;
  2382. }
  2383. if( verbose != 0 )
  2384. mbedtls_printf( "passed\n" );
  2385. if( verbose != 0 )
  2386. mbedtls_printf( " MPI test #5 (simple gcd): " );
  2387. for( i = 0; i < GCD_PAIR_COUNT; i++ )
  2388. {
  2389. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
  2390. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
  2391. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
  2392. if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
  2393. {
  2394. if( verbose != 0 )
  2395. mbedtls_printf( "failed at %d\n", i );
  2396. ret = 1;
  2397. goto cleanup;
  2398. }
  2399. }
  2400. if( verbose != 0 )
  2401. mbedtls_printf( "passed\n" );
  2402. cleanup:
  2403. if( ret != 0 && verbose != 0 )
  2404. mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
  2405. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
  2406. mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
  2407. if( verbose != 0 )
  2408. mbedtls_printf( "\n" );
  2409. return( ret );
  2410. }
  2411. #endif /* MBEDTLS_SELF_TEST */
  2412. #endif /* MBEDTLS_BIGNUM_C */