6262#include " DHPrivateKey.h"
6363#include " GOSTPublicKey.h"
6464#include " GOSTPrivateKey.h"
65+ #ifdef WITH_ML_DSA
6566#include " MLDSAParameters.h"
6667#include " MLDSAPublicKey.h"
6768#include " MLDSAPrivateKey.h"
69+ #include " MLDSAUtil.h"
70+ #endif
6871#include " cryptoki.h"
6972#include " SoftHSM.h"
7073#include " osmutex.h"
@@ -143,8 +146,10 @@ static CK_RV newP11Object(CK_OBJECT_CLASS objClass, CK_KEY_TYPE keyType, CK_CERT
143146 *p11object = new P11GOSTPublicKeyObj ();
144147 else if (keyType == CKK_EC_EDWARDS)
145148 *p11object = new P11EDPublicKeyObj ();
149+ #ifdef WITH_ML_DSA
146150 else if (keyType == CKK_ML_DSA)
147151 *p11object = new P11MLDSAPublicKeyObj ();
152+ #endif
148153 else
149154 return CKR_ATTRIBUTE_VALUE_INVALID;
150155 break ;
@@ -162,8 +167,10 @@ static CK_RV newP11Object(CK_OBJECT_CLASS objClass, CK_KEY_TYPE keyType, CK_CERT
162167 *p11object = new P11GOSTPrivateKeyObj ();
163168 else if (keyType == CKK_EC_EDWARDS)
164169 *p11object = new P11EDPrivateKeyObj ();
170+ #ifdef WITH_ML_DSA
165171 else if (keyType == CKK_ML_DSA)
166172 *p11object = new P11MLDSAPrivateKeyObj ();
173+ #endif
167174 else
168175 return CKR_ATTRIBUTE_VALUE_INVALID;
169176 break ;
@@ -4561,7 +4568,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
45614568 return CKR_HOST_MEMORY;
45624569 }
45634570
4564- if (getMLDSAPrivateKey ((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK)
4571+ if (MLDSAUtil:: getMLDSAPrivateKey ((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK)
45654572 {
45664573 asymCrypto->recyclePrivateKey (privateKey);
45674574 CryptoFactory::i ()->recycleAsymmetricAlgorithm (asymCrypto);
@@ -5593,7 +5600,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
55935600 return CKR_HOST_MEMORY;
55945601 }
55955602
5596- if (getMLDSAPublicKey ((MLDSAPublicKey*)publicKey, token, key) != CKR_OK)
5603+ if (MLDSAUtil:: getMLDSAPublicKey ((MLDSAPublicKey*)publicKey, token, key) != CKR_OK)
55975604 {
55985605 asymCrypto->recyclePublicKey (publicKey);
55995606 CryptoFactory::i ()->recycleAsymmetricAlgorithm (asymCrypto);
@@ -6987,7 +6994,7 @@ CK_RV SoftHSM::C_WrapKey
69876994#endif
69886995#ifdef WITH_ML_DSA
69896996 case CKK_ML_DSA:
6990- rv = getMLDSAPrivateKey ((MLDSAPrivateKey*)privateKey, token, key);
6997+ rv = MLDSAUtil:: getMLDSAPrivateKey ((MLDSAPrivateKey*)privateKey, token, key);
69916998 break ;
69926999#endif
69937000 }
@@ -7669,7 +7676,7 @@ CK_RV SoftHSM::C_UnwrapKey
76697676#ifdef WITH_ML_DSA
76707677 else if (keyType == CKK_ML_DSA)
76717678 {
7672- bOK = bOK && setMLDSAPrivateKey (osobject, keydata, token, isPrivate != CK_FALSE);
7679+ bOK = bOK && MLDSAUtil:: setMLDSAPrivateKey (osobject, keydata, token, isPrivate != CK_FALSE);
76737680 }
76747681#endif
76757682 else
@@ -10091,35 +10098,37 @@ CK_RV SoftHSM::generateMLDSA
1009110098 return CKR_GENERAL_ERROR;
1009210099
1009310100 // Extract desired key information
10094- unsigned long * params = 0 ;
10101+ CK_ULONG paramSet = 0 ;
1009510102 for (CK_ULONG i = 0 ; i < ulPublicKeyAttributeCount; i++)
1009610103 {
1009710104 switch (pPublicKeyTemplate[i].type )
1009810105 {
1009910106 case CKA_PARAMETER_SET:
10100- params = (unsigned long *)pPublicKeyTemplate[i].pValue ;
10107+ if (pPublicKeyTemplate[i].ulValueLen != sizeof (CK_ULONG)) {
10108+ INFO_MSG (" CKA_PARAMETER_SET must be sizeof(CK_ULONG)" );
10109+ return CKR_ATTRIBUTE_VALUE_INVALID;
10110+ }
10111+ paramSet = *(CK_ULONG*)pPublicKeyTemplate[i].pValue ;
1010110112 break ;
1010210113 default :
1010310114 break ;
1010410115 }
1010510116 }
1010610117
1010710118 // The parameters must be specified to be able to generate a key pair.
10108- if (params == 0 ) {
10119+ if (paramSet == 0 ) {
1010910120 INFO_MSG (" Missing parameter(s) in pPublicKeyTemplate" );
1011010121 return CKR_TEMPLATE_INCOMPLETE;
1011110122 }
1011210123
10113- if (*params != 1UL && *params != 2UL && *params != 3UL ) {
10114- INFO_MSG (" Wrong parameterSet : %ld " , *params );
10124+ if (paramSet != CKP_ML_DSA_44 && paramSet != CKP_ML_DSA_65 && paramSet != CKP_ML_DSA_87 ) {
10125+ INFO_MSG (" Unsupported parameter set : %lu " , ( unsigned long )paramSet );
1011510126 return CKR_PARAMETER_SET_NOT_SUPPORTED;
1011610127 }
1011710128
1011810129 // Set the parameters
1011910130 MLDSAParameters p;
10120- p.setParameterSet (*params);
10121-
10122- DEBUG_MSG (" params=%d, p.parameterSet=%d" , *params, p.getParameterSet ());
10131+ p.setParameterSet (paramSet);
1012310132
1012410133 // Generate key pair
1012510134 AsymmetricKeyPair* kp = NULL ;
@@ -10185,8 +10194,6 @@ CK_RV SoftHSM::generateMLDSA
1018510194 CK_ULONG ulKeyGenMechanism = (CK_ULONG)CKM_ML_DSA_KEY_PAIR_GEN;
1018610195 bOK = bOK && osobject->setAttribute (CKA_KEY_GEN_MECHANISM,ulKeyGenMechanism);
1018710196
10188- DEBUG_MSG (" pub->getParameterSet()=%d, pub->getValue()=%s" , pub->getParameterSet (), pub->getValue ().hex_str ().c_str ());
10189-
1019010197 // ML-DSA Public Key Attributes
1019110198 ByteString value;
1019210199 if (isPublicKeyPrivate)
@@ -10268,7 +10275,6 @@ CK_RV SoftHSM::generateMLDSA
1026810275 ByteString parameterSet;
1026910276 ByteString value;
1027010277 ByteString seed;
10271- DEBUG_MSG (" priv->getParameterSet()=%d, priv->getSeed()=%s, priv->getValue()=%s" , priv->getParameterSet (), priv->getSeed ().hex_str ().c_str (), priv->getValue ().hex_str ().c_str ());
1027210278 if (isPrivateKeyPrivate)
1027310279 {
1027410280 token->encrypt (priv->getValue (), value);
@@ -10280,8 +10286,6 @@ CK_RV SoftHSM::generateMLDSA
1028010286 seed = priv->getSeed ();
1028110287 }
1028210288
10283- DEBUG_MSG (" parameterSet=%d, seed=%s, value=%s" , priv->getParameterSet (), seed.hex_str ().c_str (), value.hex_str ().c_str ());
10284-
1028510289 bOK = bOK && osobject->setAttribute (CKA_PARAMETER_SET, priv->getParameterSet ());
1028610290 bOK = bOK && osobject->setAttribute (CKA_VALUE, value);
1028710291 bOK = bOK && osobject->setAttribute (CKA_SEED, seed);
@@ -13054,65 +13058,7 @@ CK_RV SoftHSM::getEDPublicKey(EDPublicKey* publicKey, Token* token, OSObject* ke
1305413058 return CKR_OK;
1305513059}
1305613060
13057- CK_RV SoftHSM::getMLDSAPrivateKey (MLDSAPrivateKey* privateKey, Token* token, OSObject* key)
13058- {
13059- if (privateKey == NULL ) return CKR_ARGUMENTS_BAD;
13060- if (token == NULL ) return CKR_ARGUMENTS_BAD;
13061- if (key == NULL ) return CKR_ARGUMENTS_BAD;
13062-
13063- // Get the CKA_PRIVATE attribute, when the attribute is not present use default false
13064- bool isKeyPrivate = key->getBooleanValue (CKA_PRIVATE, false );
13065-
13066- // ML-DSA Private Key Attributes
13067- ByteString value;
13068- ByteString seed;
13069- if (isKeyPrivate)
13070- {
13071- bool bOK = true ;
13072- bOK = bOK && token->decrypt (key->getByteStringValue (CKA_VALUE), value);
13073- bOK = bOK && token->decrypt (key->getByteStringValue (CKA_SEED), seed);
13074- if (!bOK)
13075- return CKR_GENERAL_ERROR;
13076- }
13077- else
13078- {
13079- value = key->getByteStringValue (CKA_VALUE);
13080- seed = key->getByteStringValue (CKA_SEED);
13081- }
13082-
13083- privateKey->setValue (value);
13084- privateKey->setSeed (seed);
13085-
13086- return CKR_OK;
13087- }
13088-
13089- CK_RV SoftHSM::getMLDSAPublicKey (MLDSAPublicKey* publicKey, Token* token, OSObject* key)
13090- {
13091- if (publicKey == NULL ) return CKR_ARGUMENTS_BAD;
13092- if (token == NULL ) return CKR_ARGUMENTS_BAD;
13093- if (key == NULL ) return CKR_ARGUMENTS_BAD;
13094-
13095- // Get the CKA_PRIVATE attribute, when the attribute is not present use default false
13096- bool isKeyPrivate = key->getBooleanValue (CKA_PRIVATE, false );
13097-
13098- // EC Public Key Attributes
13099- ByteString value;
13100- if (isKeyPrivate)
13101- {
13102- bool bOK = true ;
13103- bOK = bOK && token->decrypt (key->getByteStringValue (CKA_VALUE), value);
13104- if (!bOK)
13105- return CKR_GENERAL_ERROR;
13106- }
13107- else
13108- {
13109- value = key->getByteStringValue (CKA_VALUE);
13110- }
13111-
13112- publicKey->setValue (value);
1311313061
13114- return CKR_OK;
13115- }
1311613062
1311713063CK_RV SoftHSM::getDHPrivateKey (DHPrivateKey* privateKey, Token* token, OSObject* key)
1311813064{
@@ -13571,48 +13517,6 @@ bool SoftHSM::setEDPrivateKey(OSObject* key, const ByteString &ber, Token* token
1357113517 return bOK;
1357213518}
1357313519
13574- bool SoftHSM::setMLDSAPrivateKey (OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const
13575- {
13576- AsymmetricAlgorithm* mldsa = CryptoFactory::i ()->getAsymmetricAlgorithm (AsymAlgo::MLDSA);
13577- if (mldsa == NULL )
13578- return false ;
13579- PrivateKey* priv = mldsa->newPrivateKey ();
13580- if (priv == NULL )
13581- {
13582- CryptoFactory::i ()->recycleAsymmetricAlgorithm (mldsa);
13583- return false ;
13584- }
13585- if (!priv->PKCS8Decode (ber))
13586- {
13587- mldsa->recyclePrivateKey (priv);
13588- CryptoFactory::i ()->recycleAsymmetricAlgorithm (mldsa);
13589- return false ;
13590- }
13591- // ML-DSA Private Key Attributes
13592- ByteString parameterSet;
13593- ByteString seed;
13594- ByteString value;
13595- if (isPrivate)
13596- {
13597- token->encrypt (((MLDSAPrivateKey*)priv)->getSeed (), seed);
13598- token->encrypt (((MLDSAPrivateKey*)priv)->getValue (), value);
13599- }
13600- else
13601- {
13602- seed = ((MLDSAPrivateKey*)priv)->getSeed ();
13603- value = ((MLDSAPrivateKey*)priv)->getValue ();
13604- }
13605- bool bOK = true ;
13606- bOK = bOK && key->setAttribute (CKA_PARAMETER_SET, ((MLDSAPrivateKey*)priv)->getParameterSet ());
13607- bOK = bOK && key->setAttribute (CKA_SEED, seed);
13608- bOK = bOK && key->setAttribute (CKA_VALUE, value);
13609-
13610- mldsa->recyclePrivateKey (priv);
13611- CryptoFactory::i ()->recycleAsymmetricAlgorithm (mldsa);
13612-
13613- return bOK;
13614- }
13615-
1361613520bool SoftHSM::setGOSTPrivateKey (OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const
1361713521{
1361813522 AsymmetricAlgorithm* gost = CryptoFactory::i ()->getAsymmetricAlgorithm (AsymAlgo::GOST);
0 commit comments