]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
8fa5e498062e57f5e5e8795687c36e0c1a0f5a94
   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 void mf_crypto1_decrypt(struct Crypto1State 
*pcs
, uint8_t *data
, int len
){ 
  30                 for (i 
= 0; i 
< len
; i
++) 
  31                         data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
]; 
  34                 for (i 
= 0; i 
< 4; i
++) 
  35                         bt 
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], i
)) << i
; 
  42 void mf_crypto1_encrypt(struct Crypto1State 
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) { 
  47         for (i 
= 0; i 
< len
; i
++) { 
  49                 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
]; 
  52                 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity(bt
)) & 0x01)<<(7-(i
&0x0007))); 
  57 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State 
*pcs
, uint8_t data
) { 
  61         for (i 
= 0; i 
< 4; i
++) 
  62                 bt 
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
; 
  68 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
) 
  70         return mifare_sendcmd_shortex(pcs
, crypted
, cmd
, data
, answer
, answer_parity
, timing
); 
  73 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
) 
  82         AppendCrc14443a(dcmd
, 6); 
  83         ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
); 
  84         int len 
= ReaderReceive(answer
, answer_parity
); 
  86                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Authentication failed. Card timeout."); 
  92 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
) 
  97     memcpy(dcmd
+1,data
,16); 
  98         AppendCrc14443a(dcmd
, 17); 
 100         ReaderTransmit(dcmd
, sizeof(dcmd
), timing
); 
 101         len 
= ReaderReceive(answer
, answer_parity
); 
 103         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)   Dbprintf("Authentication failed. Card timeout."); 
 104         len 
= ReaderReceive(answer
,answer_parity
); 
 107                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)   Dbprintf("NAK - Authentication failed."); 
 113 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
) 
 115         uint8_t dcmd
[4], ecmd
[4]; 
 117         uint8_t par
[1];                 // 1 Byte parity is enough here 
 120         AppendCrc14443a(dcmd
, 2); 
 122         memcpy(ecmd
, dcmd
, sizeof(dcmd
)); 
 126                 for (pos 
= 0; pos 
< 4; pos
++) 
 128                         ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
]; 
 129                         par
[0] |= (((filter(pcs
->odd
) ^ oddparity(dcmd
[pos
])) & 0x01) << (7-pos
)); 
 132                 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
); 
 135                 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
); 
 138         int len 
= ReaderReceive(answer
, par
); 
 140         if (answer_parity
) *answer_parity 
= par
[0]; 
 142         if (crypted 
== CRYPT_ALL
) { 
 145                         for (pos 
= 0; pos 
< 4; pos
++) 
 146                                 res 
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
; 
 151                         for (pos 
= 0; pos 
< len
; pos
++) 
 153                                 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
]; 
 161 // mifare classic commands 
 162 int mifare_classic_auth(struct Crypto1State 
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)  
 164         return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
); 
 167 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
)  
 173         uint8_t par
[1] = {0x00}; 
 175         uint32_t nt
, ntpp
; // Supplied tag nonce 
 177         uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; 
 178         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
 179         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
 181         // Transmit MIFARE_CLASSIC_AUTH 
 182         len 
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType 
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
); 
 183         if (MF_DBGLEVEL 
>= 4)   Dbprintf("rand tag nonce len: %x", len
);   
 184         if (len 
!= 4) return 1; 
 186         // "random" reader nonce: 
 192         // Save the tag nonce (nt) 
 193         nt 
= bytes_to_num(receivedAnswer
, 4); 
 195         //  ----------------------------- crypto1 create 
 197                 crypto1_destroy(pcs
); 
 199         // Init cipher with key 
 200         crypto1_create(pcs
, ui64Key
); 
 202         if (isNested 
== AUTH_NESTED
) { 
 203                 // decrypt nt with help of new key  
 204                 nt 
= crypto1_word(pcs
, nt 
^ uid
, 1) ^ nt
; 
 206                 // Load (plain) uid^nt into the cipher 
 207                 crypto1_word(pcs
, nt 
^ uid
, 0); 
 211         if (!ntptr 
&& (MF_DBGLEVEL 
>= 3)) 
 212                 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);   
 218         // Generate (encrypted) nr+parity by loading it into the cipher (Nr) 
 220         for (pos 
= 0; pos 
< 4; pos
++) 
 222                 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
]; 
 223                 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nr
