Skip to content

Commit 9461b4f

Browse files
antoinelochetAntoine Lochet
authored andcommitted
Moved MLDSA key util methods to dedicated class
1 parent e68ed24 commit 9461b4f

7 files changed

Lines changed: 155 additions & 112 deletions

File tree

src/lib/SoftHSM.cpp

Lines changed: 7 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@
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"
@@ -4568,7 +4571,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
45684571
return CKR_HOST_MEMORY;
45694572
}
45704573

4571-
if (getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK)
4574+
if (MLDSAUtil::getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK)
45724575
{
45734576
asymCrypto->recyclePrivateKey(privateKey);
45744577
CryptoFactory::i()->recycleAsymmetricAlgorithm(asymCrypto);
@@ -5600,7 +5603,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
56005603
return CKR_HOST_MEMORY;
56015604
}
56025605

5603-
if (getMLDSAPublicKey((MLDSAPublicKey*)publicKey, token, key) != CKR_OK)
5606+
if (MLDSAUtil::getMLDSAPublicKey((MLDSAPublicKey*)publicKey, token, key) != CKR_OK)
56045607
{
56055608
asymCrypto->recyclePublicKey(publicKey);
56065609
CryptoFactory::i()->recycleAsymmetricAlgorithm(asymCrypto);
@@ -6994,7 +6997,7 @@ CK_RV SoftHSM::C_WrapKey
69946997
#endif
69956998
#ifdef WITH_ML_DSA
69966999
case CKK_ML_DSA:
6997-
rv = getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key);
7000+
rv = MLDSAUtil::getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key);
69987001
break;
69997002
#endif
70007003
}
@@ -7676,7 +7679,7 @@ CK_RV SoftHSM::C_UnwrapKey
76767679
#ifdef WITH_ML_DSA
76777680
else if (keyType == CKK_ML_DSA)
76787681
{
7679-
bOK = bOK && setMLDSAPrivateKey(osobject, keydata, token, isPrivate != CK_FALSE);
7682+
bOK = bOK && MLDSAUtil::setMLDSAPrivateKey(osobject, keydata, token, isPrivate != CK_FALSE);
76807683
}
76817684
#endif
76827685
else
@@ -13058,65 +13061,7 @@ CK_RV SoftHSM::getEDPublicKey(EDPublicKey* publicKey, Token* token, OSObject* ke
1305813061
return CKR_OK;
1305913062
}
1306013063

13061-
CK_RV SoftHSM::getMLDSAPrivateKey(MLDSAPrivateKey* privateKey, Token* token, OSObject* key)
13062-
{
13063-
if (privateKey == NULL) return CKR_ARGUMENTS_BAD;
13064-
if (token == NULL) return CKR_ARGUMENTS_BAD;
13065-
if (key == NULL) return CKR_ARGUMENTS_BAD;
13066-
13067-
// Get the CKA_PRIVATE attribute, when the attribute is not present use default false
13068-
bool isKeyPrivate = key->getBooleanValue(CKA_PRIVATE, false);
13069-
13070-
// ML-DSA Private Key Attributes
13071-
ByteString value;
13072-
ByteString seed;
13073-
if (isKeyPrivate)
13074-
{
13075-
bool bOK = true;
13076-
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_VALUE), value);
13077-
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_SEED), seed);
13078-
if (!bOK)
13079-
return CKR_GENERAL_ERROR;
13080-
}
13081-
else
13082-
{
13083-
value = key->getByteStringValue(CKA_VALUE);
13084-
seed = key->getByteStringValue(CKA_SEED);
13085-
}
13086-
13087-
privateKey->setValue(value);
13088-
privateKey->setSeed(seed);
13089-
13090-
return CKR_OK;
13091-
}
13092-
13093-
CK_RV SoftHSM::getMLDSAPublicKey(MLDSAPublicKey* publicKey, Token* token, OSObject* key)
13094-
{
13095-
if (publicKey == NULL) return CKR_ARGUMENTS_BAD;
13096-
if (token == NULL) return CKR_ARGUMENTS_BAD;
13097-
if (key == NULL) return CKR_ARGUMENTS_BAD;
1309813064

13099-
// Get the CKA_PRIVATE attribute, when the attribute is not present use default false
13100-
bool isKeyPrivate = key->getBooleanValue(CKA_PRIVATE, false);
13101-
13102-
// EC Public Key Attributes
13103-
ByteString value;
13104-
if (isKeyPrivate)
13105-
{
13106-
bool bOK = true;
13107-
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_VALUE), value);
13108-
if (!bOK)
13109-
return CKR_GENERAL_ERROR;
13110-
}
13111-
else
13112-
{
13113-
value = key->getByteStringValue(CKA_VALUE);
13114-
}
13115-
13116-
publicKey->setValue(value);
13117-
13118-
return CKR_OK;
13119-
}
1312013065

