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