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"
26 int MF_DBGLEVEL
= MF_DBG_ALL
;
29 void mf_crypto1_decryptEx(struct Crypto1State
*pcs
, uint8_t *data_in
, int len
, uint8_t *data_out
){
34 for (i
= 0; i
< len
; i
++)
35 data_out
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data_in
[i
];
38 for (i
= 0; i
< 4; i
++)
39 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data_in
[0], i
)) << i
;
46 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
){
47 mf_crypto1_decryptEx(pcs
, data
, len
, data
);
50 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) {
55 for (i
= 0; i
< len
; i
++) {
57 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
60 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(bt
)) & 0x01)<<(7-(i
&0x0007)));
65 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
69 for (i
= 0; i
< 4; i
++)
70 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
;
75 // send X byte basic commands
76 int mifare_sendcmd(uint8_t cmd
, uint8_t* data
, uint8_t data_size
, uint8_t* answer
, uint8_t *answer_parity
, uint32_t *timing
)
78 uint8_t dcmd
[data_size
+3];
80 memcpy(dcmd
+1,data
,data_size
);
81 AppendCrc14443a(dcmd
, data_size
+1);
82 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
83 int len
= ReaderReceive(answer
, answer_parity
);
85 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("%02X Cmd failed. Card timeout.", cmd
);
86 len
= ReaderReceive(answer
,answer_parity
);
92 // send 2 byte commands
93 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
)
95 uint8_t dcmd
[4], ecmd
[4];
97 uint8_t par
[1]; // 1 Byte parity is enough here
100 AppendCrc14443a(dcmd
, 2);
102 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
106 for (pos
= 0; pos
< 4; pos
++)
108 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
109 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(dcmd
[pos
])) & 0x01) << (7-pos
));
112 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
115 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
118 int len
= ReaderReceive(answer
, par
);
120 if (answer_parity
) *answer_parity
= par
[0];
122 if (crypted
== CRYPT_ALL
) {
125 for (pos
= 0; pos
< 4; pos
++)
126 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
131 for (pos
= 0; pos
< len
; pos
++)
133 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
141 // mifare classic commands
142 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)
144 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
147 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
)
153 uint8_t par
[1] = {0x00};
155 uint32_t nt
, ntpp
; // Supplied tag nonce
157 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
158 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
159 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
161 // Transmit MIFARE_CLASSIC_AUTH
162 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
163 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
164 if (len
!= 4) return 1;
166 // "random" reader nonce:
172 // Save the tag nonce (nt)
173 nt
= bytes_to_num(receivedAnswer
, 4);
175 // ----------------------------- crypto1 create
177 crypto1_destroy(pcs
);
179 // Init cipher with key
180 crypto1_create(pcs
, ui64Key
);
182 if (isNested
== AUTH_NESTED
) {
183 // decrypt nt with help of new key
184 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
186 // Load (plain) uid^nt into the cipher
187 crypto1_word(pcs
, nt
^ uid
, 0);
191 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
192 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
198 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
200 for (pos
= 0; pos
< 4; pos
++)
202 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
203 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nr
[pos
])) & 0x01) << (7-pos
));
206 // Skip 32 bits in pseudo random generator
207 nt
= prng_successor(nt
,32);
210 for (pos
= 4; pos
< 8; pos
++)
212 nt
= prng_successor(nt
,8);
213 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
214 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
)) & 0x01) << (7-pos
));
217 // Transmit reader nonce and reader answer
218 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
220 // Receive 4 byte tag answer
221 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
224 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
228 memcpy(tmp4
, receivedAnswer
, 4);
229 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
231 if (ntpp
!= bytes_to_num(tmp4
, 4)) {
232 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
239 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
245 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
246 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
248 // command MIFARE_CLASSIC_READBLOCK
249 len
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
251 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
255 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
259 memcpy(bt
, receivedAnswer
+ 16, 2);
260 AppendCrc14443a(receivedAnswer
, 16);
261 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
262 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
266 memcpy(blockData
, receivedAnswer
, 16);
270 // mifare ultralight commands
271 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
276 uint8_t key
[4] = {0x00};
277 memcpy(key
, keybytes
, 4);
279 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
280 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
281 len
= mifare_sendcmd(0x1B, key
, sizeof(key
), resp
, respPar
, NULL
);
282 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
284 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
288 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
289 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
291 memcpy(pack
, resp
, 4);
295 int mifare_ultra_auth(uint8_t *keybytes
){
299 mbedtls_des3_context ctx
= { 0x00 };
300 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
301 uint8_t random_b
[8] = {0x00};
302 uint8_t enc_random_b
[8] = {0x00};
303 uint8_t rnd_ab
[16] = {0x00};
304 uint8_t IV
[8] = {0x00};
305 uint8_t key
[16] = {0x00};
306 memcpy(key
, keybytes
, 16);
309 uint8_t resp
[19] = {0x00};
310 uint8_t respPar
[3] = {0,0,0};
312 // REQUEST AUTHENTICATION
313 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, resp
, respPar
,NULL
);
315 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
320 memcpy(enc_random_b
,resp
+1,8);
323 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
324 mbedtls_des3_set2key_dec(&ctx
, key
);
325 mbedtls_des3_crypt_cbc(&ctx
// des3_context
326 , MBEDTLS_DES_DECRYPT
// int mode
327 , sizeof(random_b
) // length
329 , enc_random_b
// input
334 memcpy(rnd_ab
,random_a
,8);
335 memcpy(rnd_ab
+8,random_b
,8);
337 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
338 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
339 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]);
341 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
342 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
344 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
345 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
347 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
348 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
351 // encrypt out, in, length, key, iv
352 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
353 mbedtls_des3_set2key_enc(&ctx
, key
);
354 mbedtls_des3_crypt_cbc(&ctx
// des3_context
355 , MBEDTLS_DES_ENCRYPT
// int mode
356 , sizeof(rnd_ab
) // length
357 , enc_random_b
// iv[8]
362 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
363 len
= mifare_sendcmd(0xAF, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
365 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
369 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
370 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
371 memcpy(enc_resp
, resp
+1, 8);
373 // decrypt out, in, length, key, iv
374 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
375 mbedtls_des3_set2key_dec(&ctx
, key
);
376 mbedtls_des3_crypt_cbc(&ctx
// des3_context
377 , MBEDTLS_DES_DECRYPT
// int mode
379 , enc_random_b
// iv[8]
381 , resp_random_a
// output
383 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
384 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
388 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
389 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
390 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
391 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
393 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
394 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
395 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
397 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
398 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
399 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
401 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
402 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
403 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
409 #define MFU_MAX_RETRIES 5
410 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
)
414 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
415 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
419 for (retries
= 0; retries
< MFU_MAX_RETRIES
; retries
++) {
420 len
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
422 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
427 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
432 memcpy(bt
, receivedAnswer
+ 16, 2);
433 AppendCrc14443a(receivedAnswer
, 16);
434 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
435 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
440 // No errors encountered; don't retry
446 Dbprintf("Cmd Error: too many retries; read failed");
450 memcpy(blockData
, receivedAnswer
, 14);
454 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
459 uint8_t par
[3] = {0}; // enough for 18 Bytes to send
462 uint8_t d_block
[18], d_block_enc
[18];
463 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
464 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
466 // command MIFARE_CLASSIC_WRITEBLOCK
467 len
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
469 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
470 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
474 memcpy(d_block
, blockData
, 16);
475 AppendCrc14443a(d_block
, 16);
478 for (pos
= 0; pos
< 18; pos
++)
480 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
481 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
484 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
486 // Receive the response
487 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
490 for (i
= 0; i
< 4; i
++)
491 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
493 if ((len
!= 1) || (res
!= 0x0A)) {
494 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
501 /* // command not needed, but left for future testing
502 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
505 uint8_t par[3] = {0}; // enough for 18 parity bits
506 uint8_t d_block[18] = {0x00};
507 uint8_t receivedAnswer[MAX_FRAME_SIZE];
508 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
510 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
512 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
513 if (MF_DBGLEVEL >= MF_DBG_ERROR)
514 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
518 memcpy(d_block, blockData, 16);
519 AppendCrc14443a(d_block, 16);
521 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
523 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
525 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
526 if (MF_DBGLEVEL >= MF_DBG_ERROR)
527 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
534 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)
537 uint8_t d_block
[5] = {0x00};
538 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
539 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
541 // command MIFARE_CLASSIC_WRITEBLOCK
543 memcpy(d_block
+1,blockData
,4);
544 //AppendCrc14443a(d_block, 6);
546 len
= mifare_sendcmd(0xA2, d_block
, sizeof(d_block
), receivedAnswer
, receivedAnswerPar
, NULL
);
548 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
549 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
550 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
556 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
)
559 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
560 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
562 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
564 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
565 Dbprintf("halt error. response len: %x", len
);
572 int mifare_ultra_halt()
575 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
576 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
578 len
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
580 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
581 Dbprintf("halt error. response len: %x", len
);
588 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
589 // plus evtl. 8 sectors with 16 blocks each (4k cards)
590 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
598 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
603 return 32*4 + (sectorNo
- 32) * 16;
607 uint8_t SectorTrailer(uint8_t blockNo
)
609 if (blockNo
< 32*4) {
610 return (blockNo
| 0x03);
612 return (blockNo
| 0x0f);
616 bool IsSectorTrailer(uint8_t blockNo
)
618 return (blockNo
== SectorTrailer(blockNo
));
621 // work with emulator memory
622 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
623 uint8_t* emCARD
= BigBuf_get_EM_addr();
624 memcpy(emCARD
+ blockNum
* 16, data
, blocksCount
* 16);
627 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
628 uint8_t* emCARD
= BigBuf_get_EM_addr();
629 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
632 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
633 uint8_t* emCARD
= BigBuf_get_EM_addr();
634 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
637 int emlCheckValBl(int blockNum
) {
638 uint8_t* emCARD
= BigBuf_get_EM_addr();
639 uint8_t* data
= emCARD
+ blockNum
* 16;
641 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
642 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
643 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
644 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
645 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
646 (data
[12] != (data
[15] ^ 0xff))
652 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
653 uint8_t* emCARD
= BigBuf_get_EM_addr();
654 uint8_t* data
= emCARD
+ blockNum
* 16;
656 if (emlCheckValBl(blockNum
)) {
660 memcpy(blReg
, data
, 4);
665 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
666 uint8_t* emCARD
= BigBuf_get_EM_addr();
667 uint8_t* data
= emCARD
+ blockNum
* 16;
669 memcpy(data
+ 0, &blReg
, 4);
670 memcpy(data
+ 8, &blReg
, 4);
671 blReg
= blReg
^ 0xffffffff;
672 memcpy(data
+ 4, &blReg
, 4);
675 data
[13] = blBlock
^ 0xff;
677 data
[15] = blBlock
^ 0xff;
682 uint64_t emlGetKey(int sectorNum
, int keyType
) {
684 uint8_t* emCARD
= BigBuf_get_EM_addr();
686 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
687 return bytes_to_num(key
, 6);
690 void emlClearMem(void) {
693 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
694 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
695 uint8_t* emCARD
= BigBuf_get_EM_addr();
697 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
699 // fill sectors trailer data
700 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
701 emlSetMem((uint8_t *)trailer
, b
, 1);
705 emlSetMem((uint8_t *)uid
, 0, 1);
710 // Mifare desfire commands
711 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
)
713 uint8_t dcmd
[5] = {0x00};
715 memcpy(dcmd
+1,data
,2);
716 AppendCrc14443a(dcmd
, 3);
718 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
719 int len
= ReaderReceive(answer
, answer_parity
);
721 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
722 Dbprintf("Authentication failed. Card timeout.");
728 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
)
730 uint8_t dcmd
[20] = {0x00};
732 memcpy(dcmd
+1,data
,17);
733 AppendCrc14443a(dcmd
, 18);
735 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
736 int len
= ReaderReceive(answer
, answer_parity
);
738 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
739 Dbprintf("Authentication failed. Card timeout.");
745 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
748 // load key, keynumber
749 uint8_t data
[2]={0x0a, 0x00};
750 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
751 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
753 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
755 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
756 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
761 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
762 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
763 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
764 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
765 receivedAnswer
[10],receivedAnswer
[11]);
767 memcpy(blockData
, receivedAnswer
, 12);
773 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
776 uint8_t data
[17] = {0x00};
778 memcpy(data
+1,key
,16);
780 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
781 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
783 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
785 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
786 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
787 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
792 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
793 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
794 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
795 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
796 receivedAnswer
[10],receivedAnswer
[11]);
798 memcpy(blockData
, receivedAnswer
, 12);
804 //-----------------------------------------------------------------------------
807 //-----------------------------------------------------------------------------
809 int MifareChkBlockKey(uint8_t *uid
, uint32_t *cuid
, uint8_t *cascade_levels
, uint64_t ui64Key
, uint8_t blockNo
, uint8_t keyType
, uint8_t debugLevel
) {
811 struct Crypto1State mpcs
= {0, 0};
812 struct Crypto1State
*pcs
;
815 // Iceman: use piwi's faster nonce collecting part in hardnested.
816 if (*cascade_levels
== 0) { // need a full select cycle to get the uid first
817 iso14a_card_select_t card_info
;
818 if(!iso14443a_select_card(uid
, &card_info
, cuid
, true, 0, true)) {
819 if (debugLevel
>= 1) Dbprintf("ChkKeys: Can't select card");
822 switch (card_info
.uidlen
) {
823 case 4 : *cascade_levels
= 1; break;
824 case 7 : *cascade_levels
= 2; break;
825 case 10: *cascade_levels
= 3; break;
828 } else { // no need for anticollision. We can directly select the card
829 if(!iso14443a_select_card(uid
, NULL
, NULL
, false, *cascade_levels
, true)) {
830 if (debugLevel
>= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels
);
835 if(mifare_classic_auth(pcs
, *cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
)) {
836 // SpinDelayUs(AUTHENTICATION_TIMEOUT); // it not needs because mifare_classic_auth have timeout from iso14a_set_timeout()
839 /* // let it be here. it like halt command, but maybe it will work in some strange cases
840 uint8_t dummy_answer = 0;
841 ReaderTransmit(&dummy_answer, 1, NULL);
842 int timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;
843 // wait for the card to become ready again
844 while(GetCountSspClk() < timeout) {};
846 // it needs after success authentication
847 mifare_classic_halt(pcs
, *cuid
);
854 int MifareChkBlockKeys(uint8_t *keys
, uint8_t keyCount
, uint8_t blockNo
, uint8_t keyType
, uint8_t debugLevel
) {
857 uint8_t cascade_levels
= 0;
858 uint64_t ui64Key
= 0;
861 for (uint8_t i
= 0; i
< keyCount
; i
++) {
863 // Allow button press / usb cmd to interrupt device
864 if (BUTTON_PRESS() && !usb_poll_validate_length()) {
865 Dbprintf("ChkKeys: Cancel operation. Exit...");
869 ui64Key
= bytes_to_num(keys
+ i
* 6, 6);
870 int res
= MifareChkBlockKey(uid
, &cuid
, &cascade_levels
, ui64Key
, blockNo
, keyType
, debugLevel
);
875 if (retryCount
>= 5) {
876 Dbprintf("ChkKeys: block=%d key=%d. Can't select. Exit...", blockNo
, keyType
);
879 --i
; // try the same key once again
882 // Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);
886 // can't authenticate
889 continue; // can't auth. wrong key.
898 // multisector multikey check
899 int MifareMultisectorChk(uint8_t *keys
, uint8_t keyCount
, uint8_t SectorCount
, uint8_t keyType
, uint8_t debugLevel
, TKeyIndex
*keyIndex
) {
902 // int clk = GetCountSspClk();
904 for(int sc
= 0; sc
< SectorCount
; sc
++){
909 res
= MifareChkBlockKeys(keys
, keyCount
, FirstBlockOfSector(sc
), keyAB
& 0x01, debugLevel
);
914 (*keyIndex
)[keyAB
& 0x01][sc
] = res
;
916 } while(--keyAB
> 0);
919 // Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));