Fixed buffer initialization errors, as reported in http://www.proxmark.org/forum...
[legacy-proxmark3.git] / armsrc / mifareutil.c
blobf79c2ede2d78676ddc6aef7b373129e3198a23ca
1 //-----------------------------------------------------------------------------
2 // Merlok, May 2011, 2012
3 // Many authors, whom made it possible
4 //
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
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Work with mifare cards.
10 //-----------------------------------------------------------------------------
12 #include "proxmark3.h"
13 #include "apps.h"
14 #include "util.h"
15 #include "string.h"
17 #include "iso14443crc.h"
18 #include "iso14443a.h"
19 #include "crapto1.h"
20 #include "mifareutil.h"
22 int MF_DBGLEVEL = MF_DBG_ALL;
24 // crypto1 helpers
25 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
26 uint8_t bt = 0;
27 int i;
29 if (len != 1) {
30 for (i = 0; i < len; i++)
31 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
32 } else {
33 bt = 0;
34 for (i = 0; i < 4; i++)
35 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], i)) << i;
37 data[0] = bt;
39 return;
42 void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {
43 uint8_t bt = 0;
44 int i;
45 par[0] = 0;
47 for (i = 0; i < len; i++) {
48 bt = data[i];
49 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
50 if((i&0x0007) == 0)
51 par[i>>3] = 0;
52 par[i>>3] |= (((filter(pcs->odd) ^ oddparity(bt)) & 0x01)<<(7-(i&0x0007)));
54 return;
57 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
58 uint8_t bt = 0;
59 int i;
61 for (i = 0; i < 4; i++)
62 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;
64 return bt;
67 // send commands
68 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)
70 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, answer_parity, timing);
73 int mifare_sendcmd_short_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
75 uint8_t dcmd[8];
76 dcmd[0] = cmd;
77 dcmd[1] = data[0];
78 dcmd[2] = data[1];
79 dcmd[3] = data[2];
80 dcmd[4] = data[3];
81 dcmd[5] = data[4];
82 AppendCrc14443a(dcmd, 6);
83 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
84 int len = ReaderReceive(answer, answer_parity);
85 if(!len) {
86 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
87 return 2;
89 return len;
92 int mifare_sendcmd_short_mfucauth(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t *data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)
94 uint8_t dcmd[19];
95 int len;
96 dcmd[0] = cmd;
97 memcpy(dcmd+1,data,16);
98 AppendCrc14443a(dcmd, 17);
100 ReaderTransmit(dcmd, sizeof(dcmd), timing);
101 len = ReaderReceive(answer, answer_parity);
102 if(!len) {
103 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed. Card timeout.");
104 len = ReaderReceive(answer,answer_parity);
106 if(len==1) {
107 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("NAK - Authentication failed.");
108 return 1;
110 return len;
113 int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)
115 uint8_t dcmd[4], ecmd[4];
116 uint16_t pos, res;
117 uint8_t par[1]; // 1 Byte parity is enough here
118 dcmd[0] = cmd;
119 dcmd[1] = data;
120 AppendCrc14443a(dcmd, 2);
122 memcpy(ecmd, dcmd, sizeof(dcmd));
124 if (crypted) {
125 par[0] = 0;
126 for (pos = 0; pos < 4; pos++)
128 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
129 par[0] |= (((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) << (7-pos));
132 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
134 } else {
135 ReaderTransmit(dcmd, sizeof(dcmd), timing);
138 int len = ReaderReceive(answer, par);
140 if (answer_parity) *answer_parity = par[0];
142 if (crypted == CRYPT_ALL) {
143 if (len == 1) {
144 res = 0;
145 for (pos = 0; pos < 4; pos++)
146 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
148 answer[0] = res;
150 } else {
151 for (pos = 0; pos < len; pos++)
153 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
158 return len;
161 // mifare classic commands
162 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested)
164 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);
167 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)
169 // variables
170 int len;
171 uint32_t pos;
172 uint8_t tmp4[4];
173 uint8_t par[1] = {0x00};
174 byte_t nr[4];
175 uint32_t nt, ntpp; // Supplied tag nonce
177 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
178 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
179 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
181 // Transmit MIFARE_CLASSIC_AUTH
182 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);
183 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len);
184 if (len != 4) return 1;
186 // "random" reader nonce:
187 nr[0] = 0x55;
188 nr[1] = 0x41;
189 nr[2] = 0x49;
190 nr[3] = 0x92;
192 // Save the tag nonce (nt)
193 nt = bytes_to_num(receivedAnswer, 4);
195 // ----------------------------- crypto1 create
196 if (isNested)
197 crypto1_destroy(pcs);
199 // Init cipher with key
200 crypto1_create(pcs, ui64Key);
202 if (isNested == AUTH_NESTED) {
203 // decrypt nt with help of new key
204 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
205 } else {
206 // Load (plain) uid^nt into the cipher
207 crypto1_word(pcs, nt ^ uid, 0);
210 // some statistic
211 if (!ntptr && (MF_DBGLEVEL >= 3))
212 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
214 // save Nt
215 if (ntptr)
216 *ntptr = nt;
218 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
219 par[0] = 0;
220 for (pos = 0; pos < 4; pos++)
222 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];
223 par[0] |= (((filter(pcs->odd) ^ oddparity(nr[pos])) & 0x01) << (7-pos));
226 // Skip 32 bits in pseudo random generator
227 nt = prng_successor(nt,32);
229 // ar+parity
230 for (pos = 4; pos < 8; pos++)
232 nt = prng_successor(nt,8);
233 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
234 par[0] |= (((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) << (7-pos));
237 // Transmit reader nonce and reader answer
238 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
240 // Receive 4 byte tag answer
241 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
242 if (!len)
244 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
245 return 2;
248 memcpy(tmp4, receivedAnswer, 4);
249 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
251 if (ntpp != bytes_to_num(tmp4, 4)) {
252 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
253 return 3;
256 return 0;
259 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
261 // variables
262 int len;
263 uint8_t bt[2];
265 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
266 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
268 // command MIFARE_CLASSIC_READBLOCK
269 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
270 if (len == 1) {
271 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
272 return 1;
274 if (len != 18) {
275 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
276 return 2;
279 memcpy(bt, receivedAnswer + 16, 2);
280 AppendCrc14443a(receivedAnswer, 16);
281 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
282 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
283 return 3;
286 memcpy(blockData, receivedAnswer, 16);
287 return 0;
290 // mifare ultralight commands
291 int mifare_ultra_auth1(uint32_t uid, uint8_t *blockData){
293 uint16_t len;
294 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
295 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
297 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);
298 if (len == 1) {
299 if (MF_DBGLEVEL >= MF_DBG_ERROR)
300 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
301 return 1;
303 if (len != 11)
304 return 1;
306 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
307 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
308 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
309 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
310 receivedAnswer[10]);
312 memcpy(blockData, receivedAnswer, 11);
313 return 0;
316 int mifare_ultra_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
318 uint16_t len;
319 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
320 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
322 len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, key, receivedAnswer, receivedAnswerPar, NULL);
323 if (len == 1) {
324 if (MF_DBGLEVEL >= MF_DBG_ERROR)
325 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
326 return 1;
328 if (len != 11)
329 return 1;
331 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
332 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
333 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
334 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
335 receivedAnswer[10]);
337 memcpy(blockData, receivedAnswer, 11);
338 return 0;
341 int mifare_ultra_readblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
343 uint16_t len;
344 uint8_t bt[2];
345 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
346 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
349 // command MIFARE_CLASSIC_READBLOCK
350 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
351 if (len == 1) {
352 if (MF_DBGLEVEL >= MF_DBG_ERROR)
353 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
354 return 1;
356 if (len != 18) {
357 if (MF_DBGLEVEL >= MF_DBG_ERROR)
358 Dbprintf("Cmd Error: card timeout. len: %x", len);
359 return 2;
362 memcpy(bt, receivedAnswer + 16, 2);
363 AppendCrc14443a(receivedAnswer, 16);
364 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
365 if (MF_DBGLEVEL >= MF_DBG_ERROR)
366 Dbprintf("Cmd CRC response error.");
367 return 3;
370 memcpy(blockData, receivedAnswer, 14);
371 return 0;
375 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
377 // variables
378 uint16_t len, i;
379 uint32_t pos;
380 uint8_t par[3] = {0}; // enough for 18 Bytes to send
381 byte_t res;
383 uint8_t d_block[18], d_block_enc[18];
384 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
385 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
387 // command MIFARE_CLASSIC_WRITEBLOCK
388 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
390 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
391 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
392 return 1;
395 memcpy(d_block, blockData, 16);
396 AppendCrc14443a(d_block, 16);
398 // crypto
399 for (pos = 0; pos < 18; pos++)
401 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
402 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
405 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
407 // Receive the response
408 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
410 res = 0;
411 for (i = 0; i < 4; i++)
412 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
414 if ((len != 1) || (res != 0x0A)) {
415 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
416 return 2;
419 return 0;
422 int mifare_ultra_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
424 uint16_t len;
425 uint8_t par[3] = {0}; // enough for 18 parity bits
426 uint8_t d_block[18] = {0x00};
427 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
428 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
430 // command MIFARE_CLASSIC_WRITEBLOCK
431 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
433 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
434 if (MF_DBGLEVEL >= MF_DBG_ERROR)
435 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
436 return 1;
439 memcpy(d_block, blockData, 16);
440 AppendCrc14443a(d_block, 16);
442 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
444 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
446 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
447 if (MF_DBGLEVEL >= MF_DBG_ERROR)
448 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
449 return 2;
451 return 0;
454 int mifare_ultra_special_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
456 uint16_t len;
457 uint8_t d_block[8] = {0x00};
458 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
459 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
461 // command MIFARE_CLASSIC_WRITEBLOCK
462 d_block[0]= blockNo;
463 memcpy(d_block+1,blockData,4);
464 AppendCrc14443a(d_block, 6);
466 len = mifare_sendcmd_short_special(NULL, 1, 0xA2, d_block, receivedAnswer, receivedAnswerPar, NULL);
468 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
469 if (MF_DBGLEVEL >= MF_DBG_ERROR)
470 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
471 return 1;
473 return 0;
476 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
478 uint16_t len;
479 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
480 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
482 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
483 if (len != 0) {
484 if (MF_DBGLEVEL >= MF_DBG_ERROR)
485 Dbprintf("halt error. response len: %x", len);
486 return 1;
489 return 0;
492 int mifare_ultra_halt(uint32_t uid)
494 uint16_t len;
495 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
496 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
498 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
499 if (len != 0) {
500 if (MF_DBGLEVEL >= MF_DBG_ERROR)
501 Dbprintf("halt error. response len: %x", len);
502 return 1;
504 return 0;
508 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
509 // plus evtl. 8 sectors with 16 blocks each (4k cards)
510 uint8_t NumBlocksPerSector(uint8_t sectorNo)
512 if (sectorNo < 32)
513 return 4;
514 else
515 return 16;
518 uint8_t FirstBlockOfSector(uint8_t sectorNo)
520 if (sectorNo < 32)
521 return sectorNo * 4;
522 else
523 return 32*4 + (sectorNo - 32) * 16;
528 // work with emulator memory
529 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
530 uint8_t* emCARD = BigBuf_get_EM_addr();
531 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
534 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
535 uint8_t* emCARD = BigBuf_get_EM_addr();
536 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
539 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
540 uint8_t* emCARD = BigBuf_get_EM_addr();
541 memcpy(data, emCARD + bytePtr, byteCount);
544 int emlCheckValBl(int blockNum) {
545 uint8_t* emCARD = BigBuf_get_EM_addr();
546 uint8_t* data = emCARD + blockNum * 16;
548 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
549 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
550 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
551 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
552 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
553 (data[12] != (data[15] ^ 0xff))
555 return 1;
556 return 0;
559 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
560 uint8_t* emCARD = BigBuf_get_EM_addr();
561 uint8_t* data = emCARD + blockNum * 16;
563 if (emlCheckValBl(blockNum)) {
564 return 1;
567 memcpy(blReg, data, 4);
568 *blBlock = data[12];
569 return 0;
572 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
573 uint8_t* emCARD = BigBuf_get_EM_addr();
574 uint8_t* data = emCARD + blockNum * 16;
576 memcpy(data + 0, &blReg, 4);
577 memcpy(data + 8, &blReg, 4);
578 blReg = blReg ^ 0xffffffff;
579 memcpy(data + 4, &blReg, 4);
581 data[12] = blBlock;
582 data[13] = blBlock ^ 0xff;
583 data[14] = blBlock;
584 data[15] = blBlock ^ 0xff;
586 return 0;
589 uint64_t emlGetKey(int sectorNum, int keyType) {
590 uint8_t key[6];
591 uint8_t* emCARD = BigBuf_get_EM_addr();
593 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
594 return bytes_to_num(key, 6);
597 void emlClearMem(void) {
598 int b;
600 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
601 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
602 uint8_t* emCARD = BigBuf_get_EM_addr();
604 memset(emCARD, 0, CARD_MEMORY_SIZE);
606 // fill sectors trailer data
607 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
608 emlSetMem((uint8_t *)trailer, b , 1);
611 // uid
612 emlSetMem((uint8_t *)uid, 0, 1);
613 return;
617 // Mifare desfire commands
618 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)
620 uint8_t dcmd[5] = {0x00};
621 dcmd[0] = cmd;
622 memcpy(dcmd+1,data,2);
623 AppendCrc14443a(dcmd, 3);
625 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
626 int len = ReaderReceive(answer, answer_parity);
627 if(!len) {
628 if (MF_DBGLEVEL >= MF_DBG_ERROR)
629 Dbprintf("Authentication failed. Card timeout.");
630 return 1;
632 return len;
635 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)
637 uint8_t dcmd[20] = {0x00};
638 dcmd[0] = cmd;
639 memcpy(dcmd+1,data,17);
640 AppendCrc14443a(dcmd, 18);
642 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
643 int len = ReaderReceive(answer, answer_parity);
644 if(!len){
645 if (MF_DBGLEVEL >= MF_DBG_ERROR)
646 Dbprintf("Authentication failed. Card timeout.");
647 return 1;
649 return len;
652 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){
654 int len;
655 // load key, keynumber
656 uint8_t data[2]={0x0a, 0x00};
657 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
658 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
660 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);
661 if (len == 1) {
662 if (MF_DBGLEVEL >= MF_DBG_ERROR)
663 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
664 return 1;
667 if (len == 12) {
668 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
669 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
670 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
671 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
672 receivedAnswer[10],receivedAnswer[11]);
674 memcpy(blockData, receivedAnswer, 12);
675 return 0;
677 return 1;
680 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
682 int len;
683 uint8_t data[17] = {0x00};
684 data[0] = 0xAF;
685 memcpy(data+1,key,16);
687 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
688 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
690 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);
692 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {
693 if (MF_DBGLEVEL >= MF_DBG_ERROR)
694 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);
695 return 1;
698 if (len == 12){
699 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
700 Dbprintf("Auth2 Resp: %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);
706 return 0;
708 return 1;