Skip to content

Commit af30be5

Browse files
committed
Applied some CodeRabbitAI suggestions
1 parent 42e76f8 commit af30be5

14 files changed

Lines changed: 100 additions & 53 deletions

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ jobs:
8585
env:
8686
OPENSSL_VERSION: 3.5.2
8787
OPENSSL_INSTALL_DIR: /usr/local/openssl-3.5
88-
LDFLAGS: "-Wl,-R/usr/local/openssl-3.5/lib64 -L/usr/local/openssl-3.5/lib64"
88+
LDFLAGS: "-Wl,-rpath,/usr/local/openssl-3.5/lib64 -L/usr/local/openssl-3.5/lib64"
89+
PKG_CONFIG_PATH: "/usr/local/openssl-3.5/lib64/pkgconfig"
8990
run: |
9091
sudo apt-get update -qq
9192
sudo apt-get install -y libcppunit-dev p11-kit build-essential checkinstall zlib1g-dev sudo autoconf libtool git
@@ -103,7 +104,8 @@ jobs:
103104
# Once all OpenSSL deprecations fixed, uncomment this
104105
# CXXFLAGS: -Werror
105106
OPENSSL_INSTALL_DIR: /usr/local/openssl-3.5
106-
LDFLAGS: "-Wl,-R/usr/local/openssl-3.5/lib64 -L/usr/local/openssl-3.5/lib64"
107+
LDFLAGS: "-Wl,-rpath,/usr/local/openssl-3.5/lib64 -L/usr/local/openssl-3.5/lib64"
108+
PKG_CONFIG_PATH: "/usr/local/openssl-3.5/lib64/pkgconfig"
107109
run: |
108110
./autogen.sh
109111
./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: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10038,35 +10038,37 @@ CK_RV SoftHSM::generateMLDSA
1003810038
return CKR_GENERAL_ERROR;
1003910039

1004010040
// Extract desired key information
10041-
unsigned long* params = 0;
10041+
CK_ULONG paramSet = 0;
1004210042
for (CK_ULONG i = 0; i < ulPublicKeyAttributeCount; i++)
1004310043
{
1004410044
switch (pPublicKeyTemplate[i].type)
1004510045
{
1004610046
case CKA_PARAMETER_SET:
10047-
params = (unsigned long*)pPublicKeyTemplate[i].pValue;
10047+
if (pPublicKeyTemplate[i].ulValueLen != sizeof(CK_ULONG)) {
10048+
INFO_MSG("CKA_PARAMETER_SET must be sizeof(CK_ULONG)");
10049+
return CKR_ATTRIBUTE_VALUE_INVALID;
10050+
}
10051+
paramSet = *(CK_ULONG*)pPublicKeyTemplate[i].pValue;
1004810052
break;
1004910053
default:
1005010054
break;
1005110055
}
1005210056
}
1005310057

