kwalletd: remove ECB support

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2021-03-29 13:29:54 +03:00
parent a58ea0da52
commit 91780aff84
4 changed files with 11 additions and 70 deletions

View file

@ -47,14 +47,15 @@ namespace KWallet {
typedef char Digest[16]; typedef char Digest[16];
static BlowfishPersistHandler *blowfishHandler =0; static BlowfishPersistHandler *blowfishHandler = 0;
BackendPersistHandler *BackendPersistHandler::getPersistHandler(BackendCipherType cipherType) BackendPersistHandler *BackendPersistHandler::getPersistHandler(BackendCipherType cipherType)
{ {
switch (cipherType){ switch (cipherType){
case BACKEND_CIPHER_BLOWFISH: { case BACKEND_CIPHER_BLOWFISH: {
if (0 == blowfishHandler) if (blowfishHandler == 0) {
blowfishHandler = new BlowfishPersistHandler; blowfishHandler = new BlowfishPersistHandler;
}
return blowfishHandler; return blowfishHandler;
} }
default: { default: {
@ -66,14 +67,10 @@ BackendPersistHandler *BackendPersistHandler::getPersistHandler(BackendCipherTyp
BackendPersistHandler *BackendPersistHandler::getPersistHandler(char magicBuf[KWMAGIC_LEN]) BackendPersistHandler *BackendPersistHandler::getPersistHandler(char magicBuf[KWMAGIC_LEN])
{ {
if ((magicBuf[2] == KWALLET_CIPHER_BLOWFISH_ECB || magicBuf[2] == KWALLET_CIPHER_BLOWFISH_CBC) && if (magicBuf[2] == KWALLET_CIPHER_BLOWFISH_CBC &&
(magicBuf[3] == KWALLET_HASH_SHA1 || magicBuf[3] == KWALLET_HASH_PBKDF2_SHA512)) { (magicBuf[3] == KWALLET_HASH_SHA1 || magicBuf[3] == KWALLET_HASH_PBKDF2_SHA512)) {
if (0 == blowfishHandler) { if (blowfishHandler == 0) {
bool useECBforReading = magicBuf[2] == KWALLET_CIPHER_BLOWFISH_ECB; blowfishHandler = new BlowfishPersistHandler();
if (useECBforReading) {
qDebug() << "this wallet uses ECB encryption. It'll be converted to CBC on next save.";
}
blowfishHandler = new BlowfishPersistHandler(useECBforReading);
} }
return blowfishHandler; return blowfishHandler;
} }
@ -84,11 +81,6 @@ int BlowfishPersistHandler::write(Backend* wb, KSaveFile& sf, QByteArray& versio
{ {
assert(wb->_cipherType == BACKEND_CIPHER_BLOWFISH); assert(wb->_cipherType == BACKEND_CIPHER_BLOWFISH);
if (_useECBforReading) {
qDebug() << "This wallet used ECB and is now saved using CBC";
_useECBforReading = false;
}
version[2] = KWALLET_CIPHER_BLOWFISH_CBC; version[2] = KWALLET_CIPHER_BLOWFISH_CBC;
if(!wb->_useNewHash) { if(!wb->_useNewHash) {
version[3] = KWALLET_HASH_SHA1; version[3] = KWALLET_HASH_SHA1;
@ -255,7 +247,7 @@ int BlowfishPersistHandler::read(Backend* wb, QFile& db, WId)
assert(encrypted.size() < db.size()); assert(encrypted.size() < db.size());
BlowFish _bf; BlowFish _bf;
CipherBlockChain bf(&_bf, _useECBforReading); CipherBlockChain bf(&_bf);
int blksz = bf.blockSize(); int blksz = bf.blockSize();
if ((encrypted.size() % blksz) != 0) { if ((encrypted.size() % blksz) != 0) {
return -5; // invalid file structure return -5; // invalid file structure

View file

@ -56,13 +56,11 @@ public:
class BlowfishPersistHandler : public BackendPersistHandler { class BlowfishPersistHandler : public BackendPersistHandler {
public: public:
explicit BlowfishPersistHandler(bool useECBforReading =false) : _useECBforReading(useECBforReading) {} explicit BlowfishPersistHandler() {}
virtual ~BlowfishPersistHandler() {} virtual ~BlowfishPersistHandler() {}
virtual int write(Backend* wb, KSaveFile& sf, QByteArray& version, WId w); virtual int write(Backend* wb, KSaveFile& sf, QByteArray& version, WId w);
virtual int read(Backend* wb, QFile& sf, WId w); virtual int read(Backend* wb, QFile& sf, WId w);
private:
bool _useECBforReading;
}; };
} // namespace } // namespace

View file

@ -21,9 +21,8 @@
#include <string.h> #include <string.h>
#include <kdebug.h> #include <kdebug.h>
CipherBlockChain::CipherBlockChain(BlockCipher *cipher, bool useECBforReading) : CipherBlockChain::CipherBlockChain(BlockCipher *cipher)
_cipher(cipher) : _cipher(cipher)
, _useECBforReading(useECBforReading)
{ {
_next = 0L; _next = 0L;
_register = 0L; _register = 0L;
@ -111,54 +110,8 @@ int CipherBlockChain::encrypt(void *block, int len)
return rc; return rc;
} }
// This is the old decrypt method, that was decrypting using ECB
// instead of CBC
int CipherBlockChain::decryptECB(void *block, int len) {
if (_cipher && !_writer) {
int rc;
_reader |= 1;
if (!_register) {
_register = new unsigned char[len];
_len = len;
memset(_register, 0, len);
} else if (len > _len) {
return -1;
}
if (!_next) {
_next = new unsigned char[_len];
}
memcpy(_next, block, _len);
rc = _cipher->decrypt(block, len);
if (rc != -1) {
// This might be optimizable
char *tb = (char *)block;
for (int i = 0; i < len; i++) {
tb[i] ^= ((char *)_register)[i];
}
}
void *temp;
temp = _next;
_next = _register;
_register = temp;
return rc;
}
return -1;
}
int CipherBlockChain::decrypt(void *block, int len) int CipherBlockChain::decrypt(void *block, int len)
{ {
if (_useECBforReading) {
kDebug() << "decrypting using ECB!";
return decryptECB(block, len);
}
if (_cipher && !_writer) { if (_cipher && !_writer) {
int rc = 0; int rc = 0;

View file

@ -34,7 +34,7 @@
class CipherBlockChain : public BlockCipher class CipherBlockChain : public BlockCipher
{ {
public: public:
CipherBlockChain(BlockCipher *cipher, bool useECBforReading =false); CipherBlockChain(BlockCipher *cipher);
virtual ~CipherBlockChain(); virtual ~CipherBlockChain();
virtual bool setKey(void *key, int bitlength); virtual bool setKey(void *key, int bitlength);
@ -49,14 +49,12 @@ public:
private: private:
void initRegister(); void initRegister();
int decryptECB(void *block, int len);
BlockCipher *_cipher; BlockCipher *_cipher;
void *_register; void *_register;
void *_next; void *_next;
int _len; int _len;
int _reader, _writer; int _reader, _writer;
bool _useECBforReading;
}; };
#endif #endif