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