1005410058
// The parameters must be specified to be able to generate a key pair.
10055-
if (params == 0) {
10059+
if (paramSet == 0) {
1005610060
INFO_MSG("Missing parameter(s) in pPublicKeyTemplate");
1005710061
return CKR_TEMPLATE_INCOMPLETE;
1005810062
}
1005910063

10060-
if (*params != 1UL && *params != 2UL && *params != 3UL) {
10061-
INFO_MSG("Wrong parameterSet: %ld", *params);
10064+
if (paramSet != 1UL && paramSet != 2UL && paramSet != 3UL) {
10065+
INFO_MSG("Wrong parameterSet: %ld", paramSet);
1006210066
return CKR_PARAMETER_SET_NOT_SUPPORTED;
1006310067
}
1006410068

1006510069
// Set the parameters
1006610070
MLDSAParameters p;
10067-
p.setParameterSet(*params);
10068-
10069-
DEBUG_MSG("params=%d, p.parameterSet=%d", *params, p.getParameterSet());
10071+
p.setParameterSet(paramSet);
1007010072

1007110073
// Generate key pair
1007210074
AsymmetricKeyPair* kp = NULL;
@@ -10132,8 +10134,6 @@ CK_RV SoftHSM::generateMLDSA
1013210134
CK_ULONG ulKeyGenMechanism = (CK_ULONG)CKM_ML_DSA_KEY_PAIR_GEN;
1013310135
bOK = bOK && osobject->setAttribute(CKA_KEY_GEN_MECHANISM,ulKeyGenMechanism);
1013410136

10135-
DEBUG_MSG("pub->getParameterSet()=%d, pub->getValue()=%s", pub->getParameterSet(), pub->getValue().hex_str().c_str());
10136-
1013710137
// ML-DSA Public Key Attributes
1013810138
ByteString value;
1013910139
if (isPublicKeyPrivate)
@@ -10215,7 +10215,6 @@ CK_RV SoftHSM::generateMLDSA
1021510215
ByteString parameterSet;
1021610216
ByteString value;
1021710217
ByteString seed;
10218-
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());
1021910218
if (isPrivateKeyPrivate)
1022010219
{
1022110220
token->encrypt(priv->getValue(), value);
@@ -10227,8 +10226,6 @@ CK_RV SoftHSM::generateMLDSA
1022710226
seed = priv->getSeed();
1022810227
}
1022910228

10230-
DEBUG_MSG("parameterSet=%d, seed=%s, value=%s", priv->getParameterSet(), seed.hex_str().c_str(), value.hex_str().c_str());
10231-
1023210229
bOK = bOK && osobject->setAttribute(CKA_PARAMETER_SET, priv->getParameterSet());
1023310230
bOK = bOK && osobject->setAttribute(CKA_VALUE, value);
1023410231
bOK = bOK && osobject->setAttribute(CKA_SEED, seed);

src/lib/SoftHSM.h

Lines changed: 8 additions & 0 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,10 @@ 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);
458+
#ifdef WITH_ML_DSA
454459
CK_RV getMLDSAPrivateKey(MLDSAPrivateKey* privateKey, Token* token, OSObject* key);
455460
CK_RV getMLDSAPublicKey(MLDSAPublicKey* publicKey, Token* token, OSObject* key);
461+
#endif
456462

457463
ByteString getECDHPubData(ByteString& pubData);
458464

@@ -462,7 +468,9 @@ class SoftHSM
462468
bool setECPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
463469
bool setEDPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
464470
bool setGOSTPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
471+
#ifdef WITH_ML_DSA
465472
bool setMLDSAPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const;
473+
#endif
466474

467475

468476
CK_RV WrapKeyAsym

src/lib/crypto/MLDSAPublicKey.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "config.h"
1111
#include "PublicKey.h"
12+
#include "ByteString.h"
1213

