]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifareutil.c
FIX: Coverity, logical vs bitwise operator, remove the extra '&' for it to become...
[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
f8ada309 21#include "parity.h"\r
4d2e4eea 22#include "des.h"\r
20f9a2a1 23\r
3de04590 24int MF_DBGLEVEL = MF_DBG_ALL;\r
f397b5cc 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
a501c82b 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
a501c82b 47 par[0] = 0;\r
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
a501c82b 52 if((i&0x0007) == 0) \r
53 par[i>>3] = 0;\r
f8ada309 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
95aeb706 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
64d1b4ef 71{\r
95aeb706 72 uint8_t dcmd[data_size+3];\r
64d1b4ef 73 dcmd[0] = cmd;\r
95aeb706 74 memcpy(dcmd+1,data,data_size);\r
75 AppendCrc14443a(dcmd, data_size+1);\r
76 ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
64d1b4ef 77 int len = ReaderReceive(answer, answer_parity);\r
78 if(!len) {\r
95aeb706 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
f38a1528 82 }\r
83 return len;\r
84}\r
85\r
95aeb706 86// send 2 byte commands\r
2d2f7d19 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
64d1b4ef 88{\r
89 uint8_t dcmd[4], ecmd[4];\r
a501c82b 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
a501c82b 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
f8ada309 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
a501c82b 112 int len = ReaderReceive(answer, par);\r
f89c7050 113 \r
a501c82b 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
225ccb91 135// mifare classic commands\r
a501c82b 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
a501c82b 147 uint8_t par[1] = {0x00};\r
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
a501c82b 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
a501c82b 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
a501c82b 193 par[0] = 0;\r
4abe4f58
M
194 for (pos = 0; pos < 4; pos++)\r
195 {\r
a501c82b 196 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];\r
f8ada309 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
f8ada309 208 par[0] |= (((filter(pcs->odd) ^ oddparity8(nt & 0xff)) & 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
a501c82b 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
a501c82b 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
64d1b4ef 261 return 0;\r
f38a1528 262}\r
263\r
225ccb91 264// mifare ultralight commands\r
4d2e4eea 265int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){\r
225ccb91 266\r
a501c82b 267 uint16_t len;\r
4d2e4eea 268 uint8_t resp[4];\r
269 uint8_t respPar[1];\r
8258f409 270 uint8_t key[4] = {0x00};\r
4d2e4eea 271 memcpy(key, keybytes, 4);\r
8258f409 272\r
95aeb706 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
95aeb706 275 len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);\r
0de8e387 276\r
4d2e4eea 277 if (len != 4) {\r
278 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);\r
e7e95088 279 return 0;\r
f38a1528 280 }\r
8258f409 281\r
4d2e4eea 282 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r
8258f409 283 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);\r
284\r
4d2e4eea 285 memcpy(pack, resp, 4);\r
e7e95088 286 return 1;\r
f38a1528 287}\r
288\r
4d2e4eea 289int mifare_ultra_auth(uint8_t *keybytes){\r
225ccb91 290\r
8258f409 291 /// 3des2k\r
292\r
4d2e4eea 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
8258f409 298 uint8_t key[16] = {0x00};\r
4d2e4eea 299 memcpy(key, keybytes, 16);\r
a631936e 300\r
a501c82b 301 uint16_t len;\r
4d2e4eea 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
307 if (len != 11) {\r
308 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
e7e95088 309 return 0;\r
4d2e4eea 310 }\r
311\r
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
8258f409 320\r
4d2e4eea 321 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
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
4d2e4eea 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
327\r
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
8258f409 330\r
4d2e4eea 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
8258f409 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
95aeb706 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
225ccb91 355 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
4d2e4eea 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
8258f409 367\r
4d2e4eea 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
f38a1528 371 }\r
e7e95088 372 return 1;\r
a631936e 373}\r
374\r
aa60d156 375int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)\r
64d1b4ef 376{\r
377 uint16_t len;\r
378 uint8_t bt[2];\r
aa60d156 379 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
380 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
3fe4ff4f 381 \r
95aeb706 382\r
64d1b4ef 383 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
384 if (len == 1) {\r
aa60d156 385 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
64d1b4ef 386 return 1;\r
387 }\r
388 if (len != 18) {\r
aa60d156 389 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);\r
64d1b4ef 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
aa60d156 396 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");\r
64d1b4ef 397 return 3;\r
398 }\r
399 \r
400 memcpy(blockData, receivedAnswer, 14);\r
401 return 0;\r
402}\r
403\r
64d1b4ef 404int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
405{\r
406 // variables\r
a501c82b 407 uint16_t len, i; \r
add0504d 408 uint32_t pos = 0;\r
409 uint8_t par[3] = {0x00}; // enough for 18 Bytes to send\r
410 byte_t res = 0;\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
a501c82b 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
f8ada309 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
a501c82b 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
64d1b4ef 448 return 0;\r
449}\r
450\r
95aeb706 451/* // command not needed, but left for future testing\r
452int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) \r
64d1b4ef 453{\r
f168b263 454 uint16_t len;\r
64d1b4ef 455 uint8_t par[3] = {0}; // enough for 18 parity bits\r
456 uint8_t d_block[18] = {0x00};\r
aa60d156 457 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
458 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
f168b263 459\r
64d1b4ef 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
225ccb91 463 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 464 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);\r
64d1b4ef 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
f168b263 473 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
64d1b4ef 474\r
475 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
225ccb91 476 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
64d1b4ef 477 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);\r
478 return 2;\r
f168b263 479 }\r
64d1b4ef 480 return 0;\r
f168b263 481}\r
95aeb706 482*/\r
64d1b4ef 483\r
95aeb706 484int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)\r
64d1b4ef 485{\r
486 uint16_t len;\r
95aeb706 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
64d1b4ef 490\r
491 // command MIFARE_CLASSIC_WRITEBLOCK\r
492 d_block[0]= blockNo;\r
493 memcpy(d_block+1,blockData,4);\r
95aeb706 494 //AppendCrc14443a(d_block, 6);\r
64d1b4ef 495\r
95aeb706 496 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);\r
64d1b4ef 497\r
498 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK\r
225ccb91 499 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
64d1b4ef 500 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);\r
501 return 1;\r
502 }\r
503 return 0;\r
504}\r
c2731f37 505int mifare_classic_halt_ex(struct Crypto1State *pcs) {\r
a501c82b 506 uint16_t len; \r
c2731f37 507 uint8_t receivedAnswer[4];\r
508 uint8_t receivedAnswerPar[4];\r
20f9a2a1 509\r
a501c82b 510 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
4abe4f58 511 if (len != 0) {\r
c2731f37 512 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("halt error. response len: %x", len); \r
20f9a2a1
M
513 return 1;\r
514 }\r
64d1b4ef 515 return 0;\r
516}\r
c2731f37 517int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) {\r
518 return mifare_classic_halt_ex(pcs);\r
519}\r
64d1b4ef 520\r
aa60d156 521int mifare_ultra_halt()\r
64d1b4ef 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
64d1b4ef 526 \r
527 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
528 if (len != 0) {\r
225ccb91 529 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
64d1b4ef 530 Dbprintf("halt error. response len: %x", len);\r
531 return 1;\r
225ccb91 532 }\r
64d1b4ef 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
64d1b4ef 557// work with emulator memory\r
558void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
0de8e387 559 emlSetMem_xt(data, blockNum, blocksCount, 16);\r
560}\r
561\r
562void emlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth) {\r
64d1b4ef 563 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0de8e387 564 memcpy(emCARD + blockNum * blockBtWidth, data, blocksCount * blockBtWidth);\r
8f51ddb0
M
565}\r
566\r
567void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
f71f4deb 568 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
569 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r
570}\r
571\r
572void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r
f71f4deb 573 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
574 memcpy(data, emCARD + bytePtr, byteCount);\r
575}\r
576\r
0014cb46 577int emlCheckValBl(int blockNum) {\r
f71f4deb 578 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
579 uint8_t* data = emCARD + blockNum * 16;\r
580\r
581 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||\r
582 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||\r
583 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||\r
584 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||\r
585 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||\r
586 (data[12] != (data[15] ^ 0xff))\r
587 ) \r
588 return 1;\r
589 return 0;\r
590}\r
591\r
592int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r
f71f4deb 593 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
594 uint8_t* data = emCARD + blockNum * 16;\r
595 \r
596 if (emlCheckValBl(blockNum)) {\r
597 return 1;\r
598 }\r
599 \r
600 memcpy(blReg, data, 4);\r
601 *blBlock = data[12];\r
0014cb46
M
602 return 0;\r
603}\r
604\r
605int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r
f71f4deb 606 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
607 uint8_t* data = emCARD + blockNum * 16;\r
608 \r
609 memcpy(data + 0, &blReg, 4);\r
610 memcpy(data + 8, &blReg, 4);\r
611 blReg = blReg ^ 0xffffffff;\r
612 memcpy(data + 4, &blReg, 4);\r
613 \r
614 data[12] = blBlock;\r
615 data[13] = blBlock ^ 0xff;\r
616 data[14] = blBlock;\r
617 data[15] = blBlock ^ 0xff;\r
618 \r
619 return 0;\r
620}\r
621\r
8556b852
M
622uint64_t emlGetKey(int sectorNum, int keyType) {\r
623 uint8_t key[6];\r
f71f4deb 624 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8556b852 625 \r
baeaf579 626 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r
8556b852
M
627 return bytes_to_num(key, 6);\r
628}\r
629\r
8f51ddb0 630void emlClearMem(void) {\r
aea4d766 631 int b;\r
8f51ddb0
M
632 \r
633 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
8f51ddb0 634 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r
f71f4deb 635 uint8_t* emCARD = BigBuf_get_EM_addr();\r
aea4d766 636 \r
a501c82b 637 memset(emCARD, 0, CARD_MEMORY_SIZE);\r
aea4d766 638 \r
639 // fill sectors trailer data\r
640 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {\r
641 emlSetMem((uint8_t *)trailer, b , 1);\r
642 } \r
8f51ddb0
M
643\r
644 // uid\r
645 emlSetMem((uint8_t *)uid, 0, 1);\r
646 return;\r
647}\r
6ff6ade2 648\r
225ccb91 649\r
650// Mifare desfire commands\r
6ff6ade2 651int 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
652{\r
653 uint8_t dcmd[5] = {0x00};\r
654 dcmd[0] = cmd;\r
655 memcpy(dcmd+1,data,2);\r
656 AppendCrc14443a(dcmd, 3);\r
657 \r
658 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
659 int len = ReaderReceive(answer, answer_parity);\r
660 if(!len) {\r
225ccb91 661 if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
662 Dbprintf("Authentication failed. Card timeout.");\r
663 return 1;\r
6ff6ade2 664 }\r
665 return len;\r
666}\r
667\r
668int 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
669{\r
670 uint8_t dcmd[20] = {0x00};\r
671 dcmd[0] = cmd;\r
672 memcpy(dcmd+1,data,17);\r
673 AppendCrc14443a(dcmd, 18);\r
674\r
675 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
676 int len = ReaderReceive(answer, answer_parity);\r
677 if(!len){\r
225ccb91 678 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
679 Dbprintf("Authentication failed. Card timeout.");\r
680 return 1;\r
6ff6ade2 681 }\r
682 return len;\r
683}\r
684\r
685int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r
225ccb91 686\r
6ff6ade2 687 int len;\r
e3ab50ca 688 // load key, keynumber\r
6ff6ade2 689 uint8_t data[2]={0x0a, 0x00};\r
aa60d156 690 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
691 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
6ff6ade2 692 \r
6ff6ade2 693 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r
694 if (len == 1) {\r
225ccb91 695 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
696 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
6ff6ade2 697 return 1;\r
698 }\r
699 \r
700 if (len == 12) {\r
225ccb91 701 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
702 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
703 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
704 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
705 receivedAnswer[10],receivedAnswer[11]);\r
706 }\r
707 memcpy(blockData, receivedAnswer, 12);\r
6ff6ade2 708 return 0;\r
709 }\r
710 return 1;\r
711}\r
712\r
713int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r
225ccb91 714\r
6ff6ade2 715 int len;\r
e3ab50ca 716 uint8_t data[17] = {0x00};\r
717 data[0] = 0xAF;\r
6ff6ade2 718 memcpy(data+1,key,16);\r
719 \r
aa60d156 720 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
721 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
6ff6ade2 722 \r
6ff6ade2 723 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r
724 \r
e3ab50ca 725 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r
225ccb91 726 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
727 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r
6ff6ade2 728 return 1;\r
729 }\r
225ccb91 730 \r
6ff6ade2 731 if (len == 12){\r
225ccb91 732 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
733 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
734 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
735 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
736 receivedAnswer[10],receivedAnswer[11]);\r
737 }\r
6ff6ade2 738 memcpy(blockData, receivedAnswer, 12);\r
739 return 0;\r
740 }\r
741 return 1;\r
64d1b4ef 742}\r
Impressum, Datenschutz