]>
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 | |
15 | // | |
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 | // | |
24 | // + 22279: : 0c 03 e8 01 | |
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 | // | |
34 | // + 21112: : 0c 03 e8 01 | |
35 | // + 85: 0: TAG ff ff ff ff ff ff ff ff ea f5 | |
36 | // | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
f38a1528 | 39 | #include "../include/proxmark3.h" |
cee5a30d | 40 | #include "apps.h" |
41 | #include "util.h" | |
42 | #include "string.h" | |
7e67e42f | 43 | #include "common.h" |
f38a1528 | 44 | #include "cmd.h" |
1e262141 | 45 | // Needed for CRC in emulation mode; |
46 | // same construction as in ISO 14443; | |
47 | // different initial value (CRC_ICLASS) | |
f38a1528 | 48 | #include "../common/iso14443crc.h" |
49 | #include "../common/iso15693tools.h" | |
c3963755 | 50 | #include "iso15693tools.h" |
f38a1528 | 51 | |
cee5a30d | 52 | |
1e262141 | 53 | static int timeout = 4096; |
cee5a30d | 54 | |
cee5a30d | 55 | |
1e262141 | 56 | static int SendIClassAnswer(uint8_t *resp, int respLen, int delay); |
cee5a30d | 57 | |
58 | //----------------------------------------------------------------------------- | |
59 | // The software UART that receives commands from the reader, and its state | |
60 | // variables. | |
61 | //----------------------------------------------------------------------------- | |
62 | static struct { | |
63 | enum { | |
64 | STATE_UNSYNCD, | |
65 | STATE_START_OF_COMMUNICATION, | |
66 | STATE_RECEIVING | |
67 | } state; | |
68 | uint16_t shiftReg; | |
69 | int bitCnt; | |
70 | int byteCnt; | |
71 | int byteCntMax; | |
72 | int posCnt; | |
73 | int nOutOfCnt; | |
74 | int OutOfCnt; | |
75 | int syncBit; | |
1e262141 | 76 | int parityBits; |
77 | int samples; | |
cee5a30d | 78 | int highCnt; |
79 | int swapper; | |
80 | int counter; | |
81 | int bitBuffer; | |
82 | int dropPosition; | |
83 | uint8_t *output; | |
84 | } Uart; | |
85 | ||
1e262141 | 86 | static RAMFUNC int OutOfNDecoding(int bit) |
cee5a30d | 87 | { |
9f693930 | 88 | //int error = 0; |
cee5a30d | 89 | int bitright; |
90 | ||
91 | if(!Uart.bitBuffer) { | |
92 | Uart.bitBuffer = bit ^ 0xFF0; | |
93 | return FALSE; | |
94 | } | |
95 | else { | |
96 | Uart.bitBuffer <<= 4; | |
97 | Uart.bitBuffer ^= bit; | |
98 | } | |
99 | ||
100 | /*if(Uart.swapper) { | |
101 | Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF; | |
102 | Uart.byteCnt++; | |
103 | Uart.swapper = 0; | |
104 | if(Uart.byteCnt > 15) { return TRUE; } | |
105 | } | |
106 | else { | |
107 | Uart.swapper = 1; | |
108 | }*/ | |
109 | ||
110 | if(Uart.state != STATE_UNSYNCD) { | |
111 | Uart.posCnt++; | |
112 | ||
113 | if((Uart.bitBuffer & Uart.syncBit) ^ Uart.syncBit) { | |
114 | bit = 0x00; | |
115 | } | |
116 | else { | |
117 | bit = 0x01; | |
118 | } | |
119 | if(((Uart.bitBuffer << 1) & Uart.syncBit) ^ Uart.syncBit) { | |
120 | bitright = 0x00; | |
121 | } | |
122 | else { | |
123 | bitright = 0x01; | |
124 | } | |
125 | if(bit != bitright) { bit = bitright; } | |
126 | ||
127 | ||
128 | // So, now we only have to deal with *bit*, lets see... | |
129 | if(Uart.posCnt == 1) { | |
130 | // measurement first half bitperiod | |
131 | if(!bit) { | |
132 | // Drop in first half means that we are either seeing | |
133 | // an SOF or an EOF. | |
134 | ||
135 | if(Uart.nOutOfCnt == 1) { | |
136 | // End of Communication | |
137 | Uart.state = STATE_UNSYNCD; | |
138 | Uart.highCnt = 0; | |
139 | if(Uart.byteCnt == 0) { | |
140 | // Its not straightforward to show single EOFs | |
141 | // So just leave it and do not return TRUE | |
142 | Uart.output[Uart.byteCnt] = 0xf0; | |
143 | Uart.byteCnt++; | |
144 | ||
145 | // Calculate the parity bit for the client... | |
146 | Uart.parityBits = 1; | |
147 | } | |
148 | else { | |
149 | return TRUE; | |
150 | } | |
151 | } | |
152 | else if(Uart.state != STATE_START_OF_COMMUNICATION) { | |
153 | // When not part of SOF or EOF, it is an error | |
154 | Uart.state = STATE_UNSYNCD; | |
155 | Uart.highCnt = 0; | |
9f693930 | 156 | //error = 4; |
cee5a30d | 157 | } |
158 | } | |
159 | } | |
160 | else { | |
161 | // measurement second half bitperiod | |
162 | // Count the bitslot we are in... (ISO 15693) | |
163 | Uart.nOutOfCnt++; | |
164 | ||
165 | if(!bit) { | |
166 | if(Uart.dropPosition) { | |
167 | if(Uart.state == STATE_START_OF_COMMUNICATION) { | |
9f693930 | 168 | //error = 1; |
cee5a30d | 169 | } |
170 | else { | |
9f693930 | 171 | //error = 7; |
cee5a30d | 172 | } |
173 | // It is an error if we already have seen a drop in current frame | |
174 | Uart.state = STATE_UNSYNCD; | |
175 | Uart.highCnt = 0; | |
176 | } | |
177 | else { | |
178 | Uart.dropPosition = Uart.nOutOfCnt; | |
179 | } | |
180 | } | |
181 | ||
182 | Uart.posCnt = 0; | |
183 | ||
184 | ||
185 | if(Uart.nOutOfCnt == Uart.OutOfCnt && Uart.OutOfCnt == 4) { | |
186 | Uart.nOutOfCnt = 0; | |
187 | ||
188 | if(Uart.state == STATE_START_OF_COMMUNICATION) { | |
189 | if(Uart.dropPosition == 4) { | |
190 | Uart.state = STATE_RECEIVING; | |
191 | Uart.OutOfCnt = 256; | |
192 | } | |
193 | else if(Uart.dropPosition == 3) { | |
194 | Uart.state = STATE_RECEIVING; | |
195 | Uart.OutOfCnt = 4; | |
196 | //Uart.output[Uart.byteCnt] = 0xdd; | |
197 | //Uart.byteCnt++; | |
198 | } | |
199 | else { | |
200 | Uart.state = STATE_UNSYNCD; | |
201 | Uart.highCnt = 0; | |
202 | } | |
203 | Uart.dropPosition = 0; | |
204 | } | |
205 | else { | |
206 | // RECEIVING DATA | |
207 | // 1 out of 4 | |
208 | if(!Uart.dropPosition) { | |
209 | Uart.state = STATE_UNSYNCD; | |
210 | Uart.highCnt = 0; | |
9f693930 | 211 | //error = 9; |
cee5a30d | 212 | } |
213 | else { | |
214 | Uart.shiftReg >>= 2; | |
215 | ||
216 | // Swap bit order | |
217 | Uart.dropPosition--; | |
218 | //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; } | |
219 | //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; } | |
220 | ||
221 | Uart.shiftReg ^= ((Uart.dropPosition & 0x03) << 6); | |
222 | Uart.bitCnt += 2; | |
223 | Uart.dropPosition = 0; | |
224 | ||
225 | if(Uart.bitCnt == 8) { | |
226 | Uart.output[Uart.byteCnt] = (Uart.shiftReg & 0xff); | |
227 | Uart.byteCnt++; | |
228 | ||
229 | // Calculate the parity bit for the client... | |
230 | Uart.parityBits <<= 1; | |
95e63594 | 231 | //Uart.parityBits ^= OddByteParity[(Uart.shiftReg & 0xff)]; |
232 | Uart.parityBits ^= oddparity(Uart.shiftReg & 0xff); | |
cee5a30d | 233 | |
234 | Uart.bitCnt = 0; | |
235 | Uart.shiftReg = 0; | |
236 | } | |
237 | } | |
238 | } | |
239 | } | |
240 | else if(Uart.nOutOfCnt == Uart.OutOfCnt) { | |
241 | // RECEIVING DATA | |
242 | // 1 out of 256 | |
243 | if(!Uart.dropPosition) { | |
244 | Uart.state = STATE_UNSYNCD; | |
245 | Uart.highCnt = 0; | |
9f693930 | 246 | //error = 3; |
cee5a30d | 247 | } |
248 | else { | |
249 | Uart.dropPosition--; | |
250 | Uart.output[Uart.byteCnt] = (Uart.dropPosition & 0xff); | |
251 | Uart.byteCnt++; | |
252 | ||
253 | // Calculate the parity bit for the client... | |
254 | Uart.parityBits <<= 1; | |
95e63594 | 255 | //Uart.parityBits ^= OddByteParity[(Uart.dropPosition & 0xff)]; |
256 | Uart.parityBits ^= oddparity((Uart.dropPosition & 0xff)); | |
cee5a30d | 257 | |
258 | Uart.bitCnt = 0; | |
259 | Uart.shiftReg = 0; | |
260 | Uart.nOutOfCnt = 0; | |
261 | Uart.dropPosition = 0; | |
262 | } | |
263 | } | |
264 | ||
265 | /*if(error) { | |
266 | Uart.output[Uart.byteCnt] = 0xAA; | |
267 | Uart.byteCnt++; | |
268 | Uart.output[Uart.byteCnt] = error & 0xFF; | |
269 | Uart.byteCnt++; | |
270 | Uart.output[Uart.byteCnt] = 0xAA; | |
271 | Uart.byteCnt++; | |
272 | Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF; | |
273 | Uart.byteCnt++; | |
274 | Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF; | |
275 | Uart.byteCnt++; | |
276 | Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF; | |
277 | Uart.byteCnt++; | |
278 | Uart.output[Uart.byteCnt] = 0xAA; | |
279 | Uart.byteCnt++; | |
280 | return TRUE; | |
281 | }*/ | |
282 | } | |
283 | ||
284 | } | |
285 | else { | |
286 | bit = Uart.bitBuffer & 0xf0; | |
287 | bit >>= 4; | |
288 | bit ^= 0x0F; // drops become 1s ;-) | |
289 | if(bit) { | |
290 | // should have been high or at least (4 * 128) / fc | |
291 | // according to ISO this should be at least (9 * 128 + 20) / fc | |
292 | if(Uart.highCnt == 8) { | |
293 | // we went low, so this could be start of communication | |
294 | // it turns out to be safer to choose a less significant | |
295 | // syncbit... so we check whether the neighbour also represents the drop | |
296 | Uart.posCnt = 1; // apparently we are busy with our first half bit period | |
297 | Uart.syncBit = bit & 8; | |
298 | Uart.samples = 3; | |
299 | if(!Uart.syncBit) { Uart.syncBit = bit & 4; Uart.samples = 2; } | |
300 | else if(bit & 4) { Uart.syncBit = bit & 4; Uart.samples = 2; bit <<= 2; } | |
301 | if(!Uart.syncBit) { Uart.syncBit = bit & 2; Uart.samples = 1; } | |
302 | else if(bit & 2) { Uart.syncBit = bit & 2; Uart.samples = 1; bit <<= 1; } | |
303 | if(!Uart.syncBit) { Uart.syncBit = bit & 1; Uart.samples = 0; | |
304 | if(Uart.syncBit && (Uart.bitBuffer & 8)) { | |
305 | Uart.syncBit = 8; | |
306 | ||
307 | // the first half bit period is expected in next sample | |
308 | Uart.posCnt = 0; | |
309 | Uart.samples = 3; | |
310 | } | |
311 | } | |
312 | else if(bit & 1) { Uart.syncBit = bit & 1; Uart.samples = 0; } | |
313 | ||
314 | Uart.syncBit <<= 4; | |
315 | Uart.state = STATE_START_OF_COMMUNICATION; | |
316 | Uart.bitCnt = 0; | |
317 | Uart.byteCnt = 0; | |
318 | Uart.parityBits = 0; | |
319 | Uart.nOutOfCnt = 0; | |
320 | Uart.OutOfCnt = 4; // Start at 1/4, could switch to 1/256 | |
321 | Uart.dropPosition = 0; | |
322 | Uart.shiftReg = 0; | |
9f693930 | 323 | //error = 0; |
cee5a30d | 324 | } |
325 | else { | |
326 | Uart.highCnt = 0; | |
327 | } | |
328 | } | |
329 | else { | |
330 | if(Uart.highCnt < 8) { | |
331 | Uart.highCnt++; | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
336 | return FALSE; | |
337 | } | |
338 | ||
339 | //============================================================================= | |
1e262141 | 340 | // Manchester |
cee5a30d | 341 | //============================================================================= |
342 | ||
343 | static struct { | |
344 | enum { | |
345 | DEMOD_UNSYNCD, | |
346 | DEMOD_START_OF_COMMUNICATION, | |
347 | DEMOD_START_OF_COMMUNICATION2, | |
348 | DEMOD_START_OF_COMMUNICATION3, | |
349 | DEMOD_SOF_COMPLETE, | |
350 | DEMOD_MANCHESTER_D, | |
351 | DEMOD_MANCHESTER_E, | |
352 | DEMOD_END_OF_COMMUNICATION, | |
353 | DEMOD_END_OF_COMMUNICATION2, | |
354 | DEMOD_MANCHESTER_F, | |
355 | DEMOD_ERROR_WAIT | |
356 | } state; | |
357 | int bitCount; | |
358 | int posCount; | |
359 | int syncBit; | |
360 | int parityBits; | |
361 | uint16_t shiftReg; | |
362 | int buffer; | |
363 | int buffer2; | |
364 | int buffer3; | |
365 | int buff; | |
366 | int samples; | |
367 | int len; | |
368 | enum { | |
369 | SUB_NONE, | |
370 | SUB_FIRST_HALF, | |
371 | SUB_SECOND_HALF, | |
372 | SUB_BOTH | |
373 | } sub; | |
374 | uint8_t *output; | |
375 | } Demod; | |
376 | ||
377 | static RAMFUNC int ManchesterDecoding(int v) | |
378 | { | |
379 | int bit; | |
380 | int modulation; | |
381 | int error = 0; | |
382 | ||
383 | bit = Demod.buffer; | |
384 | Demod.buffer = Demod.buffer2; | |
385 | Demod.buffer2 = Demod.buffer3; | |
386 | Demod.buffer3 = v; | |
387 | ||
388 | if(Demod.buff < 3) { | |
389 | Demod.buff++; | |
390 | return FALSE; | |
391 | } | |
392 | ||
393 | if(Demod.state==DEMOD_UNSYNCD) { | |
394 | Demod.output[Demod.len] = 0xfa; | |
395 | Demod.syncBit = 0; | |
396 | //Demod.samples = 0; | |
397 | Demod.posCount = 1; // This is the first half bit period, so after syncing handle the second part | |
cee5a30d | 398 | |
399 | if(bit & 0x08) { | |
400 | Demod.syncBit = 0x08; | |
401 | } | |
402 | ||
403 | if(bit & 0x04) { | |
404 | if(Demod.syncBit) { | |
405 | bit <<= 4; | |
406 | } | |
407 | Demod.syncBit = 0x04; | |
408 | } | |
409 | ||
410 | if(bit & 0x02) { | |
411 | if(Demod.syncBit) { | |
412 | bit <<= 2; | |
413 | } | |
414 | Demod.syncBit = 0x02; | |
415 | } | |
416 | ||
417 | if(bit & 0x01 && Demod.syncBit) { | |
418 | Demod.syncBit = 0x01; | |
419 | } | |
420 | ||
421 | if(Demod.syncBit) { | |
422 | Demod.len = 0; | |
423 | Demod.state = DEMOD_START_OF_COMMUNICATION; | |
424 | Demod.sub = SUB_FIRST_HALF; | |
425 | Demod.bitCount = 0; | |
426 | Demod.shiftReg = 0; | |
427 | Demod.parityBits = 0; | |
428 | Demod.samples = 0; | |
429 | if(Demod.posCount) { | |
430 | //if(trigger) LED_A_OFF(); // Not useful in this case... | |
431 | switch(Demod.syncBit) { | |
432 | case 0x08: Demod.samples = 3; break; | |
433 | case 0x04: Demod.samples = 2; break; | |
434 | case 0x02: Demod.samples = 1; break; | |
435 | case 0x01: Demod.samples = 0; break; | |
436 | } | |
437 | // SOF must be long burst... otherwise stay unsynced!!! | |
438 | if(!(Demod.buffer & Demod.syncBit) || !(Demod.buffer2 & Demod.syncBit)) { | |
439 | Demod.state = DEMOD_UNSYNCD; | |
440 | } | |
441 | } | |
442 | else { | |
443 | // SOF must be long burst... otherwise stay unsynced!!! | |
444 | if(!(Demod.buffer2 & Demod.syncBit) || !(Demod.buffer3 & Demod.syncBit)) { | |
445 | Demod.state = DEMOD_UNSYNCD; | |
446 | error = 0x88; | |
447 | } | |
448 | ||
449 | } | |
450 | error = 0; | |
451 | ||
452 | } | |
453 | } | |
454 | else { | |
455 | modulation = bit & Demod.syncBit; | |
456 | modulation |= ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit; | |
95e63594 | 457 | |
cee5a30d | 458 | Demod.samples += 4; |
459 | ||
460 | if(Demod.posCount==0) { | |
461 | Demod.posCount = 1; | |
462 | if(modulation) { | |
463 | Demod.sub = SUB_FIRST_HALF; | |
464 | } | |
465 | else { | |
466 | Demod.sub = SUB_NONE; | |
467 | } | |
468 | } | |
469 | else { | |
470 | Demod.posCount = 0; | |
471 | /*(modulation && (Demod.sub == SUB_FIRST_HALF)) { | |
472 | if(Demod.state!=DEMOD_ERROR_WAIT) { | |
473 | Demod.state = DEMOD_ERROR_WAIT; | |
474 | Demod.output[Demod.len] = 0xaa; | |
475 | error = 0x01; | |
476 | } | |
477 | }*/ | |
478 | //else if(modulation) { | |
479 | if(modulation) { | |
480 | if(Demod.sub == SUB_FIRST_HALF) { | |
481 | Demod.sub = SUB_BOTH; | |
482 | } | |
483 | else { | |
484 | Demod.sub = SUB_SECOND_HALF; | |
485 | } | |
486 | } | |
487 | else if(Demod.sub == SUB_NONE) { | |
488 | if(Demod.state == DEMOD_SOF_COMPLETE) { | |
489 | Demod.output[Demod.len] = 0x0f; | |
490 | Demod.len++; | |
491 | Demod.parityBits <<= 1; | |
95e63594 | 492 | //Demod.parityBits ^= OddByteParity[0x0f]; |
493 | Demod.parityBits ^= oddparity(0x0f); | |
cee5a30d | 494 | Demod.state = DEMOD_UNSYNCD; |
495 | // error = 0x0f; | |
496 | return TRUE; | |
497 | } | |
498 | else { | |
499 | Demod.state = DEMOD_ERROR_WAIT; | |
500 | error = 0x33; | |
501 | } | |
502 | /*if(Demod.state!=DEMOD_ERROR_WAIT) { | |
503 | Demod.state = DEMOD_ERROR_WAIT; | |
504 | Demod.output[Demod.len] = 0xaa; | |
505 | error = 0x01; | |
506 | }*/ | |
507 | } | |
508 | ||
509 | switch(Demod.state) { | |
510 | case DEMOD_START_OF_COMMUNICATION: | |
511 | if(Demod.sub == SUB_BOTH) { | |
512 | //Demod.state = DEMOD_MANCHESTER_D; | |
513 | Demod.state = DEMOD_START_OF_COMMUNICATION2; | |
514 | Demod.posCount = 1; | |
515 | Demod.sub = SUB_NONE; | |
516 | } | |
517 | else { | |
518 | Demod.output[Demod.len] = 0xab; | |
519 | Demod.state = DEMOD_ERROR_WAIT; | |
520 | error = 0xd2; | |
521 | } | |
522 | break; | |
523 | case DEMOD_START_OF_COMMUNICATION2: | |
524 | if(Demod.sub == SUB_SECOND_HALF) { | |
525 | Demod.state = DEMOD_START_OF_COMMUNICATION3; | |
526 | } | |
527 | else { | |
528 | Demod.output[Demod.len] = 0xab; | |
529 | Demod.state = DEMOD_ERROR_WAIT; | |
530 | error = 0xd3; | |
531 | } | |
532 | break; | |
533 | case DEMOD_START_OF_COMMUNICATION3: | |
534 | if(Demod.sub == SUB_SECOND_HALF) { | |
535 | // Demod.state = DEMOD_MANCHESTER_D; | |
536 | Demod.state = DEMOD_SOF_COMPLETE; | |
537 | //Demod.output[Demod.len] = Demod.syncBit & 0xFF; | |
538 | //Demod.len++; | |
539 | } | |
540 | else { | |
541 | Demod.output[Demod.len] = 0xab; | |
542 | Demod.state = DEMOD_ERROR_WAIT; | |
543 | error = 0xd4; | |
544 | } | |
545 | break; | |
546 | case DEMOD_SOF_COMPLETE: | |
547 | case DEMOD_MANCHESTER_D: | |
548 | case DEMOD_MANCHESTER_E: | |
549 | // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443) | |
550 | // 00001111 = 1 (0 in 14443) | |
551 | if(Demod.sub == SUB_SECOND_HALF) { // SUB_FIRST_HALF | |
552 | Demod.bitCount++; | |
553 | Demod.shiftReg = (Demod.shiftReg >> 1) ^ 0x100; | |
554 | Demod.state = DEMOD_MANCHESTER_D; | |
555 | } | |
556 | else if(Demod.sub == SUB_FIRST_HALF) { // SUB_SECOND_HALF | |
557 | Demod.bitCount++; | |
558 | Demod.shiftReg >>= 1; | |
559 | Demod.state = DEMOD_MANCHESTER_E; | |
560 | } | |
561 | else if(Demod.sub == SUB_BOTH) { | |
562 | Demod.state = DEMOD_MANCHESTER_F; | |
563 | } | |
564 | else { | |
565 | Demod.state = DEMOD_ERROR_WAIT; | |
566 | error = 0x55; | |
567 | } | |
568 | break; | |
569 | ||
570 | case DEMOD_MANCHESTER_F: | |
571 | // Tag response does not need to be a complete byte! | |
572 | if(Demod.len > 0 || Demod.bitCount > 0) { | |
573 | if(Demod.bitCount > 1) { // was > 0, do not interpret last closing bit, is part of EOF | |
574 | Demod.shiftReg >>= (9 - Demod.bitCount); | |
575 | Demod.output[Demod.len] = Demod.shiftReg & 0xff; | |
576 | Demod.len++; | |
577 | // No parity bit, so just shift a 0 | |
578 | Demod.parityBits <<= 1; | |
579 | } | |
580 | ||
581 | Demod.state = DEMOD_UNSYNCD; | |
582 | return TRUE; | |
583 | } | |
584 | else { | |
585 | Demod.output[Demod.len] = 0xad; | |
586 | Demod.state = DEMOD_ERROR_WAIT; | |
587 | error = 0x03; | |
588 | } | |
589 | break; | |
590 | ||
591 | case DEMOD_ERROR_WAIT: | |
592 | Demod.state = DEMOD_UNSYNCD; | |
593 | break; | |
594 | ||
595 | default: | |
596 | Demod.output[Demod.len] = 0xdd; | |
597 | Demod.state = DEMOD_UNSYNCD; | |
598 | break; | |
599 | } | |
600 | ||
601 | /*if(Demod.bitCount>=9) { | |
602 | Demod.output[Demod.len] = Demod.shiftReg & 0xff; | |
603 | Demod.len++; | |
604 | ||
605 | Demod.parityBits <<= 1; | |
606 | Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01); | |
607 | ||
608 | Demod.bitCount = 0; | |
609 | Demod.shiftReg = 0; | |
610 | }*/ | |
611 | if(Demod.bitCount>=8) { | |
612 | Demod.shiftReg >>= 1; | |
613 | Demod.output[Demod.len] = (Demod.shiftReg & 0xff); | |
614 | Demod.len++; | |
615 | ||
616 | // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT | |
617 | Demod.parityBits <<= 1; | |
95e63594 | 618 | //Demod.parityBits ^= OddByteParity[(Demod.shiftReg & 0xff)]; |
619 | Demod.parityBits ^= oddparity((Demod.shiftReg & 0xff)); | |
cee5a30d | 620 | |
621 | Demod.bitCount = 0; | |
622 | Demod.shiftReg = 0; | |
623 | } | |
624 | ||
625 | if(error) { | |
626 | Demod.output[Demod.len] = 0xBB; | |
627 | Demod.len++; | |
628 | Demod.output[Demod.len] = error & 0xFF; | |
629 | Demod.len++; | |
630 | Demod.output[Demod.len] = 0xBB; | |
631 | Demod.len++; | |
632 | Demod.output[Demod.len] = bit & 0xFF; | |
633 | Demod.len++; | |
634 | Demod.output[Demod.len] = Demod.buffer & 0xFF; | |
635 | Demod.len++; | |
636 | // Look harder ;-) | |
637 | Demod.output[Demod.len] = Demod.buffer2 & 0xFF; | |
638 | Demod.len++; | |
639 | Demod.output[Demod.len] = Demod.syncBit & 0xFF; | |
640 | Demod.len++; | |
641 | Demod.output[Demod.len] = 0xBB; | |
642 | Demod.len++; | |
643 | return TRUE; | |
644 | } | |
645 | ||
646 | } | |
647 | ||
648 | } // end (state != UNSYNCED) | |
649 | ||
650 | return FALSE; | |
651 | } | |
652 | ||
653 | //============================================================================= | |
1e262141 | 654 | // Finally, a `sniffer' for iClass communication |
cee5a30d | 655 | // Both sides of communication! |
656 | //============================================================================= | |
657 | ||
658 | //----------------------------------------------------------------------------- | |
659 | // Record the sequence of commands sent by the reader to the tag, with | |
660 | // triggering so that we start recording at the point that the tag is moved | |
661 | // near the reader. | |
662 | //----------------------------------------------------------------------------- | |
663 | void RAMFUNC SnoopIClass(void) | |
664 | { | |
17cba269 | 665 | |
cee5a30d | 666 | |
667 | // We won't start recording the frames that we acquire until we trigger; | |
668 | // a good trigger condition to get started is probably when we see a | |
669 | // response from the tag. | |
9f693930 | 670 | //int triggered = FALSE; // FALSE to wait first for card |
cee5a30d | 671 | |
672 | // The command (reader -> tag) that we're receiving. | |
673 | // The length of a received command will in most cases be no more than 18 bytes. | |
674 | // So 32 should be enough! | |
17cba269 | 675 | uint8_t *readerToTagCmd = (((uint8_t *)BigBuf) + RECV_CMD_OFFSET); |
cee5a30d | 676 | // The response (tag -> reader) that we're receiving. |
17cba269 | 677 | uint8_t *tagToReaderResponse = (((uint8_t *)BigBuf) + RECV_RES_OFFSET); |
cee5a30d | 678 | |
7cc204bf | 679 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
680 | ||
1e262141 | 681 | // reset traceLen to 0 |
682 | iso14a_set_tracing(TRUE); | |
d19929cb | 683 | iso14a_clear_trace(); |
1e262141 | 684 | iso14a_set_trigger(FALSE); |
cee5a30d | 685 | |
686 | // The DMA buffer, used to stream samples from the FPGA | |
687 | int8_t *dmaBuf = ((int8_t *)BigBuf) + DMA_BUFFER_OFFSET; | |
688 | int lastRxCounter; | |
689 | int8_t *upTo; | |
690 | int smpl; | |
691 | int maxBehindBy = 0; | |
692 | ||
693 | // Count of samples received so far, so that we can include timing | |
694 | // information in the trace buffer. | |
695 | int samples = 0; | |
696 | rsamples = 0; | |
697 | ||
cee5a30d | 698 | // Set up the demodulator for tag -> reader responses. |
17cba269 | 699 | Demod.output = tagToReaderResponse; |
cee5a30d | 700 | Demod.len = 0; |
701 | Demod.state = DEMOD_UNSYNCD; | |
702 | ||
703 | // Setup for the DMA. | |
704 | FpgaSetupSsc(); | |
705 | upTo = dmaBuf; | |
706 | lastRxCounter = DMA_BUFFER_SIZE; | |
707 | FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE); | |
708 | ||
709 | // And the reader -> tag commands | |
710 | memset(&Uart, 0, sizeof(Uart)); | |
17cba269 | 711 | Uart.output = readerToTagCmd; |
cee5a30d | 712 | Uart.byteCntMax = 32; // was 100 (greg)//////////////////////////////////////////////////////////////////////// |
713 | Uart.state = STATE_UNSYNCD; | |
714 | ||
715 | // And put the FPGA in the appropriate mode | |
716 | // Signal field is off with the appropriate LED | |
717 | LED_D_OFF(); | |
718 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_SNIFFER); | |
719 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
720 | ||
81012e67 MHS |
721 | uint32_t time_0 = GetCountSspClk(); |
722 | ||
723 | ||
cee5a30d | 724 | int div = 0; |
725 | //int div2 = 0; | |
726 | int decbyte = 0; | |
727 | int decbyter = 0; | |
728 | ||
729 | // And now we loop, receiving samples. | |
730 | for(;;) { | |
731 | LED_A_ON(); | |
732 | WDT_HIT(); | |
733 | int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & | |
734 | (DMA_BUFFER_SIZE-1); | |
735 | if(behindBy > maxBehindBy) { | |
736 | maxBehindBy = behindBy; | |
737 | if(behindBy > 400) { | |
738 | Dbprintf("blew circular buffer! behindBy=0x%x", behindBy); | |
739 | goto done; | |
740 | } | |
741 | } | |
742 | if(behindBy < 1) continue; | |
743 | ||
744 | LED_A_OFF(); | |
745 | smpl = upTo[0]; | |
746 | upTo++; | |
747 | lastRxCounter -= 1; | |
748 | if(upTo - dmaBuf > DMA_BUFFER_SIZE) { | |
749 | upTo -= DMA_BUFFER_SIZE; | |
750 | lastRxCounter += DMA_BUFFER_SIZE; | |
751 | AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo; | |
752 | AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE; | |
753 | } | |
754 | ||
755 | //samples += 4; | |
756 | samples += 1; | |
cee5a30d | 757 | |
cee5a30d | 758 | if(smpl & 0xF) { |
759 | decbyte ^= (1 << (3 - div)); | |
760 | } | |
cee5a30d | 761 | |
762 | // FOR READER SIDE COMMUMICATION... | |
17cba269 | 763 | |
cee5a30d | 764 | decbyter <<= 2; |
765 | decbyter ^= (smpl & 0x30); | |
766 | ||
767 | div++; | |
768 | ||
769 | if((div + 1) % 2 == 0) { | |
770 | smpl = decbyter; | |
1e262141 | 771 | if(OutOfNDecoding((smpl & 0xF0) >> 4)) { |
cee5a30d | 772 | rsamples = samples - Uart.samples; |
773 | LED_C_ON(); | |
17cba269 | 774 | |
81012e67 | 775 | //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break; |
17cba269 | 776 | //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break; |
81012e67 MHS |
777 | if(tracing) |
778 | { | |
779 | LogTrace(Uart.output,Uart.byteCnt, (GetCountSspClk()-time_0) << 4, Uart.parityBits,TRUE); | |
780 | LogTrace(NULL, 0, (GetCountSspClk()-time_0) << 4, 0, TRUE); | |
781 | } | |
782 | ||
17cba269 MHS |
783 | |
784 | /* And ready to receive another command. */ | |
cee5a30d | 785 | Uart.state = STATE_UNSYNCD; |
786 | /* And also reset the demod code, which might have been */ | |
787 | /* false-triggered by the commands from the reader. */ | |
788 | Demod.state = DEMOD_UNSYNCD; | |
789 | LED_B_OFF(); | |
790 | Uart.byteCnt = 0; | |
791 | } | |
792 | decbyter = 0; | |
793 | } | |
794 | ||
795 | if(div > 3) { | |
796 | smpl = decbyte; | |
797 | if(ManchesterDecoding(smpl & 0x0F)) { | |
798 | rsamples = samples - Demod.samples; | |
799 | LED_B_ON(); | |
800 | ||
81012e67 MHS |
801 | if(tracing) |
802 | { | |
803 | LogTrace(Demod.output,Demod.len, (GetCountSspClk()-time_0) << 4 , Demod.parityBits,FALSE); | |
804 | LogTrace(NULL, 0, (GetCountSspClk()-time_0) << 4, 0, FALSE); | |
805 | } | |
17cba269 | 806 | |
cee5a30d | 807 | |
808 | // And ready to receive another response. | |
809 | memset(&Demod, 0, sizeof(Demod)); | |
17cba269 | 810 | Demod.output = tagToReaderResponse; |
cee5a30d | 811 | Demod.state = DEMOD_UNSYNCD; |
812 | LED_C_OFF(); | |
813 | } | |
814 | ||
815 | div = 0; | |
816 | decbyte = 0x00; | |
817 | } | |
818 | //} | |
819 | ||
820 | if(BUTTON_PRESS()) { | |
821 | DbpString("cancelled_a"); | |
822 | goto done; | |
823 | } | |
824 | } | |
825 | ||
826 | DbpString("COMMAND FINISHED"); | |
827 | ||
828 | Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt); | |
829 | Dbprintf("%x %x %x", Uart.byteCntMax, traceLen, (int)Uart.output[0]); | |
830 | ||
831 | done: | |
832 | AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS; | |
833 | Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt); | |
834 | Dbprintf("%x %x %x", Uart.byteCntMax, traceLen, (int)Uart.output[0]); | |
835 | LED_A_OFF(); | |
836 | LED_B_OFF(); | |
1e262141 | 837 | LED_C_OFF(); |
838 | LED_D_OFF(); | |
839 | } | |
840 | ||
912a3e94 | 841 | void rotateCSN(uint8_t* originalCSN, uint8_t* rotatedCSN) { |
842 | int i; | |
843 | for(i = 0; i < 8; i++) { | |
844 | rotatedCSN[i] = (originalCSN[i] >> 3) | (originalCSN[(i+1)%8] << 5); | |
1e262141 | 845 | } |
846 | } | |
847 | ||
848 | //----------------------------------------------------------------------------- | |
849 | // Wait for commands from reader | |
850 | // Stop when button is pressed | |
851 | // Or return TRUE when command is captured | |
852 | //----------------------------------------------------------------------------- | |
853 | static int GetIClassCommandFromReader(uint8_t *received, int *len, int maxLen) | |
854 | { | |
912a3e94 | 855 | // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen |
1e262141 | 856 | // only, since we are receiving, not transmitting). |
857 | // Signal field is off with the appropriate LED | |
858 | LED_D_OFF(); | |
859 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN); | |
860 | ||
861 | // Now run a `software UART' on the stream of incoming samples. | |
862 | Uart.output = received; | |
863 | Uart.byteCntMax = maxLen; | |
864 | Uart.state = STATE_UNSYNCD; | |
865 | ||
866 | for(;;) { | |
867 | WDT_HIT(); | |
868 | ||
869 | if(BUTTON_PRESS()) return FALSE; | |
870 | ||
871 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
872 | AT91C_BASE_SSC->SSC_THR = 0x00; | |
873 | } | |
874 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
875 | uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; | |
95e63594 | 876 | |
1e262141 | 877 | if(OutOfNDecoding(b & 0x0f)) { |
878 | *len = Uart.byteCnt; | |
879 | return TRUE; | |
880 | } | |
881 | } | |
882 | } | |
883 | } | |
884 | ||
885 | ||
886 | //----------------------------------------------------------------------------- | |
887 | // Prepare tag messages | |
888 | //----------------------------------------------------------------------------- | |
889 | static void CodeIClassTagAnswer(const uint8_t *cmd, int len) | |
890 | { | |
81012e67 MHS |
891 | //So far a dummy implementation, not used |
892 | //int lastProxToAirDuration =0; | |
1e262141 | 893 | int i; |
894 | ||
895 | ToSendReset(); | |
896 | ||
897 | // Send SOF | |
898 | ToSend[++ToSendMax] = 0x00; | |
899 | ToSend[++ToSendMax] = 0x00; | |
900 | ToSend[++ToSendMax] = 0x00; | |
81012e67 | 901 | ToSend[++ToSendMax] = 0xff;//Proxtoair duration starts here |
1e262141 | 902 | ToSend[++ToSendMax] = 0xff; |
903 | ToSend[++ToSendMax] = 0xff; | |
904 | ToSend[++ToSendMax] = 0x00; | |
905 | ToSend[++ToSendMax] = 0xff; | |
906 | ||
907 | for(i = 0; i < len; i++) { | |
908 | int j; | |
909 | uint8_t b = cmd[i]; | |
910 | ||
911 | // Data bits | |
912 | for(j = 0; j < 8; j++) { | |
913 | if(b & 1) { | |
914 | ToSend[++ToSendMax] = 0x00; | |
915 | ToSend[++ToSendMax] = 0xff; | |
916 | } else { | |
917 | ToSend[++ToSendMax] = 0xff; | |
918 | ToSend[++ToSendMax] = 0x00; | |
919 | } | |
920 | b >>= 1; | |
921 | } | |
922 | } | |
923 | ||
924 | // Send EOF | |
925 | ToSend[++ToSendMax] = 0xff; | |
926 | ToSend[++ToSendMax] = 0x00; | |
927 | ToSend[++ToSendMax] = 0xff; | |
928 | ToSend[++ToSendMax] = 0xff; | |
81012e67 | 929 | ToSend[++ToSendMax] = 0xff; |
1e262141 | 930 | ToSend[++ToSendMax] = 0x00; |
931 | ToSend[++ToSendMax] = 0x00; | |
932 | ToSend[++ToSendMax] = 0x00; | |
933 | ||
81012e67 MHS |
934 | //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end |
935 | ||
1e262141 | 936 | // Convert from last byte pos to length |
937 | ToSendMax++; | |
938 | } | |
939 | ||
940 | // Only SOF | |
941 | static void CodeIClassTagSOF() | |
942 | { | |
81012e67 MHS |
943 | //So far a dummy implementation, not used |
944 | //int lastProxToAirDuration =0; | |
1e262141 | 945 | |
81012e67 | 946 | ToSendReset(); |
1e262141 | 947 | // Send SOF |
948 | ToSend[++ToSendMax] = 0x00; | |
949 | ToSend[++ToSendMax] = 0x00; | |
950 | ToSend[++ToSendMax] = 0x00; | |
951 | ToSend[++ToSendMax] = 0xff; | |
952 | ToSend[++ToSendMax] = 0xff; | |
953 | ToSend[++ToSendMax] = 0xff; | |
954 | ToSend[++ToSendMax] = 0x00; | |
955 | ToSend[++ToSendMax] = 0xff; | |
81012e67 MHS |
956 | |
957 | // lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning | |
958 | ||
1e262141 | 959 | |
960 | // Convert from last byte pos to length | |
961 | ToSendMax++; | |
962 | } | |
9f6e9d15 | 963 | int doIClassSimulation(uint8_t csn[], int breakAfterMacReceived, uint8_t *reader_mac_buf); |
ff7bb4ef MHS |
964 | /** |
965 | * @brief SimulateIClass simulates an iClass card. | |
966 | * @param arg0 type of simulation | |
967 | * - 0 uses the first 8 bytes in usb data as CSN | |
968 | * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified | |
969 | * in the usb data. This mode collects MAC from the reader, in order to do an offline | |
970 | * attack on the keys. For more info, see "dismantling iclass" and proxclone.com. | |
971 | * - Other : Uses the default CSN (031fec8af7ff12e0) | |
972 | * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only) | |
973 | * @param arg2 | |
974 | * @param datain | |
975 | */ | |
976 | void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain) | |
1e262141 | 977 | { |
ff7bb4ef MHS |
978 | uint32_t simType = arg0; |
979 | uint32_t numberOfCSNS = arg1; | |
7cc204bf | 980 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
1e262141 | 981 | |
ff7bb4ef MHS |
982 | // Enable and clear the trace |
983 | iso14a_set_tracing(TRUE); | |
984 | iso14a_clear_trace(); | |
81cd0474 | 985 | |
ff7bb4ef | 986 | uint8_t csn_crc[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 }; |
ff7bb4ef MHS |
987 | if(simType == 0) { |
988 | // Use the CSN from commandline | |
989 | memcpy(csn_crc, datain, 8); | |
9f6e9d15 | 990 | doIClassSimulation(csn_crc,0,NULL); |
ff7bb4ef MHS |
991 | }else if(simType == 1) |
992 | { | |
9f6e9d15 | 993 | doIClassSimulation(csn_crc,0,NULL); |
ff7bb4ef MHS |
994 | } |
995 | else if(simType == 2) | |
996 | { | |
9f6e9d15 MHS |
997 | |
998 | uint8_t mac_responses[64] = { 0 }; | |
17cba269 | 999 | Dbprintf("Going into attack mode"); |
ff7bb4ef MHS |
1000 | // In this mode, a number of csns are within datain. We'll simulate each one, one at a time |
1001 | // in order to collect MAC's from the reader. This can later be used in an offlne-attack | |
1002 | // in order to obtain the keys, as in the "dismantling iclass"-paper. | |
9f6e9d15 MHS |
1003 | int i = 0; |
1004 | for( ; i < numberOfCSNS && i*8+8 < USB_CMD_DATA_SIZE; i++) | |
ff7bb4ef MHS |
1005 | { |
1006 | // The usb data is 512 bytes, fitting 65 8-byte CSNs in there. | |
1007 | ||
1008 | memcpy(csn_crc, datain+(i*8), 8); | |
9f6e9d15 | 1009 | if(doIClassSimulation(csn_crc,1,mac_responses)) |
f83cc126 MHS |
1010 | { |
1011 | return; // Button pressed | |
1012 | } | |
ff7bb4ef | 1013 | } |
9f6e9d15 MHS |
1014 | cmd_send(CMD_ACK,CMD_SIMULATE_TAG_ICLASS,i,0,mac_responses,i*8); |
1015 | ||
81012e67 MHS |
1016 | } |
1017 | else{ | |
ff7bb4ef MHS |
1018 | // We may want a mode here where we hardcode the csns to use (from proxclone). |
1019 | // That will speed things up a little, but not required just yet. | |
1020 | Dbprintf("The mode is not implemented, reserved for future use"); | |
1021 | } | |
9f6e9d15 | 1022 | Dbprintf("Done..."); |
ff7bb4ef MHS |
1023 | |
1024 | } | |
1025 | /** | |
1026 | * @brief Does the actual simulation | |
1027 | * @param csn - csn to use | |
1028 | * @param breakAfterMacReceived if true, returns after reader MAC has been received. | |
1029 | */ | |
9f6e9d15 | 1030 | int doIClassSimulation(uint8_t csn[], int breakAfterMacReceived, uint8_t *reader_mac_buf) |
ff7bb4ef | 1031 | { |
81012e67 | 1032 | |
81cd0474 | 1033 | |
1e262141 | 1034 | // CSN followed by two CRC bytes |
1e262141 | 1035 | uint8_t response2[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
ff7bb4ef MHS |
1036 | uint8_t response3[] = { 0,0,0,0,0,0,0,0,0,0}; |
1037 | memcpy(response3,csn,sizeof(response3)); | |
f83cc126 | 1038 | 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 | 1039 | // e-Purse |
1040 | uint8_t response4[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
1e262141 | 1041 | |
1e262141 | 1042 | // Construct anticollision-CSN |
912a3e94 | 1043 | rotateCSN(response3,response2); |
1e262141 | 1044 | |
1045 | // Compute CRC on both CSNs | |
1046 | ComputeCrc14443(CRC_ICLASS, response2, 8, &response2[8], &response2[9]); | |
1047 | ComputeCrc14443(CRC_ICLASS, response3, 8, &response3[8], &response3[9]); | |
1048 | ||
ff7bb4ef | 1049 | int exitLoop = 0; |
1e262141 | 1050 | // Reader 0a |
1051 | // Tag 0f | |
1052 | // Reader 0c | |
1053 | // Tag anticoll. CSN | |
1054 | // Reader 81 anticoll. CSN | |
1055 | // Tag CSN | |
1056 | ||
81cd0474 | 1057 | uint8_t *resp; |
1058 | int respLen; | |
1059 | uint8_t* respdata = NULL; | |
1060 | int respsize = 0; | |
1061 | uint8_t sof = 0x0f; | |
1e262141 | 1062 | |
1063 | // Respond SOF -- takes 8 bytes | |
81cd0474 | 1064 | uint8_t *resp1 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET); |
1e262141 | 1065 | int resp1Len; |
1066 | ||
1067 | // Anticollision CSN (rotated CSN) | |
1068 | // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit) | |
81cd0474 | 1069 | uint8_t *resp2 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 10); |
1e262141 | 1070 | int resp2Len; |
1071 | ||
1072 | // CSN | |
1073 | // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit) | |
81cd0474 | 1074 | uint8_t *resp3 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 190); |
912a3e94 | 1075 | int resp3Len; |
1e262141 | 1076 | |
1077 | // e-Purse | |
1078 | // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit) | |
81cd0474 | 1079 | uint8_t *resp4 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 370); |
1e262141 | 1080 | int resp4Len; |
1081 | ||
1082 | // + 1720.. | |
ff7bb4ef | 1083 | uint8_t *receivedCmd = (((uint8_t *)BigBuf) + RECV_CMD_OFFSET); |
81cd0474 | 1084 | memset(receivedCmd, 0x44, RECV_CMD_SIZE); |
1e262141 | 1085 | int len; |
1086 | ||
1e262141 | 1087 | // Prepare card messages |
1088 | ToSendMax = 0; | |
1089 | ||
1090 | // First card answer: SOF | |
1091 | CodeIClassTagSOF(); | |
1092 | memcpy(resp1, ToSend, ToSendMax); resp1Len = ToSendMax; | |
1093 | ||
1094 | // Anticollision CSN | |
1095 | CodeIClassTagAnswer(response2, sizeof(response2)); | |
1096 | memcpy(resp2, ToSend, ToSendMax); resp2Len = ToSendMax; | |
1097 | ||
1098 | // CSN | |
1099 | CodeIClassTagAnswer(response3, sizeof(response3)); | |
912a3e94 | 1100 | memcpy(resp3, ToSend, ToSendMax); resp3Len = ToSendMax; |
1e262141 | 1101 | |
1102 | // e-Purse | |
1103 | CodeIClassTagAnswer(response4, sizeof(response4)); | |
1104 | memcpy(resp4, ToSend, ToSendMax); resp4Len = ToSendMax; | |
1105 | ||
e3dc1e4c MHS |
1106 | |
1107 | // Start from off (no field generated) | |
fa541aca MHS |
1108 | //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
1109 | //SpinDelay(200); | |
1110 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN); | |
1111 | SpinDelay(100); | |
1112 | StartCountSspClk(); | |
1e262141 | 1113 | // We need to listen to the high-frequency, peak-detected path. |
1114 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
1115 | FpgaSetupSsc(); | |
1116 | ||
1117 | // To control where we are in the protocol | |
1e262141 | 1118 | int cmdsRecvd = 0; |
81012e67 MHS |
1119 | uint32_t time_0 = GetCountSspClk(); |
1120 | uint32_t t2r_time =0; | |
1121 | uint32_t r2t_time =0; | |
912a3e94 | 1122 | |
1e262141 | 1123 | LED_A_ON(); |
f83cc126 | 1124 | bool buttonPressed = false; |
9f6e9d15 MHS |
1125 | |
1126 | /** Hack for testing | |
1127 | memcpy(reader_mac_buf,csn,8); | |
1128 | exitLoop = true; | |
1129 | end hack **/ | |
1130 | ||
ff7bb4ef | 1131 | while(!exitLoop) { |
81012e67 | 1132 | |
1e262141 | 1133 | LED_B_OFF(); |
e3dc1e4c MHS |
1134 | //Signal tracer |
1135 | // Can be used to get a trigger for an oscilloscope.. | |
1136 | LED_C_OFF(); | |
1137 | ||
1e262141 | 1138 | if(!GetIClassCommandFromReader(receivedCmd, &len, 100)) { |
f83cc126 | 1139 | buttonPressed = true; |
1e262141 | 1140 | break; |
81cd0474 | 1141 | } |
81012e67 | 1142 | r2t_time = GetCountSspClk(); |
e3dc1e4c MHS |
1143 | //Signal tracer |
1144 | LED_C_ON(); | |
1e262141 | 1145 | |
81cd0474 | 1146 | // Okay, look at the command now. |
f83cc126 | 1147 | if(receivedCmd[0] == 0x0a ) { |
1e262141 | 1148 | // Reader in anticollission phase |
1149 | resp = resp1; respLen = resp1Len; //order = 1; | |
81cd0474 | 1150 | respdata = &sof; |
1151 | respsize = sizeof(sof); | |
1e262141 | 1152 | } else if(receivedCmd[0] == 0x0c) { |
1153 | // Reader asks for anticollission CSN | |
1154 | resp = resp2; respLen = resp2Len; //order = 2; | |
81cd0474 | 1155 | respdata = response2; |
1156 | respsize = sizeof(response2); | |
1e262141 | 1157 | //DbpString("Reader requests anticollission CSN:"); |
1158 | } else if(receivedCmd[0] == 0x81) { | |
1159 | // Reader selects anticollission CSN. | |
1160 | // Tag sends the corresponding real CSN | |
912a3e94 | 1161 | resp = resp3; respLen = resp3Len; //order = 3; |
81cd0474 | 1162 | respdata = response3; |
1163 | respsize = sizeof(response3); | |
1e262141 | 1164 | //DbpString("Reader selects anticollission CSN:"); |
1165 | } else if(receivedCmd[0] == 0x88) { | |
1166 | // Read e-purse (88 02) | |
1167 | resp = resp4; respLen = resp4Len; //order = 4; | |
81cd0474 | 1168 | respdata = response4; |
1169 | respsize = sizeof(response4); | |
1e262141 | 1170 | LED_B_ON(); |
1171 | } else if(receivedCmd[0] == 0x05) { | |
1172 | // Reader random and reader MAC!!! | |
1e262141 | 1173 | // Do not respond |
f38a1528 | 1174 | // We do not know what to answer, so lets keep quiet |
1e262141 | 1175 | resp = resp1; respLen = 0; //order = 5; |
81cd0474 | 1176 | respdata = NULL; |
1177 | respsize = 0; | |
ff7bb4ef | 1178 | if (breakAfterMacReceived){ |
ff7bb4ef | 1179 | // dbprintf:ing ... |
9f6e9d15 | 1180 | Dbprintf("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]); |
ff7bb4ef MHS |
1181 | Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len, |
1182 | receivedCmd[0], receivedCmd[1], receivedCmd[2], | |
1183 | receivedCmd[3], receivedCmd[4], receivedCmd[5], | |
1184 | receivedCmd[6], receivedCmd[7], receivedCmd[8]); | |
9f6e9d15 MHS |
1185 | if (reader_mac_buf != NULL) |
1186 | { | |
1187 | memcpy(reader_mac_buf,receivedCmd+1,8); | |
1188 | } | |
ff7bb4ef MHS |
1189 | exitLoop = true; |
1190 | } | |
1e262141 | 1191 | } else if(receivedCmd[0] == 0x00 && len == 1) { |
1192 | // Reader ends the session | |
1193 | resp = resp1; respLen = 0; //order = 0; | |
81cd0474 | 1194 | respdata = NULL; |
1195 | respsize = 0; | |
1196 | } else { | |
17cba269 | 1197 | //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44 |
1e262141 | 1198 | // Never seen this command before |
1199 | Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x", | |
1200 | len, | |
1201 | receivedCmd[0], receivedCmd[1], receivedCmd[2], | |
1202 | receivedCmd[3], receivedCmd[4], receivedCmd[5], | |
1203 | receivedCmd[6], receivedCmd[7], receivedCmd[8]); | |
1204 | // Do not respond | |
1205 | resp = resp1; respLen = 0; //order = 0; | |
81cd0474 | 1206 | respdata = NULL; |
1207 | respsize = 0; | |
1e262141 | 1208 | } |
1209 | ||
81012e67 MHS |
1210 | if(cmdsRecvd > 100) { |
1211 | //DbpString("100 commands later..."); | |
9f6e9d15 | 1212 | //break; |
1e262141 | 1213 | } |
1214 | else { | |
1215 | cmdsRecvd++; | |
1216 | } | |
1217 | ||
81cd0474 | 1218 | if(respLen > 0) { |
1219 | SendIClassAnswer(resp, respLen, 21); | |
81012e67 | 1220 | t2r_time = GetCountSspClk(); |
81cd0474 | 1221 | } |
f83cc126 | 1222 | |
81cd0474 | 1223 | if (tracing) { |
81012e67 MHS |
1224 | LogTrace(receivedCmd,len, (r2t_time-time_0)<< 4, Uart.parityBits,TRUE); |
1225 | LogTrace(NULL,0, (r2t_time-time_0) << 4, 0,TRUE); | |
17cba269 MHS |
1226 | |
1227 | if (respdata != NULL) { | |
81012e67 MHS |
1228 | LogTrace(respdata,respsize, (t2r_time-time_0) << 4,SwapBits(GetParity(respdata,respsize),respsize),FALSE); |
1229 | LogTrace(NULL,0, (t2r_time-time_0) << 4,0,FALSE); | |
1230 | ||
1231 | ||
17cba269 | 1232 | } |
81012e67 MHS |
1233 | if(!tracing) { |
1234 | DbpString("Trace full"); | |
1235 | //break; | |
1236 | } | |
1237 | ||
81cd0474 | 1238 | } |
81cd0474 | 1239 | memset(receivedCmd, 0x44, RECV_CMD_SIZE); |
1240 | } | |
1e262141 | 1241 | |
9f6e9d15 | 1242 | //Dbprintf("%x", cmdsRecvd); |
1e262141 | 1243 | LED_A_OFF(); |
1244 | LED_B_OFF(); | |
f83cc126 MHS |
1245 | if(buttonPressed) |
1246 | { | |
1247 | DbpString("Button pressed"); | |
1248 | } | |
f83cc126 | 1249 | return buttonPressed; |
1e262141 | 1250 | } |
1251 | ||
1252 | static int SendIClassAnswer(uint8_t *resp, int respLen, int delay) | |
1253 | { | |
e3dc1e4c | 1254 | int i = 0, d=0;//, u = 0, d = 0; |
1e262141 | 1255 | uint8_t b = 0; |
e3dc1e4c MHS |
1256 | |
1257 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR|FPGA_HF_SIMULATOR_MODULATE_424K); | |
1258 | ||
1e262141 | 1259 | AT91C_BASE_SSC->SSC_THR = 0x00; |
1260 | FpgaSetupSsc(); | |
e3dc1e4c MHS |
1261 | while(!BUTTON_PRESS()) { |
1262 | if((AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)){ | |
1263 | b = AT91C_BASE_SSC->SSC_RHR; (void) b; | |
1e262141 | 1264 | } |
e3dc1e4c MHS |
1265 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)){ |
1266 | b = 0x00; | |
1e262141 | 1267 | if(d < delay) { |
1e262141 | 1268 | d++; |
1269 | } | |
e3dc1e4c MHS |
1270 | else { |
1271 | if( i < respLen){ | |
1272 | b = resp[i]; | |
1273 | //Hack | |
1274 | //b = 0xAC; | |
1275 | } | |
1276 | i++; | |
1e262141 | 1277 | } |
1278 | AT91C_BASE_SSC->SSC_THR = b; | |
1e262141 | 1279 | } |
e3dc1e4c MHS |
1280 | |
1281 | if (i > respLen +4) break; | |
1e262141 | 1282 | } |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | /// THE READER CODE | |
1288 | ||
1289 | //----------------------------------------------------------------------------- | |
1290 | // Transmit the command (to the tag) that was placed in ToSend[]. | |
1291 | //----------------------------------------------------------------------------- | |
1292 | static void TransmitIClassCommand(const uint8_t *cmd, int len, int *samples, int *wait) | |
1293 | { | |
1294 | int c; | |
1e262141 | 1295 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD); |
1296 | AT91C_BASE_SSC->SSC_THR = 0x00; | |
1297 | FpgaSetupSsc(); | |
1298 | ||
1299 | if (wait) | |
1300 | if(*wait < 10) | |
1301 | *wait = 10; | |
1302 | ||
1303 | for(c = 0; c < *wait;) { | |
1304 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
1305 | AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing! | |
1306 | c++; | |
1307 | } | |
1308 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
1309 | volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; | |
1310 | (void)r; | |
1311 | } | |
1312 | WDT_HIT(); | |
1313 | } | |
1314 | ||
1315 | uint8_t sendbyte; | |
1316 | bool firstpart = TRUE; | |
1317 | c = 0; | |
1318 | for(;;) { | |
1319 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
1320 | ||
1321 | // DOUBLE THE SAMPLES! | |
1322 | if(firstpart) { | |
1323 | sendbyte = (cmd[c] & 0xf0) | (cmd[c] >> 4); | |
1324 | } | |
1325 | else { | |
1326 | sendbyte = (cmd[c] & 0x0f) | (cmd[c] << 4); | |
1327 | c++; | |
1328 | } | |
1329 | if(sendbyte == 0xff) { | |
1330 | sendbyte = 0xfe; | |
1331 | } | |
1332 | AT91C_BASE_SSC->SSC_THR = sendbyte; | |
1333 | firstpart = !firstpart; | |
1334 | ||
1335 | if(c >= len) { | |
1336 | break; | |
1337 | } | |
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 | if (samples) *samples = (c + *wait) << 3; | |
1346 | } | |
1347 | ||
1348 | ||
1349 | //----------------------------------------------------------------------------- | |
1350 | // Prepare iClass reader command to send to FPGA | |
1351 | //----------------------------------------------------------------------------- | |
1352 | void CodeIClassCommand(const uint8_t * cmd, int len) | |
1353 | { | |
1354 | int i, j, k; | |
1355 | uint8_t b; | |
1356 | ||
1357 | ToSendReset(); | |
1358 | ||
1359 | // Start of Communication: 1 out of 4 | |
1360 | ToSend[++ToSendMax] = 0xf0; | |
1361 | ToSend[++ToSendMax] = 0x00; | |
1362 | ToSend[++ToSendMax] = 0x0f; | |
1363 | ToSend[++ToSendMax] = 0x00; | |
1364 | ||
1365 | // Modulate the bytes | |
1366 | for (i = 0; i < len; i++) { | |
1367 | b = cmd[i]; | |
1368 | for(j = 0; j < 4; j++) { | |
1369 | for(k = 0; k < 4; k++) { | |
e3dc1e4c MHS |
1370 | if(k == (b & 3)) { |
1371 | ToSend[++ToSendMax] = 0x0f; | |
1372 | } | |
1373 | else { | |
1374 | ToSend[++ToSendMax] = 0x00; | |
1375 | } | |
1e262141 | 1376 | } |
1377 | b >>= 2; | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | // End of Communication | |
1382 | ToSend[++ToSendMax] = 0x00; | |
1383 | ToSend[++ToSendMax] = 0x00; | |
1384 | ToSend[++ToSendMax] = 0xf0; | |
1385 | ToSend[++ToSendMax] = 0x00; | |
1386 | ||
1387 | // Convert from last character reference to length | |
1388 | ToSendMax++; | |
1389 | } | |
1390 | ||
1391 | void ReaderTransmitIClass(uint8_t* frame, int len) | |
1392 | { | |
1393 | int wait = 0; | |
1394 | int samples = 0; | |
1395 | int par = 0; | |
1396 | ||
1397 | // This is tied to other size changes | |
1e262141 | 1398 | CodeIClassCommand(frame,len); |
1399 | ||
1400 | // Select the card | |
1401 | TransmitIClassCommand(ToSend, ToSendMax, &samples, &wait); | |
1402 | if(trigger) | |
1403 | LED_A_ON(); | |
1404 | ||
1405 | // Store reader command in buffer | |
7bc95e2e | 1406 | if (tracing) LogTrace(frame,len,rsamples,par,TRUE); |
1e262141 | 1407 | } |
1408 | ||
1409 | //----------------------------------------------------------------------------- | |
1410 | // Wait a certain time for tag response | |
1411 | // If a response is captured return TRUE | |
1412 | // If it takes too long return FALSE | |
1413 | //----------------------------------------------------------------------------- | |
1414 | static int GetIClassAnswer(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) //uint8_t *buffer | |
1415 | { | |
1416 | // buffer needs to be 512 bytes | |
1417 | int c; | |
1418 | ||
1419 | // Set FPGA mode to "reader listen mode", no modulation (listen | |
1420 | // only, since we are receiving, not transmitting). | |
1421 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_LISTEN); | |
1422 | ||
1423 | // Now get the answer from the card | |
1424 | Demod.output = receivedResponse; | |
1425 | Demod.len = 0; | |
1426 | Demod.state = DEMOD_UNSYNCD; | |
1427 | ||
1428 | uint8_t b; | |
1429 | if (elapsed) *elapsed = 0; | |
1430 | ||
1431 | bool skip = FALSE; | |
1432 | ||
1433 | c = 0; | |
1434 | for(;;) { | |
1435 | WDT_HIT(); | |
1436 | ||
95e63594 | 1437 | if(BUTTON_PRESS()) return FALSE; |
1e262141 | 1438 | |
1439 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
1440 | AT91C_BASE_SSC->SSC_THR = 0x00; // To make use of exact timing of next command from reader!! | |
1441 | if (elapsed) (*elapsed)++; | |
1442 | } | |
1443 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
1444 | if(c < timeout) { c++; } else { return FALSE; } | |
1445 | b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; | |
1446 | skip = !skip; | |
1447 | if(skip) continue; | |
95e63594 | 1448 | |
1e262141 | 1449 | if(ManchesterDecoding(b & 0x0f)) { |
1450 | *samples = c << 3; | |
1451 | return TRUE; | |
1452 | } | |
1453 | } | |
1454 | } | |
1455 | } | |
1456 | ||
1457 | int ReaderReceiveIClass(uint8_t* receivedAnswer) | |
1458 | { | |
1459 | int samples = 0; | |
1460 | if (!GetIClassAnswer(receivedAnswer,160,&samples,0)) return FALSE; | |
7bc95e2e | 1461 | rsamples += samples; |
1462 | if (tracing) LogTrace(receivedAnswer,Demod.len,rsamples,Demod.parityBits,FALSE); | |
1e262141 | 1463 | if(samples == 0) return FALSE; |
1464 | return Demod.len; | |
1465 | } | |
1466 | ||
f38a1528 | 1467 | void setupIclassReader() |
1468 | { | |
1469 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); | |
1470 | // Reset trace buffer | |
1471 | iso14a_set_tracing(TRUE); | |
1472 | iso14a_clear_trace(); | |
1473 | ||
1474 | // Setup SSC | |
1475 | FpgaSetupSsc(); | |
1476 | // Start from off (no field generated) | |
1477 | // Signal field is off with the appropriate LED | |
1478 | LED_D_OFF(); | |
1479 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); | |
1480 | SpinDelay(200); | |
1481 | ||
1482 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
1483 | ||
1484 | // Now give it time to spin up. | |
1485 | // Signal field is on with the appropriate LED | |
1486 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD); | |
1487 | SpinDelay(200); | |
1488 | LED_A_ON(); | |
1489 | ||
1490 | } | |
1491 | ||
1e262141 | 1492 | // Reader iClass Anticollission |
1493 | void ReaderIClass(uint8_t arg0) { | |
1e262141 | 1494 | uint8_t act_all[] = { 0x0a }; |
1495 | uint8_t identify[] = { 0x0c }; | |
4ab4336a | 1496 | uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
f38a1528 | 1497 | uint8_t readcheck_cc[]= { 0x88, 0x02 }; |
1498 | ||
1499 | uint8_t card_data[24]={0}; | |
1500 | uint8_t last_csn[8]={0}; | |
1e262141 | 1501 | |
1502 | uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes | |
1503 | ||
f38a1528 | 1504 | int read_status= 0; |
1505 | bool abort_after_read = arg0 & FLAG_ICLASS_READER_ONLY_ONCE; | |
1506 | ||
1507 | setupIclassReader(); | |
1508 | ||
1509 | size_t datasize = 0; | |
1510 | while(!BUTTON_PRESS()) | |
1511 | { | |
1512 | WDT_HIT(); | |
1513 | ||
aa41c605 MHS |
1514 | // Send act_all |
1515 | ReaderTransmitIClass(act_all, 1); | |
1516 | // Card present? | |
1517 | if(ReaderReceiveIClass(resp)) { | |
f38a1528 | 1518 | |
aa41c605 | 1519 | ReaderTransmitIClass(identify, 1); |
f38a1528 | 1520 | |
aa41c605 | 1521 | if(ReaderReceiveIClass(resp) == 10) { |
f38a1528 | 1522 | //Copy the Anti-collision CSN to our select-packet |
aa41c605 | 1523 | memcpy(&select[1],resp,8); |
f38a1528 | 1524 | //Dbprintf("Anti-collision CSN: %02x %02x %02x %02x %02x %02x %02x %02x",resp[0], resp[1], resp[2], |
1525 | // resp[3], resp[4], resp[5], | |
1526 | // resp[6], resp[7]); | |
1527 | //Select the card | |
aa41c605 | 1528 | ReaderTransmitIClass(select, sizeof(select)); |
f38a1528 | 1529 | |
aa41c605 | 1530 | if(ReaderReceiveIClass(resp) == 10) { |
f38a1528 | 1531 | //Save CSN in response data |
1532 | memcpy(card_data,resp,8); | |
1533 | datasize += 8; | |
1534 | //Flag that we got to at least stage 1, read CSN | |
1535 | read_status = 1; | |
1536 | ||
1537 | // Card selected | |
1538 | //Dbprintf("Readcheck on Sector 2"); | |
1539 | ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc)); | |
1540 | if(ReaderReceiveIClass(resp) == 8) { | |
1541 | //Save CC (e-purse) in response data | |
1542 | memcpy(card_data+8,resp,8); | |
1543 | datasize += 8; | |
1544 | //Got both | |
1545 | read_status = 2; | |
1546 | } | |
1547 | ||
1548 | LED_B_ON(); | |
1549 | //Send back to client, but don't bother if we already sent this | |
1550 | if(memcmp(last_csn, card_data, 8) != 0) | |
1551 | cmd_send(CMD_ACK,read_status,0,0,card_data,datasize); | |
1552 | ||
1553 | //Save that we already sent this.... | |
1554 | if(read_status == 2) | |
1555 | memcpy(last_csn, card_data, 8); | |
1556 | ||
1557 | LED_B_OFF(); | |
1558 | ||
1559 | if(abort_after_read) break; | |
1560 | } | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | if(traceLen > TRACE_SIZE) { | |
1565 | DbpString("Trace full"); | |
1566 | break; | |
1567 | } | |
1568 | } | |
aa41c605 | 1569 | LED_A_OFF(); |
f38a1528 | 1570 | } |
1571 | ||
1572 | void ReaderIClass_Replay(uint8_t arg0, uint8_t *MAC) { | |
1573 | uint8_t act_all[] = { 0x0a }; | |
1574 | uint8_t identify[] = { 0x0c }; | |
1575 | uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
1576 | uint8_t readcheck_cc[]= { 0x88, 0x02 }; | |
1577 | uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
1578 | uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 }; | |
1579 | ||
fecd8202 | 1580 | uint16_t crc = 0; |
f38a1528 | 1581 | uint8_t cardsize=0; |
1582 | bool read_success=false; | |
1583 | uint8_t mem=0; | |
1584 | ||
1585 | static struct memory_t{ | |
1586 | int k16; | |
1587 | int book; | |
1588 | int k2; | |
1589 | int lockauth; | |
1590 | int keyaccess; | |
1591 | } memory; | |
1592 | ||
1593 | uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes | |
1594 | ||
1595 | setupIclassReader(); | |
1596 | ||
1597 | ||
1598 | for(int i=0;i<1;i++) { | |
1599 | ||
1600 | if(traceLen > TRACE_SIZE) { | |
1601 | DbpString("Trace full"); | |
1602 | break; | |
1603 | } | |
1604 | ||
1605 | if (BUTTON_PRESS()) break; | |
1606 | ||
1607 | // Send act_all | |
1608 | ReaderTransmitIClass(act_all, 1); | |
1609 | // Card present? | |
1610 | if(ReaderReceiveIClass(resp)) { | |
1611 | ReaderTransmitIClass(identify, 1); | |
1612 | if(ReaderReceiveIClass(resp) == 10) { | |
1613 | // Select card | |
1614 | memcpy(&select[1],resp,8); | |
1615 | ReaderTransmitIClass(select, sizeof(select)); | |
1616 | ||
1617 | if(ReaderReceiveIClass(resp) == 10) { | |
1618 | Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x", | |
1619 | resp[0], resp[1], resp[2], | |
1620 | resp[3], resp[4], resp[5], | |
1621 | resp[6], resp[7]); | |
1622 | } | |
1623 | // Card selected | |
1624 | Dbprintf("Readcheck on Sector 2"); | |
1625 | ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc)); | |
1626 | if(ReaderReceiveIClass(resp) == 8) { | |
1627 | Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x", | |
1628 | resp[0], resp[1], resp[2], | |
1629 | resp[3], resp[4], resp[5], | |
1630 | resp[6], resp[7]); | |
1631 | }else return; | |
1632 | Dbprintf("Authenticate"); | |
1633 | //for now replay captured auth (as cc not updated) | |
1634 | memcpy(check+5,MAC,4); | |
1635 | //Dbprintf(" AA: %02x %02x %02x %02x", | |
1636 | // check[5], check[6], check[7],check[8]); | |
1637 | ReaderTransmitIClass(check, sizeof(check)); | |
1638 | if(ReaderReceiveIClass(resp) == 4) { | |
1639 | Dbprintf(" AR: %02x %02x %02x %02x", | |
1640 | resp[0], resp[1], resp[2],resp[3]); | |
1641 | }else { | |
1642 | Dbprintf("Error: Authentication Fail!"); | |
1643 | return; | |
1644 | } | |
1645 | Dbprintf("Dump Contents"); | |
1646 | //first get configuration block | |
1647 | read_success=false; | |
1648 | read[1]=1; | |
1649 | uint8_t *blockno=&read[1]; | |
1650 | crc = iclass_crc16((char *)blockno,1); | |
1651 | read[2] = crc >> 8; | |
1652 | read[3] = crc & 0xff; | |
1653 | while(!read_success){ | |
1654 | ReaderTransmitIClass(read, sizeof(read)); | |
1655 | if(ReaderReceiveIClass(resp) == 10) { | |
1656 | read_success=true; | |
1657 | mem=resp[5]; | |
1658 | memory.k16= (mem & 0x80); | |
1659 | memory.book= (mem & 0x20); | |
1660 | memory.k2= (mem & 0x8); | |
1661 | memory.lockauth= (mem & 0x2); | |
1662 | memory.keyaccess= (mem & 0x1); | |
1663 | ||
1664 | } | |
1665 | } | |
1666 | if (memory.k16){ | |
1667 | cardsize=255; | |
1668 | }else cardsize=32; | |
1669 | //then loop around remaining blocks | |
1670 | for(uint8_t j=0; j<cardsize; j++){ | |
1671 | read_success=false; | |
1672 | uint8_t *blockno=&j; | |
1673 | //crc_data[0]=j; | |
1674 | read[1]=j; | |
1675 | crc = iclass_crc16((char *)blockno,1); | |
1676 | read[2] = crc >> 8; | |
1677 | read[3] = crc & 0xff; | |
1678 | while(!read_success){ | |
1679 | ReaderTransmitIClass(read, sizeof(read)); | |
1680 | if(ReaderReceiveIClass(resp) == 10) { | |
1681 | read_success=true; | |
1682 | Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x", | |
1683 | j, resp[0], resp[1], resp[2], | |
1684 | resp[3], resp[4], resp[5], | |
1685 | resp[6], resp[7]); | |
1686 | } | |
1687 | } | |
1688 | } | |
1689 | } | |
1690 | } | |
1691 | WDT_HIT(); | |
1692 | } | |
1693 | ||
1694 | LED_A_OFF(); | |
1695 | } | |
1696 | ||
1697 | //2. Create Read method (cut-down from above) based off responses from 1. | |
1698 | // Since we have the MAC could continue to use replay function. | |
1699 | //3. Create Write method | |
1700 | /* | |
1701 | void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) { | |
1702 | uint8_t act_all[] = { 0x0a }; | |
1703 | uint8_t identify[] = { 0x0c }; | |
1704 | uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
1705 | uint8_t readcheck_cc[]= { 0x88, 0x02 }; | |
1706 | uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
1707 | uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 }; | |
1708 | uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; | |
1709 | ||
1710 | uint16_t crc = 0; | |
1711 | ||
1712 | uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes | |
7cc204bf | 1713 | |
1e262141 | 1714 | // Reset trace buffer |
ff7bb4ef | 1715 | memset(trace, 0x44, RECV_CMD_OFFSET); |
1e262141 | 1716 | traceLen = 0; |
1717 | ||
1718 | // Setup SSC | |
1719 | FpgaSetupSsc(); | |
1720 | // Start from off (no field generated) | |
1721 | // Signal field is off with the appropriate LED | |
cee5a30d | 1722 | LED_D_OFF(); |
1e262141 | 1723 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
1724 | SpinDelay(200); | |
1725 | ||
1726 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
1727 | ||
1728 | // Now give it time to spin up. | |
1729 | // Signal field is on with the appropriate LED | |
1730 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD); | |
1731 | SpinDelay(200); | |
1732 | ||
1733 | LED_A_ON(); | |
1734 | ||
f38a1528 | 1735 | for(int i=0;i<1;i++) { |
4ab4336a | 1736 | |
1737 | if(traceLen > TRACE_SIZE) { | |
1738 | DbpString("Trace full"); | |
1739 | break; | |
1740 | } | |
1741 | ||
1742 | if (BUTTON_PRESS()) break; | |
1e262141 | 1743 | |
1744 | // Send act_all | |
1745 | ReaderTransmitIClass(act_all, 1); | |
1746 | // Card present? | |
1747 | if(ReaderReceiveIClass(resp)) { | |
1748 | ReaderTransmitIClass(identify, 1); | |
4ab4336a | 1749 | if(ReaderReceiveIClass(resp) == 10) { |
1750 | // Select card | |
1751 | memcpy(&select[1],resp,8); | |
1752 | ReaderTransmitIClass(select, sizeof(select)); | |
1753 | ||
1754 | if(ReaderReceiveIClass(resp) == 10) { | |
1755 | Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x", | |
1756 | resp[0], resp[1], resp[2], | |
1757 | resp[3], resp[4], resp[5], | |
1758 | resp[6], resp[7]); | |
1759 | } | |
f38a1528 | 1760 | // Card selected |
1761 | Dbprintf("Readcheck on Sector 2"); | |
1762 | ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc)); | |
1763 | if(ReaderReceiveIClass(resp) == 8) { | |
1764 | Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x", | |
1765 | resp[0], resp[1], resp[2], | |
1766 | resp[3], resp[4], resp[5], | |
1767 | resp[6], resp[7]); | |
1768 | }else return; | |
1769 | Dbprintf("Authenticate"); | |
1770 | //for now replay captured auth (as cc not updated) | |
1771 | memcpy(check+5,MAC,4); | |
1772 | Dbprintf(" AA: %02x %02x %02x %02x", | |
1773 | check[5], check[6], check[7],check[8]); | |
1774 | ReaderTransmitIClass(check, sizeof(check)); | |
1775 | if(ReaderReceiveIClass(resp) == 4) { | |
1776 | Dbprintf(" AR: %02x %02x %02x %02x", | |
1777 | resp[0], resp[1], resp[2],resp[3]); | |
1778 | }else { | |
1779 | Dbprintf("Error: Authentication Fail!"); | |
1780 | return; | |
1781 | } | |
1782 | Dbprintf("Write Block"); | |
1783 | ||
1784 | //read configuration for max block number | |
1785 | read_success=false; | |
1786 | read[1]=1; | |
1787 | uint8_t *blockno=&read[1]; | |
1788 | crc = iclass_crc16((char *)blockno,1); | |
1789 | read[2] = crc >> 8; | |
1790 | read[3] = crc & 0xff; | |
1791 | while(!read_success){ | |
1792 | ReaderTransmitIClass(read, sizeof(read)); | |
1793 | if(ReaderReceiveIClass(resp) == 10) { | |
1794 | read_success=true; | |
1795 | mem=resp[5]; | |
1796 | memory.k16= (mem & 0x80); | |
1797 | memory.book= (mem & 0x20); | |
1798 | memory.k2= (mem & 0x8); | |
1799 | memory.lockauth= (mem & 0x2); | |
1800 | memory.keyaccess= (mem & 0x1); | |
1801 | ||
1802 | } | |
1803 | } | |
1804 | if (memory.k16){ | |
1805 | cardsize=255; | |
1806 | }else cardsize=32; | |
1807 | //check card_size | |
1808 | ||
1809 | memcpy(write+1,blockNo,1); | |
1810 | memcpy(write+2,data,8); | |
1811 | memcpy(write+10,mac,4); | |
1812 | while(!send_success){ | |
1813 | ReaderTransmitIClass(write, sizeof(write)); | |
fecd8202 | 1814 | if(ReaderReceiveIClass(resp) == 10) { |
f38a1528 | 1815 | write_success=true; |
1816 | } | |
1817 | }// | |
1e262141 | 1818 | } |
1819 | WDT_HIT(); | |
1820 | } | |
1821 | ||
1822 | LED_A_OFF(); | |
f38a1528 | 1823 | }*/ |