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"
17 #define MAX_UL_BLOCKS 0x0f
18 #define MAX_ULC_BLOCKS 0x2f
19 #define MAX_ULEV1a_BLOCKS 0x0b;
20 #define MAX_ULEV1b_BLOCKS 0x20;
22 uint8_t default_3des_keys
[7][16] = {
23 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
24 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
25 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
26 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
27 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
28 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
29 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
32 static int CmdHelp(const char *Cmd
);
34 // return 1 if tag responded to 0x1A.
35 uint8_t requestAuthentication( uint8_t* nonce
){
37 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_RAW
| ISO14A_APPEND_CRC
,2 ,0}};
38 c
.d
.asBytes
[0] = 0x1A;
39 c
.d
.asBytes
[1] = 0x00;
42 WaitForResponse(CMD_ACK
, &resp
); // skip select answer.
44 if ( !(resp
.arg
[0] & 0xff) )
47 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
49 if ( resp
.arg
[0] & 0xff ) {
50 memcpy(nonce
, resp
.d
.asBytes
+1, 8);
57 typedef enum TAGTYPE_UL
{
64 UL_MAGIC
= UL
| MAGIC
,
65 UL_C_MAGIC
= UL_C
| MAGIC
,
69 uint8_t GetHF14AMfU_Type(void){
71 TagTypeUL_t tagtype
= UNKNOWN
;
72 iso14a_card_select_t card
;
74 // select and run 0x60 (GET_VERSION - EV1 command)
75 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_RAW
| ISO14A_APPEND_CRC
, 1, 0}};
76 c
.d
.asBytes
[0] = 0x60;
79 WaitForResponse(CMD_ACK
, &resp
);
81 if ( resp
.arg
[0] == 0 ) return UL_ERROR
;
83 memcpy(&card
, (iso14a_card_select_t
*)resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
85 // Ultralight - ATQA / SAK
86 if ( card
.atqa
[1] != 0x00 && card
.atqa
[0] != 0x44 && card
.sak
!= 0x00 ) return UL_ERROR
;
89 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
91 uint8_t version
[10] = {0,0,0,0,0,0,0,0,0,0};
92 memcpy(version
, resp
.d
.asBytes
, resp
.arg
[0] < sizeof(version
) ? resp
.arg
[0] : sizeof(version
));
93 uint8_t len
= resp
.arg
[0] & 0xff;
95 if ( len
== 0x0A && version
[6] == 0x0B )
97 else if ( len
== 0x0A && version
[6] != 0x0B )
99 else if ( len
== 0x01 )
101 else if ( len
== 0x00 )
105 // Magic UL-C, mine have a static nonce response to 0x1A command.
106 uint8_t nonce1
[8] = {0,0,0,0,0,0,0,0};
107 uint8_t nonce2
[8] = {0,0,0,0,0,0,0,0};
108 uint8_t status
= requestAuthentication(nonce1
);
110 requestAuthentication(nonce2
);
111 if ( !memcmp(nonce1
, nonce2
, 8) ){
112 tagtype
= UL_C_MAGIC
;
115 // Magic Ultralight test here - TODO
120 int CmdHF14AMfUInfo(const char *Cmd
){
122 uint8_t datatemp
[7] = {0x00};
124 uint8_t data
[16] = {0x00};
127 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
128 if (tagtype
== UL_ERROR
) return -1;
130 PrintAndLog("\n-- Tag Information ---------");
131 PrintAndLog("-------------------------------------------------------------");
134 PrintAndLog(" TYPE : MIFARE Ultralight %s", (tagtype
& MAGIC
)?"(magic)":"");
135 else if ( tagtype
& UL_C
)
136 PrintAndLog(" TYPE : MIFARE Ultralight C %s", (tagtype
& MAGIC
)?"(magic)":"" );
137 else if ( tagtype
& UL_EV1_48
)
138 PrintAndLog(" TYPE : MIFARE Ultralight EV1 48 bytes");
139 else if ( tagtype
& UL_EV1_128
)
140 PrintAndLog(" TYPE : MIFARE Ultralight EV1 128 bytes");
142 PrintAndLog(" TYPE : Unknown %x",tagtype
);
144 // read pages 0,1,2,4
145 UsbCommand c
= {CMD_MIFAREU_READCARD
, {0, 4}};
149 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
150 isOK
= resp
.arg
[0] & 0xff;
151 memcpy(data
, resp
.d
.asBytes
, sizeof(data
));
154 PrintAndLog("Error reading from tag");
158 PrintAndLog("Command execute timed out");
163 memcpy( datatemp
, data
, 3);
164 memcpy( datatemp
+3, data
+4, 4);
166 PrintAndLog(" UID : %s ", sprint_hex(datatemp
, 7));
167 PrintAndLog(" UID[0] : (Manufacturer Byte) = %02x, Manufacturer: %s", datatemp
[0], getTagInfo(datatemp
[0]) );
170 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
171 int crc0
= 0x88 ^ data
[0] ^ data
[1] ^data
[2];
172 if ( data
[3] == crc0
)
173 PrintAndLog(" BCC0 : %02x - Ok", data
[3]);
175 PrintAndLog(" BCC0 : %02x - crc should be %02x", data
[3], crc0
);
177 int crc1
= data
[4] ^ data
[5] ^ data
[6] ^data
[7];
178 if ( data
[8] == crc1
)
179 PrintAndLog(" BCC1 : %02x - Ok", data
[8]);
181 PrintAndLog(" BCC1 : %02x - crc should be %02x", data
[8], crc1
);
183 PrintAndLog(" Internal : %s ", sprint_hex(data
+ 9, 1));
185 memcpy(datatemp
, data
+10, 2);
186 PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp
, 2),printBits( 2, &datatemp
) );
187 PrintAndLog("OneTimePad : %s ", sprint_hex(data
+ 3*4, 4));
192 if ((tagtype
& UL_C
)){
194 PrintAndLog("Trying some default 3des keys");
196 for (uint8_t i
= 0; i
< 5; ++i
){
197 key
= default_3des_keys
[i
];
198 if (try3DesAuthentication(key
)){
199 PrintAndLog("Found default 3des key: %s", sprint_hex(key
,16));
204 else if ((tagtype
& (UL_EV1_48
| UL_EV1_128
))) {
205 //********** TODO ********************************
206 // --problem, there is a failed pwd tries counter in UL-EV1
207 //PrintAndLog("Trying some known EV1 passwords.");
213 // Mifare Ultralight Write Single Block
215 int CmdHF14AMfUWrBl(const char *Cmd
){
216 uint8_t blockNo
= -1;
217 bool chinese_card
= FALSE
;
218 uint8_t bldata
[16] = {0x00};
221 char cmdp
= param_getchar(Cmd
, 0);
222 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
223 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
224 PrintAndLog(" [block number]");
225 PrintAndLog(" [block data] - (8 hex symbols)");
226 PrintAndLog(" [w] - Chinese magic ultralight tag");
228 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
233 blockNo
= param_get8(Cmd
, 0);
235 if (blockNo
> MAX_UL_BLOCKS
){
236 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
240 if (param_gethex(Cmd
, 1, bldata
, 8)) {
241 PrintAndLog("Block data must include 8 HEX symbols");
245 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
251 PrintAndLog("Access Denied");
253 PrintAndLog("--specialblock no:%02x", blockNo
);
254 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
255 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
256 memcpy(d
.d
.asBytes
,bldata
, 4);
258 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
259 uint8_t isOK
= resp
.arg
[0] & 0xff;
260 PrintAndLog("isOk:%02x", isOK
);
262 PrintAndLog("Command execute timeout");
266 PrintAndLog("--block no:%02x", blockNo
);
267 PrintAndLog("--data: %s", sprint_hex(bldata
, 4));
268 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
269 memcpy(e
.d
.asBytes
,bldata
, 4);
271 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
272 uint8_t isOK
= resp
.arg
[0] & 0xff;
273 PrintAndLog("isOk:%02x", isOK
);
275 PrintAndLog("Command execute timeout");
282 // Mifare Ultralight Read Single Block
284 int CmdHF14AMfURdBl(const char *Cmd
){
287 uint8_t blockNo
= -1;
288 char cmdp
= param_getchar(Cmd
, 0);
290 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
291 PrintAndLog("Usage: hf mfu rdbl <block number>");
292 PrintAndLog(" sample: hfu mfu rdbl 0");
296 blockNo
= param_get8(Cmd
, 0);
298 if (blockNo
> MAX_UL_BLOCKS
){
299 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
303 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
307 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
308 uint8_t isOK
= resp
.arg
[0] & 0xff;
310 uint8_t *data
= resp
.d
.asBytes
;
311 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
314 PrintAndLog("Failed reading block: (%02x)", isOK
);
317 PrintAndLog("Command execute time-out");
323 int usage_hf_mfu_dump(void)
325 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
326 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
327 PrintAndLog("It autodetects card type.\n");
328 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
329 PrintAndLog(" sample : hf mfu dump");
330 PrintAndLog(" : hf mfu dump n myfile");
334 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
335 // Read and Dump Card Contents, using auto detection of tag size.
337 // TODO: take a password to read UL-C / UL-EV1 tags.
338 int CmdHF14AMfUDump(const char *Cmd
){
341 char filename
[FILE_PATH_SIZE
] = {0x00};
342 char *fnameptr
= filename
;
343 char *str
= "Dumping Ultralight%s%s Card Data...";
344 uint8_t *lockbytes_t
= NULL
;
345 uint8_t lockbytes
[2] = {0x00};
346 uint8_t *lockbytes_t2
= NULL
;
347 uint8_t lockbytes2
[2] = {0x00};
348 bool bit
[16] = {0x00};
349 bool bit2
[16] = {0x00};
350 uint8_t data
[1024] = {0x00};
354 bool tmplockbit
= false;
361 while(param_getchar(Cmd
, cmdp
) != 0x00)
363 switch(param_getchar(Cmd
, cmdp
))
367 return usage_hf_mfu_dump();
370 dataLen
= param_gethex(Cmd
, cmdp
+1, data
, 32);
374 key
= SwapEndian64(data
, 16);
375 PrintAndLog("3des key: %s",sprint_hex(key
, 16));
382 fileNlen
= param_getstr(Cmd
, cmdp
+1, filename
);
383 if (!fileNlen
) errors
= true;
384 if (fileNlen
> FILE_PATH_SIZE
-5) fileNlen
= FILE_PATH_SIZE
-5;
388 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
398 return usage_hf_mfu_dump();
401 TagTypeUL_t tagtype
= GetHF14AMfU_Type();
402 if (tagtype
== UL_ERROR
) return -1;
404 if ( tagtype
& UL
) {
406 PrintAndLog(str
,"", (tagtype
& MAGIC
)?" (magic)":"" );
408 else if ( tagtype
& UL_C
) {
410 PrintAndLog(str
,"-C", (tagtype
& MAGIC
)?" (magic)":"" );
412 else if ( tagtype
& UL_EV1_48
) {
414 PrintAndLog(str
," EV1_48","");
416 else if ( tagtype
& UL_EV1_128
) {
418 PrintAndLog(str
," EV1_128","");
421 PrintAndLog("Dumping unknown Ultralight, using default values.");
424 for (uint8_t i
= 0; i
<Pages
; i
++){
426 UsbCommand c
= {CMD_MIFAREU_READBL
, {i
}};
429 memcpy(c
.d
.asBytes
,key
,16);
434 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
435 uint8_t isOK
= resp
.arg
[0] & 0xff;
437 memcpy(data
+ (i
*4), resp
.d
.asBytes
, 4);
440 PrintAndLog("Failed reading block: (%02x)", i
);
443 PrintAndLog("Command execute time-out");
450 lockbytes_t
= data
+ 8;
451 lockbytes
[0] = lockbytes_t
[2];
452 lockbytes
[1] = lockbytes_t
[3];
453 for(j
= 0; j
< 16; j
++){
454 bit
[j
] = lockbytes
[j
/8] & ( 1 <<(7-j
%8));
457 // Load bottom lockbytes if available
459 lockbytes_t2
= data
+ (40*4);
460 lockbytes2
[0] = lockbytes_t2
[2];
461 lockbytes2
[1] = lockbytes_t2
[3];
462 for (j
= 0; j
< 16; j
++) {
463 bit2
[j
] = lockbytes2
[j
/8] & ( 1 <<(7-j
%8));
469 memcpy(data
+ Pages
*4, key
, 16);
472 for (i
= 0; i
< Pages
; ++i
) {
474 PrintAndLog("Block %02x:%s ", i
,sprint_hex(data
+ i
* 4, 4));
478 case 3: tmplockbit
= bit
[4]; break;
479 case 4: tmplockbit
= bit
[3]; break;
480 case 5: tmplockbit
= bit
[2]; break;
481 case 6: tmplockbit
= bit
[1]; break;
482 case 7: tmplockbit
= bit
[0]; break;
483 case 8: tmplockbit
= bit
[15]; break;
484 case 9: tmplockbit
= bit
[14]; break;
485 case 10: tmplockbit
= bit
[13]; break;
486 case 11: tmplockbit
= bit
[12]; break;
487 case 12: tmplockbit
= bit
[11]; break;
488 case 13: tmplockbit
= bit
[10]; break;
489 case 14: tmplockbit
= bit
[9]; break;
490 case 15: tmplockbit
= bit
[8]; break;
494 case 19: tmplockbit
= bit2
[6]; break;
498 case 23: tmplockbit
= bit2
[5]; break;
502 case 27: tmplockbit
= bit2
[4]; break;
506 case 31: tmplockbit
= bit2
[2]; break;
510 case 35: tmplockbit
= bit2
[1]; break;
514 case 39: tmplockbit
= bit2
[0]; break;
515 case 40: tmplockbit
= bit2
[12]; break;
516 case 41: tmplockbit
= bit2
[11]; break;
517 case 42: tmplockbit
= bit2
[10]; break; //auth0
518 case 43: tmplockbit
= bit2
[9]; break; //auth1
521 PrintAndLog("Block %02x:%s [%d]", i
,sprint_hex(data
+ i
* 4, 4),tmplockbit
);
524 // user supplied filename?
526 // UID = data 0-1-2 4-5-6-7 (skips a beat)
527 sprintf(fnameptr
,"%02X%02X%02X%02X%02X%02X%02X.bin",
528 data
[0],data
[1], data
[2], data
[4],data
[5],data
[6], data
[7]);
530 sprintf(fnameptr
+ fileNlen
," .bin");
533 if ((fout
= fopen(filename
,"wb")) == NULL
) {
534 PrintAndLog("Could not create file name %s", filename
);
537 fwrite( data
, 1, Pages
*4, fout
);
540 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages
, Pages
*4, filename
);
544 // Needed to Authenticate to Ultralight C tags
545 void rol (uint8_t *data
, const size_t len
){
546 uint8_t first
= data
[0];
547 for (size_t i
= 0; i
< len
-1; i
++) {
553 //-------------------------------------------------------------------------------
554 // Ultralight C Methods
555 //-------------------------------------------------------------------------------
558 // Ultralight C Authentication Demo {currently uses hard-coded key}
560 int CmdHF14AMfucAuth(const char *Cmd
){
565 char cmdp
= param_getchar(Cmd
, 0);
567 //Change key to user defined one
568 if (cmdp
== 'k' || cmdp
== 'K'){
569 keyNo
= param_get8(Cmd
, 1);
574 if (cmdp
== 'h' || cmdp
== 'H')
578 PrintAndLog("Usage: hf mfu cauth k <key number>");
579 PrintAndLog(" 0 (default): 3DES standard key");
580 PrintAndLog(" 1 : all 0x00 key");
581 PrintAndLog(" 2 : 0x00-0x0F key");
582 PrintAndLog(" 3 : nfc key");
583 PrintAndLog(" 4 : all 0x01 key");
584 PrintAndLog(" 5 : all 0xff key");
585 PrintAndLog(" 6 : 0x00-0xFF key");
586 PrintAndLog("\n sample : hf mfu cauth k");
587 PrintAndLog(" : hf mfu cauth k 3");
591 uint8_t *key
= default_3des_keys
[keyNo
];
592 if (try3DesAuthentication(key
)>0)
593 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key
, 16));
595 PrintAndLog("Authentication failed");
600 int try3DesAuthentication( uint8_t *key
){
605 des3_context ctx
= { 0 };
607 uint8_t random_a
[8] = { 1,1,1,1,1,1,1,1 };
608 uint8_t random_b
[8] = { 0 };
609 uint8_t enc_random_b
[8] = { 0 };
610 uint8_t rnd_ab
[16] = { 0 };
611 uint8_t iv
[8] = { 0 };
613 UsbCommand c
= {CMD_MIFAREUC_AUTH1
, {blockNo
}};
616 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500) ) return -1;
617 if ( !(resp
.arg
[0] & 0xff) ) return -2;
620 memcpy(enc_random_b
,resp
.d
.asBytes
+1,8);
622 des3_set2key_dec(&ctx
, key
);
623 // context, mode, length, IV, input, output
624 des3_crypt_cbc( &ctx
, DES_DECRYPT
, sizeof(random_b
), iv
, enc_random_b
, random_b
);
627 memcpy(rnd_ab
,random_a
,8);
628 memcpy(rnd_ab
+8,random_b
,8);
630 des3_set2key_enc(&ctx
, key
);
631 // context, mode, length, IV, input, output
632 des3_crypt_cbc(&ctx
, DES_ENCRYPT
, sizeof(rnd_ab
), enc_random_b
, rnd_ab
, rnd_ab
);
635 c
.cmd
= CMD_MIFAREUC_AUTH2
;
637 memcpy(c
.d
.asBytes
, rnd_ab
, 16);
640 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) return -1;
641 if ( !(resp
.arg
[0] & 0xff)) return -2;
643 uint8_t enc_resp
[8] = { 0 };
644 uint8_t resp_random_a
[8] = { 0 };
645 memcpy(enc_resp
, resp
.d
.asBytes
+1, 8);
647 des3_set2key_dec(&ctx
, key
);
648 // context, mode, length, IV, input, output
649 des3_crypt_cbc( &ctx
, DES_DECRYPT
, 8, enc_random_b
, enc_resp
, resp_random_a
);
651 if ( !memcmp(resp_random_a
, random_a
, 8))
655 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
656 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
657 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
658 //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16));
659 //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16));
660 //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8));
664 A test function to validate that the polarssl-function works the same
665 was as the openssl-implementation.
666 Commented out, since it requires openssl
668 int CmdTestDES(const char * cmd)
670 uint8_t key[16] = {0x00};
672 memcpy(key,key3_3des_data,16);
673 DES_cblock RndA, RndB;
675 PrintAndLog("----------OpenSSL DES implementation----------");
677 uint8_t e_RndB[8] = {0x00};
678 unsigned char RndARndB[16] = {0x00};
680 DES_cblock iv = { 0 };
681 DES_key_schedule ks1,ks2;
682 DES_cblock key1,key2;
684 memcpy(key,key3_3des_data,16);
686 memcpy(key2,key+8,8);
689 DES_set_key((DES_cblock *)key1,&ks1);
690 DES_set_key((DES_cblock *)key2,&ks2);
692 DES_random_key(&RndA);
693 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
694 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
695 //void DES_ede2_cbc_encrypt(const unsigned char *input,
696 // unsigned char *output, long length, DES_key_schedule *ks1,
697 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
698 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
700 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
702 memcpy(RndARndB,RndA,8);
703 memcpy(RndARndB+8,RndB,8);
704 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
705 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
706 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
709 PrintAndLog("----------PolarSSL implementation----------");
711 uint8_t random_a[8] = { 0 };
712 uint8_t enc_random_a[8] = { 0 };
713 uint8_t random_b[8] = { 0 };
714 uint8_t enc_random_b[8] = { 0 };
715 uint8_t random_a_and_b[16] = { 0 };
716 des3_context ctx = { 0 };
718 memcpy(random_a, RndA,8);
720 uint8_t output[8] = { 0 };
721 uint8_t iv[8] = { 0 };
723 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
724 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
726 des3_set2key_dec(&ctx, key);
728 des3_crypt_cbc(&ctx // des3_context *ctx
729 , DES_DECRYPT // int mode
730 , sizeof(random_b) // size_t length
731 , iv // unsigned char iv[8]
732 , enc_random_b // const unsigned char *input
733 , random_b // unsigned char *output
736 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
739 memcpy(random_a_and_b ,random_a,8);
740 memcpy(random_a_and_b+8,random_b,8);
742 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
744 des3_set2key_enc(&ctx, key);
746 des3_crypt_cbc(&ctx // des3_context *ctx
747 , DES_ENCRYPT // int mode
748 , sizeof(random_a_and_b) // size_t length
749 , enc_random_b // unsigned char iv[8]
750 , random_a_and_b // const unsigned char *input
751 , random_a_and_b // unsigned char *output
754 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
761 // Ultralight C Read Single Block
763 int CmdHF14AMfUCRdBl(const char *Cmd
)
767 uint8_t blockNo
= -1;
769 char cmdp
= param_getchar(Cmd
, 0);
771 if (strlen(Cmd
) < 1 || cmdp
== 'h' || cmdp
== 'H') {
772 PrintAndLog("Usage: hf mfu crdbl <block number> <password>");
774 PrintAndLog("sample: hf mfu crdbl 0");
775 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
779 blockNo
= param_get8(Cmd
, 0);
781 PrintAndLog("Wrong block number");
785 if (blockNo
> MAX_ULC_BLOCKS
){
786 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
791 if ( strlen(Cmd
) > 3){
792 if (param_gethex(Cmd
, 1, key
, 32)) {
793 PrintAndLog("Key must include %d HEX symbols", 32);
799 uint8_t *key2
= SwapEndian64(key
, 16);
802 UsbCommand c
= {CMD_MIFAREU_READBL
, {blockNo
}};
805 memcpy(c
.d
.asBytes
,key2
,16);
809 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
810 uint8_t isOK
= resp
.arg
[0] & 0xff;
812 uint8_t *data
= resp
.d
.asBytes
;
813 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo
, blockNo
, sprint_hex(data
, 4));
816 PrintAndLog("Failed reading block: (%02x)", isOK
);
819 PrintAndLog("Command execute time-out");
825 // Mifare Ultralight C Write Single Block
827 int CmdHF14AMfUCWrBl(const char *Cmd
){
829 uint8_t blockNo
= -1;
830 bool chinese_card
= FALSE
;
831 uint8_t bldata
[16] = {0x00};
834 char cmdp
= param_getchar(Cmd
, 0);
836 if (strlen(Cmd
) < 3 || cmdp
== 'h' || cmdp
== 'H') {
837 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
838 PrintAndLog(" [block number]");
839 PrintAndLog(" [block data] - (8 hex symbols)");
840 PrintAndLog(" [w] - Chinese magic ultralight tag");
842 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
847 blockNo
= param_get8(Cmd
, 0);
848 if (blockNo
> MAX_ULC_BLOCKS
){
849 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
853 if (param_gethex(Cmd
, 1, bldata
, 8)) {
854 PrintAndLog("Block data must include 8 HEX symbols");
858 if (strchr(Cmd
,'w') != 0 || strchr(Cmd
,'W') != 0 ) {
862 if ( blockNo
<= 3 ) {
864 PrintAndLog("Access Denied");
867 PrintAndLog("--Special block no: 0x%02x", blockNo
);
868 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
869 UsbCommand d
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
870 memcpy(d
.d
.asBytes
,bldata
, 4);
872 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
873 uint8_t isOK
= resp
.arg
[0] & 0xff;
874 PrintAndLog("isOk:%02x", isOK
);
876 PrintAndLog("Command execute timeout");
881 PrintAndLog("--Block no : 0x%02x", blockNo
);
882 PrintAndLog("--Data: %s", sprint_hex(bldata
, 4));
883 UsbCommand e
= {CMD_MIFAREU_WRITEBL
, {blockNo
}};
884 memcpy(e
.d
.asBytes
,bldata
, 4);
886 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
887 uint8_t isOK
= resp
.arg
[0] & 0xff;
888 PrintAndLog("isOk : %02x", isOK
);
890 PrintAndLog("Command execute timeout");
898 // Mifare Ultralight C - Set password
900 int CmdHF14AMfucSetPwd(const char *Cmd
){
902 uint8_t pwd
[16] = {0x00};
904 char cmdp
= param_getchar(Cmd
, 0);
906 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
907 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
908 PrintAndLog(" [password] - (32 hex symbols)");
910 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
915 if (param_gethex(Cmd
, 0, pwd
, 32)) {
916 PrintAndLog("Password must include 32 HEX symbols");
920 UsbCommand c
= {CMD_MIFAREUC_SETPWD
};
921 memcpy( c
.d
.asBytes
, pwd
, 16);
926 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
927 if ( (resp
.arg
[0] & 0xff) == 1)
928 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd
,16));
930 PrintAndLog("Failed writing at block %d", resp
.arg
[1] & 0xff);
935 PrintAndLog("command execution time out");
943 // Magic UL / UL-C tags - Set UID
945 int CmdHF14AMfucSetUid(const char *Cmd
){
949 uint8_t uid
[7] = {0x00};
950 char cmdp
= param_getchar(Cmd
, 0);
952 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') {
953 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
954 PrintAndLog(" [uid] - (14 hex symbols)");
955 PrintAndLog("\nThis only works for Magic Ultralight tags.");
957 PrintAndLog("sample: hf mfu setuid 11223344556677");
962 if (param_gethex(Cmd
, 0, uid
, 14)) {
963 PrintAndLog("UID must include 14 HEX symbols");
968 c
.cmd
= CMD_MIFAREU_READBL
;
971 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
972 PrintAndLog("Command execute timeout");
977 uint8_t oldblock2
[4] = {0x00};
978 memcpy(resp
.d
.asBytes
, oldblock2
, 4);
981 c
.cmd
= CMD_MIFAREU_WRITEBL
;
983 c
.d
.asBytes
[0] = uid
[0];
984 c
.d
.asBytes
[1] = uid
[1];
985 c
.d
.asBytes
[2] = uid
[2];
986 c
.d
.asBytes
[3] = 0x88 ^ uid
[0] ^ uid
[1] ^ uid
[2];
988 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
989 PrintAndLog("Command execute timeout");
995 c
.d
.asBytes
[0] = uid
[3];
996 c
.d
.asBytes
[1] = uid
[4];
997 c
.d
.asBytes
[2] = uid
[5];
998 c
.d
.asBytes
[3] = uid
[6];
1000 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1001 PrintAndLog("Command execute timeout");
1007 c
.d
.asBytes
[0] = uid
[3] ^ uid
[4] ^ uid
[5] ^ uid
[6];
1008 c
.d
.asBytes
[1] = oldblock2
[1];
1009 c
.d
.asBytes
[2] = oldblock2
[2];
1010 c
.d
.asBytes
[3] = oldblock2
[3];
1012 if (!WaitForResponseTimeout(CMD_ACK
,&resp
,1500) ) {
1013 PrintAndLog("Command execute timeout");
1020 int CmdHF14AMfuGenDiverseKeys(const char *Cmd
){
1022 uint8_t iv
[8] = { 0x00 };
1023 uint8_t block
= 0x07;
1026 //04 57 b6 e2 05 3f 80 UID
1028 uint8_t uid
[] = { 0xF4,0xEA, 0x54, 0x8E };
1029 uint8_t mifarekeyA
[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1030 uint8_t mifarekeyB
[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1031 uint8_t dkeyA
[8] = { 0x00 };
1032 uint8_t dkeyB
[8] = { 0x00 };
1034 uint8_t masterkey
[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1036 uint8_t mix
[8] = { 0x00 };
1037 uint8_t divkey
[8] = { 0x00 };
1039 memcpy(mix
, mifarekeyA
, 4);
1041 mix
[4] = mifarekeyA
[4] ^ uid
[0];
1042 mix
[5] = mifarekeyA
[5] ^ uid
[1];
1043 mix
[6] = block
^ uid
[2];
1046 des3_context ctx
= { 0x00 };
1047 des3_set2key_enc(&ctx
, masterkey
);
1049 des3_crypt_cbc(&ctx
// des3_context
1050 , DES_ENCRYPT
// int mode
1051 , sizeof(mix
) // length
1057 PrintAndLog("3DES version");
1058 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey
,sizeof(masterkey
)));
1059 PrintAndLog("UID :\t %s", sprint_hex(uid
, sizeof(uid
)));
1060 PrintAndLog("Sector :\t %0d", block
);
1061 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA
, sizeof(mifarekeyA
)));
1062 PrintAndLog("Message :\t %s", sprint_hex(mix
, sizeof(mix
)));
1063 PrintAndLog("Diversified key: %s", sprint_hex(divkey
+1, 6));
1065 PrintAndLog("\n DES version");
1067 for (int i
=0; i
< sizeof(mifarekeyA
); ++i
){
1068 dkeyA
[i
] = (mifarekeyA
[i
] << 1) & 0xff;
1069 dkeyA
[6] |= ((mifarekeyA
[i
] >> 7) & 1) << (i
+1);
1072 for (int i
=0; i
< sizeof(mifarekeyB
); ++i
){
1073 dkeyB
[1] |= ((mifarekeyB
[i
] >> 7) & 1) << (i
+1);
1074 dkeyB
[2+i
] = (mifarekeyB
[i
] << 1) & 0xff;
1077 uint8_t zeros
[8] = {0x00};
1078 uint8_t newpwd
[8] = {0x00};
1079 uint8_t dmkey
[24] = {0x00};
1080 memcpy(dmkey
, dkeyA
, 8);
1081 memcpy(dmkey
+8, dkeyB
, 8);
1082 memcpy(dmkey
+16, dkeyA
, 8);
1083 memset(iv
, 0x00, 8);
1085 des3_set3key_enc(&ctx
, dmkey
);
1087 des3_crypt_cbc(&ctx
// des3_context
1088 , DES_ENCRYPT
// int mode
1089 , sizeof(newpwd
) // length
1095 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA
, sizeof(dkeyA
)));
1096 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB
, sizeof(dkeyB
)));
1097 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey
, sizeof(dmkey
)));
1098 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd
, sizeof(newpwd
)));
1103 // static uint8_t * diversify_key(uint8_t * key){
1105 // for(int i=0; i<16; i++){
1106 // if(i<=6) key[i]^=cuid[i];
1107 // if(i>6) key[i]^=cuid[i%7];
1112 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1113 // for (int i=0; i<len; ++i){
1119 //------------------------------------
1121 //------------------------------------
1122 static command_t CommandTable
[] =
1124 {"help", CmdHelp
, 1, "This help"},
1125 {"dbg", CmdHF14AMfDbg
, 0, "Set default debug mode"},
1126 {"info", CmdHF14AMfUInfo
, 0, "Tag information"},
1127 {"dump", CmdHF14AMfUDump
, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1128 {"rdbl", CmdHF14AMfURdBl
, 0, "Read block - Ultralight"},
1129 {"wrbl", CmdHF14AMfUWrBl
, 0, "Write block - Ultralight"},
1130 {"crdbl", CmdHF14AMfUCRdBl
, 0, "Read block - Ultralight C"},
1131 {"cwrbl", CmdHF14AMfUCWrBl
, 0, "Write block - Ultralight C"},
1132 {"cauth", CmdHF14AMfucAuth
, 0, "Authentication - Ultralight C"},
1133 {"setpwd", CmdHF14AMfucSetPwd
, 1, "Set 3des password - Ultralight-C"},
1134 {"setuid", CmdHF14AMfucSetUid
, 1, "Set UID - MAGIC tags only"},
1135 {"gen", CmdHF14AMfuGenDiverseKeys
, 1, "Generate 3des mifare diversified keys"},
1136 {NULL
, NULL
, 0, NULL
}
1139 int CmdHFMFUltra(const char *Cmd
){
1140 WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1141 CmdsParse(CommandTable
, Cmd
);
1145 int CmdHelp(const char *Cmd
){
1146 CmdsHelp(CommandTable
);