Skip to content

Commit 428aa9b

Browse files
committed
Cleaning up unused code and appending changes missed in #287
1 parent c28597a commit 428aa9b

File tree

4 files changed

+80
-77
lines changed

4 files changed

+80
-77
lines changed

Firmware/Chameleon-Mini/Application/DESFire/DESFireISO14443Support.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
347347
Uid[1] = Uid[0];
348348
Uid[0] = ISO14443A_UID0_CT;
349349
}
350-
if (ISO14443ASelect(Buffer, &BitCount, Uid, SAK_CL1_VALUE)) {
350+
if (ISO14443ASelectDesfire(Buffer, &BitCount, Uid, SAK_CL1_VALUE)) {
351351
/* CL1 stage has ended successfully */
352352
const char *debugPrintStr = PSTR("ISO14443-4: Select OK");
353353
LogDebuggingMsg(debugPrintStr);
@@ -372,7 +372,7 @@ uint16_t ISO144433APiccProcess(uint8_t* Buffer, uint16_t BitCount) {
372372
/* Load UID CL2 and perform anticollision */
373373
ConfigurationUidType Uid;
374374
ApplicationGetUid(Uid);
375-
if (ISO14443ASelect(Buffer, &BitCount, &Uid[3], SAK_CL2_VALUE)) {
375+
if (ISO14443ASelectDesfire(Buffer, &BitCount, &Uid[3], SAK_CL2_VALUE)) {
376376
/* CL2 stage has ended successfully. This means
377377
* our complete UID has been sent to the reader. */
378378
ISO144433ASwitchState(ISO14443_3A_STATE_ACTIVE);

Firmware/Chameleon-Mini/Application/ISO14443-3A.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#define CRC_INIT 0x6363
1111
#define CRC_INIT_R 0xC6C6 /* Bit reversed */
1212

13+
#ifdef CONFIG_MF_DESFIRE_SUPPORT
14+
uint8_t FirstUidCL[4] = { 0 };
15+
#endif
16+
1317
#define USE_HW_CRC
1418

1519
#ifdef USE_HW_CRC

Firmware/Chameleon-Mini/Application/ISO14443-3A.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,80 @@ bool ISO14443ASelect(void *Buffer, uint16_t *BitCount, uint8_t *UidCL, uint8_t S
121121
}
122122
}
123123

124+
#ifdef CONFIG_MF_DESFIRE_SUPPORT
125+
extern uint8_t FirstUidCL[4];
126+
127+
INLINE
128+
bool ISO14443ASelectDesfire(void* Buffer, uint16_t* BitCount, uint8_t* UidCL, uint8_t SAKValue)
129+
{
130+
uint8_t* DataPtr = (uint8_t*) Buffer;
131+
uint8_t NVB = DataPtr[1];
132+
133+
switch (NVB) {
134+
case ISO14443A_NVB_AC_START:
135+
/* Start of anticollision procedure.
136+
* Send whole UID CLn + BCC */
137+
DataPtr[0] = UidCL[0];
138+
DataPtr[1] = UidCL[1];
139+
DataPtr[2] = UidCL[2];
140+
DataPtr[3] = UidCL[3];
141+
DataPtr[ISO14443A_CL_BCC_OFFSET] = ISO14443A_CALC_BCC(DataPtr);
142+
memcpy(FirstUidCL, UidCL, ISO14443A_CL_UID_SIZE);
143+
*BitCount = ISO14443A_CL_FRAME_SIZE;
144+
return false;
145+
146+
case ISO14443A_NVB_AC_END:
147+
/* End of anticollision procedure.
148+
* Send SAK CLn if we are selected. */
149+
if ( (DataPtr[2] == UidCL[0]) &&
150+
(DataPtr[3] == UidCL[1]) &&
151+
(DataPtr[4] == UidCL[2]) &&
152+
(DataPtr[5] == UidCL[3]) ) {
153+
DataPtr[0] = SAKValue;
154+
*BitCount = BITS_PER_BYTE;
155+
return true;
156+
}
157+
else {
158+
/* We have not been selected. Don't send anything. */
159+
*BitCount = 0;
160+
return false;
161+
}
162+
default:
163+
{
164+
uint8_t CollisionByteCount = ((NVB >> 4) & 0x0f) - 2;
165+
uint8_t CollisionBitCount = (NVB >> 0) & 0x0f;
166+
uint8_t mask = 0xFF >> (8 - CollisionBitCount);
167+
// Since the UidCL does not contain the BCC, we have to distinguish here
168+
if (
169+
((CollisionByteCount == 5 || (CollisionByteCount == 4 && CollisionBitCount > 0)) &&
170+
memcmp(UidCL, &DataPtr[2], 4) == 0 && (ISO14443A_CALC_BCC(UidCL) & mask) == (DataPtr[6] & mask))
171+
||
172+
(CollisionByteCount == 4 && CollisionBitCount == 0 && memcmp(UidCL, &DataPtr[2], 4) == 0)
173+
||
174+
(CollisionByteCount < 4 && memcmp(UidCL, &DataPtr[2], CollisionByteCount) == 0 &&
175+
(UidCL[CollisionByteCount] & mask) == (DataPtr[CollisionByteCount + 2] & mask))
176+
)
177+
{
178+
DataPtr[0] = UidCL[0];
179+
DataPtr[1] = UidCL[1];
180+
DataPtr[2] = UidCL[2];
181+
DataPtr[3] = UidCL[3];
182+
DataPtr[4] = ISO14443A_CALC_BCC(DataPtr);
183+
184+
*BitCount = ISO14443A_CL_FRAME_SIZE;
185+
} else {
186+
*BitCount = 0;
187+
}
188+
return false;
189+
}
190+
/* No anticollision supported */
191+
*BitCount = 0;
192+
return false;
193+
}
194+
}
195+
196+
#endif
197+
124198
INLINE
125199
bool ISO14443AWakeUp(void *Buffer, uint16_t *BitCount, uint16_t ATQAValue, bool FromHalt) {
126200
uint8_t *DataPtr = (uint8_t *) Buffer;

Software/DESFireLibNFCTesting/LocalInclude/Iso7816Utils.h

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)