@@ -804,6 +804,7 @@ static void wolfBoot_verify_signature_ml_dsa(uint8_t key_slot,
804804 struct wolfBoot_image * img , uint8_t * sig )
805805{
806806 int ret = 0 ;
807+ int key_inited = 0 ;
807808 wc_MlDsaKey ml_dsa ;
808809#if !defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT ) && \
809810 !defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER )
@@ -831,8 +832,9 @@ static void wolfBoot_verify_signature_ml_dsa(uint8_t key_slot,
831832 if (ret != 0 ) {
832833 wolfBoot_printf ("error: wc_MlDsaKey_Init returned %d\n" , ret );
833834 }
835+ else {
836+ key_inited = 1 ;
834837
835- if (ret == 0 ) {
836838 /* Set the ML-DSA security level. */
837839 ret = wc_MlDsaKey_SetParams (& ml_dsa , ML_DSA_LEVEL );
838840
@@ -847,32 +849,35 @@ static void wolfBoot_verify_signature_ml_dsa(uint8_t key_slot,
847849 defined(WOLFBOOT_CERT_CHAIN_VERIFY ))
848850 /* Use the public key ID directly with wolfHSM (no local keystore) */
849851 (void )key_slot ;
852+ if (ret == 0 ) {
850853#if defined(WOLFBOOT_CERT_CHAIN_VERIFY )
851- /* If using certificate chain verification and we have a verified leaf key
852- * ID */
853- if (g_leafKeyIdValid ) {
854- /* Use the leaf key ID from certificate verification */
855- #if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT )
856- ret = wh_Client_MlDsaSetKeyId (& ml_dsa , g_certLeafKeyId );
857- #elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER )
858- ret = wh_Server_MlDsaKeyCacheExport (& hsmServerCtx , g_certLeafKeyId ,
859- & ml_dsa );
860- #endif
861- wolfBoot_printf (
862- "Using leaf cert public key (ID: %08x) for ML-DSA verification\n" ,
863- (unsigned int )g_certLeafKeyId );
864- }
865- else {
866- /* Default behavior: use the pre-configured public key ID */
867- #if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT )
868- ret = wh_Client_MlDsaSetKeyId (& ml_dsa , hsmKeyIdPubKey );
869- #endif
870- }
854+ /* If using certificate chain verification and we have a verified leaf
855+ * key ID */
856+ if (g_leafKeyIdValid ) {
857+ /* Use the leaf key ID from certificate verification */
858+ #if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT )
859+ ret = wh_Client_MlDsaSetKeyId (& ml_dsa , g_certLeafKeyId );
860+ #elif defined(WOLFBOOT_ENABLE_WOLFHSM_SERVER )
861+ ret = wh_Server_MlDsaKeyCacheExport (& hsmServerCtx , g_certLeafKeyId ,
862+ & ml_dsa );
863+ #endif
864+ wolfBoot_printf (
865+ "Using leaf cert public key (ID: %08x) for ML-DSA "
866+ "verification\n" ,
867+ (unsigned int )g_certLeafKeyId );
868+ }
869+ else {
870+ /* Default behavior: use the pre-configured public key ID */
871+ #if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT )
872+ ret = wh_Client_MlDsaSetKeyId (& ml_dsa , hsmKeyIdPubKey );
873+ #endif
874+ }
871875#else
872- ret = wh_Client_MlDsaSetKeyId (& ml_dsa , hsmKeyIdPubKey );
876+ ret = wh_Client_MlDsaSetKeyId (& ml_dsa , hsmKeyIdPubKey );
873877#endif
874- if (ret != 0 ) {
875- wolfBoot_printf ("error: ML-DSA set key ID returned %d\n" , ret );
878+ if (ret != 0 ) {
879+ wolfBoot_printf ("error: ML-DSA set key ID returned %d\n" , ret );
880+ }
876881 }
877882#else
878883 /* Make sure pub key matches parameters and import it */
@@ -969,7 +974,9 @@ static void wolfBoot_verify_signature_ml_dsa(uint8_t key_slot,
969974 }
970975#endif /* WOLFBOOT_CERT_CHAIN_VERIFY && WOLFHSM */
971976
972- wc_MlDsaKey_Free (& ml_dsa );
977+ if (key_inited ) {
978+ wc_MlDsaKey_Free (& ml_dsa );
979+ }
973980}
974981
975982#endif /* WOLFBOOT_SIGN_ML_DSA */
0 commit comments