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