[pos
])) & 0x01) << (7-pos
)); 
 226         // Skip 32 bits in pseudo random generator 
 227         nt 
= prng_successor(nt
,32); 
 230         for (pos 
= 4; pos 
< 8; pos
++) 
 232                 nt 
= prng_successor(nt
,8); 
 233                 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt 
& 0xff); 
 234                 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nt 
& 0xff)) & 0x01) << (7-pos
)); 
 237         // Transmit reader nonce and reader answer 
 238         ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
); 
 240         // Receive 4 byte tag answer 
 241         len 
= ReaderReceive(receivedAnswer
, receivedAnswerPar
); 
 244                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Authentication failed. Card timeout."); 
 248         memcpy(tmp4
, receivedAnswer
, 4); 
 249         ntpp 
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0); 
 251         if (ntpp 
!= bytes_to_num(tmp4
, 4)) { 
 252                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Authentication failed. Error card response."); 
 259 int mifare_classic_readblock(struct Crypto1State 
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)  
 265         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
 266         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
 268         // command MIFARE_CLASSIC_READBLOCK 
 269         len 
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 271                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);   
 275                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd Error: card timeout. len: %x", len
);   
 279         memcpy(bt
, receivedAnswer 
+ 16, 2); 
 280         AppendCrc14443a(receivedAnswer
, 16); 
 281         if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) { 
 282                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd CRC response error.");   
 286         memcpy(blockData
, receivedAnswer
, 16); 
 290 // mifare ultralight commands 
 291 int mifare_ultra_auth1(uint8_t *blockData
){ 
 294         uint8_t receivedAnswer
[MAX_FRAME_SIZE
]; 
 295         uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
]; 
 297         len 
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, receivedAnswer
,receivedAnswerPar 
,NULL
); 
 299                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 303         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) { 
 304                 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
 305                         receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4], 
 306                         receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9], 
 309         memcpy(blockData
, receivedAnswer
, 11); 
 313 int mifare_ultra_auth2(uint8_t *key
, uint8_t *blockData
){ 
 316         uint8_t receivedAnswer
[MAX_FRAME_SIZE
]; 
 317         uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
]; 
 319         len 
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, key
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 321                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 325         if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) { 
 326                 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
 327                         receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4], 
 328                         receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9], 
 331         memcpy(blockData
, receivedAnswer
, 11); 
 335 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
) 
 339         uint8_t receivedAnswer
[MAX_FRAME_SIZE
]; 
 340         uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
]; 
 342         len 
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 344                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 348                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
); 
 352         memcpy(bt
, receivedAnswer 
+ 16, 2); 
 353         AppendCrc14443a(receivedAnswer
, 16); 
 354         if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) { 
 355                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error."); 
 359         memcpy(blockData
, receivedAnswer
, 14); 
 364 int mifare_classic_writeblock(struct Crypto1State 
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)  
 369         uint8_t par
[3] = {0};           // enough for 18 Bytes to send 
 372         uint8_t d_block
[18], d_block_enc
[18]; 
 373         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
 374         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
 376         // command MIFARE_CLASSIC_WRITEBLOCK 
 377         len 
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 379         if ((len 
!= 1) || (receivedAnswer
[0] != 0x0A)) {   //  0x0a - ACK 
 380                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);   
 384         memcpy(d_block
, blockData
, 16); 
 385         AppendCrc14443a(d_block
, 16); 
 388         for (pos 
= 0; pos 
< 18; pos
++) 
 390                 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
]; 
 391                 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007))); 
 394         ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
); 
 396         // Receive the response 
 397         len 
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);  
 400         for (i 
= 0; i 
< 4; i
++) 
 401                 res 
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
; 
 403         if ((len 
!= 1) || (res 
!= 0x0A)) { 
 404                 if (MF_DBGLEVEL 
>= 1)   Dbprintf("Cmd send data2 Error: %02x", res
);   
 411 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)  
 414     uint8_t par
