Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed use of ECB with Botan #724

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions src/lib/SoftHSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ CK_RV SoftHSM::C_Initialize(CK_VOID_PTR pInitArgs)
}

// Load the enabled list of algorithms
prepareSupportedMecahnisms(mechanisms_table);
prepareSupportedMechanisms(mechanisms_table);

isRemovable = Configuration::i()->getBool("slots.removable", false);

Expand Down Expand Up @@ -720,7 +720,7 @@ CK_RV SoftHSM::C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo)
return token->getTokenInfo(pInfo);
}

void SoftHSM::prepareSupportedMecahnisms(std::map<std::string, CK_MECHANISM_TYPE> &t)
void SoftHSM::prepareSupportedMechanisms(std::map<std::string, CK_MECHANISM_TYPE> &t)
{
#ifndef WITH_FIPS
t["CKM_MD5"] = CKM_MD5;
Expand Down Expand Up @@ -765,20 +765,30 @@ void SoftHSM::prepareSupportedMecahnisms(std::map<std::string, CK_MECHANISM_TYPE
t["CKM_DES2_KEY_GEN"] = CKM_DES2_KEY_GEN;
t["CKM_DES3_KEY_GEN"] = CKM_DES3_KEY_GEN;
#ifndef WITH_FIPS
#ifndef WITH_BOTAN
t["CKM_DES_ECB"] = CKM_DES_ECB;
#endif
t["CKM_DES_CBC"] = CKM_DES_CBC;
t["CKM_DES_CBC_PAD"] = CKM_DES_CBC_PAD;
#ifndef WITH_BOTAN
t["CKM_DES_ECB_ENCRYPT_DATA"] = CKM_DES_ECB_ENCRYPT_DATA;
#endif
t["CKM_DES_CBC_ENCRYPT_DATA"] = CKM_DES_CBC_ENCRYPT_DATA;
#endif
#ifndef WITH_BOTAN
t["CKM_DES3_ECB"] = CKM_DES3_ECB;
#endif
t["CKM_DES3_CBC"] = CKM_DES3_CBC;
t["CKM_DES3_CBC_PAD"] = CKM_DES3_CBC_PAD;
#ifndef WITH_BOTAN
t["CKM_DES3_ECB_ENCRYPT_DATA"] = CKM_DES3_ECB_ENCRYPT_DATA;
#endif
t["CKM_DES3_CBC_ENCRYPT_DATA"] = CKM_DES3_CBC_ENCRYPT_DATA;
t["CKM_DES3_CMAC"] = CKM_DES3_CMAC;
t["CKM_AES_KEY_GEN"] = CKM_AES_KEY_GEN;
#ifndef WITH_BOTAN
t["CKM_AES_ECB"] = CKM_AES_ECB;
#endif
t["CKM_AES_CBC"] = CKM_AES_CBC;
t["CKM_AES_CBC_PAD"] = CKM_AES_CBC_PAD;
t["CKM_AES_CTR"] = CKM_AES_CTR;
Expand All @@ -787,7 +797,9 @@ void SoftHSM::prepareSupportedMecahnisms(std::map<std::string, CK_MECHANISM_TYPE
#ifdef HAVE_AES_KEY_WRAP_PAD
t["CKM_AES_KEY_WRAP_PAD"] = CKM_AES_KEY_WRAP_PAD;
#endif
#ifndef WITH_BOTAN
t["CKM_AES_ECB_ENCRYPT_DATA"] = CKM_AES_ECB_ENCRYPT_DATA;
#endif
t["CKM_AES_CBC_ENCRYPT_DATA"] = CKM_AES_CBC_ENCRYPT_DATA;
t["CKM_AES_CMAC"] = CKM_AES_CMAC;
t["CKM_DSA_PARAMETER_GEN"] = CKM_DSA_PARAMETER_GEN;
Expand Down Expand Up @@ -1130,20 +1142,24 @@ CK_RV SoftHSM::C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, CK_
pInfo->flags = CKF_WRAP | CKF_UNWRAP;
/* FALLTHROUGH */
#ifndef WITH_FIPS
#ifndef WITH_BOTAN
case CKM_DES_ECB:
/* FALLTHROUGH */
#endif
case CKM_DES_CBC:
/* FALLTHROUGH */
#endif
case CKM_DES3_CBC:
pInfo->flags |= CKF_WRAP;
/* FALLTHROUGH */
#ifndef WITH_BOTAN
case CKM_DES3_ECB:
// Key size is not in use
pInfo->ulMinKeySize = 0;
pInfo->ulMaxKeySize = 0;
pInfo->flags |= CKF_ENCRYPT | CKF_DECRYPT;
break;
#endif
case CKM_DES3_CMAC:
// Key size is not in use
pInfo->ulMinKeySize = 0;
Expand All @@ -1160,7 +1176,9 @@ CK_RV SoftHSM::C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, CK_
/* FALLTHROUGH */
case CKM_AES_CBC:
pInfo->flags |= CKF_WRAP;
#ifndef WITH_BOTAN
case CKM_AES_ECB:
#endif
case CKM_AES_CTR:
case CKM_AES_GCM:
pInfo->ulMinKeySize = 16;
Expand All @@ -1180,12 +1198,18 @@ CK_RV SoftHSM::C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, CK_
break;
#endif
#ifndef WITH_FIPS
#ifndef WITH_BOTAN
case CKM_DES_ECB_ENCRYPT_DATA:
#endif
case CKM_DES_CBC_ENCRYPT_DATA:
#endif
#ifndef WITH_BOTAN
case CKM_DES3_ECB_ENCRYPT_DATA:
#endif
case CKM_DES3_CBC_ENCRYPT_DATA:
#ifndef WITH_BOTAN
case CKM_AES_ECB_ENCRYPT_DATA:
#endif
case CKM_AES_CBC_ENCRYPT_DATA:
// Key size is not in use
pInfo->ulMinKeySize = 0;
Expand Down Expand Up @@ -2203,13 +2227,15 @@ CK_RV SoftHSM::SymEncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
size_t tagBytes = 0;
switch(pMechanism->mechanism) {
#ifndef WITH_FIPS
#ifndef WITH_BOTAN
case CKM_DES_ECB:
if (keyType != CKK_DES)
return CKR_KEY_TYPE_INCONSISTENT;
algo = SymAlgo::DES;
mode = SymMode::ECB;
bb = 7;
break;
#endif
case CKM_DES_CBC:
if (keyType != CKK_DES)
return CKR_KEY_TYPE_INCONSISTENT;
Expand Down Expand Up @@ -2242,13 +2268,15 @@ CK_RV SoftHSM::SymEncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
bb = 7;
break;
#endif
#ifndef WITH_BOTAN
case CKM_DES3_ECB:
if (keyType != CKK_DES2 && keyType != CKK_DES3)
return CKR_KEY_TYPE_INCONSISTENT;
algo = SymAlgo::DES3;
mode = SymMode::ECB;
bb = 7;
break;
#endif
case CKM_DES3_CBC:
if (keyType != CKK_DES2 && keyType != CKK_DES3)
return CKR_KEY_TYPE_INCONSISTENT;
Expand Down Expand Up @@ -2280,12 +2308,14 @@ CK_RV SoftHSM::SymEncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
memcpy(&iv[0], pMechanism->pParameter, pMechanism->ulParameterLen);
bb = 7;
break;
#ifndef WITH_BOTAN
case CKM_AES_ECB:
if (keyType != CKK_AES)
return CKR_KEY_TYPE_INCONSISTENT;
algo = SymAlgo::AES;
mode = SymMode::ECB;
break;
#endif
case CKM_AES_CBC:
if (keyType != CKK_AES)
return CKR_KEY_TYPE_INCONSISTENT;
Expand Down Expand Up @@ -2933,13 +2963,15 @@ CK_RV SoftHSM::SymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
size_t tagBytes = 0;
switch(pMechanism->mechanism) {
#ifndef WITH_FIPS
#ifndef WITH_BOTAN
case CKM_DES_ECB:
if (keyType != CKK_DES)
return CKR_KEY_TYPE_INCONSISTENT;
algo = SymAlgo::DES;
mode = SymMode::ECB;
bb = 7;
break;
#endif
case CKM_DES_CBC:
if (keyType != CKK_DES)
return CKR_KEY_TYPE_INCONSISTENT;
Expand Down Expand Up @@ -2972,13 +3004,15 @@ CK_RV SoftHSM::SymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
bb = 7;
break;
#endif
#ifndef WITH_BOTAN
case CKM_DES3_ECB:
if (keyType != CKK_DES2 && keyType != CKK_DES3)
return CKR_KEY_TYPE_INCONSISTENT;
algo = SymAlgo::DES3;
mode = SymMode::ECB;
bb = 7;
break;
#endif
case CKM_DES3_CBC:
if (keyType != CKK_DES2 && keyType != CKK_DES3)
return CKR_KEY_TYPE_INCONSISTENT;
Expand Down Expand Up @@ -3010,12 +3044,14 @@ CK_RV SoftHSM::SymDecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMech
memcpy(&iv[0], pMechanism->pParameter, pMechanism->ulParameterLen);
bb = 7;
break;
#ifndef WITH_BOTAN
case CKM_AES_ECB:
if (keyType != CKK_AES)
return CKR_KEY_TYPE_INCONSISTENT;
algo = SymAlgo::AES;
mode = SymMode::ECB;
break;
#endif
case CKM_AES_CBC:
if (keyType != CKK_AES)
return CKR_KEY_TYPE_INCONSISTENT;
Expand Down Expand Up @@ -7257,12 +7293,18 @@ CK_RV SoftHSM::C_DeriveKey
case CKM_ECDH1_DERIVE:
#endif
#ifndef WITH_FIPS
#ifndef WITH_BOTAN
case CKM_DES_ECB_ENCRYPT_DATA:
#endif
case CKM_DES_CBC_ENCRYPT_DATA:
#endif
#ifndef WITH_BOTAN
case CKM_DES3_ECB_ENCRYPT_DATA:
#endif
case CKM_DES3_CBC_ENCRYPT_DATA:
#ifndef WITH_BOTAN
case CKM_AES_ECB_ENCRYPT_DATA:
#endif
case CKM_AES_CBC_ENCRYPT_DATA:
case CKM_CONCATENATE_DATA_AND_BASE:
case CKM_CONCATENATE_BASE_AND_DATA:
Expand Down Expand Up @@ -8004,6 +8046,7 @@ CK_RV SoftHSM::generateDES
kcv = key->getKeyCheckValue();
}
bOK = bOK && osobject->setAttribute(CKA_VALUE, value);

if (checkValue)
bOK = bOK && osobject->setAttribute(CKA_CHECK_VALUE, kcv);

Expand Down Expand Up @@ -11547,11 +11590,13 @@ CK_RV SoftHSM::deriveSymmetric
size_t bb = 8;
switch(pMechanism->mechanism) {
#ifndef WITH_FIPS
#ifndef WITH_BOTAN
case CKM_DES_ECB_ENCRYPT_DATA:
algo = SymAlgo::DES;
mode = SymMode::ECB;
bb = 7;
break;
#endif
case CKM_DES_CBC_ENCRYPT_DATA:
algo = SymAlgo::DES;
mode = SymMode::CBC;
Expand All @@ -11562,11 +11607,13 @@ CK_RV SoftHSM::deriveSymmetric
8);
break;
#endif
#ifndef WITH_BOTAN
case CKM_DES3_ECB_ENCRYPT_DATA:
algo = SymAlgo::DES3;
mode = SymMode::ECB;
bb = 7;
break;
#endif
case CKM_DES3_CBC_ENCRYPT_DATA:
algo = SymAlgo::DES3;
mode = SymMode::CBC;
Expand All @@ -11576,10 +11623,12 @@ CK_RV SoftHSM::deriveSymmetric
&(CK_DES_CBC_ENCRYPT_DATA_PARAMS_PTR(pMechanism->pParameter)->iv[0]),
8);
break;
#ifndef WITH_BOTAN
case CKM_AES_ECB_ENCRYPT_DATA:
algo = SymAlgo::AES;
mode = SymMode::ECB;
break;
#endif
case CKM_AES_CBC_ENCRYPT_DATA:
algo = SymAlgo::AES;
mode = SymMode::CBC;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/SoftHSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ class SoftHSM
CK_RV MechParamCheckRSAPKCSOAEP(CK_MECHANISM_PTR pMechanism);

bool isMechanismPermitted(OSObject* key, CK_MECHANISM_PTR pMechanism);
void prepareSupportedMecahnisms(std::map<std::string, CK_MECHANISM_TYPE> &t);
void prepareSupportedMechanisms(std::map<std::string, CK_MECHANISM_TYPE> &t);
bool detectFork(void);
};

6 changes: 5 additions & 1 deletion src/lib/crypto/AESKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ ByteString AESKey::getKeyCheckValue() const
data.resize(cipher->getBlockSize());
memset(&data[0], 0, data.size());

if (!cipher->encryptInit(this, SymMode::ECB, iv, false) ||
// Single block of null (0x00) bytes
iv.resize(cipher->getBlockSize());
memset(&iv[0], 0, iv.size());

if (!cipher->encryptInit(this, SymMode::CBC, iv, false) ||
!cipher->encryptUpdate(data, encryptedData) ||
!cipher->encryptFinal(encryptedFinal))
{
Expand Down
6 changes: 5 additions & 1 deletion src/lib/crypto/DESKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ ByteString DESKey::getKeyCheckValue() const
data.resize(cipher->getBlockSize());
memset(&data[0], 0, data.size());

if (!cipher->encryptInit(this, SymMode::ECB, iv, false) ||
// Single block of null (0x00) bytes
iv.resize(cipher->getBlockSize());
memset(&iv[0], 0, iv.size());

if (!cipher->encryptInit(this, SymMode::CBC, iv, false) ||
!cipher->encryptUpdate(data, encryptedData) ||
!cipher->encryptFinal(encryptedFinal))
{
Expand Down
2 changes: 2 additions & 0 deletions src/lib/crypto/test/AESTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class AESTests : public CppUnit::TestFixture
CPPUNIT_TEST_SUITE(AESTests);
CPPUNIT_TEST(testBlockSize);
CPPUNIT_TEST(testCBC);
#ifndef WITH_BOTAN
CPPUNIT_TEST(testECB);
#endif
CPPUNIT_TEST(testCTR);
CPPUNIT_TEST(testGCM);
#ifdef HAVE_AES_KEY_WRAP
Expand Down
2 changes: 2 additions & 0 deletions src/lib/crypto/test/DESTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class DESTests : public CppUnit::TestFixture
CPPUNIT_TEST_SUITE(DESTests);
CPPUNIT_TEST(testBlockSize);
CPPUNIT_TEST(testCBC);
#ifndef WITH_BOTAN
CPPUNIT_TEST(testECB);
#endif
CPPUNIT_TEST(testOFB);
CPPUNIT_TEST(testCFB);
CPPUNIT_TEST_SUITE_END();
Expand Down
Loading