Skip to content

Commit

Permalink
fix erroneous macro guard of MLDSA inside !NO_SHA256
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbrett committed Nov 14, 2024
1 parent caf937d commit ad1ac54
Showing 1 changed file with 92 additions and 93 deletions.
185 changes: 92 additions & 93 deletions src/wh_client_cryptocb.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,98 @@ static int _xferSha256BlockAndUpdateDigest(whClientContext* ctx,
return ret;
}

#ifdef WOLFHSM_CFG_DMA

static int _handleSha256Dma(wc_CryptoInfo* info, void* inCtx, whPacket* packet)
{
int ret = WH_ERROR_OK;
whClientContext* ctx = inCtx;
wc_Sha256* sha256 = info->hash.sha256;
uint16_t respSz = 0;
uint16_t group = WH_MESSAGE_GROUP_CRYPTO_DMA;

#if WH_DMA_IS_32BIT
wh_Packet_hash_sha256_Dma32_req* req = &packet->hashSha256Dma32Req;
wh_Packet_hash_sha256_Dma32_res* resp = &packet->hashSha256Dma32Res;
#else
wh_Packet_hash_sha256_Dma64_req* req = &packet->hashSha256Dma64Req;
wh_Packet_hash_sha256_Dma64_res* resp = &packet->hashSha256Dma64Res;
#endif

/* Caller invoked SHA Update:
* wc_CryptoCb_Sha256Hash(sha256, data, len, NULL) */
if (info->hash.in != NULL) {
req->type = WC_HASH_TYPE_SHA256;
req->finalize = 0;
req->state.addr = (uintptr_t)sha256;
req->state.sz = sizeof(*sha256);
req->input.addr = (uintptr_t)info->hash.in;
req->input.sz = info->hash.inSz;

#ifdef DEBUG_CRYPTOCB_VERBOSE
printf("[client] SHA256 DMA UPDATE: inAddr=%p, inSz=%u\n",
info->hash.in, info->hash.inSz);
#endif
ret = wh_Client_SendRequest(ctx, group, WC_ALGO_TYPE_HASH,
WH_PACKET_STUB_SIZE + sizeof(*req),
(uint8_t*)packet);
if (ret == WH_ERROR_OK) {
do {
ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz,
(uint8_t*)packet);
} while (ret == WH_ERROR_NOTREADY);
}

if (ret == WH_ERROR_OK) {
if (packet->rc != WH_ERROR_OK) {
ret = packet->rc;
}
/* Nothing to do on success, as server will have updated the context
* in client memory */
}
}

/* Caller invoked SHA finalize:
* wc_CryptoCb_Sha256Hash(sha256, NULL, 0, * hash) */
if ((ret == WH_ERROR_OK) && (info->hash.digest != NULL)) {
/* Packet will have been trashed, so re-populate all fields */
req->type = WC_HASH_TYPE_SHA256;
req->finalize = 1;
req->state.addr = (uintptr_t)sha256;
req->state.sz = sizeof(*sha256);
req->output.addr = (uintptr_t)info->hash.digest;
req->output.sz = WC_SHA256_DIGEST_SIZE; /* not needed, but YOLO */

#ifdef DEBUG_CRYPTOCB_VERBOSE
printf("[client] SHA256 DMA FINAL: outAddr=%p\n", info->hash.digest);
#endif
/* send the request to the server */
ret = wh_Client_SendRequest(ctx, group, WC_ALGO_TYPE_HASH,
WH_PACKET_STUB_SIZE + sizeof(*req),
(uint8_t*)packet);
if (ret == WH_ERROR_OK) {
do {
ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz,
(uint8_t*)packet);
} while (ret == WH_ERROR_NOTREADY);
}

/* Copy out the final hash value */
if (ret == WH_ERROR_OK) {
if (packet->rc != WH_ERROR_OK) {
ret = packet->rc;
(void)resp;
}
/* Nothing to do on success, as server will have updated the output
* hash in client memory */
}
}

return ret;
}
#endif /* WOLFHSM_CFG_DMA */
#endif /* ! NO_SHA256 */