[3] = {0};  // enough for 18 parity bits 
 415         uint8_t d_block
[18] = {0x00}; 
 416         uint8_t receivedAnswer
[MAX_FRAME_SIZE
]; 
 417         uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
]; 
 419     // command MIFARE_CLASSIC_WRITEBLOCK 
 420     len 
= mifare_sendcmd_short(NULL
, true, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 422     if ((len 
!= 1) || (receivedAnswer
[0] != 0x0A)) {   //  0x0a - ACK 
 423                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 424                         Dbprintf("Cmd Addr Error: %02x", receivedAnswer
[0]);   
 428         memcpy(d_block
, blockData
, 16); 
 429     AppendCrc14443a(d_block
, 16); 
 431         ReaderTransmitPar(d_block
, sizeof(d_block
), par
, NULL
); 
 433     len 
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);     
 435         if ((len 
!= 1) || (receivedAnswer
[0] != 0x0A)) {   //  0x0a - ACK 
 436                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 437                         Dbprintf("Cmd Data Error: %02x %d", receivedAnswer
[0],len
); 
 443 int mifare_ultra_special_writeblock(uint8_t blockNo
, uint8_t *blockData
) 
 446         uint8_t d_block
[8] = {0x00}; 
 447         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
 448         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
 450     // command MIFARE_CLASSIC_WRITEBLOCK 
 452         memcpy(d_block
+1,blockData
,4); 
 453         AppendCrc14443a(d_block
, 6); 
 455     len 
= mifare_sendcmd_short_special(NULL
, 1, 0xA2, d_block
, receivedAnswer
, receivedAnswerPar
, NULL
); 
 457     if (receivedAnswer
[0] != 0x0A) {   //  0x0a - ACK 
 458                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 459                         Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
); 
 465 int mifare_classic_halt(struct Crypto1State 
*pcs
, uint32_t uid
)  
 468         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
 469         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
 471         len 
= mifare_sendcmd_short(pcs
, pcs 
== NULL 
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
); 
 473                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 474                         Dbprintf("halt error. response len: %x", len
);   
 481 int mifare_ultra_halt() 
 484         uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
]; 
 485         uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
]; 
 487         len 
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
); 
 489                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 490                         Dbprintf("halt error. response len: %x", len
); 
 497 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards), 
 498 // plus evtl. 8 sectors with 16 blocks each (4k cards) 
 499 uint8_t NumBlocksPerSector(uint8_t sectorNo
)  
 507 uint8_t FirstBlockOfSector(uint8_t sectorNo
)  
 512                 return 32*4 + (sectorNo 
- 32) * 16; 
 517 // work with emulator memory 
 518 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) { 
 519         uint8_t* emCARD 
= BigBuf_get_EM_addr(); 
 520         memcpy(emCARD 
+ blockNum 
* 16, data
, blocksCount 
* 16); 
 523 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) { 
 524         uint8_t* emCARD 
= BigBuf_get_EM_addr(); 
 525         memcpy(data
, emCARD 
+ blockNum 
* 16, blocksCount 
* 16); 
 528 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) { 
 529         uint8_t* emCARD 
= BigBuf_get_EM_addr(); 
 530         memcpy(data
, emCARD 
+ bytePtr
, byteCount
); 
 533 int emlCheckValBl(int blockNum
) { 
 534         uint8_t* emCARD 
= BigBuf_get_EM_addr(); 
 535         uint8_t* data 
= emCARD 
+ blockNum 
* 16; 
 537         if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) || 
 538                         (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) || 
 539                         (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) || 
 540                         (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) || 
 541                         (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) || 
 542                         (data
[12] != (data
[15] ^ 0xff)) 
 548 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) { 
 549         uint8_t* emCARD 
= BigBuf_get_EM_addr(); 
 550         uint8_t* data 
= emCARD 
+ blockNum 
* 16; 
 552         if (emlCheckValBl(blockNum
)) { 
 556         memcpy(blReg
, data
, 4); 
 561 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) { 
 562         uint8_t* emCARD 
= BigBuf_get_EM_addr(); 
 563         uint8_t* data 
= emCARD 
+ blockNum 
* 16; 
 565         memcpy(data 
+ 0, &blReg
, 4); 
 566         memcpy(data 
+ 8, &blReg
, 4); 
 567         blReg 
= blReg 
^ 0xffffffff; 
 568         memcpy(data 
+ 4, &blReg
, 4); 
 571         data
[13] = blBlock 
^ 0xff; 
 573         data
[15] = blBlock 
^ 0xff; 
 578 uint64_t emlGetKey(int sectorNum
, int keyType
) { 
 580         uint8_t* emCARD 
= BigBuf_get_EM_addr(); 
 582         memcpy(key
, emCARD 
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType 
* 10, 6); 
 583         return bytes_to_num(key
, 6); 
 586 void emlClearMem(void) { 
 589         const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 
 590         const uint8_t uid
[]   =   {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04}; 
 591         uint8_t* emCARD 
= BigBuf_get_EM_addr(); 
 593         memset(emCARD
, 0, CARD_MEMORY_SIZE
); 
 595         // fill sectors trailer data 
 596         for(b 
= 3; b 
< 256; b
<127?(b
+=4):(b
+=16)) { 
 597                 emlSetMem((uint8_t *)trailer
, b 
, 1); 
 601         emlSetMem((uint8_t *)uid
, 0, 1); 
 606 // Mifare desfire commands 
 607 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
) 
 609     uint8_t dcmd
[5] = {0x00}; 
 611     memcpy(dcmd
+1,data
,2); 
 612         AppendCrc14443a(dcmd
, 3); 
 614         ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
); 
 615         int len 
= ReaderReceive(answer
, answer_parity
); 
 617                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
)  
 618                         Dbprintf("Authentication failed. Card timeout."); 
 624 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
) 
 626     uint8_t dcmd
[20] = {0x00}; 
 628     memcpy(dcmd
+1,data
,17); 
 629         AppendCrc14443a(dcmd
, 18); 
 631         ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
); 
 632         int len 
