1 //-----------------------------------------------------------------------------
2 // Gerhard de Koning Gans - May 2008
3 // Hagen Fritsch - June 2010
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // Routines to support ISO 14443 type A.
10 //-----------------------------------------------------------------------------
12 #include "proxmark3.h"
17 #include "iso14443crc.h"
18 #include "iso14443a.h"
20 static uint8_t *trace
= (uint8_t *) BigBuf
;
21 static int traceLen
= 0;
22 static int rsamples
= 0;
23 static int tracing
= TRUE
;
24 static uint32_t iso14a_timeout
;
27 // Sequence D: 11110000 modulation with subcarrier during first half
28 // Sequence E: 00001111 modulation with subcarrier during second half
29 // Sequence F: 00000000 no modulation with subcarrier
31 // Sequence X: 00001100 drop after half a period
32 // Sequence Y: 00000000 no drop
33 // Sequence Z: 11000000 drop at start
41 static const uint8_t OddByteParity
[256] = {
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 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 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
48 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
49 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
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 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
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,
55 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
56 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
57 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
60 // BIG CHANGE - UNDERSTAND THIS BEFORE WE COMMIT
61 #define RECV_CMD_OFFSET 3032
62 #define RECV_RES_OFFSET 3096
63 #define DMA_BUFFER_OFFSET 3160
64 #define DMA_BUFFER_SIZE 4096
65 #define TRACE_LENGTH 3000
68 void iso14a_set_trigger(int enable
) {
72 //-----------------------------------------------------------------------------
73 // Generate the parity value for a byte sequence
75 //-----------------------------------------------------------------------------
76 uint32_t GetParity(const uint8_t * pbtCmd
, int iLen
)
81 // Generate the encrypted data
82 for (i
= 0; i
< iLen
; i
++) {
83 // Save the encrypted parity bit
84 dwPar
|= ((OddByteParity
[pbtCmd
[i
]]) << i
);
89 void AppendCrc14443a(uint8_t* data
, int len
)
91 ComputeCrc14443(CRC_14443_A
,data
,len
,data
+len
,data
+len
+1);
94 int LogTrace(const uint8_t * btBytes
, int iLen
, int iSamples
, uint32_t dwParity
, int bReader
)
96 // Return when trace is full
97 if (traceLen
>= TRACE_LENGTH
) return FALSE
;
99 // Trace the random, i'm curious
100 rsamples
+= iSamples
;
101 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
102 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
103 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
104 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
106 trace
[traceLen
- 1] |= 0x80;
108 trace
[traceLen
++] = ((dwParity
>> 0) & 0xff);
109 trace
[traceLen
++] = ((dwParity
>> 8) & 0xff);
110 trace
[traceLen
++] = ((dwParity
>> 16) & 0xff);
111 trace
[traceLen
++] = ((dwParity
>> 24) & 0xff);
112 trace
[traceLen
++] = iLen
;
113 memcpy(trace
+ traceLen
, btBytes
, iLen
);
118 //-----------------------------------------------------------------------------
119 // The software UART that receives commands from the reader, and its state
121 //-----------------------------------------------------------------------------
125 STATE_START_OF_COMMUNICATION
,
149 static RAMFUNC
int MillerDecoding(int bit
)
154 if(!Uart
.bitBuffer
) {
155 Uart
.bitBuffer
= bit
^ 0xFF0;
159 Uart
.bitBuffer
<<= 4;
160 Uart
.bitBuffer
^= bit
;
165 if(Uart
.state
!= STATE_UNSYNCD
) {
168 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
174 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
180 if(bit
!= bitright
) { bit
= bitright
; }
182 if(Uart
.posCnt
== 1) {
183 // measurement first half bitperiod
185 Uart
.drop
= DROP_FIRST_HALF
;
189 // measurement second half bitperiod
190 if(!bit
& (Uart
.drop
== DROP_NONE
)) {
191 Uart
.drop
= DROP_SECOND_HALF
;
194 // measured a drop in first and second half
195 // which should not be possible
196 Uart
.state
= STATE_ERROR_WAIT
;
203 case STATE_START_OF_COMMUNICATION
:
205 if(Uart
.drop
== DROP_SECOND_HALF
) {
206 // error, should not happen in SOC
207 Uart
.state
= STATE_ERROR_WAIT
;
212 Uart
.state
= STATE_MILLER_Z
;
219 if(Uart
.drop
== DROP_NONE
) {
220 // logic '0' followed by sequence Y
221 // end of communication
222 Uart
.state
= STATE_UNSYNCD
;
225 // if(Uart.drop == DROP_FIRST_HALF) {
226 // Uart.state = STATE_MILLER_Z; stay the same
227 // we see a logic '0' }
228 if(Uart
.drop
== DROP_SECOND_HALF
) {
229 // we see a logic '1'
230 Uart
.shiftReg
|= 0x100;
231 Uart
.state
= STATE_MILLER_X
;
237 if(Uart
.drop
== DROP_NONE
) {
238 // sequence Y, we see a '0'
239 Uart
.state
= STATE_MILLER_Y
;
242 if(Uart
.drop
== DROP_FIRST_HALF
) {
243 // Would be STATE_MILLER_Z
244 // but Z does not follow X, so error
245 Uart
.state
= STATE_ERROR_WAIT
;
248 if(Uart
.drop
== DROP_SECOND_HALF
) {
249 // We see a '1' and stay in state X
250 Uart
.shiftReg
|= 0x100;
258 if(Uart
.drop
== DROP_NONE
) {
259 // logic '0' followed by sequence Y
260 // end of communication
261 Uart
.state
= STATE_UNSYNCD
;
264 if(Uart
.drop
== DROP_FIRST_HALF
) {
266 Uart
.state
= STATE_MILLER_Z
;
268 if(Uart
.drop
== DROP_SECOND_HALF
) {
269 // We see a '1' and go to state X
270 Uart
.shiftReg
|= 0x100;
271 Uart
.state
= STATE_MILLER_X
;
275 case STATE_ERROR_WAIT
:
276 // That went wrong. Now wait for at least two bit periods
277 // and try to sync again
278 if(Uart
.drop
== DROP_NONE
) {
280 Uart
.state
= STATE_UNSYNCD
;
285 Uart
.state
= STATE_UNSYNCD
;
290 Uart
.drop
= DROP_NONE
;
292 // should have received at least one whole byte...
293 if((Uart
.bitCnt
== 2) && EOC
&& (Uart
.byteCnt
> 0)) {
297 if(Uart
.bitCnt
== 9) {
298 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
301 Uart
.parityBits
<<= 1;
302 Uart
.parityBits
^= ((Uart
.shiftReg
>> 8) & 0x01);
305 // when End of Communication received and
306 // all data bits processed..
313 Uart.output[Uart.byteCnt] = 0xAA;
315 Uart.output[Uart.byteCnt] = error & 0xFF;
317 Uart.output[Uart.byteCnt] = 0xAA;
319 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
321 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
323 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
325 Uart.output[Uart.byteCnt] = 0xAA;
333 bit
= Uart
.bitBuffer
& 0xf0;
337 // should have been high or at least (4 * 128) / fc
338 // according to ISO this should be at least (9 * 128 + 20) / fc
339 if(Uart
.highCnt
== 8) {
340 // we went low, so this could be start of communication
341 // it turns out to be safer to choose a less significant
342 // syncbit... so we check whether the neighbour also represents the drop
343 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
344 Uart
.syncBit
= bit
& 8;
346 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
347 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
348 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
349 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
350 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
351 if(Uart
.syncBit
&& (Uart
.bitBuffer
& 8)) {
354 // the first half bit period is expected in next sample
359 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
362 Uart
.state
= STATE_START_OF_COMMUNICATION
;
363 Uart
.drop
= DROP_FIRST_HALF
;
374 if(Uart
.highCnt
< 8) {
383 //=============================================================================
384 // ISO 14443 Type A - Manchester
385 //=============================================================================
390 DEMOD_START_OF_COMMUNICATION
,
413 static RAMFUNC
int ManchesterDecoding(int v
)
429 if(Demod
.state
==DEMOD_UNSYNCD
) {
430 Demod
.output
[Demod
.len
] = 0xfa;
433 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
436 Demod
.syncBit
= 0x08;
443 Demod
.syncBit
= 0x04;
450 Demod
.syncBit
= 0x02;
453 if(bit
& 0x01 && Demod
.syncBit
) {
454 Demod
.syncBit
= 0x01;
459 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
460 Demod
.sub
= SUB_FIRST_HALF
;
463 Demod
.parityBits
= 0;
466 if(trigger
) LED_A_OFF();
467 switch(Demod
.syncBit
) {
468 case 0x08: Demod
.samples
= 3; break;
469 case 0x04: Demod
.samples
= 2; break;
470 case 0x02: Demod
.samples
= 1; break;
471 case 0x01: Demod
.samples
= 0; break;
478 //modulation = bit & Demod.syncBit;
479 modulation
= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
483 if(Demod
.posCount
==0) {
486 Demod
.sub
= SUB_FIRST_HALF
;
489 Demod
.sub
= SUB_NONE
;
494 if(modulation
&& (Demod
.sub
== SUB_FIRST_HALF
)) {
495 if(Demod
.state
!=DEMOD_ERROR_WAIT
) {
496 Demod
.state
= DEMOD_ERROR_WAIT
;
497 Demod
.output
[Demod
.len
] = 0xaa;
501 else if(modulation
) {
502 Demod
.sub
= SUB_SECOND_HALF
;
505 switch(Demod
.state
) {
506 case DEMOD_START_OF_COMMUNICATION
:
507 if(Demod
.sub
== SUB_FIRST_HALF
) {
508 Demod
.state
= DEMOD_MANCHESTER_D
;
511 Demod
.output
[Demod
.len
] = 0xab;
512 Demod
.state
= DEMOD_ERROR_WAIT
;
517 case DEMOD_MANCHESTER_D
:
518 case DEMOD_MANCHESTER_E
:
519 if(Demod
.sub
== SUB_FIRST_HALF
) {
521 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
522 Demod
.state
= DEMOD_MANCHESTER_D
;
524 else if(Demod
.sub
== SUB_SECOND_HALF
) {
526 Demod
.shiftReg
>>= 1;
527 Demod
.state
= DEMOD_MANCHESTER_E
;
530 Demod
.state
= DEMOD_MANCHESTER_F
;
534 case DEMOD_MANCHESTER_F
:
535 // Tag response does not need to be a complete byte!
536 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
537 if(Demod
.bitCount
> 0) {
538 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
539 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
541 // No parity bit, so just shift a 0
542 Demod
.parityBits
<<= 1;
545 Demod
.state
= DEMOD_UNSYNCD
;
549 Demod
.output
[Demod
.len
] = 0xad;
550 Demod
.state
= DEMOD_ERROR_WAIT
;
555 case DEMOD_ERROR_WAIT
:
556 Demod
.state
= DEMOD_UNSYNCD
;
560 Demod
.output
[Demod
.len
] = 0xdd;
561 Demod
.state
= DEMOD_UNSYNCD
;
565 if(Demod
.bitCount
>=9) {
566 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
569 Demod
.parityBits
<<= 1;
570 Demod
.parityBits
^= ((Demod
.shiftReg
>> 8) & 0x01);
577 Demod.output[Demod.len] = 0xBB;
579 Demod.output[Demod.len] = error & 0xFF;
581 Demod.output[Demod.len] = 0xBB;
583 Demod.output[Demod.len] = bit & 0xFF;
585 Demod.output[Demod.len] = Demod.buffer & 0xFF;
587 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
589 Demod.output[Demod.len] = 0xBB;
596 } // end (state != UNSYNCED)
601 //=============================================================================
602 // Finally, a `sniffer' for ISO 14443 Type A
603 // Both sides of communication!
604 //=============================================================================
606 //-----------------------------------------------------------------------------
607 // Record the sequence of commands sent by the reader to the tag, with
608 // triggering so that we start recording at the point that the tag is moved
610 //-----------------------------------------------------------------------------
611 void RAMFUNC
SnoopIso14443a(void)
613 // #define RECV_CMD_OFFSET 2032 // original (working as of 21/2/09) values
614 // #define RECV_RES_OFFSET 2096 // original (working as of 21/2/09) values
615 // #define DMA_BUFFER_OFFSET 2160 // original (working as of 21/2/09) values
616 // #define DMA_BUFFER_SIZE 4096 // original (working as of 21/2/09) values
617 // #define TRACE_LENGTH 2000 // original (working as of 21/2/09) values
619 // We won't start recording the frames that we acquire until we trigger;
620 // a good trigger condition to get started is probably when we see a
621 // response from the tag.
622 int triggered
= FALSE
; // FALSE to wait first for card
624 // The command (reader -> tag) that we're receiving.
625 // The length of a received command will in most cases be no more than 18 bytes.
626 // So 32 should be enough!
627 uint8_t *receivedCmd
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
);
628 // The response (tag -> reader) that we're receiving.
629 uint8_t *receivedResponse
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
);
631 // As we receive stuff, we copy it from receivedCmd or receivedResponse
632 // into trace, along with its length and other annotations.
633 //uint8_t *trace = (uint8_t *)BigBuf;
635 traceLen
= 0; // uncommented to fix ISSUE 15 - gerhard - jan2011
637 // The DMA buffer, used to stream samples from the FPGA
638 int8_t *dmaBuf
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
;
644 // Count of samples received so far, so that we can include timing
645 // information in the trace buffer.
649 memset(trace
, 0x44, RECV_CMD_OFFSET
);
651 // Set up the demodulator for tag -> reader responses.
652 Demod
.output
= receivedResponse
;
654 Demod
.state
= DEMOD_UNSYNCD
;
656 // Setup for the DMA.
659 lastRxCounter
= DMA_BUFFER_SIZE
;
660 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
662 // And the reader -> tag commands
663 memset(&Uart
, 0, sizeof(Uart
));
664 Uart
.output
= receivedCmd
;
665 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
666 Uart
.state
= STATE_UNSYNCD
;
668 // And put the FPGA in the appropriate mode
669 // Signal field is off with the appropriate LED
671 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
672 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
675 // And now we loop, receiving samples.
679 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
681 if(behindBy
> maxBehindBy
) {
682 maxBehindBy
= behindBy
;
684 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
688 if(behindBy
< 1) continue;
694 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
695 upTo
-= DMA_BUFFER_SIZE
;
696 lastRxCounter
+= DMA_BUFFER_SIZE
;
697 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
698 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
702 if(MillerDecoding((smpl
& 0xF0) >> 4)) {
703 rsamples
= samples
- Uart
.samples
;
706 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
707 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
708 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
709 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
710 trace
[traceLen
++] = ((Uart
.parityBits
>> 0) & 0xff);
711 trace
[traceLen
++] = ((Uart
.parityBits
>> 8) & 0xff);
712 trace
[traceLen
++] = ((Uart
.parityBits
>> 16) & 0xff);
713 trace
[traceLen
++] = ((Uart
.parityBits
>> 24) & 0xff);
714 trace
[traceLen
++] = Uart
.byteCnt
;
715 memcpy(trace
+traceLen
, receivedCmd
, Uart
.byteCnt
);
716 traceLen
+= Uart
.byteCnt
;
717 if(traceLen
> TRACE_LENGTH
) break;
719 /* And ready to receive another command. */
720 Uart
.state
= STATE_UNSYNCD
;
721 /* And also reset the demod code, which might have been */
722 /* false-triggered by the commands from the reader. */
723 Demod
.state
= DEMOD_UNSYNCD
;
727 if(ManchesterDecoding(smpl
& 0x0F)) {
728 rsamples
= samples
- Demod
.samples
;
731 // timestamp, as a count of samples
732 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
733 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
734 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
735 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
736 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
737 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
738 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
739 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
741 trace
[traceLen
++] = Demod
.len
;
742 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
743 traceLen
+= Demod
.len
;
744 if(traceLen
> TRACE_LENGTH
) break;
748 // And ready to receive another response.
749 memset(&Demod
, 0, sizeof(Demod
));
750 Demod
.output
= receivedResponse
;
751 Demod
.state
= DEMOD_UNSYNCD
;
756 DbpString("cancelled_a");
761 DbpString("COMMAND FINISHED");
763 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
764 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
767 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
768 Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
769 Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
776 //-----------------------------------------------------------------------------
777 // Prepare tag messages
778 //-----------------------------------------------------------------------------
779 static void CodeIso14443aAsTag(const uint8_t *cmd
, int len
)
786 // Correction bit, might be removed when not needed
791 ToSendStuffBit(1); // 1
797 ToSend
[++ToSendMax
] = SEC_D
;
799 for(i
= 0; i
< len
; i
++) {
805 for(j
= 0; j
< 8; j
++) {
806 oddparity
^= (b
& 1);
808 ToSend
[++ToSendMax
] = SEC_D
;
810 ToSend
[++ToSendMax
] = SEC_E
;
817 ToSend
[++ToSendMax
] = SEC_D
;
819 ToSend
[++ToSendMax
] = SEC_E
;
824 ToSend
[++ToSendMax
] = SEC_F
;
826 // Flush the buffer in FPGA!!
827 for(i
= 0; i
< 5; i
++) {
828 ToSend
[++ToSendMax
] = SEC_F
;
831 // Convert from last byte pos to length
834 // Add a few more for slop
835 ToSend
[ToSendMax
++] = 0x00;
836 ToSend
[ToSendMax
++] = 0x00;
840 //-----------------------------------------------------------------------------
841 // This is to send a NACK kind of answer, its only 3 bits, I know it should be 4
842 //-----------------------------------------------------------------------------
843 static void CodeStrangeAnswer()
849 // Correction bit, might be removed when not needed
854 ToSendStuffBit(1); // 1
860 ToSend
[++ToSendMax
] = SEC_D
;
863 ToSend
[++ToSendMax
] = SEC_E
;
866 ToSend
[++ToSendMax
] = SEC_E
;
869 ToSend
[++ToSendMax
] = SEC_D
;
872 ToSend
[++ToSendMax
] = SEC_F
;
874 // Flush the buffer in FPGA!!
875 for(i
= 0; i
< 5; i
++) {
876 ToSend
[++ToSendMax
] = SEC_F
;
879 // Convert from last byte pos to length
882 // Add a few more for slop
883 ToSend
[ToSendMax
++] = 0x00;
884 ToSend
[ToSendMax
++] = 0x00;
888 //-----------------------------------------------------------------------------
889 // Wait for commands from reader
890 // Stop when button is pressed
891 // Or return TRUE when command is captured
892 //-----------------------------------------------------------------------------
893 static int GetIso14443aCommandFromReader(uint8_t *received
, int *len
, int maxLen
)
895 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
896 // only, since we are receiving, not transmitting).
897 // Signal field is off with the appropriate LED
899 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
901 // Now run a `software UART' on the stream of incoming samples.
902 Uart
.output
= received
;
903 Uart
.byteCntMax
= maxLen
;
904 Uart
.state
= STATE_UNSYNCD
;
909 if(BUTTON_PRESS()) return FALSE
;
911 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
912 AT91C_BASE_SSC
->SSC_THR
= 0x00;
914 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
915 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
916 if(MillerDecoding((b
& 0xf0) >> 4)) {
920 if(MillerDecoding(b
& 0x0f)) {
928 //-----------------------------------------------------------------------------
929 // Main loop of simulated tag: receive commands from reader, decide what
930 // response to send, and send it.
931 //-----------------------------------------------------------------------------
932 void SimulateIso14443aTag(int tagType
, int TagUid
)
934 // This function contains the tag emulation
936 // Prepare protocol messages
937 // static const uint8_t cmd1[] = { 0x26 };
938 // static const uint8_t response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg
940 static const uint8_t response1
[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me
941 // static const uint8_t response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me
944 // static const uint8_t cmd2[] = { 0x93, 0x20 };
945 //static const uint8_t response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg
948 static const uint8_t response2
[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips
951 // When reader selects us during cascade1 it will send cmd3
952 //uint8_t response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE)
953 uint8_t response3
[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire)
954 ComputeCrc14443(CRC_14443_A
, response3
, 1, &response3
[1], &response3
[2]);
956 // send cascade2 2nd half of UID
957 static const uint8_t response2a
[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck
958 // NOTE : THE CRC on the above may be wrong as I have obfuscated the actual UID
960 // When reader selects us during cascade2 it will send cmd3a
961 //uint8_t response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE)
962 uint8_t response3a
[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire)
963 ComputeCrc14443(CRC_14443_A
, response3a
, 1, &response3a
[1], &response3a
[2]);
965 static const uint8_t response5
[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce
970 // Longest possible response will be 16 bytes + 2 CRC = 18 bytes
972 // 144 data bits (18 * 8)
975 // 1 Correction bit (Answer in 1172 or 1236 periods, see FPGA)
976 // 1 just for the case
980 // 166 bytes, since every bit that needs to be send costs us a byte
983 // Respond with card type
984 uint8_t *resp1
= (((uint8_t *)BigBuf
) + 800);
987 // Anticollision cascade1 - respond with uid
988 uint8_t *resp2
= (((uint8_t *)BigBuf
) + 970);
991 // Anticollision cascade2 - respond with 2nd half of uid if asked
992 // we're only going to be asked if we set the 1st byte of the UID (during cascade1) to 0x88
993 uint8_t *resp2a
= (((uint8_t *)BigBuf
) + 1140);
996 // Acknowledge select - cascade 1
997 uint8_t *resp3
= (((uint8_t *)BigBuf
) + 1310);
1000 // Acknowledge select - cascade 2
1001 uint8_t *resp3a
= (((uint8_t *)BigBuf
) + 1480);
1004 // Response to a read request - not implemented atm
1005 uint8_t *resp4
= (((uint8_t *)BigBuf
) + 1550);
1008 // Authenticate response - nonce
1009 uint8_t *resp5
= (((uint8_t *)BigBuf
) + 1720);
1012 uint8_t *receivedCmd
= (uint8_t *)BigBuf
;
1019 // To control where we are in the protocol
1023 // Just to allow some checks
1031 memset(receivedCmd
, 0x44, 400);
1033 // Prepare the responses of the anticollision phase
1034 // there will be not enough time to do this at the moment the reader sends it REQA
1036 // Answer to request
1037 CodeIso14443aAsTag(response1
, sizeof(response1
));
1038 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
1040 // Send our UID (cascade 1)
1041 CodeIso14443aAsTag(response2
, sizeof(response2
));
1042 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
1044 // Answer to select (cascade1)
1045 CodeIso14443aAsTag(response3
, sizeof(response3
));
1046 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1048 // Send the cascade 2 2nd part of the uid
1049 CodeIso14443aAsTag(response2a
, sizeof(response2a
));
1050 memcpy(resp2a
, ToSend
, ToSendMax
); resp2aLen
= ToSendMax
;
1052 // Answer to select (cascade 2)
1053 CodeIso14443aAsTag(response3a
, sizeof(response3a
));
1054 memcpy(resp3a
, ToSend
, ToSendMax
); resp3aLen
= ToSendMax
;
1056 // Strange answer is an example of rare message size (3 bits)
1057 CodeStrangeAnswer();
1058 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1060 // Authentication answer (random nonce)
1061 CodeIso14443aAsTag(response5
, sizeof(response5
));
1062 memcpy(resp5
, ToSend
, ToSendMax
); resp5Len
= ToSendMax
;
1064 // We need to listen to the high-frequency, peak-detected path.
1065 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1073 if(!GetIso14443aCommandFromReader(receivedCmd
, &len
, 100)) {
1074 DbpString("button press");
1077 // 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
1078 // Okay, look at the command now.
1080 i
= 1; // first byte transmitted
1081 if(receivedCmd
[0] == 0x26) {
1082 // Received a REQUEST
1083 resp
= resp1
; respLen
= resp1Len
; order
= 1;
1084 //DbpString("Hello request from reader:");
1085 } else if(receivedCmd
[0] == 0x52) {
1086 // Received a WAKEUP
1087 resp
= resp1
; respLen
= resp1Len
; order
= 6;
1088 // //DbpString("Wakeup request from reader:");
1090 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] == 0x93) { // greg - cascade 1 anti-collision
1091 // Received request for UID (cascade 1)
1092 resp
= resp2
; respLen
= resp2Len
; order
= 2;
1093 // DbpString("UID (cascade 1) request from reader:");
1094 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1097 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] ==0x95) { // greg - cascade 2 anti-collision
1098 // Received request for UID (cascade 2)
1099 resp
= resp2a
; respLen
= resp2aLen
; order
= 20;
1100 // DbpString("UID (cascade 2) request from reader:");
1101 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1104 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x93) { // greg - cascade 1 select
1105 // Received a SELECT
1106 resp
= resp3
; respLen
= resp3Len
; order
= 3;
1107 // DbpString("Select (cascade 1) request from reader:");
1108 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1111 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x95) { // greg - cascade 2 select
1112 // Received a SELECT
1113 resp
= resp3a
; respLen
= resp3aLen
; order
= 30;
1114 // DbpString("Select (cascade 2) request from reader:");
1115 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1118 } else if(receivedCmd
[0] == 0x30) {
1120 resp
= resp4
; respLen
= resp4Len
; order
= 4; // Do nothing
1121 Dbprintf("Read request from reader: %x %x %x",
1122 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1125 } else if(receivedCmd
[0] == 0x50) {
1127 resp
= resp1
; respLen
= 0; order
= 5; // Do nothing
1128 DbpString("Reader requested we HALT!:");
1130 } else if(receivedCmd
[0] == 0x60) {
1131 // Received an authentication request
1132 resp
= resp5
; respLen
= resp5Len
; order
= 7;
1133 Dbprintf("Authenticate request from reader: %x %x %x",
1134 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1136 } else if(receivedCmd
[0] == 0xE0) {
1137 // Received a RATS request
1138 resp
= resp1
; respLen
= 0;order
= 70;
1139 Dbprintf("RATS request from reader: %x %x %x",
1140 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1142 // Never seen this command before
1143 Dbprintf("Unknown command received from reader: %x %x %x %x %x %x %x %x %x",
1144 receivedCmd
[0], receivedCmd
[1], receivedCmd
[2],
1145 receivedCmd
[3], receivedCmd
[3], receivedCmd
[4],
1146 receivedCmd
[5], receivedCmd
[6], receivedCmd
[7]);
1148 resp
= resp1
; respLen
= 0; order
= 0;
1151 // Count number of wakeups received after a halt
1152 if(order
== 6 && lastorder
== 5) { happened
++; }
1154 // Count number of other messages after a halt
1155 if(order
!= 6 && lastorder
== 5) { happened2
++; }
1157 // Look at last parity bit to determine timing of answer
1158 if((Uart
.parityBits
& 0x01) || receivedCmd
[0] == 0x52) {
1159 // 1236, so correction bit needed
1163 memset(receivedCmd
, 0x44, 32);
1165 if(cmdsRecvd
> 999) {
1166 DbpString("1000 commands later...");
1173 if(respLen
<= 0) continue;
1175 // Modulate Manchester
1176 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_MOD
);
1177 AT91C_BASE_SSC
->SSC_THR
= 0x00;
1180 // ### Transmit the response ###
1183 fdt_indicator
= FALSE
;
1185 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1186 volatile uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1189 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1197 AT91C_BASE_SSC
->SSC_THR
= b
;
1203 if(BUTTON_PRESS()) {
1210 Dbprintf("%x %x %x", happened
, happened2
, cmdsRecvd
);
1214 //-----------------------------------------------------------------------------
1215 // Transmit the command (to the tag) that was placed in ToSend[].
1216 //-----------------------------------------------------------------------------
1217 static void TransmitFor14443a(const uint8_t *cmd
, int len
, int *samples
, int *wait
)
1221 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1227 for(c
= 0; c
< *wait
;) {
1228 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1229 AT91C_BASE_SSC
->SSC_THR
= 0x00; // For exact timing!
1232 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1233 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1241 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1242 AT91C_BASE_SSC
->SSC_THR
= cmd
[c
];
1248 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1249 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1254 if (samples
) *samples
= (c
+ *wait
) << 3;
1257 //-----------------------------------------------------------------------------
1258 // Code a 7-bit command without parity bit
1259 // This is especially for 0x26 and 0x52 (REQA and WUPA)
1260 //-----------------------------------------------------------------------------
1261 void ShortFrameFromReader(const uint8_t bt
)
1269 // Start of Communication (Seq. Z)
1270 ToSend
[++ToSendMax
] = SEC_Z
;
1274 for(j
= 0; j
< 7; j
++) {
1277 ToSend
[++ToSendMax
] = SEC_X
;
1282 ToSend
[++ToSendMax
] = SEC_Z
;
1286 ToSend
[++ToSendMax
] = SEC_Y
;
1293 // End of Communication
1296 ToSend
[++ToSendMax
] = SEC_Z
;
1300 ToSend
[++ToSendMax
] = SEC_Y
;
1304 ToSend
[++ToSendMax
] = SEC_Y
;
1307 ToSend
[++ToSendMax
] = SEC_Y
;
1308 ToSend
[++ToSendMax
] = SEC_Y
;
1309 ToSend
[++ToSendMax
] = SEC_Y
;
1311 // Convert from last character reference to length
1315 //-----------------------------------------------------------------------------
1316 // Prepare reader command to send to FPGA
1318 //-----------------------------------------------------------------------------
1319 void CodeIso14443aAsReaderPar(const uint8_t * cmd
, int len
, uint32_t dwParity
)
1327 // Start of Communication (Seq. Z)
1328 ToSend
[++ToSendMax
] = SEC_Z
;
1331 // Generate send structure for the data bits
1332 for (i
= 0; i
< len
; i
++) {
1333 // Get the current byte to send
1336 for (j
= 0; j
< 8; j
++) {
1339 ToSend
[++ToSendMax
] = SEC_X
;
1344 ToSend
[++ToSendMax
] = SEC_Z
;
1347 ToSend
[++ToSendMax
] = SEC_Y
;
1354 // Get the parity bit
1355 if ((dwParity
>> i
) & 0x01) {
1357 ToSend
[++ToSendMax
] = SEC_X
;
1362 ToSend
[++ToSendMax
] = SEC_Z
;
1365 ToSend
[++ToSendMax
] = SEC_Y
;
1371 // End of Communication
1374 ToSend
[++ToSendMax
] = SEC_Z
;
1377 ToSend
[++ToSendMax
] = SEC_Y
;
1381 ToSend
[++ToSendMax
] = SEC_Y
;
1384 ToSend
[++ToSendMax
] = SEC_Y
;
1385 ToSend
[++ToSendMax
] = SEC_Y
;
1386 ToSend
[++ToSendMax
] = SEC_Y
;
1388 // Convert from last character reference to length
1392 //-----------------------------------------------------------------------------
1393 // Wait a certain time for tag response
1394 // If a response is captured return TRUE
1395 // If it takes to long return FALSE
1396 //-----------------------------------------------------------------------------
1397 static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer
1399 // buffer needs to be 512 bytes
1402 // Set FPGA mode to "reader listen mode", no modulation (listen
1403 // only, since we are receiving, not transmitting).
1404 // Signal field is on with the appropriate LED
1406 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1408 // Now get the answer from the card
1409 Demod
.output
= receivedResponse
;
1411 Demod
.state
= DEMOD_UNSYNCD
;
1414 if (elapsed
) *elapsed
= 0;
1420 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1421 AT91C_BASE_SSC
->SSC_THR
= 0x00; // To make use of exact timing of next command from reader!!
1422 if (elapsed
) (*elapsed
)++;
1424 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1425 if(c
< iso14a_timeout
) { c
++; } else { return FALSE
; }
1426 b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1427 if(ManchesterDecoding((b
>>4) & 0xf)) {
1428 *samples
= ((c
- 1) << 3) + 4;
1431 if(ManchesterDecoding(b
& 0x0f)) {
1439 void ReaderTransmitShort(const uint8_t* bt
)
1444 ShortFrameFromReader(*bt
);
1447 TransmitFor14443a(ToSend
, ToSendMax
, &samples
, &wait
);
1449 // Store reader command in buffer
1450 if (tracing
) LogTrace(bt
,1,0,GetParity(bt
,1),TRUE
);
1453 void ReaderTransmitPar(uint8_t* frame
, int len
, uint32_t par
)
1458 // This is tied to other size changes
1459 // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024;
1460 CodeIso14443aAsReaderPar(frame
,len
,par
);
1463 TransmitFor14443a(ToSend
, ToSendMax
, &samples
, &wait
);
1467 // Store reader command in buffer
1468 if (tracing
) LogTrace(frame
,len
,0,par
,TRUE
);
1472 void ReaderTransmit(uint8_t* frame
, int len
)
1474 // Generate parity and redirect
1475 ReaderTransmitPar(frame
,len
,GetParity(frame
,len
));
1478 int ReaderReceive(uint8_t* receivedAnswer
)
1481 if (!GetIso14443aAnswerFromTag(receivedAnswer
,100,&samples
,0)) return FALSE
;
1482 if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,samples
,Demod
.parityBits
,FALSE
);
1483 if(samples
== 0) return FALSE
;
1487 /* performs iso14443a anticolision procedure
1488 * fills the uid pointer unless NULL
1489 * fills resp_data unless NULL */
1490 int iso14443a_select_card(uint8_t * uid_ptr
, iso14a_card_select_t
* resp_data
) {
1491 uint8_t wupa
[] = { 0x52 };
1492 uint8_t sel_all
[] = { 0x93,0x20 };
1493 uint8_t sel_uid
[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1494 uint8_t rats
[] = { 0xE0,0x80,0x00,0x00 }; // FSD=256, FSDI=8, CID=0
1496 uint8_t* resp
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1497 uint8_t* uid
= resp
+ 7;
1499 uint8_t sak
= 0x04; // cascade uid
1500 int cascade_level
= 0;
1504 // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
1505 ReaderTransmitShort(wupa
);
1507 if(!ReaderReceive(resp
)) return 0;
1510 memcpy(resp_data
->atqa
, resp
, 2);
1512 ReaderTransmit(sel_all
,sizeof(sel_all
));
1513 if(!ReaderReceive(uid
)) return 0;
1515 // OK we will select at least at cascade 1, lets see if first byte of UID was 0x88 in
1516 // which case we need to make a cascade 2 request and select - this is a long UID
1517 // While the UID is not complete, the 3nd bit (from the right) is set in the SAK.
1518 for(; sak
& 0x04; cascade_level
++)
1520 // SELECT_* (L1: 0x93, L2: 0x95, L3: 0x97)
1521 sel_uid
[0] = sel_all
[0] = 0x93 + cascade_level
* 2;
1524 ReaderTransmit(sel_all
,sizeof(sel_all
));
1525 if (!ReaderReceive(resp
)) return 0;
1526 if(uid_ptr
) memcpy(uid_ptr
+ cascade_level
*4, resp
, 4);
1528 // Construct SELECT UID command
1529 memcpy(sel_uid
+2,resp
,5);
1530 AppendCrc14443a(sel_uid
,7);
1531 ReaderTransmit(sel_uid
,sizeof(sel_uid
));
1534 if (!ReaderReceive(resp
)) return 0;
1538 resp_data
->sak
= sak
;
1539 resp_data
->ats_len
= 0;
1542 if( (sak
& 0x20) == 0)
1543 return 2; // non iso14443a compliant tag
1545 // Request for answer to select
1546 AppendCrc14443a(rats
, 2);
1547 ReaderTransmit(rats
, sizeof(rats
));
1548 if (!(len
= ReaderReceive(resp
))) return 0;
1550 memcpy(resp_data
->ats
, resp
, sizeof(resp_data
->ats
));
1551 resp_data
->ats_len
= len
;
1557 void iso14443a_setup() {
1560 // Start from off (no field generated)
1561 // Signal field is off with the appropriate LED
1563 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1566 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1568 // Now give it time to spin up.
1569 // Signal field is on with the appropriate LED
1571 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1574 iso14a_timeout
= 2048; //default
1577 int iso14_apdu(uint8_t * cmd
, size_t cmd_len
, void * data
) {
1578 uint8_t real_cmd
[cmd_len
+4];
1579 real_cmd
[0] = 0x0a; //I-Block
1580 real_cmd
[1] = 0x00; //CID: 0 //FIXME: allow multiple selected cards
1581 memcpy(real_cmd
+2, cmd
, cmd_len
);
1582 AppendCrc14443a(real_cmd
,cmd_len
+2);
1584 ReaderTransmit(real_cmd
, cmd_len
+4);
1585 size_t len
= ReaderReceive(data
);
1587 return -1; //DATA LINK ERROR
1593 //-----------------------------------------------------------------------------
1594 // Read an ISO 14443a tag. Send out commands and store answers.
1596 //-----------------------------------------------------------------------------
1597 void ReaderIso14443a(UsbCommand
* c
, UsbCommand
* ack
)
1599 iso14a_command_t param
= c
->arg
[0];
1600 uint8_t * cmd
= c
->d
.asBytes
;
1601 size_t len
= c
->arg
[1];
1603 if(param
& ISO14A_REQUEST_TRIGGER
) iso14a_set_trigger(1);
1605 if(param
& ISO14A_CONNECT
) {
1607 ack
->arg
[0] = iso14443a_select_card(ack
->d
.asBytes
, (iso14a_card_select_t
*) (ack
->d
.asBytes
+12));
1608 UsbSendPacket((void *)ack
, sizeof(UsbCommand
));
1611 if(param
& ISO14A_SET_TIMEOUT
) {
1612 iso14a_timeout
= c
->arg
[2];
1615 if(param
& ISO14A_SET_TIMEOUT
) {
1616 iso14a_timeout
= c
->arg
[2];
1619 if(param
& ISO14A_APDU
) {
1620 ack
->arg
[0] = iso14_apdu(cmd
, len
, ack
->d
.asBytes
);
1621 UsbSendPacket((void *)ack
, sizeof(UsbCommand
));
1624 if(param
& ISO14A_RAW
) {
1625 if(param
& ISO14A_APPEND_CRC
) {
1626 AppendCrc14443a(cmd
,len
);
1629 ReaderTransmit(cmd
,len
);
1630 ack
->arg
[0] = ReaderReceive(ack
->d
.asBytes
);
1631 UsbSendPacket((void *)ack
, sizeof(UsbCommand
));
1634 if(param
& ISO14A_REQUEST_TRIGGER
) iso14a_set_trigger(0);
1636 if(param
& ISO14A_NO_DISCONNECT
)
1639 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1642 //-----------------------------------------------------------------------------
1643 // Read an ISO 14443a tag. Send out commands and store answers.
1645 //-----------------------------------------------------------------------------
1646 void ReaderMifare(uint32_t parameter
)
1649 uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b };
1650 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
1652 uint8_t* receivedAnswer
= (((uint8_t *)BigBuf
) + 3560); // was 3560 - tied to other size changes
1665 byte_t par_mask
= 0xff;
1671 byte_t nt_attacked
[4];
1674 num_to_bytes(parameter
,4,nt_attacked
);
1678 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1680 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1682 // Test if the action was cancelled
1683 if(BUTTON_PRESS()) {
1687 if(!iso14443a_select_card(NULL
, NULL
)) 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;
1694 memcpy(nt
,receivedAnswer
,4);
1696 // Transmit reader nonce and reader answer
1697 ReaderTransmitPar(mf_nr_ar
,sizeof(mf_nr_ar
),par
);
1699 // Receive 4 bit answer
1700 if (ReaderReceive(receivedAnswer
))
1705 memcpy(nt_attacked
,nt
,4);
1707 par_low
= par
& 0x07;
1710 if (memcmp(nt
,nt_attacked
,4) != 0) continue;
1713 if(led_on
) LED_B_ON(); else LED_B_OFF();
1714 par_list
[nt_diff
] = par
;
1715 ks_list
[nt_diff
] = receivedAnswer
[0]^0x05;
1717 // Test if the information is complete
1718 if (nt_diff
== 0x07) break;
1720 nt_diff
= (nt_diff
+1) & 0x07;
1721 mf_nr_ar
[3] = nt_diff
<< 5;
1728 par
= (((par
>>3)+1) << 3) | par_low
;
1733 LogTrace(nt
,4,0,GetParity(nt
,4),TRUE
);
1734 LogTrace(par_list
,8,0,GetParity(par_list
,8),TRUE
);
1735 LogTrace(ks_list
,8,0,GetParity(ks_list
,8),TRUE
);
1738 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);