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