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