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 "../include/proxmark3.h"
45 // Needed for CRC in emulation mode;
46 // same construction as in ISO 14443;
47 // different initial value (CRC_ICLASS)
48 #include "../common/iso14443crc.h"
49 #include "../common/iso15693tools.h"
52 static int timeout
= 4096;
55 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
);
57 //-----------------------------------------------------------------------------
58 // The software UART that receives commands from the reader, and its state
60 //-----------------------------------------------------------------------------
64 STATE_START_OF_COMMUNICATION
,
85 static RAMFUNC
int OutOfNDecoding(int bit
)
91 Uart
.bitBuffer
= bit
^ 0xFF0;
96 Uart
.bitBuffer
^= bit
;
100 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
103 if(Uart.byteCnt > 15) { return TRUE; }
109 if(Uart
.state
!= STATE_UNSYNCD
) {
112 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
118 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
124 if(bit
!= bitright
) { bit
= bitright
; }
127 // So, now we only have to deal with *bit*, lets see...
128 if(Uart
.posCnt
== 1) {
129 // measurement first half bitperiod
131 // Drop in first half means that we are either seeing
134 if(Uart
.nOutOfCnt
== 1) {
135 // End of Communication
136 Uart
.state
= STATE_UNSYNCD
;
138 if(Uart
.byteCnt
== 0) {
139 // Its not straightforward to show single EOFs
140 // So just leave it and do not return TRUE
141 Uart
.output
[Uart
.byteCnt
] = 0xf0;
144 // Calculate the parity bit for the client...
151 else if(Uart
.state
!= STATE_START_OF_COMMUNICATION
) {
152 // When not part of SOF or EOF, it is an error
153 Uart
.state
= STATE_UNSYNCD
;
160 // measurement second half bitperiod
161 // Count the bitslot we are in... (ISO 15693)
165 if(Uart
.dropPosition
) {
166 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
172 // It is an error if we already have seen a drop in current frame
173 Uart
.state
= STATE_UNSYNCD
;
177 Uart
.dropPosition
= Uart
.nOutOfCnt
;
184 if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
&& Uart
.OutOfCnt
== 4) {
187 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
188 if(Uart
.dropPosition
== 4) {
189 Uart
.state
= STATE_RECEIVING
;
192 else if(Uart
.dropPosition
== 3) {
193 Uart
.state
= STATE_RECEIVING
;
195 //Uart.output[Uart.byteCnt] = 0xdd;
199 Uart
.state
= STATE_UNSYNCD
;
202 Uart
.dropPosition
= 0;
207 if(!Uart
.dropPosition
) {
208 Uart
.state
= STATE_UNSYNCD
;
217 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
218 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
220 Uart
.shiftReg
^= ((Uart
.dropPosition
& 0x03) << 6);
222 Uart
.dropPosition
= 0;
224 if(Uart
.bitCnt
== 8) {
225 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
228 // Calculate the parity bit for the client...
229 Uart
.parityBits
<<= 1;
230 Uart
.parityBits
^= OddByteParity
[(Uart
.shiftReg
& 0xff)];
238 else if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
) {
241 if(!Uart
.dropPosition
) {
242 Uart
.state
= STATE_UNSYNCD
;
248 Uart
.output
[Uart
.byteCnt
] = (Uart
.dropPosition
& 0xff);
251 // Calculate the parity bit for the client...
252 Uart
.parityBits
<<= 1;
253 Uart
.parityBits
^= OddByteParity
[(Uart
.dropPosition
& 0xff)];
258 Uart
.dropPosition
= 0;
263 Uart.output[Uart.byteCnt] = 0xAA;
265 Uart.output[Uart.byteCnt] = error & 0xFF;
267 Uart.output[Uart.byteCnt] = 0xAA;
269 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
271 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
273 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
275 Uart.output[Uart.byteCnt] = 0xAA;
283 bit
= Uart
.bitBuffer
& 0xf0;
285 bit
^= 0x0F; // drops become 1s ;-)
287 // should have been high or at least (4 * 128) / fc
288 // according to ISO this should be at least (9 * 128 + 20) / fc
289 if(Uart
.highCnt
== 8) {
290 // we went low, so this could be start of communication
291 // it turns out to be safer to choose a less significant
292 // syncbit... so we check whether the neighbour also represents the drop
293 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
294 Uart
.syncBit
= bit
& 8;
296 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
297 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
298 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
299 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
300 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
301 if(Uart
.syncBit
&& (Uart
.bitBuffer
& 8)) {
304 // the first half bit period is expected in next sample
309 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
312 Uart
.state
= STATE_START_OF_COMMUNICATION
;
317 Uart
.OutOfCnt
= 4; // Start at 1/4, could switch to 1/256
318 Uart
.dropPosition
= 0;
327 if(Uart
.highCnt
< 8) {
336 //=============================================================================
338 //=============================================================================
343 DEMOD_START_OF_COMMUNICATION
,
344 DEMOD_START_OF_COMMUNICATION2
,
345 DEMOD_START_OF_COMMUNICATION3
,
349 DEMOD_END_OF_COMMUNICATION
,
350 DEMOD_END_OF_COMMUNICATION2
,
374 static RAMFUNC
int ManchesterDecoding(int v
)
381 Demod
.buffer
= Demod
.buffer2
;
382 Demod
.buffer2
= Demod
.buffer3
;
390 if(Demod
.state
==DEMOD_UNSYNCD
) {
391 Demod
.output
[Demod
.len
] = 0xfa;
394 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
397 Demod
.syncBit
= 0x08;
404 Demod
.syncBit
= 0x04;
411 Demod
.syncBit
= 0x02;
414 if(bit
& 0x01 && Demod
.syncBit
) {
415 Demod
.syncBit
= 0x01;
420 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
421 Demod
.sub
= SUB_FIRST_HALF
;
424 Demod
.parityBits
= 0;
427 //if(trigger) LED_A_OFF(); // Not useful in this case...
428 switch(Demod
.syncBit
) {
429 case 0x08: Demod
.samples
= 3; break;
430 case 0x04: Demod
.samples
= 2; break;
431 case 0x02: Demod
.samples
= 1; break;
432 case 0x01: Demod
.samples
= 0; break;
434 // SOF must be long burst... otherwise stay unsynced!!!
435 if(!(Demod
.buffer
& Demod
.syncBit
) || !(Demod
.buffer2
& Demod
.syncBit
)) {
436 Demod
.state
= DEMOD_UNSYNCD
;
440 // SOF must be long burst... otherwise stay unsynced!!!
441 if(!(Demod
.buffer2
& Demod
.syncBit
) || !(Demod
.buffer3
& Demod
.syncBit
)) {
442 Demod
.state
= DEMOD_UNSYNCD
;
452 modulation
= bit
& Demod
.syncBit
;
453 modulation
|= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
454 //modulation = ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
458 if(Demod
.posCount
==0) {
461 Demod
.sub
= SUB_FIRST_HALF
;
464 Demod
.sub
= SUB_NONE
;
469 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
470 if(Demod.state!=DEMOD_ERROR_WAIT) {
471 Demod.state = DEMOD_ERROR_WAIT;
472 Demod.output[Demod.len] = 0xaa;
476 //else if(modulation) {
478 if(Demod
.sub
== SUB_FIRST_HALF
) {
479 Demod
.sub
= SUB_BOTH
;
482 Demod
.sub
= SUB_SECOND_HALF
;
485 else if(Demod
.sub
== SUB_NONE
) {
486 if(Demod
.state
== DEMOD_SOF_COMPLETE
) {
487 Demod
.output
[Demod
.len
] = 0x0f;
489 Demod
.parityBits
<<= 1;
490 Demod
.parityBits
^= OddByteParity
[0x0f];
491 Demod
.state
= DEMOD_UNSYNCD
;
496 Demod
.state
= DEMOD_ERROR_WAIT
;
499 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
500 Demod.state = DEMOD_ERROR_WAIT;
501 Demod.output[Demod.len] = 0xaa;
506 switch(Demod
.state
) {
507 case DEMOD_START_OF_COMMUNICATION
:
508 if(Demod
.sub
== SUB_BOTH
) {
509 //Demod.state = DEMOD_MANCHESTER_D;
510 Demod
.state
= DEMOD_START_OF_COMMUNICATION2
;
512 Demod
.sub
= SUB_NONE
;
515 Demod
.output
[Demod
.len
] = 0xab;
516 Demod
.state
= DEMOD_ERROR_WAIT
;
520 case DEMOD_START_OF_COMMUNICATION2
:
521 if(Demod
.sub
== SUB_SECOND_HALF
) {
522 Demod
.state
= DEMOD_START_OF_COMMUNICATION3
;
525 Demod
.output
[Demod
.len
] = 0xab;
526 Demod
.state
= DEMOD_ERROR_WAIT
;
530 case DEMOD_START_OF_COMMUNICATION3
:
531 if(Demod
.sub
== SUB_SECOND_HALF
) {
532 // Demod.state = DEMOD_MANCHESTER_D;
533 Demod
.state
= DEMOD_SOF_COMPLETE
;
534 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
538 Demod
.output
[Demod
.len
] = 0xab;
539 Demod
.state
= DEMOD_ERROR_WAIT
;
543 case DEMOD_SOF_COMPLETE
:
544 case DEMOD_MANCHESTER_D
:
545 case DEMOD_MANCHESTER_E
:
546 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
547 // 00001111 = 1 (0 in 14443)
548 if(Demod
.sub
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF
550 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
551 Demod
.state
= DEMOD_MANCHESTER_D
;
553 else if(Demod
.sub
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF
555 Demod
.shiftReg
>>= 1;
556 Demod
.state
= DEMOD_MANCHESTER_E
;
558 else if(Demod
.sub
== SUB_BOTH
) {
559 Demod
.state
= DEMOD_MANCHESTER_F
;
562 Demod
.state
= DEMOD_ERROR_WAIT
;
567 case DEMOD_MANCHESTER_F
:
568 // Tag response does not need to be a complete byte!
569 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
570 if(Demod
.bitCount
> 1) { // was > 0, do not interpret last closing bit, is part of EOF
571 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
572 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
574 // No parity bit, so just shift a 0
575 Demod
.parityBits
<<= 1;
578 Demod
.state
= DEMOD_UNSYNCD
;
582 Demod
.output
[Demod
.len
] = 0xad;
583 Demod
.state
= DEMOD_ERROR_WAIT
;
588 case DEMOD_ERROR_WAIT
:
589 Demod
.state
= DEMOD_UNSYNCD
;
593 Demod
.output
[Demod
.len
] = 0xdd;
594 Demod
.state
= DEMOD_UNSYNCD
;
598 /*if(Demod.bitCount>=9) {
599 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
602 Demod.parityBits <<= 1;
603 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
608 if(Demod
.bitCount
>=8) {
609 Demod
.shiftReg
>>= 1;
610 Demod
.output
[Demod
.len
] = (Demod
.shiftReg
& 0xff);
613 // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT
614 Demod
.parityBits
<<= 1;
615 Demod
.parityBits
^= OddByteParity
[(Demod
.shiftReg
& 0xff)];
622 Demod
.output
[Demod
.len
] = 0xBB;
624 Demod
.output
[Demod
.len
] = error
& 0xFF;
626 Demod
.output
[Demod
.len
] = 0xBB;
628 Demod
.output
[Demod
.len
] = bit
& 0xFF;
630 Demod
.output
[Demod
.len
] = Demod
.buffer
& 0xFF;
633 Demod
.output
[Demod
.len
] = Demod
.buffer2
& 0xFF;
635 Demod
.output
[Demod
.len
] = Demod
.syncBit
& 0xFF;
637 Demod
.output
[Demod
.len
] = 0xBB;
644 } // end (state != UNSYNCED)
649 //=============================================================================
650 // Finally, a `sniffer' for iClass communication
651 // Both sides of communication!
652 //=============================================================================
654 //-----------------------------------------------------------------------------
655 // Record the sequence of commands sent by the reader to the tag, with
656 // triggering so that we start recording at the point that the tag is moved
658 //-----------------------------------------------------------------------------
659 void RAMFUNC
SnoopIClass(void)
663 // We won't start recording the frames that we acquire until we trigger;
664 // a good trigger condition to get started is probably when we see a
665 // response from the tag.
666 //int triggered = FALSE; // FALSE to wait first for card
668 // The command (reader -> tag) that we're receiving.
669 // The length of a received command will in most cases be no more than 18 bytes.
670 // So 32 should be enough!
671 uint8_t *readerToTagCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
672 // The response (tag -> reader) that we're receiving.
673 uint8_t *tagToReaderResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
675 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
677 // reset traceLen to 0
678 iso14a_set_tracing(TRUE
);
679 iso14a_clear_trace();
680 iso14a_set_trigger(FALSE
);
682 // The DMA buffer, used to stream samples from the FPGA
683 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
689 // Count of samples received so far, so that we can include timing
690 // information in the trace buffer.
694 // Set up the demodulator for tag -> reader responses.
695 Demod
.output
= tagToReaderResponse
;
697 Demod
.state
= DEMOD_UNSYNCD
;
699 // Setup for the DMA.
702 lastRxCounter
= DMA_BUFFER_SIZE
;
703 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
705 // And the reader -> tag commands
706 memset(&Uart
, 0, sizeof(Uart
));
707 Uart
.output
= readerToTagCmd
;
708 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
709 Uart
.state
= STATE_UNSYNCD
;
711 // And put the FPGA in the appropriate mode
712 // Signal field is off with the appropriate LED
714 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
715 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
717 uint32_t time_0
= GetCountSspClk();
725 // And now we loop, receiving samples.
729 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
731 if(behindBy
> maxBehindBy
) {
732 maxBehindBy
= behindBy
;
734 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
738 if(behindBy
< 1) continue;
744 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
745 upTo
-= DMA_BUFFER_SIZE
;
746 lastRxCounter
+= DMA_BUFFER_SIZE
;
747 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
748 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
755 decbyte
^= (1 << (3 - div
));
758 // FOR READER SIDE COMMUMICATION...
761 decbyter
^= (smpl
& 0x30);
765 if((div
+ 1) % 2 == 0) {
767 if(OutOfNDecoding((smpl
& 0xF0) >> 4)) {
768 rsamples
= samples
- Uart
.samples
;
771 //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
772 //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
775 LogTrace(Uart
.output
,Uart
.byteCnt
, (GetCountSspClk()-time_0
) << 4, Uart
.parityBits
,TRUE
);
776 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, TRUE
);
780 /* And ready to receive another command. */
781 Uart
.state
= STATE_UNSYNCD
;
782 /* And also reset the demod code, which might have been */
783 /* false-triggered by the commands from the reader. */
784 Demod
.state
= DEMOD_UNSYNCD
;
793 if(ManchesterDecoding(smpl
& 0x0F)) {
794 rsamples
= samples
- Demod
.samples
;
799 LogTrace(Demod
.output
,Demod
.len
, (GetCountSspClk()-time_0
) << 4 , Demod
.parityBits
,FALSE
);
800 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, FALSE
);
804 // And ready to receive another response.
805 memset(&Demod
, 0, sizeof(Demod
));
806 Demod
.output
= tagToReaderResponse
;
807 Demod
.state
= DEMOD_UNSYNCD
;
817 DbpString("cancelled_a");
822 DbpString("COMMAND FINISHED");
824 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
825 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
828 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
829 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
830 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
837 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) {
839 for(i
= 0; i
< 8; i
++) {
840 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8] << 5);
844 //-----------------------------------------------------------------------------
845 // Wait for commands from reader
846 // Stop when button is pressed
847 // Or return TRUE when command is captured
848 //-----------------------------------------------------------------------------
849 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
851 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
852 // only, since we are receiving, not transmitting).
853 // Signal field is off with the appropriate LED
855 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
857 // Now run a `software UART' on the stream of incoming samples.
858 Uart
.output
= received
;
859 Uart
.byteCntMax
= maxLen
;
860 Uart
.state
= STATE_UNSYNCD
;
865 if(BUTTON_PRESS()) return FALSE
;
867 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
868 AT91C_BASE_SSC
->SSC_THR
= 0x00;
870 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
871 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
872 /*if(OutOfNDecoding((b & 0xf0) >> 4)) {
876 if(OutOfNDecoding(b
& 0x0f)) {
885 //-----------------------------------------------------------------------------
886 // Prepare tag messages
887 //-----------------------------------------------------------------------------
888 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
)
890 //So far a dummy implementation, not used
891 //int lastProxToAirDuration =0;
897 ToSend
[++ToSendMax
] = 0x00;
898 ToSend
[++ToSendMax
] = 0x00;
899 ToSend
[++ToSendMax
] = 0x00;
900 ToSend
[++ToSendMax
] = 0xff;//Proxtoair duration starts here
901 ToSend
[++ToSendMax
] = 0xff;
902 ToSend
[++ToSendMax
] = 0xff;
903 ToSend
[++ToSendMax
] = 0x00;
904 ToSend
[++ToSendMax
] = 0xff;
906 for(i
= 0; i
< len
; i
++) {
911 for(j
= 0; j
< 8; j
++) {
913 ToSend
[++ToSendMax
] = 0x00;
914 ToSend
[++ToSendMax
] = 0xff;
916 ToSend
[++ToSendMax
] = 0xff;
917 ToSend
[++ToSendMax
] = 0x00;
924 ToSend
[++ToSendMax
] = 0xff;
925 ToSend
[++ToSendMax
] = 0x00;
926 ToSend
[++ToSendMax
] = 0xff;
927 ToSend
[++ToSendMax
] = 0xff;
928 ToSend
[++ToSendMax
] = 0xff;
929 ToSend
[++ToSendMax
] = 0x00;
930 ToSend
[++ToSendMax
] = 0x00;
931 ToSend
[++ToSendMax
] = 0x00;
933 //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end
935 // Convert from last byte pos to length
940 static void CodeIClassTagSOF()
942 //So far a dummy implementation, not used
943 //int lastProxToAirDuration =0;
947 ToSend
[++ToSendMax
] = 0x00;
948 ToSend
[++ToSendMax
] = 0x00;
949 ToSend
[++ToSendMax
] = 0x00;
950 ToSend
[++ToSendMax
] = 0xff;
951 ToSend
[++ToSendMax
] = 0xff;
952 ToSend
[++ToSendMax
] = 0xff;
953 ToSend
[++ToSendMax
] = 0x00;
954 ToSend
[++ToSendMax
] = 0xff;
956 // lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning
959 // Convert from last byte pos to length
962 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
);
964 * @brief SimulateIClass simulates an iClass card.
965 * @param arg0 type of simulation
966 * - 0 uses the first 8 bytes in usb data as CSN
967 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
968 * in the usb data. This mode collects MAC from the reader, in order to do an offline
969 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
970 * - Other : Uses the default CSN (031fec8af7ff12e0)
971 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
975 void SimulateIClass(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
)
977 uint32_t simType
= arg0
;
978 uint32_t numberOfCSNS
= arg1
;
979 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
981 // Enable and clear the trace
982 iso14a_set_tracing(TRUE
);
983 iso14a_clear_trace();
985 uint8_t csn_crc
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
987 // Use the CSN from commandline
988 memcpy(csn_crc
, datain
, 8);
989 doIClassSimulation(csn_crc
,0,NULL
);
990 }else if(simType
== 1)
992 doIClassSimulation(csn_crc
,0,NULL
);
994 else if(simType
== 2)
997 uint8_t mac_responses
[64] = { 0 };
998 Dbprintf("Going into attack mode");
999 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
1000 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
1001 // in order to obtain the keys, as in the "dismantling iclass"-paper.
1003 for( ; i
< numberOfCSNS
&& i
*8+8 < USB_CMD_DATA_SIZE
; i
++)
1005 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
1007 memcpy(csn_crc
, datain
+(i
*8), 8);
1008 if(doIClassSimulation(csn_crc
,1,mac_responses
))
1010 return; // Button pressed
1013 cmd_send(CMD_ACK
,CMD_SIMULATE_TAG_ICLASS
,i
,0,mac_responses
,i
*8);
1017 // We may want a mode here where we hardcode the csns to use (from proxclone).
1018 // That will speed things up a little, but not required just yet.
1019 Dbprintf("The mode is not implemented, reserved for future use");
1021 Dbprintf("Done...");
1025 * @brief Does the actual simulation
1026 * @param csn - csn to use
1027 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1029 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
)
1033 // CSN followed by two CRC bytes
1034 uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1035 uint8_t response3
[] = { 0,0,0,0,0,0,0,0,0,0};
1036 memcpy(response3
,csn
,sizeof(response3
));
1037 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]);
1039 uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1041 // Construct anticollision-CSN
1042 rotateCSN(response3
,response2
);
1044 // Compute CRC on both CSNs
1045 ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]);
1046 ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]);
1052 // Tag anticoll. CSN
1053 // Reader 81 anticoll. CSN
1058 uint8_t* respdata
= NULL
;
1062 // Respond SOF -- takes 8 bytes
1063 uint8_t *resp1
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
);
1066 // Anticollision CSN (rotated CSN)
1067 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1068 uint8_t *resp2
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 10);
1072 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1073 uint8_t *resp3
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 190);
1077 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
1078 uint8_t *resp4
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 370);
1082 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
1083 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1086 // Prepare card messages
1089 // First card answer: SOF
1091 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1093 // Anticollision CSN
1094 CodeIClassTagAnswer(response2
, sizeof(response2
));
1095 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1098 CodeIClassTagAnswer(response3
, sizeof(response3
));
1099 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1102 CodeIClassTagAnswer(response4
, sizeof(response4
));
1103 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1106 // Start from off (no field generated)
1107 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1109 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
1112 // We need to listen to the high-frequency, peak-detected path.
1113 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1116 // To control where we are in the protocol
1118 uint32_t time_0
= GetCountSspClk();
1119 uint32_t t2r_time
=0;
1120 uint32_t r2t_time
=0;
1123 bool buttonPressed
= false;
1125 /** Hack for testing
1126 memcpy(reader_mac_buf,csn,8);
1134 // Can be used to get a trigger for an oscilloscope..
1137 if(!GetIClassCommandFromReader(receivedCmd
, &len
, 100)) {
1138 buttonPressed
= true;
1141 r2t_time
= GetCountSspClk();
1145 // Okay, look at the command now.
1146 if(receivedCmd
[0] == 0x0a ) {
1147 // Reader in anticollission phase
1148 resp
= resp1
; respLen
= resp1Len
; //order = 1;
1150 respsize
= sizeof(sof
);
1151 } else if(receivedCmd
[0] == 0x0c) {
1152 // Reader asks for anticollission CSN
1153 resp
= resp2
; respLen
= resp2Len
; //order = 2;
1154 respdata
= response2
;
1155 respsize
= sizeof(response2
);
1156 //DbpString("Reader requests anticollission CSN:");
1157 } else if(receivedCmd
[0] == 0x81) {
1158 // Reader selects anticollission CSN.
1159 // Tag sends the corresponding real CSN
1160 resp
= resp3
; respLen
= resp3Len
; //order = 3;
1161 respdata
= response3
;
1162 respsize
= sizeof(response3
);
1163 //DbpString("Reader selects anticollission CSN:");
1164 } else if(receivedCmd
[0] == 0x88) {
1165 // Read e-purse (88 02)
1166 resp
= resp4
; respLen
= resp4Len
; //order = 4;
1167 respdata
= response4
;
1168 respsize
= sizeof(response4
);
1170 } else if(receivedCmd
[0] == 0x05) {
1171 // Reader random and reader MAC!!!
1173 // We do not know what to answer, so lets keep quiet
1174 resp
= resp1
; respLen
= 0; //order = 5;
1177 if (breakAfterMacReceived
){
1179 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]);
1180 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len
,
1181 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1182 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1183 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1184 if (reader_mac_buf
!= NULL
)
1186 memcpy(reader_mac_buf
,receivedCmd
+1,8);
1190 } else if(receivedCmd
[0] == 0x00 && len
== 1) {
1191 // Reader ends the session
1192 resp
= resp1
; respLen
= 0; //order = 0;
1196 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1197 // Never seen this command before
1198 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1200 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1201 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1202 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1204 resp
= resp1
; respLen
= 0; //order = 0;
1209 if(cmdsRecvd
> 100) {
1210 //DbpString("100 commands later...");
1218 SendIClassAnswer(resp
, respLen
, 21);
1219 t2r_time
= GetCountSspClk();
1223 LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, Uart
.parityBits
,TRUE
);
1224 LogTrace(NULL
,0, (r2t_time
-time_0
) << 4, 0,TRUE
);
1226 if (respdata
!= NULL
) {
1227 LogTrace(respdata
,respsize
, (t2r_time
-time_0
) << 4,SwapBits(GetParity(respdata
,respsize
),respsize
),FALSE
);
1228 LogTrace(NULL
,0, (t2r_time
-time_0
) << 4,0,FALSE
);
1233 DbpString("Trace full");
1238 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1241 //Dbprintf("%x", cmdsRecvd);
1246 DbpString("Button pressed");
1248 return buttonPressed
;
1251 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1253 int i
= 0, d
=0;//, u = 0, d = 0;
1256 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
1258 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1260 while(!BUTTON_PRESS()) {
1261 if((AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)){
1262 b
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
;
1264 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)){
1277 AT91C_BASE_SSC
->SSC_THR
= b
;
1280 if (i
> respLen
+4) break;
1288 //-----------------------------------------------------------------------------
1289 // Transmit the command (to the tag) that was placed in ToSend[].
1290 //-----------------------------------------------------------------------------
1291 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1294 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1295 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1302 for(c
= 0; c
< *wait
;) {
1303 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1304 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1307 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1308 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1315 bool firstpart
= TRUE
;
1318 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1320 // DOUBLE THE SAMPLES!
1322 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1325 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1328 if(sendbyte
== 0xff) {
1331 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1332 firstpart
= !firstpart
;
1338 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1339 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1344 if (samples
) *samples
= (c
+ *wait
) << 3;
1348 //-----------------------------------------------------------------------------
1349 // Prepare iClass reader command to send to FPGA
1350 //-----------------------------------------------------------------------------
1351 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1358 // Start of Communication: 1 out of 4
1359 ToSend
[++ToSendMax
] = 0xf0;
1360 ToSend
[++ToSendMax
] = 0x00;
1361 ToSend
[++ToSendMax
] = 0x0f;
1362 ToSend
[++ToSendMax
] = 0x00;
1364 // Modulate the bytes
1365 for (i
= 0; i
< len
; i
++) {
1367 for(j
= 0; j
< 4; j
++) {
1368 for(k
= 0; k
< 4; k
++) {
1370 ToSend
[++ToSendMax
] = 0x0f;
1373 ToSend
[++ToSendMax
] = 0x00;
1380 // End of Communication
1381 ToSend
[++ToSendMax
] = 0x00;
1382 ToSend
[++ToSendMax
] = 0x00;
1383 ToSend
[++ToSendMax
] = 0xf0;
1384 ToSend
[++ToSendMax
] = 0x00;
1386 // Convert from last character reference to length
1390 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1396 // This is tied to other size changes
1397 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1398 CodeIClassCommand(frame
,len
);
1401 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1405 // Store reader command in buffer
1406 if (tracing
) LogTrace(frame
,len
,rsamples
,par
,TRUE
);
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
1416 // buffer needs to be 512 bytes
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
);
1423 // Now get the answer from the card
1424 Demod
.output
= receivedResponse
;
1426 Demod
.state
= DEMOD_UNSYNCD
;
1429 if (elapsed
) *elapsed
= 0;
1437 if(BUTTON_PRESS()) return FALSE
;
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
)++;
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
;
1448 /*if(ManchesterDecoding((b>>4) & 0xf)) {
1449 *samples = ((c - 1) << 3) + 4;
1452 if(ManchesterDecoding(b
& 0x0f)) {
1460 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1463 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1464 rsamples
+= samples
;
1465 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,rsamples
,Demod
.parityBits
,FALSE
);
1466 if(samples
== 0) return FALSE
;
1470 void setupIclassReader()
1472 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1473 // Reset trace buffer
1474 iso14a_set_tracing(TRUE
);
1475 iso14a_clear_trace();
1479 // Start from off (no field generated)
1480 // Signal field is off with the appropriate LED
1482 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1485 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1487 // Now give it time to spin up.
1488 // Signal field is on with the appropriate LED
1489 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1495 // Reader iClass Anticollission
1496 void ReaderIClass(uint8_t arg0
) {
1497 uint8_t act_all
[] = { 0x0a };
1498 uint8_t identify
[] = { 0x0c };
1499 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1500 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1502 uint8_t card_data
[24]={0};
1503 uint8_t last_csn
[8]={0};
1505 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1508 bool abort_after_read
= arg0
& FLAG_ICLASS_READER_ONLY_ONCE
;
1510 setupIclassReader();
1512 size_t datasize
= 0;
1513 while(!BUTTON_PRESS())
1518 ReaderTransmitIClass(act_all
, 1);
1520 if(ReaderReceiveIClass(resp
)) {
1522 ReaderTransmitIClass(identify
, 1);
1524 if(ReaderReceiveIClass(resp
) == 10) {
1525 //Copy the Anti-collision CSN to our select-packet
1526 memcpy(&select
[1],resp
,8);
1527 //Dbprintf("Anti-collision CSN: %02x %02x %02x %02x %02x %02x %02x %02x",resp[0], resp[1], resp[2],
1528 // resp[3], resp[4], resp[5],
1529 // resp[6], resp[7]);
1531 ReaderTransmitIClass(select
, sizeof(select
));
1533 if(ReaderReceiveIClass(resp
) == 10) {
1534 //Save CSN in response data
1535 memcpy(card_data
,resp
,8);
1537 //Flag that we got to at least stage 1, read CSN
1541 //Dbprintf("Readcheck on Sector 2");
1542 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1543 if(ReaderReceiveIClass(resp
) == 8) {
1544 //Save CC (e-purse) in response data
1545 memcpy(card_data
+8,resp
,8);
1552 //Send back to client, but don't bother if we already sent this
1553 if(memcmp(last_csn
, card_data
, 8) != 0)
1554 cmd_send(CMD_ACK
,read_status
,0,0,card_data
,datasize
);
1556 //Save that we already sent this....
1557 if(read_status
== 2)
1558 memcpy(last_csn
, card_data
, 8);
1562 if(abort_after_read
) break;
1567 if(traceLen
> TRACE_SIZE
) {
1568 DbpString("Trace full");
1575 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) {
1576 uint8_t act_all
[] = { 0x0a };
1577 uint8_t identify
[] = { 0x0c };
1578 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1579 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1580 uint8_t check
[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1581 uint8_t read
[] = { 0x0c, 0x00, 0x00, 0x00 };
1585 bool read_success
=false;
1588 static struct memory_t
{
1596 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1598 setupIclassReader();
1601 for(int i
=0;i
<1;i
++) {
1603 if(traceLen
> TRACE_SIZE
) {
1604 DbpString("Trace full");
1608 if (BUTTON_PRESS()) break;
1611 ReaderTransmitIClass(act_all
, 1);
1613 if(ReaderReceiveIClass(resp
)) {
1614 ReaderTransmitIClass(identify
, 1);
1615 if(ReaderReceiveIClass(resp
) == 10) {
1617 memcpy(&select
[1],resp
,8);
1618 ReaderTransmitIClass(select
, sizeof(select
));
1620 if(ReaderReceiveIClass(resp
) == 10) {
1621 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1622 resp
[0], resp
[1], resp
[2],
1623 resp
[3], resp
[4], resp
[5],
1627 Dbprintf("Readcheck on Sector 2");
1628 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1629 if(ReaderReceiveIClass(resp
) == 8) {
1630 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1631 resp
[0], resp
[1], resp
[2],
1632 resp
[3], resp
[4], resp
[5],
1635 Dbprintf("Authenticate");
1636 //for now replay captured auth (as cc not updated)
1637 memcpy(check
+5,MAC
,4);
1638 //Dbprintf(" AA: %02x %02x %02x %02x",
1639 // check[5], check[6], check[7],check[8]);
1640 ReaderTransmitIClass(check
, sizeof(check
));
1641 if(ReaderReceiveIClass(resp
) == 4) {
1642 Dbprintf(" AR: %02x %02x %02x %02x",
1643 resp
[0], resp
[1], resp
[2],resp
[3]);
1645 Dbprintf("Error: Authentication Fail!");
1648 Dbprintf("Dump Contents");
1649 //first get configuration block
1652 uint8_t *blockno
=&read
[1];
1653 crc
= iclass_crc16((char *)blockno
,1);
1655 read
[3] = crc
& 0xff;
1656 while(!read_success
){
1657 ReaderTransmitIClass(read
, sizeof(read
));
1658 if(ReaderReceiveIClass(resp
) == 10) {
1661 memory
.k16
= (mem
& 0x80);
1662 memory
.book
= (mem
& 0x20);
1663 memory
.k2
= (mem
& 0x8);
1664 memory
.lockauth
= (mem
& 0x2);
1665 memory
.keyaccess
= (mem
& 0x1);
1672 //then loop around remaining blocks
1673 for(uint8_t j
=0; j
<cardsize
; j
++){
1675 uint8_t *blockno
=&j
;
1678 crc
= iclass_crc16((char *)blockno
,1);
1680 read
[3] = crc
& 0xff;
1681 while(!read_success
){
1682 ReaderTransmitIClass(read
, sizeof(read
));
1683 if(ReaderReceiveIClass(resp
) == 10) {
1685 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1686 j
, resp
[0], resp
[1], resp
[2],
1687 resp
[3], resp
[4], resp
[5],
1700 //2. Create Read method (cut-down from above) based off responses from 1.
1701 // Since we have the MAC could continue to use replay function.
1702 //3. Create Write method
1704 void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) {
1705 uint8_t act_all[] = { 0x0a };
1706 uint8_t identify[] = { 0x0c };
1707 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1708 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1709 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1710 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1711 uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1715 uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
1717 // Reset trace buffer
1718 memset(trace, 0x44, RECV_CMD_OFFSET);
1723 // Start from off (no field generated)
1724 // Signal field is off with the appropriate LED
1726 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1729 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1731 // Now give it time to spin up.
1732 // Signal field is on with the appropriate LED
1733 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1738 for(int i=0;i<1;i++) {
1740 if(traceLen > TRACE_SIZE) {
1741 DbpString("Trace full");
1745 if (BUTTON_PRESS()) break;
1748 ReaderTransmitIClass(act_all, 1);
1750 if(ReaderReceiveIClass(resp)) {
1751 ReaderTransmitIClass(identify, 1);
1752 if(ReaderReceiveIClass(resp) == 10) {
1754 memcpy(&select[1],resp,8);
1755 ReaderTransmitIClass(select, sizeof(select));
1757 if(ReaderReceiveIClass(resp) == 10) {
1758 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1759 resp[0], resp[1], resp[2],
1760 resp[3], resp[4], resp[5],
1764 Dbprintf("Readcheck on Sector 2");
1765 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1766 if(ReaderReceiveIClass(resp) == 8) {
1767 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1768 resp[0], resp[1], resp[2],
1769 resp[3], resp[4], resp[5],
1772 Dbprintf("Authenticate");
1773 //for now replay captured auth (as cc not updated)
1774 memcpy(check+5,MAC,4);
1775 Dbprintf(" AA: %02x %02x %02x %02x",
1776 check[5], check[6], check[7],check[8]);
1777 ReaderTransmitIClass(check, sizeof(check));
1778 if(ReaderReceiveIClass(resp) == 4) {
1779 Dbprintf(" AR: %02x %02x %02x %02x",
1780 resp[0], resp[1], resp[2],resp[3]);
1782 Dbprintf("Error: Authentication Fail!");
1785 Dbprintf("Write Block");
1787 //read configuration for max block number
1790 uint8_t *blockno=&read[1];
1791 crc = iclass_crc16((char *)blockno,1);
1793 read[3] = crc & 0xff;
1794 while(!read_success){
1795 ReaderTransmitIClass(read, sizeof(read));
1796 if(ReaderReceiveIClass(resp) == 10) {
1799 memory.k16= (mem & 0x80);
1800 memory.book= (mem & 0x20);
1801 memory.k2= (mem & 0x8);
1802 memory.lockauth= (mem & 0x2);
1803 memory.keyaccess= (mem & 0x1);
1812 memcpy(write+1,blockNo,1);
1813 memcpy(write+2,data,8);
1814 memcpy(write+10,mac,4);
1815 while(!send_success){
1816 ReaderTransmitIClass(write, sizeof(write));
1817 if(ReaderReceiveIClass(resp) == 10) {