]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/iclass.c
Implemented client side changes for iclass hack, attempted to fix issues with trace...
[proxmark3-svn] / armsrc / iclass.c
CommitLineData
cee5a30d 1//-----------------------------------------------------------------------------
2// Gerhard de Koning Gans - May 2008
3// Hagen Fritsch - June 2010
4// Gerhard de Koning Gans - May 2011
1e262141 5// Gerhard de Koning Gans - June 2012 - Added iClass card and reader emulation
cee5a30d 6//
7// This code is licensed to you under the terms of the GNU GPL, version 2 or,
8// at your option, any later version. See the LICENSE.txt file for the text of
9// the license.
10//-----------------------------------------------------------------------------
11// Routines to support iClass.
12//-----------------------------------------------------------------------------
13// Based on ISO14443a implementation. Still in experimental phase.
14// Contribution made during a security research at Radboud University Nijmegen
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
39#include "proxmark3.h"
40#include "apps.h"
41#include "util.h"
42#include "string.h"
7e67e42f 43#include "common.h"
1e262141 44// Needed for CRC in emulation mode;
45// same construction as in ISO 14443;
46// different initial value (CRC_ICLASS)
47#include "iso14443crc.h"
cee5a30d 48
1e262141 49static int timeout = 4096;
cee5a30d 50
51// CARD TO READER
52// Sequence D: 11110000 modulation with subcarrier during first half
53// Sequence E: 00001111 modulation with subcarrier during second half
54// Sequence F: 00000000 no modulation with subcarrier
55// READER TO CARD
56// Sequence X: 00001100 drop after half a period
57// Sequence Y: 00000000 no drop
58// Sequence Z: 11000000 drop at start
cee5a30d 59#define SEC_X 0x0c
60#define SEC_Y 0x00
61#define SEC_Z 0xc0
62
1e262141 63static int SendIClassAnswer(uint8_t *resp, int respLen, int delay);
cee5a30d 64
65//-----------------------------------------------------------------------------
66// The software UART that receives commands from the reader, and its state
67// variables.
68//-----------------------------------------------------------------------------
69static struct {
70 enum {
71 STATE_UNSYNCD,
72 STATE_START_OF_COMMUNICATION,
73 STATE_RECEIVING
74 } state;
75 uint16_t shiftReg;
76 int bitCnt;
77 int byteCnt;
78 int byteCntMax;
79 int posCnt;
80 int nOutOfCnt;
81 int OutOfCnt;
82 int syncBit;
1e262141 83 int parityBits;
84 int samples;
cee5a30d 85 int highCnt;
86 int swapper;
87 int counter;
88 int bitBuffer;
89 int dropPosition;
90 uint8_t *output;
91} Uart;
92
1e262141 93static RAMFUNC int OutOfNDecoding(int bit)
cee5a30d 94{
9f693930 95 //int error = 0;
cee5a30d 96 int bitright;
97
98 if(!Uart.bitBuffer) {
99 Uart.bitBuffer = bit ^ 0xFF0;
100 return FALSE;
101 }
102 else {
103 Uart.bitBuffer <<= 4;
104 Uart.bitBuffer ^= bit;
105 }
106
107 /*if(Uart.swapper) {
108 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
109 Uart.byteCnt++;
110 Uart.swapper = 0;
111 if(Uart.byteCnt > 15) { return TRUE; }
112 }
113 else {
114 Uart.swapper = 1;
115 }*/
116
117 if(Uart.state != STATE_UNSYNCD) {
118 Uart.posCnt++;
119
120 if((Uart.bitBuffer & Uart.syncBit) ^ Uart.syncBit) {
121 bit = 0x00;
122 }
123 else {
124 bit = 0x01;
125 }
126 if(((Uart.bitBuffer << 1) & Uart.syncBit) ^ Uart.syncBit) {
127 bitright = 0x00;
128 }
129 else {
130 bitright = 0x01;
131 }
132 if(bit != bitright) { bit = bitright; }
133
134
135 // So, now we only have to deal with *bit*, lets see...
136 if(Uart.posCnt == 1) {
137 // measurement first half bitperiod
138 if(!bit) {
139 // Drop in first half means that we are either seeing
140 // an SOF or an EOF.
141
142 if(Uart.nOutOfCnt == 1) {
143 // End of Communication
144 Uart.state = STATE_UNSYNCD;
145 Uart.highCnt = 0;
146 if(Uart.byteCnt == 0) {
147 // Its not straightforward to show single EOFs
148 // So just leave it and do not return TRUE
149 Uart.output[Uart.byteCnt] = 0xf0;
150 Uart.byteCnt++;
151
152 // Calculate the parity bit for the client...
153 Uart.parityBits = 1;
154 }
155 else {
156 return TRUE;
157 }
158 }
159 else if(Uart.state != STATE_START_OF_COMMUNICATION) {
160 // When not part of SOF or EOF, it is an error
161 Uart.state = STATE_UNSYNCD;
162 Uart.highCnt = 0;
9f693930 163 //error = 4;
cee5a30d 164 }
165 }
166 }
167 else {
168 // measurement second half bitperiod
169 // Count the bitslot we are in... (ISO 15693)
170 Uart.nOutOfCnt++;
171
172 if(!bit) {
173 if(Uart.dropPosition) {
174 if(Uart.state == STATE_START_OF_COMMUNICATION) {
9f693930 175 //error = 1;
cee5a30d 176 }
177 else {
9f693930 178 //error = 7;
cee5a30d 179 }
180 // It is an error if we already have seen a drop in current frame
181 Uart.state = STATE_UNSYNCD;
182 Uart.highCnt = 0;
183 }
184 else {
185 Uart.dropPosition = Uart.nOutOfCnt;
186 }
187 }
188
189 Uart.posCnt = 0;
190
191
192 if(Uart.nOutOfCnt == Uart.OutOfCnt && Uart.OutOfCnt == 4) {
193 Uart.nOutOfCnt = 0;
194
195 if(Uart.state == STATE_START_OF_COMMUNICATION) {
196 if(Uart.dropPosition == 4) {
197 Uart.state = STATE_RECEIVING;
198 Uart.OutOfCnt = 256;
199 }
200 else if(Uart.dropPosition == 3) {
201 Uart.state = STATE_RECEIVING;
202 Uart.OutOfCnt = 4;
203 //Uart.output[Uart.byteCnt] = 0xdd;
204 //Uart.byteCnt++;
205 }
206 else {
207 Uart.state = STATE_UNSYNCD;
208 Uart.highCnt = 0;
209 }
210 Uart.dropPosition = 0;
211 }
212 else {
213 // RECEIVING DATA
214 // 1 out of 4
215 if(!Uart.dropPosition) {
216 Uart.state = STATE_UNSYNCD;
217 Uart.highCnt = 0;
9f693930 218 //error = 9;
cee5a30d 219 }
220 else {
221 Uart.shiftReg >>= 2;
222
223 // Swap bit order
224 Uart.dropPosition--;
225 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
226 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
227
228 Uart.shiftReg ^= ((Uart.dropPosition & 0x03) << 6);
229 Uart.bitCnt += 2;
230 Uart.dropPosition = 0;
231
232 if(Uart.bitCnt == 8) {
233 Uart.output[Uart.byteCnt] = (Uart.shiftReg & 0xff);
234 Uart.byteCnt++;
235
236 // Calculate the parity bit for the client...
237 Uart.parityBits <<= 1;
238 Uart.parityBits ^= OddByteParity[(Uart.shiftReg & 0xff)];
239
240 Uart.bitCnt = 0;
241 Uart.shiftReg = 0;
242 }
243 }
244 }
245 }
246 else if(Uart.nOutOfCnt == Uart.OutOfCnt) {
247 // RECEIVING DATA
248 // 1 out of 256
249 if(!Uart.dropPosition) {
250 Uart.state = STATE_UNSYNCD;
251 Uart.highCnt = 0;
9f693930 252 //error = 3;
cee5a30d 253 }
254 else {
255 Uart.dropPosition--;
256 Uart.output[Uart.byteCnt] = (Uart.dropPosition & 0xff);
257 Uart.byteCnt++;
258
259 // Calculate the parity bit for the client...
260 Uart.parityBits <<= 1;
261 Uart.parityBits ^= OddByteParity[(Uart.dropPosition & 0xff)];
262
263 Uart.bitCnt = 0;
264 Uart.shiftReg = 0;
265 Uart.nOutOfCnt = 0;
266 Uart.dropPosition = 0;
267 }
268 }
269
270 /*if(error) {
271 Uart.output[Uart.byteCnt] = 0xAA;
272 Uart.byteCnt++;
273 Uart.output[Uart.byteCnt] = error & 0xFF;
274 Uart.byteCnt++;
275 Uart.output[Uart.byteCnt] = 0xAA;
276 Uart.byteCnt++;
277 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
278 Uart.byteCnt++;
279 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
280 Uart.byteCnt++;
281 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
282 Uart.byteCnt++;
283 Uart.output[Uart.byteCnt] = 0xAA;
284 Uart.byteCnt++;
285 return TRUE;
286 }*/
287 }
288
289 }
290 else {
291 bit = Uart.bitBuffer & 0xf0;
292 bit >>= 4;
293 bit ^= 0x0F; // drops become 1s ;-)
294 if(bit) {
295 // should have been high or at least (4 * 128) / fc
296 // according to ISO this should be at least (9 * 128 + 20) / fc
297 if(Uart.highCnt == 8) {
298 // we went low, so this could be start of communication
299 // it turns out to be safer to choose a less significant
300 // syncbit... so we check whether the neighbour also represents the drop
301 Uart.posCnt = 1; // apparently we are busy with our first half bit period
302 Uart.syncBit = bit & 8;
303 Uart.samples = 3;
304 if(!Uart.syncBit) { Uart.syncBit = bit & 4; Uart.samples = 2; }
305 else if(bit & 4) { Uart.syncBit = bit & 4; Uart.samples = 2; bit <<= 2; }
306 if(!Uart.syncBit) { Uart.syncBit = bit & 2; Uart.samples = 1; }
307 else if(bit & 2) { Uart.syncBit = bit & 2; Uart.samples = 1; bit <<= 1; }
308 if(!Uart.syncBit) { Uart.syncBit = bit & 1; Uart.samples = 0;
309 if(Uart.syncBit && (Uart.bitBuffer & 8)) {
310 Uart.syncBit = 8;
311
312 // the first half bit period is expected in next sample
313 Uart.posCnt = 0;
314 Uart.samples = 3;
315 }
316 }
317 else if(bit & 1) { Uart.syncBit = bit & 1; Uart.samples = 0; }
318
319 Uart.syncBit <<= 4;
320 Uart.state = STATE_START_OF_COMMUNICATION;
321 Uart.bitCnt = 0;
322 Uart.byteCnt = 0;
323 Uart.parityBits = 0;
324 Uart.nOutOfCnt = 0;
325 Uart.OutOfCnt = 4; // Start at 1/4, could switch to 1/256
326 Uart.dropPosition = 0;
327 Uart.shiftReg = 0;
9f693930 328 //error = 0;
cee5a30d 329 }
330 else {
331 Uart.highCnt = 0;
332 }
333 }
334 else {
335 if(Uart.highCnt < 8) {
336 Uart.highCnt++;
337 }
338 }
339 }
340
341 return FALSE;
342}
343
344//=============================================================================
1e262141 345// Manchester
cee5a30d 346//=============================================================================
347
348static struct {
349 enum {
350 DEMOD_UNSYNCD,
351 DEMOD_START_OF_COMMUNICATION,
352 DEMOD_START_OF_COMMUNICATION2,
353 DEMOD_START_OF_COMMUNICATION3,
354 DEMOD_SOF_COMPLETE,
355 DEMOD_MANCHESTER_D,
356 DEMOD_MANCHESTER_E,
357 DEMOD_END_OF_COMMUNICATION,
358 DEMOD_END_OF_COMMUNICATION2,
359 DEMOD_MANCHESTER_F,
360 DEMOD_ERROR_WAIT
361 } state;
362 int bitCount;
363 int posCount;
364 int syncBit;
365 int parityBits;
366 uint16_t shiftReg;
367 int buffer;
368 int buffer2;
369 int buffer3;
370 int buff;
371 int samples;
372 int len;
373 enum {
374 SUB_NONE,
375 SUB_FIRST_HALF,
376 SUB_SECOND_HALF,
377 SUB_BOTH
378 } sub;
379 uint8_t *output;
380} Demod;
381
382static RAMFUNC int ManchesterDecoding(int v)
383{
384 int bit;
385 int modulation;
386 int error = 0;
387
388 bit = Demod.buffer;
389 Demod.buffer = Demod.buffer2;
390 Demod.buffer2 = Demod.buffer3;
391 Demod.buffer3 = v;
392
393 if(Demod.buff < 3) {
394 Demod.buff++;
395 return FALSE;
396 }
397
398 if(Demod.state==DEMOD_UNSYNCD) {
399 Demod.output[Demod.len] = 0xfa;
400 Demod.syncBit = 0;
401 //Demod.samples = 0;
402 Demod.posCount = 1; // This is the first half bit period, so after syncing handle the second part
cee5a30d 403
404 if(bit & 0x08) {
405 Demod.syncBit = 0x08;
406 }
407
408 if(bit & 0x04) {
409 if(Demod.syncBit) {
410 bit <<= 4;
411 }
412 Demod.syncBit = 0x04;
413 }
414
415 if(bit & 0x02) {
416 if(Demod.syncBit) {
417 bit <<= 2;
418 }
419 Demod.syncBit = 0x02;
420 }
421
422 if(bit & 0x01 && Demod.syncBit) {
423 Demod.syncBit = 0x01;
424 }
425
426 if(Demod.syncBit) {
427 Demod.len = 0;
428 Demod.state = DEMOD_START_OF_COMMUNICATION;
429 Demod.sub = SUB_FIRST_HALF;
430 Demod.bitCount = 0;
431 Demod.shiftReg = 0;
432 Demod.parityBits = 0;
433 Demod.samples = 0;
434 if(Demod.posCount) {
435 //if(trigger) LED_A_OFF(); // Not useful in this case...
436 switch(Demod.syncBit) {
437 case 0x08: Demod.samples = 3; break;
438 case 0x04: Demod.samples = 2; break;
439 case 0x02: Demod.samples = 1; break;
440 case 0x01: Demod.samples = 0; break;
441 }
442 // SOF must be long burst... otherwise stay unsynced!!!
443 if(!(Demod.buffer & Demod.syncBit) || !(Demod.buffer2 & Demod.syncBit)) {
444 Demod.state = DEMOD_UNSYNCD;
445 }
446 }
447 else {
448 // SOF must be long burst... otherwise stay unsynced!!!
449 if(!(Demod.buffer2 & Demod.syncBit) || !(Demod.buffer3 & Demod.syncBit)) {
450 Demod.state = DEMOD_UNSYNCD;
451 error = 0x88;
452 }
453
454 }
455 error = 0;
456
457 }
458 }
459 else {
460 modulation = bit & Demod.syncBit;
461 modulation |= ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
462 //modulation = ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
463
464 Demod.samples += 4;
465
466 if(Demod.posCount==0) {
467 Demod.posCount = 1;
468 if(modulation) {
469 Demod.sub = SUB_FIRST_HALF;
470 }
471 else {
472 Demod.sub = SUB_NONE;
473 }
474 }
475 else {
476 Demod.posCount = 0;
477 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
478 if(Demod.state!=DEMOD_ERROR_WAIT) {
479 Demod.state = DEMOD_ERROR_WAIT;
480 Demod.output[Demod.len] = 0xaa;
481 error = 0x01;
482 }
483 }*/
484 //else if(modulation) {
485 if(modulation) {
486 if(Demod.sub == SUB_FIRST_HALF) {
487 Demod.sub = SUB_BOTH;
488 }
489 else {
490 Demod.sub = SUB_SECOND_HALF;
491 }
492 }
493 else if(Demod.sub == SUB_NONE) {
494 if(Demod.state == DEMOD_SOF_COMPLETE) {
495 Demod.output[Demod.len] = 0x0f;
496 Demod.len++;
497 Demod.parityBits <<= 1;
498 Demod.parityBits ^= OddByteParity[0x0f];
499 Demod.state = DEMOD_UNSYNCD;
500// error = 0x0f;
501 return TRUE;
502 }
503 else {
504 Demod.state = DEMOD_ERROR_WAIT;
505 error = 0x33;
506 }
507 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
508 Demod.state = DEMOD_ERROR_WAIT;
509 Demod.output[Demod.len] = 0xaa;
510 error = 0x01;
511 }*/
512 }
513
514 switch(Demod.state) {
515 case DEMOD_START_OF_COMMUNICATION:
516 if(Demod.sub == SUB_BOTH) {
517 //Demod.state = DEMOD_MANCHESTER_D;
518 Demod.state = DEMOD_START_OF_COMMUNICATION2;
519 Demod.posCount = 1;
520 Demod.sub = SUB_NONE;
521 }
522 else {
523 Demod.output[Demod.len] = 0xab;
524 Demod.state = DEMOD_ERROR_WAIT;
525 error = 0xd2;
526 }
527 break;
528 case DEMOD_START_OF_COMMUNICATION2:
529 if(Demod.sub == SUB_SECOND_HALF) {
530 Demod.state = DEMOD_START_OF_COMMUNICATION3;
531 }
532 else {
533 Demod.output[Demod.len] = 0xab;
534 Demod.state = DEMOD_ERROR_WAIT;
535 error = 0xd3;
536 }
537 break;
538 case DEMOD_START_OF_COMMUNICATION3:
539 if(Demod.sub == SUB_SECOND_HALF) {
540// Demod.state = DEMOD_MANCHESTER_D;
541 Demod.state = DEMOD_SOF_COMPLETE;
542 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
543 //Demod.len++;
544 }
545 else {
546 Demod.output[Demod.len] = 0xab;
547 Demod.state = DEMOD_ERROR_WAIT;
548 error = 0xd4;
549 }
550 break;
551 case DEMOD_SOF_COMPLETE:
552 case DEMOD_MANCHESTER_D:
553 case DEMOD_MANCHESTER_E:
554 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
555 // 00001111 = 1 (0 in 14443)
556 if(Demod.sub == SUB_SECOND_HALF) { // SUB_FIRST_HALF
557 Demod.bitCount++;
558 Demod.shiftReg = (Demod.shiftReg >> 1) ^ 0x100;
559 Demod.state = DEMOD_MANCHESTER_D;
560 }
561 else if(Demod.sub == SUB_FIRST_HALF) { // SUB_SECOND_HALF
562 Demod.bitCount++;
563 Demod.shiftReg >>= 1;
564 Demod.state = DEMOD_MANCHESTER_E;
565 }
566 else if(Demod.sub == SUB_BOTH) {
567 Demod.state = DEMOD_MANCHESTER_F;
568 }
569 else {
570 Demod.state = DEMOD_ERROR_WAIT;
571 error = 0x55;
572 }
573 break;
574
575 case DEMOD_MANCHESTER_F:
576 // Tag response does not need to be a complete byte!
577 if(Demod.len > 0 || Demod.bitCount > 0) {
578 if(Demod.bitCount > 1) { // was > 0, do not interpret last closing bit, is part of EOF
579 Demod.shiftReg >>= (9 - Demod.bitCount);
580 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
581 Demod.len++;
582 // No parity bit, so just shift a 0
583 Demod.parityBits <<= 1;
584 }
585
586 Demod.state = DEMOD_UNSYNCD;
587 return TRUE;
588 }
589 else {
590 Demod.output[Demod.len] = 0xad;
591 Demod.state = DEMOD_ERROR_WAIT;
592 error = 0x03;
593 }
594 break;
595
596 case DEMOD_ERROR_WAIT:
597 Demod.state = DEMOD_UNSYNCD;
598 break;
599
600 default:
601 Demod.output[Demod.len] = 0xdd;
602 Demod.state = DEMOD_UNSYNCD;
603 break;
604 }
605
606 /*if(Demod.bitCount>=9) {
607 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
608 Demod.len++;
609
610 Demod.parityBits <<= 1;
611 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
612
613 Demod.bitCount = 0;
614 Demod.shiftReg = 0;
615 }*/
616 if(Demod.bitCount>=8) {
617 Demod.shiftReg >>= 1;
618 Demod.output[Demod.len] = (Demod.shiftReg & 0xff);
619 Demod.len++;
620
621 // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT
622 Demod.parityBits <<= 1;
623 Demod.parityBits ^= OddByteParity[(Demod.shiftReg & 0xff)];
624
625 Demod.bitCount = 0;
626 Demod.shiftReg = 0;
627 }
628
629 if(error) {
630 Demod.output[Demod.len] = 0xBB;
631 Demod.len++;
632 Demod.output[Demod.len] = error & 0xFF;
633 Demod.len++;
634 Demod.output[Demod.len] = 0xBB;
635 Demod.len++;
636 Demod.output[Demod.len] = bit & 0xFF;
637 Demod.len++;
638 Demod.output[Demod.len] = Demod.buffer & 0xFF;
639 Demod.len++;
640 // Look harder ;-)
641 Demod.output[Demod.len] = Demod.buffer2 & 0xFF;
642 Demod.len++;
643 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
644 Demod.len++;
645 Demod.output[Demod.len] = 0xBB;
646 Demod.len++;
647 return TRUE;
648 }
649
650 }
651
652 } // end (state != UNSYNCED)
653
654 return FALSE;
655}
656
657//=============================================================================
1e262141 658// Finally, a `sniffer' for iClass communication
cee5a30d 659// Both sides of communication!
660//=============================================================================
661
662//-----------------------------------------------------------------------------
663// Record the sequence of commands sent by the reader to the tag, with
664// triggering so that we start recording at the point that the tag is moved
665// near the reader.
666//-----------------------------------------------------------------------------
667void RAMFUNC SnoopIClass(void)
668{
17cba269 669
cee5a30d 670
671 // We won't start recording the frames that we acquire until we trigger;
672 // a good trigger condition to get started is probably when we see a
673 // response from the tag.
9f693930 674 //int triggered = FALSE; // FALSE to wait first for card
cee5a30d 675
676 // The command (reader -> tag) that we're receiving.
677 // The length of a received command will in most cases be no more than 18 bytes.
678 // So 32 should be enough!
17cba269 679 uint8_t *readerToTagCmd = (((uint8_t *)BigBuf) + RECV_CMD_OFFSET);
cee5a30d 680 // The response (tag -> reader) that we're receiving.
17cba269 681 uint8_t *tagToReaderResponse = (((uint8_t *)BigBuf) + RECV_RES_OFFSET);
cee5a30d 682
1e262141 683 // reset traceLen to 0
684 iso14a_set_tracing(TRUE);
d19929cb 685 iso14a_clear_trace();
1e262141 686 iso14a_set_trigger(FALSE);
cee5a30d 687
688 // The DMA buffer, used to stream samples from the FPGA
689 int8_t *dmaBuf = ((int8_t *)BigBuf) + DMA_BUFFER_OFFSET;
690 int lastRxCounter;
691 int8_t *upTo;
692 int smpl;
693 int maxBehindBy = 0;
694
695 // Count of samples received so far, so that we can include timing
696 // information in the trace buffer.
697 int samples = 0;
698 rsamples = 0;
699
700 memset(trace, 0x44, RECV_CMD_OFFSET);
701
702 // Set up the demodulator for tag -> reader responses.
17cba269 703 Demod.output = tagToReaderResponse;
cee5a30d 704 Demod.len = 0;
705 Demod.state = DEMOD_UNSYNCD;
706
707 // Setup for the DMA.
708 FpgaSetupSsc();
709 upTo = dmaBuf;
710 lastRxCounter = DMA_BUFFER_SIZE;
711 FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE);
712
713 // And the reader -> tag commands
714 memset(&Uart, 0, sizeof(Uart));
17cba269 715 Uart.output = readerToTagCmd;
cee5a30d 716 Uart.byteCntMax = 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
717 Uart.state = STATE_UNSYNCD;
718
719 // And put the FPGA in the appropriate mode
720 // Signal field is off with the appropriate LED
721 LED_D_OFF();
722 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_SNIFFER);
723 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
724
725 int div = 0;
726 //int div2 = 0;
727 int decbyte = 0;
728 int decbyter = 0;
729
730 // And now we loop, receiving samples.
731 for(;;) {
732 LED_A_ON();
733 WDT_HIT();
734 int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) &
735 (DMA_BUFFER_SIZE-1);
736 if(behindBy > maxBehindBy) {
737 maxBehindBy = behindBy;
738 if(behindBy > 400) {
739 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy);
740 goto done;
741 }
742 }
743 if(behindBy < 1) continue;
744
745 LED_A_OFF();
746 smpl = upTo[0];
747 upTo++;
748 lastRxCounter -= 1;
749 if(upTo - dmaBuf > DMA_BUFFER_SIZE) {
750 upTo -= DMA_BUFFER_SIZE;
751 lastRxCounter += DMA_BUFFER_SIZE;
752 AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
753 AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE;
754 }
755
756 //samples += 4;
757 samples += 1;
cee5a30d 758
cee5a30d 759 if(smpl & 0xF) {
760 decbyte ^= (1 << (3 - div));
761 }
cee5a30d 762
763 // FOR READER SIDE COMMUMICATION...
17cba269 764
cee5a30d 765 decbyter <<= 2;
766 decbyter ^= (smpl & 0x30);
767
768 div++;
769
770 if((div + 1) % 2 == 0) {
771 smpl = decbyter;
1e262141 772 if(OutOfNDecoding((smpl & 0xF0) >> 4)) {
cee5a30d 773 rsamples = samples - Uart.samples;
774 LED_C_ON();
17cba269
MHS
775
776 if(!LogTrace(readerToTagCmd,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
777 //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
778
779 /* And ready to receive another command. */
cee5a30d 780 Uart.state = STATE_UNSYNCD;
781 /* And also reset the demod code, which might have been */
782 /* false-triggered by the commands from the reader. */
783 Demod.state = DEMOD_UNSYNCD;
784 LED_B_OFF();
785 Uart.byteCnt = 0;
786 }
787 decbyter = 0;
788 }
789
790 if(div > 3) {
791 smpl = decbyte;
792 if(ManchesterDecoding(smpl & 0x0F)) {
793 rsamples = samples - Demod.samples;
794 LED_B_ON();
795
17cba269
MHS
796 if(!LogTrace(tagToReaderResponse,Demod.len, rsamples, Demod.parityBits,FALSE)) break;
797 //if (!LogTrace(NULL, 0, Demod.endTime*16 - DELAY_TAG_AIR2ARM_AS_SNIFFER, 0, FALSE)) break;
798
cee5a30d 799
800 // And ready to receive another response.
801 memset(&Demod, 0, sizeof(Demod));
17cba269 802 Demod.output = tagToReaderResponse;
cee5a30d 803 Demod.state = DEMOD_UNSYNCD;
804 LED_C_OFF();
805 }
806
807 div = 0;
808 decbyte = 0x00;
809 }
810 //}
811
812 if(BUTTON_PRESS()) {
813 DbpString("cancelled_a");
814 goto done;
815 }
816 }
817
818 DbpString("COMMAND FINISHED");
819
820 Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
821 Dbprintf("%x %x %x", Uart.byteCntMax, traceLen, (int)Uart.output[0]);
822
823done:
824 AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
825 Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
826 Dbprintf("%x %x %x", Uart.byteCntMax, traceLen, (int)Uart.output[0]);
827 LED_A_OFF();
828 LED_B_OFF();
1e262141 829 LED_C_OFF();
830 LED_D_OFF();
831}
832
912a3e94 833void rotateCSN(uint8_t* originalCSN, uint8_t* rotatedCSN) {
834 int i;
835 for(i = 0; i < 8; i++) {
836 rotatedCSN[i] = (originalCSN[i] >> 3) | (originalCSN[(i+1)%8] << 5);
1e262141 837 }
838}
839
840//-----------------------------------------------------------------------------
841// Wait for commands from reader
842// Stop when button is pressed
843// Or return TRUE when command is captured
844//-----------------------------------------------------------------------------
845static int GetIClassCommandFromReader(uint8_t *received, int *len, int maxLen)
846{
912a3e94 847 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
1e262141 848 // only, since we are receiving, not transmitting).
849 // Signal field is off with the appropriate LED
850 LED_D_OFF();
851 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN);
852
853 // Now run a `software UART' on the stream of incoming samples.
854 Uart.output = received;
855 Uart.byteCntMax = maxLen;
856 Uart.state = STATE_UNSYNCD;
857
858 for(;;) {
859 WDT_HIT();
860
861 if(BUTTON_PRESS()) return FALSE;
862
863 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
864 AT91C_BASE_SSC->SSC_THR = 0x00;
865 }
866 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
867 uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
868 /*if(OutOfNDecoding((b & 0xf0) >> 4)) {
869 *len = Uart.byteCnt;
870 return TRUE;
871 }*/
872 if(OutOfNDecoding(b & 0x0f)) {
873 *len = Uart.byteCnt;
874 return TRUE;
875 }
876 }
877 }
878}
879
880
881//-----------------------------------------------------------------------------
882// Prepare tag messages
883//-----------------------------------------------------------------------------
884static void CodeIClassTagAnswer(const uint8_t *cmd, int len)
885{
886 int i;
887
888 ToSendReset();
889
890 // Send SOF
891 ToSend[++ToSendMax] = 0x00;
892 ToSend[++ToSendMax] = 0x00;
893 ToSend[++ToSendMax] = 0x00;
894 ToSend[++ToSendMax] = 0xff;
895 ToSend[++ToSendMax] = 0xff;
896 ToSend[++ToSendMax] = 0xff;
897 ToSend[++ToSendMax] = 0x00;
898 ToSend[++ToSendMax] = 0xff;
899
900 for(i = 0; i < len; i++) {
901 int j;
902 uint8_t b = cmd[i];
903
904 // Data bits
905 for(j = 0; j < 8; j++) {
906 if(b & 1) {
907 ToSend[++ToSendMax] = 0x00;
908 ToSend[++ToSendMax] = 0xff;
909 } else {
910 ToSend[++ToSendMax] = 0xff;
911 ToSend[++ToSendMax] = 0x00;
912 }
913 b >>= 1;
914 }
915 }
916
917 // Send EOF
918 ToSend[++ToSendMax] = 0xff;
919 ToSend[++ToSendMax] = 0x00;
920 ToSend[++ToSendMax] = 0xff;
921 ToSend[++ToSendMax] = 0xff;
922 ToSend[++ToSendMax] = 0xff;
923 ToSend[++ToSendMax] = 0x00;
924 ToSend[++ToSendMax] = 0x00;
925 ToSend[++ToSendMax] = 0x00;
926
927 // Convert from last byte pos to length
928 ToSendMax++;
929}
930
931// Only SOF
932static void CodeIClassTagSOF()
933{
934 ToSendReset();
935
936 // Send SOF
937 ToSend[++ToSendMax] = 0x00;
938 ToSend[++ToSendMax] = 0x00;
939 ToSend[++ToSendMax] = 0x00;
940 ToSend[++ToSendMax] = 0xff;
941 ToSend[++ToSendMax] = 0xff;
942 ToSend[++ToSendMax] = 0xff;
943 ToSend[++ToSendMax] = 0x00;
944 ToSend[++ToSendMax] = 0xff;
945
946 // Convert from last byte pos to length
947 ToSendMax++;
948}
949
ff7bb4ef
MHS
950/**
951 * @brief SimulateIClass simulates an iClass card.
952 * @param arg0 type of simulation
953 * - 0 uses the first 8 bytes in usb data as CSN
954 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
955 * in the usb data. This mode collects MAC from the reader, in order to do an offline
956 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
957 * - Other : Uses the default CSN (031fec8af7ff12e0)
958 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
959 * @param arg2
960 * @param datain
961 */
962void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain)
1e262141 963{
ff7bb4ef
MHS
964 uint32_t simType = arg0;
965 uint32_t numberOfCSNS = arg1;
1e262141 966
ff7bb4ef
MHS
967 // Enable and clear the trace
968 iso14a_set_tracing(TRUE);
969 iso14a_clear_trace();
81cd0474 970
ff7bb4ef
MHS
971 uint8_t csn_crc[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
972
973 if(simType == 0) {
974 // Use the CSN from commandline
975 memcpy(csn_crc, datain, 8);
976 doIClassSimulation(csn_crc,0);
977 }else if(simType == 1)
978 {
979 doIClassSimulation(csn_crc,0);
980 }
981 else if(simType == 2)
982 {
17cba269 983 Dbprintf("Going into attack mode");
ff7bb4ef
MHS
984 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
985 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
986 // in order to obtain the keys, as in the "dismantling iclass"-paper.
987 for(int i = 0 ; i < numberOfCSNS && i*8+8 < USB_CMD_DATA_SIZE; i++)
988 {
989 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
990
991 memcpy(csn_crc, datain+(i*8), 8);
992 doIClassSimulation(csn_crc,1);
993 }
994 }else{
995 // We may want a mode here where we hardcode the csns to use (from proxclone).
996 // That will speed things up a little, but not required just yet.
997 Dbprintf("The mode is not implemented, reserved for future use");
998 }
999
1000}
1001/**
1002 * @brief Does the actual simulation
1003 * @param csn - csn to use
1004 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1005 */
1006void doIClassSimulation(uint8_t csn[], int breakAfterMacReceived)
1007{
1e262141 1008 // CSN followed by two CRC bytes
1e262141 1009 uint8_t response2[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
ff7bb4ef
MHS
1010 uint8_t response3[] = { 0,0,0,0,0,0,0,0,0,0};
1011 memcpy(response3,csn,sizeof(response3));
1e262141 1012
1013 // e-Purse
1014 uint8_t response4[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1e262141 1015
1e262141 1016 // Construct anticollision-CSN
912a3e94 1017 rotateCSN(response3,response2);
1e262141 1018
1019 // Compute CRC on both CSNs
1020 ComputeCrc14443(CRC_ICLASS, response2, 8, &response2[8], &response2[9]);
1021 ComputeCrc14443(CRC_ICLASS, response3, 8, &response3[8], &response3[9]);
1022
ff7bb4ef 1023 int exitLoop = 0;
1e262141 1024 // Reader 0a
1025 // Tag 0f
1026 // Reader 0c
1027 // Tag anticoll. CSN
1028 // Reader 81 anticoll. CSN
1029 // Tag CSN
1030
81cd0474 1031 uint8_t *resp;
1032 int respLen;
1033 uint8_t* respdata = NULL;
1034 int respsize = 0;
1035 uint8_t sof = 0x0f;
1e262141 1036
1037 // Respond SOF -- takes 8 bytes
81cd0474 1038 uint8_t *resp1 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET);
1e262141 1039 int resp1Len;
1040
1041 // Anticollision CSN (rotated CSN)
1042 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
81cd0474 1043 uint8_t *resp2 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 10);
1e262141 1044 int resp2Len;
1045
1046 // CSN
1047 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
81cd0474 1048 uint8_t *resp3 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 190);
912a3e94 1049 int resp3Len;
1e262141 1050
1051 // e-Purse
1052 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
81cd0474 1053 uint8_t *resp4 = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET + 370);
1e262141 1054 int resp4Len;
1055
1056 // + 1720..
ff7bb4ef 1057 uint8_t *receivedCmd = (((uint8_t *)BigBuf) + RECV_CMD_OFFSET);
81cd0474 1058 memset(receivedCmd, 0x44, RECV_CMD_SIZE);
1e262141 1059 int len;
1060
1e262141 1061 // Prepare card messages
1062 ToSendMax = 0;
1063
1064 // First card answer: SOF
1065 CodeIClassTagSOF();
1066 memcpy(resp1, ToSend, ToSendMax); resp1Len = ToSendMax;
1067
1068 // Anticollision CSN
1069 CodeIClassTagAnswer(response2, sizeof(response2));
1070 memcpy(resp2, ToSend, ToSendMax); resp2Len = ToSendMax;
1071
1072 // CSN
1073 CodeIClassTagAnswer(response3, sizeof(response3));
912a3e94 1074 memcpy(resp3, ToSend, ToSendMax); resp3Len = ToSendMax;
1e262141 1075
1076 // e-Purse
1077 CodeIClassTagAnswer(response4, sizeof(response4));
1078 memcpy(resp4, ToSend, ToSendMax); resp4Len = ToSendMax;
1079
1080 // We need to listen to the high-frequency, peak-detected path.
1081 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1082 FpgaSetupSsc();
1083
1084 // To control where we are in the protocol
1e262141 1085 int cmdsRecvd = 0;
912a3e94 1086
1e262141 1087 LED_A_ON();
ff7bb4ef 1088 while(!exitLoop) {
1e262141 1089 LED_B_OFF();
1090 if(!GetIClassCommandFromReader(receivedCmd, &len, 100)) {
81cd0474 1091 DbpString("button press");
1e262141 1092 break;
81cd0474 1093 }
1e262141 1094
81cd0474 1095 // Okay, look at the command now.
17cba269 1096 if(receivedCmd[0] == 0x0a || receivedCmd[0] == 0x26) {
1e262141 1097 // Reader in anticollission phase
1098 resp = resp1; respLen = resp1Len; //order = 1;
81cd0474 1099 respdata = &sof;
1100 respsize = sizeof(sof);
1e262141 1101 //resp = resp2; respLen = resp2Len; order = 2;
17cba269 1102 Dbprintf("Hello request from reader, %02x, tracing=%d", receivedCmd[0], tracing);
1e262141 1103 } else if(receivedCmd[0] == 0x0c) {
1104 // Reader asks for anticollission CSN
1105 resp = resp2; respLen = resp2Len; //order = 2;
81cd0474 1106 respdata = response2;
1107 respsize = sizeof(response2);
1e262141 1108 //DbpString("Reader requests anticollission CSN:");
1109 } else if(receivedCmd[0] == 0x81) {
1110 // Reader selects anticollission CSN.
1111 // Tag sends the corresponding real CSN
912a3e94 1112 resp = resp3; respLen = resp3Len; //order = 3;
81cd0474 1113 respdata = response3;
1114 respsize = sizeof(response3);
1e262141 1115 //DbpString("Reader selects anticollission CSN:");
1116 } else if(receivedCmd[0] == 0x88) {
1117 // Read e-purse (88 02)
1118 resp = resp4; respLen = resp4Len; //order = 4;
81cd0474 1119 respdata = response4;
1120 respsize = sizeof(response4);
1e262141 1121 LED_B_ON();
1122 } else if(receivedCmd[0] == 0x05) {
1123 // Reader random and reader MAC!!!
1e262141 1124 // Do not respond
1125 // We do not know what to answer, so lets keep quit
1126 resp = resp1; respLen = 0; //order = 5;
81cd0474 1127 respdata = NULL;
1128 respsize = 0;
ff7bb4ef
MHS
1129 if (breakAfterMacReceived){
1130 // TODO, actually return this to the caller instead of just
1131 // dbprintf:ing ...
1132 Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x");
1133 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len,
1134 receivedCmd[0], receivedCmd[1], receivedCmd[2],
1135 receivedCmd[3], receivedCmd[4], receivedCmd[5],
1136 receivedCmd[6], receivedCmd[7], receivedCmd[8]);
1137 exitLoop = true;
1138 }
1e262141 1139 } else if(receivedCmd[0] == 0x00 && len == 1) {
1140 // Reader ends the session
1141 resp = resp1; respLen = 0; //order = 0;
81cd0474 1142 respdata = NULL;
1143 respsize = 0;
1144 } else {
17cba269 1145 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1e262141 1146 // Never seen this command before
1147 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1148 len,
1149 receivedCmd[0], receivedCmd[1], receivedCmd[2],
1150 receivedCmd[3], receivedCmd[4], receivedCmd[5],
1151 receivedCmd[6], receivedCmd[7], receivedCmd[8]);
1152 // Do not respond
1153 resp = resp1; respLen = 0; //order = 0;
81cd0474 1154 respdata = NULL;
1155 respsize = 0;
1e262141 1156 }
1157
1158 if(cmdsRecvd > 999) {
1159 DbpString("1000 commands later...");
1160 break;
1161 }
1162 else {
1163 cmdsRecvd++;
1164 }
1165
81cd0474 1166 if(respLen > 0) {
1167 SendIClassAnswer(resp, respLen, 21);
1168 }
1169
1170 if (tracing) {
17cba269
MHS
1171 //LogTrace(receivedCmd,len, rsamples, Uart.parityBits, TRUE);
1172 if(!LogTrace(receivedCmd,len, rsamples, Uart.parityBits,TRUE))
1173 {
4ab4336a 1174 DbpString("Trace full");
1175 break;
1176 }
17cba269
MHS
1177
1178 if (respdata != NULL) {
1179 //LogTrace(respdata,respsize, rsamples, SwapBits(GetParity(respdata,respsize),respsize), FALSE);
1180 if(!LogTrace(respdata,respsize, rsamples,SwapBits(GetParity(respdata,respsize),respsize),FALSE))
1181 {
1182 DbpString("Trace full");
1183 break;
1184 }
1185 }
81cd0474 1186 }
81cd0474 1187 memset(receivedCmd, 0x44, RECV_CMD_SIZE);
1188 }
1e262141 1189
912a3e94 1190 Dbprintf("%x", cmdsRecvd);
1e262141 1191 LED_A_OFF();
1192 LED_B_OFF();
1193}
1194
1195static int SendIClassAnswer(uint8_t *resp, int respLen, int delay)
1196{
1197 int i = 0, u = 0, d = 0;
1198 uint8_t b = 0;
912a3e94 1199 // return 0;
1200 // Modulate Manchester
1201 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_MOD424);
1e262141 1202 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_MOD);
1203 AT91C_BASE_SSC->SSC_THR = 0x00;
1204 FpgaSetupSsc();
1205
1206 // send cycle
1207 for(;;) {
1208 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1209 volatile uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
1210 (void)b;
1211 }
1212 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1213 if(d < delay) {
1214 b = 0x00;
1215 d++;
1216 }
1217 else if(i >= respLen) {
1218 b = 0x00;
1219 u++;
1220 } else {
1221 b = resp[i];
1222 u++;
1223 if(u > 1) { i++; u = 0; }
1224 }
1225 AT91C_BASE_SSC->SSC_THR = b;
1226
1227 if(u > 4) break;
1228 }
1229 if(BUTTON_PRESS()) {
1230 break;
1231 }
1232 }
1233
1234 return 0;
1235}
1236
1237/// THE READER CODE
1238
1239//-----------------------------------------------------------------------------
1240// Transmit the command (to the tag) that was placed in ToSend[].
1241//-----------------------------------------------------------------------------
1242static void TransmitIClassCommand(const uint8_t *cmd, int len, int *samples, int *wait)
1243{
1244 int c;
1245
1246 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1247 AT91C_BASE_SSC->SSC_THR = 0x00;
1248 FpgaSetupSsc();
1249
1250 if (wait)
1251 if(*wait < 10)
1252 *wait = 10;
1253
1254 for(c = 0; c < *wait;) {
1255 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1256 AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing!
1257 c++;
1258 }
1259 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1260 volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
1261 (void)r;
1262 }
1263 WDT_HIT();
1264 }
1265
1266 uint8_t sendbyte;
1267 bool firstpart = TRUE;
1268 c = 0;
1269 for(;;) {
1270 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1271
1272 // DOUBLE THE SAMPLES!
1273 if(firstpart) {
1274 sendbyte = (cmd[c] & 0xf0) | (cmd[c] >> 4);
1275 }
1276 else {
1277 sendbyte = (cmd[c] & 0x0f) | (cmd[c] << 4);
1278 c++;
1279 }
1280 if(sendbyte == 0xff) {
1281 sendbyte = 0xfe;
1282 }
1283 AT91C_BASE_SSC->SSC_THR = sendbyte;
1284 firstpart = !firstpart;
1285
1286 if(c >= len) {
1287 break;
1288 }
1289 }
1290 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1291 volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
1292 (void)r;
1293 }
1294 WDT_HIT();
1295 }
1296 if (samples) *samples = (c + *wait) << 3;
1297}
1298
1299
1300//-----------------------------------------------------------------------------
1301// Prepare iClass reader command to send to FPGA
1302//-----------------------------------------------------------------------------
1303void CodeIClassCommand(const uint8_t * cmd, int len)
1304{
1305 int i, j, k;
1306 uint8_t b;
1307
1308 ToSendReset();
1309
1310 // Start of Communication: 1 out of 4
1311 ToSend[++ToSendMax] = 0xf0;
1312 ToSend[++ToSendMax] = 0x00;
1313 ToSend[++ToSendMax] = 0x0f;
1314 ToSend[++ToSendMax] = 0x00;
1315
1316 // Modulate the bytes
1317 for (i = 0; i < len; i++) {
1318 b = cmd[i];
1319 for(j = 0; j < 4; j++) {
1320 for(k = 0; k < 4; k++) {
1321 if(k == (b & 3)) {
1322 ToSend[++ToSendMax] = 0x0f;
1323 }
1324 else {
1325 ToSend[++ToSendMax] = 0x00;
1326 }
1327 }
1328 b >>= 2;
1329 }
1330 }
1331
1332 // End of Communication
1333 ToSend[++ToSendMax] = 0x00;
1334 ToSend[++ToSendMax] = 0x00;
1335 ToSend[++ToSendMax] = 0xf0;
1336 ToSend[++ToSendMax] = 0x00;
1337
1338 // Convert from last character reference to length
1339 ToSendMax++;
1340}
1341
1342void ReaderTransmitIClass(uint8_t* frame, int len)
1343{
1344 int wait = 0;
1345 int samples = 0;
1346 int par = 0;
1347
1348 // This is tied to other size changes
1349 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1350 CodeIClassCommand(frame,len);
1351
1352 // Select the card
1353 TransmitIClassCommand(ToSend, ToSendMax, &samples, &wait);
1354 if(trigger)
1355 LED_A_ON();
1356
1357 // Store reader command in buffer
7bc95e2e 1358 if (tracing) LogTrace(frame,len,rsamples,par,TRUE);
1e262141 1359}
1360
1361//-----------------------------------------------------------------------------
1362// Wait a certain time for tag response
1363// If a response is captured return TRUE
1364// If it takes too long return FALSE
1365//-----------------------------------------------------------------------------
1366static int GetIClassAnswer(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) //uint8_t *buffer
1367{
1368 // buffer needs to be 512 bytes
1369 int c;
1370
1371 // Set FPGA mode to "reader listen mode", no modulation (listen
1372 // only, since we are receiving, not transmitting).
1373 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_LISTEN);
1374
1375 // Now get the answer from the card
1376 Demod.output = receivedResponse;
1377 Demod.len = 0;
1378 Demod.state = DEMOD_UNSYNCD;
1379
1380 uint8_t b;
1381 if (elapsed) *elapsed = 0;
1382
1383 bool skip = FALSE;
1384
1385 c = 0;
1386 for(;;) {
1387 WDT_HIT();
1388
1389 if(BUTTON_PRESS()) return FALSE;
1390
1391 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
1392 AT91C_BASE_SSC->SSC_THR = 0x00; // To make use of exact timing of next command from reader!!
1393 if (elapsed) (*elapsed)++;
1394 }
1395 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
1396 if(c < timeout) { c++; } else { return FALSE; }
1397 b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
1398 skip = !skip;
1399 if(skip) continue;
1400 /*if(ManchesterDecoding((b>>4) & 0xf)) {
1401 *samples = ((c - 1) << 3) + 4;
1402 return TRUE;
1403 }*/
1404 if(ManchesterDecoding(b & 0x0f)) {
1405 *samples = c << 3;
1406 return TRUE;
1407 }
1408 }
1409 }
1410}
1411
1412int ReaderReceiveIClass(uint8_t* receivedAnswer)
1413{
1414 int samples = 0;
1415 if (!GetIClassAnswer(receivedAnswer,160,&samples,0)) return FALSE;
7bc95e2e 1416 rsamples += samples;
1417 if (tracing) LogTrace(receivedAnswer,Demod.len,rsamples,Demod.parityBits,FALSE);
1e262141 1418 if(samples == 0) return FALSE;
1419 return Demod.len;
1420}
1421
1422// Reader iClass Anticollission
1423void ReaderIClass(uint8_t arg0) {
1e262141 1424 uint8_t act_all[] = { 0x0a };
1425 uint8_t identify[] = { 0x0c };
4ab4336a 1426 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1e262141 1427
1428 uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
1429
1430 // Reset trace buffer
ff7bb4ef 1431 memset(trace, 0x44, RECV_CMD_OFFSET);
1e262141 1432 traceLen = 0;
1433
1434 // Setup SSC
1435 FpgaSetupSsc();
1436 // Start from off (no field generated)
1437 // Signal field is off with the appropriate LED
cee5a30d 1438 LED_D_OFF();
1e262141 1439 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1440 SpinDelay(200);
1441
1442 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1443
1444 // Now give it time to spin up.
1445 // Signal field is on with the appropriate LED
1446 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1447 SpinDelay(200);
1448
1449 LED_A_ON();
1450
1451 for(;;) {
4ab4336a 1452
1453 if(traceLen > TRACE_SIZE) {
1454 DbpString("Trace full");
1455 break;
1456 }
1457
1458 if (BUTTON_PRESS()) break;
1e262141 1459
1460 // Send act_all
1461 ReaderTransmitIClass(act_all, 1);
1462 // Card present?
1463 if(ReaderReceiveIClass(resp)) {
1464 ReaderTransmitIClass(identify, 1);
4ab4336a 1465 if(ReaderReceiveIClass(resp) == 10) {
1466 // Select card
1467 memcpy(&select[1],resp,8);
1468 ReaderTransmitIClass(select, sizeof(select));
1469
1470 if(ReaderReceiveIClass(resp) == 10) {
1471 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1472 resp[0], resp[1], resp[2],
1473 resp[3], resp[4], resp[5],
1474 resp[6], resp[7]);
1475 }
1476 // Card selected, whats next... ;-)
1e262141 1477 }
1478 }
1479 WDT_HIT();
1480 }
1481
1482 LED_A_OFF();
cee5a30d 1483}
1484
912a3e94 1485
Impressum, Datenschutz