]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/iclass.c
fix 'hf iclass snoop'
[proxmark3-svn] / armsrc / iclass.c
CommitLineData
cee5a30d 1//-----------------------------------------------------------------------------
2// Gerhard de Koning Gans - May 2008
3// Hagen Fritsch - June 2010
4// Gerhard de Koning Gans - May 2011
1e262141 5// Gerhard de Koning Gans - June 2012 - Added iClass card and reader emulation
cee5a30d 6//
7// This code is licensed to you under the terms of the GNU GPL, version 2 or,
8// at your option, any later version. See the LICENSE.txt file for the text of
9// the license.
10//-----------------------------------------------------------------------------
11// Routines to support iClass.
12//-----------------------------------------------------------------------------
13// Based on ISO14443a implementation. Still in experimental phase.
14// Contribution made during a security research at Radboud University Nijmegen
17505ce2 15//
cee5a30d 16// Please feel free to contribute and extend iClass support!!
17//-----------------------------------------------------------------------------
18//
cee5a30d 19// FIX:
20// ====
21// We still have sometimes a demodulation error when snooping iClass communication.
22// The resulting trace of a read-block-03 command may look something like this:
23//
17505ce2 24// + 22279: : 0c 03 e8 01
cee5a30d 25//
26// ...with an incorrect answer...
27//
28// + 85: 0: TAG ff! ff! ff! ff! ff! ff! ff! ff! bb 33 bb 00 01! 0e! 04! bb !crc
29//
30// We still left the error signalling bytes in the traces like 0xbb
31//
32// A correct trace should look like this:
33//
17505ce2 34// + 21112: : 0c 03 e8 01
35// + 85: 0: TAG ff ff ff ff ff ff ff ff ea f5
cee5a30d 36//
37//-----------------------------------------------------------------------------
38
17505ce2 39#include "iclass.h"
40
cee5a30d 41#include "proxmark3.h"
42#include "apps.h"
43#include "util.h"
44#include "string.h"
3d2c9c9b 45#include "printf.h"
7e67e42f 46#include "common.h"
fecd8202 47#include "cmd.h"
6e49717b 48#include "iso14443a.h"
3d2c9c9b 49#include "iso15693.h"
1e262141 50// Needed for CRC in emulation mode;
51// same construction as in ISO 14443;
52// different initial value (CRC_ICLASS)
53#include "iso14443crc.h"
c3963755 54#include "iso15693tools.h"
b67f7ec3 55#include "protocols.h"
10a8875c 56#include "optimized_cipher.h"
979c7655 57#include "usb_cdc.h" // for usb_poll_validate_length
fc52fbd4 58#include "fpgaloader.h"
10a8875c 59
8efd0b80 60// iCLASS has a slightly different timing compared to ISO15693. According to the picopass data sheet the tag response is expected 330us after
61// the reader command. This is measured from end of reader EOF to first modulation of the tag's SOF which starts with a 56,64us unmodulated period.
62// 330us = 140 ssp_clk cycles @ 423,75kHz when simulating.
63// 56,64us = 24 ssp_clk_cycles
c41dd5f9 64#define DELAY_ICLASS_VCD_TO_VICC_SIM (140 - 24)
65// times in ssp_clk_cycles @ 3,3625MHz when acting as reader
ece38ef3 66#define DELAY_ICLASS_VICC_TO_VCD_READER DELAY_ISO15693_VICC_TO_VCD_READER
c41dd5f9 67// times in samples @ 212kHz when acting as reader
ece38ef3 68#define ICLASS_READER_TIMEOUT_ACTALL 330 // 1558us, nominal 330us + 7slots*160us = 1450us
496bb4be 69#define ICLASS_READER_TIMEOUT_UPDATE 3390 // 16000us, nominal 4-15ms
c41dd5f9 70#define ICLASS_READER_TIMEOUT_OTHERS 80 // 380us, nominal 330us
71
be09ea86 72#define ICLASS_BUFFER_SIZE 34 // we expect max 34 bytes as tag answer (response to READ4)
8efd0b80 73
cee5a30d 74
75//=============================================================================
be09ea86 76// A `sniffer' for iClass communication
cee5a30d 77// Both sides of communication!
78//=============================================================================
be09ea86 79void SnoopIClass(uint8_t jam_search_len, uint8_t *jam_search_string) {
80 SnoopIso15693(jam_search_len, jam_search_string);
1e262141 81}
82
be09ea86 83
912a3e94 84void rotateCSN(uint8_t* originalCSN, uint8_t* rotatedCSN) {
17505ce2 85 int i;
86 for (i = 0; i < 8; i++) {
912a3e94 87 rotatedCSN[i] = (originalCSN[i] >> 3) | (originalCSN[(i+1)%8] << 5);
1e262141 88 }
89}
90
be09ea86 91
3d2c9c9b 92// Encode SOF only
17505ce2 93static void CodeIClassTagSOF() {
81012e67 94 ToSendReset();
645c960f 95 ToSend[++ToSendMax] = 0x1D;
1e262141 96 ToSendMax++;
97}
1e262141 98
be09ea86 99
17505ce2 100static void AppendCrc(uint8_t *data, int len) {
101 ComputeCrc14443(CRC_ICLASS, data, len, data+len, data+len+1);
102}
81cd0474 103
b67f7ec3 104
ff7bb4ef
MHS
105/**
106 * @brief Does the actual simulation
ff7bb4ef 107 */
17505ce2 108int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) {
0ab9002f 109
b67f7ec3
MHS
110 // free eventually allocated BigBuf memory
111 BigBuf_free_keep_EM();
55eaed8f 112
ae60ceca 113 uint16_t page_size = 32 * 8;
114 uint8_t current_page = 0;
0ab9002f 115
ae60ceca 116 // maintain cipher states for both credit and debit key for each page
117 State cipher_state_KC[8];
118 State cipher_state_KD[8];
119 State *cipher_state = &cipher_state_KD[0];
8efd0b80 120
0ab9002f 121 uint8_t *emulator = BigBuf_get_EM_addr();
122 uint8_t *csn = emulator;
0ab9002f 123
1e262141 124 // CSN followed by two CRC bytes
ae60ceca 125 uint8_t anticoll_data[10];
126 uint8_t csn_data[10];
17505ce2 127 memcpy(csn_data, csn, sizeof(csn_data));
128 Dbprintf("Simulating CSN %02x%02x%02x%02x%02x%02x%02x%02x", csn[0], csn[1], csn[2], csn[3], csn[4], csn[5], csn[6], csn[7]);
1e262141 129
1e262141 130 // Construct anticollision-CSN
17505ce2 131 rotateCSN(csn_data, anticoll_data);
1e262141 132
133 // Compute CRC on both CSNs
0ab9002f 134 AppendCrc(anticoll_data, 8);
135 AppendCrc(csn_data, 8);
b67f7ec3 136
8efd0b80 137 uint8_t diversified_key_d[8] = { 0x00 };
138 uint8_t diversified_key_c[8] = { 0x00 };
ae60ceca 139 uint8_t *diversified_key = diversified_key_d;
8efd0b80 140
ae60ceca 141 // configuration block
142 uint8_t conf_block[10] = {0x12, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0xFF, 0x3C, 0x00, 0x00};
ae60ceca 143
b67f7ec3 144 // e-Purse
0ab9002f 145 uint8_t card_challenge_data[8] = { 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
ae60ceca 146
0ab9002f 147 if (simulationMode == ICLASS_SIM_MODE_FULL) {
ae60ceca 148 // initialize from page 0
149 memcpy(conf_block, emulator + 8 * 1, 8);
150 memcpy(card_challenge_data, emulator + 8 * 2, 8); // e-purse
151 memcpy(diversified_key_d, emulator + 8 * 3, 8); // Kd
152 memcpy(diversified_key_c, emulator + 8 * 4, 8); // Kc
b67f7ec3 153 }
ae60ceca 154
8efd0b80 155 AppendCrc(conf_block, 8);
156
0ab9002f 157 // save card challenge for sim2,4 attack
158 if (reader_mac_buf != NULL) {
159 memcpy(reader_mac_buf, card_challenge_data, 8);
160 }
1e262141 161
ae60ceca 162 if (conf_block[5] & 0x80) {
163 page_size = 256 * 8;
164 }
165
166 // From PicoPass DS:
167 // When the page is in personalization mode this bit is equal to 1.
168 // Once the application issuer has personalized and coded its dedicated areas, this bit must be set to 0:
169 // the page is then "in application mode".
170 bool personalization_mode = conf_block[7] & 0x80;
171
172 // chip memory may be divided in 8 pages
173 uint8_t max_page = conf_block[4] & 0x10 ? 0 : 7;
8efd0b80 174
ae60ceca 175 // Precalculate the cipher states, feeding it the CC
176 cipher_state_KD[0] = opt_doTagMAC_1(card_challenge_data, diversified_key_d);
177 cipher_state_KC[0] = opt_doTagMAC_1(card_challenge_data, diversified_key_c);
178 if (simulationMode == ICLASS_SIM_MODE_FULL) {
179 for (int i = 1; i < max_page; i++) {
180 uint8_t *epurse = emulator + i*page_size + 8*2;
181 uint8_t *Kd = emulator + i*page_size + 8*3;
182 uint8_t *Kc = emulator + i*page_size + 8*4;
183 cipher_state_KD[i] = opt_doTagMAC_1(epurse, Kd);
184 cipher_state_KC[i] = opt_doTagMAC_1(epurse, Kc);
185 }
186 }
8efd0b80 187
ff7bb4ef 188 int exitLoop = 0;
1e262141 189 // Reader 0a
190 // Tag 0f
191 // Reader 0c
192 // Tag anticoll. CSN
193 // Reader 81 anticoll. CSN
194 // Tag CSN
195
55eaed8f 196 uint8_t *modulated_response;
b19caaef 197 int modulated_response_size = 0;
17505ce2 198 uint8_t *trace_data = NULL;
55eaed8f 199 int trace_data_size = 0;
1e262141 200
645c960f 201 // Respond SOF -- takes 1 bytes
ae60ceca 202 uint8_t *resp_sof = BigBuf_malloc(1);
b67f7ec3 203 int resp_sof_Len;
1e262141 204
205 // Anticollision CSN (rotated CSN)
645c960f 206 // 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte)
0ab9002f 207 uint8_t *resp_anticoll = BigBuf_malloc(22);
b67f7ec3 208 int resp_anticoll_len;
1e262141 209
0ab9002f 210 // CSN (block 0)
645c960f 211 // 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte)
0ab9002f 212 uint8_t *resp_csn = BigBuf_malloc(22);
b67f7ec3 213 int resp_csn_len;
1e262141 214
0ab9002f 215 // configuration (block 1) picopass 2ks
216 uint8_t *resp_conf = BigBuf_malloc(22);
217 int resp_conf_len;
0ab9002f 218
219 // e-Purse (block 2)
b3cc5f29 220 // 18: Takes 2 bytes for SOF/EOF and 8 * 2 = 16 bytes (2 bytes/bit)
0ab9002f 221 uint8_t *resp_cc = BigBuf_malloc(18);
b67f7ec3 222 int resp_cc_len;
1e262141 223
a66f26da 224 // Kd, Kc (blocks 3 and 4). Cannot be read. Always respond with 0xff bytes only
225 uint8_t *resp_ff = BigBuf_malloc(22);
226 int resp_ff_len;
227 uint8_t ff_data[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00};
228 AppendCrc(ff_data, 8);
229
0ab9002f 230 // Application Issuer Area (block 5)
231 uint8_t *resp_aia = BigBuf_malloc(22);
232 int resp_aia_len;
233 uint8_t aia_data[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00};
234 AppendCrc(aia_data, 8);
235
f71f4deb 236 uint8_t *receivedCmd = BigBuf_malloc(MAX_FRAME_SIZE);
1e262141 237 int len;
238
1e262141 239 // Prepare card messages
1e262141 240
3d2c9c9b 241 // First card answer: SOF only
1e262141 242 CodeIClassTagSOF();
17505ce2 243 memcpy(resp_sof, ToSend, ToSendMax);
244 resp_sof_Len = ToSendMax;
1e262141 245
246 // Anticollision CSN
3d2c9c9b 247 CodeIso15693AsTag(anticoll_data, sizeof(anticoll_data));
17505ce2 248 memcpy(resp_anticoll, ToSend, ToSendMax);
249 resp_anticoll_len = ToSendMax;
1e262141 250
0ab9002f 251 // CSN (block 0)
3d2c9c9b 252 CodeIso15693AsTag(csn_data, sizeof(csn_data));
17505ce2 253 memcpy(resp_csn, ToSend, ToSendMax);
254 resp_csn_len = ToSendMax;
1e262141 255
0ab9002f 256 // Configuration (block 1)
ae60ceca 257 CodeIso15693AsTag(conf_block, sizeof(conf_block));
0ab9002f 258 memcpy(resp_conf, ToSend, ToSendMax);
259 resp_conf_len = ToSendMax;
260
261 // e-Purse (block 2)
3d2c9c9b 262 CodeIso15693AsTag(card_challenge_data, sizeof(card_challenge_data));
0ab9002f 263 memcpy(resp_cc, ToSend, ToSendMax);
264 resp_cc_len = ToSendMax;
265
a66f26da 266 // Kd, Kc (blocks 3 and 4)
267 CodeIso15693AsTag(ff_data, sizeof(ff_data));
268 memcpy(resp_ff, ToSend, ToSendMax);
269 resp_ff_len = ToSendMax;
270
0ab9002f 271 // Application Issuer Area (block 5)
3d2c9c9b 272 CodeIso15693AsTag(aia_data, sizeof(aia_data));
0ab9002f 273 memcpy(resp_aia, ToSend, ToSendMax);
274 resp_aia_len = ToSendMax;
1e262141 275
b19caaef 276 //This is used for responding to READ-block commands or other data which is dynamically generated
a66f26da 277 uint8_t *data_generic_trace = BigBuf_malloc(32 + 2); // 32 bytes data + 2byte CRC is max tag answer
278 uint8_t *data_response = BigBuf_malloc( (32 + 2) * 2 + 2);
e3dc1e4c 279
f83cc126 280 bool buttonPressed = false;
5b12974a 281 enum { IDLE, ACTIVATED, SELECTED, HALTED } chip_state = IDLE;
282
17505ce2 283 while (!exitLoop) {
0ab9002f 284 WDT_HIT();
3fe4ff4f 285
3d2c9c9b 286 uint32_t reader_eof_time = 0;
287 len = GetIso15693CommandFromReader(receivedCmd, MAX_FRAME_SIZE, &reader_eof_time);
288 if (len < 0) {
f83cc126 289 buttonPressed = true;
1e262141 290 break;
81cd0474 291 }
a66f26da 292
0ab9002f 293 // Now look at the reader command and provide appropriate responses
294 // default is no response:
295 modulated_response = NULL;
296 modulated_response_size = 0;
297 trace_data = NULL;
298 trace_data_size = 0;
a66f26da 299
5b12974a 300 if (receivedCmd[0] == ICLASS_CMD_ACTALL && len == 1) {
301 // Reader in anticollision phase
302 if (chip_state != HALTED) {
303 modulated_response = resp_sof;
304 modulated_response_size = resp_sof_Len;
5b12974a 305 chip_state = ACTIVATED;
306 }
0ab9002f 307
308 } else if (receivedCmd[0] == ICLASS_CMD_READ_OR_IDENTIFY && len == 1) { // identify
5b12974a 309 // Reader asks for anticollision CSN
310 if (chip_state == SELECTED || chip_state == ACTIVATED) {
311 modulated_response = resp_anticoll;
312 modulated_response_size = resp_anticoll_len;
313 trace_data = anticoll_data;
314 trace_data_size = sizeof(anticoll_data);
315 }
316
317 } else if (receivedCmd[0] == ICLASS_CMD_SELECT && len == 9) {
318 // Reader selects anticollision CSN.
319 // Tag sends the corresponding real CSN
320 if (chip_state == ACTIVATED || chip_state == SELECTED) {
321 if (!memcmp(receivedCmd+1, anticoll_data, 8)) {
322 modulated_response = resp_csn;
323 modulated_response_size = resp_csn_len;
324 trace_data = csn_data;
325 trace_data_size = sizeof(csn_data);
326 chip_state = SELECTED;
327 } else {
328 chip_state = IDLE;
329 }
330 } else if (chip_state == HALTED) {
331 // RESELECT with CSN
332 if (!memcmp(receivedCmd+1, csn_data, 8)) {
333 modulated_response = resp_csn;
334 modulated_response_size = resp_csn_len;
335 trace_data = csn_data;
336 trace_data_size = sizeof(csn_data);
337 chip_state = SELECTED;
338 }
339 }
a66f26da 340
0ab9002f 341 } else if (receivedCmd[0] == ICLASS_CMD_READ_OR_IDENTIFY && len == 4) { // read block
342 uint16_t blockNo = receivedCmd[1];
5b12974a 343 if (chip_state == SELECTED) {
344 if (simulationMode == ICLASS_SIM_MODE_EXIT_AFTER_MAC) {
345 // provide defaults for blocks 0 ... 5
346 switch (blockNo) {
347 case 0: // csn (block 00)
348 modulated_response = resp_csn;
349 modulated_response_size = resp_csn_len;
350 trace_data = csn_data;
351 trace_data_size = sizeof(csn_data);
352 break;
353 case 1: // configuration (block 01)
354 modulated_response = resp_conf;
355 modulated_response_size = resp_conf_len;
ae60ceca 356 trace_data = conf_block;
357 trace_data_size = sizeof(conf_block);
5b12974a 358 break;
359 case 2: // e-purse (block 02)
360 modulated_response = resp_cc;
361 modulated_response_size = resp_cc_len;
362 trace_data = card_challenge_data;
363 trace_data_size = sizeof(card_challenge_data);
364 // set epurse of sim2,4 attack
365 if (reader_mac_buf != NULL) {
366 memcpy(reader_mac_buf, card_challenge_data, 8);
367 }
368 break;
369 case 3:
370 case 4: // Kd, Kc, always respond with 0xff bytes
371 modulated_response = resp_ff;
372 modulated_response_size = resp_ff_len;
373 trace_data = ff_data;
374 trace_data_size = sizeof(ff_data);
375 break;
376 case 5: // Application Issuer Area (block 05)
377 modulated_response = resp_aia;
378 modulated_response_size = resp_aia_len;
379 trace_data = aia_data;
380 trace_data_size = sizeof(aia_data);
381 break;
382 // default: don't respond
383 }
384 } else if (simulationMode == ICLASS_SIM_MODE_FULL) {
385 if (blockNo == 3 || blockNo == 4) { // Kd, Kc, always respond with 0xff bytes
a66f26da 386 modulated_response = resp_ff;
387 modulated_response_size = resp_ff_len;
388 trace_data = ff_data;
389 trace_data_size = sizeof(ff_data);
5b12974a 390 } else { // use data from emulator memory
ae60ceca 391 memcpy(data_generic_trace, emulator + current_page*page_size + 8*blockNo, 8);
5b12974a 392 AppendCrc(data_generic_trace, 8);
393 trace_data = data_generic_trace;
394 trace_data_size = 10;
395 CodeIso15693AsTag(trace_data, trace_data_size);
396 memcpy(data_response, ToSend, ToSendMax);
397 modulated_response = data_response;
398 modulated_response_size = ToSendMax;
399 }
0ab9002f 400 }
5b12974a 401 }
402
403 } else if ((receivedCmd[0] == ICLASS_CMD_READCHECK_KD
8ddb81a2 404 || receivedCmd[0] == ICLASS_CMD_READCHECK_KC) && receivedCmd[1] == 0x02 && len == 2) {
5b12974a 405 // Read e-purse (88 02 || 18 02)
406 if (chip_state == SELECTED) {
ae60ceca 407 if(receivedCmd[0] == ICLASS_CMD_READCHECK_KD){
408 cipher_state = &cipher_state_KD[current_page];
409 diversified_key = diversified_key_d;
410 } else {
8efd0b80 411 cipher_state = &cipher_state_KC[current_page];
ae60ceca 412 diversified_key = diversified_key_c;
413 }
5b12974a 414 modulated_response = resp_cc;
415 modulated_response_size = resp_cc_len;
416 trace_data = card_challenge_data;
417 trace_data_size = sizeof(card_challenge_data);
5b12974a 418 }
419
8efd0b80 420 } else if ((receivedCmd[0] == ICLASS_CMD_CHECK_KC
8ddb81a2 421 || receivedCmd[0] == ICLASS_CMD_CHECK_KD) && len == 9) {
5b12974a 422 // Reader random and reader MAC!!!
423 if (chip_state == SELECTED) {
424 if (simulationMode == ICLASS_SIM_MODE_FULL) {
425 //NR, from reader, is in receivedCmd+1
ae60ceca 426 opt_doTagMAC_2(*cipher_state, receivedCmd+1, data_generic_trace, diversified_key);
a66f26da 427 trace_data = data_generic_trace;
5b12974a 428 trace_data_size = 4;
a66f26da 429 CodeIso15693AsTag(trace_data, trace_data_size);
430 memcpy(data_response, ToSend, ToSendMax);
431 modulated_response = data_response;
432 modulated_response_size = ToSendMax;
5b12974a 433 //exitLoop = true;
434 } else { // Not fullsim, we don't respond
435 // We do not know what to answer, so lets keep quiet
436 if (simulationMode == ICLASS_SIM_MODE_EXIT_AFTER_MAC) {
437 if (reader_mac_buf != NULL) {
438 // save NR and MAC for sim 2,4
439 memcpy(reader_mac_buf + 8, receivedCmd + 1, 8);
440 }
441 exitLoop = true;
442 }
a66f26da 443 }
0ab9002f 444 }
445
5b12974a 446 } else if (receivedCmd[0] == ICLASS_CMD_HALT && len == 1) {
447 if (chip_state == SELECTED) {
448 // Reader ends the session
ae60ceca 449 modulated_response = resp_sof;
450 modulated_response_size = resp_sof_Len;
5b12974a 451 chip_state = HALTED;
452 }
0ab9002f 453
5b12974a 454 } else if (simulationMode == ICLASS_SIM_MODE_FULL && receivedCmd[0] == ICLASS_CMD_READ4 && len == 4) { // 0x06
455 //Read 4 blocks
456 if (chip_state == SELECTED) {
ae60ceca 457 uint8_t blockNo = receivedCmd[1];
458 memcpy(data_generic_trace, emulator + current_page*page_size + blockNo*8, 8 * 4);
5b12974a 459 AppendCrc(data_generic_trace, 8 * 4);
b19caaef 460 trace_data = data_generic_trace;
5b12974a 461 trace_data_size = 8 * 4 + 2;
3d2c9c9b 462 CodeIso15693AsTag(trace_data, trace_data_size);
b67f7ec3
MHS
463 memcpy(data_response, ToSend, ToSendMax);
464 modulated_response = data_response;
465 modulated_response_size = ToSendMax;
ff7bb4ef 466 }
b67f7ec3 467
5b12974a 468 } else if (receivedCmd[0] == ICLASS_CMD_UPDATE && (len == 12 || len == 14)) {
0ab9002f 469 // We're expected to respond with the data+crc, exactly what's already in the receivedCmd
470 // receivedCmd is now UPDATE 1b | ADDRESS 1b | DATA 8b | Signature 4b or CRC 2b
5b12974a 471 if (chip_state == SELECTED) {
8ddb81a2 472 uint8_t blockNo = receivedCmd[1];
473 if (blockNo == 2) { // update e-purse
474 memcpy(card_challenge_data, receivedCmd+2, 8);
475 CodeIso15693AsTag(card_challenge_data, sizeof(card_challenge_data));
476 memcpy(resp_cc, ToSend, ToSendMax);
477 resp_cc_len = ToSendMax;
ae60ceca 478 cipher_state_KD[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_d);
479 cipher_state_KC[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_c);
8ddb81a2 480 if (simulationMode == ICLASS_SIM_MODE_FULL) {
ae60ceca 481 memcpy(emulator + current_page*page_size + 8*2, card_challenge_data, 8);
8ddb81a2 482 }
483 } else if (blockNo == 3) { // update Kd
ae60ceca 484 for (int i = 0; i < 8; i++) {
485 if (personalization_mode) {
8efd0b80 486 diversified_key_d[i] = receivedCmd[2 + i];
ae60ceca 487 } else {
488 diversified_key_d[i] ^= receivedCmd[2 + i];
8efd0b80 489 }
8ddb81a2 490 }
8efd0b80 491 cipher_state_KD[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_d);
8ddb81a2 492 if (simulationMode == ICLASS_SIM_MODE_FULL) {
ae60ceca 493 memcpy(emulator + current_page*page_size + 8*3, diversified_key_d, 8);
8ddb81a2 494 }
8efd0b80 495 } else if (blockNo == 4) { // update Kc
ae60ceca 496 for (int i = 0; i < 8; i++) {
497 if (personalization_mode) {
8efd0b80 498 diversified_key_c[i] = receivedCmd[2 + i];
ae60ceca 499 } else {
500 diversified_key_c[i] ^= receivedCmd[2 + i];
8efd0b80 501 }
8ddb81a2 502 }
ae60ceca 503 cipher_state_KC[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_c);
8ddb81a2 504 if (simulationMode == ICLASS_SIM_MODE_FULL) {
ae60ceca 505 memcpy(emulator + current_page*page_size + 8*4, diversified_key_c, 8);
8ddb81a2 506 }
507 } else if (simulationMode == ICLASS_SIM_MODE_FULL) { // update any other data block
ae60ceca 508 memcpy(emulator + current_page*page_size + 8*blockNo, receivedCmd+2, 8);
8efd0b80 509 }
5b12974a 510 memcpy(data_generic_trace, receivedCmd + 2, 8);
511 AppendCrc(data_generic_trace, 8);
512 trace_data = data_generic_trace;
513 trace_data_size = 10;
514 CodeIso15693AsTag(trace_data, trace_data_size);
515 memcpy(data_response, ToSend, ToSendMax);
516 modulated_response = data_response;
517 modulated_response_size = ToSendMax;
518 }
519
520 } else if (receivedCmd[0] == ICLASS_CMD_PAGESEL && len == 4) {
0ab9002f 521 // Pagesel
ae60ceca 522 // Chips with a single page will not answer to this command
8efd0b80 523 // Otherwise, we should answer 8bytes (conf block 1) + 2bytes CRC
5b12974a 524 if (chip_state == SELECTED) {
ae60ceca 525 if (simulationMode == ICLASS_SIM_MODE_FULL && max_page > 0) {
526 current_page = receivedCmd[1];
527 memcpy(data_generic_trace, emulator + current_page*page_size + 8*1, 8);
528 memcpy(diversified_key_d, emulator + current_page*page_size + 8*3, 8);
8efd0b80 529 memcpy(diversified_key_c, emulator + current_page*page_size + 8*4, 8);
ae60ceca 530 cipher_state = &cipher_state_KD[current_page];
531 personalization_mode = data_generic_trace[7] & 0x80;
532 AppendCrc(data_generic_trace, 8);
533 trace_data = data_generic_trace;
534 trace_data_size = 10;
535 CodeIso15693AsTag(trace_data, trace_data_size);
8efd0b80 536 memcpy(data_response, ToSend, ToSendMax);
ae60ceca 537 modulated_response = data_response;
538 modulated_response_size = ToSendMax;
539 }
5b12974a 540 }
0ab9002f 541
e49d31c0 542 } else if (receivedCmd[0] == 0x26 && len == 5) {
543 // standard ISO15693 INVENTORY command. Ignore.
544
17505ce2 545 } else {
5b12974a 546 // don't know how to handle this command
3d2c9c9b 547 char debug_message[250]; // should be enough
548 sprintf(debug_message, "Unhandled command (len = %d) received from reader:", len);
549 for (int i = 0; i < len && strlen(debug_message) < sizeof(debug_message) - 3 - 1; i++) {
550 sprintf(debug_message + strlen(debug_message), " %02x", receivedCmd[i]);
551 }
552 Dbprintf("%s", debug_message);
1e262141 553 // Do not respond
1e262141 554 }
555
55eaed8f 556 /**
8efd0b80 557 A legit tag has about 273,4us delay between reader EOT and tag SOF.
55eaed8f 558 **/
17505ce2 559 if (modulated_response_size > 0) {
c41dd5f9 560 uint32_t response_time = reader_eof_time + DELAY_ICLASS_VCD_TO_VICC_SIM;
8efd0b80 561 TransmitTo15693Reader(modulated_response, modulated_response_size, &response_time, 0, false);
c41dd5f9 562 LogTrace_ISO15693(trace_data, trace_data_size, response_time*32, response_time*32 + modulated_response_size/2, NULL, false);
81cd0474 563 }
f83cc126 564
81cd0474 565 }
1e262141 566
17505ce2 567 if (buttonPressed)
f83cc126
MHS
568 {
569 DbpString("Button pressed");
570 }
f83cc126 571 return buttonPressed;
1e262141 572}
573
17505ce2 574/**
575 * @brief SimulateIClass simulates an iClass card.
576 * @param arg0 type of simulation
577 * - 0 uses the first 8 bytes in usb data as CSN
578 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
579 * in the usb data. This mode collects MAC from the reader, in order to do an offline
580 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
581 * - Other : Uses the default CSN (031fec8af7ff12e0)
582 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
583 * @param arg2
584 * @param datain
585 */
586void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain) {
8efd0b80 587
ae60ceca 588 LED_A_ON();
8efd0b80 589
17505ce2 590 uint32_t simType = arg0;
591 uint32_t numberOfCSNS = arg1;
0ab9002f 592
3d2c9c9b 593 // setup hardware for simulation:
17505ce2 594 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
3d2c9c9b 595 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
a66f26da 596 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION);
ae60ceca 597 LED_D_OFF();
3d2c9c9b 598 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR);
599 StartCountSspClk();
e3dc1e4c 600
17505ce2 601 // Enable and clear the trace
602 set_tracing(true);
603 clear_trace();
604 //Use the emulator memory for SIM
605 uint8_t *emulator = BigBuf_get_EM_addr();
e3dc1e4c 606
0ab9002f 607 if (simType == ICLASS_SIM_MODE_CSN) {
17505ce2 608 // Use the CSN from commandline
609 memcpy(emulator, datain, 8);
0ab9002f 610 doIClassSimulation(ICLASS_SIM_MODE_CSN, NULL);
611 } else if (simType == ICLASS_SIM_MODE_CSN_DEFAULT) {
17505ce2 612 //Default CSN
613 uint8_t csn_crc[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
614 // Use the CSN from commandline
615 memcpy(emulator, csn_crc, 8);
0ab9002f 616 doIClassSimulation(ICLASS_SIM_MODE_CSN, NULL);
617 } else if (simType == ICLASS_SIM_MODE_READER_ATTACK) {
17505ce2 618 uint8_t mac_responses[USB_CMD_DATA_SIZE] = { 0 };
619 Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS);
620 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
0ab9002f 621 // in order to collect MAC's from the reader. This can later be used in an offline-attack
17505ce2 622 // in order to obtain the keys, as in the "dismantling iclass"-paper.
0ab9002f 623 int i;
624 for (i = 0; i < numberOfCSNS && i*16+16 <= USB_CMD_DATA_SIZE; i++) {
625 // The usb data is 512 bytes, fitting 32 responses (8 byte CC + 4 Byte NR + 4 Byte MAC = 16 Byte response).
17505ce2 626 memcpy(emulator, datain+(i*8), 8);
0ab9002f 627 if (doIClassSimulation(ICLASS_SIM_MODE_EXIT_AFTER_MAC, mac_responses+i*16)) {
628 // Button pressed
629 break;
1e262141 630 }
3d2c9c9b 631 Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
632 datain[i*8+0], datain[i*8+1], datain[i*8+2], datain[i*8+3],
633 datain[i*8+4], datain[i*8+5], datain[i*8+6], datain[i*8+7]);
634 Dbprintf("NR,MAC: %02x %02x %02x %02x %02x %02x %02x %02x",
5b12974a 635 mac_responses[i*16+ 8], mac_responses[i*16+ 9], mac_responses[i*16+10], mac_responses[i*16+11],
636 mac_responses[i*16+12], mac_responses[i*16+13], mac_responses[i*16+14], mac_responses[i*16+15]);
637 SpinDelay(100); // give the reader some time to prepare for next CSN
1e262141 638 }
0ab9002f 639 cmd_send(CMD_ACK, CMD_SIMULATE_TAG_ICLASS, i, 0, mac_responses, i*16);
640 } else if (simType == ICLASS_SIM_MODE_FULL) {
17505ce2 641 //This is 'full sim' mode, where we use the emulator storage for data.
0ab9002f 642 doIClassSimulation(ICLASS_SIM_MODE_FULL, NULL);
17505ce2 643 } else {
644 // We may want a mode here where we hardcode the csns to use (from proxclone).
645 // That will speed things up a little, but not required just yet.
646 Dbprintf("The mode is not implemented, reserved for future use");
1e262141 647 }
ae60ceca 648
17505ce2 649 Dbprintf("Done...");
1e262141 650
ae60ceca 651 LED_A_OFF();
1e262141 652}
653
17505ce2 654
1e262141 655/// THE READER CODE
656
c41dd5f9 657static void ReaderTransmitIClass(uint8_t *frame, int len, uint32_t *start_time) {
17505ce2 658
c41dd5f9 659 CodeIso15693AsReader(frame, len);
17505ce2 660
c41dd5f9 661 TransmitTo15693Tag(ToSend, ToSendMax, start_time);
17505ce2 662
c41dd5f9 663 uint32_t end_time = *start_time + 32*(8*ToSendMax-4); // substract the 4 padding bits after EOF
664 LogTrace_ISO15693(frame, len, *start_time*4, end_time*4, NULL, true);
1e262141 665}
666
667
ece38ef3 668static bool sendCmdGetResponseWithRetries(uint8_t* command, size_t cmdsize, uint8_t* resp, size_t max_resp_size,
496bb4be 669 uint8_t expected_size, uint8_t retries, uint32_t start_time, uint32_t timeout, uint32_t *eof_time) {
17505ce2 670 while (retries-- > 0) {
c41dd5f9 671 ReaderTransmitIClass(command, cmdsize, &start_time);
496bb4be 672 if (expected_size == GetIso15693AnswerFromTag(resp, max_resp_size, timeout, eof_time)) {
aa53efc3 673 return true;
c8dd9b09
MHS
674 }
675 }
aa53efc3 676 return false;//Error
c8dd9b09
MHS
677}
678
679/**
ece38ef3 680 * @brief Selects an iclass tag
681 * @param card_data where the CSN is stored for return
682 * @return false = fail
683 * true = success
c8dd9b09 684 */
ece38ef3 685static bool selectIclassTag(uint8_t *card_data, uint32_t *eof_time) {
c41dd5f9 686 uint8_t act_all[] = { 0x0a };
687 uint8_t identify[] = { 0x0c };
688 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
caaf9618 689
f71f4deb 690 uint8_t resp[ICLASS_BUFFER_SIZE];
c8dd9b09 691
c41dd5f9 692 uint32_t start_time = GetCountSspClk();
c8dd9b09
MHS
693
694 // Send act_all
c41dd5f9 695 ReaderTransmitIClass(act_all, 1, &start_time);
c8dd9b09 696 // Card present?
ece38ef3 697 if (GetIso15693AnswerFromTag(resp, sizeof(resp), ICLASS_READER_TIMEOUT_ACTALL, eof_time) < 0) return false;//Fail
698
c8dd9b09 699 //Send Identify
c41dd5f9 700 start_time = *eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
701 ReaderTransmitIClass(identify, 1, &start_time);
c8dd9b09 702 //We expect a 10-byte response here, 8 byte anticollision-CSN and 2 byte CRC
c41dd5f9 703 uint8_t len = GetIso15693AnswerFromTag(resp, sizeof(resp), ICLASS_READER_TIMEOUT_OTHERS, eof_time);
ece38ef3 704 if (len != 10) return false;//Fail
c8dd9b09
MHS
705
706 //Copy the Anti-collision CSN to our select-packet
17505ce2 707 memcpy(&select[1], resp, 8);
c8dd9b09 708 //Select the card
c41dd5f9 709 start_time = *eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
710 ReaderTransmitIClass(select, sizeof(select), &start_time);
c8dd9b09 711 //We expect a 10-byte response here, 8 byte CSN and 2 byte CRC
c41dd5f9 712 len = GetIso15693AnswerFromTag(resp, sizeof(resp), ICLASS_READER_TIMEOUT_OTHERS, eof_time);
ece38ef3 713 if (len != 10) return false;//Fail
c8dd9b09 714
ece38ef3 715 //Success - we got CSN
c8dd9b09 716 //Save CSN in response data
17505ce2 717 memcpy(card_data, resp, 8);
c8dd9b09 718
ece38ef3 719 return true;
aa53efc3 720}
c8dd9b09 721
caaf9618 722
ece38ef3 723// Select an iClass tag and read all blocks which are always readable without authentication
496bb4be 724void ReaderIClass(uint8_t flags) {
1e262141 725
ece38ef3 726 LED_A_ON();
727
17505ce2 728 uint8_t card_data[6 * 8] = {0};
83602aff 729 memset(card_data, 0xFF, sizeof(card_data));
34e2af02 730 uint8_t resp[ICLASS_BUFFER_SIZE];
caaf9618 731 //Read conf block CRC(0x01) => 0xfa 0x22
ece38ef3 732 uint8_t readConf[] = {ICLASS_CMD_READ_OR_IDENTIFY, 0x01, 0xfa, 0x22};
733 //Read e-purse block CRC(0x02) => 0x61 0x10
734 uint8_t readEpurse[] = {ICLASS_CMD_READ_OR_IDENTIFY, 0x02, 0x61, 0x10};
34e2af02 735 //Read App Issuer Area block CRC(0x05) => 0xde 0x64
ece38ef3 736 uint8_t readAA[] = {ICLASS_CMD_READ_OR_IDENTIFY, 0x05, 0xde, 0x64};
caaf9618 737
caaf9618 738 uint8_t result_status = 0;
34e2af02 739
496bb4be 740 if (flags & FLAG_ICLASS_READER_INIT) {
741 Iso15693InitReader();
742 }
34e2af02 743
496bb4be 744 if (flags & FLAG_ICLASS_READER_CLEARTRACE) {
745 set_tracing(true);
746 clear_trace();
747 StartCountSspClk();
748 }
1e262141 749
c41dd5f9 750 uint32_t start_time = 0;
751 uint32_t eof_time = 0;
ece38ef3 752
753 if (selectIclassTag(resp, &eof_time)) {
754 result_status = FLAG_ICLASS_READER_CSN;
755 memcpy(card_data, resp, 8);
756 }
496bb4be 757
ece38ef3 758 start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
496bb4be 759
ece38ef3 760 //Read block 1, config
496bb4be 761 if (flags & FLAG_ICLASS_READER_CONF) {
762 if (sendCmdGetResponseWithRetries(readConf, sizeof(readConf), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
ece38ef3 763 result_status |= FLAG_ICLASS_READER_CONF;
764 memcpy(card_data+8, resp, 8);
765 } else {
766 Dbprintf("Failed to read config block");
c8dd9b09 767 }
c41dd5f9 768 start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
ece38ef3 769 }
c8dd9b09 770
ece38ef3 771 //Read block 2, e-purse
496bb4be 772 if (flags & FLAG_ICLASS_READER_CC) {
773 if (sendCmdGetResponseWithRetries(readEpurse, sizeof(readEpurse), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
ece38ef3 774 result_status |= FLAG_ICLASS_READER_CC;
775 memcpy(card_data + (8*2), resp, 8);
776 } else {
777 Dbprintf("Failed to read e-purse");
caaf9618 778 }
ece38ef3 779 start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
780 }
caaf9618 781
ece38ef3 782 //Read block 5, AA
496bb4be 783 if (flags & FLAG_ICLASS_READER_AA) {
784 if (sendCmdGetResponseWithRetries(readAA, sizeof(readAA), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
ece38ef3 785 result_status |= FLAG_ICLASS_READER_AA;
786 memcpy(card_data + (8*5), resp, 8);
787 } else {
788 Dbprintf("Failed to read AA block");
c8dd9b09 789 }
6ce0e538 790 }
ece38ef3 791
792 cmd_send(CMD_ACK, result_status, 0, 0, card_data, sizeof(card_data));
793
3ac22ee1 794 LED_A_OFF();
cee5a30d 795}
796
ece38ef3 797
c3963755 798void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) {
c8dd9b09 799
ece38ef3 800 LED_A_ON();
801
802 bool use_credit_key = false;
cb29e00a 803 uint8_t card_data[USB_CMD_DATA_SIZE]={0};
39d3ce5d
MHS
804 uint16_t block_crc_LUT[255] = {0};
805
17505ce2 806 //Generate a lookup table for block crc
807 for (int block = 0; block < 255; block++){
808 char bl = block;
809 block_crc_LUT[block] = iclass_crc16(&bl ,1);
39d3ce5d
MHS
810 }
811 //Dbprintf("Lookup table: %02x %02x %02x" ,block_crc_LUT[0],block_crc_LUT[1],block_crc_LUT[2]);
c8dd9b09 812
ece38ef3 813 uint8_t readcheck_cc[] = { ICLASS_CMD_READCHECK_KD, 0x02 };
814 if (use_credit_key)
815 readcheck_cc[0] = ICLASS_CMD_READCHECK_KC;
c3963755 816 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
817 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
17505ce2 818
819 uint16_t crc = 0;
820 uint8_t cardsize = 0;
821 uint8_t mem = 0;
822
823 static struct memory_t {
824 int k16;
825 int book;
826 int k2;
827 int lockauth;
828 int keyaccess;
c3963755 829 } memory;
17505ce2 830
f71f4deb 831 uint8_t resp[ICLASS_BUFFER_SIZE];
17505ce2 832
44964fd1 833 set_tracing(true);
c41dd5f9 834 clear_trace();
835 Iso15693InitReader();
c3963755 836
c41dd5f9 837 StartCountSspClk();
838 uint32_t start_time = 0;
839 uint32_t eof_time = 0;
ece38ef3 840
17505ce2 841 while (!BUTTON_PRESS()) {
842
39d3ce5d
MHS
843 WDT_HIT();
844
17505ce2 845 if (!get_tracing()) {
c3963755 846 DbpString("Trace full");
847 break;
848 }
17505ce2 849
ece38ef3 850 if (!selectIclassTag(card_data, &eof_time)) continue;
c41dd5f9 851
ece38ef3 852 start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
496bb4be 853 if (!sendCmdGetResponseWithRetries(readcheck_cc, sizeof(readcheck_cc), resp, sizeof(resp), 8, 3, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) continue;
c8dd9b09 854
ece38ef3 855 // replay captured auth (cc must not have been updated)
17505ce2 856 memcpy(check+5, MAC, 4);
c8dd9b09 857
ece38ef3 858 start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
496bb4be 859 if (!sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 5, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
c8dd9b09
MHS
860 Dbprintf("Error: Authentication Fail!");
861 continue;
862 }
863
39d3ce5d
MHS
864 //first get configuration block (block 1)
865 crc = block_crc_LUT[1];
17505ce2 866 read[1] = 1;
c8dd9b09
MHS
867 read[2] = crc >> 8;
868 read[3] = crc & 0xff;
869
ece38ef3 870 start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
496bb4be 871 if (!sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
c41dd5f9 872 start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
39d3ce5d 873 Dbprintf("Dump config (block 1) failed");
c8dd9b09
MHS
874 continue;
875 }
876
17505ce2 877 mem = resp[5];
878 memory.k16 = (mem & 0x80);
879 memory.book = (mem & 0x20);
880 memory.k2 = (mem & 0x8);
881 memory.lockauth = (mem & 0x2);
882 memory.keyaccess = (mem & 0x1);
c8dd9b09
MHS
883
884 cardsize = memory.k16 ? 255 : 32;
885 WDT_HIT();
cb29e00a 886 //Set card_data to all zeroes, we'll fill it with data
17505ce2 887 memset(card_data, 0x0, USB_CMD_DATA_SIZE);
888 uint8_t failedRead = 0;
889 uint32_t stored_data_length = 0;
c8dd9b09 890 //then loop around remaining blocks
17505ce2 891 for (int block = 0; block < cardsize; block++) {
892 read[1] = block;
39d3ce5d 893 crc = block_crc_LUT[block];
c8dd9b09
MHS
894 read[2] = crc >> 8;
895 read[3] = crc & 0xff;
896
ece38ef3 897 start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
496bb4be 898 if (sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
c8dd9b09 899 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
17505ce2 900 block, resp[0], resp[1], resp[2],
c8dd9b09
MHS
901 resp[3], resp[4], resp[5],
902 resp[6], resp[7]);
903
cb29e00a 904 //Fill up the buffer
17505ce2 905 memcpy(card_data+stored_data_length, resp, 8);
cb29e00a 906 stored_data_length += 8;
17505ce2 907 if (stored_data_length +8 > USB_CMD_DATA_SIZE) {
908 //Time to send this off and start afresh
cb29e00a
MHS
909 cmd_send(CMD_ACK,
910 stored_data_length,//data length
911 failedRead,//Failed blocks?
912 0,//Not used ATM
913 card_data, stored_data_length);
914 //reset
915 stored_data_length = 0;
916 failedRead = 0;
917 }
918
17505ce2 919 } else {
cb29e00a 920 failedRead = 1;
17505ce2 921 stored_data_length += 8;//Otherwise, data becomes misaligned
c8dd9b09 922 Dbprintf("Failed to dump block %d", block);
c3963755 923 }
924 }
428d6221 925
cb29e00a 926 //Send off any remaining data
17505ce2 927 if (stored_data_length > 0) {
cb29e00a
MHS
928 cmd_send(CMD_ACK,
929 stored_data_length,//data length
930 failedRead,//Failed blocks?
931 0,//Not used ATM
17505ce2 932 card_data,
933 stored_data_length);
cb29e00a 934 }
c8dd9b09
MHS
935 //If we got here, let's break
936 break;
c3963755 937 }
cb29e00a
MHS
938 //Signal end of transmission
939 cmd_send(CMD_ACK,
940 0,//data length
941 0,//Failed blocks?
942 0,//Not used ATM
17505ce2 943 card_data,
944 0);
cb29e00a 945
f784539d 946 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
ece38ef3 947 LED_D_OFF();
c3963755 948 LED_A_OFF();
949}
950
ece38ef3 951
952void iClass_Check(uint8_t *MAC) {
496bb4be 953 uint8_t check[9] = {ICLASS_CMD_CHECK_KD, 0x00};
ece38ef3 954 uint8_t resp[4];
17505ce2 955 memcpy(check+5, MAC, 4);
c41dd5f9 956 uint32_t eof_time;
496bb4be 957 bool isOK = sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 6, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
ece38ef3 958 cmd_send(CMD_ACK, isOK, 0, 0, resp, sizeof(resp));
959}
960
961
962void iClass_Readcheck(uint8_t block, bool use_credit_key) {
963 uint8_t readcheck[2] = {ICLASS_CMD_READCHECK_KD, block};
964 if (use_credit_key) {
965 readcheck[0] = ICLASS_CMD_READCHECK_KC;
966 }
967 uint8_t resp[8];
968 uint32_t eof_time;
496bb4be 969 bool isOK = sendCmdGetResponseWithRetries(readcheck, sizeof(readcheck), resp, sizeof(resp), 8, 6, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
ece38ef3 970 cmd_send(CMD_ACK, isOK, 0, 0, resp, sizeof(resp));
aa53efc3 971}
17505ce2 972
ece38ef3 973
f784539d 974static bool iClass_ReadBlock(uint8_t blockNo, uint8_t *readdata) {
3ac22ee1 975 uint8_t readcmd[] = {ICLASS_CMD_READ_OR_IDENTIFY, blockNo, 0x00, 0x00}; //0x88, 0x00 // can i use 0C?
976 char bl = blockNo;
977 uint16_t rdCrc = iclass_crc16(&bl, 1);
978 readcmd[2] = rdCrc >> 8;
979 readcmd[3] = rdCrc & 0xff;
c41dd5f9 980 uint8_t resp[10];
c41dd5f9 981 uint32_t eof_time;
ece38ef3 982
496bb4be 983 bool isOK = sendCmdGetResponseWithRetries(readcmd, sizeof(readcmd), resp, sizeof(resp), 10, 10, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
3ac22ee1 984 memcpy(readdata, resp, sizeof(resp));
fecd8202 985
aa53efc3 986 return isOK;
987}
fecd8202 988
ece38ef3 989
3ac22ee1 990void iClass_ReadBlk(uint8_t blockno) {
ece38ef3 991
992 LED_A_ON();
993
3ac22ee1 994 uint8_t readblockdata[] = {0,0,0,0,0,0,0,0,0,0};
496bb4be 995 bool isOK = iClass_ReadBlock(blockno, readblockdata);
3ac22ee1 996 cmd_send(CMD_ACK, isOK, 0, 0, readblockdata, 8);
f784539d 997 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
ece38ef3 998 LED_D_OFF();
999
1000 LED_A_OFF();
aa53efc3 1001}
fecd8202 1002
496bb4be 1003
1004void iClass_Dump(uint8_t startblock, uint8_t numblks) {
ece38ef3 1005
1006 LED_A_ON();
1007
496bb4be 1008 uint8_t readblockdata[USB_CMD_DATA_SIZE+2] = {0};
aa53efc3 1009 bool isOK = false;
496bb4be 1010 uint16_t blkCnt = 0;
fecd8202 1011
496bb4be 1012 if (numblks > USB_CMD_DATA_SIZE / 8) {
1013 numblks = USB_CMD_DATA_SIZE / 8;
aa53efc3 1014 }
496bb4be 1015
1016 for (blkCnt = 0; blkCnt < numblks; blkCnt++) {
1017 isOK = iClass_ReadBlock(startblock+blkCnt, readblockdata+8*blkCnt);
1018 if (!isOK) {
1019 Dbprintf("Block %02X failed to read", startblock+blkCnt);
1020 break;
fecd8202 1021 }
aa53efc3 1022 }
ece38ef3 1023
496bb4be 1024 cmd_send(CMD_ACK, isOK, blkCnt, 0, readblockdata, blkCnt*8);
ece38ef3 1025
1026 LED_A_OFF();
aa53efc3 1027}
1028
ece38ef3 1029
17505ce2 1030static bool iClass_WriteBlock_ext(uint8_t blockNo, uint8_t *data) {
ece38ef3 1031
496bb4be 1032 uint8_t write[16] = {ICLASS_CMD_UPDATE, blockNo};
aa53efc3 1033 memcpy(write+2, data, 12); // data + mac
496bb4be 1034 AppendCrc(write+1, 13);
c41dd5f9 1035 uint8_t resp[10];
671ff89f 1036 bool isOK = false;
c41dd5f9 1037 uint32_t eof_time = 0;
ece38ef3 1038
496bb4be 1039 isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 10, 0, ICLASS_READER_TIMEOUT_UPDATE, &eof_time);
1040 if (isOK && blockNo != 3 && blockNo != 4 && memcmp(write+2, resp, 8)) { // check response
1041 isOK = false;
fecd8202 1042 }
ece38ef3 1043
aa53efc3 1044 return isOK;
1045}
1046
ece38ef3 1047
3ac22ee1 1048void iClass_WriteBlock(uint8_t blockNo, uint8_t *data) {
ece38ef3 1049
1050 LED_A_ON();
1051
3ac22ee1 1052 bool isOK = iClass_WriteBlock_ext(blockNo, data);
496bb4be 1053 if (isOK) {
17505ce2 1054 Dbprintf("Write block [%02x] successful", blockNo);
aa53efc3 1055 } else {
17505ce2 1056 Dbprintf("Write block [%02x] failed", blockNo);
aa53efc3 1057 }
17505ce2 1058 cmd_send(CMD_ACK, isOK, 0, 0, 0, 0);
ece38ef3 1059
f784539d 1060 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
ece38ef3 1061 LED_D_OFF();
1062
1063 LED_A_OFF();
aa53efc3 1064}
1065
496bb4be 1066
3ac22ee1 1067void iClass_Clone(uint8_t startblock, uint8_t endblock, uint8_t *data) {
aa53efc3 1068 int i;
1069 int written = 0;
1070 int total_block = (endblock - startblock) + 1;
17505ce2 1071 for (i = 0; i < total_block; i++) {
aa53efc3 1072 // block number
17505ce2 1073 if (iClass_WriteBlock_ext(i+startblock, data + (i*12))){
1074 Dbprintf("Write block [%02x] successful", i + startblock);
aa53efc3 1075 written++;
1076 } else {
17505ce2 1077 if (iClass_WriteBlock_ext(i+startblock, data + (i*12))){
1078 Dbprintf("Write block [%02x] successful", i + startblock);
aa53efc3 1079 written++;
1080 } else {
17505ce2 1081 Dbprintf("Write block [%02x] failed", i + startblock);
aa53efc3 1082 }
1083 }
1084 }
1085 if (written == total_block)
1086 Dbprintf("Clone complete");
1087 else
17505ce2 1088 Dbprintf("Clone incomplete");
aa53efc3 1089
17505ce2 1090 cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
aa53efc3 1091 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
ece38ef3 1092 LED_D_OFF();
1093 LED_A_OFF();
aa53efc3 1094}
Impressum, Datenschutz