1 // Merlok, May 2011, 2012
2 // Many authors, whom made it possible
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Work with mifare cards.
9 //-----------------------------------------------------------------------------
10 #include "mifareutil.h"
14 #include "iso14443a.h"
18 #include "commonutil.h"
20 #include "protocols.h"
21 #include "desfire_crypto.h"
24 void mf_crypto1_decryptEx(struct Crypto1State
*pcs
, uint8_t *data_in
, int len
, uint8_t *data_out
) {
26 for (int i
= 0; i
< len
; i
++)
27 data_out
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data_in
[i
];
30 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data_in
[0], 0)) << 0;
31 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data_in
[0], 1)) << 1;
32 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data_in
[0], 2)) << 2;
33 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data_in
[0], 3)) << 3;
39 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
) {
40 mf_crypto1_decryptEx(pcs
, data
, len
, data
);
43 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) {
44 mf_crypto1_encryptEx(pcs
, data
, NULL
, data
, len
, par
);
47 void mf_crypto1_encryptEx(struct Crypto1State
*pcs
, uint8_t *data_in
, uint8_t *keystream
, uint8_t *data_out
, uint16_t len
, uint8_t *par
) {
51 for (i
= 0; i
< len
; i
++) {
52 uint8_t bt
= data_in
[i
];
53 data_out
[i
] = crypto1_byte(pcs
, keystream
? keystream
[i
] : 0x00, 0) ^ data_in
[i
];
54 if ((i
& 0x0007) == 0)
56 par
[ i
>> 3 ] |= (((filter(pcs
->odd
) ^ oddparity8(bt
)) & 0x01) << (7 - (i
& 0x0007)));
60 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
62 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, 0)) << 0;
63 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, 1)) << 1;
64 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, 2)) << 2;
65 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, 3)) << 3;
69 // send X byte basic commands
70 int mifare_sendcmd(uint8_t cmd
, uint8_t *data
, uint8_t data_size
, uint8_t *answer
, uint8_t *answer_parity
, uint32_t *timing
) {
72 uint8_t dcmd
[data_size
+ 3];
75 memcpy(dcmd
+ 1, data
, data_size
);
77 AddCrc14A(dcmd
, data_size
+ 1);
78 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
79 int len
= ReaderReceive(answer
, answer_parity
);
81 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("%02X Cmd failed. Card timeout.", cmd
);
82 len
= ReaderReceive(answer
, answer_parity
);
87 // send 2 byte commands
88 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
) {
90 uint8_t dcmd
[4] = {cmd
, data
, 0x00, 0x00};
91 uint8_t ecmd
[4] = {0x00, 0x00, 0x00, 0x00};
92 uint8_t par
[1] = {0x00}; // 1 Byte parity is enough here
94 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
98 for (pos
= 0; pos
< 4; pos
++) {
99 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
100 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(dcmd
[pos
])) & 0x01) << (7 - pos
));
102 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
104 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
107 int len
= ReaderReceive(answer
, par
);
109 if (answer_parity
) *answer_parity
= par
[0];
111 if (crypted
== CRYPT_ALL
) {
114 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 0)) << 0;
115 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 1)) << 1;
116 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 2)) << 2;
117 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], 3)) << 3;
120 for (pos
= 0; pos
< len
; pos
++)
121 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
127 // mifare classic commands
128 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
) {
129 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
132 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
) {
134 uint32_t pos
, nt
, ntpp
; // Supplied tag nonce
135 uint8_t par
[1] = {0x00};
137 uint8_t mf_nr_ar
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
138 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
139 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
141 // "random" reader nonce:
142 num_to_bytes(prng_successor(GetTickCount(), 32), 4, nr
);
144 // Transmit MIFARE_CLASSIC_AUTH
145 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
146 if (len
!= 4) return 1;
148 // Save the tag nonce (nt)
149 nt
= bytes_to_num(receivedAnswer
, 4);
151 // ----------------------------- crypto1 create
155 // Init cipher with key
156 crypto1_init(pcs
, ui64Key
);
158 if (isNested
== AUTH_NESTED
) {
159 // decrypt nt with help of new key
160 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
162 // Load (plain) uid^nt into the cipher
163 crypto1_word(pcs
, nt
^ uid
, 0);
167 if (!ntptr
&& (DBGLEVEL
>= DBG_EXTENDED
))
168 Dbprintf("auth uid: %08x | nr: %08x | nt: %08x", uid
, nr
, nt
);
174 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
176 for (pos
= 0; pos
< 4; pos
++) {
177 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
178 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nr
[pos
])) & 0x01) << (7 - pos
));
181 // Skip 32 bits in pseudo random generator
182 nt
= prng_successor(nt
, 32);
185 for (pos
= 4; pos
< 8; pos
++) {
186 nt
= prng_successor(nt
, 8);
187 mf_nr_ar
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ (nt
& 0xff);
188 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
& 0xff)) & 0x01) << (7 - pos
));
191 // Transmit reader nonce and reader answer
192 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
194 // save standard timeout
195 uint32_t save_timeout
= iso14a_get_timeout();
197 // set timeout for authentication response
198 if (save_timeout
> 103)
199 iso14a_set_timeout(103);
201 // Receive 4 byte tag answer
202 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
204 iso14a_set_timeout(save_timeout
);
207 if (DBGLEVEL
>= DBG_EXTENDED
) Dbprintf("Authentication failed. Card timeout");
211 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0, 0);
213 if (ntpp
!= bytes_to_num(receivedAnswer
, 4)) {
214 if (DBGLEVEL
>= DBG_EXTENDED
) Dbprintf("Authentication failed. Error card response");
220 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
) {
223 uint8_t bt
[2] = {0x00, 0x00};
224 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
225 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
227 len
= mifare_sendcmd_short(pcs
, 1, ISO14443A_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
229 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd Error %02x", receivedAnswer
[0]);
233 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("wrong response len %d (expected 18)", len
);
237 memcpy(bt
, receivedAnswer
+ 16, 2);
238 AddCrc14A(receivedAnswer
, 16);
239 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
240 if (DBGLEVEL
>= DBG_INFO
) Dbprintf("CRC response error");
244 memcpy(blockData
, receivedAnswer
, 16);
248 // mifare ultralight commands
249 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
) {
252 uint8_t resp
[4] = {0x00, 0x00, 0x00, 0x00};
253 uint8_t respPar
[1] = {0x00};
254 uint8_t key
[4] = {0x00, 0x00, 0x00, 0x00};
255 memcpy(key
, keybytes
, 4);
257 if (DBGLEVEL
>= DBG_EXTENDED
)
258 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
260 len
= mifare_sendcmd(MIFARE_ULEV1_AUTH
, key
, sizeof(key
), resp
, respPar
, NULL
);
263 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
267 if (DBGLEVEL
>= DBG_EXTENDED
)
268 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0], resp
[1], resp
[2], resp
[3]);
270 memcpy(pack
, resp
, 4);
274 int mifare_ultra_auth(uint8_t *keybytes
) {
277 uint8_t random_a
[8] = {1, 1, 1, 1, 1, 1, 1, 1};
278 uint8_t random_b
[8] = {0x00};
279 uint8_t enc_random_b
[8] = {0x00};
280 uint8_t rnd_ab
[16] = {0x00};
281 uint8_t IV
[8] = {0x00};
282 uint8_t key
[16] = {0x00};
283 memcpy(key
, keybytes
, 16);
286 uint8_t resp
[19] = {0x00};
287 uint8_t respPar
[3] = {0, 0, 0};
289 // REQUEST AUTHENTICATION
290 len
= mifare_sendcmd_short(NULL
, CRYPT_NONE
, MIFARE_ULC_AUTH_1
, 0x00, resp
, respPar
, NULL
);
292 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
297 memcpy(enc_random_b
, resp
+ 1, 8);
300 tdes_nxp_receive((void *)enc_random_b
, (void *)random_b
, sizeof(random_b
), (const void *)key
, IV
, 2);
302 memcpy(rnd_ab
, random_a
, 8);
303 memcpy(rnd_ab
+ 8, random_b
, 8);
305 if (DBGLEVEL
>= DBG_EXTENDED
) {
306 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
307 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]);
309 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
310 random_b
[0], random_b
[1], random_b
[2], random_b
[3], random_b
[4], random_b
[5], random_b
[6], random_b
[7]);
312 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
313 rnd_ab
[0], rnd_ab
[1], rnd_ab
[2], rnd_ab
[3], rnd_ab
[4], rnd_ab
[5], rnd_ab
[6], rnd_ab
[7]);
315 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
316 rnd_ab
[8], rnd_ab
[9], rnd_ab
[10], rnd_ab
[11], rnd_ab
[12], rnd_ab
[13], rnd_ab
[14], rnd_ab
[15]);
319 // encrypt out, in, length, key, iv
320 tdes_nxp_send(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
, 2);
322 len
= mifare_sendcmd(MIFARE_ULC_AUTH_2
, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
324 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
328 uint8_t enc_resp
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
329 uint8_t resp_random_a
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
330 memcpy(enc_resp
, resp
+ 1, 8);
332 // decrypt out, in, length, key, iv
333 tdes_nxp_receive(enc_resp
, resp_random_a
, 8, key
, enc_random_b
, 2);
334 if (memcmp(resp_random_a
, random_a
, 8) != 0) {
335 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("failed authentication");
339 if (DBGLEVEL
>= DBG_EXTENDED
) {
340 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
341 rnd_ab
[0], rnd_ab
[1], rnd_ab
[2], rnd_ab
[3],
342 rnd_ab
[4], rnd_ab
[5], rnd_ab
[6], rnd_ab
[7]);
344 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
345 rnd_ab
[8], rnd_ab
[9], rnd_ab
[10], rnd_ab
[11],
346 rnd_ab
[12], rnd_ab
[13], rnd_ab
[14], rnd_ab
[15]);
348 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
349 random_a
[0], random_a
[1], random_a
[2], random_a
[3],
350 random_a
[4], random_a
[5], random_a
[6], random_a
[7]);
352 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
353 resp_random_a
[0], resp_random_a
[1], resp_random_a
[2], resp_random_a
[3],
354 resp_random_a
[4], resp_random_a
[5], resp_random_a
[6], resp_random_a
[7]);
359 static int mifare_ultra_readblockEx(uint8_t blockNo
, uint8_t *blockData
) {
361 uint8_t bt
[2] = {0x00, 0x00};
362 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
363 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
365 len
= mifare_sendcmd_short(NULL
, CRYPT_NONE
, ISO14443A_CMD_READBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
367 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
371 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
375 memcpy(bt
, receivedAnswer
+ 16, 2);
376 AddCrc14A(receivedAnswer
, 16);
377 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
378 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd CRC response error.");
382 memcpy(blockData
, receivedAnswer
, 16);
385 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
) {
386 #define MFU_MAX_RETRIES 5
389 for (uint8_t retries
= 0; retries
< MFU_MAX_RETRIES
; ++retries
) {
390 res
= mifare_ultra_readblockEx(blockNo
, blockData
);
392 // break if OK, or NACK.
404 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
) {
408 uint8_t par
[3] = {0x00, 0x00, 0x00}; // enough for 18 Bytes to send
411 uint8_t d_block
[18], d_block_enc
[18];
412 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
413 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
415 // command MIFARE_CLASSIC_WRITEBLOCK
416 len
= mifare_sendcmd_short(pcs
, 1, ISO14443A_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
418 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
419 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
423 memcpy(d_block
, blockData
, 16);
424 AddCrc14A(d_block
, 16);
427 for (pos
= 0; pos
< 18; pos
++) {
428 d_block_enc
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ d_block
[pos
];
429 par
[pos
>> 3] |= (((filter(pcs
->odd
) ^ oddparity8(d_block
[pos
])) & 0x01) << (7 - (pos
& 0x0007)));
432 ReaderTransmitPar(d_block_enc
, sizeof(d_block_enc
), par
, NULL
);
434 // Receive the response
435 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
438 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 0)) << 0;
439 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 1)) << 1;
440 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 2)) << 2;
441 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], 3)) << 3;
443 if ((len
!= 1) || (res
!= 0x0A)) {
444 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Cmd send data2 Error: %02x", res
);
450 int mifare_ultra_writeblock_compat(uint8_t blockNo
, uint8_t *blockData
) {
455 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
456 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
458 len
= mifare_sendcmd_short(NULL
, CRYPT_NONE
, ISO14443A_CMD_WRITEBLOCK
, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
460 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
461 if (DBGLEVEL
>= DBG_ERROR
)
462 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0], len
);
466 memcpy(d_block
, blockData
, 16);
467 AddCrc14A(d_block
, 16);
469 ReaderTransmit(d_block
, sizeof(d_block
), NULL
);
471 // Receive the response
472 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
474 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
475 if (DBGLEVEL
>= DBG_ERROR
)
476 Dbprintf("Cmd Send Data Error: %02x %d", receivedAnswer
[0], len
);
482 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
) {
484 uint8_t block
[5] = {blockNo
, 0x00, 0x00, 0x00, 0x00 };
485 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
] = {0x00};
486 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
] = {0x00};
488 // command MIFARE_CLASSIC_WRITEBLOCK
489 memcpy(block
+ 1, blockData
, 4);
491 len
= mifare_sendcmd(MIFARE_ULC_WRITE
, block
, sizeof(block
), receivedAnswer
, receivedAnswerPar
, NULL
);
493 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
494 if (DBGLEVEL
>= DBG_ERROR
)
495 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0], len
);
500 int mifare_classic_halt_ex(struct Crypto1State
*pcs
) {
501 uint8_t receivedAnswer
[4] = {0x00, 0x00, 0x00, 0x00};
502 uint16_t len
= mifare_sendcmd_short(pcs
, (pcs
== NULL
) ? CRYPT_NONE
: CRYPT_ALL
, ISO14443A_CMD_HALT
, 0x00, receivedAnswer
, NULL
, NULL
);
504 if (DBGLEVEL
>= DBG_EXTENDED
) Dbprintf("halt warning. response len: %x", len
);
509 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
) {
510 return mifare_classic_halt_ex(pcs
);
513 int mifare_ultra_halt(void) {
515 uint8_t receivedAnswer
[4] = {0x00, 0x00, 0x00, 0x00};
516 len
= mifare_sendcmd_short(NULL
, CRYPT_NONE
, ISO14443A_CMD_HALT
, 0x00, receivedAnswer
, NULL
, NULL
);
518 if (DBGLEVEL
>= DBG_EXTENDED
) Dbprintf("halt warning. response len: %x", len
);
525 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
526 // plus evtl. 8 sectors with 16 blocks each (4k cards)
527 uint8_t NumBlocksPerSector(uint8_t sectorNo
) {
528 return (sectorNo
< 32) ? 4 : 16;
531 uint8_t FirstBlockOfSector(uint8_t sectorNo
) {
535 return 32 * 4 + (sectorNo
- 32) * 16;
539 // work with emulator memory
540 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
541 emlSetMem_xt(data
, blockNum
, blocksCount
, 16);
544 void emlSetMem_xt(uint8_t *data
, int blockNum
, int blocksCount
, int blockBtWidth
) {
545 uint8_t *emCARD
= BigBuf_get_EM_addr();
546 memcpy(emCARD
+ blockNum
* blockBtWidth
, data
, blocksCount
* blockBtWidth
);
549 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
550 uint8_t *emCARD
= BigBuf_get_EM_addr();
551 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
554 void emlGetMemBt(uint8_t *data
, int offset
, int byteCount
) {
555 uint8_t *emCARD
= BigBuf_get_EM_addr();
556 memcpy(data
, emCARD
+ offset
, byteCount
);
559 int emlCheckValBl(int blockNum
) {
560 uint8_t *emCARD
= BigBuf_get_EM_addr();
561 uint8_t *data
= emCARD
+ blockNum
* 16;
563 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
564 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
565 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
566 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
567 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
568 (data
[12] != (data
[15] ^ 0xff))
574 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
575 uint8_t *emCARD
= BigBuf_get_EM_addr();
576 uint8_t *data
= emCARD
+ blockNum
* 16;
578 if (emlCheckValBl(blockNum
))
581 memcpy(blReg
, data
, 4);
586 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
587 uint8_t *emCARD
= BigBuf_get_EM_addr();
588 uint8_t *data
= emCARD
+ blockNum
* 16;
590 memcpy(data
+ 0, &blReg
, 4);
591 memcpy(data
+ 8, &blReg
, 4);
592 blReg
= blReg
^ 0xffffffff;
593 memcpy(data
+ 4, &blReg
, 4);
596 data
[13] = blBlock
^ 0xff;
598 data
[15] = blBlock
^ 0xff;
603 uint64_t emlGetKey(int sectorNum
, int keyType
) {
604 uint8_t key
[6] = {0x00};
605 uint8_t *emCARD
= BigBuf_get_EM_addr();
606 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
607 return bytes_to_num(key
, 6);
610 void emlClearMem(void) {
611 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
612 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
613 uint8_t *emCARD
= BigBuf_get_EM_addr();
614 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
616 // fill sectors trailer data
617 for (uint16_t b
= 3; b
< MIFARE_4K_MAXBLOCK
; ((b
< MIFARE_2K_MAXBLOCK
- 4) ? (b
+= 4) : (b
+= 16)))
618 emlSetMem((uint8_t *)trailer
, b
, 1);
621 emlSetMem((uint8_t *)uid
, 0, 1);
625 uint8_t SectorTrailer(uint8_t blockNo
) {
626 if (blockNo
<= MIFARE_2K_MAXBLOCK
) {
627 if (DBGLEVEL
>= DBG_EXTENDED
)
628 Dbprintf("Sector Trailer for block %d : %d", blockNo
, (blockNo
| 0x03));
629 return (blockNo
| 0x03);
631 if (DBGLEVEL
>= DBG_EXTENDED
)
632 Dbprintf("Sector Trailer for block %d : %d", blockNo
, (blockNo
| 0x0f));
633 return (blockNo
| 0x0f);
637 bool IsSectorTrailer(uint8_t blockNo
) {
638 return (blockNo
== SectorTrailer(blockNo
));
641 // Mifare desfire commands
642 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
) {
643 uint8_t dcmd
[5] = {cmd
, data
[0], data
[1], 0x00, 0x00};
646 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
647 int len
= ReaderReceive(answer
, answer_parity
);
649 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Authentication failed. Card timeout.");
655 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
) {
656 uint8_t dcmd
[20] = {0x00};
658 memcpy(dcmd
+ 1, data
, 17);
661 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
662 int len
= ReaderReceive(answer
, answer_parity
);
664 if (DBGLEVEL
>= DBG_ERROR
) Dbprintf("Authentication failed. Card timeout.");
670 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
) {
673 // load key, keynumber
674 uint8_t data
[2] = {MFDES_AUTHENTICATE
, 0x00};
675 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
676 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
678 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
, receivedAnswerPar
, NULL
);
680 if (DBGLEVEL
>= DBG_ERROR
)
681 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
686 if (DBGLEVEL
>= DBG_EXTENDED
) {
687 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
688 receivedAnswer
[0], receivedAnswer
[1], receivedAnswer
[2], receivedAnswer
[3], receivedAnswer
[4],
689 receivedAnswer
[5], receivedAnswer
[6], receivedAnswer
[7], receivedAnswer
[8], receivedAnswer
[9],
690 receivedAnswer
[10], receivedAnswer
[11]);
692 memcpy(blockData
, receivedAnswer
, 12);
698 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
) {
701 uint8_t data
[17] = {MFDES_ADDITIONAL_FRAME
};
702 memcpy(data
+ 1, key
, 16);
704 uint8_t receivedAnswer
[MAX_FRAME_SIZE
] = {0x00};
705 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
] = {0x00};
707 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
, NULL
);
709 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
710 if (DBGLEVEL
>= DBG_ERROR
)
711 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
716 if (DBGLEVEL
>= DBG_EXTENDED
) {
717 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
718 receivedAnswer
[0], receivedAnswer
[1], receivedAnswer
[2], receivedAnswer
[3], receivedAnswer
[4],
719 receivedAnswer
[5], receivedAnswer
[6], receivedAnswer
[7], receivedAnswer
[8], receivedAnswer
[9],
720 receivedAnswer
[10], receivedAnswer
[11]);
722 memcpy(blockData
, receivedAnswer
, 12);