1314
class MLDSAPublicKey : public PublicKey
1415
{

src/lib/crypto/OSSLMLDSA.cpp

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ bool OSSLMLDSA::sign(PrivateKey *privateKey, const ByteString &dataToSign,
3131
return false;
3232
}
3333

34+
if (privateKey == NULL)
35+
{
36+
ERROR_MSG("No private key supplied");
37+
return false;
38+
}
39+
3440
// Check if the private key is the right type
3541
if (!privateKey->isOfType(OSSLMLDSAPrivateKey::type))
3642
{
@@ -68,6 +74,12 @@ bool OSSLMLDSA::sign(PrivateKey *privateKey, const ByteString &dataToSign,
6874
memset(&signature[0], 0, len);
6975

7076
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
77+
if (ctx == NULL)
78+
{
79+
ERROR_MSG("ML-DSA sign ctx alloc failed");
80+
return false;
81+
}
82+
7183
if (!EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey))
7284
{
7385
ERROR_MSG("ML-DSA sign init failed (0x%08X)", ERR_get_error());
@@ -117,6 +129,12 @@ bool OSSLMLDSA::verify(PublicKey *publicKey, const ByteString &originalData,
117129
return false;
118130
}
119131

132+
if (publicKey == NULL)
133+
{
134+
ERROR_MSG("No public key supplied");
135+
return false;
136+
}
137+
120138
// Check if the private key is the right type
121139
if (!publicKey->isOfType(OSSLMLDSAPublicKey::type))
122140
{
@@ -159,6 +177,14 @@ bool OSSLMLDSA::verify(PublicKey *publicKey, const ByteString &originalData,
159177

160178
unsigned long parameterSet = pk->getParameterSet();
161179
const char* name = OSSL::mldsaParameterSet2Name(parameterSet);
180+
181+
if (name == NULL)
182+
{
183+
ERROR_MSG("Unknown ML-DSA parameter set (%lu)", parameterSet);
184+
EVP_PKEY_CTX_free(vctx);
185+
return false;
186+
}
187+
162188
sig_alg = EVP_SIGNATURE_fetch(NULL, name, NULL);
163189
if (sig_alg == NULL) {
164190
ERROR_MSG("ML-DSA EVP_SIGNATURE_fetch failed (0x%08X)", ERR_get_error());
@@ -176,12 +202,20 @@ bool OSSLMLDSA::verify(PublicKey *publicKey, const ByteString &originalData,
176202
int verifyRV = EVP_PKEY_verify(vctx, signature.const_byte_str(), signature.size(),
177203
originalData.const_byte_str(), originalData.size());
178204

179-
if (verifyRV != 1) {
180-
ERROR_MSG("ML-DSA verify failed (0x%08X)", verifyRV);
181-
EVP_PKEY_CTX_free(vctx);
182-
EVP_SIGNATURE_free(sig_alg);
183-
return false;
184-
}
205+
if (verifyRV != 1)
206+
{
207+
if (verifyRV == 0)
208+
{
209+
ERROR_MSG("ML-DSA signature invalid");
210+
} else
211+
{
212+
ERROR_MSG("ML-DSA verify error (0x%08X)", ERR_get_error());
213+
}
214+
EVP_PKEY_CTX_free(vctx);
215+
EVP_SIGNATURE_free(sig_alg);
216+
return false;
217+
}
218+
185219
EVP_PKEY_CTX_free(vctx);
186220
EVP_SIGNATURE_free(sig_alg);
187221
return true;
@@ -265,6 +299,12 @@ bool OSSLMLDSA::generateKeyPair(AsymmetricKeyPair **ppKeyPair, AsymmetricParamet
265299
unsigned long parameterSet = params->getParameterSet();
266300
const char* name = OSSL::mldsaParameterSet2Name(parameterSet);
267301

302+
if (name == NULL)
303+
{
304+
ERROR_MSG("Unknown ML-DSA parameter set (%lu)", parameterSet);
305+
return false;
306+
}
307+
268308
EVP_PKEY_CTX *ctx = NULL;
269309
EVP_PKEY *pkey = NULL;
270310
ctx = EVP_PKEY_CTX_new_from_name(NULL, name, NULL);
@@ -288,8 +328,11 @@ bool OSSLMLDSA::generateKeyPair(AsymmetricKeyPair **ppKeyPair, AsymmetricParamet
288328
// Create an asymmetric key-pair object to return
289329
OSSLMLDSAKeyPair *kp = new OSSLMLDSAKeyPair();
290330

291-
((OSSLMLDSAPrivateKey *)kp->getPrivateKey())->setFromOSSL(pkey);
292-
((OSSLMLDSAPublicKey *)kp->getPublicKey())->setFromOSSL(pkey);
331+
// bump refcount for each wrapper
332+
EVP_PKEY_up_ref(pkey);
333+
((OSSLMLDSAPrivateKey*)kp->getPrivateKey())->setFromOSSL(pkey);
334+
EVP_PKEY_up_ref(pkey);
335+
((OSSLMLDSAPublicKey*) kp->getPublicKey())->setFromOSSL(pkey);
293336

294337
*ppKeyPair = kp;
295338

src/lib/crypto/OSSLMLDSA.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ class OSSLMLDSA : public AsymmetricAlgorithm
5151
private:
5252
};
5353

54-
#endif // !_SOFTHSM_V2_OSSLEDDSA_H
54+
#endif // !_SOFTHSM_V2_OSSLMLDSA_H
5555

src/lib/crypto/OSSLMLDSAKeyPair.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
// Set the public key
1313
void OSSLMLDSAKeyPair::setPublicKey(OSSLMLDSAPublicKey& publicKey)
1414
{
15-
pubKey = publicKey;
15+
// Copy only the public material; avoid sharing OpenSSL handles
16+
pubKey.setValue(publicKey.getValue());
1617
}
1718

1819
// Set the private key
1920
void OSSLMLDSAKeyPair::setPrivateKey(OSSLMLDSAPrivateKey& privateKey)
2021
{
21-
privKey = privateKey;
22+
// Copy only the raw material; avoid sharing OpenSSL handles
23+
privKey.setSeed(privateKey.getSeed());
24+
privKey.setValue(privateKey.getValue());
2225
}
2326

2427
// Return the public key

0 commit comments

Comments
 (0)