renesas_tsip_util.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918
  1. /* renesas_tsip_util.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. #include <wolfssl/wolfcrypt/settings.h>
  22. #if defined(WOLFSSL_RENESAS_TSIP)
  23. #include <wolfssl/wolfcrypt/wc_port.h>
  24. #include <wolfssl/wolfcrypt/types.h>
  25. #include <wolfssl/wolfcrypt/asn.h>
  26. #include <wolfssl/wolfcrypt/memory.h>
  27. #include <wolfssl/wolfcrypt/error-crypt.h>
  28. #include <wolfssl/wolfcrypt/aes.h>
  29. #ifdef NO_INLINE
  30. #include <wolfssl/wolfcrypt/misc.h>
  31. #else
  32. #define WOLFSSL_MISC_INCLUDED
  33. #include <wolfcrypt/src/misc.c>
  34. #endif
  35. #include <wolfssl/ssl.h>
  36. #include <wolfssl/internal.h>
  37. #include <wolfssl/error-ssl.h>
  38. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  39. #include <wolfssl/wolfcrypt/port/Renesas/renesas_cmn.h>
  40. #include <stdio.h>
  41. #define TSIP_SIGNING_DATA_PREFIX_SZ 64
  42. #define TSIP_SIGNING_DATA_PREFIX_BYTE 0x20
  43. #define TSIP_MAX_SIG_DATA_SZ 130
  44. #define TSIP_CERT_VFY_LABEL_SZ 34
  45. /* function pointer typedefs for TSIP SHAxx HMAC Verification */
  46. typedef e_tsip_err_t (*shaHmacInitFn)
  47. (tsip_hmac_sha_handle_t*, tsip_hmac_sha_key_index_t*);
  48. typedef e_tsip_err_t (*shaHmacUpdateFn)
  49. (tsip_hmac_sha_handle_t*, uint8_t*, uint32_t);
  50. typedef e_tsip_err_t (*shaHmacFinalFn)
  51. (tsip_hmac_sha_handle_t*, uint8_t*, uint32_t);
  52. /* ./ca-cert.der.sign, */
  53. /* expect to have these variables defined at user application */
  54. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER>=109)
  55. extern uint32_t s_inst2[R_TSIP_SINST2_WORD_SIZE];
  56. #elif defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER>=106)
  57. extern uint32_t s_flash[];
  58. extern uint32_t s_inst1[R_TSIP_SINST_WORD_SIZE];
  59. #endif
  60. wolfSSL_Mutex tsip_mutex;
  61. static int tsip_CryptHwMutexInit_ = 0;
  62. static const byte* ca_cert_sig = NULL;
  63. static tsip_key_data g_user_key_info;
  64. struct WOLFSSL_HEAP_HINT* tsip_heap_hint = NULL;
  65. /* tsip only keep one encrypted ca public key */
  66. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  67. static uint32_t g_encrypted_publicCA_key[R_TSIP_SINST_WORD_SIZE];
  68. /* index of CM table. must be global since renesas_common access it. */
  69. extern uint32_t g_CAscm_Idx;
  70. #if defined(WOLFSSL_TLS13)
  71. /* The server certificate verification label. */
  72. static const byte serverCertVfyLabel[TSIP_CERT_VFY_LABEL_SZ] =
  73. "TLS 1.3, server CertificateVerify";
  74. static const byte clientCertVfyLabel[TSIP_CERT_VFY_LABEL_SZ] =
  75. "TLS 1.3, client CertificateVerify";
  76. #endif /* WOLFSSL_TLS13 */
  77. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  78. static int tsip_CryptHwMutexInit(wolfSSL_Mutex* mutex)
  79. {
  80. return wc_InitMutex(mutex);
  81. }
  82. static int tsip_CryptHwMutexLock(wolfSSL_Mutex* mutex)
  83. {
  84. return wc_LockMutex(mutex);
  85. }
  86. static int tsip_CryptHwMutexUnLock(wolfSSL_Mutex* mutex)
  87. {
  88. return wc_UnLockMutex(mutex);
  89. }
  90. #ifdef WOLF_PRIVATE_KEY_ID
  91. /* Set client encrypted private key data.
  92. * parameters:
  93. * ssl WOLFSSL object
  94. * keyBuf buffer holding wrapped key which Renesas key tool generated.
  95. * keyBufLen buffer length
  96. * keyType 0: RSA 2048bit, 1: RSA 4096bit, 2 ECC P256
  97. * return 0 on success, others on failure.
  98. */
  99. WOLFSSL_API int tsip_use_PrivateKey_buffer(struct WOLFSSL* ssl,
  100. const char* keyBuf, int keyBufLen, int keyType)
  101. {
  102. int ret = 0;
  103. TsipUserCtx* tuc = NULL;
  104. WOLFSSL_ENTER("tsip_use_PrivateKey_buffer");
  105. if (ssl == NULL || keyBuf == NULL || keyBufLen == 0 ) {
  106. ret = BAD_FUNC_ARG;
  107. }
  108. if (ret == 0){
  109. tuc = ssl->RenesasUserCtx;
  110. tuc->wrappedPrivateKey = (uint8_t*)keyBuf;
  111. tuc->wrappedKeyType = keyType;
  112. /* store keyType as Id since Id capacity is 32 bytes */
  113. ret = wolfSSL_use_PrivateKey_Id(ssl,
  114. (const unsigned char*)keyBuf, 32, tuc->devId);
  115. if (ret == WOLFSSL_SUCCESS) {
  116. ret = 0;
  117. }
  118. }
  119. WOLFSSL_LEAVE("tsip_use_PrivateKey_buffer", ret);
  120. return ret;
  121. }
  122. /* Set client encrypted public key data.
  123. * parameters:
  124. * ssl WOLFSSL object
  125. * keyBuf buffer holding wrapped key which Renesas key tool generated.
  126. * keyBufLen buffer length
  127. * keyType 0: RSA 2048bit, 1: RSA 4096bit, 2 ECC P256
  128. * return 0 on success, others on failure.
  129. */
  130. WOLFSSL_API int tsip_use_PublicKey_buffer(WOLFSSL* ssl,
  131. const char* keyBuf, int keyBufLen, int keyType)
  132. {
  133. int ret = 0;
  134. TsipUserCtx* tuc = NULL;
  135. WOLFSSL_ENTER("tsip_use_PublicKey_buffer");
  136. if (ssl == NULL || keyBuf == NULL || keyBufLen == 0) {
  137. ret = BAD_FUNC_ARG;
  138. }
  139. if (ret == 0){
  140. tuc = ssl->RenesasUserCtx;
  141. tuc->wrappedPublicKey = (uint8_t*)keyBuf;
  142. tuc->wrappedKeyType = keyType;
  143. }
  144. WOLFSSL_LEAVE("tsip_use_PublicKey_buffer", ret);
  145. return ret;
  146. }
  147. #endif /* WOLF_PRIVATE_KEY_ID */
  148. /* Obsolete function. Use tsip_use_PrivateKey_buffer instead.
  149. * Set client encrypted private key data.
  150. * parameters:
  151. * key Renesas Secure Flash Programmer generated key.
  152. * keyType 0: RSA 2048bit, 1: RSA 4096bit, 2 ECC P256
  153. * return 0 on success, others on failure.
  154. */
  155. WOLFSSL_API int tsip_set_clientPrivateKeyEnc(const byte* encKey, int keyType)
  156. {
  157. int ret = 0;
  158. WOLFSSL_ENTER("tsip_set_clientPrivateKeyEnc");
  159. if (ret == 0) {
  160. g_user_key_info.encrypted_user_private_key = (uint8_t*)encKey;
  161. g_user_key_info.encrypted_user_private_key_type = keyType;
  162. }
  163. WOLFSSL_LEAVE("tsip_set_clientPrivateKeyEnc", ret);
  164. return ret;
  165. }
  166. /* Flush raw handshake messages in MsgBag
  167. *
  168. */
  169. static void tsipFlushMessages(struct WOLFSSL* ssl)
  170. {
  171. TsipUserCtx* tuc = NULL;
  172. MsgBag* bag = NULL;
  173. if (ssl == NULL)
  174. return;
  175. /* get user context for TSIP */
  176. tuc = ssl->RenesasUserCtx;
  177. if (tuc == NULL) {
  178. return;
  179. }
  180. bag = &(tuc->messageBag);
  181. ForceZero(bag, sizeof(MsgBag));
  182. }
  183. WOLFSSL_LOCAL int tsip_TlsCleanup(struct WOLFSSL* ssl)
  184. {
  185. int ret = 0;
  186. TsipUserCtx* tuc = NULL;
  187. if (ssl == NULL)
  188. return BAD_FUNC_ARG;
  189. tuc = ssl->RenesasUserCtx;
  190. if (tuc == NULL)
  191. return ret;
  192. /* free stored messages */
  193. tsipFlushMessages(ssl);
  194. return ret;
  195. }
  196. #if defined(WOLFSSL_TLS13)
  197. /* generate ECC P265 key pair for ECDHE.
  198. * generated public key is stored in KeyShareEntry.pubkey and private key is
  199. * stored in TsipUserCtx.EcdhPrivKey13Idx as tsip specific format.
  200. * return 0 on success, CRYPTOCB_UNAVAILABLE when tsip can not handle and is
  201. * expecting to fallback to S/W, other negative values on error.
  202. */
  203. WOLFSSL_LOCAL int tsip_Tls13GenEccKeyPair(WOLFSSL* ssl, KeyShareEntry* kse)
  204. {
  205. int ret = 0;
  206. e_tsip_err_t err = TSIP_SUCCESS;
  207. int isTLS13 = 0;
  208. word16 curveId;
  209. ecc_key* ecckey = NULL;
  210. TsipUserCtx* tuc = NULL;
  211. WOLFSSL_ENTER("tsip_Tls13GenEccKeyPair");
  212. if (ssl == NULL || kse == NULL)
  213. ret = BAD_FUNC_ARG;
  214. if (ret == 0) {
  215. if (ssl->version.major == SSLv3_MAJOR &&
  216. ssl->version.minor == TLSv1_3_MINOR) {
  217. isTLS13 = 1;
  218. }
  219. /* TSIP works only in TLS13 client side */
  220. if (!isTLS13 || ssl->options.side != WOLFSSL_CLIENT_END) {
  221. ret = CRYPTOCB_UNAVAILABLE;
  222. }
  223. }
  224. if (ret == 0) {
  225. /* TSIP can handle SECP256R1 */
  226. if (kse->group != WOLFSSL_ECC_SECP256R1) {
  227. WOLFSSL_MSG("TSIP can't handle the specified ECC curve.");
  228. ret = CRYPTOCB_UNAVAILABLE;
  229. }
  230. }
  231. if (ret == 0) {
  232. /* get user context for TSIP */
  233. tuc = ssl->RenesasUserCtx;
  234. if (tuc == NULL) {
  235. ret = CRYPTOCB_UNAVAILABLE;
  236. }
  237. }
  238. curveId = ECC_SECP256R1;
  239. /* Allocate space for the public key */
  240. if (ret == 0) {
  241. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  242. DYNAMIC_TYPE_PUBLIC_KEY);
  243. if (kse->pubKey == NULL) {
  244. WOLFSSL_MSG("Key data Memory error");
  245. ret = MEMORY_E;
  246. }
  247. else {
  248. ForceZero(kse->pubKey, kse->pubKeyLen);
  249. }
  250. }
  251. /* Allocate an ECC key to hold private key. */
  252. if (ret == 0) {
  253. kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
  254. if (kse->key == NULL) {
  255. WOLFSSL_MSG("EccTempKey Memory error");
  256. ret = MEMORY_E;
  257. }
  258. else {
  259. ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
  260. }
  261. }
  262. if (ret == 0) {
  263. ecckey = (ecc_key*)kse->key;
  264. ret = wc_ecc_set_curve(ecckey, kse->keyLen, curveId);
  265. }
  266. kse->pubKey[0] = ECC_POINT_UNCOMP;
  267. /* generate ecc key pair with TSIP */
  268. if (ret == 0) {
  269. if ((ret = tsip_hw_lock()) == 0) {
  270. tuc->Dhe_key_set =0;
  271. err = R_TSIP_GenerateTls13P256EccKeyIndex(
  272. &(tuc->handle13),
  273. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  274. &(tuc->EcdhPrivKey13Idx), /* private key index */
  275. &(kse->pubKey[1])); /* generated public key */
  276. if (err != TSIP_SUCCESS){ret = WC_HW_E;}
  277. if (ret == 0) {
  278. WOLFSSL_MSG("ECDH private key-index is stored by TSIP");
  279. tuc->Dhe_key_set =1;
  280. }
  281. tsip_hw_unlock();
  282. }
  283. else {
  284. WOLFSSL_MSG("mutex locking error");
  285. }
  286. }
  287. if (ret != 0) {
  288. if (kse->key != NULL)
  289. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  290. if (kse->pubKey != NULL)
  291. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  292. }
  293. WOLFSSL_LEAVE("tsip_Tls13GenEccKeyPair", ret);
  294. return ret;
  295. }
  296. #endif /* WOLFSSL_TLS13 */
  297. #if defined(WOLFSSL_TLS13)
  298. /* generate shared secret(pre-master secret)
  299. * get peer's raw ECDHE public key from KeyShareEntry.
  300. * The pre-master secret generated by TSIP is stored into
  301. * TsipUserCtx.sharedSecret13Idx as TSIP specific format.
  302. *
  303. * return 0 on success, CRYPTOCB_UNAVAILABLE when tsip can not handle and is
  304. * expecting to fallback to S/W, other negative values on error.
  305. */
  306. WOLFSSL_LOCAL int tsip_Tls13GenSharedSecret(struct WOLFSSL* ssl,
  307. struct KeyShareEntry* kse)
  308. {
  309. int ret = 0;
  310. e_tsip_err_t err = TSIP_SUCCESS;
  311. int isTLS13 = 0;
  312. uint8_t* pubkeyraw = NULL;
  313. TsipUserCtx* tuc = NULL;
  314. WOLFSSL_ENTER("tsip_Tls13GenSharedSecret");
  315. if (ssl == NULL || kse == NULL)
  316. ret = BAD_FUNC_ARG;
  317. if (ret == 0) {
  318. if (ssl->version.major == SSLv3_MAJOR &&
  319. ssl->version.minor == TLSv1_3_MINOR) {
  320. isTLS13 = 1;
  321. }
  322. if (!isTLS13 || ssl->options.side != WOLFSSL_CLIENT_END) {
  323. WOLFSSL_MSG("Not in TLS1.3 or in client");
  324. ret = CRYPTOCB_UNAVAILABLE;
  325. }
  326. }
  327. if (ret == 0) {
  328. /* make sure it is in TLS13 and in client side */
  329. if (kse->group != WOLFSSL_ECC_SECP256R1) {
  330. WOLFSSL_MSG("TSIP can't handle the specified group");
  331. ret = CRYPTOCB_UNAVAILABLE;
  332. }
  333. }
  334. if (ret == 0) {
  335. /* get user context for TSIP */
  336. tuc = ssl->RenesasUserCtx;
  337. if (tuc == NULL) {
  338. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  339. ret = CRYPTOCB_UNAVAILABLE;
  340. }
  341. }
  342. if (ret == 0) {
  343. if (!tuc->Dhe_key_set) {
  344. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  345. ret = CRYPTOCB_UNAVAILABLE;
  346. }
  347. }
  348. if (ret == 0) {
  349. if ((ret = tsip_hw_lock()) == 0) {
  350. tuc->SharedSecret_set = 0;
  351. pubkeyraw = kse->ke + 1; /* peer's raw public key data */
  352. /* derive shared secret */
  353. err = R_TSIP_Tls13GenerateEcdheSharedSecret(
  354. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  355. pubkeyraw, /* peer's ECDHE public key */
  356. &(tuc->EcdhPrivKey13Idx), /*(out) own ECDHE priv key */
  357. &(tuc->sharedSecret13Idx)); /*(out) PreMasterSecret */
  358. if (err != TSIP_SUCCESS) {
  359. WOLFSSL_MSG("R_TSIP_Tls13GenerateEcdheSharedSecret error");
  360. ret = WC_HW_E;
  361. }
  362. if (ret == 0) {
  363. /* set flag for later tsip operations */
  364. tuc->SharedSecret_set = 1;
  365. }
  366. tsip_hw_unlock();
  367. }
  368. else {
  369. WOLFSSL_MSG("mutex locking error");
  370. }
  371. }
  372. WOLFSSL_LEAVE("tsip_Tls13GenSharedSecret", ret);
  373. return ret;
  374. }
  375. #endif /* WOLFSSL_TLS13 */
  376. #if defined(WOLFSSL_TLS13)
  377. WOLFSSL_LOCAL int tsip_Tls13DeriveEarlySecret(struct WOLFSSL* ssl)
  378. {
  379. int ret = 0;
  380. TsipUserCtx* tuc = NULL;
  381. WOLFSSL_ENTER("tsip_Tls13DeriveEarlySecret");
  382. if (ssl == NULL)
  383. ret = BAD_FUNC_ARG;
  384. if (ret == 0) {
  385. /* get user context for TSIP */
  386. tuc = ssl->RenesasUserCtx;
  387. if (tuc == NULL) {
  388. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  389. ret = CRYPTOCB_UNAVAILABLE;
  390. }
  391. else {
  392. tuc->EarlySecret_set = 1;
  393. }
  394. }
  395. WOLFSSL_LEAVE("tsip_Tls13DeriveEarlySecret", ret);
  396. return ret;
  397. }
  398. #endif /* WOLFSSL_TLS13 */
  399. #if defined(WOLFSSL_TLS13)
  400. /* derive handshake secret.
  401. * get pre-master secret stored in TsipUserCtx.sharedSecret13Idx.
  402. * Derived handshake secret is stored into TsipUserCtx.handshakeSecret13Idx
  403. * as tsip specific format.
  404. *
  405. * return 0 on success, CRYPTOCB_UNAVAILABLE when tsip can not handle and is
  406. * expecting to fallback to S/W, other negative values on error.
  407. */
  408. WOLFSSL_LOCAL int tsip_Tls13DeriveHandshakeSecret(struct WOLFSSL* ssl)
  409. {
  410. int ret = 0;
  411. e_tsip_err_t err = TSIP_SUCCESS;
  412. int isTLS13 = 0;
  413. TsipUserCtx* tuc = NULL;
  414. WOLFSSL_ENTER("tsip_Tls13DeriveHandshakeSecret");
  415. if (ssl == NULL)
  416. ret = BAD_FUNC_ARG;
  417. if (ret == 0) {
  418. if (ssl->version.major == SSLv3_MAJOR &&
  419. ssl->version.minor == TLSv1_3_MINOR) {
  420. isTLS13 = 1;
  421. }
  422. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  423. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  424. }
  425. }
  426. if (ret == 0) {
  427. /* get user context for TSIP */
  428. tuc = ssl->RenesasUserCtx;
  429. if (tuc == NULL) {
  430. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  431. ret = CRYPTOCB_UNAVAILABLE;
  432. }
  433. }
  434. if (ret == 0) {
  435. /* check if pre-master secret is generated by tsip */
  436. if (!tuc->SharedSecret_set) {
  437. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  438. ret = CRYPTOCB_UNAVAILABLE;
  439. }
  440. }
  441. if (ret == 0) {
  442. if ((ret = tsip_hw_lock()) == 0) {
  443. tuc->HandshakeSecret_set = 0;
  444. err = R_TSIP_Tls13GenerateHandshakeSecret(
  445. &(tuc->sharedSecret13Idx),
  446. &(tuc->handshakeSecret13Idx));
  447. if (err != TSIP_SUCCESS) {
  448. WOLFSSL_MSG("R_TSIP_Tls13GenerateHandshakeSecret error");
  449. ret = WC_HW_E;
  450. }
  451. if (ret == 0) {
  452. tuc->HandshakeSecret_set = 1;
  453. }
  454. tsip_hw_unlock();
  455. }
  456. else {
  457. WOLFSSL_MSG("mutex locking error");
  458. }
  459. }
  460. WOLFSSL_LEAVE("tsip_Tls13DeriveHandshakeSecret", ret);
  461. return ret;
  462. }
  463. #endif /* WOLFSSL_TLS13 */
  464. #if defined(WOLFSSL_TLS13)
  465. static int tsipTls13DeriveClientHandshakeKeys(struct WOLFSSL* ssl)
  466. {
  467. int ret = 0;
  468. e_tsip_err_t err = TSIP_SUCCESS;
  469. int isTLS13 = 0;
  470. TsipUserCtx* tuc = NULL;
  471. byte hash[WC_SHA256_DIGEST_SIZE];
  472. WOLFSSL_ENTER("tsipTls13DeriveClientHandshakeKeys");
  473. if (ssl == NULL)
  474. ret = BAD_FUNC_ARG;
  475. if (ret == 0) {
  476. if (ssl->version.major == SSLv3_MAJOR &&
  477. ssl->version.minor == TLSv1_3_MINOR) {
  478. isTLS13 = 1;
  479. }
  480. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  481. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  482. }
  483. }
  484. if (ret == 0) {
  485. /* get user context for TSIP */
  486. tuc = ssl->RenesasUserCtx;
  487. if (tuc == NULL) {
  488. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  489. ret = CRYPTOCB_UNAVAILABLE;
  490. }
  491. }
  492. if (ret == 0) {
  493. /* make sure client handshake secret is generated by tsip */
  494. if (!tuc->HandshakeSecret_set) {
  495. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  496. ret = CRYPTOCB_UNAVAILABLE;
  497. }
  498. }
  499. if (ret == 0) {
  500. /* get digest of handshake messages */
  501. ret = tsip_GetMessageSha256(ssl, hash, NULL);
  502. }
  503. if (ret == 0) {
  504. if ((ret = tsip_hw_lock()) == 0) {
  505. tuc->HandshakeClientTrafficKey_set = 0;
  506. err = R_TSIP_Tls13GenerateClientHandshakeTrafficKey(
  507. &(tuc->handle13),
  508. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  509. &(tuc->handshakeSecret13Idx),
  510. hash,
  511. &(tuc->clientWriteKey13Idx),
  512. &(tuc->clientFinished13Idx));
  513. if (err != TSIP_SUCCESS) {
  514. WOLFSSL_MSG(
  515. "R_TSIP_Tls13GenerateClientHandshakeTrafficKey error");
  516. ret = WC_HW_E;
  517. }
  518. /* key derivation succeeded */
  519. if (ret == 0) {
  520. tuc->HandshakeClientTrafficKey_set = 1;
  521. }
  522. tsip_hw_unlock();
  523. }
  524. else {
  525. WOLFSSL_MSG("mutex locking error");
  526. }
  527. }
  528. WOLFSSL_LEAVE("tsipTls13DeriveClientHandshakeKeys", ret);
  529. return ret;
  530. }
  531. #endif /* WOLFSSL_TLS13 */
  532. #if defined(WOLFSSL_TLS13)
  533. static int tsipTls13DeriveServerHandshakeKeys(struct WOLFSSL* ssl)
  534. {
  535. int ret = 0;
  536. e_tsip_err_t err = TSIP_SUCCESS;
  537. int isTLS13 = 0;
  538. TsipUserCtx* tuc = NULL;
  539. byte hash[WC_SHA256_DIGEST_SIZE];
  540. WOLFSSL_ENTER("tsipTls13DeriveServerHandshakeKeys");
  541. if (ssl == NULL)
  542. ret = BAD_FUNC_ARG;
  543. if (ret == 0) {
  544. if (ssl->version.major == SSLv3_MAJOR &&
  545. ssl->version.minor == TLSv1_3_MINOR) {
  546. isTLS13 = 1;
  547. }
  548. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  549. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  550. }
  551. }
  552. if (ret == 0) {
  553. /* get user context for TSIP */
  554. tuc = ssl->RenesasUserCtx;
  555. if (tuc == NULL) {
  556. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  557. ret = CRYPTOCB_UNAVAILABLE;
  558. }
  559. }
  560. if (ret == 0) {
  561. /* make sure client handshake secret is generated by tsip */
  562. if (!tuc->HandshakeSecret_set) {
  563. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  564. ret = CRYPTOCB_UNAVAILABLE;
  565. }
  566. }
  567. if (ret == 0) {
  568. /* get digest of handshake packets */
  569. ret = tsip_GetMessageSha256(ssl, hash, NULL);
  570. }
  571. if (ret == 0) {
  572. if ((ret = tsip_hw_lock()) == 0) {
  573. tuc->HandshakeServerTrafficKey_set = 0;
  574. err = R_TSIP_Tls13GenerateServerHandshakeTrafficKey(
  575. &(tuc->handle13),
  576. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  577. &(tuc->handshakeSecret13Idx),
  578. hash,
  579. &(tuc->serverWriteKey13Idx),
  580. &(tuc->serverFinished13Idx));
  581. if (err != TSIP_SUCCESS) {
  582. WOLFSSL_MSG(
  583. "R_TSIP_Tls13GenerateServerHandshakeTrafficKey error");
  584. ret = WC_HW_E;
  585. }
  586. /* key derivation succeeded */
  587. if (ret == 0) {
  588. tuc->HandshakeServerTrafficKey_set = 1;
  589. }
  590. tsip_hw_unlock();
  591. }
  592. else {
  593. WOLFSSL_MSG("mutex locking error");
  594. }
  595. }
  596. WOLFSSL_LEAVE("tsipTls13DeriveServerHandshakeKeys", ret);
  597. return ret;
  598. }
  599. #endif /* WOLFSSL_TLS13 */
  600. #if defined(WOLFSSL_TLS13)
  601. static int tsipTls13DeriveTrafficKeys(struct WOLFSSL* ssl)
  602. {
  603. int ret = 0;
  604. e_tsip_err_t err = TSIP_SUCCESS;
  605. int isTLS13 = 0;
  606. TsipUserCtx* tuc = NULL;
  607. byte hash[WC_SHA256_DIGEST_SIZE];
  608. WOLFSSL_ENTER("tsipTls13DeriveTrafficKeys");
  609. if (ssl == NULL)
  610. ret = BAD_FUNC_ARG;
  611. if (ret == 0) {
  612. if (ssl->version.major == SSLv3_MAJOR &&
  613. ssl->version.minor == TLSv1_3_MINOR) {
  614. isTLS13 = 1;
  615. }
  616. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  617. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  618. }
  619. }
  620. if (ret == 0) {
  621. /* get user context for TSIP */
  622. tuc = ssl->RenesasUserCtx;
  623. if (tuc == NULL) {
  624. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  625. ret = CRYPTOCB_UNAVAILABLE;
  626. }
  627. }
  628. if (ret == 0) {
  629. /* make sure master secret is generated by tsip */
  630. if (!tuc->MasterSecret_set) {
  631. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  632. ret = CRYPTOCB_UNAVAILABLE;
  633. }
  634. }
  635. if (ret == 0) {
  636. /* get digest of handshake messages */
  637. ret = tsip_GetMessageSha256(ssl, hash, NULL);
  638. }
  639. if (ret == 0) {
  640. if ((ret = tsip_hw_lock()) == 0) {
  641. tuc->ServerTrafficSecret_set = 0;
  642. tuc->ClientTrafficSecret_set = 0;
  643. tuc->ServerWriteTrafficKey_set = 0;
  644. tuc->ClientWriteTrafficKey_set = 0;
  645. err = R_TSIP_Tls13GenerateApplicationTrafficKey(
  646. &(tuc->handle13),
  647. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  648. &(tuc->masterSecret13Idx),
  649. (uint8_t*)hash,
  650. &(tuc->serverAppTraffic13Secret),
  651. &(tuc->clientAppTraffic13Secret),
  652. &(tuc->serverAppWriteKey13Idx),
  653. &(tuc->clientAppWriteKey13Idx));
  654. if (err != TSIP_SUCCESS) {
  655. WOLFSSL_MSG(
  656. "R_TSIP_Tls13GenerateApplicationTrafficKey error");
  657. ret = WC_HW_E;
  658. }
  659. /* key derivation succeeded */
  660. if (ret == 0) {
  661. tuc->ServerTrafficSecret_set = 1;
  662. tuc->ClientTrafficSecret_set = 1;
  663. tuc->ServerWriteTrafficKey_set = 1;
  664. tuc->ClientWriteTrafficKey_set = 1;
  665. }
  666. tsip_hw_unlock();
  667. }
  668. else {
  669. WOLFSSL_MSG("mutex locking error");
  670. }
  671. }
  672. WOLFSSL_LEAVE("tsipTls13DeriveTrafficKeys", ret);
  673. return ret;
  674. }
  675. #endif /* WOLFSSL_TLS13 */
  676. #if defined(WOLFSSL_TLS13)
  677. static int tsipTls13UpdateClientTrafficKeys(struct WOLFSSL* ssl)
  678. {
  679. int ret = 0;
  680. e_tsip_err_t err = TSIP_SUCCESS;
  681. int isTLS13 = 0;
  682. TsipUserCtx* tuc = NULL;
  683. WOLFSSL_ENTER("tsipTls13UpdateClientTrafficKeys");
  684. if (ssl == NULL)
  685. ret = BAD_FUNC_ARG;
  686. if (ret == 0) {
  687. if (ssl->version.major == SSLv3_MAJOR &&
  688. ssl->version.minor == TLSv1_3_MINOR) {
  689. isTLS13 = 1;
  690. }
  691. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  692. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  693. }
  694. }
  695. if (ret == 0) {
  696. /* get user context for TSIP */
  697. tuc = ssl->RenesasUserCtx;
  698. if (tuc == NULL) {
  699. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  700. ret = CRYPTOCB_UNAVAILABLE;
  701. }
  702. }
  703. if (ret == 0) {
  704. /* make sure application secret is generated by tsip */
  705. if (!tuc->ClientTrafficSecret_set) {
  706. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  707. ret = CRYPTOCB_UNAVAILABLE;
  708. }
  709. }
  710. if (ret == 0) {
  711. if ((ret = tsip_hw_lock()) == 0) {
  712. tuc->ClientWriteTrafficKey_set = 0;
  713. err = R_TSIP_Tls13UpdateApplicationTrafficKey(
  714. &(tuc->handle13),
  715. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  716. TSIP_TLS13_UPDATE_CLIENT_KEY,
  717. &(tuc->clientAppTraffic13Secret),
  718. &(tuc->clientAppTraffic13Secret),
  719. &(tuc->clientAppWriteKey13Idx));
  720. if (err != TSIP_SUCCESS) {
  721. WOLFSSL_MSG("R_TSIP_Tls13UpdateApplicationTrafficKey error");
  722. ret = WC_HW_E;
  723. }
  724. else {
  725. tuc->ClientWriteTrafficKey_set = 1;
  726. }
  727. tsip_hw_unlock();
  728. }
  729. else {
  730. WOLFSSL_MSG("mutex locking error");
  731. }
  732. }
  733. WOLFSSL_LEAVE("tsipTls13UpdateClientTrafficKeys", ret);
  734. return ret;
  735. }
  736. #endif /* WOLFSSL_TLS13 */
  737. #if defined(WOLFSSL_TLS13)
  738. static int tsipTls13UpdateServerTrafficKeys(struct WOLFSSL* ssl)
  739. {
  740. int ret = 0;
  741. e_tsip_err_t err = TSIP_SUCCESS;
  742. int isTLS13 = 0;
  743. TsipUserCtx* tuc = NULL;
  744. WOLFSSL_ENTER("tsipTls13UpdateServerTrafficKeys");
  745. if (ssl == NULL)
  746. ret = BAD_FUNC_ARG;
  747. if (ret == 0) {
  748. if (ssl->version.major == SSLv3_MAJOR &&
  749. ssl->version.minor == TLSv1_3_MINOR) {
  750. isTLS13 = 1;
  751. }
  752. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  753. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  754. }
  755. }
  756. if (ret == 0) {
  757. /* get user context for TSIP */
  758. tuc = ssl->RenesasUserCtx;
  759. if (tuc == NULL) {
  760. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  761. ret = CRYPTOCB_UNAVAILABLE;
  762. }
  763. }
  764. if (ret == 0) {
  765. /* make sure application secret is generated by tsip */
  766. if (!tuc->ServerTrafficSecret_set) {
  767. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  768. ret = CRYPTOCB_UNAVAILABLE;
  769. }
  770. }
  771. if (ret == 0) {
  772. if ((ret = tsip_hw_lock()) == 0) {
  773. tuc->ServerWriteTrafficKey_set = 0;
  774. err = R_TSIP_Tls13UpdateApplicationTrafficKey(
  775. &(tuc->handle13),
  776. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  777. TSIP_TLS13_UPDATE_SERVER_KEY,
  778. &(tuc->serverAppTraffic13Secret),
  779. &(tuc->serverAppTraffic13Secret),
  780. &(tuc->serverAppWriteKey13Idx));
  781. if (err != TSIP_SUCCESS) {
  782. WOLFSSL_MSG("R_TSIP_Tls13UpdateApplicationTrafficKey error");
  783. ret = WC_HW_E;
  784. }
  785. else {
  786. tuc->ServerWriteTrafficKey_set = 1;
  787. }
  788. tsip_hw_unlock();
  789. }
  790. else {
  791. WOLFSSL_MSG("mutex locking error");
  792. }
  793. }
  794. WOLFSSL_LEAVE("tsipTls13UpdateServerTrafficKeys", ret);
  795. return ret;
  796. }
  797. #endif /* WOLFSSL_TLS13 */
  798. #if defined(WOLFSSL_TLS13)
  799. /* Derive the keys for TLS v1.3.
  800. *
  801. * ssl The WOLFSSL object.
  802. * keyType kind of keys to derive.
  803. * handshake_key: when deriving keys for encrypting handshake messages.
  804. * traffic_key: when deriving first keys for encrypting traffic messages.
  805. * update_traffic_key: when deriving next keys for encrypting
  806. * traffic messages.
  807. *
  808. * side ENCRYPT_SIDE_ONLY: when only encryption secret needs to be derived.
  809. * DECRYPT_SIDE_ONLY: when only decryption secret needs to be derived.
  810. * ENCRYPT_AND_DECRYPT_SIDE: when both secret needs to be derived.
  811. *
  812. * returns 0 on success, CRYPTOCB_UNAVAILABLE when tsip can not handle and is
  813. * expecting to fallback to S/W, other negative values on error.
  814. */
  815. WOLFSSL_LOCAL int tsip_Tls13DeriveKeys(struct WOLFSSL* ssl,
  816. int keyType, int side)
  817. {
  818. int ret = 0;
  819. int provision;
  820. WOLFSSL_ENTER("tsip_Tls13DeriveKeys");
  821. if (side == ENCRYPT_AND_DECRYPT_SIDE) {
  822. provision = PROVISION_CLIENT_SERVER;
  823. }
  824. else {
  825. provision = ((ssl->options.side != WOLFSSL_CLIENT_END) ^
  826. (side == ENCRYPT_SIDE_ONLY)) ? PROVISION_CLIENT :
  827. PROVISION_SERVER;
  828. }
  829. /* derive client key */
  830. switch (keyType) {
  831. case early_data_key:
  832. WOLFSSL_MSG("TSIP can't handle early data key");
  833. ret = CRYPTOCB_UNAVAILABLE;
  834. break;
  835. case handshake_key:
  836. if (provision & PROVISION_CLIENT) {
  837. ret = tsipTls13DeriveClientHandshakeKeys(ssl);
  838. }
  839. break;
  840. case traffic_key:
  841. ret = tsipTls13DeriveTrafficKeys(ssl);
  842. break;
  843. case update_traffic_key:
  844. if (provision & PROVISION_CLIENT) {
  845. ret = tsipTls13UpdateClientTrafficKeys(ssl);
  846. }
  847. break;
  848. default:
  849. ret = CRYPTOCB_UNAVAILABLE;
  850. break;
  851. }
  852. if (ret == 0) {
  853. /* derive server key */
  854. switch (keyType) {
  855. case early_data_key:
  856. WOLFSSL_MSG("TSIP can't handle early data key");
  857. ret = CRYPTOCB_UNAVAILABLE;
  858. break;
  859. case handshake_key:
  860. if (provision & PROVISION_SERVER) {
  861. ret = tsipTls13DeriveServerHandshakeKeys(ssl);
  862. }
  863. break;
  864. case traffic_key:
  865. /* traffic key for server was derived in
  866. * tsipTls13DeriveTrafficKeys
  867. */
  868. break;
  869. case update_traffic_key:
  870. if (provision & PROVISION_SERVER) {
  871. ret = tsipTls13UpdateServerTrafficKeys(ssl);
  872. }
  873. break;
  874. default:
  875. ret = CRYPTOCB_UNAVAILABLE;
  876. break;
  877. }
  878. }
  879. WOLFSSL_LEAVE("tsip_Tls13DeriveKeys", ret);
  880. return ret;
  881. }
  882. #endif /* WOLFSSL_TLS13 */
  883. #if defined(WOLFSSL_TLS13)
  884. WOLFSSL_LOCAL int tsip_Tls13DeriveMasterSecret(struct WOLFSSL* ssl)
  885. {
  886. int ret = 0;
  887. e_tsip_err_t err = TSIP_SUCCESS;
  888. int isTLS13 = 0;
  889. TsipUserCtx* tuc = NULL;
  890. WOLFSSL_ENTER("tsip_Tls13DeriveMasterSecret");
  891. if (ssl == NULL)
  892. ret = BAD_FUNC_ARG;
  893. if (ret == 0) {
  894. if (ssl->version.major == SSLv3_MAJOR &&
  895. ssl->version.minor == TLSv1_3_MINOR) {
  896. isTLS13 = 1;
  897. }
  898. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  899. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  900. }
  901. }
  902. if (ret == 0) {
  903. /* get user context for TSIP */
  904. tuc = ssl->RenesasUserCtx;
  905. if (tuc == NULL) {
  906. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  907. ret = CRYPTOCB_UNAVAILABLE;
  908. }
  909. }
  910. if (ret == 0) {
  911. /* make sure handshake secret and verify data has been set by TSIP */
  912. if (!tuc->HandshakeSecret_set ||
  913. !tuc->HandshakeVerifiedData_set) {
  914. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  915. ret = CRYPTOCB_UNAVAILABLE;
  916. }
  917. }
  918. if (ret == 0) {
  919. if ((ret = tsip_hw_lock()) == 0) {
  920. tuc->MasterSecret_set = 0;
  921. err = R_TSIP_Tls13GenerateMasterSecret(
  922. &(tuc->handle13),
  923. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  924. &(tuc->handshakeSecret13Idx),
  925. (uint32_t*)tuc->verifyData13Idx,
  926. &(tuc->masterSecret13Idx));
  927. if (err != TSIP_SUCCESS) {
  928. WOLFSSL_MSG(
  929. "R_TSIP_Tls13GenerateMasterSecret( error");
  930. ret = WC_HW_E;
  931. }
  932. if (ret == 0) {
  933. tuc->MasterSecret_set = 1;
  934. }
  935. tsip_hw_unlock();
  936. }
  937. else {
  938. WOLFSSL_MSG("mutex locking error");
  939. }
  940. }
  941. WOLFSSL_LEAVE("tsip_Tls13DeriveMasterSecret", ret);
  942. return ret;
  943. }
  944. #endif /* WOLFSSL_TLS13 */
  945. #if defined(WOLFSSL_TLS13)
  946. /* verify handshake
  947. * ssl WOLFSSL object
  948. * hash buffer holding decrypted finished message content from server.
  949. *
  950. */
  951. static int tsipTls13VerifyHandshake(struct WOLFSSL* ssl,
  952. const byte* hash)/*finished message*/
  953. {
  954. int ret = 0;
  955. e_tsip_err_t err = TSIP_SUCCESS;
  956. int isTLS13 = 0;
  957. TsipUserCtx* tuc = NULL;
  958. word32 msgHash[WC_SHA256_DIGEST_SIZE/sizeof(word32)];
  959. WOLFSSL_ENTER("tsipTls13VerifyHandshake");
  960. if (ssl == NULL)
  961. ret = BAD_FUNC_ARG;
  962. if (ret == 0) {
  963. if (ssl->version.major == SSLv3_MAJOR &&
  964. ssl->version.minor == TLSv1_3_MINOR) {
  965. isTLS13 = 1;
  966. }
  967. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  968. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  969. }
  970. }
  971. if (ret == 0) {
  972. /* get user context for TSIP */
  973. tuc = ssl->RenesasUserCtx;
  974. if (tuc == NULL) {
  975. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  976. ret = CRYPTOCB_UNAVAILABLE;
  977. }
  978. }
  979. if (ret == 0) {
  980. /* make sure handshake secret is generated by tsip */
  981. if (!tuc->HandshakeServerTrafficKey_set) {
  982. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  983. ret = CRYPTOCB_UNAVAILABLE;
  984. }
  985. }
  986. /* get digest of handshake messages */
  987. if (ret == 0) {
  988. ret = tsip_GetMessageSha256(ssl, (byte*)msgHash, NULL);
  989. }
  990. if (ret == 0) {
  991. if ((ret = tsip_hw_lock()) == 0) {
  992. tuc->HandshakeVerifiedData_set = 0;
  993. err = R_TSIP_Tls13ServerHandshakeVerification(
  994. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  995. &(tuc->serverFinished13Idx),
  996. (uint8_t*)msgHash,
  997. (uint8_t*)hash,
  998. (uint32_t*)(tuc->verifyData13Idx));
  999. if (err == TSIP_ERR_VERIFICATION_FAIL) {
  1000. WOLFSSL_MSG("Handshake verification error");
  1001. ret = VERIFY_FINISHED_ERROR;
  1002. }
  1003. else if (err != TSIP_SUCCESS) {
  1004. WOLFSSL_MSG("R_TSIP_Tls13ServerHandshakeVerification error");
  1005. ret = WC_HW_E;
  1006. }
  1007. if (ret == 0) {
  1008. WOLFSSL_MSG("Verified handshake");
  1009. tuc->HandshakeVerifiedData_set = 1;
  1010. }
  1011. tsip_hw_unlock();
  1012. }
  1013. else {
  1014. WOLFSSL_MSG("mutex locking error");
  1015. }
  1016. }
  1017. WOLFSSL_LEAVE("tsipTls13VerifyHandshake", ret);
  1018. return ret;
  1019. }
  1020. #endif /* WOLFSSL_TLS13 */
  1021. #if defined(WOLFSSL_TLS13)
  1022. /* handles finished message from server.
  1023. * verify hmac in the message. Also output verify data to
  1024. * TsipUserCtx.verifyDataIdx, which is used for deriving master secret.
  1025. *
  1026. * ssl WOLFSSL object
  1027. * input the buffer holding decrypted finished message, type and padding
  1028. * inOutIdx On entry, the index into the message content of Finished.
  1029. * On exit, the index of byte after the Finished message and padding.
  1030. * size Length of message content(excluding type and padding)
  1031. * totalSz Length in the record header. means message + type + pad.
  1032. * return 0, on success, others on failure.
  1033. */
  1034. WOLFSSL_LOCAL int tsip_Tls13HandleFinished(
  1035. struct WOLFSSL* ssl,
  1036. const byte* input,
  1037. word32* inOutIdx,
  1038. word32 size,
  1039. word32 totalSz)
  1040. {
  1041. int ret = 0;
  1042. WOLFSSL_ENTER("tsip_Tls13HandleFinished");
  1043. if (ssl == NULL || input == NULL || inOutIdx == NULL) {
  1044. ret = BAD_FUNC_ARG;
  1045. }
  1046. if (ret == 0) {
  1047. ret = tsipTls13VerifyHandshake(ssl, input + *inOutIdx);
  1048. }
  1049. if (ret == 0) {
  1050. /* Force input exhaustion at ProcessReply by consuming padSz. */
  1051. *inOutIdx += size + ssl->keys.padSz;
  1052. ssl->options.serverState = SERVER_FINISHED_COMPLETE;
  1053. }
  1054. WOLFSSL_LEAVE("tsip_Tls13HandleFinished", ret);
  1055. return ret;
  1056. }
  1057. #endif /* WOLFSSL_TLS13 */
  1058. #if defined(WOLFSSL_TLS13)
  1059. /* Build TLS v1.3 Message and make it encrypted with AEAD algorithm.
  1060. * TSIP supports AES-GCM and AES-CCM.
  1061. * ssl The WOLFSSL object.
  1062. * output The buffer to write record message to.
  1063. * outSz Size of the buffer being written into.
  1064. * input The handshake message data to encrypt (excluding trailing type).
  1065. * inSz The size of the handshake message (including message header).
  1066. * type The real content type being put after the message data.
  1067. * hashOutput Whether to hash the unencrypted record data.
  1068. * returns the size of the record including header, CRYPTOCB_UNAVAILABLE
  1069. * when tsip can not handle and is expecting to fallback to S/W,
  1070. * other negative values on error.
  1071. */
  1072. WOLFSSL_LOCAL int tsip_Tls13BuildMessage(struct WOLFSSL* ssl,
  1073. byte* output,
  1074. int outSz,
  1075. const byte* input,
  1076. int inSz,
  1077. int type,
  1078. int hashOutput)
  1079. {
  1080. int ret = 0;
  1081. int recSz;
  1082. int isTLS13 = 0;
  1083. RecordLayerHeader* rl = NULL;
  1084. (void)outSz;
  1085. WOLFSSL_ENTER("tsip_Tls13BuildMessage");
  1086. if (ssl == NULL || output == NULL || input == NULL) {
  1087. ret = BAD_FUNC_ARG;
  1088. }
  1089. if (ret == 0) {
  1090. if (ssl->version.major == SSLv3_MAJOR &&
  1091. ssl->version.minor == TLSv1_3_MINOR) {
  1092. isTLS13 = 1;
  1093. }
  1094. if (!isTLS13 || (ssl->options.side != WOLFSSL_CLIENT_END)) {
  1095. ret = CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  1096. }
  1097. }
  1098. if (ret == 0) {
  1099. /* make sure hash algorithm is SHA256 */
  1100. if (ssl->specs.mac_algorithm != sha256_mac ) {
  1101. WOLFSSL_MSG("TSIP can't handle this hash algorithm.");
  1102. ret = CRYPTOCB_UNAVAILABLE;
  1103. }
  1104. }
  1105. if (ret == 0) {
  1106. if ((ssl->specs.bulk_cipher_algorithm != wolfssl_aes_gcm) &&
  1107. (ssl->specs.bulk_cipher_algorithm != wolfssl_aes_ccm)) {
  1108. WOLFSSL_MSG("TSIP can't handle the specified algorithm");
  1109. ret = CRYPTOCB_UNAVAILABLE;
  1110. }
  1111. }
  1112. if (ret == 0) {
  1113. /* set size in record header */
  1114. recSz = inSz + 1 + ssl->specs.aead_mac_size;
  1115. /* update the record header with the new size. */
  1116. rl = (RecordLayerHeader*)output;
  1117. rl->type = application_data;
  1118. rl->pvMajor = ssl->version.major;
  1119. rl->pvMinor = TLSv1_2_MINOR;
  1120. c16toa((word16)recSz, rl->length);
  1121. if (input != output + RECORD_HEADER_SZ) {
  1122. XMEMCPY(output + RECORD_HEADER_SZ, input, inSz);
  1123. }
  1124. if (hashOutput) {
  1125. ret = HashOutput(ssl, output, RECORD_HEADER_SZ + inSz, 0);
  1126. }
  1127. }
  1128. if (ret == 0) {
  1129. /* The real record content type goes at the end of the data. */
  1130. output[RECORD_HEADER_SZ + inSz] = (byte)type;
  1131. ret = tsip_Tls13AesEncrypt(ssl,
  1132. output + RECORD_HEADER_SZ, /* output */
  1133. output + RECORD_HEADER_SZ, /* plain message */
  1134. inSz + 1); /* plain data size(= inSz + 1 for type) */
  1135. if (ret > 0) {
  1136. ret = recSz + RECORD_HEADER_SZ; /* return record size */
  1137. }
  1138. }
  1139. WOLFSSL_LEAVE("tsip_Tls13BuildMessage", ret);
  1140. return ret;
  1141. }
  1142. #endif /* WOLFSSL_TLS13 */
  1143. #if defined(WOLFSSL_TLS13)
  1144. /* Send finished message to the server.
  1145. *
  1146. * ssl WOLFSSL object
  1147. * output buffer to output packet, including packet header and finished message
  1148. * outSz buffer size of output
  1149. * input buffer holding finished message
  1150. * hashOut
  1151. * return 0 on success, CRYPTOCB_UNAVAILABLE when TSIP can not handle,
  1152. * other negative values on error.
  1153. */
  1154. WOLFSSL_LOCAL int tsip_Tls13SendFinished(
  1155. struct WOLFSSL* ssl,
  1156. byte* output,
  1157. int outSz,
  1158. const byte* input,
  1159. int hashOut)
  1160. {
  1161. int ret = 0;
  1162. int finishedSz;
  1163. int headerSz = HANDSHAKE_HEADER_SZ;
  1164. int recordSz;
  1165. WOLFSSL_ENTER("tsip_Tls13SendFinished");
  1166. if (ssl == NULL || output == NULL || input == NULL || outSz == 0) {
  1167. ret = BAD_FUNC_ARG;
  1168. }
  1169. if (ret == 0) {
  1170. finishedSz = ssl->specs.hash_size;
  1171. ret = tsip_Tls13GetHmacMessages(ssl, (byte*)&input[headerSz]);
  1172. }
  1173. if (ret == 0) {
  1174. recordSz = WC_MAX_DIGEST_SIZE + DTLS_HANDSHAKE_HEADER_SZ + MAX_MSG_EXTRA;
  1175. /* check for available size */
  1176. ret = CheckAvailableSize(ssl, recordSz);
  1177. recordSz = 0;
  1178. }
  1179. if (ret == 0) {
  1180. recordSz = tsip_Tls13BuildMessage(ssl,
  1181. output, outSz,
  1182. input, headerSz + finishedSz,
  1183. handshake, hashOut);
  1184. if (recordSz > 0) {
  1185. ssl->options.clientState = CLIENT_FINISHED_COMPLETE;
  1186. ssl->options.handShakeState = HANDSHAKE_DONE;
  1187. ssl->options.handShakeDone = 1;
  1188. ssl->buffers.outputBuffer.length += recordSz; /* advance length */
  1189. ret = SendBuffered(ssl);
  1190. }
  1191. else {
  1192. ret = recordSz;
  1193. }
  1194. }
  1195. WOLFSSL_LEAVE("tsip_Tls13SendFinished", ret);
  1196. return ret;
  1197. }
  1198. #endif /* WOLFSSL_TLS13 */
  1199. #if defined(WOLFSSL_TLS13)
  1200. /* Parse and handle a TLS v1.3 CertificateVerify message sent from a server.
  1201. *
  1202. * ssl WOLFSSL object
  1203. * input buffer holding certificate verify message
  1204. * inOutIdx On entry, the index into the message buffer of
  1205. * CertificateVerify.
  1206. * On exit, the index of byte after the CertificateVerify message.
  1207. * totalSz The length of the current handshake message.
  1208. * return 0 on success, CRYPTOCB_UNAVAILABLE when TSIP can not handle,
  1209. * other negative values on error.
  1210. */
  1211. WOLFSSL_LOCAL int tsip_Tls13CertificateVerify(struct WOLFSSL* ssl,
  1212. const byte* input, word32* inOutIdx,
  1213. word32 totalSz)
  1214. {
  1215. int ret = 0;
  1216. byte* sigData = NULL;
  1217. byte hiAlgo,loAlgo;
  1218. int messageSz;
  1219. word16 signatureLen;
  1220. word16 idx;
  1221. e_tsip_err_t err = TSIP_SUCCESS;
  1222. TsipUserCtx* tuc = NULL;
  1223. e_tsip_tls13_signature_scheme_type_t sig_scheme;
  1224. WOLFSSL_ENTER("tsip_Tls13CertificateVerify");
  1225. if (ssl == NULL || input == NULL || inOutIdx == NULL) {
  1226. ret = BAD_FUNC_ARG;
  1227. }
  1228. if (ret == 0) {
  1229. if (ENUM_LEN + ENUM_LEN > totalSz) {
  1230. ret = BUFFER_ERROR;
  1231. }
  1232. /* parse certificate verify message to get hash-algo */
  1233. hiAlgo = *(input + *inOutIdx);
  1234. loAlgo = *(input + *inOutIdx + 1);
  1235. }
  1236. if (ret == 0) {
  1237. /* get signature length */
  1238. ato16(input + *inOutIdx + 2, &signatureLen);
  1239. if (ENUM_LEN + ENUM_LEN + OPAQUE16_LEN > totalSz) {
  1240. ret = BUFFER_ERROR;
  1241. }
  1242. }
  1243. if (ret == 0) {
  1244. if (ENUM_LEN + ENUM_LEN + OPAQUE16_LEN + signatureLen > totalSz) {
  1245. ret = BUFFER_ERROR;
  1246. }
  1247. }
  1248. if (ret == 0) {
  1249. /* check if tsip accepts signature algorithm */
  1250. if (hiAlgo == NEW_SA_MAJOR && loAlgo == sha256_mac) {
  1251. /* rsa_pss_rsae_sha256 0x0804 */
  1252. WOLFSSL_MSG("Peer sent RSA sig");
  1253. sig_scheme = TSIP_TLS13_SIGNATURE_SCHEME_RSA_PSS_RSAE_SHA256;
  1254. }
  1255. else if (hiAlgo == 0x04 && loAlgo == ecc_dsa_sa_algo) {
  1256. /* ecdsa_secp256r1_sha256 0x0403 */
  1257. WOLFSSL_MSG("Peer sent ECC sig");
  1258. sig_scheme = TSIP_TLS13_SIGNATURE_SCHEME_ECDSA_SECP256R1_SHA256;
  1259. }
  1260. else {
  1261. ret = CRYPTOCB_UNAVAILABLE;
  1262. }
  1263. }
  1264. if (ret == 0) {
  1265. /* get user context for TSIP */
  1266. tuc = ssl->RenesasUserCtx;
  1267. if (tuc == NULL) {
  1268. WOLFSSL_MSG("TsipUserCtx is not set to ssl.");
  1269. ret = CRYPTOCB_UNAVAILABLE;
  1270. }
  1271. }
  1272. /* check if peer's public key is stored */
  1273. if (ret == 0) {
  1274. if (ssl->peerSceTsipEncRsaKeyIndex == NULL) {
  1275. ret = CRYPTOCB_UNAVAILABLE;
  1276. }
  1277. }
  1278. if (ret == 0) {
  1279. /* create sign data */
  1280. sigData = tuc->sigDataCertVerify;
  1281. idx = 0;
  1282. ForceZero(sigData, sizeof(tuc->sigDataCertVerify));
  1283. XMEMSET(sigData, TSIP_SIGNING_DATA_PREFIX_BYTE,
  1284. TSIP_SIGNING_DATA_PREFIX_SZ);
  1285. idx += TSIP_SIGNING_DATA_PREFIX_SZ;
  1286. XMEMCPY(&sigData[idx], serverCertVfyLabel, TSIP_CERT_VFY_LABEL_SZ);
  1287. idx += TSIP_CERT_VFY_LABEL_SZ;
  1288. ret = tsip_GetMessageSha256(ssl, &sigData[idx], &messageSz);
  1289. }
  1290. if (ret == 0) {
  1291. if ((ret = tsip_hw_lock()) == 0) {
  1292. err = R_TSIP_Tls13CertificateVerifyVerification(
  1293. (uint32_t*)ssl->peerSceTsipEncRsaKeyIndex,
  1294. sig_scheme,
  1295. &sigData[idx],
  1296. (uint8_t*)(input + *inOutIdx),
  1297. totalSz);
  1298. if (err == TSIP_SUCCESS) {
  1299. *inOutIdx += totalSz;
  1300. *inOutIdx += ssl->keys.padSz;
  1301. ssl->options.peerAuthGood = 1;
  1302. ssl->options.havePeerVerify = 1;
  1303. #if !defined(NO_WOLFSSL_CLIENT)
  1304. if (ssl->options.side == WOLFSSL_CLIENT_END)
  1305. ssl->options.serverState = SERVER_CERT_VERIFY_COMPLETE;
  1306. #endif
  1307. }
  1308. else {
  1309. ret = WC_HW_E;
  1310. if (err == TSIP_ERR_AUTHENTICATION) {
  1311. WOLFSSL_MSG("Certificate Verification failed.");
  1312. }
  1313. }
  1314. tsip_hw_unlock();
  1315. }
  1316. else {
  1317. WOLFSSL_MSG("mutex locking error");
  1318. }
  1319. }
  1320. WOLFSSL_LEAVE("tsip_Tls13CertificateVerify", ret);
  1321. return ret;
  1322. }
  1323. #endif /* WOLFSSL_TLS13 */
  1324. /*
  1325. * Import wrapped private key then convert it into TSIP key_index format.
  1326. * The target key should be set with tsip_use_PrivateKey_buffer in advance.
  1327. * Acceptable key types are:
  1328. * TSIP_KEY_TYPE_RSA2048 rsa 2048 bit key
  1329. * TSIP_KEY_TYPE_RSA4096 rsa 4096 bit key(Not supported as of now)
  1330. * TSIP_KEY_TYPE_ECDSAP256 ecdsa p256r1 key
  1331. */
  1332. static int tsipImportPrivateKey(TsipUserCtx* tuc, int keyType)
  1333. {
  1334. int ret = 0;
  1335. e_tsip_err_t err = TSIP_SUCCESS;
  1336. uint8_t* provisioning_key = g_user_key_info.encrypted_provisioning_key;
  1337. uint8_t* iv = g_user_key_info.iv;
  1338. uint8_t* encPrivKey;
  1339. WOLFSSL_ENTER("tsipImportPrivateKey");
  1340. if (tuc == NULL)
  1341. return BAD_FUNC_ARG;
  1342. encPrivKey = tuc->wrappedPrivateKey;
  1343. if (encPrivKey == NULL || provisioning_key == NULL || iv == NULL) {
  1344. WOLFSSL_MSG("Missing some key materials used for import" );
  1345. return CRYPTOCB_UNAVAILABLE;
  1346. }
  1347. if (ret == 0) {
  1348. if (keyType != tuc->wrappedKeyType) {
  1349. WOLFSSL_MSG("No public key of specified type is set" );
  1350. return CRYPTOCB_UNAVAILABLE;
  1351. }
  1352. }
  1353. if ((ret = tsip_hw_lock()) == 0) {
  1354. switch (keyType) {
  1355. #if !defined(NO_RSA)
  1356. case TSIP_KEY_TYPE_RSA2048:
  1357. tuc->ClientRsa2048PrivKey_set = 0;
  1358. err = R_TSIP_GenerateRsa2048PrivateKeyIndex(
  1359. provisioning_key, iv, (uint8_t*)encPrivKey,
  1360. &(tuc->Rsa2048PrivateKeyIdx));
  1361. if (err == TSIP_SUCCESS) {
  1362. tuc->ClientRsa2048PrivKey_set = 1;
  1363. }
  1364. else {
  1365. ret = WC_HW_E;
  1366. }
  1367. break;
  1368. #endif
  1369. case TSIP_KEY_TYPE_RSA4096:
  1370. /* not supported as of TSIPv1.15 */
  1371. ret = CRYPTOCB_UNAVAILABLE;
  1372. break;
  1373. #if defined(HAVE_ECC)
  1374. case TSIP_KEY_TYPE_ECDSAP256:
  1375. tuc->ClientEccP256PrivKey_set = 0;
  1376. err = R_TSIP_GenerateEccP256PrivateKeyIndex(
  1377. provisioning_key, iv, (uint8_t*)encPrivKey,
  1378. &(tuc->EcdsaP256PrivateKeyIdx));
  1379. if (err == TSIP_SUCCESS) {
  1380. tuc->ClientEccP256PrivKey_set = 1;
  1381. }
  1382. else {
  1383. ret = WC_HW_E;
  1384. }
  1385. break;
  1386. #endif
  1387. default:
  1388. ret = BAD_FUNC_ARG;
  1389. break;
  1390. }
  1391. tsip_hw_unlock();
  1392. }
  1393. else {
  1394. WOLFSSL_MSG("mutex locking error");
  1395. }
  1396. WOLFSSL_LEAVE("tsipImportPrivateKey", ret);
  1397. return ret;
  1398. }
  1399. /*
  1400. * Import wrapped public key then convert it into TSIP key_index format.
  1401. * The target key should be set with tsip_use_PublicKey_buffer in advance.
  1402. * Acceptable key types are:
  1403. * TSIP_KEY_TYPE_RSA2048 rsa 2048 bit key
  1404. * TSIP_KEY_TYPE_RSA4096 rsa 4096 bit key(Not supported as of now)
  1405. * TSIP_KEY_TYPE_ECDSAP256 ecdsa p256r1 key
  1406. */
  1407. static int tsipImportPublicKey(TsipUserCtx* tuc, int keyType)
  1408. {
  1409. int ret = 0;
  1410. e_tsip_err_t err = TSIP_SUCCESS;
  1411. uint8_t* provisioning_key = g_user_key_info.encrypted_provisioning_key;
  1412. uint8_t* iv = g_user_key_info.iv;
  1413. uint8_t* encPubKey;
  1414. WOLFSSL_ENTER("tsipImportPublicKey");
  1415. if (tuc == NULL ) {
  1416. return BAD_FUNC_ARG;
  1417. }
  1418. encPubKey = tuc->wrappedPublicKey;
  1419. if (encPubKey == NULL || provisioning_key == NULL || iv == NULL) {
  1420. WOLFSSL_MSG("Missing some key materials used for import" );
  1421. return CRYPTOCB_UNAVAILABLE;
  1422. }
  1423. if (ret == 0) {
  1424. if (keyType != tuc->wrappedKeyType) {
  1425. WOLFSSL_MSG("No public key of specified type is set" );
  1426. return CRYPTOCB_UNAVAILABLE;
  1427. }
  1428. }
  1429. if ((ret = tsip_hw_lock()) == 0) {
  1430. switch(keyType) {
  1431. #if !defined(NO_RSA)
  1432. case TSIP_KEY_TYPE_RSA2048:
  1433. tuc->ClientRsa2048PubKey_set = 0;
  1434. err = R_TSIP_GenerateRsa2048PublicKeyIndex(
  1435. provisioning_key, iv, (uint8_t*)encPubKey,
  1436. &(tuc->Rsa2048PublicKeyIdx));
  1437. if (err == TSIP_SUCCESS) {
  1438. tuc->ClientRsa2048PubKey_set = 1;
  1439. }
  1440. else {
  1441. ret = WC_HW_E;
  1442. }
  1443. break;
  1444. #endif
  1445. #if !defined(NO_RSA)
  1446. case TSIP_KEY_TYPE_RSA4096:
  1447. /* not supported as of TSIPv1.15 */
  1448. ret = CRYPTOCB_UNAVAILABLE;
  1449. break;
  1450. #endif
  1451. #if defined(HAVE_ECC)
  1452. case TSIP_KEY_TYPE_ECDSAP256:
  1453. tuc->ClientEccP256PubKey_set = 0;
  1454. err = R_TSIP_GenerateEccP256PublicKeyIndex(
  1455. provisioning_key, iv, (uint8_t*)encPubKey,
  1456. &(tuc->EcdsaP256PublicKeyIdx));
  1457. if (err == TSIP_SUCCESS) {
  1458. tuc->ClientEccP256PubKey_set = 1;
  1459. }
  1460. else {
  1461. ret = WC_HW_E;
  1462. }
  1463. break;
  1464. #endif
  1465. default:
  1466. ret = BAD_FUNC_ARG;
  1467. break;
  1468. }
  1469. tsip_hw_unlock();
  1470. }
  1471. else {
  1472. WOLFSSL_MSG("mutex locking error");
  1473. }
  1474. WOLFSSL_LEAVE("tsipImportPublicKey", ret);
  1475. return ret;
  1476. }
  1477. #if defined(WOLFSSL_TLS13)
  1478. /* Send the TLS v1.3 CertificateVerify message. A part of the message is
  1479. * processed by TSIP for acceleration.
  1480. *
  1481. * Prior to this function call, the appropriate key-pair should be set via
  1482. * tsip_use_PrivateKey_buffer and tsip_use_PublicKey_buffer APIs. Those key pair
  1483. * can be generated by the tool named "Renesas secure flash programmer".
  1484. * When RSA certificate is used, both public and private keys should be set.
  1485. * The public key is used for self-verify the generated certificateVerify
  1486. * message. When ECC certificate is used, the self-verify will be performed only
  1487. * WOLFSSL_CHECK_SIG_FAULTS is defined.
  1488. *
  1489. * Returns 0 on success, CRYPTOCB_UNAVAILABLE when the required key is not
  1490. * provided or unsupported algo is specified and otherwise failure.
  1491. */
  1492. WOLFSSL_LOCAL int tsip_Tls13SendCertVerify(WOLFSSL* ssl)
  1493. {
  1494. int ret = 0;
  1495. e_tsip_err_t err = TSIP_SUCCESS;
  1496. byte* sigData = NULL;
  1497. word16 idx;
  1498. int isTLS13 = 0;
  1499. TsipUserCtx* tuc = NULL;
  1500. byte* output = NULL;
  1501. byte* message = NULL;
  1502. byte* derSig = NULL;
  1503. int isRsa = -1;
  1504. uint32_t messageSz,recordSz,hashSz;
  1505. byte hash[WC_SHA256_DIGEST_SIZE];
  1506. byte sig_rs[R_TSIP_ECDSA_DATA_BYTE_SIZE];
  1507. tsip_rsa_byte_data_t rsa_sig,rsa_hash;
  1508. tsip_ecdsa_byte_data_t ecdsa_sig,ecdsa_hash;
  1509. WOLFSSL_ENTER("tsip_Tls13SendCertVerify");
  1510. (void)derSig;
  1511. (void)rsa_sig;
  1512. (void)rsa_hash;
  1513. (void)ecdsa_sig;
  1514. (void)ecdsa_hash;
  1515. (void)sig_rs;
  1516. if (ssl == NULL) {
  1517. ret = BAD_FUNC_ARG;
  1518. }
  1519. if (ret == 0) {
  1520. if (ssl->version.major == SSLv3_MAJOR &&
  1521. ssl->version.minor == TLSv1_3_MINOR)
  1522. isTLS13 = 1;
  1523. /* check if it's TLS13 and client side */
  1524. if (!isTLS13 || ssl->options.side != WOLFSSL_CLIENT_END) {
  1525. ret = CRYPTOCB_UNAVAILABLE;
  1526. }
  1527. }
  1528. if (ret == 0) {
  1529. /* get user context for TSIP */
  1530. tuc = ssl->RenesasUserCtx;
  1531. if (tuc == NULL) {
  1532. ret = CRYPTOCB_UNAVAILABLE;
  1533. }
  1534. }
  1535. if (ret == 0) {
  1536. #if !defined(NO_RSA)
  1537. if (ssl->options.haveRSA)
  1538. isRsa = 1;
  1539. else
  1540. #endif
  1541. #ifdef HAVE_ECC
  1542. if (ssl->options.haveECC)
  1543. isRsa = 0;
  1544. else
  1545. #endif /* HAVE_ECC */
  1546. isRsa = -1;
  1547. if (isRsa != 0 && isRsa != 1) {
  1548. ret = CRYPTOCB_UNAVAILABLE;
  1549. }
  1550. }
  1551. if (ret == 0) {
  1552. ret = tsipImportPrivateKey(tuc, tuc->wrappedKeyType);
  1553. }
  1554. if (ret == 0) {
  1555. if (isRsa) {
  1556. if (!tuc->ClientRsa2048PrivKey_set) {
  1557. ret = NO_PRIVATE_KEY;
  1558. }
  1559. }
  1560. else {
  1561. if (!tuc->ClientEccP256PrivKey_set) {
  1562. ret = NO_PRIVATE_KEY;
  1563. }
  1564. }
  1565. }
  1566. if (ret == 0) {
  1567. /* get message hash */
  1568. ForceZero(hash, sizeof(hash));
  1569. ret = tsip_GetMessageSha256(ssl, hash, (int*)&hashSz);
  1570. }
  1571. if (ret == 0) {
  1572. recordSz = MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA * 2;
  1573. /* check for available size */
  1574. ret = CheckAvailableSize(ssl, recordSz);
  1575. recordSz = 0;
  1576. }
  1577. if (ret == 0) {
  1578. /* get output buffer for record header */
  1579. output = ssl->buffers.outputBuffer.buffer +
  1580. ssl->buffers.outputBuffer.length;
  1581. /* buffer for message header */
  1582. message = output + RECORD_HEADER_SZ;
  1583. }
  1584. /* generate signature */
  1585. if (ret == 0) {
  1586. if ((ret = tsip_hw_lock()) == 0) {
  1587. if (isRsa) {
  1588. err = R_TSIP_Tls13CertificateVerifyGenerate(
  1589. (uint32_t*)&(tuc->Rsa2048PrivateKeyIdx),
  1590. TSIP_TLS13_SIGNATURE_SCHEME_RSA_PSS_RSAE_SHA256,
  1591. hash,
  1592. message + HANDSHAKE_HEADER_SZ,
  1593. &messageSz);
  1594. }
  1595. else {
  1596. err = R_TSIP_Tls13CertificateVerifyGenerate(
  1597. (uint32_t*)&(tuc->EcdsaP256PrivateKeyIdx),
  1598. TSIP_TLS13_SIGNATURE_SCHEME_ECDSA_SECP256R1_SHA256,
  1599. hash,
  1600. message + HANDSHAKE_HEADER_SZ,
  1601. &messageSz);
  1602. }
  1603. if (err != TSIP_SUCCESS) {
  1604. WOLFSSL_MSG("failed to make certificate verify message");
  1605. ret = WC_HW_E;
  1606. }
  1607. tsip_hw_unlock();
  1608. }
  1609. else {
  1610. WOLFSSL_MSG("mutex locking error");
  1611. }
  1612. }
  1613. if (ret == 0) {
  1614. if (isRsa) {
  1615. ret = tsipImportPublicKey(tuc, tuc->wrappedKeyType);
  1616. }
  1617. else {
  1618. #if defined(WOLFSSL_CHECK_SIG_FAULTS)
  1619. ret = tsipImportPublicKey(tuc, tuc->wrappedKeyType);
  1620. #endif
  1621. }
  1622. }
  1623. if (ret == 0) {
  1624. if (isRsa) {
  1625. if (!tuc->ClientRsa2048PubKey_set) {
  1626. ret = NO_PRIVATE_KEY;
  1627. }
  1628. }
  1629. else {
  1630. #if defined(WOLFSSL_CHECK_SIG_FAULTS)
  1631. if (!tuc->ClientEccP256PubKey_set) {
  1632. ret = NO_PRIVATE_KEY;
  1633. }
  1634. #endif /* WOLFSSL_CHECK_SIG_FAULTS */
  1635. }
  1636. }
  1637. if (ret == 0) {
  1638. sigData = tuc->sigDataCertVerify;
  1639. idx = 0;
  1640. ForceZero(sigData, sizeof(tuc->sigDataCertVerify));
  1641. XMEMSET(sigData, TSIP_SIGNING_DATA_PREFIX_BYTE,
  1642. TSIP_SIGNING_DATA_PREFIX_SZ);
  1643. idx += TSIP_SIGNING_DATA_PREFIX_SZ;
  1644. XMEMCPY(&sigData[idx], clientCertVfyLabel, TSIP_CERT_VFY_LABEL_SZ);
  1645. idx += TSIP_CERT_VFY_LABEL_SZ;
  1646. XMEMCPY(&sigData[idx], hash, hashSz);
  1647. }
  1648. if (ret == 0) {
  1649. /* extract signature data from generated CertificateVerify message */
  1650. if (!isRsa) {
  1651. #if defined(WOLFSSL_CHECK_SIG_FAULTS)
  1652. idx = 4;
  1653. derSig = message +
  1654. HANDSHAKE_HEADER_SZ + HASH_SIG_SIZE + VERIFY_HEADER;
  1655. if (derSig[idx] == 0x00)
  1656. idx++;
  1657. XMEMCPY(sig_rs, &derSig[idx], R_TSIP_ECDSA_DATA_BYTE_SIZE / 2);
  1658. idx += (R_TSIP_ECDSA_DATA_BYTE_SIZE / 2) + ASN_TAG_SZ + 1;
  1659. if (derSig[idx] == 0x00)
  1660. idx++;
  1661. XMEMCPY(&sig_rs[R_TSIP_ECDSA_DATA_BYTE_SIZE / 2],
  1662. &derSig[idx], R_TSIP_ECDSA_DATA_BYTE_SIZE / 2);
  1663. #endif /* WOLFSSL_CHECK_SIG_FAULTS */
  1664. }
  1665. }
  1666. if (ret == 0) {
  1667. if ((ret = tsip_hw_lock()) == 0) {
  1668. if (isRsa) {
  1669. rsa_sig.pdata = message + HANDSHAKE_HEADER_SZ +
  1670. HASH_SIG_SIZE + VERIFY_HEADER;
  1671. rsa_hash.pdata = sigData;
  1672. rsa_hash.data_length = TSIP_SIGNING_DATA_PREFIX_SZ +
  1673. TSIP_CERT_VFY_LABEL_SZ + sizeof(hash);
  1674. rsa_hash.data_type = 0;
  1675. err = R_TSIP_RsassaPss2048SignatureVerification(
  1676. &rsa_sig, &rsa_hash,
  1677. &tuc->Rsa2048PublicKeyIdx,
  1678. R_TSIP_RSA_HASH_SHA256);
  1679. WOLFSSL_MSG("Perform self-verify for rsa signature");
  1680. }
  1681. else {
  1682. err = TSIP_SUCCESS;
  1683. #if defined(WOLFSSL_CHECK_SIG_FAULTS)
  1684. ecdsa_sig.pdata = sig_rs;
  1685. ecdsa_hash.pdata = sigData;
  1686. ecdsa_hash.data_length = TSIP_SIGNING_DATA_PREFIX_SZ +
  1687. TSIP_CERT_VFY_LABEL_SZ + sizeof(hash);
  1688. ecdsa_hash.data_type = 0;
  1689. err = R_TSIP_EcdsaP256SignatureVerification(
  1690. &ecdsa_sig, &ecdsa_hash,
  1691. &tuc->EcdsaP256PublicKeyIdx);
  1692. WOLFSSL_MSG("Perform self-verify for ecc signature");
  1693. #endif /* WOLFSSL_CHECK_SIG_FAULTS */
  1694. }
  1695. if (err != TSIP_SUCCESS) {
  1696. WOLFSSL_MSG("Failed to verify signature");
  1697. ret = VERIFY_SIGN_ERROR;
  1698. }
  1699. tsip_hw_unlock();
  1700. }
  1701. else {
  1702. WOLFSSL_MSG("mutex locking error");
  1703. }
  1704. }
  1705. /* create message header */
  1706. if (ret == 0) {
  1707. ((HandShakeHeader*)message)->type = certificate_verify;
  1708. c32to24(messageSz, ((HandShakeHeader*)message)->length);
  1709. recordSz = tsip_Tls13BuildMessage(ssl, output, 0, message,
  1710. messageSz + HANDSHAKE_HEADER_SZ,
  1711. handshake, 1);
  1712. if (recordSz > 0) {
  1713. ssl->buffers.outputBuffer.length += recordSz;
  1714. ret = SendBuffered(ssl);
  1715. }
  1716. else {
  1717. ret = recordSz;
  1718. }
  1719. }
  1720. WOLFSSL_LEAVE("tsip_Tls13SendCertVerify", ret);
  1721. return ret;
  1722. }
  1723. #endif /* WOLFSSL_TLS13 */
  1724. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >=109)
  1725. static uint32_t GetTsipCipherSuite(
  1726. uint8_t cipherSuiteFirst,
  1727. uint8_t cipherSuite)
  1728. {
  1729. WOLFSSL_ENTER("GetTsipCipherSuite");
  1730. uint32_t tsipCipher;
  1731. if (cipherSuiteFirst == CIPHER_BYTE)
  1732. {
  1733. switch(cipherSuite) {
  1734. case TLS_RSA_WITH_AES_128_CBC_SHA: /*2F*/
  1735. tsipCipher = R_TSIP_TLS_RSA_WITH_AES_128_CBC_SHA; /*0*/
  1736. break;
  1737. case TLS_RSA_WITH_AES_128_CBC_SHA256:
  1738. tsipCipher = R_TSIP_TLS_RSA_WITH_AES_128_CBC_SHA256;
  1739. break;
  1740. case TLS_RSA_WITH_AES_256_CBC_SHA:
  1741. tsipCipher = R_TSIP_TLS_RSA_WITH_AES_256_CBC_SHA;
  1742. break;
  1743. case TLS_RSA_WITH_AES_256_CBC_SHA256:
  1744. tsipCipher = R_TSIP_TLS_RSA_WITH_AES_256_CBC_SHA256;
  1745. break;
  1746. default:
  1747. tsipCipher = (uint32_t)WOLFSSL_TSIP_ILLEGAL_CIPHERSUITE;
  1748. break;
  1749. }
  1750. WOLFSSL_LEAVE("GetTsipCipherSuite", tsipCipher);
  1751. return tsipCipher;
  1752. }
  1753. else if (cipherSuiteFirst == ECC_BYTE)
  1754. {
  1755. tsipCipher = (uint32_t)WOLFSSL_TSIP_ILLEGAL_CIPHERSUITE;
  1756. switch(cipherSuite) {
  1757. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  1758. tsipCipher = R_TSIP_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256;
  1759. break;
  1760. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  1761. tsipCipher = R_TSIP_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256;
  1762. break;
  1763. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  1764. tsipCipher = R_TSIP_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256;
  1765. break;
  1766. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  1767. tsipCipher = R_TSIP_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
  1768. break;
  1769. default:
  1770. tsipCipher = (uint32_t)WOLFSSL_TSIP_ILLEGAL_CIPHERSUITE;
  1771. break;
  1772. }
  1773. }
  1774. else {
  1775. tsipCipher = (uint32_t)WOLFSSL_TSIP_ILLEGAL_CIPHERSUITE;
  1776. }
  1777. WOLFSSL_LEAVE("GetTsipCipherSuite", tsipCipher);
  1778. return tsipCipher;
  1779. }
  1780. /* Attempt to get a public key exchanged with the peer in ECDHE.
  1781. * the public key is verified by given signature then stored into ctx.
  1782. *
  1783. * return WOLFSSL_SUCCESS on success, WOLFSSL_FAILURE on failure.
  1784. */
  1785. static int tsip_ServerKeyExVerify(
  1786. word32 type, /* public key type 0:RSA2048 2:ECDSA P-256 */
  1787. WOLFSSL* ssl,
  1788. const byte* sig,
  1789. word32 sigSz,
  1790. void* ctx)
  1791. {
  1792. int ret = WOLFSSL_FAILURE;
  1793. byte qx[MAX_ECC_BYTES], qy[MAX_ECC_BYTES];
  1794. byte* peerkey = NULL;
  1795. word32 qxLen = sizeof(qx), qyLen = sizeof(qy);
  1796. TsipUserCtx* userCtx = NULL;
  1797. WOLFSSL_ENTER("tsip_ServerKeyExVerify");
  1798. /* sanity check */
  1799. if (ssl == NULL || sig == NULL || ctx == NULL)
  1800. return ret;
  1801. userCtx = (TsipUserCtx*)ctx;
  1802. /* export public peer public key */
  1803. ret = wc_ecc_export_public_raw(ssl->peerEccKey, qx, &qxLen, qy, &qyLen);
  1804. if (ret != 0) {
  1805. WOLFSSL_MSG("failed to export peer ecc key");
  1806. WOLFSSL_LEAVE("tsip_ServerKeyExVerify", ret);
  1807. return ret;
  1808. }
  1809. /* make peer ecc key data for SCE */
  1810. /* 0padding(24bit) || 04(8bit) || Qx(256bit) || Qy(256bit) */
  1811. peerkey = (byte*)XMALLOC((3 + 1 + qxLen + qyLen), NULL,
  1812. DYNAMIC_TYPE_TMP_BUFFER);
  1813. if (peerkey == NULL) {
  1814. WOLFSSL_MSG("failed to malloc ecc key");
  1815. WOLFSSL_LEAVE("tsip_ServerKeyExVerify", ret);
  1816. return WOLFSSL_FAILURE;
  1817. }
  1818. ForceZero(peerkey, (3 + 1 + qxLen + qyLen));
  1819. peerkey[3] = ECC_POINT_UNCOMP;
  1820. XMEMCPY(&peerkey[4], qx, qxLen);
  1821. XMEMCPY(&peerkey[4+qxLen], qy, qyLen);
  1822. /* 0 : RSA 2048bit, 1 : Reserved, 2 : ECDSA P-256 */
  1823. if ((ret = tsip_hw_lock()) == 0) {
  1824. ret = R_TSIP_TlsServersEphemeralEcdhPublicKeyRetrieves(
  1825. type,
  1826. (uint8_t*) ssl->arrays->clientRandom,
  1827. (uint8_t*) ssl->arrays->serverRandom,
  1828. (uint8_t*) peerkey,
  1829. (uint8_t*) sig,
  1830. (uint32_t*)ssl->peerSceTsipEncRsaKeyIndex,
  1831. (uint32_t*)userCtx->encrypted_ephemeral_ecdh_public_key);
  1832. if (ret !=TSIP_SUCCESS) {
  1833. WOLFSSL_MSG("R_TSIP_TlsServersEphemeralEcdhPublicKeyRetrieves failed");
  1834. }
  1835. else {
  1836. ret = WOLFSSL_SUCCESS;
  1837. }
  1838. tsip_hw_unlock();
  1839. }
  1840. else {
  1841. WOLFSSL_MSG("Failed to lock tsip hw");
  1842. }
  1843. XFREE(peerkey, 0, DYNAMIC_TYPE_TMP_BUFFER);
  1844. WOLFSSL_LEAVE("tsip_ServerKeyExVerify", ret);
  1845. return ret;
  1846. }
  1847. /*
  1848. * return 0 on success
  1849. */
  1850. int wc_tsip_RsaVerify(
  1851. WOLFSSL* ssl,
  1852. byte* sig, word32 sigSz,
  1853. byte** out,
  1854. const byte* key,
  1855. word32 keySz,
  1856. void* ctx)
  1857. {
  1858. int ret;
  1859. WOLFSSL_ENTER("tsip_RsaVerify");
  1860. if (tsip_usable(ssl, 0))
  1861. ret = tsip_ServerKeyExVerify(0, ssl, sig, sigSz, ctx);
  1862. else
  1863. ret = CRYPTOCB_UNAVAILABLE;
  1864. if (ret == WOLFSSL_SUCCESS)
  1865. ret = 0;
  1866. WOLFSSL_LEAVE("tsip_RsaVerify", ret);
  1867. return ret;
  1868. }
  1869. /* Verify signature for Server Key Exchange with TSIP
  1870. * TSIP can handle prime256v1 curve and sha256 hash
  1871. * parameters:
  1872. * ssl WOLFSSL object
  1873. * sig buffer holding DER encoded ecdsa signature data
  1874. * sigSz signature data size
  1875. * hash buffer holding sha256 hash data
  1876. * hashSz hash data size
  1877. * key buffer holding peer's public key (NOT used in this function)
  1878. * keySz public key size((NOT used in this function))
  1879. * result address of the variable to output result
  1880. * ctx context
  1881. * return 0 on success, CRYPTOCB_UNAVAILABLE in case TSIP cannot handle
  1882. */
  1883. int wc_tsip_EccVerify(
  1884. WOLFSSL* ssl,
  1885. const byte* sig, word32 sigSz,
  1886. const byte* hash, word32 hashSz,
  1887. const byte* key, word32 keySz,
  1888. int* result, void* ctx)
  1889. {
  1890. int ret = WOLFSSL_FAILURE;
  1891. uint8_t sigforSCE [R_TSIP_ECDSA_DATA_BYTE_SIZE] = {0};
  1892. const byte rs_size = R_TSIP_ECDSA_DATA_BYTE_SIZE/2;
  1893. byte offset = 0x3;
  1894. WOLFSSL_ENTER("wc_tsip_EccVerify");
  1895. /* check if TSIP can handle given cipher suite */
  1896. if (!tsip_usable(ssl, 0)) {
  1897. WOLFSSL_MSG("Cannot handle cipher suite by TSIP");
  1898. WOLFSSL_LEAVE("wc_tsip_EccVerify", CRYPTOCB_UNAVAILABLE);
  1899. return CRYPTOCB_UNAVAILABLE;
  1900. }
  1901. /* in TLS1.3 */
  1902. if (ssl->version.major == SSLv3_MAJOR &&
  1903. ssl->version.minor == TLSv1_3_MINOR) {
  1904. WOLFSSL_LEAVE("wc_tsip_EccVerify", CRYPTOCB_UNAVAILABLE);
  1905. return CRYPTOCB_UNAVAILABLE;
  1906. }
  1907. /* concatenate r and s parts of the signature so that TSIP can handle it*/
  1908. /* r */
  1909. if (sig[offset] == 0x20) {
  1910. XMEMCPY(sigforSCE, &sig[offset+1], rs_size);
  1911. offset = 0x25;
  1912. /* s */
  1913. if (sig[offset] == 0x20) {
  1914. XMEMCPY(&sigforSCE[rs_size], &sig[offset+1], rs_size);
  1915. }
  1916. else {
  1917. XMEMCPY(&sigforSCE[rs_size], &sig[offset+2], rs_size);
  1918. }
  1919. }
  1920. else {
  1921. XMEMCPY(sigforSCE, &sig[offset+2], rs_size);
  1922. offset = 0x26;
  1923. /* s */
  1924. if (sig[offset] == rs_size) {
  1925. XMEMCPY(&sigforSCE[rs_size], &sig[offset+1], rs_size);
  1926. }
  1927. else {
  1928. XMEMCPY(&sigforSCE[rs_size], &sig[offset+2], rs_size);
  1929. }
  1930. }
  1931. ret = tsip_ServerKeyExVerify(2, ssl, sigforSCE, 64, ctx);
  1932. if (ret == WOLFSSL_SUCCESS) {
  1933. *result = 1;
  1934. ret = 0; /* for success */
  1935. }
  1936. else
  1937. *result = 0;
  1938. WOLFSSL_LEAVE("wc_tsip_EccVerify", ret);
  1939. return ret;
  1940. }
  1941. /*
  1942. * generate premaster secret
  1943. * 1. generate P256 ECC key pair for ECDHE key exchange
  1944. * 2. generate pre-master secret
  1945. * output 64 bytes premaster secret to "out" buffer.
  1946. */
  1947. int wc_tsip_EccSharedSecret(
  1948. WOLFSSL* ssl,
  1949. ecc_key* otherKey,
  1950. unsigned char* pubKeyDer, unsigned int* pubKeySz,
  1951. unsigned char* out, unsigned int* outlen,
  1952. int side, void* ctx)
  1953. {
  1954. int ret;
  1955. TsipUserCtx* usrCtx = (TsipUserCtx*)ctx;
  1956. (void)ssl;
  1957. (void)otherKey;
  1958. WOLFSSL_ENTER("wc_tsip_EccSharedSecret");
  1959. /* sanity check */
  1960. if (ssl == NULL || pubKeyDer == NULL || pubKeySz == NULL ||
  1961. out == NULL || outlen == NULL || ctx == NULL) {
  1962. WOLFSSL_LEAVE("wc_tsip_EccSharedSecret", WOLFSSL_FAILURE);
  1963. return WOLFSSL_FAILURE;
  1964. }
  1965. if ((ret = tsip_hw_lock()) == 0) {
  1966. /* Generate ECC public key for key exchange */
  1967. ret = R_TSIP_GenerateTlsP256EccKeyIndex(
  1968. &usrCtx->ecc_p256_wrapped_key,
  1969. (uint8_t*)&usrCtx->ecc_ecdh_public_key);
  1970. if (ret == TSIP_SUCCESS) {
  1971. /* copy generated ecdh public key into buffer */
  1972. pubKeyDer[0] = ECC_POINT_UNCOMP;
  1973. *pubKeySz = 1 + sizeof(usrCtx->ecc_ecdh_public_key);
  1974. XMEMCPY(&pubKeyDer[1], &usrCtx->ecc_ecdh_public_key,
  1975. sizeof(usrCtx->ecc_ecdh_public_key));
  1976. /* Generate Premaster Secret */
  1977. ret = R_TSIP_TlsGeneratePreMasterSecretWithEccP256Key(
  1978. (uint32_t*)&usrCtx->encrypted_ephemeral_ecdh_public_key,
  1979. &usrCtx->ecc_p256_wrapped_key,
  1980. (uint32_t*)out/* pre-master secret 64 bytes */);
  1981. }
  1982. if (ret == TSIP_SUCCESS) {
  1983. *outlen = 64;
  1984. wolfSSL_CTX_SetGenMasterSecretCb(ssl->ctx,
  1985. Renesas_cmn_genMasterSecret);
  1986. wolfSSL_SetGenMasterSecretCtx(ssl, usrCtx);
  1987. }
  1988. tsip_hw_unlock();
  1989. }
  1990. else {
  1991. WOLFSSL_MSG("Failed to lock tsip hw");
  1992. }
  1993. WOLFSSL_LEAVE("wc_tsip_EccSharedSecret", ret);
  1994. return ret;
  1995. }
  1996. WOLFSSL_API void tsip_set_callbacks(WOLFSSL_CTX* ctx)
  1997. {
  1998. WOLFSSL_ENTER("tsip_set_callbacks");
  1999. wolfSSL_CTX_SetEccVerifyCb(ctx, (CallbackEccVerify)Renesas_cmn_EccVerify);
  2000. wolfSSL_CTX_SetRsaVerifyCb(ctx, (CallbackRsaVerify)Renesas_cmn_RsaVerify);
  2001. wolfSSL_CTX_SetGenPreMasterCb(ctx, Renesas_cmn_generatePremasterSecret);
  2002. wolfSSL_CTX_SetRsaEncCb(ctx, Renesas_cmn_RsaEnc);
  2003. #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
  2004. wolfSSL_CTX_SetVerifyMacCb(ctx, (CallbackVerifyMac)Renesas_cmn_VerifyHmac);
  2005. #endif /* !WOLFSSL_NO_TLS12 && !WOLFSSL_AEAD_ONLY */
  2006. wolfSSL_CTX_SetEccSharedSecretCb(ctx, NULL);
  2007. /* Set ssl-> options.sendVerify to SEND_CERT by the following two
  2008. * registrations. This will allow the client certificate to be sent to
  2009. * the server even if the private key is empty. The two callbacks do
  2010. * virtually nothing.
  2011. */
  2012. #ifdef WOLFSSL_TLS13
  2013. #ifdef HAVE_ECC
  2014. wolfSSL_CTX_SetEccSignCb(ctx, Renesas_cmn_EccSignCb);
  2015. #endif
  2016. #ifndef NO_RSA
  2017. wolfSSL_CTX_SetRsaSignCb(ctx, Renesas_cmn_RsaSignCb);
  2018. #endif
  2019. #endif /* WOLFSSL_TLS13 */
  2020. wolfSSL_CTX_SetRsaSignCheckCb(ctx, Renesas_cmn_RsaSignCheckCb);
  2021. /* set heap-hint to tsip_heap_hint so that tsip sha funcs can refer it */
  2022. if (ctx->heap != NULL) {
  2023. tsip_heap_hint = ctx->heap;
  2024. }
  2025. WOLFSSL_LEAVE("tsip_set_callbacks", 0);
  2026. }
  2027. WOLFSSL_API int tsip_set_callback_ctx(WOLFSSL* ssl, void* user_ctx)
  2028. {
  2029. WOLFSSL_ENTER("tsip_set_callback_ctx");
  2030. TsipUserCtx* uCtx = (TsipUserCtx*)user_ctx;
  2031. if (user_ctx == NULL) {
  2032. WOLFSSL_LEAVE("tsip_set_callback_ctx", 0);
  2033. return 0;
  2034. }
  2035. ForceZero(uCtx, sizeof(TsipUserCtx));
  2036. uCtx->ssl = ssl;
  2037. uCtx->ctx = ssl->ctx;
  2038. uCtx->heap = ssl->heap;
  2039. uCtx->side = ssl->ctx->method->side;
  2040. ssl->RenesasUserCtx = user_ctx; /* ssl doesn't own user_ctx */
  2041. wolfSSL_SetEccVerifyCtx(ssl, user_ctx);
  2042. wolfSSL_SetRsaEncCtx(ssl, user_ctx);
  2043. wolfSSL_SetRsaVerifyCtx(ssl, user_ctx);
  2044. wolfSSL_SetRsaSignCtx(ssl, user_ctx);
  2045. wolfSSL_SetGenPreMasterCtx(ssl, user_ctx);
  2046. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  2047. #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
  2048. wolfSSL_SetVerifyMacCtx(ssl, user_ctx);
  2049. #endif /* !WOLFSSL_NO_TLS12 && !WOLFSSL_AEAD_ONLY */
  2050. /* set up crypt callback */
  2051. wc_CryptoCb_CryptInitRenesasCmn(ssl, user_ctx);
  2052. WOLFSSL_LEAVE("tsip_set_callback_ctx", 0);
  2053. return 0;
  2054. }
  2055. #elif defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >=106)
  2056. /* convert def to tsip define */
  2057. static byte _tls2tsipdef(byte cipher)
  2058. {
  2059. byte def = R_TSIP_TLS_RSA_WITH_AES_128_CBC_SHA;
  2060. switch(cipher) {
  2061. case l_TLS_RSA_WITH_AES_128_CBC_SHA:
  2062. break;
  2063. case l_TLS_RSA_WITH_AES_128_CBC_SHA256:
  2064. def = R_TSIP_TLS_RSA_WITH_AES_128_CBC_SHA256;
  2065. break;
  2066. case l_TLS_RSA_WITH_AES_256_CBC_SHA:
  2067. def = R_TSIP_TLS_RSA_WITH_AES_256_CBC_SHA;
  2068. break;
  2069. case l_TLS_RSA_WITH_AES_256_CBC_SHA256:
  2070. def = R_TSIP_TLS_RSA_WITH_AES_256_CBC_SHA256;
  2071. break;
  2072. default:break;
  2073. }
  2074. return def;
  2075. }
  2076. #endif
  2077. /*
  2078. * lock hw engine.
  2079. * this should be called before using engine.
  2080. */
  2081. WOLFSSL_LOCAL int tsip_hw_lock(void)
  2082. {
  2083. int ret = 0;
  2084. if (tsip_CryptHwMutexInit_ == 0) {
  2085. ret = tsip_CryptHwMutexInit(&tsip_mutex);
  2086. if (ret == 0) {
  2087. tsip_CryptHwMutexInit_ = 1;
  2088. }
  2089. else {
  2090. WOLFSSL_MSG(" mutex initialization failed.");
  2091. return -1;
  2092. }
  2093. }
  2094. if (tsip_CryptHwMutexLock(&tsip_mutex) != 0) {
  2095. /* this should not happens */
  2096. return -1;
  2097. }
  2098. return ret;
  2099. }
  2100. /*
  2101. * release hw engine
  2102. */
  2103. WOLFSSL_LOCAL void tsip_hw_unlock(void)
  2104. {
  2105. tsip_CryptHwMutexUnLock(&tsip_mutex);
  2106. }
  2107. /* check if tsip tls functions can be used for the cipher */
  2108. /* return :1 when tsip can be used , 0 not be used. */
  2109. int tsip_usable(const WOLFSSL *ssl, uint8_t session_key_generated)
  2110. {
  2111. byte cipher0 = ssl->options.cipherSuite0;
  2112. byte cipher = ssl->options.cipherSuite;
  2113. byte side = ssl->options.side;
  2114. int ret = WOLFSSL_SUCCESS;
  2115. const Ciphers *enc;
  2116. const Ciphers *dec;
  2117. WOLFSSL_ENTER("tsip_usable");
  2118. /* sanity check */
  2119. if (ssl == NULL) {
  2120. WOLFSSL_MSG("ssl is NULL");
  2121. ret = BAD_FUNC_ARG;
  2122. }
  2123. /* when rsa key index == NULL, tsip isn't used for cert verification. */
  2124. /* in the case, we cannot use TSIP. */
  2125. if (ret == WOLFSSL_SUCCESS) {
  2126. if (!ssl->peerSceTsipEncRsaKeyIndex) {
  2127. WOLFSSL_MSG("ssl->peerSceTsipEncRsaKeyIndex is NULL");
  2128. ret = WOLFSSL_FAILURE;
  2129. }
  2130. }
  2131. if (ret == WOLFSSL_SUCCESS) {
  2132. if (session_key_generated) {
  2133. enc = &ssl->encrypt;
  2134. dec = &ssl->decrypt;
  2135. if (enc == NULL || dec == NULL) {
  2136. /* something wrong */
  2137. ret = WOLFSSL_FAILURE;
  2138. }
  2139. if (enc->aes == NULL || dec->aes == NULL) {
  2140. ret = WOLFSSL_FAILURE;
  2141. }
  2142. if (enc->aes->ctx.setup == 0) {
  2143. /* session key for SCE is not created */
  2144. ret = WOLFSSL_FAILURE;
  2145. }
  2146. }
  2147. }
  2148. /* when enabled Extended Master Secret, we cannot use TSIP. */
  2149. if (ret == WOLFSSL_SUCCESS) {
  2150. if (ssl->options.haveEMS) {
  2151. WOLFSSL_MSG("ssl->options.haveEMS");
  2152. ret = WOLFSSL_FAILURE;
  2153. }
  2154. }
  2155. /* TSIP works only for TLS client */
  2156. if (ret == WOLFSSL_SUCCESS) {
  2157. if (side != WOLFSSL_CLIENT_END) {
  2158. WOLFSSL_MSG("Not client side");
  2159. ret = WOLFSSL_FAILURE;
  2160. }
  2161. }
  2162. /* Check if TSIP can handle cipher suite */
  2163. if (ret == WOLFSSL_SUCCESS) {
  2164. if (
  2165. (cipher0 == CIPHER_BYTE &&
  2166. (cipher == l_TLS_RSA_WITH_AES_128_CBC_SHA ||
  2167. cipher == l_TLS_RSA_WITH_AES_128_CBC_SHA256 ||
  2168. cipher == l_TLS_RSA_WITH_AES_256_CBC_SHA ||
  2169. cipher == l_TLS_RSA_WITH_AES_256_CBC_SHA256))
  2170. #if (WOLFSSL_RENESAS_TSIP_VER >= 109)
  2171. ||
  2172. (cipher0 == ECC_BYTE &&
  2173. (cipher == l_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 ||
  2174. cipher == l_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 ||
  2175. cipher == l_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 ||
  2176. cipher == l_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256))
  2177. #endif
  2178. #if (WOLFSSL_RENESAS_TSIP_VER >= 114)
  2179. ||
  2180. (cipher0 == TLS13_BYTE &&
  2181. (cipher == l_TLS_AES_128_GCM_SHA256 ||
  2182. cipher == l_TLS_AES_128_CCM_SHA256))
  2183. #endif
  2184. ) {
  2185. WOLFSSL_MSG("supported cipher suite");
  2186. }
  2187. else {
  2188. WOLFSSL_MSG("unsupported cipher suite");
  2189. ret = WOLFSSL_FAILURE;;
  2190. }
  2191. }
  2192. WOLFSSL_LEAVE("tsip_usable", ret);
  2193. return ret;
  2194. }
  2195. /* open TSIP driver
  2196. * return 0 on success.
  2197. */
  2198. WOLFSSL_LOCAL int tsip_Open(void)
  2199. {
  2200. int ret = TSIP_SUCCESS;
  2201. WOLFSSL_ENTER("tsip_Open");
  2202. if ((ret = tsip_hw_lock()) == 0) {
  2203. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER>=109)
  2204. ret = R_TSIP_Open(NULL,NULL);
  2205. if (ret != TSIP_SUCCESS) {
  2206. WOLFSSL_MSG("RENESAS TSIP Open failed");
  2207. }
  2208. if (ret == TSIP_SUCCESS && g_user_key_info.encrypted_user_tls_key) {
  2209. ret = R_TSIP_GenerateTlsRsaPublicKeyIndex(
  2210. g_user_key_info.encrypted_provisioning_key,
  2211. g_user_key_info.iv,
  2212. g_user_key_info.encrypted_user_tls_key,
  2213. &g_user_key_info.user_rsa2048_tls_pubindex); /* OUT */
  2214. R_TSIP_Close(); /* close once */
  2215. if (ret != TSIP_SUCCESS) {
  2216. WOLFSSL_MSG("R_TSIP_GenerateTlsRsa: NG");
  2217. }
  2218. else {
  2219. /* open again with newly created TLS public key index*/
  2220. ret = R_TSIP_Open(
  2221. &g_user_key_info.user_rsa2048_tls_pubindex,
  2222. (tsip_update_key_ring_t*)s_inst2);
  2223. if (ret != TSIP_SUCCESS) {
  2224. WOLFSSL_MSG("R_TSIP_(Re)Open: NG");
  2225. }
  2226. /* init vars */
  2227. g_CAscm_Idx = (uint32_t)-1;
  2228. }
  2229. }
  2230. #elif defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER>=106)
  2231. ret = R_TSIP_Open((uint32_t*)s_flash, s_inst1, s_inst2);
  2232. if (ret != TSIP_SUCCESS) {
  2233. WOLFSSL_MSG("RENESAS TSIP Open failed");
  2234. }
  2235. /* generate TLS Rsa public key for Certificate verification */
  2236. if (ret == TSIP_SUCCESS && g_user_key_info.encrypted_user_tls_key) {
  2237. ret = R_TSIP_GenerateTlsRsaPublicKeyIndex(
  2238. g_user_key_info.encrypted_session_key,
  2239. g_user_key_info.iv,
  2240. g_user_key_info.encrypted_user_tls_key,
  2241. &g_user_key_info.user_rsa2048_tls_pubindex);
  2242. if (ret != TSIP_SUCCESS) {
  2243. WOLFSSL_MSG("R_TSIP_GenerateTlsRsaPublicKeyIndex failed");
  2244. }
  2245. else {
  2246. /* close once */
  2247. tsip_Close();
  2248. /* open again with s_inst[] */
  2249. XMEMCPY(s_inst1,
  2250. g_user_key_info.user_rsa2048_tls_pubindex.value,
  2251. sizeof(s_inst1));
  2252. ret = R_TSIP_Open((uint32_t*)s_flash, s_inst1, s_inst2);
  2253. if (ret != TSIP_SUCCESS) {
  2254. WOLFSSL_MSG("R_TSIP_(Re)Open failed");
  2255. }
  2256. /* init vars */
  2257. g_CAscm_Idx = (uint32_t)-1;
  2258. }
  2259. }
  2260. #else
  2261. ret = R_TSIP_Open((uint32_t*)s_flash, s_inst1, s_inst2);
  2262. if (ret != TSIP_SUCCESS) {
  2263. WOLFSSL_MSG("RENESAS TSIP Open failed");
  2264. }
  2265. #endif
  2266. /* unlock hw */
  2267. tsip_hw_unlock();
  2268. }
  2269. else
  2270. WOLFSSL_MSG("Failed to lock tsip hw ");
  2271. WOLFSSL_LEAVE("tsip_Open", ret);
  2272. return ret;
  2273. }
  2274. /* close TSIP driver */
  2275. WOLFSSL_LOCAL void tsip_Close(void)
  2276. {
  2277. WOLFSSL_ENTER("tsip_Close");
  2278. int ret;
  2279. if ((ret = tsip_hw_lock()) == 0) {
  2280. /* close TSIP */
  2281. ret = R_TSIP_Close();
  2282. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  2283. g_CAscm_Idx = (uint32_t)-1;
  2284. #endif
  2285. /* unlock hw */
  2286. tsip_hw_unlock();
  2287. if (ret != TSIP_SUCCESS) {
  2288. WOLFSSL_MSG("RENESAS TSIP Close failed");
  2289. }
  2290. }
  2291. else
  2292. WOLFSSL_MSG("Failed to unlock tsip hw");
  2293. WOLFSSL_LEAVE("tsip_Close", 0);
  2294. }
  2295. /* Support functions for TSIP TLS Capability */
  2296. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  2297. /* to inform ca certificate sign */
  2298. /* signature format expects RSA 2048 PSS with SHA256 */
  2299. void tsip_inform_cert_sign(const byte *sign)
  2300. {
  2301. if (sign)
  2302. ca_cert_sig = sign;
  2303. }
  2304. #if (WOLFSSL_RENESAS_TSIP_VER>=109)
  2305. void tsip_inform_user_keys_ex(
  2306. byte* encrypted_provisioning_key,
  2307. byte* iv,
  2308. byte* encrypted_user_tls_key,
  2309. word32 encrypted_user_tls_key_type)
  2310. {
  2311. WOLFSSL_ENTER("tsip_inform_user_keys_ex");
  2312. ForceZero(&g_user_key_info, sizeof(g_user_key_info));
  2313. g_user_key_info.encrypted_provisioning_key = NULL;
  2314. g_user_key_info.iv = NULL;
  2315. if (encrypted_provisioning_key) {
  2316. g_user_key_info.encrypted_provisioning_key = encrypted_provisioning_key;
  2317. }
  2318. if (iv) {
  2319. g_user_key_info.iv = iv;
  2320. }
  2321. if (encrypted_user_tls_key) {
  2322. g_user_key_info.encrypted_user_tls_key = encrypted_user_tls_key;
  2323. }
  2324. g_user_key_info.encrypted_user_tls_key_type = encrypted_user_tls_key_type;
  2325. WOLFSSL_LEAVE("tsip_inform_user_keys_ex", 0);
  2326. }
  2327. #elif (WOLFSSL_RENESAS_TSIP_VER>=106)
  2328. /* inform user key */
  2329. /* the function expects to be called from user application */
  2330. /* user has to create these key information by Renesas tool in advance.*/
  2331. void tsip_inform_user_keys(
  2332. byte *encrypted_session_key,
  2333. byte *iv,
  2334. byte *encrypted_user_tls_key)
  2335. {
  2336. g_user_key_info.encrypted_session_key = NULL;
  2337. g_user_key_info.iv = NULL;
  2338. g_user_key_info.encrypted_user_tls_key = NULL;
  2339. if (encrypted_session_key) {
  2340. g_user_key_info.encrypted_session_key = encrypted_session_key;
  2341. }
  2342. if (iv) {
  2343. g_user_key_info.iv = iv;
  2344. }
  2345. if (encrypted_user_tls_key) {
  2346. g_user_key_info.encrypted_user_tls_key = encrypted_user_tls_key;
  2347. }
  2348. }
  2349. #endif
  2350. /* Sha1Hmac */
  2351. int wc_tsip_Sha1HmacGenerate(
  2352. const WOLFSSL *ssl,
  2353. const byte* myInner,
  2354. word32 innerSz,
  2355. const byte* in,
  2356. word32 sz,
  2357. byte* digest)
  2358. {
  2359. WOLFSSL_ENTER("wc_tsip_Sha1HmacGenerate");
  2360. tsip_hmac_sha_handle_t _handle;
  2361. tsip_hmac_sha_key_index_t key_index;
  2362. int ret;
  2363. if ((ssl == NULL) || (myInner == NULL) || (in == NULL) ||
  2364. (digest == NULL)) {
  2365. WOLFSSL_LEAVE("wc_tsip_Sha1HmacGenerate", BAD_FUNC_ARG);
  2366. return BAD_FUNC_ARG;
  2367. }
  2368. if ((ret = tsip_hw_lock()) != 0) {
  2369. WOLFSSL_MSG("hw lock failed");
  2370. WOLFSSL_LEAVE("wc_tsip_Sha1HmacGenerate", ret);
  2371. return ret;
  2372. }
  2373. key_index = ssl->keys.tsip_client_write_MAC_secret;
  2374. ret = R_TSIP_Sha1HmacGenerateInit(
  2375. &_handle,
  2376. &key_index);
  2377. if (ret == TSIP_SUCCESS)
  2378. ret = R_TSIP_Sha1HmacGenerateUpdate(
  2379. &_handle,
  2380. (uint8_t*)myInner,
  2381. (uint32_t)innerSz);
  2382. if (ret == TSIP_SUCCESS)
  2383. ret = R_TSIP_Sha1HmacGenerateUpdate(
  2384. &_handle,
  2385. (uint8_t*)in,
  2386. sz);
  2387. if (ret == TSIP_SUCCESS)
  2388. ret = R_TSIP_Sha1HmacGenerateFinal(
  2389. &_handle,
  2390. digest);
  2391. tsip_hw_unlock();
  2392. WOLFSSL_LEAVE("wc_tsip_Sha1HmacGenerate", ret);
  2393. return ret;
  2394. }
  2395. /* Sha256Hmac */
  2396. int wc_tsip_Sha256HmacGenerate(
  2397. const WOLFSSL *ssl,
  2398. const byte* myInner,
  2399. word32 innerSz,
  2400. const byte* in,
  2401. word32 sz,
  2402. byte* digest)
  2403. {
  2404. WOLFSSL_ENTER("wc_tsip_Sha256HmacGenerate");
  2405. tsip_hmac_sha_handle_t _handle;
  2406. tsip_hmac_sha_key_index_t key_index;
  2407. int ret;
  2408. if ((ssl == NULL) || (myInner == NULL) || (in == NULL) ||
  2409. (digest == NULL))
  2410. return BAD_FUNC_ARG;
  2411. key_index = ssl->keys.tsip_client_write_MAC_secret;
  2412. if ((ret = tsip_hw_lock()) != 0) {
  2413. WOLFSSL_MSG("hw lock failed");
  2414. return ret;
  2415. }
  2416. ret = R_TSIP_Sha256HmacGenerateInit(
  2417. &_handle,
  2418. &key_index);
  2419. if (ret == TSIP_SUCCESS) {
  2420. ret = R_TSIP_Sha256HmacGenerateUpdate(
  2421. &_handle,
  2422. (uint8_t*)myInner,
  2423. innerSz);
  2424. }
  2425. else {
  2426. WOLFSSL_MSG("R_TSIP_Sha256HmacGenerateInit failed");
  2427. }
  2428. if (ret == TSIP_SUCCESS) {
  2429. ret = R_TSIP_Sha256HmacGenerateUpdate(
  2430. &_handle,
  2431. (uint8_t*)in,
  2432. sz);
  2433. }
  2434. else {
  2435. WOLFSSL_MSG("R_TSIP_Sha256HmacGenerateUpdate: inner failed");
  2436. }
  2437. if (ret == TSIP_SUCCESS) {
  2438. ret = R_TSIP_Sha256HmacGenerateFinal(
  2439. &_handle,
  2440. digest);
  2441. }
  2442. else {
  2443. WOLFSSL_MSG("R_TSIP_Sha256HmacGenerateUpdate: in failed");
  2444. }
  2445. if (ret != TSIP_SUCCESS) {
  2446. WOLFSSL_MSG("R_TSIP_Sha256HmacGenerateFinal failed");
  2447. ret = 1;
  2448. }
  2449. /* unlock hw */
  2450. tsip_hw_unlock();
  2451. WOLFSSL_LEAVE("wc_tsip_Sha256HmacGenerate", ret);
  2452. return ret;
  2453. }
  2454. /*
  2455. * Perform SHA1 and SHA256 Hmac verification
  2456. */
  2457. int wc_tsip_ShaXHmacVerify(
  2458. const WOLFSSL *ssl,
  2459. const byte* message,
  2460. word32 messageSz,
  2461. word32 macSz,
  2462. word32 content)
  2463. {
  2464. WOLFSSL_ENTER("tsip_ShaXHmacVerify");
  2465. tsip_hmac_sha_handle_t handle;
  2466. tsip_hmac_sha_key_index_t wrapped_key;
  2467. shaHmacInitFn initFn = NULL;
  2468. shaHmacUpdateFn updateFn = NULL;
  2469. shaHmacFinalFn finalFn = NULL;
  2470. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  2471. int ret;
  2472. if ((ssl == NULL) || (message == NULL)) {
  2473. WOLFSSL_LEAVE("tsip_ShaXHmacVerify", BAD_FUNC_ARG);
  2474. return BAD_FUNC_ARG;
  2475. }
  2476. wrapped_key = ssl->keys.tsip_server_write_MAC_secret;
  2477. if (wrapped_key.type == TSIP_KEY_INDEX_TYPE_HMAC_SHA1_FOR_TLS) {
  2478. WOLFSSL_MSG("perform Sha1-Hmac verification");
  2479. initFn = R_TSIP_Sha1HmacVerifyInit;
  2480. updateFn = R_TSIP_Sha1HmacVerifyUpdate;
  2481. finalFn = R_TSIP_Sha1HmacVerifyFinal;
  2482. }
  2483. else if (wrapped_key.type == TSIP_KEY_INDEX_TYPE_HMAC_SHA256_FOR_TLS) {
  2484. WOLFSSL_MSG("perform Sha256-Hmac verification");
  2485. initFn = R_TSIP_Sha256HmacVerifyInit;
  2486. updateFn = R_TSIP_Sha256HmacVerifyUpdate;
  2487. finalFn = R_TSIP_Sha256HmacVerifyFinal;
  2488. }
  2489. else {
  2490. WOLFSSL_MSG("unsupported key type");
  2491. WOLFSSL_LEAVE("tsip_ShaXHmacVerify", BAD_FUNC_ARG);
  2492. return BAD_FUNC_ARG;
  2493. }
  2494. if ((ret = tsip_hw_lock()) != 0) {
  2495. WOLFSSL_MSG("hw lock failed\n");
  2496. WOLFSSL_LEAVE("tsip_ShaXHmacVerify", ret);
  2497. return ret;
  2498. }
  2499. wolfSSL_SetTlsHmacInner((WOLFSSL*)ssl, (byte*)myInner,
  2500. messageSz, content, 1);
  2501. ret = initFn(&handle, &wrapped_key);
  2502. if (ret == TSIP_SUCCESS) {
  2503. ret = updateFn(&handle, myInner, WOLFSSL_TLS_HMAC_INNER_SZ);
  2504. }
  2505. if (ret == TSIP_SUCCESS) {
  2506. ret = updateFn(&handle, (uint8_t*)message, (uint32_t)messageSz);
  2507. }
  2508. if (ret == TSIP_SUCCESS) {
  2509. ret = finalFn(&handle, (uint8_t*)(message + messageSz), (uint32_t)macSz);
  2510. }
  2511. if (ret != TSIP_SUCCESS) {
  2512. WOLFSSL_MSG("TSIP Mac verification failed");
  2513. }
  2514. /* unlock hw */
  2515. tsip_hw_unlock();
  2516. WOLFSSL_LEAVE("tsip_ShaXHmacVerify", ret);
  2517. return ret;
  2518. }
  2519. /* generate Verify Data based on master secret */
  2520. int wc_tsip_generateVerifyData(
  2521. const byte* ms, /* master secret */
  2522. const byte* side, /* 0:client-side 1:server-side */
  2523. const byte* handshake_hash,
  2524. byte* hashes) /* out */
  2525. {
  2526. int ret ;
  2527. uint32_t l_side = R_TSIP_TLS_GENERATE_CLIENT_VERIFY;
  2528. WOLFSSL_ENTER("tsip_generateVerifyData");
  2529. if ((ms == NULL) || (side == NULL) || (handshake_hash == NULL) ||
  2530. (hashes == NULL)) {
  2531. WOLFSSL_LEAVE("tsip_generateVerifyData", BAD_FUNC_ARG);
  2532. return BAD_FUNC_ARG;
  2533. }
  2534. if (XSTRNCMP((const char*)side, (const char*)kTlsServerFinStr,
  2535. FINISHED_LABEL_SZ) == 0) {
  2536. l_side = R_TSIP_TLS_GENERATE_SERVER_VERIFY;
  2537. }
  2538. if ((ret = tsip_hw_lock()) == 0) {
  2539. ret = R_TSIP_TlsGenerateVerifyData(l_side, (uint32_t*)ms,
  2540. (uint8_t*)handshake_hash, hashes/* out */);
  2541. if (ret != TSIP_SUCCESS) {
  2542. WOLFSSL_MSG("R_TSIP_TlsGenerateSessionKey failed");
  2543. }
  2544. }
  2545. /* unlock hw */
  2546. tsip_hw_unlock();
  2547. WOLFSSL_LEAVE("tsip_generateVerifyData", ret);
  2548. return ret;
  2549. }
  2550. /* generate keys for TLS communication */
  2551. int wc_tsip_generateSessionKey(
  2552. WOLFSSL *ssl,
  2553. TsipUserCtx* ctx,
  2554. int devId)
  2555. {
  2556. int ret;
  2557. Ciphers *enc;
  2558. Ciphers *dec;
  2559. tsip_hmac_sha_key_index_t key_client_mac;
  2560. tsip_hmac_sha_key_index_t key_server_mac;
  2561. tsip_aes_key_index_t key_client_aes;
  2562. tsip_aes_key_index_t key_server_aes;
  2563. WOLFSSL_ENTER("wc_tsip_generateSessionKey");
  2564. if (ssl== NULL)
  2565. return BAD_FUNC_ARG;
  2566. if ((ret = tsip_hw_lock()) == 0) {
  2567. #if (WOLFSSL_RENESAS_TSIP_VER>=109)
  2568. uint32_t tsipCS = GetTsipCipherSuite(ssl->options.cipherSuite0,
  2569. ssl->options.cipherSuite);
  2570. if (tsipCS == R_TSIP_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 ||
  2571. tsipCS == R_TSIP_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) {
  2572. WOLFSSL_MSG("Session key for AES-GCM generation skipped.");
  2573. /* Do not release ssl-> arrays to reference the master secret and
  2574. * randoms, as the AES GCM session key will be generated in
  2575. * encryption or description timing.
  2576. */
  2577. wolfSSL_KeepArrays(ssl);
  2578. ret = TSIP_SUCCESS;
  2579. }
  2580. else {
  2581. ret = R_TSIP_TlsGenerateSessionKey(
  2582. tsipCS,
  2583. (uint32_t*)ssl->arrays->tsip_masterSecret,
  2584. (uint8_t*) ssl->arrays->clientRandom,
  2585. (uint8_t*) ssl->arrays->serverRandom,
  2586. NULL, /* nonce is required only for AES-GCM key */
  2587. &key_client_mac,
  2588. &key_server_mac,
  2589. &key_client_aes,
  2590. &key_server_aes,
  2591. NULL, NULL);
  2592. }
  2593. #else /* WOLFSSL_RENESAS_TSIP_VER < 109 */
  2594. ret = R_TSIP_TlsGenerateSessionKey(
  2595. _tls2tsipdef(ssl->options.cipherSuite),
  2596. (uint32_t*)ssl->arrays->tsip_masterSecret,
  2597. (uint8_t*)ssl->arrays->clientRandom,
  2598. (uint8_t*)ssl->arrays->serverRandom,
  2599. &key_client_mac,
  2600. &key_server_mac,
  2601. &key_client_aes,
  2602. &key_server_aes,
  2603. NULL, NULL);
  2604. #endif
  2605. if (ret != TSIP_SUCCESS) {
  2606. WOLFSSL_MSG("R_TSIP_TlsGenerateSessionKey failed");
  2607. }
  2608. else {
  2609. /* succeeded creating session keys */
  2610. /* alloc aes instance for both enc and dec */
  2611. enc = &ssl->encrypt;
  2612. dec = &ssl->decrypt;
  2613. if (enc) {
  2614. if (enc->aes == NULL) {
  2615. enc->aes = (Aes*)XMALLOC(sizeof(Aes), ssl->heap,
  2616. DYNAMIC_TYPE_CIPHER);
  2617. if (enc->aes == NULL)
  2618. return MEMORY_E;
  2619. }
  2620. ForceZero(enc->aes, sizeof(Aes));
  2621. }
  2622. if (dec) {
  2623. if (dec->aes == NULL) {
  2624. dec->aes = (Aes*)XMALLOC(sizeof(Aes), ssl->heap,
  2625. DYNAMIC_TYPE_CIPHER);
  2626. if (dec->aes == NULL) {
  2627. if (enc) {
  2628. XFREE(enc->aes, NULL, DYNAMIC_TYPE_CIPHER);
  2629. }
  2630. return MEMORY_E;
  2631. }
  2632. }
  2633. ForceZero(dec->aes, sizeof(Aes));
  2634. }
  2635. /* copy key index into aes */
  2636. if (ssl->options.side == PROVISION_CLIENT) {
  2637. XMEMCPY(&enc->aes->ctx.tsip_keyIdx, &key_client_aes,
  2638. sizeof(key_client_aes));
  2639. XMEMCPY(&dec->aes->ctx.tsip_keyIdx, &key_server_aes,
  2640. sizeof(key_server_aes));
  2641. }
  2642. else {
  2643. XMEMCPY(&enc->aes->ctx.tsip_keyIdx, &key_server_aes,
  2644. sizeof(key_server_aes));
  2645. XMEMCPY(&dec->aes->ctx.tsip_keyIdx, &key_client_aes,
  2646. sizeof(key_client_aes));
  2647. }
  2648. /* copy hac key index into keys */
  2649. ssl->keys.tsip_client_write_MAC_secret = key_client_mac;
  2650. ssl->keys.tsip_server_write_MAC_secret = key_server_mac;
  2651. /* set up key size and marked ready */
  2652. if (enc) {
  2653. enc->aes->ctx.keySize = ssl->specs.key_size;
  2654. enc->aes->ctx.setup = 1;
  2655. /* ready for use */
  2656. enc->setup = 1;
  2657. }
  2658. /* set up key size and marked ready */
  2659. if (dec) {
  2660. dec->aes->ctx.keySize = ssl->specs.key_size;
  2661. dec->aes->ctx.setup = 1;
  2662. /* ready for use */
  2663. dec->setup = 1;
  2664. }
  2665. if (ctx->tsip_cipher ==
  2666. R_TSIP_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ||
  2667. ctx->tsip_cipher ==
  2668. R_TSIP_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
  2669. enc->aes->nonceSz = AEAD_MAX_IMP_SZ;
  2670. dec->aes->nonceSz = AEAD_MAX_IMP_SZ;
  2671. }
  2672. enc->aes->devId = devId;
  2673. dec->aes->devId = devId;
  2674. ctx->session_key_set = 1;
  2675. }
  2676. /* unlock hw */
  2677. tsip_hw_unlock();
  2678. }
  2679. else
  2680. WOLFSSL_MSG("hw lock failed");
  2681. WOLFSSL_LEAVE("wc_tsip_generateSessionKey", ret);
  2682. return ret;
  2683. }
  2684. /* generate Master secrete by TSIP */
  2685. #if (WOLFSSL_RENESAS_TSIP_VER>=109)
  2686. int wc_tsip_generateMasterSecretEx(
  2687. byte cipherSuiteFirst,
  2688. byte cipherSuite,
  2689. const byte *pr, /* pre-master */
  2690. const byte *cr, /* client random */
  2691. const byte *sr, /* server random */
  2692. byte *ms)
  2693. {
  2694. int ret;
  2695. WOLFSSL_ENTER("tsip_generateMasterSecretEx");
  2696. if ((pr == NULL) || (cr == NULL) || (sr == NULL) ||
  2697. (ms == NULL))
  2698. return BAD_FUNC_ARG;
  2699. uint32_t tsipCS = GetTsipCipherSuite(cipherSuiteFirst,cipherSuite);
  2700. if (tsipCS == 0xffffffff)
  2701. return BAD_FUNC_ARG;
  2702. if ((ret = tsip_hw_lock()) == 0) {
  2703. ret = R_TSIP_TlsGenerateMasterSecret(
  2704. tsipCS,
  2705. (uint32_t*)pr,
  2706. (uint8_t*)cr, (uint8_t*)sr, (uint32_t*)ms);
  2707. if (ret != TSIP_SUCCESS) {
  2708. WOLFSSL_MSG("R_TSIP_TlsGenerateMasterSecret failed");
  2709. }
  2710. /* unlock hw */
  2711. tsip_hw_unlock();
  2712. }
  2713. else {
  2714. WOLFSSL_MSG(" hw lock failed ");
  2715. }
  2716. WOLFSSL_LEAVE("tsip_generateMasterSecretEx", ret);
  2717. return ret;
  2718. }
  2719. #else /* WOLFSSL_RENESAS_TSIP_VER < 109 */
  2720. int wc_tsip_generateMasterSecret(
  2721. const byte* pr, /* pre-master */
  2722. const byte* cr, /* client random */
  2723. const byte* sr, /* server random */
  2724. byte* ms)
  2725. {
  2726. int ret;
  2727. WOLFSSL_ENTER("tsip_generateMasterSecret");
  2728. if ((pr == NULL) || (cr == NULL) || (sr == NULL) ||
  2729. (ms == NULL))
  2730. return BAD_FUNC_ARG;
  2731. if ((ret = tsip_hw_lock()) == 0) {
  2732. ret = R_TSIP_TlsGenerateMasterSecret(
  2733. (uint32_t*)pr,
  2734. (uint8_t*)cr,
  2735. (uint8_t*)sr,
  2736. (uint32_t*)ms);
  2737. if (ret != TSIP_SUCCESS) {
  2738. WOLFSSL_MSG("R_TSIP_TlsGenerateMasterSecret failed");
  2739. }
  2740. /* unlock hw */
  2741. tsip_hw_unlock();
  2742. }
  2743. else {
  2744. WOLFSSL_MSG(" hw lock failed ");
  2745. }
  2746. WOLFSSL_LEAVE("tsip_generateMasterSecret", ret);
  2747. return ret;
  2748. }
  2749. #endif /* WOLFSSL_RENESAS_TSIP_VER */
  2750. /* store elements for session key generation into ssl->keys.
  2751. * return 0 on success, negative value on failure
  2752. */
  2753. int wc_tsip_storeKeyCtx(WOLFSSL* ssl, TsipUserCtx* userCtx)
  2754. {
  2755. int ret = 0;
  2756. WOLFSSL_ENTER("tsip_storeKeyCtx");
  2757. if (ssl == NULL || userCtx == NULL)
  2758. ret = BAD_FUNC_ARG;
  2759. if (ret == 0) {
  2760. XMEMCPY(userCtx->tsip_masterSecret, ssl->arrays->tsip_masterSecret,
  2761. TSIP_TLS_MASTERSECRET_SIZE);
  2762. XMEMCPY(userCtx->tsip_clientRandom, ssl->arrays->clientRandom,
  2763. TSIP_TLS_CLIENTRANDOM_SZ);
  2764. XMEMCPY(userCtx->tsip_serverRandom, ssl->arrays->serverRandom,
  2765. TSIP_TLS_SERVERRANDOM_SZ);
  2766. userCtx->tsip_cipher = GetTsipCipherSuite(ssl->options.cipherSuite0,
  2767. ssl->options.cipherSuite);
  2768. }
  2769. WOLFSSL_LEAVE("tsip_storeKeyCtx", ret);
  2770. return ret;
  2771. }
  2772. /* generate pre-Master secrete by TSIP */
  2773. int wc_tsip_generatePremasterSecret(byte *premaster, word32 preSz)
  2774. {
  2775. WOLFSSL_ENTER("tsip_generatePremasterSecret");
  2776. int ret;
  2777. if (premaster == NULL)
  2778. return BAD_FUNC_ARG;
  2779. if ((ret = tsip_hw_lock()) == 0 && preSz >=
  2780. (R_TSIP_TLS_MASTER_SECRET_WORD_SIZE*4)) {
  2781. /* generate pre-master, 80 bytes */
  2782. ret = R_TSIP_TlsGeneratePreMasterSecret((uint32_t*)premaster);
  2783. if (ret != TSIP_SUCCESS) {
  2784. WOLFSSL_MSG(" R_TSIP_TlsGeneratePreMasterSecret failed");
  2785. }
  2786. /* unlock hw */
  2787. tsip_hw_unlock();
  2788. }
  2789. else {
  2790. WOLFSSL_MSG(" hw lock failed or preSz is smaller than 80");
  2791. }
  2792. WOLFSSL_LEAVE("tsip_generatePremasterSecret", ret);
  2793. return ret;
  2794. }
  2795. /*
  2796. * generate encrypted pre-Master secrete by TSIP
  2797. */
  2798. int wc_tsip_generateEncryptPreMasterSecret(
  2799. WOLFSSL* ssl,
  2800. byte* out,
  2801. word32* outSz)
  2802. {
  2803. int ret;
  2804. WOLFSSL_ENTER("tsip_generateEncryptPreMasterSecret");
  2805. if ((ssl == NULL) || (out == NULL) || (outSz == NULL))
  2806. return BAD_FUNC_ARG;
  2807. if ((ret = tsip_hw_lock()) == 0) {
  2808. if (*outSz >= 256)
  2809. #if (WOLFSSL_RENESAS_TSIP_VER>=109)
  2810. ret = R_TSIP_TlsEncryptPreMasterSecretWithRsa2048PublicKey(
  2811. (uint32_t*)ssl->peerSceTsipEncRsaKeyIndex,
  2812. (uint32_t*)ssl->arrays->preMasterSecret,
  2813. (uint8_t*)out);
  2814. #else
  2815. ret = R_TSIP_TlsEncryptPreMasterSecret(
  2816. (uint32_t*)ssl->peerSceTsipEncRsaKeyIndex,
  2817. (uint32_t*)ssl->arrays->preMasterSecret,
  2818. (uint8_t*)out);
  2819. #endif
  2820. else
  2821. ret = -1;
  2822. if (ret != TSIP_SUCCESS) {
  2823. WOLFSSL_MSG(" R_TSIP_TlsEncryptPreMasterSecret failed");
  2824. }
  2825. else {
  2826. *outSz = 256; /* TSIP can only handles 2048 RSA */
  2827. void* ctx = wolfSSL_GetRsaVerifyCtx(ssl);
  2828. wolfSSL_CTX_SetGenMasterSecretCb(ssl->ctx,
  2829. Renesas_cmn_genMasterSecret);
  2830. wolfSSL_SetGenMasterSecretCtx(ssl, ctx);
  2831. }
  2832. tsip_hw_unlock();
  2833. }
  2834. else {
  2835. WOLFSSL_MSG(" hw lock failed ");
  2836. }
  2837. WOLFSSL_LEAVE("tsip_generateEncryptPreMasterSecret", ret);
  2838. return ret;
  2839. }
  2840. /* Certificate verification by TSIP */
  2841. int wc_tsip_tls_CertVerify(
  2842. const uint8_t* cert, uint32_t certSz,
  2843. const uint8_t* signature, uint32_t sigSz,
  2844. uint32_t key_n_start, uint32_t key_n_len,
  2845. uint32_t key_e_start, uint32_t key_e_len,
  2846. uint8_t* tsip_encRsaKeyIndex)
  2847. {
  2848. int ret;
  2849. uint8_t *sigforSCE = NULL;
  2850. uint8_t *pSig = NULL;
  2851. const byte rs_size = 0x20;
  2852. byte offset = 0x3;
  2853. WOLFSSL_ENTER("wc_tsip_tls_CertVerify");
  2854. if (cert == NULL)
  2855. return BAD_FUNC_ARG;
  2856. if (!signature) {
  2857. WOLFSSL_MSG(" signature for ca verification is not set");
  2858. return -1;
  2859. }
  2860. if (!tsip_encRsaKeyIndex) {
  2861. WOLFSSL_MSG(" tsip_encRsaKeyIndex is NULL.");
  2862. return -1;
  2863. }
  2864. /* Public key type: Prime256r1 */
  2865. if (g_user_key_info.encrypted_user_tls_key_type ==
  2866. R_TSIP_TLS_PUBLIC_KEY_TYPE_ECDSA_P256) {
  2867. if ((sigforSCE = (uint8_t*)XMALLOC(R_TSIP_ECDSA_DATA_BYTE_SIZE,
  2868. NULL, DYNAMIC_TYPE_ECC)) == NULL) {
  2869. WOLFSSL_MSG("failed to malloc memory");
  2870. return MEMORY_E;
  2871. }
  2872. /* initialization */
  2873. ForceZero(sigforSCE, R_TSIP_ECDSA_DATA_BYTE_SIZE);
  2874. if (signature[offset] == 0x20) {
  2875. XMEMCPY(sigforSCE, &signature[offset+1], rs_size);
  2876. offset = 0x25;
  2877. if (signature[offset] == 0x20) {
  2878. XMEMCPY(&sigforSCE[rs_size], &signature[offset+1], rs_size);
  2879. }
  2880. else {
  2881. XMEMCPY(&sigforSCE[rs_size], &signature[offset+2], rs_size);
  2882. }
  2883. }
  2884. else {
  2885. XMEMCPY(sigforSCE, &signature[offset+2], rs_size);
  2886. offset = 0x26;
  2887. if (signature[offset] == rs_size) {
  2888. XMEMCPY(&sigforSCE[rs_size], &signature[offset+1], rs_size);
  2889. }
  2890. else {
  2891. XMEMCPY(&sigforSCE[rs_size], &signature[offset+2], rs_size);
  2892. }
  2893. }
  2894. pSig = sigforSCE;
  2895. }
  2896. /* Public key type: RSA 2048bit */
  2897. else {
  2898. pSig = (uint8_t*)signature;
  2899. }
  2900. if ((ret = tsip_hw_lock()) == 0) {
  2901. #if (WOLFSSL_RENESAS_TSIP_VER>=109)
  2902. ret = R_TSIP_TlsCertificateVerification(
  2903. g_user_key_info.encrypted_user_tls_key_type,
  2904. (uint32_t*)g_encrypted_publicCA_key,/* encrypted public key */
  2905. (uint8_t*)cert, /* certificate der */
  2906. certSz, /* length of der */
  2907. (uint8_t*)pSig, /* sign data by RSA PSS */
  2908. key_n_start, /* start position of public key n in bytes */
  2909. (key_n_start + key_n_len), /* length of the public key n */
  2910. key_e_start, /* start pos, key e in bytes */
  2911. (key_e_start + key_e_len), /* length of the public key e */
  2912. (uint32_t*)tsip_encRsaKeyIndex /* returned encrypted key */
  2913. );
  2914. #elif (WOLFSSL_RENESAS_TSIP_VER>=106)
  2915. ret = R_TSIP_TlsCertificateVerification(
  2916. (uint32_t*)g_encrypted_publicCA_key,/* encrypted public key */
  2917. (uint8_t*)cert, /* certificate der */
  2918. certSz, /* length of der */
  2919. (uint8_t*)pSig, /* sign data by RSA PSS */
  2920. key_n_start, /* start position of public key n in bytes */
  2921. (key_n_start + key_n_len), /* length of the public key n */
  2922. key_e_start, /* start pos, key e in bytes */
  2923. (key_e_start + key_e_len), /* length of the public key e */
  2924. (uint32_t*)tsip_encRsaKeyIndex /* returned encrypted key */
  2925. );
  2926. #endif
  2927. if (ret != TSIP_SUCCESS) {
  2928. WOLFSSL_MSG(" R_TSIP_TlsCertificateVerification failed");
  2929. }
  2930. if (sigforSCE) {
  2931. XFREE(sigforSCE, NULL, DYNAMIC_TYPE_ECC);
  2932. }
  2933. tsip_hw_unlock();
  2934. }
  2935. else {
  2936. WOLFSSL_MSG(" hw lock failed ");
  2937. }
  2938. WOLFSSL_LEAVE("wc_tsip_tls_CertVerify", ret);
  2939. return ret;
  2940. }
  2941. /* Root Certificate verification */
  2942. int wc_tsip_tls_RootCertVerify(
  2943. const byte* cert, word32 cert_len,
  2944. word32 key_n_start, word32 key_n_len,
  2945. word32 key_e_start, word32 key_e_len,
  2946. word32 cm_row)
  2947. {
  2948. int ret;
  2949. /* call to generate encrypted public key for certificate verification */
  2950. uint8_t *signature = (uint8_t*)ca_cert_sig;
  2951. WOLFSSL_ENTER("wc_tsip_tls_RootCertVerify");
  2952. if (cert == NULL)
  2953. return BAD_FUNC_ARG;
  2954. if (!signature) {
  2955. WOLFSSL_MSG(" signature for ca verification is not set");
  2956. return -1;
  2957. }
  2958. if ((ret = tsip_hw_lock()) == 0) {
  2959. #if (WOLFSSL_RENESAS_TSIP_VER>=109)
  2960. ret = R_TSIP_TlsRootCertificateVerification(
  2961. g_user_key_info.encrypted_user_tls_key_type,
  2962. (uint8_t*)cert, /* CA cert */
  2963. (uint32_t)cert_len, /* length of CA cert */
  2964. key_n_start, /* Byte position of public key */
  2965. (key_n_start + key_n_len),
  2966. key_e_start,
  2967. (key_e_start + key_e_len),
  2968. (uint8_t*)ca_cert_sig, /* RSA 2048 PSS with SHA256 */
  2969. g_encrypted_publicCA_key /* RSA-2048 public key 560 bytes */
  2970. );
  2971. #else /* WOLFSSL_RENESAS_TSIP_VER < 109 */
  2972. ret = R_TSIP_TlsRootCertificateVerification(
  2973. (uint8_t*)cert,/* CA cert */
  2974. (uint32_t)cert_len,/* length of CA cert */
  2975. key_n_start, /* Byte position of public key */
  2976. (key_n_start + key_n_len),
  2977. key_e_start,
  2978. (key_e_start + key_e_len),
  2979. (uint8_t*)ca_cert_sig,/* "RSA 2048 PSS with SHA256" */
  2980. /* RSA-2048 public key used by RSA-2048 PSS with SHA256. 560 Bytes */
  2981. g_encrypted_publicCA_key
  2982. );
  2983. #endif
  2984. if (ret != TSIP_SUCCESS) {
  2985. WOLFSSL_MSG(" R_TSIP_TlsRootCertificateVerification failed");
  2986. }
  2987. else {
  2988. g_CAscm_Idx = cm_row;
  2989. }
  2990. tsip_hw_unlock();
  2991. }
  2992. else {
  2993. WOLFSSL_MSG(" hw lock failed ");
  2994. }
  2995. WOLFSSL_LEAVE("wc_tsip_tls_RootCertVerify", ret);
  2996. return ret;
  2997. }
  2998. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  2999. #if !defined(NO_RSA)
  3000. /* Perform signing with the client's RSA private key on hash value of messages
  3001. * exchanged with server.
  3002. *
  3003. * parameters
  3004. * info->pk.rsa.in : not used
  3005. * info->pk.rsa.inlen : not used
  3006. * info->pk.rsa.out : the buffer where the signature data is output to
  3007. * info->pk.rsa.outlen: the length of the pk.rsa.out
  3008. * tuc: the pointer to the TsipUserCtx structure
  3009. * returns
  3010. * 0 on success, CRYPTOCB_UNAVAILABLE on unsupported key type specified.
  3011. *
  3012. */
  3013. WOLFSSL_LOCAL int tsip_SignRsaPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
  3014. {
  3015. int ret = 0;
  3016. e_tsip_err_t err = TSIP_SUCCESS;
  3017. tsip_rsa_byte_data_t hashData, sigData;
  3018. WOLFSSL* ssl = NULL;
  3019. uint8_t tsip_hash_type;
  3020. WOLFSSL_ENTER("tsip_SignRsaPkcs");
  3021. if (info == NULL || tuc == NULL || tuc->ssl == NULL) {
  3022. ret = BAD_FUNC_ARG;
  3023. }
  3024. if (ret == 0) {
  3025. ssl = tuc->ssl;
  3026. if (ssl->version.major == SSLv3_MAJOR &&
  3027. ssl->version.minor == TLSv1_3_MINOR) {
  3028. ret = CRYPTOCB_UNAVAILABLE;
  3029. }
  3030. }
  3031. if (ret == 0) {
  3032. /* import private key_index from wrapped key */
  3033. ret = tsipImportPrivateKey(tuc, tuc->wrappedKeyType);
  3034. }
  3035. if (ret == 0) {
  3036. if (ssl->options.hashAlgo == md5_mac)
  3037. tsip_hash_type = R_TSIP_RSA_HASH_MD5;
  3038. else if (ssl->options.hashAlgo == sha_mac)
  3039. tsip_hash_type = R_TSIP_RSA_HASH_SHA1;
  3040. else if (ssl->options.hashAlgo == sha256_mac)
  3041. tsip_hash_type = R_TSIP_RSA_HASH_SHA256;
  3042. else
  3043. ret = CRYPTOCB_UNAVAILABLE;
  3044. }
  3045. if (ret == 0) {
  3046. hashData.pdata = (uint8_t*)ssl->buffers.digest.buffer;
  3047. hashData.data_type = 1;
  3048. sigData.pdata = (uint8_t*)info->pk.rsa.in;
  3049. sigData.data_length = 0; /* signature size will be returned here */
  3050. if ((ret = tsip_hw_lock()) == 0) {
  3051. switch (tuc->wrappedKeyType) {
  3052. case TSIP_KEY_TYPE_RSA2048:
  3053. err = R_TSIP_RsassaPkcs2048SignatureGenerate(
  3054. &hashData, &sigData,
  3055. &tuc->Rsa2048PrivateKeyIdx,
  3056. tsip_hash_type);
  3057. if (err != TSIP_SUCCESS) {
  3058. ret = WC_HW_E;
  3059. }
  3060. break;
  3061. case TSIP_KEY_TYPE_RSA4096:
  3062. ret = CRYPTOCB_UNAVAILABLE;
  3063. break;
  3064. default:
  3065. WOLFSSL_MSG("wrapped private key is not supported");
  3066. ret = CRYPTOCB_UNAVAILABLE;
  3067. break;
  3068. }
  3069. tsip_hw_unlock();
  3070. }
  3071. else {
  3072. WOLFSSL_MSG("mutex locking error");
  3073. }
  3074. }
  3075. WOLFSSL_LEAVE("tsip_SignRsaPkcs", ret);
  3076. return ret;
  3077. }
  3078. #endif /* !NO_RSA */
  3079. #if !defined(NO_RSA)
  3080. WOLFSSL_LOCAL int tsip_VerifyRsaPkcsCb(
  3081. WOLFSSL* ssl,
  3082. unsigned char* sig, unsigned int sigSz,
  3083. unsigned char** out,
  3084. const unsigned char* keyDer, unsigned int keySz,
  3085. void* ctx)
  3086. {
  3087. int ret = 0;
  3088. e_tsip_err_t err = TSIP_SUCCESS;
  3089. tsip_rsa_byte_data_t hashData, sigData;
  3090. TsipUserCtx* tuc = NULL;
  3091. uint8_t tsip_hash_type;
  3092. (void)keyDer;
  3093. (void)keySz;
  3094. WOLFSSL_ENTER("tsip_VerifyRsaPkcsCb");
  3095. if (sig == NULL || out == NULL || ctx == NULL) {
  3096. ret = BAD_FUNC_ARG;
  3097. }
  3098. if (ssl->version.major == SSLv3_MAJOR &&
  3099. ssl->version.minor == TLSv1_3_MINOR) {
  3100. ret = CRYPTOCB_UNAVAILABLE;
  3101. }
  3102. if (ret == 0) {
  3103. tuc = (TsipUserCtx*)ctx;
  3104. if (tuc == NULL)
  3105. ret = CRYPTOCB_UNAVAILABLE;
  3106. }
  3107. if (ret == 0) {
  3108. /* import public key_index from wrapped key */
  3109. ret = tsipImportPublicKey(tuc, tuc->wrappedKeyType);
  3110. }
  3111. if (ret == 0) {
  3112. if (ssl->options.hashAlgo == md5_mac)
  3113. tsip_hash_type = R_TSIP_RSA_HASH_MD5;
  3114. else if (ssl->options.hashAlgo == sha_mac)
  3115. tsip_hash_type = R_TSIP_RSA_HASH_SHA1;
  3116. else if (ssl->options.hashAlgo == sha256_mac)
  3117. tsip_hash_type = R_TSIP_RSA_HASH_SHA256;
  3118. else {
  3119. ret = CRYPTOCB_UNAVAILABLE;
  3120. }
  3121. }
  3122. if (ret == 0) {
  3123. sigData.pdata = (uint8_t*)sig;
  3124. hashData.pdata = (uint8_t*)ssl->buffers.digest.buffer;
  3125. hashData.data_type = 1; /* hash value */
  3126. if ((ret = tsip_hw_lock()) == 0) {
  3127. switch (tuc->wrappedKeyType) {
  3128. case TSIP_KEY_TYPE_RSA2048:
  3129. sigData.data_length = 256;
  3130. err = R_TSIP_RsassaPkcs2048SignatureVerification(
  3131. &sigData, &hashData,
  3132. &tuc->Rsa2048PublicKeyIdx,
  3133. tsip_hash_type);
  3134. if (err == TSIP_ERR_AUTHENTICATION) {
  3135. ret = VERIFY_CERT_ERROR;
  3136. }
  3137. else if (err == TSIP_SUCCESS) {
  3138. ret = 0;
  3139. }
  3140. else {
  3141. ret = WC_HW_E;
  3142. }
  3143. break;
  3144. case TSIP_KEY_TYPE_RSA4096:
  3145. ret = CRYPTOCB_UNAVAILABLE;
  3146. break;
  3147. default:
  3148. WOLFSSL_MSG("wrapped private key is not supported");
  3149. ret = CRYPTOCB_UNAVAILABLE;
  3150. break;
  3151. }
  3152. tsip_hw_unlock();
  3153. }
  3154. else {
  3155. WOLFSSL_MSG("mutex locking error");
  3156. }
  3157. }
  3158. WOLFSSL_LEAVE("tsip_VerifyRsaPkcsCb", ret);
  3159. return ret;
  3160. }
  3161. #endif /* !NO_RSA */
  3162. #if defined(HAVE_ECC)
  3163. /* Perform signing with the client's ECC private key on hash value of messages
  3164. * exchanged with server.
  3165. *
  3166. * parameters
  3167. * info->pk.eccsign.in : the buffer holding hash value of messages
  3168. * info->pk.eccsign.inlen : hash data size
  3169. * info->pk.eccsign.out : the buffer where the signature data is output to
  3170. * info->pk.eccsign.outlen: the length of the buffer pk.eccsign.out
  3171. * tuc: the pointer to the TsipUserCtx structure
  3172. * returns
  3173. * 0 on success, CRYPTOCB_UNAVAILABLE on unsupported key type specified.
  3174. * note
  3175. * signature will be DER encoded and stored into out buffer.
  3176. * the private key must be imported as TSIP specific format.
  3177. */
  3178. WOLFSSL_LOCAL int tsip_SignEcdsa(wc_CryptoInfo* info, TsipUserCtx* tuc)
  3179. {
  3180. int ret = 0;
  3181. e_tsip_err_t err = TSIP_SUCCESS;
  3182. tsip_rsa_byte_data_t hashData, sigData;
  3183. byte offsetForWork;
  3184. byte* out = NULL;
  3185. byte* sig = NULL;
  3186. int rSz = 0;
  3187. int sSz = 0;
  3188. int idx = 0;
  3189. int sz = 0;
  3190. WOLFSSL* ssl = NULL;
  3191. WOLFSSL_ENTER("tsip_SignEcdsa");
  3192. if (info == NULL || tuc == NULL) {
  3193. ret = CRYPTOCB_UNAVAILABLE;
  3194. }
  3195. if (ret == 0) {
  3196. ssl = tuc->ssl;
  3197. if (ssl->version.major == SSLv3_MAJOR &&
  3198. ssl->version.minor == TLSv1_3_MINOR) {
  3199. ret = CRYPTOCB_UNAVAILABLE;
  3200. }
  3201. }
  3202. if (ret == 0) {
  3203. /* import private key_index from wrapped key */
  3204. ret = tsipImportPrivateKey(tuc, tuc->wrappedKeyType);
  3205. }
  3206. if (ret == 0) {
  3207. hashData.pdata = (uint8_t*)info->pk.eccsign.in;
  3208. hashData.data_type = 1;
  3209. sigData.pdata = (uint8_t*)info->pk.eccsign.out;
  3210. sigData.data_length = 0; /* signature size will be returned here */
  3211. if ((ret = tsip_hw_lock()) == 0) {
  3212. switch (tuc->wrappedKeyType) {
  3213. #if defined(HAVE_ECC)
  3214. case TSIP_KEY_TYPE_ECDSAP256:
  3215. offsetForWork = R_TSIP_ECDSA_DATA_BYTE_SIZE + 32;
  3216. if (*(info->pk.eccsign.outlen) <
  3217. R_TSIP_ECDSA_DATA_BYTE_SIZE + offsetForWork) {
  3218. ret = BUFFER_E;
  3219. break;
  3220. }
  3221. sigData.pdata = (uint8_t*)info->pk.eccsign.out +
  3222. offsetForWork;
  3223. err = R_TSIP_EcdsaP256SignatureGenerate(
  3224. &hashData, &sigData,
  3225. &tuc->EcdsaP256PrivateKeyIdx);
  3226. if (err != TSIP_SUCCESS) {
  3227. ret = WC_HW_E;
  3228. break;
  3229. }
  3230. out = info->pk.eccsign.out;
  3231. sig = sigData.pdata;
  3232. rSz = sSz = R_TSIP_ECDSA_DATA_BYTE_SIZE / 2;
  3233. rSz += (sig[0] & 0x80)?1:0;
  3234. sSz += (sig[sSz] & 0x80)?1:0;
  3235. sz = (ASN_TAG_SZ + 1) * 2 + rSz + sSz;
  3236. /* encode ASN sequence */
  3237. out[idx++] = ASN_SEQUENCE | ASN_CONSTRUCTED;
  3238. out[idx++] = sz;
  3239. /* copy r part */
  3240. out[idx++] = ASN_INTEGER;
  3241. out[idx++] = rSz;
  3242. if (rSz > R_TSIP_ECDSA_DATA_BYTE_SIZE / 2)
  3243. out[idx++] = 0x00;
  3244. XMEMCPY(&out[idx], sig, R_TSIP_ECDSA_DATA_BYTE_SIZE / 2);
  3245. idx += R_TSIP_ECDSA_DATA_BYTE_SIZE / 2;
  3246. sig += R_TSIP_ECDSA_DATA_BYTE_SIZE / 2;
  3247. /* copy s part */
  3248. out[idx++] = ASN_INTEGER;
  3249. out[idx++] = sSz;
  3250. if (sSz > R_TSIP_ECDSA_DATA_BYTE_SIZE / 2)
  3251. out[idx++] = 0x00;
  3252. XMEMCPY(&out[idx], sig, R_TSIP_ECDSA_DATA_BYTE_SIZE / 2);
  3253. /* out size */
  3254. *(info->pk.eccsign.outlen) = ASN_TAG_SZ + 1 + sz;
  3255. break;
  3256. #endif
  3257. #if defined(HAVE_ECC192)
  3258. case TSIP_KEY_TYPE_ECDSAP192:
  3259. ret = CRYPTOCB_UNAVAILABLE;
  3260. break;
  3261. #endif
  3262. #if defined(HAVE_ECC224)
  3263. case TSIP_KEY_TYPE_ECDSAP224:
  3264. ret = CRYPTOCB_UNAVAILABLE;
  3265. break;
  3266. #endif
  3267. #if defined(HAVE_ECC384)
  3268. case TSIP_KEY_TYPE_ECDSAP384:
  3269. ret = CRYPTOCB_UNAVAILABLE;
  3270. break;
  3271. #endif
  3272. default:
  3273. WOLFSSL_MSG("wrapped private key is not supported");
  3274. ret = CRYPTOCB_UNAVAILABLE;
  3275. break;
  3276. }
  3277. tsip_hw_unlock();
  3278. }
  3279. else {
  3280. WOLFSSL_MSG("mutex locking error");
  3281. }
  3282. }
  3283. WOLFSSL_LEAVE("tsip_SignEcdsa", ret);
  3284. return ret;
  3285. }
  3286. #endif /* HAVE_ECC */
  3287. #ifdef WOLFSSL_RENESAS_TSIP_CRYPT_DEBUG
  3288. /* err
  3289. * e_tsip_err
  3290. TSIP_SUCCESS = 0,
  3291. TSIP_ERR_SELF_CHECK1, // Self-check 1 fail or TSIP function internal err.
  3292. TSIP_ERR_RESOURCE_CONFLICT, // A resource conflict occurred.
  3293. TSIP_ERR_SELF_CHECK2, // Self-check 2 fail.
  3294. TSIP_ERR_KEY_SET, // setting the invalid key.
  3295. TSIP_ERR_AUTHENTICATION, // Authentication failed.
  3296. TSIP_ERR_CALLBACK_UNREGIST, // Callback function is not registered.
  3297. TSIP_ERR_PARAMETER, // Illegal Input data.
  3298. TSIP_ERR_PROHIBIT_FUNCTION, // An invalid function call occurred.
  3299. * TSIP_RESUME_FIRMWARE_GENERATE_MAC,
  3300. // There is a continuation of R_TSIP_GenerateFirmwareMAC.
  3301. */
  3302. static void hexdump(const uint8_t* in, uint32_t len)
  3303. {
  3304. uint32_t i;
  3305. if (in == NULL)
  3306. return;
  3307. for (i = 0; i <= len;i++, in++) {
  3308. printf("%02x:", *in);
  3309. if (((i+1)%16)==0) {
  3310. printf("\n");
  3311. }
  3312. }
  3313. printf("\n");
  3314. }
  3315. byte *ret2err(word32 ret)
  3316. {
  3317. switch(ret) {
  3318. case TSIP_SUCCESS: return "success";
  3319. case TSIP_ERR_SELF_CHECK1: return "selfcheck1";
  3320. case TSIP_ERR_RESOURCE_CONFLICT: return "rsconflict";
  3321. case TSIP_ERR_SELF_CHECK2: return "selfcheck2";
  3322. case TSIP_ERR_KEY_SET: return "keyset";
  3323. case TSIP_ERR_AUTHENTICATION: return "authentication";
  3324. case TSIP_ERR_CALLBACK_UNREGIST: return "callback unreg";
  3325. case TSIP_ERR_PARAMETER: return "badarg";
  3326. case TSIP_ERR_PROHIBIT_FUNCTION: return "prohibitfunc";
  3327. case TSIP_RESUME_FIRMWARE_GENERATE_MAC: return "conti-generate-mac";
  3328. default:return "unknown";
  3329. }
  3330. }
  3331. #endif /* WOLFSSL_RENESAS_TSIP_CRYPT_DEBUG */
  3332. #endif /* WOLFSSL_RENESAS_TSIP */