Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 10 Aug 2003 16:30:39 +0200
From:      Pascal Pype <pascal.pype@skynet.be>
To:        questions@freebsd.org
Subject:   DES encryption.
Message-ID:  <3F36570F.9080300@skynet.be>

next in thread | raw e-mail | index | archive | help
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 <stdlib.h>
#include <string.h>

#define _XOPEN_SOURCE
#include <unistd.h>
#ifdef _LINUX
#include <crypt.h>
#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(0x80000000),
     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(U32);

   }

} // TDesEncryptorDecryptor::ByteBlockToBase2Block

VOID TDesEncryptorDecryptor::ReorganizeBits()
{ // TDesEncryptorDecryptor::ReorganizeBits

   C8 Tmp;

   U8 i;
   U8 NrBitsPerWord = mNR_BITS_PER_DWORD / 2;
   for (i = 0;i < NrBitsPerWord;i++) {

     Tmp = m_pBase2Block[i];
     m_pBase2Block[i] = m_pBase2Block[mNR_BITS_PER_DWORD - i - 1];
     m_pBase2Block[mNR_BITS_PER_DWORD - i - 1] = Tmp;

     Tmp = m_pBase2Block[i + mNR_BITS_PER_DWORD];
     m_pBase2Block[i + mNR_BITS_PER_DWORD] = m_pBase2Block[2 * 
mNR_BITS_PER_DWORD - i - 1];
     m_pBase2Block[2 * mNR_BITS_PER_DWORD - i - 1] = Tmp;

   }

} // TDesEncryptorDecryptor::ReorganizeBits

VOID TDesEncryptorDecryptor::Base2BlockToByteBlock(PU8 pBuffer)
{ // TDesEncryptorDecryptor::Base2BlockToByteBlock

   mBase2Pos = 0;

   ReorganizeBits();

   U8 i;
   for (i = 0;i < mNR_DWORDS_PER_DES_BLOCK;i++) {

     U32 dWord = 0;
     U32 Weight = 1;

     U8 j;
     for (j = 0;j < mNR_BITS_PER_DWORD;j++) {

       if (m_pBase2Block[mBase2Pos++] == '\1')
	dWord += Weight;
       Weight *= 2;

     }

     ReorganizeBytes(dWord);

     memcpy(pBuffer,& dWord,sizeof(U32));

     pBuffer += sizeof(U32);

   }

} // TDesEncryptorDecryptor::Base2BlockToByteBlock

VOID TDesEncryptorDecryptor::AddHeaderToBuffer(PDesAlgorithmBuffer 
pDesAlgoBuf,
					       U8 RecalibrateDelta)
{ // TDesEncryptorDecryptor::AddHeaderToBuffer

   pDesAlgoBuf->pBuffer = (PU8) 
realloc(pDesAlgoBuf->pBuffer,pDesAlgoBuf->Length + sizeof(U32) + 
sizeof(U8));
   memmove(pDesAlgoBuf->pBuffer + sizeof(U32) + 
sizeof(U8),pDesAlgoBuf->pBuffer,pDesAlgoBuf->Length);
   pDesAlgoBuf->Length += sizeof(U8);
   memcpy(pDesAlgoBuf->pBuffer,& pDesAlgoBuf->Length,sizeof(U32));
   memcpy(pDesAlgoBuf->pBuffer + sizeof(U32),& RecalibrateDelta,sizeof(U8));
   pDesAlgoBuf->Length += sizeof(U32);

} // TDesEncryptorDecryptor::AddHeaderToBuffer

VOID TDesEncryptorDecryptor::IterateOverBuffer(PDesAlgorithmBuffer 
pDesAlgoBuf)					
{ // TDesEncryptorDecryptor::IterateOverBuffer

   U8 RecalibrateDelta;

   if (m_fDecr)
     StripHeaderFromBuffer(pDesAlgoBuf,RecalibrateDelta);
   else
     RecalibrateBuffer(pDesAlgoBuf,RecalibrateDelta);

   U32 i;
   U32 NrDesBlocks = pDesAlgoBuf->Length / mNR_BYTES_PER_DES_BLOCK;
   PU8 pBuf = pDesAlgoBuf->pBuffer;

   for (i = 0;i < NrDesBlocks;i++) {

     ByteBlockToBase2Block(pBuf);
     encrypt(m_pBase2Block,m_fDecr);
     Base2BlockToByteBlock(pBuf);

     pBuf += mNR_BYTES_PER_DES_BLOCK;

   }

   if (m_fDecr)
     pDesAlgoBuf->Length -= RecalibrateDelta;
   else
     AddHeaderToBuffer(pDesAlgoBuf,RecalibrateDelta);

} // TDesEncryptorDecryptor::IterateOverBuffer

