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