From e3b4344f8e93d018d303f47b4d2c48fd7bb0871d Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Wed, 15 Jul 2026 15:59:15 -0700 Subject: [PATCH] Add unified response size checking in wh_CommClient_RecvResponse --- src/wh_client.c | 68 ++++++++---- src/wh_client_auth.c | 14 +-- src/wh_client_cert.c | 29 +++--- src/wh_client_crypto.c | 168 +++++++++++++++++++++--------- src/wh_client_keywrap.c | 15 ++- src/wh_client_nvm.c | 22 ++-- src/wh_client_she.c | 49 ++++++--- src/wh_comm.c | 22 ++-- test-refactor/misc/wh_test_comm.c | 25 +++-- test/wh_test_clientserver.c | 20 ++++ test/wh_test_comm.c | 75 +++++++++++-- wolfhsm/wh_client.h | 5 +- wolfhsm/wh_comm.h | 7 +- 13 files changed, 369 insertions(+), 150 deletions(-) diff --git a/src/wh_client.c b/src/wh_client.c index 31112e130..3110b2667 100644 --- a/src/wh_client.c +++ b/src/wh_client.c @@ -255,7 +255,7 @@ int wh_Client_SendRequest(whClientContext* c, int wh_Client_RecvResponse(whClientContext *c, uint16_t *out_group, uint16_t *out_action, - uint16_t *out_size, void* data) + uint16_t *out_size, uint16_t data_size, void* data) { int rc = 0; uint16_t resp_kind = 0; @@ -268,7 +268,7 @@ int wh_Client_RecvResponse(whClientContext *c, /* Comm layer performs magic and sequence validation */ rc = wh_CommClient_RecvResponse(c->comm, NULL, &resp_kind, &resp_id, - &resp_size, data); + &resp_size, data_size, data); if (rc == 0) { if ((resp_kind != c->last_req_kind) || (resp_id != c->last_req_id)) { /* Response kind/id doesn't match outstanding request. */ @@ -286,6 +286,16 @@ int wh_Client_RecvResponse(whClientContext *c, } } } + else if (rc == WH_ERROR_BUFFER_SIZE) { + if ((resp_kind != c->last_req_kind) || (resp_id != c->last_req_id)) { + /* Response kind/id doesn't match outstanding request. */ + rc = WH_ERROR_ABORTED; + } + else if (out_size != NULL) { + /* Payload exceeded the caller's buffer; report the required size. */ + *out_size = resp_size; + } + } return rc; } @@ -329,7 +339,7 @@ int wh_Client_CommInitResponse(whClientContext* c, rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_COMM) || @@ -409,7 +419,7 @@ int wh_Client_CommInfoResponse(whClientContext* c, rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_COMM) || @@ -587,7 +597,7 @@ int wh_Client_CommCloseResponse(whClientContext* c) rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, NULL); + &resp_size, 0, NULL); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_COMM) || @@ -660,7 +670,7 @@ int wh_Client_EchoResponse(whClientContext* c, uint16_t *out_size, void* data) rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, msg); + &resp_size, WOLFHSM_CFG_COMM_DATA_LEN, msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_COMM) || @@ -722,7 +732,8 @@ int wh_Client_CustomCbResponse(whClientContext* c, } rc = - wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &resp); + wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, + sizeof(resp), &resp); if (rc != WH_ERROR_OK) { return rc; } @@ -872,7 +883,8 @@ int wh_Client_KeyCacheResponse(whClientContext* c, uint16_t* keyId) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -959,7 +971,8 @@ int wh_Client_KeyCacheRandomResponse(whClientContext* c, uint16_t* outKeyId) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -1027,7 +1040,8 @@ int wh_Client_KeyEvictResponse(whClientContext* c) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)&resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), + (uint8_t*)&resp); if (ret == 0) { if (resp.rc != 0) { @@ -1090,7 +1104,8 @@ int wh_Client_KeyExportResponse(whClientContext* c, uint8_t* label, } packOut = (uint8_t*)(resp + 1); - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -1175,7 +1190,8 @@ int wh_Client_KeyExportPublicResponse(whClientContext* c, uint8_t* label, } packOut = (uint8_t*)(resp + 1); - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -1252,7 +1268,8 @@ int wh_Client_KeyCommitResponse(whClientContext* c) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -1308,7 +1325,8 @@ int wh_Client_KeyEraseResponse(whClientContext* c) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != 0) { ret = resp->rc; @@ -1364,7 +1382,8 @@ int wh_Client_KeyRevokeResponse(whClientContext* c) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != 0) { ret = resp->rc; @@ -1422,7 +1441,8 @@ int wh_Client_CounterInitResponse(whClientContext* c, uint32_t* counter) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -1500,7 +1520,8 @@ int wh_Client_CounterIncrementResponse(whClientContext* c, uint32_t* counter) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -1560,7 +1581,8 @@ int wh_Client_CounterReadResponse(whClientContext* c, uint32_t* counter) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -1620,7 +1642,8 @@ int wh_Client_CounterDestroyResponse(whClientContext* c) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { if (resp->rc != 0) { ret = resp->rc; @@ -1715,7 +1738,8 @@ int wh_Client_KeyCacheDmaResponse(whClientContext* c, uint16_t* keyId) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(c, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); /* NOTREADY: response not in yet - return without POST so the pending * request keeps its mapping; POST runs once the response arrives. */ if (ret == WH_ERROR_NOTREADY) { @@ -1826,7 +1850,7 @@ int wh_Client_KeyExportDmaResponse(whClientContext* c, uint8_t* label, } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - (uint8_t*)resp); + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); /* NOTREADY: response not in yet - return without POST so the pending * request keeps its mapping; POST runs once the response arrives. */ if (rc == WH_ERROR_NOTREADY) { @@ -1951,7 +1975,7 @@ int wh_Client_KeyExportPublicDmaResponse(whClientContext* c, uint8_t* label, } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - (uint8_t*)resp); + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); /* NOTREADY: response not in yet - return without POST so the pending * request keeps its mapping; POST runs once the response arrives. */ if (rc == WH_ERROR_NOTREADY) { diff --git a/src/wh_client_auth.c b/src/wh_client_auth.c index 2e5f8012d..e8c2d94ff 100644 --- a/src/wh_client_auth.c +++ b/src/wh_client_auth.c @@ -134,7 +134,7 @@ int wh_Client_AuthLoginResponse(whClientContext* c, int32_t* out_rc, } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - buffer); + sizeof(buffer), buffer); if (rc == WH_ERROR_OK) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_AUTH) || @@ -219,7 +219,7 @@ int wh_Client_AuthLogoutResponse(whClientContext* c, int32_t* out_rc) } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - buffer); + sizeof(buffer), buffer); if (rc == WH_ERROR_OK) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_AUTH) || @@ -338,7 +338,7 @@ int wh_Client_AuthUserAddResponse(whClientContext* c, int32_t* out_rc, } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - buffer); + sizeof(buffer), buffer); if (rc == WH_ERROR_OK) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_AUTH) || @@ -413,7 +413,7 @@ int wh_Client_AuthUserDeleteResponse(whClientContext* c, int32_t* out_rc) } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - buffer); + sizeof(buffer), buffer); if (rc == WH_ERROR_OK) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_AUTH) || @@ -489,7 +489,7 @@ int wh_Client_AuthUserGetResponse(whClientContext* c, int32_t* out_rc, } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - buffer); + sizeof(buffer), buffer); if (rc == WH_ERROR_OK) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_AUTH) || @@ -576,7 +576,7 @@ int wh_Client_AuthUserSetPermissionsResponse(whClientContext* c, } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - buffer); + sizeof(buffer), buffer); if (rc == WH_ERROR_OK) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_AUTH) || @@ -702,7 +702,7 @@ int wh_Client_AuthUserSetCredentialsResponse(whClientContext* c, } rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, - buffer); + sizeof(buffer), buffer); if (rc == WH_ERROR_OK) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_AUTH) || diff --git a/src/wh_client_cert.c b/src/wh_client_cert.c index 41e7cd2bc..a74c07478 100644 --- a/src/wh_client_cert.c +++ b/src/wh_client_cert.c @@ -78,7 +78,7 @@ int wh_Client_CertInitResponse(whClientContext* c, int32_t* out_rc) } /* Receive and validate response */ - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); if (rc == 0) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_INIT) || (size != sizeof(resp))) { @@ -166,7 +166,7 @@ int wh_Client_CertAddTrustedResponse(whClientContext* c, int32_t* out_rc) } /* Receive and validate response */ - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); if (rc == 0) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_ADDTRUSTED) || @@ -237,7 +237,7 @@ int wh_Client_CertEraseTrustedResponse(whClientContext* c, int32_t* out_rc) } /* Receive and validate response */ - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); if (rc == 0) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_ERASETRUSTED) || @@ -310,7 +310,8 @@ int wh_Client_CertReadTrustedResponse(whClientContext* c, uint8_t* cert, } /* Receive and validate response */ - rc = wh_Client_RecvResponse(c, &group, &action, &size, buffer); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(buffer), + buffer); if (rc == 0) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_READTRUSTED) || @@ -419,7 +420,7 @@ static int _certVerifyResponse(whClientContext* c, whKeyId* out_keyId, } /* Receive and validate response */ - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); if (rc == 0) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_VERIFY) || @@ -573,7 +574,7 @@ static int _certVerifyMultiRootResponse(whClientContext* c, whKeyId* out_keyId, return WH_ERROR_BADARGS; } - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); if (rc == 0) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_VERIFY_MULTI_ROOT) || @@ -703,7 +704,7 @@ int wh_Client_CertVerifyCacheClearResponse(whClientContext* c, int32_t* out_rc) return WH_ERROR_BADARGS; } - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); if (rc == WH_ERROR_OK) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_VERIFY_CACHE_CLEAR) || @@ -768,7 +769,7 @@ int wh_Client_CertVerifyCacheSetEnabledResponse(whClientContext* c, return WH_ERROR_BADARGS; } - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); if (rc == WH_ERROR_OK) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_VERIFY_CACHE_SET_ENABLED) || @@ -863,7 +864,7 @@ int wh_Client_CertAddTrustedDmaResponse(whClientContext* c, int32_t* out_rc) } /* Receive and validate response */ - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); /* Not ready yet: keep the mapping; POST runs when the response arrives. */ if (rc == WH_ERROR_NOTREADY) { return rc; @@ -961,7 +962,7 @@ int wh_Client_CertReadTrustedDmaResponse(whClientContext* c, int32_t* out_rc) } /* Receive and validate response */ - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); /* Not ready yet: keep the mapping; POST runs when the response arrives. */ if (rc == WH_ERROR_NOTREADY) { return rc; @@ -1066,7 +1067,7 @@ static int _certVerifyDmaResponse(whClientContext* c, whKeyId* out_keyId, } /* Receive and validate response */ - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); /* Not ready yet: keep the mapping; POST runs when the response arrives. */ if (rc == WH_ERROR_NOTREADY) { return rc; @@ -1235,7 +1236,7 @@ static int _certVerifyMultiRootDmaResponse(whClientContext* c, return WH_ERROR_BADARGS; } - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); /* Not ready yet: keep the mapping; POST runs when the response arrives. */ if (rc == WH_ERROR_NOTREADY) { return rc; @@ -1392,7 +1393,7 @@ int wh_Client_CertVerifyAcertResponse(whClientContext* c, int32_t* out_rc) return WH_ERROR_BADARGS; } - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); if (rc == 0) { if ((group != WH_MESSAGE_GROUP_CERT) || (action != WH_MESSAGE_CERT_ACTION_VERIFY_ACERT) || @@ -1482,7 +1483,7 @@ int wh_Client_CertVerifyAcertDmaResponse(whClientContext* c, int32_t* out_rc) return WH_ERROR_BADARGS; } - rc = wh_Client_RecvResponse(c, &group, &action, &size, &resp); + rc = wh_Client_RecvResponse(c, &group, &action, &size, sizeof(resp), &resp); /* Not ready yet: keep the mapping; POST runs when the response arrives. */ if (rc == WH_ERROR_NOTREADY) { return rc; diff --git a/src/wh_client_crypto.c b/src/wh_client_crypto.c index d11e64609..af253c52f 100644 --- a/src/wh_client_crypto.c +++ b/src/wh_client_crypto.c @@ -249,7 +249,8 @@ int wh_Client_RngGenerateResponse(whClientContext* ctx, uint8_t* out, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -390,7 +391,8 @@ int wh_Client_RngGenerateDmaResponse(whClientContext* ctx) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -516,7 +518,8 @@ int wh_Client_AesCtrResponse(whClientContext* ctx, Aes* aes, uint8_t* out, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_OK) { ret = _getCryptoResponse(dataPtr, WC_CIPHER_AES_CTR, (uint8_t**)&res); if (ret == WH_ERROR_OK) { @@ -701,7 +704,8 @@ int wh_Client_AesCtrDmaResponse(whClientContext* ctx, Aes* aes) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -847,7 +851,8 @@ int wh_Client_AesEcbResponse(whClientContext* ctx, Aes* aes, uint8_t* out, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_OK) { ret = _getCryptoResponse(dataPtr, WC_CIPHER_AES_ECB, (uint8_t**)&res); if (ret == WH_ERROR_OK) { @@ -1018,7 +1023,8 @@ int wh_Client_AesEcbDmaResponse(whClientContext* ctx, Aes* aes) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -1171,7 +1177,8 @@ int wh_Client_AesCbcResponse(whClientContext* ctx, Aes* aes, uint8_t* out, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_OK) { ret = _getCryptoResponse(dataPtr, WC_CIPHER_AES_CBC, (uint8_t**)&res); if (ret == WH_ERROR_OK) { @@ -1354,7 +1361,8 @@ int wh_Client_AesCbcDmaResponse(whClientContext* ctx, Aes* aes) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -1527,7 +1535,8 @@ int wh_Client_AesGcmResponse(whClientContext* ctx, Aes* aes, uint8_t* out, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_OK) { ret = _getCryptoResponse(dataPtr, WC_CIPHER_AES_GCM, (uint8_t**)&res); if (ret == WH_ERROR_OK) { @@ -1751,7 +1760,8 @@ int wh_Client_AesGcmDmaResponse(whClientContext* ctx, Aes* aes, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -1997,7 +2007,8 @@ static int _EccMakeKeyResponse(whClientContext* ctx, whKeyId* out_key_id, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -2201,7 +2212,8 @@ static int _EccSharedSecretResponse(whClientContext* ctx, uint8_t* out, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -2436,7 +2448,8 @@ int wh_Client_EccSignResponse(whClientContext* ctx, uint8_t* sig, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -2638,7 +2651,8 @@ int wh_Client_EccVerifyResponse(whClientContext* ctx, ecc_key* opt_key, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -2832,7 +2846,7 @@ int wh_Client_EccCheckPubKey(whClientContext* ctx, ecc_key* key, if (ret == 0) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, - (uint8_t*)packet); + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)packet); } while (ret == WH_ERROR_NOTREADY); } if (ret == 0) { @@ -2997,6 +3011,7 @@ static int _Curve25519MakeKey(whClientContext* ctx, uint16_t size, if (ret == 0) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &data_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -3138,7 +3153,8 @@ static int _Curve25519SharedSecretResponse(whClientContext* ctx, uint8_t* out, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -3456,6 +3472,7 @@ static int _Ed25519MakeKey(whClientContext* ctx, whKeyId* inout_key_id, uint16_t res_len = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -3602,6 +3619,7 @@ int wh_Client_Ed25519Sign(whClientContext* ctx, ed25519_key* key, uint16_t res_len = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -3742,6 +3760,7 @@ int wh_Client_Ed25519Verify(whClientContext* ctx, ed25519_key* key, uint16_t res_len = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -3882,6 +3901,7 @@ int wh_Client_Ed25519SignDma(whClientContext* ctx, ed25519_key* key, uint16_t res_len = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -4029,6 +4049,7 @@ int wh_Client_Ed25519VerifyDma(whClientContext* ctx, ed25519_key* key, uint16_t res_len = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -4254,7 +4275,8 @@ static int _RsaMakeKeyResponse(whClientContext* ctx, whKeyId* out_key_id, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -4434,7 +4456,8 @@ int wh_Client_RsaFunctionResponse(whClientContext* ctx, uint8_t* out, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -4629,7 +4652,8 @@ int wh_Client_RsaGetSizeResponse(whClientContext* ctx, int* out_size) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -4826,7 +4850,8 @@ static int _HkdfMakeKey(whClientContext* ctx, int hashType, whKeyId keyIdIn, uint16_t res_len = 0; do { ret = - wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); } while (ret == WH_ERROR_NOTREADY); WH_DEBUG_CLIENT_VERBOSE("HKDF Res recv: ret:%d, res_len: %u\n", ret, @@ -4993,7 +5018,8 @@ static int _CmacKdfMakeKey(whClientContext* ctx, whKeyId saltKeyId, uint16_t res_len = 0; do { - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); } while (ret == WH_ERROR_NOTREADY); if (ret == WH_ERROR_OK) { @@ -5221,7 +5247,8 @@ int wh_Client_CmacGenerateResponse(whClientContext* ctx, Cmac* cmac, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -5342,7 +5369,8 @@ int wh_Client_CmacUpdateResponse(whClientContext* ctx, Cmac* cmac) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -5432,7 +5460,8 @@ int wh_Client_CmacFinalResponse(whClientContext* ctx, Cmac* cmac, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -5672,7 +5701,8 @@ int wh_Client_CmacGenerateDmaResponse(whClientContext* ctx, Cmac* cmac, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -5817,7 +5847,8 @@ int wh_Client_CmacDmaUpdateResponse(whClientContext* ctx, Cmac* cmac) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -5907,7 +5938,8 @@ int wh_Client_CmacDmaFinalResponse(whClientContext* ctx, Cmac* cmac, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -6150,7 +6182,8 @@ int wh_Client_Sha256UpdateResponse(whClientContext* ctx, wc_Sha256* sha) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -6230,7 +6263,8 @@ int wh_Client_Sha256FinalResponse(whClientContext* ctx, wc_Sha256* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != 0) { return ret; } @@ -6448,7 +6482,8 @@ int wh_Client_Sha256DmaUpdateResponse(whClientContext* ctx, wc_Sha256* sha) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -6552,7 +6587,8 @@ int wh_Client_Sha256DmaFinalResponse(whClientContext* ctx, wc_Sha256* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -6747,7 +6783,8 @@ int wh_Client_Sha224UpdateResponse(whClientContext* ctx, wc_Sha224* sha) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -6828,7 +6865,8 @@ int wh_Client_Sha224FinalResponse(whClientContext* ctx, wc_Sha224* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != 0) { return ret; } @@ -7034,7 +7072,8 @@ int wh_Client_Sha224DmaUpdateResponse(whClientContext* ctx, wc_Sha224* sha) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -7134,7 +7173,8 @@ int wh_Client_Sha224DmaFinalResponse(whClientContext* ctx, wc_Sha224* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -7329,7 +7369,8 @@ int wh_Client_Sha384UpdateResponse(whClientContext* ctx, wc_Sha384* sha) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -7411,7 +7452,8 @@ int wh_Client_Sha384FinalResponse(whClientContext* ctx, wc_Sha384* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != 0) { return ret; } @@ -7618,7 +7660,8 @@ int wh_Client_Sha384DmaUpdateResponse(whClientContext* ctx, wc_Sha384* sha) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -7719,7 +7762,8 @@ int wh_Client_Sha384DmaFinalResponse(whClientContext* ctx, wc_Sha384* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -7913,7 +7957,8 @@ int wh_Client_Sha512UpdateResponse(whClientContext* ctx, wc_Sha512* sha) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -8001,7 +8046,8 @@ int wh_Client_Sha512FinalResponse(whClientContext* ctx, wc_Sha512* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != 0) { return ret; } @@ -8231,7 +8277,8 @@ int wh_Client_Sha512DmaUpdateResponse(whClientContext* ctx, wc_Sha512* sha) return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -8338,7 +8385,8 @@ int wh_Client_Sha512DmaFinalResponse(whClientContext* ctx, wc_Sha512* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -8596,7 +8644,8 @@ static int _Sha3UpdateResponse(whClientContext* ctx, wc_Sha3* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != WH_ERROR_OK) { return ret; } @@ -8675,7 +8724,8 @@ static int _Sha3FinalResponse(whClientContext* ctx, wc_Sha3* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret != 0) { return ret; } @@ -9014,7 +9064,8 @@ static int _Sha3DmaUpdateResponse(whClientContext* ctx, wc_Sha3* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -9110,7 +9161,8 @@ static int _Sha3DmaFinalResponse(whClientContext* ctx, wc_Sha3* sha, return WH_ERROR_BADARGS; } - ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, dataPtr); + ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz, + WOLFHSM_CFG_COMM_DATA_LEN, dataPtr); if (ret == WH_ERROR_NOTREADY) { return ret; } @@ -9459,6 +9511,7 @@ static int _MlDsaMakeKey(whClientContext* ctx, int size, int level, uint16_t res_len; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -9629,6 +9682,7 @@ int wh_Client_MlDsaSign(whClientContext* ctx, const byte* in, word32 in_len, /* Recv Response */ do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -9776,6 +9830,7 @@ int wh_Client_MlDsaVerify(whClientContext* ctx, const byte* sig, word32 sig_len, /* Recv Response */ do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); if (ret == 0) { @@ -9976,6 +10031,7 @@ static int _MlDsaMakeKeyDma(whClientContext* ctx, int level, if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -10161,6 +10217,7 @@ int wh_Client_MlDsaSignDma(whClientContext* ctx, const byte* in, word32 in_len, /* Recv Response */ do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -10304,6 +10361,7 @@ int wh_Client_MlDsaVerifyDma(whClientContext* ctx, const byte* sig, /* Recv Response */ do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); @@ -10528,7 +10586,8 @@ static int _MlKemMakeKey(whClientContext* ctx, int level, do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, - (uint8_t*)dataPtr); + WOLFHSM_CFG_COMM_DATA_LEN, + (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); if (ret != WH_ERROR_OK) { return ret; @@ -10661,6 +10720,7 @@ int wh_Client_MlKemEncapsulate(whClientContext* ctx, MlKemKey* key, evict = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -10789,6 +10849,7 @@ int wh_Client_MlKemDecapsulate(whClientContext* ctx, MlKemKey* key, evict = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -10988,6 +11049,7 @@ static int _MlKemMakeKeyDma(whClientContext* ctx, int level, if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -11112,6 +11174,7 @@ int wh_Client_MlKemEncapsulateDma(whClientContext* ctx, MlKemKey* key, evict = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -11236,6 +11299,7 @@ int wh_Client_MlKemDecapsulateDma(whClientContext* ctx, MlKemKey* key, evict = 0; do { ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -11390,6 +11454,7 @@ int wh_Client_LmsMakeKeyDma(whClientContext* ctx, LmsKey* key, if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -11492,6 +11557,7 @@ int wh_Client_LmsSignDma(whClientContext* ctx, const byte* msg, word32 msgSz, if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -11591,6 +11657,7 @@ int wh_Client_LmsVerifyDma(whClientContext* ctx, const byte* sig, word32 sigSz, if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -11661,6 +11728,7 @@ int wh_Client_LmsSigsLeftDma(whClientContext* ctx, LmsKey* key) if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -11833,6 +11901,7 @@ int wh_Client_XmssMakeKeyDma(whClientContext* ctx, XmssKey* key, if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -11935,6 +12004,7 @@ int wh_Client_XmssSignDma(whClientContext* ctx, const byte* msg, word32 msgSz, if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -12035,6 +12105,7 @@ int wh_Client_XmssVerifyDma(whClientContext* ctx, const byte* sig, if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } @@ -12105,6 +12176,7 @@ int wh_Client_XmssSigsLeftDma(whClientContext* ctx, XmssKey* key) if (ret == WH_ERROR_OK) { do { ret = wh_Client_RecvResponse(ctx, &group, &action, &req_len, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)dataPtr); } while (ret == WH_ERROR_NOTREADY); } diff --git a/src/wh_client_keywrap.c b/src/wh_client_keywrap.c index 0bb1e15f0..cb8b6b655 100644 --- a/src/wh_client_keywrap.c +++ b/src/wh_client_keywrap.c @@ -75,7 +75,8 @@ int wh_Client_KeyWrapResponse(whClientContext* ctx, } /* Receive the response */ - ret = wh_Client_RecvResponse(ctx, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(ctx, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret != WH_ERROR_OK) { return ret; } @@ -194,7 +195,8 @@ int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx, } /* Receive the response */ - ret = wh_Client_RecvResponse(ctx, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(ctx, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret != WH_ERROR_OK) { return ret; } @@ -310,7 +312,8 @@ int wh_Client_KeyUnwrapAndCacheResponse(whClientContext* ctx, } /* Receive the response */ - ret = wh_Client_RecvResponse(ctx, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(ctx, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret != WH_ERROR_OK) { return ret; } @@ -415,7 +418,8 @@ int wh_Client_DataWrapResponse(whClientContext* ctx, } /* Receive the response */ - ret = wh_Client_RecvResponse(ctx, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(ctx, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret != WH_ERROR_OK) { return ret; } @@ -528,7 +532,8 @@ int wh_Client_DataUnwrapResponse(whClientContext* ctx, } /* Receive the response */ - ret = wh_Client_RecvResponse(ctx, &group, &action, &size, (uint8_t*)resp); + ret = wh_Client_RecvResponse(ctx, &group, &action, &size, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret != WH_ERROR_OK) { return ret; } diff --git a/src/wh_client_nvm.c b/src/wh_client_nvm.c index c1fc36d86..62247a9ca 100644 --- a/src/wh_client_nvm.c +++ b/src/wh_client_nvm.c @@ -71,7 +71,7 @@ int wh_Client_NvmInitResponse(whClientContext* c, int32_t *out_rc, rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_NVM) || @@ -143,7 +143,7 @@ int wh_Client_NvmCleanupResponse(whClientContext* c, int32_t *out_rc) rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_NVM) || @@ -209,7 +209,7 @@ int wh_Client_NvmGetAvailableResponse(whClientContext* c, int32_t *out_rc, rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_NVM) || @@ -313,7 +313,7 @@ int wh_Client_NvmAddObjectResponse(whClientContext* c, int32_t *out_rc) rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_NVM) || @@ -390,7 +390,7 @@ int wh_Client_NvmListResponse(whClientContext* c, int32_t *out_rc, rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_NVM) || @@ -469,7 +469,7 @@ int wh_Client_NvmGetMetadataResponse(whClientContext* c, int32_t *out_rc, rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_NVM) || @@ -565,7 +565,7 @@ int wh_Client_NvmDestroyObjectsResponse(whClientContext* c, int32_t *out_rc) rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, &msg); + &resp_size, sizeof(msg), &msg); if (rc == 0) { /* Validate response */ if ( (resp_group != WH_MESSAGE_GROUP_NVM) || @@ -642,7 +642,7 @@ int wh_Client_NvmReadResponse(whClientContext* c, int32_t *out_rc, rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, - &resp_size, buffer); + &resp_size, sizeof(buffer), buffer); if (rc == 0) { /* Validate response */ if ((resp_group != WH_MESSAGE_GROUP_NVM) || @@ -760,7 +760,8 @@ int wh_Client_NvmAddObjectDmaResponse(whClientContext* c, int32_t* out_rc) return WH_ERROR_BADARGS; } - rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &msg); + rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, + sizeof(msg), &msg); /* NOTREADY: response not in yet - return without POST so the pending * request keeps its mappings; POST runs once the response arrives. */ if (rc == WH_ERROR_NOTREADY) { @@ -866,7 +867,8 @@ int wh_Client_NvmReadDmaResponse(whClientContext* c, int32_t* out_rc) return WH_ERROR_BADARGS; } - rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, &msg); + rc = wh_Client_RecvResponse(c, &resp_group, &resp_action, &resp_size, + sizeof(msg), &msg); /* NOTREADY: response not in yet - return without POST so the pending * request keeps its mapping; POST runs once the response arrives. */ if (rc == WH_ERROR_NOTREADY) { diff --git a/src/wh_client_she.c b/src/wh_client_she.c index 716934f83..f25b387d8 100644 --- a/src/wh_client_she.c +++ b/src/wh_client_she.c @@ -95,7 +95,8 @@ int wh_Client_SheSetUidResponse(whClientContext* c) } resp = (whMessageShe_SetUidResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == WH_ERROR_OK) { ret = resp->rc; } @@ -146,7 +147,8 @@ int wh_Client_SheSecureBoot(whClientContext* c, uint8_t* bootloader, if (ret == 0) { do { - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, respBuf); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, respBuf); initResp = (whMessageShe_SecureBootInitResponse*)respBuf; } while (ret == WH_ERROR_NOTREADY); } @@ -183,6 +185,7 @@ int wh_Client_SheSecureBoot(whClientContext* c, uint8_t* bootloader, if (ret == 0) { do { ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, respBuf); } while (ret == WH_ERROR_NOTREADY); } @@ -201,7 +204,8 @@ int wh_Client_SheSecureBoot(whClientContext* c, uint8_t* bootloader, if (ret == 0) { do { - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, respBuf); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, respBuf); finishResp = (whMessageShe_SecureBootFinishResponse*)respBuf; } while (ret == WH_ERROR_NOTREADY); } @@ -241,7 +245,8 @@ int wh_Client_SheGetStatusResponse(whClientContext* c, uint8_t* sreg) resp = (whMessageShe_GetStatusResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); /* return error or set sreg */ if (ret == 0) { @@ -305,7 +310,8 @@ int wh_Client_SheLoadKeyResponse(whClientContext* c, uint8_t* messageFour, resp = (whMessageShe_LoadKeyResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) { ret = resp->rc; @@ -365,7 +371,8 @@ int wh_Client_SheLoadPlainKeyResponse(whClientContext* c) resp = (whMessageShe_LoadPlainKeyResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { ret = resp->rc; } @@ -415,7 +422,8 @@ int wh_Client_SheExportRamKeyResponse(whClientContext* c, uint8_t* messageOne, resp = (whMessageShe_ExportRamKeyResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) { ret = resp->rc; @@ -472,7 +480,8 @@ int wh_Client_SheInitRndResponse(whClientContext* c) } resp = (whMessageShe_InitRngResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { ret = resp->rc; } @@ -515,7 +524,8 @@ int wh_Client_SheRndResponse(whClientContext* c, uint8_t* out, uint32_t* outSz) resp = (whMessageShe_RndResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) @@ -575,7 +585,8 @@ int wh_Client_SheExtendSeedResponse(whClientContext* c) } resp = (whMessageShe_ExtendSeedResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { ret = resp->rc; @@ -638,7 +649,8 @@ int wh_Client_SheEncEcbResponse(whClientContext* c, uint8_t* out, uint32_t sz) /* out is after fixed sized fields */ packOut = (uint8_t*)(resp + 1); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) { ret = resp->rc; @@ -710,7 +722,8 @@ int wh_Client_SheEncCbcResponse(whClientContext* c, uint8_t* out, uint32_t sz) /* out is after fixed sized fields */ packOut = (uint8_t*)(resp + 1); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) { ret = resp->rc; @@ -778,7 +791,8 @@ int wh_Client_SheDecEcbResponse(whClientContext* c, uint8_t* out, uint32_t sz) /* out is after fixed sized fields */ packOut = (uint8_t*)(resp + 1); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) { ret = resp->rc; @@ -850,7 +864,8 @@ int wh_Client_SheDecCbcResponse(whClientContext* c, uint8_t* out, uint32_t sz) /* out is after fixed sized fields */ packOut = (uint8_t*)(resp + 1); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) { ret = resp->rc; @@ -917,7 +932,8 @@ int wh_Client_SheGenerateMacResponse(whClientContext* c, uint8_t* out, resp = (whMessageShe_GenMacResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) { ret = resp->rc; @@ -989,7 +1005,8 @@ int wh_Client_SheVerifyMacResponse(whClientContext* c, uint8_t* outStatus) } resp = (whMessageShe_VerifyMacResponse*)wh_CommClient_GetDataPtr(c->comm); - ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, (uint8_t*)resp); + ret = wh_Client_RecvResponse(c, &group, &action, &dataSz, + WOLFHSM_CFG_COMM_DATA_LEN, (uint8_t*)resp); if (ret == 0) { if (resp->rc != WH_SHE_ERC_NO_ERROR) { ret = resp->rc; diff --git a/src/wh_comm.c b/src/wh_comm.c index af189dcc8..37d8182eb 100644 --- a/src/wh_comm.c +++ b/src/wh_comm.c @@ -155,14 +155,14 @@ int wh_CommClient_SendRequest(whCommClient* context, uint16_t magic, */ int wh_CommClient_RecvResponse(whCommClient* context, uint16_t* out_magic, uint16_t* out_kind, uint16_t* out_seq, - uint16_t* out_size, void* data) + uint16_t* out_size, uint16_t data_size, void* data) { int rc = 0; uint16_t magic = 0; uint16_t kind = 0; uint16_t seq = 0; uint16_t size = sizeof(context->packet); - uint16_t data_size = 0; + uint16_t resp_size = 0; if ((context == NULL) || (context->hdr == NULL) || (context->initialized == 0) || (context->transport_cb == NULL) || @@ -193,7 +193,7 @@ int wh_CommClient_RecvResponse(whCommClient* context, rc = WH_ERROR_ABORTED; } if (rc == 0) { - data_size = size - sizeof(*context->hdr); + resp_size = size - sizeof(*context->hdr); magic = context->hdr->magic; kind = wh_Translate16(magic, context->hdr->kind); seq = wh_Translate16(magic, context->hdr->seq); @@ -224,15 +224,21 @@ int wh_CommClient_RecvResponse(whCommClient* context, return WH_ERROR_NOTREADY; } - if ( (data != NULL) && - (data_size != 0) && - (data != context->data)) { - memcpy(data, context->data, data_size); + if ((data != NULL) && (resp_size > data_size)) { + rc = WH_ERROR_BUFFER_SIZE; + } + else { + /* Copy the data from the internal buffer if necessary */ + if ( (data != NULL) && + (resp_size != 0) && + (data != context->data)) { + memcpy(data, context->data, resp_size); + } } if (out_magic != NULL) *out_magic = magic; if (out_kind != NULL) *out_kind = kind; if (out_seq != NULL) *out_seq = seq; - if (out_size != NULL) *out_size = data_size; + if (out_size != NULL) *out_size = resp_size; context->pending = 0; } } diff --git a/test-refactor/misc/wh_test_comm.c b/test-refactor/misc/wh_test_comm.c index 2655f7cd4..6e46b7992 100644 --- a/test-refactor/misc/wh_test_comm.c +++ b/test-refactor/misc/wh_test_comm.c @@ -136,7 +136,8 @@ static int _whTest_CommMem(void) WH_TEST_ASSERT_RETURN( WH_ERROR_NOTREADY == wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, sizeof(rx_resp), + rx_resp)); for (counter = 0; counter < REPEAT_COUNT; counter++) { (void)snprintf((char*)tx_req, sizeof(tx_req), "Request:%u", counter); @@ -150,9 +151,13 @@ static int _whTest_CommMem(void) if (counter == 0) { WH_TEST_ASSERT_RETURN(WH_ERROR_NOTREADY == - wh_CommClient_RecvResponse( - client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + wh_CommClient_RecvResponse(client, + &rx_resp_flags, + &rx_resp_type, + &rx_resp_seq, + &rx_resp_len, + sizeof(rx_resp), + rx_resp)); WH_TEST_ASSERT_RETURN( WH_ERROR_REQUEST_PENDING == @@ -183,7 +188,8 @@ static int _whTest_CommMem(void) WH_TEST_RETURN_ON_FAIL( wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, + sizeof(rx_resp), rx_resp)); WH_TEST_DEBUG_PRINT( "Client RecvResponse:%d, flags %x, type:%x, seq:%d, len:%d, %s\n", @@ -222,14 +228,16 @@ static int _whTest_CommMem(void) /* Successful recv clears pending */ WH_TEST_RETURN_ON_FAIL( wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, sizeof(rx_resp), + rx_resp)); WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); /* Second Recv with no outstanding request again yields NOTREADY */ WH_TEST_ASSERT_RETURN( WH_ERROR_NOTREADY == wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, sizeof(rx_resp), + rx_resp)); /* Send, then manually abort. Seq must not advance. */ WH_TEST_RETURN_ON_FAIL(wh_CommClient_SendRequest( @@ -253,7 +261,8 @@ static int _whTest_CommMem(void) WH_TEST_ASSERT_RETURN( WH_ERROR_NOTREADY == wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, sizeof(rx_resp), + rx_resp)); WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); WH_TEST_RETURN_ON_FAIL(wh_CommServer_Cleanup(server)); diff --git a/test/wh_test_clientserver.c b/test/wh_test_clientserver.c index 56caebc70..a90d8c2b9 100644 --- a/test/wh_test_clientserver.c +++ b/test/wh_test_clientserver.c @@ -1068,6 +1068,26 @@ int whTest_ClientServerSequential(whTestNvmBackendType nvmType) WH_TEST_ASSERT_RETURN(strncmp(recv_buffer, send_buffer, recv_len) == 0); } + /* wh_Client_RecvResponse must return WH_ERROR_BUFFER_SIZE and report the + * required size. */ + { + uint8_t tiny_buf[8] = {0}; + uint16_t r_group = 0; + uint16_t r_action = 0; + uint16_t r_size = 0; + + send_len = snprintf(send_buffer, sizeof(send_buffer), + "PayloadLargerThanTinyBuffer"); + WH_TEST_RETURN_ON_FAIL( + wh_Client_EchoRequest(client, send_len, send_buffer)); + WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); + WH_TEST_ASSERT_RETURN( + WH_ERROR_BUFFER_SIZE == + wh_Client_RecvResponse(client, &r_group, &r_action, &r_size, + sizeof(tiny_buf), tiny_buf)); + WH_TEST_ASSERT_RETURN(r_size == send_len); + } + /* Perform NVM tests */ WH_TEST_RETURN_ON_FAIL(wh_Client_NvmInitRequest(client)); WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); diff --git a/test/wh_test_comm.c b/test/wh_test_comm.c index 42503a3b1..a29874a29 100644 --- a/test/wh_test_comm.c +++ b/test/wh_test_comm.c @@ -151,7 +151,8 @@ int whTest_CommMem(void) WH_TEST_ASSERT_RETURN( WH_ERROR_NOTREADY == wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, sizeof(rx_resp), + rx_resp)); for (counter = 0; counter < REPEAT_COUNT; counter++) { (void)snprintf((char*)tx_req, sizeof(tx_req), "Request:%u", counter); @@ -165,9 +166,13 @@ int whTest_CommMem(void) if (counter == 0) { WH_TEST_ASSERT_RETURN(WH_ERROR_NOTREADY == - wh_CommClient_RecvResponse( - client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + wh_CommClient_RecvResponse(client, + &rx_resp_flags, + &rx_resp_type, + &rx_resp_seq, + &rx_resp_len, + sizeof(rx_resp), + rx_resp)); WH_TEST_ASSERT_RETURN( WH_ERROR_REQUEST_PENDING == @@ -198,7 +203,8 @@ int whTest_CommMem(void) WH_TEST_RETURN_ON_FAIL( wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, + sizeof(rx_resp), rx_resp)); WH_TEST_DEBUG_PRINT( "Client RecvResponse:%d, flags %x, type:%x, seq:%d, len:%d, %s\n", @@ -209,6 +215,53 @@ int whTest_CommMem(void) /* Pending tracking: context is idle after the loop */ WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); + /* Buffer-size handling: a response larger than the caller's buffer must + * return WH_ERROR_BUFFER_SIZE, report the required size, leave the buffer + * untouched, and still consume the packet. An exact-fit buffer succeeds. */ + (void)snprintf((char*)tx_req, sizeof(tx_req), "BufSizeTest"); + tx_req_len = (uint16_t)strlen((char*)tx_req); + tx_req_type = 0x1234; + memset(tx_resp, 'Z', sizeof(tx_resp)); + tx_resp_len = REQ_SIZE; + + /* Exact fit: capacity == payload succeeds and copies the data. */ + WH_TEST_RETURN_ON_FAIL(wh_CommClient_SendRequest( + client, tx_req_flags, tx_req_type, &tx_req_seq, tx_req_len, tx_req)); + WH_TEST_RETURN_ON_FAIL(wh_CommServer_RecvRequest( + server, &rx_req_flags, &rx_req_type, &rx_req_seq, &rx_req_len, + sizeof(rx_req), rx_req)); + WH_TEST_RETURN_ON_FAIL(wh_CommServer_SendResponse( + server, rx_req_flags, rx_req_type, rx_req_seq, tx_resp_len, tx_resp)); + memset(rx_resp, 0, sizeof(rx_resp)); + rx_resp_len = 0; + WH_TEST_RETURN_ON_FAIL( + wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, + &rx_resp_seq, &rx_resp_len, tx_resp_len, + rx_resp)); + WH_TEST_ASSERT_RETURN(rx_resp_len == tx_resp_len); + WH_TEST_ASSERT_RETURN(0 == memcmp(rx_resp, tx_resp, tx_resp_len)); + WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); + + /* One byte short: capacity < payload returns BUFFER_SIZE, reports the + * required size, leaves the buffer untouched, and clears pending. */ + WH_TEST_RETURN_ON_FAIL(wh_CommClient_SendRequest( + client, tx_req_flags, tx_req_type, &tx_req_seq, tx_req_len, tx_req)); + WH_TEST_RETURN_ON_FAIL(wh_CommServer_RecvRequest( + server, &rx_req_flags, &rx_req_type, &rx_req_seq, &rx_req_len, + sizeof(rx_req), rx_req)); + WH_TEST_RETURN_ON_FAIL(wh_CommServer_SendResponse( + server, rx_req_flags, rx_req_type, rx_req_seq, tx_resp_len, tx_resp)); + memset(rx_resp, 0, sizeof(rx_resp)); + rx_resp_len = 0; + WH_TEST_ASSERT_RETURN( + WH_ERROR_BUFFER_SIZE == + wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, + &rx_resp_seq, &rx_resp_len, tx_resp_len - 1, + rx_resp)); + WH_TEST_ASSERT_RETURN(rx_resp_len == tx_resp_len); + WH_TEST_ASSERT_RETURN(rx_resp[0] == 0); + WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); + /* Send a request: transitions to pending */ (void)snprintf((char*)tx_req, sizeof(tx_req), "PendingTest"); tx_req_len = (uint16_t)strlen((char*)tx_req); @@ -237,14 +290,16 @@ int whTest_CommMem(void) /* Successful recv clears pending */ WH_TEST_RETURN_ON_FAIL( wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, sizeof(rx_resp), + rx_resp)); WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); /* Second Recv with no outstanding request again yields NOTREADY */ WH_TEST_ASSERT_RETURN( WH_ERROR_NOTREADY == wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, sizeof(rx_resp), + rx_resp)); /* Send, then manually abort. Seq must not advance. */ WH_TEST_RETURN_ON_FAIL(wh_CommClient_SendRequest( @@ -268,7 +323,8 @@ int whTest_CommMem(void) WH_TEST_ASSERT_RETURN( WH_ERROR_NOTREADY == wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, - &rx_resp_seq, &rx_resp_len, rx_resp)); + &rx_resp_seq, &rx_resp_len, sizeof(rx_resp), + rx_resp)); WH_TEST_ASSERT_RETURN(0 == wh_CommClient_IsRequestPending(client)); WH_TEST_RETURN_ON_FAIL(wh_CommServer_Cleanup(server)); @@ -338,7 +394,8 @@ static void* _whCommClientTask(void* cf) do { ret = wh_CommClient_RecvResponse(client, &rx_resp_flags, &rx_resp_type, &rx_resp_seq, - &rx_resp_len, rx_resp); + &rx_resp_len, sizeof(rx_resp), + rx_resp); WH_TEST_ASSERT_MSG((ret == WH_ERROR_NOTREADY) || (0 == ret), "Client RecvResponse: ret=%d", ret); if(ret != WH_ERROR_NOTREADY) { diff --git a/wolfhsm/wh_client.h b/wolfhsm/wh_client.h index d99d4264c..07d1b025f 100644 --- a/wolfhsm/wh_client.h +++ b/wolfhsm/wh_client.h @@ -305,12 +305,15 @@ int wh_Client_SendRequest(whClientContext* c, uint16_t group, uint16_t action, * @param out_group Pointer to store the received group value. * @param out_action Pointer to store the received action value. * @param out_size Pointer to store the received size value. + * @param data_size The capacity of the caller-supplied data buffer. If the + * received payload exceeds it, WH_ERROR_BUFFER_SIZE is returned with *out_size + * set to the required size. * @param data Pointer to store the received data. * @return 0 if successful, a negative value if an error occurred. */ int wh_Client_RecvResponse(whClientContext* c, uint16_t* out_group, uint16_t* out_action, uint16_t* out_size, - void* data); + uint16_t data_size, void* data); /** * @brief Reports whether a request has been sent whose matching response has diff --git a/wolfhsm/wh_comm.h b/wolfhsm/wh_comm.h index a36392d4e..0b1ba74a3 100644 --- a/wolfhsm/wh_comm.h +++ b/wolfhsm/wh_comm.h @@ -210,11 +210,14 @@ int wh_CommClient_SendRequest(whCommClient* context, uint16_t magic, uint16_t kind, uint16_t *out_seq, uint16_t data_size, const void* data); /* If a response packet has been buffered, get the header and copy the data out - * of the buffer. + * of the buffer. data_size is the capacity of the caller-supplied data buffer; + * if the received payload exceeds it, returns WH_ERROR_BUFFER_SIZE with + * *out_size set to the required size. On success *out_size holds the actual + * payload size. */ int wh_CommClient_RecvResponse(whCommClient* context, uint16_t* out_magic, uint16_t* out_kind, uint16_t* out_seq, - uint16_t* out_size, void* data); + uint16_t* out_size, uint16_t data_size, void* data); /* Get a pointer to the data portion of the internal buffer that is * HW_COMM_DATA_LEN bytes.