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 "../common/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
,
43 uint8_t default_3des_keys
[7][16] = {
44 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
45 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
46 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
47 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
48 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
49 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
50 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
53 static int CmdHelp(const char *Cmd
);
55 char* getProductTypeStr( uint8_t id
){
62 sprintf(retStr
, "0x%02X %s", id
, "(Ultralight)");
65 sprintf(retStr
, "0x%02X %s", id
, "(NTAG)");
68 sprintf(retStr
, "0x%02X %s", id
, "(unknown)");
75 The 7 MSBits (=n) code the storage size itself based on 2^n,
76 the LSBit is set to '0' if the size is exactly 2^n
77 and set to '1' if the storage size is between 2^n and 2^(n+1).
79 char* getUlev1CardSizeStr( uint8_t fsize
){
84 uint8_t usize
= 1 << ((fsize
>>1) + 1);
85 uint8_t lsize
= 1 << (fsize
>>1);
89 sprintf(retStr
, "0x%02X (%d - %d bytes)",fsize
, usize
, lsize
);
91 sprintf(retStr
, "0x%02X (%d bytes)", fsize
, lsize
);
95 static void ul_switch_on_field(void) {
96 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
, 0, 0}};
100 static void ul_switch_off_field(void) {
101 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
105 static int ul_send_cmd_raw( uint8_t *cmd
, uint8_t cmdlen
, uint8_t *response
, uint16_t responseLength
) {
106 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
| ISO14A_APPEND_CRC
, cmdlen
, 0}};
107 memcpy(c
.d
.asBytes
, cmd
, cmdlen
);
110 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
112 uint16_t resplen
= (resp
.arg
[0] < responseLength
) ? resp
.arg
[0] : responseLength
;
114 memcpy(response
, resp
.d
.asBytes
, resplen
);
119 static int ul_send_cmd_raw_crc( uint8_t *cmd
, uint8_t cmdlen
, uint8_t *response
, uint16_t responseLength
, bool append_crc
) {
121 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
, cmdlen
, 0}};
123 c
.arg
[0] |= ISO14A_APPEND_CRC
;
125 memcpy(c
.d
.asBytes
, cmd
, cmdlen
);
128 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
130 uint16_t resplen
= (resp
.arg
[0] < responseLength
) ? resp
.arg
[0] : responseLength
;
132 memcpy(response
, resp
.d
.asBytes
, resplen
);
137 static int ul_select( iso14a_card_select_t
*card
){
139 ul_switch_on_field();
142 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
143 ul_switch_off_field();
147 if (resp
.arg
[0] > 0) {
148 memcpy(card
, resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
153 // This read command will at least return 16bytes.
154 static int ul_read( uint8_t page
, uint8_t *response
, uint16_t responseLength
){
156 uint8_t cmd
[] = {ISO14443A_CMD_READBLOCK
, page
};
157 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
159 ul_switch_off_field();
163 static int ulc_requestAuthentication( uint8_t blockNo
, uint8_t *nonce
, uint16_t nonceLength
){
165 uint8_t cmd
[] = {MIFARE_ULC_AUTH_1
, blockNo
};
166 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), nonce
, nonceLength
);
168 ul_switch_off_field();
172 static int ulev1_requestAuthentication( uint8_t *pwd
, uint8_t *pack
, uint16_t packLength
){
174 uint8_t cmd
[] = {MIFARE_ULEV1_AUTH
, pwd
[0], pwd
[1], pwd
[2], pwd
[3]};
175 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), pack
, packLength
);
177 ul_switch_off_field();
181 static int ulev1_getVersion( uint8_t *response
, uint16_t responseLength
){
183 uint8_t cmd
[] = {MIFARE_ULEV1_VERSION
};
184 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
186 ul_switch_off_field();
190 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
192 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
194 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
195 // ul_switch_off_field();
201 static int ulev1_readCounter( uint8_t counter
, uint8_t *response
, uint16_t responseLength
){
203 uint8_t cmd
[] = {MIFARE_ULEV1_READ_CNT
, counter
};
204 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
206 ul_switch_off_field();
210 static int ulev1_readSignature( uint8_t *response
, uint16_t responseLength
){
212 uint8_t cmd
[] = {MIFARE_ULEV1_READSIG
, 0x00};
213 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
215 ul_switch_off_field();
219 static int ul_print_default( uint8_t *data
){
231 PrintAndLog(" UID : %s ", sprint_hex(uid
, 7));
232 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", uid
[0], getTagInfo(uid
[0]) );
235 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
236 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
237 if ( data
[3] == crc0
)
238 PrintAndLog(" BCC0 : 0x%02X - Ok", data
[3]);
240 PrintAndLog(" BCC0 : 0x%02X - crc should be %02x", data
[3], crc0
);
242 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
243 if ( data
[8] == crc1
)
244 PrintAndLog(" BCC1 : 0x%02X - Ok", data
[8]);
246 PrintAndLog(" BCC1 : 0x%02X - crc should be 0x%02X", data
[8], crc1
);
248 PrintAndLog(" Internal : 0x%02X - %s default", data
[9], (data
[9]==0x48)?"":"not" );
249 PrintAndLog(" Lock : %s - %s", sprint_hex(data
+10, 2),printBits( 2, data
+10) );
250 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 12, 4));
255 static int ntag_print_CC(uint8_t *data
) {
256 if(data
[0] != 0xe1) {
257 PrintAndLog("no NDEF message");
258 return -1; // no NDEF message
261 PrintAndLog("Capability Container: %s", sprint_hex(data
,4) );
262 PrintAndLog(" %02X: NDEF Magic Number", data
[0]);
263 PrintAndLog(" %02X: version %d.%d supported by tag", data
[1], (data
[1] & 0xF0) >> 4, data
[1] & 0x0f);
264 PrintAndLog(" %02X: Physical Memory Size: %d bytes", data
[2], (data
[2] + 1) * 8);
265 if ( data
[2] == 0x12 )
266 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 144);
267 else if ( data
[2] == 0x3e )
268 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 496);
269 else if ( data
[2] == 0x6d )
270 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 872);
272 PrintAndLog(" %02X: %s / %s", data
[3],
273 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
274 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
278 static int ul_print_version(uint8_t *data
){
279 PrintAndLog("\n--- UL-EV1 / NTAG Version");
280 PrintAndLog("Raw version bytes: %s", sprint_hex(data
, 8) );
281 PrintAndLog(" Vendor ID : 0x%02X, Manufacturer: %s", data
[1], getTagInfo(data
[1]));
282 PrintAndLog(" Product type : %s" , getProductTypeStr(data
[2]));
283 PrintAndLog(" Product subtype : 0x%02X %s" , data
[3], (data
[3]==1) ?"17 pF":"50pF");
284 PrintAndLog(" Major version : 0x%02X" , data
[4]);
285 PrintAndLog(" Minor version : 0x%02X" , data
[5]);
286 PrintAndLog(" Size : %s", getUlev1CardSizeStr(data
[6]));
287 PrintAndLog(" Protocol type : 0x%02X" , data
[7]);
291 static int ul_print_type(uint16_t tagtype
){
293 PrintAndLog(" TYPE : MIFARE Ultralight (MF0ICU1) %s", (tagtype
& MAGIC
)?"<magic>":"");
294 else if ( tagtype
& UL_C
)
295 PrintAndLog(" TYPE : MIFARE Ultralight C (MF0ULC) %s", (tagtype
& MAGIC
)?"<magic>":"" );
296 else if ( tagtype
& UL_EV1_48
)
297 PrintAndLog(" TYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)");
298 else if ( tagtype
& UL_EV1_128
)
299 PrintAndLog(" TYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101");
300 else if ( tagtype
& NTAG_213
)
301 PrintAndLog(" TYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)");
302 else if ( tagtype
& NTAG_215
)
303 PrintAndLog(" TYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)");
304 else if ( tagtype
& NTAG_216
)
305 PrintAndLog(" TYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)");
307 PrintAndLog(" TYPE : Unknown %04x",tagtype
);
311 static int ulc_print_3deskey( uint8_t *data
){
312 PrintAndLog(" deskey1 [44/0x2C]: %s", sprint_hex(data
,4));
313 PrintAndLog(" deskey1 [45/0x2D]: %s", sprint_hex(data
+4 ,4));
314 PrintAndLog(" deskey2 [46/0x2E]: %s", sprint_hex(data
+8 ,4));
315 PrintAndLog(" deskey2 [47/0x2F]: %s", sprint_hex(data
+12,4));
316 PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data
, 16), 16));
320 static int ulc_print_configuration( uint8_t *data
){
322 PrintAndLog("--- UL-C Configuration");
323 PrintAndLog(" Higher Lockbits [40/0x28]: %s %s", sprint_hex(data
, 4), printBits(2, data
));
324 PrintAndLog(" Counter [41/0x29]: %s %s", sprint_hex(data
+4, 4), printBits(2, data
+4));
326 bool validAuth
= (data
[8] >= 0x03 && data
[8] <= 0x30);
328 PrintAndLog(" Auth0 [42/0x2A]: %s - Pages above %d needs authentication", sprint_hex(data
+8, 4), data
[8] );
331 PrintAndLog(" Auth0 [42/0x2A]: %s - default", sprint_hex(data
+8, 4) );
333 PrintAndLog(" Auth0 [42/0x2A]: %s - auth byte is out-of-range", sprint_hex(data
+8, 4) );
336 PrintAndLog(" Auth1 [43/0x2B]: %s - %s",
337 sprint_hex(data
+12, 4),
338 (data
[12] & 1) ? "write access restricted": "read and write access restricted"
343 static int ulev1_print_configuration( uint8_t *data
){
345 PrintAndLog("\n--- UL-EV1 Configuration");
347 bool strg_mod_en
= (data
[0] & 2);
348 uint8_t authlim
= (data
[4] & 0x07);
349 bool cfglck
= (data
[4] & 0x40);
350 bool prot
= (data
[4] & 0x80);
351 uint8_t vctid
= data
[5];
353 PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data
, 4));
354 PrintAndLog(" - pages above %d needs authentication",data
[3]);
355 PrintAndLog(" - strong modulation mode %s", (strg_mod_en
) ? "enabled":"disabled");
356 PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data
+4, 4) );
358 PrintAndLog(" - Max number of password attempts is unlimited");
360 PrintAndLog(" - Max number of password attempts is %d", authlim
);
361 PrintAndLog(" - user configuration %s", cfglck
? "permanently locked":"writeable");
362 PrintAndLog(" - %s access is protected with password", prot
? "read and write":"write");
363 PrintAndLog(" 0x%02X - Virtual Card Type Identifier is %s default", vctid
, (vctid
==0x05)? "":"not");
364 PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data
+8, 4));
365 PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data
+12, 4));
369 static int ulev1_print_counters(){
370 PrintAndLog("--- UL-EV1 Counters");
371 uint8_t counter
[3] = {0,0,0};
372 for ( uint8_t i
= 0; i
<3; ++i
) {
373 ulev1_readCounter(i
,counter
, sizeof(counter
) );
374 PrintAndLog("Counter [%d] : %s", i
, sprint_hex(counter
,3));
379 uint16_t GetHF14AMfU_Type(void){
381 TagTypeUL_t tagtype
= UNKNOWN
;
382 iso14a_card_select_t card
;
383 uint8_t version
[10] = {0x00};
384 uint8_t nonce1
[11] = {0x00};
385 uint8_t nonce2
[11] = {0x00};
389 status
= ul_select(&card
);
391 PrintAndLog("Error: couldn't select");
395 // Ultralight - ATQA / SAK
396 if ( card
.atqa
[1] != 0x00 && card
.atqa
[0] != 0x44 && card
.sak
!= 0x00 ) {
397 ul_switch_off_field();
401 len
= ulev1_getVersion(version
, sizeof(version
));
405 tagtype
= (UL
| UL_C
);
409 if ( version
[2] == 0x03 && version
[6] == 0x0B )
411 else if ( version
[2] == 0x03 && version
[6] != 0x0B )
412 tagtype
= UL_EV1_128
;
413 else if ( version
[2] == 0x04 && version
[6] == 0x0F )
415 else if ( version
[2] == 0x04 && version
[6] != 0x11 )
417 else if ( version
[2] == 0x04 && version
[6] == 0x13 )
419 else if ( version
[2] == 0x04 )
438 if ((tagtype
& ( UL_C
| UL
))) {
439 // Magic UL-C, by observation,
440 // it seems to have a static nonce response to 0x1A command.
441 status
= ul_select(&card
);
442 status
= ulc_requestAuthentication(0, nonce1
, sizeof(nonce1
));
445 status
= ulc_requestAuthentication(0, nonce2
, sizeof(nonce2
));
447 tagtype
=( !memcmp(nonce1
, nonce2
, 11) ) ? UL_C_MAGIC
: UL_C
;
454 //PrintAndLog("ICE %d", tagtype);
455 //Magic Ultralight test here. It takes present UID, and tries to write it back.
456 if ( (tagtype
& UL
) ){
457 // read 3des key or PWD,
458 // if response bytes == all zeros its a NORMAL tag.
462 ul_switch_off_field();
466 int CmdHF14AMfUInfo(const char *Cmd
){
468 uint8_t data
[16] = {0x00};
469 iso14a_card_select_t card
;
473 PrintAndLog("\n--- Tag Information ---------");
474 PrintAndLog("-------------------------------------------------------------");
476 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
477 if (tagtype
== UL_ERROR
) return -1;
479 ul_print_type(tagtype
);
481 status
= ul_select(&card
);
483 PrintAndLog("Error: couldn't select");
484 ul_switch_off_field();
488 // read pages 0,1,2,4 (should read 4pages)
489 status
= ul_read(0, data
, sizeof(data
));
491 PrintAndLog("Error: tag didn't answer to READ");
492 ul_switch_off_field();
496 ul_print_default(data
);
499 if ((tagtype
& UL_C
)){
501 // read pages 0x28, 0x29, 0x2A, 0x2B
502 uint8_t ulc_conf
[16] = {0x00};
503 status
= ul_read(0x28, ulc_conf
, sizeof(ulc_conf
));
505 PrintAndLog("Error: tag didn't answer to READ");
506 ul_switch_off_field();
510 ulc_print_configuration(ulc_conf
);
512 if ((tagtype
& UL_C_MAGIC
)){
514 uint8_t ulc_deskey
[16] = {0x00};
515 status
= ul_read(0x2C, ulc_deskey
, sizeof(ulc_deskey
));
517 PrintAndLog("Error: tag didn't answer to READ");
518 ul_switch_off_field();
522 ulc_print_3deskey(ulc_deskey
);
526 PrintAndLog("Trying some default 3des keys");
527 for (uint8_t i
= 0; i
< 7; ++i
){
528 key
= default_3des_keys
[i
];
529 if (try3DesAuthentication(key
) == 1){
530 PrintAndLog("Found default 3des key: %s", sprint_hex(key
,16));
537 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
539 ulev1_print_counters();
541 uint8_t startconfigblock
= (tagtype
& UL_EV1_48
) ? 0x10 : 0x25;
542 uint8_t ulev1_conf
[16] = {0x00};
543 status
= ul_read(startconfigblock
, ulev1_conf
, sizeof(ulev1_conf
));
545 PrintAndLog("Error: tag didn't answer to READ");
546 ul_switch_off_field();
550 ulev1_print_configuration(ulev1_conf
);
552 uint8_t ulev1_signature
[32] = {0x00};
553 status
= ulev1_readSignature( ulev1_signature
, sizeof(ulev1_signature
));
555 PrintAndLog("Error: tag didn't answer to READ SIGNATURE");
556 ul_switch_off_field();
560 PrintAndLog(" ECC Signature : %s", sprint_hex(ulev1_signature
, sizeof(ulev1_signature
)));
561 PrintAndLog(" Verify it with the following 'secp128r1' for Elliptic curve cryptograghy in OpenSSL");
565 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
| NTAG_213
| NTAG_215
| NTAG_216
))) {
567 uint8_t version
[10] = {0x00};
568 status
= ulev1_getVersion(version
, sizeof(version
));
570 PrintAndLog("Error: tag didn't answer to GETVERSION");
571 ul_switch_off_field();
574 ul_print_version(version
);
576 //********** TODO ********************************
577 // --problem, there is a failed pwd tries counter in UL-EV1
578 PrintAndLog("\nTrying some known EV1/NTAG passwords.");
580 uint8_t password
[4] ={0xff,0xff,0xff,0xff};
581 uint8_t pack
[4] = {0,0,0,0};
582 status
= ulev1_requestAuthentication(password
, pack
, sizeof(pack
));
584 PrintAndLog("Error: tag didn't answer to AUTHENTICATE");
585 ul_switch_off_field();
588 PrintAndLog("Found default password: %s",sprint_hex(password
, sizeof(password
)));
589 PrintAndLog("Got PACK : %s", sprint_hex(pack
,sizeof(pack
)));
592 if ((tagtype
& (NTAG_213
| NTAG_215
| NTAG_216
))){
594 PrintAndLog("\n--- NTAG NDEF Message");
595 uint8_t cc
[16] = {0x00};
596 status
= ul_read(2, cc
, sizeof(cc
));
598 PrintAndLog("Error: tag didn't answer to READ");
599 ul_switch_off_field();
605 ul_switch_off_field();
610 // Mifare Ultralight Write Single Block
612 int CmdHF14AMfUWrBl(const char *Cmd
){
613 uint8_t blockNo
= -1;
614 bool chinese_card
= FALSE
;
615 uint8_t bldata
[16] = {0x00};
618 char cmdp
= param_getchar(Cmd
, 0);
619 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
620 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
621 PrintAndLog(" [block number]");
622 PrintAndLog(" [block data] - (8 hex symbols)");
623 PrintAndLog(" [w] - Chinese magic ultralight tag");
625 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
630 blockNo
= param_get8(Cmd
, 0);
632 if (blockNo
> MAX_UL_BLOCKS
){
633 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
637 if (param_gethex(Cmd
, 1, bldata
, 8)) {
638 PrintAndLog("Block data must include 8 HEX symbols");
642 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
648 PrintAndLog("Access Denied");
650 PrintAndLog("--specialblock no:%02x", blockNo
);
651 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
652 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
653 memcpy(d
.d
.asBytes
,bldata
, 4);
655 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
656 uint8_t isOK
= resp
.arg
[0] & 0xff;
657 PrintAndLog("isOk:%02x", isOK
);
659 PrintAndLog("Command execute timeout");
663 PrintAndLog("--block no:%02x", blockNo
);
664 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
665 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
666 memcpy(e
.d
.asBytes
,bldata
, 4);
668 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
669 uint8_t isOK
= resp
.arg
[0] & 0xff;
670 PrintAndLog("isOk:%02x", isOK
);
672 PrintAndLog("Command execute timeout");
679 // Mifare Ultralight Read Single Block
681 int CmdHF14AMfURdBl(const char *Cmd
){
684 uint8_t blockNo
= -1;
685 char cmdp
= param_getchar(Cmd
, 0);
687 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
688 PrintAndLog("Usage: hf mfu rdbl <block number>");
689 PrintAndLog(" sample: hfu mfu rdbl 0");
693 blockNo
= param_get8(Cmd
, 0);
695 if (blockNo
> MAX_UL_BLOCKS
){
696 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
700 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
704 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
705 uint8_t isOK
= resp
.arg
[0] & 0xff;
707 uint8_t *data
= resp
.d
.asBytes
;
708 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
711 PrintAndLog("Failed reading block: (%02x)", isOK
);
714 PrintAndLog("Command execute time-out");
720 int usage_hf_mfu_dump(void)
722 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
723 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
724 PrintAndLog("It autodetects card type.\n");
725 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
726 PrintAndLog(" sample : hf mfu dump");
727 PrintAndLog(" : hf mfu dump n myfile");
731 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
732 // Read and Dump Card Contents, using auto detection of tag size.
734 // TODO: take a password to read UL-C / UL-EV1 tags.
735 int CmdHF14AMfUDump(const char *Cmd
){
738 char filename
[FILE_PATH_SIZE
] = {0x00};
739 char * fnameptr
= filename
;
740 char* str
= "Dumping Ultralight%s%s Card Data...";
741 uint8_t *lockbytes_t
= NULL
;
742 uint8_t lockbytes
[2] = {0x00};
743 uint8_t *lockbytes_t2
= NULL
;
744 uint8_t lockbytes2
[2] = {0x00};
745 bool bit
[16] = {0x00};
746 bool bit2
[16] = {0x00};
747 uint8_t data
[1024] = {0x00};
751 bool tmplockbit
= false;
754 //uint8_t key[16]= {0x00};
759 while(param_getchar(Cmd
, cmdp
) != 0x00)
761 switch(param_getchar(Cmd
, cmdp
))
765 return usage_hf_mfu_dump();
768 dataLen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
772 //memcpy(key, data, 16);
773 key
= SwapEndian64(data
, 16);
774 PrintAndLog("3des key: %s",sprint_hex(key
, 16));
781 fileNlen
= param_getstr(Cmd
, cmdp
+1, filename
);
782 if (!fileNlen
) errors
= true;
783 if (fileNlen
> FILE_PATH_SIZE
-5) fileNlen
= FILE_PATH_SIZE
-5;
787 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
795 if(errors
) return usage_hf_mfu_dump();
797 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
798 if (tagtype
== UL_ERROR
) return -1;
800 if ( tagtype
& UL
) {
802 PrintAndLog(str
,"", (tagtype
& MAGIC
)?" (magic)":"" );
804 else if ( tagtype
& UL_C
) {
806 PrintAndLog(str
,"-C", (tagtype
& MAGIC
)?" (magic)":"" );
808 else if ( tagtype
& UL_EV1_48
) {
810 PrintAndLog(str
," EV1_48","");
812 else if ( tagtype
& UL_EV1_128
) {
814 PrintAndLog(str
," EV1_128","");
817 PrintAndLog("Dumping unknown Ultralight, using default values.");
820 UsbCommand c
= {CMD_MIFAREUC_READCARD
, {0,Pages
}};
823 memcpy(c
.d
.asBytes
, key
, 16);
827 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
828 PrintAndLog("Command execute time-out");
832 uint8_t isOK
= resp
.arg
[0] & 0xff;
834 memcpy(data
, resp
.d
.asBytes
, resp
.arg
[1]);
836 PrintAndLog("Failed reading block: (%02x)", i
);
843 lockbytes_t
= data
+ 8;
844 lockbytes
[0] = lockbytes_t
[2];
845 lockbytes
[1] = lockbytes_t
[3];
846 for(j
= 0; j
< 16; j
++){
847 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
850 // Load bottom lockbytes if available
852 lockbytes_t2
= data
+ (40*4);
853 lockbytes2
[0] = lockbytes_t2
[2];
854 lockbytes2
[1] = lockbytes_t2
[3];
855 for (j
= 0; j
< 16; j
++) {
856 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
862 memcpy(data
+ Pages
*4, key
, 16);
865 for (i
= 0; i
< Pages
; ++i
) {
867 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
871 case 3: tmplockbit
= bit
[4]; break;
872 case 4: tmplockbit
= bit
[3]; break;
873 case 5: tmplockbit
= bit
[2]; break;
874 case 6: tmplockbit
= bit
[1]; break;
875 case 7: tmplockbit
= bit
[0]; break;
876 case 8: tmplockbit
= bit
[15]; break;
877 case 9: tmplockbit
= bit
[14]; break;
878 case 10: tmplockbit
= bit
[13]; break;
879 case 11: tmplockbit
= bit
[12]; break;
880 case 12: tmplockbit
= bit
[11]; break;
881 case 13: tmplockbit
= bit
[10]; break;
882 case 14: tmplockbit
= bit
[9]; break;
883 case 15: tmplockbit
= bit
[8]; break;
887 case 19: tmplockbit
= bit2
[6]; break;
891 case 23: tmplockbit
= bit2
[5]; break;
895 case 27: tmplockbit
= bit2
[4]; break;
899 case 31: tmplockbit
= bit2
[2]; break;
903 case 35: tmplockbit
= bit2
[1]; break;
907 case 39: tmplockbit
= bit2
[0]; break;
908 case 40: tmplockbit
= bit2
[12]; break;
909 case 41: tmplockbit
= bit2
[11]; break;
910 case 42: tmplockbit
= bit2
[10]; break; //auth0
911 case 43: tmplockbit
= bit2
[9]; break; //auth1
914 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
917 // user supplied filename?
919 // UID = data 0-1-2 4-5-6-7 (skips a beat)
920 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
921 data
[0], data
[1], data
[2], data
[4], data
[5], data
[6], data
[7]);
923 sprintf(fnameptr
+ fileNlen
," .bin");
926 if ((fout
= fopen(filename
,"wb")) == NULL
) {
927 PrintAndLog("Could not create file name %s", filename
);
930 fwrite( data
, 1, Pages
*4, fout
);
933 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
937 // Needed to Authenticate to Ultralight C tags
938 void rol (uint8_t *data
, const size_t len
){
939 uint8_t first
= data
[0];
940 for (size_t i
= 0; i
< len
-1; i
++) {
946 //-------------------------------------------------------------------------------
947 // Ultralight C Methods
948 //-------------------------------------------------------------------------------
951 // Ultralight C Authentication Demo {currently uses hard-coded key}
953 int CmdHF14AMfucAuth(const char *Cmd
){
958 char cmdp
= param_getchar(Cmd
, 0);
960 //Change key to user defined one
961 if (cmdp
== 'k' || cmdp
== 'K'){
962 keyNo
= param_get8(Cmd
, 1);
967 if (cmdp
== 'h' || cmdp
== 'H')
971 PrintAndLog("Usage: hf mfu cauth k <key number>");
972 PrintAndLog(" 0 (default): 3DES standard key");
973 PrintAndLog(" 1 : all 0x00 key");
974 PrintAndLog(" 2 : 0x00-0x0F key");
975 PrintAndLog(" 3 : nfc key");
976 PrintAndLog(" 4 : all 0x01 key");
977 PrintAndLog(" 5 : all 0xff key");
978 PrintAndLog(" 6 : 0x00-0xFF key");
979 PrintAndLog("\n sample : hf mfu cauth k");
980 PrintAndLog(" : hf mfu cauth k 3");
984 uint8_t *key
= default_3des_keys
[keyNo
];
985 if (try3DesAuthentication(key
)>0)
986 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 16));
988 PrintAndLog("Authentication failed");
993 int try3DesAuthentication( uint8_t *key
){
998 des3_context ctx
= { 0 };
1000 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
1001 uint8_t random_b
[8] = { 0 };
1002 uint8_t enc_random_b
[8] = { 0 };
1003 uint8_t rnd_ab
[16] = { 0 };
1004 uint8_t iv
[8] = { 0 };
1006 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
1009 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
1010 if ( !(resp
.arg
[0] & 0xff) ) return -2;
1013 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
1015 des3_set2key_dec(&ctx
, key
);
1016 // context, mode, length, IV, input, output
1017 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
1020 memcpy(rnd_ab
,random_a
,8);
1021 memcpy(rnd_ab
+8,random_b
,8);
1023 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
1024 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
1025 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
1026 //PrintAndLog(" A+B :%s", sprint_hex(rnd_ab, 16));
1028 des3_set2key_enc(&ctx
, key
);
1029 // context, mode, length, IV, input, output
1030 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
1033 c
.cmd
= CMD_MIFAREUC_AUTH2
;
1035 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
1038 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
1039 if ( !(resp
.arg
[0] & 0xff)) return -2;
1041 uint8_t enc_resp
[8] = { 0 };
1042 uint8_t resp_random_a
[8] = { 0 };
1043 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
1045 des3_set2key_dec(&ctx
, key
);
1046 // context, mode, length, IV, input, output
1047 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
1049 //PrintAndLog(" enc(A+B) :%s", sprint_hex(rnd_ab, 16));
1050 //PrintAndLog(" enc(RndA') :%s", sprint_hex(enc_resp, 8));
1052 if ( !memcmp(resp_random_a
, random_a
, 8))
1058 A test function to validate that the polarssl-function works the same
1059 was as the openssl-implementation.
1060 Commented out, since it requires openssl
1062 int CmdTestDES(const char * cmd)
1064 uint8_t key[16] = {0x00};
1066 memcpy(key,key3_3des_data,16);
1067 DES_cblock RndA, RndB;
1069 PrintAndLog("----------OpenSSL DES implementation----------");
1071 uint8_t e_RndB[8] = {0x00};
1072 unsigned char RndARndB[16] = {0x00};
1074 DES_cblock iv = { 0 };
1075 DES_key_schedule ks1,ks2;
1076 DES_cblock key1,key2;
1078 memcpy(key,key3_3des_data,16);
1080 memcpy(key2,key+8,8);
1083 DES_set_key((DES_cblock *)key1,&ks1);
1084 DES_set_key((DES_cblock *)key2,&ks2);
1086 DES_random_key(&RndA);
1087 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
1088 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
1089 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1090 // unsigned char *output, long length, DES_key_schedule *ks1,
1091 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1092 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1094 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
1096 memcpy(RndARndB,RndA,8);
1097 memcpy(RndARndB+8,RndB,8);
1098 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
1099 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1100 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
1103 PrintAndLog("----------PolarSSL implementation----------");
1105 uint8_t random_a[8] = { 0 };
1106 uint8_t enc_random_a[8] = { 0 };
1107 uint8_t random_b[8] = { 0 };
1108 uint8_t enc_random_b[8] = { 0 };
1109 uint8_t random_a_and_b[16] = { 0 };
1110 des3_context ctx = { 0 };
1112 memcpy(random_a, RndA,8);
1114 uint8_t output[8] = { 0 };
1115 uint8_t iv[8] = { 0 };
1117 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
1118 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
1120 des3_set2key_dec(&ctx, key);
1122 des3_crypt_cbc(&ctx // des3_context *ctx
1123 , DES_DECRYPT // int mode
1124 , sizeof(random_b) // size_t length
1125 , iv // unsigned char iv[8]
1126 , enc_random_b // const unsigned char *input
1127 , random_b // unsigned char *output
1130 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
1133 memcpy(random_a_and_b ,random_a,8);
1134 memcpy(random_a_and_b+8,random_b,8);
1136 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
1138 des3_set2key_enc(&ctx, key);
1140 des3_crypt_cbc(&ctx // des3_context *ctx
1141 , DES_ENCRYPT // int mode
1142 , sizeof(random_a_and_b) // size_t length
1143 , enc_random_b // unsigned char iv[8]
1144 , random_a_and_b // const unsigned char *input
1145 , random_a_and_b // unsigned char *output
1148 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1155 // Ultralight C Read Single Block
1157 int CmdHF14AMfUCRdBl(const char *Cmd
)
1160 bool hasPwd
= FALSE
;
1161 uint8_t blockNo
= -1;
1162 unsigned char key
[16];
1163 char cmdp
= param_getchar(Cmd
, 0);
1165 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
1166 PrintAndLog("Usage: hf mfu crdbl <block number> <password>");
1168 PrintAndLog("sample: hf mfu crdbl 0");
1169 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
1173 blockNo
= param_get8(Cmd
, 0);
1175 PrintAndLog("Wrong block number");
1179 if (blockNo
> MAX_ULC_BLOCKS
){
1180 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
1185 if ( strlen(Cmd
) > 3){
1186 if (param_gethex(Cmd
, 1, key
, 32)) {
1187 PrintAndLog("Key must include %d HEX symbols", 32);
1195 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
1198 memcpy(c
.d
.asBytes
,key
,16);
1202 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1203 uint8_t isOK
= resp
.arg
[0] & 0xff;
1205 uint8_t *data
= resp
.d
.asBytes
;
1206 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
1209 PrintAndLog("Failed reading block: (%02x)", isOK
);
1212 PrintAndLog("Command execute time-out");
1218 // Mifare Ultralight C Write Single Block
1220 int CmdHF14AMfUCWrBl(const char *Cmd
){
1222 uint8_t blockNo
= -1;
1223 bool chinese_card
= FALSE
;
1224 uint8_t bldata
[16] = {0x00};
1227 char cmdp
= param_getchar(Cmd
, 0);
1229 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
1230 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
1231 PrintAndLog(" [block number]");
1232 PrintAndLog(" [block data] - (8 hex symbols)");
1233 PrintAndLog(" [w] - Chinese magic ultralight tag");
1235 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
1240 blockNo
= param_get8(Cmd
, 0);
1241 if (blockNo
> MAX_ULC_BLOCKS
){
1242 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
1246 if (param_gethex(Cmd
, 1, bldata
, 8)) {
1247 PrintAndLog("Block data must include 8 HEX symbols");
1251 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
1252 chinese_card
= TRUE
;
1255 if ( blockNo
<= 3 ) {
1257 PrintAndLog("Access Denied");
1259 PrintAndLog("--Special block no: 0x%02x", blockNo
);
1260 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1261 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1262 memcpy(d
.d
.asBytes
,bldata
, 4);
1264 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1265 uint8_t isOK
= resp
.arg
[0] & 0xff;
1266 PrintAndLog("isOk:%02x", isOK
);
1268 PrintAndLog("Command execute timeout");
1272 PrintAndLog("--Block no : 0x%02x", blockNo
);
1273 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1274 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1275 memcpy(e
.d
.asBytes
,bldata
, 4);
1277 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1278 uint8_t isOK
= resp
.arg
[0] & 0xff;
1279 PrintAndLog("isOk : %02x", isOK
);
1281 PrintAndLog("Command execute timeout");
1288 // Mifare Ultralight C - Set password
1290 int CmdHF14AMfucSetPwd(const char *Cmd
){
1292 uint8_t pwd
[16] = {0x00};
1294 char cmdp
= param_getchar(Cmd
, 0);
1296 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1297 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
1298 PrintAndLog(" [password] - (32 hex symbols)");
1300 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1305 if (param_gethex(Cmd
, 0, pwd
, 32)) {
1306 PrintAndLog("Password must include 32 HEX symbols");
1310 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
1311 memcpy( c
.d
.asBytes
, pwd
, 16);
1316 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1317 if ( (resp
.arg
[0] & 0xff) == 1)
1318 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
1320 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
1325 PrintAndLog("command execution time out");
1333 // Magic UL / UL-C tags - Set UID
1335 int CmdHF14AMfucSetUid(const char *Cmd
){
1339 uint8_t uid
[7] = {0x00};
1340 char cmdp
= param_getchar(Cmd
, 0);
1342 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1343 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
1344 PrintAndLog(" [uid] - (14 hex symbols)");
1345 PrintAndLog("\nThis only works for Magic Ultralight tags.");
1347 PrintAndLog("sample: hf mfu setuid 11223344556677");
1352 if (param_gethex(Cmd
, 0, uid
, 14)) {
1353 PrintAndLog("UID must include 14 HEX symbols");
1358 c
.cmd
= CMD_MIFAREU_READBL
;
1361 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1362 PrintAndLog("Command execute timeout");
1367 uint8_t oldblock2
[4] = {0x00};
1368 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
1371 c
.cmd
= CMD_MIFAREU_WRITEBL
;
1373 c
.d
.asBytes
[0] = uid
[0];
1374 c
.d
.asBytes
[1] = uid
[1];
1375 c
.d
.asBytes
[2] = uid
[2];
1376 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
1378 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1379 PrintAndLog("Command execute timeout");
1385 c
.d
.asBytes
[0] = uid
[3];
1386 c
.d
.asBytes
[1] = uid
[4];
1387 c
.d
.asBytes
[2] = uid
[5];
1388 c
.d
.asBytes
[3] = uid
[6];
1390 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1391 PrintAndLog("Command execute timeout");
1397 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
1398 c
.d
.asBytes
[1] = oldblock2
[1];
1399 c
.d
.asBytes
[2] = oldblock2
[2];
1400 c
.d
.asBytes
[3] = oldblock2
[3];
1402 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1403 PrintAndLog("Command execute timeout");
1410 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
1412 uint8_t iv
[8] = { 0x00 };
1413 uint8_t block
= 0x07;
1416 //04 57 b6 e2 05 3f 80 UID
1418 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
1419 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1420 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1421 uint8_t dkeyA
[8] = { 0x00 };
1422 uint8_t dkeyB
[8] = { 0x00 };
1424 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1426 uint8_t mix
[8] = { 0x00 };
1427 uint8_t divkey
[8] = { 0x00 };
1429 memcpy(mix
, mifarekeyA
, 4);
1431 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1432 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1433 mix
[6] = block
^ uid
[2];
1436 des3_context ctx
= { 0x00 };
1437 des3_set2key_enc(&ctx
, masterkey
);
1439 des3_crypt_cbc(&ctx
// des3_context
1440 , DES_ENCRYPT
// int mode
1441 , sizeof(mix
) // length
1447 PrintAndLog("3DES version");
1448 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
1449 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
1450 PrintAndLog("Sector :\t %0d", block
);
1451 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
1452 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
1453 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
1455 PrintAndLog("\n DES version");
1457 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
1458 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
1459 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
1462 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
1463 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1464 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1467 uint8_t zeros
[8] = {0x00};
1468 uint8_t newpwd
[8] = {0x00};
1469 uint8_t dmkey
[24] = {0x00};
1470 memcpy(dmkey
, dkeyA
, 8);
1471 memcpy(dmkey
+8, dkeyB
, 8);
1472 memcpy(dmkey
+16, dkeyA
, 8);
1473 memset(iv
, 0x00, 8);
1475 des3_set3key_enc(&ctx
, dmkey
);
1477 des3_crypt_cbc(&ctx
// des3_context
1478 , DES_ENCRYPT
// int mode
1479 , sizeof(newpwd
) // length
1485 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1486 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1487 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1488 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1493 // static uint8_t * diversify_key(uint8_t * key){
1495 // for(int i=0; i<16; i++){
1496 // if(i<=6) key[i]^=cuid[i];
1497 // if(i>6) key[i]^=cuid[i%7];
1502 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1503 // for (int i=0; i<len; ++i){
1509 static command_t CommandTable
[] =
1511 {"help", CmdHelp
, 1, "This help"},
1512 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1513 {"info", CmdHF14AMfUInfo
, 0, "Tag information"},
1514 {"dump", CmdHF14AMfUDump
, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1515 {"rdbl", CmdHF14AMfURdBl
, 0, "Read block - Ultralight"},
1516 {"wrbl", CmdHF14AMfUWrBl
, 0, "Write block - Ultralight"},
1517 {"crdbl", CmdHF14AMfUCRdBl
, 0, "Read block - Ultralight C"},
1518 {"cwrbl", CmdHF14AMfUCWrBl
, 0, "Write block - Ultralight C"},
1519 {"cauth", CmdHF14AMfucAuth
, 0, "Authentication - Ultralight C"},
1520 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password - Ultralight-C"},
1521 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID - MAGIC tags only"},
1522 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1523 {NULL
, NULL
, 0, NULL
}
1526 int CmdHFMFUltra(const char *Cmd
){
1527 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1528 CmdsParse(CommandTable
, Cmd
);
1532 int CmdHelp(const char *Cmd
){
1533 CmdsHelp(CommandTable
);