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
) {
82 uint8_t dcmd
[data_size
+3];
84 memcpy(dcmd
+1,data
,data_size
);
85 AppendCrc14443a(dcmd
, data_size
+1);
86 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
87 int len
= ReaderReceive(answer
, answer_parity
);
89 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("%02X Cmd failed. Card timeout.", cmd
);
90 len
= ReaderReceive(answer
,answer_parity
);
96 // send 2 byte commands
97 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
) {
98 uint8_t dcmd
[4], ecmd
[4];
100 uint8_t par
[1]; // 1 Byte parity is enough here
103 AppendCrc14443a(dcmd
, 2);
105 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
109 for (pos
= 0; pos
< 4; pos
++)
111 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
112 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(dcmd
[pos
])) & 0x01) << (7-pos
));
114 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
116 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
119 int len
= ReaderReceive(answer
, par
);
121 if (answer_parity
) *answer_parity
= par
[0];
123 if (crypted
== CRYPT_ALL
) {
126 for (pos
= 0; pos
< 4; pos
++)
127 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
132 for (pos
= 0; pos
< len
; pos
++)
134 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
143 // mifare classic commands
144 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
) {
146 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
, auth_timeout
);
150 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
) {
154 uint8_t par
[1] = {0x00};
156 uint32_t nt
, ntpp
; // Supplied tag nonce
158 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
159 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
160 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
162 // Transmit MIFARE_CLASSIC_AUTH
163 len
= mifare_sendcmd_short(pcs
, isNested
, keyType
& 0x01 ? MIFARE_AUTH_KEYB
: MIFARE_AUTH_KEYA
, blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
164 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
165 if (len
!= 4) return 1;
167 // "random" reader nonce:
173 // Save the tag nonce (nt)
174 nt
= bytes_to_num(receivedAnswer
, 4);
176 // ----------------------------- crypto1 create
178 crypto1_destroy(pcs
);
180 // Init cipher with key
181 crypto1_create(pcs
, ui64Key
);
183 if (isNested
== AUTH_NESTED
) {
184 // decrypt nt with help of new key
185 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
187 // Load (plain) uid^nt into the cipher
188 crypto1_word(pcs
, nt
^ uid
, 0);
192 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
193 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
199 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
201 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
++) {
211 nt
= prng_successor(nt
,8);
212 mf_nr_ar
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ (nt
& 0xff);
213 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
)) & 0x01) << (7-pos
));
216 // Transmit reader nonce and reader answer
217 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
219 // Receive 4 byte tag answer
220 uint32_t save_timeout
= iso14a_get_timeout(); // save standard timeout
221 if (auth_timeout
&& *auth_timeout
) {
222 iso14a_set_timeout(*auth_timeout
); // set timeout for authentication response
224 uint32_t auth_timeout_start
= GetCountSspClk();
225 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
226 iso14a_set_timeout(save_timeout
); // restore standard timeout
228 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
231 if (auth_timeout
&& !*auth_timeout
) { // measure time for future authentication response timeout
232 *auth_timeout
= (GetCountSspClk() - auth_timeout_start
- (len
* 9 + 2) * 8) / 8 + 1;
235 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0, 0);
237 if (ntpp
!= bytes_to_num(receivedAnswer
, 4)) {
238 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
246 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
) {
251 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
252 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
254 // command MIFARE_CLASSIC_READBLOCK
255 len
= mifare_sendcmd_short(pcs
, 1, MIFARE_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
257 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
261 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
265 memcpy(bt
, receivedAnswer
+ 16, 2);
266 AppendCrc14443a(receivedAnswer
, 16);
267 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
268 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
272 memcpy(blockData
, receivedAnswer
, 16);
276 // mifare ultralight commands
277 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
282 uint8_t key
[4] = {0x00};
283 memcpy(key
, keybytes
, 4);
285 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
286 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
287 len
= mifare_sendcmd(MIFARE_ULEV1_AUTH
, key
, sizeof(key
), resp
, respPar
, NULL
);
288 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
290 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
294 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
295 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
297 memcpy(pack
, resp
, 4);
301 int mifare_ultra_auth(uint8_t *keybytes
){
305 mbedtls_des3_context ctx
= { {0} };
306 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
307 uint8_t random_b
[8] = {0x00};
308 uint8_t enc_random_b
[8] = {0x00};
309 uint8_t rnd_ab
[16] = {0x00};
310 uint8_t IV
[8] = {0x00};
311 uint8_t key
[16] = {0x00};
312 memcpy(key
, keybytes
, 16);
315 uint8_t resp
[19] = {0x00};
316 uint8_t respPar
[3] = {0,0,0};
318 // REQUEST AUTHENTICATION
319 len
= mifare_sendcmd_short(NULL
, 1, MIFARE_ULC_AUTH_1
, 0x00, resp
, respPar
,NULL
);
321 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
326 memcpy(enc_random_b
,resp
+1,8);
329 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
330 mbedtls_des3_set2key_dec(&ctx
, key
);
331 mbedtls_des3_crypt_cbc(&ctx
// des3_context
332 , MBEDTLS_DES_DECRYPT
// int mode
333 , sizeof(random_b
) // length
335 , enc_random_b
// input
340 memcpy(rnd_ab
,random_a
,8);
341 memcpy(rnd_ab
+8,random_b
,8);
343 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
344 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
345 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]);
347 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
348 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
350 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
351 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
353 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
354 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
357 // encrypt out, in, length, key, iv
358 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
359 mbedtls_des3_set2key_enc(&ctx
, key
);
360 mbedtls_des3_crypt_cbc(&ctx
// des3_context
361 , MBEDTLS_DES_ENCRYPT
// int mode
362 , sizeof(rnd_ab
) // length
363 , enc_random_b
// iv[8]
368 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
369 len
= mifare_sendcmd(MIFARE_ULC_AUTH_2
, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
371 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
375 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
376 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
377 memcpy(enc_resp
, resp
+1, 8);
379 // decrypt out, in, length, key, iv
380 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
381 mbedtls_des3_set2key_dec(&ctx
, key
);
382 mbedtls_des3_crypt_cbc(&ctx
// des3_context
383 , MBEDTLS_DES_DECRYPT
// int mode
385 , enc_random_b
// iv[8]
387 , resp_random_a
// output
389 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
390 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
394 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
395 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
396 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
397 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
399 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
400 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
401 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
403 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
404 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
405 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
407 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
408 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
409 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
415 #define MFU_MAX_RETRIES 5
416 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
)
420 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
421 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
425 for (retries
= 0; retries
< MFU_MAX_RETRIES
; retries
++) {
426 len
= mifare_sendcmd_short(NULL
, 1, MIFARE_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
428 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
433 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
438 memcpy(bt
, receivedAnswer
+ 16, 2);
439 AppendCrc14443a(receivedAnswer
, 16);
440 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
441 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
446 // No errors encountered; don't retry
452 Dbprintf("Cmd Error: too many retries; read failed");
456 memcpy(blockData
, receivedAnswer
, 16);
460 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
465 uint8_t par
[3] = {0}; // enough for 18 Bytes to send
468 uint8_t d_block
[18], d_block_enc
[18];
469 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
470 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
472 // command MIFARE_CLASSIC_WRITEBLOCK
473 len
= mifare_sendcmd_short(pcs
, 1, MIFARE_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
475 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
476 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
480 memcpy(d_block
, blockData
, 16);
481 AppendCrc14443a(d_block
, 16);
484 for (pos
= 0; pos
< 18; pos
++)
486 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
487 par
[pos
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(d_block
[pos
])) & 0x01) << (7 - (pos
&0x0007)));
490 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
492 // Receive the response
493 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
496 for (i
= 0; i
< 4; i
++)
497 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
499 if ((len
!= 1) || (res
!= 0x0A)) {
500 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
507 /* // command not needed, but left for future testing
508 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
511 uint8_t par[3] = {0}; // enough for 18 parity bits
512 uint8_t d_block[18] = {0x00};
513 uint8_t receivedAnswer[MAX_FRAME_SIZE];
514 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
516 len = mifare_sendcmd_short(NULL, true, MIFARE_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);
518 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
519 if (MF_DBGLEVEL >= MF_DBG_ERROR)
520 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
524 memcpy(d_block, blockData, 16);
525 AppendCrc14443a(d_block, 16);
527 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
529 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
531 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
532 if (MF_DBGLEVEL >= MF_DBG_ERROR)
533 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
540 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)
543 uint8_t d_block
[5] = {0x00};
544 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
545 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
547 // command MIFARE_CLASSIC_WRITEBLOCK
549 memcpy(d_block
+1,blockData
,4);
550 //AppendCrc14443a(d_block, 6);
552 len
= mifare_sendcmd(0xA2, d_block
, sizeof(d_block
), receivedAnswer
, receivedAnswerPar
, NULL
);
554 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
555 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
556 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
562 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
)
565 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
566 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
568 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, ISO14443A_CMD_HALT
, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
570 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
571 Dbprintf("halt error. response len: %x", len
);
578 int mifare_ultra_halt()
581 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
582 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
584 len
= mifare_sendcmd_short(NULL
, true, ISO14443A_CMD_HALT
, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
586 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
587 Dbprintf("halt error. response len: %x", len
);
594 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
595 // plus evtl. 8 sectors with 16 blocks each (4k cards)
596 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
604 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
609 return 32*4 + (sectorNo
- 32) * 16;
613 uint8_t SectorTrailer(uint8_t blockNo
)
615 if (blockNo
< 32*4) {
616 return (blockNo
| 0x03);
618 return (blockNo
| 0x0f);
622 bool IsSectorTrailer(uint8_t blockNo
)
624 return (blockNo
== SectorTrailer(blockNo
));
627 // work with emulator memory
628 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
629 uint8_t* emCARD
= BigBuf_get_EM_addr();
630 memcpy(emCARD
+ blockNum
* 16, data
, blocksCount
* 16);
633 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
634 uint8_t* emCARD
= BigBuf_get_EM_addr();
635 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
638 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
639 uint8_t* emCARD
= BigBuf_get_EM_addr();
640 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
643 int emlCheckValBl(int blockNum
) {
644 uint8_t* emCARD
= BigBuf_get_EM_addr();
645 uint8_t* data
= emCARD
+ blockNum
* 16;
647 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
648 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
649 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
650 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
651 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
652 (data
[12] != (data
[15] ^ 0xff))
658 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
659 uint8_t* emCARD
= BigBuf_get_EM_addr();
660 uint8_t* data
= emCARD
+ blockNum
* 16;
662 if (emlCheckValBl(blockNum
)) {
666 memcpy(blReg
, data
, 4);
671 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
672 uint8_t* emCARD
= BigBuf_get_EM_addr();
673 uint8_t* data
= emCARD
+ blockNum
* 16;
675 memcpy(data
+ 0, &blReg
, 4);
676 memcpy(data
+ 8, &blReg
, 4);
677 blReg
= blReg
^ 0xffffffff;
678 memcpy(data
+ 4, &blReg
, 4);
681 data
[13] = blBlock
^ 0xff;
683 data
[15] = blBlock
^ 0xff;
688 uint64_t emlGetKey(int sectorNum
, int keyType
) {
690 uint8_t* emCARD
= BigBuf_get_EM_addr();
692 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
693 return bytes_to_num(key
, 6);
696 void emlClearMem(void) {
699 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
700 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
701 uint8_t* emCARD
= BigBuf_get_EM_addr();
703 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
705 // fill sectors trailer data
706 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
707 emlSetMem((uint8_t *)trailer
, b
, 1);
711 emlSetMem((uint8_t *)uid
, 0, 1);
716 // Mifare desfire commands
717 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
)
719 uint8_t dcmd
[5] = {0x00};
721 memcpy(dcmd
+1,data
,2);
722 AppendCrc14443a(dcmd
, 3);
724 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
725 int len
= ReaderReceive(answer
, answer_parity
);
727 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
728 Dbprintf("Authentication failed. Card timeout.");
734 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
)
736 uint8_t dcmd
[20] = {0x00};
738 memcpy(dcmd
+1,data
,17);
739 AppendCrc14443a(dcmd
, 18);
741 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
742 int len
= ReaderReceive(answer
, answer_parity
);
744 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
745 Dbprintf("Authentication failed. Card timeout.");
751 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
754 // load key, keynumber
755 uint8_t data
[2]={0x0a, 0x00};
756 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
757 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
759 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
761 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
762 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
767 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
768 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
769 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
770 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
771 receivedAnswer
[10],receivedAnswer
[11]);
773 memcpy(blockData
, receivedAnswer
, 12);
779 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
782 uint8_t data
[17] = {0x00};
784 memcpy(data
+1,key
,16);
786 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
787 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
789 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
791 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
792 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
793 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
798 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
799 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
800 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
801 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
802 receivedAnswer
[10],receivedAnswer
[11]);
804 memcpy(blockData
, receivedAnswer
, 12);
810 //-----------------------------------------------------------------------------
813 //-----------------------------------------------------------------------------
815 static int MifareChkBlockKey(uint8_t *uid
, uint32_t *cuid
, uint8_t *cascade_levels
, uint8_t *key
, uint8_t blockNo
, uint8_t keyType
, uint32_t *auth_timeout
, uint8_t debugLevel
, bool fixed_nonce
) {
817 struct Crypto1State mpcs
= {0, 0};
818 struct Crypto1State
*pcs
;
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
);
841 uint64_t ui64Key
= bytes_to_num(key
, 6);
842 if (mifare_classic_auth(pcs
, *cuid
, blockNo
, keyType
, ui64Key
, AUTH_FIRST
, auth_timeout
)) { // authentication failed
845 mifare_classic_halt(pcs
, *cuid
);
848 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
849 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
850 // Transmit MIFARE_CLASSIC_AUTH
851 int len
= mifare_sendcmd_short(pcs
, false, keyType
& 0x01 ? MIFARE_AUTH_KEYB
: MIFARE_AUTH_KEYA
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
852 if (len
!= 4) return -2;
853 // Transmit encrypted reader nonce and reader answer
854 uint8_t mf_nr_ar
[8] = NESTED_FIXED_NR_ENC
;
855 memcpy(mf_nr_ar
+ 4, key
, 4);
856 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), key
+ 4, NULL
);
857 uint32_t save_timeout
= iso14a_get_timeout(); // save standard timeout
858 iso14a_set_timeout(*auth_timeout
); // set timeout for authentication response
859 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
860 iso14a_set_timeout(save_timeout
); // restore standard timeout
868 static int MifareChkBlockKeysEx(uint8_t *keys
, uint8_t keyCount
, uint8_t blockNo
, uint8_t keyType
, uint32_t *auth_timeout
, uint8_t debugLevel
, bool fixed_nonce
) {
872 uint8_t cascade_levels
= 0;
875 for (uint8_t i
= 0; i
< keyCount
; i
++) {
876 uint8_t bytes_per_key
= fixed_nonce
? 5 : 6;
877 int res
= MifareChkBlockKey(uid
, &cuid
, &cascade_levels
, keys
+ i
*bytes_per_key
, blockNo
, keyType
, auth_timeout
, debugLevel
, fixed_nonce
);
878 if (res
== -1) { // couldn't select
880 if (retryCount
>= 5) {
881 Dbprintf("ChkKeys: block=%d key=%d. Couldn't select. Exit...", blockNo
, keyType
);
884 --i
; // try the same key once again
886 // Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);
890 if (res
== -2) { // couldn't authenticate with this key
895 return i
+ 1; // successful authentication
899 if (BUTTON_PRESS()) {
903 return 0; // couldn't authenticate with any key
907 int MifareChkBlockKeys(uint8_t *keys
, uint8_t keyCount
, uint8_t blockNo
, uint8_t keyType
, uint32_t *auth_timeout
, uint8_t debugLevel
) {
908 return MifareChkBlockKeysEx(keys
, keyCount
, blockNo
, keyType
, auth_timeout
, debugLevel
, false);
913 int MifareChkBlockKeysFixedNonce(uint8_t *ar_par
, uint8_t ar_par_cnt
, uint8_t blockNo
, uint8_t keyType
, uint32_t *auth_timeout
, uint8_t debugLevel
) {
914 return MifareChkBlockKeysEx(ar_par
, ar_par_cnt
, blockNo
, keyType
, auth_timeout
, debugLevel
, true);
918 // multisector multikey check
919 int MifareMultisectorChk(uint8_t *keys
, uint8_t keyCount
, uint8_t SectorCount
, uint8_t keyType
, uint32_t *auth_timeout
, uint8_t debugLevel
, TKeyIndex
*keyIndex
) {
922 // int clk = GetCountSspClk();
924 for(int sc
= 0; sc
< SectorCount
; sc
++){
929 res
= MifareChkBlockKeys(keys
, keyCount
, FirstBlockOfSector(sc
), keyAB
& 0x01, auth_timeout
, debugLevel
);
934 (*keyIndex
)[keyAB
& 0x01][sc
] = res
;
936 } while(--keyAB
> 0);
939 // Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));