Permalink
Browse files

Fixed keys with NDEF records (added READ_AS_NDEF to memdump)

  • Loading branch information...
1 parent 3fc9641 commit c519bc64327304c66bfad6d8c19c2101101f587c K. Townsend committed Oct 23, 2012
@@ -81,6 +81,11 @@ void loop(void) {
uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Serial.println("");
+ Serial.println("PLEASE NOTE: Formatting your card for NDEF records will change the");
+ Serial.println("authentication keys and you will no longer be able to read the");
+ Serial.println("card as a normal Mifare card without resetting all keys. Try to keep");
+ Serial.println("seperate cards for NDEF and non-NDEF purposes.");
+ Serial.println("");
Serial.println("Place your Mifare Classic card on the reader to format with NDEF");
Serial.println("and press any key to continue ...");
// Wait for user input before proceeding
@@ -29,6 +29,36 @@
#define IRQ (2)
#define RESET (3) // Not connected by default on the NFC Shield
+// ***********************
+// TODO: !!! IMPORTANT !!!
+// ***********************
+// Set this field depending on if this is an NDEF formatted card.
+// For a non-NDEF card (for example an unformatted Mifare Classic card),
+// set this to 0.
+//
+// NDEF formatted cards use specific keys for authentication, and the
+// authentication requests will fail if you use the default Mifare keys
+// (0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for both key a and key B).
+//
+// Note that you will no longer be able to authenticate non-NDEF blocks
+// when using NDEF keys since they will not match what is being provided.
+// Also, because key a changes in block 0 (the MAD Sector for NDEF
+// records), NDEF formatted cards will no longer work in normal
+// Mifare mode. Cards should be treated as NDEF or not, and it's best
+// not to mix them because key managed becomes complicated.
+//
+// Possible values:
+//
+// READ_AS_NDEF = 0 No NDEF Sectors ... use default Mifare Classic keys
+// READ_AS_NDEF = 1 NDEF Formatted Card ... use NDEF keys (only NDEF blocks will authenticate!)
+//
+// For more information see:
+//
+// http://www.ladyada.net/products/rfidnfc/mifare.html
+// http://www.ladyada.net/products/rfidnfc/ndef.html
+
+#define READ_AS_NDEF (0)
+
Adafruit_NFCShield_I2C nfc(IRQ, RESET);
void setup(void) {
@@ -63,11 +93,16 @@ void loop(void) {
bool authenticated = false; // Flag to indicate if the sector is authenticated
uint8_t data[16]; // Array to store block data during reads
- // Use the default KEYA: FF FF FF FF FF FF
- uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
- uint8_t keyb[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
- // uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
- // uint8_t keyb[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 };
+ // Use the appropriate keys depending on whether this is an NDEF card or not
+ #if READ_AS_NDEF == 1
+ // Use the NDEF keys and read NDEF blocks
+ uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
+ uint8_t keyb[6] = { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 };
+ #else
+ // Use the default (non-NDEF) Mifare card keys
+ uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
+ uint8_t keyb[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
+ #endif
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
@@ -87,7 +122,7 @@ void loop(void) {
// We probably have a Mifare Classic card ...
Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
- // Now we try to go through all 16 sector (each having 4 blocks)
+ // Now we try to go through all 16 sectors (each having 4 blocks)
// authenticating each sector, and then dumping the blocks
for (currentblock = 0; currentblock < 64; currentblock++)
{
@@ -101,10 +136,14 @@ void loop(void) {
Serial.print("------------------------Sector ");Serial.print(currentblock/4, DEC);Serial.println("-------------------------");
if (currentblock == 0)
{
+ // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)
+ // or 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 for NDEF formatted cards
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 0, keya);
}
else
{
+ // This will be 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for Mifare Classic (non-NDEF!)
+ // or 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 for NDEF formatted cards
success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, currentblock, 0, keyb);
}
if (success)
@@ -161,4 +200,3 @@ void loop(void) {
while (!Serial.available());
Serial.flush();
}
-

0 comments on commit c519bc6

Please sign in to comment.