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