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