libnfc  1.7.1
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  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  * 1) Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2 )Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Note that this license only applies on the examples, NFC library itself is under LGPL
34  *
35  */
36 
42 #ifndef _LIBNFC_MIFARE_H_
43 # define _LIBNFC_MIFARE_H_
44 
45 # include <nfc/nfc-types.h>
46 
47 // Compiler directive, set struct alignment to 1 uint8_t for compatibility
48 # pragma pack(1)
49 
50 typedef enum {
51  MC_AUTH_A = 0x60,
52  MC_AUTH_B = 0x61,
53  MC_READ = 0x30,
54  MC_WRITE = 0xA0,
55  MC_TRANSFER = 0xB0,
56  MC_DECREMENT = 0xC0,
57  MC_INCREMENT = 0xC1,
58  MC_STORE = 0xC2
59 } mifare_cmd;
60 
61 // MIFARE command params
62 struct mifare_param_auth {
63  uint8_t abtKey[6];
64  uint8_t abtAuthUid[4];
65 };
66 
67 struct mifare_param_data {
68  uint8_t abtData[16];
69 };
70 
71 struct mifare_param_value {
72  uint8_t abtValue[4];
73 };
74 
75 typedef union {
76  struct mifare_param_auth mpa;
77  struct mifare_param_data mpd;
78  struct mifare_param_value mpv;
79 } mifare_param;
80 
81 // Reset struct alignment to default
82 # pragma pack()
83 
84 bool nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp);
85 
86 // Compiler directive, set struct alignment to 1 uint8_t for compatibility
87 # pragma pack(1)
88 
89 // MIFARE Classic
90 typedef struct {
91  uint8_t abtUID[4]; // beware for 7bytes UID it goes over next fields
92  uint8_t btBCC;
93  uint8_t btSAK; // beware it's not always exactly SAK
94  uint8_t abtATQA[2];
95  uint8_t abtManufacturer[8];
96 } mifare_classic_block_manufacturer;
97 
98 typedef struct {
99  uint8_t abtData[16];
100 } mifare_classic_block_data;
101 
102 typedef struct {
103  uint8_t abtKeyA[6];
104  uint8_t abtAccessBits[4];
105  uint8_t abtKeyB[6];
106 } mifare_classic_block_trailer;
107 
108 typedef union {
109  mifare_classic_block_manufacturer mbm;
110  mifare_classic_block_data mbd;
111  mifare_classic_block_trailer mbt;
112 } mifare_classic_block;
113 
114 typedef struct {
115  mifare_classic_block amb[256];
116 } mifare_classic_tag;
117 
118 // MIFARE Ultralight
119 typedef struct {
120  uint8_t sn0[3];
121  uint8_t btBCC0;
122  uint8_t sn1[4];
123  uint8_t btBCC1;
124  uint8_t internal;
125  uint8_t lock[2];
126  uint8_t otp[4];
127 } mifareul_block_manufacturer;
128 
129 typedef struct {
130  uint8_t abtData[16];
131 } mifareul_block_data;
132 
133 typedef union {
134  mifareul_block_manufacturer mbm;
135  mifareul_block_data mbd;
136 } mifareul_block;
137 
138 typedef struct {
139  mifareul_block amb[4];
140 } mifareul_tag;
141 
142 // Reset struct alignment to default
143 # pragma pack()
144 
145 #endif // _LIBNFC_MIFARE_H_
nfc_device
NFC device information.
Definition: nfc-internal.h:190
nfc_initiator_mifare_cmd
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
nfc-types.h
Define NFC types.