1 //----------------------------------------------------------------------------- 
   2 // Merlok - June 2011, 2012 
   3 // Gerhard de Koning Gans - May 2008 
   4 // Hagen Fritsch - June 2010 
   5 // Midnitesnake - Dec 2013 
   6 // Andy Davies  - Apr 2014 
   9 // This code is licensed to you under the terms of the GNU GPL, version 2 or, 
  10 // at your option, any later version. See the LICENSE.txt file for the text of 
  12 //----------------------------------------------------------------------------- 
  13 // Routines to support ISO 14443 type A. 
  14 //----------------------------------------------------------------------------- 
  16 #include "mifarecmd.h" 
  23 // the block number for the ISO14443-4 PCB 
  24 uint8_t pcb_blocknum 
= 0; 
  25 // Deselect card by sending a s-block. the crc is precalced for speed 
  26 static  uint8_t deselect_cmd
[] = {0xc2,0xe0,0xb4}; 
  29 //----------------------------------------------------------------------------- 
  30 // Select, Authenticate, Read a MIFARE tag.  
  32 //----------------------------------------------------------------------------- 
  33 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
  36         uint8_t blockNo 
= arg0
; 
  37         uint8_t keyType 
= arg1
; 
  39         ui64Key 
= bytes_to_num(datain
, 6); 
  43         byte_t dataoutbuf
[16]; 
  46         struct Crypto1State mpcs 
= {0, 0}; 
  47         struct Crypto1State 
*pcs
; 
  52         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
  59                 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
  60                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
  64                 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
  65                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
  69                 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) { 
  70                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Read block error"); 
  74                 if(mifare_classic_halt(pcs
, cuid
)) { 
  75                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
  83         //  ----------------------------- crypto1 destroy 
  86         if (MF_DBGLEVEL 
>= 2)   DbpString("READ BLOCK FINISHED"); 
  89         cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16); 
  92         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
  96 void MifareUC_Auth1(uint8_t arg0
, uint8_t *datain
){ 
  98         byte_t dataoutbuf
[16] = {0x00}; 
  99         uint8_t uid
[10] = {0x00}; 
 100         uint32_t cuid 
= 0x00; 
 102         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
 105         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 107         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 108                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card"); 
 113         if(mifare_ultra_auth1(dataoutbuf
)){ 
 114                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail."); 
 119         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED"); 
 121     cmd_send(CMD_ACK
,1,cuid
,0,dataoutbuf
,11); 
 124 void MifareUC_Auth2(uint32_t arg0
, uint8_t *datain
){ 
 126         uint8_t key
[16] = {0x00}; 
 127         byte_t dataoutbuf
[16] = {0x00}; 
 129         memcpy(key
, datain
, 16); 
 131         LED_A_ON();     LED_B_OFF(); LED_C_OFF(); 
 133         if(mifare_ultra_auth2(key
, dataoutbuf
)){ 
 134             if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Authentication part2: Fail..."); 
 139         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED"); 
 141         cmd_send(CMD_ACK
,1,0,0,dataoutbuf
,11); 
 142         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 146 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
) 
 148         uint8_t blockNo 
= arg0
; 
 149         byte_t dataout
[16] = {0x00}; 
 150         uint8_t uid
[10] = {0x00}; 
 151         uint8_t key
[16] = {0x00}; 
 152         bool usePwd 
= (arg1 
== 1); 
 154         LED_A_ON();     LED_B_OFF(); LED_C_OFF(); 
 157         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 159         int len 
= iso14443a_select_card(uid
, NULL
, NULL
); 
 161                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
); 
 166          // authenticate here. 
 169                 memcpy(key
, datain
, 16); 
 171                 // Dbprintf("KEY: %02x %02x %02x %02x %02x %02x %02x %02x", key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7] ); 
 172                 // Dbprintf("KEY: %02x %02x %02x %02x %02x %02x %02x %02x", key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15] ); 
 174                 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1 }; 
 175                 uint8_t random_b
[8] = {0x00}; 
 176                 uint8_t enc_random_b
[8] = {0x00}; 
 177                 uint8_t rnd_ab
[16] = {0x00}; 
 178                 uint8_t IV
[8] = {0x00}; 
 181                 uint8_t receivedAnswer
[MAX_FRAME_SIZE
]; 
 182                 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
]; 
 184                 len 
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar 
,NULL
); 
 186                         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 192                 memcpy(enc_random_b
,receivedAnswer
+1,8); 
 195                 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV 
); 
 200                 memcpy(rnd_ab  
,random_a
,8); 
 201                 memcpy(rnd_ab
+8,random_b
,8); 
 203                 if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) { 
 204                         Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x", 
 205                                 enc_random_b
[0],enc_random_b
[1],enc_random_b
[2],enc_random_b
[3], 
 206                                 enc_random_b
[4],enc_random_b
[5],enc_random_b
[6],enc_random_b
[7]); 
 208                         Dbprintf("    B: %02x %02x %02x %02x %02x %02x %02x %02x", 
 209                                 random_b
[0],random_b
[1],random_b
[2],random_b
[3], 
 210                                 random_b
[4],random_b
[5],random_b
[6],random_b
[7]); 
 212                         Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x", 
 213                                         rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3], 
 214                                         rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]); 
 216                         Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x", 
 217                                         rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11], 
 218                                         rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] ); 
 221                 // encrypt    out, in, length, key, iv 
 222                 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
); 
 225                 len 
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, rnd_ab
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 227                         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 232                 uint8_t enc_resp
[8] = { 0 }; 
 233                 uint8_t resp_random_a
