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
,
84 static RAMFUNC
int OutOfNDecoding(int bit
)
90 Uart
.bitBuffer
= bit
^ 0xFF0;
95 Uart
.bitBuffer
^= bit
;
99 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
102 if(Uart.byteCnt > 15) { return TRUE; }
108 if(Uart
.state
!= STATE_UNSYNCD
) {
111 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
117 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
123 if(bit
!= bitright
) { bit
= bitright
; }
126 // So, now we only have to deal with *bit*, lets see...
127 if(Uart
.posCnt
== 1) {
128 // measurement first half bitperiod
130 // Drop in first half means that we are either seeing
133 if(Uart
.nOutOfCnt
== 1) {
134 // End of Communication
135 Uart
.state
= STATE_UNSYNCD
;
137 if(Uart
.byteCnt
== 0) {
138 // Its not straightforward to show single EOFs
139 // So just leave it and do not return TRUE
140 Uart
.output
[Uart
.byteCnt
] = 0xf0;
143 // Calculate the parity bit for the client...
150 else if(Uart
.state
!= STATE_START_OF_COMMUNICATION
) {
151 // When not part of SOF or EOF, it is an error
152 Uart
.state
= STATE_UNSYNCD
;
159 // measurement second half bitperiod
160 // Count the bitslot we are in... (ISO 15693)
164 if(Uart
.dropPosition
) {
165 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
171 // It is an error if we already have seen a drop in current frame
172 Uart
.state
= STATE_UNSYNCD
;
176 Uart
.dropPosition
= Uart
.nOutOfCnt
;
183 if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
&& Uart
.OutOfCnt
== 4) {
186 if(Uart
.state
== STATE_START_OF_COMMUNICATION
) {
187 if(Uart
.dropPosition
== 4) {
188 Uart
.state
= STATE_RECEIVING
;
191 else if(Uart
.dropPosition
== 3) {
192 Uart
.state
= STATE_RECEIVING
;
194 //Uart.output[Uart.byteCnt] = 0xdd;
198 Uart
.state
= STATE_UNSYNCD
;
201 Uart
.dropPosition
= 0;
206 if(!Uart
.dropPosition
) {
207 Uart
.state
= STATE_UNSYNCD
;
216 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; }
217 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; }
219 Uart
.shiftReg
^= ((Uart
.dropPosition
& 0x03) << 6);
221 Uart
.dropPosition
= 0;
223 if(Uart
.bitCnt
== 8) {
224 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
227 // Calculate the parity bit for the client...
228 Uart
.parityBits
<<= 1;
229 Uart
.parityBits
^= OddByteParity
[(Uart
.shiftReg
& 0xff)];
237 else if(Uart
.nOutOfCnt
== Uart
.OutOfCnt
) {
240 if(!Uart
.dropPosition
) {
241 Uart
.state
= STATE_UNSYNCD
;
247 Uart
.output
[Uart
.byteCnt
] = (Uart
.dropPosition
& 0xff);
250 // Calculate the parity bit for the client...
251 Uart
.parityBits
<<= 1;
252 Uart
.parityBits
^= OddByteParity
[(Uart
.dropPosition
& 0xff)];
257 Uart
.dropPosition
= 0;
262 Uart.output[Uart.byteCnt] = 0xAA;
264 Uart.output[Uart.byteCnt] = error & 0xFF;
266 Uart.output[Uart.byteCnt] = 0xAA;
268 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
270 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
272 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
274 Uart.output[Uart.byteCnt] = 0xAA;
282 bit
= Uart
.bitBuffer
& 0xf0;
284 bit
^= 0x0F; // drops become 1s ;-)
286 // should have been high or at least (4 * 128) / fc
287 // according to ISO this should be at least (9 * 128 + 20) / fc
288 if(Uart
.highCnt
== 8) {
289 // we went low, so this could be start of communication
290 // it turns out to be safer to choose a less significant
291 // syncbit... so we check whether the neighbour also represents the drop
292 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
293 Uart
.syncBit
= bit
& 8;
295 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
296 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
297 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
298 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
299 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
300 if(Uart
.syncBit
&& (Uart
.bitBuffer
& 8)) {
303 // the first half bit period is expected in next sample
308 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
311 Uart
.state
= STATE_START_OF_COMMUNICATION
;
316 Uart
.OutOfCnt
= 4; // Start at 1/4, could switch to 1/256
317 Uart
.dropPosition
= 0;
326 if(Uart
.highCnt
< 8) {
335 //=============================================================================
337 //=============================================================================
342 DEMOD_START_OF_COMMUNICATION
,
343 DEMOD_START_OF_COMMUNICATION2
,
344 DEMOD_START_OF_COMMUNICATION3
,
348 DEMOD_END_OF_COMMUNICATION
,
349 DEMOD_END_OF_COMMUNICATION2
,
373 static RAMFUNC
int ManchesterDecoding(int v
)
380 Demod
.buffer
= Demod
.buffer2
;
381 Demod
.buffer2
= Demod
.buffer3
;
389 if(Demod
.state
==DEMOD_UNSYNCD
) {
390 Demod
.output
[Demod
.len
] = 0xfa;
393 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
396 Demod
.syncBit
= 0x08;
403 Demod
.syncBit
= 0x04;
410 Demod
.syncBit
= 0x02;
413 if(bit
& 0x01 && Demod
.syncBit
) {
414 Demod
.syncBit
= 0x01;
419 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
420 Demod
.sub
= SUB_FIRST_HALF
;
423 Demod
.parityBits
= 0;
426 //if(trigger) LED_A_OFF(); // Not useful in this case...
427 switch(Demod
.syncBit
) {
428 case 0x08: Demod
.samples
= 3; break;
429 case 0x04: Demod
.samples
= 2; break;
430 case 0x02: Demod
.samples
= 1; break;
431 case 0x01: Demod
.samples
= 0; break;
433 // SOF must be long burst... otherwise stay unsynced!!!
434 if(!(Demod
.buffer
& Demod
.syncBit
) || !(Demod
.buffer2
& Demod
.syncBit
)) {
435 Demod
.state
= DEMOD_UNSYNCD
;
439 // SOF must be long burst... otherwise stay unsynced!!!
440 if(!(Demod
.buffer2
& Demod
.syncBit
) || !(Demod
.buffer3
& Demod
.syncBit
)) {
441 Demod
.state
= DEMOD_UNSYNCD
;
451 modulation
= bit
& Demod
.syncBit
;
452 modulation
|= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
453 //modulation = ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
457 if(Demod
.posCount
==0) {
460 Demod
.sub
= SUB_FIRST_HALF
;
463 Demod
.sub
= SUB_NONE
;
468 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
469 if(Demod.state!=DEMOD_ERROR_WAIT) {
470 Demod.state = DEMOD_ERROR_WAIT;
471 Demod.output[Demod.len] = 0xaa;
475 //else if(modulation) {
477 if(Demod
.sub
== SUB_FIRST_HALF
) {
478 Demod
.sub
= SUB_BOTH
;
481 Demod
.sub
= SUB_SECOND_HALF
;
484 else if(Demod
.sub
== SUB_NONE
) {
485 if(Demod
.state
== DEMOD_SOF_COMPLETE
) {
486 Demod
.output
[Demod
.len
] = 0x0f;
488 Demod
.parityBits
<<= 1;
489 Demod
.parityBits
^= OddByteParity
[0x0f];
490 Demod
.state
= DEMOD_UNSYNCD
;
495 Demod
.state
= DEMOD_ERROR_WAIT
;
498 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
499 Demod.state = DEMOD_ERROR_WAIT;
500 Demod.output[Demod.len] = 0xaa;
505 switch(Demod
.state
) {
506 case DEMOD_START_OF_COMMUNICATION
:
507 if(Demod
.sub
== SUB_BOTH
) {
508 //Demod.state = DEMOD_MANCHESTER_D;
509 Demod
.state
= DEMOD_START_OF_COMMUNICATION2
;
511 Demod
.sub
= SUB_NONE
;
514 Demod
.output
[Demod
.len
] = 0xab;
515 Demod
.state
= DEMOD_ERROR_WAIT
;
519 case DEMOD_START_OF_COMMUNICATION2
:
520 if(Demod
.sub
== SUB_SECOND_HALF
) {
521 Demod
.state
= DEMOD_START_OF_COMMUNICATION3
;
524 Demod
.output
[Demod
.len
] = 0xab;
525 Demod
.state
= DEMOD_ERROR_WAIT
;
529 case DEMOD_START_OF_COMMUNICATION3
:
530 if(Demod
.sub
== SUB_SECOND_HALF
) {
531 // Demod.state = DEMOD_MANCHESTER_D;
532 Demod
.state
= DEMOD_SOF_COMPLETE
;
533 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
537 Demod
.output
[Demod
.len
] = 0xab;
538 Demod
.state
= DEMOD_ERROR_WAIT
;
542 case DEMOD_SOF_COMPLETE
:
543 case DEMOD_MANCHESTER_D
:
544 case DEMOD_MANCHESTER_E
:
545 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
546 // 00001111 = 1 (0 in 14443)
547 if(Demod
.sub
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF
549 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
550 Demod
.state
= DEMOD_MANCHESTER_D
;
552 else if(Demod
.sub
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF
554 Demod
.shiftReg
>>= 1;
555 Demod
.state
= DEMOD_MANCHESTER_E
;
557 else if(Demod
.sub
== SUB_BOTH
) {
558 Demod
.state
= DEMOD_MANCHESTER_F
;
561 Demod
.state
= DEMOD_ERROR_WAIT
;
566 case DEMOD_MANCHESTER_F
:
567 // Tag response does not need to be a complete byte!
568 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
569 if(Demod
.bitCount
> 1) { // was > 0, do not interpret last closing bit, is part of EOF
570 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
571 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
573 // No parity bit, so just shift a 0
574 Demod
.parityBits
<<= 1;
577 Demod
.state
= DEMOD_UNSYNCD
;
581 Demod
.output
[Demod
.len
] = 0xad;
582 Demod
.state
= DEMOD_ERROR_WAIT
;
587 case DEMOD_ERROR_WAIT
:
588 Demod
.state
= DEMOD_UNSYNCD
;
592 Demod
.output
[Demod
.len
] = 0xdd;
593 Demod
.state
= DEMOD_UNSYNCD
;
597 /*if(Demod.bitCount>=9) {
598 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
601 Demod.parityBits <<= 1;
602 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
607 if(Demod
.bitCount
>=8) {
608 Demod
.shiftReg
>>= 1;
609 Demod
.output
[Demod
.len
] = (Demod
.shiftReg
& 0xff);
612 // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT
613 Demod
.parityBits
<<= 1;
614 Demod
.parityBits
^= OddByteParity
[(Demod
.shiftReg
& 0xff)];
621 Demod
.output
[Demod
.len
] = 0xBB;
623 Demod
.output
[Demod
.len
] = error
& 0xFF;
625 Demod
.output
[Demod
.len
] = 0xBB;
627 Demod
.output
[Demod
.len
] = bit
& 0xFF;
629 Demod
.output
[Demod
.len
] = Demod
.buffer
& 0xFF;
632 Demod
.output
[Demod
.len
] = Demod
.buffer2
& 0xFF;
634 Demod
.output
[Demod
.len
] = Demod
.syncBit
& 0xFF;
636 Demod
.output
[Demod
.len
] = 0xBB;
643 } // end (state != UNSYNCED)
648 //=============================================================================
649 // Finally, a `sniffer' for iClass communication
650 // Both sides of communication!
651 //=============================================================================
653 //-----------------------------------------------------------------------------
654 // Record the sequence of commands sent by the reader to the tag, with
655 // triggering so that we start recording at the point that the tag is moved
657 //-----------------------------------------------------------------------------
658 void RAMFUNC
SnoopIClass(void)
662 // We won't start recording the frames that we acquire until we trigger;
663 // a good trigger condition to get started is probably when we see a
664 // response from the tag.
665 //int triggered = FALSE; // FALSE to wait first for card
667 // The command (reader -> tag) that we're receiving.
668 // The length of a received command will in most cases be no more than 18 bytes.
669 // So 32 should be enough!
670 uint8_t *readerToTagCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
671 // The response (tag -> reader) that we're receiving.
672 uint8_t *tagToReaderResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
674 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
676 // reset traceLen to 0
677 iso14a_set_tracing(TRUE
);
678 iso14a_clear_trace();
679 iso14a_set_trigger(FALSE
);
681 // The DMA buffer, used to stream samples from the FPGA
682 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
688 // Count of samples received so far, so that we can include timing
689 // information in the trace buffer.
693 // Set up the demodulator for tag -> reader responses.
694 Demod
.output
= tagToReaderResponse
;
696 Demod
.state
= DEMOD_UNSYNCD
;
698 // Setup for the DMA.
701 lastRxCounter
= DMA_BUFFER_SIZE
;
702 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
704 // And the reader -> tag commands
705 memset(&Uart
, 0, sizeof(Uart
));
706 Uart
.output
= readerToTagCmd
;
707 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
708 Uart
.state
= STATE_UNSYNCD
;
710 // And put the FPGA in the appropriate mode
711 // Signal field is off with the appropriate LED
713 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
714 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
716 uint32_t time_0
= GetCountSspClk();
724 // And now we loop, receiving samples.
728 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
730 if(behindBy
> maxBehindBy
) {
731 maxBehindBy
= behindBy
;
733 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
737 if(behindBy
< 1) continue;
743 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
744 upTo
-= DMA_BUFFER_SIZE
;
745 lastRxCounter
+= DMA_BUFFER_SIZE
;
746 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
747 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
754 decbyte
^= (1 << (3 - div
));
757 // FOR READER SIDE COMMUMICATION...
760 decbyter
^= (smpl
& 0x30);
764 if((div
+ 1) % 2 == 0) {
766 if(OutOfNDecoding((smpl
& 0xF0) >> 4)) {
767 rsamples
= samples
- Uart
.samples
;
770 //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
771 //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
774 LogTrace(Uart
.output
,Uart
.byteCnt
, (GetCountSspClk()-time_0
) << 4, Uart
.parityBits
,TRUE
);
775 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, TRUE
);
779 /* And ready to receive another command. */
780 Uart
.state
= STATE_UNSYNCD
;
781 /* And also reset the demod code, which might have been */
782 /* false-triggered by the commands from the reader. */
783 Demod
.state
= DEMOD_UNSYNCD
;
792 if(ManchesterDecoding(smpl
& 0x0F)) {
793 rsamples
= samples
- Demod
.samples
;
798 LogTrace(Demod
.output
,Demod
.len
, (GetCountSspClk()-time_0
) << 4 , Demod
.parityBits
,FALSE
);
799 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, FALSE
);
803 // And ready to receive another response.
804 memset(&Demod
, 0, sizeof(Demod
));
805 Demod
.output
= tagToReaderResponse
;
806 Demod
.state
= DEMOD_UNSYNCD
;
816 DbpString("cancelled_a");
821 DbpString("COMMAND FINISHED");
823 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
824 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
827 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
828 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
829 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
836 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) {
838 for(i
= 0; i
< 8; i
++) {
839 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8] << 5);
843 //-----------------------------------------------------------------------------
844 // Wait for commands from reader
845 // Stop when button is pressed
846 // Or return TRUE when command is captured
847 //-----------------------------------------------------------------------------
848 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
850 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
851 // only, since we are receiving, not transmitting).
852 // Signal field is off with the appropriate LED
854 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
856 // Now run a `software UART' on the stream of incoming samples.
857 Uart
.output
= received
;
858 Uart
.byteCntMax
= maxLen
;
859 Uart
.state
= STATE_UNSYNCD
;
864 if(BUTTON_PRESS()) return FALSE
;
866 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
867 AT91C_BASE_SSC
->SSC_THR
= 0x00;
869 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
870 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
871 /*if(OutOfNDecoding((b & 0xf0) >> 4)) {
875 if(OutOfNDecoding(b
& 0x0f)) {
884 //-----------------------------------------------------------------------------
885 // Prepare tag messages
886 //-----------------------------------------------------------------------------
887 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
)
889 //So far a dummy implementation, not used
890 //int lastProxToAirDuration =0;
896 ToSend
[++ToSendMax
] = 0x00;
897 ToSend
[++ToSendMax
] = 0x00;
898 ToSend
[++ToSendMax
] = 0x00;
899 ToSend
[++ToSendMax
] = 0xff;//Proxtoair duration starts here
900 ToSend
[++ToSendMax
] = 0xff;
901 ToSend
[++ToSendMax
] = 0xff;
902 ToSend
[++ToSendMax
] = 0x00;
903 ToSend
[++ToSendMax
] = 0xff;
905 for(i
= 0; i
< len
; i
++) {
910 for(j
= 0; j
< 8; j
++) {
912 ToSend
[++ToSendMax
] = 0x00;
913 ToSend
[++ToSendMax
] = 0xff;
915 ToSend
[++ToSendMax
] = 0xff;
916 ToSend
[++ToSendMax
] = 0x00;
923 ToSend
[++ToSendMax
] = 0xff;
924 ToSend
[++ToSendMax
] = 0x00;
925 ToSend
[++ToSendMax
] = 0xff;
926 ToSend
[++ToSendMax
] = 0xff;
927 ToSend
[++ToSendMax
] = 0xff;
928 ToSend
[++ToSendMax
] = 0x00;
929 ToSend
[++ToSendMax
] = 0x00;
930 ToSend
[++ToSendMax
] = 0x00;
932 //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end
934 // Convert from last byte pos to length
939 static void CodeIClassTagSOF()
941 //So far a dummy implementation, not used
942 //int lastProxToAirDuration =0;
946 ToSend
[++ToSendMax
] = 0x00;
947 ToSend
[++ToSendMax
] = 0x00;
948 ToSend
[++ToSendMax
] = 0x00;
949 ToSend
[++ToSendMax
] = 0xff;
950 ToSend
[++ToSendMax
] = 0xff;
951 ToSend
[++ToSendMax
] = 0xff;
952 ToSend
[++ToSendMax
] = 0x00;
953 ToSend
[++ToSendMax
] = 0xff;
955 // lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning
958 // Convert from last byte pos to length
961 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
);
963 * @brief SimulateIClass simulates an iClass card.
964 * @param arg0 type of simulation
965 * - 0 uses the first 8 bytes in usb data as CSN
966 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
967 * in the usb data. This mode collects MAC from the reader, in order to do an offline
968 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
969 * - Other : Uses the default CSN (031fec8af7ff12e0)
970 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
974 void SimulateIClass(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
)
976 uint32_t simType
= arg0
;
977 uint32_t numberOfCSNS
= arg1
;
978 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
980 // Enable and clear the trace
981 iso14a_set_tracing(TRUE
);
982 iso14a_clear_trace();
984 uint8_t csn_crc
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
986 // Use the CSN from commandline
987 memcpy(csn_crc
, datain
, 8);
988 doIClassSimulation(csn_crc
,0,NULL
);
989 }else if(simType
== 1)
991 doIClassSimulation(csn_crc
,0,NULL
);
993 else if(simType
== 2)
996 uint8_t mac_responses
[64] = { 0 };
997 Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS
);
998 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
999 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
1000 // in order to obtain the keys, as in the "dismantling iclass"-paper.
1002 for( ; i
< numberOfCSNS
&& i
*8+8 < USB_CMD_DATA_SIZE
; i
++)
1004 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
1006 memcpy(csn_crc
, datain
+(i
*8), 8);
1007 if(doIClassSimulation(csn_crc
,1,mac_responses
+i
*8))
1009 return; // Button pressed
1012 cmd_send(CMD_ACK
,CMD_SIMULATE_TAG_ICLASS
,i
,0,mac_responses
,i
*8);
1016 // We may want a mode here where we hardcode the csns to use (from proxclone).
1017 // That will speed things up a little, but not required just yet.
1018 Dbprintf("The mode is not implemented, reserved for future use");
1020 Dbprintf("Done...");
1024 * @brief Does the actual simulation
1025 * @param csn - csn to use
1026 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1028 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
)
1032 // CSN followed by two CRC bytes
1033 uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1034 uint8_t response3
[] = { 0,0,0,0,0,0,0,0,0,0};
1035 memcpy(response3
,csn
,sizeof(response3
));
1036 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]);
1038 uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1040 // Construct anticollision-CSN
1041 rotateCSN(response3
,response2
);
1043 // Compute CRC on both CSNs
1044 ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]);
1045 ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]);
1051 // Tag anticoll. CSN
1052 // Reader 81 anticoll. CSN
1057 uint8_t* respdata
= NULL
;
1061 // Respond SOF -- takes 8 bytes
1062 uint8_t *resp1
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
);
1065 // Anticollision CSN (rotated CSN)
1066 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1067 uint8_t *resp2
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 10);
1071 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1072 uint8_t *resp3
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 190);
1076 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
1077 uint8_t *resp4
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 370);
1081 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
1082 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1085 // Prepare card messages
1088 // First card answer: SOF
1090 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1092 // Anticollision CSN
1093 CodeIClassTagAnswer(response2
, sizeof(response2
));
1094 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1097 CodeIClassTagAnswer(response3
, sizeof(response3
));
1098 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1101 CodeIClassTagAnswer(response4
, sizeof(response4
));
1102 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1105 // Start from off (no field generated)
1106 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1108 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
1111 // We need to listen to the high-frequency, peak-detected path.
1112 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1115 // To control where we are in the protocol
1117 uint32_t time_0
= GetCountSspClk();
1118 uint32_t t2r_time
=0;
1119 uint32_t r2t_time
=0;
1122 bool buttonPressed
= false;
1124 /** Hack for testing
1125 memcpy(reader_mac_buf,csn,8);
1133 // Can be used to get a trigger for an oscilloscope..
1135 if(!GetIClassCommandFromReader(receivedCmd
, &len
, 100)) {
1136 buttonPressed
= true;
1139 r2t_time
= GetCountSspClk();
1143 // Okay, look at the command now.
1144 if(receivedCmd
[0] == 0x0a ) {
1145 // Reader in anticollission phase
1146 resp
= resp1
; respLen
= resp1Len
; //order = 1;
1148 respsize
= sizeof(sof
);
1149 } else if(receivedCmd
[0] == 0x0c) {
1150 // Reader asks for anticollission CSN
1151 resp
= resp2
; respLen
= resp2Len
; //order = 2;
1152 respdata
= response2
;
1153 respsize
= sizeof(response2
);
1154 //DbpString("Reader requests anticollission CSN:");
1155 } else if(receivedCmd
[0] == 0x81) {
1156 // Reader selects anticollission CSN.
1157 // Tag sends the corresponding real CSN
1158 resp
= resp3
; respLen
= resp3Len
; //order = 3;
1159 respdata
= response3
;
1160 respsize
= sizeof(response3
);
1161 //DbpString("Reader selects anticollission CSN:");
1162 } else if(receivedCmd
[0] == 0x88) {
1163 // Read e-purse (88 02)
1164 resp
= resp4
; respLen
= resp4Len
; //order = 4;
1165 respdata
= response4
;
1166 respsize
= sizeof(response4
);
1168 } else if(receivedCmd
[0] == 0x05) {
1169 // Reader random and reader MAC!!!
1171 // We do not know what to answer, so lets keep quiet
1172 resp
= resp1
; respLen
= 0; //order = 5;
1175 if (breakAfterMacReceived
){
1177 Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x"
1178 ,csn
[0],csn
[1],csn
[2],csn
[3],csn
[4],csn
[5],csn
[6],csn
[7]);
1179 Dbprintf("RDR: (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len
,
1180 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1181 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1182 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1183 if (reader_mac_buf
!= NULL
)
1185 memcpy(reader_mac_buf
,receivedCmd
+1,8);
1189 } else if(receivedCmd
[0] == 0x00 && len
== 1) {
1190 // Reader ends the session
1191 resp
= resp1
; respLen
= 0; //order = 0;
1195 //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44
1196 // Never seen this command before
1197 Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x",
1199 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1200 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5],
1201 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1203 resp
= resp1
; respLen
= 0; //order = 0;
1208 if(cmdsRecvd
> 100) {
1209 //DbpString("100 commands later...");
1217 SendIClassAnswer(resp
, respLen
, 21);
1218 t2r_time
= GetCountSspClk();
1222 LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, Uart
.parityBits
,TRUE
);
1223 LogTrace(NULL
,0, (r2t_time
-time_0
) << 4, 0,TRUE
);
1225 if (respdata
!= NULL
) {
1226 LogTrace(respdata
,respsize
, (t2r_time
-time_0
) << 4,SwapBits(GetParity(respdata
,respsize
),respsize
),FALSE
);
1227 LogTrace(NULL
,0, (t2r_time
-time_0
) << 4,0,FALSE
);
1232 DbpString("Trace full");
1237 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
);
1240 //Dbprintf("%x", cmdsRecvd);
1245 DbpString("Button pressed");
1247 return buttonPressed
;
1250 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1252 int i
= 0, d
=0;//, u = 0, d = 0;
1255 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
1257 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1259 while(!BUTTON_PRESS()) {
1260 if((AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)){
1261 b
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
;
1263 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)){
1276 AT91C_BASE_SSC
->SSC_THR
= b
;
1279 if (i
> respLen
+4) break;
1287 //-----------------------------------------------------------------------------
1288 // Transmit the command (to the tag) that was placed in ToSend[].
1289 //-----------------------------------------------------------------------------
1290 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1293 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1294 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1299 if(*wait
< 10) *wait
= 10;
1301 for(c
= 0; c
< *wait
;) {
1302 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1303 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1306 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1307 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1317 bool firstpart
= TRUE
;
1320 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1322 // DOUBLE THE SAMPLES!
1324 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1327 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1330 if(sendbyte
== 0xff) {
1333 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1334 firstpart
= !firstpart
;
1340 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1341 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1346 if (samples
) *samples
= (c
+ *wait
) << 3;
1350 //-----------------------------------------------------------------------------
1351 // Prepare iClass reader command to send to FPGA
1352 //-----------------------------------------------------------------------------
1353 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1360 // Start of Communication: 1 out of 4
1361 ToSend
[++ToSendMax
] = 0xf0;
1362 ToSend
[++ToSendMax
] = 0x00;
1363 ToSend
[++ToSendMax
] = 0x0f;
1364 ToSend
[++ToSendMax
] = 0x00;
1366 // Modulate the bytes
1367 for (i
= 0; i
< len
; i
++) {
1369 for(j
= 0; j
< 4; j
++) {
1370 for(k
= 0; k
< 4; k
++) {
1372 ToSend
[++ToSendMax
] = 0x0f;
1375 ToSend
[++ToSendMax
] = 0x00;
1382 // End of Communication
1383 ToSend
[++ToSendMax
] = 0x00;
1384 ToSend
[++ToSendMax
] = 0x00;
1385 ToSend
[++ToSendMax
] = 0xf0;
1386 ToSend
[++ToSendMax
] = 0x00;
1388 // Convert from last character reference to length
1392 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1398 // This is tied to other size changes
1399 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1400 CodeIClassCommand(frame
,len
);
1403 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1407 // Store reader command in buffer
1408 if (tracing
) LogTrace(frame
,len
,rsamples
,par
,TRUE
);
1411 //-----------------------------------------------------------------------------
1412 // Wait a certain time for tag response
1413 // If a response is captured return TRUE
1414 // If it takes too long return FALSE
1415 //-----------------------------------------------------------------------------
1416 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1418 // buffer needs to be 512 bytes
1421 // Set FPGA mode to "reader listen mode", no modulation (listen
1422 // only, since we are receiving, not transmitting).
1423 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1425 // Now get the answer from the card
1426 Demod
.output
= receivedResponse
;
1428 Demod
.state
= DEMOD_UNSYNCD
;
1431 if (elapsed
) *elapsed
= 0;
1439 if(BUTTON_PRESS()) return FALSE
;
1441 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1442 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1443 if (elapsed
) (*elapsed
)++;
1445 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1446 if(c
< timeout
) { c
++; } else { return FALSE
; }
1447 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1450 /*if(ManchesterDecoding((b>>4) & 0xf)) {
1451 *samples = ((c - 1) << 3) + 4;
1454 if(ManchesterDecoding(b
& 0x0f)) {
1462 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1465 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1466 rsamples
+= samples
;
1467 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,rsamples
,Demod
.parityBits
,FALSE
);
1468 if(samples
== 0) return FALSE
;
1472 void setupIclassReader()
1474 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1475 // Reset trace buffer
1476 iso14a_set_tracing(TRUE
);
1477 iso14a_clear_trace();
1481 // Start from off (no field generated)
1482 // Signal field is off with the appropriate LED
1484 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1487 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1489 // Now give it time to spin up.
1490 // Signal field is on with the appropriate LED
1491 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1497 // Reader iClass Anticollission
1498 void ReaderIClass(uint8_t arg0
) {
1499 uint8_t act_all
[] = { 0x0a };
1500 uint8_t identify
[] = { 0x0c };
1501 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1502 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1504 uint8_t card_data
[24]={0};
1505 uint8_t last_csn
[8]={0};
1507 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1510 bool abort_after_read
= arg0
& FLAG_ICLASS_READER_ONLY_ONCE
;
1512 setupIclassReader();
1514 size_t datasize
= 0;
1515 while(!BUTTON_PRESS())
1520 ReaderTransmitIClass(act_all
, 1);
1522 if(ReaderReceiveIClass(resp
)) {
1524 ReaderTransmitIClass(identify
, 1);
1526 if(ReaderReceiveIClass(resp
) == 10) {
1527 //Copy the Anti-collision CSN to our select-packet
1528 memcpy(&select
[1],resp
,8);
1529 //Dbprintf("Anti-collision CSN: %02x %02x %02x %02x %02x %02x %02x %02x",resp[0], resp[1], resp[2],
1530 // resp[3], resp[4], resp[5],
1531 // resp[6], resp[7]);
1533 ReaderTransmitIClass(select
, sizeof(select
));
1535 if(ReaderReceiveIClass(resp
) == 10) {
1536 //Save CSN in response data
1537 memcpy(card_data
,resp
,8);
1539 //Flag that we got to at least stage 1, read CSN
1543 //Dbprintf("Readcheck on Sector 2");
1544 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1545 if(ReaderReceiveIClass(resp
) == 8) {
1546 //Save CC (e-purse) in response data
1547 memcpy(card_data
+8,resp
,8);
1554 //Send back to client, but don't bother if we already sent this
1555 if(memcmp(last_csn
, card_data
, 8) != 0)
1556 cmd_send(CMD_ACK
,read_status
,0,0,card_data
,datasize
);
1558 //Save that we already sent this....
1559 if(read_status
== 2)
1560 memcpy(last_csn
, card_data
, 8);
1564 if(abort_after_read
) break;
1569 if(traceLen
> TRACE_SIZE
) {
1570 DbpString("Trace full");
1577 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) {
1578 uint8_t act_all
[] = { 0x0a };
1579 uint8_t identify
[] = { 0x0c };
1580 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1581 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1582 uint8_t check
[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1583 uint8_t read
[] = { 0x0c, 0x00, 0x00, 0x00 };
1587 bool read_success
=false;
1590 static struct memory_t
{
1598 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1600 setupIclassReader();
1603 for(int i
=0;i
<1;i
++) {
1605 if(traceLen
> TRACE_SIZE
) {
1606 DbpString("Trace full");
1610 if (BUTTON_PRESS()) break;
1613 ReaderTransmitIClass(act_all
, 1);
1615 if(ReaderReceiveIClass(resp
)) {
1616 ReaderTransmitIClass(identify
, 1);
1617 if(ReaderReceiveIClass(resp
) == 10) {
1619 memcpy(&select
[1],resp
,8);
1620 ReaderTransmitIClass(select
, sizeof(select
));
1622 if(ReaderReceiveIClass(resp
) == 10) {
1623 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1624 resp
[0], resp
[1], resp
[2],
1625 resp
[3], resp
[4], resp
[5],
1629 Dbprintf("Readcheck on Sector 2");
1630 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1631 if(ReaderReceiveIClass(resp
) == 8) {
1632 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1633 resp
[0], resp
[1], resp
[2],
1634 resp
[3], resp
[4], resp
[5],
1637 Dbprintf("Authenticate");
1638 //for now replay captured auth (as cc not updated)
1639 memcpy(check
+5,MAC
,4);
1640 //Dbprintf(" AA: %02x %02x %02x %02x",
1641 // check[5], check[6], check[7],check[8]);
1642 ReaderTransmitIClass(check
, sizeof(check
));
1643 if(ReaderReceiveIClass(resp
) == 4) {
1644 Dbprintf(" AR: %02x %02x %02x %02x",
1645 resp
[0], resp
[1], resp
[2],resp
[3]);
1647 Dbprintf("Error: Authentication Fail!");
1650 Dbprintf("Dump Contents");
1651 //first get configuration block
1654 uint8_t *blockno
=&read
[1];
1655 crc
= iclass_crc16((char *)blockno
,1);
1657 read
[3] = crc
& 0xff;
1658 while(!read_success
){
1659 ReaderTransmitIClass(read
, sizeof(read
));
1660 if(ReaderReceiveIClass(resp
) == 10) {
1663 memory
.k16
= (mem
& 0x80);
1664 memory
.book
= (mem
& 0x20);
1665 memory
.k2
= (mem
& 0x8);
1666 memory
.lockauth
= (mem
& 0x2);
1667 memory
.keyaccess
= (mem
& 0x1);
1674 //then loop around remaining blocks
1675 for(uint8_t j
=0; j
<cardsize
; j
++){
1677 uint8_t *blockno
=&j
;
1680 crc
= iclass_crc16((char *)blockno
,1);
1682 read
[3] = crc
& 0xff;
1683 while(!read_success
){
1684 ReaderTransmitIClass(read
, sizeof(read
));
1685 if(ReaderReceiveIClass(resp
) == 10) {
1687 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1688 j
, resp
[0], resp
[1], resp
[2],
1689 resp
[3], resp
[4], resp
[5],
1702 //2. Create Read method (cut-down from above) based off responses from 1.
1703 // Since we have the MAC could continue to use replay function.
1704 //3. Create Write method
1706 void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) {
1707 uint8_t act_all[] = { 0x0a };
1708 uint8_t identify[] = { 0x0c };
1709 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1710 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1711 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1712 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1713 uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1717 uint8_t* resp = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
1719 // Reset trace buffer
1720 memset(trace, 0x44, RECV_CMD_OFFSET);
1725 // Start from off (no field generated)
1726 // Signal field is off with the appropriate LED
1728 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1731 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1733 // Now give it time to spin up.
1734 // Signal field is on with the appropriate LED
1735 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1740 for(int i=0;i<1;i++) {
1742 if(traceLen > TRACE_SIZE) {
1743 DbpString("Trace full");
1747 if (BUTTON_PRESS()) break;
1750 ReaderTransmitIClass(act_all, 1);
1752 if(ReaderReceiveIClass(resp)) {
1753 ReaderTransmitIClass(identify, 1);
1754 if(ReaderReceiveIClass(resp) == 10) {
1756 memcpy(&select[1],resp,8);
1757 ReaderTransmitIClass(select, sizeof(select));
1759 if(ReaderReceiveIClass(resp) == 10) {
1760 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1761 resp[0], resp[1], resp[2],
1762 resp[3], resp[4], resp[5],
1766 Dbprintf("Readcheck on Sector 2");
1767 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1768 if(ReaderReceiveIClass(resp) == 8) {
1769 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1770 resp[0], resp[1], resp[2],
1771 resp[3], resp[4], resp[5],
1774 Dbprintf("Authenticate");
1775 //for now replay captured auth (as cc not updated)
1776 memcpy(check+5,MAC,4);
1777 Dbprintf(" AA: %02x %02x %02x %02x",
1778 check[5], check[6], check[7],check[8]);
1779 ReaderTransmitIClass(check, sizeof(check));
1780 if(ReaderReceiveIClass(resp) == 4) {
1781 Dbprintf(" AR: %02x %02x %02x %02x",
1782 resp[0], resp[1], resp[2],resp[3]);
1784 Dbprintf("Error: Authentication Fail!");
1787 Dbprintf("Write Block");
1789 //read configuration for max block number
1792 uint8_t *blockno=&read[1];
1793 crc = iclass_crc16((char *)blockno,1);
1795 read[3] = crc & 0xff;
1796 while(!read_success){
1797 ReaderTransmitIClass(read, sizeof(read));
1798 if(ReaderReceiveIClass(resp) == 10) {
1801 memory.k16= (mem & 0x80);
1802 memory.book= (mem & 0x20);
1803 memory.k2= (mem & 0x8);
1804 memory.lockauth= (mem & 0x2);
1805 memory.keyaccess= (mem & 0x1);
1814 memcpy(write+1,blockNo,1);
1815 memcpy(write+2,data,8);
1816 memcpy(write+10,mac,4);
1817 while(!send_success){
1818 ReaderTransmitIClass(write, sizeof(write));
1819 if(ReaderReceiveIClass(resp) == 10) {