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
,
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 if (resp
.arg
[0] > 0) {
112 uint16_t resplen
= (resp
.arg
[0] < responseLength
) ? resp
.arg
[0] : responseLength
;
113 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;
131 if (resp.arg[0] > 0) {
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)) return -1;
143 if (resp
.arg
[0] < 0) return -1;
145 memcpy(card
, resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
150 // This read command will at least return 16bytes.
151 static int ul_read( uint8_t page
, uint8_t *response
, uint16_t responseLength
){
153 uint8_t cmd
[] = {ISO14443A_CMD_READBLOCK
, page
};
154 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
156 ul_switch_off_field();
160 static int ulc_requestAuthentication( uint8_t blockNo
, uint8_t *nonce
, uint16_t nonceLength
){
162 uint8_t cmd
[] = {MIFARE_ULC_AUTH_1
, blockNo
};
163 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), nonce
, nonceLength
);
165 ul_switch_off_field();
169 static int ulev1_requestAuthentication( uint8_t *pwd
, uint8_t *pack
, uint16_t packLength
){
171 uint8_t cmd
[] = {MIFARE_ULEV1_AUTH
, pwd
[0], pwd
[1], pwd
[2], pwd
[3]};
172 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), pack
, packLength
);
174 ul_switch_off_field();
178 static int ulev1_getVersion( uint8_t *response
, uint16_t responseLength
){
180 uint8_t cmd
[] = {MIFARE_ULEV1_VERSION
};
181 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
183 ul_switch_off_field();
187 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
189 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
191 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
192 // ul_switch_off_field();
198 static int ulev1_readCounter( uint8_t counter
, uint8_t *response
, uint16_t responseLength
){
200 uint8_t cmd
[] = {MIFARE_ULEV1_READ_CNT
, counter
};
201 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
203 ul_switch_off_field();
207 static int ulev1_readSignature( uint8_t *response
, uint16_t responseLength
){
209 uint8_t cmd
[] = {MIFARE_ULEV1_READSIG
, 0x00};
210 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
212 ul_switch_off_field();
216 static int ul_print_default( uint8_t *data
){
228 PrintAndLog(" UID : %s ", sprint_hex(uid
, 7));
229 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", uid
[0], getTagInfo(uid
[0]) );
232 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
233 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
234 if ( data
[3] == crc0
)
235 PrintAndLog(" BCC0 : 0x%02X - Ok", data
[3]);
237 PrintAndLog(" BCC0 : 0x%02X - crc should be %02x", data
[3], crc0
);
239 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
240 if ( data
[8] == crc1
)
241 PrintAndLog(" BCC1 : 0x%02X - Ok", data
[8]);
243 PrintAndLog(" BCC1 : 0x%02X - crc should be 0x%02X", data
[8], crc1
);
245 PrintAndLog(" Internal : 0x%02X - %s default", data
[9], (data
[9]==0x48)?"":"not" );
246 PrintAndLog(" Lock : %s - %s", sprint_hex(data
+10, 2),printBits( 2, data
+10) );
247 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 12, 4));
252 static int ntag_print_CC(uint8_t *data
) {
253 if(data
[0] != 0xe1) {
254 PrintAndLog("no NDEF message");
255 return -1; // no NDEF message
258 PrintAndLog("Capability Container: %s", sprint_hex(data
,4) );
259 PrintAndLog(" %02X: NDEF Magic Number", data
[0]);
260 PrintAndLog(" %02X: version %d.%d supported by tag", data
[1], (data
[1] & 0xF0) >> 4, data
[1] & 0x0f);
261 PrintAndLog(" %02X: Physical Memory Size: %d bytes", data
[2], (data
[2] + 1) * 8);
262 if ( data
[2] == 0x12 )
263 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 144);
264 else if ( data
[2] == 0x3e )
265 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 496);
266 else if ( data
[2] == 0x6d )
267 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 872);
269 PrintAndLog(" %02X: %s / %s", data
[3],
270 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
271 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
275 static int ul_print_version(uint8_t *data
){
276 PrintAndLog("\n--- UL-EV1 / NTAG Version");
277 PrintAndLog("Raw version bytes: %s", sprint_hex(data
, 8) );
278 PrintAndLog(" Vendor ID : 0x%02X, Manufacturer: %s", data
[1], getTagInfo(data
[1]));
279 PrintAndLog(" Product type : %s" , getProductTypeStr(data
[2]));
280 PrintAndLog(" Product subtype : 0x%02X %s" , data
[3], (data
[3]==1) ?"17 pF":"50pF");
281 PrintAndLog(" Major version : 0x%02X" , data
[4]);
282 PrintAndLog(" Minor version : 0x%02X" , data
[5]);
283 PrintAndLog(" Size : %s", getUlev1CardSizeStr(data
[6]));
284 PrintAndLog(" Protocol type : 0x%02X" , data
[7]);
288 static int ul_print_type(uint16_t tagtype
){
290 PrintAndLog(" TYPE : MIFARE Ultralight (MF0ICU1) %s", (tagtype
& MAGIC
)?"<magic>":"");
291 else if ( tagtype
& UL_C
)
292 PrintAndLog(" TYPE : MIFARE Ultralight C (MF0ULC) %s [%x]", (tagtype
& MAGIC
)?"<magic>":"", tagtype
);
293 else if ( tagtype
& UL_EV1_48
)
294 PrintAndLog(" TYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)");
295 else if ( tagtype
& UL_EV1_128
)
296 PrintAndLog(" TYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)");
297 else if ( tagtype
& NTAG_213
)
298 PrintAndLog(" TYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)");
299 else if ( tagtype
& NTAG_215
)
300 PrintAndLog(" TYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)");
301 else if ( tagtype
& NTAG_216
)
302 PrintAndLog(" TYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)");
304 PrintAndLog(" TYPE : Unknown %04x",tagtype
);
308 static int ulc_print_3deskey( uint8_t *data
){
309 PrintAndLog(" deskey1 [44/0x2C]: %s", sprint_hex(data
,4));
310 PrintAndLog(" deskey1 [45/0x2D]: %s", sprint_hex(data
+4 ,4));
311 PrintAndLog(" deskey2 [46/0x2E]: %s", sprint_hex(data
+8 ,4));
312 PrintAndLog(" deskey2 [47/0x2F]: %s", sprint_hex(data
+12,4));
313 PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data
, 16), 16));
317 static int ulc_print_configuration( uint8_t *data
){
319 PrintAndLog("--- UL-C Configuration");
320 PrintAndLog(" Higher Lockbits [40/0x28]: %s %s", sprint_hex(data
, 4), printBits(2, data
));
321 PrintAndLog(" Counter [41/0x29]: %s %s", sprint_hex(data
+4, 4), printBits(2, data
+4));
323 bool validAuth
= (data
[8] >= 0x03 && data
[8] <= 0x30);
325 PrintAndLog(" Auth0 [42/0x2A]: %s - Pages above %d needs authentication", sprint_hex(data
+8, 4), data
[8] );
328 PrintAndLog(" Auth0 [42/0x2A]: %s - default", sprint_hex(data
+8, 4) );
330 PrintAndLog(" Auth0 [42/0x2A]: %s - auth byte is out-of-range", sprint_hex(data
+8, 4) );
333 PrintAndLog(" Auth1 [43/0x2B]: %s - %s",
334 sprint_hex(data
+12, 4),
335 (data
[12] & 1) ? "write access restricted": "read and write access restricted"
340 static int ulev1_print_configuration( uint8_t *data
){
342 PrintAndLog("\n--- UL-EV1 Configuration");
344 bool strg_mod_en
= (data
[0] & 2);
345 uint8_t authlim
= (data
[4] & 0x07);
346 bool cfglck
= (data
[4] & 0x40);
347 bool prot
= (data
[4] & 0x80);
348 uint8_t vctid
= data
[5];
350 PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data
, 4));
351 PrintAndLog(" - pages above %d needs authentication",data
[3]);
352 PrintAndLog(" - strong modulation mode %s", (strg_mod_en
) ? "enabled":"disabled");
353 PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data
+4, 4) );
355 PrintAndLog(" - Max number of password attempts is unlimited");
357 PrintAndLog(" - Max number of password attempts is %d", authlim
);
358 PrintAndLog(" - user configuration %s", cfglck
? "permanently locked":"writeable");
359 PrintAndLog(" - %s access is protected with password", prot
? "read and write":"write");
360 PrintAndLog(" 0x%02X - Virtual Card Type Identifier is %s default", vctid
, (vctid
==0x05)? "":"not");
361 PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data
+8, 4));
362 PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data
+12, 4));
366 static int ulev1_print_counters(){
367 PrintAndLog("--- UL-EV1 Counters");
368 uint8_t counter
[3] = {0,0,0};
369 for ( uint8_t i
= 0; i
<3; ++i
) {
370 ulev1_readCounter(i
,counter
, sizeof(counter
) );
371 PrintAndLog("Counter [%d] : %s", i
, sprint_hex(counter
,3));
376 static int ulev1_print_signature( uint8_t *data
, uint8_t len
){
378 PrintAndLog("IC signature public key name : NXP NTAG21x 2013");
379 PrintAndLog("IC signature public key value : 04494e1a386d3d3cfe3dc10e5de68a499b1c202db5b132393e89ed19fe5be8bc61");
380 PrintAndLog(" Elliptic curve parameters : secp128r1");
381 PrintAndLog(" Tag ECC Signature : %s", sprint_hex(data
, len
));
382 //to do: verify if signature is valid
383 //PrintAndLog("IC signature status: %s valid", (iseccvalid() )?"":"not");
387 uint16_t GetHF14AMfU_Type(void){
389 TagTypeUL_t tagtype
= UNKNOWN
;
390 iso14a_card_select_t card
;
391 uint8_t version
[10] = {0x00};
392 uint8_t nonce1
[11] = {0x00};
393 uint8_t nonce2
[11] = {0x00};
397 status
= ul_select(&card
);
399 PrintAndLog("Error: couldn't select");
400 ul_switch_off_field();
404 // Ultralight - ATQA / SAK
405 if ( card
.atqa
[1] != 0x00 && card
.atqa
[0] != 0x44 && card
.sak
!= 0x00 ) {
406 ul_switch_off_field();
410 len
= ulev1_getVersion(version
, sizeof(version
));
411 ul_switch_off_field();
415 tagtype
= (UL
| UL_C
);
419 if ( version
[2] == 0x03 && version
[6] == 0x0B )
421 else if ( version
[2] == 0x03 && version
[6] != 0x0B )
422 tagtype
= UL_EV1_128
;
423 else if ( version
[2] == 0x04 && version
[6] == 0x0F )
425 else if ( version
[2] == 0x04 && version
[6] != 0x11 )
427 else if ( version
[2] == 0x04 && version
[6] == 0x13 )
429 else if ( version
[2] == 0x04 )
448 if ((tagtype
& ( UL_C
| UL
))) {
449 // Magic UL-C, by observation,
450 // it seems to have a static nonce response to 0x1A command.
451 status
= ul_select(&card
);
452 status
= ulc_requestAuthentication(0, nonce1
, sizeof(nonce1
));
455 status
= ulc_requestAuthentication(0, nonce2
, sizeof(nonce2
));
457 tagtype
=( !memcmp(nonce1
, nonce2
, 11) ) ? UL_C_MAGIC
: UL_C
;
462 ul_switch_off_field();
465 //PrintAndLog("ICE %d", tagtype);
466 //Magic Ultralight test here. It takes present UID, and tries to write it back.
467 if ( (tagtype
& UL
) ){
468 // read 3des key or PWD,
469 // if response bytes == all zeros its a NORMAL tag.
476 int CmdHF14AMfUInfo(const char *Cmd
){
478 uint8_t data
[16] = {0x00};
479 iso14a_card_select_t card
;
483 PrintAndLog("\n--- Tag Information ---------");
484 PrintAndLog("-------------------------------------------------------------");
486 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
487 if (tagtype
== UL_ERROR
) return -1;
489 ul_print_type(tagtype
);
491 status
= ul_select(&card
);
493 PrintAndLog("Error: couldn't select");
494 ul_switch_off_field();
498 // read pages 0,1,2,4 (should read 4pages)
499 status
= ul_read(0, data
, sizeof(data
));
501 PrintAndLog("Error: tag didn't answer to READ A");
502 ul_switch_off_field();
506 ul_print_default(data
);
508 if ((tagtype
& UL_C
)){
510 // lookup lockbits before?
511 // read pages 0x28, 0x29, 0x2A, 0x2B
513 uint8_t ulc_conf
[16] = {0x00};
514 status
= ul_read(0x28, ulc_conf
, sizeof(ulc_conf
));
516 PrintAndLog("Error: tag didn't answer to READ - possibly locked");
518 ulc_print_configuration(ulc_conf
);
521 if ((tagtype
& MAGIC
)){
523 uint8_t ulc_deskey
[16] = {0x00};
524 status
= ul_read(0x2C, ulc_deskey
, sizeof(ulc_deskey
));
526 PrintAndLog("Error: tag didn't answer to READ B");
527 ul_switch_off_field();
531 ulc_print_3deskey(ulc_deskey
);
535 PrintAndLog("Trying some default 3des keys");
536 ul_switch_off_field();
537 for (uint8_t i
= 0; i
< 7; ++i
){
538 key
= default_3des_keys
[i
];
539 if (try3DesAuthentication(key
) == 1){
540 PrintAndLog("Found default 3des key: %s", sprint_hex(key
,16));
547 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
549 ulev1_print_counters();
551 uint8_t startconfigblock
= (tagtype
& UL_EV1_48
) ? 0x10 : 0x25;
552 uint8_t ulev1_conf
[16] = {0x00};
553 status
= ul_read(startconfigblock
, ulev1_conf
, sizeof(ulev1_conf
));
555 PrintAndLog("Error: tag didn't answer to READ C");
556 ul_switch_off_field();
559 ulev1_print_configuration(ulev1_conf
);
561 uint8_t ulev1_signature
[32] = {0x00};
562 status
= ulev1_readSignature( ulev1_signature
, sizeof(ulev1_signature
));
564 PrintAndLog("Error: tag didn't answer to READ SIGNATURE");
565 ul_switch_off_field();
568 ulev1_print_signature( ulev1_signature
, sizeof(ulev1_signature
));
571 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
| NTAG_213
| NTAG_215
| NTAG_216
))) {
573 uint8_t version
[10] = {0x00};
574 status
= ulev1_getVersion(version
, sizeof(version
));
576 PrintAndLog("Error: tag didn't answer to GETVERSION");
577 ul_switch_off_field();
580 ul_print_version(version
);
582 //********** TODO ********************************
583 // --problem, there is a failed pwd tries counter in UL-EV1
584 PrintAndLog("\nTrying some known EV1/NTAG passwords.");
586 uint8_t password
[4] ={0xff,0xff,0xff,0xff};
587 uint8_t pack
[4] = {0,0,0,0};
588 status
= ulev1_requestAuthentication(password
, pack
, sizeof(pack
));
590 PrintAndLog("Error: tag didn't answer to AUTHENTICATE");
591 ul_switch_off_field();
594 PrintAndLog("Found default password: %s",sprint_hex(password
, sizeof(password
)));
595 PrintAndLog("Got PACK : %s", sprint_hex(pack
,sizeof(pack
)));
598 if ((tagtype
& (NTAG_213
| NTAG_215
| NTAG_216
))){
600 PrintAndLog("\n--- NTAG NDEF Message");
601 uint8_t cc
[16] = {0x00};
602 status
= ul_read(2, cc
, sizeof(cc
));
604 PrintAndLog("Error: tag didn't answer to READ D");
605 ul_switch_off_field();
611 ul_switch_off_field();
616 // Mifare Ultralight Write Single Block
618 int CmdHF14AMfUWrBl(const char *Cmd
){
619 uint8_t blockNo
= -1;
620 bool chinese_card
= FALSE
;
621 uint8_t bldata
[16] = {0x00};
624 char cmdp
= param_getchar(Cmd
, 0);
625 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
626 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
627 PrintAndLog(" [block number]");
628 PrintAndLog(" [block data] - (8 hex symbols)");
629 PrintAndLog(" [w] - Chinese magic ultralight tag");
631 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
636 blockNo
= param_get8(Cmd
, 0);
638 if (blockNo
> MAX_UL_BLOCKS
){
639 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
643 if (param_gethex(Cmd
, 1, bldata
, 8)) {
644 PrintAndLog("Block data must include 8 HEX symbols");
648 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
654 PrintAndLog("Access Denied");
656 PrintAndLog("--specialblock no:%02x", blockNo
);
657 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
658 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
659 memcpy(d
.d
.asBytes
,bldata
, 4);
661 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
662 uint8_t isOK
= resp
.arg
[0] & 0xff;
663 PrintAndLog("isOk:%02x", isOK
);
665 PrintAndLog("Command execute timeout");
669 PrintAndLog("--block no:%02x", blockNo
);
670 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
671 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
672 memcpy(e
.d
.asBytes
,bldata
, 4);
674 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
675 uint8_t isOK
= resp
.arg
[0] & 0xff;
676 PrintAndLog("isOk:%02x", isOK
);
678 PrintAndLog("Command execute timeout");
685 // Mifare Ultralight Read Single Block
687 int CmdHF14AMfURdBl(const char *Cmd
){
690 uint8_t blockNo
= -1;
691 char cmdp
= param_getchar(Cmd
, 0);
693 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
694 PrintAndLog("Usage: hf mfu rdbl <block number>");
695 PrintAndLog(" sample: hfu mfu rdbl 0");
699 blockNo
= param_get8(Cmd
, 0);
701 if (blockNo
> MAX_UL_BLOCKS
){
702 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
706 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
710 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
711 uint8_t isOK
= resp
.arg
[0] & 0xff;
713 uint8_t *data
= resp
.d
.asBytes
;
714 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
717 PrintAndLog("Failed reading block: (%02x)", isOK
);
720 PrintAndLog("Command execute time-out");
726 int usage_hf_mfu_dump(void)
728 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
729 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
730 PrintAndLog("It autodetects card type.\n");
731 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
732 PrintAndLog(" Options : ");
733 PrintAndLog(" k <key> : Enter key for authentication");
734 PrintAndLog(" n <FN > : Enter filename w/o .bin to save the dump as");
735 PrintAndLog(" s : Swap entered key's endianness for auth");
737 PrintAndLog(" sample : hf mfu dump");
738 PrintAndLog(" : hf mfu dump n myfile");
742 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
743 // Read and Dump Card Contents, using auto detection of tag size.
745 // TODO: take a password to read UL-C / UL-EV1 tags.
746 int CmdHF14AMfUDump(const char *Cmd
){
749 char filename
[FILE_PATH_SIZE
] = {0x00};
750 char * fnameptr
= filename
;
751 char* str
= "Dumping Ultralight%s%s Card Data...";
752 uint8_t *lockbytes_t
= NULL
;
753 uint8_t lockbytes
[2] = {0x00};
754 uint8_t *lockbytes_t2
= NULL
;
755 uint8_t lockbytes2
[2] = {0x00};
756 bool bit
[16] = {0x00};
757 bool bit2
[16] = {0x00};
758 uint8_t data
[1024] = {0x00};
762 bool tmplockbit
= false;
765 uint8_t key
[16] = {0x00};
766 uint8_t *keyPtr
= key
;
769 bool swapEndian
= false;
771 while(param_getchar(Cmd
, cmdp
) != 0x00)
773 switch(param_getchar(Cmd
, cmdp
))
777 return usage_hf_mfu_dump();
780 dataLen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
784 memcpy(key
, data
, 16);
791 fileNlen
= param_getstr(Cmd
, cmdp
+1, filename
);
792 if (!fileNlen
) errors
= true;
793 if (fileNlen
> FILE_PATH_SIZE
-5) fileNlen
= FILE_PATH_SIZE
-5;
800 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
808 if(errors
) return usage_hf_mfu_dump();
811 keyPtr
= SwapEndian64(data
, 16);
813 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
814 if (tagtype
== UL_ERROR
) return -1;
816 if ( tagtype
& UL
) {
818 PrintAndLog(str
,"", (tagtype
& MAGIC
)?" (magic)":"" );
820 else if ( tagtype
& UL_C
) {
822 PrintAndLog(str
,"-C", (tagtype
& MAGIC
)?" (magic)":"" );
824 else if ( tagtype
& UL_EV1_48
) {
826 PrintAndLog(str
," EV1_48","");
828 else if ( tagtype
& UL_EV1_128
) {
830 PrintAndLog(str
," EV1_128","");
833 PrintAndLog("Dumping unknown Ultralight, using default values.");
836 UsbCommand c
= {CMD_MIFAREUC_READCARD
, {0,Pages
}};
839 memcpy(c
.d
.asBytes
, key
, 16);
843 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
844 PrintAndLog("Command execute time-out");
847 PrintAndLog ("%u,%u",resp
.arg
[0],resp
.arg
[1]);
848 uint8_t isOK
= resp
.arg
[0] & 0xff;
850 memcpy(data
, resp
.d
.asBytes
, resp
.arg
[1]);
852 PrintAndLog("Failed reading block: (%02x)", i
);
859 lockbytes_t
= data
+ 8;
860 lockbytes
[0] = lockbytes_t
[2];
861 lockbytes
[1] = lockbytes_t
[3];
862 for(j
= 0; j
< 16; j
++){
863 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
866 // Load bottom lockbytes if available
868 lockbytes_t2
= data
+ (40*4);
869 lockbytes2
[0] = lockbytes_t2
[2];
870 lockbytes2
[1] = lockbytes_t2
[3];
871 for (j
= 0; j
< 16; j
++) {
872 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
878 memcpy(data
+ Pages
*4, key
, 16);
881 for (i
= 0; i
< Pages
; ++i
) {
883 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
887 case 3: tmplockbit
= bit
[4]; break;
888 case 4: tmplockbit
= bit
[3]; break;
889 case 5: tmplockbit
= bit
[2]; break;
890 case 6: tmplockbit
= bit
[1]; break;
891 case 7: tmplockbit
= bit
[0]; break;
892 case 8: tmplockbit
= bit
[15]; break;
893 case 9: tmplockbit
= bit
[14]; break;
894 case 10: tmplockbit
= bit
[13]; break;
895 case 11: tmplockbit
= bit
[12]; break;
896 case 12: tmplockbit
= bit
[11]; break;
897 case 13: tmplockbit
= bit
[10]; break;
898 case 14: tmplockbit
= bit
[9]; break;
899 case 15: tmplockbit
= bit
[8]; break;
903 case 19: tmplockbit
= bit2
[6]; break;
907 case 23: tmplockbit
= bit2
[5]; break;
911 case 27: tmplockbit
= bit2
[4]; break;
915 case 31: tmplockbit
= bit2
[2]; break;
919 case 35: tmplockbit
= bit2
[1]; break;
923 case 39: tmplockbit
= bit2
[0]; break;
924 case 40: tmplockbit
= bit2
[12]; break;
925 case 41: tmplockbit
= bit2
[11]; break;
926 case 42: tmplockbit
= bit2
[10]; break; //auth0
927 case 43: tmplockbit
= bit2
[9]; break; //auth1
930 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
933 // user supplied filename?
935 // UID = data 0-1-2 4-5-6-7 (skips a beat)
936 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
937 data
[0], data
[1], data
[2], data
[4], data
[5], data
[6], data
[7]);
939 sprintf(fnameptr
+ fileNlen
," .bin");
942 if ((fout
= fopen(filename
,"wb")) == NULL
) {
943 PrintAndLog("Could not create file name %s", filename
);
946 fwrite( data
, 1, Pages
*4, fout
);
949 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
953 // Needed to Authenticate to Ultralight C tags
954 void rol (uint8_t *data
, const size_t len
){
955 uint8_t first
= data
[0];
956 for (size_t i
= 0; i
< len
-1; i
++) {
962 //-------------------------------------------------------------------------------
963 // Ultralight C Methods
964 //-------------------------------------------------------------------------------
967 // Ultralight C Authentication Demo {currently uses hard-coded key}
969 int CmdHF14AMfucAuth(const char *Cmd
){
974 char cmdp
= param_getchar(Cmd
, 0);
976 //Change key to user defined one
977 if (cmdp
== 'k' || cmdp
== 'K'){
978 keyNo
= param_get8(Cmd
, 1);
983 if (cmdp
== 'h' || cmdp
== 'H')
987 PrintAndLog("Usage: hf mfu cauth k <key number>");
988 PrintAndLog(" 0 (default): 3DES standard key");
989 PrintAndLog(" 1 : all 0x00 key");
990 PrintAndLog(" 2 : 0x00-0x0F key");
991 PrintAndLog(" 3 : nfc key");
992 PrintAndLog(" 4 : all 0x01 key");
993 PrintAndLog(" 5 : all 0xff key");
994 PrintAndLog(" 6 : 0x00-0xFF key");
995 PrintAndLog("\n sample : hf mfu cauth k");
996 PrintAndLog(" : hf mfu cauth k 3");
1000 uint8_t *key
= default_3des_keys
[keyNo
];
1001 if (try3DesAuthentication(key
)>0)
1002 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 16));
1004 PrintAndLog("Authentication failed");
1009 int try3DesAuthentication( uint8_t *key
){
1011 uint8_t blockNo
= 0;
1014 des3_context ctx
= { 0 };
1016 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
1017 uint8_t random_b
[8] = { 0 };
1018 uint8_t enc_random_b
[8] = { 0 };
1019 uint8_t rnd_ab
[16] = { 0 };
1020 uint8_t iv
[8] = { 0 };
1022 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
1025 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
1026 if ( !(resp
.arg
[0] & 0xff) ) return -2;
1029 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
1031 des3_set2key_dec(&ctx
, key
);
1032 // context, mode, length, IV, input, output
1033 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
1036 memcpy(rnd_ab
,random_a
,8);
1037 memcpy(rnd_ab
+8,random_b
,8);
1039 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
1040 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
1041 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
1042 //PrintAndLog(" A+B :%s", sprint_hex(rnd_ab, 16));
1044 des3_set2key_enc(&ctx
, key
);
1045 // context, mode, length, IV, input, output
1046 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
1049 c
.cmd
= CMD_MIFAREUC_AUTH2
;
1051 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
1054 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
1055 if ( !(resp
.arg
[0] & 0xff)) return -2;
1057 uint8_t enc_resp
[8] = { 0 };
1058 uint8_t resp_random_a
[8] = { 0 };
1059 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
1061 des3_set2key_dec(&ctx
, key
);
1062 // context, mode, length, IV, input, output
1063 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
1065 //PrintAndLog(" enc(A+B) :%s", sprint_hex(rnd_ab, 16));
1066 //PrintAndLog(" enc(RndA') :%s", sprint_hex(enc_resp, 8));
1068 if ( !memcmp(resp_random_a
, random_a
, 8))
1074 A test function to validate that the polarssl-function works the same
1075 was as the openssl-implementation.
1076 Commented out, since it requires openssl
1078 int CmdTestDES(const char * cmd)
1080 uint8_t key[16] = {0x00};
1082 memcpy(key,key3_3des_data,16);
1083 DES_cblock RndA, RndB;
1085 PrintAndLog("----------OpenSSL DES implementation----------");
1087 uint8_t e_RndB[8] = {0x00};
1088 unsigned char RndARndB[16] = {0x00};
1090 DES_cblock iv = { 0 };
1091 DES_key_schedule ks1,ks2;
1092 DES_cblock key1,key2;
1094 memcpy(key,key3_3des_data,16);
1096 memcpy(key2,key+8,8);
1099 DES_set_key((DES_cblock *)key1,&ks1);
1100 DES_set_key((DES_cblock *)key2,&ks2);
1102 DES_random_key(&RndA);
1103 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
1104 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
1105 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1106 // unsigned char *output, long length, DES_key_schedule *ks1,
1107 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1108 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1110 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
1112 memcpy(RndARndB,RndA,8);
1113 memcpy(RndARndB+8,RndB,8);
1114 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
1115 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1116 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
1119 PrintAndLog("----------PolarSSL implementation----------");
1121 uint8_t random_a[8] = { 0 };
1122 uint8_t enc_random_a[8] = { 0 };
1123 uint8_t random_b[8] = { 0 };
1124 uint8_t enc_random_b[8] = { 0 };
1125 uint8_t random_a_and_b[16] = { 0 };
1126 des3_context ctx = { 0 };
1128 memcpy(random_a, RndA,8);
1130 uint8_t output[8] = { 0 };
1131 uint8_t iv[8] = { 0 };
1133 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
1134 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
1136 des3_set2key_dec(&ctx, key);
1138 des3_crypt_cbc(&ctx // des3_context *ctx
1139 , DES_DECRYPT // int mode
1140 , sizeof(random_b) // size_t length
1141 , iv // unsigned char iv[8]
1142 , enc_random_b // const unsigned char *input
1143 , random_b // unsigned char *output
1146 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
1149 memcpy(random_a_and_b ,random_a,8);
1150 memcpy(random_a_and_b+8,random_b,8);
1152 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
1154 des3_set2key_enc(&ctx, key);
1156 des3_crypt_cbc(&ctx // des3_context *ctx
1157 , DES_ENCRYPT // int mode
1158 , sizeof(random_a_and_b) // size_t length
1159 , enc_random_b // unsigned char iv[8]
1160 , random_a_and_b // const unsigned char *input
1161 , random_a_and_b // unsigned char *output
1164 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1171 // Ultralight C Read Single Block
1173 int CmdHF14AMfUCRdBl(const char *Cmd
)
1176 bool hasPwd
= FALSE
;
1177 uint8_t blockNo
= -1;
1179 char cmdp
= param_getchar(Cmd
, 0);
1181 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
1182 PrintAndLog("Usage: hf mfu crdbl <block number> <key>");
1184 PrintAndLog("sample: hf mfu crdbl 0");
1185 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
1189 blockNo
= param_get8(Cmd
, 0);
1191 PrintAndLog("Wrong block number");
1195 if (blockNo
> MAX_ULC_BLOCKS
){
1196 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
1201 if ( strlen(Cmd
) > 3){
1202 if (param_gethex(Cmd
, 1, key
, 32)) {
1203 PrintAndLog("Key must include %d HEX symbols", 32);
1211 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
1214 memcpy(c
.d
.asBytes
,key
,16);
1218 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1219 uint8_t isOK
= resp
.arg
[0] & 0xff;
1221 uint8_t *data
= resp
.d
.asBytes
;
1222 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
1225 PrintAndLog("Failed reading block: (%02x)", isOK
);
1228 PrintAndLog("Command execute time-out");
1234 // Mifare Ultralight C Write Single Block
1236 int CmdHF14AMfUCWrBl(const char *Cmd
){
1238 uint8_t blockNo
= -1;
1239 bool chinese_card
= FALSE
;
1240 uint8_t bldata
[16] = {0x00};
1243 char cmdp
= param_getchar(Cmd
, 0);
1245 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
1246 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
1247 PrintAndLog(" [block number]");
1248 PrintAndLog(" [block data] - (8 hex symbols)");
1249 PrintAndLog(" [w] - Chinese magic ultralight tag");
1251 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
1256 blockNo
= param_get8(Cmd
, 0);
1257 if (blockNo
> MAX_ULC_BLOCKS
){
1258 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
1262 if (param_gethex(Cmd
, 1, bldata
, 8)) {
1263 PrintAndLog("Block data must include 8 HEX symbols");
1267 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
1268 chinese_card
= TRUE
;
1271 if ( blockNo
<= 3 ) {
1273 PrintAndLog("Access Denied");
1276 PrintAndLog("--Special block no: 0x%02x", blockNo
);
1277 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1278 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1279 memcpy(d
.d
.asBytes
,bldata
, 4);
1281 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1282 uint8_t isOK
= resp
.arg
[0] & 0xff;
1283 PrintAndLog("isOk:%02x", isOK
);
1285 PrintAndLog("Command execute timeout");
1290 PrintAndLog("--Block no : 0x%02x", blockNo
);
1291 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1292 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1293 memcpy(e
.d
.asBytes
,bldata
, 4);
1295 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1296 uint8_t isOK
= resp
.arg
[0] & 0xff;
1297 PrintAndLog("isOk : %02x", isOK
);
1299 PrintAndLog("Command execute timeout");
1307 // Mifare Ultralight C - Set password
1309 int CmdHF14AMfucSetPwd(const char *Cmd
){
1311 uint8_t pwd
[16] = {0x00};
1313 char cmdp
= param_getchar(Cmd
, 0);
1315 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1316 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
1317 PrintAndLog(" [password] - (32 hex symbols)");
1319 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1324 if (param_gethex(Cmd
, 0, pwd
, 32)) {
1325 PrintAndLog("Password must include 32 HEX symbols");
1329 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
1330 memcpy( c
.d
.asBytes
, pwd
, 16);
1335 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1336 if ( (resp
.arg
[0] & 0xff) == 1)
1337 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
1339 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
1344 PrintAndLog("command execution time out");
1352 // Magic UL / UL-C tags - Set UID
1354 int CmdHF14AMfucSetUid(const char *Cmd
){
1358 uint8_t uid
[7] = {0x00};
1359 char cmdp
= param_getchar(Cmd
, 0);
1361 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1362 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
1363 PrintAndLog(" [uid] - (14 hex symbols)");
1364 PrintAndLog("\nThis only works for Magic Ultralight tags.");
1366 PrintAndLog("sample: hf mfu setuid 11223344556677");
1371 if (param_gethex(Cmd
, 0, uid
, 14)) {
1372 PrintAndLog("UID must include 14 HEX symbols");
1377 c
.cmd
= CMD_MIFAREU_READBL
;
1380 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1381 PrintAndLog("Command execute timeout");
1386 uint8_t oldblock2
[4] = {0x00};
1387 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
1390 c
.cmd
= CMD_MIFAREU_WRITEBL
;
1392 c
.d
.asBytes
[0] = uid
[0];
1393 c
.d
.asBytes
[1] = uid
[1];
1394 c
.d
.asBytes
[2] = uid
[2];
1395 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
1397 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1398 PrintAndLog("Command execute timeout");
1404 c
.d
.asBytes
[0] = uid
[3];
1405 c
.d
.asBytes
[1] = uid
[4];
1406 c
.d
.asBytes
[2] = uid
[5];
1407 c
.d
.asBytes
[3] = uid
[6];
1409 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1410 PrintAndLog("Command execute timeout");
1416 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
1417 c
.d
.asBytes
[1] = oldblock2
[1];
1418 c
.d
.asBytes
[2] = oldblock2
[2];
1419 c
.d
.asBytes
[3] = oldblock2
[3];
1421 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1422 PrintAndLog("Command execute timeout");
1429 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
1431 uint8_t iv
[8] = { 0x00 };
1432 uint8_t block
= 0x07;
1435 //04 57 b6 e2 05 3f 80 UID
1437 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
1438 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1439 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1440 uint8_t dkeyA
[8] = { 0x00 };
1441 uint8_t dkeyB
[8] = { 0x00 };
1443 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1445 uint8_t mix
[8] = { 0x00 };
1446 uint8_t divkey
[8] = { 0x00 };
1448 memcpy(mix
, mifarekeyA
, 4);
1450 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1451 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1452 mix
[6] = block
^ uid
[2];
1455 des3_context ctx
= { 0x00 };
1456 des3_set2key_enc(&ctx
, masterkey
);
1458 des3_crypt_cbc(&ctx
// des3_context
1459 , DES_ENCRYPT
// int mode
1460 , sizeof(mix
) // length
1466 PrintAndLog("3DES version");
1467 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
1468 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
1469 PrintAndLog("Sector :\t %0d", block
);
1470 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
1471 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
1472 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
1474 PrintAndLog("\n DES version");
1476 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
1477 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
1478 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
1481 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
1482 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1483 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1486 uint8_t zeros
[8] = {0x00};
1487 uint8_t newpwd
[8] = {0x00};
1488 uint8_t dmkey
[24] = {0x00};
1489 memcpy(dmkey
, dkeyA
, 8);
1490 memcpy(dmkey
+8, dkeyB
, 8);
1491 memcpy(dmkey
+16, dkeyA
, 8);
1492 memset(iv
, 0x00, 8);
1494 des3_set3key_enc(&ctx
, dmkey
);
1496 des3_crypt_cbc(&ctx
// des3_context
1497 , DES_ENCRYPT
// int mode
1498 , sizeof(newpwd
) // length
1504 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1505 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1506 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1507 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1512 // static uint8_t * diversify_key(uint8_t * key){
1514 // for(int i=0; i<16; i++){
1515 // if(i<=6) key[i]^=cuid[i];
1516 // if(i>6) key[i]^=cuid[i%7];
1521 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1522 // for (int i=0; i<len; ++i){
1528 static command_t CommandTable
[] =
1530 {"help", CmdHelp
, 1, "This help"},
1531 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1532 {"info", CmdHF14AMfUInfo
, 0, "Tag information"},
1533 {"dump", CmdHF14AMfUDump
, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1534 {"rdbl", CmdHF14AMfURdBl
, 0, "Read block - Ultralight"},
1535 {"wrbl", CmdHF14AMfUWrBl
, 0, "Write block - Ultralight"},
1536 {"crdbl", CmdHF14AMfUCRdBl
, 0, "Read block - Ultralight C"},
1537 {"cwrbl", CmdHF14AMfUCWrBl
, 0, "Write block - Ultralight C"},
1538 {"cauth", CmdHF14AMfucAuth
, 0, "Authentication - Ultralight C"},
1539 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password - Ultralight-C"},
1540 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID - MAGIC tags only"},
1541 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1542 {NULL
, NULL
, 0, NULL
}
1545 int CmdHFMFUltra(const char *Cmd
){
1546 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1547 CmdsParse(CommandTable
, Cmd
);
1551 int CmdHelp(const char *Cmd
){
1552 CmdsHelp(CommandTable
);