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