@@ -947,7 +947,7 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
947947 memset (bigKey , 0xBB , sizeof (bigKey ));
948948
949949 /* Test 1: Cache small key first, then cache same keyId with big key */
950- keyId = 0x1000 ; /* Use specific keyId to ensure we control the ID */
950+ keyId = WH_KEYID_ERASED ;
951951 ret = wh_Client_KeyCache (ctx , 0 , labelSmall , sizeof (labelSmall ),
952952 smallKey , sizeof (smallKey ), & keyId );
953953 if (ret != 0 ) {
@@ -964,11 +964,12 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
964964 }
965965 else {
966966 /* Verify the cached key is the big key by exporting it */
967+ exportedKeySize = sizeof (exportedKey );
967968 ret = wh_Client_KeyExport (ctx , keyId , exportedLabel ,
968969 sizeof (exportedLabel ), exportedKey ,
969970 & exportedKeySize );
970971 if (ret != 0 ) {
971- WH_ERROR_PRINT ("Failed to export key after eviction : %d\n" ,
972+ WH_ERROR_PRINT ("Failed to export key after cache : %d\n" ,
972973 ret );
973974 }
974975 else {
@@ -991,10 +992,17 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
991992 }
992993 }
993994 /* Clean up */
994- ret = wh_Client_KeyEvict (ctx , keyId );
995- if (ret != 0 ) {
996- WH_ERROR_PRINT ("Failed to evict key: %d\n" , ret );
995+ if (ret == 0 ) {
996+ ret = wh_Client_KeyEvict (ctx , keyId );
997+ if (ret != 0 ) {
998+ WH_ERROR_PRINT ("Failed to evict key: %d\n" , ret );
999+ }
9971000 }
1001+ else {
1002+ /* On error, try our best to clean up */
1003+ (void )wh_Client_KeyEvict (ctx , keyId );
1004+ }
1005+
9981006 if (ret == 0 ) {
9991007 ret = wh_Client_KeyEvict (ctx , keyId );
10001008 if (ret != 0 ) {
@@ -1012,7 +1020,7 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
10121020
10131021 /* Test 2: Cache big key first, then cache same keyId with small key */
10141022 if (ret == 0 ) {
1015- keyId = 0x2000 ; /* Use different keyId */
1023+ keyId = WH_KEYID_ERASED ;
10161024 ret = wh_Client_KeyCache (ctx , 0 , labelBig , sizeof (labelBig ), bigKey ,
10171025 sizeof (bigKey ), & keyId );
10181026 if (ret != 0 ) {
@@ -1030,12 +1038,13 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
10301038 }
10311039 else {
10321040 /* Verify the cached key is the small key by exporting it */
1041+ exportedKeySize = sizeof (exportedKey );
10331042 ret = wh_Client_KeyExport (ctx , keyId , exportedLabel ,
10341043 sizeof (exportedLabel ),
10351044 exportedKey , & exportedKeySize );
10361045 if (ret != 0 ) {
10371046 WH_ERROR_PRINT (
1038- "Failed to export key after eviction : %d\n" , ret );
1047+ "Failed to export key after cache : %d\n" , ret );
10391048 }
10401049 else {
10411050 /* Verify exported key matches the small key */
@@ -1057,10 +1066,17 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
10571066 }
10581067 }
10591068 /* Clean up */
1060- ret = wh_Client_KeyEvict (ctx , keyId );
1061- if (ret != 0 ) {
1062- WH_ERROR_PRINT ("Failed to evict key: %d\n" , ret );
1069+ if (ret == 0 ) {
1070+ ret = wh_Client_KeyEvict (ctx , keyId );
1071+ if (ret != 0 ) {
1072+ WH_ERROR_PRINT ("Failed to evict key: %d\n" , ret );
1073+ }
1074+ }
1075+ else {
1076+ /* On error, try our best to clean up */
1077+ (void )wh_Client_KeyEvict (ctx , keyId );
10631078 }
1079+
10641080 if (ret == 0 ) {
10651081 ret = wh_Client_KeyEvict (ctx , keyId );
10661082 if (ret != 0 ) {
@@ -1128,7 +1144,7 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
11281144
11291145 /* Test 1: Cache small key with DMA first, then cache same keyId
11301146 * with big key using DMA */
1131- keyId = 0x3000 ;
1147+ keyId = WH_KEYID_ERASED ;
11321148 ret = wh_Client_KeyCacheDma (ctx , 0 , labelSmall , sizeof (labelSmall ),
11331149 smallKey , sizeof (smallKey ), & keyId );
11341150 if (ret != 0 ) {
@@ -1148,10 +1164,10 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
11481164 /* Verify the cached key is the big key by exporting it */
11491165 exportedKeySize = bigKeySize ;
11501166 ret = wh_Client_KeyExportDma (
1151- ctx , keyId , exportedKey , exportedKeySize , exportedLabel ,
1152- sizeof (exportedLabel ), & exportedKeySize );
1167+ ctx , keyId , exportedKey , exportedKeySize , exportedLabel ,
1168+ sizeof (exportedLabel ), & exportedKeySize );
11531169 if (ret != 0 ) {
1154- WH_ERROR_PRINT ("Failed to export key after eviction : %d\n" ,
1170+ WH_ERROR_PRINT ("Failed to export key after cache : %d\n" ,
11551171 ret );
11561172 }
11571173 else {
@@ -1174,10 +1190,17 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
11741190 }
11751191 }
11761192 /* Clean up */
1177- ret = wh_Client_KeyEvict (ctx , keyId );
1178- if (ret != 0 ) {
1179- WH_ERROR_PRINT ("Failed to evict key: %d\n" , ret );
1193+ if (ret == 0 ) {
1194+ ret = wh_Client_KeyEvict (ctx , keyId );
1195+ if (ret != 0 ) {
1196+ WH_ERROR_PRINT ("Failed to evict key: %d\n" , ret );
1197+ }
11801198 }
1199+ else {
1200+ /* On error, try our best to clean up */
1201+ (void )wh_Client_KeyEvict (ctx , keyId );
1202+ }
1203+
11811204 if (ret == 0 ) {
11821205 ret = wh_Client_KeyEvict (ctx , keyId );
11831206 if (ret != 0 ) {
@@ -1196,7 +1219,7 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
11961219 /* Test 2: Cache big key with DMA first, then cache
11971220 * same keyId with small key using DMA */
11981221 if (ret == 0 ) {
1199- keyId = 0x4000 ; /* Use different keyId */
1222+ keyId = WH_KEYID_ERASED ;
12001223 ret = wh_Client_KeyCacheDma (ctx , 0 , labelBig , sizeof (labelBig ),
12011224 bigKey , sizeof (bigKey ), & keyId );
12021225 if (ret != 0 ) {
@@ -1217,11 +1240,11 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
12171240 /* Verify the cached key is the small key by exporting it */
12181241 exportedKeySize = smallKeySize ;
12191242 ret = wh_Client_KeyExportDma (
1220- ctx , keyId , exportedKey , exportedKeySize , exportedLabel ,
1221- sizeof (exportedLabel ), & exportedKeySize );
1243+ ctx , keyId , exportedKey , exportedKeySize , exportedLabel ,
1244+ sizeof (exportedLabel ), & exportedKeySize );
12221245 if (ret != 0 ) {
12231246 WH_ERROR_PRINT (
1224- "Failed to export key after eviction : %d\n" , ret );
1247+ "Failed to export key after cache : %d\n" , ret );
12251248 }
12261249 else {
12271250 /* Verify exported key matches the small key */
@@ -1243,10 +1266,17 @@ static int whTest_KeyCache(whClientContext* ctx, int devId, WC_RNG* rng)
12431266 }
12441267 }
12451268 /* Clean up */
1246- ret = wh_Client_KeyEvict (ctx , keyId );
1247- if (ret != 0 ) {
1248- WH_ERROR_PRINT ("Failed to evict key: %d\n" , ret );
1269+ if (ret == 0 ) {
1270+ ret = wh_Client_KeyEvict (ctx , keyId );
1271+ if (ret != 0 ) {
1272+ WH_ERROR_PRINT ("Failed to evict key: %d\n" , ret );
1273+ }
1274+ }
1275+ else {
1276+ /* On error, try our best to clean up */
1277+ (void )wh_Client_KeyEvict (ctx , keyId );
12491278 }
1279+
12501280 if (ret == 0 ) {
12511281 ret = wh_Client_KeyEvict (ctx , keyId );
12521282 if (ret != 0 ) {
0 commit comments