diff --git a/libs/SDK/include/ReaderInterface.h b/libs/SDK/include/ReaderInterface.h index 3611055..baaa9e8 100755 --- a/libs/SDK/include/ReaderInterface.h +++ b/libs/SDK/include/ReaderInterface.h @@ -37,11 +37,9 @@ * You should assume that an application implemenation of this delegate will call [ReaderInterface cardInterface] to obtian a card interface. * @param attached is YES if the 301 has become attached to the phone or NO if the 301 has become detached from the phone. */ -- (void) readerInterfaceDidChange: -(BOOL)attached; +- (void) readerInterfaceDidChange:(BOOL)attached; -- (void) cardInterfaceDidDetach: -(BOOL)attached; +- (void) cardInterfaceDidDetach:(BOOL)attached; @end @@ -54,9 +52,9 @@ * methods of the CardInterface class. */ -@interface ReaderInterface : -NSObject { - +@interface ReaderInterface : NSObject +{ + } /** @@ -65,8 +63,7 @@ NSObject { * Our applicaiton layer might set this back to nil when notifications are no longer desired. * @param delegate is the caller provided delegate. */ -- (void) setDelegate: -(id)delegate; +- (void) setDelegate:(id)delegate; /** * Query if our custom version of the 301 is currently attached to the phone. diff --git a/libs/SDK/include/ft301u.h b/libs/SDK/include/ft301u.h index 2046977..aebc133 100755 --- a/libs/SDK/include/ft301u.h +++ b/libs/SDK/include/ft301u.h @@ -1,215 +1,215 @@ -/* -* Support for bR301(Bluetooth) smart card reader -* -* ft301u.h: header file for ft_ccid.c -* -* Copyright (C) Feitian 2014, Ben -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 2.1 of the License, or (at your option) any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public -* License along with this library; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -/** - * @file - * @This isfeitian's private cmd. - */ - -#ifndef __ft301u_h__ -#define __ft301u_h__ - - -#include "wintypes.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - LONG SCARD_CTL_CODE(unsigned int code); - /* - Function: FtGetSerialNum - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - length IN length of buffer(>=8) - buffer OUT Serial number - - Description: - This function userd to get serial number of iR301. - */ - - LONG FtGetSerialNum(SCARDHANDLE hCard, unsigned int length, - char * buffer); - /* - Function: FtWriteFlash - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - bOffset IN Offset of flash to write - blength IN The length of data - buffer IN The data for write - - Description: - This function userd to write data to flash. - */ - LONG FtWriteFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, - unsigned char buffer[]); - /* - Function: FtReadFlash - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - bOffset IN Offset of flash to write - blength IN The length of read data - buffer OUT The read data - - Description: - This function used to read data from flash. - */ - LONG FtReadFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, - unsigned char buffer[]); - - /* - Function: FtSetTimeout - - Parameters: - hContext IN Connection context to the PC/SC Resource Manager - dwTimeout IN New transmission timeout value of between 301 and card (millisecond ) - - Description: - The function New transmission timeout value of between 301 and card. - - */ - LONG FtSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout); - - - //for dukpt - /* - Function: FtDukptInit - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - encBuf IN Ciphertext use TDES_ECB_PKCS7/AES_ECB_PKCS7 (See "Key C" ) - nLen IN encBuf length(40(TDES_ECB_PKCS7 ciphertext length) 48(AES_ECB_PKCS7 ciphertext length)) - - Description: - Init iR301 new ipek and ksn for dukpt. - - */ - LONG FtDukptInit(SCARDHANDLE hCard,unsigned char *encBuf,unsigned int nLen); - - - /* - Function: FtDukptSetEncMod - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - bEncrypt IN 1: SCardTransmit Encrypted - 0:SCardTransmit not Encrypted - - bEncFunc IN 0: Encryption functioon use TDES_ECB_PKCS7 - 1:Encryption functioon use AES_ECB_PKCS7 - - bEncType IN 1: SCardTransmit: Plaintext in Ciphertext out - 0: SCardTransmit: Ciphertext in Ciphertext out - - Description: - Set the encryption mode of iR301 for dukpt. - - */ - LONG FtDukptSetEncMod(SCARDHANDLE hCard, - unsigned int bEncrypt, - unsigned int bEncFunc, - unsigned int bEncType); - - - - - /* - Function: FtDukptGetKSN - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - pnlength INOUT IN: The size of ksn buffer(>=10) - OUT: The real size(if successful has been 10) - buffer OUT Buffer of ksn - - Description: - Get Ksn from iR301 for dukpt. - - */ - - LONG FtDukptGetKSN(SCARDHANDLE hCard,unsigned int * pnlength,unsigned char *buffer); - /* - Function: FtDidEnterBackground - - Parameters: - bDidEnter IN must be set 1 - - - Description: - Use this method to release monitor thread of reader status - - */ - void FtDidEnterBackground(unsigned int bDidEnter); - /* - Function: FtSle4442Cmd - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - pbCmd IN SLE4442 cmd(3 byte) which are listed in sle4442 cmd table - bLengthToRead IN The data length which reading from the card - bIsClockNum IN If bIsClockNum = 1, then bToReadLength should be '1' and that means the returned one byte is - the clock number for card completing the command. - If bIsClockNum = 0, firmware don't care it. - pbRecvBuffer OUT Return data - pcbRecvLength INOUT IN:length of pbRecvBuffer OUT: length of return data - - Description: - This function userd to transmit data with SLE4442 card - */ - - LONG FtSle4442Cmd(SCARDHANDLE hCard,LPCBYTE pbCmd,DWORD bLengthToRead,BYTE bIsClockNum,LPBYTE pbRecvBuffer,LPDWORD pcbRecvLength); - LONG FtGetDevVer( SCARDCONTEXT hContext,char *firmwareRevision,char *hardwareRevision); - /* - Function: FtGetLibVersion - - Parameters: - buffer :buffer of libVersion - - - Description: - Get the Current Lib Version - - */ - void FtGetLibVersion (char *buffer); - - /* - Function: FtGetCurrentReaderType - - Parameters: - readerType : Get reader type - - - Description: - Get the Current reader type - - */ - LONG FtGetCurrentReaderType(unsigned int *readerType); - -#ifdef __cplusplus -} -#endif - -#endif - + /* + * Support for bR301(Bluetooth) smart card reader + * + * ft301u.h: header file for ft_ccid.c + * + * Copyright (C) Feitian 2014, Ben + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * @file + * @This isfeitian's private cmd. + */ + +#ifndef __ft301u_h__ +#define __ft301u_h__ + + +#include "wintypes.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + LONG SCARD_CTL_CODE(unsigned int code); + /* + Function: FtGetSerialNum + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + length IN length of buffer(>=8) + buffer OUT Serial number + + Description: + This function userd to get serial number of iR301. + */ + + LONG FtGetSerialNum(SCARDHANDLE hCard, unsigned int length, + char * buffer); + /* + Function: FtWriteFlash + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + bOffset IN Offset of flash to write + blength IN The length of data + buffer IN The data for write + + Description: + This function userd to write data to flash. + */ + LONG FtWriteFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, + unsigned char buffer[]); + /* + Function: FtReadFlash + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + bOffset IN Offset of flash to write + blength IN The length of read data + buffer OUT The read data + + Description: + This function used to read data from flash. + */ + LONG FtReadFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, + unsigned char buffer[]); + + /* + Function: FtSetTimeout + + Parameters: + hContext IN Connection context to the PC/SC Resource Manager + dwTimeout IN New transmission timeout value of between 301 and card (millisecond ) + + Description: + The function New transmission timeout value of between 301 and card. + + */ + LONG FtSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout); + + + //for dukpt + /* + Function: FtDukptInit + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + encBuf IN Ciphertext use TDES_ECB_PKCS7/AES_ECB_PKCS7 (See "Key C" ) + nLen IN encBuf length(40(TDES_ECB_PKCS7 ciphertext length) 48(AES_ECB_PKCS7 ciphertext length)) + + Description: + Init iR301 new ipek and ksn for dukpt. + + */ + LONG FtDukptInit(SCARDHANDLE hCard,unsigned char *encBuf,unsigned int nLen); + + + /* + Function: FtDukptSetEncMod + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + bEncrypt IN 1: SCardTransmit Encrypted + 0:SCardTransmit not Encrypted + + bEncFunc IN 0: Encryption functioon use TDES_ECB_PKCS7 + 1:Encryption functioon use AES_ECB_PKCS7 + + bEncType IN 1: SCardTransmit: Plaintext in Ciphertext out + 0: SCardTransmit: Ciphertext in Ciphertext out + + Description: + Set the encryption mode of iR301 for dukpt. + + */ + LONG FtDukptSetEncMod(SCARDHANDLE hCard, + unsigned int bEncrypt, + unsigned int bEncFunc, + unsigned int bEncType); + + + + + /* + Function: FtDukptGetKSN + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + pnlength INOUT IN: The size of ksn buffer(>=10) + OUT: The real size(if successful has been 10) + buffer OUT Buffer of ksn + + Description: + Get Ksn from iR301 for dukpt. + + */ + + LONG FtDukptGetKSN(SCARDHANDLE hCard,unsigned int * pnlength,unsigned char *buffer); + /* + Function: FtDidEnterBackground + + Parameters: + bDidEnter IN must be set 1 + + + Description: + Use this method to release monitor thread of reader status + + */ + void FtDidEnterBackground(unsigned int bDidEnter); + /* + Function: FtSle4442Cmd + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + pbCmd IN SLE4442 cmd(3 byte) which are listed in sle4442 cmd table + bLengthToRead IN The data length which reading from the card + bIsClockNum IN If bIsClockNum = 1, then bToReadLength should be '1' and that means the returned one byte is + the clock number for card completing the command. + If bIsClockNum = 0, firmware don't care it. + pbRecvBuffer OUT Return data + pcbRecvLength INOUT IN:length of pbRecvBuffer OUT: length of return data + + Description: + This function userd to transmit data with SLE4442 card + */ + + LONG FtSle4442Cmd(SCARDHANDLE hCard,LPCBYTE pbCmd,DWORD bLengthToRead,BYTE bIsClockNum,LPBYTE pbRecvBuffer,LPDWORD pcbRecvLength); + LONG FtGetDevVer( SCARDCONTEXT hContext,char *firmwareRevision,char *hardwareRevision); + /* + Function: FtGetLibVersion + + Parameters: + buffer :buffer of libVersion + + + Description: + Get the Current Lib Version + + */ + void FtGetLibVersion (char *buffer); + + /* + Function: FtGetCurrentReaderType + + Parameters: + readerType : Get reader type + + + Description: + Get the Current reader type + + */ + LONG FtGetCurrentReaderType(unsigned int *readerType); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/libs/SDK/include/winscard.h b/libs/SDK/include/winscard.h index 96438c5..43d6e7d 100755 --- a/libs/SDK/include/winscard.h +++ b/libs/SDK/include/winscard.h @@ -1,157 +1,159 @@ -/* - * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html ) - * - * Copyright (C) 1999-2003 - * David Corcoran - * Copyright (C) 2002-2009 - * Ludovic Rousseau - * - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - Changes to this license can be made only by the copyright author with - explicit written consent. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $Id: winscard.h 6851 2014-02-14 15:43:32Z rousseau $ - */ - -/** - * @file - * @brief This handles smart card reader communications. - */ - -#ifndef __winscard_h__ -#define __winscard_h__ - - -#include "wintypes.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#ifndef PCSC_API -#define PCSC_API -#endif - -#define MAX_ATR_SIZE 33 - - typedef LONG SCARDCONTEXT; /**< \p hContext returned by SCardEstablishContext() */ - typedef SCARDCONTEXT *PSCARDCONTEXT; - typedef SCARDCONTEXT *LPSCARDCONTEXT; - typedef LONG SCARDHANDLE; /**< \p hCard returned by SCardConnect() */ - typedef SCARDHANDLE *PSCARDHANDLE; - typedef SCARDHANDLE *LPSCARDHANDLE; - - - typedef struct { - const char *szReader; - const char *pvUserData; - DWORD dwCurrentState; - DWORD dwEventState; - DWORD cbAtr; - DWORD isConnected; - unsigned char rgbAtr[MAX_ATR_SIZE]; - } - SCARD_READERSTATE, *LPSCARD_READERSTATE; - /** Protocol Control Information (PCI) */ - typedef struct _SCARD_IO_REQUEST { - uint32_t dwProtocol; /**< Protocol identifier */ - uint32_t cbPciLength; /**< Protocol Control Inf Length */ - } - SCARD_IO_REQUEST, *PSCARD_IO_REQUEST, *LPSCARD_IO_REQUEST; - - typedef const SCARD_IO_REQUEST *LPCSCARD_IO_REQUEST; - - extern SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci, - g_rgSCardRawPci; - - PCSC_API LONG SCardEstablishContext(DWORD dwScope, - /*@null@*/ LPCVOID pvReserved1, /*@null@*/ LPCVOID pvReserved2, - /*@out@*/ LPSCARDCONTEXT phContext); - - PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext); - - PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext); - - PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, - LPCSTR szReader, - DWORD dwShareMode, - DWORD dwPreferredProtocols, - /*@out@*/ LPSCARDHANDLE phCard, /*@out@*/ LPDWORD pdwActiveProtocol); - - PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition); - - PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard); - - PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition); - - PCSC_API LONG SCardStatus(SCARDHANDLE hCard, - /*@null@*/ /*@out@*/ LPSTR mszReaderName, - /*@null@*/ /*@out@*/ LPDWORD pcchReaderLen, - /*@null@*/ /*@out@*/ LPDWORD pdwState, - /*@null@*/ /*@out@*/ LPDWORD pdwProtocol, - /*@null@*/ /*@out@*/ LPBYTE pbAtr, - /*@null@*/ /*@out@*/ LPDWORD pcbAtrLen); - - PCSC_API LONG SCardGetStatusChange(SCARDCONTEXT hContext, - DWORD dwTimeout, - LPSCARD_READERSTATE rgReaderStates, DWORD cReaders); - - PCSC_API LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, - LPCVOID pbSendBuffer, DWORD cbSendLength, - /*@out@*/ LPVOID pbRecvBuffer, DWORD cbRecvLength, - LPDWORD lpBytesReturned); - - PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, - const SCARD_IO_REQUEST *pioSendPci, - LPCBYTE pbSendBuffer, DWORD cbSendLength, - /*@out@*/ SCARD_IO_REQUEST *pioRecvPci, - /*@out@*/ LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength); - - PCSC_API LONG SCardListReaderGroups(SCARDCONTEXT hContext, - /*@out@*/ LPSTR mszGroups, LPDWORD pcchGroups); - - PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, - /*@null@*/ /*@out@*/ LPCSTR mszGroups, - /*@null@*/ /*@out@*/ LPSTR mszReaders, - /*@out@*/ LPDWORD pcchReaders); - - PCSC_API LONG SCardCancel(SCARDCONTEXT hContext); - PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, - DWORD dwPreferredProtocols, DWORD dwInitialization, - LPDWORD pdwActiveProtocol); - - PCSC_API LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, - /*@out@*/ LPBYTE pbAttr, LPDWORD pcbAttrLen); - PCSC_API LONG SCardSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout); - - - -#ifdef __cplusplus -} -#endif - -#endif - +/* + * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html ) + * + * Copyright (C) 1999-2003 + * David Corcoran + * Copyright (C) 2002-2009 + * Ludovic Rousseau + * + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + Changes to this license can be made only by the copyright author with + explicit written consent. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $Id: winscard.h 6851 2014-02-14 15:43:32Z rousseau $ + */ + +/** + * @file + * @brief This handles smart card reader communications. + */ + +#ifndef __winscard_h__ +#define __winscard_h__ + + +#include "wintypes.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#ifndef PCSC_API +#define PCSC_API +#endif + +#define MAX_ATR_SIZE 33 + + typedef LONG SCARDCONTEXT; /**< \p hContext returned by SCardEstablishContext() */ + typedef SCARDCONTEXT *PSCARDCONTEXT; + typedef SCARDCONTEXT *LPSCARDCONTEXT; + typedef LONG SCARDHANDLE; /**< \p hCard returned by SCardConnect() */ + typedef SCARDHANDLE *PSCARDHANDLE; + typedef SCARDHANDLE *LPSCARDHANDLE; + + + typedef struct + { + const char *szReader; + const char *pvUserData; + DWORD dwCurrentState; + DWORD dwEventState; + DWORD cbAtr; + DWORD isConnected; + unsigned char rgbAtr[MAX_ATR_SIZE]; + } + SCARD_READERSTATE, *LPSCARD_READERSTATE; + /** Protocol Control Information (PCI) */ + typedef struct _SCARD_IO_REQUEST + { + uint32_t dwProtocol; /**< Protocol identifier */ + uint32_t cbPciLength; /**< Protocol Control Inf Length */ + } + SCARD_IO_REQUEST, *PSCARD_IO_REQUEST, *LPSCARD_IO_REQUEST; + + typedef const SCARD_IO_REQUEST *LPCSCARD_IO_REQUEST; + + extern SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci, + g_rgSCardRawPci; + + PCSC_API LONG SCardEstablishContext(DWORD dwScope, + /*@null@*/ LPCVOID pvReserved1, /*@null@*/ LPCVOID pvReserved2, + /*@out@*/ LPSCARDCONTEXT phContext); + + PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext); + + PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext); + + PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, + LPCSTR szReader, + DWORD dwShareMode, + DWORD dwPreferredProtocols, + /*@out@*/ LPSCARDHANDLE phCard, /*@out@*/ LPDWORD pdwActiveProtocol); + + PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition); + + PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard); + + PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition); + + PCSC_API LONG SCardStatus(SCARDHANDLE hCard, + /*@null@*/ /*@out@*/ LPSTR mszReaderName, + /*@null@*/ /*@out@*/ LPDWORD pcchReaderLen, + /*@null@*/ /*@out@*/ LPDWORD pdwState, + /*@null@*/ /*@out@*/ LPDWORD pdwProtocol, + /*@null@*/ /*@out@*/ LPBYTE pbAtr, + /*@null@*/ /*@out@*/ LPDWORD pcbAtrLen); + + PCSC_API LONG SCardGetStatusChange(SCARDCONTEXT hContext, + DWORD dwTimeout, + LPSCARD_READERSTATE rgReaderStates, DWORD cReaders); + + PCSC_API LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, + LPCVOID pbSendBuffer, DWORD cbSendLength, + /*@out@*/ LPVOID pbRecvBuffer, DWORD cbRecvLength, + LPDWORD lpBytesReturned); + + PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, + const SCARD_IO_REQUEST *pioSendPci, + LPCBYTE pbSendBuffer, DWORD cbSendLength, + /*@out@*/ SCARD_IO_REQUEST *pioRecvPci, + /*@out@*/ LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength); + + PCSC_API LONG SCardListReaderGroups(SCARDCONTEXT hContext, + /*@out@*/ LPSTR mszGroups, LPDWORD pcchGroups); + + PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, + /*@null@*/ /*@out@*/ LPCSTR mszGroups, + /*@null@*/ /*@out@*/ LPSTR mszReaders, + /*@out@*/ LPDWORD pcchReaders); + + PCSC_API LONG SCardCancel(SCARDCONTEXT hContext); + PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, + DWORD dwPreferredProtocols, DWORD dwInitialization, + LPDWORD pdwActiveProtocol); + + PCSC_API LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, + /*@out@*/ LPBYTE pbAttr, LPDWORD pcbAttrLen); + PCSC_API LONG SCardSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout); + + + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/libs/SDK/include/wintypes.h b/libs/SDK/include/wintypes.h index 880056f..7599a89 100755 --- a/libs/SDK/include/wintypes.h +++ b/libs/SDK/include/wintypes.h @@ -1,265 +1,265 @@ -/* - * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html ) - * - * Copyright (C) 1999 - * David Corcoran - * Copyright (C) 2002-2011 - * Ludovic Rousseau - * - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - Changes to this license can be made only by the copyright author with - explicit written consent. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $Id: wintypes.h 6851 2014-02-14 15:43:32Z rousseau $ - */ - -/** - * @file - * @brief This keeps a list of Windows(R) types. - */ - -#ifndef __wintypes_h__ -#define __wintypes_h__ - -#ifdef __cplusplus -extern "C" -{ -#endif - -#ifdef __APPLE__ - -#include - -#ifndef BYTE - typedef uint8_t BYTE; -#endif - typedef uint8_t UCHAR; - typedef uint8_t *PUCHAR; - typedef uint16_t USHORT; - -#ifndef __COREFOUNDATION_CFPLUGINCOM__ - typedef uint32_t ULONG; - typedef void *LPVOID; -//haozi err? typedef int16_t BOOL; -#endif - - typedef uint32_t *PULONG; - typedef const void *LPCVOID; - typedef uint32_t DWORD; - typedef uint32_t *PDWORD; - typedef uint16_t WORD; - typedef int32_t LONG; - typedef const char *LPCSTR; - typedef const BYTE *LPCBYTE; - typedef BYTE *LPBYTE; - typedef DWORD *LPDWORD; - typedef char *LPSTR; - -#else - -#ifndef BYTE - typedef unsigned char BYTE; -#endif - typedef unsigned char UCHAR; - typedef unsigned char *PUCHAR; - typedef unsigned short USHORT; - -#ifndef __COREFOUNDATION_CFPLUGINCOM__ - typedef unsigned long ULONG; - typedef void *LPVOID; -#endif - - typedef const void *LPCVOID; - typedef unsigned long DWORD; - typedef unsigned long *PDWORD; - typedef long LONG; - typedef const char *LPCSTR; - typedef const BYTE *LPCBYTE; - typedef BYTE *LPBYTE; - typedef DWORD *LPDWORD; - typedef char *LPSTR; - - /* these types were deprecated but still used by old drivers and - * applications. So just declare and use them. */ - typedef LPSTR LPTSTR; - typedef LPCSTR LPCTSTR; - - /* types unused by pcsc-lite */ - typedef short BOOL; - typedef unsigned short WORD; - typedef unsigned long *PULONG; - -#endif - - - ///////////////////////////////////////// - - -#define SCARD_PCI_T0 (&g_rgSCardT0Pci) /**< protocol control information (PCI) for T=0 */ -#define SCARD_PCI_T1 (&g_rgSCardT1Pci) /**< protocol control information (PCI) for T=1 */ -#define SCARD_PCI_RAW (&g_rgSCardRawPci) /**< protocol control information (PCI) for RAW protocol */ - - /** error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx - */ -#define SCARD_S_SUCCESS ((LONG)0x00000000) /**< No error was encountered. */ -#define SCARD_F_INTERNAL_ERROR ((LONG)0x80100001) /**< An internal consistency check failed. */ -#define SCARD_E_CANCELLED ((LONG)0x80100002) /**< The action was cancelled by an SCardCancel request. */ -#define SCARD_E_INVALID_HANDLE ((LONG)0x80100003) /**< The supplied handle was invalid. */ -#define SCARD_E_INVALID_PARAMETER ((LONG)0x80100004) /**< One or more of the supplied parameters could not be properly interpreted. */ -#define SCARD_E_INVALID_TARGET ((LONG)0x80100005) /**< Registry startup information is missing or invalid. */ -#define SCARD_E_NO_MEMORY ((LONG)0x80100006) /**< Not enough memory available to complete this command. */ -#define SCARD_F_WAITED_TOO_LONG ((LONG)0x80100007) /**< An internal consistency timer has expired. */ -#define SCARD_E_INSUFFICIENT_BUFFER ((LONG)0x80100008) /**< The data buffer to receive returned data is too small for the returned data. */ -#define SCARD_E_UNKNOWN_READER ((LONG)0x80100009) /**< The specified reader name is not recognized. */ -#define SCARD_E_TIMEOUT ((LONG)0x8010000A) /**< The user-specified timeout value has expired. */ -#define SCARD_E_SHARING_VIOLATION ((LONG)0x8010000B) /**< The smart card cannot be accessed because of other connections outstanding. */ -#define SCARD_E_NO_SMARTCARD ((LONG)0x8010000C) /**< The operation requires a Smart Card, but no Smart Card is currently in the device. */ -#define SCARD_E_UNKNOWN_CARD ((LONG)0x8010000D) /**< The specified smart card name is not recognized. */ -#define SCARD_E_CANT_DISPOSE ((LONG)0x8010000E) /**< The system could not dispose of the media in the requested manner. */ -#define SCARD_E_PROTO_MISMATCH ((LONG)0x8010000F) /**< The requested protocols are incompatible with the protocol currently in use with the smart card. */ -#define SCARD_E_NOT_READY ((LONG)0x80100010) /**< The reader or smart card is not ready to accept commands. */ -#define SCARD_E_INVALID_VALUE ((LONG)0x80100011) /**< One or more of the supplied parameters values could not be properly interpreted. */ -#define SCARD_E_SYSTEM_CANCELLED ((LONG)0x80100012) /**< The action was cancelled by the system, presumably to log off or shut down. */ -#define SCARD_F_COMM_ERROR ((LONG)0x80100013) /**< An internal communications error has been detected. */ -#define SCARD_F_UNKNOWN_ERROR ((LONG)0x80100014) /**< An internal error has been detected, but the source is unknown. */ -#define SCARD_E_INVALID_ATR ((LONG)0x80100015) /**< An ATR obtained from the registry is not a valid ATR string. */ -#define SCARD_E_NOT_TRANSACTED ((LONG)0x80100016) /**< An attempt was made to end a non-existent transaction. */ -#define SCARD_E_READER_UNAVAILABLE ((LONG)0x80100017) /**< The specified reader is not currently available for use. */ -#define SCARD_P_SHUTDOWN ((LONG)0x80100018) /**< The operation has been aborted to allow the server application to exit. */ -#define SCARD_E_PCI_TOO_SMALL ((LONG)0x80100019) /**< The PCI Receive buffer was too small. */ -#define SCARD_E_READER_UNSUPPORTED ((LONG)0x8010001A) /**< The reader driver does not meet minimal requirements for support. */ -#define SCARD_E_DUPLICATE_READER ((LONG)0x8010001B) /**< The reader driver did not produce a unique reader name. */ -#define SCARD_E_CARD_UNSUPPORTED ((LONG)0x8010001C) /**< The smart card does not meet minimal requirements for support. */ -#define SCARD_E_NO_SERVICE ((LONG)0x8010001D) /**< The Smart card resource manager is not running. */ -#define SCARD_E_SERVICE_STOPPED ((LONG)0x8010001E) /**< The Smart card resource manager has shut down. */ -#define SCARD_E_UNEXPECTED ((LONG)0x8010001F) /**< An unexpected card error has occurred. */ -#define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x8010001F) /**< This smart card does not support the requested feature. */ -#define SCARD_E_ICC_INSTALLATION ((LONG)0x80100020) /**< No primary provider can be found for the smart card. */ -#define SCARD_E_ICC_CREATEORDER ((LONG)0x80100021) /**< The requested order of object creation is not supported. */ - /* #define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x80100022) / **< This smart card does not support the requested feature. */ -#define SCARD_E_DIR_NOT_FOUND ((LONG)0x80100023) /**< The identified directory does not exist in the smart card. */ -#define SCARD_E_FILE_NOT_FOUND ((LONG)0x80100024) /**< The identified file does not exist in the smart card. */ -#define SCARD_E_NO_DIR ((LONG)0x80100025) /**< The supplied path does not represent a smart card directory. */ -#define SCARD_E_NO_FILE ((LONG)0x80100026) /**< The supplied path does not represent a smart card file. */ -#define SCARD_E_NO_ACCESS ((LONG)0x80100027) /**< Access is denied to this file. */ -#define SCARD_E_WRITE_TOO_MANY ((LONG)0x80100028) /**< The smart card does not have enough memory to store the information. */ -#define SCARD_E_BAD_SEEK ((LONG)0x80100029) /**< There was an error trying to set the smart card file object pointer. */ -#define SCARD_E_INVALID_CHV ((LONG)0x8010002A) /**< The supplied PIN is incorrect. */ -#define SCARD_E_UNKNOWN_RES_MNG ((LONG)0x8010002B) /**< An unrecognized error code was returned from a layered component. */ -#define SCARD_E_NO_SUCH_CERTIFICATE ((LONG)0x8010002C) /**< The requested certificate does not exist. */ -#define SCARD_E_CERTIFICATE_UNAVAILABLE ((LONG)0x8010002D) /**< The requested certificate could not be obtained. */ -#define SCARD_E_NO_READERS_AVAILABLE ((LONG)0x8010002E) /**< Cannot find a smart card reader. */ -#define SCARD_E_COMM_DATA_LOST ((LONG)0x8010002F) /**< A communications error with the smart card has been detected. Retry the operation. */ -#define SCARD_E_NO_KEY_CONTAINER ((LONG)0x80100030) /**< The requested key container does not exist on the smart card. */ -#define SCARD_E_SERVER_TOO_BUSY ((LONG)0x80100031) /**< The Smart Card Resource Manager is too busy to complete this operation. */ - -#define SCARD_W_UNSUPPORTED_CARD ((LONG)0x80100065) /**< The reader cannot communicate with the card, due to ATR string configuration conflicts. */ -#define SCARD_W_UNRESPONSIVE_CARD ((LONG)0x80100066) /**< The smart card is not responding to a reset. */ -#define SCARD_W_UNPOWERED_CARD ((LONG)0x80100067) /**< Power has been removed from the smart card, so that further communication is not possible. */ -#define SCARD_W_RESET_CARD ((LONG)0x80100068) /**< The smart card has been reset, so any shared state information is invalid. */ -#define SCARD_W_REMOVED_CARD ((LONG)0x80100069) /**< The smart card has been removed, so further communication is not possible. */ - -#define SCARD_W_SECURITY_VIOLATION ((LONG)0x8010006A) /**< Access was denied because of a security violation. */ -#define SCARD_W_WRONG_CHV ((LONG)0x8010006B) /**< The card cannot be accessed because the wrong PIN was presented. */ -#define SCARD_W_CHV_BLOCKED ((LONG)0x8010006C) /**< The card cannot be accessed because the maximum number of PIN entry attempts has been reached. */ -#define SCARD_W_EOF ((LONG)0x8010006D) /**< The end of the smart card file has been reached. */ -#define SCARD_W_CANCELLED_BY_USER ((LONG)0x8010006E) /**< The user pressed "Cancel" on a Smart Card Selection Dialog. */ -#define SCARD_W_CARD_NOT_AUTHENTICATED ((LONG)0x8010006F) /**< No PIN was presented to the smart card. */ - -#define SCARD_AUTOALLOCATE (DWORD)(-1) /**< see SCardFreeMemory() */ -#define SCARD_SCOPE_USER 0x0000 /**< Scope in user space */ -#define SCARD_SCOPE_TERMINAL 0x0001 /**< Scope in terminal */ -#define SCARD_SCOPE_SYSTEM 0x0002 /**< Scope in system */ - - -#define SCARD_PROTOCOL_UNDEFINED 0x0000 /**< protocol not set */ -#define SCARD_PROTOCOL_UNSET SCARD_PROTOCOL_UNDEFINED /* backward compat */ -#define SCARD_PROTOCOL_T0 0x0001 /**< T=0 active protocol. */ -#define SCARD_PROTOCOL_T1 0x0002 /**< T=1 active protocol. */ -#define SCARD_PROTOCOL_RAW 0x0004 /**< Raw active protocol. */ -#define SCARD_PROTOCOL_T15 0x0008 /**< T=15 protocol. */ - -#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1) /**< IFD determines prot. */ - -#define SCARD_SHARE_EXCLUSIVE 0x0001 /**< Exclusive mode only */ -#define SCARD_SHARE_SHARED 0x0002 /**< Shared mode only */ -#define SCARD_SHARE_DIRECT 0x0003 /**< Raw mode only */ - -#define SCARD_LEAVE_CARD 0x0000 /**< Do nothing on close */ -#define SCARD_RESET_CARD 0x0001 /**< Reset on close */ -#define SCARD_UNPOWER_CARD 0x0002 /**< Power down on close */ -#define SCARD_EJECT_CARD 0x0003 /**< Eject on close */ - -#define SCARD_UNKNOWN 0x0001 /**< Unknown state */ -#define SCARD_ABSENT 0x0002 /**< Card is absent */ -#define SCARD_PRESENT 0x0004 /**< Card is present */ -#define SCARD_SWALLOWED 0x0008 /**< Card not powered */ -#define SCARD_POWERED 0x0010 /**< Card is powered */ -#define SCARD_NEGOTIABLE 0x0020 /**< Ready for PTS */ -#define SCARD_SPECIFIC 0x0040 /**< PTS has been set */ - -#define SCARD_STATE_UNAWARE 0x0000 /**< App wants status */ -#define SCARD_STATE_IGNORE 0x0001 /**< Ignore this reader */ -#define SCARD_STATE_CHANGED 0x0002 /**< State has changed */ -#define SCARD_STATE_UNKNOWN 0x0004 /**< Reader unknown */ -#define SCARD_STATE_UNAVAILABLE 0x0008 /**< Status unavailable */ -#define SCARD_STATE_EMPTY 0x0010 /**< Card removed */ -#define SCARD_STATE_PRESENT 0x0020 /**< Card inserted */ -#define SCARD_STATE_ATRMATCH 0x0040 /**< ATR matches card */ -#define SCARD_STATE_EXCLUSIVE 0x0080 /**< Exclusive Mode */ -#define SCARD_STATE_INUSE 0x0100 /**< Shared Mode */ -#define SCARD_STATE_MUTE 0x0200 /**< Unresponsive card */ -#define SCARD_STATE_UNPOWERED 0x0400 /**< Unpowered card */ - - -#define MAXIMUM_SMARTCARD_READERS 10 -#ifndef INFINITE -#define INFINITE 0xFFFFFFFF /**< Infinite timeout */ -#endif - -#define PCSCLITE_VERSION_NUMBER "1.7.4" /**< Current version */ - /** Maximum readers context (a slot is count as a reader) */ -#define PCSCLITE_MAX_READERS_CONTEXTS 16 -#define PCSCLITE_STATUS_WAIT 10000 -#define MAX_READERNAME 100 - -#ifndef SCARD_ATR_LENGTH -#define SCARD_ATR_LENGTH MAX_ATR_SIZE /**< Maximum ATR size */ -#endif - - /* - * The message and buffer sizes must be multiples of 16. - * The max message size must be at least large enough - * to accomodate the transmit_struct - */ -#define MAX_BUFFER_SIZE 264 /**< Maximum Tx/Rx Buffer for short APDU */ -#define MAX_BUFFER_SIZE_EXTENDED (4 + 3 + (1<<16) + 3) /**< enhanced (64K + APDU + Lc + Le) Tx/Rx Buffer */ - - -#ifdef __cplusplus -} -#endif - -#endif +/* + * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html ) + * + * Copyright (C) 1999 + * David Corcoran + * Copyright (C) 2002-2011 + * Ludovic Rousseau + * + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + Changes to this license can be made only by the copyright author with + explicit written consent. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $Id: wintypes.h 6851 2014-02-14 15:43:32Z rousseau $ + */ + +/** + * @file + * @brief This keeps a list of Windows(R) types. + */ + +#ifndef __wintypes_h__ +#define __wintypes_h__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#ifdef __APPLE__ + +#include + +#ifndef BYTE + typedef uint8_t BYTE; +#endif + typedef uint8_t UCHAR; + typedef uint8_t *PUCHAR; + typedef uint16_t USHORT; + +#ifndef __COREFOUNDATION_CFPLUGINCOM__ + typedef uint32_t ULONG; + typedef void *LPVOID; +//haozi err? typedef int16_t BOOL; +#endif + + typedef uint32_t *PULONG; + typedef const void *LPCVOID; + typedef uint32_t DWORD; + typedef uint32_t *PDWORD; + typedef uint16_t WORD; + typedef int32_t LONG; + typedef const char *LPCSTR; + typedef const BYTE *LPCBYTE; + typedef BYTE *LPBYTE; + typedef DWORD *LPDWORD; + typedef char *LPSTR; + +#else + +#ifndef BYTE + typedef unsigned char BYTE; +#endif + typedef unsigned char UCHAR; + typedef unsigned char *PUCHAR; + typedef unsigned short USHORT; + +#ifndef __COREFOUNDATION_CFPLUGINCOM__ + typedef unsigned long ULONG; + typedef void *LPVOID; +#endif + + typedef const void *LPCVOID; + typedef unsigned long DWORD; + typedef unsigned long *PDWORD; + typedef long LONG; + typedef const char *LPCSTR; + typedef const BYTE *LPCBYTE; + typedef BYTE *LPBYTE; + typedef DWORD *LPDWORD; + typedef char *LPSTR; + + /* these types were deprecated but still used by old drivers and + * applications. So just declare and use them. */ + typedef LPSTR LPTSTR; + typedef LPCSTR LPCTSTR; + + /* types unused by pcsc-lite */ + typedef short BOOL; + typedef unsigned short WORD; + typedef unsigned long *PULONG; + +#endif + + + ///////////////////////////////////////// + + +#define SCARD_PCI_T0 (&g_rgSCardT0Pci) /**< protocol control information (PCI) for T=0 */ +#define SCARD_PCI_T1 (&g_rgSCardT1Pci) /**< protocol control information (PCI) for T=1 */ +#define SCARD_PCI_RAW (&g_rgSCardRawPci) /**< protocol control information (PCI) for RAW protocol */ + + /** error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx + */ +#define SCARD_S_SUCCESS ((LONG)0x00000000) /**< No error was encountered. */ +#define SCARD_F_INTERNAL_ERROR ((LONG)0x80100001) /**< An internal consistency check failed. */ +#define SCARD_E_CANCELLED ((LONG)0x80100002) /**< The action was cancelled by an SCardCancel request. */ +#define SCARD_E_INVALID_HANDLE ((LONG)0x80100003) /**< The supplied handle was invalid. */ +#define SCARD_E_INVALID_PARAMETER ((LONG)0x80100004) /**< One or more of the supplied parameters could not be properly interpreted. */ +#define SCARD_E_INVALID_TARGET ((LONG)0x80100005) /**< Registry startup information is missing or invalid. */ +#define SCARD_E_NO_MEMORY ((LONG)0x80100006) /**< Not enough memory available to complete this command. */ +#define SCARD_F_WAITED_TOO_LONG ((LONG)0x80100007) /**< An internal consistency timer has expired. */ +#define SCARD_E_INSUFFICIENT_BUFFER ((LONG)0x80100008) /**< The data buffer to receive returned data is too small for the returned data. */ +#define SCARD_E_UNKNOWN_READER ((LONG)0x80100009) /**< The specified reader name is not recognized. */ +#define SCARD_E_TIMEOUT ((LONG)0x8010000A) /**< The user-specified timeout value has expired. */ +#define SCARD_E_SHARING_VIOLATION ((LONG)0x8010000B) /**< The smart card cannot be accessed because of other connections outstanding. */ +#define SCARD_E_NO_SMARTCARD ((LONG)0x8010000C) /**< The operation requires a Smart Card, but no Smart Card is currently in the device. */ +#define SCARD_E_UNKNOWN_CARD ((LONG)0x8010000D) /**< The specified smart card name is not recognized. */ +#define SCARD_E_CANT_DISPOSE ((LONG)0x8010000E) /**< The system could not dispose of the media in the requested manner. */ +#define SCARD_E_PROTO_MISMATCH ((LONG)0x8010000F) /**< The requested protocols are incompatible with the protocol currently in use with the smart card. */ +#define SCARD_E_NOT_READY ((LONG)0x80100010) /**< The reader or smart card is not ready to accept commands. */ +#define SCARD_E_INVALID_VALUE ((LONG)0x80100011) /**< One or more of the supplied parameters values could not be properly interpreted. */ +#define SCARD_E_SYSTEM_CANCELLED ((LONG)0x80100012) /**< The action was cancelled by the system, presumably to log off or shut down. */ +#define SCARD_F_COMM_ERROR ((LONG)0x80100013) /**< An internal communications error has been detected. */ +#define SCARD_F_UNKNOWN_ERROR ((LONG)0x80100014) /**< An internal error has been detected, but the source is unknown. */ +#define SCARD_E_INVALID_ATR ((LONG)0x80100015) /**< An ATR obtained from the registry is not a valid ATR string. */ +#define SCARD_E_NOT_TRANSACTED ((LONG)0x80100016) /**< An attempt was made to end a non-existent transaction. */ +#define SCARD_E_READER_UNAVAILABLE ((LONG)0x80100017) /**< The specified reader is not currently available for use. */ +#define SCARD_P_SHUTDOWN ((LONG)0x80100018) /**< The operation has been aborted to allow the server application to exit. */ +#define SCARD_E_PCI_TOO_SMALL ((LONG)0x80100019) /**< The PCI Receive buffer was too small. */ +#define SCARD_E_READER_UNSUPPORTED ((LONG)0x8010001A) /**< The reader driver does not meet minimal requirements for support. */ +#define SCARD_E_DUPLICATE_READER ((LONG)0x8010001B) /**< The reader driver did not produce a unique reader name. */ +#define SCARD_E_CARD_UNSUPPORTED ((LONG)0x8010001C) /**< The smart card does not meet minimal requirements for support. */ +#define SCARD_E_NO_SERVICE ((LONG)0x8010001D) /**< The Smart card resource manager is not running. */ +#define SCARD_E_SERVICE_STOPPED ((LONG)0x8010001E) /**< The Smart card resource manager has shut down. */ +#define SCARD_E_UNEXPECTED ((LONG)0x8010001F) /**< An unexpected card error has occurred. */ +#define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x8010001F) /**< This smart card does not support the requested feature. */ +#define SCARD_E_ICC_INSTALLATION ((LONG)0x80100020) /**< No primary provider can be found for the smart card. */ +#define SCARD_E_ICC_CREATEORDER ((LONG)0x80100021) /**< The requested order of object creation is not supported. */ +/* #define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x80100022) / **< This smart card does not support the requested feature. */ +#define SCARD_E_DIR_NOT_FOUND ((LONG)0x80100023) /**< The identified directory does not exist in the smart card. */ +#define SCARD_E_FILE_NOT_FOUND ((LONG)0x80100024) /**< The identified file does not exist in the smart card. */ +#define SCARD_E_NO_DIR ((LONG)0x80100025) /**< The supplied path does not represent a smart card directory. */ +#define SCARD_E_NO_FILE ((LONG)0x80100026) /**< The supplied path does not represent a smart card file. */ +#define SCARD_E_NO_ACCESS ((LONG)0x80100027) /**< Access is denied to this file. */ +#define SCARD_E_WRITE_TOO_MANY ((LONG)0x80100028) /**< The smart card does not have enough memory to store the information. */ +#define SCARD_E_BAD_SEEK ((LONG)0x80100029) /**< There was an error trying to set the smart card file object pointer. */ +#define SCARD_E_INVALID_CHV ((LONG)0x8010002A) /**< The supplied PIN is incorrect. */ +#define SCARD_E_UNKNOWN_RES_MNG ((LONG)0x8010002B) /**< An unrecognized error code was returned from a layered component. */ +#define SCARD_E_NO_SUCH_CERTIFICATE ((LONG)0x8010002C) /**< The requested certificate does not exist. */ +#define SCARD_E_CERTIFICATE_UNAVAILABLE ((LONG)0x8010002D) /**< The requested certificate could not be obtained. */ +#define SCARD_E_NO_READERS_AVAILABLE ((LONG)0x8010002E) /**< Cannot find a smart card reader. */ +#define SCARD_E_COMM_DATA_LOST ((LONG)0x8010002F) /**< A communications error with the smart card has been detected. Retry the operation. */ +#define SCARD_E_NO_KEY_CONTAINER ((LONG)0x80100030) /**< The requested key container does not exist on the smart card. */ +#define SCARD_E_SERVER_TOO_BUSY ((LONG)0x80100031) /**< The Smart Card Resource Manager is too busy to complete this operation. */ + +#define SCARD_W_UNSUPPORTED_CARD ((LONG)0x80100065) /**< The reader cannot communicate with the card, due to ATR string configuration conflicts. */ +#define SCARD_W_UNRESPONSIVE_CARD ((LONG)0x80100066) /**< The smart card is not responding to a reset. */ +#define SCARD_W_UNPOWERED_CARD ((LONG)0x80100067) /**< Power has been removed from the smart card, so that further communication is not possible. */ +#define SCARD_W_RESET_CARD ((LONG)0x80100068) /**< The smart card has been reset, so any shared state information is invalid. */ +#define SCARD_W_REMOVED_CARD ((LONG)0x80100069) /**< The smart card has been removed, so further communication is not possible. */ + +#define SCARD_W_SECURITY_VIOLATION ((LONG)0x8010006A) /**< Access was denied because of a security violation. */ +#define SCARD_W_WRONG_CHV ((LONG)0x8010006B) /**< The card cannot be accessed because the wrong PIN was presented. */ +#define SCARD_W_CHV_BLOCKED ((LONG)0x8010006C) /**< The card cannot be accessed because the maximum number of PIN entry attempts has been reached. */ +#define SCARD_W_EOF ((LONG)0x8010006D) /**< The end of the smart card file has been reached. */ +#define SCARD_W_CANCELLED_BY_USER ((LONG)0x8010006E) /**< The user pressed "Cancel" on a Smart Card Selection Dialog. */ +#define SCARD_W_CARD_NOT_AUTHENTICATED ((LONG)0x8010006F) /**< No PIN was presented to the smart card. */ + +#define SCARD_AUTOALLOCATE (DWORD)(-1) /**< see SCardFreeMemory() */ +#define SCARD_SCOPE_USER 0x0000 /**< Scope in user space */ +#define SCARD_SCOPE_TERMINAL 0x0001 /**< Scope in terminal */ +#define SCARD_SCOPE_SYSTEM 0x0002 /**< Scope in system */ + + +#define SCARD_PROTOCOL_UNDEFINED 0x0000 /**< protocol not set */ +#define SCARD_PROTOCOL_UNSET SCARD_PROTOCOL_UNDEFINED /* backward compat */ +#define SCARD_PROTOCOL_T0 0x0001 /**< T=0 active protocol. */ +#define SCARD_PROTOCOL_T1 0x0002 /**< T=1 active protocol. */ +#define SCARD_PROTOCOL_RAW 0x0004 /**< Raw active protocol. */ +#define SCARD_PROTOCOL_T15 0x0008 /**< T=15 protocol. */ + +#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1) /**< IFD determines prot. */ + +#define SCARD_SHARE_EXCLUSIVE 0x0001 /**< Exclusive mode only */ +#define SCARD_SHARE_SHARED 0x0002 /**< Shared mode only */ +#define SCARD_SHARE_DIRECT 0x0003 /**< Raw mode only */ + +#define SCARD_LEAVE_CARD 0x0000 /**< Do nothing on close */ +#define SCARD_RESET_CARD 0x0001 /**< Reset on close */ +#define SCARD_UNPOWER_CARD 0x0002 /**< Power down on close */ +#define SCARD_EJECT_CARD 0x0003 /**< Eject on close */ + +#define SCARD_UNKNOWN 0x0001 /**< Unknown state */ +#define SCARD_ABSENT 0x0002 /**< Card is absent */ +#define SCARD_PRESENT 0x0004 /**< Card is present */ +#define SCARD_SWALLOWED 0x0008 /**< Card not powered */ +#define SCARD_POWERED 0x0010 /**< Card is powered */ +#define SCARD_NEGOTIABLE 0x0020 /**< Ready for PTS */ +#define SCARD_SPECIFIC 0x0040 /**< PTS has been set */ + +#define SCARD_STATE_UNAWARE 0x0000 /**< App wants status */ +#define SCARD_STATE_IGNORE 0x0001 /**< Ignore this reader */ +#define SCARD_STATE_CHANGED 0x0002 /**< State has changed */ +#define SCARD_STATE_UNKNOWN 0x0004 /**< Reader unknown */ +#define SCARD_STATE_UNAVAILABLE 0x0008 /**< Status unavailable */ +#define SCARD_STATE_EMPTY 0x0010 /**< Card removed */ +#define SCARD_STATE_PRESENT 0x0020 /**< Card inserted */ +#define SCARD_STATE_ATRMATCH 0x0040 /**< ATR matches card */ +#define SCARD_STATE_EXCLUSIVE 0x0080 /**< Exclusive Mode */ +#define SCARD_STATE_INUSE 0x0100 /**< Shared Mode */ +#define SCARD_STATE_MUTE 0x0200 /**< Unresponsive card */ +#define SCARD_STATE_UNPOWERED 0x0400 /**< Unpowered card */ + + +#define MAXIMUM_SMARTCARD_READERS 10 +#ifndef INFINITE +#define INFINITE 0xFFFFFFFF /**< Infinite timeout */ +#endif + +#define PCSCLITE_VERSION_NUMBER "1.7.4" /**< Current version */ + /** Maximum readers context (a slot is count as a reader) */ +#define PCSCLITE_MAX_READERS_CONTEXTS 16 +#define PCSCLITE_STATUS_WAIT 10000 +#define MAX_READERNAME 100 + +#ifndef SCARD_ATR_LENGTH +#define SCARD_ATR_LENGTH MAX_ATR_SIZE /**< Maximum ATR size */ +#endif + + /* + * The message and buffer sizes must be multiples of 16. + * The max message size must be at least large enough + * to accomodate the transmit_struct + */ +#define MAX_BUFFER_SIZE 264 /**< Maximum Tx/Rx Buffer for short APDU */ +#define MAX_BUFFER_SIZE_EXTENDED (4 + 3 + (1<<16) + 3) /**< enhanced (64K + APDU + Lc + Le) Tx/Rx Buffer */ + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libs/source/iRockey301_ccid.xcodeproj/project.pbxproj b/libs/source/iRockey301_ccid.xcodeproj/project.pbxproj index 1fcf958..d571149 100755 --- a/libs/source/iRockey301_ccid.xcodeproj/project.pbxproj +++ b/libs/source/iRockey301_ccid.xcodeproj/project.pbxproj @@ -32,7 +32,7 @@ B5132B1E1BB9394600665BCD /* winscard.h in Headers */ = {isa = PBXBuildFile; fileRef = B5132B1B1BB9394600665BCD /* winscard.h */; }; B5132B1F1BB9394600665BCD /* wintypes.h in Headers */ = {isa = PBXBuildFile; fileRef = B5132B1C1BB9394600665BCD /* wintypes.h */; }; B52B69A31D7831BC00D83CDC /* bR301SessionController.h in Headers */ = {isa = PBXBuildFile; fileRef = B52B69A21D7831BC00D83CDC /* bR301SessionController.h */; }; - B5B875101C29615800F9B057 /* libFTSessionController.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B5B8750F1C29615800F9B057 /* libFTSessionController.a */; }; + B557B4CD1E5D76BE000C9CEC /* libFTSessionController_release.a in Frameworks */ = {isa = PBXBuildFile; fileRef = B557B4CC1E5D76BE000C9CEC /* libFTSessionController_release.a */; }; B5F2344B1BB92BBA00C0B7CD /* ReaderInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = B5F2344A1BB92BBA00C0B7CD /* ReaderInterface.h */; }; D32B614B1935BAA800FC89B3 /* ft301u.h in Headers */ = {isa = PBXBuildFile; fileRef = D32B61441935BAA800FC89B3 /* ft301u.h */; }; D32B614C1935BAA800FC89B3 /* ReaderInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = D32B61451935BAA800FC89B3 /* ReaderInterface.h */; }; @@ -70,7 +70,7 @@ B5132B1B1BB9394600665BCD /* winscard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = winscard.h; path = src/winscard.h; sourceTree = ""; }; B5132B1C1BB9394600665BCD /* wintypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = wintypes.h; path = src/wintypes.h; sourceTree = ""; }; B52B69A21D7831BC00D83CDC /* bR301SessionController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = bR301SessionController.h; path = src/bR301SessionController.h; sourceTree = ""; }; - B5B8750F1C29615800F9B057 /* libFTSessionController.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libFTSessionController.a; path = src/libFTSessionController.a; sourceTree = ""; }; + B557B4CC1E5D76BE000C9CEC /* libFTSessionController_release.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libFTSessionController_release.a; path = src/libFTSessionController_release.a; sourceTree = ""; }; B5F2344A1BB92BBA00C0B7CD /* ReaderInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ReaderInterface.h; path = src/ReaderInterface.h; sourceTree = ""; }; D32B61441935BAA800FC89B3 /* ft301u.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ft301u.h; sourceTree = ""; }; D32B61451935BAA800FC89B3 /* ReaderInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ReaderInterface.h; sourceTree = ""; }; @@ -89,8 +89,8 @@ D33D1ED018EA8E2A0041D55E /* ExternalAccessory.framework in Frameworks */, D33D1ECE18EA8DE80041D55E /* UIKit.framework in Frameworks */, D37003DF18BC6521005CD978 /* CoreFoundation.framework in Frameworks */, + B557B4CD1E5D76BE000C9CEC /* libFTSessionController_release.a in Frameworks */, 52263B2D1573846500CE4DCC /* Foundation.framework in Frameworks */, - B5B875101C29615800F9B057 /* libFTSessionController.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -100,31 +100,19 @@ 5207A28415E333D000C156DF /* src */ = { isa = PBXGroup; children = ( - B5132B1A1BB9394600665BCD /* ft301u.h */, - B5132B1B1BB9394600665BCD /* winscard.h */, - B5132B1C1BB9394600665BCD /* wintypes.h */, - B5F2344A1BB92BBA00C0B7CD /* ReaderInterface.h */, - 33ED7113172CCF9200268F11 /* crypto.h */, - 33ED710F172CCF6B00268F11 /* aes.h */, - 335C019F16773A3700ABE095 /* DES_C.h */, - 5207A28615E333E300C156DF /* buffer.h */, - 5207A28815E333E300C156DF /* checksum.h */, - B52B69A21D7831BC00D83CDC /* bR301SessionController.h */, - 5207A28E15E333E300C156DF /* ft_ccid.h */, - 5207A29015E333E300C156DF /* proto-t1.h */, - 33ED7112172CCF9100268F11 /* crypto.c */, + B557B4B21E5D6E30000C9CEC /* Header files */, 33ED710E172CCF6B00268F11 /* aes.c */, 3374951B16F9D2E800445C6F /* atr.c */, - 335C019E16773A3700ABE095 /* DES_C.c */, 5207A28715E333E300C156DF /* buffer.c */, 5207A28915E333E300C156DF /* checksum.c */, + 33ED7112172CCF9100268F11 /* crypto.c */, 5207A28A15E333E300C156DF /* debug.c */, + 335C019E16773A3700ABE095 /* DES_C.c */, 5207A28F15E333E300C156DF /* pps.c */, 5207A29115E333E300C156DF /* proto-t1.c */, - 5207A29215E333E300C156DF /* winscard.m */, - 33CC95DA172BC8460007B3BD /* ReaderInterface.m */, 5207A28D15E333E300C156DF /* ft_ccid_cmd.m */, - B5B8750F1C29615800F9B057 /* libFTSessionController.a */, + 33CC95DA172BC8460007B3BD /* ReaderInterface.m */, + 5207A29215E333E300C156DF /* winscard.m */, ); name = src; sourceTree = ""; @@ -151,6 +139,7 @@ 52263B2B1573846500CE4DCC /* Frameworks */ = { isa = PBXGroup; children = ( + B557B4CC1E5D76BE000C9CEC /* libFTSessionController_release.a */, D33D1ECF18EA8E2A0041D55E /* ExternalAccessory.framework */, D33D1ECD18EA8DE80041D55E /* UIKit.framework */, D37003DE18BC6520005CD978 /* CoreFoundation.framework */, @@ -168,6 +157,25 @@ path = iRockey301_ccid; sourceTree = ""; }; + B557B4B21E5D6E30000C9CEC /* Header files */ = { + isa = PBXGroup; + children = ( + 33ED710F172CCF6B00268F11 /* aes.h */, + B52B69A21D7831BC00D83CDC /* bR301SessionController.h */, + 5207A28615E333E300C156DF /* buffer.h */, + 5207A28815E333E300C156DF /* checksum.h */, + 33ED7113172CCF9200268F11 /* crypto.h */, + 335C019F16773A3700ABE095 /* DES_C.h */, + 5207A28E15E333E300C156DF /* ft_ccid.h */, + B5132B1A1BB9394600665BCD /* ft301u.h */, + 5207A29015E333E300C156DF /* proto-t1.h */, + B5F2344A1BB92BBA00C0B7CD /* ReaderInterface.h */, + B5132B1B1BB9394600665BCD /* winscard.h */, + B5132B1C1BB9394600665BCD /* wintypes.h */, + ); + name = "Header files"; + sourceTree = ""; + }; D32B61421935BAA800FC89B3 /* SDK */ = { isa = PBXGroup; children = ( @@ -249,7 +257,7 @@ 52263B201573846500CE4DCC /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0510; + LastUpgradeCheck = 0820; ORGANIZATIONNAME = ftsafe; }; buildConfigurationList = 52263B231573846500CE4DCC /* Build configuration list for PBXProject "iRockey301_ccid" */; @@ -296,10 +304,22 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CONFIGURATION_BUILD_DIR = "$(SRCROOT)/../SDK/lib/"; COPY_PHASE_STRIP = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", @@ -307,10 +327,13 @@ ); GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_VERSION = ""; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 5.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; VALID_ARCHS = "arm64 armv7 armv7s"; @@ -321,14 +344,28 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CONFIGURATION_BUILD_DIR = "$(SRCROOT)/../SDK/lib/"; COPY_PHASE_STRIP = YES; + ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; GCC_VERSION = ""; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_UNDECLARED_SELECTOR = YES; GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 5.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; VALIDATE_PRODUCT = YES; @@ -339,18 +376,13 @@ 52263B371573846500CE4DCC /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ARCHS = ( - arm64, - armv7s, - armv7, - ); CLANG_ENABLE_OBJC_ARC = YES; CONFIGURATION_BUILD_DIR = "$(SRCROOT)/../SDK/lib/"; - CONFIGURATION_TEMP_DIR = "$(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)"; + CONFIGURATION_TEMP_DIR = "$(SRCROOT)/../SDK/lib/"; DSTROOT = /tmp/iRockey301_ccid.dst; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "iRockey301_ccid/iRockey301_ccid-Prefix.pch"; - IPHONEOS_DEPLOYMENT_TARGET = 5.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/src", @@ -360,6 +392,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SHARED_PRECOMPS_DIR = "$(SRCROOT)/../SDK/"; SKIP_INSTALL = YES; + SYMROOT = "$(SRCROOT)/../SDK/lib/"; TARGETED_DEVICE_FAMILY = "1,2"; VALID_ARCHS = "arm64 armv7 armv7s"; }; @@ -368,18 +401,13 @@ 52263B381573846500CE4DCC /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ARCHS = ( - arm64, - armv7s, - armv7, - ); CLANG_ENABLE_OBJC_ARC = YES; CONFIGURATION_BUILD_DIR = "$(SRCROOT)/../SDK/lib/"; - CONFIGURATION_TEMP_DIR = "$(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)"; + CONFIGURATION_TEMP_DIR = "$(SRCROOT)/../SDK/lib/"; DSTROOT = /tmp/iRockey301_ccid.dst; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "iRockey301_ccid/iRockey301_ccid-Prefix.pch"; - IPHONEOS_DEPLOYMENT_TARGET = 5.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/src", @@ -389,6 +417,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SHARED_PRECOMPS_DIR = "$(SRCROOT)/../SDK/"; SKIP_INSTALL = YES; + SYMROOT = "$(SRCROOT)/../SDK/lib/"; TARGETED_DEVICE_FAMILY = "1,2"; VALID_ARCHS = "arm64 armv7 armv7s"; }; diff --git a/libs/source/iRockey301_ccid.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/libs/source/iRockey301_ccid.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 2476eb8..1fee66d 100755 --- a/libs/source/iRockey301_ccid.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/libs/source/iRockey301_ccid.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -1,7 +1,7 @@ - - - - - + + + + + diff --git a/libs/source/iRockey301_ccid.xcodeproj/project.xcworkspace/xcuserdata/xwhbin.xcuserdatad/WorkspaceSettings.xcsettings b/libs/source/iRockey301_ccid.xcodeproj/project.xcworkspace/xcuserdata/xwhbin.xcuserdatad/WorkspaceSettings.xcsettings index 659c876..a31511d 100755 --- a/libs/source/iRockey301_ccid.xcodeproj/project.xcworkspace/xcuserdata/xwhbin.xcuserdatad/WorkspaceSettings.xcsettings +++ b/libs/source/iRockey301_ccid.xcodeproj/project.xcworkspace/xcuserdata/xwhbin.xcuserdatad/WorkspaceSettings.xcsettings @@ -1,10 +1,10 @@ - - - - - HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges - - SnapshotAutomaticallyBeforeSignificantChanges - - - + + + + + HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges + + SnapshotAutomaticallyBeforeSignificantChanges + + + diff --git a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/Ben.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/Ben.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme index c1fe974..d88c536 100755 --- a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/Ben.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme +++ b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/Ben.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme @@ -1,6 +1,6 @@ iRockey301_ccid.xcscheme orderHint - 3 + 1 SuppressBuildableAutocreation diff --git a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/liyalin.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/liyalin.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme index f2e1e39..fa5b76b 100755 --- a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/liyalin.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme +++ b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/liyalin.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme @@ -1,77 +1,77 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/liyalin.xcuserdatad/xcschemes/xcschememanagement.plist b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/liyalin.xcuserdatad/xcschemes/xcschememanagement.plist index b55ad0b..6accc3b 100755 --- a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/liyalin.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/liyalin.xcuserdatad/xcschemes/xcschememanagement.plist @@ -1,22 +1,22 @@ - - - - - SchemeUserState - - iRockey301_ccid.xcscheme - - orderHint - 3 - - - SuppressBuildableAutocreation - - 52263B281573846500CE4DCC - - primary - - - - - + + + + + SchemeUserState + + iRockey301_ccid.xcscheme + + orderHint + 3 + + + SuppressBuildableAutocreation + + 52263B281573846500CE4DCC + + primary + + + + + diff --git a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/xwhbin.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/xwhbin.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme index 407c0ac..ab59f0a 100755 --- a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/xwhbin.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme +++ b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/xwhbin.xcuserdatad/xcschemes/iRockey301_ccid.xcscheme @@ -1,59 +1,59 @@ - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/xwhbin.xcuserdatad/xcschemes/xcschememanagement.plist b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/xwhbin.xcuserdatad/xcschemes/xcschememanagement.plist index a945854..de620c9 100755 --- a/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/xwhbin.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/libs/source/iRockey301_ccid.xcodeproj/xcuserdata/xwhbin.xcuserdatad/xcschemes/xcschememanagement.plist @@ -1,22 +1,22 @@ - - - - - SchemeUserState - - iRockey301_ccid.xcscheme - - orderHint - 0 - - - SuppressBuildableAutocreation - - 52263B281573846500CE4DCC - - primary - - - - - + + + + + SchemeUserState + + iRockey301_ccid.xcscheme + + orderHint + 0 + + + SuppressBuildableAutocreation + + 52263B281573846500CE4DCC + + primary + + + + + diff --git a/libs/source/iRockey301_ccid/iRockey301_ccid-Prefix.pch b/libs/source/iRockey301_ccid/iRockey301_ccid-Prefix.pch old mode 100644 new mode 100755 index 745e775..ca91a16 --- a/libs/source/iRockey301_ccid/iRockey301_ccid-Prefix.pch +++ b/libs/source/iRockey301_ccid/iRockey301_ccid-Prefix.pch @@ -1,7 +1,7 @@ -// -// Prefix header for all source files of the 'iRockey301_ccid' target in the 'iRockey301_ccid' project -// - -#ifdef __OBJC__ - #import -#endif +// +// Prefix header for all source files of the 'iRockey301_ccid' target in the 'iRockey301_ccid' project +// + +#ifdef __OBJC__ + #import +#endif diff --git a/libs/source/src/DES_C.c b/libs/source/src/DES_C.c index e37a816..38518ab 100755 --- a/libs/source/src/DES_C.c +++ b/libs/source/src/DES_C.c @@ -27,29 +27,29 @@ #include #include static void SetKey( - unsigned char *key + unsigned char *key ); static void Permute( - unsigned char *inblock, - char perm[16][16][8], - unsigned char *outblock + unsigned char *inblock, + char perm[16][16][8], + unsigned char *outblock ); static void Round( - int num, - unsigned int *block + int num, + unsigned int *block ); static int F_DES( - unsigned int r, - unsigned char subKey[8] + unsigned int r, + unsigned char subKey[8] ); static void PermInit( - char perm[16][16][8], - const char p[64] + char perm[16][16][8], + const char p[64] ); static void SPInit(void); static unsigned int ByteSwap( - unsigned int x + unsigned int x ); #define SINGLE 8 @@ -61,26 +61,26 @@ static unsigned int ByteSwap( ************************************************************************/ static const char ip[] = /* Initial permutation IP */ { - 58, 50, 42, 34, 26, 18, 10, 2, - 60, 52, 44, 36, 28, 20, 12, 4, - 62, 54, 46, 38, 30, 22, 14, 6, - 64, 56, 48, 40, 32, 24, 16, 8, - 57, 49, 41, 33, 25, 17, 9, 1, - 59, 51, 43, 35, 27, 19, 11, 3, - 61, 53, 45, 37, 29, 21, 13, 5, - 63, 55, 47, 39, 31, 23, 15, 7 + 58, 50, 42, 34, 26, 18, 10, 2, + 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, + 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, + 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, + 63, 55, 47, 39, 31, 23, 15, 7 }; /* ip[] */ static const char fp[] = /* Final permutation IP^-1 */ { - 40, 8, 48, 16, 56, 24, 64, 32, - 39, 7, 47, 15, 55, 23, 63, 31, - 38, 6, 46, 14, 54, 22, 62, 30, - 37, 5, 45, 13, 53, 21, 61, 29, - 36, 4, 44, 12, 52, 20, 60, 28, - 35, 3, 43, 11, 51, 19, 59, 27, - 34, 2, 42, 10, 50, 18, 58, 26, - 33, 1, 41, 9, 49, 17, 57, 25 + 40, 8, 48, 16, 56, 24, 64, 32, + 39, 7, 47, 15, 55, 23, 63, 31, + 38, 6, 46, 14, 54, 22, 62, 30, + 37, 5, 45, 13, 53, 21, 61, 29, + 36, 4, 44, 12, 52, 20, 60, 28, + 35, 3, 43, 11, 51, 19, 59, 27, + 34, 2, 42, 10, 50, 18, 58, 26, + 33, 1, 41, 9, 49, 17, 57, 25 }; /* fp[] */ /* @@ -89,109 +89,111 @@ This is for reference only; it is unused in the code as the F() function performs it implicitly for speed */ #ifdef notdef -static const char ei[] = { - 32, 1, 2, 3, 4, 5, - 4, 5, 6, 7, 8, 9, - 8, 9, 10, 11, 12, 13, - 12, 13, 14, 15, 16, 17, - 16, 17, 18, 19, 20, 21, - 20, 21, 22, 23, 24, 25, - 24, 25, 26, 27, 28, 29, - 28, 29, 30, 31, 32, 1 +static const char ei[] = +{ + 32, 1, 2, 3, 4, 5, + 4, 5, 6, 7, 8, 9, + 8, 9, 10, 11, 12, 13, + 12, 13, 14, 15, 16, 17, + 16, 17, 18, 19, 20, 21, + 20, 21, 22, 23, 24, 25, + 24, 25, 26, 27, 28, 29, + 28, 29, 30, 31, 32, 1 }; #endif /* notdef */ static const char pc1[] = /* Permuted choice table (key) */ { - 57, 49, 41, 33, 25, 17, 9, - 1, 58, 50, 42, 34, 26, 18, - 10, 2, 59, 51, 43, 35, 27, - 19, 11, 3, 60, 52, 44, 36, - - 63, 55, 47, 39, 31, 23, 15, - 7, 62, 54, 46, 38, 30, 22, - 14, 6, 61, 53, 45, 37, 29, - 21, 13, 5, 28, 20, 12, 4 + 57, 49, 41, 33, 25, 17, 9, + 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, + 19, 11, 3, 60, 52, 44, 36, + + 63, 55, 47, 39, 31, 23, 15, + 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, + 21, 13, 5, 28, 20, 12, 4 }; static const char totRot[] = /* Number left rotations of pc1 */ { - 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 + 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 }; /* totRot[] */ static const char pc2[] = /* Permuted choice key (table) */ { - 14, 17, 11, 24, 1, 5, - 3, 28, 15, 6, 21, 10, - 23, 19, 12, 4, 26, 8, - 16, 7, 27, 20, 13, 2, - 41, 52, 31, 37, 47, 55, - 30, 40, 51, 45, 33, 48, - 44, 49, 39, 56, 34, 53, - 46, 42, 50, 36, 29, 32 + 14, 17, 11, 24, 1, 5, + 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, + 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, + 30, 40, 51, 45, 33, 48, + 44, 49, 39, 56, 34, 53, + 46, 42, 50, 36, 29, 32 }; /* pc2[] */ static const char si[8][64] = /* The (in)famous S-boxes */ { - /* S1 */ - 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13, - - /* S2 */ - 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9, - - /* S3 */ - 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12, - - /* S4 */ - 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14, - - /* S5 */ - 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3, - - /* S6 */ - 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13, - - /* S7 */ - 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12, - - /* S8 */ - 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 + /* S1 */ + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13, + + /* S2 */ + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9, + + /* S3 */ + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12, + + /* S4 */ + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14, + + /* S5 */ + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3, + + /* S6 */ + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13, + + /* S7 */ + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12, + + /* S8 */ + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 }; /* si[] */ /* 32-bit permutation function P used on the output of the S-boxes */ -static const char p32i[] = { - 16, 7, 20, 21, - 29, 12, 28, 17, - 1, 15, 23, 26, - 5, 18, 31, 10, - 2, 8, 24, 14, - 32, 27, 3, 9, - 19, 13, 30, 6, - 22, 11, 4, 25 +static const char p32i[] = +{ + 16, 7, 20, 21, + 29, 12, 28, 17, + 1, 15, 23, 26, + 5, 18, 31, 10, + 2, 8, 24, 14, + 32, 27, 3, 9, + 19, 13, 30, 6, + 22, 11, 4, 25 }; /* p32i[] */ /************************************************************************ @@ -206,12 +208,14 @@ static int desInited = 0; static unsigned char kn[16][8]; /* bit 0 is left-most in byte */ -static const int byteBit[] = { - 0200, 0100, 040, 020, 010, 04, 02, 01 +static const int byteBit[] = +{ + 0200, 0100, 040, 020, 010, 04, 02, 01 }; /* byteBit[] */ -static const int nibbleBit[] = { - 010, 004, 002, 001 +static const int nibbleBit[] = +{ + 010, 004, 002, 001 }; /* nibbleBit[] */ @@ -219,68 +223,73 @@ static const int nibbleBit[] = { //3des encryption and decryption void DES_C_DDES(unsigned char *key, unsigned char *data, BOOL encrypt) { - if (encrypt) { - DES_C_DES(key, data, true ); - DES_C_DES(key + SINGLE, data, false ); - DES_C_DES(key, data, true ); - } /* end of if */ - else { - DES_C_DES(key, data, false ); - DES_C_DES(key + SINGLE, data, true ); - DES_C_DES(key, data, false ); - } /* end of else */ - - return; + if (encrypt) + { + DES_C_DES(key, data, true ); + DES_C_DES(key + SINGLE, data, false ); + DES_C_DES(key, data, true ); + } /* end of if */ + else + { + DES_C_DES(key, data, false ); + DES_C_DES(key + SINGLE, data, true ); + DES_C_DES(key, data, false ); + } /* end of else */ + + return; } // DDES() //1des encryption and decryption void DES_C_DES(unsigned char *key, unsigned char *data, BOOL doEncrypt) { - int i; - unsigned int work[2]; /* Working data storage */ - int tmp; - - if (!desInited) { - SPInit(); - PermInit(iperm, ip); - PermInit(fperm, fp); - desInited = 1; - } /* end of if */ - - SetKey(key); - - /* Initial Permutation */ - Permute(data, iperm, (unsigned char *)work); - - work[0] = ByteSwap(work[0]); - work[1] = ByteSwap(work[1]); - - if (doEncrypt) { - /* Do the 16 rounds */ - for (i = 0; i < 16; i++) - Round(i, work); - - /* Left/right half swap */ - tmp = work[0]; - work[0] = work[1]; - work[1] = tmp; - } /* end of if */ - else { - /* Left/right half swap */ - tmp = work[0]; - work[0] = work[1]; - work[1] = tmp; - - /* Do the 16 rounds in reverse order */ - for (i = 15; i >= 0; i--) - Round(i, work); - } /* end of else */ - - work[0] = ByteSwap(work[0]); - work[1] = ByteSwap(work[1]); - - /* Inverse initial permutation */ - Permute((unsigned char *)work, fperm, data); + int i; + unsigned int work[2]; /* Working data storage */ + int tmp; + + if (!desInited) + { + SPInit(); + PermInit(iperm, ip); + PermInit(fperm, fp); + desInited = 1; + } /* end of if */ + + SetKey(key); + + /* Initial Permutation */ + Permute(data, iperm, (unsigned char *)work); + + work[0] = ByteSwap(work[0]); + work[1] = ByteSwap(work[1]); + + if (doEncrypt) + { + /* Do the 16 rounds */ + for (i = 0; i < 16; i++) + Round(i, work); + + /* Left/right half swap */ + tmp = work[0]; + work[0] = work[1]; + work[1] = tmp; + } /* end of if */ + else + { + /* Left/right half swap */ + tmp = work[0]; + work[0] = work[1]; + work[1] = tmp; + + /* Do the 16 rounds in reverse order */ + for (i = 15; i >= 0; i--) + Round(i, work); + } /* end of else */ + + work[0] = ByteSwap(work[0]); + work[1] = ByteSwap(work[1]); + + /* Inverse initial permutation */ + Permute((unsigned char *)work, fperm, data); } /* DES() */ /************************************************************************ @@ -297,69 +306,77 @@ void DES_C_DES(unsigned char *key, unsigned char *data, BOOL doEncrypt) ************************************************************************/ static void SetKey(unsigned char *key) { - char pc1m[56]; /* Place to modify pc1 into */ - char pcr[56]; /* Place to rotate pc1 into */ - register int i ,j , k; - int m; - -#if 0 - /* - In mode 2, the 128 bytes of subkey are set directly from the - user's key, allowing him to use completely independent - subkeys for each round. Note that the user MUST specify a - full 128 bytes. - - I would like to think that this technique gives the NSA a real - headache, but I'm not THAT naive. - */ - if (desmode == 2) { - for (i = 0; i < 16; i++) { - for (j = 0; j < 8; j++) - kn[i][j] = *key++; - } /* end of for i */ - - return; - } /* end of if */ -#endif - - /* Clear key schedule */ - for (i = 0; i < 16; i++) { - for (j = 0; j < 8; j++) - kn[i][j] = 0; - } /* end of for i */ - - /* Convert pc1 to bits of key */ - for (j = 0; j < 56; j++) { - k = pc1[j] - 1; /* Integer bit location */ - m = k & 07; /* find bit */ - - /* - Find which key byte l is in and which bit of that byte - and store 1-bit result - */ - pc1m[j] = (key[k >> 3] & byteBit[m]) ? 1 : 0; - } /* end of for j */ - - /* Key chunk for each iteration */ - for (i = 0; i < 16; i++) { - /* rotate pc1 the right amount */ - for (j = 0; j < 56; j++) { - k = j + totRot[i]; - pcr[j] = pc1m[(k < (j < 28 ? 28 : 56)) ? k : k - 28]; - } /* end of for j */ - - /* Rotate left and right halves independently */ - for (j=0; j<48; j++) { - /* Select bits individually, check bit that goes to kn[j] */ - if (pcr[pc2[j] - 1]) { - /* mask it in if it's there */ - k = j % 6; - kn[i][j / 6] |= byteBit[k] >> 2; - } /* end of if */ - } /* end of j */ - } /* end of for i */ - - return; + char pc1m[56]; /* Place to modify pc1 into */ + char pcr[56]; /* Place to rotate pc1 into */ + register int i ,j , k; + int m; + + #if 0 + /* + In mode 2, the 128 bytes of subkey are set directly from the + user's key, allowing him to use completely independent + subkeys for each round. Note that the user MUST specify a + full 128 bytes. + + I would like to think that this technique gives the NSA a real + headache, but I'm not THAT naive. + */ + if (desmode == 2) + { + for (i = 0; i < 16; i++) + { + for (j = 0; j < 8; j++) + kn[i][j] = *key++; + } /* end of for i */ + + return; + } /* end of if */ + #endif + + /* Clear key schedule */ + for (i = 0; i < 16; i++) + { + for (j = 0; j < 8; j++) + kn[i][j] = 0; + } /* end of for i */ + + /* Convert pc1 to bits of key */ + for (j = 0; j < 56; j++) + { + k = pc1[j] - 1; /* Integer bit location */ + m = k & 07; /* find bit */ + + /* + Find which key byte l is in and which bit of that byte + and store 1-bit result + */ + pc1m[j] = (key[k >> 3] & byteBit[m]) ? 1 : 0; + } /* end of for j */ + + /* Key chunk for each iteration */ + for (i = 0; i < 16; i++) + { + /* rotate pc1 the right amount */ + for (j = 0; j < 56; j++) + { + k = j + totRot[i]; + pcr[j] = pc1m[(k < (j < 28 ? 28 : 56)) ? k : k - 28]; + } /* end of for j */ + + /* Rotate left and right halves independently */ + for (j=0; j<48; j++) + { + /* Select bits individually, check bit that goes to kn[j] */ + if (pcr[pc2[j] - 1]) + { + /* mask it in if it's there */ + k = j % 6; + kn[i][j / 6] |= byteBit[k] >> 2; + } /* end of if */ + } /* end of j */ + } /* end of for i */ + + return; } /* SetKey() */ /************************************************************************ @@ -377,39 +394,41 @@ static void SetKey(unsigned char *key) * 20Jan99 RCS Created. ************************************************************************/ static void Permute(unsigned char *inBlock, char perm[16][16][8], - unsigned char *outBlock - ) +unsigned char *outBlock +) { - int i; - int j; - char *p; - char *q; - unsigned char *oBlock; - - if (perm == NULL) { - /* No permutation, just copy */ - for (i = 8; i != 0; i--) - *outBlock++ = *inBlock++; - - return; - } /* end of if */ - - /* Clear output block */ - memset(outBlock, 0, 8); - - for (j = 0; j < 16; j += 2) { - oBlock = outBlock; - - /* For each input nibble and each output byte, OR the masks together */ - p = perm[j][(*inBlock >> 4) & 017]; - q = perm[j + 1][*inBlock & 017]; - for (i = 8; i != 0; i--) - *oBlock++ |= *p++ | *q++; - - inBlock++; - } /* end of for j */ - - return; + int i; + int j; + char *p; + char *q; + unsigned char *oBlock; + + if (perm == NULL) + { + /* No permutation, just copy */ + for (i = 8; i != 0; i--) + *outBlock++ = *inBlock++; + + return; + } /* end of if */ + + /* Clear output block */ + memset(outBlock, 0, 8); + + for (j = 0; j < 16; j += 2) + { + oBlock = outBlock; + + /* For each input nibble and each output byte, OR the masks together */ + p = perm[j][(*inBlock >> 4) & 017]; + q = perm[j + 1][*inBlock & 017]; + for (i = 8; i != 0; i--) + *oBlock++ |= *p++ | *q++; + + inBlock++; + } /* end of for j */ + + return; } /* Permute() */ /************************************************************************ @@ -427,17 +446,17 @@ static void Permute(unsigned char *inBlock, char perm[16][16][8], ************************************************************************/ static void Round(int num, unsigned int *block) { - /* - The rounds are numbered from 0 to 15. On even rounds - the right half is fed to f() and the result exclusive-ORs - the left half; on odd rounds the reverse is done. - */ - if (num & 1) - block[1] ^= F_DES(block[0], kn[num]); - else - block[0] ^= F_DES(block[1], kn[num]); - - return; + /* + The rounds are numbered from 0 to 15. On even rounds + the right half is fed to f() and the result exclusive-ORs + the left half; on odd rounds the reverse is done. + */ + if (num & 1) + block[1] ^= F_DES(block[0], kn[num]); + else + block[0] ^= F_DES(block[1], kn[num]); + + return; } /* Round() */ /************************************************************************ @@ -455,27 +474,27 @@ static void Round(int num, unsigned int *block) ************************************************************************/ static int F_DES(unsigned int r, unsigned char subKey[8]) { - unsigned int rVal, rt; - - /* - Run E(R) ^ K through the combined S & P boxes - This code takes advantage of a convenient regularity in - E, namely that each group of 6 bits in E(R) feeding - a single S-box is a contiguous segment of R. - */ - rt = (r >> 1) | ((r & 1) ? 0x80000000 : 0); - rVal = 0; - rVal |= g____sp[0][((rt >> 26) ^ *subKey++) & 0x3f]; - rVal |= g____sp[1][((rt >> 22) ^ *subKey++) & 0x3f]; - rVal |= g____sp[2][((rt >> 18) ^ *subKey++) & 0x3f]; - rVal |= g____sp[3][((rt >> 14) ^ *subKey++) & 0x3f]; - rVal |= g____sp[4][((rt >> 10) ^ *subKey++) & 0x3f]; - rVal |= g____sp[5][((rt >> 6) ^ *subKey++) & 0x3f]; - rVal |= g____sp[6][((rt >> 2) ^ *subKey++) & 0x3f]; - rt = (r << 1) | ((r & 0x80000000) ? 1 : 0); - rVal |= g____sp[7][(rt ^ *subKey) & 0x3f]; - - return rVal; + unsigned int rVal, rt; + + /* + Run E(R) ^ K through the combined S & P boxes + This code takes advantage of a convenient regularity in + E, namely that each group of 6 bits in E(R) feeding + a single S-box is a contiguous segment of R. + */ + rt = (r >> 1) | ((r & 1) ? 0x80000000 : 0); + rVal = 0; + rVal |= g____sp[0][((rt >> 26) ^ *subKey++) & 0x3f]; + rVal |= g____sp[1][((rt >> 22) ^ *subKey++) & 0x3f]; + rVal |= g____sp[2][((rt >> 18) ^ *subKey++) & 0x3f]; + rVal |= g____sp[3][((rt >> 14) ^ *subKey++) & 0x3f]; + rVal |= g____sp[4][((rt >> 10) ^ *subKey++) & 0x3f]; + rVal |= g____sp[5][((rt >> 6) ^ *subKey++) & 0x3f]; + rVal |= g____sp[6][((rt >> 2) ^ *subKey++) & 0x3f]; + rt = (r << 1) | ((r & 0x80000000) ? 1 : 0); + rVal |= g____sp[7][(rt ^ *subKey) & 0x3f]; + + return rVal; } /* F_DES() */ /************************************************************************ @@ -492,35 +511,40 @@ static int F_DES(unsigned int r, unsigned char subKey[8]) ************************************************************************/ static void PermInit(char perm[16][16][8], const char p[64]) { - int i, j, k, m, n; - - /* Clear the permutation array */ - for (i = 0; i < 16; i++) { - for (j = 0; j < 16; j++) { - for (k = 0; k < 8; k++) - perm[i][j][k]=0; - } /* end of for j */ - } /* end of for i */ - - for (i = 0; i < 16; i++) { /* Each input nibble position */ - for (j = 0; j < 16; j++) { /* Each possible input nibble */ - for (k = 0; k < 64; k++) { /* Each output bit position */ - n = p[k] - 1; - - /* Does this bit come from input position ? */ - if ((n >> 2) != i) - continue; /* No, bit k is 0 */ - - if (!(j & nibbleBit[n & 3])) - continue; - - m = k & 07; /* Which bit is this in the byte */ - perm[i][j][k >> 3] |= byteBit[m]; - } /* end of for k */ - } /* end of for j */ - } /* end of for i */ - - return; + int i, j, k, m, n; + + /* Clear the permutation array */ + for (i = 0; i < 16; i++) + { + for (j = 0; j < 16; j++) + { + for (k = 0; k < 8; k++) + perm[i][j][k]=0; + } /* end of for j */ + } /* end of for i */ + + for (i = 0; i < 16; i++) /* Each input nibble position */ + { + for (j = 0; j < 16; j++) /* Each possible input nibble */ + { + for (k = 0; k < 64; k++) /* Each output bit position */ + { + n = p[k] - 1; + + /* Does this bit come from input position ? */ + if ((n >> 2) != i) + continue; /* No, bit k is 0 */ + + if (!(j & nibbleBit[n & 3])) + continue; + + m = k & 07; /* Which bit is this in the byte */ + perm[i][j][k >> 3] |= byteBit[m]; + } /* end of for k */ + } /* end of for j */ + } /* end of for i */ + + return; } /* PermInit() */ /************************************************************************ @@ -537,37 +561,43 @@ static void PermInit(char perm[16][16][8], const char p[64]) ************************************************************************/ static void SPInit(void) { - char pBox[32]; - int p, i, s, j, rowCol; - int val; - - /* Compute pbox, the inverse of p32i. This is easier to work with. */ - for (p = 0; p < 32; p++) { - for (i = 0; i < 32; i++) { - if (p32i[i] - 1 == p) { - pBox[p] = i; - break; - } /* end of if */ - } /* end of for i */ - } /* end of for p */ - - for (s = 0; s < 8; s++) { /* For each S-box */ - for(i = 0; i < 64; i++) { /* For each possible input */ - val = 0; - /* - The row number is formed from the first and last - bits; the column number is from the middle 4. - */ - rowCol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf); - for (j = 0; j < 4; j++) { /* For each output bit */ - if (si[s][rowCol] & (8 >> j)) - val |= 1L << (31 - pBox[4 * s + j]); - } /* end of for */ - g____sp[s][i] = val; - } /* end of for */ - } /* end of for s */ - - return; + char pBox[32]; + int p, i, s, j, rowCol; + int val; + + /* Compute pbox, the inverse of p32i. This is easier to work with. */ + for (p = 0; p < 32; p++) + { + for (i = 0;i < 32; i++) + { + if (p32i[i] - 1 == p) + { + pBox[p] = i; + break; + } /* end of if */ + } /* end of for i */ + } /* end of for p */ + + for (s = 0; s < 8; s++) /* For each S-box */ + { + for(i = 0; i < 64; i++) /* For each possible input */ + { + val = 0; + /* + The row number is formed from the first and last + bits; the column number is from the middle 4. + */ + rowCol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf); + for (j = 0; j < 4; j++) /* For each output bit */ + { + if (si[s][rowCol] & (8 >> j)) + val |= 1L << (31 - pBox[4 * s + j]); + } /* end of for */ + g____sp[s][i] = val; + } /* end of for */ + } /* end of for s */ + + return; } /* SPInit() */ /************************************************************************ @@ -584,19 +614,19 @@ static void SPInit(void) ************************************************************************/ static unsigned int ByteSwap(unsigned int x) { - char tmp; - char *cp; + char tmp; + char *cp; - cp = (char *)&x; - tmp = cp[3]; - cp[3] = cp[0]; - cp[0] = tmp; + cp = (char *)&x; + tmp = cp[3]; + cp[3] = cp[0]; + cp[0] = tmp; - tmp = cp[2]; - cp[2] = cp[1]; - cp[1] = tmp; + tmp = cp[2]; + cp[2] = cp[1]; + cp[1] = tmp; - return x; + return x; } /* ByteSwap() */ void @@ -607,8 +637,10 @@ pboc3des( unsigned char *key, unsigned char *data, int len ) memset( tmp, 0, sizeof( tmp ) ); - for( i = 0; i < len; i += 8 ) { - for( j = 0; j < 8; j++ ) { + for( i = 0; i < len; i += 8 ) + { + for( j = 0; j < 8; j++ ) + { tmp[ j ] ^= data[ i + j ]; } DES_C_DDES( key, tmp, true ); @@ -625,8 +657,10 @@ pbocdes( unsigned char *key, unsigned char *data, int len, OUT unsigned char *ma memset( tmp, 0, sizeof( tmp ) ); - for( i = 0; i < len; i += 8 ) { - for( j = 0; j < 8; j++ ) { + for( i = 0; i < len; i += 8 ) + { + for( j = 0; j < 8; j++ ) + { tmp[ j ] ^= data[ i + j ]; } DES_C_DES( key, tmp, TRUE ); diff --git a/libs/source/src/DES_C.h b/libs/source/src/DES_C.h index 8255cbe..f8c58aa 100755 --- a/libs/source/src/DES_C.h +++ b/libs/source/src/DES_C.h @@ -26,10 +26,10 @@ #define false 0 #define DES_ENCRYPT 1 #define DES_DECRYPT 0 -// The following ifdef block is the standard way of creating macros which make exporting +// The following ifdef block is the standard way of creating macros which make exporting // from a DLL simpler. All files within this DLL are compiled with the DES_C_EXPORTS // symbol defined on the command line. this symbol should not be defined on any project -// that uses this DLL. This way any other project whose source files include this file see +// that uses this DLL. This way any other project whose source files include this file see // DES_C_API functions as being imported from a DLL, wheras this DLL sees symbols // defined with this macro as being exported. @@ -38,38 +38,38 @@ // parama length(BYTEs) //function : Triple Des encrypt //name : DES_C_DDES -//parama -// key : the input key 8 or 16 -// data : the input data block 8 +//parama +// key : the input key 8 or 16 +// data : the input data block 8 // encrypt : encrypt or decrypt flag true: encrypt; -// false: decrypt +// false: decrypt //author : xiao wei -//last edit date : 2003.4.11 +//last edit date : 2003.4.11 //last edit time : 15:14 //***************************************************************************** void DES_C_DDES( - unsigned char *key, - unsigned char *data, - BOOL encrypt + unsigned char *key, + unsigned char *data, + BOOL encrypt ); //***************************************************************************** // parama length(BYTEs) //function : Des encrypt //name : DES_C_DES -//parama -// key : the input key 8 -// data : the input data block 8 +//parama +// key : the input key 8 +// data : the input data block 8 // doEncrypt : encrypt or decrypt flag 0:encrypt; -// 1:decrypt +// 1:decrypt //author : xiao wei -//last edit date : 2003.4.11 +//last edit date : 2003.4.11 //last edit time : 15:14 //***************************************************************************** void DES_C_DES( - unsigned char *key, - unsigned char *data, - BOOL doEncrypt + unsigned char *key, + unsigned char *data, + BOOL doEncrypt ); diff --git a/libs/source/src/ReaderInterface.h b/libs/source/src/ReaderInterface.h index 3611055..baaa9e8 100755 --- a/libs/source/src/ReaderInterface.h +++ b/libs/source/src/ReaderInterface.h @@ -37,11 +37,9 @@ * You should assume that an application implemenation of this delegate will call [ReaderInterface cardInterface] to obtian a card interface. * @param attached is YES if the 301 has become attached to the phone or NO if the 301 has become detached from the phone. */ -- (void) readerInterfaceDidChange: -(BOOL)attached; +- (void) readerInterfaceDidChange:(BOOL)attached; -- (void) cardInterfaceDidDetach: -(BOOL)attached; +- (void) cardInterfaceDidDetach:(BOOL)attached; @end @@ -54,9 +52,9 @@ * methods of the CardInterface class. */ -@interface ReaderInterface : -NSObject { - +@interface ReaderInterface : NSObject +{ + } /** @@ -65,8 +63,7 @@ NSObject { * Our applicaiton layer might set this back to nil when notifications are no longer desired. * @param delegate is the caller provided delegate. */ -- (void) setDelegate: -(id)delegate; +- (void) setDelegate:(id)delegate; /** * Query if our custom version of the 301 is currently attached to the phone. diff --git a/libs/source/src/ReaderInterface.m b/libs/source/src/ReaderInterface.m index ce8acbb..31894de 100755 --- a/libs/source/src/ReaderInterface.m +++ b/libs/source/src/ReaderInterface.m @@ -30,53 +30,60 @@ @implementation ReaderInterface -- (id)init { - self = [super init]; - if (self) { - } - - return self; +- (id)init +{ + self = [super init]; + if (self) { + } + + return self; } --(BOOL) isReaderAttached { - return gIsOpen; +-(BOOL) isReaderAttached +{ + return gIsOpen; } -- (BOOL)isCardAttached { - - LONG rv; - +- (BOOL)isCardAttached +{ + + LONG rv; + int readerCount=0; SCARD_READERSTATE rgReaderStates[1]; - - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); - - SCARDCONTEXT ContxtHandle; - - ContxtHandle = (SCARDCONTEXT)ccid_descriptor; - - rv = SCardGetStatusChange(ContxtHandle, INFINITE, rgReaderStates, readerCount); - if(rv!=SCARD_S_SUCCESS) { - return NO; - } else - { - if(rgReaderStates[0].dwEventState & SCARD_STATE_EMPTY) { - return NO; - } else - { - return YES; - } + + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); + + SCARDCONTEXT ContxtHandle; + + ContxtHandle = (SCARDCONTEXT)ccid_descriptor; + + rv = SCardGetStatusChange(ContxtHandle, INFINITE, rgReaderStates, readerCount); + if(rv!=SCARD_S_SUCCESS) + { + return NO; + } + else + { + if(rgReaderStates[0].dwEventState & SCARD_STATE_EMPTY) + { + return NO; + } + else + { + return YES; + } } - return NO; + return NO; } -- (void) setDelegate: -(id)delegate { - bR301SessionController *sessionController = [bR301SessionController sharedController]; - -[sessionController setDelegate:delegate]; - +- (void) setDelegate:(id)delegate +{ + bR301SessionController *sessionController = [bR301SessionController sharedController]; + + [sessionController setDelegate:delegate]; + } @end diff --git a/libs/source/src/aes.c b/libs/source/src/aes.c index 5aeeb76..76e044d 100755 --- a/libs/source/src/aes.c +++ b/libs/source/src/aes.c @@ -53,50 +53,52 @@ void KeyExpansion (BYTE* w,BYTE*key,BYTE Nr,BYTE Nk); void AddRoundKey (DWORD round,BYTE *w,BYTE State[4][4]); -const BYTE independency_newAesSbox[16*16]= { - // populate the Sbox matrix - /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ - /*0*/ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, - /*1*/ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, - /*2*/ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, - /*3*/ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, - /*4*/ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, - /*5*/ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, - /*6*/ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, - /*7*/ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, - /*8*/ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, - /*9*/ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, - /*a*/ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, - /*b*/ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, - /*c*/ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, - /*d*/ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, - /*e*/ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, - /*f*/ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 +const BYTE independency_newAesSbox[16*16]= +{// populate the Sbox matrix + /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ + /*0*/ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, + /*1*/ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, + /*2*/ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, + /*3*/ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, + /*4*/ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, + /*5*/ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, + /*6*/ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, + /*7*/ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, + /*8*/ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, + /*9*/ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, + /*a*/ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, + /*b*/ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, + /*c*/ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, + /*d*/ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, + /*e*/ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, + /*f*/ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; -const BYTE independency_newAesiSbox[16*16]= { +const BYTE independency_newAesiSbox[16*16]= +{ // populate the iSbox matrix - /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ - /*0*/ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, - /*1*/ 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, - /*2*/ 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, - /*3*/ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, - /*4*/ 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, - /*5*/ 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, - /*6*/ 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, - /*7*/ 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, - /*8*/ 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, - /*9*/ 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, - /*a*/ 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, - /*b*/ 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, - /*c*/ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, - /*d*/ 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, - /*e*/ 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, - /*f*/ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d + /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ + /*0*/ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, + /*1*/ 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, + /*2*/ 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, + /*3*/ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, + /*4*/ 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, + /*5*/ 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, + /*6*/ 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, + /*7*/ 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, + /*8*/ 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, + /*9*/ 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, + /*a*/ 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, + /*b*/ 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, + /*c*/ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, + /*d*/ 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, + /*e*/ 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, + /*f*/ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; -const BYTE independency_newAesRcon[11*4]= { - 0x00, 0x00, 0x00, 0x00, +const BYTE independency_newAesRcon[11*4]= +{ + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, @@ -113,17 +115,19 @@ const BYTE independency_newAesRcon[11*4]= { void AES_Memcpy(BYTE * d, BYTE * s, int Len) { int i; - for(i=0; i 6 && (row % Nk == 4) ) { + } + else if ( Nk > 6 && (row % Nk == 4) ) + { SubDWORD(temp); - } - - // w[row] = w[row-Nk] xor temp - w[4*row+0] = ( BYTE ) ( (DWORD) w[4*(row-Nk)+0] ^ (DWORD)temp[0] ); - w[4*row+1] = ( BYTE ) ( (DWORD) w[4*(row-Nk)+1] ^ (DWORD)temp[1] ); - w[4*row+2] = ( BYTE ) ( (DWORD) w[4*(row-Nk)+2] ^ (DWORD)temp[2] ); - w[4*row+3] = ( BYTE ) ( (DWORD) w[4*(row-Nk)+3] ^ (DWORD)temp[3] ); + } + + // w[row] = w[row-Nk] xor temp + w[4*row+0] = ( BYTE ) ( (DWORD) w[4*(row-Nk)+0] ^ (DWORD)temp[0] ); + w[4*row+1] = ( BYTE ) ( (DWORD) w[4*(row-Nk)+1] ^ (DWORD)temp[1] ); + w[4*row+2] = ( BYTE ) ( (DWORD) w[4*(row-Nk)+2] ^ (DWORD)temp[2] ); + w[4*row+3] = ( BYTE ) ( (DWORD) w[4*(row-Nk)+3] ^ (DWORD)temp[3] ); } // for loop - + } void RotDWORD(BYTE* w) @@ -205,7 +219,8 @@ void SubDWORD(BYTE* oneWord) { BYTE temp[ 4 ]; DWORD j; - for(j=0; j<4; j++) { + for(j=0;j<4;j++) + { temp[j] = independency_newAesSbox[16*(oneWord[j] >> 4)+(oneWord[j] & 0x0f)]; } AES_Memcpy( oneWord, temp, 4 ); @@ -218,25 +233,28 @@ void AES_enc(BYTE* input, BYTE* output,BYTE *w,BYTE Nr) DWORD round; BYTE gState[4][4]; AES_Memset(&gState[0][0],0,16); - for( i=0; i<4*NB; i++) { + for( i=0;i<4*NB;i++) + { gState[i%4][i/4]=input[i]; } AddRoundKey(0,w,gState); - - for ( round = 1; round <= (Nr - 1); round++) { - SubBytes(gState); - ShiftRows(gState); - MixColumns(gState); - AddRoundKey(round,w,gState); + + for ( round = 1; round <= (Nr - 1); round++) + { + SubBytes(gState); + ShiftRows(gState); + MixColumns(gState); + AddRoundKey(round,w,gState); } // main round loop - + SubBytes(gState); ShiftRows(gState); AddRoundKey(Nr,w,gState); - + // output = state - for (i = 0; i < (4 * NB); i++) { - output[i] = gState[i % 4][ i / 4]; + for (i = 0; i < (4 * NB); i++) + { + output[i] = gState[i % 4][ i / 4]; } } @@ -246,35 +264,40 @@ void AES_dec(BYTE* input,BYTE* output,BYTE *w,BYTE Nr) BYTE gState[4][4]; DWORD i,round; AES_Memset(&gState[0][0],0,16); - for ( i = 0; i < (4 * NB); i++) { + for ( i = 0; i < (4 * NB); i++) + { gState[i % 4][ i / 4] = input[i]; } - + AddRoundKey(Nr,w,gState); - - for ( round = Nr-1; round >= 1; round--) { // main round loop - InvShiftRows(gState); - InvSubBytes(gState); - AddRoundKey(round,w,gState); - InvMixColumns(gState); + + for ( round = Nr-1; round >= 1; round--) // main round loop + { + InvShiftRows(gState); + InvSubBytes(gState); + AddRoundKey(round,w,gState); + InvMixColumns(gState); } // end main round loop for InvCipher - + InvShiftRows(gState); InvSubBytes(gState); AddRoundKey(0,w,gState); - + // output = state - for (i = 0; i < (4 * NB); i++) { - output[i] = gState[i % 4][ i / 4]; + for (i = 0; i < (4 * NB); i++) + { + output[i] = gState[i % 4][ i / 4]; } } void AddRoundKey(DWORD round,BYTE *w,BYTE State[4][4]) { DWORD i,j; - for(j=0; j<4; j++) { - for(i=0; i<4; i++) { - State[i][j]=(BYTE)((DWORD)State[i][j]^(DWORD)w[4*((round*4)+j)+i]); + for(j=0;j<4;j++) + { + for(i=0;i<4;i++) + { + State[i][j]=(BYTE)((DWORD)State[i][j]^(DWORD)w[4*((round*4)+j)+i]); } } } @@ -282,8 +305,10 @@ void AddRoundKey(DWORD round,BYTE *w,BYTE State[4][4]) void SubBytes(BYTE State[4][4]) //Page 103 { DWORD i,j; - for(j=0; j<4; j++) { - for(i=0; i<4; i++) { + for(j=0;j<4;j++) + { + for(i=0;i<4;i++) + { State[i][j]=independency_newAesSbox[State[i][j]]; } @@ -292,8 +317,10 @@ void SubBytes(BYTE State[4][4]) //Page 103 void InvSubBytes(BYTE State[4][4]) { DWORD i,j; - for(j=0; j<4; j++) { - for(i=0; i<4; i++) { + for(j=0;j<4;j++) + { + for(i=0;i<4;i++) + { State[i][j]=independency_newAesiSbox[State[i][j]]; } } @@ -304,13 +331,17 @@ void ShiftRows(BYTE State[4][4]) { BYTE temp[4*4]; //Page105 DWORD i,j; - for(j=0; j<4; j++) { - for(i=0; i<4; i++) { + for(j=0;j<4;j++) + { + for(i=0;i<4;i++) + { temp[4*i+j]=State[i][j]; } } - for(i=1; i<4; i++) { - for(j=0; j<4; j++) { + for(i=1;i<4;i++) + { + for(j=0;j<4;j++) + { if(i==1)State[i][j]=temp[4*i+(j+1)%4]; else if(i==2)State[i][j]=temp[4*i+(j+2)%4]; else if(i==3)State[i][j]=temp[4*i+(j+3)%4]; @@ -322,64 +353,71 @@ void InvShiftRows(BYTE State[4][4]) { BYTE temp[4*4]; DWORD i,j; - for(j=0; j<4; j++) { - for(i=0; i<4; i++) { + for(j=0;j<4;j++) + { + for(i=0;i<4;i++) + { temp[4*i+j]=State[i][j]; } } - for(i=1; i<4; i++) { - for(j=0; j<4; j++) { + for(i=1;i<4;i++) + { + for(j=0;j<4;j++) + { if(i==1)State[i][j]=temp[4*i+(j+3)%4]; else if(i==2)State[i][j]=temp[4*i+(j+2)%4]; else if(i==3)State[i][j]=temp[4*i+(j+1)%4]; } } - + } //////////////////////////////////////////////////////////////////////////////////////////////// void MixColumns(BYTE State[4][4]) { BYTE temp[4*4]; DWORD i,j; - for(j=0; j<4; j++) { //2 3 1 1 Page107 - //1 2 3 1 - for(i=0; i<4; i++) { //1 1 2 3 - //3 1 1 2 + for(j=0;j<4;j++) //2 3 1 1 Page107 + { //1 2 3 1 + for(i=0;i<4;i++) //1 1 2 3 + { //3 1 1 2 temp[4*i+j]=State[i][j]; } } - for(j=0; j<4; j++) { + for(j=0;j<4;j++) + { State[0][j] = (BYTE) ( (DWORD)gfmultby02(temp[0+j]) ^ (DWORD)gfmultby03(temp[4*1+j]) ^ - (DWORD)gfmultby01(temp[4*2+j]) ^ (DWORD)gfmultby01(temp[4*3+j]) ); + (DWORD)gfmultby01(temp[4*2+j]) ^ (DWORD)gfmultby01(temp[4*3+j]) ); State[1][j] = (BYTE) ( (DWORD)gfmultby01(temp[0+j]) ^ (DWORD)gfmultby02(temp[4*1+j]) ^ - (DWORD)gfmultby03(temp[4*2+j]) ^ (DWORD)gfmultby01(temp[4*3+j]) ); + (DWORD)gfmultby03(temp[4*2+j]) ^ (DWORD)gfmultby01(temp[4*3+j]) ); State[2][j] = (BYTE) ( (DWORD)gfmultby01(temp[0+j]) ^ (DWORD)gfmultby01(temp[4*1+j]) ^ - (DWORD)gfmultby02(temp[4*2+j]) ^ (DWORD)gfmultby03(temp[4*3+j]) ); + (DWORD)gfmultby02(temp[4*2+j]) ^ (DWORD)gfmultby03(temp[4*3+j]) ); State[3][j] = (BYTE) ( (DWORD)gfmultby03(temp[0+j]) ^ (DWORD)gfmultby01(temp[4*1+j]) ^ - (DWORD)gfmultby01(temp[4*2+j]) ^ (DWORD)gfmultby02(temp[4*3+j]) ); + (DWORD)gfmultby01(temp[4*2+j]) ^ (DWORD)gfmultby02(temp[4*3+j]) ); } - + } void InvMixColumns(BYTE State[4][4]) { BYTE temp[4*4]; DWORD i,j; - for (i = 0; i < 4; i++) { // copy State DWORDo temp[] - for (j = 0; j < 4; j++) { //0e 0b 0d 09 Page108 - //09 0e 0b 0d + for (i = 0; i < 4; i++) // copy State DWORDo temp[] + { + for (j = 0; j < 4; j++) //0e 0b 0d 09 Page108 + { //09 0e 0b 0d temp[4*i+j] = State[i][j]; //0d 09 0e 0b - } //0b 0d 09 0e + } //0b 0d 09 0e } - - for (j = 0; j < 4; j++) { + + for (j = 0; j < 4; j++) + { State[0][j] = (BYTE) ( (DWORD)gfmultby0e(temp[j]) ^ (DWORD)gfmultby0b(temp[4+j]) ^ - (DWORD)gfmultby0d(temp[4*2+j]) ^ (DWORD)gfmultby09(temp[4*3+j]) ); + (DWORD)gfmultby0d(temp[4*2+j]) ^ (DWORD)gfmultby09(temp[4*3+j]) ); State[1][j] = (BYTE) ( (DWORD)gfmultby09(temp[j]) ^ (DWORD)gfmultby0e(temp[4+j]) ^ - (DWORD)gfmultby0b(temp[4*2+j]) ^ (DWORD)gfmultby0d(temp[4*3+j]) ); + (DWORD)gfmultby0b(temp[4*2+j]) ^ (DWORD)gfmultby0d(temp[4*3+j]) ); State[2][j] = (BYTE) ( (DWORD)gfmultby0d(temp[j]) ^ (DWORD)gfmultby09(temp[4+j]) ^ - (DWORD)gfmultby0e(temp[4*2+j]) ^ (DWORD)gfmultby0b(temp[4*3+j]) ); + (DWORD)gfmultby0e(temp[4*2+j]) ^ (DWORD)gfmultby0b(temp[4*3+j]) ); State[3][j] = (BYTE) ( (DWORD)gfmultby0b(temp[j]) ^ (DWORD)gfmultby0d(temp[4+j]) ^ - (DWORD)gfmultby09(temp[4*2+j]) ^ (DWORD)gfmultby0e(temp[4*3+j]) ); + (DWORD)gfmultby09(temp[4*2+j]) ^ (DWORD)gfmultby0e(temp[4*3+j]) ); } } //////////////////////////////////////////////////////////////////////////////////////////////// @@ -390,9 +428,9 @@ BYTE gfmultby01(BYTE b) BYTE gfmultby02(BYTE b) { if (b < 0x80) - return (BYTE)(DWORD)(b <<1); + return (BYTE)(DWORD)(b <<1); else - return (BYTE)( (DWORD)(b << 1) ^ (DWORD)(0x1b) ); + return (BYTE)( (DWORD)(b << 1) ^ (DWORD)(0x1b) ); } BYTE gfmultby03(BYTE b) @@ -408,19 +446,19 @@ BYTE gfmultby09(BYTE b) BYTE gfmultby0b(BYTE b) { return (BYTE)( (DWORD)gfmultby02(gfmultby02(gfmultby02(b))) ^ - (DWORD)gfmultby02(b) ^ (DWORD)b ); + (DWORD)gfmultby02(b) ^ (DWORD)b ); } BYTE gfmultby0d(BYTE b) { return (BYTE)( (DWORD)gfmultby02(gfmultby02(gfmultby02(b))) ^ - (DWORD)gfmultby02(gfmultby02(b)) ^ (DWORD)(b) ); + (DWORD)gfmultby02(gfmultby02(b)) ^ (DWORD)(b) ); } BYTE gfmultby0e(BYTE b) { return (BYTE)( (DWORD)gfmultby02(gfmultby02(gfmultby02(b))) ^ - (DWORD)gfmultby02(gfmultby02(b)) ^(DWORD)gfmultby02(b) ); + (DWORD)gfmultby02(gfmultby02(b)) ^(DWORD)gfmultby02(b) ); } void Ft_iR301U_AES_encrypt(PBYTE plaintext, PBYTE ciphertext, PBYTE key, int szKey) @@ -429,7 +467,7 @@ void Ft_iR301U_AES_encrypt(PBYTE plaintext, PBYTE ciphertext, PBYTE key, int szK BYTE aeskey[32]; BYTE NNr,NNk; Aes_setKey(szKey, key, ww, aeskey, &NNr, &NNk); - AES_enc(plaintext, ciphertext, ww, NNr); + AES_enc(plaintext, ciphertext, ww, NNr); } void Ft_iR301U_AES_decrypt(PBYTE ciphertext, PBYTE plaintext, PBYTE key, int szKey) @@ -438,5 +476,5 @@ void Ft_iR301U_AES_decrypt(PBYTE ciphertext, PBYTE plaintext, PBYTE key, int szK BYTE aeskey[32]; BYTE NNr,NNk; Aes_setKey(szKey, key, ww, aeskey, &NNr, &NNk); - AES_dec(ciphertext, plaintext, ww, NNr); + AES_dec(ciphertext, plaintext, ww, NNr); } diff --git a/libs/source/src/aes.h b/libs/source/src/aes.h index a2ab251..4ae3466 100755 --- a/libs/source/src/aes.h +++ b/libs/source/src/aes.h @@ -30,50 +30,53 @@ #define MODE_ENCRYPT 1 #define MODE_DECRYPT 0 -typedef struct st_aes_cfb_context { - unsigned char inter_in[AES_BLOCK_SIZE+1]; - unsigned char inter_out[AES_BLOCK_SIZE+1]; - int szBlock; - int k; +typedef struct st_aes_cfb_context +{ + unsigned char inter_in[AES_BLOCK_SIZE+1]; + unsigned char inter_out[AES_BLOCK_SIZE+1]; + int szBlock; + int k; } AES_CFB_CONTEXT, AES_CFB_CTX; -typedef struct st_aes_ofb_context { - unsigned char interBlk[AES_BLOCK_SIZE]; - int szBlock; +typedef struct st_aes_ofb_context +{ + unsigned char interBlk[AES_BLOCK_SIZE]; + int szBlock; } AES_OFB_CONTEXT, AES_OFB_CTX; -typedef struct st_aes_ctr_context { - unsigned char interBlk[AES_BLOCK_SIZE]; - int szBlock; +typedef struct st_aes_ctr_context +{ + unsigned char interBlk[AES_BLOCK_SIZE]; + int szBlock; } AES_CTR_CONTEXT, AES_CTR_CTX; #ifdef __cplusplus -extern "C" { +extern "C"{ #endif - - /***************************************************************************** - Prototype : AES_encrypt - Description : AES encryption - Param : unsigned char* plaintext [in ] the password in plain text£¨buffer size is 16 bytes - Param : unsigned char* ciphertext [out] Password ciphertext output£¨buffer size is 16 bytes - Param : unsigned char* key [in ] key £¨buffer size is keyLen bytes - Param : int szKey [in ] size of key,16/24/32 - Return Value : none - *****************************************************************************/ - extern void Ft_iR301U_AES_encrypt( unsigned char * plaintext, unsigned char * ciphertext, unsigned char * key, int szKey); - - /***************************************************************************** - Prototype : AES_decrypt - Description : AES decryption - Param : unsigned char* plaintext [in ] the password in plain text£¨buffer size is 16 bytes - Param : unsigned char* ciphertext [out] Password ciphertext output£¨buffer size is 16 bytes - Param : unsigned char* key [in ] key £¨buffer size is keyLen bytes - Param : int szKey [in ] size of key,16/24/32 - Return Value : none - *****************************************************************************/ - extern void Ft_iR301U_AES_decrypt( unsigned char * ciphertext, unsigned char * plaintext, unsigned char * key, int szKey); + +/***************************************************************************** + Prototype : AES_encrypt + Description : AES encryption + Param : unsigned char* plaintext [in ] the password in plain text£¨buffer size is 16 bytes + Param : unsigned char* ciphertext [out] Password ciphertext output£¨buffer size is 16 bytes + Param : unsigned char* key [in ] key £¨buffer size is keyLen bytes + Param : int szKey [in ] size of key,16/24/32 + Return Value : none + *****************************************************************************/ +extern void Ft_iR301U_AES_encrypt( unsigned char * plaintext, unsigned char * ciphertext, unsigned char * key, int szKey); + +/***************************************************************************** + Prototype : AES_decrypt + Description : AES decryption + Param : unsigned char* plaintext [in ] the password in plain text£¨buffer size is 16 bytes + Param : unsigned char* ciphertext [out] Password ciphertext output£¨buffer size is 16 bytes + Param : unsigned char* key [in ] key £¨buffer size is keyLen bytes + Param : int szKey [in ] size of key,16/24/32 + Return Value : none + *****************************************************************************/ +extern void Ft_iR301U_AES_decrypt( unsigned char * ciphertext, unsigned char * plaintext, unsigned char * key, int szKey); #ifdef __cplusplus diff --git a/libs/source/src/atr.c b/libs/source/src/atr.c index 3fb42ab..f603a0f 100755 --- a/libs/source/src/atr.c +++ b/libs/source/src/atr.c @@ -1,353 +1,410 @@ -/* - atr.c - ISO 7816 ICC's answer to reset abstract data type implementation - - This file is part of the Unix driver for Towitoko smartcard readers - Copyright (C) 2000 Carlos Prados - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this library; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "ft_ccid.h" - -static unsigned -atr_num_ib_table[16] = { - 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 -}; - -/* - * Exported variables definition - */ - -static unsigned -atr_f_table[16] = { - 372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0 -}; - -static unsigned -atr_d_table[16] = { - 0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 0, 0, 0, 0, 0, 0 -}; - -static unsigned -atr_i_table[4] = { - 25, 50, 100, 0 -}; - -/* - * Exported functions definition - */ - -// modified by Zhou Yu 2009.4.28, formalize none 7816 compliant Fi/Di -int -ATR_AdjustFiDi(BYTE atr_buffer[ATR_MAX_SIZE], unsigned length) -{ - ATR_t atr; - BYTE TDi; - BYTE Fi, Di; - unsigned i; - BYTE table[16][16] = { - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x12, 0x12, 0x22, 0x32, 0x11, 0x11, 0x11, 0x11, 0x11, 0x92, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x13, 0x13, 0x23, 0x33, 0x42, 0x53, 0x11, 0x11, 0x11, 0x93, 0xa3, 0xb3, 0x11, 0x11, 0x11, 0x11, - 0x14, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x11, 0x11, 0x94, 0xa4, 0xb4, 0xc4, 0xd4, 0x11, 0x11, - 0x15, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x11, 0x11, 0x95, 0xa5, 0xb5, 0xc5, 0xd5, 0x11, 0x11, - 0x16, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x11, 0x11, 0x96, 0xa6, 0xb6, 0xc6, 0xd6, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x18, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x11, 0x11, 0x98, 0xa8, 0xb8, 0xc8, 0xc8, 0x11, 0x11, - 0x19, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x11, 0x11, 0x99, 0xa9, 0xb9, 0xc9, 0xc9, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 - }; - - ATR_InitFromArray(&atr, atr_buffer, length); - - if(ATR_NOT_FOUND - == ATR_GetIntegerValue(&atr, ATR_INTEGER_VALUE_FI, &Fi)) { - Fi = 1; - } - - if(ATR_NOT_FOUND - == ATR_GetIntegerValue(&atr, ATR_INTEGER_VALUE_DI, &Di)) { - Di = 1; - } - - /* Check size of buffer */ - if (length < 2) - return (ATR_MALFORMED); - - TDi = atr_buffer[1]; - - /* Check TAi is present */ - if ((TDi | 0xEF) == 0xFF) { - atr_buffer[2] = table[Di][Fi]; - } else { - return (ATR_NOT_FOUND); - } - - /* recalculate TCK */ - if ((atr.TCK).present) { - atr_buffer[length - 1] = 0; - - for(i = 1; i < length - 1; i++) { - atr_buffer[length - 1] ^= - atr_buffer[i]; - - } - - } - - return (ATR_OK); -} - -int -ATR_InitFromArray (ATR_t * atr, const BYTE atr_buffer[ATR_MAX_SIZE], unsigned length) -{ - BYTE TDi; - unsigned pointer = 0, pn = 0; - - /* Check size of buffer */ - if (length < 2) - return (ATR_MALFORMED); - - /* Store T0 and TS */ - atr->TS = atr_buffer[0]; - - atr->T0 = TDi = atr_buffer[1]; - pointer = 1; - - /* Store number of historical bytes */ - atr->hbn = TDi & 0x0F; - - /* TCK is not present by default */ - (atr->TCK).present = false; - - /* Extract interface bytes */ - while (pointer < length) { - /* Check buffer is long enought */ - if (pointer + atr_num_ib_table[(0xF0 & TDi) >> 4] >= length) { - return (ATR_MALFORMED); - } - /* Check TAi is present */ - if ((TDi | 0xEF) == 0xFF) { - pointer++; - atr->ib[pn][ATR_INTERFACE_BYTE_TA].value = atr_buffer[pointer]; - atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = true; - } else - atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = false; - /* Check TBi is present */ - if ((TDi | 0xDF) == 0xFF) { - pointer++; - atr->ib[pn][ATR_INTERFACE_BYTE_TB].value = atr_buffer[pointer]; - atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = true; - } else - atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = false; - - /* Check TCi is present */ - if ((TDi | 0xBF) == 0xFF) { - pointer++; - atr->ib[pn][ATR_INTERFACE_BYTE_TC].value = atr_buffer[pointer]; - atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = true; - } else - atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = false; - - /* Read TDi if present */ - if ((TDi | 0x7F) == 0xFF) { - pointer++; - TDi = atr->ib[pn][ATR_INTERFACE_BYTE_TD].value = atr_buffer[pointer]; - atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = true; - (atr->TCK).present = ((TDi & 0x0F) != ATR_PROTOCOL_TYPE_T0); - if (pn >= ATR_MAX_PROTOCOLS) - return (ATR_MALFORMED); - pn++; - } else { - atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = false; - break; - } - } - - /* Store number of protocols */ - atr->pn = pn + 1; - - /* Store historical bytes */ - if (pointer + atr->hbn >= length) - return (ATR_MALFORMED); - - memcpy (atr->hb, atr_buffer + pointer + 1, atr->hbn); - pointer += (atr->hbn); - - /* Store TCK */ - if ((atr->TCK).present) { - - if (pointer + 1 >= length) - return (ATR_MALFORMED); - - pointer++; - - (atr->TCK).value = atr_buffer[pointer]; - } - - atr->length = pointer + 1; - return (ATR_OK); -} - -int -ATR_GetConvention (ATR_t * atr, int *convention) -{ - if (atr->TS == 0x3B) - (*convention) = ATR_CONVENTION_DIRECT; - else if (atr->TS == 0x3F) - (*convention) = ATR_CONVENTION_INVERSE; - else - return (ATR_MALFORMED); - return (ATR_OK); -} - -int -ATR_GetIntegerValue (ATR_t * atr, int name, BYTE * value) -{ - int ret; - - if (name == ATR_INTEGER_VALUE_FI) { - if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present) { - (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0xF0) >> 4; - ret = ATR_OK; - } else - ret = ATR_NOT_FOUND; - } else if (name == ATR_INTEGER_VALUE_DI) { - if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present) { - (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0x0F); - ret = ATR_OK; - } else - ret = ATR_NOT_FOUND; - } else if (name == ATR_INTEGER_VALUE_II) { - if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present) { - (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x60) >> 5; - ret = ATR_OK; - } else - ret = ATR_NOT_FOUND; - } else if (name == ATR_INTEGER_VALUE_PI1) { - if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present) { - (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x1F); - ret = ATR_OK; - } else - ret = ATR_NOT_FOUND; - } else if (name == ATR_INTEGER_VALUE_PI2) { - if (atr->ib[1][ATR_INTERFACE_BYTE_TB].present) { - (*value) = atr->ib[1][ATR_INTERFACE_BYTE_TB].value; - ret = ATR_OK; - } else - ret = ATR_NOT_FOUND; - } else if (name == ATR_INTEGER_VALUE_N) { - if (atr->ib[0][ATR_INTERFACE_BYTE_TC].present) { - (*value) = atr->ib[0][ATR_INTERFACE_BYTE_TC].value; - ret = ATR_OK; - } else - ret = ATR_NOT_FOUND; - } else - ret = ATR_NOT_FOUND; - - return ret; -} - -int -ATR_GetParameter (ATR_t * atr, int name, double *parameter) -{ - BYTE FI, DI, II, PI1, PI2, N; - - if (name == ATR_PARAMETER_F) { - if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &FI) == ATR_OK) - (*parameter) = (double) (atr_f_table[FI]); - else - (*parameter) = (double) ATR_DEFAULT_F; - - return (ATR_OK); - } else if (name == ATR_PARAMETER_D) { - if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_DI, &DI) == ATR_OK) - (*parameter) = (double) (atr_d_table[DI]); - else - (*parameter) = (double) ATR_DEFAULT_D; - - return (ATR_OK); - } - - else if (name == ATR_PARAMETER_I) { - if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_II, &II) == ATR_OK) - (*parameter) = (double) (atr_i_table[II]); - else - (*parameter) = ATR_DEFAULT_I; - - return (ATR_OK); - } else if (name == ATR_PARAMETER_P) { - if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI2, &PI2) == ATR_OK) - (*parameter) = (double) PI2; - else if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI1, &PI1) == ATR_OK) - (*parameter) = (double) PI1; - else - (*parameter) = (double) ATR_DEFAULT_P; - - return (ATR_OK); - } - - else if (name == ATR_PARAMETER_N) { - if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_N, &N) == ATR_OK) - (*parameter) = (double) N; - else - (*parameter) = (double) ATR_DEFAULT_N; - - return (ATR_OK); - } - - return (ATR_NOT_FOUND); -} - -/* - * This function was greatly inspired by ATRDecodeAtr() and - * PHGetDefaultProtocol() from pcsc-lite - * - * It was rewritten by Ludovic Rousseau, 2004 - */ -#define PROTOCOL_UNSET -1 -int ATR_GetDefaultProtocol(ATR_t * atr, int *protocol) -{ - int i; - - /* default value */ - *protocol = PROTOCOL_UNSET; - - for (i=0; iib[i][ATR_INTERFACE_BYTE_TD].present && (PROTOCOL_UNSET == *protocol)) { - /* set to the first protocol byte found */ - *protocol = atr->ib[i][ATR_INTERFACE_BYTE_TD].value & 0x0F; - } - - /* specific mode if TA2 present */ - if (atr->ib[1][ATR_INTERFACE_BYTE_TA].present) { - *protocol = atr->ib[1][ATR_INTERFACE_BYTE_TA].value & 0x0F; - } - - if (PROTOCOL_UNSET == *protocol) { - // no default protocol found in ATR. Using T=0 - *protocol = ATR_PROTOCOL_TYPE_T0; - } - - return ATR_OK; -} - +/* + atr.c + ISO 7816 ICC's answer to reset abstract data type implementation + + This file is part of the Unix driver for Towitoko smartcard readers + Copyright (C) 2000 Carlos Prados + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this library; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "ft_ccid.h" + +static unsigned +atr_num_ib_table[16] = +{ + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 +}; + +/* + * Exported variables definition + */ + +static unsigned +atr_f_table[16] = +{ + 372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0 +}; + +static unsigned +atr_d_table[16] = +{ + 0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 0, 0, 0, 0, 0, 0 +}; + +static unsigned +atr_i_table[4] = +{ + 25, 50, 100, 0 +}; + +/* + * Exported functions definition + */ + +// modified by Zhou Yu 2009.4.28, formalize none 7816 compliant Fi/Di +int +ATR_AdjustFiDi(BYTE atr_buffer[ATR_MAX_SIZE], unsigned length) +{ + ATR_t atr; + BYTE TDi; + BYTE Fi, Di; + unsigned i; + BYTE table[16][16] = + {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x12, 0x12, 0x22, 0x32, 0x11, 0x11, 0x11, 0x11, 0x11, 0x92, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x13, 0x13, 0x23, 0x33, 0x42, 0x53, 0x11, 0x11, 0x11, 0x93, 0xa3, 0xb3, 0x11, 0x11, 0x11, 0x11, + 0x14, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x11, 0x11, 0x94, 0xa4, 0xb4, 0xc4, 0xd4, 0x11, 0x11, + 0x15, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x11, 0x11, 0x95, 0xa5, 0xb5, 0xc5, 0xd5, 0x11, 0x11, + 0x16, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x11, 0x11, 0x96, 0xa6, 0xb6, 0xc6, 0xd6, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x18, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x11, 0x11, 0x98, 0xa8, 0xb8, 0xc8, 0xc8, 0x11, 0x11, + 0x19, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x11, 0x11, 0x99, 0xa9, 0xb9, 0xc9, 0xc9, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 + }; + + ATR_InitFromArray(&atr, atr_buffer, length); + + if(ATR_NOT_FOUND + == ATR_GetIntegerValue(&atr, ATR_INTEGER_VALUE_FI, &Fi)) + { + Fi = 1; + } + + if(ATR_NOT_FOUND + == ATR_GetIntegerValue(&atr, ATR_INTEGER_VALUE_DI, &Di)) + { + Di = 1; + } + + /* Check size of buffer */ + if (length < 2) + return (ATR_MALFORMED); + + TDi = atr_buffer[1]; + + /* Check TAi is present */ + if ((TDi | 0xEF) == 0xFF) + { + atr_buffer[2] = table[Di][Fi]; + } + else + { + return (ATR_NOT_FOUND); + } + + /* recalculate TCK */ + if ((atr.TCK).present) + { + atr_buffer[length - 1] = 0; + + for(i = 1; i < length - 1; i++) + { + atr_buffer[length - 1] ^= + atr_buffer[i]; + + } + + } + + return (ATR_OK); +} + +int +ATR_InitFromArray (ATR_t * atr, const BYTE atr_buffer[ATR_MAX_SIZE], unsigned length) +{ + BYTE TDi; + unsigned pointer = 0, pn = 0; + + /* Check size of buffer */ + if (length < 2) + return (ATR_MALFORMED); + + /* Store T0 and TS */ + atr->TS = atr_buffer[0]; + + atr->T0 = TDi = atr_buffer[1]; + pointer = 1; + + /* Store number of historical bytes */ + atr->hbn = TDi & 0x0F; + + /* TCK is not present by default */ + (atr->TCK).present = false; + + /* Extract interface bytes */ + while (pointer < length) + { + /* Check buffer is long enought */ + if (pointer + atr_num_ib_table[(0xF0 & TDi) >> 4] >= length) + { + return (ATR_MALFORMED); + } + /* Check TAi is present */ + if ((TDi | 0xEF) == 0xFF) + { + pointer++; + atr->ib[pn][ATR_INTERFACE_BYTE_TA].value = atr_buffer[pointer]; + atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = true; + } + else + atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = false; + /* Check TBi is present */ + if ((TDi | 0xDF) == 0xFF) + { + pointer++; + atr->ib[pn][ATR_INTERFACE_BYTE_TB].value = atr_buffer[pointer]; + atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = true; + } + else + atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = false; + + /* Check TCi is present */ + if ((TDi | 0xBF) == 0xFF) + { + pointer++; + atr->ib[pn][ATR_INTERFACE_BYTE_TC].value = atr_buffer[pointer]; + atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = true; + } + else + atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = false; + + /* Read TDi if present */ + if ((TDi | 0x7F) == 0xFF) + { + pointer++; + TDi = atr->ib[pn][ATR_INTERFACE_BYTE_TD].value = atr_buffer[pointer]; + atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = true; + (atr->TCK).present = ((TDi & 0x0F) != ATR_PROTOCOL_TYPE_T0); + if (pn >= ATR_MAX_PROTOCOLS) + return (ATR_MALFORMED); + pn++; + } + else + { + atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = false; + break; + } + } + + /* Store number of protocols */ + atr->pn = pn + 1; + + /* Store historical bytes */ + if (pointer + atr->hbn >= length) + return (ATR_MALFORMED); + + memcpy (atr->hb, atr_buffer + pointer + 1, atr->hbn); + pointer += (atr->hbn); + + /* Store TCK */ + if ((atr->TCK).present) + { + + if (pointer + 1 >= length) + return (ATR_MALFORMED); + + pointer++; + + (atr->TCK).value = atr_buffer[pointer]; + } + + atr->length = pointer + 1; + return (ATR_OK); +} + +int +ATR_GetConvention (ATR_t * atr, int *convention) +{ + if (atr->TS == 0x3B) + (*convention) = ATR_CONVENTION_DIRECT; + else if (atr->TS == 0x3F) + (*convention) = ATR_CONVENTION_INVERSE; + else + return (ATR_MALFORMED); + return (ATR_OK); +} + +int +ATR_GetIntegerValue (ATR_t * atr, int name, BYTE * value) +{ + int ret; + + if (name == ATR_INTEGER_VALUE_FI) + { + if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present) + { + (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0xF0) >> 4; + ret = ATR_OK; + } + else + ret = ATR_NOT_FOUND; + } + else if (name == ATR_INTEGER_VALUE_DI) + { + if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present) + { + (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0x0F); + ret = ATR_OK; + } + else + ret = ATR_NOT_FOUND; + } + else if (name == ATR_INTEGER_VALUE_II) + { + if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present) + { + (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x60) >> 5; + ret = ATR_OK; + } + else + ret = ATR_NOT_FOUND; + } + else if (name == ATR_INTEGER_VALUE_PI1) + { + if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present) + { + (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x1F); + ret = ATR_OK; + } + else + ret = ATR_NOT_FOUND; + } + else if (name == ATR_INTEGER_VALUE_PI2) + { + if (atr->ib[1][ATR_INTERFACE_BYTE_TB].present) + { + (*value) = atr->ib[1][ATR_INTERFACE_BYTE_TB].value; + ret = ATR_OK; + } + else + ret = ATR_NOT_FOUND; + } + else if (name == ATR_INTEGER_VALUE_N) + { + if (atr->ib[0][ATR_INTERFACE_BYTE_TC].present) + { + (*value) = atr->ib[0][ATR_INTERFACE_BYTE_TC].value; + ret = ATR_OK; + } + else + ret = ATR_NOT_FOUND; + } + else + ret = ATR_NOT_FOUND; + + return ret; +} + +int +ATR_GetParameter (ATR_t * atr, int name, double *parameter) +{ + BYTE FI, DI, II, PI1, PI2, N; + + if (name == ATR_PARAMETER_F) + { + if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &FI) == ATR_OK) + (*parameter) = (double) (atr_f_table[FI]); + else + (*parameter) = (double) ATR_DEFAULT_F; + + return (ATR_OK); + } + else if (name == ATR_PARAMETER_D) + { + if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_DI, &DI) == ATR_OK) + (*parameter) = (double) (atr_d_table[DI]); + else + (*parameter) = (double) ATR_DEFAULT_D; + + return (ATR_OK); + } + + else if (name == ATR_PARAMETER_I) + { + if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_II, &II) == ATR_OK) + (*parameter) = (double) (atr_i_table[II]); + else + (*parameter) = ATR_DEFAULT_I; + + return (ATR_OK); + } + else if (name == ATR_PARAMETER_P) + { + if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI2, &PI2) == ATR_OK) + (*parameter) = (double) PI2; + else if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI1, &PI1) == ATR_OK) + (*parameter) = (double) PI1; + else + (*parameter) = (double) ATR_DEFAULT_P; + + return (ATR_OK); + } + + else if (name == ATR_PARAMETER_N) + { + if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_N, &N) == ATR_OK) + (*parameter) = (double) N; + else + (*parameter) = (double) ATR_DEFAULT_N; + + return (ATR_OK); + } + + return (ATR_NOT_FOUND); +} + +/* + * This function was greatly inspired by ATRDecodeAtr() and + * PHGetDefaultProtocol() from pcsc-lite + * + * It was rewritten by Ludovic Rousseau, 2004 + */ +#define PROTOCOL_UNSET -1 +int ATR_GetDefaultProtocol(ATR_t * atr, int *protocol) +{ + int i; + + /* default value */ + *protocol = PROTOCOL_UNSET; + + for (i=0; iib[i][ATR_INTERFACE_BYTE_TD].present && (PROTOCOL_UNSET == *protocol)) + { + /* set to the first protocol byte found */ + *protocol = atr->ib[i][ATR_INTERFACE_BYTE_TD].value & 0x0F; + } + + /* specific mode if TA2 present */ + if (atr->ib[1][ATR_INTERFACE_BYTE_TA].present) + { + *protocol = atr->ib[1][ATR_INTERFACE_BYTE_TA].value & 0x0F; + } + + if (PROTOCOL_UNSET == *protocol) + { + // no default protocol found in ATR. Using T=0 + *protocol = ATR_PROTOCOL_TYPE_T0; + } + + return ATR_OK; +} + diff --git a/libs/source/src/bR301SessionController.h b/libs/source/src/bR301SessionController.h old mode 100644 new mode 100755 index 0b5ae47..0666a8b --- a/libs/source/src/bR301SessionController.h +++ b/libs/source/src/bR301SessionController.h @@ -1,36 +1,37 @@ -// -// bR301SessionController.h -// bR301SessionController -// -// Created by Ben on 12/2/15. -// Copyright © 2015 FTSAFE. All rights reserved. -// - -#import -#import -#import "ReaderInterface.h" - -@interface bR301SessionController : NSObject { - - // id detegate; -} - -+ (bR301SessionController *)sharedController; - -- (void) setDelegate:(id)Delegate; - --(void) RegisterAccessoryConnectNotification; --(void) UnRegisterAccessoryConnectNotification; -- (int)identifyAccessoryCount; -- (int)writeData:(unsigned char *)data withLength:(unsigned int) len; -- (int)readData:(unsigned char *) data withbytesToRead:(unsigned int*)bytesToRead; - - -@property (nonatomic, strong) EAAccessory *accessory; -@property (nonatomic, strong) NSString *protocolString; -@property (nonatomic, strong) EASession *session; -@property (nonatomic, strong) NSThread *thread; -@property (nonatomic, strong) NSThread *CardStatusDidChange; -@property (nonatomic, strong) id detegate; - -@end +// +// bR301SessionController.h +// bR301SessionController +// +// Created by Ben on 12/2/15. +// Copyright © 2015 FTSAFE. All rights reserved. +// + +#import +#import +#import "ReaderInterface.h" + +@interface bR301SessionController : NSObject { + + // id detegate; +} + ++ (bR301SessionController *)sharedController; + +- (void) setDelegate:(id)Delegate; + +-(void) RegisterAccessoryConnectNotification; +-(void) UnRegisterAccessoryConnectNotification; +- (int)identifyAccessoryCount; +- (int)writeData:(unsigned char *)data withLength:(unsigned int) len; +- (int)readData:(unsigned char *) data withbytesToRead:(unsigned int*)bytesToRead; + + +//@property (atomic,strong) NSRecursiveLock *theLock;//atomic多线程访问互斥 +@property (nonatomic, strong) EAAccessory *accessory; +@property (nonatomic, strong) NSString *protocolString; +@property (nonatomic, strong) EASession *session; +@property (nonatomic, strong) NSThread *thread; +@property (nonatomic, strong) NSThread *CardStatusDidChange; +@property (nonatomic, strong) id detegate; + +@end diff --git a/libs/source/src/buffer.c b/libs/source/src/buffer.c index 5f359cb..b03d03d 100755 --- a/libs/source/src/buffer.c +++ b/libs/source/src/buffer.c @@ -1,72 +1,72 @@ -/* - * Buffer handling functions - * - * Copyright (C) 2003, Olaf Kirch - */ - - -#include -#include -#include "buffer.h" - -void -ct_buf_init(ct_buf_t *bp, void *mem, unsigned int len) -{ - memset(bp, 0, sizeof(*bp)); - bp->base = (unsigned char *) mem; - bp->size = len; -} - -void -ct_buf_set(ct_buf_t *bp, void *mem, unsigned int len) -{ - ct_buf_init(bp, mem, len); - bp->tail = len; -} - -int -ct_buf_get(ct_buf_t *bp, void *mem, unsigned int len) -{ - if (len > bp->tail - bp->head) - return -1; - if (mem) - memcpy(mem, bp->base + bp->head, len); - - bp->head += len; - return len; -} - -int -ct_buf_put(ct_buf_t *bp, const void *mem, unsigned int len) -{ - if (len > bp->size - bp->tail) { - bp->overrun = 1; - return -1; - } - if (mem) - memcpy(bp->base + bp->tail, mem, len); - - bp->tail += len; - return len; -} - -int -ct_buf_putc(ct_buf_t *bp, int byte) -{ - unsigned char c = byte; - - return ct_buf_put(bp, &c, 1); -} - -unsigned int -ct_buf_avail(ct_buf_t *bp) -{ - return bp->tail - bp->head; -} - -void * -ct_buf_head(ct_buf_t *bp) -{ - return bp->base + bp->head; -} - +/* + * Buffer handling functions + * + * Copyright (C) 2003, Olaf Kirch + */ + + +#include +#include +#include "buffer.h" + +void +ct_buf_init(ct_buf_t *bp, void *mem, unsigned int len) +{ + memset(bp, 0, sizeof(*bp)); + bp->base = (unsigned char *) mem; + bp->size = len; +} + +void +ct_buf_set(ct_buf_t *bp, void *mem, unsigned int len) +{ + ct_buf_init(bp, mem, len); + bp->tail = len; +} + +int +ct_buf_get(ct_buf_t *bp, void *mem, unsigned int len) +{ + if (len > bp->tail - bp->head) + return -1; + if (mem) + memcpy(mem, bp->base + bp->head, len); + + bp->head += len; + return len; +} + +int +ct_buf_put(ct_buf_t *bp, const void *mem, unsigned int len) +{ + if (len > bp->size - bp->tail) { + bp->overrun = 1; + return -1; + } + if (mem) + memcpy(bp->base + bp->tail, mem, len); + + bp->tail += len; + return len; +} + +int +ct_buf_putc(ct_buf_t *bp, int byte) +{ + unsigned char c = byte; + + return ct_buf_put(bp, &c, 1); +} + +unsigned int +ct_buf_avail(ct_buf_t *bp) +{ + return bp->tail - bp->head; +} + +void * +ct_buf_head(ct_buf_t *bp) +{ + return bp->base + bp->head; +} + diff --git a/libs/source/src/buffer.h b/libs/source/src/buffer.h index f85d308..078e4dd 100755 --- a/libs/source/src/buffer.h +++ b/libs/source/src/buffer.h @@ -1,35 +1,35 @@ -/* - * Buffer handling functions of the IFD handler library - * - * Copyright (C) 2003, Olaf Kirch - */ - -#ifndef OPENCT_BUFFER_H -#define OPENCT_BUFFER_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - - - typedef struct ct_buf { - unsigned char * base; - unsigned int head, tail, size; - unsigned int overrun; - } ct_buf_t; - - extern void ct_buf_init(ct_buf_t *, void *, unsigned int); - extern void ct_buf_set(ct_buf_t *, void *, unsigned int); - extern int ct_buf_get(ct_buf_t *, void *, unsigned int); - extern int ct_buf_put(ct_buf_t *, const void *, unsigned int); - extern int ct_buf_putc(ct_buf_t *, int); - extern unsigned int ct_buf_avail(ct_buf_t *); - extern void * ct_buf_head(ct_buf_t *); - -#ifdef __cplusplus -} -#endif - -#endif /* OPENCT_BUFFER_H */ +/* + * Buffer handling functions of the IFD handler library + * + * Copyright (C) 2003, Olaf Kirch + */ + +#ifndef OPENCT_BUFFER_H +#define OPENCT_BUFFER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + + +typedef struct ct_buf { + unsigned char * base; + unsigned int head, tail, size; + unsigned int overrun; +} ct_buf_t; + +extern void ct_buf_init(ct_buf_t *, void *, unsigned int); +extern void ct_buf_set(ct_buf_t *, void *, unsigned int); +extern int ct_buf_get(ct_buf_t *, void *, unsigned int); +extern int ct_buf_put(ct_buf_t *, const void *, unsigned int); +extern int ct_buf_putc(ct_buf_t *, int); +extern unsigned int ct_buf_avail(ct_buf_t *); +extern void * ct_buf_head(ct_buf_t *); + +#ifdef __cplusplus +} +#endif + +#endif /* OPENCT_BUFFER_H */ diff --git a/libs/source/src/checksum.c b/libs/source/src/checksum.c index f103d35..4fcf045 100755 --- a/libs/source/src/checksum.c +++ b/libs/source/src/checksum.c @@ -1,105 +1,107 @@ -/* - proto-t1.h: header file for proto-t1.c - Copyright (C) 2004 Ludovic Rousseau - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this library; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/* $Id: checksum.h 2974 2008-05-28 18:32:52Z rousseau $ */ - -#include "checksum.h" - -#define min( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) ) - -/* ISO STD 3309 */ -/* From: medin@catbyte.b30.ingr.com (Dave Medin) - * Subject: CCITT checksums - * Newsgroups: sci.electronics - * Date: Mon, 7 Dec 1992 17:33:39 GMT - */ - -/* Correct Table? */ - -static unsigned short crctab[256] = { - 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, - 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, - 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, - 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, - 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, - 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, - 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, - 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, - 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, - 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, - 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, - 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, - 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, - 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, - 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, - 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, - 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, - 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, - 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, - 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, - 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, - 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, - 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, - 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, - 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, - 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, - 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, - 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, - 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, - 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, - 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, - 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 -}; - -/* - * Returns LRC of data. - */ -unsigned int -csum_lrc_compute(const uint8_t *in, size_t len, unsigned char *rc) -{ - unsigned char lrc = 0; - - while (len--) - lrc ^= *in++; - - if (rc) - *rc = lrc; - return 1; -} - -/* - * Compute CRC of data. - */ -unsigned int -csum_crc_compute(const uint8_t * data, size_t len, unsigned char *rc) -{ - unsigned short v = 0xFFFF; - - while (len--) { - v = ((v >> 8) & 0xFF) ^ crctab[(v ^ *data++) & 0xFF]; - } - - if (rc) { - rc[0] = (v >> 8) & 0xFF; - rc[1] = v & 0xFF; - } - - return 2; -} - +/* + proto-t1.h: header file for proto-t1.c + Copyright (C) 2004 Ludovic Rousseau + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this library; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* $Id: checksum.h 2974 2008-05-28 18:32:52Z rousseau $ */ + +#include "checksum.h" + +#define min( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) ) + +/* ISO STD 3309 */ +/* From: medin@catbyte.b30.ingr.com (Dave Medin) + * Subject: CCITT checksums + * Newsgroups: sci.electronics + * Date: Mon, 7 Dec 1992 17:33:39 GMT + */ + +/* Correct Table? */ + +static unsigned short crctab[256] = { + 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, + 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, + 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, + 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, + 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, + 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, + 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, + 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, + 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, + 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, + 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, + 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, + 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, + 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, + 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, + 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, + 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, + 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, + 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, + 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, + 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, + 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, + 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, + 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, + 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, + 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, + 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, + 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, + 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, + 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, + 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, + 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 +}; + +/* + * Returns LRC of data. + */ +unsigned int +csum_lrc_compute(const uint8_t *in, size_t len, unsigned char *rc) +{ + unsigned char lrc = 0; + + while (len--) + lrc ^= *in++; + + if (rc) + *rc = lrc; + return 1; +} + +/* + * Compute CRC of data. + */ +unsigned int +csum_crc_compute(const uint8_t * data, size_t len, unsigned char *rc) +{ + unsigned short v = 0xFFFF; + + while (len--) + { + v = ((v >> 8) & 0xFF) ^ crctab[(v ^ *data++) & 0xFF]; + } + + if (rc) + { + rc[0] = (v >> 8) & 0xFF; + rc[1] = v & 0xFF; + } + + return 2; +} + diff --git a/libs/source/src/checksum.h b/libs/source/src/checksum.h index 37920f0..0748c00 100755 --- a/libs/source/src/checksum.h +++ b/libs/source/src/checksum.h @@ -1,19 +1,19 @@ -/* - * Checksum handling - * - * Copyright Matthias Bruestle 1999-2002 - * For licensing, see the file LICENCE - */ - -#ifndef __CHECKSUM_H__ -#define __CHECKSUM_H__ - - -#include -#include - -extern unsigned int csum_lrc_compute(const uint8_t *, size_t, unsigned char *); -extern unsigned int csum_crc_compute(const uint8_t *, size_t, unsigned char *); - -#endif - +/* + * Checksum handling + * + * Copyright Matthias Bruestle 1999-2002 + * For licensing, see the file LICENCE + */ + +#ifndef __CHECKSUM_H__ +#define __CHECKSUM_H__ + + +#include +#include + +extern unsigned int csum_lrc_compute(const uint8_t *, size_t, unsigned char *); +extern unsigned int csum_crc_compute(const uint8_t *, size_t, unsigned char *); + +#endif + diff --git a/libs/source/src/crypto.c b/libs/source/src/crypto.c index 04ac625..e73b8b2 100755 --- a/libs/source/src/crypto.c +++ b/libs/source/src/crypto.c @@ -25,57 +25,66 @@ int PKCS7_Padding(unsigned char *pbMsg, int nMsgLen, int szblk) { - int nPadLen; + int nPadLen; - nPadLen = szblk - nMsgLen % szblk; - //pading - if (nPadLen) { - memset( pbMsg+nMsgLen, nPadLen, nPadLen ); - } + nPadLen = szblk - nMsgLen % szblk; + //pading + if (nPadLen) + { + memset( pbMsg+nMsgLen, nPadLen, nPadLen ); + } - return (nMsgLen+nPadLen); + return (nMsgLen+nPadLen); } int PKCS7_Padding_Check(unsigned char *pbMsg, int nMsgLen, int szblk) { - unsigned char i; - unsigned char *p = pbMsg + nMsgLen - 1; - unsigned char nPadLen = *p; - - if(0 != nMsgLen%szblk || 0 == nPadLen || nPadLen > szblk) { - return 0; - } - - for (i = 0; i < nPadLen; ++i) { - if(nPadLen != *p--) { - return 0; - } - } - - return 1; + unsigned char i; + unsigned char *p = pbMsg + nMsgLen - 1; + unsigned char nPadLen = *p; + + if(0 != nMsgLen%szblk || 0 == nPadLen || nPadLen > szblk) + { + return 0; + } + + for (i = 0; i < nPadLen; ++i) + { + if(nPadLen != *p--) + { + return 0; + } + } + + return 1; } static void xor(unsigned char *a, unsigned char *b, unsigned char *r, int len) { - while(len--) { - *r++ = *a++ ^ *b++; - } + while(len--) + { + *r++ = *a++ ^ *b++; + } - return; + return; } void AES_ECB(unsigned char *inBlk, int len, unsigned char *outBlk, unsigned char *key, int keyLen, int mode) { - int i; - - len -= len % AES_BLOCK_SIZE; - - for (i = 0; i < len; i += AES_BLOCK_SIZE ) { - if ( mode == MODE_ENCRYPT ) { + int i; + + len -= len % AES_BLOCK_SIZE; + + for (i = 0; i < len; i += AES_BLOCK_SIZE ) + { + if ( mode == MODE_ENCRYPT ) + { Ft_iR301U_AES_encrypt( inBlk, outBlk, key, keyLen ); - } else { + } + else + { Ft_iR301U_AES_decrypt( inBlk, outBlk, key, keyLen ); } inBlk += AES_BLOCK_SIZE; @@ -89,103 +98,116 @@ void AES_ECB(unsigned char *inBlk, int len, unsigned char *outBlk, unsigned cha void TDES(unsigned char * inBlk, unsigned char * outBlk, unsigned char *key, int keyLen, int mode) { - + unsigned char *key1, *key2, *key3; - switch ( keyLen ) { + switch ( keyLen ) + { case 8: - key1 = key; - key2 = key; - key3 = key; + key1 = key; + key2 = key; + key3 = key; break; case 16: - key1 = key; - key2 = key + 8; - key3 = key; + key1 = key; + key2 = key + 8; + key3 = key; break; case 24: - if (MODE_DECRYPT != mode) { - key1 = key + 0; - key2 = key + 8; - key3 = key + 16; - } else { - key1 = key + 16; - key2 = key + 8; - key3 = key + 0; - } + if (MODE_DECRYPT != mode) + { + key1 = key + 0; + key2 = key + 8; + key3 = key + 16; + } + else + { + key1 = key + 16; + key2 = key + 8; + key3 = key + 0; + } break; } - memcpy(outBlk, inBlk, 8); + memcpy(outBlk, inBlk, 8); DES_C_DES(key1, outBlk, mode); - DES_C_DES(key2, outBlk, !mode); - DES_C_DES(key3, outBlk, mode); + DES_C_DES(key2, outBlk, !mode); + DES_C_DES(key3, outBlk, mode); - return; + return; } void TDES_ECB(unsigned char *inBlk, int len, unsigned char *outBlk, unsigned char *key, int keyLen, int mode) { - int i = 0; + int i = 0; - for (i = 0; i < (len/DES_BLOCK_SIZE); i++) { - TDES(inBlk, outBlk, key, keyLen, mode); - inBlk += DES_BLOCK_SIZE; - outBlk += DES_BLOCK_SIZE; - } + for (i = 0; i < (len/DES_BLOCK_SIZE); i++) + { + TDES(inBlk, outBlk, key, keyLen, mode); + inBlk += DES_BLOCK_SIZE; + outBlk += DES_BLOCK_SIZE; + } - return; + return; } int TDES_ECB_PKCS7(unsigned char *inBuf, unsigned int inLen, unsigned char *outBuf, unsigned int *outLen, unsigned char *key, unsigned int keyLen, int mode) { - int nLastBlk = inLen % DES_BLOCK_SIZE; - unsigned char *outLastBlk = outBuf+inLen-nLastBlk; - - TDES_ECB(inBuf, inLen-nLastBlk, outBuf, key, keyLen, mode); - - if(MODE_ENCRYPT == mode) { - memcpy(outLastBlk, inBuf+inLen-nLastBlk, nLastBlk); - PKCS7_Padding(outLastBlk, nLastBlk, DES_BLOCK_SIZE); - TDES_ECB(outLastBlk, DES_BLOCK_SIZE, outLastBlk, key, keyLen, mode); - - *outLen = (inLen/DES_BLOCK_SIZE + 1) * DES_BLOCK_SIZE; - } else { - if (0 == PKCS7_Padding_Check(outBuf, inLen, DES_BLOCK_SIZE)) { - return 0; - } - - *outLen = inLen - *(outLastBlk-1); - } - - return 1; + int nLastBlk = inLen % DES_BLOCK_SIZE; + unsigned char *outLastBlk = outBuf+inLen-nLastBlk; + + TDES_ECB(inBuf, inLen-nLastBlk, outBuf, key, keyLen, mode); + + if(MODE_ENCRYPT == mode) + { + memcpy(outLastBlk, inBuf+inLen-nLastBlk, nLastBlk); + PKCS7_Padding(outLastBlk, nLastBlk, DES_BLOCK_SIZE); + TDES_ECB(outLastBlk, DES_BLOCK_SIZE, outLastBlk, key, keyLen, mode); + + *outLen = (inLen/DES_BLOCK_SIZE + 1) * DES_BLOCK_SIZE; + } + else + { + if (0 == PKCS7_Padding_Check(outBuf, inLen, DES_BLOCK_SIZE)) + { + return 0; + } + + *outLen = inLen - *(outLastBlk-1); + } + + return 1; } int AES_ECB_PKCS7(unsigned char *inBuf, unsigned int inLen, unsigned char *outBuf, unsigned int *outLen, unsigned char *key, unsigned int keyLen, int mode) { - int nLastBlk = inLen % AES_BLOCK_SIZE; - unsigned char *outLastBlk = outBuf+inLen-nLastBlk; - - AES_ECB(inBuf, inLen-nLastBlk, outBuf, key, keyLen, mode); - - if(MODE_ENCRYPT == mode) { - memcpy(outLastBlk, inBuf+inLen-nLastBlk, nLastBlk); - PKCS7_Padding(outLastBlk, nLastBlk, AES_BLOCK_SIZE); - AES_ECB(outLastBlk, AES_BLOCK_SIZE, outLastBlk, key, keyLen, mode); - - *outLen = (inLen/AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE; - } else { - if (0 == PKCS7_Padding_Check(outBuf, inLen, AES_BLOCK_SIZE)) { - return 0; - } - - *outLen = inLen - *(outLastBlk-1); - } - - return 1; + int nLastBlk = inLen % AES_BLOCK_SIZE; + unsigned char *outLastBlk = outBuf+inLen-nLastBlk; + + AES_ECB(inBuf, inLen-nLastBlk, outBuf, key, keyLen, mode); + + if(MODE_ENCRYPT == mode) + { + memcpy(outLastBlk, inBuf+inLen-nLastBlk, nLastBlk); + PKCS7_Padding(outLastBlk, nLastBlk, AES_BLOCK_SIZE); + AES_ECB(outLastBlk, AES_BLOCK_SIZE, outLastBlk, key, keyLen, mode); + + *outLen = (inLen/AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE; + } + else + { + if (0 == PKCS7_Padding_Check(outBuf, inLen, AES_BLOCK_SIZE)) + { + return 0; + } + + *outLen = inLen - *(outLastBlk-1); + } + + return 1; } diff --git a/libs/source/src/crypto.h b/libs/source/src/crypto.h index 7633fe6..fa42b1b 100755 --- a/libs/source/src/crypto.h +++ b/libs/source/src/crypto.h @@ -31,46 +31,46 @@ #ifdef __cplusplus -extern "C" { +extern "C"{ #endif /* __cplusplus */ //***************************************************************************** -// +// //function : Universal PKCS7 padding //name : PKCS7_Padding -//parama +//parama // unsigned char *pbMsg: in&out buffer // int nMsgLen : Data byte length // int szblk : Encryption block length in bytes (DES-8,AES-16) // -//author : -//last edit date : -//last edit time : +//author : +//last edit date : +//last edit time : // //***************************************************************************** - extern int PKCS7_Padding(unsigned char *pbMsg, int nMsgLen, int szblk); +extern int PKCS7_Padding(unsigned char *pbMsg, int nMsgLen, int szblk); //***************************************************************************** -// +// //function : Detect compliance PKCS#7 padding //name : PKCS7_Padding_Check -//parama +//parama // unsigned char *pbMsg: in&out buffer // int nMsgLen : Data byte length // int szblk : Encryption block length in bytes (DES-8,AES-16) // -//author : -//last edit date : -//last edit time : +//author : +//last edit date : +//last edit time : // //***************************************************************************** - extern int PKCS7_Padding_Check(unsigned char *pbMsg, int nMsgLen, int szblk); +extern int PKCS7_Padding_Check(unsigned char *pbMsg, int nMsgLen, int szblk); //***************************************************************************** -// +// //function : TDES ECB Operation Mode and PKCS#7 padding //name : TDES_ECB_PKCS7 -//parama +//parama // unsigned char *inBlk: in buffer // int len : data byte length // unsigned char *outBlk: out buffer @@ -79,18 +79,18 @@ extern "C" { // int keyLen : key length (16,24,32) // int mode : encryption and decryption mode (MODE_ENCRYPT/MODE_DECRYPT) // -//author : -//last edit date : -//last edit time : +//author : +//last edit date : +//last edit time : // //***************************************************************************** - extern int TDES_ECB_PKCS7(unsigned char *inBuf, unsigned int inLen, unsigned char *outBuf, unsigned int *outLen, unsigned char *key, unsigned int keyLen, int mode); +extern int TDES_ECB_PKCS7(unsigned char *inBuf, unsigned int inLen, unsigned char *outBuf, unsigned int *outLen, unsigned char *key, unsigned int keyLen, int mode); //***************************************************************************** -// +// //function : AES ECB Operation Mode and PKCS#7 padding //name : AES_ECB_PKCS7 -//parama +//parama // unsigned char *inBlk: in buffer // int len : data byte length // unsigned char *outBlk: out buffer @@ -99,21 +99,21 @@ extern "C" { // int keyLen : key length (16,24,32) // int mode : encryption and decryption mode (MODE_ENCRYPT/MODE_DECRYPT) // -//author : -//last edit date : -//last edit time : +//author : +//last edit date : +//last edit time : // //***************************************************************************** - extern int AES_ECB_PKCS7(unsigned char *inBuf, unsigned int inLen, unsigned char *outBuf, unsigned int *outLen, unsigned char *key, unsigned int keyLen, int mode); +extern int AES_ECB_PKCS7(unsigned char *inBuf, unsigned int inLen, unsigned char *outBuf, unsigned int *outLen, unsigned char *key, unsigned int keyLen, int mode); - extern void TDES(unsigned char * inBlk, unsigned char * outBlk, unsigned char *key, int keyLen, int mode); +extern void TDES(unsigned char * inBlk, unsigned char * outBlk, unsigned char *key, int keyLen, int mode); //***************************************************************************** -// +// //function : TDES ECB Operation Mode //name : TDES_ECB -//parama +//parama // unsigned char *inBlk: in buffer // int len : data byte length // unsigned char *outBlk: out buffer @@ -121,18 +121,18 @@ extern "C" { // int keyLen : key length (16,24,32) // int mode : encryption and decryption mode (MODE_ENCRYPT/MODE_DECRYPT) // -//author : -//last edit date : -//last edit time : +//author : +//last edit date : +//last edit time : // //***************************************************************************** - extern void TDES_ECB(unsigned char *inBlk, int len, unsigned char *outBlk, unsigned char *key, int keyLen, int mode); +extern void TDES_ECB(unsigned char *inBlk, int len, unsigned char *outBlk, unsigned char *key, int keyLen, int mode); //***************************************************************************** -// +// //function : AES ECB Operation Mode //name : AES_ECB -//parama +//parama // unsigned char *inBlk: in buffer // int len : data byte length // unsigned char *outBlk: out buffer @@ -140,12 +140,12 @@ extern "C" { // int keyLen : key length (16,24,32) // int mode : encryption and decryption mode (MODE_ENCRYPT/MODE_DECRYPT) // -//author : -//last edit date : -//last edit time : +//author : +//last edit date : +//last edit time : // //***************************************************************************** - extern void AES_ECB(unsigned char *inBlk, int len, unsigned char *outBlk, unsigned char *key, int keyLen, int mode); +extern void AES_ECB(unsigned char *inBlk, int len, unsigned char *outBlk, unsigned char *key, int keyLen, int mode); #ifdef __cplusplus } diff --git a/libs/source/src/debug.c b/libs/source/src/debug.c index ec6c3c5..24112f5 100755 --- a/libs/source/src/debug.c +++ b/libs/source/src/debug.c @@ -1,17 +1,17 @@ /* debug.c: log (or not) messages Copyright (C) 2003-2011 Ludovic Rousseau - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - + This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -25,22 +25,22 @@ #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include "ft_ccid.h" #define DEBUG_BUF_SIZE ((256+20)*3+10) -#if defined(__MACH__) && defined(__FreeBSD__) && defined(__NetBSD__) && defined(__OpenBSD__) && defined(__DragonFly__) -#define ERROR_SIGNAL SIGBUS -#else -#define ERROR_SIGNAL SIGSEGV -#endif +#if defined(__MACH__) && defined(__FreeBSD__) && defined(__NetBSD__) && defined(__OpenBSD__) && defined(__DragonFly__) +#define ERROR_SIGNAL SIGBUS +#else +#define ERROR_SIGNAL SIGSEGV +#endif -#define FT_IR301_DEBUG +//#define FT_IR301_DEBUG #ifdef FT_IR301_DEBUG #define LOG_TO_STDERR 0 @@ -49,7 +49,7 @@ #endif static char DebugBuffer[DEBUG_BUF_SIZE]; -static sigjmp_buf badreadjmpbuf; +static sigjmp_buf badreadjmpbuf; void log_msg(const char *fmt, ...) { @@ -68,7 +68,7 @@ void log_msg(const char *fmt, ...) void Dpt(const char *fmt, ...) { #if LOG_TO_STDERR - return; + return; #endif va_list argptr; @@ -79,198 +79,204 @@ void Dpt(const char *fmt, ...) }/* Dpt */ -static void badreadfunc(int signo) -{ - /*write(STDOUT_FILENO, "catch\n", 6);*/ - siglongjmp(badreadjmpbuf, 1); +static void badreadfunc(int signo) +{ + /*write(STDOUT_FILENO, "catch\n", 6);*/ + siglongjmp(badreadjmpbuf, 1); } //***************************************************************************** -// +// //function : verifies that the calling process has read access to the specified range of memory //name : isbadreadptr -//parama +//parama // void *ptr : memory address // int length : size of block // -//author : -//last edit date : -//last edit time : +//author : +//last edit date : +//last edit time : // //***************************************************************************** -int isbadreadptr(void *ptr, int length) -{ - struct sigaction sa, osa; - int ret = 0; - int ntmp ; - char btmp; - - /*init new handler struct*/ - sa.sa_handler = badreadfunc; - sigemptyset(&sa.sa_mask); - sa.sa_flags = 0; - - /*retrieve old and set new handlers*/ - if(sigaction(ERROR_SIGNAL, &sa, &osa)<0) - return (-1); - - if(sigsetjmp(badreadjmpbuf, 1) == 0) { - int i, hi=length/sizeof(int), remain=length%sizeof(int); - int* pi = (int *)ptr; - char* pc = (char*)ptr + hi; - for(i=0; i= 1) && (error <= 127)) - (void)snprintf(var_text, sizeof(var_text), "error on byte %d", - error); - else - (void)snprintf(var_text, sizeof(var_text), - "Unknown CCID error: 0x%02X", error); - - text = var_text; - break; + + switch (error) + { + case 0x00: + text = "Command not supported or not allowed"; + break; + + case 0x01: + text = "Wrong command length"; + break; + + case 0x05: + text = "Invalid slot number"; + break; + + case 0xA2: + text = "Card short-circuiting. Card powered off"; + break; + + case 0xA3: + text = "ATR too long (> 33)"; + break; + + case 0xAB: + text = "No data exchanged"; + break; + + case 0xB0: + text = "Reader in EMV mode and T=1 message too long"; + break; + + case 0xBB: + text = "Protocol error in EMV mode"; + break; + + case 0xBD: + text = "Card error during T=1 exchange"; + break; + + case 0xBE: + text = "Wrong APDU command length"; + break; + + case 0xE0: + text = "Slot busy"; + break; + + case 0xEF: + text = "PIN cancelled"; + break; + + case 0xF0: + text = "PIN timeout"; + break; + + case 0xF2: + text = "Busy with autosequence"; + break; + + case 0xF3: + text = "Deactivated protocol"; + break; + + case 0xF4: + text = "Procedure byte conflict"; + break; + + case 0xF5: + text = "Class not supported"; + break; + + case 0xF6: + text = "Protocol not supported"; + break; + + case 0xF7: + text = "Invalid ATR checksum byte (TCK)"; + break; + + case 0xF8: + text = "Invalid ATR first byte"; + break; + + case 0xFB: + text = "Hardware error"; + break; + + case 0xFC: + text = "Overrun error"; + break; + + case 0xFD: + text = "Parity error during exchange"; + break; + + case 0xFE: + text = "Card absent or mute"; + break; + + case 0xFF: + text = "Activity aborted by Host"; + break; + + default: + if ((error >= 1) && (error <= 127)) + (void)snprintf(var_text, sizeof(var_text), "error on byte %d", + error); + else + (void)snprintf(var_text, sizeof(var_text), + "Unknown CCID error: 0x%02X", error); + + text = var_text; + break; } log_msg( "%s:%d:%s %s", file, line, function, text); - + } /* ccid_error */ diff --git a/libs/source/src/ft301u.h b/libs/source/src/ft301u.h index d041dd9..fccebe1 100755 --- a/libs/source/src/ft301u.h +++ b/libs/source/src/ft301u.h @@ -1,202 +1,202 @@ -/* -* Support for bR301(Bluetooth) smart card reader -* -* ft301u.h: header file for ft_ccid.c -* -* Copyright (C) Feitian 2014, Ben -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 2.1 of the License, or (at your option) any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public -* License along with this library; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -/** - * @file - * @This isfeitian's private cmd. - */ - -#ifndef __ft301u_h__ -#define __ft301u_h__ - - -#include "wintypes.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - LONG SCARD_CTL_CODE(unsigned int code); - /* - Function: FtGetSerialNum - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - length IN length of buffer(>=8) - buffer OUT Serial number - - Description: - This function userd to get serial number of iR301. - */ - - LONG FtGetSerialNum(SCARDHANDLE hCard, unsigned int length, - char * buffer); - /* - Function: FtWriteFlash - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - bOffset IN Offset of flash to write - blength IN The length of data - buffer IN The data for write - - Description: - This function userd to write data to flash. - */ - LONG FtWriteFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, - unsigned char buffer[]); - /* - Function: FtReadFlash - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - bOffset IN Offset of flash to write - blength IN The length of read data - buffer OUT The read data - - Description: - This function used to read data from flash. - */ - LONG FtReadFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, - unsigned char buffer[]); - - /* - Function: FtSetTimeout - - Parameters: - hContext IN Connection context to the PC/SC Resource Manager - dwTimeout IN New transmission timeout value of between 301 and card (millisecond ) - - Description: - The function New transmission timeout value of between 301 and card. - - */ - LONG FtSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout); - - - //for dukpt - /* - Function: FtDukptInit - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - encBuf IN Ciphertext use TDES_ECB_PKCS7/AES_ECB_PKCS7 (See "Key C" ) - nLen IN encBuf length(40(TDES_ECB_PKCS7 ciphertext length) 48(AES_ECB_PKCS7 ciphertext length)) - - Description: - Init iR301 new ipek and ksn for dukpt. - - */ - LONG FtDukptInit(SCARDHANDLE hCard,unsigned char *encBuf,unsigned int nLen); - - - /* - Function: FtDukptSetEncMod - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - bEncrypt IN 1: SCardTransmit Encrypted - 0:SCardTransmit not Encrypted - - bEncFunc IN 0: Encryption functioon use TDES_ECB_PKCS7 - 1:Encryption functioon use AES_ECB_PKCS7 - - bEncType IN 1: SCardTransmit: Plaintext in Ciphertext out - 0: SCardTransmit: Ciphertext in Ciphertext out - - Description: - Set the encryption mode of iR301 for dukpt. - - */ - LONG FtDukptSetEncMod(SCARDHANDLE hCard, - unsigned int bEncrypt, - unsigned int bEncFunc, - unsigned int bEncType); - - - - - /* - Function: FtDukptGetKSN - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - pnlength INOUT IN: The size of ksn buffer(>=10) - OUT: The real size(if successful has been 10) - buffer OUT Buffer of ksn - - Description: - Get Ksn from iR301 for dukpt. - - */ - - LONG FtDukptGetKSN(SCARDHANDLE hCard,unsigned int * pnlength,unsigned char *buffer); - /* - Function: FtDidEnterBackground - - Parameters: - bDidEnter IN must be set 1 - - - Description: - Use this method to release monitor thread of reader status - - */ - void FtDidEnterBackground(unsigned int bDidEnter); - /* - Function: FtSle4442Cmd - - Parameters: - hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) - pbCmd IN SLE4442 cmd(3 byte) which are listed in sle4442 cmd table - bLengthToRead IN The data length which reading from the card - bIsClockNum IN If bIsClockNum = 1, then bToReadLength should be '1' and that means the returned one byte is - the clock number for card completing the command. - If bIsClockNum = 0, firmware don't care it. - pbRecvBuffer OUT Return data - pcbRecvLength INOUT IN:length of pbRecvBuffer OUT: length of return data - - Description: - This function userd to transmit data with SLE4442 card - */ - - LONG FtSle4442Cmd(SCARDHANDLE hCard,LPCBYTE pbCmd,DWORD bLengthToRead,BYTE bIsClockNum,LPBYTE pbRecvBuffer,LPDWORD pcbRecvLength); - LONG FtGetDevVer( SCARDCONTEXT hContext,char *firmwareRevision,char *hardwareRevision); - /* - Function: FtGetLibVersion - - Parameters: - buffer :buffer of libVersion - - - Description: - Get the Current Lib Version - - */ - void FtGetLibVersion (char *buffer); - -#ifdef __cplusplus -} -#endif - -#endif - + /* + * Support for bR301(Bluetooth) smart card reader + * + * ft301u.h: header file for ft_ccid.c + * + * Copyright (C) Feitian 2014, Ben + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** + * @file + * @This isfeitian's private cmd. + */ + +#ifndef __ft301u_h__ +#define __ft301u_h__ + + +#include "wintypes.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + LONG SCARD_CTL_CODE(unsigned int code); + /* + Function: FtGetSerialNum + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + length IN length of buffer(>=8) + buffer OUT Serial number + + Description: + This function userd to get serial number of iR301. + */ + + LONG FtGetSerialNum(SCARDHANDLE hCard, unsigned int length, + char * buffer); + /* + Function: FtWriteFlash + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + bOffset IN Offset of flash to write + blength IN The length of data + buffer IN The data for write + + Description: + This function userd to write data to flash. + */ + LONG FtWriteFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, + unsigned char buffer[]); + /* + Function: FtReadFlash + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + bOffset IN Offset of flash to write + blength IN The length of read data + buffer OUT The read data + + Description: + This function used to read data from flash. + */ + LONG FtReadFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, + unsigned char buffer[]); + + /* + Function: FtSetTimeout + + Parameters: + hContext IN Connection context to the PC/SC Resource Manager + dwTimeout IN New transmission timeout value of between 301 and card (millisecond ) + + Description: + The function New transmission timeout value of between 301 and card. + + */ + LONG FtSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout); + + + //for dukpt + /* + Function: FtDukptInit + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + encBuf IN Ciphertext use TDES_ECB_PKCS7/AES_ECB_PKCS7 (See "Key C" ) + nLen IN encBuf length(40(TDES_ECB_PKCS7 ciphertext length) 48(AES_ECB_PKCS7 ciphertext length)) + + Description: + Init iR301 new ipek and ksn for dukpt. + + */ + LONG FtDukptInit(SCARDHANDLE hCard,unsigned char *encBuf,unsigned int nLen); + + + /* + Function: FtDukptSetEncMod + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + bEncrypt IN 1: SCardTransmit Encrypted + 0:SCardTransmit not Encrypted + + bEncFunc IN 0: Encryption functioon use TDES_ECB_PKCS7 + 1:Encryption functioon use AES_ECB_PKCS7 + + bEncType IN 1: SCardTransmit: Plaintext in Ciphertext out + 0: SCardTransmit: Ciphertext in Ciphertext out + + Description: + Set the encryption mode of iR301 for dukpt. + + */ + LONG FtDukptSetEncMod(SCARDHANDLE hCard, + unsigned int bEncrypt, + unsigned int bEncFunc, + unsigned int bEncType); + + + + + /* + Function: FtDukptGetKSN + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + pnlength INOUT IN: The size of ksn buffer(>=10) + OUT: The real size(if successful has been 10) + buffer OUT Buffer of ksn + + Description: + Get Ksn from iR301 for dukpt. + + */ + + LONG FtDukptGetKSN(SCARDHANDLE hCard,unsigned int * pnlength,unsigned char *buffer); + /* + Function: FtDidEnterBackground + + Parameters: + bDidEnter IN must be set 1 + + + Description: + Use this method to release monitor thread of reader status + + */ + void FtDidEnterBackground(unsigned int bDidEnter); + /* + Function: FtSle4442Cmd + + Parameters: + hCard IN Connection made from SCardConnect(Ignore this parameter and just set to zero in iOS system) + pbCmd IN SLE4442 cmd(3 byte) which are listed in sle4442 cmd table + bLengthToRead IN The data length which reading from the card + bIsClockNum IN If bIsClockNum = 1, then bToReadLength should be '1' and that means the returned one byte is + the clock number for card completing the command. + If bIsClockNum = 0, firmware don't care it. + pbRecvBuffer OUT Return data + pcbRecvLength INOUT IN:length of pbRecvBuffer OUT: length of return data + + Description: + This function userd to transmit data with SLE4442 card + */ + + LONG FtSle4442Cmd(SCARDHANDLE hCard,LPCBYTE pbCmd,DWORD bLengthToRead,BYTE bIsClockNum,LPBYTE pbRecvBuffer,LPDWORD pcbRecvLength); + LONG FtGetDevVer( SCARDCONTEXT hContext,char *firmwareRevision,char *hardwareRevision); + /* + Function: FtGetLibVersion + + Parameters: + buffer :buffer of libVersion + + + Description: + Get the Current Lib Version + + */ + void FtGetLibVersion (char *buffer); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/libs/source/src/ft_ccid.h b/libs/source/src/ft_ccid.h index 736155d..4a358e2 100755 --- a/libs/source/src/ft_ccid.h +++ b/libs/source/src/ft_ccid.h @@ -36,36 +36,37 @@ extern "C" { #endif - typedef long RESPONSECODE; +typedef long RESPONSECODE; #define CCID_DRIVER_MAX_READERS 1 - typedef enum READERTYPE { - READER_UNKOWN = 0, - READER_bR301, - READER_iR301U_DOCK, - READER_iR301U_LIGHTING - - } READERTYPE; - - typedef enum FTREADER_INTERNAL { - FT_READER_DEFAULT = 0x00, - FT_READER_UA = 0x01, - FT_READER_UB = 0x02, - FT_READER_UB_LT = 0x03, - FT_READER_UC = 0x04, - FT_READER_UC_LT = 0x05, - FT_READER_UC_B = 0x06, - FT_READER_UC_LT_B = 0x07, - FT_READER_UM = 0x08, - FT_READER_UD = 0x09, - FT_READER_UD_LT = 0x0A, - - } FTREADER_INTERNAL; +typedef enum READERTYPE{ + READER_UNKOWN = 0, + READER_bR301, + READER_iR301U_DOCK, + READER_iR301U_LIGHTING + +}READERTYPE; + +typedef enum FTREADER_INTERNAL +{ + FT_READER_DEFAULT = 0x00, + FT_READER_UA = 0x01, + FT_READER_UB = 0x02, + FT_READER_UB_LT = 0x03, + FT_READER_UC = 0x04, + FT_READER_UC_LT = 0x05, + FT_READER_UC_B = 0x06, + FT_READER_UC_LT_B = 0x07, + FT_READER_UM = 0x08, + FT_READER_UD = 0x09, + FT_READER_UD_LT = 0x0A, + +}FTREADER_INTERNAL; #define ReadPort ReadSerial #define WritePort WriteSerial - + //#ifndef BOOL //#define BOOL signed char //#endif @@ -73,28 +74,28 @@ extern "C" { //#ifndef TRUE //#define TRUE 1 //#endif -// +// //#ifndef FALSE //#define FALSE 0 //#endif + - - /* Protocols */ +/* Protocols */ #define T_0 0 #define T_1 1 #define T_RAW 3 - /* Default communication read timeout in milliseconds */ +/* Default communication read timeout in milliseconds */ -#define DEFAULT_COM_READ_TIMEOUT (6000) +#define DEFAULT_COM_READ_TIMEOUT (6000) - /* bInterfaceProtocol for ICCD */ +/* bInterfaceProtocol for ICCD */ #define PROTOCOL_CCID 0 /* plain CCID */ #define PROTOCOL_ICCD_A 1 /* ICCD Version A */ #define PROTOCOL_ICCD_B 2 /* ICCD Version B */ - /* Features from dwFeatures */ +/* Features from dwFeatures */ #define CCID_CLASS_AUTO_CONF_ATR 0x00000002 #define CCID_CLASS_AUTO_VOLTAGE 0x00000008 #define CCID_CLASS_AUTO_BAUD 0x00000020 @@ -107,7 +108,7 @@ extern "C" { #define CCID_CLASS_EXTENDED_APDU 0x00040000 #define CCID_CLASS_EXCHANGE_MASK 0x00070000 - /* Features from bPINSupport */ +/* Features from bPINSupport */ #define CCID_CLASS_PIN_VERIFY 0x01 #define CCID_CLASS_PIN_MODIFY 0x02 @@ -116,117 +117,118 @@ extern "C" { #define MAX_BUFFER_SIZE_ENC 1024 #define CMD_BUF_SIZE MAX_BUFFER_SIZE_EXTENDED #define CMD_BUF_SIZE_EACH (MAX_BUFFER_SIZE_ENC+10) + +typedef enum { + STATUS_NO_SUCH_DEVICE = 0xF9, + STATUS_SUCCESS = 0xFA, + STATUS_UNSUCCESSFUL = 0xFB, + STATUS_COMM_ERROR = 0xFC, + STATUS_DEVICE_PROTOCOL_ERROR = 0xFD, + STATUS_COMM_NAK = 0xFE, + STATUS_SECONDARY_SLOT = 0xFF +} status_t; + +typedef struct +{ + /* + * CCID Sequence number + */ + unsigned char real_bSeq; + + /* + * VendorID << 16 + ProductID + */ + int readerID; + + /* + * Maximum message length + */ + unsigned int dwMaxCCIDMessageLength; + + /* + * Maximum IFSD + */ + int dwMaxIFSD; + + /* + * Features supported by the reader (directly from Class Descriptor) + */ + int dwFeatures; + + /* + * PIN support of the reader (directly from Class Descriptor) + */ + char bPINSupport; + + /* + * Display dimensions of the reader (directly from Class Descriptor) + */ + unsigned int wLcdLayout; + + /* + * Default Clock + */ + int dwDefaultClock; + + /* + * Max Data Rate + */ + unsigned int dwMaxDataRate; + + /* + * Number of available slots + */ + char bMaxSlotIndex; + + /* + * Slot in use + */ + char bCurrentSlotIndex; + + /* + * The array of data rates supported by the reader + */ + unsigned int *arrayOfSupportedDataRates; + + /* + * Read communication port timeout + * value is milliseconds + * this value can evolve dynamically if card request it (time processing). + */ + unsigned int readTimeout; + + /* + * Card protocol + */ + int cardProtocol; + + /* + * bInterfaceProtocol (CCID, ICCD-A, ICCD-B) + */ + int bInterfaceProtocol; + + + /* + * GemCore SIM PRO slot status management + * The reader always reports a card present even if no card is inserted. + * If the Power Up fails the driver will report IFD_ICC_NOT_PRESENT instead + * of IFD_ICC_PRESENT + */ + int dwSlotStatus; + + /* + * bVoltageSupport (bit field) + * 1 = 5.0V + * 2 = 3.0V + * 4 = 1.8V + */ + int bVoltageSupport; - typedef enum { - STATUS_NO_SUCH_DEVICE = 0xF9, - STATUS_SUCCESS = 0xFA, - STATUS_UNSUCCESSFUL = 0xFB, - STATUS_COMM_ERROR = 0xFC, - STATUS_DEVICE_PROTOCOL_ERROR = 0xFD, - STATUS_COMM_NAK = 0xFE, - STATUS_SECONDARY_SLOT = 0xFF - } status_t; - - typedef struct { - /* - * CCID Sequence number - */ - unsigned char real_bSeq; - - /* - * VendorID << 16 + ProductID - */ - int readerID; - - /* - * Maximum message length - */ - unsigned int dwMaxCCIDMessageLength; - - /* - * Maximum IFSD - */ - int dwMaxIFSD; - - /* - * Features supported by the reader (directly from Class Descriptor) - */ - int dwFeatures; - - /* - * PIN support of the reader (directly from Class Descriptor) - */ - char bPINSupport; - - /* - * Display dimensions of the reader (directly from Class Descriptor) - */ - unsigned int wLcdLayout; - - /* - * Default Clock - */ - int dwDefaultClock; - - /* - * Max Data Rate - */ - unsigned int dwMaxDataRate; - - /* - * Number of available slots - */ - char bMaxSlotIndex; - - /* - * Slot in use - */ - char bCurrentSlotIndex; - - /* - * The array of data rates supported by the reader - */ - unsigned int *arrayOfSupportedDataRates; - - /* - * Read communication port timeout - * value is milliseconds - * this value can evolve dynamically if card request it (time processing). - */ - unsigned int readTimeout; - - /* - * Card protocol - */ - int cardProtocol; - - /* - * bInterfaceProtocol (CCID, ICCD-A, ICCD-B) - */ - int bInterfaceProtocol; - - - /* - * GemCore SIM PRO slot status management - * The reader always reports a card present even if no card is inserted. - * If the Power Up fails the driver will report IFD_ICC_NOT_PRESENT instead - * of IFD_ICC_PRESENT - */ - int dwSlotStatus; - - /* - * bVoltageSupport (bit field) - * 1 = 5.0V - * 2 = 3.0V - * 4 = 1.8V - */ - int bVoltageSupport; - - - } _ccid_descriptor; - - - /* See CCID specs ch. 4.2.1 */ + +} _ccid_descriptor; + + +/* See CCID specs ch. 4.2.1 */ #define CCID_ICC_PRESENT_ACTIVE 0x00 /* 00 0000 00 */ #define CCID_ICC_PRESENT_INACTIVE 0x01 /* 00 0000 01 */ #define CCID_ICC_ABSENT 0x02 /* 00 0000 10 */ @@ -241,16 +243,16 @@ extern "C" { #define SIZE_GET_SLOT_STATUS 10 - /* convert a 4 byte integer in USB format into an int */ +/* convert a 4 byte integer in USB format into an int */ #define dw2i(a, x) (unsigned int)((((((a[x+3] << 8) + a[x+2]) << 8) + a[x+1]) << 8) + a[x]) #define IFD_PARITY_ERROR 699 ///////////////////////////////////////// - /* - * List of defines available to ifdhandler - */ +/* + * List of defines available to ifdhandler + */ #define IFD_SUCCESS 0 /**< no error */ #define IFD_NEGOTIATE_PTS1 1 /**< negotiate PTS1 */ #define IFD_NEGOTIATE_PTS2 2 /**< negotiate PTS2 */ @@ -274,11 +276,11 @@ extern "C" { #define IFD_NOT_SUPPORTED 614 /**< request is not supported */ #define IFD_ICC_PRESENT 615 /**< card is present */ #define IFD_ICC_NOT_PRESENT 616 /**< card is absent */ - /** - * The \ref IFD_NO_SUCH_DEVICE error must be returned by the driver when - * it detects the reader is no more present. This will tell pcscd to - * remove the reader from the list of available readers. - */ +/** + * The \ref IFD_NO_SUCH_DEVICE error must be returned by the driver when + * it detects the reader is no more present. This will tell pcscd to + * remove the reader from the list of available readers. + */ #define IFD_NO_SUCH_DEVICE 617 #define IFD_ERROR_INSUFFICIENT_BUFFER 618 /**< buffer is too small */ @@ -287,9 +289,9 @@ extern "C" { ////////////////////////////////////////////////////////////////////////// //pps // - /* - * Exported constants definition - */ +/* +* Exported constants definition +*/ #define PPS_OK 0 /* Negotiation OK */ #define PPS_ICC_ERROR 1 /* Comunication error */ @@ -303,17 +305,17 @@ extern "C" { ////////////////////////////////////////////////////////////////////////// //atr -// +// - /* Return values */ +/* Return values */ #define ATR_OK 0 /* ATR could be parsed and data returned */ #define ATR_NOT_FOUND 1 /* Data not present in ATR */ #define ATR_MALFORMED 2 /* ATR could not be parsed */ #define ATR_IO_ERROR 3 /* I/O stream error */ - - /* Paramenters */ + +/* Paramenters */ #define ATR_MAX_SIZE 33 /* Maximum size of ATR byte array */ #define ATR_MAX_HISTORICAL 15 /* Maximum number of historical bytes */ #define ATR_MAX_PROTOCOLS 7 /* Maximun number of protocols */ @@ -343,14 +345,14 @@ extern "C" { #define DUKPT_ENC_OFF 0x00 //default #define DUKPT_ENC_ON 0x01 - + #define DUKPT_ENC_FUNC_3DES 0x00 //default #define DUKPT_ENC_FUNC_AES 0x01 - + #define DUKPT_ENC_TYPE_DIR 0x00 //default #define DUKPT_ENC_TYPE_UNDIR 0x01 - - /* Default values for paramenters */ + +/* Default values for paramenters */ #define ATR_DEFAULT_D 1 #define ATR_DEFAULT_N 0 @@ -358,67 +360,70 @@ extern "C" { #define ATR_DEFAULT_I 50 #define ATR_DEFAULT_F 372 - /* - * Exported data types definition - */ +/* + * Exported data types definition + */ - typedef struct { - unsigned length; - BYTE TS; - BYTE T0; - struct { - BYTE value; - bool present; - } - ib[ATR_MAX_PROTOCOLS][ATR_MAX_IB], TCK; - unsigned pn; - BYTE hb[ATR_MAX_HISTORICAL]; - unsigned hbn; - } ATR_t; +typedef struct +{ + unsigned length; + BYTE TS; + BYTE T0; + struct + { + BYTE value; + bool present; + } + ib[ATR_MAX_PROTOCOLS][ATR_MAX_IB], TCK; + unsigned pn; + BYTE hb[ATR_MAX_HISTORICAL]; + unsigned hbn; +} ATR_t; ////////////////////////////////////////////////////////////////////////// - typedef struct CCID_DESC { - /* - * ATR - */ - int nATRLength; - UCHAR pcATRBuffer[MAX_ATR_SIZE]; - - /* - * Card state - */ - UCHAR bPowerFlags; - - /* - * T=1 Protocol context - */ - t1_state_t t1; - - /* reader name passed to IFDHCreateChannelByName() */ - // char *readerName; - } CcidDesc; - - /* Powerflag (used to detect quick insertion removals unnoticed by the - * resource manager) */ - /* Initial value */ -#define POWERFLAGS_RAZ 0x00 - /* Flag set when a power up has been requested */ -#define MASK_POWERFLAGS_PUP 0x01 - /* Flag set when a power down is requested */ -#define MASK_POWERFLAGS_PDWN 0x02 +typedef struct CCID_DESC +{ + /* + * ATR + */ + int nATRLength; + UCHAR pcATRBuffer[MAX_ATR_SIZE]; /* - * Possible values : - * 3 -> 1.8V, 3V, 5V - * 2 -> 3V, 5V - * 1 -> 5V only - * 0 -> automatic (selection made by the reader) + * Card state */ + UCHAR bPowerFlags; + /* - * To be safe we default to 5V - * otherwise we would have to parse the ATR and get the value of TAi (i>2) when - * in T=15 + * T=1 Protocol context */ + t1_state_t t1; + + /* reader name passed to IFDHCreateChannelByName() */ + // char *readerName; +} CcidDesc; + +/* Powerflag (used to detect quick insertion removals unnoticed by the + * resource manager) */ +/* Initial value */ +#define POWERFLAGS_RAZ 0x00 +/* Flag set when a power up has been requested */ +#define MASK_POWERFLAGS_PUP 0x01 +/* Flag set when a power down is requested */ +#define MASK_POWERFLAGS_PDWN 0x02 + +/* + * Possible values : + * 3 -> 1.8V, 3V, 5V + * 2 -> 3V, 5V + * 1 -> 5V only + * 0 -> automatic (selection made by the reader) + */ +/* + * To be safe we default to 5V + * otherwise we would have to parse the ATR and get the value of TAi (i>2) when + * in T=15 + */ #define VOLTAGE_AUTO 0 #define VOLTAGE_5V 1 @@ -427,86 +432,86 @@ extern "C" { //////////////////////////////////////////////////////////////////////////// - extern RESPONSECODE CCID_Transmit(unsigned int reader_index, unsigned int tx_length, - const unsigned char tx_buffer[], unsigned short rx_length, unsigned char bBWI); - extern RESPONSECODE CCID_Receive(unsigned int reader_index, unsigned int *rx_length, - unsigned char rx_buffer[], unsigned char *chain_parameter); - extern int isCharLevel(int reader_index); - - extern RESPONSECODE CmdPowerOn(unsigned int reader_index, unsigned int * nlength, - unsigned char buffer[], int voltage); - - extern RESPONSECODE CmdPowerOff(unsigned int reader_index); - - extern RESPONSECODE SetParameters(unsigned int reader_index, char protocol, - unsigned int length, unsigned char buffer[]); - extern _ccid_descriptor *get_ccid_descriptor(unsigned int reader_index); - - extern CcidDesc *get_ccid_slot(unsigned int reader_index); - - extern RESPONSECODE CmdXfrBlock(unsigned int reader_index, unsigned int tx_length, - unsigned char tx_buffer[], unsigned int *rx_length, - unsigned char rx_buffer[], int protocol); - - extern int ATR_InitFromArray (ATR_t * atr, const BYTE atr_buffer[ATR_MAX_SIZE], unsigned length); - - extern int ATR_GetIntegerValue (ATR_t * atr, int name, BYTE * value); - - extern int ATR_GetDefaultProtocol(ATR_t * atr, int *protocol); - - extern int ATR_GetConvention (ATR_t * atr, int *convention); - - extern int ATR_GetParameter (ATR_t * atr, int name, double *parameter); - - extern RESPONSECODE Scrd_Negotiate(unsigned int reader_index); - - extern RESPONSECODE CmdGetSlotStatus(unsigned int reader_index, unsigned char buffer[]); - - extern int isbadreadptr(void *ptr, int length); - - extern RESPONSECODE CmdGetSerialNum(unsigned int reader_index, unsigned int * pnlength, - unsigned char buffer[]); - extern RESPONSECODE CmdWriteFlash(unsigned int reader_index,unsigned char bOffset, unsigned char blength, - unsigned char buffer[]); - extern RESPONSECODE CmdReadFlash(unsigned int reader_index, unsigned char bOffset, unsigned char blength, - unsigned char buffer[]); - extern RESPONSECODE CmdGetDevInfo(unsigned int reader_index, unsigned int * pnlength, - unsigned char buffer[]); - extern RESPONSECODE CmdGetDevVer(unsigned int reader_index,char *firmwareRevision,char *hardwareRevision); - - extern RESPONSECODE CmdWriteIKSN2IPEKFlash(unsigned int reader_index,unsigned char *encBuf,unsigned int nLen); - - extern RESPONSECODE CmdSetEncMod(unsigned int reader_index,unsigned int bEncrypt,unsigned int bEncFunc, - unsigned int bEncType); - - extern RESPONSECODE CmdGetKSN(unsigned int reader_index, unsigned int * pnlength, - unsigned char buffer[]); - - extern RESPONSECODE CmdEscape(unsigned int reader_index, - const unsigned char TxBuffer[], unsigned int TxLength, - unsigned char RxBuffer[], unsigned int *RxLength); +extern RESPONSECODE CCID_Transmit(unsigned int reader_index, unsigned int tx_length, + const unsigned char tx_buffer[], unsigned short rx_length, unsigned char bBWI); +extern RESPONSECODE CCID_Receive(unsigned int reader_index, unsigned int *rx_length, + unsigned char rx_buffer[], unsigned char *chain_parameter); +extern int isCharLevel(int reader_index); + +extern RESPONSECODE CmdPowerOn(unsigned int reader_index, unsigned int * nlength, + unsigned char buffer[], int voltage); + +extern RESPONSECODE CmdPowerOff(unsigned int reader_index); + +extern RESPONSECODE SetParameters(unsigned int reader_index, char protocol, + unsigned int length, unsigned char buffer[]); +extern _ccid_descriptor *get_ccid_descriptor(unsigned int reader_index); + +extern CcidDesc *get_ccid_slot(unsigned int reader_index); + +extern RESPONSECODE CmdXfrBlock(unsigned int reader_index, unsigned int tx_length, + unsigned char tx_buffer[], unsigned int *rx_length, + unsigned char rx_buffer[], int protocol); + +extern int ATR_InitFromArray (ATR_t * atr, const BYTE atr_buffer[ATR_MAX_SIZE], unsigned length); + +extern int ATR_GetIntegerValue (ATR_t * atr, int name, BYTE * value); + +extern int ATR_GetDefaultProtocol(ATR_t * atr, int *protocol); + +extern int ATR_GetConvention (ATR_t * atr, int *convention); + +extern int ATR_GetParameter (ATR_t * atr, int name, double *parameter); + +extern RESPONSECODE Scrd_Negotiate(unsigned int reader_index); + +extern RESPONSECODE CmdGetSlotStatus(unsigned int reader_index, unsigned char buffer[]); + +extern int isbadreadptr(void *ptr, int length); + +extern RESPONSECODE CmdGetSerialNum(unsigned int reader_index, unsigned int * pnlength, + unsigned char buffer[]); +extern RESPONSECODE CmdWriteFlash(unsigned int reader_index,unsigned char bOffset, unsigned char blength, + unsigned char buffer[]); +extern RESPONSECODE CmdReadFlash(unsigned int reader_index, unsigned char bOffset, unsigned char blength, + unsigned char buffer[]); +extern RESPONSECODE CmdGetDevInfo(unsigned int reader_index, unsigned int * pnlength, + unsigned char buffer[]); +extern RESPONSECODE CmdGetDevVer(unsigned int reader_index,char *firmwareRevision,char *hardwareRevision); + +extern RESPONSECODE CmdWriteIKSN2IPEKFlash(unsigned int reader_index,unsigned char *encBuf,unsigned int nLen); + +extern RESPONSECODE CmdSetEncMod(unsigned int reader_index,unsigned int bEncrypt,unsigned int bEncFunc, + unsigned int bEncType); + +extern RESPONSECODE CmdGetKSN(unsigned int reader_index, unsigned int * pnlength, + unsigned char buffer[]); + +extern RESPONSECODE CmdEscape(unsigned int reader_index, + const unsigned char TxBuffer[], unsigned int TxLength, + unsigned char RxBuffer[], unsigned int *RxLength); //////////////////////////////////////////////////////////////////////////// - extern bool gIsOpen; - extern unsigned int gDevType; - extern unsigned int gIsReadData ; - extern unsigned int iR301_or_bR301; - extern unsigned int isDukpt; - extern void log_msg( const char *fmt, ...); - extern void Dpt( const char *fmt, ...); - extern void ccid_error(int error, const char *file, int line, const char *function); - - +extern bool gIsOpen; +extern unsigned int gDevType; +extern unsigned int gIsReadData ; +extern unsigned int iR301_or_bR301; +extern unsigned int isDukpt; +extern void log_msg( const char *fmt, ...); +extern void Dpt( const char *fmt, ...); +extern void ccid_error(int error, const char *file, int line, const char *function); + + #ifdef __APPLE__ #pragma pack(1) #endif - - /* restore default structure elements alignment */ + +/* restore default structure elements alignment */ #ifdef __APPLE__ #pragma pack() #endif #ifdef __cplusplus -} + } #endif -#endif //__HZ_CCID_H__ +#endif //__H \ No newline at end of file diff --git a/libs/source/src/ft_ccid_cmd.m b/libs/source/src/ft_ccid_cmd.m index d0c4525..0a47eca 100755 --- a/libs/source/src/ft_ccid_cmd.m +++ b/libs/source/src/ft_ccid_cmd.m @@ -1,1592 +1,1625 @@ -/* - * Support for bR301(Bluetooth) smart card reader - * - * ft_ccid_cmd.m: The code has moved from CCID Driver, which is open souce code by GNU license (http://pcsclite.alioth.debian.org/ccid.html) - * Some parts has integrated private command, these command only using for Feitian bluetooth and iDock reader,like ComdGetSerialNum,CmdGetSerialNum,CmdGetDevInfo,CmdWriteFlash,CmdReadFlash,CmdWriteIKSN2IPEKFlash,CmdGetKSN,CmdSetEncMod - * - * And the code was apply commands.c file which is GNU CCID open source code - * - * $Id: commands.c 6879 2014-03-23 15:11:20Z rousseau $ - * - * The file has packaged PC/SC API by Objective-C Lang, user can called Objective-C interface to monitor - * card and reader event - * Copyright (C) Feitian 2014, Ben - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - - - -#include "ft_ccid.h" -#include "wintypes.h" -#include "proto-t1.h" -#include - -#import "bR301SessionController.h" - -#define FT_IR301_DEBUG - -static _ccid_descriptor s_ccidDevice[CCID_DRIVER_MAX_READERS]= {0}; - -static CcidDesc s_CcidSlots[CCID_DRIVER_MAX_READERS]= {0}; - -#pragma mark ccid tool func -/***************************************************************************** - * - * i2dw - * - ****************************************************************************/ - -static void i2dw(int value, unsigned char buffer[]) -{ - buffer[0] = value & 0xFF; - buffer[1] = (value >> 8) & 0xFF; - buffer[2] = (value >> 16) & 0xFF; - buffer[3] = (value >> 24) & 0xFF; -} /* i2dw */ - -/***************************************************************************** -* -* bei2i (big endian integer to host order interger) -* -****************************************************************************/ - -static unsigned int bei2i(unsigned char buffer[]) -{ - return (buffer[0]<<24) + (buffer[1]<<16) + (buffer[2]<<8) + buffer[3]; -} - -/***************************************************************************** - * - * LOCK - * - ****************************************************************************/ - -//int ftLock(int condition) -//{ -// -// EADSessionController *sessionController = [EADSessionController sharedController]; -// return [sessionController myLock:condition]; -//} - - -/***************************************************************************** - * - * UNLOCK - * - ****************************************************************************/ - -//int ftUnLock(int condition) -//{ -// -// EADSessionController *sessionController = [EADSessionController sharedController]; -// return [sessionController myUnLock:condition]; -//} - - -/***************************************************************************** - * - * get_ccid_descriptor - * - ****************************************************************************/ -_ccid_descriptor *get_ccid_descriptor(unsigned int reader_index) -{ - return &s_ccidDevice[0]; -} /* get_ccid_descriptor */ - - -CcidDesc *get_ccid_slot(unsigned int reader_index) -{ - return &s_CcidSlots[0]; -} /* get_ccid_slot */ - - -/***************************************************************************** - * - * ReadSerial: Receive bytes from the card reader - * - *****************************************************************************/ -status_t ReadSerial(unsigned int reader_index, - unsigned int *length, unsigned char *buffer) -{ - @try { - - int iRet=0; - - bR301SessionController *sessionController = [bR301SessionController sharedController]; - gIsReadData = 1; -iRet = [sessionController readData: buffer withbytesToRead:length]; - gIsReadData = 0; - if(iRet != 0) { -#ifdef FT_IR301_DEBUG - //NSLog(@"==========read error\n"); -#endif - return STATUS_COMM_ERROR; - } else { -#ifdef FT_IR301_DEBUG - NSMutableData *tmpData = [NSMutableData data]; -[tmpData appendBytes:buffer length:*length]; - -NSString* dataString= [NSString stringWithFormat:@"%@",tmpData]; -NSRange begin = [dataString rangeOfString:@"<"]; -NSRange end = [dataString rangeOfString:@">"]; - NSRange range = NSMakeRange(begin.location + begin.length, end.location- begin.location - 1); -dataString = [dataString substringWithRange:range]; - NSLog(@"==========read:\n%@",dataString); -#endif - return STATUS_SUCCESS; - } - } - @catch (...) { - - return STATUS_COMM_ERROR; - } - @finally { - - } - -} - -/***************************************************************************** - * - * WriteSerial: Send bytes to the card reader - * - *****************************************************************************/ -status_t WriteSerial(unsigned int reader_index, unsigned int length, - unsigned char *buffer) -{ - @try { - int iRet=0; - - bR301SessionController *sessionController = [bR301SessionController sharedController]; - -iRet = [sessionController writeData: buffer withLength:length]; - -#ifdef FT_IR301_DEBUG - NSMutableData *tmpData = [NSMutableData data]; -[tmpData appendBytes:buffer length:length]; - -NSString* dataString= [NSString stringWithFormat:@"%@",tmpData]; -NSRange begin = [dataString rangeOfString:@"<"]; -NSRange end = [dataString rangeOfString:@">"]; - NSRange range = NSMakeRange(begin.location + begin.length, end.location- begin.location - 1); -dataString = [dataString substringWithRange:range]; - NSLog(@"==========write:\n%@",dataString); -#endif - - - if (iRet == 0 ) { - return STATUS_SUCCESS; - } else { - - return STATUS_COMM_ERROR; - } - - } - @catch (...) { - - return STATUS_COMM_ERROR; - } - @finally { - - } - -} - -#pragma mark feitian's private tool cmd - -RESPONSECODE CmdGetDevVer(unsigned int reader_index,char *firmwareRevision,char *hardwareRevision) -{ - if (gIsOpen == NO) { - return IFD_ERROR_TAG; - } - - bR301SessionController *sessionController = [bR301SessionController sharedController]; - NSString *tmp=[[sessionController accessory] firmwareRevision]; - if (iR301_or_bR301 == 0) - { - if (tmp != nil) { - strcpy(firmwareRevision,[tmp UTF8String]); - } - else{ - return IFD_ERROR_TAG; - } - } - - tmp=[[sessionController accessory] hardwareRevision]; - if (tmp != nil) { - strcpy(hardwareRevision,[tmp UTF8String]); - } - else{ - return IFD_ERROR_TAG; - } - - - if (iR301_or_bR301 == 1) - { - unsigned char cmd[10+6]={0}; - unsigned char tmp[10+32]={0}; - status_t res; - unsigned int length = 0; - - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = 0x04; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - cmd[10] = 0x5A; - cmd[11] = 0xA5; - cmd[12] = 0x20; - cmd[13] = 0x00; - - res = WritePort(reader_index, 10+4, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length = sizeof(tmp); - res = ReadPort(reader_index, &length, tmp); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) - { - return IFD_COMMUNICATION_ERROR; - } - - if(length < 10+4) - { - - return IFD_COMMUNICATION_ERROR; - } - - sprintf(firmwareRevision, "%x.%02x",tmp[10],tmp[11]); - - } - - return IFD_SUCCESS; - - return IFD_SUCCESS; -} - -/***************************************************************************** -* -* CmdGetSerialNum this is a feitian's private cmd -* -****************************************************************************/ -RESPONSECODE CmdGetSerialNum(unsigned int reader_index, unsigned int * pnlength, - unsigned char buffer[]) -{ - unsigned char cmd[10+4]= {0}; - unsigned char tmp[10+300]= {0}; - status_t res; - unsigned int length = 0; - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = 0x04; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - cmd[10] = 0x5A; - cmd[11] = 0xA5; - cmd[12] = 0x31; - cmd[13] = 0x31; - - - res = WritePort(reader_index, 10+4, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length = *pnlength; - - res = ReadPort(reader_index, &length, tmp); - - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) { - return IFD_COMMUNICATION_ERROR; - } - - if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) { - ccid_error(tmp[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - - return IFD_COMMUNICATION_ERROR; - - } - - if(length < 10+ 16) { - - return IFD_COMMUNICATION_ERROR; - } - - *pnlength = 16; - memcpy(buffer, tmp+10, 16); - - return return_value; -} /* CmdGetSerialNum */ - - -/***************************************************************************** - * - * CmdGetDevInfo this is a feitian's private cmd - * - ****************************************************************************/ -RESPONSECODE CmdGetDevInfo(unsigned int reader_index, unsigned int * pnlength, - unsigned char buffer[]) -{ - unsigned char cmd[10+4]= {0}; - unsigned char tmp[10+300]= {0}; - status_t res; - unsigned int length = 0; - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = 0x02; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - cmd[10] = 0x5A; - cmd[11] = 0xA1; - - - res = WritePort(reader_index, 10+2, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length = *pnlength; - - res = ReadPort(reader_index, &length, tmp); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) { - return IFD_COMMUNICATION_ERROR; - } - - if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) { - - ccid_error(tmp[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - - return IFD_COMMUNICATION_ERROR; - - } - - if(length < 10+ 10) { - - return IFD_COMMUNICATION_ERROR; - } - - *pnlength = 10; - memcpy(buffer, tmp+10, 10); - - return return_value; -} /* CmdGetDevInfo */ - -/***************************************************************************** - * - * CmdWriteFlash this is a feitian's private cmd - * - ****************************************************************************/ -RESPONSECODE CmdWriteFlash(unsigned int reader_index,unsigned char bOffset, unsigned char blength,unsigned char buffer[]) -{ - unsigned char cmd[10+4]= {0}; - unsigned char tmp[10+300]= {0}; - status_t res; - unsigned int length=sizeof(cmd); - - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - //first read flash - if (bOffset == 0 && blength == 255) { - memcpy(tmp+10+bOffset,buffer,blength); - } else { - return_value = CmdReadFlash( reader_index,0,255,tmp+10); - - if (IFD_SUCCESS != return_value) { - return return_value; - } - memcpy(tmp+10+bOffset,buffer,blength); - - bOffset = 0; - blength = 255; - - } - - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = 0x04; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - cmd[10] = 0x5A; - cmd[11] = 0xA6; - cmd[12] = blength; - cmd[13] = bOffset; - - - res = WritePort(reader_index, 10+4, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - res = ReadPort(reader_index, &length, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) { - return IFD_COMMUNICATION_ERROR; - } - - if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) { - ccid_error(buffer[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - - return IFD_COMMUNICATION_ERROR; - - } - - //now write flash data - - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = blength; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - - memcpy(tmp, cmd, 10); - - res = WritePort(reader_index,blength+10, tmp); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length=sizeof(tmp); - res = ReadPort(reader_index, &length, tmp); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) { - return IFD_COMMUNICATION_ERROR; - } - - if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) { - ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - return IFD_COMMUNICATION_ERROR; - - } - - return return_value; -} /* CmdWriteFlash */ - - - -/***************************************************************************** - * - * CmdReadFlash this is a feitian's private cmd - * - ****************************************************************************/ -RESPONSECODE CmdReadFlash(unsigned int reader_index, unsigned char bOffset, unsigned char blength,unsigned char buffer[]) -{ - unsigned char cmd[10+300]= {0}; - status_t res; - unsigned int length=sizeof(cmd); - - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = 0x04; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = 0; - cmd[8] = cmd[9] = 0; /* RFU */ - cmd[10] = 0x5A; - cmd[11] = 0xA7; - cmd[12] = blength; - cmd[13] = bOffset; - - - res = WritePort(reader_index, 10+4, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - - res = ReadPort(reader_index, &length, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) { - return IFD_COMMUNICATION_ERROR; - } - - if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) { - ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - - return IFD_COMMUNICATION_ERROR; - - } - - if(length < 10+ blength) { - return IFD_COMMUNICATION_ERROR; - } - - - memcpy(buffer, cmd+10, blength); - - return return_value; -} /* CmdReadFlash */ - - - -#pragma mark feitian's private duktp cmd - -/***************************************************************************** -* -* CmdWriteIKSN2IPEKFlash this is a feitian's private cmd of dukpt -* -****************************************************************************/ -RESPONSECODE CmdWriteIKSN2IPEKFlash(unsigned int reader_index,unsigned char *encBuf,unsigned int nLen) -{ - unsigned char cmd[10+6]= {0}; - unsigned char tmp[10+64]= {0}; - status_t res; - unsigned int length; - - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = 4+nLen; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - cmd[10] = 0x5B; - cmd[11] = 0x02; - cmd[12] = 0x00; //private lenght h - cmd[13] = nLen; //private lenght l - - memcpy(tmp, cmd, 14); - memcpy(tmp+14, encBuf, nLen); - res = WritePort(reader_index,10+4+nLen, tmp); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length=sizeof(tmp); - res = ReadPort(reader_index, &length, tmp); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) { - return IFD_COMMUNICATION_ERROR; - } - - /*if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - return IFD_COMMUNICATION_ERROR; - - }*/ - if(length < 10+ 6) { - return IFD_COMMUNICATION_ERROR; - } - - // Can not judge -#if 0 - if (tmp[10] != 0xB5 || - tmp[11] != 0x02 || - tmp[12] != 0x00 || - tmp[13] != 0x02 ) { - return IFD_COMMUNICATION_ERROR; - } -#endif - // - if (tmp[14] != 0x90 || - tmp[15] != 0x00) { - return IFD_COMMUNICATION_ERROR; - } - return return_value; -} /* CmdWriteIKSN2IPEKFlash */ - -/***************************************************************************** - * - * CmdGetKSN this is a feitian's private cmd of dukpt - * - ****************************************************************************/ -RESPONSECODE CmdGetKSN(unsigned int reader_index, unsigned int * pnlength, - unsigned char buffer[]) -{ - unsigned char cmd[10+32]= {0}; - unsigned char tmp[10+64]= {0}; - status_t res; - unsigned int length = 0; - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = 0x04; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - cmd[10] = 0x5B; - cmd[11] = 0x03; - cmd[12] = 0x00; //private lenght h - cmd[13] = 0x00; //private lenght l - - - res = WritePort(reader_index, 10+4, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length = *pnlength; - - res = ReadPort(reader_index, &length, tmp); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) { - return IFD_COMMUNICATION_ERROR; - } - - /*if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - ccid_error(buffer[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - - return IFD_COMMUNICATION_ERROR; - }*/ - - if(length < 10+ 16) { - return IFD_COMMUNICATION_ERROR; - } - // Can not judge -#if 0 - if (buffer[10] != 0xB5 || - buffer[11] != 0x03 || - buffer[12] != 0x00 || - buffer[13] != 0x0C || - buffer[24] != 0x90 || - buffer[25] != 0x00) { - return IFD_COMMUNICATION_ERROR; - } -#endif - - *pnlength = 10; - memcpy(buffer, tmp+14, 10); - - return return_value; -} /* CmdGetKSN */ - - -/***************************************************************************** - * - * CmdSetEncryptMod this is a feitian's private cmd of dukpt - * - ****************************************************************************/ -RESPONSECODE CmdSetEncMod(unsigned int reader_index, - unsigned int bEncrypt, - unsigned int bEncFunc, - unsigned int bEncType) -{ - unsigned char cmd[10+32]= {0}; - status_t res; - unsigned int length = 0; - RESPONSECODE return_value = IFD_SUCCESS; - BYTE Encrypt = (bEncrypt == DUKPT_ENC_OFF ? DUKPT_ENC_OFF: DUKPT_ENC_ON); - BYTE EncFunc = (bEncFunc == DUKPT_ENC_FUNC_3DES ? DUKPT_ENC_FUNC_3DES: DUKPT_ENC_FUNC_AES); - BYTE EncType = (bEncType == DUKPT_ENC_TYPE_DIR ? DUKPT_ENC_TYPE_DIR: DUKPT_ENC_TYPE_UNDIR); - - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - cmd[0] = 0x6B; /* feitian private cmd */ - cmd[1] = 0x07; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - cmd[10] = 0x5B; - cmd[11] = 0x01; - cmd[12] = 0x00; //private lenght h - cmd[13] = 0x03; //private lenght - cmd[14] = Encrypt; - cmd[15] = EncFunc; - cmd[16] = EncType; - - res = WritePort(reader_index, 10+7, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length = sizeof(cmd); - - res = ReadPort(reader_index, &length, cmd); - - if (res != STATUS_SUCCESS) { - - return IFD_COMMUNICATION_ERROR; - } - - if (length < STATUS_OFFSET+1) { - - return IFD_COMMUNICATION_ERROR; - } - - /*if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - - return IFD_COMMUNICATION_ERROR; - }*/ - - if (length < 10 + 9) { - - return IFD_COMMUNICATION_ERROR; - } - - - // Can not judge -#if 0 - if (cmd[10] != 0xB5 || - cmd[11] != 0x01 || - cmd[12] != 0x00 || - cmd[13] != 0x05 || - cmd[17] != 0x90 || - cmd[18] != 0x00) { - return IFD_COMMUNICATION_ERROR; - } -#endif - - if (cmd[14] != Encrypt || - cmd[15] != EncFunc || - cmd[16] != EncType ) { - return IFD_ERROR_TAG; - } - - return return_value; -} /* CmdSetEncryptMod */ - - -#pragma mark ccid's sub func - -/***************************************************************************** -* -* CmdGetSlotStatus -* -****************************************************************************/ -RESPONSECODE CmdGetSlotStatus(unsigned int reader_index, unsigned char buffer[]) -{ - unsigned char cmd[10]; - status_t res; - unsigned int length; - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - cmd[0] = 0x65; /* GetSlotStatus */ - cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - - res = WritePort(reader_index, sizeof(cmd), cmd); - if (res != STATUS_SUCCESS) - { - if (STATUS_NO_SUCH_DEVICE == res) - return IFD_NO_SUCH_DEVICE; - return IFD_COMMUNICATION_ERROR; - } - - length = SIZE_GET_SLOT_STATUS; - res = ReadPort(reader_index, &length, buffer); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) - { - return IFD_COMMUNICATION_ERROR; - } - - if (buffer[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - /* card absent or mute is not an communication error */ - if ((buffer[ERROR_OFFSET] != 0xFE)) - { - return_value = IFD_COMMUNICATION_ERROR; - } - else - { - ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; - } - } - - return return_value; -} /* CmdGetSlotStatus */ - - -/***************************************************************************** -* -* CmdPowerOff -* -****************************************************************************/ -RESPONSECODE CmdPowerOff(unsigned int reader_index) -{ - unsigned char cmd[10]; - status_t res; - unsigned int length; - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - cmd[0] = 0x63; /* IccPowerOff */ - cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ - - res = WritePort(reader_index, sizeof(cmd), cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length = sizeof(cmd); - res = ReadPort(reader_index, &length, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) - { - return IFD_COMMUNICATION_ERROR; - } - - if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - if (cmd[ERROR_OFFSET] == 0xFE) { - - ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; - } - return_value = IFD_COMMUNICATION_ERROR; - } - - return return_value; -} /* CmdPowerOff */ - -/***************************************************************************** -* -* Escape -* -****************************************************************************/ -RESPONSECODE CmdEscape(unsigned int reader_index, - const unsigned char TxBuffer[], unsigned int TxLength, - unsigned char RxBuffer[], unsigned int *RxLength) -{ - unsigned char cmd[10+300]={0}; - status_t res; - unsigned int length=sizeof(cmd); - unsigned int nLen=0; - - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - nLen = TxLength & 0xFF; - - cmd[0] = 0x6B; - cmd[1] = nLen; - cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = 0; - cmd[8] = cmd[9] = 0; /* RFU */ - memcpy(cmd+10, TxBuffer, nLen); - - res = WritePort(reader_index, 10+nLen, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - - res = ReadPort(reader_index, &length, cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) - { - return IFD_COMMUNICATION_ERROR; - } - - if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - - return IFD_COMMUNICATION_ERROR; - } - - if (length > 10) - { - nLen = length -10; - if(*RxLength < nLen) - { - return IFD_NOT_SUPPORTED; - } - memcpy(RxBuffer, cmd+10, nLen); - *RxLength=nLen; - } - else - { - *RxLength = 0; - } - - return return_value; - -} /* Escape */ - - -/***************************************************************************** -* -* Escape -* -****************************************************************************/ -RESPONSECODE CmdEscape0(unsigned int reader_index, - const unsigned char TxBuffer[], unsigned int TxLength, - unsigned char RxBuffer[], unsigned int *RxLength) -{ - unsigned char *cmd_in, *cmd_out; - status_t res; - unsigned int length_in, length_out; - RESPONSECODE return_value = IFD_SUCCESS; - int old_read_timeout; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - old_read_timeout = ccid_descriptor -> readTimeout; - ccid_descriptor -> readTimeout = 30*1000; /* 30 seconds */ - -again: - /* allocate buffers */ - length_in = 10 + TxLength; - if (NULL == (cmd_in = (BYTE*)malloc(length_in))) - { - return_value = IFD_COMMUNICATION_ERROR; - goto end; - } - - length_out = 10 + *RxLength; - if (NULL == (cmd_out = (BYTE*)malloc(length_out))) - { - free(cmd_in); - return_value = IFD_COMMUNICATION_ERROR; - goto end; - } - - cmd_in[0] = 0x6B; /* PC_to_RDR_Escape */ - i2dw(length_in - 10, cmd_in+1); /* dwLength */ - cmd_in[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd_in[6] = ccid_descriptor->real_bSeq; - cmd_in[7] = cmd_in[8] = cmd_in[9] = 0; /* RFU */ - - /* copy the command */ - memcpy(&cmd_in[10], TxBuffer, TxLength); - - res = WritePort(reader_index, length_in, cmd_in); - free(cmd_in); - if (res != STATUS_SUCCESS) - { - free(cmd_out); - return_value = IFD_COMMUNICATION_ERROR; - goto end; - } - - res = ReadPort(reader_index, &length_out, cmd_out); - - /* replay the command if NAK - * This (generally) happens only for the first command sent to the reader - * with the serial protocol so it is not really needed for all the other - * ReadPort() calls */ - if (STATUS_COMM_NAK == res) - { - free(cmd_out); - goto again; - } - - if (res != STATUS_SUCCESS) - { - free(cmd_out); - return_value = IFD_COMMUNICATION_ERROR; - goto end; - } - - if (length_out < STATUS_OFFSET+1) - { - return_value = IFD_COMMUNICATION_ERROR; - goto end; - } - - if (cmd_out[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - if (cmd_out[ERROR_OFFSET] == 0xFE) { - - ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; - } - return_value = IFD_COMMUNICATION_ERROR; - } - - /* copy the response */ - length_out = dw2i(cmd_out, 1); - if (length_out > *RxLength) - length_out = *RxLength; - *RxLength = length_out; - memcpy(RxBuffer, &cmd_out[10], length_out); - - free(cmd_out); - -end: - ccid_descriptor -> readTimeout = old_read_timeout; - return return_value; -} /* Escape */ - -/***************************************************************************** -* -* CmdPowerOn -* -****************************************************************************/ -RESPONSECODE CmdPowerOn(unsigned int reader_index, unsigned int * nlength, - unsigned char buffer[], int voltage) -{ - unsigned char cmd[10]; - status_t res; - int length=64; - unsigned int atr_len; - RESPONSECODE return_value = IFD_SUCCESS; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - cmd[0] = 0x62; /* IccPowerOn */ - cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = voltage; - cmd[8] = cmd[9] = 0; /* RFU */ - - res = WritePort(reader_index, sizeof(cmd), cmd); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - /* reset available buffer size */ - /* needed if we go back after a switch to ISO mode */ - *nlength = length; - - res = ReadPort(reader_index, nlength, buffer); - if (res != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (*nlength < STATUS_OFFSET+1) - { - return IFD_COMMUNICATION_ERROR; - } - - if (buffer[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - - // here voltage - //VOLTAGE_3V/*VOLTAGE_AUTO/*VOLTAGE_5V*/ - if (buffer[ERROR_OFFSET] == 0xFE) { - - ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; - } - return IFD_COMMUNICATION_ERROR; - - } - - /* extract the ATR */ - atr_len = dw2i(buffer, 1); /* ATR length */ - if (atr_len > *nlength) - atr_len = *nlength; - else - *nlength = atr_len; - - memmove(buffer, buffer+10, atr_len); - - return return_value; -} /* CmdPowerOn */ - - -/***************************************************************************** -* -* SetParameters -* -****************************************************************************/ -RESPONSECODE SetParameters(unsigned int reader_index, char protocol, - unsigned int length, unsigned char buffer[]) -{ - unsigned char cmd[10+CMD_BUF_SIZE_EACH]; /* CCID + APDU buffer */ - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - - cmd[0] = 0x61; /* SetParameters */ - i2dw(length, cmd+1); /* APDU length */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = protocol; /* bProtocolNum */ - cmd[8] = cmd[9] = 0; /* RFU */ - - /* check that the command is not too large */ - if (length > CMD_BUF_SIZE_EACH) - return IFD_NOT_SUPPORTED; - - memcpy(cmd+10, buffer, length); - - if (WritePort(reader_index, 10+length, cmd) != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - length = sizeof(cmd); - if (ReadPort(reader_index, &length, cmd) != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - if (length < STATUS_OFFSET+1) - { - - return IFD_COMMUNICATION_ERROR; - } - - if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); - if (0x00 == cmd[ERROR_OFFSET]) /* command not supported */ - return IFD_NOT_SUPPORTED; - else - if ((cmd[ERROR_OFFSET] >= 1) && (cmd[ERROR_OFFSET] <= 127)) - /* a parameter is not changeable */ - return IFD_SUCCESS; - else if (cmd[ERROR_OFFSET] == 0xFE) - - ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; - else - return IFD_COMMUNICATION_ERROR; - } - - return IFD_SUCCESS; -} /* SetParameters */ - - - - -/***************************************************************************** -* -* CCID_Transmit -* -****************************************************************************/ -RESPONSECODE CCID_Transmit(unsigned int reader_index, unsigned int tx_length, - const unsigned char tx_buffer[], unsigned short rx_length, unsigned char bBWI) -{ - unsigned char cmd[10+CMD_BUF_SIZE_EACH]; /* CCID + APDU buffer */ - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - status_t ret; - - cmd[0] = 0x6F; /* XfrBlock */ - i2dw(tx_length, cmd+1); /* APDU length */ - cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ - cmd[6] = ccid_descriptor->real_bSeq++; - cmd[7] = bBWI; /* extend block waiting timeout */ - cmd[8] = rx_length & 0xFF; /* Expected length, in character mode only */ - cmd[9] = (rx_length >> 8) & 0xFF; - - /* check that the command is not too large */ - if (tx_length > CMD_BUF_SIZE_EACH) - { - return IFD_NOT_SUPPORTED; - } - - //type b - if (1) { - if (NULL == tx_buffer) - rx_length = 0x10; /* bLevelParameter */ - } - if (tx_length >0 ) { - memcpy(cmd+10, tx_buffer, tx_length); - } - - - ret = WritePort(reader_index, 10+tx_length, cmd); - if (STATUS_NO_SUCH_DEVICE == ret) - return IFD_NO_SUCH_DEVICE; - if (ret != STATUS_SUCCESS) - return IFD_COMMUNICATION_ERROR; - - return IFD_SUCCESS; -} /* CCID_Transmit */ - - - -/***************************************************************************** -* -* CCID_Receive -* -****************************************************************************/ -RESPONSECODE CCID_Receive(unsigned int reader_index, unsigned int *rx_length, - unsigned char rx_buffer[], unsigned char *chain_parameter) -{ - unsigned char cmd[10+CMD_BUF_SIZE_EACH]; /* CCID + APDU buffer */ - unsigned int length; - RESPONSECODE return_value = IFD_SUCCESS; - status_t ret; - - //_ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - //type B - if(1) - { - // int r; - unsigned char rx_tmp[4]; - //unsigned char *old_rx_buffer = NULL; - //int old_rx_length = 0; - - /* read a nul block. buffer need to be at least 4-bytes */ - if (NULL == rx_buffer) - { - rx_buffer = rx_tmp; - *rx_length = sizeof(rx_tmp); - } - - } -time_request: - length = sizeof(cmd); - ret = ReadPort(reader_index, &length, cmd); - if (ret != STATUS_SUCCESS) - { - if (STATUS_NO_SUCH_DEVICE == ret) - return IFD_NO_SUCH_DEVICE; - return IFD_COMMUNICATION_ERROR; - } - - if (length < STATUS_OFFSET+1) - { - return IFD_COMMUNICATION_ERROR; - } - - if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) - { - switch (cmd[ERROR_OFFSET]) - { - case 0xEF: /* cancel */ - if (*rx_length < 2) - return IFD_COMMUNICATION_ERROR; - rx_buffer[0]= 0x64; - rx_buffer[1]= 0x01; - *rx_length = 2; - return IFD_SUCCESS; - - case 0xF0: /* timeout */ - if (*rx_length < 2) - return IFD_COMMUNICATION_ERROR; - rx_buffer[0]= 0x64; - rx_buffer[1]= 0x00; - *rx_length = 2; - return IFD_SUCCESS; - - case 0xFD: /* Parity error during exchange */ - return IFD_PARITY_ERROR; - case 0xFE: - return IFD_COMMUNICATION_ERROR; - default: - return IFD_COMMUNICATION_ERROR; - } - } - - if (cmd[STATUS_OFFSET] & CCID_TIME_EXTENSION) - { - goto time_request; - } - - /* we have read less (or more) data than the CCID frame says to contain */ - if (length-10 != dw2i(cmd, 1)) - { - return_value = IFD_COMMUNICATION_ERROR; - } - - length = dw2i(cmd, 1); - if (length <= *rx_length) - *rx_length = length; - else - { - length = *rx_length; - return_value = IFD_ERROR_INSUFFICIENT_BUFFER; - } - - /* Kobil firmware bug. No support for chaining */ - if (length && (NULL == rx_buffer)) - { - return_value = IFD_COMMUNICATION_ERROR; - } - else - memcpy(rx_buffer, cmd+10, length); - - /* Extended case? - * Only valid for RDR_to_PC_DataBlock frames */ - if (chain_parameter) - *chain_parameter = cmd[CHAIN_PARAMETER_OFFSET]; - - return return_value; -} /* CCID_Receive */ - - - -/***************************************************************************** -* -* CmdXfrBlockAPDU_extended -* -****************************************************************************/ -static RESPONSECODE CmdXfrBlockAPDU_extended(unsigned int reader_index, - unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length, - unsigned char rx_buffer[]) -{ - RESPONSECODE return_value; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - unsigned char chain_parameter; - unsigned int local_tx_length, sent_length; - unsigned int local_rx_length, received_length; - int buffer_overflow = 0; - - /* send the APDU */ - sent_length = 0; - - /* we suppose one command is enough */ - chain_parameter = 0x00; - - local_tx_length = tx_length - sent_length; - if (local_tx_length > CMD_BUF_SIZE) - { - local_tx_length = CMD_BUF_SIZE; - /* the command APDU begins with this command, and continue in the next - * PC_to_RDR_XfrBlock */ - chain_parameter = 0x01; - } - //dukpt Encrypted communication, here is not to the subcontract - if ((local_tx_length > ccid_descriptor->dwMaxCCIDMessageLength-10) && (isDukpt == 0)) - { - local_tx_length = ccid_descriptor->dwMaxCCIDMessageLength-10; - chain_parameter = 0x01; - } - -send_next_block: - return_value = CCID_Transmit(reader_index, local_tx_length, tx_buffer, - chain_parameter, 0); - if (return_value != IFD_SUCCESS) - return return_value; - - sent_length += local_tx_length; - tx_buffer += local_tx_length; - - /* we just sent the last block (0x02) or only one block was needded (0x00) */ - if ((0x02 == chain_parameter) || (0x00 == chain_parameter)) - goto receive_block; - - /* read a nul block */ - return_value = CCID_Receive(reader_index, &local_rx_length, NULL, NULL); - if (return_value != IFD_SUCCESS) - return return_value; - - /* size of the next block */ - if (tx_length - sent_length > local_tx_length) - { - /* the abData field continues a command APDU and - * another block is to follow */ - chain_parameter = 0x03; - } - else - { - /* this abData field continues a command APDU and ends - * the APDU command */ - chain_parameter = 0x02; - - /* last (smaller) block */ - local_tx_length = tx_length - sent_length; - } - - goto send_next_block; - -receive_block: - /* receive the APDU */ - received_length = 0; - -receive_next_block: - local_rx_length = *rx_length - received_length; - return_value = CCID_Receive(reader_index, &local_rx_length, rx_buffer, - &chain_parameter); - if (IFD_ERROR_INSUFFICIENT_BUFFER == return_value) - { - buffer_overflow = 1; - - /* we continue to read all the response APDU */ - return_value = IFD_SUCCESS; - } - - if (return_value != IFD_SUCCESS) - return return_value; - - /* advance in the reiceiving buffer */ - rx_buffer += local_rx_length; - received_length += local_rx_length; - - switch (chain_parameter) - { - /* the response APDU begins and ends in this command */ - case 0x00: - /* this abData field continues the response APDU and ends the response - * APDU */ - case 0x02: - break; - - /* the response APDU begins with this command and is to continue */ - case 0x01: - /* this abData field continues the response APDU and another block is - * to follow */ - case 0x03: - /* empty abData field, continuation of the command APDU is expected in - * next PC_to_RDR_XfrBlock command */ - case 0x10: - /* send a nul block */ - /* set wLevelParameter to 0010h: empty abData field, - * continuation of response APDU is - * expected in the next RDR_to_PC_DataBlock. */ - return_value = CCID_Transmit(reader_index, 0, NULL, 0x10, 0); - if (return_value != IFD_SUCCESS) - return return_value; - - goto receive_next_block; - } - - *rx_length = received_length; - - /* generate an overflow detected by pcscd */ - if (buffer_overflow) - (*rx_length)++; - - return IFD_SUCCESS; -} /* CmdXfrBlockAPDU_extended */ - -/***************************************************************************** -* -* CmdXfrBlockTPDU_T0 -* -****************************************************************************/ -static RESPONSECODE CmdXfrBlockTPDU_T0(unsigned int reader_index, - unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length, - unsigned char rx_buffer[]) -{ - RESPONSECODE return_value = IFD_SUCCESS; - //_ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - - - /* command length too big for CCID reader? */ -#if 0 - if (tx_length > ccid_descriptor->dwMaxCCIDMessageLength-10) - { - return IFD_COMMUNICATION_ERROR; - } -#else - if (tx_length > MAX_BUFFER_SIZE) - { - return IFD_COMMUNICATION_ERROR; - } - -#endif - /* command length too big for CCID driver? */ - if (tx_length > CMD_BUF_SIZE) - { - //DEBUG_CRITICAL3("Command too long (%d bytes) for max: %d bytes", - // tx_length, CMD_BUF_SIZE); - return IFD_COMMUNICATION_ERROR; - } - - return_value = CCID_Transmit(reader_index, tx_length, tx_buffer, 0, 0); - if (return_value != IFD_SUCCESS) - return return_value; - - return CCID_Receive(reader_index, rx_length, rx_buffer, NULL); -} /* CmdXfrBlockTPDU_T0 */ - - - - - -/***************************************************************************** -* -* CmdXfrBlockTPDU_T1 -* -****************************************************************************/ -static RESPONSECODE CmdXfrBlockTPDU_T1(unsigned int reader_index, - unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length, - unsigned char rx_buffer[]) -{ - RESPONSECODE return_value = IFD_SUCCESS; - int ret; - - - ret = t1_transceive(&((get_ccid_slot(reader_index)) -> t1), 0, - tx_buffer, tx_length, rx_buffer, *rx_length); - - if (ret < 0) - return_value = IFD_COMMUNICATION_ERROR; - else - *rx_length = ret; - - return return_value; -} /* CmdXfrBlockTPDU_T1 */ - - -/***************************************************************************** -* -* CmdXfrBlock -* -****************************************************************************/ -RESPONSECODE CmdXfrBlock(unsigned int reader_index, unsigned int tx_length, - unsigned char tx_buffer[], unsigned int *rx_length, - unsigned char rx_buffer[], int protocol) -{ - RESPONSECODE return_value = IFD_SUCCESS; - //_ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - - //here a lot of protocol - if (protocol == T_0) - // return_value = CmdXfrBlockCHAR_T0(reader_index, tx_length, - // tx_buffer, rx_length, rx_buffer); - // return_value = CmdXfrBlockTPDU_T0(reader_index, - // tx_length, tx_buffer, rx_length, rx_buffer); - return_value = CmdXfrBlockAPDU_extended(reader_index,tx_length, tx_buffer, rx_length, rx_buffer); - else - if (protocol == T_1) - return_value = CmdXfrBlockTPDU_T1(reader_index, tx_length, - tx_buffer, rx_length, rx_buffer); - else - { - - return_value=IFD_ERROR_NOT_SUPPORTED; - } - - return return_value; -} /* CmdXfrBlock */ - - - - - - - - - - - - - - +/* + * Support for bR301(Bluetooth) smart card reader + * + * ft_ccid_cmd.m: The code has moved from CCID Driver, which is open souce code by GNU license (http://pcsclite.alioth.debian.org/ccid.html) + * Some parts has integrated private command, these command only using for Feitian bluetooth and iDock reader,like ComdGetSerialNum,CmdGetSerialNum,CmdGetDevInfo,CmdWriteFlash,CmdReadFlash,CmdWriteIKSN2IPEKFlash,CmdGetKSN,CmdSetEncMod + * + * And the code was apply commands.c file which is GNU CCID open source code + * + * $Id: commands.c 6879 2014-03-23 15:11:20Z rousseau $ + * + * The file has packaged PC/SC API by Objective-C Lang, user can called Objective-C interface to monitor + * card and reader event + * Copyright (C) Feitian 2014, Ben + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + + +#include "ft_ccid.h" +#include "wintypes.h" +#include "proto-t1.h" +#include + +#import "bR301SessionController.h" + +//#define FT_IR301_DEBUG + +static _ccid_descriptor s_ccidDevice[CCID_DRIVER_MAX_READERS]={0}; + +static CcidDesc s_CcidSlots[CCID_DRIVER_MAX_READERS]={0}; + +#pragma mark ccid tool func +/***************************************************************************** + * + * i2dw + * + ****************************************************************************/ + +static void i2dw(int value, unsigned char buffer[]) +{ + buffer[0] = value & 0xFF; + buffer[1] = (value >> 8) & 0xFF; + buffer[2] = (value >> 16) & 0xFF; + buffer[3] = (value >> 24) & 0xFF; +} /* i2dw */ + +/***************************************************************************** +* +* bei2i (big endian integer to host order interger) +* +****************************************************************************/ + +//static unsigned int bei2i(unsigned char buffer[]) +//{ +// return (buffer[0]<<24) + (buffer[1]<<16) + (buffer[2]<<8) + buffer[3]; +//} + +/***************************************************************************** + * + * LOCK + * + ****************************************************************************/ + +//int ftLock(int condition) +//{ +// +// EADSessionController *sessionController = [EADSessionController sharedController]; +// return [sessionController myLock:condition]; +//} + + +/***************************************************************************** + * + * UNLOCK + * + ****************************************************************************/ + +//int ftUnLock(int condition) +//{ +// +// EADSessionController *sessionController = [EADSessionController sharedController]; +// return [sessionController myUnLock:condition]; +//} + + +/***************************************************************************** + * + * get_ccid_descriptor + * + ****************************************************************************/ +_ccid_descriptor *get_ccid_descriptor(unsigned int reader_index) +{ + return &s_ccidDevice[0]; +} /* get_ccid_descriptor */ + + +CcidDesc *get_ccid_slot(unsigned int reader_index) +{ + return &s_CcidSlots[0]; +} /* get_ccid_slot */ + + +/***************************************************************************** + * + * ReadSerial: Receive bytes from the card reader + * + *****************************************************************************/ +status_t ReadSerial(unsigned int reader_index, + unsigned int *length, unsigned char *buffer) +{ + @try { + + int iRet=0; + + bR301SessionController *sessionController = [bR301SessionController sharedController]; + gIsReadData = 1; + iRet = [sessionController readData: buffer withbytesToRead:length]; + gIsReadData = 0; + if(iRet != 0) + { +#ifdef FT_IR301_DEBUG + NSLog(@"==========read error iRet=%d\n", iRet); +#endif + return STATUS_COMM_ERROR; + } + else + { +#ifdef FT_IR301_DEBUG + NSMutableData *tmpData = [NSMutableData data]; + [tmpData appendBytes:buffer length:*length]; + + NSString* dataString= [NSString stringWithFormat:@"%@",tmpData]; + NSRange begin = [dataString rangeOfString:@"<"]; + NSRange end = [dataString rangeOfString:@">"]; + NSRange range = NSMakeRange(begin.location + begin.length, end.location- begin.location - 1); + dataString = [dataString substringWithRange:range]; + NSLog(@"==========read:\n%@",dataString); +#endif + return STATUS_SUCCESS; + } + } + @catch (...) { + NSLog(@"==========read error STATUS_COMM_ERROR\n"); + return STATUS_COMM_ERROR; + } + @finally { + + } + +} + +/***************************************************************************** + * + * WriteSerial: Send bytes to the card reader + * + *****************************************************************************/ +status_t WriteSerial(unsigned int reader_index, unsigned int length, + unsigned char *buffer) +{ + @try { + int iRet=0; + + bR301SessionController *sessionController = [bR301SessionController sharedController]; + + iRet = [sessionController writeData: buffer withLength:length]; + +#ifdef FT_IR301_DEBUG + NSMutableData *tmpData = [NSMutableData data]; + [tmpData appendBytes:buffer length:length]; + + NSString* dataString= [NSString stringWithFormat:@"%@",tmpData]; + NSRange begin = [dataString rangeOfString:@"<"]; + NSRange end = [dataString rangeOfString:@">"]; + NSRange range = NSMakeRange(begin.location + begin.length, end.location- begin.location - 1); + dataString = [dataString substringWithRange:range]; + NSLog(@"==========write:\n%@",dataString); +#endif + + + if (iRet == 0 ) + { + return STATUS_SUCCESS; + } + else + { + + return STATUS_COMM_ERROR; + } + + } + @catch (...) { + + return STATUS_COMM_ERROR; + } + @finally { + + } + +} + +#pragma mark feitian's private tool cmd + +RESPONSECODE CmdGetDevVer(unsigned int reader_index,char *firmwareRevision,char *hardwareRevision) +{ + if (gIsOpen == NO) { + return IFD_ERROR_TAG; + } + + bR301SessionController *sessionController = [bR301SessionController sharedController]; + NSString *tmp=[[sessionController accessory] firmwareRevision]; + if (iR301_or_bR301 == 0) + { + if (tmp != nil) { + strcpy(firmwareRevision,[tmp UTF8String]); + } + else{ + return IFD_ERROR_TAG; + } + } + + tmp=[[sessionController accessory] hardwareRevision]; + if (tmp != nil) { + strcpy(hardwareRevision,[tmp UTF8String]); + } + else{ + return IFD_ERROR_TAG; + } + + + if (iR301_or_bR301 == 1) + { + unsigned char cmd[10+6]={0}; + unsigned char tmp[10+32]={0}; + status_t res; + unsigned int length = 0; + + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = 0x04; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + cmd[10] = 0x5A; + cmd[11] = 0xA5; + cmd[12] = 0x20; + cmd[13] = 0x00; + + res = WritePort(reader_index, 10+4, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length = sizeof(tmp); + res = ReadPort(reader_index, &length, tmp); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if(length < 10+4) + { + + return IFD_COMMUNICATION_ERROR; + } + + sprintf(firmwareRevision, "%x.%02x",tmp[10],tmp[11]); + + } + + return IFD_SUCCESS; + + return IFD_SUCCESS; +} + +/***************************************************************************** + * + * CmdGetSerialNum this is a feitian's private cmd + * + ****************************************************************************/ +RESPONSECODE CmdGetSerialNum(unsigned int reader_index, unsigned int * pnlength, + unsigned char buffer[]) +{ + unsigned char cmd[10+4]={0}; + unsigned char tmp[10+300]={0}; + status_t res; + unsigned int length = 0; + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = 0x04; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + cmd[10] = 0x5A; + cmd[11] = 0xA5; + cmd[12] = 0x31; + cmd[13] = 0x31; + + + res = WritePort(reader_index, 10+4, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length = *pnlength; + + res = ReadPort(reader_index, &length, tmp); + + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(tmp[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + + return IFD_COMMUNICATION_ERROR; + + } + + if(length < 10+ 16) + { + + return IFD_COMMUNICATION_ERROR; + } + + *pnlength = 16; + memcpy(buffer, tmp+10, 16); + + return return_value; +} /* CmdGetSerialNum */ + + +/***************************************************************************** + * + * CmdGetDevInfo this is a feitian's private cmd + * + ****************************************************************************/ +RESPONSECODE CmdGetDevInfo(unsigned int reader_index, unsigned int * pnlength, + unsigned char buffer[]) +{ + unsigned char cmd[10+4]={0}; + unsigned char tmp[10+300]={0}; + status_t res; + unsigned int length = 0; + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = 0x02; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + cmd[10] = 0x5A; + cmd[11] = 0xA1; + + + res = WritePort(reader_index, 10+2, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length = *pnlength; + + res = ReadPort(reader_index, &length, tmp); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + + ccid_error(tmp[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + + return IFD_COMMUNICATION_ERROR; + + } + + if(length < 10+ 10) + { + + return IFD_COMMUNICATION_ERROR; + } + + *pnlength = 10; + memcpy(buffer, tmp+10, 10); + + return return_value; +} /* CmdGetDevInfo */ + +/***************************************************************************** + * + * CmdWriteFlash this is a feitian's private cmd + * + ****************************************************************************/ +RESPONSECODE CmdWriteFlash(unsigned int reader_index,unsigned char bOffset, unsigned char blength,unsigned char buffer[]) +{ + unsigned char cmd[10+4]={0}; + unsigned char tmp[10+300]={0}; + status_t res; + unsigned int length=sizeof(cmd); + + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + //first read flash + if (bOffset == 0 && blength == 255) + { + memcpy(tmp+10+bOffset,buffer,blength); + } + else + { + return_value = CmdReadFlash( reader_index,0,255,tmp+10); + + if (IFD_SUCCESS != return_value) + { + return return_value; + } + memcpy(tmp+10+bOffset,buffer,blength); + + bOffset = 0; + blength = 255; + + } + + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = 0x04; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + cmd[10] = 0x5A; + cmd[11] = 0xA6; + cmd[12] = blength; + cmd[13] = bOffset; + + + res = WritePort(reader_index, 10+4, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + res = ReadPort(reader_index, &length, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(buffer[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + + return IFD_COMMUNICATION_ERROR; + + } + + //now write flash data + + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = blength; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + + memcpy(tmp, cmd, 10); + + res = WritePort(reader_index,blength+10, tmp); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length=sizeof(tmp); + res = ReadPort(reader_index, &length, tmp); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + return IFD_COMMUNICATION_ERROR; + + } + + return return_value; +} /* CmdWriteFlash */ + + + +/***************************************************************************** + * + * CmdReadFlash this is a feitian's private cmd + * + ****************************************************************************/ +RESPONSECODE CmdReadFlash(unsigned int reader_index, unsigned char bOffset, unsigned char blength,unsigned char buffer[]) +{ + unsigned char cmd[10+300]={0}; + status_t res; + unsigned int length=sizeof(cmd); + + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = 0x04; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = 0; + cmd[8] = cmd[9] = 0; /* RFU */ + cmd[10] = 0x5A; + cmd[11] = 0xA7; + cmd[12] = blength; + cmd[13] = bOffset; + + + res = WritePort(reader_index, 10+4, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + + res = ReadPort(reader_index, &length, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + + return IFD_COMMUNICATION_ERROR; + + } + + if(length < 10+ blength) + { + return IFD_COMMUNICATION_ERROR; + } + + + memcpy(buffer, cmd+10, blength); + + return return_value; +} /* CmdReadFlash */ + + + +#pragma mark feitian's private duktp cmd + +/***************************************************************************** + * + * CmdWriteIKSN2IPEKFlash this is a feitian's private cmd of dukpt + * + ****************************************************************************/ +RESPONSECODE CmdWriteIKSN2IPEKFlash(unsigned int reader_index,unsigned char *encBuf,unsigned int nLen) +{ + unsigned char cmd[10+6]={0}; + unsigned char tmp[10+64]={0}; + status_t res; + unsigned int length; + + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = 4+nLen; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + cmd[10] = 0x5B; + cmd[11] = 0x02; + cmd[12] = 0x00; //private lenght h + cmd[13] = nLen; //private lenght l + + memcpy(tmp, cmd, 14); + memcpy(tmp+14, encBuf, nLen); + res = WritePort(reader_index,10+4+nLen, tmp); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length=sizeof(tmp); + res = ReadPort(reader_index, &length, tmp); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + /*if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + return IFD_COMMUNICATION_ERROR; + + }*/ + if(length < 10+ 6) + { + return IFD_COMMUNICATION_ERROR; + } + + // Can not judge +#if 0 + if (tmp[10] != 0xB5 || + tmp[11] != 0x02 || + tmp[12] != 0x00 || + tmp[13] != 0x02 ) + { + return IFD_COMMUNICATION_ERROR; + } +#endif + // + if (tmp[14] != 0x90 || + tmp[15] != 0x00) + { + return IFD_COMMUNICATION_ERROR; + } + return return_value; +} /* CmdWriteIKSN2IPEKFlash */ + +/***************************************************************************** + * + * CmdGetKSN this is a feitian's private cmd of dukpt + * + ****************************************************************************/ +RESPONSECODE CmdGetKSN(unsigned int reader_index, unsigned int * pnlength, + unsigned char buffer[]) +{ + unsigned char cmd[10+32]={0}; + unsigned char tmp[10+64]={0}; + status_t res; + unsigned int length = 0; + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = 0x04; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + cmd[10] = 0x5B; + cmd[11] = 0x03; + cmd[12] = 0x00; //private lenght h + cmd[13] = 0x00; //private lenght l + + + res = WritePort(reader_index, 10+4, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length = *pnlength; + + res = ReadPort(reader_index, &length, tmp); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + /*if (tmp[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(buffer[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + + return IFD_COMMUNICATION_ERROR; + }*/ + + if(length < 10+ 16) + { + return IFD_COMMUNICATION_ERROR; + } + // Can not judge +#if 0 + if (buffer[10] != 0xB5 || + buffer[11] != 0x03 || + buffer[12] != 0x00 || + buffer[13] != 0x0C || + buffer[24] != 0x90 || + buffer[25] != 0x00) + { + return IFD_COMMUNICATION_ERROR; + } +#endif + + *pnlength = 10; + memcpy(buffer, tmp+14, 10); + + return return_value; +} /* CmdGetKSN */ + + +/***************************************************************************** + * + * CmdSetEncryptMod this is a feitian's private cmd of dukpt + * + ****************************************************************************/ +RESPONSECODE CmdSetEncMod(unsigned int reader_index, + unsigned int bEncrypt, + unsigned int bEncFunc, + unsigned int bEncType) +{ + unsigned char cmd[10+32]={0}; + status_t res; + unsigned int length = 0; + RESPONSECODE return_value = IFD_SUCCESS; + BYTE Encrypt = (bEncrypt == DUKPT_ENC_OFF ? DUKPT_ENC_OFF: DUKPT_ENC_ON); + BYTE EncFunc = (bEncFunc == DUKPT_ENC_FUNC_3DES ? DUKPT_ENC_FUNC_3DES: DUKPT_ENC_FUNC_AES); + BYTE EncType = (bEncType == DUKPT_ENC_TYPE_DIR ? DUKPT_ENC_TYPE_DIR: DUKPT_ENC_TYPE_UNDIR); + + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + cmd[0] = 0x6B; /* feitian private cmd */ + cmd[1] = 0x07; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + cmd[10] = 0x5B; + cmd[11] = 0x01; + cmd[12] = 0x00; //private lenght h + cmd[13] = 0x03; //private lenght + cmd[14] = Encrypt; + cmd[15] = EncFunc; + cmd[16] = EncType; + + res = WritePort(reader_index, 10+7, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length = sizeof(cmd); + + res = ReadPort(reader_index, &length, cmd); + + if (res != STATUS_SUCCESS){ + + return IFD_COMMUNICATION_ERROR; + } + + if (length < STATUS_OFFSET+1) + { + + return IFD_COMMUNICATION_ERROR; + } + + /*if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + + return IFD_COMMUNICATION_ERROR; + }*/ + + if (length < 10 + 9) { + + return IFD_COMMUNICATION_ERROR; + } + + + // Can not judge +#if 0 + if (cmd[10] != 0xB5 || + cmd[11] != 0x01 || + cmd[12] != 0x00 || + cmd[13] != 0x05 || + cmd[17] != 0x90 || + cmd[18] != 0x00) + { + return IFD_COMMUNICATION_ERROR; + } +#endif + + if (cmd[14] != Encrypt || + cmd[15] != EncFunc || + cmd[16] != EncType ) + { + return IFD_ERROR_TAG; + } + + return return_value; +} /* CmdSetEncryptMod */ + + +#pragma mark ccid's sub func + +/***************************************************************************** + * + * CmdGetSlotStatus + * + ****************************************************************************/ +RESPONSECODE CmdGetSlotStatus(unsigned int reader_index, unsigned char buffer[]) +{ + unsigned char cmd[10]; + status_t res; + unsigned int length; + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + cmd[0] = 0x65; /* GetSlotStatus */ + cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + + res = WritePort(reader_index, sizeof(cmd), cmd); + if (res != STATUS_SUCCESS) + { + if (STATUS_NO_SUCH_DEVICE == res) + return IFD_NO_SUCH_DEVICE; + return IFD_COMMUNICATION_ERROR; + } + + length = SIZE_GET_SLOT_STATUS; + res = ReadPort(reader_index, &length, buffer); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (buffer[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + /* card absent or mute is not an communication error */ + if ((buffer[ERROR_OFFSET] != 0xFE)) + { + return_value = IFD_COMMUNICATION_ERROR; + } + else + { + ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; + } + } + + return return_value; +} /* CmdGetSlotStatus */ + + +/***************************************************************************** + * + * CmdPowerOff + * + ****************************************************************************/ +RESPONSECODE CmdPowerOff(unsigned int reader_index) +{ + unsigned char cmd[10]; + status_t res; + unsigned int length; + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + cmd[0] = 0x63; /* IccPowerOff */ + cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = cmd[8] = cmd[9] = 0; /* RFU */ + + res = WritePort(reader_index, sizeof(cmd), cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length = sizeof(cmd); + res = ReadPort(reader_index, &length, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + if (cmd[ERROR_OFFSET] == 0xFE) { + + ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; + } + return_value = IFD_COMMUNICATION_ERROR; + } + + return return_value; +} /* CmdPowerOff */ + +/***************************************************************************** + * + * Escape + * + ****************************************************************************/ +RESPONSECODE CmdEscape(unsigned int reader_index, + const unsigned char TxBuffer[], unsigned int TxLength, + unsigned char RxBuffer[], unsigned int *RxLength) +{ + unsigned char cmd[10+300]={0}; + status_t res; + unsigned int length=sizeof(cmd); + unsigned int nLen=0; + + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + nLen = TxLength & 0xFF; + + cmd[0] = 0x6B; + cmd[1] = nLen; + cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = 0; + cmd[8] = cmd[9] = 0; /* RFU */ + memcpy(cmd+10, TxBuffer, nLen); + + res = WritePort(reader_index, 10+nLen, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + + res = ReadPort(reader_index, &length, cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + + return IFD_COMMUNICATION_ERROR; + } + + if (length > 10) + { + nLen = length -10; + if(*RxLength < nLen) + { + return IFD_NOT_SUPPORTED; + } + memcpy(RxBuffer, cmd+10, nLen); + *RxLength=nLen; + } + else + { + *RxLength = 0; + } + + return return_value; + +} /* Escape */ + + +/***************************************************************************** + * + * Escape + * + ****************************************************************************/ +RESPONSECODE CmdEscape0(unsigned int reader_index, + const unsigned char TxBuffer[], unsigned int TxLength, + unsigned char RxBuffer[], unsigned int *RxLength) +{ + unsigned char *cmd_in, *cmd_out; + status_t res; + unsigned int length_in, length_out; + RESPONSECODE return_value = IFD_SUCCESS; + int old_read_timeout; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + old_read_timeout = ccid_descriptor -> readTimeout; + ccid_descriptor -> readTimeout = 30*1000; /* 30 seconds */ + +again: + /* allocate buffers */ + length_in = 10 + TxLength; + if (NULL == (cmd_in = (BYTE*)malloc(length_in))) + { + return_value = IFD_COMMUNICATION_ERROR; + goto end; + } + + length_out = 10 + *RxLength; + if (NULL == (cmd_out = (BYTE*)malloc(length_out))) + { + free(cmd_in); + return_value = IFD_COMMUNICATION_ERROR; + goto end; + } + + cmd_in[0] = 0x6B; /* PC_to_RDR_Escape */ + i2dw(length_in - 10, cmd_in+1); /* dwLength */ + cmd_in[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd_in[6] = ccid_descriptor->real_bSeq; + cmd_in[7] = cmd_in[8] = cmd_in[9] = 0; /* RFU */ + + /* copy the command */ + memcpy(&cmd_in[10], TxBuffer, TxLength); + + res = WritePort(reader_index, length_in, cmd_in); + free(cmd_in); + if (res != STATUS_SUCCESS) + { + free(cmd_out); + return_value = IFD_COMMUNICATION_ERROR; + goto end; + } + + res = ReadPort(reader_index, &length_out, cmd_out); + + /* replay the command if NAK + * This (generally) happens only for the first command sent to the reader + * with the serial protocol so it is not really needed for all the other + * ReadPort() calls */ + if (STATUS_COMM_NAK == res) + { + free(cmd_out); + goto again; + } + + if (res != STATUS_SUCCESS) + { + free(cmd_out); + return_value = IFD_COMMUNICATION_ERROR; + goto end; + } + + if (length_out < STATUS_OFFSET+1) + { + return_value = IFD_COMMUNICATION_ERROR; + goto end; + } + + if (cmd_out[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + if (cmd_out[ERROR_OFFSET] == 0xFE) { + + ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; + } + return_value = IFD_COMMUNICATION_ERROR; + } + + /* copy the response */ + length_out = dw2i(cmd_out, 1); + if (length_out > *RxLength) + length_out = *RxLength; + *RxLength = length_out; + memcpy(RxBuffer, &cmd_out[10], length_out); + + free(cmd_out); + +end: + ccid_descriptor -> readTimeout = old_read_timeout; + return return_value; +} /* Escape */ + +/***************************************************************************** + * + * CmdPowerOn + * + ****************************************************************************/ +RESPONSECODE CmdPowerOn(unsigned int reader_index, unsigned int * nlength, + unsigned char buffer[], int voltage) +{ + unsigned char cmd[10]; + status_t res; + int length=64; + unsigned int atr_len; + RESPONSECODE return_value = IFD_SUCCESS; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + cmd[0] = 0x62; /* IccPowerOn */ + cmd[1] = cmd[2] = cmd[3] = cmd[4] = 0; /* dwLength */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = voltage; + cmd[8] = cmd[9] = 0; /* RFU */ + + res = WritePort(reader_index, sizeof(cmd), cmd); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + /* reset available buffer size */ + /* needed if we go back after a switch to ISO mode */ + *nlength = length; + + res = ReadPort(reader_index, nlength, buffer); + if (res != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (*nlength < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (buffer[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + + // here voltage + //VOLTAGE_3V/*VOLTAGE_AUTO/*VOLTAGE_5V*/ + if (buffer[ERROR_OFFSET] == 0xFE) { + + ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; + } + return IFD_COMMUNICATION_ERROR; + + } + + /* extract the ATR */ + atr_len = dw2i(buffer, 1); /* ATR length */ + if (atr_len > *nlength) + atr_len = *nlength; + else + *nlength = atr_len; + + memmove(buffer, buffer+10, atr_len); + + return return_value; +} /* CmdPowerOn */ + + +/***************************************************************************** + * + * SetParameters + * + ****************************************************************************/ +RESPONSECODE SetParameters(unsigned int reader_index, char protocol, + unsigned int length, unsigned char buffer[]) +{ + unsigned char cmd[10+CMD_BUF_SIZE_EACH]; /* CCID + APDU buffer */ + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + + cmd[0] = 0x61; /* SetParameters */ + i2dw(length, cmd+1); /* APDU length */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = protocol; /* bProtocolNum */ + cmd[8] = cmd[9] = 0; /* RFU */ + + /* check that the command is not too large */ + if (length > CMD_BUF_SIZE_EACH) + return IFD_NOT_SUPPORTED; + + memcpy(cmd+10, buffer, length); + + if (WritePort(reader_index, 10+length, cmd) != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + length = sizeof(cmd); + if (ReadPort(reader_index, &length, cmd) != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + if (length < STATUS_OFFSET+1) + { + + return IFD_COMMUNICATION_ERROR; + } + + if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + ccid_error(cmd[ERROR_OFFSET], __FILE__, __LINE__, __FUNCTION__); + if (0x00 == cmd[ERROR_OFFSET]) /* command not supported */ + return IFD_NOT_SUPPORTED; + else + if ((cmd[ERROR_OFFSET] >= 1) && (cmd[ERROR_OFFSET] <= 127)) + /* a parameter is not changeable */ + return IFD_SUCCESS; + else if (cmd[ERROR_OFFSET] == 0xFE) + + ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; + else + return IFD_COMMUNICATION_ERROR; + } + + return IFD_SUCCESS; +} /* SetParameters */ + + + + +/***************************************************************************** + * + * CCID_Transmit + * + ****************************************************************************/ +RESPONSECODE CCID_Transmit(unsigned int reader_index, unsigned int tx_length, + const unsigned char tx_buffer[], unsigned short rx_length, unsigned char bBWI) +{ + unsigned char cmd[10+CMD_BUF_SIZE_EACH]; /* CCID + APDU buffer */ + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + status_t ret; + + cmd[0] = 0x6F; /* XfrBlock */ + i2dw(tx_length, cmd+1); /* APDU length */ + cmd[5] = ccid_descriptor->bCurrentSlotIndex; /* slot number */ + cmd[6] = ccid_descriptor->real_bSeq++; + cmd[7] = bBWI; /* extend block waiting timeout */ + cmd[8] = rx_length & 0xFF; /* Expected length, in character mode only */ + cmd[9] = (rx_length >> 8) & 0xFF; + + /* check that the command is not too large */ + if (tx_length > CMD_BUF_SIZE_EACH) + { + return IFD_NOT_SUPPORTED; + } + + //type b + if (1) { + if (NULL == tx_buffer) + rx_length = 0x10; /* bLevelParameter */ + } + if (tx_length >0 ) { + memcpy(cmd+10, tx_buffer, tx_length); + } + + + ret = WritePort(reader_index, 10+tx_length, cmd); + if (STATUS_NO_SUCH_DEVICE == ret) + return IFD_NO_SUCH_DEVICE; + if (ret != STATUS_SUCCESS) + return IFD_COMMUNICATION_ERROR; + + return IFD_SUCCESS; +} /* CCID_Transmit */ + + + +/***************************************************************************** + * + * CCID_Receive + * + ****************************************************************************/ +RESPONSECODE CCID_Receive(unsigned int reader_index, unsigned int *rx_length, + unsigned char rx_buffer[], unsigned char *chain_parameter) +{ + unsigned char cmd[10+CMD_BUF_SIZE_EACH]; /* CCID + APDU buffer */ + unsigned int length; + RESPONSECODE return_value = IFD_SUCCESS; + status_t ret; + + //_ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + //type B + if(1) + { + // int r; + unsigned char rx_tmp[4]; + //unsigned char *old_rx_buffer = NULL; + //int old_rx_length = 0; + + /* read a nul block. buffer need to be at least 4-bytes */ + if (NULL == rx_buffer) + { + rx_buffer = rx_tmp; + *rx_length = sizeof(rx_tmp); + } + + } +time_request: + length = sizeof(cmd); + ret = ReadPort(reader_index, &length, cmd); + if (ret != STATUS_SUCCESS) + { + if (STATUS_NO_SUCH_DEVICE == ret) + return IFD_NO_SUCH_DEVICE; + return IFD_COMMUNICATION_ERROR; + } + + if (length < STATUS_OFFSET+1) + { + return IFD_COMMUNICATION_ERROR; + } + + if (cmd[STATUS_OFFSET] & CCID_COMMAND_FAILED) + { + switch (cmd[ERROR_OFFSET]) + { + case 0xEF: /* cancel */ + if (*rx_length < 2) + return IFD_COMMUNICATION_ERROR; + rx_buffer[0]= 0x64; + rx_buffer[1]= 0x01; + *rx_length = 2; + return IFD_SUCCESS; + + case 0xF0: /* timeout */ + if (*rx_length < 2) + return IFD_COMMUNICATION_ERROR; + rx_buffer[0]= 0x64; + rx_buffer[1]= 0x00; + *rx_length = 2; + return IFD_SUCCESS; + + case 0xFD: /* Parity error during exchange */ + return IFD_PARITY_ERROR; + case 0xFE: + return IFD_COMMUNICATION_ERROR; + default: + return IFD_COMMUNICATION_ERROR; + } + } + + if (cmd[STATUS_OFFSET] & CCID_TIME_EXTENSION) + { + goto time_request; + } + + /* we have read less (or more) data than the CCID frame says to contain */ + if (length-10 != dw2i(cmd, 1)) + { + return_value = IFD_COMMUNICATION_ERROR; + } + + length = dw2i(cmd, 1); + if (length <= *rx_length) + *rx_length = length; + else + { + length = *rx_length; + return_value = IFD_ERROR_INSUFFICIENT_BUFFER; + } + + /* Kobil firmware bug. No support for chaining */ + if (length && (NULL == rx_buffer)) + { + return_value = IFD_COMMUNICATION_ERROR; + } + else + memcpy(rx_buffer, cmd+10, length); + + /* Extended case? + * Only valid for RDR_to_PC_DataBlock frames */ + if (chain_parameter) + *chain_parameter = cmd[CHAIN_PARAMETER_OFFSET]; + + return return_value; +} /* CCID_Receive */ + + + +/***************************************************************************** + * + * CmdXfrBlockAPDU_extended + * + ****************************************************************************/ +static RESPONSECODE CmdXfrBlockAPDU_extended(unsigned int reader_index, + unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length, + unsigned char rx_buffer[]) +{ + RESPONSECODE return_value; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + unsigned char chain_parameter; + unsigned int local_tx_length, sent_length; + unsigned int local_rx_length, received_length; + int buffer_overflow = 0; + + /* send the APDU */ + sent_length = 0; + + /* we suppose one command is enough */ + chain_parameter = 0x00; + + local_tx_length = tx_length - sent_length; + if (local_tx_length > CMD_BUF_SIZE) + { + local_tx_length = CMD_BUF_SIZE; + /* the command APDU begins with this command, and continue in the next + * PC_to_RDR_XfrBlock */ + chain_parameter = 0x01; + } + //dukpt Encrypted communication, here is not to the subcontract + if ((local_tx_length > ccid_descriptor->dwMaxCCIDMessageLength-10) && (isDukpt == 0)) + { + local_tx_length = ccid_descriptor->dwMaxCCIDMessageLength-10; + chain_parameter = 0x01; + } + +send_next_block: + return_value = CCID_Transmit(reader_index, local_tx_length, tx_buffer, + chain_parameter, 0); + if (return_value != IFD_SUCCESS) + return return_value; + + sent_length += local_tx_length; + tx_buffer += local_tx_length; + + /* we just sent the last block (0x02) or only one block was needded (0x00) */ + if ((0x02 == chain_parameter) || (0x00 == chain_parameter)) + goto receive_block; + + /* read a nul block */ + return_value = CCID_Receive(reader_index, &local_rx_length, NULL, NULL); + if (return_value != IFD_SUCCESS) + return return_value; + + /* size of the next block */ + if (tx_length - sent_length > local_tx_length) + { + /* the abData field continues a command APDU and + * another block is to follow */ + chain_parameter = 0x03; + } + else + { + /* this abData field continues a command APDU and ends + * the APDU command */ + chain_parameter = 0x02; + + /* last (smaller) block */ + local_tx_length = tx_length - sent_length; + } + + goto send_next_block; + +receive_block: + /* receive the APDU */ + received_length = 0; + +receive_next_block: + local_rx_length = *rx_length - received_length; + return_value = CCID_Receive(reader_index, &local_rx_length, rx_buffer, + &chain_parameter); + if (IFD_ERROR_INSUFFICIENT_BUFFER == return_value) + { + buffer_overflow = 1; + + /* we continue to read all the response APDU */ + return_value = IFD_SUCCESS; + } + + if (return_value != IFD_SUCCESS) + return return_value; + + /* advance in the reiceiving buffer */ + rx_buffer += local_rx_length; + received_length += local_rx_length; + + switch (chain_parameter) + { + /* the response APDU begins and ends in this command */ + case 0x00: + /* this abData field continues the response APDU and ends the response + * APDU */ + case 0x02: + break; + + /* the response APDU begins with this command and is to continue */ + case 0x01: + /* this abData field continues the response APDU and another block is + * to follow */ + case 0x03: + /* empty abData field, continuation of the command APDU is expected in + * next PC_to_RDR_XfrBlock command */ + case 0x10: + /* send a nul block */ + /* set wLevelParameter to 0010h: empty abData field, + * continuation of response APDU is + * expected in the next RDR_to_PC_DataBlock. */ + return_value = CCID_Transmit(reader_index, 0, NULL, 0x10, 0); + if (return_value != IFD_SUCCESS) + return return_value; + + goto receive_next_block; + } + + *rx_length = received_length; + + /* generate an overflow detected by pcscd */ + if (buffer_overflow) + (*rx_length)++; + + return IFD_SUCCESS; +} /* CmdXfrBlockAPDU_extended */ + +/***************************************************************************** + * + * CmdXfrBlockTPDU_T0 + * + ****************************************************************************/ +static RESPONSECODE CmdXfrBlockTPDU_T0(unsigned int reader_index, + unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length, + unsigned char rx_buffer[]) +{ + RESPONSECODE return_value = IFD_SUCCESS; + //_ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + + + /* command length too big for CCID reader? */ +#if 0 + if (tx_length > ccid_descriptor->dwMaxCCIDMessageLength-10) + { + return IFD_COMMUNICATION_ERROR; + } +#else + if (tx_length > MAX_BUFFER_SIZE) + { + return IFD_COMMUNICATION_ERROR; + } + +#endif + /* command length too big for CCID driver? */ + if (tx_length > CMD_BUF_SIZE) + { + //DEBUG_CRITICAL3("Command too long (%d bytes) for max: %d bytes", + // tx_length, CMD_BUF_SIZE); + return IFD_COMMUNICATION_ERROR; + } + + return_value = CCID_Transmit(reader_index, tx_length, tx_buffer, 0, 0); + if (return_value != IFD_SUCCESS) + return return_value; + + return CCID_Receive(reader_index, rx_length, rx_buffer, NULL); +} /* CmdXfrBlockTPDU_T0 */ + + + + + +/***************************************************************************** + * + * CmdXfrBlockTPDU_T1 + * + ****************************************************************************/ +static RESPONSECODE CmdXfrBlockTPDU_T1(unsigned int reader_index, + unsigned int tx_length, unsigned char tx_buffer[], unsigned int *rx_length, + unsigned char rx_buffer[]) +{ + RESPONSECODE return_value = IFD_SUCCESS; + int ret; + + +ret = t1_transceive(&((get_ccid_slot(reader_index)) -> t1), 0, + tx_buffer, tx_length, rx_buffer, *rx_length); + + if (ret < 0) + return_value = IFD_COMMUNICATION_ERROR; + else + *rx_length = ret; + + return return_value; +} /* CmdXfrBlockTPDU_T1 */ + + +/***************************************************************************** + * + * CmdXfrBlock + * + ****************************************************************************/ +RESPONSECODE CmdXfrBlock(unsigned int reader_index, unsigned int tx_length, + unsigned char tx_buffer[], unsigned int *rx_length, + unsigned char rx_buffer[], int protocol) +{ + RESPONSECODE return_value = IFD_SUCCESS; + //_ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + + //here a lot of protocol + if (protocol == T_0) + // return_value = CmdXfrBlockCHAR_T0(reader_index, tx_length, + // tx_buffer, rx_length, rx_buffer); + // return_value = CmdXfrBlockTPDU_T0(reader_index, + // tx_length, tx_buffer, rx_length, rx_buffer); + return_value = CmdXfrBlockAPDU_extended(reader_index,tx_length, tx_buffer, rx_length, rx_buffer); + else + if (protocol == T_1) + return_value = CmdXfrBlockTPDU_T1(reader_index, tx_length, + tx_buffer, rx_length, rx_buffer); + else + { + + return_value=IFD_ERROR_NOT_SUPPORTED; + } + + return return_value; +} /* CmdXfrBlock */ + + + + + + + + + + + + + + diff --git a/libs/source/src/libFTSessionController.a b/libs/source/src/libFTSessionController.a index f3edad2..1ac228f 100644 Binary files a/libs/source/src/libFTSessionController.a and b/libs/source/src/libFTSessionController.a differ diff --git a/libs/source/src/pps.c b/libs/source/src/pps.c index 9d32782..03e5aee 100755 --- a/libs/source/src/pps.c +++ b/libs/source/src/pps.c @@ -1,20 +1,20 @@ /* pps.c Protocol Parameters Selection - + This file is part of the Unix driver for Towitoko smartcard readers Copyright (C) 2000 2001 Carlos Prados - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. - + You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. @@ -32,9 +32,9 @@ static DWORD Fi[16] = {372,372,558,744,1116,1488,1860,0,0,512,768,1024,1536,2048,0,0}; static BYTE Di[16] = {0,1,2,4,8,16,32,64,12,20,0,0,0,0,0,0}; static DWORD B55_maxPPS = 318.279;/*KHZ*/ //TA1=0X16 Clock = 3.7MHZ -static DWORD C63_maxPPS = 344.086;/*KHZ*/ //TA1=0X16 Clock = 4MHZ +//static DWORD C63_maxPPS = 344.086;/*KHZ*/ //TA1=0X16 Clock = 4MHZ static float B55_clock = 3700;/*KHZ*/ -static float C63_clock = 4000;/*KHZ*/ +//static float C63_clock = 4000;/*KHZ*/ static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * reply, unsigned len_reply); @@ -48,37 +48,37 @@ PPS_Exchange (int lun, BYTE * params, unsigned *length, unsigned char *pps1) BYTE confirm[PPS_MAX_LENGTH]; unsigned len_request, len_confirm; int ret; - + len_request = PPS_GetLength (params); params[len_request - 1] = PPS_GetPCK(params, len_request - 1); /* Send PPS request */ if (CCID_Transmit (lun, len_request, params, isCharLevel(lun) ? 4 : 0, 0) - != IFD_SUCCESS) + != IFD_SUCCESS) return PPS_ICC_ERROR; - + /* Get PPS confirm */ len_confirm = sizeof(confirm); if (CCID_Receive (lun, &len_confirm, confirm, NULL) != IFD_SUCCESS) return PPS_ICC_ERROR; + - - + if (!PPS_Match (params, len_request, confirm, len_confirm)) ret = PPS_HANDSAKE_ERROR; else ret = PPS_OK; - + *pps1 = 0x11; /* default TA1 */ - + /* if PPS1 is echoed */ if (PPS_HAS_PPS1 (params) && PPS_HAS_PPS1 (confirm)) *pps1 = confirm[2]; - + /* Copy PPS handsake */ memcpy (params, confirm, len_confirm); (*length) = len_confirm; - + return ret; } @@ -87,16 +87,16 @@ PPS_Match (BYTE * request, unsigned len_request, BYTE * confirm, unsigned len_co { /* See if the reply differs from request */ if ((len_request == len_confirm) && /* same length */ - memcmp (request, confirm, len_request)) /* different contents */ + memcmp (request, confirm, len_request)) /* different contents */ return false; - + if (len_request < len_confirm) /* confirm longer than request */ return false; - + /* See if the card specifies other than default FI and D */ if ((PPS_HAS_PPS1 (confirm)) && (confirm[2] != request[2])) return false; - + return true; } @@ -104,16 +104,16 @@ static unsigned PPS_GetLength (BYTE * block) { unsigned length = 3; - + if (PPS_HAS_PPS1 (block)) length++; - + if (PPS_HAS_PPS2 (block)) length++; - + if (PPS_HAS_PPS3 (block)) length++; - + return length; } @@ -122,11 +122,11 @@ PPS_GetPCK (BYTE * block, unsigned length) { BYTE pck; unsigned i; - + pck = block[0]; for (i = 1; i < length; i++) pck ^= block[i]; - + return pck; } @@ -146,54 +146,59 @@ void extra_egt(ATR_t *atr, _ccid_descriptor *ccid_desc, DWORD Protocol) * * This modification is harmless, the reader will just be less restrictive */ - + unsigned int card_baudrate; unsigned int default_baudrate; double f, d; int i; - + /* if TA1 not present */ if (! atr->ib[0][ATR_INTERFACE_BYTE_TA].present) return; - + (void)ATR_GetParameter(atr, ATR_PARAMETER_D, &d); (void)ATR_GetParameter(atr, ATR_PARAMETER_F, &f); - + /* may happen with non ISO cards */ if ((0 == f) || (0 == d)) return; - + /* Baudrate = f x D/F */ card_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock * d / f); - + default_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock - * ATR_DEFAULT_D / ATR_DEFAULT_F); - + * ATR_DEFAULT_D / ATR_DEFAULT_F); + /* TA1 > 11? */ if (card_baudrate <= default_baudrate) return; - + /* Current EGT = 0 or FF? */ if (atr->ib[0][ATR_INTERFACE_BYTE_TC].present && - ((0x00 == atr->ib[0][ATR_INTERFACE_BYTE_TC].value) || - (0xFF == atr->ib[0][ATR_INTERFACE_BYTE_TC].value))) { - if (SCARD_PROTOCOL_T0 == Protocol) { + ((0x00 == atr->ib[0][ATR_INTERFACE_BYTE_TC].value) || + (0xFF == atr->ib[0][ATR_INTERFACE_BYTE_TC].value))) + { + if (SCARD_PROTOCOL_T0 == Protocol) + { /* Init TC1 */ atr->ib[0][ATR_INTERFACE_BYTE_TC].present = true; atr->ib[0][ATR_INTERFACE_BYTE_TC].value = 2; - + } - - if (SCARD_PROTOCOL_T1 == Protocol) { + + if (SCARD_PROTOCOL_T1 == Protocol) + { /* TBi (i>2) present? BWI/CWI */ - for (i=2; i= 2 ? */ if (atr->ib[i][ATR_INTERFACE_BYTE_TB].present && - ((atr->ib[i][ATR_INTERFACE_BYTE_TB].value & 0x0F) >= 2)) { + ((atr->ib[i][ATR_INTERFACE_BYTE_TB].value & 0x0F) >= 2)) + { /* Init TC1 */ atr->ib[0][ATR_INTERFACE_BYTE_TC].present = true; atr->ib[0][ATR_INTERFACE_BYTE_TC].value = 2; - + /* only the first TBi (i>2) must be used */ break; } @@ -214,103 +219,118 @@ void extra_egt(ATR_t *atr, _ccid_descriptor *ccid_desc, DWORD Protocol) */ RESPONSECODE ppsNegotiationValue(ATR_t *atr, BYTE *negotiationResult) { - BYTE F = 0, D = 0; - - ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &F);//Hight 4 bit - ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_DI, &D);//Low 4 bit - + BYTE F = 0, D = 0; + + ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &F);//Hight 4 bit + ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_DI, &D);//Low 4 bit + if (Fi[F] == 0 || Di[D] == 0) { - return IFD_ERROR_NOT_SUPPORTED; - } - - DWORD speedValue = Di[D]*B55_clock/Fi[F]; - if (speedValue >= B55_maxPPS) { - *negotiationResult = 0x96; - } else { - *negotiationResult = atr->ib[0][ATR_INTERFACE_BYTE_TA].value; - } - return IFD_SUCCESS; + return IFD_ERROR_NOT_SUPPORTED; + } + + DWORD speedValue = Di[D]*B55_clock/Fi[F]; + if (speedValue >= B55_maxPPS) { + *negotiationResult = 0x96; + }else{ + *negotiationResult = atr->ib[0][ATR_INTERFACE_BYTE_TA].value; + } + return IFD_SUCCESS; } ///////////// -RESPONSECODE +RESPONSECODE Scrd_Negotiate(unsigned int reader_index) { ATR_t atr; BYTE pps[PPS_MAX_LENGTH]; int protocol; int convention; - CcidDesc *ccid_slot; - RESPONSECODE ret; - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); - + CcidDesc *ccid_slot; + RESPONSECODE ret; + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index); + memset(&atr, 0, sizeof(atr)); memset(pps, 0, sizeof(pps)); - - ccid_slot = get_ccid_slot(reader_index); + + ccid_slot = get_ccid_slot(reader_index); #if 0 - if( FT_READER_UA != gDevType) { - if (ccid_slot->nATRLength == 18 ) { - if (memcmp(ccid_slot->pcATRBuffer+3, "\x53\x6c\x65\x34\x34\x33\x32\x2d\x34\x32\x3d", 11) == 0) { - ccid_descriptor->cardProtocol = T_RAW; - } - } - } + if( FT_READER_UA != gDevType) + { + if (ccid_slot->nATRLength == 18 ) + { + if (memcmp(ccid_slot->pcATRBuffer+3, "\x53\x6c\x65\x34\x34\x33\x32\x2d\x34\x32\x3d", 11) == 0) + { + ccid_descriptor->cardProtocol = T_RAW; + } + } + } #endif - + /* Parse ATR */ ATR_InitFromArray(&atr, ccid_slot->pcATRBuffer, ccid_slot->nATRLength); if (ATR_MALFORMED == ATR_GetDefaultProtocol(&atr, &protocol)) return IFD_PROTOCOL_NOT_SUPPORTED; - - + + /* TA2 present -> specific mode */ - if (atr.ib[1][ATR_INTERFACE_BYTE_TA].present) { - if (pps[1] != (atr.ib[1][ATR_INTERFACE_BYTE_TA].value & 0x0F)) { + if (atr.ib[1][ATR_INTERFACE_BYTE_TA].present) + { + if (pps[1] != (atr.ib[1][ATR_INTERFACE_BYTE_TA].value & 0x0F)) + { /* wrong protocol */ return IFD_PROTOCOL_NOT_SUPPORTED; } } - ccid_descriptor->cardProtocol = protocol; - + ccid_descriptor->cardProtocol = protocol; + /* TCi (i>2) indicates CRC instead of LRC */ - if (T_1 == protocol) { + if (T_1 == protocol) + { t1_state_t *t1 = &(ccid_slot -> t1); int i; - + /* TCi (i>2) present? */ for (i=2; i2) must be used */ break; } } /* TA2 absent -> negotiate mode */ - //haozi iR301 is not support so set 0 - if (!atr.ib[1][ATR_INTERFACE_BYTE_TA].present && 0) { + //haozi iR301 is not support so set 0 + if (!atr.ib[1][ATR_INTERFACE_BYTE_TA].present && 0) + { BYTE FI, DI; // assigned default value FI = DI = 1; - if(atr.ib[0][ATR_INTERFACE_BYTE_TA].present) { + if(atr.ib[0][ATR_INTERFACE_BYTE_TA].present) + { ATR_GetIntegerValue (&atr, ATR_INTEGER_VALUE_DI, &DI); ATR_GetIntegerValue (&atr, ATR_INTEGER_VALUE_FI, &FI); } if(DI > 1 - && DI < 10) { + && DI < 10) + { // do PPS unsigned int len; @@ -321,38 +341,43 @@ Scrd_Negotiate(unsigned int reader_index) pps[2] = atr.ib[0][ATR_INTERFACE_BYTE_TA].value; if (PPS_Exchange(reader_index, pps, &len, &pps[2]) != PPS_OK) { - return IFD_ERROR_PTS_FAILURE; - } + return IFD_ERROR_PTS_FAILURE; + } - } else { - ;// DbgPrint("######### No PPS #########\n"); } - } else { + else + { + ;// DbgPrint("######### No PPS #########\n"); + } + } + else + { /* TA2 present -> specific mode */ //DbgPrint("######### Specific mode #########\n"); pps[1] |= 0x10; /* PTS1 presence */ - BYTE NegotiationPps = 0x00; - RESPONSECODE returnValue = ppsNegotiationValue(&atr, &NegotiationPps); - if (returnValue != IFD_SUCCESS) { - return IFD_SUCCESS; - } - if (atr.ib[0][ATR_INTERFACE_BYTE_TA].value != NegotiationPps) { - pps[2] = NegotiationPps; - } else { - - pps[2] = atr.ib[0][ATR_INTERFACE_BYTE_TA].value; - if (gDevType != FT_READER_UA) { - return IFD_SUCCESS; - } - } + BYTE NegotiationPps = 0x00; + RESPONSECODE returnValue = ppsNegotiationValue(&atr, &NegotiationPps); + if (returnValue != IFD_SUCCESS) { + return IFD_SUCCESS; + } + if (atr.ib[0][ATR_INTERFACE_BYTE_TA].value != NegotiationPps) { + pps[2] = NegotiationPps; + }else{ + + pps[2] = atr.ib[0][ATR_INTERFACE_BYTE_TA].value; + if (gDevType != FT_READER_UA) { + return IFD_SUCCESS; + } + } } // set parameters for T=0 or T=1 ATR_GetConvention(&atr, &convention); - if(protocol == T_1) { + if(protocol == T_1) + { int i; BYTE param[] = { 0x11, /* Fi/Di */ @@ -369,20 +394,24 @@ Scrd_Negotiate(unsigned int reader_index) param[0] = pps[2]; /* TCi (i>2) indicates CRC instead of LRC */ - for (i=2; i2) must be used */ break; } } - + /* the CCID should ignore this bit */ if (ATR_CONVENTION_INVERSE == convention) param[1] |= 0x02; @@ -392,8 +421,10 @@ Scrd_Negotiate(unsigned int reader_index) param[2] = atr.ib[0][ATR_INTERFACE_BYTE_TC].value; /* TBi (i>2) present? BWI/CWI */ - for (i=2; i2) must be used */ @@ -402,8 +433,10 @@ Scrd_Negotiate(unsigned int reader_index) } /* TAi (i>2) present? IFSC */ - for (i=2; i - * - * improvements by: - * Copyright (C) 2004 Ludovic Rousseau - */ - -#include "buffer.h" -#include "proto-t1.h" -#include "checksum.h" - - -#include "ft_ccid.h" - -#include -#include - -/* I block */ -#define T1_I_SEQ_SHIFT 6 - -/* R block */ -#define T1_IS_ERROR(pcb) ((pcb) & 0x0F) -#define T1_EDC_ERROR 0x01 -#define T1_OTHER_ERROR 0x02 -#define T1_R_SEQ_SHIFT 4 - -/* S block stuff */ -#define T1_S_IS_RESPONSE(pcb) ((pcb) & T1_S_RESPONSE) -#define T1_S_TYPE(pcb) ((pcb) & 0x0F) -#define T1_S_RESPONSE 0x20 -#define T1_S_RESYNC 0x00 -#define T1_S_IFS 0x01 -#define T1_S_ABORT 0x02 -#define T1_S_WTX 0x03 - -#define swap_nibbles(x) ( (x >> 4) | ((x & 0xF) << 4) ) - -#ifndef TRUE -#define TRUE 1 -#define FALSE 0 -#endif - -#define NAD 0 -#define PCB 1 -#define LEN 2 -#define DATA 3 - -/* internal state, do not mess with it. */ -/* should be != DEAD after reset/init */ -enum { - SENDING, RECEIVING, RESYNCH, DEAD -}; - -static void t1_set_checksum(t1_state_t *, int); -static unsigned int t1_block_type(unsigned char); -static unsigned int t1_seq(unsigned char); -static unsigned int t1_rebuild(t1_state_t *t1, unsigned char *block); -static unsigned int t1_compute_checksum(t1_state_t *, unsigned char *, size_t); -static int t1_verify_checksum(t1_state_t *, unsigned char *, size_t); -static int t1_xcv(t1_state_t *, unsigned char *, size_t, size_t); - - -/***************************************************************************** - * - * isCharLevel - * - ****************************************************************************/ -int isCharLevel(int reader_index) -{ - return 0;//haozi return CCID_CLASS_CHARACTER == (get_ccid_descriptor(reader_index)->dwFeatures & CCID_CLASS_EXCHANGE_MASK); -} /* isCharLevel */ - - - -/* - * Set default T=1 protocol parameters - */ -static void t1_set_defaults(t1_state_t * t1) -{ - t1->retries = 3; - /* This timeout is rather insane, but we need this right now - * to support cryptoflex keygen */ - t1->ifsc = 32; - t1->ifsd = 32; - t1->nr = 0; - t1->ns = 0; - t1->wtx = 0; -} - -static void t1_set_checksum(t1_state_t * t1, int csum) -{ - switch (csum) { - case IFD_PROTOCOL_T1_CHECKSUM_LRC: - t1->rc_bytes = 1; - t1->checksum = csum_lrc_compute; - break; - case IFD_PROTOCOL_T1_CHECKSUM_CRC: - t1->rc_bytes = 2; - t1->checksum = csum_crc_compute; - break; - } -} - -/* - * Attach t1 protocol - */ -int t1_init(t1_state_t * t1, int lun) -{ - t1_set_defaults(t1); - t1_set_param(t1, IFD_PROTOCOL_T1_CHECKSUM_LRC, 0); - t1_set_param(t1, IFD_PROTOCOL_T1_STATE, SENDING); - t1_set_param(t1, IFD_PROTOCOL_T1_MORE, FALSE); - - t1->lun = lun; - - return 0; -} - -/* - * Detach t1 protocol - */ -void t1_release(/*@unused@*/ t1_state_t * t1) -{ - (void)t1; - /* NOP */ -} - -/* - * Get/set parmaters for T1 protocol - */ -int t1_set_param(t1_state_t * t1, int type, long value) -{ - switch (type) { - case IFD_PROTOCOL_T1_CHECKSUM_LRC: - case IFD_PROTOCOL_T1_CHECKSUM_CRC: - t1_set_checksum(t1, type); - break; - case IFD_PROTOCOL_T1_IFSC: - t1->ifsc = (unsigned int)value; - break; - case IFD_PROTOCOL_T1_IFSD: - t1->ifsd = (unsigned int)value; - break; - case IFD_PROTOCOL_T1_STATE: - t1->state = (unsigned int)value; - break; - case IFD_PROTOCOL_T1_MORE: - t1->more = value; - break; - default: - ;//DEBUG_INFO2("Unsupported parameter %d", type); - return -1; - } - - return 0; -} - -/* - * Send an APDU through T=1 - */ -int t1_transceive(t1_state_t * t1, unsigned int dad, - const void *snd_buf, size_t snd_len, - void *rcv_buf, size_t rcv_len) -{ - ct_buf_t sbuf, rbuf, tbuf; - unsigned char sdata[T1_BUFFER_SIZE], sblk[5]; - unsigned int slen, retries, resyncs, sent_length = 0; - size_t last_send = 0; - - if (snd_len == 0) - return -1; - - /* we can't talk to a dead card / reader. Reset it! */ - if (t1->state == DEAD) { - //DEBUG_CRITICAL("T=1 state machine is DEAD. Reset the card first."); - return -1; - } - - t1->state = SENDING; - retries = t1->retries; - resyncs = 3; - - /* Initialize send/recv buffer */ - ct_buf_set(&sbuf, (void *)snd_buf, (unsigned int)snd_len); - ct_buf_init(&rbuf, rcv_buf, (unsigned int)rcv_len); - - /* Send the first block */ - slen = t1_build(t1, sdata, dad, T1_I_BLOCK, &sbuf, &last_send); - - while (1) { - unsigned char pcb; - int n; - - retries--; - - n = t1_xcv(t1, sdata, slen, sizeof(sdata)); - if (-2 == n) { - // DEBUG_COMM("Parity error"); - /* ISO 7816-3 Rule 7.4.2 */ - if (retries == 0) - goto resync; - - /* ISO 7816-3 Rule 7.2 */ - if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) { -// DEBUG_COMM("Rule 7.2"); - slen = t1_rebuild(t1, sdata); - continue; - } - - slen = t1_build(t1, sdata, - dad, T1_R_BLOCK | T1_EDC_ERROR, - NULL, NULL); - continue; - } - - if (n < 0) { -// DEBUG_CRITICAL("fatal: transmit/receive failed"); - t1->state = DEAD; - goto error; - } - - if ((sdata[NAD] != swap_nibbles(dad)) /* wrong NAD */ - || (sdata[LEN] == 0xFF)) { /* length == 0xFF (illegal) */ -// DEBUG_COMM("R-BLOCK required"); - /* ISO 7816-3 Rule 7.4.2 */ - if (retries == 0) - goto resync; - - /* ISO 7816-3 Rule 7.2 */ - if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) { -// DEBUG_COMM("Rule 7.2"); - slen = t1_rebuild(t1, sdata); - continue; - } - - slen = t1_build(t1, sdata, - dad, T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - - if (!t1_verify_checksum(t1, sdata, n)) { -// DEBUG_COMM("checksum failed"); - /* ISO 7816-3 Rule 7.4.2 */ - if (retries == 0) - goto resync; - - /* ISO 7816-3 Rule 7.2 */ - if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) { -// DEBUG_COMM("Rule 7.2"); - slen = t1_rebuild(t1, sdata); - continue; - } - - slen = t1_build(t1, sdata, - dad, T1_R_BLOCK | T1_EDC_ERROR, - NULL, NULL); - continue; - } - - pcb = sdata[PCB]; - switch (t1_block_type(pcb)) { - case T1_R_BLOCK: - if ((sdata[LEN] != 0x00) /* length != 0x00 (illegal) */ - || (pcb & 0x20) /* b6 of pcb is set */ - ) { -// DEBUG_COMM("R-Block required"); - /* ISO 7816-3 Rule 7.4.2 */ - if (retries == 0) - goto resync; - - /* ISO 7816-3 Rule 7.2 */ - if (T1_R_BLOCK == t1_block_type(t1->previous_block[1])) { -// DEBUG_COMM("Rule 7.2"); - slen = t1_rebuild(t1, sdata); - continue; - } - - slen = t1_build(t1, sdata, - dad, T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - - if (((t1_seq(pcb) != t1->ns) /* wrong sequence number & no bit more */ - && ! t1->more) - ) { -// DEBUG_COMM4("received: %d, expected: %d, more: %d", -// t1_seq(pcb), t1->ns, t1->more); - - /* ISO 7816-3 Rule 7.2 */ - if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) { -// DEBUG_COMM("Rule 7.2"); - slen = t1_rebuild(t1, sdata); - continue; - } - -// DEBUG_COMM("R-Block required"); - /* ISO 7816-3 Rule 7.4.2 */ - if (retries == 0) - goto resync; - slen = t1_build(t1, sdata, - dad, T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - - if (t1->state == RECEIVING) { - /* ISO 7816-3 Rule 7.2 */ - if (T1_R_BLOCK == t1_block_type(t1->previous_block[1])) { -// DEBUG_COMM("Rule 7.2"); - slen = t1_rebuild(t1, sdata); - continue; - } - -// DEBUG_COMM(""); - slen = t1_build(t1, sdata, - dad, T1_R_BLOCK, - NULL, NULL); - break; - } - - /* If the card terminal requests the next - * sequence number, it received the previous - * block successfully */ - if (t1_seq(pcb) != t1->ns) { - ct_buf_get(&sbuf, NULL, (unsigned int)last_send); - sent_length += last_send; - last_send = 0; - t1->ns ^= 1; - } - - /* If there's no data available, the ICC - * shouldn't be asking for more */ - if (ct_buf_avail(&sbuf) == 0) - goto resync; - - slen = t1_build(t1, sdata, dad, T1_I_BLOCK, - &sbuf, &last_send); - break; - - case T1_I_BLOCK: - /* The first I-block sent by the ICC indicates - * the last block we sent was received successfully. */ - if (t1->state == SENDING) { -// DEBUG_COMM(""); - ct_buf_get(&sbuf, NULL, (unsigned int)last_send); - last_send = 0; - t1->ns ^= 1; - } - - t1->state = RECEIVING; - - /* If the block sent by the card doesn't match - * what we expected it to send, reply with - * an R block */ - if (t1_seq(pcb) != t1->nr) { -// DEBUG_COMM("wrong nr"); - slen = t1_build(t1, sdata, dad, - T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - - t1->nr ^= 1; - - if (ct_buf_put(&rbuf, sdata + 3, sdata[LEN]) < 0) { -// DEBUG_CRITICAL2("buffer overrun by %d bytes", sdata[LEN] - (rbuf.size - rbuf.tail)); - goto error; - } - - if ((pcb & T1_MORE_BLOCKS) == 0) - goto done; - - slen = t1_build(t1, sdata, dad, T1_R_BLOCK, NULL, NULL); - break; - - case T1_S_BLOCK: - if (T1_S_IS_RESPONSE(pcb) && t1->state == RESYNCH) { - /* ISO 7816-3 Rule 6.2 */ -// DEBUG_COMM("S-Block answer received"); - /* ISO 7816-3 Rule 6.3 */ - t1->state = SENDING; - sent_length = 0; - last_send = 0; - resyncs = 3; - retries = t1->retries; - ct_buf_init(&rbuf, rcv_buf, (unsigned int)rcv_len); - slen = t1_build(t1, sdata, dad, T1_I_BLOCK, - &sbuf, &last_send); - continue; - } - - if (T1_S_IS_RESPONSE(pcb)) { - /* ISO 7816-3 Rule 7.4.2 */ - if (retries == 0) - goto resync; - - /* ISO 7816-3 Rule 7.2 */ - if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) { -// DEBUG_COMM("Rule 7.2"); - slen = t1_rebuild(t1, sdata); - continue; - } - -// DEBUG_CRITICAL("wrong response S-BLOCK received"); - slen = t1_build(t1, sdata, - dad, T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - - ct_buf_init(&tbuf, sblk, sizeof(sblk)); - -// DEBUG_COMM("S-Block request received"); - switch (T1_S_TYPE(pcb)) { - case T1_S_RESYNC: - if (sdata[LEN] != 0) { -// DEBUG_COMM2("Wrong length: %d", sdata[LEN]); - slen = t1_build(t1, sdata, dad, - T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - -// DEBUG_COMM("Resync requested"); - /* the card is not allowed to send a resync. */ - goto resync; - - case T1_S_ABORT: - if (sdata[LEN] != 0) { -// DEBUG_COMM2("Wrong length: %d", sdata[LEN]); - slen = t1_build(t1, sdata, dad, - T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - - /* ISO 7816-3 Rule 9 */ -// DEBUG_CRITICAL("abort requested"); - break; - - case T1_S_IFS: - if (sdata[LEN] != 1) { -// DEBUG_COMM2("Wrong length: %d", sdata[LEN]); - slen = t1_build(t1, sdata, dad, - T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - -// DEBUG_CRITICAL2("CT sent S-block with ifs=%u", sdata[DATA]); - if (sdata[DATA] == 0) - goto resync; - t1->ifsc = sdata[DATA]; - ct_buf_putc(&tbuf, sdata[DATA]); - break; - - case T1_S_WTX: - if (sdata[LEN] != 1) { -// DEBUG_COMM2("Wrong length: %d", sdata[LEN]); - slen = t1_build(t1, sdata, dad, - T1_R_BLOCK | T1_OTHER_ERROR, - NULL, NULL); - continue; - } - -// DEBUG_COMM2("CT sent S-block with wtx=%u", sdata[DATA]); - t1->wtx = sdata[DATA]; - ct_buf_putc(&tbuf, sdata[DATA]); - break; - - default: -// DEBUG_CRITICAL2("T=1: Unknown S block type 0x%02x", T1_S_TYPE(pcb)); - goto resync; - } - - slen = t1_build(t1, sdata, dad, - T1_S_BLOCK | T1_S_RESPONSE | T1_S_TYPE(pcb), - &tbuf, NULL); - } - - /* Everything went just splendid */ - retries = t1->retries; - continue; - -resync: - /* the number or resyncs is limited, too */ - /* ISO 7816-3 Rule 6.4 */ - if (resyncs == 0) - goto error; - - /* ISO 7816-3 Rule 6 */ - resyncs--; - t1->ns = 0; - t1->nr = 0; - slen = t1_build(t1, sdata, dad, T1_S_BLOCK | T1_S_RESYNC, NULL, - NULL); - t1->state = RESYNCH; - t1->more = FALSE; - retries = 1; - continue; - } - -done: - return ct_buf_avail(&rbuf); - -error: - t1->state = DEAD; - return -1; -} - -static unsigned t1_block_type(unsigned char pcb) -{ - switch (pcb & 0xC0) { - case T1_R_BLOCK: - return T1_R_BLOCK; - case T1_S_BLOCK: - return T1_S_BLOCK; - default: - return T1_I_BLOCK; - } -} - -static unsigned int t1_seq(unsigned char pcb) -{ - switch (pcb & 0xC0) { - case T1_R_BLOCK: - return (pcb >> T1_R_SEQ_SHIFT) & 1; - case T1_S_BLOCK: - return 0; - default: - return (pcb >> T1_I_SEQ_SHIFT) & 1; - } -} - -unsigned int t1_build(t1_state_t * t1, unsigned char *block, - unsigned char dad, unsigned char pcb, - ct_buf_t *bp, size_t *lenp) -{ - unsigned int len; - char more = FALSE; - - len = bp ? ct_buf_avail(bp) : 0; - if (len > t1->ifsc) { - pcb |= T1_MORE_BLOCKS; - len = t1->ifsc; - more = TRUE; - } - - /* Add the sequence number */ - switch (t1_block_type(pcb)) { - case T1_R_BLOCK: - pcb |= t1->nr << T1_R_SEQ_SHIFT; - break; - case T1_I_BLOCK: - pcb |= t1->ns << T1_I_SEQ_SHIFT; - t1->more = more; -// DEBUG_COMM2("more bit: %d", more); - break; - } - - block[0] = dad; - block[1] = pcb; - block[2] = len; - - if (len) - memcpy(block + 3, ct_buf_head(bp), len); - if (lenp) - *lenp = len; - - len = t1_compute_checksum(t1, block, len + 3); - - /* memorize the last sent block */ - /* only 4 bytes since we are only interesed in R-blocks */ - memcpy(t1->previous_block, block, 4); - - return len; -} - -static unsigned int -t1_rebuild(t1_state_t *t1, unsigned char *block) -{ - unsigned char pcb = t1 -> previous_block[1]; - - /* copy the last sent block */ - if (T1_R_BLOCK == t1_block_type(pcb)) - memcpy(block, t1 -> previous_block, 4); - else { - return 0; - } - - return 4; -} - -/* - * Build/verify checksum - */ -static unsigned int t1_compute_checksum(t1_state_t * t1, - unsigned char *data, size_t len) -{ - return (unsigned int)(len + t1->checksum(data, len, data + len)); -} - -static int t1_verify_checksum(t1_state_t * t1, unsigned char *rbuf, - size_t len) -{ - unsigned char csum[2]; - unsigned long m, n = 0; - - m = len - t1->rc_bytes; - n = t1->rc_bytes; -// -// if (m < 0) -// return 0; - - t1->checksum(rbuf, m, csum); - if (!memcmp(rbuf + m, csum, n)) - return 1; - - return 0; -} - -/* - * Send/receive block - */ -static int t1_xcv(t1_state_t * t1, unsigned char *block, size_t slen, - size_t rmax) -{ - unsigned int n; - _ccid_descriptor *ccid_desc ; - int oldReadTimeout; - unsigned int rmax_int; - -// DEBUG_XXD("sending: ", block, slen); - - ccid_desc = get_ccid_descriptor(t1->lun); - oldReadTimeout = ccid_desc->readTimeout; - - if (t1->wtx > 1) { - /* set the new temporary timeout at WTX card request */ - ccid_desc->readTimeout *= t1->wtx; -// DEBUG_INFO2("New timeout at WTX request: %d sec", -// ccid_desc->readTimeout); - } - - if (isCharLevel(t1->lun)) { - rmax = 3; - - n = (unsigned int)CCID_Transmit(t1 -> lun, (unsigned int)slen, block, rmax, t1->wtx); - if (n != IFD_SUCCESS) - return n; - - /* the second argument of CCID_Receive() is (unsigned int *) - * so we can't use &rmax since &rmax is a (size_t *) and may not - * be the same on 64-bits architectures for example (iMac G5) */ - rmax_int = (unsigned int)rmax; - n = (unsigned int)CCID_Receive(t1 -> lun, &rmax_int, block, NULL); - - if (n == IFD_PARITY_ERROR) - return -2; - if (n != IFD_SUCCESS) - return -1; - - rmax = block[2] + 1; - - n = (unsigned int)CCID_Transmit(t1 -> lun, 0, block, rmax, t1->wtx); - if (n != IFD_SUCCESS) - return n; - - rmax_int = (unsigned int)rmax; - n = (unsigned int)CCID_Receive(t1 -> lun, &rmax_int, &block[3], NULL); - rmax = rmax_int; - if (n == IFD_PARITY_ERROR) - return -2; - if (n != IFD_SUCCESS) - return -1; - - n = (unsigned int)rmax + 3; - } else { - n = (unsigned int)CCID_Transmit(t1 -> lun, (unsigned int)slen, block, 0, t1->wtx); - t1->wtx = 0; /* reset to default value */ - if (n != IFD_SUCCESS) - return n; - - /* Get the response en bloc */ - rmax_int = (unsigned int)rmax; - n = (unsigned int)CCID_Receive(t1 -> lun, &rmax_int, block, NULL); - rmax = rmax_int; - if (n == IFD_PARITY_ERROR) - return -2; - if (n != IFD_SUCCESS) - return -1; - - n = (unsigned int)rmax; - } - -// if (n >= 0) -// { -// m = block[2] + 3 + t1->rc_bytes; -// if (m < n) -// n = m; -// } - - //if (n >= 0) - ;// DEBUG_XXD("received: ", block, n); - - /* Restore initial timeout */ - ccid_desc->readTimeout = oldReadTimeout; - - return n; -} - -int t1_negotiate_ifsd(t1_state_t * t1, unsigned int dad, int ifsd) -{ - ct_buf_t sbuf; - unsigned char sdata[T1_BUFFER_SIZE]; - unsigned int slen; - unsigned int retries; - size_t snd_len; - int n; - unsigned char snd_buf[1]; - - retries = t1->retries; - - /* S-block IFSD request */ - snd_buf[0] = ifsd; - snd_len = 1; - - /* Initialize send/recv buffer */ - ct_buf_set(&sbuf, (void *)snd_buf, (unsigned int)snd_len); - - while (TRUE) { - /* Build the block */ - slen = t1_build(t1, sdata, 0, T1_S_BLOCK | T1_S_IFS, &sbuf, NULL); - - /* Send the block */ - n = t1_xcv(t1, sdata, slen, sizeof(sdata)); - - retries--; - /* ISO 7816-3 Rule 7.4.2 */ - if (retries == 0) - goto error; - - if (-1 == n) { -// DEBUG_CRITICAL("fatal: transmit/receive failed"); - goto error; - } - - if ((-2 == n) /* Parity error */ - || (sdata[DATA] != ifsd) /* Wrong ifsd received */ - || (sdata[NAD] != swap_nibbles(dad)) /* wrong NAD */ - || (!t1_verify_checksum(t1, sdata, n)) /* checksum failed */ - || (n != 4 + (int)t1->rc_bytes) /* wrong frame length */ - || (sdata[LEN] != 1) /* wrong data length */ - || (sdata[PCB] != (T1_S_BLOCK | T1_S_RESPONSE | T1_S_IFS))) /* wrong PCB */ - continue; - - /* no more error */ - goto done; - } - -done: - return n; - -error: - t1->state = DEAD; - return -1; -} +/* + * Implementation of T=1 + * + * Copyright (C) 2003, Olaf Kirch + * + * improvements by: + * Copyright (C) 2004 Ludovic Rousseau + */ + +#include "buffer.h" +#include "proto-t1.h" +#include "checksum.h" + + +#include "ft_ccid.h" + +#include +#include + +/* I block */ +#define T1_I_SEQ_SHIFT 6 + +/* R block */ +#define T1_IS_ERROR(pcb) ((pcb) & 0x0F) +#define T1_EDC_ERROR 0x01 +#define T1_OTHER_ERROR 0x02 +#define T1_R_SEQ_SHIFT 4 + +/* S block stuff */ +#define T1_S_IS_RESPONSE(pcb) ((pcb) & T1_S_RESPONSE) +#define T1_S_TYPE(pcb) ((pcb) & 0x0F) +#define T1_S_RESPONSE 0x20 +#define T1_S_RESYNC 0x00 +#define T1_S_IFS 0x01 +#define T1_S_ABORT 0x02 +#define T1_S_WTX 0x03 + +#define swap_nibbles(x) ( (x >> 4) | ((x & 0xF) << 4) ) + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +#endif + +#define NAD 0 +#define PCB 1 +#define LEN 2 +#define DATA 3 + +/* internal state, do not mess with it. */ +/* should be != DEAD after reset/init */ +enum { + SENDING, RECEIVING, RESYNCH, DEAD +}; + +static void t1_set_checksum(t1_state_t *, int); +static unsigned int t1_block_type(unsigned char); +static unsigned int t1_seq(unsigned char); +static unsigned int t1_rebuild(t1_state_t *t1, unsigned char *block); +static unsigned int t1_compute_checksum(t1_state_t *, unsigned char *, size_t); +static int t1_verify_checksum(t1_state_t *, unsigned char *, size_t); +static int t1_xcv(t1_state_t *, unsigned char *, size_t, size_t); + + +/***************************************************************************** + * + * isCharLevel + * + ****************************************************************************/ +int isCharLevel(int reader_index) +{ + return 0;//haozi return CCID_CLASS_CHARACTER == (get_ccid_descriptor(reader_index)->dwFeatures & CCID_CLASS_EXCHANGE_MASK); +} /* isCharLevel */ + + + +/* + * Set default T=1 protocol parameters + */ +static void t1_set_defaults(t1_state_t * t1) +{ + t1->retries = 3; + /* This timeout is rather insane, but we need this right now + * to support cryptoflex keygen */ + t1->ifsc = 32; + t1->ifsd = 32; + t1->nr = 0; + t1->ns = 0; + t1->wtx = 0; +} + +static void t1_set_checksum(t1_state_t * t1, int csum) +{ + switch (csum) { + case IFD_PROTOCOL_T1_CHECKSUM_LRC: + t1->rc_bytes = 1; + t1->checksum = csum_lrc_compute; + break; + case IFD_PROTOCOL_T1_CHECKSUM_CRC: + t1->rc_bytes = 2; + t1->checksum = csum_crc_compute; + break; + } +} + +/* + * Attach t1 protocol + */ +int t1_init(t1_state_t * t1, int lun) +{ + t1_set_defaults(t1); + t1_set_param(t1, IFD_PROTOCOL_T1_CHECKSUM_LRC, 0); + t1_set_param(t1, IFD_PROTOCOL_T1_STATE, SENDING); + t1_set_param(t1, IFD_PROTOCOL_T1_MORE, FALSE); + + t1->lun = lun; + + return 0; +} + +/* + * Detach t1 protocol + */ +void t1_release(/*@unused@*/ t1_state_t * t1) +{ + (void)t1; + /* NOP */ +} + +/* + * Get/set parmaters for T1 protocol + */ +int t1_set_param(t1_state_t * t1, int type, long value) +{ + switch (type) { + case IFD_PROTOCOL_T1_CHECKSUM_LRC: + case IFD_PROTOCOL_T1_CHECKSUM_CRC: + t1_set_checksum(t1, type); + break; + case IFD_PROTOCOL_T1_IFSC: + t1->ifsc = (unsigned int)value; + break; + case IFD_PROTOCOL_T1_IFSD: + t1->ifsd = (unsigned int)value; + break; + case IFD_PROTOCOL_T1_STATE: + t1->state = (unsigned int)value; + break; + case IFD_PROTOCOL_T1_MORE: + t1->more = value; + break; + default: + ;//DEBUG_INFO2("Unsupported parameter %d", type); + return -1; + } + + return 0; +} + +/* + * Send an APDU through T=1 + */ +int t1_transceive(t1_state_t * t1, unsigned int dad, + const void *snd_buf, size_t snd_len, + void *rcv_buf, size_t rcv_len) +{ + ct_buf_t sbuf, rbuf, tbuf; + unsigned char sdata[T1_BUFFER_SIZE], sblk[5]; + unsigned int slen, retries, resyncs, sent_length = 0; + size_t last_send = 0; + + if (snd_len == 0) + return -1; + + /* we can't talk to a dead card / reader. Reset it! */ + if (t1->state == DEAD) + { + //DEBUG_CRITICAL("T=1 state machine is DEAD. Reset the card first."); + return -1; + } + + t1->state = SENDING; + retries = t1->retries; + resyncs = 3; + + /* Initialize send/recv buffer */ + ct_buf_set(&sbuf, (void *)snd_buf, (unsigned int)snd_len); + ct_buf_init(&rbuf, rcv_buf, (unsigned int)rcv_len); + + /* Send the first block */ + slen = t1_build(t1, sdata, dad, T1_I_BLOCK, &sbuf, &last_send); + + while (1) { + unsigned char pcb; + int n; + + retries--; + + n = t1_xcv(t1, sdata, slen, sizeof(sdata)); + if (-2 == n) + { + // DEBUG_COMM("Parity error"); + /* ISO 7816-3 Rule 7.4.2 */ + if (retries == 0) + goto resync; + + /* ISO 7816-3 Rule 7.2 */ + if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) + { +// DEBUG_COMM("Rule 7.2"); + slen = t1_rebuild(t1, sdata); + continue; + } + + slen = t1_build(t1, sdata, + dad, T1_R_BLOCK | T1_EDC_ERROR, + NULL, NULL); + continue; + } + + if (n < 0) { +// DEBUG_CRITICAL("fatal: transmit/receive failed"); + t1->state = DEAD; + goto error; + } + + if ((sdata[NAD] != swap_nibbles(dad)) /* wrong NAD */ + || (sdata[LEN] == 0xFF)) /* length == 0xFF (illegal) */ + { +// DEBUG_COMM("R-BLOCK required"); + /* ISO 7816-3 Rule 7.4.2 */ + if (retries == 0) + goto resync; + + /* ISO 7816-3 Rule 7.2 */ + if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) + { +// DEBUG_COMM("Rule 7.2"); + slen = t1_rebuild(t1, sdata); + continue; + } + + slen = t1_build(t1, sdata, + dad, T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + + if (!t1_verify_checksum(t1, sdata, n)) { +// DEBUG_COMM("checksum failed"); + /* ISO 7816-3 Rule 7.4.2 */ + if (retries == 0) + goto resync; + + /* ISO 7816-3 Rule 7.2 */ + if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) + { +// DEBUG_COMM("Rule 7.2"); + slen = t1_rebuild(t1, sdata); + continue; + } + + slen = t1_build(t1, sdata, + dad, T1_R_BLOCK | T1_EDC_ERROR, + NULL, NULL); + continue; + } + + pcb = sdata[PCB]; + switch (t1_block_type(pcb)) { + case T1_R_BLOCK: + if ((sdata[LEN] != 0x00) /* length != 0x00 (illegal) */ + || (pcb & 0x20) /* b6 of pcb is set */ + ) + { +// DEBUG_COMM("R-Block required"); + /* ISO 7816-3 Rule 7.4.2 */ + if (retries == 0) + goto resync; + + /* ISO 7816-3 Rule 7.2 */ + if (T1_R_BLOCK == t1_block_type(t1->previous_block[1])) + { +// DEBUG_COMM("Rule 7.2"); + slen = t1_rebuild(t1, sdata); + continue; + } + + slen = t1_build(t1, sdata, + dad, T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + + if (((t1_seq(pcb) != t1->ns) /* wrong sequence number & no bit more */ + && ! t1->more) + ) + { +// DEBUG_COMM4("received: %d, expected: %d, more: %d", +// t1_seq(pcb), t1->ns, t1->more); + + /* ISO 7816-3 Rule 7.2 */ + if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) + { +// DEBUG_COMM("Rule 7.2"); + slen = t1_rebuild(t1, sdata); + continue; + } + +// DEBUG_COMM("R-Block required"); + /* ISO 7816-3 Rule 7.4.2 */ + if (retries == 0) + goto resync; + slen = t1_build(t1, sdata, + dad, T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + + if (t1->state == RECEIVING) { + /* ISO 7816-3 Rule 7.2 */ + if (T1_R_BLOCK == t1_block_type(t1->previous_block[1])) + { +// DEBUG_COMM("Rule 7.2"); + slen = t1_rebuild(t1, sdata); + continue; + } + +// DEBUG_COMM(""); + slen = t1_build(t1, sdata, + dad, T1_R_BLOCK, + NULL, NULL); + break; + } + + /* If the card terminal requests the next + * sequence number, it received the previous + * block successfully */ + if (t1_seq(pcb) != t1->ns) { + ct_buf_get(&sbuf, NULL, (unsigned int)last_send); + sent_length += last_send; + last_send = 0; + t1->ns ^= 1; + } + + /* If there's no data available, the ICC + * shouldn't be asking for more */ + if (ct_buf_avail(&sbuf) == 0) + goto resync; + + slen = t1_build(t1, sdata, dad, T1_I_BLOCK, + &sbuf, &last_send); + break; + + case T1_I_BLOCK: + /* The first I-block sent by the ICC indicates + * the last block we sent was received successfully. */ + if (t1->state == SENDING) { +// DEBUG_COMM(""); + ct_buf_get(&sbuf, NULL, (unsigned int)last_send); + last_send = 0; + t1->ns ^= 1; + } + + t1->state = RECEIVING; + + /* If the block sent by the card doesn't match + * what we expected it to send, reply with + * an R block */ + if (t1_seq(pcb) != t1->nr) { +// DEBUG_COMM("wrong nr"); + slen = t1_build(t1, sdata, dad, + T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + + t1->nr ^= 1; + + if (ct_buf_put(&rbuf, sdata + 3, sdata[LEN]) < 0) + { +// DEBUG_CRITICAL2("buffer overrun by %d bytes", sdata[LEN] - (rbuf.size - rbuf.tail)); + goto error; + } + + if ((pcb & T1_MORE_BLOCKS) == 0) + goto done; + + slen = t1_build(t1, sdata, dad, T1_R_BLOCK, NULL, NULL); + break; + + case T1_S_BLOCK: + if (T1_S_IS_RESPONSE(pcb) && t1->state == RESYNCH) { + /* ISO 7816-3 Rule 6.2 */ +// DEBUG_COMM("S-Block answer received"); + /* ISO 7816-3 Rule 6.3 */ + t1->state = SENDING; + sent_length = 0; + last_send = 0; + resyncs = 3; + retries = t1->retries; + ct_buf_init(&rbuf, rcv_buf, (unsigned int)rcv_len); + slen = t1_build(t1, sdata, dad, T1_I_BLOCK, + &sbuf, &last_send); + continue; + } + + if (T1_S_IS_RESPONSE(pcb)) + { + /* ISO 7816-3 Rule 7.4.2 */ + if (retries == 0) + goto resync; + + /* ISO 7816-3 Rule 7.2 */ + if (T1_R_BLOCK == t1_block_type(t1->previous_block[PCB])) + { +// DEBUG_COMM("Rule 7.2"); + slen = t1_rebuild(t1, sdata); + continue; + } + +// DEBUG_CRITICAL("wrong response S-BLOCK received"); + slen = t1_build(t1, sdata, + dad, T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + + ct_buf_init(&tbuf, sblk, sizeof(sblk)); + +// DEBUG_COMM("S-Block request received"); + switch (T1_S_TYPE(pcb)) { + case T1_S_RESYNC: + if (sdata[LEN] != 0) + { +// DEBUG_COMM2("Wrong length: %d", sdata[LEN]); + slen = t1_build(t1, sdata, dad, + T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + +// DEBUG_COMM("Resync requested"); + /* the card is not allowed to send a resync. */ + goto resync; + + case T1_S_ABORT: + if (sdata[LEN] != 0) + { +// DEBUG_COMM2("Wrong length: %d", sdata[LEN]); + slen = t1_build(t1, sdata, dad, + T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + + /* ISO 7816-3 Rule 9 */ +// DEBUG_CRITICAL("abort requested"); + break; + + case T1_S_IFS: + if (sdata[LEN] != 1) + { +// DEBUG_COMM2("Wrong length: %d", sdata[LEN]); + slen = t1_build(t1, sdata, dad, + T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + +// DEBUG_CRITICAL2("CT sent S-block with ifs=%u", sdata[DATA]); + if (sdata[DATA] == 0) + goto resync; + t1->ifsc = sdata[DATA]; + ct_buf_putc(&tbuf, sdata[DATA]); + break; + + case T1_S_WTX: + if (sdata[LEN] != 1) + { +// DEBUG_COMM2("Wrong length: %d", sdata[LEN]); + slen = t1_build(t1, sdata, dad, + T1_R_BLOCK | T1_OTHER_ERROR, + NULL, NULL); + continue; + } + +// DEBUG_COMM2("CT sent S-block with wtx=%u", sdata[DATA]); + t1->wtx = sdata[DATA]; + ct_buf_putc(&tbuf, sdata[DATA]); + break; + + default: +// DEBUG_CRITICAL2("T=1: Unknown S block type 0x%02x", T1_S_TYPE(pcb)); + goto resync; + } + + slen = t1_build(t1, sdata, dad, + T1_S_BLOCK | T1_S_RESPONSE | T1_S_TYPE(pcb), + &tbuf, NULL); + } + + /* Everything went just splendid */ + retries = t1->retries; + continue; + +resync: + /* the number or resyncs is limited, too */ + /* ISO 7816-3 Rule 6.4 */ + if (resyncs == 0) + goto error; + + /* ISO 7816-3 Rule 6 */ + resyncs--; + t1->ns = 0; + t1->nr = 0; + slen = t1_build(t1, sdata, dad, T1_S_BLOCK | T1_S_RESYNC, NULL, + NULL); + t1->state = RESYNCH; + t1->more = FALSE; + retries = 1; + continue; + } + +done: + return ct_buf_avail(&rbuf); + +error: + t1->state = DEAD; + return -1; +} + +static unsigned t1_block_type(unsigned char pcb) +{ + switch (pcb & 0xC0) { + case T1_R_BLOCK: + return T1_R_BLOCK; + case T1_S_BLOCK: + return T1_S_BLOCK; + default: + return T1_I_BLOCK; + } +} + +static unsigned int t1_seq(unsigned char pcb) +{ + switch (pcb & 0xC0) { + case T1_R_BLOCK: + return (pcb >> T1_R_SEQ_SHIFT) & 1; + case T1_S_BLOCK: + return 0; + default: + return (pcb >> T1_I_SEQ_SHIFT) & 1; + } +} + +unsigned int t1_build(t1_state_t * t1, unsigned char *block, + unsigned char dad, unsigned char pcb, + ct_buf_t *bp, size_t *lenp) +{ + unsigned int len; + char more = FALSE; + + len = bp ? ct_buf_avail(bp) : 0; + if (len > t1->ifsc) { + pcb |= T1_MORE_BLOCKS; + len = t1->ifsc; + more = TRUE; + } + + /* Add the sequence number */ + switch (t1_block_type(pcb)) { + case T1_R_BLOCK: + pcb |= t1->nr << T1_R_SEQ_SHIFT; + break; + case T1_I_BLOCK: + pcb |= t1->ns << T1_I_SEQ_SHIFT; + t1->more = more; +// DEBUG_COMM2("more bit: %d", more); + break; + } + + block[0] = dad; + block[1] = pcb; + block[2] = len; + + if (len) + memcpy(block + 3, ct_buf_head(bp), len); + if (lenp) + *lenp = len; + + len = t1_compute_checksum(t1, block, len + 3); + + /* memorize the last sent block */ + /* only 4 bytes since we are only interesed in R-blocks */ + memcpy(t1->previous_block, block, 4); + + return len; +} + +static unsigned int +t1_rebuild(t1_state_t *t1, unsigned char *block) +{ + unsigned char pcb = t1 -> previous_block[1]; + + /* copy the last sent block */ + if (T1_R_BLOCK == t1_block_type(pcb)) + memcpy(block, t1 -> previous_block, 4); + else + { + return 0; + } + + return 4; +} + +/* + * Build/verify checksum + */ +static unsigned int t1_compute_checksum(t1_state_t * t1, + unsigned char *data, size_t len) +{ + return (unsigned int)(len + t1->checksum(data, len, data + len)); +} + +static int t1_verify_checksum(t1_state_t * t1, unsigned char *rbuf, + size_t len) +{ + unsigned char csum[2]; + unsigned long m, n = 0; + + m = len - t1->rc_bytes; + n = t1->rc_bytes; +// +// if (m < 0) +// return 0; + + t1->checksum(rbuf, m, csum); + if (!memcmp(rbuf + m, csum, n)) + return 1; + + return 0; +} + +/* + * Send/receive block + */ +static int t1_xcv(t1_state_t * t1, unsigned char *block, size_t slen, + size_t rmax) +{ + unsigned int n; + _ccid_descriptor *ccid_desc ; + int oldReadTimeout; + unsigned int rmax_int; + +// DEBUG_XXD("sending: ", block, slen); + + ccid_desc = get_ccid_descriptor(t1->lun); + oldReadTimeout = ccid_desc->readTimeout; + + if (t1->wtx > 1) + { + /* set the new temporary timeout at WTX card request */ + ccid_desc->readTimeout *= t1->wtx; +// DEBUG_INFO2("New timeout at WTX request: %d sec", +// ccid_desc->readTimeout); + } + + if (isCharLevel(t1->lun)) + { + rmax = 3; + + n = (unsigned int)CCID_Transmit(t1 -> lun, (unsigned int)slen, block, rmax, t1->wtx); + if (n != IFD_SUCCESS) + return n; + + /* the second argument of CCID_Receive() is (unsigned int *) + * so we can't use &rmax since &rmax is a (size_t *) and may not + * be the same on 64-bits architectures for example (iMac G5) */ + rmax_int = (unsigned int)rmax; + n = (unsigned int)CCID_Receive(t1 -> lun, &rmax_int, block, NULL); + + if (n == IFD_PARITY_ERROR) + return -2; + if (n != IFD_SUCCESS) + return -1; + + rmax = block[2] + 1; + + n = (unsigned int)CCID_Transmit(t1 -> lun, 0, block, rmax, t1->wtx); + if (n != IFD_SUCCESS) + return n; + + rmax_int = (unsigned int)rmax; + n = (unsigned int)CCID_Receive(t1 -> lun, &rmax_int, &block[3], NULL); + rmax = rmax_int; + if (n == IFD_PARITY_ERROR) + return -2; + if (n != IFD_SUCCESS) + return -1; + + n = (unsigned int)rmax + 3; + } + else + { + n = (unsigned int)CCID_Transmit(t1 -> lun, (unsigned int)slen, block, 0, t1->wtx); + t1->wtx = 0; /* reset to default value */ + if (n != IFD_SUCCESS) + return n; + + /* Get the response en bloc */ + rmax_int = (unsigned int)rmax; + n = (unsigned int)CCID_Receive(t1 -> lun, &rmax_int, block, NULL); + rmax = rmax_int; + if (n == IFD_PARITY_ERROR) + return -2; + if (n != IFD_SUCCESS) + return -1; + + n = (unsigned int)rmax; + } + +// if (n >= 0) +// { +// m = block[2] + 3 + t1->rc_bytes; +// if (m < n) +// n = m; +// } + + //if (n >= 0) + ;// DEBUG_XXD("received: ", block, n); + + /* Restore initial timeout */ + ccid_desc->readTimeout = oldReadTimeout; + + return n; +} + +int t1_negotiate_ifsd(t1_state_t * t1, unsigned int dad, int ifsd) +{ + ct_buf_t sbuf; + unsigned char sdata[T1_BUFFER_SIZE]; + unsigned int slen; + unsigned int retries; + size_t snd_len; + int n; + unsigned char snd_buf[1]; + + retries = t1->retries; + + /* S-block IFSD request */ + snd_buf[0] = ifsd; + snd_len = 1; + + /* Initialize send/recv buffer */ + ct_buf_set(&sbuf, (void *)snd_buf, (unsigned int)snd_len); + + while (TRUE) + { + /* Build the block */ + slen = t1_build(t1, sdata, 0, T1_S_BLOCK | T1_S_IFS, &sbuf, NULL); + + /* Send the block */ + n = t1_xcv(t1, sdata, slen, sizeof(sdata)); + + retries--; + /* ISO 7816-3 Rule 7.4.2 */ + if (retries == 0) + goto error; + + if (-1 == n) + { +// DEBUG_CRITICAL("fatal: transmit/receive failed"); + goto error; + } + + if ((-2 == n) /* Parity error */ + || (sdata[DATA] != ifsd) /* Wrong ifsd received */ + || (sdata[NAD] != swap_nibbles(dad)) /* wrong NAD */ + || (!t1_verify_checksum(t1, sdata, n)) /* checksum failed */ + || (n != 4 + (int)t1->rc_bytes) /* wrong frame length */ + || (sdata[LEN] != 1) /* wrong data length */ + || (sdata[PCB] != (T1_S_BLOCK | T1_S_RESPONSE | T1_S_IFS))) /* wrong PCB */ + continue; + + /* no more error */ + goto done; + } + +done: + return n; + +error: + t1->state = DEAD; + return -1; +} diff --git a/libs/source/src/proto-t1.h b/libs/source/src/proto-t1.h index 7cd1ac3..6c3c685 100755 --- a/libs/source/src/proto-t1.h +++ b/libs/source/src/proto-t1.h @@ -1,85 +1,85 @@ -/* - * Implementation of T=1 - * - * Copyright (C) 2003, Olaf Kirch - * - * improvements by: - * Copyright (C) 2004 Ludovic Rousseau - */ - -/* $Id: proto-t1.h 3292 2009-01-26 13:02:58Z rousseau $ */ - -#ifndef __PROTO_T1_H__ -#define __PROTO_T1_H__ - - - -#include - -#include - -#include "buffer.h" -#ifdef __cplusplus -extern "C" { -#endif - - - /* T=1 protocol constants */ -#define T1_I_BLOCK 0x00 -#define T1_R_BLOCK 0x80 -#define T1_S_BLOCK 0xC0 -#define T1_MORE_BLOCKS 0x20 - - enum { - IFD_PROTOCOL_RECV_TIMEOUT = 0x0000, - IFD_PROTOCOL_T1_BLOCKSIZE, - IFD_PROTOCOL_T1_CHECKSUM_CRC, - IFD_PROTOCOL_T1_CHECKSUM_LRC, - IFD_PROTOCOL_T1_IFSC, - IFD_PROTOCOL_T1_IFSD, - IFD_PROTOCOL_T1_STATE, - IFD_PROTOCOL_T1_MORE - }; - -#define T1_BUFFER_SIZE (3 + 254 + 2) - - /* see /usr/include/PCSC/ifdhandler.h for other values - * this one is for internal use only */ -#define IFD_PARITY_ERROR 699 - - typedef struct { - int lun; - int state; - - unsigned char ns; /* reader side */ - unsigned char nr; /* card side */ - unsigned int ifsc; - unsigned int ifsd; - - unsigned char wtx; - unsigned int retries; - unsigned int rc_bytes; - - unsigned int (*checksum)(const uint8_t *, size_t, unsigned char *); - - char more; /* more data bit */ - unsigned char previous_block[4]; /* to store the last R-block */ - } t1_state_t; - - int t1_transceive(t1_state_t *t1, unsigned int dad, - const void *snd_buf, size_t snd_len, - void *rcv_buf, size_t rcv_len); - int t1_init(t1_state_t *t1, int lun); - void t1_release(t1_state_t *t1); - int t1_set_param(t1_state_t *t1, int type, long value); - int t1_negotiate_ifsd(t1_state_t *t1, unsigned int dad, int ifsd); - unsigned int t1_build(t1_state_t *, unsigned char *, - unsigned char, unsigned char, ct_buf_t *, size_t *); - - -#ifdef __cplusplus -} -#endif - -#endif - +/* + * Implementation of T=1 + * + * Copyright (C) 2003, Olaf Kirch + * + * improvements by: + * Copyright (C) 2004 Ludovic Rousseau + */ + +/* $Id: proto-t1.h 3292 2009-01-26 13:02:58Z rousseau $ */ + +#ifndef __PROTO_T1_H__ +#define __PROTO_T1_H__ + + + +#include + +#include + +#include "buffer.h" +#ifdef __cplusplus +extern "C" { +#endif + + +/* T=1 protocol constants */ +#define T1_I_BLOCK 0x00 +#define T1_R_BLOCK 0x80 +#define T1_S_BLOCK 0xC0 +#define T1_MORE_BLOCKS 0x20 + +enum { + IFD_PROTOCOL_RECV_TIMEOUT = 0x0000, + IFD_PROTOCOL_T1_BLOCKSIZE, + IFD_PROTOCOL_T1_CHECKSUM_CRC, + IFD_PROTOCOL_T1_CHECKSUM_LRC, + IFD_PROTOCOL_T1_IFSC, + IFD_PROTOCOL_T1_IFSD, + IFD_PROTOCOL_T1_STATE, + IFD_PROTOCOL_T1_MORE +}; + +#define T1_BUFFER_SIZE (3 + 254 + 2) + +/* see /usr/include/PCSC/ifdhandler.h for other values + * this one is for internal use only */ +#define IFD_PARITY_ERROR 699 + +typedef struct { + int lun; + int state; + + unsigned char ns; /* reader side */ + unsigned char nr; /* card side */ + unsigned int ifsc; + unsigned int ifsd; + + unsigned char wtx; + unsigned int retries; + unsigned int rc_bytes; + + unsigned int (*checksum)(const uint8_t *, size_t, unsigned char *); + + char more; /* more data bit */ + unsigned char previous_block[4]; /* to store the last R-block */ +} t1_state_t; + +int t1_transceive(t1_state_t *t1, unsigned int dad, + const void *snd_buf, size_t snd_len, + void *rcv_buf, size_t rcv_len); +int t1_init(t1_state_t *t1, int lun); +void t1_release(t1_state_t *t1); +int t1_set_param(t1_state_t *t1, int type, long value); +int t1_negotiate_ifsd(t1_state_t *t1, unsigned int dad, int ifsd); +unsigned int t1_build(t1_state_t *, unsigned char *, + unsigned char, unsigned char, ct_buf_t *, size_t *); + + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/libs/source/src/winscard.h b/libs/source/src/winscard.h index 96438c5..43d6e7d 100755 --- a/libs/source/src/winscard.h +++ b/libs/source/src/winscard.h @@ -1,157 +1,159 @@ -/* - * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html ) - * - * Copyright (C) 1999-2003 - * David Corcoran - * Copyright (C) 2002-2009 - * Ludovic Rousseau - * - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - Changes to this license can be made only by the copyright author with - explicit written consent. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $Id: winscard.h 6851 2014-02-14 15:43:32Z rousseau $ - */ - -/** - * @file - * @brief This handles smart card reader communications. - */ - -#ifndef __winscard_h__ -#define __winscard_h__ - - -#include "wintypes.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#ifndef PCSC_API -#define PCSC_API -#endif - -#define MAX_ATR_SIZE 33 - - typedef LONG SCARDCONTEXT; /**< \p hContext returned by SCardEstablishContext() */ - typedef SCARDCONTEXT *PSCARDCONTEXT; - typedef SCARDCONTEXT *LPSCARDCONTEXT; - typedef LONG SCARDHANDLE; /**< \p hCard returned by SCardConnect() */ - typedef SCARDHANDLE *PSCARDHANDLE; - typedef SCARDHANDLE *LPSCARDHANDLE; - - - typedef struct { - const char *szReader; - const char *pvUserData; - DWORD dwCurrentState; - DWORD dwEventState; - DWORD cbAtr; - DWORD isConnected; - unsigned char rgbAtr[MAX_ATR_SIZE]; - } - SCARD_READERSTATE, *LPSCARD_READERSTATE; - /** Protocol Control Information (PCI) */ - typedef struct _SCARD_IO_REQUEST { - uint32_t dwProtocol; /**< Protocol identifier */ - uint32_t cbPciLength; /**< Protocol Control Inf Length */ - } - SCARD_IO_REQUEST, *PSCARD_IO_REQUEST, *LPSCARD_IO_REQUEST; - - typedef const SCARD_IO_REQUEST *LPCSCARD_IO_REQUEST; - - extern SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci, - g_rgSCardRawPci; - - PCSC_API LONG SCardEstablishContext(DWORD dwScope, - /*@null@*/ LPCVOID pvReserved1, /*@null@*/ LPCVOID pvReserved2, - /*@out@*/ LPSCARDCONTEXT phContext); - - PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext); - - PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext); - - PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, - LPCSTR szReader, - DWORD dwShareMode, - DWORD dwPreferredProtocols, - /*@out@*/ LPSCARDHANDLE phCard, /*@out@*/ LPDWORD pdwActiveProtocol); - - PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition); - - PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard); - - PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition); - - PCSC_API LONG SCardStatus(SCARDHANDLE hCard, - /*@null@*/ /*@out@*/ LPSTR mszReaderName, - /*@null@*/ /*@out@*/ LPDWORD pcchReaderLen, - /*@null@*/ /*@out@*/ LPDWORD pdwState, - /*@null@*/ /*@out@*/ LPDWORD pdwProtocol, - /*@null@*/ /*@out@*/ LPBYTE pbAtr, - /*@null@*/ /*@out@*/ LPDWORD pcbAtrLen); - - PCSC_API LONG SCardGetStatusChange(SCARDCONTEXT hContext, - DWORD dwTimeout, - LPSCARD_READERSTATE rgReaderStates, DWORD cReaders); - - PCSC_API LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, - LPCVOID pbSendBuffer, DWORD cbSendLength, - /*@out@*/ LPVOID pbRecvBuffer, DWORD cbRecvLength, - LPDWORD lpBytesReturned); - - PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, - const SCARD_IO_REQUEST *pioSendPci, - LPCBYTE pbSendBuffer, DWORD cbSendLength, - /*@out@*/ SCARD_IO_REQUEST *pioRecvPci, - /*@out@*/ LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength); - - PCSC_API LONG SCardListReaderGroups(SCARDCONTEXT hContext, - /*@out@*/ LPSTR mszGroups, LPDWORD pcchGroups); - - PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, - /*@null@*/ /*@out@*/ LPCSTR mszGroups, - /*@null@*/ /*@out@*/ LPSTR mszReaders, - /*@out@*/ LPDWORD pcchReaders); - - PCSC_API LONG SCardCancel(SCARDCONTEXT hContext); - PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, - DWORD dwPreferredProtocols, DWORD dwInitialization, - LPDWORD pdwActiveProtocol); - - PCSC_API LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, - /*@out@*/ LPBYTE pbAttr, LPDWORD pcbAttrLen); - PCSC_API LONG SCardSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout); - - - -#ifdef __cplusplus -} -#endif - -#endif - +/* + * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html ) + * + * Copyright (C) 1999-2003 + * David Corcoran + * Copyright (C) 2002-2009 + * Ludovic Rousseau + * + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + Changes to this license can be made only by the copyright author with + explicit written consent. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $Id: winscard.h 6851 2014-02-14 15:43:32Z rousseau $ + */ + +/** + * @file + * @brief This handles smart card reader communications. + */ + +#ifndef __winscard_h__ +#define __winscard_h__ + + +#include "wintypes.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +#ifndef PCSC_API +#define PCSC_API +#endif + +#define MAX_ATR_SIZE 33 + + typedef LONG SCARDCONTEXT; /**< \p hContext returned by SCardEstablishContext() */ + typedef SCARDCONTEXT *PSCARDCONTEXT; + typedef SCARDCONTEXT *LPSCARDCONTEXT; + typedef LONG SCARDHANDLE; /**< \p hCard returned by SCardConnect() */ + typedef SCARDHANDLE *PSCARDHANDLE; + typedef SCARDHANDLE *LPSCARDHANDLE; + + + typedef struct + { + const char *szReader; + const char *pvUserData; + DWORD dwCurrentState; + DWORD dwEventState; + DWORD cbAtr; + DWORD isConnected; + unsigned char rgbAtr[MAX_ATR_SIZE]; + } + SCARD_READERSTATE, *LPSCARD_READERSTATE; + /** Protocol Control Information (PCI) */ + typedef struct _SCARD_IO_REQUEST + { + uint32_t dwProtocol; /**< Protocol identifier */ + uint32_t cbPciLength; /**< Protocol Control Inf Length */ + } + SCARD_IO_REQUEST, *PSCARD_IO_REQUEST, *LPSCARD_IO_REQUEST; + + typedef const SCARD_IO_REQUEST *LPCSCARD_IO_REQUEST; + + extern SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci, + g_rgSCardRawPci; + + PCSC_API LONG SCardEstablishContext(DWORD dwScope, + /*@null@*/ LPCVOID pvReserved1, /*@null@*/ LPCVOID pvReserved2, + /*@out@*/ LPSCARDCONTEXT phContext); + + PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext); + + PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext); + + PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, + LPCSTR szReader, + DWORD dwShareMode, + DWORD dwPreferredProtocols, + /*@out@*/ LPSCARDHANDLE phCard, /*@out@*/ LPDWORD pdwActiveProtocol); + + PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition); + + PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard); + + PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition); + + PCSC_API LONG SCardStatus(SCARDHANDLE hCard, + /*@null@*/ /*@out@*/ LPSTR mszReaderName, + /*@null@*/ /*@out@*/ LPDWORD pcchReaderLen, + /*@null@*/ /*@out@*/ LPDWORD pdwState, + /*@null@*/ /*@out@*/ LPDWORD pdwProtocol, + /*@null@*/ /*@out@*/ LPBYTE pbAtr, + /*@null@*/ /*@out@*/ LPDWORD pcbAtrLen); + + PCSC_API LONG SCardGetStatusChange(SCARDCONTEXT hContext, + DWORD dwTimeout, + LPSCARD_READERSTATE rgReaderStates, DWORD cReaders); + + PCSC_API LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, + LPCVOID pbSendBuffer, DWORD cbSendLength, + /*@out@*/ LPVOID pbRecvBuffer, DWORD cbRecvLength, + LPDWORD lpBytesReturned); + + PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, + const SCARD_IO_REQUEST *pioSendPci, + LPCBYTE pbSendBuffer, DWORD cbSendLength, + /*@out@*/ SCARD_IO_REQUEST *pioRecvPci, + /*@out@*/ LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength); + + PCSC_API LONG SCardListReaderGroups(SCARDCONTEXT hContext, + /*@out@*/ LPSTR mszGroups, LPDWORD pcchGroups); + + PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, + /*@null@*/ /*@out@*/ LPCSTR mszGroups, + /*@null@*/ /*@out@*/ LPSTR mszReaders, + /*@out@*/ LPDWORD pcchReaders); + + PCSC_API LONG SCardCancel(SCARDCONTEXT hContext); + PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, + DWORD dwPreferredProtocols, DWORD dwInitialization, + LPDWORD pdwActiveProtocol); + + PCSC_API LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, + /*@out@*/ LPBYTE pbAttr, LPDWORD pcbAttrLen); + PCSC_API LONG SCardSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout); + + + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/libs/source/src/winscard.m b/libs/source/src/winscard.m index 8d25f5a..d20b13c 100755 --- a/libs/source/src/winscard.m +++ b/libs/source/src/winscard.m @@ -20,7 +20,7 @@ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - + 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright @@ -28,10 +28,10 @@ documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. - + Changes to this license can be made only by the copyright author with explicit written consent. - + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. @@ -44,7 +44,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $Id: winscard.c 6851 2014-02-14 15:43:32Z rousseau $ - + * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -102,14 +102,16 @@ //1.31.4 - Add get reader type API //1.31.5 - add auto pps //1.31.6 - add customer OEM reader support -//1.31.7 - fix bug while in close session, the behave is using bluetooth printer with bR301 and iR301, the reader session will close. -char Ft_iR301U_Version[3]={0x01,0x31,0x07}; +//1.31.7 - The version using by customer OEM +//1.31.9 - Fix bug while in close session, the behave is using bluetooth printer with bR301 and iR301, the reader session will close. +//1.32.0 - Fix block issue while in reading data from reader, the behave is random get 0x80100016 error, the error only happen with iR301 series +char Ft_iR301U_Version[3]={0x01,0x32,0x00}; volatile int eStablishContextCount = 0; volatile int eShCardHandleCount = 0; SCARD_IO_REQUEST g_rgSCardT0Pci= {T_0,0}, g_rgSCardT1Pci= {T_1,0}, - g_rgSCardRawPci= {T_RAW,0}; +g_rgSCardRawPci= {T_RAW,0}; @@ -118,67 +120,67 @@ LONG SCardEstablishContext(DWORD dwScope, /*@unused@*/ LPCVOID pvReserved1, /*@unused@*/ LPCVOID pvReserved2, LPSCARDCONTEXT phContext) { - - if ( SCARD_SCOPE_SYSTEM != dwScope ) { - return SCARD_E_INVALID_VALUE; - } - - if (NULL == phContext) { - return SCARD_E_INVALID_PARAMETER ; - } - - pthread_mutex_lock(&CommunicatonMutex); - - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); - ccid_descriptor->dwMaxCCIDMessageLength = 272; - ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; - eStablishContextCount = eStablishContextCount + 1; - - *phContext = (SCARDCONTEXT)ccid_descriptor; - - [[bR301SessionController sharedController] RegisterAccessoryConnectNotification]; - pthread_mutex_unlock(&CommunicatonMutex); - - return SCARD_S_SUCCESS; + + if ( SCARD_SCOPE_SYSTEM != dwScope ) { + return SCARD_E_INVALID_VALUE; + } + + if (NULL == phContext) { + return SCARD_E_INVALID_PARAMETER ; + } + + pthread_mutex_lock(&CommunicatonMutex); + + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); + ccid_descriptor->dwMaxCCIDMessageLength = 272; + ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; + eStablishContextCount = eStablishContextCount + 1; + + *phContext = (SCARDCONTEXT)ccid_descriptor; + + [[bR301SessionController sharedController] RegisterAccessoryConnectNotification]; + pthread_mutex_unlock(&CommunicatonMutex); + + return SCARD_S_SUCCESS; } LONG SCardReleaseContext(SCARDCONTEXT hContext) { - - if (0 == hContext) { - return SCARD_E_INVALID_HANDLE; - } - - hContext = 0; - - pthread_mutex_lock(&CommunicatonMutex); - if (eStablishContextCount <= 0) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_S_SUCCESS; - } - eStablishContextCount = eStablishContextCount - 1; - if (eStablishContextCount <= 0) { - [[bR301SessionController sharedController] UnRegisterAccessoryConnectNotification]; - } - pthread_mutex_unlock(&CommunicatonMutex); - - return SCARD_S_SUCCESS; + + if (0 == hContext) { + return SCARD_E_INVALID_HANDLE; + } + + hContext = 0; + + pthread_mutex_lock(&CommunicatonMutex); + if (eStablishContextCount <= 0) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_S_SUCCESS; + } + eStablishContextCount = eStablishContextCount - 1; + if (eStablishContextCount <= 0) { + [[bR301SessionController sharedController] UnRegisterAccessoryConnectNotification]; + } + pthread_mutex_unlock(&CommunicatonMutex); + + return SCARD_S_SUCCESS; } LONG SCardIsValidContext(SCARDCONTEXT hContext) { - if (0 == hContext) { - return SCARD_E_INVALID_HANDLE; - } - - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); - if(isbadreadptr(ccid_descriptor,sizeof(_ccid_descriptor))) { - return SCARD_E_INVALID_HANDLE; - } - - return SCARD_S_SUCCESS; + if (0 == hContext) { + return SCARD_E_INVALID_HANDLE; + } + + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); + if(isbadreadptr(ccid_descriptor,sizeof(_ccid_descriptor))) { + return SCARD_E_INVALID_HANDLE; + } + + return SCARD_S_SUCCESS; } LONG SCardListReaders(SCARDCONTEXT hContext, @@ -186,119 +188,119 @@ LONG SCardListReaders(SCARDCONTEXT hContext, /*@null@*/ /*@out@*/ LPSTR mszReaders, /*@out@*/ LPDWORD pcchReaders) { - int namelen = strlen("FT smartcard reader")+2; - - if (0 == hContext) { - return SCARD_E_INVALID_HANDLE; - } - - if (pcchReaders == NULL) { - - return SCARD_E_INVALID_PARAMETER; - } - pthread_mutex_lock(&CommunicatonMutex); - bR301SessionController *sessionController = [bR301SessionController sharedController]; - - pthread_mutex_unlock(&CommunicatonMutex); - if(0 == [sessionController identifyAccessoryCount]) { - return SCARD_E_READER_UNAVAILABLE; - } - - if (mszReaders != NULL) { - if (*pcchReaders < namelen) { - return SCARD_E_INSUFFICIENT_BUFFER ; - } - memcpy(mszReaders, "FT smartcard reader",namelen-1); - mszReaders[namelen-1]=0; - } - *pcchReaders = namelen; - - return SCARD_S_SUCCESS; + int namelen = strlen("FT smartcard reader")+2; + + if (0 == hContext) { + return SCARD_E_INVALID_HANDLE; + } + + if (pcchReaders == NULL) { + + return SCARD_E_INVALID_PARAMETER; + } + pthread_mutex_lock(&CommunicatonMutex); + bR301SessionController *sessionController = [bR301SessionController sharedController]; + + pthread_mutex_unlock(&CommunicatonMutex); + if(0 == [sessionController identifyAccessoryCount]) { + return SCARD_E_READER_UNAVAILABLE; + } + + if (mszReaders != NULL) { + if (*pcchReaders < namelen) { + return SCARD_E_INSUFFICIENT_BUFFER ; + } + memcpy(mszReaders, "FT smartcard reader",namelen-1); + mszReaders[namelen-1]=0; + } + *pcchReaders = namelen; + + return SCARD_S_SUCCESS; } LONG SCardConnect( SCARDCONTEXT hContext, LPCSTR szReader, - DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, - LPDWORD pdwActiveProtocol) + DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, + LPDWORD pdwActiveProtocol) { - unsigned char buf[256]= {0}; - unsigned int len1 =sizeof(buf); - long ReturnValue = 0; - - ////////////////////////////////////////////////////// - if ( NO == gIsOpen) { - return SCARD_E_READER_UNAVAILABLE; - } - ///////////////////////////////////////////////////// - - if (0 == hContext) { - return SCARD_E_INVALID_HANDLE; - } - if (NULL == phCard) { - return SCARD_E_INVALID_PARAMETER; - } - - if (pdwActiveProtocol == NULL) { - return SCARD_E_INVALID_PARAMETER; - } - - pthread_mutex_lock(&CommunicatonMutex); - CcidDesc * CcidSlots = get_ccid_slot(0); - if ( NO == gIsOpen) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - ReturnValue = CmdPowerOff(0); - if (ReturnValue != IFD_SUCCESS) { - [NSThread sleepForTimeInterval:0.002]; - } - - ReturnValue = CmdPowerOn(0,&len1,buf,VOLTAGE_AUTO); - - if(IFD_SUCCESS != ReturnValue) { - //add if power on failed then clear atr - CcidSlots->nATRLength = 0; - *CcidSlots->pcATRBuffer = '\0'; - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_NOT_TRANSACTED; - - } else { - - if(len1 >= 1) { - CcidSlots->nATRLength = len1; - memcpy(CcidSlots->pcATRBuffer,buf,len1); - CcidSlots->bPowerFlags |= MASK_POWERFLAGS_PUP; - } else { - - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNSUPPORTED; - } - } - - /* initialise T=1 context */ - (void)t1_init(&(CcidSlots->t1), 0); - Scrd_Negotiate(0); - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); - int CardProtocol = ccid_descriptor->cardProtocol; - - - if (CardProtocol == T_0) { - *pdwActiveProtocol = SCARD_PROTOCOL_T0; - } else if(CardProtocol == T_1) { - *pdwActiveProtocol = SCARD_PROTOCOL_T1; - } else { - *pdwActiveProtocol = SCARD_PROTOCOL_UNDEFINED; - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_UNSUPPORTED_FEATURE; - } - - *phCard = (SCARDHANDLE)CcidSlots; - eShCardHandleCount = eShCardHandleCount + 1; - pthread_mutex_unlock(&CommunicatonMutex); - - return SCARD_S_SUCCESS; + unsigned char buf[256]= {0}; + unsigned int len1 =sizeof(buf); + long ReturnValue = 0; + + ////////////////////////////////////////////////////// + if ( NO == gIsOpen) { + return SCARD_E_READER_UNAVAILABLE; + } + ///////////////////////////////////////////////////// + + if (0 == hContext) { + return SCARD_E_INVALID_HANDLE; + } + if (NULL == phCard) { + return SCARD_E_INVALID_PARAMETER; + } + + if (pdwActiveProtocol == NULL) { + return SCARD_E_INVALID_PARAMETER; + } + + pthread_mutex_lock(&CommunicatonMutex); + CcidDesc * CcidSlots = get_ccid_slot(0); + if ( NO == gIsOpen) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + ReturnValue = CmdPowerOff(0); + if (ReturnValue != IFD_SUCCESS) { + [NSThread sleepForTimeInterval:0.002]; + } + + ReturnValue = CmdPowerOn(0,&len1,buf,VOLTAGE_AUTO); + + if(IFD_SUCCESS != ReturnValue) { + //add if power on failed then clear atr + CcidSlots->nATRLength = 0; + *CcidSlots->pcATRBuffer = '\0'; + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_NOT_TRANSACTED; + + } else { + + if(len1 >= 1) { + CcidSlots->nATRLength = len1; + memcpy(CcidSlots->pcATRBuffer,buf,len1); + CcidSlots->bPowerFlags |= MASK_POWERFLAGS_PUP; + } else { + + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNSUPPORTED; + } + } + + /* initialise T=1 context */ + (void)t1_init(&(CcidSlots->t1), 0); + Scrd_Negotiate(0); + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); + int CardProtocol = ccid_descriptor->cardProtocol; + + + if (CardProtocol == T_0) { + *pdwActiveProtocol = SCARD_PROTOCOL_T0; + } else if(CardProtocol == T_1) { + *pdwActiveProtocol = SCARD_PROTOCOL_T1; + } else { + *pdwActiveProtocol = SCARD_PROTOCOL_UNDEFINED; + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_UNSUPPORTED_FEATURE; + } + + *phCard = (SCARDHANDLE)CcidSlots; + eShCardHandleCount = eShCardHandleCount + 1; + pthread_mutex_unlock(&CommunicatonMutex); + + return SCARD_S_SUCCESS; } @@ -306,74 +308,74 @@ LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol) { - - SCardDisconnect(hCard, dwInitialization); - - return SCardConnect(hCard,"iRockey 301",dwShareMode,dwPreferredProtocols,&hCard,pdwActiveProtocol); - + + SCardDisconnect(hCard, dwInitialization); + + return SCardConnect(hCard,"iRockey 301",dwShareMode,dwPreferredProtocols,&hCard,pdwActiveProtocol); + } LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition) { - long ReturnValue = 0; - - if ( NO == gIsOpen) { - return SCARD_E_READER_UNAVAILABLE; - } - - if (0 == hCard) { - return SCARD_E_INVALID_HANDLE; - } - - - if ((dwDisposition != SCARD_LEAVE_CARD) - && (dwDisposition != SCARD_UNPOWER_CARD) - && (dwDisposition != SCARD_RESET_CARD) - && (dwDisposition != SCARD_EJECT_CARD)) { - return SCARD_E_INVALID_VALUE; - } - - pthread_mutex_lock(&CommunicatonMutex); - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - if (eShCardHandleCount <= 0) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_INVALID_TARGET; - } - - if (dwDisposition == SCARD_LEAVE_CARD) { - eShCardHandleCount = eShCardHandleCount -1; - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_S_SUCCESS; - } - - - eShCardHandleCount = eShCardHandleCount - 1; - - - if (eShCardHandleCount > 0) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_S_SUCCESS; - } - - CcidDesc *CcidSlots = get_ccid_slot(0); - CcidSlots->nATRLength = 0; - *CcidSlots->pcATRBuffer = '\0'; - CcidSlots->bPowerFlags |= MASK_POWERFLAGS_PDWN; - t1_release(&(CcidSlots->t1)); - - ReturnValue = CmdPowerOff(0); - isDukpt = 0; - pthread_mutex_unlock(&CommunicatonMutex); - - if (ReturnValue != IFD_SUCCESS ) { - return SCARD_F_COMM_ERROR; - } - - return SCARD_S_SUCCESS; + long ReturnValue = 0; + + if ( NO == gIsOpen) { + return SCARD_E_READER_UNAVAILABLE; + } + + if (0 == hCard) { + return SCARD_E_INVALID_HANDLE; + } + + + if ((dwDisposition != SCARD_LEAVE_CARD) + && (dwDisposition != SCARD_UNPOWER_CARD) + && (dwDisposition != SCARD_RESET_CARD) + && (dwDisposition != SCARD_EJECT_CARD)) { + return SCARD_E_INVALID_VALUE; + } + + pthread_mutex_lock(&CommunicatonMutex); + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + if (eShCardHandleCount <= 0) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_INVALID_TARGET; + } + + if (dwDisposition == SCARD_LEAVE_CARD) { + eShCardHandleCount = eShCardHandleCount -1; + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_S_SUCCESS; + } + + + eShCardHandleCount = eShCardHandleCount - 1; + + + if (eShCardHandleCount > 0) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_S_SUCCESS; + } + + CcidDesc *CcidSlots = get_ccid_slot(0); + CcidSlots->nATRLength = 0; + *CcidSlots->pcATRBuffer = '\0'; + CcidSlots->bPowerFlags |= MASK_POWERFLAGS_PDWN; + t1_release(&(CcidSlots->t1)); + + ReturnValue = CmdPowerOff(0); + isDukpt = 0; + pthread_mutex_unlock(&CommunicatonMutex); + + if (ReturnValue != IFD_SUCCESS ) { + return SCARD_F_COMM_ERROR; + } + + return SCARD_S_SUCCESS; } @@ -381,203 +383,203 @@ LONG SCardStatus(SCARDHANDLE hCard, LPSTR mszReaderNames, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen) { - - unsigned char pcbuffer[SIZE_GET_SLOT_STATUS]; - unsigned char ATRBuffer[33]= {0}; - int ATRLength = 0; - int CardProtocol = 0; - int namelen = strlen("iRockey 301")+2; - unsigned int oldReadTimeout; - RESPONSECODE return_value = IFD_COMMUNICATION_ERROR; - - if (pcchReaderLen != NULL ) { - if (mszReaderNames != NULL && *pcchReaderLen >= namelen) { - strcpy(mszReaderNames, "iRockey 301"); - mszReaderNames[namelen -1]='\0'; - *pcchReaderLen = namelen; - } else { - *pcchReaderLen = 0; - - return SCARD_E_INSUFFICIENT_BUFFER; - } - } - - pthread_mutex_lock(&CommunicatonMutex); - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); - CcidDesc *CcidSlots = get_ccid_slot(0); - - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - /* save the current read timeout computed from card capabilities */ - /* use default timeout since the reader may not be present anymore */ - - oldReadTimeout = g_dwTimeOut; - if (iR301_or_bR301 == 0) { - g_dwTimeOut= 600; - } - if (iR301_or_bR301 == 1) { - g_dwTimeOut = 1500; - } - - return_value = CmdGetSlotStatus(0, pcbuffer); - - /* set back the old timeout */ - g_dwTimeOut = oldReadTimeout; - - if (return_value != IFD_SUCCESS) { - return_value = SCARD_E_NOT_TRANSACTED; - pthread_mutex_unlock(&CommunicatonMutex); - return (int)return_value; - } - - return_value = IFD_COMMUNICATION_ERROR; - switch (pcbuffer[STATUS_OFFSET] & CCID_ICC_STATUS_MASK) { /* bStatus */ - case CCID_ICC_PRESENT_ACTIVE: - return_value = SCARD_PRESENT; - ccid_descriptor->dwSlotStatus = IFD_ICC_PRESENT; - /* use default slot */ - break; - - case CCID_ICC_PRESENT_INACTIVE: - return_value = SCARD_SWALLOWED; - - /* Reset ATR buffer */ - CcidSlots->nATRLength = 0; - *CcidSlots->pcATRBuffer = '\0'; - /* Reset PowerFlags */ - CcidSlots->bPowerFlags = POWERFLAGS_RAZ; - ccid_descriptor->dwSlotStatus = IFD_ICC_PRESENT; - break; - - case CCID_ICC_ABSENT: - /* Reset ATR buffer */ - CcidSlots->nATRLength = 0; - *CcidSlots->pcATRBuffer = '\0'; - ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; - /* Reset PowerFlags */ - CcidSlots->bPowerFlags = POWERFLAGS_RAZ; - return_value = SCARD_ABSENT; - break; - } - - if (pcbuffer[ERROR_OFFSET] == 0xFE) { - return_value = SCARD_ABSENT; - } - - if (return_value == SCARD_PRESENT) { - - ATRLength = CcidSlots->nATRLength; - memcpy(ATRBuffer, CcidSlots->pcATRBuffer, ATRLength); - CardProtocol = ccid_descriptor->cardProtocol; - - } - - if (pdwState != NULL) { - *pdwState = (unsigned int)return_value; - } - - - if (pcbAtrLen != NULL) { - - if (*pcbAtrLen < ATRLength) { - *pcbAtrLen = 0; - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_INSUFFICIENT_BUFFER; - } - if ( pbAtr != NULL) { - - memcpy(pbAtr,ATRBuffer ,ATRLength); - } - - *pcbAtrLen = ATRLength; - - } - - if (pdwProtocol != NULL) { - - if (CardProtocol == T_0) { - *pdwProtocol = SCARD_PROTOCOL_T0; - } else if(CardProtocol == T_1) { - *pdwProtocol = SCARD_PROTOCOL_T1; - } else { - *pdwProtocol = SCARD_PROTOCOL_UNDEFINED; - } - - } - - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_S_SUCCESS; - + + unsigned char pcbuffer[SIZE_GET_SLOT_STATUS]; + unsigned char ATRBuffer[33]= {0}; + int ATRLength = 0; + int CardProtocol = 0; + int namelen = strlen("iRockey 301")+2; + unsigned int oldReadTimeout; + RESPONSECODE return_value = IFD_COMMUNICATION_ERROR; + + if (pcchReaderLen != NULL ) { + if (mszReaderNames != NULL && *pcchReaderLen >= namelen) { + strcpy(mszReaderNames, "iRockey 301"); + mszReaderNames[namelen -1]='\0'; + *pcchReaderLen = namelen; + } else { + *pcchReaderLen = 0; + + return SCARD_E_INSUFFICIENT_BUFFER; + } + } + + pthread_mutex_lock(&CommunicatonMutex); + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); + CcidDesc *CcidSlots = get_ccid_slot(0); + + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + /* save the current read timeout computed from card capabilities */ + /* use default timeout since the reader may not be present anymore */ + + oldReadTimeout = g_dwTimeOut; + if (iR301_or_bR301 == 0) { + g_dwTimeOut= 600; + } + if (iR301_or_bR301 == 1) { + g_dwTimeOut = 1500; + } + + return_value = CmdGetSlotStatus(0, pcbuffer); + + /* set back the old timeout */ + g_dwTimeOut = oldReadTimeout; + + if (return_value != IFD_SUCCESS) { + return_value = SCARD_E_NOT_TRANSACTED; + pthread_mutex_unlock(&CommunicatonMutex); + return (int)return_value; + } + + return_value = IFD_COMMUNICATION_ERROR; + switch (pcbuffer[STATUS_OFFSET] & CCID_ICC_STATUS_MASK) { /* bStatus */ + case CCID_ICC_PRESENT_ACTIVE: + return_value = SCARD_PRESENT; + ccid_descriptor->dwSlotStatus = IFD_ICC_PRESENT; + /* use default slot */ + break; + + case CCID_ICC_PRESENT_INACTIVE: + return_value = SCARD_SWALLOWED; + + /* Reset ATR buffer */ + CcidSlots->nATRLength = 0; + *CcidSlots->pcATRBuffer = '\0'; + /* Reset PowerFlags */ + CcidSlots->bPowerFlags = POWERFLAGS_RAZ; + ccid_descriptor->dwSlotStatus = IFD_ICC_PRESENT; + break; + + case CCID_ICC_ABSENT: + /* Reset ATR buffer */ + CcidSlots->nATRLength = 0; + *CcidSlots->pcATRBuffer = '\0'; + ccid_descriptor->dwSlotStatus = IFD_ICC_NOT_PRESENT; + /* Reset PowerFlags */ + CcidSlots->bPowerFlags = POWERFLAGS_RAZ; + return_value = SCARD_ABSENT; + break; + } + + if (pcbuffer[ERROR_OFFSET] == 0xFE) { + return_value = SCARD_ABSENT; + } + + if (return_value == SCARD_PRESENT) { + + ATRLength = CcidSlots->nATRLength; + memcpy(ATRBuffer, CcidSlots->pcATRBuffer, ATRLength); + CardProtocol = ccid_descriptor->cardProtocol; + + } + + if (pdwState != NULL) { + *pdwState = (unsigned int)return_value; + } + + + if (pcbAtrLen != NULL) { + + if (*pcbAtrLen < ATRLength) { + *pcbAtrLen = 0; + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_INSUFFICIENT_BUFFER; + } + if ( pbAtr != NULL) { + + memcpy(pbAtr,ATRBuffer ,ATRLength); + } + + *pcbAtrLen = ATRLength; + + } + + if (pdwProtocol != NULL) { + + if (CardProtocol == T_0) { + *pdwProtocol = SCARD_PROTOCOL_T0; + } else if(CardProtocol == T_1) { + *pdwProtocol = SCARD_PROTOCOL_T1; + } else { + *pdwProtocol = SCARD_PROTOCOL_UNDEFINED; + } + + } + + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_S_SUCCESS; + } LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, LPBYTE pbAttr, LPDWORD pcbAttrLen) { - int ATRLength = 0; - unsigned char ATRBuffer[33]= {0}; - - if (0 == hCard) { - return SCARD_E_INVALID_HANDLE; - } - - if (pcbAttrLen == NULL || pbAttr == NULL) { - return SCARD_E_INVALID_PARAMETER; - } - - pthread_mutex_lock(&CommunicatonMutex); - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - CcidDesc *CcidSlots = get_ccid_slot(0); - if (CcidSlots->nATRLength> 0) { - ATRLength = CcidSlots->nATRLength; - memcpy(ATRBuffer, CcidSlots->pcATRBuffer, ATRLength); - } - pthread_mutex_unlock(&CommunicatonMutex); - - if(ATRLength > 0) { - if (*pcbAttrLen < ATRLength ) { - *pcbAttrLen = 0; - return SCARD_E_INVALID_PARAMETER; - } - memcpy(pbAttr,ATRBuffer,ATRLength); - *pcbAttrLen = ATRLength; - } else { - return SCARD_E_INSUFFICIENT_BUFFER; - } - - return SCARD_S_SUCCESS;; + int ATRLength = 0; + unsigned char ATRBuffer[33]= {0}; + + if (0 == hCard) { + return SCARD_E_INVALID_HANDLE; + } + + if (pcbAttrLen == NULL || pbAttr == NULL) { + return SCARD_E_INVALID_PARAMETER; + } + + pthread_mutex_lock(&CommunicatonMutex); + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + CcidDesc *CcidSlots = get_ccid_slot(0); + if (CcidSlots->nATRLength> 0) { + ATRLength = CcidSlots->nATRLength; + memcpy(ATRBuffer, CcidSlots->pcATRBuffer, ATRLength); + } + pthread_mutex_unlock(&CommunicatonMutex); + + if(ATRLength > 0) { + if (*pcbAttrLen < ATRLength ) { + *pcbAttrLen = 0; + return SCARD_E_INVALID_PARAMETER; + } + memcpy(pbAttr,ATRBuffer,ATRLength); + *pcbAttrLen = ATRLength; + } else { + return SCARD_E_INSUFFICIENT_BUFFER; + } + + return SCARD_S_SUCCESS;; } LONG SCardBeginTransaction(SCARDHANDLE hCard) { - - if (0 == hCard) { - return SCARD_E_INVALID_HANDLE; - } - - return SCARD_S_SUCCESS; + + if (0 == hCard) { + return SCARD_E_INVALID_HANDLE; + } + + return SCARD_S_SUCCESS; } LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition) { - - if (0 == hCard) { - return SCARD_E_INVALID_HANDLE; - } - if ((dwDisposition != SCARD_LEAVE_CARD) - && (dwDisposition != SCARD_UNPOWER_CARD) - && (dwDisposition != SCARD_RESET_CARD) - && (dwDisposition != SCARD_EJECT_CARD)) - return SCARD_E_INVALID_VALUE; - - return SCARD_S_SUCCESS; + + if (0 == hCard) { + return SCARD_E_INVALID_HANDLE; + } + if ((dwDisposition != SCARD_LEAVE_CARD) + && (dwDisposition != SCARD_UNPOWER_CARD) + && (dwDisposition != SCARD_RESET_CARD) + && (dwDisposition != SCARD_EJECT_CARD)) + return SCARD_E_INVALID_VALUE; + + return SCARD_S_SUCCESS; } @@ -585,56 +587,56 @@ LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition) ////////////////////////////NULL func///////////////////////////////////////// LONG SCARD_CTL_CODE(unsigned int code) { - LONG nCode= (code + 0x31); - return nCode; + LONG nCode= (code + 0x31); + return nCode; } LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned) { - - long rv; - unsigned char buf[300]; - unsigned int len =sizeof(buf); - - if (0 == hCard) { - return SCARD_E_INVALID_HANDLE; - } - - if (pbSendBuffer == NULL || pbRecvBuffer == NULL || lpBytesReturned == NULL) - return SCARD_E_INVALID_PARAMETER; - - if (cbSendLength > 272 -10 ) { - return SCARD_E_INVALID_PARAMETER; - } - - pthread_mutex_lock(&CommunicatonMutex); - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - if (dwControlCode == SCARD_CTL_CODE(3500)) { - - - rv = CmdEscape(0, (unsigned char*)pbSendBuffer, cbSendLength, buf, &len); - pthread_mutex_unlock(&CommunicatonMutex); - if( IFD_SUCCESS != rv) { - - return SCARD_E_NOT_TRANSACTED; - } - - if (cbRecvLength < len) { - return SCARD_E_INSUFFICIENT_BUFFER; - } - *lpBytesReturned = len; - memcpy(pbRecvBuffer,buf,len); - - return SCARD_S_SUCCESS; - } - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNSUPPORTED; + + long rv; + unsigned char buf[300]; + unsigned int len =sizeof(buf); + + if (0 == hCard) { + return SCARD_E_INVALID_HANDLE; + } + + if (pbSendBuffer == NULL || pbRecvBuffer == NULL || lpBytesReturned == NULL) + return SCARD_E_INVALID_PARAMETER; + + if (cbSendLength > 272 -10 ) { + return SCARD_E_INVALID_PARAMETER; + } + + pthread_mutex_lock(&CommunicatonMutex); + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + if (dwControlCode == SCARD_CTL_CODE(3500)) { + + + rv = CmdEscape(0, (unsigned char*)pbSendBuffer, cbSendLength, buf, &len); + pthread_mutex_unlock(&CommunicatonMutex); + if( IFD_SUCCESS != rv) { + + return SCARD_E_NOT_TRANSACTED; + } + + if (cbRecvLength < len) { + return SCARD_E_INSUFFICIENT_BUFFER; + } + *lpBytesReturned = len; + memcpy(pbRecvBuffer,buf,len); + + return SCARD_S_SUCCESS; + } + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNSUPPORTED; } @@ -643,170 +645,170 @@ LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength) { - - long rv; - unsigned char buf[CMD_BUF_SIZE]; - unsigned int len =sizeof(buf); - - if ( NO == gIsOpen) { - return SCARD_E_READER_UNAVAILABLE; - } - - - if (0 == hCard) { - return SCARD_E_INVALID_HANDLE; - } - - if (NULL == pioSendPci) { - return SCARD_E_INVALID_VALUE; - } - - if (pbSendBuffer == NULL || pbRecvBuffer == NULL || pcbRecvLength == NULL) { - return SCARD_E_INVALID_PARAMETER; - } - - /* - * Must at least have 2 status words even for SCardControl - */ - if (*pcbRecvLength < 2) { - return SCARD_E_INSUFFICIENT_BUFFER; - } - - pthread_mutex_lock(&CommunicatonMutex); - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); - len = CMD_BUF_SIZE; - if( FT_READER_UA != gDevType) { - rv = CmdXfrBlock(0, (unsigned int)cbSendLength,(unsigned char*)pbSendBuffer, &len, buf,T_0); - } else { - rv = CmdXfrBlock(0, (unsigned int)cbSendLength,(unsigned char*)pbSendBuffer, &len, buf,ccid_descriptor->cardProtocol); - } - + + long rv; + unsigned char buf[CMD_BUF_SIZE]; + unsigned int len =sizeof(buf); + + if ( NO == gIsOpen) { + return SCARD_E_READER_UNAVAILABLE; + } + + + if (0 == hCard) { + return SCARD_E_INVALID_HANDLE; + } + + if (NULL == pioSendPci) { + return SCARD_E_INVALID_VALUE; + } + + if (pbSendBuffer == NULL || pbRecvBuffer == NULL || pcbRecvLength == NULL) { + return SCARD_E_INVALID_PARAMETER; + } + + /* + * Must at least have 2 status words even for SCardControl + */ + if (*pcbRecvLength < 2) { + return SCARD_E_INSUFFICIENT_BUFFER; + } + + pthread_mutex_lock(&CommunicatonMutex); + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(0); + len = CMD_BUF_SIZE; + if( FT_READER_UA != gDevType) { + rv = CmdXfrBlock(0, (unsigned int)cbSendLength,(unsigned char*)pbSendBuffer, &len, buf,T_0); + } else { + rv = CmdXfrBlock(0, (unsigned int)cbSendLength,(unsigned char*)pbSendBuffer, &len, buf,ccid_descriptor->cardProtocol); + } + #if 1 - /*Difference between the old UA and UB method is test error*/ - if (T_1 == ccid_descriptor->cardProtocol - && IFD_SUCCESS != rv - && FT_READER_DEFAULT == gDevType && iR301_or_bR301 == 0) { - - len = CMD_BUF_SIZE; - rv = CmdXfrBlock(0, (unsigned int)cbSendLength,(unsigned char*)pbSendBuffer, &len, buf,ccid_descriptor->cardProtocol); - if( IFD_SUCCESS == rv) { - gDevType = FT_READER_UA; - } - } + /*Difference between the old UA and UB method is test error*/ + if (T_1 == ccid_descriptor->cardProtocol + && IFD_SUCCESS != rv + && FT_READER_DEFAULT == gDevType && iR301_or_bR301 == 0) { + + len = CMD_BUF_SIZE; + rv = CmdXfrBlock(0, (unsigned int)cbSendLength,(unsigned char*)pbSendBuffer, &len, buf,ccid_descriptor->cardProtocol); + if( IFD_SUCCESS == rv) { + gDevType = FT_READER_UA; + } + } #endif - - pthread_mutex_unlock(&CommunicatonMutex); - if( IFD_SUCCESS != rv) { - - return SCARD_E_NOT_TRANSACTED; - } - - if (*pcbRecvLength < len) { - *pcbRecvLength = 0; - return SCARD_E_INSUFFICIENT_BUFFER; - } - *pcbRecvLength = len; - memcpy(pbRecvBuffer,buf,len); - - return SCARD_S_SUCCESS; - + + pthread_mutex_unlock(&CommunicatonMutex); + if( IFD_SUCCESS != rv) { + + return SCARD_E_NOT_TRANSACTED; + } + + if (*pcbRecvLength < len) { + *pcbRecvLength = 0; + return SCARD_E_INSUFFICIENT_BUFFER; + } + *pcbRecvLength = len; + memcpy(pbRecvBuffer,buf,len); + + return SCARD_S_SUCCESS; + } LONG SCardGetStatusChange(SCARDCONTEXT hContext, DWORD dwTimeout, LPSCARD_READERSTATE rgReaderStates, DWORD cReaders) { - LONG rv =0; - DWORD dwState; - - if (rgReaderStates == NULL && cReaders > 0) { - return SCARD_E_INVALID_PARAMETER; - } - - rv = SCardStatus(NULL,NULL,NULL,&dwState,NULL,NULL,NULL); - - if(SCARD_S_SUCCESS != rv) { - rgReaderStates->dwEventState = SCARD_STATE_EMPTY; - return rv; - } else { - if (SCARD_ABSENT == dwState) { - eShCardHandleCount = 0; - isDukpt = 0; - rgReaderStates->dwEventState = SCARD_STATE_EMPTY; - } else { - rgReaderStates->dwEventState = SCARD_STATE_PRESENT; - } - } - - return SCARD_S_SUCCESS; + LONG rv =0; + DWORD dwState; + + if (rgReaderStates == NULL && cReaders > 0) { + return SCARD_E_INVALID_PARAMETER; + } + + rv = SCardStatus(NULL,NULL,NULL,&dwState,NULL,NULL,NULL); + + if(SCARD_S_SUCCESS != rv) { + rgReaderStates->dwEventState = SCARD_STATE_EMPTY; + return rv; + } else { + if (SCARD_ABSENT == dwState) { + eShCardHandleCount = 0; + isDukpt = 0; + rgReaderStates->dwEventState = SCARD_STATE_EMPTY; + } else { + rgReaderStates->dwEventState = SCARD_STATE_PRESENT; + } + } + + return SCARD_S_SUCCESS; } LONG SCardListReaderGroups(SCARDCONTEXT hContext, LPSTR mszGroups, LPDWORD pcchGroups) { - return SCARD_S_SUCCESS; - + return SCARD_S_SUCCESS; + } LONG SCardCancel(SCARDCONTEXT hContext) { - - return SCARD_S_SUCCESS; + + return SCARD_S_SUCCESS; } PCSC_API LONG SCardSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout) { - //We haven't implement this function - return 0; + //We haven't implement this function + return 0; } #pragma mark duktp LONG FtGetDevVer( SCARDCONTEXT hContext,char *firmwareRevision,char *hardwareRevision) { - long rv=0; - - pthread_mutex_lock(&CommunicatonMutex); - bR301SessionController *sessionController = [bR301SessionController sharedController]; - if (sessionController.identifyAccessoryCount == 0 ) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - rv = CmdGetDevVer(0,firmwareRevision,hardwareRevision); - pthread_mutex_unlock(&CommunicatonMutex); - if (IFD_SUCCESS == rv) { - return SCARD_S_SUCCESS; - } else { - return SCARD_E_NO_READERS_AVAILABLE; - } + long rv=0; + + pthread_mutex_lock(&CommunicatonMutex); + bR301SessionController *sessionController = [bR301SessionController sharedController]; + if (sessionController.identifyAccessoryCount == 0 ) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + rv = CmdGetDevVer(0,firmwareRevision,hardwareRevision); + pthread_mutex_unlock(&CommunicatonMutex); + if (IFD_SUCCESS == rv) { + return SCARD_S_SUCCESS; + } else { + return SCARD_E_NO_READERS_AVAILABLE; + } } LONG FtDukptInit(SCARDHANDLE hCard,unsigned char *encBuf,unsigned int nLen) { - - long rv=0; - if (nLen != 40 && nLen != 48) { - return SCARD_E_INVALID_PARAMETER; - } - pthread_mutex_lock(&CommunicatonMutex); - - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - rv= CmdWriteIKSN2IPEKFlash( 0,encBuf,nLen); - pthread_mutex_unlock(&CommunicatonMutex); - - if (IFD_SUCCESS == rv) { - return SCARD_S_SUCCESS; - } else { - return SCARD_F_COMM_ERROR; - } - return 0; - + + long rv=0; + if (nLen != 40 && nLen != 48) { + return SCARD_E_INVALID_PARAMETER; + } + pthread_mutex_lock(&CommunicatonMutex); + + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + rv= CmdWriteIKSN2IPEKFlash( 0,encBuf,nLen); + pthread_mutex_unlock(&CommunicatonMutex); + + if (IFD_SUCCESS == rv) { + return SCARD_S_SUCCESS; + } else { + return SCARD_F_COMM_ERROR; + } + return 0; + } @@ -815,238 +817,238 @@ LONG FtDukptSetEncMod(SCARDHANDLE hCard, unsigned int bEncFunc, unsigned int bEncType) { - long rv=0; - - pthread_mutex_lock(&CommunicatonMutex); - - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - rv= CmdSetEncMod(0,bEncrypt,bEncFunc,bEncType); - pthread_mutex_unlock(&CommunicatonMutex); - - if (IFD_SUCCESS == rv) { - if (bEncrypt && (!bEncType)) { - isDukpt = 1; - } else { - isDukpt = 0; - } - return SCARD_S_SUCCESS; - } else { - return SCARD_F_COMM_ERROR; - } - return 0; + long rv=0; + + pthread_mutex_lock(&CommunicatonMutex); + + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + rv= CmdSetEncMod(0,bEncrypt,bEncFunc,bEncType); + pthread_mutex_unlock(&CommunicatonMutex); + + if (IFD_SUCCESS == rv) { + if (bEncrypt && (!bEncType)) { + isDukpt = 1; + } else { + isDukpt = 0; + } + return SCARD_S_SUCCESS; + } else { + return SCARD_F_COMM_ERROR; + } + return 0; } LONG FtDukptGetKSN(SCARDHANDLE hCard, unsigned int * pnlength, unsigned char *buffer) { - long rv=0; - unsigned char temp[64]; - unsigned int nlength=sizeof(temp); - - if (* pnlength < 10) { - return SCARD_E_INVALID_PARAMETER; - } - - pthread_mutex_lock(&CommunicatonMutex); - - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - rv= CmdGetKSN( 0, &nlength,temp); - pthread_mutex_unlock(&CommunicatonMutex); - - if (IFD_SUCCESS == rv) { - memcpy(buffer, temp,10); - return SCARD_S_SUCCESS; - } else { - return SCARD_F_COMM_ERROR; - } + long rv=0; + unsigned char temp[64]; + unsigned int nlength=sizeof(temp); + + if (* pnlength < 10) { + return SCARD_E_INVALID_PARAMETER; + } + + pthread_mutex_lock(&CommunicatonMutex); + + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + rv= CmdGetKSN( 0, &nlength,temp); + pthread_mutex_unlock(&CommunicatonMutex); + + if (IFD_SUCCESS == rv) { + memcpy(buffer, temp,10); + return SCARD_S_SUCCESS; + } else { + return SCARD_F_COMM_ERROR; + } } #pragma mark feitian's private command of reader void FtGetLibVersion (char *buffer) { - if (buffer == NULL) { - return; - } - sprintf(buffer, " ST:%01x.%01x.%01x",Ft_iR301U_Version[0],Ft_iR301U_Version[1],Ft_iR301U_Version[2]); + if (buffer == NULL) { + return; + } + sprintf(buffer, " ST:%01x.%01x.%01x",Ft_iR301U_Version[0],Ft_iR301U_Version[1],Ft_iR301U_Version[2]); } /** -* get the current Reader type -* -* @param readerType -* -* @return errorCode -*/ + * get the current Reader type + * + * @param readerType + * + * @return errorCode + */ LONG FtGetCurrentReaderType(unsigned int *readerType) { - - long returnValue = -1; - unsigned char temp[64] = {0}; - unsigned int nlength=sizeof(temp); - - if (gIsOpen == 0) { - - return SCARD_E_READER_UNAVAILABLE; - } - - if (readerType == NULL) { - - return SCARD_E_INVALID_PARAMETER; - } - - //bR301 - if (iR301_or_bR301 == 1) { - *readerType = READER_bR301; - return SCARD_S_SUCCESS; - } - - pthread_mutex_lock(&CommunicatonMutex); - - *readerType = READER_UNKOWN; - returnValue = CmdGetDevInfo(0,&nlength , temp); - - pthread_mutex_unlock(&CommunicatonMutex); - - if (IFD_SUCCESS == returnValue) { - - if (temp[0] == FT_READER_UA || temp[0] == FT_READER_UB || temp[0] == FT_READER_UC - || temp[0] == FT_READER_UC_B || temp[0] == FT_READER_UM || temp[0] == FT_READER_UD) { - *readerType = READER_iR301U_DOCK; - } - - if (temp[0] == FT_READER_UB_LT || temp[0] == FT_READER_UC_LT || - temp[0] == FT_READER_UC_LT_B || temp[0] == FT_READER_UD_LT) { - *readerType = READER_iR301U_LIGHTING; - } - } - - return (int)returnValue; - + + long returnValue = -1; + unsigned char temp[64] = {0}; + unsigned int nlength=sizeof(temp); + + if (gIsOpen == 0) { + + return SCARD_E_READER_UNAVAILABLE; + } + + if (readerType == NULL) { + + return SCARD_E_INVALID_PARAMETER; + } + + //bR301 + if (iR301_or_bR301 == 1) { + *readerType = READER_bR301; + return SCARD_S_SUCCESS; + } + + pthread_mutex_lock(&CommunicatonMutex); + + *readerType = READER_UNKOWN; + returnValue = CmdGetDevInfo(0,&nlength , temp); + + pthread_mutex_unlock(&CommunicatonMutex); + + if (IFD_SUCCESS == returnValue) { + + if (temp[0] == FT_READER_UA || temp[0] == FT_READER_UB || temp[0] == FT_READER_UC + || temp[0] == FT_READER_UC_B || temp[0] == FT_READER_UM || temp[0] == FT_READER_UD) { + *readerType = READER_iR301U_DOCK; + } + + if (temp[0] == FT_READER_UB_LT || temp[0] == FT_READER_UC_LT || + temp[0] == FT_READER_UC_LT_B || temp[0] == FT_READER_UD_LT) { + *readerType = READER_iR301U_LIGHTING; + } + } + + return (int)returnValue; + } LONG FtGetSerialNum(SCARDHANDLE hCard, unsigned int length, char * buffer) { - long rv=0; - unsigned char temp[64]={0}; - unsigned int nlength=sizeof(temp); - - if (length < 16) - { - return SCARD_E_INVALID_PARAMETER; - } - - pthread_mutex_lock(&CommunicatonMutex); - if (gIsOpen == 0) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - rv= CmdGetSerialNum( 0, &nlength,temp); - - pthread_mutex_unlock(&CommunicatonMutex); - - if (IFD_SUCCESS == rv) - { - memcpy(buffer, temp,16); - buffer[16]='\0'; - return SCARD_S_SUCCESS; - } - else - { - return SCARD_F_COMM_ERROR; - } + long rv=0; + unsigned char temp[64]={0}; + unsigned int nlength=sizeof(temp); + + if (length < 16) + { + return SCARD_E_INVALID_PARAMETER; + } + + pthread_mutex_lock(&CommunicatonMutex); + if (gIsOpen == 0) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + rv= CmdGetSerialNum( 0, &nlength,temp); + + pthread_mutex_unlock(&CommunicatonMutex); + + if (IFD_SUCCESS == rv) + { + memcpy(buffer, temp,16); + buffer[16]='\0'; + return SCARD_S_SUCCESS; + } + else + { + return SCARD_F_COMM_ERROR; + } } LONG FtWriteFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, unsigned char buffer[]) { - long rv=0; - - if (bOffset+blength > 255 || blength == 0) - { - return SCARD_E_INVALID_PARAMETER; - } - - pthread_mutex_lock(&CommunicatonMutex); - - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - - rv= CmdWriteFlash(0,bOffset,blength,buffer); - pthread_mutex_unlock(&CommunicatonMutex); - - if (IFD_SUCCESS == rv) - { - return SCARD_S_SUCCESS; - } - else - { - return SCARD_F_COMM_ERROR; - } + long rv=0; + + if (bOffset+blength > 255 || blength == 0) + { + return SCARD_E_INVALID_PARAMETER; + } + + pthread_mutex_lock(&CommunicatonMutex); + + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + + rv= CmdWriteFlash(0,bOffset,blength,buffer); + pthread_mutex_unlock(&CommunicatonMutex); + + if (IFD_SUCCESS == rv) + { + return SCARD_S_SUCCESS; + } + else + { + return SCARD_F_COMM_ERROR; + } } LONG FtReadFlash(SCARDHANDLE hCard,unsigned char bOffset, unsigned char blength, unsigned char buffer[]) { - long rv=0; - unsigned char temp[300]; - - if (bOffset+blength > 255 || blength == 0) - { - return SCARD_E_INVALID_PARAMETER; - } - - pthread_mutex_lock(&CommunicatonMutex); - - if (gIsOpen == NO) { - pthread_mutex_unlock(&CommunicatonMutex); - return SCARD_E_READER_UNAVAILABLE; - } - rv= CmdReadFlash( 0,bOffset,blength,temp); - pthread_mutex_unlock(&CommunicatonMutex); - - if (IFD_SUCCESS == rv) - { - memcpy(buffer, temp, blength); - return SCARD_S_SUCCESS; - } - else - { - return SCARD_F_COMM_ERROR; - } + long rv=0; + unsigned char temp[300]; + + if (bOffset+blength > 255 || blength == 0) + { + return SCARD_E_INVALID_PARAMETER; + } + + pthread_mutex_lock(&CommunicatonMutex); + + if (gIsOpen == NO) { + pthread_mutex_unlock(&CommunicatonMutex); + return SCARD_E_READER_UNAVAILABLE; + } + rv= CmdReadFlash( 0,bOffset,blength,temp); + pthread_mutex_unlock(&CommunicatonMutex); + + if (IFD_SUCCESS == rv) + { + memcpy(buffer, temp, blength); + return SCARD_S_SUCCESS; + } + else + { + return SCARD_F_COMM_ERROR; + } } LONG FtSetTimeout(SCARDCONTEXT hContext, DWORD dwTimeout) { - if (0 == hContext) - { - return SCARD_E_INVALID_HANDLE; - } - - if (dwTimeout >= 1 ) - { - g_dwTimeOut = dwTimeout ; - } - else - { - return SCARD_E_INVALID_VALUE; - } - - return SCARD_S_SUCCESS; + if (0 == hContext) + { + return SCARD_E_INVALID_HANDLE; + } + + if (dwTimeout >= 1 ) + { + g_dwTimeOut = dwTimeout ; + } + else + { + return SCARD_E_INVALID_VALUE; + } + + return SCARD_S_SUCCESS; } void FtDidEnterBackground(unsigned int bDidEnter) { - + } diff --git a/libs/source/src/wintypes.h b/libs/source/src/wintypes.h index 880056f..7599a89 100755 --- a/libs/source/src/wintypes.h +++ b/libs/source/src/wintypes.h @@ -1,265 +1,265 @@ -/* - * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html ) - * - * Copyright (C) 1999 - * David Corcoran - * Copyright (C) 2002-2011 - * Ludovic Rousseau - * - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - Changes to this license can be made only by the copyright author with - explicit written consent. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $Id: wintypes.h 6851 2014-02-14 15:43:32Z rousseau $ - */ - -/** - * @file - * @brief This keeps a list of Windows(R) types. - */ - -#ifndef __wintypes_h__ -#define __wintypes_h__ - -#ifdef __cplusplus -extern "C" -{ -#endif - -#ifdef __APPLE__ - -#include - -#ifndef BYTE - typedef uint8_t BYTE; -#endif - typedef uint8_t UCHAR; - typedef uint8_t *PUCHAR; - typedef uint16_t USHORT; - -#ifndef __COREFOUNDATION_CFPLUGINCOM__ - typedef uint32_t ULONG; - typedef void *LPVOID; -//haozi err? typedef int16_t BOOL; -#endif - - typedef uint32_t *PULONG; - typedef const void *LPCVOID; - typedef uint32_t DWORD; - typedef uint32_t *PDWORD; - typedef uint16_t WORD; - typedef int32_t LONG; - typedef const char *LPCSTR; - typedef const BYTE *LPCBYTE; - typedef BYTE *LPBYTE; - typedef DWORD *LPDWORD; - typedef char *LPSTR; - -#else - -#ifndef BYTE - typedef unsigned char BYTE; -#endif - typedef unsigned char UCHAR; - typedef unsigned char *PUCHAR; - typedef unsigned short USHORT; - -#ifndef __COREFOUNDATION_CFPLUGINCOM__ - typedef unsigned long ULONG; - typedef void *LPVOID; -#endif - - typedef const void *LPCVOID; - typedef unsigned long DWORD; - typedef unsigned long *PDWORD; - typedef long LONG; - typedef const char *LPCSTR; - typedef const BYTE *LPCBYTE; - typedef BYTE *LPBYTE; - typedef DWORD *LPDWORD; - typedef char *LPSTR; - - /* these types were deprecated but still used by old drivers and - * applications. So just declare and use them. */ - typedef LPSTR LPTSTR; - typedef LPCSTR LPCTSTR; - - /* types unused by pcsc-lite */ - typedef short BOOL; - typedef unsigned short WORD; - typedef unsigned long *PULONG; - -#endif - - - ///////////////////////////////////////// - - -#define SCARD_PCI_T0 (&g_rgSCardT0Pci) /**< protocol control information (PCI) for T=0 */ -#define SCARD_PCI_T1 (&g_rgSCardT1Pci) /**< protocol control information (PCI) for T=1 */ -#define SCARD_PCI_RAW (&g_rgSCardRawPci) /**< protocol control information (PCI) for RAW protocol */ - - /** error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx - */ -#define SCARD_S_SUCCESS ((LONG)0x00000000) /**< No error was encountered. */ -#define SCARD_F_INTERNAL_ERROR ((LONG)0x80100001) /**< An internal consistency check failed. */ -#define SCARD_E_CANCELLED ((LONG)0x80100002) /**< The action was cancelled by an SCardCancel request. */ -#define SCARD_E_INVALID_HANDLE ((LONG)0x80100003) /**< The supplied handle was invalid. */ -#define SCARD_E_INVALID_PARAMETER ((LONG)0x80100004) /**< One or more of the supplied parameters could not be properly interpreted. */ -#define SCARD_E_INVALID_TARGET ((LONG)0x80100005) /**< Registry startup information is missing or invalid. */ -#define SCARD_E_NO_MEMORY ((LONG)0x80100006) /**< Not enough memory available to complete this command. */ -#define SCARD_F_WAITED_TOO_LONG ((LONG)0x80100007) /**< An internal consistency timer has expired. */ -#define SCARD_E_INSUFFICIENT_BUFFER ((LONG)0x80100008) /**< The data buffer to receive returned data is too small for the returned data. */ -#define SCARD_E_UNKNOWN_READER ((LONG)0x80100009) /**< The specified reader name is not recognized. */ -#define SCARD_E_TIMEOUT ((LONG)0x8010000A) /**< The user-specified timeout value has expired. */ -#define SCARD_E_SHARING_VIOLATION ((LONG)0x8010000B) /**< The smart card cannot be accessed because of other connections outstanding. */ -#define SCARD_E_NO_SMARTCARD ((LONG)0x8010000C) /**< The operation requires a Smart Card, but no Smart Card is currently in the device. */ -#define SCARD_E_UNKNOWN_CARD ((LONG)0x8010000D) /**< The specified smart card name is not recognized. */ -#define SCARD_E_CANT_DISPOSE ((LONG)0x8010000E) /**< The system could not dispose of the media in the requested manner. */ -#define SCARD_E_PROTO_MISMATCH ((LONG)0x8010000F) /**< The requested protocols are incompatible with the protocol currently in use with the smart card. */ -#define SCARD_E_NOT_READY ((LONG)0x80100010) /**< The reader or smart card is not ready to accept commands. */ -#define SCARD_E_INVALID_VALUE ((LONG)0x80100011) /**< One or more of the supplied parameters values could not be properly interpreted. */ -#define SCARD_E_SYSTEM_CANCELLED ((LONG)0x80100012) /**< The action was cancelled by the system, presumably to log off or shut down. */ -#define SCARD_F_COMM_ERROR ((LONG)0x80100013) /**< An internal communications error has been detected. */ -#define SCARD_F_UNKNOWN_ERROR ((LONG)0x80100014) /**< An internal error has been detected, but the source is unknown. */ -#define SCARD_E_INVALID_ATR ((LONG)0x80100015) /**< An ATR obtained from the registry is not a valid ATR string. */ -#define SCARD_E_NOT_TRANSACTED ((LONG)0x80100016) /**< An attempt was made to end a non-existent transaction. */ -#define SCARD_E_READER_UNAVAILABLE ((LONG)0x80100017) /**< The specified reader is not currently available for use. */ -#define SCARD_P_SHUTDOWN ((LONG)0x80100018) /**< The operation has been aborted to allow the server application to exit. */ -#define SCARD_E_PCI_TOO_SMALL ((LONG)0x80100019) /**< The PCI Receive buffer was too small. */ -#define SCARD_E_READER_UNSUPPORTED ((LONG)0x8010001A) /**< The reader driver does not meet minimal requirements for support. */ -#define SCARD_E_DUPLICATE_READER ((LONG)0x8010001B) /**< The reader driver did not produce a unique reader name. */ -#define SCARD_E_CARD_UNSUPPORTED ((LONG)0x8010001C) /**< The smart card does not meet minimal requirements for support. */ -#define SCARD_E_NO_SERVICE ((LONG)0x8010001D) /**< The Smart card resource manager is not running. */ -#define SCARD_E_SERVICE_STOPPED ((LONG)0x8010001E) /**< The Smart card resource manager has shut down. */ -#define SCARD_E_UNEXPECTED ((LONG)0x8010001F) /**< An unexpected card error has occurred. */ -#define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x8010001F) /**< This smart card does not support the requested feature. */ -#define SCARD_E_ICC_INSTALLATION ((LONG)0x80100020) /**< No primary provider can be found for the smart card. */ -#define SCARD_E_ICC_CREATEORDER ((LONG)0x80100021) /**< The requested order of object creation is not supported. */ - /* #define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x80100022) / **< This smart card does not support the requested feature. */ -#define SCARD_E_DIR_NOT_FOUND ((LONG)0x80100023) /**< The identified directory does not exist in the smart card. */ -#define SCARD_E_FILE_NOT_FOUND ((LONG)0x80100024) /**< The identified file does not exist in the smart card. */ -#define SCARD_E_NO_DIR ((LONG)0x80100025) /**< The supplied path does not represent a smart card directory. */ -#define SCARD_E_NO_FILE ((LONG)0x80100026) /**< The supplied path does not represent a smart card file. */ -#define SCARD_E_NO_ACCESS ((LONG)0x80100027) /**< Access is denied to this file. */ -#define SCARD_E_WRITE_TOO_MANY ((LONG)0x80100028) /**< The smart card does not have enough memory to store the information. */ -#define SCARD_E_BAD_SEEK ((LONG)0x80100029) /**< There was an error trying to set the smart card file object pointer. */ -#define SCARD_E_INVALID_CHV ((LONG)0x8010002A) /**< The supplied PIN is incorrect. */ -#define SCARD_E_UNKNOWN_RES_MNG ((LONG)0x8010002B) /**< An unrecognized error code was returned from a layered component. */ -#define SCARD_E_NO_SUCH_CERTIFICATE ((LONG)0x8010002C) /**< The requested certificate does not exist. */ -#define SCARD_E_CERTIFICATE_UNAVAILABLE ((LONG)0x8010002D) /**< The requested certificate could not be obtained. */ -#define SCARD_E_NO_READERS_AVAILABLE ((LONG)0x8010002E) /**< Cannot find a smart card reader. */ -#define SCARD_E_COMM_DATA_LOST ((LONG)0x8010002F) /**< A communications error with the smart card has been detected. Retry the operation. */ -#define SCARD_E_NO_KEY_CONTAINER ((LONG)0x80100030) /**< The requested key container does not exist on the smart card. */ -#define SCARD_E_SERVER_TOO_BUSY ((LONG)0x80100031) /**< The Smart Card Resource Manager is too busy to complete this operation. */ - -#define SCARD_W_UNSUPPORTED_CARD ((LONG)0x80100065) /**< The reader cannot communicate with the card, due to ATR string configuration conflicts. */ -#define SCARD_W_UNRESPONSIVE_CARD ((LONG)0x80100066) /**< The smart card is not responding to a reset. */ -#define SCARD_W_UNPOWERED_CARD ((LONG)0x80100067) /**< Power has been removed from the smart card, so that further communication is not possible. */ -#define SCARD_W_RESET_CARD ((LONG)0x80100068) /**< The smart card has been reset, so any shared state information is invalid. */ -#define SCARD_W_REMOVED_CARD ((LONG)0x80100069) /**< The smart card has been removed, so further communication is not possible. */ - -#define SCARD_W_SECURITY_VIOLATION ((LONG)0x8010006A) /**< Access was denied because of a security violation. */ -#define SCARD_W_WRONG_CHV ((LONG)0x8010006B) /**< The card cannot be accessed because the wrong PIN was presented. */ -#define SCARD_W_CHV_BLOCKED ((LONG)0x8010006C) /**< The card cannot be accessed because the maximum number of PIN entry attempts has been reached. */ -#define SCARD_W_EOF ((LONG)0x8010006D) /**< The end of the smart card file has been reached. */ -#define SCARD_W_CANCELLED_BY_USER ((LONG)0x8010006E) /**< The user pressed "Cancel" on a Smart Card Selection Dialog. */ -#define SCARD_W_CARD_NOT_AUTHENTICATED ((LONG)0x8010006F) /**< No PIN was presented to the smart card. */ - -#define SCARD_AUTOALLOCATE (DWORD)(-1) /**< see SCardFreeMemory() */ -#define SCARD_SCOPE_USER 0x0000 /**< Scope in user space */ -#define SCARD_SCOPE_TERMINAL 0x0001 /**< Scope in terminal */ -#define SCARD_SCOPE_SYSTEM 0x0002 /**< Scope in system */ - - -#define SCARD_PROTOCOL_UNDEFINED 0x0000 /**< protocol not set */ -#define SCARD_PROTOCOL_UNSET SCARD_PROTOCOL_UNDEFINED /* backward compat */ -#define SCARD_PROTOCOL_T0 0x0001 /**< T=0 active protocol. */ -#define SCARD_PROTOCOL_T1 0x0002 /**< T=1 active protocol. */ -#define SCARD_PROTOCOL_RAW 0x0004 /**< Raw active protocol. */ -#define SCARD_PROTOCOL_T15 0x0008 /**< T=15 protocol. */ - -#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1) /**< IFD determines prot. */ - -#define SCARD_SHARE_EXCLUSIVE 0x0001 /**< Exclusive mode only */ -#define SCARD_SHARE_SHARED 0x0002 /**< Shared mode only */ -#define SCARD_SHARE_DIRECT 0x0003 /**< Raw mode only */ - -#define SCARD_LEAVE_CARD 0x0000 /**< Do nothing on close */ -#define SCARD_RESET_CARD 0x0001 /**< Reset on close */ -#define SCARD_UNPOWER_CARD 0x0002 /**< Power down on close */ -#define SCARD_EJECT_CARD 0x0003 /**< Eject on close */ - -#define SCARD_UNKNOWN 0x0001 /**< Unknown state */ -#define SCARD_ABSENT 0x0002 /**< Card is absent */ -#define SCARD_PRESENT 0x0004 /**< Card is present */ -#define SCARD_SWALLOWED 0x0008 /**< Card not powered */ -#define SCARD_POWERED 0x0010 /**< Card is powered */ -#define SCARD_NEGOTIABLE 0x0020 /**< Ready for PTS */ -#define SCARD_SPECIFIC 0x0040 /**< PTS has been set */ - -#define SCARD_STATE_UNAWARE 0x0000 /**< App wants status */ -#define SCARD_STATE_IGNORE 0x0001 /**< Ignore this reader */ -#define SCARD_STATE_CHANGED 0x0002 /**< State has changed */ -#define SCARD_STATE_UNKNOWN 0x0004 /**< Reader unknown */ -#define SCARD_STATE_UNAVAILABLE 0x0008 /**< Status unavailable */ -#define SCARD_STATE_EMPTY 0x0010 /**< Card removed */ -#define SCARD_STATE_PRESENT 0x0020 /**< Card inserted */ -#define SCARD_STATE_ATRMATCH 0x0040 /**< ATR matches card */ -#define SCARD_STATE_EXCLUSIVE 0x0080 /**< Exclusive Mode */ -#define SCARD_STATE_INUSE 0x0100 /**< Shared Mode */ -#define SCARD_STATE_MUTE 0x0200 /**< Unresponsive card */ -#define SCARD_STATE_UNPOWERED 0x0400 /**< Unpowered card */ - - -#define MAXIMUM_SMARTCARD_READERS 10 -#ifndef INFINITE -#define INFINITE 0xFFFFFFFF /**< Infinite timeout */ -#endif - -#define PCSCLITE_VERSION_NUMBER "1.7.4" /**< Current version */ - /** Maximum readers context (a slot is count as a reader) */ -#define PCSCLITE_MAX_READERS_CONTEXTS 16 -#define PCSCLITE_STATUS_WAIT 10000 -#define MAX_READERNAME 100 - -#ifndef SCARD_ATR_LENGTH -#define SCARD_ATR_LENGTH MAX_ATR_SIZE /**< Maximum ATR size */ -#endif - - /* - * The message and buffer sizes must be multiples of 16. - * The max message size must be at least large enough - * to accomodate the transmit_struct - */ -#define MAX_BUFFER_SIZE 264 /**< Maximum Tx/Rx Buffer for short APDU */ -#define MAX_BUFFER_SIZE_EXTENDED (4 + 3 + (1<<16) + 3) /**< enhanced (64K + APDU + Lc + Le) Tx/Rx Buffer */ - - -#ifdef __cplusplus -} -#endif - -#endif +/* + * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html ) + * + * Copyright (C) 1999 + * David Corcoran + * Copyright (C) 2002-2011 + * Ludovic Rousseau + * + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + Changes to this license can be made only by the copyright author with + explicit written consent. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $Id: wintypes.h 6851 2014-02-14 15:43:32Z rousseau $ + */ + +/** + * @file + * @brief This keeps a list of Windows(R) types. + */ + +#ifndef __wintypes_h__ +#define __wintypes_h__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#ifdef __APPLE__ + +#include + +#ifndef BYTE + typedef uint8_t BYTE; +#endif + typedef uint8_t UCHAR; + typedef uint8_t *PUCHAR; + typedef uint16_t USHORT; + +#ifndef __COREFOUNDATION_CFPLUGINCOM__ + typedef uint32_t ULONG; + typedef void *LPVOID; +//haozi err? typedef int16_t BOOL; +#endif + + typedef uint32_t *PULONG; + typedef const void *LPCVOID; + typedef uint32_t DWORD; + typedef uint32_t *PDWORD; + typedef uint16_t WORD; + typedef int32_t LONG; + typedef const char *LPCSTR; + typedef const BYTE *LPCBYTE; + typedef BYTE *LPBYTE; + typedef DWORD *LPDWORD; + typedef char *LPSTR; + +#else + +#ifndef BYTE + typedef unsigned char BYTE; +#endif + typedef unsigned char UCHAR; + typedef unsigned char *PUCHAR; + typedef unsigned short USHORT; + +#ifndef __COREFOUNDATION_CFPLUGINCOM__ + typedef unsigned long ULONG; + typedef void *LPVOID; +#endif + + typedef const void *LPCVOID; + typedef unsigned long DWORD; + typedef unsigned long *PDWORD; + typedef long LONG; + typedef const char *LPCSTR; + typedef const BYTE *LPCBYTE; + typedef BYTE *LPBYTE; + typedef DWORD *LPDWORD; + typedef char *LPSTR; + + /* these types were deprecated but still used by old drivers and + * applications. So just declare and use them. */ + typedef LPSTR LPTSTR; + typedef LPCSTR LPCTSTR; + + /* types unused by pcsc-lite */ + typedef short BOOL; + typedef unsigned short WORD; + typedef unsigned long *PULONG; + +#endif + + + ///////////////////////////////////////// + + +#define SCARD_PCI_T0 (&g_rgSCardT0Pci) /**< protocol control information (PCI) for T=0 */ +#define SCARD_PCI_T1 (&g_rgSCardT1Pci) /**< protocol control information (PCI) for T=1 */ +#define SCARD_PCI_RAW (&g_rgSCardRawPci) /**< protocol control information (PCI) for RAW protocol */ + + /** error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx + */ +#define SCARD_S_SUCCESS ((LONG)0x00000000) /**< No error was encountered. */ +#define SCARD_F_INTERNAL_ERROR ((LONG)0x80100001) /**< An internal consistency check failed. */ +#define SCARD_E_CANCELLED ((LONG)0x80100002) /**< The action was cancelled by an SCardCancel request. */ +#define SCARD_E_INVALID_HANDLE ((LONG)0x80100003) /**< The supplied handle was invalid. */ +#define SCARD_E_INVALID_PARAMETER ((LONG)0x80100004) /**< One or more of the supplied parameters could not be properly interpreted. */ +#define SCARD_E_INVALID_TARGET ((LONG)0x80100005) /**< Registry startup information is missing or invalid. */ +#define SCARD_E_NO_MEMORY ((LONG)0x80100006) /**< Not enough memory available to complete this command. */ +#define SCARD_F_WAITED_TOO_LONG ((LONG)0x80100007) /**< An internal consistency timer has expired. */ +#define SCARD_E_INSUFFICIENT_BUFFER ((LONG)0x80100008) /**< The data buffer to receive returned data is too small for the returned data. */ +#define SCARD_E_UNKNOWN_READER ((LONG)0x80100009) /**< The specified reader name is not recognized. */ +#define SCARD_E_TIMEOUT ((LONG)0x8010000A) /**< The user-specified timeout value has expired. */ +#define SCARD_E_SHARING_VIOLATION ((LONG)0x8010000B) /**< The smart card cannot be accessed because of other connections outstanding. */ +#define SCARD_E_NO_SMARTCARD ((LONG)0x8010000C) /**< The operation requires a Smart Card, but no Smart Card is currently in the device. */ +#define SCARD_E_UNKNOWN_CARD ((LONG)0x8010000D) /**< The specified smart card name is not recognized. */ +#define SCARD_E_CANT_DISPOSE ((LONG)0x8010000E) /**< The system could not dispose of the media in the requested manner. */ +#define SCARD_E_PROTO_MISMATCH ((LONG)0x8010000F) /**< The requested protocols are incompatible with the protocol currently in use with the smart card. */ +#define SCARD_E_NOT_READY ((LONG)0x80100010) /**< The reader or smart card is not ready to accept commands. */ +#define SCARD_E_INVALID_VALUE ((LONG)0x80100011) /**< One or more of the supplied parameters values could not be properly interpreted. */ +#define SCARD_E_SYSTEM_CANCELLED ((LONG)0x80100012) /**< The action was cancelled by the system, presumably to log off or shut down. */ +#define SCARD_F_COMM_ERROR ((LONG)0x80100013) /**< An internal communications error has been detected. */ +#define SCARD_F_UNKNOWN_ERROR ((LONG)0x80100014) /**< An internal error has been detected, but the source is unknown. */ +#define SCARD_E_INVALID_ATR ((LONG)0x80100015) /**< An ATR obtained from the registry is not a valid ATR string. */ +#define SCARD_E_NOT_TRANSACTED ((LONG)0x80100016) /**< An attempt was made to end a non-existent transaction. */ +#define SCARD_E_READER_UNAVAILABLE ((LONG)0x80100017) /**< The specified reader is not currently available for use. */ +#define SCARD_P_SHUTDOWN ((LONG)0x80100018) /**< The operation has been aborted to allow the server application to exit. */ +#define SCARD_E_PCI_TOO_SMALL ((LONG)0x80100019) /**< The PCI Receive buffer was too small. */ +#define SCARD_E_READER_UNSUPPORTED ((LONG)0x8010001A) /**< The reader driver does not meet minimal requirements for support. */ +#define SCARD_E_DUPLICATE_READER ((LONG)0x8010001B) /**< The reader driver did not produce a unique reader name. */ +#define SCARD_E_CARD_UNSUPPORTED ((LONG)0x8010001C) /**< The smart card does not meet minimal requirements for support. */ +#define SCARD_E_NO_SERVICE ((LONG)0x8010001D) /**< The Smart card resource manager is not running. */ +#define SCARD_E_SERVICE_STOPPED ((LONG)0x8010001E) /**< The Smart card resource manager has shut down. */ +#define SCARD_E_UNEXPECTED ((LONG)0x8010001F) /**< An unexpected card error has occurred. */ +#define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x8010001F) /**< This smart card does not support the requested feature. */ +#define SCARD_E_ICC_INSTALLATION ((LONG)0x80100020) /**< No primary provider can be found for the smart card. */ +#define SCARD_E_ICC_CREATEORDER ((LONG)0x80100021) /**< The requested order of object creation is not supported. */ +/* #define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x80100022) / **< This smart card does not support the requested feature. */ +#define SCARD_E_DIR_NOT_FOUND ((LONG)0x80100023) /**< The identified directory does not exist in the smart card. */ +#define SCARD_E_FILE_NOT_FOUND ((LONG)0x80100024) /**< The identified file does not exist in the smart card. */ +#define SCARD_E_NO_DIR ((LONG)0x80100025) /**< The supplied path does not represent a smart card directory. */ +#define SCARD_E_NO_FILE ((LONG)0x80100026) /**< The supplied path does not represent a smart card file. */ +#define SCARD_E_NO_ACCESS ((LONG)0x80100027) /**< Access is denied to this file. */ +#define SCARD_E_WRITE_TOO_MANY ((LONG)0x80100028) /**< The smart card does not have enough memory to store the information. */ +#define SCARD_E_BAD_SEEK ((LONG)0x80100029) /**< There was an error trying to set the smart card file object pointer. */ +#define SCARD_E_INVALID_CHV ((LONG)0x8010002A) /**< The supplied PIN is incorrect. */ +#define SCARD_E_UNKNOWN_RES_MNG ((LONG)0x8010002B) /**< An unrecognized error code was returned from a layered component. */ +#define SCARD_E_NO_SUCH_CERTIFICATE ((LONG)0x8010002C) /**< The requested certificate does not exist. */ +#define SCARD_E_CERTIFICATE_UNAVAILABLE ((LONG)0x8010002D) /**< The requested certificate could not be obtained. */ +#define SCARD_E_NO_READERS_AVAILABLE ((LONG)0x8010002E) /**< Cannot find a smart card reader. */ +#define SCARD_E_COMM_DATA_LOST ((LONG)0x8010002F) /**< A communications error with the smart card has been detected. Retry the operation. */ +#define SCARD_E_NO_KEY_CONTAINER ((LONG)0x80100030) /**< The requested key container does not exist on the smart card. */ +#define SCARD_E_SERVER_TOO_BUSY ((LONG)0x80100031) /**< The Smart Card Resource Manager is too busy to complete this operation. */ + +#define SCARD_W_UNSUPPORTED_CARD ((LONG)0x80100065) /**< The reader cannot communicate with the card, due to ATR string configuration conflicts. */ +#define SCARD_W_UNRESPONSIVE_CARD ((LONG)0x80100066) /**< The smart card is not responding to a reset. */ +#define SCARD_W_UNPOWERED_CARD ((LONG)0x80100067) /**< Power has been removed from the smart card, so that further communication is not possible. */ +#define SCARD_W_RESET_CARD ((LONG)0x80100068) /**< The smart card has been reset, so any shared state information is invalid. */ +#define SCARD_W_REMOVED_CARD ((LONG)0x80100069) /**< The smart card has been removed, so further communication is not possible. */ + +#define SCARD_W_SECURITY_VIOLATION ((LONG)0x8010006A) /**< Access was denied because of a security violation. */ +#define SCARD_W_WRONG_CHV ((LONG)0x8010006B) /**< The card cannot be accessed because the wrong PIN was presented. */ +#define SCARD_W_CHV_BLOCKED ((LONG)0x8010006C) /**< The card cannot be accessed because the maximum number of PIN entry attempts has been reached. */ +#define SCARD_W_EOF ((LONG)0x8010006D) /**< The end of the smart card file has been reached. */ +#define SCARD_W_CANCELLED_BY_USER ((LONG)0x8010006E) /**< The user pressed "Cancel" on a Smart Card Selection Dialog. */ +#define SCARD_W_CARD_NOT_AUTHENTICATED ((LONG)0x8010006F) /**< No PIN was presented to the smart card. */ + +#define SCARD_AUTOALLOCATE (DWORD)(-1) /**< see SCardFreeMemory() */ +#define SCARD_SCOPE_USER 0x0000 /**< Scope in user space */ +#define SCARD_SCOPE_TERMINAL 0x0001 /**< Scope in terminal */ +#define SCARD_SCOPE_SYSTEM 0x0002 /**< Scope in system */ + + +#define SCARD_PROTOCOL_UNDEFINED 0x0000 /**< protocol not set */ +#define SCARD_PROTOCOL_UNSET SCARD_PROTOCOL_UNDEFINED /* backward compat */ +#define SCARD_PROTOCOL_T0 0x0001 /**< T=0 active protocol. */ +#define SCARD_PROTOCOL_T1 0x0002 /**< T=1 active protocol. */ +#define SCARD_PROTOCOL_RAW 0x0004 /**< Raw active protocol. */ +#define SCARD_PROTOCOL_T15 0x0008 /**< T=15 protocol. */ + +#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1) /**< IFD determines prot. */ + +#define SCARD_SHARE_EXCLUSIVE 0x0001 /**< Exclusive mode only */ +#define SCARD_SHARE_SHARED 0x0002 /**< Shared mode only */ +#define SCARD_SHARE_DIRECT 0x0003 /**< Raw mode only */ + +#define SCARD_LEAVE_CARD 0x0000 /**< Do nothing on close */ +#define SCARD_RESET_CARD 0x0001 /**< Reset on close */ +#define SCARD_UNPOWER_CARD 0x0002 /**< Power down on close */ +#define SCARD_EJECT_CARD 0x0003 /**< Eject on close */ + +#define SCARD_UNKNOWN 0x0001 /**< Unknown state */ +#define SCARD_ABSENT 0x0002 /**< Card is absent */ +#define SCARD_PRESENT 0x0004 /**< Card is present */ +#define SCARD_SWALLOWED 0x0008 /**< Card not powered */ +#define SCARD_POWERED 0x0010 /**< Card is powered */ +#define SCARD_NEGOTIABLE 0x0020 /**< Ready for PTS */ +#define SCARD_SPECIFIC 0x0040 /**< PTS has been set */ + +#define SCARD_STATE_UNAWARE 0x0000 /**< App wants status */ +#define SCARD_STATE_IGNORE 0x0001 /**< Ignore this reader */ +#define SCARD_STATE_CHANGED 0x0002 /**< State has changed */ +#define SCARD_STATE_UNKNOWN 0x0004 /**< Reader unknown */ +#define SCARD_STATE_UNAVAILABLE 0x0008 /**< Status unavailable */ +#define SCARD_STATE_EMPTY 0x0010 /**< Card removed */ +#define SCARD_STATE_PRESENT 0x0020 /**< Card inserted */ +#define SCARD_STATE_ATRMATCH 0x0040 /**< ATR matches card */ +#define SCARD_STATE_EXCLUSIVE 0x0080 /**< Exclusive Mode */ +#define SCARD_STATE_INUSE 0x0100 /**< Shared Mode */ +#define SCARD_STATE_MUTE 0x0200 /**< Unresponsive card */ +#define SCARD_STATE_UNPOWERED 0x0400 /**< Unpowered card */ + + +#define MAXIMUM_SMARTCARD_READERS 10 +#ifndef INFINITE +#define INFINITE 0xFFFFFFFF /**< Infinite timeout */ +#endif + +#define PCSCLITE_VERSION_NUMBER "1.7.4" /**< Current version */ + /** Maximum readers context (a slot is count as a reader) */ +#define PCSCLITE_MAX_READERS_CONTEXTS 16 +#define PCSCLITE_STATUS_WAIT 10000 +#define MAX_READERNAME 100 + +#ifndef SCARD_ATR_LENGTH +#define SCARD_ATR_LENGTH MAX_ATR_SIZE /**< Maximum ATR size */ +#endif + + /* + * The message and buffer sizes must be multiples of 16. + * The max message size must be at least large enough + * to accomodate the transmit_struct + */ +#define MAX_BUFFER_SIZE 264 /**< Maximum Tx/Rx Buffer for short APDU */ +#define MAX_BUFFER_SIZE_EXTENDED (4 + 3 + (1<<16) + 3) /**< enhanced (64K + APDU + Lc + Le) Tx/Rx Buffer */ + + +#ifdef __cplusplus +} +#endif + +#endif