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
,
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
[0] = 0xf0;
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);
230 else if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
) {
233 if(!Uart
.dropPosition
) {
234 Uart
.state
= STATE_UNSYNCD
;
240 Uart
.output
[Uart
.byteCnt
] = (Uart
.dropPosition
& 0xff);
245 Uart
.dropPosition
= 0;
250 Uart.output[Uart.byteCnt] = 0xAA;
252 Uart.output[Uart.byteCnt] = error & 0xFF;
254 Uart.output[Uart.byteCnt] = 0xAA;
256 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
258 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
260 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
262 Uart.output[Uart.byteCnt] = 0xAA;
270 bit
= Uart
.bitBuffer
& 0xf0;
272 bit
^= 0x0F; // drops become 1s ;-)
274 // should have been high or at least (4 * 128) / fc
275 // according to ISO this should be at least (9 * 128 + 20) / fc
276 if(Uart
.highCnt
== 8) {
277 // we went low, so this could be start of communication
278 // it turns out to be safer to choose a less significant
279 // syncbit... so we check whether the neighbour also represents the drop
280 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
281 Uart
.syncBit
= bit
& 8;
283 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
284 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
285 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
286 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
287 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
288 if(Uart
.syncBit
&& (Uart
.bitBuffer
& 8)) {
291 // the first half bit period is expected in next sample
296 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
299 Uart
.state
= STATE_START_OF_COMMUNICATION
;
303 Uart
.OutOfCnt
= 4; // Start at 1/4, could switch to 1/256
304 Uart
.dropPosition
= 0;
313 if(Uart
.highCnt
< 8) {
322 //=============================================================================
324 //=============================================================================
329 DEMOD_START_OF_COMMUNICATION
,
330 DEMOD_START_OF_COMMUNICATION2
,
331 DEMOD_START_OF_COMMUNICATION3
,
335 DEMOD_END_OF_COMMUNICATION
,
336 DEMOD_END_OF_COMMUNICATION2
,
359 static RAMFUNC
int ManchesterDecoding(int v
)
366 Demod
.buffer
= Demod
.buffer2
;
367 Demod
.buffer2
= Demod
.buffer3
;
375 if(Demod
.state
==DEMOD_UNSYNCD
) {
376 Demod
.output
[Demod
.len
] = 0xfa;
379 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
382 Demod
.syncBit
= 0x08;
389 Demod
.syncBit
= 0x04;
396 Demod
.syncBit
= 0x02;
399 if(bit
& 0x01 && Demod
.syncBit
) {
400 Demod
.syncBit
= 0x01;
405 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
406 Demod
.sub
= SUB_FIRST_HALF
;
411 //if(trigger) LED_A_OFF(); // Not useful in this case...
412 switch(Demod
.syncBit
) {
413 case 0x08: Demod
.samples
= 3; break;
414 case 0x04: Demod
.samples
= 2; break;
415 case 0x02: Demod
.samples
= 1; break;
416 case 0x01: Demod
.samples
= 0; break;
418 // SOF must be long burst... otherwise stay unsynced!!!
419 if(!(Demod
.buffer
& Demod
.syncBit
) || !(Demod
.buffer2
& Demod
.syncBit
)) {
420 Demod
.state
= DEMOD_UNSYNCD
;
424 // SOF must be long burst... otherwise stay unsynced!!!
425 if(!(Demod
.buffer2
& Demod
.syncBit
) || !(Demod
.buffer3
& Demod
.syncBit
)) {
426 Demod
.state
= DEMOD_UNSYNCD
;
436 modulation
= bit
& Demod
.syncBit
;
437 modulation
|= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
441 if(Demod
.posCount
==0) {
444 Demod
.sub
= SUB_FIRST_HALF
;
447 Demod
.sub
= SUB_NONE
;
452 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
453 if(Demod.state!=DEMOD_ERROR_WAIT) {
454 Demod.state = DEMOD_ERROR_WAIT;
455 Demod.output[Demod.len] = 0xaa;
459 //else if(modulation) {
461 if(Demod
.sub
== SUB_FIRST_HALF
) {
462 Demod
.sub
= SUB_BOTH
;
465 Demod
.sub
= SUB_SECOND_HALF
;
468 else if(Demod
.sub
== SUB_NONE
) {
469 if(Demod
.state
== DEMOD_SOF_COMPLETE
) {
470 Demod
.output
[Demod
.len
] = 0x0f;
472 Demod
.state
= DEMOD_UNSYNCD
;
477 Demod
.state
= DEMOD_ERROR_WAIT
;
480 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
481 Demod.state = DEMOD_ERROR_WAIT;
482 Demod.output[Demod.len] = 0xaa;
487 switch(Demod
.state
) {
488 case DEMOD_START_OF_COMMUNICATION
:
489 if(Demod
.sub
== SUB_BOTH
) {
490 //Demod.state = DEMOD_MANCHESTER_D;
491 Demod
.state
= DEMOD_START_OF_COMMUNICATION2
;
493 Demod
.sub
= SUB_NONE
;
496 Demod
.output
[Demod
.len
] = 0xab;
497 Demod
.state
= DEMOD_ERROR_WAIT
;
501 case DEMOD_START_OF_COMMUNICATION2
:
502 if(Demod
.sub
== SUB_SECOND_HALF
) {
503 Demod
.state
= DEMOD_START_OF_COMMUNICATION3
;
506 Demod
.output
[Demod
.len
] = 0xab;
507 Demod
.state
= DEMOD_ERROR_WAIT
;
511 case DEMOD_START_OF_COMMUNICATION3
:
512 if(Demod
.sub
== SUB_SECOND_HALF
) {
513 // Demod.state = DEMOD_MANCHESTER_D;
514 Demod
.state
= DEMOD_SOF_COMPLETE
;
515 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
519 Demod
.output
[Demod
.len
] = 0xab;
520 Demod
.state
= DEMOD_ERROR_WAIT
;
524 case DEMOD_SOF_COMPLETE
:
525 case DEMOD_MANCHESTER_D
:
526 case DEMOD_MANCHESTER_E
:
527 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
528 // 00001111 = 1 (0 in 14443)
529 if(Demod
.sub
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF
531 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
532 Demod
.state
= DEMOD_MANCHESTER_D
;
534 else if(Demod
.sub
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF
536 Demod
.shiftReg
>>= 1;
537 Demod
.state
= DEMOD_MANCHESTER_E
;
539 else if(Demod
.sub
== SUB_BOTH
) {
540 Demod
.state
= DEMOD_MANCHESTER_F
;
543 Demod
.state
= DEMOD_ERROR_WAIT
;
548 case DEMOD_MANCHESTER_F
:
549 // Tag response does not need to be a complete byte!
550 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
551 if(Demod
.bitCount
> 1) { // was > 0, do not interpret last closing bit, is part of EOF
552 Demod
.shiftReg
>>= (9 - Demod
.bitCount
); // rright align data
553 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
557 Demod
.state
= DEMOD_UNSYNCD
;
561 Demod
.output
[Demod
.len
] = 0xad;
562 Demod
.state
= DEMOD_ERROR_WAIT
;
567 case DEMOD_ERROR_WAIT
:
568 Demod
.state
= DEMOD_UNSYNCD
;
572 Demod
.output
[Demod
.len
] = 0xdd;
573 Demod
.state
= DEMOD_UNSYNCD
;
577 /*if(Demod.bitCount>=9) {
578 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
581 Demod.parityBits <<= 1;
582 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
587 if(Demod
.bitCount
>=8) {
588 Demod
.shiftReg
>>= 1;
589 Demod
.output
[Demod
.len
] = (Demod
.shiftReg
& 0xff);
596 Demod
.output
[Demod
.len
] = 0xBB;
598 Demod
.output
[Demod
.len
] = error
& 0xFF;
600 Demod
.output
[Demod
.len
] = 0xBB;
602 Demod
.output
[Demod
.len
] = bit
& 0xFF;
604 Demod
.output
[Demod
.len
] = Demod
.buffer
& 0xFF;
607 Demod
.output
[Demod
.len
] = Demod
.buffer2
& 0xFF;
609 Demod
.output
[Demod
.len
] = Demod
.syncBit
& 0xFF;
611 Demod
.output
[Demod
.len
] = 0xBB;
618 } // end (state != UNSYNCED)
623 //=============================================================================
624 // Finally, a `sniffer' for iClass communication
625 // Both sides of communication!
626 //=============================================================================
628 //-----------------------------------------------------------------------------
629 // Record the sequence of commands sent by the reader to the tag, with
630 // triggering so that we start recording at the point that the tag is moved
632 //-----------------------------------------------------------------------------
633 void RAMFUNC
SnoopIClass(void)
637 // We won't start recording the frames that we acquire until we trigger;
638 // a good trigger condition to get started is probably when we see a
639 // response from the tag.
640 //int triggered = FALSE; // FALSE to wait first for card
642 // The command (reader -> tag) that we're receiving.
643 // The length of a received command will in most cases be no more than 18 bytes.
644 // So 32 should be enough!
645 uint8_t *readerToTagCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
646 // The response (tag -> reader) that we're receiving.
647 uint8_t *tagToReaderResponse
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
649 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
651 // reset traceLen to 0
652 iso14a_set_tracing(TRUE
);
653 iso14a_clear_trace();
654 iso14a_set_trigger(FALSE
);
656 // The DMA buffer, used to stream samples from the FPGA
657 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
663 // Count of samples received so far, so that we can include timing
664 // information in the trace buffer.
668 // Set up the demodulator for tag -> reader responses.
669 Demod
.output
= tagToReaderResponse
;
671 Demod
.state
= DEMOD_UNSYNCD
;
673 // Setup for the DMA.
676 lastRxCounter
= DMA_BUFFER_SIZE
;
677 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
679 // And the reader -> tag commands
680 memset(&Uart
, 0, sizeof(Uart
));
681 Uart
.output
= readerToTagCmd
;
682 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
683 Uart
.state
= STATE_UNSYNCD
;
685 // And put the FPGA in the appropriate mode
686 // Signal field is off with the appropriate LED
688 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
689 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
691 uint32_t time_0
= GetCountSspClk();
699 // And now we loop, receiving samples.
703 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
705 if(behindBy
> maxBehindBy
) {
706 maxBehindBy
= behindBy
;
708 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
712 if(behindBy
< 1) continue;
718 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
719 upTo
-= DMA_BUFFER_SIZE
;
720 lastRxCounter
+= DMA_BUFFER_SIZE
;
721 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
722 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
729 decbyte
^= (1 << (3 - div
));
732 // FOR READER SIDE COMMUMICATION...
735 decbyter
^= (smpl
& 0x30);
739 if((div
+ 1) % 2 == 0) {
741 if(OutOfNDecoding((smpl
& 0xF0) >> 4)) {
742 rsamples
= samples
- Uart
.samples
;
745 //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
746 //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
748 uint8_t parity
[MAX_PARITY_SIZE
];
749 GetParity(Uart
.output
, Uart
.byteCnt
, parity
);
750 LogTrace(Uart
.output
,Uart
.byteCnt
, (GetCountSspClk()-time_0
) << 4, (GetCountSspClk()-time_0
) << 4, parity
, TRUE
);
754 /* And ready to receive another command. */
755 Uart
.state
= STATE_UNSYNCD
;
756 /* And also reset the demod code, which might have been */
757 /* false-triggered by the commands from the reader. */
758 Demod
.state
= DEMOD_UNSYNCD
;
767 if(ManchesterDecoding(smpl
& 0x0F)) {
768 rsamples
= samples
- Demod
.samples
;
772 uint8_t parity
[MAX_PARITY_SIZE
];
773 GetParity(Demod
.output
, Demod
.len
, parity
);
774 LogTrace(Demod
.output
, Demod
.len
, (GetCountSspClk()-time_0
) << 4, (GetCountSspClk()-time_0
) << 4, parity
, FALSE
);
778 // And ready to receive another response.
779 memset(&Demod
, 0, sizeof(Demod
));
780 Demod
.output
= tagToReaderResponse
;
781 Demod
.state
= DEMOD_UNSYNCD
;
791 DbpString("cancelled_a");
796 DbpString("COMMAND FINISHED");
798 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
799 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
802 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
803 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
804 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
811 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) {
813 for(i
= 0; i
< 8; i
++) {
814 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8] << 5);
818 //-----------------------------------------------------------------------------
819 // Wait for commands from reader
820 // Stop when button is pressed
821 // Or return TRUE when command is captured
822 //-----------------------------------------------------------------------------
823 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
825 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
826 // only, since we are receiving, not transmitting).
827 // Signal field is off with the appropriate LED
829 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
831 // Now run a `software UART' on the stream of incoming samples.
832 Uart
.output
= received
;
833 Uart
.byteCntMax
= maxLen
;
834 Uart
.state
= STATE_UNSYNCD
;
839 if(BUTTON_PRESS()) return FALSE
;
841 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
842 AT91C_BASE_SSC
->SSC_THR
= 0x00;
844 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
845 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
847 if(OutOfNDecoding(b
& 0x0f)) {
856 //-----------------------------------------------------------------------------
857 // Prepare tag messages
858 //-----------------------------------------------------------------------------
859 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
)
861 //So far a dummy implementation, not used
862 //int lastProxToAirDuration =0;
868 ToSend
[++ToSendMax
] = 0x00;
869 ToSend
[++ToSendMax
] = 0x00;
870 ToSend
[++ToSendMax
] = 0x00;
871 ToSend
[++ToSendMax
] = 0xff;//Proxtoair duration starts here
872 ToSend
[++ToSendMax
] = 0xff;
873 ToSend
[++ToSendMax
] = 0xff;
874 ToSend
[++ToSendMax
] = 0x00;
875 ToSend
[++ToSendMax
] = 0xff;
877 for(i
= 0; i
< len
; i
++) {
882 for(j
= 0; j
< 8; j
++) {
884 ToSend
[++ToSendMax
] = 0x00;
885 ToSend
[++ToSendMax
] = 0xff;
887 ToSend
[++ToSendMax
] = 0xff;
888 ToSend
[++ToSendMax
] = 0x00;
895 ToSend
[++ToSendMax
] = 0xff;
896 ToSend
[++ToSendMax
] = 0x00;
897 ToSend
[++ToSendMax
] = 0xff;
898 ToSend
[++ToSendMax
] = 0xff;
899 ToSend
[++ToSendMax
] = 0xff;
900 ToSend
[++ToSendMax
] = 0x00;
901 ToSend
[++ToSendMax
] = 0x00;
902 ToSend
[++ToSendMax
] = 0x00;
904 //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end
906 // Convert from last byte pos to length
911 static void CodeIClassTagSOF()
913 //So far a dummy implementation, not used
914 //int lastProxToAirDuration =0;
918 ToSend
[++ToSendMax
] = 0x00;
919 ToSend
[++ToSendMax
] = 0x00;
920 ToSend
[++ToSendMax
] = 0x00;
921 ToSend
[++ToSendMax
] = 0xff;
922 ToSend
[++ToSendMax
] = 0xff;
923 ToSend
[++ToSendMax
] = 0xff;
924 ToSend
[++ToSendMax
] = 0x00;
925 ToSend
[++ToSendMax
] = 0xff;
927 // lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning
930 // Convert from last byte pos to length
933 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
);
935 * @brief SimulateIClass simulates an iClass card.
936 * @param arg0 type of simulation
937 * - 0 uses the first 8 bytes in usb data as CSN
938 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
939 * in the usb data. This mode collects MAC from the reader, in order to do an offline
940 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
941 * - Other : Uses the default CSN (031fec8af7ff12e0)
942 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
946 void SimulateIClass(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
)
948 uint32_t simType
= arg0
;
949 uint32_t numberOfCSNS
= arg1
;
950 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
952 // Enable and clear the trace
953 iso14a_set_tracing(TRUE
);
954 iso14a_clear_trace();
956 uint8_t csn_crc
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
958 // Use the CSN from commandline
959 memcpy(csn_crc
, datain
, 8);
960 doIClassSimulation(csn_crc
,0,NULL
);
961 }else if(simType
== 1)
963 doIClassSimulation(csn_crc
,0,NULL
);
965 else if(simType
== 2)
968 uint8_t mac_responses
[64] = { 0 };
969 Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS
);
970 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
971 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
972 // in order to obtain the keys, as in the "dismantling iclass"-paper.
974 for( ; i
< numberOfCSNS
&& i
*8+8 < USB_CMD_DATA_SIZE
; i
++)
976 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
978 memcpy(csn_crc
, datain
+(i
*8), 8);
979 if(doIClassSimulation(csn_crc
,1,mac_responses
+i
*8))
981 return; // Button pressed
984 cmd_send(CMD_ACK
,CMD_SIMULATE_TAG_ICLASS
,i
,0,mac_responses
,i
*8);
988 // We may want a mode here where we hardcode the csns to use (from proxclone).
989 // That will speed things up a little, but not required just yet.
990 Dbprintf("The mode is not implemented, reserved for future use");
996 * @brief Does the actual simulation
997 * @param csn - csn to use
998 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1000 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
)
1002 // CSN followed by two CRC bytes
1003 uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1004 uint8_t response3
[] = { 0,0,0,0,0,0,0,0,0,0};
1005 memcpy(response3
,csn
,sizeof(response3
));
1006 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]);
1008 uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1010 // Construct anticollision-CSN
1011 rotateCSN(response3
,response2
);
1013 // Compute CRC on both CSNs
1014 ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]);
1015 ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]);
1021 // Tag anticoll. CSN
1022 // Reader 81 anticoll. CSN
1027 uint8_t* respdata
= NULL
;
1031 // Respond SOF -- takes 8 bytes
1032 uint8_t *resp1
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
);
1035 // Anticollision CSN (rotated CSN)
1036 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1037 uint8_t *resp2
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 10);
1041 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1042 uint8_t *resp3
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 190);
1046 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
1047 uint8_t *resp4
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 370);
1051 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
1052 memset(receivedCmd
, 0x44, MAX_FRAME_SIZE
);
1055 // Prepare card messages
1058 // First card answer: SOF
1060 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1062 // Anticollision CSN
1063 CodeIClassTagAnswer(response2
, sizeof(response2
));
1064 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1067 CodeIClassTagAnswer(response3
, sizeof(response3
));
1068 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1071 CodeIClassTagAnswer(response4
, sizeof(response4
));
1072 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1075 // Start from off (no field generated)
1076 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1078 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
1081 // We need to listen to the high-frequency, peak-detected path.
1082 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1085 // To control where we are in the protocol
1087 uint32_t time_0
= GetCountSspClk();
1088 uint32_t t2r_time
=0;
1089 uint32_t r2t_time
=0;
1092 bool buttonPressed
= false;
1094 /** Hack for testing
1095 memcpy(reader_mac_buf,csn,8);
1103 // Can be used to get a trigger for an oscilloscope..
1106 if(!GetIClassCommandFromReader(receivedCmd
, &len
, 100)) {
1107 buttonPressed
= true;
1110 r2t_time
= GetCountSspClk();
1114 // Okay, look at the command now.
1115 if(receivedCmd
[0] == 0x0a ) {
1116 // Reader in anticollission phase
1117 resp
= resp1
; respLen
= resp1Len
; //order = 1;
1119 respsize
= sizeof(sof
);
1120 } else if(receivedCmd
[0] == 0x0c) {
1121 // Reader asks for anticollission CSN
1122 resp
= resp2
; respLen
= resp2Len
; //order = 2;
1123 respdata
= response2
;
1124 respsize
= sizeof(response2
);
1125 //DbpString("Reader requests anticollission CSN:");
1126 } else if(receivedCmd
[0] == 0x81) {
1127 // Reader selects anticollission CSN.
1128 // Tag sends the corresponding real CSN
1129 resp
= resp3
; respLen
= resp3Len
; //order = 3;
1130 respdata
= response3
;
1131 respsize
= sizeof(response3
);
1132 //DbpString("Reader selects anticollission CSN:");
1133 } else if(receivedCmd
[0] == 0x88) {
1134 // Read e-purse (88 02)
1135 resp
= resp4
; respLen
= resp4Len
; //order = 4;
1136 respdata
= response4
;
1137 respsize
= sizeof(response4
);
1139 } else if(receivedCmd
[0] == 0x05) {
1140 // Reader random and reader MAC!!!
1142 // We do not know what to answer, so lets keep quiet
1143 resp
= resp1
; respLen
= 0; //order = 5;
1146 if (breakAfterMacReceived
){
1148 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]);
1149 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len
,
1150 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1151 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1152 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1153 if (reader_mac_buf
!= NULL
)
1155 memcpy(reader_mac_buf
,receivedCmd
+1,8);
1159 } else if(receivedCmd
[0] == 0x00 && len
== 1) {
1160 // Reader ends the session
1161 resp
= resp1
; respLen
= 0; //order = 0;
1165 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1166 // Never seen this command before
1167 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1169 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1170 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1171 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1173 resp
= resp1
; respLen
= 0; //order = 0;
1178 if(cmdsRecvd
> 100) {
1179 //DbpString("100 commands later...");
1187 SendIClassAnswer(resp
, respLen
, 21);
1188 t2r_time
= GetCountSspClk();
1192 uint8_t parity
[MAX_PARITY_SIZE
];
1193 GetParity(receivedCmd
, len
, parity
);
1194 LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, (r2t_time
-time_0
) << 4, parity
, TRUE
);
1196 if (respdata
!= NULL
) {
1197 GetParity(respdata
, respsize
, parity
);
1198 LogTrace(respdata
, respsize
, (t2r_time
-time_0
) << 4, (t2r_time
-time_0
) << 4, parity
, FALSE
);
1201 DbpString("Trace full");
1206 memset(receivedCmd
, 0x44, MAX_FRAME_SIZE
);
1209 //Dbprintf("%x", cmdsRecvd);
1214 DbpString("Button pressed");
1216 return buttonPressed
;
1219 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1221 int i
= 0, d
=0;//, u = 0, d = 0;
1224 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
1226 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1228 while(!BUTTON_PRESS()) {
1229 if((AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)){
1230 b
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
;
1232 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)){
1245 AT91C_BASE_SSC
->SSC_THR
= b
;
1248 if (i
> respLen
+4) break;
1256 //-----------------------------------------------------------------------------
1257 // Transmit the command (to the tag) that was placed in ToSend[].
1258 //-----------------------------------------------------------------------------
1259 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1262 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1263 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1270 for(c
= 0; c
< *wait
;) {
1271 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1272 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1275 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1276 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1283 bool firstpart
= TRUE
;
1286 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1288 // DOUBLE THE SAMPLES!
1290 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1293 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1296 if(sendbyte
== 0xff) {
1299 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1300 firstpart
= !firstpart
;
1306 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1307 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1312 if (samples
) *samples
= (c
+ *wait
) << 3;
1316 //-----------------------------------------------------------------------------
1317 // Prepare iClass reader command to send to FPGA
1318 //-----------------------------------------------------------------------------
1319 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1326 // Start of Communication: 1 out of 4
1327 ToSend
[++ToSendMax
] = 0xf0;
1328 ToSend
[++ToSendMax
] = 0x00;
1329 ToSend
[++ToSendMax
] = 0x0f;
1330 ToSend
[++ToSendMax
] = 0x00;
1332 // Modulate the bytes
1333 for (i
= 0; i
< len
; i
++) {
1335 for(j
= 0; j
< 4; j
++) {
1336 for(k
= 0; k
< 4; k
++) {
1338 ToSend
[++ToSendMax
] = 0x0f;
1341 ToSend
[++ToSendMax
] = 0x00;
1348 // End of Communication
1349 ToSend
[++ToSendMax
] = 0x00;
1350 ToSend
[++ToSendMax
] = 0x00;
1351 ToSend
[++ToSendMax
] = 0xf0;
1352 ToSend
[++ToSendMax
] = 0x00;
1354 // Convert from last character reference to length
1358 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1363 // This is tied to other size changes
1364 CodeIClassCommand(frame
,len
);
1367 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1371 // Store reader command in buffer
1373 uint8_t par
[MAX_PARITY_SIZE
];
1374 GetParity(frame
, len
, par
);
1375 LogTrace(frame
, len
, rsamples
, rsamples
, par
, TRUE
);
1379 //-----------------------------------------------------------------------------
1380 // Wait a certain time for tag response
1381 // If a response is captured return TRUE
1382 // If it takes too long return FALSE
1383 //-----------------------------------------------------------------------------
1384 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1386 // buffer needs to be 512 bytes
1389 // Set FPGA mode to "reader listen mode", no modulation (listen
1390 // only, since we are receiving, not transmitting).
1391 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1393 // Now get the answer from the card
1394 Demod
.output
= receivedResponse
;
1396 Demod
.state
= DEMOD_UNSYNCD
;
1399 if (elapsed
) *elapsed
= 0;
1407 if(BUTTON_PRESS()) return FALSE
;
1409 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1410 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1411 if (elapsed
) (*elapsed
)++;
1413 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1414 if(c
< timeout
) { c
++; } else { return FALSE
; }
1415 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1419 if(ManchesterDecoding(b
& 0x0f)) {
1427 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1430 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1431 rsamples
+= samples
;
1433 uint8_t parity
[MAX_PARITY_SIZE
];
1434 GetParity(receivedAnswer
, Demod
.len
, parity
);
1435 LogTrace(receivedAnswer
,Demod
.len
,rsamples
,rsamples
,parity
,FALSE
);
1437 if(samples
== 0) return FALSE
;
1441 void setupIclassReader()
1443 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1444 // Reset trace buffer
1445 iso14a_set_tracing(TRUE
);
1446 iso14a_clear_trace();
1450 // Start from off (no field generated)
1451 // Signal field is off with the appropriate LED
1453 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1456 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1458 // Now give it time to spin up.
1459 // Signal field is on with the appropriate LED
1460 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1466 // Reader iClass Anticollission
1467 void ReaderIClass(uint8_t arg0
) {
1468 uint8_t act_all
[] = { 0x0a };
1469 uint8_t identify
[] = { 0x0c };
1470 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1471 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1473 uint8_t card_data
[24]={0};
1474 uint8_t last_csn
[8]={0};
1476 uint8_t *resp
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
1479 bool abort_after_read
= arg0
& FLAG_ICLASS_READER_ONLY_ONCE
;
1481 setupIclassReader();
1483 size_t datasize
= 0;
1484 while(!BUTTON_PRESS())
1489 ReaderTransmitIClass(act_all
, 1);
1491 if(ReaderReceiveIClass(resp
)) {
1493 ReaderTransmitIClass(identify
, 1);
1495 if(ReaderReceiveIClass(resp
) == 10) {
1496 //Copy the Anti-collision CSN to our select-packet
1497 memcpy(&select
[1],resp
,8);
1498 //Dbprintf("Anti-collision CSN: %02x %02x %02x %02x %02x %02x %02x %02x",resp[0], resp[1], resp[2],
1499 // resp[3], resp[4], resp[5],
1500 // resp[6], resp[7]);
1502 ReaderTransmitIClass(select
, sizeof(select
));
1504 if(ReaderReceiveIClass(resp
) == 10) {
1505 //Save CSN in response data
1506 memcpy(card_data
,resp
,8);
1508 //Flag that we got to at least stage 1, read CSN
1512 //Dbprintf("Readcheck on Sector 2");
1513 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1514 if(ReaderReceiveIClass(resp
) == 8) {
1515 //Save CC (e-purse) in response data
1516 memcpy(card_data
+8,resp
,8);
1523 //Send back to client, but don't bother if we already sent this
1524 if(memcmp(last_csn
, card_data
, 8) != 0)
1525 cmd_send(CMD_ACK
,read_status
,0,0,card_data
,datasize
);
1527 //Save that we already sent this....
1528 if(read_status
== 2)
1529 memcpy(last_csn
, card_data
, 8);
1533 if(abort_after_read
) break;
1538 if(traceLen
> TRACE_SIZE
) {
1539 DbpString("Trace full");
1546 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) {
1547 uint8_t act_all
[] = { 0x0a };
1548 uint8_t identify
[] = { 0x0c };
1549 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1550 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1551 uint8_t check
[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1552 uint8_t read
[] = { 0x0c, 0x00, 0x00, 0x00 };
1556 bool read_success
=false;
1559 static struct memory_t
{
1567 uint8_t* resp
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
1569 setupIclassReader();
1572 for(int i
=0;i
<1;i
++) {
1574 if(traceLen
> TRACE_SIZE
) {
1575 DbpString("Trace full");
1579 if (BUTTON_PRESS()) break;
1582 ReaderTransmitIClass(act_all
, 1);
1584 if(ReaderReceiveIClass(resp
)) {
1585 ReaderTransmitIClass(identify
, 1);
1586 if(ReaderReceiveIClass(resp
) == 10) {
1588 memcpy(&select
[1],resp
,8);
1589 ReaderTransmitIClass(select
, sizeof(select
));
1591 if(ReaderReceiveIClass(resp
) == 10) {
1592 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1593 resp
[0], resp
[1], resp
[2],
1594 resp
[3], resp
[4], resp
[5],
1598 Dbprintf("Readcheck on Sector 2");
1599 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1600 if(ReaderReceiveIClass(resp
) == 8) {
1601 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1602 resp
[0], resp
[1], resp
[2],
1603 resp
[3], resp
[4], resp
[5],
1606 Dbprintf("Authenticate");
1607 //for now replay captured auth (as cc not updated)
1608 memcpy(check
+5,MAC
,4);
1609 //Dbprintf(" AA: %02x %02x %02x %02x",
1610 // check[5], check[6], check[7],check[8]);
1611 ReaderTransmitIClass(check
, sizeof(check
));
1612 if(ReaderReceiveIClass(resp
) == 4) {
1613 Dbprintf(" AR: %02x %02x %02x %02x",
1614 resp
[0], resp
[1], resp
[2],resp
[3]);
1616 Dbprintf("Error: Authentication Fail!");
1619 Dbprintf("Dump Contents");
1620 //first get configuration block
1623 uint8_t *blockno
=&read
[1];
1624 crc
= iclass_crc16((char *)blockno
,1);
1626 read
[3] = crc
& 0xff;
1627 while(!read_success
){
1628 ReaderTransmitIClass(read
, sizeof(read
));
1629 if(ReaderReceiveIClass(resp
) == 10) {
1632 memory
.k16
= (mem
& 0x80);
1633 memory
.book
= (mem
& 0x20);
1634 memory
.k2
= (mem
& 0x8);
1635 memory
.lockauth
= (mem
& 0x2);
1636 memory
.keyaccess
= (mem
& 0x1);
1643 //then loop around remaining blocks
1644 for(uint8_t j
=0; j
<cardsize
; j
++){
1646 uint8_t *blockno
=&j
;
1649 crc
= iclass_crc16((char *)blockno
,1);
1651 read
[3] = crc
& 0xff;
1652 while(!read_success
){
1653 ReaderTransmitIClass(read
, sizeof(read
));
1654 if(ReaderReceiveIClass(resp
) == 10) {
1656 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1657 j
, resp
[0], resp
[1], resp
[2],
1658 resp
[3], resp
[4], resp
[5],
1671 //2. Create Read method (cut-down from above) based off responses from 1.
1672 // Since we have the MAC could continue to use replay function.
1673 //3. Create Write method
1675 void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) {
1676 uint8_t act_all[] = { 0x0a };
1677 uint8_t identify[] = { 0x0c };
1678 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1679 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1680 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1681 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1682 uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1686 uint8_t* resp = (((uint8_t *)BigBuf) + RECV_RESP_OFFSET);
1688 // Reset trace buffer
1689 memset(trace, 0x44, RECV_CMD_OFFSET);
1694 // Start from off (no field generated)
1695 // Signal field is off with the appropriate LED
1697 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1700 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1702 // Now give it time to spin up.
1703 // Signal field is on with the appropriate LED
1704 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1709 for(int i=0;i<1;i++) {
1711 if(traceLen > TRACE_SIZE) {
1712 DbpString("Trace full");
1716 if (BUTTON_PRESS()) break;
1719 ReaderTransmitIClass(act_all, 1);
1721 if(ReaderReceiveIClass(resp)) {
1722 ReaderTransmitIClass(identify, 1);
1723 if(ReaderReceiveIClass(resp) == 10) {
1725 memcpy(&select[1],resp,8);
1726 ReaderTransmitIClass(select, sizeof(select));
1728 if(ReaderReceiveIClass(resp) == 10) {
1729 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1730 resp[0], resp[1], resp[2],
1731 resp[3], resp[4], resp[5],
1735 Dbprintf("Readcheck on Sector 2");
1736 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1737 if(ReaderReceiveIClass(resp) == 8) {
1738 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1739 resp[0], resp[1], resp[2],
1740 resp[3], resp[4], resp[5],
1743 Dbprintf("Authenticate");
1744 //for now replay captured auth (as cc not updated)
1745 memcpy(check+5,MAC,4);
1746 Dbprintf(" AA: %02x %02x %02x %02x",
1747 check[5], check[6], check[7],check[8]);
1748 ReaderTransmitIClass(check, sizeof(check));
1749 if(ReaderReceiveIClass(resp) == 4) {
1750 Dbprintf(" AR: %02x %02x %02x %02x",
1751 resp[0], resp[1], resp[2],resp[3]);
1753 Dbprintf("Error: Authentication Fail!");
1756 Dbprintf("Write Block");
1758 //read configuration for max block number
1761 uint8_t *blockno=&read[1];
1762 crc = iclass_crc16((char *)blockno,1);
1764 read[3] = crc & 0xff;
1765 while(!read_success){
1766 ReaderTransmitIClass(read, sizeof(read));
1767 if(ReaderReceiveIClass(resp) == 10) {
1770 memory.k16= (mem & 0x80);
1771 memory.book= (mem & 0x20);
1772 memory.k2= (mem & 0x8);
1773 memory.lockauth= (mem & 0x2);
1774 memory.keyaccess= (mem & 0x1);
1783 memcpy(write+1,blockNo,1);
1784 memcpy(write+2,data,8);
1785 memcpy(write+10,mac,4);
1786 while(!send_success){
1787 ReaderTransmitIClass(write, sizeof(write));
1788 if(ReaderReceiveIClass(resp) == 10) {