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