]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
c706e78a4a5d7f022a193e5c6d3a34a1dc1ae192
[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 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);
323 len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);
324 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
325 if (len != 4) {
326 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);
327 return 0;
328 }
329
330 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
331 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);
332
333 memcpy(pack, resp, 4);
334 return 1;
335 }
336
337 int mifare_ultra_auth(uint8_t *keybytes){
338
339 /// 3des2k
340
341 uint8_t random_a[8] = {1,1,1,1,1,1,1,1};
342 uint8_t random_b[8] = {0x00};
343 uint8_t enc_random_b[8] = {0x00};
344 uint8_t rnd_ab[16] = {0x00};
345 uint8_t IV[8] = {0x00};
346 uint8_t key[16] = {0x00};
347 memcpy(key, keybytes, 16);
348
349 uint16_t len;
350 uint8_t resp[19] = {0x00};
351 uint8_t respPar[3] = {0,0,0};
352
353 // REQUEST AUTHENTICATION
354 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);
355 if (len != 11) {
356 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
357 return 0;
358 }
359
360 // tag nonce.
361 memcpy(enc_random_b,resp+1,8);
362
363 // decrypt nonce.
364 tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
365 rol(random_b,8);
366 memcpy(rnd_ab ,random_a,8);
367 memcpy(rnd_ab+8,random_b,8);
368
369 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
370 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
371 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]);
372
373 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
374 random_b[0],random_b[1],random_b[2],random_b[3],random_b[4],random_b[5],random_b[6],random_b[7]);
375
376 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
377 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
378
379 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
380 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15] );
381 }
382
383 // encrypt out, in, length, key, iv
384 tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
385 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
386 len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);
387 if (len != 11) {
388 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
389 return 0;
390 }
391
392 uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };
393 uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };
394 memcpy(enc_resp, resp+1, 8);
395
396 // decrypt out, in, length, key, iv
397 tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
398 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {
399 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");
400 return 0;
401 }
402
403 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
404 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
405 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],
406 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
407
408 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
409 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],
410 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);
411
412 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
413 random_a[0],random_a[1],random_a[2],random_a[3],
414 random_a[4],random_a[5],random_a[6],random_a[7]);
415
416 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
417 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],
418 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);
419 }
420 return 1;
421 }
422
423 int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)
424 {
425 uint16_t len;
426 uint8_t bt[2];
427 uint8_t receivedAnswer[MAX_FRAME_SIZE];
428 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
429
430
431 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
432 if (len == 1) {
433 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
434 return 1;
435 }
436 if (len != 18) {
437 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);
438 return 2;
439 }
440
441 memcpy(bt, receivedAnswer + 16, 2);
442 AppendCrc14443a(receivedAnswer, 16);
443 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
444 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");
445 return 3;
446 }
447
448 memcpy(blockData, receivedAnswer, 14);
449 return 0;
450 }
451
452 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
453 {
454 // variables
455 uint16_t len, i;
456 uint32_t pos;
457 uint8_t par[3] = {0}; // enough for 18 Bytes to send
458 byte_t res;
459
460 uint8_t d_block[18], d_block_enc[18];
461 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
462 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
463
464 // command MIFARE_CLASSIC_WRITEBLOCK
465 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
466
467 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
468 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
469 return 1;
470 }
471
472 memcpy(d_block, blockData, 16);
473 AppendCrc14443a(d_block, 16);
474
475 // crypto
476 for (pos = 0; pos < 18; pos++)
477 {
478 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
479 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
480 }
481
482 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
483
484 // Receive the response
485 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
486
487 res = 0;
488 for (i = 0; i < 4; i++)
489 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
490
491 if ((len != 1) || (res != 0x0A)) {
492 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
493 return 2;
494 }
495
496 return 0;
497 }
498
499 /* // command not needed, but left for future testing
500 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
501 {
502 uint16_t len;
503 uint8_t par[3] = {0}; // enough for 18 parity bits
504 uint8_t d_block[18] = {0x00};
505 uint8_t receivedAnswer[MAX_FRAME_SIZE];
506 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
507
508 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
509
510 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
511 if (MF_DBGLEVEL >= MF_DBG_ERROR)
512 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
513 return 1;
514 }
515
516 memcpy(d_block, blockData, 16);
517 AppendCrc14443a(d_block, 16);
518
519 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
520
521 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
522
523 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
524 if (MF_DBGLEVEL >= MF_DBG_ERROR)
525 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
526 return 2;
527 }
528 return 0;
529 }
530 */
531
532 int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)
533 {
534 uint16_t len;
535 uint8_t d_block[5] = {0x00};
536 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
537 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
538
539 // command MIFARE_CLASSIC_WRITEBLOCK
540 d_block[0]= blockNo;
541 memcpy(d_block+1,blockData,4);
542 //AppendCrc14443a(d_block, 6);
543
544 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);
545
546 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
547 if (MF_DBGLEVEL >= MF_DBG_ERROR)
548 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
549 return 1;
550 }
551 return 0;
552 }
553
554 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
555 {
556 uint16_t len;
557 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
558 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
559
560 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
561 if (len != 0) {
562 if (MF_DBGLEVEL >= MF_DBG_ERROR)
563 Dbprintf("halt error. response len: %x", len);
564 return 1;
565 }
566
567 return 0;
568 }
569
570 int mifare_ultra_halt()
571 {
572 uint16_t len;
573 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
574 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
575
576 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
577 if (len != 0) {
578 if (MF_DBGLEVEL >= MF_DBG_ERROR)
579 Dbprintf("halt error. response len: %x", len);
580 return 1;
581 }
582 return 0;
583 }
584
585
586 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
587 // plus evtl. 8 sectors with 16 blocks each (4k cards)
588 uint8_t NumBlocksPerSector(uint8_t sectorNo)
589 {
590 if (sectorNo < 32)
591 return 4;
592 else
593 return 16;
594 }
595
596 uint8_t FirstBlockOfSector(uint8_t sectorNo)
597 {
598 if (sectorNo < 32)
599 return sectorNo * 4;
600 else
601 return 32*4 + (sectorNo - 32) * 16;
602
603 }
604
605
606 // work with emulator memory
607 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
608 uint8_t* emCARD = BigBuf_get_EM_addr();
609 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
610 }
611
612 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
613 uint8_t* emCARD = BigBuf_get_EM_addr();
614 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
615 }
616
617 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
618 uint8_t* emCARD = BigBuf_get_EM_addr();
619 memcpy(data, emCARD + bytePtr, byteCount);
620 }
621
622 int emlCheckValBl(int blockNum) {
623 uint8_t* emCARD = BigBuf_get_EM_addr();
624 uint8_t* data = emCARD + blockNum * 16;
625
626 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
627 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
628 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
629 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
630 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
631 (data[12] != (data[15] ^ 0xff))
632 )
633 return 1;
634 return 0;
635 }
636
637 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
638 uint8_t* emCARD = BigBuf_get_EM_addr();
639 uint8_t* data = emCARD + blockNum * 16;
640
641 if (emlCheckValBl(blockNum)) {
642 return 1;
643 }
644
645 memcpy(blReg, data, 4);
646 *blBlock = data[12];
647 return 0;
648 }
649
650 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
651 uint8_t* emCARD = BigBuf_get_EM_addr();
652 uint8_t* data = emCARD + blockNum * 16;
653
654 memcpy(data + 0, &blReg, 4);
655 memcpy(data + 8, &blReg, 4);
656 blReg = blReg ^ 0xffffffff;
657 memcpy(data + 4, &blReg, 4);
658
659 data[12] = blBlock;
660 data[13] = blBlock ^ 0xff;
661 data[14] = blBlock;
662 data[15] = blBlock ^ 0xff;
663
664 return 0;
665 }
666
667 uint64_t emlGetKey(int sectorNum, int keyType) {
668 uint8_t key[6];
669 uint8_t* emCARD = BigBuf_get_EM_addr();
670
671 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
672 return bytes_to_num(key, 6);
673 }
674
675 void emlClearMem(void) {
676 int b;
677
678 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
679 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
680 uint8_t* emCARD = BigBuf_get_EM_addr();
681
682 memset(emCARD, 0, CARD_MEMORY_SIZE);
683
684 // fill sectors trailer data
685 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
686 emlSetMem((uint8_t *)trailer, b , 1);
687 }
688
689 // uid
690 emlSetMem((uint8_t *)uid, 0, 1);
691 return;
692 }
693
694
695 // Mifare desfire commands
696 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)
697 {
698 uint8_t dcmd[5] = {0x00};
699 dcmd[0] = cmd;
700 memcpy(dcmd+1,data,2);
701 AppendCrc14443a(dcmd, 3);
702
703 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
704 int len = ReaderReceive(answer, answer_parity);
705 if(!len) {
706 if (MF_DBGLEVEL >= MF_DBG_ERROR)
707 Dbprintf("Authentication failed. Card timeout.");
708 return 1;
709 }
710 return len;
711 }
712
713 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)
714 {
715 uint8_t dcmd[20] = {0x00};
716 dcmd[0] = cmd;
717 memcpy(dcmd+1,data,17);
718 AppendCrc14443a(dcmd, 18);
719
720 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
721 int len = ReaderReceive(answer, answer_parity);
722 if(!len){
723 if (MF_DBGLEVEL >= MF_DBG_ERROR)
724 Dbprintf("Authentication failed. Card timeout.");
725 return 1;
726 }
727 return len;
728 }
729
730 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){
731
732 int len;
733 // load key, keynumber
734 uint8_t data[2]={0x0a, 0x00};
735 uint8_t receivedAnswer[MAX_FRAME_SIZE];
736 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
737
738 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);
739 if (len == 1) {
740 if (MF_DBGLEVEL >= MF_DBG_ERROR)
741 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
742 return 1;
743 }
744
745 if (len == 12) {
746 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
747 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
748 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
749 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
750 receivedAnswer[10],receivedAnswer[11]);
751 }
752 memcpy(blockData, receivedAnswer, 12);
753 return 0;
754 }
755 return 1;
756 }
757
758 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
759
760 int len;
761 uint8_t data[17] = {0x00};
762 data[0] = 0xAF;
763 memcpy(data+1,key,16);
764
765 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
766 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
767
768 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);
769
770 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {
771 if (MF_DBGLEVEL >= MF_DBG_ERROR)
772 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);
773 return 1;
774 }
775
776 if (len == 12){
777 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
778 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
779 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
780 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
781 receivedAnswer[10],receivedAnswer[11]);
782 }
783 memcpy(blockData, receivedAnswer, 12);
784 return 0;
785 }
786 return 1;
787 }
Impressum, Datenschutz