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
{
68 uint8_t GetHF14AMfU_Type(uint8_t *data
, uint8_t dataSize
){
69 TagTypeUL_t tagtype
= UNKNOWN
;
72 UsbCommand c
= {CMD_MIFAREU_READCARD
, {0, 4}};
76 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
77 isOK
= resp
.arg
[0] & 0xff;
78 memcpy(data
, resp
.d
.asBytes
, dataSize
);
81 PrintAndLog("Error reading from tag");
85 PrintAndLog("Command execute timed out");
89 c
.cmd
= CMD_READER_ISO_14443a
;
90 c
.arg
[0] = ISO14A_CONNECT
| ISO14A_RAW
| ISO14A_APPEND_CRC
;
93 c
.d
.asBytes
[0] = 0x60;
95 WaitForResponse(CMD_ACK
, &resp
);
98 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
100 uint8_t version
[8] = {0,0,0,0,0,0,0,0};
101 memcpy(&version
, resp
.d
.asBytes
, sizeof(version
));
102 uint8_t len
= resp
.arg
[0] & 0xff;
104 if ( len
== 0x0A && version
[6] == 0x0B )
106 else if ( len
== 0x0A && version
[6] != 0x0B )
107 tagtype
= UL_EV1_128
;
108 else if ( len
== 0x01 )
110 else if ( len
== 0x00 )
111 tagtype
= UL
; //| UL_MAGIC | UL_C_MAGIC
115 // Magic UL-C, mine have a static nonce response to 0x1A command.
116 uint8_t nonce1
[8] = {0,0,0,0,0,0,0,0};
117 uint8_t nonce2
[8] = {0,0,0,0,0,0,0,0};
118 uint8_t status
= requestAuthentication(nonce1
);
120 requestAuthentication(nonce2
);
121 if ( !memcmp(nonce1
, nonce2
, 8) )
124 // Magic Ultralight test here - TODO
129 int CmdHF14AMfUInfo(const char *Cmd
){
131 TagTypeUL_t tagtype
= UNKNOWN
;
133 uint8_t datatemp
[7] = {0x00};
134 uint8_t data
[16] = {0x00};
136 tagtype
= GetHF14AMfU_Type(data
, sizeof(data
));
137 if (tagtype
== UL_ERROR
) return -1;
139 PrintAndLog("\n-- Tag Information ---------");
140 PrintAndLog("-------------------------------------------------------------");
142 case UNKNOWN
: PrintAndLog(" TYPE : Unknown"); return 0;
143 case UL
: PrintAndLog(" TYPE : MIFARE Ultralight");break;
144 case UL_C
: PrintAndLog(" TYPE : MIFARE Ultralight C");break;
145 case UL_EV1_48
: PrintAndLog(" TYPE : MIFARE Ultralight EV1 48 bytes"); break;
146 case UL_EV1_128
: PrintAndLog(" TYPE : MIFARE Ultralight EV1 128 bytes"); break;
147 case UL_MAGIC
: PrintAndLog(" TYPE : MIFARE Ultralight (MAGIC)");break;
148 case UL_C_MAGIC
: PrintAndLog(" TYPE : MIFARE Ultralight-C (MAGIC)");break;
149 default : PrintAndLog(" TYPE : Unknown %x",tagtype
);break;
153 memcpy( datatemp
, data
, 3);
154 memcpy( datatemp
+3, data
+4, 4);
156 PrintAndLog(" UID : %s ", sprint_hex(datatemp
, 7));
157 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", datatemp
[0], getTagInfo(datatemp
[0]) );
160 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
161 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
162 if ( data
[3] == crc0
)
163 PrintAndLog(" BCC0 : %02x - Ok", data
[3]);
165 PrintAndLog(" BCC0 : %02x - crc should be %02x", data
[3], crc0
);
167 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
168 if ( data
[8] == crc1
)
169 PrintAndLog(" BCC1 : %02x - Ok", data
[8]);
171 PrintAndLog(" BCC1 : %02x - crc should be %02x", data
[8], crc1
);
173 PrintAndLog(" Internal : %s ", sprint_hex(data
+ 9, 1));
175 memcpy(datatemp
, data
+10, 2);
176 PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp
, 2),printBits( 2, &datatemp
) );
177 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 3*4, 4));
182 if ((tagtype
& UL_C
)){
184 PrintAndLog("Trying some default 3des keys");
187 for (uint8_t i
= 0; i
< 5; ++i
){
188 key
= default_3des_keys
[i
];
189 if (try3DesAuthentication(key
)){
190 PrintAndLog("Found default 3des key: %s", sprint_hex(key
,16));
195 else if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
196 //********** TODO ********************************
197 //PrintAndLog("Trying some known EV1 passwords.");
203 // Mifare Ultralight Write Single Block
205 int CmdHF14AMfUWrBl(const char *Cmd
){
206 uint8_t blockNo
= -1;
207 bool chinese_card
= FALSE
;
208 uint8_t bldata
[16] = {0x00};
211 char cmdp
= param_getchar(Cmd
, 0);
212 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
213 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
214 PrintAndLog(" [block number]");
215 PrintAndLog(" [block data] - (8 hex symbols)");
216 PrintAndLog(" [w] - Chinese magic ultralight tag");
218 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
223 blockNo
= param_get8(Cmd
, 0);
225 if (blockNo
> MAX_UL_BLOCKS
){
226 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
230 if (param_gethex(Cmd
, 1, bldata
, 8)) {
231 PrintAndLog("Block data must include 8 HEX symbols");
235 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
241 PrintAndLog("Access Denied");
243 PrintAndLog("--specialblock no:%02x", blockNo
);
244 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
245 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
246 memcpy(d
.d
.asBytes
,bldata
, 4);
248 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
249 uint8_t isOK
= resp
.arg
[0] & 0xff;
250 PrintAndLog("isOk:%02x", isOK
);
252 PrintAndLog("Command execute timeout");
256 PrintAndLog("--block no:%02x", blockNo
);
257 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
258 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
259 memcpy(e
.d
.asBytes
,bldata
, 4);
261 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
262 uint8_t isOK
= resp
.arg
[0] & 0xff;
263 PrintAndLog("isOk:%02x", isOK
);
265 PrintAndLog("Command execute timeout");
272 // Mifare Ultralight Read Single Block
274 int CmdHF14AMfURdBl(const char *Cmd
){
277 uint8_t blockNo
= -1;
278 char cmdp
= param_getchar(Cmd
, 0);
280 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
281 PrintAndLog("Usage: hf mfu rdbl <block number>");
282 PrintAndLog(" sample: hfu mfu rdbl 0");
286 blockNo
= param_get8(Cmd
, 0);
288 if (blockNo
> MAX_UL_BLOCKS
){
289 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
293 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
297 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
298 uint8_t isOK
= resp
.arg
[0] & 0xff;
300 uint8_t *data
= resp
.d
.asBytes
;
301 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
304 PrintAndLog("Failed reading block: (%02x)", isOK
);
307 PrintAndLog("Command execute time-out");
314 // Mifare Ultralight / Ultralight-C; Read and Dump Card Contents
316 int CmdHF14AMfUDump(const char *Cmd
){
319 char filename
[FILE_PATH_SIZE
] = {0x00};
320 char * fnameptr
= filename
;
322 uint8_t *lockbytes_t
= NULL
;
323 uint8_t lockbytes
[2] = {0x00};
325 uint8_t *lockbytes_t2
= NULL
;
326 uint8_t lockbytes2
[2] = {0x00};
328 bool bit
[16] = {0x00};
329 bool bit2
[16] = {0x00};
335 bool tmplockbit
= false;
337 uint8_t *data
= NULL
;
339 char cmdp
= param_getchar(Cmd
, 0);
341 if (cmdp
== 'h' || cmdp
== 'H') {
342 PrintAndLog("Reads all pages from Mifare Ultralight or Ultralight-C tag.");
343 PrintAndLog("It saves binary dump into the file `filename.bin` or `cardUID.bin`");
344 PrintAndLog("Usage: hf mfu dump <c> <filename w/o .bin>");
345 PrintAndLog(" <c> optional cardtype c == Ultralight-C, Defaults to Ultralight");
346 PrintAndLog(" sample: hf mfu dump");
347 PrintAndLog(" : hf mfu dump myfile");
348 PrintAndLog(" : hf mfu dump c myfile");
353 Pages
= (cmdp
== 'c' || cmdp
== 'C') ? 44 : 16;
355 uint8_t data2
[16] = {0x00};
356 TagTypeUL_t tagtype
= GetHF14AMfU_Type(data2
, sizeof(data2
));
360 PrintAndLog("Dumping Ultralight_C Card Data...");
364 PrintAndLog("Dumping Ultralight EV1_48 Card Data...");
368 PrintAndLog("Dumping Ultralight EV1_128 Card Data...");
372 PrintAndLog("Dumping Ultralight (Magic) Card Data...");
376 PrintAndLog("Dumping Ultralight_C (Magic) Card Data...");
381 PrintAndLog("Dumping Ultralight Card Data...");
384 //PrintAndLog("Dumping Ultralight%s Card Data...", (Pages ==16)?"":"-C");
386 UsbCommand c
= {CMD_MIFAREU_READCARD
, {BlockNo
,Pages
}};
390 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
391 isOK
= resp
.arg
[0] & 0xff;
393 PrintAndLog("Command error");
396 data
= resp
.d
.asBytes
;
398 PrintAndLog("Command execute time-out");
405 lockbytes_t
= data
+ 8;
406 lockbytes
[0] = lockbytes_t
[2];
407 lockbytes
[1] = lockbytes_t
[3];
408 for(j
= 0; j
< 16; j
++){
409 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
412 // Load bottom lockbytes if available
415 lockbytes_t2
= data
+ (40*4);
416 lockbytes2
[0] = lockbytes_t2
[2];
417 lockbytes2
[1] = lockbytes_t2
[3];
418 for (j
= 0; j
< 16; j
++) {
419 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
423 for (i
= 0; i
< Pages
; ++i
) {
426 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
431 case 3: tmplockbit
= bit
[4]; break;
432 case 4: tmplockbit
= bit
[3]; break;
433 case 5: tmplockbit
= bit
[2]; break;
434 case 6: tmplockbit
= bit
[1]; break;
435 case 7: tmplockbit
= bit
[0]; break;
436 case 8: tmplockbit
= bit
[15]; break;
437 case 9: tmplockbit
= bit
[14]; break;
438 case 10: tmplockbit
= bit
[13]; break;
439 case 11: tmplockbit
= bit
[12]; break;
440 case 12: tmplockbit
= bit
[11]; break;
441 case 13: tmplockbit
= bit
[10]; break;
442 case 14: tmplockbit
= bit
[9]; break;
443 case 15: tmplockbit
= bit
[8]; break;
447 case 19: tmplockbit
= bit2
[6]; break;
451 case 23: tmplockbit
= bit2
[5]; break;
455 case 27: tmplockbit
= bit2
[4]; break;
459 case 31: tmplockbit
= bit2
[2]; break;
463 case 35: tmplockbit
= bit2
[1]; break;
467 case 39: tmplockbit
= bit2
[0]; break;
468 case 40: tmplockbit
= bit2
[12]; break;
469 case 41: tmplockbit
= bit2
[11]; break;
470 case 42: tmplockbit
= bit2
[10]; break; //auth0
471 case 43: tmplockbit
= bit2
[9]; break; //auth1
474 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
479 len
= param_getstr(Cmd
,0,filename
);
481 len
= param_getstr(Cmd
,1,filename
);
483 if (len
> FILE_PATH_SIZE
-5) len
= FILE_PATH_SIZE
-5;
485 // user supplied filename?
488 // UID = data 0-1-2 4-5-6-7 (skips a beat)
489 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
490 data
[0],data
[1], data
[2], data
[4],data
[5],data
[6], data
[7]);
493 sprintf(fnameptr
+ len
," .bin");
497 if ((fout
= fopen(filename
,"wb")) == NULL
) {
498 PrintAndLog("Could not create file name %s", filename
);
501 fwrite( data
, 1, Pages
*4, fout
);
504 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
508 // Needed to Authenticate to Ultralight C tags
509 void rol (uint8_t *data
, const size_t len
){
510 uint8_t first
= data
[0];
511 for (size_t i
= 0; i
< len
-1; i
++) {
517 //-------------------------------------------------------------------------------
518 // Ultralight C Methods
519 //-------------------------------------------------------------------------------
522 // Ultralight C Authentication Demo {currently uses hard-coded key}
524 int CmdHF14AMfucAuth(const char *Cmd
){
529 char cmdp
= param_getchar(Cmd
, 0);
531 //Change key to user defined one
532 if (cmdp
== 'k' || cmdp
== 'K'){
533 keyNo
= param_get8(Cmd
, 1);
538 if (cmdp
== 'h' || cmdp
== 'H')
542 PrintAndLog("Usage: hf mfu cauth k <key number>");
543 PrintAndLog(" 0 (default): 3DES standard key");
544 PrintAndLog(" 1 : all 0x00 key");
545 PrintAndLog(" 2 : 0x00-0x0F key");
546 PrintAndLog(" 3 : nfc key");
547 PrintAndLog(" 4 : all 0x01 key");
548 PrintAndLog(" 5 : all 0xff key");
549 PrintAndLog(" 6 : 0x00-0xFF key");
550 PrintAndLog("\n sample : hf mfu cauth k");
551 PrintAndLog(" : hf mfu cauth k 3");
555 uint8_t *key
= default_3des_keys
[keyNo
];
556 if (try3DesAuthentication(key
))
557 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 8));
559 PrintAndLog("Authentication failed");
564 int try3DesAuthentication( uint8_t *key
){
569 des3_context ctx
= { 0 };
571 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
572 uint8_t random_b
[8] = { 0 };
573 uint8_t enc_random_b
[8] = { 0 };
574 uint8_t rnd_ab
[16] = { 0 };
575 uint8_t iv
[8] = { 0 };
577 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
580 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
581 if ( !(resp
.arg
[0] & 0xff) ) return -2;
584 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
586 des3_set2key_dec(&ctx
, key
);
587 // context, mode, length, IV, input, output
588 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
591 memcpy(rnd_ab
,random_a
,8);
592 memcpy(rnd_ab
+8,random_b
,8);
594 des3_set2key_enc(&ctx
, key
);
595 // context, mode, length, IV, input, output
596 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
599 c
.cmd
= CMD_MIFAREUC_AUTH2
;
601 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
604 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
605 if ( !(resp
.arg
[0] & 0xff)) return -2;
607 uint8_t enc_resp
[8] = { 0 };
608 uint8_t resp_random_a
[8] = { 0 };
609 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
611 des3_set2key_dec(&ctx
, key
);
612 // context, mode, length, IV, input, output
613 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
615 if ( !memcmp(resp_random_a
, random_a
, 8))
619 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
620 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
621 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
622 //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16));
623 //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16));
624 //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8));
628 A test function to validate that the polarssl-function works the same
629 was as the openssl-implementation.
630 Commented out, since it requires openssl
632 int CmdTestDES(const char * cmd)
634 uint8_t key[16] = {0x00};
636 memcpy(key,key3_3des_data,16);
637 DES_cblock RndA, RndB;
639 PrintAndLog("----------OpenSSL DES implementation----------");
641 uint8_t e_RndB[8] = {0x00};
642 unsigned char RndARndB[16] = {0x00};
644 DES_cblock iv = { 0 };
645 DES_key_schedule ks1,ks2;
646 DES_cblock key1,key2;
648 memcpy(key,key3_3des_data,16);
650 memcpy(key2,key+8,8);
653 DES_set_key((DES_cblock *)key1,&ks1);
654 DES_set_key((DES_cblock *)key2,&ks2);
656 DES_random_key(&RndA);
657 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
658 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
659 //void DES_ede2_cbc_encrypt(const unsigned char *input,
660 // unsigned char *output, long length, DES_key_schedule *ks1,
661 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
662 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
664 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
666 memcpy(RndARndB,RndA,8);
667 memcpy(RndARndB+8,RndB,8);
668 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
669 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
670 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
673 PrintAndLog("----------PolarSSL implementation----------");
675 uint8_t random_a[8] = { 0 };
676 uint8_t enc_random_a[8] = { 0 };
677 uint8_t random_b[8] = { 0 };
678 uint8_t enc_random_b[8] = { 0 };
679 uint8_t random_a_and_b[16] = { 0 };
680 des3_context ctx = { 0 };
682 memcpy(random_a, RndA,8);
684 uint8_t output[8] = { 0 };
685 uint8_t iv[8] = { 0 };
687 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
688 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
690 des3_set2key_dec(&ctx, key);
692 des3_crypt_cbc(&ctx // des3_context *ctx
693 , DES_DECRYPT // int mode
694 , sizeof(random_b) // size_t length
695 , iv // unsigned char iv[8]
696 , enc_random_b // const unsigned char *input
697 , random_b // unsigned char *output
700 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
703 memcpy(random_a_and_b ,random_a,8);
704 memcpy(random_a_and_b+8,random_b,8);
706 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
708 des3_set2key_enc(&ctx, key);
710 des3_crypt_cbc(&ctx // des3_context *ctx
711 , DES_ENCRYPT // int mode
712 , sizeof(random_a_and_b) // size_t length
713 , enc_random_b // unsigned char iv[8]
714 , random_a_and_b // const unsigned char *input
715 , random_a_and_b // unsigned char *output
718 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
724 // Ultralight C Read Single Block
726 int CmdHF14AMfUCRdBl(const char *Cmd
)
730 uint8_t blockNo
= -1;
731 unsigned char key
[16];
732 char cmdp
= param_getchar(Cmd
, 0);
734 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
735 PrintAndLog("Usage: hf mfu crdbl <block number> <password>");
737 PrintAndLog("sample: hf mfu crdbl 0");
738 PrintAndLog(" hf mfu crdbl 0 112233445566778899AABBCCDDEEFF");
742 blockNo
= param_get8(Cmd
, 0);
744 PrintAndLog("Wrong block number");
748 if (blockNo
> MAX_ULC_BLOCKS
){
749 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
754 if ( strlen(Cmd
) > 3){
755 if (param_gethex(Cmd
, 1, key
, 32)) {
756 PrintAndLog("Key must include %d HEX symbols", 32);
764 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
767 memcpy(c
.d
.asBytes
,key
,16);
771 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
772 uint8_t isOK
= resp
.arg
[0] & 0xff;
774 uint8_t *data
= resp
.d
.asBytes
;
775 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
778 PrintAndLog("Failed reading block: (%02x)", isOK
);
781 PrintAndLog("Command execute time-out");
787 // Mifare Ultralight C Write Single Block
789 int CmdHF14AMfUCWrBl(const char *Cmd
){
791 uint8_t blockNo
= -1;
792 bool chinese_card
= FALSE
;
793 uint8_t bldata
[16] = {0x00};
796 char cmdp
= param_getchar(Cmd
, 0);
798 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
799 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
800 PrintAndLog(" [block number]");
801 PrintAndLog(" [block data] - (8 hex symbols)");
802 PrintAndLog(" [w] - Chinese magic ultralight tag");
804 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
809 blockNo
= param_get8(Cmd
, 0);
810 if (blockNo
> MAX_ULC_BLOCKS
){
811 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
815 if (param_gethex(Cmd
, 1, bldata
, 8)) {
816 PrintAndLog("Block data must include 8 HEX symbols");
820 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
824 if ( blockNo
<= 3 ) {
826 PrintAndLog("Access Denied");
828 PrintAndLog("--Special block no: 0x%02x", blockNo
);
829 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
830 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
831 memcpy(d
.d
.asBytes
,bldata
, 4);
833 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
834 uint8_t isOK
= resp
.arg
[0] & 0xff;
835 PrintAndLog("isOk:%02x", isOK
);
837 PrintAndLog("Command execute timeout");
841 PrintAndLog("--Block no : 0x%02x", blockNo
);
842 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
843 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
844 memcpy(e
.d
.asBytes
,bldata
, 4);
846 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
847 uint8_t isOK
= resp
.arg
[0] & 0xff;
848 PrintAndLog("isOk : %02x", isOK
);
850 PrintAndLog("Command execute timeout");
857 // Mifare Ultralight C - Set password
859 int CmdHF14AMfucSetPwd(const char *Cmd
){
861 uint8_t pwd
[16] = {0x00};
863 char cmdp
= param_getchar(Cmd
, 0);
865 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
866 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
867 PrintAndLog(" [password] - (32 hex symbols)");
869 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
874 if (param_gethex(Cmd
, 0, pwd
, 32)) {
875 PrintAndLog("Password must include 32 HEX symbols");
879 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
880 memcpy( c
.d
.asBytes
, pwd
, 16);
885 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
886 if ( (resp
.arg
[0] & 0xff) == 1)
887 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
889 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
894 PrintAndLog("command execution time out");
902 // Mifare Ultraligh - Set UID
904 int CmdHF14AMfucSetUid(const char *Cmd
){
908 uint8_t uid
[7] = {0x00};
909 char cmdp
= param_getchar(Cmd
, 0);
911 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
912 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
913 PrintAndLog(" [uid] - (14 hex symbols)");
914 PrintAndLog("\nThis only works for Magic Ultralight tags.");
916 PrintAndLog("sample: hf mfu setuid 11223344556677");
921 if (param_gethex(Cmd
, 0, uid
, 14)) {
922 PrintAndLog("UID must include 14 HEX symbols");
927 c
.cmd
= CMD_MIFAREU_READBL
;
930 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
931 PrintAndLog("Command execute timeout");
936 uint8_t oldblock2
[4] = {0x00};
937 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
940 c
.cmd
= CMD_MIFAREU_WRITEBL
;
942 c
.d
.asBytes
[0] = uid
[0];
943 c
.d
.asBytes
[1] = uid
[1];
944 c
.d
.asBytes
[2] = uid
[2];
945 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
947 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
948 PrintAndLog("Command execute timeout");
954 c
.d
.asBytes
[0] = uid
[3];
955 c
.d
.asBytes
[1] = uid
[4];
956 c
.d
.asBytes
[2] = uid
[5];
957 c
.d
.asBytes
[3] = uid
[6];
959 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
960 PrintAndLog("Command execute timeout");
966 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
967 c
.d
.asBytes
[1] = oldblock2
[1];
968 c
.d
.asBytes
[2] = oldblock2
[2];
969 c
.d
.asBytes
[3] = oldblock2
[3];
971 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
972 PrintAndLog("Command execute timeout");
979 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
981 uint8_t iv
[8] = { 0x00 };
982 uint8_t block
= 0x07;
985 //04 57 b6 e2 05 3f 80 UID
987 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
988 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
989 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
990 uint8_t dkeyA
[8] = { 0x00 };
991 uint8_t dkeyB
[8] = { 0x00 };
993 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
995 uint8_t mix
[8] = { 0x00 };
996 uint8_t divkey
[8] = { 0x00 };
998 memcpy(mix
, mifarekeyA
, 4);
1000 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1001 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1002 mix
[6] = block
^ uid
[2];
1005 des3_context ctx
= { 0x00 };
1006 des3_set2key_enc(&ctx
, masterkey
);
1008 des3_crypt_cbc(&ctx
// des3_context
1009 , DES_ENCRYPT
// int mode
1010 , sizeof(mix
) // length
1016 PrintAndLog("3DES version");
1017 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
1018 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
1019 PrintAndLog("Sector :\t %0d", block
);
1020 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
1021 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
1022 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
1024 PrintAndLog("\n DES version");
1026 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
1027 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
1028 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
1031 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
1032 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1033 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1036 uint8_t zeros
[8] = {0x00};
1037 uint8_t newpwd
[8] = {0x00};
1038 uint8_t dmkey
[24] = {0x00};
1039 memcpy(dmkey
, dkeyA
, 8);
1040 memcpy(dmkey
+8, dkeyB
, 8);
1041 memcpy(dmkey
+16, dkeyA
, 8);
1042 memset(iv
, 0x00, 8);
1044 des3_set3key_enc(&ctx
, dmkey
);
1046 des3_crypt_cbc(&ctx
// des3_context
1047 , DES_ENCRYPT
// int mode
1048 , sizeof(newpwd
) // length
1054 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1055 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1056 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1057 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1062 // static uint8_t * diversify_key(uint8_t * key){
1064 // for(int i=0; i<16; i++){
1065 // if(i<=6) key[i]^=cuid[i];
1066 // if(i>6) key[i]^=cuid[i%7];
1071 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1072 // for (int i=0; i<len; ++i){
1078 //------------------------------------
1080 //------------------------------------
1081 static command_t CommandTable
[] =
1083 {"help", CmdHelp
, 1,"This help"},
1084 {"dbg", CmdHF14AMfDbg
, 0,"Set default debug mode"},
1085 {"info", CmdHF14AMfUInfo
, 0,"Taginfo"},
1086 {"dump", CmdHF14AMfUDump
, 0,"Dump MIFARE Ultralight / Ultralight-C tag to binary file"},
1087 {"rdbl", CmdHF14AMfURdBl
, 0,"Read block - MIFARE Ultralight"},
1088 {"wrbl", CmdHF14AMfUWrBl
, 0,"Write block - MIFARE Ultralight"},
1089 {"crdbl", CmdHF14AMfUCRdBl
, 0,"Read block - MIFARE Ultralight C"},
1090 {"cwrbl", CmdHF14AMfUCWrBl
, 0,"Write block - MIFARE Ultralight C"},
1091 {"cauth", CmdHF14AMfucAuth
, 0,"Ultralight C Authentication"},
1092 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password [Ultralight-C only]"},
1093 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID"},
1094 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1095 {NULL
, NULL
, 0, NULL
}
1098 int CmdHFMFUltra(const char *Cmd
){
1099 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1100 CmdsParse(CommandTable
, Cmd
);
1104 int CmdHelp(const char *Cmd
){
1105 CmdsHelp(CommandTable
);