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
;
436 //modulation = ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
440 if(Demod
.posCount
==0) {
443 Demod
.sub
= SUB_FIRST_HALF
;
446 Demod
.sub
= SUB_NONE
;
451 /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
452 if(Demod.state!=DEMOD_ERROR_WAIT) {
453 Demod.state = DEMOD_ERROR_WAIT;
454 Demod.output[Demod.len] = 0xaa;
458 //else if(modulation) {
460 if(Demod
.sub
== SUB_FIRST_HALF
) {
461 Demod
.sub
= SUB_BOTH
;
464 Demod
.sub
= SUB_SECOND_HALF
;
467 else if(Demod
.sub
== SUB_NONE
) {
468 if(Demod
.state
== DEMOD_SOF_COMPLETE
) {
469 Demod
.output
[Demod
.len
] = 0x0f;
471 Demod
.state
= DEMOD_UNSYNCD
;
476 Demod
.state
= DEMOD_ERROR_WAIT
;
479 /*if(Demod.state!=DEMOD_ERROR_WAIT) {
480 Demod.state = DEMOD_ERROR_WAIT;
481 Demod.output[Demod.len] = 0xaa;
486 switch(Demod
.state
) {
487 case DEMOD_START_OF_COMMUNICATION
:
488 if(Demod
.sub
== SUB_BOTH
) {
489 //Demod.state = DEMOD_MANCHESTER_D;
490 Demod
.state
= DEMOD_START_OF_COMMUNICATION2
;
492 Demod
.sub
= SUB_NONE
;
495 Demod
.output
[Demod
.len
] = 0xab;
496 Demod
.state
= DEMOD_ERROR_WAIT
;
500 case DEMOD_START_OF_COMMUNICATION2
:
501 if(Demod
.sub
== SUB_SECOND_HALF
) {
502 Demod
.state
= DEMOD_START_OF_COMMUNICATION3
;
505 Demod
.output
[Demod
.len
] = 0xab;
506 Demod
.state
= DEMOD_ERROR_WAIT
;
510 case DEMOD_START_OF_COMMUNICATION3
:
511 if(Demod
.sub
== SUB_SECOND_HALF
) {
512 // Demod.state = DEMOD_MANCHESTER_D;
513 Demod
.state
= DEMOD_SOF_COMPLETE
;
514 //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
518 Demod
.output
[Demod
.len
] = 0xab;
519 Demod
.state
= DEMOD_ERROR_WAIT
;
523 case DEMOD_SOF_COMPLETE
:
524 case DEMOD_MANCHESTER_D
:
525 case DEMOD_MANCHESTER_E
:
526 // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
527 // 00001111 = 1 (0 in 14443)
528 if(Demod
.sub
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF
530 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
531 Demod
.state
= DEMOD_MANCHESTER_D
;
533 else if(Demod
.sub
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF
535 Demod
.shiftReg
>>= 1;
536 Demod
.state
= DEMOD_MANCHESTER_E
;
538 else if(Demod
.sub
== SUB_BOTH
) {
539 Demod
.state
= DEMOD_MANCHESTER_F
;
542 Demod
.state
= DEMOD_ERROR_WAIT
;
547 case DEMOD_MANCHESTER_F
:
548 // Tag response does not need to be a complete byte!
549 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
550 if(Demod
.bitCount
> 1) { // was > 0, do not interpret last closing bit, is part of EOF
551 Demod
.shiftReg
>>= (9 - Demod
.bitCount
); // right align data
552 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
556 Demod
.state
= DEMOD_UNSYNCD
;
560 Demod
.output
[Demod
.len
] = 0xad;
561 Demod
.state
= DEMOD_ERROR_WAIT
;
566 case DEMOD_ERROR_WAIT
:
567 Demod
.state
= DEMOD_UNSYNCD
;
571 Demod
.output
[Demod
.len
] = 0xdd;
572 Demod
.state
= DEMOD_UNSYNCD
;
576 /*if(Demod.bitCount>=9) {
577 Demod.output[Demod.len] = Demod.shiftReg & 0xff;
580 Demod.parityBits <<= 1;
581 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
586 if(Demod
.bitCount
>=8) {
587 Demod
.shiftReg
>>= 1;
588 Demod
.output
[Demod
.len
] = (Demod
.shiftReg
& 0xff);
595 Demod
.output
[Demod
.len
] = 0xBB;
597 Demod
.output
[Demod
.len
] = error
& 0xFF;
599 Demod
.output
[Demod
.len
] = 0xBB;
601 Demod
.output
[Demod
.len
] = bit
& 0xFF;
603 Demod
.output
[Demod
.len
] = Demod
.buffer
& 0xFF;
606 Demod
.output
[Demod
.len
] = Demod
.buffer2
& 0xFF;
608 Demod
.output
[Demod
.len
] = Demod
.syncBit
& 0xFF;
610 Demod
.output
[Demod
.len
] = 0xBB;
617 } // end (state != UNSYNCED)
622 //=============================================================================
623 // Finally, a `sniffer' for iClass communication
624 // Both sides of communication!
625 //=============================================================================
627 //-----------------------------------------------------------------------------
628 // Record the sequence of commands sent by the reader to the tag, with
629 // triggering so that we start recording at the point that the tag is moved
631 //-----------------------------------------------------------------------------
632 void RAMFUNC
SnoopIClass(void)
636 // We won't start recording the frames that we acquire until we trigger;
637 // a good trigger condition to get started is probably when we see a
638 // response from the tag.
639 //int triggered = FALSE; // FALSE to wait first for card
641 // The command (reader -> tag) that we're receiving.
642 // The length of a received command will in most cases be no more than 18 bytes.
643 // So 32 should be enough!
644 uint8_t *readerToTagCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
645 // The response (tag -> reader) that we're receiving.
646 uint8_t *tagToReaderResponse
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
648 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
650 // reset traceLen to 0
651 iso14a_set_tracing(TRUE
);
652 iso14a_clear_trace();
653 iso14a_set_trigger(FALSE
);
655 // The DMA buffer, used to stream samples from the FPGA
656 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
662 // Count of samples received so far, so that we can include timing
663 // information in the trace buffer.
667 // Set up the demodulator for tag -> reader responses.
668 Demod
.output
= tagToReaderResponse
;
670 Demod
.state
= DEMOD_UNSYNCD
;
672 // Setup for the DMA.
675 lastRxCounter
= DMA_BUFFER_SIZE
;
676 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
678 // And the reader -> tag commands
679 memset(&Uart
, 0, sizeof(Uart
));
680 Uart
.output
= readerToTagCmd
;
681 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
682 Uart
.state
= STATE_UNSYNCD
;
684 // And put the FPGA in the appropriate mode
685 // Signal field is off with the appropriate LED
687 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
688 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
690 uint32_t time_0
= GetCountSspClk();
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
;
744 //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
745 //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
747 uint8_t parity
[MAX_PARITY_SIZE
];
748 GetParity(Uart
.output
, Uart
.byteCnt
, parity
);
749 LogTrace(Uart
.output
,Uart
.byteCnt
, (GetCountSspClk()-time_0
) << 4, (GetCountSspClk()-time_0
) << 4, parity
, TRUE
);
753 /* And ready to receive another command. */
754 Uart
.state
= STATE_UNSYNCD
;
755 /* And also reset the demod code, which might have been */
756 /* false-triggered by the commands from the reader. */
757 Demod
.state
= DEMOD_UNSYNCD
;
766 if(ManchesterDecoding(smpl
& 0x0F)) {
767 rsamples
= samples
- Demod
.samples
;
771 uint8_t parity
[MAX_PARITY_SIZE
];
772 GetParity(Demod
.output
, Demod
.len
, parity
);
773 LogTrace(Demod
.output
, Demod
.len
, (GetCountSspClk()-time_0
) << 4, (GetCountSspClk()-time_0
) << 4, parity
, FALSE
);
777 // And ready to receive another response.
778 memset(&Demod
, 0, sizeof(Demod
));
779 Demod
.output
= tagToReaderResponse
;
780 Demod
.state
= DEMOD_UNSYNCD
;
790 DbpString("cancelled_a");
795 DbpString("COMMAND FINISHED");
797 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
798 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
801 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
802 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
803 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
810 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) {
812 for(i
= 0; i
< 8; i
++) {
813 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8] << 5);
817 //-----------------------------------------------------------------------------
818 // Wait for commands from reader
819 // Stop when button is pressed
820 // Or return TRUE when command is captured
821 //-----------------------------------------------------------------------------
822 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
824 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
825 // only, since we are receiving, not transmitting).
826 // Signal field is off with the appropriate LED
828 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
830 // Now run a `software UART' on the stream of incoming samples.
831 Uart
.output
= received
;
832 Uart
.byteCntMax
= maxLen
;
833 Uart
.state
= STATE_UNSYNCD
;
838 if(BUTTON_PRESS()) return FALSE
;
840 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
841 AT91C_BASE_SSC
->SSC_THR
= 0x00;
843 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
844 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
845 /*if(OutOfNDecoding((b & 0xf0) >> 4)) {
849 if(OutOfNDecoding(b
& 0x0f)) {
858 //-----------------------------------------------------------------------------
859 // Prepare tag messages
860 //-----------------------------------------------------------------------------
861 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
)
863 //So far a dummy implementation, not used
864 //int lastProxToAirDuration =0;
870 ToSend
[++ToSendMax
] = 0x00;
871 ToSend
[++ToSendMax
] = 0x00;
872 ToSend
[++ToSendMax
] = 0x00;
873 ToSend
[++ToSendMax
] = 0xff;//Proxtoair duration starts here
874 ToSend
[++ToSendMax
] = 0xff;
875 ToSend
[++ToSendMax
] = 0xff;
876 ToSend
[++ToSendMax
] = 0x00;
877 ToSend
[++ToSendMax
] = 0xff;
879 for(i
= 0; i
< len
; i
++) {
884 for(j
= 0; j
< 8; j
++) {
886 ToSend
[++ToSendMax
] = 0x00;
887 ToSend
[++ToSendMax
] = 0xff;
889 ToSend
[++ToSendMax
] = 0xff;
890 ToSend
[++ToSendMax
] = 0x00;
897 ToSend
[++ToSendMax
] = 0xff;
898 ToSend
[++ToSendMax
] = 0x00;
899 ToSend
[++ToSendMax
] = 0xff;
900 ToSend
[++ToSendMax
] = 0xff;
901 ToSend
[++ToSendMax
] = 0xff;
902 ToSend
[++ToSendMax
] = 0x00;
903 ToSend
[++ToSendMax
] = 0x00;
904 ToSend
[++ToSendMax
] = 0x00;
906 //lastProxToAirDuration = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end
908 // Convert from last byte pos to length
913 static void CodeIClassTagSOF()
915 //So far a dummy implementation, not used
916 //int lastProxToAirDuration =0;
920 ToSend
[++ToSendMax
] = 0x00;
921 ToSend
[++ToSendMax
] = 0x00;
922 ToSend
[++ToSendMax
] = 0x00;
923 ToSend
[++ToSendMax
] = 0xff;
924 ToSend
[++ToSendMax
] = 0xff;
925 ToSend
[++ToSendMax
] = 0xff;
926 ToSend
[++ToSendMax
] = 0x00;
927 ToSend
[++ToSendMax
] = 0xff;
929 // lastProxToAirDuration = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning
932 // Convert from last byte pos to length
935 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
);
937 * @brief SimulateIClass simulates an iClass card.
938 * @param arg0 type of simulation
939 * - 0 uses the first 8 bytes in usb data as CSN
940 * - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified
941 * in the usb data. This mode collects MAC from the reader, in order to do an offline
942 * attack on the keys. For more info, see "dismantling iclass" and proxclone.com.
943 * - Other : Uses the default CSN (031fec8af7ff12e0)
944 * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only)
948 void SimulateIClass(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
)
950 uint32_t simType
= arg0
;
951 uint32_t numberOfCSNS
= arg1
;
952 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
954 // Enable and clear the trace
955 iso14a_set_tracing(TRUE
);
956 iso14a_clear_trace();
958 uint8_t csn_crc
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 };
960 // Use the CSN from commandline
961 memcpy(csn_crc
, datain
, 8);
962 doIClassSimulation(csn_crc
,0,NULL
);
963 }else if(simType
== 1)
965 doIClassSimulation(csn_crc
,0,NULL
);
967 else if(simType
== 2)
970 uint8_t mac_responses
[64] = { 0 };
971 Dbprintf("Going into attack mode, %d CSNS sent", numberOfCSNS
);
972 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time
973 // in order to collect MAC's from the reader. This can later be used in an offlne-attack
974 // in order to obtain the keys, as in the "dismantling iclass"-paper.
976 for( ; i
< numberOfCSNS
&& i
*8+8 < USB_CMD_DATA_SIZE
; i
++)
978 // The usb data is 512 bytes, fitting 65 8-byte CSNs in there.
980 memcpy(csn_crc
, datain
+(i
*8), 8);
981 if(doIClassSimulation(csn_crc
,1,mac_responses
+i
*8))
983 return; // Button pressed
986 cmd_send(CMD_ACK
,CMD_SIMULATE_TAG_ICLASS
,i
,0,mac_responses
,i
*8);
990 // We may want a mode here where we hardcode the csns to use (from proxclone).
991 // That will speed things up a little, but not required just yet.
992 Dbprintf("The mode is not implemented, reserved for future use");
998 * @brief Does the actual simulation
999 * @param csn - csn to use
1000 * @param breakAfterMacReceived if true, returns after reader MAC has been received.
1002 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
)
1006 // CSN followed by two CRC bytes
1007 uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1008 uint8_t response3
[] = { 0,0,0,0,0,0,0,0,0,0};
1009 memcpy(response3
,csn
,sizeof(response3
));
1010 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]);
1012 uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1014 // Construct anticollision-CSN
1015 rotateCSN(response3
,response2
);
1017 // Compute CRC on both CSNs
1018 ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]);
1019 ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]);
1025 // Tag anticoll. CSN
1026 // Reader 81 anticoll. CSN
1031 uint8_t* respdata
= NULL
;
1035 // Respond SOF -- takes 8 bytes
1036 uint8_t *resp1
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
);
1039 // Anticollision CSN (rotated CSN)
1040 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1041 uint8_t *resp2
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 10);
1045 // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit)
1046 uint8_t *resp3
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 190);
1050 // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit)
1051 uint8_t *resp4
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
+ 370);
1055 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
1056 memset(receivedCmd
, 0x44, MAX_FRAME_SIZE
);
1059 // Prepare card messages
1062 // First card answer: SOF
1064 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1066 // Anticollision CSN
1067 CodeIClassTagAnswer(response2
, sizeof(response2
));
1068 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1071 CodeIClassTagAnswer(response3
, sizeof(response3
));
1072 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1075 CodeIClassTagAnswer(response4
, sizeof(response4
));
1076 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1079 // Start from off (no field generated)
1080 //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1082 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
1085 // We need to listen to the high-frequency, peak-detected path.
1086 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1089 // To control where we are in the protocol
1091 uint32_t time_0
= GetCountSspClk();
1092 uint32_t t2r_time
=0;
1093 uint32_t r2t_time
=0;
1096 bool buttonPressed
= false;
1098 /** Hack for testing
1099 memcpy(reader_mac_buf,csn,8);
1107 // 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 resp
= resp1
; respLen
= resp1Len
; //order = 1;
1122 respsize
= sizeof(sof
);
1123 } else if(receivedCmd
[0] == 0x0c) {
1124 // Reader asks for anticollission CSN
1125 resp
= resp2
; respLen
= resp2Len
; //order = 2;
1126 respdata
= response2
;
1127 respsize
= 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 resp
= resp3
; respLen
= resp3Len
; //order = 3;
1133 respdata
= response3
;
1134 respsize
= sizeof(response3
);
1135 //DbpString("Reader selects anticollission CSN:");
1136 } else if(receivedCmd
[0] == 0x88) {
1137 // Read e-purse (88 02)
1138 resp
= resp4
; respLen
= resp4Len
; //order = 4;
1139 respdata
= response4
;
1140 respsize
= 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 resp
= resp1
; respLen
= 0; //order = 5;
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 resp
= resp1
; respLen
= 0; //order = 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 resp
= resp1
; respLen
= 0; //order = 0;
1182 if(cmdsRecvd
> 100) {
1183 //DbpString("100 commands later...");
1191 SendIClassAnswer(resp
, respLen
, 21);
1192 t2r_time
= GetCountSspClk();
1196 uint8_t parity
[MAX_PARITY_SIZE
];
1197 GetParity(receivedCmd
, len
, parity
);
1198 LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, (r2t_time
-time_0
) << 4, parity
, TRUE
);
1200 if (respdata
!= NULL
) {
1201 GetParity(respdata
, respsize
, parity
);
1202 LogTrace(respdata
, respsize
, (t2r_time
-time_0
) << 4, (t2r_time
-time_0
) << 4, parity
, FALSE
);
1205 DbpString("Trace full");
1210 memset(receivedCmd
, 0x44, MAX_FRAME_SIZE
);
1213 //Dbprintf("%x", cmdsRecvd);
1218 DbpString("Button pressed");
1220 return buttonPressed
;
1223 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
)
1225 int i
= 0, d
=0;//, u = 0, d = 0;
1228 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
);
1230 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1232 while(!BUTTON_PRESS()) {
1233 if((AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)){
1234 b
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
;
1236 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)){
1249 AT91C_BASE_SSC
->SSC_THR
= b
;
1252 if (i
> respLen
+4) break;
1260 //-----------------------------------------------------------------------------
1261 // Transmit the command (to the tag) that was placed in ToSend[].
1262 //-----------------------------------------------------------------------------
1263 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1266 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1267 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1272 if(*wait
< 10) *wait
= 10;
1274 for(c
= 0; c
< *wait
;) {
1275 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1276 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1279 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1280 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1290 bool firstpart
= TRUE
;
1293 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1295 // DOUBLE THE SAMPLES!
1297 sendbyte
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);
1300 sendbyte
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4);
1303 if(sendbyte
== 0xff) {
1306 AT91C_BASE_SSC
->SSC_THR
= sendbyte
;
1307 firstpart
= !firstpart
;
1313 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1314 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1319 if (samples
) *samples
= (c
+ *wait
) << 3;
1323 //-----------------------------------------------------------------------------
1324 // Prepare iClass reader command to send to FPGA
1325 //-----------------------------------------------------------------------------
1326 void CodeIClassCommand(const uint8_t * cmd
, int len
)
1333 // Start of Communication: 1 out of 4
1334 ToSend
[++ToSendMax
] = 0xf0;
1335 ToSend
[++ToSendMax
] = 0x00;
1336 ToSend
[++ToSendMax
] = 0x0f;
1337 ToSend
[++ToSendMax
] = 0x00;
1339 // Modulate the bytes
1340 for (i
= 0; i
< len
; i
++) {
1342 for(j
= 0; j
< 4; j
++) {
1343 for(k
= 0; k
< 4; k
++) {
1345 ToSend
[++ToSendMax
] = 0x0f;
1348 ToSend
[++ToSendMax
] = 0x00;
1355 // End of Communication
1356 ToSend
[++ToSendMax
] = 0x00;
1357 ToSend
[++ToSendMax
] = 0x00;
1358 ToSend
[++ToSendMax
] = 0xf0;
1359 ToSend
[++ToSendMax
] = 0x00;
1361 // Convert from last character reference to length
1365 void ReaderTransmitIClass(uint8_t* frame
, int len
)
1370 // This is tied to other size changes
1371 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1372 CodeIClassCommand(frame
,len
);
1375 TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
);
1379 // Store reader command in buffer
1381 uint8_t par
[MAX_PARITY_SIZE
];
1382 GetParity(frame
, len
, par
);
1383 LogTrace(frame
, len
, rsamples
, rsamples
, par
, TRUE
);
1387 //-----------------------------------------------------------------------------
1388 // Wait a certain time for tag response
1389 // If a response is captured return TRUE
1390 // If it takes too long return FALSE
1391 //-----------------------------------------------------------------------------
1392 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1394 // buffer needs to be 512 bytes
1397 // Set FPGA mode to "reader listen mode", no modulation (listen
1398 // only, since we are receiving, not transmitting).
1399 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1401 // Now get the answer from the card
1402 Demod
.output
= receivedResponse
;
1404 Demod
.state
= DEMOD_UNSYNCD
;
1407 if (elapsed
) *elapsed
= 0;
1415 if(BUTTON_PRESS()) return FALSE
;
1417 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1418 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1419 if (elapsed
) (*elapsed
)++;
1421 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1422 if(c
< timeout
) { c
++; } else { return FALSE
; }
1423 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1426 /*if(ManchesterDecoding((b>>4) & 0xf)) {
1427 *samples = ((c - 1) << 3) + 4;
1430 if(ManchesterDecoding(b
& 0x0f)) {
1438 int ReaderReceiveIClass(uint8_t* receivedAnswer
)
1441 if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
;
1442 rsamples
+= samples
;
1444 uint8_t parity
[MAX_PARITY_SIZE
];
1445 GetParity(receivedAnswer
, Demod
.len
, parity
);
1446 LogTrace(receivedAnswer
,Demod
.len
,rsamples
,rsamples
,parity
,FALSE
);
1448 if(samples
== 0) return FALSE
;
1452 void setupIclassReader()
1454 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1455 // Reset trace buffer
1456 iso14a_set_tracing(TRUE
);
1457 iso14a_clear_trace();
1461 // Start from off (no field generated)
1462 // Signal field is off with the appropriate LED
1464 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1467 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1469 // Now give it time to spin up.
1470 // Signal field is on with the appropriate LED
1471 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1477 // Reader iClass Anticollission
1478 void ReaderIClass(uint8_t arg0
) {
1479 uint8_t act_all
[] = { 0x0a };
1480 uint8_t identify
[] = { 0x0c };
1481 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1482 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1484 uint8_t card_data
[24]={0};
1485 uint8_t last_csn
[8]={0};
1487 uint8_t *resp
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
1490 bool abort_after_read
= arg0
& FLAG_ICLASS_READER_ONLY_ONCE
;
1492 setupIclassReader();
1494 size_t datasize
= 0;
1495 while(!BUTTON_PRESS())
1500 ReaderTransmitIClass(act_all
, 1);
1502 if(ReaderReceiveIClass(resp
)) {
1504 ReaderTransmitIClass(identify
, 1);
1506 if(ReaderReceiveIClass(resp
) == 10) {
1507 //Copy the Anti-collision CSN to our select-packet
1508 memcpy(&select
[1],resp
,8);
1509 //Dbprintf("Anti-collision CSN: %02x %02x %02x %02x %02x %02x %02x %02x",resp[0], resp[1], resp[2],
1510 // resp[3], resp[4], resp[5],
1511 // resp[6], resp[7]);
1513 ReaderTransmitIClass(select
, sizeof(select
));
1515 if(ReaderReceiveIClass(resp
) == 10) {
1516 //Save CSN in response data
1517 memcpy(card_data
,resp
,8);
1519 //Flag that we got to at least stage 1, read CSN
1523 //Dbprintf("Readcheck on Sector 2");
1524 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1525 if(ReaderReceiveIClass(resp
) == 8) {
1526 //Save CC (e-purse) in response data
1527 memcpy(card_data
+8,resp
,8);
1534 //Send back to client, but don't bother if we already sent this
1535 if(memcmp(last_csn
, card_data
, 8) != 0)
1536 cmd_send(CMD_ACK
,read_status
,0,0,card_data
,datasize
);
1538 //Save that we already sent this....
1539 if(read_status
== 2)
1540 memcpy(last_csn
, card_data
, 8);
1544 if(abort_after_read
) break;
1549 if(traceLen
> TRACE_SIZE
) {
1550 DbpString("Trace full");
1557 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) {
1558 uint8_t act_all
[] = { 0x0a };
1559 uint8_t identify
[] = { 0x0c };
1560 uint8_t select
[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1561 uint8_t readcheck_cc
[]= { 0x88, 0x02 };
1562 uint8_t check
[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1563 uint8_t read
[] = { 0x0c, 0x00, 0x00, 0x00 };
1567 bool read_success
=false;
1570 static struct memory_t
{
1578 uint8_t* resp
= (((uint8_t *)BigBuf
) + RECV_RESP_OFFSET
);
1580 setupIclassReader();
1583 for(int i
=0;i
<1;i
++) {
1585 if(traceLen
> TRACE_SIZE
) {
1586 DbpString("Trace full");
1590 if (BUTTON_PRESS()) break;
1593 ReaderTransmitIClass(act_all
, 1);
1595 if(ReaderReceiveIClass(resp
)) {
1596 ReaderTransmitIClass(identify
, 1);
1597 if(ReaderReceiveIClass(resp
) == 10) {
1599 memcpy(&select
[1],resp
,8);
1600 ReaderTransmitIClass(select
, sizeof(select
));
1602 if(ReaderReceiveIClass(resp
) == 10) {
1603 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1604 resp
[0], resp
[1], resp
[2],
1605 resp
[3], resp
[4], resp
[5],
1609 Dbprintf("Readcheck on Sector 2");
1610 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
));
1611 if(ReaderReceiveIClass(resp
) == 8) {
1612 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1613 resp
[0], resp
[1], resp
[2],
1614 resp
[3], resp
[4], resp
[5],
1617 Dbprintf("Authenticate");
1618 //for now replay captured auth (as cc not updated)
1619 memcpy(check
+5,MAC
,4);
1620 //Dbprintf(" AA: %02x %02x %02x %02x",
1621 // check[5], check[6], check[7],check[8]);
1622 ReaderTransmitIClass(check
, sizeof(check
));
1623 if(ReaderReceiveIClass(resp
) == 4) {
1624 Dbprintf(" AR: %02x %02x %02x %02x",
1625 resp
[0], resp
[1], resp
[2],resp
[3]);
1627 Dbprintf("Error: Authentication Fail!");
1630 Dbprintf("Dump Contents");
1631 //first get configuration block
1634 uint8_t *blockno
=&read
[1];
1635 crc
= iclass_crc16((char *)blockno
,1);
1637 read
[3] = crc
& 0xff;
1638 while(!read_success
){
1639 ReaderTransmitIClass(read
, sizeof(read
));
1640 if(ReaderReceiveIClass(resp
) == 10) {
1643 memory
.k16
= (mem
& 0x80);
1644 memory
.book
= (mem
& 0x20);
1645 memory
.k2
= (mem
& 0x8);
1646 memory
.lockauth
= (mem
& 0x2);
1647 memory
.keyaccess
= (mem
& 0x1);
1654 //then loop around remaining blocks
1655 for(uint8_t j
=0; j
<cardsize
; j
++){
1657 uint8_t *blockno
=&j
;
1660 crc
= iclass_crc16((char *)blockno
,1);
1662 read
[3] = crc
& 0xff;
1663 while(!read_success
){
1664 ReaderTransmitIClass(read
, sizeof(read
));
1665 if(ReaderReceiveIClass(resp
) == 10) {
1667 Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
1668 j
, resp
[0], resp
[1], resp
[2],
1669 resp
[3], resp
[4], resp
[5],
1682 //2. Create Read method (cut-down from above) based off responses from 1.
1683 // Since we have the MAC could continue to use replay function.
1684 //3. Create Write method
1686 void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) {
1687 uint8_t act_all[] = { 0x0a };
1688 uint8_t identify[] = { 0x0c };
1689 uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1690 uint8_t readcheck_cc[]= { 0x88, 0x02 };
1691 uint8_t check[] = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1692 uint8_t read[] = { 0x0c, 0x00, 0x00, 0x00 };
1693 uint8_t write[] = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1697 uint8_t* resp = (((uint8_t *)BigBuf) + 3560);
1699 // Reset trace buffer
1700 memset(trace, 0x44, RECV_CMD_OFFSET);
1705 // Start from off (no field generated)
1706 // Signal field is off with the appropriate LED
1708 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1711 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1713 // Now give it time to spin up.
1714 // Signal field is on with the appropriate LED
1715 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1720 for(int i=0;i<1;i++) {
1722 if(traceLen > TRACE_SIZE) {
1723 DbpString("Trace full");
1727 if (BUTTON_PRESS()) break;
1730 ReaderTransmitIClass(act_all, 1);
1732 if(ReaderReceiveIClass(resp)) {
1733 ReaderTransmitIClass(identify, 1);
1734 if(ReaderReceiveIClass(resp) == 10) {
1736 memcpy(&select[1],resp,8);
1737 ReaderTransmitIClass(select, sizeof(select));
1739 if(ReaderReceiveIClass(resp) == 10) {
1740 Dbprintf(" Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x",
1741 resp[0], resp[1], resp[2],
1742 resp[3], resp[4], resp[5],
1746 Dbprintf("Readcheck on Sector 2");
1747 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
1748 if(ReaderReceiveIClass(resp) == 8) {
1749 Dbprintf(" CC: %02x %02x %02x %02x %02x %02x %02x %02x",
1750 resp[0], resp[1], resp[2],
1751 resp[3], resp[4], resp[5],
1754 Dbprintf("Authenticate");
1755 //for now replay captured auth (as cc not updated)
1756 memcpy(check+5,MAC,4);
1757 Dbprintf(" AA: %02x %02x %02x %02x",
1758 check[5], check[6], check[7],check[8]);
1759 ReaderTransmitIClass(check, sizeof(check));
1760 if(ReaderReceiveIClass(resp) == 4) {
1761 Dbprintf(" AR: %02x %02x %02x %02x",
1762 resp[0], resp[1], resp[2],resp[3]);
1764 Dbprintf("Error: Authentication Fail!");
1767 Dbprintf("Write Block");
1769 //read configuration for max block number
1772 uint8_t *blockno=&read[1];
1773 crc = iclass_crc16((char *)blockno,1);
1775 read[3] = crc & 0xff;
1776 while(!read_success){
1777 ReaderTransmitIClass(read, sizeof(read));
1778 if(ReaderReceiveIClass(resp) == 10) {
1781 memory.k16= (mem & 0x80);
1782 memory.book= (mem & 0x20);
1783 memory.k2= (mem & 0x8);
1784 memory.lockauth= (mem & 0x2);
1785 memory.keyaccess= (mem & 0x1);
1794 memcpy(write+1,blockNo,1);
1795 memcpy(write+2,data,8);
1796 memcpy(write+10,mac,4);
1797 while(!send_success){
1798 ReaderTransmitIClass(write, sizeof(write));
1799 if(ReaderReceiveIClass(resp) == 10) {