diff --git a/src/wh_client.c b/src/wh_client.c index 1ba38a36..9a87124d 100644 --- a/src/wh_client.c +++ b/src/wh_client.c @@ -23,7 +23,7 @@ /* Message definitions */ #include "wolfhsm/wh_message.h" #include "wolfhsm/wh_message_comm.h" -#include "wolfhsm/wh_message_custom.h" +#include "wolfhsm/wh_message_customcb.h" #include "wolfhsm/wh_client.h" @@ -202,9 +202,9 @@ int wh_Client_Echo(whClientContext* c, uint16_t snd_len, const void* snd_data, return rc; } -int wh_Client_CustomRequest(whClientContext* c, const whMessageCustom_Request* req) +int wh_Client_CustomCbRequest(whClientContext* c, const whMessageCustomCb_Request* req) { - if (NULL == c || req == NULL || req->id >= WH_CUSTOM_RQST_NUM_CALLBACKS) { + if (NULL == c || req == NULL || req->id >= WH_CUSTOM_CB_NUM_CALLBACKS) { return WH_ERROR_BADARGS; } @@ -212,10 +212,10 @@ int wh_Client_CustomRequest(whClientContext* c, const whMessageCustom_Request* r sizeof(*req), req); } -int wh_Client_CustomResponse(whClientContext* c, - whMessageCustom_Response* outResp) +int wh_Client_CustomCbResponse(whClientContext* c, + whMessageCustomCb_Response* outResp) { - whMessageCustom_Response resp; + whMessageCustomCb_Response resp; uint16_t resp_group = 0; uint16_t resp_action = 0; uint16_t resp_size = 0; @@ -232,7 +232,7 @@ int wh_Client_CustomResponse(whClientContext* c, } if (resp_size != sizeof(resp) || resp_group != WH_MESSAGE_GROUP_CUSTOM || - resp_action >= WH_CUSTOM_RQST_NUM_CALLBACKS) { + resp_action >= WH_CUSTOM_CB_NUM_CALLBACKS) { /* message invalid */ return WH_ERROR_ABORTED; } @@ -242,37 +242,37 @@ int wh_Client_CustomResponse(whClientContext* c, return WH_ERROR_OK; } -int wh_Client_CustomRequestCheckRegistered(whClientContext* c, uint32_t id) +int wh_Client_CustomCheckRegisteredRequest(whClientContext* c, uint32_t id) { - whMessageCustom_Request req = {0}; + whMessageCustomCb_Request req = {0}; - if (c == NULL || id >= WH_CUSTOM_RQST_NUM_CALLBACKS) { + if (c == NULL || id >= WH_CUSTOM_CB_NUM_CALLBACKS) { return WH_ERROR_BADARGS; } req.id = id; - req.type = WH_MESSAGE_CUSTOM_TYPE_QUERY; + req.type = WH_MESSAGE_CUSTOM_CB_TYPE_QUERY; return wh_Client_SendRequest(c, WH_MESSAGE_GROUP_CUSTOM, req.id, sizeof(req), &req); } -int wh_Client_CustomResponseCheckRegistered(whClientContext* c, uint16_t* outId, int* responseError) +int wh_Client_CustomCbCheckRegisteredResponse(whClientContext* c, uint16_t* outId, int* responseError) { int rc = 0; - whMessageCustom_Response resp = {0}; + whMessageCustomCb_Response resp = {0}; if (c == NULL || outId == NULL || responseError == NULL) { return WH_ERROR_BADARGS; } - rc = wh_Client_CustomResponse(c, &resp); + rc = wh_Client_CustomCbResponse(c, &resp); if (rc != WH_ERROR_OK) { return rc; } - if (resp.type != WH_MESSAGE_CUSTOM_TYPE_QUERY) { + if (resp.type != WH_MESSAGE_CUSTOM_CB_TYPE_QUERY) { /* message invalid */ return WH_ERROR_ABORTED; } @@ -286,4 +286,26 @@ int wh_Client_CustomResponseCheckRegistered(whClientContext* c, uint16_t* outId, *responseError = resp.err; return WH_ERROR_OK; +} + + +int wh_Client_CustomCbCheckRegistered(whClientContext* c, uint16_t id, int* responseError) +{ + int rc = 0; + + if (NULL == c || NULL == responseError || id >= WH_CUSTOM_CB_NUM_CALLBACKS) { + return WH_ERROR_BADARGS; + } + + do { + rc = wh_Client_CustomCheckRegisteredRequest(c, id); + } while (rc == WH_ERROR_NOTREADY); + + if (rc == WH_ERROR_OK) { + do { + rc = wh_Client_CustomCbCheckRegisteredResponse(c, &id, responseError); + } while (rc == WH_ERROR_NOTREADY); + } + + return rc; } \ No newline at end of file diff --git a/src/wh_message_custom.c b/src/wh_message_customcb.c similarity index 66% rename from src/wh_message_custom.c rename to src/wh_message_customcb.c index b8ed5963..1512d4be 100644 --- a/src/wh_message_custom.c +++ b/src/wh_message_customcb.c @@ -1,21 +1,21 @@ #include #include -#include "wolfhsm/wh_message_custom.h" +#include "wolfhsm/wh_message_customcb.h" #include "wolfhsm/wh_error.h" #include "wolfhsm/wh_comm.h" static void _translateCustomData(uint16_t magic, uint32_t translatedType, - const whMessageCustom_Data* src, - whMessageCustom_Data* dst) + const whMessageCustomCb_Data* src, + whMessageCustomCb_Data* dst) { - if (translatedType < WH_MESSAGE_CUSTOM_TYPE_USER_DEFINED_START) { + if (translatedType < WH_MESSAGE_CUSTOM_CB_TYPE_USER_DEFINED_START) { switch (translatedType) { - case WH_MESSAGE_CUSTOM_TYPE_QUERY: { + case WH_MESSAGE_CUSTOM_CB_TYPE_QUERY: { /* right now, no further translations required */ } break; - case WH_MESSAGE_CUSTOM_TYPE_DMA32: { + case WH_MESSAGE_CUSTOM_CB_TYPE_DMA32: { dst->dma32.client_addr = wh_Translate32(magic, src->dma32.client_addr); dst->dma32.client_sz = @@ -25,7 +25,7 @@ static void _translateCustomData(uint16_t magic, uint32_t translatedType, dst->dma32.server_sz = wh_Translate32(magic, src->dma32.server_sz); } break; - case WH_MESSAGE_CUSTOM_TYPE_DMA64: { + case WH_MESSAGE_CUSTOM_CB_TYPE_DMA64: { dst->dma64.client_addr = wh_Translate64(magic, src->dma64.client_addr); dst->dma64.client_sz = @@ -42,21 +42,20 @@ static void _translateCustomData(uint16_t magic, uint32_t translatedType, } else { /* use memmove in case data is translated "in place" */ - memmove(dst->buffer.data, src->buffer.data, - sizeof(dst->buffer.data)); + memmove(dst->buffer.data, src->buffer.data, sizeof(dst->buffer.data)); } } -int wh_MessageCustom_TranslateRequest(uint16_t magic, - const whMessageCustom_Request* src, - whMessageCustom_Request* dst) +int wh_MessageCustomCb_TranslateRequest(uint16_t magic, + const whMessageCustomCb_Request* src, + whMessageCustomCb_Request* dst) { if ((src == NULL) || (dst == NULL)) { return WH_ERROR_BADARGS; } - dst->id = wh_Translate16(magic, src->id); + dst->id = wh_Translate32(magic, src->id); dst->type = wh_Translate32(magic, src->type); _translateCustomData(magic, dst->type, &src->data, &dst->data); @@ -64,9 +63,9 @@ int wh_MessageCustom_TranslateRequest(uint16_t magic, } -int wh_MessageCustom_TranslateResponse(uint16_t magic, - const whMessageCustom_Response* src, - whMessageCustom_Response* dst) +int wh_MessageCustomCb_TranslateResponse(uint16_t magic, + const whMessageCustomCb_Response* src, + whMessageCustomCb_Response* dst) { if ((src == NULL) || (dst == NULL)) { return WH_ERROR_BADARGS; @@ -77,7 +76,7 @@ int wh_MessageCustom_TranslateResponse(uint16_t magic, /* TODO: should we continue to translate responses for err != 0? * Probably still should...*/ - dst->id = wh_Translate16(magic, src->id); + dst->id = wh_Translate32(magic, src->id); dst->type = wh_Translate32(magic, src->type); _translateCustomData(magic, dst->type, &src->data, &dst->data); diff --git a/src/wh_server.c b/src/wh_server.c index d302df2e..edd9ee19 100644 --- a/src/wh_server.c +++ b/src/wh_server.c @@ -265,7 +265,7 @@ int wh_Server_HandleRequestMessage(whServerContext* server) #endif case WH_MESSAGE_GROUP_CUSTOM: - rc = wh_Server_HandleCustomRequest(server, magic, action, seq, + rc = wh_Server_HandleCustomCbRequest(server, magic, action, seq, size, data, &size, data); break; diff --git a/src/wh_server_custom.c b/src/wh_server_customcb.c similarity index 64% rename from src/wh_server_custom.c rename to src/wh_server_customcb.c index c588a298..5685b7bb 100644 --- a/src/wh_server_custom.c +++ b/src/wh_server_customcb.c @@ -2,13 +2,14 @@ #include "wolfhsm/wh_common.h" #include "wolfhsm/wh_error.h" #include "wolfhsm/wh_message.h" -#include "wolfhsm/wh_message_custom.h" +#include "wolfhsm/wh_message_customcb.h" -int wh_Server_RegisterCustomCb(whServerContext* server, uint16_t action, whServerCustomCb handler) +int wh_Server_RegisterCustomCb(whServerContext* server, uint16_t action, + whServerCustomCb handler) { if (NULL == server || NULL == handler || - action >= WH_CUSTOM_RQST_NUM_CALLBACKS) { + action >= WH_CUSTOM_CB_NUM_CALLBACKS) { return WH_ERROR_BADARGS; } @@ -18,33 +19,33 @@ int wh_Server_RegisterCustomCb(whServerContext* server, uint16_t action, whServe } -int wh_Server_HandleCustomRequest(whServerContext* server, uint16_t magic, - uint16_t action, uint16_t seq, - uint16_t req_size, const void* req_packet, - uint16_t* out_resp_size, void* resp_packet) +int wh_Server_HandleCustomCbRequest(whServerContext* server, uint16_t magic, + uint16_t action, uint16_t seq, + uint16_t req_size, const void* req_packet, + uint16_t* out_resp_size, void* resp_packet) { - int rc = 0; - whMessageCustom_Request req = {0}; - whMessageCustom_Response resp = {0}; + int rc = 0; + whMessageCustomCb_Request req = {0}; + whMessageCustomCb_Response resp = {0}; if (NULL == server || NULL == req_packet || NULL == resp_packet || out_resp_size == NULL) { return WH_ERROR_BADARGS; } - if (action >= WH_CUSTOM_RQST_NUM_CALLBACKS) { + if (action >= WH_CUSTOM_CB_NUM_CALLBACKS) { /* Invalid callback index */ /* TODO: is this the appropriate error to return? */ return WH_ERROR_BADARGS; } - if (req_size != sizeof(whMessageCustom_Request)) { + if (req_size != sizeof(whMessageCustomCb_Request)) { /* Request is malformed */ return WH_ERROR_ABORTED; } /* Translate the request */ - if ((rc = wh_MessageCustom_TranslateRequest(magic, req_packet, &req)) != + if ((rc = wh_MessageCustomCb_TranslateRequest(magic, req_packet, &req)) != WH_ERROR_OK) { return rc; } @@ -52,7 +53,7 @@ int wh_Server_HandleCustomRequest(whServerContext* server, uint16_t magic, if (server->customHandlerTable[action] != NULL) { /* If this isn't a query to check if the callback exists, invoke the * registered callback, storing the return value in the reponse */ - if (req.type != WH_MESSAGE_CUSTOM_TYPE_QUERY) { + if (req.type != WH_MESSAGE_CUSTOM_CB_TYPE_QUERY) { resp.rc = server->customHandlerTable[action](server, &req, &resp); } /* TODO: propagate other wolfHSM error codes (requires modifiying caller @@ -69,8 +70,8 @@ int wh_Server_HandleCustomRequest(whServerContext* server, uint16_t magic, resp.id = req.id; /* Translate the response */ - if ((rc = wh_MessageCustom_TranslateResponse(magic, &resp, resp_packet)) != - WH_ERROR_OK) { + if ((rc = wh_MessageCustomCb_TranslateResponse( + magic, &resp, resp_packet)) != WH_ERROR_OK) { return rc; } diff --git a/test/Makefile b/test/Makefile index 6a45c258..489a6e52 100644 --- a/test/Makefile +++ b/test/Makefile @@ -80,13 +80,13 @@ SRC_C += \ $(WOLFHSM_DIR)/src/wh_flash_unit.c \ $(WOLFHSM_DIR)/src/wh_message_comm.c \ $(WOLFHSM_DIR)/src/wh_message_nvm.c \ - $(WOLFHSM_DIR)/src/wh_message_custom.c \ + $(WOLFHSM_DIR)/src/wh_message_customcb.c \ $(WOLFHSM_DIR)/src/wh_nvm.c \ $(WOLFHSM_DIR)/src/wh_nvm_flash.c \ $(WOLFHSM_DIR)/src/wh_server.c \ $(WOLFHSM_DIR)/src/wh_server_nvm.c \ $(WOLFHSM_DIR)/src/wh_server_crypto.c \ - $(WOLFHSM_DIR)/src/wh_server_custom.c \ + $(WOLFHSM_DIR)/src/wh_server_customcb.c \ $(WOLFHSM_DIR)/src/wh_client_cryptocb.c \ $(WOLFHSM_DIR)/src/wh_transport_mem.c \ $(WOLFHSM_DIR)/src/wh_flash_ramsim.c diff --git a/test/wh_test_clientserver.c b/test/wh_test_clientserver.c index 83d37d32..560d1a46 100644 --- a/test/wh_test_clientserver.c +++ b/test/wh_test_clientserver.c @@ -35,20 +35,20 @@ /* Dummy callback that loopback-copies client data */ static int _customServerCb(whServerContext* server, - const whMessageCustom_Request* req, - whMessageCustom_Response* resp) + const whMessageCustomCb_Request* req, + whMessageCustomCb_Response* resp) { uint8_t* serverPtr = NULL; uint8_t* clientPtr = NULL; size_t copySz = 0; - if (req->type == WH_MESSAGE_CUSTOM_TYPE_DMA64) { + if (req->type == WH_MESSAGE_CUSTOM_CB_TYPE_DMA64) { clientPtr = (uint8_t*)((uintptr_t)req->data.dma64.client_addr); serverPtr = (uint8_t*)((uintptr_t)req->data.dma64.server_addr); resp->data.dma64.client_sz = req->data.dma64.server_sz; copySz = req->data.dma64.server_sz; } - else if (req->type == WH_MESSAGE_CUSTOM_TYPE_DMA32) { + else if (req->type == WH_MESSAGE_CUSTOM_CB_TYPE_DMA32) { clientPtr = (uint8_t*)((uintptr_t)req->data.dma32.client_addr); serverPtr = (uint8_t*)((uintptr_t)req->data.dma32.server_addr); resp->data.dma32.client_sz = req->data.dma32.server_sz; @@ -65,8 +65,8 @@ static int _customServerCb(whServerContext* server, static int _testCallbacks(whServerContext* server, whClientContext* client) { size_t counter; - whMessageCustom_Request req = {0}; - whMessageCustom_Response resp = {0}; + whMessageCustomCb_Request req = {0}; + whMessageCustomCb_Response resp = {0}; uint16_t outId = 0; int respErr = 0; @@ -74,20 +74,20 @@ static int _testCallbacks(whServerContext* server, whClientContext* client) "universe and everything is 42"; char output[sizeof(input)] = {0}; - for (counter = 0; counter < WH_CUSTOM_RQST_NUM_CALLBACKS; counter++) { + for (counter = 0; counter < WH_CUSTOM_CB_NUM_CALLBACKS; counter++) { req.id = counter; /* Check that the callback shows as unregistered */ - WH_TEST_RETURN_ON_FAIL(wh_Client_CustomRequestCheckRegistered(client, req.id)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CustomCheckRegisteredRequest(client, req.id)); WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); - WH_TEST_RETURN_ON_FAIL(wh_Client_CustomResponseCheckRegistered(client, &outId, &respErr)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CustomCbCheckRegisteredResponse(client, &outId, &respErr)); WH_TEST_ASSERT_RETURN(outId == req.id); WH_TEST_ASSERT_RETURN(respErr == WH_ERROR_NO_HANDLER); /* Test that calling an unregistered callback returns error */ - WH_TEST_RETURN_ON_FAIL(wh_Client_CustomRequest(client, &req)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CustomCbRequest(client, &req)); WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); - WH_TEST_RETURN_ON_FAIL(wh_Client_CustomResponse(client, &resp)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CustomCbResponse(client, &resp)); WH_TEST_ASSERT_RETURN(resp.err == WH_ERROR_NO_HANDLER); /* Register a custom callback */ @@ -95,16 +95,16 @@ static int _testCallbacks(whServerContext* server, whClientContext* client) wh_Server_RegisterCustomCb(server, counter, _customServerCb)); /* Check that the callback now shows as registered */ - WH_TEST_RETURN_ON_FAIL(wh_Client_CustomRequestCheckRegistered(client, req.id)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CustomCheckRegisteredRequest(client, req.id)); WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); - WH_TEST_RETURN_ON_FAIL(wh_Client_CustomResponseCheckRegistered(client, &outId, &respErr)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CustomCbCheckRegisteredResponse(client, &outId, &respErr)); WH_TEST_ASSERT_RETURN(outId == req.id); WH_TEST_ASSERT_RETURN(respErr == WH_ERROR_OK); /* prepare the rest of the request */ if (sizeof(uintptr_t) == sizeof(uint64_t)) { /* 64-bit host system */ - req.type = WH_MESSAGE_CUSTOM_TYPE_DMA64; + req.type = WH_MESSAGE_CUSTOM_CB_TYPE_DMA64; req.data.dma64.server_addr = (uint64_t)((uintptr_t)input); req.data.dma64.server_sz = sizeof(input); req.data.dma64.client_addr = (uint64_t)((uintptr_t)output); @@ -112,16 +112,16 @@ static int _testCallbacks(whServerContext* server, whClientContext* client) } else if (sizeof(uintptr_t) == sizeof(uint32_t)) { /* 32-bit host system */ - req.type = WH_MESSAGE_CUSTOM_TYPE_DMA32; + req.type = WH_MESSAGE_CUSTOM_CB_TYPE_DMA32; req.data.dma32.server_addr = (uint32_t)((uintptr_t)&input); req.data.dma32.server_sz = sizeof(input); req.data.dma32.client_addr = (uint32_t)((uintptr_t)output); req.data.dma32.client_sz = 0; } - WH_TEST_RETURN_ON_FAIL(wh_Client_CustomRequest(client, &req)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CustomCbRequest(client, &req)); WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server)); - WH_TEST_RETURN_ON_FAIL(wh_Client_CustomResponse(client, &resp)); + WH_TEST_RETURN_ON_FAIL(wh_Client_CustomCbResponse(client, &resp)); WH_TEST_ASSERT_RETURN(resp.err == WH_ERROR_OK); WH_TEST_ASSERT_RETURN(resp.rc == counter); WH_TEST_ASSERT_RETURN(0 == memcmp(output, input, sizeof(input))); diff --git a/wolfhsm/wh_client.h b/wolfhsm/wh_client.h index b19d176a..cf7beedc 100644 --- a/wolfhsm/wh_client.h +++ b/wolfhsm/wh_client.h @@ -26,7 +26,7 @@ /* Component includes */ #include "wolfhsm/wh_comm.h" -#include "wolfhsm/wh_message_custom.h" +#include "wolfhsm/wh_message_customcb.h" /* Client context */ struct whClientContext_t { @@ -180,14 +180,14 @@ int wh_Client_NvmReadDma(whClientContext* c, /* Client custom-callback support */ -int wh_Client_CustomRequest(whClientContext* c, const whMessageCustom_Request* req); -int wh_Client_CustomResponse(whClientContext* c, whMessageCustom_Response *resp); +int wh_Client_CustomCbRequest(whClientContext* c, const whMessageCustomCb_Request* req); +int wh_Client_CustomCbResponse(whClientContext* c, whMessageCustomCb_Response *resp); /* Instructs server to query if a callback is registered */ -int wh_Client_CustomRequestCheckRegistered(whClientContext* c, uint32_t id); +int wh_Client_CustomCheckRegisteredRequest(whClientContext* c, uint32_t id); /* Processes a server response to callback query. OutId is set to the ID of the * received query. ResponseError is set to WH_ERROR_OK if the callback is * registered, and WH_ERROR_NO_HANDLER if not */ -int wh_Client_CustomResponseCheckRegistered(whClientContext* c, uint16_t* outId, int* responseError); +int wh_Client_CustomCbCheckRegisteredResponse(whClientContext* c, uint16_t* outId, int* responseError); #endif /* WOLFHSM_WH_CLIENT_H_ */ diff --git a/wolfhsm/wh_common.h b/wolfhsm/wh_common.h index cec5ab69..f023c26e 100644 --- a/wolfhsm/wh_common.h +++ b/wolfhsm/wh_common.h @@ -99,6 +99,6 @@ typedef struct { extern const whManifest_ex manifests[WOLFHSM_NUM_MANIFESTS]; /* Custom request shared defs */ -#define WH_CUSTOM_RQST_NUM_CALLBACKS 8 +#define WH_CUSTOM_CB_NUM_CALLBACKS 8 #endif /* WOLFHSM_WH_COMMON_H_ */ diff --git a/wolfhsm/wh_message_custom.h b/wolfhsm/wh_message_custom.h deleted file mode 100644 index b2eb498a..00000000 --- a/wolfhsm/wh_message_custom.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef WH_MESSAGE_CUSTOM_H_ -#define WH_MESSAGE_CUSTOM_H_ - -#include - -#define WH_MESSAGE_CUSTOM_BUF_SIZE (256) - -/* Type indicator for custom request/response messages. Indicates how - * to interpret whMessageCustomData */ -typedef enum { - /* message types reserved for internal usage*/ - WH_MESSAGE_CUSTOM_TYPE_QUERY = 0, - WH_MESSAGE_CUSTOM_TYPE_DMA32 = 1, - WH_MESSAGE_CUSTOM_TYPE_DMA64 = 2, - WH_MESSAGE_CUSTOM_TYPE_RESERVED_3 = 3, - WH_MESSAGE_CUSTOM_TYPE_RESERVED_4 = 4, - WH_MESSAGE_CUSTOM_TYPE_RESERVED_5 = 5, - WH_MESSAGE_CUSTOM_TYPE_RESERVED_6 = 6, - WH_MESSAGE_CUSTOM_TYPE_RESERVED_7 = 7, - /* User-defined types start from here, up to UINT32_MAX */ - WH_MESSAGE_CUSTOM_TYPE_USER_DEFINED_START = 8, -} whMessageCustom_Type; - - -/* union providing some helpful abstractions for passing pointers in/out of - * custom callbacks on top of a raw data buffer */ -typedef union { - /* pointer/size pairs for 32-bit systems */ - struct { - uint32_t client_addr; - uint32_t client_sz; - uint32_t server_addr; - uint32_t server_sz; - } dma32; - /* pointer/size pairs for 64-bit systems */ - struct { - uint64_t client_addr; - uint64_t client_sz; - uint64_t server_addr; - uint64_t server_sz; - } dma64; - /* raw data buffer for user-defined schema */ - struct { - uint8_t data[WH_MESSAGE_CUSTOM_BUF_SIZE]; - } buffer; -} whMessageCustom_Data; - -/* request message to the custom server callback */ -typedef struct { - uint16_t id; /* indentifier of registered callback */ - uint32_t type; /* whMessageCustom_Type */ - whMessageCustom_Data data; -} whMessageCustom_Request; - -/* response message from the custom server callback */ -typedef struct { - uint16_t id; /* indentifier of registered callback */ - uint32_t type; /* whMessageCustom_Type */ - int32_t rc; /* Return code from custom callback. Invalid if err != 0 */ - int32_t err; /* wolfHSM-specific error. If err != 0, rc is invalid */ - whMessageCustom_Data data; -} whMessageCustom_Response; - - -/* Translates a custom request message. The whMessageCustom_Request.data field - * will not be translated for whMessageCustom_Request.type values greater than - * WH_MESSAGE_CUSTOM_TYPE_USER_DEFINED_START */ -int wh_MessageCustom_TranslateRequest(uint16_t magic, - const whMessageCustom_Request* src, - whMessageCustom_Request* dst); - -/* Translates a custom response message. The whMessageCustom_Request.data field - * will not be translated for whMessageCustom_Request.type values greater than - * WH_MESSAGE_CUSTOM_TYPE_USER_DEFINED_START */ -int wh_MessageCustom_TranslateResponse(uint16_t magic, - const whMessageCustom_Response* src, - whMessageCustom_Response* dst); - -#endif /* WH_MESSAGE_CUSTOM_H_*/ \ No newline at end of file diff --git a/wolfhsm/wh_message_customcb.h b/wolfhsm/wh_message_customcb.h new file mode 100644 index 00000000..c4b07213 --- /dev/null +++ b/wolfhsm/wh_message_customcb.h @@ -0,0 +1,79 @@ +#ifndef WH_MESSAGE_CUSTOM_CB_H_ +#define WH_MESSAGE_CUSTOM_CB_H_ + +#include + +#define WH_MESSAGE_CUSTOM_CB_BUF_SIZE (256) + +/* Type indicator for custom request/response messages. Indicates how + * to interpret whMessageCustomData */ +typedef enum { + /* message types reserved for internal usage*/ + WH_MESSAGE_CUSTOM_CB_TYPE_QUERY = 0, + WH_MESSAGE_CUSTOM_CB_TYPE_DMA32 = 1, + WH_MESSAGE_CUSTOM_CB_TYPE_DMA64 = 2, + WH_MESSAGE_CUSTOM_CB_TYPE_RESERVED_3 = 3, + WH_MESSAGE_CUSTOM_CB_TYPE_RESERVED_4 = 4, + WH_MESSAGE_CUSTOM_CB_TYPE_RESERVED_5 = 5, + WH_MESSAGE_CUSTOM_CB_TYPE_RESERVED_6 = 6, + WH_MESSAGE_CUSTOM_CB_TYPE_RESERVED_7 = 7, + /* User-defined types start from here, up to UINT32_MAX */ + WH_MESSAGE_CUSTOM_CB_TYPE_USER_DEFINED_START = 8, +} whMessageCustomCb_Type; + + +/* union providing some helpful abstractions for passing pointers in/out of + * custom callbacks on top of a raw data buffer */ +typedef union { + /* pointer/size pairs for 32-bit systems */ + struct { + uint32_t client_addr; + uint32_t client_sz; + uint32_t server_addr; + uint32_t server_sz; + } dma32; + /* pointer/size pairs for 64-bit systems */ + struct { + uint64_t client_addr; + uint64_t client_sz; + uint64_t server_addr; + uint64_t server_sz; + } dma64; + /* raw data buffer for user-defined schema */ + struct { + uint8_t data[WH_MESSAGE_CUSTOM_CB_BUF_SIZE]; + } buffer; +} whMessageCustomCb_Data; + +/* request message to the custom server callback */ +typedef struct { + uint32_t id; /* indentifier of registered callback */ + uint32_t type; /* whMessageCustomCb_Type */ + whMessageCustomCb_Data data; +} whMessageCustomCb_Request; + +/* response message from the custom server callback */ +typedef struct { + uint32_t id; /* indentifier of registered callback */ + uint32_t type; /* whMessageCustomCb_Type */ + int32_t rc; /* Return code from custom callback. Invalid if err != 0 */ + int32_t err; /* wolfHSM-specific error. If err != 0, rc is invalid */ + whMessageCustomCb_Data data; +} whMessageCustomCb_Response; + + +/* Translates a custom request message. The whMessageCustomCb_Request.data field + * will not be translated for whMessageCustomCb_Request.type values greater than + * WH_MESSAGE_CUSTOM_CB_TYPE_USER_DEFINED_START */ +int wh_MessageCustomCb_TranslateRequest(uint16_t magic, + const whMessageCustomCb_Request* src, + whMessageCustomCb_Request* dst); + +/* Translates a custom response message. The whMessageCustomCb_Request.data + * field will not be translated for whMessageCustomCb_Request.type values + * greater than WH_MESSAGE_CUSTOM_CB_TYPE_USER_DEFINED_START */ +int wh_MessageCustomCb_TranslateResponse(uint16_t magic, + const whMessageCustomCb_Response* src, + whMessageCustomCb_Response* dst); + +#endif /* WH_MESSAGE_CUSTOM_CB_H_*/ \ No newline at end of file diff --git a/wolfhsm/wh_server.h b/wolfhsm/wh_server.h index 3f8656c0..8944b767 100644 --- a/wolfhsm/wh_server.h +++ b/wolfhsm/wh_server.h @@ -12,7 +12,7 @@ #include "wolfhsm/wh_common.h" #include "wolfhsm/wh_comm.h" #include "wolfhsm/wh_nvm.h" -#include "wolfhsm/wh_message_custom.h" +#include "wolfhsm/wh_message_customcb.h" #include "wolfssl/wolfcrypt/settings.h" #include "wolfssl/wolfcrypt/random.h" @@ -45,8 +45,8 @@ struct whServerContext_t; /* Type definition for a custom server callback */ typedef int (*whServerCustomCb)( struct whServerContext_t* server, /* points to dispatching server ctx */ - const whMessageCustom_Request* req, /* request from client to callback */ - whMessageCustom_Response* resp /* response from callback to client */ + const whMessageCustomCb_Request* req, /* request from client to callback */ + whMessageCustomCb_Response* resp /* response from callback to client */ ); /* Context structure to maintain the state of an HSM server */ @@ -56,7 +56,7 @@ typedef struct whServerContext_t { whNvmContext nvm[1]; crypto_context crypto[1]; CacheSlot cache[WOLFHSM_NUM_RAMKEYS]; - whServerCustomCb customHandlerTable[WH_CUSTOM_RQST_NUM_CALLBACKS]; + whServerCustomCb customHandlerTable[WH_CUSTOM_CB_NUM_CALLBACKS]; } whServerContext; typedef struct whServerConfig_t { @@ -86,7 +86,7 @@ int wh_Server_RegisterCustomCb(whServerContext* server, uint16_t actionId, whSer /* Receive and handle an incoming custom callback request */ -int wh_Server_HandleCustomRequest(whServerContext* server, uint16_t magic, +int wh_Server_HandleCustomCbRequest(whServerContext* server, uint16_t magic, uint16_t action, uint16_t seq, uint16_t req_size, const void* req_packet, uint16_t* out_resp_size, void* resp_packet);