PN532-RFID-NFC-Carte-et-tag-MiFare

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Cartes & tags MiFare

MiFare est l'un des 4 "protocoles" des cartes 13.56MHz (FeliCa en est un autre bien connu). Toutes les cartes et tags vendu sur le wheshop de MCHobby utilise le populaire et économique chipset MiFare Classic.

Cartes MiFare Classic

Les cartes MIFARE Classic sont disponibles sous les formats 1K et 4K. Bien qu'il existe une variété plus importante de puce MiFare, ce tutoriel va se pencher sur deux chipsets principaux disposant de documents publiquement accessibles:

Les cartes Mifare Classic ont typiquement 4-octets NUID qui identifie de manière unique chaque carte (dans la limites des valeurs numériques codables sur 4 octets). Il est également possible d'avoir des modèles avec une identification sur 7 octets mais les modèles 4 octets sont, de loin, les carte Mifare Classic les plus répandues.

Mémoire EEPROM

Mifare Classic cards have either 1K or 4K of EEPROM memory. Each memory block can be configured with different access conditions, with two seperate authentication keys present in each block.

Mifare Classic cards are divided into section called sectors and blocks. Each "sector" has individual access rights, and contains a fixed number of "blocks" that are controlled by these access rights. Each block contains 16 bytes, and sectors contains either 4 blocks (1K/4K cards) for a total of 64 bytes per sector, or 16 blocks (4K cards only) for a total of 256 bytes per sector. The card types are organised as follows:

  • cartes de 1K - 16 sectors of 4 blocks each (sectors 0..15)
  • Cartes de 4K - 32 sectors of 4 blocks each (sectors 0..31) and 8 sectors of 16 blocks each (sectors 32..39)

Secteurs de 4 blocs

1K and 4K cards both use 16 sectors of 4 blocks each, with the bottom 1K of memory on the 4K cards being organised identically to the 1K models for compatability reasons. These individual 4 block sectors (containing 64 byts each) have basic security features are can each be configured with seperate read/write access and two different 6-byte authentication keys (the keys can be different for each sector). Due to these security features (which are stored in the last block, called the Sector Trailer), only the bottom 3 blocks of each sector are actually available for data storage, meaning you have 48 bytes per 64 byte sector available for your own use.

Each 4 block sector is organised as follows, with four rows of 16 bytes each for a total of 64-bytes per sector. The first two sectors of any card are shown:

    Sector  Block   Bytes                                                           Description
    ------  -----   -----                                                           -----------
                      0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
      1       3       [-------KEY A-------]   [Access Bits]   [-------KEY B-------]   Sector Trailer
              2       [                            Data                           ]   Data
              1       [                            Data                           ]   Data
              0       [                            Data                           ]   Data
      0       3       [-------KEY A-------]   [Access Bits]   [-------KEY B-------]   Sector Trailer
              2       [                            Data                           ]   Data
              1       [                            Data                           ]   Data
              0       [                     Manufacturer Data                     ]   Manufacturer Block

Secteur de fin (bloc 3)

Le secteur de fin s'appelle "sector trailer" en anglais. Ce secteur contient deux clés secrètes Clé A et Clé B (Key A et Key B en anglais), ainsi que les conditions d'accès pour les 4 blocs.

Ce secteur à la structure suivante:

          Sector Trailer Bytes
          --------------------------------------------------------------
          0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
          [       Key A       ]   [Access Bits]   [       Key B        ]

For more information in using Keys to access the clock contents, see Accessing Data Blocks further below.

Blocs de données (Blocs 0..2)

Data blocks are 16 bytes wide and, depending on the permissions set in the access bits, can be read from and written to. You are free to use the 16 data bytes in any way you wish. You can easily store text input, store four 32-bit integer values, a 16 character uri, etc.

Blocs de données comme "bloc valeurs"

An alternative to storing random data in the 16 byte-wide blocks is to configure them as "bloc valeur" ("Value Blocks" en anglais). Value blocks allow performing electronic purse functions (valid commands are: read, write, increment, decrement, restore, transfer).

Each Value block contains a single signed 32-bit value, and this value is stored 3 times for data integrity and security reasons. It is stored twice non-inverted, and once inverted. The last 4 bytes are used for a 1-byte address, which is stored 4 times (twice non-inverted, and twice inverted).