#if defined(HAVE_FALCON) || defined(HAVE_DILITHIUM)
static int _handlePqcSigKeyGen(whClientContext* ctx, wc_CryptoInfo* info)
Expand Down Expand Up @@ -778,99 +870,6 @@ static int _handlePqcSigCheckPrivKey(whClientContext* ctx, wc_CryptoInfo* info)
#endif /* HAVE_FALCON || HAVE_DILITHIUM */


#ifdef WOLFHSM_CFG_DMA

static int _handleSha256Dma(wc_CryptoInfo* info, void* inCtx, whPacket* packet)
{
int ret = WH_ERROR_OK;
whClientContext* ctx = inCtx;
wc_Sha256* sha256 = info->hash.sha256;
uint16_t respSz = 0;
uint16_t group = WH_MESSAGE_GROUP_CRYPTO_DMA;

#if WH_DMA_IS_32BIT
wh_Packet_hash_sha256_Dma32_req* req = &packet->hashSha256Dma32Req;
wh_Packet_hash_sha256_Dma32_res* resp = &packet->hashSha256Dma32Res;
#else
wh_Packet_hash_sha256_Dma64_req* req = &packet->hashSha256Dma64Req;
wh_Packet_hash_sha256_Dma64_res* resp = &packet->hashSha256Dma64Res;
#endif

/* Caller invoked SHA Update:
* wc_CryptoCb_Sha256Hash(sha256, data, len, NULL) */
if (info->hash.in != NULL) {
req->type = WC_HASH_TYPE_SHA256;
req->finalize = 0;
req->state.addr = (uintptr_t)sha256;
req->state.sz = sizeof(*sha256);
req->input.addr = (uintptr_t)info->hash.in;
req->input.sz = info->hash.inSz;

#ifdef DEBUG_CRYPTOCB_VERBOSE
printf("[client] SHA256 DMA UPDATE: inAddr=%p, inSz=%u\n",
info->hash.in, info->hash.inSz);
#endif
ret = wh_Client_SendRequest(ctx, group, WC_ALGO_TYPE_HASH,
WH_PACKET_STUB_SIZE + sizeof(*req),
(uint8_t*)packet);
if (ret == WH_ERROR_OK) {
do {
ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz,
(uint8_t*)packet);
} while (ret == WH_ERROR_NOTREADY);
}

if (ret == WH_ERROR_OK) {
if (packet->rc != WH_ERROR_OK) {
ret = packet->rc;
}
/* Nothing to do on success, as server will have updated the context
* in client memory */
}
}

/* Caller invoked SHA finalize:
* wc_CryptoCb_Sha256Hash(sha256, NULL, 0, * hash) */
if ((ret == WH_ERROR_OK) && (info->hash.digest != NULL)) {
/* Packet will have been trashed, so re-populate all fields */
req->type = WC_HASH_TYPE_SHA256;
req->finalize = 1;
req->state.addr = (uintptr_t)sha256;
req->state.sz = sizeof(*sha256);
req->output.addr = (uintptr_t)info->hash.digest;
req->output.sz = WC_SHA256_DIGEST_SIZE; /* not needed, but YOLO */

#ifdef DEBUG_CRYPTOCB_VERBOSE
printf("[client] SHA256 DMA FINAL: outAddr=%p\n", info->hash.digest);
#endif
/* send the request to the server */
ret = wh_Client_SendRequest(ctx, group, WC_ALGO_TYPE_HASH,
WH_PACKET_STUB_SIZE + sizeof(*req),
(uint8_t*)packet);
if (ret == WH_ERROR_OK) {
do {
ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz,
(uint8_t*)packet);
} while (ret == WH_ERROR_NOTREADY);
}

/* Copy out the final hash value */
if (ret == WH_ERROR_OK) {
if (packet->rc != WH_ERROR_OK) {
ret = packet->rc;
(void)resp;
}
/* Nothing to do on success, as server will have updated the output
* hash in client memory */
}
}

return ret;
}
#endif /* WOLFHSM_CFG_DMA */
#endif /* ! NO_SHA256 */


#ifdef WOLFHSM_CFG_DMA
int wh_Client_CryptoCbDma(int devId, wc_CryptoInfo* info, void* inCtx)
{
Expand Down

0 comments on commit ad1ac54

Please sign in to comment.