]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhfmfu.c
HF Search - refactoring cmds to work with it
[proxmark3-svn] / client / cmdhfmfu.c
CommitLineData
81740aa5 1//-----------------------------------------------------------------------------
2// Ultralight Code (c) 2013,2014 Midnitesnake & Andy Davies of Pentura
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// High frequency MIFARE ULTRALIGHT (C) commands
9//-----------------------------------------------------------------------------
afceaf40 10#include "loclass/des.h"
81740aa5 11#include "cmdhfmfu.h"
12#include "cmdhfmf.h"
13#include "cmdhf14a.h"
f168b263 14#include "mifare.h"
f9848fd6 15#include "util.h"
75377d29 16#include "protocols.h"
81740aa5 17
75377d29 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
25
345fb24a 26#define KEYS_3DES_COUNT 7
27uint8_t default_3des_keys[KEYS_3DES_COUNT][16] = {
f168b263 28 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
29 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
30 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
31 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
32 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
33 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
34 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
75377d29 35};
36
345fb24a 37#define KEYS_PWD_COUNT 8
38uint8_t default_pwd_pack[KEYS_PWD_COUNT][4] = {
c585a5cf 39 {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default
40 {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed)
345fb24a 41 {0x33,0x6B,0xA1,0x19}, // PACK 0x9c,0x2d -- italian bus (sniffed)
42 {0xFF,0x90,0x6C,0xB2}, // PACK 0x12,0x9e -- italian bus (sniffed)
43 {0x05,0x22,0xE6,0xB4}, // PACK 0x80,0x80 -- Amiiboo (sniffed) pikachu-b UID:
44 {0x7E,0x22,0xE6,0xB4}, // PACK 0x80,0x80 -- AMiiboo (sniffed)
45 {0x02,0xE1,0xEE,0x36}, // PACK 0x80,0x80 -- AMiiboo (sniffed) sonic UID: 04d257 7ae33e8027
46 {0x32,0x0C,0x16,0x17}, // PACK 0x80,0x80 -- AMiiboo (sniffed)
c585a5cf 47};
48
f168b263 49static int CmdHelp(const char *Cmd);
afceaf40 50
75377d29 51char* getProductTypeStr( uint8_t id){
52
53 static char buf[20];
54 char *retStr = buf;
55
56 switch(id) {
57 case 3:
58 sprintf(retStr, "0x%02X %s", id, "(Ultralight)");
59 break;
60 case 4:
61 sprintf(retStr, "0x%02X %s", id, "(NTAG)");
62 break;
63 default:
64 sprintf(retStr, "0x%02X %s", id, "(unknown)");
65 break;
66 }
67 return buf;
68}
69
70/*
71 The 7 MSBits (=n) code the storage size itself based on 2^n,
72 the LSBit is set to '0' if the size is exactly 2^n
73 and set to '1' if the storage size is between 2^n and 2^(n+1).
74*/
75char* getUlev1CardSizeStr( uint8_t fsize ){
76
345fb24a 77 static char buf[40];
75377d29 78 char *retStr = buf;
2be768af 79 memset(buf, 0, sizeof(buf));
75377d29 80
2be768af 81 uint16_t usize = 1 << ((fsize >>1) + 1);
82 uint16_t lsize = 1 << (fsize >>1);
75377d29 83
84 // is LSB set?
85 if ( fsize & 1 )
345fb24a 86 sprintf(retStr, "%02X (%u <-> %u bytes)",fsize, usize, lsize);
75377d29 87 else
345fb24a 88 sprintf(retStr, "%02X (%u bytes)", fsize, lsize);
75377d29 89 return buf;
90}
81740aa5 91
75377d29 92static void ul_switch_on_field(void) {
93 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
f168b263 94 SendCommand(&c);
75377d29 95}
81740aa5 96
75377d29 97static void ul_switch_off_field(void) {
98 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
99 SendCommand(&c);
100}
101
102static int ul_send_cmd_raw( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength ) {
103 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_APPEND_CRC, cmdlen, 0}};
104 memcpy(c.d.asBytes, cmd, cmdlen);
105 SendCommand(&c);
106 UsbCommand resp;
107 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
372a8257 108 if (!resp.arg[0] && responseLength) return -1;
75377d29 109
110 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
abab60ae 111 memcpy(response, resp.d.asBytes, resplen);
112 return resplen;
75377d29 113}
114/*
115static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) {
116 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}};
117 if (append_crc)
118 c.arg[0] |= ISO14A_APPEND_CRC;
119
120 memcpy(c.d.asBytes, cmd, cmdlen);
121 SendCommand(&c);
122 UsbCommand resp;
123 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
c585a5cf 124 if (!resp.arg[0] && responseLength) return -1;
75377d29 125
126 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
c585a5cf 127 memcpy(response, resp.d.asBytes, resplen);
128 return resplen;
75377d29 129}
130*/
131static int ul_select( iso14a_card_select_t *card ){
132
133 ul_switch_on_field();
134
135 UsbCommand resp;
136 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
137 if (resp.arg[0] < 1) return -1;
138
139 memcpy(card, resp.d.asBytes, sizeof(iso14a_card_select_t));
140 return resp.arg[0];
141}
142
143// This read command will at least return 16bytes.
144static int ul_read( uint8_t page, uint8_t *response, uint16_t responseLength ){
145
146 uint8_t cmd[] = {ISO14443A_CMD_READBLOCK, page};
147 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
148 if ( len == -1 )
149 ul_switch_off_field();
150 return len;
151}
152
c585a5cf 153static int ul_comp_write( uint8_t page, uint8_t *data, uint8_t datalen ){
154
155 uint8_t cmd[18];
156 memset(cmd, 0x00, sizeof(cmd));
157 datalen = ( datalen > 16) ? 16 : datalen;
158
159 cmd[0] = ISO14443A_CMD_WRITEBLOCK;
160 cmd[1] = page;
161 memcpy(cmd+2, data, datalen);
162
163 uint8_t response[1] = {0xff};
164 int len = ul_send_cmd_raw(cmd, 2+datalen, response, sizeof(response));
165 if ( len == -1 )
166 ul_switch_off_field();
167 // ACK
168 if ( response[0] == 0x0a ) return 0;
169 // NACK
170 return -1;
171}
345fb24a 172
75377d29 173static int ulc_requestAuthentication( uint8_t blockNo, uint8_t *nonce, uint16_t nonceLength ){
174
175 uint8_t cmd[] = {MIFARE_ULC_AUTH_1, blockNo};
176 int len = ul_send_cmd_raw(cmd, sizeof(cmd), nonce, nonceLength);
177 if ( len == -1 )
178 ul_switch_off_field();
179 return len;
180}
345fb24a 181
75377d29 182static int ulev1_requestAuthentication( uint8_t *pwd, uint8_t *pack, uint16_t packLength ){
183
184 uint8_t cmd[] = {MIFARE_ULEV1_AUTH, pwd[0], pwd[1], pwd[2], pwd[3]};
185 int len = ul_send_cmd_raw(cmd, sizeof(cmd), pack, packLength);
186 if ( len == -1)
187 ul_switch_off_field();
188 return len;
189}
190
191static int ulev1_getVersion( uint8_t *response, uint16_t responseLength ){
192
193 uint8_t cmd[] = {MIFARE_ULEV1_VERSION};
194 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
195 if ( len == -1 )
196 ul_switch_off_field();
197 return len;
198}
199
200// static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
f168b263 201
75377d29 202 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
f168b263 203
75377d29 204 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
205 // ul_switch_off_field();
206 // return -1;
207 // }
208 // return 0;
209// }
210
211static int ulev1_readCounter( uint8_t counter, uint8_t *response, uint16_t responseLength ){
212
213 uint8_t cmd[] = {MIFARE_ULEV1_READ_CNT, counter};
214 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
215 if (len == -1)
216 ul_switch_off_field();
217 return len;
218}
219
c585a5cf 220static int ulev1_readSignature( uint8_t *response, uint16_t responseLength ){
221
222 uint8_t cmd[] = {MIFARE_ULEV1_READSIG, 0x00};
223 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
224 if (len == -1)
225 ul_switch_off_field();
226 return len;
227}
228
75377d29 229static int ul_print_default( uint8_t *data){
230
231 uint8_t uid[7];
232
233 uid[0] = data[0];
234 uid[1] = data[1];
235 uid[2] = data[2];
236 uid[3] = data[4];
237 uid[4] = data[5];
238 uid[5] = data[6];
239 uid[6] = data[7];
240
241 PrintAndLog(" UID : %s ", sprint_hex(uid, 7));
4693c188 242 PrintAndLog(" UID[0] : %02X, Manufacturer: %s", uid[0], getTagInfo(uid[0]) );
345fb24a 243 if ( uid[0] == 0x05 ) {
244 uint8_t chip = (data[8] & 0xC7); // 11000111 mask, bit 3,4,5 RFU
245 switch (chip){
246 case 0xc2: PrintAndLog(" IC type : SLE 66R04P"); break;
247 case 0xc4: PrintAndLog(" IC type : SLE 66R16P"); break;
248 case 0xc6: PrintAndLog(" IC type : SLE 66R32P"); break;
249 }
250 }
75377d29 251 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
252 int crc0 = 0x88 ^ data[0] ^ data[1] ^data[2];
253 if ( data[3] == crc0 )
4693c188 254 PrintAndLog(" BCC0 : %02X - Ok", data[3]);
75377d29 255 else
4693c188 256 PrintAndLog(" BCC0 : %02X - crc should be %02X", data[3], crc0);
75377d29 257
258 int crc1 = data[4] ^ data[5] ^ data[6] ^data[7];
259 if ( data[8] == crc1 )
4693c188 260 PrintAndLog(" BCC1 : %02X - Ok", data[8]);
75377d29 261 else
4693c188 262 PrintAndLog(" BCC1 : %02X - crc should be %02X", data[8], crc1 );
75377d29 263
4693c188 264 PrintAndLog(" Internal : %02X - %s default", data[9], (data[9]==0x48)?"":"not" );
75377d29 265 PrintAndLog(" Lock : %s - %s", sprint_hex(data+10, 2),printBits( 2, data+10) );
266 PrintAndLog("OneTimePad : %s ", sprint_hex(data + 12, 4));
4693c188 267 PrintAndLog("");
75377d29 268 return 0;
269}
270
c585a5cf 271static int ntag_print_CC(uint8_t *data) {
345fb24a 272
273 PrintAndLog("\n--- NTAG NDEF Message");
274
75377d29 275 if(data[0] != 0xe1) {
276 PrintAndLog("no NDEF message");
277 return -1; // no NDEF message
278 }
279
280 PrintAndLog("Capability Container: %s", sprint_hex(data,4) );
281 PrintAndLog(" %02X: NDEF Magic Number", data[0]);
282 PrintAndLog(" %02X: version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f);
c585a5cf 283 PrintAndLog(" %02X: Physical Memory Size: %d bytes", data[2], (data[2] + 1) * 8);
284 if ( data[2] == 0x12 )
f04ef473 285 PrintAndLog(" %02X: NDEF Memory Size: %d bytes", data[2], 144);
c585a5cf 286 else if ( data[2] == 0x3e )
f04ef473 287 PrintAndLog(" %02X: NDEF Memory Size: %d bytes", data[2], 496);
c585a5cf 288 else if ( data[2] == 0x6d )
f04ef473 289 PrintAndLog(" %02X: NDEF Memory Size: %d bytes", data[2], 872);
c585a5cf 290
75377d29 291 PrintAndLog(" %02X: %s / %s", data[3],
292 (data[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
293 (data[3] & 0x0F)==0 ? "Write access granted without any security" : (data[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
75377d29 294 return 0;
295}
296
8ceb6b03 297int ul_print_type(uint16_t tagtype, uint8_t spaces){
298 char spc[11] = " ";
299 spc[10]=0x00;
300 char *spacer = spc + (10-spaces);
301
75377d29 302 if ( tagtype & UL )
8ceb6b03 303 PrintAndLog("%sTYPE : MIFARE Ultralight (MF0ICU1) %s [%x]", spacer, (tagtype & MAGIC)?"<magic>":"", tagtype);
75377d29 304 else if ( tagtype & UL_C)
8ceb6b03 305 PrintAndLog("%sTYPE : MIFARE Ultralight C (MF0ULC) %s [%x]", spacer, (tagtype & MAGIC)?"<magic>":"", tagtype );
75377d29 306 else if ( tagtype & UL_EV1_48)
8ceb6b03 307 PrintAndLog("%sTYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)", spacer);
345fb24a 308 else if ( tagtype & UL_EV1_128)
8ceb6b03 309 PrintAndLog("%sTYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)", spacer);
75377d29 310 else if ( tagtype & NTAG_213 )
8ceb6b03 311 PrintAndLog("%sTYPE : MIFARE NTAG 213 144bytes (NT2H1311G0DU)", spacer);
75377d29 312 else if ( tagtype & NTAG_215 )
8ceb6b03 313 PrintAndLog("%sTYPE : MIFARE NTAG 215 504bytes (NT2H1511G0DU)", spacer);
75377d29 314 else if ( tagtype & NTAG_216 )
8ceb6b03 315 PrintAndLog("%sTYPE : MIFARE NTAG 216 888bytes (NT2H1611G0DU)", spacer);
345fb24a 316 else if ( tagtype & MY_D )
8ceb6b03 317 PrintAndLog("%sTYPE : INFINEON my-d\x99", spacer);
345fb24a 318 else if ( tagtype & MY_D_NFC )
8ceb6b03 319 PrintAndLog("%sTYPE : INFINEON my-d\x99 NFC", spacer);
345fb24a 320 else if ( tagtype & MY_D_MOVE )
8ceb6b03 321 PrintAndLog("%sTYPE : INFINEON my-d\x99 move", spacer);
345fb24a 322 else if ( tagtype & MY_D_MOVE_NFC )
8ceb6b03 323 PrintAndLog("%sTYPE : INFINEON my-d\x99 move NFC", spacer);
75377d29 324 else
8ceb6b03 325 PrintAndLog("%sTYPE : Unknown %04x", spacer, tagtype);
75377d29 326 return 0;
327}
328
329static int ulc_print_3deskey( uint8_t *data){
c585a5cf 330 PrintAndLog(" deskey1 [44/0x2C]: %s [%.4s]", sprint_hex(data ,4),data);
331 PrintAndLog(" deskey1 [45/0x2D]: %s [%.4s]", sprint_hex(data+4 ,4),data+4);
332 PrintAndLog(" deskey2 [46/0x2E]: %s [%.4s]", sprint_hex(data+8 ,4),data+8);
333 PrintAndLog(" deskey2 [47/0x2F]: %s [%.4s]", sprint_hex(data+12,4),data+12);
75377d29 334 PrintAndLog(" 3des key : %s", sprint_hex(SwapEndian64(data, 16), 16));
335 return 0;
336}
337
338static int ulc_print_configuration( uint8_t *data){
339
340 PrintAndLog("--- UL-C Configuration");
345fb24a 341 PrintAndLog(" Higher Lockbits [40/0x28]: %s - %s", sprint_hex(data, 4), printBits(2, data));
342 PrintAndLog(" Counter [41/0x29]: %s - %s", sprint_hex(data+4, 4), printBits(2, data+4));
75377d29 343
344 bool validAuth = (data[8] >= 0x03 && data[8] <= 0x30);
345 if ( validAuth )
345fb24a 346 PrintAndLog(" Auth0 [42/0x2A]: %s Pages above %d needs authentication", sprint_hex(data+8, 4), data[8] );
75377d29 347 else{
348 if ( data[8] == 0){
345fb24a 349 PrintAndLog(" Auth0 [42/0x2A]: %s default", sprint_hex(data+8, 4) );
75377d29 350 } else {
345fb24a 351 PrintAndLog(" Auth0 [42/0x2A]: %s auth byte is out-of-range", sprint_hex(data+8, 4) );
f168b263 352 }
75377d29 353 }
345fb24a 354 PrintAndLog(" Auth1 [43/0x2B]: %s %s",
75377d29 355 sprint_hex(data+12, 4),
356 (data[12] & 1) ? "write access restricted": "read and write access restricted"
357 );
f168b263 358 return 0;
359}
81740aa5 360
75377d29 361static int ulev1_print_configuration( uint8_t *data){
f168b263 362
75377d29 363 PrintAndLog("\n--- UL-EV1 Configuration");
364
365 bool strg_mod_en = (data[0] & 2);
366 uint8_t authlim = (data[4] & 0x07);
367 bool cfglck = (data[4] & 0x40);
368 bool prot = (data[4] & 0x80);
369 uint8_t vctid = data[5];
370
371 PrintAndLog(" cfg0 [16/0x10]: %s", sprint_hex(data, 4));
345fb24a 372 if ( data[3] < 0xff )
373 PrintAndLog(" - pages above %d needs authentication",data[3]);
374 else
375 PrintAndLog(" - pages don't need authentication");
75377d29 376 PrintAndLog(" - strong modulation mode %s", (strg_mod_en) ? "enabled":"disabled");
377 PrintAndLog(" cfg1 [17/0x11]: %s", sprint_hex(data+4, 4) );
378 if ( authlim == 0)
345fb24a 379 PrintAndLog(" - Unlimited password attempts");
75377d29 380 else
381 PrintAndLog(" - Max number of password attempts is %d", authlim);
382 PrintAndLog(" - user configuration %s", cfglck ? "permanently locked":"writeable");
383 PrintAndLog(" - %s access is protected with password", prot ? "read and write":"write");
345fb24a 384 PrintAndLog(" %02X - Virtual Card Type Identifier is %s default", vctid, (vctid==0x05)? "":"not");
75377d29 385 PrintAndLog(" PWD [18/0x12]: %s", sprint_hex(data+8, 4));
386 PrintAndLog(" PACK [19/0x13]: %s", sprint_hex(data+12, 4));
387 return 0;
388}
389
390static int ulev1_print_counters(){
391 PrintAndLog("--- UL-EV1 Counters");
392 uint8_t counter[3] = {0,0,0};
393 for ( uint8_t i = 0; i<3; ++i) {
394 ulev1_readCounter(i,counter, sizeof(counter) );
345fb24a 395 PrintAndLog(" [%0d] : %s", i, sprint_hex(counter,3));
75377d29 396 }
397 return 0;
398}
399
c585a5cf 400static int ulev1_print_signature( uint8_t *data, uint8_t len){
401 PrintAndLog("\n--- UL-EV1 Signature");
402 PrintAndLog("IC signature public key name : NXP NTAG21x 2013");
403 PrintAndLog("IC signature public key value : 04494e1a386d3d3cfe3dc10e5de68a499b1c202db5b132393e89ed19fe5be8bc61");
404 PrintAndLog(" Elliptic curve parameters : secp128r1");
405 PrintAndLog(" Tag ECC Signature : %s", sprint_hex(data, len));
406 //to do: verify if signature is valid
407 //PrintAndLog("IC signature status: %s valid", (iseccvalid() )?"":"not");
408 return 0;
409}
345fb24a 410
411static int ulev1_print_version(uint8_t *data){
412 PrintAndLog("\n--- UL-EV1 / NTAG Version");
413 PrintAndLog(" Raw bytes : %s", sprint_hex(data, 8) );
414 PrintAndLog(" Vendor ID : %02X, Manufacturer: %s", data[1], getTagInfo(data[1]));
415 PrintAndLog(" Product type : %s", getProductTypeStr(data[2]));
416 PrintAndLog(" Product subtype : %02X %s", data[3], (data[3]==1) ?"17 pF":"50pF");
417 PrintAndLog(" Major version : %02X", data[4]);
418 PrintAndLog(" Minor version : %02X", data[5]);
419 PrintAndLog(" Size : %s", getUlev1CardSizeStr(data[6]));
420 PrintAndLog(" Protocol type : %02X", data[7]);
421 return 0;
422}
423
f04ef473 424/*
c585a5cf 425static int ulc_magic_test(){
426 // Magic Ultralight test
427 // Magic UL-C, by observation,
428 // 1) it seems to have a static nonce response to 0x1A command.
429 // 2) the deskey bytes is not-zero:d out on as datasheet states.
430 // 3) UID - changeable, not only, but pages 0-1-2-3.
431 // 4) use the ul_magic_test ! magic tags answers specially!
432 int returnValue = UL_ERROR;
433 iso14a_card_select_t card;
434 uint8_t nonce1[11] = {0x00};
435 uint8_t nonce2[11] = {0x00};
436 int status = ul_select(&card);
437 if ( status < 1 ){
438 PrintAndLog("Error: couldn't select ulc_magic_test");
439 ul_switch_off_field();
440 return UL_ERROR;
441 }
442 status = ulc_requestAuthentication(0, nonce1, sizeof(nonce1));
443 if ( status > 0 ) {
444 status = ulc_requestAuthentication(0, nonce2, sizeof(nonce2));
445 returnValue = ( !memcmp(nonce1, nonce2, 11) ) ? UL_C_MAGIC : UL_C;
446 } else {
447 returnValue = UL;
448 }
449 ul_switch_off_field();
450 return returnValue;
451}
f04ef473 452*/
c585a5cf 453static int ul_magic_test(){
454
455 // Magic Ultralight tests
456 // 1) take present UID, and try to write it back. OBSOLETE
457 // 2) make a wrong length write to page0, and see if tag answers with ACK/NACK:
458 iso14a_card_select_t card;
459 int status = ul_select(&card);
460 if ( status < 1 ){
461 PrintAndLog("Error: couldn't select ul_magic_test");
462 ul_switch_off_field();
463 return UL_ERROR;
464 }
465 status = ul_comp_write(0, NULL, 0);
466 ul_switch_off_field();
467 if ( status == 0)
468 return UL_MAGIC;
469 return UL;
470}
f04ef473 471
75377d29 472uint16_t GetHF14AMfU_Type(void){
81740aa5 473
92690507 474 TagTypeUL_t tagtype = UNKNOWN;
475 iso14a_card_select_t card;
75377d29 476 uint8_t version[10] = {0x00};
75377d29 477 int status = 0;
478 int len;
479
480 status = ul_select(&card);
481 if ( status < 1 ){
482 PrintAndLog("Error: couldn't select");
483 ul_switch_off_field();
484 return UL_ERROR;
485 }
92690507 486 // Ultralight - ATQA / SAK
abab60ae 487 if ( card.atqa[1] != 0x00 || card.atqa[0] != 0x44 || card.sak != 0x00 ) {
4693c188 488 PrintAndLog("Tag is not Ultralight | NTAG | MY-D [ATQA: %02X %02X SAK: %02X]\n", card.atqa[1], card.atqa[0], card.sak);
75377d29 489 ul_switch_off_field();
490 return UL_ERROR;
491 }
92690507 492
345fb24a 493 if ( card.uid[0] != 0x05) {
75377d29 494
345fb24a 495 len = ulev1_getVersion(version, sizeof(version));
496 if (len > -1) ul_switch_off_field(); //if -1 it is already off
497
498 switch (len) {
499 case 0x0A: {
500
501 if ( version[2] == 0x03 && version[6] == 0x0B )
502 tagtype = UL_EV1_48;
503 else if ( version[2] == 0x03 && version[6] != 0x0B )
504 tagtype = UL_EV1_128;
505 else if ( version[2] == 0x04 && version[6] == 0x0F )
506 tagtype = NTAG_213;
507 else if ( version[2] == 0x04 && version[6] == 0x11 )
508 tagtype = NTAG_215;
509 else if ( version[2] == 0x04 && version[6] == 0x13 )
510 tagtype = NTAG_216;
511 else if ( version[2] == 0x04 )
512 tagtype = NTAG;
513
514 break;
515 }
516 case 0x01: tagtype = UL_C; break;
517 case 0x00: tagtype = UL; break;
518 case -1 : tagtype = (UL | UL_C); break; //when does this happen?
519 default : tagtype = UNKNOWN; break;
520 }
521 if (tagtype == (UL | UL_C)) {
522 status = ul_select(&card);
523 if ( status < 1 ){
524 PrintAndLog("Error: couldn't select 2");
525 ul_switch_off_field();
526 return UL_ERROR;
527 }
528 uint8_t nonce1[11] = {0x00};
529 status = ulc_requestAuthentication(0, nonce1, sizeof(nonce1));
530 if ( status > 0 )
531 tagtype = UL_C;
532 else
533 tagtype = UL;
534
535 if (status != -1) ul_switch_off_field();
536 }
537 } else {
538 // Infinition MY-D tests Exam high nibble
539 uint8_t nib = (card.uid[1] & 0xf0) >> 4;
540 switch ( nib ){
541 case 1: tagtype = MY_D; break;
542 case 2: tagtype = (MY_D | MY_D_NFC); break;
543 case 3: tagtype = (MY_D_MOVE | MY_D_MOVE_NFC); break;
75377d29 544 }
f168b263 545 }
75377d29 546
f805ac7a 547 tagtype = (ul_magic_test() == UL_MAGIC) ? (tagtype | MAGIC) : tagtype;
548 //if ((tagtype & UL)) tagtype = ul_magic_test();
75377d29 549
f168b263 550 return tagtype;
551}
552
553int CmdHF14AMfUInfo(const char *Cmd){
554
c585a5cf 555 uint8_t authlim = 0xff;
f168b263 556 uint8_t data[16] = {0x00};
75377d29 557 iso14a_card_select_t card;
92690507 558 uint8_t *key;
75377d29 559 int status;
f168b263 560
75377d29 561 TagTypeUL_t tagtype = GetHF14AMfU_Type();
562 if (tagtype == UL_ERROR) return -1;
92690507 563
abab60ae 564 PrintAndLog("\n--- Tag Information ---------");
565 PrintAndLog("-------------------------------------------------------------");
8ceb6b03 566 ul_print_type(tagtype, 6);
92690507 567
75377d29 568 status = ul_select(&card);
569 if ( status < 1 ){
570 PrintAndLog("Error: couldn't select");
571 ul_switch_off_field();
572 return status;
573 }
92690507 574
75377d29 575 // read pages 0,1,2,4 (should read 4pages)
576 status = ul_read(0, data, sizeof(data));
577 if ( status == -1 ){
f805ac7a 578 PrintAndLog("Error: tag didn't answer to READ");
75377d29 579 return status;
f168b263 580 }
92690507 581
75377d29 582 ul_print_default(data);
583
f168b263 584 if ((tagtype & UL_C)){
75377d29 585
586 // read pages 0x28, 0x29, 0x2A, 0x2B
587 uint8_t ulc_conf[16] = {0x00};
588 status = ul_read(0x28, ulc_conf, sizeof(ulc_conf));
589 if ( status == -1 ){
590 PrintAndLog("Error: tag didn't answer to READ - possibly locked");
abab60ae 591 return status;
592 }
abab60ae 593 ulc_print_configuration(ulc_conf);
75377d29 594
595 if ((tagtype & MAGIC)){
596
597 uint8_t ulc_deskey[16] = {0x00};
598 status = ul_read(0x2C, ulc_deskey, sizeof(ulc_deskey));
599 if ( status == -1 ){
f805ac7a 600 PrintAndLog("Error: tag didn't answer to READ magic");
75377d29 601 return status;
602 }
75377d29 603 ulc_print_3deskey(ulc_deskey);
604
c585a5cf 605 } else {
75377d29 606 PrintAndLog("Trying some default 3des keys");
607 ul_switch_off_field();
608 for (uint8_t i = 0; i < 7; ++i ){
609 key = default_3des_keys[i];
610 if (try3DesAuthentication(key) == 1){
c585a5cf 611 PrintAndLog("Found default 3des key: "); //%s", sprint_hex(key,16));
612 ulc_print_3deskey(SwapEndian64(key,16));
75377d29 613 return 0;
614 }
f168b263 615 }
75377d29 616 }
617 }
618
619 if ((tagtype & (UL_EV1_48 | UL_EV1_128))) {
345fb24a 620 //do counters and signature first (don't neet auth)
621 ulev1_print_counters();
622
623 uint8_t ulev1_signature[32] = {0x00};
624 status = ulev1_readSignature( ulev1_signature, sizeof(ulev1_signature));
625 if ( status == -1 ){
626 PrintAndLog("Error: tag didn't answer to READ SIGNATURE");
627 return status;
628 }
629 ulev1_print_signature( ulev1_signature, sizeof(ulev1_signature));
630
c585a5cf 631 uint8_t startconfigblock = (tagtype & UL_EV1_48) ? 0x10 : 0x25;
75377d29 632 uint8_t ulev1_conf[16] = {0x00};
633 status = ul_read(startconfigblock, ulev1_conf, sizeof(ulev1_conf));
634 if ( status == -1 ){
f805ac7a 635 PrintAndLog("Error: tag didn't answer to READ EV1");
75377d29 636 return status;
637 }
c585a5cf 638 // save AUTHENTICATION LIMITS for later:
639 authlim = (ulev1_conf[4] & 0x07);
75377d29 640 ulev1_print_configuration(ulev1_conf);
f168b263 641 }
75377d29 642
643 if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_213 | NTAG_215 | NTAG_216))) {
644
645 uint8_t version[10] = {0x00};
646 status = ulev1_getVersion(version, sizeof(version));
647 if ( status == -1 ){
648 PrintAndLog("Error: tag didn't answer to GETVERSION");
75377d29 649 return status;
650 }
c585a5cf 651 ulev1_print_version(version);
652
653 // AUTHLIMIT, (number of failed authentications)
654 // 0 = limitless.
655 // 1-7 = ... should we even try then?
656 if ( authlim == 0 ){
657 PrintAndLog("\n--- Known EV1/NTAG passwords.");
658
659 uint8_t pack[4] = {0,0,0,0};
f805ac7a 660 int len=0; //if len goes to -1 the connection will be turned off.
c585a5cf 661 for (uint8_t i = 0; i < 3; ++i ){
662 key = default_pwd_pack[i];
f805ac7a 663 if ( len > -1 ){
664 len = ulev1_requestAuthentication(key, pack, sizeof(pack));
c585a5cf 665 PrintAndLog("Found a default password: %s || Pack: %02X %02X",sprint_hex(key, 4), pack[0], pack[1]);
f04ef473 666 break;
c585a5cf 667 }
668 }
f805ac7a 669 if (len > -1) ul_switch_off_field();
75377d29 670 }
f168b263 671 }
c585a5cf 672
75377d29 673 if ((tagtype & (NTAG_213 | NTAG_215 | NTAG_216))){
c585a5cf 674
75377d29 675 uint8_t cc[16] = {0x00};
f04ef473 676 status = ul_read(3, cc, sizeof(cc));
75377d29 677 if ( status == -1 ){
f805ac7a 678 PrintAndLog("Error: tag didn't answer to READ ntag");
75377d29 679 return status;
680 }
c585a5cf 681 ntag_print_CC(cc);
75377d29 682 }
c585a5cf 683
75377d29 684 ul_switch_off_field();
345fb24a 685 PrintAndLog("");
81740aa5 686 return 0;
687}
688
689//
690// Mifare Ultralight Write Single Block
691//
692int CmdHF14AMfUWrBl(const char *Cmd){
afceaf40
MHS
693 uint8_t blockNo = -1;
694 bool chinese_card = FALSE;
695 uint8_t bldata[16] = {0x00};
696 UsbCommand resp;
81740aa5 697
afceaf40 698 char cmdp = param_getchar(Cmd, 0);
81740aa5 699 if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
afceaf40 700 PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
81740aa5 701 PrintAndLog(" [block number]");
702 PrintAndLog(" [block data] - (8 hex symbols)");
703 PrintAndLog(" [w] - Chinese magic ultralight tag");
704 PrintAndLog("");
afceaf40 705 PrintAndLog(" sample: hf mfu wrbl 0 01020304");
81740aa5 706 PrintAndLog("");
afceaf40
MHS
707 return 0;
708 }
709
81740aa5 710 blockNo = param_get8(Cmd, 0);
711
f168b263 712 if (blockNo > MAX_UL_BLOCKS){
afceaf40
MHS
713 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
714 return 1;
715 }
81740aa5 716
afceaf40
MHS
717 if (param_gethex(Cmd, 1, bldata, 8)) {
718 PrintAndLog("Block data must include 8 HEX symbols");
719 return 1;
720 }
81740aa5 721
afceaf40
MHS
722 if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) {
723 chinese_card = TRUE;
724 }
81740aa5 725
afceaf40 726 if ( blockNo <= 3) {
81740aa5 727 if (!chinese_card){
728 PrintAndLog("Access Denied");
729 } else {
730 PrintAndLog("--specialblock no:%02x", blockNo);
731 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
732 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
733 memcpy(d.d.asBytes,bldata, 4);
734 SendCommand(&d);
735 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
736 uint8_t isOK = resp.arg[0] & 0xff;
737 PrintAndLog("isOk:%02x", isOK);
738 } else {
739 PrintAndLog("Command execute timeout");
740 }
741 }
742 } else {
afceaf40
MHS
743 PrintAndLog("--block no:%02x", blockNo);
744 PrintAndLog("--data: %s", sprint_hex(bldata, 4));
745 UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
81740aa5 746 memcpy(e.d.asBytes,bldata, 4);
747 SendCommand(&e);
afceaf40
MHS
748 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
749 uint8_t isOK = resp.arg[0] & 0xff;
750 PrintAndLog("isOk:%02x", isOK);
751 } else {
752 PrintAndLog("Command execute timeout");
753 }
754 }
755 return 0;
81740aa5 756}
757
758//
759// Mifare Ultralight Read Single Block
760//
761int CmdHF14AMfURdBl(const char *Cmd){
81740aa5 762
f168b263 763 UsbCommand resp;
764 uint8_t blockNo = -1;
afceaf40 765 char cmdp = param_getchar(Cmd, 0);
81740aa5 766
767 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
afceaf40
MHS
768 PrintAndLog("Usage: hf mfu rdbl <block number>");
769 PrintAndLog(" sample: hfu mfu rdbl 0");
770 return 0;
f168b263 771 }
772
afceaf40
MHS
773 blockNo = param_get8(Cmd, 0);
774
f168b263 775 if (blockNo > MAX_UL_BLOCKS){
776 PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
777 return 1;
afceaf40 778 }
f168b263 779
afceaf40
MHS
780 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
781 SendCommand(&c);
782
f168b263 783
afceaf40 784 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
f168b263 785 uint8_t isOK = resp.arg[0] & 0xff;
786 if (isOK) {
787 uint8_t *data = resp.d.asBytes;
788 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4));
789 }
790 else {
791 PrintAndLog("Failed reading block: (%02x)", isOK);
792 }
81740aa5 793 } else {
f168b263 794 PrintAndLog("Command execute time-out");
afceaf40 795 }
f168b263 796
afceaf40 797 return 0;
81740aa5 798}
799
92690507 800int usage_hf_mfu_dump(void)
801{
802 PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
803 PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
804 PrintAndLog("It autodetects card type.\n");
f9848fd6 805 PrintAndLog("Usage: hf mfu dump k <key> n <filename w/o .bin>");
75377d29 806 PrintAndLog(" Options : ");
807 PrintAndLog(" k <key> : Enter key for authentication");
808 PrintAndLog(" n <FN > : Enter filename w/o .bin to save the dump as");
809 PrintAndLog(" s : Swap entered key's endianness for auth");
810 PrintAndLog("");
92690507 811 PrintAndLog(" sample : hf mfu dump");
f9848fd6 812 PrintAndLog(" : hf mfu dump n myfile");
92690507 813 return 0;
814}
81740aa5 815//
92690507 816// Mifare Ultralight / Ultralight-C / Ultralight-EV1
817// Read and Dump Card Contents, using auto detection of tag size.
81740aa5 818//
92690507 819// TODO: take a password to read UL-C / UL-EV1 tags.
81740aa5 820int CmdHF14AMfUDump(const char *Cmd){
821
afceaf40 822 FILE *fout;
81740aa5 823 char filename[FILE_PATH_SIZE] = {0x00};
92690507 824 char *fnameptr = filename;
825 char *str = "Dumping Ultralight%s%s Card Data...";
afceaf40
MHS
826 uint8_t *lockbytes_t = NULL;
827 uint8_t lockbytes[2] = {0x00};
afceaf40
MHS
828 uint8_t *lockbytes_t2 = NULL;
829 uint8_t lockbytes2[2] = {0x00};
afceaf40 830 bool bit[16] = {0x00};
81740aa5 831 bool bit2[16] = {0x00};
f9848fd6 832 uint8_t data[1024] = {0x00};
833 bool hasPwd = false;
92690507 834 int i = 0;
835 int Pages = 16;
836 bool tmplockbit = false;
f9848fd6 837 uint8_t dataLen=0;
838 uint8_t cmdp =0;
75377d29 839 uint8_t key[16] = {0x00};
840 uint8_t *keyPtr = key;
f9848fd6 841 size_t fileNlen = 0;
75377d29 842 bool errors = false;
843 bool swapEndian = false;
f9848fd6 844
845 while(param_getchar(Cmd, cmdp) != 0x00)
846 {
847 switch(param_getchar(Cmd, cmdp))
848 {
849 case 'h':
850 case 'H':
851 return usage_hf_mfu_dump();
852 case 'k':
853 case 'K':
854 dataLen = param_gethex(Cmd, cmdp+1, data, 32);
855 if (dataLen) {
856 errors = true;
857 } else {
75377d29 858 memcpy(key, data, 16);
f9848fd6 859 }
860 cmdp += 2;
861 hasPwd = true;
862 break;
863 case 'n':
864 case 'N':
865 fileNlen = param_getstr(Cmd, cmdp+1, filename);
866 if (!fileNlen) errors = true;
867 if (fileNlen > FILE_PATH_SIZE-5) fileNlen = FILE_PATH_SIZE-5;
868 cmdp += 2;
869 break;
75377d29 870 case 's':
871 swapEndian = true;
872 cmdp++;
2be768af 873 break;
f9848fd6 874 default:
875 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
876 errors = true;
877 break;
878 }
879 if(errors) break;
880 }
881
882 //Validations
75377d29 883 if(errors) return usage_hf_mfu_dump();
884
885 if (swapEndian)
886 keyPtr = SwapEndian64(data, 16);
81740aa5 887
92690507 888 TagTypeUL_t tagtype = GetHF14AMfU_Type();
889 if (tagtype == UL_ERROR) return -1;
81740aa5 890
92690507 891 if ( tagtype & UL ) {
892 Pages = 16;
893 PrintAndLog(str,"", (tagtype & MAGIC)?" (magic)":"" );
81740aa5 894 }
92690507 895 else if ( tagtype & UL_C ) {
896 Pages = 44;
897 PrintAndLog(str,"-C", (tagtype & MAGIC)?" (magic)":"" );
f168b263 898 }
92690507 899 else if ( tagtype & UL_EV1_48 ) {
900 Pages = 18;
901 PrintAndLog(str," EV1_48","");
902 }
903 else if ( tagtype & UL_EV1_128 ) {
904 Pages = 32;
905 PrintAndLog(str," EV1_128","");
906 } else {
907 Pages = 16;
908 PrintAndLog("Dumping unknown Ultralight, using default values.");
909 }
910
75377d29 911 UsbCommand c = {CMD_MIFAREUC_READCARD, {0,Pages}};
912 if ( hasPwd ) {
913 c.arg[2] = 1;
914 memcpy(c.d.asBytes, key, 16);
915 }
916 SendCommand(&c);
917 UsbCommand resp;
918 if (!WaitForResponseTimeout(CMD_ACK, &resp,1500)) {
919 PrintAndLog("Command execute time-out");
920 return 1;
921 }
922 PrintAndLog ("%u,%u",resp.arg[0],resp.arg[1]);
923 uint8_t isOK = resp.arg[0] & 0xff;
924 if (isOK) {
925 memcpy(data, resp.d.asBytes, resp.arg[1]);
926 } else {
927 PrintAndLog("Failed reading block: (%02x)", i);
928 return 1;
92690507 929 }
92690507 930
81740aa5 931 // Load lock bytes.
932 int j = 0;
92690507 933
81740aa5 934 lockbytes_t = data + 8;
935 lockbytes[0] = lockbytes_t[2];
936 lockbytes[1] = lockbytes_t[3];
937 for(j = 0; j < 16; j++){
938 bit[j] = lockbytes[j/8] & ( 1 <<(7-j%8));
92690507 939 }
940
81740aa5 941 // Load bottom lockbytes if available
942 if ( Pages == 44 ) {
81740aa5 943 lockbytes_t2 = data + (40*4);
944 lockbytes2[0] = lockbytes_t2[2];
945 lockbytes2[1] = lockbytes_t2[3];
946 for (j = 0; j < 16; j++) {
947 bit2[j] = lockbytes2[j/8] & ( 1 <<(7-j%8));
948 }
949 }
950
f9848fd6 951 // add keys
952 if (hasPwd){
953 memcpy(data + Pages*4, key, 16);
954 Pages += 4;
955 }
81740aa5 956 for (i = 0; i < Pages; ++i) {
81740aa5 957 if ( i < 3 ) {
958 PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4));
959 continue;
960 }
81740aa5 961 switch(i){
962 case 3: tmplockbit = bit[4]; break;
963 case 4: tmplockbit = bit[3]; break;
964 case 5: tmplockbit = bit[2]; break;
965 case 6: tmplockbit = bit[1]; break;
966 case 7: tmplockbit = bit[0]; break;
967 case 8: tmplockbit = bit[15]; break;
968 case 9: tmplockbit = bit[14]; break;
969 case 10: tmplockbit = bit[13]; break;
970 case 11: tmplockbit = bit[12]; break;
971 case 12: tmplockbit = bit[11]; break;
972 case 13: tmplockbit = bit[10]; break;
973 case 14: tmplockbit = bit[9]; break;
974 case 15: tmplockbit = bit[8]; break;
975 case 16:
976 case 17:
977 case 18:
978 case 19: tmplockbit = bit2[6]; break;
979 case 20:
980 case 21:
981 case 22:
982 case 23: tmplockbit = bit2[5]; break;
983 case 24:
984 case 25:
985 case 26:
986 case 27: tmplockbit = bit2[4]; break;
987 case 28:
988 case 29:
989 case 30:
990 case 31: tmplockbit = bit2[2]; break;
991 case 32:
992 case 33:
993 case 34:
994 case 35: tmplockbit = bit2[1]; break;
995 case 36:
996 case 37:
997 case 38:
998 case 39: tmplockbit = bit2[0]; break;
999 case 40: tmplockbit = bit2[12]; break;
1000 case 41: tmplockbit = bit2[11]; break;
1001 case 42: tmplockbit = bit2[10]; break; //auth0
1002 case 43: tmplockbit = bit2[9]; break; //auth1
1003 default: break;
1004 }
1005 PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),tmplockbit);
1006 }
81740aa5 1007
81740aa5 1008 // user supplied filename?
f9848fd6 1009 if (fileNlen < 1) {
81740aa5 1010 // UID = data 0-1-2 4-5-6-7 (skips a beat)
afceaf40
MHS
1011 sprintf(fnameptr,"%02X%02X%02X%02X%02X%02X%02X.bin",
1012 data[0],data[1], data[2], data[4],data[5],data[6], data[7]);
81740aa5 1013 } else {
f9848fd6 1014 sprintf(fnameptr + fileNlen," .bin");
81740aa5 1015 }
1016
81740aa5 1017 if ((fout = fopen(filename,"wb")) == NULL) {
1018 PrintAndLog("Could not create file name %s", filename);
1019 return 1;
1020 }
1021 fwrite( data, 1, Pages*4, fout );
1022 fclose(fout);
1023
1024 PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages, Pages*4, filename);
afceaf40 1025 return 0;
81740aa5 1026}
1027
1028// Needed to Authenticate to Ultralight C tags
1029void rol (uint8_t *data, const size_t len){
afceaf40
MHS
1030 uint8_t first = data[0];
1031 for (size_t i = 0; i < len-1; i++) {
1032 data[i] = data[i+1];
1033 }
1034 data[len-1] = first;
81740aa5 1035}
1036
1037//-------------------------------------------------------------------------------
1038// Ultralight C Methods
1039//-------------------------------------------------------------------------------
1040
1041//
1042// Ultralight C Authentication Demo {currently uses hard-coded key}
1043//
1044int CmdHF14AMfucAuth(const char *Cmd){
afceaf40 1045
afceaf40
MHS
1046 uint8_t keyNo = 0;
1047 bool errors = false;
f168b263 1048
1049 char cmdp = param_getchar(Cmd, 0);
1050
afceaf40
MHS
1051 //Change key to user defined one
1052 if (cmdp == 'k' || cmdp == 'K'){
1053 keyNo = param_get8(Cmd, 1);
f168b263 1054 if(keyNo > 6)
1055 errors = true;
afceaf40
MHS
1056 }
1057
f168b263 1058 if (cmdp == 'h' || cmdp == 'H')
afceaf40 1059 errors = true;
f168b263 1060
afceaf40
MHS
1061 if (errors) {
1062 PrintAndLog("Usage: hf mfu cauth k <key number>");
1063 PrintAndLog(" 0 (default): 3DES standard key");
f168b263 1064 PrintAndLog(" 1 : all 0x00 key");
afceaf40
MHS
1065 PrintAndLog(" 2 : 0x00-0x0F key");
1066 PrintAndLog(" 3 : nfc key");
f168b263 1067 PrintAndLog(" 4 : all 0x01 key");
1068 PrintAndLog(" 5 : all 0xff key");
1069 PrintAndLog(" 6 : 0x00-0xFF key");
1070 PrintAndLog("\n sample : hf mfu cauth k");
81740aa5 1071 PrintAndLog(" : hf mfu cauth k 3");
afceaf40
MHS
1072 return 0;
1073 }
1074
f168b263 1075 uint8_t *key = default_3des_keys[keyNo];
7eec1204 1076 if (try3DesAuthentication(key)>0)
1077 PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key, 16));
f168b263 1078 else
1079 PrintAndLog("Authentication failed");
1080
1081 return 0;
1082}
1083
1084int try3DesAuthentication( uint8_t *key){
1085
afceaf40
MHS
1086 uint8_t blockNo = 0;
1087 uint32_t cuid = 0;
81740aa5 1088
f168b263 1089 des3_context ctx = { 0 };
1090
1091 uint8_t random_a[8] = { 1,1,1,1,1,1,1,1 };
1092 uint8_t random_b[8] = { 0 };
1093 uint8_t enc_random_b[8] = { 0 };
1094 uint8_t rnd_ab[16] = { 0 };
1095 uint8_t iv[8] = { 0 };
1096
81740aa5 1097 UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}};
1098 SendCommand(&c);
1099 UsbCommand resp;
f168b263 1100 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return -1;
1101 if ( !(resp.arg[0] & 0xff) ) return -2;
1102
1103 cuid = resp.arg[1];
1104 memcpy(enc_random_b,resp.d.asBytes+1,8);
afceaf40
MHS
1105
1106 des3_set2key_dec(&ctx, key);
f168b263 1107 // context, mode, length, IV, input, output
1108 des3_crypt_cbc( &ctx, DES_DECRYPT, sizeof(random_b), iv , enc_random_b , random_b);
afceaf40
MHS
1109
1110 rol(random_b,8);
f168b263 1111 memcpy(rnd_ab ,random_a,8);
1112 memcpy(rnd_ab+8,random_b,8);
afceaf40
MHS
1113
1114 des3_set2key_enc(&ctx, key);
f168b263 1115 // context, mode, length, IV, input, output
1116 des3_crypt_cbc(&ctx, DES_ENCRYPT, sizeof(rnd_ab), enc_random_b, rnd_ab, rnd_ab);
afceaf40
MHS
1117
1118 //Auth2
f168b263 1119 c.cmd = CMD_MIFAREUC_AUTH2;
1120 c.arg[0] = cuid;
1121 memcpy(c.d.asBytes, rnd_ab, 16);
1122 SendCommand(&c);
81740aa5 1123
f168b263 1124 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
1125 if ( !(resp.arg[0] & 0xff)) return -2;
1126
1127 uint8_t enc_resp[8] = { 0 };
1128 uint8_t resp_random_a[8] = { 0 };
1129 memcpy(enc_resp, resp.d.asBytes+1, 8);
81740aa5 1130
f168b263 1131 des3_set2key_dec(&ctx, key);
1132 // context, mode, length, IV, input, output
1133 des3_crypt_cbc( &ctx, DES_DECRYPT, 8, enc_random_b, enc_resp, resp_random_a);
1134
1135 if ( !memcmp(resp_random_a, random_a, 8))
1136 return 1;
afceaf40 1137 return 0;
f168b263 1138
1139 //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8));
1140 //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8));
1141 //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8));
1142 //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16));
1143 //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16));
1144 //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8));
81740aa5 1145}
f168b263 1146
afceaf40
MHS
1147/**
1148A test function to validate that the polarssl-function works the same
1149was as the openssl-implementation.
1150Commented out, since it requires openssl
1151
1152int CmdTestDES(const char * cmd)
1153{
1154 uint8_t key[16] = {0x00};
1155
1156 memcpy(key,key3_3des_data,16);
1157 DES_cblock RndA, RndB;
1158
1159 PrintAndLog("----------OpenSSL DES implementation----------");
1160 {
1161 uint8_t e_RndB[8] = {0x00};
1162 unsigned char RndARndB[16] = {0x00};
1163
1164 DES_cblock iv = { 0 };
1165 DES_key_schedule ks1,ks2;
1166 DES_cblock key1,key2;
1167
1168 memcpy(key,key3_3des_data,16);
1169 memcpy(key1,key,8);
1170 memcpy(key2,key+8,8);
1171
1172
1173 DES_set_key((DES_cblock *)key1,&ks1);
1174 DES_set_key((DES_cblock *)key2,&ks2);
1175
1176 DES_random_key(&RndA);
1177 PrintAndLog(" RndA:%s",sprint_hex(RndA, 8));
1178 PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8));
1179 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1180 // unsigned char *output, long length, DES_key_schedule *ks1,
1181 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1182 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1183
1184 PrintAndLog(" RndB:%s",sprint_hex(RndB, 8));
1185 rol(RndB,8);
1186 memcpy(RndARndB,RndA,8);
1187 memcpy(RndARndB+8,RndB,8);
1188 PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16));
1189 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1190 PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16));
1191
1192 }
1193 PrintAndLog("----------PolarSSL implementation----------");
1194 {
1195 uint8_t random_a[8] = { 0 };
1196 uint8_t enc_random_a[8] = { 0 };
1197 uint8_t random_b[8] = { 0 };
1198 uint8_t enc_random_b[8] = { 0 };
1199 uint8_t random_a_and_b[16] = { 0 };
1200 des3_context ctx = { 0 };
1201
1202 memcpy(random_a, RndA,8);
1203
1204 uint8_t output[8] = { 0 };
1205 uint8_t iv[8] = { 0 };
1206
1207 PrintAndLog(" RndA :%s",sprint_hex(random_a, 8));
1208 PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8));
1209
1210 des3_set2key_dec(&ctx, key);
1211
1212 des3_crypt_cbc(&ctx // des3_context *ctx
1213 , DES_DECRYPT // int mode
1214 , sizeof(random_b) // size_t length
1215 , iv // unsigned char iv[8]
1216 , enc_random_b // const unsigned char *input
1217 , random_b // unsigned char *output
1218 );
1219
1220 PrintAndLog(" RndB:%s",sprint_hex(random_b, 8));
1221
1222 rol(random_b,8);
1223 memcpy(random_a_and_b ,random_a,8);
1224 memcpy(random_a_and_b+8,random_b,8);
1225
1226 PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16));
1227
1228 des3_set2key_enc(&ctx, key);
81740aa5 1229
afceaf40
MHS
1230 des3_crypt_cbc(&ctx // des3_context *ctx
1231 , DES_ENCRYPT // int mode
1232 , sizeof(random_a_and_b) // size_t length
1233 , enc_random_b // unsigned char iv[8]
1234 , random_a_and_b // const unsigned char *input
1235 , random_a_and_b // unsigned char *output
1236 );
1237
1238 PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1239 }
1240 return 0;
1241}
1242**/
f9848fd6 1243
81740aa5 1244//
1245// Ultralight C Read Single Block
1246//
1247int CmdHF14AMfUCRdBl(const char *Cmd)
1248{
f168b263 1249 UsbCommand resp;
1250 bool hasPwd = FALSE;
afceaf40 1251 uint8_t blockNo = -1;
f9848fd6 1252 uint8_t key[16];
afceaf40 1253 char cmdp = param_getchar(Cmd, 0);
75377d29 1254
81740aa5 1255 if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
75377d29 1256 PrintAndLog("Usage: hf mfu crdbl <block number> <key>");
f168b263 1257 PrintAndLog("");
1258 PrintAndLog("sample: hf mfu crdbl 0");
b3125340 1259 PrintAndLog(" hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
afceaf40 1260 return 0;
75377d29 1261 }
1262
afceaf40 1263 blockNo = param_get8(Cmd, 0);
81740aa5 1264 if (blockNo < 0) {
1265 PrintAndLog("Wrong block number");
1266 return 1;
1267 }
75377d29 1268
f168b263 1269 if (blockNo > MAX_ULC_BLOCKS ){
1270 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
afceaf40
MHS
1271 return 1;
1272 }
81740aa5 1273
f168b263 1274 // key
1275 if ( strlen(Cmd) > 3){
1276 if (param_gethex(Cmd, 1, key, 32)) {
1277 PrintAndLog("Key must include %d HEX symbols", 32);
1278 return 1;
1279 } else {
1280 hasPwd = TRUE;
1281 }
1282 }
75377d29 1283 //uint8_t *key2 = SwapEndian64(key, 16);
afceaf40
MHS
1284
1285 //Read Block
f168b263 1286 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
1287 if ( hasPwd ) {
1288 c.arg[1] = 1;
75377d29 1289 memcpy(c.d.asBytes,key,16);
f168b263 1290 }
1291 SendCommand(&c);
1292
1293 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1294 uint8_t isOK = resp.arg[0] & 0xff;
1295 if (isOK) {
1296 uint8_t *data = resp.d.asBytes;
1297 PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4));
1298 }
1299 else {
1300 PrintAndLog("Failed reading block: (%02x)", isOK);
1301 }
81740aa5 1302 } else {
f168b263 1303 PrintAndLog("Command execute time-out");
81740aa5 1304 }
afceaf40 1305 return 0;
81740aa5 1306}
1307
1308//
1309// Mifare Ultralight C Write Single Block
1310//
1311int CmdHF14AMfUCWrBl(const char *Cmd){
afceaf40
MHS
1312
1313 uint8_t blockNo = -1;
1314 bool chinese_card = FALSE;
1315 uint8_t bldata[16] = {0x00};
1316 UsbCommand resp;
81740aa5 1317
afceaf40 1318 char cmdp = param_getchar(Cmd, 0);
81740aa5 1319
1320 if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
afceaf40 1321 PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
81740aa5 1322 PrintAndLog(" [block number]");
1323 PrintAndLog(" [block data] - (8 hex symbols)");
1324 PrintAndLog(" [w] - Chinese magic ultralight tag");
1325 PrintAndLog("");
afceaf40 1326 PrintAndLog(" sample: hf mfu cwrbl 0 01020304");
81740aa5 1327 PrintAndLog("");
afceaf40
MHS
1328 return 0;
1329 }
81740aa5 1330
afceaf40 1331 blockNo = param_get8(Cmd, 0);
f168b263 1332 if (blockNo > MAX_ULC_BLOCKS ){
afceaf40
MHS
1333 PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
1334 return 1;
1335 }
81740aa5 1336
afceaf40
MHS
1337 if (param_gethex(Cmd, 1, bldata, 8)) {
1338 PrintAndLog("Block data must include 8 HEX symbols");
1339 return 1;
1340 }
b3125340 1341
afceaf40
MHS
1342 if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) {
1343 chinese_card = TRUE;
1344 }
b3125340 1345
81740aa5 1346 if ( blockNo <= 3 ) {
1347 if (!chinese_card){
b3125340 1348 PrintAndLog("Access Denied");
1349 return 1;
81740aa5 1350 } else {
1351 PrintAndLog("--Special block no: 0x%02x", blockNo);
1352 PrintAndLog("--Data: %s", sprint_hex(bldata, 4));
1353 UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
1354 memcpy(d.d.asBytes,bldata, 4);
1355 SendCommand(&d);
1356 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1357 uint8_t isOK = resp.arg[0] & 0xff;
1358 PrintAndLog("isOk:%02x", isOK);
1359 } else {
1360 PrintAndLog("Command execute timeout");
b3125340 1361 return 1;
1362 }
1363 }
81740aa5 1364 } else {
afceaf40
MHS
1365 PrintAndLog("--Block no : 0x%02x", blockNo);
1366 PrintAndLog("--Data: %s", sprint_hex(bldata, 4));
1367 UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
1368 memcpy(e.d.asBytes,bldata, 4);
1369 SendCommand(&e);
1370 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1371 uint8_t isOK = resp.arg[0] & 0xff;
1372 PrintAndLog("isOk : %02x", isOK);
1373 } else {
1374 PrintAndLog("Command execute timeout");
b3125340 1375 return 1;
afceaf40 1376 }
81740aa5 1377 }
1378 return 0;
1379}
1380
f168b263 1381//
1382// Mifare Ultralight C - Set password
1383//
1384int CmdHF14AMfucSetPwd(const char *Cmd){
1385
1386 uint8_t pwd[16] = {0x00};
1387
1388 char cmdp = param_getchar(Cmd, 0);
1389
1390 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
1391 PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>");
1392 PrintAndLog(" [password] - (32 hex symbols)");
1393 PrintAndLog("");
1394 PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1395 PrintAndLog("");
1396 return 0;
1397 }
1398
1399 if (param_gethex(Cmd, 0, pwd, 32)) {
1400 PrintAndLog("Password must include 32 HEX symbols");
1401 return 1;
1402 }
1403
1404 UsbCommand c = {CMD_MIFAREUC_SETPWD};
1405 memcpy( c.d.asBytes, pwd, 16);
1406 SendCommand(&c);
1407
1408 UsbCommand resp;
1409
1410 if (WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1411 if ( (resp.arg[0] & 0xff) == 1)
1412 PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd,16));
1413 else{
1414 PrintAndLog("Failed writing at block %d", resp.arg[1] & 0xff);
1415 return 1;
1416 }
1417 }
1418 else {
1419 PrintAndLog("command execution time out");
1420 return 1;
1421 }
1422
1423 return 0;
1424}
1425
1426//
92690507 1427// Magic UL / UL-C tags - Set UID
f168b263 1428//
1429int CmdHF14AMfucSetUid(const char *Cmd){
1430
1431 UsbCommand c;
1432 UsbCommand resp;
1433 uint8_t uid[7] = {0x00};
1434 char cmdp = param_getchar(Cmd, 0);
1435
1436 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
1437 PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>");
1438 PrintAndLog(" [uid] - (14 hex symbols)");
1439 PrintAndLog("\nThis only works for Magic Ultralight tags.");
1440 PrintAndLog("");
1441 PrintAndLog("sample: hf mfu setuid 11223344556677");
1442 PrintAndLog("");
1443 return 0;
1444 }
1445
1446 if (param_gethex(Cmd, 0, uid, 14)) {
1447 PrintAndLog("UID must include 14 HEX symbols");
1448 return 1;
1449 }
1450
1451 // read block2.
1452 c.cmd = CMD_MIFAREU_READBL;
1453 c.arg[0] = 2;
1454 SendCommand(&c);
1455 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1456 PrintAndLog("Command execute timeout");
1457 return 2;
1458 }
1459
1460 // save old block2.
1461 uint8_t oldblock2[4] = {0x00};
1462 memcpy(resp.d.asBytes, oldblock2, 4);
1463
1464 // block 0.
1465 c.cmd = CMD_MIFAREU_WRITEBL;
1466 c.arg[0] = 0;
1467 c.d.asBytes[0] = uid[0];
1468 c.d.asBytes[1] = uid[1];
1469 c.d.asBytes[2] = uid[2];
1470 c.d.asBytes[3] = 0x88 ^ uid[0] ^ uid[1] ^ uid[2];
1471 SendCommand(&c);
1472 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1473 PrintAndLog("Command execute timeout");
1474 return 3;
1475 }
1476
1477 // block 1.
1478 c.arg[0] = 1;
1479 c.d.asBytes[0] = uid[3];
1480 c.d.asBytes[1] = uid[4];
1481 c.d.asBytes[2] = uid[5];
1482 c.d.asBytes[3] = uid[6];
1483 SendCommand(&c);
1484 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1485 PrintAndLog("Command execute timeout");
1486 return 4;
1487 }
1488
1489 // block 2.
1490 c.arg[0] = 2;
1491 c.d.asBytes[0] = uid[3] ^ uid[4] ^ uid[5] ^ uid[6];
1492 c.d.asBytes[1] = oldblock2[1];
1493 c.d.asBytes[2] = oldblock2[2];
1494 c.d.asBytes[3] = oldblock2[3];
1495 SendCommand(&c);
1496 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1497 PrintAndLog("Command execute timeout");
1498 return 5;
1499 }
1500
1501 return 0;
1502}
1503
1504int CmdHF14AMfuGenDiverseKeys(const char *Cmd){
1505
1506 uint8_t iv[8] = { 0x00 };
1507 uint8_t block = 0x07;
1508
1509 // UL-EV1
1510 //04 57 b6 e2 05 3f 80 UID
1511 //4a f8 4b 19 PWD
1512 uint8_t uid[] = { 0xF4,0xEA, 0x54, 0x8E };
1513 uint8_t mifarekeyA[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1514 uint8_t mifarekeyB[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1515 uint8_t dkeyA[8] = { 0x00 };
1516 uint8_t dkeyB[8] = { 0x00 };
1517
1518 uint8_t masterkey[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1519
1520 uint8_t mix[8] = { 0x00 };
1521 uint8_t divkey[8] = { 0x00 };
1522
1523 memcpy(mix, mifarekeyA, 4);
1524
1525 mix[4] = mifarekeyA[4] ^ uid[0];
1526 mix[5] = mifarekeyA[5] ^ uid[1];
1527 mix[6] = block ^ uid[2];
1528 mix[7] = uid[3];
1529
1530 des3_context ctx = { 0x00 };
1531 des3_set2key_enc(&ctx, masterkey);
1532
1533 des3_crypt_cbc(&ctx // des3_context
1534 , DES_ENCRYPT // int mode
1535 , sizeof(mix) // length
1536 , iv // iv[8]
1537 , mix // input
1538 , divkey // output
1539 );
1540
1541 PrintAndLog("3DES version");
1542 PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey,sizeof(masterkey)));
1543 PrintAndLog("UID :\t %s", sprint_hex(uid, sizeof(uid)));
1544 PrintAndLog("Sector :\t %0d", block);
1545 PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA, sizeof(mifarekeyA)));
1546 PrintAndLog("Message :\t %s", sprint_hex(mix, sizeof(mix)));
1547 PrintAndLog("Diversified key: %s", sprint_hex(divkey+1, 6));
1548
1549 PrintAndLog("\n DES version");
1550
1551 for (int i=0; i < sizeof(mifarekeyA); ++i){
1552 dkeyA[i] = (mifarekeyA[i] << 1) & 0xff;
1553 dkeyA[6] |= ((mifarekeyA[i] >> 7) & 1) << (i+1);
1554 }
1555
1556 for (int i=0; i < sizeof(mifarekeyB); ++i){
1557 dkeyB[1] |= ((mifarekeyB[i] >> 7) & 1) << (i+1);
1558 dkeyB[2+i] = (mifarekeyB[i] << 1) & 0xff;
1559 }
1560
1561 uint8_t zeros[8] = {0x00};
1562 uint8_t newpwd[8] = {0x00};
1563 uint8_t dmkey[24] = {0x00};
1564 memcpy(dmkey, dkeyA, 8);
1565 memcpy(dmkey+8, dkeyB, 8);
1566 memcpy(dmkey+16, dkeyA, 8);
1567 memset(iv, 0x00, 8);
1568
1569 des3_set3key_enc(&ctx, dmkey);
1570
1571 des3_crypt_cbc(&ctx // des3_context
1572 , DES_ENCRYPT // int mode
1573 , sizeof(newpwd) // length
1574 , iv // iv[8]
1575 , zeros // input
1576 , newpwd // output
1577 );
1578
1579 PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA, sizeof(dkeyA)));
1580 PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB, sizeof(dkeyB)));
1581 PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey, sizeof(dmkey)));
1582 PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd, sizeof(newpwd)));
1583
1584 return 0;
1585}
1586
1587// static uint8_t * diversify_key(uint8_t * key){
1588
1589 // for(int i=0; i<16; i++){
1590 // if(i<=6) key[i]^=cuid[i];
1591 // if(i>6) key[i]^=cuid[i%7];
1592 // }
1593 // return key;
1594// }
1595
1596// static void GenerateUIDe( uint8_t *uid, uint8_t len){
1597 // for (int i=0; i<len; ++i){
1598
1599 // }
1600 // return;
1601// }
1602
81740aa5 1603//------------------------------------
1604// Menu Stuff
1605//------------------------------------
1606static command_t CommandTable[] =
1607{
92690507 1608 {"help", CmdHelp, 1, "This help"},
1609 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1610 {"info", CmdHF14AMfUInfo, 0, "Tag information"},
1611 {"dump", CmdHF14AMfUDump, 0, "Dump Ultralight / Ultralight-C tag to binary file"},
1612 {"rdbl", CmdHF14AMfURdBl, 0, "Read block - Ultralight"},
1613 {"wrbl", CmdHF14AMfUWrBl, 0, "Write block - Ultralight"},
1614 {"crdbl", CmdHF14AMfUCRdBl, 0, "Read block - Ultralight C"},
1615 {"cwrbl", CmdHF14AMfUCWrBl, 0, "Write block - Ultralight C"},
1616 {"cauth", CmdHF14AMfucAuth, 0, "Authentication - Ultralight C"},
1617 {"setpwd", CmdHF14AMfucSetPwd, 1, "Set 3des password - Ultralight-C"},
1618 {"setuid", CmdHF14AMfucSetUid, 1, "Set UID - MAGIC tags only"},
f168b263 1619 {"gen", CmdHF14AMfuGenDiverseKeys , 1, "Generate 3des mifare diversified keys"},
afceaf40 1620 {NULL, NULL, 0, NULL}
81740aa5 1621};
1622
1623int CmdHFMFUltra(const char *Cmd){
afceaf40
MHS
1624 WaitForResponseTimeout(CMD_ACK,NULL,100);
1625 CmdsParse(CommandTable, Cmd);
1626 return 0;
81740aa5 1627}
1628
1629int CmdHelp(const char *Cmd){
afceaf40
MHS
1630 CmdsHelp(CommandTable);
1631 return 0;
f168b263 1632}
Impressum, Datenschutz