@@ -1746,22 +1746,34 @@ static int _HandleSha256(whServerContext* ctx, uint16_t magic,
17461746 int ret = 0 ;
17471747 wc_Sha256 sha256 [1 ];
17481748 whMessageCrypto_Sha256Request req ;
1749+ whMessageCrypto_Sha256Response res = {0 };
17491750
17501751 /* THe server SHA256 struct doesn't persist state (it is a union), meaning
17511752 * the devId may get blown away between calls. We must restore the server
17521753 * devId each time */
17531754 sha256 -> devId = ctx -> crypto -> devId ;
17541755
1755- whMessageCrypto_Sha2Response res ;
17561756 /* Translate the request */
17571757 ret = wh_MessageCrypto_TranslateSha256Request (magic , cryptoDataIn , & req );
17581758 if (ret != 0 ) {
17591759 return ret ;
17601760 }
1761- /* always init sha2 struct with the devid */
1762- ret = wc_InitSha256_ex (sha256 , NULL , ctx -> crypto -> devId );
1763- if (ret != 0 ) {
1764- return ret ;
1761+
1762+ /* Validate lastBlockLen to prevent potential buffer overread */
1763+ if (req .lastBlockLen > WC_SHA256_BLOCK_SIZE ) {
1764+ return WH_ERROR_BADARGS ;
1765+ }
1766+
1767+ /* Init the SHA256 context if this is the first time, otherwise restore the
1768+ * hash state from the client */
1769+ if (req .resumeState .hiLen == 0 && req .resumeState .loLen == 0 ) {
1770+ ret = wc_InitSha256_ex (sha256 , NULL , ctx -> crypto -> devId );
1771+ }
1772+ else {
1773+ /* HAVE_DILITHIUM */
1774+ memcpy (sha256 -> digest , req .resumeState .hash , WC_SHA256_DIGEST_SIZE );
1775+ sha256 -> loLen = req .resumeState .loLen ;
1776+ sha256 -> hiLen = req .resumeState .hiLen ;
17651777 }
17661778 /* restore the hash state from the client */
17671779 memcpy (sha256 -> digest , req .resumeState .hash , WC_SHA256_DIGEST_SIZE );
0 commit comments