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