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