diff options
| author | 2011-07-23 11:29:03 +0000 | |
|---|---|---|
| committer | 2011-07-23 11:29:03 +0000 | |
| commit | 835c5244ec55f1e51bb3d493a3bdbd328f01956e (patch) | |
| tree | 6a0a65a03e97f6864f60a61a9afc1bc9555542aa /src | |
| parent | fixed cbc: hex mode crypto keys parsing (diff) | |
| download | KVIrc-835c5244ec55f1e51bb3d493a3bdbd328f01956e.tar.gz KVIrc-835c5244ec55f1e51bb3d493a3bdbd328f01956e.tar.bz2 KVIrc-835c5244ec55f1e51bb3d493a3bdbd328f01956e.zip | |
KviCString: removed duplicate setLength/setLen, added padRight
Crypto: splitted Initalization Vector engine to its own file, added real cbc mode to Rijndael engine (wip)
git-svn-id: https://svn.kvirc.de/svn/trunk/kvirc@5911 17fca916-40b9-46aa-a4ea-0a15b648b75c
Diffstat (limited to 'src')
| -rw-r--r-- | src/kvilib/core/KviCString.cpp | 29 | ||||
| -rw-r--r-- | src/kvilib/core/KviCString.h | 7 | ||||
| -rw-r--r-- | src/kvilib/file/KviFile.cpp | 2 | ||||
| -rw-r--r-- | src/modules/rijndael/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/modules/rijndael/InitVectorEngine.cpp | 48 | ||||
| -rw-r--r-- | src/modules/rijndael/InitVectorEngine.h | 32 | ||||
| -rw-r--r-- | src/modules/rijndael/Rijndael.cpp | 55 | ||||
| -rw-r--r-- | src/modules/rijndael/Rijndael.h | 9 | ||||
| -rw-r--r-- | src/modules/rijndael/UglyBase64.cpp | 4 | ||||
| -rw-r--r-- | src/modules/rijndael/libkvirijndael.cpp | 124 | ||||
| -rw-r--r-- | src/modules/rijndael/libkvirijndael.h | 2 |
11 files changed, 236 insertions, 77 deletions
diff --git a/src/kvilib/core/KviCString.cpp b/src/kvilib/core/KviCString.cpp index f9873bb0a..69a10fa9b 100644 --- a/src/kvilib/core/KviCString.cpp +++ b/src/kvilib/core/KviCString.cpp @@ -1188,14 +1188,6 @@ KviCString::~KviCString() KviMemory::free(m_ptr); } -void KviCString::setLength(int iLen) -{ - KVI_ASSERT(iLen >= 0); - m_len = iLen; - m_ptr = (char *)KviMemory::reallocate(m_ptr,m_len+1); - *(m_ptr + m_len) = '\0'; -} - KviCString & KviCString::operator=(const KviCString &str) { KVI_ASSERT(str.m_ptr); @@ -2601,12 +2593,23 @@ KviCString & KviCString::cutFromLast(const char *c,bool bIncluded) return (*this); } -KviCString & KviCString::setLen(int len) +KviCString & KviCString::setLen(int iLen) { - KVI_ASSERT(len >= 0); - m_ptr = (char *)KviMemory::reallocate(m_ptr,len+1); - *(m_ptr+len)='\0'; - m_len = len; + KVI_ASSERT(iLen >= 0); + m_len = iLen; + m_ptr = (char *)KviMemory::reallocate(m_ptr,m_len+1); + *(m_ptr + m_len) = '\0'; + return (*this); +} + +KviCString & KviCString::padRight(int iLen, const char c) +{ + KVI_ASSERT(iLen >= 0); + m_ptr = (char *)KviMemory::reallocate(m_ptr,iLen+1); + *(m_ptr + iLen) = '\0'; + if(iLen > m_len) + KviMemory::set(m_ptr+m_len,c,iLen-m_len); + m_len = iLen; return (*this); } diff --git a/src/kvilib/core/KviCString.h b/src/kvilib/core/KviCString.h index 83bfc2875..9ec060938 100644 --- a/src/kvilib/core/KviCString.h +++ b/src/kvilib/core/KviCString.h @@ -182,9 +182,6 @@ public: // this is better than string = "", it does not call strlen void clear(); - // forces the length of this string to be iLen (iLen does NOT include the trailing null : it is automatically added) - void setLength(int iLen); - // Returns true if there is something "readable" inside the string bool hasNonWhiteSpaceData() const; @@ -217,12 +214,12 @@ public: // Null terminator is NOT included in len KviCString & setLen(int len); + // str must not be 0, but len can be anything (it is checked) KviCString & setStr(const char *str,int len = -1); // Like the special constructor that gets the same args. void extractFromString(const char *begin,const char *end); - // Safe sprintf. This one will never write past the end of the string // It can handle only %s %d %u and %c format flags. KviCString & sprintf(const char *fmt,...); @@ -331,6 +328,8 @@ public: KviCString & stripLeft(char c); KviCString & stripRight(char c); + // either "truncate to" or "add padding up" to iLen characters. + KviCString & padRight(int iLen, const char c='\0'); //============================================================================= // Tokenize //============================================================================= diff --git a/src/kvilib/file/KviFile.cpp b/src/kvilib/file/KviFile.cpp index 0b982aa4a..2aec00266 100644 --- a/src/kvilib/file/KviFile.cpp +++ b/src/kvilib/file/KviFile.cpp @@ -157,7 +157,7 @@ bool KviFile::load(KviCString & szData) { kvi_u32_t iLen; if(!load(iLen))return false; - szData.setLength(iLen); + szData.setLen(iLen); return (read((char *)(szData.ptr()),iLen) == iLen); } diff --git a/src/modules/rijndael/CMakeLists.txt b/src/modules/rijndael/CMakeLists.txt index 0ebf043c2..dab16ee57 100644 --- a/src/modules/rijndael/CMakeLists.txt +++ b/src/modules/rijndael/CMakeLists.txt @@ -18,6 +18,7 @@ IF(WANT_CRYPTOPP OR WANT_OPENSSL) Rijndael.cpp BlowFish.cpp UglyBase64.cpp + InitVectorEngine.cpp ) SET(kvirijndael_MOC_HDRS diff --git a/src/modules/rijndael/InitVectorEngine.cpp b/src/modules/rijndael/InitVectorEngine.cpp new file mode 100644 index 000000000..83faeb3ee --- /dev/null +++ b/src/modules/rijndael/InitVectorEngine.cpp @@ -0,0 +1,48 @@ +//============================================================================= +// +// File : InitVectorEngine.cpp +// Creation date : Sun Feb 6 2011 22:25:10 CEST by Fabio Bas +// +// This file is part of the KVIrc irc client distribution +// Copyright (C) 2011 Fabio Bas (ctrlaltca at gmail dot com) +// +// This program is FREE software. You can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your opinion) any later version. +// +// This program is distributed in the HOPE that it will be USEFUL, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, write to the Free Software Foundation, +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// +//============================================================================= + +#include "InitVectorEngine.h" + +#include <stdlib.h> +#include "KviTimeUtils.h" + +namespace InitVectorEngine +{ + + bool bDidInit = false; + + int fillRandomIV(unsigned char * pIn, int iLen) + { + if(!bDidInit) + { + srand((int)kvi_unixTime()); + bDidInit = true; + } + + for(int i=0;i<iLen;i++) + pIn[i] = (unsigned char)(rand() % 256); + + return iLen; + } +}; diff --git a/src/modules/rijndael/InitVectorEngine.h b/src/modules/rijndael/InitVectorEngine.h new file mode 100644 index 000000000..ae6565990 --- /dev/null +++ b/src/modules/rijndael/InitVectorEngine.h @@ -0,0 +1,32 @@ +#ifndef _INITVECTORENGINE_H_ +#define _INITVECTORENGINE_H_ +//============================================================================= +// +// File : InitVectorEngine.h +// Creation date : Sun Feb 6 2011 22:25:10 CEST by Fabio Bas +// +// This file is part of the KVIrc irc client distribution +// Copyright (C) 2011 Fabio Bas (ctrlaltca at gmail dot com) +// +// This program is FREE software. You can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your opinion) any later version. +// +// This program is distributed in the HOPE that it will be USEFUL, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, write to the Free Software Foundation, +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// +//============================================================================= + +namespace InitVectorEngine +{ + extern int fillRandomIV(unsigned char * pIn, int iLen=8); +} + +#endif //!_INITVECTORENGINE_H_ diff --git a/src/modules/rijndael/Rijndael.cpp b/src/modules/rijndael/Rijndael.cpp index ef86971d5..620a28177 100644 --- a/src/modules/rijndael/Rijndael.cpp +++ b/src/modules/rijndael/Rijndael.cpp @@ -994,20 +994,8 @@ Rijndael::~Rijndael() // nothing here } -int Rijndael::init(Mode mode,Direction dir,const UINT8 * key,KeyLength keyLen,UINT8 * initVector) +void Rijndael::updateInitVector(UINT8 * initVector) { - // Not initialized yet - m_state = Invalid; - - // Check the mode - if((mode != CBC) && (mode != ECB) && (mode != CFB1))return RIJNDAEL_UNSUPPORTED_MODE; - m_mode = mode; - - // And the direction - if((dir != Encrypt) && (dir != Decrypt))return RIJNDAEL_UNSUPPORTED_DIRECTION; - m_direction = dir; - - // Allow to set an init vector if(initVector) { // specified init vector @@ -1022,6 +1010,23 @@ int Rijndael::init(Mode mode,Direction dir,const UINT8 * key,KeyLength keyLen,UI m_initVector[i] = 0; } } +} + +int Rijndael::init(Mode mode,Direction dir,const UINT8 * key,KeyLength keyLen,UINT8 * initVector) +{ + // Not initialized yet + m_state = Invalid; + + // Check the mode + if((mode != CBC) && (mode != ECB) && (mode != CFB1))return RIJNDAEL_UNSUPPORTED_MODE; + m_mode = mode; + + // And the direction + if((dir != Encrypt) && (dir != Decrypt))return RIJNDAEL_UNSUPPORTED_DIRECTION; + m_direction = dir; + + // Call this function even if no init vector has been specified + updateInitVector(initVector); UINT32 uKeyLenInBytes; @@ -1062,11 +1067,15 @@ int Rijndael::init(Mode mode,Direction dir,const UINT8 * key,KeyLength keyLen,UI return RIJNDAEL_SUCCESS; } -int Rijndael::blockEncrypt(const UINT8 *input,int inputLen,UINT8 *outBuffer) +int Rijndael::blockEncrypt(const UINT8 *input,int inputLen,UINT8 *outBuffer, UINT8 * initVector) { int i, k, numBlocks; UINT8 block[16], iv[4][4]; + // update the init vector only if a new one has been specified + if(initVector) + updateInitVector(initVector); + if(m_state != Valid)return RIJNDAEL_NOT_INITIALIZED; if(m_direction != Encrypt)return RIJNDAEL_BAD_DIRECTION; @@ -1147,11 +1156,15 @@ int Rijndael::blockEncrypt(const UINT8 *input,int inputLen,UINT8 *outBuffer) return 128 * numBlocks; } -int Rijndael::padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer) +int Rijndael::padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer, UINT8 * initVector) { int i, numBlocks, padLen; UINT8 block[16], *iv; + // update the init vector only if a new one has been specified + if(initVector) + updateInitVector(initVector); + if(m_state != Valid)return RIJNDAEL_NOT_INITIALIZED; if(m_direction != Encrypt)return RIJNDAEL_NOT_INITIALIZED; @@ -1205,11 +1218,15 @@ int Rijndael::padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer) return 16*(numBlocks + 1); } -int Rijndael::blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer) +int Rijndael::blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer, UINT8 * initVector) { int i, k, numBlocks; UINT8 block[16], iv[4][4]; + // update the init vector only if a new one has been specified + if(initVector) + updateInitVector(initVector); + if(m_state != Valid)return RIJNDAEL_NOT_INITIALIZED; if((m_mode != CFB1) && (m_direction == Encrypt))return RIJNDAEL_BAD_DIRECTION; @@ -1302,12 +1319,16 @@ int Rijndael::blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer) return 128*numBlocks; } -int Rijndael::padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer) +int Rijndael::padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer, UINT8 * initVector) { int i, numBlocks, padLen; UINT8 block[16]; UINT32 iv[4]; + // update the init vector only if a new one has been specified + if(initVector) + updateInitVector(initVector); + if(m_state != Valid)return RIJNDAEL_NOT_INITIALIZED; if(m_direction != Decrypt)return RIJNDAEL_BAD_DIRECTION; diff --git a/src/modules/rijndael/Rijndael.h b/src/modules/rijndael/Rijndael.h index 0a140a8c5..8f1b0c72c 100644 --- a/src/modules/rijndael/Rijndael.h +++ b/src/modules/rijndael/Rijndael.h @@ -130,24 +130,25 @@ public: // Encrypts inputLen / 128 blocks of input and puts it in outBuffer // outBuffer must be at least inputLen / 8 bytes long. // Returns the encrypted buffer length in BITS or an error code < 0 in case of error - int blockEncrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer); + int blockEncrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer, UINT8 * initVector = 0); // Input len is in BYTES! // outBuffer must be at least inputLen + 16 bytes long // Returns the encrypted buffer length in BYTES or an error code < 0 in case of error - int padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer); + int padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer, UINT8 * initVector = 0); // Input len is in BITS! // outBuffer must be at least inputLen / 8 bytes long // Returns the decrypted buffer length in BITS and an error code < 0 in case of error - int blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer); + int blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer, UINT8 * initVector = 0); // Input len is in BYTES! // outBuffer must be at least inputLen bytes long // Returns the decrypted buffer length in BYTES and an error code < 0 in case of error - int padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer); + int padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer, UINT8 * initVector = 0); protected: void keySched(UINT8 key[_MAX_KEY_COLUMNS][4]); void keyEncToDec(); void encrypt(const UINT8 a[16], UINT8 b[16]); void decrypt(const UINT8 a[16], UINT8 b[16]); + void updateInitVector(UINT8 * initVector = 0); }; #endif // COMPILE_CRYPT_SUPPORT diff --git a/src/modules/rijndael/UglyBase64.cpp b/src/modules/rijndael/UglyBase64.cpp index 23166295f..5683a3604 100644 --- a/src/modules/rijndael/UglyBase64.cpp +++ b/src/modules/rijndael/UglyBase64.cpp @@ -71,7 +71,7 @@ namespace UglyBase64 const unsigned char * oute = out + len; int ll = (len * 3) / 2; - szText.setLength(ll); + szText.setLen(ll); unsigned char * p = (unsigned char *)szText.ptr(); while(outb < oute) @@ -102,7 +102,7 @@ namespace UglyBase64 if(szText.len() % 12) { int oldL = szText.len(); - szText.setLength(szText.len() + (12 - (szText.len() % 12))); + szText.setLen(szText.len() + (12 - (szText.len() % 12))); char * padB = szText.ptr() + oldL; char * padE = szText.ptr() + szText.len(); while(padB < padE)*padB++ = 0; diff --git a/src/modules/rijndael/libkvirijndael.cpp b/src/modules/rijndael/libkvirijndael.cpp index 88857c077..7544120ba 100644 --- a/src/modules/rijndael/libkvirijndael.cpp +++ b/src/modules/rijndael/libkvirijndael.cpp @@ -30,9 +30,9 @@ #include "kvi_debug.h" #include "KviLocale.h" #include "KviControlCodes.h" -#include "KviTimeUtils.h" #include "UglyBase64.h" - +#include "InitVectorEngine.h" + //#warning "Other engines: mircStrip koi2win colorizer lamerizer etc.." /* @@ -118,34 +118,64 @@ } } + KviCString szTmpEncryptKey = KviCString(encKey,encKeyLen); + KviCString szTmpDecryptKey = KviCString(decKey,decKeyLen); + if(kvi_strEqualCIN("cbc:",szTmpEncryptKey.ptr(),4) && (szTmpEncryptKey.len() > 4)) + { + m_bEncryptCBC = true; + szTmpEncryptKey.cutLeft(4); + } else { + m_bEncryptCBC = false; + } + if(kvi_strEqualCIN("cbc:",szTmpDecryptKey.ptr(),4) && (szTmpDecryptKey.len() > 4)) + { + m_bDecryptCBC = true; + szTmpDecryptKey.cutLeft(4); + } else { + m_bDecryptCBC = false; + } + int defLen = getKeyLen(); - char * encryptKey = (char *)KviMemory::allocate(defLen); - char * decryptKey = (char *)KviMemory::allocate(defLen); + szTmpEncryptKey.padRight(defLen); + szTmpDecryptKey.padRight(defLen); - if(encKeyLen > defLen)encKeyLen = defLen; - KviMemory::move(encryptKey,encKey,encKeyLen); - if(encKeyLen < defLen)KviMemory::set(encryptKey + encKeyLen,'0',defLen - encKeyLen); + m_pEncryptCipher = new Rijndael(); - if(decKeyLen > defLen)decKeyLen = defLen; - KviMemory::move(decryptKey,decKey,decKeyLen); - if(decKeyLen < defLen)KviMemory::set(decryptKey + decKeyLen,'0',defLen - decKeyLen); + /* FIXME Historically KVirc's Rijndael engine worked always in CBC mode, + * but before KVIrc 4.2 no IV was used on the first block + * This degraded the cypher to a pseudo-ECB mode, making the sequential + * block XOR useless. Once KVIrc < 4.2 is deprecated, we can stop supporting + * that broken implementation and just let it go ECB, patching the cypher's + * init method call first parameter lieke this: + * + * m_bEncryptCBC ? Rijndael::CBC : Rijndael::ECB, + */ - m_pEncryptCipher = new Rijndael(); - int retVal = m_pEncryptCipher->init(Rijndael::CBC,Rijndael::Encrypt,(unsigned char *)encryptKey,getKeyLenId()); - KviMemory::free(encryptKey); + int retVal = m_pEncryptCipher->init( + Rijndael::CBC, + Rijndael::Encrypt, + (unsigned char *)szTmpEncryptKey.ptr(), + getKeyLenId()); if(retVal != RIJNDAEL_SUCCESS) { - KviMemory::free(decryptKey); delete m_pEncryptCipher; m_pEncryptCipher = 0; setLastErrorFromRijndaelErrorCode(retVal); return false; } + /* FIXME read up two dozen lines + * + * m_bDecryptCBC ? Rijndael::CBC : Rijndael::ECB, + */ + m_pDecryptCipher = new Rijndael(); - retVal = m_pDecryptCipher->init(Rijndael::CBC,Rijndael::Decrypt,(unsigned char *)decryptKey,getKeyLenId()); - KviMemory::free(decryptKey); + retVal = m_pDecryptCipher->init( + Rijndael::CBC, + Rijndael::Decrypt, + (unsigned char *)szTmpDecryptKey.ptr(), + getKeyLenId()); if(retVal != RIJNDAEL_SUCCESS) { delete m_pEncryptCipher; @@ -184,8 +214,14 @@ } int len = (int)kvi_strLen(plainText); char * buf = (char *)KviMemory::allocate(len + 16); + unsigned char * iv = 0; + if(m_bEncryptCBC) + { + iv = (unsigned char *)KviMemory::allocate(8); + InitVectorEngine::fillRandomIV(iv, 8); + } - int retVal = m_pEncryptCipher->padEncrypt((const unsigned char *)plainText,len,(unsigned char *)buf); + int retVal = m_pEncryptCipher->padEncrypt((const unsigned char *)plainText,len,(unsigned char *)buf, iv); if(retVal < 0) { KviMemory::free(buf); @@ -193,6 +229,16 @@ return KviCryptEngine::EncryptError; } + if(m_bEncryptCBC) + { + // prepend the iv to the cyphered text + KviMemory::reallocate(buf, retVal + 8); + KviMemory::move(buf + 8, buf, retVal); + KviMemory::move(buf, iv, 8); + KviMemory::free(iv); + retVal+=8; + } + if(!binaryToAscii(buf,retVal,outBuffer)) { KviMemory::free(buf); @@ -240,9 +286,20 @@ if(!asciiToBinary(inBuffer,&len,&binary))return KviCryptEngine::DecryptError; char * buf = (char *)KviMemory::allocate(len + 1); + unsigned char * iv = 0; + if(m_bEncryptCBC) + { + // extract the IV from the cyphered string + len-=8; + iv = (unsigned char *)KviMemory::allocate(8); + KviMemory::move(iv, binary, 8); + KviMemory::move(binary, binary + 8, len); + KviMemory::reallocate(binary, len); + } - int retVal = m_pDecryptCipher->padDecrypt((const unsigned char *)binary,len,(unsigned char *)buf); + int retVal = m_pDecryptCipher->padDecrypt((const unsigned char *)binary,len,(unsigned char *)buf, iv); KviMemory::free(binary); + KviMemory::free(iv); if(retVal < 0) { @@ -386,13 +443,19 @@ m_szEncryptKey = KviCString(encKey,encKeyLen); m_szDecryptKey = KviCString(decKey,decKeyLen); if(kvi_strEqualCIN("cbc:",m_szEncryptKey.ptr(),4) && (m_szEncryptKey.len() > 4)) + { + m_bEncryptCBC = true; m_szEncryptKey.cutLeft(4); - else + } else { m_bEncryptCBC = false; + } if(kvi_strEqualCIN("cbc:",m_szDecryptKey.ptr(),4) && (m_szDecryptKey.len() > 4)) + { + m_bDecryptCBC = true; m_szDecryptKey.cutLeft(4); - else + } else { m_bDecryptCBC = false; + } return true; } @@ -474,7 +537,7 @@ if(plain.len() % 8) { int oldL = plain.len(); - plain.setLength(plain.len() + (8 - (plain.len() % 8))); + plain.setLen(plain.len() + (8 - (plain.len() % 8))); char * padB = plain.ptr() + oldL; char * padE = plain.ptr() + plain.len(); while(padB < padE)*padB++ = 0; @@ -499,7 +562,7 @@ int len; UglyBase64::decode(encoded, &buf, &len); - plain.setLength(len); + plain.setLen(len); BlowFish bf((unsigned char *)m_szDecryptKey.ptr(),m_szDecryptKey.len()); bf.ResetChain(); bf.Decrypt(buf,(unsigned char *)plain.ptr(),len,BlowFish::ECB); @@ -515,7 +578,7 @@ if(plain.len() % 8) { int oldL = plain.len(); - plain.setLength(plain.len() + (8 - (plain.len() % 8))); + plain.setLen(plain.len() + (8 - (plain.len() % 8))); char * padB = plain.ptr() + oldL; char * padE = plain.ptr() + plain.len(); while(padB < padE)*padB++ = 0; @@ -524,18 +587,7 @@ int ll = plain.len() + 8; unsigned char * in = (unsigned char *)KviMemory::allocate(ll); - // choose an IV - static bool bDidInit = false; - - int t = (int)kvi_unixTime(); - - if(!bDidInit) - { - srand(t); - bDidInit = true; - } - - for(int i=0;i<8;i++)in[i] = (unsigned char)(rand() % 256); + InitVectorEngine::fillRandomIV(in, 8); KviMemory::copy(in+8,plain.ptr(),plain.len()); @@ -577,7 +629,7 @@ return false; } - plain.setLength(len); + plain.setLen(len); BlowFish bf((unsigned char *)m_szDecryptKey.ptr(),m_szDecryptKey.len()); bf.ResetChain(); bf.Decrypt((unsigned char *)tmpBuf,(unsigned char *)plain.ptr(),len,BlowFish::CBC); diff --git a/src/modules/rijndael/libkvirijndael.h b/src/modules/rijndael/libkvirijndael.h index 96519322c..3985fee67 100644 --- a/src/modules/rijndael/libkvirijndael.h +++ b/src/modules/rijndael/libkvirijndael.h @@ -42,6 +42,8 @@ private: Rijndael * m_pEncryptCipher; Rijndael * m_pDecryptCipher; + bool m_bEncryptCBC; + bool m_bDecryptCBC; public: virtual bool init(const char *encKey,int encKeyLen,const char *decKey,int decKeyLen); virtual KviCryptEngine::EncryptResult encrypt(const char * plainText,KviCString &outBuffer); |
