def nfcType3Read(ServiceCodeList, BlockCodeList): Data = [] for s in ServiceCodeList: if s < 0 or s > 0xFFFF: raise apduBuilderException( "invalid argument in service code list, a value between 0 and 65535 was expected, got " + str(s)) lsb = s & 0xFF msb = (s >> 8) & 0xFF Data.extend([msb, lsb]) for s in BlockCodeList: if s < 0 or s > 0xFFFF: raise apduBuilderException( "invalid argument in block code list, a value between 0 and 65535 was expected, got " + str(s)) lsb = s & 0xFF msb = (s >> 8) & 0xFF Data.extend([msb, lsb]) return ApduDefault(cla=0xFF, ins=0x46, p1=len(ServiceCodes), p2=len(BlockCodes), data=Data)
def nfcType3Write(ServiceCodeList,BlockCodeList,DataList): #check len(BlockCodeList) vs len(DataList) if len(BlockCodeList) * 16 != len(DataList): raise apduBuilderException("invalid DataList size, must be a list equals to "+str(len(BlockCodeList) * 16)+" bytes, got "+str(len(DataList))) Data = [] for s in ServiceCodeList: if s < 0 or s > 0xFFFF: raise apduBuilderException("invalid argument in service code list, a value between 0 and 65535 was expected, got "+str(s)) lsb = s&0xFF msb = (s>>8)&0xFF Data.extend([msb,lsb]) for s in BlockCodeList: if s < 0 or s > 0xFFFF: raise apduBuilderException("invalid argument in block code list, a value between 0 and 65535 was expected, got "+str(s)) lsb = s&0xFF msb = (s>>8)&0xFF Data.extend([msb,lsb]) #append DataList Data.extend(DataList) return ApduDefault(cla=0xFF,ins=0x48,p1=len(ServiceCodes),p2=len(BlockCodes),data=Data)
def nfcType1WriteByte(byteValue, byteNumber=0, blockNumber=0, erase=True): if byteValue < 0 or byteValue > 255: raise apduBuilderException( "invalid argument byteValue, a value between 0 and 255 was expected, got " + str(byteValue)) if byteNumber < 0 or byteNumber > 7: raise apduBuilderException( "invalid argument byteNumber, a value between 0 and 7 was expected, got " + str(byteNumber)) if blockNumber < 0 or blockNumber > 0xE: raise apduBuilderException( "invalid argument byteNumber, a value between 0 and 14 was expected, got " + str(byteNumber)) P2 = (byteNumber & 0x7) | ((blockNumber & 0x0f) << 3) if erase: return ApduDefault(cla=0xFF, ins=0x56, p1=0x00, p2=P2, data=[byteValue]) else: return ApduDefault(cla=0xFF, ins=0x58, p1=0x00, p2=P2, data=[byteValue])
def mifareClassifWrite(blockNumber,datas,KeyName = 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 KeyName == None: return ApduDefault(cla=0xFF,ins=0xF4,p1=0x00,p2=blockNumber,data=datas) else: if KeyName not in keys: raise apduBuilderException("the key name doesn't exist "+str(KeyName)) if len(keys[KeyName]) != 6: raise apduBuilderException("invalid key length, must be 6, got "+str(len(keys[KeyName]))) #if len(KeyName) != 6: # raise apduBuilderException("invalid key size, must be 6, got "+str(len(KeyName))) toSend = [] toSend.extend(datas) toSend.extend(keys[KeyName]) return ApduDefault(cla=0xFF,ins=0xF4,p1=0x00,p2=blockNumber,data=toSend)
def increment(address=0, value=0x01, increment=True): if address < 0 or address > 255: raise apduBuilderException( "invalid argument address, a value between 0 and 255 was expected, got " + str(address)) if value < 0 or value > 0xFFFFFFFF: raise apduBuilderException( "invalid argument value, a value between 0 and 4294967295 was expected, got " + str(value)) if increment: return ApduDefault(cla=0xFF, ins=0xF0, p1=0x00, p2=address, data=[ 0xC0, address, value & 0xFF, (value >> 8) & 0xFF, (value >> 16) & 0xFF, (value >> 24) & 0xFF ]) else: return ApduDefault(cla=0xFF, ins=0xF0, p1=0x00, p2=address, data=[ 0xC1, address, value & 0xFF, (value >> 8) & 0xFF, (value >> 16) & 0xFF, (value >> 24) & 0xFF ])
def updateBinary(datas,address=0): if address < 0 or address > 255: raise apduBuilderException("invalid argument address, a value between 0 and 255 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))) return ApduDefault(cla=0xFF,ins=0xD6,p1=0x00,p2=address&0xFF,data=datas)
def writeSector(datas, address=0): if address < 0 or address > 0x0F: raise apduBuilderException("invalid argument address, a value between 0 and 15 was expected, got "+str(address)) if len(datas) != 4: raise apduBuilderException("invalid datas size, must be a list with 4 item, got "+str(len(datas))) return ApduMifareUltralight(ins=0xA2,param=(address&0xFF),data=datas)
def nfcType1ReadByte(byteNumber=0,blockNumber=0): if byteNumber < 0 or byteNumber > 7: raise apduBuilderException("invalid argument byteNumber, a value between 0 and 7 was expected, got "+str(byteNumber)) if blockNumber < 0 or blockNumber > 0xE: raise apduBuilderException("invalid argument byteNumber, a value between 0 and 14 was expected, got "+str(byteNumber)) P2 = (byteNumber&0x7) | ((blockNumber&0x0f)<<3) return ApduDefault(cla=0xFF,ins=0x54,p1=0x00,p2=P2)
def CompatibilityWrite(datas, address=0): if address < 0 or address > 0x0F: raise apduBuilderException("invalid argument address, a value between 0 and 15 was expected, got "+str(address)) if len(datas) != 4: raise apduBuilderException("invalid datas size, must be a list with 4 item, got "+str(len(datas))) datas.extend([0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) return ApduMifareUltralight(ins=0xA0,param=(address&0xFF),data=datas)
def InListPassiveTarget(BrTy,MaxTg=1,InitiatorData=[]): if MaxTg < 1 or MaxTg > 2: raise apduBuilderException("invalid argument MaxTg, a value between 1 and 2 was expected, got "+str(MaxTg)) if BrTy < 0 or BrTy > 4: raise apduBuilderException("invalid argument BrTy, a value between 0 and 4 was expected, got "+str(BrTy)) data = [MaxTg,BrTy] data.extend(InitiatorData) return ApduPn53x(0x4A,data)
def increment(address=0,value=0x01,increment=True): if address < 0 or address > 255: raise apduBuilderException("invalid argument address, a value between 0 and 255 was expected, got "+str(address)) if value < 0 or value > 0xFFFFFFFF: raise apduBuilderException("invalid argument value, a value between 0 and 4294967295 was expected, got "+str(value)) if increment: return ApduDefault(cla=0xFF,ins=0xF0,p1=0x00,p2=address,data=[0xC0,address,value&0xFF,(value>>8)&0xFF,(value>>16)&0xFF,(value>>24)&0xFF]) else: return ApduDefault(cla=0xFF,ins=0xF0,p1=0x00,p2=address,data=[0xC1,address,value&0xFF,(value>>8)&0xFF,(value>>16)&0xFF,(value>>24)&0xFF])
def nfcType3REQC(serviceCode,RFU,TSN): if serviceCode < 0 or serviceCode > 0xFFFF: raise apduBuilderException("invalid argument serviceCode, a value between 0 and 65535 was expected, got "+str(serviceCode)) if RFU < 0 or RFU > 0xFF: raise apduBuilderException("invalid argument RFU, a value between 0 and 255 was expected, got "+str(RFU)) if TSN < 0 or TSN > 0xFF: raise apduBuilderException("invalid argument TSN, a value between 0 and 255 was expected, got "+str(TSN)) return ApduDefault(cla=0xFF,ins=0x40,p1=0x00,p2=0x00,data=[(serviceCode>>8)&0xFF,serviceCode&0xFF,RFU,TSN])
def nfcType1Write8Bytes(bytes,address=0,erase=True): if address < 0 or address > 0xFF: raise apduBuilderException("invalid argument address, a value between 0 and 255 was expected, got "+str(address)) if len(bytes) != 8: raise apduBuilderException("invalid bytes size, must be a list of 8 item, got "+str(len(bytes))) if erase: return ApduDefault(cla=0xFF,ins=0x5E,p1=0x00,p2=address,data=bytes) else: return ApduDefault(cla=0xFF,ins=0x60,p1=0x00,p2=address,data=bytes)
def mifareDecrement(address,value = 0): if address < 0 or address > 65535: raise apduBuilderException("invalid argument address, a value between 0 and 65535 was expected, got "+str(address)) if value < 0 or value > 0xffffffff: raise apduBuilderException("invalid argument value, a value between 0 and "+str(0xffffffff)+" was expected, got "+str(value)) lsb = address&0xFF msb = (address>>8)&0xFF return ApduDefault(cla=0xFF,ins=0xD8,p1=msb,p2=lsb,data=[value&0xff , (value>>8)&0xff , (value>>16)&0xff , (value>>24)&0xff])
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 ledAndBuzzerControl(initialRed, initialGreen, finalRed, finalGreen, T1Duration, T2Duration, Repetition, LinkToBuzzer): "acr122 manage led and buzzer" P2 = 0 if finalRed != None: if finalRed: P2 |= 0x01 P2 |= 0x04 if finalGreen != None: if finalGreen: P2 |= 0x02 P2 |= 0x08 if initialRed != None: if initialRed: P2 |= 0x10 P2 |= 0x40 if initialGreen != None: if initialGreen: P2 |= 0x20 P2 |= 0x80 if T1Duration < 1 or T1Duration > 255: raise apduBuilderException( "invalid argument T1Duration, must be a value between 1 and 255, got " + str(T1Duration)) if T2Duration < 1 or T2Duration > 255: raise apduBuilderException( "invalid argument T2Duration, must be a value between 1 and 255, got " + str(T2Duration)) if Repetition < 1 or Repetition > 255: raise apduBuilderException( "invalid argument Repetition, must be a value between 1 and 255, got " + str(Repetition)) if LinkToBuzzer < 0 or LinkToBuzzer > 3: raise apduBuilderException( "invalid argument LinkToBuzzer, must be a value between 0 and 4, got " + str(LinkToBuzzer)) return ApduDefault( cla=0xFF, ins=0x00, p1=0x40, p2=P2, data=[T1Duration, T2Duration, Repetition, LinkToBuzzer])
def nfcType1ReadByte(byteNumber=0, blockNumber=0): if byteNumber < 0 or byteNumber > 7: raise apduBuilderException( "invalid argument byteNumber, a value between 0 and 7 was expected, got " + str(byteNumber)) if blockNumber < 0 or blockNumber > 0xE: raise apduBuilderException( "invalid argument byteNumber, a value between 0 and 14 was expected, got " + str(byteNumber)) P2 = (byteNumber & 0x7) | ((blockNumber & 0x0f) << 3) return ApduDefault(cla=0xFF, ins=0x54, p1=0x00, p2=P2)
def InListPassiveTarget(BrTy, MaxTg=1, InitiatorData=[]): if MaxTg < 1 or MaxTg > 2: raise apduBuilderException( "invalid argument MaxTg, a value between 1 and 2 was expected, got " + str(MaxTg)) if BrTy < 0 or BrTy > 4: raise apduBuilderException( "invalid argument BrTy, a value between 0 and 4 was expected, got " + str(BrTy)) data = [MaxTg, BrTy] data.extend(InitiatorData) return ApduPn53x(0x4A, data)
def encapsulate(datas,protocolType=0x00,timeoutType=0x00,defaultSW = True): #TODO limite de 255 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 writeSector(datas, address=0): if address < 0 or address > 0x0F: raise apduBuilderException( "invalid argument address, a value between 0 and 15 was expected, got " + str(address)) if len(datas) != 4: raise apduBuilderException( "invalid datas size, must be a list with 4 item, got " + str(len(datas))) return ApduMifareUltralight(ins=0xA2, param=(address & 0xFF), 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 InSelect(): if target < 0 or target > 2: raise apduBuilderException( "invalid argument target, a value between 0 and 2 was expected, got " + str(target)) return ApduPn53x(0x54, [target])
def InDataExchange(target, args=[]): if target < 1 or target > 2: raise apduBuilderException( "invalid argument target, a value between 1 and 2 was expected, got " + str(target)) return ApduPn53x(0x02, args)
def directTransmit(args): "acr122 transmit data to pn532" if len(args) < 1 or len(args) > 255: raise apduBuilderException("invalid args size, must be a list from 1 to 255 item, got "+str(len(datas))) return ApduDefault(cla=0xFF,ins=0x00,p1=0x00,p2=0x00,data=args)
def getResponse(Length): "acr122 get response from pn532" if Length < 1 or Length > 255: raise apduBuilderException("invalid argument Length, must be a value between 1 and 255, got "+str(Length)) return ApduDefault(cla=0xFF,ins=0xC0,p1=0x00,p2=0x00,expected_answer=Length)
def nfcType1Read8bytes(address=0): if address < 0 or address > 0xFF: raise apduBuilderException( "invalid argument address, a value between 0 and 255 was expected, got " + str(address)) return ApduDefault(cla=0xFF, ins=0x5C, p1=0x00, p2=address)
def updateBinary(datas, address=0): if address < 0 or address > 255: raise apduBuilderException( "invalid argument address, a value between 0 and 255 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))) return ApduDefault(cla=0xFF, ins=0xD6, p1=0x00, p2=address & 0xFF, data=datas)
def readBinary(address=0): if address < 0 or address > 255: raise apduBuilderException( "invalid argument address, a value between 0 and 255 was expected, got " + str(address)) return ApduDefault(cla=0xFF, ins=0xB0, p1=0x00, p2=address & 0xFF)
def readSector(address=0): if address < 0 or address > 0x0F: raise apduBuilderException( "invalid argument address, a value between 0 and 15 was expected, got " + str(address)) return ApduMifareUltralight(ins=0x30, param=(address & 0xFF))
def nfcType1WriteByte(byteValue,byteNumber=0,blockNumber=0,erase=True): if byteValue < 0 or byteValue > 255: raise apduBuilderException("invalid argument byteValue, a value between 0 and 255 was expected, got "+str(byteValue)) if byteNumber < 0 or byteNumber > 7: raise apduBuilderException("invalid argument byteNumber, a value between 0 and 7 was expected, got "+str(byteNumber)) if blockNumber < 0 or blockNumber > 0xE: raise apduBuilderException("invalid argument byteNumber, a value between 0 and 14 was expected, got "+str(byteNumber)) P2 = (byteNumber&0x7) | ((blockNumber&0x0f)<<3) if erase: return ApduDefault(cla=0xFF,ins=0x56,p1=0x00,p2=P2,data=[byteValue]) else: return ApduDefault(cla=0xFF,ins=0x58,p1=0x00,p2=P2,data=[byteValue])
def generalAuthenticate(blockNumber,isTypeA = True,KeyIndex = 0): if blockNumber < 0 or blockNumber > 65535: raise apduBuilderException("invalid argument blockNumber, a value between 0 and 65535 was expected, got "+str(blockNumber)) lsb = address&0xFF msb = (address>>8)&0xFF if KeyIndex < 0 or KeyIndex > 255: #verification hasardeuse, le nombre de cle dans la doc n'est pas dis raise apduBuilderException("invalid argument KeyIndex, a value between 0 and 255 was expected, got "+str(KeyIndex)) if isTypeA: datas = [0x01,msb,lsb,0x60,KeyIndex] else: datas = [0x01,msb,lsb,0x61,KeyIndex] return ApduDefault(cla=0xFF,ins=0x88,p1=0x00,p2=0x00,data=datas)
def sendCommandGenericSession(Cmd): if len(Cmd) < 1 or len(Cmd) > 249: raise apduBuilderException("invalid Cmd size, must be a list from 1 to 249 item, got "+str(len(Cmd))) d = [0x01, 0x00,0xF3, 0x00, 0x00, 0x64] d.extend(Cmd) return ApduDefault(cla=0xFF,ins=0xA0,p1=0x00,p2=0x05,data=[0x01 , 0x00 , 0x02])
def selectType(TYPE=0): if TYPE < 0 or TYPE > 0xD: raise apduBuilderException( "invalid argument TYPE, a value between 0 and 13 was expected, got " + str(TYPE)) return ApduDefault(cla=0xFF, ins=0xA4, p1=0x00, p2=0x00, data=[TYPE])
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 mifareClassicRead(blockNumber,KeyName = None): if blockNumber < 0 or blockNumber > 255: raise apduBuilderException("invalid argument blockNumber, a value between 0 and 255 was expected, got "+str(blockNumber)) if KeyName == None: return ApduDefault(cla=0xFF,ins=0xF3,p1=0x00,p2=blockNumber) else: if KeyName not in keys: raise apduBuilderException("the key name doesn't exist "+str(KeyName)) if len(keys[KeyName]) != 6: raise apduBuilderException("invalid key length, must be 6, got "+str(len(keys[KeyName]))) #if len(KeyName) != 6: # raise apduBuilderException("invalid key size, must be 6, got "+str(len(KeyName))) return ApduDefault(cla=0xFF,ins=0xF3,p1=0x00,p2=blockNumber,data=keys[KeyName])
def iclassGetKeySlotInfo(KeySlot=0,Secured=False): if KeySlot < 0 or KeySlot > 255: raise apduBuilderException("invalid argument KeySlot, a value between 0 and 255 was expected, got "+str(address)) if Secured: return ApduDefault(cla=0x84,ins=0xC4,p1=0x00,p2=KeySlot,data=[]) else: return ApduDefault(cla=0x80,ins=0xC4,p1=0x00,p2=KeySlot,data=[])
def directTransmit(args): "acr122 transmit data to pn532" if len(args) < 1 or len(args) > 255: raise apduBuilderException( "invalid args size, must be a list from 1 to 255 item, got " + str(len(datas))) return ApduDefault(cla=0xFF, ins=0x00, p1=0x00, p2=0x00, data=args)
def CompatibilityWrite(datas, address=0): if address < 0 or address > 0x0F: raise apduBuilderException( "invalid argument address, a value between 0 and 15 was expected, got " + str(address)) if len(datas) != 4: raise apduBuilderException( "invalid datas size, must be a list with 4 item, got " + str(len(datas))) datas.extend([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]) return ApduMifareUltralight(ins=0xA0, param=(address & 0xFF), data=datas)
def nfcType1ReadSegment(address=0): if address < 0 or address > 0xF: raise apduBuilderException( "invalid argument address, a value between 0 and 15 was expected, got " + str(address)) return ApduDefault(cla=0xFF, ins=0x5A, p1=0x00, p2=((address & 0xF) << 4))
def generalAuthenticate(blockNumber, isTypeA=True, KeyIndex=0): if blockNumber < 0 or blockNumber > 65535: raise apduBuilderException( "invalid argument blockNumber, a value between 0 and 65535 was expected, got " + str(blockNumber)) lsb = address & 0xFF msb = (address >> 8) & 0xFF if KeyIndex < 0 or KeyIndex > 255: #verification hasardeuse, le nombre de cle dans la doc n'est pas dis raise apduBuilderException( "invalid argument KeyIndex, a value between 0 and 255 was expected, got " + str(KeyIndex)) if isTypeA: datas = [0x01, msb, lsb, 0x60, KeyIndex] else: datas = [0x01, msb, lsb, 0x61, KeyIndex] return ApduDefault(cla=0xFF, ins=0x88, p1=0x00, p2=0x00, data=datas)
def loadKey(KeyIndex,KeyName,InVolatile = True ,isTypeA = True): if KeyName not in keys: raise apduBuilderException("the key name doesn't exist "+str(KeyName)) #only allow mifare key if len(keys[KeyName]) != 6: raise apduBuilderException("invalid key length, must be 6, got "+str(len(keys[KeyName]))) #proxnroll specific params if InVolatile: if KeyIndex < 0 or KeyIndex > 3: raise apduBuilderException("invalid argument KeyIndex, a value between 0 and 3 was expected, got "+str(KeyIndex)) else: if KeyIndex < 0 or KeyIndex > 15: raise apduBuilderException("invalid argument KeyIndex, a value between 0 and 15 was expected, got "+str(KeyIndex)) #B key special index if not isTypeA: KeyIndex &= 0x10 return pcscAPDUBuilder.loadKey(KeyIndex,KeyName,InVolatile)
def loadKey(KeyName,isTypeA = True): #TODO KeyName if len(Key) != 6: raise apduBuilderException("invalid key size, must be 6, got "+str(len(Key))) if isTypeA: P2 = 0x60 else: P2 = 0x61 return ApduDefault(cla=0xFF,ins=0x82,p1=0x00,p2=P2,data=Key)
def nfcType3Read(ServiceCodeList,BlockCodeList): Data = [] for s in ServiceCodeList: if s < 0 or s > 0xFFFF: raise apduBuilderException("invalid argument in service code list, a value between 0 and 65535 was expected, got "+str(s)) lsb = s&0xFF msb = (s>>8)&0xFF Data.extend([msb,lsb]) for s in BlockCodeList: if s < 0 or s > 0xFFFF: raise apduBuilderException("invalid argument in block code list, a value between 0 and 65535 was expected, got "+str(s)) lsb = s&0xFF msb = (s>>8)&0xFF Data.extend([msb,lsb]) return ApduDefault(cla=0xFF,ins=0x46,p1=len(ServiceCodes),p2=len(BlockCodes),data=Data)
def nfcType3Write(ServiceCodeList, BlockCodeList, DataList): #check len(BlockCodeList) vs len(DataList) if len(BlockCodeList) * 16 != len(DataList): raise apduBuilderException( "invalid DataList size, must be a list equals to " + str(len(BlockCodeList) * 16) + " bytes, got " + str(len(DataList))) Data = [] for s in ServiceCodeList: if s < 0 or s > 0xFFFF: raise apduBuilderException( "invalid argument in service code list, a value between 0 and 65535 was expected, got " + str(s)) lsb = s & 0xFF msb = (s >> 8) & 0xFF Data.extend([msb, lsb]) for s in BlockCodeList: if s < 0 or s > 0xFFFF: raise apduBuilderException( "invalid argument in block code list, a value between 0 and 65535 was expected, got " + str(s)) lsb = s & 0xFF msb = (s >> 8) & 0xFF Data.extend([msb, lsb]) #append DataList Data.extend(DataList) return ApduDefault(cla=0xFF, ins=0x48, p1=len(ServiceCodes), p2=len(BlockCodes), data=Data)
def mifareDecrement(address, value=0): if address < 0 or address > 65535: raise apduBuilderException( "invalid argument address, a value between 0 and 65535 was expected, got " + str(address)) if value < 0 or value > 0xffffffff: raise apduBuilderException( "invalid argument value, a value between 0 and " + str(0xffffffff) + " was expected, got " + str(value)) lsb = address & 0xFF msb = (address >> 8) & 0xFF return ApduDefault(cla=0xFF, ins=0xD8, p1=msb, p2=lsb, data=[ value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, (value >> 24) & 0xff ])
def sendCommandGenericSession(Cmd): if len(Cmd) < 1 or len(Cmd) > 249: raise apduBuilderException( "invalid Cmd size, must be a list from 1 to 249 item, got " + str(len(Cmd))) d = [0x01, 0x00, 0xF3, 0x00, 0x00, 0x64] d.extend(Cmd) return ApduDefault(cla=0xFF, ins=0xA0, p1=0x00, p2=0x05, data=[0x01, 0x00, 0x02])
def loadKey(KeyName, isTypeA=True): #TODO KeyName if len(Key) != 6: raise apduBuilderException("invalid key size, must be 6, got " + str(len(Key))) if isTypeA: P2 = 0x60 else: P2 = 0x61 return ApduDefault(cla=0xFF, ins=0x82, p1=0x00, p2=P2, data=Key)
def nfcType3REQC(serviceCode, RFU, TSN): if serviceCode < 0 or serviceCode > 0xFFFF: raise apduBuilderException( "invalid argument serviceCode, a value between 0 and 65535 was expected, got " + str(serviceCode)) if RFU < 0 or RFU > 0xFF: raise apduBuilderException( "invalid argument RFU, a value between 0 and 255 was expected, got " + str(RFU)) if TSN < 0 or TSN > 0xFF: raise apduBuilderException( "invalid argument TSN, a value between 0 and 255 was expected, got " + str(TSN)) return ApduDefault(cla=0xFF, ins=0x40, p1=0x00, p2=0x00, data=[(serviceCode >> 8) & 0xFF, serviceCode & 0xFF, RFU, TSN])
def ledAndBuzzerControl(initialRed,initialGreen,finalRed,finalGreen,T1Duration,T2Duration,Repetition,LinkToBuzzer): "acr122 manage led and buzzer" P2 = 0 if finalRed != None: if finalRed: P2 |= 0x01 P2 |= 0x04 if finalGreen != None: if finalGreen: P2 |= 0x02 P2 |= 0x08 if initialRed != None: if initialRed: P2 |= 0x10 P2 |= 0x40 if initialGreen != None: if initialGreen: P2 |= 0x20 P2 |= 0x80 if T1Duration < 1 or T1Duration > 255: raise apduBuilderException("invalid argument T1Duration, must be a value between 1 and 255, got "+str(T1Duration)) if T2Duration < 1 or T2Duration > 255: raise apduBuilderException("invalid argument T2Duration, must be a value between 1 and 255, got "+str(T2Duration)) if Repetition < 1 or Repetition > 255: raise apduBuilderException("invalid argument Repetition, must be a value between 1 and 255, got "+str(Repetition)) if LinkToBuzzer < 0 or LinkToBuzzer > 3: raise apduBuilderException("invalid argument LinkToBuzzer, must be a value between 0 and 4, got "+str(LinkToBuzzer)) return ApduDefault(cla=0xFF,ins=0x00,p1=0x40,p2=P2,data=[T1Duration,T2Duration,Repetition,LinkToBuzzer])
def generalAuthenticate(blockNumber,KeyIndex,InVolatile = True ,isTypeA = True): if blockNumber < 0 or blockNumber > 255: raise apduBuilderException("invalid argument blockNumber, a value between 0 and 255 was expected, got "+str(blockNumber)) if InVolatile: if KeyIndex < 0 or KeyIndex > 3: raise apduBuilderException("invalid argument KeyIndex, a value between 0 and 3 was expected, got "+str(KeyIndex)) #volatile key special index KeyIndex &= 0x20 else: if KeyIndex < 0 or KeyIndex > 15: raise apduBuilderException("invalid argument KeyIndex, a value between 0 and 15 was expected, got "+str(KeyIndex)) #if isTypeA: # datas = [0x01,0x00,blockNumber,0x60,KeyIndex] #else: # datas = [0x01,0x00,blockNumber,0x61,KeyIndex] # ap = pcscAPDUBuilder.generalAuthenticate(blockNumber,KeyIndex,InVolatile,isTypeA) ap.setIns(0x88) return ap
def nfcType1ReadSegment(address=0): if address < 0 or address > 0xF: raise apduBuilderException("invalid argument address, a value between 0 and 15 was expected, got "+str(address)) return ApduDefault(cla=0xFF,ins=0x5A,p1=0x00,p2=((address&0xF)<<4))
def readBinary(address = 0): if address < 0 or address > 255: raise apduBuilderException("invalid argument address, a value between 0 and 255 was expected, got "+str(address)) return ApduDefault(cla=0xFF,ins=0xB0,p1=0x00,p2=address&0xFF)
def nfcType1Read8bytes(address=0): if address < 0 or address > 0xFF: raise apduBuilderException("invalid argument address, a value between 0 and 255 was expected, got "+str(address)) return ApduDefault(cla=0xFF,ins=0x5C,p1=0x00,p2=address)