]>
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
];
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
)
70 uint8_t dcmd
[data_size
+3];
72 memcpy(dcmd
+1,data
,data_size
);
73 AppendCrc14443a(dcmd
, data_size
+1);
74 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
75 int len
= ReaderReceive(answer
, answer_parity
);
77 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) 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] = {0x00};
87 uint8_t ecmd
[4] = {0x00};
89 uint8_t par
[1] = {0x00}; // 1 Byte parity is enough here
92 AppendCrc14443a(dcmd
, 2);
94 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
98 for (pos
= 0; pos
< 4; pos
++) {
99 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
100 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(dcmd
[pos
])) & 0x01) << (7-pos
));
103 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
106 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
109 int len
= ReaderReceive(answer
, par
);
111 if (answer_parity
) *answer_parity
= par
[0];
113 if (crypted
== CRYPT_ALL
) {
116 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 0)) << 0;
117 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 1)) << 1;
118 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 2)) << 2;
119 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 3)) << 3;
123 for (pos
= 0; pos
< len
; pos
++)
124 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
131 // mifare classic commands
132 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)
134 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
137 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
)
142 uint8_t par
[1] = {0x00};
144 // "random" reader nonce:
145 //byte_t nr[4] = {0x55, 0x41, 0x49, 0x92};
146 byte_t nr
[4] = {0x01, 0x01, 0x01, 0x01};
148 uint32_t nt
, ntpp
; // Supplied tag nonce
150 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
151 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
152 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
154 // Transmit MIFARE_CLASSIC_AUTH
155 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
156 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
157 if (len
!= 4) return 1;
159 // Save the tag nonce (nt)
160 nt
= bytes_to_num(receivedAnswer
, 4);
162 // ----------------------------- crypto1 create
164 crypto1_destroy(pcs
);
166 // Init cipher with key
167 crypto1_create(pcs
, ui64Key
);
169 if (isNested
== AUTH_NESTED
) {
170 // decrypt nt with help of new key
171 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
173 // Load (plain) uid^nt into the cipher
174 crypto1_word(pcs
, nt
^ uid
, 0);
178 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
179 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
185 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
187 for (pos
= 0; pos
< 4; pos
++) {
188 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
189 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nr
[pos
])) & 0x01) << (7-pos
));
192 // Skip 32 bits in pseudo random generator
193 nt
= prng_successor(nt
,32);
196 for (pos
= 4; pos
< 8; pos
++) {
197 nt
= prng_successor(nt
,8);
198 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
199 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
& 0xff)) & 0x01) << (7-pos
));
202 // Transmit reader nonce and reader answer
203 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
205 // Receive 4 byte tag answer
206 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
208 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
212 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
214 if (ntpp
!= bytes_to_num(receivedAnswer
, 4)) {
215 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
221 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
225 uint8_t bt
[2] = {0x00};
227 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
228 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
230 // command MIFARE_CLASSIC_READBLOCK
231 len
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
233 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
237 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
241 memcpy(bt
, receivedAnswer
+ 16, 2);
242 AppendCrc14443a(receivedAnswer
, 16);
243 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
244 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
248 memcpy(blockData
, receivedAnswer
, 16);
252 // mifare ultralight commands
253 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
256 uint8_t resp
[4] = {0x00};
257 uint8_t respPar
[1] = {0x00};
258 uint8_t key
[4] = {0x00};
259 memcpy(key
, keybytes
, 4);
261 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
262 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
264 len
= mifare_sendcmd(0x1B, key
, sizeof(key
), resp
, respPar
, NULL
);
267 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
271 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
272 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
274 memcpy(pack
, resp
, 4);
278 int mifare_ultra_auth(uint8_t *keybytes
){
282 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
283 uint8_t random_b
[8] = {0x00};
284 uint8_t enc_random_b
[8] = {0x00};
285 uint8_t rnd_ab
[16] = {0x00};
286 uint8_t IV
[8] = {0x00};
287 uint8_t key
[16] = {0x00};
288 memcpy(key
, keybytes
, 16);
291 uint8_t resp
[19] = {0x00};
292 uint8_t respPar
[3] = {0,0,0};
294 // REQUEST AUTHENTICATION
295 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, resp
, respPar
,NULL
);
297 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
302 memcpy(enc_random_b
,resp
+1,8);
305 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
307 memcpy(rnd_ab
,random_a
,8);
308 memcpy(rnd_ab
+8,random_b
,8);
310 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
311 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
312 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]);
314 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
315 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
317 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
318 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
320 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
321 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
324 // encrypt out, in, length, key, iv
325 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
326 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
327 len
= mifare_sendcmd(0xAF, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
329 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
333 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
334 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
335 memcpy(enc_resp
, resp
+1, 8);
337 // decrypt out, in, length, key, iv
338 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
339 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
340 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
344 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
345 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
346 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
347 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
349 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
350 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
351 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
353 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
354 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
355 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
357 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
358 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
359 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
364 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
)
367 uint8_t bt
[2] = {0x00};
368 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
369 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
371 len
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
373 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
377 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
381 memcpy(bt
, receivedAnswer
+ 16, 2);
382 AppendCrc14443a(receivedAnswer
, 16);
383 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
384 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
388 memcpy(blockData
, receivedAnswer
, 14);
392 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
397 uint8_t par
[3] = {0x00}; // enough for 18 Bytes to send
400 uint8_t d_block
[18], d_block_enc
[18];
401 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
402 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
404 // command MIFARE_CLASSIC_WRITEBLOCK
405 len
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
407 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
408 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
412 memcpy(d_block
, blockData
, 16);
413 AppendCrc14443a(d_block
, 16);
416 for (pos
= 0; pos
< 18; pos
++) {
417 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
418 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
421 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
423 // Receive the response
424 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
427 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 0)) << 0;
428 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 1)) << 1;
429 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 2)) << 2;
430 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 3)) << 3;
432 if ((len
!= 1) || (res
!= 0x0A)) {
433 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
440 /* // command not needed, but left for future testing
441 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
444 uint8_t par[3] = {0}; // enough for 18 parity bits
445 uint8_t d_block[18] = {0x00};
446 uint8_t receivedAnswer[MAX_FRAME_SIZE];
447 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
449 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
451 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
452 if (MF_DBGLEVEL >= MF_DBG_ERROR)
453 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
457 memcpy(d_block, blockData, 16);
458 AppendCrc14443a(d_block, 16);
460 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
462 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
464 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
465 if (MF_DBGLEVEL >= MF_DBG_ERROR)
466 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
473 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)
476 uint8_t d_block
[5] = {0x00};
477 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
478 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
480 // command MIFARE_CLASSIC_WRITEBLOCK
482 memcpy(d_block
+1,blockData
,4);
483 //AppendCrc14443a(d_block, 6);
485 len
= mifare_sendcmd(0xA2, d_block
, sizeof(d_block
), receivedAnswer
, receivedAnswerPar
, NULL
);
487 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
488 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
489 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
494 int mifare_classic_halt_ex(struct Crypto1State
*pcs
) {
496 uint8_t receivedAnswer
[4] = {0x00};
497 uint8_t receivedAnswerPar
[4] = {0x00};
499 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
501 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("halt error. response len: %x", len
);
506 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
) {
507 return mifare_classic_halt_ex(pcs
);
510 int mifare_ultra_halt()
513 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
514 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
516 len
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
518 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
519 Dbprintf("halt error. response len: %x", len
);
526 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
527 // plus evtl. 8 sectors with 16 blocks each (4k cards)
528 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
535 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
540 return 32*4 + (sectorNo
- 32) * 16;
545 // work with emulator memory
546 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
547 emlSetMem_xt(data
, blockNum
, blocksCount
, 16);
550 void emlSetMem_xt(uint8_t *data
, int blockNum
, int blocksCount
, int blockBtWidth
) {
551 uint8_t* emCARD
= BigBuf_get_EM_addr();
552 memcpy(emCARD
+ blockNum
* blockBtWidth
, data
, blocksCount
* blockBtWidth
);
555 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
556 uint8_t* emCARD
= BigBuf_get_EM_addr();
557 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
560 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
561 uint8_t* emCARD
= BigBuf_get_EM_addr();
562 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
565 int emlCheckValBl(int blockNum
) {
566 uint8_t* emCARD
= BigBuf_get_EM_addr();
567 uint8_t* data
= emCARD
+ blockNum
* 16;
569 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
570 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
571 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
572 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
573 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
574 (data
[12] != (data
[15] ^ 0xff))
580 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
581 uint8_t* emCARD
= BigBuf_get_EM_addr();
582 uint8_t* data
= emCARD
+ blockNum
* 16;
584 if (emlCheckValBl(blockNum
))
587 memcpy(blReg
, data
, 4);
592 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
593 uint8_t* emCARD
= BigBuf_get_EM_addr();
594 uint8_t* data
= emCARD
+ blockNum
* 16;
596 memcpy(data
+ 0, &blReg
, 4);
597 memcpy(data
+ 8, &blReg
, 4);
598 blReg
= blReg
^ 0xffffffff;
599 memcpy(data
+ 4, &blReg
, 4);
602 data
[13] = blBlock
^ 0xff;
604 data
[15] = blBlock
^ 0xff;
609 uint64_t emlGetKey(int sectorNum
, int keyType
) {
610 uint8_t key
[6] = {0x00};
611 uint8_t* emCARD
= BigBuf_get_EM_addr();
613 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
614 return bytes_to_num(key
, 6);
617 void emlClearMem(void) {
620 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
621 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
622 uint8_t* emCARD
= BigBuf_get_EM_addr();
624 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
626 // fill sectors trailer data
627 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16))
628 emlSetMem((uint8_t *)trailer
, b
, 1);
631 emlSetMem((uint8_t *)uid
, 0, 1);
636 // Mifare desfire commands
637 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
)
639 uint8_t dcmd
[5] = {0x00};
641 memcpy(dcmd
+1,data
,2);
642 AppendCrc14443a(dcmd
, 3);
644 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
645 int len
= ReaderReceive(answer
, answer_parity
);
647 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
648 Dbprintf("Authentication failed. Card timeout.");
654 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
)
656 uint8_t dcmd
[20] = {0x00};
658 memcpy(dcmd
+1,data
,17);
659 AppendCrc14443a(dcmd
, 18);
661 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
662 int len
= ReaderReceive(answer
, answer_parity
);
664 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
665 Dbprintf("Authentication failed. Card timeout.");
671 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
674 // load key, keynumber
675 uint8_t data
[2]={0x0a, 0x00};
676 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
677 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
679 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
681 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
682 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
687 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
688 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
689 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
690 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
691 receivedAnswer
[10],receivedAnswer
[11]);
693 memcpy(blockData
, receivedAnswer
, 12);
699 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
702 uint8_t data
[17] = {0x00};
704 memcpy(data
+1,key
,16);
706 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
707 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
709 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
711 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
712 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
713 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
718 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
719 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
720 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
721 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
722 receivedAnswer
[10],receivedAnswer
[11]);
724 memcpy(blockData
, receivedAnswer
, 12);