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 "mifareutil.h"
17 #include "proxmark3.h"
21 #include "iso14443crc.h"
22 #include "iso14443a.h"
23 #include "crapto1/crapto1.h"
24 #include "mbedtls/des.h"
25 #include "protocols.h"
27 int MF_DBGLEVEL
= MF_DBG_INFO
;
30 void mf_crypto1_decryptEx(struct Crypto1State
*pcs
, uint8_t *data_in
, int len
, uint8_t *data_out
){
35 for (i
= 0; i
< len
; i
++)
36 data_out
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data_in
[i
];
39 for (i
= 0; i
< 4; i
++)
40 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data_in
[0], i
)) << i
;
47 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
){
48 mf_crypto1_decryptEx(pcs
, data
, len
, data
);
51 void mf_crypto1_encryptEx(struct Crypto1State
*pcs
, uint8_t *data
, uint8_t *in
, uint16_t len
, uint8_t *par
) {
56 for (i
= 0; i
< len
; i
++) {
58 data
[i
] = crypto1_byte(pcs
, in
==NULL
?0x00:in
[i
], 0) ^ data
[i
];
61 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(bt
)) & 0x01)<<(7-(i
&0x0007)));
66 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) {
67 mf_crypto1_encryptEx(pcs
, data
, NULL
, len
, par
);
70 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
74 for (i
= 0; i
< 4; i
++)
75 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
;
80 // send X byte basic commands
81 int mifare_sendcmd(uint8_t cmd
, uint8_t* data
, uint8_t data_size
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
)
83 uint8_t dcmd
[data_size
+3];
85 memcpy(dcmd
+1,data
,data_size
);
86 AppendCrc14443a(dcmd
, data_size
+1);
87 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
88 int len
= ReaderReceive(answer
, answer_parity
);
90 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("%02X Cmd failed. Card timeout.", cmd
);
91 len
= ReaderReceive(answer
,answer_parity
);
97 // send 2 byte commands
98 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
)
100 uint8_t dcmd
[4], ecmd
[4];
102 uint8_t par
[1]; // 1 Byte parity is enough here
105 AppendCrc14443a(dcmd
, 2);
107 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
111 for (pos
= 0; pos
< 4; pos
++)
113 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
114 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(dcmd
[pos
])) & 0x01) << (7-pos
));
117 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
120 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
123 int len
= ReaderReceive(answer
, par
);
125 if (answer_parity
) *answer_parity
= par
[0];
127 if (crypted
== CRYPT_ALL
) {
130 for (pos
= 0; pos
< 4; pos
++)
131 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
136 for (pos
= 0; pos
< len
; pos
++)
138 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
146 // mifare classic commands
147 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)
149 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
152 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
)
158 uint8_t par
[1] = {0x00};
160 uint32_t nt
, ntpp
; // Supplied tag nonce
162 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
163 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
164 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
166 // Transmit MIFARE_CLASSIC_AUTH
167 len
= mifare_sendcmd_short(pcs
, isNested
, keyType
& 0x01 ? MIFARE_AUTH_KEYB
: MIFARE_AUTH_KEYA
, blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
168 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
169 if (len
!= 4) return 1;
171 // "random" reader nonce:
177 // Save the tag nonce (nt)
178 nt
= bytes_to_num(receivedAnswer
, 4);
180 // ----------------------------- crypto1 create
182 crypto1_destroy(pcs
);
184 // Init cipher with key
185 crypto1_create(pcs
, ui64Key
);
187 if (isNested
== AUTH_NESTED
) {
188 // decrypt nt with help of new key
189 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
191 // Load (plain) uid^nt into the cipher
192 crypto1_word(pcs
, nt
^ uid
, 0);
196 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
197 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
203 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
205 for (pos
= 0; pos
< 4; pos
++)
207 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
208 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nr
[pos
])) & 0x01) << (7-pos
));
211 // Skip 32 bits in pseudo random generator
212 nt
= prng_successor(nt
,32);
215 for (pos
= 4; pos
< 8; pos
++)
217 nt
= prng_successor(nt
,8);
218 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
219 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
)) & 0x01) << (7-pos
));
222 // Transmit reader nonce and reader answer
223 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
225 // Receive 4 byte tag answer
226 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
229 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
233 memcpy(tmp4
, receivedAnswer
, 4);
234 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
236 if (ntpp
!= bytes_to_num(tmp4
, 4)) {
237 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
244 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
250 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
251 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
253 // command MIFARE_CLASSIC_READBLOCK
254 len
= mifare_sendcmd_short(pcs
, 1, MIFARE_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
256 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
260 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
264 memcpy(bt
, receivedAnswer
+ 16, 2);
265 AppendCrc14443a(receivedAnswer
, 16);
266 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
267 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
271 memcpy(blockData
, receivedAnswer
, 16);
275 // mifare ultralight commands
276 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
281 uint8_t key
[4] = {0x00};
282 memcpy(key
, keybytes
, 4);
284 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
285 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
286 len
= mifare_sendcmd(MIFARE_ULEV1_AUTH
, key
, sizeof(key
), resp
, respPar
, NULL
);
287 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
289 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
293 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
294 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
296 memcpy(pack
, resp
, 4);
300 int mifare_ultra_auth(uint8_t *keybytes
){
304 mbedtls_des3_context ctx
= { {0} };
305 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
306 uint8_t random_b
[8] = {0x00};
307 uint8_t enc_random_b
[8] = {0x00};
308 uint8_t rnd_ab
[16] = {0x00};
309 uint8_t IV
[8] = {0x00};
310 uint8_t key
[16] = {0x00};
311 memcpy(key
, keybytes
, 16);
314 uint8_t resp
[19] = {0x00};
315 uint8_t respPar
[3] = {0,0,0};
317 // REQUEST AUTHENTICATION
318 len
= mifare_sendcmd_short(NULL
, 1, MIFARE_ULC_AUTH_1
, 0x00, resp
, respPar
,NULL
);
320 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
325 memcpy(enc_random_b
,resp
+1,8);
328 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
329 mbedtls_des3_set2key_dec(&ctx
, key
);
330 mbedtls_des3_crypt_cbc(&ctx
// des3_context
331 , MBEDTLS_DES_DECRYPT
// int mode
332 , sizeof(random_b
) // length
334 , enc_random_b
// input
339 memcpy(rnd_ab
,random_a
,8);
340 memcpy(rnd_ab
+8,random_b
,8);
342 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
343 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
344 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]);
346 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
347 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
349 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
350 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
352 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
353 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
356 // encrypt out, in, length, key, iv
357 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
358 mbedtls_des3_set2key_enc(&ctx
, key
);
359 mbedtls_des3_crypt_cbc(&ctx
// des3_context
360 , MBEDTLS_DES_ENCRYPT
// int mode
361 , sizeof(rnd_ab
) // length
362 , enc_random_b
// iv[8]
367 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
368 len
= mifare_sendcmd(MIFARE_ULC_AUTH_2
, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
370 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
374 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
375 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
376 memcpy(enc_resp
, resp
+1, 8);
378 // decrypt out, in, length, key, iv
379 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
380 mbedtls_des3_set2key_dec(&ctx
, key
);
381 mbedtls_des3_crypt_cbc(&ctx
// des3_context
382 , MBEDTLS_DES_DECRYPT
// int mode
384 , enc_random_b
// iv[8]
386 , resp_random_a
// output
388 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
389 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
393 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
394 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
395 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
396 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
398 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
399 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
400 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
402 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
403 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
404 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
406 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
407 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
408 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
414 #define MFU_MAX_RETRIES 5
415 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
)
419 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
420 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
424 for (retries
= 0; retries
< MFU_MAX_RETRIES
; retries
++) {
425 len
= mifare_sendcmd_short(NULL
, 1, MIFARE_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
427 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
432 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
437 memcpy(bt
, receivedAnswer
+ 16, 2);
438 AppendCrc14443a(receivedAnswer
, 16);
439 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
440 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
445 // No errors encountered; don't retry
451 Dbprintf("Cmd Error: too many retries; read failed");
455 memcpy(blockData
, receivedAnswer
, 16);
459 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
464 uint8_t par
[3] = {0}; // enough for 18 Bytes to send
467 uint8_t d_block
[18], d_block_enc
[18];
468 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
469 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
471 // command MIFARE_CLASSIC_WRITEBLOCK
472 len
= mifare_sendcmd_short(pcs
, 1, MIFARE_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
474 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
475 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
479 memcpy(d_block
, blockData
, 16);
480 AppendCrc14443a(d_block
, 16);
483 for (pos
= 0; pos
< 18; pos
++)
485 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
486 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
489 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
491 // Receive the response
492 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
495 for (i
= 0; i
< 4; i
++)
496 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
498 if ((len
!= 1) || (res
!= 0x0A)) {
499 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
506 /* // command not needed, but left for future testing
507 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
510 uint8_t par[3] = {0}; // enough for 18 parity bits
511 uint8_t d_block[18] = {0x00};
512 uint8_t receivedAnswer[MAX_FRAME_SIZE];
513 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
515 len = mifare_sendcmd_short(NULL, true, MIFARE_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);
517 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
518 if (MF_DBGLEVEL >= MF_DBG_ERROR)
519 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
523 memcpy(d_block, blockData, 16);
524 AppendCrc14443a(d_block, 16);
526 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
528 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
530 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
531 if (MF_DBGLEVEL >= MF_DBG_ERROR)
532 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
539 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)
542 uint8_t d_block
[5] = {0x00};
543 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
544 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
546 // command MIFARE_CLASSIC_WRITEBLOCK
548 memcpy(d_block
+1,blockData
,4);
549 //AppendCrc14443a(d_block, 6);
551 len
= mifare_sendcmd(0xA2, d_block
, sizeof(d_block
), receivedAnswer
, receivedAnswerPar
, NULL
);
553 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
554 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
555 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
561 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
)
564 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
565 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
567 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, ISO14443A_CMD_HALT
, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
569 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
570 Dbprintf("halt error. response len: %x", len
);
577 int mifare_ultra_halt()
580 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
581 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
583 len
= mifare_sendcmd_short(NULL
, true, ISO14443A_CMD_HALT
, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
585 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
586 Dbprintf("halt error. response len: %x", len
);
593 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
594 // plus evtl. 8 sectors with 16 blocks each (4k cards)
595 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
603 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
608 return 32*4 + (sectorNo
- 32) * 16;
612 uint8_t SectorTrailer(uint8_t blockNo
)
614 if (blockNo
< 32*4) {
615 return (blockNo
| 0x03);
617 return (blockNo
| 0x0f);
621 bool IsSectorTrailer(uint8_t blockNo
)
623 return (blockNo
== SectorTrailer(blockNo
));
626 // work with emulator memory
627 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
628 uint8_t* emCARD
= BigBuf_get_EM_addr();
629 memcpy(emCARD
+ blockNum
* 16, data
, blocksCount
* 16);
632 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
633 uint8_t* emCARD
= BigBuf_get_EM_addr();
634 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
637 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
638 uint8_t* emCARD
= BigBuf_get_EM_addr();
639 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
642 int emlCheckValBl(int blockNum
) {
643 uint8_t* emCARD
= BigBuf_get_EM_addr();
644 uint8_t* data
= emCARD
+ blockNum
* 16;
646 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
647 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
648 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
649 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
650 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
651 (data
[12] != (data
[15] ^ 0xff))
657 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
658 uint8_t* emCARD
= BigBuf_get_EM_addr();
659 uint8_t* data
= emCARD
+ blockNum
* 16;
661 if (emlCheckValBl(blockNum
)) {
665 memcpy(blReg
, data
, 4);
670 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
671 uint8_t* emCARD
= BigBuf_get_EM_addr();
672 uint8_t* data
= emCARD
+ blockNum
* 16;
674 memcpy(data
+ 0, &blReg
, 4);
675 memcpy(data
+ 8, &blReg
, 4);
676 blReg
= blReg
^ 0xffffffff;
677 memcpy(data
+ 4, &blReg
, 4);
680 data
[13] = blBlock
^ 0xff;
682 data
[15] = blBlock
^ 0xff;
687 uint64_t emlGetKey(int sectorNum
, int keyType
) {
689 uint8_t* emCARD
= BigBuf_get_EM_addr();
691 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
692 return bytes_to_num(key
, 6);
695 void emlClearMem(void) {
698 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
699 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
700 uint8_t* emCARD
= BigBuf_get_EM_addr();
702 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
704 // fill sectors trailer data
705 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
706 emlSetMem((uint8_t *)trailer
, b
, 1);
710 emlSetMem((uint8_t *)uid
, 0, 1);
715 // Mifare desfire commands
716 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
)
718 uint8_t dcmd
[5] = {0x00};
720 memcpy(dcmd
+1,data
,2);
721 AppendCrc14443a(dcmd
, 3);
723 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
724 int len
= ReaderReceive(answer
, answer_parity
);
726 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
727 Dbprintf("Authentication failed. Card timeout.");
733 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
)
735 uint8_t dcmd
[20] = {0x00};
737 memcpy(dcmd
+1,data
,17);
738 AppendCrc14443a(dcmd
, 18);
740 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
741 int len
= ReaderReceive(answer
, answer_parity
);
743 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
744 Dbprintf("Authentication failed. Card timeout.");
750 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
753 // load key, keynumber
754 uint8_t data
[2]={0x0a, 0x00};
755 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
756 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
758 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
760 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
761 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
766 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
767 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
768 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
769 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
770 receivedAnswer
[10],receivedAnswer
[11]);
772 memcpy(blockData
, receivedAnswer
, 12);
778 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
781 uint8_t data
[17] = {0x00};
783 memcpy(data
+1,key
,16);
785 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
786 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
788 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
790 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
791 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
792 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
797 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
798 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
799 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
800 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
801 receivedAnswer
[10],receivedAnswer
[11]);
803 memcpy(blockData
, receivedAnswer
, 12);
809 //-----------------------------------------------------------------------------
812 //-----------------------------------------------------------------------------
814 int MifareChkBlockKey(uint8_t *uid
, uint32_t *cuid
, uint8_t *cascade_levels
, uint64_t ui64Key
, uint8_t blockNo
, uint8_t keyType
, uint8_t debugLevel
) {
816 struct Crypto1State mpcs
= {0, 0};
817 struct Crypto1State
*pcs
;
820 // Iceman: use piwi's faster nonce collecting part in hardnested.
821 if (*cascade_levels
== 0) { // need a full select cycle to get the uid first
822 iso14a_card_select_t card_info
;
823 if(!iso14443a_select_card(uid
, &card_info
, cuid
, true, 0, true)) {
824 if (debugLevel
>= 1) Dbprintf("ChkKeys: Can't select card");
827 switch (card_info
.uidlen
) {
828 case 4 : *cascade_levels
= 1; break;
829 case 7 : *cascade_levels
= 2; break;
830 case 10: *cascade_levels
= 3; break;
833 } else { // no need for anticollision. We can directly select the card
834 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, *cascade_levels
, true)) {
835 if (debugLevel
>= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels
);
840 if(mifare_classic_auth(pcs
, *cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
841 // SpinDelayUs(AUTHENTICATION_TIMEOUT); // it not needs because mifare_classic_auth have timeout from iso14a_set_timeout()
844 /* // let it be here. it like halt command, but maybe it will work in some strange cases
845 uint8_t dummy_answer = 0;
846 ReaderTransmit(&dummy_answer, 1, NULL);
847 int timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;
848 // wait for the card to become ready again
849 while(GetCountSspClk() < timeout) {};
851 // it needs after success authentication
852 mifare_classic_halt(pcs
, *cuid
);
859 int MifareChkBlockKeys(uint8_t *keys
, uint8_t keyCount
, uint8_t blockNo
, uint8_t keyType
, uint8_t debugLevel
) {
862 uint8_t cascade_levels
= 0;
863 uint64_t ui64Key
= 0;
866 for (uint8_t i
= 0; i
< keyCount
; i
++) {
868 // Allow button press / usb cmd to interrupt device
869 if (BUTTON_PRESS() && !usb_poll_validate_length()) {
870 Dbprintf("ChkKeys: Cancel operation. Exit...");
874 ui64Key
= bytes_to_num(keys
+ i
* 6, 6);
875 int res
= MifareChkBlockKey(uid
, &cuid
, &cascade_levels
, ui64Key
, blockNo
, keyType
, debugLevel
);
880 if (retryCount
>= 5) {
881 Dbprintf("ChkKeys: block=%d key=%d. Can't select. Exit...", blockNo
, keyType
);
884 --i
; // try the same key once again
887 // Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);
891 // can't authenticate
894 continue; // can't auth. wrong key.
903 // multisector multikey check
904 int MifareMultisectorChk(uint8_t *keys
, uint8_t keyCount
, uint8_t SectorCount
, uint8_t keyType
, uint8_t debugLevel
, TKeyIndex
*keyIndex
) {
907 // int clk = GetCountSspClk();
909 for(int sc
= 0; sc
< SectorCount
; sc
++){
914 res
= MifareChkBlockKeys(keys
, keyCount
, FirstBlockOfSector(sc
), keyAB
& 0x01, debugLevel
);
919 (*keyIndex
)[keyAB
& 0x01][sc
] = res
;
921 } while(--keyAB
> 0);
924 // Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));