@@ -1357,3 +1357,96 @@ int wh_DemoClient_CryptoCmacOneshotImport(whClientContext* clientContext)
13571357 return ret ;
13581358}
13591359#endif /* WOLFSSL_CMAC && !NO_AES */
1360+
1361+ #if defined(HAVE_HKDF )
1362+ /*
1363+ * Demonstrates deriving key material using HKDF and exporting it directly to
1364+ * the client with the wolfCrypt API. This example provides the HKDF operation
1365+ * with input keying material (IKM), optional salt, and info. The derived output
1366+ * key material (OKM) is produced on the HSM and returned to the client buffer.
1367+ *
1368+ * After deriving the OKM, you can use it as a symmetric key (for example as an
1369+ * AES key) or application-specific secret.
1370+ */
1371+ int wh_DemoClient_CryptoHkdfExport (whClientContext * clientContext )
1372+ {
1373+ int ret = 0 ;
1374+ int devId = WH_DEV_ID ;
1375+ /* Example inputs for HKDF. The data mirrors sizes from RFC 5869 test case 1
1376+ */
1377+ const byte ikm [] = {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
1378+ 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F };
1379+ const byte salt [] = {0xA0 , 0xA1 , 0xA2 , 0xA3 };
1380+ const byte info [] = {0xB0 , 0xB1 , 0xB2 , 0xB3 , 0xB4 };
1381+ byte okm [32 ]; /* 32 bytes for WC_SHA256-based HKDF */
1382+
1383+ (void )clientContext ; /* Unused in the export form */
1384+
1385+ /* Derive 32 bytes using HKDF with SHA-256. The OKM is exported directly
1386+ * back to the client buffer 'okm'. */
1387+ ret = wc_HKDF_ex (WC_SHA256 , ikm , sizeof (ikm ), salt , sizeof (salt ), info ,
1388+ sizeof (info ), okm , (word32 )sizeof (okm ), NULL , devId );
1389+ if (ret != 0 ) {
1390+ printf ("Failed to wc_HKDF_ex %d\n" , ret );
1391+ }
1392+
1393+ /* At this point 'okm' holds the derived key material.
1394+ * Now you can use the key for your application. */
1395+
1396+ return ret ;
1397+ }
1398+
1399+ /*
1400+ * Demonstrates deriving key material using HKDF and storing it in the HSM
1401+ * key cache. The client does not receive the key material; instead, it gets a
1402+ * keyId that can be used with compatible operations (for example, attaching
1403+ * the keyId to an AES context).
1404+ *
1405+ * After the key is cached, you can reference it by keyId for relevant
1406+ * operations. This example optionally evicts the cached key at the end.
1407+ */
1408+ int wh_DemoClient_CryptoHkdfCache (whClientContext * clientContext )
1409+ {
1410+ int ret = 0 ;
1411+ int needEvict = 0 ;
1412+ whKeyId keyId = WH_KEYID_ERASED ;
1413+ /* Example inputs for HKDF. */
1414+ const byte ikm [] = {0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 ,
1415+ 0x18 , 0x19 , 0x1A , 0x1B , 0x1C , 0x1D , 0x1E , 0x1F };
1416+ const byte salt [] = {0xC0 , 0xC1 , 0xC2 , 0xC3 };
1417+ const byte info [] = {0xD0 , 0xD1 , 0xD2 };
1418+ const uint32_t outSz = 32 ; /* arbitrary output size */
1419+ /* Metadata flags/label for the cached key. Adjust to your requirements. */
1420+ whNvmFlags flags = WH_NVM_FLAGS_NONE ;
1421+ char label [] = "hkdf-derived key" ;
1422+
1423+ /* Request the HSM to derive HKDF output and store it in the key cache.
1424+ * On success, 'keyId' is assigned by the HSM (unless pre-set) and can be
1425+ * used to reference the cached key material. */
1426+ ret = wh_Client_HkdfMakeCacheKey (
1427+ clientContext , WC_SHA256 , ikm , (uint32_t )sizeof (ikm ), salt ,
1428+ (uint32_t )sizeof (salt ), info , (uint32_t )sizeof (info ), & keyId , flags ,
1429+ (const uint8_t * )label , (uint32_t )strlen (label ), outSz );
1430+ if (ret != WH_ERROR_OK ) {
1431+ printf ("Failed to wh_Client_HkdfMakeCacheKey %d\n" , ret );
1432+ goto exit ;
1433+ }
1434+
1435+ needEvict = 1 ;
1436+
1437+ /* Now you can use the cached key, referring to it by ID for relevant
1438+ * operations. */
1439+
1440+ exit :
1441+ if (needEvict ) {
1442+ int evictRet = wh_Client_KeyEvict (clientContext , keyId );
1443+ if (evictRet != 0 ) {
1444+ printf ("Failed to wh_Client_KeyEvict %d\n" , evictRet );
1445+ if (ret == 0 ) {
1446+ ret = evictRet ;
1447+ }
1448+ }
1449+ }
1450+ return ret ;
1451+ }
1452+ #endif /* HAVE_HKDF */
0 commit comments