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"
50 #include "iso15693tools.h"
53 static int timeout
= 4096;
56 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
);
58 //-----------------------------------------------------------------------------
59 // The software UART that receives commands from the reader, and its state
61 //-----------------------------------------------------------------------------
65 STATE_START_OF_COMMUNICATION
,
86 static RAMFUNC
int OutOfNDecoding(int bit
)
92 Uart
.bitBuffer
= bit
^ 0xFF0;
97 Uart
.bitBuffer
^= bit
;
101 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
104 if(Uart.byteCnt > 15) { return TRUE; }
110 if(Uart
.state
!= STATE_UNSYNCD
) {
113 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
119 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
125 if(bit
!= bitright
) { bit
= bitright
; }
128 // So, now we only have to deal with *bit*, lets see...
129 if(Uart
.posCnt
== 1) {
130 // measurement first half bitperiod
132 // Drop in first half means that we are either seeing
135 if(Uart
.nOutOfCnt
== 1) {
136 // End of Communication
137 Uart
.state
= STATE_UNSYNCD
;
139 if(Uart
.byteCnt
== 0) {
140 // Its not straightforward to show single EOFs
141 // So just leave it and do not return TRUE
142 Uart
.output
[Uart
.byteCnt
] = 0xf0;
145 // Calculate the parity bit for the client...
152 else if(Uart
.state
!= STATE_START_OF_COMMUNICATION
) {
153 // When not part of SOF or EOF, it is an error
154 Uart
.state
= STATE_UNSYNCD
;
161 // measurement second half bitperiod
162 // Count the bitslot we are in... (ISO 15693)
166 if(Uart
.dropPosition
) {
167 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
173 // It is an error if we already have seen a drop in current frame
174 Uart
.state
= STATE_UNSYNCD
;
178 Uart
.dropPosition
= Uart
.nOutOfCnt
;
185 if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
&& Uart
.OutOfCnt
== 4) {
188 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
189 if(Uart
.dropPosition
== 4) {
190 Uart
.state
= STATE_RECEIVING
;
193 else if(Uart
.dropPosition
== 3) {
194 Uart
.state
= STATE_RECEIVING
;
196 //Uart.output[Uart.byteCnt] = 0xdd;
200 Uart
.state
= STATE_UNSYNCD
;
203 Uart
.dropPosition
= 0;
208 if(!Uart
.dropPosition
) {
209 Uart
.state
= STATE_UNSYNCD
;
218 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
219 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
221 Uart
.shiftReg
^= ((Uart
.dropPosition
& 0x03) << 6);
223 Uart
.dropPosition
= 0;
225 if(Uart
.bitCnt
== 8) {
226 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
229 // Calculate the parity bit for the client...
230 Uart
.parityBits
<<= 1;
231 Uart
.parityBits
^= OddByteParity
[(Uart
.shiftReg
& 0xff)];
239 else if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
) {
242 if(!Uart
.dropPosition
) {
243 Uart
.state
= STATE_UNSYNCD
;
249 Uart
.output
[Uart
.byteCnt
] = (Uart
.dropPosition
& 0xff);
252 // Calculate the parity bit for the client...
253 Uart
.parityBits
<<= 1;
254 Uart
.parityBits
^= OddByteParity
[(Uart
.dropPosition
& 0xff)];
259 Uart
.dropPosition
= 0;
264 Uart.output[Uart.byteCnt] = 0xAA;
266 Uart.output[Uart.byteCnt] = error & 0xFF;
268 Uart.output[Uart.byteCnt] = 0xAA;
270 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
272 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
274 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
276 Uart.output[Uart.byteCnt] = 0xAA;
284 bit
= Uart
.bitBuffer
& 0xf0;
286 bit
^= 0x0F; // drops become 1s ;-)
288 // should have been high or at least (4 * 128) / fc
289 // according to ISO this should be at least (9 * 128 + 20) / fc
290 if(Uart
.highCnt
== 8) {
291 // we went low, so this could be start of communication
292 // it turns out to be safer to choose a less significant
293 // syncbit... so we check whether the neighbour also represents the drop
294 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
295 Uart
.syncBit
= bit
& 8;
297 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
298 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
299 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
300 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
301 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
302 if(Uart
.syncBit
&& (Uart
.bitBuffer
& 8)) {
305 // the first half bit period is expected in next sample
310 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
313 Uart
.state
= STATE_START_OF_COMMUNICATION
;
318 Uart
.OutOfCnt
= 4; // Start at 1/4, could switch to 1/256
319 Uart
.dropPosition
= 0;
328 if(Uart
.highCnt
< 8) {
337 //=============================================================================
339 //=============================================================================
344 DEMOD_START_OF_COMMUNICATION
,
345 DEMOD_START_OF_COMMUNICATION2
,
346 DEMOD_START_OF_COMMUNICATION3
,
350 DEMOD_END_OF_COMMUNICATION
,
351 DEMOD_END_OF_COMMUNICATION2
,
375 static RAMFUNC
int ManchesterDecoding(int v
)
382 Demod
.buffer
= Demod
.buffer2
;
383 Demod
.buffer2
= Demod
.buffer3
;
391 if(Demod
.state
==DEMOD_UNSYNCD
) {
392 Demod
.output
[Demod
.len
] = 0xfa;
395 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
398 Demod
.syncBit
= 0x08;
405 Demod
.syncBit
= 0x04;
412 Demod
.syncBit
= 0x02;
415 if(bit
& 0x01 && Demod
.syncBit
) {
416 Demod
.syncBit
= 0x01;
421 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
422 Demod
.sub
= SUB_FIRST_HALF
;
425 Demod
.parityBits
= 0;
428 //if(trigger) LED_A_OFF(); // Not useful in this case...
429 switch(Demod
.syncBit
) {
430 case 0x08: Demod
.samples
= 3; break;
431 case 0x04: Demod
.samples
= 2; break;
432 case 0x02: Demod
.samples
= 1; break;
433 case 0x01: Demod
.samples
= 0; break;
435 // SOF must be long burst... otherwise stay unsynced!!!
436 if(!(Demod
.buffer
& Demod
.syncBit
) || !(Demod
.buffer2
& Demod
.syncBit
)) {
437 Demod
.state
= DEMOD_UNSYNCD
;
441 // SOF must be long burst... otherwise stay unsynced!!!
442 if(!(Demod
.buffer2
& Demod
.syncBit
) || !(Demod
.buffer3
& Demod
.syncBit
)) {
443 Demod
.state
= DEMOD_UNSYNCD
;
453 modulation
= bit
& Demod
.syncBit
;
454 modulation
|= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
455 //modulation = ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
459 if(Demod
.posCount
==0) {
462 Demod
.sub
= SUB_FIRST_HALF
;
465 Demod
.sub
= SUB_NONE
;
470 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
471 if(Demod.state!=DEMOD_ERROR_WAIT) {
472 Demod.state = DEMOD_ERROR_WAIT;
473 Demod.output[Demod.len] = 0xaa;
477 //else if(modulation) {
479 if(Demod
.sub
== SUB_FIRST_HALF
) {
480 Demod
.sub
= SUB_BOTH
;
483 Demod
.sub
= SUB_SECOND_HALF
;
486 else if(Demod
.sub
== SUB_NONE
) {
487 if(Demod
.state
== DEMOD_SOF_COMPLETE
) {
488 Demod
.output
[Demod
.len
] = 0x0f;
490 Demod
.parityBits
<<= 1;
491 Demod
.parityBits
^= OddByteParity
[0x0f];
492 Demod
.state
= DEMOD_UNSYNCD
;
497 Demod
.state
= DEMOD_ERROR_WAIT
;
500 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
501 Demod.state = DEMOD_ERROR_WAIT;
502 Demod.output[Demod.len] = 0xaa;
507 switch(Demod
.state
) {
508 case DEMOD_START_OF_COMMUNICATION
:
509 if(Demod
.sub
== SUB_BOTH
) {
510 //Demod.state = DEMOD_MANCHESTER_D;
511 Demod
.state
= DEMOD_START_OF_COMMUNICATION2
;
513 Demod
.sub
= SUB_NONE
;
516 Demod
.output
[Demod
.len
] = 0xab;
517 Demod
.state
= DEMOD_ERROR_WAIT
;
521 case DEMOD_START_OF_COMMUNICATION2
:
522 if(Demod
.sub
== SUB_SECOND_HALF
) {
523 Demod
.state
= DEMOD_START_OF_COMMUNICATION3
;
526 Demod
.output
[Demod
.len
] = 0xab;
527 Demod
.state
= DEMOD_ERROR_WAIT
;
531 case DEMOD_START_OF_COMMUNICATION3
:
532 if(Demod
.sub
== SUB_SECOND_HALF
) {
533 // Demod.state = DEMOD_MANCHESTER_D;
534 Demod
.state
= DEMOD_SOF_COMPLETE
;
535 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
539 Demod
.output
[Demod
.len
] = 0xab;
540 Demod
.state
= DEMOD_ERROR_WAIT
;
544 case DEMOD_SOF_COMPLETE
:
545 case DEMOD_MANCHESTER_D
:
546 case DEMOD_MANCHESTER_E
:
547 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
548 // 00001111 = 1 (0 in 14443)
549 if(Demod
.sub
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF
551 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
552 Demod
.state
= DEMOD_MANCHESTER_D
;
554 else if(Demod
.sub
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF
556 Demod
.shiftReg
>>= 1;
557 Demod
.state
= DEMOD_MANCHESTER_E
;
559 else if(Demod
.sub
== SUB_BOTH
) {
560 Demod
.state
= DEMOD_MANCHESTER_F
;
563 Demod
.state
= DEMOD_ERROR_WAIT
;
568 case DEMOD_MANCHESTER_F
:
569 // Tag response does not need to be a complete byte!
570 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
571 if(Demod
.bitCount
> 1) { // was > 0, do not interpret last closing bit, is part of EOF
572 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
573 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
575 // No parity bit, so just shift a 0
576 Demod
.parityBits
<<= 1;
579 Demod
.state
= DEMOD_UNSYNCD
;
583 Demod
.output
[Demod
.len
] = 0xad;
584 Demod
.state
= DEMOD_ERROR_WAIT
;
589 case DEMOD_ERROR_WAIT
:
590 Demod
.state
= DEMOD_UNSYNCD
;
594 Demod
.output
[Demod
.len
] = 0xdd;
595 Demod
.state
= DEMOD_UNSYNCD
;
599 /*if(Demod.bitCount>=9) {
600 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
603 Demod.parityBits <<= 1;
604 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
609 if(Demod
.bitCount
>=8) {
610 Demod
.shiftReg
>>= 1;
611 Demod
.output
[Demod
.len
] = (Demod
.shiftReg
& 0xff);
614 // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT
615 Demod
.parityBits
<<= 1;
616 Demod
.parityBits
^= OddByteParity
[(Demod
.shiftReg
& 0xff)];
623 Demod
.output
[Demod
.len
] = 0xBB;
625 Demod
.output
[Demod
.len
] = error
& 0xFF;
627 Demod
.output
[Demod
.len
] = 0xBB;
629 Demod
.output
[Demod
.len
] = bit
& 0xFF;
631 Demod
.output
[Demod
.len
] = Demod
.buffer
& 0xFF;
634 Demod
.output
[Demod
.len
] = Demod
.buffer2
& 0xFF;
636 Demod
.output
[Demod
.len
] = Demod
.syncBit
& 0xFF;
638 Demod
.output
[Demod
.len
] = 0xBB;
645 } // end (state != UNSYNCED)
650 //=============================================================================
651 // Finally, a `sniffer' for iClass communication
652 // Both sides of communication!
653 //=============================================================================
655 //-----------------------------------------------------------------------------
656 // Record the sequence of commands sent by the reader to the tag, with
657 // triggering so that we start recording at the point that the tag is moved
659 //-----------------------------------------------------------------------------
660 void RAMFUNC
SnoopIClass(void)
664 // We won't start recording the frames that we acquire until we trigger;
665 // a good trigger condition to get started is probably when we see a
666 // response from the tag.
667 //int triggered = FALSE; // FALSE to wait first for card
669 // The command (reader -> tag) that we're receiving.
670 // The length of a received command will in most cases be no more than 18 bytes.
671 // So 32 should be enough!
672 uint8_t *readerToTagCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
673 // The response (tag -> reader) that we're receiving.
674 uint8_t *tagToReaderResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
676 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
678 // reset traceLen to 0
679 iso14a_set_tracing(TRUE
);
680 iso14a_clear_trace();
681 iso14a_set_trigger(FALSE
);
683 // The DMA buffer, used to stream samples from the FPGA
684 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
690 // Count of samples received so far, so that we can include timing
691 // information in the trace buffer.
695 // Set up the demodulator for tag -> reader responses.
696 Demod
.output
= tagToReaderResponse
;
698 Demod
.state
= DEMOD_UNSYNCD
;
700 // Setup for the DMA.
703 lastRxCounter
= DMA_BUFFER_SIZE
;
704 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
706 // And the reader -> tag commands
707 memset(&Uart
, 0, sizeof(Uart
));
708 Uart
.output
= readerToTagCmd
;
709 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
710 Uart
.state
= STATE_UNSYNCD
;
712 // And put the FPGA in the appropriate mode
713 // Signal field is off with the appropriate LED
715 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
716 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
718 uint32_t time_0
= GetCountSspClk();
726 // And now we loop, receiving samples.
730 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
732 if(behindBy
> maxBehindBy
) {
733 maxBehindBy
= behindBy
;
735 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
739 if(behindBy
< 1) continue;
745 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
746 upTo
-= DMA_BUFFER_SIZE
;
747 lastRxCounter
+= DMA_BUFFER_SIZE
;
748 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
749 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
756 decbyte
^= (1 << (3 - div
));
759 // FOR READER SIDE COMMUMICATION...
762 decbyter
^= (smpl
& 0x30);
766 if((div
+ 1) % 2 == 0) {
768 if(OutOfNDecoding((smpl
& 0xF0) >> 4)) {
769 rsamples
= samples
- Uart
.samples
;
772 //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
773 //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
776 LogTrace(Uart
.output
,Uart
.byteCnt
, (GetCountSspClk()-time_0
) << 4, Uart
.parityBits
,TRUE
);
777 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, TRUE
);
781 /* And ready to receive another command. */
782 Uart
.state
= STATE_UNSYNCD
;
783 /* And also reset the demod code, which might have been */
784 /* false-triggered by the commands from the reader. */
785 Demod
.state
= DEMOD_UNSYNCD
;
794 if(ManchesterDecoding(smpl
& 0x0F)) {
795 rsamples
= samples
- Demod
.samples
;
800 LogTrace(Demod
.output
,Demod
.len
, (GetCountSspClk()-time_0
) << 4 , Demod
.parityBits
,FALSE
);
801 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, FALSE
);
805 // And ready to receive another response.
806 memset(&Demod
, 0, sizeof(Demod
));
807 Demod
.output
= tagToReaderResponse
;
808 Demod
.state
= DEMOD_UNSYNCD
;
818 DbpString("cancelled_a");
823 DbpString("COMMAND FINISHED");
825 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
826 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
829 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
830 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
831 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
838 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) {
840 for(i
= 0; i
< 8; i
++) {
841 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8] << 5);
845 //-----------------------------------------------------------------------------
846 // Wait for commands from reader
847 // Stop when button is pressed
848 // Or return TRUE when command is captured
849 //-----------------------------------------------------------------------------
850 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
852 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
853 // only, since we are receiving, not transmitting).
854 // Signal field is off with the appropriate LED
856 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
858 // Now run a `software UART' on the stream of incoming samples.
859 Uart
.output
= received
;
860 Uart
.byteCntMax
= maxLen
;
861 Uart
.state
= STATE_UNSYNCD
;
866 if(BUTTON_PRESS()) return FALSE
;
868 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
869 AT91C_BASE_SSC
->SSC_THR
= 0x00;
871 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
872 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
873 /*if(OutOfNDecoding((b & 0xf0) >> 4)) {
877 if(OutOfNDecoding(b
& 0x0f)) {
886 //-----------------------------------------------------------------------------
887 // Prepare tag messages
888 //-----------------------------------------------------------------------------
889 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
)
891 //So far a dummy implementation, not used
892 //int lastProxToAirDuration =0;
898 ToSend
[++ToSendMax
] = 0x00;
899 ToSend
[++ToSendMax
] = 0x00;
900 ToSend
[++ToSendMax
] = 0x00;
901 ToSend
[++ToSendMax
] = 0xff;//Proxtoair duration starts here
902 ToSend
[++ToSendMax
] = 0xff;
903 ToSend
[++ToSendMax
] = 0xff;
904 ToSend
[++ToSendMax
] = 0x00;
905 ToSend
[++ToSendMax
] = 0xff;
907 for(i
= 0; i
< len
; i
++) {
912 for(j
= 0; j
< 8; j
++) {
914 ToSend
[++ToSendMax
] = 0x00;
915 ToSend
[++ToSendMax
] = 0xff;
917 ToSend
[++ToSendMax
] = 0xff;
918 ToSend
[++ToSendMax
] = 0x00;
925 ToSend
[++ToSendMax
] = 0xff;
926 ToSend
[++ToSendMax
] = 0x00;
927 ToSend
[++ToSendMax
] = 0xff;
928 ToSend
[++ToSendMax
] = 0xff;
929 ToSend
[++ToSendMax
] = 0xff;
930 ToSend
[++ToSendMax
] = 0x00;
931 ToSend
[++ToSendMax
] = 0x00;
932 ToSend
[++ToSendMax
] = 0x00;
934 //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end
936 // Convert from last byte pos to length
941 static void CodeIClassTagSOF()
943 //So far a dummy implementation, not used
944 //int lastProxToAirDuration =0;
948 ToSend
[++ToSendMax
] = 0x00;
949 ToSend
[++ToSendMax
] = 0x00;
950 ToSend
[++ToSendMax
] = 0x00;
951 ToSend
[++ToSendMax
] = 0xff;
952 ToSend
[++ToSendMax
] = 0xff;
953 ToSend
[++ToSendMax
] = 0xff;
954 ToSend
[++ToSendMax
] = 0x00;
955 ToSend
[++ToSendMax
] = 0xff;
957 // lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning
960 // Convert from last byte pos to length
963 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
);
965 * @brief SimulateIClass simulates an iClass card.
966 * @param arg0 type of simulation
967 * - 0 uses the first 8 bytes in usb data as CSN
968 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
969 * in the usb data. This mode collects MAC from the reader, in order to do an offline
970 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
971 * - Other : Uses the default CSN (031fec8af7ff12e0)
972 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
976 void SimulateIClass(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
)
978 uint32_t simType
= arg0
;
979 uint32_t numberOfCSNS
= arg1
;
980 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
982 // Enable and clear the trace
983 iso14a_set_tracing(TRUE
);
984 iso14a_clear_trace();
986 uint8_t csn_crc
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
988 // Use the CSN from commandline
989 memcpy(csn_crc
, datain
, 8);
990 doIClassSimulation(csn_crc
,0,NULL
);
991 }else if(simType
== 1)
993 doIClassSimulation(csn_crc
,0,NULL
);
995 else if(simType
== 2)
998 uint8_t mac_responses
[64] = { 0 };
999 Dbprintf("Going into attack mode");
1000 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
1001 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
1002 // in order to obtain the keys, as in the "dismantling iclass"-paper.
1004 for( ; i
< numberOfCSNS
&& i
*8+8 < USB_CMD_DATA_SIZE
; i
++)
1006 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
1008 memcpy(csn_crc
, datain
+(i
*8), 8);
1009 if(doIClassSimulation(csn_crc
,1,mac_responses
))
1011 return; // Button pressed
1014 cmd_send(CMD_ACK
,CMD_SIMULATE_TAG_ICLASS
,i
,0,mac_responses
,i
*8);
1018 // We may want a mode here where we hardcode the csns to use (from proxclone).
1019 // That will speed things up a little, but not required just yet.
1020 Dbprintf("The mode is not implemented, reserved for future use");
1022 Dbprintf("Done...");
1026 * @brief Does the actual simulation
1027 * @param csn - csn to use
1028 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1030 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
)
1034 // CSN followed by two CRC bytes
1035 uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1036 uint8_t response3
[] = { 0,0,0,0,0,0,0,0,0,0};
1037 memcpy(response3
,csn
,sizeof(response3
));
1038 Dbprintf("Simulating CSN %02x%02x%02x%02x%02x%02x%02x%02x",csn
[0],csn
[1],csn
[2],csn
[3],csn
[4],csn
[5],csn
[6],csn
[7]);
1040 uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1042 // Construct anticollision-CSN
1043 rotateCSN(response3
,response2
);
1045 // Compute CRC on both CSNs
1046 ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]);
1047 ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]);
1053 // Tag anticoll. CSN
1054 // Reader 81 anticoll. CSN
1059 uint8_t* respdata
= NULL
;
1063 // Respond SOF -- takes 8 bytes
1064 uint8_t *resp1
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
);
1067 // Anticollision CSN (rotated CSN)
1068 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1069 uint8_t *resp2
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 10);
1073 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1074 uint8_t *resp3
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 190);
1078 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
1079 uint8_t *resp4
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 370);
1083 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
1084 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1087 // Prepare card messages
1090 // First card answer: SOF
1092 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1094 // Anticollision CSN
1095 CodeIClassTagAnswer(response2
, sizeof(response2
));
1096 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1099 CodeIClassTagAnswer(response3
, sizeof(response3
));
1100 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1103 CodeIClassTagAnswer(response4
, sizeof(response4
));
1104 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1107 // Start from off (no field generated)
1108 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1110 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
1113 // We need to listen to the high-frequency, peak-detected path.
1114 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1117 // To control where we are in the protocol
1119 uint32_t time_0
= GetCountSspClk();
1120 uint32_t t2r_time
=0;
1121 uint32_t r2t_time
=0;
1124 bool buttonPressed
= false;
1126 /** Hack for testing
1127 memcpy(reader_mac_buf,csn,8);
1135 // Can be used to get a trigger for an oscilloscope..
1138 if(!GetIClassCommandFromReader(receivedCmd
, &len
, 100)) {
1139 buttonPressed
= true;
1142 r2t_time
= GetCountSspClk();
1146 // Okay, look at the command now.
1147 if(receivedCmd
[0] == 0x0a ) {
1148 // Reader in anticollission phase
1149 resp
= resp1
; respLen
= resp1Len
; //order = 1;
1151 respsize
= sizeof(sof
);
1152 } else if(receivedCmd
[0] == 0x0c) {
1153 // Reader asks for anticollission CSN
1154 resp
= resp2
; respLen
= resp2Len
; //order = 2;
1155 respdata
= response2
;
1156 respsize
= sizeof(response2
);
1157 //DbpString("Reader requests anticollission CSN:");
1158 } else if(receivedCmd
[0] == 0x81) {
1159 // Reader selects anticollission CSN.
1160 // Tag sends the corresponding real CSN
1161 resp
= resp3
; respLen
= resp3Len
; //order = 3;
1162 respdata
= response3
;
1163 respsize
= sizeof(response3
);
1164 //DbpString("Reader selects anticollission CSN:");
1165 } else if(receivedCmd
[0] == 0x88) {
1166 // Read e-purse (88 02)
1167 resp
= resp4
; respLen
= resp4Len
; //order = 4;
1168 respdata
= response4
;
1169 respsize
= sizeof(response4
);
1171 } else if(receivedCmd
[0] == 0x05) {
1172 // Reader random and reader MAC!!!
1174 // We do not know what to answer, so lets keep quiet
1175 resp
= resp1
; respLen
= 0; //order = 5;
1178 if (breakAfterMacReceived
){
1180 Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x",csn
[0],csn
[1],csn
[2],csn
[3],csn
[4],csn
[5],csn
[6],csn
[7]);
1181 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len
,
1182 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1183 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1184 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1185 if (reader_mac_buf
!= NULL
)
1187 memcpy(reader_mac_buf
,receivedCmd
+1,8);
1191 } else if(receivedCmd
[0] == 0x00 && len
== 1) {
1192 // Reader ends the session
1193 resp
= resp1
; respLen
= 0; //order = 0;
1197 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1198 // Never seen this command before
1199 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1201 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1202 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1203 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1205 resp
= resp1
; respLen
= 0; //order = 0;
1210 if(cmdsRecvd
> 100) {
1211 //DbpString("100 commands later...");
1219 SendIClassAnswer(resp
, respLen
, 21);
1220 t2r_time
= GetCountSspClk();
1224 LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, Uart
.parityBits
,TRUE
);
1225 LogTrace(NULL
,0, (r2t_time
-time_0
) << 4, 0,TRUE
);
1227 if (respdata
!= NULL
) {
1228 LogTrace(respdata
,respsize
, (t2r_time
-time_0
) << 4,SwapBits(GetParity(respdata
,respsize
),respsize
),FALSE
);
1229 LogTrace(NULL
,0, (t2r_time
-time_0
) << 4,0,FALSE
);
1234 DbpString("Trace full");
1239 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1242 //Dbprintf("%x", cmdsRecvd);
1247 DbpString("Button pressed");
1249 return buttonPressed
;
1252 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1254 int i
= 0, d
=0;//, u = 0, d = 0;
1257 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
1259 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1261 while(!BUTTON_PRESS()) {
1262 if((AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)){
1263 b
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
;
1265 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)){
1278 AT91C_BASE_SSC
->SSC_THR
= b
;
1281 if (i
> respLen
+4) break;
1289 //-----------------------------------------------------------------------------
1290 // Transmit the command (to the tag) that was placed in ToSend[].
1291 //-----------------------------------------------------------------------------
1292 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1295 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1296 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1303 for(c
= 0; c
< *wait
;) {
1304 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1305 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1308 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1309 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1316 bool firstpart
= TRUE
;
1319 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1321 // DOUBLE THE SAMPLES!
1323 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1326 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1329 if(sendbyte
== 0xff) {
1332 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1333 firstpart
= !firstpart
;
1339 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1340 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1345 if (samples
) *samples
= (c
+ *wait
) << 3;
1349 //-----------------------------------------------------------------------------
1350 // Prepare iClass reader command to send to FPGA
1351 //-----------------------------------------------------------------------------
1352 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1359 // Start of Communication: 1 out of 4
1360 ToSend
[++ToSendMax
] = 0xf0;
1361 ToSend
[++ToSendMax
] = 0x00;
1362 ToSend
[++ToSendMax
] = 0x0f;
1363 ToSend
[++ToSendMax
] = 0x00;
1365 // Modulate the bytes
1366 for (i
= 0; i
< len
; i
++) {
1368 for(j
= 0; j
< 4; j
++) {
1369 for(k
= 0; k
< 4; k
++) {
1371 ToSend
[++ToSendMax
] = 0x0f;
1374 ToSend
[++ToSendMax
] = 0x00;
1381 // End of Communication
1382 ToSend
[++ToSendMax
] = 0x00;
1383 ToSend
[++ToSendMax
] = 0x00;
1384 ToSend
[++ToSendMax
] = 0xf0;
1385 ToSend
[++ToSendMax
] = 0x00;
1387 // Convert from last character reference to length
1391 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1397 // This is tied to other size changes
1398 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1399 CodeIClassCommand(frame
,len
);
1402 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1406 // Store reader command in buffer
1407 if (tracing
) LogTrace(frame
,len
,rsamples
,par
,TRUE
);
1410 //-----------------------------------------------------------------------------
1411 // Wait a certain time for tag response
1412 // If a response is captured return TRUE
1413 // If it takes too long return FALSE
1414 //-----------------------------------------------------------------------------
1415 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1417 // buffer needs to be 512 bytes
1420 // Set FPGA mode to "reader listen mode", no modulation (listen
1421 // only, since we are receiving, not transmitting).
1422 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1424 // Now get the answer from the card
1425 Demod
.output
= receivedResponse
;
1427 Demod
.state
= DEMOD_UNSYNCD
;
1430 if (elapsed
) *elapsed
= 0;
1438 if(BUTTON_PRESS()) return FALSE
;
1440 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1441 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1442 if (elapsed
) (*elapsed
)++;
1444 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1445 if(c
< timeout
) { c
++; } else { return FALSE
; }
1446 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1449 /*if(ManchesterDecoding((b>>4) & 0xf)) {
1450 *samples = ((c - 1) << 3) + 4;
1453 if(ManchesterDecoding(b
& 0x0f)) {
1461 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1464 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1465 rsamples
+= samples
;
1466 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,rsamples
,Demod
.parityBits
,FALSE
);
1467 if(samples
== 0) return FALSE
;
1471 void setupIclassReader()
1473 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1474 // Reset trace buffer
1475 iso14a_set_tracing(TRUE
);
1476 iso14a_clear_trace();
1480 // Start from off (no field generated)
1481 // Signal field is off with the appropriate LED
1483 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1486 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1488 // Now give it time to spin up.
1489 // Signal field is on with the appropriate LED
1490 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1496 // Reader iClass Anticollission
1497 void ReaderIClass(uint8_t arg0
) {
1498 uint8_t act_all
[] = { 0x0a };
1499 uint8_t identify
[] = { 0x0c };
1500 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1501 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1503 uint8_t card_data
[24]={0};
1504 uint8_t last_csn
[8]={0};
1506 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1509 bool abort_after_read
= arg0
& FLAG_ICLASS_READER_ONLY_ONCE
;
1511 setupIclassReader();
1513 size_t datasize
= 0;
1514 while(!BUTTON_PRESS())
1519 ReaderTransmitIClass(act_all
, 1);
1521 if(ReaderReceiveIClass(resp
)) {
1523 ReaderTransmitIClass(identify
, 1);
1525 if(ReaderReceiveIClass(resp
) == 10) {
1526 //Copy the Anti-collision CSN to our select-packet
1527 memcpy(&select
[1],resp
,8);
1528 //Dbprintf("Anti-collision CSN: %02x %02x %02x %02x %02x %02x %02x %02x",resp[0], resp[1], resp[2],
1529 // resp[3], resp[4], resp[5],
1530 // resp[6], resp[7]);
1532 ReaderTransmitIClass(select
, sizeof(select
));
1534 if(ReaderReceiveIClass(resp
) == 10) {
1535 //Save CSN in response data
1536 memcpy(card_data
,resp
,8);
1538 //Flag that we got to at least stage 1, read CSN
1542 //Dbprintf("Readcheck on Sector 2");
1543 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1544 if(ReaderReceiveIClass(resp
) == 8) {
1545 //Save CC (e-purse) in response data
1546 memcpy(card_data
+8,resp
,8);
1553 //Send back to client, but don't bother if we already sent this
1554 if(memcmp(last_csn
, card_data
, 8) != 0)
1555 cmd_send(CMD_ACK
,read_status
,0,0,card_data
,datasize
);
1557 //Save that we already sent this....
1558 if(read_status
== 2)
1559 memcpy(last_csn
, card_data
, 8);
1563 if(abort_after_read
) break;
1568 if(traceLen
> TRACE_SIZE
) {
1569 DbpString("Trace full");
1576 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) {
1577 uint8_t act_all
[] = { 0x0a };
1578 uint8_t identify
[] = { 0x0c };
1579 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1580 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1581 uint8_t check
[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1582 uint8_t read
[] = { 0x0c, 0x00, 0x00, 0x00 };
1586 bool read_success
=false;
1589 static struct memory_t
{
1597 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1599 setupIclassReader();
1602 for(int i
=0;i
<1;i
++) {
1604 if(traceLen
> TRACE_SIZE
) {
1605 DbpString("Trace full");
1609 if (BUTTON_PRESS()) break;
1612 ReaderTransmitIClass(act_all
, 1);
1614 if(ReaderReceiveIClass(resp
)) {
1615 ReaderTransmitIClass(identify
, 1);
1616 if(ReaderReceiveIClass(resp
) == 10) {
1618 memcpy(&select
[1],resp
,8);
1619 ReaderTransmitIClass(select
, sizeof(select
));
1621 if(ReaderReceiveIClass(resp
) == 10) {
1622 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1623 resp
[0], resp
[1], resp
[2],
1624 resp
[3], resp
[4], resp
[5],
1628 Dbprintf("Readcheck on Sector 2");
1629 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1630 if(ReaderReceiveIClass(resp
) == 8) {
1631 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1632 resp
[0], resp
[1], resp
[2],
1633 resp
[3], resp
[4], resp
[5],
1636 Dbprintf("Authenticate");
1637 //for now replay captured auth (as cc not updated)
1638 memcpy(check
+5,MAC
,4);
1639 //Dbprintf(" AA: %02x %02x %02x %02x",
1640 // check[5], check[6], check[7],check[8]);
1641 ReaderTransmitIClass(check
, sizeof(check
));
1642 if(ReaderReceiveIClass(resp
) == 4) {
1643 Dbprintf(" AR: %02x %02x %02x %02x",
1644 resp
[0], resp
[1], resp
[2],resp
[3]);
1646 Dbprintf("Error: Authentication Fail!");
1649 Dbprintf("Dump Contents");
1650 //first get configuration block
1653 uint8_t *blockno
=&read
[1];
1654 crc
= iclass_crc16((char *)blockno
,1);
1656 read
[3] = crc
& 0xff;
1657 while(!read_success
){
1658 ReaderTransmitIClass(read
, sizeof(read
));
1659 if(ReaderReceiveIClass(resp
) == 10) {
1662 memory
.k16
= (mem
& 0x80);
1663 memory
.book
= (mem
& 0x20);
1664 memory
.k2
= (mem
& 0x8);
1665 memory
.lockauth
= (mem
& 0x2);
1666 memory
.keyaccess
= (mem
& 0x1);
1673 //then loop around remaining blocks
1674 for(uint8_t j
=0; j
<cardsize
; j
++){
1676 uint8_t *blockno
=&j
;
1679 crc
= iclass_crc16((char *)blockno
,1);
1681 read
[3] = crc
& 0xff;
1682 while(!read_success
){
1683 ReaderTransmitIClass(read
, sizeof(read
));
1684 if(ReaderReceiveIClass(resp
) == 10) {
1686 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1687 j
, resp
[0], resp
[1], resp
[2],
1688 resp
[3], resp
[4], resp
[5],
1701 //2. Create Read method (cut-down from above) based off responses from 1.
1702 // Since we have the MAC could continue to use replay function.
1703 //3. Create Write method
1705 void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) {
1706 uint8_t act_all[] = { 0x0a };
1707 uint8_t identify[] = { 0x0c };
1708 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1709 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1710 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1711 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1712 uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1716 uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
1718 // Reset trace buffer
1719 memset(trace, 0x44, RECV_CMD_OFFSET);
1724 // Start from off (no field generated)
1725 // Signal field is off with the appropriate LED
1727 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1730 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1732 // Now give it time to spin up.
1733 // Signal field is on with the appropriate LED
1734 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1739 for(int i=0;i<1;i++) {
1741 if(traceLen > TRACE_SIZE) {
1742 DbpString("Trace full");
1746 if (BUTTON_PRESS()) break;
1749 ReaderTransmitIClass(act_all, 1);
1751 if(ReaderReceiveIClass(resp)) {
1752 ReaderTransmitIClass(identify, 1);
1753 if(ReaderReceiveIClass(resp) == 10) {
1755 memcpy(&select[1],resp,8);
1756 ReaderTransmitIClass(select, sizeof(select));
1758 if(ReaderReceiveIClass(resp) == 10) {
1759 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1760 resp[0], resp[1], resp[2],
1761 resp[3], resp[4], resp[5],
1765 Dbprintf("Readcheck on Sector 2");
1766 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1767 if(ReaderReceiveIClass(resp) == 8) {
1768 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1769 resp[0], resp[1], resp[2],
1770 resp[3], resp[4], resp[5],
1773 Dbprintf("Authenticate");
1774 //for now replay captured auth (as cc not updated)
1775 memcpy(check+5,MAC,4);
1776 Dbprintf(" AA: %02x %02x %02x %02x",
1777 check[5], check[6], check[7],check[8]);
1778 ReaderTransmitIClass(check, sizeof(check));
1779 if(ReaderReceiveIClass(resp) == 4) {
1780 Dbprintf(" AR: %02x %02x %02x %02x",
1781 resp[0], resp[1], resp[2],resp[3]);
1783 Dbprintf("Error: Authentication Fail!");
1786 Dbprintf("Write Block");
1788 //read configuration for max block number
1791 uint8_t *blockno=&read[1];
1792 crc = iclass_crc16((char *)blockno,1);
1794 read[3] = crc & 0xff;
1795 while(!read_success){
1796 ReaderTransmitIClass(read, sizeof(read));
1797 if(ReaderReceiveIClass(resp) == 10) {
1800 memory.k16= (mem & 0x80);
1801 memory.book= (mem & 0x20);
1802 memory.k2= (mem & 0x8);
1803 memory.lockauth= (mem & 0x2);
1804 memory.keyaccess= (mem & 0x1);
1813 memcpy(write+1,blockNo,1);
1814 memcpy(write+2,data,8);
1815 memcpy(write+10,mac,4);
1816 while(!send_success){
1817 ReaderTransmitIClass(write, sizeof(write));
1818 if(ReaderReceiveIClass(resp) == 10) {