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" 
  22 //----------------------------------------------------------------------------- 
  23 // Select, Authenticate, Read a MIFARE tag.  
  25 //----------------------------------------------------------------------------- 
  26 void MifareReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
  29         uint8_t blockNo 
= arg0
; 
  30         uint8_t keyType 
= arg1
; 
  32         ui64Key 
= bytes_to_num(datain
, 6); 
  36         byte_t dataoutbuf
[16]; 
  39         struct Crypto1State mpcs 
= {0, 0}; 
  40         struct Crypto1State 
*pcs
; 
  45         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
  52                 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
  53                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
  57                 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
  58                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
  62                 if(mifare_classic_readblock(pcs
, cuid
, blockNo
, dataoutbuf
)) { 
  63                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Read block error"); 
  67                 if(mifare_classic_halt(pcs
, cuid
)) { 
  68                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
  76         //  ----------------------------- crypto1 destroy 
  79         if (MF_DBGLEVEL 
>= 2)   DbpString("READ BLOCK FINISHED"); 
  82         cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16); 
  85         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
  89 void MifareUC_Auth(uint8_t arg0
, uint8_t *keybytes
){ 
  91         bool turnOffField 
= (arg0 
== 1); 
  93         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
  95         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
  97         if(!iso14443a_select_card(NULL
, NULL
, NULL
)) { 
  98                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card"); 
 103         if(!mifare_ultra_auth(keybytes
)){ 
 104                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Authentication failed"); 
 110                 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 113         cmd_send(CMD_ACK
,1,0,0,0,0); 
 117 // Arg1 = UsePwd bool 
 118 // datain = PWD bytes, 
 119 void MifareUReadBlock(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
) 
 121         uint8_t blockNo 
= arg0
; 
 122         byte_t dataout
[16] = {0x00}; 
 123         bool useKey 
= (arg1 
== 1); //UL_C 
 124         bool usePwd 
= (arg1 
== 2); //UL_EV1/NTAG 
 129         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 131         int len 
= iso14443a_select_card(NULL
, NULL
, NULL
); 
 133                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%02X)",len
); 
 138         // UL-C authentication 
 140                 uint8_t key
[16] = {0x00}; 
 141                 memcpy(key
, datain
, sizeof(key
) ); 
 143                 if ( !mifare_ultra_auth(key
) ) { 
 149         // UL-EV1 / NTAG authentication 
 151                 uint8_t pwd
[4] = {0x00}; 
 152                 memcpy(pwd
, datain
, 4); 
 153                 uint8_t pack
[4] = {0,0,0,0}; 
 154                 if (!mifare_ul_ev1_auth(pwd
, pack
)) { 
 160         if( mifare_ultra_readblock(blockNo
, dataout
) ) { 
 161                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Read block error"); 
 166         if( mifare_ultra_halt() ) { 
 167                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Halt error"); 
 172     cmd_send(CMD_ACK
,1,0,0,dataout
,16); 
 173         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 177 //----------------------------------------------------------------------------- 
 178 // Select, Authenticate, Read a MIFARE tag.  
 179 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes) 
 180 //----------------------------------------------------------------------------- 
 181 void MifareReadSector(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 184         uint8_t sectorNo 
= arg0
; 
 185         uint8_t keyType 
= arg1
; 
 186         uint64_t ui64Key 
= 0; 
 187         ui64Key 
= bytes_to_num(datain
, 6); 
 191         byte_t dataoutbuf
[16 * 16]; 
 194         struct Crypto1State mpcs 
= {0, 0}; 
 195         struct Crypto1State 
*pcs
; 
 201         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 208         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 210                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 214         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) { 
 216                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
 219         for (uint8_t blockNo 
= 0; isOK 
&& blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 220                 if(mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf 
+ 16 * blockNo
)) { 
 222                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Read sector %2d block %2d error", sectorNo
, blockNo
); 
 227         if(mifare_classic_halt(pcs
, cuid
)) { 
 228                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 231         //  ----------------------------- crypto1 destroy 
 232         crypto1_destroy(pcs
); 
 234         if (MF_DBGLEVEL 
>= 2) DbpString("READ SECTOR FINISHED"); 
 237         cmd_send(CMD_ACK
,isOK
,0,0,dataoutbuf
,16*NumBlocksPerSector(sectorNo
)); 
 241         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
) 
 251         // free eventually allocated BigBuf memory 
 256         uint8_t blockNo 
= arg0
; 
 257         uint16_t blocks 
= arg1
; 
 258         bool useKey 
= (arg2 
== 1); //UL_C 
 259         bool usePwd 
= (arg2 
== 2); //UL_EV1/NTAG 
 260         uint32_t countblocks 
= 0; 
 261         uint8_t *dataout 
= BigBuf_malloc(CARD_MEMORY_SIZE
); 
 262         if (dataout 
== NULL
){ 
 263                 Dbprintf("out of memory"); 
 270         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 272         int len 
= iso14443a_select_card(NULL
, NULL
, NULL
); 
 274                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card (RC:%d)",len
); 
 279         // UL-C authentication 
 281                 uint8_t key
[16] = {0x00}; 
 282                 memcpy(key
, datain
, sizeof(key
) ); 
 284                 if ( !mifare_ultra_auth(key
) ) { 
 290         // UL-EV1 / NTAG authentication 
 292                 uint8_t pwd
[4] = {0x00}; 
 293                 memcpy(pwd
, datain
, sizeof(pwd
)); 
 294                 uint8_t pack
[4] = {0,0,0,0}; 
 296                 if (!mifare_ul_ev1_auth(pwd
, pack
)){ 
 302         for (int i 
= 0; i 
< blocks
; i
++){ 
 303                 if ((i
*4) + 4 > CARD_MEMORY_SIZE
) { 
 304                         Dbprintf("Data exceeds buffer!!"); 
 308                 len 
= mifare_ultra_readblock(blockNo 
+ i
, dataout 
+ 4 * i
); 
 311                         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Read block %d error",i
); 
 312                         // if no blocks read - error out 
 317                                 //stop at last successful read block and return what we got 
 325         len 
= mifare_ultra_halt(); 
 327                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Halt error"); 
 332         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) Dbprintf("Blocks read %d", countblocks
); 
 335         cmd_send(CMD_ACK
, 1, countblocks
, countblocks
, 0, 0); 
 336         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 340 //----------------------------------------------------------------------------- 
 341 // Select, Authenticate, Write a MIFARE tag.  
 343 //----------------------------------------------------------------------------- 
 344 void MifareWriteBlock(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 347         uint8_t blockNo 
= arg0
; 
 348         uint8_t keyType 
= arg1
; 
 349         uint64_t ui64Key 
= 0; 
 350         byte_t blockdata
[16]; 
 352         ui64Key 
= bytes_to_num(datain
, 6); 
 353         memcpy(blockdata
, datain 
+ 10, 16); 
 359         struct Crypto1State mpcs 
= {0, 0}; 
 360         struct Crypto1State 
*pcs
; 
 366         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 373                         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 374                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 378                 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
 379                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Auth error"); 
 383                 if(mifare_classic_writeblock(pcs
, cuid
, blockNo
, blockdata
)) { 
 384                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Write block error"); 
 388                 if(mifare_classic_halt(pcs
, cuid
)) { 
 389                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 397         //  ----------------------------- crypto1 destroy 
 398         crypto1_destroy(pcs
); 
 400         if (MF_DBGLEVEL 
>= 2)   DbpString("WRITE BLOCK FINISHED"); 
 403         cmd_send(CMD_ACK
,isOK
,0,0,0,0); 
 408         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 412 void MifareUWriteBlock(uint8_t arg0
, uint8_t *datain
) 
 414         uint8_t blockNo 
= arg0
; 
 415         byte_t blockdata
[16] = {0x00}; 
 417         memcpy(blockdata
, datain
, 16); 
 419         uint8_t uid
[10] = {0x00}; 
 421         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
 424         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 426         if(!iso14443a_select_card(uid
, NULL
, NULL
)) { 
 427                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 432         if(mifare_ultra_writeblock(blockNo
, blockdata
)) { 
 433                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Write block error"); 
 437         if(mifare_ultra_halt()) { 
 438                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 443         if (MF_DBGLEVEL 
>= 2)   DbpString("WRITE BLOCK FINISHED"); 
 445         cmd_send(CMD_ACK
,1,0,0,0,0); 
 446         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 450 // Arg0   : Block to write to. 
 451 // Arg1   : 0 = use no authentication. 
 452 //          1 = use 0x1A authentication. 
 453 //          2 = use 0x1B authentication. 
 454 // datain : 4 first bytes is data to be written. 
 455 //        : 4/16 next bytes is authentication key. 
 456 void MifareUWriteBlock_Special(uint8_t arg0
, uint8_t arg1
, uint8_t *datain
) 
 458         uint8_t blockNo 
= arg0
; 
 459         bool useKey 
= (arg1 
== 1); //UL_C 
 460         bool usePwd 
= (arg1 
== 2); //UL_EV1/NTAG 
 461         byte_t blockdata
[4] = {0x00}; 
 463         memcpy(blockdata
, datain
,4); 
 468         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 470         if(!iso14443a_select_card(NULL
, NULL
, NULL
)) { 
 471                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Can't select card"); 
 476         // UL-C authentication 
 478                 uint8_t key
[16] = {0x00};        
 479                 memcpy(key
, datain
+4, sizeof(key
) ); 
 481                 if ( !mifare_ultra_auth(key
) ) { 
 487         // UL-EV1 / NTAG authentication 
 489                 uint8_t pwd
[4] = {0x00}; 
 490                 memcpy(pwd
, datain
+4, 4); 
 491                 uint8_t pack
[4] = {0,0,0,0}; 
 492                 if (!mifare_ul_ev1_auth(pwd
, pack
)) { 
 498         if(mifare_ultra_special_writeblock(blockNo
, blockdata
)) { 
 499                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 504         if(mifare_ultra_halt()) { 
 505                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Halt error"); 
 510         if (MF_DBGLEVEL 
>= 2) DbpString("WRITE BLOCK FINISHED"); 
 512         cmd_send(CMD_ACK
,1,0,0,0,0); 
 513         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 517 void MifareUSetPwd(uint8_t arg0
, uint8_t *datain
){ 
 519         uint8_t pwd
[16] = {0x00}; 
 520         byte_t blockdata
[4] = {0x00}; 
 522         memcpy(pwd
, datain
, 16); 
 524         LED_A_ON(); LED_B_OFF(); LED_C_OFF(); 
 526         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 528         if(!iso14443a_select_card(NULL
, NULL
, NULL
)) { 
 529                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Can't select card"); 
 534         blockdata
[0] = pwd
[7]; 
 535         blockdata
[1] = pwd
[6]; 
 536         blockdata
[2] = pwd
[5]; 
 537         blockdata
[3] = pwd
[4]; 
 538         if(mifare_ultra_special_writeblock( 44, blockdata
)) { 
 539                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 544         blockdata
[0] = pwd
[3]; 
 545         blockdata
[1] = pwd
[2]; 
 546         blockdata
[2] = pwd
[1]; 
 547         blockdata
[3] = pwd
[0]; 
 548         if(mifare_ultra_special_writeblock( 45, blockdata
)) { 
 549                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 554         blockdata
[0] = pwd
[15]; 
 555         blockdata
[1] = pwd
[14]; 
 556         blockdata
[2] = pwd
[13]; 
 557         blockdata
[3] = pwd
[12]; 
 558         if(mifare_ultra_special_writeblock( 46, blockdata
)) { 
 559                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 564         blockdata
[0] = pwd
[11]; 
 565         blockdata
[1] = pwd
[10]; 
 566         blockdata
[2] = pwd
[9]; 
 567         blockdata
[3] = pwd
[8]; 
 568         if(mifare_ultra_special_writeblock( 47, blockdata
)) { 
 569                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Write block error"); 
 574         if(mifare_ultra_halt()) { 
 575                 if (MF_DBGLEVEL 
>= 1) Dbprintf("Halt error"); 
 580         cmd_send(CMD_ACK
,1,0,0,0,0); 
 581         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 585 // Return 1 if the nonce is invalid else return 0 
 586 int valid_nonce(uint32_t Nt
, uint32_t NtEnc
, uint32_t Ks1
, uint8_t *parity
) { 
 587         return ((oddparity((Nt 
>> 24) & 0xFF) == ((parity
[0]) ^ oddparity((NtEnc 
>> 24) & 0xFF) ^ BIT(Ks1
,16))) & \
 
 588         (oddparity((Nt 
>> 16) & 0xFF) == ((parity
[1]) ^ oddparity((NtEnc 
>> 16) & 0xFF) ^ BIT(Ks1
,8))) & \
 
 589         (oddparity((Nt 
>> 8) & 0xFF) == ((parity
[2]) ^ oddparity((NtEnc 
>> 8) & 0xFF) ^ BIT(Ks1
,0)))) ? 1 : 0; 
 593 //----------------------------------------------------------------------------- 
 594 // MIFARE nested authentication.  
 596 //----------------------------------------------------------------------------- 
 597 void MifareNested(uint32_t arg0
, uint32_t arg1
, uint32_t calibrate
, uint8_t *datain
) 
 600         uint8_t blockNo 
= arg0 
& 0xff; 
 601         uint8_t keyType 
= (arg0 
>> 8) & 0xff; 
 602         uint8_t targetBlockNo 
= arg1 
& 0xff; 
 603         uint8_t targetKeyType 
= (arg1 
>> 8) & 0xff; 
 604         uint64_t ui64Key 
= 0; 
 606         ui64Key 
= bytes_to_num(datain
, 6); 
 609         uint16_t rtr
, i
, j
, len
; 
 611         static uint16_t dmin
, dmax
; 
 613         uint32_t cuid
, nt1
, nt2
, nttmp
, nttest
, ks1
; 
 615         uint32_t target_nt
[2], target_ks
[2]; 
 617         uint8_t par_array
[4]; 
 619         struct Crypto1State mpcs 
= {0, 0}; 
 620         struct Crypto1State 
*pcs
; 
 622         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
 624         uint32_t auth1_time
, auth2_time
; 
 625         static uint16_t delta_time
; 
 627         // free eventually allocated BigBuf memory 
 633         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 639         // statistics on nonce distance 
 640         if (calibrate
) {        // for first call only. Otherwise reuse previous calibration 
 648                 for (rtr 
= 0; rtr 
< 17; rtr
++) { 
 650                         // prepare next select. No need to power down the card. 
 651                         if(mifare_classic_halt(pcs
, cuid
)) { 
 652                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Halt error"); 
 657                         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 658                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Can't select card"); 
 664                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) { 
 665                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth1 error"); 
 671                                 auth2_time 
= auth1_time 
+ delta_time
; 
 675                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_NESTED
, &nt2
, &auth2_time
)) { 
 676                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth2 error"); 
 681                         nttmp 
= prng_successor(nt1
, 100);                               //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160 
 682                         for (i 
= 101; i 
< 1200; i
++) { 
 683                                 nttmp 
= prng_successor(nttmp
, 1); 
 684                                 if (nttmp 
== nt2
) break; 
 694                                         delta_time 
= auth2_time 
- auth1_time 
+ 32;  // allow some slack for proper timing 
 696                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nested: calibrating... ntdist=%d", i
); 
 700                 if (rtr 
<= 1)   return; 
 702                 davg 
= (davg 
+ (rtr 
- 1)/2) / (rtr 
- 1); 
 704                 if (MF_DBGLEVEL 
>= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin
, dmax
, davg
, delta_time
); 
 712 //  -------------------------------------------------------------------------------------------------    
 716         //  get crypted nonces for target sector 
 717         for(i
=0; i 
< 2; i
++) { // look for exactly two different nonces 
 720                 while(target_nt
[i
] == 0) { // continue until we have an unambiguous nonce 
 722                         // prepare next select. No need to power down the card. 
 723                         if(mifare_classic_halt(pcs
, cuid
)) { 
 724                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Halt error"); 
 728                         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 729                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Can't select card"); 
 734                         if(mifare_classic_authex(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, &nt1
, &auth1_time
)) { 
 735                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth1 error"); 
 739                         // nested authentication 
 740                         auth2_time 
= auth1_time 
+ delta_time
; 
 741                         len 
= mifare_sendcmd_shortex(pcs
, AUTH_NESTED
, 0x60 + (targetKeyType 
& 0x01), targetBlockNo
, receivedAnswer
, par
, &auth2_time
); 
 743                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Nested: Auth2 error len=%d", len
); 
 747                         nt2 
= bytes_to_num(receivedAnswer
, 4);           
 748                         if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i
+1, nt1
, nt2
, par
[0]); 
 750                         // Parity validity check 
 751                         for (j 
= 0; j 
< 4; j
++) { 
 752                                 par_array
[j
] = (oddparity(receivedAnswer
[j
]) != ((par
[0] >> (7-j
)) & 0x01)); 
 756                         nttest 
= prng_successor(nt1
, dmin 
- 1); 
 757                         for (j 
= dmin
; j 
< dmax 
+ 1; j
++) { 
 758                                 nttest 
= prng_successor(nttest
, 1); 
 761                                 if (valid_nonce(nttest
, nt2
, ks1
, par_array
)){ 
 762                                         if (ncount 
> 0) {               // we are only interested in disambiguous nonces, try again 
 763                                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i
+1, j
); 
 767                                         target_nt
[i
] = nttest
; 
 770                                         if (i 
== 1 && target_nt
[1] == target_nt
[0]) { // we need two different nonces 
 772                                                 if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j
); 
 775                                         if (MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i
+1, j
); 
 778                         if (target_nt
[i
] == 0 && j 
== dmax
+1 && MF_DBGLEVEL 
>= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i
+1); 
 784         //  ----------------------------- crypto1 destroy 
 785         crypto1_destroy(pcs
); 
 787         byte_t buf
[4 + 4 * 4]; 
 788         memcpy(buf
, &cuid
, 4); 
 789         memcpy(buf
+4, &target_nt
[0], 4); 
 790         memcpy(buf
+8, &target_ks
[0], 4); 
 791         memcpy(buf
+12, &target_nt
[1], 4); 
 792         memcpy(buf
+16, &target_ks
[1], 4); 
 795         cmd_send(CMD_ACK
, 0, 2, targetBlockNo 
+ (targetKeyType 
* 0x100), buf
, sizeof(buf
)); 
 798         if (MF_DBGLEVEL 
>= 3)   DbpString("NESTED FINISHED"); 
 800         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 805 //----------------------------------------------------------------------------- 
 806 // MIFARE check keys. key count up to 85.  
 808 //----------------------------------------------------------------------------- 
 809 void MifareChkKeys(uint8_t arg0
, uint8_t arg1
, uint8_t arg2
, uint8_t *datain
) 
 812         uint8_t blockNo 
= arg0
; 
 813         uint8_t keyType 
= arg1
; 
 814         uint8_t keyCount 
= arg2
; 
 815         uint64_t ui64Key 
= 0; 
 822         struct Crypto1State mpcs 
= {0, 0}; 
 823         struct Crypto1State 
*pcs
; 
 827         int OLD_MF_DBGLEVEL 
= MF_DBGLEVEL
;       
 828         MF_DBGLEVEL 
= MF_DBG_NONE
; 
 834         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 840         for (i 
= 0; i 
< keyCount
; i
++) { 
 841                 if(mifare_classic_halt(pcs
, cuid
)) { 
 842                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("ChkKeys: Halt error"); 
 845                 if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 846                         if (OLD_MF_DBGLEVEL 
>= 1)       Dbprintf("ChkKeys: Can't select card"); 
 850                 ui64Key 
= bytes_to_num(datain 
+ i 
* 6, 6); 
 851                 if(mifare_classic_auth(pcs
, cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) { 
 859         //  ----------------------------- crypto1 destroy 
 860         crypto1_destroy(pcs
); 
 863     cmd_send(CMD_ACK
,isOK
,0,0,datain 
+ i 
* 6,6); 
 866         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 869         // restore debug level 
 870         MF_DBGLEVEL 
= OLD_MF_DBGLEVEL
;   
 873 //----------------------------------------------------------------------------- 
 874 // MIFARE commands set debug level 
 876 //----------------------------------------------------------------------------- 
 877 void MifareSetDbgLvl(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 879         Dbprintf("Debug level: %d", MF_DBGLEVEL
); 
 882 //----------------------------------------------------------------------------- 
 883 // Work with emulator memory 
 885 //----------------------------------------------------------------------------- 
 886 void MifareEMemClr(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 890 void MifareEMemSet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 891         emlSetMem(datain
, arg0
, arg1
); // data, block num, blocks count 
 894 void MifareEMemGet(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 895         byte_t buf
[USB_CMD_DATA_SIZE
]; 
 896         emlGetMem(buf
, arg0
, arg1
); // data, block num, blocks count (max 4) 
 899         cmd_send(CMD_ACK
,arg0
,arg1
,0,buf
,USB_CMD_DATA_SIZE
); 
 903 //----------------------------------------------------------------------------- 
 904 // Load a card into the emulator memory 
 906 //----------------------------------------------------------------------------- 
 907 void MifareECardLoad(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 908         uint8_t numSectors 
= arg0
; 
 909         uint8_t keyType 
= arg1
; 
 910         uint64_t ui64Key 
= 0; 
 912         struct Crypto1State mpcs 
= {0, 0}; 
 913         struct Crypto1State 
*pcs
; 
 917         byte_t dataoutbuf
[16]; 
 918         byte_t dataoutbuf2
[16]; 
 925         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
 933         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
 935                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
 938         for (uint8_t sectorNo 
= 0; isOK 
&& sectorNo 
< numSectors
; sectorNo
++) { 
 939                 ui64Key 
= emlGetKey(sectorNo
, keyType
); 
 941                         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_FIRST
)) { 
 943                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Sector[%2d]. Auth error", sectorNo
); 
 947                         if(isOK 
&& mifare_classic_auth(pcs
, cuid
, FirstBlockOfSector(sectorNo
), keyType
, ui64Key
, AUTH_NESTED
)) { 
 949                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Sector[%2d]. Auth nested error", sectorNo
); 
 954                 for (uint8_t blockNo 
= 0; isOK 
&& blockNo 
< NumBlocksPerSector(sectorNo
); blockNo
++) { 
 955                         if(isOK 
&& mifare_classic_readblock(pcs
, cuid
, FirstBlockOfSector(sectorNo
) + blockNo
, dataoutbuf
)) { 
 957                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Error reading sector %2d block %2d", sectorNo
, blockNo
); 
 961                                 if (blockNo 
< NumBlocksPerSector(sectorNo
) - 1) { 
 962                                         emlSetMem(dataoutbuf
, FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
 963                                 } else {        // sector trailer, keep the keys, set only the AC 
 964                                         emlGetMem(dataoutbuf2
, FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
 965                                         memcpy(&dataoutbuf2
[6], &dataoutbuf
[6], 4); 
 966                                         emlSetMem(dataoutbuf2
,  FirstBlockOfSector(sectorNo
) + blockNo
, 1); 
 973         if(mifare_classic_halt(pcs
, cuid
)) { 
 974                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
 977         //  ----------------------------- crypto1 destroy 
 978         crypto1_destroy(pcs
); 
 980         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
 983         if (MF_DBGLEVEL 
>= 2) DbpString("EMUL FILL SECTORS FINISHED"); 
 988 //----------------------------------------------------------------------------- 
 989 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn) 
 991 //----------------------------------------------------------------------------- 
 992 void MifareCSetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
 995         uint8_t needWipe 
= arg0
; 
 996         // bit 0 - need get UID 
 998         // bit 2 - need HALT after sequence 
 999         // bit 3 - need init FPGA and field before sequence 
1000         // bit 4 - need reset FPGA and LED 
1001         uint8_t workFlags 
= arg1
; 
1002         uint8_t blockNo 
= arg2
; 
1005         uint8_t wupC1
[]       = { 0x40 };  
1006         uint8_t wupC2
[]       = { 0x43 };  
1007         uint8_t wipeC
[]       = { 0x41 };  
1011         uint8_t uid
[10] = {0x00}; 
1012         uint8_t d_block
[18] = {0x00}; 
1015         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
1016         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
1018         // reset FPGA and LED 
1019         if (workFlags 
& 0x08) { 
1026                 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1031                 // get UID from chip 
1032                 if (workFlags 
& 0x01) { 
1033                         if(!iso14443a_select_card(uid
, NULL
, &cuid
)) { 
1034                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Can't select card"); 
1038                         if(mifare_classic_halt(NULL
, cuid
)) { 
1039                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1046                         ReaderTransmitBitsPar(wupC1
,7,0, NULL
); 
1047                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1048                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC1 error"); 
1052                         ReaderTransmit(wipeC
, sizeof(wipeC
), NULL
); 
1053                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1054                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wipeC error"); 
1058                         if(mifare_classic_halt(NULL
, cuid
)) { 
1059                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1065                 if (workFlags 
& 0x02) { 
1066                         ReaderTransmitBitsPar(wupC1
,7,0, NULL
); 
1067                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1068                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC1 error"); 
1072                         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1073                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1074                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC2 error"); 
1079                 if ((mifare_sendcmd_short(NULL
, 0, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 1) || (receivedAnswer
[0] != 0x0a)) { 
1080                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("write block send command error"); 
1084                 memcpy(d_block
, datain
, 16); 
1085                 AppendCrc14443a(d_block
, 16); 
1087                 ReaderTransmit(d_block
, sizeof(d_block
), NULL
); 
1088                 if ((ReaderReceive(receivedAnswer
, receivedAnswerPar
) != 1) || (receivedAnswer
[0] != 0x0a)) { 
1089                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("write block send data error"); 
1093                 if (workFlags 
& 0x04) { 
1094                         if (mifare_classic_halt(NULL
, cuid
)) { 
1095                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1105         cmd_send(CMD_ACK
,isOK
,0,0,uid
,4); 
1108         if ((workFlags 
& 0x10) || (!isOK
)) { 
1109                 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1115 void MifareCGetBlock(uint32_t arg0
, uint32_t arg1
, uint32_t arg2
, uint8_t *datain
){ 
1118         // bit 1 - need wupC 
1119         // bit 2 - need HALT after sequence 
1120         // bit 3 - need init FPGA and field before sequence 
1121         // bit 4 - need reset FPGA and LED 
1122         uint8_t workFlags 
= arg0
; 
1123         uint8_t blockNo 
= arg2
; 
1126         uint8_t wupC1
[]       = { 0x40 };  
1127         uint8_t wupC2
[]       = { 0x43 };  
1131         uint8_t data
[18] = {0x00}; 
1134         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
1135         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
1137         if (workFlags 
& 0x08) { 
1144                 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1148                 if (workFlags 
& 0x02) { 
1149                         ReaderTransmitBitsPar(wupC1
,7,0, NULL
); 
1150                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1151                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC1 error"); 
1155                         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1156                         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1157                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("wupC2 error"); 
1163                 if ((mifare_sendcmd_short(NULL
, 0, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
) != 18)) { 
1164                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("read block send command error"); 
1167                 memcpy(data
, receivedAnswer
, 18); 
1169                 if (workFlags 
& 0x04) { 
1170                         if (mifare_classic_halt(NULL
, cuid
)) { 
1171                                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Halt error"); 
1181         cmd_send(CMD_ACK
,isOK
,0,0,data
,18); 
1184         if ((workFlags 
& 0x10) || (!isOK
)) { 
1185                 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1190 void MifareCIdent(){ 
1193         uint8_t wupC1
[]       = { 0x40 };  
1194         uint8_t wupC2
[]       = { 0x43 };  
1199         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
1200         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
1202         ReaderTransmitBitsPar(wupC1
,7,0, NULL
); 
1203         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1207         ReaderTransmit(wupC2
, sizeof(wupC2
), NULL
); 
1208         if(!ReaderReceive(receivedAnswer
, receivedAnswerPar
) || (receivedAnswer
[0] != 0x0a)) { 
1212         if (mifare_classic_halt(NULL
, 0)) { 
1216         cmd_send(CMD_ACK
,isOK
,0,0,0,0); 
1219 void MifareCollectNonces(uint32_t arg0
, uint32_t arg1
){ 
1223         uint32_t iterations 
= arg0
; 
1224         uint8_t uid
[10] = {0x00}; 
1226         uint8_t *response 
= BigBuf_malloc(MAX_MIFARE_FRAME_SIZE
); 
1227         uint8_t *responsePar 
= BigBuf_malloc(MAX_MIFARE_PARITY_SIZE
); 
1229         uint8_t mf_auth
[] = { 0x60,0x00,0xf5,0x7b }; 
1231         // get memory from BigBuf. 
1232         uint8_t *nonces 
= BigBuf_malloc(iterations 
* 4); 
1240         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1242         for (int i 
= 0; i 
< iterations
; i
++) { 
1246                 // Test if the action was cancelled 
1247                 if(BUTTON_PRESS()) break; 
1249                 //              if(mifare_classic_halt(pcs, cuid)) { 
1250                 //                      if (MF_DBGLEVEL >= 1) Dbprintf("Halt error"); 
1253                 if(!iso14443a_select_card(uid
, NULL
, NULL
)) { 
1254                         if (MF_DBGLEVEL 
>= 1) Dbprintf("Can't select card"); 
1258                 // Transmit MIFARE_CLASSIC_AUTH. 
1259                 ReaderTransmit(mf_auth
, sizeof(mf_auth
), NULL
); 
1261                 // Receive the (4 Byte) "random" nonce 
1262                 if (!ReaderReceive(response
, responsePar
)) { 
1263                         if (MF_DBGLEVEL 
>= 1)   Dbprintf("Couldn't receive tag nonce"); 
1267                 nonces
[i
*4] = bytes_to_num(response
, 4); 
1270         int packLen 
=  iterations 
* 4; 
1273         while (packLen 
> 0) { 
1274                 packSize 
= MIN(USB_CMD_DATA_SIZE
, packLen
); 
1276                 cmd_send(CMD_ACK
, 77, 0, packSize
, nonces 
- packLen
, packSize
); 
1279                 packLen 
-= packSize
; 
1282         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1290 void Mifare_DES_Auth1(uint8_t arg0
, uint8_t *datain
){ 
1292         byte_t dataout
[11] = {0x00}; 
1293         uint8_t uid
[10] = {0x00}; 
1294         uint32_t cuid 
= 0x00; 
1297         iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN
); 
1299         int len 
= iso14443a_select_card(uid
, NULL
, &cuid
); 
1301                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Can't select card"); 
1306         if(mifare_desfire_des_auth1(cuid
, dataout
)){ 
1307                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Authentication part1: Fail."); 
1312         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) DbpString("AUTH 1 FINISHED"); 
1313     cmd_send(CMD_ACK
,1,cuid
,0,dataout
, sizeof(dataout
)); 
1316 void Mifare_DES_Auth2(uint32_t arg0
, uint8_t *datain
){ 
1318         uint32_t cuid 
= arg0
; 
1319         uint8_t key
[16] = {0x00}; 
1320         byte_t dataout
[12] = {0x00}; 
1323         memcpy(key
, datain
, 16); 
1325         isOK 
= mifare_desfire_des_auth2(cuid
, key
, dataout
); 
1328             if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) Dbprintf("Authentication part2: Failed");   
1333         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) DbpString("AUTH 2 FINISHED"); 
1335         cmd_send(CMD_ACK
, isOK
, 0, 0, dataout
, sizeof(dataout
)); 
1336         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);