@@ -302,16 +302,25 @@ static int _HandleRsaKeyGen(whServerContext* ctx, uint16_t magic,
302302 printf ("[server] RsaKeyGen UniqueId: keyId:%u, ret:%d\n" ,
303303 key_id , ret );
304304#endif
305+ if (ret != WH_ERROR_OK ) {
306+ /* Early return on unique ID generation failure */
307+ wc_FreeRsaKey (rsa );
308+ return ret ;
309+ }
305310 }
306311
307- ret = wh_Server_CacheImportRsaKey (ctx , rsa , key_id , flags ,
308- label_size , label );
312+ if (ret == 0 ) {
313+ ret = wh_Server_CacheImportRsaKey (ctx , rsa , key_id , flags ,
314+ label_size , label );
315+ }
309316#ifdef DEBUG_CRYPTOCB_VERBOSE
310317 printf ("[server] RsaKeyGen CacheKeyRsa: keyId:%u, ret:%d\n" ,
311318 key_id , ret );
312319#endif
313- res .keyId = WH_KEYID_ID (key_id );
314- res .len = 0 ;
320+ if (ret == 0 ) {
321+ res .keyId = WH_KEYID_ID (key_id );
322+ res .len = 0 ;
323+ }
315324 }
316325 }
317326 wc_FreeRsaKey (rsa );
@@ -742,9 +751,16 @@ static int _HandleEccKeyGen(whServerContext* ctx, uint16_t magic,
742751 printf ("[server] %s UniqueId: keyId:%u, ret:%d\n" , __func__ ,
743752 key_id , ret );
744753#endif
754+ if (ret != WH_ERROR_OK ) {
755+ /* Early return on unique ID generation failure */
756+ wc_ecc_free (key );
757+ return ret ;
758+ }
759+ }
760+ if (ret == 0 ) {
761+ ret = wh_Server_EccKeyCacheImport (ctx , key , key_id , flags ,
762+ label_size , label );
745763 }
746- ret = wh_Server_EccKeyCacheImport (ctx , key , key_id , flags ,
747- label_size , label );
748764#ifdef DEBUG_CRYPTOCB
749765 printf ("[server] %s CacheImport: keyId:%u, ret:%d\n" , __func__ ,
750766 key_id , ret );
@@ -1159,10 +1175,17 @@ static int _HandleCurve25519KeyGen(whServerContext* ctx, uint16_t magic,
11591175 printf ("[server] %s UniqueId: keyId:%u, ret:%d\n" , __func__ ,
11601176 key_id , ret );
11611177#endif
1178+ if (ret != WH_ERROR_OK ) {
1179+ /* Early return on unique ID generation failure */
1180+ wc_curve25519_free (key );
1181+ return ret ;
1182+ }
11621183 }
11631184
1164- ret = wh_Server_CacheImportCurve25519Key (
1165- ctx , key , key_id , flags , label_size , label );
1185+ if (ret == 0 ) {
1186+ ret = wh_Server_CacheImportCurve25519Key (
1187+ ctx , key , key_id , flags , label_size , label );
1188+ }
11661189#ifdef DEBUG_CRYPTOCB
11671190 printf ("[server] %s CacheImport: keyId:%u, ret:%d\n" , __func__ ,
11681191 key_id , ret );
@@ -1928,12 +1951,16 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq,
19281951 if (moveToBigCache == 1 ) {
19291952 ret = wh_Server_KeystoreEvictKey (ctx , keyId );
19301953 }
1931- meta -> id = keyId ;
1932- meta -> len = sizeof (ctx -> crypto -> algoCtx .cmac );
1933- ret = wh_Server_KeystoreCacheKey (
1934- ctx , meta , (uint8_t * )ctx -> crypto -> algoCtx .cmac );
1935- res .keyId = WH_KEYID_ID (keyId );
1936- res .outSz = 0 ;
1954+ if (ret == 0 ) {
1955+ meta -> id = keyId ;
1956+ meta -> len = sizeof (ctx -> crypto -> algoCtx .cmac );
1957+ ret = wh_Server_KeystoreCacheKey (
1958+ ctx , meta , (uint8_t * )ctx -> crypto -> algoCtx .cmac );
1959+ if (ret == 0 ) {
1960+ res .keyId = WH_KEYID_ID (keyId );
1961+ res .outSz = 0 ;
1962+ }
1963+ }
19371964#ifdef DEBUG_CRYPTOCB_VERBOSE
19381965 printf ("[server] cmac saved state in keyid:%x %x len:%u ret:%d type:%d\n" ,
19391966 keyId , WH_KEYID_ID (keyId ), meta -> len , ret , ctx -> crypto -> algoCtx .cmac -> type );
@@ -1969,7 +1996,8 @@ static int _HandleSha256(whServerContext* ctx, uint16_t magic,
19691996 int ret = 0 ;
19701997 wc_Sha256 sha256 [1 ];
19711998 whMessageCrypto_Sha256Request req ;
1972- whMessageCrypto_Sha2Response res ;
1999+ whMessageCrypto_Sha2Response res = {0 };
2000+
19732001 /* Translate the request */
19742002 ret = wh_MessageCrypto_TranslateSha256Request (magic , cryptoDataIn , & req );
19752003 if (ret != 0 ) {
@@ -1986,6 +2014,10 @@ static int _HandleSha256(whServerContext* ctx, uint16_t magic,
19862014 sha256 -> hiLen = req .resumeState .hiLen ;
19872015
19882016 if (req .isLastBlock ) {
2017+ /* Validate lastBlockLen to prevent potential buffer overread */
2018+ if ((unsigned int )req .lastBlockLen > WC_SHA256_BLOCK_SIZE ) {
2019+ return WH_ERROR_BADARGS ;
2020+ }
19892021 /* wolfCrypt (or cryptoCb) is responsible for last block padding */
19902022 if (ret == 0 ) {
19912023 ret = wc_Sha256Update (sha256 , req .inBlock , req .lastBlockLen );
@@ -2348,9 +2380,17 @@ static int _HandleMlDsaKeyGen(whServerContext* ctx, uint16_t magic,
23482380 printf ("[server] %s UniqueId: keyId:%u, ret:%d\n" ,
23492381 __func__ , key_id , ret );
23502382#endif
2383+ if (ret != WH_ERROR_OK ) {
2384+ /* Early return on unique ID generation failure
2385+ */
2386+ wc_MlDsaKey_Free (key );
2387+ return ret ;
2388+ }
2389+ }
2390+ if (ret == 0 ) {
2391+ ret = wh_Server_MlDsaKeyCacheImport (
2392+ ctx , key , key_id , flags , label_size , label );
23512393 }
2352- ret = wh_Server_MlDsaKeyCacheImport (
2353- ctx , key , key_id , flags , label_size , label );
23542394#ifdef DEBUG_CRYPTOCB
23552395 printf ("[server] %s CacheImport: keyId:%u, ret:%d\n" ,
23562396 __func__ , key_id , ret );
@@ -2410,6 +2450,16 @@ static int _HandleMlDsaSign(whServerContext* ctx, uint16_t magic,
24102450 uint32_t options = req .options ;
24112451 int evict = !!(options & WH_MESSAGE_CRYPTO_MLDSA_SIGN_OPTIONS_EVICT );
24122452
2453+ /* Validate input length against available data to prevent buffer overread
2454+ */
2455+ if (inSize < sizeof (whMessageCrypto_MlDsaSignRequest )) {
2456+ return WH_ERROR_BADARGS ;
2457+ }
2458+ word32 available_data = inSize - sizeof (whMessageCrypto_MlDsaSignRequest );
2459+ if (in_len > available_data ) {
2460+ return WH_ERROR_BADARGS ;
2461+ }
2462+
24132463 /* Response message */
24142464 byte * res_out =
24152465 (uint8_t * )(cryptoDataOut ) + sizeof (whMessageCrypto_MlDsaSignResponse );
@@ -2479,6 +2529,17 @@ static int _HandleMlDsaVerify(whServerContext* ctx, uint16_t magic,
24792529 uint32_t sig_len = req .sigSz ;
24802530 byte * req_sig =
24812531 (uint8_t * )(cryptoDataIn ) + sizeof (whMessageCrypto_MlDsaVerifyRequest );
2532+
2533+ /* Validate lengths against available payload (overflow-safe) */
2534+ if (inSize < sizeof (whMessageCrypto_MlDsaVerifyRequest )) {
2535+ return WH_ERROR_BADARGS ;
2536+ }
2537+ uint32_t available = inSize - sizeof (whMessageCrypto_MlDsaVerifyRequest );
2538+ if ((sig_len > available ) || (hash_len > available ) ||
2539+ (sig_len > (available - hash_len ))) {
2540+ return WH_ERROR_BADARGS ;
2541+ }
2542+
24822543 byte * req_hash = req_sig + sig_len ;
24832544 int evict = !!(options & WH_MESSAGE_CRYPTO_MLDSA_VERIFY_OPTIONS_EVICT );
24842545
@@ -3463,6 +3524,12 @@ static int _HandleMlDsaKeyGenDma(whServerContext* ctx, uint16_t magic,
34633524 printf ("[server] %s UniqueId: keyId:%u, ret:%d\n" ,
34643525 __func__ , keyId , ret );
34653526#endif
3527+ if (ret != WH_ERROR_OK ) {
3528+ /* Early return on unique ID generation failure
3529+ */
3530+ wc_MlDsaKey_Free (key );
3531+ return ret ;
3532+ }
34663533 }
34673534
34683535 if (ret == 0 ) {
0 commit comments