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 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_SNIFFER
);
571 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
573 // Setup for the DMA.
576 lastRxCounter
= DMA_BUFFER_SIZE
;
577 FpgaSetupSscDma((BYTE
*)dmaBuf
, DMA_BUFFER_SIZE
);
581 // And now we loop, receiving samples.
584 int behindBy
= (lastRxCounter
- PDC_RX_COUNTER(SSC_BASE
)) &
586 if(behindBy
> maxBehindBy
) {
587 maxBehindBy
= behindBy
;
589 DbpString("blew circular buffer!");
593 if(behindBy
< 1) continue;
598 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
599 upTo
-= DMA_BUFFER_SIZE
;
600 lastRxCounter
+= DMA_BUFFER_SIZE
;
601 PDC_RX_NEXT_POINTER(SSC_BASE
) = (DWORD
)upTo
;
602 PDC_RX_NEXT_COUNTER(SSC_BASE
) = DMA_BUFFER_SIZE
;
606 #define HANDLE_BIT_IF_BODY \
609 trace[traceLen++] = ((rsamples >> 0) & 0xff); \
610 trace[traceLen++] = ((rsamples >> 8) & 0xff); \
611 trace[traceLen++] = ((rsamples >> 16) & 0xff); \
612 trace[traceLen++] = ((rsamples >> 24) & 0xff); \
613 trace[traceLen++] = ((Uart.parityBits >> 0) & 0xff); \
614 trace[traceLen++] = ((Uart.parityBits >> 8) & 0xff); \
615 trace[traceLen++] = ((Uart.parityBits >> 16) & 0xff); \
616 trace[traceLen++] = ((Uart.parityBits >> 24) & 0xff); \
617 trace[traceLen++] = Uart.byteCnt; \
618 memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \
619 traceLen += Uart.byteCnt; \
620 if(traceLen > TRACE_LENGTH) break; \
622 /* And ready to receive another command. */ \
623 Uart.state = STATE_UNSYNCD; \
624 /* And also reset the demod code, which might have been */ \
625 /* false-triggered by the commands from the reader. */ \
626 Demod.state = DEMOD_UNSYNCD; \
629 if(MillerDecoding((smpl & 0xF0) >> 4)) {
630 rsamples
= samples
- Uart
.samples
;
633 if(ManchesterDecoding(smpl
& 0x0F)) {
634 rsamples
= samples
- Demod
.samples
;
637 // timestamp, as a count of samples
638 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
639 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
640 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
641 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
642 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
643 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
644 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
645 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
647 trace
[traceLen
++] = Demod
.len
;
648 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
649 traceLen
+= Demod
.len
;
650 if(traceLen
> TRACE_LENGTH
) break;
654 // And ready to receive another response.
655 memset(&Demod
, 0, sizeof(Demod
));
656 Demod
.output
= receivedResponse
;
657 Demod
.state
= DEMOD_UNSYNCD
;
662 DbpString("cancelled_a");
667 DbpString("COMMAND FINISHED");
669 DbpIntegers(maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
670 DbpIntegers(Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
673 PDC_CONTROL(SSC_BASE
) = PDC_RX_DISABLE
;
674 DbpIntegers(maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
675 DbpIntegers(Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]);
682 // Prepare communication bits to send to FPGA
683 void Sequence(SecType seq
)
689 // Sequence D: 11110000
690 // modulation with subcarrier during first half
691 ToSend
[ToSendMax
] = 0xf0;
694 // Sequence E: 00001111
695 // modulation with subcarrier during second half
696 ToSend
[ToSendMax
] = 0x0f;
699 // Sequence F: 00000000
700 // no modulation with subcarrier
701 ToSend
[ToSendMax
] = 0x00;
705 // Sequence X: 00001100
706 // drop after half a period
707 ToSend
[ToSendMax
] = 0x0c;
711 // Sequence Y: 00000000
713 ToSend
[ToSendMax
] = 0x00;
716 // Sequence Z: 11000000
718 ToSend
[ToSendMax
] = 0xc0;
723 //-----------------------------------------------------------------------------
724 // Prepare tag messages
725 //-----------------------------------------------------------------------------
726 static void CodeIso14443aAsTag(const BYTE
*cmd
, int len
)
733 // Correction bit, might be removed when not needed
738 ToSendStuffBit(1); // 1
746 for(i
= 0; i
< len
; i
++) {
752 for(j
= 0; j
< 8; j
++) {
753 oddparity
^= (b
& 1);
773 // Flush the buffer in FPGA!!
774 for(i
= 0; i
< 5; i
++) {
778 // Convert from last byte pos to length
781 // Add a few more for slop
782 ToSend
[ToSendMax
++] = 0x00;
783 ToSend
[ToSendMax
++] = 0x00;
787 //-----------------------------------------------------------------------------
788 // This is to send a NACK kind of answer, its only 3 bits, I know it should be 4
789 //-----------------------------------------------------------------------------
790 static void CodeStrangeAnswer()
796 // Correction bit, might be removed when not needed
801 ToSendStuffBit(1); // 1
821 // Flush the buffer in FPGA!!
822 for(i
= 0; i
< 5; i
++) {
826 // Convert from last byte pos to length
829 // Add a few more for slop
830 ToSend
[ToSendMax
++] = 0x00;
831 ToSend
[ToSendMax
++] = 0x00;
835 //-----------------------------------------------------------------------------
836 // Wait for commands from reader
837 // Stop when button is pressed
838 // Or return TRUE when command is captured
839 //-----------------------------------------------------------------------------
840 static BOOL
GetIso14443aCommandFromReader(BYTE
*received
, int *len
, int maxLen
)
842 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
843 // only, since we are receiving, not transmitting).
844 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
);
846 // Now run a `software UART' on the stream of incoming samples.
847 Uart
.output
= received
;
848 Uart
.byteCntMax
= maxLen
;
849 Uart
.state
= STATE_UNSYNCD
;
854 if(BUTTON_PRESS()) return FALSE
;
856 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
857 SSC_TRANSMIT_HOLDING
= 0x00;
859 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
860 BYTE b
= (BYTE
)SSC_RECEIVE_HOLDING
;
861 if(MillerDecoding((b
& 0xf0) >> 4)) {
865 if(MillerDecoding(b
& 0x0f)) {
873 //-----------------------------------------------------------------------------
874 // Main loop of simulated tag: receive commands from reader, decide what
875 // response to send, and send it.
876 //-----------------------------------------------------------------------------
877 void SimulateIso14443aTag(int tagType
, int TagUid
)
879 // This function contains the tag emulation
881 // Prepare protocol messages
882 // static const BYTE cmd1[] = { 0x26 };
883 // static const BYTE response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg
885 static const BYTE response1
[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me
886 // static const BYTE response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me
889 // static const BYTE cmd2[] = { 0x93, 0x20 };
890 //static const BYTE response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg
895 static const BYTE response2
[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips
898 // When reader selects us during cascade1 it will send cmd3
899 //BYTE response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE)
900 BYTE response3
[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire)
901 ComputeCrc14443(CRC_14443_A
, response3
, 1, &response3
[1], &response3
[2]);
903 // send cascade2 2nd half of UID
904 static const BYTE response2a
[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck
905 // NOTE : THE CRC on the above may be wrong as I have obfuscated the actual UID
908 // When reader selects us during cascade2 it will send cmd3a
909 //BYTE response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE)
910 BYTE response3a
[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire)
911 ComputeCrc14443(CRC_14443_A
, response3a
, 1, &response3a
[1], &response3a
[2]);
913 // When reader tries to authenticate
914 // static const BYTE cmd5[] = { 0x60, 0x00, 0xf5, 0x7b };
915 static const BYTE response5
[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce
920 // Longest possible response will be 16 bytes + 2 CRC = 18 bytes
922 // 144 data bits (18 * 8)
925 // 1 Correction bit (Answer in 1172 or 1236 periods, see FPGA)
926 // 1 just for the case
930 // 166 bytes, since every bit that needs to be send costs us a byte
934 // Respond with card type
935 BYTE
*resp1
= (((BYTE
*)BigBuf
) + 800);
938 // Anticollision cascade1 - respond with uid
939 BYTE
*resp2
= (((BYTE
*)BigBuf
) + 970);
942 // Anticollision cascade2 - respond with 2nd half of uid if asked
943 // we're only going to be asked if we set the 1st byte of the UID (during cascade1) to 0x88
944 BYTE
*resp2a
= (((BYTE
*)BigBuf
) + 1140);
947 // Acknowledge select - cascade 1
948 BYTE
*resp3
= (((BYTE
*)BigBuf
) + 1310);
951 // Acknowledge select - cascade 2
952 BYTE
*resp3a
= (((BYTE
*)BigBuf
) + 1480);
955 // Response to a read request - not implemented atm
956 BYTE
*resp4
= (((BYTE
*)BigBuf
) + 1550);
959 // Authenticate response - nonce
960 BYTE
*resp5
= (((BYTE
*)BigBuf
) + 1720);
963 BYTE
*receivedCmd
= (BYTE
*)BigBuf
;
970 // To control where we are in the protocol
974 // Just to allow some checks
982 memset(receivedCmd
, 0x44, 400);
984 // Prepare the responses of the anticollision phase
985 // there will be not enough time to do this at the moment the reader sends it REQA
988 CodeIso14443aAsTag(response1
, sizeof(response1
));
989 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
991 // Send our UID (cascade 1)
992 CodeIso14443aAsTag(response2
, sizeof(response2
));
993 memcpy(resp2
, ToSend
, ToSendMax
); resp2Len
= ToSendMax
;
995 // Answer to select (cascade1)
996 CodeIso14443aAsTag(response3
, sizeof(response3
));
997 memcpy(resp3
, ToSend
, ToSendMax
); resp3Len
= ToSendMax
;
999 // Send the cascade 2 2nd part of the uid
1000 CodeIso14443aAsTag(response2a
, sizeof(response2a
));
1001 memcpy(resp2a
, ToSend
, ToSendMax
); resp2aLen
= ToSendMax
;
1003 // Answer to select (cascade 2)
1004 CodeIso14443aAsTag(response3a
, sizeof(response3a
));
1005 memcpy(resp3a
, ToSend
, ToSendMax
); resp3aLen
= ToSendMax
;
1007 // Strange answer is an example of rare message size (3 bits)
1008 CodeStrangeAnswer();
1009 memcpy(resp4
, ToSend
, ToSendMax
); resp4Len
= ToSendMax
;
1011 // Authentication answer (random nonce)
1012 CodeIso14443aAsTag(response5
, sizeof(response5
));
1013 memcpy(resp5
, ToSend
, ToSendMax
); resp5Len
= ToSendMax
;
1015 // We need to listen to the high-frequency, peak-detected path.
1016 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1024 if(!GetIso14443aCommandFromReader(receivedCmd
, &len
, 100)) {
1025 DbpString("button press");
1028 // 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
1029 // Okay, look at the command now.
1031 i
= 1; // first byte transmitted
1032 if(receivedCmd
[0] == 0x26) {
1033 // Received a REQUEST
1034 resp
= resp1
; respLen
= resp1Len
; order
= 1;
1035 //DbpString("Hello request from reader:");
1036 } else if(receivedCmd
[0] == 0x52) {
1037 // Received a WAKEUP
1038 resp
= resp1
; respLen
= resp1Len
; order
= 6;
1039 // //DbpString("Wakeup request from reader:");
1041 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] == 0x93) { // greg - cascade 1 anti-collision
1042 // Received request for UID (cascade 1)
1043 resp
= resp2
; respLen
= resp2Len
; order
= 2;
1044 // DbpString("UID (cascade 1) request from reader:");
1045 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1048 } else if(receivedCmd
[1] == 0x20 && receivedCmd
[0] ==0x95) { // greg - cascade 2 anti-collision
1049 // Received request for UID (cascade 2)
1050 resp
= resp2a
; respLen
= resp2aLen
; order
= 20;
1051 // DbpString("UID (cascade 2) request from reader:");
1052 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1055 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x93) { // greg - cascade 1 select
1056 // Received a SELECT
1057 resp
= resp3
; respLen
= resp3Len
; order
= 3;
1058 // DbpString("Select (cascade 1) request from reader:");
1059 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1062 } else if(receivedCmd
[1] == 0x70 && receivedCmd
[0] ==0x95) { // greg - cascade 2 select
1063 // Received a SELECT
1064 resp
= resp3a
; respLen
= resp3aLen
; order
= 30;
1065 // DbpString("Select (cascade 2) request from reader:");
1066 // DbpIntegers(receivedCmd[0], receivedCmd[1], receivedCmd[2]);
1069 } else if(receivedCmd
[0] == 0x30) {
1071 resp
= resp4
; respLen
= resp4Len
; order
= 4; // Do nothing
1072 DbpString("Read request from reader:");
1073 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1076 } else if(receivedCmd
[0] == 0x50) {
1078 resp
= resp1
; respLen
= 0; order
= 5; // Do nothing
1079 DbpString("Reader requested we HALT!:");
1081 } else if(receivedCmd
[0] == 0x60) {
1082 // Received an authentication request
1083 resp
= resp5
; respLen
= resp5Len
; order
= 7;
1084 DbpString("Authenticate request from reader:");
1085 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1087 } else if(receivedCmd
[0] == 0xE0) {
1088 // Received a RATS request
1089 resp
= resp1
; respLen
= 0;order
= 70;
1090 DbpString("RATS request from reader:");
1091 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1093 // Never seen this command before
1094 DbpString("Unknown command received from reader:");
1095 DbpIntegers(receivedCmd
[0], receivedCmd
[1], receivedCmd
[2]);
1096 DbpIntegers(receivedCmd
[3], receivedCmd
[4], receivedCmd
[5]);
1097 DbpIntegers(receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]);
1100 resp
= resp1
; respLen
= 0; order
= 0;
1103 // Count number of wakeups received after a halt
1104 if(order
== 6 && lastorder
== 5) { happened
++; }
1106 // Count number of other messages after a halt
1107 if(order
!= 6 && lastorder
== 5) { happened2
++; }
1109 // Look at last parity bit to determine timing of answer
1110 if((Uart
.parityBits
& 0x01) || receivedCmd
[0] == 0x52) {
1111 // 1236, so correction bit needed
1115 memset(receivedCmd
, 0x44, 32);
1117 if(cmdsRecvd
> 999) {
1118 DbpString("1000 commands later...");
1125 if(respLen
<= 0) continue;
1127 // Modulate Manchester
1128 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_TAGSIM_MOD
);
1129 SSC_TRANSMIT_HOLDING
= 0x00;
1132 // ### Transmit the response ###
1135 fdt_indicator
= FALSE
;
1137 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
1138 volatile BYTE b
= (BYTE
)SSC_RECEIVE_HOLDING
;
1141 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
1149 SSC_TRANSMIT_HOLDING
= b
;
1155 if(BUTTON_PRESS()) {
1162 DbpIntegers(happened
, happened2
, cmdsRecvd
);
1166 //-----------------------------------------------------------------------------
1167 // Transmit the command (to the tag) that was placed in ToSend[].
1168 //-----------------------------------------------------------------------------
1169 static void TransmitFor14443a(const BYTE
*cmd
, int len
, int *samples
, int *wait
)
1173 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1175 if(*wait
< 10) { *wait
= 10; }
1177 for(c
= 0; c
< *wait
;) {
1178 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
1179 SSC_TRANSMIT_HOLDING
= 0x00; // For exact timing!
1182 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
1183 volatile DWORD r
= SSC_RECEIVE_HOLDING
;
1191 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
1192 SSC_TRANSMIT_HOLDING
= cmd
[c
];
1198 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
1199 volatile DWORD r
= SSC_RECEIVE_HOLDING
;
1204 *samples
= (c
+ *wait
) << 3;
1207 //-----------------------------------------------------------------------------
1208 // To generate an arbitrary stream from reader
1210 //-----------------------------------------------------------------------------
1211 void ArbitraryFromReader(const BYTE
*cmd
, int parity
, int len
)
1220 // Start of Communication (Seq. Z)
1224 for(i
= 0; i
< len
; i
++) {
1227 for(j
= 0; j
< 8; j
++) {
1247 // Predefined parity bit, the flipper flips when needed, because of flips in byte sent
1248 if(((parity
>> (len
- i
- 1)) & 1)) {
1265 // End of Communication
1283 // Convert from last character reference to length
1287 //-----------------------------------------------------------------------------
1288 // Code a 7-bit command without parity bit
1289 // This is especially for 0x26 and 0x52 (REQA and WUPA)
1290 //-----------------------------------------------------------------------------
1291 void ShortFrameFromReader(const BYTE
*cmd
)
1299 // Start of Communication (Seq. Z)
1304 for(j
= 0; j
< 7; j
++) {
1323 // End of Communication
1341 // Convert from last character reference to length
1345 //-----------------------------------------------------------------------------
1346 // Prepare reader command to send to FPGA
1348 //-----------------------------------------------------------------------------
1349 void CodeIso14443aAsReader(const BYTE
*cmd
, int len
)
1358 // Start of Communication (Seq. Z)
1362 for(i
= 0; i
< len
; i
++) {
1366 for(j
= 0; j
< 8; j
++) {
1367 oddparity
^= (b
& 1);
1404 // End of Communication
1422 // Convert from last character reference to length
1427 //-----------------------------------------------------------------------------
1428 // Wait a certain time for tag response
1429 // If a response is captured return TRUE
1430 // If it takes to long return FALSE
1431 //-----------------------------------------------------------------------------
1432 static BOOL
GetIso14443aAnswerFromTag(BYTE
*receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //BYTE *buffer
1434 // buffer needs to be 512 bytes
1437 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
1438 // only, since we are receiving, not transmitting).
1439 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_LISTEN
);
1441 // Now get the answer from the card
1442 Demod
.output
= receivedResponse
;
1444 Demod
.state
= DEMOD_UNSYNCD
;
1453 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
1454 SSC_TRANSMIT_HOLDING
= 0x00; // To make use of exact timing of next command from reader!!
1457 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
1458 if(c
< 512) { c
++; } else { return FALSE
; }
1459 b
= (BYTE
)SSC_RECEIVE_HOLDING
;
1460 if(ManchesterDecoding((b
& 0xf0) >> 4)) {
1461 *samples
= ((c
- 1) << 3) + 4;
1464 if(ManchesterDecoding(b
& 0x0f)) {
1472 //-----------------------------------------------------------------------------
1473 // Read an ISO 14443a tag. Send out commands and store answers.
1475 //-----------------------------------------------------------------------------
1476 void ReaderIso14443a(DWORD parameter
)
1479 static const BYTE cmd1
[] = { 0x52 }; // or 0x26
1480 static const BYTE cmd2
[] = { 0x93,0x20 };
1481 // UID = 0x2a,0x69,0x8d,0x43,0x8d, last two bytes are CRC bytes
1482 BYTE cmd3
[] = { 0x93,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1484 // For Ultralight add an extra anticollission layer -> 95 20 and then 95 70
1486 // greg - here we will add our cascade level 2 anticolission and select functions to deal with ultralight // and 7-byte UIDs in generall...
1487 BYTE cmd4
[] = {0x95,0x20}; // ask for cascade 2 select
1489 //BYTE cmd3a[] = { 0x95,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1493 BYTE cmd5
[] = { 0x95,0x70,0x2a,0x69,0x8d,0x43,0x8d,0x52,0x55 };
1496 // RATS (request for answer to select)
1497 //BYTE cmd6[] = { 0xe0,0x50,0xbc,0xa5 }; // original RATS
1498 BYTE cmd6
[] = { 0xe0,0x21,0xb2,0xc7 }; // Desfire RATS
1500 int reqaddr
= 2024; // was 2024 - tied to other size changes
1503 BYTE
*req1
= (((BYTE
*)BigBuf
) + reqaddr
);
1506 BYTE
*req2
= (((BYTE
*)BigBuf
) + reqaddr
+ reqsize
);
1509 BYTE
*req3
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 2));
1512 // greg added req 4 & 5 to deal with cascade 2 section
1513 BYTE
*req4
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 3));
1516 BYTE
*req5
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 4));
1519 BYTE
*req6
= (((BYTE
*)BigBuf
) + reqaddr
+ (reqsize
* 5));
1522 //BYTE *req7 = (((BYTE *)BigBuf) + reqaddr + (reqsize * 6));
1525 BYTE
*receivedAnswer
= (((BYTE
*)BigBuf
) + 3560); // was 3560 - tied to other size changes
1527 BYTE
*trace
= (BYTE
*)BigBuf
;
1531 memset(trace
, 0x44, 2000); // was 2000 - tied to oter size chnages
1532 // setting it to 3000 causes no tag responses to be detected (2900 is ok)
1533 // setting it to 1000 causes no tag responses to be detected
1535 // Prepare some commands!
1536 ShortFrameFromReader(cmd1
);
1537 memcpy(req1
, ToSend
, ToSendMax
); req1Len
= ToSendMax
;
1539 CodeIso14443aAsReader(cmd2
, sizeof(cmd2
));
1540 memcpy(req2
, ToSend
, ToSendMax
); req2Len
= ToSendMax
;
1542 CodeIso14443aAsReader(cmd3
, sizeof(cmd3
));
1543 memcpy(req3
, ToSend
, ToSendMax
); req3Len
= ToSendMax
;
1546 CodeIso14443aAsReader(cmd4
, sizeof(cmd4
)); // 4 is cascade 2 request
1547 memcpy(req4
, ToSend
, ToSendMax
); req4Len
= ToSendMax
;
1550 CodeIso14443aAsReader(cmd5
, sizeof(cmd5
)); // 5 is cascade 2 select
1551 memcpy(req5
, ToSend
, ToSendMax
); req5Len
= ToSendMax
;
1554 CodeIso14443aAsReader(cmd6
, sizeof(cmd6
));
1555 memcpy(req6
, ToSend
, ToSendMax
); req6Len
= ToSendMax
;
1560 // Start from off (no field generated)
1561 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1564 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1567 // Now give it time to spin up.
1568 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A
| FPGA_HF_ISO14443A_READER_MOD
);
1582 // Send WUPA (or REQA)
1583 TransmitFor14443a(req1
, req1Len
, &tsamples
, &wait
);
1584 // Store answer in buffer
1585 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1586 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1587 trace
[traceLen
++] = 1;
1588 memcpy(trace
+traceLen
, cmd1
, 1);
1590 if(traceLen
> TRACE_LENGTH
) goto done
;
1592 while(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1593 if(BUTTON_PRESS()) goto done
;
1595 // No answer, just continue polling
1596 TransmitFor14443a(req1
, req1Len
, &tsamples
, &wait
);
1597 // Store answer in buffer
1598 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1599 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1600 trace
[traceLen
++] = 1;
1601 memcpy(trace
+traceLen
, cmd1
, 1);
1603 if(traceLen
> TRACE_LENGTH
) goto done
;
1606 // Store answer in buffer
1607 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1608 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1609 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1610 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1611 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1612 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1613 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1614 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1615 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1616 trace
[traceLen
++] = Demod
.len
;
1617 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1618 traceLen
+= Demod
.len
;
1619 if(traceLen
> TRACE_LENGTH
) goto done
;
1622 TransmitFor14443a(req2
, req2Len
, &tsamples
, &wait
);
1623 // Store answer in buffer
1624 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1625 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1626 trace
[traceLen
++] = 2;
1627 memcpy(trace
+traceLen
, cmd2
, 2);
1629 if(traceLen
> TRACE_LENGTH
) goto done
;
1631 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1635 // Store answer in buffer
1636 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1637 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1638 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1639 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1640 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1641 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1642 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1643 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1644 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1645 trace
[traceLen
++] = Demod
.len
;
1646 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1647 traceLen
+= Demod
.len
;
1648 if(traceLen
> TRACE_LENGTH
) goto done
;
1650 // Construct SELECT UID command
1651 // First copy the 5 bytes (Mifare Classic) after the 93 70
1652 memcpy(cmd3
+2,receivedAnswer
,5);
1653 // Secondly compute the two CRC bytes at the end
1654 ComputeCrc14443(CRC_14443_A
, cmd3
, 7, &cmd3
[7], &cmd3
[8]);
1655 // Prepare the bit sequence to modulate the subcarrier
1656 // Store answer in buffer
1657 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1658 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1659 trace
[traceLen
++] = 9;
1660 memcpy(trace
+traceLen
, cmd3
, 9);
1662 if(traceLen
> TRACE_LENGTH
) goto done
;
1663 CodeIso14443aAsReader(cmd3
, sizeof(cmd3
));
1664 memcpy(req3
, ToSend
, ToSendMax
); req3Len
= ToSendMax
;
1667 TransmitFor14443a(req3
, req3Len
, &samples
, &wait
);
1668 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1672 // Store answer in buffer
1673 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1674 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1675 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1676 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1677 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1678 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1679 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1680 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1681 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1682 trace
[traceLen
++] = Demod
.len
;
1683 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1684 traceLen
+= Demod
.len
;
1685 if(traceLen
> TRACE_LENGTH
) goto done
;
1687 // OK we have selected at least at cascade 1, lets see if first byte of UID was 0x88 in
1688 // which case we need to make a cascade 2 request and select - this is a long UID
1689 if (receivedAnswer
[0] == 0x88)
1691 // Do cascade level 2 stuff
1692 ///////////////////////////////////////////////////////////////////
1693 // First issue a '95 20' identify request
1694 // Ask for card UID (part 2)
1695 TransmitFor14443a(req4
, req4Len
, &tsamples
, &wait
);
1696 // Store answer in buffer
1697 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1698 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1699 trace
[traceLen
++] = 2;
1700 memcpy(trace
+traceLen
, cmd4
, 2);
1702 if(traceLen
> TRACE_LENGTH
) {
1703 DbpString("Bugging out, just popped tracelength");
1706 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1709 // Store answer in buffer
1710 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1711 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1712 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1713 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1714 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1715 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1716 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1717 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1718 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1719 trace
[traceLen
++] = Demod
.len
;
1720 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1721 traceLen
+= Demod
.len
;
1722 if(traceLen
> TRACE_LENGTH
) goto done
;
1723 //////////////////////////////////////////////////////////////////
1724 // Then Construct SELECT UID (cascasde 2) command
1725 DbpString("Just about to copy the UID out of the cascade 2 id req");
1726 // First copy the 5 bytes (Mifare Classic) after the 95 70
1727 memcpy(cmd5
+2,receivedAnswer
,5);
1728 // Secondly compute the two CRC bytes at the end
1729 ComputeCrc14443(CRC_14443_A
, cmd4
, 7, &cmd5
[7], &cmd5
[8]);
1730 // Prepare the bit sequence to modulate the subcarrier
1731 // Store answer in buffer
1732 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1733 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1734 trace
[traceLen
++] = 9;
1735 memcpy(trace
+traceLen
, cmd5
, 9);
1737 if(traceLen
> TRACE_LENGTH
) goto done
;
1738 CodeIso14443aAsReader(cmd5
, sizeof(cmd5
));
1739 memcpy(req5
, ToSend
, ToSendMax
); req5Len
= ToSendMax
;
1742 TransmitFor14443a(req4
, req4Len
, &samples
, &wait
);
1743 if(!GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1747 // Store answer in buffer
1748 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1749 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1750 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1751 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1752 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1753 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1754 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1755 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1756 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1757 trace
[traceLen
++] = Demod
.len
;
1758 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1759 traceLen
+= Demod
.len
;
1760 if(traceLen
> TRACE_LENGTH
) goto done
;
1771 // Secondly compute the two CRC bytes at the end
1772 ComputeCrc14443(CRC_14443_A
, cmd5
, 2, &cmd5
[2], &cmd5
[3]);
1773 // Send authentication request (Mifare Classic)
1774 TransmitFor14443a(req5
, req5Len
, &samples
, &wait
);
1775 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1776 trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0; trace
[traceLen
++] = 0;
1777 trace
[traceLen
++] = 4;
1778 memcpy(trace
+traceLen
, cmd5
, 4);
1780 if(traceLen
> TRACE_LENGTH
) goto done
;
1781 if(GetIso14443aAnswerFromTag(receivedAnswer
, 100, &samples
, &elapsed
)) {
1783 // We received probably a random, continue and trace!
1790 // Trace the random, i'm curious
1791 rsamples
= rsamples
+ (samples
- Demod
.samples
);
1792 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
1793 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
1794 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
1795 trace
[traceLen
++] = 0x80 | ((rsamples
>> 24) & 0xff);
1796 trace
[traceLen
++] = ((Demod
.parityBits
>> 0) & 0xff);
1797 trace
[traceLen
++] = ((Demod
.parityBits
>> 8) & 0xff);
1798 trace
[traceLen
++] = ((Demod
.parityBits
>> 16) & 0xff);
1799 trace
[traceLen
++] = ((Demod
.parityBits
>> 24) & 0xff);
1800 trace
[traceLen
++] = Demod
.len
;
1801 memcpy(trace
+traceLen
, receivedAnswer
, Demod
.len
);
1802 traceLen
+= Demod
.len
;
1803 if(traceLen
> TRACE_LENGTH
) goto done
;
1813 DbpIntegers(rsamples
, 0xCC, 0xCC);
1814 DbpString("ready..");