Data blocks configured as "Value Blocks" have the following structure:

          Value Block Bytes
          --------------------------------------------------------------
          0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
          [   Value   ]   [   ~Value  ]   [   Value   ]   [A  ~A  A   ~A]

Bloc Fabricant (Secteur 0, Bloc 0)

Sector 0 is special since it contains the bloc Fabricant ("Manufacturer Block" en anglais). This block contains the manufacturer data, and is read-only. It should be avoided unless you know what you are doing.

Secteurs de 16 blocs

16 block sectors are identical to 4 block sectors, but with more data blocks. The same structure described in the 4 block sectors above applies.

      Sector  Block   Bytes                                                           Description
      ------  -----   -----                                                           ----------
                      0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
      32      15      [-------KEY A-------]   [Access Bits]   [-------KEY B-------]   Sector Trailer 32
              14      [                            Data                           ]   Data
              13      [                            Data                           ]   Data
              ...
              2       [                            Data                           ]   Data
              1       [                            Data                           ]   Data
              0       [                            Data                           ]   Data

Accéder à la mémoire de l'EEPROM

To access the EEPROM on the cards, you need to perform the following steps:

  1. You must retrieve the 4-byte NUID of the card (this can sometimes be 7-bytes long as well, though rarely for Mifare Classic cards). This is required for the subsequent authentication process.
  2. You must authenticate the sector you wish to access according to the access rules defined in the Sector Trailer block for that sector, by passing in the appropriate 6 byte Authentication Key (ex. 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF for new cards).
  3. Once authenication has succeeded, and depending on the sector permissions, you can then read/write/increment/decrement the contents of the specific block. Note that you need to re-authenticate for each sector that you access, since each sector can have it's own distinct access keys and rights!

Note sur l'Authentification

Before you can do access the sector's memory, you first need to "authenticate" according to the security settings stored in the Sector Trailer. By default, any new card will generally be configured to allow full access to every block in the sector using Key A and a value of 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF. Some other common keys that you may wish to try if this doesn't work are:

              0XFF 0XFF 0XFF 0XFF 0XFF 0XFF
              0XD3 0XF7 0XD3 0XF7 0XD3 0XF7
              0XA0 0XA1 0XA2 0XA3 0XA4 0XA5
              0XB0 0XB1 0XB2 0XB3 0XB4 0XB5
              0X4D 0X3A 0X99 0XC3 0X51 0XDD
              0X1A 0X98 0X2C 0X7E 0X45 0X9A
              0XAA 0XBB 0XCC 0XDD 0XEE 0XFF
              0X00 0X00 0X00 0X00 0X00 0X00
              0XAB 0XCD 0XEF 0X12 0X34 0X56

Carte MiFare Classic 1K - Exemple

Voici un exemple avec un carte MiFare Classic toute neuve.

Le dump de la mémoire illustre la structure d'une carte MiFace Classic de 1K où vous pouvez clairement voir les données et secteurs de fin (trailer):

    [--------------------------Début du dump mémoire-------------------------]
    -----------------------Secteur 0-------------------------
    Bloc 0 : 8E 02 6F 66 85 08 04 00 62 63 64 65 66 67 68 69  ?.of?...bcdefghi
    Bloc 1 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 2 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 3 : 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 1-------------------------
    Bloc 4 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 5 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 6 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 7 : 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 2-------------------------
    Bloc 8 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 9 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 11: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 3-------------------------
    Bloc 12: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 13: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 14: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 15: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 4-------------------------
    Bloc 16: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 17: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 18: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 19: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 5-------------------------
    Bloc 20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 21: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 22: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 23: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 6-------------------------
    Bloc 24: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 25: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 26: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 27: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 7-------------------------
    Bloc 28: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 29: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 31: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 8-------------------------
    Bloc 32: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 33: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 34: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 35: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 9-------------------------
    Bloc 36: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 37: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 38: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 39: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 10-------------------------
    Bloc 40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 41: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 42: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 43: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 11-------------------------
    Bloc 44: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 45: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 46: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 47: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 12-------------------------
    Bloc 48: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 49: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 51: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 13-------------------------
    Bloc 52: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 53: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 54: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 55: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 14-------------------------
    Bloc 56: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 57: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 58: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 59: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    -----------------------Secteur 15-------------------------
    Bloc 60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 61: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 62: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    Bloc 63: 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF  ......ÿ.?iÿÿÿÿÿÿ
    [---------------------------Fin du dump mémoire--------------------------]

Cartes MiFare Ultralight

