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 
   7 // Iceman - May 2014,2015,2016 
   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" 
  18 //----------------------------------------------------------------------------- 
  19 // Select, Authenticate, Read a MIFARE tag.  
  21 //----------------------------------------------------------------------------- 
  22 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
  25         uint8_t blockNo 
= arg0
; 
  26         uint8_t keyType 
= arg1
; 
  28         ui64Key 
= bytes_to_num(datain
, 6); 
  32         byte_t dataoutbuf
[16] = {0x00}; 
  33         uint8_t uid
[10] = {0x00}; 
  35         struct Crypto1State mpcs 
= {0, 0}; 
  36         struct Crypto1State 
*pcs
; 
  39         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
  49                 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) { 
  50                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
  54                 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
  55                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
  59                 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) { 
  60                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Read block error"); 
  64                 if(mifare_classic_halt(pcs
, cuid
)) { 
  65                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
  75         if (MF_DBGLEVEL 
>= 2)   DbpString("READ BLOCK FINISHED"); 
  78         cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16); 
  81         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
  85 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){ 
  87         bool turnOffField 
= (arg0 
== 1); 
  89         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
  91         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
  96         if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) { 
  97                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card"); 
 102         if(!mifare_ultra_auth(keybytes
)){ 
 103                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Authentication failed"); 
 109                 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 112         cmd_send(CMD_ACK
,1,0,0,0,0); 
 116 // Arg1 = UsePwd bool 
 117 // datain = PWD bytes, 
 118 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
) 
 120         uint8_t blockNo 
= arg0
; 
 121         byte_t dataout
[16] = {0x00}; 
 122         bool useKey 
= (arg1 
== 1); //UL_C 
 123         bool usePwd 
= (arg1 
== 2); //UL_EV1/NTAG 
 127         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 132         int len 
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0); 
 134                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
); 
 139         // UL-C authentication 
 141                 uint8_t key
