2 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 rfid_hexdump(const void *data, unsigned int len)
31 static char string[1024];
32 unsigned char *d = (unsigned char *) data;
36 left = sizeof(string);
37 for (i = 0; len--; i += 3) {
38 if (i >= sizeof(string) -4)
40 snprintf(string+i, 4, " %02x", *d++);
45 struct pegoda_handle {
46 struct usb_dev_handle *handle;
52 struct usb_device *find_device(u_int16_t vendor, u_int16_t device)
56 for (bus = usb_get_busses(); bus; bus = bus->next) {
57 struct usb_device *dev;
58 for (dev = bus->devices; dev; dev = dev->next) {
59 if (dev->descriptor.idVendor == vendor &&
60 dev->descriptor.idProduct == device) {
68 int pegoda_transcieve(struct pegoda_handle *ph,
69 u_int8_t cmd, unsigned char *tx, unsigned int tx_len,
70 unsigned char *rx, unsigned int *rx_len)
72 unsigned char txbuf[256];
73 unsigned char rxbuf[256];
75 unsigned int len_expected;
76 struct pegoda_cmd_hdr *hdr = (struct pegoda_cmd_hdr *)txbuf;
78 hdr->seq = ++(ph->seq);
80 hdr->len = htons(tx_len);
81 memcpy(txbuf + sizeof(*hdr), tx, tx_len);
83 printf("tx [%u]: %s\n", tx_len+sizeof(*hdr),
84 rfid_hexdump(txbuf, tx_len + sizeof(*hdr)));
85 rc = usb_bulk_write(ph->handle, 0x02, (char *)txbuf,
86 tx_len + sizeof(*hdr), 0);
90 rc = usb_bulk_read(ph->handle, 0x81, (char *)rxbuf, sizeof(rxbuf), 0);
95 fprintf(stderr, "unexpected: received %u bytes as length?\n");
98 printf("len [%u]: %s\n", rc, rfid_hexdump(rxbuf, rc));
100 len_expected = rxbuf[0];
102 if (len_expected > sizeof(rxbuf))
105 rc = usb_bulk_read(ph->handle, 0x81, (char *)rxbuf, len_expected, 0);
108 printf("rx [%u]: %s\n", rc, rfid_hexdump(rxbuf, rc));
110 memcpy(rx, rxbuf+1, rc-1);
116 struct pegoda_handle *pegoda_open(void)
118 struct usb_device *pegoda;
119 unsigned char rbuf[16];
120 unsigned int rlen = sizeof(rbuf);
121 struct pegoda_handle *ph;
127 pegoda = find_device(USB_VENDOR_PHILIPS, USB_DEVICE_PEGODA);
132 ph = malloc(sizeof(*ph));
135 memset(ph, 0, sizeof(*ph));
137 printf("found pegoda, %u configurations\n",
138 pegoda->descriptor.bNumConfigurations);
140 printf("config 2 [nr %u] has %u interfaces\n",
141 pegoda->config[1].bConfigurationValue,
142 pegoda->config[1].bNumInterfaces);
144 printf("config 2 interface 0 has %u altsettings\n",
145 pegoda->config[1].interface[0].num_altsetting);
147 ph->handle = usb_open(pegoda);
151 if (usb_set_configuration(ph->handle, 2))
154 printf("configuration 2 successfully set\n");
156 if (usb_claim_interface(ph->handle, 0))
159 printf("interface 0 claimed\n");
161 if (usb_set_altinterface(ph->handle, 1))
164 printf("alt setting 1 selected\n");
166 pegoda_transcieve(ph, PEGODA_CMD_PCD_CONFIG, NULL, 0, rbuf, &rlen);
174 /* Transform crypto1 key from generic 6byte into rc632 specific 12byte */
176 mifare_transform_key(const u_int8_t *key6, u_int8_t *key12)
182 for (i = 0; i < 6; i++) {
185 key12[i * 2 + 1] = (~ln << 4) | ln;
186 key12[i * 2] = (~hn << 4) | hn;
191 static int pegoda_auth_e2(struct pegoda_handle *ph,
192 u_int8_t keynr, u_int8_t sector)
194 unsigned char buf[3];
195 unsigned char rbuf[16];
196 unsigned int rlen = sizeof(rbuf);
199 buf[1] = keynr; /* key number */
200 buf[2] = sector; /* sector */
202 pegoda_transcieve(ph, PEGODA_CMD_PICC_AUTH, buf, 3, rbuf, &rlen);
204 /* FIXME: check response */
209 static int pegoda_auth_key(struct pegoda_handle *ph,
210 u_int8_t sector, const unsigned char *key6)
212 unsigned char buf[1+4+12+1];
213 unsigned char rbuf[16];
214 unsigned int rlen = sizeof(rbuf);
217 memcpy(buf+1, ph->snr, 4);
218 mifare_transform_key(key6, buf+5);
221 pegoda_transcieve(ph, PEGODA_CMD_PICC_AUTH_KEY, buf, 18, rbuf, &rlen);
223 /* FIXME: check response */
228 static int pegoda_read16(struct pegoda_handle *ph,
229 u_int8_t page, unsigned char *rx)
231 unsigned int rlen = 24;
233 return pegoda_transcieve(ph, PEGODA_CMD_PICC_READ,
234 &page, 1, rx, &rlen);
237 int main(int argc, char **argv)
239 unsigned char buf[256];
240 unsigned char rbuf[256];
241 unsigned int rlen = sizeof(rbuf);
242 struct pegoda_handle *ph;
251 pegoda_transcieve(ph, PEGODA_CMD_SWITCH_LED, buf, 1, rbuf, &rlen);
257 pegoda_transcieve(ph, PEGODA_CMD_PICC_COMMON_REQUEST,
258 buf, 1, rbuf, &rlen);
263 pegoda_transcieve(ph, PEGODA_CMD_PICC_CASC_ANTICOLL,
264 buf, 6, rbuf, &rlen);
266 memcpy(ph->snr, rbuf+3, 4);
269 memcpy(buf+1, ph->snr, 4);
271 pegoda_transcieve(ph, PEGODA_CMD_PICC_CASC_SELECT,
272 buf, 5, rbuf, &rlen);
274 pegoda_auth_key(ph, 0, "\xff\xff\xff\xff\xff\xff");
275 pegoda_read16(ph, 0, rbuf);