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