bignum.c 57 KB

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