From 76adf6e3831b19292e68ad30dd7363a4e063e38c Mon Sep 17 00:00:00 2001 From: Alexander Inyukhin Date: Sat, 10 Sep 2016 11:01:34 +0300 Subject: [PATCH] Authenticate devices with long uids It seems that the last four uid bytes should be used for auth instead of first ones. This patch allows to work with Mifare plus cards in SL1 mode, that have 7 byte uids (e.g. Troyka transport cards.) See also "3.2.5 MIFARE Classic Authentication" of AN10927 document. http://cache.nxp.com/documents/application_note/AN10927.pdf --- MFRC522.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/MFRC522.cpp b/MFRC522.cpp index 6a4fe2d..100bc7e 100644 --- a/MFRC522.cpp +++ b/MFRC522.cpp @@ -843,8 +843,12 @@ MFRC522::StatusCode MFRC522::PCD_Authenticate(byte command, ///< PICC_CMD_MF_AU for (byte i = 0; i < MF_KEY_SIZE; i++) { // 6 key bytes sendData[2+i] = key->keyByte[i]; } - for (byte i = 0; i < 4; i++) { // The first 4 bytes of the UID - sendData[8+i] = uid->uidByte[i]; + // Use the last uid bytes as specified in http://cache.nxp.com/documents/application_note/AN10927.pdf + // section 3.2.5 "MIFARE Classic Authentication". + // The only missed case is the MF1Sxxxx shortcut activation, + // but it requires cascade tag (CT) byte, that is not part of uid. + for (byte i = 0; i < 4; i++) { // The last 4 bytes of the UID + sendData[8+i] = uid->uidByte[i+uid->size-4]; } // Start the authentication.