3636
3737#include <wolfssl/wolfcrypt/wc_port.h>
3838#include <wolfssl/wolfcrypt/error-crypt.h>
39+ #ifdef WOLFSSL_RENESAS_TSIP_TLS
3940#include <wolfssl/internal.h>
41+ #endif
4042#include <wolfssl/wolfcrypt/aes.h>
4143#include "wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h"
4244#ifdef NO_INLINE
@@ -381,24 +383,25 @@ WOLFSSL_LOCAL int tsip_Tls13AesDecrypt(
381383#if (WOLFSSL_RENESAS_TSIP_VER >= 109 )
382384#ifdef WOLF_CRYPTO_CB
383385
384- WOLFSSL_LOCAL int wc_tsip_AesCipher (int devIdArg , wc_CryptoInfo * info ,
385- void * ctx )
386+ int wc_tsip_AesCipher (int devIdArg , wc_CryptoInfo * info , void * ctx )
386387{
387388 int ret = WC_NO_ERR_TRACE (NOT_COMPILED_IN );
388389 TsipUserCtx * cbInfo = (TsipUserCtx * )ctx ;
389390
390391 WOLFSSL_ENTER ("wc_tsip_AesCipher" );
391392
392- if (info == NULL || ctx == NULL )
393+ if (info == NULL || ctx == NULL ) {
393394 return BAD_FUNC_ARG ;
395+ }
394396
395- if ( info -> algo_type == WC_ALGO_TYPE_CIPHER ) {
397+ ( void ) devIdArg ;
396398
397- #if !defined(NO_AES ) || !defined(NO_DES3 )
399+ if (info -> algo_type == WC_ALGO_TYPE_CIPHER ) {
400+ #if !defined(NO_AES )
398401#ifdef HAVE_AESGCM
399402 if (info -> cipher .type == WC_CIPHER_AES_GCM
400403 #ifdef WOLFSSL_RENESAS_TSIP_TLS
401- && cbInfo -> session_key_set == 1
404+ && cbInfo != NULL && cbInfo -> session_key_set == 1
402405 #endif
403406 ) {
404407
@@ -433,10 +436,26 @@ WOLFSSL_LOCAL int wc_tsip_AesCipher(int devIdArg, wc_CryptoInfo* info,
433436 }
434437 }
435438 #endif /* HAVE_AESGCM */
439+
440+ #ifdef WOLFSSL_AES_COUNTER
441+ if (info -> cipher .type == WC_CIPHER_AES_CTR
442+ #ifdef WOLFSSL_RENESAS_TSIP_TLS
443+ && cbInfo != NULL && cbInfo -> session_key_set == 1
444+ #endif
445+ ) {
446+ /* encrypt and decrypt use same routine */
447+ ret = wc_tsip_AesCtr (
448+ info -> cipher .aesctr .aes ,
449+ (byte * )info -> cipher .aesctr .out ,
450+ (byte * )info -> cipher .aesctr .in ,
451+ info -> cipher .aesctr .sz );
452+ }
453+ #endif /* WOLFSSL_AES_COUNTER */
454+
436455 #ifdef HAVE_AES_CBC
437456 if (info -> cipher .type == WC_CIPHER_AES_CBC
438457 #ifdef WOLFSSL_RENESAS_TSIP_TLS
439- && cbInfo -> session_key_set == 1
458+ && cbInfo != NULL && cbInfo -> session_key_set == 1
440459 #endif
441460 ) {
442461
@@ -457,7 +476,7 @@ WOLFSSL_LOCAL int wc_tsip_AesCipher(int devIdArg, wc_CryptoInfo* info,
457476 }
458477 }
459478 #endif /* HAVE_AES_CBC */
460- #endif /* !NO_AES || !NO_DES3 */
479+ #endif /* !NO_AES */
461480
462481 }
463482 WOLFSSL_LEAVE ("wc_tsip_AesCipher" , ret );
@@ -466,8 +485,7 @@ WOLFSSL_LOCAL int wc_tsip_AesCipher(int devIdArg, wc_CryptoInfo* info,
466485#endif /* WOLF_CRYPTO_CB */
467486#endif /* WOLFSSL_RENESAS_TSIP_VER >= 109 */
468487
469-
470-
488+ #ifdef HAVE_AES_CBC
471489int wc_tsip_AesCbcEncrypt (struct Aes * aes , byte * out , const byte * in , word32 sz )
472490{
473491 tsip_aes_handle_t _handle ;
@@ -584,6 +602,64 @@ int wc_tsip_AesCbcDecrypt(struct Aes* aes, byte* out, const byte* in, word32 sz)
584602 tsip_hw_unlock ();
585603 return ret ;
586604}
605+ #endif /* HAVE_AES_CBC */
606+
607+ #ifdef WOLFSSL_AES_COUNTER
608+ int wc_tsip_AesCtr (struct Aes * aes , byte * out , const byte * in , word32 sz )
609+ {
610+ tsip_aes_handle_t _handle ;
611+ int ret ;
612+ byte * iv ;
613+
614+ if ((in == NULL ) || (out == NULL ) || (aes == NULL ))
615+ return BAD_FUNC_ARG ;
616+
617+ /* while doing TLS handshake, TSIP driver keeps true-key and iv *
618+ * on the device. iv is dummy */
619+ iv = (uint8_t * )aes -> reg ;
620+
621+ if ((ret = tsip_hw_lock ()) != 0 ) {
622+ WOLFSSL_MSG ("Failed to lock" );
623+ return ret ;
624+ }
625+
626+ if (aes -> ctx .keySize == 16 ) {
627+ ret = R_TSIP_Aes128CtrInit (& _handle , & aes -> ctx .tsip_keyIdx , iv );
628+ }
629+ else if (aes -> ctx .keySize == 32 ) {
630+ ret = R_TSIP_Aes256CtrInit (& _handle , & aes -> ctx .tsip_keyIdx , iv );
631+ }
632+ else {
633+ tsip_hw_unlock ();
634+ return -1 ;
635+ }
636+
637+ if (aes -> ctx .keySize == 16 )
638+ ret = R_TSIP_Aes128CtrUpdate (& _handle , (uint8_t * )in ,
639+ (uint8_t * )out , sz );
640+ else
641+ ret = R_TSIP_Aes256CtrUpdate (& _handle , (uint8_t * )in ,
642+ (uint8_t * )out , sz );
643+
644+ if (ret == TSIP_SUCCESS ) {
645+ if (aes -> ctx .keySize == 16 ) {
646+ ret = R_TSIP_Aes128CtrFinal (& _handle );
647+ }
648+ else {
649+ ret = R_TSIP_Aes256CtrFinal (& _handle );
650+ }
651+ }
652+ else {
653+ WOLFSSL_MSG ("TSIP AES CTR failed" );
654+ ret = -1 ;
655+ }
656+
657+ tsip_hw_unlock ();
658+ return ret ;
659+ }
660+ #endif /* WOLFSSL_AES_COUNTER */
661+
662+ #ifdef HAVE_AESGCM
587663/*
588664 * Encrypt plain data then output encrypted data and authentication tag data.
589665 * The session key used for encryption is generated inside this function and
@@ -975,6 +1051,7 @@ int wc_tsip_AesGcmDecrypt(
9751051 WOLFSSL_LEAVE ("wc_tsip_AesGcmDecrypt" , ret );
9761052 return ret ;
9771053}
1054+ #endif /* HAVE_AESGCM */
9781055#endif /* WOLFSSL_RENESAS_TSIP_TLS) || WOLFSSL_RENESAS_TSIP_CRYPTONLY
9791056 && NO_WOLFSSL_RENESAS_TSIP_CRYPT_AES */
9801057#endif /* NO_AES */
0 commit comments