Skip to content

Commit d374f66

Browse files
committed
Clean up wrap key test code. Fix minor issues with Wrap key server and client code
1 parent 0428166 commit d374f66

File tree

5 files changed

+111
-77
lines changed

5 files changed

+111
-77
lines changed

examples/demo/client/wh_demo_client_wrapkey.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@
4646
#define WH_TEST_AES_WRAPPED_KEYSIZE \
4747
(WH_TEST_AES_AUTHSIZE + WH_TEST_AES_TAGSIZE + WH_TEST_AES_KEYSIZE + \
4848
sizeof(whNvmMetadata))
49+
#define WH_TEST_WRAPKEY_ID 8
4950

5051
int wh_DemoClient_AesGcmWrapKeyBasic(whClientContext* ctx, WC_RNG* rng)
5152
{
52-
53-
5453
int ret = 0;
5554
uint8_t iv[AES_BLOCK_SIZE];
5655
uint8_t key[WH_TEST_AES_KEYSIZE];
@@ -60,7 +59,8 @@ int wh_DemoClient_AesGcmWrapKeyBasic(whClientContext* ctx, WC_RNG* rng)
6059
uint8_t label[WH_NVM_LABEL_LEN] = "Server AES Key Label";
6160
whKeyId serverKeyId;
6261
whKeyId wrappedKeyId;
63-
whNvmMetadata metadata = {.label = "AES Key Label",
62+
whNvmMetadata metadata = {.id = WH_TEST_WRAPKEY_ID,
63+
.label = "AES Key Label",
6464
.access = WH_NVM_ACCESS_ANY,
6565
.len = WH_TEST_AES_KEYSIZE};
6666
whNvmMetadata tmpMetadata;

src/wh_server_keystore.c

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,26 @@ static int _FindInCache(whServerContext* server, whKeyId keyId, int* out_index,
344344
return ret;
345345
}
346346

347+
static bool _ExistsInCache(whServerContext* server, whKeyId keyId)
348+
{
349+
int ret = 0;
350+
int foundIndex = -1;
351+
int foundBigIndex = -1;
352+
whNvmMetadata* tmpMeta;
353+
uint8_t* tmpBuf;
354+
355+
ret = _FindInCache(server, keyId, &foundIndex,
356+
&foundBigIndex, &tmpBuf, &tmpMeta);
357+
358+
if (ret != WH_ERROR_OK) {
359+
/* Key doesn't exist in the cache */
360+
return false;
361+
}
362+
363+
/* Key exists in the cache */
364+
return true;
365+
}
366+
347367
/* try to put the specified key into cache if it isn't already, return pointers
348368
* to meta and the cached data*/
349369
int wh_Server_KeystoreFreshenKey(whServerContext* server, whKeyId keyId,
@@ -532,9 +552,6 @@ int wh_Server_KeystoreEraseKey(whServerContext* server, whNvmId keyId)
532552
}
533553

534554

535-
#define WOLFHSM_WRAPKEY_AES_GCM_TAG_SIZE 16
536-
#define WOLFHSM_WRAPKEY_AES_GCM_IV_SIZE 16
537-
#define WOLFHSM_WRAPKEY_MAX_KEY_SIZE 4096
538555

539556
static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
540557
uint8_t* keyIn, uint16_t keySz,
@@ -543,9 +560,8 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
543560
{
544561
int ret = 0;
545562
Aes aes[1];
546-
WC_RNG rng[1];
547-
uint8_t authTag[WOLFHSM_WRAPKEY_AES_GCM_TAG_SIZE];
548-
uint8_t iv[WOLFHSM_WRAPKEY_AES_GCM_IV_SIZE];
563+
uint8_t authTag[WOLFHSM_CFG_WRAPKEY_AES_GCM_TAG_SIZE];
564+
uint8_t iv[WOLFHSM_CFG_WRAPKEY_AES_GCM_IV_SIZE];
549565
uint8_t serverKey[AES_MAX_KEY_SIZE];
550566
uint32_t serverKeySz = sizeof(serverKey);
551567

@@ -558,12 +574,6 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
558574
sizeof(iv) + sizeof(authTag) + sizeof(*metadataIn) + keySz)
559575
return WH_ERROR_BUFFER_SIZE;
560576

561-
/* Initialize RNG context */
562-
ret = wc_InitRng_ex(rng, NULL, server->crypto->devId);
563-
if (ret != 0) {
564-
return ret;
565-
}
566-
567577
/* Get the server side key */
568578
ret = wh_Server_KeystoreReadKey(
569579
server,
@@ -576,20 +586,18 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
576586
/* Initialize AES context and set it to use the server side key */
577587
ret = wc_AesInit(aes, NULL, server->crypto->devId);
578588
if (ret != 0) {
579-
wc_FreeRng(rng);
580589
return ret;
581590
}
582591

583592
ret = wc_AesGcmSetKey(aes, serverKey, serverKeySz);
584593
if (ret != 0) {
585-
wc_FreeRng(rng);
594+
wc_AesFree(aes);
586595
return ret;
587596
}
588597

589598
/* Generate the IV */
590-
ret = wc_RNG_GenerateBlock(rng, iv, sizeof(iv));
599+
ret = wc_RNG_GenerateBlock(server->crypto->rng, iv, sizeof(iv));
591600
if (ret != 0) {
592-
wc_FreeRng(rng);
593601
wc_AesFree(aes);
594602
return ret;
595603
}
@@ -604,7 +612,6 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
604612
ret = wc_AesGcmEncrypt(aes, encBlob, plainBlob, sizeof(plainBlob), iv,
605613
sizeof(iv), authTag, sizeof(authTag), NULL, 0);
606614
if (ret != 0) {
607-
wc_FreeRng(rng);
608615
wc_AesFree(aes);
609616
return ret;
610617
}
@@ -613,7 +620,6 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
613620
memcpy(wrappedKeyOut, iv, sizeof(iv));
614621
memcpy(wrappedKeyOut + sizeof(iv), authTag, sizeof(authTag));
615622

616-
wc_FreeRng(rng);
617623
wc_AesFree(aes);
618624

619625
return WH_ERROR_OK;
@@ -626,8 +632,8 @@ static int _AesGcmUnwrapKey(whServerContext* server, uint16_t serverKeyId,
626632
{
627633
int ret = 0;
628634
Aes aes[1];
629-
uint8_t authTag[WOLFHSM_WRAPKEY_AES_GCM_TAG_SIZE];
630-
uint8_t iv[WOLFHSM_WRAPKEY_AES_GCM_IV_SIZE];
635+
uint8_t authTag[WOLFHSM_CFG_WRAPKEY_AES_GCM_TAG_SIZE];
636+
uint8_t iv[WOLFHSM_CFG_WRAPKEY_AES_GCM_IV_SIZE];
631637
uint8_t serverKey[AES_MAX_KEY_SIZE];
632638
uint32_t serverKeySz = sizeof(serverKey);
633639
uint8_t* encBlob = (uint8_t*)wrappedKeyIn + sizeof(iv) + sizeof(authTag);
@@ -685,17 +691,15 @@ static int _HandleWrapKeyRequest(whServerContext* server,
685691
whMessageKeystore_WrapResponse* resp,
686692
uint8_t* respData, uint32_t respDataSz)
687693
{
688-
if (server == NULL || req == NULL || reqData == NULL || resp == NULL ||
689-
respData == NULL)
690-
return WH_ERROR_BADARGS;
691-
692-
if (req->keySz > WOLFHSM_WRAPKEY_MAX_KEY_SIZE)
693-
return WH_ERROR_BADARGS;
694694

695695
int ret;
696696
uint8_t* wrappedKey;
697697
whNvmMetadata metadata;
698-
uint8_t key[WOLFHSM_WRAPKEY_MAX_KEY_SIZE];
698+
uint8_t key[WOLFHSM_CFG_WRAPKEY_MAX_KEY_SIZE];
699+
700+
if (server == NULL || req == NULL || reqData == NULL || resp == NULL ||
701+
respData == NULL || req->keySz > WOLFHSM_CFG_WRAPKEY_MAX_KEY_SIZE)
702+
return WH_ERROR_BADARGS;
699703

700704
/* Check if the reqData is big enough to hold the metadata and key */
701705
if (reqDataSz < sizeof(metadata) + req->keySz)
@@ -710,8 +714,8 @@ static int _HandleWrapKeyRequest(whServerContext* server,
710714

711715
switch (req->cipherType) {
712716
case WC_CIPHER_AES_GCM: {
713-
uint16_t wrappedKeySz = WOLFHSM_WRAPKEY_AES_GCM_IV_SIZE +
714-
WOLFHSM_WRAPKEY_AES_GCM_TAG_SIZE +
717+
uint16_t wrappedKeySz = WOLFHSM_CFG_WRAPKEY_AES_GCM_IV_SIZE +
718+
WOLFHSM_CFG_WRAPKEY_AES_GCM_TAG_SIZE +
715719
sizeof(metadata) + req->keySz;
716720

717721
/* Check if the response data can fit the wrapped key */
@@ -766,8 +770,8 @@ static int _HandleUnwrapExportKeyRequest(whServerContext* server,
766770
switch (req->cipherType) {
767771
case WC_CIPHER_AES_GCM: {
768772
uint16_t keySz =
769-
req->wrappedKeySz - WOLFHSM_WRAPKEY_AES_GCM_IV_SIZE -
770-
WOLFHSM_WRAPKEY_AES_GCM_TAG_SIZE - sizeof(*metadata);
773+
req->wrappedKeySz - WOLFHSM_CFG_WRAPKEY_AES_GCM_IV_SIZE -
774+
WOLFHSM_CFG_WRAPKEY_AES_GCM_TAG_SIZE - sizeof(*metadata);
771775

772776
/* Check if the response data can fit the metadata + key */
773777
if (respDataSz < sizeof(*metadata) + keySz)
@@ -780,6 +784,11 @@ static int _HandleUnwrapExportKeyRequest(whServerContext* server,
780784
return ret;
781785
}
782786

787+
/* Check if the key is exportable */
788+
if (metadata->flags & WH_NVM_FLAGS_NONEXPORTABLE) {
789+
return WH_ERROR_ACCESS;
790+
}
791+
783792
/* Tell the client how big the key is */
784793
resp->keySz = keySz;
785794
resp->cipherType = WC_CIPHER_AES_GCM;
@@ -811,7 +820,7 @@ static int _HandleUnwrapCacheKeyRequest(whServerContext* server,
811820
uint8_t* wrappedKey;
812821
whNvmMetadata metadata;
813822
uint16_t keySz;
814-
uint8_t key[WOLFHSM_WRAPKEY_MAX_KEY_SIZE];
823+
uint8_t key[WOLFHSM_CFG_WRAPKEY_MAX_KEY_SIZE];
815824

816825
/* Check if the reqData is big enough to hold the wrapped key */
817826
if (reqDataSz < req->wrappedKeySz)
@@ -824,8 +833,8 @@ static int _HandleUnwrapCacheKeyRequest(whServerContext* server,
824833
switch (req->cipherType) {
825834
case WC_CIPHER_AES_GCM: {
826835
keySz =
827-
req->wrappedKeySz - WOLFHSM_WRAPKEY_AES_GCM_IV_SIZE -
828-
WOLFHSM_WRAPKEY_AES_GCM_TAG_SIZE - sizeof(metadata);
836+
req->wrappedKeySz - WOLFHSM_CFG_WRAPKEY_AES_GCM_IV_SIZE -
837+
WOLFHSM_CFG_WRAPKEY_AES_GCM_TAG_SIZE - sizeof(metadata);
829838

830839
ret = _AesGcmUnwrapKey(server, req->serverKeyId, wrappedKey,
831840
req->wrappedKeySz, &metadata, key, keySz);
@@ -834,6 +843,7 @@ static int _HandleUnwrapCacheKeyRequest(whServerContext* server,
834843
}
835844

836845
resp->cipherType = WC_CIPHER_AES_GCM;
846+
resp->keyId = metadata.id;
837847

838848
} break;
839849
default:
@@ -845,17 +855,12 @@ static int _HandleUnwrapCacheKeyRequest(whServerContext* server,
845855
return WH_ERROR_BADARGS;
846856
}
847857

848-
/* Get a new id if one wasn't provided */
849-
if (WH_KEYID_ISERASED(metadata.id)) {
850-
ret = wh_Server_KeystoreGetUniqueId(server, &metadata.id);
851-
if (ret != WH_ERROR_OK) {
852-
return ret;
853-
}
858+
/* Check if this key already exists in the cache */
859+
if (_ExistsInCache(server, metadata.id)) {
860+
return WH_ERROR_ABORTED;
854861
}
855862

856-
resp->keyId = metadata.id;
857-
858-
/* Write the key */
863+
/* Cache the key */
859864
return wh_Server_KeystoreCacheKey(server, &metadata, key);
860865
}
861866

@@ -1230,7 +1235,7 @@ int wh_Server_HandleKeyRequest(whServerContext* server, uint16_t magic,
12301235
cacheResp.rc = ret;
12311236

12321237
(void)wh_MessageKeystore_TranslateUnwrapCacheResponse(magic, &cacheResp,
1233-
resp_packet);
1238+
resp_packet);
12341239
*out_resp_size = sizeof(cacheResp);
12351240

12361241
} break;

test/wh_test_crypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,8 +2141,8 @@ static int whTestCrypto_Aes(whClientContext* ctx, int devId, WC_RNG* rng)
21412141
WH_ERROR_PRINT("Failed to wc_AesSetIV %d\n",
21422142
ret);
21432143
} else {
2144-
ret = wc_AesCbcDecrypt(aes, plainOut, cipher,
2145-
sizeof(plainIn));
2144+
ret = wc_AesCbcDecrypt(aes, plainOut,
2145+
cipher, sizeof(plainIn));
21462146
if (ret != 0) {
21472147
WH_ERROR_PRINT("Failed to decrypt %d\n",
21482148
ret);

test/wh_test_wrapkey.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646

4747
#ifdef HAVE_AESGCM
4848

49-
static int whTest_Client_AesGcmWrapKey(whClientContext* ctx, int devId,
50-
WC_RNG* rng)
51-
{
5249
#define WH_TEST_AES_KEYSIZE 16
5350
#define WH_TEST_AES_TEXTSIZE 16
5451
#define WH_TEST_AES_AUTHSIZE 16
@@ -57,6 +54,10 @@ static int whTest_Client_AesGcmWrapKey(whClientContext* ctx, int devId,
5754
(WH_TEST_AES_AUTHSIZE + WH_TEST_AES_TAGSIZE + WH_TEST_AES_KEYSIZE + \
5855
sizeof(whNvmMetadata))
5956

57+
static int whTest_Client_AesGcmWrapKey(whClientContext* ctx, int devId,
58+
WC_RNG* rng)
59+
{
60+
6061
int ret = 0;
6162
uint8_t iv[AES_BLOCK_SIZE];
6263
uint8_t key[WH_TEST_AES_KEYSIZE];
@@ -66,7 +67,8 @@ static int whTest_Client_AesGcmWrapKey(whClientContext* ctx, int devId,
6667
uint8_t label[WH_NVM_LABEL_LEN] = "Server AES Key Label";
6768
whKeyId serverKeyId;
6869
whKeyId wrappedKeyId;
69-
whNvmMetadata metadata = {.label = "AES Key Label",
70+
whNvmMetadata metadata = {.id = 8,
71+
.label = "AES Key Label",
7072
.len = WH_TEST_AES_KEYSIZE};
7173
whNvmMetadata tmpMetadata;
7274

@@ -104,22 +106,21 @@ static int whTest_Client_AesGcmWrapKey(whClientContext* ctx, int devId,
104106
return ret;
105107
}
106108

107-
ret = wh_Client_WrapKeyCache(ctx, WC_CIPHER_AES_GCM, serverKeyId, wrappedKey,
108-
sizeof(wrappedKey), &wrappedKeyId);
109+
ret = wh_Client_UnwrapKeyCache(ctx, WC_CIPHER_AES_GCM, serverKeyId, wrappedKey,
110+
sizeof(wrappedKey), &wrappedKeyId);
109111
if (ret != 0) {
110112
printf("Failed to wh_Client_AesGcmWrapKeyCache %d\n", ret);
111113
return ret;
112114
}
113115

114-
ret = wh_Client_UnwrapKey(ctx, WC_CIPHER_AES_GCM, serverKeyId, wrappedKey,
115-
sizeof(wrappedKey), &tmpMetadata,
116-
tmpPlainKey, sizeof(tmpPlainKey));
116+
ret = wh_Client_UnwrapKeyExport(ctx, WC_CIPHER_AES_GCM, serverKeyId, wrappedKey,
117+
sizeof(wrappedKey), &tmpMetadata,
118+
tmpPlainKey, sizeof(tmpPlainKey));
117119
if (ret != 0) {
118120
printf("Failed to wh_Client_AesGcmUnwrapKeyCache %d\n", ret);
119121
return ret;
120122
}
121123

122-
123124
if (memcmp(plainKey, tmpPlainKey, sizeof(plainKey)) != 0) {
124125
printf("AES GCM wrap/unwrap key failed to match\n");
125126
return ret;
@@ -151,21 +152,29 @@ static int whTest_Client_AesWrapKey(whClientContext* ctx, int devId,
151152

152153
#endif /* !NO_AES */
153154

154-
static int whTest_Client_WrapKey(whClientContext* ctx, int devId, WC_RNG* rng)
155+
static int whTest_Client_WrapKey(whClientContext* ctx, int devId)
155156
{
156157
int ret = 0;
158+
WC_RNG rng[1];
159+
160+
ret = wc_InitRng_ex(rng, NULL, devId);
161+
if (ret != 0) {
162+
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
163+
return ret;
164+
}
165+
157166
#ifndef NO_AES
158167
ret = whTest_Client_AesWrapKey(ctx, devId, rng);
159168
#endif
160169

170+
(void)wc_FreeRng(rng);
161171
return ret;
162172
}
163173

164174
int whTest_WrapKeyClientConfig(whClientConfig* config)
165175
{
166176
int ret = 0;
167177
whClientContext client[1] = {0};
168-
WC_RNG rng[1];
169178

170179
if (config == NULL) {
171180
return WH_ERROR_BADARGS;
@@ -175,28 +184,17 @@ int whTest_WrapKeyClientConfig(whClientConfig* config)
175184

176185
ret = wh_Client_CommInit(client, NULL, NULL);
177186
if (ret != 0) {
178-
WH_ERROR_PRINT("Failed to comm init:%d\n", ret);
187+
WH_ERROR_PRINT("Failed to wh_Client_Init %d\n", ret);
179188
(void)wh_Client_Cleanup(client);
180189
return ret;
181190
}
182191

183-
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
184-
if (ret != 0) {
185-
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
186-
(void)wh_Client_CommClose(client);
187-
(void)wh_Client_Cleanup(client);
188-
return ret;
189-
}
190-
191-
ret = whTest_Client_WrapKey(client, WH_DEV_ID, rng);
192+
ret = whTest_Client_WrapKey(client, WH_DEV_ID);
192193
if (ret != 0) {
193194
WH_ERROR_PRINT("Failed to whTest_Client_WrapKey %d\n", ret);
194-
goto cleanup_and_exit;
195195
}
196196

197-
cleanup_and_exit:
198197
/* Clean up used resources */
199-
(void)wc_FreeRng(rng);
200198
(void)wh_Client_CommClose(client);
201199
(void)wh_Client_Cleanup(client);
202200

0 commit comments

Comments
 (0)