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