]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifareutil.c
Merge pull request #461 from merlokk/14a_rework3
[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_decryptEx(struct Crypto1State *pcs, uint8_t *data_in, int len, uint8_t *data_out){\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_out[i] = crypto1_byte(pcs, 0x00, 0) ^ data_in[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_in[0], i)) << i;\r
38 \r
39 data_out[0] = bt;\r
40 }\r
41 return;\r
42}\r
43\r
44void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){\r
45 mf_crypto1_decryptEx(pcs, data, len, data);\r
46}\r
47\r
48void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {\r
49 uint8_t bt = 0;\r
50 int i;\r
51 par[0] = 0;\r
52 \r
53 for (i = 0; i < len; i++) {\r
54 bt = data[i];\r
55 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r
56 if((i&0x0007) == 0) \r
57 par[i>>3] = 0;\r
58 par[i>>3] |= (((filter(pcs->odd) ^ oddparity8(bt)) & 0x01)<<(7-(i&0x0007)));\r
59 } \r
60 return;\r
61}\r
62\r
63uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {\r
64 uint8_t bt = 0;\r
65 int i;\r
66\r
67 for (i = 0; i < 4; i++)\r
68 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;\r
69 \r
70 return bt;\r
71}\r
72\r
73// send X byte basic commands\r
74int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r
75{\r
76 uint8_t dcmd[data_size+3];\r
77 dcmd[0] = cmd;\r
78 memcpy(dcmd+1,data,data_size);\r
79 AppendCrc14443a(dcmd, data_size+1);\r
80 ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
81 int len = ReaderReceive(answer, answer_parity);\r
82 if(!len) {\r
83 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("%02X Cmd failed. Card timeout.", cmd);\r
84 len = ReaderReceive(answer,answer_parity);\r
85 //return 0;\r
86 }\r
87 return len;\r
88}\r
89\r
90// send 2 byte commands\r
91int 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
92{\r
93 uint8_t dcmd[4], ecmd[4];\r
94 uint16_t pos, res;\r
95 uint8_t par[1]; // 1 Byte parity is enough here\r
96 dcmd[0] = cmd;\r
97 dcmd[1] = data;\r
98 AppendCrc14443a(dcmd, 2);\r
99 \r
100 memcpy(ecmd, dcmd, sizeof(dcmd));\r
101 \r
102 if (crypted) {\r
103 par[0] = 0;\r
104 for (pos = 0; pos < 4; pos++)\r
105 {\r
106 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];\r
107 par[0] |= (((filter(pcs->odd) ^ oddparity8(dcmd[pos])) & 0x01) << (7-pos));\r
108 } \r
109\r
110 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);\r
111\r
112 } else {\r
113 ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
114 }\r
115\r
116 int len = ReaderReceive(answer, par);\r
117 \r
118 if (answer_parity) *answer_parity = par[0];\r
119 \r
120 if (crypted == CRYPT_ALL) {\r
121 if (len == 1) {\r
122 res = 0;\r
123 for (pos = 0; pos < 4; pos++)\r
124 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;\r
125 \r
126 answer[0] = res;\r
127 \r
128 } else {\r
129 for (pos = 0; pos < len; pos++)\r
130 {\r
131 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];\r
132 }\r
133 }\r
134 }\r
135 \r
136 return len;\r
137}\r
138\r
139// mifare classic commands\r
140int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested) \r
141{\r
142 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);\r
143}\r
144\r
145int 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
146{\r
147 // variables\r
148 int len; \r
149 uint32_t pos;\r
150 uint8_t tmp4[4];\r
151 uint8_t par[1] = {0x00};\r
152 byte_t nr[4];\r
153 uint32_t nt, ntpp; // Supplied tag nonce\r
154 \r
155 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };\r
156 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
157 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
158 \r
159 // Transmit MIFARE_CLASSIC_AUTH\r
160 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);\r
161 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len); \r
162 if (len != 4) return 1;\r
163 \r
164 // "random" reader nonce:\r
165 nr[0] = 0x55;\r
166 nr[1] = 0x41;\r
167 nr[2] = 0x49;\r
168 nr[3] = 0x92; \r
169 \r
170 // Save the tag nonce (nt)\r
171 nt = bytes_to_num(receivedAnswer, 4);\r
172\r
173 // ----------------------------- crypto1 create\r
174 if (isNested)\r
175 crypto1_destroy(pcs);\r
176\r
177 // Init cipher with key\r
178 crypto1_create(pcs, ui64Key);\r
179\r
180 if (isNested == AUTH_NESTED) {\r
181 // decrypt nt with help of new key \r
182 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;\r
183 } else {\r
184 // Load (plain) uid^nt into the cipher\r
185 crypto1_word(pcs, nt ^ uid, 0);\r
186 }\r
187\r
188 // some statistic\r
189 if (!ntptr && (MF_DBGLEVEL >= 3))\r
190 Dbprintf("auth uid: %08x nt: %08x", uid, nt); \r
191 \r
192 // save Nt\r
193 if (ntptr)\r
194 *ntptr = nt;\r
195\r
196 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)\r
197 par[0] = 0;\r
198 for (pos = 0; pos < 4; pos++)\r
199 {\r
200 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];\r
201 par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));\r
202 } \r
203 \r
204 // Skip 32 bits in pseudo random generator\r
205 nt = prng_successor(nt,32);\r
206\r
207 // ar+parity\r
208 for (pos = 4; pos < 8; pos++)\r
209 {\r
210 nt = prng_successor(nt,8);\r
211 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r
212 par[0] |= (((filter(pcs->odd) ^ oddparity8(nt)) & 0x01) << (7-pos));\r
213 } \r
214 \r
215 // Transmit reader nonce and reader answer\r
216 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);\r
217\r
218 // Receive 4 byte tag answer\r
219 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
220 if (!len)\r
221 {\r
222 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");\r
223 return 2;\r
224 }\r
225 \r
226 memcpy(tmp4, receivedAnswer, 4);\r
227 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);\r
228 \r
229 if (ntpp != bytes_to_num(tmp4, 4)) {\r
230 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");\r
231 return 3;\r
232 }\r
233\r
234 return 0;\r
235}\r
236\r
237int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
238{\r
239 // variables\r
240 int len; \r
241 uint8_t bt[2];\r
242 \r
243 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
244 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
245 \r
246 // command MIFARE_CLASSIC_READBLOCK\r
247 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
248 if (len == 1) {\r
249 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
250 return 1;\r
251 }\r
252 if (len != 18) {\r
253 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len); \r
254 return 2;\r
255 }\r
256\r
257 memcpy(bt, receivedAnswer + 16, 2);\r
258 AppendCrc14443a(receivedAnswer, 16);\r
259 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r
260 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error."); \r
261 return 3;\r
262 }\r
263 \r
264 memcpy(blockData, receivedAnswer, 16);\r
265 return 0;\r
266}\r
267\r
268// mifare ultralight commands\r
269int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){\r
270\r
271 uint16_t len;\r
272 uint8_t resp[4];\r
273 uint8_t respPar[1];\r
274 uint8_t key[4] = {0x00};\r
275 memcpy(key, keybytes, 4);\r
276\r
277 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r
278 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);\r
279 len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);\r
280 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);\r
281 if (len != 4) {\r
282 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);\r
283 return 0;\r
284 }\r
285\r
286 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r
287 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);\r
288\r
289 memcpy(pack, resp, 4);\r
290 return 1;\r
291}\r
292\r
293int mifare_ultra_auth(uint8_t *keybytes){\r
294\r
295 /// 3des2k\r
296\r
297 des3_context ctx = { 0x00 };\r
298 uint8_t random_a[8] = {1,1,1,1,1,1,1,1};\r
299 uint8_t random_b[8] = {0x00};\r
300 uint8_t enc_random_b[8] = {0x00};\r
301 uint8_t rnd_ab[16] = {0x00};\r
302 uint8_t IV[8] = {0x00};\r
303 uint8_t key[16] = {0x00};\r
304 memcpy(key, keybytes, 16);\r
305\r
306 uint16_t len;\r
307 uint8_t resp[19] = {0x00};\r
308 uint8_t respPar[3] = {0,0,0};\r
309\r
310 // REQUEST AUTHENTICATION\r
311 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);\r
312 if (len != 11) {\r
313 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
314 return 0;\r
315 }\r
316\r
317 // tag nonce.\r
318 memcpy(enc_random_b,resp+1,8);\r
319\r
320 // decrypt nonce.\r
321 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );\r
322 des3_set2key_dec(&ctx, key);\r
323 des3_crypt_cbc(&ctx // des3_context\r
324 , DES_DECRYPT // int mode\r
325 , sizeof(random_b) // length\r
326 , IV // iv[8]\r
327 , enc_random_b // input\r
328 , random_b // output\r
329 );\r
330\r
331 rol(random_b,8);\r
332 memcpy(rnd_ab ,random_a,8);\r
333 memcpy(rnd_ab+8,random_b,8);\r
334\r
335 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
336 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",\r
337 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
338\r
339 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",\r
340 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
341\r
342 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r
343 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
344\r
345 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r
346 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
347 }\r
348\r
349 // encrypt out, in, length, key, iv\r
350 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);\r
351 des3_set2key_enc(&ctx, key);\r
352 des3_crypt_cbc(&ctx // des3_context\r
353 , DES_ENCRYPT // int mode\r
354 , sizeof(rnd_ab) // length\r
355 , enc_random_b // iv[8]\r
356 , rnd_ab // input\r
357 , rnd_ab // output\r
358 );\r
359\r
360 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);\r
361 len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);\r
362 if (len != 11) {\r
363 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
364 return 0;\r
365 }\r
366\r
367 uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };\r
368 uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };\r
369 memcpy(enc_resp, resp+1, 8);\r
370\r
371 // decrypt out, in, length, key, iv \r
372 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);\r
373 des3_set2key_dec(&ctx, key);\r
374 des3_crypt_cbc(&ctx // des3_context\r
375 , DES_DECRYPT // int mode\r
376 , 8 // length\r
377 , enc_random_b // iv[8]\r
378 , enc_resp // input\r
379 , resp_random_a // output\r
380 );\r
381 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {\r
382 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");\r
383 return 0;\r
384 }\r
385\r
386 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
387 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x", \r
388 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],\r
389 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r
390\r
391 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",\r
392 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],\r
393 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);\r
394\r
395 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",\r
396 random_a[0],random_a[1],random_a[2],random_a[3],\r
397 random_a[4],random_a[5],random_a[6],random_a[7]);\r
398\r
399 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",\r
400 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],\r
401 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);\r
402 }\r
403 return 1;\r
404}\r
405\r
406int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)\r
407{\r
408 uint16_t len;\r
409 uint8_t bt[2];\r
410 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
411 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
412 \r
413\r
414 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
415 if (len == 1) {\r
416 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
417 return 1;\r
418 }\r
419 if (len != 18) {\r
420 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);\r
421 return 2;\r
422 }\r
423 \r
424 memcpy(bt, receivedAnswer + 16, 2);\r
425 AppendCrc14443a(receivedAnswer, 16);\r
426 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r
427 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");\r
428 return 3;\r
429 }\r
430 \r
431 memcpy(blockData, receivedAnswer, 14);\r
432 return 0;\r
433}\r
434\r
435int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
436{\r
437 // variables\r
438 uint16_t len, i; \r
439 uint32_t pos;\r
440 uint8_t par[3] = {0}; // enough for 18 Bytes to send\r
441 byte_t res;\r
442 \r
443 uint8_t d_block[18], d_block_enc[18];\r
444 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
445 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
446 \r
447 // command MIFARE_CLASSIC_WRITEBLOCK\r
448 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
449\r
450 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
451 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
452 return 1;\r
453 }\r
454 \r
455 memcpy(d_block, blockData, 16);\r
456 AppendCrc14443a(d_block, 16);\r
457 \r
458 // crypto\r
459 for (pos = 0; pos < 18; pos++)\r
460 {\r
461 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r
462 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r
463 } \r
464\r
465 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r
466\r
467 // Receive the response\r
468 len = ReaderReceive(receivedAnswer, receivedAnswerPar); \r
469\r
470 res = 0;\r
471 for (i = 0; i < 4; i++)\r
472 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;\r
473\r
474 if ((len != 1) || (res != 0x0A)) {\r
475 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r
476 return 2;\r
477 }\r
478 \r
479 return 0;\r
480}\r
481\r
482/* // command not needed, but left for future testing\r
483int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) \r
484{\r
485 uint16_t len;\r
486 uint8_t par[3] = {0}; // enough for 18 parity bits\r
487 uint8_t d_block[18] = {0x00};\r
488 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
489 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
490\r
491 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
492\r
493 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
494 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
495 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);\r
496 return 1;\r
497 }\r
498\r
499 memcpy(d_block, blockData, 16);\r
500 AppendCrc14443a(d_block, 16);\r
501\r
502 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);\r
503\r
504 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
505\r
506 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
507 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
508 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);\r
509 return 2;\r
510 }\r
511 return 0;\r
512}\r
513*/\r
514\r
515int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)\r
516{\r
517 uint16_t len;\r
518 uint8_t d_block[5] = {0x00};\r
519 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
520 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
521\r
522 // command MIFARE_CLASSIC_WRITEBLOCK\r
523 d_block[0]= blockNo;\r
524 memcpy(d_block+1,blockData,4);\r
525 //AppendCrc14443a(d_block, 6);\r
526\r
527 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);\r
528\r
529 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK\r
530 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
531 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);\r
532 return 1;\r
533 }\r
534 return 0;\r
535}\r
536\r
537int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) \r
538{\r
539 uint16_t len; \r
540 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
541 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
542\r
543 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
544 if (len != 0) {\r
545 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
546 Dbprintf("halt error. response len: %x", len); \r
547 return 1;\r
548 }\r
549\r
550 return 0;\r
551}\r
552\r
553int mifare_ultra_halt()\r
554{\r
555 uint16_t len;\r
556 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
557 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
558 \r
559 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
560 if (len != 0) {\r
561 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
562 Dbprintf("halt error. response len: %x", len);\r
563 return 1;\r
564 }\r
565 return 0;\r
566}\r
567\r
568\r
569// Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),\r
570// plus evtl. 8 sectors with 16 blocks each (4k cards)\r
571uint8_t NumBlocksPerSector(uint8_t sectorNo) \r
572{\r
573 if (sectorNo < 32) \r
574 return 4;\r
575 else\r
576 return 16;\r
577}\r
578\r
579uint8_t FirstBlockOfSector(uint8_t sectorNo) \r
580{\r
581 if (sectorNo < 32)\r
582 return sectorNo * 4;\r
583 else\r
584 return 32*4 + (sectorNo - 32) * 16;\r
585 \r
586}\r
587\r
588\r
589// work with emulator memory\r
590void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
591 uint8_t* emCARD = BigBuf_get_EM_addr();\r
592 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);\r
593}\r
594\r
595void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
596 uint8_t* emCARD = BigBuf_get_EM_addr();\r
597 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r
598}\r
599\r
600void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r
601 uint8_t* emCARD = BigBuf_get_EM_addr();\r
602 memcpy(data, emCARD + bytePtr, byteCount);\r
603}\r
604\r
605int emlCheckValBl(int blockNum) {\r
606 uint8_t* emCARD = BigBuf_get_EM_addr();\r
607 uint8_t* data = emCARD + blockNum * 16;\r
608\r
609 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||\r
610 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||\r
611 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||\r
612 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||\r
613 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||\r
614 (data[12] != (data[15] ^ 0xff))\r
615 ) \r
616 return 1;\r
617 return 0;\r
618}\r
619\r
620int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r
621 uint8_t* emCARD = BigBuf_get_EM_addr();\r
622 uint8_t* data = emCARD + blockNum * 16;\r
623 \r
624 if (emlCheckValBl(blockNum)) {\r
625 return 1;\r
626 }\r
627 \r
628 memcpy(blReg, data, 4);\r
629 *blBlock = data[12];\r
630 return 0;\r
631}\r
632\r
633int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r
634 uint8_t* emCARD = BigBuf_get_EM_addr();\r
635 uint8_t* data = emCARD + blockNum * 16;\r
636 \r
637 memcpy(data + 0, &blReg, 4);\r
638 memcpy(data + 8, &blReg, 4);\r
639 blReg = blReg ^ 0xffffffff;\r
640 memcpy(data + 4, &blReg, 4);\r
641 \r
642 data[12] = blBlock;\r
643 data[13] = blBlock ^ 0xff;\r
644 data[14] = blBlock;\r
645 data[15] = blBlock ^ 0xff;\r
646 \r
647 return 0;\r
648}\r
649\r
650uint64_t emlGetKey(int sectorNum, int keyType) {\r
651 uint8_t key[6];\r
652 uint8_t* emCARD = BigBuf_get_EM_addr();\r
653 \r
654 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r
655 return bytes_to_num(key, 6);\r
656}\r
657\r
658void emlClearMem(void) {\r
659 int b;\r
660 \r
661 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
662 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r
663 uint8_t* emCARD = BigBuf_get_EM_addr();\r
664 \r
665 memset(emCARD, 0, CARD_MEMORY_SIZE);\r
666 \r
667 // fill sectors trailer data\r
668 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {\r
669 emlSetMem((uint8_t *)trailer, b , 1);\r
670 } \r
671\r
672 // uid\r
673 emlSetMem((uint8_t *)uid, 0, 1);\r
674 return;\r
675}\r
676\r
677\r
678// Mifare desfire commands\r
679int 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
680{\r
681 uint8_t dcmd[5] = {0x00};\r
682 dcmd[0] = cmd;\r
683 memcpy(dcmd+1,data,2);\r
684 AppendCrc14443a(dcmd, 3);\r
685 \r
686 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
687 int len = ReaderReceive(answer, answer_parity);\r
688 if(!len) {\r
689 if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
690 Dbprintf("Authentication failed. Card timeout.");\r
691 return 1;\r
692 }\r
693 return len;\r
694}\r
695\r
696int 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
697{\r
698 uint8_t dcmd[20] = {0x00};\r
699 dcmd[0] = cmd;\r
700 memcpy(dcmd+1,data,17);\r
701 AppendCrc14443a(dcmd, 18);\r
702\r
703 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
704 int len = ReaderReceive(answer, answer_parity);\r
705 if(!len){\r
706 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
707 Dbprintf("Authentication failed. Card timeout.");\r
708 return 1;\r
709 }\r
710 return len;\r
711}\r
712\r
713int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r
714\r
715 int len;\r
716 // load key, keynumber\r
717 uint8_t data[2]={0x0a, 0x00};\r
718 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
719 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
720 \r
721 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r
722 if (len == 1) {\r
723 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
724 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
725 return 1;\r
726 }\r
727 \r
728 if (len == 12) {\r
729 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
730 Dbprintf("Auth1 Resp: %02x%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
740\r
741int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r
742\r
743 int len;\r
744 uint8_t data[17] = {0x00};\r
745 data[0] = 0xAF;\r
746 memcpy(data+1,key,16);\r
747 \r
748 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
749 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
750 \r
751 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r
752 \r
753 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r
754 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
755 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r
756 return 1;\r
757 }\r
758 \r
759 if (len == 12){\r
760 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
761 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
762 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
763 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
764 receivedAnswer[10],receivedAnswer[11]);\r
765 }\r
766 memcpy(blockData, receivedAnswer, 12);\r
767 return 0;\r
768 }\r
769 return 1;\r
770}\r
771\r
772//-----------------------------------------------------------------------------\r
773// MIFARE check keys\r
774//\r
775//-----------------------------------------------------------------------------\r
776// one key check\r
777int MifareChkBlockKey(uint8_t *uid, uint32_t *cuid, uint8_t *cascade_levels, uint64_t ui64Key, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel) {\r
778\r
779 struct Crypto1State mpcs = {0, 0};\r
780 struct Crypto1State *pcs;\r
781 pcs = &mpcs;\r
782\r
783 // Iceman: use piwi's faster nonce collecting part in hardnested.\r
784 if (*cascade_levels == 0) { // need a full select cycle to get the uid first\r
785 iso14a_card_select_t card_info;\r
786 if(!iso14443a_select_card(uid, &card_info, cuid, true, 0, true)) {\r
787 if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card");\r
788 return 1;\r
789 }\r
790 switch (card_info.uidlen) {\r
791 case 4 : *cascade_levels = 1; break;\r
792 case 7 : *cascade_levels = 2; break;\r
793 case 10: *cascade_levels = 3; break;\r
794 default: break;\r
795 }\r
796 } else { // no need for anticollision. We can directly select the card\r
797 if(!iso14443a_select_card(uid, NULL, NULL, false, *cascade_levels, true)) {\r
798 if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels);\r
799 return 1;\r
800 }\r
801 }\r
802 \r
803 if(mifare_classic_auth(pcs, *cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
804// SpinDelayUs(AUTHENTICATION_TIMEOUT); // it not needs because mifare_classic_auth have timeout from iso14a_set_timeout()\r
805 return 2;\r
806 } else {\r
807/* // let it be here. it like halt command, but maybe it will work in some strange cases\r
808 uint8_t dummy_answer = 0;\r
809 ReaderTransmit(&dummy_answer, 1, NULL);\r
810 int timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT; \r
811 // wait for the card to become ready again\r
812 while(GetCountSspClk() < timeout) {};\r
813*/\r
814 // it needs after success authentication\r
815 mifare_classic_halt(pcs, *cuid);\r
816 }\r
817 \r
818 return 0;\r
819}\r
820\r
821// multi key check\r
822int MifareChkBlockKeys(uint8_t *keys, uint8_t keyCount, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel) {\r
823 uint8_t uid[10];\r
824 uint32_t cuid = 0;\r
825 uint8_t cascade_levels = 0;\r
826 uint64_t ui64Key = 0;\r
827\r
828 int retryCount = 0;\r
829 for (uint8_t i = 0; i < keyCount; i++) {\r
830\r
831 // Allow button press / usb cmd to interrupt device\r
832 if (BUTTON_PRESS() && !usb_poll_validate_length()) { \r
833 Dbprintf("ChkKeys: Cancel operation. Exit...");\r
834 return -2;\r
835 }\r
836\r
837 ui64Key = bytes_to_num(keys + i * 6, 6);\r
838 int res = MifareChkBlockKey(uid, &cuid, &cascade_levels, ui64Key, blockNo, keyType, debugLevel);\r
839 \r
840 // can't select\r
841 if (res == 1) {\r
842 retryCount++;\r
843 if (retryCount >= 5) {\r
844 Dbprintf("ChkKeys: block=%d key=%d. Can't select. Exit...", blockNo, keyType);\r
845 return -1;\r
846 }\r
847 --i; // try the same key once again\r
848\r
849 SpinDelay(20);\r
850// Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);\r
851 continue;\r
852 }\r
853 \r
854 // can't authenticate\r
855 if (res == 2) {\r
856 retryCount = 0;\r
857 continue; // can't auth. wrong key.\r
858 }\r
859\r
860 return i + 1;\r
861 }\r
862 \r
863 return 0;\r
864}\r
865\r
866// multisector multikey check\r
867int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint8_t debugLevel, TKeyIndex *keyIndex) {\r
868 int res = 0;\r
869 \r
870// int clk = GetCountSspClk();\r
871\r
872 for(int sc = 0; sc < SectorCount; sc++){\r
873 WDT_HIT();\r
874\r
875 int keyAB = keyType;\r
876 do {\r
877 res = MifareChkBlockKeys(keys, keyCount, FirstBlockOfSector(sc), keyAB & 0x01, debugLevel);\r
878 if (res < 0){\r
879 return res;\r
880 }\r
881 if (res > 0){\r
882 (*keyIndex)[keyAB & 0x01][sc] = res;\r
883 }\r
884 } while(--keyAB > 0);\r
885 }\r
886 \r
887// Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));\r
888 \r
889 return 0;\r
890}\r
891\r
892\r
Impressum, Datenschutz