{**************************************************************************************
* Copyright (c)
* All rights reserved.
*
* FileName：
*
* History:
*  ------------------------------------------------------------
*  2011/08/01   Create
*
*******************************************************************************************}
unit Rockey1;

interface

uses
  SysUtils, Windows, WinSock, Nb30;

// const definition
const   R1_S_SUCCESS						        =$00000000;	//** 操作成功*/
const   R1_E_OPEN_FAILED					      =$F0000001;	//** 打开锁失败*/
const   R1_E_NO_LIST						        =$F0000002;	//** 没有找到设备列表*/
const   R1_E_KEY_REMOVED					      =$F0000003;	//** 设备未连接，或者被移除*/
const   R1_E_INVALID_PARAMETER				  =$F0000004;	//** 参数错误*/
const   R1_E_WORNG_USER_PIN					    =$F0000005;	//** 错误的用户口令*/
const   R1_E_WORNG_SO_PIN					      =$F0000006;	//** 错误的开发商口令*/
const   R1_E_CHANGE_USER_PIN				    =$F0000007;	//** 修改用户口令错误*/
const   R1_E_CHANGE_SO_PIN					    =$F0000008;	//** 修改开发商口令错误*/
const   R1_E_TDES_BUF_LENGTH_ERROR			=$F0000009;	//** 3DES加解密的内容长度错误。（必须被8整除）*/
const   R1_E_RSA_BUF_LENGTH_ERROR			  =$F000000A;	//** RSA加解密的内容长度错误*/
const   R1_E_RSA_ID_ERROR	        		  =$F000000B;	//** RSA密钥对ID错误*/
const   R1_E_RSA_ENC_ERROR        			=$F000000C;	//** RSA公钥加密失败*/
const   R1_E_SET_UPDATE_ERROR       		=$F000000D;	//** 请先设置升级内容*/
const   R1_E_RSA_UPDATE_ERROR       		=$F000000E;	//** 制作升级RSA密钥时失败*/
const   R1_E_DES_UPDATE_ERROR       		=$F000000F;	//** 制作升级3des密钥时失败*/
const   R1_E_FORMAT_UPDATE_ERROR        =$F000001A;  //**升级包格式不合法*/
const   R1_E_GET_TIME_ERROR       			=$F0000010;	//** 获得时间失败*/
const   R1_E_SHARING_VIOLATION       		=$F0000011;	//** 打开设备失败*/
const   R1_E_ACCESS_DENIED         			=$F0000012;	//** 打开设备权限失败*/
const   R1_E_CREATE_SHARE_M         		=$F0000013;	//** 创建共享内存失败*/
const   R1_E_EXIST_SHARE_M          		=$F0000014;	//** 共享内存存在*/
const   R1_E_GET_RSA_KEY          			=$F0000015;	//** 获得RSA密钥对失败*/
const   R1_E_RSA_NOT_PAIR          			=$F0000016;	//** RSA密钥对不匹配*/
const   R1_E_GEN_RSA_PEM          			=$F0000017;	//** 产生RSA PEM文件失败*/
const   R1_E_PEM_BITS_ERR          			=$F0000018;	//** RSA PEM文件与设置的位数不匹配*/
const   R1_E_RSA_KEY_1024          			=$F0000019;  //** RSA 密钥对文件必须是1024*/

//! 底层设备的错误码
const ERR_SUCCEF     		    =$d0;//成功
const ERR_VERIFYUSERPIN	  	=$d1;//验证用户密码失败
const ERR_VERIFYSOPIN		    =$d2;//验证开发商密码失败
const ERR_NOUSRPERMISSION   =$d3;//没有usr权限
const ERR_UPCOUNTUSRPIN     =$d4;//已超过usrpin设置上限
const ERR_NOSOPERMISSION    =$d5;//没有开发商权限
const ERR_UPCOUNTSOPIN      =$d6;//已超过sopin设置上限
const ERR_UPREADSOMEMORY	  =$d7;//读私有内存越界错误
const ERR_UPREADUSRMEMORY	  =$d8;//读公有内存越界错误
const ERR_UPWRITESOMEMORY	  =$d9;//写私有内存越界错误
const ERR_UPWRITEUSRMEMORY  =$da;//写公有内存越界错误
const ERR_RESETSTATE  	    =$db;//返回匿名态错误
const ERR_NODESKEY  	      =$dc;//未知的DESKEYID
const ERR_NOFILE			      =$dd;//未知的文件格式
const ERR_NOHID			        =$de;//错误的HID
const ERR_NOPID			        =$df;//错误的PID
const ERR_UPDATE_TIME		    =$e0;//升级包已过期
const ERR_PARAMETERS		    =$e1;//参数错误
const ERR_UNFINISHED		    =$e2;//数据包接收不完全
const ERR_INACTIVE		      =$e3;//COS未激活
const ERR_NOKEY			        =$e4;//密钥未设置
const ERR_AUTHORIZATION	    =$e5;//授权已用完
const ERR_CLEANCOS          =$e6;//清COS错误
const ERR_PID_IS_F          =$e7;//pid的8个字节都为=$ff错误
const ERR_RSA_ENC_DATA      =$ec;//使用512对数据加密，数据每64个字节的前两个字节不为=0x00,使用1024对数据加密，数据每128个字节的前两个字节不为=0x00错误
const ERR_CHECK_SUM		      =$ed;//校验和错误

