|
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" |
@@ -4568,7 +4571,7 @@ CK_RV SoftHSM::AsymSignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechan |
4568 | 4571 | return CKR_HOST_MEMORY; |
4569 | 4572 | } |
4570 | 4573 |
|
4571 | | - if (getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK) |
| 4574 | + if (MLDSAUtil::getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key) != CKR_OK) |
4572 | 4575 | { |
4573 | 4576 | asymCrypto->recyclePrivateKey(privateKey); |
4574 | 4577 | CryptoFactory::i()->recycleAsymmetricAlgorithm(asymCrypto); |
@@ -5600,7 +5603,7 @@ CK_RV SoftHSM::AsymVerifyInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech |
5600 | 5603 | return CKR_HOST_MEMORY; |
5601 | 5604 | } |
5602 | 5605 |
|
5603 | | - if (getMLDSAPublicKey((MLDSAPublicKey*)publicKey, token, key) != CKR_OK) |
| 5606 | + if (MLDSAUtil::getMLDSAPublicKey((MLDSAPublicKey*)publicKey, token, key) != CKR_OK) |
5604 | 5607 | { |
5605 | 5608 | asymCrypto->recyclePublicKey(publicKey); |
5606 | 5609 | CryptoFactory::i()->recycleAsymmetricAlgorithm(asymCrypto); |
@@ -6994,7 +6997,7 @@ CK_RV SoftHSM::C_WrapKey |
6994 | 6997 | #endif |
6995 | 6998 | #ifdef WITH_ML_DSA |
6996 | 6999 | case CKK_ML_DSA: |
6997 | | - rv = getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key); |
| 7000 | + rv = MLDSAUtil::getMLDSAPrivateKey((MLDSAPrivateKey*)privateKey, token, key); |
6998 | 7001 | break; |
6999 | 7002 | #endif |
7000 | 7003 | } |
@@ -7676,7 +7679,7 @@ CK_RV SoftHSM::C_UnwrapKey |
7676 | 7679 | #ifdef WITH_ML_DSA |
7677 | 7680 | else if (keyType == CKK_ML_DSA) |
7678 | 7681 | { |
7679 | | - bOK = bOK && setMLDSAPrivateKey(osobject, keydata, token, isPrivate != CK_FALSE); |
| 7682 | + bOK = bOK && MLDSAUtil::setMLDSAPrivateKey(osobject, keydata, token, isPrivate != CK_FALSE); |
7680 | 7683 | } |
7681 | 7684 | #endif |
7682 | 7685 | else |
@@ -13058,65 +13061,7 @@ CK_RV SoftHSM::getEDPublicKey(EDPublicKey* publicKey, Token* token, OSObject* ke |
13058 | 13061 | return CKR_OK; |
13059 | 13062 | } |
13060 | 13063 |
|
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; |
13098 | 13064 |
|
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 | | -} |
13120 | 13065 |
|
13121 | 13066 | CK_RV SoftHSM::getDHPrivateKey(DHPrivateKey* privateKey, Token* token, OSObject* key) |
13122 | 13067 | { |
@@ -13575,48 +13520,6 @@ bool SoftHSM::setEDPrivateKey(OSObject* key, const ByteString &ber, Token* token |
13575 | 13520 | return bOK; |
13576 | 13521 | } |
13577 | 13522 |
|
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 | | - |
13620 | 13523 | bool SoftHSM::setGOSTPrivateKey(OSObject* key, const ByteString &ber, Token* token, bool isPrivate) const |
13621 | 13524 | { |
13622 | 13525 | AsymmetricAlgorithm* gost = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::GOST); |
|
0 commit comments