]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
cc54b74022ae32fc1ed8a9faaa0800c06aad69d2
[proxmark3-svn] / armsrc / mifareutil.c
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 //-----------------------------------------------------------------------------
11
12 #include "proxmark3.h"
13 #include "apps.h"
14 #include "util.h"
15 #include "string.h"
16
17 #include "iso14443crc.h"
18 #include "iso14443a.h"
19 #include "crapto1.h"
20 #include "mifareutil.h"
21
22 int MF_DBGLEVEL = MF_DBG_ALL;
23
24 // crypto1 helpers
25 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
26 uint8_t bt = 0;
27 int i;
28
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;
36
37 data[0] = bt;
38 }
39 return;
40 }
41
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;
46
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)));
53 }
54 return;
55 }
56
57 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
58 uint8_t bt = 0;
59 int i;
60
61 for (i = 0; i < 4; i++)
62 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;
63
64 return bt;
65 }
66
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)
69 {
70 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, answer_parity, timing);
71 }
72
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)
74 {
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;
88 }
89 return len;
90 }
91
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)
93 {
94 uint8_t dcmd[19];
95 int len;
96 dcmd[0] = cmd;
97 memcpy(dcmd+1,data,16);
98 AppendCrc14443a(dcmd, 17);
99
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);
105 }
106 if(len==1) {
107 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("NAK - Authentication failed.");
108 return 1;
109 }
110 return len;
111 }
112
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)
114 {
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);
121
122 memcpy(ecmd, dcmd, sizeof(dcmd));
123
124 if (crypted) {
125 par[0] = 0;
126 for (pos = 0; pos < 4; pos++)
127 {
128 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
129 par[0] |= (((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) << (7-pos));
130 }
131
132 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
133
134 } else {
135 ReaderTransmit(dcmd, sizeof(dcmd), timing);
136 }
137
138 int len = ReaderReceive(answer, par);
139
140 if (answer_parity) *answer_parity = par[0];
141
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;
147
148 answer[0] = res;
149
150 } else {
151 for (pos = 0; pos < len; pos++)
152 {
153 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
154 }
155 }
156 }
157
158 return len;
159 }
160
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)
163 {
164 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);
165 }
166
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)
168 {
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
176
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];
180
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;
185
186 // "random" reader nonce:
187 nr[0] = 0x55;
188 nr[1] = 0x41;
189 nr[2] = 0x49;
190 nr[3] = 0x92;
191
192 // Save the tag nonce (nt)
193 nt = bytes_to_num(receivedAnswer, 4);
194
195 // ----------------------------- crypto1 create
196 if (isNested)
197 crypto1_destroy(pcs);
198
199 // Init cipher with key
200 crypto1_create(pcs, ui64Key);
201
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);
208 }
209
210 // some statistic
211 if (!ntptr && (MF_DBGLEVEL >= 3))
212 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
213
214 // save Nt
215 if (ntptr)
216 *ntptr = nt;
217
218 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
219 par[0] = 0;
220 for (pos = 0; pos < 4; pos++)
221 {
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));
224 }
225
226 // Skip 32 bits in pseudo random generator
227 nt = prng_successor(nt,32);
228
229 // ar+parity
230 for (pos = 4; pos < 8; pos++)
231 {
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));
235 }
236
237 // Transmit reader nonce and reader answer
238 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
239
240 // Receive 4 byte tag answer
241 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
242 if (!len)
243 {
244 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
245 return 2;
246 }
247
248 memcpy(tmp4, receivedAnswer, 4);
249 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
250
251 if (ntpp != bytes_to_num(tmp4, 4)) {
252 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
253 return 3;
254 }
255
256 return 0;
257 }
258
259 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
260 {
261 // variables
262 int len;
263 uint8_t bt[2];
264
265 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
266 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
267
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;
273 }
274 if (len != 18) {
275 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
276 return 2;
277 }
278
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;
284 }
285
286 memcpy(blockData, receivedAnswer, 16);
287 return 0;
288 }
289
290 // mifare ultralight commands
291 int mifare_ul_ev1_auth(uint8_t *key, uint8_t *pack){
292
293 uint16_t len;
294 uint8_t receivedAnswer[MAX_FRAME_SIZE];
295 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
296
297 len = mifare_sendcmd_short_mfucauth(NULL, 0, 0x1B, key, receivedAnswer, receivedAnswerPar, NULL);
298 if (len != 4) {
299 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", receivedAnswer[0], len);
300 return 1;
301 }
302
303 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
304 Dbprintf("Auth Resp: %02x%02x%02x%02x",
305 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3]);
306 }
307 memcpy(pack, receivedAnswer, 4);
308 return 0;
309 }
310
311 int mifare_ultra_auth1(uint8_t *blockData){
312
313 uint16_t len;
314 uint8_t receivedAnswer[MAX_FRAME_SIZE];
315 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
316
317 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);
318 if (len != 11) {
319 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
320 return 1;
321 }
322
323 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
324 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
325 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
326 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
327 receivedAnswer[10]);
328 }
329 memcpy(blockData, receivedAnswer, 11);
330 return 0;
331 }
332
333 int mifare_ultra_auth2(uint8_t *key, uint8_t *blockData){
334
335 uint16_t len;
336 uint8_t receivedAnswer[MAX_FRAME_SIZE];
337 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
338
339 len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, key, receivedAnswer, receivedAnswerPar, NULL);
340 if (len != 11) {
341 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
342 return 1;
343 }
344
345 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
346 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
347 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
348 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
349 receivedAnswer[10]);
350 }
351 memcpy(blockData, receivedAnswer, 11);
352 return 0;
353 }
354
355 int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)
356 {
357 uint16_t len;
358 uint8_t bt[2];
359 uint8_t receivedAnswer[MAX_FRAME_SIZE];
360 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
361
362 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
363 if (len == 1) {
364 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
365 return 1;
366 }
367 if (len != 18) {
368 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);
369 return 2;
370 }
371
372 memcpy(bt, receivedAnswer + 16, 2);
373 AppendCrc14443a(receivedAnswer, 16);
374 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
375 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");
376 return 3;
377 }
378
379 memcpy(blockData, receivedAnswer, 14);
380 return 0;
381 }
382
383
384
385 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
386 {
387 // variables
388 uint16_t len, i;
389 uint32_t pos;
390 uint8_t par[3] = {0}; // enough for 18 Bytes to send
391 byte_t res;
392
393 uint8_t d_block[18], d_block_enc[18];
394 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
395 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
396
397 // command MIFARE_CLASSIC_WRITEBLOCK
398 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
399
400 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
401 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
402 return 1;
403 }
404
405 memcpy(d_block, blockData, 16);
406 AppendCrc14443a(d_block, 16);
407
408 // crypto
409 for (pos = 0; pos < 18; pos++)
410 {
411 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
412 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
413 }
414
415 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
416
417 // Receive the response
418 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
419
420 res = 0;
421 for (i = 0; i < 4; i++)
422 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
423
424 if ((len != 1) || (res != 0x0A)) {
425 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
426 return 2;
427 }
428
429 return 0;
430 }
431
432 int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)
433 {
434 uint16_t len;
435 uint8_t par[3] = {0}; // enough for 18 parity bits
436 uint8_t d_block[18] = {0x00};
437 uint8_t receivedAnswer[MAX_FRAME_SIZE];
438 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
439
440 // command MIFARE_CLASSIC_WRITEBLOCK
441 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
442
443 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
444 if (MF_DBGLEVEL >= MF_DBG_ERROR)
445 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
446 return 1;
447 }
448
449 memcpy(d_block, blockData, 16);
450 AppendCrc14443a(d_block, 16);
451
452 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
453
454 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
455
456 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
457 if (MF_DBGLEVEL >= MF_DBG_ERROR)
458 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
459 return 2;
460 }
461 return 0;
462 }
463
464 int mifare_ultra_special_writeblock(uint8_t blockNo, uint8_t *blockData)
465 {
466 uint16_t len;
467 uint8_t d_block[8] = {0x00};
468 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
469 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
470
471 // command MIFARE_CLASSIC_WRITEBLOCK
472 d_block[0]= blockNo;
473 memcpy(d_block+1,blockData,4);
474 AppendCrc14443a(d_block, 6);
475
476 len = mifare_sendcmd_short_special(NULL, 1, 0xA2, d_block, receivedAnswer, receivedAnswerPar, NULL);
477
478 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
479 if (MF_DBGLEVEL >= MF_DBG_ERROR)
480 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
481 return 1;
482 }
483 return 0;
484 }
485
486 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
487 {
488 uint16_t len;
489 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
490 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
491
492 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
493 if (len != 0) {
494 if (MF_DBGLEVEL >= MF_DBG_ERROR)
495 Dbprintf("halt error. response len: %x", len);
496 return 1;
497 }
498
499 return 0;
500 }
501
502 int mifare_ultra_halt()
503 {
504 uint16_t len;
505 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
506 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
507
508 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
509 if (len != 0) {
510 if (MF_DBGLEVEL >= MF_DBG_ERROR)
511 Dbprintf("halt error. response len: %x", len);
512 return 1;
513 }
514 return 0;
515 }
516
517
518 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
519 // plus evtl. 8 sectors with 16 blocks each (4k cards)
520 uint8_t NumBlocksPerSector(uint8_t sectorNo)
521 {
522 if (sectorNo < 32)
523 return 4;
524 else
525 return 16;
526 }
527
528 uint8_t FirstBlockOfSector(uint8_t sectorNo)
529 {
530 if (sectorNo < 32)
531 return sectorNo * 4;
532 else
533 return 32*4 + (sectorNo - 32) * 16;
534
535 }
536
537
538 // work with emulator memory
539 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
540 uint8_t* emCARD = BigBuf_get_EM_addr();
541 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
542 }
543
544 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
545 uint8_t* emCARD = BigBuf_get_EM_addr();
546 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
547 }
548
549 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
550 uint8_t* emCARD = BigBuf_get_EM_addr();
551 memcpy(data, emCARD + bytePtr, byteCount);
552 }
553
554 int emlCheckValBl(int blockNum) {
555 uint8_t* emCARD = BigBuf_get_EM_addr();
556 uint8_t* data = emCARD + blockNum * 16;
557
558 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
559 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
560 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
561 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
562 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
563 (data[12] != (data[15] ^ 0xff))
564 )
565 return 1;
566 return 0;
567 }
568
569 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
570 uint8_t* emCARD = BigBuf_get_EM_addr();
571 uint8_t* data = emCARD + blockNum * 16;
572
573 if (emlCheckValBl(blockNum)) {
574 return 1;
575 }
576
577 memcpy(blReg, data, 4);
578 *blBlock = data[12];
579 return 0;
580 }
581
582 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
583 uint8_t* emCARD = BigBuf_get_EM_addr();
584 uint8_t* data = emCARD + blockNum * 16;
585
586 memcpy(data + 0, &blReg, 4);
587 memcpy(data + 8, &blReg, 4);
588 blReg = blReg ^ 0xffffffff;
589 memcpy(data + 4, &blReg, 4);
590
591 data[12] = blBlock;
592 data[13] = blBlock ^ 0xff;
593 data[14] = blBlock;
594 data[15] = blBlock ^ 0xff;
595
596 return 0;
597 }
598
599 uint64_t emlGetKey(int sectorNum, int keyType) {
600 uint8_t key[6];
601 uint8_t* emCARD = BigBuf_get_EM_addr();
602
603 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
604 return bytes_to_num(key, 6);
605 }
606
607 void emlClearMem(void) {
608 int b;
609
610 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
611 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
612 uint8_t* emCARD = BigBuf_get_EM_addr();
613
614 memset(emCARD, 0, CARD_MEMORY_SIZE);
615
616 // fill sectors trailer data
617 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
618 emlSetMem((uint8_t *)trailer, b , 1);
619 }
620
621 // uid
622 emlSetMem((uint8_t *)uid, 0, 1);
623 return;
624 }
625
626
627 // Mifare desfire commands
628 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)
629 {
630 uint8_t dcmd[5] = {0x00};
631 dcmd[0] = cmd;
632 memcpy(dcmd+1,data,2);
633 AppendCrc14443a(dcmd, 3);
634
635 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
636 int len = ReaderReceive(answer, answer_parity);
637 if(!len) {
638 if (MF_DBGLEVEL >= MF_DBG_ERROR)
639 Dbprintf("Authentication failed. Card timeout.");
640 return 1;
641 }
642 return len;
643 }
644
645 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)
646 {
647 uint8_t dcmd[20] = {0x00};
648 dcmd[0] = cmd;
649 memcpy(dcmd+1,data,17);
650 AppendCrc14443a(dcmd, 18);
651
652 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
653 int len = ReaderReceive(answer, answer_parity);
654 if(!len){
655 if (MF_DBGLEVEL >= MF_DBG_ERROR)
656 Dbprintf("Authentication failed. Card timeout.");
657 return 1;
658 }
659 return len;
660 }
661
662 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){
663
664 int len;
665 // load key, keynumber
666 uint8_t data[2]={0x0a, 0x00};
667 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
668 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
669
670 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);
671 if (len == 1) {
672 if (MF_DBGLEVEL >= MF_DBG_ERROR)
673 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
674 return 1;
675 }
676
677 if (len == 12) {
678 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
679 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
680 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
681 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
682 receivedAnswer[10],receivedAnswer[11]);
683 }
684 memcpy(blockData, receivedAnswer, 12);
685 return 0;
686 }
687 return 1;
688 }
689
690 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
691
692 int len;
693 uint8_t data[17] = {0x00};
694 data[0] = 0xAF;
695 memcpy(data+1,key,16);
696
697 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
698 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
699
700 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);
701
702 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {
703 if (MF_DBGLEVEL >= MF_DBG_ERROR)
704 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);
705 return 1;
706 }
707
708 if (len == 12){
709 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
710 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
711 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
712 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
713 receivedAnswer[10],receivedAnswer[11]);
714 }
715 memcpy(blockData, receivedAnswer, 12);
716 return 0;
717 }
718 return 1;
719 }
Impressum, Datenschutz