Skip to content

Commit c87d7e3

Browse files
committed
keystore: invert check and avoid a nested if level, typos
1 parent ea1ae4f commit c87d7e3

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

src/wh_server_keystore.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,9 @@ static int _KeyIsCommitted(whServerContext* server, whKeyId keyId)
100100

101101
whKeyCacheContext* ctx = _GetCacheContext(server, keyId);
102102
ret = _FindInCache(server, keyId, &index, &big, NULL, NULL);
103-
if (ret == WH_ERROR_NOTFOUND) {
103+
if (ret != WH_ERROR_OK) {
104104
return 0;
105105
}
106-
else if (ret != WH_ERROR_OK) {
107-
return ret;
108-
}
109106

110107
if (big == 0) {
111108
return ctx->cache[index].committed;
@@ -394,7 +391,6 @@ static int _EvictKeyFromCache(whKeyCacheContext* ctx, whKeyId keyId)
394391
whNvmMetadata* meta = NULL;
395392
uint8_t* outBuffer = NULL;
396393

397-
398394
int ret = _FindInKeyCache(ctx, keyId, NULL, NULL, &outBuffer, &meta);
399395

400396
if (ret == WH_ERROR_OK && meta != NULL) {
@@ -482,7 +478,7 @@ int wh_Server_KeystoreGetUniqueId(whServerContext* server, whNvmId* inout_id)
482478
return WH_ERROR_OK;
483479
}
484480

485-
/* find a slot to cache a key. If key is already there, is evicetd first */
481+
/* find a slot to cache a key. If key is already there, is evicted first */
486482
int wh_Server_KeystoreGetCacheSlot(whServerContext* server, whKeyId keyId,
487483
uint16_t keySz, uint8_t** outBuf,
488484
whNvmMetadata** outMeta)
@@ -634,32 +630,36 @@ int wh_Server_KeystoreFreshenKey(whServerContext* server, whKeyId keyId,
634630

635631
ret = _FindInCache(server, keyId, &foundIndex, &foundBigIndex, cacheBufOut,
636632
cacheMetaOut);
637-
if (ret == WH_ERROR_NOTFOUND) {
638-
/* For wrapped keys, just probe the cache and error if not found. We
639-
* don't support automatically unwrapping and caching outside of the
640-
* keywrap API */
641-
if (WH_KEYID_TYPE(keyId) == WH_KEYTYPE_WRAPPED) {
642-
return WH_ERROR_NOTFOUND;
643-
}
633+
if (ret != WH_ERROR_NOTFOUND) {
634+
return ret;
635+
}
644636

645-
/* Not in cache. Check if it is in NVM */
646-
ret = wh_Nvm_GetMetadata(server->nvm, keyId, tmpMeta);
637+
/* key not in the cache */
638+
639+
/* For wrapped keys, just probe the cache and error if not found. We
640+
* don't support automatically unwrapping and caching outside of the
641+
* keywrap API */
642+
if (WH_KEYID_TYPE(keyId) == WH_KEYTYPE_WRAPPED) {
643+
return WH_ERROR_NOTFOUND;
644+
}
645+
646+
/* Not in cache. Check if it is in NVM */
647+
ret = wh_Nvm_GetMetadata(server->nvm, keyId, tmpMeta);
648+
if (ret == WH_ERROR_OK) {
649+
/* Key found in NVM, get a free cache slot */
650+
ret = wh_Server_KeystoreGetCacheSlot(server, keyId, tmpMeta->len,
651+
cacheBufOut, cacheMetaOut);
647652
if (ret == WH_ERROR_OK) {
648-
/* Key found in NVM, get a free cache slot */
649-
ret = wh_Server_KeystoreGetCacheSlot(server, keyId, tmpMeta->len,
650-
cacheBufOut, cacheMetaOut);
653+
/* Read the key from NVM into the cache slot */
654+
ret = wh_Nvm_Read(server->nvm, keyId, 0, tmpMeta->len,
655+
*cacheBufOut);
651656
if (ret == WH_ERROR_OK) {
652-
/* Read the key from NVM into the cache slot */
653-
ret = wh_Nvm_Read(server->nvm, keyId, 0, tmpMeta->len,
654-
*cacheBufOut);
655-
if (ret == WH_ERROR_OK) {
656-
/* Copy the metadata to the cache slot if key read is
657-
* successful*/
658-
memcpy((uint8_t*)*cacheMetaOut, (uint8_t*)tmpMeta,
659-
sizeof(whNvmMetadata));
660-
_MarkKeyCommitted(_GetCacheContext(server, keyId), keyId,
661-
1);
662-
}
657+
/* Copy the metadata to the cache slot if key read is
658+
* successful*/
659+
memcpy((uint8_t*)*cacheMetaOut, (uint8_t*)tmpMeta,
660+
sizeof(whNvmMetadata));
661+
_MarkKeyCommitted(_GetCacheContext(server, keyId), keyId,
662+
1);
663663
}
664664
}
665665
}

wolfhsm/wh_server_keystore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ int wh_Server_KeystoreCacheKeyDma(whServerContext* server, whNvmMetadata* meta,
225225
uint64_t keyAddr);
226226

227227
/**
228-
* @brief cache a key with DMA after policy enforcement
228+
* @brief Cache a key with DMA after policy enforcement
229229
*
230230
* Performs policy checks before exporting a key via DMA.
231231
*/

0 commit comments

Comments
 (0)