]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifareutil.c
Fix detection of AVX512 support for Apple clang compiler
[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
930763e8 12#include <string.h>\r
33443e7c 13#include "mifareutil.h"\r
20f9a2a1
M
14#include "proxmark3.h"\r
15#include "apps.h"\r
16#include "util.h"\r
1f065e1d 17#include "parity.h"\r
20f9a2a1
M
18\r
19#include "iso14443crc.h"\r
20#include "iso14443a.h"\r
33443e7c 21#include "crapto1/crapto1.h"\r
930763e8 22#include "polarssl/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
930763e8 293 des3_context ctx = { 0x00 };\r
8258f409 294 uint8_t random_a[8] = {1,1,1,1,1,1,1,1};\r
295 uint8_t random_b[8] = {0x00};\r
296 uint8_t enc_random_b[8] = {0x00};\r
297 uint8_t rnd_ab[16] = {0x00};\r
298 uint8_t IV[8] = {0x00};\r
299 uint8_t key[16] = {0x00};\r
300 memcpy(key, keybytes, 16);\r
a631936e 301\r
302 uint16_t len;\r
8258f409 303 uint8_t resp[19] = {0x00};\r
304 uint8_t respPar[3] = {0,0,0};\r
305\r
306 // REQUEST AUTHENTICATION\r
307 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);\r
f168b263 308 if (len != 11) {\r
8258f409 309 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
9d87eb66 310 return 0;\r
a631936e 311 }\r
a631936e 312\r
8258f409 313 // tag nonce.\r
314 memcpy(enc_random_b,resp+1,8);\r
315\r
316 // decrypt nonce.\r
930763e8 317 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );\r
318 des3_set2key_dec(&ctx, key);\r
319 des3_crypt_cbc(&ctx // des3_context\r
320 , DES_DECRYPT // int mode\r
321 , sizeof(random_b) // length\r
322 , IV // iv[8]\r
323 , enc_random_b // input\r
324 , random_b // output\r
325 );\r
326\r
8258f409 327 rol(random_b,8);\r
328 memcpy(rnd_ab ,random_a,8);\r
329 memcpy(rnd_ab+8,random_b,8);\r
330\r
a631936e 331 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
8258f409 332 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",\r
333 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 334\r
8258f409 335 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",\r
336 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 337\r
8258f409 338 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r
339 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
340\r
341 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r
342 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
343 }\r
344\r
345 // encrypt out, in, length, key, iv\r
930763e8 346 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);\r
347 des3_set2key_enc(&ctx, key);\r
348 des3_crypt_cbc(&ctx // des3_context\r
349 , DES_ENCRYPT // int mode\r
350 , sizeof(rnd_ab) // length\r
351 , enc_random_b // iv[8]\r
352 , rnd_ab // input\r
353 , rnd_ab // output\r
354 );\r
355\r
4973f23d 356 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);\r
357 len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);\r
f168b263 358 if (len != 11) {\r
8258f409 359 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
9d87eb66 360 return 0;\r
a631936e 361 }\r
8258f409 362\r
363 uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };\r
364 uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };\r
365 memcpy(enc_resp, resp+1, 8);\r
366\r
367 // decrypt out, in, length, key, iv \r
930763e8 368 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);\r
369 des3_set2key_dec(&ctx, key);\r
370 des3_crypt_cbc(&ctx // des3_context\r
371 , DES_DECRYPT // int mode\r
372 , 8 // length\r
373 , enc_random_b // iv[8]\r
374 , enc_resp // input\r
375 , resp_random_a // output\r
376 );\r
8258f409 377 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {\r
378 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");\r
9d87eb66 379 return 0;\r
8258f409 380 }\r
381\r
a631936e 382 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
8258f409 383 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x", \r
384 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],\r
385 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r
386\r
387 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",\r
388 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],\r
389 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);\r
390\r
391 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",\r
392 random_a[0],random_a[1],random_a[2],random_a[3],\r
393 random_a[4],random_a[5],random_a[6],random_a[7]);\r
394\r
395 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",\r
396 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],\r
397 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);\r
a631936e 398 }\r
9d87eb66 399 return 1;\r
a631936e 400}\r
401\r
f168b263 402int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)\r
403{\r
404 uint16_t len;\r
405 uint8_t bt[2];\r
406 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
407 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
3fe4ff4f 408 \r
4973f23d 409\r
f168b263 410 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
411 if (len == 1) {\r
412 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
413 return 1;\r
414 }\r
415 if (len != 18) {\r
416 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);\r
417 return 2;\r
418 }\r
419 \r
420 memcpy(bt, receivedAnswer + 16, 2);\r
421 AppendCrc14443a(receivedAnswer, 16);\r
422 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r
423 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");\r
424 return 3;\r
425 }\r
426 \r
427 memcpy(blockData, receivedAnswer, 14);\r
428 return 0;\r
429}\r
430\r
f168b263 431int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r
432{\r
433 // variables\r
3fe4ff4f 434 uint16_t len, i; \r
20f9a2a1 435 uint32_t pos;\r
6a1f2d82 436 uint8_t par[3] = {0}; // enough for 18 Bytes to send\r
4abe4f58 437 byte_t res;\r
20f9a2a1
M
438 \r
439 uint8_t d_block[18], d_block_enc[18];\r
f71f4deb 440 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
441 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
20f9a2a1 442 \r
4abe4f58 443 // command MIFARE_CLASSIC_WRITEBLOCK\r
6a1f2d82 444 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
20f9a2a1
M
445\r
446 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
f397b5cc 447 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
20f9a2a1
M
448 return 1;\r
449 }\r
450 \r
451 memcpy(d_block, blockData, 16);\r
452 AppendCrc14443a(d_block, 16);\r
453 \r
454 // crypto\r
4abe4f58
M
455 for (pos = 0; pos < 18; pos++)\r
456 {\r
457 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r
1f065e1d 458 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r
4abe4f58 459 } \r
20f9a2a1 460\r
9492e0b0 461 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r
20f9a2a1 462\r
4abe4f58 463 // Receive the response\r
6a1f2d82 464 len = ReaderReceive(receivedAnswer, receivedAnswerPar); \r
20f9a2a1
M
465\r
466 res = 0;\r
467 for (i = 0; i < 4; i++)\r
468 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;\r
469\r
470 if ((len != 1) || (res != 0x0A)) {\r
f397b5cc 471 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r
20f9a2a1
M
472 return 2;\r
473 }\r
474 \r
f168b263 475 return 0;\r
476}\r
477\r
4973f23d 478/* // command not needed, but left for future testing\r
479int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) \r
f168b263 480{\r
481 uint16_t len;\r
482 uint8_t par[3] = {0}; // enough for 18 parity bits\r
483 uint8_t d_block[18] = {0x00};\r
484 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
485 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
486\r
f168b263 487 len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
488\r
489 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
787b5bd8 490 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 491 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);\r
492 return 1;\r
493 }\r
494\r
495 memcpy(d_block, blockData, 16);\r
496 AppendCrc14443a(d_block, 16);\r
497\r
498 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);\r
499\r
500 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
501\r
502 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
787b5bd8 503 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 504 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);\r
505 return 2;\r
506 }\r
507 return 0;\r
508}\r
4973f23d 509*/\r
f168b263 510\r
4973f23d 511int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)\r
f168b263 512{\r
513 uint16_t len;\r
4973f23d 514 uint8_t d_block[5] = {0x00};\r
f71f4deb 515 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
516 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
f168b263 517\r
518 // command MIFARE_CLASSIC_WRITEBLOCK\r
519 d_block[0]= blockNo;\r
520 memcpy(d_block+1,blockData,4);\r
4973f23d 521 //AppendCrc14443a(d_block, 6);\r
f168b263 522\r
4973f23d 523 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);\r
f168b263 524\r
525 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK\r
787b5bd8 526 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 527 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);\r
528 return 1;\r
529 }\r
530 return 0;\r
531}\r
532\r
533int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) \r
534{\r
6a1f2d82 535 uint16_t len; \r
f71f4deb 536 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
537 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
20f9a2a1 538\r
6a1f2d82 539 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
4abe4f58 540 if (len != 0) {\r
787b5bd8 541 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
542 Dbprintf("halt error. response len: %x", len); \r
20f9a2a1
M
543 return 1;\r
544 }\r
545\r
f168b263 546 return 0;\r
547}\r
548\r
549int mifare_ultra_halt()\r
550{\r
551 uint16_t len;\r
f71f4deb 552 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
553 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
f168b263 554 \r
555 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
556 if (len != 0) {\r
787b5bd8 557 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 558 Dbprintf("halt error. response len: %x", len);\r
559 return 1;\r
560 }\r
561 return 0;\r
562}\r
563\r
baeaf579 564\r
565// Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),\r
566// plus evtl. 8 sectors with 16 blocks each (4k cards)\r
567uint8_t NumBlocksPerSector(uint8_t sectorNo) \r
568{\r
569 if (sectorNo < 32) \r
570 return 4;\r
571 else\r
572 return 16;\r
573}\r
574\r
575uint8_t FirstBlockOfSector(uint8_t sectorNo) \r
576{\r
577 if (sectorNo < 32)\r
578 return sectorNo * 4;\r
579 else\r
580 return 32*4 + (sectorNo - 32) * 16;\r
581 \r
582}\r
583\r
584\r
f168b263 585// work with emulator memory\r
586void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
587 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
588 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);\r
589}\r
590\r
591void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
f71f4deb 592 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
593 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r
594}\r
595\r
596void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r
f71f4deb 597 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
598 memcpy(data, emCARD + bytePtr, byteCount);\r
599}\r
600\r
0014cb46 601int emlCheckValBl(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 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||\r
606 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||\r
607 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||\r
608 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||\r
609 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||\r
610 (data[12] != (data[15] ^ 0xff))\r
611 ) \r
612 return 1;\r
613 return 0;\r
614}\r
615\r
616int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r
f71f4deb 617 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
618 uint8_t* data = emCARD + blockNum * 16;\r
619 \r
620 if (emlCheckValBl(blockNum)) {\r
621 return 1;\r
622 }\r
623 \r
624 memcpy(blReg, data, 4);\r
625 *blBlock = data[12];\r
0014cb46
M
626 return 0;\r
627}\r
628\r
629int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r
f71f4deb 630 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
631 uint8_t* data = emCARD + blockNum * 16;\r
632 \r
633 memcpy(data + 0, &blReg, 4);\r
634 memcpy(data + 8, &blReg, 4);\r
635 blReg = blReg ^ 0xffffffff;\r
636 memcpy(data + 4, &blReg, 4);\r
637 \r
638 data[12] = blBlock;\r
639 data[13] = blBlock ^ 0xff;\r
640 data[14] = blBlock;\r
641 data[15] = blBlock ^ 0xff;\r
642 \r
643 return 0;\r
644}\r
645\r
8556b852
M
646uint64_t emlGetKey(int sectorNum, int keyType) {\r
647 uint8_t key[6];\r
f71f4deb 648 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8556b852 649 \r
baeaf579 650 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r
8556b852
M
651 return bytes_to_num(key, 6);\r
652}\r
653\r
8f51ddb0 654void emlClearMem(void) {\r
aea4d766 655 int b;\r
8f51ddb0
M
656 \r
657 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
8f51ddb0 658 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r
f71f4deb 659 uint8_t* emCARD = BigBuf_get_EM_addr();\r
aea4d766 660 \r
6a1f2d82 661 memset(emCARD, 0, CARD_MEMORY_SIZE);\r
aea4d766 662 \r
663 // fill sectors trailer data\r
664 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {\r
665 emlSetMem((uint8_t *)trailer, b , 1);\r
666 } \r
8f51ddb0
M
667\r
668 // uid\r
669 emlSetMem((uint8_t *)uid, 0, 1);\r
670 return;\r
671}\r
c8b6da22 672\r
673\r
674// Mifare desfire commands\r
675int 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
676{\r
677 uint8_t dcmd[5] = {0x00};\r
678 dcmd[0] = cmd;\r
679 memcpy(dcmd+1,data,2);\r
680 AppendCrc14443a(dcmd, 3);\r
681 \r
682 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
683 int len = ReaderReceive(answer, answer_parity);\r
684 if(!len) {\r
685 if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
686 Dbprintf("Authentication failed. Card timeout.");\r
687 return 1;\r
688 }\r
689 return len;\r
690}\r
691\r
692int 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
693{\r
694 uint8_t dcmd[20] = {0x00};\r
695 dcmd[0] = cmd;\r
696 memcpy(dcmd+1,data,17);\r
697 AppendCrc14443a(dcmd, 18);\r
698\r
699 ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
700 int len = ReaderReceive(answer, answer_parity);\r
701 if(!len){\r
702 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
703 Dbprintf("Authentication failed. Card timeout.");\r
704 return 1;\r
705 }\r
706 return len;\r
707}\r
708\r
709int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r
710\r
711 int len;\r
712 // load key, keynumber\r
713 uint8_t data[2]={0x0a, 0x00};\r
29250969 714 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
715 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
c8b6da22 716 \r
717 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r
718 if (len == 1) {\r
719 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
720 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
721 return 1;\r
722 }\r
723 \r
724 if (len == 12) {\r
725 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
726 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
727 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
728 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
729 receivedAnswer[10],receivedAnswer[11]);\r
730 }\r
731 memcpy(blockData, receivedAnswer, 12);\r
732 return 0;\r
733 }\r
734 return 1;\r
735}\r
736\r
737int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r
738\r
739 int len;\r
740 uint8_t data[17] = {0x00};\r
741 data[0] = 0xAF;\r
742 memcpy(data+1,key,16);\r
743 \r
f71f4deb 744 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
745 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
c8b6da22 746 \r
747 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r
748 \r
749 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r
750 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
751 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r
752 return 1;\r
753 }\r
754 \r
755 if (len == 12){\r
756 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
757 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
758 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
759 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
760 receivedAnswer[10],receivedAnswer[11]);\r
761 }\r
762 memcpy(blockData, receivedAnswer, 12);\r
763 return 0;\r
764 }\r
765 return 1;\r
f168b263 766}\r
Impressum, Datenschutz