Skip to content

Commit 8609c5a

Browse files
committed
Moved MLDSA key util methods to dedicated class
1 parent af30be5 commit 8609c5a

File tree

7 files changed

+155
-112
lines changed

7 files changed

+155
-112
lines changed

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"
@@ -4533,7 +4536,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan
45334536
return CKR_HOST_MEMORY;
45344537
}
45354538

4536-
if (getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK)
4539+
if (MLDSAUtil::getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK)
45374540
{
45384541
asymCrypto->recyclePrivateKey(privateKey);
45394542
CryptoFactory::i()->recycleAsymmetricAlgorithm(asymCrypto);
@@ -5540,7 +5543,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
55405543
return CKR_HOST_MEMORY;
55415544
}
55425545

5543-
if (getMLDSAPublicKey((MLDSAPublicKey*)publicKey, token, key) != CKR_OK)
5546+
if (MLDSAUtil::getMLDSAPublicKey((MLDSAPublicKey*)publicKey, token, key) != CKR_OK)
55445547
{
55455548
asymCrypto->recyclePublicKey(publicKey);
55465549
CryptoFactory::i()->recycleAsymmetricAlgorithm(asymCrypto);
@@ -6934,7 +6937,7 @@ CK_RV SoftHSM::C_WrapKey
69346937
#endif
69356938
#ifdef WITH_ML_DSA
69366939
case CKK_ML_DSA:
6937-
rv = getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key);
6940+
rv = MLDSAUtil::getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key);
69386941
break;
69396942
#endif
69406943
}
@@ -7616,7 +7619,7 @@ CK_RV SoftHSM::C_UnwrapKey
76167619
#ifdef WITH_ML_DSA
76177620
else if (keyType == CKK_ML_DSA)
76187621
{
7619-
bOK = bOK && setMLDSAPrivateKey(osobject, keydata, token, isPrivate != CK_FALSE);
7622+
bOK = bOK && MLDSAUtil::setMLDSAPrivateKey(osobject, keydata, token, isPrivate != CK_FALSE);
76207623
}
76217624
#endif
76227625
else
@@ -12998,65 +13001,7 @@ CK_RV SoftHSM::getEDPublicKey(EDPublicKey* publicKey, Token* token, OSObject* ke
1299813001
return CKR_OK;
1299913002
}
1300013003

13001-
CK_RV SoftHSM::getMLDSAPrivateKey(MLDSAPrivateKey* privateKey, Token* token, OSObject* key)
13002-
{
13003-
if (privateKey == NULL) return CKR_ARGUMENTS_BAD;
13004-
if (token == NULL) return CKR_ARGUMENTS_BAD;
13005-
if (key == NULL) return CKR_ARGUMENTS_BAD;
13006-
13007-
// Get the CKA_PRIVATE attribute, when the attribute is not present use default false
13008-
bool isKeyPrivate = key->getBooleanValue(CKA_PRIVATE, false);
13009-
13010-
// ML-DSA Private Key Attributes
13011-
ByteString value;
13012-
ByteString seed;
13013-
if (isKeyPrivate)
13014-
{
13015-
bool bOK = true;
13016-
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_VALUE), value);
13017-
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_SEED), seed);
13018-
if (!bOK)
13019-
return CKR_GENERAL_ERROR;
13020-
}
13021-
else
13022-
{
13023-
value = key->getByteStringValue(CKA_VALUE);
13024-
seed = key->getByteStringValue(CKA_SEED);
13025-
}
13026-
13027-
privateKey->setValue(value);
13028-
privateKey->setSeed(seed);
13029-
13030-
return CKR_OK;
13031-
}
13032-
13033-
CK_RV SoftHSM::getMLDSAPublicKey(MLDSAPublicKey* publicKey, Token* token, OSObject* key)
13034-
{
13035-
if (publicKey == NULL) return CKR_ARGUMENTS_BAD;
13036-
if (token == NULL) return CKR_ARGUMENTS_BAD;
13037-
if (key == NULL) return CKR_ARGUMENTS_BAD;
1303813004

13039-
// Get the CKA_PRIVATE attribute, when the attribute is not present use default false
13040-
bool isKeyPrivate = key->getBooleanValue(CKA_PRIVATE, false);
13041-
13042-
// EC Public Key Attributes
13043-
ByteString value;
13044-
if (isKeyPrivate)
13045-
{
13046-
bool bOK = true;
13047-
bOK = bOK && token->decrypt(key->getByteStringValue(CKA_VALUE), value);
13048-
if (!bOK)
13049-
return CKR_GENERAL_ERROR;
13050-
}
13051-
else
13052-
{
13053-
value = key->getByteStringValue(CKA_VALUE);
13054-
}
13055-
13056-
publicKey->setValue(value);
13057-
13058-
return CKR_OK;
13059-
}
1306013005

1306113006
CK_RV SoftHSM::getDHPrivateKey(DHPrivateKey* privateKey, Token* token, OSObject* key)
1306213007
{
@@ -13515,48 +13460,6 @@ bool SoftHSM::setEDPrivateKey(OSObject* key, const ByteString &ber, Token* token
1351513460
return bOK;
1351613461
}
1351713462

13518-
bool SoftHSM::setMLDSAPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const
13519-
{
13520-
AsymmetricAlgorithm* mldsa = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::MLDSA);
13521-
if (mldsa == NULL)
13522-
return false;
13523-
PrivateKey* priv = mldsa->newPrivateKey();
13524-
if (priv == NULL)
13525-
{
13526-
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
13527-
return false;
13528-
}
13529-
if (!priv->PKCS8Decode(ber))
13530-
{
13531-
mldsa->recyclePrivateKey(priv);
13532-
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
13533-
return false;
13534-
}
13535-
// ML-DSA Private Key Attributes
13536-
ByteString parameterSet;
13537-
ByteString seed;
13538-
ByteString value;
13539-
if (isPrivate)
13540-
{
13541-
token->encrypt(((MLDSAPrivateKey*)priv)->getSeed(), seed);
13542-
token->encrypt(((MLDSAPrivateKey*)priv)->getValue(), value);
13543-
}
13544-
else
13545-
{
13546-
seed = ((MLDSAPrivateKey*)priv)->getSeed();
13547-
value = ((MLDSAPrivateKey*)priv)->getValue();
13548-
}
13549-
bool bOK = true;
13550-
bOK = bOK && key->setAttribute(CKA_PARAMETER_SET, ((MLDSAPrivateKey*)priv)->getParameterSet());
13551-
bOK = bOK && key->setAttribute(CKA_SEED, seed);
13552-
bOK = bOK && key->setAttribute(CKA_VALUE, value);
13553-
13554-
mldsa->recyclePrivateKey(priv);
13555-
CryptoFactory::i()->recycleAsymmetricAlgorithm(mldsa);
13556-
13557-
return bOK;
13558-
}
13559-
1356013463
bool SoftHSM::setGOSTPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const
1356113464
{
1356213465
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)