From bf3b9dc6be2a8868ee8a48e2c63996e7c9a58cc5 Mon Sep 17 00:00:00 2001 From: Brett Nicholas <7547222+bigbrett@users.noreply.github.com> Date: Thu, 13 Jun 2024 15:36:00 -0600 Subject: [PATCH] added demo framework, added NVM demo, run NVM demo in posix/tcp tests --- demo/client/client_counter.c | 0 demo/client/client_counter.h | 0 demo/client/client_crypto.c | 0 demo/client/client_crypto.h | 0 demo/client/client_demo_driver.c | 16 +++ demo/client/client_demo_driver.h | 8 ++ demo/client/client_keystore.c | 0 demo/client/client_keystore.h | 0 demo/client/client_nvm.c | 142 ++++++++++++++++++++++++ demo/client/client_nvm.h | 9 ++ posix/tcp/wh_client_tcp/Makefile | 28 +++-- posix/tcp/wh_client_tcp/wh_client_tcp.c | 10 ++ posix/tcp/wh_server_tcp/Makefile | 2 +- posix/tcp/wh_server_tcp/wh_server_tcp.c | 39 +++++++ 14 files changed, 242 insertions(+), 12 deletions(-) create mode 100644 demo/client/client_counter.c create mode 100644 demo/client/client_counter.h create mode 100644 demo/client/client_crypto.c create mode 100644 demo/client/client_crypto.h create mode 100644 demo/client/client_demo_driver.c create mode 100644 demo/client/client_demo_driver.h create mode 100644 demo/client/client_keystore.c create mode 100644 demo/client/client_keystore.h create mode 100644 demo/client/client_nvm.c create mode 100644 demo/client/client_nvm.h diff --git a/demo/client/client_counter.c b/demo/client/client_counter.c new file mode 100644 index 0000000..e69de29 diff --git a/demo/client/client_counter.h b/demo/client/client_counter.h new file mode 100644 index 0000000..e69de29 diff --git a/demo/client/client_crypto.c b/demo/client/client_crypto.c new file mode 100644 index 0000000..e69de29 diff --git a/demo/client/client_crypto.h b/demo/client/client_crypto.h new file mode 100644 index 0000000..e69de29 diff --git a/demo/client/client_demo_driver.c b/demo/client/client_demo_driver.c new file mode 100644 index 0000000..47a619c --- /dev/null +++ b/demo/client/client_demo_driver.c @@ -0,0 +1,16 @@ +#include "client_demo_driver.h" +#include "client_nvm.h" + +int client_demo_driver(whClientContext* clientContext) +{ + int rc = 0; + + rc = client_nvm(clientContext); + if (rc != 0) { + return rc; + } + + return rc; +} + + diff --git a/demo/client/client_demo_driver.h b/demo/client/client_demo_driver.h new file mode 100644 index 0000000..0caea9b --- /dev/null +++ b/demo/client/client_demo_driver.h @@ -0,0 +1,8 @@ +#ifndef CLIENT_DEMO_DRIVER_H_ +#define CLIENT_DEMO_DRIVER_H_ + +#include "wolfhsm/wh_client.h" + +int client_demo_driver(whClientContext* clientContext); + +#endif /* CLIENT_DEMO_DRIVER_H_ */ diff --git a/demo/client/client_keystore.c b/demo/client/client_keystore.c new file mode 100644 index 0000000..e69de29 diff --git a/demo/client/client_keystore.h b/demo/client/client_keystore.h new file mode 100644 index 0000000..e69de29 diff --git a/demo/client/client_nvm.c b/demo/client/client_nvm.c new file mode 100644 index 0000000..531fab3 --- /dev/null +++ b/demo/client/client_nvm.c @@ -0,0 +1,142 @@ +#include +#include +#include + +#include "wolfhsm/wh_client.h" +#include "wolfhsm/wh_error.h" + +/** + * @brief Demonstrates use of client NVM API + + * @param clientContext an initialized client context + * @return 0 on success, error code on failure + */ +int client_nvm(whClientContext* clientContext) +{ + const int NUM_OBJECTS = 3; + + int32_t rc; + int32_t serverRc; + uint32_t availSize, reclaimSize; + whNvmId availObjects, reclaimObjects; + + whNvmId objectIds[] = {1, 2, 3}; + uint8_t labels[][6] = {"label1", "label2", "label3"}; + uint8_t data[][6] = {"data1", "data2", "data3"}; + uint8_t readData[6]; + whNvmSize dataLen = 6; + whNvmSize readLen; + + if (clientContext == NULL) { + printf("Client context is NULL\n"); + return WH_ERROR_BADARGS; + } + + /* Initialize NVM */ + rc = wh_Client_NvmInit(clientContext, &serverRc, NULL, NULL); + if (rc != 0 || serverRc != 0) { + printf("NVM Init failed with error code: %d, server error code: %d\n", + rc, serverRc); + return (rc != 0) ? rc : serverRc; + } + printf("NVM Initialized successfully\n"); + + /* Add multiple objects, reading back each one and comparing the data + * against what we wrote */ + for (int i = 0; i < NUM_OBJECTS; i++) { + /* Add an object */ + rc = wh_Client_NvmAddObject(clientContext, objectIds[i], + WOLFHSM_NVM_ACCESS_ANY, + WOLFHSM_NVM_FLAGS_ANY, sizeof(labels[i]), + labels[i], dataLen, data[i], &serverRc); + if (rc != 0 || serverRc != 0) { + printf("Add Object %d failed with error code: %d, server error " + "code: %d\n", + objectIds[i], rc, serverRc); + return (rc != 0) ? rc : serverRc; + } + printf("Object %d added successfully\n", objectIds[i]); + + /* Read the object data */ + rc = wh_Client_NvmRead(clientContext, objectIds[i], 0, dataLen, + &serverRc, &readLen, readData); + if (rc != 0 || serverRc != 0) { + printf("Read Object %d failed with error code: %d, server error " + "code: %d\n", + objectIds[i], rc, serverRc); + return (rc != 0) ? rc : serverRc; + } + printf("Object %d read successfully: Data=%s\n", objectIds[i], + readData); + + /* Ensure data we read matches data we wrote */ + if (memcmp(data[i], readData, dataLen) != 0) { + printf("Readback check failed for Object %d: Data read does not " + "match data written\n", + objectIds[i]); + return WH_ERROR_ABORTED; + } + printf("Readback check passed for Object %d: Data read matches data " + "written\n", + objectIds[i]); + } + + /* Get available objects */ + rc = + wh_Client_NvmGetAvailable(clientContext, &serverRc, &availSize, + &availObjects, &reclaimSize, &reclaimObjects); + if (rc != 0 || serverRc != 0) { + printf("Get Available Objects failed with error code: %d, server error " + "code: %d\n", + rc, serverRc); + return (rc != 0) ? rc : serverRc; + } + printf("Available Objects retrieved successfully: Available Size=%d, " + "Available Objects=%d, Reclaim Size=%d, Reclaim Objects=%d\n", + availSize, availObjects, reclaimSize, reclaimObjects); + + /* Delete one object */ + rc = wh_Client_NvmDestroyObjects(clientContext, 1, objectIds, 0, NULL, + &serverRc); + if (rc != 0 || serverRc != 0) { + printf("Delete Objects failed with error code: %d, server error code: " + "%d\n", + rc, serverRc); + return (rc != 0) ? rc : serverRc; + } + printf("Objects deleted successfully\n"); + + /* Delete multiple objects */ + rc = wh_Client_NvmDestroyObjects(clientContext, NUM_OBJECTS - 1, + &objectIds[1], 0, NULL, &serverRc); + if (rc != 0 || serverRc != 0) { + printf("Delete Objects failed with error code: %d, server error code: " + "%d\n", + rc, serverRc); + return (rc != 0) ? rc : serverRc; + } + printf("Objects deleted successfully\n"); + + /* Reclaim space */ + rc = + wh_Client_NvmDestroyObjects(clientContext, 0, NULL, 0, NULL, &serverRc); + if (rc != 0 || serverRc != 0) { + printf("Reclaim Objects failed with error code: %d, server error code: " + "%d\n", + rc, serverRc); + return (rc != 0) ? rc : serverRc; + } + printf("Reclaimed space successfully\n"); + + /* Cleanup NVM */ + rc = wh_Client_NvmCleanup(clientContext, &serverRc); + if (rc != 0 || serverRc != 0) { + printf( + "NVM Cleanup failed with error code: %d, server error code: %d\n", + rc, serverRc); + return (rc != 0) ? rc : serverRc; + } + printf("NVM Cleaned up successfully\n"); + + return 0; +} diff --git a/demo/client/client_nvm.h b/demo/client/client_nvm.h new file mode 100644 index 0000000..2386217 --- /dev/null +++ b/demo/client/client_nvm.h @@ -0,0 +1,9 @@ +#ifndef CLIENT_NVM_H_ +#define CLIENT_NVM_H_ + +#include "wolfhsm/wh_client.h" + +int client_nvm(whClientContext* clientContext); + +#endif /* CLIENT_NVM_H_ */ + diff --git a/posix/tcp/wh_client_tcp/Makefile b/posix/tcp/wh_client_tcp/Makefile index ab86c79..a9cba49 100644 --- a/posix/tcp/wh_client_tcp/Makefile +++ b/posix/tcp/wh_client_tcp/Makefile @@ -1,12 +1,13 @@ # Set to @ if you want to suppress command echo -CMD_ECHO = +CMD_ECHO = # Important directories BUILD_DIR = ./Build WOLFHSM_DIR = $(CURDIR)/../../../../wolfHSM - +WOLFHSM_CLIENT_DEMO_DIR = $(CURDIR)/../../../demo/client WOLFSSL_DIR ?= $(CURDIR)/../../../../wolfssl + # Project name BIN = wh_client_tcp @@ -15,12 +16,13 @@ USER_SETTINGS_DIR ?= ./ INC = -I$(WOLFHSM_DIR) \ -I$(USER_SETTINGS_DIR) \ -I$(WOLFSSL_DIR) \ + -I$(WOLFHSM_CLIENT_DEMO_DIR) # Defines DEF = -DWOLFSSL_USER_SETTINGS -D_GNUC_ - + # Architecture -ARCHFLAGS ?= +ARCHFLAGS ?= # Compiler and linker flags ASFLAGS ?= $(ARCHFLAGS) @@ -34,21 +36,21 @@ LDFLAGS ?= $(ARCHFLAGS) # Libc for printf LIBS = -lc -# Optimization level and place functions / data into separate sections to allow dead code removal -CFLAGS += -O0 -ffunction-sections -fdata-sections +# Optimization level and place functions / data into separate sections to allow dead code removal +CFLAGS += -O0 -ffunction-sections -fdata-sections #-fstrict-volatile-bitfields #-fno-builtin # Remove unused sections and link time optimizations -#LDFLAGS += -Wl,--cref -Wl,--gc-sections -static -u _printf_float -Wl,--start-group $(LIBS) -Wl,--end-group -flto -#-nostartfiles #-nodefaultlibs # -nostdlib +#LDFLAGS += -Wl,--cref -Wl,--gc-sections -static -u _printf_float -Wl,--start-group $(LIBS) -Wl,--end-group -flto +#-nostartfiles #-nodefaultlibs # -nostdlib # Debugging -DBGFLAGS = #-ggdb -g3 +DBGFLAGS = -ggdb -g3 CFLAGS += $(DBGFLAGS) LDFLAGS += $(DBGFLAGS) # Assembly source files -SRC_ASM += +#SRC_ASM += #wolfCrypt source files SRC_C += \ @@ -83,8 +85,12 @@ SRC_C += \ # WolfHSM port\HAL code SRC_C += $(WOLFHSM_DIR)/port/posix/posix_transport_tcp.c +# Demo client code +SRC_C += $(WOLFHSM_CLIENT_DEMO_DIR)/client_nvm.c \ + $(WOLFHSM_CLIENT_DEMO_DIR)/client_demo_driver.c + # APP -SRC_C += ./src/wh_client_tcp.c +SRC_C += ./src/wh_client_tcp.c FILENAMES_C = $(notdir $(SRC_C)) diff --git a/posix/tcp/wh_client_tcp/wh_client_tcp.c b/posix/tcp/wh_client_tcp/wh_client_tcp.c index bec9dbf..5093a15 100644 --- a/posix/tcp/wh_client_tcp/wh_client_tcp.c +++ b/posix/tcp/wh_client_tcp/wh_client_tcp.c @@ -13,6 +13,8 @@ #include "wolfhsm/wh_client.h" #include "port/posix/posix_transport_tcp.h" +#include "client_demo_driver.h" + /** Local declarations */ static void* wh_ClientTask(void* cf); @@ -86,6 +88,14 @@ static void* wh_ClientTask(void* cf) break; } } + + /* run the client demos */ + ret = client_demo_driver(client); + if (ret != 0) { + printf("Client demo failed: ret=%d\n", ret); + } + + wh_Client_CommClose(client); ret = wh_Client_Cleanup(client); printf("Client disconnected\n"); diff --git a/posix/tcp/wh_server_tcp/Makefile b/posix/tcp/wh_server_tcp/Makefile index e467715..c1cbf3d 100644 --- a/posix/tcp/wh_server_tcp/Makefile +++ b/posix/tcp/wh_server_tcp/Makefile @@ -43,7 +43,7 @@ CFLAGS += -O0 -ffunction-sections -fdata-sections #-nostartfiles #-nodefaultlibs # -nostdlib # Debugging -DBGFLAGS = #-ggdb -g3 +DBGFLAGS = -ggdb -g3 CFLAGS += $(DBGFLAGS) LDFLAGS += $(DBGFLAGS) diff --git a/posix/tcp/wh_server_tcp/wh_server_tcp.c b/posix/tcp/wh_server_tcp/wh_server_tcp.c index 83c4117..67ea1e1 100644 --- a/posix/tcp/wh_server_tcp/wh_server_tcp.c +++ b/posix/tcp/wh_server_tcp/wh_server_tcp.c @@ -11,6 +11,9 @@ #include "wolfhsm/wh_comm.h" #include "wolfhsm/wh_message.h" #include "wolfhsm/wh_server.h" +#include "wolfhsm/wh_nvm.h" +#include "wolfhsm/wh_nvm_flash.h" +#include "wolfhsm/wh_flash_ramsim.h" #include "port/posix/posix_transport_tcp.h" /** Local declarations */ @@ -18,6 +21,7 @@ static void* wh_ServerTask(void* cf); enum { ONE_MS = 1000, + FLASH_RAM_SIZE = 1024 * 1024, }; #define WH_SERVER_TCP_IPSTRING "127.0.0.1" @@ -66,6 +70,7 @@ static void* wh_ServerTask(void* cf) int main(int argc, char** argv) { (void)argc; (void)argv; + int rc = 0; /* Server configuration/context */ whTransportServerCb ptttcb[1] = {PTT_SERVER_CB}; @@ -80,10 +85,44 @@ int main(int argc, char** argv) .transport_config = (void*)mytcpconfig, .server_id = WH_SERVER_ID, }}; + + /* RamSim Flash state and configuration */ + whFlashRamsimCtx fc[1] = {0}; + whFlashRamsimCfg fc_conf[1] = {{ + .size = FLASH_RAM_SIZE, + .sectorSize = FLASH_RAM_SIZE/2, + .pageSize = 8, + .erasedByte = (uint8_t)0, + }}; + const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; + + /* NVM Flash Configuration using RamSim HAL Flash */ + whNvmFlashConfig nf_conf[1] = {{ + .cb = fcb, + .context = fc, + .config = fc_conf, + }}; + whNvmFlashContext nfc[1] = {0}; + whNvmCb nfcb[1] = {WH_NVM_FLASH_CB}; + + whNvmConfig n_conf[1] = {{ + .cb = nfcb, + .context = nfc, + .config = nf_conf, + }}; + whNvmContext nvm[1] = {{0}}; + whServerConfig s_conf[1] = {{ .comm_config = cs_conf, + .nvm = nvm, }}; + rc = wh_Nvm_Init(nvm, n_conf); + if (rc != 0) { + printf("Failed to initialize NVM: %d\n", rc); + return rc; + } + wh_ServerTask(s_conf); return 0;