[8] = { 0 }; 
 234                 memcpy(enc_resp
, receivedAnswer
+1, 8); 
 236                 // decrypt    out, in, length, key, iv  
 237                 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
); 
 238                 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) 
 239                         Dbprintf("failed authentication"); 
 241                 if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) { 
 242                         Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",  
 243                                         rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3], 
 244                                         rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]); 
 246                         Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x", 
 247                                         rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11], 
 248                                         rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]); 
 250                         Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x", 
 251                                         random_a
[0],random_a
[1],random_a
[2],random_a
[3], 
 252                                         random_a
[4],random_a
[5],random_a
[6],random_a
[7]); 
 254                         Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x", 
 255                                         resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3], 
 256                                         resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]); 
 260         if( mifare_ultra_readblock(blockNo
, dataout
) ) { 
 261                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Read block error"); 
 266         if( mifare_ultra_halt() ) { 
 267                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Halt error"); 
 272     cmd_send(CMD_ACK
,1,0,0,dataout
,16); 
 273         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 277 //----------------------------------------------------------------------------- 
 278 // Select, Authenticate, Read a MIFARE tag.  
 279 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes) 
 280 //----------------------------------------------------------------------------- 
 281 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 284         uint8_t sectorNo 
= arg0
; 
 285         uint8_t keyType 
= arg1
; 
 286         uint64_t ui64Key 
= 0; 
 287         ui64Key 
= bytes_to_num(datain
, 6); 
 291         byte_t dataoutbuf
[16 * 16]; 
 294         struct Crypto1State mpcs 
= {0, 0}; 
 295         struct Crypto1State 
*pcs
; 
 301         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 308         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 310                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 314         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) { 
 316                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
 319         for (uint8_t blockNo 
= 0; isOK 
&& blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 320                 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf 
+ 16 * blockNo
)) { 
 322                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
); 
 327         if(mifare_classic_halt(pcs
, cuid
)) { 
 328                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 331         //  ----------------------------- crypto1 destroy 
 332         crypto1_destroy(pcs
); 
 334         if (MF_DBGLEVEL 
>= 2) DbpString("READ SECTOR FINISHED"); 
 337         cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
)); 
 341         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 345 void MifareUReadCard(uint8_t arg0
, int arg1
, uint8_t *datain
) 
 348         uint8_t sectorNo 
