renesas_common.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /* renesas_common.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_FSPSM_TLS) \
  23. || defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) \
  24. || defined(WOLFSSL_RENESAS_TSIP_TLS) \
  25. || defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  26. #if defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
  27. defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  28. #include <wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h>
  29. #define cmn_hw_lock wc_fspsm_hw_lock
  30. #define cmn_hw_unlock wc_fspsm_hw_unlock
  31. #elif defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  32. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  33. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  34. #define cmn_hw_lock tsip_hw_lock
  35. #define cmn_hw_unlock tsip_hw_unlock
  36. #define FSPSM_ST TsipUserCtx;
  37. #define MAX_FSPSM_CBINDEX 5
  38. #endif
  39. #include <wolfssl/wolfcrypt/wc_port.h>
  40. #include <wolfssl/wolfcrypt/types.h>
  41. #include <wolfssl/wolfcrypt/asn.h>
  42. #include <wolfssl/internal.h>
  43. #include <wolfssl/error-ssl.h>
  44. #include <wolfssl/wolfcrypt/error-crypt.h>
  45. #include <wolfssl/wolfcrypt/logging.h>
  46. #include <wolfssl/wolfcrypt/port/renesas/renesas_cmn.h>
  47. uint32_t g_CAscm_Idx = (uint32_t)-1; /* index of CM table */
  48. static int gdevId = 7890; /* initial dev Id for Crypt Callback */
  49. #ifdef WOLF_CRYPTO_CB
  50. /* store callback ctx by devId */
  51. #if defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
  52. defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  53. FSPSM_ST *gCbCtx[MAX_FSPSM_CBINDEX];
  54. #elif defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  55. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  56. #define FSPSM_ST TsipUserCtx;
  57. #define MAX_FSPSM_CBINDEX 5
  58. TsipUserCtx *gCbCtx[MAX_FSPSM_CBINDEX];
  59. #endif
  60. #include <wolfssl/wolfcrypt/cryptocb.h>
  61. WOLFSSL_LOCAL int Renesas_cmn_Cleanup(WOLFSSL* ssl)
  62. {
  63. int ret = 0;
  64. WOLFSSL_ENTER("Renesas_cmn_Cleanup");
  65. (void) ssl;
  66. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  67. ret = tsip_TlsCleanup(ssl);
  68. #endif
  69. WOLFSSL_LEAVE("Renesas_cmn_Cleanup", ret);
  70. return ret;
  71. }
  72. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  73. WOLFSSL_LOCAL int Renesas_cmn_RsaSignCb(WOLFSSL* ssl,
  74. const unsigned char* in, unsigned int inSz,
  75. unsigned char* out, word32* outSz,
  76. const unsigned char* keyDer, unsigned int keySz,
  77. void* ctx)
  78. {
  79. int ret = CRYPTOCB_UNAVAILABLE;
  80. WOLFSSL_ENTER("Renesas_cmn_RsaSignCb");
  81. /* This is just a stub function that provides no logic */
  82. WOLFSSL_LEAVE("Renesas_cmn_RsaSignCb", ret);
  83. return ret;
  84. }
  85. /* This function is a callback passed to wolfSSL_CTX_SetRsaSignCheckCb.
  86. * It tries to verify the signature passed to it by decrypting with a public
  87. * key.
  88. * returns 0 on success, CRYPTOCB_UNAVAILABLE when public key is not set.
  89. */
  90. WOLFSSL_LOCAL int Renesas_cmn_RsaSignCheckCb(WOLFSSL* ssl,
  91. unsigned char* sig, unsigned int sigSz,
  92. unsigned char** out,
  93. const unsigned char* keyDer, unsigned int keySz,
  94. void* ctx)
  95. {
  96. int ret = CRYPTOCB_UNAVAILABLE;
  97. WOLFSSL_ENTER("Renesas_cmn_RsaSignCheckCb");
  98. #if defined(WOLFSSL_RENESAS_TSIP)
  99. return tsip_VerifyRsaPkcsCb(ssl, sig, sigSz, out, keyDer, keySz, ctx);
  100. #endif /* WOLFSSL_RENESAS_TSIP */
  101. WOLFSSL_LEAVE("Renesas_cmn_RsaSignCheckCb", ret);
  102. return ret;
  103. }
  104. WOLFSSL_LOCAL int Renesas_cmn_EccSignCb(WOLFSSL* ssl,
  105. const unsigned char* in, unsigned int inSz,
  106. unsigned char* out, word32* outSz,
  107. const unsigned char* keyDer, unsigned int keySz,
  108. void* ctx)
  109. {
  110. int ret = CRYPTOCB_UNAVAILABLE;
  111. WOLFSSL_ENTER("Renesas_cmn_EccSignCb");
  112. /* This is just a stub function that provides no logic */
  113. WOLFSSL_LEAVE("Renesas_cmn_EccSignCb", ret);
  114. return ret;
  115. }
  116. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  117. /* Renesas Security Library Common Callback
  118. * For Crypto Callbacks
  119. *
  120. * devIdArg device Id
  121. * info pointer to wc_CryptInfo
  122. * ctx Crypto Callback context
  123. * return 0 on success, otherwise MEMORY_E or BAD_FUNC_ARG on failure
  124. */
  125. static int Renesas_cmn_CryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  126. {
  127. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  128. WOLFSSL_ENTER("Renesas_cmn_CryptoDevCb");
  129. #if defined(WOLFSSL_RENESAS_TSIP_TLS) \
  130. || defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  131. TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
  132. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
  133. defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  134. FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
  135. #endif
  136. if (info == NULL || ctx == NULL)
  137. return BAD_FUNC_ARG;
  138. #ifdef DEBUG_WOLFSSL
  139. printf("CryptoDevCb: Algo Type %d session key set: %d\n",
  140. info->algo_type, cbInfo->session_key_set);
  141. #endif
  142. #if defined(WOLFSSL_RENESAS_TSIP) \
  143. || defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  144. ret = CRYPTOCB_UNAVAILABLE;
  145. if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  146. #if !defined(NO_AES) || !defined(NO_DES3)
  147. #ifdef HAVE_AESGCM
  148. if (info->cipher.type == WC_CIPHER_AES_GCM
  149. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  150. && cbInfo->session_key_set == 1
  151. #endif
  152. ) {
  153. if (info->cipher.enc) {
  154. ret = wc_tsip_AesGcmEncrypt(
  155. info->cipher.aesgcm_enc.aes,
  156. (byte*)info->cipher.aesgcm_enc.out,
  157. (byte*)info->cipher.aesgcm_enc.in,
  158. info->cipher.aesgcm_enc.sz,
  159. (byte*)info->cipher.aesgcm_enc.iv,
  160. info->cipher.aesgcm_enc.ivSz,
  161. (byte*)info->cipher.aesgcm_enc.authTag,
  162. info->cipher.aesgcm_enc.authTagSz,
  163. (byte*)info->cipher.aesgcm_enc.authIn,
  164. info->cipher.aesgcm_enc.authInSz,
  165. (void*)ctx);
  166. }
  167. else {
  168. ret = wc_tsip_AesGcmDecrypt(
  169. info->cipher.aesgcm_dec.aes,
  170. (byte*)info->cipher.aesgcm_dec.out,
  171. (byte*)info->cipher.aesgcm_dec.in,
  172. info->cipher.aesgcm_dec.sz,
  173. (byte*)info->cipher.aesgcm_dec.iv,
  174. info->cipher.aesgcm_dec.ivSz,
  175. (byte*)info->cipher.aesgcm_dec.authTag,
  176. info->cipher.aesgcm_dec.authTagSz,
  177. (byte*)info->cipher.aesgcm_dec.authIn,
  178. info->cipher.aesgcm_dec.authInSz,
  179. (void*)ctx);
  180. }
  181. }
  182. #endif /* HAVE_AESGCM */
  183. #ifdef HAVE_AES_CBC
  184. if (info->cipher.type == WC_CIPHER_AES_CBC
  185. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  186. && cbInfo->session_key_set == 1
  187. #endif
  188. ) {
  189. if (info->cipher.enc) {
  190. ret = wc_tsip_AesCbcEncrypt(
  191. info->cipher.aescbc.aes,
  192. (byte*)info->cipher.aescbc.out,
  193. (byte*)info->cipher.aescbc.in,
  194. info->cipher.aescbc.sz);
  195. }
  196. else {
  197. ret = wc_tsip_AesCbcDecrypt(
  198. info->cipher.aescbc.aes,
  199. (byte*)info->cipher.aescbc.out,
  200. (byte*)info->cipher.aescbc.in,
  201. info->cipher.aescbc.sz);
  202. }
  203. }
  204. #endif /* HAVE_AES_CBC */
  205. #endif /* !NO_AES || !NO_DES3 */
  206. }
  207. #if defined(WOLFSSL_KEY_GEN)
  208. if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN &&
  209. (info->pk.rsakg.size == 1024 ||
  210. info->pk.rsakg.size == 2048)) {
  211. ret = wc_tsip_MakeRsaKey(info->pk.rsakg.size, (void*)ctx);
  212. }
  213. #endif
  214. /* Is called for signing
  215. * Can handle only RSA PkCS#1v1.5 padding scheme here.
  216. */
  217. if (info->algo_type == WC_ALGO_TYPE_PK) {
  218. #if !defined(NO_RSA)
  219. if (info->pk.type == WC_PK_TYPE_RSA) {
  220. if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT) {
  221. ret = tsip_SignRsaPkcs(info, ctx);
  222. }
  223. #if defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  224. else if (info->pk.rsa.type == RSA_PUBLIC_DECRYPT /* verify */) {
  225. ret = wc_tsip_RsaVerifyPkcs(info, ctx);
  226. }
  227. #endif
  228. }
  229. #endif /* NO_RSA */
  230. #if defined(HAVE_ECC) && defined(WOLFSSL_RENESAS_TSIP_TLS)
  231. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  232. ret = tsip_SignEcdsa(info, ctx);
  233. }
  234. #endif /* HAVE_ECC */
  235. }
  236. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS) ||\
  237. defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  238. if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  239. #if !defined(NO_AES) || !defined(NO_DES3)
  240. #ifdef HAVE_AESGCM
  241. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  242. if (info->cipher.enc &&
  243. (cbInfo->keyflgs_tls.bits.session_key_set == 1 ||
  244. (cbInfo->keyflgs_crypt.bits.aes256_installedkey_set == 1 &&
  245. info->cipher.aesgcm_enc.aes->keylen == 32) ||
  246. (cbInfo->keyflgs_crypt.bits.aes128_installedkey_set == 1 &&
  247. info->cipher.aesgcm_enc.aes->keylen == 16))) {
  248. ret = wc_fspsm_AesGcmEncrypt(
  249. info->cipher.aesgcm_enc.aes,
  250. (byte*)info->cipher.aesgcm_enc.out,
  251. (byte*)info->cipher.aesgcm_enc.in,
  252. info->cipher.aesgcm_enc.sz,
  253. (byte*)info->cipher.aesgcm_enc.iv,
  254. info->cipher.aesgcm_enc.ivSz,
  255. (byte*)info->cipher.aesgcm_enc.authTag,
  256. info->cipher.aesgcm_enc.authTagSz,
  257. (byte*)info->cipher.aesgcm_enc.authIn,
  258. info->cipher.aesgcm_enc.authInSz,
  259. (void*)ctx);
  260. }
  261. else if (cbInfo->keyflgs_tls.bits.session_key_set == 1 ||
  262. (cbInfo->keyflgs_crypt.bits.aes256_installedkey_set == 1 &&
  263. info->cipher.aesgcm_dec.aes->keylen == 32) ||
  264. (cbInfo->keyflgs_crypt.bits.aes128_installedkey_set == 1 &&
  265. info->cipher.aesgcm_dec.aes->keylen == 16)) {
  266. ret = wc_fspsm_AesGcmDecrypt(
  267. info->cipher.aesgcm_dec.aes,
  268. (byte*)info->cipher.aesgcm_dec.out,
  269. (byte*)info->cipher.aesgcm_dec.in,
  270. info->cipher.aesgcm_dec.sz,
  271. (byte*)info->cipher.aesgcm_dec.iv,
  272. info->cipher.aesgcm_dec.ivSz,
  273. (byte*)info->cipher.aesgcm_dec.authTag,
  274. info->cipher.aesgcm_dec.authTagSz,
  275. (byte*)info->cipher.aesgcm_dec.authIn,
  276. info->cipher.aesgcm_dec.authInSz,
  277. (void*)ctx);
  278. }
  279. }
  280. #endif /* HAVE_AESGCM */
  281. #ifdef HAVE_AES_CBC
  282. if ((info->cipher.type == WC_CIPHER_AES_CBC) &&
  283. (cbInfo->keyflgs_tls.bits.session_key_set == 1 ||
  284. (cbInfo->keyflgs_crypt.bits.aes256_installedkey_set == 1 &&
  285. info->cipher.aescbc.aes->keylen == 32) ||
  286. (cbInfo->keyflgs_crypt.bits.aes128_installedkey_set == 1 &&
  287. info->cipher.aescbc.aes->keylen == 16))) {
  288. if (info->cipher.enc) {
  289. ret = wc_fspsm_AesCbcEncrypt(
  290. info->cipher.aescbc.aes,
  291. (byte*)info->cipher.aescbc.out,
  292. (byte*)info->cipher.aescbc.in,
  293. info->cipher.aescbc.sz);
  294. }
  295. else {
  296. ret = wc_fspsm_AesCbcDecrypt(
  297. info->cipher.aescbc.aes,
  298. (byte*)info->cipher.aescbc.out,
  299. (byte*)info->cipher.aescbc.in,
  300. info->cipher.aescbc.sz);
  301. }
  302. }
  303. #endif /* HAVE_AES_CBC */
  304. #endif /* !NO_AES || !NO_DES3 */
  305. }
  306. #if !defined(NO_RSA) && defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  307. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  308. #if !defined(NO_RSA)
  309. #if defined(WOLFSSL_KEY_GEN)
  310. if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN &&
  311. (info->pk.rsakg.size == 1024 ||
  312. info->pk.rsakg.size == 2048)) {
  313. ret = wc_fspsm_MakeRsaKey(info->pk.rsakg.key,
  314. info->pk.rsakg.size, (void*)ctx);
  315. }
  316. #endif
  317. if (info->pk.type == WC_PK_TYPE_RSA) {
  318. /* to perform RSA on SCE, wrapped keys should be installed
  319. * in advance. SCE supports 1024 or 2048 bits key size.
  320. * otherwise, falls-through happens.
  321. */
  322. if (info->pk.rsa.key->ctx.keySz == 1024 ||
  323. info->pk.rsa.key->ctx.keySz == 2048) {
  324. if (info->pk.rsa.type == RSA_PRIVATE_DECRYPT ||
  325. info->pk.rsa.type == RSA_PUBLIC_ENCRYPT )
  326. {
  327. ret = wc_fspsm_RsaFunction(info->pk.rsa.in,
  328. info->pk.rsa.inLen,
  329. info->pk.rsa.out,
  330. &info->pk.rsa.outLen,
  331. info->pk.rsa.type,
  332. info->pk.rsa.key,
  333. info->pk.rsa.rng);
  334. }
  335. else if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT /* sign */){
  336. ret = wc_fspsm_RsaSign(info->pk.rsa.in,
  337. info->pk.rsa.inLen,
  338. info->pk.rsa.out,
  339. info->pk.rsa.outLen,
  340. info->pk.rsa.key,
  341. (void*)ctx);
  342. }
  343. else if (info->pk.rsa.type == RSA_PUBLIC_DECRYPT /* verify */) {
  344. ret = wc_fspsm_RsaVerify(info->pk.rsa.in,
  345. info->pk.rsa.inLen,
  346. info->pk.rsa.out,
  347. info->pk.rsa.outLen,
  348. info->pk.rsa.key,
  349. (void*)ctx);
  350. }
  351. }
  352. else {
  353. WOLFSSL_MSG(
  354. "SCE can handle 1024 or 2048 bit key size. "
  355. "key size is not either 1024 or 2048. "
  356. "Or wrapped key is not installed. "
  357. "RSA operation falls through to SW operation.");
  358. }
  359. }
  360. #endif /* NO_RSA && WOLFSSL_RENESAS_FSPSM_CRYPTONLY */
  361. }
  362. #endif /* NO_RSA */
  363. #endif /* TSIP or SCE */
  364. (void)devIdArg;
  365. (void)ctx;
  366. WOLFSSL_LEAVE("Renesas_cmn_CryptoDevCb", ret);
  367. return ret;
  368. }
  369. /* Renesas Security Library Common Entry Point
  370. * For usable method
  371. *
  372. * ssl : a pointer to WOLFSSL object
  373. * session_key_generated : if session key has been generated
  374. * return 1 for usable, 0 for unusable
  375. */
  376. int Renesas_cmn_usable(const WOLFSSL* ssl, byte session_key_generated)
  377. {
  378. int ret;
  379. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  380. ret = tsip_usable(ssl, session_key_generated);
  381. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS) ||\
  382. defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  383. ret = wc_fspsm_usable(ssl, session_key_generated);
  384. #endif
  385. return ret;
  386. }
  387. /* Renesas Security Library Common Method
  388. * Get Callback ctx by devId
  389. *
  390. * devId : devId to get its CTX
  391. * return asocciated CTX when the method is successfully called.
  392. * otherwise, NULL
  393. */
  394. WOLFSSL_LOCAL void *Renesas_cmn_GetCbCtxBydevId(int devId)
  395. {
  396. if (devId >= 7890 && devId <= (MAX_FSPSM_CBINDEX + 7890))
  397. return gCbCtx[devId - 7890];
  398. else
  399. return NULL;
  400. }
  401. /* Renesas Security Library Common Method
  402. * Crypt Callback initialization
  403. *
  404. * ssl : a pointer to WOLFSSL object
  405. * ctx : callback context
  406. * return valid device Id on success, otherwise INVALID_DEVIID
  407. * device Id starts from 7890, and increases + 1 its number
  408. * when the method is successfully called.
  409. */
  410. int wc_CryptoCb_CryptInitRenesasCmn(WOLFSSL* ssl, void* ctx)
  411. {
  412. (void)ssl;
  413. (void)ctx;
  414. #if defined(WOLFSSL_RENESAS_TSIP_TLS) \
  415. || defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  416. TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
  417. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
  418. defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  419. FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
  420. #endif
  421. if (cbInfo == NULL
  422. #if (!defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) &&\
  423. !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  424. !defined(HAVE_RENESAS_SYNC)
  425. || ssl == NULL) {
  426. #else
  427. ) {
  428. #endif
  429. printf("Invalid devId\n");
  430. return INVALID_DEVID;
  431. }
  432. /* need exclusive control because of static variable */
  433. if ((cmn_hw_lock()) == 0) {
  434. cbInfo->devId = gdevId++;
  435. cmn_hw_unlock();
  436. }
  437. else {
  438. WOLFSSL_MSG("Failed to lock tsip hw");
  439. return INVALID_DEVID;
  440. }
  441. if (wc_CryptoCb_RegisterDevice(cbInfo->devId,
  442. Renesas_cmn_CryptoDevCb, cbInfo) < 0) {
  443. /* undo devId number */
  444. gdevId--;
  445. return INVALID_DEVID;
  446. }
  447. #if !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
  448. !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) && \
  449. !defined(HAVE_RENESAS_SYNC)
  450. if (ssl)
  451. wolfSSL_SetDevId(ssl, cbInfo->devId);
  452. #endif
  453. /* sanity check for overflow */
  454. if (gdevId < 0) {
  455. gdevId = 7890;
  456. }
  457. gCbCtx[cbInfo->devId - 7890] = (void*)cbInfo;
  458. return cbInfo->devId;
  459. }
  460. /* Renesas Security Library Common Method
  461. * Clean up CryptCb
  462. *
  463. * id : a pointer to device id to unregister
  464. * no return value
  465. */
  466. void wc_CryptoCb_CleanupRenesasCmn(int* id)
  467. {
  468. wc_CryptoCb_UnRegisterDevice(*id);
  469. *id = INVALID_DEVID;
  470. }
  471. #endif /* WOLF_CRYPTO_CB */
  472. #endif /* WOLFSSL_RENESAS_FSPSM_TLS|| WOLFSSL_RENESAS_FSPSM_CRYPTONLY
  473. WOLFSSL_RENESAS_TSIP_TLS || WOLFSSL_RENESAS_TSIP_CRYPTONLY */
  474. #if defined(WOLFSSL_RENESAS_FSPSM_TLS) || defined(WOLFSSL_RENESAS_TSIP_TLS)
  475. /* Renesas Security Library Common Method
  476. * Check CA index if CA can be used for SCE/TSIP because
  477. * the CA has been verified by SCE/TSIP
  478. *
  479. * cmdIdx : ca index
  480. * return 1 can be used, otherwise 0
  481. */
  482. WOLFSSL_LOCAL byte Renesas_cmn_checkCA(word32 cmIdx)
  483. {
  484. WOLFSSL_ENTER("Renesas_cmn_checkCA");
  485. return (cmIdx == g_CAscm_Idx? 1:0);
  486. }
  487. /* check if the root CA has been verified by TSIP/SCE,
  488. * and it exists in the CM table.
  489. */
  490. static byte fspsm_tsip_rootCAverified(void)
  491. {
  492. WOLFSSL_ENTER("fspsm_tsip_rootCAverified");
  493. return (g_CAscm_Idx != (uint32_t)-1 ? 1:0);
  494. }
  495. /* Renesas Security Library Common Callback
  496. * Callback for Rsa verify
  497. *
  498. * ssl the WOLFSSL object
  499. * sig Buffer holding signature
  500. * sigSz Length of signature in bytes
  501. * out Buffer to hold hash
  502. * key Buffer to hold ecc key
  503. * keySz Length of key in bytes
  504. * return FSP_SUCCESS(0) on success, otherwise FSP/TSIP error code
  505. */
  506. WOLFSSL_LOCAL int Renesas_cmn_RsaVerify(WOLFSSL* ssl, unsigned char* sig,
  507. unsigned int sigSz, unsigned char** out,
  508. const unsigned char* key, unsigned int keySz, void* ctx)
  509. {
  510. int ret = 0;
  511. WOLFSSL_ENTER("Renesas_cmn_RsaVerify");
  512. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  513. ret = wc_tsip_RsaVerify(ssl, sig, sigSz, out, key, keySz, ctx);
  514. if (ret == 0) {
  515. /* Set Callback for SharedSecret when successful */
  516. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, wc_tsip_EccSharedSecret);
  517. wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
  518. }
  519. else {
  520. WOLFSSL_MSG("failed wc_tsip_RsaVerify");
  521. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
  522. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  523. }
  524. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  525. ret = wc_fspsm_RsaVerifyTLS(ssl, sig, sigSz, out,key, keySz, ctx);
  526. if (ret == 0) {
  527. /* Set Callback for SharedSecret when successful */
  528. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, fspsm_EccSharedSecret);
  529. wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
  530. }
  531. else {
  532. WOLFSSL_MSG("failed R_XXX_TLS_ServerKeyExchangeVerify");
  533. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
  534. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  535. }
  536. #endif
  537. return ret;
  538. }
  539. /* Renesas Security Library Common Callback
  540. * Callback for Ecc verify
  541. *
  542. * ssl the WOLFSSL object
  543. * sig Buffer holding signature
  544. * sigSz Length of signature in bytes
  545. * hash Buffer to hold hash
  546. * hashSz Length of hash
  547. * key Buffer to hold ecc key
  548. * keySz Length of key in bytes
  549. * result a pointer to int indicates if the verify is ok
  550. * return FSP_SUCCESS(0) on success, otherwise FSP/TSIP error code
  551. */
  552. WOLFSSL_LOCAL int Renesas_cmn_EccVerify(WOLFSSL* ssl, const unsigned char* sig,
  553. unsigned int sigSz, const unsigned char* hash, unsigned int hashSz,
  554. const unsigned char* key, unsigned int keySz, int* result, void* ctx)
  555. {
  556. int ret = 0;
  557. WOLFSSL_ENTER("Renesas_cmn_EccVerify");
  558. #if defined(WOLFSSL_RENESAS_TSIP)
  559. ret = wc_tsip_EccVerify(ssl, sig, sigSz, hash, hashSz, key, keySz,
  560. result, ctx);
  561. if (ret == 0 && *result == 1) {
  562. /* Set callback for SharedSecret when being successful */
  563. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, wc_tsip_EccSharedSecret);
  564. wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
  565. }
  566. else {
  567. WOLFSSL_MSG("failed wc_tsip_EccVerify");
  568. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
  569. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  570. }
  571. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  572. ret = wc_fspsm_EccVerifyTLS(ssl, sig, sigSz, hash, hashSz, key, keySz,
  573. result, ctx);
  574. if (ret == 0 && *result == 1) {
  575. /* Set callback for SharedSecret when being successful */
  576. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, fspsm_EccSharedSecret);
  577. wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
  578. }
  579. else {
  580. WOLFSSL_MSG("failed R_XXXX_TLS_ServerKeyExchangeVerify");
  581. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
  582. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  583. }
  584. #endif
  585. return ret;
  586. }
  587. /* Renesas Security Library Common Entry Point
  588. * For ROOT CA verifycation
  589. *
  590. * cert Buffer to hold cert
  591. * cert_len Length of cert
  592. * key_n_start Byte position of public key in cert
  593. * key_n_len Length of public key in bytes
  594. * key_e_start Byte position of public key exponent in cert
  595. * key_e_len Length of public key exponent
  596. * cm_row CA index
  597. * return FSP_SUCCESS(0) on success, otherwise WOLFSSL_FATAL_ERROR
  598. */
  599. int wc_Renesas_cmn_RootCertVerify(const byte* cert, word32 cert_len,
  600. word32 key_n_start, word32 key_n_len, word32 key_e_start,
  601. word32 key_e_len, word32 cm_row)
  602. {
  603. int ret;
  604. WOLFSSL_ENTER("wc_Renesas_cmn_RootCertVerify");
  605. if (fspsm_tsip_rootCAverified() == 0) {
  606. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  607. ret = wc_tsip_tls_RootCertVerify(cert, cert_len, key_n_start,
  608. key_n_len, key_e_start, key_e_len, cm_row);
  609. if (ret != TSIP_SUCCESS) {
  610. ret = WOLFSSL_FATAL_ERROR;
  611. }
  612. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  613. ret = wc_fspsm_tls_RootCertVerify(cert, cert_len, key_n_start,
  614. key_n_len, key_e_start, key_e_len, cm_row);
  615. if (ret != FSP_SUCCESS) {
  616. ret = WOLFSSL_FATAL_ERROR;
  617. }
  618. #endif
  619. }
  620. else {
  621. /* already verified. skipped */
  622. ret = 0;
  623. }
  624. WOLFSSL_LEAVE("wc_Renesas_cmn_RootCertVerify", ret);
  625. return ret;
  626. }
  627. /* Renesas Security Library Common Callback
  628. * Callback for tls finished
  629. *
  630. * ssl the WOLFSSL object
  631. * side CLIENT or SERVER
  632. * handshake_hash hash while doing handshake
  633. * hashes calculated data by SCE/TSIP pseudo random function
  634. * return FSP_SUCCESS(0) on success, otherwise FSP/TSIP error code
  635. */
  636. WOLFSSL_LOCAL int Renesas_cmn_TlsFinished(WOLFSSL* ssl, const byte *side,
  637. const byte *handshake_hash, word32 hashSz,
  638. byte *hashes, void* ctx)
  639. {
  640. int ret = -1;
  641. (void)hashSz;
  642. (void)ctx;
  643. WOLFSSL_ENTER("Renesas_cmn_TlsFinished");
  644. if (Renesas_cmn_usable(ssl, 1)) {
  645. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  646. ret = wc_tsip_generateVerifyData(ssl->arrays->tsip_masterSecret,
  647. side, handshake_hash, hashes);
  648. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  649. ret = wc_fspsm_generateVerifyData(ssl->arrays->fspsm_masterSecret,
  650. side, handshake_hash, hashes);
  651. #endif
  652. }
  653. else
  654. ret = PROTOCOLCB_UNAVAILABLE;
  655. return ret;
  656. }
  657. /* Renesas Security Library Common Callback
  658. * Callback for setting Encrypt Keys.
  659. * Register callback for setting Encrypt Keys when keys are generated
  660. * by SCE/TSIP
  661. *
  662. * ssl the WOLFSSL object
  663. * ctx Callback context
  664. * return 0 on success, -1 when keys are not generated by SCE/TSIP
  665. */
  666. static int Renesas_cmn_EncryptKeys(WOLFSSL* ssl, void* ctx)
  667. {
  668. int ret;
  669. WOLFSSL_ENTER("Renesas_cmn_EncryptKeys");
  670. /* sanity check */
  671. if (ssl == NULL || ctx == NULL)
  672. return BAD_FUNC_ARG;
  673. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  674. TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
  675. if (cbInfo->session_key_set == 1) {
  676. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  677. FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
  678. if (cbInfo->keyflgs_tls.bits.session_key_set == 1) {
  679. #endif
  680. ret = 0;
  681. wolfSSL_CTX_SetTlsFinishedCb(ssl->ctx, Renesas_cmn_TlsFinished);
  682. wolfSSL_SetTlsFinishedCtx(ssl, cbInfo);
  683. }
  684. else {
  685. wolfSSL_CTX_SetTlsFinishedCb(ssl->ctx, NULL);
  686. wolfSSL_SetTlsFinishedCtx(ssl, NULL);
  687. ret = -1;
  688. }
  689. return ret;
  690. }
  691. /* Renesas Security Library Common Callback
  692. * Callback for Session Key generation
  693. * Register callback for Set Keys when keys are successfully
  694. * generated by SCE/TSIP
  695. *
  696. * ssl the WOLFSSL object
  697. * ctx Callback context
  698. * return FSP_SUCCESS(0) on success, otherwise SCE/TSIP error code
  699. */
  700. WOLFSSL_LOCAL int Renesas_cmn_generateSessionKey(WOLFSSL* ssl, void* ctx)
  701. {
  702. int ret = -1;
  703. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  704. TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
  705. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  706. FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
  707. #endif
  708. (void)ctx;
  709. WOLFSSL_ENTER("Renesas_cmn_generateSessionKey");
  710. if (Renesas_cmn_usable(ssl, 0)) {
  711. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  712. ret = wc_tsip_generateSessionKey(ssl, (TsipUserCtx*)ctx, cbInfo->devId);
  713. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  714. ret = wc_fspsm_generateSessionKey(ssl, ctx, cbInfo->devId);
  715. #endif
  716. }
  717. else {
  718. ret = PROTOCOLCB_UNAVAILABLE;
  719. }
  720. if (ret == 0) {
  721. wolfSSL_CTX_SetEncryptKeysCb(ssl->ctx, Renesas_cmn_EncryptKeys);
  722. wolfSSL_SetEncryptKeysCtx(ssl, ctx);
  723. }
  724. else {
  725. wolfSSL_CTX_SetEncryptKeysCb(ssl->ctx, NULL);
  726. wolfSSL_SetEncryptKeysCtx(ssl, NULL);
  727. }
  728. return ret;
  729. }
  730. /* Renesas Security Library Common Callback
  731. * Callback for Premaster Secret generation
  732. * Register callback for Set Keys when keys are successfully
  733. * generated by SCE/TSIP
  734. *
  735. * ssl the WOLFSSL object
  736. * premaster Buffer to hold pre master
  737. * preSz Length of pre-master
  738. * ctx Callback context
  739. * return FSP_SUCCESS(0) on success,
  740. * otherwise PROTOCOLCB_UNAVAILABLE
  741. * so that caller could continue to process if want
  742. */
  743. WOLFSSL_LOCAL int Renesas_cmn_generatePremasterSecret(WOLFSSL* ssl,
  744. byte *premaster, word32 preSz, void* ctx)
  745. {
  746. int ret;
  747. (void) ctx;
  748. (void) ssl;
  749. WOLFSSL_ENTER("Renesas_cmn_generatePremasterSecret");
  750. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  751. if (Renesas_cmn_usable(ssl, 0)) {
  752. ret = wc_tsip_generatePremasterSecret(premaster, preSz);
  753. ssl->arrays->preMasterSz = preSz;
  754. }
  755. else
  756. ret = PROTOCOLCB_UNAVAILABLE;
  757. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  758. if (Renesas_cmn_usable(ssl, 0)) {
  759. ret = wc_fspsm_generatePremasterSecret(premaster, preSz);
  760. ssl->arrays->preMasterSz = preSz;
  761. }
  762. else
  763. ret = PROTOCOLCB_UNAVAILABLE;
  764. #endif
  765. return ret;
  766. }
  767. /* Renesas Security Library Common Callback
  768. * Callback for Master Secret generation
  769. * Register callback for Session Key Generation when master secret is
  770. * successfully generated by SCE/TSIP
  771. *
  772. * ssl the WOLFSSL object
  773. * ctx Callback context
  774. * return FSP_SUCCESS(0) on success,
  775. * otherwise PROTOCOLCB_UNAVAILABLE
  776. * so that caller could continue to process if want
  777. */
  778. WOLFSSL_LOCAL int Renesas_cmn_genMasterSecret(struct WOLFSSL* ssl, void* ctx)
  779. {
  780. int ret = WOLFSSL_NOT_IMPLEMENTED;
  781. (void) ret;
  782. (void) ctx;
  783. WOLFSSL_ENTER("Renesas_cmn_genMasterSecret");
  784. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  785. if (Renesas_cmn_usable(ssl, 0)) {
  786. #if (WOLFSSL_RENESAS_TSIP_VER >= 109)
  787. ret = wc_tsip_generateMasterSecretEx(
  788. ssl->options.cipherSuite0,
  789. ssl->options.cipherSuite,
  790. ssl->arrays->preMasterSecret,
  791. ssl->arrays->clientRandom,
  792. ssl->arrays->serverRandom,
  793. ssl->arrays->tsip_masterSecret);
  794. #else
  795. ret = wc_tsip_generateMasterSecret(
  796. ssl->arrays->preMasterSecret,
  797. ssl->arrays->clientRandom,
  798. ssl->arrays->serverRandom,
  799. ssl->arrays->tsip_masterSecret);
  800. #endif
  801. if (ret == 0) {
  802. wc_tsip_storeKeyCtx(ssl, (TsipUserCtx*)ctx);
  803. /* set Session Key generation Callback for use */
  804. wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx,
  805. Renesas_cmn_generateSessionKey);
  806. wolfSSL_SetGenSessionKeyCtx(ssl, ctx);
  807. }
  808. else {
  809. wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx, NULL);
  810. wolfSSL_SetGenSessionKeyCtx(ssl, NULL);
  811. }
  812. }
  813. else
  814. ret = PROTOCOLCB_UNAVAILABLE;
  815. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  816. if (Renesas_cmn_usable(ssl, 0)) {
  817. ret = wc_fspsm_generateMasterSecret(
  818. ssl->options.cipherSuite0,
  819. ssl->options.cipherSuite,
  820. ssl->arrays->preMasterSecret,
  821. ssl->arrays->clientRandom,
  822. ssl->arrays->serverRandom,
  823. ssl->arrays->fspsm_masterSecret);
  824. if (ret == 0) {
  825. wc_fspsm_storeKeyCtx(ssl, ctx);
  826. /* set Session Key generation Callback for use */
  827. wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx,
  828. Renesas_cmn_generateSessionKey);
  829. wolfSSL_SetGenSessionKeyCtx(ssl, ctx);
  830. }
  831. else {
  832. wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx, NULL);
  833. wolfSSL_SetGenSessionKeyCtx(ssl, NULL);
  834. }
  835. }
  836. else {
  837. WOLFSSL_MSG("PROTOCOLCB_UNAVAILABLE\n");
  838. ret = PROTOCOLCB_UNAVAILABLE;
  839. }
  840. #endif
  841. return ret;
  842. }
  843. /* Renesas Security Library Common Callback
  844. * Callback for Rsa Encryption
  845. *
  846. * ssl the WOLFSSL object
  847. * in Buffer to hold plain text
  848. * inSz Length of plain text
  849. * out Buffer to hold cipher text
  850. * outSz Length of cipher text buffer
  851. * KeyDer Buffer holding Key in der format
  852. * KeySz Length of Key Der
  853. * ctx Callback context
  854. * return FSP_SUCCESS(0) on success,
  855. * otherwise CRYPTOCB_UNAVAILABLE
  856. * so that caller could continue to process if want
  857. */
  858. WOLFSSL_LOCAL int Renesas_cmn_RsaEnc(WOLFSSL* ssl, const unsigned char* in,
  859. unsigned int inSz, unsigned char* out, word32* outSz,
  860. const unsigned char* keyDer, unsigned int keySz, void* ctx)
  861. {
  862. int ret;
  863. int EncSz;
  864. (void)ctx;
  865. (void)in;
  866. (void)inSz;
  867. (void)keyDer;
  868. (void)keySz;
  869. (void)EncSz;
  870. WOLFSSL_ENTER("Renesas_cmn_RsaEnc");
  871. /* sanity check */
  872. if (ssl == NULL || in == NULL || out == NULL || keyDer == NULL ||
  873. ctx == NULL) {
  874. return BAD_FUNC_ARG;
  875. }
  876. EncSz = wc_RsaEncryptSize(ssl->peerRsaKey);
  877. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  878. if (tsip_usable(ssl, 0)) {
  879. if (EncSz == 256) {
  880. ret = wc_tsip_generateEncryptPreMasterSecret(ssl, out, outSz);
  881. }
  882. else {
  883. WOLFSSL_MSG("TSIP can only handle 256 bytes for RSA encrypt size.");
  884. ret = CRYPTOCB_UNAVAILABLE;
  885. }
  886. }
  887. else {
  888. ret = CRYPTOCB_UNAVAILABLE;
  889. }
  890. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  891. if (wc_fspsm_usable(ssl, 0) && EncSz == 256) {
  892. ret = wc_fspsm_generateEncryptPreMasterSecret(ssl, out,
  893. (uint32_t*)outSz);
  894. }
  895. else {
  896. if (EncSz != 256)
  897. WOLFSSL_MSG("SCE cannot use"
  898. "because Rsa Encrypt Size isn't 256 bytes(2048 bits).");
  899. ret = CRYPTOCB_UNAVAILABLE;
  900. }
  901. #endif
  902. return ret;
  903. }
  904. /* Renesas Security Library Common Callback
  905. * Callback for Verify hmac
  906. *
  907. * ssl the WOLFSSL object
  908. * message Buffer to hold message
  909. * inSz Length of message
  910. * macSz Length of mac size
  911. * content content of inner data
  912. * ctx Callback context
  913. * return FSP_SUCCESS(0) on success,
  914. * otherwise PROTOCOLCB_UNAVAILABLE
  915. * so that caller could continue to process if want
  916. */
  917. WOLFSSL_LOCAL int Renesas_cmn_VerifyHmac(WOLFSSL *ssl, const byte* message,
  918. word32 messageSz, word32 macSz, word32 content, void* ctx)
  919. {
  920. int ret;
  921. (void)ctx;
  922. WOLFSSL_ENTER("Renesas_cmn_VerifyHmac");
  923. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  924. if (tsip_usable(ssl, 1)) {
  925. ret = wc_tsip_ShaXHmacVerify(ssl, message, messageSz, macSz, content);
  926. }
  927. else
  928. ret = PROTOCOLCB_UNAVAILABLE;
  929. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  930. if (wc_fspsm_usable(ssl, 1)) {
  931. ret = wc_fspsm_Sha256VerifyHmac(ssl, message,
  932. messageSz, macSz, content);
  933. }
  934. else
  935. ret = PROTOCOLCB_UNAVAILABLE;
  936. #endif
  937. return ret;
  938. }
  939. #ifndef WOLFSSL_AEAD_ONLY
  940. /* Renesas Security Library Common Callback
  941. * Callback for TLS hmac
  942. *
  943. * ssl the WOLFSSL object
  944. * digest Buffer to hold digest by hmac
  945. * in Buffer to hold in data
  946. * sz Length of in data
  947. * padSz Length of padding
  948. * content content of inner data
  949. * epochOrder
  950. * return FSP_SUCCESS(0) on success, otherwise error code
  951. */
  952. WOLFSSL_LOCAL int Renesas_cmn_TLS_hmac(WOLFSSL* ssl, byte* digest,
  953. const byte* in, word32 sz, int padSz, int content, int verify, int epochOrder)
  954. {
  955. int ret;
  956. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  957. WOLFSSL_ENTER("Renesas_cmn_TLS_hmac");
  958. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  959. if (Renesas_cmn_usable(ssl, 1)) {
  960. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  961. if (ssl->specs.hash_size == WC_SHA_DIGEST_SIZE) {
  962. ret = wc_tsip_Sha1HmacGenerate(ssl, myInner,
  963. WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
  964. }
  965. else if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE) {
  966. ret = wc_tsip_Sha256HmacGenerate(ssl, myInner,
  967. WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
  968. }
  969. else {
  970. ret = TSIP_MAC_DIGSZ_E;
  971. }
  972. }
  973. else {
  974. WOLFSSL_MSG("TLS_hmac is used instead of TSIP");
  975. /* fall through to original TLS hmac method when TSIP cannot be used */
  976. ret = TLS_hmac(ssl, digest, in, sz, padSz, content, verify, epochOrder);
  977. }
  978. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  979. if (Renesas_cmn_usable(ssl, 1)) {
  980. if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE) {
  981. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  982. ret = wc_fspsm_Sha256GenerateHmac(ssl, myInner,
  983. WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
  984. }
  985. else
  986. ret = TSIP_MAC_DIGSZ_E;
  987. }
  988. else {
  989. /* fall through to original TLS hmac method when SCE cannot be used */
  990. ret = TLS_hmac(ssl, digest, in, sz, padSz, content, verify, epochOrder);
  991. }
  992. #endif
  993. return ret;
  994. }
  995. #endif /* !WOLFSSL_AEAD_ONLY */
  996. /* Renesas Security Library Common Callback
  997. * Callback for Signature PK Rsa verify
  998. *
  999. * sig Buffer holding signature
  1000. * sigSz Length of signature in bytes
  1001. * out Buffer to hold hash
  1002. * keyDer Buffer to hold rsa key
  1003. * keySz Length of key in bytes
  1004. * ctx Callback context
  1005. * return FSP_SUCCESS(0) on success,
  1006. * otherwise CRYPTOCB_UNAVAILABLE
  1007. * so that caller could continue to process if want
  1008. */
  1009. WOLFSSL_LOCAL int Renesas_cmn_SigPkCbRsaVerify(unsigned char* sig,
  1010. unsigned int sigSz, unsigned char** out, const unsigned char* keyDer,
  1011. unsigned int keySz, void* ctx)
  1012. {
  1013. int ret;
  1014. CertAttribute* CertAtt;
  1015. (void)out;
  1016. (void)keyDer;
  1017. (void)keySz;
  1018. WOLFSSL_ENTER("Renesas_cmn_SigPkCbRsaVerify");
  1019. /* sanity check */
  1020. if (sig == NULL || out == NULL || keyDer == NULL || ctx == NULL)
  1021. return BAD_FUNC_ARG;
  1022. CertAtt = (CertAttribute*)ctx;
  1023. if (!CertAtt) {
  1024. return CRYPTOCB_UNAVAILABLE;
  1025. }
  1026. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  1027. if (CertAtt->keyIndex != NULL) {
  1028. ret = wc_tsip_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
  1029. CertAtt->pubkey_n_start - CertAtt->certBegin,
  1030. CertAtt->pubkey_n_len - 1,
  1031. CertAtt->pubkey_e_start - CertAtt->certBegin,
  1032. CertAtt->pubkey_e_len -1,
  1033. (uint8_t*)CertAtt->keyIndex);
  1034. if (ret == 0) {
  1035. CertAtt->verifyByTSIP_SCE = 1;
  1036. }
  1037. else {
  1038. WOLFSSL_MSG("RSA Verify by TSIP didn't match");
  1039. ret = ASN_SIG_CONFIRM_E;
  1040. }
  1041. }
  1042. else
  1043. ret = CRYPTOCB_UNAVAILABLE;
  1044. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  1045. if (CertAtt->keyIndex != NULL) {
  1046. ret = wc_fspsm_tls_CertVerify(CertAtt->cert, CertAtt->certSz,
  1047. sig, sigSz,
  1048. CertAtt->pubkey_n_start - CertAtt->certBegin,
  1049. CertAtt->pubkey_n_len - 1,
  1050. CertAtt->pubkey_e_start - CertAtt->certBegin,
  1051. CertAtt->pubkey_e_len -1,
  1052. (uint8_t*)CertAtt->keyIndex);
  1053. if (ret == 0) {
  1054. CertAtt->verifyByTSIP_SCE = 1;
  1055. }
  1056. else {
  1057. WOLFSSL_MSG("RSA Verify by SCE didn't match");
  1058. ret = ASN_SIG_CONFIRM_E;
  1059. }
  1060. }
  1061. else
  1062. ret = CRYPTOCB_UNAVAILABLE;
  1063. #endif
  1064. return ret;
  1065. }
  1066. /* Renesas Security Library Common Callback
  1067. * Callback for Signature PK Ecc verify
  1068. *
  1069. * sig Buffer holding signature
  1070. * sigSz Length of signature in bytes
  1071. * has Buffer to hold hash
  1072. * hashSz Length of hash
  1073. * keyDer Buffer to hold rsa key
  1074. * keySz Length of key in bytes
  1075. * result A pointer to int indicates a result
  1076. * ctx Callback context
  1077. * return FSP_SUCCESS(0) on success,
  1078. * otherwise CRYPTOCB_UNAVAILABLE
  1079. * so that caller could continue to process if want
  1080. */
  1081. WOLFSSL_LOCAL int Renesas_cmn_SigPkCbEccVerify(const unsigned char* sig,
  1082. unsigned int sigSz, const unsigned char* hash, unsigned int hashSz,
  1083. const unsigned char* keyDer, unsigned int keySz,
  1084. int* result, void* ctx)
  1085. {
  1086. int ret;
  1087. CertAttribute* CertAtt;
  1088. (void)result;
  1089. (void)keyDer;
  1090. (void)keySz;
  1091. (void)hash;
  1092. (void)hashSz;
  1093. WOLFSSL_ENTER("Renesas_cmn_SigPkCbEccVerify");
  1094. /* sanity check */
  1095. if (sig == NULL || keyDer == NULL || hash == NULL || ctx == NULL ||
  1096. result == NULL)
  1097. return BAD_FUNC_ARG;
  1098. CertAtt = (CertAttribute*)ctx;
  1099. if (!CertAtt) {
  1100. return CRYPTOCB_UNAVAILABLE;
  1101. }
  1102. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  1103. if (CertAtt->keyIndex != NULL) {
  1104. ret = wc_tsip_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
  1105. CertAtt->pubkey_n_start - CertAtt->certBegin,
  1106. CertAtt->pubkey_n_len - 1,
  1107. CertAtt->pubkey_e_start - CertAtt->certBegin,
  1108. CertAtt->pubkey_e_len -1,
  1109. (uint8_t*)CertAtt->keyIndex);
  1110. if (ret == 0) {
  1111. CertAtt->verifyByTSIP_SCE = 1;
  1112. *result = 1;
  1113. }
  1114. else {
  1115. WOLFSSL_MSG("RSA Verify by TSIP didn't match");
  1116. ret = ASN_SIG_CONFIRM_E;
  1117. }
  1118. }
  1119. else
  1120. ret = CRYPTOCB_UNAVAILABLE;
  1121. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  1122. if (CertAtt->keyIndex != NULL) {
  1123. ret = wc_fspsm_tls_CertVerify(CertAtt->cert, CertAtt->certSz,
  1124. sig, sigSz,
  1125. CertAtt->pubkey_n_start - CertAtt->certBegin,
  1126. CertAtt->pubkey_n_len - 1,
  1127. CertAtt->pubkey_e_start - CertAtt->certBegin,
  1128. CertAtt->pubkey_e_len -1,
  1129. (uint8_t*)CertAtt->keyIndex);
  1130. if (ret == 0) {
  1131. CertAtt->verifyByTSIP_SCE = 1;
  1132. *result = 1;
  1133. }
  1134. else {
  1135. WOLFSSL_MSG("RSA Verify by SCE didn't match");
  1136. ret = ASN_SIG_CONFIRM_E;
  1137. }
  1138. }
  1139. else
  1140. ret = CRYPTOCB_UNAVAILABLE;
  1141. #endif
  1142. return ret;
  1143. }
  1144. #endif /* SCEPROTECT || TSIP */