CM_LIB.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // CryptoMemory Header File
  2. //
  3. // Detailed descriptions of all functions and structures in the header file can be found in
  4. // the "CryptoMemory Library User Manual".
  5. //
  6. // Revision Date Changes
  7. // -------- --------- -----------------------------------------------------------------
  8. // 0.1 26 Oct 04 First Release
  9. // 0.2 19 Nov 04 Major additions and corrections
  10. // Protect Library against multiple inclusion
  11. #ifndef __CM_LIB_H__
  12. #define __CM_LIB_H__
  13. #include "com_IPMIDefs.h"
  14. // -------------------------------------------------------------------------------------------------
  15. // Definations
  16. // -------------------------------------------------------------------------------------------------
  17. // Basic Definations (if not available elsewhere)
  18. #ifndef FALSE
  19. #define FALSE (0)
  20. #define TRUE (!FALSE)
  21. #endif
  22. #ifndef NULL
  23. #define NULL ((void *)0)
  24. #endif
  25. // Device Configuration Register
  26. #define DCR_ADDR (0x18)
  27. #define DCR_SME (0x80)
  28. #define DCR_UCR (0x40)
  29. #define DCR_UAT (0x20)
  30. #define DCR_ETA (0x10)
  31. #define DCR_CS (0x0F)
  32. // Cryptograms
  33. #define CM_Ci (0x50)
  34. #define CM_Sk (0x58)
  35. #define CM_G (0x90)
  36. // Fuses
  37. #define CM_FAB (0x06)
  38. #define CM_CMA (0x04)
  39. #define CM_PER (0x00)
  40. // Password
  41. #define CM_PSW (0xB0)
  42. #define CM_PWREAD (1)
  43. #define CM_PWWRITE (0)
  44. // Return Code Defination
  45. #define SUCCESS (0)
  46. #define FAILED (1)
  47. #define FAIL_CMDSTART (2)
  48. #define FAIL_CMDSEND (3)
  49. #define FAIL_WRDATA (4)
  50. #define FAIL_RDDATA (5)
  51. // note: additional specific error codes may be added in the future
  52. // -------------------------------------------------------------------------------------------------
  53. void cm_InitTwiPio(void);
  54. void cm_RestorPio(void);
  55. // High Level Function Prototypes
  56. // Select Chip
  57. uint8_t cm_SelectChip(uint8_t ucChipId);
  58. // Activate Security
  59. uint8_t cm_ActiveSecurity(uint8_t ucKeySet, uint8_t * pucKey, uint8_t * pucRandom, uint8_t ucEncrypt);
  60. // Deactivate Security
  61. uint8_t cm_DeactiveSecurity(void);
  62. // Verify Password
  63. uint8_t cm_VerifyPassword(uint8_t * pucPassword, uint8_t ucSet, uint8_t ucRW);
  64. // Reset Password
  65. uint8_t cm_ResetPassword(void);
  66. // Verify Secure Code
  67. //#define cm_VerifySecureCode(CM_PW) cm_VerifyPassword(CM_PWRON_CLKS, 7, CM_PWWRITE)
  68. #define cm_VerifySecureCode(CM_PW) cm_VerifyPassword(CM_PW, 7, CM_PWWRITE)
  69. // Read Configuration Zone
  70. uint8_t cm_ReadConfigZone(uint8_t ucCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);
  71. // Write Configuration Zone
  72. uint8_t cm_WriteConfigZone(uint8_t ucCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount, uint8_t ucAntiTearing);
  73. // Set User Zone
  74. uint8_t cm_SetUserZone(uint8_t ucZoneNumber, uint8_t ucAntiTearing);
  75. // Read User Zone
  76. uint8_t cm_ReadLargeZone(uint32_t uiCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);
  77. // Read Small User Zone
  78. uint8_t cm_ReadSmallZone(uint8_t ucCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);
  79. // Write User Zone
  80. char cm_WriteLargeZone(uint32_t uiCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);
  81. // Write Small User Zone
  82. uint8_t cm_WriteSmallZone(uint8_t ucCryptoAddr, uint8_t * pucBuffer, uint8_t ucCount);
  83. // Send Checksum
  84. uint8_t cm_SendChecksum(uint8_t * pucChkSum);
  85. // Read Checksum
  86. uint8_t cm_ReadChecksum(uint8_t * pucChkSum);
  87. // Read Fuse Byte
  88. uint8_t cm_ReadFuse(uint8_t * pucFuze);
  89. // Burn Fuse
  90. uint8_t cm_BurnFuse(uint8_t ucFuze);
  91. void cm_InitTwiPio(void);
  92. void cm_RestorPio(void);
  93. void cm_InitCmVar(unsigned char Num);
  94. //加密访问API接口函数:用于认证加密
  95. int cm_Auth_Encrp(void);
  96. // -------------------------------------------------------------------------------------------------
  97. // Configuration Structures
  98. // -------------------------------------------------------------------------------------------------
  99. // CryptoMemory Low Level Linkage
  100. //
  101. typedef struct{
  102. uint8_t (*Carddetect)(void);
  103. void (*PowerOff)(void);
  104. void (*PowerOn)(void);
  105. uint8_t (*SendCommand)(uint8_t * pucCommandBuffer);
  106. uint8_t (*ReceiveRet)(uint8_t * pucReceiveData, uint8_t ucLength);
  107. uint8_t (*SendData)(uint8_t * pucSendData, uint8_t ucLength);
  108. void (*RandomGen)(uint8_t * pucRandomData);
  109. void (*WaitClock)(uint8_t ucLoop);
  110. uint8_t (*SendCmdByte)(uint8_t ucCommand);
  111. } cm_low_level;
  112. // CryptoMemory Low Level Configuration
  113. //
  114. // If any of the supplied CryptoMemory low level library functions are used, this structure must be
  115. // present in the user code. For a detailed description of the elements in the structure, please
  116. // see the "CryptoMemory Library User Manual".
  117. //
  118. typedef struct{
  119. uint8_t ucChipSelect;
  120. uint8_t ucClockPort;
  121. uint8_t ucClockPin;
  122. uint8_t ucDataPort;
  123. uint8_t ucDataPin;
  124. uint8_t ucCardSensePort;
  125. uint8_t ucCardSensePin;
  126. uint8_t ucCardSensePolarity;
  127. uint8_t ucPowerPort;
  128. uint8_t ucPowerPin;
  129. uint8_t ucPowerPolarity;
  130. uint8_t ucDelayCount;
  131. uint8_t ucStartTries;
  132. } cm_port_cfg;
  133. // -------------------------------------------------------------------------------------------------
  134. // Externals for Configuration Structures
  135. // -------------------------------------------------------------------------------------------------
  136. extern cm_low_level CM_LOW_LEVEL;
  137. extern cm_port_cfg CM_PORT_CFG;
  138. // -------------------------------------------------------------------------------------------------
  139. // Other Externals
  140. // -------------------------------------------------------------------------------------------------
  141. extern uint8_t ucCM_Encrypt;
  142. extern uint8_t ucCM_Authenticate;
  143. extern uint8_t ucCM_UserZone;
  144. extern uint8_t ucCM_AntiTearing;
  145. extern uint8_t ucCM_InsBuff[4];
  146. uint8_t cm_Auth_Encrp_Init(uint8_t authMode,uint8_t pswMode,uint8_t encryptFlag);
  147. uint8_t test_cryptomem(void);
  148. uint8_t cm_Auth_Encrp_Write(uint8_t userZone,uint8_t startInZone,uint8_t* writeData,uint8_t writeLen);
  149. uint8_t cm_Auth_Encrp_Read(uint8_t userZone,uint8_t startInZone,uint8_t* readData,uint8_t readLen);
  150. uint8_t cm_Auth_Encrp_Personal(uint8_t pswMode,uint8_t authMode);
  151. // end of multiple inclusion protection
  152. #endif