]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
1de4819e737ba9bf9ccec0885586a6ffbd0d7bf1
   1 //----------------------------------------------------------------------------- 
   2 // Merlok, May 2011, 2012 
   3 // Many authors, whom made it possible 
   5 // This code is licensed to you under the terms of the GNU GPL, version 2 or, 
   6 // at your option, any later version. See the LICENSE.txt file for the text of 
   8 //----------------------------------------------------------------------------- 
   9 // Work with mifare cards. 
  10 //----------------------------------------------------------------------------- 
  12 #include "proxmark3.h" 
  17 #include "iso14443crc.h" 
  18 #include "iso14443a.h" 
  20 #include "mifareutil.h" 
  22 int MF_DBGLEVEL 
= MF_DBG_ALL
; 
  25 uint8_t* get_bigbufptr_recvrespbuf(void) { 
  26         return BigBuf_get_addr() + RECV_RESP_OFFSET
;     
  28 uint8_t* get_bigbufptr_recvcmdbuf(void) { 
  29         return BigBuf_get_addr() + RECV_CMD_OFFSET
;      
  31 uint8_t* get_bigbufptr_emlcardmem(void) { 
  32         return BigBuf_get_addr() + CARD_MEMORY_OFFSET
; 
  36 void mf_crypto1_decrypt(struct Crypto1State 
*pcs
, uint8_t *data
, int len
){ 
  41                 for (i 
= 0; i 
< len
; i
++) 
  42                         data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
]; 
  45                 for (i 
= 0; i 
< 4; i
++) 
  46                         bt 
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], i
)) << i
; 
  53 void mf_crypto1_encrypt(struct Crypto1State 
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) { 
  58         for (i 
= 0; i 
< len
; i
++) { 
  60                 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
]; 
  63                 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity(bt
)) & 0x01)<<(7-(i
&0x0007))); 
  68 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State 
*pcs
, uint8_t data
) { 
  72         for (i 
= 0; i 
< 4; i
++) 
  73                 bt 
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
; 
  79 int mifare_sendcmd_short(struct Crypto1State 
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t data
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
) 
  81         return mifare_sendcmd_shortex(pcs
, crypted
, cmd
, data
, answer
, answer_parity
, timing
); 
  84 int mifare_sendcmd_short_special(struct Crypto1State 
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t* data
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
) 
  93         AppendCrc14443a(dcmd
, 6); 
  94         ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
); 
  95         int len 
= ReaderReceive(answer
, answer_parity
); 
  97                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Authentication failed. Card timeout."); 
 103 int mifare_sendcmd_short_mfucauth(struct Crypto1State 
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t *data
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
) 
 108     memcpy(dcmd
+1,data
,16); 
 109         AppendCrc14443a(dcmd
, 17); 
 111         ReaderTransmit(dcmd
, sizeof(dcmd
), timing
); 
 112         len 
= ReaderReceive(answer
, answer_parity
); 
 114         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)   Dbprintf("Authentication failed. Card timeout."); 
 115         len 
= ReaderReceive(answer
,answer_parity
); 
 118                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)   Dbprintf("NAK - Authentication failed."); 
 124 int mifare_sendcmd_shortex(struct Crypto1State 
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t data
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
) 
 126         uint8_t dcmd
[4], ecmd
[4]; 
 128         uint8_t par
[1];                 // 1 Byte parity is enough here 
 131         AppendCrc14443a(dcmd
, 2); 
 133         memcpy(ecmd
, dcmd
, sizeof(dcmd
)); 
 137                 for (pos 
= 0; pos 
< 4; pos
++) 
 139                         ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
]; 
 140                         par
[0] |= (((filter(pcs
->odd
) ^ oddparity(dcmd
[pos
])) & 0x01) << (7-pos
)); 
 143                 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
); 
 146                 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
); 
 149         int len 
= ReaderReceive(answer
, par
); 
 151         if (answer_parity
) *answer_parity 
= par
[0]; 
 153         if (crypted 
== CRYPT_ALL
) { 
 156                         for (pos 
= 0; pos 
< 4; pos
++) 
 157                                 res 
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
; 
 162                         for (pos 
= 0; pos 
< len
; pos
++) 
 164                                 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
]; 
 172 // mifare classic commands 
 173 int mifare_classic_auth(struct Crypto1State 
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)  
 175         return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
); 
 178 int mifare_classic_authex(struct Crypto1State 
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
, uint32_t *ntptr
, uint32_t *timing
)  
 184         uint8_t par
