]>
Commit | Line | Data |
---|---|---|
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 | #include "loclass/des.h" | |
11 | #include "cmdhfmfu.h" | |
12 | #include "cmdhfmf.h" | |
13 | #include "cmdhf14a.h" | |
14 | #include "mifare.h" | |
15 | ||
16 | #define MAX_UL_BLOCKS 0x0f | |
17 | #define MAX_ULC_BLOCKS 0x2f | |
18 | #define MAX_ULEV1a_BLOCKS 0x0b; | |
19 | #define MAX_ULEV1b_BLOCKS 0x20; | |
20 | ||
21 | uint8_t default_3des_keys[7][16] = { | |
22 | { 0x42,0x52,0x45,0x41,0x4b,0x4d,0x45,0x49,0x46,0x59,0x4f,0x55,0x43,0x41,0x4e,0x21 },// 3des std key | |
23 | { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },// all zeroes | |
24 | { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f },// 0x00-0x0F | |
25 | { 0x49,0x45,0x4D,0x4B,0x41,0x45,0x52,0x42,0x21,0x4E,0x41,0x43,0x55,0x4F,0x59,0x46 },// NFC-key | |
26 | { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },// all ones | |
27 | { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF },// all FF | |
28 | { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF } // 11 22 33 | |
29 | }; | |
30 | ||
31 | static int CmdHelp(const char *Cmd); | |
32 | ||
33 | // return 1 if tag responded to 0x1A. | |
34 | uint8_t requestAuthentication( uint8_t* nonce){ | |
35 | ||
36 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC ,2 ,0}}; | |
37 | c.d.asBytes[0] = 0x1A; | |
38 | c.d.asBytes[1] = 0x00; | |
39 | SendCommand(&c); | |
40 | UsbCommand resp; | |
41 | WaitForResponse(CMD_ACK, &resp); // skip select answer. | |
42 | ||
43 | if ( !(resp.arg[0] & 0xff) ) | |
44 | return 0; | |
45 | ||
46 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { | |
47 | ||
48 | if ( resp.arg[0] & 0xff ) { | |
49 | memcpy(nonce, resp.d.asBytes+1, 8); | |
50 | return 1; | |
51 | } | |
52 | } | |
53 | return 0; | |
54 | } | |
55 | ||
56 | typedef enum TAGTYPE_UL { | |
57 | UNKNOWN = 0x00, | |
58 | UL = 0x01, | |
59 | UL_C = 0x02, | |
60 | UL_EV1_48 = 0x04, | |
61 | UL_EV1_128 = 0x08, | |
62 | MAGIC = 0x10, | |
63 | UL_MAGIC = UL | MAGIC, | |
64 | UL_C_MAGIC = UL_C | MAGIC, | |
65 | UL_ERROR = 0xFF, | |
66 | } TagTypeUL_t; | |
67 | ||
68 | uint8_t GetHF14AMfU_Type(){ | |
69 | ||
70 | TagTypeUL_t tagtype = UNKNOWN; | |
71 | iso14a_card_select_t card; | |
72 | ||
73 | // select and run 0x60 (GET_VERSION - EV1 command) | |
74 | UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_RAW | ISO14A_APPEND_CRC, 1, 0}}; | |
75 | c.d.asBytes[0] = 0x60; | |
76 | SendCommand(&c); | |
77 | UsbCommand resp; | |
78 | WaitForResponse(CMD_ACK, &resp); | |
79 | ||
80 | if ( resp.arg[0] == 0 ) return UL_ERROR; | |
81 | ||
82 | memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t)); | |
83 | ||
84 | // Ultralight - ATQA / SAK | |
85 | if ( card.atqa[1] != 0x00 && card.atqa[0] != 0x44 && card.sak != 0x00 ) return UL_ERROR; | |
86 | ||
87 | // EV1 GetVersion | |
88 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { | |
89 | ||
90 | uint8_t version[8] = {0,0,0,0,0,0,0,0}; | |
91 | memcpy(&version, resp.d.asBytes, sizeof(version)); | |
92 | uint8_t len = resp.arg[0] & 0xff; | |
93 | ||
94 | if ( len == 0x0A && version[6] == 0x0B ) | |
95 | tagtype = UL_EV1_48; | |
96 | else if ( len == 0x0A && version[6] != 0x0B ) | |
97 | tagtype = UL_EV1_128; | |
98 | else if ( len == 0x01 ) | |
99 | tagtype = UL_C; | |
100 | else if ( len == 0x00 ) | |
101 | tagtype = UL; | |
102 | } | |
103 | ||
104 | // Magic UL-C, mine have a static nonce response to 0x1A command. | |
105 | uint8_t nonce1[8] = {0,0,0,0,0,0,0,0}; | |
106 | uint8_t nonce2[8] = {0,0,0,0,0,0,0,0}; | |
107 | uint8_t status = requestAuthentication(nonce1); | |
108 | if ( status ) { | |
109 | requestAuthentication(nonce2); | |
110 | if ( !memcmp(nonce1, nonce2, 8) ){ | |
111 | tagtype = UL_C_MAGIC; | |
112 | } | |
113 | } else { | |
114 | //TODO: Magic Ultralight test here | |
115 | } | |
116 | return tagtype; | |
117 | } | |
118 | ||
119 | int CmdHF14AMfUInfo(const char *Cmd){ | |
120 | ||
121 | uint8_t datatemp[7] = {0x00}; | |
122 | uint8_t isOK = 0; | |
123 | uint8_t data[16] = {0x00}; | |
124 | uint8_t *key; | |
125 | ||
126 | TagTypeUL_t tagtype = GetHF14AMfU_Type(); | |
127 | if (tagtype == UL_ERROR) return -1; | |
128 | ||
129 | PrintAndLog("\n-- Tag Information ---------"); | |
130 | PrintAndLog("-------------------------------------------------------------"); | |
131 | ||
132 | if ( tagtype & UL ) | |
133 | PrintAndLog(" TYPE : MIFARE Ultralight %s", (tagtype & MAGIC)?"(magic)":""); | |
134 | else if ( tagtype & UL_C) | |
135 | PrintAndLog(" TYPE : MIFARE Ultralight C %s", (tagtype & MAGIC)?"(magic)":"" ); | |
136 | else if ( tagtype & UL_EV1_48) | |
137 | PrintAndLog(" TYPE : MIFARE Ultralight EV1 48 bytes"); | |
138 | else if ( tagtype & UL_EV1_128) | |
139 | PrintAndLog(" TYPE : MIFARE Ultralight EV1 128 bytes"); | |
140 | else | |
141 | PrintAndLog(" TYPE : Unknown %x",tagtype); | |
142 | ||
143 | // read pages 0,1,2,4 | |
144 | UsbCommand c = {CMD_MIFAREU_READCARD, {0, 4}}; | |
145 | SendCommand(&c); | |
146 | UsbCommand resp; | |
147 | ||
148 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) { | |
149 | isOK = resp.arg[0] & 0xff; | |
150 | memcpy(data, resp.d.asBytes, sizeof(data)); | |
151 | ||
152 | if (!isOK) { | |
153 | PrintAndLog("Error reading from tag"); | |
154 | return -1; | |
155 | } | |
156 | } else { | |
157 | PrintAndLog("Command execute timed out"); | |
158 | return -1; | |
159 | } | |
160 | ||
161 | // UID | |
162 | memcpy( datatemp, data, 3); | |
163 | memcpy( datatemp+3, data+4, 4); | |
164 | ||
165 | PrintAndLog(" UID : %s ", sprint_hex(datatemp, 7)); | |
166 | PrintAndLog(" UID[0] (Manufacturer Byte) = %02x, Manufacturer: %s", datatemp[0], getTagInfo(datatemp[0]) ); | |
167 | ||
168 | // BBC | |
169 | // CT (cascade tag byte) 0x88 xor SN0 xor SN1 xor SN2 | |
170 | int crc0 = 0x88 ^ data[0] ^ data[1] ^data[2]; | |
171 | if ( data[3] == crc0 ) | |
172 | PrintAndLog(" BCC0 : %02x - Ok", data[3]); | |
173 | else | |
174 | PrintAndLog(" BCC0 : %02x - crc should be %02x", data[3], crc0); | |
175 | ||
176 | int crc1 = data[4] ^ data[5] ^ data[6] ^data[7]; | |
177 | if ( data[8] == crc1 ) | |
178 | PrintAndLog(" BCC1 : %02x - Ok", data[8]); | |
179 | else | |
180 | PrintAndLog(" BCC1 : %02x - crc should be %02x", data[8], crc1 ); | |
181 | ||
182 | PrintAndLog(" Internal : %s ", sprint_hex(data + 9, 1)); | |
183 | ||
184 | memcpy(datatemp, data+10, 2); | |
185 | PrintAndLog(" Lock : %s - %s", sprint_hex(datatemp, 2),printBits( 2, &datatemp) ); | |
186 | PrintAndLog("OneTimePad : %s ", sprint_hex(data + 3*4, 4)); | |
187 | PrintAndLog(""); | |
188 | ||
189 | ||
190 | PrintAndLog("--- "); | |
191 | if ( tagtype & UL_C ) { | |
192 | ||
193 | PrintAndLog("Trying some default 3des keys"); | |
194 | ||
195 | for (uint8_t i = 0; i < 5; ++i ){ | |
196 | key = default_3des_keys[i]; | |
197 | if (try3DesAuthentication(key)){ | |
198 | PrintAndLog("Found default 3des key: %s", sprint_hex(key,16)); | |
199 | return 0; | |
200 | } | |
201 | } | |
202 | } | |
203 | else if ((tagtype & (UL_EV1_48 || UL_EV1_128))) { | |
204 | //TODO | |
205 | // --problem, there is a failed pwd tries counter in UL-EV1 | |
206 | //PrintAndLog("Trying some known EV1 passwords."); | |
207 | } | |
208 | ||
209 | // disconnect | |
210 | // c.arg[0] = 0; | |
211 | // c.arg[1] = 0; | |
212 | // c.arg[2] = 0; | |
213 | // SendCommand(&c); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | // | |
218 | // Mifare Ultralight Write Single Block | |
219 | // | |
220 | int CmdHF14AMfUWrBl(const char *Cmd){ | |
221 | uint8_t blockNo = -1; | |
222 | bool chinese_card = FALSE; | |
223 | uint8_t bldata[16] = {0x00}; | |
224 | UsbCommand resp; | |
225 | ||
226 | char cmdp = param_getchar(Cmd, 0); | |
227 | if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') { | |
228 | PrintAndLog("Usage: hf mfu wrbl <block number> <block data (8 hex symbols)> [w]"); | |
229 | PrintAndLog(" [block number]"); | |
230 | PrintAndLog(" [block data] - (8 hex symbols)"); | |
231 | PrintAndLog(" [w] - Chinese magic ultralight tag"); | |
232 | PrintAndLog(""); | |
233 | PrintAndLog(" sample: hf mfu wrbl 0 01020304"); | |
234 | PrintAndLog(""); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | blockNo = param_get8(Cmd, 0); | |
239 | ||
240 | if (blockNo > MAX_UL_BLOCKS){ | |
241 | PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!"); | |
242 | return 1; | |
243 | } | |
244 | ||
245 | if (param_gethex(Cmd, 1, bldata, 8)) { | |
246 | PrintAndLog("Block data must include 8 HEX symbols"); | |
247 | return 1; | |
248 | } | |
249 | ||
250 | if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) { | |
251 | chinese_card = TRUE; | |
252 | } | |
253 | ||
254 | if ( blockNo <= 3) { | |
255 | if (!chinese_card){ | |
256 | PrintAndLog("Access Denied"); | |
257 | } else { | |
258 | PrintAndLog("--specialblock no:%02x", blockNo); | |
259 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
260 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
261 | memcpy(d.d.asBytes,bldata, 4); | |
262 | SendCommand(&d); | |
263 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
264 | uint8_t isOK = resp.arg[0] & 0xff; | |
265 | PrintAndLog("isOk:%02x", isOK); | |
266 | } else { | |
267 | PrintAndLog("Command execute timeout"); | |
268 | } | |
269 | } | |
270 | } else { | |
271 | PrintAndLog("--block no:%02x", blockNo); | |
272 | PrintAndLog("--data: %s", sprint_hex(bldata, 4)); | |
273 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
274 | memcpy(e.d.asBytes,bldata, 4); | |
275 | SendCommand(&e); | |
276 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
277 | uint8_t isOK = resp.arg[0] & 0xff; | |
278 | PrintAndLog("isOk:%02x", isOK); | |
279 | } else { | |
280 | PrintAndLog("Command execute timeout"); | |
281 | } | |
282 | } | |
283 | return 0; | |
284 | } | |
285 | ||
286 | // | |
287 | // Mifare Ultralight Read Single Block | |
288 | // | |
289 | int CmdHF14AMfURdBl(const char *Cmd){ | |
290 | ||
291 | UsbCommand resp; | |
292 | uint8_t blockNo = -1; | |
293 | char cmdp = param_getchar(Cmd, 0); | |
294 | ||
295 | if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') { | |
296 | PrintAndLog("Usage: hf mfu rdbl <block number>"); | |
297 | PrintAndLog(" sample: hfu mfu rdbl 0"); | |
298 | return 0; | |
299 | } | |
300 | ||
301 | blockNo = param_get8(Cmd, 0); | |
302 | ||
303 | if (blockNo > MAX_UL_BLOCKS){ | |
304 | PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight"); | |
305 | return 1; | |
306 | } | |
307 | ||
308 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; | |
309 | SendCommand(&c); | |
310 | ||
311 | ||
312 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
313 | uint8_t isOK = resp.arg[0] & 0xff; | |
314 | if (isOK) { | |
315 | uint8_t *data = resp.d.asBytes; | |
316 | PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4)); | |
317 | } | |
318 | else { | |
319 | PrintAndLog("Failed reading block: (%02x)", isOK); | |
320 | } | |
321 | } else { | |
322 | PrintAndLog("Command execute time-out"); | |
323 | } | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
328 | ||
329 | int usage_hf_mfu_dump() | |
330 | { | |
331 | PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1"); | |
332 | PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`"); | |
333 | PrintAndLog("It autodetects card type.\n"); | |
334 | PrintAndLog("Usage: hf mfu dump <filename w/o .bin>"); | |
335 | PrintAndLog(" sample : hf mfu dump"); | |
336 | PrintAndLog(" : hf mfu dump myfile"); | |
337 | PrintAndLog(" : hf mfu dump 1 myfile"); | |
338 | return 0; | |
339 | } | |
340 | // | |
341 | // Mifare Ultralight / Ultralight-C / Ultralight-EV1 | |
342 | // Read and Dump Card Contents, using auto detection of tag size. | |
343 | // | |
344 | // TODO: take a password to read UL-C / UL-EV1 tags. | |
345 | int CmdHF14AMfUDump(const char *Cmd){ | |
346 | ||
347 | char cmdp = param_getchar(Cmd, 0); | |
348 | if (cmdp == 'h' || cmdp == 'H') | |
349 | return usage_hf_mfu_dump(); | |
350 | ||
351 | FILE *fout; | |
352 | char filename[FILE_PATH_SIZE] = {0x00}; | |
353 | char * fnameptr = filename; | |
354 | char* str = "Dumping Ultralight%s%s Card Data..."; | |
355 | ||
356 | uint8_t *lockbytes_t = NULL; | |
357 | uint8_t lockbytes[2] = {0x00}; | |
358 | uint8_t *lockbytes_t2 = NULL; | |
359 | uint8_t lockbytes2[2] = {0x00}; | |
360 | bool bit[16] = {0x00}; | |
361 | bool bit2[16] = {0x00}; | |
362 | uint8_t data[176] = {0x00}; | |
363 | ||
364 | int i = 0; | |
365 | int Pages = 16; | |
366 | bool tmplockbit = false; | |
367 | ||
368 | TagTypeUL_t tagtype = GetHF14AMfU_Type(); | |
369 | if (tagtype == UL_ERROR) return -1; | |
370 | ||
371 | if ( tagtype & UL ) { | |
372 | Pages = 16; | |
373 | PrintAndLog(str,"", (tagtype & MAGIC)?" (magic)":"" ); | |
374 | } | |
375 | else if ( tagtype & UL_C ) { | |
376 | Pages = 44; | |
377 | PrintAndLog(str,"-C", (tagtype & MAGIC)?" (magic)":"" ); | |
378 | } | |
379 | else if ( tagtype & UL_EV1_48 ) { | |
380 | Pages = 18; | |
381 | PrintAndLog(str," EV1_48",""); | |
382 | } | |
383 | else if ( tagtype & UL_EV1_128 ) { | |
384 | Pages = 32; | |
385 | PrintAndLog(str," EV1_128",""); | |
386 | } else { | |
387 | Pages = 16; | |
388 | PrintAndLog("Dumping unknown Ultralight, using default values."); | |
389 | } | |
390 | ||
391 | UsbCommand c = {CMD_MIFAREU_READCARD, {0,Pages}}; | |
392 | SendCommand(&c); | |
393 | UsbCommand resp; | |
394 | ||
395 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
396 | PrintAndLog("Command execute time-out"); | |
397 | return 0; | |
398 | } | |
399 | ||
400 | if (!resp.arg[0] ) { | |
401 | PrintAndLog("Read command failed"); | |
402 | return 0; | |
403 | } | |
404 | memcpy(data, resp.d.asBytes, sizeof(data)); | |
405 | ||
406 | // Load lock bytes. | |
407 | int j = 0; | |
408 | ||
409 | lockbytes_t = data + 8; | |
410 | lockbytes[0] = lockbytes_t[2]; | |
411 | lockbytes[1] = lockbytes_t[3]; | |
412 | for(j = 0; j < 16; j++){ | |
413 | bit[j] = lockbytes[j/8] & ( 1 <<(7-j%8)); | |
414 | } | |
415 | ||
416 | // Load bottom lockbytes if available | |
417 | if ( Pages == 44 ) { | |
418 | ||
419 | lockbytes_t2 = data + (40*4); | |
420 | lockbytes2[0] = lockbytes_t2[2]; | |
421 | lockbytes2[1] = lockbytes_t2[3]; | |
422 | for (j = 0; j < 16; j++) { | |
423 | bit2[j] = lockbytes2[j/8] & ( 1 <<(7-j%8)); | |
424 | } | |
425 | } | |
426 | ||
427 | for (i = 0; i < Pages; ++i) { | |
428 | ||
429 | if ( i < 3 ) { | |
430 | PrintAndLog("Block %02x:%s ", i,sprint_hex(data + i * 4, 4)); | |
431 | continue; | |
432 | } | |
433 | ||
434 | switch(i){ | |
435 | case 3: tmplockbit = bit[4]; break; | |
436 | case 4: tmplockbit = bit[3]; break; | |
437 | case 5: tmplockbit = bit[2]; break; | |
438 | case 6: tmplockbit = bit[1]; break; | |
439 | case 7: tmplockbit = bit[0]; break; | |
440 | case 8: tmplockbit = bit[15]; break; | |
441 | case 9: tmplockbit = bit[14]; break; | |
442 | case 10: tmplockbit = bit[13]; break; | |
443 | case 11: tmplockbit = bit[12]; break; | |
444 | case 12: tmplockbit = bit[11]; break; | |
445 | case 13: tmplockbit = bit[10]; break; | |
446 | case 14: tmplockbit = bit[9]; break; | |
447 | case 15: tmplockbit = bit[8]; break; | |
448 | case 16: | |
449 | case 17: | |
450 | case 18: | |
451 | case 19: tmplockbit = bit2[6]; break; | |
452 | case 20: | |
453 | case 21: | |
454 | case 22: | |
455 | case 23: tmplockbit = bit2[5]; break; | |
456 | case 24: | |
457 | case 25: | |
458 | case 26: | |
459 | case 27: tmplockbit = bit2[4]; break; | |
460 | case 28: | |
461 | case 29: | |
462 | case 30: | |
463 | case 31: tmplockbit = bit2[2]; break; | |
464 | case 32: | |
465 | case 33: | |
466 | case 34: | |
467 | case 35: tmplockbit = bit2[1]; break; | |
468 | case 36: | |
469 | case 37: | |
470 | case 38: | |
471 | case 39: tmplockbit = bit2[0]; break; | |
472 | case 40: tmplockbit = bit2[12]; break; | |
473 | case 41: tmplockbit = bit2[11]; break; | |
474 | case 42: tmplockbit = bit2[10]; break; //auth0 | |
475 | case 43: tmplockbit = bit2[9]; break; //auth1 | |
476 | default: break; | |
477 | } | |
478 | PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),tmplockbit); | |
479 | } | |
480 | ||
481 | int len = param_getstr(Cmd,0,filename); | |
482 | if (len > FILE_PATH_SIZE-5) | |
483 | len = FILE_PATH_SIZE-5; | |
484 | ||
485 | if (len < 1) { | |
486 | sprintf(fnameptr,"%02X%02X%02X%02X%02X%02X%02X.bin", | |
487 | data[0], data[1], data[2], data[4], data[5], data[6], data[7]); | |
488 | } else { | |
489 | sprintf(fnameptr + len," .bin"); | |
490 | } | |
491 | ||
492 | if ((fout = fopen(filename,"wb")) == NULL) { | |
493 | PrintAndLog("Could not create file name %s", filename); | |
494 | return 1; | |
495 | } | |
496 | fwrite( data, 1, Pages*4, fout ); | |
497 | fclose(fout); | |
498 | ||
499 | PrintAndLog("Dumped %d pages, wrote %d bytes to %s", Pages, Pages*4, filename); | |
500 | return 0; | |
501 | } | |
502 | ||
503 | // Needed to Authenticate to Ultralight C tags | |
504 | void rol (uint8_t *data, const size_t len){ | |
505 | uint8_t first = data[0]; | |
506 | for (size_t i = 0; i < len-1; i++) { | |
507 | data[i] = data[i+1]; | |
508 | } | |
509 | data[len-1] = first; | |
510 | } | |
511 | ||
512 | //------------------------------------------------------------------------------- | |
513 | // Ultralight C Methods | |
514 | //------------------------------------------------------------------------------- | |
515 | ||
516 | // | |
517 | // Ultralight C Authentication Demo {currently uses hard-coded key} | |
518 | // | |
519 | int CmdHF14AMfucAuth(const char *Cmd){ | |
520 | ||
521 | uint8_t keyNo = 0; | |
522 | bool errors = false; | |
523 | ||
524 | char cmdp = param_getchar(Cmd, 0); | |
525 | ||
526 | //Change key to user defined one | |
527 | if (cmdp == 'k' || cmdp == 'K'){ | |
528 | keyNo = param_get8(Cmd, 1); | |
529 | if(keyNo > 6) | |
530 | errors = true; | |
531 | } | |
532 | ||
533 | if (cmdp == 'h' || cmdp == 'H') | |
534 | errors = true; | |
535 | ||
536 | if (errors) { | |
537 | PrintAndLog("Usage: hf mfu cauth k <key number>"); | |
538 | PrintAndLog(" 0 (default): 3DES standard key"); | |
539 | PrintAndLog(" 1 : all 0x00 key"); | |
540 | PrintAndLog(" 2 : 0x00-0x0F key"); | |
541 | PrintAndLog(" 3 : nfc key"); | |
542 | PrintAndLog(" 4 : all 0x01 key"); | |
543 | PrintAndLog(" 5 : all 0xff key"); | |
544 | PrintAndLog(" 6 : 0x00-0xFF key"); | |
545 | PrintAndLog("\n sample : hf mfu cauth k"); | |
546 | PrintAndLog(" : hf mfu cauth k 3"); | |
547 | return 0; | |
548 | } | |
549 | ||
550 | uint8_t *key = default_3des_keys[keyNo]; | |
551 | if (try3DesAuthentication(key)) | |
552 | PrintAndLog("Authentication successful. 3des key: %s",sprint_hex(key, 8)); | |
553 | else | |
554 | PrintAndLog("Authentication failed"); | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
559 | int try3DesAuthentication( uint8_t *key){ | |
560 | ||
561 | uint8_t blockNo = 0; | |
562 | uint32_t cuid = 0; | |
563 | ||
564 | des3_context ctx = { 0 }; | |
565 | ||
566 | uint8_t random_a[8] = { 1,1,1,1,1,1,1,1 }; | |
567 | uint8_t random_b[8] = { 0 }; | |
568 | uint8_t enc_random_b[8] = { 0 }; | |
569 | uint8_t rnd_ab[16] = { 0 }; | |
570 | uint8_t iv[8] = { 0 }; | |
571 | ||
572 | UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}}; | |
573 | SendCommand(&c); | |
574 | UsbCommand resp; | |
575 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return -1; | |
576 | if ( !(resp.arg[0] & 0xff) ) return -2; | |
577 | ||
578 | cuid = resp.arg[1]; | |
579 | memcpy(enc_random_b,resp.d.asBytes+1,8); | |
580 | ||
581 | des3_set2key_dec(&ctx, key); | |
582 | // context, mode, length, IV, input, output | |
583 | des3_crypt_cbc( &ctx, DES_DECRYPT, sizeof(random_b), iv , enc_random_b , random_b); | |
584 | ||
585 | rol(random_b,8); | |
586 | memcpy(rnd_ab ,random_a,8); | |
587 | memcpy(rnd_ab+8,random_b,8); | |
588 | ||
589 | des3_set2key_enc(&ctx, key); | |
590 | // context, mode, length, IV, input, output | |
591 | des3_crypt_cbc(&ctx, DES_ENCRYPT, sizeof(rnd_ab), enc_random_b, rnd_ab, rnd_ab); | |
592 | ||
593 | //Auth2 | |
594 | c.cmd = CMD_MIFAREUC_AUTH2; | |
595 | c.arg[0] = cuid; | |
596 | memcpy(c.d.asBytes, rnd_ab, 16); | |
597 | SendCommand(&c); | |
598 | ||
599 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1; | |
600 | if ( !(resp.arg[0] & 0xff)) return -2; | |
601 | ||
602 | uint8_t enc_resp[8] = { 0 }; | |
603 | uint8_t resp_random_a[8] = { 0 }; | |
604 | memcpy(enc_resp, resp.d.asBytes+1, 8); | |
605 | ||
606 | des3_set2key_dec(&ctx, key); | |
607 | // context, mode, length, IV, input, output | |
608 | des3_crypt_cbc( &ctx, DES_DECRYPT, 8, enc_random_b, enc_resp, resp_random_a); | |
609 | ||
610 | if ( !memcmp(resp_random_a, random_a, 8)) | |
611 | return 1; | |
612 | return 0; | |
613 | ||
614 | //PrintAndLog(" RndA :%s", sprint_hex(random_a, 8)); | |
615 | //PrintAndLog(" enc(RndB) :%s", sprint_hex(enc_random_b, 8)); | |
616 | //PrintAndLog(" RndB :%s", sprint_hex(random_b, 8)); | |
617 | //PrintAndLog(" A+B :%s", sprint_hex(random_a_and_b, 16)); | |
618 | //PrintAndLog(" enc(A+B) :%s", sprint_hex(random_a_and_b, 16)); | |
619 | //PrintAndLog(" enc(RndA') :%s", sprint_hex(data2+1, 8)); | |
620 | } | |
621 | ||
622 | /** | |
623 | A test function to validate that the polarssl-function works the same | |
624 | was as the openssl-implementation. | |
625 | Commented out, since it requires openssl | |
626 | ||
627 | int CmdTestDES(const char * cmd) | |
628 | { | |
629 | uint8_t key[16] = {0x00}; | |
630 | ||
631 | memcpy(key,key3_3des_data,16); | |
632 | DES_cblock RndA, RndB; | |
633 | ||
634 | PrintAndLog("----------OpenSSL DES implementation----------"); | |
635 | { | |
636 | uint8_t e_RndB[8] = {0x00}; | |
637 | unsigned char RndARndB[16] = {0x00}; | |
638 | ||
639 | DES_cblock iv = { 0 }; | |
640 | DES_key_schedule ks1,ks2; | |
641 | DES_cblock key1,key2; | |
642 | ||
643 | memcpy(key,key3_3des_data,16); | |
644 | memcpy(key1,key,8); | |
645 | memcpy(key2,key+8,8); | |
646 | ||
647 | ||
648 | DES_set_key((DES_cblock *)key1,&ks1); | |
649 | DES_set_key((DES_cblock *)key2,&ks2); | |
650 | ||
651 | DES_random_key(&RndA); | |
652 | PrintAndLog(" RndA:%s",sprint_hex(RndA, 8)); | |
653 | PrintAndLog(" e_RndB:%s",sprint_hex(e_RndB, 8)); | |
654 | //void DES_ede2_cbc_encrypt(const unsigned char *input, | |
655 | // unsigned char *output, long length, DES_key_schedule *ks1, | |
656 | // DES_key_schedule *ks2, DES_cblock *ivec, int enc); | |
657 | DES_ede2_cbc_encrypt(e_RndB,RndB,sizeof(e_RndB),&ks1,&ks2,&iv,0); | |
658 | ||
659 | PrintAndLog(" RndB:%s",sprint_hex(RndB, 8)); | |
660 | rol(RndB,8); | |
661 | memcpy(RndARndB,RndA,8); | |
662 | memcpy(RndARndB+8,RndB,8); | |
663 | PrintAndLog(" RA+B:%s",sprint_hex(RndARndB, 16)); | |
664 | DES_ede2_cbc_encrypt(RndARndB,RndARndB,sizeof(RndARndB),&ks1,&ks2,&e_RndB,1); | |
665 | PrintAndLog("enc(RA+B):%s",sprint_hex(RndARndB, 16)); | |
666 | ||
667 | } | |
668 | PrintAndLog("----------PolarSSL implementation----------"); | |
669 | { | |
670 | uint8_t random_a[8] = { 0 }; | |
671 | uint8_t enc_random_a[8] = { 0 }; | |
672 | uint8_t random_b[8] = { 0 }; | |
673 | uint8_t enc_random_b[8] = { 0 }; | |
674 | uint8_t random_a_and_b[16] = { 0 }; | |
675 | des3_context ctx = { 0 }; | |
676 | ||
677 | memcpy(random_a, RndA,8); | |
678 | ||
679 | uint8_t output[8] = { 0 }; | |
680 | uint8_t iv[8] = { 0 }; | |
681 | ||
682 | PrintAndLog(" RndA :%s",sprint_hex(random_a, 8)); | |
683 | PrintAndLog(" e_RndB:%s",sprint_hex(enc_random_b, 8)); | |
684 | ||
685 | des3_set2key_dec(&ctx, key); | |
686 | ||
687 | des3_crypt_cbc(&ctx // des3_context *ctx | |
688 | , DES_DECRYPT // int mode | |
689 | , sizeof(random_b) // size_t length | |
690 | , iv // unsigned char iv[8] | |
691 | , enc_random_b // const unsigned char *input | |
692 | , random_b // unsigned char *output | |
693 | ); | |
694 | ||
695 | PrintAndLog(" RndB:%s",sprint_hex(random_b, 8)); | |
696 | ||
697 | rol(random_b,8); | |
698 | memcpy(random_a_and_b ,random_a,8); | |
699 | memcpy(random_a_and_b+8,random_b,8); | |
700 | ||
701 | PrintAndLog(" RA+B:%s",sprint_hex(random_a_and_b, 16)); | |
702 | ||
703 | des3_set2key_enc(&ctx, key); | |
704 | ||
705 | des3_crypt_cbc(&ctx // des3_context *ctx | |
706 | , DES_ENCRYPT // int mode | |
707 | , sizeof(random_a_and_b) // size_t length | |
708 | , enc_random_b // unsigned char iv[8] | |
709 | , random_a_and_b // const unsigned char *input | |
710 | , random_a_and_b // unsigned char *output | |
711 | ); | |
712 | ||
713 | PrintAndLog("enc(RA+B):%s",sprint_hex(random_a_and_b, 16)); | |
714 | } | |
715 | return 0; | |
716 | } | |
717 | **/ | |
718 | // | |
719 | // Ultralight C Read Single Block | |
720 | // | |
721 | int CmdHF14AMfUCRdBl(const char *Cmd) | |
722 | { | |
723 | UsbCommand resp; | |
724 | bool hasPwd = FALSE; | |
725 | uint8_t blockNo = -1; | |
726 | unsigned char key[16]; | |
727 | char cmdp = param_getchar(Cmd, 0); | |
728 | ||
729 | if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') { | |
730 | PrintAndLog("Usage: hf mfu crdbl <block number> <password>"); | |
731 | PrintAndLog(""); | |
732 | PrintAndLog("sample: hf mfu crdbl 0"); | |
733 | PrintAndLog(" hf mfu crdbl 0 112233445566778899AABBCCDDEEFF"); | |
734 | return 0; | |
735 | } | |
736 | ||
737 | blockNo = param_get8(Cmd, 0); | |
738 | if (blockNo < 0) { | |
739 | PrintAndLog("Wrong block number"); | |
740 | return 1; | |
741 | } | |
742 | ||
743 | if (blockNo > MAX_ULC_BLOCKS ){ | |
744 | PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C"); | |
745 | return 1; | |
746 | } | |
747 | ||
748 | // key | |
749 | if ( strlen(Cmd) > 3){ | |
750 | if (param_gethex(Cmd, 1, key, 32)) { | |
751 | PrintAndLog("Key must include %d HEX symbols", 32); | |
752 | return 1; | |
753 | } else { | |
754 | hasPwd = TRUE; | |
755 | } | |
756 | } | |
757 | ||
758 | //Read Block | |
759 | UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}}; | |
760 | if ( hasPwd ) { | |
761 | c.arg[1] = 1; | |
762 | memcpy(c.d.asBytes,key,16); | |
763 | } | |
764 | SendCommand(&c); | |
765 | ||
766 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
767 | uint8_t isOK = resp.arg[0] & 0xff; | |
768 | if (isOK) { | |
769 | uint8_t *data = resp.d.asBytes; | |
770 | PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4)); | |
771 | } | |
772 | else { | |
773 | PrintAndLog("Failed reading block: (%02x)", isOK); | |
774 | } | |
775 | } else { | |
776 | PrintAndLog("Command execute time-out"); | |
777 | } | |
778 | return 0; | |
779 | } | |
780 | ||
781 | // | |
782 | // Mifare Ultralight C Write Single Block | |
783 | // | |
784 | int CmdHF14AMfUCWrBl(const char *Cmd){ | |
785 | ||
786 | uint8_t blockNo = -1; | |
787 | bool chinese_card = FALSE; | |
788 | uint8_t bldata[16] = {0x00}; | |
789 | UsbCommand resp; | |
790 | ||
791 | char cmdp = param_getchar(Cmd, 0); | |
792 | ||
793 | if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') { | |
794 | PrintAndLog("Usage: hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]"); | |
795 | PrintAndLog(" [block number]"); | |
796 | PrintAndLog(" [block data] - (8 hex symbols)"); | |
797 | PrintAndLog(" [w] - Chinese magic ultralight tag"); | |
798 | PrintAndLog(""); | |
799 | PrintAndLog(" sample: hf mfu cwrbl 0 01020304"); | |
800 | PrintAndLog(""); | |
801 | return 0; | |
802 | } | |
803 | ||
804 | blockNo = param_get8(Cmd, 0); | |
805 | if (blockNo > MAX_ULC_BLOCKS ){ | |
806 | PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!"); | |
807 | return 1; | |
808 | } | |
809 | ||
810 | if (param_gethex(Cmd, 1, bldata, 8)) { | |
811 | PrintAndLog("Block data must include 8 HEX symbols"); | |
812 | return 1; | |
813 | } | |
814 | ||
815 | if (strchr(Cmd,'w') != 0 || strchr(Cmd,'W') != 0 ) { | |
816 | chinese_card = TRUE; | |
817 | } | |
818 | ||
819 | if ( blockNo <= 3 ) { | |
820 | if (!chinese_card){ | |
821 | PrintAndLog("Access Denied"); | |
822 | } else { | |
823 | PrintAndLog("--Special block no: 0x%02x", blockNo); | |
824 | PrintAndLog("--Data: %s", sprint_hex(bldata, 4)); | |
825 | UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
826 | memcpy(d.d.asBytes,bldata, 4); | |
827 | SendCommand(&d); | |
828 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
829 | uint8_t isOK = resp.arg[0] & 0xff; | |
830 | PrintAndLog("isOk:%02x", isOK); | |
831 | } else { | |
832 | PrintAndLog("Command execute timeout"); | |
833 | } | |
834 | } | |
835 | } else { | |
836 | PrintAndLog("--Block no : 0x%02x", blockNo); | |
837 | PrintAndLog("--Data: %s", sprint_hex(bldata, 4)); | |
838 | UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}}; | |
839 | memcpy(e.d.asBytes,bldata, 4); | |
840 | SendCommand(&e); | |
841 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
842 | uint8_t isOK = resp.arg[0] & 0xff; | |
843 | PrintAndLog("isOk : %02x", isOK); | |
844 | } else { | |
845 | PrintAndLog("Command execute timeout"); | |
846 | } | |
847 | } | |
848 | return 0; | |
849 | } | |
850 | ||
851 | // | |
852 | // Mifare Ultralight C - Set password | |
853 | // | |
854 | int CmdHF14AMfucSetPwd(const char *Cmd){ | |
855 | ||
856 | uint8_t pwd[16] = {0x00}; | |
857 | char cmdp = param_getchar(Cmd, 0); | |
858 | ||
859 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') { | |
860 | PrintAndLog("Usage: hf mfu setpwd <password (32 hex symbols)>"); | |
861 | PrintAndLog(" [password] - (32 hex symbols)"); | |
862 | PrintAndLog(""); | |
863 | PrintAndLog("sample: hf mfu setpwd 000102030405060708090a0b0c0d0e0f"); | |
864 | PrintAndLog(""); | |
865 | return 0; | |
866 | } | |
867 | ||
868 | if (param_gethex(Cmd, 0, pwd, 32)) { | |
869 | PrintAndLog("Password must include 32 HEX symbols"); | |
870 | return 1; | |
871 | } | |
872 | ||
873 | UsbCommand c = {CMD_MIFAREUC_SETPWD}; | |
874 | memcpy( c.d.asBytes, pwd, 16); | |
875 | SendCommand(&c); | |
876 | ||
877 | UsbCommand resp; | |
878 | ||
879 | if (WaitForResponseTimeout(CMD_ACK,&resp,1500) ) { | |
880 | if ( (resp.arg[0] & 0xff) == 1) | |
881 | PrintAndLog("Ultralight-C new password: %s", sprint_hex(pwd,16)); | |
882 | else{ | |
883 | PrintAndLog("Failed writing at block %d", resp.arg[1] & 0xff); | |
884 | return 1; | |
885 | } | |
886 | } | |
887 | else { | |
888 | PrintAndLog("command execution time out"); | |
889 | } | |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | // | |
895 | // Magic UL / UL-C tags - Set UID | |
896 | // | |
897 | int CmdHF14AMfucSetUid(const char *Cmd){ | |
898 | ||
899 | UsbCommand c; | |
900 | UsbCommand resp; | |
901 | uint8_t uid[7] = {0x00}; | |
902 | char cmdp = param_getchar(Cmd, 0); | |
903 | ||
904 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') { | |
905 | PrintAndLog("Usage: hf mfu setuid <uid (14 hex symbols)>"); | |
906 | PrintAndLog(" [uid] - (14 hex symbols)"); | |
907 | PrintAndLog("\nThis only works for Magic Ultralight tags."); | |
908 | PrintAndLog(""); | |
909 | PrintAndLog("sample: hf mfu setuid 11223344556677"); | |
910 | PrintAndLog(""); | |
911 | return 0; | |
912 | } | |
913 | ||
914 | if (param_gethex(Cmd, 0, uid, 14)) { | |
915 | PrintAndLog("UID must include 14 HEX symbols"); | |
916 | return 1; | |
917 | } | |
918 | ||
919 | // read block2. | |
920 | c.cmd = CMD_MIFAREU_READBL; | |
921 | c.arg[0] = 2; | |
922 | SendCommand(&c); | |
923 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
924 | PrintAndLog("Command execute timeout"); | |
925 | return 2; | |
926 | } | |
927 | ||
928 | // save old block2. | |
929 | uint8_t oldblock2[4] = {0x00}; | |
930 | memcpy(resp.d.asBytes, oldblock2, 4); | |
931 | ||
932 | // block 0. | |
933 | c.cmd = CMD_MIFAREU_WRITEBL; | |
934 | c.arg[0] = 0; | |
935 | c.d.asBytes[0] = uid[0]; | |
936 | c.d.asBytes[1] = uid[1]; | |
937 | c.d.asBytes[2] = uid[2]; | |
938 | c.d.asBytes[3] = 0x88 ^ uid[0] ^ uid[1] ^ uid[2]; | |
939 | SendCommand(&c); | |
940 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) { | |
941 | PrintAndLog("Command execute timeout"); | |
942 | return 3; | |
943 | } | |
944 | ||
945 | // block 1. | |
946 | c.arg[0] = 1; | |
947 | c.d.asBytes[0] = uid[3]; | |
948 | c.d.asBytes[1] = uid[4]; | |
949 | c.d.asBytes[2] = uid[5]; | |
950 | c.d.asBytes[3] = uid[6]; | |
951 | SendCommand(&c); | |
952 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) { | |
953 | PrintAndLog("Command execute timeout"); | |
954 | return 4; | |
955 | } | |
956 | ||
957 | // block 2. | |
958 | c.arg[0] = 2; | |
959 | c.d.asBytes[0] = uid[3] ^ uid[4] ^ uid[5] ^ uid[6]; | |
960 | c.d.asBytes[1] = oldblock2[1]; | |
961 | c.d.asBytes[2] = oldblock2[2]; | |
962 | c.d.asBytes[3] = oldblock2[3]; | |
963 | SendCommand(&c); | |
964 | if (!WaitForResponseTimeout(CMD_ACK,&resp,1500) ) { | |
965 | PrintAndLog("Command execute timeout"); | |
966 | return 5; | |
967 | } | |
968 | ||
969 | return 0; | |
970 | } | |
971 | ||
972 | int CmdHF14AMfuGenDiverseKeys(const char *Cmd){ | |
973 | ||
974 | uint8_t iv[8] = { 0x00 }; | |
975 | uint8_t block = 0x07; | |
976 | ||
977 | // UL-EV1 | |
978 | //04 57 b6 e2 05 3f 80 UID | |
979 | //4a f8 4b 19 PWD | |
980 | uint8_t uid[] = { 0xF4,0xEA, 0x54, 0x8E }; | |
981 | uint8_t mifarekeyA[] = { 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5 }; | |
982 | uint8_t mifarekeyB[] = { 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5 }; | |
983 | uint8_t dkeyA[8] = { 0x00 }; | |
984 | uint8_t dkeyB[8] = { 0x00 }; | |
985 | ||
986 | uint8_t masterkey[] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff }; | |
987 | ||
988 | uint8_t mix[8] = { 0x00 }; | |
989 | uint8_t divkey[8] = { 0x00 }; | |
990 | ||
991 | memcpy(mix, mifarekeyA, 4); | |
992 | ||
993 | mix[4] = mifarekeyA[4] ^ uid[0]; | |
994 | mix[5] = mifarekeyA[5] ^ uid[1]; | |
995 | mix[6] = block ^ uid[2]; | |
996 | mix[7] = uid[3]; | |
997 | ||
998 | des3_context ctx = { 0x00 }; | |
999 | des3_set2key_enc(&ctx, masterkey); | |
1000 | ||
1001 | des3_crypt_cbc(&ctx // des3_context | |
1002 | , DES_ENCRYPT // int mode | |
1003 | , sizeof(mix) // length | |
1004 | , iv // iv[8] | |
1005 | , mix // input | |
1006 | , divkey // output | |
1007 | ); | |
1008 | ||
1009 | PrintAndLog("3DES version"); | |
1010 | PrintAndLog("Masterkey :\t %s", sprint_hex(masterkey,sizeof(masterkey))); | |
1011 | PrintAndLog("UID :\t %s", sprint_hex(uid, sizeof(uid))); | |
1012 | PrintAndLog("Sector :\t %0d", block); | |
1013 | PrintAndLog("Mifare key :\t %s", sprint_hex(mifarekeyA, sizeof(mifarekeyA))); | |
1014 | PrintAndLog("Message :\t %s", sprint_hex(mix, sizeof(mix))); | |
1015 | PrintAndLog("Diversified key: %s", sprint_hex(divkey+1, 6)); | |
1016 | ||
1017 | PrintAndLog("\n DES version"); | |
1018 | ||
1019 | for (int i=0; i < sizeof(mifarekeyA); ++i){ | |
1020 | dkeyA[i] = (mifarekeyA[i] << 1) & 0xff; | |
1021 | dkeyA[6] |= ((mifarekeyA[i] >> 7) & 1) << (i+1); | |
1022 | } | |
1023 | ||
1024 | for (int i=0; i < sizeof(mifarekeyB); ++i){ | |
1025 | dkeyB[1] |= ((mifarekeyB[i] >> 7) & 1) << (i+1); | |
1026 | dkeyB[2+i] = (mifarekeyB[i] << 1) & 0xff; | |
1027 | } | |
1028 | ||
1029 | uint8_t zeros[8] = {0x00}; | |
1030 | uint8_t newpwd[8] = {0x00}; | |
1031 | uint8_t dmkey[24] = {0x00}; | |
1032 | memcpy(dmkey, dkeyA, 8); | |
1033 | memcpy(dmkey+8, dkeyB, 8); | |
1034 | memcpy(dmkey+16, dkeyA, 8); | |
1035 | memset(iv, 0x00, 8); | |
1036 | ||
1037 | des3_set3key_enc(&ctx, dmkey); | |
1038 | ||
1039 | des3_crypt_cbc(&ctx // des3_context | |
1040 | , DES_ENCRYPT // int mode | |
1041 | , sizeof(newpwd) // length | |
1042 | , iv // iv[8] | |
1043 | , zeros // input | |
1044 | , newpwd // output | |
1045 | ); | |
1046 | ||
1047 | PrintAndLog("Mifare dkeyA :\t %s", sprint_hex(dkeyA, sizeof(dkeyA))); | |
1048 | PrintAndLog("Mifare dkeyB :\t %s", sprint_hex(dkeyB, sizeof(dkeyB))); | |
1049 | PrintAndLog("Mifare ABA :\t %s", sprint_hex(dmkey, sizeof(dmkey))); | |
1050 | PrintAndLog("Mifare Pwd :\t %s", sprint_hex(newpwd, sizeof(newpwd))); | |
1051 | ||
1052 | return 0; | |
1053 | } | |
1054 | ||
1055 | // static uint8_t * diversify_key(uint8_t * key){ | |
1056 | ||
1057 | // for(int i=0; i<16; i++){ | |
1058 | // if(i<=6) key[i]^=cuid[i]; | |
1059 | // if(i>6) key[i]^=cuid[i%7]; | |
1060 | // } | |
1061 | // return key; | |
1062 | // } | |
1063 | ||
1064 | // static void GenerateUIDe( uint8_t *uid, uint8_t len){ | |
1065 | // for (int i=0; i<len; ++i){ | |
1066 | ||
1067 | // } | |
1068 | // return; | |
1069 | // } | |
1070 | ||
1071 | static command_t CommandTable[] = | |
1072 | { | |
1073 | {"help", CmdHelp, 1, "This help"}, | |
1074 | {"dbg", CmdHF14AMfDbg, 0, "Set default debug mode"}, | |
1075 | {"info", CmdHF14AMfUInfo, 0, "Tag information"}, | |
1076 | {"dump", CmdHF14AMfUDump, 0, "Dump Ultralight / Ultralight-C tag to binary file"}, | |
1077 | {"rdbl", CmdHF14AMfURdBl, 0, "Read block - Ultralight"}, | |
1078 | {"wrbl", CmdHF14AMfUWrBl, 0, "Write block - Ultralight"}, | |
1079 | {"crdbl", CmdHF14AMfUCRdBl, 0, "Read block - Ultralight C"}, | |
1080 | {"cwrbl", CmdHF14AMfUCWrBl, 0, "Write block - Ultralight C"}, | |
1081 | {"cauth", CmdHF14AMfucAuth, 0, "Authentication - Ultralight C"}, | |
1082 | {"setpwd", CmdHF14AMfucSetPwd, 1, "Set 3des password - Ultralight-C"}, | |
1083 | {"setuid", CmdHF14AMfucSetUid, 1, "Set UID - MAGIC tags only"}, | |
1084 | {"gen", CmdHF14AMfuGenDiverseKeys , 1, "Generate 3des mifare diversified keys"}, | |
1085 | {NULL, NULL, 0, NULL} | |
1086 | }; | |
1087 | ||
1088 | int CmdHFMFUltra(const char *Cmd){ | |
1089 | WaitForResponseTimeout(CMD_ACK,NULL,100); | |
1090 | CmdsParse(CommandTable, Cmd); | |
1091 | return 0; | |
1092 | } | |
1093 | ||
1094 | int CmdHelp(const char *Cmd){ | |
1095 | CmdsHelp(CommandTable); | |
1096 | return 0; | |
1097 | } |