Skip to content

Commit ebe8816

Browse files
committed
Code size reductions (check RX TSIP enables).
1 parent 78362bc commit ebe8816

3 files changed

Lines changed: 55 additions & 33 deletions

File tree

wolfcrypt/src/port/Renesas/renesas_tsip_aes.c

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -611,50 +611,56 @@ int wc_tsip_AesCtr(struct Aes* aes, byte* out, const byte* in, word32 sz)
611611
int ret;
612612
byte *iv;
613613

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;
614+
if ((in == NULL) || (out == NULL) || (aes == NULL)) {
615+
return BAD_FUNC_ARG;
616+
}
620617

621618
if ((ret = tsip_hw_lock()) != 0) {
622619
WOLFSSL_MSG("Failed to lock");
623620
return ret;
624621
}
625622

623+
/* while doing TLS handshake, TSIP driver keeps true-key and iv *
624+
* on the device. iv is dummy */
625+
iv = (uint8_t*)aes->reg;
626+
626627
if (aes->ctx.keySize == 16) {
628+
#if defined(TSIP_AES_128_CTR) && TSIP_AES_128_CTR == 1
627629
ret = R_TSIP_Aes128CtrInit(&_handle, &aes->ctx.tsip_keyIdx, iv);
630+
if (ret == TSIP_SUCCESS) {
631+
ret = R_TSIP_Aes128CtrUpdate(&_handle, (uint8_t*)in,
632+
(uint8_t*)out, sz);
633+
if (ret == TSIP_SUCCESS) {
634+
ret = R_TSIP_Aes128CtrFinal(&_handle);
635+
}
636+
}
637+
#else
638+
ret = NOT_COMPILED_IN;
639+
#endif
628640
}
629-
else if (aes->ctx.keySize == 32) {
641+
if (aes->ctx.keySize == 32) {
642+
#if defined(TSIP_AES_256_CTR) && TSIP_AES_256_CTR == 1
630643
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);
644+
if (ret == TSIP_SUCCESS) {
645+
ret = R_TSIP_Aes256CtrUpdate(&_handle, (uint8_t*)in,
646+
(uint8_t*)out, sz);
647+
if (ret == TSIP_SUCCESS) {
648+
ret = R_TSIP_Aes256CtrFinal(&_handle);
649+
}
650650
}
651+
#else
652+
ret = NOT_COMPILED_IN;
653+
#endif
651654
}
652-
else {
655+
656+
if (ret != TSIP_SUCCESS) {
657+
WOLFSSL_ERROR(ret);
653658
WOLFSSL_MSG("TSIP AES CTR failed");
654659
ret = -1;
655660
}
656661

657662
tsip_hw_unlock();
663+
658664
return ret;
659665
}
660666
#endif /* WOLFSSL_AES_COUNTER */
@@ -720,7 +726,7 @@ int wc_tsip_AesGcmEncrypt(
720726
if (aes->ctx.keySize != 16 && aes->ctx.keySize != 32) {
721727
WOLFSSL_MSG("illegal key size");
722728
WOLFSSL_LEAVE("wc_tsip_AesGcmEncrypt", BAD_FUNC_ARG);
723-
return BAD_FUNC_ARG;
729+
return BAD_FUNC_ARG;
724730
}
725731

726732
if (aes->ctx.keySize == 16) {

wolfcrypt/src/port/Renesas/renesas_tsip_util.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ extern uint32_t s_flash[];
7272
extern uint32_t s_inst1[R_TSIP_SINST_WORD_SIZE];
7373
#endif
7474

75+
#ifndef SINGLE_THREADED
7576
wolfSSL_Mutex tsip_mutex;
7677
static int tsip_CryptHwMutexInit_ = 0;
78+
#endif
7779
static tsip_key_data g_user_key_info;
7880
struct WOLFSSL_HEAP_HINT* tsip_heap_hint = NULL;
7981

@@ -2476,6 +2478,7 @@ int tsip_ImportPublicKey(TsipUserCtx* tuc, int keyType)
24762478
tuc->keyflgs_crypt.bits.eccpub_key_set = 0;
24772479
#endif
24782480
if (keyType == TSIP_KEY_TYPE_ECDSAP256) {
2481+
#if defined(TSIP_ECDSA_P256) && TSIP_ECDSA_P256 == 1
24792482
err = R_TSIP_GenerateEccP256PublicKeyIndex(
24802483
provisioning_key, iv, (uint8_t*)encPubKey,
24812484
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
@@ -2484,8 +2487,12 @@ int tsip_ImportPublicKey(TsipUserCtx* tuc, int keyType)
24842487
&tuc->eccpub_keyIdx
24852488
#endif
24862489
);
2490+
#else
2491+
err = NOT_COMPILED_IN;
2492+
#endif
24872493
}
24882494
else if (keyType == TSIP_KEY_TYPE_ECDSAP384) {
2495+
#if defined(TSIP_ECDSA_P384) && TSIP_ECDSA_P384 == 1
24892496
err = R_TSIP_GenerateEccP384PublicKeyIndex(
24902497
provisioning_key, iv, (uint8_t*)encPubKey,
24912498
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
@@ -2494,6 +2501,9 @@ int tsip_ImportPublicKey(TsipUserCtx* tuc, int keyType)
24942501
&tuc->eccpub_keyIdx
24952502
#endif
24962503
);
2504+
#else
2505+
err = NOT_COMPILED_IN;
2506+
#endif
24972507
}
24982508
if (err == TSIP_SUCCESS) {
24992509
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
@@ -2619,6 +2629,7 @@ int tsip_usable(const WOLFSSL *ssl, uint8_t session_key_generated)
26192629
}
26202630
#endif /* WOLFSSL_RENESAS_TSIP_TLS */
26212631

2632+
#ifndef SINGLE_THREADED
26222633
/*
26232634
* lock hw engine.
26242635
* this should be called before using engine.
@@ -2654,6 +2665,7 @@ void tsip_hw_unlock(void)
26542665
{
26552666
tsip_CryptHwMutexUnLock(&tsip_mutex);
26562667
}
2668+
#endif
26572669

26582670
/* open TSIP driver
26592671
* return 0 on success.

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,17 @@ WOLFSSL_API int wc_tsip_Sha256HmacGenerate(
639639
word32 sz,
640640
byte* digest);
641641

642-
WOLFSSL_LOCAL int tsip_Open();
642+
WOLFSSL_LOCAL int tsip_Open(void);
643643

644-
WOLFSSL_LOCAL void tsip_Close();
644+
WOLFSSL_LOCAL void tsip_Close(void);
645645

646-
WOLFSSL_LOCAL int tsip_hw_lock();
647-
648-
WOLFSSL_LOCAL void tsip_hw_unlock( void );
646+
#ifdef SINGLE_THREADED
647+
#define tsip_hw_lock() 0
648+
#define tsip_hw_unlock()
649+
#else
650+
WOLFSSL_LOCAL int tsip_hw_lock(void);
651+
WOLFSSL_LOCAL void tsip_hw_unlock(void);
652+
#endif
649653

650654
WOLFSSL_LOCAL int tsip_usable(const struct WOLFSSL *ssl,
651655
uint8_t session_key_generated);

0 commit comments

Comments
 (0)