@@ -490,6 +490,9 @@ static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
490490/* ECC sign raw digest callback
491491 * This callback demonstrates HSM/secure element use case where the private
492492 * key is not passed through PKCS7 structure but obtained independently.
493+ * Note: This example callback is hash-agnostic and will work with any
494+ * hash algorithm. The hashOID parameter can be used to validate or select
495+ * different signing behavior if needed.
493496 */
494497static int eccSignRawDigestCb (PKCS7 * pkcs7 , byte * digest , word32 digestSz ,
495498 byte * out , word32 outSz , byte * privateKey ,
@@ -498,7 +501,11 @@ static int eccSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
498501 int ret ;
499502 word32 idx = 0 ;
500503 word32 sigSz = outSz ;
501- ecc_key ecc ;
504+ #ifdef WOLFSSL_SMALL_STACK
505+ ecc_key * ecc = NULL ;
506+ #else
507+ ecc_key ecc [1 ];
508+ #endif
502509
503510 /* privateKey may be NULL in HSM/secure element use case - we load it
504511 * independently in this callback to simulate that scenario */
@@ -510,15 +517,25 @@ static int eccSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
510517 return -1 ;
511518 }
512519
520+ #ifdef WOLFSSL_SMALL_STACK
521+ ecc = (ecc_key * )XMALLOC (sizeof (ecc_key ), pkcs7 -> heap , DYNAMIC_TYPE_ECC );
522+ if (ecc == NULL ) {
523+ return MEMORY_E ;
524+ }
525+ #endif
526+
513527 /* set up ECC key */
514- ret = wc_ecc_init_ex (& ecc , pkcs7 -> heap , devid );
528+ ret = wc_ecc_init_ex (ecc , pkcs7 -> heap , devid );
515529 if (ret != 0 ) {
530+ #ifdef WOLFSSL_SMALL_STACK
531+ XFREE (ecc , pkcs7 -> heap , DYNAMIC_TYPE_ECC );
532+ #endif
516533 return ret ;
517534 }
518535
519536 /* Load key from test buffer - simulates HSM/secure element access */
520537#if defined(USE_CERT_BUFFERS_256 )
521- ret = wc_EccPrivateKeyDecode (ecc_clikey_der_256 , & idx , & ecc ,
538+ ret = wc_EccPrivateKeyDecode (ecc_clikey_der_256 , & idx , ecc ,
522539 sizeof_ecc_clikey_der_256 );
523540#else
524541 {
@@ -528,28 +545,37 @@ static int eccSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
528545
529546 fp = XFOPEN ("./certs/client-ecc-key.der" , "rb" );
530547 if (fp == XBADFILE ) {
531- wc_ecc_free (& ecc );
548+ wc_ecc_free (ecc );
549+ #ifdef WOLFSSL_SMALL_STACK
550+ XFREE (ecc , pkcs7 -> heap , DYNAMIC_TYPE_ECC );
551+ #endif
532552 return -1 ;
533553 }
534554 keySz = (int )XFREAD (keyBuf , 1 , sizeof (keyBuf ), fp );
535555 XFCLOSE (fp );
536556 if (keySz <= 0 ) {
537- wc_ecc_free (& ecc );
557+ wc_ecc_free (ecc );
558+ #ifdef WOLFSSL_SMALL_STACK
559+ XFREE (ecc , pkcs7 -> heap , DYNAMIC_TYPE_ECC );
560+ #endif
538561 return -1 ;
539562 }
540- ret = wc_EccPrivateKeyDecode (keyBuf , & idx , & ecc , (word32 )keySz );
563+ ret = wc_EccPrivateKeyDecode (keyBuf , & idx , ecc , (word32 )keySz );
541564 }
542565#endif
543566
544567 /* sign digest */
545568 if (ret == 0 ) {
546- ret = wc_ecc_sign_hash (digest , digestSz , out , & sigSz , pkcs7 -> rng , & ecc );
569+ ret = wc_ecc_sign_hash (digest , digestSz , out , & sigSz , pkcs7 -> rng , ecc );
547570 if (ret == 0 ) {
548571 ret = (int )sigSz ;
549572 }
550573 }
551574
552- wc_ecc_free (& ecc );
575+ wc_ecc_free (ecc );
576+ #ifdef WOLFSSL_SMALL_STACK
577+ XFREE (ecc , pkcs7 -> heap , DYNAMIC_TYPE_ECC );
578+ #endif
553579
554580 return ret ;
555581}
0 commit comments