mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-19 21:03:48 -07:00
CHG: syntax suger,
CHG: the nested call to readerreceive is not negated..
This commit is contained in:
parent
1b4a446aba
commit
ca5bad3d73
1 changed files with 62 additions and 115 deletions
|
@ -146,11 +146,11 @@ void iso14a_set_ATS_timeout(uint8_t *ats) {
|
||||||
tb1 = ats[2];
|
tb1 = ats[2];
|
||||||
|
|
||||||
fwi = (tb1 & 0xf0) >> 4; // frame waiting indicator (FWI)
|
fwi = (tb1 & 0xf0) >> 4; // frame waiting indicator (FWI)
|
||||||
//fwt = 256 * 16 * (1 << fwi); // frame waiting time (FWT) in 1/fc
|
fwt = 256 * 16 * (1 << fwi); // frame waiting time (FWT) in 1/fc
|
||||||
fwt = 4096 * (1 << fwi);
|
//fwt = 4096 * (1 << fwi);
|
||||||
|
|
||||||
//iso14a_set_timeout(fwt/(8*16));
|
iso14a_set_timeout(fwt/(8*16));
|
||||||
iso14a_set_timeout(fwt/128);
|
//iso14a_set_timeout(fwt/128);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -431,7 +431,6 @@ void DemodInit(uint8_t *data, uint8_t *parity)
|
||||||
// use parameter non_real_time to provide a timestamp. Set to 0 if the decoder should measure real time
|
// use parameter non_real_time to provide a timestamp. Set to 0 if the decoder should measure real time
|
||||||
static RAMFUNC int ManchesterDecoding(uint8_t bit, uint16_t offset, uint32_t non_real_time)
|
static RAMFUNC int ManchesterDecoding(uint8_t bit, uint16_t offset, uint32_t non_real_time)
|
||||||
{
|
{
|
||||||
|
|
||||||
Demod.twoBits = (Demod.twoBits << 8) | bit;
|
Demod.twoBits = (Demod.twoBits << 8) | bit;
|
||||||
|
|
||||||
if (Demod.state == DEMOD_UNSYNCD) {
|
if (Demod.state == DEMOD_UNSYNCD) {
|
||||||
|
@ -802,7 +801,7 @@ static int GetIso14443aCommandFromReader(uint8_t *received, uint8_t *parity, int
|
||||||
LED_D_OFF();
|
LED_D_OFF();
|
||||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN);
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_TAGSIM_LISTEN);
|
||||||
|
|
||||||
// Now run a `software UART' on the stream of incoming samples.
|
// Now run a `software UART` on the stream of incoming samples.
|
||||||
UartInit(received, parity);
|
UartInit(received, parity);
|
||||||
|
|
||||||
// clear RXRDY:
|
// clear RXRDY:
|
||||||
|
@ -836,11 +835,11 @@ bool EmLogTrace(uint8_t *reader_data, uint16_t reader_len, uint32_t reader_Start
|
||||||
static uint8_t* free_buffer_pointer;
|
static uint8_t* free_buffer_pointer;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t* response;
|
uint8_t* response;
|
||||||
size_t response_n;
|
size_t response_n;
|
||||||
uint8_t* modulation;
|
uint8_t* modulation;
|
||||||
size_t modulation_n;
|
size_t modulation_n;
|
||||||
uint32_t ProxToAirDuration;
|
uint32_t ProxToAirDuration;
|
||||||
} tag_response_info_t;
|
} tag_response_info_t;
|
||||||
|
|
||||||
bool prepare_tag_modulation(tag_response_info_t* response_info, size_t max_buffer_size) {
|
bool prepare_tag_modulation(tag_response_info_t* response_info, size_t max_buffer_size) {
|
||||||
|
@ -886,20 +885,20 @@ bool prepare_tag_modulation(tag_response_info_t* response_info, size_t max_buffe
|
||||||
#define ALLOCATED_TAG_MODULATION_BUFFER_SIZE 453
|
#define ALLOCATED_TAG_MODULATION_BUFFER_SIZE 453
|
||||||
|
|
||||||
bool prepare_allocated_tag_modulation(tag_response_info_t* response_info) {
|
bool prepare_allocated_tag_modulation(tag_response_info_t* response_info) {
|
||||||
// Retrieve and store the current buffer index
|
// Retrieve and store the current buffer index
|
||||||
response_info->modulation = free_buffer_pointer;
|
response_info->modulation = free_buffer_pointer;
|
||||||
|
|
||||||
// Determine the maximum size we can use from our buffer
|
// Determine the maximum size we can use from our buffer
|
||||||
size_t max_buffer_size = ALLOCATED_TAG_MODULATION_BUFFER_SIZE;
|
size_t max_buffer_size = ALLOCATED_TAG_MODULATION_BUFFER_SIZE;
|
||||||
|
|
||||||
// Forward the prepare tag modulation function to the inner function
|
// Forward the prepare tag modulation function to the inner function
|
||||||
if (prepare_tag_modulation(response_info, max_buffer_size)) {
|
if (prepare_tag_modulation(response_info, max_buffer_size)) {
|
||||||
// Update the free buffer offset
|
// Update the free buffer offset
|
||||||
free_buffer_pointer += ToSendMax;
|
free_buffer_pointer += ToSendMax;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
@ -1447,21 +1446,13 @@ static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing
|
||||||
uint32_t ThisTransferTime = 0;
|
uint32_t ThisTransferTime = 0;
|
||||||
|
|
||||||
if (timing) {
|
if (timing) {
|
||||||
|
if(*timing == 0) { // Measure time
|
||||||
if (*timing != 0)
|
|
||||||
// Delay transfer (fine tuning - up to 7 MF clock ticks)
|
|
||||||
PrepareDelayedTransfer(*timing & 0x00000007);
|
|
||||||
else
|
|
||||||
// Measure time
|
|
||||||
*timing = (GetCountSspClk() + 8) & 0xfffffff8;
|
*timing = (GetCountSspClk() + 8) & 0xfffffff8;
|
||||||
|
} else {
|
||||||
|
PrepareDelayedTransfer(*timing & 0x00000007); // Delay transfer (fine tuning - up to 7 MF clock ticks)
|
||||||
if (MF_DBGLEVEL >= 4 && GetCountSspClk() >= (*timing & 0xfffffff8))
|
}
|
||||||
Dbprintf("TransmitFor14443a: Missed timing");
|
if(MF_DBGLEVEL >= 4 && GetCountSspClk() >= (*timing & 0xfffffff8)) Dbprintf("TransmitFor14443a: Missed timing");
|
||||||
|
while(GetCountSspClk() < (*timing & 0xfffffff8)); // Delay transfer (multiple of 8 MF clock ticks)
|
||||||
// Delay transfer (multiple of 8 MF clock ticks)
|
|
||||||
while (GetCountSspClk() < (*timing & 0xfffffff8));
|
|
||||||
|
|
||||||
LastTimeProxToAirStart = *timing;
|
LastTimeProxToAirStart = *timing;
|
||||||
} else {
|
} else {
|
||||||
ThisTransferTime = ((MAX(NextTransferTime, GetCountSspClk()) & 0xfffffff8) + 8);
|
ThisTransferTime = ((MAX(NextTransferTime, GetCountSspClk()) & 0xfffffff8) + 8);
|
||||||
|
@ -1573,8 +1564,7 @@ void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, const uint8
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void CodeIso14443aAsReaderPar(const uint8_t *cmd, uint16_t len, const uint8_t *parity)
|
void CodeIso14443aAsReaderPar(const uint8_t *cmd, uint16_t len, const uint8_t *parity)
|
||||||
{
|
{
|
||||||
//CodeIso14443aBitsAsReaderPar(cmd, len*8, parity);
|
CodeIso14443aBitsAsReaderPar(cmd, len*8, parity);
|
||||||
CodeIso14443aBitsAsReaderPar(cmd, len<<3, parity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1838,16 +1828,14 @@ void ReaderTransmitBitsPar(uint8_t* frame, uint16_t bits, uint8_t *par, uint32_t
|
||||||
|
|
||||||
void ReaderTransmitPar(uint8_t* frame, uint16_t len, uint8_t *par, uint32_t *timing)
|
void ReaderTransmitPar(uint8_t* frame, uint16_t len, uint8_t *par, uint32_t *timing)
|
||||||
{
|
{
|
||||||
//ReaderTransmitBitsPar(frame, len*8, par, timing);
|
ReaderTransmitBitsPar(frame, len*8, par, timing);
|
||||||
ReaderTransmitBitsPar(frame, len<<3, par, timing);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReaderTransmitBits(uint8_t* frame, uint16_t len, uint32_t *timing)
|
void ReaderTransmitBits(uint8_t* frame, uint16_t len, uint32_t *timing)
|
||||||
{
|
{
|
||||||
// Generate parity and redirect
|
// Generate parity and redirect
|
||||||
uint8_t par[MAX_PARITY_SIZE] = {0x00};
|
uint8_t par[MAX_PARITY_SIZE] = {0x00};
|
||||||
//GetParity(frame, len/8, par);
|
GetParity(frame, len/8, par);
|
||||||
GetParity(frame, len >> 3, par);
|
|
||||||
ReaderTransmitBitsPar(frame, len, par, timing);
|
ReaderTransmitBitsPar(frame, len, par, timing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1856,27 +1844,24 @@ void ReaderTransmit(uint8_t* frame, uint16_t len, uint32_t *timing)
|
||||||
// Generate parity and redirect
|
// Generate parity and redirect
|
||||||
uint8_t par[MAX_PARITY_SIZE] = {0x00};
|
uint8_t par[MAX_PARITY_SIZE] = {0x00};
|
||||||
GetParity(frame, len, par);
|
GetParity(frame, len, par);
|
||||||
//ReaderTransmitBitsPar(frame, len*8, par, timing);
|
ReaderTransmitBitsPar(frame, len*8, par, timing);
|
||||||
ReaderTransmitBitsPar(frame, len<<3, par, timing);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int ReaderReceiveOffset(uint8_t* receivedAnswer, uint16_t offset, uint8_t *parity)
|
int ReaderReceiveOffset(uint8_t* receivedAnswer, uint16_t offset, uint8_t *parity)
|
||||||
{
|
{
|
||||||
if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, offset))
|
if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, offset)) return FALSE;
|
||||||
return FALSE;
|
//if (tracing) {
|
||||||
|
LogTrace(receivedAnswer, Demod.len, Demod.startTime*16 - DELAY_AIR2ARM_AS_READER, Demod.endTime*16 - DELAY_AIR2ARM_AS_READER, parity, FALSE);
|
||||||
//LogTrace(receivedAnswer, Demod.len, Demod.startTime*16 - DELAY_AIR2ARM_AS_READER, Demod.endTime*16 - DELAY_AIR2ARM_AS_READER, parity, FALSE);
|
//}
|
||||||
LogTrace(receivedAnswer, Demod.len, (Demod.startTime<<4) - DELAY_AIR2ARM_AS_READER, (Demod.endTime<<4) - DELAY_AIR2ARM_AS_READER, parity, FALSE);
|
|
||||||
return Demod.len;
|
return Demod.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ReaderReceive(uint8_t *receivedAnswer, uint8_t *parity)
|
int ReaderReceive(uint8_t *receivedAnswer, uint8_t *parity)
|
||||||
{
|
{
|
||||||
if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, 0))
|
if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, 0)) return FALSE;
|
||||||
return FALSE;
|
//if (tracing) {
|
||||||
|
LogTrace(receivedAnswer, Demod.len, Demod.startTime*16 - DELAY_AIR2ARM_AS_READER, Demod.endTime*16 - DELAY_AIR2ARM_AS_READER, parity, FALSE);
|
||||||
//LogTrace(receivedAnswer, Demod.len, Demod.startTime*16 - DELAY_AIR2ARM_AS_READER, Demod.endTime*16 - DELAY_AIR2ARM_AS_READER, parity, FALSE);
|
//}
|
||||||
LogTrace(receivedAnswer, Demod.len, (Demod.startTime<<4) - DELAY_AIR2ARM_AS_READER, (Demod.endTime<<4) - DELAY_AIR2ARM_AS_READER, parity, FALSE);
|
|
||||||
return Demod.len;
|
return Demod.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2044,12 +2029,11 @@ void iso14443a_setup(uint8_t fpga_minor_mode) {
|
||||||
// connect Demodulated Signal to ADC:
|
// connect Demodulated Signal to ADC:
|
||||||
SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
|
SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
|
||||||
|
|
||||||
|
LED_D_OFF();
|
||||||
// Signal field is on with the appropriate LED
|
// Signal field is on with the appropriate LED
|
||||||
if (fpga_minor_mode == FPGA_HF_ISO14443A_READER_MOD
|
if (fpga_minor_mode == FPGA_HF_ISO14443A_READER_MOD ||
|
||||||
|| fpga_minor_mode == FPGA_HF_ISO14443A_READER_LISTEN)
|
fpga_minor_mode == FPGA_HF_ISO14443A_READER_LISTEN)
|
||||||
LED_D_ON();
|
LED_D_ON();
|
||||||
else
|
|
||||||
LED_D_OFF();
|
|
||||||
|
|
||||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | fpga_minor_mode);
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | fpga_minor_mode);
|
||||||
|
|
||||||
|
@ -2076,12 +2060,14 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, void *data) {
|
||||||
|
|
||||||
ReaderTransmit(real_cmd, cmd_len+4, NULL);
|
ReaderTransmit(real_cmd, cmd_len+4, NULL);
|
||||||
size_t len = ReaderReceive(data, parity);
|
size_t len = ReaderReceive(data, parity);
|
||||||
|
//DATA LINK ERROR
|
||||||
|
if (!len) return 0;
|
||||||
|
|
||||||
uint8_t *data_bytes = (uint8_t *) data;
|
uint8_t *data_bytes = (uint8_t *) data;
|
||||||
if (!len)
|
|
||||||
return 0; //DATA LINK ERROR
|
|
||||||
// if we received an I- or R(ACK)-Block with a block number equal to the
|
// if we received an I- or R(ACK)-Block with a block number equal to the
|
||||||
// current block number, toggle the current block number
|
// current block number, toggle the current block number
|
||||||
else if (len >= 4 // PCB+CID+CRC = 4 bytes
|
if (len >= 4 // PCB+CID+CRC = 4 bytes
|
||||||
&& ((data_bytes[0] & 0xC0) == 0 // I-Block
|
&& ((data_bytes[0] & 0xC0) == 0 // I-Block
|
||||||
|| (data_bytes[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0
|
|| (data_bytes[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0
|
||||||
&& (data_bytes[0] & 0x01) == iso14_pcb_blocknum) // equal block numbers
|
&& (data_bytes[0] & 0x01) == iso14_pcb_blocknum) // equal block numbers
|
||||||
|
@ -2191,58 +2177,19 @@ void ReaderIso14443a(UsbCommand *c)
|
||||||
// Therefore try in alternating directions.
|
// Therefore try in alternating directions.
|
||||||
int32_t dist_nt(uint32_t nt1, uint32_t nt2) {
|
int32_t dist_nt(uint32_t nt1, uint32_t nt2) {
|
||||||
|
|
||||||
|
uint16_t i;
|
||||||
|
uint32_t nttmp1, nttmp2;
|
||||||
|
|
||||||
if (nt1 == nt2) return 0;
|
if (nt1 == nt2) return 0;
|
||||||
|
|
||||||
uint16_t i;
|
nttmp1 = nt1;
|
||||||
uint32_t nttmp1 = nt1;
|
nttmp2 = nt2;
|
||||||
uint32_t nttmp2 = nt2;
|
|
||||||
|
|
||||||
for (i = 1; i < 0xFFFF; i += 8) {
|
for (i = 1; i < 32768; i++) {
|
||||||
nttmp1 = prng_successor_one(nttmp1); if (nttmp1 == nt2) return i;
|
nttmp1 = prng_successor(nttmp1, 1);
|
||||||
nttmp2 = prng_successor_one(nttmp2); if (nttmp2 == nt1) return -i;
|
if (nttmp1 == nt2) return i;
|
||||||
|
nttmp2 = prng_successor(nttmp2, 1);
|
||||||
nttmp1 = prng_successor_one(nttmp1); if (nttmp1 == nt2) return i+1;
|
if (nttmp2 == nt1) return -i;
|
||||||
nttmp2 = prng_successor_one(nttmp2); if (nttmp2 == nt1) return -i-1;
|
|
||||||
|
|
||||||
nttmp1 = prng_successor_one(nttmp1); if (nttmp1 == nt2) return i+2;
|
|
||||||
nttmp2 = prng_successor_one(nttmp2); if (nttmp2 == nt1) return -i-2;
|
|
||||||
|
|
||||||
nttmp1 = prng_successor_one(nttmp1); if (nttmp1 == nt2) return i+3;
|
|
||||||
nttmp2 = prng_successor_one(nttmp2); if (nttmp2 == nt1) return -i-3;
|
|
||||||
|
|
||||||
nttmp1 = prng_successor_one(nttmp1); if (nttmp1 == nt2) return i+4;
|
|
||||||
nttmp2 = prng_successor_one(nttmp2); if (nttmp2 == nt1) return -i-4;
|
|
||||||
|
|
||||||
nttmp1 = prng_successor_one(nttmp1); if (nttmp1 == nt2) return i+5;
|
|
||||||
nttmp2 = prng_successor_one(nttmp2); if (nttmp2 == nt1) return -i-5;
|
|
||||||
|
|
||||||
nttmp1 = prng_successor_one(nttmp1); if (nttmp1 == nt2) return i+6;
|
|
||||||
nttmp2 = prng_successor_one(nttmp2); if (nttmp2 == nt1) return -i-6;
|
|
||||||
|
|
||||||
nttmp1 = prng_successor_one(nttmp1); if (nttmp1 == nt2) return i+7;
|
|
||||||
nttmp2 = prng_successor_one(nttmp2); if (nttmp2 == nt1) return -i-7;
|
|
||||||
/*
|
|
||||||
if ( prng_successor(nttmp1, i) == nt2) return i;
|
|
||||||
if ( prng_successor(nttmp2, i) == nt1) return -i;
|
|
||||||
|
|
||||||
if ( prng_successor(nttmp1, i+2) == nt2) return i+2;
|
|
||||||
if ( prng_successor(nttmp2, i+2) == nt1) return -(i+2);
|
|
||||||
|
|
||||||
if ( prng_successor(nttmp1, i+3) == nt2) return i+3;
|
|
||||||
if ( prng_successor(nttmp2, i+3) == nt1) return -(i+3);
|
|
||||||
|
|
||||||
if ( prng_successor(nttmp1, i+4) == nt2) return i+4;
|
|
||||||
if ( prng_successor(nttmp2, i+4) == nt1) return -(i+4);
|
|
||||||
|
|
||||||
if ( prng_successor(nttmp1, i+5) == nt2) return i+5;
|
|
||||||
if ( prng_successor(nttmp2, i+5) == nt1) return -(i+5);
|
|
||||||
|
|
||||||
if ( prng_successor(nttmp1, i+6) == nt2) return i+6;
|
|
||||||
if ( prng_successor(nttmp2, i+6) == nt1) return -(i+6);
|
|
||||||
|
|
||||||
if ( prng_successor(nttmp1, i+7) == nt2) return i+7;
|
|
||||||
if ( prng_successor(nttmp2, i+7) == nt1) return -(i+7);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return(-99999); // either nt1 or nt2 are invalid nonces
|
return(-99999); // either nt1 or nt2 are invalid nonces
|
||||||
|
@ -2456,7 +2403,7 @@ void ReaderMifare(bool first_try, uint8_t block )
|
||||||
}
|
}
|
||||||
|
|
||||||
// Receive answer. This will be a 4 Bit NACK when the 8 parity bits are OK after decoding
|
// Receive answer. This will be a 4 Bit NACK when the 8 parity bits are OK after decoding
|
||||||
if (ReaderReceive(receivedAnswer, receivedAnswerPar)) {
|
if (!ReaderReceive(receivedAnswer, receivedAnswerPar)) {
|
||||||
catch_up_cycles = 8; // the PRNG is delayed by 8 cycles due to the NAC (4Bits = 0x05 encrypted) transfer
|
catch_up_cycles = 8; // the PRNG is delayed by 8 cycles due to the NAC (4Bits = 0x05 encrypted) transfer
|
||||||
|
|
||||||
if (nt_diff == 0)
|
if (nt_diff == 0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue