#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <io.h>
#include <conio.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>



#include "defs.h"
#include "..\LibUrne\LibUrne.h"
#include "urnbe.h"
/*#include "lcd.h"*/

char type[2],mode[2],sourceori[50],destineori[50],source[50],destine[50];
unsigned long cipherDataLen,plainDataLen;
unsigned char floppy[60];
unsigned char wrkspc[60];
int ret,count;             /* holds the     return code of important method calls. */
unsigned long matches;        /* counts the number of equal bytes in the plaintext and the decrypted ciphertext. */
unsigned char iv[100];
unsigned char ivtmp[100];
unsigned char derivedKey[100];
unsigned char decryptedMacKey[100];
unsigned char decryptedEncKey[100];
unsigned char *decrypted = NULL;
unsigned char hashResult[100];
unsigned char macResult[100];
unsigned char *authenticatedEncryptedPlaintext;
unsigned char dataFromFloppy[50];
unsigned char derivedKeyMini[100];
unsigned char decryptedMacKeyMini[100];
unsigned char decryptedEncKeyMini[100];
unsigned char fullPasswordMini[12];

struct
{
    char nom[14];
}stru_fichier[100];

int hashResultLen = sizeof (hashResult);
keyInstance encKeyInst;       /* state of the encryption engine */
cipherInstance encCipherInst; /* specifies the encryption mode */
keyInstance decKeyInst;       /* state of the decryption engine */
cipherInstance decCipherInst; /* specifies the decryption mode */
struct dosdate_t ddate;
struct dostime_t dtime;
struct find_t find;

extern unsigned char CMinipassword[12];
extern char fullPassword[11];
extern szMessage *pszMessage_Point;
extern char GCOLOR;
extern unsigned char *pszplainData;
extern unsigned char *pszcipherData;
extern unsigned char szDataFloppy[50];

/* << 9.03*/
/* << 9.02 */
/*int Encrypte_Fe_Dsk(void);
int Decrypte_Fe_Dsk(void);
extern int rijn2(int, char **);*/
/* 9.02 >> */
/* 9.03 >>*/

/***************************************************************************/
/* << 9.01b */
int sort_function(const void *_a, const void *_b)
{
	return(strcmp(_a, _b));
}

void SortFiles(void)
{
	qsort((void *)stru_fichier, count, 14, sort_function);
}
/* 9.01b >> */
/*****************************************************************************/
/*									C R C									*/
/*****************************************************************************/
/*
   <FUNCTION>
		<NAME>Crc_Rijndael</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Use Rijndael algorithm to carry out Cyclic Redundancy Check (CRC) or encryption data
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Mode,Type,src,dest</NAME>
			<TYPE>char *,char *,char *,char *</TYPE>
			<IO>IN,IN,IN,IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int Crc_Rijndael(char *Mode,char *Type,char *src,char *dest)
{
 int i;
 unsigned char strtmp[20];

  strcpy(type,Type);
  strcpy(mode,Mode);
  strcpy(sourceori,src);
  strcpy(destineori,dest);

  ret = Remplit();
  if(ret==-1)
     return(ret);

  /*<< 9.03*/
  if(strcmp(type,"2") == 0)
  {
	  /* << 9.01c */
	  SortFiles();
	  /* 9.01c >> */
  }
  /* >>*/

  if((mode[0] == 'w') || (mode[0] == 'W'))
  {

       iv[0] = Minicodage[0];
       iv[2] = Minicodage[1];
       iv[4] = Minicodage[2];
       iv[6] = Minicodage[3];

       makeKeyBinary (&encKeyInst, DIR_ENCRYPT, decryptedEncKey);
       cipherInitBinary (&encCipherInst, iv);
       ret = Encrypte();
       if(ret== -1)
	 return(ret);

       CalculMac();
       ret = Wmac();
       if(ret== -1)
	 return(ret);
  }
  else
  {
	/* << 9.01b */
	ret = MoveTbl();
	if(ret== -1)
	    return(ret);
    /* >> */
	makeKeyBinary (&decKeyInst, DIR_DECRYPT, decryptedEncKey);
	cipherInitBinary (&decCipherInst, iv);

    ret = CalculMac();
    if(ret== -1)
        return(ret);

	sprintf(source,"%s\\be.%s",sourceori,Type);
	ret = Rfile(source);
	if(ret== -1)
	    return(ret);


        for (i = 0; i < macResultLen;i++)
            if(macResult[i] != wrkspc[i+16])
		return(-1);

  }

