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