= ReaderReceive(answer
, answer_parity
); 
 634         if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 635                         Dbprintf("Authentication failed. Card timeout."); 
 641 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){ 
 644         // load key, keynumber 
 645         uint8_t data
[2]={0x0a, 0x00}; 
 646         uint8_t receivedAnswer
[MAX_FRAME_SIZE
]; 
 647         uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
]; 
 649         len 
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
); 
 651                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 652                         Dbprintf("Cmd Error: %02x", receivedAnswer
[0]); 
 657                 if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
)     { 
 658                         Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
 659                                 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4], 
 660                                 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9], 
 661                                 receivedAnswer
[10],receivedAnswer
[11]); 
 663                         memcpy(blockData
, receivedAnswer
, 12); 
 669 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){ 
 672         uint8_t data
[17] = {0x00}; 
 674         memcpy(data
+1,key
,16); 
 676         uint8_t receivedAnswer
[MAX_FRAME_SIZE
]; 
 677         uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
]; 
 679         len 
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar 
,NULL
); 
 681         if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) { 
 682                 if (MF_DBGLEVEL 
>= MF_DBG_ERROR
) 
 683                         Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]); 
 688                 if (MF_DBGLEVEL 
>= MF_DBG_EXTENDED
) { 
 689                         Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", 
 690                                 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4], 
 691                                 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9], 
 692                                 receivedAnswer
[10],receivedAnswer
[11]); 
 694                 memcpy(blockData
, receivedAnswer
, 12);