const   R1_E_ERROR_TONGXIN	=$FFFFFFFE;	//** 通信错误*/
const   R1_E_ERROR_UNKNOWN	=$FFFFFFFF;	//** 未知的错误*/

const   MEM_PUB        =$00;
const   MEM_PRI        =$01;

const  LED_OFF         =$00;
const  LED_ON          =$01;

const  RSA_PAIRS_512   =$00;
const  RSA_PAIRS_1024  =$01;

const  PUB_KEY         =$00;
const  PRI_KEY         =$01;


const Update_TYPE_HID		   =$01;	 //HID
const Update_TYPE_PID		   =$02;	 //PID
const Update_TYPE_Count	   =$03;	 //计次
const Update_TYPE_PubKey   =$04;   //公钥数据
const Update_TYPE_PriKey   =$05;   //私钥数据
const Update_TYPE_3DesKey  =$06;   //3DES密钥数据
const Update_TYPE_Memory   =$07;   //8192字节数据区

msvcrtdll       = 'msvcrt.dll';
hiddll          = 'HID.DLL';
setupapidll     = 'SETUPAPI.DLL';
ws2_32dll       = 'WSOCK32.dll';
Netapi32dll     = 'Netapi32.dll';
libeay32dll     = 'libeay32.dll';
kernel32dll     = 'kernel32.dll';
User32dll       = 'User32.dll';
OleAut32dll     = 'OleAut32.dll';

BN_CTX_NUM = 16;
BN_CTX_NUM_POS = 12;
	
type