VOID TDesEncryptorDecryptor::FreeDesAlgorithmBuffer(PDesAlgorithmBuffer 
pDesAlgoBuf)
{ // TDesEncryptorDecryptor::FreeDesAlgorithmBuffer

   if (m_fDecr)
     free(pDesAlgoBuf->pBuffer - sizeof(U8));
   else
     free (pDesAlgoBuf->pBuffer);

} // TDesEncryptorDecryptor::FreeDesAlgorithmBuffer

// Class methods.

VOID TDesEncryptorDecryptor::Encrypt(PDesAlgorithmBuffer pDesAlgoBuf)
{ // TDesEncryptorDecryptor::Encrypt

   m_fDecr = 0;
   IterateOverBuffer(pDesAlgoBuf);

} // TDesEncryptorDecryptor::Encrypt

VOID TDesEncryptorDecryptor::Decrypt(PDesAlgorithmBuffer 
pDesAlgoBuf)				
{ // TDesEncryptorDecryptor::Decrypt

   m_fDecr = 1;
   IterateOverBuffer(pDesAlgoBuf);

} // TDesEncryptorDecryptor::Decrypt

// Interface commands.

HDesEncryptorDecryptor OpenDesEncryptorDecryptor()
{ // OpenDesEncryptorDecryptor

   PDesEncryptorDecryptor pDesEncDec = new TDesEncryptorDecryptor();

   return (HDesEncryptorDecryptor) pDesEncDec;

} // OpenDesEncryptorDecryptor

VOID CloseDesEncryptorDecryptor(HDesEncryptorDecryptor hDesEncDec)
{ // CloseDesEncryptorDecryptor

   PDesEncryptorDecryptor pDesEncDec = (PDesEncryptorDecryptor) hDesEncDec;

   delete pDesEncDec;

} // CloseDesEncryptorDecrptor

VOID SetKey(HDesEncryptorDecryptor hDesEncDec,
	    PC8 pKey)
{ // SetKey

   PDesEncryptorDecryptor pDesEncDec = (PDesEncryptorDecryptor) hDesEncDec;

   pDesEncDec->SetKey(pKey);

} // SetKey

VOID Encrypt(HDesEncryptorDecryptor hDesEncDec,
	     PDesAlgorithmBuffer pDesAlgoBuf)
{ // Encrypt

   PDesEncryptorDecryptor pDesEncDec = (PDesEncryptorDecryptor) hDesEncDec;

   pDesEncDec->Encrypt(pDesAlgoBuf);

} // Encrypt

VOID Decrypt(HDesEncryptorDecryptor hDesEncDec,
	     PDesAlgorithmBuffer pDesAlgoBuf)
{ // Decrypt

   PDesEncryptorDecryptor pDesEncDec = (PDesEncryptorDecryptor) hDesEncDec;

   pDesEncDec->Decrypt(pDesAlgoBuf);

} // Decrypt

VOID FreeDesAlgorithmBuffer(HDesEncryptorDecryptor hDesEncDec,
			    PDesAlgorithmBuffer pDesAlgoBuf)
{ // FreeDesAlgorithmBuffer

   PDesEncryptorDecryptor pDesEncDec = (PDesEncryptorDecryptor) hDesEncDec;

   pDesEncDec->FreeDesAlgorithmBuffer(pDesAlgoBuf);

} // FreeDesAlgorithmBuffer

one gets the following warning message at runtime:


"WARNING! encrypt(3) not present in the system".

I use freeBSD release 4.8.

My kind regards , Pype Pascal.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3F36570F.9080300>