]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhflegic.c
CHG: Adding clarity to the command helptext.
[proxmark3-svn] / client / cmdhflegic.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 Legic commands
9 //-----------------------------------------------------------------------------
10 #include "cmdhflegic.h"
11
12 static int CmdHelp(const char *Cmd);
13
14 #define SESSION_IV 0x55
15 #define MAX_LENGTH 1024
16
17 int usage_legic_calccrc8(void){
18 PrintAndLog("Calculates the legic crc8/crc16 on the input hexbytes.");
19 PrintAndLog("There must be an even number of hexsymbols as input.");
20 PrintAndLog("Usage: hf legic crc8 [h] b <hexbytes> u <uidcrc> c <crc type>");
21 PrintAndLog("Options:");
22 PrintAndLog(" h : this help");
23 PrintAndLog(" b <hexbytes> : hex bytes");
24 PrintAndLog(" u <uidcrc> : MCC hexbyte");
25 PrintAndLog(" c <crc type> : 8|16 bit crc size");
26 PrintAndLog("");
27 PrintAndLog("Samples:");
28 PrintAndLog(" hf legic crc8 b deadbeef1122");
29 PrintAndLog(" hf legic crc8 b deadbeef1122 u 9A c 16");
30 return 0;
31 }
32 int usage_legic_load(void){
33 PrintAndLog("It loads datasamples from the file `filename` to device memory");
34 PrintAndLog("Usage: hf legic load [h] <file name>");
35 PrintAndLog("Options:");
36 PrintAndLog(" h : this help");
37 PrintAndLog(" <filename> : Name of file to load");
38 PrintAndLog("");
39 PrintAndLog("Samples:");
40 PrintAndLog(" hf legic load filename");
41 return 0;
42 }
43 int usage_legic_read(void){
44 PrintAndLog("Read data from a legic tag.");
45 PrintAndLog("Usage: hf legic read [h] <offset> <length> <IV>");
46 PrintAndLog("Options:");
47 PrintAndLog(" h : this help");
48 PrintAndLog(" <offset> : offset in data array to start download from");
49 PrintAndLog(" <length> : number of bytes to download");
50 PrintAndLog(" <IV> : (optional) Initialization vector to use");
51 PrintAndLog("");
52 PrintAndLog("Samples:");
53 PrintAndLog(" hf legic read");
54 PrintAndLog(" hf legic read 10 4");
55 return 0;
56 }
57 int usage_legic_sim(void){
58 PrintAndLog("Missing help text.");
59 return 0;
60 }
61 int usage_legic_write(void){
62 PrintAndLog(" Write sample buffer to a legic tag. (use after load or read)");
63 PrintAndLog("Usage: hf legic write [h] <offset> <length> <IV>");
64 PrintAndLog("Options:");
65 PrintAndLog(" h : this help");
66 PrintAndLog(" <offset> : offset in data array to start writing from");
67 PrintAndLog(" <length> : number of bytes to write");
68 PrintAndLog(" <IV> : (optional) Initialization vector to use");
69 PrintAndLog("");
70 PrintAndLog("Samples:");
71 PrintAndLog(" hf legic write");
72 PrintAndLog(" hf legic write 10 4");
73 return 0;
74 }
75 int usage_legic_rawwrite(void){
76 PrintAndLog("Write raw data direct to a specific address on legic tag.");
77 PrintAndLog("Usage: hf legic writeraw [h] <address> <value> <IV>");
78 PrintAndLog("Options:");
79 PrintAndLog(" h : this help");
80 PrintAndLog(" <address> : address to write to");
81 PrintAndLog(" <value> : value to write");
82 PrintAndLog(" <IV> : (optional) Initialization vector to use");
83 PrintAndLog("");
84 PrintAndLog("Samples:");
85 PrintAndLog(" hf legic writeraw");
86 PrintAndLog(" hf legic writeraw 10 4");
87 return 0;
88 }
89 int usage_legic_fill(void){
90 PrintAndLog("Missing help text.");
91 return 0;
92 }
93
94 /*
95 * Output BigBuf and deobfuscate LEGIC RF tag data.
96 * This is based on information given in the talk held
97 * by Henryk Ploetz and Karsten Nohl at 26c3
98 */
99 int CmdLegicDecode(const char *Cmd) {
100
101 int i = 0, k = 0, segmentNum = 0, segment_len = 0, segment_flag = 0;
102 int crc = 0, wrp = 0, wrc = 0;
103 uint8_t stamp_len = 0;
104 uint8_t data_buf[1052]; // receiver buffer
105 char token_type[5] = {0,0,0,0,0};
106 int dcf = 0;
107 int bIsSegmented = 0;
108
109 // download EML memory, where the "legic read" command puts the data.
110 GetEMLFromBigBuf(data_buf, sizeof(data_buf), 0);
111 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){
112 PrintAndLog("Command execute timeout");
113 return 1;
114 }
115
116 // Output CDF System area (9 bytes) plus remaining header area (12 bytes)
117 crc = data_buf[4];
118 uint32_t calc_crc = CRC8Legic(data_buf, 4);
119
120 PrintAndLog("\nCDF: System Area");
121 PrintAndLog("------------------------------------------------------");
122 PrintAndLog("MCD: %02x, MSN: %02x %02x %02x, MCC: %02x %s",
123 data_buf[0],
124 data_buf[1],
125 data_buf[2],
126 data_buf[3],
127 data_buf[4],
128 (calc_crc == crc) ? "OK":"Fail"
129 );
130
131
132 token_type[0] = 0;
133 dcf = ((int)data_buf[6] << 8) | (int)data_buf[5];
134
135 // New unwritten media?
136 if(dcf == 0xFFFF) {
137
138 PrintAndLog("DCF: %d (%02x %02x), Token Type=NM (New Media)",
139 dcf,
140 data_buf[5],
141 data_buf[6]
142 );
143
144 } else if(dcf > 60000) { // Master token?
145
146 int fl = 0;
147
148 if(data_buf[6] == 0xec) {
149 strncpy(token_type, "XAM", sizeof(token_type));
150 fl = 1;
151 stamp_len = 0x0c - (data_buf[5] >> 4);
152 } else {
153 switch (data_buf[5] & 0x7f) {
154 case 0x00 ... 0x2f:
155 strncpy(token_type, "IAM", sizeof(token_type));
156 fl = (0x2f - (data_buf[5] & 0x7f)) + 1;
157 break;
158 case 0x30 ... 0x6f:
159 strncpy(token_type, "SAM", sizeof(token_type));
160 fl = (0x6f - (data_buf[5] & 0x7f)) + 1;
161 break;
162 case 0x70 ... 0x7f:
163 strncpy(token_type, "GAM", sizeof(token_type));
164 fl = (0x7f - (data_buf[5] & 0x7f)) + 1;
165 break;
166 }
167
168 stamp_len = 0xfc - data_buf[6];
169 }
170
171 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u), OL=%02u, FL=%02u",
172 dcf,
173 data_buf[5],
174 data_buf[6],
175 token_type,
176 (data_buf[5] & 0x80 )>> 7,
177 stamp_len,
178 fl
179 );
180
181 } else { // Is IM(-S) type of card...
182
183 if(data_buf[7] == 0x9F && data_buf[8] == 0xFF) {
184 bIsSegmented = 1;
185 strncpy(token_type, "IM-S", sizeof(token_type));
186 } else {
187 strncpy(token_type, "IM", sizeof(token_type));
188 }
189
190 PrintAndLog("DCF: %d (%02x %02x), Token Type=%s (OLE=%01u)",
191 dcf,
192 data_buf[5],
193 data_buf[6],
194 token_type,
195 (data_buf[5]&0x80) >> 7
196 );
197 }
198
199 // Makes no sence to show this on blank media...
200 if(dcf != 0xFFFF) {
201
202 if(bIsSegmented) {
203 PrintAndLog("WRP=%02u, WRC=%01u, RD=%01u, SSC=%02x",
204 data_buf[7] & 0x0f,
205 (data_buf[7] & 0x70) >> 4,
206 (data_buf[7] & 0x80) >> 7,
207 data_buf[8]
208 );
209 }
210
211 // Header area is only available on IM-S cards, on master tokens this data is the master token data itself
212 if(bIsSegmented || dcf > 60000) {
213 if(dcf > 60000) {
214 PrintAndLog("Master token data");
215 PrintAndLog("%s", sprint_hex(data_buf+8, 14));
216 } else {
217 PrintAndLog("Remaining Header Area");
218 PrintAndLog("%s", sprint_hex(data_buf+9, 13));
219 }
220 }
221 }
222
223 uint8_t segCrcBytes[8] = {0,0,0,0,0,0,0,0};
224 uint32_t segCalcCRC = 0;
225 uint32_t segCRC = 0;
226
227 // Data card?
228 if(dcf <= 60000) {
229
230 PrintAndLog("\nADF: User Area");
231 PrintAndLog("------------------------------------------------------");
232
233 if(bIsSegmented) {
234
235 // Data start point on segmented cards
236 i = 22;
237
238 // decode segments
239 for (segmentNum=1; segmentNum < 128; segmentNum++ )
240 {
241 segment_len = ((data_buf[i+1] ^ crc) & 0x0f) * 256 + (data_buf[i] ^ crc);
242 segment_flag = ((data_buf[i+1] ^ crc) & 0xf0) >> 4;
243 wrp = (data_buf[i+2] ^ crc);
244 wrc = ((data_buf[i+3] ^ crc) & 0x70) >> 4;
245
246 bool hasWRC = (wrc > 0);
247 bool hasWRP = (wrp > wrc);
248 int wrp_len = (wrp - wrc);
249 int remain_seg_payload_len = (segment_len - wrp - 5);
250
251 // validate segment-crc
252 segCrcBytes[0]=data_buf[0]; //uid0
253 segCrcBytes[1]=data_buf[1]; //uid1
254 segCrcBytes[2]=data_buf[2]; //uid2
255 segCrcBytes[3]=data_buf[3]; //uid3
256 segCrcBytes[4]=(data_buf[i] ^ crc); //hdr0
257 segCrcBytes[5]=(data_buf[i+1] ^ crc); //hdr1
258 segCrcBytes[6]=(data_buf[i+2] ^ crc); //hdr2
259 segCrcBytes[7]=(data_buf[i+3] ^ crc); //hdr3
260
261 segCalcCRC = CRC8Legic(segCrcBytes, 8);
262 segCRC = data_buf[i+4] ^ crc;
263
264 PrintAndLog("Segment %02u \nraw header | 0x%02X 0x%02X 0x%02X 0x%02X \nSegment len: %u, Flag: 0x%X (valid:%01u, last:%01u), WRP: %02u, WRC: %02u, RD: %01u, CRC: 0x%02X (%s)",
265 segmentNum,
266 data_buf[i] ^ crc,
267 data_buf[i+1] ^ crc,
268 data_buf[i+2] ^ crc,
269 data_buf[i+3] ^ crc,
270 segment_len,
271 segment_flag,
272 (segment_flag & 0x4) >> 2,
273 (segment_flag & 0x8) >> 3,
274 wrp,
275 wrc,
276 ((data_buf[i+3]^crc) & 0x80) >> 7,
277 segCRC,
278 ( segCRC == segCalcCRC ) ? "OK" : "fail"
279 );
280
281 i += 5;
282
283 if ( hasWRC ) {
284 PrintAndLog("WRC protected area: (I %d | K %d| WRC %d)", i, k, wrc);
285 PrintAndLog("\nrow | data");
286 PrintAndLog("-----+------------------------------------------------");
287
288 for ( k=i; k < (i + wrc); ++k)
289 data_buf[k] ^= crc;
290
291 print_hex_break( data_buf+i, wrc, 16);
292
293 i += wrc;
294 }
295
296 if ( hasWRP ) {
297 PrintAndLog("Remaining write protected area: (I %d | K %d | WRC %d | WRP %d WRP_LEN %d)",i, k, wrc, wrp, wrp_len);
298 PrintAndLog("\nrow | data");
299 PrintAndLog("-----+------------------------------------------------");
300
301 for (k=i; k < (i+wrp_len); ++k)
302 data_buf[k] ^= crc;
303
304 print_hex_break( data_buf+i, wrp_len, 16);
305
306 i += wrp_len;
307
308 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
309 if( wrp_len == 8 )
310 PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4]^crc, data_buf[i-3]^crc, data_buf[i-2]^crc);
311 }
312
313 PrintAndLog("Remaining segment payload: (I %d | K %d | Remain LEN %d)", i, k, remain_seg_payload_len);
314 PrintAndLog("\nrow | data");
315 PrintAndLog("-----+------------------------------------------------");
316
317 for ( k=i; k < (i+remain_seg_payload_len); ++k)
318 data_buf[k] ^= crc;
319
320 print_hex_break( data_buf+i, remain_seg_payload_len, 16);
321
322 i += remain_seg_payload_len;
323
324 PrintAndLog("-----+------------------------------------------------\n");
325
326 // end with last segment
327 if (segment_flag & 0x8) return 0;
328
329 } // end for loop
330
331 } else {
332
333 // Data start point on unsegmented cards
334 i = 8;
335
336 wrp = data_buf[7] & 0x0F;
337 wrc = (data_buf[7] & 0x70) >> 4;
338
339 bool hasWRC = (wrc > 0);
340 bool hasWRP = (wrp > wrc);
341 int wrp_len = (wrp - wrc);
342 int remain_seg_payload_len = (1024 - 22 - wrp); // Any chance to get physical card size here!?
343
344 PrintAndLog("Unsegmented card - WRP: %02u, WRC: %02u, RD: %01u",
345 wrp,
346 wrc,
347 (data_buf[7] & 0x80) >> 7
348 );
349
350 if ( hasWRC ) {
351 PrintAndLog("WRC protected area: (I %d | WRC %d)", i, wrc);
352 PrintAndLog("\nrow | data");
353 PrintAndLog("-----+------------------------------------------------");
354 print_hex_break( data_buf+i, wrc, 16);
355 i += wrc;
356 }
357
358 if ( hasWRP ) {
359 PrintAndLog("Remaining write protected area: (I %d | WRC %d | WRP %d | WRP_LEN %d)", i, wrc, wrp, wrp_len);
360 PrintAndLog("\nrow | data");
361 PrintAndLog("-----+------------------------------------------------");
362 print_hex_break( data_buf + i, wrp_len, 16);
363 i += wrp_len;
364
365 // does this one work? (Answer: Only if KGH/BGH is used with BCD encoded card number! So maybe this will show just garbage...)
366 if( wrp_len == 8 )
367 PrintAndLog("Card ID: %2X%02X%02X", data_buf[i-4], data_buf[i-3], data_buf[i-2]);
368 }
369
370 PrintAndLog("Remaining segment payload: (I %d | Remain LEN %d)", i, remain_seg_payload_len);
371 PrintAndLog("\nrow | data");
372 PrintAndLog("-----+------------------------------------------------");
373 print_hex_break( data_buf + i, remain_seg_payload_len, 16);
374 i += remain_seg_payload_len;
375
376 PrintAndLog("-----+------------------------------------------------\n");
377 }
378 }
379 return 0;
380 }
381
382 int CmdLegicRFRead(const char *Cmd) {
383
384 // params:
385 // offset in data
386 // number of bytes.
387 char cmdp = param_getchar(Cmd, 0);
388 if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_read();
389
390 uint32_t offset = 0, len = 0, IV = 0;
391 sscanf(Cmd, "%x %x %x", &offset, &len, &IV);
392
393 // OUT-OF-BOUNDS check
394 if(len + offset > MAX_LENGTH) len = MAX_LENGTH - offset;
395
396 IV &= 0x7F;
397 PrintAndLog("Current IV: 0x%02x", IV);
398
399 UsbCommand c= {CMD_READER_LEGIC_RF, {offset, len, IV}};
400 clearCommandBuffer();
401 SendCommand(&c);
402 return 0;
403 }
404
405 int CmdLegicLoad(const char *Cmd) {
406
407 // iceman: potential bug, where all filepaths or filename which starts with H or h will print the helptext :)
408 char cmdp = param_getchar(Cmd, 0);
409 if ( cmdp == 'H' || cmdp == 'h' || cmdp == 0x00) return usage_legic_load();
410
411 char filename[FILE_PATH_SIZE] = {0x00};
412 int len = strlen(Cmd);
413
414 if (len > FILE_PATH_SIZE) {
415 PrintAndLog("Filepath too long (was %s bytes), max allowed is %s ", len, FILE_PATH_SIZE);
416 return 0;
417 }
418 memcpy(filename, Cmd, len);
419
420 FILE *f = fopen(filename, "r");
421 if(!f) {
422 PrintAndLog("couldn't open '%s'", Cmd);
423 return -1;
424 }
425
426 char line[80];
427 int offset = 0;
428 uint8_t data[USB_CMD_DATA_SIZE] = {0x00};
429 int index = 0;
430 int totalbytes = 0;
431 while ( fgets(line, sizeof(line), f) ) {
432 int res = sscanf(line, "%x %x %x %x %x %x %x %x",
433 (unsigned int *)&data[index],
434 (unsigned int *)&data[index + 1],
435 (unsigned int *)&data[index + 2],
436 (unsigned int *)&data[index + 3],
437 (unsigned int *)&data[index + 4],
438 (unsigned int *)&data[index + 5],
439 (unsigned int *)&data[index + 6],
440 (unsigned int *)&data[index + 7]);
441
442 if(res != 8) {
443 PrintAndLog("Error: could not read samples");
444 fclose(f);
445 return -1;
446 }
447 index += res;
448
449 if ( index == USB_CMD_DATA_SIZE ){
450 // PrintAndLog("sent %d | %d | %d", index, offset, totalbytes);
451 UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
452 memcpy(c.d.asBytes, data, sizeof(data));
453 clearCommandBuffer();
454 SendCommand(&c);
455 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
456 PrintAndLog("Command execute timeout");
457 fclose(f);
458 return 1;
459 }
460 offset += index;
461 totalbytes += index;
462 index = 0;
463 }
464 }
465 fclose(f);
466
467 // left over bytes?
468 if ( index != 0 ) {
469 UsbCommand c = { CMD_DOWNLOADED_SIM_SAMPLES_125K, {offset, 0, 0}};
470 memcpy(c.d.asBytes, data, 8);
471 clearCommandBuffer();
472 SendCommand(&c);
473 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 1500)){
474 PrintAndLog("Command execute timeout");
475 return 1;
476 }
477 totalbytes += index;
478 }
479
480 PrintAndLog("loaded %u samples", totalbytes);
481 return 0;
482 }
483
484 int CmdLegicSave(const char *Cmd) {
485 int requested = 1024;
486 int offset = 0;
487 int delivered = 0;
488 char filename[FILE_PATH_SIZE] = {0x00};
489 uint8_t got[1024] = {0x00};
490
491 memset(filename, 0, FILE_PATH_SIZE);
492
493 sscanf(Cmd, " %s %i %i", filename, &requested, &offset);
494
495 /* If no length given save entire legic read buffer */
496 /* round up to nearest 8 bytes so the saved data can be used with legicload */
497 if (requested == 0)
498 requested = 1024;
499
500 if (requested % 8 != 0) {
501 int remainder = requested % 8;
502 requested = requested + 8 - remainder;
503 }
504
505 if (offset + requested > sizeof(got)) {
506 PrintAndLog("Tried to read past end of buffer, <bytes> + <offset> > 1024");
507 return 0;
508 }
509
510 GetFromBigBuf(got, requested, offset);
511 if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2000)){
512 PrintAndLog("Command execute timeout");
513 return 1;
514 }
515
516 FILE *f = fopen(filename, "w");
517 if(!f) {
518 PrintAndLog("couldn't open '%s'", Cmd+1);
519 return -1;
520 }
521
522 for (int j = 0; j < requested; j += 8) {
523 fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
524 got[j+0], got[j+1], got[j+2], got[j+3],
525 got[j+4], got[j+5], got[j+6], got[j+7]
526 );
527 delivered += 8;
528 if (delivered >= requested) break;
529 }
530
531 fclose(f);
532 PrintAndLog("saved %u samples", delivered);
533 return 0;
534 }
535
536 //TODO: write a help text (iceman)
537 int CmdLegicRfSim(const char *Cmd) {
538 UsbCommand c = {CMD_SIMULATE_TAG_LEGIC_RF, {6,3,0}};
539 sscanf(Cmd, " %"lli" %"lli" %"lli, &c.arg[0], &c.arg[1], &c.arg[2]);
540 clearCommandBuffer();
541 SendCommand(&c);
542 return 0;
543 }
544
545 int CmdLegicRfWrite(const char *Cmd) {
546
547 // params:
548 // offset - in tag memory
549 // length - num of bytes to be written
550 // IV - initialisation vector
551
552 char cmdp = param_getchar(Cmd, 0);
553 if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_write();
554
555 uint32_t offset = 0, len = 0, IV = SESSION_IV;
556
557 UsbCommand c = {CMD_WRITER_LEGIC_RF, {0,0,0}};
558 int res = sscanf(Cmd, "%x %x %x", &offset, &len, &IV);
559 if(res < 2) {
560 PrintAndLog("Please specify the offset and length as two hex strings and, optionally, the IV also as an hex string");
561 return -1;
562 }
563
564 // OUT-OF-BOUNDS check
565 if(len + offset > MAX_LENGTH) len = MAX_LENGTH - offset;
566
567
568 IV &= 0x7F;
569 PrintAndLog("Current IV: 0x%02x", IV);
570
571 c.arg[0] = offset;
572 c.arg[1] = len;
573 c.arg[2] = IV;
574
575 clearCommandBuffer();
576 SendCommand(&c);
577 return 0;
578 }
579
580 int CmdLegicRfRawWrite(const char *Cmd) {
581
582 char cmdp = param_getchar(Cmd, 0);
583 if ( cmdp == 'H' || cmdp == 'h' ) return usage_legic_rawwrite();
584
585 uint32_t address = 0, data = 0, IV = SESSION_IV;
586 char answer;
587
588 UsbCommand c = { CMD_RAW_WRITER_LEGIC_RF, {0,0,0} };
589 int res = sscanf(Cmd, "%x %x %x", &address, &data, &IV);
590 if(res < 2)
591 return usage_legic_rawwrite();
592
593 // OUT-OF-BOUNDS check
594 if(address > MAX_LENGTH)
595 return usage_legic_rawwrite();
596
597 IV &= 0x7F;
598 PrintAndLog("Current IV: 0x%02x", IV);
599
600 c.arg[0] = address;
601 c.arg[1] = data;
602 c.arg[2] = IV;
603
604 if (c.arg[0] == 0x05 || c.arg[0] == 0x06) {
605 PrintAndLog("############# DANGER !! #############");
606 PrintAndLog("# changing the DCF is irreversible #");
607 PrintAndLog("#####################################");
608 PrintAndLog("do youe really want to continue? y(es) n(o)");
609 if (scanf(" %c", &answer) > 0 && (answer == 'y' || answer == 'Y')) {
610 SendCommand(&c);
611 return 0;
612 }
613 return -1;
614 }
615
616 clearCommandBuffer();
617 SendCommand(&c);
618 return 0;
619 }
620
621 //TODO: write a help text (iceman)
622 int CmdLegicRfFill(const char *Cmd) {
623 UsbCommand cmd = {CMD_WRITER_LEGIC_RF, {0,0,0} };
624 int res = sscanf(Cmd, " 0x%"llx" 0x%"llx" 0x%"llx, &cmd.arg[0], &cmd.arg[1], &cmd.arg[2]);
625 if(res != 3) {
626 PrintAndLog("Please specify the offset, length and value as two hex strings");
627 return -1;
628 }
629
630 int i;
631 UsbCommand c = {CMD_DOWNLOADED_SIM_SAMPLES_125K, {0, 0, 0}};
632 memset(c.d.asBytes, cmd.arg[2], 48);
633
634 for(i = 0; i < 22; i++) {
635 c.arg[0] = i*48;
636
637 clearCommandBuffer();
638 SendCommand(&c);
639 WaitForResponse(CMD_ACK, NULL);
640 }
641 clearCommandBuffer();
642 SendCommand(&cmd);
643 return 0;
644 }
645
646 int CmdLegicCalcCrc8(const char *Cmd){
647
648 uint8_t *data = NULL;
649 uint8_t cmdp = 0, uidcrc = 0, type=0;
650 bool errors = false;
651 int len = 0;
652 int bg, en;
653
654 while(param_getchar(Cmd, cmdp) != 0x00) {
655 switch(param_getchar(Cmd, cmdp)) {
656 case 'b':
657 case 'B':
658 // peek at length of the input string so we can
659 // figure out how many elements to malloc in "data"
660 bg=en=0;
661 if (param_getptr(Cmd, &bg, &en, cmdp+1)) {
662 errors = true;
663 break;
664 }
665 len = (en - bg + 1);
666
667 // check that user entered even number of characters
668 // for hex data string
669 if (len & 1) {
670 errors = true;
671 break;
672 }
673
674 // it's possible for user to accidentally enter "b" parameter
675 // more than once - we have to clean previous malloc
676 if (data) free(data);
677 data = malloc(len >> 1);
678 if ( data == NULL ) {
679 PrintAndLog("Can't allocate memory. exiting");
680 errors = true;
681 break;
682 }
683
684 if (param_gethex(Cmd, cmdp+1, data, len)) {
685 errors = true;
686 break;
687 }
688
689 len >>= 1;
690 cmdp += 2;
691 break;
692 case 'u':
693 case 'U':
694 uidcrc = param_get8ex(Cmd, cmdp+1, 0, 16);
695 cmdp += 2;
696 break;
697 case 'c':
698 case 'C':
699 type = param_get8ex(Cmd, cmdp+1, 0, 10);
700 cmdp += 2;
701 break;
702 case 'h':
703 case 'H':
704 errors = true;
705 break;
706 default:
707 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
708 errors = true;
709 break;
710 }
711 if (errors) break;
712 }
713 //Validations
714 if (errors){
715 if (data) free(data);
716 return usage_legic_calccrc8();
717 }
718
719 switch (type){
720 case 16:
721 PrintAndLog("LEGIC CRC16: %X", CRC16Legic(data, len, uidcrc));
722 break;
723 default:
724 PrintAndLog("LEGIC CRC8: %X", CRC8Legic(data, len) );
725 break;
726 }
727
728 if (data) free(data);
729 return 0;
730 }
731
732 static command_t CommandTable[] = {
733 {"help", CmdHelp, 1, "This help"},
734 {"decode", CmdLegicDecode, 0, "Display deobfuscated and decoded LEGIC RF tag data (use after hf legic reader)"},
735 {"read", CmdLegicRFRead, 0, "[offset][length] <iv> -- read bytes from a LEGIC card"},
736 {"save", CmdLegicSave, 0, "<filename> [<length>] -- Store samples"},
737 {"load", CmdLegicLoad, 0, "<filename> -- Restore samples"},
738 {"sim", CmdLegicRfSim, 0, "[phase drift [frame drift [req/resp drift]]] Start tag simulator (use after load or read)"},
739 {"write", CmdLegicRfWrite,0, "<offset> <length> <iv> -- Write sample buffer (user after load or read)"},
740 {"writeraw",CmdLegicRfRawWrite, 0, "<address> <value> <iv> -- Write direct to address"},
741 {"fill", CmdLegicRfFill, 0, "<offset> <length> <value> -- Fill/Write tag with constant value"},
742 {"crc8", CmdLegicCalcCrc8, 1, "Calculate Legic CRC8 over given hexbytes"},
743 {NULL, NULL, 0, NULL}
744 };
745
746 int CmdHFLegic(const char *Cmd) {
747 clearCommandBuffer();
748 CmdsParse(CommandTable, Cmd);
749 return 0;
750 }
751
752 int CmdHelp(const char *Cmd) {
753 CmdsHelp(CommandTable);
754 return 0;
755 }
Impressum, Datenschutz