1 //----------------------------------------------------------------------------- 
   2 // Gerhard de Koning Gans - May 2008 
   3 // Hagen Fritsch - June 2010 
   4 // Gerhard de Koning Gans - May 2011 
   5 // Gerhard de Koning Gans - June 2012 - Added iClass card and reader emulation 
   7 // This code is licensed to you under the terms of the GNU GPL, version 2 or, 
   8 // at your option, any later version. See the LICENSE.txt file for the text of 
  10 //----------------------------------------------------------------------------- 
  11 // Routines to support iClass. 
  12 //----------------------------------------------------------------------------- 
  13 // Based on ISO14443a implementation. Still in experimental phase. 
  14 // Contribution made during a security research at Radboud University Nijmegen 
  16 // Please feel free to contribute and extend iClass support!! 
  17 //----------------------------------------------------------------------------- 
  21 // We still have sometimes a demodulation error when snooping iClass communication. 
  22 // The resulting trace of a read-block-03 command may look something like this: 
  24 //  +  22279:    :     0c  03  e8  01     
  26 //    ...with an incorrect answer... 
  28 //  +     85:   0: TAG ff! ff! ff! ff! ff! ff! ff! ff! bb  33  bb  00  01! 0e! 04! bb     !crc 
  30 // We still left the error signalling bytes in the traces like 0xbb 
  32 // A correct trace should look like this: 
  34 // +  21112:    :     0c  03  e8  01     
  35 // +     85:   0: TAG ff  ff  ff  ff  ff  ff  ff  ff  ea  f5     
  37 //----------------------------------------------------------------------------- 
  39 #include "../include/proxmark3.h" 
  45 // Needed for CRC in emulation mode; 
  46 // same construction as in ISO 14443; 
  47 // different initial value (CRC_ICLASS) 
  48 #include "../common/iso14443crc.h" 
  49 #include "../common/iso15693tools.h" 
  50 #include "iso15693tools.h" 
  53 static int timeout 
= 4096; 
  56 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
); 
  58 //----------------------------------------------------------------------------- 
  59 // The software UART that receives commands from the reader, and its state 
  61 //----------------------------------------------------------------------------- 
  65         STATE_START_OF_COMMUNICATION
, 
  86 static RAMFUNC 
int OutOfNDecoding(int bit
) 
  92                 Uart
.bitBuffer 
= bit 
^ 0xFF0; 
  97                 Uart
