]>
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 |
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 | |
1e262141 | 60 | static int timeout = 4096; |
cee5a30d | 61 | |
cee5a30d | 62 | //----------------------------------------------------------------------------- |
63 | // The software UART that receives commands from the reader, and its state | |
64 | // variables. | |
65 | //----------------------------------------------------------------------------- | |
66 | static struct { | |
17505ce2 | 67 | enum { |
68 | STATE_UNSYNCD, | |
69 | STATE_START_OF_COMMUNICATION, | |
70 | STATE_RECEIVING | |
71 | } state; | |
72 | uint16_t shiftReg; | |
73 | int bitCnt; | |
74 | int byteCnt; | |
75 | int byteCntMax; | |
76 | int posCnt; | |
77 | int nOutOfCnt; | |
78 | int OutOfCnt; | |
79 | int syncBit; | |
80 | int samples; | |
81 | int highCnt; | |
82 | int swapper; | |
83 | int counter; | |
84 | int bitBuffer; | |
85 | int dropPosition; | |
86 | uint8_t *output; | |
cee5a30d | 87 | } Uart; |
88 | ||
17505ce2 | 89 | static RAMFUNC int OutOfNDecoding(int bit) { |
9f693930 | 90 | //int error = 0; |
cee5a30d | 91 | int bitright; |
92 | ||
17505ce2 | 93 | if (!Uart.bitBuffer) { |
cee5a30d | 94 | Uart.bitBuffer = bit ^ 0xFF0; |
44964fd1 | 95 | return false; |
17505ce2 | 96 | } else { |
cee5a30d | 97 | Uart.bitBuffer <<= 4; |
98 | Uart.bitBuffer ^= bit; | |
99 | } | |
17505ce2 | 100 | |
101 | /*if (Uart.swapper) { | |
cee5a30d | 102 | Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF; |
103 | Uart.byteCnt++; | |
104 | Uart.swapper = 0; | |
17505ce2 | 105 | if (Uart.byteCnt > 15) { return true; } |
cee5a30d | 106 | } |
107 | else { | |
108 | Uart.swapper = 1; | |
109 | }*/ | |
110 | ||
17505ce2 | 111 | if (Uart.state != STATE_UNSYNCD) { |
cee5a30d | 112 | Uart.posCnt++; |
113 | ||
17505ce2 | 114 | if ((Uart.bitBuffer & Uart.syncBit) ^ Uart.syncBit) { |
cee5a30d | 115 | bit = 0x00; |
17505ce2 | 116 | } else { |
cee5a30d | 117 | bit = 0x01; |
118 | } | |
17505ce2 | 119 | if (((Uart.bitBuffer << 1) & Uart.syncBit) ^ Uart.syncBit) { |
cee5a30d | 120 | bitright = 0x00; |
17505ce2 | 121 | } else { |
cee5a30d | 122 | bitright = 0x01; |
123 | } | |
17505ce2 | 124 | if (bit != bitright) { |
125 | bit = bitright; | |
126 | } | |
127 | ||
cee5a30d | 128 | |
cee5a30d | 129 | // So, now we only have to deal with *bit*, lets see... |
17505ce2 | 130 | if (Uart.posCnt == 1) { |
cee5a30d | 131 | // measurement first half bitperiod |
17505ce2 | 132 | if (!bit) { |
cee5a30d | 133 | // Drop in first half means that we are either seeing |
134 | // an SOF or an EOF. | |
135 | ||
17505ce2 | 136 | if (Uart.nOutOfCnt == 1) { |
cee5a30d | 137 | // End of Communication |
138 | Uart.state = STATE_UNSYNCD; | |
139 | Uart.highCnt = 0; | |
17505ce2 | 140 | if (Uart.byteCnt == 0) { |
cee5a30d | 141 | // Its not straightforward to show single EOFs |
44964fd1 | 142 | // So just leave it and do not return true |
6a1f2d82 | 143 | Uart.output[0] = 0xf0; |
cee5a30d | 144 | Uart.byteCnt++; |
17505ce2 | 145 | } else { |
44964fd1 | 146 | return true; |
cee5a30d | 147 | } |
17505ce2 | 148 | } else if (Uart.state != STATE_START_OF_COMMUNICATION) { |
cee5a30d | 149 | // When not part of SOF or EOF, it is an error |
150 | Uart.state = STATE_UNSYNCD; | |
151 | Uart.highCnt = 0; | |
9f693930 | 152 | //error = 4; |
cee5a30d | 153 | } |
154 | } | |
17505ce2 | 155 | } else { |
cee5a30d | 156 | // measurement second half bitperiod |
157 | // Count the bitslot we are in... (ISO 15693) | |
158 | Uart.nOutOfCnt++; | |
17505ce2 | 159 | |
160 | if (!bit) { | |
161 | if (Uart.dropPosition) { | |
162 | if (Uart.state == STATE_START_OF_COMMUNICATION) { | |
9f693930 | 163 | //error = 1; |
17505ce2 | 164 | } else { |
9f693930 | 165 | //error = 7; |
cee5a30d | 166 | } |
167 | // It is an error if we already have seen a drop in current frame | |
168 | Uart.state = STATE_UNSYNCD; | |
169 | Uart.highCnt = 0; | |
17505ce2 | 170 | } else { |
cee5a30d | 171 | Uart.dropPosition = Uart.nOutOfCnt; |
172 | } | |
173 | } | |
174 | ||
175 | Uart.posCnt = 0; | |
176 | ||
17505ce2 | 177 | |
178 | if (Uart.nOutOfCnt == Uart.OutOfCnt && Uart.OutOfCnt == 4) { | |
cee5a30d | 179 | Uart.nOutOfCnt = 0; |
17505ce2 | 180 | |
181 | if (Uart.state == STATE_START_OF_COMMUNICATION) { | |
182 | if (Uart.dropPosition == 4) { | |
cee5a30d | 183 | Uart.state = STATE_RECEIVING; |
184 | Uart.OutOfCnt = 256; | |
17505ce2 | 185 | } else if (Uart.dropPosition == 3) { |
cee5a30d | 186 | Uart.state = STATE_RECEIVING; |
187 | Uart.OutOfCnt = 4; | |
188 | //Uart.output[Uart.byteCnt] = 0xdd; | |
189 | //Uart.byteCnt++; | |
17505ce2 | 190 | } else { |
cee5a30d | 191 | Uart.state = STATE_UNSYNCD; |
192 | Uart.highCnt = 0; | |
193 | } | |
194 | Uart.dropPosition = 0; | |
17505ce2 | 195 | } else { |
cee5a30d | 196 | // RECEIVING DATA |
197 | // 1 out of 4 | |
17505ce2 | 198 | if (!Uart.dropPosition) { |
cee5a30d | 199 | Uart.state = STATE_UNSYNCD; |
200 | Uart.highCnt = 0; | |
9f693930 | 201 | //error = 9; |
17505ce2 | 202 | } else { |
cee5a30d | 203 | Uart.shiftReg >>= 2; |
17505ce2 | 204 | |
cee5a30d | 205 | // Swap bit order |
206 | Uart.dropPosition--; | |
17505ce2 | 207 | //if (Uart.dropPosition == 1) { Uart.dropPosition = 2; } |
208 | //else if (Uart.dropPosition == 2) { Uart.dropPosition = 1; } | |
209 | ||
cee5a30d | 210 | Uart.shiftReg ^= ((Uart.dropPosition & 0x03) << 6); |
211 | Uart.bitCnt += 2; | |
212 | Uart.dropPosition = 0; | |
213 | ||
17505ce2 | 214 | if (Uart.bitCnt == 8) { |
cee5a30d | 215 | Uart.output[Uart.byteCnt] = (Uart.shiftReg & 0xff); |
216 | Uart.byteCnt++; | |
cee5a30d | 217 | Uart.bitCnt = 0; |
218 | Uart.shiftReg = 0; | |
219 | } | |
220 | } | |
221 | } | |
17505ce2 | 222 | } else if (Uart.nOutOfCnt == Uart.OutOfCnt) { |
cee5a30d | 223 | // RECEIVING DATA |
224 | // 1 out of 256 | |
17505ce2 | 225 | if (!Uart.dropPosition) { |
cee5a30d | 226 | Uart.state = STATE_UNSYNCD; |
227 | Uart.highCnt = 0; | |
9f693930 | 228 | //error = 3; |
17505ce2 | 229 | } else { |
cee5a30d | 230 | Uart.dropPosition--; |
231 | Uart.output[Uart.byteCnt] = (Uart.dropPosition & 0xff); | |
232 | Uart.byteCnt++; | |
cee5a30d | 233 | Uart.bitCnt = 0; |
234 | Uart.shiftReg = 0; | |
235 | Uart.nOutOfCnt = 0; | |
236 | Uart.dropPosition = 0; | |
237 | } | |
238 | } | |
239 | ||
17505ce2 | 240 | /*if (error) { |
cee5a30d | 241 | Uart.output[Uart.byteCnt] = 0xAA; |
242 | Uart.byteCnt++; | |
243 | Uart.output[Uart.byteCnt] = error & 0xFF; | |
244 | Uart.byteCnt++; | |
245 | Uart.output[Uart.byteCnt] = 0xAA; | |
246 | Uart.byteCnt++; | |
247 | Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF; | |
248 | Uart.byteCnt++; | |
249 | Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF; | |
250 | Uart.byteCnt++; | |
251 | Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF; | |
252 | Uart.byteCnt++; | |
253 | Uart.output[Uart.byteCnt] = 0xAA; | |
254 | Uart.byteCnt++; | |
44964fd1 | 255 | return true; |
cee5a30d | 256 | }*/ |
257 | } | |
258 | ||
17505ce2 | 259 | } else { |
cee5a30d | 260 | bit = Uart.bitBuffer & 0xf0; |
261 | bit >>= 4; | |
262 | bit ^= 0x0F; // drops become 1s ;-) | |
17505ce2 | 263 | if (bit) { |
cee5a30d | 264 | // should have been high or at least (4 * 128) / fc |
265 | // according to ISO this should be at least (9 * 128 + 20) / fc | |
17505ce2 | 266 | if (Uart.highCnt == 8) { |
cee5a30d | 267 | // we went low, so this could be start of communication |
268 | // it turns out to be safer to choose a less significant | |
269 | // syncbit... so we check whether the neighbour also represents the drop | |
270 | Uart.posCnt = 1; // apparently we are busy with our first half bit period | |
271 | Uart.syncBit = bit & 8; | |
272 | Uart.samples = 3; | |
17505ce2 | 273 | if (!Uart.syncBit) { Uart.syncBit = bit & 4; Uart.samples = 2; } |
274 | else if (bit & 4) { Uart.syncBit = bit & 4; Uart.samples = 2; bit <<= 2; } | |
275 | if (!Uart.syncBit) { Uart.syncBit = bit & 2; Uart.samples = 1; } | |
276 | else if (bit & 2) { Uart.syncBit = bit & 2; Uart.samples = 1; bit <<= 1; } | |
277 | if (!Uart.syncBit) { Uart.syncBit = bit & 1; Uart.samples = 0; | |
278 | if (Uart.syncBit && (Uart.bitBuffer & 8)) { | |
cee5a30d | 279 | Uart.syncBit = 8; |
280 | ||
281 | // the first half bit period is expected in next sample | |
282 | Uart.posCnt = 0; | |
283 | Uart.samples = 3; | |
284 | } | |
17505ce2 | 285 | } else if (bit & 1) { Uart.syncBit = bit & 1; Uart.samples = 0; } |
cee5a30d | 286 | |
287 | Uart.syncBit <<= 4; | |
288 | Uart.state = STATE_START_OF_COMMUNICATION; | |
289 | Uart.bitCnt = 0; | |
290 | Uart.byteCnt = 0; | |
cee5a30d | 291 | Uart.nOutOfCnt = 0; |
292 | Uart.OutOfCnt = 4; // Start at 1/4, could switch to 1/256 | |
293 | Uart.dropPosition = 0; | |
294 | Uart.shiftReg = 0; | |
9f693930 | 295 | //error = 0; |
17505ce2 | 296 | } else { |
cee5a30d | 297 | Uart.highCnt = 0; |
298 | } | |
17505ce2 | 299 | } else if (Uart.highCnt < 8) { |
300 | Uart.highCnt++; | |
cee5a30d | 301 | } |
302 | } | |
303 | ||
17505ce2 | 304 | return false; |
cee5a30d | 305 | } |
306 | ||
17505ce2 | 307 | |
cee5a30d | 308 | //============================================================================= |
1e262141 | 309 | // Manchester |
cee5a30d | 310 | //============================================================================= |
311 | ||
312 | static struct { | |
17505ce2 | 313 | enum { |
314 | DEMOD_UNSYNCD, | |
cee5a30d | 315 | DEMOD_START_OF_COMMUNICATION, |
316 | DEMOD_START_OF_COMMUNICATION2, | |
317 | DEMOD_START_OF_COMMUNICATION3, | |
318 | DEMOD_SOF_COMPLETE, | |
319 | DEMOD_MANCHESTER_D, | |
320 | DEMOD_MANCHESTER_E, | |
321 | DEMOD_END_OF_COMMUNICATION, | |
322 | DEMOD_END_OF_COMMUNICATION2, | |
323 | DEMOD_MANCHESTER_F, | |
17505ce2 | 324 | DEMOD_ERROR_WAIT |
325 | } state; | |
326 | int bitCount; | |
327 | int posCount; | |
328 | int syncBit; | |
329 | uint16_t shiftReg; | |
330 | int buffer; | |
331 | int buffer2; | |
332 | int buffer3; | |
333 | int buff; | |
334 | int samples; | |
335 | int len; | |
cee5a30d | 336 | enum { |
337 | SUB_NONE, | |
338 | SUB_FIRST_HALF, | |
339 | SUB_SECOND_HALF, | |
340 | SUB_BOTH | |
17505ce2 | 341 | } sub; |
342 | uint8_t *output; | |
cee5a30d | 343 | } Demod; |
344 | ||
17505ce2 | 345 | static RAMFUNC int ManchesterDecoding(int v) { |
cee5a30d | 346 | int bit; |
347 | int modulation; | |
348 | int error = 0; | |
349 | ||
350 | bit = Demod.buffer; | |
351 | Demod.buffer = Demod.buffer2; | |
352 | Demod.buffer2 = Demod.buffer3; | |
353 | Demod.buffer3 = v; | |
354 | ||
17505ce2 | 355 | if (Demod.buff < 3) { |
cee5a30d | 356 | Demod.buff++; |
44964fd1 | 357 | return false; |
cee5a30d | 358 | } |
359 | ||
17505ce2 | 360 | if (Demod.state==DEMOD_UNSYNCD) { |
cee5a30d | 361 | Demod.output[Demod.len] = 0xfa; |
362 | Demod.syncBit = 0; | |
363 | //Demod.samples = 0; | |
17505ce2 | 364 | Demod.posCount = 1; // This is the first half bit period, so after syncing handle the second part |
cee5a30d | 365 | |
17505ce2 | 366 | if (bit & 0x08) { |
cee5a30d | 367 | Demod.syncBit = 0x08; |
368 | } | |
369 | ||
17505ce2 | 370 | if (bit & 0x04) { |
371 | if (Demod.syncBit) { | |
cee5a30d | 372 | bit <<= 4; |
373 | } | |
374 | Demod.syncBit = 0x04; | |
375 | } | |
376 | ||
17505ce2 | 377 | if (bit & 0x02) { |
378 | if (Demod.syncBit) { | |
cee5a30d | 379 | bit <<= 2; |
380 | } | |
381 | Demod.syncBit = 0x02; | |
382 | } | |
383 | ||
17505ce2 | 384 | if (bit & 0x01 && Demod.syncBit) { |
cee5a30d | 385 | Demod.syncBit = 0x01; |
386 | } | |
17505ce2 | 387 | |
388 | if (Demod.syncBit) { | |
cee5a30d | 389 | Demod.len = 0; |
390 | Demod.state = DEMOD_START_OF_COMMUNICATION; | |
391 | Demod.sub = SUB_FIRST_HALF; | |
392 | Demod.bitCount = 0; | |
393 | Demod.shiftReg = 0; | |
cee5a30d | 394 | Demod.samples = 0; |
17505ce2 | 395 | if (Demod.posCount) { |
0ab9002f | 396 | switch (Demod.syncBit) { |
cee5a30d | 397 | case 0x08: Demod.samples = 3; break; |
398 | case 0x04: Demod.samples = 2; break; | |
399 | case 0x02: Demod.samples = 1; break; | |
400 | case 0x01: Demod.samples = 0; break; | |
401 | } | |
402 | // SOF must be long burst... otherwise stay unsynced!!! | |
17505ce2 | 403 | if (!(Demod.buffer & Demod.syncBit) || !(Demod.buffer2 & Demod.syncBit)) { |
cee5a30d | 404 | Demod.state = DEMOD_UNSYNCD; |
405 | } | |
17505ce2 | 406 | } else { |
cee5a30d | 407 | // SOF must be long burst... otherwise stay unsynced!!! |
17505ce2 | 408 | if (!(Demod.buffer2 & Demod.syncBit) || !(Demod.buffer3 & Demod.syncBit)) { |
cee5a30d | 409 | Demod.state = DEMOD_UNSYNCD; |
410 | error = 0x88; | |
411 | } | |
412 | ||
413 | } | |
414 | error = 0; | |
415 | ||
416 | } | |
17505ce2 | 417 | } else { |
0ab9002f | 418 | // state is DEMOD is in SYNC from here on. |
cee5a30d | 419 | modulation = bit & Demod.syncBit; |
420 | modulation |= ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit; | |
cee5a30d | 421 | |
422 | Demod.samples += 4; | |
423 | ||
0ab9002f | 424 | if (Demod.posCount == 0) { |
cee5a30d | 425 | Demod.posCount = 1; |
17505ce2 | 426 | if (modulation) { |
cee5a30d | 427 | Demod.sub = SUB_FIRST_HALF; |
17505ce2 | 428 | } else { |
cee5a30d | 429 | Demod.sub = SUB_NONE; |
430 | } | |
17505ce2 | 431 | } else { |
cee5a30d | 432 | Demod.posCount = 0; |
17505ce2 | 433 | if (modulation) { |
434 | if (Demod.sub == SUB_FIRST_HALF) { | |
cee5a30d | 435 | Demod.sub = SUB_BOTH; |
17505ce2 | 436 | } else { |
cee5a30d | 437 | Demod.sub = SUB_SECOND_HALF; |
438 | } | |
17505ce2 | 439 | } else if (Demod.sub == SUB_NONE) { |
440 | if (Demod.state == DEMOD_SOF_COMPLETE) { | |
cee5a30d | 441 | Demod.output[Demod.len] = 0x0f; |
442 | Demod.len++; | |
cee5a30d | 443 | Demod.state = DEMOD_UNSYNCD; |
44964fd1 | 444 | return true; |
17505ce2 | 445 | } else { |
cee5a30d | 446 | Demod.state = DEMOD_ERROR_WAIT; |
447 | error = 0x33; | |
448 | } | |
cee5a30d | 449 | } |
450 | ||
451 | switch(Demod.state) { | |
452 | case DEMOD_START_OF_COMMUNICATION: | |
17505ce2 | 453 | if (Demod.sub == SUB_BOTH) { |
cee5a30d | 454 | Demod.state = DEMOD_START_OF_COMMUNICATION2; |
455 | Demod.posCount = 1; | |
456 | Demod.sub = SUB_NONE; | |
17505ce2 | 457 | } else { |
cee5a30d | 458 | Demod.output[Demod.len] = 0xab; |
459 | Demod.state = DEMOD_ERROR_WAIT; | |
460 | error = 0xd2; | |
461 | } | |
462 | break; | |
463 | case DEMOD_START_OF_COMMUNICATION2: | |
17505ce2 | 464 | if (Demod.sub == SUB_SECOND_HALF) { |
cee5a30d | 465 | Demod.state = DEMOD_START_OF_COMMUNICATION3; |
17505ce2 | 466 | } else { |
cee5a30d | 467 | Demod.output[Demod.len] = 0xab; |
468 | Demod.state = DEMOD_ERROR_WAIT; | |
469 | error = 0xd3; | |
470 | } | |
471 | break; | |
472 | case DEMOD_START_OF_COMMUNICATION3: | |
17505ce2 | 473 | if (Demod.sub == SUB_SECOND_HALF) { |
cee5a30d | 474 | Demod.state = DEMOD_SOF_COMPLETE; |
17505ce2 | 475 | } else { |
cee5a30d | 476 | Demod.output[Demod.len] = 0xab; |
477 | Demod.state = DEMOD_ERROR_WAIT; | |
478 | error = 0xd4; | |
479 | } | |
480 | break; | |
481 | case DEMOD_SOF_COMPLETE: | |
482 | case DEMOD_MANCHESTER_D: | |
483 | case DEMOD_MANCHESTER_E: | |
484 | // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443) | |
485 | // 00001111 = 1 (0 in 14443) | |
17505ce2 | 486 | if (Demod.sub == SUB_SECOND_HALF) { // SUB_FIRST_HALF |
cee5a30d | 487 | Demod.bitCount++; |
488 | Demod.shiftReg = (Demod.shiftReg >> 1) ^ 0x100; | |
489 | Demod.state = DEMOD_MANCHESTER_D; | |
17505ce2 | 490 | } else if (Demod.sub == SUB_FIRST_HALF) { // SUB_SECOND_HALF |
cee5a30d | 491 | Demod.bitCount++; |
492 | Demod.shiftReg >>= 1; | |
493 | Demod.state = DEMOD_MANCHESTER_E; | |
17505ce2 | 494 | } else if (Demod.sub == SUB_BOTH) { |
cee5a30d | 495 | Demod.state = DEMOD_MANCHESTER_F; |
17505ce2 | 496 | } else { |
cee5a30d | 497 | Demod.state = DEMOD_ERROR_WAIT; |
498 | error = 0x55; | |
499 | } | |
500 | break; | |
501 | ||
502 | case DEMOD_MANCHESTER_F: | |
503 | // Tag response does not need to be a complete byte! | |
17505ce2 | 504 | if (Demod.len > 0 || Demod.bitCount > 0) { |
505 | if (Demod.bitCount > 1) { // was > 0, do not interpret last closing bit, is part of EOF | |
506 | Demod.shiftReg >>= (9 - Demod.bitCount); // right align data | |
cee5a30d | 507 | Demod.output[Demod.len] = Demod.shiftReg & 0xff; |
508 | Demod.len++; | |
cee5a30d | 509 | } |
510 | ||
511 | Demod.state = DEMOD_UNSYNCD; | |
44964fd1 | 512 | return true; |
17505ce2 | 513 | } else { |
cee5a30d | 514 | Demod.output[Demod.len] = 0xad; |
515 | Demod.state = DEMOD_ERROR_WAIT; | |
516 | error = 0x03; | |
517 | } | |
518 | break; | |
519 | ||
520 | case DEMOD_ERROR_WAIT: | |
521 | Demod.state = DEMOD_UNSYNCD; | |
522 | break; | |
523 | ||
524 | default: | |
525 | Demod.output[Demod.len] = 0xdd; | |
526 | Demod.state = DEMOD_UNSYNCD; | |
527 | break; | |
528 | } | |
529 | ||
17505ce2 | 530 | if (Demod.bitCount >= 8) { |
cee5a30d | 531 | Demod.shiftReg >>= 1; |
532 | Demod.output[Demod.len] = (Demod.shiftReg & 0xff); | |
533 | Demod.len++; | |
cee5a30d | 534 | Demod.bitCount = 0; |
535 | Demod.shiftReg = 0; | |
536 | } | |
537 | ||
17505ce2 | 538 | if (error) { |
cee5a30d | 539 | Demod.output[Demod.len] = 0xBB; |
540 | Demod.len++; | |
541 | Demod.output[Demod.len] = error & 0xFF; | |
542 | Demod.len++; | |
543 | Demod.output[Demod.len] = 0xBB; | |
544 | Demod.len++; | |
545 | Demod.output[Demod.len] = bit & 0xFF; | |
546 | Demod.len++; | |
547 | Demod.output[Demod.len] = Demod.buffer & 0xFF; | |
548 | Demod.len++; | |
549 | // Look harder ;-) | |
550 | Demod.output[Demod.len] = Demod.buffer2 & 0xFF; | |
551 | Demod.len++; | |
552 | Demod.output[Demod.len] = Demod.syncBit & 0xFF; | |
553 | Demod.len++; | |
554 | Demod.output[Demod.len] = 0xBB; | |
555 | Demod.len++; | |
44964fd1 | 556 | return true; |
cee5a30d | 557 | } |
558 | ||
559 | } | |
560 | ||
561 | } // end (state != UNSYNCED) | |
562 | ||
17505ce2 | 563 | return false; |
cee5a30d | 564 | } |
565 | ||
566 | //============================================================================= | |
1e262141 | 567 | // Finally, a `sniffer' for iClass communication |
cee5a30d | 568 | // Both sides of communication! |
569 | //============================================================================= | |
570 | ||
571 | //----------------------------------------------------------------------------- | |
572 | // Record the sequence of commands sent by the reader to the tag, with | |
573 | // triggering so that we start recording at the point that the tag is moved | |
574 | // near the reader. | |
575 | //----------------------------------------------------------------------------- | |
17505ce2 | 576 | void RAMFUNC SnoopIClass(void) { |
cee5a30d | 577 | |
17505ce2 | 578 | // We won't start recording the frames that we acquire until we trigger; |
579 | // a good trigger condition to get started is probably when we see a | |
580 | // response from the tag. | |
581 | //int triggered = false; // false to wait first for card | |
cee5a30d | 582 | |
17505ce2 | 583 | // The command (reader -> tag) that we're receiving. |
cee5a30d | 584 | // The length of a received command will in most cases be no more than 18 bytes. |
585 | // So 32 should be enough! | |
f71f4deb | 586 | #define ICLASS_BUFFER_SIZE 32 |
587 | uint8_t readerToTagCmd[ICLASS_BUFFER_SIZE]; | |
17505ce2 | 588 | // The response (tag -> reader) that we're receiving. |
f71f4deb | 589 | uint8_t tagToReaderResponse[ICLASS_BUFFER_SIZE]; |
17505ce2 | 590 | |
591 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); | |
592 | ||
593 | // free all BigBuf memory | |
f71f4deb | 594 | BigBuf_free(); |
17505ce2 | 595 | // The DMA buffer, used to stream samples from the FPGA |
596 | uint8_t *dmaBuf = BigBuf_malloc(DMA_BUFFER_SIZE); | |
597 | ||
44964fd1 | 598 | set_tracing(true); |
3000dc4e | 599 | clear_trace(); |
17505ce2 | 600 | iso14a_set_trigger(false); |
cee5a30d | 601 | |
f71f4deb | 602 | int lastRxCounter; |
17505ce2 | 603 | uint8_t *upTo; |
604 | int smpl; | |
605 | int maxBehindBy = 0; | |
cee5a30d | 606 | |
17505ce2 | 607 | // Count of samples received so far, so that we can include timing |
608 | // information in the trace buffer. | |
609 | int samples = 0; | |
610 | rsamples = 0; | |
cee5a30d | 611 | |
17505ce2 | 612 | // Set up the demodulator for tag -> reader responses. |
17cba269 | 613 | Demod.output = tagToReaderResponse; |
17505ce2 | 614 | Demod.len = 0; |
615 | Demod.state = DEMOD_UNSYNCD; | |
cee5a30d | 616 | |
17505ce2 | 617 | // Setup for the DMA. |
618 | FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO14443A); | |
619 | upTo = dmaBuf; | |
620 | lastRxCounter = DMA_BUFFER_SIZE; | |
621 | FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE); | |
cee5a30d | 622 | |
17505ce2 | 623 | // And the reader -> tag commands |
624 | memset(&Uart, 0, sizeof(Uart)); | |
17cba269 | 625 | Uart.output = readerToTagCmd; |
17505ce2 | 626 | Uart.byteCntMax = 32; // was 100 (greg)//////////////////////////////////////////////////////////////////////// |
627 | Uart.state = STATE_UNSYNCD; | |
cee5a30d | 628 | |
17505ce2 | 629 | // And put the FPGA in the appropriate mode |
630 | // Signal field is off with the appropriate LED | |
631 | LED_D_OFF(); | |
632 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_SNIFFER); | |
633 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
cee5a30d | 634 | |
81012e67 | 635 | uint32_t time_0 = GetCountSspClk(); |
55eaed8f MHS |
636 | uint32_t time_start = 0; |
637 | uint32_t time_stop = 0; | |
81012e67 | 638 | |
17505ce2 | 639 | int div = 0; |
640 | //int div2 = 0; | |
641 | int decbyte = 0; | |
642 | int decbyter = 0; | |
cee5a30d | 643 | |
17505ce2 | 644 | // And now we loop, receiving samples. |
645 | for (;;) { | |
646 | LED_A_ON(); | |
647 | WDT_HIT(); | |
648 | int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & (DMA_BUFFER_SIZE-1); | |
649 | if (behindBy > maxBehindBy) { | |
650 | maxBehindBy = behindBy; | |
651 | if (behindBy > (9 * DMA_BUFFER_SIZE / 10)) { | |
652 | Dbprintf("blew circular buffer! behindBy=0x%x", behindBy); | |
653 | goto done; | |
654 | } | |
cee5a30d | 655 | } |
17505ce2 | 656 | if (behindBy < 1) continue; |
cee5a30d | 657 | |
17505ce2 | 658 | LED_A_OFF(); |
659 | smpl = upTo[0]; | |
660 | upTo++; | |
661 | lastRxCounter -= 1; | |
662 | if (upTo - dmaBuf > DMA_BUFFER_SIZE) { | |
663 | upTo -= DMA_BUFFER_SIZE; | |
664 | lastRxCounter += DMA_BUFFER_SIZE; | |
665 | AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo; | |
666 | AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE; | |
667 | } | |
668 | ||
669 | //samples += 4; | |
670 | samples += 1; | |
671 | ||
672 | if (smpl & 0xF) { | |
673 | decbyte ^= (1 << (3 - div)); | |
674 | } | |
675 | ||
676 | // FOR READER SIDE COMMUMICATION... | |
677 | ||
678 | decbyter <<= 2; | |
679 | decbyter ^= (smpl & 0x30); | |
680 | ||
681 | div++; | |
682 | ||
683 | if ((div + 1) % 2 == 0) { | |
684 | smpl = decbyter; | |
685 | if (OutOfNDecoding((smpl & 0xF0) >> 4)) { | |
686 | rsamples = samples - Uart.samples; | |
687 | time_stop = (GetCountSspClk()-time_0) << 4; | |
688 | LED_C_ON(); | |
689 | ||
690 | //if (!LogTrace(Uart.output, Uart.byteCnt, rsamples, Uart.parityBits,true)) break; | |
691 | //if (!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, true)) break; | |
692 | uint8_t parity[MAX_PARITY_SIZE]; | |
693 | GetParity(Uart.output, Uart.byteCnt, parity); | |
694 | LogTrace(Uart.output, Uart.byteCnt, time_start, time_stop, parity, true); | |
695 | ||
696 | /* And ready to receive another command. */ | |
697 | Uart.state = STATE_UNSYNCD; | |
698 | /* And also reset the demod code, which might have been */ | |
699 | /* false-triggered by the commands from the reader. */ | |
700 | Demod.state = DEMOD_UNSYNCD; | |
701 | LED_B_OFF(); | |
702 | Uart.byteCnt = 0; | |
703 | } else { | |
704 | time_start = (GetCountSspClk()-time_0) << 4; | |
705 | } | |
706 | decbyter = 0; | |
707 | } | |
708 | ||
709 | if (div > 3) { | |
710 | smpl = decbyte; | |
711 | if (ManchesterDecoding(smpl & 0x0F)) { | |
712 | time_stop = (GetCountSspClk()-time_0) << 4; | |
713 | ||
714 | rsamples = samples - Demod.samples; | |
715 | LED_B_ON(); | |
716 | ||
717 | uint8_t parity[MAX_PARITY_SIZE]; | |
718 | GetParity(Demod.output, Demod.len, parity); | |
719 | LogTrace(Demod.output, Demod.len, time_start, time_stop, parity, false); | |
720 | ||
721 | // And ready to receive another response. | |
722 | memset(&Demod, 0, sizeof(Demod)); | |
723 | Demod.output = tagToReaderResponse; | |
724 | Demod.state = DEMOD_UNSYNCD; | |
725 | LED_C_OFF(); | |
726 | } else { | |
727 | time_start = (GetCountSspClk()-time_0) << 4; | |
728 | } | |
729 | ||
730 | div = 0; | |
731 | decbyte = 0x00; | |
cee5a30d | 732 | } |
cee5a30d | 733 | |
17505ce2 | 734 | if (BUTTON_PRESS()) { |
735 | DbpString("cancelled_a"); | |
736 | goto done; | |
737 | } | |
738 | } | |
cee5a30d | 739 | |
17505ce2 | 740 | DbpString("COMMAND FINISHED"); |
cee5a30d | 741 | |
17505ce2 | 742 | Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt); |
3000dc4e | 743 | Dbprintf("%x %x %x", Uart.byteCntMax, BigBuf_get_traceLen(), (int)Uart.output[0]); |
cee5a30d | 744 | |
745 | done: | |
17505ce2 | 746 | AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS; |
747 | Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt); | |
3000dc4e | 748 | Dbprintf("%x %x %x", Uart.byteCntMax, BigBuf_get_traceLen(), (int)Uart.output[0]); |
17505ce2 | 749 | LEDsoff(); |
1e262141 | 750 | } |
751 | ||
912a3e94 | 752 | void rotateCSN(uint8_t* originalCSN, uint8_t* rotatedCSN) { |
17505ce2 | 753 | int i; |
754 | for (i = 0; i < 8; i++) { | |
912a3e94 | 755 | rotatedCSN[i] = (originalCSN[i] >> 3) | (originalCSN[(i+1)%8] << 5); |
1e262141 | 756 | } |
757 | } | |
758 | ||
3d2c9c9b | 759 | // Encode SOF only |
17505ce2 | 760 | static void CodeIClassTagSOF() { |
81012e67 | 761 | ToSendReset(); |
645c960f | 762 | ToSend[++ToSendMax] = 0x1D; |
1e262141 | 763 | ToSendMax++; |
764 | } | |
1e262141 | 765 | |
17505ce2 | 766 | static void AppendCrc(uint8_t *data, int len) { |
767 | ComputeCrc14443(CRC_ICLASS, data, len, data+len, data+len+1); | |
768 | } | |
81cd0474 | 769 | |
b67f7ec3 | 770 | |
ff7bb4ef MHS |
771 | /** |
772 | * @brief Does the actual simulation | |
ff7bb4ef | 773 | */ |
17505ce2 | 774 | int doIClassSimulation(int simulationMode, uint8_t *reader_mac_buf) { |
0ab9002f | 775 | |
b67f7ec3 MHS |
776 | // free eventually allocated BigBuf memory |
777 | BigBuf_free_keep_EM(); | |
55eaed8f | 778 | |
ae60ceca | 779 | uint16_t page_size = 32 * 8; |
780 | uint8_t current_page = 0; | |
0ab9002f | 781 | |
ae60ceca | 782 | // maintain cipher states for both credit and debit key for each page |
783 | State cipher_state_KC[8]; | |
784 | State cipher_state_KD[8]; | |
785 | State *cipher_state = &cipher_state_KD[0]; | |
786 | ||
0ab9002f | 787 | uint8_t *emulator = BigBuf_get_EM_addr(); |
788 | uint8_t *csn = emulator; | |
0ab9002f | 789 | |
1e262141 | 790 | // CSN followed by two CRC bytes |
ae60ceca | 791 | uint8_t anticoll_data[10]; |
792 | uint8_t csn_data[10]; | |
17505ce2 | 793 | memcpy(csn_data, csn, sizeof(csn_data)); |
794 | 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 | 795 | |
1e262141 | 796 | // Construct anticollision-CSN |
17505ce2 | 797 | rotateCSN(csn_data, anticoll_data); |
1e262141 | 798 | |
799 | // Compute CRC on both CSNs | |
0ab9002f | 800 | AppendCrc(anticoll_data, 8); |
801 | AppendCrc(csn_data, 8); | |
b67f7ec3 | 802 | |
ae60ceca | 803 | uint8_t diversified_key_d[8]; |
804 | uint8_t diversified_key_c[8]; | |
805 | uint8_t *diversified_key = diversified_key_d; | |
806 | ||
807 | // configuration block | |
808 | uint8_t conf_block[10] = {0x12, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0xFF, 0x3C, 0x00, 0x00}; | |
809 | AppendCrc(conf_block, 8); | |
810 | ||
b67f7ec3 | 811 | // e-Purse |
0ab9002f | 812 | uint8_t card_challenge_data[8] = { 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
ae60ceca | 813 | |
0ab9002f | 814 | if (simulationMode == ICLASS_SIM_MODE_FULL) { |
ae60ceca | 815 | // initialize from page 0 |
816 | memcpy(conf_block, emulator + 8 * 1, 8); | |
817 | memcpy(card_challenge_data, emulator + 8 * 2, 8); // e-purse | |
818 | memcpy(diversified_key_d, emulator + 8 * 3, 8); // Kd | |
819 | memcpy(diversified_key_c, emulator + 8 * 4, 8); // Kc | |
b67f7ec3 | 820 | } |
ae60ceca | 821 | |
0ab9002f | 822 | // save card challenge for sim2,4 attack |
823 | if (reader_mac_buf != NULL) { | |
824 | memcpy(reader_mac_buf, card_challenge_data, 8); | |
825 | } | |
1e262141 | 826 | |
ae60ceca | 827 | if (conf_block[5] & 0x80) { |
828 | page_size = 256 * 8; | |
829 | } | |
830 | ||
831 | // From PicoPass DS: | |
832 | // When the page is in personalization mode this bit is equal to 1. | |
833 | // Once the application issuer has personalized and coded its dedicated areas, this bit must be set to 0: | |
834 | // the page is then "in application mode". | |
835 | bool personalization_mode = conf_block[7] & 0x80; | |
836 | ||
837 | // chip memory may be divided in 8 pages | |
838 | uint8_t max_page = conf_block[4] & 0x10 ? 0 : 7; | |
839 | ||
840 | // Precalculate the cipher states, feeding it the CC | |
841 | cipher_state_KD[0] = opt_doTagMAC_1(card_challenge_data, diversified_key_d); | |
842 | cipher_state_KC[0] = opt_doTagMAC_1(card_challenge_data, diversified_key_c); | |
843 | if (simulationMode == ICLASS_SIM_MODE_FULL) { | |
844 | for (int i = 1; i < max_page; i++) { | |
845 | uint8_t *epurse = emulator + i*page_size + 8*2; | |
846 | uint8_t *Kd = emulator + i*page_size + 8*3; | |
847 | uint8_t *Kc = emulator + i*page_size + 8*4; | |
848 | cipher_state_KD[i] = opt_doTagMAC_1(epurse, Kd); | |
849 | cipher_state_KC[i] = opt_doTagMAC_1(epurse, Kc); | |
850 | } | |
851 | } | |
852 | ||
ff7bb4ef | 853 | int exitLoop = 0; |
1e262141 | 854 | // Reader 0a |
855 | // Tag 0f | |
856 | // Reader 0c | |
857 | // Tag anticoll. CSN | |
858 | // Reader 81 anticoll. CSN | |
859 | // Tag CSN | |
860 | ||
55eaed8f | 861 | uint8_t *modulated_response; |
b19caaef | 862 | int modulated_response_size = 0; |
17505ce2 | 863 | uint8_t *trace_data = NULL; |
55eaed8f | 864 | int trace_data_size = 0; |
1e262141 | 865 | |
645c960f | 866 | // Respond SOF -- takes 1 bytes |
ae60ceca | 867 | uint8_t *resp_sof = BigBuf_malloc(1); |
b67f7ec3 | 868 | int resp_sof_Len; |
1e262141 | 869 | |
870 | // Anticollision CSN (rotated CSN) | |
645c960f | 871 | // 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte) |
0ab9002f | 872 | uint8_t *resp_anticoll = BigBuf_malloc(22); |
b67f7ec3 | 873 | int resp_anticoll_len; |
1e262141 | 874 | |
0ab9002f | 875 | // CSN (block 0) |
645c960f | 876 | // 22: Takes 2 bytes for SOF/EOF and 10 * 2 = 20 bytes (2 bytes/byte) |
0ab9002f | 877 | uint8_t *resp_csn = BigBuf_malloc(22); |
b67f7ec3 | 878 | int resp_csn_len; |
1e262141 | 879 | |
0ab9002f | 880 | // configuration (block 1) picopass 2ks |
881 | uint8_t *resp_conf = BigBuf_malloc(22); | |
882 | int resp_conf_len; | |
0ab9002f | 883 | |
884 | // e-Purse (block 2) | |
b3cc5f29 | 885 | // 18: Takes 2 bytes for SOF/EOF and 8 * 2 = 16 bytes (2 bytes/bit) |
0ab9002f | 886 | uint8_t *resp_cc = BigBuf_malloc(18); |
b67f7ec3 | 887 | int resp_cc_len; |
1e262141 | 888 | |
a66f26da | 889 | // Kd, Kc (blocks 3 and 4). Cannot be read. Always respond with 0xff bytes only |
890 | uint8_t *resp_ff = BigBuf_malloc(22); | |
891 | int resp_ff_len; | |
892 | uint8_t ff_data[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}; | |
893 | AppendCrc(ff_data, 8); | |
894 | ||
0ab9002f | 895 | // Application Issuer Area (block 5) |
896 | uint8_t *resp_aia = BigBuf_malloc(22); | |
897 | int resp_aia_len; | |
898 | uint8_t aia_data[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}; | |
899 | AppendCrc(aia_data, 8); | |
900 | ||
f71f4deb | 901 | uint8_t *receivedCmd = BigBuf_malloc(MAX_FRAME_SIZE); |
1e262141 | 902 | int len; |
903 | ||
1e262141 | 904 | // Prepare card messages |
1e262141 | 905 | |
3d2c9c9b | 906 | // First card answer: SOF only |
1e262141 | 907 | CodeIClassTagSOF(); |
17505ce2 | 908 | memcpy(resp_sof, ToSend, ToSendMax); |
909 | resp_sof_Len = ToSendMax; | |
1e262141 | 910 | |
911 | // Anticollision CSN | |
3d2c9c9b | 912 | CodeIso15693AsTag(anticoll_data, sizeof(anticoll_data)); |
17505ce2 | 913 | memcpy(resp_anticoll, ToSend, ToSendMax); |
914 | resp_anticoll_len = ToSendMax; | |
1e262141 | 915 | |
0ab9002f | 916 | // CSN (block 0) |
3d2c9c9b | 917 | CodeIso15693AsTag(csn_data, sizeof(csn_data)); |
17505ce2 | 918 | memcpy(resp_csn, ToSend, ToSendMax); |
919 | resp_csn_len = ToSendMax; | |
1e262141 | 920 | |
0ab9002f | 921 | // Configuration (block 1) |
ae60ceca | 922 | CodeIso15693AsTag(conf_block, sizeof(conf_block)); |
0ab9002f | 923 | memcpy(resp_conf, ToSend, ToSendMax); |
924 | resp_conf_len = ToSendMax; | |
925 | ||
926 | // e-Purse (block 2) | |
3d2c9c9b | 927 | CodeIso15693AsTag(card_challenge_data, sizeof(card_challenge_data)); |
0ab9002f | 928 | memcpy(resp_cc, ToSend, ToSendMax); |
929 | resp_cc_len = ToSendMax; | |
930 | ||
a66f26da | 931 | // Kd, Kc (blocks 3 and 4) |
932 | CodeIso15693AsTag(ff_data, sizeof(ff_data)); | |
933 | memcpy(resp_ff, ToSend, ToSendMax); | |
934 | resp_ff_len = ToSendMax; | |
935 | ||
0ab9002f | 936 | // Application Issuer Area (block 5) |
3d2c9c9b | 937 | CodeIso15693AsTag(aia_data, sizeof(aia_data)); |
0ab9002f | 938 | memcpy(resp_aia, ToSend, ToSendMax); |
939 | resp_aia_len = ToSendMax; | |
1e262141 | 940 | |
b19caaef | 941 | //This is used for responding to READ-block commands or other data which is dynamically generated |
a66f26da | 942 | uint8_t *data_generic_trace = BigBuf_malloc(32 + 2); // 32 bytes data + 2byte CRC is max tag answer |
943 | uint8_t *data_response = BigBuf_malloc( (32 + 2) * 2 + 2); | |
e3dc1e4c | 944 | |
f83cc126 | 945 | bool buttonPressed = false; |
5b12974a | 946 | enum { IDLE, ACTIVATED, SELECTED, HALTED } chip_state = IDLE; |
947 | ||
17505ce2 | 948 | while (!exitLoop) { |
0ab9002f | 949 | WDT_HIT(); |
3fe4ff4f | 950 | |
3d2c9c9b | 951 | uint32_t reader_eof_time = 0; |
952 | len = GetIso15693CommandFromReader(receivedCmd, MAX_FRAME_SIZE, &reader_eof_time); | |
953 | if (len < 0) { | |
f83cc126 | 954 | buttonPressed = true; |
1e262141 | 955 | break; |
81cd0474 | 956 | } |
a66f26da | 957 | |
0ab9002f | 958 | // Now look at the reader command and provide appropriate responses |
959 | // default is no response: | |
960 | modulated_response = NULL; | |
961 | modulated_response_size = 0; | |
962 | trace_data = NULL; | |
963 | trace_data_size = 0; | |
a66f26da | 964 | |
5b12974a | 965 | if (receivedCmd[0] == ICLASS_CMD_ACTALL && len == 1) { |
966 | // Reader in anticollision phase | |
967 | if (chip_state != HALTED) { | |
968 | modulated_response = resp_sof; | |
969 | modulated_response_size = resp_sof_Len; | |
5b12974a | 970 | chip_state = ACTIVATED; |
971 | } | |
0ab9002f | 972 | |
973 | } else if (receivedCmd[0] == ICLASS_CMD_READ_OR_IDENTIFY && len == 1) { // identify | |
5b12974a | 974 | // Reader asks for anticollision CSN |
975 | if (chip_state == SELECTED || chip_state == ACTIVATED) { | |
976 | modulated_response = resp_anticoll; | |
977 | modulated_response_size = resp_anticoll_len; | |
978 | trace_data = anticoll_data; | |
979 | trace_data_size = sizeof(anticoll_data); | |
980 | } | |
981 | ||
982 | } else if (receivedCmd[0] == ICLASS_CMD_SELECT && len == 9) { | |
983 | // Reader selects anticollision CSN. | |
984 | // Tag sends the corresponding real CSN | |
985 | if (chip_state == ACTIVATED || chip_state == SELECTED) { | |
986 | if (!memcmp(receivedCmd+1, anticoll_data, 8)) { | |
987 | modulated_response = resp_csn; | |
988 | modulated_response_size = resp_csn_len; | |
989 | trace_data = csn_data; | |
990 | trace_data_size = sizeof(csn_data); | |
991 | chip_state = SELECTED; | |
992 | } else { | |
993 | chip_state = IDLE; | |
994 | } | |
995 | } else if (chip_state == HALTED) { | |
996 | // RESELECT with CSN | |
997 | if (!memcmp(receivedCmd+1, csn_data, 8)) { | |
998 | modulated_response = resp_csn; | |
999 | modulated_response_size = resp_csn_len; | |
1000 | trace_data = csn_data; | |
1001 | trace_data_size = sizeof(csn_data); | |
1002 | chip_state = SELECTED; | |
1003 | } | |
1004 | } | |
a66f26da | 1005 | |
0ab9002f | 1006 | } else if (receivedCmd[0] == ICLASS_CMD_READ_OR_IDENTIFY && len == 4) { // read block |
1007 | uint16_t blockNo = receivedCmd[1]; | |
5b12974a | 1008 | if (chip_state == SELECTED) { |
1009 | if (simulationMode == ICLASS_SIM_MODE_EXIT_AFTER_MAC) { | |
1010 | // provide defaults for blocks 0 ... 5 | |
1011 | switch (blockNo) { | |
1012 | case 0: // csn (block 00) | |
1013 | modulated_response = resp_csn; | |
1014 | modulated_response_size = resp_csn_len; | |
1015 | trace_data = csn_data; | |
1016 | trace_data_size = sizeof(csn_data); | |
1017 | break; | |
1018 | case 1: // configuration (block 01) | |
1019 | modulated_response = resp_conf; | |
1020 | modulated_response_size = resp_conf_len; | |
ae60ceca | 1021 | trace_data = conf_block; |
1022 | trace_data_size = sizeof(conf_block); | |
5b12974a | 1023 | break; |
1024 | case 2: // e-purse (block 02) | |
1025 | modulated_response = resp_cc; | |
1026 | modulated_response_size = resp_cc_len; | |
1027 | trace_data = card_challenge_data; | |
1028 | trace_data_size = sizeof(card_challenge_data); | |
1029 | // set epurse of sim2,4 attack | |
1030 | if (reader_mac_buf != NULL) { | |
1031 | memcpy(reader_mac_buf, card_challenge_data, 8); | |
1032 | } | |
1033 | break; | |
1034 | case 3: | |
1035 | case 4: // Kd, Kc, always respond with 0xff bytes | |
1036 | modulated_response = resp_ff; | |
1037 | modulated_response_size = resp_ff_len; | |
1038 | trace_data = ff_data; | |
1039 | trace_data_size = sizeof(ff_data); | |
1040 | break; | |
1041 | case 5: // Application Issuer Area (block 05) | |
1042 | modulated_response = resp_aia; | |
1043 | modulated_response_size = resp_aia_len; | |
1044 | trace_data = aia_data; | |
1045 | trace_data_size = sizeof(aia_data); | |
1046 | break; | |
1047 | // default: don't respond | |
1048 | } | |
1049 | } else if (simulationMode == ICLASS_SIM_MODE_FULL) { | |
1050 | if (blockNo == 3 || blockNo == 4) { // Kd, Kc, always respond with 0xff bytes | |
a66f26da | 1051 | modulated_response = resp_ff; |
1052 | modulated_response_size = resp_ff_len; | |
1053 | trace_data = ff_data; | |
1054 | trace_data_size = sizeof(ff_data); | |
5b12974a | 1055 | } else { // use data from emulator memory |
ae60ceca | 1056 | memcpy(data_generic_trace, emulator + current_page*page_size + 8*blockNo, 8); |
5b12974a | 1057 | AppendCrc(data_generic_trace, 8); |
1058 | trace_data = data_generic_trace; | |
1059 | trace_data_size = 10; | |
1060 | CodeIso15693AsTag(trace_data, trace_data_size); | |
1061 | memcpy(data_response, ToSend, ToSendMax); | |
1062 | modulated_response = data_response; | |
1063 | modulated_response_size = ToSendMax; | |
1064 | } | |
0ab9002f | 1065 | } |
5b12974a | 1066 | } |
1067 | ||
1068 | } else if ((receivedCmd[0] == ICLASS_CMD_READCHECK_KD | |
8ddb81a2 | 1069 | || receivedCmd[0] == ICLASS_CMD_READCHECK_KC) && receivedCmd[1] == 0x02 && len == 2) { |
5b12974a | 1070 | // Read e-purse (88 02 || 18 02) |
1071 | if (chip_state == SELECTED) { | |
ae60ceca | 1072 | if(receivedCmd[0] == ICLASS_CMD_READCHECK_KD){ |
1073 | cipher_state = &cipher_state_KD[current_page]; | |
1074 | diversified_key = diversified_key_d; | |
1075 | } else { | |
1076 | cipher_state = &cipher_state_KC[current_page]; | |
1077 | diversified_key = diversified_key_c; | |
1078 | } | |
5b12974a | 1079 | modulated_response = resp_cc; |
1080 | modulated_response_size = resp_cc_len; | |
1081 | trace_data = card_challenge_data; | |
1082 | trace_data_size = sizeof(card_challenge_data); | |
5b12974a | 1083 | } |
1084 | ||
8ddb81a2 | 1085 | } else if ((receivedCmd[0] == ICLASS_CMD_CHECK_KC |
1086 | || receivedCmd[0] == ICLASS_CMD_CHECK_KD) && len == 9) { | |
5b12974a | 1087 | // Reader random and reader MAC!!! |
1088 | if (chip_state == SELECTED) { | |
1089 | if (simulationMode == ICLASS_SIM_MODE_FULL) { | |
1090 | //NR, from reader, is in receivedCmd+1 | |
ae60ceca | 1091 | opt_doTagMAC_2(*cipher_state, receivedCmd+1, data_generic_trace, diversified_key); |
a66f26da | 1092 | trace_data = data_generic_trace; |
5b12974a | 1093 | trace_data_size = 4; |
a66f26da | 1094 | CodeIso15693AsTag(trace_data, trace_data_size); |
1095 | memcpy(data_response, ToSend, ToSendMax); | |
1096 | modulated_response = data_response; | |
1097 | modulated_response_size = ToSendMax; | |
5b12974a | 1098 | //exitLoop = true; |
1099 | } else { // Not fullsim, we don't respond | |
1100 | // We do not know what to answer, so lets keep quiet | |
1101 | if (simulationMode == ICLASS_SIM_MODE_EXIT_AFTER_MAC) { | |
1102 | if (reader_mac_buf != NULL) { | |
1103 | // save NR and MAC for sim 2,4 | |
1104 | memcpy(reader_mac_buf + 8, receivedCmd + 1, 8); | |
1105 | } | |
1106 | exitLoop = true; | |
1107 | } | |
a66f26da | 1108 | } |
0ab9002f | 1109 | } |
1110 | ||
5b12974a | 1111 | } else if (receivedCmd[0] == ICLASS_CMD_HALT && len == 1) { |
1112 | if (chip_state == SELECTED) { | |
1113 | // Reader ends the session | |
ae60ceca | 1114 | modulated_response = resp_sof; |
1115 | modulated_response_size = resp_sof_Len; | |
5b12974a | 1116 | chip_state = HALTED; |
1117 | } | |
0ab9002f | 1118 | |
5b12974a | 1119 | } else if (simulationMode == ICLASS_SIM_MODE_FULL && receivedCmd[0] == ICLASS_CMD_READ4 && len == 4) { // 0x06 |
1120 | //Read 4 blocks | |
1121 | if (chip_state == SELECTED) { | |
ae60ceca | 1122 | uint8_t blockNo = receivedCmd[1]; |
1123 | memcpy(data_generic_trace, emulator + current_page*page_size + blockNo*8, 8 * 4); | |
5b12974a | 1124 | AppendCrc(data_generic_trace, 8 * 4); |
b19caaef | 1125 | trace_data = data_generic_trace; |
5b12974a | 1126 | trace_data_size = 8 * 4 + 2; |
3d2c9c9b | 1127 | CodeIso15693AsTag(trace_data, trace_data_size); |
b67f7ec3 MHS |
1128 | memcpy(data_response, ToSend, ToSendMax); |
1129 | modulated_response = data_response; | |
1130 | modulated_response_size = ToSendMax; | |
ff7bb4ef | 1131 | } |
b67f7ec3 | 1132 | |
5b12974a | 1133 | } else if (receivedCmd[0] == ICLASS_CMD_UPDATE && (len == 12 || len == 14)) { |
0ab9002f | 1134 | // We're expected to respond with the data+crc, exactly what's already in the receivedCmd |
1135 | // receivedCmd is now UPDATE 1b | ADDRESS 1b | DATA 8b | Signature 4b or CRC 2b | |
5b12974a | 1136 | if (chip_state == SELECTED) { |
8ddb81a2 | 1137 | uint8_t blockNo = receivedCmd[1]; |
1138 | if (blockNo == 2) { // update e-purse | |
1139 | memcpy(card_challenge_data, receivedCmd+2, 8); | |
1140 | CodeIso15693AsTag(card_challenge_data, sizeof(card_challenge_data)); | |
1141 | memcpy(resp_cc, ToSend, ToSendMax); | |
1142 | resp_cc_len = ToSendMax; | |
ae60ceca | 1143 | cipher_state_KD[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_d); |
1144 | cipher_state_KC[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_c); | |
8ddb81a2 | 1145 | if (simulationMode == ICLASS_SIM_MODE_FULL) { |
ae60ceca | 1146 | memcpy(emulator + current_page*page_size + 8*2, card_challenge_data, 8); |
8ddb81a2 | 1147 | } |
1148 | } else if (blockNo == 3) { // update Kd | |
ae60ceca | 1149 | for (int i = 0; i < 8; i++) { |
1150 | if (personalization_mode) { | |
1151 | diversified_key_d[i] = receivedCmd[2 + i]; | |
1152 | } else { | |
1153 | diversified_key_d[i] ^= receivedCmd[2 + i]; | |
1154 | } | |
8ddb81a2 | 1155 | } |
ae60ceca | 1156 | cipher_state_KD[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_d); |
8ddb81a2 | 1157 | if (simulationMode == ICLASS_SIM_MODE_FULL) { |
ae60ceca | 1158 | memcpy(emulator + current_page*page_size + 8*3, diversified_key_d, 8); |
8ddb81a2 | 1159 | } |
1160 | } else if (blockNo == 4) { // update Kc | |
ae60ceca | 1161 | for (int i = 0; i < 8; i++) { |
1162 | if (personalization_mode) { | |
1163 | diversified_key_c[i] = receivedCmd[2 + i]; | |
1164 | } else { | |
1165 | diversified_key_c[i] ^= receivedCmd[2 + i]; | |
1166 | } | |
8ddb81a2 | 1167 | } |
ae60ceca | 1168 | cipher_state_KC[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_c); |
8ddb81a2 | 1169 | if (simulationMode == ICLASS_SIM_MODE_FULL) { |
ae60ceca | 1170 | memcpy(emulator + current_page*page_size + 8*4, diversified_key_c, 8); |
8ddb81a2 | 1171 | } |
1172 | } else if (simulationMode == ICLASS_SIM_MODE_FULL) { // update any other data block | |
ae60ceca | 1173 | memcpy(emulator + current_page*page_size + 8*blockNo, receivedCmd+2, 8); |
8ddb81a2 | 1174 | } |
5b12974a | 1175 | memcpy(data_generic_trace, receivedCmd + 2, 8); |
1176 | AppendCrc(data_generic_trace, 8); | |
1177 | trace_data = data_generic_trace; | |
1178 | trace_data_size = 10; | |
1179 | CodeIso15693AsTag(trace_data, trace_data_size); | |
1180 | memcpy(data_response, ToSend, ToSendMax); | |
1181 | modulated_response = data_response; | |
1182 | modulated_response_size = ToSendMax; | |
1183 | } | |
1184 | ||
1185 | } else if (receivedCmd[0] == ICLASS_CMD_PAGESEL && len == 4) { | |
0ab9002f | 1186 | // Pagesel |
ae60ceca | 1187 | // Chips with a single page will not answer to this command |
1188 | // Otherwise, we should answer 8bytes (block) + 2bytes CRC | |
5b12974a | 1189 | if (chip_state == SELECTED) { |
ae60ceca | 1190 | if (simulationMode == ICLASS_SIM_MODE_FULL && max_page > 0) { |
1191 | current_page = receivedCmd[1]; | |
1192 | memcpy(data_generic_trace, emulator + current_page*page_size + 8*1, 8); | |
1193 | memcpy(diversified_key_d, emulator + current_page*page_size + 8*3, 8); | |
1194 | memcpy(diversified_key_c, emulator + current_page*page_size + 8*4, 8); | |
1195 | cipher_state = &cipher_state_KD[current_page]; | |
1196 | personalization_mode = data_generic_trace[7] & 0x80; | |
1197 | AppendCrc(data_generic_trace, 8); | |
1198 | trace_data = data_generic_trace; | |
1199 | trace_data_size = 10; | |
1200 | CodeIso15693AsTag(trace_data, trace_data_size); | |
1201 | memcpy(data_response, ToSend, ToSendMax); | |
1202 | modulated_response = data_response; | |
1203 | modulated_response_size = ToSendMax; | |
1204 | } | |
5b12974a | 1205 | } |
0ab9002f | 1206 | |
e49d31c0 | 1207 | } else if (receivedCmd[0] == 0x26 && len == 5) { |
1208 | // standard ISO15693 INVENTORY command. Ignore. | |
1209 | ||
17505ce2 | 1210 | } else { |
5b12974a | 1211 | // don't know how to handle this command |
3d2c9c9b | 1212 | char debug_message[250]; // should be enough |
1213 | sprintf(debug_message, "Unhandled command (len = %d) received from reader:", len); | |
1214 | for (int i = 0; i < len && strlen(debug_message) < sizeof(debug_message) - 3 - 1; i++) { | |
1215 | sprintf(debug_message + strlen(debug_message), " %02x", receivedCmd[i]); | |
1216 | } | |
1217 | Dbprintf("%s", debug_message); | |
1e262141 | 1218 | // Do not respond |
1e262141 | 1219 | } |
1220 | ||
55eaed8f | 1221 | /** |
a66f26da | 1222 | A legit tag has about 311,5us delay between reader EOT and tag SOF. |
55eaed8f | 1223 | **/ |
17505ce2 | 1224 | if (modulated_response_size > 0) { |
3d2c9c9b | 1225 | uint32_t response_time = reader_eof_time + DELAY_ISO15693_VCD_TO_VICC_SIM - DELAY_ARM_TO_READER_SIM; |
1226 | TransmitTo15693Reader(modulated_response, modulated_response_size, response_time, false); | |
1227 | LogTrace(trace_data, trace_data_size, response_time + DELAY_ARM_TO_READER_SIM, response_time + (modulated_response_size << 6) + DELAY_ARM_TO_READER_SIM, NULL, false); | |
81cd0474 | 1228 | } |
f83cc126 | 1229 | |
81cd0474 | 1230 | } |
1e262141 | 1231 | |
17505ce2 | 1232 | if (buttonPressed) |
f83cc126 MHS |
1233 | { |
1234 | DbpString("Button pressed"); | |
1235 | } | |
f83cc126 | 1236 | return buttonPressed; |
1e262141 | 1237 | } |
1238 | ||
17505ce2 | 1239 | /** |
1240 | * @brief SimulateIClass simulates an iClass card. | |
1241 | * @param arg0 type of simulation | |
1242 | * - 0 uses the first 8 bytes in usb data as CSN | |
1243 | * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified | |
1244 | * in the usb data. This mode collects MAC from the reader, in order to do an offline | |
1245 | * attack on the keys. For more info, see "dismantling iclass" and proxclone.com. | |
1246 | * - Other : Uses the default CSN (031fec8af7ff12e0) | |
1247 | * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only) | |
1248 | * @param arg2 | |
1249 | * @param datain | |
1250 | */ | |
1251 | void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain) { | |
ae60ceca | 1252 | |
1253 | LED_A_ON(); | |
1254 | ||
17505ce2 | 1255 | uint32_t simType = arg0; |
1256 | uint32_t numberOfCSNS = arg1; | |
0ab9002f | 1257 | |
3d2c9c9b | 1258 | // setup hardware for simulation: |
17505ce2 | 1259 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
3d2c9c9b | 1260 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); |
a66f26da | 1261 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION); |
ae60ceca | 1262 | LED_D_OFF(); |
3d2c9c9b | 1263 | FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR); |
1264 | StartCountSspClk(); | |
e3dc1e4c | 1265 | |
17505ce2 | 1266 | // Enable and clear the trace |
1267 | set_tracing(true); | |
1268 | clear_trace(); | |
1269 | //Use the emulator memory for SIM | |
1270 | uint8_t *emulator = BigBuf_get_EM_addr(); | |
e3dc1e4c | 1271 | |
0ab9002f | 1272 | if (simType == ICLASS_SIM_MODE_CSN) { |
17505ce2 | 1273 | // Use the CSN from commandline |
1274 | memcpy(emulator, datain, 8); | |
0ab9002f | 1275 | doIClassSimulation(ICLASS_SIM_MODE_CSN, NULL); |
1276 | } else if (simType == ICLASS_SIM_MODE_CSN_DEFAULT) { | |
17505ce2 | 1277 | //Default CSN |
1278 | uint8_t csn_crc[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 }; | |
1279 | // Use the CSN from commandline | |
1280 | memcpy(emulator, csn_crc, 8); | |
0ab9002f | 1281 | doIClassSimulation(ICLASS_SIM_MODE_CSN, NULL); |
1282 | } else if (simType == ICLASS_SIM_MODE_READER_ATTACK) { | |
17505ce2 | 1283 | uint8_t mac_responses[USB_CMD_DATA_SIZE] = { 0 }; |
1284 | Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS); | |
1285 | // In this mode, a number of csns are within datain. We'll simulate each one, one at a time | |
0ab9002f | 1286 | // in order to collect MAC's from the reader. This can later be used in an offline-attack |
17505ce2 | 1287 | // in order to obtain the keys, as in the "dismantling iclass"-paper. |
0ab9002f | 1288 | int i; |
1289 | for (i = 0; i < numberOfCSNS && i*16+16 <= USB_CMD_DATA_SIZE; i++) { | |
1290 | // The usb data is 512 bytes, fitting 32 responses (8 byte CC + 4 Byte NR + 4 Byte MAC = 16 Byte response). | |
17505ce2 | 1291 | memcpy(emulator, datain+(i*8), 8); |
0ab9002f | 1292 | if (doIClassSimulation(ICLASS_SIM_MODE_EXIT_AFTER_MAC, mac_responses+i*16)) { |
1293 | // Button pressed | |
1294 | break; | |
1e262141 | 1295 | } |
3d2c9c9b | 1296 | Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x", |
1297 | datain[i*8+0], datain[i*8+1], datain[i*8+2], datain[i*8+3], | |
1298 | datain[i*8+4], datain[i*8+5], datain[i*8+6], datain[i*8+7]); | |
1299 | Dbprintf("NR,MAC: %02x %02x %02x %02x %02x %02x %02x %02x", | |
5b12974a | 1300 | mac_responses[i*16+ 8], mac_responses[i*16+ 9], mac_responses[i*16+10], mac_responses[i*16+11], |
1301 | mac_responses[i*16+12], mac_responses[i*16+13], mac_responses[i*16+14], mac_responses[i*16+15]); | |
1302 | SpinDelay(100); // give the reader some time to prepare for next CSN | |
1e262141 | 1303 | } |
0ab9002f | 1304 | cmd_send(CMD_ACK, CMD_SIMULATE_TAG_ICLASS, i, 0, mac_responses, i*16); |
1305 | } else if (simType == ICLASS_SIM_MODE_FULL) { | |
17505ce2 | 1306 | //This is 'full sim' mode, where we use the emulator storage for data. |
0ab9002f | 1307 | doIClassSimulation(ICLASS_SIM_MODE_FULL, NULL); |
17505ce2 | 1308 | } else { |
1309 | // We may want a mode here where we hardcode the csns to use (from proxclone). | |
1310 | // That will speed things up a little, but not required just yet. | |
1311 | Dbprintf("The mode is not implemented, reserved for future use"); | |
1e262141 | 1312 | } |
ae60ceca | 1313 | |
17505ce2 | 1314 | Dbprintf("Done..."); |
1e262141 | 1315 | |
ae60ceca | 1316 | LED_A_OFF(); |
1e262141 | 1317 | } |
1318 | ||
17505ce2 | 1319 | |
1e262141 | 1320 | /// THE READER CODE |
1321 | ||
1322 | //----------------------------------------------------------------------------- | |
1323 | // Transmit the command (to the tag) that was placed in ToSend[]. | |
1324 | //----------------------------------------------------------------------------- | |
17505ce2 | 1325 | static void TransmitIClassCommand(const uint8_t *cmd, int len, int *samples, int *wait) { |
1326 | int c; | |
1327 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD); | |
1328 | AT91C_BASE_SSC->SSC_THR = 0x00; | |
1329 | FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO14443A); | |
1330 | ||
1331 | if (wait) { | |
1332 | if (*wait < 10) *wait = 10; | |
1333 | ||
1334 | for (c = 0; c < *wait;) { | |
1335 | if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
1336 | AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing! | |
1337 | c++; | |
1338 | } | |
1339 | if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
1340 | volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; | |
1341 | (void)r; | |
1342 | } | |
1343 | WDT_HIT(); | |
1344 | } | |
1345 | } | |
1346 | ||
1347 | uint8_t sendbyte; | |
1348 | bool firstpart = true; | |
1349 | c = 0; | |
1350 | for (;;) { | |
1351 | if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
1352 | ||
1353 | // DOUBLE THE SAMPLES! | |
1354 | if (firstpart) { | |
1355 | sendbyte = (cmd[c] & 0xf0) | (cmd[c] >> 4); | |
1356 | } else { | |
1357 | sendbyte = (cmd[c] & 0x0f) | (cmd[c] << 4); | |
1358 | c++; | |
1359 | } | |
1360 | if (sendbyte == 0xff) { | |
1361 | sendbyte = 0xfe; | |
1362 | } | |
1363 | AT91C_BASE_SSC->SSC_THR = sendbyte; | |
1364 | firstpart = !firstpart; | |
1365 | ||
1366 | if (c >= len) { | |
1367 | break; | |
1368 | } | |
1369 | } | |
1370 | if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
1371 | volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; | |
1372 | (void)r; | |
1373 | } | |
1374 | WDT_HIT(); | |
1375 | } | |
1376 | if (samples && wait) *samples = (c + *wait) << 3; | |
1e262141 | 1377 | } |
1378 | ||
1379 | ||
1380 | //----------------------------------------------------------------------------- | |
1381 | // Prepare iClass reader command to send to FPGA | |
1382 | //----------------------------------------------------------------------------- | |
17505ce2 | 1383 | void CodeIClassCommand(const uint8_t *cmd, int len) { |
1384 | int i, j, k; | |
1385 | ||
1386 | ToSendReset(); | |
1387 | ||
1388 | // Start of Communication: 1 out of 4 | |
1389 | ToSend[++ToSendMax] = 0xf0; | |
1390 | ToSend[++ToSendMax] = 0x00; | |
1391 | ToSend[++ToSendMax] = 0x0f; | |
1392 | ToSend[++ToSendMax] = 0x00; | |
1393 | ||
1394 | // Modulate the bytes | |
1395 | for (i = 0; i < len; i++) { | |
1396 | uint8_t b = cmd[i]; | |
1397 | for (j = 0; j < 4; j++) { | |
1398 | for (k = 0; k < 4; k++) { | |
1399 | if (k == (b & 3)) { | |
1400 | ToSend[++ToSendMax] = 0xf0; | |
1401 | } else { | |
1402 | ToSend[++ToSendMax] = 0x00; | |
1403 | } | |
e3dc1e4c | 1404 | } |
17505ce2 | 1405 | b >>= 2; |
1406 | } | |
1407 | } | |
1408 | ||
1409 | // End of Communication | |
1410 | ToSend[++ToSendMax] = 0x00; | |
1411 | ToSend[++ToSendMax] = 0x00; | |
1412 | ToSend[++ToSendMax] = 0xf0; | |
1413 | ToSend[++ToSendMax] = 0x00; | |
1414 | ||
1415 | // Convert from last character reference to length | |
1416 | ToSendMax++; | |
1e262141 | 1417 | } |
1418 | ||
17505ce2 | 1419 | static void ReaderTransmitIClass(uint8_t *frame, int len) { |
6a1f2d82 | 1420 | int wait = 0; |
1421 | int samples = 0; | |
1422 | ||
1423 | // This is tied to other size changes | |
17505ce2 | 1424 | CodeIClassCommand(frame, len); |
6a1f2d82 | 1425 | |
1426 | // Select the card | |
1427 | TransmitIClassCommand(ToSend, ToSendMax, &samples, &wait); | |
17505ce2 | 1428 | if (trigger) |
6a1f2d82 | 1429 | LED_A_ON(); |
1430 | ||
1431 | // Store reader command in buffer | |
d9de20fa | 1432 | uint8_t par[MAX_PARITY_SIZE]; |
1433 | GetParity(frame, len, par); | |
1434 | LogTrace(frame, len, rsamples, rsamples, par, true); | |
1e262141 | 1435 | } |
1436 | ||
1437 | //----------------------------------------------------------------------------- | |
1438 | // Wait a certain time for tag response | |
44964fd1 | 1439 | // If a response is captured return true |
1440 | // If it takes too long return false | |
1e262141 | 1441 | //----------------------------------------------------------------------------- |
17505ce2 | 1442 | static int GetIClassAnswer(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) { |
1443 | //uint8_t *buffer | |
1e262141 | 1444 | // buffer needs to be 512 bytes |
1445 | int c; | |
1446 | ||
1447 | // Set FPGA mode to "reader listen mode", no modulation (listen | |
1448 | // only, since we are receiving, not transmitting). | |
1449 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_LISTEN); | |
1450 | ||
1451 | // Now get the answer from the card | |
1452 | Demod.output = receivedResponse; | |
1453 | Demod.len = 0; | |
1454 | Demod.state = DEMOD_UNSYNCD; | |
1455 | ||
1456 | uint8_t b; | |
1457 | if (elapsed) *elapsed = 0; | |
1458 | ||
44964fd1 | 1459 | bool skip = false; |
1e262141 | 1460 | |
1461 | c = 0; | |
17505ce2 | 1462 | for (;;) { |
1e262141 | 1463 | WDT_HIT(); |
1464 | ||
17505ce2 | 1465 | if (BUTTON_PRESS()) return false; |
1e262141 | 1466 | |
17505ce2 | 1467 | if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { |
1e262141 | 1468 | AT91C_BASE_SSC->SSC_THR = 0x00; // To make use of exact timing of next command from reader!! |
1469 | if (elapsed) (*elapsed)++; | |
1470 | } | |
17505ce2 | 1471 | if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { |
a66f26da | 1472 | if (c < timeout) { |
1473 | c++; | |
1474 | } else { | |
1475 | return false; | |
17505ce2 | 1476 | } |
1e262141 | 1477 | b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; |
1478 | skip = !skip; | |
17505ce2 | 1479 | if (skip) continue; |
1480 | ||
1481 | if (ManchesterDecoding(b & 0x0f)) { | |
1e262141 | 1482 | *samples = c << 3; |
44964fd1 | 1483 | return true; |
1e262141 | 1484 | } |
1485 | } | |
1486 | } | |
1487 | } | |
1488 | ||
17505ce2 | 1489 | static int ReaderReceiveIClass(uint8_t *receivedAnswer) { |
1490 | int samples = 0; | |
1491 | if (!GetIClassAnswer(receivedAnswer, 160, &samples, 0)) { | |
1492 | return false; | |
1493 | } | |
1494 | rsamples += samples; | |
1495 | uint8_t parity[MAX_PARITY_SIZE]; | |
1496 | GetParity(receivedAnswer, Demod.len, parity); | |
1497 | LogTrace(receivedAnswer, Demod.len, rsamples, rsamples, parity, false); | |
1498 | if (samples == 0) return false; | |
1499 | return Demod.len; | |
1e262141 | 1500 | } |
1501 | ||
17505ce2 | 1502 | static void setupIclassReader() { |
1503 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); | |
1504 | // Reset trace buffer | |
1505 | set_tracing(true); | |
1506 | clear_trace(); | |
1507 | ||
1508 | // Setup SSC | |
1509 | FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO14443A); | |
1510 | // Start from off (no field generated) | |
1511 | // Signal field is off with the appropriate LED | |
1512 | LED_D_OFF(); | |
1513 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); | |
1514 | SpinDelay(200); | |
1515 | ||
1516 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
1517 | ||
1518 | // Now give it time to spin up. | |
1519 | // Signal field is on with the appropriate LED | |
1520 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD); | |
1521 | SpinDelay(200); | |
1522 | LED_A_ON(); | |
aa41c605 MHS |
1523 | |
1524 | } | |
1525 | ||
17505ce2 | 1526 | static bool sendCmdGetResponseWithRetries(uint8_t* command, size_t cmdsize, uint8_t* resp, uint8_t expected_size, uint8_t retries) { |
1527 | while (retries-- > 0) { | |
c8dd9b09 | 1528 | ReaderTransmitIClass(command, cmdsize); |
17505ce2 | 1529 | if (expected_size == ReaderReceiveIClass(resp)) { |
aa53efc3 | 1530 | return true; |
c8dd9b09 MHS |
1531 | } |
1532 | } | |
aa53efc3 | 1533 | return false;//Error |
c8dd9b09 MHS |
1534 | } |
1535 | ||
1536 | /** | |
1537 | * @brief Talks to an iclass tag, sends the commands to get CSN and CC. | |
1538 | * @param card_data where the CSN and CC are stored for return | |
1539 | * @return 0 = fail | |
1540 | * 1 = Got CSN | |
1541 | * 2 = Got CSN and CC | |
1542 | */ | |
17505ce2 | 1543 | static uint8_t handshakeIclassTag_ext(uint8_t *card_data, bool use_credit_key) { |
c8dd9b09 | 1544 | static uint8_t act_all[] = { 0x0a }; |
aa53efc3 | 1545 | //static uint8_t identify[] = { 0x0c }; |
1546 | static uint8_t identify[] = { 0x0c, 0x00, 0x73, 0x33 }; | |
c8dd9b09 | 1547 | static uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
aa53efc3 | 1548 | static uint8_t readcheck_cc[]= { 0x88, 0x02 }; |
1549 | if (use_credit_key) | |
1550 | readcheck_cc[0] = 0x18; | |
1551 | else | |
1552 | readcheck_cc[0] = 0x88; | |
caaf9618 | 1553 | |
f71f4deb | 1554 | uint8_t resp[ICLASS_BUFFER_SIZE]; |
c8dd9b09 MHS |
1555 | |
1556 | uint8_t read_status = 0; | |
1557 | ||
1558 | // Send act_all | |
1559 | ReaderTransmitIClass(act_all, 1); | |
1560 | // Card present? | |
17505ce2 | 1561 | if (!ReaderReceiveIClass(resp)) return read_status;//Fail |
c8dd9b09 MHS |
1562 | //Send Identify |
1563 | ReaderTransmitIClass(identify, 1); | |
1564 | //We expect a 10-byte response here, 8 byte anticollision-CSN and 2 byte CRC | |
17505ce2 | 1565 | uint8_t len = ReaderReceiveIClass(resp); |
1566 | if (len != 10) return read_status;//Fail | |
c8dd9b09 MHS |
1567 | |
1568 | //Copy the Anti-collision CSN to our select-packet | |
17505ce2 | 1569 | memcpy(&select[1], resp, 8); |
c8dd9b09 MHS |
1570 | //Select the card |
1571 | ReaderTransmitIClass(select, sizeof(select)); | |
1572 | //We expect a 10-byte response here, 8 byte CSN and 2 byte CRC | |
17505ce2 | 1573 | len = ReaderReceiveIClass(resp); |
1574 | if (len != 10) return read_status;//Fail | |
c8dd9b09 MHS |
1575 | |
1576 | //Success - level 1, we got CSN | |
1577 | //Save CSN in response data | |
17505ce2 | 1578 | memcpy(card_data, resp, 8); |
c8dd9b09 MHS |
1579 | |
1580 | //Flag that we got to at least stage 1, read CSN | |
1581 | read_status = 1; | |
1582 | ||
34e2af02 | 1583 | // Card selected, now read e-purse (cc) (only 8 bytes no CRC) |
c8dd9b09 | 1584 | ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc)); |
17505ce2 | 1585 | if (ReaderReceiveIClass(resp) == 8) { |
c8dd9b09 | 1586 | //Save CC (e-purse) in response data |
17505ce2 | 1587 | memcpy(card_data+8, resp, 8); |
caaf9618 | 1588 | read_status++; |
c8dd9b09 MHS |
1589 | } |
1590 | ||
1591 | return read_status; | |
1592 | } | |
17505ce2 | 1593 | |
1594 | static uint8_t handshakeIclassTag(uint8_t *card_data) { | |
aa53efc3 | 1595 | return handshakeIclassTag_ext(card_data, false); |
1596 | } | |
c8dd9b09 | 1597 | |
caaf9618 | 1598 | |
1e262141 | 1599 | // Reader iClass Anticollission |
1600 | void ReaderIClass(uint8_t arg0) { | |
1e262141 | 1601 | |
17505ce2 | 1602 | uint8_t card_data[6 * 8] = {0}; |
83602aff | 1603 | memset(card_data, 0xFF, sizeof(card_data)); |
17505ce2 | 1604 | uint8_t last_csn[8] = {0,0,0,0,0,0,0,0}; |
34e2af02 | 1605 | uint8_t resp[ICLASS_BUFFER_SIZE]; |
1606 | memset(resp, 0xFF, sizeof(resp)); | |
caaf9618 | 1607 | //Read conf block CRC(0x01) => 0xfa 0x22 |
17505ce2 | 1608 | uint8_t readConf[] = { ICLASS_CMD_READ_OR_IDENTIFY, 0x01, 0xfa, 0x22}; |
34e2af02 | 1609 | //Read App Issuer Area block CRC(0x05) => 0xde 0x64 |
17505ce2 | 1610 | uint8_t readAA[] = { ICLASS_CMD_READ_OR_IDENTIFY, 0x05, 0xde, 0x64}; |
caaf9618 | 1611 | |
6ce0e538 | 1612 | int read_status= 0; |
caaf9618 | 1613 | uint8_t result_status = 0; |
34e2af02 | 1614 | // flag to read until one tag is found successfully |
6ce0e538 | 1615 | bool abort_after_read = arg0 & FLAG_ICLASS_READER_ONLY_ONCE; |
34e2af02 | 1616 | // flag to only try 5 times to find one tag then return |
6ce0e538 | 1617 | bool try_once = arg0 & FLAG_ICLASS_READER_ONE_TRY; |
34e2af02 | 1618 | // if neither abort_after_read nor try_once then continue reading until button pressed. |
1619 | ||
1620 | bool use_credit_key = arg0 & FLAG_ICLASS_READER_CEDITKEY; | |
1621 | // test flags for what blocks to be sure to read | |
1622 | uint8_t flagReadConfig = arg0 & FLAG_ICLASS_READER_CONF; | |
1623 | uint8_t flagReadCC = arg0 & FLAG_ICLASS_READER_CC; | |
1624 | uint8_t flagReadAA = arg0 & FLAG_ICLASS_READER_AA; | |
1625 | ||
1626 | set_tracing(true); | |
6ce0e538 | 1627 | setupIclassReader(); |
1e262141 | 1628 | |
17505ce2 | 1629 | uint16_t tryCnt = 0; |
979c7655 | 1630 | bool userCancelled = BUTTON_PRESS() || usb_poll_validate_length(); |
17505ce2 | 1631 | while (!userCancelled) { |
979c7655 | 1632 | // if only looking for one card try 2 times if we missed it the first time |
17505ce2 | 1633 | if (try_once && tryCnt > 2) { |
1634 | break; | |
1635 | } | |
6ce0e538 | 1636 | tryCnt++; |
17505ce2 | 1637 | if (!get_tracing()) { |
c8dd9b09 MHS |
1638 | DbpString("Trace full"); |
1639 | break; | |
1640 | } | |
1641 | WDT_HIT(); | |
4ab4336a | 1642 | |
aa53efc3 | 1643 | read_status = handshakeIclassTag_ext(card_data, use_credit_key); |
2e9d4b3f | 1644 | |
17505ce2 | 1645 | if (read_status == 0) continue; |
1646 | if (read_status == 1) result_status = FLAG_ICLASS_READER_CSN; | |
1647 | if (read_status == 2) result_status = FLAG_ICLASS_READER_CSN | FLAG_ICLASS_READER_CC; | |
caaf9618 MHS |
1648 | |
1649 | // handshakeIclass returns CSN|CC, but the actual block | |
1650 | // layout is CSN|CONFIG|CC, so here we reorder the data, | |
1651 | // moving CC forward 8 bytes | |
17505ce2 | 1652 | memcpy(card_data+16, card_data+8, 8); |
caaf9618 | 1653 | //Read block 1, config |
17505ce2 | 1654 | if (flagReadConfig) { |
1655 | if (sendCmdGetResponseWithRetries(readConf, sizeof(readConf), resp, 10, 10)) { | |
caaf9618 | 1656 | result_status |= FLAG_ICLASS_READER_CONF; |
34e2af02 | 1657 | memcpy(card_data+8, resp, 8); |
aa53efc3 | 1658 | } else { |
1659 | Dbprintf("Failed to dump config block"); | |
caaf9618 MHS |
1660 | } |
1661 | } | |
c8dd9b09 | 1662 | |
caaf9618 | 1663 | //Read block 5, AA |
17505ce2 | 1664 | if (flagReadAA) { |
1665 | if (sendCmdGetResponseWithRetries(readAA, sizeof(readAA), resp, 10, 10)) { | |
caaf9618 | 1666 | result_status |= FLAG_ICLASS_READER_AA; |
17505ce2 | 1667 | memcpy(card_data + (8*5), resp, 8); |
aa53efc3 | 1668 | } else { |
1669 | //Dbprintf("Failed to dump AA block"); | |
caaf9618 MHS |
1670 | } |
1671 | } | |
1672 | ||
1673 | // 0 : CSN | |
b67f7ec3 | 1674 | // 1 : Configuration |
caaf9618 | 1675 | // 2 : e-purse |
0ab9002f | 1676 | // 3 : kd / debit / aa2 (write-only) |
1677 | // 4 : kc / credit / aa1 (write-only) | |
1678 | // 5 : AIA, Application issuer area | |
1679 | //Then we can 'ship' back the 6 * 8 bytes of data, | |
b67f7ec3 MHS |
1680 | // with 0xFF:s in block 3 and 4. |
1681 | ||
c8dd9b09 | 1682 | LED_B_ON(); |
17505ce2 | 1683 | //Send back to client, but don't bother if we already sent this - |
979c7655 | 1684 | // only useful if looping in arm (not try_once && not abort_after_read) |
17505ce2 | 1685 | if (memcmp(last_csn, card_data, 8) != 0) { |
34e2af02 | 1686 | // If caller requires that we get Conf, CC, AA, continue until we got it |
17505ce2 | 1687 | if ( (result_status ^ FLAG_ICLASS_READER_CSN ^ flagReadConfig ^ flagReadCC ^ flagReadAA) == 0) { |
1688 | cmd_send(CMD_ACK, result_status, 0, 0, card_data, sizeof(card_data)); | |
1689 | if (abort_after_read) { | |
c8dd9b09 | 1690 | LED_A_OFF(); |
979c7655 | 1691 | LED_B_OFF(); |
c8dd9b09 MHS |
1692 | return; |
1693 | } | |
1694 | //Save that we already sent this.... | |
1695 | memcpy(last_csn, card_data, 8); | |
1696 | } | |
caaf9618 | 1697 | |
c8dd9b09 MHS |
1698 | } |
1699 | LED_B_OFF(); | |
979c7655 | 1700 | userCancelled = BUTTON_PRESS() || usb_poll_validate_length(); |
1701 | } | |
1702 | if (userCancelled) { | |
17505ce2 | 1703 | cmd_send(CMD_ACK, 0xFF, 0, 0, card_data, 0); |
979c7655 | 1704 | } else { |
17505ce2 | 1705 | cmd_send(CMD_ACK, 0, 0, 0, card_data, 0); |
6ce0e538 | 1706 | } |
3ac22ee1 | 1707 | LED_A_OFF(); |
cee5a30d | 1708 | } |
1709 | ||
c3963755 | 1710 | void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) { |
c8dd9b09 | 1711 | |
cb29e00a | 1712 | uint8_t card_data[USB_CMD_DATA_SIZE]={0}; |
39d3ce5d MHS |
1713 | uint16_t block_crc_LUT[255] = {0}; |
1714 | ||
17505ce2 | 1715 | //Generate a lookup table for block crc |
1716 | for (int block = 0; block < 255; block++){ | |
1717 | char bl = block; | |
1718 | block_crc_LUT[block] = iclass_crc16(&bl ,1); | |
39d3ce5d MHS |
1719 | } |
1720 | //Dbprintf("Lookup table: %02x %02x %02x" ,block_crc_LUT[0],block_crc_LUT[1],block_crc_LUT[2]); | |
c8dd9b09 | 1721 | |
c3963755 | 1722 | uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
1723 | uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 }; | |
17505ce2 | 1724 | |
1725 | uint16_t crc = 0; | |
1726 | uint8_t cardsize = 0; | |
1727 | uint8_t mem = 0; | |
1728 | ||
1729 | static struct memory_t { | |
1730 | int k16; | |
1731 | int book; | |
1732 | int k2; | |
1733 | int lockauth; | |
1734 | int keyaccess; | |
c3963755 | 1735 | } memory; |
17505ce2 | 1736 | |
f71f4deb | 1737 | uint8_t resp[ICLASS_BUFFER_SIZE]; |
17505ce2 | 1738 | |
1739 | setupIclassReader(); | |
44964fd1 | 1740 | set_tracing(true); |
c3963755 | 1741 | |
17505ce2 | 1742 | while (!BUTTON_PRESS()) { |
1743 | ||
39d3ce5d MHS |
1744 | WDT_HIT(); |
1745 | ||
17505ce2 | 1746 | if (!get_tracing()) { |
c3963755 | 1747 | DbpString("Trace full"); |
1748 | break; | |
1749 | } | |
17505ce2 | 1750 | |
c8dd9b09 | 1751 | uint8_t read_status = handshakeIclassTag(card_data); |
17505ce2 | 1752 | if (read_status < 2) continue; |
c8dd9b09 MHS |
1753 | |
1754 | //for now replay captured auth (as cc not updated) | |
17505ce2 | 1755 | memcpy(check+5, MAC, 4); |
c8dd9b09 | 1756 | |
17505ce2 | 1757 | if (!sendCmdGetResponseWithRetries(check, sizeof(check), resp, 4, 5)) { |
c8dd9b09 MHS |
1758 | Dbprintf("Error: Authentication Fail!"); |
1759 | continue; | |
1760 | } | |
1761 | ||
39d3ce5d MHS |
1762 | //first get configuration block (block 1) |
1763 | crc = block_crc_LUT[1]; | |
17505ce2 | 1764 | read[1] = 1; |
c8dd9b09 MHS |
1765 | read[2] = crc >> 8; |
1766 | read[3] = crc & 0xff; | |
1767 | ||
17505ce2 | 1768 | if (!sendCmdGetResponseWithRetries(read, sizeof(read),resp, 10, 10)) { |
39d3ce5d | 1769 | Dbprintf("Dump config (block 1) failed"); |
c8dd9b09 MHS |
1770 | continue; |
1771 | } | |
1772 | ||
17505ce2 | 1773 | mem = resp[5]; |
1774 | memory.k16 = (mem & 0x80); | |
1775 | memory.book = (mem & 0x20); | |
1776 | memory.k2 = (mem & 0x8); | |
1777 | memory.lockauth = (mem & 0x2); | |
1778 | memory.keyaccess = (mem & 0x1); | |
c8dd9b09 MHS |
1779 | |
1780 | cardsize = memory.k16 ? 255 : 32; | |
1781 | WDT_HIT(); | |
cb29e00a | 1782 | //Set card_data to all zeroes, we'll fill it with data |
17505ce2 | 1783 | memset(card_data, 0x0, USB_CMD_DATA_SIZE); |
1784 | uint8_t failedRead = 0; | |
1785 | uint32_t stored_data_length = 0; | |
c8dd9b09 | 1786 | //then loop around remaining blocks |
17505ce2 | 1787 | for (int block = 0; block < cardsize; block++) { |
1788 | read[1] = block; | |
39d3ce5d | 1789 | crc = block_crc_LUT[block]; |
c8dd9b09 MHS |
1790 | read[2] = crc >> 8; |
1791 | read[3] = crc & 0xff; | |
1792 | ||
17505ce2 | 1793 | if (sendCmdGetResponseWithRetries(read, sizeof(read), resp, 10, 10)) { |
c8dd9b09 | 1794 | Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x", |
17505ce2 | 1795 | block, resp[0], resp[1], resp[2], |
c8dd9b09 MHS |
1796 | resp[3], resp[4], resp[5], |
1797 | resp[6], resp[7]); | |
1798 | ||
cb29e00a | 1799 | //Fill up the buffer |
17505ce2 | 1800 | memcpy(card_data+stored_data_length, resp, 8); |
cb29e00a | 1801 | stored_data_length += 8; |
17505ce2 | 1802 | if (stored_data_length +8 > USB_CMD_DATA_SIZE) { |
1803 | //Time to send this off and start afresh | |
cb29e00a MHS |
1804 | cmd_send(CMD_ACK, |
1805 | stored_data_length,//data length | |
1806 | failedRead,//Failed blocks? | |
1807 | 0,//Not used ATM | |
1808 | card_data, stored_data_length); | |
1809 | //reset | |
1810 | stored_data_length = 0; | |
1811 | failedRead = 0; | |
1812 | } | |
1813 | ||
17505ce2 | 1814 | } else { |
cb29e00a | 1815 | failedRead = 1; |
17505ce2 | 1816 | stored_data_length += 8;//Otherwise, data becomes misaligned |
c8dd9b09 | 1817 | Dbprintf("Failed to dump block %d", block); |
c3963755 | 1818 | } |
1819 | } | |
428d6221 | 1820 | |
cb29e00a | 1821 | //Send off any remaining data |
17505ce2 | 1822 | if (stored_data_length > 0) { |
cb29e00a MHS |
1823 | cmd_send(CMD_ACK, |
1824 | stored_data_length,//data length | |
1825 | failedRead,//Failed blocks? | |
1826 | 0,//Not used ATM | |
17505ce2 | 1827 | card_data, |
1828 | stored_data_length); | |
cb29e00a | 1829 | } |
c8dd9b09 MHS |
1830 | //If we got here, let's break |
1831 | break; | |
c3963755 | 1832 | } |
cb29e00a MHS |
1833 | //Signal end of transmission |
1834 | cmd_send(CMD_ACK, | |
1835 | 0,//data length | |
1836 | 0,//Failed blocks? | |
1837 | 0,//Not used ATM | |
17505ce2 | 1838 | card_data, |
1839 | 0); | |
cb29e00a | 1840 | |
c3963755 | 1841 | LED_A_OFF(); |
1842 | } | |
1843 | ||
17505ce2 | 1844 | void iClass_ReadCheck(uint8_t blockNo, uint8_t keyType) { |
3ac22ee1 | 1845 | uint8_t readcheck[] = { keyType, blockNo }; |
1846 | uint8_t resp[] = {0,0,0,0,0,0,0,0}; | |
1847 | size_t isOK = 0; | |
1848 | isOK = sendCmdGetResponseWithRetries(readcheck, sizeof(readcheck), resp, sizeof(resp), 6); | |
17505ce2 | 1849 | cmd_send(CMD_ACK,isOK, 0, 0, 0, 0); |
3ac22ee1 | 1850 | } |
1851 | ||
aa53efc3 | 1852 | void iClass_Authentication(uint8_t *MAC) { |
8ddb81a2 | 1853 | uint8_t check[] = { ICLASS_CMD_CHECK_KD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
aa53efc3 | 1854 | uint8_t resp[ICLASS_BUFFER_SIZE]; |
17505ce2 | 1855 | memcpy(check+5, MAC, 4); |
aa53efc3 | 1856 | bool isOK; |
3ac22ee1 | 1857 | isOK = sendCmdGetResponseWithRetries(check, sizeof(check), resp, 4, 6); |
17505ce2 | 1858 | cmd_send(CMD_ACK,isOK, 0, 0, 0, 0); |
aa53efc3 | 1859 | } |
17505ce2 | 1860 | |
3ac22ee1 | 1861 | bool iClass_ReadBlock(uint8_t blockNo, uint8_t *readdata) { |
1862 | uint8_t readcmd[] = {ICLASS_CMD_READ_OR_IDENTIFY, blockNo, 0x00, 0x00}; //0x88, 0x00 // can i use 0C? | |
1863 | char bl = blockNo; | |
1864 | uint16_t rdCrc = iclass_crc16(&bl, 1); | |
1865 | readcmd[2] = rdCrc >> 8; | |
1866 | readcmd[3] = rdCrc & 0xff; | |
1867 | uint8_t resp[] = {0,0,0,0,0,0,0,0,0,0}; | |
1868 | bool isOK = false; | |
912a3e94 | 1869 | |
3ac22ee1 | 1870 | //readcmd[1] = blockNo; |
1871 | isOK = sendCmdGetResponseWithRetries(readcmd, sizeof(readcmd), resp, 10, 10); | |
1872 | memcpy(readdata, resp, sizeof(resp)); | |
fecd8202 | 1873 | |
aa53efc3 | 1874 | return isOK; |
1875 | } | |
fecd8202 | 1876 | |
3ac22ee1 | 1877 | void iClass_ReadBlk(uint8_t blockno) { |
1878 | uint8_t readblockdata[] = {0,0,0,0,0,0,0,0,0,0}; | |
aa53efc3 | 1879 | bool isOK = false; |
3ac22ee1 | 1880 | isOK = iClass_ReadBlock(blockno, readblockdata); |
1881 | cmd_send(CMD_ACK, isOK, 0, 0, readblockdata, 8); | |
aa53efc3 | 1882 | } |
fecd8202 | 1883 | |
3ac22ee1 | 1884 | void iClass_Dump(uint8_t blockno, uint8_t numblks) { |
1885 | uint8_t readblockdata[] = {0,0,0,0,0,0,0,0,0,0}; | |
aa53efc3 | 1886 | bool isOK = false; |
1887 | uint8_t blkCnt = 0; | |
fecd8202 | 1888 | |
aa53efc3 | 1889 | BigBuf_free(); |
1890 | uint8_t *dataout = BigBuf_malloc(255*8); | |
17505ce2 | 1891 | if (dataout == NULL) { |
aa53efc3 | 1892 | Dbprintf("out of memory"); |
b8dd1ef6 | 1893 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
1894 | LED_D_OFF(); | |
17505ce2 | 1895 | cmd_send(CMD_ACK, 0, 1, 0, 0, 0); |
b8dd1ef6 | 1896 | LED_A_OFF(); |
aa53efc3 | 1897 | return; |
1898 | } | |
17505ce2 | 1899 | memset(dataout, 0xFF, 255*8); |
fecd8202 | 1900 | |
17505ce2 | 1901 | for ( ; blkCnt < numblks; blkCnt++) { |
3ac22ee1 | 1902 | isOK = iClass_ReadBlock(blockno+blkCnt, readblockdata); |
1903 | if (!isOK || (readblockdata[0] == 0xBB || readblockdata[7] == 0xBB || readblockdata[2] == 0xBB)) { //try again | |
1904 | isOK = iClass_ReadBlock(blockno+blkCnt, readblockdata); | |
aa53efc3 | 1905 | if (!isOK) { |
1906 | Dbprintf("Block %02X failed to read", blkCnt+blockno); | |
1907 | break; | |
1908 | } | |
fecd8202 | 1909 | } |
17505ce2 | 1910 | memcpy(dataout + (blkCnt*8), readblockdata, 8); |
aa53efc3 | 1911 | } |
1912 | //return pointer to dump memory in arg3 | |
17505ce2 | 1913 | cmd_send(CMD_ACK, isOK, blkCnt, BigBuf_max_traceLen(), 0, 0); |
aa53efc3 | 1914 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
1915 | LEDsoff(); | |
1916 | BigBuf_free(); | |
1917 | } | |
1918 | ||
17505ce2 | 1919 | static bool iClass_WriteBlock_ext(uint8_t blockNo, uint8_t *data) { |
671ff89f | 1920 | uint8_t write[] = { ICLASS_CMD_UPDATE, blockNo, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
3ac22ee1 | 1921 | //uint8_t readblockdata[10]; |
1922 | //write[1] = blockNo; | |
aa53efc3 | 1923 | memcpy(write+2, data, 12); // data + mac |
17505ce2 | 1924 | char *wrCmd = (char *)(write+1); |
671ff89f | 1925 | uint16_t wrCrc = iclass_crc16(wrCmd, 13); |
1926 | write[14] = wrCrc >> 8; | |
1927 | write[15] = wrCrc & 0xff; | |
3ac22ee1 | 1928 | uint8_t resp[] = {0,0,0,0,0,0,0,0,0,0}; |
671ff89f | 1929 | bool isOK = false; |
1930 | ||
17505ce2 | 1931 | isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10); |
671ff89f | 1932 | if (isOK) { //if reader responded correctly |
3ac22ee1 | 1933 | //Dbprintf("WriteResp: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",resp[0],resp[1],resp[2],resp[3],resp[4],resp[5],resp[6],resp[7],resp[8],resp[9]); |
17505ce2 | 1934 | if (memcmp(write+2, resp, 8)) { //if response is not equal to write values |
671ff89f | 1935 | if (blockNo != 3 && blockNo != 4) { //if not programming key areas (note key blocks don't get programmed with actual key data it is xor data) |
1936 | //error try again | |
17505ce2 | 1937 | isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10); |
1938 | } | |
fecd8202 | 1939 | } |
fecd8202 | 1940 | } |
aa53efc3 | 1941 | return isOK; |
1942 | } | |
1943 | ||
3ac22ee1 | 1944 | void iClass_WriteBlock(uint8_t blockNo, uint8_t *data) { |
1945 | bool isOK = iClass_WriteBlock_ext(blockNo, data); | |
aa53efc3 | 1946 | if (isOK){ |
17505ce2 | 1947 | Dbprintf("Write block [%02x] successful", blockNo); |
aa53efc3 | 1948 | } else { |
17505ce2 | 1949 | Dbprintf("Write block [%02x] failed", blockNo); |
aa53efc3 | 1950 | } |
17505ce2 | 1951 | cmd_send(CMD_ACK, isOK, 0, 0, 0, 0); |
aa53efc3 | 1952 | } |
1953 | ||
3ac22ee1 | 1954 | void iClass_Clone(uint8_t startblock, uint8_t endblock, uint8_t *data) { |
aa53efc3 | 1955 | int i; |
1956 | int written = 0; | |
1957 | int total_block = (endblock - startblock) + 1; | |
17505ce2 | 1958 | for (i = 0; i < total_block; i++) { |
aa53efc3 | 1959 | // block number |
17505ce2 | 1960 | if (iClass_WriteBlock_ext(i+startblock, data + (i*12))){ |
1961 | Dbprintf("Write block [%02x] successful", i + startblock); | |
aa53efc3 | 1962 | written++; |
1963 | } else { | |
17505ce2 | 1964 | if (iClass_WriteBlock_ext(i+startblock, data + (i*12))){ |
1965 | Dbprintf("Write block [%02x] successful", i + startblock); | |
aa53efc3 | 1966 | written++; |
1967 | } else { | |
17505ce2 | 1968 | Dbprintf("Write block [%02x] failed", i + startblock); |
aa53efc3 | 1969 | } |
1970 | } | |
1971 | } | |
1972 | if (written == total_block) | |
1973 | Dbprintf("Clone complete"); | |
1974 | else | |
17505ce2 | 1975 | Dbprintf("Clone incomplete"); |
aa53efc3 | 1976 | |
17505ce2 | 1977 | cmd_send(CMD_ACK, 1, 0, 0, 0, 0); |
aa53efc3 | 1978 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
1979 | LEDsoff(); | |
1980 | } |