1312113066
CK_RV SoftHSM::getDHPrivateKey(DHPrivateKey* privateKey, Token* token, OSObject* key)
1312213067
{
@@ -13575,48 +13520,6 @@ bool SoftHSM::setEDPrivateKey(OSObject* key, const ByteString &ber, Token* token
1357513520
return bOK;
1357613521
}
1357713522

13578-
bool SoftHSM::setMLDSAPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const
13579-
{
13580-
AsymmetricAlgorithm* mldsa = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::MLDSA);
13581-
if (mldsa == NULL)
13582-
return false;
13583-
PrivateKey* priv = mldsa->newPrivateKey();
13584-
if (priv == NULL)
13585-
{
13586-
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
13587-
return false;
13588-
}
13589-
if (!priv->PKCS8Decode(ber))
13590-
{
13591-
mldsa->recyclePrivateKey(priv);
13592-
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
13593-
return false;
13594-
}
13595-
// ML-DSA Private Key Attributes
13596-
ByteString parameterSet;
13597-
ByteString seed;
13598-
ByteString value;
13599-
if (isPrivate)
13600-
{
13601-
token->encrypt(((MLDSAPrivateKey*)priv)->getSeed(), seed);
13602-
token->encrypt(((MLDSAPrivateKey*)priv)->getValue(), value);
13603-
}
13604-
else
13605-
{
13606-
seed = ((MLDSAPrivateKey*)priv)->getSeed();
13607-
value = ((MLDSAPrivateKey*)priv)->getValue();
13608-
}
13609-
bool bOK = true;
13610-
bOK = bOK && key->setAttribute(CKA_PARAMETER_SET, ((MLDSAPrivateKey*)priv)->getParameterSet());
13611-
bOK = bOK && key->setAttribute(CKA_SEED, seed);
13612-
bOK = bOK && key->setAttribute(CKA_VALUE, value);
13613-
13614-
mldsa->recyclePrivateKey(priv);
13615-
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
13616-
13617-
return bOK;
13618-
}
13619-
1362013523
bool SoftHSM::setGOSTPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const
1362113524
{
1362213525
AsymmetricAlgorithm* gost = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::GOST);

src/lib/SoftHSM.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,6 @@ class SoftHSM
455455
CK_RV getGOSTPrivateKey(GOSTPrivateKey* privateKey, Token* token, OSObject* key);
456456
CK_RV getGOSTPublicKey(GOSTPublicKey* publicKey, Token* token, OSObject* key);
457457
CK_RV getSymmetricKey(SymmetricKey* skey, Token* token, OSObject* key);
458-
#ifdef WITH_ML_DSA
459-
CK_RV getMLDSAPrivateKey(MLDSAPrivateKey* privateKey, Token* token, OSObject* key);
460-
CK_RV getMLDSAPublicKey(MLDSAPublicKey* publicKey, Token* token, OSObject* key);
461-
#endif
462458

463459
ByteString getECDHPubData(ByteString& pubData);
464460

@@ -468,9 +464,6 @@ class SoftHSM
468464
bool setECPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
469465
bool setEDPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
470466
bool setGOSTPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
471-
#ifdef WITH_ML_DSA
472-
bool setMLDSAPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
473-
#endif
474467

475468

476469
CK_RV WrapKeyAsym