[16] = {0x00}; 
 142                 memcpy(key
, datain
, sizeof(key
) ); 
 144                 if ( !mifare_ultra_auth(key
) ) { 
 150         // UL-EV1 / NTAG authentication 
 152                 uint8_t pwd
[4] = {0x00}; 
 153                 memcpy(pwd
, datain
, 4); 
 154                 uint8_t pack
[4] = {0,0,0,0}; 
 155                 if (!mifare_ul_ev1_auth(pwd
, pack
)) { 
 161         if( mifare_ultra_readblock(blockNo
, dataout
) ) { 
 162                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Read block error"); 
 167         if( mifare_ultra_halt() ) { 
 168                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Halt error"); 
 173     cmd_send(CMD_ACK
,1,0,0,dataout
,16); 
 174         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 178 //----------------------------------------------------------------------------- 
 179 // Select, Authenticate, Read a MIFARE tag.  
 180 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes) 
 181 //----------------------------------------------------------------------------- 
 182 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 185         uint8_t sectorNo 
= arg0
; 
 186         uint8_t keyType 
= arg1
; 
 187         uint64_t ui64Key 
= 0; 
 188         ui64Key 
= bytes_to_num(datain
, 6); 
 192         byte_t dataoutbuf
[16 * 16]; 
 193         uint8_t uid
[10] = {0x00}; 
 195         struct Crypto1State mpcs 
= {0, 0}; 
 196         struct Crypto1State 
*pcs
; 
 199         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 209         if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) { 
 211                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 215         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) { 
 217                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
 220         for (uint8_t blockNo 
= 0; isOK 
&& blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 221                 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf 
+ 16 * blockNo
)) { 
 223                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
); 
 228         if(mifare_classic_halt(pcs
, cuid
)) { 
 229                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 232         if (MF_DBGLEVEL 
>= 2) DbpString("READ SECTOR FINISHED"); 
 234         crypto1_destroy(pcs
); 
 237         cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
)); 
 240         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 245 // arg0 = blockNo (start) 
 246 // arg1 = Pages (number of blocks) 
 248 // datain = KEY bytes 
 249 void MifareUReadCard(uint8_t arg0
, uint16_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 253         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 255         // free eventually allocated BigBuf memory 
 256         BigBuf_free(); BigBuf_Clear_ext(false); 
 261         uint8_t blockNo 
= arg0
; 
 262         uint16_t blocks 
= arg1
; 
 263         bool useKey 
= (arg2 
== 1); //UL_C 
 264         bool usePwd 
= (arg2 
== 2); //UL_EV1/NTAG 
 265         uint32_t countblocks 
= 0; 
 266         uint8_t *dataout 
= BigBuf_malloc(CARD_MEMORY_SIZE
); 
 267         if (dataout 
== NULL
){ 
 268                 Dbprintf("out of memory"); 
 273         int len 
= iso14443a_select_card(NULL
, NULL
, NULL
, true, 0); 
 275                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
); 
 280         // UL-C authentication 
 282                 uint8_t key
[16] = {0x00}; 
 283                 memcpy(key
, datain
, sizeof(key
) ); 
 285                 if ( !mifare_ultra_auth(key
) ) { 
 291         // UL-EV1 / NTAG authentication 
 293                 uint8_t pwd
[4] = {0x00}; 
 294                 memcpy(pwd
, datain
, sizeof(pwd
)); 
 295                 uint8_t pack
[4] = {0,0,0,0}; 
 297                 if (!mifare_ul_ev1_auth(pwd
, pack
)){ 
 303         for (int i 
= 0; i 
< blocks
; i
++){ 
 304                 if ((i
*4) + 4 >= CARD_MEMORY_SIZE
) { 
 305                         Dbprintf("Data exceeds buffer!!"); 
 309                 len 
= mifare_ultra_readblock(blockNo 
+ i
, dataout 
+ 4 * i
); 
 312                         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
); 
 313                         // if no blocks read - error out 
 318                                 //stop at last successful read block and return what we got 
 326         len 
= mifare_ultra_halt(); 
 328                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Halt error"); 
 333         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
); 
 337         cmd_send(CMD_ACK
, 1, countblocks
, BigBuf_max_traceLen(), 0, 0); 
 338         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 344 //----------------------------------------------------------------------------- 
 345 // Select, Authenticate, Write a MIFARE tag.  
 347 //----------------------------------------------------------------------------- 
 348 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 351         uint8_t blockNo 
= arg0
; 
 352         uint8_t keyType 
= arg1
; 
 353         uint64_t ui64Key 
= 0; 
 354         byte_t blockdata
[16] = {0x00}; 
 356         ui64Key 
= bytes_to_num(datain
, 6); 
 357         memcpy(blockdata
, datain 
+ 10, 16); 
 361         uint8_t uid
[10] = {0x00}; 
 363         struct Crypto1State mpcs 
= {0, 0}; 
 364         struct Crypto1State 
*pcs
; 
 367         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 377                 if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) { 
 378                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 382                 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
 383                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
 387                 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) { 
 388                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Write block error"); 
 392                 if(mifare_classic_halt(pcs
, cuid
)) { 
 393                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 401         crypto1_destroy(pcs
); 
 403         if (MF_DBGLEVEL 
>= 2)   DbpString("WRITE BLOCK FINISHED"); 
 405         cmd_send(CMD_ACK
,isOK
,0,0,0,0); 
 407         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 412 /* // Command not needed but left for future testing  
 413 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain) 
 415         uint8_t blockNo = arg0; 
 416         byte_t blockdata[16] = {0x00}; 
 418         memcpy(blockdata, datain, 16); 
 420         uint8_t uid[10] = {0x00}; 
 422         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
 426         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); 
 428         if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) { 
 429                 if (MF_DBGLEVEL >= 1)   Dbprintf("Can't select card"); 
 434         if(mifare_ultra_writeblock_compat(blockNo, blockdata)) { 
 435                 if (MF_DBGLEVEL >= 1)   Dbprintf("Write block error"); 
 439         if(mifare_ultra_halt()) { 
 440                 if (MF_DBGLEVEL >= 1)   Dbprintf("Halt error"); 
 445         if (MF_DBGLEVEL >= 2)   DbpString("WRITE BLOCK FINISHED"); 
 447         cmd_send(CMD_ACK,1,0,0,0,0); 
 448         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); 
 453 // Arg0   : Block to write to. 
 454 // Arg1   : 0 = use no authentication. 
 455 //          1 = use 0x1A authentication. 
 456 //          2 = use 0x1B authentication. 
 457 // datain : 4 first bytes is data to be written. 
 458 //        : 4/16 next bytes is authentication key. 
 459 void MifareUWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
) 
 461         uint8_t blockNo 
= arg0
; 
 462         bool useKey 
= (arg1 
== 1); //UL_C 
 463         bool usePwd 
= (arg1 
== 2); //UL_EV1/NTAG 
 464         byte_t blockdata
[4] = {0x00}; 
 466         memcpy(blockdata
, datain
,4); 
 470         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 475         if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) { 
 476                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Can't select card"); 
 481         // UL-C authentication 
 483                 uint8_t key
[16] = {0x00};        
 484                 memcpy(key
, datain
+4, sizeof(key
) ); 
 486                 if ( !mifare_ultra_auth(key
) ) { 
 492         // UL-EV1 / NTAG authentication 
 494                 uint8_t pwd
[4] = {0x00}; 
 495                 memcpy(pwd
, datain
+4, 4); 
 496                 uint8_t pack
[4] = {0,0,0,0}; 
 497                 if (!mifare_ul_ev1_auth(pwd
, pack
)) { 
 503         if(mifare_ultra_writeblock(blockNo
, blockdata
)) { 
 504                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 509         if(mifare_ultra_halt()) { 
 510                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Halt error"); 
 515         if (MF_DBGLEVEL 
>= 2) DbpString("WRITE BLOCK FINISHED"); 
 517         cmd_send(CMD_ACK
,1,0,0,0,0); 
 518         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 523 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){ 
 525         uint8_t pwd
[16] = {0x00}; 
 526         byte_t blockdata
[4] = {0x00}; 
 528         memcpy(pwd
, datain
, 16); 
 530         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
 531         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 536         if(!iso14443a_select_card(NULL
, NULL
, NULL
, true, 0)) { 
 537                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Can't select card"); 
 542         blockdata
[0] = pwd
[7]; 
 543         blockdata
[1] = pwd
[6]; 
 544         blockdata
[2] = pwd
[5]; 
 545         blockdata
[3] = pwd
[4]; 
 546         if(mifare_ultra_writeblock( 44, blockdata
)) { 
 547                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 552         blockdata
[0] = pwd
[3]; 
 553         blockdata
[1] = pwd
[2]; 
 554         blockdata
[2] = pwd
[1]; 
 555         blockdata
[3] = pwd
[0]; 
 556         if(mifare_ultra_writeblock( 45, blockdata
)) { 
 557                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 562         blockdata
[0] = pwd
[15]; 
 563         blockdata
[1] = pwd
[14]; 
 564         blockdata
[2] = pwd
[13]; 
 565         blockdata
[3] = pwd
[12]; 
 566         if(mifare_ultra_writeblock( 46, blockdata
)) { 
 567                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 572         blockdata
[0] = pwd
[11]; 
 573         blockdata
[1] = pwd
[10]; 
 574         blockdata
[2] = pwd
[9]; 
 575         blockdata
[3] = pwd
[8]; 
 576         if(mifare_ultra_writeblock( 47, blockdata
)) { 
 577                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 582         if(mifare_ultra_halt()) { 
 583                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Halt error"); 
 588         cmd_send(CMD_ACK
,1,0,0,0,0); 
 589         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 594 // Return 1 if the nonce is invalid else return 0 
 595 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) { 
 596         return ((oddparity8((Nt 
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity8((NtEnc 
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
 
 597         (oddparity8((Nt 
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity8((NtEnc 
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
 
 598         (oddparity8((Nt 
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity8((NtEnc 
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0; 
 602 //----------------------------------------------------------------------------- 
 603 // acquire encrypted nonces in order to perform the attack described in 
 604 // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened 
 605 // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on  
 606 // Computer and Communications Security, 2015 
 607 //----------------------------------------------------------------------------- 
 608 #define AUTHENTICATION_TIMEOUT  848 //848                       // card times out 1ms after wrong authentication (according to NXP documentation) 
 609 #define PRE_AUTHENTICATION_LEADTIME 400         // some (non standard) cards need a pause after select before they are ready for first authentication  
 611 void MifareAcquireEncryptedNonces(uint32_t arg0
, uint32_t arg1
, uint32_t flags
, uint8_t *datain
) 
 613         uint64_t ui64Key 
= 0; 
 614         uint8_t uid
[10] = {0x00}; 
 616         uint8_t cascade_levels 
= 0; 
 617         struct Crypto1State mpcs 
= {0, 0}; 
 618         struct Crypto1State 
*pcs
; 
 620         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00}; 
 622         uint8_t par_enc
[1] = {0x00}; 
 623         uint8_t nt_par_enc 
= 0; 
 624         uint8_t buf
[USB_CMD_DATA_SIZE
] = {0x00}; 
 625         uint32_t timeout 
= 0; 
 627         uint8_t blockNo 
= arg0 
& 0xff; 
 628         uint8_t keyType 
= (arg0 
>> 8) & 0xff; 
 629         uint8_t targetBlockNo 
= arg1 
& 0xff; 
 630         uint8_t targetKeyType 
= (arg1 
>> 8) & 0xff; 
 631         ui64Key 
= bytes_to_num(datain
, 6); 
 632         bool initialize 
= flags 
& 0x0001; 
 633         bool slow 
= flags 
& 0x0002; 
 634         bool field_off 
= flags 
& 0x0004; 
 639         BigBuf_free(); BigBuf_Clear_ext(false);  
 644                 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 649         uint8_t dummy_answer 
= 0;        
 650         uint16_t num_nonces 
= 0; 
 651         bool have_uid 
= false; 
 652         for (uint16_t i 
= 0; i 
<= USB_CMD_DATA_SIZE 
- 9; ) { 
 654                 // Test if the action was cancelled 
 661                 if (!have_uid
) { // need a full select cycle to get the uid first 
 662                         iso14a_card_select_t card_info
;          
 663                         if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0)) { 
 664                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("AcquireNonces: Can't select card (ALL)"); 
 667                         switch (card_info
.uidlen
) { 
 668                                 case 4 : cascade_levels 
= 1; break; 
 669                                 case 7 : cascade_levels 
= 2; break; 
 670                                 case 10: cascade_levels 
= 3; break; 
 674                 } else { // no need for anticollision. We can directly select the card 
 675                         if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
)) { 
 676                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("AcquireNonces: Can't select card (UID)"); 
 682                         timeout 
= GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME
; 
 683                         while(GetCountSspClk() < timeout
); 
 687                 if (mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, NULL
)) { 
 688                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("AcquireNonces: Auth1 error"); 
 692                 // nested authentication 
 693                 uint16_t len 
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType 
& 0x01), targetBlockNo
, receivedAnswer
, par_enc
, NULL
); 
 695                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("AcquireNonces: Auth2 error len=%d", len
); 
 699                 // send a dummy byte as reader response in order to trigger the cards authentication timeout 
 700                 ReaderTransmit(&dummy_answer
, 1, NULL
); 
 701                 timeout 
= GetCountSspClk() + AUTHENTICATION_TIMEOUT
; 
 704                 if (num_nonces 
% 2) { 
 705                         memcpy(buf
+i
, receivedAnswer
, 4); 
 706                         nt_par_enc 
= par_enc
[0] & 0xf0; 
 708                         nt_par_enc 
|= par_enc
[0]  >> 4; 
 709                         memcpy(buf
+i
+4, receivedAnswer
, 4); 
 710                         memcpy(buf
+i
+8, &nt_par_enc
, 1); 
 713                 // wait for the card to become ready again 
 714                 while(GetCountSspClk() < timeout
);       
 718         crypto1_destroy(pcs
);    
 720         cmd_send(CMD_ACK
, isOK
, cuid
, num_nonces
, buf
, sizeof(buf
)); 
 723         if (MF_DBGLEVEL 
>= 3)   DbpString("AcquireEncryptedNonces finished"); 
 726                 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 733 //----------------------------------------------------------------------------- 
 734 // MIFARE nested authentication.  
 736 //----------------------------------------------------------------------------- 
 737 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
) 
 740         uint8_t blockNo 
= arg0 
& 0xff; 
 741         uint8_t keyType 
= (arg0 
>> 8) & 0xff; 
 742         uint8_t targetBlockNo 
= arg1 
& 0xff; 
 743         uint8_t targetKeyType 
= (arg1 
>> 8) & 0xff; 
 744         uint64_t ui64Key 
= 0; 
 746         ui64Key 
= bytes_to_num(datain
, 6); 
 749         uint16_t rtr
, i
, j
, len
; 
 751         static uint16_t dmin
, dmax
; 
 752         uint8_t uid
[10] = {0x00}; 
 753         uint32_t cuid 
= 0, nt1
, nt2
, nttmp
, nttest
, ks1
; 
 754         uint8_t par
[1] = {0x00}; 
 755         uint32_t target_nt
[2] = {0x00}, target_ks
[2] = {0x00}; 
 757         uint8_t par_array
[4] = {0x00}; 
 759         struct Crypto1State mpcs 
= {0, 0}; 
 760         struct Crypto1State 
*pcs
; 
 762         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00}; 
 764         uint32_t auth1_time
, auth2_time
; 
 765         static uint16_t delta_time 
= 0; 
 769         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 771         // free eventually allocated BigBuf memory 
 772         BigBuf_free(); BigBuf_Clear_ext(false); 
 774         if (calibrate
) clear_trace(); 
 777         // statistics on nonce distance 
 779         #define NESTED_MAX_TRIES 12 
 780         uint16_t unsuccessfull_tries 
= 0; 
 781         if (calibrate
) {        // for first call only. Otherwise reuse previous calibration 
 789                 for (rtr 
= 0; rtr 
< 17; rtr
++) { 
 791                         // Test if the action was cancelled 
 797                         // prepare next select. No need to power down the card. 
 798                         if(mifare_classic_halt(pcs
, cuid
)) { 
 799                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Halt error"); 
 804                         if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) { 
 805                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Can't select card"); 
 811                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) { 
 812                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth1 error"); 
 816                         auth2_time 
= (delta_time
) ? auth1_time 
+ delta_time 
: 0; 
 818                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) { 
 819                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth2 error"); 
 824                         nttmp 
= prng_successor(nt1
, 100);                               //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160 
 825                         for (i 
= 101; i 
< 1200; i
++) { 
 826                                 nttmp 
= prng_successor(nttmp
, 1); 
 827                                 if (nttmp 
== nt2
) break; 
 837                                         delta_time 
= auth2_time 
- auth1_time 
+ 32;  // allow some slack for proper timing 
 839                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
); 
 841                                 unsuccessfull_tries
++; 
 842                                 if (unsuccessfull_tries 
> NESTED_MAX_TRIES
) {   // card isn't vulnerable to nested attack (random numbers are not predictable) 
 848                 davg 
= (davg 
+ (rtr 
- 1)/2) / (rtr 
- 1); 
 850                 if (MF_DBGLEVEL 
>= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr
, isOK
, dmin
, dmax
, davg
, delta_time
); 
 857 //  -------------------------------------------------------------------------------------------------    
 861         //  get crypted nonces for target sector 
 862         for(i
=0; i 
< 2 && !isOK
; i
++) { // look for exactly two different nonces 
 865                 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce 
 867                         // prepare next select. No need to power down the card. 
 868                         if(mifare_classic_halt(pcs
, cuid
)) { 
 869                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Halt error"); 
 873                         if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) { 
 874                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Can't select card"); 
 879                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) { 
 880                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth1 error"); 
 884                         // nested authentication 
 885                         auth2_time 
= auth1_time 
+ delta_time
; 
 887                         len 
= mifare_sendcmd_short(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType 
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
); 
 889                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth2 error len=%d", len
); 
 893                         nt2 
= bytes_to_num(receivedAnswer
, 4);           
 894                         if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]); 
 896                         // Parity validity check 
 897 //                      for (j = 0; j < 4; j++) { 
 898 //                              par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01)); 
 900                         par_array
[0] = (oddparity8(receivedAnswer
[0]) != ((par
[0] >> (7-0)) & 0x01)); 
 901                         par_array
[1] = (oddparity8(receivedAnswer
[1]) != ((par
[0] >> (7-1)) & 0x01)); 
 902                         par_array
[2] = (oddparity8(receivedAnswer
[2]) != ((par
[0] >> (7-2)) & 0x01)); 
 903                         par_array
[3] = (oddparity8(receivedAnswer
[3]) != ((par
[0] >> (7-3)) & 0x01)); 
 906                         nttest 
= prng_successor(nt1
, dmin 
- 1); 
 907                         for (j 
= dmin
; j 
< dmax 
+ 1; j
++) { 
 908                                 nttest 
= prng_successor(nttest
, 1); 
 911                                 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){ 
 912                                         if (ncount 
> 0) {               // we are only interested in disambiguous nonces, try again 
 913                                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
); 
 917                                         target_nt
[i
] = nttest
; 
 920                                         if (i 
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces 
 922                                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
); 
 925                                         if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
); 
 928                         if (target_nt
[i
] == 0 && j 
== dmax
+1 && MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1); 
 934         crypto1_destroy(pcs
); 
 936         byte_t buf
[4 + 4 * 4] = {0}; 
 937         memcpy(buf
, &cuid
, 4); 
 938         memcpy(buf
+4, &target_nt
[0], 4); 
 939         memcpy(buf
+8, &target_ks
[0], 4); 
 940         memcpy(buf
+12, &target_nt
[1], 4); 
 941         memcpy(buf
+16, &target_ks
[1], 4); 
 944         cmd_send(CMD_ACK
, isOK
, 0, targetBlockNo 
+ (targetKeyType 
* 0x100), buf
, sizeof(buf
)); 
 947         if (MF_DBGLEVEL 
>= 3)   DbpString("NESTED FINISHED"); 
 949         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 954 //----------------------------------------------------------------------------- 
 955 // MIFARE check keys. key count up to 85.  
 957 //----------------------------------------------------------------------------- 
 958 void MifareChkKeys(uint16_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) { 
 959         uint8_t blockNo 
= arg0 
& 0xff; 
 960         uint8_t keyType 
= (arg0 
>> 8) & 0xff; 
 961         bool clearTrace 
= arg1
; 
 962         uint8_t keyCount 
= arg2
; 
 963         uint64_t ui64Key 
= 0; 
 965         bool have_uid 
= FALSE
; 
 966         uint8_t cascade_levels 
= 0; 
 967         uint32_t timeout 
= 0; 
 971         uint8_t uid
[10] = {0x00}; 
 973         struct Crypto1State mpcs 
= {0, 0}; 
 974         struct Crypto1State 
*pcs
; 
 977         // save old debuglevel, and tempory turn off dbg printing. speedissues. 
 978         int OLD_MF_DBGLEVEL 
= MF_DBGLEVEL
;       
 979         MF_DBGLEVEL 
= MF_DBG_NONE
; 
 984         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 991         for (i 
= 0; i 
< keyCount
; ++i
) { 
 993                 //mifare_classic_halt(pcs, cuid); 
 995                 // this part is from Piwi's faster nonce collecting part in Hardnested. 
 996                 if (!have_uid
) { // need a full select cycle to get the uid first 
 997                         iso14a_card_select_t card_info
;          
 998                         if(!iso14443a_select_card(uid
, &card_info
, &cuid
, true, 0)) { 
 999                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("ChkKeys: Can't select card (ALL)"); 
1002                         switch (card_info
.uidlen
) { 
1003                                 case 4 : cascade_levels 
= 1; break; 
1004                                 case 7 : cascade_levels 
= 2; break; 
1005                                 case 10: cascade_levels 
= 3; break; 
1009                 } else { // no need for anticollision. We can directly select the card 
1010                         if(!iso14443a_select_card(uid
, NULL
, NULL
, false, cascade_levels
)) { 
1011                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("ChkKeys: Can't select card (UID)"); 
1016                 ui64Key 
= bytes_to_num(datain 
+ i 
* 6, 6); 
1018                 if (mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
1020                         uint8_t dummy_answer 
= 0; 
1021                         ReaderTransmit(&dummy_answer
, 1, NULL
); 
1022                         timeout 
= GetCountSspClk() + AUTHENTICATION_TIMEOUT
; 
1024                         // wait for the card to become ready again 
1025                         while(GetCountSspClk() < timeout
); 
1034     cmd_send(CMD_ACK
, isOK
, 0, 0, datain 
+ i 
* 6, 6); 
1036         // restore debug level 
1037         MF_DBGLEVEL 
= OLD_MF_DBGLEVEL
;   
1039         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1042         crypto1_destroy(pcs
); 
1045 //----------------------------------------------------------------------------- 
1046 // MIFARE commands set debug level 
1048 //----------------------------------------------------------------------------- 
1049 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
1051         Dbprintf("Debug level: %d", MF_DBGLEVEL
); 
1054 //----------------------------------------------------------------------------- 
1055 // Work with emulator memory 
1057 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not 
1058 // involved in dealing with emulator memory. But if it is called later, it might 
1059 // destroy the Emulator Memory. 
1060 //----------------------------------------------------------------------------- 
1062 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
1063         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1067 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
1068         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1069         if (arg2
==0) arg2 
= 16; // backwards compat... default bytewidth 
1070         emlSetMem_xt(datain
, arg0
, arg1
, arg2
); // data, block num, blocks count, block byte width 
1073 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
1074         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1075         byte_t buf
[USB_CMD_DATA_SIZE
] = {0x00}; 
1076         emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4) 
1079         cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
); 
1083 //----------------------------------------------------------------------------- 
1084 // Load a card into the emulator memory 
1086 //----------------------------------------------------------------------------- 
1087 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
1088         uint8_t numSectors 
= arg0
; 
1089         uint8_t keyType 
= arg1
; 
1090         uint64_t ui64Key 
= 0; 
1092         struct Crypto1State mpcs 
= {0, 0}; 
1093         struct Crypto1State 
*pcs
; 
1097         byte_t dataoutbuf
[16] = {0x00}; 
1098         byte_t dataoutbuf2
[16] = {0x00}; 
1099         uint8_t uid
[10] = {0x00}; 
1104         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1111         if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) { 
1113                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
1116         for (uint8_t sectorNo 
= 0; isOK 
&& sectorNo 
< numSectors
; sectorNo
++) { 
1117                 ui64Key 
= emlGetKey(sectorNo
, keyType
); 
1119                         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) { 
1121                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Sector[%2d]. Auth error", sectorNo
); 
1125                         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) { 
1127                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Sector[%2d]. Auth nested error", sectorNo
); 
1132                 for (uint8_t blockNo 
= 0; isOK 
&& blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
1133                         if(isOK 
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) { 
1135                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
); 
1139                                 if (blockNo 
< NumBlocksPerSector(sectorNo
) - 1) { 
1140                                         emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
1141                                 } else {        // sector trailer, keep the keys, set only the AC 
1142                                         emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
1143                                         memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4); 
1144                                         emlSetMem(dataoutbuf2
,  FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
1151         if(mifare_classic_halt(pcs
, cuid
)) 
1152                 if (MF_DBGLEVEL 
>= 1) 
1153                         Dbprintf("Halt error"); 
1155         //  ----------------------------- crypto1 destroy 
1156         crypto1_destroy(pcs
); 
1158         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1161         if (MF_DBGLEVEL 
>= 2) DbpString("EMUL FILL SECTORS FINISHED"); 
1167 //----------------------------------------------------------------------------- 
1168 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn) 
1170 // PARAMS - workFlags 
1171 // bit 0 - need get UID 
1172 // bit 1 - need wupC 
1173 // bit 2 - need HALT after sequence 
1174 // bit 3 - need turn on FPGA before sequence 
1175 // bit 4 - need turn off FPGA 
1176 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a) 
1177 // bit 6 - wipe tag. 
1178 //----------------------------------------------------------------------------- 
1179 // magic uid card generation 1 commands 
1180 uint8_t wupC1
[] = { MIFARE_MAGICWUPC1 
};  
1181 uint8_t wupC2
[] = { MIFARE_MAGICWUPC2 
};  
1182 uint8_t wipeC
[] = { MIFARE_MAGICWIPEC 
};  
1184 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
){ 
1187         uint8_t workFlags 
= arg0
; 
1188         uint8_t blockNo 
= arg1
; 
1191         bool isOK 
= false; //assume we will get an error 
1192         uint8_t errormsg 
= 0x00; 
1193         uint8_t uid
[10] = {0x00}; 
1194         uint8_t data
[18] = {0x00}; 
1197         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00}; 
1198         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00}; 
1200         if (workFlags 
& MAGIC_INIT
) { 
1203                 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1208         //loop doesn't loop just breaks out if error 
1210                 // read UID and return to client with write 
1211                 if (workFlags 
& MAGIC_UID
) { 
1212                         if(!iso14443a_select_card(uid
, NULL
, &cuid
, true, 0)) { 
1213                                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)        Dbprintf("Can't select card"); 
1214                                 errormsg 
= MAGIC_UID
; 
1216                         mifare_classic_halt_ex(NULL
); 
1220                 // wipe tag, fill it with zeros 
1221                 if (workFlags 
& MAGIC_WIPE
){ 
1222                         ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
); 
1223                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1224                                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)        Dbprintf("wupC1 error"); 
1225                                 errormsg 
= MAGIC_WIPE
; 
1229                         ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
); 
1230                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1231                                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)        Dbprintf("wipeC error"); 
1232                                 errormsg 
= MAGIC_WIPE
; 
1236                         mifare_classic_halt_ex(NULL
); 
1240                 if (workFlags 
& MAGIC_WUPC
) { 
1241                         ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
); 
1242                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1243                                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)        Dbprintf("wupC1 error"); 
1244                                 errormsg 
= MAGIC_WUPC
; 
1248                         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1249                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1250                                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)        Dbprintf("wupC2 error"); 
1251                                 errormsg 
= MAGIC_WUPC
; 
1256                 if ((mifare_sendcmd_short(NULL
, 0, ISO14443A_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) { 
1257                         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)        Dbprintf("write block send command error"); 
1262                 memcpy(data
, datain
, 16); 
1263                 AppendCrc14443a(data
, 16); 
1265                 ReaderTransmit(data
, sizeof(data
), NULL
); 
1266                 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) { 
1267                         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)        Dbprintf("write block send data error"); 
1272                 if (workFlags 
& MAGIC_OFF
)  
1273                         mifare_classic_halt_ex(NULL
); 
1281                 cmd_send(CMD_ACK
,1,0,0,uid
,sizeof(uid
)); 
1283                 OnErrorMagic(errormsg
); 
1285         if (workFlags 
& MAGIC_OFF
) 
1289 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint8_t *datain
){ 
1291         uint8_t workFlags 
= arg0
; 
1292         uint8_t blockNo 
= arg1
; 
1293         uint8_t errormsg 
= 0x00; 
1294         bool isOK 
= false; //assume we will get an error 
1297         uint8_t data
[MAX_MIFARE_FRAME_SIZE
]; 
1298         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00}; 
1299         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00}; 
1301         memset(data
, 0x00, sizeof(data
)); 
1303         if (workFlags 
& MAGIC_INIT
) { 
1306                 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
);        
1311         //loop doesn't loop just breaks out if error or done 
1313                 if (workFlags 
& MAGIC_WUPC
) { 
1314                         ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
); 
1315                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1316                                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("wupC1 error"); 
1317                                 errormsg 
= MAGIC_WUPC
; 
1321                         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1322                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1323                                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("wupC2 error"); 
1324                                 errormsg 
= MAGIC_WUPC
; 
1330                 if ((mifare_sendcmd_short(NULL
, 0, ISO14443A_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) { 
1331                         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("read block send command error"); 
1336                 memcpy(data
, receivedAnswer
, sizeof(data
)); 
1339                 if (workFlags 
& MAGIC_HALT
) 
1340                         mifare_classic_halt_ex(NULL
); 
1345         // if MAGIC_DATAIN, the data stays on device side. 
1346         if (workFlags 
& MAGIC_DATAIN
) { 
1348                         memcpy(datain
, data
, sizeof(data
)); 
1351                         cmd_send(CMD_ACK
,1,0,0,data
,sizeof(data
));       
1353                         OnErrorMagic(errormsg
);  
1356         if (workFlags 
& MAGIC_OFF
) 
1360 void MifareCIdent(){ 
1364         uint8_t receivedAnswer
[1] = {0x00}; 
1365         uint8_t receivedAnswerPar
[1] = {0x00}; 
1367         ReaderTransmitBitsPar(wupC1
, 7, NULL
, NULL
); 
1368         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1372         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1373         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1377         // removed the if,  since some magic tags misbehavies and send an answer to it. 
1378         mifare_classic_halt(NULL
, 0); 
1379         cmd_send(CMD_ACK
,isOK
,0,0,0,0); 
1382 void OnSuccessMagic(){ 
1383         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1387 void OnErrorMagic(uint8_t reason
){ 
1388         //          ACK, ISOK, reason,0,0,0 
1389         cmd_send(CMD_ACK
,0,reason
,0,0,0); 
1395 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){ 
1396         byte_t dataout
[12] = {0x00}; 
1397         uint8_t uid
[10] = {0x00}; 
1400         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1404         int len 
= iso14443a_select_card(uid
, NULL
, &cuid
, true, 0); 
1406                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card"); 
1411         if(mifare_desfire_des_auth1(cuid
, dataout
)){ 
1412                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail."); 
1417         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED"); 
1418     cmd_send(CMD_ACK
, 1, cuid
, 0, dataout
, sizeof(dataout
)); 
1421 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){ 
1422         uint32_t cuid 
= arg0
; 
1423         uint8_t key
[16] = {0x00}; 
1424         byte_t dataout
[12] = {0x00}; 
1427         memcpy(key
, datain
, 16); 
1429         isOK 
= mifare_desfire_des_auth2(cuid
, key
, dataout
); 
1432             if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");   
1437         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED"); 
1439         cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
)); 
1440         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);