renesas_tsip_aes.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /* renesas_tsip_aes.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 <string.h>
  22. #include <stdio.h>
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include <wolfssl/wolfcrypt/settings.h>
  27. #include <stdio.h>
  28. #ifndef NO_AES
  29. #if (defined(WOLFSSL_RENESAS_TSIP_TLS) || \
  30. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \
  31. !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_AES)
  32. #include <wolfssl/wolfcrypt/wc_port.h>
  33. #include <wolfssl/wolfcrypt/error-crypt.h>
  34. #include <wolfssl/internal.h>
  35. #include <wolfssl/wolfcrypt/aes.h>
  36. #include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h"
  37. #ifdef NO_INLINE
  38. #include <wolfssl/wolfcrypt/misc.h>
  39. #else
  40. #define WOLFSSL_MISC_INCLUDED
  41. #include <wolfcrypt/src/misc.c>
  42. #endif
  43. #define TSIP_AES_GCM_AUTH_TAG_SIZE 16
  44. typedef e_tsip_err_t (*aesGcmEncInitFn)
  45. (tsip_gcm_handle_t*, tsip_aes_key_index_t*, uint8_t*, uint32_t);
  46. typedef e_tsip_err_t (*aesGcmEncUpdateFn)
  47. (tsip_gcm_handle_t*,uint8_t*, uint8_t*, uint32_t, uint8_t*, uint32_t);
  48. typedef e_tsip_err_t (*aesGcmEncFinalFn)
  49. (tsip_gcm_handle_t*, uint8_t*, uint32_t*, uint8_t*);
  50. typedef e_tsip_err_t (*aesGcmDecInitFn)
  51. (tsip_gcm_handle_t*, tsip_aes_key_index_t*, uint8_t*, uint32_t);
  52. typedef e_tsip_err_t (*aesGcmDecUpdateFn)
  53. (tsip_gcm_handle_t*,uint8_t*, uint8_t*, uint32_t, uint8_t*, uint32_t);
  54. typedef e_tsip_err_t (*aesGcmDecFinalFn)
  55. (tsip_gcm_handle_t*, uint8_t*, uint32_t*, uint8_t*, uint32_t);
  56. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  57. /* function pointer type defs for TLSv13 handshake AES-GCM/CCM encryption */
  58. typedef e_tsip_err_t (*Tls13AesEncInitFn)
  59. (tsip_tls13_handle_t*, e_tsip_tls13_phase_t, e_tsip_tls13_mode_t,
  60. e_tsip_tls13_cipher_suite_t, tsip_aes_key_index_t*, uint32_t);
  61. typedef e_tsip_err_t (*Tls13AesEncUpdateFn)
  62. (tsip_tls13_handle_t*, uint8_t*, uint8_t*, uint32_t);
  63. typedef e_tsip_err_t (*Tls13AesEncFinalFn)
  64. (tsip_tls13_handle_t*, uint8_t*, uint32_t*);
  65. /* function pointer type defs for TLSv13 handshake AES-GCM/CCM decryption */
  66. typedef e_tsip_err_t (*Tls13AesDecInitFn)
  67. (tsip_tls13_handle_t*, e_tsip_tls13_phase_t, e_tsip_tls13_mode_t,
  68. e_tsip_tls13_cipher_suite_t, tsip_aes_key_index_t*, uint32_t);
  69. typedef e_tsip_err_t (*Tls13AesDecUpdateFn)
  70. (tsip_tls13_handle_t*, uint8_t*, uint8_t*, uint32_t);
  71. typedef e_tsip_err_t (*Tls13AesDecFinalFn)
  72. (tsip_tls13_handle_t*, uint8_t*, uint32_t*);
  73. /* encrypt plain data.
  74. *
  75. * return cipher data size on success, negative value on failure.
  76. * CRYPTOCB_UNAVAILABLE may be returned.
  77. */
  78. WOLFSSL_LOCAL int tsip_Tls13AesEncrypt(
  79. struct WOLFSSL* ssl,
  80. byte* output,
  81. const byte* input,
  82. word16 sz)
  83. {
  84. int ret = 0;
  85. e_tsip_err_t err = TSIP_SUCCESS;
  86. TsipUserCtx* tuc = NULL;
  87. e_tsip_tls13_cipher_suite_t cs;
  88. word32 cipher[(AES_BLOCK_SIZE + TSIP_AES_GCM_AUTH_TAG_SIZE) /
  89. sizeof(word32)];
  90. word32 plain[AES_BLOCK_SIZE / sizeof(word32)];
  91. int idxIn,idxOut;
  92. uint32_t remain;
  93. uint32_t dataSz, finalSz;
  94. e_tsip_tls13_phase_t phase;
  95. tsip_aes_key_index_t* key = NULL;
  96. WOLFSSL_ENTER("tsip_Tls13AesEncrypt");
  97. if ((ssl == NULL) || (input == NULL) || (output == NULL) || (sz == 0)) {
  98. return BAD_FUNC_ARG;
  99. }
  100. if (ssl->options.side != WOLFSSL_CLIENT_END) {
  101. return CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  102. }
  103. /* get user context for TSIP */
  104. tuc = ssl->RenesasUserCtx;
  105. if (tuc == NULL) {
  106. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  107. return CRYPTOCB_UNAVAILABLE;
  108. }
  109. /* select the appropriate encryption key and phase */
  110. if (ssl->options.handShakeDone) {
  111. if (!tuc->ClientWriteTrafficKey_set) {
  112. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  113. return CRYPTOCB_UNAVAILABLE;
  114. }
  115. key = &(tuc->clientAppWriteKey13Idx);
  116. phase = TSIP_TLS13_PHASE_APPLICATION;
  117. }
  118. else {
  119. if (!tuc->HandshakeClientTrafficKey_set) {
  120. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  121. return CRYPTOCB_UNAVAILABLE;
  122. }
  123. key = &(tuc->clientWriteKey13Idx);
  124. phase = TSIP_TLS13_PHASE_HANDSHAKE;
  125. }
  126. /* select AES mode */
  127. if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm)
  128. cs = TSIP_TLS13_CIPHER_SUITE_AES_128_GCM_SHA256;
  129. else if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_ccm)
  130. cs = TSIP_TLS13_CIPHER_SUITE_AES_128_CCM_SHA256;
  131. else
  132. return CRYPTOCB_UNAVAILABLE;
  133. remain = sz;
  134. finalSz = 0;
  135. if ((ret = tsip_hw_lock()) == 0) {
  136. err = R_TSIP_Tls13EncryptInit(
  137. &(tuc->handle13),
  138. phase,
  139. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  140. cs,
  141. key,
  142. sz);
  143. if (err != TSIP_SUCCESS) {
  144. WOLFSSL_MSG("R_TSIP_Tls13DecryptUpdate error");
  145. ret = WC_HW_E;
  146. }
  147. idxIn = 0;
  148. idxOut = 0;
  149. while (err == TSIP_SUCCESS && remain > 0) {
  150. dataSz = min(remain, AES_BLOCK_SIZE);
  151. ForceZero(plain, sizeof(plain));
  152. ForceZero(cipher, sizeof(cipher));
  153. XMEMCPY(plain, input + idxIn, dataSz);
  154. err = R_TSIP_Tls13EncryptUpdate(
  155. &(tuc->handle13),
  156. (uint8_t*)plain,
  157. (uint8_t*)cipher,
  158. dataSz);
  159. if (err == TSIP_SUCCESS) {
  160. if (dataSz >= AES_BLOCK_SIZE) {
  161. XMEMCPY(output + idxOut, cipher, dataSz);
  162. idxOut += dataSz;
  163. }
  164. idxIn += dataSz;
  165. remain -= dataSz;
  166. }
  167. else {
  168. WOLFSSL_MSG("R_TSIP_Tls13DecryptUpdate error");
  169. ret = WC_HW_E;
  170. }
  171. }
  172. if (err == TSIP_SUCCESS) {
  173. ForceZero(cipher, sizeof(cipher));
  174. /* R_TSIP_Tls13EncryptFinal outputs encrypted content and auth-data
  175. * to the buffer.
  176. */
  177. err = R_TSIP_Tls13EncryptFinal(
  178. &(tuc->handle13),
  179. (uint8_t*)cipher,
  180. &finalSz); /* total output size */
  181. if (err == TSIP_SUCCESS) {
  182. XMEMCPY(output + idxOut, cipher, finalSz - idxOut);
  183. ret = finalSz;
  184. }
  185. else {
  186. WOLFSSL_MSG("R_TSIP_Tls13EncryptFinal error");
  187. ret = WC_HW_E;
  188. }
  189. }
  190. tsip_hw_unlock();
  191. }
  192. WOLFSSL_LEAVE("tsip_Tls13AesEncrypt", ret);
  193. return ret;
  194. }
  195. /* decrypt encrypted handshake data for TLSv1.3
  196. * AES-GCM or AES-CCM can be used
  197. * return 0 on success, otherwise on error.
  198. */
  199. WOLFSSL_LOCAL int tsip_Tls13AesDecrypt(
  200. struct WOLFSSL* ssl,
  201. byte* output,
  202. const byte* input,
  203. word16 sz)
  204. {
  205. int ret = 0;
  206. e_tsip_err_t err = TSIP_SUCCESS;
  207. TsipUserCtx* tuc = NULL;
  208. e_tsip_tls13_cipher_suite_t cs;
  209. word32 cipher[AES_BLOCK_SIZE / sizeof(word32)];
  210. word32 plain[AES_BLOCK_SIZE / sizeof(word32)];
  211. int idxIn,idxOut;
  212. int blocks;
  213. uint32_t remain,conRemain;
  214. uint32_t dataSz, finalSz;
  215. e_tsip_tls13_phase_t phase;
  216. tsip_aes_key_index_t* key = NULL;
  217. WOLFSSL_ENTER("tsip_Tls13AesDecrypt");
  218. if ((ssl == NULL) || (input == NULL) || (output == NULL) || (sz == 0)) {
  219. return BAD_FUNC_ARG;
  220. }
  221. if (ssl->options.side != WOLFSSL_CLIENT_END) {
  222. return CRYPTOCB_UNAVAILABLE; /* expecting to fallback to S/W */
  223. }
  224. /* get user context for TSIP */
  225. tuc = ssl->RenesasUserCtx;
  226. if (tuc == NULL) {
  227. WOLFSSL_MSG("TsipUserCtx hasn't been set to ssl.");
  228. return CRYPTOCB_UNAVAILABLE;
  229. }
  230. /* select the appropriate encryption key and phase */
  231. if (ssl->options.handShakeDone) {
  232. if (!tuc->ServerWriteTrafficKey_set) {
  233. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  234. return CRYPTOCB_UNAVAILABLE;
  235. }
  236. key = &(tuc->serverAppWriteKey13Idx);
  237. phase = TSIP_TLS13_PHASE_APPLICATION;
  238. }
  239. else {
  240. if (!tuc->HandshakeServerTrafficKey_set) {
  241. WOLFSSL_MSG("TSIP wasn't involved in the key-exchange.");
  242. return CRYPTOCB_UNAVAILABLE;
  243. }
  244. key = &(tuc->serverWriteKey13Idx);
  245. phase = TSIP_TLS13_PHASE_HANDSHAKE;
  246. }
  247. /* select AES mode */
  248. if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm)
  249. cs = TSIP_TLS13_CIPHER_SUITE_AES_128_GCM_SHA256;
  250. else if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_ccm)
  251. cs = TSIP_TLS13_CIPHER_SUITE_AES_128_CCM_SHA256;
  252. else
  253. return CRYPTOCB_UNAVAILABLE;
  254. blocks = sz / AES_BLOCK_SIZE;
  255. remain = sz;
  256. conRemain = sz - TSIP_AES_GCM_AUTH_TAG_SIZE;
  257. if ((ret = tsip_hw_lock()) == 0) {
  258. err = R_TSIP_Tls13DecryptInit(
  259. &(tuc->handle13),
  260. phase,
  261. TSIP_TLS13_MODE_FULL_HANDSHAKE,
  262. cs,
  263. key,
  264. sz);
  265. if (err != TSIP_SUCCESS) {
  266. WOLFSSL_MSG("R_TSIP_Tls13DecryptInit error");
  267. ret = WC_HW_E;
  268. }
  269. idxIn = 0;
  270. idxOut = 0;
  271. while (err == TSIP_SUCCESS && (blocks--) >= 0) {
  272. dataSz = min(remain, AES_BLOCK_SIZE);
  273. XMEMCPY(cipher, input + idxIn, dataSz);
  274. ForceZero(plain, AES_BLOCK_SIZE);
  275. err = R_TSIP_Tls13DecryptUpdate(
  276. &(tuc->handle13),
  277. (uint8_t*)cipher,
  278. (uint8_t*)plain,
  279. dataSz);
  280. if (err == TSIP_SUCCESS) {
  281. if (dataSz >= AES_BLOCK_SIZE && conRemain >= AES_BLOCK_SIZE) {
  282. XMEMCPY(output + idxOut, plain, dataSz);
  283. idxOut += dataSz;
  284. conRemain -= min(conRemain, dataSz);
  285. }
  286. idxIn += dataSz;
  287. remain -= dataSz;
  288. }
  289. else {
  290. WOLFSSL_MSG("R_TSIP_Tls13DecryptUpdate error");
  291. ret = WC_HW_E;
  292. }
  293. }
  294. if (err == TSIP_SUCCESS) {
  295. err = R_TSIP_Tls13DecryptFinal(
  296. &(tuc->handle13),
  297. (uint8_t*)plain,
  298. &finalSz); /* total size will be returned */
  299. if (err == TSIP_SUCCESS) {
  300. XMEMCPY(output + idxOut, plain, conRemain);
  301. }
  302. else if (err== TSIP_ERR_AUTHENTICATION) {
  303. WOLFSSL_MSG("tsip_Tls13AesDecrypt authentication error");
  304. ret = AES_GCM_AUTH_E;
  305. }
  306. else {
  307. WOLFSSL_MSG("R_TSIP_Tls13DecryptFinal error");
  308. ret = WC_HW_E;
  309. }
  310. }
  311. tsip_hw_unlock();
  312. }
  313. WOLFSSL_LEAVE("tsip_Tls13AesDecrypt", ret);
  314. return ret;
  315. }
  316. #endif /* WOLFSSL_RENESAS_TSIP_TLS */
  317. #if (WOLFSSL_RENESAS_TSIP_VER >= 109)
  318. #ifdef WOLF_CRYPTO_CB
  319. WOLFSSL_LOCAL int wc_tsip_AesCipher(int devIdArg, wc_CryptoInfo* info,
  320. void* ctx)
  321. {
  322. int ret = NOT_COMPILED_IN;
  323. TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
  324. WOLFSSL_ENTER("wc_tsip_AesCipher");
  325. if (info == NULL || ctx == NULL)
  326. return BAD_FUNC_ARG;
  327. if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  328. #if !defined(NO_AES) || !defined(NO_DES3)
  329. #ifdef HAVE_AESGCM
  330. if (info->cipher.type == WC_CIPHER_AES_GCM
  331. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  332. && cbInfo->session_key_set == 1
  333. #endif
  334. ) {
  335. if (info->cipher.enc) {
  336. ret = wc_tsip_AesGcmEncrypt(
  337. info->cipher.aesgcm_enc.aes,
  338. (byte*)info->cipher.aesgcm_enc.out,
  339. (byte*)info->cipher.aesgcm_enc.in,
  340. info->cipher.aesgcm_enc.sz,
  341. (byte*)info->cipher.aesgcm_enc.iv,
  342. info->cipher.aesgcm_enc.ivSz,
  343. (byte*)info->cipher.aesgcm_enc.authTag,
  344. info->cipher.aesgcm_enc.authTagSz,
  345. (byte*)info->cipher.aesgcm_enc.authIn,
  346. info->cipher.aesgcm_enc.authInSz,
  347. (void*)ctx);
  348. }
  349. else {
  350. ret = wc_tsip_AesGcmDecrypt(
  351. info->cipher.aesgcm_dec.aes,
  352. (byte*)info->cipher.aesgcm_dec.out,
  353. (byte*)info->cipher.aesgcm_dec.in,
  354. info->cipher.aesgcm_dec.sz,
  355. (byte*)info->cipher.aesgcm_dec.iv,
  356. info->cipher.aesgcm_dec.ivSz,
  357. (byte*)info->cipher.aesgcm_dec.authTag,
  358. info->cipher.aesgcm_dec.authTagSz,
  359. (byte*)info->cipher.aesgcm_dec.authIn,
  360. info->cipher.aesgcm_dec.authInSz,
  361. (void*)ctx);
  362. }
  363. }
  364. #endif /* HAVE_AESGCM */
  365. #ifdef HAVE_AES_CBC
  366. if (info->cipher.type == WC_CIPHER_AES_CBC
  367. #ifdef WOLFSSL_RENESAS_TSIP_TLS
  368. && cbInfo->session_key_set == 1
  369. #endif
  370. ) {
  371. if (info->cipher.enc) {
  372. ret = wc_tsip_AesCbcEncrypt(
  373. info->cipher.aescbc.aes,
  374. (byte*)info->cipher.aescbc.out,
  375. (byte*)info->cipher.aescbc.in,
  376. info->cipher.aescbc.sz);
  377. }
  378. else {
  379. ret = wc_tsip_AesCbcDecrypt(
  380. info->cipher.aescbc.aes,
  381. (byte*)info->cipher.aescbc.out,
  382. (byte*)info->cipher.aescbc.in,
  383. info->cipher.aescbc.sz);
  384. }
  385. }
  386. #endif /* HAVE_AES_CBC */
  387. #endif /* !NO_AES || !NO_DES3 */
  388. }
  389. WOLFSSL_LEAVE("wc_tsip_AesCipher", ret);
  390. return ret;
  391. }
  392. #endif /* WOLF_CRYPTO_CB */
  393. #endif /* WOLFSSL_RENESAS_TSIP_VER >= 109 */
  394. int wc_tsip_AesCbcEncrypt(struct Aes* aes, byte* out, const byte* in, word32 sz)
  395. {
  396. tsip_aes_handle_t _handle;
  397. int ret;
  398. word32 blocks = (sz / AES_BLOCK_SIZE);
  399. uint32_t dataLength;
  400. byte *iv;
  401. if ((in == NULL) || (out == NULL) || (aes == NULL))
  402. return BAD_FUNC_ARG;
  403. /* while doing TLS handshake, TSIP driver keeps true-key and iv *
  404. * on the device. iv is dummy */
  405. iv = (uint8_t*)aes->reg;
  406. if ((ret = tsip_hw_lock()) != 0) {
  407. WOLFSSL_MSG("Failed to lock");
  408. return ret;
  409. }
  410. if (aes->ctx.keySize == 16) {
  411. ret = R_TSIP_Aes128CbcEncryptInit(&_handle, &aes->ctx.tsip_keyIdx, iv);
  412. }
  413. else if (aes->ctx.keySize == 32) {
  414. ret = R_TSIP_Aes256CbcEncryptInit(&_handle, &aes->ctx.tsip_keyIdx, iv);
  415. }
  416. else {
  417. tsip_hw_unlock();
  418. return -1;
  419. }
  420. while (ret == TSIP_SUCCESS && blocks--) {
  421. if (aes->ctx.keySize == 16)
  422. ret = R_TSIP_Aes128CbcEncryptUpdate(&_handle, (uint8_t*)in,
  423. (uint8_t*)out, (uint32_t)AES_BLOCK_SIZE);
  424. else
  425. ret = R_TSIP_Aes256CbcEncryptUpdate(&_handle, (uint8_t*)in,
  426. (uint8_t*)out, (uint32_t)AES_BLOCK_SIZE);
  427. in += AES_BLOCK_SIZE;
  428. out += AES_BLOCK_SIZE;
  429. }
  430. if (ret == TSIP_SUCCESS) {
  431. if (aes->ctx.keySize == 16) {
  432. ret = R_TSIP_Aes128CbcEncryptFinal(&_handle, out, &dataLength);
  433. }
  434. else {
  435. ret = R_TSIP_Aes256CbcEncryptFinal(&_handle, out, &dataLength);
  436. }
  437. }
  438. else {
  439. WOLFSSL_MSG("TSIP AES CBC encryption failed");
  440. ret = -1;
  441. }
  442. tsip_hw_unlock();
  443. return ret;
  444. }
  445. int wc_tsip_AesCbcDecrypt(struct Aes* aes, byte* out, const byte* in, word32 sz)
  446. {
  447. tsip_aes_handle_t _handle;
  448. int ret;
  449. word32 blocks = (sz / AES_BLOCK_SIZE);
  450. uint32_t dataLength;
  451. byte *iv;
  452. if ((in == NULL) || (out == NULL) || (aes == NULL))
  453. return BAD_FUNC_ARG;
  454. iv = (uint8_t*)aes->reg;
  455. if ((ret = tsip_hw_lock()) != 0) {
  456. WOLFSSL_MSG("Failed to lock");
  457. return ret;
  458. }
  459. if (aes->ctx.keySize == 16) {
  460. ret = R_TSIP_Aes128CbcDecryptInit(&_handle, &aes->ctx.tsip_keyIdx, iv);
  461. }
  462. else if (aes->ctx.keySize == 32) {
  463. ret = R_TSIP_Aes256CbcDecryptInit(&_handle, &aes->ctx.tsip_keyIdx, iv);
  464. }
  465. else {
  466. tsip_hw_unlock();
  467. return -1;
  468. }
  469. while (ret == TSIP_SUCCESS && blocks--) {
  470. if (aes->ctx.keySize == 16)
  471. ret = R_TSIP_Aes128CbcDecryptUpdate(&_handle, (uint8_t*)in,
  472. (uint8_t*)out, (uint32_t)AES_BLOCK_SIZE);
  473. else
  474. ret = R_TSIP_Aes256CbcDecryptUpdate(&_handle, (uint8_t*)in,
  475. (uint8_t*)out, (uint32_t)AES_BLOCK_SIZE);
  476. in += AES_BLOCK_SIZE;
  477. out += AES_BLOCK_SIZE;
  478. }
  479. if (ret == TSIP_SUCCESS) {
  480. if (aes->ctx.keySize == 16)
  481. ret = R_TSIP_Aes128CbcDecryptFinal(&_handle, out, &dataLength);
  482. else
  483. ret = R_TSIP_Aes256CbcDecryptFinal(&_handle, out, &dataLength);
  484. }
  485. else {
  486. WOLFSSL_MSG("TSIP AES CBC decryption failed");
  487. ret = -1;
  488. }
  489. tsip_hw_unlock();
  490. return ret;
  491. }
  492. /*
  493. * Encrypt plain data then output encrypted data and authentication tag data.
  494. * The session key used for encryption is generated inside this function and
  495. * the key which has been generated and stored in Aes is not used.
  496. * parameter
  497. * - aes: Aes structure
  498. * - out: buffer where the cipher text is output
  499. * - in: buffer where the plain data is storead
  500. * - sz: size of plain data and also means output size
  501. * - iv: iv should be consist of implicit-iv of 4 bytes and exp-iv of 8 bytes
  502. * - authTag: buffer where the authentication data is output.
  503. * - authTagSz: buffer size for authentication data.
  504. * - authIn: buffer holding Additional Authentication Data(AAD)
  505. * - authInSz: AAD size
  506. * - ctx: TsipUserCtx
  507. * return 0 on success, otherwise on error.
  508. * Note: As of TSIPv1.13, only accept 128 and 256 bit of key size
  509. *
  510. */
  511. int wc_tsip_AesGcmEncrypt(
  512. struct Aes* aes, byte* out,
  513. const byte* in, word32 sz,
  514. byte* iv, word32 ivSz,
  515. byte* authTag, word32 authTagSz, /* auth Tag */
  516. const byte* authIn, word32 authInSz, /* AAD */
  517. void* ctx)
  518. {
  519. int ret = -1;
  520. e_tsip_err_t err;
  521. tsip_gcm_handle_t hdl;
  522. uint32_t dataLen = sz;
  523. uint32_t cipherBufSz;
  524. aesGcmEncInitFn initFn;
  525. aesGcmEncUpdateFn updateFn;
  526. aesGcmEncFinalFn finalFn;
  527. uint8_t* plainBuf = NULL;
  528. uint8_t* cipherBuf = NULL;
  529. uint8_t* aTagBuf = NULL;
  530. uint8_t* aadBuf = NULL;
  531. const uint8_t* iv_l = NULL;
  532. uint32_t ivSz_l = 0;
  533. tsip_aes_key_index_t key_client_aes;
  534. TsipUserCtx *userCtx;
  535. WOLFSSL_ENTER("wc_tsip_AesGcmEncrypt");
  536. if (aes == NULL || ctx == NULL || (ivSz == 0) ||
  537. (sz != 0 && (in == NULL || out == NULL)) ||
  538. (ivSz != 0 && iv == NULL) ||
  539. (authInSz != 0 && authIn == NULL)) {
  540. WOLFSSL_LEAVE("wc_tsip_AesGcmEncrypt", BAD_FUNC_ARG);
  541. return BAD_FUNC_ARG;
  542. }
  543. /* TSIP can handle 128 and 256 bit key only */
  544. if (aes->ctx.keySize != 16 && aes->ctx.keySize != 32) {
  545. WOLFSSL_MSG("illegal key size");
  546. WOLFSSL_LEAVE("wc_tsip_AesGcmEncrypt", BAD_FUNC_ARG);
  547. return BAD_FUNC_ARG;
  548. }
  549. if (aes->ctx.keySize == 16) {
  550. initFn = R_TSIP_Aes128GcmEncryptInit;
  551. updateFn = R_TSIP_Aes128GcmEncryptUpdate;
  552. finalFn = R_TSIP_Aes128GcmEncryptFinal;
  553. }
  554. else {
  555. initFn = R_TSIP_Aes256GcmEncryptInit;
  556. updateFn = R_TSIP_Aes256GcmEncryptUpdate;
  557. finalFn = R_TSIP_Aes256GcmEncryptFinal;
  558. }
  559. userCtx = (TsipUserCtx*)ctx;
  560. /* buffer for cipher data output must be multiple of AES_BLOCK_SIZE */
  561. cipherBufSz = ((sz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
  562. if ((ret = tsip_hw_lock()) == 0) {
  563. /* allocate buffers for plaintext, ciphertext, authTag and aad to make
  564. * sure those buffers 32bit aligned as TSIP requests.
  565. */
  566. plainBuf = XMALLOC(sz, aes->heap, DYNAMIC_TYPE_AES);
  567. cipherBuf = XMALLOC(cipherBufSz, aes->heap, DYNAMIC_TYPE_AES);
  568. aTagBuf = XMALLOC(TSIP_AES_GCM_AUTH_TAG_SIZE, aes->heap,
  569. DYNAMIC_TYPE_AES);
  570. aadBuf = XMALLOC(authInSz, aes->heap, DYNAMIC_TYPE_AES);
  571. if (plainBuf == NULL || cipherBuf == NULL || aTagBuf == NULL ||
  572. aadBuf == NULL ) {
  573. WOLFSSL_MSG("wc_tsip_AesGcmEncrypt: buffer allocation failed");
  574. ret = -1;
  575. }
  576. if (ret == 0) {
  577. XMEMCPY(plainBuf, in, sz);
  578. ForceZero(cipherBuf, cipherBufSz);
  579. ForceZero(authTag, authTagSz);
  580. XMEMCPY(aadBuf, authIn, authInSz);
  581. }
  582. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  583. if (ret == 0 &&
  584. userCtx->session_key_set == 1) {
  585. /* generate AES-GCM session key. The key stored in
  586. * Aes.ctx.tsip_keyIdx is not used here.
  587. */
  588. err = R_TSIP_TlsGenerateSessionKey(
  589. userCtx->tsip_cipher,
  590. (uint32_t*)userCtx->tsip_masterSecret,
  591. (uint8_t*) userCtx->tsip_clientRandom,
  592. (uint8_t*) userCtx->tsip_serverRandom,
  593. &iv[AESGCM_IMP_IV_SZ], /* use exp_IV */
  594. NULL,
  595. NULL,
  596. &key_client_aes,
  597. NULL,
  598. NULL, NULL);
  599. if (err != TSIP_SUCCESS) {
  600. WOLFSSL_MSG("R_TSIP_TlsGenerateSessionKey failed");
  601. ret = -1;
  602. }
  603. } else
  604. #endif
  605. if (ret == 0 &&
  606. (userCtx->user_aes128_key_set == 1 ||
  607. userCtx->user_aes256_key_set == 1)) {
  608. if (aes->ctx.keySize == 32) {
  609. XMEMCPY(&key_client_aes, &userCtx->user_aes256_key_index,
  610. sizeof(tsip_aes_key_index_t));
  611. }
  612. else {
  613. XMEMCPY(&key_client_aes, &userCtx->user_aes128_key_index,
  614. sizeof(tsip_aes_key_index_t));
  615. }
  616. iv_l = iv;
  617. ivSz_l = ivSz;
  618. }
  619. if (ret == 0) {
  620. /* Since generated session key is coupled to iv, no need to pass
  621. * iv init func.
  622. * It expects to pass iv when users create their own key.
  623. */
  624. err = initFn(&hdl, &key_client_aes, (uint8_t*)iv_l, ivSz_l);
  625. if (err == TSIP_SUCCESS) {
  626. err = updateFn(&hdl, NULL, NULL, 0UL, (uint8_t*)aadBuf, authInSz);
  627. }
  628. if (err == TSIP_SUCCESS) {
  629. err = updateFn(&hdl, plainBuf, cipherBuf, sz, NULL, 0UL);
  630. }
  631. if (err != TSIP_SUCCESS) {
  632. WOLFSSL_MSG("R_TSIP_AesXXXGcmEncryptUpdate: failed");
  633. ret = -1;
  634. }
  635. /* Once R_TSIP_AesxxxGcmEncryptInit or R_TSIP_AesxxxEncryptUpdate is
  636. * called, R_TSIP_AesxxxGcmEncryptFinal must be called regardless of
  637. * the result of the previous call. Otherwise, TSIP can not come out
  638. * from its error state and all the trailing APIs will fail.
  639. */
  640. dataLen = 0;
  641. err = finalFn(&hdl,
  642. cipherBuf + (sz / AES_BLOCK_SIZE) * AES_BLOCK_SIZE,
  643. &dataLen,
  644. aTagBuf); /* aad of 16 bytes will be output */
  645. if (err == TSIP_SUCCESS) {
  646. /* copy encrypted data to out */
  647. XMEMCPY(out, cipherBuf, sz);
  648. /* copy auth tag to caller's buffer */
  649. XMEMCPY((void*)authTag, (void*)aTagBuf,
  650. min(authTagSz, TSIP_AES_GCM_AUTH_TAG_SIZE ));
  651. }
  652. else {
  653. WOLFSSL_MSG("R_TSIP_AesxxxGcmEncryptFinal: failed");
  654. ret = -1;
  655. }
  656. }
  657. XFREE(plainBuf, aes->heap, DYNAMIC_TYPE_AES);
  658. XFREE(cipherBuf, aes->heap, DYNAMIC_TYPE_AES);
  659. XFREE(aTagBuf, aes->heap, DYNAMIC_TYPE_AES);
  660. XFREE(aadBuf, aes->heap, DYNAMIC_TYPE_AES);
  661. tsip_hw_unlock();
  662. }
  663. return ret;
  664. }
  665. /*
  666. * Decrypt cipher data into plaindata and output authentication tag data.
  667. * The session key used for decryption is generated inside this function and
  668. * the key which has been generated and stored in Aes is not used.
  669. * parameter
  670. * - aes: Aes structure
  671. * - out: buffer where the plain text is output
  672. * - in: buffer where the cipher data is storead
  673. * - sz: size of cipher data and also means output size
  674. * - iv: iv should be consist of implicit-iv of 4 bytes and exp-iv of 8 bytes
  675. * - authTag: buffer where the authentication data is stored.
  676. * - authTagSz: buffer size for authentication data.
  677. * - authIn: buffer where Additional Authentication Data(AAD) is stored
  678. * - authInSz: AAD size
  679. * return 0 on success, otherwise on error.
  680. * Note: As of TSIPv1.13, only accept 128 and 256 bit of key size
  681. *
  682. */
  683. int wc_tsip_AesGcmDecrypt(
  684. Aes* aes, byte* out,
  685. const byte* in, word32 sz,
  686. const byte* iv, word32 ivSz,
  687. const byte* authTag, word32 authTagSz,
  688. const byte* authIn, word32 authInSz,
  689. void* ctx)
  690. {
  691. int ret = -1;
  692. e_tsip_err_t err;
  693. tsip_gcm_handle_t hdl;
  694. uint32_t dataLen;
  695. uint32_t plainBufSz;
  696. aesGcmDecInitFn initFn;
  697. aesGcmDecUpdateFn updateFn;
  698. aesGcmDecFinalFn finalFn;
  699. uint8_t* cipherBuf = NULL;
  700. uint8_t* plainBuf = NULL;
  701. uint8_t* aTagBuf = NULL;
  702. uint8_t* aadBuf = NULL;
  703. const uint8_t* iv_l = NULL;
  704. uint32_t ivSz_l = 0;
  705. tsip_aes_key_index_t key_server_aes;
  706. TsipUserCtx *userCtx;
  707. WOLFSSL_ENTER("wc_tsip_AesGcmDecrypt");
  708. if (aes == NULL || in == NULL || out == NULL || sz == 0 || ctx == NULL ||
  709. iv == 0 ||
  710. (authInSz != 0 && authIn == NULL) ||
  711. (authInSz == 0 && authIn != NULL) ||
  712. (authTagSz != 0 && authTag == NULL) ||
  713. (authTagSz == 0 && authTag != NULL)) {
  714. WOLFSSL_LEAVE("wc_tsip_AesGcmDecrypt", BAD_FUNC_ARG);
  715. return BAD_FUNC_ARG;
  716. }
  717. if (aes->ctx.keySize != 16 && aes->ctx.keySize != 32) {
  718. WOLFSSL_MSG("illegal key size");
  719. WOLFSSL_LEAVE("wc_tsip_AesGcmDecrypt", BAD_FUNC_ARG);
  720. return BAD_FUNC_ARG;
  721. }
  722. if (aes->ctx.keySize == 16) {
  723. initFn = R_TSIP_Aes128GcmDecryptInit;
  724. updateFn = R_TSIP_Aes128GcmDecryptUpdate;
  725. finalFn = R_TSIP_Aes128GcmDecryptFinal;
  726. }
  727. else {
  728. initFn = R_TSIP_Aes256GcmDecryptInit;
  729. updateFn = R_TSIP_Aes256GcmDecryptUpdate;
  730. finalFn = R_TSIP_Aes256GcmDecryptFinal;
  731. }
  732. userCtx = (TsipUserCtx *)ctx;
  733. /* buffer for plain data output must be multiple of AES_BLOCK_SIZE */
  734. plainBufSz = ((sz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
  735. if ((ret = tsip_hw_lock()) == 0) {
  736. /* allocate buffers for plaintext, cipher-text, authTag and AAD.
  737. * TSIP requests those buffers 32bit aligned.
  738. */
  739. cipherBuf = XMALLOC(sz, aes->heap, DYNAMIC_TYPE_AES);
  740. plainBuf = XMALLOC(plainBufSz, aes->heap, DYNAMIC_TYPE_AES);
  741. aTagBuf = XMALLOC(TSIP_AES_GCM_AUTH_TAG_SIZE, aes->heap,
  742. DYNAMIC_TYPE_AES);
  743. aadBuf = XMALLOC(authInSz, aes->heap, DYNAMIC_TYPE_AES);
  744. if (plainBuf == NULL || cipherBuf == NULL || aTagBuf == NULL ||
  745. aadBuf == NULL) {
  746. ret = -1;
  747. }
  748. if (ret == 0) {
  749. ForceZero(plainBuf, plainBufSz);
  750. XMEMCPY(cipherBuf, in, sz);
  751. ForceZero(aTagBuf, TSIP_AES_GCM_AUTH_TAG_SIZE);
  752. XMEMCPY(aTagBuf,authTag,min(authTagSz, TSIP_AES_GCM_AUTH_TAG_SIZE));
  753. XMEMCPY(aadBuf, authIn, authInSz);
  754. }
  755. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  756. if (ret == 0 &&
  757. userCtx->session_key_set == 1) {
  758. /* generate AES-GCM session key. The key stored in
  759. * Aes.ctx.tsip_keyIdx is not used here.
  760. */
  761. err = R_TSIP_TlsGenerateSessionKey(
  762. userCtx->tsip_cipher,
  763. (uint32_t*)userCtx->tsip_masterSecret,
  764. (uint8_t*) userCtx->tsip_clientRandom,
  765. (uint8_t*) userCtx->tsip_serverRandom,
  766. (uint8_t*)&iv[AESGCM_IMP_IV_SZ], /* use exp_IV */
  767. NULL,
  768. NULL,
  769. NULL,
  770. &key_server_aes,
  771. NULL, NULL);
  772. if (err != TSIP_SUCCESS) {
  773. WOLFSSL_MSG("R_TSIP_TlsGenerateSessionKey failed");
  774. ret = -1;
  775. }
  776. } else
  777. #endif
  778. if (ret == 0 &&
  779. (userCtx->user_aes128_key_set == 1 ||
  780. userCtx->user_aes256_key_set == 1)) {
  781. if (aes->ctx.keySize == 32) {
  782. XMEMCPY(&key_server_aes, &userCtx->user_aes256_key_index,
  783. sizeof(tsip_aes_key_index_t));
  784. }
  785. else {
  786. XMEMCPY(&key_server_aes, &userCtx->user_aes128_key_index,
  787. sizeof(tsip_aes_key_index_t));
  788. }
  789. iv_l = iv;
  790. ivSz_l = ivSz;
  791. }
  792. if (ret == 0) {
  793. /* since key_index has iv and ivSz in it, no need to pass them init
  794. * func. Pass NULL and 0 as 3rd and 4th parameter respectively.
  795. *
  796. * It expects to pass iv when users create their own key.
  797. */
  798. err = initFn(&hdl, &key_server_aes, (uint8_t*)iv_l, ivSz_l);
  799. if (err == TSIP_SUCCESS) {
  800. /* pass only AAD and it's size before passing cipher text */
  801. err = updateFn(&hdl, NULL, NULL, 0UL, (uint8_t*)authIn,
  802. authInSz);
  803. }
  804. if (err == TSIP_SUCCESS) {
  805. err = updateFn(&hdl, cipherBuf, plainBuf, sz, NULL, 0UL);
  806. }
  807. if (err != TSIP_SUCCESS) {
  808. WOLFSSL_MSG("R_TSIP_AesXXXGcmDecryptUpdate: failed in decrypt");
  809. ret = -1;
  810. }
  811. if (err == TSIP_SUCCESS) {
  812. dataLen = 0;
  813. err = finalFn(&hdl,
  814. plainBuf + (sz / AES_BLOCK_SIZE) * AES_BLOCK_SIZE,
  815. &dataLen,
  816. aTagBuf,
  817. min(16, authTagSz)); /* TSIP accepts upto 16 byte */
  818. }
  819. if (err == TSIP_SUCCESS) {
  820. /* copy plain data to out */
  821. XMEMCPY(out, plainBuf, sz);
  822. }
  823. else {
  824. WOLFSSL_MSG("R_TSIP_AesXXXGcmDecryptFinal: failed");
  825. ret = -1;
  826. }
  827. }
  828. XFREE(plainBuf, aes->heap, DYNAMIC_TYPE_AES);
  829. XFREE(cipherBuf, aes->heap, DYNAMIC_TYPE_AES);
  830. XFREE(aTagBuf, aes->heap, DYNAMIC_TYPE_AES);
  831. XFREE(aadBuf, aes->heap, DYNAMIC_TYPE_AES);
  832. tsip_hw_unlock();
  833. }
  834. WOLFSSL_LEAVE("wc_tsip_AesGcmDecrypt", ret);
  835. return ret;
  836. }
  837. #endif /* WOLFSSL_RENESAS_TSIP_TLS) || WOLFSSL_RENESAS_TSIP_CRYPTONLY
  838. && NO_WOLFSSL_RENESAS_TSIP_CRYPT_AES */
  839. #endif /* NO_AES */