1 //-----------------------------------------------------------------------------
2 // Routines to support ISO 14443 type A.
4 // Gerhard de Koning Gans - May 2008
5 //-----------------------------------------------------------------------------
8 #include "../common/iso14443_crc.c"
19 //-----------------------------------------------------------------------------
20 // The software UART that receives commands from the reader, and its state
22 //-----------------------------------------------------------------------------
26 STATE_START_OF_COMMUNICATION
,
50 static BOOL
MillerDecoding(int bit
)
56 Uart
.bitBuffer
= bit
^ 0xFF0;
61 Uart
.bitBuffer
^= bit
;
66 if(Uart
.state
!= STATE_UNSYNCD
) {
69 if((Uart
.bitBuffer
& Uart
.syncBit
) ^ Uart
.syncBit
) {
75 if(((Uart
.bitBuffer
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) {
81 if(bit
!= bitright
) { bit
= bitright
; }
83 if(Uart
.posCnt
== 1) {
84 // measurement first half bitperiod
86 Uart
.drop
= DROP_FIRST_HALF
;
90 // measurement second half bitperiod
91 if(!bit
& (Uart
.drop
== DROP_NONE
)) {
92 Uart
.drop
= DROP_SECOND_HALF
;
95 // measured a drop in first and second half
96 // which should not be possible
97 Uart
.state
= STATE_ERROR_WAIT
;
104 case STATE_START_OF_COMMUNICATION
:
106 if(Uart
.drop
== DROP_SECOND_HALF
) {
107 // error, should not happen in SOC
108 Uart
.state
= STATE_ERROR_WAIT
;
113 Uart
.state
= STATE_MILLER_Z
;
120 if(Uart
.drop
== DROP_NONE
) {
121 // logic '0' followed by sequence Y
122 // end of communication
123 Uart
.state
= STATE_UNSYNCD
;
126 // if(Uart.drop == DROP_FIRST_HALF) {
127 // Uart.state = STATE_MILLER_Z; stay the same
128 // we see a logic '0' }
129 if(Uart
.drop
== DROP_SECOND_HALF
) {
130 // we see a logic '1'
131 Uart
.shiftReg
|= 0x100;
132 Uart
.state
= STATE_MILLER_X
;
138 if(Uart
.drop
== DROP_NONE
) {
139 // sequence Y, we see a '0'
140 Uart
.state
= STATE_MILLER_Y
;
143 if(Uart
.drop
== DROP_FIRST_HALF
) {
144 // Would be STATE_MILLER_Z
145 // but Z does not follow X, so error
146 Uart
.state
= STATE_ERROR_WAIT
;
149 if(Uart
.drop
== DROP_SECOND_HALF
) {
150 // We see a '1' and stay in state X
151 Uart
.shiftReg
|= 0x100;
159 if(Uart
.drop
== DROP_NONE
) {
160 // logic '0' followed by sequence Y
161 // end of communication
162 Uart
.state
= STATE_UNSYNCD
;
165 if(Uart
.drop
== DROP_FIRST_HALF
) {
167 Uart
.state
= STATE_MILLER_Z
;
169 if(Uart
.drop
== DROP_SECOND_HALF
) {
170 // We see a '1' and go to state X
171 Uart
.shiftReg
|= 0x100;
172 Uart
.state
= STATE_MILLER_X
;
176 case STATE_ERROR_WAIT
:
177 // That went wrong. Now wait for at least two bit periods
178 // and try to sync again
179 if(Uart
.drop
== DROP_NONE
) {
181 Uart
.state
= STATE_UNSYNCD
;
186 Uart
.state
= STATE_UNSYNCD
;
191 Uart
.drop
= DROP_NONE
;
193 // should have received at least one whole byte...
194 if((Uart
.bitCnt
== 2) && EOC
&& (Uart
.byteCnt
> 0)) {
198 if(Uart
.bitCnt
== 9) {
199 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
& 0xff);
202 Uart
.parityBits
<<= 1;
203 Uart
.parityBits
^= ((Uart
.shiftReg
>> 8) & 0x01);
206 // when End of Communication received and
207 // all data bits processed..
214 Uart.output[Uart.byteCnt] = 0xAA;
216 Uart.output[Uart.byteCnt] = error & 0xFF;
218 Uart.output[Uart.byteCnt] = 0xAA;
220 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF;
222 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF;
224 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF;
226 Uart.output[Uart.byteCnt] = 0xAA;
234 bit
= Uart
.bitBuffer
& 0xf0;
238 // should have been high or at least (4 * 128) / fc
239 // according to ISO this should be at least (9 * 128 + 20) / fc
240 if(Uart
.highCnt
== 8) {
241 // we went low, so this could be start of communication
242 // it turns out to be safer to choose a less significant
243 // syncbit... so we check whether the neighbour also represents the drop
244 Uart
.posCnt
= 1; // apparently we are busy with our first half bit period
245 Uart
.syncBit
= bit
& 8;
247 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; }
248 else if(bit
& 4) { Uart
.syncBit
= bit
& 4; Uart
.samples
= 2; bit
<<= 2; }
249 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; }
250 else if(bit
& 2) { Uart
.syncBit
= bit
& 2; Uart
.samples
= 1; bit
<<= 1; }
251 if(!Uart
.syncBit
) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0;
252 if(Uart
.syncBit
& (Uart
.bitBuffer
& 8)) {
255 // the first half bit period is expected in next sample
260 else if(bit
& 1) { Uart
.syncBit
= bit
& 1; Uart
.samples
= 0; }
263 Uart
.state
= STATE_START_OF_COMMUNICATION
;
264 Uart
.drop
= DROP_FIRST_HALF
;
275 if(Uart
.highCnt
< 8) {
284 //=============================================================================
285 // ISO 14443 Type A - Manchester
286 //=============================================================================
291 DEMOD_START_OF_COMMUNICATION
,
314 static BOOL
ManchesterDecoding(int v
)
330 if(Demod
.state
==DEMOD_UNSYNCD
) {
331 Demod
.output
[Demod
.len
] = 0xfa;
334 Demod
.posCount
= 1; // This is the first half bit period, so after syncing handle the second part
335 if(bit
& 0x08) { Demod
.syncBit
= 0x08; }
337 if(bit
& 0x04) { Demod
.syncBit
= 0x04; }
339 else if(bit
& 0x04) { Demod
.syncBit
= 0x04; bit
<<= 4; }
341 if(bit
& 0x02) { Demod
.syncBit
= 0x02; }
343 else if(bit
& 0x02) { Demod
.syncBit
= 0x02; bit
<<= 4; }
345 if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
347 if(Demod
.syncBit
& (Demod
.buffer
& 0x08)) {
348 Demod
.syncBit
= 0x08;
350 // The first half bitperiod is expected in next sample
352 Demod
.output
[Demod
.len
] = 0xfb;
355 else if(bit
& 0x01) { Demod
.syncBit
= 0x01; }
359 Demod
.state
= DEMOD_START_OF_COMMUNICATION
;
360 Demod
.sub
= SUB_FIRST_HALF
;
363 Demod
.parityBits
= 0;
366 switch(Demod
.syncBit
) {
367 case 0x08: Demod
.samples
= 3; break;
368 case 0x04: Demod
.samples
= 2; break;
369 case 0x02: Demod
.samples
= 1; break;
370 case 0x01: Demod
.samples
= 0; break;
377 //modulation = bit & Demod.syncBit;
378 modulation
= ((bit
<< 1) ^ ((Demod
.buffer
& 0x08) >> 3)) & Demod
.syncBit
;
382 if(Demod
.posCount
==0) {
385 Demod
.sub
= SUB_FIRST_HALF
;
388 Demod
.sub
= SUB_NONE
;
393 if(modulation
&& (Demod
.sub
== SUB_FIRST_HALF
)) {
394 if(Demod
.state
!=DEMOD_ERROR_WAIT
) {
395 Demod
.state
= DEMOD_ERROR_WAIT
;
396 Demod
.output
[Demod
.len
] = 0xaa;
400 else if(modulation
) {
401 Demod
.sub
= SUB_SECOND_HALF
;
404 switch(Demod
.state
) {
405 case DEMOD_START_OF_COMMUNICATION
:
406 if(Demod
.sub
== SUB_FIRST_HALF
) {
407 Demod
.state
= DEMOD_MANCHESTER_D
;
410 Demod
.output
[Demod
.len
] = 0xab;
411 Demod
.state
= DEMOD_ERROR_WAIT
;
416 case DEMOD_MANCHESTER_D
:
417 case DEMOD_MANCHESTER_E
:
418 if(Demod
.sub
== SUB_FIRST_HALF
) {
420 Demod
.shiftReg
= (Demod
.shiftReg
>> 1) ^ 0x100;
421 Demod
.state
= DEMOD_MANCHESTER_D
;
423 else if(Demod
.sub
== SUB_SECOND_HALF
) {
425 Demod
.shiftReg
>>= 1;
426 Demod
.state
= DEMOD_MANCHESTER_E
;
429 Demod
.state
= DEMOD_MANCHESTER_F
;
433 case DEMOD_MANCHESTER_F
:
434 // Tag response does not need to be a complete byte!
435 if(Demod
.len
> 0 || Demod
.bitCount
> 0) {
436 if(Demod
.bitCount
> 0) {
437 Demod
.shiftReg
>>= (9 - Demod
.bitCount
);
438 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
440 // No parity bit, so just shift a 0
441 Demod
.parityBits
<<= 1;
444 Demod
.state
= DEMOD_UNSYNCD
;
448 Demod
.output
[Demod
.len
] = 0xad;
449 Demod
.state
= DEMOD_ERROR_WAIT
;
454 case DEMOD_ERROR_WAIT
:
455 Demod
.state
= DEMOD_UNSYNCD
;
459 Demod
.output
[Demod
.len
] = 0xdd;
460 Demod
.state
= DEMOD_UNSYNCD
;
464 if(Demod
.bitCount
>=9) {
465 Demod
.output
[Demod
.len
] = Demod
.shiftReg
& 0xff;
468 Demod
.parityBits
<<= 1;
469 Demod
.parityBits
^= ((Demod
.shiftReg
>> 8) & 0x01);
476 Demod.output[Demod.len] = 0xBB;
478 Demod.output[Demod.len] = error & 0xFF;
480 Demod.output[Demod.len] = 0xBB;
482 Demod.output[Demod.len] = bit & 0xFF;
484 Demod.output[Demod.len] = Demod.buffer & 0xFF;
486 Demod.output[Demod.len] = Demod.syncBit & 0xFF;
488 Demod.output[Demod.len] = 0xBB;
495 } // end (state != UNSYNCED)
500 //=============================================================================
501 // Finally, a `sniffer' for ISO 14443 Type A
502 // Both sides of communication!
503 //=============================================================================
505 //-----------------------------------------------------------------------------
506 // Record the sequence of commands sent by the reader to the tag, with
507 // triggering so that we start recording at the point that the tag is moved
509 //-----------------------------------------------------------------------------
510 void SnoopIso14443a(void)
513 // BIG CHANGE - UNDERSTAND THIS BEFORE WE COMMIT
515 #define RECV_CMD_OFFSET 3032
516 #define RECV_RES_OFFSET 3096
517 #define DMA_BUFFER_OFFSET 3160
518 #define DMA_BUFFER_SIZE 4096
519 #define TRACE_LENGTH 3000
521 // #define RECV_CMD_OFFSET 2032 // original (working as of 21/2/09) values
522 // #define RECV_RES_OFFSET 2096 // original (working as of 21/2/09) values
523 // #define DMA_BUFFER_OFFSET 2160 // original (working as of 21/2/09) values
524 // #define DMA_BUFFER_SIZE 4096 // original (working as of 21/2/09) values
525 // #define TRACE_LENGTH 2000 // original (working as of 21/2/09) values
527 // We won't start recording the frames that we acquire until we trigger;
528 // a good trigger condition to get started is probably when we see a
529 // response from the tag.
530 BOOL triggered
= TRUE
; // FALSE to wait first for card
532 // The command (reader -> tag) that we're receiving.
533 // The length of a received command will in most cases be no more than 18 bytes.
534 // So 32 should be enough!
535 BYTE
*receivedCmd
= (((BYTE
*)BigBuf
) + RECV_CMD_OFFSET
);
536 // The response (tag -> reader) that we're receiving.
537 BYTE
*receivedResponse
= (((BYTE
*)BigBuf
) + RECV_RES_OFFSET
);
539 // As we receive stuff, we copy it from receivedCmd or receivedResponse
540 // into trace, along with its length and other annotations.
541 BYTE
*trace
= (BYTE
*)BigBuf
;
544 // The DMA buffer, used to stream samples from the FPGA
545 SBYTE
*dmaBuf
= ((SBYTE
*)BigBuf
) + DMA_BUFFER_OFFSET
;
551 // Count of samples received so far, so that we can include timing
552 // information in the trace buffer.
556 memset(trace
, 0x44, RECV_CMD_OFFSET
);
558 // Set up the demodulator for tag -> reader responses.
559 Demod
.output
= receivedResponse
;
561 Demod
.state
= DEMOD_UNSYNCD
;
563 // And the reader -> tag commands
564 memset(&Uart
, 0, sizeof(Uart
));
565 Uart
.output
= receivedCmd
;
566 Uart
.byteCntMax
= 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
567 Uart
.state
= STATE_UNSYNCD
;
569 // And put the FPGA in the appropriate mode
570 // Signal field is off with the appropriate LED
572 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
573 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
575 // Setup for the DMA.
578 lastRxCounter
= DMA_BUFFER_SIZE
;
579 FpgaSetupSscDma((BYTE
*)dmaBuf
, DMA_BUFFER_SIZE
);
583 // And now we loop, receiving samples.
586 int behindBy
= (lastRxCounter
- PDC_RX_COUNTER(SSC_BASE
)) &
588 if(behindBy
> maxBehindBy
) {
589 maxBehindBy
= behindBy
;
591 DbpString("blew circular buffer!");
595 if(behindBy
< 1) continue;
600 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
601 upTo
-= DMA_BUFFER_SIZE
;
602 lastRxCounter
+= DMA_BUFFER_SIZE
;
603 PDC_RX_NEXT_POINTER(SSC_BASE
) = (DWORD
)upTo
;
604 PDC_RX_NEXT_COUNTER(SSC_BASE
) = DMA_BUFFER_SIZE
;
608 #define HANDLE_BIT_IF_BODY \
611 trace[traceLen++] = ((rsamples >> 0) & 0xff); \
612 trace[traceLen++] = ((rsamples >> 8) & 0xff); \
613 trace[traceLen++] = ((rsamples >> 16) & 0xff); \
614 trace[traceLen++] = ((rsamples >> 24) & 0xff); \
615 trace[traceLen++] = ((Uart.parityBits >> 0) & 0xff); \
616 trace[traceLen++] = ((Uart.parityBits >> 8) & 0xff); \
617 trace[traceLen++] = ((Uart.parityBits >> 16) & 0xff); \
618 trace[traceLen++] = ((Uart.parityBits >> 24) & 0xff); \
619 trace[traceLen++] = Uart.byteCnt; \
620 memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \
621 traceLen += Uart.byteCnt; \
622 if(traceLen > TRACE_LENGTH) break; \
624 /* And ready to receive another command. */ \
625 Uart.state = STATE_UNSYNCD; \
626 /* And also reset the demod code, which might have been */ \
627 /* false-triggered by the commands from the reader. */ \
628 Demod.state = DEMOD_UNSYNCD; \
631 if(MillerDecoding((smpl & 0xF0) >> 4)) {
632 rsamples
= samples
- Uart
.samples
;
635 if(ManchesterDecoding(smpl
& 0x0F)) {
636 rsamples
= samples
- Demod
.samples
;
639 // timestamp, as a count of samples
640 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
641 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
642 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
643 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
644 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
645 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
646 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
647 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
649 trace
[traceLen
++] = Demod
.len
;
650 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
651 traceLen
+= Demod
.len
;
652 if(traceLen
> TRACE_LENGTH
) break;
656 // And ready to receive another response.
657 memset(&Demod
, 0, sizeof(Demod
));
658 Demod
.output
= receivedResponse
;
659 Demod
.state
= DEMOD_UNSYNCD
;
664 DbpString("cancelled_a");
669 DbpString("COMMAND FINISHED");
671 DbpIntegers(maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
672 DbpIntegers(Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
675 PDC_CONTROL(SSC_BASE
) = PDC_RX_DISABLE
;
676 DbpIntegers(maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
677 DbpIntegers(Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
684 // Prepare communication bits to send to FPGA
685 void Sequence(SecType seq
)
691 // Sequence D: 11110000
692 // modulation with subcarrier during first half
693 ToSend
[ToSendMax
] = 0xf0;
696 // Sequence E: 00001111
697 // modulation with subcarrier during second half
698 ToSend
[ToSendMax
] = 0x0f;
701 // Sequence F: 00000000
702 // no modulation with subcarrier
703 ToSend
[ToSendMax
] = 0x00;
707 // Sequence X: 00001100
708 // drop after half a period
709 ToSend
[ToSendMax
] = 0x0c;
713 // Sequence Y: 00000000
715 ToSend
[ToSendMax
] = 0x00;
718 // Sequence Z: 11000000
720 ToSend
[ToSendMax
] = 0xc0;
725 //-----------------------------------------------------------------------------
726 // Prepare tag messages
727 //-----------------------------------------------------------------------------
728 static void CodeIso14443aAsTag(const BYTE
*cmd
, int len
)
735 // Correction bit, might be removed when not needed
740 ToSendStuffBit(1); // 1
748 for(i
= 0; i
< len
; i
++) {
754 for(j
= 0; j
< 8; j
++) {
755 oddparity
^= (b
& 1);
775 // Flush the buffer in FPGA!!
776 for(i
= 0; i
< 5; i
++) {
780 // Convert from last byte pos to length
783 // Add a few more for slop
784 ToSend
[ToSendMax
++] = 0x00;
785 ToSend
[ToSendMax
++] = 0x00;
789 //-----------------------------------------------------------------------------
790 // This is to send a NACK kind of answer, its only 3 bits, I know it should be 4
791 //-----------------------------------------------------------------------------
792 static void CodeStrangeAnswer()
798 // Correction bit, might be removed when not needed
803 ToSendStuffBit(1); // 1
823 // Flush the buffer in FPGA!!
824 for(i
= 0; i
< 5; i
++) {
828 // Convert from last byte pos to length
831 // Add a few more for slop
832 ToSend
[ToSendMax
++] = 0x00;
833 ToSend
[ToSendMax
++] = 0x00;
837 //-----------------------------------------------------------------------------
838 // Wait for commands from reader
839 // Stop when button is pressed
840 // Or return TRUE when command is captured
841 //-----------------------------------------------------------------------------
842 static BOOL
GetIso14443aCommandFromReader(BYTE
*received
, int *len
, int maxLen
)
844 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
845 // only, since we are receiving, not transmitting).
846 // Signal field is off with the appropriate LED
848 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
850 // Now run a `software UART' on the stream of incoming samples.
851 Uart
.output
= received
;
852 Uart
.byteCntMax
= maxLen
;
853 Uart
.state
= STATE_UNSYNCD
;
858 if(BUTTON_PRESS()) return FALSE
;
860 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
861 SSC_TRANSMIT_HOLDING
= 0x00;
863 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
864 BYTE b
= (BYTE
)SSC_RECEIVE_HOLDING
;
865 if(MillerDecoding((b
& 0xf0) >> 4)) {
869 if(MillerDecoding(b
& 0x0f)) {
877 //-----------------------------------------------------------------------------
878 // Main loop of simulated tag: receive commands from reader, decide what
879 // response to send, and send it.
880 //-----------------------------------------------------------------------------
881 void SimulateIso14443aTag(int tagType
, int TagUid
)
883 // This function contains the tag emulation
885 // Prepare protocol messages
886 // static const BYTE cmd1[] = { 0x26 };
887 // static const BYTE response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg
889 static const BYTE response1
[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me
890 // static const BYTE response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me
893 // static const BYTE cmd2[] = { 0x93, 0x20 };
894 //static const BYTE response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg
899 static const BYTE response2
[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips
902 // When reader selects us during cascade1 it will send cmd3
903 //BYTE response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE)
904 BYTE response3
[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire)
905 ComputeCrc14443(CRC_14443_A
, response3
, 1, &response3
[1], &response3
[2]);
907 // send cascade2 2nd half of UID
908 static const BYTE response2a
[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck
909 // NOTE : THE CRC on the above may be wrong as I have obfuscated the actual UID
912 // When reader selects us during cascade2 it will send cmd3a
913 //BYTE response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE)
914 BYTE response3a
[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire)
915 ComputeCrc14443(CRC_14443_A
, response3a
, 1, &response3a
[1], &response3a
[2]);
917 // When reader tries to authenticate
918 // static const BYTE cmd5[] = { 0x60, 0x00, 0xf5, 0x7b };
919 static const BYTE response5
[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce
924 // Longest possible response will be 16 bytes + 2 CRC = 18 bytes
926 // 144 data bits (18 * 8)
929 // 1 Correction bit (Answer in 1172 or 1236 periods, see FPGA)
930 // 1 just for the case
934 // 166 bytes, since every bit that needs to be send costs us a byte
938 // Respond with card type
939 BYTE
*resp1
= (((BYTE
*)BigBuf
) + 800);
942 // Anticollision cascade1 - respond with uid
943 BYTE
*resp2
= (((BYTE
*)BigBuf
) + 970);
946 // Anticollision cascade2 - respond with 2nd half of uid if asked
947 // we're only going to be asked if we set the 1st byte of the UID (during cascade1) to 0x88
948 BYTE
*resp2a
= (((BYTE
*)BigBuf
) + 1140);
951 // Acknowledge select - cascade 1
952 BYTE
*resp3
= (((BYTE
*)BigBuf
) + 1310);
955 // Acknowledge select - cascade 2
956 BYTE
*resp3a
= (((BYTE
*)BigBuf
) + 1480);
959 // Response to a read request - not implemented atm
960 BYTE
*resp4
= (((BYTE
*)BigBuf
) + 1550);
963 // Authenticate response - nonce
964 BYTE
*resp5
= (((BYTE
*)BigBuf
) + 1720);
967 BYTE
*receivedCmd
= (BYTE
*)BigBuf
;
974 // To control where we are in the protocol
978 // Just to allow some checks
986 memset(receivedCmd
, 0x44, 400);
988 // Prepare the responses of the anticollision phase
989 // there will be not enough time to do this at the moment the reader sends it REQA
992 CodeIso14443aAsTag(response1
, sizeof(response1
));
993 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
995 // Send our UID (cascade 1)
996 CodeIso14443aAsTag(response2
, sizeof(response2
));
997 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
999 // Answer to select (cascade1)
1000 CodeIso14443aAsTag(response3
, sizeof(response3
));
1001 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
1003 // Send the cascade 2 2nd part of the uid
1004 CodeIso14443aAsTag(response2a
, sizeof(response2a
));
1005 memcpy(resp2a
, ToSend
, ToSendMax
); resp2aLen
= ToSendMax
;
1007 // Answer to select (cascade 2)
1008 CodeIso14443aAsTag(response3a
, sizeof(response3a
));
1009 memcpy(resp3a
, ToSend
, ToSendMax
); resp3aLen
= ToSendMax
;
1011 // Strange answer is an example of rare message size (3 bits)
1012 CodeStrangeAnswer();
1013 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1015 // Authentication answer (random nonce)
1016 CodeIso14443aAsTag(response5
, sizeof(response5
));
1017 memcpy(resp5
, ToSend
, ToSendMax
); resp5Len
= ToSendMax
;
1019 // We need to listen to the high-frequency, peak-detected path.
1020 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1028 if(!GetIso14443aCommandFromReader(receivedCmd
, &len
, 100)) {
1029 DbpString("button press");
1032 // 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
1033 // Okay, look at the command now.
1035 i
= 1; // first byte transmitted
1036 if(receivedCmd
[0] == 0x26) {
1037 // Received a REQUEST
1038 resp
= resp1
; respLen
= resp1Len
; order
= 1;
1039 //DbpString("Hello request from reader:");
1040 } else if(receivedCmd
[0] == 0x52) {
1041 // Received a WAKEUP
1042 resp
= resp1
; respLen
= resp1Len
; order
= 6;
1043 // //DbpString("Wakeup request from reader:");
1045 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] == 0x93) { // greg - cascade 1 anti-collision
1046 // Received request for UID (cascade 1)
1047 resp
= resp2
; respLen
= resp2Len
; order
= 2;
1048 // DbpString("UID (cascade 1) request from reader:");
1049 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1052 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] ==0x95) { // greg - cascade 2 anti-collision
1053 // Received request for UID (cascade 2)
1054 resp
= resp2a
; respLen
= resp2aLen
; order
= 20;
1055 // DbpString("UID (cascade 2) request from reader:");
1056 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1059 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x93) { // greg - cascade 1 select
1060 // Received a SELECT
1061 resp
= resp3
; respLen
= resp3Len
; order
= 3;
1062 // DbpString("Select (cascade 1) request from reader:");
1063 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1066 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x95) { // greg - cascade 2 select
1067 // Received a SELECT
1068 resp
= resp3a
; respLen
= resp3aLen
; order
= 30;
1069 // DbpString("Select (cascade 2) request from reader:");
1070 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1073 } else if(receivedCmd
[0] == 0x30) {
1075 resp
= resp4
; respLen
= resp4Len
; order
= 4; // Do nothing
1076 DbpString("Read request from reader:");
1077 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1080 } else if(receivedCmd
[0] == 0x50) {
1082 resp
= resp1
; respLen
= 0; order
= 5; // Do nothing
1083 DbpString("Reader requested we HALT!:");
1085 } else if(receivedCmd
[0] == 0x60) {
1086 // Received an authentication request
1087 resp
= resp5
; respLen
= resp5Len
; order
= 7;
1088 DbpString("Authenticate request from reader:");
1089 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1091 } else if(receivedCmd
[0] == 0xE0) {
1092 // Received a RATS request
1093 resp
= resp1
; respLen
= 0;order
= 70;
1094 DbpString("RATS request from reader:");
1095 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1097 // Never seen this command before
1098 DbpString("Unknown command received from reader:");
1099 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1100 DbpIntegers(receivedCmd
[3], receivedCmd
[4], receivedCmd
[5]);
1101 DbpIntegers(receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1104 resp
= resp1
; respLen
= 0; order
= 0;
1107 // Count number of wakeups received after a halt
1108 if(order
== 6 && lastorder
== 5) { happened
++; }
1110 // Count number of other messages after a halt
1111 if(order
!= 6 && lastorder
== 5) { happened2
++; }
1113 // Look at last parity bit to determine timing of answer
1114 if((Uart
.parityBits
& 0x01) || receivedCmd
[0] == 0x52) {
1115 // 1236, so correction bit needed
1119 memset(receivedCmd
, 0x44, 32);
1121 if(cmdsRecvd
> 999) {
1122 DbpString("1000 commands later...");
1129 if(respLen
<= 0) continue;
1131 // Modulate Manchester
1132 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_MOD
);
1133 SSC_TRANSMIT_HOLDING
= 0x00;
1136 // ### Transmit the response ###
1139 fdt_indicator
= FALSE
;
1141 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
1142 volatile BYTE b
= (BYTE
)SSC_RECEIVE_HOLDING
;
1145 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
1153 SSC_TRANSMIT_HOLDING
= b
;
1159 if(BUTTON_PRESS()) {
1166 DbpIntegers(happened
, happened2
, cmdsRecvd
);
1170 //-----------------------------------------------------------------------------
1171 // Transmit the command (to the tag) that was placed in ToSend[].
1172 //-----------------------------------------------------------------------------
1173 static void TransmitFor14443a(const BYTE
*cmd
, int len
, int *samples
, int *wait
)
1177 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1179 if(*wait
< 10) { *wait
= 10; }
1181 for(c
= 0; c
< *wait
;) {
1182 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
1183 SSC_TRANSMIT_HOLDING
= 0x00; // For exact timing!
1186 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
1187 volatile DWORD r
= SSC_RECEIVE_HOLDING
;
1195 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
1196 SSC_TRANSMIT_HOLDING
= cmd
[c
];
1202 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
1203 volatile DWORD r
= SSC_RECEIVE_HOLDING
;
1208 *samples
= (c
+ *wait
) << 3;
1211 //-----------------------------------------------------------------------------
1212 // To generate an arbitrary stream from reader
1214 //-----------------------------------------------------------------------------
1215 void ArbitraryFromReader(const BYTE
*cmd
, int parity
, int len
)
1224 // Start of Communication (Seq. Z)
1228 for(i
= 0; i
< len
; i
++) {
1231 for(j
= 0; j
< 8; j
++) {
1251 // Predefined parity bit, the flipper flips when needed, because of flips in byte sent
1252 if(((parity
>> (len
- i
- 1)) & 1)) {
1269 // End of Communication
1287 // Convert from last character reference to length
1291 //-----------------------------------------------------------------------------
1292 // Code a 7-bit command without parity bit
1293 // This is especially for 0x26 and 0x52 (REQA and WUPA)
1294 //-----------------------------------------------------------------------------
1295 void ShortFrameFromReader(const BYTE
*cmd
)
1303 // Start of Communication (Seq. Z)
1308 for(j
= 0; j
< 7; j
++) {
1327 // End of Communication
1345 // Convert from last character reference to length
1349 //-----------------------------------------------------------------------------
1350 // Prepare reader command to send to FPGA
1352 //-----------------------------------------------------------------------------
1353 void CodeIso14443aAsReader(const BYTE
*cmd
, int len
)
1362 // Start of Communication (Seq. Z)
1366 for(i
= 0; i
< len
; i
++) {
1370 for(j
= 0; j
< 8; j
++) {
1371 oddparity
^= (b
& 1);
1408 // End of Communication
1426 // Convert from last character reference to length
1431 //-----------------------------------------------------------------------------
1432 // Wait a certain time for tag response
1433 // If a response is captured return TRUE
1434 // If it takes to long return FALSE
1435 //-----------------------------------------------------------------------------
1436 static BOOL
GetIso14443aAnswerFromTag(BYTE
*receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //BYTE *buffer
1438 // buffer needs to be 512 bytes
1441 // Set FPGA mode to "reader listen mode", no modulation (listen
1442 // only, since we are receiving, not transmitting).
1443 // Signal field is on with the appropriate LED
1445 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1447 // Now get the answer from the card
1448 Demod
.output
= receivedResponse
;
1450 Demod
.state
= DEMOD_UNSYNCD
;
1459 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
1460 SSC_TRANSMIT_HOLDING
= 0x00; // To make use of exact timing of next command from reader!!
1463 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
1464 if(c
< 512) { c
++; } else { return FALSE
; }
1465 b
= (BYTE
)SSC_RECEIVE_HOLDING
;
1466 if(ManchesterDecoding((b
& 0xf0) >> 4)) {
1467 *samples
= ((c
- 1) << 3) + 4;
1470 if(ManchesterDecoding(b
& 0x0f)) {
1478 //-----------------------------------------------------------------------------
1479 // Read an ISO 14443a tag. Send out commands and store answers.
1481 //-----------------------------------------------------------------------------
1482 void ReaderIso14443a(DWORD parameter
)
1485 static const BYTE cmd1
[] = { 0x52 }; // or 0x26
1486 static const BYTE cmd2
[] = { 0x93,0x20 };
1487 // UID = 0x2a,0x69,0x8d,0x43,0x8d, last two bytes are CRC bytes
1488 BYTE cmd3
[] = { 0x93,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1490 // For Ultralight add an extra anticollission layer -> 95 20 and then 95 70
1492 // greg - here we will add our cascade level 2 anticolission and select functions to deal with ultralight // and 7-byte UIDs in generall...
1493 BYTE cmd4
[] = {0x95,0x20}; // ask for cascade 2 select
1495 //BYTE cmd3a[] = { 0x95,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1499 BYTE cmd5
[] = { 0x95,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1502 // RATS (request for answer to select)
1503 //BYTE cmd6[] = { 0xe0,0x50,0xbc,0xa5 }; // original RATS
1504 BYTE cmd6
[] = { 0xe0,0x21,0xb2,0xc7 }; // Desfire RATS
1506 int reqaddr
= 2024; // was 2024 - tied to other size changes
1509 BYTE
*req1
= (((BYTE
*)BigBuf
) + reqaddr
);
1512 BYTE
*req2
= (((BYTE
*)BigBuf
) + reqaddr
+ reqsize
);
1515 BYTE
*req3
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 2));
1518 // greg added req 4 & 5 to deal with cascade 2 section
1519 BYTE
*req4
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 3));
1522 BYTE
*req5
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 4));
1525 BYTE
*req6
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 5));
1528 //BYTE *req7 = (((BYTE *)BigBuf) + reqaddr + (reqsize * 6));
1531 BYTE
*receivedAnswer
= (((BYTE
*)BigBuf
) + 3560); // was 3560 - tied to other size changes
1533 BYTE
*trace
= (BYTE
*)BigBuf
;
1537 memset(trace
, 0x44, 2000); // was 2000 - tied to oter size chnages
1538 // setting it to 3000 causes no tag responses to be detected (2900 is ok)
1539 // setting it to 1000 causes no tag responses to be detected
1541 // Prepare some commands!
1542 ShortFrameFromReader(cmd1
);
1543 memcpy(req1
, ToSend
, ToSendMax
); req1Len
= ToSendMax
;
1545 CodeIso14443aAsReader(cmd2
, sizeof(cmd2
));
1546 memcpy(req2
, ToSend
, ToSendMax
); req2Len
= ToSendMax
;
1548 CodeIso14443aAsReader(cmd3
, sizeof(cmd3
));
1549 memcpy(req3
, ToSend
, ToSendMax
); req3Len
= ToSendMax
;
1552 CodeIso14443aAsReader(cmd4
, sizeof(cmd4
)); // 4 is cascade 2 request
1553 memcpy(req4
, ToSend
, ToSendMax
); req4Len
= ToSendMax
;
1556 CodeIso14443aAsReader(cmd5
, sizeof(cmd5
)); // 5 is cascade 2 select
1557 memcpy(req5
, ToSend
, ToSendMax
); req5Len
= ToSendMax
;
1560 CodeIso14443aAsReader(cmd6
, sizeof(cmd6
));
1561 memcpy(req6
, ToSend
, ToSendMax
); req6Len
= ToSendMax
;
1566 // Start from off (no field generated)
1567 // Signal field is off with the appropriate LED
1569 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1572 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1575 // Now give it time to spin up.
1576 // Signal field is on with the appropriate LED
1578 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1591 // Send WUPA (or REQA)
1592 TransmitFor14443a(req1
, req1Len
, &tsamples
, &wait
);
1593 // Store answer in buffer
1594 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1595 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1596 trace
[traceLen
++] = 1;
1597 memcpy(trace
+traceLen
, cmd1
, 1);
1599 if(traceLen
> TRACE_LENGTH
) goto done
;
1601 while(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1602 if(BUTTON_PRESS()) goto done
;
1604 // No answer, just continue polling
1605 TransmitFor14443a(req1
, req1Len
, &tsamples
, &wait
);
1606 // Store answer in buffer
1607 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1608 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1609 trace
[traceLen
++] = 1;
1610 memcpy(trace
+traceLen
, cmd1
, 1);
1612 if(traceLen
> TRACE_LENGTH
) goto done
;
1615 // Store answer in buffer
1616 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1617 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1618 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1619 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1620 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1621 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1622 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1623 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1624 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1625 trace
[traceLen
++] = Demod
.len
;
1626 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1627 traceLen
+= Demod
.len
;
1628 if(traceLen
> TRACE_LENGTH
) goto done
;
1631 TransmitFor14443a(req2
, req2Len
, &tsamples
, &wait
);
1632 // Store answer in buffer
1633 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1634 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1635 trace
[traceLen
++] = 2;
1636 memcpy(trace
+traceLen
, cmd2
, 2);
1638 if(traceLen
> TRACE_LENGTH
) goto done
;
1640 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1644 // Store answer in buffer
1645 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1646 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1647 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1648 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1649 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1650 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1651 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1652 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1653 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1654 trace
[traceLen
++] = Demod
.len
;
1655 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1656 traceLen
+= Demod
.len
;
1657 if(traceLen
> TRACE_LENGTH
) goto done
;
1659 // Construct SELECT UID command
1660 // First copy the 5 bytes (Mifare Classic) after the 93 70
1661 memcpy(cmd3
+2,receivedAnswer
,5);
1662 // Secondly compute the two CRC bytes at the end
1663 ComputeCrc14443(CRC_14443_A
, cmd3
, 7, &cmd3
[7], &cmd3
[8]);
1664 // Prepare the bit sequence to modulate the subcarrier
1665 // Store answer in buffer
1666 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1667 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1668 trace
[traceLen
++] = 9;
1669 memcpy(trace
+traceLen
, cmd3
, 9);
1671 if(traceLen
> TRACE_LENGTH
) goto done
;
1672 CodeIso14443aAsReader(cmd3
, sizeof(cmd3
));
1673 memcpy(req3
, ToSend
, ToSendMax
); req3Len
= ToSendMax
;
1676 TransmitFor14443a(req3
, req3Len
, &samples
, &wait
);
1677 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1681 // Store answer in buffer
1682 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1683 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1684 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1685 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1686 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1687 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1688 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1689 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1690 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1691 trace
[traceLen
++] = Demod
.len
;
1692 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1693 traceLen
+= Demod
.len
;
1694 if(traceLen
> TRACE_LENGTH
) goto done
;
1696 // OK we have selected at least at cascade 1, lets see if first byte of UID was 0x88 in
1697 // which case we need to make a cascade 2 request and select - this is a long UID
1698 if (receivedAnswer
[0] == 0x88)
1700 // Do cascade level 2 stuff
1701 ///////////////////////////////////////////////////////////////////
1702 // First issue a '95 20' identify request
1703 // Ask for card UID (part 2)
1704 TransmitFor14443a(req4
, req4Len
, &tsamples
, &wait
);
1705 // Store answer in buffer
1706 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1707 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1708 trace
[traceLen
++] = 2;
1709 memcpy(trace
+traceLen
, cmd4
, 2);
1711 if(traceLen
> TRACE_LENGTH
) {
1712 DbpString("Bugging out, just popped tracelength");
1715 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1718 // Store answer in buffer
1719 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1720 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1721 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1722 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1723 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1724 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1725 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1726 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1727 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1728 trace
[traceLen
++] = Demod
.len
;
1729 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1730 traceLen
+= Demod
.len
;
1731 if(traceLen
> TRACE_LENGTH
) goto done
;
1732 //////////////////////////////////////////////////////////////////
1733 // Then Construct SELECT UID (cascasde 2) command
1734 DbpString("Just about to copy the UID out of the cascade 2 id req");
1735 // First copy the 5 bytes (Mifare Classic) after the 95 70
1736 memcpy(cmd5
+2,receivedAnswer
,5);
1737 // Secondly compute the two CRC bytes at the end
1738 ComputeCrc14443(CRC_14443_A
, cmd4
, 7, &cmd5
[7], &cmd5
[8]);
1739 // Prepare the bit sequence to modulate the subcarrier
1740 // Store answer in buffer
1741 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1742 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1743 trace
[traceLen
++] = 9;
1744 memcpy(trace
+traceLen
, cmd5
, 9);
1746 if(traceLen
> TRACE_LENGTH
) goto done
;
1747 CodeIso14443aAsReader(cmd5
, sizeof(cmd5
));
1748 memcpy(req5
, ToSend
, ToSendMax
); req5Len
= ToSendMax
;
1751 TransmitFor14443a(req4
, req4Len
, &samples
, &wait
);
1752 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1756 // Store answer in buffer
1757 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1758 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1759 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1760 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1761 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1762 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1763 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1764 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1765 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1766 trace
[traceLen
++] = Demod
.len
;
1767 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1768 traceLen
+= Demod
.len
;
1769 if(traceLen
> TRACE_LENGTH
) goto done
;
1773 // Secondly compute the two CRC bytes at the end
1774 ComputeCrc14443(CRC_14443_A
, cmd5
, 2, &cmd5
[2], &cmd5
[3]);
1775 // Send authentication request (Mifare Classic)
1776 TransmitFor14443a(req5
, req5Len
, &samples
, &wait
);
1777 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1778 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1779 trace
[traceLen
++] = 4;
1780 memcpy(trace
+traceLen
, cmd5
, 4);
1782 if(traceLen
> TRACE_LENGTH
) goto done
;
1783 if(GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1785 // We received probably a random, continue and trace!
1792 // Trace the random, i'm curious
1793 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1794 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1795 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1796 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1797 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1798 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1799 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1800 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1801 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1802 trace
[traceLen
++] = Demod
.len
;
1803 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1804 traceLen
+= Demod
.len
;
1805 if(traceLen
> TRACE_LENGTH
) goto done
;
1814 DbpIntegers(rsamples
, 0xCC, 0xCC);
1815 DbpString("ready..");