]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
dcea901d98766f0d02704afd73ed8c647fe3a07b
[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 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 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
95 int len = ReaderReceive(answer, answer_parity);
96 if(!len) {
97 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
98 return 2;
99 }
100 return len;
101 }
102
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)
104 {
105 uint8_t dcmd[19];
106 int len;
107 dcmd[0] = cmd;
108 memcpy(dcmd+1,data,16);
109 AppendCrc14443a(dcmd, 17);
110
111 ReaderTransmit(dcmd, sizeof(dcmd), timing);
112 len = ReaderReceive(answer, answer_parity);
113 if(!len) {
114 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
115 len = ReaderReceive(answer,answer_parity);
116 }
117 if(len==1) {
118 if (MF_DBGLEVEL >= 1) Dbprintf("NAK - Authentication failed.");
119 return 1;
120 }
121 return len;
122 }
123
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)
125 {
126 uint8_t dcmd[4], ecmd[4];
127 uint16_t pos, res;
128 uint8_t par[1]; // 1 Byte parity is enough here
129 dcmd[0] = cmd;
130 dcmd[1] = data;
131 AppendCrc14443a(dcmd, 2);
132
133 memcpy(ecmd, dcmd, sizeof(dcmd));
134
135 if (crypted) {
136 par[0] = 0;
137 for (pos = 0; pos < 4; pos++)
138 {
139 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
140 par[0] |= (((filter(pcs->odd) ^ oddparity(dcmd[pos])) & 0x01) << (7-pos));
141 }
142
143 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
144
145 } else {
146 ReaderTransmit(dcmd, sizeof(dcmd), timing);
147 }
148
149 int len = ReaderReceive(answer, par);
150
151 if (answer_parity) *answer_parity = par[0];
152
153 if (crypted == CRYPT_ALL) {
154 if (len == 1) {
155 res = 0;
156 for (pos = 0; pos < 4; pos++)
157 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
158
159 answer[0] = res;
160
161 } else {
162 for (pos = 0; pos < len; pos++)
163 {
164 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
165 }
166 }
167 }
168
169 return len;
170 }
171
172 // mifare commands
173 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested)
174 {
175 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);
176 }
177
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)
179 {
180 // variables
181 int len;
182 uint32_t pos;
183 uint8_t tmp4[4];
184 uint8_t par[1] = {0x00};
185 byte_t nr[4];
186 uint32_t nt, ntpp; // Supplied tag nonce
187
188 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
189 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
190 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
191
192 // Transmit MIFARE_CLASSIC_AUTH
193 len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);
194 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len);
195 if (len != 4) return 1;
196
197 // "random" reader nonce:
198 nr[0] = 0x55;
199 nr[1] = 0x41;
200 nr[2] = 0x49;
201 nr[3] = 0x92;
202
203 // Save the tag nonce (nt)
204 nt = bytes_to_num(receivedAnswer, 4);
205
206 // ----------------------------- crypto1 create
207 if (isNested)
208 crypto1_destroy(pcs);
209
210 // Init cipher with key
211 crypto1_create(pcs, ui64Key);
212
213 if (isNested == AUTH_NESTED) {
214 // decrypt nt with help of new key
215 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
216 } else {
217 // Load (plain) uid^nt into the cipher
218 crypto1_word(pcs, nt ^ uid, 0);
219 }
220
221 // some statistic
222 if (!ntptr && (MF_DBGLEVEL >= 3))
223 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
224
225 // save Nt
226 if (ntptr)
227 *ntptr = nt;
228
229 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
230 par[0] = 0;
231 for (pos = 0; pos < 4; pos++)
232 {
233 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];
234 par[0] |= (((filter(pcs->odd) ^ oddparity(nr[pos])) & 0x01) << (7-pos));
235 }
236
237 // Skip 32 bits in pseudo random generator
238 nt = prng_successor(nt,32);
239
240 // ar+parity
241 for (pos = 4; pos < 8; pos++)
242 {
243 nt = prng_successor(nt,8);
244 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
245 par[0] |= (((filter(pcs->odd) ^ oddparity(nt & 0xff)) & 0x01) << (7-pos));
246 }
247
248 // Transmit reader nonce and reader answer
249 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
250
251 // Receive 4 byte tag answer
252 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
253 if (!len)
254 {
255 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
256 return 2;
257 }
258
259 memcpy(tmp4, receivedAnswer, 4);
260 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);
261
262 if (ntpp != bytes_to_num(tmp4, 4)) {
263 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
264 return 3;
265 }
266
267 return 0;
268 }
269
270 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
271 {
272 // variables
273 int len;
274 uint8_t bt[2];
275
276 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
277 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
278
279 // command MIFARE_CLASSIC_READBLOCK
280 len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
281 if (len == 1) {
282 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
283 return 1;
284 }
285 if (len != 18) {
286 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
287 return 2;
288 }
289
290 memcpy(bt, receivedAnswer + 16, 2);
291 AppendCrc14443a(receivedAnswer, 16);
292 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
293 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
294 return 3;
295 }
296
297 memcpy(blockData, receivedAnswer, 16);
298 return 0;
299 }
300
301 int mifare_ultra_auth1(uint32_t uid, uint8_t *blockData){
302 // variables
303 uint16_t len;
304
305 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
306 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
307
308 // command MIFARE_CLASSIC_READBLOCK
309 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);
310 if (len == 1) {
311 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
312 return 1;
313 }
314 if (len == 11) {
315 if (MF_DBGLEVEL >= 1) Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
316 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
317 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
318 receivedAnswer[10]);
319 memcpy(blockData, receivedAnswer, 11);
320 return 0;
321 }
322 //else something went wrong???
323 return 1;
324 }
325
326 int mifare_ultra_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
327 // variables
328 uint16_t len;
329
330 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
331 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
332
333 // command MIFARE_CLASSIC_READBLOCK
334 len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, key, receivedAnswer, receivedAnswerPar, NULL);
335 if (len == 1) {
336 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
337 return 1;
338 }
339 if (len == 11){
340 if (MF_DBGLEVEL >= 1) Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
341 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
342 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
343 receivedAnswer[10]);
344 memcpy(blockData, receivedAnswer, 11);
345 return 0;
346 }
347 //something went wrong?
348 return 1;
349 }
350
351 int mifare_ultra_readblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
352 {
353 // variables
354 uint16_t len;
355 uint8_t bt[2];
356
357 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
358 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
359
360
361 // command MIFARE_CLASSIC_READBLOCK
362 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
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
387 uint16_t len, i;
388 uint32_t pos;
389 uint8_t par[3] = {0}; // enough for 18 Bytes to send
390 byte_t res;
391
392 uint8_t d_block[18], d_block_enc[18];
393 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
394 uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
395
396 // command MIFARE_CLASSIC_WRITEBLOCK
397 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
398
399 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
400 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
401 return 1;
402 }
403
404 memcpy(d_block, blockData, 16);
405 AppendCrc14443a(d_block, 16);
406
407 // crypto
408 for (pos = 0; pos < 18; pos++)
409 {
410 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
411 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
412 }
413
414 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
415
416 // Receive the response
417 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
418
419 res = 0;
420 for (i = 0; i < 4; i++)
421 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
422
423 if ((len != 1) || (res != 0x0A)) {
424 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
425 return 2;
426 }
427
428 return 0;
429 }
430
431 int mifare_ultra_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
432 {
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();
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);
443
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 }
448
449 memset(d_block,'\0',18);
450 memcpy(d_block, blockData, 16);
451 AppendCrc14443a(d_block, 16);
452
453 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
454
455 // Receive the response
456 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
457
458 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
459 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
460 return 2;
461 }
462
463 return 0;
464 }
465
466 int mifare_ultra_special_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
467 {
468 uint16_t len;
469
470 uint8_t d_block[8];
471 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
472 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
473
474 // command MIFARE_CLASSIC_WRITEBLOCK
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
481 len = mifare_sendcmd_short_special(NULL, 1, 0xA2, d_block, receivedAnswer, receivedAnswerPar, NULL);
482
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;
488 }
489
490 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
491 {
492 uint16_t len;
493 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
494 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
495
496 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
497 if (len != 0) {
498 if (MF_DBGLEVEL >= 1) Dbprintf("halt error. response len: %x", len);
499 return 1;
500 }
501
502 return 0;
503 }
504
505 int mifare_ultra_halt(uint32_t uid)
506 {
507 uint16_t len;
508 uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
509 uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
510
511 len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
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
520
521 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
522 // plus evtl. 8 sectors with 16 blocks each (4k cards)
523 uint8_t NumBlocksPerSector(uint8_t sectorNo)
524 {
525 if (sectorNo < 32)
526 return 4;
527 else
528 return 16;
529 }
530
531 uint8_t FirstBlockOfSector(uint8_t sectorNo)
532 {
533 if (sectorNo < 32)
534 return sectorNo * 4;
535 else
536 return 32*4 + (sectorNo - 32) * 16;
537
538 }
539
540
541 // work with emulator memory
542 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
543 uint8_t* emCARD = get_bigbufptr_emlcardmem();
544 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
545 }
546
547 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
548 uint8_t* emCARD = get_bigbufptr_emlcardmem();
549 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
550 }
551
552 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
553 uint8_t* emCARD = get_bigbufptr_emlcardmem();
554 memcpy(data, emCARD + bytePtr, byteCount);
555 }
556
557 int emlCheckValBl(int blockNum) {
558 uint8_t* emCARD = get_bigbufptr_emlcardmem();
559 uint8_t* data = emCARD + blockNum * 16;
560
561 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
562 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
563 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
564 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
565 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
566 (data[12] != (data[15] ^ 0xff))
567 )
568 return 1;
569 return 0;
570 }
571
572 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
573 uint8_t* emCARD = get_bigbufptr_emlcardmem();
574 uint8_t* data = emCARD + blockNum * 16;
575
576 if (emlCheckValBl(blockNum)) {
577 return 1;
578 }
579
580 memcpy(blReg, data, 4);
581 *blBlock = data[12];
582 return 0;
583 }
584
585 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
586 uint8_t* emCARD = get_bigbufptr_emlcardmem();
587 uint8_t* data = emCARD + blockNum * 16;
588
589 memcpy(data + 0, &blReg, 4);
590 memcpy(data + 8, &blReg, 4);
591 blReg = blReg ^ 0xffffffff;
592 memcpy(data + 4, &blReg, 4);
593
594 data[12] = blBlock;
595 data[13] = blBlock ^ 0xff;
596 data[14] = blBlock;
597 data[15] = blBlock ^ 0xff;
598
599 return 0;
600 }
601
602 uint64_t emlGetKey(int sectorNum, int keyType) {
603 uint8_t key[6];
604 uint8_t* emCARD = get_bigbufptr_emlcardmem();
605
606 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
607 return bytes_to_num(key, 6);
608 }
609
610 void emlClearMem(void) {
611 int b;
612
613 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
614 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
615 uint8_t* emCARD = get_bigbufptr_emlcardmem();
616
617 memset(emCARD, 0, CARD_MEMORY_SIZE);
618
619 // fill sectors trailer data
620 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
621 emlSetMem((uint8_t *)trailer, b , 1);
622 }
623
624 // uid
625 emlSetMem((uint8_t *)uid, 0, 1);
626 return;
627 }
Impressum, Datenschutz