Skip to content

Commit 7a8e71b

Browse files
committed
Small stack and no malloc improvements
1 parent 72054ca commit 7a8e71b

File tree

1 file changed

+97
-38
lines changed

1 file changed

+97
-38
lines changed

wolfcrypt/src/port/st/stsafe.c

Lines changed: 97 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,12 @@ static int stsafe_shared_secret(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
379379
*/
380380
static int stsafe_read_certificate(uint8_t** ppCert, uint32_t* pCertLen)
381381
{
382+
#ifdef WOLFSSL_NO_MALLOC
383+
/* Certificate reading requires dynamic allocation */
384+
(void)ppCert;
385+
(void)pCertLen;
386+
return NOT_COMPILED_IN;
387+
#else
382388
int rc = STSAFE_A_OK;
383389
stse_ReturnCode_t ret;
384390
uint16_t certLen = 0;
@@ -418,6 +424,7 @@ static int stsafe_read_certificate(uint8_t** ppCert, uint32_t* pCertLen)
418424
}
419425

420426
return rc;
427+
#endif /* WOLFSSL_NO_MALLOC */
421428
}
422429

423430
#if !defined(WC_NO_RNG) && defined(USE_STSAFE_RNG_SEED)
@@ -628,15 +635,30 @@ static int stsafe_verify(stsafe_curve_id_t curve_id, uint8_t* pHash,
628635
int rc = (int)(uint8_t)-1;
629636
uint8_t status_code;
630637
int key_sz = stsafe_get_key_size(curve_id);
638+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
631639
StSafeA_CoordinateBuffer* X = NULL;
632640
StSafeA_CoordinateBuffer* Y = NULL;
633641
StSafeA_SignatureBuffer* R = NULL;
634642
StSafeA_SignatureBuffer* S = NULL;
635643
StSafeA_SignatureBuffer* Hash = NULL;
644+
#else
645+
/* Stack buffers: 2 bytes for Length + STSAFE_MAX_KEY_LEN for Data */
646+
byte R_buf[2 + STSAFE_MAX_KEY_LEN];
647+
byte S_buf[2 + STSAFE_MAX_KEY_LEN];
648+
byte Hash_buf[2 + STSAFE_MAX_KEY_LEN];
649+
byte X_buf[2 + STSAFE_MAX_KEY_LEN];
650+
byte Y_buf[2 + STSAFE_MAX_KEY_LEN];
651+
StSafeA_SignatureBuffer* R = (StSafeA_SignatureBuffer*)R_buf;
652+
StSafeA_SignatureBuffer* S = (StSafeA_SignatureBuffer*)S_buf;
653+
StSafeA_SignatureBuffer* Hash = (StSafeA_SignatureBuffer*)Hash_buf;
654+
StSafeA_CoordinateBuffer* X = (StSafeA_CoordinateBuffer*)X_buf;
655+
StSafeA_CoordinateBuffer* Y = (StSafeA_CoordinateBuffer*)Y_buf;
656+
#endif
636657
StSafeA_VerifySignatureBuffer* Verif = NULL;
637658

638659
*pResult = 0;
639660

661+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
640662
/* Allocate buffers */
641663
R = (StSafeA_SignatureBuffer*)XMALLOC(key_sz + 2, NULL,
642664
DYNAMIC_TYPE_TMP_BUFFER);
@@ -649,37 +671,49 @@ static int stsafe_verify(stsafe_curve_id_t curve_id, uint8_t* pHash,
649671
Y = (StSafeA_CoordinateBuffer*)XMALLOC(key_sz + 2, NULL,
650672
DYNAMIC_TYPE_TMP_BUFFER);
651673

652-
if (X != NULL && Y != NULL && R != NULL && S != NULL && Hash != NULL) {
653-
R->Length = key_sz;
654-
S->Length = key_sz;
655-
Hash->Length = key_sz;
656-
X->Length = key_sz;
657-
Y->Length = key_sz;
658-
659-
XMEMCPY(R->Data, pSigRS, key_sz);
660-
XMEMCPY(S->Data, pSigRS + key_sz, key_sz);
661-
XMEMCPY(Hash->Data, pHash, key_sz);
662-
XMEMCPY(X->Data, pPubKeyX, key_sz);
663-
XMEMCPY(Y->Data, pPubKeyY, key_sz);
664-
665-
status_code = StSafeA_VerifyMessageSignature(g_stsafe_handle,
666-
curve_id, X, Y, R, S, Hash, &Verif, STSAFE_A_NO_MAC);
667-
668-
if (status_code == STSAFE_A_OK && Verif != NULL) {
669-
*pResult = Verif->SignatureValidity ? 1 : 0;
670-
if (Verif->SignatureValidity) {
671-
rc = STSAFE_A_OK;
672-
}
674+
if (X == NULL || Y == NULL || R == NULL || S == NULL || Hash == NULL) {
675+
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
676+
XFREE(S, NULL, DYNAMIC_TYPE_TMP_BUFFER);
677+
XFREE(Hash, NULL, DYNAMIC_TYPE_TMP_BUFFER);
678+
XFREE(X, NULL, DYNAMIC_TYPE_TMP_BUFFER);
679+
XFREE(Y, NULL, DYNAMIC_TYPE_TMP_BUFFER);
680+
return MEMORY_E;
681+
}
682+
#endif
683+
684+
R->Length = key_sz;
685+
S->Length = key_sz;
686+
Hash->Length = key_sz;
687+
X->Length = key_sz;
688+
Y->Length = key_sz;
689+
690+
XMEMCPY(R->Data, pSigRS, key_sz);
691+
XMEMCPY(S->Data, pSigRS + key_sz, key_sz);
692+
XMEMCPY(Hash->Data, pHash, key_sz);
693+
XMEMCPY(X->Data, pPubKeyX, key_sz);
694+
XMEMCPY(Y->Data, pPubKeyY, key_sz);
695+
696+
status_code = StSafeA_VerifyMessageSignature(g_stsafe_handle,
697+
curve_id, X, Y, R, S, Hash, &Verif, STSAFE_A_NO_MAC);
698+
699+
if (status_code == STSAFE_A_OK && Verif != NULL) {
700+
*pResult = Verif->SignatureValidity ? 1 : 0;
701+
if (Verif->SignatureValidity) {
702+
rc = STSAFE_A_OK;
673703
}
674-
/* Free SDK-allocated buffer */
675-
XFREE(Verif, NULL, DYNAMIC_TYPE_TMP_BUFFER);
676704
}
705+
#ifndef WOLFSSL_NO_MALLOC
706+
/* Free SDK-allocated buffer */
707+
XFREE(Verif, NULL, DYNAMIC_TYPE_TMP_BUFFER);
708+
#endif
677709

710+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
678711
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
679712
XFREE(S, NULL, DYNAMIC_TYPE_TMP_BUFFER);
680713
XFREE(Hash, NULL, DYNAMIC_TYPE_TMP_BUFFER);
681714
XFREE(X, NULL, DYNAMIC_TYPE_TMP_BUFFER);
682715
XFREE(Y, NULL, DYNAMIC_TYPE_TMP_BUFFER);
716+
#endif
683717

684718
return rc;
685719
}
@@ -694,38 +728,56 @@ static int stsafe_shared_secret(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
694728
int rc = (int)(uint8_t)-1;
695729
uint8_t status_code;
696730
int key_sz = stsafe_get_key_size(curve_id);
731+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
697732
StSafeA_CoordinateBuffer* peerX = NULL;
698733
StSafeA_CoordinateBuffer* peerY = NULL;
734+
#else
735+
/* Stack buffers: 2 bytes for Length + STSAFE_MAX_KEY_LEN for Data */
736+
byte peerX_buf[2 + STSAFE_MAX_KEY_LEN];
737+
byte peerY_buf[2 + STSAFE_MAX_KEY_LEN];
738+
StSafeA_CoordinateBuffer* peerX = (StSafeA_CoordinateBuffer*)peerX_buf;
739+
StSafeA_CoordinateBuffer* peerY = (StSafeA_CoordinateBuffer*)peerY_buf;
740+
#endif
699741
StSafeA_SharedSecretBuffer* sharedSecret = NULL;
700742

701743
stsafe_set_host_keys(g_stsafe_handle);
702744

745+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
703746
peerX = (StSafeA_CoordinateBuffer*)XMALLOC(key_sz + 2, NULL,
704747
DYNAMIC_TYPE_TMP_BUFFER);
705748
peerY = (StSafeA_CoordinateBuffer*)XMALLOC(key_sz + 2, NULL,
706749
DYNAMIC_TYPE_TMP_BUFFER);
707750

708-
if (peerX != NULL && peerY != NULL) {
709-
peerX->Length = key_sz;
710-
peerY->Length = key_sz;
711-
XMEMCPY(peerX->Data, pPubKeyX, key_sz);
712-
XMEMCPY(peerY->Data, pPubKeyY, key_sz);
751+
if (peerX == NULL || peerY == NULL) {
752+
XFREE(peerX, NULL, DYNAMIC_TYPE_TMP_BUFFER);
753+
XFREE(peerY, NULL, DYNAMIC_TYPE_TMP_BUFFER);
754+
return MEMORY_E;
755+
}
756+
#endif
713757

714-
status_code = StSafeA_EstablishKey(g_stsafe_handle, slot,
715-
peerX, peerY, &sharedSecret, STSAFE_A_HOST_C_MAC);
758+
peerX->Length = key_sz;
759+
peerY->Length = key_sz;
760+
XMEMCPY(peerX->Data, pPubKeyX, key_sz);
761+
XMEMCPY(peerY->Data, pPubKeyY, key_sz);
716762

717-
if (status_code == STSAFE_A_OK && sharedSecret != NULL) {
718-
*pSharedSecretLen = sharedSecret->SharedSecret.Length;
719-
XMEMCPY(pSharedSecret, sharedSecret->SharedSecret.Data,
720-
sharedSecret->SharedSecret.Length);
721-
rc = STSAFE_A_OK;
722-
}
723-
/* Free SDK-allocated buffer */
724-
XFREE(sharedSecret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
763+
status_code = StSafeA_EstablishKey(g_stsafe_handle, slot,
764+
peerX, peerY, &sharedSecret, STSAFE_A_HOST_C_MAC);
765+
766+
if (status_code == STSAFE_A_OK && sharedSecret != NULL) {
767+
*pSharedSecretLen = sharedSecret->SharedSecret.Length;
768+
XMEMCPY(pSharedSecret, sharedSecret->SharedSecret.Data,
769+
sharedSecret->SharedSecret.Length);
770+
rc = STSAFE_A_OK;
725771
}
772+
#ifndef WOLFSSL_NO_MALLOC
773+
/* Free SDK-allocated buffer */
774+
XFREE(sharedSecret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
775+
#endif
726776

777+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
727778
XFREE(peerX, NULL, DYNAMIC_TYPE_TMP_BUFFER);
728779
XFREE(peerY, NULL, DYNAMIC_TYPE_TMP_BUFFER);
780+
#endif
729781

730782
return rc;
731783
}
@@ -735,6 +787,12 @@ static int stsafe_shared_secret(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
735787
*/
736788
static int stsafe_read_certificate(uint8_t** ppCert, uint32_t* pCertLen)
737789
{
790+
#ifdef WOLFSSL_NO_MALLOC
791+
/* Certificate reading requires dynamic allocation */
792+
(void)ppCert;
793+
(void)pCertLen;
794+
return NOT_COMPILED_IN;
795+
#else
738796
int rc = STSAFE_A_OK;
739797
uint8_t status_code;
740798
StSafeA_ReadBuffer* readBuf = NULL;
@@ -812,6 +870,7 @@ static int stsafe_read_certificate(uint8_t** ppCert, uint32_t* pCertLen)
812870
}
813871

814872
return rc;
873+
#endif /* WOLFSSL_NO_MALLOC */
815874
}
816875

817876
#if !defined(WC_NO_RNG) && defined(USE_STSAFE_RNG_SEED)

0 commit comments

Comments
 (0)