chg: hitag2, some clean up, also testing to remove relay_off, since its never relay_on anywhere.

This commit is contained in:
iceman1001 2019-03-11 14:56:03 +01:00
commit 780424fe11

View file

@ -156,8 +156,9 @@ static u64 _hitag2_round(u64 *state) {
static u32 _hitag2_byte(u64 *x) { static u32 _hitag2_byte(u64 *x) {
u32 i, c; u32 i, c;
for (i = 0, c = 0; i < 8; i++) {
for (i = 0, c = 0; i < 8; i++) c += (u32) _hitag2_round(x) << (i ^ 7); c += (u32) _hitag2_round(x) << (i ^ 7);
}
return c; return c;
} }
@ -659,7 +660,6 @@ static bool hitag2_crypto(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t *
return true; return true;
} }
static bool hitag2_authenticate(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t *txlen) { static bool hitag2_authenticate(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t *txlen) {
// Reset the transmission frame length // Reset the transmission frame length
*txlen = 0; *txlen = 0;
@ -702,7 +702,6 @@ static bool hitag2_authenticate(uint8_t *rx, const size_t rxlen, uint8_t *tx, si
return true; return true;
} }
static bool hitag2_test_auth_attempts(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t *txlen) { static bool hitag2_test_auth_attempts(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t *txlen) {
// Reset the transmission frame length // Reset the transmission frame length
@ -1225,27 +1224,19 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
int tag_sof; int tag_sof;
int t_wait = HITAG_T_WAIT_MAX; int t_wait = HITAG_T_WAIT_MAX;
bool bStop = false; bool bStop = false;
bool bQuitTraceFull = false;
bSuccessful = false; bSuccessful = false;
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
clear_trace();
set_tracing(true);
// Check configuration // Check configuration
switch (htf) { switch (htf) {
case RHT2F_PASSWORD: { case RHT2F_PASSWORD: {
Dbprintf("List identifier in password mode"); Dbprintf("List identifier in password mode");
memcpy(password, htd->pwd.password, 4); memcpy(password, htd->pwd.password, 4);
blocknr = 0; blocknr = 0;
bQuitTraceFull = false;
bQuiet = false; bQuiet = false;
bPwd = false; bPwd = false;
break;
} }
break;
case RHT2F_AUTHENTICATE: { case RHT2F_AUTHENTICATE: {
DbpString("Authenticating using nr,ar pair:"); DbpString("Authenticating using nr,ar pair:");
memcpy(NrAr, htd->auth.NrAr, 8); memcpy(NrAr, htd->auth.NrAr, 8);
@ -1253,10 +1244,8 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
bQuiet = false; bQuiet = false;
bCrypto = false; bCrypto = false;
bAuthenticating = false; bAuthenticating = false;
bQuitTraceFull = true; break;
} }
break;
case RHT2F_CRYPTO: { case RHT2F_CRYPTO: {
DbpString("Authenticating using key:"); DbpString("Authenticating using key:");
memcpy(key, htd->crypto.key, 6); //HACK; 4 or 6?? I read both in the code. memcpy(key, htd->crypto.key, 6); //HACK; 4 or 6?? I read both in the code.
@ -1265,35 +1254,35 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
bQuiet = false; bQuiet = false;
bCrypto = false; bCrypto = false;
bAuthenticating = false; bAuthenticating = false;
bQuitTraceFull = true; break;
} }
break;
case RHT2F_TEST_AUTH_ATTEMPTS: { case RHT2F_TEST_AUTH_ATTEMPTS: {
Dbprintf("Testing %d authentication attempts", (auth_table_len / 8)); Dbprintf("Testing %d authentication attempts", (auth_table_len / 8));
auth_table_pos = 0; auth_table_pos = 0;
memcpy(NrAr, auth_table, 8); memcpy(NrAr, auth_table, 8);
bQuitTraceFull = false;
bQuiet = false; bQuiet = false;
bCrypto = false; bCrypto = false;
break;
} }
break;
case RHT2F_UID_ONLY: { case RHT2F_UID_ONLY: {
blocknr = 0; blocknr = 0;
bQuiet = false; bQuiet = false;
bCrypto = false; bCrypto = false;
bAuthenticating = false; bAuthenticating = false;
bQuitTraceFull = true; break;
} }
break;
default: { default: {
Dbprintf("Error, unknown function: %d", htf); Dbprintf("Error, unknown function: %d", htf);
set_tracing(false); set_tracing(false);
return; return;
} }
break;
} }
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
BigBuf_free();
clear_trace();
set_tracing(true);
LED_D_ON(); LED_D_ON();
hitag2_init(); hitag2_init();
@ -1303,12 +1292,9 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
// Set fpga in edge detect with reader field, we can modulate as reader now // Set fpga in edge detect with reader field, we can modulate as reader now
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_READER_FIELD); FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_READER_FIELD);
SpinDelay(20);
// Set Frequency divisor which will drive the FPGA and analog mux selection
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
SetAdcMuxFor(GPIO_MUXSEL_LOPKD); SetAdcMuxFor(GPIO_MUXSEL_LOPKD);
RELAY_OFF(); // RELAY_OFF();
// Disable modulation at default, which means enable the field // Disable modulation at default, which means enable the field
LOW(GPIO_SSC_DOUT); LOW(GPIO_SSC_DOUT);
@ -1352,9 +1338,7 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
// DbpString("Configured for hitag2 reader"); // DbpString("Configured for hitag2 reader");
} else { } else {
Dbprintf("Error, unknown hitag reader type: %d", htf); Dbprintf("Error, unknown hitag reader type: %d", htf);
set_tracing(false); goto out;
LED_D_OFF();
return;
} }
uint8_t attempt_count = 0; uint8_t attempt_count = 0;
@ -1365,15 +1349,7 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
// Check if frame was captured and store it // Check if frame was captured and store it
if (rxlen > 0) { if (rxlen > 0) {
frame_count++; frame_count++;
if (!bQuiet) { LogTraceHitag(rx, rxlen, response, 0, false);
if (!LogTraceHitag(rx, rxlen, response, 0, false)) {
DbpString("Trace full");
if (bQuitTraceFull)
break;
else
bQuiet = true;
}
}
} }
// By default reset the transmission buffer // By default reset the transmission buffer
@ -1381,34 +1357,32 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
switch (htf) { switch (htf) {
case RHT2F_PASSWORD: { case RHT2F_PASSWORD: {
bStop = !hitag2_password(rx, rxlen, tx, &txlen); bStop = !hitag2_password(rx, rxlen, tx, &txlen);
break;
} }
break;
case RHT2F_AUTHENTICATE: { case RHT2F_AUTHENTICATE: {
bStop = !hitag2_authenticate(rx, rxlen, tx, &txlen); bStop = !hitag2_authenticate(rx, rxlen, tx, &txlen);
break;
} }
break;
case RHT2F_CRYPTO: { case RHT2F_CRYPTO: {
bStop = !hitag2_crypto(rx, rxlen, tx, &txlen, false); bStop = !hitag2_crypto(rx, rxlen, tx, &txlen, false);
break;
} }
break;
case RHT2F_TEST_AUTH_ATTEMPTS: { case RHT2F_TEST_AUTH_ATTEMPTS: {
bStop = !hitag2_test_auth_attempts(rx, rxlen, tx, &txlen); bStop = !hitag2_test_auth_attempts(rx, rxlen, tx, &txlen);
break;
} }
break;
case RHT2F_UID_ONLY: { case RHT2F_UID_ONLY: {
bStop = !hitag2_read_uid(rx, rxlen, tx, &txlen); bStop = !hitag2_read_uid(rx, rxlen, tx, &txlen);
attempt_count++; //attempt 3 times to get uid then quit attempt_count++; //attempt 3 times to get uid then quit
if (!bStop && attempt_count == 3) if (!bStop && attempt_count == 3)
bStop = true; bStop = true;
break;
} }
break;
default: { default: {
Dbprintf("Error, unknown function: %d", htf); Dbprintf("Error, unknown function: %d", htf);
set_tracing(false); goto out;
LED_D_OFF();
return;
} }
break;
} }
// Send and store the reader command // Send and store the reader command
@ -1431,16 +1405,7 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
// Add transmitted frame to total count // Add transmitted frame to total count
if (txlen > 0) { if (txlen > 0) {
frame_count++; frame_count++;
if (!bQuiet) { LogTraceHitag(tx, txlen, HITAG_T_WAIT_2, 0, true);
// Store the frame in the trace
if (!LogTraceHitag(tx, txlen, HITAG_T_WAIT_2, 0, true)) {
if (bQuitTraceFull) {
break;
} else {
bQuiet = true;
}
}
}
} }
// Reset values for receiving frames // Reset values for receiving frames
@ -1509,8 +1474,9 @@ void ReaderHitag(hitag_function htf, hitag_data *htd) {
} }
} }
} }
LED_B_OFF();
LED_D_OFF(); out:
LEDsoff();
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS; AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);