@@ -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*/
349369int 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
539556static 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 ;
0 commit comments