[1] = {0x00}; 
 186         uint32_t nt
, ntpp
; // Supplied tag nonce 
 188         uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; 
 189         uint8_t *receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 190         uint8_t *receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 192         // Transmit MIFARE_CLASSIC_AUTH 
 193         len 
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType 
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
); 
 194         if (MF_DBGLEVEL 
>= 4)   Dbprintf("rand tag nonce len: %x", len
);   
 195         if (len 
!= 4) return 1; 
 197         // "random" reader nonce: 
 203         // Save the tag nonce (nt) 
 204         nt 
= bytes_to_num(receivedAnswer
, 4); 
 206         //  ----------------------------- crypto1 create 
 208                 crypto1_destroy(pcs
); 
 210         // Init cipher with key 
 211         crypto1_create(pcs
, ui64Key
); 
 213         if (isNested 
== AUTH_NESTED
) { 
 214                 // decrypt nt with help of new key  
 215                 nt 
= crypto1_word(pcs
, nt 
^ uid
, 1) ^ nt
; 
 217                 // Load (plain) uid^nt into the cipher 
 218                 crypto1_word(pcs
, nt 
^ uid
, 0); 
 222         if (!ntptr 
&& (MF_DBGLEVEL 
>= 3)) 
 223                 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);   
 229         // Generate (encrypted) nr+parity by loading it into the cipher (Nr) 
 231         for (pos 
= 0; pos 
< 4; pos
++) 
 233                 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
]; 
 234                 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nr
[pos
])) & 0x01) << (7-pos
)); 
 237         // Skip 32 bits in pseudo random generator 
 238         nt 
= prng_successor(nt
,32); 
 241         for (pos 
= 4; pos 
< 8; pos
++) 
 243                 nt 
= prng_successor(nt
,8); 
 244                 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt 
& 0xff); 
 245                 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nt 
& 0xff)) & 0x01) << (7-pos
)); 
 248         // Transmit reader nonce and reader answer 
 249         ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
); 
 251         // Receive 4 byte tag answer 
 252         len 
= ReaderReceive(receivedAnswer
, receivedAnswerPar
); 
 255                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Authentication failed. Card timeout."); 
 259         memcpy(tmp4
, receivedAnswer
, 4); 
 260         ntpp 
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0); 
 262         if (ntpp 
!= bytes_to_num(tmp4
, 4)) { 
 263                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Authentication failed. Error card response."); 
 270 int mifare_classic_readblock(struct Crypto1State 
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)  
 276         uint8_t* receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 277         uint8_t* receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 279         // command MIFARE_CLASSIC_READBLOCK 
 280         len 
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 282                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);   
 286                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd Error: card timeout. len: %x", len
);   
 290         memcpy(bt
, receivedAnswer 
+ 16, 2); 
 291         AppendCrc14443a(receivedAnswer
, 16); 
 292         if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) { 
 293                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd CRC response error.");   
 297         memcpy(blockData
, receivedAnswer
, 16); 
 301 // mifare ultralight commands 
 302 int mifare_ultra_auth1(uint32_t uid
, uint8_t *blockData
){ 
 305         uint8_t *receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 306         uint8_t *receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 308         len 
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar 
,NULL
); 
 310                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 311                         Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 317         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) { 
 318                 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
 319                         receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4], 
 320                         receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9], 
 323         memcpy(blockData
, receivedAnswer
, 11); 
 327 int mifare_ultra_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){ 
 330         uint8_t *receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 331         uint8_t *receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 333         len 
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, key
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 335                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 336                         Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 342         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) { 
 343                 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
 344                         receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4], 
 345                         receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9], 
 348         memcpy(blockData
, receivedAnswer
, 11); 
 352 int mifare_ultra_readblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
) 
 356         uint8_t* receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 357         uint8_t* receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 360         // command MIFARE_CLASSIC_READBLOCK 
 361         len 
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 363                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 364                         Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 368                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 369                         Dbprintf("Cmd Error: card timeout. len: %x", len
); 
 373         memcpy(bt
, receivedAnswer 
+ 16, 2); 
 374         AppendCrc14443a(receivedAnswer
, 16); 
 375         if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) { 
 376                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 377                         Dbprintf("Cmd CRC response error."); 
 381         memcpy(blockData
, receivedAnswer
, 14); 
 386 int mifare_classic_writeblock(struct Crypto1State 
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)  
 391         uint8_t par
[3] = {0};           // enough for 18 Bytes to send 
 394         uint8_t d_block
[18], d_block_enc
[18]; 
 395         uint8_t* receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 396         uint8_t* receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 398         // command MIFARE_CLASSIC_WRITEBLOCK 
 399         len 
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 401         if ((len 
!= 1) || (receivedAnswer
[0] != 0x0A)) {   //  0x0a - ACK 
 402                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);   
 406         memcpy(d_block
, blockData
, 16); 
 407         AppendCrc14443a(d_block
, 16); 
 410         for (pos 
= 0; pos 
< 18; pos
++) 
 412                 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
]; 
 413                 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007))); 
 416         ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
); 
 418         // Receive the response 
 419         len 
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);  
 422         for (i 
= 0; i 
< 4; i
++) 
 423                 res 
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
; 
 425         if ((len 
!= 1) || (res 
!= 0x0A)) { 
 426                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd send data2 Error: %02x", res
);   
 433 int mifare_ultra_writeblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)  
 436     uint8_t par
