]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifareutil.c
Merge pull request #337 from Proxmark/iceman1001-patch-4
[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 "mifareutil.h"\r
13#include "proxmark3.h"\r
14#include "apps.h"\r
15#include "util.h"\r
16#include "parity.h"\r
17#include "string.h"\r
18\r
19#include "iso14443crc.h"\r
20#include "iso14443a.h"\r
21#include "crapto1/crapto1.h"\r
22#include "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 uint8_t random_a[8] = {1,1,1,1,1,1,1,1};\r
294 uint8_t random_b[8] = {0x00};\r
295 uint8_t enc_random_b[8] = {0x00};\r
296 uint8_t rnd_ab[16] = {0x00};\r
297 uint8_t IV[8] = {0x00};\r
298 uint8_t key[16] = {0x00};\r
299 memcpy(key, keybytes, 16);\r
300\r
301 uint16_t len;\r
302 uint8_t resp[19] = {0x00};\r
303 uint8_t respPar[3] = {0,0,0};\r
304\r
305 // REQUEST AUTHENTICATION\r
306 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);\r
307 if (len != 11) {\r
308 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
309 return 0;\r
310 }\r
311\r
312 // tag nonce.\r
313 memcpy(enc_random_b,resp+1,8);\r
314\r
315 // decrypt nonce.\r
316 tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );\r
317 rol(random_b,8);\r
318 memcpy(rnd_ab ,random_a,8);\r
319 memcpy(rnd_ab+8,random_b,8);\r
320\r
321 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
322 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",\r
323 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
324\r
325 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",\r
326 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
327\r
328 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r
329 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
330\r
331 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r
332 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
333 }\r
334\r
335 // encrypt out, in, length, key, iv\r
336 tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);\r
337 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);\r
338 len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);\r
339 if (len != 11) {\r
340 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
341 return 0;\r
342 }\r
343\r
344 uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };\r
345 uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };\r
346 memcpy(enc_resp, resp+1, 8);\r
347\r
348 // decrypt out, in, length, key, iv \r
349 tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);\r
350 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {\r
351 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");\r
352 return 0;\r
353 }\r
354\r
355 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
356 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x", \r
357 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],\r
358 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r
359\r
360 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",\r
361 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],\r
362 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);\r
363\r
364 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",\r
365 random_a[0],random_a[1],random_a[2],random_a[3],\r
366 random_a[4],random_a[5],random_a[6],random_a[7]);\r
367\r
368 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",\r
369 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],\r
370 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);\r
371 }\r
372 return 1;\r
373}\r
374\r
375int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)\r
376{\r
377 uint16_t len;\r
378 uint8_t bt[2];\r
379 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
380 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
381 \r
382\r
383 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
384 if (len == 1) {\r
385 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
386 return 1;\r
387 }\r
388 if (len != 18) {\r
389 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);\r
390 return 2;\r
391 }\r
392 \r
393 memcpy(bt, receivedAnswer + 16, 2);\r
394 AppendCrc14443a(receivedAnswer, 16);\r
395 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r
396 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");\r
397 return 3;\r
398 }\r
399 \r
400 memcpy(blockData, receivedAnswer, 14);\r
401 return 0;\r
402}\r
403\r
404int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
405{\r
406 // variables\r
407 uint16_t len, i; \r
408 uint32_t pos;\r
409 uint8_t par[3] = {0}; // enough for 18 Bytes to send\r
410 byte_t res;\r
411 \r
412 uint8_t d_block[18], d_block_enc[18];\r
413 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
414 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
415 \r
416 // command MIFARE_CLASSIC_WRITEBLOCK\r
417 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
418\r
419 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
420 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
421 return 1;\r
422 }\r
423 \r
424 memcpy(d_block, blockData, 16);\r
425 AppendCrc14443a(d_block, 16);\r
426 \r
427 // crypto\r
428 for (pos = 0; pos < 18; pos++)\r
429 {\r
430 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r
431 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r
432 } \r
433\r
434 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r
435\r
436 // Receive the response\r
437 len = ReaderReceive(receivedAnswer, receivedAnswerPar); \r
438\r
439 res = 0;\r
440 for (i = 0; i < 4; i++)\r
441 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;\r
442\r
443 if ((len != 1) || (res != 0x0A)) {\r
444 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r
445 return 2;\r
446 }\r
447 \r
448 return 0;\r
449}\r
450\r
451/* // command not needed, but left for future testing\r
452int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) \r
453{\r
454 uint16_t len;\r
455 uint8_t par[3] = {0}; // enough for 18 parity bits\r
456 uint8_t d_block[18] = {0x00};\r
457 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
458 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
459\r
460 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
461\r
462 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
463 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
464 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);\r
465 return 1;\r
466 }\r
467\r
468 memcpy(d_block, blockData, 16);\r
469 AppendCrc14443a(d_block, 16);\r
470\r
471 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);\r
472\r
473 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
474\r
475 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
476 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
477 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);\r
478 return 2;\r
479 }\r
480 return 0;\r
481}\r
482*/\r
483\r
484int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)\r
485{\r
486 uint16_t len;\r
487 uint8_t d_block[5] = {0x00};\r
488 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
489 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
490\r
491 // command MIFARE_CLASSIC_WRITEBLOCK\r
492 d_block[0]= blockNo;\r
493 memcpy(d_block+1,blockData,4);\r
494 //AppendCrc14443a(d_block, 6);\r
495\r
496 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);\r
497\r
498 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK\r
499 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
500 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);\r
501 return 1;\r
502 }\r
503 return 0;\r
504}\r
505\r
506int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) \r
507{\r
508 uint16_t len; \r
509 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
510 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
511\r
512 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
513 if (len != 0) {\r
514 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
515 Dbprintf("halt error. response len: %x", len); \r
516 return 1;\r
517 }\r
518\r
519 return 0;\r
520}\r
521\r
522int mifare_ultra_halt()\r
523{\r
524 uint16_t len;\r
525 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
526 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
527 \r
528 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
529 if (len != 0) {\r
530 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
531 Dbprintf("halt error. response len: %x", len);\r
532 return 1;\r
533 }\r
534 return 0;\r
535}\r
536\r
537\r
538// Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),\r
539// plus evtl. 8 sectors with 16 blocks each (4k cards)\r
540uint8_t NumBlocksPerSector(uint8_t sectorNo) \r
541{\r
542 if (sectorNo < 32) \r
543 return 4;\r
544 else\r
545 return 16;\r
546}\r
547\r
548uint8_t FirstBlockOfSector(uint8_t sectorNo) \r
549{\r
550 if (sectorNo < 32)\r
551 return sectorNo * 4;\r
552 else\r
553 return 32*4 + (sectorNo - 32) * 16;\r
554 \r
555}\r
556\r
557\r
558// work with emulator memory\r
559void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
560 uint8_t* emCARD = BigBuf_get_EM_addr();\r
561 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);\r
562}\r
563\r
564void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
565 uint8_t* emCARD = BigBuf_get_EM_addr();\r
566 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r
567}\r
568\r
569void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r
570 uint8_t* emCARD = BigBuf_get_EM_addr();\r
571 memcpy(data, emCARD + bytePtr, byteCount);\r
572}\r
573\r
574int emlCheckValBl(int blockNum) {\r
575 uint8_t* emCARD = BigBuf_get_EM_addr();\r
576 uint8_t* data = emCARD + blockNum * 16;\r
577\r
578 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||\r
579 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||\r
580 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||\r
581 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||\r
582 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||\r
583 (data[12] != (data[15] ^ 0xff))\r
584 ) \r
585 return 1;\r
586 return 0;\r
587}\r
588\r
589int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r
590 uint8_t* emCARD = BigBuf_get_EM_addr();\r
591 uint8_t* data = emCARD + blockNum * 16;\r
592 \r
593 if (emlCheckValBl(blockNum)) {\r
594 return 1;\r
595 }\r
596 \r
597 memcpy(blReg, data, 4);\r
598 *blBlock = data[12];\r
599 return 0;\r
600}\r
601\r
602int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r
603 uint8_t* emCARD = BigBuf_get_EM_addr();\r
604 uint8_t* data = emCARD + blockNum * 16;\r
605 \r
606 memcpy(data + 0, &blReg, 4);\r
607 memcpy(data + 8, &blReg, 4);\r
608 blReg = blReg ^ 0xffffffff;\r
609 memcpy(data + 4, &blReg, 4);\r
610 \r
611 data[12] = blBlock;\r
612 data[13] = blBlock ^ 0xff;\r
613 data[14] = blBlock;\r
614 data[15] = blBlock ^ 0xff;\r
615 \r
616 return 0;\r
617}\r
618\r
619uint64_t emlGetKey(int sectorNum, int keyType) {\r
620 uint8_t key[6];\r
621 uint8_t* emCARD = BigBuf_get_EM_addr();\r
622 \r
623 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r
624 return bytes_to_num(key, 6);\r
625}\r
626\r
627void emlClearMem(void) {\r
628 int b;\r
629 \r
630 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
631 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r
632 uint8_t* emCARD = BigBuf_get_EM_addr();\r
633 \r
634 memset(emCARD, 0, CARD_MEMORY_SIZE);\r
635 \r
636 // fill sectors trailer data\r
637 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {\r
638 emlSetMem((uint8_t *)trailer, b , 1);\r
639 } \r
640\r
641 // uid\r
642 emlSetMem((uint8_t *)uid, 0, 1);\r
643 return;\r
644}\r
645\r
646\r
647// Mifare desfire commands\r
648int 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
649{\r
650 uint8_t dcmd[5] = {0x00};\r
651 dcmd[0] = cmd;\r
652 memcpy(dcmd+1,data,2);\r
653 AppendCrc14443a(dcmd, 3);\r
654 \r
655 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
656 int len = ReaderReceive(answer, answer_parity);\r
657 if(!len) {\r
658 if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
659 Dbprintf("Authentication failed. Card timeout.");\r
660 return 1;\r
661 }\r
662 return len;\r
663}\r
664\r
665int 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
666{\r
667 uint8_t dcmd[20] = {0x00};\r
668 dcmd[0] = cmd;\r
669 memcpy(dcmd+1,data,17);\r
670 AppendCrc14443a(dcmd, 18);\r
671\r
672 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
673 int len = ReaderReceive(answer, answer_parity);\r
674 if(!len){\r
675 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
676 Dbprintf("Authentication failed. Card timeout.");\r
677 return 1;\r
678 }\r
679 return len;\r
680}\r
681\r
682int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r
683\r
684 int len;\r
685 // load key, keynumber\r
686 uint8_t data[2]={0x0a, 0x00};\r
687 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
688 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
689 \r
690 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r
691 if (len == 1) {\r
692 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
693 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
694 return 1;\r
695 }\r
696 \r
697 if (len == 12) {\r
698 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
699 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
700 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
701 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
702 receivedAnswer[10],receivedAnswer[11]);\r
703 }\r
704 memcpy(blockData, receivedAnswer, 12);\r
705 return 0;\r
706 }\r
707 return 1;\r
708}\r
709\r
710int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r
711\r
712 int len;\r
713 uint8_t data[17] = {0x00};\r
714 data[0] = 0xAF;\r
715 memcpy(data+1,key,16);\r
716 \r
717 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
718 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
719 \r
720 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r
721 \r
722 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r
723 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
724 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r
725 return 1;\r
726 }\r
727 \r
728 if (len == 12){\r
729 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
730 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
731 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
732 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
733 receivedAnswer[10],receivedAnswer[11]);\r
734 }\r
735 memcpy(blockData, receivedAnswer, 12);\r
736 return 0;\r
737 }\r
738 return 1;\r
739}\r
Impressum, Datenschutz