/******************************************************************************
*                                                                             *
*              Copyright (C),  Philips Semiconductors SLH                     *
*                                                                             *
*  All rights are reserved. Reproduction in whole or in part is prohibited    *
*           without the written consent of the copyright owner.               *
*                                                                             *
*  Philips reserves the right to make changes without notice at any time.     *
* Philips makes no warranty, expressed, implied or statutory, including but   *
* not limited to any implied warranty of merchantibility or fitness for any   *
*   particular purpose, or that the use will not infringe any third party     *
* patent, copyright or trademark. Philips must not be liable for any loss or  *
*                      damage arising from its use.                           *
*                                                                             *
*******************************************************************************
*                                                                             *
*       Project: 336355/105 - Control SW Library for HTRC110/HTRC120/HITAG2  *
*                                                                             *
*       File: HTRC120.H                                                       *
*       Author: F. Boeh                                                       *
*               S. de Zeeuw                                                   *
*                                                                             *
*******************************************************************************
*      DATE      *          CHANGES DONE                         *    BY      *
*******************************************************************************
*                *                                               *            *
*  Apr 27,1997   * Version 2.0, tested Version                   * de Zeeuw   *
*                *                                               *            *
*******************************************************************************/
#ifndef __HTRC120_H
#define __HTRC120_H


#include "reg51.h"

#pragma save
#pragma regparms

/******************************************************************************
*                                                                             *
*  APPLICATION SYSTEM PARAMETERS, to be adapted by the user                   *
*                                                                             *
******************************************************************************/
                                 /* microcontroller port assignment */
#define CRST         (P2_0)      /* reset                           */
#define CCLK         (P2_1)      /* sysclock                        */
#define CDATA        (P2_2)      /* data                            */
#define READY        (P2_3)      /* /ready                          */
#define SLEEP        (P2_4)      /* sleep                           */



//#define ENABLE_PWD_CHECK



/* HTRC120 command definitions */
#define CMD_GET_VERSION           (0xFE)
#define CMD_PERSONALIZE           (0xBF)
#define CMD_START_AUTHENT         (0x67)
#define CMD_START_AUTHENT_PWCHECK (0x77)

/* HTRC120 EEPROM "count value" at 16 MHz clock. Pls adapt this to your system. */
#define COUNT_VALUE (0x7D)




/******************************************************************************
*                                                                             *
* SOFTWARE PARAMETERS                                                         *
*                                                                             *
******************************************************************************/

#define LONG_VERSION   (1)
#define SHORT_VERSION  (0)


/******************************************************************************
*                                                                             *
* Function prototypes of COPRO.C                                              *
*                                                                             *
******************************************************************************/



extern unsigned char send_copro( unsigned char idata * addr, unsigned char length );
/******************************************************************************
*                                                                             *
* Description:                                                                *
* This procedure sends a bitstream from the microcontroller to the            *
* cryptocontroller.                                                           *
*                                                                             *
* Parameters:                                                                 *
*   addr:      address where the bitstream has been stored                    *
*   length:    number of bits to be send                                      *
* Return:                                                                     *
*   FALSE        in case that no error occured                                *
*   TRUE         in case of an error                                          *
*                                                                             *
******************************************************************************/



extern unsigned char receive_copro( unsigned char idata * addr, unsigned char length );
/******************************************************************************
*                                                                             *
* Description:                                                                *
* This procedure receives a bitstream from the the cryptocontroller.          *
*                                                                             *
* Parameters:                                                                 *
*   addr:      memory address where the received data have to be stored       *
*   length:    number of bits to be read                                      *      
* Return:                                                                     *
*   FALSE         in case that no error occured                               *
*   TRUE          in case of an error                                         *
*                                                                             *
******************************************************************************/


 
extern unsigned char wait_copro (void);
/******************************************************************************
*                                                                             *
* Description:                                                                *
* This procedure waits for /READY = LOW. It should not be called when \READY  *
* is already LOW. Minimum waiting time is 50 ms (16 MHz clock) and maximum    *
* 100 ms (8 MHz clock frequency)                                              *
*                                                                             *
* Parameters: none                                                            *
* Return:     none                                                            *
*                                                                             *
******************************************************************************/


extern void init_copro (void);
/******************************************************************************
*                                                                             *
* Description:                                                                *
* This procedure is called before sending a command to the HTRC120.           *
* Afterwards the co-processor expects the next Byte to be a valid command.    *
*                                                                             *
* Parameters: none                                                            *
* Return:     none                                                            *
*                                                                             *
******************************************************************************/



extern void sleep_copro (void);
/******************************************************************************
*                                                                             *
* Description:                                                                *
* This procedure puts the cryptoprocessor into the SLEEP-modee                *
*                                                                             *
* Parameters: none                                                            *
* Return:     none                                                            *
*                                                                             *
******************************************************************************/