[3] = {0};  // enough for 18 parity bits 
 437         uint8_t d_block
[18] = {0x00}; 
 438     uint8_t* receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 439         uint8_t* receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 441     // command MIFARE_CLASSIC_WRITEBLOCK 
 442     len 
= mifare_sendcmd_short(NULL
, true, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 444     if ((len 
!= 1) || (receivedAnswer
[0] != 0x0A)) {   //  0x0a - ACK 
 445                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 446                         Dbprintf("Cmd Addr Error: %02x", receivedAnswer
[0]);   
 450         memcpy(d_block
, blockData
, 16); 
 451     AppendCrc14443a(d_block
, 16); 
 453         ReaderTransmitPar(d_block
, sizeof(d_block
), par
, NULL
); 
 455     len 
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);     
 457         if ((len 
!= 1) || (receivedAnswer
[0] != 0x0A)) {   //  0x0a - ACK 
 458                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 459                         Dbprintf("Cmd Data Error: %02x %d", receivedAnswer
[0],len
); 
 465 int mifare_ultra_special_writeblock(uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
) 
 468         uint8_t d_block
[8] = {0x00}; 
 469     uint8_t *receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 470         uint8_t *receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 472     // command MIFARE_CLASSIC_WRITEBLOCK 
 474         memcpy(d_block
+1,blockData
,4); 
 475         AppendCrc14443a(d_block
, 6); 
 477     len 
= mifare_sendcmd_short_special(NULL
, 1, 0xA2, d_block
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 479     if (receivedAnswer
[0] != 0x0A) {   //  0x0a - ACK 
 480                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 481                         Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
); 
 487 int mifare_classic_halt(struct Crypto1State 
*pcs
, uint32_t uid
)  
 490         uint8_t *receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 491         uint8_t *receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 493         len 
= mifare_sendcmd_short(pcs
, pcs 
== NULL 
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
); 
 495                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 496                         Dbprintf("halt error. response len: %x", len
);   
 503 int mifare_ultra_halt(uint32_t uid
) 
 506         uint8_t *receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 507         uint8_t *receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 509         len 
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
); 
 511                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 512                         Dbprintf("halt error. response len: %x", len
); 
 519 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards), 
 520 // plus evtl. 8 sectors with 16 blocks each (4k cards) 
 521 uint8_t NumBlocksPerSector(uint8_t sectorNo
)  
 529 uint8_t FirstBlockOfSector(uint8_t sectorNo
)  
 534                 return 32*4 + (sectorNo 
- 32) * 16; 
 539 // work with emulator memory 
 540 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) { 
 541         uint8_t* emCARD 
= get_bigbufptr_emlcardmem(); 
 542         memcpy(emCARD 
+ blockNum 
* 16, data
, blocksCount 
* 16); 
 545 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) { 
 546         uint8_t* emCARD 
= get_bigbufptr_emlcardmem(); 
 547         memcpy(data
, emCARD 
+ blockNum 
* 16, blocksCount 
* 16); 
 550 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) { 
 551         uint8_t* emCARD 
= get_bigbufptr_emlcardmem(); 
 552         memcpy(data
, emCARD 
+ bytePtr
, byteCount
); 
 555 int emlCheckValBl(int blockNum
) { 
 556         uint8_t* emCARD 
= get_bigbufptr_emlcardmem(); 
 557         uint8_t* data 
= emCARD 
+ blockNum 
* 16; 
 559         if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) || 
 560                         (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) || 
 561                         (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) || 
 562                         (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) || 
 563                         (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) || 
 564                         (data
[12] != (data
[15] ^ 0xff)) 
 570 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) { 
 571         uint8_t* emCARD 
= get_bigbufptr_emlcardmem(); 
 572         uint8_t* data 
= emCARD 
+ blockNum 
* 16; 
 574         if (emlCheckValBl(blockNum
)) { 
 578         memcpy(blReg
, data
, 4); 
 583 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) { 
 584         uint8_t* emCARD 
= get_bigbufptr_emlcardmem(); 
 585         uint8_t* data 
= emCARD 
+ blockNum 
* 16; 
 587         memcpy(data 
+ 0, &blReg
, 4); 
 588         memcpy(data 
+ 8, &blReg
, 4); 
 589         blReg 
= blReg 
^ 0xffffffff; 
 590         memcpy(data 
+ 4, &blReg
, 4); 
 593         data
[13] = blBlock 
^ 0xff; 
 595         data
[15] = blBlock 
^ 0xff; 
 600 uint64_t emlGetKey(int sectorNum
, int keyType
) { 
 602         uint8_t* emCARD 
= get_bigbufptr_emlcardmem(); 
 604         memcpy(key
, emCARD 
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType 
* 10, 6); 
 605         return bytes_to_num(key
, 6); 
 608 void emlClearMem(void) { 
 611         const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 
 612         const uint8_t uid
[]   =   {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04}; 
 613         uint8_t* emCARD 
= get_bigbufptr_emlcardmem(); 
 615         memset(emCARD
, 0, CARD_MEMORY_SIZE
); 
 617         // fill sectors trailer data 
 618         for(b 
= 3; b 
< 256; b
<127?(b
+=4):(b
+=16)) { 
 619                 emlSetMem((uint8_t *)trailer
, b 
, 1); 
 623         emlSetMem((uint8_t *)uid
, 0, 1); 
 628 // Mifare desfire commands 
 629 int mifare_sendcmd_special(struct Crypto1State 
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t* data
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
) 
 631     uint8_t dcmd
[5] = {0x00}; 
 633     memcpy(dcmd
+1,data
,2); 
 634         AppendCrc14443a(dcmd
, 3); 
 636         ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
); 
 637         int len 
= ReaderReceive(answer
, answer_parity
); 
 639                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)  
 640                         Dbprintf("Authentication failed. Card timeout."); 
 646 int mifare_sendcmd_special2(struct Crypto1State 
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t* data
, uint8_t* answer
,uint8_t *answer_parity
, uint32_t *timing
) 
 648     uint8_t dcmd
[20] = {0x00}; 
 650     memcpy(dcmd
+1,data
,17); 
 651         AppendCrc14443a(dcmd
, 18); 
 653         ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
); 
 654         int len 
= ReaderReceive(answer
, answer_parity
); 
 656         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 657                         Dbprintf("Authentication failed. Card timeout."); 
 663 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){ 
 666         // load key, keynumber 
 667         uint8_t data
[2]={0x0a, 0x00}; 
 668         uint8_t* receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 669         uint8_t *receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 671         len 
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
); 
 673                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 674                         Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 679                 if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
)     { 
 680                         Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
 681                                 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4], 
 682                                 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9], 
 683                                 receivedAnswer
[10],receivedAnswer
[11]); 
 685                         memcpy(blockData
, receivedAnswer
, 12); 
 691 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){ 
 694         uint8_t data
[17] = {0x00}; 
 696         memcpy(data
+1,key
,16); 
 698         uint8_t* receivedAnswer 
= get_bigbufptr_recvrespbuf(); 
 699         uint8_t *receivedAnswerPar 
= receivedAnswer 
+ MAX_FRAME_SIZE
; 
 701         len 
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar 
,NULL
); 
 703         if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) { 
 704                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 705                         Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]); 
 710                 if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) { 
 711                         Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
 712                                 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4], 
 713                                 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9], 
 714                                 receivedAnswer
[10],receivedAnswer
[11]); 
 716                 memcpy(blockData
, receivedAnswer
, 12);