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