]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhfmfu.c
dac51be35a32c20cc616e4c41a473b736c2c9b85
[proxmark3-svn] / client / cmdhfmfu.c
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 //-----------------------------------------------------------------------------
10
11 #include "cmdhfmfu.h"
12
13 #include <stdint.h>
14 #include <stdio.h>
15 #include "comms.h"
16 #include "usb_cmd.h"
17 #include "cmdmain.h"
18 #include "ui.h"
19 #include "mbedtls/des.h"
20 #include "cmdhfmf.h"
21 #include "cmdhf14a.h" // DropField()
22 #include "mifare.h"
23 #include "util.h"
24 #include "protocols.h"
25 #include "taginfo.h"
26
27 typedef enum TAGTYPE_UL {
28 UNKNOWN = 0x000000,
29 UL = 0x000001,
30 UL_C = 0x000002,
31 UL_EV1_48 = 0x000004,
32 UL_EV1_128 = 0x000008,
33 NTAG = 0x000010,
34 NTAG_203 = 0x000020,
35 NTAG_210 = 0x000040,
36 NTAG_212 = 0x000080,
37 NTAG_213 = 0x000100,
38 NTAG_215 = 0x000200,
39 NTAG_216 = 0x000400,
40 MY_D = 0x000800,
41 MY_D_NFC = 0x001000,
42 MY_D_MOVE = 0x002000,
43 MY_D_MOVE_NFC = 0x004000,
44 MY_D_MOVE_LEAN= 0x008000,
45 NTAG_I2C_1K = 0x010000,
46 NTAG_I2C_2K = 0x020000,
47 FUDAN_UL = 0x040000,
48 MAGIC = 0x080000,
49 UL_MAGIC = UL | MAGIC,
50 UL_C_MAGIC = UL_C | MAGIC,
51 UL_ERROR = 0xFFFFFF,
52 } TagTypeUL_t;
53
54 #define MAX_UL_BLOCKS 0x0f
55 #define MAX_ULC_BLOCKS 0x2b
56 #define MAX_ULEV1a_BLOCKS 0x13
57 #define MAX_ULEV1b_BLOCKS 0x28
58 #define MAX_NTAG_203 0x29
59 #define MAX_NTAG_210 0x13
60 #define MAX_NTAG_212 0x28
61 #define MAX_NTAG_213 0x2c
62 #define MAX_NTAG_215 0x86
63 #define MAX_NTAG_216 0xe6
64 #define MAX_MY_D_NFC 0xff
65 #define MAX_MY_D_MOVE 0x25
66 #define MAX_MY_D_MOVE_LEAN 0x0f
67
68 #define PUBLIC_ECDA_KEYLEN 33
69 static uint8_t public_ecda_key[PUBLIC_ECDA_KEYLEN] = {
70 0x04, 0x49, 0x4e, 0x1a, 0x38, 0x6d, 0x3d, 0x3c,
71 0xfe, 0x3d, 0xc1, 0x0e, 0x5d, 0xe6, 0x8a, 0x49,
72 0x9b, 0x1c, 0x20, 0x2d, 0xb5, 0xb1, 0x32, 0x39,
73 0x3e, 0x89, 0xed, 0x19, 0xfe, 0x5b, 0xe8, 0xbc,
74 0x61
75 };
76
77 #define KEYS_3DES_COUNT 7
78 static uint8_t default_3des_keys[KEYS_3DES_COUNT][16] = {
79 { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key
80 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes
81 { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F
82 { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key
83 { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones
84 { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF
85 { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33
86 };
87
88 #define KEYS_PWD_COUNT 6
89 static uint8_t default_pwd_pack[KEYS_PWD_COUNT][4] = {
90 {0xFF,0xFF,0xFF,0xFF}, // PACK 0x00,0x00 -- factory default
91 {0x4A,0xF8,0x4B,0x19}, // PACK 0xE5,0xBE -- italian bus (sniffed)
92 {0x33,0x6B,0xA1,0x19}, // PACK 0x9c,0x2d -- italian bus (sniffed)
93 {0xFF,0x90,0x6C,0xB2}, // PACK 0x12,0x9e -- italian bus (sniffed)
94 {0x46,0x1c,0xA3,0x19}, // PACK 0xE9,0x5A -- italian bus (sniffed)
95 {0x35,0x1C,0xD0,0x19}, // PACK 0x9A,0x5a -- italian bus (sniffed)
96 };
97
98 #define MAX_UL_TYPES 18
99 static uint32_t UL_TYPES_ARRAY[MAX_UL_TYPES] = {UNKNOWN, UL, UL_C, UL_EV1_48, UL_EV1_128, NTAG, NTAG_203,
100 NTAG_210, NTAG_212, NTAG_213, NTAG_215, NTAG_216, MY_D, MY_D_NFC, MY_D_MOVE, MY_D_MOVE_NFC, MY_D_MOVE_LEAN, FUDAN_UL};
101
102 static uint8_t UL_MEMORY_ARRAY[MAX_UL_TYPES] = {MAX_UL_BLOCKS, MAX_UL_BLOCKS, MAX_ULC_BLOCKS, MAX_ULEV1a_BLOCKS,
103 MAX_ULEV1b_BLOCKS, MAX_NTAG_203, MAX_NTAG_203, MAX_NTAG_210, MAX_NTAG_212, MAX_NTAG_213,
104 MAX_NTAG_215, MAX_NTAG_216, MAX_UL_BLOCKS, MAX_MY_D_NFC, MAX_MY_D_MOVE, MAX_MY_D_MOVE, MAX_MY_D_MOVE_LEAN, MAX_UL_BLOCKS};
105
106
107 // get version nxp product type
108 static char *getProductTypeStr( uint8_t id){
109
110 static char buf[20];
111 char *retStr = buf;
112
113 switch(id) {
114 case 3: sprintf(retStr, "%02X, Ultralight", id); break;
115 case 4: sprintf(retStr, "%02X, NTAG", id); break;
116 default: sprintf(retStr, "%02X, unknown", id); break;
117 }
118 return buf;
119 }
120
121 /*
122 The 7 MSBits (=n) code the storage size itself based on 2^n,
123 the LSBit is set to '0' if the size is exactly 2^n
124 and set to '1' if the storage size is between 2^n and 2^(n+1).
125 */
126 char *getUlev1CardSizeStr( uint8_t fsize ){
127
128 static char buf[40];
129 char *retStr = buf;
130 memset(buf, 0, sizeof(buf));
131
132 uint16_t usize = 1 << ((fsize >>1) + 1);
133 uint16_t lsize = 1 << (fsize >>1);
134
135 // is LSB set?
136 if ( fsize & 1 )
137 sprintf(retStr, "%02X, (%u <-> %u bytes)",fsize, usize, lsize);
138 else
139 sprintf(retStr, "%02X, (%u bytes)", fsize, lsize);
140 return buf;
141 }
142
143 static void ul_switch_on_field(void) {
144 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT | ISO14A_NO_RATS, 0, 0}};
145 clearCommandBuffer();
146 SendCommand(&c);
147 }
148
149 static int ul_send_cmd_raw( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength ) {
150 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_APPEND_CRC, cmdlen, 0}};
151 memcpy(c.d.asBytes, cmd, cmdlen);
152 clearCommandBuffer();
153 SendCommand(&c);
154 UsbCommand resp;
155 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
156 if (!resp.arg[0] && responseLength) return -1;
157
158 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
159 memcpy(response, resp.d.asBytes, resplen);
160 return resplen;
161 }
162 /*
163 static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) {
164 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}};
165 if (append_crc)
166 c.arg[0] |= ISO14A_APPEND_CRC;
167
168 memcpy(c.d.asBytes, cmd, cmdlen);
169 clearCommandBuffer();
170 SendCommand(&c);
171 UsbCommand resp;
172 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
173 if (!resp.arg[0] && responseLength) return -1;
174
175 uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
176 memcpy(response, resp.d.asBytes, resplen);
177 return resplen;
178 }
179 */
180 static int ul_select( iso14a_card_select_t *card ){
181
182 ul_switch_on_field();
183
184 UsbCommand resp;
185 bool ans = false;
186 ans = WaitForResponseTimeout(CMD_ACK, &resp, 1500);
187 if (!ans || resp.arg[0] < 1) {
188 PrintAndLogEx(WARNING, "iso14443a card select failed");
189 DropField();
190 return 0;
191 }
192
193 memcpy(card, resp.d.asBytes, sizeof(iso14a_card_select_t));
194 return 1;
195 }
196
197 // This read command will at least return 16bytes.
198 static int ul_read( uint8_t page, uint8_t *response, uint16_t responseLength ){
199
200 uint8_t cmd[] = {ISO14443A_CMD_READBLOCK, page};
201 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
202 return len;
203 }
204
205 static int ul_comp_write( uint8_t page, uint8_t *data, uint8_t datalen ){
206
207 uint8_t cmd[18];
208 memset(cmd, 0x00, sizeof(cmd));
209 datalen = ( datalen > 16) ? 16 : datalen;
210
211 cmd[0] = ISO14443A_CMD_WRITEBLOCK;
212 cmd[1] = page;
213 memcpy(cmd+2, data, datalen);
214
215 uint8_t response[1] = {0xff};
216 ul_send_cmd_raw(cmd, 2+datalen, response, sizeof(response));
217 // ACK
218 if ( response[0] == 0x0a ) return 0;
219 // NACK
220 return -1;
221 }
222
223 static int ulc_requestAuthentication( uint8_t *nonce, uint16_t nonceLength ){
224
225 uint8_t cmd[] = {MIFARE_ULC_AUTH_1, 0x00};
226 int len = ul_send_cmd_raw(cmd, sizeof(cmd), nonce, nonceLength);
227 return len;
228 }
229
230 static int ulc_authentication( uint8_t *key, bool switch_off_field ){
231
232 UsbCommand c = {CMD_MIFAREUC_AUTH, {switch_off_field}};
233 memcpy(c.d.asBytes, key, 16);
234 clearCommandBuffer();
235 SendCommand(&c);
236 UsbCommand resp;
237 if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return 0;
238 if ( resp.arg[0] == 1 ) return 1;
239
240 return 0;
241 }
242
243 static int ulev1_requestAuthentication( uint8_t *pwd, uint8_t *pack, uint16_t packLength ){
244
245 uint8_t cmd[] = {MIFARE_ULEV1_AUTH, pwd[0], pwd[1], pwd[2], pwd[3]};
246 int len = ul_send_cmd_raw(cmd, sizeof(cmd), pack, packLength);
247 return len;
248 }
249
250 static int ul_auth_select( iso14a_card_select_t *card, TagTypeUL_t tagtype, bool hasAuthKey, uint8_t *authenticationkey, uint8_t *pack, uint8_t packSize){
251 if ( hasAuthKey && (tagtype & UL_C)) {
252 //will select card automatically and close connection on error
253 if (!ulc_authentication(authenticationkey, false)) {
254 PrintAndLogEx(WARNING, "Authentication Failed UL-C");
255 return 0;
256 }
257 } else {
258 if ( !ul_select(card) ) return 0;
259
260 if (hasAuthKey) {
261 if (ulev1_requestAuthentication(authenticationkey, pack, packSize) < 1) {
262 DropField();
263 PrintAndLogEx(WARNING, "Authentication Failed UL-EV1/NTAG");
264 return 0;
265 }
266 }
267 }
268 return 1;
269 }
270
271 static int ulev1_getVersion( uint8_t *response, uint16_t responseLength ){
272
273 uint8_t cmd[] = {MIFARE_ULEV1_VERSION};
274 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
275 return len;
276 }
277
278 // static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){
279
280 // uint8_t cmd[] = {MIFARE_ULEV1_FASTREAD, startblock, endblock};
281
282 // if ( !ul_send_cmd_raw(cmd, sizeof(cmd), response)){
283 // return -1;
284 // }
285 // return 0;
286 // }
287
288 static int ulev1_readCounter( uint8_t counter, uint8_t *response, uint16_t responseLength ){
289
290 uint8_t cmd[] = {MIFARE_ULEV1_READ_CNT, counter};
291 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
292 return len;
293 }
294
295 static int ulev1_readTearing( uint8_t counter, uint8_t *response, uint16_t responseLength ){
296
297 uint8_t cmd[] = {MIFARE_ULEV1_CHECKTEAR, counter};
298 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
299 return len;
300 }
301
302 static int ulev1_readSignature( uint8_t *response, uint16_t responseLength ){
303
304 uint8_t cmd[] = {MIFARE_ULEV1_READSIG, 0x00};
305 int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
306 return len;
307 }
308
309
310 // Fudan check checks for which error is given for a command with incorrect crc
311 // NXP UL chip responds with 01, fudan 00.
312 // other possible checks:
313 // send a0 + crc
314 // UL responds with 00, fudan doesn't respond
315 // or
316 // send a200 + crc
317 // UL doesn't respond, fudan responds with 00
318 // or
319 // send 300000 + crc (read with extra byte(s))
320 // UL responds with read of page 0, fudan doesn't respond.
321 //
322 // make sure field is off before calling this function
323 static int ul_fudan_check( void ){
324 iso14a_card_select_t card;
325 if ( !ul_select(&card) )
326 return UL_ERROR;
327
328 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT, 4, 0}};
329
330 uint8_t cmd[4] = {0x30,0x00,0x02,0xa7}; //wrong crc on purpose should be 0xa8
331 memcpy(c.d.asBytes, cmd, 4);
332 clearCommandBuffer();
333 SendCommand(&c);
334 UsbCommand resp;
335 if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return UL_ERROR;
336 if (resp.arg[0] != 1) return UL_ERROR;
337
338 return (!resp.d.asBytes[0]) ? FUDAN_UL : UL; //if response == 0x00 then Fudan, else Genuine NXP
339 }
340
341 static int ul_print_default( uint8_t *data){
342
343 uint8_t uid[7];
344 uid[0] = data[0];
345 uid[1] = data[1];
346 uid[2] = data[2];
347 uid[3] = data[4];
348 uid[4] = data[5];
349 uid[5] = data[6];
350 uid[6] = data[7];
351
352 PrintAndLogEx(NORMAL," UID : %s ", sprint_hex(uid, 7));
353 PrintAndLogEx(NORMAL," UID[0] : %02X, %s", uid[0], getManufacturerName(uid[0]));
354 if ( uid[0] == 0x05 && ((uid[1] & 0xf0) >> 4) == 2 ) { // is infineon and 66RxxP
355 uint8_t chip = (data[8] & 0xC7); // 11000111 mask, bit 3,4,5 RFU
356 switch (chip){
357 case 0xc2: PrintAndLogEx(NORMAL, " IC type : SLE 66R04P 770 Bytes"); break; //77 pages
358 case 0xc4: PrintAndLogEx(NORMAL, " IC type : SLE 66R16P 2560 Bytes"); break; //256 pages
359 case 0xc6: PrintAndLogEx(NORMAL, " IC type : SLE 66R32P 5120 Bytes"); break; //512 pages /2 sectors
360 }
361 }
362 // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2
363 int crc0 = 0x88 ^ data[0] ^ data[1] ^data[2];
364 if ( data[3] == crc0 )
365 PrintAndLogEx(NORMAL, " BCC0 : %02X, Ok", data[3]);
366 else
367 PrintAndLogEx(NORMAL, " BCC0 : %02X, crc should be %02X", data[3], crc0);
368
369 int crc1 = data[4] ^ data[5] ^ data[6] ^data[7];
370 if ( data[8] == crc1 )
371 PrintAndLogEx(NORMAL, " BCC1 : %02X, Ok", data[8]);
372 else
373 PrintAndLogEx(NORMAL, " BCC1 : %02X, crc should be %02X", data[8], crc1 );
374
375 PrintAndLogEx(NORMAL, " Internal : %02X, %sdefault", data[9], (data[9]==0x48)?"":"not " );
376
377 PrintAndLogEx(NORMAL, " Lock : %s (binary %s %s)",
378 sprint_hex(data+10, 2),
379 printBits(1, data+10),
380 printBits(1, data+11)
381 );
382
383 PrintAndLogEx(NORMAL, "OneTimePad : %s (binary %s %s %s %s)\n",
384 sprint_hex(data + 12, 4),
385 printBits(1, data+12),
386 printBits(1, data+13),
387 printBits(1, data+14),
388 printBits(1, data+15)
389 );
390
391 return 0;
392 }
393
394 static int ndef_print_CC(uint8_t *data) {
395 // no NDEF message
396 if(data[0] != 0xe1)
397 return -1;
398
399 PrintAndLogEx(NORMAL, "--- NDEF Message");
400 PrintAndLogEx(NORMAL, "Capability Container: %s", sprint_hex(data,4) );
401 PrintAndLogEx(NORMAL, " %02X : NDEF Magic Number", data[0]);
402 PrintAndLogEx(NORMAL, " %02X : version %d.%d supported by tag", data[1], (data[1] & 0xF0) >> 4, data[1] & 0x0f);
403 PrintAndLogEx(NORMAL, " %02X : Physical Memory Size: %d bytes", data[2], (data[2] + 1) * 8);
404 if ( data[2] == 0x12 )
405 PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 144);
406 else if ( data[2] == 0x3e )
407 PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 496);
408 else if ( data[2] == 0x6d )
409 PrintAndLogEx(NORMAL, " %02X : NDEF Memory Size: %d bytes", data[2], 872);
410
411 PrintAndLogEx(NORMAL, " %02X : %s / %s", data[3],
412 (data[3] & 0xF0) ? "(RFU)" : "Read access granted without any security",
413 (data[3] & 0x0F)==0 ? "Write access granted without any security" : (data[3] & 0x0F)==0x0F ? "No write access granted at all" : "(RFU)");
414 return 0;
415 }
416
417 int ul_print_type(uint32_t tagtype, uint8_t spaces){
418 char spc[11] = " ";
419 spc[10]=0x00;
420 char *spacer = spc + (10-spaces);
421
422 if ( tagtype & UL )
423 PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight (MF0ICU1) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" );
424 else if ( tagtype & UL_C)
425 PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight C (MF0ULC) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" );
426 else if ( tagtype & UL_EV1_48)
427 PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight EV1 48bytes (MF0UL1101)", spacer);
428 else if ( tagtype & UL_EV1_128)
429 PrintAndLogEx(NORMAL, "%sTYPE : MIFARE Ultralight EV1 128bytes (MF0UL2101)", spacer);
430 else if ( tagtype & NTAG )
431 PrintAndLogEx(NORMAL, "%sTYPE : NTAG UNKNOWN", spacer);
432 else if ( tagtype & NTAG_203 )
433 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 203 144bytes (NT2H0301F0DT)", spacer);
434 else if ( tagtype & NTAG_210 )
435 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 210 48bytes (NT2L1011G0DU)", spacer);
436 else if ( tagtype & NTAG_212 )
437 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 212 128bytes (NT2L1211G0DU)", spacer);
438 else if ( tagtype & NTAG_213 )
439 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 213 144bytes (NT2H1311G0DU)", spacer);
440 else if ( tagtype & NTAG_215 )
441 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 215 504bytes (NT2H1511G0DU)", spacer);
442 else if ( tagtype & NTAG_216 )
443 PrintAndLogEx(NORMAL, "%sTYPE : NTAG 216 888bytes (NT2H1611G0DU)", spacer);
444 else if ( tagtype & NTAG_I2C_1K )
445 PrintAndLogEx(NORMAL, "%sTYPE : NTAG I%sC 888bytes (NT3H1101FHK)", spacer, "\xFD");
446 else if ( tagtype & NTAG_I2C_2K )
447 PrintAndLogEx(NORMAL, "%sTYPE : NTAG I%sC 1904bytes (NT3H1201FHK)", spacer, "\xFD");
448 else if ( tagtype & MY_D )
449 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 (SLE 66RxxS)", spacer);
450 else if ( tagtype & MY_D_NFC )
451 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 NFC (SLE 66RxxP)", spacer);
452 else if ( tagtype & MY_D_MOVE )
453 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 move (SLE 66R01P)", spacer);
454 else if ( tagtype & MY_D_MOVE_NFC )
455 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 move NFC (SLE 66R01P)", spacer);
456 else if ( tagtype & MY_D_MOVE_LEAN )
457 PrintAndLogEx(NORMAL, "%sTYPE : INFINEON my-d\x99 move lean (SLE 66R01L)", spacer);
458 else if ( tagtype & FUDAN_UL )
459 PrintAndLogEx(NORMAL, "%sTYPE : FUDAN Ultralight Compatible (or other compatible) %s", spacer, (tagtype & MAGIC) ? "<magic>" : "" );
460 else
461 PrintAndLogEx(NORMAL, "%sTYPE : Unknown %06x", spacer, tagtype);
462 return 0;
463 }
464
465 static int ulc_print_3deskey( uint8_t *data){
466 PrintAndLogEx(NORMAL, " deskey1 [44/0x2C] : %s [%.4s]", sprint_hex(data ,4),data);
467 PrintAndLogEx(NORMAL, " deskey1 [45/0x2D] : %s [%.4s]", sprint_hex(data+4 ,4),data+4);
468 PrintAndLogEx(NORMAL, " deskey2 [46/0x2E] : %s [%.4s]", sprint_hex(data+8 ,4),data+8);
469 PrintAndLogEx(NORMAL, " deskey2 [47/0x2F] : %s [%.4s]", sprint_hex(data+12,4),data+12);
470 PrintAndLogEx(NORMAL, "\n 3des key : %s", sprint_hex(SwapEndian64(data, 16, 8), 16));
471 return 0;
472 }
473
474 static int ulc_print_configuration( uint8_t *data){
475
476 PrintAndLogEx(NORMAL, "--- UL-C Configuration");
477 PrintAndLogEx(NORMAL, " Higher Lockbits [40/0x28] : %s (binary %s %s)",
478 sprint_hex(data, 2),
479 printBits(1, data),
480 printBits(1, data+1)
481 );
482 PrintAndLogEx(NORMAL, " Counter [41/0x29] : %s (binary %s %s %s %s)",
483 sprint_hex(data+4, 4),
484 printBits(1, data+4),
485 printBits(1, data+5),
486 printBits(1, data+6),
487 printBits(1, data+7)
488 );
489
490 bool validAuth = (data[8] >= 0x03 && data[8] <= 0x30);
491 if ( validAuth )
492 PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s page %d/0x%02X and above need authentication", sprint_hex(data+8, 4), data[8], data[8] );
493 else{
494 if ( data[8] == 0){
495 PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s default", sprint_hex(data+8, 4) );
496 } else {
497 PrintAndLogEx(NORMAL, " Auth0 [42/0x2A] : %s auth byte is out-of-range", sprint_hex(data+8, 4) );
498 }
499 }
500 PrintAndLogEx(NORMAL, " Auth1 [43/0x2B] : %s %s",
501 sprint_hex(data+12, 4),
502 (data[12] & 1) ? "write access restricted": "read and write access restricted"
503 );
504 return 0;
505 }
506
507 static int ulev1_print_configuration( uint8_t *data, uint8_t startPage){
508
509 PrintAndLogEx(NORMAL, "\n--- Tag Configuration");
510
511 bool strg_mod_en = (data[0] & 2);
512 uint8_t authlim = (data[4] & 0x07);
513 bool cfglck = (data[4] & 0x40);
514 bool prot = (data[4] & 0x80);
515 uint8_t vctid = data[5];
516
517 PrintAndLogEx(NORMAL, " cfg0 [%u/0x%02X] : %s", startPage, startPage, sprint_hex(data, 4));
518 if ( data[3] < 0xff )
519 PrintAndLogEx(NORMAL, " - page %d and above need authentication",data[3]);
520 else
521 PrintAndLogEx(NORMAL, " - pages don't need authentication");
522 PrintAndLogEx(NORMAL, " - strong modulation mode %s", (strg_mod_en) ? "enabled" : "disabled");
523 PrintAndLogEx(NORMAL, " cfg1 [%u/0x%02X] : %s", startPage + 1, startPage + 1, sprint_hex(data+4, 4) );
524 if ( authlim == 0)
525 PrintAndLogEx(NORMAL, " - Unlimited password attempts");
526 else
527 PrintAndLogEx(NORMAL, " - Max number of password attempts is %d", authlim);
528 PrintAndLogEx(NORMAL, " - user configuration %s", cfglck ? "permanently locked":"writeable");
529 PrintAndLogEx(NORMAL, " - %s access is protected with password", prot ? "read and write":"write");
530 PrintAndLogEx(NORMAL, " - %02X, Virtual Card Type Identifier is %s default", vctid, (vctid==0x05)? "":"not");
531 PrintAndLogEx(NORMAL, " PWD [%u/0x%02X] : %s- (cannot be read)", startPage + 2, startPage + 2, sprint_hex(data+8, 4));
532 PrintAndLogEx(NORMAL, " PACK [%u/0x%02X] : %s - (cannot be read)", startPage + 3, startPage + 3, sprint_hex(data+12, 2));
533 PrintAndLogEx(NORMAL, " RFU [%u/0x%02X] : %s- (cannot be read)", startPage + 3, startPage + 3, sprint_hex(data+14, 2));
534 return 0;
535 }
536
537 static int ulev1_print_counters(){
538 PrintAndLogEx(NORMAL, "--- Tag Counters");
539 uint8_t tear[1] = {0};
540 uint8_t counter[3] = {0,0,0};
541 uint16_t len = 0;
542 for ( uint8_t i = 0; i<3; ++i) {
543 ulev1_readTearing(i,tear,sizeof(tear));
544 len = ulev1_readCounter(i,counter, sizeof(counter) );
545 if (len == 3) {
546 PrintAndLogEx(NORMAL, " [%0d] : %s", i, sprint_hex(counter,3));
547 PrintAndLogEx(NORMAL, " - %02X tearing %s", tear[0], ( tear[0]==0xBD)?"Ok":"failure");
548 }
549 }
550 return len;
551 }
552
553 static int ulev1_print_signature( uint8_t *data, uint8_t len){
554 PrintAndLogEx(NORMAL, "\n--- Tag Signature");
555 PrintAndLogEx(NORMAL, "IC signature public key name : NXP NTAG21x (2013)");
556 PrintAndLogEx(NORMAL, "IC signature public key value : %s", sprint_hex(public_ecda_key, PUBLIC_ECDA_KEYLEN));
557 PrintAndLogEx(NORMAL, " Elliptic curve parameters : secp128r1");
558 PrintAndLogEx(NORMAL, " Tag ECC Signature : %s", sprint_hex(data, len));
559 //to do: verify if signature is valid
560 //PrintAndLogEx(NORMAL, "IC signature status: %s valid", (iseccvalid() )?"":"not");
561 return 0;
562 }
563
564 static int ulev1_print_version(uint8_t *data){
565 PrintAndLogEx(NORMAL, "\n--- Tag Version");
566 PrintAndLogEx(NORMAL, " Raw bytes : %s",sprint_hex(data, 8) );
567 PrintAndLogEx(NORMAL, " Vendor ID : %02X, %s", data[1], getManufacturerName(data[1]));
568 PrintAndLogEx(NORMAL, " Product type : %s", getProductTypeStr(data[2]));
569 PrintAndLogEx(NORMAL, " Product subtype : %02X, %s", data[3], (data[3]==1) ?"17 pF":"50pF");
570 PrintAndLogEx(NORMAL, " Major version : %02X", data[4]);
571 PrintAndLogEx(NORMAL, " Minor version : %02X", data[5]);
572 PrintAndLogEx(NORMAL, " Size : %s", getUlev1CardSizeStr(data[6]));
573 PrintAndLogEx(NORMAL, " Protocol type : %02X", data[7]);
574 return 0;
575 }
576
577 /*
578 static int ulc_magic_test(){
579 // Magic Ultralight test
580 // Magic UL-C, by observation,
581 // 1) it seems to have a static nonce response to 0x1A command.
582 // 2) the deskey bytes is not-zero:d out on as datasheet states.
583 // 3) UID - changeable, not only, but pages 0-1-2-3.
584 // 4) use the ul_magic_test ! magic tags answers specially!
585 int returnValue = UL_ERROR;
586 iso14a_card_select_t card;
587 uint8_t nonce1[11] = {0x00};
588 uint8_t nonce2[11] = {0x00};
589 int status = ul_select(&card);
590 if ( !status ){
591 return UL_ERROR;
592 }
593 status = ulc_requestAuthentication(nonce1, sizeof(nonce1));
594 if ( status > 0 ) {
595 status = ulc_requestAuthentication(nonce2, sizeof(nonce2));
596 returnValue = ( !memcmp(nonce1, nonce2, 11) ) ? UL_C_MAGIC : UL_C;
597 } else {
598 returnValue = UL;
599 }
600 DropField();
601 return returnValue;
602 }
603 */
604 static int ul_magic_test(){
605
606 // Magic Ultralight tests
607 // 1) take present UID, and try to write it back. OBSOLETE
608 // 2) make a wrong length write to page0, and see if tag answers with ACK/NACK:
609 iso14a_card_select_t card;
610 if ( !ul_select(&card) )
611 return UL_ERROR;
612 int status = ul_comp_write(0, NULL, 0);
613 DropField();
614 if ( status == 0 )
615 return MAGIC;
616 return 0;
617 }
618
619 uint32_t GetHF14AMfU_Type(void){
620
621 TagTypeUL_t tagtype = UNKNOWN;
622 iso14a_card_select_t card;
623 uint8_t version[10] = {0x00};
624 int status = 0;
625 int len;
626
627 if (!ul_select(&card)) return UL_ERROR;
628
629 // Ultralight - ATQA / SAK
630 if ( card.atqa[1] != 0x00 || card.atqa[0] != 0x44 || card.sak != 0x00 ) {
631 PrintAndLogEx(NORMAL, "Tag is not Ultralight | NTAG | MY-D [ATQA: %02X %02X SAK: %02X]\n", card.atqa[1], card.atqa[0], card.sak);
632 DropField();
633 return UL_ERROR;
634 }
635
636 if ( card.uid[0] != 0x05) {
637
638 len = ulev1_getVersion(version, sizeof(version));
639 DropField();
640
641 switch (len) {
642 case 0x0A: {
643
644 if ( version[2] == 0x03 && version[6] == 0x0B )
645 tagtype = UL_EV1_48;
646 else if ( version[2] == 0x03 && version[6] != 0x0B )
647 tagtype = UL_EV1_128;
648 else if ( version[2] == 0x04 && version[3] == 0x01 && version[6] == 0x0B )
649 tagtype = NTAG_210;
650 else if ( version[2] == 0x04 && version[3] == 0x01 && version[6] == 0x0E )
651 tagtype = NTAG_212;
652 else if ( version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x0F )
653 tagtype = NTAG_213;
654 else if ( version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x11 )
655 tagtype = NTAG_215;
656 else if ( version[2] == 0x04 && version[3] == 0x02 && version[6] == 0x13 )
657 tagtype = NTAG_216;
658 else if ( version[2] == 0x04 && version[3] == 0x05 && version[6] == 0x13 )
659 tagtype = NTAG_I2C_1K;
660 else if ( version[2] == 0x04 && version[3] == 0x05 && version[6] == 0x15 )
661 tagtype = NTAG_I2C_2K;
662 else if ( version[2] == 0x04 )
663 tagtype = NTAG;
664
665 break;
666 }
667 case 0x01: tagtype = UL_C; break;
668 case 0x00: tagtype = UL; break;
669 case -1 : tagtype = (UL | UL_C | NTAG_203); break; // could be UL | UL_C magic tags
670 default : tagtype = UNKNOWN; break;
671 }
672 // UL vs UL-C vs ntag203 test
673 if (tagtype & (UL | UL_C | NTAG_203)) {
674 if ( !ul_select(&card) ) return UL_ERROR;
675
676 // do UL_C check first...
677 uint8_t nonce[11] = {0x00};
678 status = ulc_requestAuthentication(nonce, sizeof(nonce));
679 DropField();
680 if (status > 1) {
681 tagtype = UL_C;
682 } else {
683 // need to re-select after authentication error
684 if ( !ul_select(&card) ) return UL_ERROR;
685
686 uint8_t data[16] = {0x00};
687 // read page 0x26-0x29 (last valid ntag203 page)
688 status = ul_read(0x26, data, sizeof(data));
689 if ( status <= 1 ) {
690 tagtype = UL;
691 } else {
692 // read page 0x30 (should error if it is a ntag203)
693 status = ul_read(0x30, data, sizeof(data));
694 if ( status <= 1 ){
695 tagtype = NTAG_203;
696 } else {
697 tagtype = UNKNOWN;
698 }
699 }
700 DropField();
701 }
702 }
703 if (tagtype & UL) {
704 tagtype = ul_fudan_check();
705 DropField();
706 }
707 } else {
708 DropField();
709 // Infinition MY-D tests Exam high nibble
710 uint8_t nib = (card.uid[1] & 0xf0) >> 4;
711 switch ( nib ){
712 // case 0: tagtype = SLE66R35E7; break; //or SLE 66R35E7 - mifare compat... should have different sak/atqa for mf 1k
713 case 1: tagtype = MY_D; break; //or SLE 66RxxS ... up to 512 pages of 8 user bytes...
714 case 2: tagtype = (MY_D_NFC); break; //or SLE 66RxxP ... up to 512 pages of 8 user bytes... (or in nfc mode FF pages of 4 bytes)
715 case 3: tagtype = (MY_D_MOVE | MY_D_MOVE_NFC); break; //or SLE 66R01P // 38 pages of 4 bytes //notice: we can not currently distinguish between these two
716 case 7: tagtype = MY_D_MOVE_LEAN; break; //or SLE 66R01L // 16 pages of 4 bytes
717 }
718 }
719
720 tagtype |= ul_magic_test();
721 if (tagtype == (UNKNOWN | MAGIC)) tagtype = (UL_MAGIC);
722 return tagtype;
723 }
724
725 static int usage_hf_mfu_info(void) {
726 PrintAndLogEx(NORMAL, "It gathers information about the tag and tries to detect what kind it is.");
727 PrintAndLogEx(NORMAL, "Sometimes the tags are locked down, and you may need a key to be able to read the information");
728 PrintAndLogEx(NORMAL, "The following tags can be identified:\n");
729 PrintAndLogEx(NORMAL, "Ultralight, Ultralight-C, Ultralight EV1, NTAG 203, NTAG 210,");
730 PrintAndLogEx(NORMAL, "NTAG 212, NTAG 213, NTAG 215, NTAG 216, NTAG I2C 1K & 2K");
731 PrintAndLogEx(NORMAL, "my-d, my-d NFC, my-d move, my-d move NFC\n");
732 PrintAndLogEx(NORMAL, "Usage: hf mfu info k <key> l");
733 PrintAndLogEx(NORMAL, " Options : ");
734 PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
735 PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness");
736 PrintAndLogEx(NORMAL, "");
737 PrintAndLogEx(NORMAL, " sample : hf mfu info");
738 PrintAndLogEx(NORMAL, " : hf mfu info k 00112233445566778899AABBCCDDEEFF");
739 PrintAndLogEx(NORMAL, " : hf mfu info k AABBCCDDD");
740 return 0;
741 }
742
743 static int CmdHF14AMfUInfo(const char *Cmd){
744
745 uint8_t authlim = 0xff;
746 uint8_t data[16] = {0x00};
747 iso14a_card_select_t card;
748 int status;
749 bool errors = false;
750 bool hasAuthKey = false;
751 bool locked = false;
752 bool swapEndian = false;
753 uint8_t cmdp = 0;
754 uint8_t dataLen = 0;
755 uint8_t authenticationkey[16] = {0x00};
756 uint8_t *authkeyptr = authenticationkey;
757 uint8_t *key;
758 uint8_t pack[4] = {0,0,0,0};
759 int len = 0;
760 char tempStr[50];
761
762 while(param_getchar(Cmd, cmdp) != 0x00)
763 {
764 switch(param_getchar(Cmd, cmdp))
765 {
766 case 'h':
767 case 'H':
768 return usage_hf_mfu_info();
769 case 'k':
770 case 'K':
771 dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
772 if (dataLen == 32 || dataLen == 8) { //ul-c or ev1/ntag key length
773 errors = param_gethex(tempStr, 0, authenticationkey, dataLen);
774 dataLen /= 2; // handled as bytes from now on
775 } else {
776 PrintAndLogEx(ERR, "Key has incorrect length\n");
777 errors = true;
778 }
779 cmdp += 2;
780 hasAuthKey = true;
781 break;
782 case 'l':
783 case 'L':
784 swapEndian = true;
785 cmdp++;
786 break;
787 default:
788 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
789 errors = true;
790 break;
791 }
792 if(errors) break;
793 }
794
795 //Validations
796 if(errors) return usage_hf_mfu_info();
797
798 TagTypeUL_t tagtype = GetHF14AMfU_Type();
799 if (tagtype == UL_ERROR) return -1;
800
801 PrintAndLogEx(NORMAL, "\n--- Tag Information ---------");
802 PrintAndLogEx(NORMAL, "-------------------------------------------------------------");
803 ul_print_type(tagtype, 6);
804
805 // Swap endianness
806 if (swapEndian && hasAuthKey) authkeyptr = SwapEndian64(authenticationkey, dataLen, (dataLen == 16) ? 8 : 4 );
807
808 if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
809
810 // read pages 0,1,2,3 (should read 4pages)
811 status = ul_read(0, data, sizeof(data));
812 if ( status == -1 ) {
813 DropField();
814 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ");
815 return status;
816 } else if (status == 16) {
817 ul_print_default(data);
818 ndef_print_CC(data+12);
819 } else {
820 locked = true;
821 }
822
823 // UL_C Specific
824 if ((tagtype & UL_C)) {
825
826 // read pages 0x28, 0x29, 0x2A, 0x2B
827 uint8_t ulc_conf[16] = {0x00};
828 status = ul_read(0x28, ulc_conf, sizeof(ulc_conf));
829 if ( status == -1 ){
830 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ UL-C");
831 DropField();
832 return status;
833 }
834 if (status == 16) ulc_print_configuration(ulc_conf);
835 else locked = true;
836
837 if ((tagtype & MAGIC)) {
838 //just read key
839 uint8_t ulc_deskey[16] = {0x00};
840 status = ul_read(0x2C, ulc_deskey, sizeof(ulc_deskey));
841 if ( status == -1 ) {
842 DropField();
843 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ magic");
844 return status;
845 }
846 if (status == 16) ulc_print_3deskey(ulc_deskey);
847
848 } else {
849 DropField();
850 // if we called info with key, just return
851 if ( hasAuthKey ) return 1;
852
853 // also try to diversify default keys.. look into CmdHF14AMfuGenDiverseKeys
854 PrintAndLogEx(INFO, "Trying some default 3des keys");
855 for (uint8_t i = 0; i < KEYS_3DES_COUNT; ++i ) {
856 key = default_3des_keys[i];
857 if (ulc_authentication(key, true)) {
858 PrintAndLogEx(SUCCESS, "Found default 3des key: ");
859 uint8_t keySwap[16];
860 memcpy(keySwap, SwapEndian64(key,16,8), 16);
861 ulc_print_3deskey(keySwap);
862 return 1;
863 }
864 }
865 return 1;
866 }
867 }
868
869 // do counters and signature first (don't neet auth)
870
871 // ul counters are different than ntag counters
872 if ((tagtype & (UL_EV1_48 | UL_EV1_128))) {
873 if (ulev1_print_counters() != 3) {
874 // failed - re-select
875 if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
876 }
877 }
878
879 if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_213 | NTAG_215 | NTAG_216 | NTAG_I2C_1K | NTAG_I2C_2K ))) {
880 uint8_t ulev1_signature[32] = {0x00};
881 status = ulev1_readSignature( ulev1_signature, sizeof(ulev1_signature));
882 if ( status == -1 ) {
883 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ SIGNATURE");
884 DropField();
885 return status;
886 }
887 if (status == 32) ulev1_print_signature( ulev1_signature, sizeof(ulev1_signature));
888 else {
889 // re-select
890 if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
891 }
892 }
893
894 if ((tagtype & (UL_EV1_48 | UL_EV1_128 | NTAG_210 | NTAG_212 | NTAG_213 | NTAG_215 | NTAG_216 | NTAG_I2C_1K | NTAG_I2C_2K))) {
895 uint8_t version[10] = {0x00};
896 status = ulev1_getVersion(version, sizeof(version));
897 if ( status == -1 ) {
898 PrintAndLogEx(WARNING, "Error: tag didn't answer to GETVERSION");
899 DropField();
900 return status;
901 } else if (status == 10) {
902 ulev1_print_version(version);
903 } else {
904 locked = true;
905 if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
906 }
907
908 uint8_t startconfigblock = 0;
909 uint8_t ulev1_conf[16] = {0x00};
910 // config blocks always are last 4 pages
911 for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++)
912 if (tagtype & UL_TYPES_ARRAY[idx])
913 startconfigblock = UL_MEMORY_ARRAY[idx]-3;
914
915 if (startconfigblock){ // if we know where the config block is...
916 status = ul_read(startconfigblock, ulev1_conf, sizeof(ulev1_conf));
917 if ( status == -1 ) {
918 PrintAndLogEx(WARNING, "Error: tag didn't answer to READ EV1");
919 DropField();
920 return status;
921 } else if (status == 16) {
922 // save AUTHENTICATION LIMITS for later:
923 authlim = (ulev1_conf[4] & 0x07);
924 ulev1_print_configuration(ulev1_conf, startconfigblock);
925 }
926 }
927
928 // AUTHLIMIT, (number of failed authentications)
929 // 0 = limitless.
930 // 1-7 = limit. No automatic tries then.
931 // hasAuthKey, if we was called with key, skip test.
932 if ( !authlim && !hasAuthKey ) {
933 PrintAndLogEx(NORMAL, "\n--- Known EV1/NTAG passwords.");
934 len = 0;
935 for (uint8_t i = 0; i < KEYS_PWD_COUNT; ++i ) {
936 key = default_pwd_pack[i];
937 len = ulev1_requestAuthentication(key, pack, sizeof(pack));
938 if (len >= 1) {
939 PrintAndLogEx(SUCCESS, "Found a default password: %s || Pack: %02X %02X",sprint_hex(key, 4), pack[0], pack[1]);
940 break;
941 } else {
942 if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
943 }
944 }
945 if (len < 1) PrintAndLogEx(WARNING, "password not known");
946 }
947 }
948
949 DropField();
950 if (locked) PrintAndLogEx(FAILED, "\nTag appears to be locked, try using the key to get more info");
951 PrintAndLogEx(NORMAL, "");
952 return 1;
953 }
954
955 //
956 // Write Single Block
957 //
958 static int usage_hf_mfu_wrbl(void) {
959 PrintAndLogEx(NORMAL, "Write a block. It autodetects card type.\n");
960 PrintAndLogEx(NORMAL, "Usage: hf mfu wrbl b <block number> d <data> k <key> l\n");
961 PrintAndLogEx(NORMAL, " Options:");
962 PrintAndLogEx(NORMAL, " b <no> : block to write");
963 PrintAndLogEx(NORMAL, " d <data> : block data - (8 hex symbols)");
964 PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
965 PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness");
966 PrintAndLogEx(NORMAL, "");
967 PrintAndLogEx(NORMAL, " sample : hf mfu wrbl b 0 d 01234567");
968 PrintAndLogEx(NORMAL, " : hf mfu wrbl b 0 d 01234567 k AABBCCDDD\n");
969 return 0;
970 }
971
972 static int CmdHF14AMfUWrBl(const char *Cmd){
973
974 int blockNo = -1;
975 bool errors = false;
976 bool hasAuthKey = false;
977 bool hasPwdKey = false;
978 bool swapEndian = false;
979
980 uint8_t cmdp = 0;
981 uint8_t keylen = 0;
982 uint8_t blockdata[20] = {0x00};
983 uint8_t data[16] = {0x00};
984 uint8_t authenticationkey[16] = {0x00};
985 uint8_t *authKeyPtr = authenticationkey;
986
987 while(param_getchar(Cmd, cmdp) != 0x00)
988 {
989 switch(param_getchar(Cmd, cmdp))
990 {
991 case 'h':
992 case 'H':
993 return usage_hf_mfu_wrbl();
994 case 'k':
995 case 'K':
996 // EV1/NTAG size key
997 keylen = param_gethex(Cmd, cmdp+1, data, 8);
998 if ( !keylen ) {
999 memcpy(authenticationkey, data, 4);
1000 cmdp += 2;
1001 hasPwdKey = true;
1002 break;
1003 }
1004 // UL-C size key
1005 keylen = param_gethex(Cmd, cmdp+1, data, 32);
1006 if (!keylen){
1007 memcpy(authenticationkey, data, 16);
1008 cmdp += 2;
1009 hasAuthKey = true;
1010 break;
1011 }
1012 PrintAndLogEx(ERR, "Key has incorrect length\n");
1013 errors = true;
1014 break;
1015 case 'b':
1016 case 'B':
1017 blockNo = param_get8(Cmd, cmdp+1);
1018 if (blockNo < 0) {
1019 PrintAndLogEx(ERR, "Wrong block number");
1020 errors = true;
1021 }
1022 cmdp += 2;
1023 break;
1024 case 'l':
1025 case 'L':
1026 swapEndian = true;
1027 cmdp++;
1028 break;
1029 case 'd':
1030 case 'D':
1031 if ( param_gethex(Cmd, cmdp+1, blockdata, 8) ) {
1032 PrintAndLogEx(ERR, "Block data must include 8 HEX symbols");
1033 errors = true;
1034 break;
1035 }
1036 cmdp += 2;
1037 break;
1038 default:
1039 PrintAndLogEx(ERR, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1040 errors = true;
1041 break;
1042 }
1043 //Validations
1044 if(errors) return usage_hf_mfu_wrbl();
1045 }
1046
1047 if ( blockNo == -1 ) return usage_hf_mfu_wrbl();
1048 // starting with getting tagtype
1049 TagTypeUL_t tagtype = GetHF14AMfU_Type();
1050 if (tagtype == UL_ERROR) return -1;
1051
1052 uint8_t maxblockno = 0;
1053 for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++){
1054 if (tagtype & UL_TYPES_ARRAY[idx])
1055 maxblockno = UL_MEMORY_ARRAY[idx];
1056 }
1057 if (blockNo > maxblockno){
1058 PrintAndLogEx(WARNING, "block number too large. Max block is %u/0x%02X \n", maxblockno,maxblockno);
1059 return usage_hf_mfu_wrbl();
1060 }
1061
1062 // Swap endianness
1063 if (swapEndian && hasAuthKey) authKeyPtr = SwapEndian64(authenticationkey, 16, 8);
1064 if (swapEndian && hasPwdKey) authKeyPtr = SwapEndian64(authenticationkey, 4, 4);
1065
1066 if ( blockNo <= 3)
1067 PrintAndLogEx(NORMAL, "Special Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(blockdata, 4));
1068 else
1069 PrintAndLogEx(NORMAL, "Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(blockdata, 4));
1070
1071 //Send write Block
1072 UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}};
1073 memcpy(c.d.asBytes,blockdata,4);
1074
1075 if ( hasAuthKey ) {
1076 c.arg[1] = 1;
1077 memcpy(c.d.asBytes+4,authKeyPtr,16);
1078 }
1079 else if ( hasPwdKey ) {
1080 c.arg[1] = 2;
1081 memcpy(c.d.asBytes+4,authKeyPtr,4);
1082 }
1083
1084 clearCommandBuffer();
1085 SendCommand(&c);
1086 UsbCommand resp;
1087 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1088 uint8_t isOK = resp.arg[0] & 0xff;
1089 PrintAndLogEx(SUCCESS, "isOk:%02x", isOK);
1090 } else {
1091 PrintAndLogEx(ERR, "Command execute timeout");
1092 }
1093
1094 return 0;
1095 }
1096
1097
1098 //
1099 // Read Single Block
1100 //
1101 static int usage_hf_mfu_rdbl(void) {
1102 PrintAndLogEx(NORMAL, "Read a block and print. It autodetects card type.\n");
1103 PrintAndLogEx(NORMAL, "Usage: hf mfu rdbl b <block number> k <key> l\n");
1104 PrintAndLogEx(NORMAL, " Options:");
1105 PrintAndLogEx(NORMAL, " b <no> : block to read");
1106 PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
1107 PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness");
1108 PrintAndLogEx(NORMAL, "");
1109 PrintAndLogEx(NORMAL, " sample : hf mfu rdbl b 0");
1110 PrintAndLogEx(NORMAL, " : hf mfu rdbl b 0 k 00112233445566778899AABBCCDDEEFF");
1111 PrintAndLogEx(NORMAL, " : hf mfu rdbl b 0 k AABBCCDDD\n");
1112 return 0;
1113 }
1114
1115 static int CmdHF14AMfURdBl(const char *Cmd){
1116
1117 int blockNo = -1;
1118 bool errors = false;
1119 bool hasAuthKey = false;
1120 bool hasPwdKey = false;
1121 bool swapEndian = false;
1122 uint8_t cmdp = 0;
1123 uint8_t keylen = 0;
1124 uint8_t data[16] = {0x00};
1125 uint8_t authenticationkey[16] = {0x00};
1126 uint8_t *authKeyPtr = authenticationkey;
1127
1128 while(param_getchar(Cmd, cmdp) != 0x00)
1129 {
1130 switch(param_getchar(Cmd, cmdp))
1131 {
1132 case 'h':
1133 case 'H':
1134 return usage_hf_mfu_rdbl();
1135 case 'k':
1136 case 'K':
1137 // EV1/NTAG size key
1138 keylen = param_gethex(Cmd, cmdp+1, data, 8);
1139 if ( !keylen ) {
1140 memcpy(authenticationkey, data, 4);
1141 cmdp += 2;
1142 hasPwdKey = true;
1143 break;
1144 }
1145 // UL-C size key
1146 keylen = param_gethex(Cmd, cmdp+1, data, 32);
1147 if (!keylen){
1148 memcpy(authenticationkey, data, 16);
1149 cmdp += 2;
1150 hasAuthKey = true;
1151 break;
1152 }
1153 PrintAndLogEx(ERR, "Key has incorrect length\n");
1154 errors = true;
1155 break;
1156 case 'b':
1157 case 'B':
1158 blockNo = param_get8(Cmd, cmdp+1);
1159 if (blockNo < 0) {
1160 PrintAndLogEx(ERR, "Wrong block number");
1161 errors = true;
1162 }
1163 cmdp += 2;
1164 break;
1165 case 'l':
1166 case 'L':
1167 swapEndian = true;
1168 cmdp++;
1169 break;
1170 default:
1171 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1172 errors = true;
1173 break;
1174 }
1175 //Validations
1176 if(errors) return usage_hf_mfu_rdbl();
1177 }
1178
1179 if ( blockNo == -1 ) return usage_hf_mfu_rdbl();
1180 // start with getting tagtype
1181 TagTypeUL_t tagtype = GetHF14AMfU_Type();
1182 if (tagtype == UL_ERROR) return -1;
1183
1184 uint8_t maxblockno = 0;
1185 for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++){
1186 if (tagtype & UL_TYPES_ARRAY[idx])
1187 maxblockno = UL_MEMORY_ARRAY[idx];
1188 }
1189 if (blockNo > maxblockno){
1190 PrintAndLogEx(WARNING, "block number to large. Max block is %u/0x%02X \n", maxblockno,maxblockno);
1191 return usage_hf_mfu_rdbl();
1192 }
1193
1194 // Swap endianness
1195 if (swapEndian && hasAuthKey) authKeyPtr = SwapEndian64(authenticationkey, 16, 8);
1196 if (swapEndian && hasPwdKey) authKeyPtr = SwapEndian64(authenticationkey, 4, 4);
1197
1198 //Read Block
1199 UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
1200 if ( hasAuthKey ){
1201 c.arg[1] = 1;
1202 memcpy(c.d.asBytes,authKeyPtr,16);
1203 }
1204 else if ( hasPwdKey ) {
1205 c.arg[1] = 2;
1206 memcpy(c.d.asBytes,authKeyPtr,4);
1207 }
1208
1209 clearCommandBuffer();
1210 SendCommand(&c);
1211 UsbCommand resp;
1212 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1213 uint8_t isOK = resp.arg[0] & 0xff;
1214 if (isOK) {
1215 uint8_t *data = resp.d.asBytes;
1216 PrintAndLogEx(NORMAL, "\n Block# | Data | Ascii");
1217 PrintAndLogEx(NORMAL, "---------+-------------+------");
1218 PrintAndLogEx(NORMAL, " %02d/0x%02X | %s| %s\n", blockNo, blockNo, sprint_hex(data, 4), sprint_ascii(data, 4));
1219 } else {
1220 PrintAndLogEx(ERR, "Failed reading block: (%02x)", isOK);
1221 }
1222 } else {
1223 PrintAndLogEx(ERR, "Command execute time-out");
1224 }
1225 return 0;
1226 }
1227
1228
1229 //
1230 // Mifare Ultralight / Ultralight-C / Ultralight-EV1
1231 // Read and Dump Card Contents, using auto detection of tag size.
1232 static int usage_hf_mfu_dump(void) {
1233 PrintAndLogEx(NORMAL, "Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
1234 PrintAndLogEx(NORMAL, "NTAG 203, NTAG 210, NTAG 212, NTAG 213, NTAG 215, NTAG 216");
1235 PrintAndLogEx(NORMAL, "and saves binary dump into the file `filename.bin` or `cardUID.bin`");
1236 PrintAndLogEx(NORMAL, "It autodetects card type.\n");
1237 PrintAndLogEx(NORMAL, "Usage: hf mfu dump k <key> l n <filename w/o .bin>");
1238 PrintAndLogEx(NORMAL, " Options : ");
1239 PrintAndLogEx(NORMAL, " k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
1240 PrintAndLogEx(NORMAL, " l : (optional) swap entered key's endianness");
1241 PrintAndLogEx(NORMAL, " n <FN > : filename w/o .bin to save the dump as");
1242 PrintAndLogEx(NORMAL, " p <Pg > : starting Page number to manually set a page to start the dump at");
1243 PrintAndLogEx(NORMAL, " q <qty> : number of Pages to manually set how many pages to dump");
1244
1245 PrintAndLogEx(NORMAL, "");
1246 PrintAndLogEx(NORMAL, " sample : hf mfu dump");
1247 PrintAndLogEx(NORMAL, " : hf mfu dump n myfile");
1248 PrintAndLogEx(NORMAL, " : hf mfu dump k 00112233445566778899AABBCCDDEEFF");
1249 PrintAndLogEx(NORMAL, " : hf mfu dump k AABBCCDDD\n");
1250 return 0;
1251 }
1252
1253 static int CmdHF14AMfUDump(const char *Cmd){
1254
1255 FILE *fout;
1256 char filename[FILE_PATH_SIZE] = {0x00};
1257 char *fnameptr = filename;
1258 uint8_t *lockbytes_t = NULL;
1259 uint8_t lockbytes[2] = {0x00};
1260 uint8_t *lockbytes_t2 = NULL;
1261 uint8_t lockbytes2[2] = {0x00};
1262 bool bit[16] = {0x00};
1263 bool bit2[16] = {0x00};
1264 uint8_t data[1024] = {0x00};
1265 bool hasAuthKey = false;
1266 int i = 0;
1267 int Pages = 16;
1268 bool tmplockbit = false;
1269 uint8_t dataLen = 0;
1270 uint8_t cmdp = 0;
1271 uint8_t authenticationkey[16] = {0x00};
1272 uint8_t *authKeyPtr = authenticationkey;
1273 size_t fileNlen = 0;
1274 bool errors = false;
1275 bool swapEndian = false;
1276 bool manualPages = false;
1277 uint8_t startPage = 0;
1278 char tempStr[50];
1279 unsigned char cleanASCII[4];
1280
1281 while(param_getchar(Cmd, cmdp) != 0x00)
1282 {
1283 switch(param_getchar(Cmd, cmdp))
1284 {
1285 case 'h':
1286 case 'H':
1287 return usage_hf_mfu_dump();
1288 case 'k':
1289 case 'K':
1290 dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
1291 if (dataLen == 32 || dataLen == 8) { //ul-c or ev1/ntag key length
1292 errors = param_gethex(tempStr, 0, authenticationkey, dataLen);
1293 dataLen /= 2;
1294 } else {
1295 PrintAndLogEx(ERR, "Key has incorrect length\n");
1296 errors = true;
1297 }
1298 cmdp += 2;
1299 hasAuthKey = true;
1300 break;
1301 case 'l':
1302 case 'L':
1303 swapEndian = true;
1304 cmdp++;
1305 break;
1306 case 'n':
1307 case 'N':
1308 fileNlen = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));
1309 if (!fileNlen) errors = true;
1310 if (fileNlen > FILE_PATH_SIZE-5) fileNlen = FILE_PATH_SIZE-5;
1311 cmdp += 2;
1312 break;
1313 case 'p':
1314 case 'P':
1315 startPage = param_get8(Cmd, cmdp+1);
1316 manualPages = true;
1317 cmdp += 2;
1318 break;
1319 case 'q':
1320 case 'Q':
1321 Pages = param_get8(Cmd, cmdp+1);
1322 cmdp += 2;
1323 manualPages = true;
1324 break;
1325 default:
1326 PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
1327 errors = true;
1328 break;
1329 }
1330 if(errors) break;
1331 }
1332
1333 //Validations
1334 if(errors) return usage_hf_mfu_dump();
1335
1336 if (swapEndian && hasAuthKey)
1337 authKeyPtr = SwapEndian64(authenticationkey, dataLen, (dataLen == 16) ? 8 : 4);
1338
1339 TagTypeUL_t tagtype = GetHF14AMfU_Type();
1340 if (tagtype == UL_ERROR) return -1;
1341
1342 if (!manualPages) //get number of pages to read
1343 for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++)
1344 if (tagtype & UL_TYPES_ARRAY[idx])
1345 Pages = UL_MEMORY_ARRAY[idx]+1; //add one as maxblks starts at 0
1346
1347 ul_print_type(tagtype, 0);
1348 PrintAndLogEx(NORMAL, "Reading tag memory...");
1349 UsbCommand c = {CMD_MIFAREU_READCARD, {startPage,Pages}};
1350 if ( hasAuthKey ) {
1351 if (tagtype & UL_C)
1352 c.arg[2] = 1; //UL_C auth
1353 else
1354 c.arg[2] = 2; //UL_EV1/NTAG auth
1355
1356 memcpy(c.d.asBytes, authKeyPtr, dataLen);
1357 }
1358
1359 clearCommandBuffer();
1360 SendCommand(&c);
1361 UsbCommand resp;
1362 if (!WaitForResponseTimeout(CMD_ACK, &resp,1500)) {
1363 PrintAndLogEx(ERR, "Command execute time-out");
1364 return 1;
1365 }
1366 if (resp.arg[0] != 1) {
1367 PrintAndLogEx(ERR, "Failed reading block: (%02x)", i);
1368 return 1;
1369 }
1370
1371 uint32_t startindex = resp.arg[2];
1372 uint32_t bufferSize = resp.arg[1];
1373 if (bufferSize > sizeof(data)) {
1374 PrintAndLogEx(FAILED, "Data exceeded Buffer size!");
1375 bufferSize = sizeof(data);
1376 }
1377 GetFromBigBuf(data, bufferSize, startindex, NULL, -1, false);
1378
1379 Pages = bufferSize/4;
1380 // Load lock bytes.
1381 int j = 0;
1382
1383 lockbytes_t = data + 8;
1384 lockbytes[0] = lockbytes_t[2];
1385 lockbytes[1] = lockbytes_t[3];
1386 for(j = 0; j < 16; j++){
1387 bit[j] = lockbytes[j/8] & ( 1 <<(7-j%8));
1388 }
1389
1390 // Load bottom lockbytes if available
1391 // TODO -- FIGURE OUT LOCK BYTES FOR TO EV1 and/or NTAG
1392 if ( Pages == 44 ) {
1393 lockbytes_t2 = data + (40*4);
1394 lockbytes2[0] = lockbytes_t2[2];
1395 lockbytes2[1] = lockbytes_t2[3];
1396 for (j = 0; j < 16; j++) {
1397 bit2[j] = lockbytes2[j/8] & ( 1 <<(7-j%8));
1398 }
1399 }
1400
1401 // add keys to block dump
1402 if (hasAuthKey) {
1403 if (!swapEndian){
1404 authKeyPtr = SwapEndian64(authenticationkey, dataLen, (dataLen == 16) ? 8 : 4);
1405 } else {
1406 authKeyPtr = authenticationkey;
1407 }
1408
1409 if (tagtype & UL_C){ //add 4 pages
1410 memcpy(data + Pages*4, authKeyPtr, dataLen);
1411 Pages += dataLen/4;
1412 } else { // 2nd page from end
1413 memcpy(data + (Pages*4) - 8, authenticationkey, dataLen);
1414 }
1415 }
1416
1417 PrintAndLogEx(NORMAL, "\n Block# | Data |lck| Ascii");
1418 PrintAndLogEx(NORMAL, "---------+-------------+---+------");
1419 for (i = 0; i < Pages; ++i) {
1420 if ( i < 3 ) {
1421 PrintAndLogEx(NORMAL, "%3d/0x%02X | %s| | ", i+startPage, i+startPage, sprint_hex(data + i * 4, 4));
1422 continue;
1423 }
1424 switch(i){
1425 case 3: tmplockbit = bit[4]; break;
1426 case 4: tmplockbit = bit[3]; break;
1427 case 5: tmplockbit = bit[2]; break;
1428 case 6: tmplockbit = bit[1]; break;
1429 case 7: tmplockbit = bit[0]; break;
1430 case 8: tmplockbit = bit[15]; break;
1431 case 9: tmplockbit = bit[14]; break;
1432 case 10: tmplockbit = bit[13]; break;
1433 case 11: tmplockbit = bit[12]; break;
1434 case 12: tmplockbit = bit[11]; break;
1435 case 13: tmplockbit = bit[10]; break;
1436 case 14: tmplockbit = bit[9]; break;
1437 case 15: tmplockbit = bit[8]; break;
1438 case 16:
1439 case 17:
1440 case 18:
1441 case 19: tmplockbit = bit2[6]; break;
1442 case 20:
1443 case 21:
1444 case 22:
1445 case 23: tmplockbit = bit2[5]; break;
1446 case 24:
1447 case 25:
1448 case 26:
1449 case 27: tmplockbit = bit2[4]; break;
1450 case 28:
1451 case 29:
1452 case 30:
1453 case 31: tmplockbit = bit2[2]; break;
1454 case 32:
1455 case 33:
1456 case 34:
1457 case 35: tmplockbit = bit2[1]; break;
1458 case 36:
1459 case 37:
1460 case 38:
1461 case 39: tmplockbit = bit2[0]; break;
1462 case 40: tmplockbit = bit2[12]; break;
1463 case 41: tmplockbit = bit2[11]; break;
1464 case 42: tmplockbit = bit2[10]; break; //auth0
1465 case 43: tmplockbit = bit2[9]; break; //auth1
1466 default: break;
1467 }
1468
1469 // convert unprintable characters and line breaks to dots
1470 memcpy(cleanASCII, data+i*4, 4);
1471 clean_ascii(cleanASCII, 4);
1472
1473 PrintAndLogEx(NORMAL, "%3d/0x%02X | %s| %d | %.4s", i+startPage, i+startPage, sprint_hex(data + i * 4, 4), tmplockbit, cleanASCII);
1474 }
1475 PrintAndLogEx(NORMAL, "---------------------------------");
1476
1477 // user supplied filename?
1478 if (fileNlen < 1) {
1479 // UID = data 0-1-2 4-5-6-7 (skips a beat)
1480 sprintf(fnameptr,"%02X%02X%02X%02X%02X%02X%02X.bin",
1481 data[0],data[1], data[2], data[4],data[5],data[6], data[7]);
1482 } else {
1483 sprintf(fnameptr + fileNlen,".bin");
1484 }
1485
1486 if ((fout = fopen(filename,"wb")) == NULL) {
1487 PrintAndLogEx(NORMAL, "Could not create file name %s", filename);
1488 return 1;
1489 }
1490 fwrite( data, 1, Pages*4, fout );
1491 fclose(fout);
1492
1493 PrintAndLogEx(SUCCESS, "Dumped %d pages, wrote %d bytes to %s", Pages, Pages*4, filename);
1494 return 0;
1495 }
1496
1497 //-------------------------------------------------------------------------------
1498 // Ultralight C Methods
1499 //-------------------------------------------------------------------------------
1500
1501 //
1502 // Ultralight C Authentication Demo {currently uses hard-coded key}
1503 //
1504 static int CmdHF14AMfucAuth(const char *Cmd){
1505
1506 uint8_t keyNo = 3;
1507 bool errors = false;
1508
1509 char cmdp = param_getchar(Cmd, 0);
1510
1511 //Change key to user defined one
1512 if (cmdp == 'k' || cmdp == 'K'){
1513 keyNo = param_get8(Cmd, 1);
1514 if(keyNo > KEYS_3DES_COUNT-1)
1515 errors = true;
1516 }
1517
1518 if (cmdp == 'h' || cmdp == 'H')
1519 errors = true;
1520
1521 if (errors) {
1522 PrintAndLogEx(NORMAL, "Usage: hf mfu cauth k <key number>");
1523 PrintAndLogEx(NORMAL, " 0 (default): 3DES standard key");
1524 PrintAndLogEx(NORMAL, " 1 : all 0x00 key");
1525 PrintAndLogEx(NORMAL, " 2 : 0x00-0x0F key");
1526 PrintAndLogEx(NORMAL, " 3 : nfc key");
1527 PrintAndLogEx(NORMAL, " 4 : all 0x01 key");
1528 PrintAndLogEx(NORMAL, " 5 : all 0xff key");
1529 PrintAndLogEx(NORMAL, " 6 : 0x00-0xFF key");
1530 PrintAndLogEx(NORMAL, "\n sample : hf mfu cauth k");
1531 PrintAndLogEx(NORMAL, " : hf mfu cauth k 3");
1532 return 0;
1533 }
1534
1535 uint8_t *key = default_3des_keys[keyNo];
1536 if (ulc_authentication(key, true))
1537 PrintAndLogEx(SUCCESS, "Authentication successful. 3des key: %s",sprint_hex(key, 16));
1538 else
1539 PrintAndLogEx(WARNING, "Authentication failed");
1540
1541 return 0;
1542 }
1543
1544 /**
1545 A test function to validate that the polarssl-function works the same
1546 was as the openssl-implementation.
1547 Commented out, since it requires openssl
1548
1549 int CmdTestDES(const char * cmd)
1550 {
1551 uint8_t key[16] = {0x00};
1552
1553 memcpy(key,key3_3des_data,16);
1554 DES_cblock RndA, RndB;
1555
1556 PrintAndLogEx(NORMAL, "----------OpenSSL DES implementation----------");
1557 {
1558 uint8_t e_RndB[8] = {0x00};
1559 unsigned char RndARndB[16] = {0x00};
1560
1561 DES_cblock iv = { 0 };
1562 DES_key_schedule ks1,ks2;
1563 DES_cblock key1,key2;
1564
1565 memcpy(key,key3_3des_data,16);
1566 memcpy(key1,key,8);
1567 memcpy(key2,key+8,8);
1568
1569
1570 DES_set_key((DES_cblock *)key1,&ks1);
1571 DES_set_key((DES_cblock *)key2,&ks2);
1572
1573 DES_random_key(&RndA);
1574 PrintAndLogEx(NORMAL, " RndA:%s",sprint_hex(RndA, 8));
1575 PrintAndLogEx(NORMAL, " e_RndB:%s",sprint_hex(e_RndB, 8));
1576 //void DES_ede2_cbc_encrypt(const unsigned char *input,
1577 // unsigned char *output, long length, DES_key_schedule *ks1,
1578 // DES_key_schedule *ks2, DES_cblock *ivec, int enc);
1579 DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0);
1580
1581 PrintAndLogEx(NORMAL, " RndB:%s",sprint_hex(RndB, 8));
1582 rol(RndB,8);
1583 memcpy(RndARndB,RndA,8);
1584 memcpy(RndARndB+8,RndB,8);
1585 PrintAndLogEx(NORMAL, " RA+B:%s",sprint_hex(RndARndB, 16));
1586 DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1);
1587 PrintAndLogEx(NORMAL, "enc(RA+B):%s",sprint_hex(RndARndB, 16));
1588
1589 }
1590 PrintAndLogEx(NORMAL, "----------PolarSSL implementation----------");
1591 {
1592 uint8_t random_a[8] = { 0 };
1593 uint8_t enc_random_a[8] = { 0 };
1594 uint8_t random_b[8] = { 0 };
1595 uint8_t enc_random_b[8] = { 0 };
1596 uint8_t random_a_and_b[16] = { 0 };
1597 des3_context ctx = { 0 };
1598
1599 memcpy(random_a, RndA,8);
1600
1601 uint8_t output[8] = { 0 };
1602 uint8_t iv[8] = { 0 };
1603
1604 PrintAndLogEx(NORMAL, " RndA :%s",sprint_hex(random_a, 8));
1605 PrintAndLogEx(NORMAL, " e_RndB:%s",sprint_hex(enc_random_b, 8));
1606
1607 des3_set2key_dec(&ctx, key);
1608
1609 des3_crypt_cbc(&ctx // des3_context *ctx
1610 , DES_DECRYPT // int mode
1611 , sizeof(random_b) // size_t length
1612 , iv // unsigned char iv[8]
1613 , enc_random_b // const unsigned char *input
1614 , random_b // unsigned char *output
1615 );
1616
1617 PrintAndLogEx(NORMAL, " RndB:%s",sprint_hex(random_b, 8));
1618
1619 rol(random_b,8);
1620 memcpy(random_a_and_b ,random_a,8);
1621 memcpy(random_a_and_b+8,random_b,8);
1622
1623 PrintAndLogEx(NORMAL, " RA+B:%s",sprint_hex(random_a_and_b, 16));
1624
1625 des3_set2key_enc(&ctx, key);
1626
1627 des3_crypt_cbc(&ctx // des3_context *ctx
1628 , DES_ENCRYPT // int mode
1629 , sizeof(random_a_and_b) // size_t length
1630 , enc_random_b // unsigned char iv[8]
1631 , random_a_and_b // const unsigned char *input
1632 , random_a_and_b // unsigned char *output
1633 );
1634
1635 PrintAndLogEx(NORMAL, "enc(RA+B):%s",sprint_hex(random_a_and_b, 16));
1636 }
1637 return 0;
1638 }
1639 **/
1640
1641 //
1642 // Mifare Ultralight C - Set password
1643 //
1644 static int CmdHF14AMfucSetPwd(const char *Cmd){
1645
1646 uint8_t pwd[16] = {0x00};
1647
1648 char cmdp = param_getchar(Cmd, 0);
1649
1650 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
1651 PrintAndLogEx(NORMAL, "Usage: hf mfu setpwd <password (32 hex symbols)>");
1652 PrintAndLogEx(NORMAL, " [password] - (32 hex symbols)");
1653 PrintAndLogEx(NORMAL, "");
1654 PrintAndLogEx(NORMAL, "sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f");
1655 PrintAndLogEx(NORMAL, "");
1656 return 0;
1657 }
1658
1659 if (param_gethex(Cmd, 0, pwd, 32)) {
1660 PrintAndLogEx(WARNING, "Password must include 32 HEX symbols");
1661 return 1;
1662 }
1663
1664 UsbCommand c = {CMD_MIFAREUC_SETPWD};
1665 memcpy( c.d.asBytes, pwd, 16);
1666 clearCommandBuffer();
1667 SendCommand(&c);
1668
1669 UsbCommand resp;
1670
1671 if (WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1672 if ( (resp.arg[0] & 0xff) == 1)
1673 PrintAndLogEx(INFO, "Ultralight-C new password: %s", sprint_hex(pwd,16));
1674 else{
1675 PrintAndLogEx(ERR, "Failed writing at block %d", resp.arg[1] & 0xff);
1676 return 1;
1677 }
1678 }
1679 else {
1680 PrintAndLogEx(ERR, "command execution time out");
1681 return 1;
1682 }
1683
1684 return 0;
1685 }
1686
1687 //
1688 // Magic UL / UL-C tags - Set UID
1689 //
1690 static int CmdHF14AMfucSetUid(const char *Cmd){
1691
1692 UsbCommand c;
1693 UsbCommand resp;
1694 uint8_t uid[7] = {0x00};
1695 char cmdp = param_getchar(Cmd, 0);
1696
1697 if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') {
1698 PrintAndLogEx(NORMAL, "Usage: hf mfu setuid <uid (14 hex symbols)>");
1699 PrintAndLogEx(NORMAL, " [uid] - (14 hex symbols)");
1700 PrintAndLogEx(NORMAL, "\nThis only works for Magic Ultralight tags.");
1701 PrintAndLogEx(NORMAL, "");
1702 PrintAndLogEx(NORMAL, "sample: hf mfu setuid 11223344556677");
1703 PrintAndLogEx(NORMAL, "");
1704 return 0;
1705 }
1706
1707 if (param_gethex(Cmd, 0, uid, 14)) {
1708 PrintAndLogEx(WARNING, "UID must include 14 HEX symbols");
1709 return 1;
1710 }
1711
1712 // read block2.
1713 c.cmd = CMD_MIFAREU_READBL;
1714 c.arg[0] = 2;
1715 clearCommandBuffer();
1716 SendCommand(&c);
1717 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1718 PrintAndLogEx(WARNING, "Command execute timeout");
1719 return 2;
1720 }
1721
1722 // save old block2.
1723 uint8_t oldblock2[4] = {0x00};
1724 memcpy(resp.d.asBytes, oldblock2, 4);
1725
1726 // block 0.
1727 c.cmd = CMD_MIFAREU_WRITEBL;
1728 c.arg[0] = 0;
1729 c.d.asBytes[0] = uid[0];
1730 c.d.asBytes[1] = uid[1];
1731 c.d.asBytes[2] = uid[2];
1732 c.d.asBytes[3] = 0x88 ^ uid[0] ^ uid[1] ^ uid[2];
1733 clearCommandBuffer();
1734 SendCommand(&c);
1735 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
1736 PrintAndLogEx(WARNING, "Command execute timeout");
1737 return 3;
1738 }
1739
1740 // block 1.
1741 c.arg[0] = 1;
1742 c.d.asBytes[0] = uid[3];
1743 c.d.asBytes[1] = uid[4];
1744 c.d.asBytes[2] = uid[5];
1745 c.d.asBytes[3] = uid[6];
1746 clearCommandBuffer();
1747 SendCommand(&c);
1748 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1749 PrintAndLogEx(WARNING, "Command execute timeout");
1750 return 4;
1751 }
1752
1753 // block 2.
1754 c.arg[0] = 2;
1755 c.d.asBytes[0] = uid[3] ^ uid[4] ^ uid[5] ^ uid[6];
1756 c.d.asBytes[1] = oldblock2[1];
1757 c.d.asBytes[2] = oldblock2[2];
1758 c.d.asBytes[3] = oldblock2[3];
1759 clearCommandBuffer();
1760 SendCommand(&c);
1761 if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
1762 PrintAndLogEx(WARNING, "Command execute timeout");
1763 return 5;
1764 }
1765
1766 return 0;
1767 }
1768
1769 static int CmdHF14AMfuGenDiverseKeys(const char *Cmd){
1770
1771 uint8_t iv[8] = { 0x00 };
1772 uint8_t block = 0x07;
1773
1774 // UL-EV1
1775 //04 57 b6 e2 05 3f 80 UID
1776 //4a f8 4b 19 PWD
1777 uint8_t uid[] = { 0xF4,0xEA, 0x54, 0x8E };
1778 uint8_t mifarekeyA[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 };
1779 uint8_t mifarekeyB[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 };
1780 uint8_t dkeyA[8] = { 0x00 };
1781 uint8_t dkeyB[8] = { 0x00 };
1782
1783 uint8_t masterkey[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff };
1784
1785 uint8_t mix[8] = { 0x00 };
1786 uint8_t divkey[8] = { 0x00 };
1787
1788 memcpy(mix, mifarekeyA, 4);
1789
1790 mix[4] = mifarekeyA[4] ^ uid[0];
1791 mix[5] = mifarekeyA[5] ^ uid[1];
1792 mix[6] = block ^ uid[2];
1793 mix[7] = uid[3];
1794
1795 mbedtls_des3_context ctx = { {0} };
1796 mbedtls_des3_set2key_enc(&ctx, masterkey);
1797
1798 mbedtls_des3_crypt_cbc(&ctx // des3_context
1799 , MBEDTLS_DES_ENCRYPT // int mode
1800 , sizeof(mix) // length
1801 , iv // iv[8]
1802 , mix // input
1803 , divkey // output
1804 );
1805
1806 PrintAndLogEx(NORMAL, "-- 3DES version");
1807 PrintAndLogEx(NORMAL, "Masterkey :\t %s", sprint_hex(masterkey,sizeof(masterkey)));
1808 PrintAndLogEx(NORMAL, "UID :\t %s", sprint_hex(uid, sizeof(uid)));
1809 PrintAndLogEx(NORMAL, "Block :\t %0d", block);
1810 PrintAndLogEx(NORMAL, "Mifare key :\t %s", sprint_hex(mifarekeyA, sizeof(mifarekeyA)));
1811 PrintAndLogEx(NORMAL, "Message :\t %s", sprint_hex(mix, sizeof(mix)));
1812 PrintAndLogEx(NORMAL, "Diversified key: %s", sprint_hex(divkey+1, 6));
1813
1814 for (int i=0; i < sizeof(mifarekeyA); ++i){
1815 dkeyA[i] = (mifarekeyA[i] << 1) & 0xff;
1816 dkeyA[6] |= ((mifarekeyA[i] >> 7) & 1) << (i+1);
1817 }
1818
1819 for (int i=0; i < sizeof(mifarekeyB); ++i){
1820 dkeyB[1] |= ((mifarekeyB[i] >> 7) & 1) << (i+1);
1821 dkeyB[2+i] = (mifarekeyB[i] << 1) & 0xff;
1822 }
1823
1824 uint8_t zeros[8] = {0x00};
1825 uint8_t newpwd[8] = {0x00};
1826 uint8_t dmkey[24] = {0x00};
1827 memcpy(dmkey, dkeyA, 8);
1828 memcpy(dmkey+8, dkeyB, 8);
1829 memcpy(dmkey+16, dkeyA, 8);
1830 memset(iv, 0x00, 8);
1831
1832 mbedtls_des3_set3key_enc(&ctx, dmkey);
1833
1834 mbedtls_des3_crypt_cbc(&ctx // des3_context
1835 , MBEDTLS_DES_ENCRYPT // int mode
1836 , sizeof(newpwd) // length
1837 , iv // iv[8]
1838 , zeros // input
1839 , newpwd // output
1840 );
1841
1842 PrintAndLogEx(NORMAL, "\n-- DES version");
1843 PrintAndLogEx(NORMAL, "Mifare dkeyA :\t %s", sprint_hex(dkeyA, sizeof(dkeyA)));
1844 PrintAndLogEx(NORMAL, "Mifare dkeyB :\t %s", sprint_hex(dkeyB, sizeof(dkeyB)));
1845 PrintAndLogEx(NORMAL, "Mifare ABA :\t %s", sprint_hex(dmkey, sizeof(dmkey)));
1846 PrintAndLogEx(NORMAL, "Mifare Pwd :\t %s", sprint_hex(newpwd, sizeof(newpwd)));
1847
1848 return 0;
1849 }
1850
1851 // static uint8_t * diversify_key(uint8_t * key){
1852
1853 // for(int i=0; i<16; i++){
1854 // if(i<=6) key[i]^=cuid[i];
1855 // if(i>6) key[i]^=cuid[i%7];
1856 // }
1857 // return key;
1858 // }
1859
1860 // static void GenerateUIDe( uint8_t *uid, uint8_t len){
1861 // for (int i=0; i<len; ++i){
1862
1863 // }
1864 // return;
1865 // }
1866
1867 //------------------------------------
1868 // Menu Stuff
1869 //------------------------------------
1870 static int CmdHelp(const char *Cmd);
1871
1872 static command_t CommandTable[] =
1873 {
1874 {"help", CmdHelp, 1, "This help"},
1875 {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"},
1876 {"info", CmdHF14AMfUInfo, 0, "Tag information"},
1877 {"dump", CmdHF14AMfUDump, 0, "Dump Ultralight / Ultralight-C / NTAG tag to binary file"},
1878 {"rdbl", CmdHF14AMfURdBl, 0, "Read block"},
1879 {"wrbl", CmdHF14AMfUWrBl, 0, "Write block"},
1880 {"cauth", CmdHF14AMfucAuth, 0, "Authentication - Ultralight C"},
1881 {"setpwd", CmdHF14AMfucSetPwd, 0, "Set 3des password - Ultralight-C"},
1882 {"setuid", CmdHF14AMfucSetUid, 0, "Set UID - MAGIC tags only"},
1883 {"gen", CmdHF14AMfuGenDiverseKeys, 1, "Generate 3des mifare diversified keys"},
1884 {NULL, NULL, 0, NULL}
1885 };
1886
1887 int CmdHFMFUltra(const char *Cmd){
1888 (void)WaitForResponseTimeout(CMD_ACK,NULL,100);
1889 CmdsParse(CommandTable, Cmd);
1890 return 0;
1891 }
1892
1893 static int CmdHelp(const char *Cmd){
1894 CmdsHelp(CommandTable);
1895 return 0;
1896 }
Impressum, Datenschutz