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