@@ -174,10 +174,11 @@ uint16_t MifareDesfireProcessCommand(uint8_t *Buffer, uint16_t ByteCount) {
174
174
uint16_t MifareDesfireProcess (uint8_t * Buffer , uint16_t BitCount ) {
175
175
size_t ByteCount = (BitCount + BITS_PER_BYTE - 1 ) / BITS_PER_BYTE ;
176
176
DesfireCmdCLA = Buffer [0 ];
177
- LogEntry (LOG_INFO_DESFIRE_INCOMING_DATA , Buffer , ByteCount );
177
+ // LogEntry(LOG_INFO_DESFIRE_INCOMING_DATA, Buffer, ByteCount);
178
178
if (BitCount == 0 ) {
179
+ LogEntry (LOG_INFO_DESFIRE_INCOMING_DATA , Buffer , ByteCount );
179
180
return ISO14443A_APP_NO_RESPONSE ;
180
- } else if ((ByteCount >= 6 && DesfireCLA (Buffer [0 ]) && Buffer [2 ] == 0x00 &&
181
+ } else if ((ByteCount >= 8 && DesfireCLA (Buffer [0 ]) && Buffer [2 ] == 0x00 &&
181
182
Buffer [3 ] == 0x00 && (Buffer [4 ] == ByteCount - 6 || Buffer [4 ] == ByteCount - 8 )) || Iso7816CLA (DesfireCmdCLA )) {
182
183
/* Wrapped native command structure or ISO7816: */
183
184
if (Iso7816CLA (DesfireCmdCLA )) {
@@ -186,7 +187,7 @@ uint16_t MifareDesfireProcess(uint8_t *Buffer, uint16_t BitCount) {
186
187
Buffer [0 ] = (uint8_t )((iso7816ParamsStatus >> 8 ) & 0x00ff );
187
188
Buffer [1 ] = (uint8_t )(iso7816ParamsStatus & 0x00ff );
188
189
ByteCount = 2 ;
189
- return ByteCount * BITS_PER_BYTE ;
190
+ return ByteCount * BITS_PER_BYTE ;
190
191
}
191
192
}
192
193
ByteCount = Buffer [4 ];
@@ -203,7 +204,7 @@ uint16_t MifareDesfireProcess(uint8_t *Buffer, uint16_t BitCount) {
203
204
} else {
204
205
/* Re-wrap into ISO 7816-4 */
205
206
}
206
- LogEntry (LOG_INFO_DESFIRE_OUTGOING_DATA , Buffer , ByteCount );
207
+ // LogEntry(LOG_INFO_DESFIRE_OUTGOING_DATA, Buffer, ByteCount);
207
208
return ByteCount * BITS_PER_BYTE ;
208
209
} else {
209
210
/* ISO/IEC 14443-4 PDUs: No extra work */
@@ -215,41 +216,37 @@ uint16_t MifareDesfireProcess(uint8_t *Buffer, uint16_t BitCount) {
215
216
uint16_t MifareDesfireAppProcess (uint8_t * Buffer , uint16_t BitCount ) {
216
217
uint16_t ByteCount = (BitCount + BITS_PER_BYTE - 1 ) / BITS_PER_BYTE ;
217
218
uint16_t ReturnedBytes = 0 ;
218
- if (ByteCount >= 6 && DesfireCLA (Buffer [0 ]) && Buffer [2 ] == 0x00 &&
219
- Buffer [3 ] == 0x00 && (Buffer [4 ] == ByteCount - 6 || Buffer [4 ] == ByteCount - 8 )) {
219
+ if (ByteCount >= 8 && DesfireCLA (Buffer [0 ]) && Buffer [2 ] == 0x00 &&
220
+ Buffer [3 ] == 0x00 && Buffer [4 ] == ByteCount - 8 ) {
221
+ DesfireCmdCLA = Buffer [0 ];
220
222
uint16_t IncomingByteCount = (BitCount + BITS_PER_BYTE - 1 ) / BITS_PER_BYTE ;
221
223
uint16_t UnwrappedBitCount = DesfirePreprocessAPDU (ActiveCommMode , Buffer , IncomingByteCount ) * BITS_PER_BYTE ;
222
224
uint16_t ProcessedBitCount = MifareDesfireProcess (Buffer , UnwrappedBitCount );
223
225
uint16_t ProcessedByteCount = (ProcessedBitCount + BITS_PER_BYTE - 1 ) / BITS_PER_BYTE ;
224
- ProcessedBitCount = DesfirePostprocessAPDU (ActiveCommMode , Buffer , ProcessedByteCount ) * BITS_PER_BYTE ;
225
- return ProcessedBitCount ;
226
- } else if (BitCount == 4 && (Buffer [0 ] & 0xF0 ) == 0xA0 ) {
227
- // NXP-based PCD sent a "keep alive" response of ACK,
228
- // so we respond with a corresponding NAK (with CRCA bytes appended):
229
- Buffer [0 ] = 0x00 ;
230
- return 4 ;
226
+ return ISO14443AStoreLastDataFrameAndReturn (Buffer , DesfirePostprocessAPDU (ActiveCommMode , Buffer , ProcessedByteCount ) * BITS_PER_BYTE );
231
227
} else if (IsWrappedISO7816CommandType (Buffer , ByteCount )) {
228
+ DesfireCmdCLA = Buffer [2 ];
232
229
uint8_t ISO7816PrologueBytes [2 ];
233
230
memcpy (& ISO7816PrologueBytes [0 ], Buffer , 2 );
234
231
uint16_t IncomingByteCount = DesfirePreprocessAPDU (ActiveCommMode , Buffer , IncomingByteCount );
235
- memmove (& Buffer [0 ], & Buffer [2 ], IncomingByteCount - 2 );
232
+ memmove (& Buffer [0 ], & Buffer [2 ], IncomingByteCount - 2 );
236
233
uint16_t UnwrappedBitCount = (IncomingByteCount - 2 ) * BITS_PER_BYTE ;
237
- uint16_t ProcessedBitCount = MifareDesfireProcess (Buffer , UnwrappedBitCount );
234
+ uint16_t ProcessedBitCount = MifareDesfireProcess (Buffer , UnwrappedBitCount );
238
235
uint16_t ProcessedByteCount = (ProcessedBitCount + BITS_PER_BYTE - 1 ) / BITS_PER_BYTE ;
239
236
/* Append the same ISO7816 prologue bytes to the response: */
240
237
memmove (& Buffer [2 ], & Buffer [0 ], ProcessedByteCount );
241
238
memcpy (& Buffer [0 ], & ISO7816PrologueBytes [0 ], 2 );
242
239
ProcessedBitCount = DesfirePostprocessAPDU (ActiveCommMode , Buffer , ProcessedByteCount + 2 ) * BITS_PER_BYTE ;
243
- return ProcessedBitCount ;
240
+ return ISO14443AStoreLastDataFrameAndReturn ( Buffer , ProcessedBitCount ) ;
244
241
} else if ((ReturnedBytes = CallInstructionHandler (Buffer , ByteCount )) != ISO14443A_APP_NO_RESPONSE ) {
245
242
/* This case should handle non-wrappped native commands. No pre/postprocessing afterwards: */
246
- return ReturnedBytes ;
247
- } else { // if (!AnticolNoResp) {
243
+ return ISO14443AStoreLastDataFrameAndReturn ( Buffer , ReturnedBytes * BITS_PER_BYTE ) ;
244
+ } else if (!AnticolNoResp || BitCount < BITS_PER_BYTE ) {
248
245
/* This case is to exchange anticollision loop and RATS data. No need to pre/postprocess it depending
249
- * on the CommMode, which has not been set yet if we reach this point:
246
+ * on the CommMode, which has not been set yet if we reach this point:
250
247
*/
251
- uint16_t PiccProcessRespBytes = ISO144433APiccProcess (Buffer , BitCount );
252
- if (PiccProcessRespBytes == ISO14443A_APP_NO_RESPONSE ) {
248
+ uint16_t PiccProcessRespBits = ISO144433APiccProcess (Buffer , BitCount );
249
+ if (PiccProcessRespBits == ISO14443A_APP_NO_RESPONSE ) {
253
250
// Stop pesky USB readers trying to autodetect all tag types by brute-force enumeration
254
251
// from interfering with making it into the command exchange (DESFIRE_IDLE) states.
255
252
// Once the anticollision and/or RATS has completed, set this flag to keep it from
@@ -259,8 +256,9 @@ uint16_t MifareDesfireAppProcess(uint8_t *Buffer, uint16_t BitCount) {
259
256
// and it really screws things up timing-wise!
260
257
AnticolNoResp = true;
261
258
}
262
- return PiccProcessRespBytes ;
259
+ return ISO14443AStoreLastDataFrameAndReturn ( Buffer , PiccProcessRespBits ) ;
263
260
}
261
+ LogEntry (LOG_INFO_DESFIRE_OUTGOING_DATA , Buffer , ByteCount );
264
262
return ISO14443A_APP_NO_RESPONSE ;
265
263
}
266
264
0 commit comments