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 //-----------------------------------------------------------------------------
10 #include "loclass/des.h"
16 #include "protocols.h"
18 #define MAX_UL_BLOCKS 0x0f
19 #define MAX_ULC_BLOCKS 0x2f
20 #define MAX_ULEV1a_BLOCKS 0x0b
21 #define MAX_ULEV1b_BLOCKS 0x20
22 #define MAX_NTAG_213 0x2c
23 #define MAX_NTAG_215 0x86
24 #define MAX_NTAG_216 0xe6
26 typedef enum TAGTYPE_UL
{
37 UL_MAGIC
= UL
| MAGIC
,
38 UL_C_MAGIC
= UL_C
| MAGIC
,
42 uint8_t default_3des_keys
[7][16] = {
43 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
44 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
45 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
46 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
47 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
48 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
49 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
52 static int CmdHelp(const char *Cmd
);
54 char* getProductTypeStr( uint8_t id
){
61 sprintf(retStr
, "0x%02X %s", id
, "(Ultralight)");
64 sprintf(retStr
, "0x%02X %s", id
, "(NTAG)");
67 sprintf(retStr
, "0x%02X %s", id
, "(unknown)");
74 The 7 MSBits (=n) code the storage size itself based on 2^n,
75 the LSBit is set to '0' if the size is exactly 2^n
76 and set to '1' if the storage size is between 2^n and 2^(n+1).
78 char* getUlev1CardSizeStr( uint8_t fsize
){
83 uint8_t usize
= 1 << ((fsize
>>1) + 1);
84 uint8_t lsize
= 1 << (fsize
>>1);
88 sprintf(retStr
, "0x%02X (%u - %u bytes)",fsize
, usize
, lsize
);
90 sprintf(retStr
, "0x%02X (%u bytes)", fsize
, lsize
);
94 static void ul_switch_on_field(void) {
95 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
, 0, 0}};
99 static void ul_switch_off_field(void) {
100 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
104 static int ul_send_cmd_raw( uint8_t *cmd
, uint8_t cmdlen
, uint8_t *response
, uint16_t responseLength
) {
105 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
| ISO14A_APPEND_CRC
, cmdlen
, 0}};
106 memcpy(c
.d
.asBytes
, cmd
, cmdlen
);
109 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
111 uint16_t resplen
= (resp
.arg
[0] < responseLength
) ? resp
.arg
[0] : responseLength
;
112 memcpy(response
, resp
.d
.asBytes
, resplen
);
116 static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) {
117 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}};
119 c.arg[0] |= ISO14A_APPEND_CRC;
121 memcpy(c.d.asBytes, cmd, cmdlen);
124 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
126 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
127 if (resp.arg[0] > 0) {
128 memcpy(response, resp.d.asBytes, resplen);
133 static int ul_select( iso14a_card_select_t
*card
){
135 ul_switch_on_field();
138 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
139 if (resp
.arg
[0] < 1) return -1;
141 memcpy(card
, resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
145 // This read command will at least return 16bytes.
146 static int ul_read( uint8_t page
, uint8_t *response
, uint16_t responseLength
){
148 uint8_t cmd
[] = {ISO14443A_CMD_READBLOCK
, page
};
149 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
151 ul_switch_off_field();
155 static int ulc_requestAuthentication( uint8_t blockNo
, uint8_t *nonce
, uint16_t nonceLength
){
157 uint8_t cmd
[] = {MIFARE_ULC_AUTH_1
, blockNo
};
158 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), nonce
, nonceLength
);
160 ul_switch_off_field();
164 static int ulev1_requestAuthentication( uint8_t *pwd
, uint8_t *pack
, uint16_t packLength
){
166 uint8_t cmd
[] = {MIFARE_ULEV1_AUTH
, pwd
[0], pwd
[1], pwd
[2], pwd
[3]};
167 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), pack
, packLength
);
169 ul_switch_off_field();
173 static int ulev1_getVersion( uint8_t *response
, uint16_t responseLength
){
175 uint8_t cmd
[] = {MIFARE_ULEV1_VERSION
};
176 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
178 ul_switch_off_field();
182 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
184 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
186 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
187 // ul_switch_off_field();
193 static int ulev1_readCounter( uint8_t counter
, uint8_t *response
, uint16_t responseLength
){
195 uint8_t cmd
[] = {MIFARE_ULEV1_READ_CNT
, counter
};
196 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
198 ul_switch_off_field();
202 static int ul_print_default( uint8_t *data
){
214 PrintAndLog(" UID : %s ", sprint_hex(uid
, 7));
215 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", uid
[0], getTagInfo(uid
[0]) );
218 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
219 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
220 if ( data
[3] == crc0
)
221 PrintAndLog(" BCC0 : 0x%02X - Ok", data
[3]);
223 PrintAndLog(" BCC0 : 0x%02X - crc should be %02x", data
[3], crc0
);
225 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
226 if ( data
[8] == crc1
)
227 PrintAndLog(" BCC1 : 0x%02X - Ok", data
[8]);
229 PrintAndLog(" BCC1 : 0x%02X - crc should be 0x%02X", data
[8], crc1
);
231 PrintAndLog(" Internal : 0x%02X - %s default", data
[9], (data
[9]==0x48)?"":"not" );
232 PrintAndLog(" Lock : %s - %s", sprint_hex(data
+10, 2),printBits( 2, data
+10) );
233 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 12, 4));
238 static int ul_print_CC(uint8_t *data
) {
239 if(data
[0] != 0xe1) {
240 PrintAndLog("no NDEF message");
241 return -1; // no NDEF message
244 PrintAndLog("Capability Container: %s", sprint_hex(data
,4) );
245 PrintAndLog(" %02X: NDEF Magic Number", data
[0]);
246 PrintAndLog(" %02X: version %d.%d supported by tag", data
[1], (data
[1] & 0xF0) >> 4, data
[1] & 0x0f);
247 PrintAndLog(" %02X: Physical Memory Size of this tag: %d bytes", data
[2], (data
[2] + 1) * 8);
248 PrintAndLog(" %02X: %s / %s", data
[3],
249 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
250 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
254 static int ul_print_version(uint8_t *data
){
255 PrintAndLog("\n--- UL-EV1 / NTAG Version");
256 PrintAndLog("Raw version bytes: %s", sprint_hex(data
, 8) );
257 PrintAndLog(" Vendor ID : 0x%02X, Manufacturer: %s", data
[1], getTagInfo(data
[1]));
258 PrintAndLog(" Product type : %s" , getProductTypeStr(data
[2]));
259 PrintAndLog(" Product subtype : 0x%02X %s" , data
[3], (data
[3]==1) ?"17 pF":"50pF");
260 PrintAndLog(" Major version : 0x%02X" , data
[4]);
261 PrintAndLog(" Minor version : 0x%02X" , data
[5]);
262 PrintAndLog(" Size : %s", getUlev1CardSizeStr(data
[6]));
263 PrintAndLog(" Protocol type : 0x%02X" , data
[7]);
267 static int ul_print_type(uint16_t tagtype
){
269 PrintAndLog(" TYPE : MIFARE Ultralight (MF0ICU1) %s", (tagtype
& MAGIC
)?"<magic>":"");
270 else if ( tagtype
& UL_C
)
271 PrintAndLog(" TYPE : MIFARE Ultralight C (MF0ULC) %s [%x]", (tagtype
& MAGIC
)?"<magic>":"", tagtype
);
272 else if ( tagtype
& UL_EV1_48
)
273 PrintAndLog(" TYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)");
274 else if ( tagtype
& UL_EV1_128
)
275 PrintAndLog(" TYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)");
276 else if ( tagtype
& NTAG_213
)
277 PrintAndLog(" TYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)");
278 else if ( tagtype
& NTAG_215
)
279 PrintAndLog(" TYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)");
280 else if ( tagtype
& NTAG_216
)
281 PrintAndLog(" TYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)");
283 PrintAndLog(" TYPE : Unknown %04x",tagtype
);
287 static int ulc_print_3deskey( uint8_t *data
){
288 PrintAndLog(" deskey1 [44/0x2C]: %s", sprint_hex(data
,4));
289 PrintAndLog(" deskey1 [45/0x2D]: %s", sprint_hex(data
+4 ,4));
290 PrintAndLog(" deskey2 [46/0x2E]: %s", sprint_hex(data
+8 ,4));
291 PrintAndLog(" deskey2 [47/0x2F]: %s", sprint_hex(data
+12,4));
292 PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data
, 16), 16));
296 static int ulc_print_configuration( uint8_t *data
){
298 PrintAndLog("--- UL-C Configuration");
299 PrintAndLog(" Higher Lockbits [40/0x28]: %s %s", sprint_hex(data
, 4), printBits(2, data
));
300 PrintAndLog(" Counter [41/0x29]: %s %s", sprint_hex(data
+4, 4), printBits(2, data
+4));
302 bool validAuth
= (data
[8] >= 0x03 && data
[8] <= 0x30);
304 PrintAndLog(" Auth0 [42/0x2A]: %s - Pages above %d needs authentication", sprint_hex(data
+8, 4), data
[8] );
307 PrintAndLog(" Auth0 [42/0x2A]: %s - default", sprint_hex(data
+8, 4) );
309 PrintAndLog(" Auth0 [42/0x2A]: %s - auth byte is out-of-range", sprint_hex(data
+8, 4) );
312 PrintAndLog(" Auth1 [43/0x2B]: %s - %s",
313 sprint_hex(data
+12, 4),
314 (data
[12] & 1) ? "write access restricted": "read and write access restricted"
319 static int ulev1_print_configuration( uint8_t *data
){
321 PrintAndLog("\n--- UL-EV1 Configuration");
323 bool strg_mod_en
= (data
[0] & 2);
324 uint8_t authlim
= (data
[4] & 0x07);
325 bool cfglck
= (data
[4] & 0x40);
326 bool prot
= (data
[4] & 0x80);
327 uint8_t vctid
= data
[5];
329 PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data
, 4));
330 PrintAndLog(" - pages above %d needs authentication",data
[3]);
331 PrintAndLog(" - strong modulation mode %s", (strg_mod_en
) ? "enabled":"disabled");
332 PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data
+4, 4) );
334 PrintAndLog(" - Max number of password attempts is unlimited");
336 PrintAndLog(" - Max number of password attempts is %d", authlim
);
337 PrintAndLog(" - user configuration %s", cfglck
? "permanently locked":"writeable");
338 PrintAndLog(" - %s access is protected with password", prot
? "read and write":"write");
339 PrintAndLog(" 0x%02X - Virtual Card Type Identifier is %s default", vctid
, (vctid
==0x05)? "":"not");
340 PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data
+8, 4));
341 PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data
+12, 4));
345 static int ulev1_print_counters(){
346 PrintAndLog("--- UL-EV1 Counters");
347 uint8_t counter
[3] = {0,0,0};
348 for ( uint8_t i
= 0; i
<3; ++i
) {
349 ulev1_readCounter(i
,counter
, sizeof(counter
) );
350 PrintAndLog("Counter [%d] : %s", i
, sprint_hex(counter
,3));
355 uint16_t GetHF14AMfU_Type(void){
357 TagTypeUL_t tagtype
= UNKNOWN
;
358 iso14a_card_select_t card
;
359 uint8_t version
[10] = {0x00};
360 uint8_t nonce1
[11] = {0x00};
361 uint8_t nonce2
[11] = {0x00};
365 status
= ul_select(&card
);
367 PrintAndLog("Error: couldn't select");
368 ul_switch_off_field();
371 // Ultralight - ATQA / SAK
372 if ( card
.atqa
[1] != 0x00 || card
.atqa
[0] != 0x44 || card
.sak
!= 0x00 ) {
373 PrintAndLog ("Tag is not UL or NTAG, ATQA1: %x, ATQA0: %x, SAK: %d", card
.atqa
[1],card
.atqa
[0],card
.sak
);
374 ul_switch_off_field();
378 len
= ulev1_getVersion(version
, sizeof(version
));
379 ul_switch_off_field();
383 tagtype
= (UL
| UL_C
);
387 if ( version
[2] == 0x03 && version
[6] == 0x0B )
389 else if ( version
[2] == 0x03 && version
[6] != 0x0B )
390 tagtype
= UL_EV1_128
;
391 else if ( version
[2] == 0x04 && version
[6] == 0x0F )
393 else if ( version
[2] == 0x04 && version
[6] != 0x11 )
395 else if ( version
[2] == 0x04 && version
[6] == 0x13 )
397 else if ( version
[2] == 0x04 )
416 if ((tagtype
& ( UL_C
| UL
))) {
417 // Magic UL-C, by observation,
418 // it seems to have a static nonce response to 0x1A command.
419 status
= ul_select(&card
);
421 PrintAndLog("Error: couldn't select B");
422 ul_switch_off_field();
425 status
= ulc_requestAuthentication(0, nonce1
, sizeof(nonce1
));
427 status
= ulc_requestAuthentication(0, nonce2
, sizeof(nonce2
));
428 tagtype
=( !memcmp(nonce1
, nonce2
, 11) ) ? UL_C_MAGIC
: UL_C
;
432 ul_switch_off_field();
435 //PrintAndLog("ICE %d", tagtype);
436 //Magic Ultralight test here. It takes present UID, and tries to write it back.
437 if ( (tagtype
& UL
) ){
438 // read 3des key or PWD,
439 // if response bytes == all zeros its a NORMAL tag.
446 int CmdHF14AMfUInfo(const char *Cmd
){
449 uint8_t data
[16] = {0x00};
450 iso14a_card_select_t card
;
455 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
456 if (tagtype
== UL_ERROR
) return -1;
458 PrintAndLog("\n--- Tag Information ---------");
459 PrintAndLog("-------------------------------------------------------------");
460 ul_print_type(tagtype
);
462 status
= ul_select(&card
);
464 PrintAndLog("Error: couldn't select");
465 ul_switch_off_field();
469 // read pages 0,1,2,4 (should read 4pages)
470 status
= ul_read(0, data
, sizeof(data
));
472 PrintAndLog("Error: tag didn't answer to READ A");
473 ul_switch_off_field();
477 ul_print_default(data
);
479 if ((tagtype
& UL_C
)){
481 // read pages 0x28, 0x29, 0x2A, 0x2B
482 uint8_t ulc_conf
[16] = {0x00};
483 status
= ul_read(0x28, ulc_conf
, sizeof(ulc_conf
));
485 PrintAndLog("Error: tag didn't answer to READ - possibly locked");
486 ul_switch_off_field();
490 ulc_print_configuration(ulc_conf
);
492 if ((tagtype
& MAGIC
)){
494 uint8_t ulc_deskey
[16] = {0x00};
495 status
= ul_read(0x2C, ulc_deskey
, sizeof(ulc_deskey
));
497 PrintAndLog("Error: tag didn't answer to READ B");
498 ul_switch_off_field();
502 ulc_print_3deskey(ulc_deskey
);
506 PrintAndLog("Trying some default 3des keys");
507 ul_switch_off_field();
508 for (uint8_t i
= 0; i
< 7; ++i
){
509 key
= default_3des_keys
[i
];
510 if (try3DesAuthentication(key
) == 1){
511 PrintAndLog("Found default 3des key: %s", sprint_hex(key
,16));
518 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
520 ulev1_print_counters();
522 uint8_t startconfigblock
= (tagtype
& UL_EV1_48
) ? 0x10 : 0x24;
523 uint8_t ulev1_conf
[16] = {0x00};
524 status
= ul_read(startconfigblock
, ulev1_conf
, sizeof(ulev1_conf
));
526 PrintAndLog("Error: tag didn't answer to READ C");
527 ul_switch_off_field();
531 ulev1_print_configuration(ulev1_conf
);
534 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
| NTAG_213
| NTAG_215
| NTAG_216
))) {
536 uint8_t version
[10] = {0x00};
537 status
= ulev1_getVersion(version
, sizeof(version
));
539 PrintAndLog("Error: tag didn't answer to GETVERSION");
540 ul_switch_off_field();
543 ul_print_version(version
);
545 //********** TODO ********************************
546 // --problem, there is a failed pwd tries counter in UL-EV1
547 PrintAndLog("\nTrying some known EV1/NTAG passwords.");
549 uint8_t password
[4] ={0xff,0xff,0xff,0xff};
550 uint8_t pack
[4] = {0,0,0,0};
551 status
= ulev1_requestAuthentication(password
, pack
, sizeof(pack
));
553 PrintAndLog("Error: tag didn't answer to AUTHENTICATE");
554 ul_switch_off_field();
557 PrintAndLog("Found default password: %s",sprint_hex(password
, sizeof(password
)));
558 PrintAndLog("Got PACK : %s", sprint_hex(pack
,sizeof(pack
)));
561 if ((tagtype
& (NTAG_213
| NTAG_215
| NTAG_216
))){
563 PrintAndLog("\n--- NTAG NDEF Message");
564 uint8_t cc
[16] = {0x00};
565 status
= ul_read(2, cc
, sizeof(cc
));
567 PrintAndLog("Error: tag didn't answer to READ D");
568 ul_switch_off_field();
574 ul_switch_off_field();
579 // Mifare Ultralight Write Single Block
581 int CmdHF14AMfUWrBl(const char *Cmd
){
582 uint8_t blockNo
= -1;
583 bool chinese_card
= FALSE
;
584 uint8_t bldata
[16] = {0x00};
587 char cmdp
= param_getchar(Cmd
, 0);
588 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
589 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
590 PrintAndLog(" [block number]");
591 PrintAndLog(" [block data] - (8 hex symbols)");
592 PrintAndLog(" [w] - Chinese magic ultralight tag");
594 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
599 blockNo
= param_get8(Cmd
, 0);
601 if (blockNo
> MAX_UL_BLOCKS
){
602 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
606 if (param_gethex(Cmd
, 1, bldata
, 8)) {
607 PrintAndLog("Block data must include 8 HEX symbols");
611 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
617 PrintAndLog("Access Denied");
619 PrintAndLog("--specialblock no:%02x", blockNo
);
620 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
621 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
622 memcpy(d
.d
.asBytes
,bldata
, 4);
624 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
625 uint8_t isOK
= resp
.arg
[0] & 0xff;
626 PrintAndLog("isOk:%02x", isOK
);
628 PrintAndLog("Command execute timeout");
632 PrintAndLog("--block no:%02x", blockNo
);
633 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
634 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
635 memcpy(e
.d
.asBytes
,bldata
, 4);
637 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
638 uint8_t isOK
= resp
.arg
[0] & 0xff;
639 PrintAndLog("isOk:%02x", isOK
);
641 PrintAndLog("Command execute timeout");
648 // Mifare Ultralight Read Single Block
650 int CmdHF14AMfURdBl(const char *Cmd
){
653 uint8_t blockNo
= -1;
654 char cmdp
= param_getchar(Cmd
, 0);
656 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
657 PrintAndLog("Usage: hf mfu rdbl <block number>");
658 PrintAndLog(" sample: hfu mfu rdbl 0");
662 blockNo
= param_get8(Cmd
, 0);
664 if (blockNo
> MAX_UL_BLOCKS
){
665 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
669 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
673 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
674 uint8_t isOK
= resp
.arg
[0] & 0xff;
676 uint8_t *data
= resp
.d
.asBytes
;
677 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
680 PrintAndLog("Failed reading block: (%02x)", isOK
);
683 PrintAndLog("Command execute time-out");
689 int usage_hf_mfu_dump(void)
691 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
692 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
693 PrintAndLog("It autodetects card type.\n");
694 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
695 PrintAndLog(" Options : ");
696 PrintAndLog(" k <key> : Enter key for authentication");
697 PrintAndLog(" n <FN > : Enter filename w/o .bin to save the dump as");
698 PrintAndLog(" s : Swap entered key's endianness for auth");
700 PrintAndLog(" sample : hf mfu dump");
701 PrintAndLog(" : hf mfu dump n myfile");
705 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
706 // Read and Dump Card Contents, using auto detection of tag size.
708 // TODO: take a password to read UL-C / UL-EV1 tags.
709 int CmdHF14AMfUDump(const char *Cmd
){
712 char filename
[FILE_PATH_SIZE
] = {0x00};
713 char *fnameptr
= filename
;
714 char *str
= "Dumping Ultralight%s%s Card Data...";
715 uint8_t *lockbytes_t
= NULL
;
716 uint8_t lockbytes
[2] = {0x00};
717 uint8_t *lockbytes_t2
= NULL
;
718 uint8_t lockbytes2
[2] = {0x00};
719 bool bit
[16] = {0x00};
720 bool bit2
[16] = {0x00};
721 uint8_t data
[1024] = {0x00};
725 bool tmplockbit
= false;
728 uint8_t key
[16] = {0x00};
729 uint8_t *keyPtr
= key
;
732 bool swapEndian
= false;
734 while(param_getchar(Cmd
, cmdp
) != 0x00)
736 switch(param_getchar(Cmd
, cmdp
))
740 return usage_hf_mfu_dump();
743 dataLen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
747 memcpy(key
, data
, 16);
754 fileNlen
= param_getstr(Cmd
, cmdp
+1, filename
);
755 if (!fileNlen
) errors
= true;
756 if (fileNlen
> FILE_PATH_SIZE
-5) fileNlen
= FILE_PATH_SIZE
-5;
763 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
771 if(errors
) return usage_hf_mfu_dump();
774 keyPtr
= SwapEndian64(data
, 16);
776 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
777 if (tagtype
== UL_ERROR
) return -1;
779 if ( tagtype
& UL
) {
781 PrintAndLog(str
,"", (tagtype
& MAGIC
)?" (magic)":"" );
783 else if ( tagtype
& UL_C
) {
785 PrintAndLog(str
,"-C", (tagtype
& MAGIC
)?" (magic)":"" );
787 else if ( tagtype
& UL_EV1_48
) {
789 PrintAndLog(str
," EV1_48","");
791 else if ( tagtype
& UL_EV1_128
) {
793 PrintAndLog(str
," EV1_128","");
796 PrintAndLog("Dumping unknown Ultralight, using default values.");
799 UsbCommand c
= {CMD_MIFAREUC_READCARD
, {0,Pages
}};
802 memcpy(c
.d
.asBytes
, key
, 16);
806 if (!WaitForResponseTimeout(CMD_ACK
, &resp
,1500)) {
807 PrintAndLog("Command execute time-out");
810 PrintAndLog ("%u,%u",resp
.arg
[0],resp
.arg
[1]);
811 uint8_t isOK
= resp
.arg
[0] & 0xff;
813 memcpy(data
, resp
.d
.asBytes
, resp
.arg
[1]);
815 PrintAndLog("Failed reading block: (%02x)", i
);
822 lockbytes_t
= data
+ 8;
823 lockbytes
[0] = lockbytes_t
[2];
824 lockbytes
[1] = lockbytes_t
[3];
825 for(j
= 0; j
< 16; j
++){
826 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
829 // Load bottom lockbytes if available
831 lockbytes_t2
= data
+ (40*4);
832 lockbytes2
[0] = lockbytes_t2
[2];
833 lockbytes2
[1] = lockbytes_t2
[3];
834 for (j
= 0; j
< 16; j
++) {
835 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
841 memcpy(data
+ Pages
*4, key
, 16);
844 for (i
= 0; i
< Pages
; ++i
) {
846 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
850 case 3: tmplockbit
= bit
[4]; break;
851 case 4: tmplockbit
= bit
[3]; break;
852 case 5: tmplockbit
= bit
[2]; break;
853 case 6: tmplockbit
= bit
[1]; break;
854 case 7: tmplockbit
= bit
[0]; break;
855 case 8: tmplockbit
= bit
[15]; break;
856 case 9: tmplockbit
= bit
[14]; break;
857 case 10: tmplockbit
= bit
[13]; break;
858 case 11: tmplockbit
= bit
[12]; break;
859 case 12: tmplockbit
= bit
[11]; break;
860 case 13: tmplockbit
= bit
[10]; break;
861 case 14: tmplockbit
= bit
[9]; break;
862 case 15: tmplockbit
= bit
[8]; break;
866 case 19: tmplockbit
= bit2
[6]; break;
870 case 23: tmplockbit
= bit2
[5]; break;
874 case 27: tmplockbit
= bit2
[4]; break;
878 case 31: tmplockbit
= bit2
[2]; break;
882 case 35: tmplockbit
= bit2
[1]; break;
886 case 39: tmplockbit
= bit2
[0]; break;
887 case 40: tmplockbit
= bit2
[12]; break;
888 case 41: tmplockbit
= bit2
[11]; break;
889 case 42: tmplockbit
= bit2
[10]; break; //auth0
890 case 43: tmplockbit
= bit2
[9]; break; //auth1
893 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
896 // user supplied filename?
898 // UID = data 0-1-2 4-5-6-7 (skips a beat)
899 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
900 data
[0],data
[1], data
[2], data
[4],data
[5],data
[6], data
[7]);
902 sprintf(fnameptr
+ fileNlen
," .bin");
905 if ((fout
= fopen(filename
,"wb")) == NULL
) {
906 PrintAndLog("Could not create file name %s", filename
);
909 fwrite( data
, 1, Pages
*4, fout
);
912 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
916 // Needed to Authenticate to Ultralight C tags
917 void rol (uint8_t *data
, const size_t len
){
918 uint8_t first
= data
[0];
919 for (size_t i
= 0; i
< len
-1; i
++) {
925 //-------------------------------------------------------------------------------
926 // Ultralight C Methods
927 //-------------------------------------------------------------------------------
930 // Ultralight C Authentication Demo {currently uses hard-coded key}
932 int CmdHF14AMfucAuth(const char *Cmd
){
937 char cmdp
= param_getchar(Cmd
, 0);
939 //Change key to user defined one
940 if (cmdp
== 'k' || cmdp
== 'K'){
941 keyNo
= param_get8(Cmd
, 1);
946 if (cmdp
== 'h' || cmdp
== 'H')
950 PrintAndLog("Usage: hf mfu cauth k <key number>");
951 PrintAndLog(" 0 (default): 3DES standard key");
952 PrintAndLog(" 1 : all 0x00 key");
953 PrintAndLog(" 2 : 0x00-0x0F key");
954 PrintAndLog(" 3 : nfc key");
955 PrintAndLog(" 4 : all 0x01 key");
956 PrintAndLog(" 5 : all 0xff key");
957 PrintAndLog(" 6 : 0x00-0xFF key");
958 PrintAndLog("\n sample : hf mfu cauth k");
959 PrintAndLog(" : hf mfu cauth k 3");
963 uint8_t *key
= default_3des_keys
[keyNo
];
964 if (try3DesAuthentication(key
)>0)
965 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 16));
967 PrintAndLog("Authentication failed");
972 int try3DesAuthentication( uint8_t *key
){
977 des3_context ctx
= { 0 };
979 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
980 uint8_t random_b
[8] = { 0 };
981 uint8_t enc_random_b
[8] = { 0 };
982 uint8_t rnd_ab
[16] = { 0 };
983 uint8_t iv
[8] = { 0 };
985 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
988 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
989 if ( !(resp
.arg
[0] & 0xff) ) return -2;
992 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
994 des3_set2key_dec(&ctx
, key
);
995 // context, mode, length, IV, input, output
996 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
999 memcpy(rnd_ab
,random_a
,8);
1000 memcpy(rnd_ab
+8,random_b
,8);
1002 des3_set2key_enc(&ctx
, key
);
1003 // context, mode, length, IV, input, output
1004 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
1007 c
.cmd
= CMD_MIFAREUC_AUTH2
;
1009 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
1012 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
1013 if ( !(resp
.arg
[0] & 0xff)) return -2;
1015 uint8_t enc_resp
[8] = { 0 };
1016 uint8_t resp_random_a
[8] = { 0 };
1017 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
1019 des3_set2key_dec(&ctx
, key
);
1020 // context, mode, length, IV, input, output
1021 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
1023 if ( !memcmp(resp_random_a
, random_a
, 8))
1027 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
1028 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
1029 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
1030 //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16));
1031 //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16));
1032 //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8));
1036 A test function to validate that the polarssl-function works the same
1037 was as the openssl-implementation.
1038 Commented out, since it requires openssl
1040 int CmdTestDES(const char * cmd)
1042 uint8_t key[16] = {0x00};
1044 memcpy(key,key3_3des_data,16);
1045 DES_cblock RndA, RndB;
1047 PrintAndLog("----------OpenSSL DES implementation----------");
1049 uint8_t e_RndB[8] = {0x00};
1050 unsigned char RndARndB[16] = {0x00};
1052 DES_cblock iv = { 0 };
1053 DES_key_schedule ks1,ks2;
1054 DES_cblock key1,key2;
1056 memcpy(key,key3_3des_data,16);
1058 memcpy(key2,key+8,8);
1061 DES_set_key((DES_cblock *)key1,&ks1);
1062 DES_set_key((DES_cblock *)key2,&ks2);
1064 DES_random_key(&RndA);
1065 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
1066 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
1067 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1068 // unsigned char *output, long length, DES_key_schedule *ks1,
1069 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1070 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1072 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
1074 memcpy(RndARndB,RndA,8);
1075 memcpy(RndARndB+8,RndB,8);
1076 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
1077 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1078 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
1081 PrintAndLog("----------PolarSSL implementation----------");
1083 uint8_t random_a[8] = { 0 };
1084 uint8_t enc_random_a[8] = { 0 };
1085 uint8_t random_b[8] = { 0 };
1086 uint8_t enc_random_b[8] = { 0 };
1087 uint8_t random_a_and_b[16] = { 0 };
1088 des3_context ctx = { 0 };
1090 memcpy(random_a, RndA,8);
1092 uint8_t output[8] = { 0 };
1093 uint8_t iv[8] = { 0 };
1095 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
1096 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
1098 des3_set2key_dec(&ctx, key);
1100 des3_crypt_cbc(&ctx // des3_context *ctx
1101 , DES_DECRYPT // int mode
1102 , sizeof(random_b) // size_t length
1103 , iv // unsigned char iv[8]
1104 , enc_random_b // const unsigned char *input
1105 , random_b // unsigned char *output
1108 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
1111 memcpy(random_a_and_b ,random_a,8);
1112 memcpy(random_a_and_b+8,random_b,8);
1114 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
1116 des3_set2key_enc(&ctx, key);
1118 des3_crypt_cbc(&ctx // des3_context *ctx
1119 , DES_ENCRYPT // int mode
1120 , sizeof(random_a_and_b) // size_t length
1121 , enc_random_b // unsigned char iv[8]
1122 , random_a_and_b // const unsigned char *input
1123 , random_a_and_b // unsigned char *output
1126 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1133 // Ultralight C Read Single Block
1135 int CmdHF14AMfUCRdBl(const char *Cmd
)
1138 bool hasPwd
= FALSE
;
1139 uint8_t blockNo
= -1;
1141 char cmdp
= param_getchar(Cmd
, 0);
1143 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
1144 PrintAndLog("Usage: hf mfu crdbl <block number> <key>");
1146 PrintAndLog("sample: hf mfu crdbl 0");
1147 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
1151 blockNo
= param_get8(Cmd
, 0);
1153 PrintAndLog("Wrong block number");
1157 if (blockNo
> MAX_ULC_BLOCKS
){
1158 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
1163 if ( strlen(Cmd
) > 3){
1164 if (param_gethex(Cmd
, 1, key
, 32)) {
1165 PrintAndLog("Key must include %d HEX symbols", 32);
1171 //uint8_t *key2 = SwapEndian64(key, 16);
1174 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
1177 memcpy(c
.d
.asBytes
,key
,16);
1181 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1182 uint8_t isOK
= resp
.arg
[0] & 0xff;
1184 uint8_t *data
= resp
.d
.asBytes
;
1185 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
1188 PrintAndLog("Failed reading block: (%02x)", isOK
);
1191 PrintAndLog("Command execute time-out");
1197 // Mifare Ultralight C Write Single Block
1199 int CmdHF14AMfUCWrBl(const char *Cmd
){
1201 uint8_t blockNo
= -1;
1202 bool chinese_card
= FALSE
;
1203 uint8_t bldata
[16] = {0x00};
1206 char cmdp
= param_getchar(Cmd
, 0);
1208 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
1209 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
1210 PrintAndLog(" [block number]");
1211 PrintAndLog(" [block data] - (8 hex symbols)");
1212 PrintAndLog(" [w] - Chinese magic ultralight tag");
1214 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
1219 blockNo
= param_get8(Cmd
, 0);
1220 if (blockNo
> MAX_ULC_BLOCKS
){
1221 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
1225 if (param_gethex(Cmd
, 1, bldata
, 8)) {
1226 PrintAndLog("Block data must include 8 HEX symbols");
1230 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
1231 chinese_card
= TRUE
;
1234 if ( blockNo
<= 3 ) {
1236 PrintAndLog("Access Denied");
1239 PrintAndLog("--Special block no: 0x%02x", blockNo
);
1240 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1241 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1242 memcpy(d
.d
.asBytes
,bldata
, 4);
1244 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1245 uint8_t isOK
= resp
.arg
[0] & 0xff;
1246 PrintAndLog("isOk:%02x", isOK
);
1248 PrintAndLog("Command execute timeout");
1253 PrintAndLog("--Block no : 0x%02x", blockNo
);
1254 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1255 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1256 memcpy(e
.d
.asBytes
,bldata
, 4);
1258 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1259 uint8_t isOK
= resp
.arg
[0] & 0xff;
1260 PrintAndLog("isOk : %02x", isOK
);
1262 PrintAndLog("Command execute timeout");
1270 // Mifare Ultralight C - Set password
1272 int CmdHF14AMfucSetPwd(const char *Cmd
){
1274 uint8_t pwd
[16] = {0x00};
1276 char cmdp
= param_getchar(Cmd
, 0);
1278 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1279 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
1280 PrintAndLog(" [password] - (32 hex symbols)");
1282 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1287 if (param_gethex(Cmd
, 0, pwd
, 32)) {
1288 PrintAndLog("Password must include 32 HEX symbols");
1292 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
1293 memcpy( c
.d
.asBytes
, pwd
, 16);
1298 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1299 if ( (resp
.arg
[0] & 0xff) == 1)
1300 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
1302 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
1307 PrintAndLog("command execution time out");
1315 // Magic UL / UL-C tags - Set UID
1317 int CmdHF14AMfucSetUid(const char *Cmd
){
1321 uint8_t uid
[7] = {0x00};
1322 char cmdp
= param_getchar(Cmd
, 0);
1324 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1325 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
1326 PrintAndLog(" [uid] - (14 hex symbols)");
1327 PrintAndLog("\nThis only works for Magic Ultralight tags.");
1329 PrintAndLog("sample: hf mfu setuid 11223344556677");
1334 if (param_gethex(Cmd
, 0, uid
, 14)) {
1335 PrintAndLog("UID must include 14 HEX symbols");
1340 c
.cmd
= CMD_MIFAREU_READBL
;
1343 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1344 PrintAndLog("Command execute timeout");
1349 uint8_t oldblock2
[4] = {0x00};
1350 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
1353 c
.cmd
= CMD_MIFAREU_WRITEBL
;
1355 c
.d
.asBytes
[0] = uid
[0];
1356 c
.d
.asBytes
[1] = uid
[1];
1357 c
.d
.asBytes
[2] = uid
[2];
1358 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
1360 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1361 PrintAndLog("Command execute timeout");
1367 c
.d
.asBytes
[0] = uid
[3];
1368 c
.d
.asBytes
[1] = uid
[4];
1369 c
.d
.asBytes
[2] = uid
[5];
1370 c
.d
.asBytes
[3] = uid
[6];
1372 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1373 PrintAndLog("Command execute timeout");
1379 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
1380 c
.d
.asBytes
[1] = oldblock2
[1];
1381 c
.d
.asBytes
[2] = oldblock2
[2];
1382 c
.d
.asBytes
[3] = oldblock2
[3];
1384 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1385 PrintAndLog("Command execute timeout");
1392 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
1394 uint8_t iv
[8] = { 0x00 };
1395 uint8_t block
= 0x07;
1398 //04 57 b6 e2 05 3f 80 UID
1400 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
1401 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1402 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1403 uint8_t dkeyA
[8] = { 0x00 };
1404 uint8_t dkeyB
[8] = { 0x00 };
1406 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1408 uint8_t mix
[8] = { 0x00 };
1409 uint8_t divkey
[8] = { 0x00 };
1411 memcpy(mix
, mifarekeyA
, 4);
1413 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1414 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1415 mix
[6] = block
^ uid
[2];
1418 des3_context ctx
= { 0x00 };
1419 des3_set2key_enc(&ctx
, masterkey
);
1421 des3_crypt_cbc(&ctx
// des3_context
1422 , DES_ENCRYPT
// int mode
1423 , sizeof(mix
) // length
1429 PrintAndLog("3DES version");
1430 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
1431 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
1432 PrintAndLog("Sector :\t %0d", block
);
1433 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
1434 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
1435 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
1437 PrintAndLog("\n DES version");
1439 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
1440 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
1441 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
1444 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
1445 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1446 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1449 uint8_t zeros
[8] = {0x00};
1450 uint8_t newpwd
[8] = {0x00};
1451 uint8_t dmkey
[24] = {0x00};
1452 memcpy(dmkey
, dkeyA
, 8);
1453 memcpy(dmkey
+8, dkeyB
, 8);
1454 memcpy(dmkey
+16, dkeyA
, 8);
1455 memset(iv
, 0x00, 8);
1457 des3_set3key_enc(&ctx
, dmkey
);
1459 des3_crypt_cbc(&ctx
// des3_context
1460 , DES_ENCRYPT
// int mode
1461 , sizeof(newpwd
) // length
1467 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1468 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1469 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1470 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1475 // static uint8_t * diversify_key(uint8_t * key){
1477 // for(int i=0; i<16; i++){
1478 // if(i<=6) key[i]^=cuid[i];
1479 // if(i>6) key[i]^=cuid[i%7];
1484 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1485 // for (int i=0; i<len; ++i){
1491 //------------------------------------
1493 //------------------------------------
1494 static command_t CommandTable
[] =
1496 {"help", CmdHelp
, 1, "This help"},
1497 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1498 {"info", CmdHF14AMfUInfo
, 0, "Tag information"},
1499 {"dump", CmdHF14AMfUDump
, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1500 {"rdbl", CmdHF14AMfURdBl
, 0, "Read block - Ultralight"},
1501 {"wrbl", CmdHF14AMfUWrBl
, 0, "Write block - Ultralight"},
1502 {"crdbl", CmdHF14AMfUCRdBl
, 0, "Read block - Ultralight C"},
1503 {"cwrbl", CmdHF14AMfUCWrBl
, 0, "Write block - Ultralight C"},
1504 {"cauth", CmdHF14AMfucAuth
, 0, "Authentication - Ultralight C"},
1505 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password - Ultralight-C"},
1506 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID - MAGIC tags only"},
1507 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1508 {NULL
, NULL
, 0, NULL
}
1511 int CmdHFMFUltra(const char *Cmd
){
1512 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1513 CmdsParse(CommandTable
, Cmd
);
1517 int CmdHelp(const char *Cmd
){
1518 CmdsHelp(CommandTable
);