|
62 | 62 | #include "DHPrivateKey.h" |
63 | 63 | #include "GOSTPublicKey.h" |
64 | 64 | #include "GOSTPrivateKey.h" |
| 65 | +#ifdef WITH_ML_DSA |
65 | 66 | #include "MLDSAParameters.h" |
66 | 67 | #include "MLDSAPublicKey.h" |
67 | 68 | #include "MLDSAPrivateKey.h" |
| 69 | +#include "MLDSAUtil.h" |
| 70 | +#endif |
68 | 71 | #include "cryptoki.h" |
69 | 72 | #include "SoftHSM.h" |
70 | 73 | #include "osmutex.h" |
@@ -4533,7 +4536,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan |
4533 | 4536 | return CKR_HOST_MEMORY; |
4534 | 4537 | } |
4535 | 4538 |
|
4536 | | - if (getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK) |
| 4539 | + if (MLDSAUtil::getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK) |
4537 | 4540 | { |
4538 | 4541 | asymCrypto->recyclePrivateKey(privateKey); |
4539 | 4542 | CryptoFactory::i()->recycleAsymmetricAlgorithm(asymCrypto); |
@@ -5540,7 +5543,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech |
5540 | 5543 | return CKR_HOST_MEMORY; |
5541 | 5544 | } |
5542 | 5545 |
|
5543 | | - if (getMLDSAPublicKey((MLDSAPublicKey*)publicKey, token, key) != CKR_OK) |
| 5546 | + if (MLDSAUtil::getMLDSAPublicKey((MLDSAPublicKey*)publicKey, token, key) != CKR_OK) |
5544 | 5547 | { |
5545 | 5548 | asymCrypto->recyclePublicKey(publicKey); |
5546 | 5549 | CryptoFactory::i()->recycleAsymmetricAlgorithm(asymCrypto); |
@@ -6934,7 +6937,7 @@ CK_RV SoftHSM::C_WrapKey |
6934 | 6937 | #endif |
6935 | 6938 | #ifdef WITH_ML_DSA |
6936 | 6939 | case CKK_ML_DSA: |
6937 | | - rv = getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key); |
| 6940 | + rv = MLDSAUtil::getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key); |
6938 | 6941 | break; |
6939 | 6942 | #endif |
6940 | 6943 | } |
@@ -7616,7 +7619,7 @@ CK_RV SoftHSM::C_UnwrapKey |
7616 | 7619 | #ifdef WITH_ML_DSA |
7617 | 7620 | else if (keyType == CKK_ML_DSA) |
7618 | 7621 | { |
7619 | | - bOK = bOK && setMLDSAPrivateKey(osobject, keydata, token, isPrivate != CK_FALSE); |
| 7622 | + bOK = bOK && MLDSAUtil::setMLDSAPrivateKey(osobject, keydata, token, isPrivate != CK_FALSE); |
7620 | 7623 | } |
7621 | 7624 | #endif |
7622 | 7625 | else |
@@ -12998,65 +13001,7 @@ CK_RV SoftHSM::getEDPublicKey(EDPublicKey* publicKey, Token* token, OSObject* ke |
12998 | 13001 | return CKR_OK; |
12999 | 13002 | } |
13000 | 13003 |
|
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; |
13038 | 13004 |
|
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 | | -} |
13060 | 13005 |
|
13061 | 13006 | CK_RV SoftHSM::getDHPrivateKey(DHPrivateKey* privateKey, Token* token, OSObject* key) |
13062 | 13007 | { |
@@ -13515,48 +13460,6 @@ bool SoftHSM::setEDPrivateKey(OSObject* key, const ByteString &ber, Token* token |
13515 | 13460 | return bOK; |
13516 | 13461 | } |
13517 | 13462 |
|
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 | | - |
13560 | 13463 | bool SoftHSM::setGOSTPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const |
13561 | 13464 | { |
13562 | 13465 | AsymmetricAlgorithm* gost = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::GOST); |
|
0 commit comments