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