ssl_bn.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  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. WOLFSSL_BIGNUM* res = dv;
  1498. /* BN context not needed. */
  1499. (void)ctx;
  1500. WOLFSSL_ENTER("wolfSSL_BN_div");
  1501. if (BN_IS_NULL(res)) {
  1502. res = wolfSSL_BN_new();
  1503. }
  1504. /* Validate parameters. */
  1505. if (BN_IS_NULL(res) || BN_IS_NULL(rem) || BN_IS_NULL(a) || BN_IS_NULL(d)) {
  1506. ret = 0;
  1507. }
  1508. /* Have wolfCrypt perform operation with internal representations. */
  1509. if ((ret == 1) && (mp_div((mp_int*)a->internal, (mp_int*)d->internal,
  1510. (mp_int*)res->internal, (mp_int*)rem->internal) != MP_OKAY)) {
  1511. ret = 0;
  1512. }
  1513. if (res != dv)
  1514. wolfSSL_BN_free(res);
  1515. WOLFSSL_LEAVE("wolfSSL_BN_div", ret);
  1516. return ret;
  1517. }
  1518. #endif
  1519. /* Calculate a mod b into r. r = a % b
  1520. *
  1521. * @param [out] r Big number to put result into.
  1522. * @param [in] a Big number to reduced
  1523. * @param [in] b Big number to reduce with.
  1524. * @param [in] ctx BN context object. Unused.
  1525. *
  1526. * @return 1 on success.
  1527. * @return 0 when r, a or b is NULL.
  1528. * @return 0 on failure.
  1529. */
  1530. int wolfSSL_BN_mod(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
  1531. const WOLFSSL_BIGNUM* b, const WOLFSSL_BN_CTX* c)
  1532. {
  1533. int ret = 1;
  1534. (void)c;
  1535. WOLFSSL_ENTER("wolfSSL_BN_mod");
  1536. /* Validate parameters. */
  1537. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
  1538. ret = 0;
  1539. }
  1540. /* Have wolfCrypt perform operation with internal representations. */
  1541. if ((ret == 1) && (mp_mod((mp_int*)a->internal,(mp_int*)b->internal,
  1542. (mp_int*)r->internal) != MP_OKAY)) {
  1543. ret = 0;
  1544. }
  1545. WOLFSSL_LEAVE("wolfSSL_BN_mod mp_mod", ret);
  1546. return ret;
  1547. }
  1548. /*******************************************************************************
  1549. * Math and Mod APIs
  1550. ******************************************************************************/
  1551. #ifndef WOLFSSL_SP_MATH
  1552. /* Add a to b modulo m into r. r = a + b (mod m)
  1553. *
  1554. * @param [in, out] r Big number to hold result.
  1555. * @param [in] a Big number to add to.
  1556. * @param [in] b Big number to add with.
  1557. * @param [in] m Big number that is the modulus.
  1558. * @param [in] ctx BN context. Not used.
  1559. * @return 1 on success.
  1560. * @return 0 when r, a or b or internal representation is NULL.
  1561. * @return 0 on calculation failure.
  1562. */
  1563. int wolfSSL_BN_mod_add(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
  1564. const WOLFSSL_BIGNUM *b, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  1565. {
  1566. int ret = 1;
  1567. /* BN context not needed. */
  1568. (void)ctx;
  1569. WOLFSSL_ENTER("wolfSSL_BN_add");
  1570. /* Validate parameters. */
  1571. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b) || BN_IS_NULL(m)) {
  1572. WOLFSSL_MSG("bn NULL error");
  1573. ret = 0;
  1574. }
  1575. /* Perform operation with wolfCrypt. */
  1576. if ((ret == 1) && (mp_addmod((mp_int*)a->internal, (mp_int*)b->internal,
  1577. (mp_int*)m->internal, (mp_int*)r->internal) != MP_OKAY)) {
  1578. WOLFSSL_MSG("mp_add_d error");
  1579. ret = 0;
  1580. }
  1581. return ret;
  1582. }
  1583. #endif
  1584. /* Calculate a multiplied by b, mod m into r. r = (a * b) % m
  1585. *
  1586. * @param [out] r Big number to put result into.
  1587. * @param [in] a Base as a big number.
  1588. * @param [in] b Multiplier as a big number.
  1589. * @param [in] m Modulus as a big number.
  1590. * @param [in] ctx BN context object. Unused.
  1591. *
  1592. * @return 1 on success.
  1593. * @return 0 when r, a, b or m is NULL.
  1594. * @return 0 on failure.
  1595. */
  1596. int wolfSSL_BN_mod_mul(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
  1597. const WOLFSSL_BIGNUM *b, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  1598. {
  1599. int ret = 1;
  1600. /* BN context not needed. */
  1601. (void)ctx;
  1602. WOLFSSL_ENTER("wolfSSL_BN_mod_mul");
  1603. /* Validate parameters. */
  1604. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b) || BN_IS_NULL(m)) {
  1605. WOLFSSL_MSG("Bad Argument");
  1606. ret = 0;
  1607. }
  1608. /* Have wolfCrypt perform operation with internal representations. */
  1609. if ((ret == 1) && (mp_mulmod((mp_int*)a->internal, (mp_int*)b->internal,
  1610. (mp_int*)m->internal, (mp_int*)r->internal)) != MP_OKAY) {
  1611. ret = 0;
  1612. }
  1613. WOLFSSL_LEAVE("wolfSSL_BN_mod_mul", ret);
  1614. return ret;
  1615. }
  1616. /* Calculate a to the power of e, mod m into r. r = (a ^ e) % m
  1617. *
  1618. * @param [out] r Big number to put result into.
  1619. * @param [in] a Base as a big number.
  1620. * @param [in] e Exponent as a big number.
  1621. * @param [in] m Modulus as a big number.
  1622. * @param [in] ctx BN context object. Unused.
  1623. *
  1624. * @return 1 on success.
  1625. * @return 0 when r, a, p or m is NULL.
  1626. * @return 0 on failure.
  1627. */
  1628. int wolfSSL_BN_mod_exp(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
  1629. const WOLFSSL_BIGNUM *e, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  1630. {
  1631. int ret = 1;
  1632. /* BN context not needed. */
  1633. (void)ctx;
  1634. WOLFSSL_ENTER("wolfSSL_BN_mod_exp");
  1635. /* Validate parameters. */
  1636. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(e) || BN_IS_NULL(m)) {
  1637. WOLFSSL_MSG("Bad Argument");
  1638. ret = 0;
  1639. }
  1640. /* Have wolfCrypt perform operation with internal representations. */
  1641. if ((ret == 1) && (mp_exptmod((mp_int*)a->internal, (mp_int*)e->internal,
  1642. (mp_int*)m->internal, (mp_int*)r->internal)) != MP_OKAY) {
  1643. ret = 0;
  1644. }
  1645. WOLFSSL_LEAVE("wolfSSL_BN_mod_exp", ret);
  1646. return ret;
  1647. }
  1648. /* Calculate the modular inverse of a mod m into r. r = a^-1 mod m
  1649. *
  1650. * A new big number is allocated when r is NULL.
  1651. *
  1652. * @param [in, out] r Big number to hold result. May be NULL.
  1653. * @param [in] a Big number to invert.
  1654. * @param [in] m Big number that is the modulus.
  1655. * @param [in] ctx BN context. Not used.
  1656. * @return Big number holding result on success.
  1657. * @return NULL on failure.
  1658. */
  1659. WOLFSSL_BIGNUM *wolfSSL_BN_mod_inverse(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a,
  1660. const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  1661. {
  1662. int err = 0;
  1663. WOLFSSL_BIGNUM* t = NULL;
  1664. /* BN context not needed. */
  1665. (void)ctx;
  1666. WOLFSSL_ENTER("wolfSSL_BN_mod_inverse");
  1667. /* Validate parameters. */
  1668. if (BN_IS_NULL(a) || BN_IS_NULL(m) || ((r != NULL) &&
  1669. (r->internal == NULL))) {
  1670. WOLFSSL_MSG("a or n NULL error");
  1671. err = 1;
  1672. }
  1673. /* Check whether we have a result big number. */
  1674. if ((!err) && (r == NULL)) {
  1675. /* Allocate a new big number to hold result and be returned. */
  1676. t = wolfSSL_BN_new();
  1677. if (t == NULL){
  1678. WOLFSSL_MSG("WolfSSL_BN_new() failed");
  1679. err = 1;
  1680. }
  1681. r = t;
  1682. }
  1683. /* Compute inverse of a modulo n and return in r */
  1684. if ((!err) && (mp_invmod((mp_int *)a->internal, (mp_int *)m->internal,
  1685. (mp_int*)r->internal) != MP_OKAY)) {
  1686. WOLFSSL_MSG("mp_invmod() error");
  1687. err = 1;
  1688. }
  1689. if (err) {
  1690. wolfSSL_BN_free(t);
  1691. r = NULL;
  1692. }
  1693. return r;
  1694. }
  1695. /*******************************************************************************
  1696. * Other Math APIs
  1697. ******************************************************************************/
  1698. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  1699. /* Needed to get mp_gcd. */
  1700. /* Greatest Common Divisor (GCM) of a and b into r. r = GCD(a, b)
  1701. *
  1702. * @param [out] r Big number to put result into.
  1703. * @param [in] a First big number.
  1704. * @param [in] b Second big number.
  1705. * @param [in] ctx BN context object. Unused.
  1706. *
  1707. * @return 1 on success.
  1708. * @return 0 when r, a or b is NULL.
  1709. * @return 0 when internal representation of r, a or b is NULL.
  1710. * @return 0 on failure.
  1711. */
  1712. int wolfSSL_BN_gcd(WOLFSSL_BIGNUM* r, WOLFSSL_BIGNUM* a, WOLFSSL_BIGNUM* b,
  1713. WOLFSSL_BN_CTX* ctx)
  1714. {
  1715. int ret = 1;
  1716. /* BN context not needed. */
  1717. (void)ctx;
  1718. WOLFSSL_ENTER("wolfSSL_BN_gcd");
  1719. /* Validate parameters. */
  1720. if (BN_IS_NULL(r) || BN_IS_NULL(a) || BN_IS_NULL(b)) {
  1721. ret = 0;
  1722. }
  1723. /* Have wolfCrypt perform operation with internal representations. */
  1724. if ((ret == 1) && (mp_gcd((mp_int*)a->internal, (mp_int*)b->internal,
  1725. (mp_int*)r->internal) != MP_OKAY)) {
  1726. ret = 0;
  1727. }
  1728. WOLFSSL_LEAVE("wolfSSL_BN_gcd", ret);
  1729. return ret;
  1730. }
  1731. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  1732. /*******************************************************************************
  1733. * Random APIs
  1734. ******************************************************************************/
  1735. /* Generates a random number up to bits long.
  1736. *
  1737. * @param [in, out] bn Big number to generate into.
  1738. * @param [in] bits Number of bits in word.
  1739. * @param [in] top Whether top bits must be set.
  1740. * Valid values: WOLFSSL_BN_RAND_TOP_ANY,
  1741. * WOLFSSL_BN_RAND_TOP_ONE, WOLFSSL_BN_RAND_TOP_TWO.
  1742. * @param [in] bottom Whether bottom bit must be set.
  1743. * Valid values: WOLFSSL_BN_RAND_BOTTOM_ANY,
  1744. WOLFSSL_BN_RAND_BOTTOM_ODD.
  1745. * @return 1 on success.
  1746. * @return 0 when bn is NULL.
  1747. * @return 0 when bits is invalid.
  1748. * @return 0 when bits and top/bottom are invalid.
  1749. * @return 0 when generation fails.
  1750. */
  1751. int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
  1752. {
  1753. int ret = 1;
  1754. word32 len = (word32)((bits + 7) / 8);
  1755. WC_RNG* rng;
  1756. WOLFSSL_ENTER("wolfSSL_BN_rand");
  1757. /* Validate parameters. */
  1758. if (BN_IS_NULL(bn)) {
  1759. WOLFSSL_MSG("Bad argument - bn");
  1760. ret = 0;
  1761. }
  1762. else if (bits < 0) {
  1763. WOLFSSL_MSG("Bad argument - bits < 0");
  1764. ret = 0;
  1765. }
  1766. else if ((bits == 0) && ((bottom != WOLFSSL_BN_RAND_BOTTOM_ANY) ||
  1767. (top != WOLFSSL_BN_RAND_TOP_ANY))) {
  1768. WOLFSSL_MSG("Bad top/bottom - bits == 0");
  1769. ret = 0;
  1770. }
  1771. else if ((bits == 1) && (top > WOLFSSL_BN_RAND_TOP_ONE)) {
  1772. WOLFSSL_MSG("Bad top - bits == 1");
  1773. ret = 0;
  1774. }
  1775. /* Handle simple case of zero bits. */
  1776. if ((ret == 1) && (bits == 0)) {
  1777. mp_zero((mp_int*)bn->internal);
  1778. }
  1779. else if (ret == 1) {
  1780. byte* buff = NULL;
  1781. /* Get random to global random to generate bits. */
  1782. if ((rng = wolfssl_make_global_rng()) == NULL) {
  1783. WOLFSSL_MSG("Failed to use global RNG.");
  1784. ret = 0;
  1785. }
  1786. /* Allocate buffer to hold generated bits. */
  1787. if ((ret == 1) && ((buff = (byte*)XMALLOC(len, NULL,
  1788. DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  1789. WOLFSSL_MSG("Failed to allocate buffer.");
  1790. ret = 0;
  1791. }
  1792. /* Generate bytes to cover bits. */
  1793. if ((ret == 1) && wc_RNG_GenerateBlock(rng, buff, len) != 0) {
  1794. WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
  1795. ret = 0;
  1796. }
  1797. /* Read bytes in to big number. */
  1798. if ((ret == 1) && mp_read_unsigned_bin((mp_int*)bn->internal, buff, len)
  1799. != MP_OKAY) {
  1800. WOLFSSL_MSG("mp_read_unsigned_bin failed");
  1801. ret = 0;
  1802. }
  1803. /* Dispose of buffer - no longer needed. */
  1804. XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1805. if (ret == 1) {
  1806. /* Truncate to requested bit length. */
  1807. mp_rshb((mp_int*)bn->internal, 8 - (bits % 8));
  1808. }
  1809. /* Set top bit when required. */
  1810. if ((ret == 1) && (top >= WOLFSSL_BN_RAND_TOP_ONE) &&
  1811. (mp_set_bit((mp_int*)bn->internal, bits - 1) != MP_OKAY)) {
  1812. WOLFSSL_MSG("Failed to set top bit");
  1813. ret = 0;
  1814. }
  1815. /* Set second top bit when required. */
  1816. if ((ret == 1) && (top > WOLFSSL_BN_RAND_TOP_ONE) &&
  1817. (mp_set_bit((mp_int*)bn->internal, bits - 2) != MP_OKAY)) {
  1818. WOLFSSL_MSG("Failed to set second top bit");
  1819. ret = 0;
  1820. }
  1821. /* Set bottom bit when required. */
  1822. if ((ret == 1) && (bottom == WOLFSSL_BN_RAND_BOTTOM_ODD) &&
  1823. (mp_set_bit((mp_int*)bn->internal, 0) != MP_OKAY)) {
  1824. WOLFSSL_MSG("Failed to set 0th bit");
  1825. ret = 0;
  1826. }
  1827. }
  1828. WOLFSSL_LEAVE("wolfSSL_BN_rand", ret);
  1829. return ret;
  1830. }
  1831. /* Generates a pseudo-random number up to bits long.
  1832. *
  1833. * Implemented using wolfSSL_BN_rand().
  1834. *
  1835. * @param [in, out] bn Big number to generate into.
  1836. * @param [in] bits Number of bits in word.
  1837. * @param [in] top Whether top bits must be set.
  1838. * Valid values: WOLFSSL_BN_RAND_TOP_ANY,
  1839. * WOLFSSL_BN_RAND_TOP_ONE, WOLFSSL_BN_RAND_TOP_TWO.
  1840. * @param [in] bottom Whether bottom bit must be set.
  1841. * Valid values: WOLFSSL_BN_RAND_BOTTOM_ANY,
  1842. WOLFSSL_BN_RAND_BOTTOM_ODD.
  1843. * @return 1 on success.
  1844. * @return 0 when bn is NULL.
  1845. * @return 0 when bits is invalid.
  1846. * @return 0 when bits and top/bottom are invalid.
  1847. * @return 0 when generation fails.
  1848. */
  1849. int wolfSSL_BN_pseudo_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
  1850. {
  1851. return wolfSSL_BN_rand(bn, bits, top, bottom);
  1852. }
  1853. /* Maximum number of trials to attempt at generating a number in range. */
  1854. #define RANGE_MAX_TRIALS 100
  1855. /* Generate big number to be a value between 0 and range-1.
  1856. *
  1857. * N = length of range input var
  1858. *
  1859. * Generate N-bit length numbers until generated number is less than range
  1860. * @param [in] r Big number to generate into.
  1861. * @param [in] range The upper limit of generated number.
  1862. * @return 1 on success.
  1863. * @return 0 on failure.
  1864. */
  1865. int wolfSSL_BN_rand_range(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *range)
  1866. {
  1867. int ret = 1;
  1868. WOLFSSL_ENTER("wolfSSL_BN_rand_range");
  1869. /* Validate parameters. */
  1870. if (BN_IS_NULL(r) || BN_IS_NULL(range)) {
  1871. WOLFSSL_MSG("Bad parameter");
  1872. ret = 0;
  1873. }
  1874. if (ret == 1) {
  1875. /* Calculate number of bits in modulus. */
  1876. int n = wolfSSL_BN_num_bits(range);
  1877. if (n <= 1) {
  1878. /* Modulus is 0 or 1. */
  1879. wolfSSL_BN_zero(r);
  1880. }
  1881. else {
  1882. int i;
  1883. /* Try generating a number in range for a limited number of trials.
  1884. */
  1885. for (i = 0; (ret == 1) && (i < RANGE_MAX_TRIALS); i++) {
  1886. /* Generate a random number in range. */
  1887. if (wolfSSL_BN_pseudo_rand(r, n, WOLFSSL_BN_RAND_TOP_ANY,
  1888. WOLFSSL_BN_RAND_BOTTOM_ANY) == 0) {
  1889. WOLFSSL_MSG("wolfSSL_BN_rand error");
  1890. ret = 0;
  1891. }
  1892. /* Check if in range. */
  1893. else if (wolfSSL_BN_cmp(r, range) < 0) {
  1894. break;
  1895. }
  1896. }
  1897. /* Fail if max trial attempts made. */
  1898. if (i >= RANGE_MAX_TRIALS) {
  1899. WOLFSSL_MSG("wolfSSL_BN_rand_range too many iterations");
  1900. ret = 0;
  1901. }
  1902. }
  1903. }
  1904. return ret;
  1905. }
  1906. /*******************************************************************************
  1907. * Prime APIs
  1908. ******************************************************************************/
  1909. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  1910. !defined(NO_DSA))
  1911. /* Generate a prime number.
  1912. *
  1913. * @param [in, out] prime Big number to generate into.
  1914. * @param [in] bits Number of bits in generated number.
  1915. * @param [in] safe Whether number must be a safe prime.
  1916. * @param [in] add Value to add when generating. Not used.
  1917. * @param [in] rem Remainder of number modulo add. Not used.
  1918. * @param [in] cb Generation callback. Not used.
  1919. * @return 1 on success.
  1920. * @return 0 when prime is NULL.
  1921. * @return 0 when safe required or add or rem is not NULL.
  1922. * @return 0 on generation failure.
  1923. */
  1924. int wolfSSL_BN_generate_prime_ex(WOLFSSL_BIGNUM* prime, int bits,
  1925. int safe, const WOLFSSL_BIGNUM* add, const WOLFSSL_BIGNUM* rem,
  1926. WOLFSSL_BN_GENCB* cb)
  1927. {
  1928. int ret = 1;
  1929. #ifdef WOLFSSL_SMALL_STACK
  1930. WC_RNG* tmpRng = NULL;
  1931. #else
  1932. WC_RNG tmpRng[1];
  1933. #endif
  1934. WC_RNG* rng = NULL;
  1935. int localRng = 0;
  1936. /* Callback not used. */
  1937. (void)cb;
  1938. WOLFSSL_ENTER("wolfSSL_BN_generate_prime_ex");
  1939. /* Check unsupported parameters. */
  1940. if ((safe == 1) || (add != NULL) || (rem != NULL)) {
  1941. ret = 0;
  1942. }
  1943. /* Validate parameters. */
  1944. else if (BN_IS_NULL(prime)) {
  1945. ret = 0;
  1946. }
  1947. /* Create a new RNG or use global. */
  1948. if ((ret == 1) && ((rng = wolfssl_make_rng(tmpRng, &localRng)) == NULL)) {
  1949. ret = 0;
  1950. }
  1951. /* Use wolfCrypt to generate a prime. */
  1952. if ((ret == 1) && (mp_rand_prime((mp_int*)prime->internal, (bits + 7) / 8,
  1953. rng, NULL) != MP_OKAY)) {
  1954. ret = 0;
  1955. }
  1956. if (localRng) {
  1957. /* Dispose of local RNG that was created. */
  1958. wc_FreeRng(rng);
  1959. #ifdef WOLFSSL_SMALL_STACK
  1960. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  1961. #endif
  1962. }
  1963. WOLFSSL_LEAVE("wolfSSL_BN_generate_prime_ex", ret);
  1964. return ret;
  1965. }
  1966. /* Check whether a big number is prime.
  1967. *
  1968. * Return code compliant with OpenSSL.
  1969. *
  1970. * @param [in] bn Big number to check.
  1971. * @param [in] checks Number of Miller-Rabin tests to perform.
  1972. * @param [in] ctx BN context. Not used.
  1973. * @param [in] cb Generation callback. Not used.
  1974. * @return 1 when number is prime.
  1975. * @return 0 when number is not prime.
  1976. * @return -1 when bn is NULL or failure when checking.
  1977. */
  1978. int wolfSSL_BN_is_prime_ex(const WOLFSSL_BIGNUM *bn, int checks,
  1979. WOLFSSL_BN_CTX *ctx, WOLFSSL_BN_GENCB *cb)
  1980. {
  1981. int ret = 1;
  1982. WC_RNG* rng = NULL;
  1983. #ifdef WOLFSSL_SMALL_STACK
  1984. WC_RNG* tmpRng = NULL;
  1985. #else
  1986. WC_RNG tmpRng[1];
  1987. #endif
  1988. int localRng = 0;
  1989. int res = MP_NO;
  1990. /* BN context not needed. */
  1991. (void)ctx;
  1992. (void)cb;
  1993. WOLFSSL_ENTER("wolfSSL_BN_is_prime_ex");
  1994. if (BN_IS_NULL(bn)) {
  1995. WOLFSSL_MSG("bn NULL error");
  1996. ret = -1;
  1997. }
  1998. /* Create a new RNG or use global. */
  1999. if ((ret == 1) && ((rng = wolfssl_make_rng(tmpRng, &localRng)) == NULL)) {
  2000. ret = -1;
  2001. }
  2002. if ((ret == 1) && (mp_prime_is_prime_ex((mp_int*)bn->internal, checks, &res,
  2003. rng) != MP_OKAY)) {
  2004. WOLFSSL_MSG("mp_prime_is_prime_ex error");
  2005. ret = -1;
  2006. }
  2007. if (localRng) {
  2008. wc_FreeRng(rng);
  2009. #ifdef WOLFSSL_SMALL_STACK
  2010. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  2011. #endif
  2012. }
  2013. if ((ret != -1) && (res != MP_YES)) {
  2014. WOLFSSL_MSG("mp_prime_is_prime_ex not prime");
  2015. ret = 0;
  2016. }
  2017. return ret;
  2018. }
  2019. #endif /* WOLFSSL_KEY_GEN && (!NO_RSA || !NO_DH || !NO_DSA) */
  2020. /*******************************************************************************
  2021. * Print APIs
  2022. ******************************************************************************/
  2023. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  2024. /* Print big number to file pointer.
  2025. *
  2026. * Return code compliant with OpenSSL.
  2027. *
  2028. * @param [in] fp File pointer.
  2029. * @param [in] bn Big number to print.
  2030. * @return 1 on success.
  2031. * @return 0 when fp is a bad file pointer.
  2032. * @return 0 when bn is NULL.
  2033. * @return 0 when creating hex string fails.
  2034. * @return 0 when printing fails.
  2035. */
  2036. int wolfSSL_BN_print_fp(XFILE fp, const WOLFSSL_BIGNUM *bn)
  2037. {
  2038. int ret = 1;
  2039. char* buf = NULL;
  2040. WOLFSSL_ENTER("wolfSSL_BN_print_fp");
  2041. /* Validate parameters. */
  2042. if ((fp == XBADFILE) || BN_IS_NULL(bn)) {
  2043. WOLFSSL_MSG("bn NULL error");
  2044. ret = 0;
  2045. }
  2046. /* Create a hex string of big number. */
  2047. if ((ret == 1) && ((buf = wolfSSL_BN_bn2hex(bn)) == NULL)) {
  2048. WOLFSSL_MSG("wolfSSL_BN_bn2hex failure");
  2049. ret = 0;
  2050. }
  2051. /* Print hex string to file pointer. */
  2052. if ((ret == 1) && (XFPRINTF(fp, "%s", buf) < 0)) {
  2053. ret = 0;
  2054. }
  2055. /* Dispose of any allocated data. */
  2056. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  2057. return ret;
  2058. }
  2059. #endif /* !NO_FILESYSTEM && XFPRINTF */
  2060. /*******************************************************************************
  2061. * BN_CTX APIs
  2062. ******************************************************************************/
  2063. /* Allocate and return a new BN context object.
  2064. *
  2065. * BN context not needed for operations.
  2066. *
  2067. * @return Pointer to dummy object.
  2068. */
  2069. WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void)
  2070. {
  2071. /* wolfcrypt doesn't need BN context. */
  2072. static int ctx;
  2073. WOLFSSL_ENTER("wolfSSL_BN_CTX_new");
  2074. return (WOLFSSL_BN_CTX*)&ctx;
  2075. }
  2076. /* Initialize a BN context object.
  2077. *
  2078. * BN context not needed for operations.
  2079. *
  2080. * @param [in] ctx Dummy BN context.
  2081. */
  2082. void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
  2083. {
  2084. (void)ctx;
  2085. WOLFSSL_ENTER("wolfSSL_BN_CTX_init");
  2086. }
  2087. /* Free a BN context object.
  2088. *
  2089. * BN context not needed for operations.
  2090. *
  2091. * @param [in] ctx Dummy BN context.
  2092. */
  2093. void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx)
  2094. {
  2095. (void)ctx;
  2096. WOLFSSL_ENTER("wolfSSL_BN_CTX_free");
  2097. /* Don't do anything since using dummy, static BN context. */
  2098. }
  2099. /* Get a big number based on the BN context.
  2100. *
  2101. * @param [in] ctx BN context. Not used.
  2102. * @return Big number on success.
  2103. * @return NULL on failure.
  2104. */
  2105. WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx)
  2106. {
  2107. /* ctx is not used - returning a new big number. */
  2108. (void)ctx;
  2109. WOLFSSL_ENTER("wolfSSL_BN_CTX_get");
  2110. /* Return a new big number. */
  2111. return wolfSSL_BN_new();
  2112. }
  2113. #ifndef NO_WOLFSSL_STUB
  2114. /* Start stack of temporary big numbers.
  2115. *
  2116. * Newly allocated big numbers are returned instead of having a stack.
  2117. *
  2118. * @param [in] ctx BN context. Not used.
  2119. */
  2120. void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx)
  2121. {
  2122. (void)ctx;
  2123. WOLFSSL_ENTER("wolfSSL_BN_CTX_start");
  2124. WOLFSSL_STUB("BN_CTX_start");
  2125. WOLFSSL_MSG("wolfSSL_BN_CTX_start TBD");
  2126. }
  2127. #endif
  2128. #endif /* OPENSSL_EXTRA */
  2129. #endif /* !WOLFSSL_SSL_BN_INCLUDED */