= arg0
; 
 351         byte_t dataout
[176] = {0x00};; 
 356         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 358         int len 
= iso14443a_select_card(NULL
, NULL
, NULL
); 
 360                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
); 
 365         for (int i 
= 0; i 
< Pages
; i
++){ 
 367                 len 
= mifare_ultra_readblock(sectorNo 
* 4 + i
, dataout 
+ 4 * i
); 
 370                         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
); 
 378         len 
= mifare_ultra_halt(); 
 380                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Halt error"); 
 385         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) Dbprintf("Pages read %d", countpages
); 
 389         cmd_send(CMD_ACK
, 1, 0, 0, dataout
, len
);        
 390         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 394 //----------------------------------------------------------------------------- 
 395 // Select, Authenticate, Write a MIFARE tag.  
 397 //----------------------------------------------------------------------------- 
 398 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 401         uint8_t blockNo 
= arg0
; 
 402         uint8_t keyType 
= arg1
; 
 403         uint64_t ui64Key 
= 0; 
 404         byte_t blockdata
[16]; 
 406         ui64Key 
= bytes_to_num(datain
, 6); 
 407         memcpy(blockdata
, datain 
+ 10, 16); 
 413         struct Crypto1State mpcs 
= {0, 0}; 
 414         struct Crypto1State 
