]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifarecmd.c
Merge remote-tracking branch 'upstream/master'
[proxmark3-svn] / armsrc / mifarecmd.c
1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
5 // Midnitesnake - Dec 2013
6 // Andy Davies - Apr 2014
7 // Iceman - May 2014
8 //
9 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
10 // at your option, any later version. See the LICENSE.txt file for the text of
11 // the license.
12 //-----------------------------------------------------------------------------
13 // Routines to support ISO 14443 type A.
14 //-----------------------------------------------------------------------------
15
16 #include "mifarecmd.h"
17 #include "apps.h"
18 #include "util.h"
19 #include "crc.h"
20
21 // the block number for the ISO14443-4 PCB
22 uint8_t pcb_blocknum = 0;
23 // Deselect card by sending a s-block. the crc is precalced for speed
24 static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};
25
26 //-----------------------------------------------------------------------------
27 // Select, Authenticate, Read a MIFARE tag.
28 // read block
29 //-----------------------------------------------------------------------------
30 void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
31 {
32 // params
33 uint8_t blockNo = arg0;
34 uint8_t keyType = arg1;
35 uint64_t ui64Key = 0;
36 ui64Key = bytes_to_num(datain, 6);
37
38 // variables
39 byte_t isOK = 0;
40 byte_t dataoutbuf[16];
41 uint8_t uid[10];
42 uint32_t cuid;
43 struct Crypto1State mpcs = {0, 0};
44 struct Crypto1State *pcs;
45 pcs = &mpcs;
46
47 // clear trace
48 clear_trace();
49 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
50
51 LED_A_ON();
52 LED_B_OFF();
53 LED_C_OFF();
54
55 while (true) {
56 if(!iso14443a_select_card(uid, NULL, &cuid)) {
57 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
58 break;
59 };
60
61 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
62 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
63 break;
64 };
65
66 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {
67 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");
68 break;
69 };
70
71 if(mifare_classic_halt(pcs, cuid)) {
72 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
73 break;
74 };
75
76 isOK = 1;
77 break;
78 }
79
80 // ----------------------------- crypto1 destroy
81 crypto1_destroy(pcs);
82
83 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");
84
85 LED_B_ON();
86 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);
87 LED_B_OFF();
88
89 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
90 LEDsoff();
91 }
92
93 void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){
94
95 bool turnOffField = (arg0 == 1);
96
97 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
98 clear_trace();
99 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
100
101 if(!iso14443a_select_card(NULL, NULL, NULL)) {
102 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
103 OnError(0);
104 return;
105 };
106
107 if(!mifare_ultra_auth(keybytes)){
108 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");
109 OnError(1);
110 return;
111 }
112
113 if (turnOffField) {
114 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
115 LEDsoff();
116 }
117 cmd_send(CMD_ACK,1,0,0,0,0);
118 }
119
120 // Arg0 = BlockNo,
121 // Arg1 = UsePwd bool
122 // datain = PWD bytes,
123 void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
124 {
125 uint8_t blockNo = arg0;
126 byte_t dataout[16] = {0x00};
127 bool useKey = (arg1 == 1); //UL_C
128 bool usePwd = (arg1 == 2); //UL_EV1/NTAG
129
130 LEDsoff();
131 LED_A_ON();
132 clear_trace();
133 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
134
135 int len = iso14443a_select_card(NULL, NULL, NULL);
136 if(!len) {
137 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);
138 OnError(1);
139 return;
140 }
141
142 // UL-C authentication
143 if ( useKey ) {
144 uint8_t key[16] = {0x00};
145 memcpy(key, datain, sizeof(key) );
146
147 if ( !mifare_ultra_auth(key) ) {
148 OnError(1);
149 return;
150 }
151 }
152
153 // UL-EV1 / NTAG authentication
154 if ( usePwd ) {
155 uint8_t pwd[4] = {0x00};
156 memcpy(pwd, datain, 4);
157 uint8_t pack[4] = {0,0,0,0};
158 if (!mifare_ul_ev1_auth(pwd, pack)) {
159 OnError(1);
160 return;
161 }
162 }
163
164 if( mifare_ultra_readblock(blockNo, dataout) ) {
165 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");
166 OnError(2);
167 return;
168 }
169
170 if( mifare_ultra_halt() ) {
171 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
172 OnError(3);
173 return;
174 }
175
176 cmd_send(CMD_ACK,1,0,0,dataout,16);
177 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
178 LEDsoff();
179 }
180
181 //-----------------------------------------------------------------------------
182 // Select, Authenticate, Read a MIFARE tag.
183 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
184 //-----------------------------------------------------------------------------
185 void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
186 {
187 // params
188 uint8_t sectorNo = arg0;
189 uint8_t keyType = arg1;
190 uint64_t ui64Key = 0;
191 ui64Key = bytes_to_num(datain, 6);
192
193 // variables
194 byte_t isOK = 0;
195 byte_t dataoutbuf[16 * 16];
196 uint8_t uid[10];
197 uint32_t cuid;
198 struct Crypto1State mpcs = {0, 0};
199 struct Crypto1State *pcs;
200 pcs = &mpcs;
201
202 // clear trace
203 clear_trace();
204
205 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
206
207 LED_A_ON();
208 LED_B_OFF();
209 LED_C_OFF();
210
211 isOK = 1;
212 if(!iso14443a_select_card(uid, NULL, &cuid)) {
213 isOK = 0;
214 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
215 }
216
217
218 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
219 isOK = 0;
220 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
221 }
222
223 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
224 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {
225 isOK = 0;
226 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);
227 break;
228 }
229 }
230
231 if(mifare_classic_halt(pcs, cuid)) {
232 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
233 }
234
235 // ----------------------------- crypto1 destroy
236 crypto1_destroy(pcs);
237
238 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");
239
240 LED_B_ON();
241 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));
242 LED_B_OFF();
243
244 // Thats it...
245 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
246 LEDsoff();
247 }
248
249 // arg0 = blockNo (start)
250 // arg1 = Pages (number of blocks)
251 // arg2 = useKey
252 // datain = KEY bytes
253 void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)
254 {
255 // free eventually allocated BigBuf memory
256 BigBuf_free();
257 // clear trace
258 clear_trace();
259
260 // params
261 uint8_t blockNo = arg0;
262 uint16_t blocks = arg1;
263 bool useKey = (arg2 == 1); //UL_C
264 bool usePwd = (arg2 == 2); //UL_EV1/NTAG
265 uint32_t countblocks = 0;
266 uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);
267 if (dataout == NULL){
268 Dbprintf("out of memory");
269 OnError(1);
270 return;
271 }
272
273 LEDsoff();
274 LED_A_ON();
275 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
276
277 int len = iso14443a_select_card(NULL, NULL, NULL);
278 if (!len) {
279 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);
280 OnError(1);
281 return;
282 }
283
284 // UL-C authentication
285 if ( useKey ) {
286 uint8_t key[16] = {0x00};
287 memcpy(key, datain, sizeof(key) );
288
289 if ( !mifare_ultra_auth(key) ) {
290 OnError(1);
291 return;
292 }
293 }
294
295 // UL-EV1 / NTAG authentication
296 if (usePwd) {
297 uint8_t pwd[4] = {0x00};
298 memcpy(pwd, datain, sizeof(pwd));
299 uint8_t pack[4] = {0,0,0,0};
300
301 if (!mifare_ul_ev1_auth(pwd, pack)){
302 OnError(1);
303 return;
304 }
305 }
306
307 for (int i = 0; i < blocks; i++){
308 if ((i*4) + 4 > CARD_MEMORY_SIZE) {
309 Dbprintf("Data exceeds buffer!!");
310 break;
311 }
312
313 len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);
314
315 if (len) {
316 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);
317 // if no blocks read - error out
318 if (i==0){
319 OnError(2);
320 return;
321 } else {
322 //stop at last successful read block and return what we got
323 break;
324 }
325 } else {
326 countblocks++;
327 }
328 }
329
330 len = mifare_ultra_halt();
331 if (len) {
332 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
333 OnError(3);
334 return;
335 }
336
337 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Blocks read %d", countblocks);
338
339 countblocks *= 4;
340 /*
341 LED_B_ON();
342 for(size_t i=0; i < countblocks; i += USB_CMD_DATA_SIZE) {
343 size_t len = MIN((countblocks - i),USB_CMD_DATA_SIZE);
344 cmd_send(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K,i,len,countblocks,dataout+i,len);
345 }
346 */
347 cmd_send(CMD_ACK, 1, countblocks, BigBuf_max_traceLen(), 0, 0);
348 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
349 LEDsoff();
350 }
351
352 //-----------------------------------------------------------------------------
353 // Select, Authenticate, Write a MIFARE tag.
354 // read block
355 //-----------------------------------------------------------------------------
356 void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
357 {
358 // params
359 uint8_t blockNo = arg0;
360 uint8_t keyType = arg1;
361 uint64_t ui64Key = 0;
362 byte_t blockdata[16];
363
364 ui64Key = bytes_to_num(datain, 6);
365 memcpy(blockdata, datain + 10, 16);
366
367 // variables
368 byte_t isOK = 0;
369 uint8_t uid[10];
370 uint32_t cuid;
371 struct Crypto1State mpcs = {0, 0};
372 struct Crypto1State *pcs;
373 pcs = &mpcs;
374
375 // clear trace
376 clear_trace();
377
378 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
379
380 LED_A_ON();
381 LED_B_OFF();
382 LED_C_OFF();
383
384 while (true) {
385 if(!iso14443a_select_card(uid, NULL, &cuid)) {
386 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
387 break;
388 };
389
390 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
391 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
392 break;
393 };
394
395 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {
396 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
397 break;
398 };
399
400 if(mifare_classic_halt(pcs, cuid)) {
401 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
402 break;
403 };
404
405 isOK = 1;
406 break;
407 }
408
409 // ----------------------------- crypto1 destroy
410 crypto1_destroy(pcs);
411
412 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
413
414 LED_B_ON();
415 cmd_send(CMD_ACK,isOK,0,0,0,0);
416 LED_B_OFF();
417
418
419 // Thats it...
420 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
421 LEDsoff();
422 }
423
424 void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)
425 {
426 uint8_t blockNo = arg0;
427 byte_t blockdata[16] = {0x00};
428
429 memcpy(blockdata, datain, 16);
430
431 uint8_t uid[10] = {0x00};
432
433 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
434
435 clear_trace();
436 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
437
438 if(!iso14443a_select_card(uid, NULL, NULL)) {
439 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
440 OnError(0);
441 return;
442 };
443
444 if(mifare_ultra_writeblock(blockNo, blockdata)) {
445 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
446 OnError(0);
447 return; };
448
449 if(mifare_ultra_halt()) {
450 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
451 OnError(0);
452 return;
453 };
454
455 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
456
457 cmd_send(CMD_ACK,1,0,0,0,0);
458 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
459 LEDsoff();
460 }
461
462 // Arg0 : Block to write to.
463 // Arg1 : 0 = use no authentication.
464 // 1 = use 0x1A authentication.
465 // 2 = use 0x1B authentication.
466 // datain : 4 first bytes is data to be written.
467 // : 4/16 next bytes is authentication key.
468 void MifareUWriteBlock_Special(uint8_t arg0, uint8_t arg1, uint8_t *datain)
469 {
470 uint8_t blockNo = arg0;
471 bool useKey = (arg1 == 1); //UL_C
472 bool usePwd = (arg1 == 2); //UL_EV1/NTAG
473 byte_t blockdata[4] = {0x00};
474
475 memcpy(blockdata, datain,4);
476
477 LEDsoff();
478 LED_A_ON();
479 clear_trace();
480 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
481
482 if(!iso14443a_select_card(NULL, NULL, NULL)) {
483 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
484 OnError(0);
485 return;
486 };
487
488 // UL-C authentication
489 if ( useKey ) {
490 uint8_t key[16] = {0x00};
491 memcpy(key, datain+4, sizeof(key) );
492
493 if ( !mifare_ultra_auth(key) ) {
494 OnError(1);
495 return;
496 }
497 }
498
499 // UL-EV1 / NTAG authentication
500 if (usePwd) {
501 uint8_t pwd[4] = {0x00};
502 memcpy(pwd, datain+4, 4);
503 uint8_t pack[4] = {0,0,0,0};
504 if (!mifare_ul_ev1_auth(pwd, pack)) {
505 OnError(1);
506 return;
507 }
508 }
509
510 if(mifare_ultra_special_writeblock(blockNo, blockdata)) {
511 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
512 OnError(0);
513 return;
514 };
515
516 if(mifare_ultra_halt()) {
517 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
518 OnError(0);
519 return;
520 };
521
522 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
523
524 cmd_send(CMD_ACK,1,0,0,0,0);
525 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
526 LEDsoff();
527 }
528
529 void MifareUSetPwd(uint8_t arg0, uint8_t *datain){
530
531 uint8_t pwd[16] = {0x00};
532 byte_t blockdata[4] = {0x00};
533
534 memcpy(pwd, datain, 16);
535
536 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
537 clear_trace();
538 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
539
540 if(!iso14443a_select_card(NULL, NULL, NULL)) {
541 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
542 OnError(0);
543 return;
544 };
545
546 blockdata[0] = pwd[7];
547 blockdata[1] = pwd[6];
548 blockdata[2] = pwd[5];
549 blockdata[3] = pwd[4];
550 if(mifare_ultra_special_writeblock( 44, blockdata)) {
551 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
552 OnError(44);
553 return;
554 };
555
556 blockdata[0] = pwd[3];
557 blockdata[1] = pwd[2];
558 blockdata[2] = pwd[1];
559 blockdata[3] = pwd[0];
560 if(mifare_ultra_special_writeblock( 45, blockdata)) {
561 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
562 OnError(45);
563 return;
564 };
565
566 blockdata[0] = pwd[15];
567 blockdata[1] = pwd[14];
568 blockdata[2] = pwd[13];
569 blockdata[3] = pwd[12];
570 if(mifare_ultra_special_writeblock( 46, blockdata)) {
571 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
572 OnError(46);
573 return;
574 };
575
576 blockdata[0] = pwd[11];
577 blockdata[1] = pwd[10];
578 blockdata[2] = pwd[9];
579 blockdata[3] = pwd[8];
580 if(mifare_ultra_special_writeblock( 47, blockdata)) {
581 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
582 OnError(47);
583 return;
584 };
585
586 if(mifare_ultra_halt()) {
587 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
588 OnError(0);
589 return;
590 };
591
592 cmd_send(CMD_ACK,1,0,0,0,0);
593 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
594 LEDsoff();
595 }
596
597 // Return 1 if the nonce is invalid else return 0
598 int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {
599 return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \
600 (oddparity((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \
601 (oddparity((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;
602 }
603
604
605 //-----------------------------------------------------------------------------
606 // MIFARE nested authentication.
607 //
608 //-----------------------------------------------------------------------------
609 void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)
610 {
611 // params
612 uint8_t blockNo = arg0 & 0xff;
613 uint8_t keyType = (arg0 >> 8) & 0xff;
614 uint8_t targetBlockNo = arg1 & 0xff;
615 uint8_t targetKeyType = (arg1 >> 8) & 0xff;
616 uint64_t ui64Key = 0;
617
618 ui64Key = bytes_to_num(datain, 6);
619
620 // variables
621 uint16_t rtr, i, j, len;
622 uint16_t davg;
623 static uint16_t dmin, dmax;
624 uint8_t uid[10];
625 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;
626 uint8_t par[1];
627 uint32_t target_nt[2], target_ks[2];
628
629 uint8_t par_array[4];
630 uint16_t ncount = 0;
631 struct Crypto1State mpcs = {0, 0};
632 struct Crypto1State *pcs;
633 pcs = &mpcs;
634 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
635
636 uint32_t auth1_time, auth2_time;
637 static uint16_t delta_time;
638
639 // free eventually allocated BigBuf memory
640 BigBuf_free();
641 // clear trace
642 clear_trace();
643 set_tracing(false);
644
645 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
646
647 LED_A_ON();
648 LED_C_OFF();
649
650
651 // statistics on nonce distance
652 if (calibrate) { // for first call only. Otherwise reuse previous calibration
653 LED_B_ON();
654 WDT_HIT();
655
656 davg = dmax = 0;
657 dmin = 2000;
658 delta_time = 0;
659
660 for (rtr = 0; rtr < 17; rtr++) {
661
662 // prepare next select. No need to power down the card.
663 if(mifare_classic_halt(pcs, cuid)) {
664 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
665 rtr--;
666 continue;
667 }
668
669 if(!iso14443a_select_card(uid, NULL, &cuid)) {
670 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
671 rtr--;
672 continue;
673 };
674
675 auth1_time = 0;
676 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
677 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
678 rtr--;
679 continue;
680 };
681
682 if (delta_time) {
683 auth2_time = auth1_time + delta_time;
684 } else {
685 auth2_time = 0;
686 }
687 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {
688 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");
689 rtr--;
690 continue;
691 };
692
693 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
694 for (i = 101; i < 1200; i++) {
695 nttmp = prng_successor(nttmp, 1);
696 if (nttmp == nt2) break;
697 }
698
699 if (i != 1200) {
700 if (rtr != 0) {
701 davg += i;
702 dmin = MIN(dmin, i);
703 dmax = MAX(dmax, i);
704 }
705 else {
706 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing
707 }
708 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);
709 }
710 }
711
712 if (rtr <= 1) return;
713
714 davg = (davg + (rtr - 1)/2) / (rtr - 1);
715
716 if (MF_DBGLEVEL >= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin, dmax, davg, delta_time);
717
718 dmin = davg - 2;
719 dmax = davg + 2;
720
721 LED_B_OFF();
722
723 }
724 // -------------------------------------------------------------------------------------------------
725
726 LED_C_ON();
727
728 // get crypted nonces for target sector
729 for(i=0; i < 2; i++) { // look for exactly two different nonces
730
731 target_nt[i] = 0;
732 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce
733
734 // prepare next select. No need to power down the card.
735 if(mifare_classic_halt(pcs, cuid)) {
736 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
737 continue;
738 }
739
740 if(!iso14443a_select_card(uid, NULL, &cuid)) {
741 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
742 continue;
743 };
744
745 auth1_time = 0;
746 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
747 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
748 continue;
749 };
750
751 // nested authentication
752 auth2_time = auth1_time + delta_time;
753 len = mifare_sendcmd_shortex(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);
754 if (len != 4) {
755 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);
756 continue;
757 };
758
759 nt2 = bytes_to_num(receivedAnswer, 4);
760 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);
761
762 // Parity validity check
763 for (j = 0; j < 4; j++) {
764 par_array[j] = (oddparity(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));
765 }
766
767 ncount = 0;
768 nttest = prng_successor(nt1, dmin - 1);
769 for (j = dmin; j < dmax + 1; j++) {
770 nttest = prng_successor(nttest, 1);
771 ks1 = nt2 ^ nttest;
772
773 if (valid_nonce(nttest, nt2, ks1, par_array)){
774 if (ncount > 0) { // we are only interested in disambiguous nonces, try again
775 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);
776 target_nt[i] = 0;
777 break;
778 }
779 target_nt[i] = nttest;
780 target_ks[i] = ks1;
781 ncount++;
782 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces
783 target_nt[i] = 0;
784 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);
785 break;
786 }
787 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);
788 }
789 }
790 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);
791 }
792 }
793
794 LED_C_OFF();
795
796 // ----------------------------- crypto1 destroy
797 crypto1_destroy(pcs);
798
799 byte_t buf[4 + 4 * 4];
800 memcpy(buf, &cuid, 4);
801 memcpy(buf+4, &target_nt[0], 4);
802 memcpy(buf+8, &target_ks[0], 4);
803 memcpy(buf+12, &target_nt[1], 4);
804 memcpy(buf+16, &target_ks[1], 4);
805
806 LED_B_ON();
807 cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));
808 LED_B_OFF();
809
810 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");
811
812 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
813 LEDsoff();
814 set_tracing(TRUE);
815 }
816
817 //-----------------------------------------------------------------------------
818 // MIFARE check keys. key count up to 85.
819 //
820 //-----------------------------------------------------------------------------
821 void MifareChkKeys(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
822 {
823 // params
824 uint8_t blockNo = arg0;
825 uint8_t keyType = arg1;
826 uint8_t keyCount = arg2;
827 uint64_t ui64Key = 0;
828
829 // variables
830 int i;
831 byte_t isOK = 0;
832 uint8_t uid[10];
833 uint32_t cuid;
834 struct Crypto1State mpcs = {0, 0};
835 struct Crypto1State *pcs;
836 pcs = &mpcs;
837
838 // clear debug level
839 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;
840 MF_DBGLEVEL = MF_DBG_NONE;
841
842 // clear trace
843 clear_trace();
844 set_tracing(TRUE);
845
846 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
847
848 LED_A_ON();
849 LED_B_OFF();
850 LED_C_OFF();
851
852 for (i = 0; i < keyCount; i++) {
853 if(mifare_classic_halt(pcs, cuid)) {
854 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");
855 }
856
857 if(!iso14443a_select_card(uid, NULL, &cuid)) {
858 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");
859 break;
860 };
861
862 ui64Key = bytes_to_num(datain + i * 6, 6);
863 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
864 continue;
865 };
866
867 isOK = 1;
868 break;
869 }
870
871 // ----------------------------- crypto1 destroy
872 crypto1_destroy(pcs);
873
874 LED_B_ON();
875 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);
876 LED_B_OFF();
877
878 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
879 LEDsoff();
880
881 // restore debug level
882 MF_DBGLEVEL = OLD_MF_DBGLEVEL;
883 }
884
885 //-----------------------------------------------------------------------------
886 // MIFARE commands set debug level
887 //
888 //-----------------------------------------------------------------------------
889 void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
890 MF_DBGLEVEL = arg0;
891 Dbprintf("Debug level: %d", MF_DBGLEVEL);
892 }
893
894 //-----------------------------------------------------------------------------
895 // Work with emulator memory
896 //
897 //-----------------------------------------------------------------------------
898 void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
899 emlClearMem();
900 }
901
902 void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
903 emlSetMem(datain, arg0, arg1); // data, block num, blocks count
904 }
905
906 void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
907 byte_t buf[USB_CMD_DATA_SIZE];
908 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)
909
910 LED_B_ON();
911 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);
912 LED_B_OFF();
913 }
914
915 //-----------------------------------------------------------------------------
916 // Load a card into the emulator memory
917 //
918 //-----------------------------------------------------------------------------
919 void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
920 uint8_t numSectors = arg0;
921 uint8_t keyType = arg1;
922 uint64_t ui64Key = 0;
923 uint32_t cuid;
924 struct Crypto1State mpcs = {0, 0};
925 struct Crypto1State *pcs;
926 pcs = &mpcs;
927
928 // variables
929 byte_t dataoutbuf[16];
930 byte_t dataoutbuf2[16];
931 uint8_t uid[10];
932
933 // clear trace
934 clear_trace();
935 set_tracing(false);
936
937 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
938
939 LED_A_ON();
940 LED_B_OFF();
941 LED_C_OFF();
942
943 bool isOK = true;
944
945 if(!iso14443a_select_card(uid, NULL, &cuid)) {
946 isOK = false;
947 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
948 }
949
950 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {
951 ui64Key = emlGetKey(sectorNo, keyType);
952 if (sectorNo == 0){
953 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
954 isOK = false;
955 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);
956 break;
957 }
958 } else {
959 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {
960 isOK = false;
961 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);
962 break;
963 }
964 }
965
966 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
967 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {
968 isOK = false;
969 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);
970 break;
971 };
972 if (isOK) {
973 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {
974 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);
975 } else { // sector trailer, keep the keys, set only the AC
976 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
977 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);
978 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
979 }
980 }
981 }
982
983 }
984
985 if(mifare_classic_halt(pcs, cuid)) {
986 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
987 };
988
989 // ----------------------------- crypto1 destroy
990 crypto1_destroy(pcs);
991
992 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
993 LEDsoff();
994
995 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");
996
997 }
998
999
1000 //-----------------------------------------------------------------------------
1001 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1002 //
1003 //-----------------------------------------------------------------------------
1004 void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1005
1006 // params
1007 uint8_t needWipe = arg0;
1008 // bit 0 - need get UID
1009 // bit 1 - need wupC
1010 // bit 2 - need HALT after sequence
1011 // bit 3 - need init FPGA and field before sequence
1012 // bit 4 - need reset FPGA and LED
1013 uint8_t workFlags = arg1;
1014 uint8_t blockNo = arg2;
1015
1016 // card commands
1017 uint8_t wupC1[] = { 0x40 };
1018 uint8_t wupC2[] = { 0x43 };
1019 uint8_t wipeC[] = { 0x41 };
1020
1021 // variables
1022 byte_t isOK = 0;
1023 uint8_t uid[10] = {0x00};
1024 uint8_t d_block[18] = {0x00};
1025 uint32_t cuid;
1026
1027 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1028 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1029
1030 // reset FPGA and LED
1031 if (workFlags & 0x08) {
1032 LED_A_ON();
1033 LED_B_OFF();
1034 LED_C_OFF();
1035
1036 clear_trace();
1037 set_tracing(TRUE);
1038 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1039 }
1040
1041 while (true) {
1042
1043 // get UID from chip
1044 if (workFlags & 0x01) {
1045 if(!iso14443a_select_card(uid, NULL, &cuid)) {
1046 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1047 break;
1048 };
1049
1050 if(mifare_classic_halt(NULL, cuid)) {
1051 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1052 break;
1053 };
1054 };
1055
1056 // reset chip
1057 if (needWipe){
1058 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1059 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1060 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1061 break;
1062 };
1063
1064 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
1065 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1066 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
1067 break;
1068 };
1069
1070 if(mifare_classic_halt(NULL, cuid)) {
1071 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1072 break;
1073 };
1074 };
1075
1076 // write block
1077 if (workFlags & 0x02) {
1078 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1079 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1080 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1081 break;
1082 };
1083
1084 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1085 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1086 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1087 break;
1088 };
1089 }
1090
1091 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
1092 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
1093 break;
1094 };
1095
1096 memcpy(d_block, datain, 16);
1097 AppendCrc14443a(d_block, 16);
1098
1099 ReaderTransmit(d_block, sizeof(d_block), NULL);
1100 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {
1101 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
1102 break;
1103 };
1104
1105 if (workFlags & 0x04) {
1106 if (mifare_classic_halt(NULL, cuid)) {
1107 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1108 break;
1109 };
1110 }
1111
1112 isOK = 1;
1113 break;
1114 }
1115
1116 LED_B_ON();
1117 cmd_send(CMD_ACK,isOK,0,0,uid,4);
1118 LED_B_OFF();
1119
1120 if ((workFlags & 0x10) || (!isOK)) {
1121 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1122 LEDsoff();
1123 }
1124 }
1125
1126
1127 void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1128
1129 // params
1130 // bit 1 - need wupC
1131 // bit 2 - need HALT after sequence
1132 // bit 3 - need init FPGA and field before sequence
1133 // bit 4 - need reset FPGA and LED
1134 uint8_t workFlags = arg0;
1135 uint8_t blockNo = arg2;
1136
1137 // card commands
1138 uint8_t wupC1[] = { 0x40 };
1139 uint8_t wupC2[] = { 0x43 };
1140
1141 // variables
1142 byte_t isOK = 0;
1143 uint8_t data[18] = {0x00};
1144 uint32_t cuid = 0;
1145
1146 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1147 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1148
1149 if (workFlags & 0x08) {
1150 LED_A_ON();
1151 LED_B_OFF();
1152 LED_C_OFF();
1153
1154 clear_trace();
1155 set_tracing(TRUE);
1156 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1157 }
1158
1159 while (true) {
1160 if (workFlags & 0x02) {
1161 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1162 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1163 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1164 break;
1165 };
1166
1167 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1168 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1169 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1170 break;
1171 };
1172 }
1173
1174 // read block
1175 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {
1176 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");
1177 break;
1178 };
1179 memcpy(data, receivedAnswer, 18);
1180
1181 if (workFlags & 0x04) {
1182 if (mifare_classic_halt(NULL, cuid)) {
1183 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1184 break;
1185 };
1186 }
1187
1188 isOK = 1;
1189 break;
1190 }
1191
1192 LED_B_ON();
1193 cmd_send(CMD_ACK,isOK,0,0,data,18);
1194 LED_B_OFF();
1195
1196 if ((workFlags & 0x10) || (!isOK)) {
1197 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1198 LEDsoff();
1199 }
1200 }
1201
1202 void MifareCIdent(){
1203
1204 // card commands
1205 uint8_t wupC1[] = { 0x40 };
1206 uint8_t wupC2[] = { 0x43 };
1207
1208 // variables
1209 byte_t isOK = 1;
1210
1211 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1212 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1213
1214 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1215 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1216 isOK = 0;
1217 };
1218
1219 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1220 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1221 isOK = 0;
1222 };
1223
1224 if (mifare_classic_halt(NULL, 0)) {
1225 isOK = 0;
1226 };
1227
1228 cmd_send(CMD_ACK,isOK,0,0,0,0);
1229 }
1230
1231 //
1232 // DESFIRE
1233 //
1234
1235 void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){
1236
1237 byte_t dataout[11] = {0x00};
1238 uint8_t uid[10] = {0x00};
1239 uint32_t cuid;
1240
1241 clear_trace();
1242 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1243
1244 int len = iso14443a_select_card(uid, NULL, &cuid);
1245 if(!len) {
1246 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
1247 OnError(1);
1248 return;
1249 };
1250
1251 if(mifare_desfire_des_auth1(cuid, dataout)){
1252 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");
1253 OnError(4);
1254 return;
1255 }
1256
1257 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");
1258 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));
1259 }
1260
1261 void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){
1262
1263 uint32_t cuid = arg0;
1264 uint8_t key[16] = {0x00};
1265 byte_t isOK = 0;
1266 byte_t dataout[12] = {0x00};
1267
1268 memcpy(key, datain, 16);
1269
1270 isOK = mifare_desfire_des_auth2(cuid, key, dataout);
1271
1272 if( isOK) {
1273 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
1274 Dbprintf("Authentication part2: Failed");
1275 //OnError(4);
1276 return;
1277 }
1278
1279 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
1280 DbpString("AUTH 2 FINISHED");
1281
1282 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));
1283 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1284 LEDsoff();
1285 }
1286
1287 void OnSuccess(){
1288 pcb_blocknum = 0;
1289 ReaderTransmit(deselect_cmd, 3 , NULL);
1290 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1291 LEDsoff();
1292 }
1293
1294 void OnError(uint8_t reason){
1295 pcb_blocknum = 0;
1296 ReaderTransmit(deselect_cmd, 3 , NULL);
1297 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1298 cmd_send(CMD_ACK,0,reason,0,0,0);
1299 LEDsoff();
1300 }
Impressum, Datenschutz