FIX: Some uninitialized variables, some syntax suger, and some extra WDT_HIT calls inside "ReaderMifare" to stop it from trigger when running multiple times in a row.

This commit is contained in:
iceman1001 2016-01-20 19:53:58 +01:00
commit 5ebcb867a3
2 changed files with 40 additions and 34 deletions

View file

@ -1464,7 +1464,6 @@ void PrepareDelayedTransfer(uint16_t delay)
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing) static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing)
{ {
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD); FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
uint32_t ThisTransferTime = 0; uint32_t ThisTransferTime = 0;
@ -1492,9 +1491,8 @@ static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing
if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
AT91C_BASE_SSC->SSC_THR = cmd[c]; AT91C_BASE_SSC->SSC_THR = cmd[c];
c++; c++;
if(c >= len) { if(c >= len)
break; break;
}
} }
} }
@ -1508,7 +1506,7 @@ static void TransmitFor14443a(const uint8_t *cmd, uint16_t len, uint32_t *timing
void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, const uint8_t *parity) void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, const uint8_t *parity)
{ {
int i, j; int i, j;
int last; int last = 0;
uint8_t b; uint8_t b;
ToSendReset(); ToSendReset();
@ -1516,7 +1514,6 @@ void CodeIso14443aBitsAsReaderPar(const uint8_t *cmd, uint16_t bits, const uint8
// Start of Communication (Seq. Z) // Start of Communication (Seq. Z)
ToSend[++ToSendMax] = SEC_Z; ToSend[++ToSendMax] = SEC_Z;
LastProxToAirDuration = 8 * (ToSendMax+1) - 6; LastProxToAirDuration = 8 * (ToSendMax+1) - 6;
last = 0;
size_t bytecount = nbytes(bits); size_t bytecount = nbytes(bits);
// Generate send structure for the data bits // Generate send structure for the data bits
@ -1729,7 +1726,7 @@ int EmSend4bitEx(uint8_t resp, bool correctionNeeded){
Code4bitAnswerAsTag(resp); Code4bitAnswerAsTag(resp);
int res = EmSendCmd14443aRaw(ToSend, ToSendMax, correctionNeeded); int res = EmSendCmd14443aRaw(ToSend, ToSendMax, correctionNeeded);
// do the tracing for the previous reader request and this tag answer: // do the tracing for the previous reader request and this tag answer:
uint8_t par[1]; uint8_t par[1] = {0x00};
GetParity(&resp, 1, par); GetParity(&resp, 1, par);
EmLogTrace(Uart.output, EmLogTrace(Uart.output,
Uart.len, Uart.len,
@ -1766,13 +1763,13 @@ int EmSendCmdExPar(uint8_t *resp, uint16_t respLen, bool correctionNeeded, uint8
} }
int EmSendCmdEx(uint8_t *resp, uint16_t respLen, bool correctionNeeded){ int EmSendCmdEx(uint8_t *resp, uint16_t respLen, bool correctionNeeded){
uint8_t par[MAX_PARITY_SIZE]; uint8_t par[MAX_PARITY_SIZE] = {0x00};
GetParity(resp, respLen, par); GetParity(resp, respLen, par);
return EmSendCmdExPar(resp, respLen, correctionNeeded, par); return EmSendCmdExPar(resp, respLen, correctionNeeded, par);
} }
int EmSendCmd(uint8_t *resp, uint16_t respLen){ int EmSendCmd(uint8_t *resp, uint16_t respLen){
uint8_t par[MAX_PARITY_SIZE]; uint8_t par[MAX_PARITY_SIZE] = {0x00};
GetParity(resp, respLen, par); GetParity(resp, respLen, par);
return EmSendCmdExPar(resp, respLen, false, par); return EmSendCmdExPar(resp, respLen, false, par);
} }
@ -1793,9 +1790,12 @@ bool EmLogTrace(uint8_t *reader_data, uint16_t reader_len, uint32_t reader_Start
uint16_t exact_fdt = (approx_fdt - 20 + 32)/64 * 64 + 20; uint16_t exact_fdt = (approx_fdt - 20 + 32)/64 * 64 + 20;
reader_EndTime = tag_StartTime - exact_fdt; reader_EndTime = tag_StartTime - exact_fdt;
reader_StartTime = reader_EndTime - reader_modlen; reader_StartTime = reader_EndTime - reader_modlen;
if (!LogTrace(reader_data, reader_len, reader_StartTime, reader_EndTime, reader_Parity, TRUE)) {
if (!LogTrace(reader_data, reader_len, reader_StartTime, reader_EndTime, reader_Parity, TRUE))
return FALSE; return FALSE;
} else return(!LogTrace(tag_data, tag_len, tag_StartTime, tag_EndTime, tag_Parity, FALSE)); else
return(!LogTrace(tag_data, tag_len, tag_StartTime, tag_EndTime, tag_Parity, FALSE));
} else { } else {
return TRUE; return TRUE;
} }
@ -1847,9 +1847,8 @@ void ReaderTransmitBitsPar(uint8_t* frame, uint16_t bits, uint8_t *par, uint32_t
LED_A_ON(); LED_A_ON();
// Log reader command in trace buffer // Log reader command in trace buffer
if (tracing) { if (tracing)
LogTrace(frame, nbytes(bits), LastTimeProxToAirStart*16 + DELAY_ARM2AIR_AS_READER, (LastTimeProxToAirStart + LastProxToAirDuration)*16 + DELAY_ARM2AIR_AS_READER, par, TRUE); LogTrace(frame, nbytes(bits), LastTimeProxToAirStart*16 + DELAY_ARM2AIR_AS_READER, (LastTimeProxToAirStart + LastProxToAirDuration)*16 + DELAY_ARM2AIR_AS_READER, par, TRUE);
}
} }
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)
@ -1860,7 +1859,7 @@ void ReaderTransmitPar(uint8_t* frame, uint16_t len, uint8_t *par, uint32_t *tim
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]; uint8_t par[MAX_PARITY_SIZE] = {0x00};
GetParity(frame, len/8, par); GetParity(frame, len/8, par);
ReaderTransmitBitsPar(frame, len, par, timing); ReaderTransmitBitsPar(frame, len, par, timing);
} }
@ -1868,26 +1867,30 @@ void ReaderTransmitBits(uint8_t* frame, uint16_t len, uint32_t *timing)
void ReaderTransmit(uint8_t* frame, uint16_t len, uint32_t *timing) 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]; 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);
} }
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)) return FALSE; if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, offset))
if (tracing) { 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);
}
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)) return FALSE; if (!GetIso14443aAnswerFromTag(receivedAnswer, parity, 0))
if (tracing) { 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);
}
return Demod.len; return Demod.len;
} }
@ -2254,7 +2257,7 @@ void ReaderMifare(bool first_try)
static byte_t par_low = 0; static byte_t par_low = 0;
bool led_on = TRUE; bool led_on = TRUE;
uint8_t uid[10] = {0}; uint8_t uid[10] = {0};
uint32_t cuid; uint32_t cuid = 0;
uint32_t nt = 0; uint32_t nt = 0;
uint32_t previous_nt = 0; uint32_t previous_nt = 0;
@ -2267,7 +2270,7 @@ void ReaderMifare(bool first_try)
static int32_t sync_cycles = 0; static int32_t sync_cycles = 0;
int catch_up_cycles = 0; int catch_up_cycles = 0;
int last_catch_up = 0; int last_catch_up = 0;
uint16_t elapsed_prng_sequences; uint16_t elapsed_prng_sequences = 0;
uint16_t consecutive_resyncs = 0; uint16_t consecutive_resyncs = 0;
int isOK = 0; int isOK = 0;
@ -2299,8 +2302,8 @@ void ReaderMifare(bool first_try)
int16_t debug_info_nr = -1; int16_t debug_info_nr = -1;
uint16_t strategy = 0; uint16_t strategy = 0;
int32_t debug_info[MAX_STRATEGY][NUM_DEBUG_INFOS]; int32_t debug_info[MAX_STRATEGY][NUM_DEBUG_INFOS];
uint32_t select_time; uint32_t select_time = 0;
uint32_t halt_time; uint32_t halt_time = 0;
for(uint16_t i = 0; TRUE; ++i) { for(uint16_t i = 0; TRUE; ++i) {
@ -2328,6 +2331,7 @@ void ReaderMifare(bool first_try)
SpinDelay(200); SpinDelay(200);
iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD); iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD);
SpinDelay(100); SpinDelay(100);
WDT_HIT();
} }
if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) { if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {
@ -2345,10 +2349,11 @@ void ReaderMifare(bool first_try)
while(GetCountSspClk() > sync_time) { while(GetCountSspClk() > sync_time) {
elapsed_prng_sequences++; elapsed_prng_sequences++;
sync_time = (sync_time & 0xfffffff8) + sync_cycles; sync_time = (sync_time & 0xfffffff8) + sync_cycles;
} }
// Transmit MIFARE_CLASSIC_AUTH at synctime. Should result in returning the same tag nonce (== nt_attacked) // Transmit MIFARE_CLASSIC_AUTH at synctime. Should result in returning the same tag nonce (== nt_attacked)
ReaderTransmit(mf_auth, sizeof(mf_auth), &sync_time); ReaderTransmit(mf_auth, sizeof(mf_auth), &sync_time);
} else { } else {
// collect some information on tag nonces for debugging: // collect some information on tag nonces for debugging:
#define DEBUG_FIXED_SYNC_CYCLES PRNG_SEQUENCE_LENGTH #define DEBUG_FIXED_SYNC_CYCLES PRNG_SEQUENCE_LENGTH
@ -2473,7 +2478,7 @@ void ReaderMifare(bool first_try)
} else { } else {
if (nt_diff == 0 && first_try) { if (nt_diff == 0 && first_try) {
par[0]++; par[0]++;
if (par[0] == 0x00) { // tried all 256 possible parities without success. Card doesn't send NACK. if (par[0] == 0x00) { // tried all 256 possible parities without success. Card doesn't send NACK.
isOK = -2; isOK = -2;
break; break;
} }
@ -2483,9 +2488,10 @@ void ReaderMifare(bool first_try)
} }
} }
mf_nr_ar[3] &= 0x1F; mf_nr_ar[3] &= 0x1F;
WDT_HIT();
if (isOK == -4) { if (isOK == -4) {
if (MF_DBGLEVEL >= 3) { if (MF_DBGLEVEL >= 3) {
for (uint16_t i = 0; i <= MAX_STRATEGY; ++i) { for (uint16_t i = 0; i <= MAX_STRATEGY; ++i) {
@ -2544,10 +2550,10 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t *
struct Crypto1State *pcs; struct Crypto1State *pcs;
pcs = &mpcs; pcs = &mpcs;
uint32_t numReads = 0;//Counts numer of times reader read a block uint32_t numReads = 0;//Counts numer of times reader read a block
uint8_t receivedCmd[MAX_MIFARE_FRAME_SIZE]; uint8_t receivedCmd[MAX_MIFARE_FRAME_SIZE] = {0x00};
uint8_t receivedCmd_par[MAX_MIFARE_PARITY_SIZE]; uint8_t receivedCmd_par[MAX_MIFARE_PARITY_SIZE] = {0x00};
uint8_t response[MAX_MIFARE_FRAME_SIZE]; uint8_t response[MAX_MIFARE_FRAME_SIZE] = {0x00};
uint8_t response_par[MAX_MIFARE_PARITY_SIZE]; uint8_t response_par[MAX_MIFARE_PARITY_SIZE] = {0x00};
uint8_t rATQA[] = {0x04, 0x00}; // Mifare classic 1k 4BUID uint8_t rATQA[] = {0x04, 0x00}; // Mifare classic 1k 4BUID
uint8_t rUIDBCC1[] = {0xde, 0xad, 0xbe, 0xaf, 0x62}; uint8_t rUIDBCC1[] = {0xde, 0xad, 0xbe, 0xaf, 0x62};

View file

@ -750,7 +750,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
uint16_t davg = 0; uint16_t davg = 0;
static uint16_t dmin, dmax; static uint16_t dmin, dmax;
uint8_t uid[10] = {0x00}; uint8_t uid[10] = {0x00};
uint32_t cuid, nt1, nt2, nttmp, nttest, ks1; uint32_t cuid = 0, nt1, nt2, nttmp, nttest, ks1;
uint8_t par[1] = {0x00}; uint8_t par[1] = {0x00};
uint32_t target_nt[2] = {0x00}, target_ks[2] = {0x00}; uint32_t target_nt[2] = {0x00}, target_ks[2] = {0x00};
@ -759,7 +759,7 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat
struct Crypto1State mpcs = {0, 0}; struct Crypto1State mpcs = {0, 0};
struct Crypto1State *pcs; struct Crypto1State *pcs;
pcs = &mpcs; pcs = &mpcs;
uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE]; uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};
uint32_t auth1_time, auth2_time; uint32_t auth1_time, auth2_time;
static uint16_t delta_time; static uint16_t delta_time;