ssl_bn.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  1. /* ssl_bn.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL 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. * wolfSSL 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
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/internal.h>
  26. #ifndef WC_NO_RNG
  27. #include <wolfssl/wolfcrypt/random.h>
  28. #endif
  29. #if !defined(WOLFSSL_SSL_BN_INCLUDED)
  30. #ifndef WOLFSSL_IGNORE_FILE_WARN
  31. #warning ssl_bn.c does not need to be compiled separately from ssl.c
  32. #endif
  33. #else
  34. /* Check on validity of big number.
  35. *
  36. * Used for parameter validation.
  37. *
  38. * @param [in] bn Big number.
  39. * @return 1 when bn is not NULL and internal representation is not NULL.
  40. * @return 0 otherwise.
  41. */
  42. #define BN_IS_NULL(bn) (((bn) == NULL) || ((bn)->internal == NULL))
  43. /*******************************************************************************
  44. * Constructor/Destructor/Initializer APIs
  45. ******************************************************************************/
  46. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  47. /* Set big number to be negative.
  48. *
  49. * @param [in, out] bn Big number to make negative.
  50. * @param [in] neg Whether number is negative.
  51. * @return 1 on success.
  52. * @return -1 when bn or internal representation of bn is NULL.
  53. */
  54. static int wolfssl_bn_set_neg(WOLFSSL_BIGNUM* bn, int neg)
  55. {
  56. int ret = 1;
  57. if (BN_IS_NULL(bn)) {
  58. WOLFSSL_MSG("bn NULL error");
  59. ret = -1;
  60. }
  61. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_INT_NEGATIVE)
  62. else if (neg) {
  63. mp_setneg((mp_int*)bn->internal);
  64. }
  65. else {
  66. ((mp_int*)bn->internal)->sign = MP_ZPOS;
  67. }
  68. #endif
  69. return ret;
  70. }
  71. #endif /* OPENSSL_EXTRA && !NO_ASN */
  72. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  73. /* Get the internal representation value into an MP integer.
  74. *
  75. * When calling wolfssl_bn_get_value, mpi should be cleared by caller if no
  76. * longer used. ie mp_free(mpi). This is to free data when fastmath is
  77. * disabled since a copy of mpi is made by this function and placed into bn.
  78. *
  79. * @param [in] bn Big number to copy value from.
  80. * @param [in, out] mpi MP integer to copy into.
  81. * @return 1 on success.
  82. * @return -1 when bn or internal representation of bn is NULL.
  83. * @return -1 when mpi is NULL.
  84. * @return -1 when copy fails.
  85. */
  86. int wolfssl_bn_get_value(WOLFSSL_BIGNUM* bn, mp_int* mpi)
  87. {
  88. int ret = 1;
  89. WOLFSSL_MSG("Entering wolfssl_bn_get_value_mp");
  90. /* Validate parameters. */
  91. if (BN_IS_NULL(bn)) {
  92. WOLFSSL_MSG("bn NULL error");
  93. ret = -1;
  94. }
  95. else if (mpi == NULL) {
  96. WOLFSSL_MSG("mpi NULL error");
  97. ret = -1;
  98. }
  99. /* Copy the internal representation into MP integer. */
  100. if ((ret == 1) && mp_copy((mp_int*)bn->internal, mpi) != MP_OKAY) {
  101. WOLFSSL_MSG("mp_copy error");
  102. ret = -1;
  103. }
  104. return ret;
  105. }
  106. /* Set big number internal representation to value in mpi.
  107. *
  108. * Will create a new big number if bn points to NULL.
  109. *
  110. * When calling wolfssl_bn_set_value, mpi should be cleared by caller if no
  111. * longer used. ie mp_free(mpi). This is to free data when fastmath is
  112. * disabled since a copy of mpi is made by this function and placed into bn.
  113. *
  114. * @param [in, out] bn Pointer to big number to have value.
  115. * @param [in] mpi MP integer with value to set.
  116. * @return 1 on success.
  117. * @return -1 when mpi or bn is NULL.
  118. * @return -1 when creating a new big number fails.
  119. * @return -1 when copying MP integer fails.
  120. */
  121. int wolfssl_bn_set_value(WOLFSSL_BIGNUM** bn, mp_int* mpi)
  122. {
  123. int ret = 1;
  124. WOLFSSL_BIGNUM* a = NULL;
  125. #ifdef WOLFSSL_DEBUG_OPENSSL
  126. WOLFSSL_ENTER("wolfssl_bn_set_value");
  127. #endif
  128. /* Validate parameters. */
  129. if ((bn == NULL) || (mpi == NULL)) {
  130. WOLFSSL_MSG("mpi or bn NULL error");
  131. ret = -1;
  132. }
  133. /* Allocate a new big number if one not passed in. */
  134. if ((ret == 1) && (*bn == NULL)) {
  135. a = wolfSSL_BN_new();
  136. if (a == NULL) {
  137. WOLFSSL_MSG("wolfssl_bn_set_value alloc failed");
  138. ret = -1;
  139. }
  140. *bn = a;
  141. }
  142. /* Copy MP integer value into internal representation of big number. */
  143. if ((ret == 1) && (mp_copy(mpi, (mp_int*)((*bn)->internal)) != MP_OKAY)) {
  144. WOLFSSL_MSG("mp_copy error");
  145. ret = -1;
  146. }
  147. /* Dispose of any allocated big number on error. */
  148. if ((ret == -1) && (a != NULL)) {
  149. BN_free(a);
  150. *bn = NULL;
  151. }
  152. return ret;
  153. }
  154. /* Initialize a big number.
  155. *
  156. * Assumes bn is not NULL.
  157. *
  158. * @param [in, out] bn Big number to initialize.
  159. */
  160. static void wolfssl_bn_init(WOLFSSL_BIGNUM* bn)
  161. {
  162. /* Clear fields of big number. */
  163. XMEMSET(bn, 0, sizeof(WOLFSSL_BIGNUM));
  164. /* Initialization only fails when passed NULL. */
  165. (void)mp_init(&bn->mpi);
  166. /* Set an internal representation. */
  167. bn->internal = &bn->mpi;
  168. }
  169. /* Create a new big number.
  170. *
  171. * @return An allocated and initialized big number on success.
  172. * @return NULL on failure.
  173. */
  174. WOLFSSL_BIGNUM* wolfSSL_BN_new(void)
  175. {
  176. WOLFSSL_BIGNUM* bn = NULL;
  177. #ifdef WOLFSSL_DEBUG_OPENSSL
  178. WOLFSSL_ENTER("wolfSSL_BN_new");
  179. #endif
  180. /* Allocate memory for big number. */
  181. bn = (WOLFSSL_BIGNUM*)XMALLOC(sizeof(WOLFSSL_BIGNUM), NULL,
  182. DYNAMIC_TYPE_BIGINT);
  183. if (bn == NULL) {
  184. WOLFSSL_MSG("wolfSSL_BN_new malloc WOLFSSL_BIGNUM failure");
  185. }
  186. else {
  187. /* Initialize newly allocated object. */
  188. wolfssl_bn_init(bn);
  189. }
  190. return bn;
  191. }
  192. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  193. /* Initialize a big number.
  194. *
  195. * Call this instead of wolfSSL_BN_new() and wolfSSL_BN_free().
  196. *
  197. * Do not call this API after wolfSSL_BN_new() or wolfSSL_BN_init().
  198. *
  199. * @param [in, out] bn Big number to initialize.
  200. */
  201. void wolfSSL_BN_init(WOLFSSL_BIGNUM* bn)
  202. {
  203. #ifdef WOLFSSL_DEBUG_OPENSSL
  204. WOLFSSL_ENTER("wolfSSL_BN_init");
  205. #endif
  206. /* Validate parameter. */
  207. if (bn != NULL) {
  208. /* Initialize big number object. */
  209. wolfssl_bn_init(bn);
  210. }
  211. }
  212. #endif
  213. /* Dispose of big number.
  214. *
  215. * bn is unusable after this call.
  216. *
  217. * @param [in, out] bn Big number to free.
  218. */
  219. void wolfSSL_BN_free(WOLFSSL_BIGNUM* bn)
  220. {
  221. #ifdef WOLFSSL_DEBUG_OPENSSL
  222. WOLFSSL_ENTER("wolfSSL_BN_free");
  223. #endif
  224. /* Validate parameter. */
  225. if (bn != NULL) {
  226. /* Cleanup any internal representation. */
  227. if (bn->internal != NULL) {
  228. /* Free MP integer. */
  229. mp_free(&bn->mpi);
  230. }
  231. /* Dispose of big number object. */
  232. XFREE(bn, NULL, DYNAMIC_TYPE_BIGINT);
  233. /* bn = NULL, don't try to access or double free it */
  234. }
  235. }
  236. /* Zeroize and dispose of big number.
  237. *
  238. * bn is unusable after this call.
  239. *
  240. * @param [in, out] bn Big number to clear and free.
  241. */
  242. void wolfSSL_BN_clear_free(WOLFSSL_BIGNUM* bn)
  243. {
  244. #ifdef WOLFSSL_DEBUG_OPENSSL
  245. WOLFSSL_ENTER("wolfSSL_BN_clear_free");
  246. #endif
  247. /* Validate parameter. */
  248. if (bn != NULL) {
  249. /* Check for internal representation. */
  250. if (bn->internal != NULL) {
  251. /* Zeroize MP integer. */
  252. mp_forcezero((mp_int*)bn->internal);
  253. }
  254. /* Dispose of big number. */
  255. wolfSSL_BN_free(bn);
  256. }
  257. }
  258. /* Zeroize big number.
  259. *
  260. * @param [in, out] bn Big number to clear.
  261. */
  262. void wolfSSL_BN_clear(WOLFSSL_BIGNUM* bn)
  263. {
  264. #ifdef WOLFSSL_DEBUG_OPENSSL
  265. WOLFSSL_ENTER("wolfSSL_BN_clear");
  266. #endif
  267. /* Validate parameter. */
  268. if (!BN_IS_NULL(bn)) {
  269. /* Zeroize MP integer. */
  270. mp_forcezero((mp_int*)bn->internal);
  271. }
  272. }
  273. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  274. #ifdef OPENSSL_EXTRA
  275. static WOLFSSL_BIGNUM* bn_one = NULL;
  276. /* Return a big number with the value of one.
  277. *
  278. * @return A big number with the value one on success.
  279. * @return NULL on failure.
  280. */
  281. const WOLFSSL_BIGNUM* wolfSSL_BN_value_one(void)
  282. {
  283. WOLFSSL_BIGNUM* one;
  284. WOLFSSL_ENTER("wolfSSL_BN_value_one");
  285. /* Get the global object. */
  286. one = bn_one;
  287. /* Create a new big number if global not set. */
  288. if ((one == NULL) && ((one = wolfSSL_BN_new()) != NULL)) {
  289. /* Set internal representation to have a value of 1. */
  290. if (mp_set_int((mp_int*)one->internal, 1) != MP_OKAY) {
  291. /* Dispose of big number on error. */
  292. wolfSSL_BN_free(one);
  293. one = NULL;
  294. }
  295. else
  296. #ifndef SINGLE_THREADED
  297. /* Ensure global has not been set by another thread. */
  298. if (bn_one == NULL)
  299. #endif
  300. {
  301. /* Set this big number as the global. */
  302. bn_one = one;
  303. }
  304. #ifndef SINGLE_THREADED
  305. /* Check if another thread has set the global. */
  306. if (bn_one != one) {
  307. /* Dispose of this big number and return the global. */
  308. wolfSSL_BN_free(one);
  309. one = bn_one;
  310. }
  311. #endif
  312. }
  313. return one;
  314. }
  315. static void wolfSSL_BN_free_one(void) {
  316. wolfSSL_BN_free(bn_one);
  317. bn_one = NULL;
  318. }
  319. /* Create a new big number with the same value as the one passed in.
  320. *
  321. * @param [in] bn Big number to duplicate.
  322. * @return Big number on success.
  323. * @return NULL when bn or internal representation of bn is NULL.
  324. * @return NULL when creating a new big number fails.
  325. * @return NULL when copying the internal representation fails.
  326. */
  327. WOLFSSL_BIGNUM* wolfSSL_BN_dup(const WOLFSSL_BIGNUM* bn)
  328. {
  329. int err = 0;
  330. WOLFSSL_BIGNUM* ret = NULL;
  331. WOLFSSL_ENTER("wolfSSL_BN_dup");
  332. /* Validate parameter. */
  333. if (BN_IS_NULL(bn)) {
  334. WOLFSSL_MSG("bn NULL error");
  335. err = 1;
  336. }
  337. /* Create a new big number to return. */
  338. if ((!err) && ((ret = wolfSSL_BN_new()) == NULL)) {
  339. WOLFSSL_MSG("bn new error");
  340. err = 1;
  341. }
  342. if (!err) {
  343. err = (wolfSSL_BN_copy(ret, bn) == NULL);
  344. }
  345. if (err) {
  346. /* Dispose of dynamically allocated data. */
  347. wolfSSL_BN_free(ret);
  348. ret = NULL;
  349. }
  350. return ret;
  351. }
  352. /* Copy value from bn into another r.
  353. *
  354. * @param [in, out] r Big number to copy into.
  355. * @param [in] bn Big number to copy from.
  356. * @return Big number copied into on success.
  357. * @return NULL when r or bn is NULL.
  358. * @return NULL when copying fails.
  359. */
  360. WOLFSSL_BIGNUM* wolfSSL_BN_copy(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* bn)
  361. {
  362. WOLFSSL_ENTER("wolfSSL_BN_copy");
  363. /* Validate parameters. */
  364. if (BN_IS_NULL(r) || BN_IS_NULL(bn)) {
  365. WOLFSSL_MSG("r or bn NULL error");
  366. r = NULL;
  367. }
  368. /* Copy the value in. */
  369. if ((r != NULL) && mp_copy((mp_int*)bn->internal, (mp_int*)r->internal) !=
  370. MP_OKAY) {
  371. WOLFSSL_MSG("mp_copy error");
  372. r = NULL;
  373. }
  374. if (r != NULL) {
  375. /* Copy other fields in a big number. */
  376. r->neg = bn->neg;
  377. }
  378. return r;
  379. }
  380. /*******************************************************************************
  381. * Encode/Decode APIs.
  382. ******************************************************************************/
  383. /* Encode the number is a big-endian byte array.
  384. *
  385. * Assumes byte array is large enough to hold encoding when not NULL.
  386. * Use NULL for byte array to get length.
  387. *
  388. * Return compliant with OpenSSL.
  389. *
  390. * @param [in] bn Big number to reduced
  391. * @param [out] r Byte array to encode into. May be NULL.
  392. * @return Length of big number in bytes on success.
  393. * @return -1 when bn is NULL or encoding fails.
  394. */
  395. int wolfSSL_BN_bn2bin(const WOLFSSL_BIGNUM* bn, unsigned char* r)
  396. {
  397. int ret;
  398. WOLFSSL_ENTER("wolfSSL_BN_bn2bin");
  399. /* Validate parameters. */
  400. if (BN_IS_NULL(bn)) {
  401. WOLFSSL_MSG("NULL bn error");
  402. ret = -1;
  403. }
  404. else {
  405. /* Get the length of the encoding. */
  406. ret = mp_unsigned_bin_size((mp_int*)bn->internal);
  407. /* Encode if byte array supplied. */
  408. if ((r != NULL) && (mp_to_unsigned_bin((mp_int*)bn->internal, r) !=
  409. MP_OKAY)) {
  410. WOLFSSL_MSG("mp_to_unsigned_bin error");
  411. ret = -1;
  412. }
  413. }
  414. return ret;
  415. }
  416. /* Return a big number with value of the decoding of the big-endian byte array.
  417. *
  418. * Returns ret when not NULL.
  419. * Allocates a big number when ret is NULL.
  420. * Assumes str is not NULL.
  421. *
  422. * @param [in] data Byte array to decode.
  423. * @param [in] len Number of bytes in byte array.
  424. * @param [in, out] ret Big number to reduced. May be NULL.
  425. * @return A big number on success.
  426. * @return NULL on failure.
  427. */
  428. WOLFSSL_BIGNUM* wolfSSL_BN_bin2bn(const unsigned char* data, int len,
  429. WOLFSSL_BIGNUM* ret)
  430. {
  431. WOLFSSL_BIGNUM* bn = NULL;
  432. WOLFSSL_ENTER("wolfSSL_BN_bin2bn");
  433. /* Validate parameters. */
  434. if ((data == NULL) || (len < 0)) {
  435. ret = NULL;
  436. }
  437. /* Allocate a new big number when ret is NULL. */
  438. else if (ret == NULL) {
  439. ret = wolfSSL_BN_new();
  440. bn = ret;
  441. }
  442. /* Check ret is usable. */
  443. if (ret != NULL) {
  444. /* Check internal representation is usable. */
  445. if (ret->internal == NULL) {
  446. ret = NULL;
  447. }
  448. else {
  449. /* Decode into big number. */
  450. if (mp_read_unsigned_bin((mp_int*)ret->internal, data, (word32)len)
  451. != 0) {
  452. WOLFSSL_MSG("mp_read_unsigned_bin failure");
  453. /* Don't return anything on failure. bn will be freed if set. */
  454. ret = NULL;
  455. }
  456. else {
  457. /* Don't free bn as we may be returning it. */
  458. bn = NULL;
  459. }
  460. }
  461. }
  462. /* Dispose of allocated BN not being returned. */
  463. wolfSSL_BN_free(bn);
  464. return ret;
  465. }
  466. /* Encode the big number value into a string, of the radix, that is allocated.
  467. *
  468. * @param [in] bn Big number to encode.
  469. * @param [in] radix Radix to encode to.
  470. * @return String with encoding on success.
  471. * @return NULL when bn or internal representation of bn is NULL.
  472. * @return NULL on failure.
  473. */
  474. static char* wolfssl_bn_bn2radix(const WOLFSSL_BIGNUM* bn, int radix)
  475. {
  476. int err = 0;
  477. int len = 0;
  478. char* str = NULL;
  479. /* Validate parameter. */
  480. if (BN_IS_NULL(bn)) {
  481. WOLFSSL_MSG("bn NULL error");
  482. err = 1;
  483. }
  484. /* Determine length of encoding. */
  485. if ((!err) && (mp_radix_size((mp_int*)bn->internal, radix, &len) !=
  486. MP_OKAY)) {
  487. WOLFSSL_MSG("mp_radix_size failure");
  488. err = 1;
  489. }
  490. if (!err) {
  491. /* Allocate string. */
  492. str = (char*)XMALLOC((size_t)len, NULL, DYNAMIC_TYPE_OPENSSL);
  493. if (str == NULL) {
  494. WOLFSSL_MSG("BN_bn2hex malloc string failure");
  495. err = 1;
  496. }
  497. }
  498. /* Encode into string using wolfCrypt. */
  499. if ((!err) && (mp_toradix((mp_int*)bn->internal, str, radix) != MP_OKAY)) {
  500. err = 1;
  501. }
  502. if (err) {
  503. /* Dispose of dynamically allocated data. */
  504. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  505. /* Don't return freed string. */
  506. str = NULL;
  507. }
  508. return str;
  509. }
  510. /* Encode the big number value into hex string that is allocated.
  511. *
  512. * @param [in] bn Big number to encode.
  513. * @return String with encoding on success.
  514. * @return NULL when bn or internal representation of bn is NULL.
  515. * @return NULL on failure.
  516. */
  517. char* wolfSSL_BN_bn2hex(const WOLFSSL_BIGNUM *bn)
  518. {
  519. WOLFSSL_ENTER("wolfSSL_BN_bn2hex");
  520. return wolfssl_bn_bn2radix(bn, MP_RADIX_HEX);
  521. }
  522. /* Decode string of a radix into a big number.
  523. *
  524. * If bn is a pointer to NULL, then a new big number is allocated and assigned.
  525. *
  526. * Note on use: this function expects str to be an even length. It is
  527. * converting pairs of bytes into 8-bit values. As an example, the RSA
  528. * public exponent is commonly 0x010001. To get it to convert, you need
  529. * to pass in the string "010001", it will fail if you use "10001". This
  530. * is an affect of how Base16_Decode() works.
  531. *
  532. * @param [in, out] bn Pointer to a big number. May point to NULL.
  533. * @param [in] str Hex string to decode.
  534. * @param [in] radix Radix to decode from.
  535. * @return 1 on success.
  536. * @return 0 when bn or str is NULL or str is zero length.
  537. * @return 0 when creating a new big number fails.
  538. * @return 0 when decoding fails.
  539. */
  540. static int wolfssl_bn_radix2bn(WOLFSSL_BIGNUM** bn, const char* str, int radix)
  541. {
  542. int ret = 1;
  543. WOLFSSL_BIGNUM* a = NULL;
  544. /* Validate parameters. */
  545. if ((bn == NULL) || (str == NULL) || (str[0] == '\0')) {
  546. WOLFSSL_MSG("Bad function argument");
  547. ret = 0;
  548. }
  549. /* Check if we have a big number to decode into. */
  550. if ((ret == 1) && (*bn == NULL)) {
  551. /* Allocate a new big number. */
  552. a = wolfSSL_BN_new();
  553. if (a == NULL) {
  554. WOLFSSL_MSG("BN new failed");
  555. ret = 0;
  556. }
  557. /* Return allocated big number. */
  558. *bn = a;
  559. }
  560. /* Decode hex string into internal representation. */
  561. if ((ret == 1) && (mp_read_radix((mp_int*)(*bn)->internal, str, radix) !=
  562. MP_OKAY)) {
  563. WOLFSSL_MSG("Bad read_radix error");
  564. ret = 0;
  565. }
  566. if ((ret == 0) && (a != NULL)) {
  567. /* Dispose of big number. */
  568. wolfSSL_BN_free(a);
  569. /* Don't return freed big number. */
  570. *bn = NULL;
  571. }
  572. return ret;
  573. }
  574. /* Decode hex string into a big number.
  575. *
  576. * If bn is a pointer to NULL, then a new big number is allocated and assigned.
  577. *
  578. * Note on use: this function expects str to be an even length. It is
  579. * converting pairs of bytes into 8-bit values. As an example, the RSA
  580. * public exponent is commonly 0x010001. To get it to convert, you need
  581. * to pass in the string "010001", it will fail if you use "10001". This
  582. * is an affect of how Base16_Decode() works.
  583. *
  584. * @param [in, out] bn Pointer to a big number. May point to NULL.
  585. * @param [in] str Hex string to decode.
  586. * @return 1 on success.
  587. * @return 0 when bn or str is NULL or str is zero length.
  588. * @return 0 when creating a new big number fails.
  589. * @return 0 when decoding fails.
  590. */
  591. int wolfSSL_BN_hex2bn(WOLFSSL_BIGNUM** bn, const char* str)
  592. {
  593. WOLFSSL_ENTER("wolfSSL_BN_hex2bn");
  594. return wolfssl_bn_radix2bn(bn, str, MP_RADIX_HEX);
  595. }
  596. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  597. /* Encode big number into decimal string.
  598. *
  599. * @param [in] bn Big number to encode.
  600. * @return String with encoding on success.
  601. * @return NULL when bn or internal representation of bn is NULL.
  602. * @return NULL on failure.
  603. */
  604. char* wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM *bn)
  605. {
  606. WOLFSSL_ENTER("wolfSSL_BN_bn2hex");
  607. return wolfssl_bn_bn2radix(bn, MP_RADIX_DEC);
  608. }
  609. #else
  610. /* Encode big number into decimal string.
  611. *
  612. * @param [in] bn Big number to encode.
  613. * @return NULL as implementation not available.
  614. */
  615. char* wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM* bn)
  616. {
  617. (void)bn;
  618. WOLFSSL_ENTER("wolfSSL_BN_bn2dec");
  619. return NULL;
  620. }
  621. #endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
  622. #ifndef NO_RSA
  623. /* Decode hex string into a big number.
  624. *
  625. * If bn is a pointer to NULL, then a new big number is allocated and assigned.
  626. *
  627. * Note on use: this function expects str to be an even length. It is
  628. * converting pairs of bytes into 8-bit values. As an example, the RSA
  629. * public exponent is commonly 0x010001. To get it to convert, you need
  630. * to pass in the string "010001", it will fail if you use "10001". This
  631. * is an affect of how Base16_Decode() works.
  632. *
  633. * @param [in, out] bn Pointer to a big number. May point to NULL.
  634. * @param [in] str Hex string to decode.
  635. * @return 1 on success.
  636. * @return 0 when bn or str is NULL or str is zero length.
  637. * @return 0 when creating a new big number fails.
  638. * @return 0 when decoding fails.
  639. */
  640. int wolfSSL_BN_dec2bn(WOLFSSL_BIGNUM** bn, const char* str)
  641. {
  642. WOLFSSL_ENTER("wolfSSL_BN_bn2dec");
  643. return wolfssl_bn_radix2bn(bn, str, MP_RADIX_DEC);
  644. }
  645. #else
  646. /* Decode hex string into a big number.
  647. *
  648. * @param [in, out] bn Pointer to a big number. May point to NULL.
  649. * @param [in] str Hex string to decode.
  650. * @return 0 as implementation not available..
  651. */
  652. int wolfSSL_BN_dec2bn(WOLFSSL_BIGNUM** bn, const char* str)
  653. {
  654. (void)bn;
  655. (void)str;
  656. WOLFSSL_ENTER("wolfSSL_BN_bn2dec");
  657. return 0;
  658. }
  659. #endif
  660. /*******************************************************************************
  661. * Get/Set APIs
  662. ******************************************************************************/
  663. /* Calculate the number of bytes need to represent big number.
  664. *
  665. * Return compliant with OpenSSL.
  666. *
  667. * @param [in] bn Big number to use.
  668. * @return Size of BIGNUM in bytes on success.
  669. * @return 0 when bn or internal representation of bn is NULL.
  670. */
  671. int wolfSSL_BN_num_bytes(const WOLFSSL_BIGNUM* bn)
  672. {
  673. int ret;
  674. WOLFSSL_ENTER("wolfSSL_BN_num_bytes");
  675. /* Validate parameter. */
  676. if (BN_IS_NULL(bn)) {
  677. ret = 0;
  678. }
  679. else {
  680. /* Get size from wolfCrypt. */
  681. ret = mp_unsigned_bin_size((mp_int*)bn->internal);
  682. }
  683. return ret;
  684. }
  685. /* Calculate the number of bits need to represent big number.
  686. *
  687. * Return compliant with OpenSSL.
  688. *
  689. * @param [in] bn Big number to use.
  690. * @return Size of BIGNUM in bits on success.
  691. * @return 0 when bn or internal representation of bn is NULL.
  692. */
  693. int wolfSSL_BN_num_bits(const WOLFSSL_BIGNUM* bn)
  694. {
  695. int ret;
  696. WOLFSSL_ENTER("wolfSSL_BN_num_bits");
  697. /* Validate parameter. */
  698. if (BN_IS_NULL(bn)) {
  699. ret = 0;
  700. }
  701. else {
  702. /* Get size from wolfCrypt. */
  703. ret = mp_count_bits((mp_int*)bn->internal);
  704. }
  705. return ret;
  706. }
  707. /* Indicates whether a big number is negative.
  708. *
  709. * @param [in] bn Big number to use.
  710. * @return 1 when number is negative.
  711. * @return 0 when number is positive.
  712. * @return 0 when bn is NULL.
  713. */
  714. int wolfSSL_BN_is_negative(const WOLFSSL_BIGNUM* bn)
  715. {
  716. int ret;
  717. /* Validate parameter. */
  718. if (BN_IS_NULL(bn)) {
  719. ret = 0;
  720. }
  721. else {
  722. /* Check sign with wolfCrypt. */
  723. ret = mp_isneg((mp_int*)bn->internal);
  724. }
  725. return ret;
  726. }
  727. /* Indicates whether a big number is odd.
  728. *
  729. * Return compliant with OpenSSL.
  730. *
  731. * @param [in] bn Big number to use.
  732. * @return 1 when number is odd.
  733. * @return 0 when number is even.
  734. * @return 0 when bn or internal representation of bn is NULL.
  735. */
  736. int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn)
  737. {
  738. int ret;
  739. WOLFSSL_ENTER("wolfSSL_BN_is_odd");
  740. /* Validate parameter. */
  741. if (BN_IS_NULL(bn)) {
  742. ret = 0;
  743. }
  744. else {
  745. /* wolfCrypt checks whether value is odd. */
  746. ret = (mp_isodd((mp_int*)bn->internal) == MP_YES);
  747. }
  748. return ret;
  749. }
  750. #ifndef NO_WOLFSSL_STUB
  751. /* Mask the lowest n bits.
  752. *
  753. * TODO: mp_mod_2d()
  754. *
  755. * Return compliant with OpenSSL.
  756. *
  757. * @param [in, out] bn Big number to operation on.
  758. * @param [in] n Number of bits.
  759. * @return 0 on failure.
  760. */
  761. int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n)
  762. {
  763. (void)bn;
  764. (void)n;
  765. WOLFSSL_ENTER("wolfSSL_BN_mask_bits");
  766. WOLFSSL_STUB("BN_mask_bits");
  767. return 0;
  768. }
  769. #endif
  770. /* Set a bit of the value in a big number.
  771. *
  772. * Return code compliant with OpenSSL.
  773. *
  774. * @param [in, out] bn Big number to modify.
  775. * @return 1 on success.
  776. * @return 0 when bn or internal representation of bn is NULL.
  777. * @return 0 when failed to set bit.
  778. */
  779. int wolfSSL_BN_set_bit(WOLFSSL_BIGNUM* bn, int n)
  780. {
  781. int ret = 1;
  782. if (BN_IS_NULL(bn)) {
  783. WOLFSSL_MSG("bn NULL error");
  784. ret = 0;
  785. }
  786. else if (mp_set_bit((mp_int*)bn->internal, n) != MP_OKAY) {
  787. WOLFSSL_MSG("mp_set_bit error");
  788. ret = 0;
  789. }
  790. return ret;
  791. }
  792. /* Clear a bit of the value in a big number.
  793. *
  794. * Return code compliant with OpenSSL.
  795. *
  796. * @param [in] bn Big number to check.
  797. * @param [in] n Inidex of bit to check.
  798. * @return 1 on success.
  799. * @return 0 when bn or internal representation of bn is NULL.
  800. * @return 0 when failed to clear bit.
  801. */
  802. int wolfSSL_BN_clear_bit(WOLFSSL_BIGNUM* bn, int n)
  803. {
  804. int ret = 1;
  805. #ifndef WOLFSSL_SMALL_STACK
  806. mp_int tmp[1];
  807. #else
  808. mp_int* tmp = NULL;
  809. #endif
  810. /* Validate parameters. */
  811. if (BN_IS_NULL(bn) || (n < 0)) {
  812. WOLFSSL_MSG("bn NULL error");
  813. ret = 0;
  814. }
  815. /* Check if bit is set to clear. */
  816. if ((ret == 1) && (mp_is_bit_set((mp_int*)bn->internal, n))) {
  817. #ifdef WOLFSSL_SMALL_STACK
  818. /* Allocate a new MP integer to hold bit to clear. */
  819. tmp = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  820. if (tmp == NULL) {
  821. ret = 0;
  822. }
  823. #endif
  824. if (ret == 1) {
  825. /* Reset new MP integer. */
  826. XMEMSET(tmp, 0, sizeof(mp_int));
  827. if (mp_init(tmp) != MP_OKAY) {
  828. ret = 0;
  829. }
  830. }
  831. /* Set the bit to clear into temporary MP integer. */
  832. if ((ret == 1) && (mp_set_bit(tmp, n) != MP_OKAY)) {
  833. ret = 0;
  834. }
  835. /* Clear bit by sutraction. */
  836. if ((ret == 1) && (mp_sub((mp_int*)bn->internal, tmp,
  837. (mp_int*)bn->internal) != MP_OKAY)) {
  838. ret = 0;
  839. }
  840. /* Free any dynamic memory in MP integer. */
  841. mp_clear(tmp);
  842. #ifdef WOLFSSL_SMALL_STACK
  843. /* Dispose of temporary MP integer. */
  844. XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
  845. #endif
  846. }
  847. return ret;
  848. }
  849. /* Returns whether the bit is set in the value of the big number.
  850. *
  851. * When bn is NULL, returns 0.
  852. *
  853. * Return code compliant with OpenSSL.
  854. *
  855. * @param [in] bn Big number to check.
  856. * @param [in] n Inidex of bit to check.
  857. * @return 1 if bit set.
  858. * @return 0 otherwise.
  859. */
  860. int wolfSSL_BN_is_bit_set(const WOLFSSL_BIGNUM* bn, int n)
  861. {
  862. int ret;
  863. /* Check for big number value. */
  864. if (BN_IS_NULL(bn) || (n < 0)) {
  865. WOLFSSL_MSG("bn NULL error");
  866. ret = 0;
  867. }
  868. else {
  869. /* Set bit with wolfCrypt. */
  870. ret = mp_is_bit_set((mp_int*)bn->internal, (mp_digit)n);
  871. }
  872. return ret;
  873. }
  874. /* Set the big number to the value 0.
  875. *
  876. * @param [in, out] bn Big number to use.
  877. */
  878. void wolfSSL_BN_zero(WOLFSSL_BIGNUM* bn)
  879. {
  880. /* Validate parameter. */
  881. if (!BN_IS_NULL(bn)) {
  882. /* Set wolfCrypt representation to 0. */
  883. mp_zero((mp_int*)bn->internal);
  884. }
  885. }
  886. /* Set the big number to the value 0.
  887. *
  888. * @param [in, out] bn Big number to use.
  889. * @return 1 on success.
  890. * @return 0 when bn or internal representation of bn is NULL.
  891. */
  892. int wolfSSL_BN_one(WOLFSSL_BIGNUM* bn)
  893. {
  894. int ret;
  895. /* Validate parameter. */
  896. if (BN_IS_NULL(bn)) {
  897. ret = 0;
  898. }
  899. else {
  900. /* Set to value one. */
  901. ret = wolfSSL_BN_set_word(bn, 1);
  902. }
  903. return ret;
  904. }
  905. /* Get the value of the MP integer as a word.
  906. *
  907. * Assumes the MP integer value will fit in a word.
  908. *
  909. * @param [in] mp MP integer.
  910. * @return Value of MP integer as an unsigned long.
  911. */
  912. static WOLFSSL_BN_ULONG wolfssl_bn_get_word_1(mp_int *mp) {
  913. #if DIGIT_BIT >= (SIZEOF_LONG * CHAR_BIT)
  914. return (WOLFSSL_BN_ULONG)mp->dp[0];
  915. #else
  916. WOLFSSL_BN_ULONG ret = 0UL;
  917. unsigned int i;
  918. for (i = 0; i < (unsigned int)mp->used; ++i) {
  919. ret |= ((WOLFSSL_BN_ULONG)mp->dp[i]) << (DIGIT_BIT * i);
  920. }
  921. return ret;
  922. #endif
  923. }
  924. /* Return the value of big number as an unsigned long if possible.
  925. *
  926. * @param [in] bn Big number to get value from.
  927. * @return Value or 0xFFFFFFFFL if bigger than unsigned long.
  928. */
  929. WOLFSSL_BN_ULONG wolfSSL_BN_get_word(const WOLFSSL_BIGNUM* bn)
  930. {
  931. WOLFSSL_BN_ULONG ret;
  932. WOLFSSL_ENTER("wolfSSL_BN_get_word");
  933. /* Validate parameter. */
  934. if (BN_IS_NULL(bn)) {
  935. WOLFSSL_MSG("Invalid argument");
  936. ret = 0;
  937. }
  938. /* Check whether big number is to fit in an unsigned long. */
  939. else if (wolfSSL_BN_num_bytes(bn) > (int)sizeof(unsigned long)) {
  940. WOLFSSL_MSG("bignum is larger than unsigned long");
  941. ret = WOLFSSL_BN_MAX_VAL;
  942. }
  943. else {
  944. /* Get the word from the internal representation. */
  945. ret = wolfssl_bn_get_word_1((mp_int*)bn->internal);
  946. }
  947. return ret;
  948. }
  949. /* Set the big number to the value in the word.
  950. *
  951. * Return code compliant with OpenSSL.
  952. *
  953. * @param [in, out] bn Big number to set.
  954. * @param [in w Word to set.
  955. * @return 1 on success.
  956. * @return 0 when bn is NULL or setting value failed.
  957. */
  958. int wolfSSL_BN_set_word(WOLFSSL_BIGNUM* bn, unsigned long w)
  959. {
  960. int ret = 1;
  961. WOLFSSL_ENTER("wolfSSL_BN_set_word");
  962. /* Validate parameters. */
  963. if (BN_IS_NULL(bn)) {
  964. WOLFSSL_MSG("bn NULL error");
  965. ret = 0;
  966. }
  967. /* Set the word into the internal representation. */
  968. if ((ret == 1) && (mp_set_int((mp_int*)bn->internal, w) != MP_OKAY)) {
  969. WOLFSSL_MSG("mp_init_set_int error");
  970. ret = 0;
  971. }
  972. return ret;
  973. }
  974. /*******************************************************************************
  975. * Comparison APIs
  976. ******************************************************************************/
  977. /* Compares two big numbers. a <=> b
  978. *
  979. * NULL equals NULL
  980. * NULL less than not NULL
  981. * not NULL greater than NULL.
  982. *
  983. * Return compliant with OpenSSL.
  984. *
  985. * @param [in] bn First big number to compare.
  986. * @param [in] bn Second big number to compare.
  987. * @return -1 when a is less than b (a < b).
  988. * @return 0 when a is equal to b (a == b).
  989. * @return 1 when a is greater than b (a > b).
  990. * @return 0 when bn or internal representation of bn is NULL.
  991. */
  992. int wolfSSL_BN_cmp(const WOLFSSL_BIGNUM* a, const WOLFSSL_BIGNUM* b)
  993. {
  994. int ret;
  995. int bIsNull;
  996. WOLFSSL_ENTER("wolfSSL_BN_cmp");
  997. /* Must know whether b is NULL. */
  998. bIsNull = BN_IS_NULL(b);
  999. /* Check whether a is NULL. */
  1000. if (BN_IS_NULL(a)) {
  1001. if (bIsNull) {
  1002. /* NULL equals NULL. */
  1003. ret = 0;
  1004. }
  1005. else {
  1006. /* NULL less than not NULL. */
  1007. ret = -1;
  1008. }
  1009. }
  1010. else if (bIsNull) {
  1011. /* not NULL greater than NULL. */
  1012. ret = 1;
  1013. }
  1014. else {
  1015. /* Compare big numbers with wolfCrypt. */
  1016. ret = mp_cmp((mp_int*)a->internal, (mp_int*)b->internal);
  1017. /* Convert wolfCrypt return value. */
  1018. if (ret == MP_EQ) {
  1019. ret = 0;
  1020. }
  1021. else if (ret == MP_GT) {
  1022. ret = 1;
  1023. }
  1024. else {
  1025. ret = -1;
  1026. }
  1027. }
  1028. return ret;
  1029. }
  1030. /* Indicates whether a big number is the value 0.
  1031. *
  1032. * Return compliant with OpenSSL.
  1033. *
  1034. * @param [in] bn Big number to use.
  1035. * @return 1 when number is zero.
  1036. * @return 0 when number is not zero.
  1037. * @return 0 when bn or internal representation of bn is NULL.
  1038. */
  1039. int wolfSSL_BN_is_zero(const WOLFSSL_BIGNUM* bn)
  1040. {
  1041. int ret;
  1042. WOLFSSL_ENTER("wolfSSL_BN_is_zero");
  1043. /* Validate parameter. */
  1044. if (BN_IS_NULL(bn)) {
  1045. ret = 0;
  1046. }
  1047. else {
  1048. /* wolfCrypt checks whether value is 0. */
  1049. ret = (mp_iszero((mp_int*)bn->internal) == MP_YES);
  1050. }
  1051. return ret;
  1052. }
  1053. /* Indicates whether a big number is the value 1.
  1054. *
  1055. * Return compliant with OpenSSL.
  1056. *
  1057. * @param [in] bn Big number to use.
  1058. * @return 1 when number is one.
  1059. * @return 0 when number is not one.
  1060. * @return 0 when bn or internal representation of bn is NULL.
  1061. */
  1062. int wolfSSL_BN_is_one(const WOLFSSL_BIGNUM* bn)
  1063. {
  1064. int ret;
  1065. WOLFSSL_ENTER("wolfSSL_BN_is_one");
  1066. /* Validate parameter. */
  1067. if (BN_IS_NULL(bn)) {
  1068. ret = 0;
  1069. }
  1070. else {
  1071. /* wolfCrypt checks whether value is 1. */
  1072. ret = (mp_cmp_d((mp_int*)bn->internal, 1) == MP_EQ);
  1073. }
  1074. return ret;
  1075. }
  1076. /* Indicates whether a big number is the value passed in.
  1077. *
  1078. * Return compliant with OpenSSL.
  1079. *
  1080. * @param [in] bn Big number to use.
  1081. * @return 1 when big number is the value.
  1082. * @return 0 when big number is not the value.
  1083. * @return 0 when bn or internal representation of bn is NULL.
  1084. */
  1085. int wolfSSL_BN_is_word(const WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w)
  1086. {
  1087. int ret;
  1088. WOLFSSL_ENTER("wolfSSL_BN_is_word");
  1089. /* Validate parameter. */
  1090. if (BN_IS_NULL(bn)) {
  1091. WOLFSSL_MSG("bn NULL error");
  1092. ret = 0;
  1093. }
  1094. else
  1095. #if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT)
  1096. /* When value is greater than what can be stored in one digit - special
  1097. * case. */
  1098. if (w > (WOLFSSL_BN_ULONG)MP_MASK) {
  1099. /* TODO: small stack */
  1100. mp_int w_mp;
  1101. /* Create a MP to hold the number. */
  1102. if (mp_init(&w_mp) != MP_OKAY) {
  1103. ret = 0;
  1104. }
  1105. /* Set the value - held in more than one digit. */
  1106. else if (mp_set_int(&w_mp, w) != MP_OKAY) {
  1107. ret = 0;
  1108. }
  1109. else {
  1110. /* Compare MP representations. */
  1111. ret = (mp_cmp((mp_int *)bn->internal, &w_mp) == MP_EQ);
  1112. mp_free(&w_mp);
  1113. }
  1114. }
  1115. else
  1116. #endif
  1117. {
  1118. /* wolfCrypt checks whether it is the value. */
  1119. ret = (mp_isword((mp_int*)bn->internal, (mp_digit)w) == MP_YES);
  1120. }
  1121. return ret;
  1122. }
  1123. /*******************************************************************************
  1124. * Word operation APIs.
  1125. ******************************************************************************/
  1126. /* Add/subtract a word to/from a big number.
  1127. *
  1128. * Internal function for adding/subtracting an unsigned long from a
  1129. * WOLFSSL_BIGNUM. To add, pass "sub" as 0. To subtract, pass it as 1.
  1130. *
  1131. * @param [in, out] bn Big number to operate on.
  1132. * @param [in] w Word to operate with.
  1133. * @param [in] sub Indicates whether operation to perform is a subtract.
  1134. * @return 1 on success.
  1135. * @return 0 in failure.
  1136. */
  1137. static int wolfssl_bn_add_word_int(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w,
  1138. int sub)
  1139. {
  1140. int ret = 1;
  1141. #if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT)
  1142. #ifdef WOLFSSL_SMALL_STACK
  1143. mp_int* w_mp = NULL;
  1144. #else
  1145. mp_int w_mp[1];
  1146. #endif /* WOLFSSL_SMALL_STACK */
  1147. #endif
  1148. #if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT)
  1149. #ifdef WOLFSSL_SMALL_STACK
  1150. /* Allocate temporary MP integer. */
  1151. w_mp = (mp_int*)XMALLOC(sizeof(*w_mp), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1152. if (w_mp == NULL) {
  1153. ret = 0;
  1154. }
  1155. else
  1156. #endif /* WOLFSSL_SMALL_STACK */
  1157. {
  1158. /* Clear out MP integer so it can be freed. */
  1159. XMEMSET(w_mp, 0, sizeof(*w_mp));
  1160. }
  1161. #endif
  1162. /* Validate parameters. */
  1163. if (BN_IS_NULL(bn)) {
  1164. WOLFSSL_MSG("bn NULL error");
  1165. ret = 0;
  1166. }
  1167. if (ret == 1) {
  1168. int rc = 0;
  1169. #if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT)
  1170. if (w > (WOLFSSL_BN_ULONG)MP_MASK) {
  1171. /* Initialize temporary MP integer. */
  1172. if (mp_init(w_mp) != MP_OKAY) {
  1173. ret = 0;
  1174. }
  1175. /* Set value into temporary MP integer. */
  1176. if ((ret == 1) && (mp_set_int(w_mp, w) != MP_OKAY)) {
  1177. ret = 0;
  1178. }
  1179. if (ret == 1) {
  1180. if (sub) {
  1181. /* Subtract as MP integer. */
  1182. rc = mp_sub((mp_int *)bn->internal, w_mp,
  1183. (mp_int *)bn->internal);
  1184. }
  1185. else {
  1186. /* Add as MP integer. */
  1187. rc = mp_add((mp_int *)bn->internal, w_mp,
  1188. (mp_int *)bn->internal);
  1189. }
  1190. if (rc != MP_OKAY) {
  1191. WOLFSSL_MSG("mp_add/sub error");
  1192. ret = 0;
  1193. }
  1194. }
  1195. }
  1196. else
  1197. #endif
  1198. {
  1199. if (sub) {
  1200. /* Subtract word from MP integer. */
  1201. rc = mp_sub_d((mp_int*)bn->internal, (mp_digit)w,
  1202. (mp_int*)bn->internal);
  1203. }
  1204. else {
  1205. /* Add word from MP integer. */
  1206. rc = mp_add_d((mp_int*)bn->internal, (mp_digit)w,
  1207. (mp_int*)bn->internal);
  1208. }
  1209. if (rc != MP_OKAY) {
  1210. WOLFSSL_MSG("mp_add/sub_d error");
  1211. ret = 0;
  1212. }
  1213. }
  1214. }
  1215. #if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT)
  1216. mp_free(w_mp);
  1217. #ifdef WOLFSSL_SMALL_STACK
  1218. XFREE(w_mp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1219. #endif /* WOLFSSL_SMALL_STACK */
  1220. #endif
  1221. return ret;
  1222. }
  1223. /* Add a word to a big number.
  1224. *
  1225. * Return code compliant with OpenSSL.
  1226. *
  1227. * @param [in, out] bn Big number to operate on.
  1228. * @param [in] w Word to operate with.
  1229. * @return 1 on success.
  1230. * @return 0 in failure.
  1231. */
  1232. int wolfSSL_BN_add_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w)
  1233. {
  1234. int ret;
  1235. WOLFSSL_ENTER("wolfSSL_BN_add_word");
  1236. ret = wolfssl_bn_add_word_int(bn, w, 0);
  1237. WOLFSSL_LEAVE("wolfSSL_BN_add_word", ret);
  1238. return ret;
  1239. }
  1240. /* Subtract a word from a big number.
  1241. *
  1242. * Return code compliant with OpenSSL.
  1243. *
  1244. * @param [in, out] bn Big number to operate on.
  1245. * @param [in] w Word to operate with.
  1246. * @return 1 on success.
  1247. * @return 0 in failure.
  1248. */
  1249. int wolfSSL_BN_sub_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w)
  1250. {
  1251. int ret;
  1252. WOLFSSL_ENTER("wolfSSL_BN_sub_word");
  1253. ret = wolfssl_bn_add_word_int(bn, w, 1);
  1254. WOLFSSL_LEAVE("wolfSSL_BN_sub_word", ret);
  1255. return ret;
  1256. }
  1257. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  1258. !defined(NO_DSA))
  1259. /* Calculate bn modulo word w. bn % w
  1260. *
  1261. * Return code compliant with OpenSSL.
  1262. *
  1263. * @return Word result on success
  1264. * @return -1 on error
  1265. */
  1266. WOLFSSL_BN_ULONG wolfSSL_BN_mod_word(const WOLFSSL_BIGNUM *bn,
  1267. WOLFSSL_BN_ULONG w)
  1268. {
  1269. WOLFSSL_BN_ULONG ret = 0;
  1270. WOLFSSL_ENTER("wolfSSL_BN_mod_word");
  1271. /* Validate parameters. */
  1272. if (BN_IS_NULL(bn)) {
  1273. WOLFSSL_MSG("bn NULL error");
  1274. ret = (WOLFSSL_BN_ULONG)-1;
  1275. }
  1276. #if DIGIT_BIT < (SIZEOF_LONG * CHAR_BIT)
  1277. if ((ret == 0) && (w > (WOLFSSL_BN_ULONG)MP_MASK)) {
  1278. /* TODO: small stack */
  1279. mp_int w_mp;
  1280. mp_int r_mp;
  1281. /* Memset MP integers to be safe to free. */
  1282. XMEMSET(&w_mp, 0, sizeof(w_mp));
  1283. XMEMSET(&r_mp, 0, sizeof(r_mp));
  1284. /* Initialize MP integer to hold word. */
  1285. if (mp_init(&w_mp) != MP_OKAY) {
  1286. ret = (WOLFSSL_BN_ULONG)-1;
  1287. }
  1288. /* Initialize MP integer to hold result word. */
  1289. if ((ret == 0) && (mp_init(&r_mp) != MP_OKAY)) {
  1290. ret = (WOLFSSL_BN_ULONG)-1;
  1291. }
  1292. /* Set modulus word into MP integer. */
  1293. if ((ret == 0) && (mp_set_int(&w_mp, w) != MP_OKAY)) {
  1294. ret = (WOLFSSL_BN_ULONG)-1;
  1295. }
  1296. /* Calculate modulus result. */
  1297. if ((ret == 0) && (mp_mod((mp_int *)bn->internal, &w_mp, &r_mp) !=
  1298. MP_OKAY)) {
  1299. WOLFSSL_MSG("mp_mod error");
  1300. ret = (WOLFSSL_BN_ULONG)-1;
  1301. }
  1302. if (ret == 0) {
  1303. /* Get modulus result into an unsigned long. */
  1304. ret = wolfssl_bn_get_word_1(&r_mp);
  1305. }
  1306. /* Dispose of dynamically allocated data. */
  1307. mp_free(&r_mp);
  1308. mp_free(&w_mp);
  1309. }
  1310. else
  1311. #endif
  1312. if (ret == 0) {
  1313. mp_digit mp_ret;
  1314. /* Calculate modulus result using wolfCrypt. */
  1315. if (mp_mod_d((mp_int*)bn->internal, (mp_digit)w, &mp_ret) != MP_OKAY) {
  1316. WOLFSSL_MSG("mp_add_d error");
  1317. ret = (WOLFSSL_BN_ULONG)-1;
  1318. }
  1319. else {
  1320. /* Return result. */
  1321. ret = (WOLFSSL_BN_ULONG)mp_ret;
  1322. }
  1323. }
  1324. return ret;
  1325. }
  1326. #endif /* WOLFSSL_KEY_GEN && (!NO_RSA || !NO_DH || !NO_DSA) */
  1327. /*******************************************************************************
  1328. * Shift APIs
  1329. ******************************************************************************/
  1330. #ifndef WOLFSSL_SP_MATH
  1331. /* Shift the value in bn left by n bits into r. r = bn << n
  1332. *
  1333. * Return code compliant with OpenSSL.
  1334. *
  1335. * @return 1 on success.
  1336. * @return 0 when r or bn or internal representation is NULL.
  1337. * @return 0 on failure.
  1338. */
  1339. int wolfSSL_BN_lshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
  1340. {
  1341. int ret = 1;
  1342. WOLFSSL_ENTER("wolfSSL_BN_lshift");
  1343. /* Validate parameters. */
  1344. if (BN_IS_NULL(r) || BN_IS_NULL(bn)) {
  1345. WOLFSSL_MSG("bn NULL error");
  1346. ret = 0;
  1347. }
  1348. else if (n < 0) {
  1349. WOLFSSL_MSG("n value error");
  1350. ret = 0;
  1351. }
  1352. /* Use wolfCrypt perform operation. */
  1353. if ((ret == 1) && (mp_mul_2d((mp_int*)bn->internal, n,
  1354. (mp_int*)r->internal) != MP_OKAY)) {
  1355. WOLFSSL_MSG("mp_mul_2d error");
  1356. ret = 0;
  1357. }
  1358. return ret;
  1359. }
  1360. /* Shift the value in bn right by n bits into r. r = bn >> n
  1361. *
  1362. * Return code compliant with OpenSSL.
  1363. *
  1364. * @return 1 on success.
  1365. * @return 0 when r or bn or internal representation is NULL.
  1366. * @return 0 on failure.
  1367. */
  1368. int wolfSSL_BN_rshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
  1369. {
  1370. int ret = 1;
  1371. WOLFSSL_ENTER("wolfSSL_BN_rshift");
  1372. /* Validate parameters. */
  1373. if (BN_IS_NULL(r) || BN_IS_NULL(bn)) {
  1374. WOLFSSL_MSG("bn NULL error");
  1375. ret = 0;
  1376. }
  1377. else if (n < 0) {
  1378. WOLFSSL_MSG("n value error");
  1379. ret = 0;
  1380. }
  1381. /* Use wolfCrypt perform operation. */
  1382. if ((ret == 1) && (mp_div_2d((mp_int*)bn->internal, n, (mp_int*)r->internal,
  1383. NULL) != MP_OKAY)) {
  1384. WOLFSSL_MSG("mp_mul_2d error");
  1385. ret = 0;
  1386. }
  1387. return ret;
  1388. }
  1389. #endif
  1390. /*******************************************************************************
  1391. * Simple Math APIs
  1392. ******************************************************************************/
  1393. /* Add a to b into r. r = a + b
  1394. *
  1395. * Return code compliant with OpenSSL.
  1396. *
  1397. * @param [out] r Big number to put result into.
  1398. * @param [in] a Big number to be added to.
  1399. * @param [in] b Big number to add with.
  1400. *
  1401. * @return 1 on success.
  1402. * @return 0 when r, a or b or internal representation is NULL.
  1403. * @return 0 on failure.
  1404. */
  1405. int wolfSSL_BN_add(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a, WOLFSSL_BIGNUM *b)
  1406. {
  1407. int ret = 1;
  1408. WOLFSSL_ENTER("wolfSSL_BN_add");
  1409. /* Validate parameters. */
  1410. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
  1411. WOLFSSL_MSG("bn NULL error");
  1412. ret = 0;
  1413. }
  1414. /* Add the internal representations into internal representation. */
  1415. if ((ret == 1) && (mp_add((mp_int*)a->internal, (mp_int*)b->internal,
  1416. (mp_int*)r->internal) != MP_OKAY)) {
  1417. WOLFSSL_MSG("mp_add_d error");
  1418. ret = 0;
  1419. }
  1420. return ret;
  1421. }
  1422. /* Subtract a from b into r. r = a - b
  1423. *
  1424. * @param [out] r Big number to put result into.
  1425. * @param [in] a Big number to be subtracted from.
  1426. * @param [in] b Big number to subtract with.
  1427. *
  1428. * @return 1 on success.
  1429. * @return 0 when r, a or b is NULL.
  1430. * @return 0 on failure.
  1431. */
  1432. int wolfSSL_BN_sub(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
  1433. const WOLFSSL_BIGNUM* b)
  1434. {
  1435. int ret = 1;
  1436. WOLFSSL_ENTER("wolfSSL_BN_sub");
  1437. /* Validate parameters. */
  1438. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
  1439. ret = 0;
  1440. }
  1441. /* Have wolfCrypt perform operation with internal representations. */
  1442. if ((ret == 1) && (mp_sub((mp_int*)a->internal,(mp_int*)b->internal,
  1443. (mp_int*)r->internal) != MP_OKAY)) {
  1444. ret = 0;
  1445. }
  1446. WOLFSSL_LEAVE("wolfSSL_BN_sub mp_sub", ret);
  1447. return ret;
  1448. }
  1449. /* Multiply a with b into r. r = a * b
  1450. *
  1451. * @param [out] r Big number to put result into.
  1452. * @param [in] a Big number to be multiplied.
  1453. * @param [in] b Big number to multiply with.
  1454. * @param [in] ctx BN context object. Unused.
  1455. *
  1456. * @return 1 on success.
  1457. * @return 0 when r, a or b is NULL.
  1458. * @return 0 when internal representation of r, a or b is NULL.
  1459. * @return 0 on failure.
  1460. */
  1461. int wolfSSL_BN_mul(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a, WOLFSSL_BIGNUM *b,
  1462. WOLFSSL_BN_CTX *ctx)
  1463. {
  1464. int ret = 1;
  1465. (void)ctx;
  1466. WOLFSSL_ENTER("wolfSSL_BN_mul");
  1467. /* Validate parameters. */
  1468. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
  1469. ret = 0;
  1470. }
  1471. /* Have wolfCrypt perform operation with internal representations. */
  1472. if ((ret == 1) && (mp_mul((mp_int*)a->internal, (mp_int*)b->internal,
  1473. (mp_int*)r->internal) != MP_OKAY)) {
  1474. ret = 0;
  1475. }
  1476. WOLFSSL_LEAVE("wolfSSL_BN_mul", ret);
  1477. return ret;
  1478. }
  1479. #ifndef WOLFSSL_SP_MATH
  1480. /* Divide a by b into dv and put remainder into rem. dv = a / b, rem = a % b
  1481. *
  1482. * @param [out] dv Big number to put division result into.
  1483. * @param [out] rem Big number to put remainder into.
  1484. * @param [in] a Big number to be divided.
  1485. * @param [in] b Big number to divide with.
  1486. * @param [in] ctx BN context object. Unused.
  1487. *
  1488. * @return 1 on success.
  1489. * @return 0 when dv, rem, a or b is NULL.
  1490. * @return 0 when internal representation of dv, rem, a or b is NULL.
  1491. * @return 0 on failure.
  1492. */
  1493. int wolfSSL_BN_div(WOLFSSL_BIGNUM* dv, WOLFSSL_BIGNUM* rem,
  1494. const WOLFSSL_BIGNUM* a, const WOLFSSL_BIGNUM* d, WOLFSSL_BN_CTX* ctx)
  1495. {
  1496. int ret = 1;
  1497. /* BN context not needed. */
  1498. (void)ctx;
  1499. WOLFSSL_ENTER("wolfSSL_BN_div");
  1500. /* Validate parameters. */
  1501. if (BN_IS_NULL(dv) || BN_IS_NULL(rem) || BN_IS_NULL(a) || BN_IS_NULL(d)) {
  1502. ret = 0;
  1503. }
  1504. /* Have wolfCrypt perform operation with internal representations. */
  1505. if ((ret == 1) && (mp_div((mp_int*)a->internal, (mp_int*)d->internal,
  1506. (mp_int*)dv->internal, (mp_int*)rem->internal) != MP_OKAY)) {
  1507. ret = 0;
  1508. }
  1509. WOLFSSL_LEAVE("wolfSSL_BN_div", ret);
  1510. return ret;
  1511. }
  1512. #endif
  1513. /* Calculate a mod b into r. r = a % b
  1514. *
  1515. * @param [out] r Big number to put result into.
  1516. * @param [in] a Big number to reduced
  1517. * @param [in] b Big number to reduce with.
  1518. * @param [in] ctx BN context object. Unused.
  1519. *
  1520. * @return 1 on success.
  1521. * @return 0 when r, a or b is NULL.
  1522. * @return 0 on failure.
  1523. */
  1524. int wolfSSL_BN_mod(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
  1525. const WOLFSSL_BIGNUM* b, const WOLFSSL_BN_CTX* c)
  1526. {
  1527. int ret = 1;
  1528. (void)c;
  1529. WOLFSSL_ENTER("wolfSSL_BN_mod");
  1530. /* Validate parameters. */
  1531. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
  1532. ret = 0;
  1533. }
  1534. /* Have wolfCrypt perform operation with internal representations. */
  1535. if ((ret == 1) && (mp_mod((mp_int*)a->internal,(mp_int*)b->internal,
  1536. (mp_int*)r->internal) != MP_OKAY)) {
  1537. ret = 0;
  1538. }
  1539. WOLFSSL_LEAVE("wolfSSL_BN_mod mp_mod", ret);
  1540. return ret;
  1541. }
  1542. /*******************************************************************************
  1543. * Math and Mod APIs
  1544. ******************************************************************************/
  1545. #ifndef WOLFSSL_SP_MATH
  1546. /* Add a to b modulo m into r. r = a + b (mod m)
  1547. *
  1548. * @param [in, out] r Big number to hold result.
  1549. * @param [in] a Big number to add to.
  1550. * @param [in] b Big number to add with.
  1551. * @param [in] m Big number that is the modulus.
  1552. * @param [in] ctx BN context. Not used.
  1553. * @return 1 on success.
  1554. * @return 0 when r, a or b or internal representation is NULL.
  1555. * @return 0 on calculation failure.
  1556. */
  1557. int wolfSSL_BN_mod_add(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
  1558. const WOLFSSL_BIGNUM *b, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  1559. {
  1560. int ret = 1;
  1561. /* BN context not needed. */
  1562. (void)ctx;
  1563. WOLFSSL_ENTER("wolfSSL_BN_add");
  1564. /* Validate parameters. */
  1565. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b) || BN_IS_NULL(m)) {
  1566. WOLFSSL_MSG("bn NULL error");
  1567. ret = 0;
  1568. }
  1569. /* Perform operation with wolfCrypt. */
  1570. if ((ret == 1) && (mp_addmod((mp_int*)a->internal, (mp_int*)b->internal,
  1571. (mp_int*)m->internal, (mp_int*)r->internal) != MP_OKAY)) {
  1572. WOLFSSL_MSG("mp_add_d error");
  1573. ret = 0;
  1574. }
  1575. return ret;
  1576. }
  1577. #endif
  1578. /* Calculate a multiplied by b, mod m into r. r = (a * b) % m
  1579. *
  1580. * @param [out] r Big number to put result into.
  1581. * @param [in] a Base as a big number.
  1582. * @param [in] b Multiplier as a big number.
  1583. * @param [in] m Modulus as a big number.
  1584. * @param [in] ctx BN context object. Unused.
  1585. *
  1586. * @return 1 on success.
  1587. * @return 0 when r, a, b or m is NULL.
  1588. * @return 0 on failure.
  1589. */
  1590. int wolfSSL_BN_mod_mul(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
  1591. const WOLFSSL_BIGNUM *b, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  1592. {
  1593. int ret = 1;
  1594. /* BN context not needed. */
  1595. (void)ctx;
  1596. WOLFSSL_ENTER("wolfSSL_BN_mod_mul");
  1597. /* Validate parameters. */
  1598. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b) || BN_IS_NULL(m)) {
  1599. WOLFSSL_MSG("Bad Argument");
  1600. ret = 0;
  1601. }
  1602. /* Have wolfCrypt perform operation with internal representations. */
  1603. if ((ret == 1) && (mp_mulmod((mp_int*)a->internal, (mp_int*)b->internal,
  1604. (mp_int*)m->internal, (mp_int*)r->internal)) != MP_OKAY) {
  1605. ret = 0;
  1606. }
  1607. WOLFSSL_LEAVE("wolfSSL_BN_mod_mul", ret);
  1608. return ret;
  1609. }
  1610. /* Calculate a to the power of e, mod m into r. r = (a ^ e) % m
  1611. *
  1612. * @param [out] r Big number to put result into.
  1613. * @param [in] a Base as a big number.
  1614. * @param [in] e Exponent as a big number.
  1615. * @param [in] m Modulus as a big number.
  1616. * @param [in] ctx BN context object. Unused.
  1617. *
  1618. * @return 1 on success.
  1619. * @return 0 when r, a, p or m is NULL.
  1620. * @return 0 on failure.
  1621. */
  1622. int wolfSSL_BN_mod_exp(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
  1623. const WOLFSSL_BIGNUM *e, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  1624. {
  1625. int ret = 1;
  1626. /* BN context not needed. */
  1627. (void)ctx;
  1628. WOLFSSL_ENTER("wolfSSL_BN_mod_exp");
  1629. /* Validate parameters. */
  1630. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(e) || BN_IS_NULL(m)) {
  1631. WOLFSSL_MSG("Bad Argument");
  1632. ret = 0;
  1633. }
  1634. /* Have wolfCrypt perform operation with internal representations. */
  1635. if ((ret == 1) && (mp_exptmod((mp_int*)a->internal, (mp_int*)e->internal,
  1636. (mp_int*)m->internal, (mp_int*)r->internal)) != MP_OKAY) {
  1637. ret = 0;
  1638. }
  1639. WOLFSSL_LEAVE("wolfSSL_BN_mod_exp", ret);
  1640. return ret;
  1641. }
  1642. /* Calculate the modular inverse of a mod m into r. r = a^-1 mod m
  1643. *
  1644. * A new big number is allocated when r is NULL.
  1645. *
  1646. * @param [in, out] r Big number to hold result. May be NULL.
  1647. * @param [in] a Big number to invert.
  1648. * @param [in] m Big number that is the modulus.
  1649. * @param [in] ctx BN context. Not used.
  1650. * @return Big number holding result on success.
  1651. * @return NULL on failure.
  1652. */
  1653. WOLFSSL_BIGNUM *wolfSSL_BN_mod_inverse(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a,
  1654. const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  1655. {
  1656. int err = 0;
  1657. WOLFSSL_BIGNUM* t = NULL;
  1658. /* BN context not needed. */
  1659. (void)ctx;
  1660. WOLFSSL_ENTER("wolfSSL_BN_mod_inverse");
  1661. /* Validate parameters. */
  1662. if (BN_IS_NULL(a) || BN_IS_NULL(m) || ((r != NULL) &&
  1663. (r->internal == NULL))) {
  1664. WOLFSSL_MSG("a or n NULL error");
  1665. err = 1;
  1666. }
  1667. /* Check whether we have a result big number. */
  1668. if ((!err) && (r == NULL)) {
  1669. /* Allocate a new big number to hold result and be returned. */
  1670. t = wolfSSL_BN_new();
  1671. if (t == NULL){
  1672. WOLFSSL_MSG("WolfSSL_BN_new() failed");
  1673. err = 1;
  1674. }
  1675. r = t;
  1676. }
  1677. /* Compute inverse of a modulo n and return in r */
  1678. if ((!err) && (mp_invmod((mp_int *)a->internal, (mp_int *)m->internal,
  1679. (mp_int*)r->internal) != MP_OKAY)) {
  1680. WOLFSSL_MSG("mp_invmod() error");
  1681. err = 1;
  1682. }
  1683. if (err) {
  1684. wolfSSL_BN_free(t);
  1685. r = NULL;
  1686. }
  1687. return r;
  1688. }
  1689. /*******************************************************************************
  1690. * Other Math APIs
  1691. ******************************************************************************/
  1692. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  1693. /* Needed to get mp_gcd. */
  1694. /* Greatest Common Divisor (GCM) of a and b into r. r = GCD(a, b)
  1695. *
  1696. * @param [out] r Big number to put result into.
  1697. * @param [in] a First big number.
  1698. * @param [in] b Second big number.
  1699. * @param [in] ctx BN context object. Unused.
  1700. *
  1701. * @return 1 on success.
  1702. * @return 0 when r, a or b is NULL.
  1703. * @return 0 when internal representation of r, a or b is NULL.
  1704. * @return 0 on failure.
  1705. */
  1706. int wolfSSL_BN_gcd(WOLFSSL_BIGNUM* r, WOLFSSL_BIGNUM* a, WOLFSSL_BIGNUM* b,
  1707. WOLFSSL_BN_CTX* ctx)
  1708. {
  1709. int ret = 1;
  1710. /* BN context not needed. */
  1711. (void)ctx;
  1712. WOLFSSL_ENTER("wolfSSL_BN_gcd");
  1713. /* Validate parameters. */
  1714. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
  1715. ret = 0;
  1716. }
  1717. /* Have wolfCrypt perform operation with internal representations. */
  1718. if ((ret == 1) && (mp_gcd((mp_int*)a->internal, (mp_int*)b->internal,
  1719. (mp_int*)r->internal) != MP_OKAY)) {
  1720. ret = 0;
  1721. }
  1722. WOLFSSL_LEAVE("wolfSSL_BN_gcd", ret);
  1723. return ret;
  1724. }
  1725. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  1726. /*******************************************************************************
  1727. * Random APIs
  1728. ******************************************************************************/
  1729. /* Generates a random number up to bits long.
  1730. *
  1731. * @param [in, out] bn Big number to generate into.
  1732. * @param [in] bits Number of bits in word.
  1733. * @param [in] top Whether top bits must be set.
  1734. * Valid values: WOLFSSL_BN_RAND_TOP_ANY,
  1735. * WOLFSSL_BN_RAND_TOP_ONE, WOLFSSL_BN_RAND_TOP_TWO.
  1736. * @param [in] bottom Whether bottom bit must be set.
  1737. * Valid values: WOLFSSL_BN_RAND_BOTTOM_ANY,
  1738. WOLFSSL_BN_RAND_BOTTOM_ODD.
  1739. * @return 1 on success.
  1740. * @return 0 when bn is NULL.
  1741. * @return 0 when bits is invalid.
  1742. * @return 0 when bits and top/bottom are invalid.
  1743. * @return 0 when generation fails.
  1744. */
  1745. int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
  1746. {
  1747. int ret = 1;
  1748. word32 len = (word32)((bits + 7) / 8);
  1749. WC_RNG* rng;
  1750. WOLFSSL_ENTER("wolfSSL_BN_rand");
  1751. /* Validate parameters. */
  1752. if (BN_IS_NULL(bn)) {
  1753. WOLFSSL_MSG("Bad argument - bn");
  1754. ret = 0;
  1755. }
  1756. else if (bits < 0) {
  1757. WOLFSSL_MSG("Bad argument - bits < 0");
  1758. ret = 0;
  1759. }
  1760. else if ((bits == 0) && ((bottom != WOLFSSL_BN_RAND_BOTTOM_ANY) ||
  1761. (top != WOLFSSL_BN_RAND_TOP_ANY))) {
  1762. WOLFSSL_MSG("Bad top/bottom - bits == 0");
  1763. ret = 0;
  1764. }
  1765. else if ((bits == 1) && (top > WOLFSSL_BN_RAND_TOP_ONE)) {
  1766. WOLFSSL_MSG("Bad top - bits == 1");
  1767. ret = 0;
  1768. }
  1769. /* Handle simple case of zero bits. */
  1770. if ((ret == 1) && (bits == 0)) {
  1771. mp_zero((mp_int*)bn->internal);
  1772. }
  1773. else if (ret == 1) {
  1774. byte* buff = NULL;
  1775. /* Get random to global random to generate bits. */
  1776. if ((rng = wolfssl_make_global_rng()) == NULL) {
  1777. WOLFSSL_MSG("Failed to use global RNG.");
  1778. ret = 0;
  1779. }
  1780. /* Allocate buffer to hold generated bits. */
  1781. if ((ret == 1) && ((buff = (byte*)XMALLOC(len, NULL,
  1782. DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  1783. WOLFSSL_MSG("Failed to allocate buffer.");
  1784. ret = 0;
  1785. }
  1786. /* Generate bytes to cover bits. */
  1787. if ((ret == 1) && wc_RNG_GenerateBlock(rng, buff, len) != 0) {
  1788. WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
  1789. ret = 0;
  1790. }
  1791. /* Read bytes in to big number. */
  1792. if ((ret == 1) && mp_read_unsigned_bin((mp_int*)bn->internal, buff, len)
  1793. != MP_OKAY) {
  1794. WOLFSSL_MSG("mp_read_unsigned_bin failed");
  1795. ret = 0;
  1796. }
  1797. /* Dispose of buffer - no longer needed. */
  1798. XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1799. if (ret == 1) {
  1800. /* Truncate to requested bit length. */
  1801. mp_rshb((mp_int*)bn->internal, 8 - (bits % 8));
  1802. }
  1803. /* Set top bit when required. */
  1804. if ((ret == 1) && (top >= WOLFSSL_BN_RAND_TOP_ONE) &&
  1805. (mp_set_bit((mp_int*)bn->internal, bits - 1) != MP_OKAY)) {
  1806. WOLFSSL_MSG("Failed to set top bit");
  1807. ret = 0;
  1808. }
  1809. /* Set second top bit when required. */
  1810. if ((ret == 1) && (top > WOLFSSL_BN_RAND_TOP_ONE) &&
  1811. (mp_set_bit((mp_int*)bn->internal, bits - 2) != MP_OKAY)) {
  1812. WOLFSSL_MSG("Failed to set second top bit");
  1813. ret = 0;
  1814. }
  1815. /* Set bottom bit when required. */
  1816. if ((ret == 1) && (bottom == WOLFSSL_BN_RAND_BOTTOM_ODD) &&
  1817. (mp_set_bit((mp_int*)bn->internal, 0) != MP_OKAY)) {
  1818. WOLFSSL_MSG("Failed to set 0th bit");
  1819. ret = 0;
  1820. }
  1821. }
  1822. WOLFSSL_LEAVE("wolfSSL_BN_rand", ret);
  1823. return ret;
  1824. }
  1825. /* Generates a pseudo-random number up to bits long.
  1826. *
  1827. * Implemented using wolfSSL_BN_rand().
  1828. *
  1829. * @param [in, out] bn Big number to generate into.
  1830. * @param [in] bits Number of bits in word.
  1831. * @param [in] top Whether top bits must be set.
  1832. * Valid values: WOLFSSL_BN_RAND_TOP_ANY,
  1833. * WOLFSSL_BN_RAND_TOP_ONE, WOLFSSL_BN_RAND_TOP_TWO.
  1834. * @param [in] bottom Whether bottom bit must be set.
  1835. * Valid values: WOLFSSL_BN_RAND_BOTTOM_ANY,
  1836. WOLFSSL_BN_RAND_BOTTOM_ODD.
  1837. * @return 1 on success.
  1838. * @return 0 when bn is NULL.
  1839. * @return 0 when bits is invalid.
  1840. * @return 0 when bits and top/bottom are invalid.
  1841. * @return 0 when generation fails.
  1842. */
  1843. int wolfSSL_BN_pseudo_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
  1844. {
  1845. return wolfSSL_BN_rand(bn, bits, top, bottom);
  1846. }
  1847. /* Maximum number of trials to attempt at generating a number in range. */
  1848. #define RANGE_MAX_TRIALS 100
  1849. /* Generate big number to be a value between 0 and range-1.
  1850. *
  1851. * N = length of range input var
  1852. *
  1853. * Generate N-bit length numbers until generated number is less than range
  1854. * @param [in] r Big number to generate into.
  1855. * @param [in] range The upper limit of generated number.
  1856. * @return 1 on success.
  1857. * @return 0 on failure.
  1858. */
  1859. int wolfSSL_BN_rand_range(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *range)
  1860. {
  1861. int ret = 1;
  1862. WOLFSSL_ENTER("wolfSSL_BN_rand_range");
  1863. /* Validate parameters. */
  1864. if (BN_IS_NULL(r) || BN_IS_NULL(range)) {
  1865. WOLFSSL_MSG("Bad parameter");
  1866. ret = 0;
  1867. }
  1868. if (ret == 1) {
  1869. /* Calculate number of bits in modulus. */
  1870. int n = wolfSSL_BN_num_bits(range);
  1871. if (n <= 1) {
  1872. /* Modulus is 0 or 1. */
  1873. wolfSSL_BN_zero(r);
  1874. }
  1875. else {
  1876. int i;
  1877. /* Try generating a number in range for a limited number of trials.
  1878. */
  1879. for (i = 0; (ret == 1) && (i < RANGE_MAX_TRIALS); i++) {
  1880. /* Generate a random number in range. */
  1881. if (wolfSSL_BN_pseudo_rand(r, n, WOLFSSL_BN_RAND_TOP_ANY,
  1882. WOLFSSL_BN_RAND_BOTTOM_ANY) == 0) {
  1883. WOLFSSL_MSG("wolfSSL_BN_rand error");
  1884. ret = 0;
  1885. }
  1886. /* Check if in range. */
  1887. else if (wolfSSL_BN_cmp(r, range) < 0) {
  1888. break;
  1889. }
  1890. }
  1891. /* Fail if max trial attempts made. */
  1892. if (i >= RANGE_MAX_TRIALS) {
  1893. WOLFSSL_MSG("wolfSSL_BN_rand_range too many iterations");
  1894. ret = 0;
  1895. }
  1896. }
  1897. }
  1898. return ret;
  1899. }
  1900. /*******************************************************************************
  1901. * Prime APIs
  1902. ******************************************************************************/
  1903. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  1904. !defined(NO_DSA))
  1905. /* Generate a prime number.
  1906. *
  1907. * @param [in, out] prime Big number to generate into.
  1908. * @param [in] bits Number of bits in generated number.
  1909. * @param [in] safe Whether number must be a safe prime.
  1910. * @param [in] add Value to add when generating. Not used.
  1911. * @param [in] rem Remainder of number modulo add. Not used.
  1912. * @param [in] cb Generation callback. Not used.
  1913. * @return 1 on success.
  1914. * @return 0 when prime is NULL.
  1915. * @return 0 when safe required or add or rem is not NULL.
  1916. * @return 0 on generation failure.
  1917. */
  1918. int wolfSSL_BN_generate_prime_ex(WOLFSSL_BIGNUM* prime, int bits,
  1919. int safe, const WOLFSSL_BIGNUM* add, const WOLFSSL_BIGNUM* rem,
  1920. WOLFSSL_BN_GENCB* cb)
  1921. {
  1922. int ret = 1;
  1923. #ifdef WOLFSSL_SMALL_STACK
  1924. WC_RNG* tmpRng = NULL;
  1925. #else
  1926. WC_RNG tmpRng[1];
  1927. #endif
  1928. WC_RNG* rng = NULL;
  1929. int localRng = 0;
  1930. /* Callback not used. */
  1931. (void)cb;
  1932. WOLFSSL_ENTER("wolfSSL_BN_generate_prime_ex");
  1933. /* Check unsupported parameters. */
  1934. if ((safe == 1) || (add != NULL) || (rem != NULL)) {
  1935. ret = 0;
  1936. }
  1937. /* Validate parameters. */
  1938. else if (BN_IS_NULL(prime)) {
  1939. ret = 0;
  1940. }
  1941. /* Create a new RNG or use global. */
  1942. if ((ret == 1) && ((rng = wolfssl_make_rng(tmpRng, &localRng)) == NULL)) {
  1943. ret = 0;
  1944. }
  1945. /* Use wolfCrypt to generate a prime. */
  1946. if ((ret == 1) && (mp_rand_prime((mp_int*)prime->internal, (bits + 7) / 8,
  1947. rng, NULL) != MP_OKAY)) {
  1948. ret = 0;
  1949. }
  1950. if (localRng) {
  1951. /* Dispose of local RNG that was created. */
  1952. wc_FreeRng(rng);
  1953. #ifdef WOLFSSL_SMALL_STACK
  1954. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  1955. #endif
  1956. }
  1957. WOLFSSL_LEAVE("wolfSSL_BN_generate_prime_ex", ret);
  1958. return ret;
  1959. }
  1960. /* Check whether a big number is prime.
  1961. *
  1962. * Return code compliant with OpenSSL.
  1963. *
  1964. * @param [in] bn Big number to check.
  1965. * @param [in] checks Number of Miller-Rabin tests to perform.
  1966. * @param [in] ctx BN context. Not used.
  1967. * @param [in] cb Generation callback. Not used.
  1968. * @return 1 when number is prime.
  1969. * @return 0 when number is not prime.
  1970. * @return -1 when bn is NULL or failure when checking.
  1971. */
  1972. int wolfSSL_BN_is_prime_ex(const WOLFSSL_BIGNUM *bn, int checks,
  1973. WOLFSSL_BN_CTX *ctx, WOLFSSL_BN_GENCB *cb)
  1974. {
  1975. int ret = 1;
  1976. WC_RNG* rng = NULL;
  1977. #ifdef WOLFSSL_SMALL_STACK
  1978. WC_RNG* tmpRng = NULL;
  1979. #else
  1980. WC_RNG tmpRng[1];
  1981. #endif
  1982. int localRng = 0;
  1983. int res = MP_NO;
  1984. /* BN context not needed. */
  1985. (void)ctx;
  1986. (void)cb;
  1987. WOLFSSL_ENTER("wolfSSL_BN_is_prime_ex");
  1988. if (BN_IS_NULL(bn)) {
  1989. WOLFSSL_MSG("bn NULL error");
  1990. ret = -1;
  1991. }
  1992. /* Create a new RNG or use global. */
  1993. if ((ret == 1) && ((rng = wolfssl_make_rng(tmpRng, &localRng)) == NULL)) {
  1994. ret = -1;
  1995. }
  1996. if ((ret == 1) && (mp_prime_is_prime_ex((mp_int*)bn->internal, checks, &res,
  1997. rng) != MP_OKAY)) {
  1998. WOLFSSL_MSG("mp_prime_is_prime_ex error");
  1999. ret = -1;
  2000. }
  2001. if (localRng) {
  2002. wc_FreeRng(rng);
  2003. #ifdef WOLFSSL_SMALL_STACK
  2004. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  2005. #endif
  2006. }
  2007. if ((ret != -1) && (res != MP_YES)) {
  2008. WOLFSSL_MSG("mp_prime_is_prime_ex not prime");
  2009. ret = 0;
  2010. }
  2011. return ret;
  2012. }
  2013. #endif /* WOLFSSL_KEY_GEN && (!NO_RSA || !NO_DH || !NO_DSA) */
  2014. /*******************************************************************************
  2015. * Print APIs
  2016. ******************************************************************************/
  2017. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  2018. /* Print big number to file pointer.
  2019. *
  2020. * Return code compliant with OpenSSL.
  2021. *
  2022. * @param [in] fp File pointer.
  2023. * @param [in] bn Big number to print.
  2024. * @return 1 on success.
  2025. * @return 0 when fp is a bad file pointer.
  2026. * @return 0 when bn is NULL.
  2027. * @return 0 when creating hex string fails.
  2028. * @return 0 when printing fails.
  2029. */
  2030. int wolfSSL_BN_print_fp(XFILE fp, const WOLFSSL_BIGNUM *bn)
  2031. {
  2032. int ret = 1;
  2033. char* buf = NULL;
  2034. WOLFSSL_ENTER("wolfSSL_BN_print_fp");
  2035. /* Validate parameters. */
  2036. if ((fp == XBADFILE) || BN_IS_NULL(bn)) {
  2037. WOLFSSL_MSG("bn NULL error");
  2038. ret = 0;
  2039. }
  2040. /* Create a hex string of big number. */
  2041. if ((ret == 1) && ((buf = wolfSSL_BN_bn2hex(bn)) == NULL)) {
  2042. WOLFSSL_MSG("wolfSSL_BN_bn2hex failure");
  2043. ret = 0;
  2044. }
  2045. /* Print hex string to file pointer. */
  2046. if ((ret == 1) && (XFPRINTF(fp, "%s", buf) < 0)) {
  2047. ret = 0;
  2048. }
  2049. /* Dispose of any allocated data. */
  2050. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  2051. return ret;
  2052. }
  2053. #endif /* !NO_FILESYSTEM && XFPRINTF */
  2054. /*******************************************************************************
  2055. * BN_CTX APIs
  2056. ******************************************************************************/
  2057. /* Allocate and return a new BN context object.
  2058. *
  2059. * BN context not needed for operations.
  2060. *
  2061. * @return Pointer to dummy object.
  2062. */
  2063. WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void)
  2064. {
  2065. /* wolfcrypt doesn't need BN context. */
  2066. static int ctx;
  2067. WOLFSSL_ENTER("wolfSSL_BN_CTX_new");
  2068. return (WOLFSSL_BN_CTX*)&ctx;
  2069. }
  2070. /* Initialize a BN context object.
  2071. *
  2072. * BN context not needed for operations.
  2073. *
  2074. * @param [in] ctx Dummy BN context.
  2075. */
  2076. void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
  2077. {
  2078. (void)ctx;
  2079. WOLFSSL_ENTER("wolfSSL_BN_CTX_init");
  2080. }
  2081. /* Free a BN context object.
  2082. *
  2083. * BN context not needed for operations.
  2084. *
  2085. * @param [in] ctx Dummy BN context.
  2086. */
  2087. void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx)
  2088. {
  2089. (void)ctx;
  2090. WOLFSSL_ENTER("wolfSSL_BN_CTX_free");
  2091. /* Don't do anything since using dummy, static BN context. */
  2092. }
  2093. /* Get a big number based on the BN context.
  2094. *
  2095. * @param [in] ctx BN context. Not used.
  2096. * @return Big number on success.
  2097. * @return NULL on failure.
  2098. */
  2099. WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx)
  2100. {
  2101. /* ctx is not used - returning a new big number. */
  2102. (void)ctx;
  2103. WOLFSSL_ENTER("wolfSSL_BN_CTX_get");
  2104. /* Return a new big number. */
  2105. return wolfSSL_BN_new();
  2106. }
  2107. #ifndef NO_WOLFSSL_STUB
  2108. /* Start stack of temporary big numbers.
  2109. *
  2110. * Newly allocated big numbers are returned instead of having a stack.
  2111. *
  2112. * @param [in] ctx BN context. Not used.
  2113. */
  2114. void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx)
  2115. {
  2116. (void)ctx;
  2117. WOLFSSL_ENTER("wolfSSL_BN_CTX_start");
  2118. WOLFSSL_STUB("BN_CTX_start");
  2119. WOLFSSL_MSG("wolfSSL_BN_CTX_start TBD");
  2120. }
  2121. #endif
  2122. #endif /* OPENSSL_EXTRA */
  2123. #endif /* !WOLFSSL_SSL_BN_INCLUDED */