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
); // right 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"
1149 ,csn
[0],csn
[1],csn
[2],csn
[3],csn
[4],csn
[5],csn
[6],csn
[7]);
1150 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len
,
1151 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1152 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1153 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1154 if (reader_mac_buf
!= NULL
)
1156 memcpy(reader_mac_buf
,receivedCmd
+1,8);
1160 } else if(receivedCmd
[0] == 0x00 && len
== 1) {
1161 // Reader ends the session
1162 resp
= resp1
; respLen
= 0; //order = 0;
1166 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1167 // Never seen this command before
1168 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1170 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1171 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1172 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1174 resp
= resp1
; respLen
= 0; //order = 0;
1179 if(cmdsRecvd
> 100) {
1180 //DbpString("100 commands later...");
1188 SendIClassAnswer(resp
, respLen
, 21);
1189 t2r_time
= GetCountSspClk();
1193 uint8_t parity
[MAX_PARITY_SIZE
];
1194 GetParity(receivedCmd
, len
, parity
);
1195 LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, (r2t_time
-time_0
) << 4, parity
, TRUE
);
1197 if (respdata
!= NULL
) {
1198 GetParity(respdata
, respsize
, parity
);
1199 LogTrace(respdata
, respsize
, (t2r_time
-time_0
) << 4, (t2r_time
-time_0
) << 4, parity
, FALSE
);
1202 DbpString("Trace full");
1207 memset(receivedCmd
, 0x44, MAX_FRAME_SIZE
);
1210 //Dbprintf("%x", cmdsRecvd);
1215 DbpString("Button pressed");
1217 return buttonPressed
;
1220 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1222 int i
= 0, d
=0;//, u = 0, d = 0;
1225 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
1227 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1229 while(!BUTTON_PRESS()) {
1230 if((AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)){
1231 b
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
;
1233 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)){
1246 AT91C_BASE_SSC
->SSC_THR
= b
;
1249 if (i
> respLen
+4) break;
1257 //-----------------------------------------------------------------------------
1258 // Transmit the command (to the tag) that was placed in ToSend[].
1259 //-----------------------------------------------------------------------------
1260 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1263 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1264 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1269 if(*wait
< 10) *wait
= 10;
1271 for(c
= 0; c
< *wait
;) {
1272 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1273 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1276 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1277 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1287 bool firstpart
= TRUE
;
1290 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1292 // DOUBLE THE SAMPLES!
1294 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1297 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1300 if(sendbyte
== 0xff) {
1303 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1304 firstpart
= !firstpart
;
1310 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1311 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1316 if (samples
) *samples
= (c
+ *wait
) << 3;
1320 //-----------------------------------------------------------------------------
1321 // Prepare iClass reader command to send to FPGA
1322 //-----------------------------------------------------------------------------
1323 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1330 // Start of Communication: 1 out of 4
1331 ToSend
[++ToSendMax
] = 0xf0;
1332 ToSend
[++ToSendMax
] = 0x00;
1333 ToSend
[++ToSendMax
] = 0x0f;
1334 ToSend
[++ToSendMax
] = 0x00;
1336 // Modulate the bytes
1337 for (i
= 0; i
< len
; i
++) {
1339 for(j
= 0; j
< 4; j
++) {
1340 for(k
= 0; k
< 4; k
++) {
1342 ToSend
[++ToSendMax
] = 0x0f;
1345 ToSend
[++ToSendMax
] = 0x00;
1352 // End of Communication
1353 ToSend
[++ToSendMax
] = 0x00;
1354 ToSend
[++ToSendMax
] = 0x00;
1355 ToSend
[++ToSendMax
] = 0xf0;
1356 ToSend
[++ToSendMax
] = 0x00;
1358 // Convert from last character reference to length
1362 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1367 // This is tied to other size changes
1368 CodeIClassCommand(frame
,len
);
1371 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1375 // Store reader command in buffer
1377 uint8_t par
[MAX_PARITY_SIZE
];
1378 GetParity(frame
, len
, par
);
1379 LogTrace(frame
, len
, rsamples
, rsamples
, par
, TRUE
);
1383 //-----------------------------------------------------------------------------
1384 // Wait a certain time for tag response
1385 // If a response is captured return TRUE
1386 // If it takes too long return FALSE
1387 //-----------------------------------------------------------------------------
1388 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1390 // buffer needs to be 512 bytes
1393 // Set FPGA mode to "reader listen mode", no modulation (listen
1394 // only, since we are receiving, not transmitting).
1395 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1397 // Now get the answer from the card
1398 Demod
.output
= receivedResponse
;
1400 Demod
.state
= DEMOD_UNSYNCD
;
1403 if (elapsed
) *elapsed
= 0;
1411 if(BUTTON_PRESS()) return FALSE
;
1413 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1414 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1415 if (elapsed
) (*elapsed
)++;
1417 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1418 if(c
< timeout
) { c
++; } else { return FALSE
; }
1419 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1423 if(ManchesterDecoding(b
& 0x0f)) {
1431 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1434 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1435 rsamples
+= samples
;
1437 uint8_t parity
[MAX_PARITY_SIZE
];
1438 GetParity(receivedAnswer
, Demod
.len
, parity
);
1439 LogTrace(receivedAnswer
,Demod
.len
,rsamples
,rsamples
,parity
,FALSE
);
1441 if(samples
== 0) return FALSE
;
1445 void setupIclassReader()
1447 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1448 // Reset trace buffer
1449 iso14a_set_tracing(TRUE
);
1450 iso14a_clear_trace();
1454 // Start from off (no field generated)
1455 // Signal field is off with the appropriate LED
1457 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1460 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1462 // Now give it time to spin up.
1463 // Signal field is on with the appropriate LED
1464 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1470 size_t sendCmdGetResponseWithRetries(uint8_t* command
, size_t cmdsize
, uint8_t* resp
, uint8_t expected_size
, uint8_t retries
)
1472 while(retries
-- > 0)
1474 ReaderTransmitIClass(command
, cmdsize
);
1475 if(expected_size
== ReaderReceiveIClass(resp
)){
1483 * @brief Talks to an iclass tag, sends the commands to get CSN and CC.
1484 * @param card_data where the CSN and CC are stored for return
1487 * 2 = Got CSN and CC
1489 uint8_t handshakeIclassTag(uint8_t *card_data
)
1491 static uint8_t act_all
[] = { 0x0a };
1492 static uint8_t identify
[] = { 0x0c };
1493 static uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1494 static uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1495 uint8_t *resp
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
1497 uint8_t read_status
= 0;
1500 ReaderTransmitIClass(act_all
, 1);
1502 if(!ReaderReceiveIClass(resp
)) return read_status
;//Fail
1504 ReaderTransmitIClass(identify
, 1);
1505 //We expect a 10-byte response here, 8 byte anticollision-CSN and 2 byte CRC
1506 uint8_t len
= ReaderReceiveIClass(resp
);
1507 if(len
!= 10) return read_status
;//Fail
1509 //Copy the Anti-collision CSN to our select-packet
1510 memcpy(&select
[1],resp
,8);
1512 ReaderTransmitIClass(select
, sizeof(select
));
1513 //We expect a 10-byte response here, 8 byte CSN and 2 byte CRC
1514 len
= ReaderReceiveIClass(resp
);
1515 if(len
!= 10) return read_status
;//Fail
1517 //Success - level 1, we got CSN
1518 //Save CSN in response data
1519 memcpy(card_data
,resp
,8);
1521 //Flag that we got to at least stage 1, read CSN
1524 // Card selected, now read e-purse (cc)
1525 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1526 if(ReaderReceiveIClass(resp
) == 8) {
1527 //Save CC (e-purse) in response data
1528 memcpy(card_data
+8,resp
,8);
1537 // Reader iClass Anticollission
1538 void ReaderIClass(uint8_t arg0
) {
1540 uint8_t card_data
[24]={0};
1541 uint8_t last_csn
[8]={0};
1544 bool abort_after_read
= arg0
& FLAG_ICLASS_READER_ONLY_ONCE
;
1545 bool get_cc
= arg0
& FLAG_ICLASS_READER_GET_CC
;
1547 setupIclassReader();
1549 size_t datasize
= 0;
1550 while(!BUTTON_PRESS())
1553 if(traceLen
> TRACE_SIZE
) {
1554 DbpString("Trace full");
1559 read_status
= handshakeIclassTag(card_data
);
1561 if(read_status
== 0) continue;
1562 if(read_status
== 1) datasize
= 8;
1563 if(read_status
== 2) datasize
= 16;
1566 //Send back to client, but don't bother if we already sent this
1567 if(memcmp(last_csn
, card_data
, 8) != 0)
1570 if(!get_cc
|| (get_cc
&& read_status
== 2))
1572 cmd_send(CMD_ACK
,read_status
,0,0,card_data
,datasize
);
1573 if(abort_after_read
) {
1577 //Save that we already sent this....
1578 memcpy(last_csn
, card_data
, 8);
1580 //If 'get_cc' was specified and we didn't get a CC, we'll just keep trying...
1584 cmd_send(CMD_ACK
,0,0,0,card_data
, 0);
1588 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) {
1590 uint8_t card_data
[24]={0};
1591 uint16_t block_crc_LUT
[255] = {0};
1593 {//Generate a lookup table for block crc
1594 for(int block
= 0; block
< 255; block
++){
1596 block_crc_LUT
[block
] = iclass_crc16(&bl
,1);
1599 //Dbprintf("Lookup table: %02x %02x %02x" ,block_crc_LUT[0],block_crc_LUT[1],block_crc_LUT[2]);
1601 uint8_t check
[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1602 uint8_t read
[] = { 0x0c, 0x00, 0x00, 0x00 };
1608 static struct memory_t
{
1616 uint8_t* resp
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
1618 setupIclassReader();
1621 while(!BUTTON_PRESS()) {
1625 if(traceLen
> TRACE_SIZE
) {
1626 DbpString("Trace full");
1630 uint8_t read_status
= handshakeIclassTag(card_data
);
1631 if(read_status
< 2) continue;
1633 //for now replay captured auth (as cc not updated)
1634 memcpy(check
+5,MAC
,4);
1636 if(sendCmdGetResponseWithRetries(check
, sizeof(check
),resp
, 4, 5))
1638 Dbprintf("Error: Authentication Fail!");
1642 //first get configuration block (block 1)
1643 crc
= block_crc_LUT
[1];
1646 read
[3] = crc
& 0xff;
1648 if(sendCmdGetResponseWithRetries(read
, sizeof(read
),resp
, 10, 10))
1650 Dbprintf("Dump config (block 1) failed");
1655 memory
.k16
= (mem
& 0x80);
1656 memory
.book
= (mem
& 0x20);
1657 memory
.k2
= (mem
& 0x8);
1658 memory
.lockauth
= (mem
& 0x2);
1659 memory
.keyaccess
= (mem
& 0x1);
1661 cardsize
= memory
.k16
? 255 : 32;
1664 //then loop around remaining blocks
1665 for(int block
=0; block
< cardsize
; block
++){
1668 crc
= block_crc_LUT
[block
];
1670 read
[3] = crc
& 0xff;
1672 if(!sendCmdGetResponseWithRetries(read
, sizeof(read
), resp
, 10, 10))
1674 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1675 block
, resp
[0], resp
[1], resp
[2],
1676 resp
[3], resp
[4], resp
[5],
1680 Dbprintf("Failed to dump block %d", block
);
1684 //If we got here, let's break
1690 //2. Create Read method (cut-down from above) based off responses from 1.
1691 // Since we have the MAC could continue to use replay function.
1692 //3. Create Write method
1694 void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) {
1695 uint8_t act_all[] = { 0x0a };
1696 uint8_t identify[] = { 0x0c };
1697 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1698 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1699 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1700 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1701 uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1705 uint8_t* resp = (((uint8_t *)BigBuf) + RECV_RESP_OFFSET);
1707 // Reset trace buffer
1708 memset(trace, 0x44, RECV_CMD_OFFSET);
1713 // Start from off (no field generated)
1714 // Signal field is off with the appropriate LED
1716 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1719 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1721 // Now give it time to spin up.
1722 // Signal field is on with the appropriate LED
1723 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1728 for(int i=0;i<1;i++) {
1730 if(traceLen > TRACE_SIZE) {
1731 DbpString("Trace full");
1735 if (BUTTON_PRESS()) break;
1738 ReaderTransmitIClass(act_all, 1);
1740 if(ReaderReceiveIClass(resp)) {
1741 ReaderTransmitIClass(identify, 1);
1742 if(ReaderReceiveIClass(resp) == 10) {
1744 memcpy(&select[1],resp,8);
1745 ReaderTransmitIClass(select, sizeof(select));
1747 if(ReaderReceiveIClass(resp) == 10) {
1748 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1749 resp[0], resp[1], resp[2],
1750 resp[3], resp[4], resp[5],
1754 Dbprintf("Readcheck on Sector 2");
1755 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1756 if(ReaderReceiveIClass(resp) == 8) {
1757 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1758 resp[0], resp[1], resp[2],
1759 resp[3], resp[4], resp[5],
1762 Dbprintf("Authenticate");
1763 //for now replay captured auth (as cc not updated)
1764 memcpy(check+5,MAC,4);
1765 Dbprintf(" AA: %02x %02x %02x %02x",
1766 check[5], check[6], check[7],check[8]);
1767 ReaderTransmitIClass(check, sizeof(check));
1768 if(ReaderReceiveIClass(resp) == 4) {
1769 Dbprintf(" AR: %02x %02x %02x %02x",
1770 resp[0], resp[1], resp[2],resp[3]);
1772 Dbprintf("Error: Authentication Fail!");
1775 Dbprintf("Write Block");
1777 //read configuration for max block number
1780 uint8_t *blockno=&read[1];
1781 crc = iclass_crc16((char *)blockno,1);
1783 read[3] = crc & 0xff;
1784 while(!read_success){
1785 ReaderTransmitIClass(read, sizeof(read));
1786 if(ReaderReceiveIClass(resp) == 10) {
1789 memory.k16= (mem & 0x80);
1790 memory.book= (mem & 0x20);
1791 memory.k2= (mem & 0x8);
1792 memory.lockauth= (mem & 0x2);
1793 memory.keyaccess= (mem & 0x1);
1802 memcpy(write+1,blockNo,1);
1803 memcpy(write+2,data,8);
1804 memcpy(write+10,mac,4);
1805 while(!send_success){
1806 ReaderTransmitIClass(write, sizeof(write));
1807 if(ReaderReceiveIClass(resp) == 10) {