1 //-----------------------------------------------------------------------------
2 // Gerhard de Koning Gans - May 2008
3 // Hagen Fritsch - June 2010
4 // Gerhard de Koning Gans - May 2011
5 // Gerhard de Koning Gans - June 2012 - Added iClass card and reader emulation
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
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
16 // Please feel free to contribute and extend iClass support!!
17 //-----------------------------------------------------------------------------
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:
24 // + 22279: : 0c 03 e8 01
26 // ...with an incorrect answer...
28 // + 85: 0: TAG ff! ff! ff! ff! ff! ff! ff! ff! bb 33 bb 00 01! 0e! 04! bb !crc
30 // We still left the error signalling bytes in the traces like 0xbb
32 // A correct trace should look like this:
34 // + 21112: : 0c 03 e8 01
35 // + 85: 0: TAG ff ff ff ff ff ff ff ff ea f5
37 //-----------------------------------------------------------------------------
39 #include "proxmark3.h"
44 // Needed for CRC in emulation mode;
45 // same construction as in ISO 14443;
46 // different initial value (CRC_ICLASS)
47 #include "iso14443crc.h"
49 static int timeout
= 4096;
52 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
);
54 //-----------------------------------------------------------------------------
55 // The software UART that receives commands from the reader, and its state
57 //-----------------------------------------------------------------------------
61 STATE_START_OF_COMMUNICATION
,
82 static RAMFUNC
int OutOfNDecoding(int bit
)
88 Uart
.bitBuffer
= bit
^ 0xFF0;
93 Uart
.bitBuffer
^= bit
;
97 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
100 if(Uart.byteCnt > 15) { return TRUE; }
106 if(Uart
.state
!= STATE_UNSYNCD
) {
109 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
115 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
121 if(bit
!= bitright
) { bit
= bitright
; }
124 // So, now we only have to deal with *bit*, lets see...
125 if(Uart
.posCnt
== 1) {
126 // measurement first half bitperiod
128 // Drop in first half means that we are either seeing
131 if(Uart
.nOutOfCnt
== 1) {
132 // End of Communication
133 Uart
.state
= STATE_UNSYNCD
;
135 if(Uart
.byteCnt
== 0) {
136 // Its not straightforward to show single EOFs
137 // So just leave it and do not return TRUE
138 Uart
.output
[Uart
.byteCnt
] = 0xf0;
141 // Calculate the parity bit for the client...
148 else if(Uart
.state
!= STATE_START_OF_COMMUNICATION
) {
149 // When not part of SOF or EOF, it is an error
150 Uart
.state
= STATE_UNSYNCD
;
157 // measurement second half bitperiod
158 // Count the bitslot we are in... (ISO 15693)
162 if(Uart
.dropPosition
) {
163 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
169 // It is an error if we already have seen a drop in current frame
170 Uart
.state
= STATE_UNSYNCD
;
174 Uart
.dropPosition
= Uart
.nOutOfCnt
;
181 if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
&& Uart
.OutOfCnt
== 4) {
184 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
185 if(Uart
.dropPosition
== 4) {
186 Uart
.state
= STATE_RECEIVING
;
189 else if(Uart
.dropPosition
== 3) {
190 Uart
.state
= STATE_RECEIVING
;
192 //Uart.output[Uart.byteCnt] = 0xdd;
196 Uart
.state
= STATE_UNSYNCD
;
199 Uart
.dropPosition
= 0;
204 if(!Uart
.dropPosition
) {
205 Uart
.state
= STATE_UNSYNCD
;
214 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
215 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
217 Uart
.shiftReg
^= ((Uart
.dropPosition
& 0x03) << 6);
219 Uart
.dropPosition
= 0;
221 if(Uart
.bitCnt
== 8) {
222 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
225 // Calculate the parity bit for the client...
226 Uart
.parityBits
<<= 1;
227 Uart
.parityBits
^= OddByteParity
[(Uart
.shiftReg
& 0xff)];
235 else if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
) {
238 if(!Uart
.dropPosition
) {
239 Uart
.state
= STATE_UNSYNCD
;
245 Uart
.output
[Uart
.byteCnt
] = (Uart
.dropPosition
& 0xff);
248 // Calculate the parity bit for the client...
249 Uart
.parityBits
<<= 1;
250 Uart
.parityBits
^= OddByteParity
[(Uart
.dropPosition
& 0xff)];
255 Uart
.dropPosition
= 0;
260 Uart.output[Uart.byteCnt] = 0xAA;
262 Uart.output[Uart.byteCnt] = error & 0xFF;
264 Uart.output[Uart.byteCnt] = 0xAA;
266 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
268 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
270 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
272 Uart.output[Uart.byteCnt] = 0xAA;
280 bit
= Uart
.bitBuffer
& 0xf0;
282 bit
^= 0x0F; // drops become 1s ;-)
284 // should have been high or at least (4 * 128) / fc
285 // according to ISO this should be at least (9 * 128 + 20) / fc
286 if(Uart
.highCnt
== 8) {
287 // we went low, so this could be start of communication
288 // it turns out to be safer to choose a less significant
289 // syncbit... so we check whether the neighbour also represents the drop
290 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
291 Uart
.syncBit
= bit
& 8;
293 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
294 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
295 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
296 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
297 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
298 if(Uart
.syncBit
&& (Uart
.bitBuffer
& 8)) {
301 // the first half bit period is expected in next sample
306 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
309 Uart
.state
= STATE_START_OF_COMMUNICATION
;
314 Uart
.OutOfCnt
= 4; // Start at 1/4, could switch to 1/256
315 Uart
.dropPosition
= 0;
324 if(Uart
.highCnt
< 8) {
333 //=============================================================================
335 //=============================================================================
340 DEMOD_START_OF_COMMUNICATION
,
341 DEMOD_START_OF_COMMUNICATION2
,
342 DEMOD_START_OF_COMMUNICATION3
,
346 DEMOD_END_OF_COMMUNICATION
,
347 DEMOD_END_OF_COMMUNICATION2
,
371 static RAMFUNC
int ManchesterDecoding(int v
)
378 Demod
.buffer
= Demod
.buffer2
;
379 Demod
.buffer2
= Demod
.buffer3
;
387 if(Demod
.state
==DEMOD_UNSYNCD
) {
388 Demod
.output
[Demod
.len
] = 0xfa;
391 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
394 Demod
.syncBit
= 0x08;
401 Demod
.syncBit
= 0x04;
408 Demod
.syncBit
= 0x02;
411 if(bit
& 0x01 && Demod
.syncBit
) {
412 Demod
.syncBit
= 0x01;
417 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
418 Demod
.sub
= SUB_FIRST_HALF
;
421 Demod
.parityBits
= 0;
424 //if(trigger) LED_A_OFF(); // Not useful in this case...
425 switch(Demod
.syncBit
) {
426 case 0x08: Demod
.samples
= 3; break;
427 case 0x04: Demod
.samples
= 2; break;
428 case 0x02: Demod
.samples
= 1; break;
429 case 0x01: Demod
.samples
= 0; break;
431 // SOF must be long burst... otherwise stay unsynced!!!
432 if(!(Demod
.buffer
& Demod
.syncBit
) || !(Demod
.buffer2
& Demod
.syncBit
)) {
433 Demod
.state
= DEMOD_UNSYNCD
;
437 // SOF must be long burst... otherwise stay unsynced!!!
438 if(!(Demod
.buffer2
& Demod
.syncBit
) || !(Demod
.buffer3
& Demod
.syncBit
)) {
439 Demod
.state
= DEMOD_UNSYNCD
;
449 modulation
= bit
& Demod
.syncBit
;
450 modulation
|= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
451 //modulation = ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
455 if(Demod
.posCount
==0) {
458 Demod
.sub
= SUB_FIRST_HALF
;
461 Demod
.sub
= SUB_NONE
;
466 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
467 if(Demod.state!=DEMOD_ERROR_WAIT) {
468 Demod.state = DEMOD_ERROR_WAIT;
469 Demod.output[Demod.len] = 0xaa;
473 //else if(modulation) {
475 if(Demod
.sub
== SUB_FIRST_HALF
) {
476 Demod
.sub
= SUB_BOTH
;
479 Demod
.sub
= SUB_SECOND_HALF
;
482 else if(Demod
.sub
== SUB_NONE
) {
483 if(Demod
.state
== DEMOD_SOF_COMPLETE
) {
484 Demod
.output
[Demod
.len
] = 0x0f;
486 Demod
.parityBits
<<= 1;
487 Demod
.parityBits
^= OddByteParity
[0x0f];
488 Demod
.state
= DEMOD_UNSYNCD
;
493 Demod
.state
= DEMOD_ERROR_WAIT
;
496 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
497 Demod.state = DEMOD_ERROR_WAIT;
498 Demod.output[Demod.len] = 0xaa;
503 switch(Demod
.state
) {
504 case DEMOD_START_OF_COMMUNICATION
:
505 if(Demod
.sub
== SUB_BOTH
) {
506 //Demod.state = DEMOD_MANCHESTER_D;
507 Demod
.state
= DEMOD_START_OF_COMMUNICATION2
;
509 Demod
.sub
= SUB_NONE
;
512 Demod
.output
[Demod
.len
] = 0xab;
513 Demod
.state
= DEMOD_ERROR_WAIT
;
517 case DEMOD_START_OF_COMMUNICATION2
:
518 if(Demod
.sub
== SUB_SECOND_HALF
) {
519 Demod
.state
= DEMOD_START_OF_COMMUNICATION3
;
522 Demod
.output
[Demod
.len
] = 0xab;
523 Demod
.state
= DEMOD_ERROR_WAIT
;
527 case DEMOD_START_OF_COMMUNICATION3
:
528 if(Demod
.sub
== SUB_SECOND_HALF
) {
529 // Demod.state = DEMOD_MANCHESTER_D;
530 Demod
.state
= DEMOD_SOF_COMPLETE
;
531 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
535 Demod
.output
[Demod
.len
] = 0xab;
536 Demod
.state
= DEMOD_ERROR_WAIT
;
540 case DEMOD_SOF_COMPLETE
:
541 case DEMOD_MANCHESTER_D
:
542 case DEMOD_MANCHESTER_E
:
543 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
544 // 00001111 = 1 (0 in 14443)
545 if(Demod
.sub
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF
547 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
548 Demod
.state
= DEMOD_MANCHESTER_D
;
550 else if(Demod
.sub
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF
552 Demod
.shiftReg
>>= 1;
553 Demod
.state
= DEMOD_MANCHESTER_E
;
555 else if(Demod
.sub
== SUB_BOTH
) {
556 Demod
.state
= DEMOD_MANCHESTER_F
;
559 Demod
.state
= DEMOD_ERROR_WAIT
;
564 case DEMOD_MANCHESTER_F
:
565 // Tag response does not need to be a complete byte!
566 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
567 if(Demod
.bitCount
> 1) { // was > 0, do not interpret last closing bit, is part of EOF
568 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
569 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
571 // No parity bit, so just shift a 0
572 Demod
.parityBits
<<= 1;
575 Demod
.state
= DEMOD_UNSYNCD
;
579 Demod
.output
[Demod
.len
] = 0xad;
580 Demod
.state
= DEMOD_ERROR_WAIT
;
585 case DEMOD_ERROR_WAIT
:
586 Demod
.state
= DEMOD_UNSYNCD
;
590 Demod
.output
[Demod
.len
] = 0xdd;
591 Demod
.state
= DEMOD_UNSYNCD
;
595 /*if(Demod.bitCount>=9) {
596 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
599 Demod.parityBits <<= 1;
600 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
605 if(Demod
.bitCount
>=8) {
606 Demod
.shiftReg
>>= 1;
607 Demod
.output
[Demod
.len
] = (Demod
.shiftReg
& 0xff);
610 // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT
611 Demod
.parityBits
<<= 1;
612 Demod
.parityBits
^= OddByteParity
[(Demod
.shiftReg
& 0xff)];
619 Demod
.output
[Demod
.len
] = 0xBB;
621 Demod
.output
[Demod
.len
] = error
& 0xFF;
623 Demod
.output
[Demod
.len
] = 0xBB;
625 Demod
.output
[Demod
.len
] = bit
& 0xFF;
627 Demod
.output
[Demod
.len
] = Demod
.buffer
& 0xFF;
630 Demod
.output
[Demod
.len
] = Demod
.buffer2
& 0xFF;
632 Demod
.output
[Demod
.len
] = Demod
.syncBit
& 0xFF;
634 Demod
.output
[Demod
.len
] = 0xBB;
641 } // end (state != UNSYNCED)
646 //=============================================================================
647 // Finally, a `sniffer' for iClass communication
648 // Both sides of communication!
649 //=============================================================================
651 //-----------------------------------------------------------------------------
652 // Record the sequence of commands sent by the reader to the tag, with
653 // triggering so that we start recording at the point that the tag is moved
655 //-----------------------------------------------------------------------------
656 void RAMFUNC
SnoopIClass(void)
660 // We won't start recording the frames that we acquire until we trigger;
661 // a good trigger condition to get started is probably when we see a
662 // response from the tag.
663 //int triggered = FALSE; // FALSE to wait first for card
665 // The command (reader -> tag) that we're receiving.
666 // The length of a received command will in most cases be no more than 18 bytes.
667 // So 32 should be enough!
668 uint8_t *readerToTagCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
669 // The response (tag -> reader) that we're receiving.
670 uint8_t *tagToReaderResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
672 // reset traceLen to 0
673 iso14a_set_tracing(TRUE
);
674 iso14a_clear_trace();
675 iso14a_set_trigger(FALSE
);
677 // The DMA buffer, used to stream samples from the FPGA
678 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
684 // Count of samples received so far, so that we can include timing
685 // information in the trace buffer.
689 // Set up the demodulator for tag -> reader responses.
690 Demod
.output
= tagToReaderResponse
;
692 Demod
.state
= DEMOD_UNSYNCD
;
694 // Setup for the DMA.
697 lastRxCounter
= DMA_BUFFER_SIZE
;
698 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
700 // And the reader -> tag commands
701 memset(&Uart
, 0, sizeof(Uart
));
702 Uart
.output
= readerToTagCmd
;
703 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
704 Uart
.state
= STATE_UNSYNCD
;
706 // And put the FPGA in the appropriate mode
707 // Signal field is off with the appropriate LED
709 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
710 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
712 uint32_t time_0
= GetCountSspClk();
720 // And now we loop, receiving samples.
724 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
726 if(behindBy
> maxBehindBy
) {
727 maxBehindBy
= behindBy
;
729 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
733 if(behindBy
< 1) continue;
739 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
740 upTo
-= DMA_BUFFER_SIZE
;
741 lastRxCounter
+= DMA_BUFFER_SIZE
;
742 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
743 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
750 decbyte
^= (1 << (3 - div
));
753 // FOR READER SIDE COMMUMICATION...
756 decbyter
^= (smpl
& 0x30);
760 if((div
+ 1) % 2 == 0) {
762 if(OutOfNDecoding((smpl
& 0xF0) >> 4)) {
763 rsamples
= samples
- Uart
.samples
;
766 //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
767 //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
770 LogTrace(Uart
.output
,Uart
.byteCnt
, (GetCountSspClk()-time_0
) << 4, Uart
.parityBits
,TRUE
);
771 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, TRUE
);
775 /* And ready to receive another command. */
776 Uart
.state
= STATE_UNSYNCD
;
777 /* And also reset the demod code, which might have been */
778 /* false-triggered by the commands from the reader. */
779 Demod
.state
= DEMOD_UNSYNCD
;
788 if(ManchesterDecoding(smpl
& 0x0F)) {
789 rsamples
= samples
- Demod
.samples
;
794 LogTrace(Demod
.output
,Demod
.len
, (GetCountSspClk()-time_0
) << 4 , Demod
.parityBits
,FALSE
);
795 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, FALSE
);
799 // And ready to receive another response.
800 memset(&Demod
, 0, sizeof(Demod
));
801 Demod
.output
= tagToReaderResponse
;
802 Demod
.state
= DEMOD_UNSYNCD
;
812 DbpString("cancelled_a");
817 DbpString("COMMAND FINISHED");
819 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
820 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
823 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
824 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
825 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
832 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) {
834 for(i
= 0; i
< 8; i
++) {
835 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8] << 5);
839 //-----------------------------------------------------------------------------
840 // Wait for commands from reader
841 // Stop when button is pressed
842 // Or return TRUE when command is captured
843 //-----------------------------------------------------------------------------
844 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
846 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
847 // only, since we are receiving, not transmitting).
848 // Signal field is off with the appropriate LED
850 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
852 // Now run a `software UART' on the stream of incoming samples.
853 Uart
.output
= received
;
854 Uart
.byteCntMax
= maxLen
;
855 Uart
.state
= STATE_UNSYNCD
;
860 if(BUTTON_PRESS()) return FALSE
;
862 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
863 AT91C_BASE_SSC
->SSC_THR
= 0x00;
865 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
866 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
867 /*if(OutOfNDecoding((b & 0xf0) >> 4)) {
871 if(OutOfNDecoding(b
& 0x0f)) {
880 //-----------------------------------------------------------------------------
881 // Prepare tag messages
882 //-----------------------------------------------------------------------------
883 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
)
885 //So far a dummy implementation, not used
886 //int lastProxToAirDuration =0;
892 ToSend
[++ToSendMax
] = 0x00;
893 ToSend
[++ToSendMax
] = 0x00;
894 ToSend
[++ToSendMax
] = 0x00;
895 ToSend
[++ToSendMax
] = 0xff;//Proxtoair duration starts here
896 ToSend
[++ToSendMax
] = 0xff;
897 ToSend
[++ToSendMax
] = 0xff;
898 ToSend
[++ToSendMax
] = 0x00;
899 ToSend
[++ToSendMax
] = 0xff;
901 for(i
= 0; i
< len
; i
++) {
906 for(j
= 0; j
< 8; j
++) {
908 ToSend
[++ToSendMax
] = 0x00;
909 ToSend
[++ToSendMax
] = 0xff;
911 ToSend
[++ToSendMax
] = 0xff;
912 ToSend
[++ToSendMax
] = 0x00;
919 ToSend
[++ToSendMax
] = 0xff;
920 ToSend
[++ToSendMax
] = 0x00;
921 ToSend
[++ToSendMax
] = 0xff;
922 ToSend
[++ToSendMax
] = 0xff;
923 ToSend
[++ToSendMax
] = 0xff;
924 ToSend
[++ToSendMax
] = 0x00;
925 ToSend
[++ToSendMax
] = 0x00;
926 ToSend
[++ToSendMax
] = 0x00;
928 //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end
930 // Convert from last byte pos to length
935 static void CodeIClassTagSOF()
937 //So far a dummy implementation, not used
938 //int lastProxToAirDuration =0;
942 ToSend
[++ToSendMax
] = 0x00;
943 ToSend
[++ToSendMax
] = 0x00;
944 ToSend
[++ToSendMax
] = 0x00;
945 ToSend
[++ToSendMax
] = 0xff;
946 ToSend
[++ToSendMax
] = 0xff;
947 ToSend
[++ToSendMax
] = 0xff;
948 ToSend
[++ToSendMax
] = 0x00;
949 ToSend
[++ToSendMax
] = 0xff;
951 // lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning
954 // Convert from last byte pos to length
957 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
);
959 * @brief SimulateIClass simulates an iClass card.
960 * @param arg0 type of simulation
961 * - 0 uses the first 8 bytes in usb data as CSN
962 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
963 * in the usb data. This mode collects MAC from the reader, in order to do an offline
964 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
965 * - Other : Uses the default CSN (031fec8af7ff12e0)
966 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
970 void SimulateIClass(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
)
972 uint32_t simType
= arg0
;
973 uint32_t numberOfCSNS
= arg1
;
975 // Enable and clear the trace
976 iso14a_set_tracing(TRUE
);
977 iso14a_clear_trace();
979 uint8_t csn_crc
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
981 // Use the CSN from commandline
982 memcpy(csn_crc
, datain
, 8);
983 doIClassSimulation(csn_crc
,0,NULL
);
984 }else if(simType
== 1)
986 doIClassSimulation(csn_crc
,0,NULL
);
988 else if(simType
== 2)
991 uint8_t mac_responses
[64] = { 0 };
992 Dbprintf("Going into attack mode");
993 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
994 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
995 // in order to obtain the keys, as in the "dismantling iclass"-paper.
997 for( ; i
< numberOfCSNS
&& i
*8+8 < USB_CMD_DATA_SIZE
; i
++)
999 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
1001 memcpy(csn_crc
, datain
+(i
*8), 8);
1002 if(doIClassSimulation(csn_crc
,1,mac_responses
))
1004 return; // Button pressed
1007 cmd_send(CMD_ACK
,CMD_SIMULATE_TAG_ICLASS
,i
,0,mac_responses
,i
*8);
1011 // We may want a mode here where we hardcode the csns to use (from proxclone).
1012 // That will speed things up a little, but not required just yet.
1013 Dbprintf("The mode is not implemented, reserved for future use");
1015 Dbprintf("Done...");
1019 * @brief Does the actual simulation
1020 * @param csn - csn to use
1021 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1023 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
)
1026 // CSN followed by two CRC bytes
1027 uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1028 uint8_t response3
[] = { 0,0,0,0,0,0,0,0,0,0};
1029 memcpy(response3
,csn
,sizeof(response3
));
1030 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]);
1032 uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1034 // Construct anticollision-CSN
1035 rotateCSN(response3
,response2
);
1037 // Compute CRC on both CSNs
1038 ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]);
1039 ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]);
1045 // Tag anticoll. CSN
1046 // Reader 81 anticoll. CSN
1051 uint8_t* respdata
= NULL
;
1055 // Respond SOF -- takes 8 bytes
1056 uint8_t *resp1
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
);
1059 // Anticollision CSN (rotated CSN)
1060 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1061 uint8_t *resp2
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 10);
1065 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1066 uint8_t *resp3
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 190);
1070 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
1071 uint8_t *resp4
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 370);
1075 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
1076 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1079 // Prepare card messages
1082 // First card answer: SOF
1084 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1086 // Anticollision CSN
1087 CodeIClassTagAnswer(response2
, sizeof(response2
));
1088 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1091 CodeIClassTagAnswer(response3
, sizeof(response3
));
1092 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1095 CodeIClassTagAnswer(response4
, sizeof(response4
));
1096 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1099 // Start from off (no field generated)
1100 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1102 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
1105 // We need to listen to the high-frequency, peak-detected path.
1106 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1109 // To control where we are in the protocol
1111 uint32_t time_0
= GetCountSspClk();
1112 uint32_t t2r_time
=0;
1113 uint32_t r2t_time
=0;
1116 bool buttonPressed
= false;
1118 /** Hack for testing
1119 memcpy(reader_mac_buf,csn,8);
1127 // Can be used to get a trigger for an oscilloscope..
1130 if(!GetIClassCommandFromReader(receivedCmd
, &len
, 100)) {
1131 buttonPressed
= true;
1134 r2t_time
= GetCountSspClk();
1138 // Okay, look at the command now.
1139 if(receivedCmd
[0] == 0x0a ) {
1140 // Reader in anticollission phase
1141 resp
= resp1
; respLen
= resp1Len
; //order = 1;
1143 respsize
= sizeof(sof
);
1144 } else if(receivedCmd
[0] == 0x0c) {
1145 // Reader asks for anticollission CSN
1146 resp
= resp2
; respLen
= resp2Len
; //order = 2;
1147 respdata
= response2
;
1148 respsize
= sizeof(response2
);
1149 //DbpString("Reader requests anticollission CSN:");
1150 } else if(receivedCmd
[0] == 0x81) {
1151 // Reader selects anticollission CSN.
1152 // Tag sends the corresponding real CSN
1153 resp
= resp3
; respLen
= resp3Len
; //order = 3;
1154 respdata
= response3
;
1155 respsize
= sizeof(response3
);
1156 //DbpString("Reader selects anticollission CSN:");
1157 } else if(receivedCmd
[0] == 0x88) {
1158 // Read e-purse (88 02)
1159 resp
= resp4
; respLen
= resp4Len
; //order = 4;
1160 respdata
= response4
;
1161 respsize
= sizeof(response4
);
1163 } else if(receivedCmd
[0] == 0x05) {
1164 // Reader random and reader MAC!!!
1166 // We do not know what to answer, so lets keep quit
1167 resp
= resp1
; respLen
= 0; //order = 5;
1170 if (breakAfterMacReceived
){
1171 // TODO, actually return this to the caller instead of just
1173 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]);
1174 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len
,
1175 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1176 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1177 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1178 if (reader_mac_buf
!= NULL
)
1180 memcpy(reader_mac_buf
,receivedCmd
+1,8);
1184 } else if(receivedCmd
[0] == 0x00 && len
== 1) {
1185 // Reader ends the session
1186 resp
= resp1
; respLen
= 0; //order = 0;
1190 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1191 // Never seen this command before
1192 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1194 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1195 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1196 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1198 resp
= resp1
; respLen
= 0; //order = 0;
1203 if(cmdsRecvd
> 100) {
1204 //DbpString("100 commands later...");
1212 SendIClassAnswer(resp
, respLen
, 21);
1213 t2r_time
= GetCountSspClk();
1217 LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, Uart
.parityBits
,TRUE
);
1218 LogTrace(NULL
,0, (r2t_time
-time_0
) << 4, 0,TRUE
);
1220 if (respdata
!= NULL
) {
1221 LogTrace(respdata
,respsize
, (t2r_time
-time_0
) << 4,SwapBits(GetParity(respdata
,respsize
),respsize
),FALSE
);
1222 LogTrace(NULL
,0, (t2r_time
-time_0
) << 4,0,FALSE
);
1227 DbpString("Trace full");
1232 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1235 //Dbprintf("%x", cmdsRecvd);
1240 DbpString("Button pressed");
1242 return buttonPressed
;
1245 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1247 int i
= 0, d
=0;//, u = 0, d = 0;
1250 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
1252 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1254 while(!BUTTON_PRESS()) {
1255 if((AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)){
1256 b
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
;
1258 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)){
1271 AT91C_BASE_SSC
->SSC_THR
= b
;
1274 if (i
> respLen
+4) break;
1282 //-----------------------------------------------------------------------------
1283 // Transmit the command (to the tag) that was placed in ToSend[].
1284 //-----------------------------------------------------------------------------
1285 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1288 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1289 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1296 for(c
= 0; c
< *wait
;) {
1297 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1298 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1301 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1302 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1309 bool firstpart
= TRUE
;
1312 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1314 // DOUBLE THE SAMPLES!
1316 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1319 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1322 if(sendbyte
== 0xff) {
1325 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1326 firstpart
= !firstpart
;
1332 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1333 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1338 if (samples
) *samples
= (c
+ *wait
) << 3;
1342 //-----------------------------------------------------------------------------
1343 // Prepare iClass reader command to send to FPGA
1344 //-----------------------------------------------------------------------------
1345 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1352 // Start of Communication: 1 out of 4
1353 ToSend
[++ToSendMax
] = 0xf0;
1354 ToSend
[++ToSendMax
] = 0x00;
1355 ToSend
[++ToSendMax
] = 0x0f;
1356 ToSend
[++ToSendMax
] = 0x00;
1358 // Modulate the bytes
1359 for (i
= 0; i
< len
; i
++) {
1361 for(j
= 0; j
< 4; j
++) {
1362 for(k
= 0; k
< 4; k
++) {
1364 ToSend
[++ToSendMax
] = 0x0f;
1367 ToSend
[++ToSendMax
] = 0x00;
1374 // End of Communication
1375 ToSend
[++ToSendMax
] = 0x00;
1376 ToSend
[++ToSendMax
] = 0x00;
1377 ToSend
[++ToSendMax
] = 0xf0;
1378 ToSend
[++ToSendMax
] = 0x00;
1380 // Convert from last character reference to length
1384 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1390 // This is tied to other size changes
1391 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1392 CodeIClassCommand(frame
,len
);
1395 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1399 // Store reader command in buffer
1400 if (tracing
) LogTrace(frame
,len
,rsamples
,par
,TRUE
);
1403 //-----------------------------------------------------------------------------
1404 // Wait a certain time for tag response
1405 // If a response is captured return TRUE
1406 // If it takes too long return FALSE
1407 //-----------------------------------------------------------------------------
1408 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1410 // buffer needs to be 512 bytes
1413 // Set FPGA mode to "reader listen mode", no modulation (listen
1414 // only, since we are receiving, not transmitting).
1415 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1417 // Now get the answer from the card
1418 Demod
.output
= receivedResponse
;
1420 Demod
.state
= DEMOD_UNSYNCD
;
1423 if (elapsed
) *elapsed
= 0;
1431 if(BUTTON_PRESS()) return FALSE
;
1433 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1434 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1435 if (elapsed
) (*elapsed
)++;
1437 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1438 if(c
< timeout
) { c
++; } else { return FALSE
; }
1439 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1442 /*if(ManchesterDecoding((b>>4) & 0xf)) {
1443 *samples = ((c - 1) << 3) + 4;
1446 if(ManchesterDecoding(b
& 0x0f)) {
1454 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1457 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1458 rsamples
+= samples
;
1459 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,rsamples
,Demod
.parityBits
,FALSE
);
1460 if(samples
== 0) return FALSE
;
1464 // Reader iClass Anticollission
1465 void ReaderIClass(uint8_t arg0
) {
1466 uint8_t act_all
[] = { 0x0a };
1467 uint8_t identify
[] = { 0x0c };
1468 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1470 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1472 // Reset trace buffer
1473 memset(trace
, 0x44, RECV_CMD_OFFSET
);
1478 // Start from off (no field generated)
1479 // Signal field is off with the appropriate LED
1481 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1484 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1486 // Now give it time to spin up.
1487 // Signal field is on with the appropriate LED
1488 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1495 if(traceLen
> TRACE_SIZE
) {
1496 DbpString("Trace full");
1500 if (BUTTON_PRESS()) break;
1503 ReaderTransmitIClass(act_all
, 1);
1505 if(ReaderReceiveIClass(resp
)) {
1506 ReaderTransmitIClass(identify
, 1);
1507 if(ReaderReceiveIClass(resp
) == 10) {
1509 memcpy(&select
[1],resp
,8);
1510 ReaderTransmitIClass(select
, sizeof(select
));
1512 if(ReaderReceiveIClass(resp
) == 10) {
1513 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1514 resp
[0], resp
[1], resp
[2],
1515 resp
[3], resp
[4], resp
[5],
1518 // Card selected, whats next... ;-)