extern void delay_copro (unsigned char cycles);
/******************************************************************************
*                                                                             *
* Description:                                                                *
* This function is intended to provide some delay time for the co-processor as*
* demanded in the specificatio. It was intended to avoid a                    *
* superfluous use of an extra timer.                                          *
*                                                                             *
* Parameters:                                                                 *
* cycles  : number of waiting loops (equivalent to a time interval)           *
*                                                                             *
* Return:    none                                                             *
*                                                                             *
******************************************************************************/


extern unsigned char Crypt_Get_Version( unsigned char idata * addr,
                                        unsigned char version_length );
/******************************************************************************
*                                                                             *
* Description:                                                                *
* The procedure sends the get_version command and a serial (dummy) number to  *
* the co-processor. Afterwards it reads the version data from the HTRC120.    *
*                                                                             *
* Parameters:                                                                 *
*   addr:      memory address where the version data shall be stored          *
*   version_length:                                                           *
*              LONG_VERSION, if 33 bytes shall be stored.                     *
*              SHORT_VERSION, if only the first 2 Bytes have to be stored     *
* Return:                                                                     *
*   FALSE      in case  no error occured                                      *
*   TRUE       in case of an error occured                                    *
*                                                                             *
******************************************************************************/


extern unsigned char Crypt_Personalize( unsigned char idata * addr_newkey,
                                        unsigned char idata * addr_oldkey,
                                        unsigned char idata * addr_newpass,
                                        unsigned char idata * addr_oldpass );
/********************************************************************************
*                                                                               *
* Description:                                                                  *
* This procedure executes the personalization of the co-processor as described  *
* in the data sheet. The value of CntVal depends on the above definition of     *
* Count_Value. The HITAG1 personalization is not perfomed, all corresponding    *
* values are assumed to have the initial values and are not changed.            *
*                                                                               *
* Parameters:                                                                   *
*   addr_newkey:  memory address where the new HITAG2-key has to be stored      *
*   addr_oldkey:  memory address where the old HITAG2-key has to be stored      *
*   addr_newpass: memory address where the new HITAG2-password has to be stored *
*                    (first Byte shall be a dummy Byte)                         *
*   addr_oldpass: memory address where the old HITAG2-password has to be stored *
*                    (first Byte shall be a dummy Byte)                         *
* Return:                                                                       *
*   FALSE         in case that no error occured                                 *
*   TRUE          in case of an error                                           *
*                                                                               *
********************************************************************************/




extern unsigned char Crypt_Block( unsigned char idata * addr, unsigned char length );
/********************************************************************************
*                                                                               *
* Description:                                                                  *
* This procedure reads a bit stream from the co-processor and XORs it           *
* with the data bits, the values are overwritten                                *
*                                                                               *
* Parameters:                                                                   *
* Return:                                                                       *
*   FALSE         in case that no error occured                                 *
*   TRUE          in case of an error                                           *
*                                                                               *
********************************************************************************/



extern unsigned char Crypt_Set_Ident( unsigned char idata * addr );
/********************************************************************************
*                                                                               *
* Description:                                                                  *
* This procedure sends the transponder identifier the co-processor.             *
*                                                                               *
* Parameters:                                                                   *
*   addr:      memory address where the identifier is stored                    *
* Return:                                                                       *
*   FALSE         in case that no error occured                                 *
*   TRUE          in case of an error                                           *
*                                                                               *
********************************************************************************/



extern unsigned char Crypt_Get_Rand_RWDPass( unsigned char idata * addr );
/********************************************************************************
*                                                                               *
* Description:                                                                  *     
* This routine reads  the random number and the encrypted RWD-password from     *
* the HTRC120.                                                                  *
*                                                                               *
* Parameters:                                                                   *
*   addr:      memory address where the data shall be stored                    *
* Return:                                                                       *
*   FALSE         in case that no error occured                                 *
*   TRUE          in case of an error                                           *
*                                                                               *                  
********************************************************************************/



extern unsigned char Crypt_Decipher_HT2Pass( unsigned char idata * addr );
/********************************************************************************
*                                                                               *
* Description:                                                                  *
* This routine transmits the encrypted transponder password to the HTRC120.     *
* Depending on the definition of the pre-processor constant "ENABLE_PWD_CHECK"  *
* the decrypted configuration Byte/page 3 is received.                          *
*                                                                               *
* Parameters:                                                                   *
*   addr:      memory address where the enciphered transponder password is      *
*              stored and where the deciphered page3/coniguration Byte shall    *
*              be stored                                                        *
* Return:                                                                       *
*   FALSE         in case that no error occured                                 *
*   TRUE          in case of an error                                           *
*                                                                               *
********************************************************************************/


#pragma restore


#endif /* __HTRC120_H */
