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