]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf.c
fix: broken edge detector implementation in hi_iso14443a.v resulted in decreased...
[proxmark3-svn] / client / cmdhf.c
CommitLineData
a553f267 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//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
4c3de57a 12#include <string.h>
902cb3c0 13#include "proxmark3.h"
7fe9b0b7 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"
5acd09bd 21#include "cmdhfepa.h"
7fe9b0b7 22#include "cmdhflegic.h"
cee5a30d 23#include "cmdhficlass.h"
9ca155ba 24#include "cmdhfmf.h"
5ee70129 25#include "cmdhfmfu.h"
b67f7ec3 26#include "protocols.h"
7fe9b0b7 27
28static int CmdHelp(const char *Cmd);
29
30int CmdHFTune(const char *Cmd)
31{
32 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF};
33 SendCommand(&c);
34 return 0;
35}
4c3de57a 36
4c3de57a
MHS
37
38void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
39{
40 switch(cmd[0])
41 {
41fdd0f0
MHS
42 case ISO14443A_CMD_WUPA: snprintf(exp,size,"WUPA"); break;
43 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
4df54240
MHS
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)
383608a6 46 if(cmd[1] == 0x70)
4c3de57a
MHS
47 {
48 snprintf(exp,size,"SELECT_UID"); break;
49 }else
50 {
4df54240
MHS
51 snprintf(exp,size,"ANTICOLL"); break;
52 }
53 }
41fdd0f0 54 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
4df54240
MHS
55 //95 20 = Anticollision of cascade level2
56 //95 70 = Select of cascade level2
57 if(cmd[2] == 0x70)
58 {
59 snprintf(exp,size,"SELECT_UID-2"); break;
60 }else
61 {
62 snprintf(exp,size,"ANTICOLL-2"); break;
4c3de57a
MHS
63 }
64 }
41fdd0f0
MHS
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;
4df54240
MHS
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;
383608a6
MHS
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;
4df54240
MHS
76 case MIFARE_MAGICMODE: snprintf(exp,size,"MAGIC"); break;
77 default: snprintf(exp,size,"?"); break;
4c3de57a
MHS
78 }
79 return;
80}
81
82void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
83{
4c3de57a
MHS
84 switch(cmd[0])
85 {
337818f7 86 case ICLASS_CMD_ACTALL: snprintf(exp,size,"ACTALL"); break;
4df54240
MHS
87 case ICLASS_CMD_READ_OR_IDENTIFY:{
88 if(cmdsize > 1){
89 snprintf(exp,size,"READ(%d)",cmd[1]);
90 }else{
91 snprintf(exp,size,"IDENTIFY");
92 }
93 break;
94 }
337818f7 95 case ICLASS_CMD_SELECT: snprintf(exp,size,"SELECT"); break;
49726b40
MHS
96 case ICLASS_CMD_PAGESEL: snprintf(exp,size,"PAGESEL(%d)", cmd[1]); break;
97 case ICLASS_CMD_READCHECK_KC:snprintf(exp,size,"READCHECK[Kc](%d)", cmd[1]); break;
98 case ICLASS_CMD_READCHECK_KD:snprintf(exp,size,"READCHECK[Kd](%d)", cmd[1]); break;
337818f7 99 case ICLASS_CMD_CHECK: snprintf(exp,size,"CHECK"); break;
49726b40 100 case ICLASS_CMD_DETECT: snprintf(exp,size,"DETECT"); break;
337818f7 101 case ICLASS_CMD_HALT: snprintf(exp,size,"HALT"); break;
49726b40
MHS
102 case ICLASS_CMD_UPDATE: snprintf(exp,size,"UPDATE(%d)",cmd[1]); break;
103 case ICLASS_CMD_ACT: snprintf(exp,size,"ACT"); break;
104 case ICLASS_CMD_READ4: snprintf(exp,size,"READ4(%d)",cmd[1]); break;
337818f7 105 default: snprintf(exp,size,"?"); break;
4c3de57a
MHS
106 }
107 return;
108}
109
4df54240
MHS
110void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
111{
112
113 if(cmd[0] == 0x26)
114 {
115 switch(cmd[1]){
116 case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");break;
117 case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");break;
118 default: snprintf(exp,size,"?"); break;
4c3de57a 119
4df54240
MHS
120 }
121 }else if(cmd[0] == 0x02)
122 {
123 switch(cmd[1])
124 {
125 case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");break;
126 case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");break;
127 case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");break;
128 case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");break;
129 case ISO15693_SELECT :snprintf(exp, size, "SELECT");break;
130 case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");break;
131 case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");break;
132 case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");break;
133 case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");break;
134 case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");break;
135 case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");break;
136 case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");break;
41fdd0f0 137 default: snprintf(exp,size,"?"); break;
4df54240
MHS
138 }
139 }
140}
08e8317c
MHS
141
142/**
14306 00 = INITIATE
1440E xx = SELECT ID (xx = Chip-ID)
1450B = Get UID
14608 yy = Read Block (yy = block number)
14709 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
1480C = Reset to Inventory
1490F = Completion
1500A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
151**/
152
41fdd0f0
MHS
153void annotateIso14443b(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
154{
155 switch(cmd[0]){
156 case ISO14443B_REQB : snprintf(exp,size,"REQB");break;
157 case ISO14443B_ATTRIB : snprintf(exp,size,"ATTRIB");break;
158 case ISO14443B_HALT : snprintf(exp,size,"HALT");break;
08e8317c
MHS
159 case ISO14443B_INITIATE : snprintf(exp,size,"INITIATE");break;
160 case ISO14443B_SELECT : snprintf(exp,size,"SELECT(%d)",cmd[1]);break;
161 case ISO14443B_GET_UID : snprintf(exp,size,"GET UID");break;
162 case ISO14443B_READ_BLK : snprintf(exp,size,"READ_BLK(%d)", cmd[1]);break;
163 case ISO14443B_WRITE_BLK : snprintf(exp,size,"WRITE_BLK(%d)",cmd[1]);break;
164 case ISO14443B_RESET : snprintf(exp,size,"RESET");break;
165 case ISO14443B_COMPLETION : snprintf(exp,size,"COMPLETION");break;
166 case ISO14443B_AUTHENTICATE : snprintf(exp,size,"AUTHENTICATE");break;
167 default : snprintf(exp,size ,"?");break;
41fdd0f0
MHS
168 }
169
170}
171
172/**
173 * @brief iso14443B_CRC_Ok Checks CRC in command or response
174 * @param isResponse
175 * @param data
176 * @param len
177 * @return 0 : CRC-command, CRC not ok
178 * 1 : CRC-command, CRC ok
179 * 2 : Not crc-command
180 */
181
182uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
183{
184 uint8_t b1,b2;
185
186 if(len <= 2) return 2;
187
188 ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2);
189 if(b1 != data[len-2] || b2 != data[len-1]) {
190 return 0;
191 }
192 return 1;
193}
194
49726b40
MHS
195/**
196 * @brief iclass_CRC_Ok Checks CRC in command or response
197 * @param isResponse
198 * @param data
199 * @param len
200 * @return 0 : CRC-command, CRC not ok
201 * 1 : CRC-command, CRC ok
202 * 2 : Not crc-command
203 */
41fdd0f0 204uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
49726b40
MHS
205{
206 if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes
207
208 uint8_t b1, b2;
209
210 if(!isResponse)//Commands to tag
211 {
212 /**
213 These commands should have CRC. Total length leftmost
214 4 READ
215 4 READ4
216 12 UPDATE - unsecured, ends with CRC16
217 14 UPDATE - secured, ends with signature instead
218 4 PAGESEL
219 **/
220 if(len == 4 || len == 12)//Covers three of them
221 {
222 //Don't include the command byte
223 ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2);
224 return b1 == data[len -2] && b2 == data[len-1];
225 }
226 return 2;
227 }else{
228 /**
229 These tag responses should have CRC. Total length leftmost
230
231 10 READ data[8] crc[2]
232 34 READ4 data[32]crc[2]
233 10 UPDATE data[8] crc[2]
234 10 SELECT csn[8] crc[2]
235 10 IDENTIFY asnb[8] crc[2]
236 10 PAGESEL block1[8] crc[2]
237 10 DETECT csn[8] crc[2]
238
239 These should not
240
241 4 CHECK chip_response[4]
242 8 READCHECK data[8]
243 1 ACTALL sof[1]
244 1 ACT sof[1]
245
246 In conclusion, without looking at the command; any response
247 of length 10 or 34 should have CRC
248 **/
249 if(len != 10 && len != 34) return true;
250
251 ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2);
252 return b1 == data[len -2] && b2 == data[len-1];
253 }
254}
4c3de57a 255
f71f4deb 256uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles)
4c3de57a
MHS
257{
258 bool isResponse;
f71f4deb 259 uint16_t duration, data_len, parity_len;
4c3de57a
MHS
260
261 uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
262 char explanation[30] = {0};
263
f71f4deb 264 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
265
4c3de57a
MHS
266 first_timestamp = *((uint32_t *)(trace));
267 timestamp = *((uint32_t *)(trace + tracepos));
4c3de57a
MHS
268
269 tracepos += 4;
270 duration = *((uint16_t *)(trace + tracepos));
271 tracepos += 2;
272 data_len = *((uint16_t *)(trace + tracepos));
273 tracepos += 2;
274
275 if (data_len & 0x8000) {
276 data_len &= 0x7fff;
277 isResponse = true;
278 } else {
279 isResponse = false;
280 }
281 parity_len = (data_len-1)/8 + 1;
282
f71f4deb 283 if (tracepos + data_len + parity_len > traceLen) {
284 return traceLen;
4c3de57a 285 }
4c3de57a
MHS
286 uint8_t *frame = trace + tracepos;
287 tracepos += data_len;
288 uint8_t *parityBytes = trace + tracepos;
289 tracepos += parity_len;
290
27eabcdc
MHS
291 //Check the CRC status
292 uint8_t crcStatus = 2;
293
294 if (data_len > 2) {
295 uint8_t b1, b2;
296 if(protocol == ICLASS)
297 {
298 crcStatus = iclass_CRC_check(isResponse, frame, data_len);
299
300 }else if (protocol == ISO_14443B)
301 {
302 crcStatus = iso14443B_CRC_check(isResponse, frame, data_len);
303 }
304 else if (protocol == ISO_14443A){//Iso 14443a
305
306 ComputeCrc14443(CRC_14443_A, frame, data_len-2, &b1, &b2);
307
308 if (b1 != frame[data_len-2] || b2 != frame[data_len-1]) {
309 if(!(isResponse & (data_len < 6)))
310 {
311 crcStatus = 0;
312 }
313 }
314 }
315 }
316 //0 CRC-command, CRC not ok
317 //1 CRC-command, CRC ok
318 //2 Not crc-command
9e8255d4 319
4c3de57a 320 //--- Draw the data column
9e8255d4 321 //char line[16][110];
4c3de57a 322 char line[16][110];
9e8255d4
MHS
323
324 for (int j = 0; j < data_len && j/16 < 16; j++) {
325
4c3de57a
MHS
326 int oddparity = 0x01;
327 int k;
328
329 for (k=0 ; k<8 ; k++) {
330 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
331 }
4c3de57a 332 uint8_t parityBits = parityBytes[j>>3];
4c3de57a 333 if (isResponse && (oddparity != ((parityBits >> (7-(j&0x0007))) & 0x01))) {
9e8255d4
MHS
334 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x! ", frame[j]);
335
4c3de57a 336 } else {
8b9393d3 337 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x ", frame[j]);
9e8255d4 338 }
27eabcdc
MHS
339
340 }
341 if(crcStatus == 1)
342 {//CRC-command
343 char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4)-1;
344 (*pos1) = '[';
345 char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4)-2;
346 (*pos2) = ']';
9e8255d4
MHS
347 }
348 if(data_len == 0)
349 {
350 if(data_len == 0){
351 sprintf(line[0],"<empty trace - possible error>");
4c3de57a
MHS
352 }
353 }
354 //--- Draw the CRC column
4c3de57a 355
49726b40 356 char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " "));
4c3de57a
MHS
357
358 EndOfTransmissionTimestamp = timestamp + duration;
359
360 if(!isResponse)
361 {
41fdd0f0 362 if(protocol == ICLASS)
79bf1ad2 363 annotateIclass(explanation,sizeof(explanation),frame,data_len);
41fdd0f0 364 else if (protocol == ISO_14443A)
79bf1ad2 365 annotateIso14443a(explanation,sizeof(explanation),frame,data_len);
41fdd0f0
MHS
366 else if(protocol == ISO_14443B)
367 annotateIso14443b(explanation,sizeof(explanation),frame,data_len);
4c3de57a
MHS
368 }
369
9e8255d4
MHS
370 int num_lines = MIN((data_len - 1)/16 + 1, 16);
371 for (int j = 0; j < num_lines ; j++) {
4c3de57a
MHS
372 if (j == 0) {
373 PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s",
374 (timestamp - first_timestamp),
375 (EndOfTransmissionTimestamp - first_timestamp),
376 (isResponse ? "Tag" : "Rdr"),
377 line[j],
378 (j == num_lines-1) ? crc : " ",
379 (j == num_lines-1) ? explanation : "");
380 } else {
381 PrintAndLog(" | | | %-64s| %s| %s",
382 line[j],
383 (j == num_lines-1)?crc:" ",
384 (j == num_lines-1) ? explanation : "");
385 }
386 }
387
f71f4deb 388 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
389
4c3de57a
MHS
390 bool next_isResponse = *((uint16_t *)(trace + tracepos + 6)) & 0x8000;
391
392 if (showWaitCycles && !isResponse && next_isResponse) {
393 uint32_t next_timestamp = *((uint32_t *)(trace + tracepos));
394 if (next_timestamp != 0x44444444) {
395 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
396 (EndOfTransmissionTimestamp - first_timestamp),
397 (next_timestamp - first_timestamp),
398 " ",
399 (next_timestamp - EndOfTransmissionTimestamp));
400 }
401 }
f71f4deb 402
4c3de57a
MHS
403 return tracepos;
404}
405
f71f4deb 406
4c3de57a
MHS
407int CmdHFList(const char *Cmd)
408{
409 bool showWaitCycles = false;
410 char type[40] = {0};
411 int tlen = param_getstr(Cmd,0,type);
412 char param = param_getchar(Cmd, 1);
413 bool errors = false;
b689b842 414 uint8_t protocol = 0;
4c3de57a 415 //Validate params
b689b842 416 if(tlen == 0)
4c3de57a
MHS
417 {
418 errors = true;
419 }
420 if(param == 'h' || (param !=0 && param != 'f'))
421 {
422 errors = true;
423 }
b689b842
MHS
424 if(!errors)
425 {
426 if(strcmp(type, "iclass") == 0)
427 {
428 protocol = ICLASS;
429 }else if(strcmp(type, "14a") == 0)
430 {
431 protocol = ISO_14443A;
432 }
433 else if(strcmp(type, "14b") == 0)
434 {
435 protocol = ISO_14443B;
436 }else if(strcmp(type,"raw")== 0)
437 {
438 protocol = -1;//No crc, no annotations
439 }else{
440 errors = true;
441 }
442 }
4c3de57a
MHS
443
444 if (errors) {
445 PrintAndLog("List protocol data in trace buffer.");
92623113
MHS
446 PrintAndLog("Usage: hf list <protocol> [f]");
447 PrintAndLog(" f - show frame delay times as well");
448 PrintAndLog("Supported <protocol> values:");
449 PrintAndLog(" raw - just show raw data without annotations");
337818f7 450 PrintAndLog(" 14a - interpret data as iso14443a communications");
41fdd0f0 451 PrintAndLog(" 14b - interpret data as iso14443b communications");
4c3de57a 452 PrintAndLog(" iclass - interpret data as iclass communications");
4c3de57a
MHS
453 PrintAndLog("");
454 PrintAndLog("example: hf list 14a f");
455 PrintAndLog("example: hf list iclass");
456 return 0;
457 }
b689b842 458
4c3de57a
MHS
459
460 if (param == 'f') {
461 showWaitCycles = true;
462 }
463
464
f71f4deb 465 uint8_t *trace;
4c3de57a 466 uint16_t tracepos = 0;
f71f4deb 467 trace = malloc(USB_CMD_DATA_SIZE);
468
469 // Query for the size of the trace
470 UsbCommand response;
471 GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0);
472 WaitForResponse(CMD_ACK, &response);
473 uint16_t traceLen = response.arg[2];
474 if (traceLen > USB_CMD_DATA_SIZE) {
475 uint8_t *p = realloc(trace, traceLen);
476 if (p == NULL) {
477 PrintAndLog("Cannot allocate memory for trace");
478 free(trace);
479 return 2;
480 }
481 trace = p;
482 GetFromBigBuf(trace, traceLen, 0);
483 WaitForResponse(CMD_ACK, NULL);
484 }
485
486 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen);
4c3de57a
MHS
487 PrintAndLog("");
488 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
489 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
490 PrintAndLog("iClass - Timings are not as accurate");
491 PrintAndLog("");
492 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
493 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
494
f71f4deb 495 while(tracepos < traceLen)
4c3de57a 496 {
f71f4deb 497 tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles);
4c3de57a 498 }
f71f4deb 499
500 free(trace);
4c3de57a
MHS
501 return 0;
502}
503
7fe9b0b7 504
505static command_t CommandTable[] =
506{
507 {"help", CmdHelp, 1, "This help"},
37239a7c 508 {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"},
509 {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"},
510 {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"},
5acd09bd 511 {"epa", CmdHFEPA, 1, "{ German Identification Card... }"},
8e220a91 512 {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"},
cee5a30d 513 {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"},
4c3de57a 514 {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"},
5ee70129 515 {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"},
c59c3405 516 {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"},
4c3de57a
MHS
517 {"list", CmdHFList, 1, "List protocol data in trace buffer"},
518 {NULL, NULL, 0, NULL}
7fe9b0b7 519};
520
521int CmdHF(const char *Cmd)
522{
523 CmdsParse(CommandTable, Cmd);
524 return 0;
525}
526
527int CmdHelp(const char *Cmd)
528{
529 CmdsHelp(CommandTable);
530 return 0;
531}
Impressum, Datenschutz