@@ -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 */
486482int 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 }
0 commit comments