src/lib/crypto/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(SOURCES AESKey.cpp
3131
MLDSAParameters.cpp
3232
MLDSAPrivateKey.cpp
3333
MLDSAPublicKey.cpp
34+
MLDSAUtil.cpp
3435
RSAParameters.cpp
3536
RSAPrivateKey.cpp
3637
RSAPublicKey.cpp

src/lib/crypto/MLDSAUtil.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*****************************************************************************
2+
MLDSAUtil.cpp
3+
4+
ML-DSA convenience functions
5+
*****************************************************************************/
6+
7+
#include "config.h"
8+
#ifdef WITH_ML_DSA
9+
#include "MLDSAUtil.h"
10+
11+
/*static*/ CK_RV MLDSAUtil::getMLDSAPrivateKey(MLDSAPrivateKey* privateKey, Token* token, OSObject* key)
12+
{
13+
if (privateKey == NULL) return CKR_ARGUMENTS_BAD;
14+
if (token == NULL) return CKR_ARGUMENTS_BAD;
15+
if (key == NULL) return CKR_ARGUMENTS_BAD;
16+
17+
// Get the CKA_PRIVATE attribute, when the attribute is not present use default false
18+
bool isKeyPrivate = key->getBooleanValue(CKA_PRIVATE, false);
19+
20+
// ML-DSA Private Key Attributes
21+
ByteString value;
22+
ByteString seed;
23+
if (isKeyPrivate)
24+
{
25+
bool bOK = true;
26+
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_VALUE), value);
27+
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_SEED), seed);
28+
if (!bOK)
29+
return CKR_GENERAL_ERROR;
30+
}
31+
else
32+
{
33+
value = key->getByteStringValue(CKA_VALUE);
34+
seed = key->getByteStringValue(CKA_SEED);
35+
}
36+
37+
privateKey->setValue(value);
38+
privateKey->setSeed(seed);
39+
40+
return CKR_OK;
41+
}
42+
43+
/*static*/ CK_RV MLDSAUtil::getMLDSAPublicKey(MLDSAPublicKey* publicKey, Token* token, OSObject* key)
44+
{
45+
if (publicKey == NULL) return CKR_ARGUMENTS_BAD;
46+
if (token == NULL) return CKR_ARGUMENTS_BAD;
47+
if (key == NULL) return CKR_ARGUMENTS_BAD;
48+
49+
// Get the CKA_PRIVATE attribute, when the attribute is not present use default false
50+
bool isKeyPrivate = key->getBooleanValue(CKA_PRIVATE, false);
51+
52+
// EC Public Key Attributes
53+
ByteString value;
54+
if (isKeyPrivate)
55+
{
56+
bool bOK = true;
57+
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_VALUE), value);
58+
if (!bOK)
59+
return CKR_GENERAL_ERROR;
60+
}
61+
else
62+
{
63+
value = key->getByteStringValue(CKA_VALUE);
64+
}
65+
66+
publicKey->setValue(value);
67+
68+
return CKR_OK;
69+
}
70+
71+
/*static*/ bool MLDSAUtil::setMLDSAPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate)
72+
{
73+
AsymmetricAlgorithm* mldsa = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::MLDSA);
74+
if (mldsa == NULL)
75+
return false;
76+
PrivateKey* priv = mldsa->newPrivateKey();
77+
if (priv == NULL)
78+
{
79+
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
80+
return false;
81+
}
82+
if (!priv->PKCS8Decode(ber))
83+
{
84+
mldsa->recyclePrivateKey(priv);
85+
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
86+
return false;
87+
}
88+
// ML-DSA Private Key Attributes
89+
ByteString parameterSet;
90+
ByteString seed;
91+
ByteString value;
92+
if (isPrivate)
93+
{
94+
token->encrypt(((MLDSAPrivateKey*)priv)->getSeed(), seed);
95+
token->encrypt(((MLDSAPrivateKey*)priv)->getValue(), value);
96+
}
97+
else
98+
{
99+
seed = ((MLDSAPrivateKey*)priv)->getSeed();
100+
value = ((MLDSAPrivateKey*)priv)->getValue();
101+
}
102+
bool bOK = true;
103+
bOK = bOK && key->setAttribute(CKA_PARAMETER_SET, ((MLDSAPrivateKey*)priv)->getParameterSet());
104+
bOK = bOK && key->setAttribute(CKA_SEED, seed);
105+
bOK = bOK && key->setAttribute(CKA_VALUE, value);
106+
107+
mldsa->recyclePrivateKey(priv);
108+
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
109+
110+
return bOK;
111+
}
112+
113+
#endif

src/lib/crypto/MLDSAUtil.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*****************************************************************************
2+
MLDSAUtil.h
3+
4+
ML-DSA convenience functions
5+
*****************************************************************************/
6+
7+
#ifndef _SOFTHSM_V2_MLDSAUTIL_H
8+
#define _SOFTHSM_V2_MLDSAUTIL_H
9+
10+
#include "config.h"
11+
#ifdef WITH_ML_DSA
12+
#include "MLDSAPrivateKey.h"
13+
#include "MLDSAPublicKey.h"
14+
#include "AsymmetricAlgorithm.h"
15+
#include "CryptoFactory.h"
16+
#include "ByteString.h"
17+
#include "Token.h"
18+
#include "OSObject.h"
19+
20+
class MLDSAUtil
21+
{
22+
public:
23+
static CK_RV getMLDSAPrivateKey(MLDSAPrivateKey* privateKey, Token* token, OSObject* key);
24+
static CK_RV getMLDSAPublicKey(MLDSAPublicKey* publicKey, Token* token, OSObject* key);
25+
26+
static bool setMLDSAPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate);
27+
};
28+
29+
#endif // !_SOFTHSM_V2_MLDSAUTIL_H
30+
#endif

src/lib/crypto/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
33
AM_CPPFLAGS = -I$(srcdir)/.. \
44
-I$(srcdir)/../common \
55
-I$(srcdir)/../data_mgr \
6+
-I$(srcdir)/../slot_mgr \
7+
-I$(srcdir)/../object_store \
68
-I$(srcdir)/../pkcs11 \
79
@CRYPTO_INCLUDES@
810

@@ -31,6 +33,7 @@ libsofthsm_crypto_la_SOURCES = AESKey.cpp \
3133
MLDSAParameters.cpp \
3234
MLDSAPrivateKey.cpp \
3335
MLDSAPublicKey.cpp \
36+
MLDSAUtil.cpp \
3437
RSAParameters.cpp \
3538
RSAPrivateKey.cpp \
3639
RSAPublicKey.cpp \

src/lib/crypto/OSSLComp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626

2727
/*****************************************************************************
28-
OSSLUtil.cpp
28+
OSSLComp.cpp
2929
3030
Adding OpenSSL forward-compatible code as suggested by OpenSSL
3131
*****************************************************************************/

0 commit comments

Comments
 (0)