// CryptoMemory Header File
//
// Detailed descriptions of all functions and structures in the header file can be found in
// the "CryptoMemory Library User Manual".
//
// Revision Date      Changes
// -------- --------- -----------------------------------------------------------------
//    0.1   26 Oct 04 First Release
//    0.2   19 Nov 04 Major additions and corrections

// Protect Library against multiple inclusion
#ifndef __CM_LIB_H__
#define __CM_LIB_H__

#include "com_IPMIDefs.h"
// -------------------------------------------------------------------------------------------------
// Definations
// -------------------------------------------------------------------------------------------------

// Basic Definations (if not available elsewhere)
#ifndef FALSE
#define FALSE (0)
#define TRUE  (!FALSE)
#endif
#ifndef NULL
#define NULL ((void *)0)
#endif

// Device Configuration Register
#define DCR_ADDR      (0x18)
#define DCR_SME       (0x80)
#define DCR_UCR       (0x40)
#define DCR_UAT       (0x20)
#define DCR_ETA       (0x10)
#define DCR_CS        (0x0F)

// Cryptograms
#define CM_Ci         (0x50)
#define CM_Sk         (0x58)
#define CM_G          (0x90)

// Fuses
#define CM_FAB        (0x06)
#define CM_CMA        (0x04)
#define CM_PER        (0x00)

// Password
#define CM_PSW        (0xB0)
#define CM_PWREAD     (1)
#define CM_PWWRITE    (0)

// Return Code Defination
#define SUCCESS       (0)
#define FAILED        (1)
#define FAIL_CMDSTART (2)
#define FAIL_CMDSEND  (3)
#define FAIL_WRDATA   (4)
#define FAIL_RDDATA   (5)
// note: additional specific error codes may be added in the future

// -------------------------------------------------------------------------------------------------
void cm_InitTwiPio(void);
void cm_RestorPio(void);
// High Level Function Prototypes

// Select Chip
uint8_t cm_SelectChip(uint8_t ucChipId);

// Activate Security
uint8_t cm_ActiveSecurity(uint8_t ucKeySet, uint8_t * pucKey, uint8_t * pucRandom, uint8_t ucEncrypt);

// Deactivate Security
uint8_t cm_DeactiveSecurity(void);

// Verify Password
uint8_t cm_VerifyPassword(uint8_t * pucPassword, uint8_t ucSet, uint8_t ucRW);

// Reset Password
uint8_t cm_ResetPassword(void);

// Verify Secure Code
//#define cm_VerifySecureCode(CM_PW) cm_VerifyPassword(CM_PWRON_CLKS, 7, CM_PWWRITE) 
#define cm_VerifySecureCode(CM_PW) cm_VerifyPassword(CM_PW, 7, CM_PWWRITE) 

// Read Configuration Zone
uint8_t cm_ReadConfigZone(uint8_t ucCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);

// Write Configuration Zone
uint8_t cm_WriteConfigZone(uint8_t ucCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount, uint8_t ucAntiTearing);

// Set User Zone
uint8_t cm_SetUserZone(uint8_t ucZoneNumber, uint8_t ucAntiTearing);

// Read User Zone
uint8_t cm_ReadLargeZone(uint32_t uiCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);

// Read Small User Zone
uint8_t cm_ReadSmallZone(uint8_t ucCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);

// Write User Zone
char cm_WriteLargeZone(uint32_t uiCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);

// Write Small User Zone
uint8_t cm_WriteSmallZone(uint8_t ucCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);

// Send Checksum
uint8_t cm_SendChecksum(uint8_t * pucChkSum);

// Read Checksum
uint8_t cm_ReadChecksum(uint8_t * pucChkSum);

// Read Fuse Byte
uint8_t cm_ReadFuse(uint8_t * pucFuze);

// Burn Fuse
uint8_t cm_BurnFuse(uint8_t ucFuze);

void cm_InitTwiPio(void);

void cm_RestorPio(void);

void cm_InitCmVar(unsigned char Num);

//加密访问API接口函数:用于认证加密
int cm_Auth_Encrp(void);

// -------------------------------------------------------------------------------------------------
// Configuration Structures
// -------------------------------------------------------------------------------------------------

// CryptoMemory Low Level Linkage
// 
typedef struct{
    uint8_t (*Carddetect)(void);
    void (*PowerOff)(void);
    void (*PowerOn)(void);
    uint8_t (*SendCommand)(uint8_t * pucCommandBuffer);
    uint8_t (*ReceiveRet)(uint8_t * pucReceiveData, uint8_t ucLength);
    uint8_t (*SendData)(uint8_t * pucSendData, uint8_t ucLength);
    void (*RandomGen)(uint8_t * pucRandomData);
    void (*WaitClock)(uint8_t ucLoop);
    uint8_t (*SendCmdByte)(uint8_t ucCommand);
} cm_low_level;

// CryptoMemory Low Level Configuration
// 
// If any of the supplied CryptoMemory low level library functions are used, this structure must be
// present in the user code. For a detailed description of the elements in the structure, please
// see the "CryptoMemory Library User Manual".
// 
typedef struct{
    uint8_t ucChipSelect;
    uint8_t ucClockPort;
    uint8_t ucClockPin;
    uint8_t ucDataPort;
    uint8_t ucDataPin;
    uint8_t ucCardSensePort;
    uint8_t ucCardSensePin; 
    uint8_t ucCardSensePolarity; 
    uint8_t ucPowerPort;
    uint8_t ucPowerPin;
    uint8_t ucPowerPolarity;
    uint8_t ucDelayCount;
    uint8_t ucStartTries;
} cm_port_cfg;

// -------------------------------------------------------------------------------------------------
// Externals for Configuration Structures
// -------------------------------------------------------------------------------------------------

extern cm_low_level CM_LOW_LEVEL;
extern cm_port_cfg  CM_PORT_CFG;

// -------------------------------------------------------------------------------------------------
// Other Externals
// -------------------------------------------------------------------------------------------------

extern uint8_t ucCM_Encrypt;
extern uint8_t ucCM_Authenticate;
extern uint8_t ucCM_UserZone;
extern uint8_t ucCM_AntiTearing;
extern uint8_t ucCM_InsBuff[4];

uint8_t cm_Auth_Encrp_Init(uint8_t authMode,uint8_t pswMode,uint8_t encryptFlag);
uint8_t test_cryptomem(void);
uint8_t cm_Auth_Encrp_Write(uint8_t userZone,uint8_t startInZone,uint8_t* writeData,uint8_t writeLen);
uint8_t cm_Auth_Encrp_Read(uint8_t userZone,uint8_t startInZone,uint8_t* readData,uint8_t readLen);
uint8_t cm_Auth_Encrp_Personal(uint8_t pswMode,uint8_t authMode);
// end of multiple inclusion protection
#endif
