add function to set mifare key from internal eeprom
[librfid] / src / rfid_proto_mifare_classic.c
1
2 /* Mifare Classic implementation, PCD side.
3  *
4  * (C) 2005-2008 by Harald Welte <laforge@gnumonks.org>
5  *
6  */
7
8 /*
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 
11  *  as published by the Free Software Foundation
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include <librfid/rfid.h>
29 #include <librfid/rfid_protocol.h>
30 #include <librfid/rfid_layer2.h>
31 #include <librfid/rfid_protocol_mifare_classic.h>
32
33 #include <librfid/rfid_reader.h>
34
35 #include "rfid_iso14443_common.h"
36
37
38 #define MIFARE_UL_CMD_WRITE     0xA2
39 #define MIFARE_UL_CMD_READ      0x30
40
41 #define MIFARE_CL_READ_FWT      250
42 #define MIFARE_CL_WRITE_FWT     600
43
44 static int
45 mfcl_read(struct rfid_protocol_handle *ph, unsigned int page,
46           unsigned char *rx_data, unsigned int *rx_len)
47 {
48         unsigned char rx_buf[16];
49         unsigned int real_rx_len = sizeof(rx_buf);
50         unsigned char tx[2];
51         int ret;
52
53         if (page > MIFARE_CL_PAGE_MAX)
54                 return -EINVAL;
55
56         tx[0] = MIFARE_CL_CMD_READ;
57         tx[1] = page & 0xff;
58
59         ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
60                                      sizeof(tx), rx_buf, &real_rx_len,
61                                      MIFARE_CL_READ_FWT, 0);
62
63         if (ret < 0)
64                 return ret;
65
66         if (real_rx_len == 1 && *rx_buf == 0x04)
67                 return -EPERM;
68
69         if (real_rx_len < *rx_len)
70                 *rx_len = real_rx_len;
71
72         memcpy(rx_data, rx_buf, *rx_len);
73
74         return ret;
75 }
76
77 static int
78 mfcl_write(struct rfid_protocol_handle *ph, unsigned int page,
79            unsigned char *tx_data, unsigned int tx_len)
80 {
81         unsigned char tx[2];
82         unsigned char rx[1];
83         unsigned int rx_len = sizeof(rx);
84         int ret;
85
86         if (page > MIFARE_CL_PAGE_MAX)
87                 return -EINVAL;
88
89         if (tx_len != 16)
90                 return -EINVAL;
91         
92         tx[0] = MIFARE_CL_CMD_WRITE16;
93         tx[1] = page & 0xff;
94
95         ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx, 2, rx,
96                                      &rx_len, MIFARE_CL_WRITE_FWT, 0);
97         if (ret < 0)
98                 return ret;
99
100         ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx_data,
101                                      tx_len, rx, &rx_len,
102                                      MIFARE_CL_WRITE_FWT, 0);
103         if (ret < 0)
104                 return ret;
105
106         if (rx[0] != MIFARE_UL_RESP_ACK)
107                 return -EIO;
108
109         return ret;
110 }
111
112 static int 
113 mfcl_getopt(struct rfid_protocol_handle *ph, int optname, void *optval,
114             unsigned int *optlen)
115 {
116         int ret = -EINVAL;
117         u_int8_t atqa[2];
118         u_int8_t sak;
119         unsigned int atqa_size = sizeof(atqa);
120         unsigned int sak_size = sizeof(sak);
121         unsigned int *size = optval;
122
123         switch (optname) {
124         case RFID_OPT_PROTO_SIZE:
125                 if (*optlen < sizeof(*size))
126                         return -EINVAL;
127                 *optlen = sizeof(*size);
128                 ret = 0;
129                 rfid_layer2_getopt(ph->l2h, RFID_OPT_14443A_ATQA,
130                                    atqa, &atqa_size);
131                 rfid_layer2_getopt(ph->l2h, RFID_OPT_14443A_SAK,
132                                    &sak, &sak_size);
133                 if (atqa[0] == 0x04 && atqa[1] == 0x00) {
134                         if (sak == 0x09) {
135                                 /* mifare mini */
136                                 *size = 320;
137                         } else
138                                 *size = 1024;
139                 } else if (atqa[0] == 0x02 && atqa[1] == 0x00)
140                         *size = 4096;
141                 else
142                         ret = -EIO;
143                 break;
144         }
145
146         return ret;
147 }
148
149 static struct rfid_protocol_handle *
150 mfcl_init(struct rfid_layer2_handle *l2h)
151 {
152         struct rfid_protocol_handle *ph;
153
154         if (l2h->l2->id != RFID_LAYER2_ISO14443A)
155                 return NULL;
156
157         if (l2h->uid_len != 4)
158                 return NULL;
159
160         ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
161         return ph;
162 }
163
164 static int mfcl_fini(struct rfid_protocol_handle *ph)
165 {
166         free_protocol_handle(ph);
167         return 0;
168 }
169
170 const struct rfid_protocol rfid_protocol_mfcl = {
171         .id     = RFID_PROTOCOL_MIFARE_CLASSIC,
172         .name   = "Mifare Classic",
173         .fn     = {
174                 .init           = &mfcl_init,
175                 .read           = &mfcl_read,
176                 .write          = &mfcl_write,
177                 .fini           = &mfcl_fini,
178                 .getopt         = &mfcl_getopt,
179         },
180 };
181
182 int mfcl_set_key(struct rfid_protocol_handle *ph, unsigned char *key)
183 {
184         if (!ph->l2h->rh->reader->mifare_classic.setkey)
185                 return -ENODEV;
186
187         return ph->l2h->rh->reader->mifare_classic.setkey(ph->l2h->rh, key);
188 }
189
190 int mfcl_set_key_ee(struct rfid_protocol_handle *ph, unsigned int addr)
191 {
192         if (!ph->l2h->rh->reader->mifare_classic.setkey_ee)
193                 return -ENODEV;
194
195         return ph->l2h->rh->reader->mifare_classic.setkey_ee(ph->l2h->rh, addr);
196 }
197
198 int mfcl_auth(struct rfid_protocol_handle *ph, u_int8_t cmd, u_int8_t block)
199 {
200         u_int32_t serno = *((u_int32_t *)ph->l2h->uid);
201
202         if (!ph->l2h->rh->reader->mifare_classic.auth)
203                 return -ENODEV;
204
205         return ph->l2h->rh->reader->mifare_classic.auth(ph->l2h->rh, cmd,
206                                                        serno, block);
207 }
208
209 int mfcl_block2sector(u_int8_t block)
210 {
211         if (block < MIFARE_CL_SMALL_SECTORS * MIFARE_CL_BLOCKS_P_SECTOR_1k)
212                 return block/MIFARE_CL_BLOCKS_P_SECTOR_1k;
213         else
214                 return (block - MIFARE_CL_SMALL_SECTORS * MIFARE_CL_BLOCKS_P_SECTOR_1k)
215                                         / MIFARE_CL_BLOCKS_P_SECTOR_4k;
216 }
217
218 int mfcl_sector2block(u_int8_t sector)
219 {
220         if (sector < MIFARE_CL_SMALL_SECTORS)
221                 return sector * MIFARE_CL_BLOCKS_P_SECTOR_1k;
222         else if (sector < MIFARE_CL_SMALL_SECTORS + MIFARE_CL_LARGE_SECTORS)
223                 return MIFARE_CL_SMALL_SECTORS * MIFARE_CL_BLOCKS_P_SECTOR_1k + 
224                         (sector - MIFARE_CL_SMALL_SECTORS) * MIFARE_CL_BLOCKS_P_SECTOR_4k; 
225         else
226                 return -EINVAL;
227 }
228
229 int mfcl_sector_blocks(u_int8_t sector)
230 {
231         if (sector < MIFARE_CL_SMALL_SECTORS)
232                 return MIFARE_CL_BLOCKS_P_SECTOR_1k;
233         else if (sector < MIFARE_CL_SMALL_SECTORS + MIFARE_CL_LARGE_SECTORS)
234                 return MIFARE_CL_BLOCKS_P_SECTOR_4k;
235         else
236                 return -EINVAL;
237 }