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 #define KEYS_3DES_COUNT 7
43 uint8_t default_3des_keys
[KEYS_3DES_COUNT
][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 #define KEYS_PWD_COUNT 8
54 uint8_t default_pwd_pack
[KEYS_PWD_COUNT
][4] = {
55 {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default
56 {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed)
57 {0x33,0x6B,0xA1,0x19}, // PACK 0x9c,0x2d -- italian bus (sniffed)
58 {0xFF,0x90,0x6C,0xB2}, // PACK 0x12,0x9e -- italian bus (sniffed)
59 {0x05,0x22,0xE6,0xB4}, // PACK 0x80,0x80 -- Amiiboo (sniffed) pikachu-b UID:
60 {0x7E,0x22,0xE6,0xB4}, // PACK 0x80,0x80 -- AMiiboo (sniffed)
61 {0x02,0xE1,0xEE,0x36}, // PACK 0x80,0x80 -- AMiiboo (sniffed) sonic UID: 04d257 7ae33e8027
62 {0x32,0x0C,0x16,0x17}, // PACK 0x80,0x80 -- AMiiboo (sniffed)
66 static int CmdHelp(const char *Cmd
);
68 char* getProductTypeStr( uint8_t id
){
75 sprintf(retStr
, "0x%02X %s", id
, "(Ultralight)");
78 sprintf(retStr
, "0x%02X %s", id
, "(NTAG)");
81 sprintf(retStr
, "0x%02X %s", id
, "(unknown)");
88 The 7 MSBits (=n) code the storage size itself based on 2^n,
89 the LSBit is set to '0' if the size is exactly 2^n
90 and set to '1' if the storage size is between 2^n and 2^(n+1).
92 char* getUlev1CardSizeStr( uint8_t fsize
){
97 uint8_t usize
= 1 << ((fsize
>>1) + 1);
98 uint8_t lsize
= 1 << (fsize
>>1);
102 sprintf(retStr
, "0x%02X (%u - %u bytes)",fsize
, usize
, lsize
);
104 sprintf(retStr
, "0x%02X (%u bytes)", fsize
, lsize
);
108 static void ul_switch_on_field(void) {
109 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
, 0, 0}};
113 static void ul_switch_off_field(void) {
114 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
118 static int ul_send_cmd_raw( uint8_t *cmd
, uint8_t cmdlen
, uint8_t *response
, uint16_t responseLength
) {
119 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_NO_DISCONNECT
| ISO14A_APPEND_CRC
, cmdlen
, 0}};
120 memcpy(c
.d
.asBytes
, cmd
, cmdlen
);
123 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
124 if ( !resp
.arg
[0] && responseLength
) return -1;
126 uint16_t resplen
= (resp
.arg
[0] < responseLength
) ? resp
.arg
[0] : responseLength
;
127 memcpy(response
, resp
.d
.asBytes
, resplen
);
131 static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) {
132 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}};
134 c.arg[0] |= ISO14A_APPEND_CRC;
136 memcpy(c.d.asBytes, cmd, cmdlen);
139 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
140 if (!resp.arg[0] && responseLength) return -1;
142 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
143 memcpy(response, resp.d.asBytes, resplen );
147 static int ul_select( iso14a_card_select_t
*card
){
149 ul_switch_on_field();
152 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
153 if (resp
.arg
[0] < 1) return -1;
155 memcpy(card
, resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
159 // This read command will at least return 16bytes.
160 static int ul_read( uint8_t page
, uint8_t *response
, uint16_t responseLength
){
162 uint8_t cmd
[] = {ISO14443A_CMD_READBLOCK
, page
};
163 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
165 ul_switch_off_field();
169 static int ul_comp_write( uint8_t page
, uint8_t *data
, uint8_t datalen
){
172 memset(cmd
, 0x00, sizeof(cmd
));
173 datalen
= ( datalen
> 16) ? 16 : datalen
;
175 cmd
[0] = ISO14443A_CMD_WRITEBLOCK
;
177 memcpy(cmd
+2, data
, datalen
);
179 uint8_t response
[1] = {0xff};
180 int len
= ul_send_cmd_raw(cmd
, 2+datalen
, response
, sizeof(response
));
182 ul_switch_off_field();
184 if ( response
[0] == 0x0a ) return 0;
189 static int ulc_requestAuthentication( uint8_t blockNo
, uint8_t *nonce
, uint16_t nonceLength
){
191 uint8_t cmd
[] = {MIFARE_ULC_AUTH_1
, blockNo
};
192 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), nonce
, nonceLength
);
194 ul_switch_off_field();
198 static int ulev1_requestAuthentication( uint8_t *pwd
, uint8_t *pack
, uint16_t packLength
){
200 uint8_t cmd
[] = {MIFARE_ULEV1_AUTH
, pwd
[0], pwd
[1], pwd
[2], pwd
[3]};
201 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), pack
, packLength
);
203 ul_switch_off_field();
207 static int ulev1_getVersion( uint8_t *response
, uint16_t responseLength
){
209 uint8_t cmd
[] = {MIFARE_ULEV1_VERSION
};
210 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
212 ul_switch_off_field();
216 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
218 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
220 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
221 // ul_switch_off_field();
227 static int ulev1_readCounter( uint8_t counter
, uint8_t *response
, uint16_t responseLength
){
229 uint8_t cmd
[] = {MIFARE_ULEV1_READ_CNT
, counter
};
230 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
232 ul_switch_off_field();
236 static int ulev1_readSignature( uint8_t *response
, uint16_t responseLength
){
238 uint8_t cmd
[] = {MIFARE_ULEV1_READSIG
, 0x00};
239 int len
= ul_send_cmd_raw(cmd
, sizeof(cmd
), response
, responseLength
);
241 ul_switch_off_field();
245 static int ul_print_default( uint8_t *data
){
257 PrintAndLog(" UID : %s ", sprint_hex(uid
, 7));
258 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", uid
[0], getTagInfo(uid
[0]) );
261 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
262 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
263 if ( data
[3] == crc0
)
264 PrintAndLog(" BCC0 : 0x%02X - Ok", data
[3]);
266 PrintAndLog(" BCC0 : 0x%02X - crc should be %02x", data
[3], crc0
);
268 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
269 if ( data
[8] == crc1
)
270 PrintAndLog(" BCC1 : 0x%02X - Ok", data
[8]);
272 PrintAndLog(" BCC1 : 0x%02X - crc should be 0x%02X", data
[8], crc1
);
274 PrintAndLog(" Internal : 0x%02X - %s default", data
[9], (data
[9]==0x48)?"":"not" );
275 PrintAndLog(" Lock : %s - %s", sprint_hex(data
+10, 2),printBits( 2, data
+10) );
276 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 12, 4));
281 static int ntag_print_CC(uint8_t *data
) {
282 if(data
[0] != 0xe1) {
283 PrintAndLog("no NDEF message");
284 return -1; // no NDEF message
287 PrintAndLog("Capability Container: %s", sprint_hex(data
,4) );
288 PrintAndLog(" %02X: NDEF Magic Number", data
[0]);
289 PrintAndLog(" %02X: version %d.%d supported by tag", data
[1], (data
[1] & 0xF0) >> 4, data
[1] & 0x0f);
290 PrintAndLog(" %02X: Physical Memory Size: %d bytes", data
[2], (data
[2] + 1) * 8);
291 if ( data
[2] == 0x12 )
292 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 144);
293 else if ( data
[2] == 0x3e )
294 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 496);
295 else if ( data
[2] == 0x6d )
296 PrintAndLog(" %02X: NDEF Memory Size: &d bytes", data
[2], 872);
298 PrintAndLog(" %02X: %s / %s", data
[3],
299 (data
[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
300 (data
[3] & 0x0F)==0 ? "Write access granted without any security" : (data
[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
304 static int ulev1_print_version(uint8_t *data
){
305 PrintAndLog("\n--- UL-EV1 / NTAG Version");
306 PrintAndLog(" Raw bytes : %s", sprint_hex(data
, 8) );
307 PrintAndLog(" Vendor ID : 0x%02X, Manufacturer: %s", data
[1], getTagInfo(data
[1]));
308 PrintAndLog(" Product type : %s" , getProductTypeStr(data
[2]));
309 PrintAndLog(" Product subtype : 0x%02X %s" , data
[3], (data
[3]==1) ?"17 pF":"50pF");
310 PrintAndLog(" Major version : 0x%02X" , data
[4]);
311 PrintAndLog(" Minor version : 0x%02X" , data
[5]);
312 PrintAndLog(" Size : %s", getUlev1CardSizeStr(data
[6]));
313 PrintAndLog(" Protocol type : 0x%02X" , data
[7]);
317 static int ul_print_type(uint16_t tagtype
){
319 PrintAndLog(" TYPE : MIFARE Ultralight (MF0ICU1) %s", (tagtype
& MAGIC
)?"<magic>":"");
320 else if ( tagtype
& UL_C
)
321 PrintAndLog(" TYPE : MIFARE Ultralight C (MF0ULC) %s [%x]", (tagtype
& MAGIC
)?"<magic>":"", tagtype
);
322 else if ( tagtype
& UL_EV1_48
)
323 PrintAndLog(" TYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)");
324 else if ( tagtype
& UL_EV1_128
)
325 PrintAndLog(" TYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)");
326 else if ( tagtype
& NTAG_213
)
327 PrintAndLog(" TYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)");
328 else if ( tagtype
& NTAG_215
)
329 PrintAndLog(" TYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)");
330 else if ( tagtype
& NTAG_216
)
331 PrintAndLog(" TYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)");
333 PrintAndLog(" TYPE : Unknown %04x",tagtype
);
337 static int ulc_print_3deskey( uint8_t *data
){
338 PrintAndLog(" deskey1 [44/0x2C]: %s [%.4s]", sprint_hex(data
,4),data
);
339 PrintAndLog(" deskey1 [45/0x2D]: %s [%.4s]", sprint_hex(data
+4 ,4),data
+4);
340 PrintAndLog(" deskey2 [46/0x2E]: %s [%.4s]", sprint_hex(data
+8 ,4),data
+8);
341 PrintAndLog(" deskey2 [47/0x2F]: %s [%.4s]", sprint_hex(data
+12,4),data
+12);
342 PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data
, 16), 16));
346 static int ulc_print_configuration( uint8_t *data
){
348 PrintAndLog("--- UL-C Configuration");
349 PrintAndLog(" Higher Lockbits [40/0x28]: %s %s", sprint_hex(data
, 4), printBits(2, data
));
350 PrintAndLog(" Counter [41/0x29]: %s %s", sprint_hex(data
+4, 4), printBits(2, data
+4));
352 bool validAuth
= (data
[8] >= 0x03 && data
[8] <= 0x30);
354 PrintAndLog(" Auth0 [42/0x2A]: %s - Pages above %d needs authentication", sprint_hex(data
+8, 4), data
[8] );
357 PrintAndLog(" Auth0 [42/0x2A]: %s - default", sprint_hex(data
+8, 4) );
359 PrintAndLog(" Auth0 [42/0x2A]: %s - auth byte is out-of-range", sprint_hex(data
+8, 4) );
362 PrintAndLog(" Auth1 [43/0x2B]: %s - %s",
363 sprint_hex(data
+12, 4),
364 (data
[12] & 1) ? "write access restricted": "read and write access restricted"
369 static int ulev1_print_configuration( uint8_t *data
){
371 PrintAndLog("\n--- UL-EV1 Configuration");
373 bool strg_mod_en
= (data
[0] & 2);
374 uint8_t authlim
= (data
[4] & 0x07);
375 bool cfglck
= (data
[4] & 0x40);
376 bool prot
= (data
[4] & 0x80);
377 uint8_t vctid
= data
[5];
379 PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data
, 4));
380 PrintAndLog(" - pages above %d needs authentication",data
[3]);
381 PrintAndLog(" - strong modulation mode %s", (strg_mod_en
) ? "enabled":"disabled");
382 PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data
+4, 4) );
384 PrintAndLog(" - Max number of password attempts is unlimited");
386 PrintAndLog(" - Max number of password attempts is %d", authlim
);
387 PrintAndLog(" - user configuration %s", cfglck
? "permanently locked":"writeable");
388 PrintAndLog(" - %s access is protected with password", prot
? "read and write":"write");
389 PrintAndLog(" 0x%02X - Virtual Card Type Identifier is %s default", vctid
, (vctid
==0x05)? "":"not");
390 PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data
+8, 4));
391 PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data
+12, 4));
395 static int ulev1_print_counters(){
396 PrintAndLog("--- UL-EV1 Counters");
397 uint8_t counter
[3] = {0,0,0};
398 for ( uint8_t i
= 0; i
<3; ++i
) {
399 ulev1_readCounter(i
,counter
, sizeof(counter
) );
400 PrintAndLog(" [0x%0d] : %s", i
, sprint_hex(counter
,3));
405 static int ulev1_print_signature( uint8_t *data
, uint8_t len
){
406 PrintAndLog("\n--- UL-EV1 Signature");
407 PrintAndLog("IC signature public key name : NXP NTAG21x 2013");
408 PrintAndLog("IC signature public key value : 04494e1a386d3d3cfe3dc10e5de68a499b1c202db5b132393e89ed19fe5be8bc61");
409 PrintAndLog(" Elliptic curve parameters : secp128r1");
410 PrintAndLog(" Tag ECC Signature : %s", sprint_hex(data
, len
));
411 //to do: verify if signature is valid
412 //PrintAndLog("IC signature status: %s valid", (iseccvalid() )?"":"not");
416 static int ulc_magic_test(){
417 // Magic Ultralight test
418 // Magic UL-C, by observation,
419 // 1) it seems to have a static nonce response to 0x1A command.
420 // 2) the deskey bytes is not-zero:d out on as datasheet states.
421 // 3) UID - changeable, not only, but pages 0-1-2-3.
422 // 4) use the ul_magic_test ! magic tags answers specially!
423 int returnValue
= UL_ERROR
;
424 iso14a_card_select_t card
;
425 uint8_t nonce1
[11] = {0x00};
426 uint8_t nonce2
[11] = {0x00};
427 int status
= ul_select(&card
);
429 PrintAndLog("Error: couldn't select ulc_magic_test");
430 ul_switch_off_field();
433 status
= ulc_requestAuthentication(0, nonce1
, sizeof(nonce1
));
435 status
= ulc_requestAuthentication(0, nonce2
, sizeof(nonce2
));
436 returnValue
= ( !memcmp(nonce1
, nonce2
, 11) ) ? UL_C_MAGIC
: UL_C
;
440 ul_switch_off_field();
444 static int ul_magic_test(){
446 // Magic Ultralight tests
447 // 1) take present UID, and try to write it back. OBSOLETE
448 // 2) make a wrong length write to page0, and see if tag answers with ACK/NACK:
449 iso14a_card_select_t card
;
450 int status
= ul_select(&card
);
452 PrintAndLog("Error: couldn't select ul_magic_test");
453 ul_switch_off_field();
456 status
= ul_comp_write(0, NULL
, 0);
457 ul_switch_off_field();
463 uint16_t GetHF14AMfU_Type(void){
465 TagTypeUL_t tagtype
= UNKNOWN
;
466 iso14a_card_select_t card
;
467 uint8_t version
[10] = {0x00};
471 status
= ul_select(&card
);
473 PrintAndLog("Error: couldn't select");
474 ul_switch_off_field();
477 // Ultralight - ATQA / SAK
478 if ( card
.atqa
[1] != 0x00 || card
.atqa
[0] != 0x44 || card
.sak
!= 0x00 ) {
479 PrintAndLog("Tag is not UL or NTAG. [ATQA: %02X %02x SAK: %02X]", card
.atqa
[1],card
.atqa
[0],card
.sak
);
480 ul_switch_off_field();
484 len
= ulev1_getVersion(version
, sizeof(version
));
485 ul_switch_off_field();
490 if ( version
[2] == 0x03 && version
[6] == 0x0B )
492 else if ( version
[2] == 0x03 && version
[6] != 0x0B )
493 tagtype
= UL_EV1_128
;
494 else if ( version
[2] == 0x04 && version
[6] == 0x0F )
496 else if ( version
[2] == 0x04 && version
[6] == 0x11 )
498 else if ( version
[2] == 0x04 && version
[6] == 0x13 )
500 else if ( version
[2] == 0x04 )
505 case 0x01: tagtype
= UL_C
; break;
506 case 0x00: tagtype
= UL
; break;
507 case -1: tagtype
= (UL
| UL_C
); break;
508 default: tagtype
= UNKNOWN
; break;
511 if ((tagtype
& ( UL_C
| UL
))) tagtype
= ulc_magic_test();
512 if ( (tagtype
& UL
) ) tagtype
= ul_magic_test();
517 int CmdHF14AMfUInfo(const char *Cmd
){
519 uint8_t authlim
= 0xff;
520 uint8_t data
[16] = {0x00};
521 iso14a_card_select_t card
;
525 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
526 if (tagtype
== UL_ERROR
) return -1;
528 PrintAndLog("\n--- Tag Information ---------");
529 PrintAndLog("-------------------------------------------------------------");
530 ul_print_type(tagtype
);
532 status
= ul_select(&card
);
534 PrintAndLog("Error: couldn't select");
535 ul_switch_off_field();
539 // read pages 0,1,2,4 (should read 4pages)
540 status
= ul_read(0, data
, sizeof(data
));
542 PrintAndLog("Error: tag didn't answer to READ A");
543 ul_switch_off_field();
547 ul_print_default(data
);
549 if ((tagtype
& UL_C
)){
551 // read pages 0x28, 0x29, 0x2A, 0x2B
552 uint8_t ulc_conf
[16] = {0x00};
553 status
= ul_read(0x28, ulc_conf
, sizeof(ulc_conf
));
555 PrintAndLog("Error: tag didn't answer to READ - possibly locked");
556 ul_switch_off_field();
560 ulc_print_configuration(ulc_conf
);
562 if ((tagtype
& MAGIC
)){
564 uint8_t ulc_deskey
[16] = {0x00};
565 status
= ul_read(0x2C, ulc_deskey
, sizeof(ulc_deskey
));
567 PrintAndLog("Error: tag didn't answer to READ B");
568 ul_switch_off_field();
571 ulc_print_3deskey(ulc_deskey
);
574 PrintAndLog("Trying some default 3des keys");
575 ul_switch_off_field();
576 for (uint8_t i
= 0; i
< KEYS_3DES_COUNT
; ++i
){
577 key
= default_3des_keys
[i
];
578 if (try3DesAuthentication(key
) == 1){
579 PrintAndLog("Found default 3des key: "); //%s", sprint_hex(key,16));
580 ulc_print_3deskey(SwapEndian64(key
,16));
587 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
589 ulev1_print_counters();
591 uint8_t ulev1_signature
[32] = {0x00};
592 status
= ulev1_readSignature( ulev1_signature
, sizeof(ulev1_signature
));
594 PrintAndLog("Error: tag didn't answer to READ SIGNATURE");
595 ul_switch_off_field();
598 ulev1_print_signature( ulev1_signature
, sizeof(ulev1_signature
));
600 uint8_t startconfigblock
= (tagtype
& UL_EV1_48
) ? 0x10 : 0x25;
601 uint8_t ulev1_conf
[16] = {0x00};
602 status
= ul_read(startconfigblock
, ulev1_conf
, sizeof(ulev1_conf
));
604 PrintAndLog("Error: tag didn't answer to READ - possibly locked");
605 ul_switch_off_field();
608 // save AUTHENTICATION LIMITS for later:
609 authlim
= (ulev1_conf
[4] & 0x07);
610 ulev1_print_configuration(ulev1_conf
);
613 if ((tagtype
& (UL_EV1_48
| UL_EV1_128
| NTAG_213
| NTAG_215
| NTAG_216
))) {
615 uint8_t version
[10] = {0x00};
616 status
= ulev1_getVersion(version
, sizeof(version
));
618 PrintAndLog("Error: tag didn't answer to GETVERSION");
619 ul_switch_off_field();
622 ulev1_print_version(version
);
624 // AUTHLIMIT, (number of failed authentications)
626 // 1-7 = ... should we even try then?
629 PrintAndLog("\n--- Known EV1/NTAG passwords.");
631 uint8_t pack
[4] = {0,0,0,0};
633 for (uint8_t i
= 0; i
< KEYS_PWD_COUNT
; ++i
){
634 key
= default_pwd_pack
[i
];
635 if ( ulev1_requestAuthentication(key
, pack
, sizeof(pack
)) > -1 ){
636 PrintAndLog("Found a default password: %s || Pack: %02X %02X",sprint_hex(key
, 4), pack
[0], pack
[1]);
639 ul_switch_off_field();
643 if ((tagtype
& (NTAG_213
| NTAG_215
| NTAG_216
))){
645 PrintAndLog("\n--- NTAG NDEF Message");
646 uint8_t cc
[16] = {0x00};
647 status
= ul_read(3, cc
, sizeof(cc
));
649 PrintAndLog("Error: tag didn't answer to READ D");
650 ul_switch_off_field();
656 ul_switch_off_field();
661 // Mifare Ultralight Write Single Block
663 int CmdHF14AMfUWrBl(const char *Cmd
){
664 uint8_t blockNo
= -1;
665 bool chinese_card
= FALSE
;
666 uint8_t bldata
[16] = {0x00};
669 char cmdp
= param_getchar(Cmd
, 0);
670 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
671 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
672 PrintAndLog(" [block number]");
673 PrintAndLog(" [block data] - (8 hex symbols)");
674 PrintAndLog(" [w] - Chinese magic ultralight tag");
676 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
681 blockNo
= param_get8(Cmd
, 0);
683 if (blockNo
> MAX_UL_BLOCKS
){
684 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
688 if (param_gethex(Cmd
, 1, bldata
, 8)) {
689 PrintAndLog("Block data must include 8 HEX symbols");
693 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
699 PrintAndLog("Access Denied");
701 PrintAndLog("--specialblock no:%02x", blockNo
);
702 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
703 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
704 memcpy(d
.d
.asBytes
,bldata
, 4);
706 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
707 uint8_t isOK
= resp
.arg
[0] & 0xff;
708 PrintAndLog("isOk:%02x", isOK
);
710 PrintAndLog("Command execute timeout");
714 PrintAndLog("--block no:%02x", blockNo
);
715 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
716 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
717 memcpy(e
.d
.asBytes
,bldata
, 4);
719 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
720 uint8_t isOK
= resp
.arg
[0] & 0xff;
721 PrintAndLog("isOk:%02x", isOK
);
723 PrintAndLog("Command execute timeout");
730 // Mifare Ultralight Read Single Block
732 int CmdHF14AMfURdBl(const char *Cmd
){
735 uint8_t blockNo
= -1;
736 char cmdp
= param_getchar(Cmd
, 0);
738 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
739 PrintAndLog("Usage: hf mfu rdbl <block number>");
740 PrintAndLog(" sample: hfu mfu rdbl 0");
744 blockNo
= param_get8(Cmd
, 0);
746 if (blockNo
> MAX_UL_BLOCKS
){
747 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
751 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
755 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
756 uint8_t isOK
= resp
.arg
[0] & 0xff;
758 uint8_t *data
= resp
.d
.asBytes
;
759 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
762 PrintAndLog("Failed reading block: (%02x)", isOK
);
765 PrintAndLog("Command execute time-out");
771 int usage_hf_mfu_dump(void)
773 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
774 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
775 PrintAndLog("It autodetects card type.\n");
776 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
777 PrintAndLog(" Options : ");
778 PrintAndLog(" k <key> : Enter key for authentication");
779 PrintAndLog(" n <FN > : Enter filename w/o .bin to save the dump as");
780 PrintAndLog(" s : Swap entered key's endianness for auth");
782 PrintAndLog(" sample : hf mfu dump");
783 PrintAndLog(" : hf mfu dump n myfile");
787 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
788 // Read and Dump Card Contents, using auto detection of tag size.
790 // TODO: take a password to read UL-C / UL-EV1 tags.
791 int CmdHF14AMfUDump(const char *Cmd
){
794 char filename
[FILE_PATH_SIZE
] = {0x00};
795 char * fnameptr
= filename
;
796 char* str
= "Dumping Ultralight%s%s Card Data...";
797 uint8_t *lockbytes_t
= NULL
;
798 uint8_t lockbytes
[2] = {0x00};
799 uint8_t *lockbytes_t2
= NULL
;
800 uint8_t lockbytes2
[2] = {0x00};
801 bool bit
[16] = {0x00};
802 bool bit2
[16] = {0x00};
803 uint8_t data
[1024] = {0x00};
807 bool tmplockbit
= false;
810 uint8_t key
[16] = {0x00};
811 uint8_t *keyPtr
= key
;
814 bool swapEndian
= false;
816 while(param_getchar(Cmd
, cmdp
) != 0x00)
818 switch(param_getchar(Cmd
, cmdp
))
822 return usage_hf_mfu_dump();
825 dataLen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
829 memcpy(key
, data
, 16);
836 fileNlen
= param_getstr(Cmd
, cmdp
+1, filename
);
837 if (!fileNlen
) errors
= true;
838 if (fileNlen
> FILE_PATH_SIZE
-5) fileNlen
= FILE_PATH_SIZE
-5;
846 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
854 if(errors
) return usage_hf_mfu_dump();
857 keyPtr
= SwapEndian64(data
, 16);
859 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
860 if (tagtype
== UL_ERROR
) return -1;
862 if ( tagtype
& UL
) {
864 PrintAndLog(str
,"", (tagtype
& MAGIC
)?" (magic)":"" );
866 else if ( tagtype
& UL_C
) {
868 PrintAndLog(str
,"-C", (tagtype
& MAGIC
)?" (magic)":"" );
870 else if ( tagtype
& UL_EV1_48
) {
872 PrintAndLog(str
," EV1_48","");
874 else if ( tagtype
& UL_EV1_128
) {
876 PrintAndLog(str
," EV1_128","");
879 PrintAndLog("Dumping unknown Ultralight, using default values.");
882 UsbCommand c
= {CMD_MIFAREUC_READCARD
, {0,Pages
}};
885 memcpy(c
.d
.asBytes
, key
, 16);
889 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
890 PrintAndLog("Command execute time-out");
893 PrintAndLog ("%u,%u",resp
.arg
[0],resp
.arg
[1]);
894 uint8_t isOK
= resp
.arg
[0] & 0xff;
896 memcpy(data
, resp
.d
.asBytes
, resp
.arg
[1]);
898 PrintAndLog("Failed reading block: (%02x)", i
);
905 lockbytes_t
= data
+ 8;
906 lockbytes
[0] = lockbytes_t
[2];
907 lockbytes
[1] = lockbytes_t
[3];
908 for(j
= 0; j
< 16; j
++){
909 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
912 // Load bottom lockbytes if available
914 lockbytes_t2
= data
+ (40*4);
915 lockbytes2
[0] = lockbytes_t2
[2];
916 lockbytes2
[1] = lockbytes_t2
[3];
917 for (j
= 0; j
< 16; j
++) {
918 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
924 memcpy(data
+ Pages
*4, key
, 16);
927 for (i
= 0; i
< Pages
; ++i
) {
929 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
933 case 3: tmplockbit
= bit
[4]; break;
934 case 4: tmplockbit
= bit
[3]; break;
935 case 5: tmplockbit
= bit
[2]; break;
936 case 6: tmplockbit
= bit
[1]; break;
937 case 7: tmplockbit
= bit
[0]; break;
938 case 8: tmplockbit
= bit
[15]; break;
939 case 9: tmplockbit
= bit
[14]; break;
940 case 10: tmplockbit
= bit
[13]; break;
941 case 11: tmplockbit
= bit
[12]; break;
942 case 12: tmplockbit
= bit
[11]; break;
943 case 13: tmplockbit
= bit
[10]; break;
944 case 14: tmplockbit
= bit
[9]; break;
945 case 15: tmplockbit
= bit
[8]; break;
949 case 19: tmplockbit
= bit2
[6]; break;
953 case 23: tmplockbit
= bit2
[5]; break;
957 case 27: tmplockbit
= bit2
[4]; break;
961 case 31: tmplockbit
= bit2
[2]; break;
965 case 35: tmplockbit
= bit2
[1]; break;
969 case 39: tmplockbit
= bit2
[0]; break;
970 case 40: tmplockbit
= bit2
[12]; break;
971 case 41: tmplockbit
= bit2
[11]; break;
972 case 42: tmplockbit
= bit2
[10]; break; //auth0
973 case 43: tmplockbit
= bit2
[9]; break; //auth1
976 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
979 // user supplied filename?
981 // UID = data 0-1-2 4-5-6-7 (skips a beat)
982 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
983 data
[0], data
[1], data
[2], data
[4], data
[5], data
[6], data
[7]);
985 sprintf(fnameptr
+ fileNlen
," .bin");
988 if ((fout
= fopen(filename
,"wb")) == NULL
) {
989 PrintAndLog("Could not create file name %s", filename
);
992 fwrite( data
, 1, Pages
*4, fout
);
995 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
999 // Needed to Authenticate to Ultralight C tags
1000 void rol (uint8_t *data
, const size_t len
){
1001 uint8_t first
= data
[0];
1002 for (size_t i
= 0; i
< len
-1; i
++) {
1003 data
[i
] = data
[i
+1];
1005 data
[len
-1] = first
;
1008 //-------------------------------------------------------------------------------
1009 // Ultralight C Methods
1010 //-------------------------------------------------------------------------------
1013 // Ultralight C Authentication Demo {currently uses hard-coded key}
1015 int CmdHF14AMfucAuth(const char *Cmd
){
1018 bool errors
= false;
1020 char cmdp
= param_getchar(Cmd
, 0);
1022 //Change key to user defined one
1023 if (cmdp
== 'k' || cmdp
== 'K'){
1024 keyNo
= param_get8(Cmd
, 1);
1025 if(keyNo
> KEYS_3DES_COUNT
)
1029 if (cmdp
== 'h' || cmdp
== 'H')
1033 PrintAndLog("Usage: hf mfu cauth k <key number>");
1034 PrintAndLog(" 0 (default): 3DES standard key");
1035 PrintAndLog(" 1 : all 0x00 key");
1036 PrintAndLog(" 2 : 0x00-0x0F key");
1037 PrintAndLog(" 3 : nfc key");
1038 PrintAndLog(" 4 : all 0x01 key");
1039 PrintAndLog(" 5 : all 0xff key");
1040 PrintAndLog(" 6 : 0x00-0xFF key");
1041 PrintAndLog("\n sample : hf mfu cauth k");
1042 PrintAndLog(" : hf mfu cauth k 3");
1046 uint8_t *key
= default_3des_keys
[keyNo
];
1047 if (try3DesAuthentication(key
)>0)
1048 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 16));
1050 PrintAndLog("Authentication failed");
1055 int try3DesAuthentication( uint8_t *key
){
1057 uint8_t blockNo
= 0;
1060 des3_context ctx
= { 0 };
1062 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
1063 uint8_t random_b
[8] = { 0 };
1064 uint8_t enc_random_b
[8] = { 0 };
1065 uint8_t rnd_ab
[16] = { 0 };
1066 uint8_t iv
[8] = { 0 };
1068 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
1071 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
1072 if ( !(resp
.arg
[0] & 0xff) ) return -2;
1075 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
1077 des3_set2key_dec(&ctx
, key
);
1078 // context, mode, length, IV, input, output
1079 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
1082 memcpy(rnd_ab
,random_a
,8);
1083 memcpy(rnd_ab
+8,random_b
,8);
1085 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
1086 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
1087 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
1088 //PrintAndLog(" A+B :%s", sprint_hex(rnd_ab, 16));
1090 des3_set2key_enc(&ctx
, key
);
1091 // context, mode, length, IV, input, output
1092 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
1095 c
.cmd
= CMD_MIFAREUC_AUTH2
;
1097 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
1100 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
1101 if ( !(resp
.arg
[0] & 0xff)) return -2;
1103 uint8_t enc_resp
[8] = { 0 };
1104 uint8_t resp_random_a
[8] = { 0 };
1105 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
1107 des3_set2key_dec(&ctx
, key
);
1108 // context, mode, length, IV, input, output
1109 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
1111 //PrintAndLog(" enc(A+B) :%s", sprint_hex(rnd_ab, 16));
1112 //PrintAndLog(" enc(RndA') :%s", sprint_hex(enc_resp, 8));
1114 if ( !memcmp(resp_random_a
, random_a
, 8))
1120 A test function to validate that the polarssl-function works the same
1121 was as the openssl-implementation.
1122 Commented out, since it requires openssl
1124 int CmdTestDES(const char * cmd)
1126 uint8_t key[16] = {0x00};
1128 memcpy(key,key3_3des_data,16);
1129 DES_cblock RndA, RndB;
1131 PrintAndLog("----------OpenSSL DES implementation----------");
1133 uint8_t e_RndB[8] = {0x00};
1134 unsigned char RndARndB[16] = {0x00};
1136 DES_cblock iv = { 0 };
1137 DES_key_schedule ks1,ks2;
1138 DES_cblock key1,key2;
1140 memcpy(key,key3_3des_data,16);
1142 memcpy(key2,key+8,8);
1145 DES_set_key((DES_cblock *)key1,&ks1);
1146 DES_set_key((DES_cblock *)key2,&ks2);
1148 DES_random_key(&RndA);
1149 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
1150 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
1151 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1152 // unsigned char *output, long length, DES_key_schedule *ks1,
1153 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1154 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1156 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
1158 memcpy(RndARndB,RndA,8);
1159 memcpy(RndARndB+8,RndB,8);
1160 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
1161 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1162 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
1165 PrintAndLog("----------PolarSSL implementation----------");
1167 uint8_t random_a[8] = { 0 };
1168 uint8_t enc_random_a[8] = { 0 };
1169 uint8_t random_b[8] = { 0 };
1170 uint8_t enc_random_b[8] = { 0 };
1171 uint8_t random_a_and_b[16] = { 0 };
1172 des3_context ctx = { 0 };
1174 memcpy(random_a, RndA,8);
1176 uint8_t output[8] = { 0 };
1177 uint8_t iv[8] = { 0 };
1179 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
1180 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
1182 des3_set2key_dec(&ctx, key);
1184 des3_crypt_cbc(&ctx // des3_context *ctx
1185 , DES_DECRYPT // int mode
1186 , sizeof(random_b) // size_t length
1187 , iv // unsigned char iv[8]
1188 , enc_random_b // const unsigned char *input
1189 , random_b // unsigned char *output
1192 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
1195 memcpy(random_a_and_b ,random_a,8);
1196 memcpy(random_a_and_b+8,random_b,8);
1198 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
1200 des3_set2key_enc(&ctx, key);
1202 des3_crypt_cbc(&ctx // des3_context *ctx
1203 , DES_ENCRYPT // int mode
1204 , sizeof(random_a_and_b) // size_t length
1205 , enc_random_b // unsigned char iv[8]
1206 , random_a_and_b // const unsigned char *input
1207 , random_a_and_b // unsigned char *output
1210 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1217 // Ultralight C Read Single Block
1219 int CmdHF14AMfUCRdBl(const char *Cmd
)
1222 bool hasPwd
= FALSE
;
1223 uint8_t blockNo
= -1;
1225 char cmdp
= param_getchar(Cmd
, 0);
1227 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
1228 PrintAndLog("Usage: hf mfu crdbl <block number> <key>");
1230 PrintAndLog("sample: hf mfu crdbl 0");
1231 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
1235 blockNo
= param_get8(Cmd
, 0);
1237 PrintAndLog("Wrong block number");
1241 if (blockNo
> MAX_ULC_BLOCKS
){
1242 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
1247 if ( strlen(Cmd
) > 3){
1248 if (param_gethex(Cmd
, 1, key
, 32)) {
1249 PrintAndLog("Key must include %d HEX symbols", 32);
1257 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
1260 memcpy(c
.d
.asBytes
,key
,16);
1264 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1265 uint8_t isOK
= resp
.arg
[0] & 0xff;
1267 uint8_t *data
= resp
.d
.asBytes
;
1268 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
1271 PrintAndLog("Failed reading block: (%02x)", isOK
);
1274 PrintAndLog("Command execute time-out");
1280 // Mifare Ultralight C Write Single Block
1282 int CmdHF14AMfUCWrBl(const char *Cmd
){
1284 uint8_t blockNo
= -1;
1285 bool chinese_card
= FALSE
;
1286 uint8_t bldata
[16] = {0x00};
1289 char cmdp
= param_getchar(Cmd
, 0);
1291 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
1292 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
1293 PrintAndLog(" [block number]");
1294 PrintAndLog(" [block data] - (8 hex symbols)");
1295 PrintAndLog(" [w] - Chinese magic ultralight tag");
1297 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
1302 blockNo
= param_get8(Cmd
, 0);
1303 if (blockNo
> MAX_ULC_BLOCKS
){
1304 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
1308 if (param_gethex(Cmd
, 1, bldata
, 8)) {
1309 PrintAndLog("Block data must include 8 HEX symbols");
1313 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
1314 chinese_card
= TRUE
;
1317 if ( blockNo
<= 3 ) {
1319 PrintAndLog("Access Denied");
1322 PrintAndLog("--Special block no: 0x%02x", blockNo
);
1323 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1324 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1325 memcpy(d
.d
.asBytes
,bldata
, 4);
1327 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1328 uint8_t isOK
= resp
.arg
[0] & 0xff;
1329 PrintAndLog("isOk:%02x", isOK
);
1331 PrintAndLog("Command execute timeout");
1336 PrintAndLog("--Block no : 0x%02x", blockNo
);
1337 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
1338 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
1339 memcpy(e
.d
.asBytes
,bldata
, 4);
1341 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1342 uint8_t isOK
= resp
.arg
[0] & 0xff;
1343 PrintAndLog("isOk : %02x", isOK
);
1345 PrintAndLog("Command execute timeout");
1353 // Mifare Ultralight C - Set password
1355 int CmdHF14AMfucSetPwd(const char *Cmd
){
1357 uint8_t pwd
[16] = {0x00};
1359 char cmdp
= param_getchar(Cmd
, 0);
1361 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1362 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
1363 PrintAndLog(" [password] - (32 hex symbols)");
1365 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1370 if (param_gethex(Cmd
, 0, pwd
, 32)) {
1371 PrintAndLog("Password must include 32 HEX symbols");
1375 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
1376 memcpy( c
.d
.asBytes
, pwd
, 16);
1381 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1382 if ( (resp
.arg
[0] & 0xff) == 1)
1383 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
1385 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
1390 PrintAndLog("command execution time out");
1398 // Magic UL / UL-C tags - Set UID
1400 int CmdHF14AMfucSetUid(const char *Cmd
){
1404 uint8_t uid
[7] = {0x00};
1405 char cmdp
= param_getchar(Cmd
, 0);
1407 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
1408 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
1409 PrintAndLog(" [uid] - (14 hex symbols)");
1410 PrintAndLog("\nThis only works for Magic Ultralight tags.");
1412 PrintAndLog("sample: hf mfu setuid 11223344556677");
1417 if (param_gethex(Cmd
, 0, uid
, 14)) {
1418 PrintAndLog("UID must include 14 HEX symbols");
1423 c
.cmd
= CMD_MIFAREU_READBL
;
1426 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1427 PrintAndLog("Command execute timeout");
1432 uint8_t oldblock2
[4] = {0x00};
1433 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
1436 c
.cmd
= CMD_MIFAREU_WRITEBL
;
1438 c
.d
.asBytes
[0] = uid
[0];
1439 c
.d
.asBytes
[1] = uid
[1];
1440 c
.d
.asBytes
[2] = uid
[2];
1441 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
1443 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
1444 PrintAndLog("Command execute timeout");
1450 c
.d
.asBytes
[0] = uid
[3];
1451 c
.d
.asBytes
[1] = uid
[4];
1452 c
.d
.asBytes
[2] = uid
[5];
1453 c
.d
.asBytes
[3] = uid
[6];
1455 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1456 PrintAndLog("Command execute timeout");
1462 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
1463 c
.d
.asBytes
[1] = oldblock2
[1];
1464 c
.d
.asBytes
[2] = oldblock2
[2];
1465 c
.d
.asBytes
[3] = oldblock2
[3];
1467 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1468 PrintAndLog("Command execute timeout");
1475 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
1477 uint8_t iv
[8] = { 0x00 };
1478 uint8_t block
= 0x07;
1481 //04 57 b6 e2 05 3f 80 UID
1483 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
1484 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1485 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1486 uint8_t dkeyA
[8] = { 0x00 };
1487 uint8_t dkeyB
[8] = { 0x00 };
1489 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1491 uint8_t mix
[8] = { 0x00 };
1492 uint8_t divkey
[8] = { 0x00 };
1494 memcpy(mix
, mifarekeyA
, 4);
1496 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1497 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1498 mix
[6] = block
^ uid
[2];
1501 des3_context ctx
= { 0x00 };
1502 des3_set2key_enc(&ctx
, masterkey
);
1504 des3_crypt_cbc(&ctx
// des3_context
1505 , DES_ENCRYPT
// int mode
1506 , sizeof(mix
) // length
1512 PrintAndLog("3DES version");
1513 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
1514 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
1515 PrintAndLog("Sector :\t %0d", block
);
1516 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
1517 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
1518 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
1520 PrintAndLog("\n DES version");
1522 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
1523 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
1524 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
1527 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
1528 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1529 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1532 uint8_t zeros
[8] = {0x00};
1533 uint8_t newpwd
[8] = {0x00};
1534 uint8_t dmkey
[24] = {0x00};
1535 memcpy(dmkey
, dkeyA
, 8);
1536 memcpy(dmkey
+8, dkeyB
, 8);
1537 memcpy(dmkey
+16, dkeyA
, 8);
1538 memset(iv
, 0x00, 8);
1540 des3_set3key_enc(&ctx
, dmkey
);
1542 des3_crypt_cbc(&ctx
// des3_context
1543 , DES_ENCRYPT
// int mode
1544 , sizeof(newpwd
) // length
1550 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1551 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1552 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1553 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1558 // static uint8_t * diversify_key(uint8_t * key){
1560 // for(int i=0; i<16; i++){
1561 // if(i<=6) key[i]^=cuid[i];
1562 // if(i>6) key[i]^=cuid[i%7];
1567 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1568 // for (int i=0; i<len; ++i){
1574 static command_t CommandTable
[] =
1576 {"help", CmdHelp
, 1, "This help"},
1577 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1578 {"info", CmdHF14AMfUInfo
, 0, "Tag information"},
1579 {"dump", CmdHF14AMfUDump
, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1580 {"rdbl", CmdHF14AMfURdBl
, 0, "Read block - Ultralight"},
1581 {"wrbl", CmdHF14AMfUWrBl
, 0, "Write block - Ultralight"},
1582 {"crdbl", CmdHF14AMfUCRdBl
, 0, "Read block - Ultralight C"},
1583 {"cwrbl", CmdHF14AMfUCWrBl
, 0, "Write block - Ultralight C"},
1584 {"cauth", CmdHF14AMfucAuth
, 0, "Authentication - Ultralight C"},
1585 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password - Ultralight-C"},
1586 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID - MAGIC tags only"},
1587 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1588 {NULL
, NULL
, 0, NULL
}
1591 int CmdHFMFUltra(const char *Cmd
){
1592 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1593 CmdsParse(CommandTable
, Cmd
);
1597 int CmdHelp(const char *Cmd
){
1598 CmdsHelp(CommandTable
);