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