Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions src/wh_server_img_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ int wh_Server_ImgMgrVerifyMethodEccWithSha256(whServerImgMgrContext* context,
/* Hash the image data from server pointer using one-shot API */
ret = wc_Sha256Hash_ex((const uint8_t*)serverPtr, (word32)img->size, hash,
NULL, server->devId);

/* Always release the DMA mapping to avoid leaking READ_PRE-allocated
* resources, even when the hash failed. Preserve the original error. */
{
int dmaRet = wh_Server_DmaProcessClientAddress(
server, img->addr, &serverPtr, img->size,
WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0});
if (ret == 0) {
ret = dmaRet;
}
}
#else
/* Hash the image data using one-shot API */
ret = wc_Sha256Hash_ex((const uint8_t*)img->addr, (word32)img->size, hash,
Expand All @@ -250,16 +261,6 @@ int wh_Server_ImgMgrVerifyMethodEccWithSha256(whServerImgMgrContext* context,
return ret;
}

#ifdef WOLFHSM_CFG_DMA
ret = wh_Server_DmaProcessClientAddress(
server, img->addr, &serverPtr, img->size, WH_DMA_OPER_CLIENT_READ_POST,
(whServerDmaFlags){0});
if (ret != WH_ERROR_OK) {
wc_ecc_free(&eccKey);
return ret;
}
#endif

/* Verify the signature */
ret = wc_ecc_verify_hash(sig, (word32)sigSz, hash, sizeof(hash),
&verifyResult, &eccKey);
Expand Down Expand Up @@ -320,6 +321,17 @@ int wh_Server_ImgMgrVerifyMethodAesCmac(whServerImgMgrContext* context,
ret = wc_AesCmacVerify_ex(&cmac, sig, (word32)sigSz, (const byte*)serverPtr,
(word32)img->size, key, (word32)keySz, NULL,
server->devId);

/* Always release the DMA mapping to avoid leaking READ_PRE-allocated
* resources, even when the verify failed. Preserve the original error. */
{
int dmaRet = wh_Server_DmaProcessClientAddress(
server, img->addr, &serverPtr, img->size,
WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0});
if (ret == 0) {
ret = dmaRet;
}
}
#else
ret = wc_AesCmacVerify_ex(&cmac, sig, (word32)sigSz, (const byte*)img->addr,
(word32)img->size, key, (word32)keySz, NULL,
Expand All @@ -329,15 +341,6 @@ int wh_Server_ImgMgrVerifyMethodAesCmac(whServerImgMgrContext* context,
return ret;
}

#ifdef WOLFHSM_CFG_DMA
ret = wh_Server_DmaProcessClientAddress(
server, img->addr, &serverPtr, img->size, WH_DMA_OPER_CLIENT_READ_POST,
(whServerDmaFlags){0});
if (ret != WH_ERROR_OK) {
return ret;
}
#endif

return WH_ERROR_OK; /* CMAC verification succeeded */
}
#endif /* WOLFSSL_CMAC */
Expand Down Expand Up @@ -390,6 +393,17 @@ int wh_Server_ImgMgrVerifyMethodRsaSslWithSha256(
/* Hash the image data from server pointer using one-shot API */
ret = wc_Sha256Hash_ex((const uint8_t*)serverPtr, (word32)img->size, hash,
NULL, server->devId);

/* Always release the DMA mapping to avoid leaking READ_PRE-allocated
* resources, even when the hash failed. Preserve the original error. */
{
int dmaRet = wh_Server_DmaProcessClientAddress(
server, img->addr, &serverPtr, img->size,
WH_DMA_OPER_CLIENT_READ_POST, (whServerDmaFlags){0});
if (ret == 0) {
ret = dmaRet;
}
}
#else
/* Hash the image data using one-shot API */
ret = wc_Sha256Hash_ex((const uint8_t*)img->addr, (word32)img->size, hash,
Expand All @@ -400,16 +414,6 @@ int wh_Server_ImgMgrVerifyMethodRsaSslWithSha256(
return ret;
}

#ifdef WOLFHSM_CFG_DMA
ret = wh_Server_DmaProcessClientAddress(
server, img->addr, &serverPtr, img->size, WH_DMA_OPER_CLIENT_READ_POST,
(whServerDmaFlags){0});
if (ret != WH_ERROR_OK) {
wc_FreeRsaKey(&rsaKey);
return ret;
}
#endif

/* Verify the signature using RSA SSL verify */
ret =
wc_RsaSSL_Verify(sig, (word32)sigSz, decrypted, decryptedLen, &rsaKey);
Expand Down
Loading