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