.bitBuffer 
^= bit
; 
 101                 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF; 
 104                 if(Uart.byteCnt > 15) { return TRUE; } 
 110         if(Uart
.state 
!= STATE_UNSYNCD
) { 
 113                 if((Uart
.bitBuffer 
& Uart
.syncBit
) ^ Uart
.syncBit
) { 
 119                 if(((Uart
.bitBuffer 
<< 1) & Uart
.syncBit
) ^ Uart
.syncBit
) { 
 125                 if(bit 
!= bitright
) { bit 
= bitright
; } 
 128                 // So, now we only have to deal with *bit*, lets see... 
 129                 if(Uart
.posCnt 
== 1) { 
 130                         // measurement first half bitperiod 
 132                                 // Drop in first half means that we are either seeing 
 135                                 if(Uart
.nOutOfCnt 
== 1) { 
 136                                         // End of Communication 
 137                                         Uart
.state 
= STATE_UNSYNCD
; 
 139                                         if(Uart
.byteCnt 
== 0) { 
 140                                                 // Its not straightforward to show single EOFs 
 141                                                 // So just leave it and do not return TRUE 
 142                                                 Uart
.output
[Uart
.byteCnt
] = 0xf0; 
 145                                                 // Calculate the parity bit for the client... 
 152                                 else if(Uart
.state 
!= STATE_START_OF_COMMUNICATION
) { 
 153                                         // When not part of SOF or EOF, it is an error 
 154                                         Uart
.state 
= STATE_UNSYNCD
; 
 161                         // measurement second half bitperiod 
 162                         // Count the bitslot we are in... (ISO 15693) 
 166                                 if(Uart
.dropPosition
) { 
 167                                         if(Uart
.state 
== STATE_START_OF_COMMUNICATION
) { 
 173                                         // It is an error if we already have seen a drop in current frame 
 174                                         Uart
.state 
= STATE_UNSYNCD
; 
 178                                         Uart
.dropPosition 
= Uart
.nOutOfCnt
; 
 185                         if(Uart
.nOutOfCnt 
== Uart
.OutOfCnt 
&& Uart
.OutOfCnt 
== 4) { 
 188                                 if(Uart
.state 
== STATE_START_OF_COMMUNICATION
) { 
 189                                         if(Uart
.dropPosition 
== 4) { 
 190                                                 Uart
.state 
= STATE_RECEIVING
; 
 193                                         else if(Uart
.dropPosition 
== 3) { 
 194                                                 Uart
.state 
= STATE_RECEIVING
; 
 196                                                 //Uart.output[Uart.byteCnt] = 0xdd; 
 200                                                 Uart
.state 
= STATE_UNSYNCD
; 
 203                                         Uart
.dropPosition 
= 0; 
 208                                         if(!Uart
.dropPosition
) { 
 209                                                 Uart
.state 
= STATE_UNSYNCD
; 
 218                                                 //if(Uart.dropPosition == 1) { Uart.dropPosition = 2; } 
 219                                                 //else if(Uart.dropPosition == 2) { Uart.dropPosition = 1; } 
 221                                                 Uart
.shiftReg 
^= ((Uart
.dropPosition 
& 0x03) << 6); 
 223                                                 Uart
.dropPosition 
= 0; 
 225                                                 if(Uart
.bitCnt 
== 8) { 
 226                                                         Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg 
& 0xff); 
 229                                                         // Calculate the parity bit for the client... 
 230                                                         Uart
.parityBits 
<<= 1; 
 231                                                         //Uart.parityBits ^= OddByteParity[(Uart.shiftReg & 0xff)]; 
 232                                                         Uart
.parityBits 
^= oddparity(Uart
.shiftReg 
& 0xff); 
 240                         else if(Uart
.nOutOfCnt 
== Uart
.OutOfCnt
) { 
 243                                 if(!Uart
.dropPosition
) { 
 244                                         Uart
.state 
= STATE_UNSYNCD
; 
 250                                         Uart
.output
[Uart
.byteCnt
] = (Uart
.dropPosition 
& 0xff); 
 253                                         // Calculate the parity bit for the client... 
 254                                         Uart
.parityBits 
<<= 1; 
 255                                         //Uart.parityBits ^= OddByteParity[(Uart.dropPosition & 0xff)]; 
 256                                         Uart
.parityBits 
^= oddparity((Uart
.dropPosition 
& 0xff)); 
 261                                         Uart
.dropPosition 
= 0; 
 266                                 Uart.output[Uart.byteCnt] = 0xAA; 
 268                                 Uart.output[Uart.byteCnt] = error & 0xFF; 
 270                                 Uart.output[Uart.byteCnt] = 0xAA; 
 272                                 Uart.output[Uart.byteCnt] = (Uart.bitBuffer >> 8) & 0xFF; 
 274                                 Uart.output[Uart.byteCnt] = Uart.bitBuffer & 0xFF; 
 276                                 Uart.output[Uart.byteCnt] = (Uart.syncBit >> 3) & 0xFF; 
 278                                 Uart.output[Uart.byteCnt] = 0xAA; 
 286                 bit 
= Uart
.bitBuffer 
& 0xf0; 
 288                 bit 
^= 0x0F; // drops become 1s ;-) 
 290                         // should have been high or at least (4 * 128) / fc 
 291                         // according to ISO this should be at least (9 * 128 + 20) / fc 
 292                         if(Uart
.highCnt 
== 8) { 
 293                                 // we went low, so this could be start of communication 
 294                                 // it turns out to be safer to choose a less significant 
 295                                 // syncbit... so we check whether the neighbour also represents the drop 
 296                                 Uart
.posCnt 
= 1;   // apparently we are busy with our first half bit period 
 297                                 Uart
.syncBit 
= bit 
& 8; 
 299                                 if(!Uart
.syncBit
)       { Uart
.syncBit 
= bit 
& 4; Uart
.samples 
= 2; } 
 300                                 else if(bit 
& 4)        { Uart
.syncBit 
= bit 
& 4; Uart
.samples 
= 2; bit 
<<= 2; } 
 301                                 if(!Uart
.syncBit
)       { Uart
.syncBit 
= bit 
& 2; Uart
.samples 
= 1; } 
 302                                 else if(bit 
& 2)        { Uart
.syncBit 
= bit 
& 2; Uart
.samples 
= 1; bit 
<<= 1; } 
 303                                 if(!Uart
.syncBit
)       { Uart
.syncBit 
= bit 
& 1; Uart
.samples 
= 0; 
 304                                         if(Uart
.syncBit 
&& (Uart
.bitBuffer 
& 8)) { 
 307                                                 // the first half bit period is expected in next sample 
 312                                 else if(bit 
& 1)        { Uart
.syncBit 
= bit 
& 1; Uart
.samples 
= 0; } 
 315                                 Uart
.state 
= STATE_START_OF_COMMUNICATION
; 
 320                                 Uart
.OutOfCnt 
= 4; // Start at 1/4, could switch to 1/256 
 321                                 Uart
.dropPosition 
= 0; 
 330                         if(Uart
.highCnt 
< 8) { 
 339 //============================================================================= 
 341 //============================================================================= 
 346                 DEMOD_START_OF_COMMUNICATION
, 
 347                 DEMOD_START_OF_COMMUNICATION2
, 
 348                 DEMOD_START_OF_COMMUNICATION3
, 
 352                 DEMOD_END_OF_COMMUNICATION
, 
 353                 DEMOD_END_OF_COMMUNICATION2
, 
 377 static RAMFUNC 
int ManchesterDecoding(int v
) 
 384         Demod
.buffer 
= Demod
.buffer2
; 
 385         Demod
.buffer2 
= Demod
.buffer3
; 
 393         if(Demod
.state
==DEMOD_UNSYNCD
) { 
 394                 Demod
.output
[Demod
.len
] = 0xfa; 
 397                 Demod
.posCount 
= 1;             // This is the first half bit period, so after syncing handle the second part 
 400                         Demod
.syncBit 
= 0x08; 
 407                         Demod
.syncBit 
= 0x04; 
 414                         Demod
.syncBit 
= 0x02; 
 417                 if(bit 
& 0x01 && Demod
.syncBit
) { 
 418                         Demod
.syncBit 
= 0x01; 
 423                         Demod
.state 
= DEMOD_START_OF_COMMUNICATION
; 
 424                         Demod
.sub 
= SUB_FIRST_HALF
; 
 427                         Demod
.parityBits 
= 0; 
 430                                 //if(trigger) LED_A_OFF();  // Not useful in this case... 
 431                                 switch(Demod
.syncBit
) { 
 432                                         case 0x08: Demod
.samples 
= 3; break; 
 433                                         case 0x04: Demod
.samples 
= 2; break; 
 434                                         case 0x02: Demod
.samples 
= 1; break; 
 435                                         case 0x01: Demod
.samples 
= 0; break; 
 437                                 // SOF must be long burst... otherwise stay unsynced!!! 
 438                                 if(!(Demod
.buffer 
& Demod
.syncBit
) || !(Demod
.buffer2 
& Demod
.syncBit
)) { 
 439                                         Demod
.state 
= DEMOD_UNSYNCD
; 
 443                                 // SOF must be long burst... otherwise stay unsynced!!! 
 444                                 if(!(Demod
.buffer2 
& Demod
.syncBit
) || !(Demod
.buffer3 
& Demod
.syncBit
)) { 
 445                                         Demod
.state 
= DEMOD_UNSYNCD
; 
 455                 modulation 
= bit 
& Demod
.syncBit
; 
 456                 modulation 
|= ((bit 
<< 1) ^ ((Demod
.buffer 
& 0x08) >> 3)) & Demod
.syncBit
; 
 460                 if(Demod
.posCount
==0) { 
 463                                 Demod
.sub 
= SUB_FIRST_HALF
; 
 466                                 Demod
.sub 
= SUB_NONE
; 
 471                         /*(modulation && (Demod.sub == SUB_FIRST_HALF)) { 
 472                                 if(Demod.state!=DEMOD_ERROR_WAIT) { 
 473                                         Demod.state = DEMOD_ERROR_WAIT; 
 474                                         Demod.output[Demod.len] = 0xaa; 
 478                         //else if(modulation) { 
 480                                 if(Demod
.sub 
== SUB_FIRST_HALF
) { 
 481                                         Demod
.sub 
= SUB_BOTH
; 
 484                                         Demod
.sub 
= SUB_SECOND_HALF
; 
 487                         else if(Demod
.sub 
== SUB_NONE
) { 
 488                                 if(Demod
.state 
== DEMOD_SOF_COMPLETE
) { 
 489                                         Demod
.output
[Demod
.len
] = 0x0f; 
 491                                         Demod
.parityBits 
<<= 1; 
 492                                         //Demod.parityBits ^= OddByteParity[0x0f]; 
 493                                         Demod
.parityBits 
^= oddparity(0x0f); 
 494                                         Demod
.state 
= DEMOD_UNSYNCD
; 
 499                                         Demod
.state 
= DEMOD_ERROR_WAIT
; 
 502                                 /*if(Demod.state!=DEMOD_ERROR_WAIT) { 
 503                                         Demod.state = DEMOD_ERROR_WAIT; 
 504                                         Demod.output[Demod.len] = 0xaa; 
 509                         switch(Demod
.state
) { 
 510                                 case DEMOD_START_OF_COMMUNICATION
: 
 511                                         if(Demod
.sub 
== SUB_BOTH
) { 
 512                                                 //Demod.state = DEMOD_MANCHESTER_D; 
 513                                                 Demod
.state 
= DEMOD_START_OF_COMMUNICATION2
; 
 515                                                 Demod
.sub 
= SUB_NONE
; 
 518                                                 Demod
.output
[Demod
.len
] = 0xab; 
 519                                                 Demod
.state 
= DEMOD_ERROR_WAIT
; 
 523                                 case DEMOD_START_OF_COMMUNICATION2
: 
 524                                         if(Demod
.sub 
== SUB_SECOND_HALF
) { 
 525                                                 Demod
.state 
= DEMOD_START_OF_COMMUNICATION3
; 
 528                                                 Demod
.output
[Demod
.len
] = 0xab; 
 529                                                 Demod
.state 
= DEMOD_ERROR_WAIT
; 
 533                                 case DEMOD_START_OF_COMMUNICATION3
: 
 534                                         if(Demod
.sub 
== SUB_SECOND_HALF
) { 
 535 //                                              Demod.state = DEMOD_MANCHESTER_D; 
 536                                                 Demod
.state 
= DEMOD_SOF_COMPLETE
; 
 537                                                 //Demod.output[Demod.len] = Demod.syncBit & 0xFF; 
 541                                                 Demod
.output
[Demod
.len
] = 0xab; 
 542                                                 Demod
.state 
= DEMOD_ERROR_WAIT
; 
 546                                 case DEMOD_SOF_COMPLETE
: 
 547                                 case DEMOD_MANCHESTER_D
: 
 548                                 case DEMOD_MANCHESTER_E
: 
 549                                         // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443) 
 550                                         //                          00001111 = 1 (0 in 14443) 
 551                                         if(Demod
.sub 
== SUB_SECOND_HALF
) { // SUB_FIRST_HALF 
 553                                                 Demod
.shiftReg 
= (Demod
.shiftReg 
>> 1) ^ 0x100; 
 554                                                 Demod
.state 
= DEMOD_MANCHESTER_D
; 
 556                                         else if(Demod
.sub 
== SUB_FIRST_HALF
) { // SUB_SECOND_HALF 
 558                                                 Demod
.shiftReg 
>>= 1; 
 559                                                 Demod
.state 
= DEMOD_MANCHESTER_E
; 
 561                                         else if(Demod
.sub 
== SUB_BOTH
) { 
 562                                                 Demod
.state 
= DEMOD_MANCHESTER_F
; 
 565                                                 Demod
.state 
= DEMOD_ERROR_WAIT
; 
 570                                 case DEMOD_MANCHESTER_F
: 
 571                                         // Tag response does not need to be a complete byte! 
 572                                         if(Demod
.len 
> 0 || Demod
.bitCount 
> 0) { 
 573                                                 if(Demod
.bitCount 
> 1) {  // was > 0, do not interpret last closing bit, is part of EOF 
 574                                                         Demod
.shiftReg 
>>= (9 - Demod
.bitCount
); 
 575                                                         Demod
.output
[Demod
.len
] = Demod
.shiftReg 
& 0xff; 
 577                                                         // No parity bit, so just shift a 0 
 578                                                         Demod
.parityBits 
<<= 1; 
 581                                                 Demod
.state 
= DEMOD_UNSYNCD
; 
 585                                                 Demod
.output
[Demod
.len
] = 0xad; 
 586                                                 Demod
.state 
= DEMOD_ERROR_WAIT
; 
 591                                 case DEMOD_ERROR_WAIT
: 
 592                                         Demod
.state 
= DEMOD_UNSYNCD
; 
 596                                         Demod
.output
[Demod
.len
] = 0xdd; 
 597                                         Demod
.state 
= DEMOD_UNSYNCD
; 
 601                         /*if(Demod.bitCount>=9) { 
 602                                 Demod.output[Demod.len] = Demod.shiftReg & 0xff; 
 605                                 Demod.parityBits <<= 1; 
 606                                 Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01); 
 611                         if(Demod
.bitCount
>=8) { 
 612                                 Demod
.shiftReg 
>>= 1; 
 613                                 Demod
.output
[Demod
.len
] = (Demod
.shiftReg 
& 0xff); 
 616                                 // FOR ISO15639 PARITY NOT SEND OTA, JUST CALCULATE IT FOR THE CLIENT 
 617                                 Demod
.parityBits 
<<= 1; 
 618                                 //Demod.parityBits ^= OddByteParity[(Demod.shiftReg & 0xff)]; 
 619                                 Demod
.parityBits 
^= oddparity((Demod
.shiftReg 
& 0xff)); 
 626                                 Demod
.output
[Demod
.len
] = 0xBB; 
 628                                 Demod
.output
[Demod
.len
] = error 
& 0xFF; 
 630                                 Demod
.output
[Demod
.len
] = 0xBB; 
 632                                 Demod
.output
[Demod
.len
] = bit 
& 0xFF; 
 634                                 Demod
.output
[Demod
.len
] = Demod
.buffer 
& 0xFF; 
 637                                 Demod
.output
[Demod
.len
] = Demod
.buffer2 
& 0xFF; 
 639                                 Demod
.output
[Demod
.len
] = Demod
.syncBit 
& 0xFF; 
 641                                 Demod
.output
[Demod
.len
] = 0xBB; 
 648         } // end (state != UNSYNCED) 
 653 //============================================================================= 
 654 // Finally, a `sniffer' for iClass communication 
 655 // Both sides of communication! 
 656 //============================================================================= 
 658 //----------------------------------------------------------------------------- 
 659 // Record the sequence of commands sent by the reader to the tag, with 
 660 // triggering so that we start recording at the point that the tag is moved 
 662 //----------------------------------------------------------------------------- 
 663 void RAMFUNC 
SnoopIClass(void) 
 667     // We won't start recording the frames that we acquire until we trigger; 
 668     // a good trigger condition to get started is probably when we see a 
 669     // response from the tag. 
 670     //int triggered = FALSE; // FALSE to wait first for card 
 672     // The command (reader -> tag) that we're receiving. 
 673         // The length of a received command will in most cases be no more than 18 bytes. 
 674         // So 32 should be enough! 
 675         uint8_t *readerToTagCmd 
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
); 
 676     // The response (tag -> reader) that we're receiving. 
 677         uint8_t *tagToReaderResponse 
= (((uint8_t *)BigBuf
) + RECV_RES_OFFSET
); 
 679     FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
 681     // reset traceLen to 0 
 682     iso14a_set_tracing(TRUE
); 
 683     iso14a_clear_trace(); 
 684     iso14a_set_trigger(FALSE
); 
 686     // The DMA buffer, used to stream samples from the FPGA 
 687     int8_t *dmaBuf 
= ((int8_t *)BigBuf
) + DMA_BUFFER_OFFSET
; 
 693     // Count of samples received so far, so that we can include timing 
 694     // information in the trace buffer. 
 698     // Set up the demodulator for tag -> reader responses. 
 699         Demod
.output 
= tagToReaderResponse
; 
 701     Demod
.state 
= DEMOD_UNSYNCD
; 
 703     // Setup for the DMA. 
 706     lastRxCounter 
= DMA_BUFFER_SIZE
; 
 707     FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
); 
 709     // And the reader -> tag commands 
 710     memset(&Uart
, 0, sizeof(Uart
)); 
 711         Uart
.output 
= readerToTagCmd
; 
 712     Uart
.byteCntMax 
= 32; // was 100 (greg)//////////////////////////////////////////////////////////////////////// 
 713     Uart
.state 
= STATE_UNSYNCD
; 
 715     // And put the FPGA in the appropriate mode 
 716     // Signal field is off with the appropriate LED 
 718     FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A 
| FPGA_HF_ISO14443A_SNIFFER
); 
 719     SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
 721         uint32_t time_0 
= GetCountSspClk(); 
 729     // And now we loop, receiving samples. 
 733         int behindBy 
= (lastRxCounter 
- AT91C_BASE_PDC_SSC
->PDC_RCR
) & 
 735         if(behindBy 
> maxBehindBy
) { 
 736             maxBehindBy 
= behindBy
; 
 738                 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
); 
 742         if(behindBy 
< 1) continue; 
 748         if(upTo 
- dmaBuf 
> DMA_BUFFER_SIZE
) { 
 749             upTo 
-= DMA_BUFFER_SIZE
; 
 750             lastRxCounter 
+= DMA_BUFFER_SIZE
; 
 751             AT91C_BASE_PDC_SSC
->PDC_RNPR 
= (uint32_t) upTo
; 
 752             AT91C_BASE_PDC_SSC
->PDC_RNCR 
= DMA_BUFFER_SIZE
; 
 759                 decbyte 
^= (1 << (3 - div
)); 
 762         // FOR READER SIDE COMMUMICATION... 
 765         decbyter 
^= (smpl 
& 0x30); 
 769         if((div 
+ 1) % 2 == 0) { 
 771                 if(OutOfNDecoding((smpl 
& 0xF0) >> 4)) { 
 772                     rsamples 
= samples 
- Uart
.samples
; 
 775                         //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break; 
 776                         //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break; 
 779                                 LogTrace(Uart
.output
,Uart
.byteCnt
, (GetCountSspClk()-time_0
) << 4, Uart
.parityBits
,TRUE
); 
 780                                 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, TRUE
); 
 784                         /* And ready to receive another command. */ 
 785                     Uart
.state 
= STATE_UNSYNCD
; 
 786                     /* And also reset the demod code, which might have been */ 
 787                     /* false-triggered by the commands from the reader. */ 
 788                     Demod
.state 
= DEMOD_UNSYNCD
; 
 797                 if(ManchesterDecoding(smpl 
& 0x0F)) { 
 798                     rsamples 
= samples 
- Demod
.samples
; 
 803                                 LogTrace(Demod
.output
,Demod
.len
, (GetCountSspClk()-time_0
) << 4 , Demod
.parityBits
,FALSE
); 
 804                                 LogTrace(NULL
, 0, (GetCountSspClk()-time_0
) << 4, 0, FALSE
); 
 808                     // And ready to receive another response. 
 809                     memset(&Demod
, 0, sizeof(Demod
)); 
 810                         Demod
.output 
= tagToReaderResponse
; 
 811                     Demod
.state 
= DEMOD_UNSYNCD
; 
 821             DbpString("cancelled_a"); 
 826     DbpString("COMMAND FINISHED"); 
 828     Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
); 
 829     Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]); 
 832     AT91C_BASE_PDC_SSC
->PDC_PTCR 
= AT91C_PDC_RXTDIS
; 
 833     Dbprintf("%x %x %x", maxBehindBy
, Uart
.state
, Uart
.byteCnt
); 
 834     Dbprintf("%x %x %x", Uart
.byteCntMax
, traceLen
, (int)Uart
.output
[0]); 
 841 void rotateCSN(uint8_t* originalCSN
, uint8_t* rotatedCSN
) { 
 843         for(i 
= 0; i 
< 8; i
++) { 
 844                 rotatedCSN
[i
] = (originalCSN
[i
] >> 3) | (originalCSN
[(i
+1)%8
] << 5); 
 848 //----------------------------------------------------------------------------- 
 849 // Wait for commands from reader 
 850 // Stop when button is pressed 
 851 // Or return TRUE when command is captured 
 852 //----------------------------------------------------------------------------- 
 853 static int GetIClassCommandFromReader(uint8_t *received
, int *len
, int maxLen
) 
 855     // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen 
 856     // only, since we are receiving, not transmitting). 
 857     // Signal field is off with the appropriate LED 
 859     FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A 
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
); 
 861     // Now run a `software UART' on the stream of incoming samples. 
 862     Uart
.output 
= received
; 
 863     Uart
.byteCntMax 
= maxLen
; 
 864     Uart
.state 
= STATE_UNSYNCD
; 
 869         if(BUTTON_PRESS()) return FALSE
; 
 871         if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
 872             AT91C_BASE_SSC
->SSC_THR 
= 0x00; 
 874         if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
 875             uint8_t b 
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
; 
 877                         if(OutOfNDecoding(b 
& 0x0f)) { 
 886 //----------------------------------------------------------------------------- 
 887 // Prepare tag messages 
 888 //----------------------------------------------------------------------------- 
 889 static void CodeIClassTagAnswer(const uint8_t *cmd
, int len
) 
 891         //So far a dummy implementation, not used 
 892         //int lastProxToAirDuration =0; 
 898         ToSend
[++ToSendMax
] = 0x00; 
 899         ToSend
[++ToSendMax
] = 0x00; 
 900         ToSend
[++ToSendMax
] = 0x00; 
 901         ToSend
[++ToSendMax
] = 0xff;//Proxtoair duration starts here 
 902         ToSend
[++ToSendMax
] = 0xff; 
 903         ToSend
[++ToSendMax
] = 0xff; 
 904         ToSend
[++ToSendMax
] = 0x00; 
 905         ToSend
[++ToSendMax
] = 0xff; 
 907         for(i 
= 0; i 
< len
; i
++) { 
 912                 for(j 
= 0; j 
< 8; j
++) { 
 914                                 ToSend
[++ToSendMax
] = 0x00; 
 915                                 ToSend
[++ToSendMax
] = 0xff; 
 917                                 ToSend
[++ToSendMax
] = 0xff; 
 918                                 ToSend
[++ToSendMax
] = 0x00; 
 925         ToSend
[++ToSendMax
] = 0xff; 
 926         ToSend
[++ToSendMax
] = 0x00; 
 927         ToSend
[++ToSendMax
] = 0xff; 
 928         ToSend
[++ToSendMax
] = 0xff; 
 929         ToSend
[++ToSendMax
] = 0xff;      
 930         ToSend
[++ToSendMax
] = 0x00; 
 931         ToSend
[++ToSendMax
] = 0x00; 
 932         ToSend
[++ToSendMax
] = 0x00; 
 934         //lastProxToAirDuration  = 8*ToSendMax - 3*8 - 3*8;//Not counting zeroes in the beginning or end 
 936         // Convert from last byte pos to length 
 941 static void CodeIClassTagSOF() 
 943         //So far a dummy implementation, not used 
 944         //int lastProxToAirDuration =0; 
 948         ToSend
[++ToSendMax
] = 0x00; 
 949         ToSend
[++ToSendMax
] = 0x00; 
 950         ToSend
[++ToSendMax
] = 0x00; 
 951         ToSend
[++ToSendMax
] = 0xff; 
 952         ToSend
[++ToSendMax
] = 0xff; 
 953         ToSend
[++ToSendMax
] = 0xff; 
 954         ToSend
[++ToSendMax
] = 0x00; 
 955         ToSend
[++ToSendMax
] = 0xff; 
 957 //      lastProxToAirDuration  = 8*ToSendMax - 3*8;//Not counting zeroes in the beginning 
 960         // Convert from last byte pos to length 
 963 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
); 
 965  * @brief SimulateIClass simulates an iClass card. 
 966  * @param arg0 type of simulation 
 967  *                      - 0 uses the first 8 bytes in usb data as CSN 
 968  *                      - 2 "dismantling iclass"-attack. This mode iterates through all CSN's specified 
 969  *                      in the usb data. This mode collects MAC from the reader, in order to do an offline 
 970  *                      attack on the keys. For more info, see "dismantling iclass" and proxclone.com. 
 971  *                      - Other : Uses the default CSN (031fec8af7ff12e0) 
 972  * @param arg1 - number of CSN's contained in datain (applicable for mode 2 only) 
 976 void SimulateIClass(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
) 
 978         uint32_t simType 
= arg0
; 
 979         uint32_t numberOfCSNS 
= arg1
; 
 980         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
 982         // Enable and clear the trace 
 983         iso14a_set_tracing(TRUE
); 
 984         iso14a_clear_trace(); 
 986         uint8_t csn_crc
[] = { 0x03, 0x1f, 0xec, 0x8a, 0xf7, 0xff, 0x12, 0xe0, 0x00, 0x00 }; 
 988                 // Use the CSN from commandline 
 989                 memcpy(csn_crc
, datain
, 8); 
 990                 doIClassSimulation(csn_crc
,0,NULL
); 
 991         }else if(simType 
== 1) 
 993                 doIClassSimulation(csn_crc
,0,NULL
); 
 995         else if(simType 
== 2) 
 998                 uint8_t mac_responses
[64] = { 0 }; 
 999                 Dbprintf("Going into attack mode"); 
1000                 // In this mode, a number of csns are within datain. We'll simulate each one, one at a time 
1001                 // in order to collect MAC's from the reader. This can later be used in an offlne-attack 
1002                 // in order to obtain the keys, as in the "dismantling iclass"-paper. 
1004                 for( ; i 
< numberOfCSNS 
&& i
*8+8 < USB_CMD_DATA_SIZE
; i
++) 
1006                         // The usb data is 512 bytes, fitting 65 8-byte CSNs in there. 
1008                         memcpy(csn_crc
, datain
+(i
*8), 8); 
1009                         if(doIClassSimulation(csn_crc
,1,mac_responses
)) 
1011                                 return; // Button pressed 
1014                 cmd_send(CMD_ACK
,CMD_SIMULATE_TAG_ICLASS
,i
,0,mac_responses
,i
*8); 
1018                 // We may want a mode here where we hardcode the csns to use (from proxclone). 
1019                 // That will speed things up a little, but not required just yet. 
1020                 Dbprintf("The mode is not implemented, reserved for future use"); 
1022         Dbprintf("Done..."); 
1026  * @brief Does the actual simulation 
1027  * @param csn - csn to use 
1028  * @param breakAfterMacReceived if true, returns after reader MAC has been received. 
1030 int doIClassSimulation(uint8_t csn
[], int breakAfterMacReceived
, uint8_t *reader_mac_buf
) 
1034         // CSN followed by two CRC bytes 
1035         uint8_t response2
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
1036         uint8_t response3
[] = { 0,0,0,0,0,0,0,0,0,0}; 
1037         memcpy(response3
,csn
,sizeof(response3
)); 
1038         Dbprintf("Simulating CSN %02x%02x%02x%02x%02x%02x%02x%02x",csn
[0],csn
[1],csn
[2],csn
[3],csn
[4],csn
[5],csn
[6],csn
[7]); 
1040         uint8_t response4
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
1042         // Construct anticollision-CSN 
1043         rotateCSN(response3
,response2
); 
1045         // Compute CRC on both CSNs 
1046         ComputeCrc14443(CRC_ICLASS
, response2
, 8, &response2
[8], &response2
[9]); 
1047         ComputeCrc14443(CRC_ICLASS
, response3
, 8, &response3
[8], &response3
[9]); 
1053         // Tag    anticoll. CSN 
1054         // Reader 81 anticoll. CSN 
1059         uint8_t* respdata 
= NULL
; 
1063         // Respond SOF -- takes 8 bytes 
1064         uint8_t *resp1 
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET
); 
1067         // Anticollision CSN (rotated CSN) 
1068         // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit) 
1069         uint8_t *resp2 
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET 
+ 10); 
1073         // 176: Takes 16 bytes for SOF/EOF and 10 * 16 = 160 bytes (2 bytes/bit) 
1074         uint8_t *resp3 
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET 
+ 190); 
1078         // 144: Takes 16 bytes for SOF/EOF and 8 * 16 = 128 bytes (2 bytes/bit) 
1079         uint8_t *resp4 
= (((uint8_t *)BigBuf
) + FREE_BUFFER_OFFSET 
+ 370); 
1083         uint8_t *receivedCmd 
= (((uint8_t *)BigBuf
) + RECV_CMD_OFFSET
); 
1084         memset(receivedCmd
, 0x44, RECV_CMD_SIZE
); 
1087         // Prepare card messages 
1090         // First card answer: SOF 
1092         memcpy(resp1
, ToSend
, ToSendMax
); resp1Len 
= ToSendMax
; 
1094         // Anticollision CSN 
1095         CodeIClassTagAnswer(response2
, sizeof(response2
)); 
1096         memcpy(resp2
, ToSend
, ToSendMax
); resp2Len 
= ToSendMax
; 
1099         CodeIClassTagAnswer(response3
, sizeof(response3
)); 
1100         memcpy(resp3
, ToSend
, ToSendMax
); resp3Len 
= ToSendMax
; 
1103         CodeIClassTagAnswer(response4
, sizeof(response4
)); 
1104         memcpy(resp4
, ToSend
, ToSendMax
); resp4Len 
= ToSendMax
; 
1107         // Start from off (no field generated) 
1108         //FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); 
1110         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A 
| FPGA_HF_ISO14443A_TAGSIM_LISTEN
); 
1113         // We need to listen to the high-frequency, peak-detected path. 
1114         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1117         // To control where we are in the protocol 
1119         uint32_t time_0 
= GetCountSspClk(); 
1120         uint32_t t2r_time 
=0; 
1121         uint32_t r2t_time 
=0; 
1124         bool buttonPressed 
= false; 
1126         /** Hack  for testing 
1127         memcpy(reader_mac_buf,csn,8); 
1135                 // Can be used to get a trigger for an oscilloscope.. 
1138                 if(!GetIClassCommandFromReader(receivedCmd
, &len
, 100)) { 
1139                         buttonPressed 
= true; 
1142                 r2t_time 
= GetCountSspClk(); 
1146                 // Okay, look at the command now. 
1147                 if(receivedCmd
[0] == 0x0a ) { 
1148                         // Reader in anticollission phase 
1149                         resp 
= resp1
; respLen 
= resp1Len
; //order = 1; 
1151                         respsize 
= sizeof(sof
); 
1152                 } else if(receivedCmd
[0] == 0x0c) { 
1153                         // Reader asks for anticollission CSN 
1154                         resp 
= resp2
; respLen 
= resp2Len
; //order = 2; 
1155                         respdata 
= response2
; 
1156                         respsize 
= sizeof(response2
); 
1157                         //DbpString("Reader requests anticollission CSN:"); 
1158                 } else if(receivedCmd
[0] == 0x81) { 
1159                         // Reader selects anticollission CSN. 
1160                         // Tag sends the corresponding real CSN 
1161                         resp 
= resp3
; respLen 
= resp3Len
; //order = 3; 
1162                         respdata 
= response3
; 
1163                         respsize 
= sizeof(response3
); 
1164                         //DbpString("Reader selects anticollission CSN:"); 
1165                 } else if(receivedCmd
[0] == 0x88) { 
1166                         // Read e-purse (88 02) 
1167                         resp 
= resp4
; respLen 
= resp4Len
; //order = 4; 
1168                         respdata 
= response4
; 
1169                         respsize 
= sizeof(response4
); 
1171                 } else if(receivedCmd
[0] == 0x05) { 
1172                         // Reader random and reader MAC!!! 
1174             // We do not know what to answer, so lets keep quiet 
1175                         resp 
= resp1
; respLen 
= 0; //order = 5; 
1178                         if (breakAfterMacReceived
){ 
1180                                 Dbprintf("CSN: %02x %02x %02x %02x %02x %02x %02x %02x",csn
[0],csn
[1],csn
[2],csn
[3],csn
[4],csn
[5],csn
[6],csn
[7]); 
1181                                 Dbprintf("RDR:  (len=%02d): %02x %02x %02x %02x %02x %02x %02x %02x %02x",len
, 
1182                                                  receivedCmd
[0], receivedCmd
[1], receivedCmd
[2], 
1183                                                 receivedCmd
[3], receivedCmd
[4], receivedCmd
[5], 
1184                                                 receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]); 
1185                                 if (reader_mac_buf 
!= NULL
) 
1187                                         memcpy(reader_mac_buf
,receivedCmd
+1,8); 
1191                 } else if(receivedCmd
[0] == 0x00 && len 
== 1) { 
1192                         // Reader ends the session 
1193                         resp 
= resp1
; respLen 
= 0; //order = 0; 
1197                         //#db# Unknown command received from reader (len=5): 26 1 0 f6 a 44 44 44 44 
1198                         // Never seen this command before 
1199                         Dbprintf("Unknown command received from reader (len=%d): %x %x %x %x %x %x %x %x %x", 
1201                         receivedCmd
[0], receivedCmd
[1], receivedCmd
[2], 
1202                         receivedCmd
[3], receivedCmd
[4], receivedCmd
[5], 
1203                         receivedCmd
[6], receivedCmd
[7], receivedCmd
[8]); 
1205                         resp 
= resp1
; respLen 
= 0; //order = 0; 
1210                 if(cmdsRecvd 
>  100) { 
1211                         //DbpString("100 commands later..."); 
1219                         SendIClassAnswer(resp
, respLen
, 21); 
1220                         t2r_time 
= GetCountSspClk(); 
1224                         LogTrace(receivedCmd
,len
, (r2t_time
-time_0
)<< 4, Uart
.parityBits
,TRUE
); 
1225                         LogTrace(NULL
,0, (r2t_time
-time_0
) << 4, 0,TRUE
); 
1227                         if (respdata 
!= NULL
) { 
1228                                 LogTrace(respdata
,respsize
, (t2r_time
-time_0
) << 4,SwapBits(GetParity(respdata
,respsize
),respsize
),FALSE
); 
1229                                 LogTrace(NULL
,0, (t2r_time
-time_0
) << 4,0,FALSE
); 
1234                                 DbpString("Trace full"); 
1239                 memset(receivedCmd
, 0x44, RECV_CMD_SIZE
); 
1242         //Dbprintf("%x", cmdsRecvd); 
1247                 DbpString("Button pressed"); 
1249         return buttonPressed
; 
1252 static int SendIClassAnswer(uint8_t *resp
, int respLen
, int delay
) 
1254         int i 
= 0, d
=0;//, u = 0, d = 0; 
1257         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
|FPGA_HF_SIMULATOR_MODULATE_424K
); 
1259         AT91C_BASE_SSC
->SSC_THR 
= 0x00; 
1261         while(!BUTTON_PRESS()) { 
1262                 if((AT91C_BASE_SSC
->SSC_SR 
& AT91C_SSC_RXRDY
)){ 
1263                         b 
= AT91C_BASE_SSC
->SSC_RHR
; (void) b
; 
1265                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)){ 
1278                         AT91C_BASE_SSC
->SSC_THR 
= b
; 
1281                 if (i 
> respLen 
+4) break; 
1289 //----------------------------------------------------------------------------- 
1290 // Transmit the command (to the tag) that was placed in ToSend[]. 
1291 //----------------------------------------------------------------------------- 
1292 static void TransmitIClassCommand(const uint8_t *cmd
, int len
, int *samples
, int *wait
) 
1295   FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A 
| FPGA_HF_ISO14443A_READER_MOD
); 
1296   AT91C_BASE_SSC
->SSC_THR 
= 0x00; 
1303   for(c 
= 0; c 
< *wait
;) { 
1304     if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
1305       AT91C_BASE_SSC
->SSC_THR 
= 0x00;           // For exact timing! 
1308     if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
1309       volatile uint32_t r 
= AT91C_BASE_SSC
->SSC_RHR
; 
1316   bool firstpart 
= TRUE
; 
1319     if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
1321       // DOUBLE THE SAMPLES! 
1323         sendbyte 
= (cmd
[c
] & 0xf0) | (cmd
[c
] >> 4);  
1326         sendbyte 
= (cmd
[c
] & 0x0f) | (cmd
[c
] << 4); 
1329       if(sendbyte 
== 0xff) { 
1332       AT91C_BASE_SSC
->SSC_THR 
= sendbyte
; 
1333       firstpart 
= !firstpart
; 
1339     if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
1340       volatile uint32_t r 
= AT91C_BASE_SSC
->SSC_RHR
; 
1345   if (samples
) *samples 
= (c 
+ *wait
) << 3; 
1349 //----------------------------------------------------------------------------- 
1350 // Prepare iClass reader command to send to FPGA 
1351 //----------------------------------------------------------------------------- 
1352 void CodeIClassCommand(const uint8_t * cmd
, int len
) 
1359   // Start of Communication: 1 out of 4 
1360   ToSend
[++ToSendMax
] = 0xf0; 
1361   ToSend
[++ToSendMax
] = 0x00; 
1362   ToSend
[++ToSendMax
] = 0x0f; 
1363   ToSend
[++ToSendMax
] = 0x00; 
1365   // Modulate the bytes  
1366   for (i 
= 0; i 
< len
; i
++) { 
1368     for(j 
= 0; j 
< 4; j
++) { 
1369       for(k 
= 0; k 
< 4; k
++) { 
1371                                 ToSend
[++ToSendMax
] = 0x0f; 
1374                                 ToSend
[++ToSendMax
] = 0x00; 
1381   // End of Communication 
1382   ToSend
[++ToSendMax
] = 0x00; 
1383   ToSend
[++ToSendMax
] = 0x00; 
1384   ToSend
[++ToSendMax
] = 0xf0; 
1385   ToSend
[++ToSendMax
] = 0x00; 
1387   // Convert from last character reference to length 
1391 void ReaderTransmitIClass(uint8_t* frame
, int len
) 
1397   // This is tied to other size changes 
1398   CodeIClassCommand(frame
,len
); 
1401   TransmitIClassCommand(ToSend
, ToSendMax
, &samples
, &wait
); 
1405   // Store reader command in buffer 
1406   if (tracing
) LogTrace(frame
,len
,rsamples
,par
,TRUE
); 
1409 //----------------------------------------------------------------------------- 
1410 // Wait a certain time for tag response 
1411 //  If a response is captured return TRUE 
1412 //  If it takes too long return FALSE 
1413 //----------------------------------------------------------------------------- 
1414 static int GetIClassAnswer(uint8_t *receivedResponse
, int maxLen
, int *samples
, int *elapsed
) //uint8_t *buffer 
1416         // buffer needs to be 512 bytes 
1419         // Set FPGA mode to "reader listen mode", no modulation (listen 
1420         // only, since we are receiving, not transmitting). 
1421         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A 
| FPGA_HF_ISO14443A_READER_LISTEN
); 
1423         // Now get the answer from the card 
1424         Demod
.output 
= receivedResponse
; 
1426         Demod
.state 
= DEMOD_UNSYNCD
; 
1429         if (elapsed
) *elapsed 
= 0; 
1437             if(BUTTON_PRESS()) return FALSE
; 
1439                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
)) { 
1440                         AT91C_BASE_SSC
->SSC_THR 
= 0x00;  // To make use of exact timing of next command from reader!! 
1441                         if (elapsed
) (*elapsed
)++; 
1443                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
1444                         if(c 
< timeout
) { c
++; } else { return FALSE
; } 
1445                         b 
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
; 
1449                         if(ManchesterDecoding(b 
& 0x0f)) { 
1457 int ReaderReceiveIClass(uint8_t* receivedAnswer
) 
1460   if (!GetIClassAnswer(receivedAnswer
,160,&samples
,0)) return FALSE
; 
1461   rsamples 
+= samples
; 
1462   if (tracing
) LogTrace(receivedAnswer
,Demod
.len
,rsamples
,Demod
.parityBits
,FALSE
); 
1463   if(samples 
== 0) return FALSE
; 
1467 void setupIclassReader() 
1469     FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1470     // Reset trace buffer 
1471     iso14a_set_tracing(TRUE
); 
1472     iso14a_clear_trace(); 
1476     // Start from off (no field generated) 
1477     // Signal field is off with the appropriate LED 
1479     FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1482     SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1484     // Now give it time to spin up. 
1485     // Signal field is on with the appropriate LED 
1486     FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A 
| FPGA_HF_ISO14443A_READER_MOD
); 
1492 // Reader iClass Anticollission 
1493 void ReaderIClass(uint8_t arg0
) { 
1494         uint8_t act_all
[]     = { 0x0a }; 
1495         uint8_t identify
[]    = { 0x0c }; 
1496         uint8_t select
[]      = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
1497     uint8_t readcheck_cc
[]= { 0x88, 0x02 }; 
1499     uint8_t card_data
[24]={0}; 
1500     uint8_t last_csn
[8]={0}; 
1502         uint8_t* resp 
= (((uint8_t *)BigBuf
) + 3560);   // was 3560 - tied to other size changes 
1505     bool abort_after_read 
= arg0 
& FLAG_ICLASS_READER_ONLY_ONCE
; 
1507     setupIclassReader(); 
1509     size_t datasize 
= 0; 
1510     while(!BUTTON_PRESS()) 
1515         ReaderTransmitIClass(act_all
, 1); 
1517         if(ReaderReceiveIClass(resp
)) { 
1519             ReaderTransmitIClass(identify
, 1); 
1521             if(ReaderReceiveIClass(resp
) == 10) { 
1522                 //Copy the Anti-collision CSN to our select-packet 
1523                 memcpy(&select
[1],resp
,8); 
1524                 //Dbprintf("Anti-collision CSN: %02x %02x %02x %02x %02x %02x %02x %02x",resp[0], resp[1], resp[2], 
1525                 //        resp[3], resp[4], resp[5], 
1526                 //        resp[6], resp[7]); 
1528                 ReaderTransmitIClass(select
, sizeof(select
)); 
1530                 if(ReaderReceiveIClass(resp
) == 10) { 
1531                     //Save CSN in response data 
1532                     memcpy(card_data
,resp
,8); 
1534                     //Flag that we got to at least stage 1, read CSN 
1538                     //Dbprintf("Readcheck on Sector 2"); 
1539                     ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
)); 
1540                     if(ReaderReceiveIClass(resp
) == 8) { 
1541                         //Save CC (e-purse) in response data 
1542                         memcpy(card_data
+8,resp
,8); 
1549                     //Send back to client, but don't bother if we already sent this 
1550                     if(memcmp(last_csn
, card_data
, 8) != 0) 
1551                         cmd_send(CMD_ACK
,read_status
,0,0,card_data
,datasize
); 
1553                     //Save that we already sent this.... 
1554                     if(read_status 
==  2) 
1555                         memcpy(last_csn
, card_data
, 8); 
1559                     if(abort_after_read
) break; 
1564         if(traceLen 
> TRACE_SIZE
) { 
1565             DbpString("Trace full"); 
1572 void ReaderIClass_Replay(uint8_t arg0
, uint8_t *MAC
) { 
1573         uint8_t act_all
[]     = { 0x0a }; 
1574         uint8_t identify
[]    = { 0x0c }; 
1575         uint8_t select
[]      = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
1576         uint8_t readcheck_cc
[]= { 0x88, 0x02 }; 
1577         uint8_t check
[]       = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
1578         uint8_t read
[]        = { 0x0c, 0x00, 0x00, 0x00 }; 
1582         bool read_success
=false; 
1585         static struct memory_t
{ 
1593         uint8_t* resp 
= (((uint8_t *)BigBuf
) + 3560);   // was 3560 - tied to other size changes 
1595     setupIclassReader(); 
1598         for(int i
=0;i
<1;i
++) { 
1600                 if(traceLen 
> TRACE_SIZE
) { 
1601                         DbpString("Trace full"); 
1605                 if (BUTTON_PRESS()) break; 
1608                 ReaderTransmitIClass(act_all
, 1); 
1610                 if(ReaderReceiveIClass(resp
)) { 
1611                         ReaderTransmitIClass(identify
, 1); 
1612                         if(ReaderReceiveIClass(resp
) == 10) { 
1614                                 memcpy(&select
[1],resp
,8); 
1615                                 ReaderTransmitIClass(select
, sizeof(select
)); 
1617                                 if(ReaderReceiveIClass(resp
) == 10) { 
1618                                         Dbprintf("     Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x", 
1619                                         resp
[0], resp
[1], resp
[2], 
1620                                         resp
[3], resp
[4], resp
[5], 
1624                                 Dbprintf("Readcheck on Sector 2"); 
1625                                 ReaderTransmitIClass(readcheck_cc
, sizeof(readcheck_cc
)); 
1626                                 if(ReaderReceiveIClass(resp
) == 8) { 
1627                                    Dbprintf("     CC: %02x %02x %02x %02x %02x %02x %02x %02x", 
1628                                         resp
[0], resp
[1], resp
[2], 
1629                                         resp
[3], resp
[4], resp
[5], 
1632                                 Dbprintf("Authenticate"); 
1633                                 //for now replay captured auth (as cc not updated) 
1634                                 memcpy(check
+5,MAC
,4); 
1635                 //Dbprintf("     AA: %02x %02x %02x %02x", 
1636                 //      check[5], check[6], check[7],check[8]); 
1637                                 ReaderTransmitIClass(check
, sizeof(check
)); 
1638                                 if(ReaderReceiveIClass(resp
) == 4) { 
1639                                    Dbprintf("     AR: %02x %02x %02x %02x", 
1640                                         resp
[0], resp
[1], resp
[2],resp
[3]); 
1642                                   Dbprintf("Error: Authentication Fail!"); 
1645                                 Dbprintf("Dump Contents"); 
1646                                 //first get configuration block 
1649                                 uint8_t *blockno
=&read
[1]; 
1650                                 crc 
= iclass_crc16((char *)blockno
,1); 
1652                                 read
[3] = crc 
& 0xff; 
1653                                 while(!read_success
){ 
1654                                       ReaderTransmitIClass(read
, sizeof(read
)); 
1655                                       if(ReaderReceiveIClass(resp
) == 10) { 
1658                                          memory
.k16
= (mem 
& 0x80); 
1659                                          memory
.book
= (mem 
& 0x20); 
1660                                          memory
.k2
= (mem 
& 0x8); 
1661                                          memory
.lockauth
= (mem 
& 0x2); 
1662                                          memory
.keyaccess
= (mem 
& 0x1); 
1669                                 //then loop around remaining blocks 
1670                                 for(uint8_t j
=0; j
<cardsize
; j
++){ 
1672                                     uint8_t *blockno
=&j
; 
1675                                     crc 
= iclass_crc16((char *)blockno
,1); 
1677                                     read
[3] = crc 
& 0xff; 
1678                                     while(!read_success
){ 
1679                                       ReaderTransmitIClass(read
, sizeof(read
)); 
1680                                       if(ReaderReceiveIClass(resp
) == 10) { 
1682                                          Dbprintf("     %02x: %02x %02x %02x %02x %02x %02x %02x %02x", 
1683                                           j
, resp
[0], resp
[1], resp
[2], 
1684                                           resp
[3], resp
[4], resp
[5], 
1697 //2. Create Read method (cut-down from above) based off responses from 1.  
1698 //   Since we have the MAC could continue to use replay function. 
1699 //3. Create Write method 
1701 void IClass_iso14443A_write(uint8_t arg0, uint8_t blockNo, uint8_t *data, uint8_t *MAC) { 
1702         uint8_t act_all[]     = { 0x0a }; 
1703         uint8_t identify[]    = { 0x0c }; 
1704         uint8_t select[]      = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
1705         uint8_t readcheck_cc[]= { 0x88, 0x02 }; 
1706         uint8_t check[]       = { 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
1707         uint8_t read[]        = { 0x0c, 0x00, 0x00, 0x00 }; 
1708         uint8_t write[]       = { 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 
1712         uint8_t* resp = (((uint8_t *)BigBuf) + 3560);   // was 3560 - tied to other size changes 
1714         // Reset trace buffer 
1715         memset(trace, 0x44, RECV_CMD_OFFSET); 
1720         // Start from off (no field generated) 
1721         // Signal field is off with the appropriate LED 
1723         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); 
1726         SetAdcMuxFor(GPIO_MUXSEL_HIPKD); 
1728         // Now give it time to spin up. 
1729         // Signal field is on with the appropriate LED 
1730         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD); 
1735         for(int i=0;i<1;i++) { 
1737                 if(traceLen > TRACE_SIZE) { 
1738                         DbpString("Trace full"); 
1742                 if (BUTTON_PRESS()) break; 
1745                 ReaderTransmitIClass(act_all, 1); 
1747                 if(ReaderReceiveIClass(resp)) { 
1748                         ReaderTransmitIClass(identify, 1); 
1749                         if(ReaderReceiveIClass(resp) == 10) { 
1751                                 memcpy(&select[1],resp,8); 
1752                                 ReaderTransmitIClass(select, sizeof(select)); 
1754                                 if(ReaderReceiveIClass(resp) == 10) { 
1755                                         Dbprintf("     Selected CSN: %02x %02x %02x %02x %02x %02x %02x %02x", 
1756                                         resp[0], resp[1], resp[2], 
1757                                         resp[3], resp[4], resp[5], 
1761                                 Dbprintf("Readcheck on Sector 2"); 
1762                                 ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc)); 
1763                                 if(ReaderReceiveIClass(resp) == 8) { 
1764                                    Dbprintf("     CC: %02x %02x %02x %02x %02x %02x %02x %02x", 
1765                                         resp[0], resp[1], resp[2], 
1766                                         resp[3], resp[4], resp[5], 
1769                                 Dbprintf("Authenticate"); 
1770                                 //for now replay captured auth (as cc not updated) 
1771                                 memcpy(check+5,MAC,4); 
1772                                 Dbprintf("     AA: %02x %02x %02x %02x", 
1773                                         check[5], check[6], check[7],check[8]); 
1774                                 ReaderTransmitIClass(check, sizeof(check)); 
1775                                 if(ReaderReceiveIClass(resp) == 4) { 
1776                                    Dbprintf("     AR: %02x %02x %02x %02x", 
1777                                         resp[0], resp[1], resp[2],resp[3]); 
1779                                   Dbprintf("Error: Authentication Fail!"); 
1782                                 Dbprintf("Write Block"); 
1784                                 //read configuration for max block number 
1787                                 uint8_t *blockno=&read[1]; 
1788                                 crc = iclass_crc16((char *)blockno,1); 
1790                                 read[3] = crc & 0xff; 
1791                                 while(!read_success){ 
1792                                       ReaderTransmitIClass(read, sizeof(read)); 
1793                                       if(ReaderReceiveIClass(resp) == 10) { 
1796                                          memory.k16= (mem & 0x80); 
1797                                          memory.book= (mem & 0x20); 
1798                                          memory.k2= (mem & 0x8); 
1799                                          memory.lockauth= (mem & 0x2); 
1800                                          memory.keyaccess= (mem & 0x1); 
1809                                 memcpy(write+1,blockNo,1); 
1810                                 memcpy(write+2,data,8); 
1811                                 memcpy(write+10,mac,4); 
1812                                 while(!send_success){ 
1813                                   ReaderTransmitIClass(write, sizeof(write)); 
1814                                   if(ReaderReceiveIClass(resp) == 10) {