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
;
32 static const uint8_t OddByteParity
[256] = {
33 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
34 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
35 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
36 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
37 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
38 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
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 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
44 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
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
51 // BIG CHANGE - UNDERSTAND THIS BEFORE WE COMMIT
52 #define RECV_CMD_OFFSET 3032
53 #define RECV_RES_OFFSET 3096
54 #define DMA_BUFFER_OFFSET 3160
55 #define DMA_BUFFER_SIZE 4096
56 #define TRACE_LENGTH 3000
58 //-----------------------------------------------------------------------------
59 // Generate the parity value for a byte sequence
61 //-----------------------------------------------------------------------------
62 uint32_t GetParity(const uint8_t * pbtCmd
, int iLen
)
67 // Generate the encrypted data
68 for (i
= 0; i
< iLen
; i
++) {
69 // Save the encrypted parity bit
70 dwPar
|= ((OddByteParity
[pbtCmd
[i
]]) << i
);
75 static void AppendCrc14443a(uint8_t* data
, int len
)
77 ComputeCrc14443(CRC_14443_A
,data
,len
,data
+len
,data
+len
+1);
80 int LogTrace(const uint8_t * btBytes
, int iLen
, int iSamples
, uint32_t dwParity
, int bReader
)
82 // Return when trace is full
83 if (traceLen
>= TRACE_LENGTH
) return FALSE
;
85 // Trace the random, i'm curious
87 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
88 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
89 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
90 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
92 trace
[traceLen
- 1] |= 0x80;
94 trace
[traceLen
++] = ((dwParity
>> 0) & 0xff);
95 trace
[traceLen
++] = ((dwParity
>> 8) & 0xff);
96 trace
[traceLen
++] = ((dwParity
>> 16) & 0xff);
97 trace
[traceLen
++] = ((dwParity
>> 24) & 0xff);
98 trace
[traceLen
++] = iLen
;
99 memcpy(trace
+ traceLen
, btBytes
, iLen
);
104 int LogTraceInfo(byte_t
* data
, size_t len
)
106 return LogTrace(data
,len
,0,GetParity(data
,len
),TRUE
);
109 //-----------------------------------------------------------------------------
110 // The software UART that receives commands from the reader, and its state
112 //-----------------------------------------------------------------------------
116 STATE_START_OF_COMMUNICATION
,
140 static int MillerDecoding(int bit
)
145 if(!Uart
.bitBuffer
) {
146 Uart
.bitBuffer
= bit
^ 0xFF0;
150 Uart
.bitBuffer
<<= 4;
151 Uart
.bitBuffer
^= bit
;
156 if(Uart
.state
!= STATE_UNSYNCD
) {
159 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
165 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
171 if(bit
!= bitright
) { bit
= bitright
; }
173 if(Uart
.posCnt
== 1) {
174 // measurement first half bitperiod
176 Uart
.drop
= DROP_FIRST_HALF
;
180 // measurement second half bitperiod
181 if(!bit
& (Uart
.drop
== DROP_NONE
)) {
182 Uart
.drop
= DROP_SECOND_HALF
;
185 // measured a drop in first and second half
186 // which should not be possible
187 Uart
.state
= STATE_ERROR_WAIT
;
194 case STATE_START_OF_COMMUNICATION
:
196 if(Uart
.drop
== DROP_SECOND_HALF
) {
197 // error, should not happen in SOC
198 Uart
.state
= STATE_ERROR_WAIT
;
203 Uart
.state
= STATE_MILLER_Z
;
210 if(Uart
.drop
== DROP_NONE
) {
211 // logic '0' followed by sequence Y
212 // end of communication
213 Uart
.state
= STATE_UNSYNCD
;
216 // if(Uart.drop == DROP_FIRST_HALF) {
217 // Uart.state = STATE_MILLER_Z; stay the same
218 // we see a logic '0' }
219 if(Uart
.drop
== DROP_SECOND_HALF
) {
220 // we see a logic '1'
221 Uart
.shiftReg
|= 0x100;
222 Uart
.state
= STATE_MILLER_X
;
228 if(Uart
.drop
== DROP_NONE
) {
229 // sequence Y, we see a '0'
230 Uart
.state
= STATE_MILLER_Y
;
233 if(Uart
.drop
== DROP_FIRST_HALF
) {
234 // Would be STATE_MILLER_Z
235 // but Z does not follow X, so error
236 Uart
.state
= STATE_ERROR_WAIT
;
239 if(Uart
.drop
== DROP_SECOND_HALF
) {
240 // We see a '1' and stay in state X
241 Uart
.shiftReg
|= 0x100;
249 if(Uart
.drop
== DROP_NONE
) {
250 // logic '0' followed by sequence Y
251 // end of communication
252 Uart
.state
= STATE_UNSYNCD
;
255 if(Uart
.drop
== DROP_FIRST_HALF
) {
257 Uart
.state
= STATE_MILLER_Z
;
259 if(Uart
.drop
== DROP_SECOND_HALF
) {
260 // We see a '1' and go to state X
261 Uart
.shiftReg
|= 0x100;
262 Uart
.state
= STATE_MILLER_X
;
266 case STATE_ERROR_WAIT
:
267 // That went wrong. Now wait for at least two bit periods
268 // and try to sync again
269 if(Uart
.drop
== DROP_NONE
) {
271 Uart
.state
= STATE_UNSYNCD
;
276 Uart
.state
= STATE_UNSYNCD
;
281 Uart
.drop
= DROP_NONE
;
283 // should have received at least one whole byte...
284 if((Uart
.bitCnt
== 2) && EOC
&& (Uart
.byteCnt
> 0)) {
288 if(Uart
.bitCnt
== 9) {
289 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
292 Uart
.parityBits
<<= 1;
293 Uart
.parityBits
^= ((Uart
.shiftReg
>> 8) & 0x01);
296 // when End of Communication received and
297 // all data bits processed..
304 Uart.output[Uart.byteCnt] = 0xAA;
306 Uart.output[Uart.byteCnt] = error & 0xFF;
308 Uart.output[Uart.byteCnt] = 0xAA;
310 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
312 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
314 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
316 Uart.output[Uart.byteCnt] = 0xAA;
324 bit
= Uart
.bitBuffer
& 0xf0;
328 // should have been high or at least (4 * 128) / fc
329 // according to ISO this should be at least (9 * 128 + 20) / fc
330 if(Uart
.highCnt
== 8) {
331 // we went low, so this could be start of communication
332 // it turns out to be safer to choose a less significant
333 // syncbit... so we check whether the neighbour also represents the drop
334 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
335 Uart
.syncBit
= bit
& 8;
337 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
338 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
339 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
340 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
341 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
342 if(Uart
.syncBit
& (Uart
.bitBuffer
& 8)) {
345 // the first half bit period is expected in next sample
350 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
353 Uart
.state
= STATE_START_OF_COMMUNICATION
;
354 Uart
.drop
= DROP_FIRST_HALF
;
365 if(Uart
.highCnt
< 8) {
374 //=============================================================================
375 // ISO 14443 Type A - Manchester
376 //=============================================================================
381 DEMOD_START_OF_COMMUNICATION
,
404 static int ManchesterDecoding(int v
)
420 if(Demod
.state
==DEMOD_UNSYNCD
) {
421 Demod
.output
[Demod
.len
] = 0xfa;
424 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
425 if(bit
& 0x08) { Demod
.syncBit
= 0x08; }
427 if(bit
& 0x04) { Demod
.syncBit
= 0x04; }
429 else if(bit
& 0x04) { Demod
.syncBit
= 0x04; bit
<<= 4; }
431 if(bit
& 0x02) { Demod
.syncBit
= 0x02; }
433 else if(bit
& 0x02) { Demod
.syncBit
= 0x02; bit
<<= 4; }
435 if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
437 if(Demod
.syncBit
& (Demod
.buffer
& 0x08)) {
438 Demod
.syncBit
= 0x08;
440 // The first half bitperiod is expected in next sample
442 Demod
.output
[Demod
.len
] = 0xfb;
445 else if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
449 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
450 Demod
.sub
= SUB_FIRST_HALF
;
453 Demod
.parityBits
= 0;
456 switch(Demod
.syncBit
) {
457 case 0x08: Demod
.samples
= 3; break;
458 case 0x04: Demod
.samples
= 2; break;
459 case 0x02: Demod
.samples
= 1; break;
460 case 0x01: Demod
.samples
= 0; break;
467 //modulation = bit & Demod.syncBit;
468 modulation
= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
472 if(Demod
.posCount
==0) {
475 Demod
.sub
= SUB_FIRST_HALF
;
478 Demod
.sub
= SUB_NONE
;
483 if(modulation
&& (Demod
.sub
== SUB_FIRST_HALF
)) {
484 if(Demod
.state
!=DEMOD_ERROR_WAIT
) {
485 Demod
.state
= DEMOD_ERROR_WAIT
;
486 Demod
.output
[Demod
.len
] = 0xaa;
490 else if(modulation
) {
491 Demod
.sub
= SUB_SECOND_HALF
;
494 switch(Demod
.state
) {
495 case DEMOD_START_OF_COMMUNICATION
:
496 if(Demod
.sub
== SUB_FIRST_HALF
) {
497 Demod
.state
= DEMOD_MANCHESTER_D
;
500 Demod
.output
[Demod
.len
] = 0xab;
501 Demod
.state
= DEMOD_ERROR_WAIT
;
506 case DEMOD_MANCHESTER_D
:
507 case DEMOD_MANCHESTER_E
:
508 if(Demod
.sub
== SUB_FIRST_HALF
) {
510 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
511 Demod
.state
= DEMOD_MANCHESTER_D
;
513 else if(Demod
.sub
== SUB_SECOND_HALF
) {
515 Demod
.shiftReg
>>= 1;
516 Demod
.state
= DEMOD_MANCHESTER_E
;
519 Demod
.state
= DEMOD_MANCHESTER_F
;
523 case DEMOD_MANCHESTER_F
:
524 // Tag response does not need to be a complete byte!
525 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
526 if(Demod
.bitCount
> 0) {
527 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
528 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
530 // No parity bit, so just shift a 0
531 Demod
.parityBits
<<= 1;
534 Demod
.state
= DEMOD_UNSYNCD
;
538 Demod
.output
[Demod
.len
] = 0xad;
539 Demod
.state
= DEMOD_ERROR_WAIT
;
544 case DEMOD_ERROR_WAIT
:
545 Demod
.state
= DEMOD_UNSYNCD
;
549 Demod
.output
[Demod
.len
] = 0xdd;
550 Demod
.state
= DEMOD_UNSYNCD
;
554 if(Demod
.bitCount
>=9) {
555 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
558 Demod
.parityBits
<<= 1;
559 Demod
.parityBits
^= ((Demod
.shiftReg
>> 8) & 0x01);
566 Demod.output[Demod.len] = 0xBB;
568 Demod.output[Demod.len] = error & 0xFF;
570 Demod.output[Demod.len] = 0xBB;
572 Demod.output[Demod.len] = bit & 0xFF;
574 Demod.output[Demod.len] = Demod.buffer & 0xFF;
576 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
578 Demod.output[Demod.len] = 0xBB;
585 } // end (state != UNSYNCED)
590 //=============================================================================
591 // Finally, a `sniffer' for ISO 14443 Type A
592 // Both sides of communication!
593 //=============================================================================
595 //-----------------------------------------------------------------------------
596 // Record the sequence of commands sent by the reader to the tag, with
597 // triggering so that we start recording at the point that the tag is moved
599 //-----------------------------------------------------------------------------
600 void SnoopIso14443a(void)
602 // #define RECV_CMD_OFFSET 2032 // original (working as of 21/2/09) values
603 // #define RECV_RES_OFFSET 2096 // original (working as of 21/2/09) values
604 // #define DMA_BUFFER_OFFSET 2160 // original (working as of 21/2/09) values
605 // #define DMA_BUFFER_SIZE 4096 // original (working as of 21/2/09) values
606 // #define TRACE_LENGTH 2000 // original (working as of 21/2/09) values
608 // We won't start recording the frames that we acquire until we trigger;
609 // a good trigger condition to get started is probably when we see a
610 // response from the tag.
611 int triggered
= TRUE
; // FALSE to wait first for card
613 // The command (reader -> tag) that we're receiving.
614 // The length of a received command will in most cases be no more than 18 bytes.
615 // So 32 should be enough!
616 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
617 // The response (tag -> reader) that we're receiving.
618 uint8_t *receivedResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
620 // As we receive stuff, we copy it from receivedCmd or receivedResponse
621 // into trace, along with its length and other annotations.
622 //uint8_t *trace = (uint8_t *)BigBuf;
625 // The DMA buffer, used to stream samples from the FPGA
626 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
632 // Count of samples received so far, so that we can include timing
633 // information in the trace buffer.
637 memset(trace
, 0x44, RECV_CMD_OFFSET
);
639 // Set up the demodulator for tag -> reader responses.
640 Demod
.output
= receivedResponse
;
642 Demod
.state
= DEMOD_UNSYNCD
;
644 // And the reader -> tag commands
645 memset(&Uart
, 0, sizeof(Uart
));
646 Uart
.output
= receivedCmd
;
647 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
648 Uart
.state
= STATE_UNSYNCD
;
650 // And put the FPGA in the appropriate mode
651 // Signal field is off with the appropriate LED
653 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
654 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
656 // Setup for the DMA.
659 lastRxCounter
= DMA_BUFFER_SIZE
;
660 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
664 // And now we loop, receiving samples.
667 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
669 if(behindBy
> maxBehindBy
) {
670 maxBehindBy
= behindBy
;
672 DbpString("blew circular buffer!");
676 if(behindBy
< 1) continue;
681 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
682 upTo
-= DMA_BUFFER_SIZE
;
683 lastRxCounter
+= DMA_BUFFER_SIZE
;
684 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
685 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
689 #define HANDLE_BIT_IF_BODY \
692 trace[traceLen++] = ((rsamples >> 0) & 0xff); \
693 trace[traceLen++] = ((rsamples >> 8) & 0xff); \
694 trace[traceLen++] = ((rsamples >> 16) & 0xff); \
695 trace[traceLen++] = ((rsamples >> 24) & 0xff); \
696 trace[traceLen++] = ((Uart.parityBits >> 0) & 0xff); \
697 trace[traceLen++] = ((Uart.parityBits >> 8) & 0xff); \
698 trace[traceLen++] = ((Uart.parityBits >> 16) & 0xff); \
699 trace[traceLen++] = ((Uart.parityBits >> 24) & 0xff); \
700 trace[traceLen++] = Uart.byteCnt; \
701 memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \
702 traceLen += Uart.byteCnt; \
703 if(traceLen > TRACE_LENGTH) break; \
705 /* And ready to receive another command. */ \
706 Uart.state = STATE_UNSYNCD; \
707 /* And also reset the demod code, which might have been */ \
708 /* false-triggered by the commands from the reader. */ \
709 Demod.state = DEMOD_UNSYNCD; \
712 if(MillerDecoding((smpl & 0xF0) >> 4)) {
713 rsamples
= samples
- Uart
.samples
;
716 if(ManchesterDecoding(smpl
& 0x0F)) {
717 rsamples
= samples
- Demod
.samples
;
720 // timestamp, as a count of samples
721 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
722 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
723 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
724 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
725 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
726 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
727 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
728 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
730 trace
[traceLen
++] = Demod
.len
;
731 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
732 traceLen
+= Demod
.len
;
733 if(traceLen
> TRACE_LENGTH
) break;
737 // And ready to receive another response.
738 memset(&Demod
, 0, sizeof(Demod
));
739 Demod
.output
= receivedResponse
;
740 Demod
.state
= DEMOD_UNSYNCD
;
745 DbpString("cancelled_a");
750 DbpString("COMMAND FINISHED");
752 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
753 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
756 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
757 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
758 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
765 // Prepare communication bits to send to FPGA
766 void Sequence(SecType seq
)
772 // Sequence D: 11110000
773 // modulation with subcarrier during first half
774 ToSend
[ToSendMax
] = 0xf0;
777 // Sequence E: 00001111
778 // modulation with subcarrier during second half
779 ToSend
[ToSendMax
] = 0x0f;
782 // Sequence F: 00000000
783 // no modulation with subcarrier
784 ToSend
[ToSendMax
] = 0x00;
788 // Sequence X: 00001100
789 // drop after half a period
790 ToSend
[ToSendMax
] = 0x0c;
794 // Sequence Y: 00000000
796 ToSend
[ToSendMax
] = 0x00;
799 // Sequence Z: 11000000
801 ToSend
[ToSendMax
] = 0xc0;
806 //-----------------------------------------------------------------------------
807 // Prepare tag messages
808 //-----------------------------------------------------------------------------
809 static void CodeIso14443aAsTag(const uint8_t *cmd
, int len
)
816 // Correction bit, might be removed when not needed
821 ToSendStuffBit(1); // 1
829 for(i
= 0; i
< len
; i
++) {
835 for(j
= 0; j
< 8; j
++) {
836 oddparity
^= (b
& 1);
856 // Flush the buffer in FPGA!!
857 for(i
= 0; i
< 5; i
++) {
861 // Convert from last byte pos to length
864 // Add a few more for slop
865 ToSend
[ToSendMax
++] = 0x00;
866 ToSend
[ToSendMax
++] = 0x00;
870 //-----------------------------------------------------------------------------
871 // This is to send a NACK kind of answer, its only 3 bits, I know it should be 4
872 //-----------------------------------------------------------------------------
873 static void CodeStrangeAnswer()
879 // Correction bit, might be removed when not needed
884 ToSendStuffBit(1); // 1
904 // Flush the buffer in FPGA!!
905 for(i
= 0; i
< 5; i
++) {
909 // Convert from last byte pos to length
912 // Add a few more for slop
913 ToSend
[ToSendMax
++] = 0x00;
914 ToSend
[ToSendMax
++] = 0x00;
918 //-----------------------------------------------------------------------------
919 // Wait for commands from reader
920 // Stop when button is pressed
921 // Or return TRUE when command is captured
922 //-----------------------------------------------------------------------------
923 static int GetIso14443aCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
925 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
926 // only, since we are receiving, not transmitting).
927 // Signal field is off with the appropriate LED
929 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
931 // Now run a `software UART' on the stream of incoming samples.
932 Uart
.output
= received
;
933 Uart
.byteCntMax
= maxLen
;
934 Uart
.state
= STATE_UNSYNCD
;
939 if(BUTTON_PRESS()) return FALSE
;
941 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
942 AT91C_BASE_SSC
->SSC_THR
= 0x00;
944 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
945 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
946 if(MillerDecoding((b
& 0xf0) >> 4)) {
950 if(MillerDecoding(b
& 0x0f)) {
958 //-----------------------------------------------------------------------------
959 // Main loop of simulated tag: receive commands from reader, decide what
960 // response to send, and send it.
961 //-----------------------------------------------------------------------------
962 void SimulateIso14443aTag(int tagType
, int TagUid
)
964 // This function contains the tag emulation
966 // Prepare protocol messages
967 // static const uint8_t cmd1[] = { 0x26 };
968 // static const uint8_t response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg
970 static const uint8_t response1
[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me
971 // static const uint8_t response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me
974 // static const uint8_t cmd2[] = { 0x93, 0x20 };
975 //static const uint8_t response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg
980 static const uint8_t response2
[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips
983 // When reader selects us during cascade1 it will send cmd3
984 //uint8_t response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE)
985 uint8_t response3
[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire)
986 ComputeCrc14443(CRC_14443_A
, response3
, 1, &response3
[1], &response3
[2]);
988 // send cascade2 2nd half of UID
989 static const uint8_t response2a
[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck
990 // NOTE : THE CRC on the above may be wrong as I have obfuscated the actual UID
993 // When reader selects us during cascade2 it will send cmd3a
994 //uint8_t response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE)
995 uint8_t response3a
[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire)
996 ComputeCrc14443(CRC_14443_A
, response3a
, 1, &response3a
[1], &response3a
[2]);
998 static const uint8_t response5
[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce
1003 // Longest possible response will be 16 bytes + 2 CRC = 18 bytes
1005 // 144 data bits (18 * 8)
1008 // 1 Correction bit (Answer in 1172 or 1236 periods, see FPGA)
1009 // 1 just for the case
1013 // 166 bytes, since every bit that needs to be send costs us a byte
1017 // Respond with card type
1018 uint8_t *resp1
= (((uint8_t *)BigBuf
) + 800);
1021 // Anticollision cascade1 - respond with uid
1022 uint8_t *resp2
= (((uint8_t *)BigBuf
) + 970);
1025 // Anticollision cascade2 - respond with 2nd half of uid if asked
1026 // we're only going to be asked if we set the 1st byte of the UID (during cascade1) to 0x88
1027 uint8_t *resp2a
= (((uint8_t *)BigBuf
) + 1140);
1030 // Acknowledge select - cascade 1
1031 uint8_t *resp3
= (((uint8_t *)BigBuf
) + 1310);
1034 // Acknowledge select - cascade 2
1035 uint8_t *resp3a
= (((uint8_t *)BigBuf
) + 1480);
1038 // Response to a read request - not implemented atm
1039 uint8_t *resp4
= (((uint8_t *)BigBuf
) + 1550);
1042 // Authenticate response - nonce
1043 uint8_t *resp5
= (((uint8_t *)BigBuf
) + 1720);
1046 uint8_t *receivedCmd
= (uint8_t *)BigBuf
;
1053 // To control where we are in the protocol
1057 // Just to allow some checks
1065 memset(receivedCmd
, 0x44, 400);
1067 // Prepare the responses of the anticollision phase
1068 // there will be not enough time to do this at the moment the reader sends it REQA
1070 // Answer to request
1071 CodeIso14443aAsTag(response1
, sizeof(response1
));
1072 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1074 // Send our UID (cascade 1)
1075 CodeIso14443aAsTag(response2
, sizeof(response2
));
1076 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1078 // Answer to select (cascade1)
1079 CodeIso14443aAsTag(response3
, sizeof(response3
));
1080 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1082 // Send the cascade 2 2nd part of the uid
1083 CodeIso14443aAsTag(response2a
, sizeof(response2a
));
1084 memcpy(resp2a
, ToSend
, ToSendMax
); resp2aLen
= ToSendMax
;
1086 // Answer to select (cascade 2)
1087 CodeIso14443aAsTag(response3a
, sizeof(response3a
));
1088 memcpy(resp3a
, ToSend
, ToSendMax
); resp3aLen
= ToSendMax
;
1090 // Strange answer is an example of rare message size (3 bits)
1091 CodeStrangeAnswer();
1092 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1094 // Authentication answer (random nonce)
1095 CodeIso14443aAsTag(response5
, sizeof(response5
));
1096 memcpy(resp5
, ToSend
, ToSendMax
); resp5Len
= ToSendMax
;
1098 // We need to listen to the high-frequency, peak-detected path.
1099 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1107 if(!GetIso14443aCommandFromReader(receivedCmd
, &len
, 100)) {
1108 DbpString("button press");
1111 // 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
1112 // Okay, look at the command now.
1114 i
= 1; // first byte transmitted
1115 if(receivedCmd
[0] == 0x26) {
1116 // Received a REQUEST
1117 resp
= resp1
; respLen
= resp1Len
; order
= 1;
1118 //DbpString("Hello request from reader:");
1119 } else if(receivedCmd
[0] == 0x52) {
1120 // Received a WAKEUP
1121 resp
= resp1
; respLen
= resp1Len
; order
= 6;
1122 // //DbpString("Wakeup request from reader:");
1124 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] == 0x93) { // greg - cascade 1 anti-collision
1125 // Received request for UID (cascade 1)
1126 resp
= resp2
; respLen
= resp2Len
; order
= 2;
1127 // DbpString("UID (cascade 1) request from reader:");
1128 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1131 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] ==0x95) { // greg - cascade 2 anti-collision
1132 // Received request for UID (cascade 2)
1133 resp
= resp2a
; respLen
= resp2aLen
; order
= 20;
1134 // DbpString("UID (cascade 2) request from reader:");
1135 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1138 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x93) { // greg - cascade 1 select
1139 // Received a SELECT
1140 resp
= resp3
; respLen
= resp3Len
; order
= 3;
1141 // DbpString("Select (cascade 1) request from reader:");
1142 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1145 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x95) { // greg - cascade 2 select
1146 // Received a SELECT
1147 resp
= resp3a
; respLen
= resp3aLen
; order
= 30;
1148 // DbpString("Select (cascade 2) request from reader:");
1149 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1152 } else if(receivedCmd
[0] == 0x30) {
1154 resp
= resp4
; respLen
= resp4Len
; order
= 4; // Do nothing
1155 Dbprintf("Read request from reader: %x %x %x",
1156 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1159 } else if(receivedCmd
[0] == 0x50) {
1161 resp
= resp1
; respLen
= 0; order
= 5; // Do nothing
1162 DbpString("Reader requested we HALT!:");
1164 } else if(receivedCmd
[0] == 0x60) {
1165 // Received an authentication request
1166 resp
= resp5
; respLen
= resp5Len
; order
= 7;
1167 Dbprintf("Authenticate request from reader: %x %x %x",
1168 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1170 } else if(receivedCmd
[0] == 0xE0) {
1171 // Received a RATS request
1172 resp
= resp1
; respLen
= 0;order
= 70;
1173 Dbprintf("RATS request from reader: %x %x %x",
1174 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1176 // Never seen this command before
1177 Dbprintf("Unknown command received from reader: %x %x %x %x %x %x %x %x %x",
1178 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1179 receivedCmd
[3], receivedCmd
[3], receivedCmd
[4],
1180 receivedCmd
[5], receivedCmd
[6], receivedCmd
[7]);
1182 resp
= resp1
; respLen
= 0; order
= 0;
1185 // Count number of wakeups received after a halt
1186 if(order
== 6 && lastorder
== 5) { happened
++; }
1188 // Count number of other messages after a halt
1189 if(order
!= 6 && lastorder
== 5) { happened2
++; }
1191 // Look at last parity bit to determine timing of answer
1192 if((Uart
.parityBits
& 0x01) || receivedCmd
[0] == 0x52) {
1193 // 1236, so correction bit needed
1197 memset(receivedCmd
, 0x44, 32);
1199 if(cmdsRecvd
> 999) {
1200 DbpString("1000 commands later...");
1207 if(respLen
<= 0) continue;
1209 // Modulate Manchester
1210 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_MOD
);
1211 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1214 // ### Transmit the response ###
1217 fdt_indicator
= FALSE
;
1219 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1220 volatile uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1223 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1231 AT91C_BASE_SSC
->SSC_THR
= b
;
1237 if(BUTTON_PRESS()) {
1244 Dbprintf("%x %x %x", happened
, happened2
, cmdsRecvd
);
1248 //-----------------------------------------------------------------------------
1249 // Transmit the command (to the tag) that was placed in ToSend[].
1250 //-----------------------------------------------------------------------------
1251 static void TransmitFor14443a(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1255 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1261 for(c
= 0; c
< *wait
;) {
1262 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1263 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1266 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1267 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1275 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1276 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
1282 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1283 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1288 if (samples
) *samples
= (c
+ *wait
) << 3;
1291 //-----------------------------------------------------------------------------
1292 // To generate an arbitrary stream from reader
1294 //-----------------------------------------------------------------------------
1295 void ArbitraryFromReader(const uint8_t *cmd
, int parity
, int len
)
1304 // Start of Communication (Seq. Z)
1308 for(i
= 0; i
< len
; i
++) {
1311 for(j
= 0; j
< 8; j
++) {
1331 // Predefined parity bit, the flipper flips when needed, because of flips in byte sent
1332 if(((parity
>> (len
- i
- 1)) & 1)) {
1349 // End of Communication
1367 // Convert from last character reference to length
1371 //-----------------------------------------------------------------------------
1372 // Code a 7-bit command without parity bit
1373 // This is especially for 0x26 and 0x52 (REQA and WUPA)
1374 //-----------------------------------------------------------------------------
1375 void ShortFrameFromReader(const uint8_t bt
)
1383 // Start of Communication (Seq. Z)
1388 for(j
= 0; j
< 7; j
++) {
1407 // End of Communication
1425 // Convert from last character reference to length
1429 //-----------------------------------------------------------------------------
1430 // Prepare reader command to send to FPGA
1432 //-----------------------------------------------------------------------------
1433 void CodeIso14443aAsReaderPar(const uint8_t * cmd
, int len
, uint32_t dwParity
)
1441 // Start of Communication (Seq. Z)
1445 // Generate send structure for the data bits
1446 for (i
= 0; i
< len
; i
++) {
1447 // Get the current byte to send
1450 for (j
= 0; j
< 8; j
++) {
1468 // Get the parity bit
1469 if ((dwParity
>> i
) & 0x01) {
1485 // End of Communication
1502 // Convert from last character reference to length
1506 //-----------------------------------------------------------------------------
1507 // Wait a certain time for tag response
1508 // If a response is captured return TRUE
1509 // If it takes to long return FALSE
1510 //-----------------------------------------------------------------------------
1511 static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1513 // buffer needs to be 512 bytes
1516 // Set FPGA mode to "reader listen mode", no modulation (listen
1517 // only, since we are receiving, not transmitting).
1518 // Signal field is on with the appropriate LED
1520 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1522 // Now get the answer from the card
1523 Demod
.output
= receivedResponse
;
1525 Demod
.state
= DEMOD_UNSYNCD
;
1528 if (elapsed
) *elapsed
= 0;
1534 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1535 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1536 if (elapsed
) (*elapsed
)++;
1538 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1539 if(c
< 512) { c
++; } else { return FALSE
; }
1540 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1541 if(ManchesterDecoding((b
& 0xf0) >> 4)) {
1542 *samples
= ((c
- 1) << 3) + 4;
1545 if(ManchesterDecoding(b
& 0x0f)) {
1553 void ReaderTransmitShort(const uint8_t* bt
)
1558 ShortFrameFromReader(*bt
);
1561 TransmitFor14443a(ToSend
, ToSendMax
, &samples
, &wait
);
1563 // Store reader command in buffer
1564 if (tracing
) LogTrace(bt
,1,0,GetParity(bt
,1),TRUE
);
1567 void ReaderTransmitPar(uint8_t* frame
, int len
, uint32_t par
)
1572 // This is tied to other size changes
1573 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1574 CodeIso14443aAsReaderPar(frame
,len
,par
);
1577 TransmitFor14443a(ToSend
, ToSendMax
, &samples
, &wait
);
1579 // Store reader command in buffer
1580 if (tracing
) LogTrace(frame
,len
,0,par
,TRUE
);
1584 void ReaderTransmit(uint8_t* frame
, int len
)
1586 // Generate parity and redirect
1587 ReaderTransmitPar(frame
,len
,GetParity(frame
,len
));
1590 int ReaderReceive(uint8_t* receivedAnswer
)
1593 if (!GetIso14443aAnswerFromTag(receivedAnswer
,100,&samples
,0)) return FALSE
;
1594 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
);
1598 //-----------------------------------------------------------------------------
1599 // Read an ISO 14443a tag. Send out commands and store answers.
1601 //-----------------------------------------------------------------------------
1602 void ReaderIso14443a(uint32_t parameter
)
1605 uint8_t wupa
[] = { 0x52 };
1606 uint8_t sel_all
[] = { 0x93,0x20 };
1607 uint8_t sel_uid
[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1608 uint8_t sel_all_c2
[] = { 0x95,0x20 };
1609 uint8_t sel_uid_c2
[] = { 0x95,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1612 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1613 // uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00 };
1615 uint8_t* receivedAnswer
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1621 // Start from off (no field generated)
1622 // Signal field is off with the appropriate LED
1624 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1627 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1630 // Now give it time to spin up.
1631 // Signal field is on with the appropriate LED
1633 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1640 while(traceLen
< TRACE_LENGTH
)
1642 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1643 ReaderTransmitShort(wupa
);
1645 // Test if the action was cancelled
1646 if(BUTTON_PRESS()) {
1651 if (!ReaderReceive(receivedAnswer
)) continue;
1653 // Transmit SELECT_ALL
1654 ReaderTransmit(sel_all
,sizeof(sel_all
));
1657 if (!ReaderReceive(receivedAnswer
)) continue;
1659 // Construct SELECT UID command
1660 // First copy the 5 bytes (Mifare Classic) after the 93 70
1661 memcpy(sel_uid
+2,receivedAnswer
,5);
1662 // Secondly compute the two CRC bytes at the end
1663 AppendCrc14443a(sel_uid
,7);
1665 // Transmit SELECT_UID
1666 ReaderTransmit(sel_uid
,sizeof(sel_uid
));
1669 if (!ReaderReceive(receivedAnswer
)) continue;
1671 // OK we have selected at least at cascade 1, lets see if first byte of UID was 0x88 in
1672 // which case we need to make a cascade 2 request and select - this is a long UID
1673 // When the UID is not complete, the 3nd bit (from the right) is set in the SAK.
1674 if (receivedAnswer
[0] &= 0x04)
1676 // Transmit SELECT_ALL
1677 ReaderTransmit(sel_all_c2
,sizeof(sel_all_c2
));
1680 if (!ReaderReceive(receivedAnswer
)) continue;
1682 // Construct SELECT UID command
1683 memcpy(sel_uid_c2
+2,receivedAnswer
,5);
1684 // Secondly compute the two CRC bytes at the end
1685 AppendCrc14443a(sel_uid_c2
,7);
1687 // Transmit SELECT_UID
1688 ReaderTransmit(sel_uid_c2
,sizeof(sel_uid_c2
));
1691 if (!ReaderReceive(receivedAnswer
)) continue;
1694 // Transmit MIFARE_CLASSIC_AUTH
1695 ReaderTransmit(mf_auth
,sizeof(mf_auth
));
1697 // Receive the (16 bit) "random" nonce
1698 if (!ReaderReceive(receivedAnswer
)) continue;
1702 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1704 Dbprintf("%x %x %x", rsamples
, 0xCC, 0xCC);
1705 DbpString("ready..");
1708 //-----------------------------------------------------------------------------
1709 // Read an ISO 14443a tag. Send out commands and store answers.
1711 //-----------------------------------------------------------------------------
1712 void ReaderMifare(uint32_t parameter
)
1716 uint8_t wupa
[] = { 0x52 };
1717 uint8_t sel_all
[] = { 0x93,0x20 };
1718 uint8_t sel_uid
[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1721 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1722 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1724 uint8_t* receivedAnswer
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1731 // Start from off (no field generated)
1732 // Signal field is off with the appropriate LED
1734 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1737 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1740 // Now give it time to spin up.
1741 // Signal field is on with the appropriate LED
1743 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1750 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1751 ReaderTransmitShort(wupa
);
1753 ReaderReceive(receivedAnswer
);
1754 // Transmit SELECT_ALL
1755 ReaderTransmit(sel_all
,sizeof(sel_all
));
1757 ReaderReceive(receivedAnswer
);
1758 // Construct SELECT UID command
1759 // First copy the 5 bytes (Mifare Classic) after the 93 70
1760 memcpy(sel_uid
+2,receivedAnswer
,5);
1761 // Secondly compute the two CRC bytes at the end
1762 AppendCrc14443a(sel_uid
,7);
1767 byte_t par_mask
= 0xff;
1773 byte_t nt_attacked
[4];
1776 num_to_bytes(parameter
,4,nt_attacked
);
1780 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1782 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1784 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1785 ReaderTransmitShort(wupa
);
1787 // Test if the action was cancelled
1788 if(BUTTON_PRESS()) {
1793 if (!ReaderReceive(receivedAnswer
)) continue;
1795 // Transmit SELECT_ALL
1796 ReaderTransmit(sel_all
,sizeof(sel_all
));
1799 if (!ReaderReceive(receivedAnswer
)) continue;
1801 // Transmit SELECT_UID
1802 ReaderTransmit(sel_uid
,sizeof(sel_uid
));
1805 if (!ReaderReceive(receivedAnswer
)) continue;
1807 // Transmit MIFARE_CLASSIC_AUTH
1808 ReaderTransmit(mf_auth
,sizeof(mf_auth
));
1810 // Receive the (16 bit) "random" nonce
1811 if (!ReaderReceive(receivedAnswer
)) continue;
1812 memcpy(nt
,receivedAnswer
,4);
1814 // Transmit reader nonce and reader answer
1815 ReaderTransmitPar(mf_nr_ar
,sizeof(mf_nr_ar
),par
);
1817 // Receive 4 bit answer
1818 if (ReaderReceive(receivedAnswer
))
1823 memcpy(nt_attacked
,nt
,4);
1825 par_low
= par
& 0x07;
1828 if (memcmp(nt
,nt_attacked
,4) != 0) continue;
1831 if(led_on
) LED_B_ON(); else LED_B_OFF();
1832 par_list
[nt_diff
] = par
;
1833 ks_list
[nt_diff
] = receivedAnswer
[0]^0x05;
1835 // Test if the information is complete
1836 if (nt_diff
== 0x07) break;
1838 nt_diff
= (nt_diff
+1) & 0x07;
1839 mf_nr_ar
[3] = nt_diff
<< 5;
1846 par
= (((par
>>3)+1) << 3) | par_low
;
1851 LogTraceInfo(sel_uid
+2,4);
1853 LogTraceInfo(par_list
,8);
1854 LogTraceInfo(ks_list
,8);
1857 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);