1 //-----------------------------------------------------------------------------
2 // Routines to support ISO 14443 type A.
4 // Gerhard de Koning Gans - May 2008
5 //-----------------------------------------------------------------------------
11 #include "iso14443crc.h"
13 static uint8_t *trace
= (uint8_t *) BigBuf
;
14 static int traceLen
= 0;
15 static int rsamples
= 0;
16 static int tracing
= TRUE
;
27 static const uint8_t OddByteParity
[256] = {
28 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
29 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
30 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
31 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
32 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
33 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
34 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
35 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
36 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
37 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
38 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
39 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
40 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
41 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
42 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
43 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
46 // BIG CHANGE - UNDERSTAND THIS BEFORE WE COMMIT
47 #define RECV_CMD_OFFSET 3032
48 #define RECV_RES_OFFSET 3096
49 #define DMA_BUFFER_OFFSET 3160
50 #define DMA_BUFFER_SIZE 4096
51 #define TRACE_LENGTH 3000
53 //-----------------------------------------------------------------------------
54 // Generate the parity value for a byte sequence
56 //-----------------------------------------------------------------------------
57 uint32_t GetParity(const uint8_t * pbtCmd
, int iLen
)
62 // Generate the encrypted data
63 for (i
= 0; i
< iLen
; i
++) {
64 // Save the encrypted parity bit
65 dwPar
|= ((OddByteParity
[pbtCmd
[i
]]) << i
);
70 static void AppendCrc14443a(uint8_t* data
, int len
)
72 ComputeCrc14443(CRC_14443_A
,data
,len
,data
+len
,data
+len
+1);
75 int LogTrace(const uint8_t * btBytes
, int iLen
, int iSamples
, uint32_t dwParity
, int bReader
)
77 // Return when trace is full
78 if (traceLen
>= TRACE_LENGTH
) return FALSE
;
80 // Trace the random, i'm curious
82 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
83 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
84 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
85 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
87 trace
[traceLen
- 1] |= 0x80;
89 trace
[traceLen
++] = ((dwParity
>> 0) & 0xff);
90 trace
[traceLen
++] = ((dwParity
>> 8) & 0xff);
91 trace
[traceLen
++] = ((dwParity
>> 16) & 0xff);
92 trace
[traceLen
++] = ((dwParity
>> 24) & 0xff);
93 trace
[traceLen
++] = iLen
;
94 memcpy(trace
+ traceLen
, btBytes
, iLen
);
99 int LogTraceInfo(byte_t
* data
, size_t len
)
101 return LogTrace(data
,len
,0,GetParity(data
,len
),TRUE
);
104 //-----------------------------------------------------------------------------
105 // The software UART that receives commands from the reader, and its state
107 //-----------------------------------------------------------------------------
111 STATE_START_OF_COMMUNICATION
,
135 static int MillerDecoding(int bit
)
140 if(!Uart
.bitBuffer
) {
141 Uart
.bitBuffer
= bit
^ 0xFF0;
145 Uart
.bitBuffer
<<= 4;
146 Uart
.bitBuffer
^= bit
;
151 if(Uart
.state
!= STATE_UNSYNCD
) {
154 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
160 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
166 if(bit
!= bitright
) { bit
= bitright
; }
168 if(Uart
.posCnt
== 1) {
169 // measurement first half bitperiod
171 Uart
.drop
= DROP_FIRST_HALF
;
175 // measurement second half bitperiod
176 if(!bit
& (Uart
.drop
== DROP_NONE
)) {
177 Uart
.drop
= DROP_SECOND_HALF
;
180 // measured a drop in first and second half
181 // which should not be possible
182 Uart
.state
= STATE_ERROR_WAIT
;
189 case STATE_START_OF_COMMUNICATION
:
191 if(Uart
.drop
== DROP_SECOND_HALF
) {
192 // error, should not happen in SOC
193 Uart
.state
= STATE_ERROR_WAIT
;
198 Uart
.state
= STATE_MILLER_Z
;
205 if(Uart
.drop
== DROP_NONE
) {
206 // logic '0' followed by sequence Y
207 // end of communication
208 Uart
.state
= STATE_UNSYNCD
;
211 // if(Uart.drop == DROP_FIRST_HALF) {
212 // Uart.state = STATE_MILLER_Z; stay the same
213 // we see a logic '0' }
214 if(Uart
.drop
== DROP_SECOND_HALF
) {
215 // we see a logic '1'
216 Uart
.shiftReg
|= 0x100;
217 Uart
.state
= STATE_MILLER_X
;
223 if(Uart
.drop
== DROP_NONE
) {
224 // sequence Y, we see a '0'
225 Uart
.state
= STATE_MILLER_Y
;
228 if(Uart
.drop
== DROP_FIRST_HALF
) {
229 // Would be STATE_MILLER_Z
230 // but Z does not follow X, so error
231 Uart
.state
= STATE_ERROR_WAIT
;
234 if(Uart
.drop
== DROP_SECOND_HALF
) {
235 // We see a '1' and stay in state X
236 Uart
.shiftReg
|= 0x100;
244 if(Uart
.drop
== DROP_NONE
) {
245 // logic '0' followed by sequence Y
246 // end of communication
247 Uart
.state
= STATE_UNSYNCD
;
250 if(Uart
.drop
== DROP_FIRST_HALF
) {
252 Uart
.state
= STATE_MILLER_Z
;
254 if(Uart
.drop
== DROP_SECOND_HALF
) {
255 // We see a '1' and go to state X
256 Uart
.shiftReg
|= 0x100;
257 Uart
.state
= STATE_MILLER_X
;
261 case STATE_ERROR_WAIT
:
262 // That went wrong. Now wait for at least two bit periods
263 // and try to sync again
264 if(Uart
.drop
== DROP_NONE
) {
266 Uart
.state
= STATE_UNSYNCD
;
271 Uart
.state
= STATE_UNSYNCD
;
276 Uart
.drop
= DROP_NONE
;
278 // should have received at least one whole byte...
279 if((Uart
.bitCnt
== 2) && EOC
&& (Uart
.byteCnt
> 0)) {
283 if(Uart
.bitCnt
== 9) {
284 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
287 Uart
.parityBits
<<= 1;
288 Uart
.parityBits
^= ((Uart
.shiftReg
>> 8) & 0x01);
291 // when End of Communication received and
292 // all data bits processed..
299 Uart.output[Uart.byteCnt] = 0xAA;
301 Uart.output[Uart.byteCnt] = error & 0xFF;
303 Uart.output[Uart.byteCnt] = 0xAA;
305 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
307 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
309 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
311 Uart.output[Uart.byteCnt] = 0xAA;
319 bit
= Uart
.bitBuffer
& 0xf0;
323 // should have been high or at least (4 * 128) / fc
324 // according to ISO this should be at least (9 * 128 + 20) / fc
325 if(Uart
.highCnt
== 8) {
326 // we went low, so this could be start of communication
327 // it turns out to be safer to choose a less significant
328 // syncbit... so we check whether the neighbour also represents the drop
329 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
330 Uart
.syncBit
= bit
& 8;
332 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
333 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
334 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
335 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
336 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
337 if(Uart
.syncBit
& (Uart
.bitBuffer
& 8)) {
340 // the first half bit period is expected in next sample
345 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
348 Uart
.state
= STATE_START_OF_COMMUNICATION
;
349 Uart
.drop
= DROP_FIRST_HALF
;
360 if(Uart
.highCnt
< 8) {
369 //=============================================================================
370 // ISO 14443 Type A - Manchester
371 //=============================================================================
376 DEMOD_START_OF_COMMUNICATION
,
399 static int ManchesterDecoding(int v
)
415 if(Demod
.state
==DEMOD_UNSYNCD
) {
416 Demod
.output
[Demod
.len
] = 0xfa;
419 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
420 if(bit
& 0x08) { Demod
.syncBit
= 0x08; }
422 if(bit
& 0x04) { Demod
.syncBit
= 0x04; }
424 else if(bit
& 0x04) { Demod
.syncBit
= 0x04; bit
<<= 4; }
426 if(bit
& 0x02) { Demod
.syncBit
= 0x02; }
428 else if(bit
& 0x02) { Demod
.syncBit
= 0x02; bit
<<= 4; }
430 if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
432 if(Demod
.syncBit
& (Demod
.buffer
& 0x08)) {
433 Demod
.syncBit
= 0x08;
435 // The first half bitperiod is expected in next sample
437 Demod
.output
[Demod
.len
] = 0xfb;
440 else if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
444 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
445 Demod
.sub
= SUB_FIRST_HALF
;
448 Demod
.parityBits
= 0;
451 switch(Demod
.syncBit
) {
452 case 0x08: Demod
.samples
= 3; break;
453 case 0x04: Demod
.samples
= 2; break;
454 case 0x02: Demod
.samples
= 1; break;
455 case 0x01: Demod
.samples
= 0; break;
462 //modulation = bit & Demod.syncBit;
463 modulation
= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
467 if(Demod
.posCount
==0) {
470 Demod
.sub
= SUB_FIRST_HALF
;
473 Demod
.sub
= SUB_NONE
;
478 if(modulation
&& (Demod
.sub
== SUB_FIRST_HALF
)) {
479 if(Demod
.state
!=DEMOD_ERROR_WAIT
) {
480 Demod
.state
= DEMOD_ERROR_WAIT
;
481 Demod
.output
[Demod
.len
] = 0xaa;
485 else if(modulation
) {
486 Demod
.sub
= SUB_SECOND_HALF
;
489 switch(Demod
.state
) {
490 case DEMOD_START_OF_COMMUNICATION
:
491 if(Demod
.sub
== SUB_FIRST_HALF
) {
492 Demod
.state
= DEMOD_MANCHESTER_D
;
495 Demod
.output
[Demod
.len
] = 0xab;
496 Demod
.state
= DEMOD_ERROR_WAIT
;
501 case DEMOD_MANCHESTER_D
:
502 case DEMOD_MANCHESTER_E
:
503 if(Demod
.sub
== SUB_FIRST_HALF
) {
505 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
506 Demod
.state
= DEMOD_MANCHESTER_D
;
508 else if(Demod
.sub
== SUB_SECOND_HALF
) {
510 Demod
.shiftReg
>>= 1;
511 Demod
.state
= DEMOD_MANCHESTER_E
;
514 Demod
.state
= DEMOD_MANCHESTER_F
;
518 case DEMOD_MANCHESTER_F
:
519 // Tag response does not need to be a complete byte!
520 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
521 if(Demod
.bitCount
> 0) {
522 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
523 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
525 // No parity bit, so just shift a 0
526 Demod
.parityBits
<<= 1;
529 Demod
.state
= DEMOD_UNSYNCD
;
533 Demod
.output
[Demod
.len
] = 0xad;
534 Demod
.state
= DEMOD_ERROR_WAIT
;
539 case DEMOD_ERROR_WAIT
:
540 Demod
.state
= DEMOD_UNSYNCD
;
544 Demod
.output
[Demod
.len
] = 0xdd;
545 Demod
.state
= DEMOD_UNSYNCD
;
549 if(Demod
.bitCount
>=9) {
550 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
553 Demod
.parityBits
<<= 1;
554 Demod
.parityBits
^= ((Demod
.shiftReg
>> 8) & 0x01);
561 Demod.output[Demod.len] = 0xBB;
563 Demod.output[Demod.len] = error & 0xFF;
565 Demod.output[Demod.len] = 0xBB;
567 Demod.output[Demod.len] = bit & 0xFF;
569 Demod.output[Demod.len] = Demod.buffer & 0xFF;
571 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
573 Demod.output[Demod.len] = 0xBB;
580 } // end (state != UNSYNCED)
585 //=============================================================================
586 // Finally, a `sniffer' for ISO 14443 Type A
587 // Both sides of communication!
588 //=============================================================================
590 //-----------------------------------------------------------------------------
591 // Record the sequence of commands sent by the reader to the tag, with
592 // triggering so that we start recording at the point that the tag is moved
594 //-----------------------------------------------------------------------------
595 void SnoopIso14443a(void)
597 // #define RECV_CMD_OFFSET 2032 // original (working as of 21/2/09) values
598 // #define RECV_RES_OFFSET 2096 // original (working as of 21/2/09) values
599 // #define DMA_BUFFER_OFFSET 2160 // original (working as of 21/2/09) values
600 // #define DMA_BUFFER_SIZE 4096 // original (working as of 21/2/09) values
601 // #define TRACE_LENGTH 2000 // original (working as of 21/2/09) values
603 // We won't start recording the frames that we acquire until we trigger;
604 // a good trigger condition to get started is probably when we see a
605 // response from the tag.
606 int triggered
= TRUE
; // FALSE to wait first for card
608 // The command (reader -> tag) that we're receiving.
609 // The length of a received command will in most cases be no more than 18 bytes.
610 // So 32 should be enough!
611 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
612 // The response (tag -> reader) that we're receiving.
613 uint8_t *receivedResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
615 // As we receive stuff, we copy it from receivedCmd or receivedResponse
616 // into trace, along with its length and other annotations.
617 //uint8_t *trace = (uint8_t *)BigBuf;
620 // The DMA buffer, used to stream samples from the FPGA
621 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
627 // Count of samples received so far, so that we can include timing
628 // information in the trace buffer.
632 memset(trace
, 0x44, RECV_CMD_OFFSET
);
634 // Set up the demodulator for tag -> reader responses.
635 Demod
.output
= receivedResponse
;
637 Demod
.state
= DEMOD_UNSYNCD
;
639 // And the reader -> tag commands
640 memset(&Uart
, 0, sizeof(Uart
));
641 Uart
.output
= receivedCmd
;
642 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
643 Uart
.state
= STATE_UNSYNCD
;
645 // And put the FPGA in the appropriate mode
646 // Signal field is off with the appropriate LED
648 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
649 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
651 // Setup for the DMA.
654 lastRxCounter
= DMA_BUFFER_SIZE
;
655 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
659 // And now we loop, receiving samples.
662 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
664 if(behindBy
> maxBehindBy
) {
665 maxBehindBy
= behindBy
;
667 DbpString("blew circular buffer!");
671 if(behindBy
< 1) continue;
676 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
677 upTo
-= DMA_BUFFER_SIZE
;
678 lastRxCounter
+= DMA_BUFFER_SIZE
;
679 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
680 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
684 #define HANDLE_BIT_IF_BODY \
687 trace[traceLen++] = ((rsamples >> 0) & 0xff); \
688 trace[traceLen++] = ((rsamples >> 8) & 0xff); \
689 trace[traceLen++] = ((rsamples >> 16) & 0xff); \
690 trace[traceLen++] = ((rsamples >> 24) & 0xff); \
691 trace[traceLen++] = ((Uart.parityBits >> 0) & 0xff); \
692 trace[traceLen++] = ((Uart.parityBits >> 8) & 0xff); \
693 trace[traceLen++] = ((Uart.parityBits >> 16) & 0xff); \
694 trace[traceLen++] = ((Uart.parityBits >> 24) & 0xff); \
695 trace[traceLen++] = Uart.byteCnt; \
696 memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \
697 traceLen += Uart.byteCnt; \
698 if(traceLen > TRACE_LENGTH) break; \
700 /* And ready to receive another command. */ \
701 Uart.state = STATE_UNSYNCD; \
702 /* And also reset the demod code, which might have been */ \
703 /* false-triggered by the commands from the reader. */ \
704 Demod.state = DEMOD_UNSYNCD; \
707 if(MillerDecoding((smpl & 0xF0) >> 4)) {
708 rsamples
= samples
- Uart
.samples
;
711 if(ManchesterDecoding(smpl
& 0x0F)) {
712 rsamples
= samples
- Demod
.samples
;
715 // timestamp, as a count of samples
716 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
717 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
718 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
719 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
720 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
721 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
722 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
723 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
725 trace
[traceLen
++] = Demod
.len
;
726 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
727 traceLen
+= Demod
.len
;
728 if(traceLen
> TRACE_LENGTH
) break;
732 // And ready to receive another response.
733 memset(&Demod
, 0, sizeof(Demod
));
734 Demod
.output
= receivedResponse
;
735 Demod
.state
= DEMOD_UNSYNCD
;
740 DbpString("cancelled_a");
745 DbpString("COMMAND FINISHED");
747 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
748 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
751 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
752 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
753 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
760 // Prepare communication bits to send to FPGA
761 void Sequence(SecType seq
)
767 // Sequence D: 11110000
768 // modulation with subcarrier during first half
769 ToSend
[ToSendMax
] = 0xf0;
772 // Sequence E: 00001111
773 // modulation with subcarrier during second half
774 ToSend
[ToSendMax
] = 0x0f;
777 // Sequence F: 00000000
778 // no modulation with subcarrier
779 ToSend
[ToSendMax
] = 0x00;
783 // Sequence X: 00001100
784 // drop after half a period
785 ToSend
[ToSendMax
] = 0x0c;
789 // Sequence Y: 00000000
791 ToSend
[ToSendMax
] = 0x00;
794 // Sequence Z: 11000000
796 ToSend
[ToSendMax
] = 0xc0;
801 //-----------------------------------------------------------------------------
802 // Prepare tag messages
803 //-----------------------------------------------------------------------------
804 static void CodeIso14443aAsTag(const uint8_t *cmd
, int len
)
811 // Correction bit, might be removed when not needed
816 ToSendStuffBit(1); // 1
824 for(i
= 0; i
< len
; i
++) {
830 for(j
= 0; j
< 8; j
++) {
831 oddparity
^= (b
& 1);
851 // Flush the buffer in FPGA!!
852 for(i
= 0; i
< 5; i
++) {
856 // Convert from last byte pos to length
859 // Add a few more for slop
860 ToSend
[ToSendMax
++] = 0x00;
861 ToSend
[ToSendMax
++] = 0x00;
865 //-----------------------------------------------------------------------------
866 // This is to send a NACK kind of answer, its only 3 bits, I know it should be 4
867 //-----------------------------------------------------------------------------
868 static void CodeStrangeAnswer()
874 // Correction bit, might be removed when not needed
879 ToSendStuffBit(1); // 1
899 // Flush the buffer in FPGA!!
900 for(i
= 0; i
< 5; i
++) {
904 // Convert from last byte pos to length
907 // Add a few more for slop
908 ToSend
[ToSendMax
++] = 0x00;
909 ToSend
[ToSendMax
++] = 0x00;
913 //-----------------------------------------------------------------------------
914 // Wait for commands from reader
915 // Stop when button is pressed
916 // Or return TRUE when command is captured
917 //-----------------------------------------------------------------------------
918 static int GetIso14443aCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
920 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
921 // only, since we are receiving, not transmitting).
922 // Signal field is off with the appropriate LED
924 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
926 // Now run a `software UART' on the stream of incoming samples.
927 Uart
.output
= received
;
928 Uart
.byteCntMax
= maxLen
;
929 Uart
.state
= STATE_UNSYNCD
;
934 if(BUTTON_PRESS()) return FALSE
;
936 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
937 AT91C_BASE_SSC
->SSC_THR
= 0x00;
939 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
940 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
941 if(MillerDecoding((b
& 0xf0) >> 4)) {
945 if(MillerDecoding(b
& 0x0f)) {
953 //-----------------------------------------------------------------------------
954 // Main loop of simulated tag: receive commands from reader, decide what
955 // response to send, and send it.
956 //-----------------------------------------------------------------------------
957 void SimulateIso14443aTag(int tagType
, int TagUid
)
959 // This function contains the tag emulation
961 // Prepare protocol messages
962 // static const uint8_t cmd1[] = { 0x26 };
963 // static const uint8_t response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg
965 static const uint8_t response1
[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me
966 // static const uint8_t response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me
969 // static const uint8_t cmd2[] = { 0x93, 0x20 };
970 //static const uint8_t response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg
975 static const uint8_t response2
[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips
978 // When reader selects us during cascade1 it will send cmd3
979 //uint8_t response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE)
980 uint8_t response3
[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire)
981 ComputeCrc14443(CRC_14443_A
, response3
, 1, &response3
[1], &response3
[2]);
983 // send cascade2 2nd half of UID
984 static const uint8_t response2a
[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck
985 // NOTE : THE CRC on the above may be wrong as I have obfuscated the actual UID
988 // When reader selects us during cascade2 it will send cmd3a
989 //uint8_t response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE)
990 uint8_t response3a
[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire)
991 ComputeCrc14443(CRC_14443_A
, response3a
, 1, &response3a
[1], &response3a
[2]);
993 static const uint8_t response5
[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce
998 // Longest possible response will be 16 bytes + 2 CRC = 18 bytes
1000 // 144 data bits (18 * 8)
1003 // 1 Correction bit (Answer in 1172 or 1236 periods, see FPGA)
1004 // 1 just for the case
1008 // 166 bytes, since every bit that needs to be send costs us a byte
1012 // Respond with card type
1013 uint8_t *resp1
= (((uint8_t *)BigBuf
) + 800);
1016 // Anticollision cascade1 - respond with uid
1017 uint8_t *resp2
= (((uint8_t *)BigBuf
) + 970);
1020 // Anticollision cascade2 - respond with 2nd half of uid if asked
1021 // we're only going to be asked if we set the 1st byte of the UID (during cascade1) to 0x88
1022 uint8_t *resp2a
= (((uint8_t *)BigBuf
) + 1140);
1025 // Acknowledge select - cascade 1
1026 uint8_t *resp3
= (((uint8_t *)BigBuf
) + 1310);
1029 // Acknowledge select - cascade 2
1030 uint8_t *resp3a
= (((uint8_t *)BigBuf
) + 1480);
1033 // Response to a read request - not implemented atm
1034 uint8_t *resp4
= (((uint8_t *)BigBuf
) + 1550);
1037 // Authenticate response - nonce
1038 uint8_t *resp5
= (((uint8_t *)BigBuf
) + 1720);
1041 uint8_t *receivedCmd
= (uint8_t *)BigBuf
;
1048 // To control where we are in the protocol
1052 // Just to allow some checks
1060 memset(receivedCmd
, 0x44, 400);
1062 // Prepare the responses of the anticollision phase
1063 // there will be not enough time to do this at the moment the reader sends it REQA
1065 // Answer to request
1066 CodeIso14443aAsTag(response1
, sizeof(response1
));
1067 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1069 // Send our UID (cascade 1)
1070 CodeIso14443aAsTag(response2
, sizeof(response2
));
1071 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1073 // Answer to select (cascade1)
1074 CodeIso14443aAsTag(response3
, sizeof(response3
));
1075 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1077 // Send the cascade 2 2nd part of the uid
1078 CodeIso14443aAsTag(response2a
, sizeof(response2a
));
1079 memcpy(resp2a
, ToSend
, ToSendMax
); resp2aLen
= ToSendMax
;
1081 // Answer to select (cascade 2)
1082 CodeIso14443aAsTag(response3a
, sizeof(response3a
));
1083 memcpy(resp3a
, ToSend
, ToSendMax
); resp3aLen
= ToSendMax
;
1085 // Strange answer is an example of rare message size (3 bits)
1086 CodeStrangeAnswer();
1087 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1089 // Authentication answer (random nonce)
1090 CodeIso14443aAsTag(response5
, sizeof(response5
));
1091 memcpy(resp5
, ToSend
, ToSendMax
); resp5Len
= ToSendMax
;
1093 // We need to listen to the high-frequency, peak-detected path.
1094 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1102 if(!GetIso14443aCommandFromReader(receivedCmd
, &len
, 100)) {
1103 DbpString("button press");
1106 // 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
1107 // Okay, look at the command now.
1109 i
= 1; // first byte transmitted
1110 if(receivedCmd
[0] == 0x26) {
1111 // Received a REQUEST
1112 resp
= resp1
; respLen
= resp1Len
; order
= 1;
1113 //DbpString("Hello request from reader:");
1114 } else if(receivedCmd
[0] == 0x52) {
1115 // Received a WAKEUP
1116 resp
= resp1
; respLen
= resp1Len
; order
= 6;
1117 // //DbpString("Wakeup request from reader:");
1119 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] == 0x93) { // greg - cascade 1 anti-collision
1120 // Received request for UID (cascade 1)
1121 resp
= resp2
; respLen
= resp2Len
; order
= 2;
1122 // DbpString("UID (cascade 1) request from reader:");
1123 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1126 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] ==0x95) { // greg - cascade 2 anti-collision
1127 // Received request for UID (cascade 2)
1128 resp
= resp2a
; respLen
= resp2aLen
; order
= 20;
1129 // DbpString("UID (cascade 2) request from reader:");
1130 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1133 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x93) { // greg - cascade 1 select
1134 // Received a SELECT
1135 resp
= resp3
; respLen
= resp3Len
; order
= 3;
1136 // DbpString("Select (cascade 1) request from reader:");
1137 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1140 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x95) { // greg - cascade 2 select
1141 // Received a SELECT
1142 resp
= resp3a
; respLen
= resp3aLen
; order
= 30;
1143 // DbpString("Select (cascade 2) request from reader:");
1144 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1147 } else if(receivedCmd
[0] == 0x30) {
1149 resp
= resp4
; respLen
= resp4Len
; order
= 4; // Do nothing
1150 Dbprintf("Read request from reader: %x %x %x",
1151 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1154 } else if(receivedCmd
[0] == 0x50) {
1156 resp
= resp1
; respLen
= 0; order
= 5; // Do nothing
1157 DbpString("Reader requested we HALT!:");
1159 } else if(receivedCmd
[0] == 0x60) {
1160 // Received an authentication request
1161 resp
= resp5
; respLen
= resp5Len
; order
= 7;
1162 Dbprintf("Authenticate request from reader: %x %x %x",
1163 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1165 } else if(receivedCmd
[0] == 0xE0) {
1166 // Received a RATS request
1167 resp
= resp1
; respLen
= 0;order
= 70;
1168 Dbprintf("RATS request from reader: %x %x %x",
1169 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1171 // Never seen this command before
1172 Dbprintf("Unknown command received from reader: %x %x %x %x %x %x %x %x %x",
1173 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1174 receivedCmd
[3], receivedCmd
[3], receivedCmd
[4],
1175 receivedCmd
[5], receivedCmd
[6], receivedCmd
[7]);
1177 resp
= resp1
; respLen
= 0; order
= 0;
1180 // Count number of wakeups received after a halt
1181 if(order
== 6 && lastorder
== 5) { happened
++; }
1183 // Count number of other messages after a halt
1184 if(order
!= 6 && lastorder
== 5) { happened2
++; }
1186 // Look at last parity bit to determine timing of answer
1187 if((Uart
.parityBits
& 0x01) || receivedCmd
[0] == 0x52) {
1188 // 1236, so correction bit needed
1192 memset(receivedCmd
, 0x44, 32);
1194 if(cmdsRecvd
> 999) {
1195 DbpString("1000 commands later...");
1202 if(respLen
<= 0) continue;
1204 // Modulate Manchester
1205 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_MOD
);
1206 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1209 // ### Transmit the response ###
1212 fdt_indicator
= FALSE
;
1214 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1215 volatile uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1218 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1226 AT91C_BASE_SSC
->SSC_THR
= b
;
1232 if(BUTTON_PRESS()) {
1239 Dbprintf("%x %x %x", happened
, happened2
, cmdsRecvd
);
1243 //-----------------------------------------------------------------------------
1244 // Transmit the command (to the tag) that was placed in ToSend[].
1245 //-----------------------------------------------------------------------------
1246 static void TransmitFor14443a(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1250 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1256 for(c
= 0; c
< *wait
;) {
1257 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1258 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1261 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1262 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1270 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1271 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
1277 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1278 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1283 if (samples
) *samples
= (c
+ *wait
) << 3;
1286 //-----------------------------------------------------------------------------
1287 // To generate an arbitrary stream from reader
1289 //-----------------------------------------------------------------------------
1290 void ArbitraryFromReader(const uint8_t *cmd
, int parity
, int len
)
1299 // Start of Communication (Seq. Z)
1303 for(i
= 0; i
< len
; i
++) {
1306 for(j
= 0; j
< 8; j
++) {
1326 // Predefined parity bit, the flipper flips when needed, because of flips in byte sent
1327 if(((parity
>> (len
- i
- 1)) & 1)) {
1344 // End of Communication
1362 // Convert from last character reference to length
1366 //-----------------------------------------------------------------------------
1367 // Code a 7-bit command without parity bit
1368 // This is especially for 0x26 and 0x52 (REQA and WUPA)
1369 //-----------------------------------------------------------------------------
1370 void ShortFrameFromReader(const uint8_t bt
)
1378 // Start of Communication (Seq. Z)
1383 for(j
= 0; j
< 7; j
++) {
1402 // End of Communication
1420 // Convert from last character reference to length
1424 //-----------------------------------------------------------------------------
1425 // Prepare reader command to send to FPGA
1427 //-----------------------------------------------------------------------------
1428 void CodeIso14443aAsReaderPar(const uint8_t * cmd
, int len
, uint32_t dwParity
)
1436 // Start of Communication (Seq. Z)
1440 // Generate send structure for the data bits
1441 for (i
= 0; i
< len
; i
++) {
1442 // Get the current byte to send
1445 for (j
= 0; j
< 8; j
++) {
1463 // Get the parity bit
1464 if ((dwParity
>> i
) & 0x01) {
1480 // End of Communication
1497 // Convert from last character reference to length
1501 //-----------------------------------------------------------------------------
1502 // Wait a certain time for tag response
1503 // If a response is captured return TRUE
1504 // If it takes to long return FALSE
1505 //-----------------------------------------------------------------------------
1506 static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1508 // buffer needs to be 512 bytes
1511 // Set FPGA mode to "reader listen mode", no modulation (listen
1512 // only, since we are receiving, not transmitting).
1513 // Signal field is on with the appropriate LED
1515 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1517 // Now get the answer from the card
1518 Demod
.output
= receivedResponse
;
1520 Demod
.state
= DEMOD_UNSYNCD
;
1523 if (elapsed
) *elapsed
= 0;
1529 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1530 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1531 if (elapsed
) (*elapsed
)++;
1533 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1534 if(c
< 512) { c
++; } else { return FALSE
; }
1535 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1536 if(ManchesterDecoding((b
& 0xf0) >> 4)) {
1537 *samples
= ((c
- 1) << 3) + 4;
1540 if(ManchesterDecoding(b
& 0x0f)) {
1548 void ReaderTransmitShort(const uint8_t* bt
)
1553 ShortFrameFromReader(*bt
);
1556 TransmitFor14443a(ToSend
, ToSendMax
, &samples
, &wait
);
1558 // Store reader command in buffer
1559 if (tracing
) LogTrace(bt
,1,0,GetParity(bt
,1),TRUE
);
1562 void ReaderTransmitPar(uint8_t* frame
, int len
, uint32_t par
)
1567 // This is tied to other size changes
1568 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1569 CodeIso14443aAsReaderPar(frame
,len
,par
);
1572 TransmitFor14443a(ToSend
, ToSendMax
, &samples
, &wait
);
1574 // Store reader command in buffer
1575 if (tracing
) LogTrace(frame
,len
,0,par
,TRUE
);
1579 void ReaderTransmit(uint8_t* frame
, int len
)
1581 // Generate parity and redirect
1582 ReaderTransmitPar(frame
,len
,GetParity(frame
,len
));
1585 int ReaderReceive(uint8_t* receivedAnswer
)
1588 if (!GetIso14443aAnswerFromTag(receivedAnswer
,100,&samples
,0)) return FALSE
;
1589 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
);
1593 //-----------------------------------------------------------------------------
1594 // Read an ISO 14443a tag. Send out commands and store answers.
1596 //-----------------------------------------------------------------------------
1597 void ReaderIso14443a(uint32_t parameter
)
1600 uint8_t wupa
[] = { 0x52 };
1601 uint8_t sel_all
[] = { 0x93,0x20 };
1602 uint8_t sel_uid
[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1603 uint8_t sel_all_c2
[] = { 0x95,0x20 };
1604 uint8_t sel_uid_c2
[] = { 0x95,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1607 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1608 // uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00 };
1610 uint8_t* receivedAnswer
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1616 // Start from off (no field generated)
1617 // Signal field is off with the appropriate LED
1619 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1622 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1625 // Now give it time to spin up.
1626 // Signal field is on with the appropriate LED
1628 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1635 while(traceLen
< TRACE_LENGTH
)
1637 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1638 ReaderTransmitShort(wupa
);
1640 // Test if the action was cancelled
1641 if(BUTTON_PRESS()) {
1646 if (!ReaderReceive(receivedAnswer
)) continue;
1648 // Transmit SELECT_ALL
1649 ReaderTransmit(sel_all
,sizeof(sel_all
));
1652 if (!ReaderReceive(receivedAnswer
)) continue;
1654 // Construct SELECT UID command
1655 // First copy the 5 bytes (Mifare Classic) after the 93 70
1656 memcpy(sel_uid
+2,receivedAnswer
,5);
1657 // Secondly compute the two CRC bytes at the end
1658 AppendCrc14443a(sel_uid
,7);
1660 // Transmit SELECT_UID
1661 ReaderTransmit(sel_uid
,sizeof(sel_uid
));
1664 if (!ReaderReceive(receivedAnswer
)) continue;
1666 // OK we have selected at least at cascade 1, lets see if first byte of UID was 0x88 in
1667 // which case we need to make a cascade 2 request and select - this is a long UID
1668 // When the UID is not complete, the 3nd bit (from the right) is set in the SAK.
1669 if (receivedAnswer
[0] &= 0x04)
1671 // Transmit SELECT_ALL
1672 ReaderTransmit(sel_all_c2
,sizeof(sel_all_c2
));
1675 if (!ReaderReceive(receivedAnswer
)) continue;
1677 // Construct SELECT UID command
1678 memcpy(sel_uid_c2
+2,receivedAnswer
,5);
1679 // Secondly compute the two CRC bytes at the end
1680 AppendCrc14443a(sel_uid_c2
,7);
1682 // Transmit SELECT_UID
1683 ReaderTransmit(sel_uid_c2
,sizeof(sel_uid_c2
));
1686 if (!ReaderReceive(receivedAnswer
)) continue;
1689 // Transmit MIFARE_CLASSIC_AUTH
1690 ReaderTransmit(mf_auth
,sizeof(mf_auth
));
1692 // Receive the (16 bit) "random" nonce
1693 if (!ReaderReceive(receivedAnswer
)) continue;
1697 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1699 Dbprintf("%x %x %x", rsamples
, 0xCC, 0xCC);
1700 DbpString("ready..");
1703 //-----------------------------------------------------------------------------
1704 // Read an ISO 14443a tag. Send out commands and store answers.
1706 //-----------------------------------------------------------------------------
1707 void ReaderMifare(uint32_t parameter
)
1711 uint8_t wupa
[] = { 0x52 };
1712 uint8_t sel_all
[] = { 0x93,0x20 };
1713 uint8_t sel_uid
[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1716 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1717 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1719 uint8_t* receivedAnswer
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1726 // Start from off (no field generated)
1727 // Signal field is off with the appropriate LED
1729 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1732 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1735 // Now give it time to spin up.
1736 // Signal field is on with the appropriate LED
1738 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1745 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1746 ReaderTransmitShort(wupa
);
1748 ReaderReceive(receivedAnswer
);
1749 // Transmit SELECT_ALL
1750 ReaderTransmit(sel_all
,sizeof(sel_all
));
1752 ReaderReceive(receivedAnswer
);
1753 // Construct SELECT UID command
1754 // First copy the 5 bytes (Mifare Classic) after the 93 70
1755 memcpy(sel_uid
+2,receivedAnswer
,5);
1756 // Secondly compute the two CRC bytes at the end
1757 AppendCrc14443a(sel_uid
,7);
1762 byte_t par_mask
= 0xff;
1768 byte_t nt_attacked
[4];
1771 num_to_bytes(parameter
,4,nt_attacked
);
1775 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1777 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1779 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1780 ReaderTransmitShort(wupa
);
1782 // Test if the action was cancelled
1783 if(BUTTON_PRESS()) {
1788 if (!ReaderReceive(receivedAnswer
)) continue;
1790 // Transmit SELECT_ALL
1791 ReaderTransmit(sel_all
,sizeof(sel_all
));
1794 if (!ReaderReceive(receivedAnswer
)) continue;
1796 // Transmit SELECT_UID
1797 ReaderTransmit(sel_uid
,sizeof(sel_uid
));
1800 if (!ReaderReceive(receivedAnswer
)) continue;
1802 // Transmit MIFARE_CLASSIC_AUTH
1803 ReaderTransmit(mf_auth
,sizeof(mf_auth
));
1805 // Receive the (16 bit) "random" nonce
1806 if (!ReaderReceive(receivedAnswer
)) continue;
1807 memcpy(nt
,receivedAnswer
,4);
1809 // Transmit reader nonce and reader answer
1810 ReaderTransmitPar(mf_nr_ar
,sizeof(mf_nr_ar
),par
);
1812 // Receive 4 bit answer
1813 if (ReaderReceive(receivedAnswer
))
1818 memcpy(nt_attacked
,nt
,4);
1820 par_low
= par
& 0x07;
1823 if (memcmp(nt
,nt_attacked
,4) != 0) continue;
1826 if(led_on
) LED_B_ON(); else LED_B_OFF();
1827 par_list
[nt_diff
] = par
;
1828 ks_list
[nt_diff
] = receivedAnswer
[0]^0x05;
1830 // Test if the information is complete
1831 if (nt_diff
== 0x07) break;
1833 nt_diff
= (nt_diff
+1) & 0x07;
1834 mf_nr_ar
[3] = nt_diff
<< 5;
1841 par
= (((par
>>3)+1) << 3) | par_low
;
1846 LogTraceInfo(sel_uid
+2,4);
1848 LogTraceInfo(par_list
,8);
1849 LogTraceInfo(ks_list
,8);
1852 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);