PinPadClass.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /////////////////////////////////////////////////////////////////////////////////
  2. /// Copyright (c) 2012 China Merchants Bank, all rights reserved ///
  3. /// Adapter Interface for PinPad. ///
  4. /// ///
  5. ////////////////////////////////////////////////////////////////////////////////
  6. #ifndef __PIN_PAD_CLASS_H
  7. #define __PIN_PAD_CLASS_H
  8. #pragma once
  9. #include "DeviceBaseClass.h"
  10. /** define specified device adapter interface file version*/
  11. /** once the change cannot compact with before one, CMBer must update this version node and reset the minor-version node*/
  12. #define PINPAD_INTERFACE_FILE_VERSION_MAJOR 1
  13. /** any change but not effect compatibility with elder one, CMBer should update this verion node*/
  14. #define PINPAD_INTERFACE_FILE_VERSION_MINOR 0
  15. #define PINPAD_INTERFACE_FILE_VERSION_STRING \
  16. DEVICE_ADAPTER_CLASS_STRINGFY(PINPAD_INTERFACE_FILE_VERSION_MAJOR, PINPAD_INTERFACE_FILE_VERSION_MINOR)
  17. #define PINPAD_ADAPTER_CLASS_ID \
  18. DEVICE_ADAPTER_CLASS_COMBINE(PINPAD_INTERFACE_FILE_VERSION_MAJOR)
  19. const int MAX_PIN_READ_SIZE = 256;
  20. const int MAX_PIN_BLOCK_SIZE = 256;
  21. const int MAX_MASTER_KEY_SIZE = 128;
  22. const int MAX_WORKING_KEY_SIZE = 128;
  23. const int MAX_ACCOUNT_LEN = 32;
  24. const int MAX_EN_DECRYPT_DATA_SIZE = 512;
  25. struct PinPadInitParam
  26. {
  27. DWORD dwPort;
  28. DWORD dwBaudRate;
  29. };
  30. struct PinReadData
  31. {
  32. DWORD dwSize;
  33. BYTE data[MAX_PIN_READ_SIZE];
  34. };
  35. struct MasterKeyParam
  36. {
  37. int smFlag; //0:3des,1:sm
  38. DWORD dwSN; //master key serial number:0,1,2,...
  39. BYTE key[MAX_MASTER_KEY_SIZE]; //key value
  40. };
  41. struct MasterKeyParamEx
  42. {
  43. int smFlag; //0:3des,1:sm,11: 本结构体的字段key为设备临时公钥加密的主密钥密文(国密)
  44. DWORD dwSN; //master key serial number:0,1,2,...
  45. BYTE key[MAX_EN_DECRYPT_DATA_SIZE]; //(plain or encrypted)key value
  46. };
  47. struct WorkKeyParam
  48. {
  49. int smFlag; //0:3des,1:sm
  50. DWORD dwMasterSN; //master key serial number:0,1,2,...
  51. DWORD dwWorkingSN; //working key serial number:0,1,2,...
  52. BYTE key[MAX_WORKING_KEY_SIZE]; //key value
  53. };
  54. struct PinBlock
  55. {
  56. DWORD dwSize;
  57. BYTE data[MAX_PIN_BLOCK_SIZE];
  58. };
  59. struct AccountInfo
  60. {
  61. DWORD dwAccLen;
  62. BYTE account[MAX_ACCOUNT_LEN];
  63. };
  64. struct EnDecryptInfo
  65. {
  66. DWORD dwSize;
  67. BYTE data[MAX_EN_DECRYPT_DATA_SIZE]; //en/decrypt data
  68. };
  69. //parameter type to set device
  70. enum SetParamTypeEnum
  71. {
  72. EPP_PT_SET_ENCRYPT_METHOD, //data encrypt/decrypt method
  73. EPP_PT_SET_MAC_ALGORITH, //mac algorithm
  74. EPP_PT_SET_PIN_ALGORITH, //pin encrypt/decrypt algorithm
  75. EPP_PT_SET_ECB_CBC_MODE, //encrypt/decrypt mode
  76. };
  77. //parameter value to set device
  78. enum SetParamValueEnum
  79. {
  80. ////values for EPP_PT_SET_ENCRYPT_METHOD
  81. EPP_ALGO_METHOD_DES,
  82. EPP_ALGO_METHOD_3DES,
  83. EPP_ALGO_METHOD_DES_TMK,
  84. EPP_ALGO_METHOD_3DES_TMK,
  85. //values for EPP_PT_SET_MAC_ALGORITH
  86. EPP_MAC_ALGO_ASNIX99,
  87. EPP_MAC_ALGO_PBOC,
  88. EPP_MAC_ALGO_UNIONPAY,
  89. //values for EPP_PT_SET_PIN_ALGORITH
  90. EPP_PIN_ALGO_ISO9564_1_ANSI,//ANSIx9.8
  91. EPP_PIN_ALGO_IBM3624,
  92. //values for EPP_PT_SET_ECB_CBC_MODE
  93. //设置加密方式,这里仅为了兼容,保留,设置ECB/CBC请使用 EPP_ALGO_MODE_ECB, EPP_ALGO_MODE_CBC,
  94. EPP_ALGO_MODE_DEC_ECB,
  95. EPP_ALGO_MODE_DEC_CBC,
  96. EPP_ALGO_MODE_3DEC_ECB,
  97. EPP_ALGO_MODE_3DEC_CBC,
  98. EPP_ALGO_METHOD_SM4,
  99. EPP_PIN_ALGO_SM4,
  100. EPP_ALGO_MODE_ECB,
  101. EPP_ALGO_MODE_CBC,
  102. };
  103. class PinPadClass : public DeviceBaseClass
  104. {
  105. public:
  106. //
  107. // Open device.
  108. // Arguments:
  109. // - dwPort[in] com port number
  110. // - dwBaudRate[in] Baud rate
  111. //
  112. virtual ErrorCodeEnum DevOpen(DWORD dwPort,DWORD dwBaudRate) = 0;
  113. //
  114. // Get input key value.
  115. // Monitor pinpad key press,if any key pressed,set [ch] value and return Error_Succeed.
  116. // If none was pressed ,return Error_DevCommFailed.
  117. //
  118. // Two input mode:plain text,pin(encrypted)
  119. // 1.In any mode,press key "*,#,blank,cancel,clear,confirm",set ch value
  120. // "0x2a,0x23,0x20,0x1b,0x08,0x0d" respectively
  121. // 2.In plain text input mode,press key 0~9,.,00,set ch 0x30~0x39,0x2e,0x4f respectively
  122. // 3.In pin input mode,press any key of 0~9,set ch 0x3f,else('.','00') set ch 0x3d
  123. // 4."clear" means clear all the input
  124. //
  125. virtual ErrorCodeEnum KeyRead(BYTE &ch) = 0;
  126. //
  127. // Set pinpad parameter.
  128. //
  129. virtual ErrorCodeEnum SetParam(SetParamTypeEnum eType,SetParamValueEnum eValue) = 0;
  130. //
  131. // Set account number.
  132. // Arguments:
  133. // - accInfo.account:(ASCII)account value
  134. // ex:card no "62260755",the accInfo.account should be 0x3632323630373535
  135. //
  136. virtual ErrorCodeEnum SetAccNo(AccountInfo accInfo) = 0;
  137. //
  138. // Load master key. (in plain text)
  139. // [key]:byte value in string type. ex. 0x123456FF --> "123456FF"
  140. //
  141. virtual ErrorCodeEnum LoadMasterKey(MasterKeyParam masterKey) = 0;
  142. //
  143. // Load working key. (encrypted data)
  144. // Use the according master key to decrypt the key,then load result into pinpad
  145. // [key]:byte value in string type. ex. 0x123456FF --> "123456FF"
  146. //
  147. virtual ErrorCodeEnum LoadWorkingKey(WorkKeyParam wkKey) = 0;
  148. //
  149. // Active a working key as current working key.
  150. // Arguments:
  151. // - dwMkSN:master key serial number
  152. // - dwWkSN:working key serial number
  153. // - ActiveWorkingKey(主密钥序号,99)表示激活“主密钥序号”的主密钥作为当前工作密钥
  154. //
  155. virtual ErrorCodeEnum ActiveWorkingKey(DWORD dwMkSN,DWORD dwWkSN) = 0;
  156. //
  157. // Start key press(pin mode).
  158. // byLen:输入密码长度,键盘检测到用户输入byLen位有效!!长度时自动结束输入,计算pinblock
  159. //
  160. virtual ErrorCodeEnum StartPinInput(BYTE byLen) = 0;
  161. //
  162. // Start key press(pin mode).
  163. // byLen:输入密码长度,键盘检测到用户输入确认键后结束输入,计算pinblock
  164. //
  165. virtual ErrorCodeEnum StartPinInputConfirm(BYTE byLen) = 0;
  166. //
  167. // Start key press(plain text mode).
  168. //
  169. virtual ErrorCodeEnum StartPlainInput() = 0;
  170. //
  171. // Close pinpad input.
  172. //
  173. virtual ErrorCodeEnum StopInput() = 0;
  174. //
  175. // Get pin block.
  176. // At the end of pin input, to get pin text.
  177. //
  178. virtual ErrorCodeEnum GetPinBlock(PinBlock &block) = 0;
  179. //
  180. // Encrypt data.
  181. // Arguments:
  182. // - srcInfo.data:source plain text data
  183. // (byte value in string type. ex. 0x123456FF --> "123456FF")
  184. // - destInfo.data:destination encrypted data
  185. //
  186. virtual ErrorCodeEnum EncryptData(EnDecryptInfo srcInfo, EnDecryptInfo &destInfo) = 0;
  187. //
  188. // Encrypt data(MAC)
  189. // Arguments:
  190. // - srcInfo.data:source plain text data
  191. // (byte value in string type. ex. 0x123456FF --> "123456FF")
  192. // - destInfo.data:destination encrypted data
  193. //
  194. virtual ErrorCodeEnum MacEncrypt(EnDecryptInfo srcInfo, EnDecryptInfo &destInfo) = 0;
  195. //
  196. // Get the encrypt method supported.
  197. // return value:
  198. // 0x1:3des only; 0x2:sm4 only; 0x3:both 3des and sm4
  199. //
  200. virtual int GetEncryptFunc() = 0;
  201. //
  202. // Load master key.
  203. // [key]:byte value in string type. ex. 0x123456FF --> "123456FF"
  204. //
  205. virtual ErrorCodeEnum LoadMasterKeyEx(MasterKeyParamEx enMasterKey) {return Error_NotImpl;}
  206. };
  207. #endif // __PIN_PAD_CLASS_H