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