]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
ADD: a script to dump a specific type of Mifare Mini tags.
[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 #include "des.h"
22
23 int MF_DBGLEVEL = MF_DBG_ALL;
24
25 // crypto1 helpers
26 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
27 uint8_t bt = 0;
28 int i;
29
30 if (len != 1) {
31 for (i = 0; i < len; i++)
32 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
33 } else {
34 bt = 0;
35 for (i = 0; i < 4; i++)
36 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], i)) << i;
37
38 data[0] = bt;
39 }
40 return;
41 }
42
43 void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {
44 uint8_t bt = 0;
45 int i;
46 par[0] = 0;
47
48 for (i = 0; i < len; i++) {
49 bt = data[i];
50 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];
51 if((i&0x0007) == 0)
52 par[i>>3] = 0;
53 par[i>>3] |= (((filter(pcs->odd) ^ oddparity(bt)) & 0x01)<<(7-(i&0x0007)));
54 }
55 return;
56 }
57
58 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
59 uint8_t bt = 0;
60 int i;
61
62 for (i = 0; i < 4; i++)
63 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;
64
65 return bt;
66 }
67
68 // send 2 byte commands
69 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 {
71 return mifare_sendcmd_shortex(pcs, crypted, cmd, data, answer, answer_parity, timing);
72 }
73
74 // send X byte basic commands
75 int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
76 {
77 uint8_t dcmd[data_size+3];
78 dcmd[0] = cmd;
79 memcpy(dcmd+1,data,data_size);
80 AppendCrc14443a(dcmd, data_size+1);
81 ReaderTransmit(dcmd, sizeof(dcmd), timing);
82 int len = ReaderReceive(answer, answer_parity);
83 if(!len) {
84 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("%02X Cmd failed. Card timeout.", cmd);
85 len = ReaderReceive(answer,answer_parity);
86 //return 0;
87 }
88 return len;
89 }
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_short_mfuev1auth(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[7];
116 int len;
117 dcmd[0] = cmd;
118 memcpy(dcmd+1,data,4);
119 AppendCrc14443a(dcmd, 5);
120
121 ReaderTransmit(dcmd, sizeof(dcmd), timing);
122 len = ReaderReceive(answer, answer_parity);
123 if(!len) {
124 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed. Card timeout.");
125 len = ReaderReceive(answer,answer_parity);
126 }
127 if(len==1) {
128 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("NAK - Authentication failed.");
129 return 1;
130 }
131 return len;
132 }
133 */
134
135 // send 2 byte commands
136 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)
137 {
138 uint8_t dcmd[4], ecmd[4];
139 uint16_t pos, res;
140 uint8_t par[1]; // 1 Byte parity is enough here
141 dcmd[0] = cmd;
142 dcmd[1] = data;
143 AppendCrc14443a(dcmd, 2);
144
145 memcpy(ecmd, dcmd, sizeof(dcmd));
146
147 if (crypted) {
148 par[0] = 0;
149 for (pos = 0; pos < 4; pos++)
150 {
151 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
152 par[0] |= (((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) << (7-pos));
153 }
154
155 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
156
157 } else {
158 ReaderTransmit(dcmd, sizeof(dcmd), timing);
159 }
160
161 int len = ReaderReceive(answer, par);
162
163 if (answer_parity) *answer_parity = par[0];
164
165 if (crypted == CRYPT_ALL) {
166 if (len == 1) {
167 res = 0;
168 for (pos = 0; pos < 4; pos++)
169 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
170
171 answer[0] = res;
172
173 } else {
174 for (pos = 0; pos < len; pos++)
175 {
176 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
177 }
178 }
179 }
180
181 return len;
182 }
183
184 // mifare classic commands
185 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested)
186 {
187 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);
188 }
189
190 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)
191 {
192 // variables
193 int len;
194 uint32_t pos;
195 uint8_t tmp4[4];
196 uint8_t par[1] = {0x00};
197 byte_t nr[4];
198 uint32_t nt, ntpp; // Supplied tag nonce
199
200 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
201 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
202 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
203
204 // Transmit MIFARE_CLASSIC_AUTH
205 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);
206 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len);
207 if (len != 4) return 1;
208
209 // "random" reader nonce:
210 nr[0] = 0x55;
211 nr[1] = 0x41;
212 nr[2] = 0x49;
213 nr[3] = 0x92;
214
215 // Save the tag nonce (nt)
216 nt = bytes_to_num(receivedAnswer, 4);
217
218 // ----------------------------- crypto1 create
219 if (isNested)
220 crypto1_destroy(pcs);
221
222 // Init cipher with key
223 crypto1_create(pcs, ui64Key);
224
225 if (isNested == AUTH_NESTED) {
226 // decrypt nt with help of new key
227 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
228 } else {
229 // Load (plain) uid^nt into the cipher
230 crypto1_word(pcs, nt ^ uid, 0);
231 }
232
233 // some statistic
234 if (!ntptr && (MF_DBGLEVEL >= 3))
235 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
236
237 // save Nt
238 if (ntptr)
239 *ntptr = nt;
240
241 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
242 par[0] = 0;
243 for (pos = 0; pos < 4; pos++)
244 {
245 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];
246 par[0] |= (((filter(pcs->odd) ^ oddparity(nr[pos])) & 0x01) << (7-pos));
247 }
248
249 // Skip 32 bits in pseudo random generator
250 nt = prng_successor(nt,32);
251
252 // ar+parity
253 for (pos = 4; pos < 8; pos++)
254 {
255 nt = prng_successor(nt,8);
256 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
257 par[0] |= (((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) << (7-pos));
258 }
259
260 // Transmit reader nonce and reader answer
261 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
262
263 // Receive 4 byte tag answer
264 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
265 if (!len)
266 {
267 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
268 return 2;
269 }
270
271 memcpy(tmp4, receivedAnswer, 4);
272 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
273
274 if (ntpp != bytes_to_num(tmp4, 4)) {
275 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
276 return 3;
277 }
278
279 return 0;
280 }
281
282 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
283 {
284 // variables
285 int len;
286 uint8_t bt[2];
287
288 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
289 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
290
291 // command MIFARE_CLASSIC_READBLOCK
292 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
293 if (len == 1) {
294 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
295 return 1;
296 }
297 if (len != 18) {
298 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
299 return 2;
300 }
301
302 memcpy(bt, receivedAnswer + 16, 2);
303 AppendCrc14443a(receivedAnswer, 16);
304 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
305 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
306 return 3;
307 }
308
309 memcpy(blockData, receivedAnswer, 16);
310 return 0;
311 }
312
313 // mifare ultralight commands
314 int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){
315
316 uint16_t len;
317 uint8_t resp[4];
318 uint8_t respPar[1];
319 uint8_t key[4] = {0x00};
320 memcpy(key, keybytes, 4);
321
322 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
323 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);
324
325 len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);
326 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
327 if (len != 4) {
328 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);
329 return 0;
330 }
331
332 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
333 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);
334
335 memcpy(pack, resp, 4);
336 return 1;
337 }
338
339 int mifare_ultra_auth(uint8_t *keybytes){
340
341 /// 3des2k
342
343 uint8_t random_a[8] = {1,1,1,1,1,1,1,1};
344 uint8_t random_b[8] = {0x00};
345 uint8_t enc_random_b[8] = {0x00};
346 uint8_t rnd_ab[16] = {0x00};
347 uint8_t IV[8] = {0x00};
348 uint8_t key[16] = {0x00};
349 memcpy(key, keybytes, 16);
350
351 uint16_t len;
352 uint8_t resp[19] = {0x00};
353 uint8_t respPar[3] = {0,0,0};
354
355 // REQUEST AUTHENTICATION
356 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);
357 if (len != 11) {
358 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
359 return 0;
360 }
361
362 // tag nonce.
363 memcpy(enc_random_b,resp+1,8);
364
365 // decrypt nonce.
366 tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
367 rol(random_b,8);
368 memcpy(rnd_ab ,random_a,8);
369 memcpy(rnd_ab+8,random_b,8);
370
371 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
372 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
373 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]);
374
375 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
376 random_b[0],random_b[1],random_b[2],random_b[3],random_b[4],random_b[5],random_b[6],random_b[7]);
377
378 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
379 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
380
381 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
382 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15] );
383 }
384
385 // encrypt out, in, length, key, iv
386 tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
387 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
388 len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);
389 if (len != 11) {
390 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
391 return 0;
392 }
393
394 uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };
395 uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };
396 memcpy(enc_resp, resp+1, 8);
397
398 // decrypt out, in, length, key, iv
399 tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
400 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {
401 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");
402 return 0;
403 }
404
405 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
406 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
407 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],
408 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
409
410 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
411 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],
412 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);
413
414 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
415 random_a[0],random_a[1],random_a[2],random_a[3],
416 random_a[4],random_a[5],random_a[6],random_a[7]);
417
418 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
419 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],
420 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);
421 }
422 return 1;
423 }
424
425 int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)
426 {
427 uint16_t len;
428 uint8_t bt[2];
429 uint8_t receivedAnswer[MAX_FRAME_SIZE];
430 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
431
432
433 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
434 if (len == 1) {
435 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
436 return 1;
437 }
438 if (len != 18) {
439 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);
440 return 2;
441 }
442
443 memcpy(bt, receivedAnswer + 16, 2);
444 AppendCrc14443a(receivedAnswer, 16);
445 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
446 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");
447 return 3;
448 }
449
450 memcpy(blockData, receivedAnswer, 14);
451 return 0;
452 }
453
454 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
455 {
456 // variables
457 uint16_t len, i;
458 uint32_t pos = 0;
459 uint8_t par[3] = {0x00}; // enough for 18 Bytes to send
460 byte_t res = 0;
461
462 uint8_t d_block[18], d_block_enc[18];
463 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
464 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
465
466 // command MIFARE_CLASSIC_WRITEBLOCK
467 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
468
469 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
470 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
471 return 1;
472 }
473
474 memcpy(d_block, blockData, 16);
475 AppendCrc14443a(d_block, 16);
476
477 // crypto
478 for (pos = 0; pos < 18; pos++)
479 {
480 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
481 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
482 }
483
484 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
485
486 // Receive the response
487 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
488
489 res = 0;
490 for (i = 0; i < 4; i++)
491 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
492
493 if ((len != 1) || (res != 0x0A)) {
494 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
495 return 2;
496 }
497
498 return 0;
499 }
500
501 /* // command not needed, but left for future testing
502 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
503 {
504 uint16_t len;
505 uint8_t par[3] = {0}; // enough for 18 parity bits
506 uint8_t d_block[18] = {0x00};
507 uint8_t receivedAnswer[MAX_FRAME_SIZE];
508 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
509
510 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
511
512 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
513 if (MF_DBGLEVEL >= MF_DBG_ERROR)
514 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
515 return 1;
516 }
517
518 memcpy(d_block, blockData, 16);
519 AppendCrc14443a(d_block, 16);
520
521 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
522
523 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
524
525 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
526 if (MF_DBGLEVEL >= MF_DBG_ERROR)
527 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
528 return 2;
529 }
530 return 0;
531 }
532 */
533
534 int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)
535 {
536 uint16_t len;
537 uint8_t d_block[5] = {0x00};
538 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
539 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
540
541 // command MIFARE_CLASSIC_WRITEBLOCK
542 d_block[0]= blockNo;
543 memcpy(d_block+1,blockData,4);
544 //AppendCrc14443a(d_block, 6);
545
546 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);
547
548 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
549 if (MF_DBGLEVEL >= MF_DBG_ERROR)
550 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
551 return 1;
552 }
553 return 0;
554 }
555
556 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
557 {
558 uint16_t len;
559 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
560 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
561
562 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
563 if (len != 0) {
564 if (MF_DBGLEVEL >= MF_DBG_ERROR)
565 Dbprintf("halt error. response len: %x", len);
566 return 1;
567 }
568
569 return 0;
570 }
571
572 int mifare_ultra_halt()
573 {
574 uint16_t len;
575 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
576 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
577
578 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
579 if (len != 0) {
580 if (MF_DBGLEVEL >= MF_DBG_ERROR)
581 Dbprintf("halt error. response len: %x", len);
582 return 1;
583 }
584 return 0;
585 }
586
587
588 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
589 // plus evtl. 8 sectors with 16 blocks each (4k cards)
590 uint8_t NumBlocksPerSector(uint8_t sectorNo)
591 {
592 if (sectorNo < 32)
593 return 4;
594 else
595 return 16;
596 }
597
598 uint8_t FirstBlockOfSector(uint8_t sectorNo)
599 {
600 if (sectorNo < 32)
601 return sectorNo * 4;
602 else
603 return 32*4 + (sectorNo - 32) * 16;
604
605 }
606
607
608 // work with emulator memory
609 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
610 uint8_t* emCARD = BigBuf_get_EM_addr();
611 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
612 }
613
614 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
615 uint8_t* emCARD = BigBuf_get_EM_addr();
616 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
617 }
618
619 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
620 uint8_t* emCARD = BigBuf_get_EM_addr();
621 memcpy(data, emCARD + bytePtr, byteCount);
622 }
623
624 int emlCheckValBl(int blockNum) {
625 uint8_t* emCARD = BigBuf_get_EM_addr();
626 uint8_t* data = emCARD + blockNum * 16;
627
628 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
629 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
630 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
631 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
632 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
633 (data[12] != (data[15] ^ 0xff))
634 )
635 return 1;
636 return 0;
637 }
638
639 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
640 uint8_t* emCARD = BigBuf_get_EM_addr();
641 uint8_t* data = emCARD + blockNum * 16;
642
643 if (emlCheckValBl(blockNum)) {
644 return 1;
645 }
646
647 memcpy(blReg, data, 4);
648 *blBlock = data[12];
649 return 0;
650 }
651
652 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
653 uint8_t* emCARD = BigBuf_get_EM_addr();
654 uint8_t* data = emCARD + blockNum * 16;
655
656 memcpy(data + 0, &blReg, 4);
657 memcpy(data + 8, &blReg, 4);
658 blReg = blReg ^ 0xffffffff;
659 memcpy(data + 4, &blReg, 4);
660
661 data[12] = blBlock;
662 data[13] = blBlock ^ 0xff;
663 data[14] = blBlock;
664 data[15] = blBlock ^ 0xff;
665
666 return 0;
667 }
668
669 uint64_t emlGetKey(int sectorNum, int keyType) {
670 uint8_t key[6];
671 uint8_t* emCARD = BigBuf_get_EM_addr();
672
673 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
674 return bytes_to_num(key, 6);
675 }
676
677 void emlClearMem(void) {
678 int b;
679
680 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
681 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
682 uint8_t* emCARD = BigBuf_get_EM_addr();
683
684 memset(emCARD, 0, CARD_MEMORY_SIZE);
685
686 // fill sectors trailer data
687 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
688 emlSetMem((uint8_t *)trailer, b , 1);
689 }
690
691 // uid
692 emlSetMem((uint8_t *)uid, 0, 1);
693 return;
694 }
695
696
697 // Mifare desfire commands
698 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)
699 {
700 uint8_t dcmd[5] = {0x00};
701 dcmd[0] = cmd;
702 memcpy(dcmd+1,data,2);
703 AppendCrc14443a(dcmd, 3);
704
705 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
706 int len = ReaderReceive(answer, answer_parity);
707 if(!len) {
708 if (MF_DBGLEVEL >= MF_DBG_ERROR)
709 Dbprintf("Authentication failed. Card timeout.");
710 return 1;
711 }
712 return len;
713 }
714
715 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)
716 {
717 uint8_t dcmd[20] = {0x00};
718 dcmd[0] = cmd;
719 memcpy(dcmd+1,data,17);
720 AppendCrc14443a(dcmd, 18);
721
722 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
723 int len = ReaderReceive(answer, answer_parity);
724 if(!len){
725 if (MF_DBGLEVEL >= MF_DBG_ERROR)
726 Dbprintf("Authentication failed. Card timeout.");
727 return 1;
728 }
729 return len;
730 }
731
732 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){
733
734 int len;
735 // load key, keynumber
736 uint8_t data[2]={0x0a, 0x00};
737 uint8_t receivedAnswer[MAX_FRAME_SIZE];
738 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
739
740 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);
741 if (len == 1) {
742 if (MF_DBGLEVEL >= MF_DBG_ERROR)
743 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
744 return 1;
745 }
746
747 if (len == 12) {
748 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
749 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
750 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
751 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
752 receivedAnswer[10],receivedAnswer[11]);
753 }
754 memcpy(blockData, receivedAnswer, 12);
755 return 0;
756 }
757 return 1;
758 }
759
760 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
761
762 int len;
763 uint8_t data[17] = {0x00};
764 data[0] = 0xAF;
765 memcpy(data+1,key,16);
766
767 uint8_t receivedAnswer[MAX_FRAME_SIZE];
768 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
769
770 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);
771
772 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {
773 if (MF_DBGLEVEL >= MF_DBG_ERROR)
774 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);
775 return 1;
776 }
777
778 if (len == 12){
779 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
780 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
781 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
782 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
783 receivedAnswer[10],receivedAnswer[11]);
784 }
785 memcpy(blockData, receivedAnswer, 12);
786 return 0;
787 }
788 return 1;
789 }
Impressum, Datenschutz