Search code examples
c#authenticationrfidmifarecontactless-smartcard

MIFARE Classic - What could cause "All-zero key" and "Unknown key" error?


I have developed a C# application to write to a MIFARE Classic card. I use default keys (FF FF FF FF FF FF) to write to the card.

After I write to the card, I notice there is no data going into the blocks of sector 1. Instead, sector 1 then looks like in the below image (when read with NXP TagInfo):

Moreover, I can no longer authenticate blocks inside sector 1.

This is happening after I write to the card with my application. Before writing, the blocks are fine (can be read).

So what could cause this problem?


Solution

  • What could cause "All-zero key" and "Unknown key" error in NXP TagInfo?

    These are not really errors. This just tells you that NXP TagInfo could not read the sector as it did not know ("Unknown key") the key necessary to authenticate to sector 1.

    What could cause the sector to become unreadable?

    The most obvious reason would be that the card you are using already contained some data that was protected with non-standard keys (neither the factory default key nor the NDEF key nor any of the other keys that NXP TagInfo tests by default. In that case, your application would not have been able to write to sector 1 of that card in the first place.

    However, since you indicate that

    This is happening after I write to the card with my application. Before writing, the blocks are fine (can be read).

    I would assume that your application writes to the last block of sector 1 (block 7). The last block of each sector is the sector trailer. This block contains the access control information for that block (authentication keys A and B as well as access bits). The block is formatted like this:

    XX XX XX XX XX XX  ZZ ZZ ZZ  GG  YY YY YY YY YY YY
    

    where XX XX XX XX XX XX (6 bytes) is key A, YY YY YY YY YY YY (6 bytes) is key B, ZZ ZZ ZZ (3 bytes) are the access conditions, and GG (1 bytes) is the general purpose byte.

    Hence, if you overwrite this block, you change the authentication keys and the access conditions for that sector. Consequently, if you wrote new authentication data (a new key A/B and new access bits) to that block, you need to use that updated information for future authentication attempts.

    However, if you wrote parts of your data to that block, you most likely set the access bits to an invalid value. In that case (the access bits contain an invalid value), the whole sector is permanently rendered unusable. As a security feature MIFARE CLassic cards will block access to sectors with invalid access conditions. Once a sector is in that state it cannot be recovered.

    Regarding the trailer block and access bits, also see these questions: