se050_port.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289
  1. /* se050_port.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 <stdint.h>
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #ifdef WOLFSSL_SE050
  27. #include <wolfssl/wolfcrypt/types.h> /* for MATH_INT_T */
  28. #include <wolfssl/wolfcrypt/wc_port.h>
  29. #include <wolfssl/wolfcrypt/aes.h>
  30. #include <wolfssl/wolfcrypt/error-crypt.h>
  31. #include <wolfssl/wolfcrypt/ed25519.h>
  32. #include <wolfssl/wolfcrypt/logging.h>
  33. #include <wolfssl/wolfcrypt/curve25519.h>
  34. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  35. #ifdef WOLFSSL_SE050_INIT
  36. #ifndef SE050_DEFAULT_PORT
  37. #define SE050_DEFAULT_PORT "/dev/i2c-1"
  38. #endif
  39. #include "ex_sss_boot.h"
  40. #endif
  41. #ifdef HAVE_ECC
  42. #include <wolfssl/wolfcrypt/ecc.h>
  43. struct ecc_key;
  44. #ifndef SE050_ECC_DER_MAX
  45. #define SE050_ECC_DER_MAX 256
  46. #endif
  47. #endif
  48. #ifndef NO_RSA
  49. #include <wolfssl/wolfcrypt/rsa.h>
  50. struct RsaKey;
  51. #endif
  52. #include <wolfssl/wolfcrypt/asn.h>
  53. #ifndef SE050_KEYID_START
  54. #define SE050_KEYID_START 100
  55. #endif
  56. /* enable for debugging */
  57. /* #define SE050_DEBUG*/
  58. /* enable to factory erase chip */
  59. /* #define WOLFSSL_SE050_FACTORY_RESET */
  60. /* Global variables */
  61. static sss_session_t *cfg_se050_i2c_pi;
  62. static sss_key_store_t *gHostKeyStore;
  63. static sss_key_store_t *gHeyStore;
  64. int wc_se050_set_config(sss_session_t *pSession, sss_key_store_t *pHostKeyStore,
  65. sss_key_store_t *pKeyStore)
  66. {
  67. WOLFSSL_MSG("Setting SE050 session configuration");
  68. cfg_se050_i2c_pi = pSession;
  69. gHostKeyStore = pHostKeyStore;
  70. gHeyStore = pKeyStore;
  71. return 0;
  72. }
  73. #ifdef WOLFSSL_SE050_INIT
  74. int wc_se050_init(const char* portName)
  75. {
  76. int ret;
  77. sss_status_t status;
  78. static ex_sss_boot_ctx_t pCtx;
  79. if (portName == NULL) {
  80. portName = SE050_DEFAULT_PORT;
  81. }
  82. status = ex_sss_boot_open(&pCtx, portName);
  83. if (status == kStatus_SSS_Success) {
  84. ret = wc_se050_set_config(&pCtx.session,
  85. #if SSS_HAVE_HOSTCRYPTO_ANY
  86. &pCtx.host_ks,
  87. #else
  88. NULL,
  89. #endif
  90. &pCtx.ks);
  91. #ifdef WOLFSSL_SE050_FACTORY_RESET
  92. ex_sss_boot_factory_reset(&pCtx);
  93. #endif
  94. }
  95. else {
  96. WOLFSSL_MSG("Failed to open SE050 context");
  97. ret = WC_HW_E;
  98. }
  99. return ret;
  100. }
  101. #endif
  102. /**
  103. * Erase and free an object stored in SE050.
  104. *
  105. * keyId ID of object to erase
  106. *
  107. * Returns 0 on success, negative on error.
  108. */
  109. int wc_se050_erase_object(word32 id)
  110. {
  111. int ret = 0;
  112. sss_object_t object;
  113. sss_key_store_t host_keystore;
  114. sss_status_t status = kStatus_SSS_Success;
  115. #ifdef SE050_DEBUG
  116. printf("wc_se050_erase_object: id %d\n", id);
  117. #endif
  118. if (cfg_se050_i2c_pi == NULL) {
  119. return BAD_FUNC_ARG;
  120. }
  121. if (wolfSSL_CryptHwMutexLock() != 0) {
  122. return BAD_MUTEX_E;
  123. }
  124. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  125. if (status == kStatus_SSS_Success) {
  126. status = sss_key_store_allocate(&host_keystore,
  127. SE050_KEYSTOREID_GENERIC);
  128. }
  129. if (status == kStatus_SSS_Success) {
  130. status = sss_key_object_init(&object, &host_keystore);
  131. }
  132. if (status == kStatus_SSS_Success) {
  133. status = sss_key_object_get_handle(&object, id);
  134. }
  135. if (status == kStatus_SSS_Success) {
  136. sss_key_store_erase_key(&host_keystore, &object);
  137. sss_key_object_free(&object);
  138. }
  139. wolfSSL_CryptHwMutexUnLock();
  140. if (status != kStatus_SSS_Success) {
  141. ret = WC_HW_E;
  142. }
  143. return ret;
  144. }
  145. word32 se050_allocate_key(int keyType)
  146. {
  147. word32 keyId = 0;
  148. static word32 keyId_allocator = SE050_KEYID_START;
  149. switch (keyType) {
  150. case SE050_AES_KEY:
  151. case SE050_ECC_KEY:
  152. case SE050_RSA_KEY:
  153. case SE050_ED25519_KEY:
  154. case SE050_CURVE25519_KEY:
  155. case SE050_ANY_KEY:
  156. keyId = keyId_allocator++;
  157. break;
  158. }
  159. #ifdef SE050_DEBUG
  160. printf("se050_allocate_key: keyId %d\n", keyId);
  161. #endif
  162. return keyId;
  163. }
  164. #if !defined(WC_NO_RNG) && !defined(WOLFSSL_SE050_NO_TRNG)
  165. int se050_get_random_number(uint32_t count, uint8_t* rand_out)
  166. {
  167. int ret = 0;
  168. sss_status_t status;
  169. sss_rng_context_t rng;
  170. #ifdef SE050_DEBUG
  171. printf("se050_get_random_number: %p (%d)\n", rand_out, count);
  172. #endif
  173. if (cfg_se050_i2c_pi == NULL) {
  174. return WC_HW_E;
  175. }
  176. if (wolfSSL_CryptHwMutexLock() != 0) {
  177. return BAD_MUTEX_E;
  178. }
  179. status = sss_rng_context_init(&rng, cfg_se050_i2c_pi);
  180. if (status == kStatus_SSS_Success) {
  181. status = sss_rng_get_random(&rng, rand_out, count);
  182. }
  183. if (status == kStatus_SSS_Success) {
  184. status = sss_rng_context_free(&rng);
  185. }
  186. if (status != kStatus_SSS_Success) {
  187. ret = RNG_FAILURE_E;
  188. }
  189. wolfSSL_CryptHwMutexUnLock();
  190. return ret;
  191. }
  192. #endif /* !WC_NO_RNG && !WOLFSSL_SE050_NO_TRNG */
  193. #ifdef WOLFSSL_SE050_HASH
  194. /* Used for sha/sha224/sha384/sha512 */
  195. int se050_hash_init(SE050_HASH_Context* se050Ctx, void* heap)
  196. {
  197. se050Ctx->heap = heap;
  198. se050Ctx->len = 0;
  199. se050Ctx->used = 0;
  200. se050Ctx->msg = NULL;
  201. return 0;
  202. }
  203. int se050_hash_copy(SE050_HASH_Context* src, SE050_HASH_Context* dst)
  204. {
  205. if (src == NULL || dst == NULL || (src->used != dst->used)) {
  206. return BAD_FUNC_ARG;
  207. }
  208. if (src->used > 0) {
  209. /* dst->msg points to same buffer as src->msg, needs to be allocated
  210. * and dep copied over instead of plain pointer copy */
  211. dst->msg = (byte*)XMALLOC(src->used, dst->heap,
  212. DYNAMIC_TYPE_TMP_BUFFER);
  213. if (dst->msg == NULL) {
  214. PRINTF("Tried to allocate %d bytes\n", dst->used);
  215. return MEMORY_E;
  216. }
  217. XMEMSET(dst->msg, 0, dst->used);
  218. XMEMCPY(dst->msg, src->msg, src->used);
  219. dst->used = src->used;
  220. dst->len = src->used;
  221. } else {
  222. dst->msg = NULL;
  223. dst->len = 0;
  224. dst->used = 0;
  225. }
  226. return 0;
  227. }
  228. int se050_hash_update(SE050_HASH_Context* se050Ctx, const byte* data, word32 len)
  229. {
  230. byte* tmp = NULL;
  231. if (se050Ctx == NULL || (len > 0 && data == NULL)) {
  232. return BAD_FUNC_ARG;
  233. }
  234. if (se050Ctx->len < se050Ctx->used + len) {
  235. if (se050Ctx->msg == NULL) {
  236. se050Ctx->msg = (byte*)XMALLOC(se050Ctx->used + len,
  237. se050Ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  238. XMEMSET(se050Ctx->msg, 0, se050Ctx->used + len);
  239. }
  240. else {
  241. tmp = (byte*)XMALLOC(se050Ctx->used + len, se050Ctx->heap,
  242. DYNAMIC_TYPE_TMP_BUFFER);
  243. if (tmp == NULL) {
  244. return MEMORY_E;
  245. }
  246. XMEMSET(tmp, 0, se050Ctx->used + len);
  247. XMEMCPY(tmp, se050Ctx->msg, se050Ctx->used);
  248. XFREE(se050Ctx->msg, se050Ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  249. se050Ctx->msg = tmp;
  250. }
  251. if (se050Ctx->msg == NULL) {
  252. return MEMORY_E;
  253. }
  254. se050Ctx->len = se050Ctx->used + len;
  255. }
  256. XMEMCPY(se050Ctx->msg + se050Ctx->used, data, len);
  257. se050Ctx->used += len;
  258. return 0;
  259. }
  260. int se050_hash_final(SE050_HASH_Context* se050Ctx, byte* hash, size_t digestLen,
  261. sss_algorithm_t algo)
  262. {
  263. int ret;
  264. sss_status_t status;
  265. sss_digest_t digest_ctx;
  266. const byte* data = se050Ctx->msg;
  267. int size = (se050Ctx->used) / SSS_BLOCK_SIZE;
  268. int leftover = (se050Ctx->used) % SSS_BLOCK_SIZE;
  269. const byte* blocks = data;
  270. if (cfg_se050_i2c_pi == NULL) {
  271. return WC_HW_E;
  272. }
  273. if (wolfSSL_CryptHwMutexLock() != 0) {
  274. return BAD_MUTEX_E;
  275. }
  276. status = sss_digest_context_init(&digest_ctx, cfg_se050_i2c_pi, algo,
  277. kMode_SSS_Digest);
  278. if (status == kStatus_SSS_Success) {
  279. status = sss_digest_init(&digest_ctx);
  280. }
  281. if (status == kStatus_SSS_Success) {
  282. /* used to send chunks of size 512 */
  283. while (status == kStatus_SSS_Success && size--) {
  284. status = sss_digest_update(&digest_ctx, blocks, SSS_BLOCK_SIZE);
  285. blocks += SSS_BLOCK_SIZE;
  286. }
  287. if (status == kStatus_SSS_Success && leftover) {
  288. status = sss_digest_update(&digest_ctx, blocks, leftover);
  289. }
  290. if (status == kStatus_SSS_Success) {
  291. status = sss_digest_finish(&digest_ctx, hash, &digestLen);
  292. }
  293. sss_digest_context_free(&digest_ctx);
  294. }
  295. if (status == kStatus_SSS_Success) {
  296. /* reset state */
  297. ret = se050_hash_init(se050Ctx, se050Ctx->heap);
  298. } else {
  299. ret = WC_HW_E;
  300. }
  301. wolfSSL_CryptHwMutexUnLock();
  302. return ret;
  303. }
  304. void se050_hash_free(SE050_HASH_Context* se050Ctx)
  305. {
  306. XFREE(se050Ctx->msg, se050Ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  307. se050Ctx->msg = NULL;
  308. se050Ctx->len = 0;
  309. se050Ctx->used = 0;
  310. }
  311. #endif /* WOLFSSL_SE050_HASH */
  312. #if defined(WOLFSSL_SE050_CRYPT) && !defined(NO_AES)
  313. int se050_aes_set_key(Aes* aes, const byte* key, word32 keylen,
  314. const byte* iv, int dir)
  315. {
  316. int ret = 0;
  317. sss_status_t status;
  318. sss_object_t newKey;
  319. sss_key_store_t host_keystore;
  320. word32 keyId;
  321. int keyCreated = 0;
  322. if (cfg_se050_i2c_pi == NULL) {
  323. return WC_HW_E;
  324. }
  325. if (wolfSSL_CryptHwMutexLock() != 0) {
  326. return BAD_MUTEX_E;
  327. }
  328. (void)dir;
  329. (void)iv;
  330. aes->rounds = keylen/4 + 6;
  331. /* free existing key in slot first before storing new one */
  332. ret = wc_se050_erase_object(aes->keyId);
  333. if (ret != 0) {
  334. return ret;
  335. }
  336. aes->keyIdSet = 0;
  337. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  338. if (status == kStatus_SSS_Success) {
  339. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_AES);
  340. }
  341. if (status == kStatus_SSS_Success) {
  342. status = sss_key_object_init(&newKey, &host_keystore);
  343. }
  344. if (status == kStatus_SSS_Success) {
  345. keyId = se050_allocate_key(SE050_AES_KEY);
  346. status = sss_key_object_allocate_handle(&newKey, keyId,
  347. kSSS_KeyPart_Default, kSSS_CipherType_AES, keylen,
  348. kKeyObject_Mode_Transient);
  349. }
  350. if (status == kStatus_SSS_Success) {
  351. status = sss_key_store_set_key(&host_keystore, &newKey, key, keylen,
  352. keylen * 8, NULL, 0);
  353. }
  354. if (status == kStatus_SSS_Success) {
  355. keyCreated = 1;
  356. aes->keyId = keyId;
  357. aes->keyIdSet = 1;
  358. ret = 0;
  359. }
  360. else {
  361. if (keyCreated) {
  362. sss_key_store_erase_key(&host_keystore, &newKey);
  363. sss_key_object_free(&newKey);
  364. }
  365. ret = WC_HW_E;
  366. }
  367. wolfSSL_CryptHwMutexUnLock();
  368. return ret;
  369. }
  370. int se050_aes_crypt(Aes* aes, const byte* in, byte* out, word32 sz, int dir,
  371. sss_algorithm_t algorithm)
  372. {
  373. int ret = 0;
  374. sss_status_t status;
  375. sss_object_t keyObject;
  376. sss_key_store_t host_keystore;
  377. if (cfg_se050_i2c_pi == NULL) {
  378. return WC_HW_E;
  379. }
  380. if (aes->keyIdSet == 0) {
  381. return BAD_FUNC_ARG;
  382. }
  383. if (wolfSSL_CryptHwMutexLock() != 0) {
  384. return BAD_MUTEX_E;
  385. }
  386. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  387. if (status == kStatus_SSS_Success) {
  388. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_AES);
  389. }
  390. if (status == kStatus_SSS_Success) {
  391. status = sss_key_object_init(&keyObject, &host_keystore);
  392. }
  393. if (status == kStatus_SSS_Success) {
  394. status = sss_key_object_get_handle(&keyObject, aes->keyId);
  395. }
  396. /* The first call to this function needs an initialization call,
  397. * subsequent calls just need to call update */
  398. if (status == kStatus_SSS_Success && aes->ctxInitDone == 0) {
  399. sss_mode_t mode;
  400. XMEMSET(&mode, 0, sizeof(mode));
  401. if (dir == AES_DECRYPTION)
  402. mode = kMode_SSS_Decrypt;
  403. else if (dir == AES_ENCRYPTION)
  404. mode = kMode_SSS_Encrypt;
  405. if (status == kStatus_SSS_Success) {
  406. status = sss_symmetric_context_init(&aes->aes_ctx,
  407. cfg_se050_i2c_pi, &keyObject, algorithm, mode);
  408. }
  409. if (status == kStatus_SSS_Success) {
  410. aes->ctxInitDone = 1;
  411. status = sss_cipher_init(&aes->aes_ctx, (uint8_t*)aes->reg,
  412. sizeof(aes->reg));
  413. }
  414. }
  415. if (status == kStatus_SSS_Success) {
  416. size_t outSz = (size_t)sz;
  417. status = sss_cipher_update(&aes->aes_ctx, in, sz, out, &outSz);
  418. }
  419. ret = (status == kStatus_SSS_Success) ? 0 : WC_HW_E;
  420. wolfSSL_CryptHwMutexUnLock();
  421. return ret;
  422. }
  423. void se050_aes_free(Aes* aes)
  424. {
  425. if (aes == NULL) {
  426. return;
  427. }
  428. if (aes->ctxInitDone) {
  429. sss_symmetric_context_free(&aes->aes_ctx);
  430. /* sets back to zero to indicate that a free has been called */
  431. aes->ctxInitDone = 0;
  432. }
  433. aes->keyId = 0;
  434. aes->keyIdSet = 0;
  435. }
  436. #endif /* WOLFSSL_SE050_CRYPT && !NO_AES */
  437. /**
  438. * Get size of a SE05X secure object at specified object ID.
  439. *
  440. * keystore SE050 keystore associated with object
  441. * keyId SE050 key ID in which object is stored
  442. *
  443. * Size returned depends on object type:
  444. * ECC key: curve size
  445. * RSA/AES/DES/HMAC key: key size
  446. * Binary file: file size
  447. *
  448. * Return size or negative on error
  449. */
  450. static int se050_get_object_size(sss_key_store_t* keystore, word32 keyId)
  451. {
  452. uint16_t size = 0;
  453. smStatus_t status = SM_NOT_OK;
  454. sss_se05x_key_store_t* se05x_keystore = NULL;
  455. if (keystore == NULL) {
  456. return BAD_FUNC_ARG;
  457. }
  458. se05x_keystore = (sss_se05x_key_store_t*)keystore;
  459. status = Se05x_API_ReadSize(&se05x_keystore->session->s_ctx,
  460. keyId, &size);
  461. if (status != SM_OK) {
  462. return WC_HW_E;
  463. }
  464. return (int)size;
  465. }
  466. /**
  467. * Insert binary object into SE050 as persistent object.
  468. *
  469. * keyId SE050 key ID to store object in
  470. * object binary object data
  471. * objectSz size of binary object, bytes
  472. *
  473. * Returns 0 on success, negative on error
  474. */
  475. int wc_se050_insert_binary_object(word32 keyId, const byte* object,
  476. word32 objectSz)
  477. {
  478. int ret = 0;
  479. sss_object_t newObj;
  480. sss_key_store_t host_keystore;
  481. sss_status_t status = kStatus_SSS_Success;
  482. if (wolfSSL_CryptHwMutexLock() != 0) {
  483. return BAD_MUTEX_E;
  484. }
  485. /* Avoid key ID conflicts with temporary key storage */
  486. if (keyId >= SE050_KEYID_START) {
  487. return BAD_FUNC_ARG;
  488. }
  489. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  490. if (status == kStatus_SSS_Success) {
  491. status = sss_key_object_init(&newObj, &host_keystore);
  492. }
  493. if (status == kStatus_SSS_Success) {
  494. status = sss_key_object_allocate_handle(&newObj, keyId,
  495. kSSS_KeyPart_Default, kSSS_CipherType_Binary, objectSz,
  496. kKeyObject_Mode_Persistent);
  497. }
  498. if (status == kStatus_SSS_Success) {
  499. status = sss_key_store_set_key(&host_keystore, &newObj, object,
  500. objectSz, (objectSz * 8), NULL, 0);
  501. }
  502. wolfSSL_CryptHwMutexUnLock();
  503. if (status != kStatus_SSS_Success) {
  504. ret = WC_HW_E;
  505. }
  506. return ret;
  507. }
  508. /**
  509. * Get binary object from SE050 from specified key ID.
  510. *
  511. * keyId SE050 key ID to get binary object from
  512. * out output buffer to place binary object
  513. * outSz size of output buffer on input, size of written object on output
  514. *
  515. * Returns 0 on success, LENGTH_ONLY_E if out is NULL with outSz set to
  516. * required buffer size, and other negative on error.
  517. */
  518. int wc_se050_get_binary_object(word32 keyId, byte* out, word32* outSz)
  519. {
  520. int ret = 0;
  521. sss_object_t object;
  522. sss_key_store_t host_keystore;
  523. sss_status_t status = kStatus_SSS_Success;
  524. size_t outBitSz = 0;
  525. /* If out is NULL, outSz set to required size and LENGTH_ONLY_E returned */
  526. if (outSz == NULL) {
  527. return BAD_FUNC_ARG;
  528. }
  529. if (wolfSSL_CryptHwMutexLock() != 0) {
  530. return BAD_MUTEX_E;
  531. }
  532. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  533. if (status == kStatus_SSS_Success) {
  534. status = sss_key_object_init(&object, &host_keystore);
  535. }
  536. if (status == kStatus_SSS_Success) {
  537. ret = se050_get_object_size(&host_keystore, keyId);
  538. if (ret < 0) {
  539. status = kStatus_SSS_Fail;
  540. }
  541. else {
  542. if (out == NULL) {
  543. *outSz = ret;
  544. return LENGTH_ONLY_E;
  545. }
  546. if ((word32)ret > *outSz) {
  547. WOLFSSL_MSG("Output buffer not large enough for object");
  548. return BAD_LENGTH_E;
  549. }
  550. ret = 0;
  551. }
  552. }
  553. if (status == kStatus_SSS_Success) {
  554. status = sss_key_object_get_handle(&object, keyId);
  555. }
  556. if (status == kStatus_SSS_Success) {
  557. outBitSz = (*outSz) * 8;
  558. status = sss_key_store_get_key(&host_keystore, &object, out,
  559. (size_t*)outSz, &outBitSz);
  560. }
  561. wolfSSL_CryptHwMutexUnLock();
  562. if (status != kStatus_SSS_Success) {
  563. ret = WC_HW_E;
  564. }
  565. return ret;
  566. }
  567. #ifndef NO_RSA
  568. /**
  569. * Use specified SE050 key ID with this RsaKey struct.
  570. * Should be called by wc_RsaUseKeyId() for using pre-populated
  571. * SE050 keys.
  572. *
  573. * key Pointer to initialized RsaKey structure
  574. * keyId SE050 key ID containing RSA key object
  575. *
  576. * Return 0 on success, negative on error.
  577. */
  578. int se050_rsa_use_key_id(struct RsaKey* key, word32 keyId)
  579. {
  580. int ret = 0;
  581. sss_object_t keyObject;
  582. sss_key_store_t host_keystore;
  583. sss_status_t status = kStatus_SSS_Success;
  584. uint8_t* derBuf = NULL;
  585. size_t derSz = 0;
  586. size_t derSzBits = 0;
  587. word32 idx = 0;
  588. #ifdef SE050_DEBUG
  589. printf("se050_rsa_use_key_id: key %p, keyId %d\n", key, keyId);
  590. #endif
  591. if (key == NULL) {
  592. return BAD_FUNC_ARG;
  593. }
  594. if (cfg_se050_i2c_pi == NULL) {
  595. return WC_HW_E;
  596. }
  597. if (wolfSSL_CryptHwMutexLock() != 0) {
  598. return BAD_MUTEX_E;
  599. }
  600. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  601. if (status == kStatus_SSS_Success) {
  602. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_RSA);
  603. }
  604. if (status == kStatus_SSS_Success) {
  605. status = sss_key_object_init(&keyObject, &host_keystore);
  606. }
  607. if (status == kStatus_SSS_Success) {
  608. status = sss_key_object_get_handle(&keyObject, keyId);
  609. }
  610. if (status == kStatus_SSS_Success) {
  611. ret = se050_get_object_size(&host_keystore, keyObject.keyId);
  612. if (ret <= 0) {
  613. status = kStatus_SSS_Fail;
  614. }
  615. else {
  616. /* double derSz to allow for ASN.1 encoding space */
  617. derSz = ((size_t)ret) * 2;
  618. ret = 0;
  619. derBuf = (uint8_t*)XMALLOC(derSz, key->heap,
  620. DYNAMIC_TYPE_TMP_BUFFER);
  621. if (derBuf == NULL) {
  622. WOLFSSL_MSG("Error calling malloc for RSA DER buffer");
  623. status = kStatus_SSS_Fail;
  624. }
  625. }
  626. }
  627. if (status == kStatus_SSS_Success) {
  628. derSzBits = derSz * 8;
  629. XMEMSET(derBuf, 0, derSz);
  630. status = sss_key_store_get_key(&host_keystore, &keyObject,
  631. derBuf, &derSz, &derSzBits);
  632. (void)derSzBits; /* not used */
  633. }
  634. if (status == kStatus_SSS_Success) {
  635. /* Populate RsaKey with general key info, for wolfCrypt to use */
  636. ret = wc_RsaPublicKeyDecode(derBuf, &idx, key, (word32)derSz);
  637. if (ret != 0) {
  638. status = kStatus_SSS_Fail;
  639. }
  640. }
  641. if (derBuf != NULL) {
  642. XFREE(derBuf, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  643. }
  644. if (status == kStatus_SSS_Success) {
  645. key->keyId = keyId;
  646. key->keyIdSet = 1;
  647. key->type = RSA_PRIVATE;
  648. ret = 0;
  649. }
  650. else if (ret == 0) {
  651. ret = WC_HW_E;
  652. }
  653. sss_key_object_free(&keyObject);
  654. wolfSSL_CryptHwMutexUnLock();
  655. #ifdef SE050_DEBUG
  656. printf("se050_rsa_use_key_id: ret %d\n", ret);
  657. #endif
  658. return ret;
  659. }
  660. /**
  661. * Get SE050 key ID associated with this RsaKey struct.
  662. * Should be called by wc_RsaGetKeyId() for the application to get
  663. * what key ID wolfCrypt picked for this RsaKey struct when generating
  664. * a key inside the SE050.
  665. *
  666. * key Pointer to initialized RsaKey structure
  667. * keyId [OUT] SE050 key ID associated with this key structure
  668. *
  669. * Return 0 on success, negative on error.
  670. */
  671. int se050_rsa_get_key_id(struct RsaKey* key, word32* keyId)
  672. {
  673. int ret = 0;
  674. if (key == NULL || keyId == NULL) {
  675. return BAD_FUNC_ARG;
  676. }
  677. if (key->keyIdSet == 1) {
  678. *keyId = key->keyId;
  679. } else {
  680. WOLFSSL_MSG("SE050 key ID not set for RsaKey struct");
  681. ret = WC_HW_E;
  682. }
  683. return ret;
  684. }
  685. /**
  686. * Create RSA key pair inside SE050.
  687. *
  688. * key RsaKey structure to store generated key information in
  689. * size RSA key size to generate in bytes
  690. * e RSA exponent, must be 65537 for SE050 compatibility
  691. *
  692. * Returns 0 on success, negative on error.
  693. */
  694. int se050_rsa_create_key(struct RsaKey* key, int size, long e)
  695. {
  696. int ret = 0;
  697. word32 keyId;
  698. int keyCreated = 0;
  699. sss_status_t status = kStatus_SSS_Success;
  700. sss_object_t keyPair;
  701. sss_key_store_t host_keystore;
  702. uint8_t* derBuf = NULL;
  703. size_t derSz = 0;
  704. size_t derSzBits = 0;
  705. word32 idx = 0;
  706. #ifdef SE050_DEBUG
  707. printf("se050_rsa_create_key: key %p, size %d, e %ld\n", key, size, e);
  708. #endif
  709. if (cfg_se050_i2c_pi == NULL) {
  710. return WC_HW_E;
  711. }
  712. if (e != 65537) {
  713. WOLFSSL_MSG("SE050 RSA key create only supports exponent of 65537");
  714. return WC_HW_E;
  715. }
  716. if (wolfSSL_CryptHwMutexLock() != 0) {
  717. return BAD_MUTEX_E;
  718. }
  719. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  720. if (status == kStatus_SSS_Success) {
  721. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_RSA);
  722. }
  723. if (status == kStatus_SSS_Success) {
  724. status = sss_key_object_init(&keyPair, &host_keystore);
  725. }
  726. if (status == kStatus_SSS_Success) {
  727. keyId = se050_allocate_key(SE050_RSA_KEY);
  728. status = sss_key_object_allocate_handle(&keyPair, keyId,
  729. kSSS_KeyPart_Pair, kSSS_CipherType_RSA, (size / 8),
  730. kKeyObject_Mode_Persistent);
  731. }
  732. if (status == kStatus_SSS_Success) {
  733. /* Try to delete existing key first. Ignore return since will fail
  734. * if no key exists */
  735. sss_key_store_erase_key(&host_keystore, &keyPair);
  736. keyCreated = 1;
  737. status = sss_key_store_generate_key(&host_keystore, &keyPair,
  738. size, NULL);
  739. }
  740. if (status == kStatus_SSS_Success) {
  741. ret = se050_get_object_size(&host_keystore, keyPair.keyId);
  742. if (ret <= 0) {
  743. status = kStatus_SSS_Fail;
  744. }
  745. else {
  746. /* double derSz to allow for ASN.1 encoding space */
  747. derSz = ((size_t)ret) * 2;
  748. ret = 0;
  749. derBuf = (uint8_t*)XMALLOC(derSz, key->heap,
  750. DYNAMIC_TYPE_TMP_BUFFER);
  751. if (derBuf == NULL) {
  752. WOLFSSL_MSG("Error calling malloc for RSA DER buffer");
  753. status = kStatus_SSS_Fail;
  754. }
  755. }
  756. }
  757. if (status == kStatus_SSS_Success) {
  758. derSzBits = derSz * 8;
  759. XMEMSET(derBuf, 0, derSz);
  760. status = sss_key_store_get_key(&host_keystore, &keyPair,
  761. derBuf, &derSz, &derSzBits);
  762. (void)derSzBits; /* not used */
  763. }
  764. if (status == kStatus_SSS_Success) {
  765. ret = wc_RsaPublicKeyDecode(derBuf, &idx, key, (word32)derSz);
  766. if (ret != 0) {
  767. status = kStatus_SSS_Fail;
  768. }
  769. }
  770. if (derBuf != NULL) {
  771. XFREE(derBuf, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  772. }
  773. if (status == kStatus_SSS_Success) {
  774. key->keyId = keyId;
  775. key->keyIdSet = 1;
  776. key->type = RSA_PRIVATE;
  777. ret = 0;
  778. }
  779. else {
  780. if (keyCreated) {
  781. sss_key_store_erase_key(&host_keystore, &keyPair);
  782. sss_key_object_free(&keyPair);
  783. }
  784. if (ret == 0) {
  785. ret = WC_HW_E;
  786. }
  787. }
  788. wolfSSL_CryptHwMutexUnLock();
  789. #ifdef SE050_DEBUG
  790. printf("se050_rsa_create_key: key %p, ret %d, keyId %d\n",
  791. key, ret, key->keyId);
  792. #endif
  793. return ret;
  794. }
  795. static int se050_rsa_insert_key(word32 keyId, const byte* rsaDer,
  796. word32 rsaDerSize, int keyType)
  797. {
  798. int ret = 0;
  799. int keySize;
  800. word32 idx = 0;
  801. sss_object_t newKey;
  802. sss_key_store_t host_keystore;
  803. sss_status_t status = kStatus_SSS_Success;
  804. struct RsaKey key;
  805. sss_key_part_t keyPart = kSSS_KeyPart_Pair;
  806. if (wolfSSL_CryptHwMutexLock() != 0) {
  807. return BAD_MUTEX_E;
  808. }
  809. /* Avoid key ID conflicts with temporary key storage */
  810. if (keyId >= SE050_KEYID_START) {
  811. return BAD_FUNC_ARG;
  812. }
  813. ret = wc_InitRsaKey(&key, NULL);
  814. if (ret != 0) {
  815. status = kStatus_SSS_Fail;
  816. }
  817. else {
  818. if (keyType == RSA_PUBLIC) {
  819. keyPart = kSSS_KeyPart_Public;
  820. ret = wc_RsaPublicKeyDecode(rsaDer, &idx, &key, rsaDerSize);
  821. }
  822. else if (keyType == RSA_PRIVATE) {
  823. keyPart = kSSS_KeyPart_Pair;
  824. ret = wc_RsaPrivateKeyDecode(rsaDer, &idx, &key, rsaDerSize);
  825. }
  826. else {
  827. ret = BAD_FUNC_ARG;
  828. }
  829. if (ret != 0) {
  830. status = kStatus_SSS_Fail;
  831. }
  832. }
  833. if (status == kStatus_SSS_Success) {
  834. keySize = wc_RsaEncryptSize(&key);
  835. if (keySize < 0) {
  836. status = kStatus_SSS_Fail;
  837. }
  838. }
  839. if (status == kStatus_SSS_Success) {
  840. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  841. }
  842. if (status == kStatus_SSS_Success) {
  843. status = sss_key_object_init(&newKey, &host_keystore);
  844. }
  845. if (status == kStatus_SSS_Success) {
  846. status = sss_key_object_allocate_handle(&newKey, keyId,
  847. keyPart, kSSS_CipherType_RSA, keySize,
  848. kKeyObject_Mode_Persistent);
  849. }
  850. if (status == kStatus_SSS_Success) {
  851. status = sss_key_store_set_key(&host_keystore, &newKey, rsaDer,
  852. rsaDerSize, (keySize * 8), NULL, 0);
  853. }
  854. wolfSSL_CryptHwMutexUnLock();
  855. wc_FreeRsaKey(&key);
  856. if (status != kStatus_SSS_Success) {
  857. if (ret == 0) {
  858. ret = WC_HW_E;
  859. }
  860. }
  861. return ret;
  862. }
  863. /**
  864. * Insert DER encoded RSA private key into SE050 as a persistent key.
  865. *
  866. * keyId SE050 key ID to store key into
  867. * rsaDer DER encoded RSA private key
  868. * rsaDerSize size of DER buffer, bytes
  869. *
  870. * Returns 0 on success, negative on error
  871. */
  872. int wc_se050_rsa_insert_private_key(word32 keyId, const byte* rsaDer,
  873. word32 rsaDerSize)
  874. {
  875. return se050_rsa_insert_key(keyId, rsaDer, rsaDerSize, RSA_PRIVATE);
  876. }
  877. /**
  878. * Insert DER encoded RSA public key into SE050 as a persistent key.
  879. *
  880. * keyId SE050 key ID to store key into
  881. * rsaDer DER encoded RSA public key
  882. * rsaDerSize size of DER buffer, bytes
  883. *
  884. * Returns 0 on success, negative on error
  885. */
  886. int wc_se050_rsa_insert_public_key(word32 keyId, const byte* rsaDer,
  887. word32 rsaDerSize)
  888. {
  889. return se050_rsa_insert_key(keyId, rsaDer, rsaDerSize, RSA_PUBLIC);
  890. }
  891. /**
  892. * Return sss_algorithm_t type for RSA sign/verify based on wolfCrypt pad type,
  893. * hash value, and mask generation function (mgf).
  894. *
  895. * padType padding type
  896. * hash hash function
  897. * mgf mask generation function (for PSS)
  898. *
  899. * Returns algorithm type or kAlgorithm_None if none supported found
  900. */
  901. static sss_algorithm_t se050_get_rsa_signature_type(int padType,
  902. enum wc_HashType hash, int mgf)
  903. {
  904. sss_algorithm_t alg = kAlgorithm_None;
  905. switch (padType) {
  906. case WC_RSA_PKCSV15_PAD:
  907. if (hash == WC_HASH_TYPE_NONE) {
  908. alg = kAlgorithm_SSS_RSASSA_PKCS1_V1_5_NO_HASH;
  909. } else if (hash == WC_HASH_TYPE_SHA) {
  910. alg = kAlgorithm_SSS_RSASSA_PKCS1_V1_5_SHA1;
  911. } else if (hash == WC_HASH_TYPE_SHA224) {
  912. alg = kAlgorithm_SSS_RSASSA_PKCS1_V1_5_SHA224;
  913. } else if (hash == WC_HASH_TYPE_SHA256) {
  914. alg = kAlgorithm_SSS_RSASSA_PKCS1_V1_5_SHA256;
  915. } else if (hash == WC_HASH_TYPE_SHA384) {
  916. alg = kAlgorithm_SSS_RSASSA_PKCS1_V1_5_SHA384;
  917. } else if (hash == WC_HASH_TYPE_SHA512) {
  918. alg = kAlgorithm_SSS_RSASSA_PKCS1_V1_5_SHA512;
  919. }
  920. break;
  921. case WC_RSA_OAEP_PAD:
  922. if (hash == WC_HASH_TYPE_SHA) {
  923. alg = kAlgorithm_SSS_RSAES_PKCS1_OAEP_SHA1;
  924. } else if (hash == WC_HASH_TYPE_SHA224) {
  925. alg = kAlgorithm_SSS_RSAES_PKCS1_OAEP_SHA224;
  926. } else if (hash == WC_HASH_TYPE_SHA256) {
  927. alg = kAlgorithm_SSS_RSAES_PKCS1_OAEP_SHA256;
  928. } else if (hash == WC_HASH_TYPE_SHA384) {
  929. alg = kAlgorithm_SSS_RSAES_PKCS1_OAEP_SHA384;
  930. } else if (hash == WC_HASH_TYPE_SHA512) {
  931. alg = kAlgorithm_SSS_RSAES_PKCS1_OAEP_SHA512;
  932. }
  933. break;
  934. case WC_RSA_PSS_PAD:
  935. if (mgf == WC_MGF1SHA1) {
  936. alg = kAlgorithm_SSS_RSASSA_PKCS1_PSS_MGF1_SHA1;
  937. } else if (mgf == WC_MGF1SHA224) {
  938. alg = kAlgorithm_SSS_RSASSA_PKCS1_PSS_MGF1_SHA224;
  939. } else if (mgf == WC_MGF1SHA256) {
  940. alg = kAlgorithm_SSS_RSASSA_PKCS1_PSS_MGF1_SHA256;
  941. } else if (mgf == WC_MGF1SHA384) {
  942. alg = kAlgorithm_SSS_RSASSA_PKCS1_PSS_MGF1_SHA384;
  943. } else if (mgf == WC_MGF1SHA512) {
  944. alg = kAlgorithm_SSS_RSASSA_PKCS1_PSS_MGF1_SHA512;
  945. }
  946. break;
  947. case WC_RSA_NO_PAD:
  948. alg = kAlgorithm_SSS_RSASSA_NO_PADDING;
  949. break;
  950. default:
  951. break;
  952. }
  953. return alg;
  954. }
  955. static sss_algorithm_t se050_get_rsa_encrypt_type(int padType,
  956. enum wc_HashType hash)
  957. {
  958. sss_algorithm_t alg = kAlgorithm_None;
  959. (void)hash;
  960. switch (padType) {
  961. case WC_RSA_PKCSV15_PAD:
  962. alg = kAlgorithm_SSS_RSAES_PKCS1_V1_5;
  963. break;
  964. case WC_RSA_OAEP_PAD:
  965. /* lower level Se05x API translation maps OAEP-SHA1 alg type to
  966. * kSE05x_RSAEncryptionAlgo_PKCS1_OAEP (generic) */
  967. alg = kAlgorithm_SSS_RSAES_PKCS1_OAEP_SHA1;
  968. break;
  969. case WC_RSA_NO_PAD:
  970. alg = kAlgorithm_SSS_RSASSA_NO_PADDING;
  971. break;
  972. default:
  973. break;
  974. }
  975. return alg;
  976. }
  977. /**
  978. * RSA sign operation.
  979. *
  980. * in input data to be signed
  981. * inLen length of input data, bytes
  982. * out output buffer containing signature
  983. * outLen length of output buffer, bytes
  984. * key pointer to initialized/populated RsaKey structure
  985. * rsa_type type of RSA: must be RSA_PRIVATE_ENCRYPT
  986. * pad_value should be RSA_BLOCK_TYPE_1 for signing
  987. * pad_type type of padding: WC_RSA_PKCSV15_PAD, WC_RSA_OAEP_PAD,
  988. * WC_RSA_NO_PAD, WC_RSA_PSS_PAD
  989. * hash type of hash algorithm, found in wolfssl/wolfcrypt/hash.h
  990. * mgf type of mask generation function to use
  991. * label optional label, not supported by SE050, must be NULL
  992. * labelSz size of label, not supported by SE050, must be 0
  993. * keySz size of RSA key, bytes
  994. *
  995. * Return size of signature on success, negative on error.
  996. */
  997. int se050_rsa_sign(const byte* in, word32 inLen, byte* out,
  998. word32 outLen, struct RsaKey* key, int rsa_type,
  999. byte pad_value, int pad_type, enum wc_HashType hash,
  1000. int mgf, byte* label, word32 labelSz, int keySz)
  1001. {
  1002. int ret = 0;
  1003. int keyCreated = 0;
  1004. word32 keyId;
  1005. size_t sigSz;
  1006. sss_object_t newKey;
  1007. sss_status_t status;
  1008. sss_key_store_t host_keystore;
  1009. sss_algorithm_t algorithm = kAlgorithm_None;
  1010. sss_asymmetric_t ctx_asymm;
  1011. byte* derBuf = NULL;
  1012. int derSz = 0;
  1013. /* SE050 does not support optional label */
  1014. (void)label;
  1015. (void)labelSz;
  1016. #ifdef SE050_DEBUG
  1017. printf("se050_rsa_sign: key %p, in %p (%d), out %p (%d), "
  1018. "key %p, type %d, pad_value = %d, pad_type = %d, mgf = %d\n",
  1019. key, in, inLen, out, outLen, key, rsa_type, pad_value,
  1020. pad_type, mgf);
  1021. #endif
  1022. if (in == NULL || out == NULL || key == NULL ||
  1023. rsa_type != RSA_PRIVATE_ENCRYPT || pad_value != RSA_BLOCK_TYPE_1) {
  1024. return BAD_FUNC_ARG;
  1025. }
  1026. if (cfg_se050_i2c_pi == NULL) {
  1027. return WC_HW_E;
  1028. }
  1029. if (wolfSSL_CryptHwMutexLock() != 0) {
  1030. return BAD_MUTEX_E;
  1031. }
  1032. algorithm = se050_get_rsa_signature_type(pad_type, hash, mgf);
  1033. if (algorithm == kAlgorithm_None) {
  1034. WOLFSSL_MSG("Unsupported padding/hash/mgf combination for SE050");
  1035. return BAD_FUNC_ARG;
  1036. }
  1037. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  1038. if (status == kStatus_SSS_Success) {
  1039. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_RSA);
  1040. }
  1041. if (status == kStatus_SSS_Success) {
  1042. status = sss_key_object_init(&newKey, &host_keystore);
  1043. }
  1044. if (status == kStatus_SSS_Success) {
  1045. keyId = key->keyId;
  1046. if (key->keyIdSet == 0) {
  1047. /* key was not generated in SE050, export RsaKey to DER
  1048. * and use that to store into SE050 keystore */
  1049. derSz = wc_RsaKeyToDer(key, NULL, 0);
  1050. if (derSz < 0) {
  1051. status = kStatus_SSS_Fail;
  1052. ret = derSz;
  1053. }
  1054. else {
  1055. derBuf = (byte*)XMALLOC(derSz, key->heap,
  1056. DYNAMIC_TYPE_TMP_BUFFER);
  1057. if (derBuf == NULL) {
  1058. WOLFSSL_MSG("malloc failed when converting RsaKey to DER");
  1059. status = kStatus_SSS_Fail;
  1060. ret = MEMORY_E;
  1061. }
  1062. }
  1063. if (status == kStatus_SSS_Success) {
  1064. derSz = wc_RsaKeyToDer(key, derBuf, derSz);
  1065. if (derSz < 0) {
  1066. status = kStatus_SSS_Fail;
  1067. ret = derSz;
  1068. }
  1069. }
  1070. if (status == kStatus_SSS_Success) {
  1071. keyId = se050_allocate_key(SE050_RSA_KEY);
  1072. status = sss_key_object_allocate_handle(&newKey, keyId,
  1073. kSSS_KeyPart_Pair, kSSS_CipherType_RSA, keySz,
  1074. kKeyObject_Mode_Persistent);
  1075. }
  1076. if (status == kStatus_SSS_Success) {
  1077. /* Try to delete existing key first, ignore return since will
  1078. * fail if no key exists yet */
  1079. sss_key_store_erase_key(&host_keystore, &newKey);
  1080. keyCreated = 1;
  1081. status = sss_key_store_set_key(&host_keystore, &newKey, derBuf,
  1082. derSz, (keySz * 8), NULL, 0);
  1083. }
  1084. if (derBuf != NULL) {
  1085. XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1086. }
  1087. }
  1088. else {
  1089. status = sss_key_object_get_handle(&newKey, keyId);
  1090. }
  1091. }
  1092. if (status == kStatus_SSS_Success) {
  1093. status = sss_asymmetric_context_init(&ctx_asymm, cfg_se050_i2c_pi,
  1094. &newKey, algorithm, kMode_SSS_Sign);
  1095. if (status == kStatus_SSS_Success) {
  1096. sigSz = outLen;
  1097. status = sss_asymmetric_sign_digest(&ctx_asymm, (uint8_t*)in, inLen,
  1098. out, &sigSz);
  1099. }
  1100. sss_asymmetric_context_free(&ctx_asymm);
  1101. }
  1102. if (status == kStatus_SSS_Success) {
  1103. key->keyId = keyId;
  1104. key->keyIdSet = 1;
  1105. ret = sigSz;
  1106. }
  1107. else {
  1108. if (keyCreated) {
  1109. sss_key_store_erase_key(&host_keystore, &newKey);
  1110. sss_key_object_free(&newKey);
  1111. }
  1112. if (ret == 0) {
  1113. ret = WC_HW_E;
  1114. }
  1115. }
  1116. wolfSSL_CryptHwMutexUnLock();
  1117. #ifdef SE050_DEBUG
  1118. printf("se050_rsa_sign: ret %d, outLen %d\n", ret, outLen);
  1119. #endif
  1120. return ret;
  1121. }
  1122. /**
  1123. * RSA verify operation.
  1124. *
  1125. * in input signature to be verified
  1126. * inLen length of sig, bytes
  1127. * out output buffer containing decoded data
  1128. * outLen length of output buffer, bytes
  1129. * key pointer to initialized/populated RsaKey structure
  1130. * rsa_type type of RSA: must be RSA_PUBLIC_DECRYPT
  1131. * pad_value should be RSA_BLOCK_TYPE_1 for sign/verify
  1132. * pad_type type of padding: WC_RSA_PKCSV15_PAD, WC_RSA_OAEP_PAD,
  1133. * WC_RSA_NO_PAD, WC_RSA_PSS_PAD
  1134. * hash type of hash algorithm, found in wolfssl/wolfcrypt/hash.h
  1135. * mgf type of mask generation function to use
  1136. * label optional label, not supported by SE050, must be NULL
  1137. * labelSz size of label, not supported by SE050, must be 0
  1138. *
  1139. * Returns size of decoded data on success, negative on error.
  1140. */
  1141. int se050_rsa_verify(const byte* in, word32 inLen, byte* out, word32 outLen,
  1142. struct RsaKey* key, int rsa_type, byte pad_value,
  1143. int pad_type, enum wc_HashType hash, int mgf, byte* label,
  1144. word32 labelSz)
  1145. {
  1146. int ret = 0;
  1147. word32 keyId;
  1148. int keySz;
  1149. int keyCreated = 0;
  1150. size_t decLen = 0;
  1151. sss_status_t status;
  1152. sss_object_t newKey;
  1153. sss_key_store_t host_keystore;
  1154. sss_asymmetric_t ctx_asymm = {0};
  1155. sss_se05x_asymmetric_t* se050_ctx_asymm = NULL;
  1156. sss_algorithm_t algorithm = kAlgorithm_None;
  1157. smStatus_t smStatus = SM_NOT_OK;
  1158. byte* pad = NULL;
  1159. byte* derBuf = NULL;
  1160. int derSz = 0;
  1161. #ifdef SE050_DEBUG
  1162. printf("se050_rsa_pkcs1v15_verify: key %p, in %p (%d), out %p (%d)\n",
  1163. key, in, inLen, out, outLen);
  1164. #endif
  1165. if (in == NULL || out == NULL || key == NULL ||
  1166. rsa_type != RSA_PUBLIC_DECRYPT || pad_value != RSA_BLOCK_TYPE_1) {
  1167. return BAD_FUNC_ARG;
  1168. }
  1169. if (cfg_se050_i2c_pi == NULL) {
  1170. return WC_HW_E;
  1171. }
  1172. if (wolfSSL_CryptHwMutexLock() != 0) {
  1173. return BAD_MUTEX_E;
  1174. }
  1175. algorithm = se050_get_rsa_signature_type(pad_type, hash, mgf);
  1176. if (algorithm == kAlgorithm_None) {
  1177. WOLFSSL_MSG("Unsupported padding/hash/mgf combination for SE050");
  1178. return BAD_FUNC_ARG;
  1179. }
  1180. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  1181. if (status == kStatus_SSS_Success) {
  1182. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_RSA);
  1183. }
  1184. if (status == kStatus_SSS_Success) {
  1185. status = sss_key_object_init(&newKey, &host_keystore);
  1186. }
  1187. if (status == kStatus_SSS_Success) {
  1188. keySz = wc_RsaEncryptSize(key);
  1189. if (keySz < 0) {
  1190. WOLFSSL_MSG("Failed to get RSA key size from struct");
  1191. status = kStatus_SSS_Fail;
  1192. }
  1193. }
  1194. if (status == kStatus_SSS_Success) {
  1195. keyId = key->keyId;
  1196. if (key->keyIdSet == 0) {
  1197. /* key was not generated in SE050, export RsaKey to DER
  1198. * and use that to store into SE050 keystore */
  1199. derSz = wc_RsaKeyToPublicDer(key, NULL, 0);
  1200. if (derSz < 0) {
  1201. status = kStatus_SSS_Fail;
  1202. ret = derSz;
  1203. }
  1204. else {
  1205. derBuf = (byte*)XMALLOC(derSz, key->heap,
  1206. DYNAMIC_TYPE_TMP_BUFFER);
  1207. if (derBuf == NULL) {
  1208. WOLFSSL_MSG("malloc failed when converting RsaKey to DER");
  1209. status = kStatus_SSS_Fail;
  1210. ret = MEMORY_E;
  1211. }
  1212. }
  1213. if (status == kStatus_SSS_Success) {
  1214. derSz = wc_RsaKeyToPublicDer(key, derBuf, derSz);
  1215. if (derSz < 0) {
  1216. status = kStatus_SSS_Fail;
  1217. ret = derSz;
  1218. }
  1219. }
  1220. if (status == kStatus_SSS_Success) {
  1221. keyId = se050_allocate_key(SE050_RSA_KEY);
  1222. status = sss_key_object_allocate_handle(&newKey, keyId,
  1223. kSSS_KeyPart_Public, kSSS_CipherType_RSA, keySz,
  1224. kKeyObject_Mode_Persistent);
  1225. }
  1226. if (status == kStatus_SSS_Success) {
  1227. /* Try to delete existing key first, ignore return since will
  1228. * fail if no key exists yet */
  1229. sss_key_store_erase_key(&host_keystore, &newKey);
  1230. keyCreated = 1;
  1231. status = sss_key_store_set_key(&host_keystore, &newKey, derBuf,
  1232. derSz, (keySz * 8), NULL, 0);
  1233. }
  1234. if (derBuf != NULL) {
  1235. XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1236. }
  1237. }
  1238. else {
  1239. status = sss_key_object_get_handle(&newKey, keyId);
  1240. }
  1241. }
  1242. if (status == kStatus_SSS_Success) {
  1243. status = sss_asymmetric_context_init(&ctx_asymm, cfg_se050_i2c_pi,
  1244. &newKey, algorithm, kMode_SSS_Verify);
  1245. if (status == kStatus_SSS_Success) {
  1246. /* Use lower Se05x API instead of sss_asymmetric_verify_digest()
  1247. * since we need to return decoded data not just verify result */
  1248. decLen = outLen;
  1249. se050_ctx_asymm = (sss_se05x_asymmetric_t*)&ctx_asymm;
  1250. smStatus = Se05x_API_RSAEncrypt(&se050_ctx_asymm->session->s_ctx,
  1251. se050_ctx_asymm->keyObject->keyId,
  1252. kSE05x_RSAEncryptionAlgo_NO_PAD,
  1253. in, inLen, out, &decLen);
  1254. if (smStatus == SM_OK) {
  1255. /* find end of padding, pad points to start of actual data */
  1256. ret = wc_RsaUnPad_ex(out, decLen, &pad, pad_value,
  1257. pad_type, hash, mgf,
  1258. label, labelSz, RSA_PSS_SALT_LEN_DEFAULT, (keySz * 8),
  1259. key->heap);
  1260. if (ret >= 0) {
  1261. XMEMCPY(out, pad, ret);
  1262. }
  1263. else {
  1264. WOLFSSL_MSG("Error in wc_RsaUnPad_ex for RSA verify");
  1265. status = kStatus_SSS_Fail;
  1266. }
  1267. }
  1268. else {
  1269. WOLFSSL_MSG("Se05x_API_RSAEncrypt failed");
  1270. status = kStatus_SSS_Fail;
  1271. }
  1272. }
  1273. sss_asymmetric_context_free(&ctx_asymm);
  1274. }
  1275. if (status == kStatus_SSS_Success) {
  1276. key->keyId = keyId;
  1277. key->keyIdSet = 1;
  1278. }
  1279. else {
  1280. if (keyCreated) {
  1281. sss_key_store_erase_key(&host_keystore, &newKey);
  1282. sss_key_object_free(&newKey);
  1283. }
  1284. if (ret == 0) {
  1285. ret = WC_HW_E;
  1286. }
  1287. }
  1288. wolfSSL_CryptHwMutexUnLock();
  1289. #ifdef SE050_DEBUG
  1290. printf("se050_rsa_verify: key %p, ret %d\n", key, ret);
  1291. #endif
  1292. return ret;
  1293. }
  1294. /**
  1295. * RSA public encrypt operation.
  1296. *
  1297. * in input data to be encrypted
  1298. * inLen length of input data, bytes
  1299. * out output buffer containing encrypted data
  1300. * outLen length of output buffer, bytes
  1301. * key pointer to initialized/populated RsaKey structure
  1302. * rsa_type type of RSA: must be RSA_PUBLIC_ENCRYPT
  1303. * pad_value should be RSA_BLOCK_TYPE_2 for encrypting
  1304. * pad_type type of padding: WC_RSA_PKCSV15_PAD, WC_RSA_OAEP_PAD,
  1305. * WC_RSA_NO_PAD, WC_RSA_PSS_PAD
  1306. * hash type of hash algorithm, found in wolfssl/wolfcrypt/hash.h
  1307. * mgf type of mask generation function to use
  1308. * label optional label, not supported by SE050, must be NULL
  1309. * labelSz size of label, not supported by SE050, must be 0
  1310. * keySz size of RSA key, bytes
  1311. *
  1312. * Returns size of encrypted data on success, negative on error.
  1313. */
  1314. int se050_rsa_public_encrypt(const byte* in, word32 inLen, byte* out,
  1315. word32 outLen, struct RsaKey* key, int rsa_type,
  1316. byte pad_value, int pad_type,
  1317. enum wc_HashType hash, int mgf, byte* label,
  1318. word32 labelSz, int keySz)
  1319. {
  1320. int ret = 0;
  1321. int keyCreated = 0;
  1322. word32 keyId;
  1323. size_t encSz;
  1324. sss_object_t newKey;
  1325. sss_status_t status;
  1326. sss_key_store_t host_keystore;
  1327. sss_algorithm_t algorithm = kAlgorithm_None;
  1328. sss_asymmetric_t ctx_asymm;
  1329. byte* derBuf = NULL;
  1330. int derSz = 0;
  1331. /* SE050 does not support optional label */
  1332. (void)label;
  1333. (void)labelSz;
  1334. (void)mgf;
  1335. #ifdef SE050_DEBUG
  1336. printf("se050_rsa_public_encrypt: key %p, in %p (%d), out %p (%d), "
  1337. "key %p\n", key, in, inLen, out, outLen, key);
  1338. #endif
  1339. if (in == NULL || out == NULL || key == NULL ||
  1340. rsa_type != RSA_PUBLIC_ENCRYPT || pad_value != RSA_BLOCK_TYPE_2) {
  1341. return BAD_FUNC_ARG;
  1342. }
  1343. if (cfg_se050_i2c_pi == NULL) {
  1344. return WC_HW_E;
  1345. }
  1346. if (wolfSSL_CryptHwMutexLock() != 0) {
  1347. return BAD_MUTEX_E;
  1348. }
  1349. algorithm = se050_get_rsa_encrypt_type(pad_type, hash);
  1350. if (algorithm == kAlgorithm_None) {
  1351. WOLFSSL_MSG("Unsupported padding/hash/mgf combination for SE050");
  1352. return BAD_FUNC_ARG;
  1353. }
  1354. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  1355. if (status == kStatus_SSS_Success) {
  1356. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_RSA);
  1357. }
  1358. if (status == kStatus_SSS_Success) {
  1359. status = sss_key_object_init(&newKey, &host_keystore);
  1360. }
  1361. if (status == kStatus_SSS_Success) {
  1362. keyId = key->keyId;
  1363. if (key->keyIdSet == 0) {
  1364. /* key was not generated in SE050, export RsaKey to DER
  1365. * and use that to store into SE050 keystore */
  1366. derSz = wc_RsaKeyToPublicDer(key, NULL, 0);
  1367. if (derSz < 0) {
  1368. status = kStatus_SSS_Fail;
  1369. ret = derSz;
  1370. }
  1371. else {
  1372. derBuf = (byte*)XMALLOC(derSz, key->heap,
  1373. DYNAMIC_TYPE_TMP_BUFFER);
  1374. if (derBuf == NULL) {
  1375. WOLFSSL_MSG("malloc failed when converting RsaKey to DER");
  1376. status = kStatus_SSS_Fail;
  1377. ret = MEMORY_E;
  1378. }
  1379. }
  1380. if (status == kStatus_SSS_Success) {
  1381. derSz = wc_RsaKeyToPublicDer(key, derBuf, derSz);
  1382. if (derSz < 0) {
  1383. status = kStatus_SSS_Fail;
  1384. ret = derSz;
  1385. }
  1386. }
  1387. if (status == kStatus_SSS_Success) {
  1388. keyId = se050_allocate_key(SE050_RSA_KEY);
  1389. status = sss_key_object_allocate_handle(&newKey, keyId,
  1390. kSSS_KeyPart_Public, kSSS_CipherType_RSA, keySz,
  1391. kKeyObject_Mode_Persistent);
  1392. }
  1393. if (status == kStatus_SSS_Success) {
  1394. /* Try to delete existing key first, ignore return since will
  1395. * fail if no key exists yet */
  1396. sss_key_store_erase_key(&host_keystore, &newKey);
  1397. keyCreated = 1;
  1398. status = sss_key_store_set_key(&host_keystore, &newKey, derBuf,
  1399. derSz, (keySz * 8), NULL, 0);
  1400. }
  1401. }
  1402. else {
  1403. status = sss_key_object_get_handle(&newKey, keyId);
  1404. }
  1405. if (derBuf != NULL) {
  1406. XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1407. }
  1408. }
  1409. if (status == kStatus_SSS_Success) {
  1410. status = sss_asymmetric_context_init(&ctx_asymm, cfg_se050_i2c_pi,
  1411. &newKey, algorithm, kMode_SSS_Encrypt);
  1412. if (status == kStatus_SSS_Success) {
  1413. encSz = outLen;
  1414. status = sss_asymmetric_encrypt(&ctx_asymm, (uint8_t*)in, inLen,
  1415. out, &encSz);
  1416. }
  1417. sss_asymmetric_context_free(&ctx_asymm);
  1418. }
  1419. if (status == kStatus_SSS_Success) {
  1420. key->keyId = keyId;
  1421. key->keyIdSet = 1;
  1422. ret = encSz;
  1423. }
  1424. else {
  1425. if (keyCreated) {
  1426. sss_key_store_erase_key(&host_keystore, &newKey);
  1427. sss_key_object_free(&newKey);
  1428. }
  1429. if (ret == 0) {
  1430. ret = WC_HW_E;
  1431. }
  1432. }
  1433. wolfSSL_CryptHwMutexUnLock();
  1434. #ifdef SE050_DEBUG
  1435. printf("se050_rsa_public_encrypt: ret %d, outLen %d\n", ret, outLen);
  1436. #endif
  1437. return ret;
  1438. }
  1439. /**
  1440. * RSA public decrypt operation.
  1441. *
  1442. * in input data to be decrypted
  1443. * inLen length of input data, bytes
  1444. * out output buffer containing decrypted data
  1445. * outLen length of output buffer, bytes
  1446. * key pointer to initialized/populated RsaKey structure
  1447. * rsa_type type of RSA: must be RSA_PRIVATE_DECRYPT
  1448. * pad_value should be RSA_BLOCK_TYPE_2 for encrypting
  1449. * pad_type type of padding: WC_RSA_PKCSV15_PAD, WC_RSA_OAEP_PAD,
  1450. * WC_RSA_NO_PAD, WC_RSA_PSS_PAD
  1451. * hash type of hash algorithm, found in wolfssl/wolfcrypt/hash.h
  1452. * mgf type of mask generation function to use
  1453. * label optional label, not supported by SE050, must be NULL
  1454. * labelSz size of label, not supported by SE050, must be 0
  1455. *
  1456. * Returns size of decrypted data on success, negative on error.
  1457. */
  1458. int se050_rsa_private_decrypt(const byte* in, word32 inLen, byte* out,
  1459. word32 outLen, struct RsaKey* key, int rsa_type,
  1460. byte pad_value, int pad_type,
  1461. enum wc_HashType hash, int mgf, byte* label,
  1462. word32 labelSz)
  1463. {
  1464. int ret = 0;
  1465. int keyCreated = 0;
  1466. word32 keyId;
  1467. int keySz;
  1468. size_t decSz;
  1469. sss_object_t newKey;
  1470. sss_status_t status;
  1471. sss_key_store_t host_keystore;
  1472. sss_algorithm_t algorithm = kAlgorithm_None;
  1473. sss_asymmetric_t ctx_asymm;
  1474. byte* derBuf = NULL;
  1475. int derSz = 0;
  1476. /* SE050 does not support optional label */
  1477. (void)label;
  1478. (void)labelSz;
  1479. (void)mgf;
  1480. #ifdef SE050_DEBUG
  1481. printf("se050_rsa_public_decrypt: key %p, in %p (%d), out %p (%d), "
  1482. "key %p\n", key, in, inLen, out, outLen, key);
  1483. #endif
  1484. if (in == NULL || out == NULL || key == NULL ||
  1485. rsa_type != RSA_PRIVATE_DECRYPT || pad_value != RSA_BLOCK_TYPE_2) {
  1486. return BAD_FUNC_ARG;
  1487. }
  1488. if (cfg_se050_i2c_pi == NULL) {
  1489. return WC_HW_E;
  1490. }
  1491. if (wolfSSL_CryptHwMutexLock() != 0) {
  1492. return BAD_MUTEX_E;
  1493. }
  1494. algorithm = se050_get_rsa_encrypt_type(pad_type, hash);
  1495. if (algorithm == kAlgorithm_None) {
  1496. WOLFSSL_MSG("Unsupported padding/hash/mgf combination for SE050");
  1497. return BAD_FUNC_ARG;
  1498. }
  1499. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  1500. if (status == kStatus_SSS_Success) {
  1501. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_RSA);
  1502. }
  1503. if (status == kStatus_SSS_Success) {
  1504. status = sss_key_object_init(&newKey, &host_keystore);
  1505. }
  1506. if (status == kStatus_SSS_Success) {
  1507. keySz = wc_RsaEncryptSize(key);
  1508. if (keySz < 0) {
  1509. WOLFSSL_MSG("Failed to get RSA key size from struct");
  1510. status = kStatus_SSS_Fail;
  1511. }
  1512. }
  1513. if (status == kStatus_SSS_Success) {
  1514. keyId = key->keyId;
  1515. if (key->keyIdSet == 0) {
  1516. /* key was not generated in SE050, export RsaKey to DER
  1517. * and use that to store into SE050 keystore */
  1518. derSz = wc_RsaKeyToDer(key, NULL, 0);
  1519. if (derSz < 0) {
  1520. status = kStatus_SSS_Fail;
  1521. ret = derSz;
  1522. }
  1523. else {
  1524. derBuf = (byte*)XMALLOC(derSz, key->heap,
  1525. DYNAMIC_TYPE_TMP_BUFFER);
  1526. if (derBuf == NULL) {
  1527. WOLFSSL_MSG("malloc failed when converting RsaKey to DER");
  1528. status = kStatus_SSS_Fail;
  1529. ret = MEMORY_E;
  1530. }
  1531. }
  1532. if (status == kStatus_SSS_Success) {
  1533. derSz = wc_RsaKeyToDer(key, derBuf, derSz);
  1534. if (derSz < 0) {
  1535. status = kStatus_SSS_Fail;
  1536. ret = derSz;
  1537. }
  1538. }
  1539. if (status == kStatus_SSS_Success) {
  1540. keyId = se050_allocate_key(SE050_RSA_KEY);
  1541. status = sss_key_object_allocate_handle(&newKey, keyId,
  1542. kSSS_KeyPart_Pair, kSSS_CipherType_RSA, keySz,
  1543. kKeyObject_Mode_Persistent);
  1544. }
  1545. if (status == kStatus_SSS_Success) {
  1546. /* Try to delete existing key first, ignore return since will
  1547. * fail if no key exists yet */
  1548. sss_key_store_erase_key(&host_keystore, &newKey);
  1549. keyCreated = 1;
  1550. status = sss_key_store_set_key(&host_keystore, &newKey, derBuf,
  1551. derSz, (keySz * 8), NULL, 0);
  1552. }
  1553. }
  1554. else {
  1555. status = sss_key_object_get_handle(&newKey, keyId);
  1556. }
  1557. if (derBuf != NULL) {
  1558. XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1559. }
  1560. }
  1561. if (status == kStatus_SSS_Success) {
  1562. status = sss_asymmetric_context_init(&ctx_asymm, cfg_se050_i2c_pi,
  1563. &newKey, algorithm, kMode_SSS_Decrypt);
  1564. if (status == kStatus_SSS_Success) {
  1565. decSz = outLen;
  1566. status = sss_asymmetric_decrypt(&ctx_asymm, (uint8_t*)in, inLen,
  1567. out, &decSz);
  1568. }
  1569. sss_asymmetric_context_free(&ctx_asymm);
  1570. }
  1571. if (status == kStatus_SSS_Success) {
  1572. key->keyId = keyId;
  1573. key->keyIdSet = 1;
  1574. ret = decSz;
  1575. }
  1576. else {
  1577. if (keyCreated) {
  1578. sss_key_store_erase_key(&host_keystore, &newKey);
  1579. sss_key_object_free(&newKey);
  1580. }
  1581. if (ret == 0) {
  1582. ret = WC_HW_E;
  1583. }
  1584. }
  1585. wolfSSL_CryptHwMutexUnLock();
  1586. #ifdef SE050_DEBUG
  1587. printf("se050_rsa_public_decrypt: ret %d, outLen %d\n", ret, outLen);
  1588. #endif
  1589. return ret;
  1590. }
  1591. #endif /* NO_RSA */
  1592. #ifdef HAVE_ECC
  1593. static int se050_map_curve(int curve_id, int keySize,
  1594. int* keySizeBits, sss_cipher_type_t* pcurve_type)
  1595. {
  1596. int ret = 0;
  1597. sss_cipher_type_t curve_type = kSSS_CipherType_NONE;
  1598. *keySizeBits = keySize * 8; /* set default */
  1599. switch (curve_id) {
  1600. case ECC_SECP160K1:
  1601. case ECC_SECP192K1:
  1602. case ECC_SECP224K1:
  1603. case ECC_SECP256K1:
  1604. #ifdef HAVE_ECC_KOBLITZ
  1605. curve_type = kSSS_CipherType_EC_NIST_K;
  1606. #else
  1607. ret = ECC_CURVE_OID_E;
  1608. #endif
  1609. break;
  1610. case ECC_BRAINPOOLP160R1:
  1611. case ECC_BRAINPOOLP192R1:
  1612. case ECC_BRAINPOOLP224R1:
  1613. case ECC_BRAINPOOLP256R1:
  1614. case ECC_BRAINPOOLP320R1:
  1615. case ECC_BRAINPOOLP384R1:
  1616. case ECC_BRAINPOOLP512R1:
  1617. #ifdef HAVE_ECC_BRAINPOOL
  1618. curve_type = kSSS_CipherType_EC_BRAINPOOL;
  1619. #else
  1620. ret = ECC_CURVE_OID_E;
  1621. #endif
  1622. break;
  1623. case ECC_CURVE_DEF:
  1624. case ECC_SECP160R1:
  1625. case ECC_SECP192R1:
  1626. case ECC_SECP224R1:
  1627. case ECC_SECP256R1:
  1628. case ECC_SECP384R1:
  1629. curve_type = kSSS_CipherType_EC_NIST_P;
  1630. break;
  1631. case ECC_SECP521R1:
  1632. curve_type = kSSS_CipherType_EC_NIST_P;
  1633. *keySizeBits = 521;
  1634. break;
  1635. case ECC_PRIME239V1:
  1636. case ECC_PRIME192V2:
  1637. case ECC_PRIME192V3:
  1638. default:
  1639. ret = ECC_CURVE_OID_E;
  1640. break;
  1641. }
  1642. if (pcurve_type)
  1643. *pcurve_type = curve_type;
  1644. return ret;
  1645. }
  1646. static sss_algorithm_t se050_map_hash_alg(int hashLen)
  1647. {
  1648. sss_algorithm_t algorithm = kAlgorithm_None;
  1649. if (hashLen == 20) {
  1650. algorithm = kAlgorithm_SSS_SHA1;
  1651. } else if (hashLen == 28) {
  1652. algorithm = kAlgorithm_SSS_SHA224;
  1653. } else if (hashLen == 32) {
  1654. algorithm = kAlgorithm_SSS_SHA256;
  1655. } else if (hashLen == 48) {
  1656. algorithm = kAlgorithm_SSS_SHA384;
  1657. } else if (hashLen == 64 || hashLen == 66) {
  1658. /* ECC P-521 can pass key size 66, use SHA-512 */
  1659. algorithm = kAlgorithm_SSS_SHA512;
  1660. }
  1661. return algorithm;
  1662. }
  1663. static int se050_ecc_insert_key(word32 keyId, const byte* eccDer,
  1664. word32 eccDerSize, int keyType)
  1665. {
  1666. int ret = 0;
  1667. struct ecc_key key;
  1668. sss_object_t newKey;
  1669. sss_key_store_t host_keystore;
  1670. sss_status_t status = kStatus_SSS_Success;
  1671. int keySizeBits = 0;
  1672. int keySize = 0;
  1673. word32 idx = 0;
  1674. sss_cipher_type_t curveType = kSSS_CipherType_NONE;
  1675. sss_key_part_t keyPart = kSSS_KeyPart_Pair;
  1676. if (wolfSSL_CryptHwMutexLock() != 0) {
  1677. return BAD_MUTEX_E;
  1678. }
  1679. /* Avoid key ID conflicts with temporary key storage */
  1680. if (keyId >= SE050_KEYID_START) {
  1681. return BAD_FUNC_ARG;
  1682. }
  1683. ret = wc_ecc_init(&key);
  1684. if (ret != 0) {
  1685. status = kStatus_SSS_Fail;
  1686. } else {
  1687. if (keyType == ECC_PUBLICKEY) {
  1688. keyPart = kSSS_KeyPart_Public;
  1689. ret = wc_EccPublicKeyDecode(eccDer, &idx, &key, eccDerSize);
  1690. }
  1691. else if (keyType == ECC_PRIVATEKEY) {
  1692. keyPart = kSSS_KeyPart_Pair;
  1693. ret = wc_EccPrivateKeyDecode(eccDer, &idx, &key, eccDerSize);
  1694. }
  1695. else {
  1696. ret = BAD_FUNC_ARG;
  1697. }
  1698. if (ret != 0) {
  1699. status = kStatus_SSS_Fail;
  1700. }
  1701. }
  1702. if (status == kStatus_SSS_Success) {
  1703. keySize = key.dp->size;
  1704. ret = se050_map_curve(key.dp->id, keySize, &keySizeBits, &curveType);
  1705. if (ret != 0) {
  1706. status = kStatus_SSS_Fail;
  1707. }
  1708. }
  1709. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  1710. if (status == kStatus_SSS_Success) {
  1711. status = sss_key_object_init(&newKey, &host_keystore);
  1712. }
  1713. if (status == kStatus_SSS_Success) {
  1714. status = sss_key_object_allocate_handle(&newKey, keyId,
  1715. keyPart, curveType, MAX_ECC_BYTES,
  1716. kKeyObject_Mode_Persistent);
  1717. }
  1718. if (status == kStatus_SSS_Success) {
  1719. status = sss_key_store_set_key(&host_keystore, &newKey, eccDer,
  1720. eccDerSize, keySizeBits,
  1721. NULL, 0);
  1722. }
  1723. wolfSSL_CryptHwMutexUnLock();
  1724. wc_ecc_free(&key);
  1725. if (status != kStatus_SSS_Success) {
  1726. if (ret == 0)
  1727. ret = WC_HW_E;
  1728. }
  1729. return ret;
  1730. }
  1731. /**
  1732. * Insert ECC public key into SE050 at specified key ID.
  1733. *
  1734. * keyId SE050 key ID to place public key into
  1735. * eccDer DER encoded ECC public key
  1736. * eccDerSize Size of eccDer, bytes
  1737. *
  1738. * Return 0 on success, negative on error.
  1739. */
  1740. int wc_se050_ecc_insert_public_key(word32 keyId, const byte* eccDer,
  1741. word32 eccDerSize)
  1742. {
  1743. return se050_ecc_insert_key(keyId, eccDer, eccDerSize, ECC_PUBLICKEY);
  1744. }
  1745. /**
  1746. * Insert ECC private key into SE050 at specified key ID.
  1747. *
  1748. * keyId SE050 key ID to place private key into
  1749. * eccDer DER encoded ECC private key
  1750. * eccDerSize Size of eccDer, bytes
  1751. *
  1752. * Return 0 on success, negative on error.
  1753. */
  1754. int wc_se050_ecc_insert_private_key(word32 keyId, const byte* eccDer,
  1755. word32 eccDerSize)
  1756. {
  1757. return se050_ecc_insert_key(keyId, eccDer, eccDerSize, ECC_PRIVATEKEY);
  1758. }
  1759. int se050_ecc_sign_hash_ex(const byte* in, word32 inLen, MATH_INT_T* r, MATH_INT_T* s,
  1760. byte* out, word32 *outLen, struct ecc_key* key)
  1761. {
  1762. int ret = 0;
  1763. sss_status_t status;
  1764. sss_asymmetric_t ctx_asymm;
  1765. sss_key_store_t host_keystore;
  1766. sss_object_t newKey;
  1767. sss_algorithm_t algorithm;
  1768. int keySize;
  1769. int keySizeBits;
  1770. int keyCreated = 0;
  1771. word32 keyId;
  1772. sss_cipher_type_t curveType;
  1773. byte sigBuf[ECC_MAX_SIG_SIZE];
  1774. size_t sigSz = sizeof(sigBuf);
  1775. word32 rLen = 0;
  1776. word32 sLen = 0;
  1777. #ifdef SE050_DEBUG
  1778. printf("se050_ecc_sign_hash_ex: key %p, in %p (%d), out %p (%d), "
  1779. "keyId %d\n", key, in, inLen, out, *outLen, key->keyId);
  1780. #endif
  1781. if (in == NULL || r == NULL || s == NULL || out == NULL ||
  1782. outLen == NULL || key == NULL) {
  1783. return BAD_FUNC_ARG;
  1784. }
  1785. if (cfg_se050_i2c_pi == NULL) {
  1786. return WC_HW_E;
  1787. }
  1788. keySize = key->dp->size;
  1789. ret = se050_map_curve(key->dp->id, keySize, &keySizeBits, &curveType);
  1790. if (ret != 0) {
  1791. return ret;
  1792. }
  1793. /* truncate if digest is larger than key size */
  1794. if (inLen > (word32)keySize) {
  1795. inLen = (word32)keySize;
  1796. }
  1797. /* For P-521, if inLen is 66, truncate down to 64 for SHA-512 */
  1798. if ((keySize == 66) && (inLen == 66)) {
  1799. inLen = 64;
  1800. }
  1801. algorithm = se050_map_hash_alg(inLen);
  1802. if (algorithm == kAlgorithm_None) {
  1803. inLen = keySize; /* try key size */
  1804. algorithm = se050_map_hash_alg(inLen);
  1805. }
  1806. if (algorithm == kAlgorithm_None) {
  1807. return ECC_CURVE_OID_E;
  1808. }
  1809. if (wolfSSL_CryptHwMutexLock() != 0) {
  1810. return BAD_MUTEX_E;
  1811. }
  1812. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  1813. if (status == kStatus_SSS_Success) {
  1814. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ECC);
  1815. }
  1816. if (status == kStatus_SSS_Success) {
  1817. status = sss_key_object_init(&newKey, &host_keystore);
  1818. }
  1819. /* this is run when a key was not generated and was instead passed in */
  1820. if (status == kStatus_SSS_Success) {
  1821. keyId = key->keyId;
  1822. if (key->keyIdSet == 0) {
  1823. byte derBuf[SE050_ECC_DER_MAX];
  1824. word32 derSz;
  1825. ret = wc_EccKeyToDer(key, derBuf, (word32)sizeof(derBuf));
  1826. if (ret >= 0) {
  1827. derSz = ret;
  1828. ret = 0;
  1829. }
  1830. else {
  1831. status = kStatus_SSS_Fail;
  1832. }
  1833. if (status == kStatus_SSS_Success) {
  1834. keyId = se050_allocate_key(SE050_ECC_KEY);
  1835. status = sss_key_object_allocate_handle(&newKey, keyId,
  1836. kSSS_KeyPart_Pair, curveType, keySize,
  1837. kKeyObject_Mode_Persistent);
  1838. }
  1839. if (status == kStatus_SSS_Success) {
  1840. /* Try to delete existing key first, ignore return since will
  1841. * fail if no key exists yet */
  1842. sss_key_store_erase_key(&host_keystore, &newKey);
  1843. keyCreated = 1;
  1844. status = sss_key_store_set_key(&host_keystore, &newKey, derBuf,
  1845. derSz, keySizeBits, NULL, 0);
  1846. }
  1847. }
  1848. else {
  1849. status = sss_key_object_get_handle(&newKey, keyId);
  1850. }
  1851. }
  1852. if (status == kStatus_SSS_Success) {
  1853. status = sss_asymmetric_context_init(&ctx_asymm, cfg_se050_i2c_pi,
  1854. &newKey, algorithm, kMode_SSS_Sign);
  1855. if (status == kStatus_SSS_Success) {
  1856. status = sss_asymmetric_sign_digest(&ctx_asymm, (uint8_t*)in, inLen,
  1857. sigBuf, &sigSz);
  1858. if (status == kStatus_SSS_Success) {
  1859. /* SE050 returns ASN.1 encoded signature */
  1860. rLen = keySize;
  1861. sLen = keySize;
  1862. ret = DecodeECC_DSA_Sig_Bin(sigBuf, (word32)sigSz,
  1863. out, &rLen,
  1864. out+keySize, &sLen);
  1865. if (ret != 0) {
  1866. status = kStatus_SSS_Fail;
  1867. } else {
  1868. /* In case rLen is smaller than keySize, move S up */
  1869. XMEMCPY(out + rLen, out + keySize, sLen);
  1870. }
  1871. }
  1872. }
  1873. sss_asymmetric_context_free(&ctx_asymm);
  1874. }
  1875. if (status == kStatus_SSS_Success) {
  1876. /* Load R and S into mp_int */
  1877. ret = mp_read_unsigned_bin(r, out, rLen);
  1878. if (ret == MP_OKAY) {
  1879. ret = mp_read_unsigned_bin(s, out + rLen, sLen);
  1880. }
  1881. if (ret != MP_OKAY) {
  1882. status = kStatus_SSS_Fail;
  1883. }
  1884. ret = 0;
  1885. }
  1886. if (status == kStatus_SSS_Success) {
  1887. key->keyId = keyId;
  1888. key->keyIdSet = 1;
  1889. ret = 0;
  1890. }
  1891. else {
  1892. if (keyCreated) {
  1893. sss_key_store_erase_key(&host_keystore, &newKey);
  1894. sss_key_object_free(&newKey);
  1895. }
  1896. if (ret == 0)
  1897. ret = WC_HW_E;
  1898. }
  1899. wolfSSL_CryptHwMutexUnLock();
  1900. #ifdef SE050_DEBUG
  1901. printf("se050_ecc_sign_hash_ex: ret %d, outLen %d\n", ret, *outLen);
  1902. #endif
  1903. (void)outLen; /* caller sets outLen */
  1904. return ret;
  1905. }
  1906. int se050_ecc_verify_hash_ex(const byte* hash, word32 hashLen, MATH_INT_T* r,
  1907. MATH_INT_T* s, struct ecc_key* key, int* res)
  1908. {
  1909. int ret = 0;
  1910. sss_status_t status;
  1911. sss_asymmetric_t ctx_asymm;
  1912. sss_object_t newKey;
  1913. sss_key_store_t host_keystore;
  1914. sss_algorithm_t algorithm;
  1915. int keyId;
  1916. int keySize;
  1917. int keySizeBits;
  1918. sss_cipher_type_t curveType;
  1919. int keyCreated = 0;
  1920. byte rBuf[ECC_MAX_CRYPTO_HW_SIZE];
  1921. byte sBuf[ECC_MAX_CRYPTO_HW_SIZE];
  1922. byte sigBuf[ECC_MAX_SIG_SIZE];
  1923. word32 rBufSz = (word32)sizeof(rBuf);
  1924. word32 sBufSz = (word32)sizeof(sBuf);
  1925. word32 sigSz = (word32)sizeof(sigBuf);
  1926. #ifdef SE050_DEBUG
  1927. printf("se050_ecc_verify_hash_ex: key %p, hash %p (%d)\n",
  1928. key, hash, hashLen);
  1929. #endif
  1930. *res = 0;
  1931. if (cfg_se050_i2c_pi == NULL) {
  1932. return WC_HW_E;
  1933. }
  1934. keySize = key->dp->size;
  1935. ret = se050_map_curve(key->dp->id, keySize, &keySizeBits, &curveType);
  1936. if (ret != 0) {
  1937. return ret;
  1938. }
  1939. /* truncate hash if larger than key size */
  1940. if (hashLen > (word32)keySize) {
  1941. hashLen = (word32)keySize;
  1942. }
  1943. /* For P-521, if inLen is 66, truncate down to 64 for SHA-512 */
  1944. if ((keySize == 66) && (hashLen == 66)) {
  1945. hashLen = 64;
  1946. }
  1947. algorithm = se050_map_hash_alg(hashLen);
  1948. if (algorithm == kAlgorithm_None) {
  1949. hashLen = keySize; /* try key size */
  1950. algorithm = se050_map_hash_alg(hashLen);
  1951. }
  1952. if (algorithm == kAlgorithm_None) {
  1953. return ECC_CURVE_OID_E;
  1954. }
  1955. if (wolfSSL_CryptHwMutexLock() != 0) {
  1956. return BAD_MUTEX_E;
  1957. }
  1958. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  1959. if (status == kStatus_SSS_Success) {
  1960. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ECC);
  1961. }
  1962. if (status == kStatus_SSS_Success) {
  1963. status = sss_key_object_init(&newKey, &host_keystore);
  1964. }
  1965. /* this is run when a key was not generated and was instead passed in */
  1966. if (status == kStatus_SSS_Success) {
  1967. keyId = key->keyId;
  1968. if (key->keyIdSet == 0) {
  1969. byte derBuf[SE050_ECC_DER_MAX];
  1970. word32 derSz;
  1971. ret = wc_EccPublicKeyToDer(key, derBuf, (word32)sizeof(derBuf), 1);
  1972. if (ret >= 0) {
  1973. derSz = ret;
  1974. ret = 0;
  1975. }
  1976. else {
  1977. status = kStatus_SSS_Fail;
  1978. }
  1979. if (status == kStatus_SSS_Success) {
  1980. keyId = se050_allocate_key(SE050_ECC_KEY);
  1981. status = sss_key_object_allocate_handle(&newKey, keyId,
  1982. kSSS_KeyPart_Public, curveType, keySize,
  1983. kKeyObject_Mode_Persistent);
  1984. }
  1985. if (status == kStatus_SSS_Success) {
  1986. /* Try to delete existing key first, ignore return since will
  1987. * fail if no key exists yet */
  1988. sss_key_store_erase_key(&host_keystore, &newKey);
  1989. keyCreated = 1;
  1990. status = sss_key_store_set_key(&host_keystore, &newKey, derBuf,
  1991. derSz, keySizeBits, NULL, 0);
  1992. }
  1993. }
  1994. else {
  1995. status = sss_key_object_get_handle(&newKey, keyId);
  1996. }
  1997. }
  1998. if (status == kStatus_SSS_Success) {
  1999. status = sss_asymmetric_context_init(&ctx_asymm, cfg_se050_i2c_pi,
  2000. &newKey, algorithm, kMode_SSS_Verify);
  2001. if (status == kStatus_SSS_Success) {
  2002. /* SE050 expects ASN.1 encoded signature */
  2003. XMEMSET(rBuf, 0, sizeof(rBuf));
  2004. XMEMSET(sBuf, 0, sizeof(sBuf));
  2005. rBufSz = mp_unsigned_bin_size(r);
  2006. sBufSz = mp_unsigned_bin_size(s);
  2007. if (rBufSz > sizeof(rBuf) || sBufSz > sizeof(sBuf)) {
  2008. WOLFSSL_MSG("Internal R/S buffers too small for signature");
  2009. ret = BUFFER_E;
  2010. }
  2011. if (ret == 0) {
  2012. ret = mp_to_unsigned_bin(r, rBuf);
  2013. if (ret == MP_OKAY) {
  2014. ret = mp_to_unsigned_bin(s, sBuf);
  2015. }
  2016. }
  2017. if (ret == 0) {
  2018. ret = StoreECC_DSA_Sig_Bin(sigBuf, &sigSz, rBuf, rBufSz,
  2019. sBuf, sBufSz);
  2020. }
  2021. if (ret == 0) {
  2022. status = sss_asymmetric_verify_digest(&ctx_asymm,
  2023. (uint8_t*)hash, hashLen, sigBuf, sigSz);
  2024. }
  2025. else {
  2026. status = kStatus_SSS_Fail;
  2027. }
  2028. }
  2029. sss_asymmetric_context_free(&ctx_asymm);
  2030. }
  2031. if (status == kStatus_SSS_Success) {
  2032. key->keyId = keyId;
  2033. key->keyIdSet = 1;
  2034. *res = 1;
  2035. ret = 0;
  2036. }
  2037. else {
  2038. if (keyCreated) {
  2039. sss_key_store_erase_key(&host_keystore, &newKey);
  2040. sss_key_object_free(&newKey);
  2041. }
  2042. if (ret == 0)
  2043. ret = WC_HW_E;
  2044. }
  2045. wolfSSL_CryptHwMutexUnLock();
  2046. #ifdef SE050_DEBUG
  2047. printf("se050_ecc_verify_hash_ex: key %p, ret %d, res %d\n",
  2048. key, ret, *res);
  2049. #endif
  2050. return ret;
  2051. }
  2052. void se050_ecc_free_key(struct ecc_key* key)
  2053. {
  2054. sss_status_t status = kStatus_SSS_Success;
  2055. sss_object_t keyObject;
  2056. sss_key_store_t host_keystore;
  2057. #ifdef SE050_DEBUG
  2058. printf("se050_ecc_free_key: key %p, keyId %d\n", key, key->keyId);
  2059. #endif
  2060. if (cfg_se050_i2c_pi == NULL) {
  2061. return;
  2062. }
  2063. if (key->keyIdSet == 0) {
  2064. return;
  2065. }
  2066. if (wolfSSL_CryptHwMutexLock() != 0) {
  2067. return;
  2068. }
  2069. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2070. if (status == kStatus_SSS_Success) {
  2071. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ECC);
  2072. }
  2073. if (status == kStatus_SSS_Success) {
  2074. status = sss_key_object_init(&keyObject, &host_keystore);
  2075. }
  2076. if (status == kStatus_SSS_Success) {
  2077. status = sss_key_object_get_handle(&keyObject, key->keyId);
  2078. }
  2079. if (status == kStatus_SSS_Success) {
  2080. sss_key_object_free(&keyObject);
  2081. key->keyId = 0;
  2082. key->keyIdSet = 0;
  2083. }
  2084. wolfSSL_CryptHwMutexUnLock();
  2085. }
  2086. /**
  2087. * Use specified SE050 key ID with this ecc_key struct.
  2088. * Should be called by wc_ecc_use_key_id() for using pre-populated
  2089. * SE050 keys.
  2090. *
  2091. * key Pointer to initialized ecc_key structure
  2092. * keyId SE050 key ID containing ECC key object
  2093. *
  2094. * Return 0 on success, negative on error.
  2095. */
  2096. int se050_ecc_use_key_id(struct ecc_key* key, word32 keyId)
  2097. {
  2098. int ret = 0;
  2099. sss_object_t keyObject;
  2100. sss_key_store_t host_keystore;
  2101. sss_status_t status = kStatus_SSS_Success;
  2102. uint8_t derBuf[SE050_ECC_DER_MAX];
  2103. size_t derSz = sizeof(derBuf);
  2104. size_t derSzBits = 0;
  2105. word32 idx = 0;
  2106. #ifdef SE050_DEBUG
  2107. printf("se050_ecc_use_key_id: key %p, keyId %d\n", key, keyId);
  2108. #endif
  2109. if (key == NULL) {
  2110. return BAD_FUNC_ARG;
  2111. }
  2112. if (cfg_se050_i2c_pi == NULL) {
  2113. return WC_HW_E;
  2114. }
  2115. if (wolfSSL_CryptHwMutexLock() != 0) {
  2116. return BAD_MUTEX_E;
  2117. }
  2118. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2119. if (status == kStatus_SSS_Success) {
  2120. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ECC);
  2121. }
  2122. if (status == kStatus_SSS_Success) {
  2123. status = sss_key_object_init(&keyObject, &host_keystore);
  2124. }
  2125. if (status == kStatus_SSS_Success) {
  2126. status = sss_key_object_get_handle(&keyObject, keyId);
  2127. }
  2128. if (status == kStatus_SSS_Success) {
  2129. derSzBits = derSz * 8;
  2130. status = sss_key_store_get_key(&host_keystore, &keyObject,
  2131. derBuf, &derSz, &derSzBits);
  2132. (void)derSzBits; /* not used */
  2133. }
  2134. if (status == kStatus_SSS_Success) {
  2135. ret = wc_EccPublicKeyDecode(derBuf, &idx, key, (word32)derSz);
  2136. if (ret != 0) {
  2137. status = kStatus_SSS_Fail;
  2138. }
  2139. }
  2140. if (status == kStatus_SSS_Success) {
  2141. key->keyId = keyId;
  2142. key->keyIdSet = 1;
  2143. ret = 0;
  2144. }
  2145. else if (ret == 0) {
  2146. ret = WC_HW_E;
  2147. }
  2148. sss_key_object_free(&keyObject);
  2149. wolfSSL_CryptHwMutexUnLock();
  2150. #ifdef SE050_DEBUG
  2151. printf("se050_ecc_use_key_id: ret %d\n", ret);
  2152. #endif
  2153. return ret;
  2154. }
  2155. /**
  2156. * Get SE050 key ID associated with this ecc_key struct.
  2157. * Should be called by wc_ecc_get_key_id() for the application to get
  2158. * what key ID wolfCrypt picked for this ecc_key struct when generating
  2159. * a key inside the SE050.
  2160. *
  2161. * key Pointer to initialized ecc_key structure
  2162. * keyId [OUT] SE050 key ID associated with this key structure
  2163. *
  2164. * Return 0 on success, negative on error.
  2165. */
  2166. int se050_ecc_get_key_id(struct ecc_key* key, word32* keyId)
  2167. {
  2168. int ret = 0;
  2169. if (key == NULL || keyId == NULL) {
  2170. return BAD_FUNC_ARG;
  2171. }
  2172. if (key->keyIdSet == 1) {
  2173. *keyId = key->keyId;
  2174. } else {
  2175. WOLFSSL_MSG("SE050 key ID not set for ecc_key struct");
  2176. ret = WC_HW_E;
  2177. }
  2178. return ret;
  2179. }
  2180. int se050_ecc_create_key(struct ecc_key* key, int curve_id, int keySize)
  2181. {
  2182. int ret = 0;
  2183. sss_status_t status = kStatus_SSS_Success;
  2184. sss_object_t keyPair;
  2185. sss_key_store_t host_keystore;
  2186. uint8_t derBuf[SE050_ECC_DER_MAX];
  2187. size_t derSz = sizeof(derBuf);
  2188. word32 keyId;
  2189. int keySizeBits;
  2190. sss_cipher_type_t curveType;
  2191. int keyCreated = 0;
  2192. #ifdef SE050_DEBUG
  2193. printf("se050_ecc_create_key: key %p, curve %d, keySize %d\n",
  2194. key, curve_id, keySize);
  2195. #endif
  2196. if (cfg_se050_i2c_pi == NULL) {
  2197. return WC_HW_E;
  2198. }
  2199. ret = se050_map_curve(curve_id, keySize, &keySizeBits, &curveType);
  2200. if (ret != 0) {
  2201. return ret;
  2202. }
  2203. if (wolfSSL_CryptHwMutexLock() != 0) {
  2204. return BAD_MUTEX_E;
  2205. }
  2206. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2207. if (status == kStatus_SSS_Success) {
  2208. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ECC);
  2209. }
  2210. if (status == kStatus_SSS_Success) {
  2211. status = sss_key_object_init(&keyPair, &host_keystore);
  2212. }
  2213. if (status == kStatus_SSS_Success) {
  2214. keyId = se050_allocate_key(SE050_ECC_KEY);
  2215. /* Using Transient key type here does not work with SE050 */
  2216. status = sss_key_object_allocate_handle(&keyPair, keyId,
  2217. kSSS_KeyPart_Pair, curveType, keySize,
  2218. kKeyObject_Mode_Persistent);
  2219. }
  2220. if (status == kStatus_SSS_Success) {
  2221. /* Try to delete existing key first. Ignore return since will fail
  2222. * if no key exists */
  2223. sss_key_store_erase_key(&host_keystore, &keyPair);
  2224. keyCreated = 1;
  2225. status = sss_key_store_generate_key(&host_keystore, &keyPair,
  2226. keySizeBits, NULL);
  2227. }
  2228. if (status == kStatus_SSS_Success) {
  2229. size_t derSzBits = derSz * 8;
  2230. status = sss_key_store_get_key(&host_keystore, &keyPair,
  2231. derBuf, &derSz, &derSzBits);
  2232. (void)derSzBits; /* not used */
  2233. }
  2234. if (status == kStatus_SSS_Success) {
  2235. word32 idx = 0;
  2236. ret = wc_EccPublicKeyDecode(derBuf, &idx, key, (word32)derSz);
  2237. if (ret != 0) {
  2238. status = kStatus_SSS_Fail;
  2239. }
  2240. }
  2241. if (status == kStatus_SSS_Success) {
  2242. key->keyId = keyId;
  2243. key->keyIdSet = 1;
  2244. ret = 0;
  2245. }
  2246. else {
  2247. if (keyCreated) {
  2248. sss_key_store_erase_key(&host_keystore, &keyPair);
  2249. sss_key_object_free(&keyPair);
  2250. }
  2251. if (ret == 0)
  2252. ret = WC_HW_E;
  2253. }
  2254. wolfSSL_CryptHwMutexUnLock();
  2255. #ifdef SE050_DEBUG
  2256. printf("se050_ecc_create_key: key %p, ret %d, keyId %d\n",
  2257. key, ret, key->keyId);
  2258. #endif
  2259. return ret;
  2260. }
  2261. int se050_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key,
  2262. byte* out, word32* outlen)
  2263. {
  2264. int ret;
  2265. sss_status_t status = kStatus_SSS_Success;
  2266. sss_key_store_t host_keystore;
  2267. sss_object_t ref_private_key;
  2268. sss_object_t ref_public_key;
  2269. sss_object_t deriveKey;
  2270. sss_derive_key_t ctx_derive_key;
  2271. word32 keyId;
  2272. int keySize;
  2273. int keySizeBits;
  2274. sss_cipher_type_t curveType;
  2275. int keyCreated = 0;
  2276. int deriveKeyCreated = 0;
  2277. #ifdef SE050_DEBUG
  2278. printf("se050_ecc_shared_secret: priv %p, pub %p, out %p (%d)\n",
  2279. private_key, public_key, out, *outlen);
  2280. #endif
  2281. if (cfg_se050_i2c_pi == NULL) {
  2282. return WC_HW_E;
  2283. }
  2284. if (private_key == NULL || public_key == NULL ||
  2285. private_key->keyIdSet == 0) {
  2286. return BAD_FUNC_ARG;
  2287. }
  2288. keySize = private_key->dp->size;
  2289. ret = se050_map_curve(private_key->dp->id, keySize, &keySizeBits,
  2290. &curveType);
  2291. if (ret != 0) {
  2292. return ret;
  2293. }
  2294. if (wolfSSL_CryptHwMutexLock() != 0) {
  2295. return BAD_MUTEX_E;
  2296. }
  2297. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2298. if (status == kStatus_SSS_Success) {
  2299. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ECC);
  2300. }
  2301. if (status == kStatus_SSS_Success) {
  2302. status = sss_key_object_init(&ref_private_key, &host_keystore);
  2303. }
  2304. if (status == kStatus_SSS_Success) {
  2305. status = sss_key_object_get_handle(&ref_private_key,
  2306. private_key->keyId);
  2307. }
  2308. if (status == kStatus_SSS_Success) {
  2309. status = sss_key_object_init(&ref_public_key, &host_keystore);
  2310. }
  2311. if (status == kStatus_SSS_Success) {
  2312. keyId = public_key->keyId;
  2313. if (public_key->keyIdSet == 0) {
  2314. byte derBuf[SE050_ECC_DER_MAX];
  2315. word32 derSz;
  2316. ret = wc_EccPublicKeyToDer(public_key, derBuf,
  2317. (word32)sizeof(derBuf), 1);
  2318. if (ret >= 0) {
  2319. derSz = ret;
  2320. ret = 0;
  2321. }
  2322. else {
  2323. status = kStatus_SSS_Fail;
  2324. }
  2325. if (status == kStatus_SSS_Success) {
  2326. keyId = se050_allocate_key(SE050_ECC_KEY);
  2327. status = sss_key_object_allocate_handle(&ref_public_key,
  2328. keyId, kSSS_KeyPart_Public, curveType, keySize,
  2329. kKeyObject_Mode_Persistent);
  2330. }
  2331. if (status == kStatus_SSS_Success) {
  2332. /* Try to delete existing key first, ignore return since will
  2333. * fail if no key exists yet */
  2334. sss_key_store_erase_key(&host_keystore, &ref_public_key);
  2335. status = sss_key_store_set_key(&host_keystore, &ref_public_key,
  2336. derBuf, derSz, keySizeBits, NULL, 0);
  2337. keyCreated = 1;
  2338. }
  2339. }
  2340. else {
  2341. status = sss_key_object_get_handle(&ref_public_key, keyId);
  2342. }
  2343. }
  2344. if (status == kStatus_SSS_Success) {
  2345. status = sss_key_object_init(&deriveKey, &host_keystore);
  2346. }
  2347. if (status == kStatus_SSS_Success) {
  2348. word32 keyIdAes = se050_allocate_key(SE050_AES_KEY);
  2349. status = sss_key_object_allocate_handle(&deriveKey,
  2350. keyIdAes,
  2351. kSSS_KeyPart_Default,
  2352. kSSS_CipherType_Binary,
  2353. keySize,
  2354. kKeyObject_Mode_Transient);
  2355. }
  2356. if (status == kStatus_SSS_Success) {
  2357. status = sss_derive_key_context_init(&ctx_derive_key, cfg_se050_i2c_pi,
  2358. &ref_private_key, kAlgorithm_SSS_ECDH,
  2359. kMode_SSS_ComputeSharedSecret);
  2360. if (status == kStatus_SSS_Success) {
  2361. /* Try to delete existing key first, ignore return since will
  2362. * fail if no key exists yet */
  2363. sss_key_store_erase_key(&host_keystore, &deriveKey);
  2364. status = sss_derive_key_dh(&ctx_derive_key, &ref_public_key,
  2365. &deriveKey);
  2366. }
  2367. if (status == kStatus_SSS_Success) {
  2368. size_t outlenSz = (size_t)*outlen;
  2369. size_t outlenSzBits = outlenSz * 8;
  2370. deriveKeyCreated = 1;
  2371. /* derived key export */
  2372. status = sss_key_store_get_key(&host_keystore, &deriveKey,
  2373. out, &outlenSz, &outlenSzBits);
  2374. *outlen = (word32)outlenSz;
  2375. (void)outlenSzBits; /* not used */
  2376. }
  2377. sss_derive_key_context_free(&ctx_derive_key);
  2378. }
  2379. if (deriveKeyCreated) {
  2380. sss_key_store_erase_key(&host_keystore, &deriveKey);
  2381. sss_key_object_free(&deriveKey);
  2382. }
  2383. if (status == kStatus_SSS_Success) {
  2384. public_key->keyId = keyId;
  2385. public_key->keyIdSet = 1;
  2386. ret = 0;
  2387. }
  2388. else {
  2389. if (keyCreated) {
  2390. sss_key_store_erase_key(&host_keystore, &ref_public_key);
  2391. sss_key_object_free(&ref_public_key);
  2392. }
  2393. if (ret == 0) {
  2394. ret = WC_HW_E;
  2395. }
  2396. }
  2397. wolfSSL_CryptHwMutexUnLock();
  2398. #ifdef SE050_DEBUG
  2399. printf("se050_ecc_shared_secret: ret %d, outlen %d\n", ret, *outlen);
  2400. #endif
  2401. return ret;
  2402. }
  2403. #endif /* HAVE_ECC */
  2404. #ifdef HAVE_ED25519
  2405. int se050_ed25519_create_key(ed25519_key* key)
  2406. {
  2407. int ret = 0;
  2408. sss_status_t status;
  2409. sss_key_store_t host_keystore;
  2410. sss_object_t newKey;
  2411. word32 keyId;
  2412. int keySize = ED25519_KEY_SIZE;
  2413. int keyCreated = 0;
  2414. #ifdef SE050_DEBUG
  2415. printf("se050_ed25519_create_key: %p\n", key);
  2416. #endif
  2417. if (cfg_se050_i2c_pi == NULL) {
  2418. return WC_HW_E;
  2419. }
  2420. if (wolfSSL_CryptHwMutexLock() != 0) {
  2421. return BAD_MUTEX_E;
  2422. }
  2423. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2424. if (status == kStatus_SSS_Success) {
  2425. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ED25519);
  2426. }
  2427. if (status == kStatus_SSS_Success) {
  2428. status = sss_key_object_init(&newKey, &host_keystore);
  2429. }
  2430. if (status == kStatus_SSS_Success) {
  2431. keyId = se050_allocate_key(SE050_ED25519_KEY);
  2432. status = sss_key_object_allocate_handle(&newKey, keyId,
  2433. kSSS_KeyPart_Pair, kSSS_CipherType_EC_TWISTED_ED, keySize,
  2434. kKeyObject_Mode_Transient);
  2435. }
  2436. if (status == kStatus_SSS_Success) {
  2437. keyCreated = 1;
  2438. status = sss_key_store_generate_key(&host_keystore, &newKey,
  2439. keySize * 8, NULL);
  2440. }
  2441. if (status == kStatus_SSS_Success) {
  2442. key->keyId = keyId;
  2443. key->keyIdSet = 1;
  2444. ret = 0;
  2445. }
  2446. else {
  2447. if (keyCreated) {
  2448. sss_key_store_erase_key(&host_keystore, &newKey);
  2449. sss_key_object_free(&newKey);
  2450. }
  2451. ret = WC_HW_E;
  2452. }
  2453. wolfSSL_CryptHwMutexUnLock();
  2454. #ifdef SE050_DEBUG
  2455. printf("se050_ed25519_create_key: ret %d, keyId %ld\n", ret, key->keyId);
  2456. #endif
  2457. return ret;
  2458. }
  2459. void se050_ed25519_free_key(ed25519_key* key)
  2460. {
  2461. sss_status_t status;
  2462. sss_object_t newKey;
  2463. sss_key_store_t host_keystore;
  2464. #ifdef SE050_DEBUG
  2465. printf("se050_ed25519_free_key: %p, id %ld\n", key, key->keyId);
  2466. #endif
  2467. if (cfg_se050_i2c_pi == NULL) {
  2468. return;
  2469. }
  2470. if (key->keyIdSet == 0) {
  2471. return;
  2472. }
  2473. if (wolfSSL_CryptHwMutexLock() != 0) {
  2474. return;
  2475. }
  2476. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2477. if (status == kStatus_SSS_Success) {
  2478. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ED25519);
  2479. }
  2480. if (status == kStatus_SSS_Success) {
  2481. status = sss_key_object_init(&newKey, &host_keystore);
  2482. }
  2483. if (status == kStatus_SSS_Success) {
  2484. status = sss_key_object_get_handle(&newKey, key->keyId);
  2485. }
  2486. if (status == kStatus_SSS_Success) {
  2487. sss_key_object_free(&newKey);
  2488. key->keyId = 0;
  2489. key->keyIdSet = 0;
  2490. }
  2491. wolfSSL_CryptHwMutexUnLock();
  2492. }
  2493. int se050_ed25519_sign_msg(const byte* in, word32 inLen, byte* out,
  2494. word32 *outLen, ed25519_key* key)
  2495. {
  2496. int ret = 0;
  2497. sss_status_t status = kStatus_SSS_Success;
  2498. sss_asymmetric_t ctx_asymm;
  2499. sss_key_store_t host_keystore;
  2500. sss_object_t newKey;
  2501. int keySize = ED25519_KEY_SIZE;
  2502. int keyCreated = 0;
  2503. word32 keyId;
  2504. #ifdef SE050_DEBUG
  2505. printf("se050_ed25519_sign_msg: key %p, in %p (%d), out %p (%d), "
  2506. "keyId %ld\n", key, in, inLen, out, *outLen, key->keyId);
  2507. #endif
  2508. if (cfg_se050_i2c_pi == NULL) {
  2509. return WC_HW_E;
  2510. }
  2511. if (wolfSSL_CryptHwMutexLock() != 0) {
  2512. return BAD_MUTEX_E;
  2513. }
  2514. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2515. if (status == kStatus_SSS_Success) {
  2516. status = sss_key_store_allocate(&host_keystore, SE050_KEYSTOREID_ED25519);
  2517. }
  2518. if (status == kStatus_SSS_Success) {
  2519. status = sss_key_object_init(&newKey, &host_keystore);
  2520. }
  2521. /* this is run when a key was not generated and was instead passed in */
  2522. if (status == kStatus_SSS_Success) {
  2523. keyId = key->keyId;
  2524. if (key->keyIdSet == 0) {
  2525. byte derBuf[SE050_ECC_DER_MAX];
  2526. word32 derSz;
  2527. ret = wc_Ed25519KeyToDer(key, derBuf, (word32)sizeof(derBuf));
  2528. if (ret >= 0) {
  2529. derSz = ret;
  2530. ret = 0;
  2531. }
  2532. else {
  2533. status = kStatus_SSS_Fail;
  2534. }
  2535. if (status == kStatus_SSS_Success) {
  2536. keyId = se050_allocate_key(SE050_ED25519_KEY);
  2537. status = sss_key_object_allocate_handle(&newKey, keyId,
  2538. kSSS_KeyPart_Pair, kSSS_CipherType_EC_TWISTED_ED, keySize,
  2539. kKeyObject_Mode_Transient);
  2540. }
  2541. if (status == kStatus_SSS_Success) {
  2542. keyCreated = 1;
  2543. status = sss_key_store_set_key(&host_keystore, &newKey, derBuf,
  2544. derSz, keySize * 8, NULL, 0);
  2545. }
  2546. }
  2547. else {
  2548. status = sss_key_object_get_handle(&newKey, keyId);
  2549. }
  2550. }
  2551. if (status == kStatus_SSS_Success) {
  2552. status = sss_asymmetric_context_init(&ctx_asymm, cfg_se050_i2c_pi,
  2553. &newKey, kAlgorithm_SSS_SHA512, kMode_SSS_Sign);
  2554. if (status == kStatus_SSS_Success) {
  2555. size_t outlenSz = (size_t)*outLen;
  2556. status = sss_se05x_asymmetric_sign(
  2557. (sss_se05x_asymmetric_t *)&ctx_asymm,
  2558. (uint8_t *)in, inLen, out, &outlenSz);
  2559. *outLen = (word32)outlenSz;
  2560. }
  2561. sss_asymmetric_context_free(&ctx_asymm);
  2562. }
  2563. if (status != kStatus_SSS_Success) {
  2564. if (keyCreated) {
  2565. sss_key_store_erase_key(&host_keystore, &newKey);
  2566. sss_key_object_free(&newKey);
  2567. }
  2568. ret = WC_HW_E;
  2569. } else {
  2570. key->keyId = keyId;
  2571. key->keyIdSet = 1;
  2572. }
  2573. wolfSSL_CryptHwMutexUnLock();
  2574. #ifdef SE050_DEBUG
  2575. printf("se050_ed25519_sign_msg: ret %d, outLen %d\n", ret, *outLen);
  2576. #endif
  2577. return ret;
  2578. }
  2579. int se050_ed25519_verify_msg(const byte* signature, word32 signatureLen,
  2580. const byte* msg, word32 msgLen, struct ed25519_key* key, int* res)
  2581. {
  2582. int ret = 0;
  2583. sss_status_t status = kStatus_SSS_Success;
  2584. sss_asymmetric_t ctx_asymm;
  2585. sss_object_t newKey;
  2586. sss_key_store_t host_keystore;
  2587. word32 keyId;
  2588. int keySize = ED25519_KEY_SIZE;
  2589. int keyCreated = 0;
  2590. #ifdef SE050_DEBUG
  2591. printf("se050_ed25519_verify_msg: key %p, sig %p (%d), msg %p (%d)\n",
  2592. key, signature, signatureLen, msg, msgLen);
  2593. #endif
  2594. if (cfg_se050_i2c_pi == NULL) {
  2595. return WC_HW_E;
  2596. }
  2597. if (wolfSSL_CryptHwMutexLock() != 0) {
  2598. return BAD_MUTEX_E;
  2599. }
  2600. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2601. if (status == kStatus_SSS_Success) {
  2602. status = sss_key_store_allocate(&host_keystore,
  2603. SE050_KEYSTOREID_ED25519);
  2604. }
  2605. if (status == kStatus_SSS_Success) {
  2606. status = sss_key_object_init(&newKey, &host_keystore);
  2607. }
  2608. if (status == kStatus_SSS_Success) {
  2609. keyId = key->keyId;
  2610. if (key->keyIdSet == 0) {
  2611. byte derBuf[ED25519_PUB_KEY_SIZE + 12]; /* seq + algo + bitstring */
  2612. word32 derSz = 0;
  2613. ret = wc_Ed25519PublicKeyToDer(key, derBuf,
  2614. (word32)sizeof(derBuf), 1);
  2615. if (ret >= 0) {
  2616. derSz = ret;
  2617. ret = 0;
  2618. }
  2619. else {
  2620. status = kStatus_SSS_Fail;
  2621. }
  2622. if (status == kStatus_SSS_Success) {
  2623. keyId = se050_allocate_key(SE050_ED25519_KEY);
  2624. status = sss_key_object_allocate_handle(&newKey, keyId,
  2625. kSSS_KeyPart_Public, kSSS_CipherType_EC_TWISTED_ED, keySize,
  2626. kKeyObject_Mode_Transient);
  2627. }
  2628. if (status == kStatus_SSS_Success) {
  2629. keyCreated = 1;
  2630. status = sss_key_store_set_key(&host_keystore, &newKey, derBuf,
  2631. derSz, keySize * 8, NULL, 0);
  2632. }
  2633. }
  2634. else {
  2635. status = sss_key_object_get_handle(&newKey, keyId);
  2636. }
  2637. }
  2638. if (status == kStatus_SSS_Success) {
  2639. status = sss_asymmetric_context_init(&ctx_asymm, cfg_se050_i2c_pi,
  2640. &newKey, kAlgorithm_SSS_SHA512, kMode_SSS_Verify);
  2641. if (status == kStatus_SSS_Success) {
  2642. status = sss_se05x_asymmetric_verify(
  2643. (sss_se05x_asymmetric_t*)&ctx_asymm, (uint8_t*)msg, msgLen,
  2644. (uint8_t*)signature, (size_t)signatureLen);
  2645. }
  2646. sss_asymmetric_context_free(&ctx_asymm);
  2647. }
  2648. if (status == kStatus_SSS_Success) {
  2649. key->keyId = keyId;
  2650. key->keyIdSet = 1;
  2651. *res = 1;
  2652. ret = 0;
  2653. }
  2654. else {
  2655. if (keyCreated) {
  2656. sss_key_store_erase_key(&host_keystore, &newKey);
  2657. sss_key_object_free(&newKey);
  2658. }
  2659. if (ret == 0)
  2660. ret = WC_HW_E;
  2661. }
  2662. wolfSSL_CryptHwMutexUnLock();
  2663. #ifdef SE050_DEBUG
  2664. printf("se050_ed25519_verify_msg: ret %d, res %d\n", ret, *res);
  2665. #endif
  2666. return ret;
  2667. }
  2668. #endif /* HAVE_ED25519 */
  2669. #ifdef HAVE_CURVE25519
  2670. int se050_curve25519_create_key(curve25519_key* key, int keySize)
  2671. {
  2672. int ret;
  2673. sss_status_t status = kStatus_SSS_Success;
  2674. sss_object_t keyPair;
  2675. sss_key_store_t host_keystore;
  2676. uint8_t derBuf[SE050_ECC_DER_MAX];
  2677. size_t derSz = sizeof(derBuf);
  2678. word32 keyId;
  2679. int keyCreated = 0;
  2680. #ifdef SE050_DEBUG
  2681. printf("se050_curve25519_create_key: key %p, keySize %d\n",
  2682. key, keySize);
  2683. #endif
  2684. if (cfg_se050_i2c_pi == NULL) {
  2685. return WC_HW_E;
  2686. }
  2687. if (wolfSSL_CryptHwMutexLock() != 0) {
  2688. return BAD_MUTEX_E;
  2689. }
  2690. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2691. if (status == kStatus_SSS_Success) {
  2692. status = sss_key_store_allocate(&host_keystore,
  2693. SE050_KEYSTOREID_CURVE25519);
  2694. }
  2695. if (status == kStatus_SSS_Success) {
  2696. status = sss_key_object_init(&keyPair, &host_keystore);
  2697. }
  2698. if (status == kStatus_SSS_Success) {
  2699. keyId = se050_allocate_key(SE050_CURVE25519_KEY);
  2700. status = sss_key_object_allocate_handle(&keyPair, keyId,
  2701. kSSS_KeyPart_Pair, kSSS_CipherType_EC_MONTGOMERY, keySize,
  2702. kKeyObject_Mode_None);
  2703. }
  2704. if (status == kStatus_SSS_Success) {
  2705. keyCreated = 1;
  2706. status = sss_key_store_generate_key(&host_keystore, &keyPair,
  2707. keySize * 8, NULL);
  2708. }
  2709. if (status == kStatus_SSS_Success) {
  2710. size_t derSzBits = derSz * 8;
  2711. status = sss_key_store_get_key(&host_keystore, &keyPair,
  2712. derBuf, &derSz, &derSzBits);
  2713. (void)derSzBits; /* not used */
  2714. }
  2715. if (status == kStatus_SSS_Success) {
  2716. word32 idx = 0;
  2717. byte pubKey[CURVE25519_KEYSIZE];
  2718. word32 pubKeyLen = (word32)sizeof(pubKey);
  2719. ret = DecodeAsymKeyPublic(derBuf, &idx, (word32)derSz,
  2720. pubKey, &pubKeyLen, X25519k);
  2721. if (ret == 0) {
  2722. ret = wc_curve25519_import_public_ex(pubKey, pubKeyLen, key,
  2723. EC25519_LITTLE_ENDIAN);
  2724. }
  2725. if (ret != 0) {
  2726. status = kStatus_SSS_Fail;
  2727. }
  2728. }
  2729. if (status == kStatus_SSS_Success) {
  2730. key->keyId = keyId;
  2731. key->keyIdSet = 1;
  2732. ret = 0;
  2733. }
  2734. else {
  2735. if (keyCreated) {
  2736. sss_key_store_erase_key(&host_keystore, &keyPair);
  2737. sss_key_object_free(&keyPair);
  2738. }
  2739. ret = WC_HW_E;
  2740. }
  2741. wolfSSL_CryptHwMutexUnLock();
  2742. #ifdef SE050_DEBUG
  2743. printf("se050_curve25519_create_key: key %p, ret %d, keyId %ld\n",
  2744. key, ret, key->keyId);
  2745. #endif
  2746. return ret;
  2747. }
  2748. int se050_curve25519_shared_secret(curve25519_key* private_key,
  2749. curve25519_key* public_key, ECPoint* out)
  2750. {
  2751. int ret = 0;
  2752. sss_status_t status = kStatus_SSS_Success;
  2753. sss_key_store_t host_keystore;
  2754. sss_object_t ref_private_key;
  2755. sss_object_t ref_public_key;
  2756. sss_object_t deriveKey;
  2757. sss_derive_key_t ctx_derive_key;
  2758. word32 keyId;
  2759. int keySize = CURVE25519_KEYSIZE;
  2760. int keyCreated = 0;
  2761. int deriveKeyCreated = 0;
  2762. #ifdef SE050_DEBUG
  2763. printf("se050_curve25519_shared_secret: priv %p, pub %p, out %p (%d)\n",
  2764. private_key, public_key, out, out->pointSz);
  2765. #endif
  2766. if (cfg_se050_i2c_pi == NULL) {
  2767. return WC_HW_E;
  2768. }
  2769. if (private_key == NULL || public_key == NULL ||
  2770. private_key->keyIdSet == 0) {
  2771. return BAD_FUNC_ARG;
  2772. }
  2773. if (wolfSSL_CryptHwMutexLock() != 0) {
  2774. return BAD_MUTEX_E;
  2775. }
  2776. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2777. if (status == kStatus_SSS_Success) {
  2778. status = sss_key_store_allocate(&host_keystore,
  2779. SE050_KEYSTOREID_CURVE25519);
  2780. }
  2781. if (status == kStatus_SSS_Success) {
  2782. status = sss_key_object_init(&ref_private_key, &host_keystore);
  2783. }
  2784. if (status == kStatus_SSS_Success) {
  2785. status = sss_key_object_get_handle(&ref_private_key,
  2786. private_key->keyId);
  2787. }
  2788. if (status == kStatus_SSS_Success) {
  2789. status = sss_key_object_init(&ref_public_key, &host_keystore);
  2790. }
  2791. if (status == kStatus_SSS_Success) {
  2792. keyId = public_key->keyId;
  2793. if (public_key->keyIdSet == 0) {
  2794. byte derBuf[CURVE25519_PUB_KEY_SIZE + 12]; /* seq + algo + bitstring */
  2795. word32 derSz;
  2796. byte pubKey[CURVE25519_PUB_KEY_SIZE];
  2797. word32 pubKeyLen = (word32)sizeof(pubKey);
  2798. ret = wc_curve25519_export_public_ex(public_key, pubKey, &pubKeyLen,
  2799. EC25519_LITTLE_ENDIAN);
  2800. if (ret == 0) {
  2801. ret = SetAsymKeyDerPublic(pubKey, pubKeyLen, derBuf,
  2802. (word32)sizeof(derBuf), X25519k, 1);
  2803. if (ret >= 0) {
  2804. derSz = ret;
  2805. ret = 0;
  2806. }
  2807. }
  2808. if (ret != 0) {
  2809. status = kStatus_SSS_Fail;
  2810. }
  2811. if (status == kStatus_SSS_Success) {
  2812. keyId = se050_allocate_key(SE050_CURVE25519_KEY);
  2813. status = sss_key_object_allocate_handle(&ref_public_key,
  2814. keyId, kSSS_KeyPart_Public, kSSS_CipherType_EC_MONTGOMERY,
  2815. keySize, kKeyObject_Mode_Transient);
  2816. }
  2817. if (status == kStatus_SSS_Success) {
  2818. keyCreated = 1;
  2819. status = sss_key_store_set_key(&host_keystore, &ref_public_key,
  2820. derBuf, derSz, keySize * 8, NULL, 0);
  2821. }
  2822. }
  2823. else {
  2824. status = sss_key_object_get_handle(&ref_public_key, keyId);
  2825. }
  2826. }
  2827. if (status == kStatus_SSS_Success) {
  2828. status = sss_key_object_init(&deriveKey, &host_keystore);
  2829. }
  2830. if (status == kStatus_SSS_Success) {
  2831. word32 keyIdAes = se050_allocate_key(SE050_AES_KEY);
  2832. deriveKeyCreated = 1;
  2833. status = sss_key_object_allocate_handle(&deriveKey,
  2834. keyIdAes,
  2835. kSSS_KeyPart_Default,
  2836. kSSS_CipherType_Binary,
  2837. keySize,
  2838. kKeyObject_Mode_Transient);
  2839. }
  2840. if (status == kStatus_SSS_Success) {
  2841. status = sss_derive_key_context_init(&ctx_derive_key, cfg_se050_i2c_pi,
  2842. &ref_private_key, kAlgorithm_SSS_ECDH,
  2843. kMode_SSS_ComputeSharedSecret);
  2844. if (status == kStatus_SSS_Success) {
  2845. status = sss_derive_key_dh(&ctx_derive_key, &ref_public_key,
  2846. &deriveKey);
  2847. }
  2848. if (status == kStatus_SSS_Success) {
  2849. size_t outlenSz = sizeof(out->point);
  2850. size_t outlenSzBits = outlenSz * 8;
  2851. /* derived key export */
  2852. status = sss_key_store_get_key(&host_keystore, &deriveKey,
  2853. out->point, &outlenSz, &outlenSzBits);
  2854. out->pointSz = (word32)outlenSz;
  2855. (void)outlenSzBits; /* not used */
  2856. }
  2857. sss_derive_key_context_free(&ctx_derive_key);
  2858. }
  2859. if (deriveKeyCreated) {
  2860. sss_key_store_erase_key(&host_keystore, &deriveKey);
  2861. sss_key_object_free(&deriveKey);
  2862. }
  2863. if (status == kStatus_SSS_Success) {
  2864. public_key->keyId = keyId;
  2865. public_key->keyIdSet = 1;
  2866. ret = 0;
  2867. }
  2868. else {
  2869. if (keyCreated) {
  2870. sss_key_store_erase_key(&host_keystore, &ref_public_key);
  2871. sss_key_object_free(&ref_public_key);
  2872. }
  2873. if (ret == 0)
  2874. ret = WC_HW_E;
  2875. }
  2876. wolfSSL_CryptHwMutexUnLock();
  2877. #ifdef SE050_DEBUG
  2878. printf("se050_curve25519_shared_secret: ret %d, outlen %d\n",
  2879. ret, out->pointSz);
  2880. #endif
  2881. return ret;
  2882. }
  2883. void se050_curve25519_free_key(struct curve25519_key* key)
  2884. {
  2885. sss_status_t status;
  2886. sss_object_t newKey;
  2887. sss_key_store_t host_keystore;
  2888. #ifdef SE050_DEBUG
  2889. printf("se050_curve25519_free_key: %p, id %ld\n", key, key->keyId);
  2890. #endif
  2891. if (cfg_se050_i2c_pi == NULL || key->keyIdSet == 0) {
  2892. return;
  2893. }
  2894. if (wolfSSL_CryptHwMutexLock() != 0) {
  2895. return;
  2896. }
  2897. status = sss_key_store_context_init(&host_keystore, cfg_se050_i2c_pi);
  2898. if (status == kStatus_SSS_Success) {
  2899. status = sss_key_store_allocate(&host_keystore,
  2900. SE050_KEYSTOREID_CURVE25519);
  2901. }
  2902. if (status == kStatus_SSS_Success) {
  2903. status = sss_key_object_init(&newKey, &host_keystore);
  2904. }
  2905. if (status == kStatus_SSS_Success) {
  2906. status = sss_key_object_get_handle(&newKey, key->keyId);
  2907. }
  2908. if (status == kStatus_SSS_Success) {
  2909. sss_key_object_free(&newKey);
  2910. key->keyId = 0;
  2911. key->keyIdSet = 0;
  2912. }
  2913. wolfSSL_CryptHwMutexUnLock();
  2914. }
  2915. #endif /* HAVE_CURVE25519 */
  2916. #endif /* WOLFSSL_SE050 */