def selectType(TYPE = TYPE_AUTO): if TYPE < 0 or TYPE > 0xD: raise apduBuilderException("invalid argument TYPE, a value between 0 and 13 was expected, got "+str(TYPE)) apdu = ApduDefault(cla=0xFF,ins=0xA4,p1=0x00,p2=0x00,data=[TYPE]) apdu.removeExpectedAnswer() return apdu
def I2C_selectPageSize(page_size = I2C_PAGE_SIZE_8BYTES): if page_size < 0x03 or page_size > 0x0A: raise apduBuilderException("invalid argument page_size, a value between 0x03 and 0x07 was expected, got "+str(page_size)) apdu = ApduDefault(cla=0xFF,ins=0x01,p1=0x00,p2=0x00, data=(page_size,)) apdu.removeExpectedAnswer() return apdu
def SLE_checkPinCode(pinBytes): if len(pinBytes) < 2 or len(pinBytes) > 3: raise apduBuilderException("invalid argument pinBytes, a list of bytes of lenght 2 or 3 was expected, got "+str(len(pinBytes))) apdu = ApduDefault(cla=0xFF,ins=0x20,p1=0x00,p2=0x00, data=pinBytes) apdu.removeExpectedAnswer() return apdu
def mifareClassifWrite(blockNumber, datas, Key=None): if blockNumber < 0 or blockNumber > 255: raise apduBuilderException( "invalid argument blockNumber, a value between 0 and 255 was expected, got " + str(blockNumber)) if len(datas) < 1 or len(datas) > 255: raise apduBuilderException( "invalid datas size, must be a list from 1 to 255 item, got " + str(len(datas))) if (len(datas) % 16) != 0: raise apduBuilderException( "invalid datas size, must be a multiple of 16") if Key == None: return ApduDefault(cla=0xFF, ins=0xF4, p1=0x00, p2=blockNumber, data=datas) else: if len(Key) != 6: raise apduBuilderException( "invalid key length, must be 6, got " + str(len(Key))) toSend = [] toSend.extend(datas) toSend.extend(Key) return ApduDefault(cla=0xFF, ins=0xF4, p1=0x00, p2=blockNumber, data=toSend)
def encapsulate(datas, protocolType=0x00, timeoutType=0x00, defaultSW=True): if len(datas) < 1 or len(datas) > 255: raise apduBuilderException( "invalid data size, a value between 1 and 255 was expected, got " + str(len(datas))) if timeoutType < 0 or timeoutType > 0x0D: raise apduBuilderException( "invalid argument timeoutType, a value between 0 and 13 was expected, got " + str(timeoutType)) if protocolType != 0x00 and protocolType != 0x01 and protocolType != 0x02 and protocolType != 0x04 and protocolType != 0x05 and protocolType != 0x09 and protocolType != 0x0A and protocolType != 0x0C and protocolType != 0x0F and protocolType != 0x1F and protocolType != 0x2F and protocolType != 0x3F and protocolType != 0x4F and protocolType != 0x5F and protocolType != 0x6F and protocolType != 0x7F and protocolType != 0x80 and protocolType != 0x81 and protocolType != 0x82 and protocolType != 0x83 and protocolType != 0x84: raise apduBuilderException( "invalid argument protocolType, see the documentation") if not defaultSW: timeoutType &= 0x80 return ApduDefault(cla=0xFF, ins=0xFE, p1=protocolType, p2=timeoutType, data=datas)
def verifyCommand(pin, reference=0, ReaderKey=None): if len(pin) < 1 or len(pin) > 255: raise apduBuilderException( "invalid pin size, must be a list from 1 to 255 item, got " + str(len(pin))) if reference < 0 or reference > 255: raise apduBuilderException( "invalid argument reference, a value between 0 and 255 was expected, got " + str(reference)) structure = 0 if ReaderKey != None: structure |= 0x40 if ReaderKey < 0 or ReaderKey > 15: raise apduBuilderException( "invalid argument ReaderKey, a value between 0 and 255 was expected, got " + str(ReaderKey)) structure |= (ReaderKey & 0xf) return ApduDefault(cla=0xFF, ins=0x20, p1=structure, p2=reference, data=pin)
def loadKey(key, InVolatile=True, isCardKey=True, ReaderKey=None): if len(key) < 1 or len(key) > 255: raise apduBuilderException( "invalid key size, must be a list from 1 to 255 item, got " + str(len(pin))) if KeyIndex < 0 or KeyIndex > 255: raise apduBuilderException( "invalid argument KeyIndex, a value between 0 and 255 was expected, got " + str(KeyIndex)) P1 = 0x00 if not InVolatile: P1 |= 0x20 if not isCardKey: P1 |= 0x80 if ReaderKey != None: if ReaderKey < 0 or ReaderKey > 15: raise apduBuilderException( "invalid argument ReaderKey, a value between 0 and 15 was expected, got " + str(ReaderKey)) P1 |= 0x40 P1 |= (ReaderKey & 0x0f) #the 0x10 bit is Reserved Future Use return ApduDefault(cla=0xFF, ins=0x82, p1=P1, p2=KeyIndex, data=keys[KeyName])
def mifareClassicRead(blockNumber, Key=None): if blockNumber < 0 or blockNumber > 255: raise apduBuilderException( "invalid argument blockNumber, a value between 0 and 255 was expected, got " + str(blockNumber)) if Key == None: return ApduDefault(cla=0xFF, ins=0xF3, p1=0x00, p2=blockNumber) else: if len(Key) != 6: raise apduBuilderException( "invalid key length, must be 6, got " + str(len(Key))) return ApduDefault(cla=0xFF, ins=0xF3, p1=0x00, p2=blockNumber, data=Key)
def write(adress, datas, area = WRITE_AREA_0): if adress < 0 or adress > 0x1ffff: raise apduBuilderException("invalid argument adress, a value between 0 and 1023 was expected, got "+str(TYPE)) if len(datas) < 1 or len(datas) > 255: raise apduBuilderException("invalid datas size, must be a list from 1 to 255 item, got "+str(len(datas))) if area < 0xd0 or area > 0xd4: raise apduBuilderException("invalid argument area, a value between 0xd0 and 0xd4 was expected, got "+str(area)) if adress >= 0x10000: area |= 0x1 msb = (adress >> 8) & 0xFF lsb = adress & 0xFF apdu = ApduDefault(cla=0xFF,ins=area,p1=msb,p2=lsb, data=datas) apdu.removeExpectedAnswer() return apdu
def generalAuthenticate(blockNumber, KeyIndex, InVolatile=True, isTypeA=True): data = [0x01] #Auth version data.extend( pcscAPDUBuilder.getAuthenticateData(blockNumber, KeyIndex, InVolatile, isTypeA)) return ApduDefault(cla=0xFF, ins=0x86, p1=0x00, p2=0x00, data=datas)
def setBuzzerDuration(duration): if duration < 0 or duration > 60000: raise apduBuilderException( "invalid argument duration, a value between 0 and 60000 was expected, got " + str(duration)) lsb = duration & 0xFF msb = (duration >> 8) & 0xFF return ApduDefault(cla=0xFF, ins=0xF0, p1=0x00, p2=0x00, data=[0x1C, msb, lsb])
def readBinary(address=0, expected=0): if address < 0 or address > 65535: raise apduBuilderException( "invalid argument address, a value between 0 and 65535 was expected, got " + str(address)) lsb = address & 0xFF msb = (address >> 8) & 0xFF return ApduDefault(cla=0xFF, ins=0xB0, p1=msb, p2=lsb, expected_answer=expected)
def updateBinary(datas, address=0): if address < 0 or address > 65535: raise apduBuilderException( "invalid argument address, a value between 0 and 65535 was expected, got " + str(address)) if len(datas) < 1 or len(datas) > 255: raise apduBuilderException( "invalid datas size, must be a list from 1 to 255 item, got " + str(len(datas))) lsb = address & 0xFF msb = (address >> 8) & 0xFF return ApduDefault(cla=0xFF, ins=0xD6, p1=msb, p2=lsb, data=datas)
def setLedColorFun(red, green, yellow_blue=None): if red < 0 or red > 5: raise apduBuilderException( "invalid argument red, a value between 0 and 5 was expected, got " + str(red)) if green < 0 or green > 5: raise apduBuilderException( "invalid argument green, a value between 0 and 5 was expected, got " + str(green)) if yellow_blue == None: d = [0x1E, red, green] else: if yellow_blue < 0 or yellow_blue > 5: raise apduBuilderException( "invalid argument yellow_blue, a value between 0 and 5 was expected, got " + str(yellow_blue)) d = [0x1E, red, green, yellow_blue] return ApduDefault(cla=0xFF, ins=0xF0, p1=0x00, p2=0x00, data=d)
def test(expected_answer_size=0, delay_to_answer=0, datas=[]): if expected_answer_size < 0 or expected_answer_size > 255: raise apduBuilderException( "invalid argument expected_answer_size, a value between 0 and 255 was expected, got " + str(expected_answer_size)) if delay_to_answer < 0 or delay_to_answer > 63: raise apduBuilderException( "invalid argument delay_to_answer, a value between 0 and 63 was expected, got " + str(delay_to_answer)) if len(datas) < 0 or len(datas) > 255: raise apduBuilderException( "invalid argument datas, a value list of length 0 to 255 was expected, got " + str(len(datas))) return ApduDefault(cla=0xFF, ins=0xFD, p1=expected_answer_size, p2=delay_to_answer, data=datas)
def getDataCB(P1, P2, Data): return ApduDefault(cla=0xFF, ins=0xCA, p1=P1, p2=P2, data=Data)
def getDataCA(P1, P2): return ApduDefault(cla=0xFF, ins=0xCA, p1=P1, p2=P2)
def configureCalypsoSamDisableInternalDigestUpdate(): return ApduDefault(cla=0xFF, ins=0xFC, p1=0x08, p2=0x01)
def configureCalypsoSamSetSpeed115200(): return ApduDefault(cla=0xFF, ins=0xFC, p1=0x04, p2=0x01)
def slotControlSuspendCardTracking(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x01, p2=0x00)
def slotControlEnableTCLAgain(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x20, p2=0x06)
def slotControlDisableEveryTCL(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x20, p2=0x05)
def slotControlTCLActivationTypeB(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x20, p2=0x02)
def slotControlResetRFField(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x10, p2=0x02)
def slotControlStopRFField(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x10, p2=0x00)
def slotControlResetAfterNextDisconnectAndDisableNextTCL(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x20, p2=0x07)
def slotControlStop(): "stop the slot" return ApduDefault(cla=0xFF, ins=0xFB, p1=0xDE, p2=0xAD)
def slotControlTCLDeactivation(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x20, p2=0x00)
def slotControlResumeCardTracking(): return ApduDefault(cla=0xFF, ins=0xFB, p1=0x00, p2=0x00)