]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
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 "proxmark3.h"
17 #include "iso14443crc.h"
18 #include "iso14443a.h"
20 #include "mifareutil.h"
24 int MF_DBGLEVEL
= MF_DBG_ALL
;
27 void mf_crypto1_decrypt(struct Crypto1State
*pcs
, uint8_t *data
, int len
){
32 for (i
= 0; i
< len
; i
++)
33 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
36 for (i
= 0; i
< 4; i
++)
37 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
[0], i
)) << i
;
44 void mf_crypto1_encrypt(struct Crypto1State
*pcs
, uint8_t *data
, uint16_t len
, uint8_t *par
) {
49 for (i
= 0; i
< len
; i
++) {
51 data
[i
] = crypto1_byte(pcs
, 0x00, 0) ^ data
[i
];
54 par
[i
>>3] |= (((filter(pcs
->odd
) ^ oddparity8(bt
)) & 0x01)<<(7-(i
&0x0007)));
59 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State
*pcs
, uint8_t data
) {
63 for (i
= 0; i
< 4; i
++)
64 bt
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(data
, i
)) << i
;
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];
74 memcpy(dcmd
+1,data
,data_size
);
75 AppendCrc14443a(dcmd
, data_size
+1);
76 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
77 int len
= ReaderReceive(answer
, answer_parity
);
79 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("%02X Cmd failed. Card timeout.", cmd
);
80 len
= ReaderReceive(answer
,answer_parity
);
86 // send 2 byte commands
87 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
)
89 uint8_t dcmd
[4], ecmd
[4];
91 uint8_t par
[1]; // 1 Byte parity is enough here
94 AppendCrc14443a(dcmd
, 2);
96 memcpy(ecmd
, dcmd
, sizeof(dcmd
));
100 for (pos
= 0; pos
< 4; pos
++)
102 ecmd
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ dcmd
[pos
];
103 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(dcmd
[pos
])) & 0x01) << (7-pos
));
106 ReaderTransmitPar(ecmd
, sizeof(ecmd
), par
, timing
);
109 ReaderTransmit(dcmd
, sizeof(dcmd
), timing
);
112 int len
= ReaderReceive(answer
, par
);
114 if (answer_parity
) *answer_parity
= par
[0];
116 if (crypted
== CRYPT_ALL
) {
119 for (pos
= 0; pos
< 4; pos
++)
120 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(answer
[0], pos
)) << pos
;
125 for (pos
= 0; pos
< len
; pos
++)
127 answer
[pos
] = crypto1_byte(pcs
, 0x00, 0) ^ answer
[pos
];
135 // mifare classic commands
136 int mifare_classic_auth(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t keyType
, uint64_t ui64Key
, uint8_t isNested
)
138 return mifare_classic_authex(pcs
, uid
, blockNo
, keyType
, ui64Key
, isNested
, NULL
, NULL
);
141 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
)
147 uint8_t par
[1] = {0x00};
149 // "random" reader nonce:
150 byte_t nr
[4] = {0x55, 0x41, 0x49, 0x92};
151 //byte_t nr[4] = {0x01, 0x01, 0x01, 0x01};
153 uint32_t nt
, ntpp
; // Supplied tag nonce
155 uint8_t mf_nr_ar
[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
156 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
157 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
159 // Transmit MIFARE_CLASSIC_AUTH
160 len
= mifare_sendcmd_short(pcs
, isNested
, 0x60 + (keyType
& 0x01), blockNo
, receivedAnswer
, receivedAnswerPar
, timing
);
161 if (MF_DBGLEVEL
>= 4) Dbprintf("rand tag nonce len: %x", len
);
162 if (len
!= 4) return 1;
164 // Save the tag nonce (nt)
165 nt
= bytes_to_num(receivedAnswer
, 4);
167 // ----------------------------- crypto1 create
169 crypto1_destroy(pcs
);
171 // Init cipher with key
172 crypto1_create(pcs
, ui64Key
);
174 if (isNested
== AUTH_NESTED
) {
175 // decrypt nt with help of new key
176 nt
= crypto1_word(pcs
, nt
^ uid
, 1) ^ nt
;
178 // Load (plain) uid^nt into the cipher
179 crypto1_word(pcs
, nt
^ uid
, 0);
183 if (!ntptr
&& (MF_DBGLEVEL
>= 3))
184 Dbprintf("auth uid: %08x nt: %08x", uid
, nt
);
190 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
192 for (pos
= 0; pos
< 4; pos
++)
194 mf_nr_ar
[pos
] = crypto1_byte(pcs
, nr
[pos
], 0) ^ nr
[pos
];
195 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nr
[pos
])) & 0x01) << (7-pos
));
198 // Skip 32 bits in pseudo random generator
199 nt
= prng_successor(nt
,32);
202 for (pos
= 4; pos
< 8; pos
++)
204 nt
= prng_successor(nt
,8);
205 mf_nr_ar
[pos
] = crypto1_byte(pcs
,0x00,0) ^ (nt
& 0xff);
206 par
[0] |= (((filter(pcs
->odd
) ^ oddparity8(nt
& 0xff)) & 0x01) << (7-pos
));
209 // Transmit reader nonce and reader answer
210 ReaderTransmitPar(mf_nr_ar
, sizeof(mf_nr_ar
), par
, NULL
);
212 // Receive 4 byte tag answer
213 len
= ReaderReceive(receivedAnswer
, receivedAnswerPar
);
216 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Card timeout.");
220 memcpy(tmp4
, receivedAnswer
, 4);
221 ntpp
= prng_successor(nt
, 32) ^ crypto1_word(pcs
, 0,0);
223 if (ntpp
!= bytes_to_num(tmp4
, 4)) {
224 if (MF_DBGLEVEL
>= 1) Dbprintf("Authentication failed. Error card response.");
231 int mifare_classic_readblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
237 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
238 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
240 // command MIFARE_CLASSIC_READBLOCK
241 len
= mifare_sendcmd_short(pcs
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
243 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
247 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: card timeout. len: %x", len
);
251 memcpy(bt
, receivedAnswer
+ 16, 2);
252 AppendCrc14443a(receivedAnswer
, 16);
253 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
254 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd CRC response error.");
258 memcpy(blockData
, receivedAnswer
, 16);
262 // mifare ultralight commands
263 int mifare_ul_ev1_auth(uint8_t *keybytes
, uint8_t *pack
){
268 uint8_t key
[4] = {0x00};
269 memcpy(key
, keybytes
, 4);
271 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
272 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key
[0], key
[1], key
[2], key
[3]);
273 len
= mifare_sendcmd(0x1B, key
, sizeof(key
), resp
, respPar
, NULL
);
276 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x %u", resp
[0], len
);
280 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
)
281 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp
[0],resp
[1],resp
[2],resp
[3]);
283 memcpy(pack
, resp
, 4);
287 int mifare_ultra_auth(uint8_t *keybytes
){
291 uint8_t random_a
[8] = {1,1,1,1,1,1,1,1};
292 uint8_t random_b
[8] = {0x00};
293 uint8_t enc_random_b
[8] = {0x00};
294 uint8_t rnd_ab
[16] = {0x00};
295 uint8_t IV
[8] = {0x00};
296 uint8_t key
[16] = {0x00};
297 memcpy(key
, keybytes
, 16);
300 uint8_t resp
[19] = {0x00};
301 uint8_t respPar
[3] = {0,0,0};
303 // REQUEST AUTHENTICATION
304 len
= mifare_sendcmd_short(NULL
, 1, 0x1A, 0x00, resp
, respPar
,NULL
);
306 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
311 memcpy(enc_random_b
,resp
+1,8);
314 tdes_2key_dec(random_b
, enc_random_b
, sizeof(random_b
), key
, IV
);
316 memcpy(rnd_ab
,random_a
,8);
317 memcpy(rnd_ab
+8,random_b
,8);
319 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
320 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
321 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]);
323 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
324 random_b
[0],random_b
[1],random_b
[2],random_b
[3],random_b
[4],random_b
[5],random_b
[6],random_b
[7]);
326 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
327 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
329 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
330 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15] );
333 // encrypt out, in, length, key, iv
334 tdes_2key_enc(rnd_ab
, rnd_ab
, sizeof(rnd_ab
), key
, enc_random_b
);
335 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
336 len
= mifare_sendcmd(0xAF, rnd_ab
, sizeof(rnd_ab
), resp
, respPar
, NULL
);
338 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", resp
[0]);
342 uint8_t enc_resp
[8] = { 0,0,0,0,0,0,0,0 };
343 uint8_t resp_random_a
[8] = { 0,0,0,0,0,0,0,0 };
344 memcpy(enc_resp
, resp
+1, 8);
346 // decrypt out, in, length, key, iv
347 tdes_2key_dec(resp_random_a
, enc_resp
, 8, key
, enc_random_b
);
348 if ( memcmp(resp_random_a
, random_a
, 8) != 0 ) {
349 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("failed authentication");
353 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
354 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
355 rnd_ab
[0],rnd_ab
[1],rnd_ab
[2],rnd_ab
[3],
356 rnd_ab
[4],rnd_ab
[5],rnd_ab
[6],rnd_ab
[7]);
358 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
359 rnd_ab
[8],rnd_ab
[9],rnd_ab
[10],rnd_ab
[11],
360 rnd_ab
[12],rnd_ab
[13],rnd_ab
[14],rnd_ab
[15]);
362 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
363 random_a
[0],random_a
[1],random_a
[2],random_a
[3],
364 random_a
[4],random_a
[5],random_a
[6],random_a
[7]);
366 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
367 resp_random_a
[0],resp_random_a
[1],resp_random_a
[2],resp_random_a
[3],
368 resp_random_a
[4],resp_random_a
[5],resp_random_a
[6],resp_random_a
[7]);
373 int mifare_ultra_readblock(uint8_t blockNo
, uint8_t *blockData
)
377 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
378 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
381 len
= mifare_sendcmd_short(NULL
, 1, 0x30, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
383 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
387 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd Error: card timeout. len: %x", len
);
391 memcpy(bt
, receivedAnswer
+ 16, 2);
392 AppendCrc14443a(receivedAnswer
, 16);
393 if (bt
[0] != receivedAnswer
[16] || bt
[1] != receivedAnswer
[17]) {
394 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("Cmd CRC response error.");
398 memcpy(blockData
, receivedAnswer
, 14);
402 int mifare_classic_writeblock(struct Crypto1State
*pcs
, uint32_t uid
, uint8_t blockNo
, uint8_t *blockData
)
407 uint8_t par
[3] = {0x00}; // enough for 18 Bytes to send
410 uint8_t d_block
[18], d_block_enc
[18];
411 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
412 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
414 // command MIFARE_CLASSIC_WRITEBLOCK
415 len
= mifare_sendcmd_short(pcs
, 1, 0xA0, blockNo
, receivedAnswer
, receivedAnswerPar
, NULL
);
417 if ((len
!= 1) || (receivedAnswer
[0] != 0x0A)) { // 0x0a - ACK
418 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
422 memcpy(d_block
, blockData
, 16);
423 AppendCrc14443a(d_block
, 16);
426 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 for (i
= 0; i
< 4; i
++)
439 res
|= (crypto1_bit(pcs
, 0, 0) ^ BIT(receivedAnswer
[0], i
)) << i
;
441 if ((len
!= 1) || (res
!= 0x0A)) {
442 if (MF_DBGLEVEL
>= 1) Dbprintf("Cmd send data2 Error: %02x", res
);
449 /* // command not needed, but left for future testing
450 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
453 uint8_t par[3] = {0}; // enough for 18 parity bits
454 uint8_t d_block[18] = {0x00};
455 uint8_t receivedAnswer[MAX_FRAME_SIZE];
456 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
458 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
460 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
461 if (MF_DBGLEVEL >= MF_DBG_ERROR)
462 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
466 memcpy(d_block, blockData, 16);
467 AppendCrc14443a(d_block, 16);
469 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
471 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
473 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
474 if (MF_DBGLEVEL >= MF_DBG_ERROR)
475 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
482 int mifare_ultra_writeblock(uint8_t blockNo
, uint8_t *blockData
)
485 uint8_t d_block
[5] = {0x00};
486 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
487 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
489 // command MIFARE_CLASSIC_WRITEBLOCK
491 memcpy(d_block
+1,blockData
,4);
492 //AppendCrc14443a(d_block, 6);
494 len
= mifare_sendcmd(0xA2, d_block
, sizeof(d_block
), receivedAnswer
, receivedAnswerPar
, NULL
);
496 if (receivedAnswer
[0] != 0x0A) { // 0x0a - ACK
497 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
498 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer
[0],len
);
503 int mifare_classic_halt_ex(struct Crypto1State
*pcs
) {
505 uint8_t receivedAnswer
[4];
506 uint8_t receivedAnswerPar
[4];
508 len
= mifare_sendcmd_short(pcs
, pcs
== NULL
? false:true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
510 if (MF_DBGLEVEL
>= MF_DBG_ERROR
) Dbprintf("halt error. response len: %x", len
);
515 int mifare_classic_halt(struct Crypto1State
*pcs
, uint32_t uid
) {
516 return mifare_classic_halt_ex(pcs
);
519 int mifare_ultra_halt()
522 uint8_t receivedAnswer
[MAX_MIFARE_FRAME_SIZE
];
523 uint8_t receivedAnswerPar
[MAX_MIFARE_PARITY_SIZE
];
525 len
= mifare_sendcmd_short(NULL
, true, 0x50, 0x00, receivedAnswer
, receivedAnswerPar
, NULL
);
527 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
528 Dbprintf("halt error. response len: %x", len
);
535 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
536 // plus evtl. 8 sectors with 16 blocks each (4k cards)
537 uint8_t NumBlocksPerSector(uint8_t sectorNo
)
545 uint8_t FirstBlockOfSector(uint8_t sectorNo
)
550 return 32*4 + (sectorNo
- 32) * 16;
555 // work with emulator memory
556 void emlSetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
557 emlSetMem_xt(data
, blockNum
, blocksCount
, 16);
560 void emlSetMem_xt(uint8_t *data
, int blockNum
, int blocksCount
, int blockBtWidth
) {
561 uint8_t* emCARD
= BigBuf_get_EM_addr();
562 memcpy(emCARD
+ blockNum
* blockBtWidth
, data
, blocksCount
* blockBtWidth
);
565 void emlGetMem(uint8_t *data
, int blockNum
, int blocksCount
) {
566 uint8_t* emCARD
= BigBuf_get_EM_addr();
567 memcpy(data
, emCARD
+ blockNum
* 16, blocksCount
* 16);
570 void emlGetMemBt(uint8_t *data
, int bytePtr
, int byteCount
) {
571 uint8_t* emCARD
= BigBuf_get_EM_addr();
572 memcpy(data
, emCARD
+ bytePtr
, byteCount
);
575 int emlCheckValBl(int blockNum
) {
576 uint8_t* emCARD
= BigBuf_get_EM_addr();
577 uint8_t* data
= emCARD
+ blockNum
* 16;
579 if ((data
[0] != (data
[4] ^ 0xff)) || (data
[0] != data
[8]) ||
580 (data
[1] != (data
[5] ^ 0xff)) || (data
[1] != data
[9]) ||
581 (data
[2] != (data
[6] ^ 0xff)) || (data
[2] != data
[10]) ||
582 (data
[3] != (data
[7] ^ 0xff)) || (data
[3] != data
[11]) ||
583 (data
[12] != (data
[13] ^ 0xff)) || (data
[12] != data
[14]) ||
584 (data
[12] != (data
[15] ^ 0xff))
590 int emlGetValBl(uint32_t *blReg
, uint8_t *blBlock
, int blockNum
) {
591 uint8_t* emCARD
= BigBuf_get_EM_addr();
592 uint8_t* data
= emCARD
+ blockNum
* 16;
594 if (emlCheckValBl(blockNum
)) {
598 memcpy(blReg
, data
, 4);
603 int emlSetValBl(uint32_t blReg
, uint8_t blBlock
, int blockNum
) {
604 uint8_t* emCARD
= BigBuf_get_EM_addr();
605 uint8_t* data
= emCARD
+ blockNum
* 16;
607 memcpy(data
+ 0, &blReg
, 4);
608 memcpy(data
+ 8, &blReg
, 4);
609 blReg
= blReg
^ 0xffffffff;
610 memcpy(data
+ 4, &blReg
, 4);
613 data
[13] = blBlock
^ 0xff;
615 data
[15] = blBlock
^ 0xff;
620 uint64_t emlGetKey(int sectorNum
, int keyType
) {
622 uint8_t* emCARD
= BigBuf_get_EM_addr();
624 memcpy(key
, emCARD
+ 16 * (FirstBlockOfSector(sectorNum
) + NumBlocksPerSector(sectorNum
) - 1) + keyType
* 10, 6);
625 return bytes_to_num(key
, 6);
628 void emlClearMem(void) {
631 const uint8_t trailer
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
632 const uint8_t uid
[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
633 uint8_t* emCARD
= BigBuf_get_EM_addr();
635 memset(emCARD
, 0, CARD_MEMORY_SIZE
);
637 // fill sectors trailer data
638 for(b
= 3; b
< 256; b
<127?(b
+=4):(b
+=16)) {
639 emlSetMem((uint8_t *)trailer
, b
, 1);
643 emlSetMem((uint8_t *)uid
, 0, 1);
648 // Mifare desfire commands
649 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
)
651 uint8_t dcmd
[5] = {0x00};
653 memcpy(dcmd
+1,data
,2);
654 AppendCrc14443a(dcmd
, 3);
656 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
657 int len
= ReaderReceive(answer
, answer_parity
);
659 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
660 Dbprintf("Authentication failed. Card timeout.");
666 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
)
668 uint8_t dcmd
[20] = {0x00};
670 memcpy(dcmd
+1,data
,17);
671 AppendCrc14443a(dcmd
, 18);
673 ReaderTransmit(dcmd
, sizeof(dcmd
), NULL
);
674 int len
= ReaderReceive(answer
, answer_parity
);
676 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
677 Dbprintf("Authentication failed. Card timeout.");
683 int mifare_desfire_des_auth1(uint32_t uid
, uint8_t *blockData
){
686 // load key, keynumber
687 uint8_t data
[2]={0x0a, 0x00};
688 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
689 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
691 len
= mifare_sendcmd_special(NULL
, 1, 0x02, data
, receivedAnswer
,receivedAnswerPar
,NULL
);
693 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
694 Dbprintf("Cmd Error: %02x", receivedAnswer
[0]);
699 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
700 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
701 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
702 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
703 receivedAnswer
[10],receivedAnswer
[11]);
705 memcpy(blockData
, receivedAnswer
, 12);
711 int mifare_desfire_des_auth2(uint32_t uid
, uint8_t *key
, uint8_t *blockData
){
714 uint8_t data
[17] = {0x00};
716 memcpy(data
+1,key
,16);
718 uint8_t receivedAnswer
[MAX_FRAME_SIZE
];
719 uint8_t receivedAnswerPar
[MAX_PARITY_SIZE
];
721 len
= mifare_sendcmd_special2(NULL
, 1, 0x03, data
, receivedAnswer
, receivedAnswerPar
,NULL
);
723 if ((receivedAnswer
[0] == 0x03) && (receivedAnswer
[1] == 0xae)) {
724 if (MF_DBGLEVEL
>= MF_DBG_ERROR
)
725 Dbprintf("Auth Error: %02x %02x", receivedAnswer
[0], receivedAnswer
[1]);
730 if (MF_DBGLEVEL
>= MF_DBG_EXTENDED
) {
731 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
732 receivedAnswer
[0],receivedAnswer
[1],receivedAnswer
[2],receivedAnswer
[3],receivedAnswer
[4],
733 receivedAnswer
[5],receivedAnswer
[6],receivedAnswer
[7],receivedAnswer
[8],receivedAnswer
[9],
734 receivedAnswer
[10],receivedAnswer
[11]);
736 memcpy(blockData
, receivedAnswer
, 12);