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