1 //-----------------------------------------------------------------------------
2 // Gerhard de Koning Gans - May 2008
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Routines to support ISO 14443 type A.
9 //-----------------------------------------------------------------------------
11 #include "proxmark3.h"
16 #include "iso14443crc.h"
18 static uint8_t *trace
= (uint8_t *) BigBuf
;
19 static int traceLen
= 0;
20 static int rsamples
= 0;
21 static int tracing
= TRUE
;
24 // Sequence D: 11110000 modulation with subcarrier during first half
25 // Sequence E: 00001111 modulation with subcarrier during second half
26 // Sequence F: 00000000 no modulation with subcarrier
28 // Sequence X: 00001100 drop after half a period
29 // Sequence Y: 00000000 no drop
30 // Sequence Z: 11000000 drop at start
38 static const uint8_t OddByteParity
[256] = {
39 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
40 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
41 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
42 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
43 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
44 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
45 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
46 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
47 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
48 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
49 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
50 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
51 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
52 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
53 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
54 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
57 // BIG CHANGE - UNDERSTAND THIS BEFORE WE COMMIT
58 #define RECV_CMD_OFFSET 3032
59 #define RECV_RES_OFFSET 3096
60 #define DMA_BUFFER_OFFSET 3160
61 #define DMA_BUFFER_SIZE 4096
62 #define TRACE_LENGTH 3000
64 //-----------------------------------------------------------------------------
65 // Generate the parity value for a byte sequence
67 //-----------------------------------------------------------------------------
68 uint32_t GetParity(const uint8_t * pbtCmd
, int iLen
)
73 // Generate the encrypted data
74 for (i
= 0; i
< iLen
; i
++) {
75 // Save the encrypted parity bit
76 dwPar
|= ((OddByteParity
[pbtCmd
[i
]]) << i
);
81 static void AppendCrc14443a(uint8_t* data
, int len
)
83 ComputeCrc14443(CRC_14443_A
,data
,len
,data
+len
,data
+len
+1);
86 int LogTrace(const uint8_t * btBytes
, int iLen
, int iSamples
, uint32_t dwParity
, int bReader
)
88 // Return when trace is full
89 if (traceLen
>= TRACE_LENGTH
) return FALSE
;
91 // Trace the random, i'm curious
93 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
94 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
95 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
96 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
98 trace
[traceLen
- 1] |= 0x80;
100 trace
[traceLen
++] = ((dwParity
>> 0) & 0xff);
101 trace
[traceLen
++] = ((dwParity
>> 8) & 0xff);
102 trace
[traceLen
++] = ((dwParity
>> 16) & 0xff);
103 trace
[traceLen
++] = ((dwParity
>> 24) & 0xff);
104 trace
[traceLen
++] = iLen
;
105 memcpy(trace
+ traceLen
, btBytes
, iLen
);
110 //-----------------------------------------------------------------------------
111 // The software UART that receives commands from the reader, and its state
113 //-----------------------------------------------------------------------------
117 STATE_START_OF_COMMUNICATION
,
141 static int MillerDecoding(int bit
)
146 if(!Uart
.bitBuffer
) {
147 Uart
.bitBuffer
= bit
^ 0xFF0;
151 Uart
.bitBuffer
<<= 4;
152 Uart
.bitBuffer
^= bit
;
157 if(Uart
.state
!= STATE_UNSYNCD
) {
160 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
166 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
172 if(bit
!= bitright
) { bit
= bitright
; }
174 if(Uart
.posCnt
== 1) {
175 // measurement first half bitperiod
177 Uart
.drop
= DROP_FIRST_HALF
;
181 // measurement second half bitperiod
182 if(!bit
& (Uart
.drop
== DROP_NONE
)) {
183 Uart
.drop
= DROP_SECOND_HALF
;
186 // measured a drop in first and second half
187 // which should not be possible
188 Uart
.state
= STATE_ERROR_WAIT
;
195 case STATE_START_OF_COMMUNICATION
:
197 if(Uart
.drop
== DROP_SECOND_HALF
) {
198 // error, should not happen in SOC
199 Uart
.state
= STATE_ERROR_WAIT
;
204 Uart
.state
= STATE_MILLER_Z
;
211 if(Uart
.drop
== DROP_NONE
) {
212 // logic '0' followed by sequence Y
213 // end of communication
214 Uart
.state
= STATE_UNSYNCD
;
217 // if(Uart.drop == DROP_FIRST_HALF) {
218 // Uart.state = STATE_MILLER_Z; stay the same
219 // we see a logic '0' }
220 if(Uart
.drop
== DROP_SECOND_HALF
) {
221 // we see a logic '1'
222 Uart
.shiftReg
|= 0x100;
223 Uart
.state
= STATE_MILLER_X
;
229 if(Uart
.drop
== DROP_NONE
) {
230 // sequence Y, we see a '0'
231 Uart
.state
= STATE_MILLER_Y
;
234 if(Uart
.drop
== DROP_FIRST_HALF
) {
235 // Would be STATE_MILLER_Z
236 // but Z does not follow X, so error
237 Uart
.state
= STATE_ERROR_WAIT
;
240 if(Uart
.drop
== DROP_SECOND_HALF
) {
241 // We see a '1' and stay in state X
242 Uart
.shiftReg
|= 0x100;
250 if(Uart
.drop
== DROP_NONE
) {
251 // logic '0' followed by sequence Y
252 // end of communication
253 Uart
.state
= STATE_UNSYNCD
;
256 if(Uart
.drop
== DROP_FIRST_HALF
) {
258 Uart
.state
= STATE_MILLER_Z
;
260 if(Uart
.drop
== DROP_SECOND_HALF
) {
261 // We see a '1' and go to state X
262 Uart
.shiftReg
|= 0x100;
263 Uart
.state
= STATE_MILLER_X
;
267 case STATE_ERROR_WAIT
:
268 // That went wrong. Now wait for at least two bit periods
269 // and try to sync again
270 if(Uart
.drop
== DROP_NONE
) {
272 Uart
.state
= STATE_UNSYNCD
;
277 Uart
.state
= STATE_UNSYNCD
;
282 Uart
.drop
= DROP_NONE
;
284 // should have received at least one whole byte...
285 if((Uart
.bitCnt
== 2) && EOC
&& (Uart
.byteCnt
> 0)) {
289 if(Uart
.bitCnt
== 9) {
290 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
293 Uart
.parityBits
<<= 1;
294 Uart
.parityBits
^= ((Uart
.shiftReg
>> 8) & 0x01);
297 // when End of Communication received and
298 // all data bits processed..
305 Uart.output[Uart.byteCnt] = 0xAA;
307 Uart.output[Uart.byteCnt] = error & 0xFF;
309 Uart.output[Uart.byteCnt] = 0xAA;
311 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
313 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
315 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
317 Uart.output[Uart.byteCnt] = 0xAA;
325 bit
= Uart
.bitBuffer
& 0xf0;
329 // should have been high or at least (4 * 128) / fc
330 // according to ISO this should be at least (9 * 128 + 20) / fc
331 if(Uart
.highCnt
== 8) {
332 // we went low, so this could be start of communication
333 // it turns out to be safer to choose a less significant
334 // syncbit... so we check whether the neighbour also represents the drop
335 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
336 Uart
.syncBit
= bit
& 8;
338 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
339 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
340 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
341 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
342 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
343 if(Uart
.syncBit
& (Uart
.bitBuffer
& 8)) {
346 // the first half bit period is expected in next sample
351 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
354 Uart
.state
= STATE_START_OF_COMMUNICATION
;
355 Uart
.drop
= DROP_FIRST_HALF
;
366 if(Uart
.highCnt
< 8) {
375 //=============================================================================
376 // ISO 14443 Type A - Manchester
377 //=============================================================================
382 DEMOD_START_OF_COMMUNICATION
,
405 static int ManchesterDecoding(int v
)
421 if(Demod
.state
==DEMOD_UNSYNCD
) {
422 Demod
.output
[Demod
.len
] = 0xfa;
425 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
426 if(bit
& 0x08) { Demod
.syncBit
= 0x08; }
428 if(bit
& 0x04) { Demod
.syncBit
= 0x04; }
430 else if(bit
& 0x04) { Demod
.syncBit
= 0x04; bit
<<= 4; }
432 if(bit
& 0x02) { Demod
.syncBit
= 0x02; }
434 else if(bit
& 0x02) { Demod
.syncBit
= 0x02; bit
<<= 4; }
436 if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
438 if(Demod
.syncBit
& (Demod
.buffer
& 0x08)) {
439 Demod
.syncBit
= 0x08;
441 // The first half bitperiod is expected in next sample
443 Demod
.output
[Demod
.len
] = 0xfb;
446 else if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
450 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
451 Demod
.sub
= SUB_FIRST_HALF
;
454 Demod
.parityBits
= 0;
457 switch(Demod
.syncBit
) {
458 case 0x08: Demod
.samples
= 3; break;
459 case 0x04: Demod
.samples
= 2; break;
460 case 0x02: Demod
.samples
= 1; break;
461 case 0x01: Demod
.samples
= 0; break;
468 //modulation = bit & Demod.syncBit;
469 modulation
= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
473 if(Demod
.posCount
==0) {
476 Demod
.sub
= SUB_FIRST_HALF
;
479 Demod
.sub
= SUB_NONE
;
484 if(modulation
&& (Demod
.sub
== SUB_FIRST_HALF
)) {
485 if(Demod
.state
!=DEMOD_ERROR_WAIT
) {
486 Demod
.state
= DEMOD_ERROR_WAIT
;
487 Demod
.output
[Demod
.len
] = 0xaa;
491 else if(modulation
) {
492 Demod
.sub
= SUB_SECOND_HALF
;
495 switch(Demod
.state
) {
496 case DEMOD_START_OF_COMMUNICATION
:
497 if(Demod
.sub
== SUB_FIRST_HALF
) {
498 Demod
.state
= DEMOD_MANCHESTER_D
;
501 Demod
.output
[Demod
.len
] = 0xab;
502 Demod
.state
= DEMOD_ERROR_WAIT
;
507 case DEMOD_MANCHESTER_D
:
508 case DEMOD_MANCHESTER_E
:
509 if(Demod
.sub
== SUB_FIRST_HALF
) {
511 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
512 Demod
.state
= DEMOD_MANCHESTER_D
;
514 else if(Demod
.sub
== SUB_SECOND_HALF
) {
516 Demod
.shiftReg
>>= 1;
517 Demod
.state
= DEMOD_MANCHESTER_E
;
520 Demod
.state
= DEMOD_MANCHESTER_F
;
524 case DEMOD_MANCHESTER_F
:
525 // Tag response does not need to be a complete byte!
526 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
527 if(Demod
.bitCount
> 0) {
528 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
529 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
531 // No parity bit, so just shift a 0
532 Demod
.parityBits
<<= 1;
535 Demod
.state
= DEMOD_UNSYNCD
;
539 Demod
.output
[Demod
.len
] = 0xad;
540 Demod
.state
= DEMOD_ERROR_WAIT
;
545 case DEMOD_ERROR_WAIT
:
546 Demod
.state
= DEMOD_UNSYNCD
;
550 Demod
.output
[Demod
.len
] = 0xdd;
551 Demod
.state
= DEMOD_UNSYNCD
;
555 if(Demod
.bitCount
>=9) {
556 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
559 Demod
.parityBits
<<= 1;
560 Demod
.parityBits
^= ((Demod
.shiftReg
>> 8) & 0x01);
567 Demod.output[Demod.len] = 0xBB;
569 Demod.output[Demod.len] = error & 0xFF;
571 Demod.output[Demod.len] = 0xBB;
573 Demod.output[Demod.len] = bit & 0xFF;
575 Demod.output[Demod.len] = Demod.buffer & 0xFF;
577 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
579 Demod.output[Demod.len] = 0xBB;
586 } // end (state != UNSYNCED)
591 //=============================================================================
592 // Finally, a `sniffer' for ISO 14443 Type A
593 // Both sides of communication!
594 //=============================================================================
596 //-----------------------------------------------------------------------------
597 // Record the sequence of commands sent by the reader to the tag, with
598 // triggering so that we start recording at the point that the tag is moved
600 //-----------------------------------------------------------------------------
601 void SnoopIso14443a(void)
603 // #define RECV_CMD_OFFSET 2032 // original (working as of 21/2/09) values
604 // #define RECV_RES_OFFSET 2096 // original (working as of 21/2/09) values
605 // #define DMA_BUFFER_OFFSET 2160 // original (working as of 21/2/09) values
606 // #define DMA_BUFFER_SIZE 4096 // original (working as of 21/2/09) values
607 // #define TRACE_LENGTH 2000 // original (working as of 21/2/09) values
609 // We won't start recording the frames that we acquire until we trigger;
610 // a good trigger condition to get started is probably when we see a
611 // response from the tag.
612 int triggered
= TRUE
; // FALSE to wait first for card
614 // The command (reader -> tag) that we're receiving.
615 // The length of a received command will in most cases be no more than 18 bytes.
616 // So 32 should be enough!
617 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
618 // The response (tag -> reader) that we're receiving.
619 uint8_t *receivedResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
621 // As we receive stuff, we copy it from receivedCmd or receivedResponse
622 // into trace, along with its length and other annotations.
623 //uint8_t *trace = (uint8_t *)BigBuf;
626 // The DMA buffer, used to stream samples from the FPGA
627 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
633 // Count of samples received so far, so that we can include timing
634 // information in the trace buffer.
638 memset(trace
, 0x44, RECV_CMD_OFFSET
);
640 // Set up the demodulator for tag -> reader responses.
641 Demod
.output
= receivedResponse
;
643 Demod
.state
= DEMOD_UNSYNCD
;
645 // And the reader -> tag commands
646 memset(&Uart
, 0, sizeof(Uart
));
647 Uart
.output
= receivedCmd
;
648 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
649 Uart
.state
= STATE_UNSYNCD
;
651 // And put the FPGA in the appropriate mode
652 // Signal field is off with the appropriate LED
654 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
655 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
657 // Setup for the DMA.
660 lastRxCounter
= DMA_BUFFER_SIZE
;
661 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
665 // And now we loop, receiving samples.
668 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
670 if(behindBy
> maxBehindBy
) {
671 maxBehindBy
= behindBy
;
673 DbpString("blew circular buffer!");
677 if(behindBy
< 1) continue;
682 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
683 upTo
-= DMA_BUFFER_SIZE
;
684 lastRxCounter
+= DMA_BUFFER_SIZE
;
685 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
686 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
690 if(MillerDecoding((smpl
& 0xF0) >> 4)) {
691 rsamples
= samples
- Uart
.samples
;
694 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
695 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
696 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
697 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
698 trace
[traceLen
++] = ((Uart
.parityBits
>> 0) & 0xff);
699 trace
[traceLen
++] = ((Uart
.parityBits
>> 8) & 0xff);
700 trace
[traceLen
++] = ((Uart
.parityBits
>> 16) & 0xff);
701 trace
[traceLen
++] = ((Uart
.parityBits
>> 24) & 0xff);
702 trace
[traceLen
++] = Uart
.byteCnt
;
703 memcpy(trace
+traceLen
, receivedCmd
, Uart
.byteCnt
);
704 traceLen
+= Uart
.byteCnt
;
705 if(traceLen
> TRACE_LENGTH
) break;
707 /* And ready to receive another command. */
708 Uart
.state
= STATE_UNSYNCD
;
709 /* And also reset the demod code, which might have been */
710 /* false-triggered by the commands from the reader. */
711 Demod
.state
= DEMOD_UNSYNCD
;
714 if(ManchesterDecoding(smpl
& 0x0F)) {
715 rsamples
= samples
- Demod
.samples
;
718 // timestamp, as a count of samples
719 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
720 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
721 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
722 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
723 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
724 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
725 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
726 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
728 trace
[traceLen
++] = Demod
.len
;
729 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
730 traceLen
+= Demod
.len
;
731 if(traceLen
> TRACE_LENGTH
) break;
735 // And ready to receive another response.
736 memset(&Demod
, 0, sizeof(Demod
));
737 Demod
.output
= receivedResponse
;
738 Demod
.state
= DEMOD_UNSYNCD
;
743 DbpString("cancelled_a");
748 DbpString("COMMAND FINISHED");
750 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
751 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
754 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
755 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
756 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
763 //-----------------------------------------------------------------------------
764 // Prepare tag messages
765 //-----------------------------------------------------------------------------
766 static void CodeIso14443aAsTag(const uint8_t *cmd
, int len
)
773 // Correction bit, might be removed when not needed
778 ToSendStuffBit(1); // 1
784 ToSend
[++ToSendMax
] = SEC_D
;
786 for(i
= 0; i
< len
; i
++) {
792 for(j
= 0; j
< 8; j
++) {
793 oddparity
^= (b
& 1);
795 ToSend
[++ToSendMax
] = SEC_D
;
797 ToSend
[++ToSendMax
] = SEC_E
;
804 ToSend
[++ToSendMax
] = SEC_D
;
806 ToSend
[++ToSendMax
] = SEC_E
;
811 ToSend
[++ToSendMax
] = SEC_F
;
813 // Flush the buffer in FPGA!!
814 for(i
= 0; i
< 5; i
++) {
815 ToSend
[++ToSendMax
] = SEC_F
;
818 // Convert from last byte pos to length
821 // Add a few more for slop
822 ToSend
[ToSendMax
++] = 0x00;
823 ToSend
[ToSendMax
++] = 0x00;
827 //-----------------------------------------------------------------------------
828 // This is to send a NACK kind of answer, its only 3 bits, I know it should be 4
829 //-----------------------------------------------------------------------------
830 static void CodeStrangeAnswer()
836 // Correction bit, might be removed when not needed
841 ToSendStuffBit(1); // 1
847 ToSend
[++ToSendMax
] = SEC_D
;
850 ToSend
[++ToSendMax
] = SEC_E
;
853 ToSend
[++ToSendMax
] = SEC_E
;
856 ToSend
[++ToSendMax
] = SEC_D
;
859 ToSend
[++ToSendMax
] = SEC_F
;
861 // Flush the buffer in FPGA!!
862 for(i
= 0; i
< 5; i
++) {
863 ToSend
[++ToSendMax
] = SEC_F
;
866 // Convert from last byte pos to length
869 // Add a few more for slop
870 ToSend
[ToSendMax
++] = 0x00;
871 ToSend
[ToSendMax
++] = 0x00;
875 //-----------------------------------------------------------------------------
876 // Wait for commands from reader
877 // Stop when button is pressed
878 // Or return TRUE when command is captured
879 //-----------------------------------------------------------------------------
880 static int GetIso14443aCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
882 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
883 // only, since we are receiving, not transmitting).
884 // Signal field is off with the appropriate LED
886 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
888 // Now run a `software UART' on the stream of incoming samples.
889 Uart
.output
= received
;
890 Uart
.byteCntMax
= maxLen
;
891 Uart
.state
= STATE_UNSYNCD
;
896 if(BUTTON_PRESS()) return FALSE
;
898 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
899 AT91C_BASE_SSC
->SSC_THR
= 0x00;
901 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
902 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
903 if(MillerDecoding((b
& 0xf0) >> 4)) {
907 if(MillerDecoding(b
& 0x0f)) {
915 //-----------------------------------------------------------------------------
916 // Main loop of simulated tag: receive commands from reader, decide what
917 // response to send, and send it.
918 //-----------------------------------------------------------------------------
919 void SimulateIso14443aTag(int tagType
, int TagUid
)
921 // This function contains the tag emulation
923 // Prepare protocol messages
924 // static const uint8_t cmd1[] = { 0x26 };
925 // static const uint8_t response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg
927 static const uint8_t response1
[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me
928 // static const uint8_t response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me
931 // static const uint8_t cmd2[] = { 0x93, 0x20 };
932 //static const uint8_t response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg
935 static const uint8_t response2
[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips
938 // When reader selects us during cascade1 it will send cmd3
939 //uint8_t response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE)
940 uint8_t response3
[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire)
941 ComputeCrc14443(CRC_14443_A
, response3
, 1, &response3
[1], &response3
[2]);
943 // send cascade2 2nd half of UID
944 static const uint8_t response2a
[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck
945 // NOTE : THE CRC on the above may be wrong as I have obfuscated the actual UID
947 // When reader selects us during cascade2 it will send cmd3a
948 //uint8_t response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE)
949 uint8_t response3a
[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire)
950 ComputeCrc14443(CRC_14443_A
, response3a
, 1, &response3a
[1], &response3a
[2]);
952 static const uint8_t response5
[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce
957 // Longest possible response will be 16 bytes + 2 CRC = 18 bytes
959 // 144 data bits (18 * 8)
962 // 1 Correction bit (Answer in 1172 or 1236 periods, see FPGA)
963 // 1 just for the case
967 // 166 bytes, since every bit that needs to be send costs us a byte
970 // Respond with card type
971 uint8_t *resp1
= (((uint8_t *)BigBuf
) + 800);
974 // Anticollision cascade1 - respond with uid
975 uint8_t *resp2
= (((uint8_t *)BigBuf
) + 970);
978 // Anticollision cascade2 - respond with 2nd half of uid if asked
979 // we're only going to be asked if we set the 1st byte of the UID (during cascade1) to 0x88
980 uint8_t *resp2a
= (((uint8_t *)BigBuf
) + 1140);
983 // Acknowledge select - cascade 1
984 uint8_t *resp3
= (((uint8_t *)BigBuf
) + 1310);
987 // Acknowledge select - cascade 2
988 uint8_t *resp3a
= (((uint8_t *)BigBuf
) + 1480);
991 // Response to a read request - not implemented atm
992 uint8_t *resp4
= (((uint8_t *)BigBuf
) + 1550);
995 // Authenticate response - nonce
996 uint8_t *resp5
= (((uint8_t *)BigBuf
) + 1720);
999 uint8_t *receivedCmd
= (uint8_t *)BigBuf
;
1006 // To control where we are in the protocol
1010 // Just to allow some checks
1018 memset(receivedCmd
, 0x44, 400);
1020 // Prepare the responses of the anticollision phase
1021 // there will be not enough time to do this at the moment the reader sends it REQA
1023 // Answer to request
1024 CodeIso14443aAsTag(response1
, sizeof(response1
));
1025 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1027 // Send our UID (cascade 1)
1028 CodeIso14443aAsTag(response2
, sizeof(response2
));
1029 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1031 // Answer to select (cascade1)
1032 CodeIso14443aAsTag(response3
, sizeof(response3
));
1033 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1035 // Send the cascade 2 2nd part of the uid
1036 CodeIso14443aAsTag(response2a
, sizeof(response2a
));
1037 memcpy(resp2a
, ToSend
, ToSendMax
); resp2aLen
= ToSendMax
;
1039 // Answer to select (cascade 2)
1040 CodeIso14443aAsTag(response3a
, sizeof(response3a
));
1041 memcpy(resp3a
, ToSend
, ToSendMax
); resp3aLen
= ToSendMax
;
1043 // Strange answer is an example of rare message size (3 bits)
1044 CodeStrangeAnswer();
1045 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1047 // Authentication answer (random nonce)
1048 CodeIso14443aAsTag(response5
, sizeof(response5
));
1049 memcpy(resp5
, ToSend
, ToSendMax
); resp5Len
= ToSendMax
;
1051 // We need to listen to the high-frequency, peak-detected path.
1052 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1060 if(!GetIso14443aCommandFromReader(receivedCmd
, &len
, 100)) {
1061 DbpString("button press");
1064 // doob - added loads of debug strings so we can see what the reader is saying to us during the sim as hi14alist is not populated
1065 // Okay, look at the command now.
1067 i
= 1; // first byte transmitted
1068 if(receivedCmd
[0] == 0x26) {
1069 // Received a REQUEST
1070 resp
= resp1
; respLen
= resp1Len
; order
= 1;
1071 //DbpString("Hello request from reader:");
1072 } else if(receivedCmd
[0] == 0x52) {
1073 // Received a WAKEUP
1074 resp
= resp1
; respLen
= resp1Len
; order
= 6;
1075 // //DbpString("Wakeup request from reader:");
1077 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] == 0x93) { // greg - cascade 1 anti-collision
1078 // Received request for UID (cascade 1)
1079 resp
= resp2
; respLen
= resp2Len
; order
= 2;
1080 // DbpString("UID (cascade 1) request from reader:");
1081 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1084 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] ==0x95) { // greg - cascade 2 anti-collision
1085 // Received request for UID (cascade 2)
1086 resp
= resp2a
; respLen
= resp2aLen
; order
= 20;
1087 // DbpString("UID (cascade 2) request from reader:");
1088 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1091 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x93) { // greg - cascade 1 select
1092 // Received a SELECT
1093 resp
= resp3
; respLen
= resp3Len
; order
= 3;
1094 // DbpString("Select (cascade 1) request from reader:");
1095 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1098 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x95) { // greg - cascade 2 select
1099 // Received a SELECT
1100 resp
= resp3a
; respLen
= resp3aLen
; order
= 30;
1101 // DbpString("Select (cascade 2) request from reader:");
1102 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1105 } else if(receivedCmd
[0] == 0x30) {
1107 resp
= resp4
; respLen
= resp4Len
; order
= 4; // Do nothing
1108 Dbprintf("Read request from reader: %x %x %x",
1109 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1112 } else if(receivedCmd
[0] == 0x50) {
1114 resp
= resp1
; respLen
= 0; order
= 5; // Do nothing
1115 DbpString("Reader requested we HALT!:");
1117 } else if(receivedCmd
[0] == 0x60) {
1118 // Received an authentication request
1119 resp
= resp5
; respLen
= resp5Len
; order
= 7;
1120 Dbprintf("Authenticate request from reader: %x %x %x",
1121 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1123 } else if(receivedCmd
[0] == 0xE0) {
1124 // Received a RATS request
1125 resp
= resp1
; respLen
= 0;order
= 70;
1126 Dbprintf("RATS request from reader: %x %x %x",
1127 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1129 // Never seen this command before
1130 Dbprintf("Unknown command received from reader: %x %x %x %x %x %x %x %x %x",
1131 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1132 receivedCmd
[3], receivedCmd
[3], receivedCmd
[4],
1133 receivedCmd
[5], receivedCmd
[6], receivedCmd
[7]);
1135 resp
= resp1
; respLen
= 0; order
= 0;
1138 // Count number of wakeups received after a halt
1139 if(order
== 6 && lastorder
== 5) { happened
++; }
1141 // Count number of other messages after a halt
1142 if(order
!= 6 && lastorder
== 5) { happened2
++; }
1144 // Look at last parity bit to determine timing of answer
1145 if((Uart
.parityBits
& 0x01) || receivedCmd
[0] == 0x52) {
1146 // 1236, so correction bit needed
1150 memset(receivedCmd
, 0x44, 32);
1152 if(cmdsRecvd
> 999) {
1153 DbpString("1000 commands later...");
1160 if(respLen
<= 0) continue;
1162 // Modulate Manchester
1163 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_MOD
);
1164 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1167 // ### Transmit the response ###
1170 fdt_indicator
= FALSE
;
1172 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1173 volatile uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1176 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1184 AT91C_BASE_SSC
->SSC_THR
= b
;
1190 if(BUTTON_PRESS()) {
1197 Dbprintf("%x %x %x", happened
, happened2
, cmdsRecvd
);
1201 //-----------------------------------------------------------------------------
1202 // Transmit the command (to the tag) that was placed in ToSend[].
1203 //-----------------------------------------------------------------------------
1204 static void TransmitFor14443a(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1208 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1214 for(c
= 0; c
< *wait
;) {
1215 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1216 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1219 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1220 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1228 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1229 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
1235 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1236 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1241 if (samples
) *samples
= (c
+ *wait
) << 3;
1244 //-----------------------------------------------------------------------------
1245 // Code a 7-bit command without parity bit
1246 // This is especially for 0x26 and 0x52 (REQA and WUPA)
1247 //-----------------------------------------------------------------------------
1248 void ShortFrameFromReader(const uint8_t bt
)
1256 // Start of Communication (Seq. Z)
1257 ToSend
[++ToSendMax
] = SEC_Z
;
1261 for(j
= 0; j
< 7; j
++) {
1264 ToSend
[++ToSendMax
] = SEC_X
;
1269 ToSend
[++ToSendMax
] = SEC_Z
;
1273 ToSend
[++ToSendMax
] = SEC_Y
;
1280 // End of Communication
1283 ToSend
[++ToSendMax
] = SEC_Z
;
1287 ToSend
[++ToSendMax
] = SEC_Y
;
1291 ToSend
[++ToSendMax
] = SEC_Y
;
1294 ToSend
[++ToSendMax
] = SEC_Y
;
1295 ToSend
[++ToSendMax
] = SEC_Y
;
1296 ToSend
[++ToSendMax
] = SEC_Y
;
1298 // Convert from last character reference to length
1302 //-----------------------------------------------------------------------------
1303 // Prepare reader command to send to FPGA
1305 //-----------------------------------------------------------------------------
1306 void CodeIso14443aAsReaderPar(const uint8_t * cmd
, int len
, uint32_t dwParity
)
1314 // Start of Communication (Seq. Z)
1315 ToSend
[++ToSendMax
] = SEC_Z
;
1318 // Generate send structure for the data bits
1319 for (i
= 0; i
< len
; i
++) {
1320 // Get the current byte to send
1323 for (j
= 0; j
< 8; j
++) {
1326 ToSend
[++ToSendMax
] = SEC_X
;
1331 ToSend
[++ToSendMax
] = SEC_Z
;
1334 ToSend
[++ToSendMax
] = SEC_Y
;
1341 // Get the parity bit
1342 if ((dwParity
>> i
) & 0x01) {
1344 ToSend
[++ToSendMax
] = SEC_X
;
1349 ToSend
[++ToSendMax
] = SEC_Z
;
1352 ToSend
[++ToSendMax
] = SEC_Y
;
1358 // End of Communication
1361 ToSend
[++ToSendMax
] = SEC_Z
;
1364 ToSend
[++ToSendMax
] = SEC_Y
;
1368 ToSend
[++ToSendMax
] = SEC_Y
;
1371 ToSend
[++ToSendMax
] = SEC_Y
;
1372 ToSend
[++ToSendMax
] = SEC_Y
;
1373 ToSend
[++ToSendMax
] = SEC_Y
;
1375 // Convert from last character reference to length
1379 //-----------------------------------------------------------------------------
1380 // Wait a certain time for tag response
1381 // If a response is captured return TRUE
1382 // If it takes to long return FALSE
1383 //-----------------------------------------------------------------------------
1384 static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1386 // buffer needs to be 512 bytes
1389 // Set FPGA mode to "reader listen mode", no modulation (listen
1390 // only, since we are receiving, not transmitting).
1391 // Signal field is on with the appropriate LED
1393 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1395 // Now get the answer from the card
1396 Demod
.output
= receivedResponse
;
1398 Demod
.state
= DEMOD_UNSYNCD
;
1401 if (elapsed
) *elapsed
= 0;
1407 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1408 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1409 if (elapsed
) (*elapsed
)++;
1411 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1412 if(c
< 512) { c
++; } else { return FALSE
; }
1413 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1414 if(ManchesterDecoding((b
>>4) & 0xf)) {
1415 *samples
= ((c
- 1) << 3) + 4;
1418 if(ManchesterDecoding(b
& 0x0f)) {
1426 void ReaderTransmitShort(const uint8_t* bt
)
1431 ShortFrameFromReader(*bt
);
1434 TransmitFor14443a(ToSend
, ToSendMax
, &samples
, &wait
);
1436 // Store reader command in buffer
1437 if (tracing
) LogTrace(bt
,1,0,GetParity(bt
,1),TRUE
);
1440 void ReaderTransmitPar(uint8_t* frame
, int len
, uint32_t par
)
1445 // This is tied to other size changes
1446 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1447 CodeIso14443aAsReaderPar(frame
,len
,par
);
1450 TransmitFor14443a(ToSend
, ToSendMax
, &samples
, &wait
);
1452 // Store reader command in buffer
1453 if (tracing
) LogTrace(frame
,len
,0,par
,TRUE
);
1457 void ReaderTransmit(uint8_t* frame
, int len
)
1459 // Generate parity and redirect
1460 ReaderTransmitPar(frame
,len
,GetParity(frame
,len
));
1463 int ReaderReceive(uint8_t* receivedAnswer
)
1466 if (!GetIso14443aAnswerFromTag(receivedAnswer
,100,&samples
,0)) return FALSE
;
1467 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
);
1471 //-----------------------------------------------------------------------------
1472 // Read an ISO 14443a tag. Send out commands and store answers.
1474 //-----------------------------------------------------------------------------
1475 void ReaderIso14443a(uint32_t parameter
)
1478 uint8_t wupa
[] = { 0x52 };
1479 uint8_t sel_all
[] = { 0x93,0x20 };
1480 uint8_t sel_uid
[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1481 uint8_t sel_all_c2
[] = { 0x95,0x20 };
1482 uint8_t sel_uid_c2
[] = { 0x95,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1485 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1486 // uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00 };
1488 uint8_t* receivedAnswer
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1494 // Start from off (no field generated)
1495 // Signal field is off with the appropriate LED
1497 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1500 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1502 // Now give it time to spin up.
1503 // Signal field is on with the appropriate LED
1505 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1512 while(traceLen
< TRACE_LENGTH
)
1514 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1515 ReaderTransmitShort(wupa
);
1517 // Test if the action was cancelled
1518 if(BUTTON_PRESS()) {
1523 if (!ReaderReceive(receivedAnswer
)) continue;
1525 // Transmit SELECT_ALL
1526 ReaderTransmit(sel_all
,sizeof(sel_all
));
1529 if (!ReaderReceive(receivedAnswer
)) continue;
1531 // Construct SELECT UID command
1532 // First copy the 5 bytes (Mifare Classic) after the 93 70
1533 memcpy(sel_uid
+2,receivedAnswer
,5);
1534 // Secondly compute the two CRC bytes at the end
1535 AppendCrc14443a(sel_uid
,7);
1537 // Transmit SELECT_UID
1538 ReaderTransmit(sel_uid
,sizeof(sel_uid
));
1541 if (!ReaderReceive(receivedAnswer
)) continue;
1543 // OK we have selected at least at cascade 1, lets see if first byte of UID was 0x88 in
1544 // which case we need to make a cascade 2 request and select - this is a long UID
1545 // When the UID is not complete, the 3nd bit (from the right) is set in the SAK.
1546 if (receivedAnswer
[0] &= 0x04)
1548 // Transmit SELECT_ALL
1549 ReaderTransmit(sel_all_c2
,sizeof(sel_all_c2
));
1552 if (!ReaderReceive(receivedAnswer
)) continue;
1554 // Construct SELECT UID command
1555 memcpy(sel_uid_c2
+2,receivedAnswer
,5);
1556 // Secondly compute the two CRC bytes at the end
1557 AppendCrc14443a(sel_uid_c2
,7);
1559 // Transmit SELECT_UID
1560 ReaderTransmit(sel_uid_c2
,sizeof(sel_uid_c2
));
1563 if (!ReaderReceive(receivedAnswer
)) continue;
1566 // Transmit MIFARE_CLASSIC_AUTH
1567 ReaderTransmit(mf_auth
,sizeof(mf_auth
));
1569 // Receive the (16 bit) "random" nonce
1570 if (!ReaderReceive(receivedAnswer
)) continue;
1574 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1576 Dbprintf("%x %x %x", rsamples
, 0xCC, 0xCC);
1577 DbpString("ready..");
1580 //-----------------------------------------------------------------------------
1581 // Read an ISO 14443a tag. Send out commands and store answers.
1583 //-----------------------------------------------------------------------------
1584 void ReaderMifare(uint32_t parameter
)
1588 uint8_t wupa
[] = { 0x52 };
1589 uint8_t sel_all
[] = { 0x93,0x20 };
1590 uint8_t sel_uid
[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1593 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1594 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1596 uint8_t* receivedAnswer
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1603 // Start from off (no field generated)
1604 // Signal field is off with the appropriate LED
1606 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1609 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1611 // Now give it time to spin up.
1612 // Signal field is on with the appropriate LED
1614 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1621 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1622 ReaderTransmitShort(wupa
);
1624 ReaderReceive(receivedAnswer
);
1625 // Transmit SELECT_ALL
1626 ReaderTransmit(sel_all
,sizeof(sel_all
));
1628 ReaderReceive(receivedAnswer
);
1629 // Construct SELECT UID command
1630 // First copy the 5 bytes (Mifare Classic) after the 93 70
1631 memcpy(sel_uid
+2,receivedAnswer
,5);
1632 // Secondly compute the two CRC bytes at the end
1633 AppendCrc14443a(sel_uid
,7);
1638 byte_t par_mask
= 0xff;
1644 byte_t nt_attacked
[4];
1647 num_to_bytes(parameter
,4,nt_attacked
);
1651 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1653 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1655 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1656 ReaderTransmitShort(wupa
);
1658 // Test if the action was cancelled
1659 if(BUTTON_PRESS()) {
1664 if (!ReaderReceive(receivedAnswer
)) continue;
1666 // Transmit SELECT_ALL
1667 ReaderTransmit(sel_all
,sizeof(sel_all
));
1670 if (!ReaderReceive(receivedAnswer
)) continue;
1672 // Transmit SELECT_UID
1673 ReaderTransmit(sel_uid
,sizeof(sel_uid
));
1676 if (!ReaderReceive(receivedAnswer
)) continue;
1678 // Transmit MIFARE_CLASSIC_AUTH
1679 ReaderTransmit(mf_auth
,sizeof(mf_auth
));
1681 // Receive the (16 bit) "random" nonce
1682 if (!ReaderReceive(receivedAnswer
)) continue;
1683 memcpy(nt
,receivedAnswer
,4);
1685 // Transmit reader nonce and reader answer
1686 ReaderTransmitPar(mf_nr_ar
,sizeof(mf_nr_ar
),par
);
1688 // Receive 4 bit answer
1689 if (ReaderReceive(receivedAnswer
))
1694 memcpy(nt_attacked
,nt
,4);
1696 par_low
= par
& 0x07;
1699 if (memcmp(nt
,nt_attacked
,4) != 0) continue;
1702 if(led_on
) LED_B_ON(); else LED_B_OFF();
1703 par_list
[nt_diff
] = par
;
1704 ks_list
[nt_diff
] = receivedAnswer
[0]^0x05;
1706 // Test if the information is complete
1707 if (nt_diff
== 0x07) break;
1709 nt_diff
= (nt_diff
+1) & 0x07;
1710 mf_nr_ar
[3] = nt_diff
<< 5;
1717 par
= (((par
>>3)+1) << 3) | par_low
;
1722 LogTrace(sel_uid
+2,4,0,GetParity(sel_uid
+2,4),TRUE
);
1723 LogTrace(nt
,4,0,GetParity(nt
,4),TRUE
);
1724 LogTrace(par_list
,8,0,GetParity(par_list
,8),TRUE
);
1725 LogTrace(ks_list
,8,0,GetParity(ks_list
,8),TRUE
);
1728 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);