@@ -361,6 +361,94 @@ static int _whTest_CryptoEd25519ServerKey(whClientContext* ctx)
361361 return ret ;
362362}
363363
364+ /* Signs and verifies through the wolfCrypt API on a key whose private material
365+ * lives only in the server, so the crypto callback must handle the operation.
366+ * A software fallback has no private scalar and returns BAD_FUNC_ARG. */
367+ static int _whTest_CryptoEd25519CryptoCbHsmKey (whClientContext * ctx )
368+ {
369+ int devId = WH_CLIENT_DEVID (ctx );
370+ int ret = 0 ;
371+ WC_RNG rng [1 ];
372+ ed25519_key key [1 ] = {0 };
373+ ed25519_key pubKey [1 ] = {0 };
374+ whKeyId signKeyId = WH_KEYID_ERASED ;
375+ whKeyId verifyKeyId = WH_KEYID_ERASED ;
376+ byte msg [] = "Ed25519 cryptocb dispatch message" ;
377+ byte sig [ED25519_SIG_SIZE ];
378+ word32 sigSz = sizeof (sig );
379+ int verified = 0 ;
380+ uint8_t label [] = "Ed25519 CryptoCb Key" ;
381+
382+ ret = wc_InitRng_ex (rng , NULL , devId );
383+ if (ret != 0 ) {
384+ WH_ERROR_PRINT ("Failed to wc_InitRng_ex %d\n" , ret );
385+ return ret ;
386+ }
387+
388+ ret = wc_ed25519_init_ex (key , NULL , devId );
389+ if (ret != 0 ) {
390+ WH_ERROR_PRINT ("Failed to initialize Ed25519 key: %d\n" , ret );
391+ (void )wc_FreeRng (rng );
392+ return ret ;
393+ }
394+
395+ ret = wc_ed25519_init_ex (pubKey , NULL , devId );
396+ if (ret != 0 ) {
397+ WH_ERROR_PRINT ("Failed to initialize Ed25519 public key: %d\n" , ret );
398+ wc_ed25519_free (key );
399+ (void )wc_FreeRng (rng );
400+ return ret ;
401+ }
402+
403+ ret = wc_ed25519_make_key (rng , ED25519_KEY_SIZE , key );
404+ if (ret != 0 ) {
405+ WH_ERROR_PRINT ("Failed to generate Ed25519 key: %d\n" , ret );
406+ }
407+ else {
408+ ret = whTest_Ed25519ImportToServer (ctx , devId , key , pubKey , label ,
409+ sizeof (label ), & signKeyId ,
410+ & verifyKeyId );
411+ }
412+
413+ if (ret == 0 ) {
414+ sigSz = sizeof (sig );
415+ ret = wc_ed25519_sign_msg (msg , (word32 )sizeof (msg ), sig , & sigSz , key );
416+ if (ret != 0 ) {
417+ WH_ERROR_PRINT ("wc_ed25519_sign_msg on server key failed: %d\n" ,
418+ ret );
419+ }
420+ }
421+
422+ if (ret == 0 ) {
423+ ret = wc_ed25519_verify_msg (sig , sigSz , msg , (word32 )sizeof (msg ),
424+ & verified , pubKey );
425+ if (ret != 0 ) {
426+ WH_ERROR_PRINT ("wc_ed25519_verify_msg on server key failed: %d\n" ,
427+ ret );
428+ }
429+ else if (verified != 1 ) {
430+ WH_ERROR_PRINT ("Server key Ed25519 signature did not verify\n" );
431+ ret = -1 ;
432+ }
433+ }
434+
435+ if (!WH_KEYID_ISERASED (signKeyId )) {
436+ (void )wh_Client_KeyEvict (ctx , signKeyId );
437+ }
438+ if (!WH_KEYID_ISERASED (verifyKeyId )) {
439+ (void )wh_Client_KeyEvict (ctx , verifyKeyId );
440+ }
441+
442+ if (ret == 0 ) {
443+ WH_TEST_PRINT ("Ed25519 CRYPTOCB DEVID=0x%X SUCCESS\n" , devId );
444+ }
445+
446+ wc_ed25519_free (pubKey );
447+ wc_ed25519_free (key );
448+ (void )wc_FreeRng (rng );
449+ return ret ;
450+ }
451+
364452#ifdef WOLFHSM_CFG_DMA
365453static int _whTest_CryptoEd25519Dma (whClientContext * ctx )
366454{
@@ -731,6 +819,7 @@ int whTest_Crypto_Ed25519(whClientContext* ctx)
731819{
732820 WH_TEST_RETURN_ON_FAIL (_whTest_CryptoEd25519Inline (ctx ));
733821 WH_TEST_RETURN_ON_FAIL (_whTest_CryptoEd25519ServerKey (ctx ));
822+ WH_TEST_RETURN_ON_FAIL (_whTest_CryptoEd25519CryptoCbHsmKey (ctx ));
734823#ifdef WOLFHSM_CFG_DMA
735824 WH_TEST_RETURN_ON_FAIL (_whTest_CryptoEd25519Dma (ctx ));
736825#endif
0 commit comments