@@ -884,7 +884,7 @@ void SoftHSM::prepareSupportedMechanisms(std::map<std::string, CK_MECHANISM_TYPE
884884 else
885885 supportedMechanisms.remove (mechanism);
886886 }
887- catch (const std::out_of_range& e )
887+ catch (const std::out_of_range&)
888888 {
889889 WARNING_MSG (" Unknown mechanism provided: %s" , token.c_str ());
890890 }
@@ -4204,6 +4204,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
42044204#endif
42054205#ifdef WITH_ML_DSA
42064206 bool isMLDSA = false ;
4207+ SIGN_ADDITIONAL_CONTEXT additionalContext = {};
42074208#endif
42084209 switch (pMechanism->mechanism ) {
42094210 case CKM_RSA_PKCS:
@@ -4476,6 +4477,51 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
44764477 mechanism = AsymMech::MLDSA;
44774478 bAllowMultiPartOp = false ;
44784479 isMLDSA = true ;
4480+ if (pMechanism->pParameter != NULL_PTR) {
4481+ if (pMechanism->ulParameterLen != sizeof (CK_SIGN_ADDITIONAL_CONTEXT))
4482+ {
4483+ ERROR_MSG (" Invalid parameters" );
4484+ return CKR_ARGUMENTS_BAD;
4485+ }
4486+ else
4487+ {
4488+ const CK_SIGN_ADDITIONAL_CONTEXT* ckSignAdditionalContext = (const CK_SIGN_ADDITIONAL_CONTEXT*) pMechanism->pParameter ;
4489+ if (ckSignAdditionalContext->ulContextLen > 255 )
4490+ {
4491+ ERROR_MSG (" ML-DSA: Invalid parameters, context length > 255" );
4492+ return CKR_ARGUMENTS_BAD;
4493+ }
4494+
4495+ // Always initialize context fields
4496+ additionalContext.contextAsChar = NULL ;
4497+ additionalContext.contextLength = 0 ;
4498+ if (ckSignAdditionalContext->ulContextLen > 0 )
4499+ {
4500+ if (ckSignAdditionalContext->pContext == NULL )
4501+ {
4502+ ERROR_MSG (" ML-DSA: Invalid parameters, pContext is NULL" );
4503+ return CKR_ARGUMENTS_BAD;
4504+ }
4505+ additionalContext.contextAsChar = (unsigned char *) ckSignAdditionalContext->pContext ;
4506+ additionalContext.contextLength = ckSignAdditionalContext->ulContextLen ;
4507+ }
4508+ switch (ckSignAdditionalContext->hedgeVariant ) {
4509+ case CKH_HEDGE_REQUIRED:
4510+ additionalContext.hedgeType = Hedge::HEDGE_REQUIRED;
4511+ break ;
4512+ case CKH_DETERMINISTIC_REQUIRED:
4513+ additionalContext.hedgeType = Hedge::DETERMINISTIC_REQUIRED;
4514+ break ;
4515+ case CKH_HEDGE_PREFERRED:
4516+ // Per PKCS11v3.2 section 6.67.5
4517+ // "If no parameter is supplied the hedgeVariant will be CKH_HEDGE_PREFERRED"
4518+ default :
4519+ additionalContext.hedgeType = Hedge::HEDGE_PREFERRED;
4520+ }
4521+ param = &additionalContext;
4522+ paramLen = sizeof (SIGN_ADDITIONAL_CONTEXT);
4523+ }
4524+ }
44794525 break ;
44804526#endif
44814527 default :
@@ -5238,6 +5284,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
52385284#endif
52395285#ifdef WITH_ML_DSA
52405286 bool isMLDSA = false ;
5287+ SIGN_ADDITIONAL_CONTEXT additionalContext = {};
52415288#endif
52425289 switch (pMechanism->mechanism ) {
52435290 case CKM_RSA_PKCS:
@@ -5508,6 +5555,49 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
55085555 mechanism = AsymMech::MLDSA;
55095556 bAllowMultiPartOp = false ;
55105557 isMLDSA = true ;
5558+ if (pMechanism->pParameter != NULL_PTR) {
5559+ if (pMechanism->ulParameterLen != sizeof (CK_SIGN_ADDITIONAL_CONTEXT))
5560+ {
5561+ ERROR_MSG (" Invalid parameters" );
5562+ return CKR_ARGUMENTS_BAD;
5563+ }
5564+ else
5565+ {
5566+ const CK_SIGN_ADDITIONAL_CONTEXT* ckSignAdditionalContext = (const CK_SIGN_ADDITIONAL_CONTEXT*) pMechanism->pParameter ;
5567+ if (ckSignAdditionalContext->ulContextLen > 255 ) {
5568+ ERROR_MSG (" ML-DSA: Invalid parameters, context length > 255" );
5569+ return CKR_ARGUMENTS_BAD;
5570+ }
5571+ // Always initialize context fields
5572+ additionalContext.contextAsChar = NULL ;
5573+ additionalContext.contextLength = 0 ;
5574+ if (ckSignAdditionalContext->ulContextLen > 0 ) {
5575+ if (ckSignAdditionalContext->pContext == NULL )
5576+ {
5577+ ERROR_MSG (" ML-DSA: Invalid parameters, pContext is NULL" );
5578+ return CKR_ARGUMENTS_BAD;
5579+ }
5580+ additionalContext.contextAsChar = (unsigned char *) ckSignAdditionalContext->pContext ;
5581+ additionalContext.contextLength = ckSignAdditionalContext->ulContextLen ;
5582+ }
5583+
5584+ switch (ckSignAdditionalContext->hedgeVariant ) {
5585+ case CKH_HEDGE_REQUIRED:
5586+ additionalContext.hedgeType = Hedge::HEDGE_REQUIRED;
5587+ break ;
5588+ case CKH_DETERMINISTIC_REQUIRED:
5589+ additionalContext.hedgeType = Hedge::DETERMINISTIC_REQUIRED;
5590+ break ;
5591+ // Per PKCS11v3.2 section 6.67.5
5592+ // "If no parameter is supplied the hedgeVariant will be CKH_HEDGE_PREFERRED"
5593+ case CKH_HEDGE_PREFERRED:
5594+ default :
5595+ additionalContext.hedgeType = Hedge::HEDGE_PREFERRED;
5596+ }
5597+ param = &additionalContext;
5598+ paramLen = sizeof (SIGN_ADDITIONAL_CONTEXT);
5599+ }
5600+ }
55115601 break ;
55125602#endif
55135603 default :
@@ -10283,7 +10373,6 @@ CK_RV SoftHSM::generateMLDSA
1028310373 bOK = bOK && osobject->setAttribute (CKA_NEVER_EXTRACTABLE, bNeverExtractable);
1028410374
1028510375 // MLDSA Private Key Attributes
10286- ByteString parameterSet;
1028710376 ByteString value;
1028810377 ByteString seed;
1028910378 if (isPrivateKeyPrivate)
0 commit comments