return(1);
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Rfloppy</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Read the file Floppy.be containing password used in Rijndael algorithm
		</DESCRIPTION>
        <PARAMETER>
			<NAME>msg</NAME>
			<TYPE>char *</TYPE>
			<IO>IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int Rfloppy(char *msg)
{
  int fd;
  int handel_Read;
  char szMsgError[80];

  strcpy(floppy,"a:");
  strcat(floppy,"Floppy.be");

  if((fd = open( floppy, O_BINARY | O_RDONLY)) == -1)
	 {
	  sprintf(szMsgError, "Error opening file %s in Rfloppy.\nFile source %s\n",floppy, __FILE__);
	  Erreur(szMsgError);
	  return(-1);
	 }
  handel_Read =read(fd,msg,80);
  if(handel_Read == -1)
	 {
	  sprintf(szMsgError,"Error reading from the file %s in Rfloppy function.\nFile source: %s\n",
								floppy, __FILE__);
	  Erreur(szMsgError);
	  return(-1);
	 }
  close(fd);
  return(1);
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Rfile</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Read the file containing the message authentication code coming from
		the control PC
		</DESCRIPTION>
        <PARAMETER>
			<NAME>fich</NAME>
			<TYPE>char *</TYPE>
			<IO>IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int Rfile(char *fich)
{
  int fd,i,j;
  unsigned char Bffer[100];
  char szMsgError[80];
  int handel_Read;

  fd = open( fich, O_RDWR | O_BINARY);
  if(fd == -1)
	 {
	  sprintf(szMsgError,"Error opening file %s in Rfile.\nFile source %s\n",fich, __FILE__);
	  Erreur(szMsgError);
	  return(-1);
	 }

  handel_Read = read(fd,Bffer,64);
  if(handel_Read == -1)
	 {
	  sprintf(szMsgError,"Error reading from the file %s in Rfile function.\nFile source: %s\n",
								fich, __FILE__);
	  Erreur(szMsgError);
	  return(-1);
	 }
  close(fd);

  Bffer[handel_Read] = 0x00;

  for ( i = 0 ; i <  handel_Read ; i++)
      {
	 if(Bffer[i] < 58)
	      Bffer[i] -= 48;
	  else
	      Bffer[i] -= 55;
      }

  for ( i = 0, j = 0; j < handel_Read / 2; j++)
      {
	   wrkspc[j]   = Bffer[i++] << 4;
	   wrkspc[j]   = Bffer[i++] | wrkspc[j];
      }
      wrkspc[j] = 0x00;
  return(1);
}
/*****************************************************************************/
/*
   <FUNCTION>
		<NAME>Remplit</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Select and load all files used for the CRC
		</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int Remplit()
{
   int iRet;
   char szMsgError[80];

   strcpy(source,sourceori);
   strcat(source,"\\");
   strcat(source,"*.*");
   count = 0;

   if(strcmp(type,"3") == 0)
   {
     strcpy(stru_fichier[count].nom,"TABLE.URN");
     count++;
   }
   else
   {
     iRet = _dos_findfirst( source, 0x00, &find);
     if(iRet)
     {
		sprintf(szMsgError,"Files not found in Remplit function.\nFile source %s\n", __FILE__);
		Erreur(szMsgError);
		return(-1);
     }

     while(!iRet)
     {
		   if(strnicmp(find.name,"BE",2) != 0)
		   {
				  if(strcmp(type,"2") == 0)
				  {
					 if((strcmp(find.name,"TABLE.URN") != 0) &&
					  (strcmp(find.name,"FE_DSK.BK") !=0)&&
					   (strcmp(find.name,"FE_DSK") !=0)&&
				   /* << 9.03 */
					   /*(strcmp(find.name,"FE_DSK") !=0)&&*/
				   /* >> */
/* << 9.02 */
					   (strcmp(find.name,"FE_DSK.CRP") !=0)&&
/* 9.02 >> */
						(strcmp(find.name,"LOG.ERR") !=0) &&
						(strcmp(find.name,"CTRLVOTE") !=0))
					 {
						strcpy(stru_fichier[count].nom,find.name);
						count++;
					 }
				  }
				  else
				  {
					  strcpy(stru_fichier[count].nom,find.name);
					  count++;
				  }
		   }

		   iRet = _dos_findnext(&find);
     }

   }

