bignum.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450
  1. /*
  2. * Multi-precision integer library
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * The following sources were referenced in the design of this Multi-precision
  25. * Integer library:
  26. *
  27. * [1] Handbook of Applied Cryptography - 1997
  28. * Menezes, van Oorschot and Vanstone
  29. *
  30. * [2] Multi-Precision Math
  31. * Tom St Denis
  32. * https://github.com/libtom/libtommath/blob/develop/tommath.pdf
  33. *
  34. * [3] GNU Multi-Precision Arithmetic Library
  35. * https://gmplib.org/manual/index.html
  36. *
  37. */
  38. #if !defined(MBEDTLS_CONFIG_FILE)
  39. #include "mbedtls/config.h"
  40. #else
  41. #include MBEDTLS_CONFIG_FILE
  42. #endif
  43. #if defined(MBEDTLS_BIGNUM_C)
  44. #include "mbedtls/bignum.h"
  45. #include "mbedtls/bn_mul.h"
  46. #include <string.h>
  47. #if defined(MBEDTLS_PLATFORM_C)
  48. #include "mbedtls/platform.h"
  49. #else
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #define mbedtls_printf printf
  53. #define mbedtls_calloc calloc
  54. #define mbedtls_free free
  55. #endif
  56. /* Implementation that should never be optimized out by the compiler */
  57. static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n ) {
  58. volatile mbedtls_mpi_uint *p = v; while( n-- ) *p++ = 0;
  59. }
  60. #define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
  61. #define biL (ciL << 3) /* bits in limb */
  62. #define biH (ciL << 2) /* half limb size */
  63. #define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
  64. /*
  65. * Convert between bits/chars and number of limbs
  66. * Divide first in order to avoid potential overflows
  67. */
  68. #define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
  69. #define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
  70. /*
  71. * Initialize one MPI
  72. */
  73. void mbedtls_mpi_init( mbedtls_mpi *X )
  74. {
  75. if( X == NULL )
  76. return;
  77. X->s = 1;
  78. X->n = 0;
  79. X->p = NULL;
  80. }
  81. /*
  82. * Unallocate one MPI
  83. */
  84. void mbedtls_mpi_free( mbedtls_mpi *X )
  85. {
  86. if( X == NULL )
  87. return;
  88. if( X->p != NULL )
  89. {
  90. mbedtls_mpi_zeroize( X->p, X->n );
  91. mbedtls_free( X->p );
  92. }
  93. X->s = 1;
  94. X->n = 0;
  95. X->p = NULL;
  96. }
  97. /*
  98. * Enlarge to the specified number of limbs
  99. */
  100. int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
  101. {
  102. mbedtls_mpi_uint *p;
  103. if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
  104. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  105. if( X->n < nblimbs )
  106. {
  107. if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( nblimbs, ciL ) ) == NULL )
  108. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  109. if( X->p != NULL )
  110. {
  111. memcpy( p, X->p, X->n * ciL );
  112. mbedtls_mpi_zeroize( X->p, X->n );
  113. mbedtls_free( X->p );
  114. }
  115. X->n = nblimbs;
  116. X->p = p;
  117. }
  118. return( 0 );
  119. }
  120. /*
  121. * Resize down as much as possible,
  122. * while keeping at least the specified number of limbs
  123. */
  124. int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
  125. {
  126. mbedtls_mpi_uint *p;
  127. size_t i;
  128. /* Actually resize up in this case */
  129. if( X->n <= nblimbs )
  130. return( mbedtls_mpi_grow( X, nblimbs ) );
  131. for( i = X->n - 1; i > 0; i-- )
  132. if( X->p[i] != 0 )
  133. break;
  134. i++;
  135. if( i < nblimbs )
  136. i = nblimbs;
  137. if( ( p = (mbedtls_mpi_uint*)mbedtls_calloc( i, ciL ) ) == NULL )
  138. return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
  139. if( X->p != NULL )
  140. {
  141. memcpy( p, X->p, i * ciL );
  142. mbedtls_mpi_zeroize( X->p, X->n );
  143. mbedtls_free( X->p );
  144. }
  145. X->n = i;
  146. X->p = p;
  147. return( 0 );
  148. }
  149. /*
  150. * Copy the contents of Y into X
  151. */
  152. int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
  153. {
  154. int ret;
  155. size_t i;
  156. if( X == Y )
  157. return( 0 );
  158. if( Y->p == NULL )
  159. {
  160. mbedtls_mpi_free( X );
  161. return( 0 );
  162. }
  163. for( i = Y->n - 1; i > 0; i-- )
  164. if( Y->p[i] != 0 )
  165. break;
  166. i++;
  167. X->s = Y->s;
  168. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
  169. memset( X->p, 0, X->n * ciL );
  170. memcpy( X->p, Y->p, i * ciL );
  171. cleanup:
  172. return( ret );
  173. }
  174. /*
  175. * Swap the contents of X and Y
  176. */
  177. void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
  178. {
  179. mbedtls_mpi T;
  180. memcpy( &T, X, sizeof( mbedtls_mpi ) );
  181. memcpy( X, Y, sizeof( mbedtls_mpi ) );
  182. memcpy( Y, &T, sizeof( mbedtls_mpi ) );
  183. }
  184. /*
  185. * Conditionally assign X = Y, without leaking information
  186. * about whether the assignment was made or not.
  187. * (Leaking information about the respective sizes of X and Y is ok however.)
  188. */
  189. int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
  190. {
  191. int ret = 0;
  192. size_t i;
  193. /* make sure assign is 0 or 1 in a time-constant manner */
  194. assign = (assign | (unsigned char)-assign) >> 7;
  195. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
  196. X->s = X->s * ( 1 - assign ) + Y->s * assign;
  197. for( i = 0; i < Y->n; i++ )
  198. X->p[i] = X->p[i] * ( 1 - assign ) + Y->p[i] * assign;
  199. for( ; i < X->n; i++ )
  200. X->p[i] *= ( 1 - assign );
  201. cleanup:
  202. return( ret );
  203. }
  204. /*
  205. * Conditionally swap X and Y, without leaking information
  206. * about whether the swap was made or not.
  207. * Here it is not ok to simply swap the pointers, which whould lead to
  208. * different memory access patterns when X and Y are used afterwards.
  209. */
  210. int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
  211. {
  212. int ret, s;
  213. size_t i;
  214. mbedtls_mpi_uint tmp;
  215. if( X == Y )
  216. return( 0 );
  217. /* make sure swap is 0 or 1 in a time-constant manner */
  218. swap = (swap | (unsigned char)-swap) >> 7;
  219. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
  220. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
  221. s = X->s;
  222. X->s = X->s * ( 1 - swap ) + Y->s * swap;
  223. Y->s = Y->s * ( 1 - swap ) + s * swap;
  224. for( i = 0; i < X->n; i++ )
  225. {
  226. tmp = X->p[i];
  227. X->p[i] = X->p[i] * ( 1 - swap ) + Y->p[i] * swap;
  228. Y->p[i] = Y->p[i] * ( 1 - swap ) + tmp * swap;
  229. }
  230. cleanup:
  231. return( ret );
  232. }
  233. /*
  234. * Set value from integer
  235. */
  236. int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
  237. {
  238. int ret;
  239. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
  240. memset( X->p, 0, X->n * ciL );
  241. X->p[0] = ( z < 0 ) ? -z : z;
  242. X->s = ( z < 0 ) ? -1 : 1;
  243. cleanup:
  244. return( ret );
  245. }
  246. /*
  247. * Get a specific bit
  248. */
  249. int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
  250. {
  251. if( X->n * biL <= pos )
  252. return( 0 );
  253. return( ( X->p[pos / biL] >> ( pos % biL ) ) & 0x01 );
  254. }
  255. /*
  256. * Set a bit to a specific value of 0 or 1
  257. */
  258. int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
  259. {
  260. int ret = 0;
  261. size_t off = pos / biL;
  262. size_t idx = pos % biL;
  263. if( val != 0 && val != 1 )
  264. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  265. if( X->n * biL <= pos )
  266. {
  267. if( val == 0 )
  268. return( 0 );
  269. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
  270. }
  271. X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
  272. X->p[off] |= (mbedtls_mpi_uint) val << idx;
  273. cleanup:
  274. return( ret );
  275. }
  276. /*
  277. * Return the number of less significant zero-bits
  278. */
  279. size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
  280. {
  281. size_t i, j, count = 0;
  282. for( i = 0; i < X->n; i++ )
  283. for( j = 0; j < biL; j++, count++ )
  284. if( ( ( X->p[i] >> j ) & 1 ) != 0 )
  285. return( count );
  286. return( 0 );
  287. }
  288. /*
  289. * Count leading zero bits in a given integer
  290. */
  291. static size_t mbedtls_clz( const mbedtls_mpi_uint x )
  292. {
  293. size_t j;
  294. mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
  295. for( j = 0; j < biL; j++ )
  296. {
  297. if( x & mask ) break;
  298. mask >>= 1;
  299. }
  300. return j;
  301. }
  302. /*
  303. * Return the number of bits
  304. */
  305. size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X )
  306. {
  307. size_t i, j;
  308. if( X->n == 0 )
  309. return( 0 );
  310. for( i = X->n - 1; i > 0; i-- )
  311. if( X->p[i] != 0 )
  312. break;
  313. j = biL - mbedtls_clz( X->p[i] );
  314. return( ( i * biL ) + j );
  315. }
  316. /*
  317. * Return the total size in bytes
  318. */
  319. size_t mbedtls_mpi_size( const mbedtls_mpi *X )
  320. {
  321. return( ( mbedtls_mpi_bitlen( X ) + 7 ) >> 3 );
  322. }
  323. /*
  324. * Convert an ASCII character to digit value
  325. */
  326. static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
  327. {
  328. *d = 255;
  329. if( c >= 0x30 && c <= 0x39 ) *d = c - 0x30;
  330. if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
  331. if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
  332. if( *d >= (mbedtls_mpi_uint) radix )
  333. return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
  334. return( 0 );
  335. }
  336. /*
  337. * Import from an ASCII string
  338. */
  339. int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
  340. {
  341. int ret;
  342. size_t i, j, slen, n;
  343. mbedtls_mpi_uint d;
  344. mbedtls_mpi T;
  345. if( radix < 2 || radix > 16 )
  346. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  347. mbedtls_mpi_init( &T );
  348. slen = strlen( s );
  349. if( radix == 16 )
  350. {
  351. if( slen > MPI_SIZE_T_MAX >> 2 )
  352. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  353. n = BITS_TO_LIMBS( slen << 2 );
  354. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
  355. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  356. for( i = slen, j = 0; i > 0; i--, j++ )
  357. {
  358. if( i == 1 && s[i - 1] == '-' )
  359. {
  360. X->s = -1;
  361. break;
  362. }
  363. MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
  364. X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
  365. }
  366. }
  367. else
  368. {
  369. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  370. for( i = 0; i < slen; i++ )
  371. {
  372. if( i == 0 && s[i] == '-' )
  373. {
  374. X->s = -1;
  375. continue;
  376. }
  377. MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
  378. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
  379. if( X->s == 1 )
  380. {
  381. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
  382. }
  383. else
  384. {
  385. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( X, &T, d ) );
  386. }
  387. }
  388. }
  389. cleanup:
  390. mbedtls_mpi_free( &T );
  391. return( ret );
  392. }
  393. /*
  394. * Helper to write the digits high-order first
  395. */
  396. static int mpi_write_hlp( mbedtls_mpi *X, int radix, char **p )
  397. {
  398. int ret;
  399. mbedtls_mpi_uint r;
  400. if( radix < 2 || radix > 16 )
  401. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  402. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
  403. MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
  404. if( mbedtls_mpi_cmp_int( X, 0 ) != 0 )
  405. MBEDTLS_MPI_CHK( mpi_write_hlp( X, radix, p ) );
  406. if( r < 10 )
  407. *(*p)++ = (char)( r + 0x30 );
  408. else
  409. *(*p)++ = (char)( r + 0x37 );
  410. cleanup:
  411. return( ret );
  412. }
  413. /*
  414. * Export into an ASCII string
  415. */
  416. int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
  417. char *buf, size_t buflen, size_t *olen )
  418. {
  419. int ret = 0;
  420. size_t n;
  421. char *p;
  422. mbedtls_mpi T;
  423. if( radix < 2 || radix > 16 )
  424. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  425. n = mbedtls_mpi_bitlen( X );
  426. if( radix >= 4 ) n >>= 1;
  427. if( radix >= 16 ) n >>= 1;
  428. /*
  429. * Round up the buffer length to an even value to ensure that there is
  430. * enough room for hexadecimal values that can be represented in an odd
  431. * number of digits.
  432. */
  433. n += 3 + ( ( n + 1 ) & 1 );
  434. if( buflen < n )
  435. {
  436. *olen = n;
  437. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  438. }
  439. p = buf;
  440. mbedtls_mpi_init( &T );
  441. if( X->s == -1 )
  442. *p++ = '-';
  443. if( radix == 16 )
  444. {
  445. int c;
  446. size_t i, j, k;
  447. for( i = X->n, k = 0; i > 0; i-- )
  448. {
  449. for( j = ciL; j > 0; j-- )
  450. {
  451. c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
  452. if( c == 0 && k == 0 && ( i + j ) != 2 )
  453. continue;
  454. *(p++) = "0123456789ABCDEF" [c / 16];
  455. *(p++) = "0123456789ABCDEF" [c % 16];
  456. k = 1;
  457. }
  458. }
  459. }
  460. else
  461. {
  462. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
  463. if( T.s == -1 )
  464. T.s = 1;
  465. MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p ) );
  466. }
  467. *p++ = '\0';
  468. *olen = p - buf;
  469. cleanup:
  470. mbedtls_mpi_free( &T );
  471. return( ret );
  472. }
  473. #if defined(MBEDTLS_FS_IO)
  474. /*
  475. * Read X from an opened file
  476. */
  477. int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
  478. {
  479. mbedtls_mpi_uint d;
  480. size_t slen;
  481. char *p;
  482. /*
  483. * Buffer should have space for (short) label and decimal formatted MPI,
  484. * newline characters and '\0'
  485. */
  486. char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
  487. memset( s, 0, sizeof( s ) );
  488. if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
  489. return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
  490. slen = strlen( s );
  491. if( slen == sizeof( s ) - 2 )
  492. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  493. if( s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
  494. if( s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
  495. p = s + slen;
  496. while( --p >= s )
  497. if( mpi_get_digit( &d, radix, *p ) != 0 )
  498. break;
  499. return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
  500. }
  501. /*
  502. * Write X into an opened file (or stdout if fout == NULL)
  503. */
  504. int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
  505. {
  506. int ret;
  507. size_t n, slen, plen;
  508. /*
  509. * Buffer should have space for (short) label and decimal formatted MPI,
  510. * newline characters and '\0'
  511. */
  512. char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
  513. memset( s, 0, sizeof( s ) );
  514. MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) );
  515. if( p == NULL ) p = "";
  516. plen = strlen( p );
  517. slen = strlen( s );
  518. s[slen++] = '\r';
  519. s[slen++] = '\n';
  520. if( fout != NULL )
  521. {
  522. if( fwrite( p, 1, plen, fout ) != plen ||
  523. fwrite( s, 1, slen, fout ) != slen )
  524. return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
  525. }
  526. else
  527. mbedtls_printf( "%s%s", p, s );
  528. cleanup:
  529. return( ret );
  530. }
  531. #endif /* MBEDTLS_FS_IO */
  532. /*
  533. * Import X from unsigned binary data, big endian
  534. */
  535. int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
  536. {
  537. int ret;
  538. size_t i, j, n;
  539. for( n = 0; n < buflen; n++ )
  540. if( buf[n] != 0 )
  541. break;
  542. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) );
  543. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  544. for( i = buflen, j = 0; i > n; i--, j++ )
  545. X->p[j / ciL] |= ((mbedtls_mpi_uint) buf[i - 1]) << ((j % ciL) << 3);
  546. cleanup:
  547. return( ret );
  548. }
  549. /*
  550. * Export X into unsigned binary data, big endian
  551. */
  552. int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen )
  553. {
  554. size_t i, j, n;
  555. n = mbedtls_mpi_size( X );
  556. if( buflen < n )
  557. return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
  558. memset( buf, 0, buflen );
  559. for( i = buflen - 1, j = 0; n > 0; i--, j++, n-- )
  560. buf[i] = (unsigned char)( X->p[j / ciL] >> ((j % ciL) << 3) );
  561. return( 0 );
  562. }
  563. /*
  564. * Left-shift: X <<= count
  565. */
  566. int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
  567. {
  568. int ret;
  569. size_t i, v0, t1;
  570. mbedtls_mpi_uint r0 = 0, r1;
  571. v0 = count / (biL );
  572. t1 = count & (biL - 1);
  573. i = mbedtls_mpi_bitlen( X ) + count;
  574. if( X->n * biL < i )
  575. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
  576. ret = 0;
  577. /*
  578. * shift by count / limb_size
  579. */
  580. if( v0 > 0 )
  581. {
  582. for( i = X->n; i > v0; i-- )
  583. X->p[i - 1] = X->p[i - v0 - 1];
  584. for( ; i > 0; i-- )
  585. X->p[i - 1] = 0;
  586. }
  587. /*
  588. * shift by count % limb_size
  589. */
  590. if( t1 > 0 )
  591. {
  592. for( i = v0; i < X->n; i++ )
  593. {
  594. r1 = X->p[i] >> (biL - t1);
  595. X->p[i] <<= t1;
  596. X->p[i] |= r0;
  597. r0 = r1;
  598. }
  599. }
  600. cleanup:
  601. return( ret );
  602. }
  603. /*
  604. * Right-shift: X >>= count
  605. */
  606. int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
  607. {
  608. size_t i, v0, v1;
  609. mbedtls_mpi_uint r0 = 0, r1;
  610. v0 = count / biL;
  611. v1 = count & (biL - 1);
  612. if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
  613. return mbedtls_mpi_lset( X, 0 );
  614. /*
  615. * shift by count / limb_size
  616. */
  617. if( v0 > 0 )
  618. {
  619. for( i = 0; i < X->n - v0; i++ )
  620. X->p[i] = X->p[i + v0];
  621. for( ; i < X->n; i++ )
  622. X->p[i] = 0;
  623. }
  624. /*
  625. * shift by count % limb_size
  626. */
  627. if( v1 > 0 )
  628. {
  629. for( i = X->n; i > 0; i-- )
  630. {
  631. r1 = X->p[i - 1] << (biL - v1);
  632. X->p[i - 1] >>= v1;
  633. X->p[i - 1] |= r0;
  634. r0 = r1;
  635. }
  636. }
  637. return( 0 );
  638. }
  639. /*
  640. * Compare unsigned values
  641. */
  642. int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
  643. {
  644. size_t i, j;
  645. for( i = X->n; i > 0; i-- )
  646. if( X->p[i - 1] != 0 )
  647. break;
  648. for( j = Y->n; j > 0; j-- )
  649. if( Y->p[j - 1] != 0 )
  650. break;
  651. if( i == 0 && j == 0 )
  652. return( 0 );
  653. if( i > j ) return( 1 );
  654. if( j > i ) return( -1 );
  655. for( ; i > 0; i-- )
  656. {
  657. if( X->p[i - 1] > Y->p[i - 1] ) return( 1 );
  658. if( X->p[i - 1] < Y->p[i - 1] ) return( -1 );
  659. }
  660. return( 0 );
  661. }
  662. /*
  663. * Compare signed values
  664. */
  665. int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
  666. {
  667. size_t i, j;
  668. for( i = X->n; i > 0; i-- )
  669. if( X->p[i - 1] != 0 )
  670. break;
  671. for( j = Y->n; j > 0; j-- )
  672. if( Y->p[j - 1] != 0 )
  673. break;
  674. if( i == 0 && j == 0 )
  675. return( 0 );
  676. if( i > j ) return( X->s );
  677. if( j > i ) return( -Y->s );
  678. if( X->s > 0 && Y->s < 0 ) return( 1 );
  679. if( Y->s > 0 && X->s < 0 ) return( -1 );
  680. for( ; i > 0; i-- )
  681. {
  682. if( X->p[i - 1] > Y->p[i - 1] ) return( X->s );
  683. if( X->p[i - 1] < Y->p[i - 1] ) return( -X->s );
  684. }
  685. return( 0 );
  686. }
  687. /*
  688. * Compare signed values
  689. */
  690. int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
  691. {
  692. mbedtls_mpi Y;
  693. mbedtls_mpi_uint p[1];
  694. *p = ( z < 0 ) ? -z : z;
  695. Y.s = ( z < 0 ) ? -1 : 1;
  696. Y.n = 1;
  697. Y.p = p;
  698. return( mbedtls_mpi_cmp_mpi( X, &Y ) );
  699. }
  700. /*
  701. * Unsigned addition: X = |A| + |B| (HAC 14.7)
  702. */
  703. int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  704. {
  705. int ret;
  706. size_t i, j;
  707. mbedtls_mpi_uint *o, *p, c, tmp;
  708. if( X == B )
  709. {
  710. const mbedtls_mpi *T = A; A = X; B = T;
  711. }
  712. if( X != A )
  713. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
  714. /*
  715. * X should always be positive as a result of unsigned additions.
  716. */
  717. X->s = 1;
  718. for( j = B->n; j > 0; j-- )
  719. if( B->p[j - 1] != 0 )
  720. break;
  721. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
  722. o = B->p; p = X->p; c = 0;
  723. /*
  724. * tmp is used because it might happen that p == o
  725. */
  726. for( i = 0; i < j; i++, o++, p++ )
  727. {
  728. tmp= *o;
  729. *p += c; c = ( *p < c );
  730. *p += tmp; c += ( *p < tmp );
  731. }
  732. while( c != 0 )
  733. {
  734. if( i >= X->n )
  735. {
  736. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
  737. p = X->p + i;
  738. }
  739. *p += c; c = ( *p < c ); i++; p++;
  740. }
  741. cleanup:
  742. return( ret );
  743. }
  744. /*
  745. * Helper for mbedtls_mpi subtraction
  746. */
  747. static void mpi_sub_hlp( size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d )
  748. {
  749. size_t i;
  750. mbedtls_mpi_uint c, z;
  751. for( i = c = 0; i < n; i++, s++, d++ )
  752. {
  753. z = ( *d < c ); *d -= c;
  754. c = ( *d < *s ) + z; *d -= *s;
  755. }
  756. while( c != 0 )
  757. {
  758. z = ( *d < c ); *d -= c;
  759. c = z; i++; d++;
  760. }
  761. }
  762. /*
  763. * Unsigned subtraction: X = |A| - |B| (HAC 14.9)
  764. */
  765. int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  766. {
  767. mbedtls_mpi TB;
  768. int ret;
  769. size_t n;
  770. if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
  771. return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
  772. mbedtls_mpi_init( &TB );
  773. if( X == B )
  774. {
  775. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
  776. B = &TB;
  777. }
  778. if( X != A )
  779. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
  780. /*
  781. * X should always be positive as a result of unsigned subtractions.
  782. */
  783. X->s = 1;
  784. ret = 0;
  785. for( n = B->n; n > 0; n-- )
  786. if( B->p[n - 1] != 0 )
  787. break;
  788. mpi_sub_hlp( n, B->p, X->p );
  789. cleanup:
  790. mbedtls_mpi_free( &TB );
  791. return( ret );
  792. }
  793. /*
  794. * Signed addition: X = A + B
  795. */
  796. int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  797. {
  798. int ret, s = A->s;
  799. if( A->s * B->s < 0 )
  800. {
  801. if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
  802. {
  803. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
  804. X->s = s;
  805. }
  806. else
  807. {
  808. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
  809. X->s = -s;
  810. }
  811. }
  812. else
  813. {
  814. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
  815. X->s = s;
  816. }
  817. cleanup:
  818. return( ret );
  819. }
  820. /*
  821. * Signed subtraction: X = A - B
  822. */
  823. int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  824. {
  825. int ret, s = A->s;
  826. if( A->s * B->s > 0 )
  827. {
  828. if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
  829. {
  830. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
  831. X->s = s;
  832. }
  833. else
  834. {
  835. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
  836. X->s = -s;
  837. }
  838. }
  839. else
  840. {
  841. MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
  842. X->s = s;
  843. }
  844. cleanup:
  845. return( ret );
  846. }
  847. /*
  848. * Signed addition: X = A + b
  849. */
  850. int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  851. {
  852. mbedtls_mpi _B;
  853. mbedtls_mpi_uint p[1];
  854. p[0] = ( b < 0 ) ? -b : b;
  855. _B.s = ( b < 0 ) ? -1 : 1;
  856. _B.n = 1;
  857. _B.p = p;
  858. return( mbedtls_mpi_add_mpi( X, A, &_B ) );
  859. }
  860. /*
  861. * Signed subtraction: X = A - b
  862. */
  863. int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  864. {
  865. mbedtls_mpi _B;
  866. mbedtls_mpi_uint p[1];
  867. p[0] = ( b < 0 ) ? -b : b;
  868. _B.s = ( b < 0 ) ? -1 : 1;
  869. _B.n = 1;
  870. _B.p = p;
  871. return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
  872. }
  873. /*
  874. * Helper for mbedtls_mpi multiplication
  875. */
  876. static
  877. #if defined(__APPLE__) && defined(__arm__)
  878. /*
  879. * Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
  880. * appears to need this to prevent bad ARM code generation at -O3.
  881. */
  882. __attribute__ ((noinline))
  883. #endif
  884. void mpi_mul_hlp( size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b )
  885. {
  886. mbedtls_mpi_uint c = 0, t = 0;
  887. #if defined(MULADDC_HUIT)
  888. for( ; i >= 8; i -= 8 )
  889. {
  890. MULADDC_INIT
  891. MULADDC_HUIT
  892. MULADDC_STOP
  893. }
  894. for( ; i > 0; i-- )
  895. {
  896. MULADDC_INIT
  897. MULADDC_CORE
  898. MULADDC_STOP
  899. }
  900. #else /* MULADDC_HUIT */
  901. for( ; i >= 16; i -= 16 )
  902. {
  903. MULADDC_INIT
  904. MULADDC_CORE MULADDC_CORE
  905. MULADDC_CORE MULADDC_CORE
  906. MULADDC_CORE MULADDC_CORE
  907. MULADDC_CORE MULADDC_CORE
  908. MULADDC_CORE MULADDC_CORE
  909. MULADDC_CORE MULADDC_CORE
  910. MULADDC_CORE MULADDC_CORE
  911. MULADDC_CORE MULADDC_CORE
  912. MULADDC_STOP
  913. }
  914. for( ; i >= 8; i -= 8 )
  915. {
  916. MULADDC_INIT
  917. MULADDC_CORE MULADDC_CORE
  918. MULADDC_CORE MULADDC_CORE
  919. MULADDC_CORE MULADDC_CORE
  920. MULADDC_CORE MULADDC_CORE
  921. MULADDC_STOP
  922. }
  923. for( ; i > 0; i-- )
  924. {
  925. MULADDC_INIT
  926. MULADDC_CORE
  927. MULADDC_STOP
  928. }
  929. #endif /* MULADDC_HUIT */
  930. t++;
  931. do {
  932. *d += c; c = ( *d < c ); d++;
  933. }
  934. while( c != 0 );
  935. }
  936. /*
  937. * Baseline multiplication: X = A * B (HAC 14.12)
  938. */
  939. int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
  940. {
  941. int ret;
  942. size_t i, j;
  943. mbedtls_mpi TA, TB;
  944. mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
  945. if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
  946. if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
  947. for( i = A->n; i > 0; i-- )
  948. if( A->p[i - 1] != 0 )
  949. break;
  950. for( j = B->n; j > 0; j-- )
  951. if( B->p[j - 1] != 0 )
  952. break;
  953. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
  954. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
  955. for( i++; j > 0; j-- )
  956. mpi_mul_hlp( i - 1, A->p, X->p + j - 1, B->p[j - 1] );
  957. X->s = A->s * B->s;
  958. cleanup:
  959. mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
  960. return( ret );
  961. }
  962. /*
  963. * Baseline multiplication: X = A * b
  964. */
  965. int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
  966. {
  967. mbedtls_mpi _B;
  968. mbedtls_mpi_uint p[1];
  969. _B.s = 1;
  970. _B.n = 1;
  971. _B.p = p;
  972. p[0] = b;
  973. return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
  974. }
  975. /*
  976. * Unsigned integer divide - double mbedtls_mpi_uint dividend, u1/u0, and
  977. * mbedtls_mpi_uint divisor, d
  978. */
  979. static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1,
  980. mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r )
  981. {
  982. #if defined(MBEDTLS_HAVE_UDBL)
  983. mbedtls_t_udbl dividend, quotient;
  984. #else
  985. const mbedtls_mpi_uint radix = (mbedtls_mpi_uint) 1 << biH;
  986. const mbedtls_mpi_uint uint_halfword_mask = ( (mbedtls_mpi_uint) 1 << biH ) - 1;
  987. mbedtls_mpi_uint d0, d1, q0, q1, rAX, r0, quotient;
  988. mbedtls_mpi_uint u0_msw, u0_lsw;
  989. size_t s;
  990. #endif
  991. /*
  992. * Check for overflow
  993. */
  994. if( 0 == d || u1 >= d )
  995. {
  996. if (r != NULL) *r = ~0;
  997. return ( ~0 );
  998. }
  999. #if defined(MBEDTLS_HAVE_UDBL)
  1000. dividend = (mbedtls_t_udbl) u1 << biL;
  1001. dividend |= (mbedtls_t_udbl) u0;
  1002. quotient = dividend / d;
  1003. if( quotient > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
  1004. quotient = ( (mbedtls_t_udbl) 1 << biL ) - 1;
  1005. if( r != NULL )
  1006. *r = (mbedtls_mpi_uint)( dividend - (quotient * d ) );
  1007. return (mbedtls_mpi_uint) quotient;
  1008. #else
  1009. /*
  1010. * Algorithm D, Section 4.3.1 - The Art of Computer Programming
  1011. * Vol. 2 - Seminumerical Algorithms, Knuth
  1012. */
  1013. /*
  1014. * Normalize the divisor, d, and dividend, u0, u1
  1015. */
  1016. s = mbedtls_clz( d );
  1017. d = d << s;
  1018. u1 = u1 << s;
  1019. u1 |= ( u0 >> ( biL - s ) ) & ( -(mbedtls_mpi_sint)s >> ( biL - 1 ) );
  1020. u0 = u0 << s;
  1021. d1 = d >> biH;
  1022. d0 = d & uint_halfword_mask;
  1023. u0_msw = u0 >> biH;
  1024. u0_lsw = u0 & uint_halfword_mask;
  1025. /*
  1026. * Find the first quotient and remainder
  1027. */
  1028. q1 = u1 / d1;
  1029. r0 = u1 - d1 * q1;
  1030. while( q1 >= radix || ( q1 * d0 > radix * r0 + u0_msw ) )
  1031. {
  1032. q1 -= 1;
  1033. r0 += d1;
  1034. if ( r0 >= radix ) break;
  1035. }
  1036. rAX = ( u1 * radix ) + ( u0_msw - q1 * d );
  1037. q0 = rAX / d1;
  1038. r0 = rAX - q0 * d1;
  1039. while( q0 >= radix || ( q0 * d0 > radix * r0 + u0_lsw ) )
  1040. {
  1041. q0 -= 1;
  1042. r0 += d1;
  1043. if ( r0 >= radix ) break;
  1044. }
  1045. if (r != NULL)
  1046. *r = ( rAX * radix + u0_lsw - q0 * d ) >> s;
  1047. quotient = q1 * radix + q0;
  1048. return quotient;
  1049. #endif
  1050. }
  1051. /*
  1052. * Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
  1053. */
  1054. int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1055. {
  1056. int ret;
  1057. size_t i, n, t, k;
  1058. mbedtls_mpi X, Y, Z, T1, T2;
  1059. if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
  1060. return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
  1061. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  1062. mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
  1063. if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
  1064. {
  1065. if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
  1066. if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
  1067. return( 0 );
  1068. }
  1069. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
  1070. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
  1071. X.s = Y.s = 1;
  1072. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
  1073. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z, 0 ) );
  1074. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
  1075. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) );
  1076. k = mbedtls_mpi_bitlen( &Y ) % biL;
  1077. if( k < biL - 1 )
  1078. {
  1079. k = biL - 1 - k;
  1080. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
  1081. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
  1082. }
  1083. else k = 0;
  1084. n = X.n - 1;
  1085. t = Y.n - 1;
  1086. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
  1087. while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
  1088. {
  1089. Z.p[n - t]++;
  1090. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
  1091. }
  1092. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
  1093. for( i = n; i > t ; i-- )
  1094. {
  1095. if( X.p[i] >= Y.p[t] )
  1096. Z.p[i - t - 1] = ~0;
  1097. else
  1098. {
  1099. Z.p[i - t - 1] = mbedtls_int_div_int( X.p[i], X.p[i - 1],
  1100. Y.p[t], NULL);
  1101. }
  1102. Z.p[i - t - 1]++;
  1103. do
  1104. {
  1105. Z.p[i - t - 1]--;
  1106. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
  1107. T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
  1108. T1.p[1] = Y.p[t];
  1109. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
  1110. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) );
  1111. T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
  1112. T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
  1113. T2.p[2] = X.p[i];
  1114. }
  1115. while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
  1116. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
  1117. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
  1118. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
  1119. if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
  1120. {
  1121. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
  1122. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
  1123. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
  1124. Z.p[i - t - 1]--;
  1125. }
  1126. }
  1127. if( Q != NULL )
  1128. {
  1129. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
  1130. Q->s = A->s * B->s;
  1131. }
  1132. if( R != NULL )
  1133. {
  1134. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
  1135. X.s = A->s;
  1136. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
  1137. if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
  1138. R->s = 1;
  1139. }
  1140. cleanup:
  1141. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  1142. mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
  1143. return( ret );
  1144. }
  1145. /*
  1146. * Division by int: A = Q * b + R
  1147. */
  1148. int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  1149. {
  1150. mbedtls_mpi _B;
  1151. mbedtls_mpi_uint p[1];
  1152. p[0] = ( b < 0 ) ? -b : b;
  1153. _B.s = ( b < 0 ) ? -1 : 1;
  1154. _B.n = 1;
  1155. _B.p = p;
  1156. return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
  1157. }
  1158. /*
  1159. * Modulo: R = A mod B
  1160. */
  1161. int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1162. {
  1163. int ret;
  1164. if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
  1165. return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
  1166. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
  1167. while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
  1168. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
  1169. while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
  1170. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
  1171. cleanup:
  1172. return( ret );
  1173. }
  1174. /*
  1175. * Modulo: r = A mod b
  1176. */
  1177. int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
  1178. {
  1179. size_t i;
  1180. mbedtls_mpi_uint x, y, z;
  1181. if( b == 0 )
  1182. return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
  1183. if( b < 0 )
  1184. return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
  1185. /*
  1186. * handle trivial cases
  1187. */
  1188. if( b == 1 )
  1189. {
  1190. *r = 0;
  1191. return( 0 );
  1192. }
  1193. if( b == 2 )
  1194. {
  1195. *r = A->p[0] & 1;
  1196. return( 0 );
  1197. }
  1198. /*
  1199. * general case
  1200. */
  1201. for( i = A->n, y = 0; i > 0; i-- )
  1202. {
  1203. x = A->p[i - 1];
  1204. y = ( y << biH ) | ( x >> biH );
  1205. z = y / b;
  1206. y -= z * b;
  1207. x <<= biH;
  1208. y = ( y << biH ) | ( x >> biH );
  1209. z = y / b;
  1210. y -= z * b;
  1211. }
  1212. /*
  1213. * If A is negative, then the current y represents a negative value.
  1214. * Flipping it to the positive side.
  1215. */
  1216. if( A->s < 0 && y != 0 )
  1217. y = b - y;
  1218. *r = y;
  1219. return( 0 );
  1220. }
  1221. /*
  1222. * Fast Montgomery initialization (thanks to Tom St Denis)
  1223. */
  1224. static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
  1225. {
  1226. mbedtls_mpi_uint x, m0 = N->p[0];
  1227. unsigned int i;
  1228. x = m0;
  1229. x += ( ( m0 + 2 ) & 4 ) << 1;
  1230. for( i = biL; i >= 8; i /= 2 )
  1231. x *= ( 2 - ( m0 * x ) );
  1232. *mm = ~x + 1;
  1233. }
  1234. /*
  1235. * Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
  1236. */
  1237. static int mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
  1238. const mbedtls_mpi *T )
  1239. {
  1240. size_t i, n, m;
  1241. mbedtls_mpi_uint u0, u1, *d;
  1242. if( T->n < N->n + 1 || T->p == NULL )
  1243. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1244. memset( T->p, 0, T->n * ciL );
  1245. d = T->p;
  1246. n = N->n;
  1247. m = ( B->n < n ) ? B->n : n;
  1248. for( i = 0; i < n; i++ )
  1249. {
  1250. /*
  1251. * T = (T + u0*B + u1*N) / 2^biL
  1252. */
  1253. u0 = A->p[i];
  1254. u1 = ( d[0] + u0 * B->p[0] ) * mm;
  1255. mpi_mul_hlp( m, B->p, d, u0 );
  1256. mpi_mul_hlp( n, N->p, d, u1 );
  1257. *d++ = u0; d[n + 1] = 0;
  1258. }
  1259. memcpy( A->p, d, ( n + 1 ) * ciL );
  1260. if( mbedtls_mpi_cmp_abs( A, N ) >= 0 )
  1261. mpi_sub_hlp( n, N->p, A->p );
  1262. else
  1263. /* prevent timing attacks */
  1264. mpi_sub_hlp( n, A->p, T->p );
  1265. return( 0 );
  1266. }
  1267. /*
  1268. * Montgomery reduction: A = A * R^-1 mod N
  1269. */
  1270. static int mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T )
  1271. {
  1272. mbedtls_mpi_uint z = 1;
  1273. mbedtls_mpi U;
  1274. U.n = U.s = (int) z;
  1275. U.p = &z;
  1276. return( mpi_montmul( A, &U, N, mm, T ) );
  1277. }
  1278. /*
  1279. * Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
  1280. */
  1281. int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR )
  1282. {
  1283. int ret;
  1284. size_t wbits, wsize, one = 1;
  1285. size_t i, j, nblimbs;
  1286. size_t bufsize, nbits;
  1287. mbedtls_mpi_uint ei, mm, state;
  1288. mbedtls_mpi RR, T, W[ 2 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
  1289. int neg;
  1290. if( mbedtls_mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
  1291. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1292. if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
  1293. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1294. /*
  1295. * Init temps and window size
  1296. */
  1297. mpi_montg_init( &mm, N );
  1298. mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
  1299. mbedtls_mpi_init( &Apos );
  1300. memset( W, 0, sizeof( W ) );
  1301. i = mbedtls_mpi_bitlen( E );
  1302. wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
  1303. ( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
  1304. if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
  1305. wsize = MBEDTLS_MPI_WINDOW_SIZE;
  1306. j = N->n + 1;
  1307. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
  1308. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1], j ) );
  1309. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
  1310. /*
  1311. * Compensate for negative A (and correct at the end)
  1312. */
  1313. neg = ( A->s == -1 );
  1314. if( neg )
  1315. {
  1316. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
  1317. Apos.s = 1;
  1318. A = &Apos;
  1319. }
  1320. /*
  1321. * If 1st call, pre-compute R^2 mod N
  1322. */
  1323. if( _RR == NULL || _RR->p == NULL )
  1324. {
  1325. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
  1326. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
  1327. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
  1328. if( _RR != NULL )
  1329. memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
  1330. }
  1331. else
  1332. memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
  1333. /*
  1334. * W[1] = A * R^2 * R^-1 mod N = A * R mod N
  1335. */
  1336. if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
  1337. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
  1338. else
  1339. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
  1340. MBEDTLS_MPI_CHK( mpi_montmul( &W[1], &RR, N, mm, &T ) );
  1341. /*
  1342. * X = R^2 * R^-1 mod N = R mod N
  1343. */
  1344. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
  1345. MBEDTLS_MPI_CHK( mpi_montred( X, N, mm, &T ) );
  1346. if( wsize > 1 )
  1347. {
  1348. /*
  1349. * W[1 << (wsize - 1)] = W[1] ^ (wsize - 1)
  1350. */
  1351. j = one << ( wsize - 1 );
  1352. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
  1353. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1] ) );
  1354. for( i = 0; i < wsize - 1; i++ )
  1355. MBEDTLS_MPI_CHK( mpi_montmul( &W[j], &W[j], N, mm, &T ) );
  1356. /*
  1357. * W[i] = W[i - 1] * W[1]
  1358. */
  1359. for( i = j + 1; i < ( one << wsize ); i++ )
  1360. {
  1361. MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
  1362. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
  1363. MBEDTLS_MPI_CHK( mpi_montmul( &W[i], &W[1], N, mm, &T ) );
  1364. }
  1365. }
  1366. nblimbs = E->n;
  1367. bufsize = 0;
  1368. nbits = 0;
  1369. wbits = 0;
  1370. state = 0;
  1371. while( 1 )
  1372. {
  1373. if( bufsize == 0 )
  1374. {
  1375. if( nblimbs == 0 )
  1376. break;
  1377. nblimbs--;
  1378. bufsize = sizeof( mbedtls_mpi_uint ) << 3;
  1379. }
  1380. bufsize--;
  1381. ei = (E->p[nblimbs] >> bufsize) & 1;
  1382. /*
  1383. * skip leading 0s
  1384. */
  1385. if( ei == 0 && state == 0 )
  1386. continue;
  1387. if( ei == 0 && state == 1 )
  1388. {
  1389. /*
  1390. * out of window, square X
  1391. */
  1392. MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
  1393. continue;
  1394. }
  1395. /*
  1396. * add ei to current window
  1397. */
  1398. state = 2;
  1399. nbits++;
  1400. wbits |= ( ei << ( wsize - nbits ) );
  1401. if( nbits == wsize )
  1402. {
  1403. /*
  1404. * X = X^wsize R^-1 mod N
  1405. */
  1406. for( i = 0; i < wsize; i++ )
  1407. MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
  1408. /*
  1409. * X = X * W[wbits] R^-1 mod N
  1410. */
  1411. MBEDTLS_MPI_CHK( mpi_montmul( X, &W[wbits], N, mm, &T ) );
  1412. state--;
  1413. nbits = 0;
  1414. wbits = 0;
  1415. }
  1416. }
  1417. /*
  1418. * process the remaining bits
  1419. */
  1420. for( i = 0; i < nbits; i++ )
  1421. {
  1422. MBEDTLS_MPI_CHK( mpi_montmul( X, X, N, mm, &T ) );
  1423. wbits <<= 1;
  1424. if( ( wbits & ( one << wsize ) ) != 0 )
  1425. MBEDTLS_MPI_CHK( mpi_montmul( X, &W[1], N, mm, &T ) );
  1426. }
  1427. /*
  1428. * X = A^E * R * R^-1 mod N = A^E mod N
  1429. */
  1430. MBEDTLS_MPI_CHK( mpi_montred( X, N, mm, &T ) );
  1431. if( neg )
  1432. {
  1433. X->s = -1;
  1434. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
  1435. }
  1436. cleanup:
  1437. for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
  1438. mbedtls_mpi_free( &W[i] );
  1439. mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
  1440. if( _RR == NULL || _RR->p == NULL )
  1441. mbedtls_mpi_free( &RR );
  1442. return( ret );
  1443. }
  1444. /*
  1445. * Greatest common divisor: G = gcd(A, B) (HAC 14.54)
  1446. */
  1447. int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
  1448. {
  1449. int ret;
  1450. size_t lz, lzt;
  1451. mbedtls_mpi TG, TA, TB;
  1452. mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
  1453. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
  1454. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
  1455. lz = mbedtls_mpi_lsb( &TA );
  1456. lzt = mbedtls_mpi_lsb( &TB );
  1457. if( lzt < lz )
  1458. lz = lzt;
  1459. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
  1460. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
  1461. TA.s = TB.s = 1;
  1462. while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
  1463. {
  1464. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
  1465. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
  1466. if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
  1467. {
  1468. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
  1469. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
  1470. }
  1471. else
  1472. {
  1473. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
  1474. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
  1475. }
  1476. }
  1477. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
  1478. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
  1479. cleanup:
  1480. mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
  1481. return( ret );
  1482. }
  1483. /*
  1484. * Fill X with size bytes of random.
  1485. *
  1486. * Use a temporary bytes representation to make sure the result is the same
  1487. * regardless of the platform endianness (useful when f_rng is actually
  1488. * deterministic, eg for tests).
  1489. */
  1490. int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
  1491. int (*f_rng)(void *, unsigned char *, size_t),
  1492. void *p_rng )
  1493. {
  1494. int ret;
  1495. unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
  1496. if( size > MBEDTLS_MPI_MAX_SIZE )
  1497. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1498. MBEDTLS_MPI_CHK( f_rng( p_rng, buf, size ) );
  1499. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( X, buf, size ) );
  1500. cleanup:
  1501. return( ret );
  1502. }
  1503. /*
  1504. * Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
  1505. */
  1506. int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
  1507. {
  1508. int ret;
  1509. mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
  1510. if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 )
  1511. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1512. mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
  1513. mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
  1514. mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
  1515. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
  1516. if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
  1517. {
  1518. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  1519. goto cleanup;
  1520. }
  1521. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
  1522. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
  1523. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
  1524. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
  1525. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
  1526. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
  1527. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
  1528. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
  1529. do
  1530. {
  1531. while( ( TU.p[0] & 1 ) == 0 )
  1532. {
  1533. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
  1534. if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
  1535. {
  1536. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
  1537. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
  1538. }
  1539. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
  1540. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
  1541. }
  1542. while( ( TV.p[0] & 1 ) == 0 )
  1543. {
  1544. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
  1545. if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
  1546. {
  1547. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
  1548. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
  1549. }
  1550. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
  1551. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
  1552. }
  1553. if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
  1554. {
  1555. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
  1556. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
  1557. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
  1558. }
  1559. else
  1560. {
  1561. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
  1562. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
  1563. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
  1564. }
  1565. }
  1566. while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
  1567. while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
  1568. MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
  1569. while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
  1570. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
  1571. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
  1572. cleanup:
  1573. mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
  1574. mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
  1575. mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
  1576. return( ret );
  1577. }
  1578. #if defined(MBEDTLS_GENPRIME)
  1579. static const int small_prime[] =
  1580. {
  1581. 3, 5, 7, 11, 13, 17, 19, 23,
  1582. 29, 31, 37, 41, 43, 47, 53, 59,
  1583. 61, 67, 71, 73, 79, 83, 89, 97,
  1584. 101, 103, 107, 109, 113, 127, 131, 137,
  1585. 139, 149, 151, 157, 163, 167, 173, 179,
  1586. 181, 191, 193, 197, 199, 211, 223, 227,
  1587. 229, 233, 239, 241, 251, 257, 263, 269,
  1588. 271, 277, 281, 283, 293, 307, 311, 313,
  1589. 317, 331, 337, 347, 349, 353, 359, 367,
  1590. 373, 379, 383, 389, 397, 401, 409, 419,
  1591. 421, 431, 433, 439, 443, 449, 457, 461,
  1592. 463, 467, 479, 487, 491, 499, 503, 509,
  1593. 521, 523, 541, 547, 557, 563, 569, 571,
  1594. 577, 587, 593, 599, 601, 607, 613, 617,
  1595. 619, 631, 641, 643, 647, 653, 659, 661,
  1596. 673, 677, 683, 691, 701, 709, 719, 727,
  1597. 733, 739, 743, 751, 757, 761, 769, 773,
  1598. 787, 797, 809, 811, 821, 823, 827, 829,
  1599. 839, 853, 857, 859, 863, 877, 881, 883,
  1600. 887, 907, 911, 919, 929, 937, 941, 947,
  1601. 953, 967, 971, 977, 983, 991, 997, -103
  1602. };
  1603. /*
  1604. * Small divisors test (X must be positive)
  1605. *
  1606. * Return values:
  1607. * 0: no small factor (possible prime, more tests needed)
  1608. * 1: certain prime
  1609. * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
  1610. * other negative: error
  1611. */
  1612. static int mpi_check_small_factors( const mbedtls_mpi *X )
  1613. {
  1614. int ret = 0;
  1615. size_t i;
  1616. mbedtls_mpi_uint r;
  1617. if( ( X->p[0] & 1 ) == 0 )
  1618. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  1619. for( i = 0; small_prime[i] > 0; i++ )
  1620. {
  1621. if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
  1622. return( 1 );
  1623. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
  1624. if( r == 0 )
  1625. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  1626. }
  1627. cleanup:
  1628. return( ret );
  1629. }
  1630. /*
  1631. * Miller-Rabin pseudo-primality test (HAC 4.24)
  1632. */
  1633. static int mpi_miller_rabin( const mbedtls_mpi *X,
  1634. int (*f_rng)(void *, unsigned char *, size_t),
  1635. void *p_rng )
  1636. {
  1637. int ret, count;
  1638. size_t i, j, k, n, s;
  1639. mbedtls_mpi W, R, T, A, RR;
  1640. mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
  1641. mbedtls_mpi_init( &RR );
  1642. /*
  1643. * W = |X| - 1
  1644. * R = W >> lsb( W )
  1645. */
  1646. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
  1647. s = mbedtls_mpi_lsb( &W );
  1648. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
  1649. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
  1650. i = mbedtls_mpi_bitlen( X );
  1651. /*
  1652. * HAC, table 4.4
  1653. */
  1654. n = ( ( i >= 1300 ) ? 2 : ( i >= 850 ) ? 3 :
  1655. ( i >= 650 ) ? 4 : ( i >= 350 ) ? 8 :
  1656. ( i >= 250 ) ? 12 : ( i >= 150 ) ? 18 : 27 );
  1657. for( i = 0; i < n; i++ )
  1658. {
  1659. /*
  1660. * pick a random A, 1 < A < |X| - 1
  1661. */
  1662. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
  1663. if( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 )
  1664. {
  1665. j = mbedtls_mpi_bitlen( &A ) - mbedtls_mpi_bitlen( &W );
  1666. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j + 1 ) );
  1667. }
  1668. A.p[0] |= 3;
  1669. count = 0;
  1670. do {
  1671. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
  1672. j = mbedtls_mpi_bitlen( &A );
  1673. k = mbedtls_mpi_bitlen( &W );
  1674. if (j > k) {
  1675. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j - k ) );
  1676. }
  1677. if (count++ > 30) {
  1678. return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  1679. }
  1680. } while ( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 ||
  1681. mbedtls_mpi_cmp_int( &A, 1 ) <= 0 );
  1682. /*
  1683. * A = A^R mod |X|
  1684. */
  1685. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
  1686. if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
  1687. mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  1688. continue;
  1689. j = 1;
  1690. while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
  1691. {
  1692. /*
  1693. * A = A * A mod |X|
  1694. */
  1695. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
  1696. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X ) );
  1697. if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  1698. break;
  1699. j++;
  1700. }
  1701. /*
  1702. * not prime if A != |X| - 1 or A == 1
  1703. */
  1704. if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
  1705. mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
  1706. {
  1707. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  1708. break;
  1709. }
  1710. }
  1711. cleanup:
  1712. mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
  1713. mbedtls_mpi_free( &RR );
  1714. return( ret );
  1715. }
  1716. /*
  1717. * Pseudo-primality test: small factors, then Miller-Rabin
  1718. */
  1719. int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
  1720. int (*f_rng)(void *, unsigned char *, size_t),
  1721. void *p_rng )
  1722. {
  1723. int ret;
  1724. mbedtls_mpi XX;
  1725. XX.s = 1;
  1726. XX.n = X->n;
  1727. XX.p = X->p;
  1728. if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
  1729. mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
  1730. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  1731. if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
  1732. return( 0 );
  1733. if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
  1734. {
  1735. if( ret == 1 )
  1736. return( 0 );
  1737. return( ret );
  1738. }
  1739. return( mpi_miller_rabin( &XX, f_rng, p_rng ) );
  1740. }
  1741. /*
  1742. * Prime number generation
  1743. */
  1744. int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
  1745. int (*f_rng)(void *, unsigned char *, size_t),
  1746. void *p_rng )
  1747. {
  1748. int ret;
  1749. size_t k, n;
  1750. mbedtls_mpi_uint r;
  1751. mbedtls_mpi Y;
  1752. if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
  1753. return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
  1754. mbedtls_mpi_init( &Y );
  1755. n = BITS_TO_LIMBS( nbits );
  1756. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
  1757. k = mbedtls_mpi_bitlen( X );
  1758. if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits + 1 ) );
  1759. mbedtls_mpi_set_bit( X, nbits-1, 1 );
  1760. X->p[0] |= 1;
  1761. if( dh_flag == 0 )
  1762. {
  1763. while( ( ret = mbedtls_mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
  1764. {
  1765. if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
  1766. goto cleanup;
  1767. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 2 ) );
  1768. }
  1769. }
  1770. else
  1771. {
  1772. /*
  1773. * An necessary condition for Y and X = 2Y + 1 to be prime
  1774. * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
  1775. * Make sure it is satisfied, while keeping X = 3 mod 4
  1776. */
  1777. X->p[0] |= 2;
  1778. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
  1779. if( r == 0 )
  1780. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
  1781. else if( r == 1 )
  1782. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
  1783. /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
  1784. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
  1785. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
  1786. while( 1 )
  1787. {
  1788. /*
  1789. * First, check small factors for X and Y
  1790. * before doing Miller-Rabin on any of them
  1791. */
  1792. if( ( ret = mpi_check_small_factors( X ) ) == 0 &&
  1793. ( ret = mpi_check_small_factors( &Y ) ) == 0 &&
  1794. ( ret = mpi_miller_rabin( X, f_rng, p_rng ) ) == 0 &&
  1795. ( ret = mpi_miller_rabin( &Y, f_rng, p_rng ) ) == 0 )
  1796. {
  1797. break;
  1798. }
  1799. if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
  1800. goto cleanup;
  1801. /*
  1802. * Next candidates. We want to preserve Y = (X-1) / 2 and
  1803. * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
  1804. * so up Y by 6 and X by 12.
  1805. */
  1806. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 12 ) );
  1807. MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6 ) );
  1808. }
  1809. }
  1810. cleanup:
  1811. mbedtls_mpi_free( &Y );
  1812. return( ret );
  1813. }
  1814. #endif /* MBEDTLS_GENPRIME */
  1815. #if defined(MBEDTLS_SELF_TEST)
  1816. #define GCD_PAIR_COUNT 3
  1817. static const int gcd_pairs[GCD_PAIR_COUNT][3] =
  1818. {
  1819. { 693, 609, 21 },
  1820. { 1764, 868, 28 },
  1821. { 768454923, 542167814, 1 }
  1822. };
  1823. /*
  1824. * Checkup routine
  1825. */
  1826. int mbedtls_mpi_self_test( int verbose )
  1827. {
  1828. int ret, i;
  1829. mbedtls_mpi A, E, N, X, Y, U, V;
  1830. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
  1831. mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
  1832. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
  1833. "EFE021C2645FD1DC586E69184AF4A31E" \
  1834. "D5F53E93B5F123FA41680867BA110131" \
  1835. "944FE7952E2517337780CB0DB80E61AA" \
  1836. "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
  1837. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
  1838. "B2E7EFD37075B9F03FF989C7C5051C20" \
  1839. "34D2A323810251127E7BF8625A4F49A5" \
  1840. "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
  1841. "5B5C25763222FEFCCFC38B832366C29E" ) );
  1842. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
  1843. "0066A198186C18C10B2F5ED9B522752A" \
  1844. "9830B69916E535C8F047518A889A43A5" \
  1845. "94B6BED27A168D31D4A52F88925AA8F5" ) );
  1846. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
  1847. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  1848. "602AB7ECA597A3D6B56FF9829A5E8B85" \
  1849. "9E857EA95A03512E2BAE7391688D264A" \
  1850. "A5663B0341DB9CCFD2C4C5F421FEC814" \
  1851. "8001B72E848A38CAE1C65F78E56ABDEF" \
  1852. "E12D3C039B8A02D6BE593F0BBBDA56F1" \
  1853. "ECF677152EF804370C1A305CAF3B5BF1" \
  1854. "30879B56C61DE584A0F53A2447A51E" ) );
  1855. if( verbose != 0 )
  1856. mbedtls_printf( " MPI test #1 (mul_mpi): " );
  1857. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  1858. {
  1859. if( verbose != 0 )
  1860. mbedtls_printf( "failed\n" );
  1861. ret = 1;
  1862. goto cleanup;
  1863. }
  1864. if( verbose != 0 )
  1865. mbedtls_printf( "passed\n" );
  1866. MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
  1867. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  1868. "256567336059E52CAE22925474705F39A94" ) );
  1869. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
  1870. "6613F26162223DF488E9CD48CC132C7A" \
  1871. "0AC93C701B001B092E4E5B9F73BCD27B" \
  1872. "9EE50D0657C77F374E903CDFA4C642" ) );
  1873. if( verbose != 0 )
  1874. mbedtls_printf( " MPI test #2 (div_mpi): " );
  1875. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
  1876. mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
  1877. {
  1878. if( verbose != 0 )
  1879. mbedtls_printf( "failed\n" );
  1880. ret = 1;
  1881. goto cleanup;
  1882. }
  1883. if( verbose != 0 )
  1884. mbedtls_printf( "passed\n" );
  1885. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
  1886. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  1887. "36E139AEA55215609D2816998ED020BB" \
  1888. "BD96C37890F65171D948E9BC7CBAA4D9" \
  1889. "325D24D6A3C12710F10A09FA08AB87" ) );
  1890. if( verbose != 0 )
  1891. mbedtls_printf( " MPI test #3 (exp_mod): " );
  1892. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  1893. {
  1894. if( verbose != 0 )
  1895. mbedtls_printf( "failed\n" );
  1896. ret = 1;
  1897. goto cleanup;
  1898. }
  1899. if( verbose != 0 )
  1900. mbedtls_printf( "passed\n" );
  1901. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
  1902. MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
  1903. "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
  1904. "C3DBA76456363A10869622EAC2DD84EC" \
  1905. "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
  1906. if( verbose != 0 )
  1907. mbedtls_printf( " MPI test #4 (inv_mod): " );
  1908. if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
  1909. {
  1910. if( verbose != 0 )
  1911. mbedtls_printf( "failed\n" );
  1912. ret = 1;
  1913. goto cleanup;
  1914. }
  1915. if( verbose != 0 )
  1916. mbedtls_printf( "passed\n" );
  1917. if( verbose != 0 )
  1918. mbedtls_printf( " MPI test #5 (simple gcd): " );
  1919. for( i = 0; i < GCD_PAIR_COUNT; i++ )
  1920. {
  1921. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
  1922. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
  1923. MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
  1924. if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
  1925. {
  1926. if( verbose != 0 )
  1927. mbedtls_printf( "failed at %d\n", i );
  1928. ret = 1;
  1929. goto cleanup;
  1930. }
  1931. }
  1932. if( verbose != 0 )
  1933. mbedtls_printf( "passed\n" );
  1934. cleanup:
  1935. if( ret != 0 && verbose != 0 )
  1936. mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
  1937. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
  1938. mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
  1939. if( verbose != 0 )
  1940. mbedtls_printf( "\n" );
  1941. return( ret );
  1942. }
  1943. #endif /* MBEDTLS_SELF_TEST */
  1944. #endif /* MBEDTLS_BIGNUM_C */