mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-19 21:03:48 -07:00
data psk demod minor fixes, pyramid demod add CS
also adjusted autocorrelate and added a printout for lf search u
This commit is contained in:
parent
1302428367
commit
73d04bb417
6 changed files with 104 additions and 50 deletions
|
@ -22,6 +22,7 @@
|
||||||
#include "cmddata.h"
|
#include "cmddata.h"
|
||||||
#include "lfdemod.h"
|
#include "lfdemod.h"
|
||||||
#include "usb_cmd.h"
|
#include "usb_cmd.h"
|
||||||
|
#include "crc.h"
|
||||||
|
|
||||||
uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN];
|
uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN];
|
||||||
uint8_t g_debugMode;
|
uint8_t g_debugMode;
|
||||||
|
@ -729,10 +730,52 @@ int Cmdaskrawdemod(const char *Cmd)
|
||||||
return ASKrawDemod(Cmd, TRUE);
|
return ASKrawDemod(Cmd, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CmdAutoCorr(const char *Cmd)
|
int AutoCorrelate(int window, bool SaveGrph, bool verbose)
|
||||||
{
|
{
|
||||||
static int CorrelBuffer[MAX_GRAPH_TRACE_LEN];
|
static int CorrelBuffer[MAX_GRAPH_TRACE_LEN];
|
||||||
|
size_t Correlation = 0;
|
||||||
|
int maxSum = 0;
|
||||||
|
int lastMax = 0;
|
||||||
|
if (verbose) PrintAndLog("performing %d correlations", GraphTraceLen - window);
|
||||||
|
for (int i = 0; i < GraphTraceLen - window; ++i) {
|
||||||
|
int sum = 0;
|
||||||
|
for (int j = 0; j < window; ++j) {
|
||||||
|
sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256;
|
||||||
|
}
|
||||||
|
CorrelBuffer[i] = sum;
|
||||||
|
if (sum >= maxSum-100 && sum <= maxSum+100){
|
||||||
|
//another max
|
||||||
|
Correlation = i-lastMax;
|
||||||
|
lastMax = i;
|
||||||
|
if (sum > maxSum) maxSum = sum;
|
||||||
|
} else if (sum > maxSum){
|
||||||
|
maxSum=sum;
|
||||||
|
lastMax = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Correlation==0){
|
||||||
|
//try again with wider margin
|
||||||
|
for (int i = 0; i < GraphTraceLen - window; i++){
|
||||||
|
if (CorrelBuffer[i] >= maxSum-(maxSum*0.05) && CorrelBuffer[i] <= maxSum+(maxSum*0.05)){
|
||||||
|
//another max
|
||||||
|
Correlation = i-lastMax;
|
||||||
|
lastMax = i;
|
||||||
|
//if (CorrelBuffer[i] > maxSum) maxSum = sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (verbose && Correlation > 0) PrintAndLog("Possible Correlation: %d samples",Correlation);
|
||||||
|
|
||||||
|
if (SaveGrph){
|
||||||
|
GraphTraceLen = GraphTraceLen - window;
|
||||||
|
memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int));
|
||||||
|
RepaintGraphWindow();
|
||||||
|
}
|
||||||
|
return Correlation;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CmdAutoCorr(const char *Cmd)
|
||||||
|
{
|
||||||
int window = atoi(Cmd);
|
int window = atoi(Cmd);
|
||||||
|
|
||||||
if (window == 0) {
|
if (window == 0) {
|
||||||
|
@ -744,21 +787,7 @@ int CmdAutoCorr(const char *Cmd)
|
||||||
GraphTraceLen);
|
GraphTraceLen);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
return AutoCorrelate(window, TRUE, TRUE);
|
||||||
PrintAndLog("performing %d correlations", GraphTraceLen - window);
|
|
||||||
|
|
||||||
for (int i = 0; i < GraphTraceLen - window; ++i) {
|
|
||||||
int sum = 0;
|
|
||||||
for (int j = 0; j < window; ++j) {
|
|
||||||
sum += (GraphBuffer[j]*GraphBuffer[i + j]) / 256;
|
|
||||||
}
|
|
||||||
CorrelBuffer[i] = sum;
|
|
||||||
}
|
|
||||||
GraphTraceLen = GraphTraceLen - window;
|
|
||||||
memcpy(GraphBuffer, CorrelBuffer, GraphTraceLen * sizeof (int));
|
|
||||||
|
|
||||||
RepaintGraphWindow();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int CmdBitsamples(const char *Cmd)
|
int CmdBitsamples(const char *Cmd)
|
||||||
|
@ -1434,7 +1463,16 @@ int CmdFSKdemodPyramid(const char *Cmd)
|
||||||
// w = wiegand parity, x = extra space for other formats
|
// w = wiegand parity, x = extra space for other formats
|
||||||
// p = unknown checksum
|
// p = unknown checksum
|
||||||
// (26 bit format shown)
|
// (26 bit format shown)
|
||||||
|
|
||||||
|
//get bytes for checksum calc
|
||||||
|
uint8_t checksum = bytebits_to_byte(BitStream + idx + 120, 8);
|
||||||
|
uint8_t csBuff[14] = {0x00};
|
||||||
|
for (uint8_t i = 0; i < 13; i++){
|
||||||
|
csBuff[i] = bytebits_to_byte(BitStream + idx + 16 + (i*8), 8);
|
||||||
|
}
|
||||||
|
//check checksum calc
|
||||||
|
int checkCS = CRC8Maxim(csBuff,13);
|
||||||
|
|
||||||
//get raw ID before removing parities
|
//get raw ID before removing parities
|
||||||
uint32_t rawLo = bytebits_to_byte(BitStream+idx+96,32);
|
uint32_t rawLo = bytebits_to_byte(BitStream+idx+96,32);
|
||||||
uint32_t rawHi = bytebits_to_byte(BitStream+idx+64,32);
|
uint32_t rawHi = bytebits_to_byte(BitStream+idx+64,32);
|
||||||
|
@ -1444,7 +1482,8 @@ int CmdFSKdemodPyramid(const char *Cmd)
|
||||||
|
|
||||||
size = removeParity(BitStream, idx+8, 8, 1, 120);
|
size = removeParity(BitStream, idx+8, 8, 1, 120);
|
||||||
if (size != 105){
|
if (size != 105){
|
||||||
if (g_debugMode==1) PrintAndLog("DEBUG: Error at parity check-tag size does not match Pyramid format, SIZE: %d, IDX: %d, hi3: %x",size, idx, rawHi3);
|
if (g_debugMode==1)
|
||||||
|
PrintAndLog("DEBUG: Error at parity check - tag size does not match Pyramid format, SIZE: %d, IDX: %d, hi3: %x",size, idx, rawHi3);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1502,6 +1541,11 @@ int CmdFSKdemodPyramid(const char *Cmd)
|
||||||
PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %08x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo);
|
PrintAndLog("Pyramid ID Found - BitLength: %d -unknown BitLength- (%d), Raw: %08x%08x%08x%08x", fmtLen, cardnum, rawHi3, rawHi2, rawHi, rawLo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (checksum == checkCS)
|
||||||
|
PrintAndLog("Checksum %02x passed", checksum);
|
||||||
|
else
|
||||||
|
PrintAndLog("Checksum %02x failed - should have been %02x", checksum, checkCS);
|
||||||
|
|
||||||
if (g_debugMode){
|
if (g_debugMode){
|
||||||
PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128);
|
PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, 128);
|
||||||
printDemodBuff();
|
printDemodBuff();
|
||||||
|
|
|
@ -23,6 +23,7 @@ int CmdAskEM410xDemod(const char *Cmd);
|
||||||
int CmdG_Prox_II_Demod(const char *Cmd);
|
int CmdG_Prox_II_Demod(const char *Cmd);
|
||||||
int Cmdaskrawdemod(const char *Cmd);
|
int Cmdaskrawdemod(const char *Cmd);
|
||||||
int Cmdaskmandemod(const char *Cmd);
|
int Cmdaskmandemod(const char *Cmd);
|
||||||
|
int AutoCorrelate(int window, bool SaveGrph, bool verbose);
|
||||||
int CmdAutoCorr(const char *Cmd);
|
int CmdAutoCorr(const char *Cmd);
|
||||||
int CmdBiphaseDecodeRaw(const char *Cmd);
|
int CmdBiphaseDecodeRaw(const char *Cmd);
|
||||||
int CmdBitsamples(const char *Cmd);
|
int CmdBitsamples(const char *Cmd);
|
||||||
|
|
|
@ -1080,7 +1080,9 @@ int CmdLFfind(const char *Cmd)
|
||||||
if (testRaw=='u' || testRaw=='U'){
|
if (testRaw=='u' || testRaw=='U'){
|
||||||
//test unknown tag formats (raw mode)
|
//test unknown tag formats (raw mode)
|
||||||
PrintAndLog("\nChecking for Unknown tags:\n");
|
PrintAndLog("\nChecking for Unknown tags:\n");
|
||||||
ans=CmdDetectClockRate("f");
|
ans=AutoCorrelate(4000, FALSE, FALSE);
|
||||||
|
if (ans > 0) PrintAndLog("Possible Auto Correlation of %d repeating samples",ans);
|
||||||
|
ans=CmdDetectClockRate("F"); //GetFSKClock("",TRUE,FALSE);
|
||||||
if (ans != 0){ //fsk
|
if (ans != 0){ //fsk
|
||||||
ans=CmdFSKrawdemod("");
|
ans=CmdFSKrawdemod("");
|
||||||
if (ans>0) {
|
if (ans>0) {
|
||||||
|
|
15
common/crc.c
15
common/crc.c
|
@ -5,8 +5,9 @@
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Generic CRC calculation code.
|
// Generic CRC calculation code.
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include "crc.h"
|
#include "crc.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor)
|
void crc_init(crc_t *crc, int order, uint32_t polynom, uint32_t initial_value, uint32_t final_xor)
|
||||||
{
|
{
|
||||||
|
@ -40,3 +41,15 @@ uint32_t crc_finish(crc_t *crc)
|
||||||
{
|
{
|
||||||
return ( crc->state ^ crc->final_xor ) & crc->mask;
|
return ( crc->state ^ crc->final_xor ) & crc->mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t CRC8Maxim(uint8_t *buff, size_t size )
|
||||||
|
{
|
||||||
|
crc_t crc;
|
||||||
|
crc_init(&crc, 9, 0x8c, 0x00, 0x00);
|
||||||
|
crc_clear(&crc);
|
||||||
|
|
||||||
|
for (size_t i=0; i < size; ++i){
|
||||||
|
crc_update(&crc, buff[i], 8);
|
||||||
|
}
|
||||||
|
return crc_finish(&crc);
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#define __CRC_H
|
#define __CRC_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
typedef struct crc {
|
typedef struct crc {
|
||||||
uint32_t state;
|
uint32_t state;
|
||||||
|
@ -36,6 +37,8 @@ extern void crc_clear(crc_t *crc);
|
||||||
/* Get the result of the crc calculation */
|
/* Get the result of the crc calculation */
|
||||||
extern uint32_t crc_finish(crc_t *crc);
|
extern uint32_t crc_finish(crc_t *crc);
|
||||||
|
|
||||||
|
// Calculate CRC-8/Maxim checksum
|
||||||
|
uint32_t CRC8Maxim(uint8_t *buff, size_t size );
|
||||||
/* Static initialization of a crc structure */
|
/* Static initialization of a crc structure */
|
||||||
#define CRC_INITIALIZER(_order, _polynom, _initial_value, _final_xor) { \
|
#define CRC_INITIALIZER(_order, _polynom, _initial_value, _final_xor) { \
|
||||||
.state = ((_initial_value) & ((1L<<(_order))-1)), \
|
.state = ((_initial_value) & ((1L<<(_order))-1)), \
|
|
@ -1589,7 +1589,7 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
|
||||||
if (*size<loopCnt) loopCnt = *size;
|
if (*size<loopCnt) loopCnt = *size;
|
||||||
|
|
||||||
uint8_t curPhase = *invert;
|
uint8_t curPhase = *invert;
|
||||||
size_t i, waveStart=0, waveEnd=0, firstFullWave=0, lastClkBit=0;
|
size_t i, waveStart=1, waveEnd=0, firstFullWave=0, lastClkBit=0;
|
||||||
uint8_t fc=0, fullWaveLen=0, tol=1;
|
uint8_t fc=0, fullWaveLen=0, tol=1;
|
||||||
uint16_t errCnt=0, waveLenCnt=0;
|
uint16_t errCnt=0, waveLenCnt=0;
|
||||||
fc = countPSK_FC(dest, *size);
|
fc = countPSK_FC(dest, *size);
|
||||||
|
@ -1601,27 +1601,21 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
|
||||||
//find first phase shift
|
//find first phase shift
|
||||||
for (i=0; i<loopCnt; i++){
|
for (i=0; i<loopCnt; i++){
|
||||||
if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
|
if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
|
||||||
if (waveStart == 0) {
|
waveEnd = i+1;
|
||||||
waveStart = i+1;
|
//PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
|
||||||
avgWaveVal=dest[i+1];
|
waveLenCnt = waveEnd-waveStart;
|
||||||
//PrintAndLog("DEBUG: waveStart: %d",waveStart);
|
if (waveLenCnt > fc && waveStart > fc){ //not first peak and is a large wave
|
||||||
} else {
|
lastAvgWaveVal = avgWaveVal/(waveLenCnt);
|
||||||
waveEnd = i+1;
|
firstFullWave = waveStart;
|
||||||
//PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
|
fullWaveLen=waveLenCnt;
|
||||||
waveLenCnt = waveEnd-waveStart;
|
//if average wave value is > graph 0 then it is an up wave or a 1
|
||||||
lastAvgWaveVal = avgWaveVal/waveLenCnt;
|
if (lastAvgWaveVal > 123) curPhase^=1; //fudge graph 0 a little 123 vs 128
|
||||||
if (waveLenCnt > fc){
|
break;
|
||||||
firstFullWave = waveStart;
|
}
|
||||||
fullWaveLen=waveLenCnt;
|
waveStart = i+1;
|
||||||
//if average wave value is > graph 0 then it is an up wave or a 1
|
avgWaveVal = 0;
|
||||||
if (lastAvgWaveVal > 128) curPhase^=1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
waveStart=0;
|
|
||||||
avgWaveVal=0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
avgWaveVal+=dest[i+1];
|
avgWaveVal+=dest[i+2];
|
||||||
}
|
}
|
||||||
//PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
|
//PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
|
||||||
lastClkBit = firstFullWave; //set start of wave as clock align
|
lastClkBit = firstFullWave; //set start of wave as clock align
|
||||||
|
@ -1629,7 +1623,7 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
|
||||||
errCnt=0;
|
errCnt=0;
|
||||||
size_t numBits=0;
|
size_t numBits=0;
|
||||||
//PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
|
//PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
|
||||||
|
dest[numBits++] = curPhase; //set first read bit
|
||||||
for (i = firstFullWave+fullWaveLen-1; i < *size-3; i++){
|
for (i = firstFullWave+fullWaveLen-1; i < *size-3; i++){
|
||||||
//top edge of wave = start of new wave
|
//top edge of wave = start of new wave
|
||||||
if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
|
if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
|
||||||
|
@ -1641,26 +1635,23 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
|
||||||
waveEnd = i+1;
|
waveEnd = i+1;
|
||||||
waveLenCnt = waveEnd-waveStart;
|
waveLenCnt = waveEnd-waveStart;
|
||||||
lastAvgWaveVal = avgWaveVal/waveLenCnt;
|
lastAvgWaveVal = avgWaveVal/waveLenCnt;
|
||||||
if (waveLenCnt > fc){
|
if (waveLenCnt > fc){
|
||||||
//PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
|
//PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
|
||||||
//if this wave is a phase shift
|
//if this wave is a phase shift
|
||||||
//PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
|
//PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
|
||||||
if (i+1 >= lastClkBit + *clock - tol){ //should be a clock bit
|
if (i+1 >= lastClkBit + *clock - tol){ //should be a clock bit
|
||||||
curPhase^=1;
|
curPhase^=1;
|
||||||
dest[numBits] = curPhase;
|
dest[numBits++] = curPhase;
|
||||||
numBits++;
|
|
||||||
lastClkBit += *clock;
|
lastClkBit += *clock;
|
||||||
} else if (i<lastClkBit+10){
|
} else if (i<lastClkBit+10+fc){
|
||||||
//noise after a phase shift - ignore
|
//noise after a phase shift - ignore
|
||||||
} else { //phase shift before supposed to based on clock
|
} else { //phase shift before supposed to based on clock
|
||||||
errCnt++;
|
errCnt++;
|
||||||
dest[numBits] = 77;
|
dest[numBits++] = 77;
|
||||||
numBits++;
|
|
||||||
}
|
}
|
||||||
} else if (i+1 > lastClkBit + *clock + tol + fc){
|
} else if (i+1 > lastClkBit + *clock + tol + fc){
|
||||||
lastClkBit += *clock; //no phase shift but clock bit
|
lastClkBit += *clock; //no phase shift but clock bit
|
||||||
dest[numBits] = curPhase;
|
dest[numBits++] = curPhase;
|
||||||
numBits++;
|
|
||||||
}
|
}
|
||||||
avgWaveVal=0;
|
avgWaveVal=0;
|
||||||
waveStart=i+1;
|
waveStart=i+1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue