renesas_common.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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 (cbInfo->keyflgs_crypt.bits.rsapri2048_installedkey_set ||
  323. cbInfo->keyflgs_crypt.bits.rsapub2048_installedkey_set ||
  324. cbInfo->keyflgs_crypt.bits.rsapri1024_installedkey_set ||
  325. cbInfo->keyflgs_crypt.bits.rsapub1024_installedkey_set ) {
  326. ret = wc_fspsm_MakeRsaKey(info->pk.rsa.key, 0, cbInfo);
  327. if (ret == CRYPTOCB_UNAVAILABLE)
  328. return ret;
  329. if (info->pk.rsa.type == RSA_PRIVATE_DECRYPT ||
  330. info->pk.rsa.type == RSA_PUBLIC_ENCRYPT )
  331. {
  332. ret = wc_fspsm_RsaFunction(info->pk.rsa.in,
  333. info->pk.rsa.inLen,
  334. info->pk.rsa.out,
  335. info->pk.rsa.outLen,
  336. info->pk.rsa.type,
  337. info->pk.rsa.key,
  338. info->pk.rsa.rng);
  339. }
  340. else if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT /* sign */){
  341. ret = wc_fspsm_RsaSign(info->pk.rsa.in,
  342. info->pk.rsa.inLen,
  343. info->pk.rsa.out,
  344. info->pk.rsa.outLen,
  345. info->pk.rsa.key,
  346. (void*)ctx);
  347. }
  348. else if (info->pk.rsa.type == RSA_PUBLIC_DECRYPT /* verify */) {
  349. ret = wc_fspsm_RsaVerify(info->pk.rsa.in,
  350. info->pk.rsa.inLen,
  351. info->pk.rsa.out,
  352. info->pk.rsa.outLen,
  353. info->pk.rsa.key,
  354. (void*)ctx);
  355. }
  356. }
  357. else {
  358. WOLFSSL_MSG(
  359. "SCE can handle 1024 or 2048 bit key size. "
  360. "key size is not either 1024 or 2048. "
  361. "Or wrapped key is not installed. "
  362. "RSA operation falls through to SW operation.");
  363. }
  364. }
  365. #endif /* NO_RSA && WOLFSSL_RENESAS_FSPSM_CRYPTONLY */
  366. }
  367. #endif /* NO_RSA */
  368. #endif /* TSIP or SCE */
  369. (void)devIdArg;
  370. (void)ctx;
  371. WOLFSSL_LEAVE("Renesas_cmn_CryptoDevCb", ret);
  372. return ret;
  373. }
  374. /* Renesas Security Library Common Entry Point
  375. * For usable method
  376. *
  377. * ssl : a pointer to WOLFSSL object
  378. * session_key_generated : if session key has been generated
  379. * return 1 for usable, 0 for unusable
  380. */
  381. int Renesas_cmn_usable(const WOLFSSL* ssl, byte session_key_generated)
  382. {
  383. int ret;
  384. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  385. ret = tsip_usable(ssl, session_key_generated);
  386. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS) ||\
  387. defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  388. ret = wc_fspsm_usable(ssl, session_key_generated);
  389. #endif
  390. return ret;
  391. }
  392. /* Renesas Security Library Common Method
  393. * Get Callback ctx by devId
  394. *
  395. * devId : devId to get its CTX
  396. * return asocciated CTX when the method is successfully called.
  397. * otherwise, NULL
  398. */
  399. WOLFSSL_LOCAL void *Renesas_cmn_GetCbCtxBydevId(int devId)
  400. {
  401. if (devId >= 7890 && devId <= (MAX_FSPSM_CBINDEX + 7890))
  402. return gCbCtx[devId - 7890];
  403. else
  404. return NULL;
  405. }
  406. /* Renesas Security Library Common Method
  407. * Crypt Callback initialization
  408. *
  409. * ssl : a pointer to WOLFSSL object
  410. * ctx : callback context
  411. * return valid device Id on success, otherwise INVALID_DEVIID
  412. * device Id starts from 7890, and increases + 1 its number
  413. * when the method is successfully called.
  414. */
  415. int wc_CryptoCb_CryptInitRenesasCmn(WOLFSSL* ssl, void* ctx)
  416. {
  417. (void)ssl;
  418. (void)ctx;
  419. #if defined(WOLFSSL_RENESAS_TSIP_TLS) \
  420. || defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  421. TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
  422. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS) || \
  423. defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
  424. FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
  425. #endif
  426. if (cbInfo == NULL
  427. #if (!defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) &&\
  428. !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  429. !defined(HAVE_RENESAS_SYNC)
  430. || ssl == NULL) {
  431. #else
  432. ) {
  433. #endif
  434. printf("Invalid devId\n");
  435. return INVALID_DEVID;
  436. }
  437. /* need exclusive control because of static variable */
  438. if ((cmn_hw_lock()) == 0) {
  439. cbInfo->devId = gdevId++;
  440. cmn_hw_unlock();
  441. }
  442. else {
  443. WOLFSSL_MSG("Failed to lock tsip hw");
  444. return INVALID_DEVID;
  445. }
  446. if (wc_CryptoCb_RegisterDevice(cbInfo->devId,
  447. Renesas_cmn_CryptoDevCb, cbInfo) < 0) {
  448. /* undo devId number */
  449. gdevId--;
  450. return INVALID_DEVID;
  451. }
  452. #if !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
  453. !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) && \
  454. !defined(HAVE_RENESAS_SYNC)
  455. if (ssl)
  456. wolfSSL_SetDevId(ssl, cbInfo->devId);
  457. #endif
  458. /* sanity check for overflow */
  459. if (gdevId < 0) {
  460. gdevId = 7890;
  461. }
  462. gCbCtx[cbInfo->devId - 7890] = (void*)cbInfo;
  463. return cbInfo->devId;
  464. }
  465. /* Renesas Security Library Common Method
  466. * Clean up CryptCb
  467. *
  468. * id : a pointer to device id to unregister
  469. * no return value
  470. */
  471. void wc_CryptoCb_CleanupRenesasCmn(int* id)
  472. {
  473. wc_CryptoCb_UnRegisterDevice(*id);
  474. *id = INVALID_DEVID;
  475. }
  476. #endif /* WOLF_CRYPTO_CB */
  477. #endif /* WOLFSSL_RENESAS_FSPSM_TLS|| WOLFSSL_RENESAS_FSPSM_CRYPTONLY
  478. WOLFSSL_RENESAS_TSIP_TLS || WOLFSSL_RENESAS_TSIP_CRYPTONLY */
  479. #if defined(WOLFSSL_RENESAS_FSPSM_TLS) || defined(WOLFSSL_RENESAS_TSIP_TLS)
  480. /* Renesas Security Library Common Method
  481. * Check CA index if CA can be used for SCE/TSIP because
  482. * the CA has been verified by SCE/TSIP
  483. *
  484. * cmdIdx : ca index
  485. * return 1 can be used, otherwise 0
  486. */
  487. WOLFSSL_LOCAL byte Renesas_cmn_checkCA(word32 cmIdx)
  488. {
  489. WOLFSSL_ENTER("Renesas_cmn_checkCA");
  490. return (cmIdx == g_CAscm_Idx? 1:0);
  491. }
  492. /* check if the root CA has been verified by TSIP/SCE,
  493. * and it exists in the CM table.
  494. */
  495. static byte fspsm_tsip_rootCAverified(void)
  496. {
  497. WOLFSSL_ENTER("fspsm_tsip_rootCAverified");
  498. return (g_CAscm_Idx != (uint32_t)-1 ? 1:0);
  499. }
  500. /* Renesas Security Library Common Callback
  501. * Callback for Rsa verify
  502. *
  503. * ssl the WOLFSSL object
  504. * sig Buffer holding signature
  505. * sigSz Length of signature in bytes
  506. * out Buffer to hold hash
  507. * key Buffer to hold ecc key
  508. * keySz Length of key in bytes
  509. * return FSP_SUCCESS(0) on success, otherwise FSP/TSIP error code
  510. */
  511. WOLFSSL_LOCAL int Renesas_cmn_RsaVerify(WOLFSSL* ssl, unsigned char* sig,
  512. unsigned int sigSz, unsigned char** out,
  513. const unsigned char* key, unsigned int keySz, void* ctx)
  514. {
  515. int ret = 0;
  516. WOLFSSL_ENTER("Renesas_cmn_RsaVerify");
  517. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  518. ret = wc_tsip_RsaVerify(ssl, sig, sigSz, out, key, keySz, ctx);
  519. if (ret == 0) {
  520. /* Set Callback for SharedSecret when successful */
  521. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, wc_tsip_EccSharedSecret);
  522. wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
  523. }
  524. else {
  525. WOLFSSL_MSG("failed wc_tsip_RsaVerify");
  526. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
  527. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  528. }
  529. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  530. ret = wc_fspsm_RsaVerifyTLS(ssl, sig, sigSz, out,key, keySz, ctx);
  531. if (ret == 0) {
  532. /* Set Callback for SharedSecret when successful */
  533. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, fspsm_EccSharedSecret);
  534. wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
  535. }
  536. else {
  537. WOLFSSL_MSG("failed R_XXX_TLS_ServerKeyExchangeVerify");
  538. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
  539. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  540. }
  541. #endif
  542. return ret;
  543. }
  544. /* Renesas Security Library Common Callback
  545. * Callback for Ecc verify
  546. *
  547. * ssl the WOLFSSL object
  548. * sig Buffer holding signature
  549. * sigSz Length of signature in bytes
  550. * hash Buffer to hold hash
  551. * hashSz Length of hash
  552. * key Buffer to hold ecc key
  553. * keySz Length of key in bytes
  554. * result a pointer to int indicates if the verify is ok
  555. * return FSP_SUCCESS(0) on success, otherwise FSP/TSIP error code
  556. */
  557. WOLFSSL_LOCAL int Renesas_cmn_EccVerify(WOLFSSL* ssl, const unsigned char* sig,
  558. unsigned int sigSz, const unsigned char* hash, unsigned int hashSz,
  559. const unsigned char* key, unsigned int keySz, int* result, void* ctx)
  560. {
  561. int ret = 0;
  562. WOLFSSL_ENTER("Renesas_cmn_EccVerify");
  563. #if defined(WOLFSSL_RENESAS_TSIP)
  564. ret = wc_tsip_EccVerify(ssl, sig, sigSz, hash, hashSz, key, keySz,
  565. result, ctx);
  566. if (ret == 0 && *result == 1) {
  567. /* Set callback for SharedSecret when being successful */
  568. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, wc_tsip_EccSharedSecret);
  569. wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
  570. }
  571. else {
  572. WOLFSSL_MSG("failed wc_tsip_EccVerify");
  573. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
  574. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  575. }
  576. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  577. ret = wc_fspsm_EccVerifyTLS(ssl, sig, sigSz, hash, hashSz, key, keySz,
  578. result, ctx);
  579. if (ret == 0 && *result == 1) {
  580. /* Set callback for SharedSecret when being successful */
  581. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, fspsm_EccSharedSecret);
  582. wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
  583. }
  584. else {
  585. WOLFSSL_MSG("failed R_XXXX_TLS_ServerKeyExchangeVerify");
  586. wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, NULL);
  587. wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
  588. }
  589. #endif
  590. return ret;
  591. }
  592. /* Renesas Security Library Common Entry Point
  593. * For ROOT CA verifycation
  594. *
  595. * cert Buffer to hold cert
  596. * cert_len Length of cert
  597. * key_n_start Byte position of public key in cert
  598. * key_n_len Length of public key in bytes
  599. * key_e_start Byte position of public key exponent in cert
  600. * key_e_len Length of public key exponent
  601. * cm_row CA index
  602. * return FSP_SUCCESS(0) on success, otherwise WOLFSSL_FATAL_ERROR
  603. */
  604. int wc_Renesas_cmn_RootCertVerify(const byte* cert, word32 cert_len,
  605. word32 key_n_start, word32 key_n_len, word32 key_e_start,
  606. word32 key_e_len, word32 cm_row)
  607. {
  608. int ret;
  609. WOLFSSL_ENTER("wc_Renesas_cmn_RootCertVerify");
  610. if (fspsm_tsip_rootCAverified() == 0) {
  611. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  612. ret = wc_tsip_tls_RootCertVerify(cert, cert_len, key_n_start,
  613. key_n_len, key_e_start, key_e_len, cm_row);
  614. if (ret != TSIP_SUCCESS) {
  615. ret = WOLFSSL_FATAL_ERROR;
  616. }
  617. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  618. ret = wc_fspsm_tls_RootCertVerify(cert, cert_len, key_n_start,
  619. key_n_len, key_e_start, key_e_len, cm_row);
  620. if (ret != FSP_SUCCESS) {
  621. ret = WOLFSSL_FATAL_ERROR;
  622. }
  623. #endif
  624. }
  625. else {
  626. /* already verified. skipped */
  627. ret = 0;
  628. }
  629. WOLFSSL_LEAVE("wc_Renesas_cmn_RootCertVerify", ret);
  630. return ret;
  631. }
  632. /* Renesas Security Library Common Callback
  633. * Callback for tls finished
  634. *
  635. * ssl the WOLFSSL object
  636. * side CLIENT or SERVER
  637. * handshake_hash hash while doing handshake
  638. * hashes calculated data by SCE/TSIP pseudo random function
  639. * return FSP_SUCCESS(0) on success, otherwise FSP/TSIP error code
  640. */
  641. WOLFSSL_LOCAL int Renesas_cmn_TlsFinished(WOLFSSL* ssl, const byte *side,
  642. const byte *handshake_hash, word32 hashSz,
  643. byte *hashes, void* ctx)
  644. {
  645. int ret = -1;
  646. (void)hashSz;
  647. (void)ctx;
  648. WOLFSSL_ENTER("Renesas_cmn_TlsFinished");
  649. if (Renesas_cmn_usable(ssl, 1)) {
  650. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  651. ret = wc_tsip_generateVerifyData(ssl->arrays->tsip_masterSecret,
  652. side, handshake_hash, hashes);
  653. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  654. ret = wc_fspsm_generateVerifyData(ssl->arrays->fspsm_masterSecret,
  655. side, handshake_hash, hashes);
  656. #endif
  657. }
  658. else
  659. ret = PROTOCOLCB_UNAVAILABLE;
  660. return ret;
  661. }
  662. /* Renesas Security Library Common Callback
  663. * Callback for setting Encrypt Keys.
  664. * Register callback for setting Encrypt Keys when keys are generated
  665. * by SCE/TSIP
  666. *
  667. * ssl the WOLFSSL object
  668. * ctx Callback context
  669. * return 0 on success, -1 when keys are not generated by SCE/TSIP
  670. */
  671. static int Renesas_cmn_EncryptKeys(WOLFSSL* ssl, void* ctx)
  672. {
  673. int ret;
  674. WOLFSSL_ENTER("Renesas_cmn_EncryptKeys");
  675. /* sanity check */
  676. if (ssl == NULL || ctx == NULL)
  677. return BAD_FUNC_ARG;
  678. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  679. TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
  680. if (cbInfo->session_key_set == 1) {
  681. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  682. FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
  683. if (cbInfo->keyflgs_tls.bits.session_key_set == 1) {
  684. #endif
  685. ret = 0;
  686. wolfSSL_CTX_SetTlsFinishedCb(ssl->ctx, Renesas_cmn_TlsFinished);
  687. wolfSSL_SetTlsFinishedCtx(ssl, cbInfo);
  688. }
  689. else {
  690. wolfSSL_CTX_SetTlsFinishedCb(ssl->ctx, NULL);
  691. wolfSSL_SetTlsFinishedCtx(ssl, NULL);
  692. ret = -1;
  693. }
  694. return ret;
  695. }
  696. /* Renesas Security Library Common Callback
  697. * Callback for Session Key generation
  698. * Register callback for Set Keys when keys are successfully
  699. * generated by SCE/TSIP
  700. *
  701. * ssl the WOLFSSL object
  702. * ctx Callback context
  703. * return FSP_SUCCESS(0) on success, otherwise SCE/TSIP error code
  704. */
  705. WOLFSSL_LOCAL int Renesas_cmn_generateSessionKey(WOLFSSL* ssl, void* ctx)
  706. {
  707. int ret = -1;
  708. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  709. TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
  710. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  711. FSPSM_ST* cbInfo = (FSPSM_ST*)ctx;
  712. #endif
  713. (void)ctx;
  714. WOLFSSL_ENTER("Renesas_cmn_generateSessionKey");
  715. if (Renesas_cmn_usable(ssl, 0)) {
  716. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  717. ret = wc_tsip_generateSessionKey(ssl, (TsipUserCtx*)ctx, cbInfo->devId);
  718. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  719. ret = wc_fspsm_generateSessionKey(ssl, ctx, cbInfo->devId);
  720. #endif
  721. }
  722. else {
  723. ret = PROTOCOLCB_UNAVAILABLE;
  724. }
  725. if (ret == 0) {
  726. wolfSSL_CTX_SetEncryptKeysCb(ssl->ctx, Renesas_cmn_EncryptKeys);
  727. wolfSSL_SetEncryptKeysCtx(ssl, ctx);
  728. }
  729. else {
  730. wolfSSL_CTX_SetEncryptKeysCb(ssl->ctx, NULL);
  731. wolfSSL_SetEncryptKeysCtx(ssl, NULL);
  732. }
  733. return ret;
  734. }
  735. /* Renesas Security Library Common Callback
  736. * Callback for Premaster Secret generation
  737. * Register callback for Set Keys when keys are successfully
  738. * generated by SCE/TSIP
  739. *
  740. * ssl the WOLFSSL object
  741. * premaster Buffer to hold pre master
  742. * preSz Length of pre-master
  743. * ctx Callback context
  744. * return FSP_SUCCESS(0) on success,
  745. * otherwise PROTOCOLCB_UNAVAILABLE
  746. * so that caller could continue to process if want
  747. */
  748. WOLFSSL_LOCAL int Renesas_cmn_generatePremasterSecret(WOLFSSL* ssl,
  749. byte *premaster, word32 preSz, void* ctx)
  750. {
  751. int ret;
  752. (void) ctx;
  753. (void) ssl;
  754. WOLFSSL_ENTER("Renesas_cmn_generatePremasterSecret");
  755. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  756. if (Renesas_cmn_usable(ssl, 0)) {
  757. ret = wc_tsip_generatePremasterSecret(premaster, preSz);
  758. ssl->arrays->preMasterSz = preSz;
  759. }
  760. else
  761. ret = PROTOCOLCB_UNAVAILABLE;
  762. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  763. if (Renesas_cmn_usable(ssl, 0)) {
  764. ret = wc_fspsm_generatePremasterSecret(premaster, preSz);
  765. ssl->arrays->preMasterSz = preSz;
  766. }
  767. else
  768. ret = PROTOCOLCB_UNAVAILABLE;
  769. #endif
  770. return ret;
  771. }
  772. /* Renesas Security Library Common Callback
  773. * Callback for Master Secret generation
  774. * Register callback for Session Key Generation when master secret is
  775. * successfully generated by SCE/TSIP
  776. *
  777. * ssl the WOLFSSL object
  778. * ctx Callback context
  779. * return FSP_SUCCESS(0) on success,
  780. * otherwise PROTOCOLCB_UNAVAILABLE
  781. * so that caller could continue to process if want
  782. */
  783. WOLFSSL_LOCAL int Renesas_cmn_genMasterSecret(struct WOLFSSL* ssl, void* ctx)
  784. {
  785. int ret = WOLFSSL_NOT_IMPLEMENTED;
  786. (void) ret;
  787. (void) ctx;
  788. WOLFSSL_ENTER("Renesas_cmn_genMasterSecret");
  789. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  790. if (Renesas_cmn_usable(ssl, 0)) {
  791. #if (WOLFSSL_RENESAS_TSIP_VER >= 109)
  792. ret = wc_tsip_generateMasterSecretEx(
  793. ssl->options.cipherSuite0,
  794. ssl->options.cipherSuite,
  795. ssl->arrays->preMasterSecret,
  796. ssl->arrays->clientRandom,
  797. ssl->arrays->serverRandom,
  798. ssl->arrays->tsip_masterSecret);
  799. #else
  800. ret = wc_tsip_generateMasterSecret(
  801. ssl->arrays->preMasterSecret,
  802. ssl->arrays->clientRandom,
  803. ssl->arrays->serverRandom,
  804. ssl->arrays->tsip_masterSecret);
  805. #endif
  806. if (ret == 0) {
  807. wc_tsip_storeKeyCtx(ssl, (TsipUserCtx*)ctx);
  808. /* set Session Key generation Callback for use */
  809. wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx,
  810. Renesas_cmn_generateSessionKey);
  811. wolfSSL_SetGenSessionKeyCtx(ssl, ctx);
  812. }
  813. else {
  814. wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx, NULL);
  815. wolfSSL_SetGenSessionKeyCtx(ssl, NULL);
  816. }
  817. }
  818. else
  819. ret = PROTOCOLCB_UNAVAILABLE;
  820. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  821. if (Renesas_cmn_usable(ssl, 0)) {
  822. ret = wc_fspsm_generateMasterSecret(
  823. ssl->options.cipherSuite0,
  824. ssl->options.cipherSuite,
  825. ssl->arrays->preMasterSecret,
  826. ssl->arrays->clientRandom,
  827. ssl->arrays->serverRandom,
  828. ssl->arrays->fspsm_masterSecret);
  829. if (ret == 0) {
  830. wc_fspsm_storeKeyCtx(ssl, ctx);
  831. /* set Session Key generation Callback for use */
  832. wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx,
  833. Renesas_cmn_generateSessionKey);
  834. wolfSSL_SetGenSessionKeyCtx(ssl, ctx);
  835. }
  836. else {
  837. wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx, NULL);
  838. wolfSSL_SetGenSessionKeyCtx(ssl, NULL);
  839. }
  840. }
  841. else {
  842. WOLFSSL_MSG("PROTOCOLCB_UNAVAILABLE\n");
  843. ret = PROTOCOLCB_UNAVAILABLE;
  844. }
  845. #endif
  846. return ret;
  847. }
  848. /* Renesas Security Library Common Callback
  849. * Callback for Rsa Encryption
  850. *
  851. * ssl the WOLFSSL object
  852. * in Buffer to hold plain text
  853. * inSz Length of plain text
  854. * out Buffer to hold cipher text
  855. * outSz Length of cipher text buffer
  856. * KeyDer Buffer holding Key in der format
  857. * KeySz Length of Key Der
  858. * ctx Callback context
  859. * return FSP_SUCCESS(0) on success,
  860. * otherwise CRYPTOCB_UNAVAILABLE
  861. * so that caller could continue to process if want
  862. */
  863. WOLFSSL_LOCAL int Renesas_cmn_RsaEnc(WOLFSSL* ssl, const unsigned char* in,
  864. unsigned int inSz, unsigned char* out, word32* outSz,
  865. const unsigned char* keyDer, unsigned int keySz, void* ctx)
  866. {
  867. int ret;
  868. int EncSz;
  869. (void)ctx;
  870. (void)in;
  871. (void)inSz;
  872. (void)keyDer;
  873. (void)keySz;
  874. (void)EncSz;
  875. WOLFSSL_ENTER("Renesas_cmn_RsaEnc");
  876. /* sanity check */
  877. if (ssl == NULL || in == NULL || out == NULL || keyDer == NULL ||
  878. ctx == NULL) {
  879. return BAD_FUNC_ARG;
  880. }
  881. EncSz = wc_RsaEncryptSize(ssl->peerRsaKey);
  882. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  883. if (tsip_usable(ssl, 0)) {
  884. if (EncSz == 256) {
  885. ret = wc_tsip_generateEncryptPreMasterSecret(ssl, out, outSz);
  886. }
  887. else {
  888. WOLFSSL_MSG("TSIP can only handle 256 bytes for RSA encrypt size.");
  889. ret = CRYPTOCB_UNAVAILABLE;
  890. }
  891. }
  892. else {
  893. ret = CRYPTOCB_UNAVAILABLE;
  894. }
  895. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  896. if (wc_fspsm_usable(ssl, 0) && EncSz == 256) {
  897. ret = wc_fspsm_generateEncryptPreMasterSecret(ssl, out,
  898. (uint32_t*)outSz);
  899. }
  900. else {
  901. if (EncSz != 256)
  902. WOLFSSL_MSG("SCE cannot use"
  903. "because Rsa Encrypt Size isn't 256 bytes(2048 bits).");
  904. ret = CRYPTOCB_UNAVAILABLE;
  905. }
  906. #endif
  907. return ret;
  908. }
  909. /* Renesas Security Library Common Callback
  910. * Callback for Verify hmac
  911. *
  912. * ssl the WOLFSSL object
  913. * message Buffer to hold message
  914. * inSz Length of message
  915. * macSz Length of mac size
  916. * content content of inner data
  917. * ctx Callback context
  918. * return FSP_SUCCESS(0) on success,
  919. * otherwise PROTOCOLCB_UNAVAILABLE
  920. * so that caller could continue to process if want
  921. */
  922. WOLFSSL_LOCAL int Renesas_cmn_VerifyHmac(WOLFSSL *ssl, const byte* message,
  923. word32 messageSz, word32 macSz, word32 content, void* ctx)
  924. {
  925. int ret;
  926. (void)ctx;
  927. WOLFSSL_ENTER("Renesas_cmn_VerifyHmac");
  928. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  929. if (tsip_usable(ssl, 1)) {
  930. ret = wc_tsip_ShaXHmacVerify(ssl, message, messageSz, macSz, content);
  931. }
  932. else
  933. ret = PROTOCOLCB_UNAVAILABLE;
  934. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  935. if (wc_fspsm_usable(ssl, 1)) {
  936. ret = wc_fspsm_Sha256VerifyHmac(ssl, message,
  937. messageSz, macSz, content);
  938. }
  939. else
  940. ret = PROTOCOLCB_UNAVAILABLE;
  941. #endif
  942. return ret;
  943. }
  944. #ifndef WOLFSSL_AEAD_ONLY
  945. /* Renesas Security Library Common Callback
  946. * Callback for TLS hmac
  947. *
  948. * ssl the WOLFSSL object
  949. * digest Buffer to hold digest by hmac
  950. * in Buffer to hold in data
  951. * sz Length of in data
  952. * padSz Length of padding
  953. * content content of inner data
  954. * epochOrder
  955. * return FSP_SUCCESS(0) on success, otherwise error code
  956. */
  957. WOLFSSL_LOCAL int Renesas_cmn_TLS_hmac(WOLFSSL* ssl, byte* digest,
  958. const byte* in, word32 sz, int padSz, int content, int verify, int epochOrder)
  959. {
  960. int ret;
  961. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  962. WOLFSSL_ENTER("Renesas_cmn_TLS_hmac");
  963. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  964. if (Renesas_cmn_usable(ssl, 1)) {
  965. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  966. if (ssl->specs.hash_size == WC_SHA_DIGEST_SIZE) {
  967. ret = wc_tsip_Sha1HmacGenerate(ssl, myInner,
  968. WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
  969. }
  970. else if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE) {
  971. ret = wc_tsip_Sha256HmacGenerate(ssl, myInner,
  972. WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
  973. }
  974. else {
  975. ret = TSIP_MAC_DIGSZ_E;
  976. }
  977. }
  978. else {
  979. WOLFSSL_MSG("TLS_hmac is used instead of TSIP");
  980. /* fall through to original TLS hmac method when TSIP cannot be used */
  981. ret = TLS_hmac(ssl, digest, in, sz, padSz, content, verify, epochOrder);
  982. }
  983. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  984. if (Renesas_cmn_usable(ssl, 1)) {
  985. if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE) {
  986. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  987. ret = wc_fspsm_Sha256GenerateHmac(ssl, myInner,
  988. WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
  989. }
  990. else
  991. ret = TSIP_MAC_DIGSZ_E;
  992. }
  993. else {
  994. /* fall through to original TLS hmac method when SCE cannot be used */
  995. ret = TLS_hmac(ssl, digest, in, sz, padSz, content, verify, epochOrder);
  996. }
  997. #endif
  998. return ret;
  999. }
  1000. #endif /* !WOLFSSL_AEAD_ONLY */
  1001. /* Renesas Security Library Common Callback
  1002. * Callback for Signature PK Rsa verify
  1003. *
  1004. * sig Buffer holding signature
  1005. * sigSz Length of signature in bytes
  1006. * out Buffer to hold hash
  1007. * keyDer Buffer to hold rsa key
  1008. * keySz Length of key in bytes
  1009. * ctx Callback context
  1010. * return FSP_SUCCESS(0) on success,
  1011. * otherwise CRYPTOCB_UNAVAILABLE
  1012. * so that caller could continue to process if want
  1013. */
  1014. WOLFSSL_LOCAL int Renesas_cmn_SigPkCbRsaVerify(unsigned char* sig,
  1015. unsigned int sigSz, unsigned char** out, const unsigned char* keyDer,
  1016. unsigned int keySz, void* ctx)
  1017. {
  1018. int ret;
  1019. CertAttribute* CertAtt;
  1020. (void)out;
  1021. (void)keyDer;
  1022. (void)keySz;
  1023. WOLFSSL_ENTER("Renesas_cmn_SigPkCbRsaVerify");
  1024. /* sanity check */
  1025. if (sig == NULL || out == NULL || keyDer == NULL || ctx == NULL)
  1026. return BAD_FUNC_ARG;
  1027. CertAtt = (CertAttribute*)ctx;
  1028. if (!CertAtt) {
  1029. return CRYPTOCB_UNAVAILABLE;
  1030. }
  1031. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  1032. if (CertAtt->keyIndex != NULL) {
  1033. ret = wc_tsip_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
  1034. CertAtt->pubkey_n_start - CertAtt->certBegin,
  1035. CertAtt->pubkey_n_len - 1,
  1036. CertAtt->pubkey_e_start - CertAtt->certBegin,
  1037. CertAtt->pubkey_e_len -1,
  1038. (uint8_t*)CertAtt->keyIndex);
  1039. if (ret == 0) {
  1040. CertAtt->verifyByTSIP_SCE = 1;
  1041. }
  1042. else {
  1043. WOLFSSL_MSG("RSA Verify by TSIP didn't match");
  1044. ret = ASN_SIG_CONFIRM_E;
  1045. }
  1046. }
  1047. else
  1048. ret = CRYPTOCB_UNAVAILABLE;
  1049. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  1050. if (CertAtt->keyIndex != NULL) {
  1051. ret = wc_fspsm_tls_CertVerify(CertAtt->cert, CertAtt->certSz,
  1052. sig, sigSz,
  1053. CertAtt->pubkey_n_start - CertAtt->certBegin,
  1054. CertAtt->pubkey_n_len - 1,
  1055. CertAtt->pubkey_e_start - CertAtt->certBegin,
  1056. CertAtt->pubkey_e_len -1,
  1057. (uint8_t*)CertAtt->keyIndex);
  1058. if (ret == 0) {
  1059. CertAtt->verifyByTSIP_SCE = 1;
  1060. }
  1061. else {
  1062. WOLFSSL_MSG("RSA Verify by SCE didn't match");
  1063. ret = ASN_SIG_CONFIRM_E;
  1064. }
  1065. }
  1066. else
  1067. ret = CRYPTOCB_UNAVAILABLE;
  1068. #endif
  1069. return ret;
  1070. }
  1071. /* Renesas Security Library Common Callback
  1072. * Callback for Signature PK Ecc verify
  1073. *
  1074. * sig Buffer holding signature
  1075. * sigSz Length of signature in bytes
  1076. * has Buffer to hold hash
  1077. * hashSz Length of hash
  1078. * keyDer Buffer to hold rsa key
  1079. * keySz Length of key in bytes
  1080. * result A pointer to int indicates a result
  1081. * ctx Callback context
  1082. * return FSP_SUCCESS(0) on success,
  1083. * otherwise CRYPTOCB_UNAVAILABLE
  1084. * so that caller could continue to process if want
  1085. */
  1086. WOLFSSL_LOCAL int Renesas_cmn_SigPkCbEccVerify(const unsigned char* sig,
  1087. unsigned int sigSz, const unsigned char* hash, unsigned int hashSz,
  1088. const unsigned char* keyDer, unsigned int keySz,
  1089. int* result, void* ctx)
  1090. {
  1091. int ret;
  1092. CertAttribute* CertAtt;
  1093. (void)result;
  1094. (void)keyDer;
  1095. (void)keySz;
  1096. (void)hash;
  1097. (void)hashSz;
  1098. WOLFSSL_ENTER("Renesas_cmn_SigPkCbEccVerify");
  1099. /* sanity check */
  1100. if (sig == NULL || keyDer == NULL || hash == NULL || ctx == NULL ||
  1101. result == NULL)
  1102. return BAD_FUNC_ARG;
  1103. CertAtt = (CertAttribute*)ctx;
  1104. if (!CertAtt) {
  1105. return CRYPTOCB_UNAVAILABLE;
  1106. }
  1107. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  1108. if (CertAtt->keyIndex != NULL) {
  1109. ret = wc_tsip_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
  1110. CertAtt->pubkey_n_start - CertAtt->certBegin,
  1111. CertAtt->pubkey_n_len - 1,
  1112. CertAtt->pubkey_e_start - CertAtt->certBegin,
  1113. CertAtt->pubkey_e_len -1,
  1114. (uint8_t*)CertAtt->keyIndex);
  1115. if (ret == 0) {
  1116. CertAtt->verifyByTSIP_SCE = 1;
  1117. *result = 1;
  1118. }
  1119. else {
  1120. WOLFSSL_MSG("RSA Verify by TSIP didn't match");
  1121. ret = ASN_SIG_CONFIRM_E;
  1122. }
  1123. }
  1124. else
  1125. ret = CRYPTOCB_UNAVAILABLE;
  1126. #elif defined(WOLFSSL_RENESAS_FSPSM_TLS)
  1127. if (CertAtt->keyIndex != NULL) {
  1128. ret = wc_fspsm_tls_CertVerify(CertAtt->cert, CertAtt->certSz,
  1129. sig, sigSz,
  1130. CertAtt->pubkey_n_start - CertAtt->certBegin,
  1131. CertAtt->pubkey_n_len - 1,
  1132. CertAtt->pubkey_e_start - CertAtt->certBegin,
  1133. CertAtt->pubkey_e_len -1,
  1134. (uint8_t*)CertAtt->keyIndex);
  1135. if (ret == 0) {
  1136. CertAtt->verifyByTSIP_SCE = 1;
  1137. *result = 1;
  1138. }
  1139. else {
  1140. WOLFSSL_MSG("RSA Verify by SCE didn't match");
  1141. ret = ASN_SIG_CONFIRM_E;
  1142. }
  1143. }
  1144. else
  1145. ret = CRYPTOCB_UNAVAILABLE;
  1146. #endif
  1147. return ret;
  1148. }
  1149. #endif /* SCEPROTECT || TSIP */