renesas_sce_util.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /* renesas_sce_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/types.h>
  22. #if defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  23. defined(WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY)
  24. #include <wolfssl/wolfcrypt/wc_port.h>
  25. #include <wolfssl/wolfcrypt/error-crypt.h>
  26. #include <wolfssl/wolfcrypt/port/Renesas/renesas-sce-crypt.h>
  27. #include <wolfssl/wolfcrypt/port/Renesas/renesas_cmn.h>
  28. #include <wolfssl/wolfcrypt/memory.h>
  29. #include <wolfssl/wolfcrypt/error-crypt.h>
  30. #include <wolfssl/wolfcrypt/aes.h>
  31. #include <wolfssl/ssl.h>
  32. #include <wolfssl/internal.h>
  33. #include <stdio.h>
  34. #if defined(DEBUG_PK_CB)
  35. #define WOLFSSL_PKMSG(_f_, ...) printf(_f_, ##__VA_ARGS__)
  36. #else
  37. #define WOLFSSL_PKMSG(_f_, ...) WC_DO_NOTHING
  38. #endif
  39. #if defined(WOLFSSL_RENESAS_SCEPROTECT_ECC)
  40. WOLFSSL_GLOBAL SCE_PKCbInfo gSCE_PKCbInfo;
  41. #endif
  42. /* expect to have these variables defined at user application */
  43. extern sce_instance_ctrl_t sce_ctrl;
  44. extern sce_cfg_t sce_cfg;
  45. static const byte* ca_cert_sig;
  46. static sce_key_data g_user_key_info;
  47. static uint32_t g_encrypted_publicCA_key[HW_SCE_SINST_WORD_SIZE];
  48. extern uint32_t g_CAscm_Idx; /* index of CM table */
  49. wolfSSL_Mutex sce_mutex;
  50. static int sce_CryptHwMutexInit_ = 0;
  51. static uint32_t sce_sess_idx = 0;
  52. /* Mutex Init */
  53. static int sce_CryptHwMutexInit(wolfSSL_Mutex* mutex)
  54. {
  55. return wc_InitMutex(mutex);
  56. }
  57. /* Mutex Lock */
  58. static int sce_CryptHwMutexLock(wolfSSL_Mutex* mutex)
  59. {
  60. return wc_LockMutex(mutex);
  61. }
  62. /* Mutex Unlock */
  63. static int sce_CryptHwMutexUnLock(wolfSSL_Mutex* mutex)
  64. {
  65. return wc_UnLockMutex(mutex);
  66. }
  67. /*
  68. * lock hw engine
  69. * this should be called before using engine.
  70. */
  71. WOLFSSL_LOCAL int wc_sce_hw_lock()
  72. {
  73. int ret = 0;
  74. if (sce_CryptHwMutexInit_ == 0) {
  75. ret = sce_CryptHwMutexInit(&sce_mutex);
  76. if (ret == 0) {
  77. sce_CryptHwMutexInit_ = 1;
  78. }
  79. else {
  80. WOLFSSL_MSG(" mutex initialization failed.");
  81. return -1;
  82. }
  83. }
  84. if (sce_CryptHwMutexLock(&sce_mutex) != 0) {
  85. /* this should not happens */
  86. return -1;
  87. }
  88. return ret;
  89. }
  90. /*
  91. * release hw engine
  92. */
  93. WOLFSSL_LOCAL void wc_sce_hw_unlock(void)
  94. {
  95. sce_CryptHwMutexUnLock(&sce_mutex);
  96. }
  97. /* Open sce driver for use */
  98. WOLFSSL_LOCAL int wc_sce_Open()
  99. {
  100. WOLFSSL_ENTER("wc_sce_Open");
  101. int ret;
  102. if ((ret = wc_sce_hw_lock()) == 0) {
  103. ret = R_SCE_Open(&sce_ctrl, &sce_cfg);
  104. if (ret != FSP_SUCCESS) {
  105. WOLFSSL_MSG("RENESAS SCE Open failed");
  106. }
  107. if (ret == FSP_SUCCESS && g_user_key_info.encrypted_user_tls_key) {
  108. ret = R_SCE_TLS_RootCertificateRSA2048PublicKeyInstall(
  109. g_user_key_info.encrypted_provisioning_key,
  110. g_user_key_info.iv,
  111. g_user_key_info.encrypted_user_tls_key,
  112. &g_user_key_info.user_rsa2048_tls_wrappedkey); /* OUT */
  113. /* init vars */
  114. g_CAscm_Idx = (uint32_t)-1;
  115. }
  116. else {
  117. WOLFSSL_MSG("Failed to lock sce hw ");
  118. }
  119. }
  120. /* unlock hw */
  121. wc_sce_hw_unlock();
  122. WOLFSSL_LEAVE("wc_sce_Open", ret);
  123. return ret;
  124. }
  125. /* close SCE driver */
  126. WOLFSSL_LOCAL void wc_sce_Close()
  127. {
  128. WOLFSSL_ENTER("sce Close");
  129. int ret;
  130. if ((ret = wc_sce_hw_lock()) == 0) {
  131. /* close SCE */
  132. ret = R_SCE_Close(&sce_ctrl);
  133. /* unlock hw */
  134. wc_sce_hw_unlock();
  135. if (ret != FSP_SUCCESS) {
  136. WOLFSSL_MSG("RENESAS SCE Close failed");
  137. }
  138. }
  139. else {
  140. WOLFSSL_MSG("Failed to unlock sce hw ");
  141. }
  142. }
  143. #ifndef WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY
  144. #if defined(WOLFSSL_RENESAS_SCEPROTECT_ECC)
  145. /* Verify Server Key Exchange while doing ECDH key exchange */
  146. static int SCE_ServerKeyExVerify(uint32_t type, WOLFSSL* ssl, const uint8_t* sig,
  147. uint32_t sigSz, void* ctx)
  148. {
  149. int ret = WOLFSSL_FAILURE;
  150. User_SCEPKCbInfo* cbInfo;
  151. byte qx[MAX_ECC_BYTES], qy[MAX_ECC_BYTES];
  152. byte *peerkey = NULL;
  153. word32 qxLen = sizeof(qx), qyLen = sizeof(qy);
  154. (void) sigSz;
  155. /* sanity check */
  156. if (ssl == NULL || sig == NULL || ctx == NULL)
  157. return ret;
  158. cbInfo = (User_SCEPKCbInfo*)ctx;
  159. /* export public peer public key */
  160. ret = wc_ecc_export_public_raw(ssl->peerEccKey, qx, &qxLen, qy, &qyLen);
  161. WOLFSSL_PKMSG("qxLen %d qyLen %d\n", qxLen, qyLen);
  162. if (ret != 0) {
  163. WOLFSSL_MSG("failed to export peer ecc key");
  164. return ret;
  165. }
  166. /* make peer ecc key data for SCE */
  167. /* 0padding(24bit) || 04(8bit) || Qx(256bit) || Qy(256bit) */
  168. peerkey = (byte*)XMALLOC((3 + 1 + qxLen + qyLen), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  169. if (peerkey == NULL) {
  170. WOLFSSL_MSG("failed to malloc ecc key");
  171. return WOLFSSL_FAILURE;
  172. }
  173. XMEMSET(peerkey, 0, (3 + 1 + qxLen + qyLen));
  174. peerkey[3] = ECC_POINT_UNCOMP;
  175. XMEMCPY(&peerkey[4], qx, qxLen);
  176. XMEMCPY(&peerkey[4+qxLen], qy, qyLen);
  177. if ((ret = wc_sce_hw_lock()) == 0) {
  178. /* 0 : RSA 2048bit, 1 : Reserved, 2 : ECDSA P-256 */
  179. ret = R_SCE_TLS_ServerKeyExchangeVerify(
  180. type,
  181. (uint8_t*) ssl->arrays->clientRandom,
  182. (uint8_t*) ssl->arrays->serverRandom,
  183. (uint8_t*) peerkey,
  184. (uint8_t*) sig,
  185. (uint32_t*)ssl->peerSceTsipEncRsaKeyIndex,
  186. (uint32_t*)cbInfo->encrypted_ephemeral_ecdh_public_key);
  187. if (ret != FSP_SUCCESS) {
  188. WOLFSSL_MSG("failed R_SCE_TLS_ServerKeyExchangeVerify");
  189. cbInfo->keyflgs_tls.bits.pk_key_set = 0;
  190. }
  191. else {
  192. ret = WOLFSSL_SUCCESS;
  193. cbInfo->keyflgs_tls.bits.pk_key_set = 1;
  194. }
  195. }
  196. else {
  197. WOLFSSL_MSG("Failed to lock sce hw ");
  198. }
  199. /* unlock hw */
  200. wc_sce_hw_unlock();
  201. XFREE(peerkey, 0, DYNAMIC_TYPE_TMP_BUFFER);
  202. return ret;
  203. }
  204. /* Callback for Rsa Verify */
  205. WOLFSSL_LOCAL int wc_SCE_RsaVerify(WOLFSSL* ssl, byte* sig, uint32_t sigSz,
  206. uint8_t** out, const byte* key, uint32_t keySz, void* ctx)
  207. {
  208. int ret = WOLFSSL_FAILURE;
  209. (void) out;
  210. (void) key;
  211. (void) keySz;
  212. WOLFSSL_PKMSG("SCE RSA Verify: sigSz %d, keySz %d\n", sigSz, keySz);
  213. if (wc_sce_usable(ssl, 0))
  214. ret = SCE_ServerKeyExVerify(0, ssl, sig, sigSz, ctx);
  215. else
  216. ret = CRYPTOCB_UNAVAILABLE;
  217. if (ret == WOLFSSL_SUCCESS)
  218. ret = 0; /* 0 for success */
  219. WOLFSSL_PKMSG("SCE RSA Verify: ret %d\n", ret);
  220. return ret;
  221. }
  222. /* Callback for Ecc Verify */
  223. WOLFSSL_LOCAL int wc_SCE_EccVerify(WOLFSSL* ssl, const uint8_t* sig,
  224. uint32_t sigSz, const uint8_t* hash, uint32_t hashSz,
  225. const uint8_t* key, uint32_t keySz, int* result, void* ctx)
  226. {
  227. int ret = WOLFSSL_FAILURE;
  228. uint8_t *sigforSCE;
  229. const byte rs_size = HW_SCE_ECDSA_DATA_BYTE_SIZE/2;
  230. byte offset = 0x3;
  231. (void) sigSz;
  232. (void) hash;
  233. (void) hashSz;
  234. (void) key;
  235. (void) keySz;
  236. sigforSCE = NULL;
  237. WOLFSSL_PKMSG("SCE ECC Verify: ssl->options.serverState = %d sigSz %d, hashSz %d, keySz %d\n",
  238. ssl->options.serverState, sigSz, hashSz, keySz);
  239. if (!wc_sce_usable(ssl, 0)) {
  240. WOLFSSL_PKMSG("Cannot handle cipher suite by SCE");
  241. return CRYPTOCB_UNAVAILABLE;
  242. }
  243. if ((sigforSCE = (uint8_t*)XMALLOC(HW_SCE_ECDSA_DATA_BYTE_SIZE, NULL,
  244. DYNAMIC_TYPE_TEMP)) == NULL) {
  245. WOLFSSL_MSG("failed to malloc memory");
  246. return MEMORY_E;
  247. }
  248. /* initialization */
  249. XMEMSET(sigforSCE, 0, HW_SCE_ECDSA_DATA_BYTE_SIZE);
  250. /* r */
  251. if (sig[offset] == 0x20) {
  252. XMEMCPY(sigforSCE, &sig[offset+1], rs_size);
  253. offset = 0x25;
  254. /* s */
  255. if (sig[offset] == 0x20) {
  256. XMEMCPY(&sigforSCE[rs_size], &sig[offset+1], rs_size);
  257. }
  258. else {
  259. XMEMCPY(&sigforSCE[rs_size], &sig[offset+2], rs_size);
  260. }
  261. }
  262. else {
  263. XMEMCPY(sigforSCE, &sig[offset+2], rs_size);
  264. offset = 0x26;
  265. /* s */
  266. if (sig[offset] == rs_size) {
  267. XMEMCPY(&sigforSCE[rs_size], &sig[offset+1], rs_size);
  268. }
  269. else {
  270. XMEMCPY(&sigforSCE[rs_size], &sig[offset+2], rs_size);
  271. }
  272. }
  273. ret = SCE_ServerKeyExVerify(2, ssl, sigforSCE, 64, ctx);
  274. if (sigforSCE)
  275. XFREE(sigforSCE, NULL, DYNAMIC_TYPE_TEMP);
  276. if (ret == WOLFSSL_SUCCESS) {
  277. *result = 1;
  278. ret = 0; /* for success */
  279. }
  280. else
  281. *result = 0;
  282. WOLFSSL_PKMSG("SCE ECC Verify: ret %d, result %d\n", ret, *result);
  283. return ret;
  284. }
  285. /* Callback for ECC shared secret */
  286. WOLFSSL_LOCAL int SCE_EccSharedSecret(WOLFSSL* ssl, ecc_key* otherKey,
  287. uint8_t* pubKeyDer, unsigned int* pubKeySz,
  288. uint8_t* out, unsigned int* outlen, int side, void* ctx)
  289. {
  290. int ret = WOLFSSL_SUCCESS;
  291. (void) otherKey;
  292. (void) side;
  293. User_SCEPKCbInfo* cbInfo = (User_SCEPKCbInfo*)ctx;
  294. (void)ssl;
  295. (void)cbInfo;
  296. /* sanity check */
  297. if (ssl == NULL || pubKeyDer == NULL || pubKeySz == NULL ||
  298. out == NULL || outlen == NULL || ctx == NULL)
  299. return WOLFSSL_FAILURE;
  300. WOLFSSL_PKMSG("PK ECC PMS: Side %s, Peer Curve %d\n",
  301. side == WOLFSSL_CLIENT_END ? "client" : "server", otherKey->dp->id);
  302. if (cbInfo->keyflgs_tls.bits.pk_key_set == 1) {
  303. if ((ret = wc_sce_hw_lock()) == 0) {
  304. /* Generate ECC PUblic key pair */
  305. ret = R_SCE_TLS_ECC_secp256r1_EphemeralWrappedKeyPairGenerate(
  306. &cbInfo->ecc_p256_wrapped_key,
  307. (uint8_t*)&cbInfo->ecc_ecdh_public_key/* Qx 32 bytes and Qy 32 bytes*/ );
  308. if (ret != FSP_SUCCESS) {
  309. WOLFSSL_PKMSG("Failed secp256r1_EphemeralWrappedKeyPairGenerate %d\n", ret);
  310. return ret;
  311. }
  312. /* copy generated ecdh public key inot buffer */
  313. pubKeyDer[0] = ECC_POINT_UNCOMP;
  314. *pubKeySz = 1 + sizeof(cbInfo->ecc_ecdh_public_key);
  315. XMEMCPY(&pubKeyDer[1], &cbInfo->ecc_ecdh_public_key,
  316. sizeof(cbInfo->ecc_ecdh_public_key));
  317. /* Generate Premaster Secret */
  318. ret = R_SCE_TLS_PreMasterSecretGenerateForECC_secp256r1(
  319. (uint32_t*)&cbInfo->encrypted_ephemeral_ecdh_public_key,
  320. &cbInfo->ecc_p256_wrapped_key,
  321. (uint32_t*)out/* pre-master secret 64 bytes */);
  322. if (ret != FSP_SUCCESS) {
  323. WOLFSSL_PKMSG("Failed PreMasterSecretGenerateForECC_secp256r1 %d\n", ret);
  324. return ret;
  325. }
  326. else {
  327. /* set master secret generation callback for use */
  328. wolfSSL_CTX_SetGenMasterSecretCb(ssl->ctx, Renesas_cmn_genMasterSecret);
  329. wolfSSL_SetGenMasterSecretCtx(ssl, cbInfo);
  330. }
  331. }
  332. else {
  333. WOLFSSL_MSG("Failed to lock sce hw ");
  334. }
  335. /* unlock hw */
  336. wc_sce_hw_unlock();
  337. *outlen = 64;
  338. WOLFSSL_PKMSG("PK ECC PMS: ret %d, PubKeySz %d, OutLen %d\n", ret, *pubKeySz, *outlen);
  339. }
  340. return ret;
  341. }
  342. #endif
  343. /* Return tls cipher suite enumeration that is supported by SCE library */
  344. static uint32_t GetSceCipherSuite(
  345. uint8_t cipherSuiteFirst,
  346. uint8_t cipherSuite)
  347. {
  348. WOLFSSL_ENTER("GetSceCipherSuite");
  349. uint32_t sceCipher;
  350. if (cipherSuiteFirst == CIPHER_BYTE)
  351. {
  352. switch(cipherSuite) {
  353. case TLS_RSA_WITH_AES_128_CBC_SHA256:
  354. sceCipher = SCE_TLS_RSA_WITH_AES_128_CBC_SHA256;
  355. break;
  356. case TLS_RSA_WITH_AES_256_CBC_SHA256:
  357. sceCipher = SCE_TLS_RSA_WITH_AES_256_CBC_SHA256;
  358. break;
  359. default:
  360. sceCipher = (uint32_t)WOLFSSL_SCE_ILLEGAL_CIPHERSUITE;
  361. break;
  362. }
  363. WOLFSSL_MSG("<< GetSceCipherSuite");
  364. return sceCipher;
  365. }
  366. else if (cipherSuiteFirst == ECC_BYTE)
  367. {
  368. sceCipher = (uint32_t)WOLFSSL_SCE_ILLEGAL_CIPHERSUITE;
  369. /* comment out until implementation completes */
  370. switch(cipherSuite) {
  371. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  372. sceCipher = SCE_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256;
  373. break;
  374. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  375. sceCipher = SCE_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256;
  376. break;
  377. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  378. sceCipher = SCE_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256;
  379. break;
  380. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  381. sceCipher = SCE_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
  382. break;
  383. default:
  384. sceCipher = (uint32_t)WOLFSSL_SCE_ILLEGAL_CIPHERSUITE;
  385. break;
  386. }
  387. }
  388. else{
  389. sceCipher = (uint32_t)WOLFSSL_SCE_ILLEGAL_CIPHERSUITE;
  390. }
  391. WOLFSSL_MSG("<< GetSceCipherSuite");
  392. return sceCipher;
  393. }
  394. /* check if sce tls functions can be used for the cipher */
  395. /* */
  396. /* ssl : a pointer to WOLFSSL object */
  397. /* session_key_generated : if session key has been generated */
  398. /* return 1 for usable, 0 for unusable */
  399. WOLFSSL_LOCAL int wc_sce_usable(const WOLFSSL *ssl,
  400. uint8_t session_key_generated)
  401. {
  402. WOLFSSL_ENTER("sce_usable");
  403. uint32_t sceCipher;
  404. byte side;
  405. const Ciphers *enc;
  406. const Ciphers *dec;
  407. /* sanity check */
  408. if (ssl == NULL)
  409. return BAD_FUNC_ARG;
  410. /* when rsa key index == NULL, SCE isn't used for cert verification. */
  411. /* in the case, we cannot use TSIP. */
  412. if (!ssl->peerSceTsipEncRsaKeyIndex)
  413. return 0;
  414. /* when enabled Extended Master Secret, we cannot use SCE. */
  415. if (ssl->options.haveEMS)
  416. return 0;
  417. /* when session_key_generated is set as 1, expects to be created */
  418. /* session key already. */
  419. if (session_key_generated) {
  420. enc = &ssl->encrypt;
  421. dec = &ssl->decrypt;
  422. if (enc == NULL || dec == NULL) {
  423. /* something wrong */
  424. return 0;
  425. }
  426. if (enc->aes == NULL || dec->aes == NULL) {
  427. return 0;
  428. }
  429. if (enc->aes->ctx.setup == 0) {
  430. /* session key for SCE is not created */
  431. return 0;
  432. }
  433. }
  434. /* retrieve cipher suite if SCE supports */
  435. sceCipher = GetSceCipherSuite(ssl->options.cipherSuite0,
  436. ssl->options.cipherSuite);
  437. side = ssl->options.side;
  438. if (sceCipher != (uint32_t)WOLFSSL_SCE_ILLEGAL_CIPHERSUITE
  439. && side == WOLFSSL_CLIENT_END)
  440. return 1;
  441. else
  442. return 0;
  443. }
  444. /* Generate Hmac by sha256*/
  445. WOLFSSL_LOCAL int wc_sce_Sha256GenerateHmac(const WOLFSSL *ssl,const uint8_t* myInner,
  446. uint32_t innerSz,const uint8_t* in, uint32_t sz, byte* digest)
  447. {
  448. WOLFSSL_ENTER("sce_Sha256HmacGenerate");
  449. sce_hmac_sha_handle_t _handle;
  450. sce_hmac_sha_wrapped_key_t wrapped_key;
  451. int ret;
  452. if ((ssl == NULL) || (myInner == NULL) || (in == NULL) ||
  453. (digest == NULL))
  454. return BAD_FUNC_ARG;
  455. wrapped_key = ssl->keys.sce_client_write_MAC_secret;
  456. if ((ret = wc_sce_hw_lock()) != 0) {
  457. WOLFSSL_MSG("hw lock failed");
  458. return ret;
  459. }
  460. ret = R_SCE_SHA256HMAC_GenerateInit(
  461. &_handle,
  462. &wrapped_key);
  463. if (ret == FSP_SUCCESS)
  464. ret = R_SCE_SHA256HMAC_GenerateUpdate(
  465. &_handle,
  466. (uint8_t*)myInner,
  467. innerSz);
  468. if (ret == FSP_SUCCESS)
  469. ret = R_SCE_SHA256HMAC_GenerateUpdate(
  470. &_handle,
  471. (uint8_t*)in,
  472. sz);
  473. if (ret == FSP_SUCCESS)
  474. ret = R_SCE_SHA256HMAC_GenerateFinal(
  475. &_handle,
  476. digest);
  477. if (ret != FSP_SUCCESS)
  478. ret = WOLFSSL_FAILURE;
  479. /* unlock hw */
  480. wc_sce_hw_unlock();
  481. WOLFSSL_LEAVE("sce_Sha256HmacGenerate", ret);
  482. return ret;
  483. }
  484. /* Verify hmac */
  485. WOLFSSL_LOCAL int wc_sce_Sha256VerifyHmac(const WOLFSSL *ssl,
  486. const uint8_t* message, uint32_t messageSz,
  487. uint32_t macSz, uint32_t content)
  488. {
  489. WOLFSSL_ENTER("sce_Sha256HmacVerify");
  490. sce_hmac_sha_handle_t _handle;
  491. sce_hmac_sha_wrapped_key_t wrapped_key;
  492. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  493. int ret;
  494. if ((ssl == NULL) || (message == NULL))
  495. return BAD_FUNC_ARG;
  496. wrapped_key = ssl->keys.sce_server_write_MAC_secret;
  497. if ((ret = wc_sce_hw_lock()) != 0) {
  498. WOLFSSL_MSG("hw lock failed");
  499. return ret;
  500. }
  501. wolfSSL_SetTlsHmacInner((WOLFSSL*)ssl, myInner,
  502. (word32)messageSz, (int)content, 1);
  503. ret = R_SCE_SHA256HMAC_VerifyInit(
  504. &_handle,
  505. &wrapped_key);
  506. if (ret == FSP_SUCCESS)
  507. ret = R_SCE_SHA256HMAC_VerifyUpdate(
  508. &_handle,
  509. (uint8_t*)myInner,
  510. WOLFSSL_TLS_HMAC_INNER_SZ);
  511. if (ret == FSP_SUCCESS)
  512. ret = R_SCE_SHA256HMAC_VerifyUpdate(
  513. &_handle,
  514. (uint8_t*)message,
  515. (uint32_t)messageSz);
  516. if (ret == FSP_SUCCESS)
  517. ret = R_SCE_SHA256HMAC_VerifyFinal(
  518. &_handle,
  519. (uint8_t*)(message+messageSz),
  520. (uint32_t)macSz);
  521. if (ret != FSP_SUCCESS) {
  522. WOLFSSL_MSG("SCE Mac verification failed");
  523. }
  524. /* unlock hw */
  525. wc_sce_hw_unlock();
  526. WOLFSSL_LEAVE("sce_Sha256HmacVerify", ret);
  527. return ret;
  528. }
  529. /* generate Verify Data based on master secret */
  530. WOLFSSL_LOCAL int wc_sce_generateVerifyData(const uint8_t *ms, /* master secret */
  531. const uint8_t *side, const uint8_t *handshake_hash,
  532. uint8_t *hashes /* out */)
  533. {
  534. WOLFSSL_ENTER("sce_generateVerifyData");
  535. int ret ;
  536. uint32_t l_side = SCE_TLS_GENERATE_CLIENT_VERIFY;
  537. if ((ms == NULL) || (side == NULL) || (handshake_hash == NULL) ||
  538. (hashes == NULL))
  539. return BAD_FUNC_ARG;
  540. if (XSTRNCMP((const char*)side, (const char*)kTlsServerFinStr,
  541. FINISHED_LABEL_SZ) == 0)
  542. {
  543. l_side = SCE_TLS_GENERATE_SERVER_VERIFY;
  544. }
  545. if ((ret = wc_sce_hw_lock()) == 0) {
  546. ret = R_SCE_TLS_VerifyDataGenerate(l_side, (uint32_t*)ms,
  547. (uint8_t*)handshake_hash, hashes/* out */);
  548. if (ret != FSP_SUCCESS) {
  549. WOLFSSL_MSG("R_SCE_TLS_VerifyDataGenerate failed");
  550. }
  551. }
  552. /* unlock hw */
  553. wc_sce_hw_unlock();
  554. WOLFSSL_LEAVE("sce_generateVerifyData", ret);
  555. return ret;
  556. }
  557. /* generate keys for TLS communication */
  558. WOLFSSL_LOCAL int wc_sce_generateSessionKey(WOLFSSL *ssl,
  559. User_SCEPKCbInfo* cbInfo, int devId)
  560. {
  561. WOLFSSL_MSG("sce_generateSessionKey()");
  562. int ret;
  563. Ciphers *enc;
  564. Ciphers *dec;
  565. sce_hmac_sha_wrapped_key_t key_client_mac;
  566. sce_hmac_sha_wrapped_key_t key_server_mac;
  567. sce_aes_wrapped_key_t key_client_aes;
  568. sce_aes_wrapped_key_t key_server_aes;
  569. uint32_t sceCS = GetSceCipherSuite(ssl->options.cipherSuite0,
  570. ssl->options.cipherSuite);
  571. if (ssl== NULL || cbInfo == NULL)
  572. return BAD_FUNC_ARG;
  573. if ((ret = wc_sce_hw_lock()) == 0) {
  574. if (sceCS == TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 ||
  575. sceCS == TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256)
  576. {
  577. WOLFSSL_MSG("Session key for AES-GCM generation skipped.");
  578. wolfSSL_KeepArrays(ssl);
  579. ret = FSP_SUCCESS;
  580. }
  581. else {
  582. ret = R_SCE_TLS_SessionKeyGenerate(
  583. GetSceCipherSuite(
  584. ssl->options.cipherSuite0,
  585. ssl->options.cipherSuite),
  586. (uint32_t*)ssl->arrays->sce_masterSecret,
  587. (uint8_t*) ssl->arrays->clientRandom,
  588. (uint8_t*) ssl->arrays->serverRandom,
  589. NULL,
  590. &key_client_mac,
  591. &key_server_mac,
  592. &key_client_aes,
  593. &key_server_aes,
  594. NULL, NULL);
  595. }
  596. if (ret != FSP_SUCCESS) {
  597. WOLFSSL_MSG("R_SCE_TLS_SessionKeyGenerate failed");
  598. }
  599. else {
  600. /* succeeded creating session keys */
  601. /* alloc aes instance for both enc and dec */
  602. enc = &ssl->encrypt;
  603. dec = &ssl->decrypt;
  604. if (enc) {
  605. if (enc->aes == NULL) {
  606. enc->aes = (Aes*)XMALLOC(sizeof(Aes), ssl->heap,
  607. DYNAMIC_TYPE_CIPHER);
  608. if (enc->aes == NULL)
  609. return MEMORY_E;
  610. }
  611. XMEMSET(enc->aes, 0, sizeof(Aes));
  612. }
  613. if (dec) {
  614. if (dec->aes == NULL) {
  615. dec->aes = (Aes*)XMALLOC(sizeof(Aes), ssl->heap,
  616. DYNAMIC_TYPE_CIPHER);
  617. if (dec->aes == NULL) {
  618. if (enc) {
  619. XFREE(enc->aes, NULL, DYNAMIC_TYPE_CIPHER);
  620. }
  621. return MEMORY_E;
  622. }
  623. }
  624. XMEMSET(dec->aes, 0, sizeof(Aes));
  625. }
  626. /* copy key index into aes */
  627. if (ssl->options.side == PROVISION_CLIENT) {
  628. XMEMCPY(&enc->aes->ctx.sce_wrapped_key, &key_client_aes,
  629. sizeof(key_client_aes));
  630. XMEMCPY(&dec->aes->ctx.sce_wrapped_key, &key_server_aes,
  631. sizeof(key_server_aes));
  632. }
  633. else {
  634. XMEMCPY(&enc->aes->ctx.sce_wrapped_key, &key_server_aes,
  635. sizeof(key_server_aes));
  636. XMEMCPY(&dec->aes->ctx.sce_wrapped_key, &key_client_aes,
  637. sizeof(key_client_aes));
  638. }
  639. /* copy mac key index into keys */
  640. ssl->keys.sce_client_write_MAC_secret = key_client_mac;
  641. ssl->keys.sce_server_write_MAC_secret = key_server_mac;
  642. /* set up key size and marked ready */
  643. if (enc) {
  644. enc->aes->ctx.keySize = ssl->specs.key_size;
  645. enc->aes->ctx.setup = 1;
  646. /* ready for use */
  647. enc->setup = 1;
  648. }
  649. /* set up key size and marked ready */
  650. if (dec) {
  651. dec->aes->ctx.keySize = ssl->specs.key_size;
  652. dec->aes->ctx.setup = 1;
  653. /* ready for use */
  654. dec->setup = 1;
  655. }
  656. if (cbInfo->sce_cipher == SCE_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ||
  657. cbInfo->sce_cipher == SCE_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
  658. enc->aes->nonceSz = AEAD_MAX_IMP_SZ;
  659. dec->aes->nonceSz = AEAD_MAX_IMP_SZ;
  660. }
  661. enc->aes->devId = devId;
  662. dec->aes->devId = devId;
  663. /* marked as session key is set */
  664. cbInfo->keyflgs_tls.bits.session_key_set = 1;
  665. }
  666. /* unlock hw */
  667. wc_sce_hw_unlock();
  668. }
  669. else {
  670. WOLFSSL_LEAVE("hw lock failed", ret);
  671. }
  672. WOLFSSL_LEAVE("sce_generateSessionKey", ret);
  673. return ret;
  674. }
  675. /* generate master secret based on pre-master which is generated by SCE */
  676. WOLFSSL_LOCAL int wc_sce_generateMasterSecret(
  677. uint8_t cipherSuiteFirst,
  678. uint8_t cipherSuite,
  679. const uint8_t *pr, /* pre-master */
  680. const uint8_t *cr, /* client random */
  681. const uint8_t *sr, /* server random */
  682. uint8_t *ms)
  683. {
  684. WOLFSSL_ENTER("sce_generateMasterSecretEx");
  685. int ret;
  686. if ((pr == NULL) || (cr == NULL) || (sr == NULL) ||
  687. (ms == NULL))
  688. return BAD_FUNC_ARG;
  689. uint32_t sceCS = GetSceCipherSuite(cipherSuiteFirst, cipherSuite);
  690. if (sceCS == 0xffffffff)
  691. return BAD_FUNC_ARG;
  692. if ((ret = wc_sce_hw_lock()) == 0) {
  693. ret = R_SCE_TLS_MasterSecretGenerate(
  694. sceCS,
  695. (uint32_t*)pr,
  696. (uint8_t*)cr, (uint8_t*)sr, (uint32_t*)ms);
  697. if (ret != FSP_SUCCESS) {
  698. WOLFSSL_MSG("R_SCE_TLS_MasterSecretGenerate failed");
  699. }
  700. /* unlock hw */
  701. wc_sce_hw_unlock();
  702. }
  703. else {
  704. WOLFSSL_MSG(" hw lock failed ");
  705. }
  706. WOLFSSL_LEAVE("sce_generateMasterSecret", ret);
  707. return ret;
  708. }
  709. /* generate pre-Master secrete by SCE */
  710. WOLFSSL_LOCAL int wc_sce_generatePremasterSecret(uint8_t *premaster,
  711. uint32_t preSz)
  712. {
  713. WOLFSSL_ENTER("sce_generatePremasterSecret");
  714. int ret;
  715. if (premaster == NULL)
  716. return BAD_FUNC_ARG;
  717. if ((ret = wc_sce_hw_lock()) == 0 && preSz >=
  718. (SCE_TLS_MASTER_SECRET_WORD_SIZE*4)) {
  719. /* generate pre-master, 80 bytes */
  720. ret = R_SCE_TLS_PreMasterSecretGenerateForRSA2048((uint32_t*)premaster);
  721. if (ret != FSP_SUCCESS) {
  722. WOLFSSL_MSG(" R_SCE_TLS_GeneratePreMasterSecret failed");
  723. }
  724. /* unlock hw */
  725. wc_sce_hw_unlock();
  726. }
  727. else {
  728. WOLFSSL_MSG(" hw lock failed or preSz is smaller than 80");
  729. }
  730. WOLFSSL_LEAVE("sce_generatePremasterSecret", ret);
  731. return ret;
  732. }
  733. /*
  734. * generate encrypted pre-Master secrete by SCE
  735. */
  736. WOLFSSL_LOCAL int wc_sce_generateEncryptPreMasterSecret(
  737. WOLFSSL* ssl,
  738. uint8_t* out,
  739. uint32_t* outSz)
  740. {
  741. WOLFSSL_MSG("sce_generateEncryptPreMasterSecret");
  742. int ret;
  743. if ((ssl == NULL) || (out == NULL) || (outSz == NULL))
  744. return BAD_FUNC_ARG;
  745. if ((ret = wc_sce_hw_lock()) == 0) {
  746. if (*outSz >= 256)
  747. ret = R_SCE_TLS_PreMasterSecretEncryptWithRSA2048(
  748. (uint32_t*)ssl->peerSceTsipEncRsaKeyIndex,
  749. (uint32_t*)ssl->arrays->preMasterSecret,
  750. (uint8_t*)out);
  751. else
  752. ret = -1;
  753. if (ret != FSP_SUCCESS) {
  754. WOLFSSL_MSG("R_SCE_TLS_PreMasterSecretEncryptWithRSA2048 failed");
  755. }
  756. else {
  757. *outSz = 256; /* SCE can only handles 2048 RSA */
  758. /* set GenMaster Callback for Master secret generation */
  759. void* ctx = wolfSSL_GetRsaVerifyCtx(ssl);
  760. wolfSSL_CTX_SetGenMasterSecretCb(ssl->ctx,
  761. Renesas_cmn_genMasterSecret);
  762. wolfSSL_SetGenMasterSecretCtx(ssl, ctx);
  763. }
  764. wc_sce_hw_unlock();
  765. }
  766. else {
  767. WOLFSSL_MSG(" hw lock failed ");
  768. }
  769. WOLFSSL_LEAVE("sce_generateEncryptPreMasterSecret", ret);
  770. return ret;
  771. }
  772. /* Certificate verification by SCE */
  773. WOLFSSL_LOCAL int wc_sce_tls_CertVerify(
  774. const uint8_t* cert, uint32_t certSz,
  775. const uint8_t* signature, uint32_t sigSz,
  776. uint32_t key_n_start,uint32_t key_n_len,
  777. uint32_t key_e_start,uint32_t key_e_len,
  778. uint8_t* sce_encPublickey)
  779. {
  780. WOLFSSL_ENTER("sce_tls_CertVerify");
  781. int ret;
  782. uint8_t *sigforSCE;
  783. uint8_t *pSig;
  784. const byte rs_size = 0x20;
  785. byte offset = 0x3;
  786. (void)sigSz;
  787. sigforSCE = NULL;
  788. pSig = NULL;
  789. if (cert == NULL)
  790. return BAD_FUNC_ARG;
  791. if (!signature) {
  792. WOLFSSL_MSG(" signature for ca verification is not set");
  793. return -1;
  794. }
  795. if (!sce_encPublickey) {
  796. WOLFSSL_MSG(" sce_encPublickey is NULL.");
  797. return -1;
  798. }
  799. if (g_user_key_info.encrypted_user_tls_key_type ==
  800. SCE_TLS_PUBLIC_KEY_TYPE_ECDSA_P256/*ECDSA*/) {
  801. if ((sigforSCE = (uint8_t*)XMALLOC(HW_SCE_ECDSA_DATA_BYTE_SIZE, NULL,
  802. DYNAMIC_TYPE_TEMP)) == NULL) {
  803. WOLFSSL_MSG("failed to malloc memory");
  804. return MEMORY_E;
  805. }
  806. /* initialization */
  807. XMEMSET(sigforSCE, 0, HW_SCE_ECDSA_DATA_BYTE_SIZE);
  808. if (signature[offset] == 0x20) {
  809. XMEMCPY(sigforSCE, &signature[offset+1], rs_size);
  810. offset = 0x25;
  811. if (signature[offset] == 0x20) {
  812. XMEMCPY(&sigforSCE[rs_size], &signature[offset+1], rs_size);
  813. }
  814. else {
  815. XMEMCPY(&sigforSCE[rs_size], &signature[offset+2], rs_size);
  816. }
  817. }
  818. else {
  819. XMEMCPY(sigforSCE, &signature[offset+2], rs_size);
  820. offset = 0x26;
  821. if (signature[offset] == rs_size) {
  822. XMEMCPY(&sigforSCE[rs_size], &signature[offset+1], rs_size);
  823. }
  824. else {
  825. XMEMCPY(&sigforSCE[rs_size], &signature[offset+2], rs_size);
  826. }
  827. }
  828. pSig = sigforSCE;
  829. }
  830. else {
  831. pSig = (uint8_t*)signature;
  832. }
  833. if ((ret = wc_sce_hw_lock()) == 0) {
  834. ret = R_SCE_TLS_CertificateVerify(
  835. g_user_key_info.encrypted_user_tls_key_type,
  836. (uint32_t*)g_encrypted_publicCA_key,/* encrypted public key */
  837. (uint8_t*)cert, /* certificate der */
  838. certSz, /* length of der */
  839. (uint8_t*)pSig, /* sign data by RSA PSS */
  840. key_n_start, /* start position of public key n in bytes */
  841. (key_n_start + key_n_len), /* length of the public key n */
  842. key_e_start, /* start pos, key e in bytes */
  843. (key_e_start + key_e_len), /* length of the public key e */
  844. (uint32_t*)sce_encPublickey /* returned encrypted key */
  845. );
  846. if (ret != FSP_SUCCESS) {
  847. WOLFSSL_MSG(" R_TSIP_TlsCertificateVerification() failed");
  848. }
  849. if (sigforSCE) {
  850. XFREE(sigforSCE, NULL, DYNAMIC_TYPE_TEMP);
  851. }
  852. wc_sce_hw_unlock();
  853. }
  854. else {
  855. WOLFSSL_MSG(" hw lock failed ");
  856. }
  857. WOLFSSL_LEAVE("sce_tls_CertVerify", ret);
  858. return ret;
  859. }
  860. /* Root Certificate verification */
  861. WOLFSSL_LOCAL int wc_sce_tls_RootCertVerify(
  862. const uint8_t* cert, uint32_t cert_len,
  863. uint32_t key_n_start, uint32_t key_n_len,
  864. uint32_t key_e_start, uint32_t key_e_len,
  865. uint32_t cm_row)
  866. {
  867. int ret;
  868. /* call to generate encrypted public key for certificate verification */
  869. uint8_t *signature = (uint8_t*)ca_cert_sig;
  870. WOLFSSL_ENTER("wc_sce_tls_RootCertVerify");
  871. if (cert == NULL)
  872. return BAD_FUNC_ARG;
  873. if (!signature) {
  874. WOLFSSL_MSG(" signature for ca verification is not set");
  875. return -1;
  876. }
  877. if ((ret = wc_sce_hw_lock()) == 0) {
  878. ret = R_SCE_TLS_RootCertificateVerify(
  879. g_user_key_info.encrypted_user_tls_key_type,
  880. (uint8_t*)cert, /* CA cert */
  881. (uint32_t)cert_len, /* length of CA cert */
  882. key_n_start, /* Byte position of public key */
  883. (key_n_start + key_n_len),
  884. key_e_start,
  885. (key_e_start + key_e_len),
  886. (uint8_t*)ca_cert_sig, /* RSA 2048 PSS with SHA256 */
  887. g_encrypted_publicCA_key); /* RSA-2048 public key 560 bytes */
  888. /* ECDSA 96 bytes */
  889. if (ret != FSP_SUCCESS) {
  890. WOLFSSL_MSG(" R_SCE_TLS_RootCertificateVerify() failed");
  891. }
  892. else {
  893. g_CAscm_Idx = cm_row;
  894. }
  895. wc_sce_hw_unlock();
  896. }
  897. else {
  898. WOLFSSL_MSG(" hw lock failed ");
  899. }
  900. WOLFSSL_LEAVE("wc_sce_tls_RootCertVerify", ret);
  901. return ret;
  902. }
  903. /* store elements for session key generation into ssl->keys.
  904. * return 0 on success, negative value on failure
  905. */
  906. WOLFSSL_LOCAL int wc_sce_storeKeyCtx(WOLFSSL* ssl, User_SCEPKCbInfo* info)
  907. {
  908. int ret = 0;
  909. WOLFSSL_ENTER("sce_storeKeyCtx");
  910. if (ssl == NULL || info == NULL)
  911. ret = BAD_FUNC_ARG;
  912. if (ret == 0) {
  913. XMEMCPY(info->sce_masterSecret, ssl->arrays->sce_masterSecret,
  914. SCE_TLS_MASTERSECRET_SIZE);
  915. XMEMCPY(info->sce_clientRandom, ssl->arrays->clientRandom, 32);
  916. XMEMCPY(info->sce_serverRandom, ssl->arrays->serverRandom, 32);
  917. info->sce_cipher = (uint8_t)GetSceCipherSuite(ssl->options.cipherSuite0,
  918. ssl->options.cipherSuite);
  919. }
  920. WOLFSSL_LEAVE("sce_storeKeyCtx", ret);
  921. return ret;
  922. }
  923. /* to inform ca certificate sign */
  924. /* signature format expects RSA 2048 PSS with SHA256 */
  925. WOLFSSL_API void wc_sce_inform_cert_sign(const uint8_t *sign)
  926. {
  927. if (sign)
  928. ca_cert_sig = sign;
  929. }
  930. /* let wolfSSL know user key information using TLS operation by SCE */
  931. WOLFSSL_API void wc_sce_inform_user_keys(
  932. uint8_t* encrypted_provisioning_key,
  933. uint8_t* iv,
  934. uint8_t* encrypted_user_tls_key,
  935. uint32_t encrypted_user_tls_key_type)
  936. {
  937. WOLFSSL_ENTER("sce_inform_user_keys");
  938. g_user_key_info.encrypted_provisioning_key = NULL;
  939. g_user_key_info.iv = NULL;
  940. g_user_key_info.encrypted_user_tls_key = NULL;
  941. if (encrypted_provisioning_key ) {
  942. g_user_key_info.encrypted_provisioning_key = encrypted_provisioning_key;
  943. }
  944. if (iv) {
  945. g_user_key_info.iv = iv;
  946. }
  947. if (encrypted_user_tls_key) {
  948. g_user_key_info.encrypted_user_tls_key = encrypted_user_tls_key;
  949. }
  950. g_user_key_info.encrypted_user_tls_key_type = encrypted_user_tls_key_type;
  951. /* set callback for ECC */
  952. WOLFSSL_MSG("sce_inform_user_keys_ex");
  953. }
  954. /* Set callbacks needed for sce TLS api handling */
  955. WOLFSSL_API void wc_sce_set_callbacks(WOLFSSL_CTX* ctx)
  956. {
  957. wolfSSL_CTX_SetEccVerifyCb(ctx, Renesas_cmn_EccVerify);
  958. wolfSSL_CTX_SetRsaVerifyCb(ctx, Renesas_cmn_RsaVerify);
  959. wolfSSL_CTX_SetGenPreMasterCb(ctx, Renesas_cmn_generatePremasterSecret);
  960. wolfSSL_CTX_SetRsaEncCb(ctx, Renesas_cmn_RsaEnc);
  961. wolfSSL_CTX_SetVerifyMacCb(ctx, Renesas_cmn_VerifyHmac);
  962. /* reset callbacks */
  963. wolfSSL_CTX_SetEccSharedSecretCb(ctx, NULL);
  964. }
  965. /* Set callback contexts needed for sce TLS api handling */
  966. WOLFSSL_API int wc_sce_set_callback_ctx(WOLFSSL* ssl, void* user_ctx)
  967. {
  968. if (sce_sess_idx > MAX_SCE_CBINDEX) {
  969. WOLFSSL_MSG("exceeds maximum session index");
  970. return -1;
  971. }
  972. gSCE_PKCbInfo.user_PKCbInfo[sce_sess_idx] = (User_SCEPKCbInfo*)user_ctx;
  973. gSCE_PKCbInfo.user_PKCbInfo[sce_sess_idx]->keyflgs_tls.bits.pk_key_set = 0;
  974. gSCE_PKCbInfo.user_PKCbInfo[sce_sess_idx]->keyflgs_tls.bits.session_key_set = 0;
  975. wolfSSL_SetEccVerifyCtx(ssl, user_ctx);
  976. wolfSSL_SetRsaEncCtx(ssl, user_ctx);
  977. wolfSSL_SetRsaVerifyCtx(ssl, user_ctx);
  978. wolfSSL_SetGenPreMasterCtx(ssl, user_ctx);
  979. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  980. wolfSSL_SetVerifyMacCtx(ssl, user_ctx);
  981. /* set up crypt callback */
  982. wc_CryptoCb_CryptInitRenesasCmn(ssl, user_ctx);
  983. gSCE_PKCbInfo.num_session = ++sce_sess_idx;
  984. return 0;
  985. }
  986. #endif /* !WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY */
  987. #endif /* WOLFSSL_RENESAS_SCEPROTECT || WOLFSSL_RENESAS_SCEPROTECT_CRYPTONLY */