Skip to content

Commit

Permalink
added demo framework, added NVM demo, run NVM demo in posix/tcp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbrett committed Jun 13, 2024
1 parent 7bb7a4f commit bf3b9dc
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 12 deletions.
Empty file added demo/client/client_counter.c
Empty file.
Empty file added demo/client/client_counter.h
Empty file.
Empty file added demo/client/client_crypto.c
Empty file.
Empty file added demo/client/client_crypto.h
Empty file.
16 changes: 16 additions & 0 deletions demo/client/client_demo_driver.c
Original file line number Diff line number Diff line change
@@ -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;
}


8 changes: 8 additions & 0 deletions demo/client/client_demo_driver.h
Original file line number Diff line number Diff line change
@@ -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_ */
Empty file added demo/client/client_keystore.c
Empty file.
Empty file added demo/client/client_keystore.h
Empty file.
142 changes: 142 additions & 0 deletions demo/client/client_nvm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#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;
}
9 changes: 9 additions & 0 deletions demo/client/client_nvm.h
Original file line number Diff line number Diff line change
@@ -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_ */

28 changes: 17 additions & 11 deletions posix/tcp/wh_client_tcp/Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand All @@ -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 += \
Expand Down Expand Up @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions posix/tcp/wh_client_tcp/wh_client_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion posix/tcp/wh_server_tcp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ CFLAGS += -O0 -ffunction-sections -fdata-sections
#-nostartfiles #-nodefaultlibs # -nostdlib

# Debugging
DBGFLAGS = #-ggdb -g3
DBGFLAGS = -ggdb -g3
CFLAGS += $(DBGFLAGS)
LDFLAGS += $(DBGFLAGS)

Expand Down
39 changes: 39 additions & 0 deletions posix/tcp/wh_server_tcp/wh_server_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
#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 */
static void* wh_ServerTask(void* cf);

enum {
ONE_MS = 1000,
FLASH_RAM_SIZE = 1024 * 1024,
};

#define WH_SERVER_TCP_IPSTRING "127.0.0.1"
Expand Down Expand Up @@ -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};
Expand All @@ -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;
Expand Down

0 comments on commit bf3b9dc

Please sign in to comment.