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 //-----------------------------------------------------------------------------
13 #include "mifareutil.h"
14 #include "proxmark3.h"
19 #include "iso14443crc.h"
20 #include "iso14443a.h"
21 #include "crapto1/crapto1.h"
22 #include "polarssl/des.h"
24 int MF_DBGLEVEL
= MF_DBG_ALL
;
27 void mf_crypto1_decryptEx(struct Crypto1State
*pcs
, uint8_t *data_in
, int len
, uint8_t *data_out
){
32 for (i
= 0; i
< len
; i
++)
33 data_out
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data_in
[i
];
36 for (i
= 0; i
< 4; i
++)
37 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data_in
[0], i
)) << i
;
44 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
){
45 mf_crypto1_decryptEx(pcs
, data
, len
, data
);
48 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) {
53 for (i
= 0; i
< len
; i
++) {
55 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
58 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(bt
)) & 0x01)<<(7-(i
&0x0007)));
63 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
67 for (i
= 0; i
< 4; i
++)
68 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
;
73 // send X byte basic commands
74 int mifare_sendcmd(uint8_t cmd
, uint8_t* data
, uint8_t data_size
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
)
76 uint8_t dcmd
[data_size
+3];
78 memcpy(dcmd
+1,data
,data_size
);
79 AppendCrc14443a(dcmd
, data_size
+1);
80 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
81 int len
= ReaderReceive(answer
, answer_parity
);
83 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("%02X Cmd failed. Card timeout.", cmd
);
84 len
= ReaderReceive(answer
,answer_parity
);
90 // send 2 byte commands
91 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
)
93 uint8_t dcmd
[4], ecmd
[4];
95 uint8_t par
[1]; // 1 Byte parity is enough here
98 AppendCrc14443a(dcmd
, 2);
100 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
104 for (pos
= 0; pos
< 4; pos
++)
106 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
107 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(dcmd
[pos
])) & 0x01) << (7-pos
));
110 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
113 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
116 int len
= ReaderReceive(answer
, par
);
118 if (answer_parity
) *answer_parity
= par
[0];
120 if (crypted
== CRYPT_ALL
) {
123 for (pos
= 0; pos
< 4; pos
++)
124 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
129 for (pos
= 0; pos
< len
; pos
++)
131 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
139 // mifare classic commands
140 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)
142 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
145 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
)
151 uint8_t par
[1] = {0x00};
153 uint32_t nt
, ntpp
; // Supplied tag nonce
155 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
156 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
157 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
159 // Transmit MIFARE_CLASSIC_AUTH
160 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
161 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
162 if (len
!= 4) return 1;
164 // "random" reader nonce:
170 // Save the tag nonce (nt)
171 nt
= bytes_to_num(receivedAnswer
, 4);
173 // ----------------------------- crypto1 create
175 crypto1_destroy(pcs
);
177 // Init cipher with key
178 crypto1_create(pcs
, ui64Key
);
180 if (isNested
== AUTH_NESTED
) {
181 // decrypt nt with help of new key
182 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
184 // Load (plain) uid^nt into the cipher
185 crypto1_word(pcs
, nt
^ uid
, 0);
189 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
190 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
196 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
198 for (pos
= 0; pos
< 4; pos
++)
200 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
201 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nr
[pos
])) & 0x01) << (7-pos
));
204 // Skip 32 bits in pseudo random generator
205 nt
= prng_successor(nt
,32);
208 for (pos
= 4; pos
< 8; pos
++)
210 nt
= prng_successor(nt
,8);
211 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
212 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
)) & 0x01) << (7-pos
));
215 // Transmit reader nonce and reader answer
216 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
218 // Receive 4 byte tag answer
219 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
222 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
226 memcpy(tmp4
, receivedAnswer
, 4);
227 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
229 if (ntpp
!= bytes_to_num(tmp4
, 4)) {
230 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
237 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
243 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
244 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
246 // command MIFARE_CLASSIC_READBLOCK
247 len
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
249 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
253 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
257 memcpy(bt
, receivedAnswer
+ 16, 2);
258 AppendCrc14443a(receivedAnswer
, 16);
259 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
260 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
264 memcpy(blockData
, receivedAnswer
, 16);
268 // mifare ultralight commands
269 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
274 uint8_t key
[4] = {0x00};
275 memcpy(key
, keybytes
, 4);
277 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
278 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
279 len
= mifare_sendcmd(0x1B, key
, sizeof(key
), resp
, respPar
, NULL
);
280 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
282 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
286 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
287 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
289 memcpy(pack
, resp
, 4);
293 int mifare_ultra_auth(uint8_t *keybytes
){
297 des3_context ctx
= { 0x00 };
298 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
299 uint8_t random_b
[8] = {0x00};
300 uint8_t enc_random_b
[8] = {0x00};
301 uint8_t rnd_ab
[16] = {0x00};
302 uint8_t IV
[8] = {0x00};
303 uint8_t key
[16] = {0x00};
304 memcpy(key
, keybytes
, 16);
307 uint8_t resp
[19] = {0x00};
308 uint8_t respPar
[3] = {0,0,0};
310 // REQUEST AUTHENTICATION
311 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, resp
, respPar
,NULL
);
313 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
318 memcpy(enc_random_b
,resp
+1,8);
321 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
322 des3_set2key_dec(&ctx
, key
);
323 des3_crypt_cbc(&ctx
// des3_context
324 , DES_DECRYPT
// int mode
325 , sizeof(random_b
) // length
327 , enc_random_b
// input
332 memcpy(rnd_ab
,random_a
,8);
333 memcpy(rnd_ab
+8,random_b
,8);
335 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
336 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
337 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]);
339 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
340 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
342 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
343 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
345 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
346 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
349 // encrypt out, in, length, key, iv
350 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
351 des3_set2key_enc(&ctx
, key
);
352 des3_crypt_cbc(&ctx
// des3_context
353 , DES_ENCRYPT
// int mode
354 , sizeof(rnd_ab
) // length
355 , enc_random_b
// iv[8]
360 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
361 len
= mifare_sendcmd(0xAF, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
363 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
367 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
368 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
369 memcpy(enc_resp
, resp
+1, 8);
371 // decrypt out, in, length, key, iv
372 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
373 des3_set2key_dec(&ctx
, key
);
374 des3_crypt_cbc(&ctx
// des3_context
375 , DES_DECRYPT
// int mode
377 , enc_random_b
// iv[8]
379 , resp_random_a
// output
381 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
382 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
386 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
387 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
388 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
389 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
391 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
392 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
393 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
395 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
396 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
397 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
399 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
400 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
401 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
406 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
)
410 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
411 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
414 len
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
416 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
420 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
424 memcpy(bt
, receivedAnswer
+ 16, 2);
425 AppendCrc14443a(receivedAnswer
, 16);
426 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
427 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
431 memcpy(blockData
, receivedAnswer
, 14);
435 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
440 uint8_t par
[3] = {0}; // enough for 18 Bytes to send
443 uint8_t d_block
[18], d_block_enc
[18];
444 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
445 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
447 // command MIFARE_CLASSIC_WRITEBLOCK
448 len
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
450 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
451 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
455 memcpy(d_block
, blockData
, 16);
456 AppendCrc14443a(d_block
, 16);
459 for (pos
= 0; pos
< 18; pos
++)
461 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
462 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
465 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
467 // Receive the response
468 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
471 for (i
= 0; i
< 4; i
++)
472 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
474 if ((len
!= 1) || (res
!= 0x0A)) {
475 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
482 /* // command not needed, but left for future testing
483 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
486 uint8_t par[3] = {0}; // enough for 18 parity bits
487 uint8_t d_block[18] = {0x00};
488 uint8_t receivedAnswer[MAX_FRAME_SIZE];
489 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
491 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
493 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
494 if (MF_DBGLEVEL >= MF_DBG_ERROR)
495 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
499 memcpy(d_block, blockData, 16);
500 AppendCrc14443a(d_block, 16);
502 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
504 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
506 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
507 if (MF_DBGLEVEL >= MF_DBG_ERROR)
508 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
515 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)
518 uint8_t d_block
[5] = {0x00};
519 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
520 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
522 // command MIFARE_CLASSIC_WRITEBLOCK
524 memcpy(d_block
+1,blockData
,4);
525 //AppendCrc14443a(d_block, 6);
527 len
= mifare_sendcmd(0xA2, d_block
, sizeof(d_block
), receivedAnswer
, receivedAnswerPar
, NULL
);
529 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
530 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
531 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
537 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
)
540 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
541 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
543 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
545 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
546 Dbprintf("halt error. response len: %x", len
);
553 int mifare_ultra_halt()
556 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
557 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
559 len
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
561 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
562 Dbprintf("halt error. response len: %x", len
);
569 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
570 // plus evtl. 8 sectors with 16 blocks each (4k cards)
571 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
579 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
584 return 32*4 + (sectorNo
- 32) * 16;
589 // work with emulator memory
590 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
591 uint8_t* emCARD
= BigBuf_get_EM_addr();
592 memcpy(emCARD
+ blockNum
* 16, data
, blocksCount
* 16);
595 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
596 uint8_t* emCARD
= BigBuf_get_EM_addr();
597 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
600 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
601 uint8_t* emCARD
= BigBuf_get_EM_addr();
602 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
605 int emlCheckValBl(int blockNum
) {
606 uint8_t* emCARD
= BigBuf_get_EM_addr();
607 uint8_t* data
= emCARD
+ blockNum
* 16;
609 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
610 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
611 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
612 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
613 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
614 (data
[12] != (data
[15] ^ 0xff))
620 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
621 uint8_t* emCARD
= BigBuf_get_EM_addr();
622 uint8_t* data
= emCARD
+ blockNum
* 16;
624 if (emlCheckValBl(blockNum
)) {
628 memcpy(blReg
, data
, 4);
633 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
634 uint8_t* emCARD
= BigBuf_get_EM_addr();
635 uint8_t* data
= emCARD
+ blockNum
* 16;
637 memcpy(data
+ 0, &blReg
, 4);
638 memcpy(data
+ 8, &blReg
, 4);
639 blReg
= blReg
^ 0xffffffff;
640 memcpy(data
+ 4, &blReg
, 4);
643 data
[13] = blBlock
^ 0xff;
645 data
[15] = blBlock
^ 0xff;
650 uint64_t emlGetKey(int sectorNum
, int keyType
) {
652 uint8_t* emCARD
= BigBuf_get_EM_addr();
654 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
655 return bytes_to_num(key
, 6);
658 void emlClearMem(void) {
661 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
662 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
663 uint8_t* emCARD
= BigBuf_get_EM_addr();
665 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
667 // fill sectors trailer data
668 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
669 emlSetMem((uint8_t *)trailer
, b
, 1);
673 emlSetMem((uint8_t *)uid
, 0, 1);
678 // Mifare desfire commands
679 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
)
681 uint8_t dcmd
[5] = {0x00};
683 memcpy(dcmd
+1,data
,2);
684 AppendCrc14443a(dcmd
, 3);
686 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
687 int len
= ReaderReceive(answer
, answer_parity
);
689 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
690 Dbprintf("Authentication failed. Card timeout.");
696 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
)
698 uint8_t dcmd
[20] = {0x00};
700 memcpy(dcmd
+1,data
,17);
701 AppendCrc14443a(dcmd
, 18);
703 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
704 int len
= ReaderReceive(answer
, answer_parity
);
706 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
707 Dbprintf("Authentication failed. Card timeout.");
713 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
716 // load key, keynumber
717 uint8_t data
[2]={0x0a, 0x00};
718 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
719 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
721 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
723 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
724 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
729 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
730 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
731 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
732 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
733 receivedAnswer
[10],receivedAnswer
[11]);
735 memcpy(blockData
, receivedAnswer
, 12);
741 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
744 uint8_t data
[17] = {0x00};
746 memcpy(data
+1,key
,16);
748 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
749 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
751 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
753 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
754 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
755 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
760 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
761 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
762 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
763 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
764 receivedAnswer
[10],receivedAnswer
[11]);
766 memcpy(blockData
, receivedAnswer
, 12);
772 //-----------------------------------------------------------------------------
775 //-----------------------------------------------------------------------------
777 int MifareChkBlockKey(uint8_t *uid
, uint32_t *cuid
, uint8_t *cascade_levels
, uint64_t ui64Key
, uint8_t blockNo
, uint8_t keyType
, uint8_t debugLevel
) {
779 struct Crypto1State mpcs
= {0, 0};
780 struct Crypto1State
*pcs
;
783 // Iceman: use piwi's faster nonce collecting part in hardnested.
784 if (*cascade_levels
== 0) { // need a full select cycle to get the uid first
785 iso14a_card_select_t card_info
;
786 if(!iso14443a_select_card(uid
, &card_info
, cuid
, true, 0, true)) {
787 if (debugLevel
>= 1) Dbprintf("ChkKeys: Can't select card");
790 switch (card_info
.uidlen
) {
791 case 4 : *cascade_levels
= 1; break;
792 case 7 : *cascade_levels
= 2; break;
793 case 10: *cascade_levels
= 3; break;
796 } else { // no need for anticollision. We can directly select the card
797 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, *cascade_levels
, true)) {
798 if (debugLevel
>= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels
);
803 if(mifare_classic_auth(pcs
, *cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
804 // SpinDelayUs(AUTHENTICATION_TIMEOUT); // it not needs because mifare_classic_auth have timeout from iso14a_set_timeout()
807 /* // let it be here. it like halt command, but maybe it will work in some strange cases
808 uint8_t dummy_answer = 0;
809 ReaderTransmit(&dummy_answer, 1, NULL);
810 int timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;
811 // wait for the card to become ready again
812 while(GetCountSspClk() < timeout) {};
814 // it needs after success authentication
815 mifare_classic_halt(pcs
, *cuid
);
822 int MifareChkBlockKeys(uint8_t *keys
, uint8_t keyCount
, uint8_t blockNo
, uint8_t keyType
, uint8_t debugLevel
) {
825 uint8_t cascade_levels
= 0;
826 uint64_t ui64Key
= 0;
829 for (uint8_t i
= 0; i
< keyCount
; i
++) {
831 // Allow button press / usb cmd to interrupt device
832 if (BUTTON_PRESS() && !usb_poll_validate_length()) {
833 Dbprintf("ChkKeys: Cancel operation. Exit...");
837 ui64Key
= bytes_to_num(keys
+ i
* 6, 6);
838 int res
= MifareChkBlockKey(uid
, &cuid
, &cascade_levels
, ui64Key
, blockNo
, keyType
, debugLevel
);
843 if (retryCount
>= 5) {
844 Dbprintf("ChkKeys: block=%d key=%d. Can't select. Exit...", blockNo
, keyType
);
847 --i
; // try the same key once again
850 // Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);
854 // can't authenticate
857 continue; // can't auth. wrong key.
866 // multisector multikey check
867 int MifareMultisectorChk(uint8_t *keys
, uint8_t keyCount
, uint8_t SectorCount
, uint8_t keyType
, uint8_t debugLevel
, TKeyIndex
*keyIndex
) {
870 // int clk = GetCountSspClk();
872 for(int sc
= 0; sc
< SectorCount
; sc
++){
877 res
= MifareChkBlockKeys(keys
, keyCount
, FirstBlockOfSector(sc
), keyAB
& 0x01, debugLevel
);
882 (*keyIndex
)[keyAB
& 0x01][sc
] = res
;
884 } while(--keyAB
> 0);
887 // Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));