]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhficlass.c
(no commit message)
[proxmark3-svn] / client / cmdhficlass.c
CommitLineData
cee5a30d 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
3// Copyright (C) 2011 Gerhard de Koning Gans
4//
5// This code is licensed to you under the terms of the GNU GPL, version 2 or,
6// at your option, any later version. See the LICENSE.txt file for the text of
7// the license.
8//-----------------------------------------------------------------------------
9// High frequency iClass commands
10//-----------------------------------------------------------------------------
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include "iso14443crc.h" // Can also be used for iClass, using 0xE012 as CRC-type
16#include "data.h"
17#include "proxusb.h"
18#include "ui.h"
19#include "cmdparser.h"
20#include "cmdhficlass.h"
21#include "common.h"
22
23static int CmdHelp(const char *Cmd);
24
25int CmdHFiClassList(const char *Cmd)
26{
27 uint8_t got[1920];
28 GetFromBigBuf(got, sizeof(got));
29
30 PrintAndLog("recorded activity:");
31 PrintAndLog(" ETU :rssi: who bytes");
32 PrintAndLog("---------+----+----+-----------");
33
34 int i = 0;
35 int prev = -1;
36
37 for (;;) {
38 if(i >= 1900) {
39 break;
40 }
41
42 bool isResponse;
43 int timestamp = *((uint32_t *)(got+i));
44 if (timestamp & 0x80000000) {
45 timestamp &= 0x7fffffff;
46 isResponse = 1;
47 } else {
48 isResponse = 0;
49 }
50
51 int metric = 0;
52 int parityBits = *((uint32_t *)(got+i+4));
53 // 4 bytes of additional information...
54 // maximum of 32 additional parity bit information
55 //
56 // TODO:
57 // at each quarter bit period we can send power level (16 levels)
58 // or each half bit period in 256 levels.
59
60
61 int len = got[i+8];
62
63 if (len > 100) {
64 break;
65 }
66 if (i + len >= 1900) {
67 break;
68 }
69
70 uint8_t *frame = (got+i+9);
71
72 // Break and stick with current result if buffer was not completely full
73 if (frame[0] == 0x44 && frame[1] == 0x44 && frame[3] == 0x44) { break; }
74
75 char line[1000] = "";
76 int j;
77 for (j = 0; j < len; j++) {
78 int oddparity = 0x01;
79 int k;
80
81 for (k=0;k<8;k++) {
82 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
83 }
84
85 //if((parityBits >> (len - j - 1)) & 0x01) {
86 if (isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) {
87 sprintf(line+(j*4), "%02x! ", frame[j]);
88 }
89 else {
90 sprintf(line+(j*4), "%02x ", frame[j]);
91 }
92 }
93
94 char *crc;
95 crc = "";
96 if (len > 2) {
97 uint8_t b1, b2;
98 for (j = 0; j < (len - 1); j++) {
99 // gives problems... search for the reason..
100 /*if(frame[j] == 0xAA) {
101 switch(frame[j+1]) {
102 case 0x01:
103 crc = "[1] Two drops close after each other";
104 break;
105 case 0x02:
106 crc = "[2] Potential SOC with a drop in second half of bitperiod";
107 break;
108 case 0x03:
109 crc = "[3] Segment Z after segment X is not possible";
110 break;
111 case 0x04:
112 crc = "[4] Parity bit of a fully received byte was wrong";
113 break;
114 default:
115 crc = "[?] Unknown error";
116 break;
117 }
118 break;
119 }*/
120 }
121
122 if (strlen(crc)==0) {
123 if(!isResponse && len == 4) {
124 // Rough guess that this is a command from the reader
125 // For iClass the command byte is not part of the CRC
126 ComputeCrc14443(CRC_ICLASS, &frame[1], len-3, &b1, &b2);
127 }
128 else {
129 // For other data.. CRC might not be applicable (UPDATE commands etc.)
130 ComputeCrc14443(CRC_ICLASS, frame, len-2, &b1, &b2);
131 }
132 //printf("%1x %1x",(unsigned)b1,(unsigned)b2);
133 if (b1 != frame[len-2] || b2 != frame[len-1]) {
134 crc = (isResponse & (len < 8)) ? "" : " !crc";
135 } else {
136 crc = "";
137 }
138 }
139 } else {
140 crc = ""; // SHORT
141 }
142
143 char metricString[100];
144 if (isResponse) {
145 sprintf(metricString, "%3d", metric);
146 } else {
147 strcpy(metricString, " ");
148 }
149
150 PrintAndLog(" +%7d: %s: %s %s %s",
151 (prev < 0 ? 0 : (timestamp - prev)),
152 metricString,
153 (isResponse ? "TAG" : " "), line, crc);
154
155 prev = timestamp;
156 i += (len + 9);
157 }
158 return 0;
159}
160
161/*void iso14a_set_timeout(uint32_t timeout) {
162 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_SET_TIMEOUT, 0, timeout}};
163 SendCommand(&c);
164}*/
165
166int CmdHFiClassSnoop(const char *Cmd)
167{
168 UsbCommand c = {CMD_SNOOP_ICLASS};
169 SendCommand(&c);
170 return 0;
171}
172
173static command_t CommandTable[] =
174{
175 {"help", CmdHelp, 1, "This help"},
176 {"list", CmdHFiClassList, 0, "List iClass history"},
177 {"snoop", CmdHFiClassSnoop, 0, "Eavesdrop iClass communication"},
178 {NULL, NULL, 0, NULL}
179};
180
181int CmdHFiClass(const char *Cmd)
182{
183 CmdsParse(CommandTable, Cmd);
184 return 0;
185}
186
187int CmdHelp(const char *Cmd)
188{
189 CmdsHelp(CommandTable);
190 return 0;
191}
Impressum, Datenschutz