@@ -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 }
@@ -4195,6 +4195,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
41954195#endif
41964196#ifdef WITH_ML_DSA
41974197 bool isMLDSA = false ;
4198+ SIGN_ADDITIONAL_CONTEXT additionalContext = {};
41984199#endif
41994200 switch (pMechanism->mechanism ) {
42004201 case CKM_RSA_PKCS:
@@ -4467,6 +4468,51 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
44674468 mechanism = AsymMech::MLDSA;
44684469 bAllowMultiPartOp = false ;
44694470 isMLDSA = true ;
4471+ if (pMechanism->pParameter != NULL_PTR) {
4472+ if (pMechanism->ulParameterLen != sizeof (CK_SIGN_ADDITIONAL_CONTEXT))
4473+ {
4474+ ERROR_MSG (" Invalid parameters" );
4475+ return CKR_ARGUMENTS_BAD;
4476+ }
4477+ else
4478+ {
4479+ const CK_SIGN_ADDITIONAL_CONTEXT* ckSignAdditionalContext = (const CK_SIGN_ADDITIONAL_CONTEXT*) pMechanism->pParameter ;
4480+ if (ckSignAdditionalContext->ulContextLen > 255 )
4481+ {
4482+ ERROR_MSG (" ML-DSA: Invalid parameters, context length > 255" );
4483+ return CKR_ARGUMENTS_BAD;
4484+ }
4485+
4486+ // Always initialize context fields
4487+ additionalContext.contextAsChar = NULL ;
4488+ additionalContext.contextLength = 0 ;
4489+ if (ckSignAdditionalContext->ulContextLen > 0 )
4490+ {
4491+ if (ckSignAdditionalContext->pContext == NULL )
4492+ {
4493+ ERROR_MSG (" ML-DSA: Invalid parameters, pContext is NULL" );
4494+ return CKR_ARGUMENTS_BAD;
4495+ }
4496+ additionalContext.contextAsChar = (unsigned char *) ckSignAdditionalContext->pContext ;
4497+ additionalContext.contextLength = ckSignAdditionalContext->ulContextLen ;
4498+ }
4499+ switch (ckSignAdditionalContext->hedgeVariant ) {
4500+ case CKH_HEDGE_REQUIRED:
4501+ additionalContext.hedgeType = Hedge::HEDGE_REQUIRED;
4502+ break ;
4503+ case CKH_DETERMINISTIC_REQUIRED:
4504+ additionalContext.hedgeType = Hedge::DETERMINISTIC_REQUIRED;
4505+ break ;
4506+ case CKH_HEDGE_PREFERRED:
4507+ // Per PKCS11v3.2 section 6.67.5
4508+ // "If no parameter is supplied the hedgeVariant will be CKH_HEDGE_PREFERRED"
4509+ default :
4510+ additionalContext.hedgeType = Hedge::HEDGE_PREFERRED;
4511+ }
4512+ param = &additionalContext;
4513+ paramLen = sizeof (SIGN_ADDITIONAL_CONTEXT);
4514+ }
4515+ }
44704516 break ;
44714517#endif
44724518 default :
@@ -5229,6 +5275,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
52295275#endif
52305276#ifdef WITH_ML_DSA
52315277 bool isMLDSA = false ;
5278+ SIGN_ADDITIONAL_CONTEXT additionalContext = {};
52325279#endif
52335280 switch (pMechanism->mechanism ) {
52345281 case CKM_RSA_PKCS:
@@ -5499,6 +5546,49 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
54995546 mechanism = AsymMech::MLDSA;
55005547 bAllowMultiPartOp = false ;
55015548 isMLDSA = true ;
5549+ if (pMechanism->pParameter != NULL_PTR) {
5550+ if (pMechanism->ulParameterLen != sizeof (CK_SIGN_ADDITIONAL_CONTEXT))
5551+ {
5552+ ERROR_MSG (" Invalid parameters" );
5553+ return CKR_ARGUMENTS_BAD;
5554+ }
5555+ else
5556+ {
5557+ const CK_SIGN_ADDITIONAL_CONTEXT* ckSignAdditionalContext = (const CK_SIGN_ADDITIONAL_CONTEXT*) pMechanism->pParameter ;
5558+ if (ckSignAdditionalContext->ulContextLen > 255 ) {
5559+ ERROR_MSG (" ML-DSA: Invalid parameters, context length > 255" );
5560+ return CKR_ARGUMENTS_BAD;
5561+ }
5562+ // Always initialize context fields
5563+ additionalContext.contextAsChar = NULL ;
5564+ additionalContext.contextLength = 0 ;
5565+ if (ckSignAdditionalContext->ulContextLen > 0 ) {
5566+ if (ckSignAdditionalContext->pContext == NULL )
5567+ {
5568+ ERROR_MSG (" ML-DSA: Invalid parameters, pContext is NULL" );
5569+ return CKR_ARGUMENTS_BAD;
5570+ }
5571+ additionalContext.contextAsChar = (unsigned char *) ckSignAdditionalContext->pContext ;
5572+ additionalContext.contextLength = ckSignAdditionalContext->ulContextLen ;
5573+ }
5574+
5575+ switch (ckSignAdditionalContext->hedgeVariant ) {
5576+ case CKH_HEDGE_REQUIRED:
5577+ additionalContext.hedgeType = Hedge::HEDGE_REQUIRED;
5578+ break ;
5579+ case CKH_DETERMINISTIC_REQUIRED:
5580+ additionalContext.hedgeType = Hedge::DETERMINISTIC_REQUIRED;
5581+ break ;
5582+ // Per PKCS11v3.2 section 6.67.5
5583+ // "If no parameter is supplied the hedgeVariant will be CKH_HEDGE_PREFERRED"
5584+ case CKH_HEDGE_PREFERRED:
5585+ default :
5586+ additionalContext.hedgeType = Hedge::HEDGE_PREFERRED;
5587+ }
5588+ param = &additionalContext;
5589+ paramLen = sizeof (SIGN_ADDITIONAL_CONTEXT);
5590+ }
5591+ }
55025592 break ;
55035593#endif
55045594 default :
@@ -10274,7 +10364,6 @@ CK_RV SoftHSM::generateMLDSA
1027410364 bOK = bOK && osobject->setAttribute (CKA_NEVER_EXTRACTABLE, bNeverExtractable);
1027510365
1027610366 // MLDSA Private Key Attributes
10277- ByteString parameterSet;
1027810367 ByteString value;
1027910368 ByteString seed;
1028010369 if (isPrivateKeyPrivate)
0 commit comments