Skip to content

Commit 74fe9aa

Browse files
committed
refactor posix demo server to register the demo client wrapped key IDs, change macro names for consistency
1 parent 8c346ce commit 74fe9aa

4 files changed

Lines changed: 60 additions & 32 deletions

File tree

examples/demo/client/wh_demo_client_keywrap.c

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535

3636
#ifdef WOLFHSM_CFG_KEYWRAP
3737

38-
#define WH_TEST_KEKID 1
38+
#define WH_DEMO_KEYWRAP_KEKID 1
3939
static int _InitServerKek(whClientContext* ctx)
4040
{
4141
/* IMPORTANT NOTE: Server KEK is typically intrinsic or set during
4242
* provisioning. Uploading the KEK via the client is for testing purposes
4343
* only and not intended as a recommendation */
44-
whKeyId serverKeyId = WH_TEST_KEKID;
44+
whKeyId serverKeyId = WH_DEMO_KEYWRAP_KEKID;
4545
whNvmFlags flags = WH_NVM_FLAGS_NONEXPORTABLE;
4646
uint8_t label[WH_NVM_LABEL_LEN] = "Server KEK key";
4747
uint8_t kek[] = {0x03, 0x03, 0x0d, 0xd9, 0xeb, 0x18, 0x17, 0x2e,
@@ -55,43 +55,42 @@ static int _InitServerKek(whClientContext* ctx)
5555

5656
static int _CleanupServerKek(whClientContext* ctx)
5757
{
58-
return wh_Client_KeyErase(ctx, WH_TEST_KEKID);
58+
return wh_Client_KeyErase(ctx, WH_DEMO_KEYWRAP_KEKID);
5959
}
6060

6161
#ifndef NO_AES
6262
#ifdef HAVE_AESGCM
6363

64-
#define WH_TEST_AES_KEYSIZE 16
65-
#define WH_TEST_AES_TEXTSIZE 16
66-
#define WH_TEST_AES_IVSIZE 12
67-
#define WH_TEST_AES_TAGSIZE 16
68-
#define WH_TEST_AES_WRAPPED_KEYSIZE \
69-
(WH_TEST_AES_IVSIZE + WH_TEST_AES_TAGSIZE + WH_TEST_AES_KEYSIZE + \
70-
sizeof(whNvmMetadata))
71-
#define WH_TEST_AESGCM_WRAPKEY_ID 8
64+
#define WH_DEMO_KEYWRAP_AES_KEYSIZE 16
65+
#define WH_DEMO_KEYWRAP_AES_TEXTSIZE 16
66+
#define WH_DEMO_KEYWRAP_AES_IVSIZE 12
67+
#define WH_DEMO_KEYWRAP_AES_TAGSIZE 16
68+
#define WH_DEMO_KEYWRAP_AES_WRAPPED_KEYSIZE \
69+
(WH_DEMO_KEYWRAP_AES_IVSIZE + WH_DEMO_KEYWRAP_AES_TAGSIZE + \
70+
WH_DEMO_KEYWRAP_AES_KEYSIZE + sizeof(whNvmMetadata))
71+
#define WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID 8
7272

7373
int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
7474
{
7575
int ret = 0;
7676
Aes aes[1];
7777
WC_RNG rng[1];
78-
uint8_t key[WH_TEST_AES_KEYSIZE];
79-
uint8_t exportedKey[WH_TEST_AES_KEYSIZE];
80-
whNvmMetadata metadata = {
81-
.id = WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO, 0, WH_TEST_AESGCM_WRAPKEY_ID),
82-
.label = "AES Key Label",
83-
.access = WH_NVM_ACCESS_ANY,
84-
.len = WH_TEST_AES_KEYSIZE};
78+
uint8_t key[WH_DEMO_KEYWRAP_AES_KEYSIZE];
79+
uint8_t exportedKey[WH_DEMO_KEYWRAP_AES_KEYSIZE];
80+
whNvmMetadata metadata = {.id = WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID,
81+
.label = "AES Key Label",
82+
.access = WH_NVM_ACCESS_ANY,
83+
.len = WH_DEMO_KEYWRAP_AES_KEYSIZE};
8584
whNvmMetadata exportedMetadata;
86-
uint8_t wrappedKey[WH_TEST_AES_WRAPPED_KEYSIZE];
85+
uint8_t wrappedKey[WH_DEMO_KEYWRAP_AES_WRAPPED_KEYSIZE];
8786
whKeyId wrappedKeyId;
8887

8988
const uint8_t plaintext[] = "hello, wolfSSL AES-GCM!";
9089
uint8_t ciphertext[sizeof(plaintext)];
9190
uint8_t decrypted[sizeof(plaintext)];
9291

93-
uint8_t tag[WH_TEST_AES_TAGSIZE];
94-
uint8_t iv[WH_TEST_AES_IVSIZE];
92+
uint8_t tag[WH_DEMO_KEYWRAP_AES_TAGSIZE];
93+
uint8_t iv[WH_DEMO_KEYWRAP_AES_IVSIZE];
9594
const uint8_t aad[] = {0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe,
9695
0xef, 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad,
9796
0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2};
@@ -127,8 +126,8 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
127126

128127
/* Now we request the server to wrap the key using the KEK we
129128
* establish above in the first step. */
130-
ret = wh_Client_KeyWrap(client, WC_CIPHER_AES_GCM, WH_TEST_KEKID, key,
131-
sizeof(key), &metadata, wrappedKey,
129+
ret = wh_Client_KeyWrap(client, WC_CIPHER_AES_GCM, WH_DEMO_KEYWRAP_KEKID,
130+
key, sizeof(key), &metadata, wrappedKey,
132131
sizeof(wrappedKey));
133132
if (ret != 0) {
134133
printf("Failed to wh_Client_KeyWrap %d\n", ret);
@@ -144,9 +143,9 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
144143
/* Request the server to unwrap and cache the wrapped key we just created.
145144
* This will provide us back a key ID that the client can use to do crypto
146145
* operations */
147-
ret = wh_Client_KeyUnwrapAndCache(client, WC_CIPHER_AES_GCM, WH_TEST_KEKID,
148-
wrappedKey, sizeof(wrappedKey),
149-
&wrappedKeyId);
146+
ret = wh_Client_KeyUnwrapAndCache(client, WC_CIPHER_AES_GCM,
147+
WH_DEMO_KEYWRAP_KEKID, wrappedKey,
148+
sizeof(wrappedKey), &wrappedKeyId);
150149
if (ret != 0) {
151150
printf("Failed to wh_Client_KeyUnwrapAndCache %d\n", ret);
152151
goto cleanup_rng;
@@ -207,10 +206,10 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
207206
/* Exporting a wrapped key */
208207

209208
/* Request the server to unwrap and export the wrapped key we created */
210-
ret = wh_Client_KeyUnwrapAndExport(client, WC_CIPHER_AES_GCM, WH_TEST_KEKID,
211-
wrappedKey, sizeof(wrappedKey),
212-
&exportedMetadata, exportedKey,
213-
sizeof(exportedKey));
209+
ret = wh_Client_KeyUnwrapAndExport(client, WC_CIPHER_AES_GCM,
210+
WH_DEMO_KEYWRAP_KEKID, wrappedKey,
211+
sizeof(wrappedKey), &exportedMetadata,
212+
exportedKey, sizeof(exportedKey));
214213
if (ret != 0) {
215214
printf("Failed to wh_Client_KeyUnwrapAndCache %d\n", ret);
216215
goto cleanup_aes;

examples/demo/client/wh_demo_client_keywrap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#include "wolfhsm/wh_client.h"
55

6+
/* Exposed in header so the demo server can obtain the ID for registration */
7+
#define WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID 8
8+
69
int wh_DemoClient_KeyWrap(whClientContext* clientContext);
710

811
#endif /* !DEMO_CLIENT_KEYWRAP_H_ */

examples/posix/wh_posix_client/wh_posix_client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static int wh_ClientTask(void* cf, const char* type, int test)
8585

8686
printf("Client connecting to server...\n");
8787
if (ret == 0 && test) {
88+
printf("Running client demos...\n");
8889
return wh_DemoClient_All(client);
8990
}
9091

examples/posix/wh_posix_server/wh_posix_server.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "wh_posix_cfg.h"
3131
#include "wh_posix_server_cfg.h"
32+
/* For demo wrapped key ID registration */
33+
#include "../../demo/client/wh_demo_client_keywrap.h"
3234

3335
/** Local declarations */
3436
static int wh_ServerTask(void* cf, const char* keyFilePath, int keyId,
@@ -118,6 +120,29 @@ static int loadAndStoreKeys(whServerContext* server, whKeyId* outKeyId,
118120
return ret;
119121
}
120122

123+
static int _InitDemoServer(whServerContext* server, whServerConfig* config)
124+
{
125+
int ret;
126+
127+
ret = wh_Server_Init(server, config);
128+
129+
#ifdef WOLFHSM_CFG_KEYWRAP
130+
if (ret == WH_ERROR_OK) {
131+
/* Register wrapped keys from demo client (wh_demo_client_keywrap.h) */
132+
const whKeyId wrappedIds[] = {WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID};
133+
ret = wh_Server_KeystoreRegisterWrappedKeys(
134+
server, wrappedIds,
135+
(uint16_t)(sizeof(wrappedIds) / sizeof(wrappedIds[0])));
136+
if (ret != WH_ERROR_OK) {
137+
printf("Failed to register wrapped key IDs: %d\n", ret);
138+
(void)wh_Server_Cleanup(server);
139+
}
140+
}
141+
#endif
142+
143+
return ret;
144+
}
145+
121146

122147
static int wh_ServerTask(void* cf, const char* keyFilePath, int keyId,
123148
int clientId)
@@ -132,7 +157,7 @@ static int wh_ServerTask(void* cf, const char* keyFilePath, int keyId,
132157
return -1;
133158
}
134159

135-
ret = wh_Server_Init(server, config);
160+
ret = _InitDemoServer(server, config);
136161

137162
/* Load keys into cache if file path is provided */
138163
if (keyFilePath != NULL) {
@@ -182,7 +207,7 @@ static int wh_ServerTask(void* cf, const char* keyFilePath, int keyId,
182207
(void)wh_Server_Cleanup(server);
183208

184209
/* Reinitialize the server */
185-
ret = wh_Server_Init(server, config);
210+
ret = _InitDemoServer(server, config);
186211
if (ret != 0) {
187212
printf("Failed to reinitialize server: %d\n", ret);
188213
break;

0 commit comments

Comments
 (0)