*pcs
; 
 420         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 427                         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 428                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 432                 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
 433                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
 437                 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) { 
 438                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Write block error"); 
 442                 if(mifare_classic_halt(pcs
, cuid
)) { 
 443                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 451         //  ----------------------------- crypto1 destroy 
 452         crypto1_destroy(pcs
); 
 454         if (MF_DBGLEVEL 
>= 2)   DbpString("WRITE BLOCK FINISHED"); 
 457         cmd_send(CMD_ACK
,isOK
,0,0,0,0); 
 462         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 466 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
) 
 468         uint8_t blockNo 
= arg0
; 
 469         byte_t blockdata
[16] = {0x00}; 
 471         memcpy(blockdata
, datain
, 16); 
 473         uint8_t uid
[10] = {0x00}; 
 475         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
 478         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 480         if(!iso14443a_select_card(uid
, NULL
, NULL
)) { 
 481                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 486         if(mifare_ultra_writeblock(blockNo
, blockdata
)) { 
 487                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Write block error"); 
 491         if(mifare_ultra_halt()) { 
 492                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 497         if (MF_DBGLEVEL 
>= 2)   DbpString("WRITE BLOCK FINISHED"); 
 499         cmd_send(CMD_ACK
,1,0,0,0,0); 
 500         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 504 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t *datain
) 
 506         uint8_t blockNo 
= arg0
; 
 507         byte_t blockdata
[4] = {0x00}; 
 509         memcpy(blockdata
, datain
,4); 
 511         uint8_t uid
[10] = {0x00}; 
 513         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
 515         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 517         if(!iso14443a_select_card(uid
, NULL
, NULL
)) { 
 518                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Can't select card"); 
 523         if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) { 
 524                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 529         if(mifare_ultra_halt()) { 
 530                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Halt error"); 
 535         if (MF_DBGLEVEL 
>= 2) DbpString("WRITE BLOCK FINISHED"); 
 537         cmd_send(CMD_ACK
,1,0,0,0,0); 
 538         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 542 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){ 
 544         uint8_t pwd
[16] = {0x00}; 
 545         byte_t blockdata
[4] = {0x00}; 
 547         memcpy(pwd
, datain
, 16); 
 549         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
 551         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 553         if(!iso14443a_select_card(NULL
, NULL
, NULL
)) { 
 554                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Can't select card"); 
 559         blockdata
[0] = pwd
[7]; 
 560         blockdata
[1] = pwd
[6]; 
 561         blockdata
[2] = pwd
[5]; 
 562         blockdata
[3] = pwd
[4]; 
 563         if(mifare_ultra_special_writeblock( 44, blockdata
)) { 
 564                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 569         blockdata
[0] = pwd
[3]; 
 570         blockdata
[1] = pwd
[2]; 
 571         blockdata
[2] = pwd
[1]; 
 572         blockdata
[3] = pwd
[0]; 
 573         if(mifare_ultra_special_writeblock( 45, blockdata
)) { 
 574                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 579         blockdata
[0] = pwd
[15]; 
 580         blockdata
[1] = pwd
[14]; 
 581         blockdata
[2] = pwd
[13]; 
 582         blockdata
[3] = pwd
[12]; 
 583         if(mifare_ultra_special_writeblock( 46, blockdata
)) { 
 584                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 589         blockdata
[0] = pwd
[11]; 
 590         blockdata
[1] = pwd
[10]; 
 591         blockdata
[2] = pwd
[9]; 
 592         blockdata
[3] = pwd
[8]; 
 593         if(mifare_ultra_special_writeblock( 47, blockdata
)) { 
 594                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 599         if(mifare_ultra_halt()) { 
 600                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Halt error"); 
 605         cmd_send(CMD_ACK
,1,0,0,0,0); 
 606         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 610 // Return 1 if the nonce is invalid else return 0 
 611 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) { 
 612         return ((oddparity((Nt 
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc 
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
 
 613         (oddparity((Nt 
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc 
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
 
 614         (oddparity((Nt 
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc 
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0; 
 618 //----------------------------------------------------------------------------- 
 619 // MIFARE nested authentication.  
 621 //----------------------------------------------------------------------------- 
 622 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
) 
 625         uint8_t blockNo 
= arg0 
& 0xff; 
 626         uint8_t keyType 
= (arg0 
>> 8) & 0xff; 
 627         uint8_t targetBlockNo 
= arg1 
& 0xff; 
 628         uint8_t targetKeyType 
= (arg1 
>> 8) & 0xff; 
 629         uint64_t ui64Key 
= 0; 
 631         ui64Key 
= bytes_to_num(datain
, 6); 
 634         uint16_t rtr
, i
, j
, len
; 
 636         static uint16_t dmin
, dmax
; 
 638         uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
; 
 640         uint32_t target_nt
[2], target_ks
[2]; 
 642         uint8_t par_array
[4]; 
 644         struct Crypto1State mpcs 
= {0, 0}; 
 645         struct Crypto1State 
*pcs
; 
 647         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
 649         uint32_t auth1_time
, auth2_time
; 
 650         static uint16_t delta_time
; 
 652         // free eventually allocated BigBuf memory 
 658         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 664         // statistics on nonce distance 
 665         if (calibrate
) {        // for first call only. Otherwise reuse previous calibration 
 673                 for (rtr 
= 0; rtr 
< 17; rtr
++) { 
 675                         // prepare next select. No need to power down the card. 
 676                         if(mifare_classic_halt(pcs
, cuid
)) { 
 677                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Halt error"); 
 682                         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 683                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Can't select card"); 
 689                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) { 
 690                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth1 error"); 
 696                                 auth2_time 
= auth1_time 
+ delta_time
; 
 700                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) { 
 701                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth2 error"); 
 706                         nttmp 
= prng_successor(nt1
, 100);                               //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160 
 707                         for (i 
= 101; i 
< 1200; i
++) { 
 708                                 nttmp 
= prng_successor(nttmp
, 1); 
 709                                 if (nttmp 
== nt2
) break; 
 719                                         delta_time 
= auth2_time 
- auth1_time 
+ 32;  // allow some slack for proper timing 
 721                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
); 
 725                 if (rtr 
<= 1)   return; 
 727                 davg 
= (davg 
+ (rtr 
- 1)/2) / (rtr 
- 1); 
 729                 if (MF_DBGLEVEL 
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
); 
 737 //  -------------------------------------------------------------------------------------------------    
 741         //  get crypted nonces for target sector 
 742         for(i
=0; i 
< 2; i
++) { // look for exactly two different nonces 
 745                 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce 
 747                         // prepare next select. No need to power down the card. 
 748                         if(mifare_classic_halt(pcs
, cuid
)) { 
 749                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Halt error"); 
 753                         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 754                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Can't select card"); 
 759                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) { 
 760                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth1 error"); 
 764                         // nested authentication 
 765                         auth2_time 
= auth1_time 
+ delta_time
; 
 766                         len 
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType 
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
); 
 768                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth2 error len=%d", len
); 
 772                         nt2 
= bytes_to_num(receivedAnswer
, 4);           
 773                         if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]); 
 775                         // Parity validity check 
 776                         for (j 
= 0; j 
< 4; j
++) { 
 777                                 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01)); 
 781                         nttest 
= prng_successor(nt1
, dmin 
- 1); 
 782                         for (j 
= dmin
; j 
< dmax 
+ 1; j
++) { 
 783                                 nttest 
= prng_successor(nttest
, 1); 
 786                                 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){ 
 787                                         if (ncount 
> 0) {               // we are only interested in disambiguous nonces, try again 
 788                                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
); 
 792                                         target_nt
[i
] = nttest
; 
 795                                         if (i 
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces 
 797                                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
); 
 800                                         if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
); 
 803                         if (target_nt
[i
] == 0 && j 
== dmax
+1 && MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1); 
 809         //  ----------------------------- crypto1 destroy 
 810         crypto1_destroy(pcs
); 
 812         byte_t buf
[4 + 4 * 4]; 
 813         memcpy(buf
, &cuid
, 4); 
 814         memcpy(buf
+4, &target_nt
[0], 4); 
 815         memcpy(buf
+8, &target_ks
[0], 4); 
 816         memcpy(buf
+12, &target_nt
[1], 4); 
 817         memcpy(buf
+16, &target_ks
[1], 4); 
 820         cmd_send(CMD_ACK
, 0, 2, targetBlockNo 
+ (targetKeyType 
* 0x100), buf
, sizeof(buf
)); 
 823         if (MF_DBGLEVEL 
>= 3)   DbpString("NESTED FINISHED"); 
 825         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 830 //----------------------------------------------------------------------------- 
 831 // MIFARE check keys. key count up to 85.  
 833 //----------------------------------------------------------------------------- 
 834 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 837         uint8_t blockNo 
= arg0
; 
 838         uint8_t keyType 
= arg1
; 
 839         uint8_t keyCount 
= arg2
; 
 840         uint64_t ui64Key 
= 0; 
 847         struct Crypto1State mpcs 
= {0, 0}; 
 848         struct Crypto1State 
*pcs
; 
 852         int OLD_MF_DBGLEVEL 
= MF_DBGLEVEL
;       
 853         MF_DBGLEVEL 
= MF_DBG_NONE
; 
 859         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 865         for (i 
= 0; i 
< keyCount
; i
++) { 
 866                 if(mifare_classic_halt(pcs
, cuid
)) { 
 867                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("ChkKeys: Halt error"); 
 870                 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 871                         if (OLD_MF_DBGLEVEL 
>= 1)       Dbprintf("ChkKeys: Can't select card"); 
 875                 ui64Key 
= bytes_to_num(datain 
+ i 
* 6, 6); 
 876                 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
 884         //  ----------------------------- crypto1 destroy 
 885         crypto1_destroy(pcs
); 
 888     cmd_send(CMD_ACK
,isOK
,0,0,datain 
+ i 
* 6,6); 
 891         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 894         // restore debug level 
 895         MF_DBGLEVEL 
= OLD_MF_DBGLEVEL
;   
 898 //----------------------------------------------------------------------------- 
 899 // MIFARE commands set debug level 
 901 //----------------------------------------------------------------------------- 
 902 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 904         Dbprintf("Debug level: %d", MF_DBGLEVEL
); 
 907 //----------------------------------------------------------------------------- 
 908 // Work with emulator memory 
 910 //----------------------------------------------------------------------------- 
 911 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 915 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 916         emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count 
 919 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 920         byte_t buf
[USB_CMD_DATA_SIZE
]; 
 921         emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4) 
 924         cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
); 
 928 //----------------------------------------------------------------------------- 
 929 // Load a card into the emulator memory 
 931 //----------------------------------------------------------------------------- 
 932 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 933         uint8_t numSectors 
= arg0
; 
 934         uint8_t keyType 
= arg1
; 
 935         uint64_t ui64Key 
= 0; 
 937         struct Crypto1State mpcs 
= {0, 0}; 
 938         struct Crypto1State 
*pcs
; 
 942         byte_t dataoutbuf
[16]; 
 943         byte_t dataoutbuf2
[16]; 
 950         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 958         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 960                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 963         for (uint8_t sectorNo 
= 0; isOK 
&& sectorNo 
< numSectors
; sectorNo
++) { 
 964                 ui64Key 
= emlGetKey(sectorNo
, keyType
); 
 966                         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) { 
 968                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Sector[%2d]. Auth error", sectorNo
); 
 972                         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) { 
 974                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Sector[%2d]. Auth nested error", sectorNo
); 
 979                 for (uint8_t blockNo 
= 0; isOK 
&& blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 980                         if(isOK 
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) { 
 982                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
); 
 986                                 if (blockNo 
< NumBlocksPerSector(sectorNo
) - 1) { 
 987                                         emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
 988                                 } else {        // sector trailer, keep the keys, set only the AC 
 989                                         emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
 990                                         memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4); 
 991                                         emlSetMem(dataoutbuf2
,  FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
 998         if(mifare_classic_halt(pcs
, cuid
)) { 
 999                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1002         //  ----------------------------- crypto1 destroy 
1003         crypto1_destroy(pcs
); 
1005         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1008         if (MF_DBGLEVEL 
>= 2) DbpString("EMUL FILL SECTORS FINISHED"); 
1013 //----------------------------------------------------------------------------- 
1014 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn) 
1016 //----------------------------------------------------------------------------- 
1017 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
1020         uint8_t needWipe 
= arg0
; 
1021         // bit 0 - need get UID 
1022         // bit 1 - need wupC 
1023         // bit 2 - need HALT after sequence 
1024         // bit 3 - need init FPGA and field before sequence 
1025         // bit 4 - need reset FPGA and LED 
1026         uint8_t workFlags 
= arg1
; 
1027         uint8_t blockNo 
= arg2
; 
1030         uint8_t wupC1
[]       = { 0x40 };  
1031         uint8_t wupC2
[]       = { 0x43 };  
1032         uint8_t wipeC
[]       = { 0x41 };  
1036         uint8_t uid
[10] = {0x00}; 
1037         uint8_t d_block
[18] = {0x00}; 
1040         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
1041         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
1043         // reset FPGA and LED 
1044         if (workFlags 
& 0x08) { 
1051                 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1056                 // get UID from chip 
1057                 if (workFlags 
& 0x01) { 
1058                         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
1059                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
1063                         if(mifare_classic_halt(NULL
, cuid
)) { 
1064                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1071                         ReaderTransmitBitsPar(wupC1
,7,0, NULL
); 
1072                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1073                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC1 error"); 
1077                         ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
); 
1078                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1079                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wipeC error"); 
1083                         if(mifare_classic_halt(NULL
, cuid
)) { 
1084                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1090                 if (workFlags 
& 0x02) { 
1091                         ReaderTransmitBitsPar(wupC1
,7,0, NULL
); 
1092                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1093                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC1 error"); 
1097                         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1098                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1099                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC2 error"); 
1104                 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) { 
1105                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("write block send command error"); 
1109                 memcpy(d_block
, datain
, 16); 
1110                 AppendCrc14443a(d_block
, 16); 
1112                 ReaderTransmit(d_block
, sizeof(d_block
), NULL
); 
1113                 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) { 
1114                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("write block send data error"); 
1118                 if (workFlags 
& 0x04) { 
1119                         if (mifare_classic_halt(NULL
, cuid
)) { 
1120                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1130         cmd_send(CMD_ACK
,isOK
,0,0,uid
,4); 
1133         if ((workFlags 
& 0x10) || (!isOK
)) { 
1134                 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1140 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
1143         // bit 1 - need wupC 
1144         // bit 2 - need HALT after sequence 
1145         // bit 3 - need init FPGA and field before sequence 
1146         // bit 4 - need reset FPGA and LED 
1147         uint8_t workFlags 
= arg0
; 
1148         uint8_t blockNo 
= arg2
; 
1151         uint8_t wupC1
[]       = { 0x40 };  
1152         uint8_t wupC2
[]       = { 0x43 };  
1156         uint8_t data
[18] = {0x00}; 
1159         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
1160         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
1162         if (workFlags 
& 0x08) { 
1169                 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1173                 if (workFlags 
& 0x02) { 
1174                         ReaderTransmitBitsPar(wupC1
,7,0, NULL
); 
1175                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1176                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC1 error"); 
1180                         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1181                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1182                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC2 error"); 
1188                 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) { 
1189                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("read block send command error"); 
1192                 memcpy(data
, receivedAnswer
, 18); 
1194                 if (workFlags 
& 0x04) { 
1195                         if (mifare_classic_halt(NULL
, cuid
)) { 
1196                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1206         cmd_send(CMD_ACK
,isOK
,0,0,data
,18); 
1209         if ((workFlags 
& 0x10) || (!isOK
)) { 
1210                 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1215 void MifareCIdent(){ 
1218         uint8_t wupC1
[]       = { 0x40 };  
1219         uint8_t wupC2
[]       = { 0x43 };  
1224         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
1225         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
1227         ReaderTransmitBitsPar(wupC1
,7,0, NULL
); 
1228         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1232         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1233         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1237         if (mifare_classic_halt(NULL
, 0)) { 
1241         cmd_send(CMD_ACK
,isOK
,0,0,0,0); 
1248 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){ 
1250         byte_t dataout
[11] = {0x00}; 
1251         uint8_t uid
[10] = {0x00}; 
1255         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1257         int len 
= iso14443a_select_card(uid
, NULL
, &cuid
); 
1259                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)         
1260                         Dbprintf("Can't select card"); 
1265         if(mifare_desfire_des_auth1(cuid
, dataout
)){ 
1266                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)         
1267                         Dbprintf("Authentication part1: Fail."); 
1272         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED"); 
1274     cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
)); 
1277 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){ 
1279         uint32_t cuid 
= arg0
; 
1280         uint8_t key
[16] = {0x00}; 
1282         byte_t dataout
[12] = {0x00}; 
1284         memcpy(key
, datain
, 16); 
1286         isOK 
= mifare_desfire_des_auth2(cuid
, key
, dataout
); 
1289             if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
)  
1290                         Dbprintf("Authentication part2: Failed");   
1295         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
)  
1296                 DbpString("AUTH 2 FINISHED"); 
1298         cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
)); 
1299         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1305         ReaderTransmit(deselect_cmd
, 3 , NULL
); 
1306         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1310 void OnError(uint8_t reason
){ 
1312         ReaderTransmit(deselect_cmd
, 3 , NULL
); 
1313         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1314         cmd_send(CMD_ACK
,0,reason
,0,0,0);