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
));
116 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
118 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
121 int len
= ReaderReceive(answer
, par
);
123 if (answer_parity
) *answer_parity
= par
[0];
125 if (crypted
== CRYPT_ALL
) {
128 for (pos
= 0; pos
< 4; pos
++)
129 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
134 for (pos
= 0; pos
< len
; pos
++)
136 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
145 // mifare classic commands
146 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
, uint32_t *auth_timeout
) {
148 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
, auth_timeout
);
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
, uint32_t *auth_timeout
) {
156 uint8_t par
[1] = {0x00};
158 uint32_t nt
, ntpp
; // Supplied tag nonce
160 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
161 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
162 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
164 // Transmit MIFARE_CLASSIC_AUTH
165 len
= mifare_sendcmd_short(pcs
, isNested
, keyType
& 0x01 ? MIFARE_AUTH_KEYB
: MIFARE_AUTH_KEYA
, blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
166 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
167 if (len
!= 4) return 1;
169 // "random" reader nonce:
175 // Save the tag nonce (nt)
176 nt
= bytes_to_num(receivedAnswer
, 4);
178 // ----------------------------- crypto1 create
180 crypto1_destroy(pcs
);
182 // Init cipher with key
183 crypto1_create(pcs
, ui64Key
);
185 if (isNested
== AUTH_NESTED
) {
186 // decrypt nt with help of new key
187 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
189 // Load (plain) uid^nt into the cipher
190 crypto1_word(pcs
, nt
^ uid
, 0);
194 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
195 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
201 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
203 for (pos
= 0; pos
< 4; pos
++) {
204 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
205 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nr
[pos
])) & 0x01) << (7-pos
));
208 // Skip 32 bits in pseudo random generator
209 nt
= prng_successor(nt
,32);
212 for (pos
= 4; pos
< 8; pos
++) {
213 nt
= prng_successor(nt
,8);
214 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
215 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
)) & 0x01) << (7-pos
));
218 // Transmit reader nonce and reader answer
219 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
221 // Receive 4 byte tag answer
222 uint32_t save_timeout
= iso14a_get_timeout(); // save standard timeout
223 if (auth_timeout
&& *auth_timeout
) {
224 iso14a_set_timeout(*auth_timeout
); // set timeout for authentication response
226 uint32_t auth_timeout_start
= GetCountSspClk();
227 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
228 iso14a_set_timeout(save_timeout
); // restore standard timeout
230 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
233 if (auth_timeout
&& !*auth_timeout
) { // measure time for future authentication response timeout
234 *auth_timeout
= (GetCountSspClk() - auth_timeout_start
- (len
* 9 + 2) * 8) / 8 + 1;
237 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0, 0);
239 if (ntpp
!= bytes_to_num(receivedAnswer
, 4)) {
240 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
248 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
) {
253 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
254 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
256 // command MIFARE_CLASSIC_READBLOCK
257 len
= mifare_sendcmd_short(pcs
, 1, MIFARE_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
259 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
263 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
267 memcpy(bt
, receivedAnswer
+ 16, 2);
268 AppendCrc14443a(receivedAnswer
, 16);
269 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
270 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
274 memcpy(blockData
, receivedAnswer
, 16);
278 // mifare ultralight commands
279 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
284 uint8_t key
[4] = {0x00};
285 memcpy(key
, keybytes
, 4);
287 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
288 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
289 len
= mifare_sendcmd(MIFARE_ULEV1_AUTH
, key
, sizeof(key
), resp
, respPar
, NULL
);
290 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
292 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
296 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
297 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
299 memcpy(pack
, resp
, 4);
303 int mifare_ultra_auth(uint8_t *keybytes
){
307 mbedtls_des3_context ctx
= { {0} };
308 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
309 uint8_t random_b
[8] = {0x00};
310 uint8_t enc_random_b
[8] = {0x00};
311 uint8_t rnd_ab
[16] = {0x00};
312 uint8_t IV
[8] = {0x00};
313 uint8_t key
[16] = {0x00};
314 memcpy(key
, keybytes
, 16);
317 uint8_t resp
[19] = {0x00};
318 uint8_t respPar
[3] = {0,0,0};
320 // REQUEST AUTHENTICATION
321 len
= mifare_sendcmd_short(NULL
, 1, MIFARE_ULC_AUTH_1
, 0x00, resp
, respPar
,NULL
);
323 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
328 memcpy(enc_random_b
,resp
+1,8);
331 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
332 mbedtls_des3_set2key_dec(&ctx
, key
);
333 mbedtls_des3_crypt_cbc(&ctx
// des3_context
334 , MBEDTLS_DES_DECRYPT
// int mode
335 , sizeof(random_b
) // length
337 , enc_random_b
// input
342 memcpy(rnd_ab
,random_a
,8);
343 memcpy(rnd_ab
+8,random_b
,8);
345 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
346 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
347 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]);
349 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
350 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
352 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
353 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
355 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
356 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
359 // encrypt out, in, length, key, iv
360 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
361 mbedtls_des3_set2key_enc(&ctx
, key
);
362 mbedtls_des3_crypt_cbc(&ctx
// des3_context
363 , MBEDTLS_DES_ENCRYPT
// int mode
364 , sizeof(rnd_ab
) // length
365 , enc_random_b
// iv[8]
370 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
371 len
= mifare_sendcmd(MIFARE_ULC_AUTH_2
, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
373 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
377 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
378 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
379 memcpy(enc_resp
, resp
+1, 8);
381 // decrypt out, in, length, key, iv
382 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
383 mbedtls_des3_set2key_dec(&ctx
, key
);
384 mbedtls_des3_crypt_cbc(&ctx
// des3_context
385 , MBEDTLS_DES_DECRYPT
// int mode
387 , enc_random_b
// iv[8]
389 , resp_random_a
// output
391 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
392 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
396 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
397 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
398 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
399 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
401 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
402 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
403 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
405 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
406 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
407 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
409 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
410 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
411 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
417 #define MFU_MAX_RETRIES 5
418 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
)
422 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
423 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
427 for (retries
= 0; retries
< MFU_MAX_RETRIES
; retries
++) {
428 len
= mifare_sendcmd_short(NULL
, 1, MIFARE_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
430 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
435 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
440 memcpy(bt
, receivedAnswer
+ 16, 2);
441 AppendCrc14443a(receivedAnswer
, 16);
442 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
443 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
448 // No errors encountered; don't retry
454 Dbprintf("Cmd Error: too many retries; read failed");
458 memcpy(blockData
, receivedAnswer
, 16);
462 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
467 uint8_t par
[3] = {0}; // enough for 18 Bytes to send
470 uint8_t d_block
[18], d_block_enc
[18];
471 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
472 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
474 // command MIFARE_CLASSIC_WRITEBLOCK
475 len
= mifare_sendcmd_short(pcs
, 1, MIFARE_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
477 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
478 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
482 memcpy(d_block
, blockData
, 16);
483 AppendCrc14443a(d_block
, 16);
486 for (pos
= 0; pos
< 18; pos
++)
488 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
489 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
492 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
494 // Receive the response
495 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
498 for (i
= 0; i
< 4; i
++)
499 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
501 if ((len
!= 1) || (res
!= 0x0A)) {
502 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
509 /* // command not needed, but left for future testing
510 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
513 uint8_t par[3] = {0}; // enough for 18 parity bits
514 uint8_t d_block[18] = {0x00};
515 uint8_t receivedAnswer[MAX_FRAME_SIZE];
516 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
518 len = mifare_sendcmd_short(NULL, true, MIFARE_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);
520 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
521 if (MF_DBGLEVEL >= MF_DBG_ERROR)
522 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
526 memcpy(d_block, blockData, 16);
527 AppendCrc14443a(d_block, 16);
529 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
531 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
533 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
534 if (MF_DBGLEVEL >= MF_DBG_ERROR)
535 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
542 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)
545 uint8_t d_block
[5] = {0x00};
546 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
547 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
549 // command MIFARE_CLASSIC_WRITEBLOCK
551 memcpy(d_block
+1,blockData
,4);
552 //AppendCrc14443a(d_block, 6);
554 len
= mifare_sendcmd(0xA2, d_block
, sizeof(d_block
), receivedAnswer
, receivedAnswerPar
, NULL
);
556 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
557 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
558 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
564 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
)
567 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
568 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
570 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, ISO14443A_CMD_HALT
, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
572 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
573 Dbprintf("halt error. response len: %x", len
);
580 int mifare_ultra_halt()
583 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
584 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
586 len
= mifare_sendcmd_short(NULL
, true, ISO14443A_CMD_HALT
, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
588 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
589 Dbprintf("halt error. response len: %x", len
);
596 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
597 // plus evtl. 8 sectors with 16 blocks each (4k cards)
598 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
606 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
611 return 32*4 + (sectorNo
- 32) * 16;
615 uint8_t SectorTrailer(uint8_t blockNo
)
617 if (blockNo
< 32*4) {
618 return (blockNo
| 0x03);
620 return (blockNo
| 0x0f);
624 bool IsSectorTrailer(uint8_t blockNo
)
626 return (blockNo
== SectorTrailer(blockNo
));
629 // work with emulator memory
630 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
631 uint8_t* emCARD
= BigBuf_get_EM_addr();
632 memcpy(emCARD
+ blockNum
* 16, data
, blocksCount
* 16);
635 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
636 uint8_t* emCARD
= BigBuf_get_EM_addr();
637 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
640 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
641 uint8_t* emCARD
= BigBuf_get_EM_addr();
642 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
645 int emlCheckValBl(int blockNum
) {
646 uint8_t* emCARD
= BigBuf_get_EM_addr();
647 uint8_t* data
= emCARD
+ blockNum
* 16;
649 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
650 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
651 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
652 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
653 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
654 (data
[12] != (data
[15] ^ 0xff))
660 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
661 uint8_t* emCARD
= BigBuf_get_EM_addr();
662 uint8_t* data
= emCARD
+ blockNum
* 16;
664 if (emlCheckValBl(blockNum
)) {
668 memcpy(blReg
, data
, 4);
673 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
674 uint8_t* emCARD
= BigBuf_get_EM_addr();
675 uint8_t* data
= emCARD
+ blockNum
* 16;
677 memcpy(data
+ 0, &blReg
, 4);
678 memcpy(data
+ 8, &blReg
, 4);
679 blReg
= blReg
^ 0xffffffff;
680 memcpy(data
+ 4, &blReg
, 4);
683 data
[13] = blBlock
^ 0xff;
685 data
[15] = blBlock
^ 0xff;
690 uint64_t emlGetKey(int sectorNum
, int keyType
) {
692 uint8_t* emCARD
= BigBuf_get_EM_addr();
694 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
695 return bytes_to_num(key
, 6);
698 void emlClearMem(void) {
701 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
702 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
703 uint8_t* emCARD
= BigBuf_get_EM_addr();
705 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
707 // fill sectors trailer data
708 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
709 emlSetMem((uint8_t *)trailer
, b
, 1);
713 emlSetMem((uint8_t *)uid
, 0, 1);
718 // Mifare desfire commands
719 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
)
721 uint8_t dcmd
[5] = {0x00};
723 memcpy(dcmd
+1,data
,2);
724 AppendCrc14443a(dcmd
, 3);
726 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
727 int len
= ReaderReceive(answer
, answer_parity
);
729 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
730 Dbprintf("Authentication failed. Card timeout.");
736 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
)
738 uint8_t dcmd
[20] = {0x00};
740 memcpy(dcmd
+1,data
,17);
741 AppendCrc14443a(dcmd
, 18);
743 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
744 int len
= ReaderReceive(answer
, answer_parity
);
746 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
747 Dbprintf("Authentication failed. Card timeout.");
753 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
756 // load key, keynumber
757 uint8_t data
[2]={0x0a, 0x00};
758 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
759 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
761 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
763 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
764 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
769 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
770 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
771 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
772 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
773 receivedAnswer
[10],receivedAnswer
[11]);
775 memcpy(blockData
, receivedAnswer
, 12);
781 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
784 uint8_t data
[17] = {0x00};
786 memcpy(data
+1,key
,16);
788 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
789 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
791 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
793 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
794 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
795 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
800 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
801 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
802 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
803 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
804 receivedAnswer
[10],receivedAnswer
[11]);
806 memcpy(blockData
, receivedAnswer
, 12);
812 //-----------------------------------------------------------------------------
815 //-----------------------------------------------------------------------------
817 int MifareChkBlockKey(uint8_t *uid
, uint32_t *cuid
, uint8_t *cascade_levels
, uint64_t ui64Key
, uint8_t blockNo
, uint8_t keyType
, uint32_t *auth_timeout
, uint8_t debugLevel
) {
819 struct Crypto1State mpcs
= {0, 0};
820 struct Crypto1State
*pcs
;
823 // Iceman: use piwi's faster nonce collecting part in hardnested.
824 if (*cascade_levels
== 0) { // need a full select cycle to get the uid first
825 iso14a_card_select_t card_info
;
826 if (!iso14443a_select_card(uid
, &card_info
, cuid
, true, 0, true)) {
827 if (debugLevel
>= 1) Dbprintf("ChkKeys: Can't select card");
830 switch (card_info
.uidlen
) {
831 case 4 : *cascade_levels
= 1; break;
832 case 7 : *cascade_levels
= 2; break;
833 case 10: *cascade_levels
= 3; break;
836 } else { // no need for anticollision. We can directly select the card
837 if (!iso14443a_select_card(uid
, NULL
, NULL
, false, *cascade_levels
, true)) {
838 if (debugLevel
>= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels
);
843 if (mifare_classic_auth(pcs
, *cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, auth_timeout
)) { // authentication failed
846 mifare_classic_halt(pcs
, *cuid
);
854 int MifareChkBlockKeys(uint8_t *keys
, uint8_t keyCount
, uint8_t blockNo
, uint8_t keyType
, uint32_t *auth_timeout
, uint8_t debugLevel
) {
858 uint8_t cascade_levels
= 0;
859 uint64_t ui64Key
= 0;
862 for (uint8_t i
= 0; i
< keyCount
; i
++) {
864 ui64Key
= bytes_to_num(keys
+ i
* 6, 6);
865 int res
= MifareChkBlockKey(uid
, &cuid
, &cascade_levels
, ui64Key
, blockNo
, keyType
, auth_timeout
, debugLevel
);
870 if (retryCount
>= 5) {
871 Dbprintf("ChkKeys: block=%d key=%d. Can't select. Exit...", blockNo
, keyType
);
874 --i
; // try the same key once again
877 // Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);
881 // can't authenticate
884 continue; // can't auth. wrong key.
887 // successful authentication
891 if (BUTTON_PRESS()) {
899 // multisector multikey check
900 int MifareMultisectorChk(uint8_t *keys
, uint8_t keyCount
, uint8_t SectorCount
, uint8_t keyType
, uint32_t *auth_timeout
, uint8_t debugLevel
, TKeyIndex
*keyIndex
) {
903 // int clk = GetCountSspClk();
905 for(int sc
= 0; sc
< SectorCount
; sc
++){
910 res
= MifareChkBlockKeys(keys
, keyCount
, FirstBlockOfSector(sc
), keyAB
& 0x01, auth_timeout
, debugLevel
);
915 (*keyIndex
)[keyAB
& 0x01][sc
] = res
;
917 } while(--keyAB
> 0);
920 // Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));