MiFare Ultralight cards typically contain 512 bits (64 bytes) of memory, including 4 bytes (32-bits) of OTP (One Time Programmable) memory where the individual bits can be written but not erased.

MiFare Ultralight cards have a 7-byte UID that uniquely identifies the card.

Mémoire EEPROM

MiFare Ultralight cards have 512 bits (64 bytes) of EEPROM memory, including 4 byte (32 bits) of OTP memory. Unlike Mifare Classic cards, there is no authentication on a per block level, although the blocks can be set to "read-only" mode using Lock Bytes (described below).

EEPROM memory is organised into 16 pages of four bytes eachs, in the following order:

      Page   Description
      ----   ------------
      0      Numéro de série (4 bytes/octets)
      1      Numéro de série (4 bytes/octets)
      2      Byte 0:    Numéro de série
             Byte 1:    Mémoire Interne
             Byte 2..3: Octets de blocage/lock bytes
      3      Mémoire programmable un seule fois/OTP memory (4 octets/bytes)
      4..15  Mémoire utilisateur (4 octets/bytes)

Here are the pages and blocks arranged in table format:

      Page   Bloc 0     Bloc 1     Bloc 2    Bloc 3 
      -----  ---------------------------------------
      0      [          Numéro de Série            ]
      1      [          Numéro de Série            ]
      2      [Serial] - [Intern] - [   Lock Bytes  ]   # N° de série - mémoire interne - octets de blocage
      3      [ Mémoire programmable une seule fois ]   # dite 'Mémoire OTP'
      4      [          Donnée utilisateur         ]   # dite 'User Data'
      5      [          Donnée utilisateur         ]
      6      [          Donnée utilisateur         ]
      7      [          Donnée utilisateur         ]
      8      [          Donnée utilisateur         ]
      9      [          Donnée utilisateur         ]
      10     [          Donnée utilisateur         ]
      11     [          Donnée utilisateur         ]
      12     [          Donnée utilisateur         ]
      13     [          Donnée utilisateur         ]
      14     [          Donnée utilisateur         ]
      15     [          Donnée utilisateur         ]

Lock Bytes (page 2)

Lock Bytes sont les octets de blocage. Pour rappel, un Byte (anglais) est un octet (Français) et contient 8 bits.

Bytes 2 and 3 of page 2 are referred to as "Lock Bytes". Each page from 0x03 and higher can individually locked by setting the corresponding locking bit to "1" to prevent further write access, effectively making the memory read only.

For more information on the lock byte mechanism, refer to section 8.5.2 of the datasheet (referenced above).

OTP Bytes (Page 3)

OTP Bytes signifie "One Time Programming Bytes" et représente des octets qui ne peuvent être écrit qu'une seule fois.

Page 3 is the OTP memory, and by default all bits on this page are set to 0. These bits can be bitwise modified using the MiFare WRITE command, and individual bits can be set to 1, but can not be changed back to 0.

Pages de données (Page 4-15)

Pages 4 to 15 are can be freely read from and written to, provided there is no conflict with the Lock Bytes described above.

After production, the bytes have the following default values:

      Page    Valeur des Bytes/Octets
      ----    ----------------------
              0     1     2     3
      4       0xFF  0xFF  0xFF  0xFF
      5..15   0x00  0x00  0x00  0x00

Accès aux blocs de données

In order to access the cards, you must following two steps:

  1. 'Connect' to a Mifare Ultralight card and retrieve the 7 byte UID of the card.
  2. Memory can be read and written directly once a passive mode connection has been made. No authentication is required for Mifare Ultralight cards.

Longueur Lecture/Ecriture

For compatability reasons, "Read" requests to a Mifare Ultralight card will retrieve 16 bytes (4 pages) at a time (which corresponds to block size of a Mifare Classic card). For example, if you specify that you want to read page 3, in reality pages 3, 4, 5 and 6 will be read and returned, and you can simply discard the last 12 bytes if they aren't needed. If you select a higher page, the 16 byte read will wrap over to page 0. For example, reading page 14 will actually return page 14, 15, 0 and 1.

"Write" requests occur in pages (4 bytes), so there is no problem with overwriting data on subsequent pages.


Source: PN532 RFID/NFC Breakout and Shield créé par LadyAda pour AdaFruit Industries. Crédit [www.adafruit.com AdaFruit Industries]

Traduit par Meurisse D. pour MCHobby.be

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.