bignum.c 76 KB

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