@@ -257,6 +257,84 @@ static int _AesGcm_TestDataWrap(whClientContext* client)
257257 return ret ;
258258}
259259
260+ static int _AesGcm_TestKeyUnwrapUnderflow (whClientContext * client )
261+ {
262+ int ret ;
263+ uint8_t dummyBuf [1 ] = {0 };
264+ whNvmMetadata tmpMetadata = {0 };
265+ uint8_t tmpKey [WH_TEST_AES_KEYSIZE ] = {0 };
266+ uint16_t tmpKeySz = sizeof (tmpKey );
267+ whKeyId wrappedKeyId = WH_KEYID_ERASED ;
268+
269+ /* wrappedKeySz=0: must return WH_ERROR_BADARGS, not underflow */
270+ ret = wh_Client_KeyUnwrapAndExport (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
271+ dummyBuf , 0 , & tmpMetadata , tmpKey ,
272+ & tmpKeySz );
273+ if (ret != WH_ERROR_BADARGS ) {
274+ WH_ERROR_PRINT ("KeyUnwrapAndExport(sz=0) expected BADARGS, got %d\n" ,
275+ ret );
276+ return WH_TEST_FAIL ;
277+ }
278+
279+ /* wrappedKeySz=1: must return WH_ERROR_BADARGS, not underflow */
280+ tmpKeySz = sizeof (tmpKey );
281+ ret = wh_Client_KeyUnwrapAndExport (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
282+ dummyBuf , 1 , & tmpMetadata , tmpKey ,
283+ & tmpKeySz );
284+ if (ret != WH_ERROR_BADARGS ) {
285+ WH_ERROR_PRINT ("KeyUnwrapAndExport(sz=1) expected BADARGS, got %d\n" ,
286+ ret );
287+ return WH_TEST_FAIL ;
288+ }
289+
290+ /* wrappedKeySz=0: test KeyUnwrapAndCache path */
291+ ret = wh_Client_KeyUnwrapAndCache (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
292+ dummyBuf , 0 , & wrappedKeyId );
293+ if (ret != WH_ERROR_BADARGS ) {
294+ WH_ERROR_PRINT ("KeyUnwrapAndCache(sz=0) expected BADARGS, got %d\n" ,
295+ ret );
296+ return WH_TEST_FAIL ;
297+ }
298+
299+ /* wrappedKeySz=1: test KeyUnwrapAndCache path */
300+ ret = wh_Client_KeyUnwrapAndCache (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
301+ dummyBuf , 1 , & wrappedKeyId );
302+ if (ret != WH_ERROR_BADARGS ) {
303+ WH_ERROR_PRINT ("KeyUnwrapAndCache(sz=1) expected BADARGS, got %d\n" ,
304+ ret );
305+ return WH_TEST_FAIL ;
306+ }
307+
308+ return WH_ERROR_OK ;
309+ }
310+
311+ static int _AesGcm_TestDataUnwrapUnderflow (whClientContext * client )
312+ {
313+ int ret ;
314+ uint8_t dummyBuf [1 ] = {0 };
315+ uint8_t outBuf [32 ] = {0 };
316+ uint32_t outSz = sizeof (outBuf );
317+
318+ /* wrappedDataSz=0: must return WH_ERROR_BADARGS, not underflow */
319+ ret = wh_Client_DataUnwrap (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
320+ dummyBuf , 0 , outBuf , & outSz );
321+ if (ret != WH_ERROR_BADARGS ) {
322+ WH_ERROR_PRINT ("DataUnwrap(sz=0) expected BADARGS, got %d\n" , ret );
323+ return WH_TEST_FAIL ;
324+ }
325+
326+ /* wrappedDataSz=1: must return WH_ERROR_BADARGS, not underflow */
327+ outSz = sizeof (outBuf );
328+ ret = wh_Client_DataUnwrap (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
329+ dummyBuf , 1 , outBuf , & outSz );
330+ if (ret != WH_ERROR_BADARGS ) {
331+ WH_ERROR_PRINT ("DataUnwrap(sz=1) expected BADARGS, got %d\n" , ret );
332+ return WH_TEST_FAIL ;
333+ }
334+
335+ return WH_ERROR_OK ;
336+ }
337+
260338#endif /* HAVE_AESGCM */
261339
262340int whTest_Client_KeyWrap (whClientContext * client )
@@ -281,6 +359,14 @@ int whTest_Client_KeyWrap(whClientContext* client)
281359 if (ret != WH_ERROR_OK ) {
282360 WH_ERROR_PRINT ("Failed to _AesGcm_TestKeyWrap %d\n" , ret );
283361 }
362+
363+ if (ret == WH_ERROR_OK ) {
364+ ret = _AesGcm_TestKeyUnwrapUnderflow (client );
365+ if (ret != WH_ERROR_OK ) {
366+ WH_ERROR_PRINT ("Failed to _AesGcm_TestKeyUnwrapUnderflow %d\n" ,
367+ ret );
368+ }
369+ }
284370#endif
285371
286372 _CleanupServerKek (client );
@@ -304,6 +390,14 @@ int whTest_Client_DataWrap(whClientContext* client)
304390 if (ret != WH_ERROR_OK ) {
305391 WH_ERROR_PRINT ("Failed to _AesGcm_TestDataWrap %d\n" , ret );
306392 }
393+
394+ if (ret == WH_ERROR_OK ) {
395+ ret = _AesGcm_TestDataUnwrapUnderflow (client );
396+ if (ret != WH_ERROR_OK ) {
397+ WH_ERROR_PRINT ("Failed to _AesGcm_TestDataUnwrapUnderflow %d\n" ,
398+ ret );
399+ }
400+ }
307401#endif
308402
309403 _CleanupServerKek (client );
0 commit comments