Skip to content

Commit

Permalink
Merge pull request #288 from maxieds/desfire-updates
Browse files Browse the repository at this point in the history
Updates to DESfire support code in #287
  • Loading branch information
fptrs authored Oct 23, 2020
2 parents c28597a + 428aa9b commit f19cd6f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
Uid[1] = Uid[0];
Uid[0] = ISO14443A_UID0_CT;
}
if (ISO14443ASelect(Buffer, &BitCount, Uid, SAK_CL1_VALUE)) {
if (ISO14443ASelectDesfire(Buffer, &BitCount, Uid, SAK_CL1_VALUE)) {
/* CL1 stage has ended successfully */
const char *debugPrintStr = PSTR("ISO14443-4: Select OK");
LogDebuggingMsg(debugPrintStr);
Expand All @@ -372,7 +372,7 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
/* Load UID CL2 and perform anticollision */
ConfigurationUidType Uid;
ApplicationGetUid(Uid);
if (ISO14443ASelect(Buffer, &BitCount, &Uid[3], SAK_CL2_VALUE)) {
if (ISO14443ASelectDesfire(Buffer, &BitCount, &Uid[3], SAK_CL2_VALUE)) {
/* CL2 stage has ended successfully. This means
* our complete UID has been sent to the reader. */
ISO144433ASwitchState(ISO14443_3A_STATE_ACTIVE);
Expand Down
4 changes: 4 additions & 0 deletions Firmware/Chameleon-Mini/Application/ISO14443-3A.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#define CRC_INIT 0x6363
#define CRC_INIT_R 0xC6C6 /* Bit reversed */

#ifdef CONFIG_MF_DESFIRE_SUPPORT
uint8_t FirstUidCL[4] = { 0 };
#endif

#define USE_HW_CRC

#ifdef USE_HW_CRC
Expand Down
74 changes: 74 additions & 0 deletions Firmware/Chameleon-Mini/Application/ISO14443-3A.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,80 @@ bool ISO14443ASelect(void *Buffer, uint16_t *BitCount, uint8_t *UidCL, uint8_t S
}
}

#ifdef CONFIG_MF_DESFIRE_SUPPORT
extern uint8_t FirstUidCL[4];

INLINE
bool ISO14443ASelectDesfire(void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue)
{
uint8_t* DataPtr = (uint8_t*) Buffer;
uint8_t NVB = DataPtr[1];

switch (NVB) {
case ISO14443A_NVB_AC_START:
/* Start of anticollision procedure.
* Send whole UID CLn + BCC */
DataPtr[0] = UidCL[0];
DataPtr[1] = UidCL[1];
DataPtr[2] = UidCL[2];
DataPtr[3] = UidCL[3];
DataPtr[ISO14443A_CL_BCC_OFFSET] = ISO14443A_CALC_BCC(DataPtr);
memcpy(FirstUidCL, UidCL, ISO14443A_CL_UID_SIZE);
*BitCount = ISO14443A_CL_FRAME_SIZE;
return false;

case ISO14443A_NVB_AC_END:
/* End of anticollision procedure.
* Send SAK CLn if we are selected. */
if ( (DataPtr[2] == UidCL[0]) &&
(DataPtr[3] == UidCL[1]) &&
(DataPtr[4] == UidCL[2]) &&
(DataPtr[5] == UidCL[3]) ) {
DataPtr[0] = SAKValue;
*BitCount = BITS_PER_BYTE;
return true;
}
else {
/* We have not been selected. Don't send anything. */
*BitCount = 0;
return false;
}
default:
{
uint8_t CollisionByteCount = ((NVB >> 4) & 0x0f) - 2;
uint8_t CollisionBitCount = (NVB >> 0) & 0x0f;
uint8_t mask = 0xFF >> (8 - CollisionBitCount);
// Since the UidCL does not contain the BCC, we have to distinguish here
if (
((CollisionByteCount == 5 || (CollisionByteCount == 4 && CollisionBitCount > 0)) &&
memcmp(UidCL, &DataPtr[2], 4) == 0 && (ISO14443A_CALC_BCC(UidCL) & mask) == (DataPtr[6] & mask))
||
(CollisionByteCount == 4 && CollisionBitCount == 0 && memcmp(UidCL, &DataPtr[2], 4) == 0)
||
(CollisionByteCount < 4 && memcmp(UidCL, &DataPtr[2], CollisionByteCount) == 0 &&
(UidCL[CollisionByteCount] & mask) == (DataPtr[CollisionByteCount + 2] & mask))
)
{
DataPtr[0] = UidCL[0];
DataPtr[1] = UidCL[1];
DataPtr[2] = UidCL[2];
DataPtr[3] = UidCL[3];
DataPtr[4] = ISO14443A_CALC_BCC(DataPtr);

*BitCount = ISO14443A_CL_FRAME_SIZE;
} else {
*BitCount = 0;
}
return false;
}
/* No anticollision supported */
*BitCount = 0;
return false;
}
}

#endif

INLINE
bool ISO14443AWakeUp(void *Buffer, uint16_t *BitCount, uint16_t ATQAValue, bool FromHalt) {
uint8_t *DataPtr = (uint8_t *) Buffer;
Expand Down
75 changes: 0 additions & 75 deletions Software/DESFireLibNFCTesting/LocalInclude/Iso7816Utils.h

This file was deleted.

0 comments on commit f19cd6f

Please sign in to comment.