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