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 #define MAX_UL_BLOCKS 0x0f
17 #define MAX_ULC_BLOCKS 0x2f
18 #define MAX_ULEV1a_BLOCKS 0x0b;
19 #define MAX_ULEV1b_BLOCKS 0x20;
21 uint8_t default_3des_keys
[7][16] = {
22 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
23 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
24 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
25 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
26 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
27 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
28 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
31 static int CmdHelp(const char *Cmd
);
33 // return 1 if tag responded to 0x1A.
34 uint8_t requestAuthentication( uint8_t* nonce
){
36 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_RAW
| ISO14A_APPEND_CRC
,2 ,0}};
37 c
.d
.asBytes
[0] = 0x1A;
38 c
.d
.asBytes
[1] = 0x00;
41 WaitForResponse(CMD_ACK
, &resp
); // skip select answer.
43 if ( !(resp
.arg
[0] & 0xff) )
46 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
48 if ( resp
.arg
[0] & 0xff ) {
49 memcpy(nonce
, resp
.d
.asBytes
+1, 8);
56 typedef enum TAGTYPE_UL
{
66 int CmdHF14AMfUInfo(const char *Cmd
){
68 TagTypeUL_t tagtype
= UNKNOWN
;
70 uint8_t datatemp
[7] = {0x00};
72 uint8_t data
[16] = {0x00};
74 UsbCommand c
= {CMD_MIFAREU_READCARD
, {0, 4}};
78 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
79 isOK
= resp
.arg
[0] & 0xff;
80 memcpy(data
, resp
.d
.asBytes
, sizeof(data
));
83 PrintAndLog("Error reading from tag");
87 PrintAndLog("Command execute timed out");
91 PrintAndLog("\n-- Tag Information ---------");
92 PrintAndLog("-------------------------------------------------------------");
94 c
.cmd
= CMD_READER_ISO_14443a
;
95 c
.arg
[0] = ISO14A_CONNECT
| ISO14A_RAW
| ISO14A_APPEND_CRC
;
98 c
.d
.asBytes
[0] = 0x60;
100 WaitForResponse(CMD_ACK
, &resp
);
103 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
105 uint8_t version
[8] = {0,0,0,0,0,0,0,0};
106 memcpy(&version
, resp
.d
.asBytes
, sizeof(version
));
107 uint8_t len
= resp
.arg
[0] & 0xff;
109 if ( len
== 0x0A && version
[6] == 0x0B )
111 else if ( len
== 0x0A && version
[6] != 0x0B )
112 tagtype
= UL_EV1_128
;
113 else if ( len
== 0x01 )
114 tagtype
= UL_C
| UL_C_MAGIC
;
115 else if ( len
== 0x00 )
116 tagtype
= UL
| UL_MAGIC
| UL_C_MAGIC
;
120 // Magic UL-C, mine have a static nonce response to 0x1A command.
121 uint8_t nonce1
[8] = {0,0,0,0,0,0,0,0};
122 uint8_t nonce2
[8] = {0,0,0,0,0,0,0,0};
123 uint8_t status
= requestAuthentication(nonce1
);
125 requestAuthentication(nonce2
);
126 if ( !memcmp(nonce1
, nonce2
, 8) )
127 tagtype
= UL_C_MAGIC
;
131 tagtype
= tagtype
& UL_C_MAGIC
;
133 // Magic Ultralight test here
137 case UNKNOWN
: PrintAndLog(" TYPE : Unknown"); return 0;
138 case UL
: PrintAndLog(" TYPE : MIFARE Ultralight");break;
139 case UL_C
: PrintAndLog(" TYPE : MIFARE Ultralight C");break;
140 case UL_EV1_48
: PrintAndLog(" TYPE : MIFARE Ultralight EV1 48 bytes"); break;
141 case UL_EV1_128
: PrintAndLog(" TYPE : MIFARE Ultralight EV1 128 bytes"); break;
142 case UL_MAGIC
: PrintAndLog(" TYPE : MIFARE Ultralight (MAGIC)");break;
143 case UL_C_MAGIC
: PrintAndLog(" TYPE : MIFARE Ultralight-C (MAGIC)");break;
147 memcpy( datatemp
, data
, 3);
148 memcpy( datatemp
+3, data
+4, 4);
150 PrintAndLog(" UID : %s ", sprint_hex(datatemp
, 7));
151 PrintAndLog(" UID[0] (Manufacturer Byte) = %02x, Manufacturer: %s", datatemp
[0], getTagInfo(datatemp
[0]) );
154 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
155 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
156 if ( data
[3] == crc0
)
157 PrintAndLog(" BCC0 : %02x - Ok", data
[3]);
159 PrintAndLog(" BCC0 : %02x - crc should be %02x", data
[3], crc0
);
161 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
162 if ( data
[8] == crc1
)
163 PrintAndLog(" BCC1 : %02x - Ok", data
[8]);
165 PrintAndLog(" BCC1 : %02x - crc should be %02x", data
[8], crc1
);
167 PrintAndLog(" Internal : %s ", sprint_hex(data
+ 9, 1));
169 memcpy(datatemp
, data
+10, 2);
170 PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp
, 2),printBits( 2, &datatemp
) );
171 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 3*4, 4));
176 if ( (tagtype
& UL_C
) == UL_C
|| (tagtype
& UL_C_MAGIC
) == UL_C_MAGIC
){
178 PrintAndLog("Trying some default 3des keys");
181 for (uint8_t i
= 0; i
< 5; ++i
){
182 key
= default_3des_keys
[i
];
183 if (try3DesAuthentication(key
)){
184 PrintAndLog("Found default 3des key: %s", sprint_hex(key
,16));
190 (tagtype
& UL_EV1_48
) == UL_EV1_48
||
191 (tagtype
& UL_EV1_128
) == UL_EV1_128
193 PrintAndLog("Trying some known EV1 passwords.");
199 // Mifare Ultralight Write Single Block
201 int CmdHF14AMfUWrBl(const char *Cmd
){
202 uint8_t blockNo
= -1;
203 bool chinese_card
= FALSE
;
204 uint8_t bldata
[16] = {0x00};
207 char cmdp
= param_getchar(Cmd
, 0);
208 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
209 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
210 PrintAndLog(" [block number]");
211 PrintAndLog(" [block data] - (8 hex symbols)");
212 PrintAndLog(" [w] - Chinese magic ultralight tag");
214 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
219 blockNo
= param_get8(Cmd
, 0);
221 if (blockNo
> MAX_UL_BLOCKS
){
222 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
226 if (param_gethex(Cmd
, 1, bldata
, 8)) {
227 PrintAndLog("Block data must include 8 HEX symbols");
231 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
237 PrintAndLog("Access Denied");
239 PrintAndLog("--specialblock no:%02x", blockNo
);
240 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
241 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
242 memcpy(d
.d
.asBytes
,bldata
, 4);
244 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
245 uint8_t isOK
= resp
.arg
[0] & 0xff;
246 PrintAndLog("isOk:%02x", isOK
);
248 PrintAndLog("Command execute timeout");
252 PrintAndLog("--block no:%02x", blockNo
);
253 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
254 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
255 memcpy(e
.d
.asBytes
,bldata
, 4);
257 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
258 uint8_t isOK
= resp
.arg
[0] & 0xff;
259 PrintAndLog("isOk:%02x", isOK
);
261 PrintAndLog("Command execute timeout");
268 // Mifare Ultralight Read Single Block
270 int CmdHF14AMfURdBl(const char *Cmd
){
273 uint8_t blockNo
= -1;
274 char cmdp
= param_getchar(Cmd
, 0);
276 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
277 PrintAndLog("Usage: hf mfu rdbl <block number>");
278 PrintAndLog(" sample: hfu mfu rdbl 0");
282 blockNo
= param_get8(Cmd
, 0);
284 if (blockNo
> MAX_UL_BLOCKS
){
285 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
289 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
293 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
294 uint8_t isOK
= resp
.arg
[0] & 0xff;
296 uint8_t *data
= resp
.d
.asBytes
;
297 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
300 PrintAndLog("Failed reading block: (%02x)", isOK
);
303 PrintAndLog("Command execute time-out");
310 // Mifare Ultralight / Ultralight-C; Read and Dump Card Contents
312 int CmdHF14AMfUDump(const char *Cmd
){
315 char filename
[FILE_PATH_SIZE
] = {0x00};
316 char * fnameptr
= filename
;
318 uint8_t *lockbytes_t
= NULL
;
319 uint8_t lockbytes
[2] = {0x00};
321 uint8_t *lockbytes_t2
= NULL
;
322 uint8_t lockbytes2
[2] = {0x00};
324 bool bit
[16] = {0x00};
325 bool bit2
[16] = {0x00};
331 bool tmplockbit
= false;
333 uint8_t *data
= NULL
;
335 char cmdp
= param_getchar(Cmd
, 0);
337 if (cmdp
== 'h' || cmdp
== 'H') {
338 PrintAndLog("Reads all pages from Mifare Ultralight or Ultralight-C tag.");
339 PrintAndLog("It saves binary dump into the file `filename.bin` or `cardUID.bin`");
340 PrintAndLog("Usage: hf mfu dump <c> <filename w/o .bin>");
341 PrintAndLog(" <c> optional cardtype c == Ultralight-C, Defaults to Ultralight");
342 PrintAndLog(" sample: hf mfu dump");
343 PrintAndLog(" : hf mfu dump myfile");
344 PrintAndLog(" : hf mfu dump c myfile");
349 Pages
= (cmdp
== 'c' || cmdp
== 'C') ? 44 : 16;
351 PrintAndLog("Dumping Ultralight%s Card Data...", (Pages
==16)?"":"-C");
353 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
,Pages
}};
357 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
358 isOK
= resp
.arg
[0] & 0xff;
360 PrintAndLog("Command error");
363 data
= resp
.d
.asBytes
;
365 PrintAndLog("Command execute time-out");
372 lockbytes_t
= data
+ 8;
373 lockbytes
[0] = lockbytes_t
[2];
374 lockbytes
[1] = lockbytes_t
[3];
375 for(j
= 0; j
< 16; j
++){
376 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
379 // Load bottom lockbytes if available
382 lockbytes_t2
= data
+ (40*4);
383 lockbytes2
[0] = lockbytes_t2
[2];
384 lockbytes2
[1] = lockbytes_t2
[3];
385 for (j
= 0; j
< 16; j
++) {
386 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
390 for (i
= 0; i
< Pages
; ++i
) {
393 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
398 case 3: tmplockbit
= bit
[4]; break;
399 case 4: tmplockbit
= bit
[3]; break;
400 case 5: tmplockbit
= bit
[2]; break;
401 case 6: tmplockbit
= bit
[1]; break;
402 case 7: tmplockbit
= bit
[0]; break;
403 case 8: tmplockbit
= bit
[15]; break;
404 case 9: tmplockbit
= bit
[14]; break;
405 case 10: tmplockbit
= bit
[13]; break;
406 case 11: tmplockbit
= bit
[12]; break;
407 case 12: tmplockbit
= bit
[11]; break;
408 case 13: tmplockbit
= bit
[10]; break;
409 case 14: tmplockbit
= bit
[9]; break;
410 case 15: tmplockbit
= bit
[8]; break;
414 case 19: tmplockbit
= bit2
[6]; break;
418 case 23: tmplockbit
= bit2
[5]; break;
422 case 27: tmplockbit
= bit2
[4]; break;
426 case 31: tmplockbit
= bit2
[2]; break;
430 case 35: tmplockbit
= bit2
[1]; break;
434 case 39: tmplockbit
= bit2
[0]; break;
435 case 40: tmplockbit
= bit2
[12]; break;
436 case 41: tmplockbit
= bit2
[11]; break;
437 case 42: tmplockbit
= bit2
[10]; break; //auth0
438 case 43: tmplockbit
= bit2
[9]; break; //auth1
441 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
446 len
= param_getstr(Cmd
,0,filename
);
448 len
= param_getstr(Cmd
,1,filename
);
450 if (len
> FILE_PATH_SIZE
-5) len
= FILE_PATH_SIZE
-5;
452 // user supplied filename?
455 // UID = data 0-1-2 4-5-6-7 (skips a beat)
456 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
457 data
[0],data
[1], data
[2], data
[4],data
[5],data
[6], data
[7]);
460 sprintf(fnameptr
+ len
," .bin");
464 if ((fout
= fopen(filename
,"wb")) == NULL
) {
465 PrintAndLog("Could not create file name %s", filename
);
468 fwrite( data
, 1, Pages
*4, fout
);
471 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
475 // Needed to Authenticate to Ultralight C tags
476 void rol (uint8_t *data
, const size_t len
){
477 uint8_t first
= data
[0];
478 for (size_t i
= 0; i
< len
-1; i
++) {
484 //-------------------------------------------------------------------------------
485 // Ultralight C Methods
486 //-------------------------------------------------------------------------------
489 // Ultralight C Authentication Demo {currently uses hard-coded key}
491 int CmdHF14AMfucAuth(const char *Cmd
){
496 char cmdp
= param_getchar(Cmd
, 0);
498 //Change key to user defined one
499 if (cmdp
== 'k' || cmdp
== 'K'){
500 keyNo
= param_get8(Cmd
, 1);
505 if (cmdp
== 'h' || cmdp
== 'H')
509 PrintAndLog("Usage: hf mfu cauth k <key number>");
510 PrintAndLog(" 0 (default): 3DES standard key");
511 PrintAndLog(" 1 : all 0x00 key");
512 PrintAndLog(" 2 : 0x00-0x0F key");
513 PrintAndLog(" 3 : nfc key");
514 PrintAndLog(" 4 : all 0x01 key");
515 PrintAndLog(" 5 : all 0xff key");
516 PrintAndLog(" 6 : 0x00-0xFF key");
517 PrintAndLog("\n sample : hf mfu cauth k");
518 PrintAndLog(" : hf mfu cauth k 3");
522 uint8_t *key
= default_3des_keys
[keyNo
];
523 if (try3DesAuthentication(key
))
524 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 8));
526 PrintAndLog("Authentication failed");
531 int try3DesAuthentication( uint8_t *key
){
536 des3_context ctx
= { 0 };
538 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
539 uint8_t random_b
[8] = { 0 };
540 uint8_t enc_random_b
[8] = { 0 };
541 uint8_t rnd_ab
[16] = { 0 };
542 uint8_t iv
[8] = { 0 };
544 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
547 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
548 if ( !(resp
.arg
[0] & 0xff) ) return -2;
551 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
553 des3_set2key_dec(&ctx
, key
);
554 // context, mode, length, IV, input, output
555 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
558 memcpy(rnd_ab
,random_a
,8);
559 memcpy(rnd_ab
+8,random_b
,8);
561 des3_set2key_enc(&ctx
, key
);
562 // context, mode, length, IV, input, output
563 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
566 c
.cmd
= CMD_MIFAREUC_AUTH2
;
568 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
571 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
572 if ( !(resp
.arg
[0] & 0xff)) return -2;
574 uint8_t enc_resp
[8] = { 0 };
575 uint8_t resp_random_a
[8] = { 0 };
576 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
578 des3_set2key_dec(&ctx
, key
);
579 // context, mode, length, IV, input, output
580 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
582 if ( !memcmp(resp_random_a
, random_a
, 8))
586 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
587 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
588 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
589 //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16));
590 //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16));
591 //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8));
595 A test function to validate that the polarssl-function works the same
596 was as the openssl-implementation.
597 Commented out, since it requires openssl
599 int CmdTestDES(const char * cmd)
601 uint8_t key[16] = {0x00};
603 memcpy(key,key3_3des_data,16);
604 DES_cblock RndA, RndB;
606 PrintAndLog("----------OpenSSL DES implementation----------");
608 uint8_t e_RndB[8] = {0x00};
609 unsigned char RndARndB[16] = {0x00};
611 DES_cblock iv = { 0 };
612 DES_key_schedule ks1,ks2;
613 DES_cblock key1,key2;
615 memcpy(key,key3_3des_data,16);
617 memcpy(key2,key+8,8);
620 DES_set_key((DES_cblock *)key1,&ks1);
621 DES_set_key((DES_cblock *)key2,&ks2);
623 DES_random_key(&RndA);
624 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
625 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
626 //void DES_ede2_cbc_encrypt(const unsigned char *input,
627 // unsigned char *output, long length, DES_key_schedule *ks1,
628 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
629 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
631 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
633 memcpy(RndARndB,RndA,8);
634 memcpy(RndARndB+8,RndB,8);
635 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
636 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
637 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
640 PrintAndLog("----------PolarSSL implementation----------");
642 uint8_t random_a[8] = { 0 };
643 uint8_t enc_random_a[8] = { 0 };
644 uint8_t random_b[8] = { 0 };
645 uint8_t enc_random_b[8] = { 0 };
646 uint8_t random_a_and_b[16] = { 0 };
647 des3_context ctx = { 0 };
649 memcpy(random_a, RndA,8);
651 uint8_t output[8] = { 0 };
652 uint8_t iv[8] = { 0 };
654 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
655 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
657 des3_set2key_dec(&ctx, key);
659 des3_crypt_cbc(&ctx // des3_context *ctx
660 , DES_DECRYPT // int mode
661 , sizeof(random_b) // size_t length
662 , iv // unsigned char iv[8]
663 , enc_random_b // const unsigned char *input
664 , random_b // unsigned char *output
667 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
670 memcpy(random_a_and_b ,random_a,8);
671 memcpy(random_a_and_b+8,random_b,8);
673 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
675 des3_set2key_enc(&ctx, key);
677 des3_crypt_cbc(&ctx // des3_context *ctx
678 , DES_ENCRYPT // int mode
679 , sizeof(random_a_and_b) // size_t length
680 , enc_random_b // unsigned char iv[8]
681 , random_a_and_b // const unsigned char *input
682 , random_a_and_b // unsigned char *output
685 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
691 // Ultralight C Read Single Block
693 int CmdHF14AMfUCRdBl(const char *Cmd
)
697 uint8_t blockNo
= -1;
698 unsigned char key
[16];
699 char cmdp
= param_getchar(Cmd
, 0);
701 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
702 PrintAndLog("Usage: hf mfu crdbl <block number> <password>");
704 PrintAndLog("sample: hf mfu crdbl 0");
705 PrintAndLog(" hf mfu crdbl 0 112233445566778899AABBCCDDEEFF");
709 blockNo
= param_get8(Cmd
, 0);
711 PrintAndLog("Wrong block number");
715 if (blockNo
> MAX_ULC_BLOCKS
){
716 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
721 if ( strlen(Cmd
) > 3){
722 if (param_gethex(Cmd
, 1, key
, 32)) {
723 PrintAndLog("Key must include %d HEX symbols", 32);
731 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
734 memcpy(c
.d
.asBytes
,key
,16);
738 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
739 uint8_t isOK
= resp
.arg
[0] & 0xff;
741 uint8_t *data
= resp
.d
.asBytes
;
742 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
745 PrintAndLog("Failed reading block: (%02x)", isOK
);
748 PrintAndLog("Command execute time-out");
754 // Mifare Ultralight C Write Single Block
756 int CmdHF14AMfUCWrBl(const char *Cmd
){
758 uint8_t blockNo
= -1;
759 bool chinese_card
= FALSE
;
760 uint8_t bldata
[16] = {0x00};
763 char cmdp
= param_getchar(Cmd
, 0);
765 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
766 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
767 PrintAndLog(" [block number]");
768 PrintAndLog(" [block data] - (8 hex symbols)");
769 PrintAndLog(" [w] - Chinese magic ultralight tag");
771 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
776 blockNo
= param_get8(Cmd
, 0);
777 if (blockNo
> MAX_ULC_BLOCKS
){
778 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
782 if (param_gethex(Cmd
, 1, bldata
, 8)) {
783 PrintAndLog("Block data must include 8 HEX symbols");
787 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
791 if ( blockNo
<= 3 ) {
793 PrintAndLog("Access Denied");
795 PrintAndLog("--Special block no: 0x%02x", blockNo
);
796 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
797 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
798 memcpy(d
.d
.asBytes
,bldata
, 4);
800 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
801 uint8_t isOK
= resp
.arg
[0] & 0xff;
802 PrintAndLog("isOk:%02x", isOK
);
804 PrintAndLog("Command execute timeout");
808 PrintAndLog("--Block no : 0x%02x", blockNo
);
809 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
810 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
811 memcpy(e
.d
.asBytes
,bldata
, 4);
813 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
814 uint8_t isOK
= resp
.arg
[0] & 0xff;
815 PrintAndLog("isOk : %02x", isOK
);
817 PrintAndLog("Command execute timeout");
824 // Mifare Ultralight C - Set password
826 int CmdHF14AMfucSetPwd(const char *Cmd
){
828 uint8_t pwd
[16] = {0x00};
830 char cmdp
= param_getchar(Cmd
, 0);
832 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
833 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
834 PrintAndLog(" [password] - (32 hex symbols)");
836 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
841 if (param_gethex(Cmd
, 0, pwd
, 32)) {
842 PrintAndLog("Password must include 32 HEX symbols");
846 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
847 memcpy( c
.d
.asBytes
, pwd
, 16);
852 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
853 if ( (resp
.arg
[0] & 0xff) == 1)
854 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
856 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
861 PrintAndLog("command execution time out");
869 // Mifare Ultraligh - Set UID
871 int CmdHF14AMfucSetUid(const char *Cmd
){
875 uint8_t uid
[7] = {0x00};
876 char cmdp
= param_getchar(Cmd
, 0);
878 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
879 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
880 PrintAndLog(" [uid] - (14 hex symbols)");
881 PrintAndLog("\nThis only works for Magic Ultralight tags.");
883 PrintAndLog("sample: hf mfu setuid 11223344556677");
888 if (param_gethex(Cmd
, 0, uid
, 14)) {
889 PrintAndLog("UID must include 14 HEX symbols");
894 c
.cmd
= CMD_MIFAREU_READBL
;
897 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
898 PrintAndLog("Command execute timeout");
903 uint8_t oldblock2
[4] = {0x00};
904 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
907 c
.cmd
= CMD_MIFAREU_WRITEBL
;
909 c
.d
.asBytes
[0] = uid
[0];
910 c
.d
.asBytes
[1] = uid
[1];
911 c
.d
.asBytes
[2] = uid
[2];
912 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
914 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
915 PrintAndLog("Command execute timeout");
921 c
.d
.asBytes
[0] = uid
[3];
922 c
.d
.asBytes
[1] = uid
[4];
923 c
.d
.asBytes
[2] = uid
[5];
924 c
.d
.asBytes
[3] = uid
[6];
926 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
927 PrintAndLog("Command execute timeout");
933 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
934 c
.d
.asBytes
[1] = oldblock2
[1];
935 c
.d
.asBytes
[2] = oldblock2
[2];
936 c
.d
.asBytes
[3] = oldblock2
[3];
938 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
939 PrintAndLog("Command execute timeout");
946 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
948 uint8_t iv
[8] = { 0x00 };
949 uint8_t block
= 0x07;
952 //04 57 b6 e2 05 3f 80 UID
954 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
955 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
956 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
957 uint8_t dkeyA
[8] = { 0x00 };
958 uint8_t dkeyB
[8] = { 0x00 };
960 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
962 uint8_t mix
[8] = { 0x00 };
963 uint8_t divkey
[8] = { 0x00 };
965 memcpy(mix
, mifarekeyA
, 4);
967 mix
[4] = mifarekeyA
[4] ^ uid
[0];
968 mix
[5] = mifarekeyA
[5] ^ uid
[1];
969 mix
[6] = block
^ uid
[2];
972 des3_context ctx
= { 0x00 };
973 des3_set2key_enc(&ctx
, masterkey
);
975 des3_crypt_cbc(&ctx
// des3_context
976 , DES_ENCRYPT
// int mode
977 , sizeof(mix
) // length
983 PrintAndLog("3DES version");
984 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
985 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
986 PrintAndLog("Sector :\t %0d", block
);
987 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
988 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
989 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
991 PrintAndLog("\n DES version");
993 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
994 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
995 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
998 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
999 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1000 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1003 uint8_t zeros
[8] = {0x00};
1004 uint8_t newpwd
[8] = {0x00};
1005 uint8_t dmkey
[24] = {0x00};
1006 memcpy(dmkey
, dkeyA
, 8);
1007 memcpy(dmkey
+8, dkeyB
, 8);
1008 memcpy(dmkey
+16, dkeyA
, 8);
1009 memset(iv
, 0x00, 8);
1011 des3_set3key_enc(&ctx
, dmkey
);
1013 des3_crypt_cbc(&ctx
// des3_context
1014 , DES_ENCRYPT
// int mode
1015 , sizeof(newpwd
) // length
1021 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1022 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1023 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1024 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1029 // static uint8_t * diversify_key(uint8_t * key){
1031 // for(int i=0; i<16; i++){
1032 // if(i<=6) key[i]^=cuid[i];
1033 // if(i>6) key[i]^=cuid[i%7];
1038 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1039 // for (int i=0; i<len; ++i){
1045 //------------------------------------
1047 //------------------------------------
1048 static command_t CommandTable
[] =
1050 {"help", CmdHelp
, 1,"This help"},
1051 {"dbg", CmdHF14AMfDbg
, 0,"Set default debug mode"},
1052 {"info", CmdHF14AMfUInfo
, 0,"Taginfo"},
1053 {"dump", CmdHF14AMfUDump
, 0,"Dump MIFARE Ultralight / Ultralight-C tag to binary file"},
1054 {"rdbl", CmdHF14AMfURdBl
, 0,"Read block - MIFARE Ultralight"},
1055 {"wrbl", CmdHF14AMfUWrBl
, 0,"Write block - MIFARE Ultralight"},
1056 {"crdbl", CmdHF14AMfUCRdBl
, 0,"Read block - MIFARE Ultralight C"},
1057 {"cwrbl", CmdHF14AMfUCWrBl
, 0,"Write block - MIFARE Ultralight C"},
1058 {"cauth", CmdHF14AMfucAuth
, 0,"Ultralight C Authentication"},
1059 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password [Ultralight-C only]"},
1060 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID"},
1061 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1062 {NULL
, NULL
, 0, NULL
}
1065 int CmdHFMFUltra(const char *Cmd
){
1066 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1067 CmdsParse(CommandTable
, Cmd
);
1071 int CmdHelp(const char *Cmd
){
1072 CmdsHelp(CommandTable
);