Added info to changelog about bootroom update
[legacy-proxmark3.git] / client / cmdhf.c
blob22063bbbe566e22e139524ad98ba2cc817c9b641
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 commands
9 //-----------------------------------------------------------------------------
11 #include <stdio.h>
12 #include <string.h>
13 #include "proxmark3.h"
14 #include "graph.h"
15 #include "ui.h"
16 #include "cmdparser.h"
17 #include "cmdhf.h"
18 #include "cmdhf14a.h"
19 #include "cmdhf14b.h"
20 #include "cmdhf15.h"
21 #include "cmdhfepa.h"
22 #include "cmdhflegic.h"
23 #include "cmdhficlass.h"
24 #include "cmdhfmf.h"
25 #include "cmdhfmfu.h"
26 #include "protocols.h"
28 static int CmdHelp(const char *Cmd);
30 int CmdHFTune(const char *Cmd)
32 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF};
33 SendCommand(&c);
34 return 0;
38 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
40 switch(cmd[0])
42 case ISO14443A_CMD_WUPA: snprintf(exp,size,"WUPA"); break;
43 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
44 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
45 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
46 if(cmd[1] == 0x70)
48 snprintf(exp,size,"SELECT_UID"); break;
49 }else
51 snprintf(exp,size,"ANTICOLL"); break;
54 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
55 //95 20 = Anticollision of cascade level2
56 //95 70 = Select of cascade level2
57 if(cmd[2] == 0x70)
59 snprintf(exp,size,"SELECT_UID-2"); break;
60 }else
62 snprintf(exp,size,"ANTICOLL-2"); break;
65 case ISO14443A_CMD_REQA: snprintf(exp,size,"REQA"); break;
66 case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
67 case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
68 case ISO14443A_CMD_HALT: snprintf(exp,size,"HALT"); break;
69 case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break;
70 case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break;
71 case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break;
72 case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
73 case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
74 case MIFARE_AUTH_KEYA: snprintf(exp,size,"AUTH-A(%d)",cmd[1]); break;
75 case MIFARE_AUTH_KEYB: snprintf(exp,size,"AUTH-B(%d)",cmd[1]); break;
76 case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break;
77 case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break;
78 case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break;
79 default: snprintf(exp,size,"?"); break;
81 return;
84 void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
86 switch(cmd[0])
88 case ICLASS_CMD_ACTALL: snprintf(exp,size,"ACTALL"); break;
89 case ICLASS_CMD_READ_OR_IDENTIFY:{
90 if(cmdsize > 1){
91 snprintf(exp,size,"READ(%d)",cmd[1]);
92 }else{
93 snprintf(exp,size,"IDENTIFY");
95 break;
97 case ICLASS_CMD_SELECT: snprintf(exp,size,"SELECT"); break;
98 case ICLASS_CMD_PAGESEL: snprintf(exp,size,"PAGESEL(%d)", cmd[1]); break;
99 case ICLASS_CMD_READCHECK_KC:snprintf(exp,size,"READCHECK[Kc](%d)", cmd[1]); break;
100 case ICLASS_CMD_READCHECK_KD:snprintf(exp,size,"READCHECK[Kd](%d)", cmd[1]); break;
101 case ICLASS_CMD_CHECK: snprintf(exp,size,"CHECK"); break;
102 case ICLASS_CMD_DETECT: snprintf(exp,size,"DETECT"); break;
103 case ICLASS_CMD_HALT: snprintf(exp,size,"HALT"); break;
104 case ICLASS_CMD_UPDATE: snprintf(exp,size,"UPDATE(%d)",cmd[1]); break;
105 case ICLASS_CMD_ACT: snprintf(exp,size,"ACT"); break;
106 case ICLASS_CMD_READ4: snprintf(exp,size,"READ4(%d)",cmd[1]); break;
107 default: snprintf(exp,size,"?"); break;
109 return;
112 void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
115 if(cmd[0] == 0x26)
117 switch(cmd[1]){
118 case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");break;
119 case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");break;
120 default: snprintf(exp,size,"?"); break;
123 }else if(cmd[0] == 0x02)
125 switch(cmd[1])
127 case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");break;
128 case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");break;
129 case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");break;
130 case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");break;
131 case ISO15693_SELECT :snprintf(exp, size, "SELECT");break;
132 case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");break;
133 case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");break;
134 case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");break;
135 case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");break;
136 case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");break;
137 case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");break;
138 case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");break;
139 default: snprintf(exp,size,"?"); break;
145 06 00 = INITIATE
146 0E xx = SELECT ID (xx = Chip-ID)
147 0B = Get UID
148 08 yy = Read Block (yy = block number)
149 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
150 0C = Reset to Inventory
151 0F = Completion
152 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
155 void annotateIso14443b(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
157 switch(cmd[0]){
158 case ISO14443B_REQB : snprintf(exp,size,"REQB");break;
159 case ISO14443B_ATTRIB : snprintf(exp,size,"ATTRIB");break;
160 case ISO14443B_HALT : snprintf(exp,size,"HALT");break;
161 case ISO14443B_INITIATE : snprintf(exp,size,"INITIATE");break;
162 case ISO14443B_SELECT : snprintf(exp,size,"SELECT(%d)",cmd[1]);break;
163 case ISO14443B_GET_UID : snprintf(exp,size,"GET UID");break;
164 case ISO14443B_READ_BLK : snprintf(exp,size,"READ_BLK(%d)", cmd[1]);break;
165 case ISO14443B_WRITE_BLK : snprintf(exp,size,"WRITE_BLK(%d)",cmd[1]);break;
166 case ISO14443B_RESET : snprintf(exp,size,"RESET");break;
167 case ISO14443B_COMPLETION : snprintf(exp,size,"COMPLETION");break;
168 case ISO14443B_AUTHENTICATE : snprintf(exp,size,"AUTHENTICATE");break;
169 default : snprintf(exp,size ,"?");break;
175 * @brief iso14443B_CRC_Ok Checks CRC in command or response
176 * @param isResponse
177 * @param data
178 * @param len
179 * @return 0 : CRC-command, CRC not ok
180 * 1 : CRC-command, CRC ok
181 * 2 : Not crc-command
184 uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
186 uint8_t b1,b2;
188 if(len <= 2) return 2;
190 ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2);
191 if(b1 != data[len-2] || b2 != data[len-1]) {
192 return 0;
194 return 1;
198 * @brief iclass_CRC_Ok Checks CRC in command or response
199 * @param isResponse
200 * @param data
201 * @param len
202 * @return 0 : CRC-command, CRC not ok
203 * 1 : CRC-command, CRC ok
204 * 2 : Not crc-command
206 uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
208 if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes
210 uint8_t b1, b2;
212 if(!isResponse)//Commands to tag
215 These commands should have CRC. Total length leftmost
216 4 READ
217 4 READ4
218 12 UPDATE - unsecured, ends with CRC16
219 14 UPDATE - secured, ends with signature instead
220 4 PAGESEL
222 if(len == 4 || len == 12)//Covers three of them
224 //Don't include the command byte
225 ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2);
226 return b1 == data[len -2] && b2 == data[len-1];
228 return 2;
229 }else{
231 These tag responses should have CRC. Total length leftmost
233 10 READ data[8] crc[2]
234 34 READ4 data[32]crc[2]
235 10 UPDATE data[8] crc[2]
236 10 SELECT csn[8] crc[2]
237 10 IDENTIFY asnb[8] crc[2]
238 10 PAGESEL block1[8] crc[2]
239 10 DETECT csn[8] crc[2]
241 These should not
243 4 CHECK chip_response[4]
244 8 READCHECK data[8]
245 1 ACTALL sof[1]
246 1 ACT sof[1]
248 In conclusion, without looking at the command; any response
249 of length 10 or 34 should have CRC
251 if(len != 10 && len != 34) return true;
253 ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2);
254 return b1 == data[len -2] && b2 == data[len-1];
258 uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles)
260 bool isResponse;
261 uint16_t duration, data_len, parity_len;
263 uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
264 char explanation[30] = {0};
266 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
268 first_timestamp = *((uint32_t *)(trace));
269 timestamp = *((uint32_t *)(trace + tracepos));
271 tracepos += 4;
272 duration = *((uint16_t *)(trace + tracepos));
273 tracepos += 2;
274 data_len = *((uint16_t *)(trace + tracepos));
275 tracepos += 2;
277 if (data_len & 0x8000) {
278 data_len &= 0x7fff;
279 isResponse = true;
280 } else {
281 isResponse = false;
283 parity_len = (data_len-1)/8 + 1;
285 if (tracepos + data_len + parity_len > traceLen) {
286 return traceLen;
288 uint8_t *frame = trace + tracepos;
289 tracepos += data_len;
290 uint8_t *parityBytes = trace + tracepos;
291 tracepos += parity_len;
293 //Check the CRC status
294 uint8_t crcStatus = 2;
296 if (data_len > 2) {
297 uint8_t b1, b2;
298 if(protocol == ICLASS)
300 crcStatus = iclass_CRC_check(isResponse, frame, data_len);
302 }else if (protocol == ISO_14443B)
304 crcStatus = iso14443B_CRC_check(isResponse, frame, data_len);
306 else if (protocol == ISO_14443A){//Iso 14443a
308 ComputeCrc14443(CRC_14443_A, frame, data_len-2, &b1, &b2);
310 if (b1 != frame[data_len-2] || b2 != frame[data_len-1]) {
311 if(!(isResponse & (data_len < 6)))
313 crcStatus = 0;
318 //0 CRC-command, CRC not ok
319 //1 CRC-command, CRC ok
320 //2 Not crc-command
322 //--- Draw the data column
323 //char line[16][110];
324 char line[16][110];
326 for (int j = 0; j < data_len && j/16 < 16; j++) {
328 int oddparity = 0x01;
329 int k;
331 for (k=0 ; k<8 ; k++) {
332 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
334 uint8_t parityBits = parityBytes[j>>3];
335 if (isResponse && (oddparity != ((parityBits >> (7-(j&0x0007))) & 0x01))) {
336 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x! ", frame[j]);
338 } else {
339 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x ", frame[j]);
343 if(crcStatus == 1)
344 {//CRC-command
345 char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4)-1;
346 (*pos1) = '[';
347 char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4)-2;
348 (*pos2) = ']';
350 if(data_len == 0)
352 if(data_len == 0){
353 sprintf(line[0],"<empty trace - possible error>");
356 //--- Draw the CRC column
358 char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " "));
360 EndOfTransmissionTimestamp = timestamp + duration;
362 if(!isResponse)
364 if(protocol == ICLASS)
365 annotateIclass(explanation,sizeof(explanation),frame,data_len);
366 else if (protocol == ISO_14443A)
367 annotateIso14443a(explanation,sizeof(explanation),frame,data_len);
368 else if(protocol == ISO_14443B)
369 annotateIso14443b(explanation,sizeof(explanation),frame,data_len);
372 int num_lines = MIN((data_len - 1)/16 + 1, 16);
373 for (int j = 0; j < num_lines ; j++) {
374 if (j == 0) {
375 PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s",
376 (timestamp - first_timestamp),
377 (EndOfTransmissionTimestamp - first_timestamp),
378 (isResponse ? "Tag" : "Rdr"),
379 line[j],
380 (j == num_lines-1) ? crc : " ",
381 (j == num_lines-1) ? explanation : "");
382 } else {
383 PrintAndLog(" | | | %-64s| %s| %s",
384 line[j],
385 (j == num_lines-1)?crc:" ",
386 (j == num_lines-1) ? explanation : "");
390 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
392 bool next_isResponse = *((uint16_t *)(trace + tracepos + 6)) & 0x8000;
394 if (showWaitCycles && !isResponse && next_isResponse) {
395 uint32_t next_timestamp = *((uint32_t *)(trace + tracepos));
396 if (next_timestamp != 0x44444444) {
397 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
398 (EndOfTransmissionTimestamp - first_timestamp),
399 (next_timestamp - first_timestamp),
400 " ",
401 (next_timestamp - EndOfTransmissionTimestamp));
405 return tracepos;
409 int CmdHFList(const char *Cmd)
411 bool showWaitCycles = false;
412 char type[40] = {0};
413 int tlen = param_getstr(Cmd,0,type);
414 char param = param_getchar(Cmd, 1);
415 bool errors = false;
416 uint8_t protocol = 0;
417 //Validate params
418 if(tlen == 0)
420 errors = true;
422 if(param == 'h' || (param !=0 && param != 'f'))
424 errors = true;
426 if(!errors)
428 if(strcmp(type, "iclass") == 0)
430 protocol = ICLASS;
431 }else if(strcmp(type, "14a") == 0)
433 protocol = ISO_14443A;
435 else if(strcmp(type, "14b") == 0)
437 protocol = ISO_14443B;
438 }else if(strcmp(type,"raw")== 0)
440 protocol = -1;//No crc, no annotations
441 }else{
442 errors = true;
446 if (errors) {
447 PrintAndLog("List protocol data in trace buffer.");
448 PrintAndLog("Usage: hf list <protocol> [f]");
449 PrintAndLog(" f - show frame delay times as well");
450 PrintAndLog("Supported <protocol> values:");
451 PrintAndLog(" raw - just show raw data without annotations");
452 PrintAndLog(" 14a - interpret data as iso14443a communications");
453 PrintAndLog(" 14b - interpret data as iso14443b communications");
454 PrintAndLog(" iclass - interpret data as iclass communications");
455 PrintAndLog("");
456 PrintAndLog("example: hf list 14a f");
457 PrintAndLog("example: hf list iclass");
458 return 0;
462 if (param == 'f') {
463 showWaitCycles = true;
467 uint8_t *trace;
468 uint16_t tracepos = 0;
469 trace = malloc(USB_CMD_DATA_SIZE);
471 // Query for the size of the trace
472 UsbCommand response;
473 GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0);
474 WaitForResponse(CMD_ACK, &response);
475 uint16_t traceLen = response.arg[2];
476 if (traceLen > USB_CMD_DATA_SIZE) {
477 uint8_t *p = realloc(trace, traceLen);
478 if (p == NULL) {
479 PrintAndLog("Cannot allocate memory for trace");
480 free(trace);
481 return 2;
483 trace = p;
484 GetFromBigBuf(trace, traceLen, 0);
485 WaitForResponse(CMD_ACK, NULL);
488 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen);
489 PrintAndLog("");
490 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
491 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
492 PrintAndLog("iClass - Timings are not as accurate");
493 PrintAndLog("");
494 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
495 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
497 while(tracepos < traceLen)
499 tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles);
502 free(trace);
503 return 0;
507 static command_t CommandTable[] =
509 {"help", CmdHelp, 1, "This help"},
510 {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"},
511 {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"},
512 {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"},
513 {"epa", CmdHFEPA, 1, "{ German Identification Card... }"},
514 {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"},
515 {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"},
516 {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"},
517 {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"},
518 {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"},
519 {"list", CmdHFList, 1, "List protocol data in trace buffer"},
520 {NULL, NULL, 0, NULL}
523 int CmdHF(const char *Cmd)
525 CmdsParse(CommandTable, Cmd);
526 return 0;
529 int CmdHelp(const char *Cmd)
531 CmdsHelp(CommandTable);
532 return 0;