proxmark3/client/graph.c

257 lines
7.4 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Graph utilities
//-----------------------------------------------------------------------------
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
#include "graph.h"
int GraphBuffer[MAX_GRAPH_TRACE_LEN];
size_t GraphTraceLen;
2017-07-28 04:05:54 +08:00
int s_Buff[MAX_GRAPH_TRACE_LEN];
2019-03-10 03:34:20 +08:00
/* write a manchester bit to the graph
TODO, verfy that this doesn't overflow buffer (iceman)
*/
void AppendGraph(bool redraw, int clock, int bit) {
2019-03-10 06:35:06 +08:00
int i;
//set first half the clock bit (all 1's or 0's for a 0 or 1 bit)
for (i = 0; i < (int)(clock / 2); ++i)
GraphBuffer[GraphTraceLen++] = bit ;
//set second half of the clock bit (all 0's or 1's for a 0 or 1 bit)
for (i = (int)(clock / 2); i < clock; ++i)
GraphBuffer[GraphTraceLen++] = bit ^ 1;
if (redraw)
RepaintGraphWindow();
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
2015-01-08 12:02:00 +08:00
// clear out our graph window
size_t ClearGraph(bool redraw) {
size_t gtl = GraphTraceLen;
2019-03-10 06:35:06 +08:00
memset(GraphBuffer, 0x00, GraphTraceLen);
GraphTraceLen = 0;
if (redraw)
RepaintGraphWindow();
return gtl;
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
// option '1' to save GraphBuffer any other to restore
void save_restoreGB(uint8_t saveOpt) {
2019-03-10 06:35:06 +08:00
static int SavedGB[MAX_GRAPH_TRACE_LEN];
static size_t SavedGBlen = 0;
2019-03-10 06:35:06 +08:00
static bool GB_Saved = false;
static int SavedGridOffsetAdj = 0;
if (saveOpt == GRAPH_SAVE) { //save
memcpy(SavedGB, GraphBuffer, sizeof(GraphBuffer));
SavedGBlen = GraphTraceLen;
GB_Saved = true;
SavedGridOffsetAdj = GridOffset;
2019-03-10 07:00:59 +08:00
} else if (GB_Saved) { //restore
2019-03-10 06:35:06 +08:00
memcpy(GraphBuffer, SavedGB, sizeof(GraphBuffer));
GraphTraceLen = SavedGBlen;
GridOffset = SavedGridOffsetAdj;
RepaintGraphWindow();
}
return;
}
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
2019-04-07 03:46:00 +08:00
void setGraphBuf(uint8_t *buff, size_t size) {
if (buff == NULL) return;
2019-03-10 06:35:06 +08:00
ClearGraph(false);
2019-03-10 07:00:59 +08:00
if (size > MAX_GRAPH_TRACE_LEN)
2019-03-10 06:35:06 +08:00
size = MAX_GRAPH_TRACE_LEN;
for (size_t i = 0; i < size; ++i)
2019-04-07 03:46:00 +08:00
GraphBuffer[i] = buff[i] - 128;
2019-03-10 06:35:06 +08:00
GraphTraceLen = size;
RepaintGraphWindow();
return;
}
2018-09-10 01:56:25 +08:00
2019-04-07 03:46:00 +08:00
size_t getFromGraphBuf(uint8_t *buff) {
if (buff == NULL) return 0;
size_t i;
2019-03-10 07:00:59 +08:00
for (i = 0; i < GraphTraceLen; ++i) {
2019-03-10 06:35:06 +08:00
//trim
if (GraphBuffer[i] > 127) GraphBuffer[i] = 127;
if (GraphBuffer[i] < -127) GraphBuffer[i] = -127;
2019-04-07 03:46:00 +08:00
buff[i] = (uint8_t)(GraphBuffer[i] + 128);
2019-03-10 06:35:06 +08:00
}
return i;
}
// A simple test to see if there is any data inside Graphbuffer.
bool HasGraphData() {
if (GraphTraceLen == 0) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "No data available, try reading something first");
return false;
}
return true;
}
// Get or auto-detect ask clock rate
int GetAskClock(const char *str, bool printAns) {
if (getSignalProperties()->isnoise)
return false;
2019-03-10 06:35:06 +08:00
2019-04-08 16:21:28 +08:00
int clock1 = param_get32ex(str, 0, 0, 10);
if (clock1 > 0)
return clock1;
2019-03-10 06:35:06 +08:00
// Auto-detect clock
uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(bits);
if (size == 0) {
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
return -1;
}
size_t ststart = 0, stend = 0;
2019-04-08 16:21:28 +08:00
bool st = DetectST(bits, &size, &clock1, &ststart, &stend);
2019-03-10 06:35:06 +08:00
int idx = stend;
if (st == false) {
2019-04-08 16:21:28 +08:00
idx = DetectASKClock(bits, size, &clock1, 20);
2019-03-10 06:35:06 +08:00
}
2019-04-08 16:21:28 +08:00
if (clock1 > 0) {
setClockGrid(clock1, idx);
2019-03-10 06:35:06 +08:00
}
// Only print this message if we're not looping something
if (printAns || g_debugMode)
2019-04-08 16:21:28 +08:00
PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d, Best Starting Position: %d", clock1, idx);
2019-03-10 06:35:06 +08:00
2019-04-08 16:21:28 +08:00
return clock1;
}
uint8_t GetPskCarrier(const char *str, bool printAns) {
if (getSignalProperties()->isnoise)
return false;
2019-03-10 06:35:06 +08:00
uint8_t carrier = 0;
uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(bits);
2019-03-10 07:00:59 +08:00
if (size == 0) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
return 0;
}
uint16_t fc = countFC(bits, size, false);
carrier = fc & 0xFF;
if (carrier != 2 && carrier != 4 && carrier != 8) return 0;
2019-03-10 07:00:59 +08:00
if ((fc >> 8) == 10 && carrier == 8) return 0;
2019-03-10 06:35:06 +08:00
// Only print this message if we're not looping something
if (printAns)
PrintAndLogEx(SUCCESS, "Auto-detected PSK carrier rate: %d", carrier);
return carrier;
2015-02-22 10:36:02 +08:00
}
int GetPskClock(const char *str, bool printAns) {
if (getSignalProperties()->isnoise)
return -1;
2019-03-10 06:35:06 +08:00
2019-04-08 16:21:28 +08:00
int clock1 = param_get32ex(str, 0, 0, 10);
if (clock1 != 0)
return clock1;
2019-03-10 06:35:06 +08:00
// Auto-detect clock
uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(grph);
2019-03-10 07:00:59 +08:00
if (size == 0) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
return -1;
}
size_t firstPhaseShiftLoc = 0;
uint8_t curPhase = 0, fc = 0;
2019-04-08 16:21:28 +08:00
clock1 = DetectPSKClock(grph, size, 0, &firstPhaseShiftLoc, &curPhase, &fc);
setClockGrid(clock1, firstPhaseShiftLoc);
2019-03-10 06:35:06 +08:00
// Only print this message if we're not looping something
if (printAns)
2019-04-08 16:21:28 +08:00
PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d", clock1);
2019-04-10 15:49:42 +08:00
2019-04-08 16:21:28 +08:00
return clock1;
}
int GetNrzClock(const char *str, bool printAns) {
if (getSignalProperties()->isnoise)
return -1;
2019-03-10 06:35:06 +08:00
2019-04-08 16:21:28 +08:00
int clock1 = param_get32ex(str, 0, 0, 10);
if (clock1 != 0)
return clock1;
2019-03-10 06:35:06 +08:00
// Auto-detect clock
uint8_t grph[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(grph);
2019-03-10 07:00:59 +08:00
if (size == 0) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "Failed to copy from graphbuffer");
return -1;
}
size_t clkStartIdx = 0;
2019-04-08 16:21:28 +08:00
clock1 = DetectNRZClock(grph, size, 0, &clkStartIdx);
setClockGrid(clock1, clkStartIdx);
2019-03-10 06:35:06 +08:00
// Only print this message if we're not looping something
if (printAns)
2019-04-08 16:21:28 +08:00
PrintAndLogEx(SUCCESS, "Auto-detected clock rate: %d", clock1);
2019-04-10 15:49:42 +08:00
2019-04-08 16:21:28 +08:00
return clock1;
}
//by marshmellow
//attempt to detect the field clock and bit clock for FSK
int GetFskClock(const char *str, bool printAns) {
2019-04-08 16:21:28 +08:00
int clock1 = param_get32ex(str, 0, 0, 10);
if (clock1 != 0)
return clock1;
2019-03-10 06:35:06 +08:00
uint8_t fc1 = 0, fc2 = 0, rf1 = 0;
int firstClockEdge = 0;
2019-03-10 07:00:59 +08:00
if (!fskClocks(&fc1, &fc2, &rf1, &firstClockEdge))
2019-03-10 06:35:06 +08:00
return 0;
2019-03-10 07:00:59 +08:00
if ((fc1 == 10 && fc2 == 8) || (fc1 == 8 && fc2 == 5)) {
2019-03-10 06:35:06 +08:00
if (printAns)
PrintAndLogEx(SUCCESS, "Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
setClockGrid(rf1, firstClockEdge);
return rf1;
}
2018-02-21 23:46:38 +08:00
2019-03-10 06:35:06 +08:00
PrintAndLogEx(DEBUG, "DEBUG: unknown fsk field clock detected");
PrintAndLogEx(DEBUG, "Detected Field Clocks: FC/%d, FC/%d - Bit Clock: RF/%d", fc1, fc2, rf1);
return 0;
}
bool fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, int *firstClockEdge) {
if (getSignalProperties()->isnoise)
return false;
2019-03-10 06:35:06 +08:00
uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(bits);
if (size == 0)
return false;
uint16_t ans = countFC(bits, size, true);
if (ans == 0) {
PrintAndLogEx(DEBUG, "DEBUG: No data found");
return false;
}
*fc1 = (ans >> 8) & 0xFF;
*fc2 = ans & 0xFF;
*rf1 = detectFSKClk(bits, size, *fc1, *fc2, firstClockEdge);
if (*rf1 == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Clock detect error");
return false;
}
return true;
}