1 //-----------------------------------------------------------------------------
2 // Ultralight Code (c) 2013,2014 Midnitesnake & Andy Davies of Pentura
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
7 //-----------------------------------------------------------------------------
8 // High frequency MIFARE ULTRALIGHT (C) commands
9 //-----------------------------------------------------------------------------
19 #include "mbedtls/des.h"
21 #include "cmdhf14a.h" // DropField()
24 #include "protocols.h"
27 typedef enum TAGTYPE_UL
{
32 UL_EV1_128
= 0x000008,
43 MY_D_MOVE_NFC
= 0x004000,
44 MY_D_MOVE_LEAN
= 0x008000,
45 NTAG_I2C_1K
= 0x010000,
46 NTAG_I2C_2K
= 0x020000,
49 UL_MAGIC
= UL
| MAGIC
,
50 UL_C_MAGIC
= UL_C
| MAGIC
,
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
68 #define PUBLIC_ECDA_KEYLEN 33
69 static 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,
77 #define KEYS_3DES_COUNT 7
78 static 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
88 #define KEYS_PWD_COUNT 6
89 static uint8_t default_pwd_pack
[KEYS_PWD_COUNT
][4] = {
90 {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default
91 {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed)
92 {0x33,0x6B,0xA1,0x19}, // PACK 0x9c,0x2d -- italian bus (sniffed)
93 {0xFF,0x90,0x6C,0xB2}, // PACK 0x12,0x9e -- italian bus (sniffed)
94 {0x46,0x1c,0xA3,0x19}, // PACK 0xE9,0x5A -- italian bus (sniffed)
95 {0x35,0x1C,0xD0,0x19}, // PACK 0x9A,0x5a -- italian bus (sniffed)
98 #define MAX_UL_TYPES 18
99 static 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
};
102 static 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
};
107 // get version nxp product type
108 static char *getProductTypeStr( uint8_t id
){
114 case 3: sprintf(retStr
, "%02X, Ultralight", id
); break;
115 case 4: sprintf(retStr
, "%02X, NTAG", id
); break;
116 default: sprintf(retStr
, "%02X, unknown", id
); break;
122 The 7 MSBits (=n) code the storage size itself based on 2^n,
123 the LSBit is set to '0' if the size is exactly 2^n
124 and set to '1' if the storage size is between 2^n and 2^(n+1).
126 char *getUlev1CardSizeStr( uint8_t fsize
){
130 memset(buf
, 0, sizeof(buf
));
132 uint16_t usize
= 1 << ((fsize
>>1) + 1);
133 uint16_t lsize
= 1 << (fsize
>>1);
137 sprintf(retStr
, "%02X, (%u <-> %u bytes)",fsize
, usize
, lsize
);
139 sprintf(retStr
, "%02X, (%u bytes)", fsize
, lsize
);
143 static void ul_switch_on_field(void) {
144 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
| ISO14A_NO_RATS
, 0, 0}};
145 clearCommandBuffer();
149 static 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
);
152 clearCommandBuffer();
155 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
156 if (!resp
.arg
[0] && responseLength
) return -1;
158 uint16_t resplen
= (resp
.arg
[0] < responseLength
) ? resp
.arg
[0] : responseLength
;
159 memcpy(response
, resp
.d
.asBytes
, resplen
);
163 static 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}};
166 c.arg[0] |= ISO14A_APPEND_CRC;
168 memcpy(c.d.asBytes, cmd, cmdlen);
169 clearCommandBuffer();
172 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
173 if (!resp.arg[0] && responseLength) return -1;
175 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
176 memcpy(response, resp.d.asBytes, resplen);
180 static int ul_select( iso14a_card_select_t
*card
){
182 ul_switch_on_field();
186 ans
= WaitForResponseTimeout(CMD_ACK
, &resp
, 1500);
187 if (!ans
|| resp
.arg
[0] < 1) {
188 PrintAndLogEx(WARNING
, "iso14443a card select failed");
193 memcpy(card
, resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
197 // This read command will at least return 16bytes.
198 static int ul_read( uint8_t page
, uint8_t *response
, uint16_t responseLength
){
200 uint8_t cmd
[] = {ISO14443A_CMD_READBLOCK
, page
};
201 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
205 static int ul_comp_write( uint8_t page
, uint8_t *data
, uint8_t datalen
){
208 memset(cmd
, 0x00, sizeof(cmd
));
209 datalen
= ( datalen
> 16) ? 16 : datalen
;
211 cmd
[0] = ISO14443A_CMD_WRITEBLOCK
;
213 memcpy(cmd
+2, data
, datalen
);
215 uint8_t response
[1] = {0xff};
216 ul_send_cmd_raw(cmd
, 2+datalen
, response
, sizeof(response
));
218 if ( response
[0] == 0x0a ) return 0;
223 static int ulc_requestAuthentication( uint8_t *nonce
, uint16_t nonceLength
){
225 uint8_t cmd
[] = {MIFARE_ULC_AUTH_1
, 0x00};
226 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), nonce
, nonceLength
);
230 static int ulc_authentication( uint8_t *key
, bool switch_off_field
){
232 UsbCommand c
= {CMD_MIFAREUC_AUTH
, {switch_off_field
}};
233 memcpy(c
.d
.asBytes
, key
, 16);
234 clearCommandBuffer();
237 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return 0;
238 if ( resp
.arg
[0] == 1 ) return 1;
243 static int ulev1_requestAuthentication( uint8_t *pwd
, uint8_t *pack
, uint16_t packLength
){
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
);
250 static 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)) {
254 PrintAndLogEx(WARNING
, "Authentication Failed UL-C");
258 if ( !ul_select(card
) ) return 0;
261 if (ulev1_requestAuthentication(authenticationkey
, pack
, packSize
) < 1) {
263 PrintAndLogEx(WARNING
, "Authentication Failed UL-EV1/NTAG");
271 static int ulev1_getVersion( uint8_t *response
, uint16_t responseLength
){
273 uint8_t cmd
[] = {MIFARE_ULEV1_VERSION
};
274 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
278 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
280 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
282 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
288 static int ulev1_readCounter( uint8_t counter
, uint8_t *response
, uint16_t responseLength
){
290 uint8_t cmd
[] = {MIFARE_ULEV1_READ_CNT
, counter
};
291 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
295 static int ulev1_readTearing( uint8_t counter
, uint8_t *response
, uint16_t responseLength
){
297 uint8_t cmd
[] = {MIFARE_ULEV1_CHECKTEAR
, counter
};
298 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
302 static int ulev1_readSignature( uint8_t *response
, uint16_t responseLength
){
304 uint8_t cmd
[] = {MIFARE_ULEV1_READSIG
, 0x00};
305 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
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:
314 // UL responds with 00, fudan doesn't respond
317 // UL doesn't respond, fudan responds with 00
319 // send 300000 + crc (read with extra byte(s))
320 // UL responds with read of page 0, fudan doesn't respond.
322 // make sure field is off before calling this function
323 static int ul_fudan_check( void ){
324 iso14a_card_select_t card
;
325 if ( !ul_select(&card
) )
328 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
, 4, 0}};
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();
335 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return UL_ERROR
;
336 if (resp
.arg
[0] != 1) return UL_ERROR
;
338 return (!resp
.d
.asBytes
[0]) ? FUDAN_UL
: UL
; //if response == 0x00 then Fudan, else Genuine NXP
341 static int ul_print_default( uint8_t *data
){
352 PrintAndLogEx(NORMAL
," UID : %s ", sprint_hex(uid
, 7));
353 PrintAndLogEx(NORMAL
," UID[0] : %02X, %s", uid
[0], getManufacturerName(uid
[0]));
354 if ( uid
[0] == 0x05 && ((uid
[1] & 0xf0) >> 4) == 2 ) { // is infineon and 66RxxP
355 uint8_t chip
= (data
[8] & 0xC7); // 11000111 mask, bit 3,4,5 RFU
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
362 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
363 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
364 if ( data
[3] == crc0
)
365 PrintAndLogEx(NORMAL
, " BCC0 : %02X, Ok", data
[3]);
367 PrintAndLogEx(NORMAL
, " BCC0 : %02X, crc should be %02X", data
[3], crc0
);
369 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
370 if ( data
[8] == crc1
)
371 PrintAndLogEx(NORMAL
, " BCC1 : %02X, Ok", data
[8]);
373 PrintAndLogEx(NORMAL
, " BCC1 : %02X, crc should be %02X", data
[8], crc1
);
375 PrintAndLogEx(NORMAL
, " Internal : %02X, %sdefault", data
[9], (data
[9]==0x48)?"":"not " );
377 PrintAndLogEx(NORMAL
, " Lock : %s (binary %s %s)",
378 sprint_hex(data
+10, 2),
379 printBits(1, data
+10),
380 printBits(1, data
+11)
383 PrintAndLogEx(NORMAL
, "OneTimePad : %s (binary %s %s %s %s)\n",
384 sprint_hex(data
+ 12, 4),
385 printBits(1, data
+12),
386 printBits(1, data
+13),
387 printBits(1, data
+14),
388 printBits(1, data
+15)
394 static int ndef_print_CC(uint8_t *data
) {
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);
404 if ( data
[2] == 0x12 )
405 PrintAndLogEx(NORMAL
, " %02X : NDEF Memory Size: %d bytes", data
[2], 144);
406 else if ( data
[2] == 0x3e )
407 PrintAndLogEx(NORMAL
, " %02X : NDEF Memory Size: %d bytes", data
[2], 496);
408 else if ( data
[2] == 0x6d )
409 PrintAndLogEx(NORMAL
, " %02X : NDEF Memory Size: %d bytes", data
[2], 872);
411 PrintAndLogEx(NORMAL
, " %02X : %s / %s", data
[3],
412 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
413 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
417 int ul_print_type(uint32_t tagtype
, uint8_t spaces
){
420 char *spacer
= spc
+ (10-spaces
);
423 PrintAndLogEx(NORMAL
, "%sTYPE : MIFARE Ultralight (MF0ICU1) %s", spacer
, (tagtype
& MAGIC
) ? "<magic>" : "" );
424 else if ( tagtype
& UL_C
)
425 PrintAndLogEx(NORMAL
, "%sTYPE : MIFARE Ultralight C (MF0ULC) %s", spacer
, (tagtype
& MAGIC
) ? "<magic>" : "" );
426 else if ( tagtype
& UL_EV1_48
)
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
);
430 else if ( tagtype
& NTAG
)
431 PrintAndLogEx(NORMAL
, "%sTYPE : NTAG UNKNOWN", spacer
);
432 else if ( tagtype
& NTAG_203
)
433 PrintAndLogEx(NORMAL
, "%sTYPE : NTAG 203 144bytes (NT2H0301F0DT)", spacer
);
434 else if ( tagtype
& NTAG_210
)
435 PrintAndLogEx(NORMAL
, "%sTYPE : NTAG 210 48bytes (NT2L1011G0DU)", spacer
);
436 else if ( tagtype
& NTAG_212
)
437 PrintAndLogEx(NORMAL
, "%sTYPE : NTAG 212 128bytes (NT2L1211G0DU)", spacer
);
438 else if ( tagtype
& NTAG_213
)
439 PrintAndLogEx(NORMAL
, "%sTYPE : NTAG 213 144bytes (NT2H1311G0DU)", spacer
);
440 else if ( tagtype
& NTAG_215
)
441 PrintAndLogEx(NORMAL
, "%sTYPE : NTAG 215 504bytes (NT2H1511G0DU)", spacer
);
442 else if ( tagtype
& NTAG_216
)
443 PrintAndLogEx(NORMAL
, "%sTYPE : NTAG 216 888bytes (NT2H1611G0DU)", spacer
);
444 else if ( tagtype
& NTAG_I2C_1K
)
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");
448 else if ( tagtype
& MY_D
)
449 PrintAndLogEx(NORMAL
, "%sTYPE : INFINEON my-d\x99 (SLE 66RxxS)", spacer
);
450 else if ( tagtype
& MY_D_NFC
)
451 PrintAndLogEx(NORMAL
, "%sTYPE : INFINEON my-d\x99 NFC (SLE 66RxxP)", spacer
);
452 else if ( tagtype
& MY_D_MOVE
)
453 PrintAndLogEx(NORMAL
, "%sTYPE : INFINEON my-d\x99 move (SLE 66R01P)", spacer
);
454 else if ( tagtype
& MY_D_MOVE_NFC
)
455 PrintAndLogEx(NORMAL
, "%sTYPE : INFINEON my-d\x99 move NFC (SLE 66R01P)", spacer
);
456 else if ( tagtype
& MY_D_MOVE_LEAN
)
457 PrintAndLogEx(NORMAL
, "%sTYPE : INFINEON my-d\x99 move lean (SLE 66R01L)", spacer
);
458 else if ( tagtype
& FUDAN_UL
)
459 PrintAndLogEx(NORMAL
, "%sTYPE : FUDAN Ultralight Compatible (or other compatible) %s", spacer
, (tagtype
& MAGIC
) ? "<magic>" : "" );
461 PrintAndLogEx(NORMAL
, "%sTYPE : Unknown %06x", spacer
, tagtype
);
465 static int ulc_print_3deskey( uint8_t *data
){
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));
474 static int ulc_print_configuration( uint8_t *data
){
476 PrintAndLogEx(NORMAL
, "--- UL-C Configuration");
477 PrintAndLogEx(NORMAL
, " Higher Lockbits [40/0x28] : %s (binary %s %s)",
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),
490 bool validAuth
= (data
[8] >= 0x03 && data
[8] <= 0x30);
492 PrintAndLogEx(NORMAL
, " Auth0 [42/0x2A] : %s page %d/0x%02X and above need authentication", sprint_hex(data
+8, 4), data
[8], data
[8] );
495 PrintAndLogEx(NORMAL
, " Auth0 [42/0x2A] : %s default", sprint_hex(data
+8, 4) );
497 PrintAndLogEx(NORMAL
, " Auth0 [42/0x2A] : %s auth byte is out-of-range", sprint_hex(data
+8, 4) );
500 PrintAndLogEx(NORMAL
, " Auth1 [43/0x2B] : %s %s",
501 sprint_hex(data
+12, 4),
502 (data
[12] & 1) ? "write access restricted": "read and write access restricted"
507 static int ulev1_print_configuration( uint8_t *data
, uint8_t startPage
){
509 PrintAndLogEx(NORMAL
, "\n--- Tag Configuration");
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];
517 PrintAndLogEx(NORMAL
, " cfg0 [%u/0x%02X] : %s", startPage
, startPage
, sprint_hex(data
, 4));
518 if ( data
[3] < 0xff )
519 PrintAndLogEx(NORMAL
, " - page %d and above need authentication",data
[3]);
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) );
525 PrintAndLogEx(NORMAL
, " - Unlimited password attempts");
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));
537 static int ulev1_print_counters(){
538 PrintAndLogEx(NORMAL
, "--- Tag Counters");
539 uint8_t tear
[1] = {0};
540 uint8_t counter
[3] = {0,0,0};
542 for ( uint8_t i
= 0; i
<3; ++i
) {
543 ulev1_readTearing(i
,tear
,sizeof(tear
));
544 len
= ulev1_readCounter(i
,counter
, sizeof(counter
) );
546 PrintAndLogEx(NORMAL
, " [%0d] : %s", i
, sprint_hex(counter
,3));
547 PrintAndLogEx(NORMAL
, " - %02X tearing %s", tear
[0], ( tear
[0]==0xBD)?"Ok":"failure");
553 static int ulev1_print_signature( uint8_t *data
, uint8_t len
){
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
));
559 //to do: verify if signature is valid
560 //PrintAndLogEx(NORMAL, "IC signature status: %s valid", (iseccvalid() )?"":"not");
564 static int ulev1_print_version(uint8_t *data
){
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]);
578 static 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);
593 status = ulc_requestAuthentication(nonce1, sizeof(nonce1));
595 status = ulc_requestAuthentication(nonce2, sizeof(nonce2));
596 returnValue = ( !memcmp(nonce1, nonce2, 11) ) ? UL_C_MAGIC : UL_C;
604 static int ul_magic_test(){
606 // Magic Ultralight tests
607 // 1) take present UID, and try to write it back. OBSOLETE
608 // 2) make a wrong length write to page0, and see if tag answers with ACK/NACK:
609 iso14a_card_select_t card
;
610 if ( !ul_select(&card
) )
612 int status
= ul_comp_write(0, NULL
, 0);
619 uint32_t GetHF14AMfU_Type(void){
621 TagTypeUL_t tagtype
= UNKNOWN
;
622 iso14a_card_select_t card
;
623 uint8_t version
[10] = {0x00};
627 if (!ul_select(&card
)) return UL_ERROR
;
629 // Ultralight - ATQA / SAK
630 if ( card
.atqa
[1] != 0x00 || card
.atqa
[0] != 0x44 || card
.sak
!= 0x00 ) {
631 PrintAndLogEx(NORMAL
, "Tag is not Ultralight | NTAG | MY-D [ATQA: %02X %02X SAK: %02X]\n", card
.atqa
[1], card
.atqa
[0], card
.sak
);
636 if ( card
.uid
[0] != 0x05) {
638 len
= ulev1_getVersion(version
, sizeof(version
));
644 if ( version
[2] == 0x03 && version
[6] == 0x0B )
646 else if ( version
[2] == 0x03 && version
[6] != 0x0B )
647 tagtype
= UL_EV1_128
;
648 else if ( version
[2] == 0x04 && version
[3] == 0x01 && version
[6] == 0x0B )
650 else if ( version
[2] == 0x04 && version
[3] == 0x01 && version
[6] == 0x0E )
652 else if ( version
[2] == 0x04 && version
[3] == 0x02 && version
[6] == 0x0F )
654 else if ( version
[2] == 0x04 && version
[3] == 0x02 && version
[6] == 0x11 )
656 else if ( version
[2] == 0x04 && version
[3] == 0x02 && version
[6] == 0x13 )
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
;
662 else if ( version
[2] == 0x04 )
667 case 0x01: tagtype
= UL_C
; break;
668 case 0x00: tagtype
= UL
; break;
669 case -1 : tagtype
= (UL
| UL_C
| NTAG_203
); break; // could be UL | UL_C magic tags
670 default : tagtype
= UNKNOWN
; break;
672 // UL vs UL-C vs ntag203 test
673 if (tagtype
& (UL
| UL_C
| NTAG_203
)) {
674 if ( !ul_select(&card
) ) return UL_ERROR
;
676 // do UL_C check first...
677 uint8_t nonce
[11] = {0x00};
678 status
= ulc_requestAuthentication(nonce
, sizeof(nonce
));
683 // need to re-select after authentication error
684 if ( !ul_select(&card
) ) return UL_ERROR
;
686 uint8_t data
[16] = {0x00};
687 // read page 0x26-0x29 (last valid ntag203 page)
688 status
= ul_read(0x26, data
, sizeof(data
));
692 // read page 0x30 (should error if it is a ntag203)
693 status
= ul_read(0x30, data
, sizeof(data
));
704 tagtype
= ul_fudan_check();
709 // Infinition MY-D tests Exam high nibble
710 uint8_t nib
= (card
.uid
[1] & 0xf0) >> 4;
712 // case 0: tagtype = SLE66R35E7; break; //or SLE 66R35E7 - mifare compat... should have different sak/atqa for mf 1k
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
716 case 7: tagtype
= MY_D_MOVE_LEAN
; break; //or SLE 66R01L // 16 pages of 4 bytes
720 tagtype
|= ul_magic_test();
721 if (tagtype
== (UNKNOWN
| MAGIC
)) tagtype
= (UL_MAGIC
);
725 static 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");
743 static int CmdHF14AMfUInfo(const char *Cmd
){
745 uint8_t authlim
= 0xff;
746 uint8_t data
[16] = {0x00};
747 iso14a_card_select_t card
;
750 bool hasAuthKey
= false;
752 bool swapEndian
= false;
755 uint8_t authenticationkey
[16] = {0x00};
756 uint8_t *authkeyptr
= authenticationkey
;
758 uint8_t pack
[4] = {0,0,0,0};
762 while(param_getchar(Cmd
, cmdp
) != 0x00)
764 switch(param_getchar(Cmd
, cmdp
))
768 return usage_hf_mfu_info();
771 dataLen
= param_getstr(Cmd
, cmdp
+1, tempStr
, sizeof(tempStr
));
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
776 PrintAndLogEx(ERR
, "Key has incorrect length\n");
788 PrintAndLogEx(WARNING
, "Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
796 if(errors
) return usage_hf_mfu_info();
798 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
799 if (tagtype
== UL_ERROR
) return -1;
801 PrintAndLogEx(NORMAL
, "\n--- Tag Information ---------");
802 PrintAndLogEx(NORMAL
, "-------------------------------------------------------------");
803 ul_print_type(tagtype
, 6);
806 if (swapEndian
&& hasAuthKey
) authkeyptr
= SwapEndian64(authenticationkey
, dataLen
, (dataLen
== 16) ? 8 : 4 );
808 if (!ul_auth_select( &card
, tagtype
, hasAuthKey
, authkeyptr
, pack
, sizeof(pack
))) return -1;
810 // read pages 0,1,2,3 (should read 4pages)
811 status
= ul_read(0, data
, sizeof(data
));
812 if ( status
== -1 ) {
814 PrintAndLogEx(WARNING
, "Error: tag didn't answer to READ");
816 } else if (status
== 16) {
817 ul_print_default(data
);
818 ndef_print_CC(data
+12);
824 if ((tagtype
& UL_C
)) {
826 // read pages 0x28, 0x29, 0x2A, 0x2B
827 uint8_t ulc_conf
[16] = {0x00};
828 status
= ul_read(0x28, ulc_conf
, sizeof(ulc_conf
));
830 PrintAndLogEx(WARNING
, "Error: tag didn't answer to READ UL-C");
834 if (status
== 16) ulc_print_configuration(ulc_conf
);
837 if ((tagtype
& MAGIC
)) {
839 uint8_t ulc_deskey
[16] = {0x00};
840 status
= ul_read(0x2C, ulc_deskey
, sizeof(ulc_deskey
));
841 if ( status
== -1 ) {
843 PrintAndLogEx(WARNING
, "Error: tag didn't answer to READ magic");
846 if (status
== 16) ulc_print_3deskey(ulc_deskey
);
850 // if we called info with key, just return
851 if ( hasAuthKey
) return 1;
853 // also try to diversify default keys.. look into CmdHF14AMfuGenDiverseKeys
854 PrintAndLogEx(INFO
, "Trying some default 3des keys");
855 for (uint8_t i
= 0; i
< KEYS_3DES_COUNT
; ++i
) {
856 key
= default_3des_keys
[i
];
857 if (ulc_authentication(key
, true)) {
858 PrintAndLogEx(SUCCESS
, "Found default 3des key: ");
860 memcpy(keySwap
, SwapEndian64(key
,16,8), 16);
861 ulc_print_3deskey(keySwap
);
869 // do counters and signature first (don't neet auth)
871 // ul counters are different than ntag counters
872 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
873 if (ulev1_print_counters() != 3) {
874 // failed - re-select
875 if (!ul_auth_select( &card
, tagtype
, hasAuthKey
, authkeyptr
, pack
, sizeof(pack
))) return -1;
879 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
| NTAG_213
| NTAG_215
| NTAG_216
| NTAG_I2C_1K
| NTAG_I2C_2K
))) {
880 uint8_t ulev1_signature
[32] = {0x00};
881 status
= ulev1_readSignature( ulev1_signature
, sizeof(ulev1_signature
));
882 if ( status
== -1 ) {
883 PrintAndLogEx(WARNING
, "Error: tag didn't answer to READ SIGNATURE");
887 if (status
== 32) ulev1_print_signature( ulev1_signature
, sizeof(ulev1_signature
));
890 if (!ul_auth_select( &card
, tagtype
, hasAuthKey
, authkeyptr
, pack
, sizeof(pack
))) return -1;
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
))) {
895 uint8_t version
[10] = {0x00};
896 status
= ulev1_getVersion(version
, sizeof(version
));
897 if ( status
== -1 ) {
898 PrintAndLogEx(WARNING
, "Error: tag didn't answer to GETVERSION");
901 } else if (status
== 10) {
902 ulev1_print_version(version
);
905 if (!ul_auth_select( &card
, tagtype
, hasAuthKey
, authkeyptr
, pack
, sizeof(pack
))) return -1;
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;
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 ) {
918 PrintAndLogEx(WARNING
, "Error: tag didn't answer to READ EV1");
921 } else if (status
== 16) {
922 // save AUTHENTICATION LIMITS for later:
923 authlim
= (ulev1_conf
[4] & 0x07);
924 ulev1_print_configuration(ulev1_conf
, startconfigblock
);
928 // AUTHLIMIT, (number of failed authentications)
930 // 1-7 = limit. No automatic tries then.
931 // hasAuthKey, if we was called with key, skip test.
932 if ( !authlim
&& !hasAuthKey
) {
933 PrintAndLogEx(NORMAL
, "\n--- Known EV1/NTAG passwords.");
935 for (uint8_t i
= 0; i
< KEYS_PWD_COUNT
; ++i
) {
936 key
= default_pwd_pack
[i
];
937 len
= ulev1_requestAuthentication(key
, pack
, sizeof(pack
));
939 PrintAndLogEx(SUCCESS
, "Found a default password: %s || Pack: %02X %02X",sprint_hex(key
, 4), pack
[0], pack
[1]);
942 if (!ul_auth_select( &card
, tagtype
, hasAuthKey
, authkeyptr
, pack
, sizeof(pack
))) return -1;
945 if (len
< 1) PrintAndLogEx(WARNING
, "password not known");
950 if (locked
) PrintAndLogEx(FAILED
, "\nTag appears to be locked, try using the key to get more info");
951 PrintAndLogEx(NORMAL
, "");
956 // Write Single Block
958 static 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");
972 static int CmdHF14AMfUWrBl(const char *Cmd
){
976 bool hasAuthKey
= false;
977 bool hasPwdKey
= false;
978 bool swapEndian
= false;
982 uint8_t blockdata
[20] = {0x00};
983 uint8_t data
[16] = {0x00};
984 uint8_t authenticationkey
[16] = {0x00};
985 uint8_t *authKeyPtr
= authenticationkey
;
987 while(param_getchar(Cmd
, cmdp
) != 0x00)
989 switch(param_getchar(Cmd
, cmdp
))
993 return usage_hf_mfu_wrbl();
997 keylen
= param_gethex(Cmd
, cmdp
+1, data
, 8);
999 memcpy(authenticationkey
, data
, 4);
1005 keylen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
1007 memcpy(authenticationkey
, data
, 16);
1012 PrintAndLogEx(ERR
, "Key has incorrect length\n");
1017 blockNo
= param_get8(Cmd
, cmdp
+1);
1019 PrintAndLogEx(ERR
, "Wrong block number");
1031 if ( param_gethex(Cmd
, cmdp
+1, blockdata
, 8) ) {
1032 PrintAndLogEx(ERR
, "Block data must include 8 HEX symbols");
1039 PrintAndLogEx(ERR
, "Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1044 if(errors
) return usage_hf_mfu_wrbl();
1047 if ( blockNo
== -1 ) return usage_hf_mfu_wrbl();
1048 // starting with getting tagtype
1049 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
1050 if (tagtype
== UL_ERROR
) return -1;
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
];
1057 if (blockNo
> maxblockno
){
1058 PrintAndLogEx(WARNING
, "block number too large. Max block is %u/0x%02X \n", maxblockno
,maxblockno
);
1059 return usage_hf_mfu_wrbl();
1063 if (swapEndian
&& hasAuthKey
) authKeyPtr
= SwapEndian64(authenticationkey
, 16, 8);
1064 if (swapEndian
&& hasPwdKey
) authKeyPtr
= SwapEndian64(authenticationkey
, 4, 4);
1067 PrintAndLogEx(NORMAL
, "Special Block: %0d (0x%02X) [ %s]", blockNo
, blockNo
, sprint_hex(blockdata
, 4));
1069 PrintAndLogEx(NORMAL
, "Block: %0d (0x%02X) [ %s]", blockNo
, blockNo
, sprint_hex(blockdata
, 4));
1072 UsbCommand c
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1073 memcpy(c
.d
.asBytes
,blockdata
,4);
1077 memcpy(c
.d
.asBytes
+4,authKeyPtr
,16);
1079 else if ( hasPwdKey
) {
1081 memcpy(c
.d
.asBytes
+4,authKeyPtr
,4);
1084 clearCommandBuffer();
1087 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1088 uint8_t isOK
= resp
.arg
[0] & 0xff;
1089 PrintAndLogEx(SUCCESS
, "isOk:%02x", isOK
);
1091 PrintAndLogEx(ERR
, "Command execute timeout");
1099 // Read Single Block
1101 static 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");
1115 static int CmdHF14AMfURdBl(const char *Cmd
){
1118 bool errors
= false;
1119 bool hasAuthKey
= false;
1120 bool hasPwdKey
= false;
1121 bool swapEndian
= false;
1124 uint8_t data
[16] = {0x00};
1125 uint8_t authenticationkey
[16] = {0x00};
1126 uint8_t *authKeyPtr
= authenticationkey
;
1128 while(param_getchar(Cmd
, cmdp
) != 0x00)
1130 switch(param_getchar(Cmd
, cmdp
))
1134 return usage_hf_mfu_rdbl();
1137 // EV1/NTAG size key
1138 keylen
= param_gethex(Cmd
, cmdp
+1, data
, 8);
1140 memcpy(authenticationkey
, data
, 4);
1146 keylen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
1148 memcpy(authenticationkey
, data
, 16);
1153 PrintAndLogEx(ERR
, "Key has incorrect length\n");
1158 blockNo
= param_get8(Cmd
, cmdp
+1);
1160 PrintAndLogEx(ERR
, "Wrong block number");
1171 PrintAndLogEx(WARNING
, "Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1176 if(errors
) return usage_hf_mfu_rdbl();
1179 if ( blockNo
== -1 ) return usage_hf_mfu_rdbl();
1180 // start with getting tagtype
1181 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
1182 if (tagtype
== UL_ERROR
) return -1;
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
];
1189 if (blockNo
> maxblockno
){
1190 PrintAndLogEx(WARNING
, "block number to large. Max block is %u/0x%02X \n", maxblockno
,maxblockno
);
1191 return usage_hf_mfu_rdbl();
1195 if (swapEndian
&& hasAuthKey
) authKeyPtr
= SwapEndian64(authenticationkey
, 16, 8);
1196 if (swapEndian
&& hasPwdKey
) authKeyPtr
= SwapEndian64(authenticationkey
, 4, 4);
1199 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
1202 memcpy(c
.d
.asBytes
,authKeyPtr
,16);
1204 else if ( hasPwdKey
) {
1206 memcpy(c
.d
.asBytes
,authKeyPtr
,4);
1209 clearCommandBuffer();
1212 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1213 uint8_t isOK
= resp
.arg
[0] & 0xff;
1215 uint8_t *data
= resp
.d
.asBytes
;
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));
1220 PrintAndLogEx(ERR
, "Failed reading block: (%02x)", isOK
);
1223 PrintAndLogEx(ERR
, "Command execute time-out");
1230 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
1231 // Read and Dump Card Contents, using auto detection of tag size.
1232 static 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");
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");
1253 static int CmdHF14AMfUDump(const char *Cmd
){
1256 char filename
[FILE_PATH_SIZE
] = {0x00};
1257 char *fnameptr
= filename
;
1258 uint8_t *lockbytes_t
= NULL
;
1259 uint8_t lockbytes
[2] = {0x00};
1260 uint8_t *lockbytes_t2
= NULL
;
1261 uint8_t lockbytes2
[2] = {0x00};
1262 bool bit
[16] = {0x00};
1263 bool bit2
[16] = {0x00};
1264 uint8_t data
[1024] = {0x00};
1265 bool hasAuthKey
= false;
1268 bool tmplockbit
= false;
1269 uint8_t dataLen
= 0;
1271 uint8_t authenticationkey
[16] = {0x00};
1272 uint8_t *authKeyPtr
= authenticationkey
;
1273 size_t fileNlen
= 0;
1274 bool errors
= false;
1275 bool swapEndian
= false;
1276 bool manualPages
= false;
1277 uint8_t startPage
= 0;
1279 unsigned char cleanASCII
[4];
1281 while(param_getchar(Cmd
, cmdp
) != 0x00)
1283 switch(param_getchar(Cmd
, cmdp
))
1287 return usage_hf_mfu_dump();
1290 dataLen
= param_getstr(Cmd
, cmdp
+1, tempStr
, sizeof(tempStr
));
1291 if (dataLen
== 32 || dataLen
== 8) { //ul-c or ev1/ntag key length
1292 errors
= param_gethex(tempStr
, 0, authenticationkey
, dataLen
);
1295 PrintAndLogEx(ERR
, "Key has incorrect length\n");
1308 fileNlen
= param_getstr(Cmd
, cmdp
+1, filename
, sizeof(filename
));
1309 if (!fileNlen
) errors
= true;
1310 if (fileNlen
> FILE_PATH_SIZE
-5) fileNlen
= FILE_PATH_SIZE
-5;
1315 startPage
= param_get8(Cmd
, cmdp
+1);
1321 Pages
= param_get8(Cmd
, cmdp
+1);
1326 PrintAndLogEx(WARNING
, "Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
1334 if(errors
) return usage_hf_mfu_dump();
1336 if (swapEndian
&& hasAuthKey
)
1337 authKeyPtr
= SwapEndian64(authenticationkey
, dataLen
, (dataLen
== 16) ? 8 : 4);
1339 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
1340 if (tagtype
== UL_ERROR
) return -1;
1342 if (!manualPages
) //get number of pages to read
1343 for (uint8_t idx
= 0; idx
< MAX_UL_TYPES
; idx
++)
1344 if (tagtype
& UL_TYPES_ARRAY
[idx
])
1345 Pages
= UL_MEMORY_ARRAY
[idx
]+1; //add one as maxblks starts at 0
1347 ul_print_type(tagtype
, 0);
1348 PrintAndLogEx(NORMAL
, "Reading tag memory...");
1349 UsbCommand c
= {CMD_MIFAREU_READCARD
, {startPage
,Pages
}};
1352 c
.arg
[2] = 1; //UL_C auth
1354 c
.arg
[2] = 2; //UL_EV1/NTAG auth
1356 memcpy(c
.d
.asBytes
, authKeyPtr
, dataLen
);
1359 clearCommandBuffer();
1362 if (!WaitForResponseTimeout(CMD_ACK
, &resp
,1500)) {
1363 PrintAndLogEx(ERR
, "Command execute time-out");
1366 if (resp
.arg
[0] != 1) {
1367 PrintAndLogEx(ERR
, "Failed reading block: (%02x)", i
);
1371 uint32_t startindex
= resp
.arg
[2];
1372 uint32_t bufferSize
= resp
.arg
[1];
1373 if (bufferSize
> sizeof(data
)) {
1374 PrintAndLogEx(FAILED
, "Data exceeded Buffer size!");
1375 bufferSize
= sizeof(data
);
1377 GetFromBigBuf(data
, bufferSize
, startindex
, NULL
, -1, false);
1379 Pages
= bufferSize
/4;
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));
1390 // Load bottom lockbytes if available
1391 // TODO -- FIGURE OUT LOCK BYTES FOR TO EV1 and/or NTAG
1392 if ( Pages
== 44 ) {
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));
1401 // add keys to block dump
1404 authKeyPtr
= SwapEndian64(authenticationkey
, dataLen
, (dataLen
== 16) ? 8 : 4);
1406 authKeyPtr
= authenticationkey
;
1409 if (tagtype
& UL_C
){ //add 4 pages
1410 memcpy(data
+ Pages
*4, authKeyPtr
, dataLen
);
1412 } else { // 2nd page from end
1413 memcpy(data
+ (Pages
*4) - 8, authenticationkey
, dataLen
);
1417 PrintAndLogEx(NORMAL
, "\n Block# | Data |lck| Ascii");
1418 PrintAndLogEx(NORMAL
, "---------+-------------+---+------");
1419 for (i
= 0; i
< Pages
; ++i
) {
1421 PrintAndLogEx(NORMAL
, "%3d/0x%02X | %s| | ", i
+startPage
, i
+startPage
, sprint_hex(data
+ i
* 4, 4));
1425 case 3: tmplockbit
= bit
[4]; break;
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;
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;
1441 case 19: tmplockbit
= bit2
[6]; break;
1445 case 23: tmplockbit
= bit2
[5]; break;
1449 case 27: tmplockbit
= bit2
[4]; break;
1453 case 31: tmplockbit
= bit2
[2]; break;
1457 case 35: tmplockbit
= bit2
[1]; break;
1461 case 39: tmplockbit
= bit2
[0]; break;
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
1469 // convert unprintable characters and line breaks to dots
1470 memcpy(cleanASCII
, data
+i
*4, 4);
1471 clean_ascii(cleanASCII
, 4);
1473 PrintAndLogEx(NORMAL
, "%3d/0x%02X | %s| %d | %.4s", i
+startPage
, i
+startPage
, sprint_hex(data
+ i
* 4, 4), tmplockbit
, cleanASCII
);
1475 PrintAndLogEx(NORMAL
, "---------------------------------");
1477 // user supplied filename?
1479 // UID = data 0-1-2 4-5-6-7 (skips a beat)
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]);
1483 sprintf(fnameptr
+ fileNlen
,".bin");
1486 if ((fout
= fopen(filename
,"wb")) == NULL
) {
1487 PrintAndLogEx(NORMAL
, "Could not create file name %s", filename
);
1490 fwrite( data
, 1, Pages
*4, fout
);
1493 PrintAndLogEx(SUCCESS
, "Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
1497 //-------------------------------------------------------------------------------
1498 // Ultralight C Methods
1499 //-------------------------------------------------------------------------------
1502 // Ultralight C Authentication Demo {currently uses hard-coded key}
1504 static int CmdHF14AMfucAuth(const char *Cmd
){
1507 bool errors
= false;
1509 char cmdp
= param_getchar(Cmd
, 0);
1511 //Change key to user defined one
1512 if (cmdp
== 'k' || cmdp
== 'K'){
1513 keyNo
= param_get8(Cmd
, 1);
1514 if(keyNo
> KEYS_3DES_COUNT
-1)
1518 if (cmdp
== 'h' || cmdp
== 'H')
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");
1535 uint8_t *key
= default_3des_keys
[keyNo
];
1536 if (ulc_authentication(key
, true))
1537 PrintAndLogEx(SUCCESS
, "Authentication successful. 3des key: %s",sprint_hex(key
, 16));
1539 PrintAndLogEx(WARNING
, "Authentication failed");
1545 A test function to validate that the polarssl-function works the same
1546 was as the openssl-implementation.
1547 Commented out, since it requires openssl
1549 int CmdTestDES(const char * cmd)
1551 uint8_t key[16] = {0x00};
1553 memcpy(key,key3_3des_data,16);
1554 DES_cblock RndA, RndB;
1556 PrintAndLogEx(NORMAL, "----------OpenSSL DES implementation----------");
1558 uint8_t e_RndB[8] = {0x00};
1559 unsigned char RndARndB[16] = {0x00};
1561 DES_cblock iv = { 0 };
1562 DES_key_schedule ks1,ks2;
1563 DES_cblock key1,key2;
1565 memcpy(key,key3_3des_data,16);
1567 memcpy(key2,key+8,8);
1570 DES_set_key((DES_cblock *)key1,&ks1);
1571 DES_set_key((DES_cblock *)key2,&ks2);
1573 DES_random_key(&RndA);
1574 PrintAndLogEx(NORMAL, " RndA:%s",sprint_hex(RndA, 8));
1575 PrintAndLogEx(NORMAL, " e_RndB:%s",sprint_hex(e_RndB, 8));
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);
1581 PrintAndLogEx(NORMAL, " RndB:%s",sprint_hex(RndB, 8));
1583 memcpy(RndARndB,RndA,8);
1584 memcpy(RndARndB+8,RndB,8);
1585 PrintAndLogEx(NORMAL, " RA+B:%s",sprint_hex(RndARndB, 16));
1586 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1587 PrintAndLogEx(NORMAL, "enc(RA+B):%s",sprint_hex(RndARndB, 16));
1590 PrintAndLogEx(NORMAL, "----------PolarSSL implementation----------");
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 };
1599 memcpy(random_a, RndA,8);
1601 uint8_t output[8] = { 0 };
1602 uint8_t iv[8] = { 0 };
1604 PrintAndLogEx(NORMAL, " RndA :%s",sprint_hex(random_a, 8));
1605 PrintAndLogEx(NORMAL, " e_RndB:%s",sprint_hex(enc_random_b, 8));
1607 des3_set2key_dec(&ctx, key);
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
1617 PrintAndLogEx(NORMAL, " RndB:%s",sprint_hex(random_b, 8));
1620 memcpy(random_a_and_b ,random_a,8);
1621 memcpy(random_a_and_b+8,random_b,8);
1623 PrintAndLogEx(NORMAL, " RA+B:%s",sprint_hex(random_a_and_b, 16));
1625 des3_set2key_enc(&ctx, key);
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
1635 PrintAndLogEx(NORMAL, "enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1642 // Mifare Ultralight C - Set password
1644 static int CmdHF14AMfucSetPwd(const char *Cmd
){
1646 uint8_t pwd
[16] = {0x00};
1648 char cmdp
= param_getchar(Cmd
, 0);
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
, "");
1659 if (param_gethex(Cmd
, 0, pwd
, 32)) {
1660 PrintAndLogEx(WARNING
, "Password must include 32 HEX symbols");
1664 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
1665 memcpy( c
.d
.asBytes
, pwd
, 16);
1666 clearCommandBuffer();
1671 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1672 if ( (resp
.arg
[0] & 0xff) == 1)
1673 PrintAndLogEx(INFO
, "Ultralight-C new password: %s", sprint_hex(pwd
,16));
1675 PrintAndLogEx(ERR
, "Failed writing at block %d", resp
.arg
[1] & 0xff);
1680 PrintAndLogEx(ERR
, "command execution time out");
1688 // Magic UL / UL-C tags - Set UID
1690 static int CmdHF14AMfucSetUid(const char *Cmd
){
1694 uint8_t uid
[7] = {0x00};
1695 char cmdp
= param_getchar(Cmd
, 0);
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
, "");
1707 if (param_gethex(Cmd
, 0, uid
, 14)) {
1708 PrintAndLogEx(WARNING
, "UID must include 14 HEX symbols");
1713 c
.cmd
= CMD_MIFAREU_READBL
;
1715 clearCommandBuffer();
1717 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1718 PrintAndLogEx(WARNING
, "Command execute timeout");
1723 uint8_t oldblock2
[4] = {0x00};
1724 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
1727 c
.cmd
= CMD_MIFAREU_WRITEBL
;
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];
1733 clearCommandBuffer();
1735 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1736 PrintAndLogEx(WARNING
, "Command execute timeout");
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];
1746 clearCommandBuffer();
1748 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1749 PrintAndLogEx(WARNING
, "Command execute timeout");
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];
1759 clearCommandBuffer();
1761 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1762 PrintAndLogEx(WARNING
, "Command execute timeout");
1769 static int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
1771 uint8_t iv
[8] = { 0x00 };
1772 uint8_t block
= 0x07;
1775 //04 57 b6 e2 05 3f 80 UID
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 };
1783 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1785 uint8_t mix
[8] = { 0x00 };
1786 uint8_t divkey
[8] = { 0x00 };
1788 memcpy(mix
, mifarekeyA
, 4);
1790 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1791 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1792 mix
[6] = block
^ uid
[2];
1795 mbedtls_des3_context ctx
= { {0} };
1796 mbedtls_des3_set2key_enc(&ctx
, masterkey
);
1798 mbedtls_des3_crypt_cbc(&ctx
// des3_context
1799 , MBEDTLS_DES_ENCRYPT
// int mode
1800 , sizeof(mix
) // length
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));
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);
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;
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);
1832 mbedtls_des3_set3key_enc(&ctx
, dmkey
);
1834 mbedtls_des3_crypt_cbc(&ctx
// des3_context
1835 , MBEDTLS_DES_ENCRYPT
// int mode
1836 , sizeof(newpwd
) // length
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
)));
1851 // static uint8_t * diversify_key(uint8_t * key){
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];
1860 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1861 // for (int i=0; i<len; ++i){
1867 //------------------------------------
1869 //------------------------------------
1870 static int CmdHelp(const char *Cmd
);
1872 static command_t CommandTable
[] =
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"},
1884 {NULL
, NULL
, 0, NULL
}
1887 int CmdHFMFUltra(const char *Cmd
){
1888 (void)WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1889 CmdsParse(CommandTable
, Cmd
);
1893 static int CmdHelp(const char *Cmd
){
1894 CmdsHelp(CommandTable
);