1 //-----------------------------------------------------------------------------
2 // Gerhard de Koning Gans - May 2008
3 // Hagen Fritsch - June 2010
4 // Gerhard de Koning Gans - May 2011
5 // Gerhard de Koning Gans - June 2012 - Added iClass card and reader emulation
7 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
8 // at your option, any later version. See the LICENSE.txt file for the text of
10 //-----------------------------------------------------------------------------
11 // Routines to support iClass.
12 //-----------------------------------------------------------------------------
13 // Based on ISO14443a implementation. Still in experimental phase.
14 // Contribution made during a security research at Radboud University Nijmegen
16 // Please feel free to contribute and extend iClass support!!
17 //-----------------------------------------------------------------------------
21 // We still have sometimes a demodulation error when snooping iClass communication.
22 // The resulting trace of a read-block-03 command may look something like this:
24 // + 22279: : 0c 03 e8 01
26 // ...with an incorrect answer...
28 // + 85: 0: TAG ff! ff! ff! ff! ff! ff! ff! ff! bb 33 bb 00 01! 0e! 04! bb !crc
30 // We still left the error signalling bytes in the traces like 0xbb
32 // A correct trace should look like this:
34 // + 21112: : 0c 03 e8 01
35 // + 85: 0: TAG ff ff ff ff ff ff ff ff ea f5
37 //-----------------------------------------------------------------------------
39 #include "proxmark3.h"
45 // Needed for CRC in emulation mode;
46 // same construction as in ISO 14443;
47 // different initial value (CRC_ICLASS)
48 #include "iso14443crc.h"
49 #include "iso15693tools.h"
51 static int timeout
= 4096;
54 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
);
56 //-----------------------------------------------------------------------------
57 // The software UART that receives commands from the reader, and its state
59 //-----------------------------------------------------------------------------
63 STATE_START_OF_COMMUNICATION
,
83 static RAMFUNC
int OutOfNDecoding(int bit
)
89 Uart
.bitBuffer
= bit
^ 0xFF0;
94 Uart
.bitBuffer
^= bit
;
98 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
101 if(Uart.byteCnt > 15) { return TRUE; }
107 if(Uart
.state
!= STATE_UNSYNCD
) {
110 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
116 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
122 if(bit
!= bitright
) { bit
= bitright
; }
125 // So, now we only have to deal with *bit*, lets see...
126 if(Uart
.posCnt
== 1) {
127 // measurement first half bitperiod
129 // Drop in first half means that we are either seeing
132 if(Uart
.nOutOfCnt
== 1) {
133 // End of Communication
134 Uart
.state
= STATE_UNSYNCD
;
136 if(Uart
.byteCnt
== 0) {
137 // Its not straightforward to show single EOFs
138 // So just leave it and do not return TRUE
139 Uart
.output
[0] = 0xf0;
146 else if(Uart
.state
!= STATE_START_OF_COMMUNICATION
) {
147 // When not part of SOF or EOF, it is an error
148 Uart
.state
= STATE_UNSYNCD
;
155 // measurement second half bitperiod
156 // Count the bitslot we are in... (ISO 15693)
160 if(Uart
.dropPosition
) {
161 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
167 // It is an error if we already have seen a drop in current frame
168 Uart
.state
= STATE_UNSYNCD
;
172 Uart
.dropPosition
= Uart
.nOutOfCnt
;
179 if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
&& Uart
.OutOfCnt
== 4) {
182 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
183 if(Uart
.dropPosition
== 4) {
184 Uart
.state
= STATE_RECEIVING
;
187 else if(Uart
.dropPosition
== 3) {
188 Uart
.state
= STATE_RECEIVING
;
190 //Uart.output[Uart.byteCnt] = 0xdd;
194 Uart
.state
= STATE_UNSYNCD
;
197 Uart
.dropPosition
= 0;
202 if(!Uart
.dropPosition
) {
203 Uart
.state
= STATE_UNSYNCD
;
212 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
213 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
215 Uart
.shiftReg
^= ((Uart
.dropPosition
& 0x03) << 6);
217 Uart
.dropPosition
= 0;
219 if(Uart
.bitCnt
== 8) {
220 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
228 else if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
) {
231 if(!Uart
.dropPosition
) {
232 Uart
.state
= STATE_UNSYNCD
;
238 Uart
.output
[Uart
.byteCnt
] = (Uart
.dropPosition
& 0xff);
243 Uart
.dropPosition
= 0;
248 Uart.output[Uart.byteCnt] = 0xAA;
250 Uart.output[Uart.byteCnt] = error & 0xFF;
252 Uart.output[Uart.byteCnt] = 0xAA;
254 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
256 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
258 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
260 Uart.output[Uart.byteCnt] = 0xAA;
268 bit
= Uart
.bitBuffer
& 0xf0;
270 bit
^= 0x0F; // drops become 1s ;-)
272 // should have been high or at least (4 * 128) / fc
273 // according to ISO this should be at least (9 * 128 + 20) / fc
274 if(Uart
.highCnt
== 8) {
275 // we went low, so this could be start of communication
276 // it turns out to be safer to choose a less significant
277 // syncbit... so we check whether the neighbour also represents the drop
278 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
279 Uart
.syncBit
= bit
& 8;
281 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
282 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
283 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
284 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
285 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
286 if(Uart
.syncBit
&& (Uart
.bitBuffer
& 8)) {
289 // the first half bit period is expected in next sample
294 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
297 Uart
.state
= STATE_START_OF_COMMUNICATION
;
301 Uart
.OutOfCnt
= 4; // Start at 1/4, could switch to 1/256
302 Uart
.dropPosition
= 0;
311 if(Uart
.highCnt
< 8) {
320 //=============================================================================
322 //=============================================================================
327 DEMOD_START_OF_COMMUNICATION
,
328 DEMOD_START_OF_COMMUNICATION2
,
329 DEMOD_START_OF_COMMUNICATION3
,
333 DEMOD_END_OF_COMMUNICATION
,
334 DEMOD_END_OF_COMMUNICATION2
,
357 static RAMFUNC
int ManchesterDecoding(int v
)
364 Demod
.buffer
= Demod
.buffer2
;
365 Demod
.buffer2
= Demod
.buffer3
;
373 if(Demod
.state
==DEMOD_UNSYNCD
) {
374 Demod
.output
[Demod
.len
] = 0xfa;
377 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
380 Demod
.syncBit
= 0x08;
387 Demod
.syncBit
= 0x04;
394 Demod
.syncBit
= 0x02;
397 if(bit
& 0x01 && Demod
.syncBit
) {
398 Demod
.syncBit
= 0x01;
403 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
404 Demod
.sub
= SUB_FIRST_HALF
;
409 //if(trigger) LED_A_OFF(); // Not useful in this case...
410 switch(Demod
.syncBit
) {
411 case 0x08: Demod
.samples
= 3; break;
412 case 0x04: Demod
.samples
= 2; break;
413 case 0x02: Demod
.samples
= 1; break;
414 case 0x01: Demod
.samples
= 0; break;
416 // SOF must be long burst... otherwise stay unsynced!!!
417 if(!(Demod
.buffer
& Demod
.syncBit
) || !(Demod
.buffer2
& Demod
.syncBit
)) {
418 Demod
.state
= DEMOD_UNSYNCD
;
422 // SOF must be long burst... otherwise stay unsynced!!!
423 if(!(Demod
.buffer2
& Demod
.syncBit
) || !(Demod
.buffer3
& Demod
.syncBit
)) {
424 Demod
.state
= DEMOD_UNSYNCD
;
434 modulation
= bit
& Demod
.syncBit
;
435 modulation
|= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
439 if(Demod
.posCount
==0) {
442 Demod
.sub
= SUB_FIRST_HALF
;
445 Demod
.sub
= SUB_NONE
;
450 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
451 if(Demod.state!=DEMOD_ERROR_WAIT) {
452 Demod.state = DEMOD_ERROR_WAIT;
453 Demod.output[Demod.len] = 0xaa;
457 //else if(modulation) {
459 if(Demod
.sub
== SUB_FIRST_HALF
) {
460 Demod
.sub
= SUB_BOTH
;
463 Demod
.sub
= SUB_SECOND_HALF
;
466 else if(Demod
.sub
== SUB_NONE
) {
467 if(Demod
.state
== DEMOD_SOF_COMPLETE
) {
468 Demod
.output
[Demod
.len
] = 0x0f;
470 Demod
.state
= DEMOD_UNSYNCD
;
475 Demod
.state
= DEMOD_ERROR_WAIT
;
478 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
479 Demod.state = DEMOD_ERROR_WAIT;
480 Demod.output[Demod.len] = 0xaa;
485 switch(Demod
.state
) {
486 case DEMOD_START_OF_COMMUNICATION
:
487 if(Demod
.sub
== SUB_BOTH
) {
488 //Demod.state = DEMOD_MANCHESTER_D;
489 Demod
.state
= DEMOD_START_OF_COMMUNICATION2
;
491 Demod
.sub
= SUB_NONE
;
494 Demod
.output
[Demod
.len
] = 0xab;
495 Demod
.state
= DEMOD_ERROR_WAIT
;
499 case DEMOD_START_OF_COMMUNICATION2
:
500 if(Demod
.sub
== SUB_SECOND_HALF
) {
501 Demod
.state
= DEMOD_START_OF_COMMUNICATION3
;
504 Demod
.output
[Demod
.len
] = 0xab;
505 Demod
.state
= DEMOD_ERROR_WAIT
;
509 case DEMOD_START_OF_COMMUNICATION3
:
510 if(Demod
.sub
== SUB_SECOND_HALF
) {
511 // Demod.state = DEMOD_MANCHESTER_D;
512 Demod
.state
= DEMOD_SOF_COMPLETE
;
513 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
517 Demod
.output
[Demod
.len
] = 0xab;
518 Demod
.state
= DEMOD_ERROR_WAIT
;
522 case DEMOD_SOF_COMPLETE
:
523 case DEMOD_MANCHESTER_D
:
524 case DEMOD_MANCHESTER_E
:
525 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
526 // 00001111 = 1 (0 in 14443)
527 if(Demod
.sub
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF
529 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
530 Demod
.state
= DEMOD_MANCHESTER_D
;
532 else if(Demod
.sub
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF
534 Demod
.shiftReg
>>= 1;
535 Demod
.state
= DEMOD_MANCHESTER_E
;
537 else if(Demod
.sub
== SUB_BOTH
) {
538 Demod
.state
= DEMOD_MANCHESTER_F
;
541 Demod
.state
= DEMOD_ERROR_WAIT
;
546 case DEMOD_MANCHESTER_F
:
547 // Tag response does not need to be a complete byte!
548 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
549 if(Demod
.bitCount
> 1) { // was > 0, do not interpret last closing bit, is part of EOF
550 Demod
.shiftReg
>>= (9 - Demod
.bitCount
); // right align data
551 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
555 Demod
.state
= DEMOD_UNSYNCD
;
559 Demod
.output
[Demod
.len
] = 0xad;
560 Demod
.state
= DEMOD_ERROR_WAIT
;
565 case DEMOD_ERROR_WAIT
:
566 Demod
.state
= DEMOD_UNSYNCD
;
570 Demod
.output
[Demod
.len
] = 0xdd;
571 Demod
.state
= DEMOD_UNSYNCD
;
575 /*if(Demod.bitCount>=9) {
576 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
579 Demod.parityBits <<= 1;
580 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
585 if(Demod
.bitCount
>=8) {
586 Demod
.shiftReg
>>= 1;
587 Demod
.output
[Demod
.len
] = (Demod
.shiftReg
& 0xff);
594 Demod
.output
[Demod
.len
] = 0xBB;
596 Demod
.output
[Demod
.len
] = error
& 0xFF;
598 Demod
.output
[Demod
.len
] = 0xBB;
600 Demod
.output
[Demod
.len
] = bit
& 0xFF;
602 Demod
.output
[Demod
.len
] = Demod
.buffer
& 0xFF;
605 Demod
.output
[Demod
.len
] = Demod
.buffer2
& 0xFF;
607 Demod
.output
[Demod
.len
] = Demod
.syncBit
& 0xFF;
609 Demod
.output
[Demod
.len
] = 0xBB;
616 } // end (state != UNSYNCED)
621 //=============================================================================
622 // Finally, a `sniffer' for iClass communication
623 // Both sides of communication!
624 //=============================================================================
626 //-----------------------------------------------------------------------------
627 // Record the sequence of commands sent by the reader to the tag, with
628 // triggering so that we start recording at the point that the tag is moved
630 //-----------------------------------------------------------------------------
631 void RAMFUNC
SnoopIClass(void)
635 // We won't start recording the frames that we acquire until we trigger;
636 // a good trigger condition to get started is probably when we see a
637 // response from the tag.
638 //int triggered = FALSE; // FALSE to wait first for card
640 // The command (reader -> tag) that we're receiving.
641 // The length of a received command will in most cases be no more than 18 bytes.
642 // So 32 should be enough!
643 uint8_t *readerToTagCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
644 // The response (tag -> reader) that we're receiving.
645 uint8_t *tagToReaderResponse
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
647 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
649 // reset traceLen to 0
650 iso14a_set_tracing(TRUE
);
651 iso14a_clear_trace();
652 iso14a_set_trigger(FALSE
);
654 // The DMA buffer, used to stream samples from the FPGA
655 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
661 // Count of samples received so far, so that we can include timing
662 // information in the trace buffer.
666 // Set up the demodulator for tag -> reader responses.
667 Demod
.output
= tagToReaderResponse
;
669 Demod
.state
= DEMOD_UNSYNCD
;
671 // Setup for the DMA.
674 lastRxCounter
= DMA_BUFFER_SIZE
;
675 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
677 // And the reader -> tag commands
678 memset(&Uart
, 0, sizeof(Uart
));
679 Uart
.output
= readerToTagCmd
;
680 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
681 Uart
.state
= STATE_UNSYNCD
;
683 // And put the FPGA in the appropriate mode
684 // Signal field is off with the appropriate LED
686 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
687 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
689 uint32_t time_0
= GetCountSspClk();
690 uint32_t time_start
= 0;
691 uint32_t time_stop
= 0;
698 // And now we loop, receiving samples.
702 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
704 if(behindBy
> maxBehindBy
) {
705 maxBehindBy
= behindBy
;
707 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
711 if(behindBy
< 1) continue;
717 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
718 upTo
-= DMA_BUFFER_SIZE
;
719 lastRxCounter
+= DMA_BUFFER_SIZE
;
720 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
721 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
728 decbyte
^= (1 << (3 - div
));
731 // FOR READER SIDE COMMUMICATION...
734 decbyter
^= (smpl
& 0x30);
738 if((div
+ 1) % 2 == 0) {
740 if(OutOfNDecoding((smpl
& 0xF0) >> 4)) {
741 rsamples
= samples
- Uart
.samples
;
742 time_stop
= (GetCountSspClk()-time_0
) << 4;
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
, time_start
, time_stop
, 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
;
762 time_start
= (GetCountSspClk()-time_0
) << 4;
769 if(ManchesterDecoding(smpl
& 0x0F)) {
770 time_stop
= (GetCountSspClk()-time_0
) << 4;
772 rsamples
= samples
- Demod
.samples
;
776 uint8_t parity
[MAX_PARITY_SIZE
];
777 GetParity(Demod
.output
, Demod
.len
, parity
);
778 LogTrace(Demod
.output
, Demod
.len
, time_start
, time_stop
, parity
, FALSE
);
781 // And ready to receive another response.
782 memset(&Demod
, 0, sizeof(Demod
));
783 Demod
.output
= tagToReaderResponse
;
784 Demod
.state
= DEMOD_UNSYNCD
;
787 time_start
= (GetCountSspClk()-time_0
) << 4;
796 DbpString("cancelled_a");
801 DbpString("COMMAND FINISHED");
803 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
804 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
807 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
808 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
809 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
816 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) {
818 for(i
= 0; i
< 8; i
++) {
819 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8] << 5);
823 //-----------------------------------------------------------------------------
824 // Wait for commands from reader
825 // Stop when button is pressed
826 // Or return TRUE when command is captured
827 //-----------------------------------------------------------------------------
828 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
830 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
831 // only, since we are receiving, not transmitting).
832 // Signal field is off with the appropriate LED
834 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
836 // Now run a `software UART' on the stream of incoming samples.
837 Uart
.output
= received
;
838 Uart
.byteCntMax
= maxLen
;
839 Uart
.state
= STATE_UNSYNCD
;
844 if(BUTTON_PRESS()) return FALSE
;
846 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
847 AT91C_BASE_SSC
->SSC_THR
= 0x00;
849 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
850 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
852 if(OutOfNDecoding(b
& 0x0f)) {
861 //-----------------------------------------------------------------------------
862 // Prepare tag messages
863 //-----------------------------------------------------------------------------
864 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
)
866 //So far a dummy implementation, not used
867 //int lastProxToAirDuration =0;
873 ToSend
[++ToSendMax
] = 0x00;
874 ToSend
[++ToSendMax
] = 0x00;
875 ToSend
[++ToSendMax
] = 0x00;
876 ToSend
[++ToSendMax
] = 0xff;//Proxtoair duration starts here
877 ToSend
[++ToSendMax
] = 0xff;
878 ToSend
[++ToSendMax
] = 0xff;
879 ToSend
[++ToSendMax
] = 0x00;
880 ToSend
[++ToSendMax
] = 0xff;
882 for(i
= 0; i
< len
; i
++) {
887 for(j
= 0; j
< 8; j
++) {
889 ToSend
[++ToSendMax
] = 0x00;
890 ToSend
[++ToSendMax
] = 0xff;
892 ToSend
[++ToSendMax
] = 0xff;
893 ToSend
[++ToSendMax
] = 0x00;
900 ToSend
[++ToSendMax
] = 0xff;
901 ToSend
[++ToSendMax
] = 0x00;
902 ToSend
[++ToSendMax
] = 0xff;
903 ToSend
[++ToSendMax
] = 0xff;
904 ToSend
[++ToSendMax
] = 0xff;
905 ToSend
[++ToSendMax
] = 0x00;
906 ToSend
[++ToSendMax
] = 0x00;
907 ToSend
[++ToSendMax
] = 0x00;
909 //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end
911 // Convert from last byte pos to length
916 static void CodeIClassTagSOF()
918 //So far a dummy implementation, not used
919 //int lastProxToAirDuration =0;
923 ToSend
[++ToSendMax
] = 0x00;
924 ToSend
[++ToSendMax
] = 0x00;
925 ToSend
[++ToSendMax
] = 0x00;
926 ToSend
[++ToSendMax
] = 0xff;
927 ToSend
[++ToSendMax
] = 0xff;
928 ToSend
[++ToSendMax
] = 0xff;
929 ToSend
[++ToSendMax
] = 0x00;
930 ToSend
[++ToSendMax
] = 0xff;
932 // lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning
935 // Convert from last byte pos to length
939 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
);
941 * @brief SimulateIClass simulates an iClass card.
942 * @param arg0 type of simulation
943 * - 0 uses the first 8 bytes in usb data as CSN
944 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
945 * in the usb data. This mode collects MAC from the reader, in order to do an offline
946 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
947 * - Other : Uses the default CSN (031fec8af7ff12e0)
948 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
952 void SimulateIClass(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
)
954 uint32_t simType
= arg0
;
955 uint32_t numberOfCSNS
= arg1
;
956 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
958 // Enable and clear the trace
959 iso14a_set_tracing(TRUE
);
960 iso14a_clear_trace();
962 uint8_t csn_crc
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
964 // Use the CSN from commandline
965 memcpy(csn_crc
, datain
, 8);
966 doIClassSimulation(csn_crc
,0,NULL
);
967 }else if(simType
== 1)
969 doIClassSimulation(csn_crc
,0,NULL
);
971 else if(simType
== 2)
974 uint8_t mac_responses
[64] = { 0 };
975 Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS
);
976 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
977 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
978 // in order to obtain the keys, as in the "dismantling iclass"-paper.
980 for( ; i
< numberOfCSNS
&& i
*8+8 < USB_CMD_DATA_SIZE
; i
++)
982 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
984 memcpy(csn_crc
, datain
+(i
*8), 8);
985 if(doIClassSimulation(csn_crc
,1,mac_responses
+i
*8))
987 return; // Button pressed
990 cmd_send(CMD_ACK
,CMD_SIMULATE_TAG_ICLASS
,i
,0,mac_responses
,i
*8);
994 // We may want a mode here where we hardcode the csns to use (from proxclone).
995 // That will speed things up a little, but not required just yet.
996 Dbprintf("The mode is not implemented, reserved for future use");
1002 * @brief Does the actual simulation
1003 * @param csn - csn to use
1004 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1006 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
)
1009 // CSN followed by two CRC bytes
1010 uint8_t response1
[] = { 0x0F} ;
1011 uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1012 uint8_t response3
[] = { 0,0,0,0,0,0,0,0,0,0};
1013 memcpy(response3
,csn
,sizeof(response3
));
1014 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]);
1016 uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1018 // Construct anticollision-CSN
1019 rotateCSN(response3
,response2
);
1021 // Compute CRC on both CSNs
1022 ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]);
1023 ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]);
1029 // Tag anticoll. CSN
1030 // Reader 81 anticoll. CSN
1033 uint8_t *modulated_response
;
1034 int modulated_response_size
;
1035 uint8_t* trace_data
= NULL
;
1036 int trace_data_size
= 0;
1037 //uint8_t sof = 0x0f;
1039 // Respond SOF -- takes 8 bytes
1040 uint8_t *resp1
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
);
1043 // Anticollision CSN (rotated CSN)
1044 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1045 uint8_t *resp2
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 10);
1049 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1050 uint8_t *resp3
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 190);
1054 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
1055 uint8_t *resp4
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 370);
1059 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
1060 memset(receivedCmd
, 0x44, MAX_FRAME_SIZE
);
1063 // Prepare card messages
1066 // First card answer: SOF
1068 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1070 // Anticollision CSN
1071 CodeIClassTagAnswer(response2
, sizeof(response2
));
1072 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1075 CodeIClassTagAnswer(response3
, sizeof(response3
));
1076 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1079 CodeIClassTagAnswer(response4
, sizeof(response4
));
1080 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1083 // Start from off (no field generated)
1084 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1086 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
1089 // We need to listen to the high-frequency, peak-detected path.
1090 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1093 // To control where we are in the protocol
1095 uint32_t time_0
= GetCountSspClk();
1096 uint32_t t2r_time
=0;
1097 uint32_t r2t_time
=0;
1100 bool buttonPressed
= false;
1106 // Can be used to get a trigger for an oscilloscope..
1109 if(!GetIClassCommandFromReader(receivedCmd
, &len
, 100)) {
1110 buttonPressed
= true;
1113 r2t_time
= GetCountSspClk();
1117 // Okay, look at the command now.
1118 if(receivedCmd
[0] == 0x0a ) {
1119 // Reader in anticollission phase
1120 modulated_response
= resp1
; modulated_response_size
= resp1Len
; //order = 1;
1121 trace_data
= response1
;
1122 trace_data_size
= sizeof(response1
);
1123 } else if(receivedCmd
[0] == 0x0c) {
1124 // Reader asks for anticollission CSN
1125 modulated_response
= resp2
; modulated_response_size
= resp2Len
; //order = 2;
1126 trace_data
= response2
;
1127 trace_data_size
= sizeof(response2
);
1128 //DbpString("Reader requests anticollission CSN:");
1129 } else if(receivedCmd
[0] == 0x81) {
1130 // Reader selects anticollission CSN.
1131 // Tag sends the corresponding real CSN
1132 modulated_response
= resp3
; modulated_response_size
= resp3Len
; //order = 3;
1133 trace_data
= response3
;
1134 trace_data_size
= sizeof(response3
);
1135 //DbpString("Reader selects anticollission CSN:");
1136 } else if(receivedCmd
[0] == 0x88) {
1137 // Read e-purse (88 02)
1138 modulated_response
= resp4
; modulated_response_size
= resp4Len
; //order = 4;
1139 trace_data
= response4
;
1140 trace_data_size
= sizeof(response4
);
1142 } else if(receivedCmd
[0] == 0x05) {
1143 // Reader random and reader MAC!!!
1145 // We do not know what to answer, so lets keep quiet
1146 modulated_response
= resp1
; modulated_response_size
= 0; //order = 5;
1148 trace_data_size
= 0;
1149 if (breakAfterMacReceived
){
1151 Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x"
1152 ,csn
[0],csn
[1],csn
[2],csn
[3],csn
[4],csn
[5],csn
[6],csn
[7]);
1153 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len
,
1154 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1155 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1156 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1157 if (reader_mac_buf
!= NULL
)
1159 memcpy(reader_mac_buf
,receivedCmd
+1,8);
1163 } else if(receivedCmd
[0] == 0x00 && len
== 1) {
1164 // Reader ends the session
1165 modulated_response
= resp1
; modulated_response_size
= 0; //order = 0;
1167 trace_data_size
= 0;
1169 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1170 // Never seen this command before
1171 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1173 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1174 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1175 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1177 modulated_response
= resp1
; modulated_response_size
= 0; //order = 0;
1179 trace_data_size
= 0;
1182 if(cmdsRecvd
> 100) {
1183 //DbpString("100 commands later...");
1190 After changes to parity calculation
1191 Time between reader EOT and pm3 SOF
1195 A legit tag has about 380us.
1197 if(modulated_response_size
> 0) {
1198 SendIClassAnswer(modulated_response
, modulated_response_size
, timeout
);
1199 t2r_time
= GetCountSspClk();
1203 uint8_t parity
[MAX_PARITY_SIZE
];
1204 GetParity(receivedCmd
, len
, parity
);
1205 LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, (r2t_time
-time_0
) << 4, parity
, TRUE
);
1207 if (trace_data
!= NULL
) {
1208 GetParity(trace_data
, trace_data_size
, parity
);
1209 LogTrace(trace_data
, trace_data_size
, (t2r_time
-time_0
) << 4, (t2r_time
-time_0
) << 4, parity
, FALSE
);
1212 DbpString("Trace full");
1217 memset(receivedCmd
, 0x44, MAX_FRAME_SIZE
);
1220 //Dbprintf("%x", cmdsRecvd);
1225 DbpString("Button pressed");
1227 return buttonPressed
;
1230 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1232 int i
= 0, d
=0;//, u = 0, d = 0;
1235 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
1237 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1239 while(!BUTTON_PRESS()) {
1240 if((AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)){
1241 b
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
;
1243 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)){
1256 AT91C_BASE_SSC
->SSC_THR
= b
;
1259 if (i
> respLen
+4) break;
1267 //-----------------------------------------------------------------------------
1268 // Transmit the command (to the tag) that was placed in ToSend[].
1269 //-----------------------------------------------------------------------------
1270 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1273 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1274 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1279 if(*wait
< 10) *wait
= 10;
1281 for(c
= 0; c
< *wait
;) {
1282 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1283 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1286 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1287 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1297 bool firstpart
= TRUE
;
1300 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1302 // DOUBLE THE SAMPLES!
1304 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1307 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1310 if(sendbyte
== 0xff) {
1313 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1314 firstpart
= !firstpart
;
1320 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1321 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1326 if (samples
) *samples
= (c
+ *wait
) << 3;
1330 //-----------------------------------------------------------------------------
1331 // Prepare iClass reader command to send to FPGA
1332 //-----------------------------------------------------------------------------
1333 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1340 // Start of Communication: 1 out of 4
1341 ToSend
[++ToSendMax
] = 0xf0;
1342 ToSend
[++ToSendMax
] = 0x00;
1343 ToSend
[++ToSendMax
] = 0x0f;
1344 ToSend
[++ToSendMax
] = 0x00;
1346 // Modulate the bytes
1347 for (i
= 0; i
< len
; i
++) {
1349 for(j
= 0; j
< 4; j
++) {
1350 for(k
= 0; k
< 4; k
++) {
1352 ToSend
[++ToSendMax
] = 0x0f;
1355 ToSend
[++ToSendMax
] = 0x00;
1362 // End of Communication
1363 ToSend
[++ToSendMax
] = 0x00;
1364 ToSend
[++ToSendMax
] = 0x00;
1365 ToSend
[++ToSendMax
] = 0xf0;
1366 ToSend
[++ToSendMax
] = 0x00;
1368 // Convert from last character reference to length
1372 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1377 // This is tied to other size changes
1378 CodeIClassCommand(frame
,len
);
1381 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1385 // Store reader command in buffer
1387 uint8_t par
[MAX_PARITY_SIZE
];
1388 GetParity(frame
, len
, par
);
1389 LogTrace(frame
, len
, rsamples
, rsamples
, par
, TRUE
);
1393 //-----------------------------------------------------------------------------
1394 // Wait a certain time for tag response
1395 // If a response is captured return TRUE
1396 // If it takes too long return FALSE
1397 //-----------------------------------------------------------------------------
1398 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1400 // buffer needs to be 512 bytes
1403 // Set FPGA mode to "reader listen mode", no modulation (listen
1404 // only, since we are receiving, not transmitting).
1405 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1407 // Now get the answer from the card
1408 Demod
.output
= receivedResponse
;
1410 Demod
.state
= DEMOD_UNSYNCD
;
1413 if (elapsed
) *elapsed
= 0;
1421 if(BUTTON_PRESS()) return FALSE
;
1423 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1424 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1425 if (elapsed
) (*elapsed
)++;
1427 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1428 if(c
< timeout
) { c
++; } else { return FALSE
; }
1429 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1433 if(ManchesterDecoding(b
& 0x0f)) {
1441 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1444 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1445 rsamples
+= samples
;
1447 uint8_t parity
[MAX_PARITY_SIZE
];
1448 GetParity(receivedAnswer
, Demod
.len
, parity
);
1449 LogTrace(receivedAnswer
,Demod
.len
,rsamples
,rsamples
,parity
,FALSE
);
1451 if(samples
== 0) return FALSE
;
1455 void setupIclassReader()
1457 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1458 // Reset trace buffer
1459 iso14a_set_tracing(TRUE
);
1460 iso14a_clear_trace();
1464 // Start from off (no field generated)
1465 // Signal field is off with the appropriate LED
1467 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1470 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1472 // Now give it time to spin up.
1473 // Signal field is on with the appropriate LED
1474 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1480 size_t sendCmdGetResponseWithRetries(uint8_t* command
, size_t cmdsize
, uint8_t* resp
, uint8_t expected_size
, uint8_t retries
)
1482 while(retries
-- > 0)
1484 ReaderTransmitIClass(command
, cmdsize
);
1485 if(expected_size
== ReaderReceiveIClass(resp
)){
1493 * @brief Talks to an iclass tag, sends the commands to get CSN and CC.
1494 * @param card_data where the CSN and CC are stored for return
1497 * 2 = Got CSN and CC
1499 uint8_t handshakeIclassTag(uint8_t *card_data
)
1501 static uint8_t act_all
[] = { 0x0a };
1502 static uint8_t identify
[] = { 0x0c };
1503 static uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1504 static uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1505 uint8_t *resp
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
1507 uint8_t read_status
= 0;
1510 ReaderTransmitIClass(act_all
, 1);
1512 if(!ReaderReceiveIClass(resp
)) return read_status
;//Fail
1514 ReaderTransmitIClass(identify
, 1);
1515 //We expect a 10-byte response here, 8 byte anticollision-CSN and 2 byte CRC
1516 uint8_t len
= ReaderReceiveIClass(resp
);
1517 if(len
!= 10) return read_status
;//Fail
1519 //Copy the Anti-collision CSN to our select-packet
1520 memcpy(&select
[1],resp
,8);
1522 ReaderTransmitIClass(select
, sizeof(select
));
1523 //We expect a 10-byte response here, 8 byte CSN and 2 byte CRC
1524 len
= ReaderReceiveIClass(resp
);
1525 if(len
!= 10) return read_status
;//Fail
1527 //Success - level 1, we got CSN
1528 //Save CSN in response data
1529 memcpy(card_data
,resp
,8);
1531 //Flag that we got to at least stage 1, read CSN
1534 // Card selected, now read e-purse (cc)
1535 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1536 if(ReaderReceiveIClass(resp
) == 8) {
1537 //Save CC (e-purse) in response data
1538 memcpy(card_data
+8,resp
,8);
1547 // Reader iClass Anticollission
1548 void ReaderIClass(uint8_t arg0
) {
1550 uint8_t card_data
[24]={0};
1551 uint8_t last_csn
[8]={0};
1554 bool abort_after_read
= arg0
& FLAG_ICLASS_READER_ONLY_ONCE
;
1555 bool get_cc
= arg0
& FLAG_ICLASS_READER_GET_CC
;
1557 setupIclassReader();
1559 size_t datasize
= 0;
1560 while(!BUTTON_PRESS())
1563 if(traceLen
> TRACE_SIZE
) {
1564 DbpString("Trace full");
1569 read_status
= handshakeIclassTag(card_data
);
1571 if(read_status
== 0) continue;
1572 if(read_status
== 1) datasize
= 8;
1573 if(read_status
== 2) datasize
= 16;
1576 //Send back to client, but don't bother if we already sent this
1577 if(memcmp(last_csn
, card_data
, 8) != 0)
1580 if(!get_cc
|| (get_cc
&& read_status
== 2))
1582 cmd_send(CMD_ACK
,read_status
,0,0,card_data
,datasize
);
1583 if(abort_after_read
) {
1587 //Save that we already sent this....
1588 memcpy(last_csn
, card_data
, 8);
1590 //If 'get_cc' was specified and we didn't get a CC, we'll just keep trying...
1594 cmd_send(CMD_ACK
,0,0,0,card_data
, 0);
1598 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) {
1600 uint8_t card_data
[USB_CMD_DATA_SIZE
]={0};
1601 uint16_t block_crc_LUT
[255] = {0};
1603 {//Generate a lookup table for block crc
1604 for(int block
= 0; block
< 255; block
++){
1606 block_crc_LUT
[block
] = iclass_crc16(&bl
,1);
1609 //Dbprintf("Lookup table: %02x %02x %02x" ,block_crc_LUT[0],block_crc_LUT[1],block_crc_LUT[2]);
1611 uint8_t check
[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1612 uint8_t read
[] = { 0x0c, 0x00, 0x00, 0x00 };
1618 static struct memory_t
{
1626 uint8_t* resp
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
1628 setupIclassReader();
1631 while(!BUTTON_PRESS()) {
1635 if(traceLen
> TRACE_SIZE
) {
1636 DbpString("Trace full");
1640 uint8_t read_status
= handshakeIclassTag(card_data
);
1641 if(read_status
< 2) continue;
1643 //for now replay captured auth (as cc not updated)
1644 memcpy(check
+5,MAC
,4);
1646 if(sendCmdGetResponseWithRetries(check
, sizeof(check
),resp
, 4, 5))
1648 Dbprintf("Error: Authentication Fail!");
1652 //first get configuration block (block 1)
1653 crc
= block_crc_LUT
[1];
1656 read
[3] = crc
& 0xff;
1658 if(sendCmdGetResponseWithRetries(read
, sizeof(read
),resp
, 10, 10))
1660 Dbprintf("Dump config (block 1) failed");
1665 memory
.k16
= (mem
& 0x80);
1666 memory
.book
= (mem
& 0x20);
1667 memory
.k2
= (mem
& 0x8);
1668 memory
.lockauth
= (mem
& 0x2);
1669 memory
.keyaccess
= (mem
& 0x1);
1671 cardsize
= memory
.k16
? 255 : 32;
1673 //Set card_data to all zeroes, we'll fill it with data
1674 memset(card_data
,0x0,USB_CMD_DATA_SIZE
);
1675 uint8_t failedRead
=0;
1676 uint8_t stored_data_length
=0;
1677 //then loop around remaining blocks
1678 for(int block
=0; block
< cardsize
; block
++){
1681 crc
= block_crc_LUT
[block
];
1683 read
[3] = crc
& 0xff;
1685 if(!sendCmdGetResponseWithRetries(read
, sizeof(read
), resp
, 10, 10))
1687 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1688 block
, resp
[0], resp
[1], resp
[2],
1689 resp
[3], resp
[4], resp
[5],
1692 //Fill up the buffer
1693 memcpy(card_data
+stored_data_length
,resp
,8);
1694 stored_data_length
+= 8;
1696 if(stored_data_length
+8 > USB_CMD_DATA_SIZE
)
1697 {//Time to send this off and start afresh
1699 stored_data_length
,//data length
1700 failedRead
,//Failed blocks?
1702 card_data
, stored_data_length
);
1704 stored_data_length
= 0;
1710 stored_data_length
+=8;//Otherwise, data becomes misaligned
1711 Dbprintf("Failed to dump block %d", block
);
1714 //Send off any remaining data
1715 if(stored_data_length
> 0)
1718 stored_data_length
,//data length
1719 failedRead
,//Failed blocks?
1721 card_data
, stored_data_length
);
1723 //If we got here, let's break
1726 //Signal end of transmission
1736 //2. Create Read method (cut-down from above) based off responses from 1.
1737 // Since we have the MAC could continue to use replay function.
1738 //3. Create Write method
1740 void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) {
1741 uint8_t act_all[] = { 0x0a };
1742 uint8_t identify[] = { 0x0c };
1743 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1744 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1745 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1746 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1747 uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1751 uint8_t* resp = (((uint8_t *)BigBuf) + 3560);
1753 // Reset trace buffer
1754 memset(trace, 0x44, RECV_CMD_OFFSET);
1759 // Start from off (no field generated)
1760 // Signal field is off with the appropriate LED
1762 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1765 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1767 // Now give it time to spin up.
1768 // Signal field is on with the appropriate LED
1769 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1774 for(int i=0;i<1;i++) {
1776 if(traceLen > TRACE_SIZE) {
1777 DbpString("Trace full");
1781 if (BUTTON_PRESS()) break;
1784 ReaderTransmitIClass(act_all, 1);
1786 if(ReaderReceiveIClass(resp)) {
1787 ReaderTransmitIClass(identify, 1);
1788 if(ReaderReceiveIClass(resp) == 10) {
1790 memcpy(&select[1],resp,8);
1791 ReaderTransmitIClass(select, sizeof(select));
1793 if(ReaderReceiveIClass(resp) == 10) {
1794 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1795 resp[0], resp[1], resp[2],
1796 resp[3], resp[4], resp[5],
1800 Dbprintf("Readcheck on Sector 2");
1801 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1802 if(ReaderReceiveIClass(resp) == 8) {
1803 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1804 resp[0], resp[1], resp[2],
1805 resp[3], resp[4], resp[5],
1808 Dbprintf("Authenticate");
1809 //for now replay captured auth (as cc not updated)
1810 memcpy(check+5,MAC,4);
1811 Dbprintf(" AA: %02x %02x %02x %02x",
1812 check[5], check[6], check[7],check[8]);
1813 ReaderTransmitIClass(check, sizeof(check));
1814 if(ReaderReceiveIClass(resp) == 4) {
1815 Dbprintf(" AR: %02x %02x %02x %02x",
1816 resp[0], resp[1], resp[2],resp[3]);
1818 Dbprintf("Error: Authentication Fail!");
1821 Dbprintf("Write Block");
1823 //read configuration for max block number
1826 uint8_t *blockno=&read[1];
1827 crc = iclass_crc16((char *)blockno,1);
1829 read[3] = crc & 0xff;
1830 while(!read_success){
1831 ReaderTransmitIClass(read, sizeof(read));
1832 if(ReaderReceiveIClass(resp) == 10) {
1835 memory.k16= (mem & 0x80);
1836 memory.book= (mem & 0x20);
1837 memory.k2= (mem & 0x8);
1838 memory.lockauth= (mem & 0x2);
1839 memory.keyaccess= (mem & 0x1);
1848 memcpy(write+1,blockNo,1);
1849 memcpy(write+2,data,8);
1850 memcpy(write+10,mac,4);
1851 while(!send_success){
1852 ReaderTransmitIClass(write, sizeof(write));
1853 if(ReaderReceiveIClass(resp) == 10) {