]>
git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
0469364c3f6cefe099db0297e771bea5b693add4
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"
23 int MF_DBGLEVEL
= MF_DBG_ALL
;
26 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
){
31 for (i
= 0; i
< len
; i
++)
32 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
35 for (i
= 0; i
< 4; i
++)
36 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], i
)) << i
;
43 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) {
48 for (i
= 0; i
< len
; i
++) {
50 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
53 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity(bt
)) & 0x01)<<(7-(i
&0x0007)));
58 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
62 for (i
= 0; i
< 4; i
++)
63 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
;
69 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
)
71 return mifare_sendcmd_shortex(pcs
, crypted
, cmd
, data
, answer
, answer_parity
, timing
);
74 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
)
83 AppendCrc14443a(dcmd
, 6);
84 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
85 int len
= ReaderReceive(answer
, answer_parity
);
87 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
93 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
)
98 memcpy(dcmd
+1,data
,16);
99 AppendCrc14443a(dcmd
, 17);
101 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
102 len
= ReaderReceive(answer
, answer_parity
);
104 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed. Card timeout.");
105 len
= ReaderReceive(answer
,answer_parity
);
108 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("NAK - Authentication failed.");
114 int mifare_sendcmd_short_mfuev1auth(struct Crypto1State
*pcs
, uint8_t crypted
, uint8_t cmd
, uint8_t *data
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
)
119 memcpy(dcmd
+1,data
,4);
120 AppendCrc14443a(dcmd
, 5);
122 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
123 len
= ReaderReceive(answer
, answer_parity
);
125 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Authentication failed. Card timeout.");
126 len
= ReaderReceive(answer
,answer_parity
);
129 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("NAK - Authentication failed.");
135 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
)
137 uint8_t dcmd
[4], ecmd
[4];
139 uint8_t par
[1]; // 1 Byte parity is enough here
142 AppendCrc14443a(dcmd
, 2);
144 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
148 for (pos
= 0; pos
< 4; pos
++)
150 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
151 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(dcmd
[pos
])) & 0x01) << (7-pos
));
154 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
157 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
160 int len
= ReaderReceive(answer
, par
);
162 if (answer_parity
) *answer_parity
= par
[0];
164 if (crypted
== CRYPT_ALL
) {
167 for (pos
= 0; pos
< 4; pos
++)
168 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
173 for (pos
= 0; pos
< len
; pos
++)
175 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
183 // mifare classic commands
184 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)
186 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
189 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
)
195 uint8_t par
[1] = {0x00};
197 uint32_t nt
, ntpp
; // Supplied tag nonce
199 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
200 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
201 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
203 // Transmit MIFARE_CLASSIC_AUTH
204 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
205 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
206 if (len
!= 4) return 1;
208 // "random" reader nonce:
214 // Save the tag nonce (nt)
215 nt
= bytes_to_num(receivedAnswer
, 4);
217 // ----------------------------- crypto1 create
219 crypto1_destroy(pcs
);
221 // Init cipher with key
222 crypto1_create(pcs
, ui64Key
);
224 if (isNested
== AUTH_NESTED
) {
225 // decrypt nt with help of new key
226 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
228 // Load (plain) uid^nt into the cipher
229 crypto1_word(pcs
, nt
^ uid
, 0);
233 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
234 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
240 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
242 for (pos
= 0; pos
< 4; pos
++)
244 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
245 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nr
[pos
])) & 0x01) << (7-pos
));
248 // Skip 32 bits in pseudo random generator
249 nt
= prng_successor(nt
,32);
252 for (pos
= 4; pos
< 8; pos
++)
254 nt
= prng_successor(nt
,8);
255 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
256 par
[0] |= (((filter(pcs
->odd
) ^ oddparity(nt
& 0xff)) & 0x01) << (7-pos
));
259 // Transmit reader nonce and reader answer
260 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
262 // Receive 4 byte tag answer
263 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
266 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
270 memcpy(tmp4
, receivedAnswer
, 4);
271 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
273 if (ntpp
!= bytes_to_num(tmp4
, 4)) {
274 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
281 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
287 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
288 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
290 // command MIFARE_CLASSIC_READBLOCK
291 len
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
293 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
297 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
301 memcpy(bt
, receivedAnswer
+ 16, 2);
302 AppendCrc14443a(receivedAnswer
, 16);
303 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
304 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
308 memcpy(blockData
, receivedAnswer
, 16);
312 // mifare ultralight commands
313 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
318 uint8_t key
[4] = {0x00};
319 memcpy(key
, keybytes
, 4);
321 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
322 len
= mifare_sendcmd_short_mfuev1auth(NULL
, 0, 0x1B, key
, resp
, respPar
, NULL
);
324 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
329 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
330 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
332 memcpy(pack
, resp
, 4);
336 int mifare_ultra_auth(uint8_t *keybytes
){
340 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
341 uint8_t random_b
[8] = {0x00};
342 uint8_t enc_random_b
[8] = {0x00};
343 uint8_t rnd_ab
[16] = {0x00};
344 uint8_t IV
[8] = {0x00};
345 uint8_t key
[16] = {0x00};
346 memcpy(key
, keybytes
, 16);
349 uint8_t resp
[19] = {0x00};
350 uint8_t respPar
[3] = {0,0,0};
352 // REQUEST AUTHENTICATION
353 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, resp
, respPar
,NULL
);
355 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
361 memcpy(enc_random_b
,resp
+1,8);
364 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
366 memcpy(rnd_ab
,random_a
,8);
367 memcpy(rnd_ab
+8,random_b
,8);
369 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
370 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
371 enc_random_b
[0],enc_random_b
[1],enc_random_b
[2],enc_random_b
[3],enc_random_b
[4],enc_random_b
[5],enc_random_b
[6],enc_random_b
[7]);
373 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
374 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
376 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
377 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
379 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
380 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
383 // encrypt out, in, length, key, iv
384 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
386 len
= mifare_sendcmd_short_mfucauth(NULL
, 1, 0xAF, rnd_ab
, resp
, respPar
, NULL
);
388 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
393 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
394 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
395 memcpy(enc_resp
, resp
+1, 8);
397 // decrypt out, in, length, key, iv
398 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
399 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
400 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
404 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
405 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
406 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
407 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
409 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
410 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
411 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
413 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
414 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
415 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
417 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
418 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
419 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
424 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
)
428 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
429 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
431 len
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
433 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
437 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
441 memcpy(bt
, receivedAnswer
+ 16, 2);
442 AppendCrc14443a(receivedAnswer
, 16);
443 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
444 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
448 memcpy(blockData
, receivedAnswer
, 14);
452 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
457 uint8_t par
[3] = {0}; // enough for 18 Bytes to send
460 uint8_t d_block
[18], d_block_enc
[18];
461 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
462 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
464 // command MIFARE_CLASSIC_WRITEBLOCK
465 len
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
467 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
468 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
472 memcpy(d_block
, blockData
, 16);
473 AppendCrc14443a(d_block
, 16);
476 for (pos
= 0; pos
< 18; pos
++)
478 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
479 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
482 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
484 // Receive the response
485 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
488 for (i
= 0; i
< 4; i
++)
489 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
491 if ((len
!= 1) || (res
!= 0x0A)) {
492 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
499 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)
502 uint8_t par
[3] = {0}; // enough for 18 parity bits
503 uint8_t d_block
[18] = {0x00};
504 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
505 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
507 // command MIFARE_CLASSIC_WRITEBLOCK
508 len
= mifare_sendcmd_short(NULL
, true, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
510 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
511 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
512 Dbprintf("Cmd Addr Error: %02x", receivedAnswer
[0]);
516 memcpy(d_block
, blockData
, 16);
517 AppendCrc14443a(d_block
, 16);
519 ReaderTransmitPar(d_block
, sizeof(d_block
), par
, NULL
);
521 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
523 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
524 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
525 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer
[0],len
);
531 int mifare_ultra_special_writeblock(uint8_t blockNo
, uint8_t *blockData
)
534 uint8_t d_block
[8] = {0x00};
535 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
536 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
538 // command MIFARE_CLASSIC_WRITEBLOCK
540 memcpy(d_block
+1,blockData
,4);
541 AppendCrc14443a(d_block
, 6);
543 len
= mifare_sendcmd_short_special(NULL
, 1, 0xA2, d_block
, receivedAnswer
, receivedAnswerPar
, NULL
);
545 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
546 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
547 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
553 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
)
556 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
557 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
559 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
561 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
562 Dbprintf("halt error. response len: %x", len
);
569 int mifare_ultra_halt()
572 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
573 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
575 len
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
577 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
578 Dbprintf("halt error. response len: %x", len
);
585 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
586 // plus evtl. 8 sectors with 16 blocks each (4k cards)
587 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
595 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
600 return 32*4 + (sectorNo
- 32) * 16;
605 // work with emulator memory
606 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
607 uint8_t* emCARD
= BigBuf_get_EM_addr();
608 memcpy(emCARD
+ blockNum
* 16, data
, blocksCount
* 16);
611 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
612 uint8_t* emCARD
= BigBuf_get_EM_addr();
613 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
616 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
617 uint8_t* emCARD
= BigBuf_get_EM_addr();
618 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
621 int emlCheckValBl(int blockNum
) {
622 uint8_t* emCARD
= BigBuf_get_EM_addr();
623 uint8_t* data
= emCARD
+ blockNum
* 16;
625 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
626 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
627 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
628 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
629 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
630 (data
[12] != (data
[15] ^ 0xff))
636 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
637 uint8_t* emCARD
= BigBuf_get_EM_addr();
638 uint8_t* data
= emCARD
+ blockNum
* 16;
640 if (emlCheckValBl(blockNum
)) {
644 memcpy(blReg
, data
, 4);
649 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
650 uint8_t* emCARD
= BigBuf_get_EM_addr();
651 uint8_t* data
= emCARD
+ blockNum
* 16;
653 memcpy(data
+ 0, &blReg
, 4);
654 memcpy(data
+ 8, &blReg
, 4);
655 blReg
= blReg
^ 0xffffffff;
656 memcpy(data
+ 4, &blReg
, 4);
659 data
[13] = blBlock
^ 0xff;
661 data
[15] = blBlock
^ 0xff;
666 uint64_t emlGetKey(int sectorNum
, int keyType
) {
668 uint8_t* emCARD
= BigBuf_get_EM_addr();
670 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
671 return bytes_to_num(key
, 6);
674 void emlClearMem(void) {
677 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
678 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
679 uint8_t* emCARD
= BigBuf_get_EM_addr();
681 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
683 // fill sectors trailer data
684 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
685 emlSetMem((uint8_t *)trailer
, b
, 1);
689 emlSetMem((uint8_t *)uid
, 0, 1);
694 // Mifare desfire commands
695 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
)
697 uint8_t dcmd
[5] = {0x00};
699 memcpy(dcmd
+1,data
,2);
700 AppendCrc14443a(dcmd
, 3);
702 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
703 int len
= ReaderReceive(answer
, answer_parity
);
705 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
706 Dbprintf("Authentication failed. Card timeout.");
712 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
)
714 uint8_t dcmd
[20] = {0x00};
716 memcpy(dcmd
+1,data
,17);
717 AppendCrc14443a(dcmd
, 18);
719 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
720 int len
= ReaderReceive(answer
, answer_parity
);
722 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
723 Dbprintf("Authentication failed. Card timeout.");
729 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
732 // load key, keynumber
733 uint8_t data
[2]={0x0a, 0x00};
734 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
735 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
737 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
739 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
740 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
745 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
746 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
747 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
748 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
749 receivedAnswer
[10],receivedAnswer
[11]);
751 memcpy(blockData
, receivedAnswer
, 12);
757 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
760 uint8_t data
[17] = {0x00};
762 memcpy(data
+1,key
,16);
764 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
765 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
767 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
769 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
770 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
771 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
776 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
777 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
778 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
779 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
780 receivedAnswer
[10],receivedAnswer
[11]);
782 memcpy(blockData
, receivedAnswer
, 12);