{$IF CompilerVersion >= 20}
  PCharacter = PAnsiChar;
{$ELSE}
  PCharacter = PChar;
{$IFEND}

  PWord = ^Word;
  PLongword = ^Longword;
  PHIDP_PREPARSED_DATA = ^Byte;
  PPHIDP_PREPARSED_DATA = ^PHIDP_PREPARSED_DATA;
  PCSTR = ^Char;
  PByte = ^Byte;
  psize_t = ^size_t;
  size_t = Cardinal;
  pRSA = pointer;
  pBIO_METHOD = pointer;
  pBIO = pointer;
  pEVP_CIPHER = pointer;

  UpdateFileHeader = packed record
	  bSign:Array [0..3] of Byte;                                //** 升级文件标识。0x4654524b,*/
	  bVersion:Array [0..1] of Byte;                            //** 升级文件版本信息。第一个字节是主版本号,第二个字节是次版本号*/
    bBind:Byte;                                   //** 升级文件绑定信息。0x00 什么也不绑定。0x01绑定HID.0x02绑定PID*/
    bReserve:Byte;                                //** 保留*/
    bHid:Array [0..19] of Byte;                                //** 升级文件要绑定的HID*/
    bPid:Array [0..19] of Byte;                                //** 升级文件要绑定的PID*/
	  dwDate:DWord;                                //** 日期*/
  end;
  PUpdateFileHeader = ^UpdateFileHeader;

  RSAKey = packed record
    bID:Byte;                                     //** 如果是RSA密钥对，最高位=0代表是512，=1代表是1024.剩下7位代表RSA密钥对序号（0~7）*/
	  bPubkey:Array [0..1023] of Byte;
	  bPrikey:Array [0..1023] of Byte;
  end;
  PRSAKey = ^RSAKey;

  TDESKey = packed record
	  bID:Byte;                                     //** 如果是3DES密钥对，代表3DES密钥序号（0~7）*/
	  bTDESkey:Array [0..15] of Byte;
  end;
  PTDESKey = ^TDESKey;

  GUID = record
        Data1:Longword;
        Data2:Word;
        Data3:Word;
        Data4: array [0..7] of Byte;
  end;

  Array128 =  array[0..127]of WORD;
  pArray128 = ^Array128;

  LPGUID = ^GUID;

  HIDP_CAPS = record
        Usage:Word;
        UsagePage:Word;
        InputReportByteLength:Word;
        OutputReportByteLength:Word;
        FeatureReportByteLength:Word;
        Reserved: array [0..16] of Word;
        NumberLinkCollectionNodes:Word;
        NumberInputButtonCaps:Word;
        NumberInputValueCaps:Word;
        NumberInputDataIndices:Word;
        NumberOutputButtonCaps:Word;
        NumberOutputValueCaps:Word;
        NumberOutputDataIndices:Word;
        NumberFeatureButtonCaps:Word;
        NumberFeatureValueCaps:Word;
        NumberFeatureDataIndices:Word;
  end;

  PHIDP_CAPS = ^HIDP_CAPS;

  HIDD_ATTRIBUTES = record
        Size:Longword;
        VendorID:Word;
        ProductID:Word;
        VersionNumber:Word;
  end;

  PHIDD_ATTRIBUTES = ^HIDD_ATTRIBUTES;

  SP_DEVICE_INTERFACE_DATA = record
        cbSize:Longword;
        InterfaceClassGuid:GUID;
        Flags:Longword;
        Reserved:Longword;
  end;

  PSP_DEVICE_INTERFACE_DATA = ^SP_DEVICE_INTERFACE_DATA;

  SP_DEVICE_INTERFACE_DETAIL_DATA_A = record
        cbSize:Longword;
        DevicePath: array [0..4096] of Char;
  end;

  PSP_DEVICE_INTERFACE_DETAIL_DATA_A = ^SP_DEVICE_INTERFACE_DETAIL_DATA_A;
  PSP_DEVICE_INTERFACE_DETAIL_DATA = ^SP_DEVICE_INTERFACE_DETAIL_DATA_A;

  SP_DEVINFO_DATA = record
        cbSize:Longword;
        ClassGuid:GUID;
        DevInst:Longword;
        Reserved:Longword;
  end;

  PSP_DEVINFO_DATA = ^SP_DEVINFO_DATA;
  Feature_Report = packed record
        id: Byte;
        command: array[0..4] of Byte;
        data: array[0..17] of Byte;
        CrcData: Byte;
  end;

  PFeature_Report = ^Feature_Report;

  Device_Descript = record
        device_path: array[0..255] of Char;
        device_handle: THandle;
        uid: Longword;
        hid: Longword;
        hver: Word;
        mutex: THandle;
        KeyPass:array[0..8] of char;
        Encrypt:integer;
  end;

  tm = record
        tm_sec:Integer;     //* seconds after the minute - [0,59] */
        tm_min:Integer;     //* minutes after the hour - [0,59] */
        tm_hour:Integer;    //* hours since midnight - [0,23] */
        tm_mday:Integer;    //* day of the month - [1,31] */
        tm_mon:Integer;     //* months since January - [0,11] */
        tm_year:Integer;    //* years since 1900 */
        tm_wday:Integer;    //* days since Sunday - [0,6] */
        tm_yday:Integer;    //* days since January 1 - [0,365] */
        tm_isdst:Integer;   //* daylight savings time flag */
   end;
   PTm = ^tm;

  pBN_ULONG = ^BN_ULONG;
  BN_ULONG = array of byte;
  BIGNUM = record
	  d: pBN_ULONG;	// Pointer to an array of 'BN_BITS2' bit chunks.
	  top: integer;	// Index of last used d +1.
                        // The next are internal book keeping for bn_expand.
	  dmax: integer;	// Size of the d array.
	  neg: integer;	// one if the number is negative
	  flags: integer;
  end;
   pBIGNUM = ^BIGNUM;

   pBN_CTX = ^BN_CTX;
  BN_CTX = record
	  tos: integer;
	  bn: array [0..BN_CTX_NUM-1] of BIGNUM;
	  flags: integer;
	  depth: integer;
	  pos: array [0..BN_CTX_NUM_POS-1] of integer;
	  too_many: integer;
   end;

  pBN_RECP_CTX = ^BN_RECP_CTX;
  BN_RECP_CTX = record
	  N: BIGNUM;	// the divisor
	  Nr: BIGNUM;	// the reciprocal
	  num_bits: integer;
	  shift: integer;
	  flags: integer;
	end;

  SHA_CTX = record
    h0, h1, h2, h3, h4: cardinal;
    Nl, Nh: cardinal;
    data: array [0..16] of cardinal;
    num: integer;
  end;
  pSHA_CTX = ^SHA_CTX;

  SYSTEMTIME = record
    wYear:WORD;
    wMonth:WORD;
    wDayOfWeek:WORD;
    wDay:WORD;
    wHour:WORD;
    wMinute:WORD;
    wSecond:WORD;
    wMilliseconds:WORD;
  end;
  PSYSTEMTIME = ^SYSTEMTIME;
  LPSYSTEMTIME = ^SYSTEMTIME;

   TPWCallbackFunction = function(buffer: PCharacter; length: integer;
      verify: integer; data: pointer): integer; cdecl;
   TProgressCallbackFunction = procedure(status: integer; progress: integer;
      data: pointer);

	// @brief  查找计算机上指定PID的加密锁,返回找到指定PID的加密锁的个数
	function R1_Find( pid : PByte ; dwCount :PLongWord):LongWord;stdcall;
	{$EXTERNALSYM R1_Find}

	//打开计算机上指定的加密锁。
	function R1_Open( handle:PHANDLE; pid:PByte; nIndex:Integer):LongWord;stdcall;
	{$EXTERNALSYM R1_Open}

	//关闭计算机上指定的加密锁。
	function R1_Close( handle:THandle):LongWord;stdcall;
	{$EXTERNALSYM R1_Close}

	//获得计算机上指定PID的加密锁的版本信息
	function R1_GetVersion( handle:THandle; bVersionMajor:PByte; bVersionMinor:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_GetVersion}

	//控制计算机上LED。(用户或者开发商权限)
	function R1_LEDControl( handle:THandle; bFlag:Byte):LongWord;stdcall;
	{$EXTERNALSYM R1_LEDControl}

	//重新产生计算机上指定加密锁的PID。需要开发商权限
	function R1_ProducePID( handle:THandle; seed:PByte; leng:BYTE; pid:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_ProducePID}

	//获取计算机上指定加密锁的HID
	function R1_GetHID( handle:THandle; hid:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_GetHID}

	//获取计算机上指定加密锁的PID
	function R1_GetPID( handle:THandle; pid:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_GetPID}

	//核实计算机上指定加密锁的用户密码
	function R1_VerifyUserPin( handle:THandle; pin:PByte; tryCount:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_VerifyUserPin}

	//修改计算机上指定加密锁的用户密码,(匿名权限),当有开发商密码的权限时，直接传入新的用户口令即可
	function R1_ChangeUserPin( handle:THandle; oldPin:PByte; newPin:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_ChangeUserPin}

	//重置计算机上指定加密锁的用户密码。需要开发商密码
	function R1_ResetUserPin( handle:THandle):LongWord;stdcall;
	{$EXTERNALSYM R1_ResetUserPin}

	//设置计算机上指定加密锁的用户密码的重试次数。(需要开发商权限)
	//如果不调用设置重置次数，加密锁默认是255，即没有限制
	function R1_SetTryCountForUserPin( handle:THandle; tryCount:Byte):LongWord;stdcall;
	{$EXTERNALSYM R1_SetTryCountForUserPin}

	//核实计算机上指定加密锁的开发商密码
	function R1_VerifySoPin( handle:THandle; pin:PByte; tryCount:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_VerifySoPin}

	//通过种子生成开发商的密码，(开发商权限)
	function R1_ProduceSoPin( handle:THandle; seed:PByte; len:BYTE; newPin:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_ProduceSoPin}

	//产生随机数，(用户权限)
	function R1_GenRandom( handle:THandle; buffer:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_GenRandom}

	//设置计算机上指定加密锁的开发商密码的重试次数。(需要开发商权限)
	//如果不调用设置重置次数，加密锁默认是255，即没有限制
	function R1_SetTryCountForSoPin( handle:THandle; tryCount:Byte):LongWord;stdcall;
	{$EXTERNALSYM R1_SetTryCountForSoPin}

	//读取计算机上指定加密锁的指定内存。
	//公有内存私有内存都是0~4k
	//开发商权限可以读写公有内存，读写私有内存
	//用户权限可以读写公有内存，读私有内存
	function R1_Read( handle:THandle; offset:WORD; length:WORD; buf:PByte; len:PWORD; bType:BYTE):LongWord;stdcall;
	{$EXTERNALSYM R1_Read}

	//写计算机上指定加密锁的指定内存。
	//公有内存私有内存都是0~4k
	//开发商权限可以读写公有内存，读写私有内存
	//用户权限可以读写公有内存，读私有内存
	function R1_Write( handle:THandle; offset:WORD; length:WORD; buf:PByte; len:PWORD; bType:BYTE):LongWord;stdcall;
	{$EXTERNALSYM R1_Write}

	///重置锁的安全状态，回到匿名状态
	function R1_ResetSecurityState( handle:THandle):LongWord;stdcall;
	{$EXTERNALSYM R1_ResetSecurityState}

	//设置3DES密钥。(需要开发商权限)
	function R1_SetTDesKey( handle:THandle; id:BYTE; buf:PByte):LongWord;stdcall;
	{$EXTERNALSYM R1_SetTDesKey}

	//利用3DES密钥加密。(需要用户权限)
	function R1_TDesEnc( handle:THandle; id:BYTE; buf:PByte; len:DWORD):LongWord;stdcall;
	{$EXTERNALSYM R1_TDesEnc}

	//利用3DES密钥解密。(需要用户权限)
	function R1_TDesDec( handle:THandle; id:BYTE; buf:PByte; len:DWORD):LongWord;stdcall;
	{$EXTERNALSYM R1_TDesDec}

	//产生RSA密钥对。(需要开发商权限)
	function R1_GenRSAKey( handle:THandle; bFlag:BYTE; id:BYTE; pubkey:PChar; prikey:PChar):LongWord;stdcall;
	{$EXTERNALSYM R1_GenRSAKey}

	//设置RSA密钥对。(需要开发商权限)
	function R1_SetRSAKey( handle:THandle; bFlag:BYTE; id:BYTE; pubkey:PChar; prikey:PChar):LongWord;stdcall;
	{$EXTERNALSYM R1_SetRSAKey}

	//利用RSA密钥对加密。(需要用户权限)
	//受RSA算法本身限制。如果是512密钥对，则每64字节的前两字节必须是0，如果是1024密钥对，则每128字节的前两字节必须是0
	function R1_RSAEnc( handle:THandle; bFlag:BYTE; id:BYTE; bBuf:PByte; len:DWORD):LongWord;stdcall;
	{$EXTERNALSYM R1_RSAEnc}

	//利用RSA密钥对解密。(需要用户权限)
	//受RSA算法本身限制。如果是512密钥对，则每64字节的前两字节必须是0，如果是1024密钥对，则每128字节的前两字节必须是0
	function R1_RSADec( handle:THandle; bFlag:BYTE; id:BYTE; bBuf:PByte; len:DWORD):LongWord;stdcall;
	{$EXTERNALSYM R1_RSADec}

	//设置计次模块次数。(需要开发商权限)
	function R1_SetCounter( handle:THandle; bCount:BYTE):LongWord;stdcall;
	{$EXTERNALSYM R1_SetCounter}

	//获得计次模块次数。(需要用户权限)
	function R1_GetCounter( handle:THandle; bCount:PBYTE):LongWord;stdcall;
	{$EXTERNALSYM R1_GetCounter}

	//设置升级包数据。(匿名权限)
	function R1_SetUpdatePacket( UpdateType:DWORD; first:WORD; second:WORD; pBuf:PByte; len:DWORD):LongWord;stdcall;
	{$EXTERNALSYM R1_SetUpdatePacket}

	//制作升级包。(匿名权限)
	function R1_GenUpdatePacket( pubkey:PChar; pEncBuf:PBYTE; pEncLen:PWORD):LongWord;stdcall;
	{$EXTERNALSYM R1_GenUpdatePacket}

	//升级。(匿名权限)
	function R1_Update( handle:THandle; bPacket:PBYTE; len:WORD):LongWord;stdcall;
	{$EXTERNALSYM R1_Update}

	//根据错误码得到错误信息。(匿名权限)
	function R1_GetErrInfo( Error:DWORD):LongWord;stdcall;
	{$EXTERNALSYM R1_GetErrInfo}

  {$EXTERNALSYM HidD_FreePreparsedData}
  function HidD_FreePreparsedData (PreparsedData:PHIDP_PREPARSED_DATA):Boolean;cdecl;
  {$EXTERNALSYM HidP_GetCaps}
  function HidP_GetCaps (PreparsedData:PHIDP_PREPARSED_DATA; Capabilities:PHIDP_CAPS):Longint;cdecl;
  {$EXTERNALSYM HidD_GetPreparsedData}
  function HidD_GetPreparsedData (HidDeviceObject:THandle; PreparsedData:PPHIDP_PREPARSED_DATA):Boolean;cdecl;
  {$EXTERNALSYM HidD_SetFeature}
  function HidD_SetFeature (HidDeviceObject:THandle; ReportBuffer:pointer; ReportBufferLength:Longword):Boolean;cdecl;
  {$EXTERNALSYM HidD_GetFeature}
  function HidD_GetFeature (HidDeviceObject:THandle; ReportBuffer:pointer; ReportBufferLength:Longword):Boolean;cdecl;
  {$EXTERNALSYM HidD_FlushQueue}
  function HidD_FlushQueue (HidDeviceObject:THandle):Boolean;cdecl;
  {$EXTERNALSYM HidD_GetSerialNumberString}
  function HidD_GetSerialNumberString(HidDeviceObject:THandle; Buffer:PChar; BufferLength:Longword):Boolean;cdecl;
  {$EXTERNALSYM HidD_GetProductString}
  function HidD_GetProductString(HidDeviceObject:THandle; Buffer:PChar; BufferLength:Longword):Boolean;cdecl;
  {$EXTERNALSYM HidD_GetAttributes}
  function HidD_GetAttributes (HidDeviceObject:THandle; Attributes:PHIDD_ATTRIBUTES):Boolean;cdecl;
  {$EXTERNALSYM HidD_GetHidGuid}
  function HidD_GetHidGuid ( HidGuid:LPGUID ):integer;cdecl;
  {$EXTERNALSYM HidD_GetManufacturerString}
  function HidD_GetManufacturerString(HidDeviceObject:THANDLE;Buffer:PCHAR; BufferLength:ULONG):Boolean;cdecl;

  {$EXTERNALSYM SetupDiDestroyDeviceInfoList}
  function SetupDiDestroyDeviceInfoList (DeviceInfoSet:THandle):Boolean;cdecl;
  {$EXTERNALSYM SetupDiGetDeviceInterfaceDetailA}
  function SetupDiGetDeviceInterfaceDetailA (DeviceInfoSet:THandle; DeviceInterfaceData:PSP_DEVICE_INTERFACE_DATA; DeviceInterfaceDetailData:PSP_DEVICE_INTERFACE_DETAIL_DATA_A; DeviceInterfaceDetailDataSize:Longword; RequiredSize:PLongword; DeviceInfoData:PSP_DEVINFO_DATA):Boolean;cdecl;
  {$EXTERNALSYM SetupDiEnumDeviceInterfaces}
  function SetupDiEnumDeviceInterfaces (DeviceInfoSet:THandle; DeviceInfoData:PSP_DEVINFO_DATA; InterfaceClassGuid:LPGUID; MemberIndex:Longword; DeviceInterfaceData:PSP_DEVICE_INTERFACE_DATA):Boolean;cdecl;
  {$EXTERNALSYM SetupDiGetClassDevsA}
  function SetupDiGetClassDevsA (ClassGuid:LPGUID; Enumerator:PCSTR; hwndParent:HWND; Flags:Longword):THandle;cdecl;

  {$EXTERNALSYM _wsprintfW}
  function _wsprintfW(lpOut:LPWSTR; lpFmt:LPCWSTR; str:size_t): Integer; cdecl;
  {$EXTERNALSYM _wcslen}
  function _wcslen(const str: PWideChar): size_t; cdecl;
  {$EXTERNALSYM _wcscpy}
  function _wcscpy(const strDestination: PWideChar; strSource:PWideChar): PWideChar; cdecl;
  {$EXTERNALSYM _memset}
  function _memset(s: Pointer; c: Integer; n: size_t): Pointer; cdecl;
  {$EXTERNALSYM _memcpy}
  function _memcpy(s1: Pointer; const s2: Pointer; n: size_t): Pointer; cdecl;
  {$EXTERNALSYM _memcmp}
  function _memcmp(s1: Pointer; const s2: Pointer; n: size_t): Integer; cdecl;
  {$EXTERNALSYM _strcpy}
  function _strcpy(s1: PChar; const s2: PChar): PChar; cdecl;
  {$EXTERNALSYM _strlen}
  function _strlen(const s: PChar): size_t; cdecl;
  {$EXTERNALSYM _strcmp}
  function _strcmp(const s1, s2: PChar): Integer; cdecl;
  {$EXTERNALSYM _strcat}
  function _strcat(dest: PChar; src: PChar): PChar; cdecl;
  {$EXTERNALSYM _strncmp}
  function _strncmp(const s1, s2: PChar; n: size_t): Integer; cdecl;
  {$EXTERNALSYM _strncpy}
  function _strncpy(s1: PChar; const s2: PChar; n: size_t): PChar; cdecl;
  {$EXTERNALSYM _sscanf}
  function _sscanf(const s, format: PChar): Integer; cdecl; varargs;
  {$EXTERNALSYM _sprintf}
  function _sprintf(s: PChar; const format: PChar): Integer; cdecl; varargs;
  {$EXTERNALSYM __ltoa}
  function __ltoa(value : Integer; s: PChar; radix: Integer): PChar; cdecl;
  {$EXTERNALSYM _atol}
  function _atol(const s: PChar): Integer; cdecl;
  {$EXTERNALSYM _atof}
  function _atof(const s: PChar): Double; cdecl;
  {$EXTERNALSYM _malloc}
  function _malloc(size : size_t) : Pointer; cdecl;
  {$EXTERNALSYM _free}
  function _free(size : size_t) : Pointer; cdecl;
  {$EXTERNALSYM _rand}
  function _rand() : Integer; cdecl;
  {$EXTERNALSYM _srand}
  procedure _srand(seed : size_t);cdecl;

  {$EXTERNALSYM _mktime}
  function _mktime(dtm:PTm):Int64;cdecl;
  function _localtime(const time:PInt64):PTm;cdecl;

  function __ftol(f:Real):Integer;cdecl;

  function SystemTimeToVariantTime(lpSystemTime:LPSYSTEMTIME; pvtime:PDouble):Integer;cdecl;
  function VariantTimeToSystemTime(pvtime:Double; lpSystemTime:LPSYSTEMTIME):Integer;cdecl;

  //Openssl
  procedure _RSA_free(r: pRSA); cdecl;
  function _RSA_size(pkey: pRSA): integer; cdecl;
  function _RSA_new: pRSA; cdecl;
  function _RSA_check_key(arg0: pRSA): integer; cdecl;
  function _RSA_generate_key(bits: integer; exp: Cardinal;
    progress: TProgressCallbackFunction; cb_arg: pointer):pRSA; cdecl;
  function _RSA_public_encrypt(flen: integer; from: PCharacter; _to: PCharacter; rsa: pRSA; padding: integer): integer; cdecl;
  function _RSA_public_decrypt(flen: integer; from: PCharacter; _to: PCharacter; rsa: pRSA; padding: integer): integer; cdecl;
  function _BIO_new(_type: pBIO_METHOD): pBIO; cdecl;
  function _BIO_s_file: pBIO_METHOD; cdecl;
  function _BIO_ctrl(bp: pBIO; cmd: Integer; larg: Longint;
    parg: Pointer): Longint; cdecl;
  procedure _BIO_free_all(a: pBIO); cdecl;
  function _BIO_free(a: pBIO): integer; cdecl;
  function _BIO_write(b: pBIO; const buf: pointer; len: integer): integer; cdecl;
  function _BIO_s_mem: pBIO_METHOD; cdecl;
  function _PEM_read_bio_RSAPublicKey(bp: pBIO; var x: pRSA;
    cb: TPWCallbackFunction; u: pointer): pRSA; cdecl;
  function _PEM_read_bio_RSAPrivateKey(bp: pBIO; var x: pRSA;
    cb: TPWCallbackFunction; u: pointer): pRSA; cdecl;
  function _PEM_write_bio_RSAPrivateKey(bp: pBIO; x: pRSA; const enc: pEVP_CIPHER;
    kstr: PCharacter; klen: integer; cb: TPWCallbackFunction;
    u: pointer): integer; cdecl;
  function _PEM_write_bio_RSAPublicKey(bp: pBIO; x: pRSA): integer; cdecl;
  function _BN_div(dv: pBIGNUM; rem: pBIGNUM; const a, d: pBIGNUM; ctx: pBN_CTX): integer; cdecl;
  function _BN_new(): pBIGNUM; cdecl;
  function _BN_sub(r: pBIGNUM; const a, b: pBIGNUM): integer; cdecl;
  function _BN_value_one(): pBIGNUM; cdecl;
  procedure _BN_free(bn: pBIGNUM); cdecl;
  function _BN_CTX_new: pBN_CTX; cdecl;
  function _BN_mod(rem: pBIGNUM; const a, m: pBIGNUM; ctx: pBN_CTX): integer;cdecl;
  procedure _BN_CTX_free(ctx: pBN_CTX); cdecl;
  function _BN_mod_inverse(ret, a: pBIGNUM; const n: pBIGNUM; ctx: pBN_CTX): pBIGNUM; cdecl;


  procedure _SHA1_Init(c:pSHA_CTX);cdecl;
  procedure _SHA1_Update(c:pSHA_CTX; const data:pointer; len:cardinal);cdecl;
  procedure _SHA1_Final(md:PChar; c:pSHA_CTX); cdecl;

