]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
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"
24 int MF_DBGLEVEL
= MF_DBG_ALL
;
27 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
){
32 for (i
= 0; i
< len
; i
++)
33 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
35 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], 0)) << 0;
36 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], 1)) << 1;
37 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], 2)) << 2;
38 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], 3)) << 3;
44 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) {
49 for (i
= 0; i
< len
; i
++) {
51 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
52 if ( ( i
& 0x0007 ) == 0)
54 par
[ i
>> 3 ] |= (((filter(pcs
->odd
) ^ oddparity8(bt
)) & 0x01)<<(7-(i
&0x0007)));
58 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
60 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, 0)) << 0;
61 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, 1)) << 1;
62 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, 2)) << 2;
63 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, 3)) << 3;
67 // send X byte basic commands
68 int mifare_sendcmd(uint8_t cmd
, uint8_t* data
, uint8_t data_size
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
) {
69 uint8_t dcmd
[data_size
+3];
71 memcpy(dcmd
+1, data
, data_size
);
72 AppendCrc14443a(dcmd
, data_size
+1);
73 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
74 int len
= ReaderReceive(answer
, answer_parity
);
76 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
77 Dbprintf("%02X Cmd failed. Card timeout.", cmd
);
78 len
= ReaderReceive(answer
,answer_parity
);
83 // send 2 byte commands
84 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
) {
86 uint8_t dcmd
[4] = {cmd
, data
, 0x00, 0x00};
87 uint8_t ecmd
[4] = {0x00, 0x00, 0x00, 0x00};
88 uint8_t par
[1] = {0x00}; // 1 Byte parity is enough here
89 AppendCrc14443a(dcmd
, 2);
90 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
94 for (pos
= 0; pos
< 4; pos
++) {
95 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
96 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(dcmd
[pos
])) & 0x01) << (7-pos
));
98 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
100 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
103 int len
= ReaderReceive(answer
, par
);
105 if (answer_parity
) *answer_parity
= par
[0];
107 if (crypted
== CRYPT_ALL
) {
110 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 0)) << 0;
111 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 1)) << 1;
112 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 2)) << 2;
113 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 3)) << 3;
116 for (pos
= 0; pos
< len
; pos
++)
117 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
123 // mifare classic commands
124 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
) {
125 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
128 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
) {
131 uint8_t par
[1] = {0x00};
133 // "random" reader nonce:
134 //byte_t nr[4] = {0x55, 0x41, 0x49, 0x92};
135 byte_t nr
[4] = {0x01, 0x01, 0x01, 0x01};
137 uint32_t nt
, ntpp
; // Supplied tag nonce
139 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
140 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
141 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
143 // Transmit MIFARE_CLASSIC_AUTH
144 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
145 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
146 if (len
!= 4) return 1;
148 // Save the tag nonce (nt)
149 nt
= bytes_to_num(receivedAnswer
, 4);
151 // ----------------------------- crypto1 create
153 crypto1_destroy(pcs
);
155 // Init cipher with key
156 crypto1_create(pcs
, ui64Key
);
158 if (isNested
== AUTH_NESTED
) {
159 // decrypt nt with help of new key
160 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
162 // Load (plain) uid^nt into the cipher
163 crypto1_word(pcs
, nt
^ uid
, 0);
167 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
168 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
174 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
176 for (pos
= 0; pos
< 4; pos
++) {
177 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
178 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nr
[pos
])) & 0x01) << (7-pos
));
181 // Skip 32 bits in pseudo random generator
182 nt
= prng_successor(nt
, 32);
185 for (pos
= 4; pos
< 8; pos
++) {
186 nt
= prng_successor(nt
,8);
187 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
188 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
& 0xff)) & 0x01) << (7-pos
));
191 // Transmit reader nonce and reader answer
192 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
194 // Receive 4 byte tag answer
195 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
197 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
201 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
203 if (ntpp
!= bytes_to_num(receivedAnswer
, 4)) {
204 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
210 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
) {
213 uint8_t bt
[2] = {0x00, 0x00};
214 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
215 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
217 len
= mifare_sendcmd_short(pcs
, 1, ISO14443A_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
219 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
223 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
227 memcpy(bt
, receivedAnswer
+ 16, 2);
228 AppendCrc14443a(receivedAnswer
, 16);
229 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
230 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
234 memcpy(blockData
, receivedAnswer
, 16);
238 // mifare ultralight commands
239 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
242 uint8_t resp
[4] = {0x00, 0x00, 0x00, 0x00};
243 uint8_t respPar
[1] = {0x00};
244 uint8_t key
[4] = {0x00, 0x00, 0x00, 0x00};
245 memcpy(key
, keybytes
, 4);
247 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
248 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
250 len
= mifare_sendcmd(0x1B, key
, sizeof(key
), resp
, respPar
, NULL
);
253 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
257 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
258 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
260 memcpy(pack
, resp
, 4);
264 int mifare_ultra_auth(uint8_t *keybytes
){
267 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
268 uint8_t random_b
[8] = {0x00};
269 uint8_t enc_random_b
[8] = {0x00};
270 uint8_t rnd_ab
[16] = {0x00};
271 uint8_t IV
[8] = {0x00};
272 uint8_t key
[16] = {0x00};
273 memcpy(key
, keybytes
, 16);
276 uint8_t resp
[19] = {0x00};
277 uint8_t respPar
[3] = {0,0,0};
279 // REQUEST AUTHENTICATION
280 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, resp
, respPar
,NULL
);
282 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
287 memcpy(enc_random_b
,resp
+1,8);
290 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
292 memcpy(rnd_ab
,random_a
,8);
293 memcpy(rnd_ab
+8,random_b
,8);
295 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
296 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
297 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]);
299 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
300 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
302 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
303 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
305 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
306 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
309 // encrypt out, in, length, key, iv
310 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
312 len
= mifare_sendcmd(0xAF, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
314 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
318 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
319 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
320 memcpy(enc_resp
, resp
+1, 8);
322 // decrypt out, in, length, key, iv
323 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
324 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
325 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
329 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
330 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
331 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
332 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
334 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
335 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
336 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
338 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
339 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
340 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
342 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
343 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
344 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
349 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
) {
351 uint8_t bt
[2] = {0x00, 0x00};
352 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
353 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
355 len
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
357 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
361 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
365 memcpy(bt
, receivedAnswer
+ 16, 2);
366 AppendCrc14443a(receivedAnswer
, 16);
367 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
368 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
372 memcpy(blockData
, receivedAnswer
, 14);
376 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
) {
380 uint8_t par
[3] = {0x00, 0x00, 0x00}; // enough for 18 Bytes to send
383 uint8_t d_block
[18], d_block_enc
[18];
384 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
385 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
387 // command MIFARE_CLASSIC_WRITEBLOCK
388 len
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
390 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
391 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
395 memcpy(d_block
, blockData
, 16);
396 AppendCrc14443a(d_block
, 16);
399 for (pos
= 0; pos
< 18; pos
++) {
400 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
401 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
404 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
406 // Receive the response
407 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
410 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 0)) << 0;
411 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 1)) << 1;
412 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 2)) << 2;
413 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 3)) << 3;
415 if ((len
!= 1) || (res
!= 0x0A)) {
416 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
422 /* // command not needed, but left for future testing
423 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) {
425 uint8_t par[3] = {0}; // enough for 18 parity bits
426 uint8_t d_block[18] = {0x00};
427 uint8_t receivedAnswer[MAX_FRAME_SIZE];
428 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
430 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
432 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
433 if (MF_DBGLEVEL >= MF_DBG_ERROR)
434 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
438 memcpy(d_block, blockData, 16);
439 AppendCrc14443a(d_block, 16);
441 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
443 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
445 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
446 if (MF_DBGLEVEL >= MF_DBG_ERROR)
447 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
454 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
) {
456 uint8_t block
[5] = {blockNo
, 0x00, 0x00, 0x00, 0x00 };
457 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
458 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
460 // command MIFARE_CLASSIC_WRITEBLOCK
461 memcpy(block
+1, blockData
, 4);
463 len
= mifare_sendcmd( MIFARE_ULC_WRITE
, block
, sizeof(block
), receivedAnswer
, receivedAnswerPar
, NULL
);
465 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
466 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
467 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
472 int mifare_classic_halt_ex(struct Crypto1State
*pcs
) {
474 uint8_t receivedAnswer
[4] = {0x00, 0x00, 0x00, 0x00};
475 len
= mifare_sendcmd_short(pcs
, (pcs
== NULL
) ? CRYPT_NONE
: CRYPT_ALL
, 0x50, 0x00, receivedAnswer
, NULL
, NULL
);
477 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("halt error. response len: %x", len
);
482 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
) {
483 return mifare_classic_halt_ex(pcs
);
486 int mifare_ultra_halt() {
488 uint8_t receivedAnswer
[4] = {0x00, 0x00, 0x00, 0x00};
489 len
= mifare_sendcmd_short(NULL
, CRYPT_NONE
, 0x50, 0x00, receivedAnswer
, NULL
, NULL
);
491 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("halt error. response len: %x", len
);
498 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
499 // plus evtl. 8 sectors with 16 blocks each (4k cards)
500 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
501 return (sectorNo
< 32) ? 4 : 16;
504 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
508 return 32*4 + (sectorNo
- 32) * 16;
512 // work with emulator memory
513 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
514 emlSetMem_xt(data
, blockNum
, blocksCount
, 16);
517 void emlSetMem_xt(uint8_t *data
, int blockNum
, int blocksCount
, int blockBtWidth
) {
518 uint8_t* emCARD
= BigBuf_get_EM_addr();
519 memcpy(emCARD
+ blockNum
* blockBtWidth
, data
, blocksCount
* blockBtWidth
);
522 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
523 uint8_t* emCARD
= BigBuf_get_EM_addr();
524 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
527 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
528 uint8_t* emCARD
= BigBuf_get_EM_addr();
529 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
532 int emlCheckValBl(int blockNum
) {
533 uint8_t* emCARD
= BigBuf_get_EM_addr();
534 uint8_t* data
= emCARD
+ blockNum
* 16;
536 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
537 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
538 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
539 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
540 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
541 (data
[12] != (data
[15] ^ 0xff))
547 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
548 uint8_t* emCARD
= BigBuf_get_EM_addr();
549 uint8_t* data
= emCARD
+ blockNum
* 16;
551 if (emlCheckValBl(blockNum
))
554 memcpy(blReg
, data
, 4);
559 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
560 uint8_t* emCARD
= BigBuf_get_EM_addr();
561 uint8_t* data
= emCARD
+ blockNum
* 16;
563 memcpy(data
+ 0, &blReg
, 4);
564 memcpy(data
+ 8, &blReg
, 4);
565 blReg
= blReg
^ 0xffffffff;
566 memcpy(data
+ 4, &blReg
, 4);
569 data
[13] = blBlock
^ 0xff;
571 data
[15] = blBlock
^ 0xff;
576 uint64_t emlGetKey(int sectorNum
, int keyType
) {
577 uint8_t key
[6] = {0x00};
578 uint8_t* emCARD
= BigBuf_get_EM_addr();
580 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
581 return bytes_to_num(key
, 6);
584 void emlClearMem(void) {
587 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
588 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
589 uint8_t* emCARD
= BigBuf_get_EM_addr();
591 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
593 // fill sectors trailer data
594 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16))
595 emlSetMem((uint8_t *)trailer
, b
, 1);
598 emlSetMem((uint8_t *)uid
, 0, 1);
603 // Mifare desfire commands
604 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
)
606 uint8_t dcmd
[5] = {cmd
, data
[0], data
[1], 0x00, 0x00};
607 AppendCrc14443a(dcmd
, 3);
609 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
610 int len
= ReaderReceive(answer
, answer_parity
);
612 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
613 Dbprintf("Authentication failed. Card timeout.");
619 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
)
621 uint8_t dcmd
[20] = {0x00};
623 memcpy(dcmd
+1,data
,17);
624 AppendCrc14443a(dcmd
, 18);
626 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
627 int len
= ReaderReceive(answer
, answer_parity
);
629 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
630 Dbprintf("Authentication failed. Card timeout.");
636 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
639 // load key, keynumber
640 uint8_t data
[2]={0x0a, 0x00};
641 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
642 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
644 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
646 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
647 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
652 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
653 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
654 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
655 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
656 receivedAnswer
[10],receivedAnswer
[11]);
658 memcpy(blockData
, receivedAnswer
, 12);
664 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
667 uint8_t data
[17] = {0x00};
669 memcpy(data
+1,key
,16);
671 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
672 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
674 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
676 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
677 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
678 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
683 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
684 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
685 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
686 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
687 receivedAnswer
[10],receivedAnswer
[11]);
689 memcpy(blockData
, receivedAnswer
, 12);