return(1);

}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Wmac</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Save the Message authentication code in the file
		</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int Wmac()
{
  int fd, i;
  int handel_Write;
  unsigned char Bffer[100];
  char szMsgError[80];
  unsigned char Cai[3];

  sprintf(destine,"%s\\be.%s",destineori,type);
  unlink(destine);

  fd = open( destine, O_BINARY | O_WRONLY | O_CREAT | O_EXCL,
					S_IREAD | S_IWRITE );

  if(fd == -1)
	 {
	  sprintf(szMsgError,"Error opening file %s in Wmac function.\nFile source: %s\n",
								destine, __FILE__);
	  Erreur(szMsgError);
	  return(-1);
	 }

  Bffer[0] = 0x00;

  for(i = 0; i < 16;i++)
	{
	  sprintf(Cai, "%02X",(unsigned char) ivtmp[i]);
	  strcat(Bffer,Cai);
	}

  for(i = 0; i < macResultLen;i++)
	{
	  sprintf(Cai, "%02X",(unsigned char) macResult[i]);
	  strcat(Bffer,Cai);
	}

  handel_Write = write(fd,Bffer,strlen(Bffer));
  if(handel_Write != strlen(Bffer))
	 {
	  sprintf(szMsgError,"Error writing to the file %s in Wmac function.\nFile source: %s\n",
								destine, __FILE__);
	  Erreur(szMsgError);
	  return(-1);
	 }
  close(fd);
  return(1);
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>CalculMac</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Determine the Message authentication code (MAC)
		</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int CalculMac()
{
int  handel_Read,fd,j,x,i;
unsigned char Buffer [1030];
char szMsgError[80];


j = 0;
pszplainData[j] = 0x00;

for (x = 0; x < count;x++)
  {
     if(strcmp(type,"3") == 0)
       strcpy(source,destineori);
     else
       strcpy(source,sourceori);

     strcat(source,"\\");
     strcat(source,stru_fichier[x].nom);

     fd = open(source, O_RDWR | O_BINARY);
     if(fd == -1)
	 {
		  sprintf(szMsgError,"Error opening file %s in CalculMac function.\nFile source: %s\n",
								source, __FILE__);
		  Erreur(szMsgError);
		  return(-1);
	 }

      while((handel_Read = read(fd,Buffer,1024)) > 0)
       {
			/* Leds */

			if(GCOLOR ==_ON_GREEN)
				  GCOLOR = _ON_RED;
			else
				  GCOLOR = _ON_GREEN;
			Light_LED(GCOLOR);

			computeMac (macResult, macResultLen, Buffer, handel_Read, decryptedMacKey);
			for (i = 0; i < macResultLen;i++,j++)
				pszplainData[j] = macResult[i];
       }
     pszplainData[j] = 0x00;
     close(fd);
  }
   computeMac (macResult, macResultLen, pszplainData, j, decryptedMacKey);
   return(1);
}
/************************************************/
/*
   <FUNCTION>
		<NAME>Calcul_Crc</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Determine the CRC of the vote results
		</DESCRIPTION>
        <PARAMETER>
			<NAME>Carte, pos</NAME>
			<TYPE>char *, int</TYPE>
			<IO>IN,IN</IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Calcul_Crc(char *Carte, int pos)
{
 int i,j;
 char Ca[3], Cai[3], Buff[70];
 FILE *Fichier;

 Ca[0] = 0x80;
 Ca[1] = 0x80;
 Ca[2] = 0x00;

 for(i = 0; i < C_VOTE_MAX_BYTE + _C_TEST_MAX_BYTE;Buff[i] = Carte[pos + i],i++);
 Buff[C_VOTE_MAX_BYTE + _C_TEST_MAX_BYTE] = 0x00;

 computeMac (macResult, macResultLen, Buff, C_VOTE_MAX_BYTE + _C_TEST_MAX_BYTE, decryptedMacKeyMini);

 for(i = 0,j = 4; i < 4;i++,j++)
	{
	  Ca[0] = Ca[0] ^ (unsigned char) macResult[i];
	  Ca[1] = Ca[1] ^ (unsigned char) macResult[j];
	}
	  sprintf(Cai, "%02X",(unsigned char) Ca[0]);
	  strcpy(Crc_Calcul,Cai);
	  sprintf(Cai, "%02X",(unsigned char) Ca[1]);
	  strcat(Crc_Calcul,Cai);
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Encrypte</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>Encrypt data</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int Encrypte()
{
int  handel_Read, handel_Write,Carry_Read,Carry_Write;
char szMsgError[80];

     strcpy(source,sourceori);
     strcat(source,"\\");
     strcat(source,"TABLE.URN");
     pszplainData[0] = 0x00;
     pszcipherData[0] = 0x00;

     handel_Read = open(source, O_RDONLY | O_BINARY);
     if(handel_Read == -1)
	 {
	  sprintf(szMsgError,"Error opening file %s in Encrypte function.\nFile source %s\n",source, __FILE__);
	  Erreur(szMsgError);
	  return(-1);
	 }

     strcpy(destine,destineori);
     strcat(destine,"\\");
     strcat(destine,"TABLE.URN");

     Carry_Read = read(handel_Read,pszplainData,PlainDataLen);
     if(Carry_Read == -1)
     {
		 sprintf(szMsgError,"Error reading from the file %s in Encrypte function.\nFile source %s\n",source, __FILE__);
		 Erreur(szMsgError);
		 /* << 9.02 */
     close(handel_Read);
     /* 9.02 >> */
		 return(-1);
     }
     close(handel_Read);
     unlink(destine);

     handel_Write = open( destine, O_BINARY | O_WRONLY | O_CREAT | O_EXCL,
					S_IREAD | S_IWRITE );
     if(handel_Write == -1)
	 {
	  sprintf(szMsgError,"Error opening file %s in Encrypte function.\nFile source %s\n",destine, __FILE__);
	  Erreur(szMsgError);
	  return(-1);
	 }

     cipherDataLen = encryptData (pszcipherData, &encCipherInst, &encKeyInst, pszplainData,
		 Carry_Read);
     Carry_Write = write(handel_Write,pszcipherData,cipherDataLen);
     if(Carry_Write != cipherDataLen)
	 {
	  sprintf(szMsgError,"Error writing to the file %s in Encrypte function.\nFile source %s\n",destine, __FILE__);
	  Erreur(szMsgError);
		 /* << 9.02 */
     close(handel_Write);
     /* 9.02 >> */
	  return(-1);
	 }
     close(handel_Write);

 return(1);
}
/*****************************************************************************************************/
/* << 9.03 */
/*
   <FUNCTION>
		<NAME>Encrypte_Fe_Dsk</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>Encrypt file FE_DSK</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
/* >> */
/* << 9.02 */
int Encrypte_Fe_Dsk(void)
{
  /* << 9.03 */
  int iRet, F_Handle;
  /* >> */
  int argc = 3;
  char *argv[3] = {
                  "rijn2",
                  "c",
                  "a:fe_dsk"/*,
                  "99d759f9aa50f1df6b1ec168fa71013c",
                  "edd55ba262c4e7455388457b077cae84"*/
                };

  Lcd_Affiche(0,0, pszMessage_Point[12]);
  Lcd_Affiche(1,0, " ");

  /* Cryptage */
  /* << 9.03 */
  /*rijn2(argc, argv);*/
  iRet = rijn2(argc, argv);
  /* >> */

  /* Delete file a:fe_dsk */
  remove("a:fe_dsk");
  /* << 9.03*/
  if(iRet == - 1) 
  {
	if(access("a:fe_dsk.crp", 0) == 0)
	   remove("a:fe_dsk.crp");

	/*Create an empty file*/
	F_Handle = open("a:fe_dsk.crp", O_RDONLY | O_BINARY);
	if(F_Handle != -1)
		close(F_Handle);
  }
  /* >> */

  Lcd_Affiche(0,0, " ");
  Lcd_Affiche(1,0, " ");

 /* << 9.03 */
  /*return(1);*/
  return(iRet);
 /* >> */
}
/***********************************************************************************************/
/* << 9.03 */
/*
   <FUNCTION>
		<NAME>Decrypte_Fe_Dsk</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>Decrypt file FE_DSK.CRP</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
/* >> */
int Decrypte_Fe_Dsk(void)
{
  /* << 9.03 */
  int iRet;
  /* >> */
  int argc = 3;
  char *argv[3] = {
                  "rijn2",
                  "d",
                  "a:fe_dsk.crp"/*,
                  "99d759f9aa50f1df6b1ec168fa71013c",
                  "edd55ba262c4e7455388457b077cae84"*/
                };

  Lcd_Affiche(0,0, pszMessage_Point[12]);
  Lcd_Affiche(1,0, " ");

  /* Decryptage */
  /* << 9.03 */
  /*rijn2(argc, argv);*/
  iRet = rijn2(argc, argv);
  /* >> */

  /* Delete file a:fe_dsk.crp */
  if(iRet == 1)
	remove("a:fe_dsk.crp");
  else if(access("a:fe_dsk", 0) == 0)
	   remove("a:fe_dsk");

  Lcd_Affiche(0,0, " ");
  Lcd_Affiche(1,0, " ");

 /* << 9.03 */
  /*return(1);*/
  return(iRet);
 /* >> */
}
/* 9.02 >> */
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>MoveTbl</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Load the contents of the file urne.tbl to the ram disk
		</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int MoveTbl()
{
	int  handel_Read, handel_Write,Carry_Read, Carry_Write;
	char szMsgError[80];

     pszplainData[0] = 0x00;
     pszcipherData[0] = 0x00;

     handel_Read = open("a:urne.tbl", O_RDWR | O_BINARY);
     if(handel_Read == -1)
	 {
		  sprintf(szMsgError,"Error opening file A:urne.tbl in MoveTbl function.\nFile source %s\n", __FILE__);
		  Erreur(szMsgError);
		  return(-1);
	 }

     Carry_Read = read(handel_Read,pszplainData,1024);
     if(Carry_Read == -1)
     {
		 sprintf(szMsgError,"Error reading from the file A:urne.tbl in MoveTbl function.\nFile source %s\n",__FILE__);
		 Erreur(szMsgError);
		 return(-1);
     }
     close(handel_Read);
     unlink("c:urne.tbl");

     handel_Write = open( "c:urne.tbl", O_BINARY | O_WRONLY | O_CREAT | O_EXCL,
					S_IREAD | S_IWRITE );
     if(handel_Write == -1)
	 {
		  sprintf(szMsgError,"Error opening file C:urne.tbl in MoveTbl function.\nFile source %s\n", __FILE__);
		  Erreur(szMsgError);
		  return(-1);
	 }

     Carry_Write = write(handel_Write,pszplainData,Carry_Read);
     if(Carry_Write != Carry_Read)
	 {
		  sprintf(szMsgError,"Error writing to the file C:urne.tbl in MoveTbl function.\nFile source: %s\n", __FILE__);
		  Erreur(szMsgError);
		  return(-1);
	 }
     close(handel_Write);
 return(1);
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Initialise_Rijndael</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Initialize the Rijndael algorithm
		</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/

int Initialise_Rijndael()
{
   int i;
   char szMsgError[80];

   makeKeyBinary (&encKeyInst, DIR_DECRYPT, derivedKey);
   cipherInitBinary (&encCipherInst, iv);
   Rfloppy(dataFromFloppy);
   strcpy(szDataFloppy,dataFromFloppy);
   ret= decryptKeys (decryptedMacKey, decryptedEncKey, dataFromFloppy, derivedKey);
   if (ret < 0)
      {
	sprintf(szMsgError,"Error during decryption in Initialise_Rijndael function.\nFile source: %s\n", __FILE__);
	Erreur(szMsgError);
	return(-1);
      }
   else
	    return(1);
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Initialise_RijndaelMini</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>
		Initialize the Rijndael algorithm
		</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Initialise_RijndaelMini()
{
   extendPassword(fullPasswordMini,CMinipassword);
   computeKeyFromPassword (derivedKeyMini, fullPasswordMini);
   computeKeyFromPassword ( decryptedEncKeyMini, fullPasswordMini);
   computeKeyFromPassword (decryptedMacKeyMini, fullPasswordMini);
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Verifie_Password</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>Check the password</DESCRIPTION>
        <PARAMETER>
			<NAME></NAME>
			<TYPE></TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE>int</TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
int Verifie_Password()
{
   int i;

   ret = validatePassword (fullPassword);
   if (ret != 0)
   {
      return(0);
   }

   ret = computeKeyFromPassword (derivedKey, fullPassword);
   if (ret != 0)
   {
      return(0);
   }

   strcpy(source,"a:");
   strcat(source,"\\");
   strcat(source,"be.2");
   ret = Rfile(source);
   if(ret == -1)
   {
        return(ret);
   }

   for (i = 0; i < 16;i++)
   {
        iv[i] = wrkspc[i];
	    ivtmp[i] = wrkspc[i];
   }
   iv[i] = 0x00;
   ivtmp[i] = 0x00;

  return(1);
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Erreur</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>Save the error message in the log file</DESCRIPTION>
        <PARAMETER>
			<NAME>msg</NAME>
			<TYPE>char *</TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Erreur(char *msg)
{
  FILE *fp;
/* << 9.01b */
/*  strcpy(destine,destineori);
  strcat(destine,"\\LOG.ERR");
  if ((fp = fopen(destine,"a")) != NULL)*/
  if ((fp = fopen("a:\\LOG.ERR","a")) != NULL)
/* 9.01b >> */
  {
	  fputs(msg,fp);
	  fclose(fp);
  }
}
/****************************************************************************/
/*
   <FUNCTION>
		<NAME>Initialise_Date</NAME>
        <TYPE>LOCAL</TYPE>
		<DESCRIPTION>Initialize the date</DESCRIPTION>
        <PARAMETER>
			<NAME>msg</NAME>
			<TYPE>char *</TYPE>
			<IO></IO>
			<DESCRIPTION></DESCRIPTION>
		</PARAMETER>
		<RETURN>
			<TYPE></TYPE>
			<DESCRIPTION></DESCRIPTION>
		</RETURN>
   </FUNCTION>
*/
void Initialise_Date(char *Buffer)
{
  char strtmp[4];
  char dyy[3],dmm[3],djj[3];
  int yy,jj,mm;


  djj[0] = Buffer[9];
  djj[1] = Buffer[10];
  djj[2] = 0x00;
  dmm[0] = Buffer[11];
  dmm[1] = Buffer[12];
  dmm[2] = 0x00;
  dyy[0] = Buffer[13];
  dyy[1] = Buffer[14];
  dyy[2] = 0x00;
  sscanf(djj,"%d",&jj);
  sscanf(dmm,"%d",&mm);
  sscanf(dyy,"%d",&yy);
  if(yy < 80)
	 yy +=2000;
  else
	 yy +=1900;
  ddate.year = yy;
  ddate.month = mm;
  ddate.day = jj;
  _dos_setdate( &ddate );
  dtime.hour = 8;
  dtime.minute =0;
  dtime.second = 0;
 _dos_settime( &dtime );

}

