Skip to content

Commit 2885d93

Browse files
author
Antoine Lochet
committed
Moved MLDSA key util methods to dedicated class
Applied some CodeRabbitAI suggestions Removed copyright banners
1 parent 5ab787f commit 2885d93

28 files changed

Lines changed: 265 additions & 577 deletions

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ jobs:
8484
env:
8585
OPENSSL_VERSION: 3.5.2
8686
OPENSSL_INSTALL_DIR: /usr/local/openssl-3.5
87-
LDFLAGS: "-Wl,-R/usr/local/openssl-3.5/lib64 -L/usr/local/openssl-3.5/lib64"
87+
LDFLAGS: "-Wl,-rpath,/usr/local/openssl-3.5/lib64 -L/usr/local/openssl-3.5/lib64"
88+
PKG_CONFIG_PATH: "/usr/local/openssl-3.5/lib64/pkgconfig"
8889
run: |
8990
sudo apt-get update -qq
9091
sudo apt-get install -y libcppunit-dev p11-kit build-essential checkinstall zlib1g-dev sudo autoconf libtool git
@@ -102,7 +103,8 @@ jobs:
102103
# Once all OpenSSL deprecations fixed, uncomment this
103104
# CXXFLAGS: -Werror
104105
OPENSSL_INSTALL_DIR: /usr/local/openssl-3.5
105-
LDFLAGS: "-Wl,-R/usr/local/openssl-3.5/lib64 -L/usr/local/openssl-3.5/lib64"
106+
LDFLAGS: "-Wl,-rpath,/usr/local/openssl-3.5/lib64 -L/usr/local/openssl-3.5/lib64"
107+
PKG_CONFIG_PATH: "/usr/local/openssl-3.5/lib64/pkgconfig"
106108
run: |
107109
./autogen.sh
108110
./configure --with-crypto-backend=openssl --with-openssl=${{ env.OPENSSL_INSTALL_DIR }}

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ option(DISABLE_VISIBILITY "Disables and unsets -fvisibility=hidden" OFF)
88
option(ENABLE_64bit "Enable 64-bit compiling" OFF)
99
option(ENABLE_ECC "Enable support for ECC" ON)
1010
option(ENABLE_EDDSA "Enable support for EDDSA" ON)
11-
option(ENABLE_MLDSA "Enable support for ML-DSA" ON)
11+
option(ENABLE_MLDSA "Enable support for ML-DSA" OFF)
1212
option(ENABLE_GOST "Enable support for GOST" OFF)
1313
option(ENABLE_FIPS "Enable support for FIPS 140-2 mode" OFF)
1414
option(ENABLE_P11_KIT "Enable p11-kit integration" ON)

m4/acx_botan_mldsa.m4

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,21 @@ AC_DEFUN([ACX_BOTAN_MLDSA],[
88
LIBS="$CRYPTO_LIBS $LIBS"
99
1010
AC_LANG_PUSH([C++])
11-
AC_CACHE_VAL([acx_cv_lib_botan_mldsa_support],[
12-
acx_cv_lib_botan_mldsa_support=no
13-
AC_RUN_IFELSE([
14-
AC_LANG_SOURCE([[
15-
#include <botan/version.h>
16-
int main()
17-
{
18-
// TODO
19-
return 1;
20-
}
21-
]])
22-
],[
23-
AC_MSG_RESULT([yes])
11+
AC_CACHE_VAL([acx_cv_lib_botan_mldsa_support], [
12+
AC_COMPILE_IFELSE([
13+
AC_LANG_SOURCE([
14+
#include <botan/version.h>
15+
#ifndef BOTAN_HAS_ML_DSA
16+
# error "no ML-DSA support"
17+
#endif
18+
int main(void){ return 0; }
19+
])
20+
], [
2421
acx_cv_lib_botan_mldsa_support=yes
25-
],[
26-
AC_MSG_RESULT([no])
27-
acx_cv_lib_botan_mldsa_support=no
28-
],[
29-
AC_MSG_WARN([Cannot test, assuming no ML-DSA])
22+
AC_MSG_RESULT([yes])
23+
], [
3024
acx_cv_lib_botan_mldsa_support=no
25+
AC_MSG_RESULT([no])
3126
])
3227
])
3328
AC_LANG_POP([C++])

src/lib/P11Attributes.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ bool P11AttrEcPoint::setDefault()
22342234
// Set default value
22352235
bool P11AttrParameterSet::setDefault()
22362236
{
2237-
OSAttribute attr(ByteString(""));
2237+
OSAttribute attr((unsigned long)0);
22382238
return osobject->setAttribute(type, attr);
22392239
}
22402240

@@ -2571,7 +2571,6 @@ bool P11AttrSeed::setDefault()
25712571
CK_RV P11AttrSeed::updateAttr(Token *token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int /*op*/)
25722572
{
25732573
ByteString plaintext((unsigned char*)pValue, ulValueLen);
2574-
DEBUG_MSG("P11AttrSeed plaintext: %s", plaintext.hex_str().c_str());
25752574
ByteString value;
25762575

25772576
// Encrypt

src/lib/SoftHSM.cpp

Lines changed: 21 additions & 117 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"
@@ -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

1311713063
CK_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-
1361613520
bool SoftHSM::setGOSTPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const
1361713521
{
1361813522
AsymmetricAlgorithm* gost = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::GOST);

src/lib/SoftHSM.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@
5252
#include "DHPrivateKey.h"
5353
#include "GOSTPublicKey.h"
5454
#include "GOSTPrivateKey.h"
55+
#ifdef WITH_ML_DSA
5556
#include "MLDSAPublicKey.h"
5657
#include "MLDSAPrivateKey.h"
58+
#endif
5759

5860
#include <memory>
5961

@@ -373,6 +375,7 @@ class SoftHSM
373375
CK_BBOOL isOnToken,
374376
CK_BBOOL isPrivate
375377
);
378+
#ifdef WITH_ML_DSA
376379
CK_RV generateMLDSA
377380
(
378381
CK_SESSION_HANDLE hSession,
@@ -387,6 +390,7 @@ class SoftHSM
387390
CK_BBOOL isPrivateKeyOnToken,
388391
CK_BBOOL isPrivateKeyPrivate
389392
);
393+
#endif
390394
#ifdef WITH_ECC
391395
CK_RV deriveECDH
392396
(
@@ -451,8 +455,6 @@ class SoftHSM
451455
CK_RV getGOSTPrivateKey(GOSTPrivateKey* privateKey, Token* token, OSObject* key);
452456
CK_RV getGOSTPublicKey(GOSTPublicKey* publicKey, Token* token, OSObject* key);
453457
CK_RV getSymmetricKey(SymmetricKey* skey, Token* token, OSObject* key);
454-
CK_RV getMLDSAPrivateKey(MLDSAPrivateKey* privateKey, Token* token, OSObject* key);
455-
CK_RV getMLDSAPublicKey(MLDSAPublicKey* publicKey, Token* token, OSObject* key);
456458

457459
ByteString getECDHPubData(ByteString& pubData);
458460

@@ -462,7 +464,6 @@ class SoftHSM
462464
bool setECPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
463465
bool setEDPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
464466
bool setGOSTPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
465-
bool setMLDSAPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
466467

467468

468469
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

0 commit comments

Comments
 (0)