rsa.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797
  1. /* rsa.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 /* configure options when using autoconf */
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/options.h>
  25. #include <wolfssl/wolfcrypt/types.h>
  26. #ifndef NO_RSA
  27. #define USER_CRYPTO_ERROR -101
  28. #ifdef OPENSSL_EXTRA
  29. #include <wolfssl/openssl/rsa.h> /* include for openssl compatibility */
  30. #include <wolfssl/openssl/bn.h>
  31. #endif
  32. #include "user_rsa.h"
  33. #ifdef DEBUG_WOLFSSL /* debug done without variadic to allow older compilers */
  34. #include <stdio.h>
  35. #define USER_DEBUG(x) printf x
  36. #else
  37. #define USER_DEBUG(x) WC_DO_NOTHING
  38. #endif
  39. #define ASN_INTEGER 0x02
  40. #define ASN_BIT_STRING 0x03
  41. #define ASN_TAG_NULL 0x05
  42. #define ASN_OBJECT_ID 0x06
  43. /* Make sure compiler doesn't skip -- used from wolfSSL */
  44. static inline void ForceZero(const void* mem, word32 len)
  45. {
  46. volatile byte* z = (volatile byte*)mem;
  47. while (len--) *z++ = 0;
  48. }
  49. enum {
  50. RSA_PUBLIC_ENCRYPT = 0,
  51. RSA_PUBLIC_DECRYPT = 1,
  52. RSA_PRIVATE_ENCRYPT = 2,
  53. RSA_PRIVATE_DECRYPT = 3,
  54. RSA_BLOCK_TYPE_1 = 1,
  55. RSA_BLOCK_TYPE_2 = 2,
  56. RSA_MIN_SIZE = 512,
  57. RSA_MAX_SIZE = 4096, /* max allowed in IPP library */
  58. RSA_MIN_PAD_SZ = 11 /* separator + 0 + pad value + 8 pads */
  59. };
  60. int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
  61. {
  62. USER_DEBUG(("Entering wc_InitRsaKey\n"));
  63. if (key == NULL)
  64. return USER_CRYPTO_ERROR;
  65. /* set full struct as 0 */
  66. ForceZero(key, sizeof(RsaKey));
  67. USER_DEBUG(("\tExit wc_InitRsaKey\n"));
  68. (void)devId;
  69. (void)heap;
  70. return 0;
  71. }
  72. int wc_InitRsaKey(RsaKey* key, void* heap)
  73. {
  74. return wc_InitRsaKey_ex(key, heap, INVALID_DEVID);
  75. }
  76. /* three functions needed for cert and key gen */
  77. #if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)
  78. /* return 1 if there is a leading bit*/
  79. int wc_Rsa_leading_bit(void* bn)
  80. {
  81. int ret = 0;
  82. int dataSz;
  83. Ipp32u* data;
  84. Ipp32u q;
  85. int qSz = sizeof(Ipp32u);
  86. if (ippsExtGet_BN(NULL, &dataSz, NULL, bn) != ippStsNoErr) {
  87. USER_DEBUG(("ippsExtGet_BN Rsa leading bit error\n"));
  88. return USER_CRYPTO_ERROR;
  89. }
  90. /* convert from size in binary to Ipp32u */
  91. dataSz = dataSz / 32 + ((dataSz % 32)? 1 : 0);
  92. data = (Ipp32u*)XMALLOC(dataSz * sizeof(Ipp32u), NULL,
  93. DYNAMIC_TYPE_USER_CRYPTO);
  94. if (data == NULL) {
  95. USER_DEBUG(("Rsa leading bit memory error\n"));
  96. return 0;
  97. }
  98. /* extract value from BN */
  99. if (ippsExtGet_BN(NULL, NULL, data, bn) != ippStsNoErr) {
  100. USER_DEBUG(("Rsa leading bit error\n"));
  101. XFREE(data, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  102. return 0;
  103. }
  104. /* use method like what's used in wolfssl tfm.c */
  105. q = data[dataSz - 1];
  106. ret = 0;
  107. while (qSz > 0) {
  108. if (q != 0)
  109. ret = (q & 0x80) != 0;
  110. q >>= 8;
  111. qSz--;
  112. }
  113. XFREE(data, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  114. return ret;
  115. }
  116. /* get the size in bytes of BN */
  117. int wc_Rsa_unsigned_bin_size(void* bn)
  118. {
  119. int ret = 0;
  120. if (ippsExtGet_BN(NULL, &ret, NULL, bn) != ippStsNoErr) {
  121. USER_DEBUG(("Rsa unsigned bin size error\n"));
  122. return USER_CRYPTO_ERROR;
  123. }
  124. return (ret / 8) + ((ret % 8)? 1: 0); /* size in bytes */
  125. }
  126. #ifndef MP_OKAY
  127. #define MP_OKAY 0
  128. #endif
  129. /* extract the bn value to a unsigned byte array and return MP_OKAY on success */
  130. int wc_Rsa_to_unsigned_bin(void* bn, byte* in, int inLen)
  131. {
  132. if (ippsGetOctString_BN((Ipp8u*)in, inLen, bn) != ippStsNoErr) {
  133. USER_DEBUG(("Rsa to unsigned bin error\n"));
  134. return USER_CRYPTO_ERROR;
  135. }
  136. return MP_OKAY;
  137. }
  138. #endif /* WOLFSSL_CERT_GEN || WOLFSSL_KEY_GEN || OPENSSL_EXTRA */
  139. #ifdef OPENSSL_EXTRA /* functions needed for openssl compatibility layer */
  140. static int SetIndividualExternal(WOLFSSL_BIGNUM** bn, IppsBigNumState* in)
  141. {
  142. IppStatus ret;
  143. byte* data;
  144. int sz;
  145. USER_DEBUG(("Entering SetIndividualExternal\n"));
  146. if (bn == NULL || in == NULL) {
  147. USER_DEBUG(("inputs NULL error\n"));
  148. return USER_CRYPTO_ERROR;
  149. }
  150. if (*bn == NULL) {
  151. *bn = wolfSSL_BN_new();
  152. if (*bn == NULL) {
  153. USER_DEBUG(("SetIndividualExternal alloc failed\n"));
  154. return USER_CRYPTO_ERROR;
  155. }
  156. }
  157. /* get size of array needed and extract oct array of data */
  158. ret = ippsGetSize_BN(in, &sz);
  159. if (ret != ippStsNoErr)
  160. return USER_CRYPTO_ERROR;
  161. data = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  162. if (data == NULL)
  163. return USER_CRYPTO_ERROR;
  164. ret = ippsGetOctString_BN(data, sz, in);
  165. if (ret != ippStsNoErr) {
  166. XFREE(data, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  167. return USER_CRYPTO_ERROR;
  168. }
  169. /* store the data into a wolfSSL Big Number */
  170. *bn = wolfSSL_BN_bin2bn(data, sz, *bn);
  171. XFREE(data, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  172. return 0;
  173. }
  174. static int SetIndividualInternal(WOLFSSL_BIGNUM* bn, IppsBigNumState** mpi)
  175. {
  176. int length, ctxSz, sz;
  177. IppStatus ret;
  178. Ipp8u* data;
  179. USER_DEBUG(("Entering SetIndividualInternal\n"));
  180. if (bn == NULL || bn->internal == NULL) {
  181. USER_DEBUG(("bn NULL error\n"));
  182. return USER_CRYPTO_ERROR;
  183. }
  184. length = wolfSSL_BN_num_bytes(bn);
  185. /* if not IPP BN then create one */
  186. if (*mpi == NULL) {
  187. ret = ippsBigNumGetSize(length, &ctxSz);
  188. if (ret != ippStsNoErr)
  189. return USER_CRYPTO_ERROR;
  190. *mpi = (IppsBigNumState*)XMALLOC(ctxSz, 0, DYNAMIC_TYPE_USER_CRYPTO);
  191. if (*mpi == NULL)
  192. return USER_CRYPTO_ERROR;
  193. ret = ippsBigNumInit(length, *mpi);
  194. if (ret != ippStsNoErr) {
  195. XFREE(*mpi, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  196. return USER_CRYPTO_ERROR;
  197. }
  198. }
  199. /* get the size of array needed and check IPP BigNum */
  200. if (ippsGetSize_BN(*mpi, &sz) != ippStsNoErr)
  201. return USER_CRYPTO_ERROR;
  202. if (sz < length) {
  203. USER_DEBUG(("big num size is too small\n"));
  204. return USER_CRYPTO_ERROR;
  205. }
  206. data = (Ipp8u*)XMALLOC(length, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  207. if (data == NULL)
  208. return USER_CRYPTO_ERROR;
  209. /* extract the wolfSSL BigNum and store it into IPP BigNum */
  210. if (wolfSSL_BN_bn2bin(bn, data) < 0) {
  211. XFREE(data, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  212. USER_DEBUG(("error in getting bin from wolfssl bn\n"));
  213. return USER_CRYPTO_ERROR;
  214. }
  215. ret = ippsSetOctString_BN(data, length, *mpi);
  216. if (ret != ippStsNoErr) {
  217. XFREE(data, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  218. return USER_CRYPTO_ERROR;
  219. }
  220. XFREE(data, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  221. return 0;
  222. }
  223. /* WolfSSL -> OpenSSL */
  224. int SetRsaExternal(WOLFSSL_RSA* rsa)
  225. {
  226. RsaKey* key;
  227. USER_DEBUG(("Entering SetRsaExternal\n"));
  228. if (rsa == NULL || rsa->internal == NULL) {
  229. USER_DEBUG(("rsa key NULL error\n"));
  230. return USER_CRYPTO_ERROR;
  231. }
  232. key = (RsaKey*)rsa->internal;
  233. if (SetIndividualExternal(&rsa->n, key->n) != 0) {
  234. USER_DEBUG(("rsa n key error\n"));
  235. return USER_CRYPTO_ERROR;
  236. }
  237. if (SetIndividualExternal(&rsa->e, key->e) != 0) {
  238. USER_DEBUG(("rsa e key error\n"));
  239. return USER_CRYPTO_ERROR;
  240. }
  241. if (key->type == RSA_PRIVATE) {
  242. if (SetIndividualExternal(&rsa->d, key->dipp) != 0) {
  243. USER_DEBUG(("rsa d key error\n"));
  244. return USER_CRYPTO_ERROR;
  245. }
  246. if (SetIndividualExternal(&rsa->p, key->pipp) != 0) {
  247. USER_DEBUG(("rsa p key error\n"));
  248. return USER_CRYPTO_ERROR;
  249. }
  250. if (SetIndividualExternal(&rsa->q, key->qipp) != 0) {
  251. USER_DEBUG(("rsa q key error\n"));
  252. return USER_CRYPTO_ERROR;
  253. }
  254. if (SetIndividualExternal(&rsa->dmp1, key->dPipp) != 0) {
  255. USER_DEBUG(("rsa dP key error\n"));
  256. return USER_CRYPTO_ERROR;
  257. }
  258. if (SetIndividualExternal(&rsa->dmq1, key->dQipp) != 0) {
  259. USER_DEBUG(("rsa dQ key error\n"));
  260. return USER_CRYPTO_ERROR;
  261. }
  262. if (SetIndividualExternal(&rsa->iqmp, key->uipp) != 0) {
  263. USER_DEBUG(("rsa u key error\n"));
  264. return USER_CRYPTO_ERROR;
  265. }
  266. }
  267. rsa->exSet = 1;
  268. /* SSL_SUCCESS */
  269. return 1;
  270. }
  271. /* Openssl -> WolfSSL */
  272. int SetRsaInternal(WOLFSSL_RSA* rsa)
  273. {
  274. int ctxSz, pSz, qSz;
  275. IppStatus ret;
  276. RsaKey* key;
  277. USER_DEBUG(("Entering SetRsaInternal\n"));
  278. if (rsa == NULL || rsa->internal == NULL) {
  279. USER_DEBUG(("rsa key NULL error\n"));
  280. return USER_CRYPTO_ERROR;
  281. }
  282. key = (RsaKey*)rsa->internal;
  283. if (SetIndividualInternal(rsa->n, &key->n) != 0) {
  284. USER_DEBUG(("rsa n key error\n"));
  285. return USER_CRYPTO_ERROR;
  286. }
  287. if (SetIndividualInternal(rsa->e, &key->e) != 0) {
  288. USER_DEBUG(("rsa e key error\n"));
  289. return USER_CRYPTO_ERROR;
  290. }
  291. /* public key */
  292. key->type = RSA_PUBLIC;
  293. if (rsa->d != NULL) {
  294. if (SetIndividualInternal(rsa->d, &key->dipp) != 0) {
  295. USER_DEBUG(("rsa d key error\n"));
  296. return USER_CRYPTO_ERROR;
  297. }
  298. /* private key */
  299. key->type = RSA_PRIVATE;
  300. }
  301. if (rsa->p != NULL &&
  302. SetIndividualInternal(rsa->p, &key->pipp) != 0) {
  303. USER_DEBUG(("rsa p key error\n"));
  304. return USER_CRYPTO_ERROR;
  305. }
  306. if (rsa->q != NULL &&
  307. SetIndividualInternal(rsa->q, &key->qipp) != 0) {
  308. USER_DEBUG(("rsa q key error\n"));
  309. return USER_CRYPTO_ERROR;
  310. }
  311. if (rsa->dmp1 != NULL &&
  312. SetIndividualInternal(rsa->dmp1, &key->dPipp) != 0) {
  313. USER_DEBUG(("rsa dP key error\n"));
  314. return USER_CRYPTO_ERROR;
  315. }
  316. if (rsa->dmq1 != NULL &&
  317. SetIndividualInternal(rsa->dmq1, &key->dQipp) != 0) {
  318. USER_DEBUG(("rsa dQ key error\n"));
  319. return USER_CRYPTO_ERROR;
  320. }
  321. if (rsa->iqmp != NULL &&
  322. SetIndividualInternal(rsa->iqmp, &key->uipp) != 0) {
  323. USER_DEBUG(("rsa u key error\n"));
  324. return USER_CRYPTO_ERROR;
  325. }
  326. rsa->inSet = 1;
  327. /* get sizes of IPP BN key states created from input */
  328. ret = ippsGetSize_BN(key->n, &key->nSz);
  329. if (ret != ippStsNoErr) {
  330. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  331. return USER_CRYPTO_ERROR;
  332. }
  333. ret = ippsGetSize_BN(key->e, &key->eSz);
  334. if (ret != ippStsNoErr) {
  335. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  336. return USER_CRYPTO_ERROR;
  337. }
  338. key->sz = key->nSz; /* set modulus size */
  339. /* convert to size in bits */
  340. key->nSz = key->nSz * 8;
  341. key->eSz = key->eSz * 8;
  342. /* set up public key state */
  343. ret = ippsRSA_GetSizePublicKey(key->nSz, key->eSz, &ctxSz);
  344. if (ret != ippStsNoErr) {
  345. USER_DEBUG(("ippsRSA_GetSizePublicKey error %s\n",
  346. ippGetStatusString(ret)));
  347. return USER_CRYPTO_ERROR;
  348. }
  349. key->pPub = (IppsRSAPublicKeyState*)XMALLOC(ctxSz, NULL,
  350. DYNAMIC_TYPE_USER_CRYPTO);
  351. if (key->pPub == NULL)
  352. return USER_CRYPTO_ERROR;
  353. ret = ippsRSA_InitPublicKey(key->nSz, key->eSz, key->pPub, ctxSz);
  354. if (ret != ippStsNoErr) {
  355. USER_DEBUG(("ippsRSA_InitPublicKey error %s\n",
  356. ippGetStatusString(ret)));
  357. return USER_CRYPTO_ERROR;
  358. }
  359. ret = ippsRSA_SetPublicKey(key->n, key->e, key->pPub);
  360. if (ret != ippStsNoErr) {
  361. USER_DEBUG(("ippsRSA_SetPublicKey error %s\n", ippGetStatusString(ret)));
  362. return USER_CRYPTO_ERROR;
  363. }
  364. if (key->pipp != NULL && key->qipp != NULL && key->dipp != NULL &&
  365. key->dPipp != NULL && key->dQipp != NULL && key->uipp != NULL) {
  366. /* get bn sizes needed for private key set up */
  367. ret = ippsGetSize_BN(key->pipp, &pSz);
  368. if (ret != ippStsNoErr) {
  369. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  370. return USER_CRYPTO_ERROR;
  371. }
  372. ret = ippsGetSize_BN(key->qipp, &qSz);
  373. if (ret != ippStsNoErr) {
  374. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  375. return USER_CRYPTO_ERROR;
  376. }
  377. /* store sizes needed for creating tmp private keys */
  378. ret = ippsGetSize_BN(key->dipp, &key->dSz);
  379. if (ret != ippStsNoErr) {
  380. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  381. return USER_CRYPTO_ERROR;
  382. }
  383. /* convert to size in bits */
  384. key->dSz = key->dSz * 8;
  385. pSz = pSz * 8;
  386. qSz = qSz * 8;
  387. /* set up private key state */
  388. ret = ippsRSA_GetSizePrivateKeyType2(pSz, qSz, &ctxSz);
  389. if (ret != ippStsNoErr) {
  390. USER_DEBUG(("ippsRSA_GetSizePrivateKey error %s\n",
  391. ippGetStatusString(ret)));
  392. return USER_CRYPTO_ERROR;
  393. }
  394. key->prvSz = ctxSz;
  395. key->pPrv = (IppsRSAPrivateKeyState*)XMALLOC(ctxSz, 0,
  396. DYNAMIC_TYPE_USER_CRYPTO);
  397. if (key->pPrv == NULL)
  398. return USER_CRYPTO_ERROR;
  399. ret = ippsRSA_InitPrivateKeyType2(pSz, qSz, key->pPrv, ctxSz);
  400. if (ret != ippStsNoErr) {
  401. USER_DEBUG(("ippsRSA_InitPrivateKey error %s\n",
  402. ippGetStatusString(ret)));
  403. return USER_CRYPTO_ERROR;
  404. }
  405. ret = ippsRSA_SetPrivateKeyType2(key->pipp, key->qipp, key->dPipp,
  406. key->dQipp, key->uipp, key->pPrv);
  407. if (ret != ippStsNoErr) {
  408. USER_DEBUG(("ippsRSA_SetPrivateKey error %s\n", ippGetStatusString(ret)));
  409. return USER_CRYPTO_ERROR;
  410. }
  411. }
  412. /* SSL_SUCCESS */
  413. return 1;
  414. }
  415. #endif /* OPENSSLEXTRA */
  416. /* Padding scheme function used in wolfSSL for signing needed for matching
  417. existing API signing scheme
  418. input : the msg to be signed
  419. inputLen : length of input msg
  420. pkcsBlock : the outputted padded msg
  421. pkcsBlockLen : length of outputted padded msg buffer
  422. padValue : the padded value after first 00 , is either 01 or 02
  423. rng : random number generator structure
  424. */
  425. static int wc_RsaPad(const byte* input, word32 inputLen, byte* pkcsBlock,
  426. word32 pkcsBlockLen, byte padValue, WC_RNG* rng)
  427. {
  428. if (inputLen == 0 || pkcsBlockLen == 0) {
  429. return USER_CRYPTO_ERROR;
  430. }
  431. pkcsBlock[0] = 0x0; /* set first byte to zero and advance */
  432. pkcsBlock++; pkcsBlockLen--;
  433. pkcsBlock[0] = padValue; /* insert padValue */
  434. if (padValue == RSA_BLOCK_TYPE_1) {
  435. if (pkcsBlockLen < inputLen + 2) {
  436. return USER_CRYPTO_ERROR;
  437. }
  438. /* pad with 0xff bytes */
  439. XMEMSET(&pkcsBlock[1], 0xFF, pkcsBlockLen - inputLen - 2);
  440. }
  441. else {
  442. /* pad with non-zero random bytes */
  443. word32 padLen, i;
  444. int ret;
  445. if (pkcsBlockLen < inputLen + 1) {
  446. return USER_CRYPTO_ERROR;
  447. }
  448. padLen = pkcsBlockLen - inputLen - 1;
  449. ret = wc_RNG_GenerateBlock(rng, &pkcsBlock[1], padLen);
  450. if (ret != 0)
  451. return ret;
  452. /* remove zeros */
  453. for (i = 1; i < padLen; i++)
  454. if (pkcsBlock[i] == 0) pkcsBlock[i] = 0x01;
  455. }
  456. pkcsBlock[pkcsBlockLen-inputLen-1] = 0; /* separator */
  457. XMEMCPY(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
  458. return 0;
  459. }
  460. /* UnPad plaintext, set start to *output, return length of plaintext,
  461. * < 0 on error */
  462. static int RsaUnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen,
  463. byte **output, byte padValue)
  464. {
  465. word32 maxOutputLen = (pkcsBlockLen > 10) ? (pkcsBlockLen - 10) : 0,
  466. invalid = 0,
  467. i = 1,
  468. outputLen;
  469. if (pkcsBlockLen == 0) {
  470. return USER_CRYPTO_ERROR;
  471. }
  472. if (pkcsBlock[0] != 0x0) /* skip past zero */
  473. invalid = 1;
  474. pkcsBlock++; pkcsBlockLen--;
  475. /* Require block type padValue */
  476. invalid = (pkcsBlock[0] != padValue) || invalid;
  477. /* verify the padding until we find the separator */
  478. if (padValue == RSA_BLOCK_TYPE_1) {
  479. while (i<pkcsBlockLen && pkcsBlock[i++] == 0xFF) {/* Null body */}
  480. }
  481. else {
  482. while (i<pkcsBlockLen && pkcsBlock[i++]) {/* Null body */}
  483. }
  484. if(!(i==pkcsBlockLen || pkcsBlock[i-1]==0)) {
  485. USER_DEBUG(("RsaUnPad error, bad formatting\n"));
  486. return USER_CRYPTO_ERROR;
  487. }
  488. outputLen = pkcsBlockLen - i;
  489. invalid = (outputLen > maxOutputLen) || invalid;
  490. if (invalid) {
  491. USER_DEBUG(("RsaUnPad error, bad formatting\n"));
  492. return USER_CRYPTO_ERROR;
  493. }
  494. *output = (byte *)(pkcsBlock + i);
  495. return outputLen;
  496. }
  497. /* Set up memory and structure for a Big Number
  498. * returns ippStsNoErr on success
  499. */
  500. static IppStatus init_bn(IppsBigNumState** in, int sz)
  501. {
  502. int ctxSz;
  503. IppStatus ret;
  504. ret = ippsBigNumGetSize(sz, &ctxSz);
  505. if (ret != ippStsNoErr) {
  506. return ret;
  507. }
  508. *in = (IppsBigNumState*)XMALLOC(ctxSz, 0, DYNAMIC_TYPE_USER_CRYPTO);
  509. if (*in == NULL) {
  510. return ippStsNoMemErr;
  511. }
  512. ret = ippsBigNumInit(sz, *in);
  513. if (ret != ippStsNoErr) {
  514. XFREE(*in, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  515. *in = NULL;
  516. return ret;
  517. }
  518. return ippStsNoErr;
  519. }
  520. /* Set up memory and structure for a Montgomery struct
  521. * returns ippStsNoErr on success
  522. */
  523. static IppStatus init_mont(IppsMontState** mont, int* ctxSz,
  524. IppsBigNumState* modul)
  525. {
  526. int mSz;
  527. Ipp32u* m;
  528. IppStatus ret;
  529. ret = ippsExtGet_BN(NULL, ctxSz, NULL, modul);
  530. if (ret != ippStsNoErr) {
  531. return ret;
  532. }
  533. /* convert bits to Ipp32u array size and round up
  534. 32 is number of bits in type */
  535. mSz = (*ctxSz/32)+((*ctxSz % 32)? 1: 0);
  536. m = (Ipp32u*)XMALLOC(mSz * sizeof(Ipp32u), 0, DYNAMIC_TYPE_USER_CRYPTO);
  537. if (m == NULL) {
  538. XFREE(m, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  539. return ippStsNoMemErr;
  540. }
  541. ret = ippsExtGet_BN(NULL, NULL, m, modul);
  542. if (ret != ippStsNoErr) {
  543. XFREE(m, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  544. return ret;
  545. }
  546. ret = ippsMontGetSize(IppsSlidingWindows, mSz, ctxSz);
  547. if (ret != ippStsNoErr) {
  548. XFREE(m, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  549. return ret;
  550. }
  551. /* 2. Allocate working buffer using malloc */
  552. *mont = (IppsMontState*)XMALLOC(*ctxSz, 0, DYNAMIC_TYPE_USER_CRYPTO);
  553. if (*mont == NULL) {
  554. XFREE(m, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  555. return ippStsNoMemErr;
  556. }
  557. ret = ippsMontInit(IppsSlidingWindows, mSz, *mont);
  558. if (ret != ippStsNoErr) {
  559. USER_DEBUG(("ippsMontInit error of %s\n", ippGetStatusString(ret)));
  560. XFREE(m, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  561. XFREE(*mont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  562. *mont = NULL;
  563. return ret;
  564. }
  565. /* 3. Call the function MontSet to set big number module */
  566. ret = ippsMontSet(m, mSz, *mont);
  567. if (ret != ippStsNoErr) {
  568. USER_DEBUG(("ippsMontSet error of %s\n", ippGetStatusString(ret)));
  569. XFREE(m, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  570. XFREE(*mont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  571. *mont = NULL;
  572. return ret;
  573. }
  574. XFREE(m, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  575. return ippStsNoErr;
  576. }
  577. int wc_FreeRsaKey(RsaKey* key)
  578. {
  579. if (key == NULL)
  580. return 0;
  581. USER_DEBUG(("Entering wc_FreeRsaKey\n"));
  582. if (key->pPub != NULL) {
  583. XFREE(key->pPub, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  584. key->pPub = NULL;
  585. }
  586. if (key->pPrv != NULL) {
  587. /* write over sensitive information */
  588. ForceZero(key->pPrv, key->prvSz);
  589. XFREE(key->pPrv, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  590. key->pPrv = NULL;
  591. }
  592. if (key->n != NULL) {
  593. XFREE(key->n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  594. key->n = NULL;
  595. }
  596. if (key->e != NULL) {
  597. XFREE(key->e, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  598. key->e = NULL;
  599. }
  600. if (key->dipp != NULL) {
  601. XFREE(key->dipp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  602. key->dipp = NULL;
  603. }
  604. if (key->pipp != NULL) {
  605. XFREE(key->pipp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  606. key->pipp = NULL;
  607. }
  608. if (key->qipp != NULL) {
  609. XFREE(key->qipp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  610. key->qipp = NULL;
  611. }
  612. if (key->dPipp != NULL) {
  613. XFREE(key->dPipp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  614. key->dPipp = NULL;
  615. }
  616. if (key->dQipp != NULL) {
  617. XFREE(key->dQipp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  618. key->dQipp = NULL;
  619. }
  620. if (key->uipp != NULL) {
  621. XFREE(key->uipp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  622. key->uipp = NULL;
  623. }
  624. USER_DEBUG(("\tExit wc_FreeRsaKey\n"));
  625. (void)key;
  626. return 0;
  627. }
  628. /* Some parsing functions from wolfSSL code needed to match wolfSSL API used */
  629. static int GetLength(const byte* input, word32* inOutIdx, int* len,
  630. word32 maxIdx)
  631. {
  632. int length = 0;
  633. word32 idx = *inOutIdx;
  634. byte b;
  635. *len = 0; /* default length */
  636. if ((idx + 1) > maxIdx) { /* for first read */
  637. USER_DEBUG(("GetLength bad index on input\n"));
  638. return USER_CRYPTO_ERROR;
  639. }
  640. b = input[idx++];
  641. if (b >= 0x80) {
  642. word32 bytes = b & 0x7F;
  643. if ((idx + bytes) > maxIdx) { /* for reading bytes */
  644. USER_DEBUG(("GetLength bad long length\n"));
  645. return USER_CRYPTO_ERROR;
  646. }
  647. while (bytes--) {
  648. b = input[idx++];
  649. length = (length << 8) | b;
  650. }
  651. }
  652. else
  653. length = b;
  654. if ((idx + length) > maxIdx) { /* for user of length */
  655. USER_DEBUG(("GetLength value exceeds buffer length\n"));
  656. return USER_CRYPTO_ERROR;
  657. }
  658. *inOutIdx = idx;
  659. if (length > 0)
  660. *len = length;
  661. return length;
  662. }
  663. static int GetASNHeader(const byte* input, byte tag, word32* inOutIdx, int* len,
  664. word32 maxIdx)
  665. {
  666. word32 idx = *inOutIdx;
  667. byte b;
  668. int length;
  669. if ((idx + 1) > maxIdx)
  670. return USER_CRYPTO_ERROR;
  671. b = input[idx++];
  672. if (b != tag)
  673. return USER_CRYPTO_ERROR;
  674. if (GetLength(input, &idx, &length, maxIdx) < 0)
  675. return USER_CRYPTO_ERROR;
  676. *len = length;
  677. *inOutIdx = idx;
  678. return length;
  679. }
  680. static int GetASNInt(const byte* input, word32* inOutIdx, int* len,
  681. word32 maxIdx)
  682. {
  683. int ret;
  684. ret = GetASNHeader(input, ASN_INTEGER, inOutIdx, len, maxIdx);
  685. if (ret < 0)
  686. return ret;
  687. if (*len > 0) {
  688. /* remove leading zero, unless there is only one 0x00 byte */
  689. if ((input[*inOutIdx] == 0x00) && (*len > 1)) {
  690. (*inOutIdx)++;
  691. (*len)--;
  692. if (*len > 0 && (input[*inOutIdx] & 0x80) == 0)
  693. return USER_CRYPTO_ERROR;
  694. }
  695. }
  696. return 0;
  697. }
  698. static int GetInt(IppsBigNumState** mpi, const byte* input, word32* inOutIdx,
  699. word32 maxIdx)
  700. {
  701. IppStatus ret;
  702. word32 idx = *inOutIdx;
  703. int length;
  704. int ctxSz;
  705. if (GetASNInt(input, &idx, &length, maxIdx) < 0) {
  706. return USER_CRYPTO_ERROR;
  707. }
  708. ret = ippsBigNumGetSize(length, &ctxSz);
  709. if (ret != ippStsNoErr)
  710. return USER_CRYPTO_ERROR;
  711. *mpi = (IppsBigNumState*)XMALLOC(ctxSz, 0, DYNAMIC_TYPE_USER_CRYPTO);
  712. if (*mpi == NULL)
  713. return USER_CRYPTO_ERROR;
  714. ret = ippsBigNumInit(length, *mpi);
  715. if (ret != ippStsNoErr)
  716. return USER_CRYPTO_ERROR;
  717. ret = ippsSetOctString_BN((Ipp8u*)input + idx, length, *mpi);
  718. if (ret != ippStsNoErr)
  719. return USER_CRYPTO_ERROR;
  720. *inOutIdx = idx + length;
  721. return 0;
  722. }
  723. static int GetSequence(const byte* input, word32* inOutIdx, int* len,
  724. word32 maxIdx)
  725. {
  726. int length = -1;
  727. word32 idx = *inOutIdx;
  728. if ((idx + 1) > maxIdx)
  729. return USER_CRYPTO_ERROR;
  730. if (input[idx++] != (0x10 | 0x20) ||
  731. GetLength(input, &idx, &length, maxIdx) < 0)
  732. return USER_CRYPTO_ERROR;
  733. *len = length;
  734. *inOutIdx = idx;
  735. return length;
  736. }
  737. static int GetMyVersion(const byte* input, word32* inOutIdx,
  738. int* version, word32 maxIdx)
  739. {
  740. word32 idx = *inOutIdx;
  741. if ((idx + 3) > maxIdx)
  742. return USER_CRYPTO_ERROR;
  743. if (input[idx++] != 0x02)
  744. return USER_CRYPTO_ERROR;
  745. if (input[idx++] != 0x01)
  746. return USER_CRYPTO_ERROR;
  747. *version = input[idx++];
  748. *inOutIdx = idx;
  749. return *version;
  750. }
  751. int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key,
  752. word32 inSz)
  753. {
  754. int version, length;
  755. int ctxSz, pSz, qSz;
  756. IppStatus ret;
  757. if (input == NULL || inOutIdx == NULL || key == NULL) {
  758. return USER_CRYPTO_ERROR;
  759. }
  760. USER_DEBUG(("Entering wc_RsaPrivateKeyDecode\n"));
  761. /* read in key information */
  762. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  763. return USER_CRYPTO_ERROR;
  764. if (GetMyVersion(input, inOutIdx, &version, inSz) < 0)
  765. return USER_CRYPTO_ERROR;
  766. key->type = RSA_PRIVATE;
  767. if (GetInt(&key->n, input, inOutIdx, inSz) < 0 ||
  768. GetInt(&key->e, input, inOutIdx, inSz) < 0 ||
  769. GetInt(&key->dipp, input, inOutIdx, inSz) < 0 ||
  770. GetInt(&key->pipp, input, inOutIdx, inSz) < 0 ||
  771. GetInt(&key->qipp, input, inOutIdx, inSz) < 0 ||
  772. GetInt(&key->dPipp, input, inOutIdx, inSz) < 0 ||
  773. GetInt(&key->dQipp, input, inOutIdx, inSz) < 0 ||
  774. GetInt(&key->uipp, input, inOutIdx, inSz) < 0 )
  775. return USER_CRYPTO_ERROR;
  776. /* get sizes of IPP BN key states created from input */
  777. ret = ippsGetSize_BN(key->n, &key->nSz);
  778. if (ret != ippStsNoErr) {
  779. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  780. return USER_CRYPTO_ERROR;
  781. }
  782. ret = ippsGetSize_BN(key->e, &key->eSz);
  783. if (ret != ippStsNoErr) {
  784. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  785. return USER_CRYPTO_ERROR;
  786. }
  787. key->sz = key->nSz; /* set modulus size */
  788. /* convert to size in bits */
  789. key->nSz = key->nSz * 8;
  790. key->eSz = key->eSz * 8;
  791. /* set up public key state */
  792. ret = ippsRSA_GetSizePublicKey(key->nSz, key->eSz, &ctxSz);
  793. if (ret != ippStsNoErr) {
  794. USER_DEBUG(("ippsRSA_GetSizePublicKey error %s\n",
  795. ippGetStatusString(ret)));
  796. return USER_CRYPTO_ERROR;
  797. }
  798. key->pPub = (IppsRSAPublicKeyState*)XMALLOC(ctxSz, NULL,
  799. DYNAMIC_TYPE_USER_CRYPTO);
  800. if (key->pPub == NULL)
  801. return USER_CRYPTO_ERROR;
  802. ret = ippsRSA_InitPublicKey(key->nSz, key->eSz, key->pPub, ctxSz);
  803. if (ret != ippStsNoErr) {
  804. USER_DEBUG(("ippsRSA_InitPublicKey error %s\n",
  805. ippGetStatusString(ret)));
  806. return USER_CRYPTO_ERROR;
  807. }
  808. ret = ippsRSA_SetPublicKey(key->n, key->e, key->pPub);
  809. if (ret != ippStsNoErr) {
  810. USER_DEBUG(("ippsRSA_SetPublicKey error %s\n",
  811. ippGetStatusString(ret)));
  812. return USER_CRYPTO_ERROR;
  813. }
  814. /* get bn sizes needed for private key set up */
  815. ret = ippsGetSize_BN(key->pipp, &pSz);
  816. if (ret != ippStsNoErr) {
  817. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  818. return USER_CRYPTO_ERROR;
  819. }
  820. ret = ippsGetSize_BN(key->qipp, &qSz);
  821. if (ret != ippStsNoErr) {
  822. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  823. return USER_CRYPTO_ERROR;
  824. }
  825. /* store sizes needed for creating tmp private keys */
  826. ret = ippsGetSize_BN(key->dipp, &key->dSz);
  827. if (ret != ippStsNoErr) {
  828. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  829. return USER_CRYPTO_ERROR;
  830. }
  831. /* convert to size in bits */
  832. key->dSz = key->dSz * 8;
  833. pSz = pSz * 8;
  834. qSz = qSz * 8;
  835. /* set up private key state */
  836. ret = ippsRSA_GetSizePrivateKeyType2(pSz, qSz, &ctxSz);
  837. if (ret != ippStsNoErr) {
  838. USER_DEBUG(("ippsRSA_GetSizePrivateKey error %s\n",
  839. ippGetStatusString(ret)));
  840. return USER_CRYPTO_ERROR;
  841. }
  842. key->prvSz = ctxSz;
  843. key->pPrv = (IppsRSAPrivateKeyState*)XMALLOC(ctxSz, 0,
  844. DYNAMIC_TYPE_USER_CRYPTO);
  845. if (key->pPrv == NULL)
  846. return USER_CRYPTO_ERROR;
  847. ret = ippsRSA_InitPrivateKeyType2(pSz, qSz, key->pPrv, ctxSz);
  848. if (ret != ippStsNoErr) {
  849. USER_DEBUG(("ippsRSA_InitPrivateKey error %s\n",
  850. ippGetStatusString(ret)));
  851. return USER_CRYPTO_ERROR;
  852. }
  853. ret = ippsRSA_SetPrivateKeyType2(key->pipp, key->qipp, key->dPipp,
  854. key->dQipp, key->uipp, key->pPrv);
  855. if (ret != ippStsNoErr) {
  856. USER_DEBUG(("ippsRSA_SetPrivateKey error %s\n",
  857. ippGetStatusString(ret)));
  858. return USER_CRYPTO_ERROR;
  859. }
  860. USER_DEBUG(("\tExit wc_RsaPrivateKeyDecode\n"));
  861. return 0;
  862. }
  863. int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx,
  864. word32 inSz, const byte** n, word32* nSz, const byte** e, word32* eSz)
  865. {
  866. IppStatus ret = 0;
  867. int length;
  868. #if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
  869. byte b;
  870. #endif
  871. if (input == NULL || inOutIdx == NULL) {
  872. return USER_CRYPTO_ERROR;
  873. }
  874. USER_DEBUG(("Entering wc_RsaPublicKeyDecode_ex\n"));
  875. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  876. return USER_CRYPTO_ERROR;
  877. #if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
  878. if ((*inOutIdx + 1) > inSz)
  879. return USER_CRYPTO_ERROR;
  880. b = input[*inOutIdx];
  881. if (b != ASN_INTEGER) {
  882. /* not from decoded cert, will have algo id, skip past */
  883. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  884. return USER_CRYPTO_ERROR;
  885. b = input[(*inOutIdx)++];
  886. if (b != ASN_OBJECT_ID)
  887. return USER_CRYPTO_ERROR;
  888. if (GetLength(input, inOutIdx, &length, inSz) < 0)
  889. return USER_CRYPTO_ERROR;
  890. *inOutIdx += length; /* skip past */
  891. /* could have NULL tag and 0 terminator, but may not */
  892. b = input[(*inOutIdx)++];
  893. if (b == ASN_TAG_NULL) {
  894. b = input[(*inOutIdx)++];
  895. if (b != 0)
  896. return USER_CRYPTO_ERROR;
  897. }
  898. else {
  899. /* go back, didn't have it */
  900. (*inOutIdx)--;
  901. }
  902. /* should have bit tag length and seq next */
  903. b = input[(*inOutIdx)++];
  904. if (b != ASN_BIT_STRING)
  905. return USER_CRYPTO_ERROR;
  906. if (GetLength(input, inOutIdx, &length, inSz) <= 0)
  907. return USER_CRYPTO_ERROR;
  908. /* could have 0 */
  909. b = input[(*inOutIdx)++];
  910. if (b != 0)
  911. (*inOutIdx)--;
  912. if (GetSequence(input, inOutIdx, &length, inSz) < 0)
  913. return USER_CRYPTO_ERROR;
  914. }
  915. #endif /* OPENSSL_EXTRA || RSA_DECODE_EXTRA */
  916. /* Get modulus */
  917. ret = GetASNInt(input, inOutIdx, &length, inSz);
  918. if (ret < 0) {
  919. return USER_CRYPTO_ERROR;
  920. }
  921. if (nSz)
  922. *nSz = length;
  923. if (n)
  924. *n = &input[*inOutIdx];
  925. *inOutIdx += length;
  926. /* Get exponent */
  927. ret = GetASNInt(input, inOutIdx, &length, inSz);
  928. if (ret < 0) {
  929. return USER_CRYPTO_ERROR;
  930. }
  931. if (eSz)
  932. *eSz = length;
  933. if (e)
  934. *e = &input[*inOutIdx];
  935. *inOutIdx += length;
  936. USER_DEBUG(("\tExit wc_RsaPublicKeyDecode_ex\n"));
  937. return ret;
  938. }
  939. /* read in a public RSA key */
  940. int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key,
  941. word32 inSz)
  942. {
  943. IppStatus ret;
  944. const byte *n = NULL, *e = NULL;
  945. word32 nSz = 0, eSz = 0;
  946. if (key == NULL)
  947. return USER_CRYPTO_ERROR;
  948. USER_DEBUG(("Entering wc_RsaPublicKeyDecode\n"));
  949. ret = wc_RsaPublicKeyDecode_ex(input, inOutIdx, inSz, &n, &nSz, &e, &eSz);
  950. if (ret == 0) {
  951. ret = wc_RsaPublicKeyDecodeRaw(n, nSz, e, eSz, key);
  952. }
  953. USER_DEBUG(("\tExit RsaPublicKeyDecode\n"));
  954. return ret;
  955. }
  956. /* import RSA public key elements (n, e) into RsaKey structure (key) */
  957. int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz, const byte* e,
  958. word32 eSz, RsaKey* key)
  959. {
  960. IppStatus ret;
  961. int ctxSz;
  962. USER_DEBUG(("Entering wc_RsaPublicKeyDecodeRaw\n"));
  963. if (n == NULL || e == NULL || key == NULL)
  964. return USER_CRYPTO_ERROR;
  965. /* set up IPP key states -- read in n */
  966. ret = init_bn(&key->n, nSz);
  967. if (ret != ippStsNoErr)
  968. return USER_CRYPTO_ERROR;
  969. ret = ippsSetOctString_BN((Ipp8u*)n, nSz, key->n);
  970. if (ret != ippStsNoErr)
  971. return USER_CRYPTO_ERROR;
  972. /* read in e */
  973. ret = init_bn(&key->e, eSz);
  974. if (ret != ippStsNoErr)
  975. return USER_CRYPTO_ERROR;
  976. ret = ippsSetOctString_BN((Ipp8u*)e, eSz, key->e);
  977. if (ret != ippStsNoErr)
  978. return USER_CRYPTO_ERROR;
  979. /* store size and convert to binary */
  980. key->sz = nSz;
  981. nSz = nSz * 8;
  982. eSz = eSz * 8;
  983. /* set up public key state */
  984. ret = ippsRSA_GetSizePublicKey(nSz, eSz, &ctxSz);
  985. if (ret != ippStsNoErr) {
  986. USER_DEBUG(("ippsRSA_GetSizePublicKey error %s\n",
  987. ippGetStatusString(ret)));
  988. return USER_CRYPTO_ERROR;
  989. }
  990. key->pPub = (IppsRSAPublicKeyState*)XMALLOC(ctxSz, NULL,
  991. DYNAMIC_TYPE_USER_CRYPTO);
  992. if (key->pPub == NULL)
  993. return USER_CRYPTO_ERROR;
  994. ret = ippsRSA_InitPublicKey(nSz, eSz, key->pPub, ctxSz);
  995. if (ret != ippStsNoErr) {
  996. USER_DEBUG(("ippsRSA_InitPublicKey error %s\n",
  997. ippGetStatusString(ret)));
  998. return USER_CRYPTO_ERROR;
  999. }
  1000. ret = ippsRSA_SetPublicKey(key->n,key->e, key->pPub);
  1001. if (ret != ippStsNoErr) {
  1002. USER_DEBUG(("ippsRSA_SetPublicKey error %s\n",
  1003. ippGetStatusString(ret)));
  1004. return USER_CRYPTO_ERROR;
  1005. }
  1006. key->nSz = nSz;
  1007. key->eSz = eSz;
  1008. key->type = RSA_PUBLIC;
  1009. return 0;
  1010. }
  1011. /* encrypt using PKCS v15 */
  1012. int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, word32 outLen,
  1013. RsaKey* key, WC_RNG* rng)
  1014. {
  1015. IppStatus ret;
  1016. Ipp8u* scratchBuffer;
  1017. int scratchSz;
  1018. if (key == NULL || in == NULL || out == NULL)
  1019. return USER_CRYPTO_ERROR;
  1020. if (key->pPub == NULL || outLen < key->sz)
  1021. return USER_CRYPTO_ERROR;
  1022. /* set size of scratch buffer */
  1023. ret = ippsRSA_GetBufferSizePublicKey(&scratchSz, key->pPub);
  1024. if (ret != ippStsNoErr)
  1025. return USER_CRYPTO_ERROR;
  1026. scratchBuffer = (Ipp8u*)XMALLOC(scratchSz*(sizeof(Ipp8u)), 0,
  1027. DYNAMIC_TYPE_USER_CRYPTO);
  1028. if (scratchBuffer == NULL)
  1029. return USER_CRYPTO_ERROR;
  1030. ret = ippsRSAEncrypt_PKCSv15((Ipp8u*)in, inLen, NULL, (Ipp8u*)out,
  1031. key->pPub, scratchBuffer);
  1032. if (ret != ippStsNoErr) {
  1033. XFREE(scratchBuffer, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1034. USER_DEBUG(("encrypt error of %s\n", ippGetStatusString(ret)));
  1035. return USER_CRYPTO_ERROR;
  1036. }
  1037. XFREE(scratchBuffer, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1038. (void)rng;
  1039. return key->sz;
  1040. }
  1041. /* decrypt using PLCS v15 */
  1042. int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out, word32 outLen,
  1043. RsaKey* key)
  1044. {
  1045. IppStatus ret;
  1046. Ipp8u* scratchBuffer;
  1047. int scratchSz;
  1048. int outSz;
  1049. if (in == NULL || out == NULL || key == NULL)
  1050. return USER_CRYPTO_ERROR;
  1051. if (key->pPrv == NULL || inLen != key->sz)
  1052. return USER_CRYPTO_ERROR;
  1053. outSz = outLen;
  1054. /* set size of scratch buffer */
  1055. ret = ippsRSA_GetBufferSizePrivateKey(&scratchSz, key->pPrv);
  1056. if (ret != ippStsNoErr) {
  1057. return USER_CRYPTO_ERROR;
  1058. }
  1059. scratchBuffer = (Ipp8u*)XMALLOC(scratchSz*(sizeof(Ipp8u)), 0,
  1060. DYNAMIC_TYPE_USER_CRYPTO);
  1061. if (scratchBuffer == NULL) {
  1062. return USER_CRYPTO_ERROR;
  1063. }
  1064. /* perform decryption using IPP */
  1065. ret = ippsRSADecrypt_PKCSv15((Ipp8u*)in, (Ipp8u*)out, &outSz, key->pPrv,
  1066. scratchBuffer);
  1067. if (ret != ippStsNoErr) {
  1068. XFREE(scratchBuffer, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1069. USER_DEBUG(("decrypt error of %s\n", ippGetStatusString(ret)));
  1070. return USER_CRYPTO_ERROR;
  1071. }
  1072. XFREE(scratchBuffer, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1073. return outSz;
  1074. }
  1075. /* out is a pointer that is set to the location in byte array "in" where input
  1076. data has been decrypted */
  1077. int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out, RsaKey* key)
  1078. {
  1079. int outSz;
  1080. byte* tmp;
  1081. USER_DEBUG(("Entering wc_RsaPrivateDecryptInline\n"));
  1082. /* allocate a buffer for max decrypted text */
  1083. tmp = (byte*)XMALLOC(key->sz, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1084. if (tmp == NULL)
  1085. return USER_CRYPTO_ERROR;
  1086. outSz = wc_RsaPrivateDecrypt(in, inLen, tmp, key->sz, key);
  1087. if (outSz >= 0) {
  1088. XMEMCPY(in, tmp, outSz);
  1089. *out = in;
  1090. }
  1091. else {
  1092. XFREE(tmp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1093. return USER_CRYPTO_ERROR;
  1094. }
  1095. XFREE(tmp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1096. USER_DEBUG(("\tExit wc_RsaPrivateDecryptInline\n"));
  1097. return outSz;
  1098. }
  1099. /* Used to clean up memory when exiting, clean up memory used */
  1100. static int FreeHelper(IppsBigNumState* pTxt, IppsBigNumState* cTxt,
  1101. Ipp8u* scratchBuffer, void* pPub)
  1102. {
  1103. if (pTxt != NULL)
  1104. XFREE(pTxt, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1105. if (cTxt != NULL)
  1106. XFREE(cTxt, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1107. if (scratchBuffer != NULL)
  1108. XFREE(scratchBuffer, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1109. if (pPub != NULL)
  1110. XFREE(pPub, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1111. return 0;
  1112. }
  1113. /* for Rsa Verify
  1114. in : byte array to be verified
  1115. inLen : length of input array
  1116. out : pointer to location of in byte array that has been verified
  1117. */
  1118. int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out, RsaKey* key)
  1119. {
  1120. int ctxSz;
  1121. int scratchSz;
  1122. Ipp8u* scratchBuffer = NULL;
  1123. IppStatus ret;
  1124. IppsRSAPrivateKeyState* pPub = NULL;
  1125. IppsBigNumState* pTxt = NULL;
  1126. IppsBigNumState* cTxt = NULL;
  1127. USER_DEBUG(("Entering wc_RsaSSL_VerifyInline\n"));
  1128. if (key == NULL || key->n == NULL || key->e == NULL) {
  1129. USER_DEBUG(("n or e element was null\n"));
  1130. return USER_CRYPTO_ERROR;
  1131. }
  1132. if (in == NULL || inLen == 0 || out == NULL)
  1133. return USER_CRYPTO_ERROR;
  1134. /* set up a private key state using public key values */
  1135. ret = ippsRSA_GetSizePrivateKeyType1(key->nSz, key->eSz, &ctxSz);
  1136. if (ret != ippStsNoErr) {
  1137. USER_DEBUG(("ippsRSA_GetSizePrivateKey error %s\n",
  1138. ippGetStatusString(ret)));
  1139. return USER_CRYPTO_ERROR;
  1140. }
  1141. pPub = (IppsRSAPrivateKeyState*)XMALLOC(ctxSz, 0, DYNAMIC_TYPE_USER_CRYPTO);
  1142. if (pPub == NULL)
  1143. return USER_CRYPTO_ERROR;
  1144. ret = ippsRSA_InitPrivateKeyType1(key->nSz, key->eSz, pPub, ctxSz);
  1145. if (ret != ippStsNoErr) {
  1146. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1147. USER_DEBUG(("ippsRSA_InitPrivateKey error %s\n",
  1148. ippGetStatusString(ret)));
  1149. return USER_CRYPTO_ERROR;
  1150. }
  1151. ret = ippsRSA_SetPrivateKeyType1(key->n, key->e, pPub);
  1152. if (ret != ippStsNoErr) {
  1153. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1154. USER_DEBUG(("ippsRSA_SetPrivateKey error %s\n",
  1155. ippGetStatusString(ret)));
  1156. return USER_CRYPTO_ERROR;
  1157. }
  1158. /* set size of scratch buffer */
  1159. ret = ippsRSA_GetBufferSizePrivateKey(&scratchSz, pPub);
  1160. if (ret != ippStsNoErr) {
  1161. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1162. return USER_CRYPTO_ERROR;
  1163. }
  1164. scratchBuffer = (Ipp8u*)XMALLOC(scratchSz*(sizeof(Ipp8u)), 0,
  1165. DYNAMIC_TYPE_USER_CRYPTO);
  1166. if (scratchBuffer == NULL) {
  1167. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1168. return USER_CRYPTO_ERROR;
  1169. }
  1170. /* load plain and cipher into big num states */
  1171. ret = init_bn(&pTxt, key->sz);
  1172. if (ret != ippStsNoErr) {
  1173. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1174. return USER_CRYPTO_ERROR;
  1175. }
  1176. ret = ippsSetOctString_BN((Ipp8u*)in, key->sz, pTxt);
  1177. if (ret != ippStsNoErr) {
  1178. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1179. return USER_CRYPTO_ERROR;
  1180. }
  1181. /* set up cipher to hold signature */
  1182. ret = init_bn(&cTxt, key->sz);
  1183. if (ret != ippStsNoErr) {
  1184. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1185. return USER_CRYPTO_ERROR;
  1186. }
  1187. ret = ippsSetOctString_BN((Ipp8u*)in, key->sz, cTxt);
  1188. if (ret != ippStsNoErr) {
  1189. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1190. return USER_CRYPTO_ERROR;
  1191. }
  1192. /* decrypt using public key information */
  1193. ret = ippsRSA_Decrypt(cTxt, pTxt, pPub, scratchBuffer);
  1194. if (ret != ippStsNoErr) {
  1195. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1196. USER_DEBUG(("decrypt error of %s\n", ippGetStatusString(ret)));
  1197. return USER_CRYPTO_ERROR;
  1198. }
  1199. /* extract big num struct to octet string */
  1200. ret = ippsGetOctString_BN((Ipp8u*)in, key->sz, pTxt);
  1201. if (ret != ippStsNoErr) {
  1202. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1203. USER_DEBUG(("BN get string error of %s\n", ippGetStatusString(ret)));
  1204. return USER_CRYPTO_ERROR;
  1205. }
  1206. FreeHelper(pTxt, cTxt, scratchBuffer, pPub);
  1207. /* unpad the decrypted information and return size of array */
  1208. return RsaUnPad(in, inLen, out, RSA_BLOCK_TYPE_1);
  1209. }
  1210. /* sets up and call VerifyInline to verify a signature */
  1211. int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out, word32 outLen,
  1212. RsaKey* key)
  1213. {
  1214. int plainLen;
  1215. byte* tmp;
  1216. byte* pad = 0;
  1217. if (out == NULL || in == NULL || key == NULL)
  1218. return USER_CRYPTO_ERROR;
  1219. tmp = (byte*)XMALLOC(inLen, key->heap, DYNAMIC_TYPE_USER_CRYPTO);
  1220. if (tmp == NULL) {
  1221. return USER_CRYPTO_ERROR;
  1222. }
  1223. XMEMCPY(tmp, in, inLen);
  1224. /* verify signature and test if output buffer is large enough */
  1225. plainLen = wc_RsaSSL_VerifyInline(tmp, inLen, &pad, key);
  1226. if (plainLen < 0) {
  1227. XFREE(tmp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1228. return plainLen;
  1229. }
  1230. if (plainLen > (int)outLen)
  1231. plainLen = USER_CRYPTO_ERROR;
  1232. else
  1233. XMEMCPY(out, pad, plainLen);
  1234. ForceZero(tmp, inLen);
  1235. XFREE(tmp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1236. return plainLen;
  1237. }
  1238. /* Check if a > b , if so c = a mod b
  1239. return ippStsNoErr on success */
  1240. static IppStatus reduce(IppsBigNumState* a, IppsBigNumState* b,
  1241. IppsBigNumState* c)
  1242. {
  1243. IppStatus ret;
  1244. if ((ret = ippsMod_BN(a, b, c)) != ippStsNoErr)
  1245. return ret;
  1246. return ippStsNoErr;
  1247. }
  1248. static IppStatus exptmod(IppsBigNumState* a, IppsBigNumState* b,
  1249. IppsMontState* mont, IppsBigNumState* out, IppsBigNumState* one)
  1250. {
  1251. IppStatus ret;
  1252. ret = ippsMontForm(a, mont, a);
  1253. if (ret != ippStsNoErr) {
  1254. USER_DEBUG(("ippsMontForm error of %s\n", ippGetStatusString(ret)));
  1255. return ret;
  1256. }
  1257. /* a = a^b mod mont */
  1258. ret = ippsMontExp(a, b, mont, out);
  1259. if (ret != ippStsNoErr) {
  1260. USER_DEBUG(("ippsMontExp error of %s\n", ippGetStatusString(ret)));
  1261. return ret;
  1262. }
  1263. /* convert back from montgomery */
  1264. ret = ippsMontMul(out, one, mont, out);
  1265. if (ret != ippStsNoErr) {
  1266. USER_DEBUG(("ippsMontMul error of %s\n", ippGetStatusString(ret)));
  1267. return ret;
  1268. }
  1269. return ippStsNoErr;
  1270. }
  1271. static void Free_BN(IppsBigNumState* bn)
  1272. {
  1273. int sz, ctxSz;
  1274. IppStatus ret;
  1275. if (bn != NULL) {
  1276. ret = ippStsNoErr;
  1277. ret |= ippsGetSize_BN(bn, &sz);
  1278. ret |= ippsBigNumGetSize(sz, &ctxSz);
  1279. if (ret == ippStsNoErr) {
  1280. ForceZero(bn, ctxSz);
  1281. }
  1282. else {
  1283. USER_DEBUG(("Issue with clearing a struct in RsaSSL_Sign free\n"));
  1284. }
  1285. XFREE(bn, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1286. }
  1287. }
  1288. /* free up memory used during CRT sign operation */
  1289. static void FreeSignHelper(IppsBigNumState* one, IppsBigNumState* tmp,
  1290. IppsBigNumState* tmpP, IppsBigNumState* tmpQ, IppsBigNumState* tmpa,
  1291. IppsBigNumState* tmpb)
  1292. {
  1293. Free_BN(one);
  1294. Free_BN(tmp);
  1295. Free_BN(tmpP);
  1296. Free_BN(tmpQ);
  1297. Free_BN(tmpa);
  1298. Free_BN(tmpb);
  1299. }
  1300. /* for Rsa Sign */
  1301. int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out, word32 outLen,
  1302. RsaKey* key, WC_RNG* rng)
  1303. {
  1304. int sz, pSz, qSz;
  1305. IppStatus ret;
  1306. word32 outSz;
  1307. IppsMontState* pMont = NULL;
  1308. IppsMontState* qMont = NULL;
  1309. IppsBigNumState* one = NULL;
  1310. IppsBigNumState* tmp = NULL;
  1311. IppsBigNumState* tmpP = NULL;
  1312. IppsBigNumState* tmpQ = NULL;
  1313. IppsBigNumState* tmpa = NULL;
  1314. IppsBigNumState* tmpb = NULL;
  1315. IppsBigNumSGN sa, sb;
  1316. Ipp8u o[1];
  1317. o[0] = 1;
  1318. USER_DEBUG(("Entering wc_RsaSSL_Sign\n"));
  1319. if (in == NULL || out == NULL || key == NULL || rng == NULL) {
  1320. USER_DEBUG(("Bad argument to wc_RsaSSL_Sign\n"));
  1321. return USER_CRYPTO_ERROR;
  1322. }
  1323. sz = key->sz;
  1324. /* sanity check on key being used */
  1325. if (key->pipp == NULL || key->qipp == NULL || key->uipp == NULL ||
  1326. key->dPipp == NULL || key->dQipp == NULL) {
  1327. USER_DEBUG(("Bad key argument to wc_RsaSSL_Sign\n"));
  1328. return USER_CRYPTO_ERROR;
  1329. }
  1330. if (sz > (int)outLen) {
  1331. USER_DEBUG(("Bad argument outLen to wc_RsaSSL_Sign\n"));
  1332. return USER_CRYPTO_ERROR;
  1333. }
  1334. if (sz < RSA_MIN_PAD_SZ) {
  1335. USER_DEBUG(("Key size is too small\n"));
  1336. return USER_CRYPTO_ERROR;
  1337. }
  1338. if (inLen > (word32)(sz - RSA_MIN_PAD_SZ)) {
  1339. USER_DEBUG(("Bad argument inLen to wc_RsaSSL_Sign\n"));
  1340. return USER_CRYPTO_ERROR;
  1341. }
  1342. /* Set up needed pkcs v15 padding */
  1343. if (wc_RsaPad(in, inLen, out, sz, RSA_BLOCK_TYPE_1, rng) != 0) {
  1344. USER_DEBUG(("RSA Padding error\n"));
  1345. return USER_CRYPTO_ERROR;
  1346. }
  1347. /* tmp = input to sign */
  1348. ret = init_bn(&tmp, sz);
  1349. if (ret != ippStsNoErr) {
  1350. USER_DEBUG(("init_BN error of %s\n", ippGetStatusString(ret)));
  1351. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1352. return USER_CRYPTO_ERROR;
  1353. }
  1354. ret = ippsSetOctString_BN(out, sz, tmp);
  1355. if (ret != ippStsNoErr) {
  1356. USER_DEBUG(("ippsSetOctString_BN error of %s\n",
  1357. ippGetStatusString(ret)));
  1358. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1359. return USER_CRYPTO_ERROR;
  1360. }
  1361. /* tmpP = tmp mod p */
  1362. ret = init_bn(&tmpP, sz);
  1363. if (ret != ippStsNoErr) {
  1364. USER_DEBUG(("init_BN error of %s\n", ippGetStatusString(ret)));
  1365. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1366. return USER_CRYPTO_ERROR;
  1367. }
  1368. /* tmpQ = tmp mod q */
  1369. ret = init_bn(&tmpQ, sz);
  1370. if (ret != ippStsNoErr) {
  1371. USER_DEBUG(("init_BN error of %s\n", ippGetStatusString(ret)));
  1372. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1373. return USER_CRYPTO_ERROR;
  1374. }
  1375. /* tmpa */
  1376. ret = init_bn(&tmpa, sz);
  1377. if (ret != ippStsNoErr) {
  1378. USER_DEBUG(("init_BN error of %s\n", ippGetStatusString(ret)));
  1379. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1380. return USER_CRYPTO_ERROR;
  1381. }
  1382. /* tmpb */
  1383. ret = init_bn(&tmpb, sz);
  1384. if (ret != ippStsNoErr) {
  1385. USER_DEBUG(("init_BN error of %s\n", ippGetStatusString(ret)));
  1386. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1387. return USER_CRYPTO_ERROR;
  1388. }
  1389. /* one : used for conversion from Montgomery to classical */
  1390. ret = init_bn(&one, sz);
  1391. if (ret != ippStsNoErr) {
  1392. USER_DEBUG(("init_BN error of %s\n", ippGetStatusString(ret)));
  1393. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1394. return USER_CRYPTO_ERROR;
  1395. }
  1396. ret = ippsSetOctString_BN(o, 1, one);
  1397. if (ret != ippStsNoErr) {
  1398. USER_DEBUG(("ippsSetOctString_BN error of %s\n",
  1399. ippGetStatusString(ret)));
  1400. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1401. return USER_CRYPTO_ERROR;
  1402. }
  1403. /**
  1404. Set up Montgomery state
  1405. */
  1406. ret = init_mont(&pMont, &pSz, key->pipp);
  1407. if (ret != ippStsNoErr) {
  1408. USER_DEBUG(("init_mont error of %s\n", ippGetStatusString(ret)));
  1409. if (pMont != NULL) {
  1410. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1411. }
  1412. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1413. return USER_CRYPTO_ERROR;
  1414. }
  1415. ret = init_mont(&qMont, &qSz, key->qipp);
  1416. if (ret != ippStsNoErr) {
  1417. USER_DEBUG(("init_mont error of %s\n", ippGetStatusString(ret)));
  1418. if (qMont != NULL) {
  1419. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1420. }
  1421. ForceZero(pMont, pSz);
  1422. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1423. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1424. return USER_CRYPTO_ERROR;
  1425. }
  1426. /**
  1427. Check and reduce input
  1428. This is needed for calls to MontExp since required value of a < modulus
  1429. */
  1430. ret = reduce(tmp, key->pipp, tmpP);
  1431. if (ret != ippStsNoErr)
  1432. {
  1433. USER_DEBUG(("reduce error of %s\n", ippGetStatusString(ret)));
  1434. ForceZero(pMont, pSz);
  1435. ForceZero(qMont, qSz);
  1436. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1437. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1438. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1439. return USER_CRYPTO_ERROR;
  1440. }
  1441. ret = reduce(tmp, key->qipp, tmpQ);
  1442. if (ret != ippStsNoErr)
  1443. {
  1444. USER_DEBUG(("reduce error of %s\n", ippGetStatusString(ret)));
  1445. ForceZero(pMont, pSz);
  1446. ForceZero(qMont, qSz);
  1447. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1448. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1449. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1450. return USER_CRYPTO_ERROR;
  1451. }
  1452. /* tmpa = (tmp mod p)^dP mod p */
  1453. ret = exptmod(tmpP, key->dPipp, pMont, tmpa, one);
  1454. if (ret != ippStsNoErr) {
  1455. USER_DEBUG(("exptmod error of %s\n", ippGetStatusString(ret)));
  1456. ForceZero(pMont, pSz);
  1457. ForceZero(qMont, qSz);
  1458. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1459. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1460. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1461. return USER_CRYPTO_ERROR;
  1462. }
  1463. /* tmpb = (tmp mod q)^dQ mod q */
  1464. ret = exptmod(tmpQ, key->dQipp, qMont, tmpb, one);
  1465. if (ret != ippStsNoErr) {
  1466. USER_DEBUG(("exptmod error of %s\n", ippGetStatusString(ret)));
  1467. ForceZero(pMont, pSz);
  1468. ForceZero(qMont, qSz);
  1469. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1470. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1471. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1472. return USER_CRYPTO_ERROR;
  1473. }
  1474. /* tmp = (tmpa - tmpb) * qInv (mod p) */
  1475. ret = ippsSub_BN(tmpa, tmpb, tmp);
  1476. if (ret != ippStsNoErr) {
  1477. USER_DEBUG(("ippsSub_BN error of %s\n", ippGetStatusString(ret)));
  1478. ForceZero(pMont, pSz);
  1479. ForceZero(qMont, qSz);
  1480. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1481. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1482. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1483. return USER_CRYPTO_ERROR;
  1484. }
  1485. ret = ippsMul_BN(tmp, key->uipp, tmp);
  1486. if (ret != ippStsNoErr) {
  1487. USER_DEBUG(("ippsMul_BN error of %s\n", ippGetStatusString(ret)));
  1488. ForceZero(pMont, pSz);
  1489. ForceZero(qMont, qSz);
  1490. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1491. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1492. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1493. return USER_CRYPTO_ERROR;
  1494. }
  1495. /* mod performed the same was as wolfSSL fp_mod -- tmpa is just scratch */
  1496. ret = ippsDiv_BN(tmp, key->pipp, tmpa, tmp);
  1497. if (ret != ippStsNoErr) {
  1498. USER_DEBUG(("ippsDiv_BN error of %s\n", ippGetStatusString(ret)));
  1499. ForceZero(pMont, pSz);
  1500. ForceZero(qMont, qSz);
  1501. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1502. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1503. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1504. return USER_CRYPTO_ERROR;
  1505. }
  1506. /* Check sign of values and perform conditional add */
  1507. ret = ippsExtGet_BN(&sa, NULL, NULL, tmp);
  1508. if (ret != ippStsNoErr) {
  1509. USER_DEBUG(("ippsExtGet_BN error of %s\n", ippGetStatusString(ret)));
  1510. ForceZero(pMont, pSz);
  1511. ForceZero(qMont, qSz);
  1512. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1513. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1514. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1515. return USER_CRYPTO_ERROR;
  1516. }
  1517. ret = ippsExtGet_BN(&sb, NULL, NULL, key->pipp);
  1518. if (ret != ippStsNoErr) {
  1519. USER_DEBUG(("ippsExtGet_BN error of %s\n", ippGetStatusString(ret)));
  1520. ForceZero(pMont, pSz);
  1521. ForceZero(qMont, qSz);
  1522. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1523. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1524. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1525. return USER_CRYPTO_ERROR;
  1526. }
  1527. if (sa != sb) {
  1528. ret = ippsAdd_BN(tmp, key->pipp, tmp);
  1529. if (ret != ippStsNoErr) {
  1530. USER_DEBUG(("ippsAdd_BN error of %s\n", ippGetStatusString(ret)));
  1531. ForceZero(pMont, pSz);
  1532. ForceZero(qMont, qSz);
  1533. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1534. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1535. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1536. return USER_CRYPTO_ERROR;
  1537. }
  1538. }
  1539. /* tmp = tmpb + q * tmp */
  1540. ret = ippsMul_BN(tmp, key->qipp, tmp);
  1541. if (ret != ippStsNoErr) {
  1542. USER_DEBUG(("ippsSub_BN error of %s\n", ippGetStatusString(ret)));
  1543. ForceZero(pMont, pSz);
  1544. ForceZero(qMont, qSz);
  1545. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1546. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1547. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1548. return USER_CRYPTO_ERROR;
  1549. }
  1550. ret = ippsAdd_BN(tmp, tmpb, tmp);
  1551. if (ret != ippStsNoErr) {
  1552. USER_DEBUG(("ippsSub_BN error of %s\n", ippGetStatusString(ret)));
  1553. ForceZero(pMont, pSz);
  1554. ForceZero(qMont, qSz);
  1555. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1556. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1557. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1558. return USER_CRYPTO_ERROR;
  1559. }
  1560. /* Extract the output */
  1561. ret = ippsGetOctString_BN(out, sz, tmp);
  1562. if (ret != ippStsNoErr) {
  1563. USER_DEBUG(("ippsGetOctString_BN error of %s\n",
  1564. ippGetStatusString(ret)));
  1565. ForceZero(pMont, pSz);
  1566. ForceZero(qMont, qSz);
  1567. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1568. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1569. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1570. return USER_CRYPTO_ERROR;
  1571. }
  1572. outSz = sz;
  1573. /* clear memory and free */
  1574. ForceZero(pMont, pSz);
  1575. ForceZero(qMont, qSz);
  1576. XFREE(qMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1577. XFREE(pMont, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1578. FreeSignHelper(one, tmp, tmpP, tmpQ, tmpa, tmpb);
  1579. return outSz;
  1580. }
  1581. int wc_RsaEncryptSize(RsaKey* key)
  1582. {
  1583. if (key == NULL)
  1584. return 0;
  1585. return key->sz;
  1586. }
  1587. /* flatten RsaKey structure into individual elements (e, n) */
  1588. int wc_RsaFlattenPublicKey(RsaKey* key, byte* e, word32* eSz, byte* n,
  1589. word32* nSz)
  1590. {
  1591. int sz, bytSz;
  1592. IppStatus ret;
  1593. USER_DEBUG(("Entering wc_RsaFlattenPublicKey\n"));
  1594. if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL)
  1595. return USER_CRYPTO_ERROR;
  1596. bytSz = sizeof(byte) * 8;
  1597. ret = ippsExtGet_BN(NULL, &sz, NULL, key->e);
  1598. if (ret != ippStsNoErr)
  1599. return USER_CRYPTO_ERROR;
  1600. /* sz is in bits change to bytes */
  1601. sz = (sz / bytSz) + ((sz % bytSz)? 1 : 0);
  1602. if (*eSz < (word32)sz)
  1603. return USER_CRYPTO_ERROR;
  1604. ret = ippsGetOctString_BN(e, sz, key->e);
  1605. if (ret != ippStsNoErr)
  1606. return USER_CRYPTO_ERROR;
  1607. *eSz = (word32)sz;
  1608. /* flatten n */
  1609. ret = ippsExtGet_BN(NULL, &sz, NULL, key->n);
  1610. if (ret != ippStsNoErr)
  1611. return USER_CRYPTO_ERROR;
  1612. /* sz is in bits change to bytes */
  1613. sz = (sz / bytSz) + ((sz % bytSz)? 1: 0);
  1614. if (*nSz < (word32)sz)
  1615. return USER_CRYPTO_ERROR;
  1616. ret = ippsGetOctString_BN(n, sz, key->n);
  1617. if (ret != ippStsNoErr)
  1618. return USER_CRYPTO_ERROR;
  1619. *nSz = (word32)sz;
  1620. return 0;
  1621. }
  1622. IppStatus wolfSSL_rng(Ipp32u* pData, int nBits, void* pEbsParams);
  1623. IppStatus wolfSSL_rng(Ipp32u* pData, int nBits, void* pEbsParams)
  1624. {
  1625. int nBytes;
  1626. if (pData == NULL) {
  1627. USER_DEBUG(("error with wolfSSL_rng argument\n"));
  1628. return ippStsErr;
  1629. }
  1630. nBytes = (nBits/8) + ((nBits % 8)? 1: 0);
  1631. if (wc_RNG_GenerateBlock((WC_RNG*)pEbsParams, (byte*)pData, nBytes) != 0) {
  1632. USER_DEBUG(("error in generating random wolfSSL block\n"));
  1633. return ippStsErr;
  1634. }
  1635. return ippStsNoErr;
  1636. }
  1637. #ifdef WOLFSSL_KEY_GEN
  1638. /* Make an RSA key for size bits, with e specified, 65537 is a good e */
  1639. int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
  1640. {
  1641. IppStatus ret;
  1642. int scratchSz;
  1643. int i; /* for trys on calling make key */
  1644. int ctxSz;
  1645. IppsBigNumState* pSrcPublicExp = NULL;
  1646. Ipp8u* scratchBuffer = NULL;
  1647. Ipp8u eAry[8];
  1648. int trys = 8; /* Miller-Rabin test parameter */
  1649. IppsPrimeState* pPrime = NULL;
  1650. int qBitSz; /* size of q factor */
  1651. int bytSz; /* size of key in bytes */
  1652. int leng;
  1653. USER_DEBUG(("Entering wc_MakeRsaKey\n"));
  1654. /* get byte size and individual private key size -- round up */
  1655. qBitSz = (size / 2) + ((size % 2)? 1: 0);
  1656. bytSz = (size / 8) + ((size % 8)? 1: 0);
  1657. if (key == NULL || rng == NULL) {
  1658. USER_DEBUG(("Error, NULL argument passed in\n"));
  1659. return USER_CRYPTO_ERROR;
  1660. }
  1661. if (e < 3 || (e&1) == 0)
  1662. return USER_CRYPTO_ERROR;
  1663. if (size > RSA_MAX_SIZE || size < RSA_MIN_SIZE)
  1664. return USER_CRYPTO_ERROR;
  1665. key->type = RSA_PRIVATE;
  1666. key->sz = bytSz;
  1667. /* initialize prime number */
  1668. ret = ippsPrimeGetSize(size, &ctxSz); /* size in bits */
  1669. if (ret != ippStsNoErr) {
  1670. USER_DEBUG(("ippsPrimeGetSize error of %s\n", ippGetStatusString(ret)));
  1671. ret = USER_CRYPTO_ERROR;
  1672. goto makeKeyEnd;
  1673. }
  1674. pPrime = (IppsPrimeState*)XMALLOC(ctxSz, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1675. if (pPrime == NULL) {
  1676. ret = USER_CRYPTO_ERROR;
  1677. goto makeKeyEnd;
  1678. }
  1679. ret = ippsPrimeInit(size, pPrime);
  1680. if (ret != ippStsNoErr) {
  1681. USER_DEBUG(("ippsPrimeInit error of %s\n", ippGetStatusString(ret)));
  1682. ret = USER_CRYPTO_ERROR;
  1683. goto makeKeyEnd;
  1684. }
  1685. /* define RSA privete key type 2 */
  1686. /* length in bits of p and q factors */
  1687. ret = ippsRSA_GetSizePrivateKeyType2(qBitSz, qBitSz, &ctxSz);
  1688. if (ret != ippStsNoErr) {
  1689. USER_DEBUG(("ippsRSA_GetSizePrivateKeyType2 error of %s\n",
  1690. ippGetStatusString(ret)));
  1691. ret = USER_CRYPTO_ERROR;
  1692. goto makeKeyEnd;
  1693. }
  1694. key->prvSz = ctxSz; /* used when freeing private key */
  1695. key->pPrv = (IppsRSAPrivateKeyState*)XMALLOC(ctxSz, NULL,
  1696. DYNAMIC_TYPE_USER_CRYPTO);
  1697. if (key->pPrv == NULL) {
  1698. ret = USER_CRYPTO_ERROR;
  1699. goto makeKeyEnd;
  1700. }
  1701. /* length in bits of p and q factors */
  1702. ret = ippsRSA_InitPrivateKeyType2(qBitSz, qBitSz, key->pPrv, ctxSz);
  1703. if (ret != ippStsNoErr) {
  1704. USER_DEBUG(("ippsRSA_InitPrivateKeyType2 error of %s\n",
  1705. ippGetStatusString(ret)));
  1706. ret = USER_CRYPTO_ERROR;
  1707. goto makeKeyEnd;
  1708. }
  1709. /* allocate scratch buffer */
  1710. ret = ippsRSA_GetBufferSizePrivateKey(&scratchSz, key->pPrv);
  1711. if (ret != ippStsNoErr) {
  1712. USER_DEBUG(("ippsRSA_GetBufferSizePrivateKey error of %s\n",
  1713. ippGetStatusString(ret)));
  1714. ret = USER_CRYPTO_ERROR;
  1715. goto makeKeyEnd;
  1716. }
  1717. scratchBuffer = (Ipp8u*)XMALLOC(scratchSz, 0, DYNAMIC_TYPE_USER_CRYPTO);
  1718. if (scratchBuffer == NULL) {
  1719. ret = USER_CRYPTO_ERROR;
  1720. goto makeKeyEnd;
  1721. }
  1722. /* set up initial value of pScrPublicExp */
  1723. leng = (int)sizeof(long); /* # of Ipp32u in long */
  1724. /* place the value of e into the array eAry then load into BN */
  1725. for (i = 0; i < leng; i++) {
  1726. eAry[i] = (e >> (8 * (leng - 1 - i))) & 0XFF;
  1727. }
  1728. ret = init_bn(&pSrcPublicExp, leng);
  1729. if (ret != ippStsNoErr) {
  1730. ret = USER_CRYPTO_ERROR;
  1731. goto makeKeyEnd;
  1732. }
  1733. ret = ippsSetOctString_BN(eAry, leng, pSrcPublicExp);
  1734. if (ret != ippStsNoErr) {
  1735. ret = USER_CRYPTO_ERROR;
  1736. goto makeKeyEnd;
  1737. }
  1738. /* initializing key->n */
  1739. ret = init_bn(&key->n, bytSz);
  1740. if (ret != ippStsNoErr) {
  1741. ret = USER_CRYPTO_ERROR;
  1742. goto makeKeyEnd;
  1743. }
  1744. /* initializing public exponent key->e */
  1745. ret = init_bn(&key->e, leng);
  1746. if (ret != ippStsNoErr) {
  1747. ret = USER_CRYPTO_ERROR;
  1748. goto makeKeyEnd;
  1749. }
  1750. /* private exponent key->dipp */
  1751. ret = init_bn(&key->dipp, bytSz);
  1752. if (ret != ippStsNoErr) {
  1753. ret = USER_CRYPTO_ERROR;
  1754. goto makeKeyEnd;
  1755. }
  1756. /* call IPP to generate keys, if inseficent entropy error call again */
  1757. ret = ippStsInsufficientEntropy;
  1758. while (ret == ippStsInsufficientEntropy) {
  1759. ret = ippsRSA_GenerateKeys(pSrcPublicExp, key->n, key->e,
  1760. key->dipp, key->pPrv, scratchBuffer, trys, pPrime,
  1761. wolfSSL_rng, rng);
  1762. if (ret == ippStsNoErr) {
  1763. break;
  1764. }
  1765. /* catch all errors other than entropy error */
  1766. if (ret != ippStsInsufficientEntropy) {
  1767. USER_DEBUG(("ippsRSA_GeneratKeys error of %s\n",
  1768. ippGetStatusString(ret)));
  1769. ret = USER_CRYPTO_ERROR;
  1770. goto makeKeyEnd;
  1771. }
  1772. }
  1773. /* get bn sizes needed for private key set up */
  1774. ret = ippsExtGet_BN(NULL, &key->eSz, NULL, key->e);
  1775. if (ret != ippStsNoErr) {
  1776. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  1777. ret = USER_CRYPTO_ERROR;
  1778. goto makeKeyEnd;
  1779. }
  1780. ret = ippsExtGet_BN(NULL, &key->nSz, NULL, key->n);
  1781. if (ret != ippStsNoErr) {
  1782. USER_DEBUG(("ippsGetSize_BN error %s\n", ippGetStatusString(ret)));
  1783. ret = USER_CRYPTO_ERROR;
  1784. goto makeKeyEnd;
  1785. }
  1786. /* set up public key state */
  1787. ret = ippsRSA_GetSizePublicKey(key->nSz, key->eSz, &ctxSz);
  1788. if (ret != ippStsNoErr) {
  1789. USER_DEBUG(("ippsRSA_GetSizePublicKey error %s nSz = %d eSz = %d\n",
  1790. ippGetStatusString(ret), key->nSz, key->eSz));
  1791. ret = USER_CRYPTO_ERROR;
  1792. goto makeKeyEnd;
  1793. }
  1794. key->pPub = (IppsRSAPublicKeyState*)XMALLOC(ctxSz, NULL,
  1795. DYNAMIC_TYPE_USER_CRYPTO);
  1796. if (key->pPub == NULL) {
  1797. ret = USER_CRYPTO_ERROR;
  1798. goto makeKeyEnd;
  1799. }
  1800. ret = ippsRSA_InitPublicKey(key->nSz, key->eSz, key->pPub, ctxSz);
  1801. if (ret != ippStsNoErr) {
  1802. USER_DEBUG(("ippsRSA_InitPublicKey error %s\n",
  1803. ippGetStatusString(ret)));
  1804. ret = USER_CRYPTO_ERROR;
  1805. goto makeKeyEnd;
  1806. }
  1807. ret = ippsRSA_SetPublicKey(key->n, key->e, key->pPub);
  1808. if (ret != ippStsNoErr) {
  1809. USER_DEBUG(("ippsRSA_SetPublicKey error %s\n",
  1810. ippGetStatusString(ret)));
  1811. ret = USER_CRYPTO_ERROR;
  1812. goto makeKeyEnd;
  1813. }
  1814. /* get private key information for key struct */
  1815. leng = size/16; /* size of q, p, u, dP, dQ */
  1816. ret = init_bn(&key->pipp, leng);
  1817. if (ret != ippStsNoErr) {
  1818. ret = USER_CRYPTO_ERROR;
  1819. goto makeKeyEnd;
  1820. }
  1821. /* set up q BN for key */
  1822. ret = init_bn(&key->qipp, leng);
  1823. if (ret != ippStsNoErr) {
  1824. ret = USER_CRYPTO_ERROR;
  1825. goto makeKeyEnd;
  1826. }
  1827. /* set up dP BN for key */
  1828. ret = init_bn(&key->dPipp, leng);
  1829. if (ret != ippStsNoErr) {
  1830. ret = USER_CRYPTO_ERROR;
  1831. goto makeKeyEnd;
  1832. }
  1833. /* set up dQ BN for key */
  1834. ret = init_bn(&key->dQipp, leng);
  1835. if (ret != ippStsNoErr) {
  1836. ret = USER_CRYPTO_ERROR;
  1837. goto makeKeyEnd;
  1838. }
  1839. /* set up u BN for key */
  1840. ret = init_bn(&key->uipp, leng);
  1841. if (ret != ippStsNoErr) {
  1842. ret = USER_CRYPTO_ERROR;
  1843. goto makeKeyEnd;
  1844. }
  1845. /* get values from created key */
  1846. ret = ippsRSA_GetPrivateKeyType2(key->pipp, key->qipp, key->dPipp,
  1847. key->dQipp, key->uipp, key->pPrv);
  1848. if (ret != ippStsNoErr) {
  1849. USER_DEBUG(("ippsRSA_GetPrivateKeyType2 error %s\n",
  1850. ippGetStatusString(ret)));
  1851. ret = USER_CRYPTO_ERROR;
  1852. goto makeKeyEnd;
  1853. }
  1854. ret = 0; /* success case */
  1855. makeKeyEnd:
  1856. /* clean up memory used */
  1857. XFREE(pSrcPublicExp, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1858. XFREE(scratchBuffer, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1859. XFREE(pPrime, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1860. if (ret != 0) { /* with fail case free RSA components created */
  1861. wc_FreeRsaKey(key);
  1862. }
  1863. return ret;
  1864. }
  1865. #endif
  1866. #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)
  1867. /********** duplicate code needed -- future refactor */
  1868. #define MAX_VERSION_SZ 5
  1869. #define MAX_SEQ_SZ 5
  1870. #define ASN_CONTEXT_SPECIFIC 0x80
  1871. #define ASN_CONSTRUCTED 0x20
  1872. #define ASN_LONG_LENGTH 0x80
  1873. #define ASN_SEQUENCE 0x10
  1874. #define RSA_INTS 8
  1875. #define FALSE 0
  1876. #define TRUE 1
  1877. #define MAX_LENGTH_SZ 4
  1878. #define RSAk 645
  1879. #define keyType 2
  1880. #define MAX_RSA_INT_SZ 517
  1881. #define MAX_RSA_E_SZ 16
  1882. #define MAX_ALGO_SZ 20
  1883. static word32 BytePrecision(word32 value)
  1884. {
  1885. word32 i;
  1886. for (i = sizeof(value); i; --i)
  1887. if (value >> ((i - 1) * WOLFSSL_BIT_SIZE))
  1888. break;
  1889. return i;
  1890. }
  1891. static int SetMyVersion(word32 version, byte* output, int header)
  1892. {
  1893. int i = 0;
  1894. if (output == NULL)
  1895. return USER_CRYPTO_ERROR;
  1896. if (header) {
  1897. output[i++] = ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED;
  1898. output[i++] = ASN_BIT_STRING;
  1899. }
  1900. output[i++] = ASN_INTEGER;
  1901. output[i++] = 0x01;
  1902. output[i++] = (byte)version;
  1903. return i;
  1904. }
  1905. static word32 SetLength(word32 length, byte* output)
  1906. {
  1907. word32 i = 0, j;
  1908. if (length < 0x80)
  1909. output[i++] = (byte)length;
  1910. else {
  1911. output[i++] = (byte)(BytePrecision(length) | ASN_LONG_LENGTH);
  1912. for (j = BytePrecision(length); j; --j) {
  1913. output[i] = (byte)(length >> ((j - 1) * WOLFSSL_BIT_SIZE));
  1914. i++;
  1915. }
  1916. }
  1917. return i;
  1918. }
  1919. static word32 SetSequence(word32 len, byte* output)
  1920. {
  1921. output[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
  1922. return SetLength(len, output + 1) + 1;
  1923. }
  1924. static word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz)
  1925. {
  1926. /* adding TAG_NULL and 0 to end */
  1927. /* RSA keyType */
  1928. #ifndef NO_RSA
  1929. static const byte RSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1930. 0x01, 0x01, 0x01, 0x05, 0x00};
  1931. #endif /* NO_RSA */
  1932. int algoSz = 0;
  1933. int tagSz = 2; /* tag null and terminator */
  1934. word32 idSz, seqSz;
  1935. const byte* algoName = 0;
  1936. byte ID_Length[MAX_LENGTH_SZ];
  1937. byte seqArray[MAX_SEQ_SZ + 1]; /* add object_id to end */
  1938. if (type == keyType) { /* keyType */
  1939. switch (algoOID) {
  1940. #ifndef NO_RSA
  1941. case RSAk:
  1942. algoSz = sizeof(RSA_AlgoID);
  1943. algoName = RSA_AlgoID;
  1944. break;
  1945. #endif /* NO_RSA */
  1946. default:
  1947. /* unknown key algo */
  1948. return 0;
  1949. }
  1950. }
  1951. else {
  1952. /* unknown algo type */
  1953. return 0;
  1954. }
  1955. idSz = SetLength(algoSz - tagSz, ID_Length); /* don't include tags */
  1956. seqSz = SetSequence(idSz + algoSz + 1 + curveSz, seqArray);
  1957. /* +1 for object id, curveID of curveSz follows for ecc */
  1958. seqArray[seqSz++] = ASN_OBJECT_ID;
  1959. XMEMCPY(output, seqArray, seqSz);
  1960. XMEMCPY(output + seqSz, ID_Length, idSz);
  1961. XMEMCPY(output + seqSz + idSz, algoName, algoSz);
  1962. return seqSz + idSz + algoSz;
  1963. }
  1964. /* Write a public RSA key to output */
  1965. static int SetRsaPublicKey(byte* output, RsaKey* key,
  1966. int outLen, int with_header)
  1967. {
  1968. #ifdef WOLFSSL_SMALL_STACK
  1969. byte* n = NULL;
  1970. byte* e = NULL;
  1971. #else
  1972. byte n[MAX_RSA_INT_SZ];
  1973. byte e[MAX_RSA_E_SZ];
  1974. #endif
  1975. byte seq[MAX_SEQ_SZ];
  1976. byte len[MAX_LENGTH_SZ + 1]; /* trailing 0 */
  1977. int nSz;
  1978. int eSz;
  1979. int seqSz;
  1980. int lenSz;
  1981. int idx;
  1982. int rawLen;
  1983. int leadingBit;
  1984. int err;
  1985. if (output == NULL || key == NULL || outLen < MAX_SEQ_SZ)
  1986. return USER_CRYPTO_ERROR;
  1987. /* n */
  1988. #ifdef WOLFSSL_SMALL_STACK
  1989. n = (byte*)XMALLOC(MAX_RSA_INT_SZ, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  1990. if (n == NULL)
  1991. return USER_CRYPTO_ERROR;
  1992. #endif
  1993. leadingBit = wc_Rsa_leading_bit(key->n);
  1994. rawLen = wc_Rsa_unsigned_bin_size(key->n);
  1995. if ((int)rawLen < 0) {
  1996. return USER_CRYPTO_ERROR;
  1997. }
  1998. rawLen = rawLen + leadingBit;
  1999. n[0] = ASN_INTEGER;
  2000. nSz = SetLength(rawLen, n + 1) + 1; /* int tag */
  2001. if ( (nSz + rawLen) < MAX_RSA_INT_SZ) {
  2002. if (leadingBit)
  2003. n[nSz] = 0;
  2004. err = ippsGetOctString_BN((Ipp8u*)n + nSz, rawLen - leadingBit, key->n);
  2005. if (err == ippStsNoErr)
  2006. nSz += rawLen;
  2007. else {
  2008. #ifdef WOLFSSL_SMALL_STACK
  2009. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2010. #endif
  2011. return USER_CRYPTO_ERROR;
  2012. }
  2013. }
  2014. else {
  2015. #ifdef WOLFSSL_SMALL_STACK
  2016. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2017. #endif
  2018. return USER_CRYPTO_ERROR;
  2019. }
  2020. /* e */
  2021. #ifdef WOLFSSL_SMALL_STACK
  2022. e = (byte*)XMALLOC(MAX_RSA_E_SZ, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2023. if (e == NULL) {
  2024. #ifdef WOLFSSL_SMALL_STACK
  2025. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2026. #endif
  2027. return USER_CRYPTO_ERROR;
  2028. }
  2029. #endif
  2030. leadingBit = wc_Rsa_leading_bit(key->e);
  2031. rawLen = wc_Rsa_unsigned_bin_size(key->e);
  2032. if ((int)rawLen < 0) {
  2033. return USER_CRYPTO_ERROR;
  2034. }
  2035. rawLen = rawLen + leadingBit;
  2036. e[0] = ASN_INTEGER;
  2037. eSz = SetLength(rawLen, e + 1) + 1; /* int tag */
  2038. if ( (eSz + rawLen) < MAX_RSA_E_SZ) {
  2039. if (leadingBit)
  2040. e[eSz] = 0;
  2041. err = ippsGetOctString_BN((Ipp8u*)e + eSz, rawLen - leadingBit, key->e);
  2042. if (err == ippStsNoErr)
  2043. eSz += rawLen;
  2044. else {
  2045. #ifdef WOLFSSL_SMALL_STACK
  2046. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2047. XFREE(e, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2048. #endif
  2049. return USER_CRYPTO_ERROR;
  2050. }
  2051. }
  2052. else {
  2053. #ifdef WOLFSSL_SMALL_STACK
  2054. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2055. XFREE(e, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2056. #endif
  2057. return USER_CRYPTO_ERROR;
  2058. }
  2059. seqSz = SetSequence(nSz + eSz, seq);
  2060. /* check output size */
  2061. if ( (seqSz + nSz + eSz) > outLen) {
  2062. #ifdef WOLFSSL_SMALL_STACK
  2063. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2064. XFREE(e, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2065. #endif
  2066. return USER_CRYPTO_ERROR;
  2067. }
  2068. /* headers */
  2069. if (with_header) {
  2070. int algoSz;
  2071. #ifdef WOLFSSL_SMALL_STACK
  2072. byte* algo;
  2073. algo = (byte*)XMALLOC(MAX_ALGO_SZ, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2074. if (algo == NULL) {
  2075. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2076. XFREE(e, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2077. return USER_CRYPTO_ERROR;
  2078. }
  2079. #else
  2080. byte algo[MAX_ALGO_SZ];
  2081. #endif
  2082. algoSz = SetAlgoID(RSAk, algo, keyType, 0);
  2083. lenSz = SetLength(seqSz + nSz + eSz + 1, len);
  2084. len[lenSz++] = 0; /* trailing 0 */
  2085. /* write, 1 is for ASN_BIT_STRING */
  2086. idx = SetSequence(nSz + eSz + seqSz + lenSz + 1 + algoSz, output);
  2087. /* check output size */
  2088. if ( (idx + algoSz + 1 + lenSz + seqSz + nSz + eSz) > outLen) {
  2089. #ifdef WOLFSSL_SMALL_STACK
  2090. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2091. XFREE(e, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2092. XFREE(algo, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2093. #endif
  2094. return USER_CRYPTO_ERROR;
  2095. }
  2096. /* algo */
  2097. XMEMCPY(output + idx, algo, algoSz);
  2098. idx += algoSz;
  2099. /* bit string */
  2100. output[idx++] = ASN_BIT_STRING;
  2101. /* length */
  2102. XMEMCPY(output + idx, len, lenSz);
  2103. idx += lenSz;
  2104. #ifdef WOLFSSL_SMALL_STACK
  2105. XFREE(algo, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2106. #endif
  2107. }
  2108. else
  2109. idx = 0;
  2110. /* seq */
  2111. XMEMCPY(output + idx, seq, seqSz);
  2112. idx += seqSz;
  2113. /* n */
  2114. XMEMCPY(output + idx, n, nSz);
  2115. idx += nSz;
  2116. /* e */
  2117. XMEMCPY(output + idx, e, eSz);
  2118. idx += eSz;
  2119. #ifdef WOLFSSL_SMALL_STACK
  2120. XFREE(n, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2121. XFREE(e, NULL, DYNAMIC_TYPE_USER_CRYPTO);
  2122. #endif
  2123. return idx;
  2124. }
  2125. static IppsBigNumState* GetRsaInt(RsaKey* key, int idx)
  2126. {
  2127. if (idx == 0)
  2128. return key->n;
  2129. if (idx == 1)
  2130. return key->e;
  2131. if (idx == 2)
  2132. return key->dipp;
  2133. if (idx == 3)
  2134. return key->pipp;
  2135. if (idx == 4)
  2136. return key->qipp;
  2137. if (idx == 5)
  2138. return key->dPipp;
  2139. if (idx == 6)
  2140. return key->dQipp;
  2141. if (idx == 7)
  2142. return key->uipp;
  2143. return NULL;
  2144. }
  2145. /* Release Tmp RSA resources */
  2146. static WC_INLINE void FreeTmpRsas(byte** tmps, void* heap)
  2147. {
  2148. int i;
  2149. (void)heap;
  2150. for (i = 0; i < RSA_INTS; i++)
  2151. XFREE(tmps[i], heap, DYNAMIC_TYPE_USER_CRYPTO);
  2152. }
  2153. /* Convert RsaKey key to DER format, write to output (inLen), return bytes
  2154. written */
  2155. int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen)
  2156. {
  2157. word32 seqSz, verSz, rawLen, intTotalLen = 0;
  2158. word32 sizes[RSA_INTS];
  2159. int i, j, outLen, ret = 0, lbit;
  2160. byte seq[MAX_SEQ_SZ];
  2161. byte ver[MAX_VERSION_SZ];
  2162. byte* tmps[RSA_INTS];
  2163. USER_DEBUG(("Entering RsaKeyToDer\n"));
  2164. if (!key)
  2165. return USER_CRYPTO_ERROR;
  2166. if (key->type != RSA_PRIVATE)
  2167. return USER_CRYPTO_ERROR;
  2168. for (i = 0; i < RSA_INTS; i++)
  2169. tmps[i] = NULL;
  2170. /* write all big ints from key to DER tmps */
  2171. for (i = 0; i < RSA_INTS; i++) {
  2172. Ipp32u isZero;
  2173. IppsBigNumState* keyInt = GetRsaInt(key, i);
  2174. ippsCmpZero_BN(keyInt, &isZero); /* makes isZero 0 if true */
  2175. rawLen = wc_Rsa_unsigned_bin_size(keyInt);
  2176. if ((int)rawLen < 0) {
  2177. return USER_CRYPTO_ERROR;
  2178. }
  2179. /* leading zero */
  2180. if (!isZero || wc_Rsa_leading_bit(keyInt))
  2181. lbit = 1;
  2182. else
  2183. lbit = 0;
  2184. rawLen += lbit;
  2185. tmps[i] = (byte*)XMALLOC(rawLen + MAX_SEQ_SZ, key->heap,
  2186. DYNAMIC_TYPE_USER_CRYPTO);
  2187. if (tmps[i] == NULL) {
  2188. ret = USER_CRYPTO_ERROR;
  2189. break;
  2190. }
  2191. tmps[i][0] = ASN_INTEGER;
  2192. sizes[i] = SetLength(rawLen, tmps[i] + 1) + 1 + lbit; /* tag & lbit */
  2193. if (sizes[i] <= MAX_SEQ_SZ) {
  2194. int err;
  2195. /* leading zero */
  2196. if (lbit)
  2197. tmps[i][sizes[i]-1] = 0x00;
  2198. /* extract data*/
  2199. err = ippsGetOctString_BN((Ipp8u*)(tmps[i] + sizes[i]),
  2200. rawLen - lbit, keyInt);
  2201. if (err == ippStsOk) {
  2202. sizes[i] += (rawLen-lbit); /* lbit included in rawLen */
  2203. intTotalLen += sizes[i];
  2204. ret = 0;
  2205. }
  2206. else {
  2207. ret = USER_CRYPTO_ERROR;
  2208. USER_DEBUG(("ippsGetOctString_BN error %s\n",
  2209. ippGetStatusString(err)));
  2210. break;
  2211. }
  2212. }
  2213. else {
  2214. ret = USER_CRYPTO_ERROR;
  2215. break;
  2216. }
  2217. }
  2218. if (ret != 0) {
  2219. FreeTmpRsas(tmps, key->heap);
  2220. return ret;
  2221. }
  2222. /* make headers */
  2223. verSz = SetMyVersion(0, ver, FALSE);
  2224. seqSz = SetSequence(verSz + intTotalLen, seq);
  2225. outLen = seqSz + verSz + intTotalLen;
  2226. if (output) {
  2227. if (outLen > (int)inLen) {
  2228. return USER_CRYPTO_ERROR;
  2229. }
  2230. /* write to output */
  2231. XMEMCPY(output, seq, seqSz);
  2232. j = seqSz;
  2233. XMEMCPY(output + j, ver, verSz);
  2234. j += verSz;
  2235. for (i = 0; i < RSA_INTS; i++) {
  2236. XMEMCPY(output + j, tmps[i], sizes[i]);
  2237. j += sizes[i];
  2238. }
  2239. }
  2240. FreeTmpRsas(tmps, key->heap);
  2241. return outLen;
  2242. }
  2243. /* Convert Rsa Public key to DER format, write to output (inLen), return bytes
  2244. written
  2245. */
  2246. int wc_RsaKeyToPublicDer(RsaKey* key, byte* output, word32 inLen)
  2247. {
  2248. return SetRsaPublicKey(output, key, inLen, 1);
  2249. }
  2250. /* Returns public DER version of the RSA key. If with_header is 0 then only a
  2251. * seq + n + e is returned in ASN.1 DER format */
  2252. int wc_RsaKeyToPublicDer_ex(RsaKey* key, byte* output, word32 inLen,
  2253. int with_header)
  2254. {
  2255. return SetRsaPublicKey(output, key, inLen, with_header);
  2256. }
  2257. #endif /* WOLFSSL_KEY_GEN || OPENSSL_EXTRA */
  2258. #ifdef WC_RSA_BLINDING
  2259. int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng)
  2260. {
  2261. if (key == NULL)
  2262. return USER_CRYPTO_ERROR;
  2263. (void)rng;
  2264. return 0;
  2265. }
  2266. #endif /* WC_RSA_BLINDING */
  2267. #endif /* NO_RSA */