Skip to content

Commit

Permalink
Add CRC_A check function for Decode Reader SELECT and RATS command
Browse files Browse the repository at this point in the history
(cherry picked from commit 4eec449)
  • Loading branch information
zt-chen committed Aug 15, 2018
1 parent 7cc3793 commit c145976
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
29 changes: 28 additions & 1 deletion Software/Chameleon/ISO14443.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import binascii
import crcmod

# Parameters for CRC_A
CRC_INIT = 0x6363
POLY = 0x11021
CRC_A_func = crcmod.mkCrcFun(POLY, initCrc=CRC_INIT, xorOut=0)

ReaderTrafficTypes = {
"SEL":{
Expand Down Expand Up @@ -31,6 +37,22 @@
CardTrafficTypes = {

}

def CRC_A(data):
return CRC_A_func(data)

def CRC_A_check(data):
datalen = len(data)
# Short frame or no space for CRC skip check
if(datalen < 4 ):
return

crc = CRC_A(bytearray(data[0:datalen-2])).to_bytes(2,'little')
if (data[datalen-2:datalen] == crc):
return True
else:
return False

def parseReader(data):
byteCount = len(data)
note = ""
Expand All @@ -51,8 +73,11 @@ def parseReader(data):
note += "SELECT - "
note += ReaderTrafficTypes["SEL"][data[0]]
note += "UID_CLn:" + binascii.hexlify(data[2:7]).decode() + " "
# Check CRC for SELECT
if(not CRC_A_check(data)):
note+=" WRONG CRC "
# note += "7bytes + 0bits "
# note += "CRC_A:"+data[8]
# note += "CRC_A:"+data[7:9]
# HALT Command
elif (byteCount == 4 and data[0] == 0x50 and data[1] == 0x00):
note += "HALT"
Expand All @@ -61,6 +86,8 @@ def parseReader(data):
note += "RATS - "
note += ReaderTrafficTypes["FSDI"][data[1]>>8]
note += "CID:" + str(data[1]&0x0f) + " "
if(not CRC_A_check(data)):
note+=" WRONG CRC "
# PPS Protocol and parameter selection request
# PSS0 only
elif (byteCount == 4 and (data[0]&0xf0 == 0xd0) and (data[1] == 0x01)):
Expand Down
1 change: 1 addition & 0 deletions Software/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pyserial
crcmod

0 comments on commit c145976

Please sign in to comment.