fixing the wrong calculations

This commit is contained in:
iceman1001 2023-08-24 10:20:01 +02:00
parent 5a56504fc1
commit 424e68dff6
3 changed files with 63 additions and 51 deletions

View file

@ -39,9 +39,14 @@
* Current timing issues with ISO14443-b implementation
* Proxmark3
* Carrier Frequency 13.56MHz
* 1 / 13 560 000 = 73.74 nano seconds ( 0.07374 µs )
* SSP_CLK runs at 13.56MHz / 4 = 3,39MHz
* 1 / 3 390 000 = 294.98 nano seconds ( 0.2949 µs )
*
*
* 1 ETU = 9.4395 µs = 32 SSP_CLK = 128 FC
* 1 SSP_CLK = 4 FC
* 1 µs 3 SSP_CLK about 14 FC
* PROBLEM 1.
* ----------
* one way of calculating time, that relates both to PM3 ssp_clk 3.39MHz, ISO freq of 13.56Mhz and ETUs
@ -106,6 +111,9 @@
*
*/
#ifndef MAX_14B_TIMEOUT
// FWT(max) = 4949 ms or 4.95 seconds.
// SSP_CLK = 4949000 * 3.39 = 16777120
@ -122,8 +130,8 @@
// ETU 14 * 9.4395 µS = 132 µS == 0.132ms
// TR2, counting from start of PICC EOF 14 ETU.
#define DELAY_ISO14443B_PICC_TO_PCD_READER ETU_TO_SSP(14)
#define DELAY_ISO14443B_PCD_TO_PICC_READER ETU_TO_SSP(15)
#define DELAY_ISO14443B_PICC_TO_PCD_READER HF14_ETU_TO_SSP(14)
#define DELAY_ISO14443B_PCD_TO_PICC_READER HF14_ETU_TO_SSP(15)
/* Guard Time (per 14443-2) in ETU
*
@ -138,41 +146,41 @@
* TR0
*/
#ifndef ISO14B_TR0
# define ISO14B_TR0 ETU_TO_SSP(32)
# define ISO14B_TR0 HF14_ETU_TO_SSP(16)
#endif
#ifndef ISO14B_TR0_MAX
# define ISO14B_TR0_MAX ETU_TO_SSP(32)
# define ISO14B_TR0_MAX HF14_ETU_TO_SSP(32)
// * TR0 - 32 ETU's maximum for ATQB only
// * TR0 - FWT for all other commands
// TR0 max is 151/fsc = 151/848kHz = 302us or 64 samples from FPGA
// 32 ETU * 9.4395 µS == 302 µS
// 32 * 8 = 256 sub carrier cycles,
// 256 / 4 = 64 I/Q pairs.
// TR0 max is 159 µS or 32 samples from FPGA
// 16 ETU * 9.4395 µS == 151 µS
// 16 * 8 = 128 sub carrier cycles,
// 128 / 4 = 32 I/Q pairs.
// since 1 I/Q pair after 4 subcarrier cycles at 848kHz subcarrier
#endif
// 8 ETU = 75 µS == 256 SSP_CLK
#ifndef ISO14B_TR0_MIN
# define ISO14B_TR0_MIN ETU_TO_SSP(8)
# define ISO14B_TR0_MIN HF14_ETU_TO_SSP(8)
#endif
// Synchronization time (per 14443-2) in ETU
// 10 ETU = 94,39 µS == 320 SSP_CLK
// 16 ETU = 151 µS == 512 SSP_CLK
#ifndef ISO14B_TR1_MIN
# define ISO14B_TR1_MIN ETU_TO_SSP(10)
# define ISO14B_TR1_MIN HF14_ETU_TO_SSP(16)
#endif
// Synchronization time (per 14443-2) in ETU
// 25 ETU == 236 µS == 800 SSP_CLK
#ifndef ISO14B_TR1_MAX
# define ISO14B_TR1 ETU_TO_SSP(25)
# define ISO14B_TR1 HF14_ETU_TO_SSP(25)
#endif
// Frame Delay Time PICC to PCD (per 14443-3 Amendment 1) in ETU
// 14 ETU == 132 µS == 448 SSP_CLK
#ifndef ISO14B_TR2
# define ISO14B_TR2 ETU_TO_SSP(14)
# define ISO14B_TR2 HF14_ETU_TO_SSP(14)
#endif
// 4sample
@ -440,7 +448,7 @@ static void Uart14bInit(uint8_t *data) {
// param timeout accepts ETU
static void iso14b_set_timeout(uint32_t timeout_etu) {
uint32_t ssp = ETU_TO_SSP(timeout_etu);
uint32_t ssp = HF14_ETU_TO_SSP(timeout_etu);
if (ssp > MAX_14B_TIMEOUT)
ssp = MAX_14B_TIMEOUT;
@ -1381,7 +1389,7 @@ static int Get14443bAnswerFromTag(uint8_t *response, uint16_t max_len, uint32_t
}
if (Demod.len > 0) {
uint32_t sof_time = *eof_time - ETU_TO_SSP(
uint32_t sof_time = *eof_time - HF14_ETU_TO_SSP(
(Demod.len * (8 + 2)) // time for byte transfers
// + (10) // time for TR1
+ (10 + 2) // time for SOF transfer
@ -1401,7 +1409,6 @@ static void TransmitFor14443b_AsReader(uint32_t *start_time) {
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SEND_SHALLOW_MOD);
// TR2 minimum 14 ETUs
if (*start_time < ISO14B_TR0) {
// *start_time = DELAY_ARM_TO_TAG;
@ -1544,7 +1551,7 @@ static void CodeAndTransmit14443bAsReader(const uint8_t *cmd, int len, uint32_t
// eof_time in ssp clocks, but bits was added here!
// *eof_time = *start_time + (10 * ts->max) + 10 + 2 + 10;
*eof_time = *start_time + ETU_TO_SSP(8 * ts->max);
*eof_time = *start_time + HF14_ETU_TO_SSP(8 * ts->max);
LogTrace(cmd, len, *start_time, *eof_time, NULL, true);
}
@ -1579,7 +1586,13 @@ int iso14443b_apdu(uint8_t const *msg, size_t msg_len, bool send_chaining, void
CodeAndTransmit14443bAsReader(real_cmd, msg_len + 3, &start_time, &eof_time, true);
eof_time += DELAY_ISO14443B_PCD_TO_PICC_READER;
int len = Get14443bAnswerFromTag(rxdata, rxmaxlen, iso14b_timeout, &eof_time);
// Activation frame waiting time
// 65536/fc == 4833 µS
// SSP_CLK = 4833 µS * 3.39 = 16384
int len = Get14443bAnswerFromTag(rxdata, rxmaxlen, iso14b_timeout , &eof_time);
FpgaDisableTracing();
uint8_t *data_bytes = (uint8_t *) rxdata;
@ -1836,7 +1849,7 @@ int iso14443b_select_xrx_card(iso14b_card_select_t *card) {
int slot;
for (slot = 0; slot < 4; slot++) {
start_time = eof_time + ETU_TO_SSP(30); //(24); // next slot after 24 ETU
start_time = eof_time + HF14_ETU_TO_SSP(30); //(24); // next slot after 24 ETU
retlen = Get14443bAnswerFromTag(x_atqb, sizeof(x_atqb), iso14b_timeout, &eof_time);

View file

@ -1116,25 +1116,24 @@ static int CmdAnalyseUnits(const char *Cmd) {
PrintAndLogEx(INFO, " 302 US = %i ETU (expect 32) %s", test, (test == 10) ? _GREEN_("ok") : _RED_("fail"));
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "ETU to US conversions");
double test_us = ETU_TO_US(1);
PrintAndLogEx(INFO, "ETU to Micro seconds (µS) conversions");
double test_us = HF14_ETU_TO_US(1);
PrintAndLogEx(INFO, " 1 ETU = %3.2f US (expect 9.44) %s", test_us, (test_us == 9.44) ? _GREEN_("ok") : _RED_("fail"));
test_us = ETU_TO_US(10);
test_us = HF14_ETU_TO_US(10);
PrintAndLogEx(INFO, " 10 ETU = %4.2f US (expect 94.40) %s", test_us, (test_us == 94.40) ? _GREEN_("ok") : _RED_("fail"));
test_us = ETU_TO_US(32);
test_us = HF14_ETU_TO_US(32);
PrintAndLogEx(INFO, " 32 ETU = %5.2f US (expect 302.06) %s", test_us, (test_us == 320.06) ? _GREEN_("ok") : _RED_("fail"));
PrintAndLogEx(INFO, "ETU in %f us", ONE_ETU_IN_US);
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "US to SSP CLK 3.39MHz conversions");
PrintAndLogEx(INFO, " 9 US = %i SSP (expect 32) ", US_TO_SSP(9));
PrintAndLogEx(INFO, " 10 US = %i SSP (expect 32 or 48) ", US_TO_SSP(10));
PrintAndLogEx(INFO, " 94 US = %i SSP (expect 320) ", US_TO_SSP(94));
PrintAndLogEx(INFO, " 95 US = %i SSP (expect 320 or 336) ", US_TO_SSP(95));
PrintAndLogEx(INFO, " 302 US = %i SSP (expect 1024) ", US_TO_SSP(302));
PrintAndLogEx(INFO, "Microseconds (µS) to SSP CLK 3.39MHz conversions");
PrintAndLogEx(INFO, " 9 µS = %i SSP (expect 32) ", US_TO_SSP(9));
PrintAndLogEx(INFO, " 10 µS = %i SSP (expect 32 or 48) ", US_TO_SSP(10));
PrintAndLogEx(INFO, " 94 µS = %i SSP (expect 320) ", US_TO_SSP(94));
PrintAndLogEx(INFO, " 95 µS = %i SSP (expect 320 or 336) ", US_TO_SSP(95));
PrintAndLogEx(INFO, " 302 µS = %i SSP (expect 1024) ", US_TO_SSP(302));
PrintAndLogEx(INFO, " 4949000 US = %i SSP ", US_TO_SSP(4949000));
PrintAndLogEx(INFO, " 4949000 µS = %i SSP ", US_TO_SSP(4949000));
PrintAndLogEx(NORMAL, "");
@ -1145,22 +1144,21 @@ static int CmdAnalyseUnits(const char *Cmd) {
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "ETU to SSP CLK 3.39MHz conversions");
PrintAndLogEx(INFO, " 1 ETU = %i SSP (expect 32) " _GREEN_("ok"), ETU_TO_SSP(1));
PrintAndLogEx(INFO, " 10 ETU = %i SSP (expect 320) " _GREEN_("ok"), ETU_TO_SSP(10));
PrintAndLogEx(INFO, " 32 ETU = %i SSP (expect 1024) " _GREEN_("ok"), ETU_TO_SSP(32));
PrintAndLogEx(INFO, " 1 ETU = %i SSP (expect 32) " _GREEN_("ok"), HF14_ETU_TO_SSP(1));
PrintAndLogEx(INFO, " 10 ETU = %i SSP (expect 320) " _GREEN_("ok"), HF14_ETU_TO_SSP(10));
PrintAndLogEx(INFO, " 32 ETU = %i SSP (expect 1024) " _GREEN_("ok"), HF14_ETU_TO_SSP(32));
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, "SSP CLK 3.39MHz to ETU conversions");
PrintAndLogEx(INFO, "1024 SSP = %i ETU (expect 32) " _GREEN_("ok"), SSP_TO_ETU(1024));
PrintAndLogEx(INFO, " 320 SSP = %i ETU (expect 10) " _GREEN_("ok"), SSP_TO_ETU(320));
PrintAndLogEx(INFO, " 32 SSP = %i ETU (expect 1) " _GREEN_("ok"), SSP_TO_ETU(32));
} else if (etu) {
PrintAndLogEx(INFO, "1024 SSP = %i ETU (expect 32) " _GREEN_("ok"), HF14_SSP_TO_ETU(1024));
PrintAndLogEx(INFO, " 320 SSP = %i ETU (expect 10) " _GREEN_("ok"), HF14_SSP_TO_ETU(320));
PrintAndLogEx(INFO, " 32 SSP = %i ETU (expect 1) " _GREEN_("ok"), HF14_SSP_TO_ETU(32));
} else if (etu > -1) {
PrintAndLogEx(INFO, " %3.2f ETU = %u us ", ETU_TO_US(etu), 0);
PrintAndLogEx(INFO, " %i ETU = %u SSP ", ETU_TO_SSP(etu), 0);
} else if (us) {
PrintAndLogEx(INFO, " %i us = %u ETU ", US_TO_ETU(us), 0);
PrintAndLogEx(INFO, " %i us = %u SSP ", US_TO_SSP(us), 0);
PrintAndLogEx(INFO, " %i ETU = %3.2f µS", etu, HF14_ETU_TO_US(etu));
PrintAndLogEx(INFO, " %i ETU = %i SSP", etu, HF14_ETU_TO_SSP(etu));
} else if (us > -1) {
PrintAndLogEx(INFO, " %i µS = %3.2f ETU = %u SSP", us, US_TO_ETU(us), US_TO_SSP(us));
}
return PM3_SUCCESS;

View file

@ -65,17 +65,18 @@ typedef struct {
} PACKED iso14b_raw_cmd_t;
#define US_TO_SSP(x) ( (int32_t)((x) * 3.39) )
#define US_TO_SSP(x) ( (int32_t) ((x) * 3.39) )
#define SSP_TO_US(x) ( (int32_t)((x) / 3.39) )
#define ETU_TO_SSP(x) ( (int32_t)((x) * 32) )
#define SSP_TO_ETU(x) ( (int32_t)((x) / 32) )
#define HF14_ETU_TO_SSP(x) ((x) << 5) // 1 ETU = 32 SSP
#define HF14_SSP_TO_ETU(x) ((x) >> 5) //
#define ONE_ETU_IN_US (12800000.0 / 1356000.0)
#define ETU_TO_US(x) (float)(ONE_ETU_IN_US * (x))
#define HF14_ETU_TO_US(x) ( (float)((x) * 9.4396) )
#define HF14_ETU_TO_US_2(x) ( (int32_t)( ((x) * 9439600) / 1000000) )
// #define ETU_TO_US(x) ( (int32_t)( ((x) * 9440000) / 1000000) )
#define US_TO_ETU(x) ( (int32_t)( ((x) * 1000000) / 9440000) )
// #define US_TO_ETU(x) ( (int32_t)( ((x) * 1000000) / 9439600) )
#define US_TO_ETU(x) ( (float)((x) / 9.4396) )
#endif // _ISO14B_H_