Hello all,
There seems to be a problem when one wants to use
the DES encryption algorithm within the freeBSD
platform.
When one compiles this :
*/
#define _C_PLUS_PLUS
// General headers.
#include
#include
#define _XOPEN_SOURCE
#include
#ifdef _LINUX
#include
#endif
// Software package specific headers.
#include "type.h"
#include "desEncDec.h"
#include "desEncDecP.h"
const U8 TDesEncryptorDecryptor::mNR_NIBBLES_PER_WORD = 4;
// Class constructor.
TDesEncryptorDecryptor::TDesEncryptorDecryptor()
: mNR_BITS_PER_NIBBLE(4),
mNR_BYTES_PER_DES_BLOCK(NR_BITS_PER_DES_BLOCK / 8),
mNR_DWORDS_PER_DES_BLOCK(mNR_BYTES_PER_DES_BLOCK / 4),
mNR_BITS_PER_DWORD(32),
mBase16ToBase2Mask(0x8000),
mByteToBase2Mask(0x8000),
mSpace(0x20)
{ // TDesEncryptorDecryptor::TDesEncryptorDecrypt
} // TDesEncryptorDecryptor::TDesEncryptorDecrypt
// Class method helpers.
VOID TDesEncryptorDecryptor::Base16ToBase2(U32 Word,
U8 NrNibbles,
PC8 pBase2)
{ // TDesEncryptorDecryptor::Base16ToBase2
if (NrNibbles < mNR_NIBBLES_PER_WORD)
mBase16ToBase2Mask = mBase16ToBase2Mask >> (NrNibbles *
mNR_BITS_PER_NIBBLE);
U8 j;
for (j = 0;j < NrNibbles * mNR_BITS_PER_NIBBLE;j++) {
if (Word & mBase16ToBase2Mask)
pBase2[mBase2Pos++] = '\1';
else
pBase2[mBase2Pos++] = '\0';
Word = Word << 1;
}
} // TDesEncryptorDecryptor::Base16ToBase2
VOID TDesEncryptorDecryptor::base16KeytoBase2Key(PC8 pKeyWord,
U8 NrNibbles)
{ // TDesEncryptorDecryptor::base16KeytoBase2Key
C8 pWord[mNR_NIBBLES_PER_WORD + 1];
strncpy(pWord,pKeyWord,NrNibbles);
pWord[NrNibbles] = 0x0;
U32 Word = strtol(pWord,0,16);
Base16ToBase2(Word,NrNibbles,m_pBase2Key);
} // TDesEncryptorDecryptor::base16KeytoBase2Key
VOID TDesEncryptorDecryptor::Base16KeyToBase2Key(PC8 pKey)
{ // TDesEncryptorDecryptor::Base16KeyToBase2Key
U8 NrNibblesPerKey = strlen(pKey);
U8 NrWordsPerKey = NrNibblesPerKey / mNR_NIBBLES_PER_WORD;
U8 NrResNibblesInKey = NrNibblesPerKey % mNR_NIBBLES_PER_WORD;
mBase2Pos = 0;
U8 i;
for (i = 0;i < NrWordsPerKey;i++) {
base16KeytoBase2Key(pKey,mNR_NIBBLES_PER_WORD);
pKey += mNR_NIBBLES_PER_WORD;
}
if (NrResNibblesInKey)
base16KeytoBase2Key(pKey,NrResNibblesInKey);
U8 NrBitsPerKey = NrNibblesPerKey * mNR_BITS_PER_NIBBLE;
U8 NrResBitsPerKey = NR_BITS_PER_DES_KEY - NrBitsPerKey;
memmove(m_pBase2Key + NrResBitsPerKey - 1,m_pBase2Key,NrBitsPerKey);
memset(m_pBase2Key,0,NrResBitsPerKey);
} // TDesEncryptorDecryptor::Base16KeyToBase2Key
// Class method.
VOID TDesEncryptorDecryptor::SetKey(PC8 pKey)
{ // TDesEncryptorDecryptor::SetKey
Base16KeyToBase2Key(pKey);
setkey(m_pBase2Key);
} // TDesEncryptorDecryptor::SetKey
// Class method helpers.
VOID TDesEncryptorDecryptor::RecalibrateBuffer(PDesAlgorithmBuffer
pDesAlgoBuf,
U8 & RecalibrateDelta)
{ // TDesEncryptorDecryptor::RecalibrateBuffer
U32 BufLen = pDesAlgoBuf->Length;
U8 LengthResidu = BufLen % mNR_BYTES_PER_DES_BLOCK;
if (LengthResidu) {
RecalibrateDelta = mNR_BYTES_PER_DES_BLOCK - LengthResidu;
U32 RecalibratedBufLen = BufLen + RecalibrateDelta;
pDesAlgoBuf->pBuffer = (PU8)
realloc(pDesAlgoBuf->pBuffer,RecalibratedBufLen);
memset(pDesAlgoBuf->pBuffer + BufLen,mSpace,RecalibrateDelta);
pDesAlgoBuf->Length = RecalibratedBufLen;
}
else
RecalibrateDelta = 0;
} // TDesEncryptorDecryptor::RecalibrateBuffer
VOID TDesEncryptorDecryptor::StripHeaderFromBuffer(PDesAlgorithmBuffer
pDesAlgoBuf,
U8 & RecalibrateDelta)
{ // TDesEncryptorDecryptor::StripHeaderFromBuffer
RecalibrateDelta = * pDesAlgoBuf->pBuffer;
pDesAlgoBuf->Length -= sizeof(U8);
pDesAlgoBuf->pBuffer += sizeof(U8);
} // TDesEncryptorDecryptor::StripHeaderFromBuffer
VOID TDesEncryptorDecryptor::ReorganizeBytes(U32 & dWord)
{ // TDesEncryptorDecryptor::ReorganizeBytes
U8 p_dWord[mNR_BITS_PER_DWORD / 8];
PU8 pByteInDWord = (PU8) (& dWord);
U8 NrBytesPerDWord = mNR_BITS_PER_DWORD / 8;
U8 i;
for (i = 0;i < NrBytesPerDWord;i++)
p_dWord[NrBytesPerDWord - 1 - i] = *pByteInDWord++;
dWord = * ((PU32) p_dWord);
} // TDesEncryptorDecryptor::ReorganizeBytes
VOID TDesEncryptorDecryptor::ByteBlockToBase2Block(PU8 pBuffer)
{ // TDesEncryptorDecryptor::ByteBlockToBase2Block
mBase2Pos = 0;
U8 i;
for (i = 0;i < mNR_DWORDS_PER_DES_BLOCK;i++) {
U32 dWord = *((PU32) pBuffer);
ReorganizeBytes(dWord);
U8 j;
for (j = 0;j < mNR_BITS_PER_DWORD;j++) {
if (dWord & mByteToBase2Mask)
m_pBase2Block[mBase2Pos++] = '\1';
else
m_pBase2Block[mBase2Pos++] = '\0';
dWord = dWord << 1;
}
pBuffer += sizeof