implementation
  {$LINK 'Rockey1Smart.obj'} //链接外部OBJ文件
  function R1_Find;external;
  function R1_Open;external;
  function R1_Close;external;
  function R1_GetVersion;external;
  function R1_LEDControl;external;
  function R1_ProducePID;external;
  function R1_GetHID;external;
  function R1_GetPID;external;
  function R1_VerifyUserPin;external;
  function R1_ChangeUserPin;external;
  function R1_ResetUserPin;external;
  function R1_SetTryCountForUserPin;external;
  function R1_VerifySoPin;external;
  function R1_ProduceSoPin;external;
  function R1_GenRandom;external;
  function R1_SetTryCountForSoPin;external;
  function R1_Read;external;
  function R1_Write;external;
  function R1_ResetSecurityState;external;
  function R1_SetTDesKey;external;
  function R1_TDesEnc;external;
  function R1_TDesDec;external;
  function R1_GenRSAKey;external;
  function R1_SetRSAKey;external;
  function R1_RSAEnc;external;
  function R1_RSADec;external;
  function R1_SetCounter;external;
  function R1_GetCounter;external;
  function R1_SetUpdatePacket;external;
  function R1_GenUpdatePacket;external;
  function R1_Update;external;
  function R1_GetErrInfo;external;

  //hid function
  function HidD_FreePreparsedData ; external hiddll name 'HidD_FreePreparsedData' ;
	function HidP_GetCaps; external hiddll name 'HidP_GetCaps' ;
	function HidD_GetPreparsedData ; external hiddll name 'HidD_GetPreparsedData' ;
	function HidD_SetFeature ; external hiddll name 'HidD_SetFeature' ;
	function HidD_GetFeature ; external hiddll name 'HidD_GetFeature' ;
	function HidD_FlushQueue ; external hiddll name 'HidD_FlushQueue';
	function HidD_GetSerialNumberString; external hiddll name 'HidD_GetSerialNumberString';
	function HidD_GetProductString; external hiddll name 'HidD_GetProductString';
	function HidD_GetAttributes ; external hiddll name 'HidD_GetAttributes';
	function HidD_GetHidGuid ; external hiddll name 'HidD_GetHidGuid';
	function HidD_GetManufacturerString; external hiddll name 'HidD_GetManufacturerString';

	//setupapi function
	function SetupDiDestroyDeviceInfoList ; external setupapidll name 'SetupDiDestroyDeviceInfoList';
	function SetupDiGetDeviceInterfaceDetailA ; external setupapidll name 'SetupDiGetDeviceInterfaceDetailA';
	function SetupDiEnumDeviceInterfaces ;external setupapidll name 'SetupDiEnumDeviceInterfaces' ;
	function SetupDiGetClassDevsA ; external setupapidll name 'SetupDiGetClassDevsA';

	//msvcrt function
	function _wcslen; external msvcrtdll name 'wcslen';
	function _wcscpy; external msvcrtdll name 'wcscpy';
	function _memset; external msvcrtdll name 'memset';
	function _memcpy; external msvcrtdll name 'memcpy';
	function _memcmp; external msvcrtdll name 'memcmp';
	function _strcpy; external msvcrtdll name 'strcpy';
	function _strlen; external msvcrtdll name 'strlen';
	function _strcmp; external msvcrtdll name 'strcmp';
	function _strncmp; external msvcrtdll name 'strncmp';
	function _strncpy; external msvcrtdll name 'strncpy';
	function _sscanf; external msvcrtdll name 'sscanf';
	function _sprintf; external msvcrtdll name 'sprintf';
	function __ltoa; external msvcrtdll name '_ltoa';
	function _atol; external msvcrtdll name 'atol';
	function _atof; external msvcrtdll name 'atof';
	function _malloc; external msvcrtdll name 'malloc';
	function _free; external msvcrtdll name 'free';
	function _rand;external msvcrtdll name 'rand';
	procedure _srand;external msvcrtdll name 'srand';
	function _strcat;external msvcrtdll name 'strcat';
	function _mktime;external msvcrtdll name 'mktime';
  function _localtime;external msvcrtdll name 'localtime';
  function __ftol;external msvcrtdll name '_ftol';

  function _wsprintfW;external User32dll name 'wsprintfW';

  function SystemTimeToVariantTime;external OleAut32dll name 'SystemTimeToVariantTime';
  function VariantTimeToSystemTime;external OleAut32dll name 'VariantTimeToSystemTime';

  procedure _RSA_free; external libeay32dll name 'RSA_free';
  function _RSA_size; external libeay32dll name 'RSA_size';
  function _RSA_new; external libeay32dll name 'RSA_new';
  function _RSA_check_key; external libeay32dll name 'RSA_check_key';
  function _RSA_generate_key; external libeay32dll name 'RSA_generate_key';
  function _RSA_public_encrypt; external libeay32dll name 'RSA_public_encrypt';
  function  _RSA_public_decrypt; external libeay32dll name 'RSA_public_decrypt';
  function _BIO_new; external libeay32dll name 'BIO_new';
  function _BIO_s_file; external libeay32dll name 'BIO_s_file';
  function _BIO_ctrl; external libeay32dll name 'BIO_ctrl';
  procedure _BIO_free_all; external libeay32dll name 'BIO_free_all';
  function _BIO_free; external libeay32dll name 'BIO_free';
  function _BIO_write; external libeay32dll name 'BIO_write';
  function _BIO_s_mem; external libeay32dll name 'BIO_s_mem';
  function _PEM_read_bio_RSAPublicKey; external libeay32dll name 'PEM_read_bio_RSAPublicKey';
  function _PEM_read_bio_RSAPrivateKey; external libeay32dll name 'PEM_read_bio_RSAPrivateKey';
  function _PEM_write_bio_RSAPrivateKey; external libeay32dll name 'PEM_write_bio_RSAPrivateKey';
  function _PEM_write_bio_RSAPublicKey; external libeay32dll name 'PEM_write_bio_RSAPublicKey';
  function _BN_div; external libeay32dll name 'BN_div';
  function _BN_new; external libeay32dll name 'BN_new';
  function _BN_sub; external libeay32dll name 'BN_sub';
  function _BN_value_one; external libeay32dll name 'BN_value_one';
  procedure _BN_free; external libeay32dll name 'BN_free';
  function _BN_CTX_new; external libeay32dll name 'BN_CTX_new';
  function BN_mod(rem: pBIGNUM; const a, m: pBIGNUM; ctx: pBN_CTX): integer;
  begin
    result := _BN_div(nil, rem, a, m, ctx);
  end;
  function _BN_mod;external libeay32dll name 'BN_mod';
  procedure _BN_CTX_free; external libeay32dll name 'BN_CTX_free';
  function _BN_mod_inverse; external libeay32dll name 'BN_mod_inverse';

  procedure _SHA1_Init;external libeay32dll name 'SHA1_Init';
  procedure _SHA1_Update;external libeay32dll name 'SHA1_Update';
  procedure _SHA1_Final;external libeay32dll name 'SHA1_Final';

  var __turboFloat: LongBool = False;
end.
