update FSF postal address (Jeremy Laine)
[librfid] / src / rfid_proto_mifare_classic.c
1
2 /* Mifare Classic implementation, PCD side.
3  *
4  * (C) 2005-2006 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 /* FIXME */
42 #define MIFARE_CL_READ_FWT      100
43 #define MIFARE_CL_WRITE_FWT     100
44
45 static int
46 mfcl_read(struct rfid_protocol_handle *ph, unsigned int page,
47           unsigned char *rx_data, unsigned int *rx_len)
48 {
49         unsigned char rx_buf[16];
50         unsigned int real_rx_len = sizeof(rx_buf);
51         unsigned char tx[2];
52         int ret;
53
54         if (page > MIFARE_CL_PAGE_MAX)
55                 return -EINVAL;
56
57         tx[0] = MIFARE_CL_CMD_READ;
58         tx[1] = page & 0xff;
59
60         ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
61                                      sizeof(tx), rx_buf, &real_rx_len,
62                                      MIFARE_CL_READ_FWT, 0);
63
64         if (ret < 0)
65                 return ret;
66
67         if (real_rx_len == 1 && *rx_buf == 0x04)
68                 return -EPERM;
69
70         if (real_rx_len < *rx_len)
71                 *rx_len = real_rx_len;
72
73         memcpy(rx_data, rx_buf, *rx_len);
74
75         return ret;
76 }
77
78 static int
79 mfcl_write(struct rfid_protocol_handle *ph, unsigned int page,
80            unsigned char *tx_data, unsigned int tx_len)
81 {
82         unsigned char tx[18];
83         unsigned char rx[1];
84         unsigned int rx_len = sizeof(rx);
85         int ret;
86
87         if (page > MIFARE_CL_PAGE_MAX)
88                 return -EINVAL;
89
90         if (tx_len != 16 && tx_len != 4)
91                 return -EINVAL;
92         
93         if (tx_len == 16) {
94                 tx[0] = MIFARE_CL_CMD_WRITE16;
95                 tx[1] = page & 0xff;
96
97                 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
98                                              2, rx, &rx_len, 
99                                              MIFARE_CL_WRITE_FWT, 0);
100                 if (ret < 0)
101                         return ret;
102
103                 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx_data,
104                                              tx_len, rx, &rx_len,
105                                              MIFARE_CL_WRITE_FWT, 0);
106                 if (ret < 0)
107                         return ret;
108
109                 if (rx[0] != MIFARE_UL_RESP_ACK)
110                         return -EIO;
111
112                 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
113                                              sizeof(tx), rx, &rx_len, 
114                                              MIFARE_CL_WRITE_FWT, 0);
115                 if (ret < 0)
116                         return ret;
117
118                 if (rx[0] != MIFARE_UL_RESP_ACK)
119                         return -EIO;
120
121         } else if (tx_len == 4) {
122
123                 tx[0] = MIFARE_CL_CMD_WRITE4;
124                 tx[1] = page & 0xff;
125
126                 memcpy(tx+2, tx_data, 4);
127
128                 ret = rfid_layer2_transceive(ph->l2h, RFID_MIFARE_FRAME, tx,
129                                              2+4, rx, &rx_len, 
130                                              MIFARE_CL_WRITE_FWT, 0);
131                 if (ret < 0)
132                         return ret;
133
134                 if (rx[0] != MIFARE_UL_RESP_ACK)
135                         return -EIO;
136
137         }
138
139         return ret;
140 }
141
142 static int 
143 mfcl_getopt(struct rfid_protocol_handle *ph, int optname, void *optval,
144             unsigned int *optlen)
145 {
146         int ret = -EINVAL;
147         u_int8_t atqa[2];
148         unsigned int atqa_size = sizeof(atqa);
149         unsigned int *size = optval;
150
151         switch (optname) {
152         case RFID_OPT_PROTO_SIZE:
153                 if (*optlen < sizeof(*size))
154                         return -EINVAL;
155                 *optlen = sizeof(*size);
156                 ret = 0;
157                 rfid_layer2_getopt(ph->l2h, RFID_OPT_14443A_ATQA,
158                                    atqa, &atqa_size);
159                 if (atqa[0] == 0x04 && atqa[1] == 0x00)
160                         *size = 1024;
161                 else if (atqa[0] == 0x02 && atqa[1] == 0x00)
162                         *size = 4096;
163                 else
164                         ret = -EIO;
165                 break;
166         }
167
168         return ret;
169 }
170
171 static struct rfid_protocol_handle *
172 mfcl_init(struct rfid_layer2_handle *l2h)
173 {
174         struct rfid_protocol_handle *ph;
175
176         if (l2h->l2->id != RFID_LAYER2_ISO14443A)
177                 return NULL;
178
179         if (l2h->uid_len != 4)
180                 return NULL;
181
182         ph = malloc_protocol_handle(sizeof(struct rfid_protocol_handle));
183         return ph;
184 }
185
186 static int mfcl_fini(struct rfid_protocol_handle *ph)
187 {
188         free_protocol_handle(ph);
189         return 0;
190 }
191
192 const struct rfid_protocol rfid_protocol_mfcl = {
193         .id     = RFID_PROTOCOL_MIFARE_CLASSIC,
194         .name   = "Mifare Classic",
195         .fn     = {
196                 .init           = &mfcl_init,
197                 .read           = &mfcl_read,
198                 .write          = &mfcl_write,
199                 .fini           = &mfcl_fini,
200                 .getopt         = &mfcl_getopt,
201         },
202 };
203
204 int mfcl_set_key(struct rfid_protocol_handle *ph, unsigned char *key)
205 {
206         if (!ph->l2h->rh->reader->mifare_classic.setkey)
207                 return -ENODEV;
208
209         return ph->l2h->rh->reader->mifare_classic.setkey(ph->l2h->rh, key);
210 }
211
212 int mfcl_auth(struct rfid_protocol_handle *ph, u_int8_t cmd, u_int8_t block)
213 {
214         u_int32_t serno = *((u_int32_t *)ph->l2h->uid);
215
216         if (!ph->l2h->rh->reader->mifare_classic.auth)
217                 return -ENODEV;
218
219         return ph->l2h->rh->reader->mifare_classic.auth(ph->l2h->rh, cmd,
220                                                        serno, block);
221 }