libnfc  1.8.0
mifare.h
Go to the documentation of this file.
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * See AUTHORS file for a more comprehensive list of contributors.
11  * Additional contributors of this file:
12  * Copyright (C) 2017-2018 Adam Laurie
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are met:
16  * 1) Redistributions of source code must retain the above copyright notice,
17  * this list of conditions and the following disclaimer.
18  * 2 )Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * Note that this license only applies on the examples, NFC library itself is under LGPL
35  *
36  */
37 
43 #ifndef _LIBNFC_MIFARE_H_
44 # define _LIBNFC_MIFARE_H_
45 
46 # include <nfc/nfc-types.h>
47 
48 // Compiler directive, set struct alignment to 1 uint8_t for compatibility
49 # pragma pack(1)
50 
51 typedef enum {
52  MC_AUTH_A = 0x60,
53  MC_AUTH_B = 0x61,
54  MC_READ = 0x30,
55  MC_WRITE = 0xA0,
56  MC_TRANSFER = 0xB0,
57  MC_DECREMENT = 0xC0,
58  MC_INCREMENT = 0xC1,
59  MC_STORE = 0xC2
60 } mifare_cmd;
61 
62 // MIFARE command params
63 struct mifare_param_auth {
64  uint8_t abtKey[6];
65  uint8_t abtAuthUid[4];
66 };
67 
68 struct mifare_param_data {
69  uint8_t abtData[16];
70 };
71 
72 struct mifare_param_value {
73  uint8_t abtValue[4];
74 };
75 
76 struct mifare_param_trailer {
77  uint8_t abtKeyA[6];
78  uint8_t abtAccessBits[4];
79  uint8_t abtKeyB[6];
80 };
81 
82 typedef union {
83  struct mifare_param_auth mpa;
84  struct mifare_param_data mpd;
85  struct mifare_param_value mpv;
86  struct mifare_param_trailer mpt;
87 } mifare_param;
88 
89 // Reset struct alignment to default
90 # pragma pack()
91 
92 bool nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp);
93 
94 // Compiler directive, set struct alignment to 1 uint8_t for compatibility
95 # pragma pack(1)
96 
97 // MIFARE Classic
98 typedef struct {
99  uint8_t abtUID[4]; // beware for 7bytes UID it goes over next fields
100  uint8_t btBCC;
101  uint8_t btSAK; // beware it's not always exactly SAK
102  uint8_t abtATQA[2];
103  uint8_t abtManufacturer[8];
104 } mifare_classic_block_manufacturer;
105 
106 typedef struct {
107  uint8_t abtData[16];
108 } mifare_classic_block_data;
109 
110 typedef struct {
111  uint8_t abtKeyA[6];
112  uint8_t abtAccessBits[4];
113  uint8_t abtKeyB[6];
114 } mifare_classic_block_trailer;
115 
116 typedef union {
117  mifare_classic_block_manufacturer mbm;
118  mifare_classic_block_data mbd;
119  mifare_classic_block_trailer mbt;
120 } mifare_classic_block;
121 
122 typedef struct {
123  mifare_classic_block amb[256];
124 } mifare_classic_tag;
125 
126 // MIFARE Ultralight
127 typedef struct {
128  uint8_t sn0[3];
129  uint8_t btBCC0;
130  uint8_t sn1[4];
131  uint8_t btBCC1;
132  uint8_t internal;
133  uint8_t lock[2];
134  uint8_t otp[4];
135 } mifareul_block_manufacturer;
136 
137 // MIFARE Ultralight EV1 MF0UL11 Config Pages
138 typedef struct {
139  uint8_t mod;
140  uint8_t rfui1[2];
141  uint8_t auth0;
142  uint8_t access;
143  uint8_t vctid;
144  uint8_t rfui2[2];
145  uint8_t pwd[4];
146  uint8_t pack[2];
147  uint8_t rfui3[2];
148 } mifareul_block_config11;
149 
150 // MIFARE Ultralight EV1 MF0UL21 ConfigA Pages
151 typedef struct {
152  uint8_t lock[3];
153  uint8_t rfui0;
154  uint8_t mod;
155  uint8_t rfui1[2];
156  uint8_t auth0;
157  uint8_t access;
158  uint8_t vctid;
159  uint8_t rfui2[2];
160  uint8_t pwd[4];
161 } mifareul_block_config21A;
162 
163 // MIFARE Ultralight EV1 MF0UL21 ConfigB Pages
164 typedef struct {
165  uint8_t pack[2];
166  uint8_t rfui3[2];
167  uint8_t dummy[12];
168 } mifareul_block_config21B;
169 
170 // MIFARE NTAG21[3/5/6] Manufacturer Pages
171 typedef struct {
172  uint8_t sn0[4];
173 } mifarentag_block_manuf21356A;
174 typedef struct {
175  uint8_t sn1[4];
176 } mifarentag_block_manuf21356B;
177 typedef struct {
178  uint8_t sn2;
179  uint8_t internal;
180  uint8_t lock[2];
181 } mifarentag_block_manuf21356C;
182 typedef struct {
183  uint8_t cc[4];
184 } mifarentag_block_manuf21356D;
185 
186 // MIFARE NTAG21[3/5/6] Config Pages
187 typedef struct {
188  uint8_t dynlock[3];
189  uint8_t rfui0;
190 } mifarentag_block_config21356A;
191 typedef struct {
192  uint8_t cfg0[4];
193 } mifarentag_block_config21356B;
194 typedef struct {
195  uint8_t cfg1[4];
196 } mifarentag_block_config21356C;
197 typedef struct {
198  uint8_t pwd[4];
199 } mifarentag_block_config21356D;
200 typedef struct {
201  uint8_t pack[2];
202  uint8_t rfui1[2];
203 } mifarentag_block_config21356E;
204 
205 typedef struct {
206  uint8_t abtData[16];
207 } mifareul_block_data;
208 
209 typedef struct {
210  uint8_t abtData[4];
211 } mifarentag_block_data;
212 
213 typedef union {
214  mifarentag_block_manuf21356A mbm21356a;
215  mifarentag_block_manuf21356B mbm21356b;
216  mifarentag_block_manuf21356C mbm21356c;
217  mifarentag_block_manuf21356D mbm21356d;
218  mifarentag_block_data mbd;
219  mifarentag_block_config21356A mbc21356a;
220  mifarentag_block_config21356B mbc21356b;
221  mifarentag_block_config21356C mbc21356c;
222  mifarentag_block_config21356D mbc21356d;
223  mifarentag_block_config21356E mbc21356e;
224 } mifarentag_block;
225 
226 typedef union {
227  mifareul_block_manufacturer mbm;
228  mifareul_block_data mbd;
229  mifareul_block_config11 mbc11;
230  mifareul_block_config21A mbc21a;
231  mifareul_block_config21B mbc21b;
232 } mifareul_block;
233 
234 // standard UL tag - 1 manuf block + 3 user blocks
235 typedef struct {
236  mifareul_block amb[4];
237 } mifareul_tag;
238 
239 // UL EV1 MF0UL11 tag - 1 manuf block + 3 user blocks + 1 config block
240 typedef struct {
241  mifareul_block amb[5];
242 } mifareul_ev1_mf0ul11_tag;
243 
244 // UL EV1 MF0UL21 tag - 1 manuf block + 8 user blocks + 1/4 lock block + 1 config block
245 // (note that tag is actually 3 bytes smaller due to 1/4 block, so don't rely on this for sizing!)
246 typedef struct {
247  mifareul_block amb[11];
248 } mifareul_ev1_mf0ul21_tag;
249 
250 // NTAG is a range of NXP tags some of which are essentially Ultralights so we can support them here
251 // if Edwin van Andel doesn't distract us...
252 // https://www.nxp.com/docs/en/data-sheet/NTAG213_215_216.pdf
253 
254 // NTAG213 EEPROM: 180 bytes, organized in 45 pages of 4 byte per page.
255 // 26 bytes reserved for manufacturer and configuration data
256 // 34 bits used for the read-only locking mechanism
257 // 4 bytes available as capability container
258 // 144 bytes user programmable read/write memory
259 typedef struct {
260  mifarentag_block amb[45];
261 } mifarentag_213_tag;
262 
263 // NTAG215 EEPROM: 540 bytes, organized in 135 pages of 4 byte per page.
264 // 26 bytes reserved for manufacturer and configuration data
265 // 28 bits used for the read-only locking mechanism
266 // 4 bytes available as capability container
267 // 504 bytes user programmable read/write memory
268 typedef struct {
269  mifarentag_block amb[135];
270 } mifarentag_215_tag;
271 
272 // NTAG216 EEPROM: 924 bytes, organized in 231 pages of 4 byte per page.
273 // 26 bytes reserved for manufacturer and configuration data
274 // 37 bits used for the read-only locking mechanism
275 // 4 bytes available as capability container
276 // 888 bytes user programmable read/write memory
277 typedef struct {
278  mifarentag_block amb[231];
279 } mifarentag_216_tag;
280 
281 // dummy max size with all structures in it for reading, rounded up to a multiple of 16 bytes
282 typedef union {
283  mifareul_block ul[58];
284  mifarentag_block nt[232];
285 } maxtag;
286 
287 // Reset struct alignment to default
288 # pragma pack()
289 
290 #endif // _LIBNFC_MIFARE_H_
bool nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp)
Execute a MIFARE Classic Command.
Definition: mifare.c:60
Define NFC types.
NFC device information.
Definition: nfc-internal.h:190