]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfmfu.c
Update lfops.c
[proxmark3-svn] / client / cmdhfmfu.c
CommitLineData
81740aa5 1//-----------------------------------------------------------------------------
2// Ultralight Code (c) 2013,2014 Midnitesnake & Andy Davies of Pentura
3//
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,
5// at your option, any later version. See the LICENSE.txt file for the text of
6// the license.
7//-----------------------------------------------------------------------------
8// High frequency MIFARE ULTRALIGHT (C) commands
9//-----------------------------------------------------------------------------
7cb8516c 10
acf0582d 11#include "cmdhfmfu.h"
12
13#include <stdint.h>
14#include <stdio.h>
ad939de5 15#include "comms.h"
7cb8516c 16#include "usb_cmd.h"
17#include "cmdmain.h"
18#include "ui.h"
700d8687 19#include "mbedtls/des.h"
81740aa5 20#include "cmdhfmf.h"
4be9f36e 21#include "cmdhf14a.h" // DropField()
f168b263 22#include "mifare.h"
f9848fd6 23#include "util.h"
b8dd1ef6 24#include "util_posix.h"
75377d29 25#include "protocols.h"
1338d245 26#include "taginfo.h"
81740aa5 27
4be9f36e 28typedef enum TAGTYPE_UL {
29 UNKNOWN = 0x000000,
30 UL = 0x000001,
31 UL_C = 0x000002,
32 UL_EV1_48 = 0x000004,
33 UL_EV1_128 = 0x000008,
34 NTAG = 0x000010,
35 NTAG_203 = 0x000020,
36 NTAG_210 = 0x000040,
37 NTAG_212 = 0x000080,
38 NTAG_213 = 0x000100,
39 NTAG_215 = 0x000200,
40 NTAG_216 = 0x000400,
41 MY_D = 0x000800,
42 MY_D_NFC = 0x001000,
43 MY_D_MOVE = 0x002000,
b8dd1ef6 44 MY_D_MOVE_LEAN= 0x004000,
45 NTAG_I2C_1K = 0x008000,
46 NTAG_I2C_2K = 0x010000,
47 FUDAN_UL = 0x020000,
48 MAGIC = 0x040000,
4be9f36e 49 UL_MAGIC = UL | MAGIC,
50 UL_C_MAGIC = UL_C | MAGIC,
51 UL_ERROR = 0xFFFFFF,
52} TagTypeUL_t;
53
4a74e2be 54#define MAX_UL_BLOCKS 0x0f
55#define MAX_ULC_BLOCKS 0x2b
56#define MAX_ULEV1a_BLOCKS 0x13
57#define MAX_ULEV1b_BLOCKS 0x28
58#define MAX_NTAG_203 0x29
59#define MAX_NTAG_210 0x13
60#define MAX_NTAG_212 0x28
61#define MAX_NTAG_213 0x2c
62#define MAX_NTAG_215 0x86
63#define MAX_NTAG_216 0xe6
64#define MAX_MY_D_NFC 0xff
65#define MAX_MY_D_MOVE 0x25
66#define MAX_MY_D_MOVE_LEAN 0x0f
75377d29 67
4be9f36e 68#define PUBLIC_ECDA_KEYLEN 33
69static uint8_t public_ecda_key[PUBLIC_ECDA_KEYLEN] = {
70 0x04, 0x49, 0x4e, 0x1a, 0x38, 0x6d, 0x3d, 0x3c,
71 0xfe, 0x3d, 0xc1, 0x0e, 0x5d, 0xe6, 0x8a, 0x49,
72 0x9b, 0x1c, 0x20, 0x2d, 0xb5, 0xb1, 0x32, 0x39,
73 0x3e, 0x89, 0xed, 0x19, 0xfe, 0x5b, 0xe8, 0xbc,
74 0x61
75};
76
345fb24a 77#define KEYS_3DES_COUNT 7
4be9f36e 78static uint8_t default_3des_keys[KEYS_3DES_COUNT][16] = {
79 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
80 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
81 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
82 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
83 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
84 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
85 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
75377d29 86};
87
7f2114d8 88#define KEYS_PWD_COUNT 6
4be9f36e 89static uint8_t default_pwd_pack[KEYS_PWD_COUNT][4] = {
c585a5cf 90 {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default
91 {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed)
345fb24a 92 {0x33,0x6B,0xA1,0x19}, // PACK 0x9c,0x2d -- italian bus (sniffed)
4be9f36e 93 {0xFF,0x90,0x6C,0xB2}, // PACK 0x12,0x9e -- italian bus (sniffed)
8258f409 94 {0x46,0x1c,0xA3,0x19}, // PACK 0xE9,0x5A -- italian bus (sniffed)
95 {0x35,0x1C,0xD0,0x19}, // PACK 0x9A,0x5a -- italian bus (sniffed)
c585a5cf 96};
97
b8dd1ef6 98#define MAX_UL_TYPES 17
4be9f36e 99static uint32_t UL_TYPES_ARRAY[MAX_UL_TYPES] = {UNKNOWN, UL, UL_C, UL_EV1_48, UL_EV1_128, NTAG, NTAG_203,
b8dd1ef6 100 NTAG_210, NTAG_212, NTAG_213, NTAG_215, NTAG_216, MY_D, MY_D_NFC, MY_D_MOVE, MY_D_MOVE_LEAN, FUDAN_UL};
5b99376a 101
4be9f36e 102static uint8_t UL_MEMORY_ARRAY[MAX_UL_TYPES] = {MAX_UL_BLOCKS, MAX_UL_BLOCKS, MAX_ULC_BLOCKS, MAX_ULEV1a_BLOCKS,
103 MAX_ULEV1b_BLOCKS, MAX_NTAG_203, MAX_NTAG_203, MAX_NTAG_210, MAX_NTAG_212, MAX_NTAG_213,
b8dd1ef6 104 MAX_NTAG_215, MAX_NTAG_216, MAX_UL_BLOCKS, MAX_MY_D_NFC, MAX_MY_D_MOVE, MAX_MY_D_MOVE_LEAN, MAX_UL_BLOCKS};
afceaf40 105
4be9f36e 106// get version nxp product type
107static char *getProductTypeStr( uint8_t id){
75377d29 108
109 static char buf[20];
110 char *retStr = buf;
111
112 switch(id) {
1c429594 113 case 3: sprintf(retStr, "%02X, Ultralight", id); break;
4be9f36e 114 case 4: sprintf(retStr, "%02X, NTAG", id); break;
1c429594 115 default: sprintf(retStr, "%02X, unknown", id); break;
75377d29 116 }
117 return buf;
118}
119
120/*
4be9f36e 121 The 7 MSBits (=n) code the storage size itself based on 2^n,
75377d29 122 the LSBit is set to '0' if the size is exactly 2^n
4be9f36e 123 and set to '1' if the storage size is between 2^n and 2^(n+1).
75377d29 124*/
b8dd1ef6 125static char *getUlev1CardSizeStr( uint8_t fsize ){
75377d29 126
345fb24a 127 static char buf[40];
75377d29 128 char *retStr = buf;
2be768af 129 memset(buf, 0, sizeof(buf));
75377d29 130
2be768af 131 uint16_t usize = 1 << ((fsize >>1) + 1);
132 uint16_t lsize = 1 << (fsize >>1);
75377d29 133
134 // is LSB set?
135 if ( fsize & 1 )
06561c34 136 sprintf(retStr, "%02X, (%u <-> %u bytes)",fsize, usize, lsize);
4be9f36e 137 else
138 sprintf(retStr, "%02X, (%u bytes)", fsize, lsize);
75377d29 139 return buf;
140}
81740aa5 141
81740aa5 142
b8dd1ef6 143static int ul_send_cmd_raw(uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength) {
75377d29 144 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_APPEND_CRC, cmdlen, 0}};
145 memcpy(c.d.asBytes, cmd, cmdlen);
22342f6d 146 clearCommandBuffer();
75377d29 147 SendCommand(&c);
148 UsbCommand resp;
149 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
372a8257 150 if (!resp.arg[0] && responseLength) return -1;
75377d29 151
152 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
abab60ae 153 memcpy(response, resp.d.asBytes, resplen);
154 return resplen;
75377d29 155}
75377d29 156
75377d29 157
b8dd1ef6 158static int ul_select(iso14a_card_select_t *card, bool clear_trace) {
75377d29 159
b8dd1ef6 160 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT | ISO14A_NO_RATS | (clear_trace?ISO14A_CLEAR_TRACE:0), 0, 0}};
161 clearCommandBuffer();
162 SendCommand(&c);
75377d29 163
164 UsbCommand resp;
c7442b76 165 bool ans = false;
166 ans = WaitForResponseTimeout(CMD_ACK, &resp, 1500);
b8dd1ef6 167 if (ans == 0 || resp.arg[0] == 0) {
4be9f36e 168 PrintAndLogEx(WARNING, "iso14443a card select failed");
c7442b76 169 return 0;
170 }
75377d29 171
172 memcpy(card, resp.d.asBytes, sizeof(iso14a_card_select_t));
c7442b76 173 return 1;
75377d29 174}
175
75377d29 176
b8dd1ef6 177// This read command will return 16 bytes.
178static int ul_read(uint8_t page, uint8_t *response, uint16_t responseLength) {
179 uint8_t cmd[] = {MIFARE_CMD_READBLOCK, page};
75377d29 180 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
75377d29 181 return len;
182}
183
c585a5cf 184
b8dd1ef6 185static int ul_halt(void) {
186 uint8_t cmd[] = {ISO14443A_CMD_HALT, 0x00};
187 uint8_t response;
188 int len = ul_send_cmd_raw(cmd, sizeof(cmd), &response, sizeof(response));
189 return len;
190}
191
192
193static int ul_comp_write_ex(uint8_t page, uint8_t *data, uint8_t datalen, bool first_part_only) {
194
195 uint8_t cmd[18] = {0x00};
c585a5cf 196 datalen = ( datalen > 16) ? 16 : datalen;
197
b8dd1ef6 198 cmd[0] = MIFARE_CMD_WRITEBLOCK;
c585a5cf 199 cmd[1] = page;
b8dd1ef6 200
201 uint8_t response = {0xff};
202 ul_send_cmd_raw(cmd, 2, &response, sizeof(response));
203 if (response != CARD_ACK)
204 return -1;
205 if (first_part_only)
206 return 0;
207
208 memcpy(cmd, data, datalen);
209 ul_send_cmd_raw(cmd, 16, &response, sizeof(response));
210 if (response != CARD_ACK)
211 return -1;
212
213 return 0;
c585a5cf 214}
345fb24a 215
b8dd1ef6 216
217// not used yet
218// static int ul_comp_write(uint8_t page, uint8_t *data, uint8_t datalen) {
219 // return ul_comp_write_ex(page, data, datalen, false);
220// }
221
222
223static int ulc_requestAuthentication(uint8_t *nonce, uint16_t nonceLength) {
75377d29 224
a2e2bb8a 225 uint8_t cmd[] = {MIFARE_ULC_AUTH_1, 0x00};
75377d29 226 int len = ul_send_cmd_raw(cmd, sizeof(cmd), nonce, nonceLength);
75377d29 227 return len;
228}
345fb24a 229
b8dd1ef6 230
231static int ulc_authentication(uint8_t *key, bool switch_off_field) {
8258f409 232
233 UsbCommand c = {CMD_MIFAREUC_AUTH, {switch_off_field}};
234 memcpy(c.d.asBytes, key, 16);
22342f6d 235 clearCommandBuffer();
8258f409 236 SendCommand(&c);
237 UsbCommand resp;
9d87eb66 238 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return 0;
239 if ( resp.arg[0] == 1 ) return 1;
8258f409 240
9d87eb66 241 return 0;
8258f409 242}
243
b8dd1ef6 244
245static int ulev1_requestAuthentication(uint8_t *pwd, uint8_t *pack, uint16_t packLength) {
75377d29 246
247 uint8_t cmd[] = {MIFARE_ULEV1_AUTH, pwd[0], pwd[1], pwd[2], pwd[3]};
248 int len = ul_send_cmd_raw(cmd, sizeof(cmd), pack, packLength);
75377d29 249 return len;
250}
251
b8dd1ef6 252
253static int ul_auth_select(iso14a_card_select_t *card, TagTypeUL_t tagtype, bool hasAuthKey, uint8_t *authenticationkey, uint8_t *pack, uint8_t packSize) {
254
255 if (hasAuthKey && (tagtype & UL_C)) {
1c429594 256 //will select card automatically and close connection on error
257 if (!ulc_authentication(authenticationkey, false)) {
b8dd1ef6 258 PrintAndLogEx(ERR, "Authentication Failed UL-C");
1c429594 259 return 0;
260 }
261 } else {
b8dd1ef6 262 if (!ul_select(card, false)) return 0;
1c429594 263
264 if (hasAuthKey) {
265 if (ulev1_requestAuthentication(authenticationkey, pack, packSize) < 1) {
b8dd1ef6 266 PrintAndLogEx(ERR, "Authentication Failed UL-EV1/NTAG");
1c429594 267 return 0;
268 }
269 }
270 }
271 return 1;
272}
273
b8dd1ef6 274static int ulev1_getVersion(uint8_t *response, uint16_t responseLength) {
75377d29 275
4be9f36e 276 uint8_t cmd[] = {MIFARE_ULEV1_VERSION};
75377d29 277 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
75377d29 278 return len;
279}
280
281// static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
4be9f36e 282
75377d29 283 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
4be9f36e 284
75377d29 285 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
75377d29 286 // return -1;
287 // }
288 // return 0;
289// }
290
b8dd1ef6 291
292static int ulev1_readCounter(uint8_t counter, uint8_t *response, uint16_t responseLength) {
75377d29 293
294 uint8_t cmd[] = {MIFARE_ULEV1_READ_CNT, counter};
295 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
75377d29 296 return len;
297}
298
b8dd1ef6 299
300static int ulev1_readTearing(uint8_t counter, uint8_t *response, uint16_t responseLength) {
a2e2bb8a 301
302 uint8_t cmd[] = {MIFARE_ULEV1_CHECKTEAR, counter};
303 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
a2e2bb8a 304 return len;
305}
306
b8dd1ef6 307
308static int ulev1_readSignature(uint8_t *response, uint16_t responseLength) {
c585a5cf 309
310 uint8_t cmd[] = {MIFARE_ULEV1_READSIG, 0x00};
311 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
c585a5cf 312 return len;
313}
314
7c8b5e68 315
316// Fudan check checks for which error is given for a command with incorrect crc
317// NXP UL chip responds with 01, fudan 00.
318// other possible checks:
4be9f36e 319// send a0 + crc
7c8b5e68 320// UL responds with 00, fudan doesn't respond
321// or
322// send a200 + crc
323// UL doesn't respond, fudan responds with 00
324// or
325// send 300000 + crc (read with extra byte(s))
326// UL responds with read of page 0, fudan doesn't respond.
327//
328// make sure field is off before calling this function
b8dd1ef6 329static int ul_fudan_check(void) {
f4217d58 330 iso14a_card_select_t card;
b8dd1ef6 331 if (!ul_select(&card, false))
f4217d58 332 return UL_ERROR;
333
334 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT, 4, 0}};
335
336 uint8_t cmd[4] = {0x30,0x00,0x02,0xa7}; //wrong crc on purpose should be 0xa8
337 memcpy(c.d.asBytes, cmd, 4);
338 clearCommandBuffer();
339 SendCommand(&c);
340 UsbCommand resp;
341 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return UL_ERROR;
342 if (resp.arg[0] != 1) return UL_ERROR;
343
344 return (!resp.d.asBytes[0]) ? FUDAN_UL : UL; //if response == 0x00 then Fudan, else Genuine NXP
345}
346
b8dd1ef6 347
348static int ul_print_default(uint8_t *data) {
75377d29 349
350 uint8_t uid[7];
b8dd1ef6 351 memcpy(uid, data, 3);
352 memcpy(uid+3, data+4, 4);
353
354 PrintAndLogEx(NORMAL," UID : %s", sprint_hex(uid, 7));
4be9f36e 355 PrintAndLogEx(NORMAL," UID[0] : %02X, %s", uid[0], getManufacturerName(uid[0]));
b8dd1ef6 356
357 if (uid[0] == 0x05 && ((uid[1] & 0xf0) >> 4) == 2 ) { // is infineon and 66RxxP
345fb24a 358 uint8_t chip = (data[8] & 0xC7); // 11000111 mask, bit 3,4,5 RFU
359 switch (chip){
4be9f36e 360 case 0xc2: PrintAndLogEx(NORMAL, " IC type : SLE 66R04P 770 Bytes"); break; //77 pages
361 case 0xc4: PrintAndLogEx(NORMAL, " IC type : SLE 66R16P 2560 Bytes"); break; //256 pages
362 case 0xc6: PrintAndLogEx(NORMAL, " IC type : SLE 66R32P 5120 Bytes"); break; //512 pages /2 sectors
345fb24a 363 }
364 }
4be9f36e 365 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
b8dd1ef6 366 int crc0 = 0x88 ^ uid[0] ^ uid[1] ^ uid[2];
75377d29 367 if ( data[3] == crc0 )
4be9f36e 368 PrintAndLogEx(NORMAL, " BCC0 : %02X, Ok", data[3]);
75377d29 369 else
4be9f36e 370 PrintAndLogEx(NORMAL, " BCC0 : %02X, crc should be %02X", data[3], crc0);
75377d29 371
b8dd1ef6 372 int crc1 = uid[3] ^ uid[4] ^ uid[5] ^ uid[6];
75377d29 373 if ( data[8] == crc1 )
4be9f36e 374 PrintAndLogEx(NORMAL, " BCC1 : %02X, Ok", data[8]);
75377d29 375 else
4be9f36e 376 PrintAndLogEx(NORMAL, " BCC1 : %02X, crc should be %02X", data[8], crc1 );
75377d29 377
4be9f36e 378 PrintAndLogEx(NORMAL, " Internal : %02X, %sdefault", data[9], (data[9]==0x48)?"":"not " );
29250969 379
b8dd1ef6 380 PrintAndLogEx(NORMAL, " Lock : %s (binary %s %s)",
6fdf42c6 381 sprint_hex(data+10, 2),
4be9f36e 382 printBits(1, data+10),
383 printBits(1, data+11)
6fdf42c6 384 );
29250969 385
4be9f36e 386 PrintAndLogEx(NORMAL, "OneTimePad : %s (binary %s %s %s %s)\n",
b8dd1ef6 387 sprint_hex(data+12, 4),
4be9f36e 388 printBits(1, data+12),
389 printBits(1, data+13),
390 printBits(1, data+14),
391 printBits(1, data+15)
6fdf42c6 392 );
29250969 393
75377d29 394 return 0;
395}
396
b8dd1ef6 397
a383f4b7 398static int ndef_print_CC(uint8_t *data) {
1c429594 399 // no NDEF message
400 if(data[0] != 0xe1)
401 return -1;
75377d29 402
4be9f36e 403 PrintAndLogEx(NORMAL, "--- NDEF Message");
404 PrintAndLogEx(NORMAL, "Capability Container: %s", sprint_hex(data,4) );
405 PrintAndLogEx(NORMAL, " %02X : NDEF Magic Number", data[0]);
406 PrintAndLogEx(NORMAL, " %02X : version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f);
407 PrintAndLogEx(NORMAL, " %02X : Physical Memory Size: %d bytes", data[2], (data[2] + 1) * 8);
c585a5cf 408 if ( data[2] == 0x12 )
4be9f36e 409 PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 144);
c585a5cf 410 else if ( data[2] == 0x3e )
4be9f36e 411 PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 496);
c585a5cf 412 else if ( data[2] == 0x6d )
4be9f36e 413 PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 872);
1c429594 414
4be9f36e 415 PrintAndLogEx(NORMAL, " %02X : %s / %s", data[3],
416 (data[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
75377d29 417 (data[3] & 0x0F)==0 ? "Write access granted without any security" : (data[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
75377d29 418 return 0;
419}
420
b8dd1ef6 421
c7442b76 422int ul_print_type(uint32_t tagtype, uint8_t spaces){
8ceb6b03 423 char spc[11] = " ";
424 spc[10]=0x00;
425 char *spacer = spc + (10-spaces);
426
b8dd1ef6 427 if (tagtype & UL )
4be9f36e 428 PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight (MF0ICU1) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" );
b8dd1ef6 429 else if (tagtype & UL_C)
4be9f36e 430 PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight C (MF0ULC) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" );
b8dd1ef6 431 else if (tagtype & UL_EV1_48)
4be9f36e 432 PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)", spacer);
b8dd1ef6 433 else if (tagtype & UL_EV1_128)
4be9f36e 434 PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)", spacer);
b8dd1ef6 435 else if (tagtype & NTAG)
4be9f36e 436 PrintAndLogEx(NORMAL, "%sTYPE : NTAG UNKNOWN", spacer);
b8dd1ef6 437 else if (tagtype & NTAG_203)
4be9f36e 438 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 203 144bytes (NT2H0301F0DT)", spacer);
b8dd1ef6 439 else if (tagtype & NTAG_210)
4be9f36e 440 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 210 48bytes (NT2L1011G0DU)", spacer);
b8dd1ef6 441 else if (tagtype & NTAG_212)
4be9f36e 442 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 212 128bytes (NT2L1211G0DU)", spacer);
b8dd1ef6 443 else if (tagtype & NTAG_213)
4be9f36e 444 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 213 144bytes (NT2H1311G0DU)", spacer);
b8dd1ef6 445 else if (tagtype & NTAG_215)
4be9f36e 446 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 215 504bytes (NT2H1511G0DU)", spacer);
b8dd1ef6 447 else if (tagtype & NTAG_216)
4be9f36e 448 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 216 888bytes (NT2H1611G0DU)", spacer);
b8dd1ef6 449 else if (tagtype & NTAG_I2C_1K)
4be9f36e 450 PrintAndLogEx(NORMAL, "%sTYPE : NTAG I%sC 888bytes (NT3H1101FHK)", spacer, "\xFD");
b8dd1ef6 451 else if (tagtype & NTAG_I2C_2K)
4be9f36e 452 PrintAndLogEx(NORMAL, "%sTYPE : NTAG I%sC 1904bytes (NT3H1201FHK)", spacer, "\xFD");
b8dd1ef6 453 else if (tagtype & MY_D)
4be9f36e 454 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 (SLE 66RxxS)", spacer);
b8dd1ef6 455 else if (tagtype & MY_D_NFC)
4be9f36e 456 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 NFC (SLE 66RxxP)", spacer);
b8dd1ef6 457 else if (tagtype & MY_D_MOVE)
458 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 move | my-d\x99move NFC (SLE 66R01P)", spacer);
459 else if (tagtype & MY_D_MOVE_LEAN)
4be9f36e 460 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 move lean (SLE 66R01L)", spacer);
b8dd1ef6 461 else if (tagtype & FUDAN_UL)
4be9f36e 462 PrintAndLogEx(NORMAL, "%sTYPE : FUDAN Ultralight Compatible (or other compatible) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" );
75377d29 463 else
4be9f36e 464 PrintAndLogEx(NORMAL, "%sTYPE : Unknown %06x", spacer, tagtype);
75377d29 465 return 0;
466}
467
b8dd1ef6 468
469static int ulc_print_3deskey(uint8_t *data) {
4be9f36e 470 PrintAndLogEx(NORMAL, " deskey1 [44/0x2C] : %s [%.4s]", sprint_hex(data ,4),data);
471 PrintAndLogEx(NORMAL, " deskey1 [45/0x2D] : %s [%.4s]", sprint_hex(data+4 ,4),data+4);
472 PrintAndLogEx(NORMAL, " deskey2 [46/0x2E] : %s [%.4s]", sprint_hex(data+8 ,4),data+8);
473 PrintAndLogEx(NORMAL, " deskey2 [47/0x2F] : %s [%.4s]", sprint_hex(data+12,4),data+12);
474 PrintAndLogEx(NORMAL, "\n 3des key : %s", sprint_hex(SwapEndian64(data, 16, 8), 16));
75377d29 475 return 0;
476}
477
b8dd1ef6 478
479static int ulc_print_configuration(uint8_t *data) {
75377d29 480
4be9f36e 481 PrintAndLogEx(NORMAL, "--- UL-C Configuration");
b8dd1ef6 482 PrintAndLogEx(NORMAL, " Higher Lockbits [40/0x28] : %s (binary %s %s)",
4be9f36e 483 sprint_hex(data, 2),
484 printBits(1, data),
485 printBits(1, data+1)
486 );
b8dd1ef6 487 PrintAndLogEx(NORMAL, " Counter [41/0x29] : %s", sprint_hex(data+4, 2));
75377d29 488
489 bool validAuth = (data[8] >= 0x03 && data[8] <= 0x30);
b8dd1ef6 490 if (validAuth)
4be9f36e 491 PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s page %d/0x%02X and above need authentication", sprint_hex(data+8, 4), data[8], data[8] );
75377d29 492 else{
b8dd1ef6 493 if (data[8] == 0) {
4be9f36e 494 PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s default", sprint_hex(data+8, 4) );
75377d29 495 } else {
4be9f36e 496 PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s auth byte is out-of-range", sprint_hex(data+8, 4) );
f168b263 497 }
75377d29 498 }
4be9f36e 499 PrintAndLogEx(NORMAL, " Auth1 [43/0x2B] : %s %s",
75377d29 500 sprint_hex(data+12, 4),
501 (data[12] & 1) ? "write access restricted": "read and write access restricted"
502 );
f168b263 503 return 0;
504}
81740aa5 505
b8dd1ef6 506
507static int ulev1_print_configuration(uint8_t *data, uint8_t startPage) {
f168b263 508
4be9f36e 509 PrintAndLogEx(NORMAL, "\n--- Tag Configuration");
75377d29 510
511 bool strg_mod_en = (data[0] & 2);
512 uint8_t authlim = (data[4] & 0x07);
513 bool cfglck = (data[4] & 0x40);
514 bool prot = (data[4] & 0x80);
515 uint8_t vctid = data[5];
516
4be9f36e 517 PrintAndLogEx(NORMAL, " cfg0 [%u/0x%02X] : %s", startPage, startPage, sprint_hex(data, 4));
b8dd1ef6 518 if (data[3] < 0xff)
519 PrintAndLogEx(NORMAL, " - page %d and above need authentication", data[3]);
4be9f36e 520 else
521 PrintAndLogEx(NORMAL, " - pages don't need authentication");
522 PrintAndLogEx(NORMAL, " - strong modulation mode %s", (strg_mod_en) ? "enabled" : "disabled");
523 PrintAndLogEx(NORMAL, " cfg1 [%u/0x%02X] : %s", startPage + 1, startPage + 1, sprint_hex(data+4, 4) );
b8dd1ef6 524 if (authlim == 0)
4be9f36e 525 PrintAndLogEx(NORMAL, " - Unlimited password attempts");
75377d29 526 else
4be9f36e 527 PrintAndLogEx(NORMAL, " - Max number of password attempts is %d", authlim);
528 PrintAndLogEx(NORMAL, " - user configuration %s", cfglck ? "permanently locked":"writeable");
529 PrintAndLogEx(NORMAL, " - %s access is protected with password", prot ? "read and write":"write");
530 PrintAndLogEx(NORMAL, " - %02X, Virtual Card Type Identifier is %s default", vctid, (vctid==0x05)? "":"not");
b8dd1ef6 531 PrintAndLogEx(NORMAL, " PWD [%u/0x%02X] : %s- (cannot be read)", startPage + 2, startPage + 2, sprint_hex(data+8, 4));
532 PrintAndLogEx(NORMAL, " PACK [%u/0x%02X] : %s - (cannot be read)", startPage + 3, startPage + 3, sprint_hex(data+12, 2));
533 PrintAndLogEx(NORMAL, " RFU [%u/0x%02X] : %s- (cannot be read)", startPage + 3, startPage + 3, sprint_hex(data+14, 2));
75377d29 534 return 0;
535}
536
b8dd1ef6 537
538static int ulev1_print_counters(void) {
4be9f36e 539 PrintAndLogEx(NORMAL, "--- Tag Counters");
a2e2bb8a 540 uint8_t tear[1] = {0};
75377d29 541 uint8_t counter[3] = {0,0,0};
c7442b76 542 uint16_t len = 0;
75377d29 543 for ( uint8_t i = 0; i<3; ++i) {
b8dd1ef6 544 ulev1_readTearing(i, tear, sizeof(tear));
545 len = ulev1_readCounter(i, counter, sizeof(counter) );
c7442b76 546 if (len == 3) {
4be9f36e 547 PrintAndLogEx(NORMAL, " [%0d] : %s", i, sprint_hex(counter,3));
548 PrintAndLogEx(NORMAL, " - %02X tearing %s", tear[0], ( tear[0]==0xBD)?"Ok":"failure");
c7442b76 549 }
75377d29 550 }
c7442b76 551 return len;
75377d29 552}
553
b8dd1ef6 554
c585a5cf 555static int ulev1_print_signature( uint8_t *data, uint8_t len){
4be9f36e 556 PrintAndLogEx(NORMAL, "\n--- Tag Signature");
557 PrintAndLogEx(NORMAL, "IC signature public key name : NXP NTAG21x (2013)");
558 PrintAndLogEx(NORMAL, "IC signature public key value : %s", sprint_hex(public_ecda_key, PUBLIC_ECDA_KEYLEN));
559 PrintAndLogEx(NORMAL, " Elliptic curve parameters : secp128r1");
560 PrintAndLogEx(NORMAL, " Tag ECC Signature : %s", sprint_hex(data, len));
c585a5cf 561 //to do: verify if signature is valid
4be9f36e 562 //PrintAndLogEx(NORMAL, "IC signature status: %s valid", (iseccvalid() )?"":"not");
c585a5cf 563 return 0;
564}
345fb24a 565
b8dd1ef6 566
345fb24a 567static int ulev1_print_version(uint8_t *data){
4be9f36e 568 PrintAndLogEx(NORMAL, "\n--- Tag Version");
b8dd1ef6 569 PrintAndLogEx(NORMAL, " Raw bytes : %s", sprint_hex(data, 8) );
4be9f36e 570 PrintAndLogEx(NORMAL, " Vendor ID : %02X, %s", data[1], getManufacturerName(data[1]));
571 PrintAndLogEx(NORMAL, " Product type : %s", getProductTypeStr(data[2]));
572 PrintAndLogEx(NORMAL, " Product subtype : %02X, %s", data[3], (data[3]==1) ?"17 pF":"50pF");
573 PrintAndLogEx(NORMAL, " Major version : %02X", data[4]);
574 PrintAndLogEx(NORMAL, " Minor version : %02X", data[5]);
575 PrintAndLogEx(NORMAL, " Size : %s", getUlev1CardSizeStr(data[6]));
576 PrintAndLogEx(NORMAL, " Protocol type : %02X", data[7]);
345fb24a 577 return 0;
578}
579
c585a5cf 580
b8dd1ef6 581static int ul_magic_test(void) {
582 // try a compatibility write to page0, and see if tag answers with ACK/NACK to the first part of the command
c585a5cf 583 iso14a_card_select_t card;
b8dd1ef6 584 if (!ul_select(&card, false))
c585a5cf 585 return UL_ERROR;
b8dd1ef6 586 int status = ul_comp_write_ex(0, NULL, 0, true);
587 if (status == 0) {
46fcd738 588 return MAGIC;
b8dd1ef6 589 }
46fcd738 590 return 0;
c585a5cf 591}
f04ef473 592
b8dd1ef6 593
c7442b76 594uint32_t GetHF14AMfU_Type(void){
81740aa5 595
92690507 596 TagTypeUL_t tagtype = UNKNOWN;
597 iso14a_card_select_t card;
75377d29 598 uint8_t version[10] = {0x00};
75377d29 599 int len;
600
b8dd1ef6 601 if (!ul_select(&card, true)) {
872337e0 602 DropField();
b8dd1ef6 603 msleep(200);
75377d29 604 return UL_ERROR;
605 }
92690507 606
b8dd1ef6 607 // Check for Ultralight Family
608 if (card.uidlen != 7 || (card.sak & 0x38) != 0x00) {
872337e0 609 DropField();
b8dd1ef6 610 PrintAndLogEx(NORMAL, "Tag is not Ultralight | NTAG | MY-D [ATQA: %02X %02X SAK: %02X]\n", card.atqa[1], card.atqa[0], card.sak);
611 return UL_ERROR;
612 }
345fb24a 613
b8dd1ef6 614 if (card.uid[0] != 0x05) {
615 len = ulev1_getVersion(version, sizeof(version));
616 if (len == 10) {
617 if (version[2] == 0x03 && version[6] == 0x0B)
618 tagtype = UL_EV1_48;
619 else if (version[2] == 0x03 && version[6] != 0x0B)
620 tagtype = UL_EV1_128;
621 else if (version[2] == 0x04 && version[3] == 0x01 && version[6] == 0x0B)
622 tagtype = NTAG_210;
623 else if (version[2] == 0x04 && version[3] == 0x01 && version[6] == 0x0E)
624 tagtype = NTAG_212;
625 else if (version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x0F)
626 tagtype = NTAG_213;
627 else if (version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x11)
628 tagtype = NTAG_215;
629 else if (version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x13)
630 tagtype = NTAG_216;
631 else if (version[2] == 0x04 && version[3] == 0x05 && version[6] == 0x13)
632 tagtype = NTAG_I2C_1K;
633 else if (version[2] == 0x04 && version[3] == 0x05 && version[6] == 0x15)
634 tagtype = NTAG_I2C_2K;
635 else if (version[2] == 0x04)
636 tagtype = NTAG;
345fb24a 637 }
b8dd1ef6 638
a383f4b7 639 // UL vs UL-C vs ntag203 test
b8dd1ef6 640 if (tagtype == UNKNOWN) {
641 ul_halt();
642 if (!ul_select(&card, false)) {
643 DropField();
644 msleep(200);
645 return UL_ERROR;
646 }
a383f4b7 647
648 // do UL_C check first...
8258f409 649 uint8_t nonce[11] = {0x00};
b8dd1ef6 650 len = ulc_requestAuthentication(nonce, sizeof(nonce));
651 ul_halt();
652 if (len == 11) {
a383f4b7 653 tagtype = UL_C;
4be9f36e 654 } else {
a383f4b7 655 // need to re-select after authentication error
b8dd1ef6 656 if (!ul_select(&card, false)) {
657 DropField();
658 msleep(200);
659 return UL_ERROR;
660 }
c7442b76 661
a383f4b7 662 uint8_t data[16] = {0x00};
b8dd1ef6 663 // read page 0x29 (last valid ntag203 page)
664 len = ul_read(0x29, data, sizeof(data));
665 if (len <= 1) {
a383f4b7 666 tagtype = UL;
667 } else {
668 // read page 0x30 (should error if it is a ntag203)
b8dd1ef6 669 len = ul_read(0x30, data, sizeof(data));
670 if (len <= 1) {
671 ul_halt();
a383f4b7 672 tagtype = NTAG_203;
a383f4b7 673 }
674 }
a383f4b7 675 }
345fb24a 676 }
f4217d58 677 if (tagtype & UL) {
4be9f36e 678 tagtype = ul_fudan_check();
b8dd1ef6 679 ul_halt();
f4217d58 680 }
b8dd1ef6 681
682 } else { // manufacturer Infineon. Check for my-d variants
683
345fb24a 684 uint8_t nib = (card.uid[1] & 0xf0) >> 4;
b8dd1ef6 685 switch (nib) {
686 case 1: tagtype = MY_D; break; //or SLE 66RxxS ... up to 512 pages of 8 user bytes...
687 case 2: tagtype = MY_D_NFC; break; //or SLE 66RxxP ... up to 512 pages of 8 user bytes... (or in nfc mode FF pages of 4 bytes)
688 case 3: tagtype = MY_D_MOVE; break; //or SLE 66R01P // 38 pages of 4 bytes
689 case 7: tagtype = MY_D_MOVE_LEAN; break; //or SLE 66R01L // 16 pages of 4 bytes
75377d29 690 }
f168b263 691 }
75377d29 692
46fcd738 693 tagtype |= ul_magic_test();
b8dd1ef6 694
46fcd738 695 if (tagtype == (UNKNOWN | MAGIC)) tagtype = (UL_MAGIC);
b8dd1ef6 696
697 DropField();
698 msleep(200);
699
700 printf("Tagtype: %08x\n", tagtype);
f168b263 701 return tagtype;
702}
703
b8dd1ef6 704
4be9f36e 705static int usage_hf_mfu_info(void) {
706 PrintAndLogEx(NORMAL, "It gathers information about the tag and tries to detect what kind it is.");
707 PrintAndLogEx(NORMAL, "Sometimes the tags are locked down, and you may need a key to be able to read the information");
708 PrintAndLogEx(NORMAL, "The following tags can be identified:\n");
709 PrintAndLogEx(NORMAL, "Ultralight, Ultralight-C, Ultralight EV1, NTAG 203, NTAG 210,");
710 PrintAndLogEx(NORMAL, "NTAG 212, NTAG 213, NTAG 215, NTAG 216, NTAG I2C 1K & 2K");
711 PrintAndLogEx(NORMAL, "my-d, my-d NFC, my-d move, my-d move NFC\n");
712 PrintAndLogEx(NORMAL, "Usage: hf mfu info k <key> l");
713 PrintAndLogEx(NORMAL, " Options : ");
714 PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
715 PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness");
716 PrintAndLogEx(NORMAL, "");
717 PrintAndLogEx(NORMAL, " sample : hf mfu info");
718 PrintAndLogEx(NORMAL, " : hf mfu info k 00112233445566778899AABBCCDDEEFF");
719 PrintAndLogEx(NORMAL, " : hf mfu info k AABBCCDDD");
720 return 0;
721}
722
b8dd1ef6 723
724static int CmdHF14AMfUInfo(const char *Cmd) {
f168b263 725
c585a5cf 726 uint8_t authlim = 0xff;
75377d29 727 iso14a_card_select_t card;
a2e2bb8a 728 bool errors = false;
b8dd1ef6 729 uint8_t keybytes[16] = {0x00};
730 uint8_t *authenticationkey = keybytes;
731 int keyLen = 0;
a2e2bb8a 732 bool hasAuthKey = false;
a383f4b7 733 bool locked = false;
06561c34 734 bool swapEndian = false;
a2e2bb8a 735 uint8_t cmdp = 0;
a2e2bb8a 736 uint8_t pack[4] = {0,0,0,0};
06561c34 737 int len = 0;
f168b263 738
a2e2bb8a 739 while(param_getchar(Cmd, cmdp) != 0x00)
740 {
741 switch(param_getchar(Cmd, cmdp))
742 {
743 case 'h':
744 case 'H':
745 return usage_hf_mfu_info();
746 case 'k':
747 case 'K':
b8dd1ef6 748 keyLen = 32;
749 errors = param_gethex_ex(Cmd, cmdp+1, authenticationkey, &keyLen);
750 if (errors || (keyLen != 32 && keyLen != 8)) { //ul-c or ev1/ntag key length
751 PrintAndLogEx(ERR, "Key has incorrect length.\n");
06561c34 752 errors = true;
a2e2bb8a 753 }
06561c34 754 cmdp += 2;
b8dd1ef6 755 keyLen /= 2;
06561c34 756 hasAuthKey = true;
757 break;
758 case 'l':
759 case 'L':
760 swapEndian = true;
761 cmdp++;
a2e2bb8a 762 break;
763 default:
4be9f36e 764 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
a2e2bb8a 765 errors = true;
766 break;
767 }
768 if(errors) break;
769 }
770
771 //Validations
b8dd1ef6 772 if (errors)
773 return usage_hf_mfu_info();
efd19351 774
75377d29 775 TagTypeUL_t tagtype = GetHF14AMfU_Type();
b8dd1ef6 776 if (tagtype == UL_ERROR) {
777 return -1;
778 }
92690507 779
4be9f36e 780 PrintAndLogEx(NORMAL, "\n--- Tag Information ---------");
781 PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
8ceb6b03 782 ul_print_type(tagtype, 6);
92690507 783
4be9f36e 784 // Swap endianness
b8dd1ef6 785 if (swapEndian && hasAuthKey)
786 authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4 );
06561c34 787
b8dd1ef6 788 if (!ul_auth_select(&card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) {
789 DropField();
790 return -1;
791 }
a2e2bb8a 792
a383f4b7 793 // read pages 0,1,2,3 (should read 4pages)
b8dd1ef6 794 uint8_t data[16];
795 len = ul_read(0, data, sizeof(data));
796 if (len == -1) {
872337e0 797 DropField();
4be9f36e 798 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ");
b8dd1ef6 799 return -1;
800 } else if (len == 16) {
a383f4b7 801 ul_print_default(data);
802 ndef_print_CC(data+12);
efd19351 803 } else {
1c429594 804 locked = true;
805 }
75377d29 806
a383f4b7 807 // UL_C Specific
808 if ((tagtype & UL_C)) {
75377d29 809
810 // read pages 0x28, 0x29, 0x2A, 0x2B
811 uint8_t ulc_conf[16] = {0x00};
b8dd1ef6 812 len = ul_read(0x28, ulc_conf, sizeof(ulc_conf));
813 if (len == -1) {
872337e0 814 DropField();
b8dd1ef6 815 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ UL-C");
816 return -1;
817 }
818 if (len == 16) {
819 ulc_print_configuration(ulc_conf);
820 } else {
821 locked = true;
4be9f36e 822 }
75377d29 823
a383f4b7 824 if ((tagtype & MAGIC)) {
825 //just read key
75377d29 826 uint8_t ulc_deskey[16] = {0x00};
b8dd1ef6 827 len = ul_read(0x2C, ulc_deskey, sizeof(ulc_deskey));
828 if (len == -1) {
872337e0 829 DropField();
4be9f36e 830 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ magic");
b8dd1ef6 831 return -1;
75377d29 832 }
b8dd1ef6 833 if (len == 16) ulc_print_3deskey(ulc_deskey);
c585a5cf 834 } else {
4be9f36e 835 // if we called info with key, just return
b8dd1ef6 836 if (hasAuthKey) {
837 DropField();
838 return 1;
839 }
a2e2bb8a 840
06561c34 841 // also try to diversify default keys.. look into CmdHF14AMfuGenDiverseKeys
4be9f36e 842 PrintAndLogEx(INFO, "Trying some default 3des keys");
a383f4b7 843 for (uint8_t i = 0; i < KEYS_3DES_COUNT; ++i ) {
b8dd1ef6 844 uint8_t *key = default_3des_keys[i];
a383f4b7 845 if (ulc_authentication(key, true)) {
b8dd1ef6 846 DropField();
4be9f36e 847 PrintAndLogEx(SUCCESS, "Found default 3des key: ");
2b3af97d 848 uint8_t keySwap[16];
849 memcpy(keySwap, SwapEndian64(key,16,8), 16);
850 ulc_print_3deskey(keySwap);
efd19351 851 return 1;
4be9f36e 852 }
f168b263 853 }
b8dd1ef6 854 DropField();
efd19351 855 return 1;
75377d29 856 }
857 }
858
4be9f36e 859 // do counters and signature first (don't neet auth)
a383f4b7 860
861 // ul counters are different than ntag counters
c7442b76 862 if ((tagtype & (UL_EV1_48 | UL_EV1_128))) {
863 if (ulev1_print_counters() != 3) {
864 // failed - re-select
b8dd1ef6 865 if (!ul_auth_select( &card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) {
866 DropField();
867 return -1;
868 }
c7442b76 869 }
870 }
345fb24a 871
4be9f36e 872 if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_213 | NTAG_215 | NTAG_216 | NTAG_I2C_1K | NTAG_I2C_2K ))) {
345fb24a 873 uint8_t ulev1_signature[32] = {0x00};
b8dd1ef6 874 len = ulev1_readSignature(ulev1_signature, sizeof(ulev1_signature));
875 if (len == -1) {
872337e0 876 DropField();
b8dd1ef6 877 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ SIGNATURE");
878 return -1;
75377d29 879 }
b8dd1ef6 880 if (len == 32) {
881 ulev1_print_signature( ulev1_signature, sizeof(ulev1_signature));
882 } else {
c7442b76 883 // re-select
b8dd1ef6 884 if (!ul_auth_select( &card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) {
885 DropField();
886 return -1;
887 }
c7442b76 888 }
f168b263 889 }
75377d29 890
46fcd738 891 if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_210 | NTAG_212 | NTAG_213 | NTAG_215 | NTAG_216 | NTAG_I2C_1K | NTAG_I2C_2K))) {
75377d29 892 uint8_t version[10] = {0x00};
b8dd1ef6 893 len = ulev1_getVersion(version, sizeof(version));
894 if (len == -1) {
872337e0 895 DropField();
b8dd1ef6 896 PrintAndLogEx(WARNING, "Error: tag didn't answer to GETVERSION");
897 return -1;
898 } else if (len == 10) {
1c429594 899 ulev1_print_version(version);
900 } else {
901 locked = true;
b8dd1ef6 902 if (!ul_auth_select( &card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) {
903 DropField();
904 return -1;
905 }
75377d29 906 }
a383f4b7 907
908 uint8_t startconfigblock = 0;
909 uint8_t ulev1_conf[16] = {0x00};
910 // config blocks always are last 4 pages
b8dd1ef6 911 for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++) {
912 if (tagtype & UL_TYPES_ARRAY[idx]) {
a383f4b7 913 startconfigblock = UL_MEMORY_ARRAY[idx]-3;
b8dd1ef6 914 break;
915 }
916 }
c585a5cf 917
b8dd1ef6 918 if (startconfigblock) { // if we know where the config block is...
919 len = ul_read(startconfigblock, ulev1_conf, sizeof(ulev1_conf));
920 if (len == -1) {
872337e0 921 DropField();
b8dd1ef6 922 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ EV1");
923 return -1;
924 } else if (len == 16) {
46fcd738 925 // save AUTHENTICATION LIMITS for later:
926 authlim = (ulev1_conf[4] & 0x07);
012c0761 927 ulev1_print_configuration(ulev1_conf, startconfigblock);
46fcd738 928 }
8258f409 929 }
a2e2bb8a 930
c585a5cf 931 // AUTHLIMIT, (number of failed authentications)
932 // 0 = limitless.
a383f4b7 933 // 1-7 = limit. No automatic tries then.
934 // hasAuthKey, if we was called with key, skip test.
b8dd1ef6 935 if (!authlim && !hasAuthKey) {
4be9f36e 936 PrintAndLogEx(NORMAL, "\n--- Known EV1/NTAG passwords.");
9d87eb66 937 len = 0;
a383f4b7 938 for (uint8_t i = 0; i < KEYS_PWD_COUNT; ++i ) {
b8dd1ef6 939 uint8_t *key = default_pwd_pack[i];
9d87eb66 940 len = ulev1_requestAuthentication(key, pack, sizeof(pack));
941 if (len >= 1) {
b8dd1ef6 942 PrintAndLogEx(SUCCESS, "Found a default password: %s || Pack: %02X %02X", sprint_hex(key, 4), pack[0], pack[1]);
9d87eb66 943 break;
944 } else {
b8dd1ef6 945 if (!ul_auth_select( &card, tagtype, hasAuthKey, authenticationkey, pack, sizeof(pack))) {
946 DropField();
947 return -1;
948 }
c585a5cf 949 }
950 }
4be9f36e 951 if (len < 1) PrintAndLogEx(WARNING, "password not known");
75377d29 952 }
f168b263 953 }
c585a5cf 954
872337e0 955 DropField();
b8dd1ef6 956
957 if (locked)
958 PrintAndLogEx(FAILED, "\nTag appears to be locked, try using the key to get more info");
4be9f36e 959 PrintAndLogEx(NORMAL, "");
b8dd1ef6 960
a2e2bb8a 961 return 1;
81740aa5 962}
963
964//
79d7bcbb 965// Write Single Block
81740aa5 966//
4be9f36e 967static int usage_hf_mfu_wrbl(void) {
968 PrintAndLogEx(NORMAL, "Write a block. It autodetects card type.\n");
969 PrintAndLogEx(NORMAL, "Usage: hf mfu wrbl b <block number> d <data> k <key> l\n");
970 PrintAndLogEx(NORMAL, " Options:");
971 PrintAndLogEx(NORMAL, " b <no> : block to write");
972 PrintAndLogEx(NORMAL, " d <data> : block data - (8 hex symbols)");
973 PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
974 PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness");
975 PrintAndLogEx(NORMAL, "");
976 PrintAndLogEx(NORMAL, " sample : hf mfu wrbl b 0 d 01234567");
977 PrintAndLogEx(NORMAL, " : hf mfu wrbl b 0 d 01234567 k AABBCCDDD\n");
978 return 0;
979}
980
b8dd1ef6 981
4be9f36e 982static int CmdHF14AMfUWrBl(const char *Cmd){
81740aa5 983
79d7bcbb 984 int blockNo = -1;
985 bool errors = false;
b8dd1ef6 986 uint8_t keybytes[16] = {0x00};
987 uint8_t *authenticationkey = keybytes;
988 int keyLen = 0;
79d7bcbb 989 bool hasAuthKey = false;
79d7bcbb 990 bool swapEndian = false;
79d7bcbb 991 uint8_t cmdp = 0;
79d7bcbb 992 uint8_t blockdata[20] = {0x00};
79d7bcbb 993
b8dd1ef6 994 while(param_getchar(Cmd, cmdp) != 0x00) {
995 switch(param_getchar(Cmd, cmdp)) {
79d7bcbb 996 case 'h':
997 case 'H':
998 return usage_hf_mfu_wrbl();
999 case 'k':
1000 case 'K':
b8dd1ef6 1001 keyLen = 32;
1002 errors = param_gethex_ex(Cmd, cmdp+1, authenticationkey, &keyLen);
1003 if (errors || (keyLen != 32 && keyLen != 8)) { //ul-c or ev1/ntag key length
1004 PrintAndLogEx(ERR, "Key has incorrect length.\n");
1005 errors = true;
79d7bcbb 1006 }
b8dd1ef6 1007 cmdp += 2;
1008 keyLen /= 2;
1009 hasAuthKey = true;
79d7bcbb 1010 break;
1011 case 'b':
1012 case 'B':
1013 blockNo = param_get8(Cmd, cmdp+1);
79d7bcbb 1014 if (blockNo < 0) {
4be9f36e 1015 PrintAndLogEx(ERR, "Wrong block number");
79d7bcbb 1016 errors = true;
1017 }
79d7bcbb 1018 cmdp += 2;
1019 break;
1020 case 'l':
1021 case 'L':
1022 swapEndian = true;
4be9f36e 1023 cmdp++;
79d7bcbb 1024 break;
1025 case 'd':
1026 case 'D':
1027 if ( param_gethex(Cmd, cmdp+1, blockdata, 8) ) {
4be9f36e 1028 PrintAndLogEx(ERR, "Block data must include 8 HEX symbols");
79d7bcbb 1029 errors = true;
1030 break;
1031 }
1032 cmdp += 2;
1033 break;
1034 default:
4be9f36e 1035 PrintAndLogEx(ERR, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
79d7bcbb 1036 errors = true;
1037 break;
1038 }
1039 //Validations
1040 if(errors) return usage_hf_mfu_wrbl();
afceaf40 1041 }
79d7bcbb 1042
b8dd1ef6 1043 if (blockNo == -1) return usage_hf_mfu_wrbl();
2ec87733 1044 // starting with getting tagtype
1045 TagTypeUL_t tagtype = GetHF14AMfU_Type();
b8dd1ef6 1046 if (tagtype == UL_ERROR) {
1047 return -1;
1048 }
2ec87733 1049
1050 uint8_t maxblockno = 0;
b8dd1ef6 1051 for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++) {
1052 if (tagtype & UL_TYPES_ARRAY[idx]) {
2ec87733 1053 maxblockno = UL_MEMORY_ARRAY[idx];
b8dd1ef6 1054 break;
1055 }
2ec87733 1056 }
1057 if (blockNo > maxblockno){
b8dd1ef6 1058 DropField();
4be9f36e 1059 PrintAndLogEx(WARNING, "block number too large. Max block is %u/0x%02X \n", maxblockno,maxblockno);
2ec87733 1060 return usage_hf_mfu_wrbl();
1061 }
79d7bcbb 1062
4be9f36e 1063 // Swap endianness
b8dd1ef6 1064 if (swapEndian && hasAuthKey) authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4);
79d7bcbb 1065
1066 if ( blockNo <= 3)
4be9f36e 1067 PrintAndLogEx(NORMAL, "Special Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(blockdata, 4));
79d7bcbb 1068 else
4be9f36e 1069 PrintAndLogEx(NORMAL, "Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(blockdata, 4));
79d7bcbb 1070
1071 //Send write Block
1072 UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}};
b8dd1ef6 1073 memcpy(c.d.asBytes, blockdata, 4);
79d7bcbb 1074
b8dd1ef6 1075 if (hasAuthKey) {
1076 c.arg[1] = (keyLen == 16) ? 1 : 2;
1077 memcpy(c.d.asBytes+4, authenticationkey, keyLen);
afceaf40 1078 }
79d7bcbb 1079
22342f6d 1080 clearCommandBuffer();
79d7bcbb 1081 SendCommand(&c);
1082 UsbCommand resp;
b8dd1ef6 1083 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
79d7bcbb 1084 uint8_t isOK = resp.arg[0] & 0xff;
4be9f36e 1085 PrintAndLogEx(SUCCESS, "isOk:%02x", isOK);
81740aa5 1086 } else {
4be9f36e 1087 PrintAndLogEx(ERR, "Command execute timeout");
afceaf40 1088 }
79d7bcbb 1089
b8dd1ef6 1090 DropField();
afceaf40 1091 return 0;
81740aa5 1092}
4be9f36e 1093
1094
81740aa5 1095//
79d7bcbb 1096// Read Single Block
81740aa5 1097//
4be9f36e 1098static int usage_hf_mfu_rdbl(void) {
1099 PrintAndLogEx(NORMAL, "Read a block and print. It autodetects card type.\n");
1100 PrintAndLogEx(NORMAL, "Usage: hf mfu rdbl b <block number> k <key> l\n");
1101 PrintAndLogEx(NORMAL, " Options:");
1102 PrintAndLogEx(NORMAL, " b <no> : block to read");
1103 PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
1104 PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness");
1105 PrintAndLogEx(NORMAL, "");
1106 PrintAndLogEx(NORMAL, " sample : hf mfu rdbl b 0");
1107 PrintAndLogEx(NORMAL, " : hf mfu rdbl b 0 k 00112233445566778899AABBCCDDEEFF");
1108 PrintAndLogEx(NORMAL, " : hf mfu rdbl b 0 k AABBCCDDD\n");
1109 return 0;
1110}
81740aa5 1111
b8dd1ef6 1112
4be9f36e 1113static int CmdHF14AMfURdBl(const char *Cmd){
1114
1115 int blockNo = -1;
79d7bcbb 1116 bool errors = false;
b8dd1ef6 1117 uint8_t keybytes[16] = {0x00};
1118 uint8_t *authenticationkey = keybytes;
1119 int keyLen = 0;
79d7bcbb 1120 bool hasAuthKey = false;
79d7bcbb 1121 bool swapEndian = false;
1122 uint8_t cmdp = 0;
f168b263 1123
79d7bcbb 1124 while(param_getchar(Cmd, cmdp) != 0x00)
1125 {
1126 switch(param_getchar(Cmd, cmdp))
1127 {
1128 case 'h':
1129 case 'H':
1130 return usage_hf_mfu_rdbl();
1131 case 'k':
1132 case 'K':
b8dd1ef6 1133 keyLen = 32;
1134 errors = param_gethex_ex(Cmd, cmdp+1, authenticationkey, &keyLen);
1135 if (errors || (keyLen != 32 && keyLen != 8)) { //ul-c or ev1/ntag key length
1136 PrintAndLogEx(ERR, "Key has incorrect length.\n");
1137 errors = true;
79d7bcbb 1138 }
b8dd1ef6 1139 cmdp += 2;
1140 keyLen /= 2;
1141 hasAuthKey = true;
79d7bcbb 1142 break;
1143 case 'b':
1144 case 'B':
1145 blockNo = param_get8(Cmd, cmdp+1);
79d7bcbb 1146 if (blockNo < 0) {
4be9f36e 1147 PrintAndLogEx(ERR, "Wrong block number");
79d7bcbb 1148 errors = true;
1149 }
79d7bcbb 1150 cmdp += 2;
1151 break;
1152 case 'l':
1153 case 'L':
1154 swapEndian = true;
1155 cmdp++;
1156 break;
1157 default:
4be9f36e 1158 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
79d7bcbb 1159 errors = true;
1160 break;
1161 }
1162 //Validations
b8dd1ef6 1163 if (errors) return usage_hf_mfu_rdbl();
afceaf40 1164 }
f168b263 1165
b8dd1ef6 1166 if (blockNo == -1) return usage_hf_mfu_rdbl();
2ec87733 1167 // start with getting tagtype
1168 TagTypeUL_t tagtype = GetHF14AMfU_Type();
b8dd1ef6 1169 if (tagtype == UL_ERROR) {
1170 return -1;
1171 }
2ec87733 1172
1173 uint8_t maxblockno = 0;
b8dd1ef6 1174 for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++) {
1175 if (tagtype & UL_TYPES_ARRAY[idx]) {
2ec87733 1176 maxblockno = UL_MEMORY_ARRAY[idx];
b8dd1ef6 1177 break;
1178 }
2ec87733 1179 }
1180 if (blockNo > maxblockno){
b8dd1ef6 1181 DropField();
4be9f36e 1182 PrintAndLogEx(WARNING, "block number to large. Max block is %u/0x%02X \n", maxblockno,maxblockno);
2ec87733 1183 return usage_hf_mfu_rdbl();
1184 }
afceaf40 1185
4be9f36e 1186 // Swap endianness
b8dd1ef6 1187 if (swapEndian) authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4);
f168b263 1188
79d7bcbb 1189 //Read Block
1190 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
b8dd1ef6 1191 if (hasAuthKey) {
1192 c.arg[1] = (keyLen == 16) ? 1 : 2;
1193 memcpy(c.d.asBytes, authenticationkey, keyLen);
79d7bcbb 1194 }
1195
22342f6d 1196 clearCommandBuffer();
79d7bcbb 1197 SendCommand(&c);
1198 UsbCommand resp;
b8dd1ef6 1199 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
f168b263 1200 uint8_t isOK = resp.arg[0] & 0xff;
1201 if (isOK) {
1202 uint8_t *data = resp.d.asBytes;
4be9f36e 1203 PrintAndLogEx(NORMAL, "\n Block# | Data | Ascii");
1204 PrintAndLogEx(NORMAL, "---------+-------------+------");
1205 PrintAndLogEx(NORMAL, " %02d/0x%02X | %s| %s\n", blockNo, blockNo, sprint_hex(data, 4), sprint_ascii(data, 4));
1206 } else {
1207 PrintAndLogEx(ERR, "Failed reading block: (%02x)", isOK);
f168b263 1208 }
81740aa5 1209 } else {
4be9f36e 1210 PrintAndLogEx(ERR, "Command execute time-out");
afceaf40 1211 }
b8dd1ef6 1212 DropField();
afceaf40 1213 return 0;
81740aa5 1214}
1215
79d7bcbb 1216
81740aa5 1217//
92690507 1218// Mifare Ultralight / Ultralight-C / Ultralight-EV1
1219// Read and Dump Card Contents, using auto detection of tag size.
b8dd1ef6 1220
1221typedef struct {
1222 uint8_t version[8];
1223 uint8_t tbo[2];
1224 uint8_t tbo1[1];
1225 uint8_t pages; // max page number in dump
1226 uint8_t signature[32];
1227 uint8_t counter_tearing[3][4]; // 3 bytes counter, 1 byte tearing flag
1228 uint8_t data[1024];
1229} mfu_dump_t;
1230
1231
1232static void printMFUdumpEx(mfu_dump_t *card, uint16_t pages, uint8_t startpage, TagTypeUL_t tagtype) {
1233
1234 bool tmplockbit = false;
1235 bool bit[16] = {false};
1236 bool bit2[16] = {false};
1237
1238 // standard lock bits
1239 for(int i = 0; i < 16; i++){
1240 bit[i] = card->data[10+i/8] & (1 << (7-i%8));
1241 }
1242
1243 // dynamic lock bits
1244 // TODO -- FIGURE OUT LOCK BYTES FOR EV1 and/or NTAG
1245 if (tagtype & UL_C) {
1246 for (int i = 0; i < 16; i++) {
1247 bit2[i] = card->data[40*4+i/8] & (1 << (7-i%8));
1248 }
1249 }
1250
1251 PrintAndLogEx(NORMAL, "\n Block# | Data |lck| Ascii");
1252 PrintAndLogEx(NORMAL, "---------+-------------+---+------");
1253
1254 for (int i = startpage; i < startpage + pages; i++) {
1255 if (i < 3) {
1256 PrintAndLogEx(NORMAL, "%3d/0x%02X | %s| | ", i, i, sprint_hex(card->data + i * 4, 4));
1257 continue;
1258 }
1259 switch(i){
1260 case 3: tmplockbit = bit[4]; break;
1261 case 4: tmplockbit = bit[3]; break;
1262 case 5: tmplockbit = bit[2]; break;
1263 case 6: tmplockbit = bit[1]; break;
1264 case 7: tmplockbit = bit[0]; break;
1265 case 8: tmplockbit = bit[15]; break;
1266 case 9: tmplockbit = bit[14]; break;
1267 case 10: tmplockbit = bit[13]; break;
1268 case 11: tmplockbit = bit[12]; break;
1269 case 12: tmplockbit = bit[11]; break;
1270 case 13: tmplockbit = bit[10]; break;
1271 case 14: tmplockbit = bit[9]; break;
1272 case 15: tmplockbit = bit[8]; break;
1273 case 16:
1274 case 17:
1275 case 18:
1276 case 19: tmplockbit = bit2[6]; break;
1277 case 20:
1278 case 21:
1279 case 22:
1280 case 23: tmplockbit = bit2[5]; break;
1281 case 24:
1282 case 25:
1283 case 26:
1284 case 27: tmplockbit = bit2[4]; break;
1285 case 28:
1286 case 29:
1287 case 30:
1288 case 31: tmplockbit = bit2[2]; break;
1289 case 32:
1290 case 33:
1291 case 34:
1292 case 35: tmplockbit = bit2[1]; break;
1293 case 36:
1294 case 37:
1295 case 38:
1296 case 39: tmplockbit = bit2[0]; break;
1297 case 40: tmplockbit = bit2[12]; break;
1298 case 41: tmplockbit = bit2[11]; break;
1299 case 42: tmplockbit = bit2[10]; break; //auth0
1300 case 43: tmplockbit = bit2[9]; break; //auth1
1301 default: break;
1302 }
1303
1304 PrintAndLogEx(NORMAL, "%3d/0x%02X | %s| %d | %.4s", i, i, sprint_hex(card->data + i * 4, 4), tmplockbit, sprint_ascii(card->data + i * 4, 4));
1305 }
1306 PrintAndLogEx(NORMAL, "---------------------------------");
1307}
1308
1309
4be9f36e 1310static int usage_hf_mfu_dump(void) {
1311 PrintAndLogEx(NORMAL, "Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
1312 PrintAndLogEx(NORMAL, "NTAG 203, NTAG 210, NTAG 212, NTAG 213, NTAG 215, NTAG 216");
1313 PrintAndLogEx(NORMAL, "and saves binary dump into the file `filename.bin` or `cardUID.bin`");
1314 PrintAndLogEx(NORMAL, "It autodetects card type.\n");
1315 PrintAndLogEx(NORMAL, "Usage: hf mfu dump k <key> l n <filename w/o .bin>");
1316 PrintAndLogEx(NORMAL, " Options : ");
1317 PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
1318 PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness");
b8dd1ef6 1319 PrintAndLogEx(NORMAL, " f <FN > : filename w/o .bin to save the dump as");
4be9f36e 1320 PrintAndLogEx(NORMAL, " p <Pg > : starting Page number to manually set a page to start the dump at");
1321 PrintAndLogEx(NORMAL, " q <qty> : number of Pages to manually set how many pages to dump");
1322
1323 PrintAndLogEx(NORMAL, "");
1324 PrintAndLogEx(NORMAL, " sample : hf mfu dump");
1325 PrintAndLogEx(NORMAL, " : hf mfu dump n myfile");
1326 PrintAndLogEx(NORMAL, " : hf mfu dump k 00112233445566778899AABBCCDDEEFF");
1327 PrintAndLogEx(NORMAL, " : hf mfu dump k AABBCCDDD\n");
1328 return 0;
1329}
1330
b8dd1ef6 1331
4be9f36e 1332static int CmdHF14AMfUDump(const char *Cmd){
81740aa5 1333
b8dd1ef6 1334 char filename[FILE_PATH_SIZE] = {'\0'};
1335 size_t fileNameLen = 0;
1336 uint8_t keybytes[16] = {0x00};
1337 uint8_t *authenticationkey = keybytes;
1338 int keyLen = 0;
06561c34 1339 bool hasAuthKey = false;
06561c34 1340 uint8_t cmdp = 0;
75377d29 1341 bool errors = false;
1342 bool swapEndian = false;
ae8303c1 1343 bool manualPages = false;
1344 uint8_t startPage = 0;
b8dd1ef6 1345 int Pages = 16;
1346 iso14a_card_select_t card_select;
1347 mfu_dump_t card;
cceabb79 1348
f9848fd6 1349 while(param_getchar(Cmd, cmdp) != 0x00)
1350 {
1351 switch(param_getchar(Cmd, cmdp))
1352 {
1353 case 'h':
1354 case 'H':
1355 return usage_hf_mfu_dump();
1356 case 'k':
1357 case 'K':
b8dd1ef6 1358 keyLen = 32;
1359 errors = param_gethex_ex(Cmd, cmdp+1, authenticationkey, &keyLen);
1360 if (errors || (keyLen != 32 && keyLen != 8)) { //ul-c or ev1/ntag key length
1361 PrintAndLogEx(ERR, "Key has incorrect length.\n");
cceabb79 1362 errors = true;
9d87eb66 1363 }
f9848fd6 1364 cmdp += 2;
b8dd1ef6 1365 keyLen /= 2;
06561c34 1366 hasAuthKey = true;
f9848fd6 1367 break;
cceabb79 1368 case 'l':
1369 case 'L':
1370 swapEndian = true;
1371 cmdp++;
1372 break;
b8dd1ef6 1373 case 'f':
1374 case 'F':
1375 fileNameLen = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));
1376 if (fileNameLen == 0) errors = true;
1377 if (fileNameLen > FILE_PATH_SIZE-5) fileNameLen = FILE_PATH_SIZE-5;
f9848fd6 1378 cmdp += 2;
1379 break;
ae8303c1 1380 case 'p':
1381 case 'P':
1382 startPage = param_get8(Cmd, cmdp+1);
1383 manualPages = true;
1384 cmdp += 2;
1385 break;
1386 case 'q':
1387 case 'Q':
1388 Pages = param_get8(Cmd, cmdp+1);
1389 cmdp += 2;
1390 manualPages = true;
1391 break;
f9848fd6 1392 default:
b8dd1ef6 1393 PrintAndLogEx(ERR, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
f9848fd6 1394 errors = true;
1395 break;
1396 }
b8dd1ef6 1397 if (errors) break;
f9848fd6 1398 }
1399
1400 //Validations
b8dd1ef6 1401 if (errors) return usage_hf_mfu_dump();
75377d29 1402
4be9f36e 1403 if (swapEndian && hasAuthKey)
b8dd1ef6 1404 authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4);
81740aa5 1405
92690507 1406 TagTypeUL_t tagtype = GetHF14AMfU_Type();
81740aa5 1407
b8dd1ef6 1408 if (tagtype == UL_ERROR) {
1409 return -1;
1410 }
1411
1412 uint8_t maxPages = 0;
1413 for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++) {
1414 if (tagtype & UL_TYPES_ARRAY[idx]) {
1415 maxPages = UL_MEMORY_ARRAY[idx]+1; //add one as maxblks starts at 0
1416 break;
1417 }
1418 }
1419
1420 if (!manualPages) {
1421 Pages = maxPages;
1422 } else {
1423 if (startPage + Pages - 1 > maxPages - 1) {
1424 PrintAndLogEx(ERR, "Invalid page range. Card has only %d readable pages.", maxPages);
1425 DropField();
1426 return 1;
1427 }
1428 }
ae8303c1 1429
5b99376a 1430 ul_print_type(tagtype, 0);
b8dd1ef6 1431
4be9f36e 1432 PrintAndLogEx(NORMAL, "Reading tag memory...");
b8dd1ef6 1433 memset(&card, 0x00, sizeof(card));
1434 UsbCommand c = {CMD_MIFAREU_READCARD, {startPage, Pages}};
1435 if (hasAuthKey) {
cceabb79 1436 if (tagtype & UL_C)
1437 c.arg[2] = 1; //UL_C auth
1438 else
1439 c.arg[2] = 2; //UL_EV1/NTAG auth
b8dd1ef6 1440 memcpy(c.d.asBytes, authenticationkey, keyLen);
f168b263 1441 }
22342f6d 1442
1443 clearCommandBuffer();
cceabb79 1444 SendCommand(&c);
1445 UsbCommand resp;
b8dd1ef6 1446 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
1447 PrintAndLogEx(ERR, "Command execution timeout");
1448 DropField();
cceabb79 1449 return 1;
92690507 1450 }
9d87eb66 1451 if (resp.arg[0] != 1) {
b8dd1ef6 1452 PrintAndLogEx(ERR, "Failed reading card");
1453 DropField();
cceabb79 1454 return 1;
92690507 1455 }
92690507 1456
0ce03d9a 1457 uint32_t startindex = resp.arg[2];
9d87eb66 1458 uint32_t bufferSize = resp.arg[1];
b8dd1ef6 1459 if (bufferSize > sizeof(card.data)) {
4be9f36e 1460 PrintAndLogEx(FAILED, "Data exceeded Buffer size!");
b8dd1ef6 1461 bufferSize = sizeof(card.data);
9d87eb66 1462 }
9d87eb66 1463
b8dd1ef6 1464 if (!GetFromBigBuf(card.data + startPage*4, bufferSize, startindex, NULL, -1, false)) {
1465 PrintAndLogEx(ERR, "Command execution timeout");
1466 DropField();
1467 return 1;
92690507 1468 }
1469
b8dd1ef6 1470 // not ul_c and not std ul then attempt to collect
1471 // VERSION, SIGNATURE, COUNTERS, TEARING, PACK
1472 if (!(tagtype & UL_C || tagtype & UL)) {
1473 //attempt to read pack
1474 if (!ul_auth_select(&card_select, tagtype, true, authenticationkey, card.data + maxPages*4 - 4, 2)) {
1475 //reset pack
1476 card.data[maxPages*4 - 4] = 0;
1477 card.data[maxPages*4 - 3] = 0;
81740aa5 1478 }
81740aa5 1479
b8dd1ef6 1480 if (hasAuthKey) {
1481 uint8_t dummy_pack[2];
1482 ul_auth_select(&card_select, tagtype, hasAuthKey, authenticationkey, dummy_pack, sizeof(dummy_pack));
06561c34 1483 } else {
b8dd1ef6 1484 ul_select(&card_select, false);
1485 }
1486 ulev1_getVersion(card.version, sizeof(card.version));
1487 for (uint8_t n = 0; n < 3; ++n) {
1488 ulev1_readTearing(n, &card.counter_tearing[n][3], 1);
1489 ulev1_readCounter(n, &card.counter_tearing[n][0], 3);
012c0761 1490 }
1491
b8dd1ef6 1492 ulev1_readSignature(card.signature, sizeof(card.signature));
1493 }
1494
1495 DropField();
1496
1497 // add key to dump data
1498 if (hasAuthKey) {
1499 authenticationkey = SwapEndian64(authenticationkey, keyLen, (keyLen == 16) ? 8 : 4);
1500 if (tagtype & UL_C){ // additional 4 pages
1501 memcpy(card.data + maxPages*4, authenticationkey, keyLen);
1502 maxPages += 4;
012c0761 1503 } else { // 2nd page from end
b8dd1ef6 1504 memcpy(card.data + (maxPages*4) - 8, authenticationkey, 4);
06561c34 1505 }
9d87eb66 1506 }
cceabb79 1507
b8dd1ef6 1508 printMFUdumpEx(&card, Pages, startPage, tagtype);
1509
1510 if (!manualPages) {
1511 // user supplied filename?
1512 if (fileNameLen < 1) {
1513 char *fptr = filename;
1514 fptr += sprintf(fptr, "hf-mfu-");
1515 uint8_t UID[] = {card.data[0], card.data[1], card.data[2], card.data[4], card.data[5], card.data[6], card.data[7]};
1516 FillFileNameByUID(fptr, UID, "-dump.bin", 7);
1517 } else {
1518 sprintf(filename + fileNameLen, ".bin");
81740aa5 1519 }
4bbf5ad1 1520
b8dd1ef6 1521#define MFU_DUMP_PREFIX_LENGTH (sizeof(card) - sizeof(card.data))
4bbf5ad1 1522
b8dd1ef6 1523 FILE *fout;
1524 if ((fout = fopen(filename, "wb")) == NULL) {
1525 PrintAndLogEx(ERR, "Could not create file name %s", filename);
1526 return 1;
1527 }
1528 fwrite(&card, 1, MFU_DUMP_PREFIX_LENGTH + maxPages*4, fout);
1529 fclose(fout);
22342f6d 1530
b8dd1ef6 1531 PrintAndLogEx(SUCCESS, "Dumped %d pages, wrote %d bytes to %s", maxPages, MFU_DUMP_PREFIX_LENGTH + maxPages*4, filename);
81740aa5 1532 }
1533
afceaf40 1534 return 0;
81740aa5 1535}
1536
b8dd1ef6 1537
81740aa5 1538//-------------------------------------------------------------------------------
1539// Ultralight C Methods
1540//-------------------------------------------------------------------------------
1541
1542//
1543// Ultralight C Authentication Demo {currently uses hard-coded key}
1544//
4be9f36e 1545static int CmdHF14AMfucAuth(const char *Cmd){
afceaf40 1546
9d87eb66 1547 uint8_t keyNo = 3;
afceaf40 1548 bool errors = false;
f168b263 1549
1550 char cmdp = param_getchar(Cmd, 0);
1551
afceaf40
MHS
1552 //Change key to user defined one
1553 if (cmdp == 'k' || cmdp == 'K'){
1554 keyNo = param_get8(Cmd, 1);
4be9f36e 1555 if(keyNo > KEYS_3DES_COUNT-1)
f168b263 1556 errors = true;
afceaf40
MHS
1557 }
1558
f168b263 1559 if (cmdp == 'h' || cmdp == 'H')
afceaf40 1560 errors = true;
4be9f36e 1561
afceaf40 1562 if (errors) {
4be9f36e 1563 PrintAndLogEx(NORMAL, "Usage: hf mfu cauth k <key number>");
1564 PrintAndLogEx(NORMAL, " 0 (default): 3DES standard key");
1565 PrintAndLogEx(NORMAL, " 1 : all 0x00 key");
1566 PrintAndLogEx(NORMAL, " 2 : 0x00-0x0F key");
1567 PrintAndLogEx(NORMAL, " 3 : nfc key");
1568 PrintAndLogEx(NORMAL, " 4 : all 0x01 key");
1569 PrintAndLogEx(NORMAL, " 5 : all 0xff key");
1570 PrintAndLogEx(NORMAL, " 6 : 0x00-0xFF key");
1571 PrintAndLogEx(NORMAL, "\n sample : hf mfu cauth k");
1572 PrintAndLogEx(NORMAL, " : hf mfu cauth k 3");
afceaf40 1573 return 0;
4be9f36e 1574 }
afceaf40 1575
f168b263 1576 uint8_t *key = default_3des_keys[keyNo];
b8dd1ef6 1577 if (ulc_authentication(key, true)) {
1578 DropField();
4be9f36e 1579 PrintAndLogEx(SUCCESS, "Authentication successful. 3des key: %s",sprint_hex(key, 16));
b8dd1ef6 1580 } else {
1581 DropField();
4be9f36e 1582 PrintAndLogEx(WARNING, "Authentication failed");
afceaf40 1583 }
4be9f36e 1584 return 0;
afceaf40 1585}
b8dd1ef6 1586
f9848fd6 1587
4be9f36e 1588//
f168b263 1589// Mifare Ultralight C - Set password
1590//
4be9f36e 1591static int CmdHF14AMfucSetPwd(const char *Cmd){
f168b263 1592
1593 uint8_t pwd[16] = {0x00};
4be9f36e 1594
f168b263 1595 char cmdp = param_getchar(Cmd, 0);
4be9f36e 1596
1597 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
1598 PrintAndLogEx(NORMAL, "Usage: hf mfu setpwd <password (32 hex symbols)>");
1599 PrintAndLogEx(NORMAL, " [password] - (32 hex symbols)");
1600 PrintAndLogEx(NORMAL, "");
1601 PrintAndLogEx(NORMAL, "sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1602 PrintAndLogEx(NORMAL, "");
f168b263 1603 return 0;
1604 }
4be9f36e 1605
f168b263 1606 if (param_gethex(Cmd, 0, pwd, 32)) {
4be9f36e 1607 PrintAndLogEx(WARNING, "Password must include 32 HEX symbols");
f168b263 1608 return 1;
1609 }
4be9f36e 1610
1611 UsbCommand c = {CMD_MIFAREUC_SETPWD};
f168b263 1612 memcpy( c.d.asBytes, pwd, 16);
22342f6d 1613 clearCommandBuffer();
f168b263 1614 SendCommand(&c);
1615
1616 UsbCommand resp;
4be9f36e 1617
b8dd1ef6 1618 if (WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) {
1619 DropField();
1620 if ((resp.arg[0] & 0xff) == 1) {
4be9f36e 1621 PrintAndLogEx(INFO, "Ultralight-C new password: %s", sprint_hex(pwd,16));
b8dd1ef6 1622 return 0;
1623 } else {
4be9f36e 1624 PrintAndLogEx(ERR, "Failed writing at block %d", resp.arg[1] & 0xff);
f168b263 1625 return 1;
1626 }
b8dd1ef6 1627 } else {
1628 DropField();
1629 PrintAndLogEx(ERR, "command execution timeout");
f168b263 1630 return 1;
1631 }
4be9f36e 1632
f168b263 1633 return 0;
1634}
1635
1636//
92690507 1637// Magic UL / UL-C tags - Set UID
f168b263 1638//
4be9f36e 1639static int CmdHF14AMfucSetUid(const char *Cmd){
f168b263 1640
1641 UsbCommand c;
1642 UsbCommand resp;
1643 uint8_t uid[7] = {0x00};
1644 char cmdp = param_getchar(Cmd, 0);
4be9f36e 1645
1646 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
1647 PrintAndLogEx(NORMAL, "Usage: hf mfu setuid <uid (14 hex symbols)>");
1648 PrintAndLogEx(NORMAL, " [uid] - (14 hex symbols)");
1649 PrintAndLogEx(NORMAL, "\nThis only works for Magic Ultralight tags.");
1650 PrintAndLogEx(NORMAL, "");
1651 PrintAndLogEx(NORMAL, "sample: hf mfu setuid 11223344556677");
1652 PrintAndLogEx(NORMAL, "");
f168b263 1653 return 0;
1654 }
4be9f36e 1655
f168b263 1656 if (param_gethex(Cmd, 0, uid, 14)) {
4be9f36e 1657 PrintAndLogEx(WARNING, "UID must include 14 HEX symbols");
f168b263 1658 return 1;
1659 }
1660
4be9f36e 1661 // read block2.
f168b263 1662 c.cmd = CMD_MIFAREU_READBL;
1663 c.arg[0] = 2;
22342f6d 1664 clearCommandBuffer();
f168b263 1665 SendCommand(&c);
1666 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
b8dd1ef6 1667 DropField();
4be9f36e 1668 PrintAndLogEx(WARNING, "Command execute timeout");
f168b263 1669 return 2;
1670 }
4be9f36e 1671
f168b263 1672 // save old block2.
1673 uint8_t oldblock2[4] = {0x00};
1674 memcpy(resp.d.asBytes, oldblock2, 4);
4be9f36e 1675
f168b263 1676 // block 0.
1677 c.cmd = CMD_MIFAREU_WRITEBL;
1678 c.arg[0] = 0;
1679 c.d.asBytes[0] = uid[0];
1680 c.d.asBytes[1] = uid[1];
1681 c.d.asBytes[2] = uid[2];
1682 c.d.asBytes[3] = 0x88 ^ uid[0] ^ uid[1] ^ uid[2];
22342f6d 1683 clearCommandBuffer();
f168b263 1684 SendCommand(&c);
b8dd1ef6 1685 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
1686 DropField();
4be9f36e 1687 PrintAndLogEx(WARNING, "Command execute timeout");
f168b263 1688 return 3;
1689 }
4be9f36e 1690
f168b263 1691 // block 1.
1692 c.arg[0] = 1;
1693 c.d.asBytes[0] = uid[3];
1694 c.d.asBytes[1] = uid[4];
1695 c.d.asBytes[2] = uid[5];
1696 c.d.asBytes[3] = uid[6];
22342f6d 1697 clearCommandBuffer();
f168b263 1698 SendCommand(&c);
b8dd1ef6 1699 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) {
1700 DropField();
4be9f36e 1701 PrintAndLogEx(WARNING, "Command execute timeout");
f168b263 1702 return 4;
1703 }
1704
1705 // block 2.
1706 c.arg[0] = 2;
1707 c.d.asBytes[0] = uid[3] ^ uid[4] ^ uid[5] ^ uid[6];
1708 c.d.asBytes[1] = oldblock2[1];
1709 c.d.asBytes[2] = oldblock2[2];
1710 c.d.asBytes[3] = oldblock2[3];
22342f6d 1711 clearCommandBuffer();
f168b263 1712 SendCommand(&c);
b8dd1ef6 1713 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) {
1714 DropField();
4be9f36e 1715 PrintAndLogEx(WARNING, "Command execute timeout");
f168b263 1716 return 5;
1717 }
4be9f36e 1718
b8dd1ef6 1719 DropField();
f168b263 1720 return 0;
1721}
1722
b8dd1ef6 1723
4be9f36e 1724static int CmdHF14AMfuGenDiverseKeys(const char *Cmd){
79d7bcbb 1725
f168b263 1726 uint8_t iv[8] = { 0x00 };
1727 uint8_t block = 0x07;
79d7bcbb 1728
f168b263 1729 // UL-EV1
1730 //04 57 b6 e2 05 3f 80 UID
1731 //4a f8 4b 19 PWD
1732 uint8_t uid[] = { 0xF4,0xEA, 0x54, 0x8E };
1733 uint8_t mifarekeyA[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1734 uint8_t mifarekeyB[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1735 uint8_t dkeyA[8] = { 0x00 };
1736 uint8_t dkeyB[8] = { 0x00 };
4be9f36e 1737
f168b263 1738 uint8_t masterkey[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
4be9f36e 1739
f168b263 1740 uint8_t mix[8] = { 0x00 };
1741 uint8_t divkey[8] = { 0x00 };
79d7bcbb 1742
f168b263 1743 memcpy(mix, mifarekeyA, 4);
79d7bcbb 1744
f168b263 1745 mix[4] = mifarekeyA[4] ^ uid[0];
1746 mix[5] = mifarekeyA[5] ^ uid[1];
1747 mix[6] = block ^ uid[2];
1748 mix[7] = uid[3];
79d7bcbb 1749
aa0b1c43 1750 mbedtls_des3_context ctx = { {0} };
700d8687 1751 mbedtls_des3_set2key_enc(&ctx, masterkey);
f168b263 1752
700d8687
OM
1753 mbedtls_des3_crypt_cbc(&ctx // des3_context
1754 , MBEDTLS_DES_ENCRYPT // int mode
f168b263 1755 , sizeof(mix) // length
1756 , iv // iv[8]
1757 , mix // input
1758 , divkey // output
1759 );
1760
4be9f36e 1761 PrintAndLogEx(NORMAL, "-- 3DES version");
1762 PrintAndLogEx(NORMAL, "Masterkey :\t %s", sprint_hex(masterkey,sizeof(masterkey)));
1763 PrintAndLogEx(NORMAL, "UID :\t %s", sprint_hex(uid, sizeof(uid)));
1764 PrintAndLogEx(NORMAL, "Block :\t %0d", block);
1765 PrintAndLogEx(NORMAL, "Mifare key :\t %s", sprint_hex(mifarekeyA, sizeof(mifarekeyA)));
1766 PrintAndLogEx(NORMAL, "Message :\t %s", sprint_hex(mix, sizeof(mix)));
1767 PrintAndLogEx(NORMAL, "Diversified key: %s", sprint_hex(divkey+1, 6));
79d7bcbb 1768
f168b263 1769 for (int i=0; i < sizeof(mifarekeyA); ++i){
1770 dkeyA[i] = (mifarekeyA[i] << 1) & 0xff;
1771 dkeyA[6] |= ((mifarekeyA[i] >> 7) & 1) << (i+1);
1772 }
4be9f36e 1773
f168b263 1774 for (int i=0; i < sizeof(mifarekeyB); ++i){
1775 dkeyB[1] |= ((mifarekeyB[i] >> 7) & 1) << (i+1);
1776 dkeyB[2+i] = (mifarekeyB[i] << 1) & 0xff;
1777 }
4be9f36e 1778
f168b263 1779 uint8_t zeros[8] = {0x00};
1780 uint8_t newpwd[8] = {0x00};
1781 uint8_t dmkey[24] = {0x00};
1782 memcpy(dmkey, dkeyA, 8);
1783 memcpy(dmkey+8, dkeyB, 8);
1784 memcpy(dmkey+16, dkeyA, 8);
1785 memset(iv, 0x00, 8);
79d7bcbb 1786
700d8687 1787 mbedtls_des3_set3key_enc(&ctx, dmkey);
f168b263 1788
700d8687
OM
1789 mbedtls_des3_crypt_cbc(&ctx // des3_context
1790 , MBEDTLS_DES_ENCRYPT // int mode
f168b263 1791 , sizeof(newpwd) // length
1792 , iv // iv[8]
1793 , zeros // input
1794 , newpwd // output
1795 );
4be9f36e 1796
1797 PrintAndLogEx(NORMAL, "\n-- DES version");
1798 PrintAndLogEx(NORMAL, "Mifare dkeyA :\t %s", sprint_hex(dkeyA, sizeof(dkeyA)));
1799 PrintAndLogEx(NORMAL, "Mifare dkeyB :\t %s", sprint_hex(dkeyB, sizeof(dkeyB)));
1800 PrintAndLogEx(NORMAL, "Mifare ABA :\t %s", sprint_hex(dmkey, sizeof(dmkey)));
1801 PrintAndLogEx(NORMAL, "Mifare Pwd :\t %s", sprint_hex(newpwd, sizeof(newpwd)));
1802
f168b263 1803 return 0;
1804}
1805
1806// static uint8_t * diversify_key(uint8_t * key){
4be9f36e 1807
f168b263 1808 // for(int i=0; i<16; i++){
1809 // if(i<=6) key[i]^=cuid[i];
1810 // if(i>6) key[i]^=cuid[i%7];
1811 // }
1812 // return key;
1813// }
1814
1815// static void GenerateUIDe( uint8_t *uid, uint8_t len){
1816 // for (int i=0; i<len; ++i){
4be9f36e 1817
f168b263 1818 // }
1819 // return;
1820// }
1821
81740aa5 1822//------------------------------------
1823// Menu Stuff
1824//------------------------------------
4be9f36e 1825static int CmdHelp(const char *Cmd);
1826
81740aa5 1827static command_t CommandTable[] =
1828{
4be9f36e 1829 {"help", CmdHelp, 1, "This help"},
1830 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1831 {"info", CmdHF14AMfUInfo, 0, "Tag information"},
1832 {"dump", CmdHF14AMfUDump, 0, "Dump Ultralight / Ultralight-C / NTAG tag to binary file"},
b8dd1ef6 1833 // {"restore", CmdHF14AMfURestore, 0, "Restore a dump onto a MFU MAGIC tag"},
4be9f36e 1834 {"rdbl", CmdHF14AMfURdBl, 0, "Read block"},
1835 {"wrbl", CmdHF14AMfUWrBl, 0, "Write block"},
1836 {"cauth", CmdHF14AMfucAuth, 0, "Authentication - Ultralight C"},
1837 {"setpwd", CmdHF14AMfucSetPwd, 0, "Set 3des password - Ultralight-C"},
1838 {"setuid", CmdHF14AMfucSetUid, 0, "Set UID - MAGIC tags only"},
1839 {"gen", CmdHF14AMfuGenDiverseKeys, 1, "Generate 3des mifare diversified keys"},
afceaf40 1840 {NULL, NULL, 0, NULL}
81740aa5 1841};
1842
1843int CmdHFMFUltra(const char *Cmd){
44964fd1 1844 (void)WaitForResponseTimeout(CMD_ACK,NULL,100);
afceaf40
MHS
1845 CmdsParse(CommandTable, Cmd);
1846 return 0;
81740aa5 1847}
1848
4be9f36e 1849static int CmdHelp(const char *Cmd){
afceaf40
MHS
1850 CmdsHelp(CommandTable);
1851 return 0;
f168b263 1852}
Impressum, Datenschutz