Skip to content

Commit 78362bc

Browse files
committed
Changes to support Renesas RX TSIP AES CTR.
1 parent 978a29d commit 78362bc

2 files changed

Lines changed: 107 additions & 16 deletions

File tree

wolfcrypt/src/port/Renesas/renesas_tsip_aes.c

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
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
471489
int 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 */

wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include <wolfssl/wolfcrypt/types.h>
4040
#include <wolfssl/wolfcrypt/logging.h>
4141
#include <wolfssl/wolfcrypt/hash.h>
42-
#ifndef WOLFSSL_RENESAS_TSIP_CRYPTONLY
42+
#ifdef WOLFSSL_RENESAS_TSIP_TLS
4343
#include <wolfssl/ssl.h>
4444
#endif
4545
#ifdef WOLF_CRYPTO_CB
@@ -573,35 +573,49 @@ WOLFSSL_API int wc_tsip_generateVerifyData(
573573
const uint8_t* side,
574574
const uint8_t* handshake_hash,
575575
uint8_t* hashes);
576+
576577
#ifndef NO_AES
578+
#ifdef HAVE_AES_CBC
577579
WOLFSSL_API int wc_tsip_AesCbcEncrypt(
578-
Aes* aes,
580+
struct Aes* aes,
579581
byte* out,
580582
const byte* in,
581583
word32 sz);
582584

583585
WOLFSSL_API int wc_tsip_AesCbcDecrypt(
584-
Aes* aes,
586+
struct Aes* aes,
585587
byte* out,
586588
const byte* in,
587589
word32 sz);
590+
#endif /* HAVE_AES_CBC */
588591

592+
#ifdef WOLFSSL_AES_COUNTER
593+
WOLFSSL_API int wc_tsip_AesCtr(
594+
struct Aes*,
595+
byte* out,
596+
const byte* in,
597+
word32 sz);
598+
#endif /* WOLFSSL_AES_COUNTER */
599+
600+
#ifdef HAVE_AESGCM
589601
WOLFSSL_API int wc_tsip_AesGcmEncrypt(
590-
Aes* aes, byte* out,
602+
struct Aes* aes, byte* out,
591603
const byte* in, word32 sz,
592604
byte* iv, word32 ivSz,
593605
byte* authTag, word32 authTagSz,
594606
const byte* authIn, word32 authInSz,
595607
void* ctx);
596608

597609
WOLFSSL_API int wc_tsip_AesGcmDecrypt(
598-
Aes* aes, byte* out,
610+
struct Aes* aes, byte* out,
599611
const byte* in, word32 sz,
600612
const byte* iv, word32 ivSz,
601613
const byte* authTag, word32 authTagSz,
602614
const byte* authIn, word32 authInSz,
603615
void* ctx);
604-
#endif /* NO_AES */
616+
#endif /* HAVE_AESGCM */
617+
#endif /* !NO_AES */
618+
605619
WOLFSSL_API int wc_tsip_ShaXHmacVerify(
606620
const struct WOLFSSL *ssl,
607621
const byte* message,

0 commit comments

Comments
 (0)