Refactor lfsr_digest16
This commit is contained in:
parent
5dba8f5cc6
commit
dc1577bd3f
4 changed files with 24 additions and 22 deletions
|
@ -151,12 +151,12 @@ uint8_t lfsr_digest8_reflect(uint8_t const message[], int bytes, uint8_t gen, ui
|
|||
|
||||
/// Digest-16 by "LFSR-based Toeplitz hash".
|
||||
///
|
||||
/// @param data up to 32 bits data, LSB aligned
|
||||
/// @param bits number of bits to digest
|
||||
/// @param message bytes of message data
|
||||
/// @param bytes number of bytes to digest
|
||||
/// @param gen key stream generator, needs to includes the MSB if the LFSR is rolling
|
||||
/// @param key initial key
|
||||
/// @return digest value
|
||||
uint16_t lfsr_digest16(uint32_t data, int bits, uint16_t gen, uint16_t key);
|
||||
uint16_t lfsr_digest16(uint8_t const message[], unsigned bytes, uint16_t gen, uint16_t key);
|
||||
|
||||
/// Compute bit parity of a single byte (8 bits).
|
||||
///
|
||||
|
|
|
@ -80,15 +80,15 @@ static int maverick_et73x_callback(r_device *decoder, bitbuffer_t *bitbuffer)
|
|||
else if (flags == 7)
|
||||
status = "init";
|
||||
|
||||
//bitbuffer_extract_bytes(bitbuffer, row, 12, b, 24);
|
||||
uint32_t chk_data = (b[1] & 0x0f) << 20 | b[2] << 12 | b[3] << 4 | b[4] >> 4;
|
||||
uint8_t chk[3];
|
||||
bitbuffer_extract_bytes(&mc, 0, 12, chk, 24);
|
||||
|
||||
//digest is used to represent a session. This means, we get a new id if a reset or battery exchange is done.
|
||||
int id = lfsr_digest16(chk_data, 24, 0x8810, 0xdd38) ^ digest;
|
||||
int id = lfsr_digest16(chk, 3, 0x8810, 0xdd38) ^ digest;
|
||||
|
||||
if (decoder->verbose)
|
||||
fprintf(stderr, "%s: pre %03x, flags %0x, t1 %d, t2 %d, digest %04x, chk_data %06x, digest xor'ed: %04x\n",
|
||||
__func__, pre, flags, temp1, temp2, digest, chk_data, id);
|
||||
fprintf(stderr, "%s: pre %03x, flags %0x, t1 %d, t2 %d, digest %04x, chk_data %02x%02x%02x, digest xor'ed: %04x\n",
|
||||
__func__, pre, flags, temp1, temp2, digest, chk[0], chk[1], chk[2], id);
|
||||
|
||||
data = data_make(
|
||||
"model", "", DATA_STRING, "Maverick-ET73x",
|
||||
|
|
|
@ -76,9 +76,8 @@ static int tfa_303196_callback(r_device *decoder, bitbuffer_t *bitbuffer)
|
|||
if (b[0] != 0xa8)
|
||||
return DECODE_FAIL_SANITY;
|
||||
|
||||
uint32_t chk_data = ((unsigned)b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
|
||||
uint16_t digest = (b[4] << 8) | (b[5]);
|
||||
int chk = lfsr_digest16(chk_data, 32, 0x8810, 0x22d0) ^ digest;
|
||||
int chk = lfsr_digest16(b, 4, 0x8810, 0x22d0) ^ digest;
|
||||
|
||||
//bitrow_printf(b, 48, "TFA-303196 (%08x %04x %04x): ", chk_data, digest, session);
|
||||
|
||||
|
|
27
src/util.c
27
src/util.c
|
@ -256,21 +256,24 @@ uint8_t lfsr_digest8_reflect(uint8_t const message[], int bytes, uint8_t gen, ui
|
|||
return sum;
|
||||
}
|
||||
|
||||
uint16_t lfsr_digest16(uint32_t data, int bits, uint16_t gen, uint16_t key)
|
||||
uint16_t lfsr_digest16(uint8_t const message[], unsigned bytes, uint16_t gen, uint16_t key)
|
||||
{
|
||||
uint16_t sum = 0;
|
||||
for (int bit = bits - 1; bit >= 0; --bit) {
|
||||
// fprintf(stderr, "key at bit %d : %04x\n", bit, key);
|
||||
// if data bit is set then xor with key
|
||||
if ((data >> bit) & 1)
|
||||
sum ^= key;
|
||||
for (unsigned k = 0; k < bytes; ++k) {
|
||||
uint8_t data = message[k];
|
||||
for (int i = 7; i >= 0; --i) {
|
||||
// fprintf(stderr, "key at bit %d : %04x\n", i, key);
|
||||
// if data bit is set then xor with key
|
||||
if ((data >> i) & 1)
|
||||
sum ^= key;
|
||||
|
||||
// roll the key right (actually the lsb is dropped here)
|
||||
// and apply the gen (needs to include the dropped lsb as msb)
|
||||
if (key & 1)
|
||||
key = (key >> 1) ^ gen;
|
||||
else
|
||||
key = (key >> 1);
|
||||
// roll the key right (actually the lsb is dropped here)
|
||||
// and apply the gen (needs to include the dropped lsb as msb)
|
||||
if (key & 1)
|
||||
key = (key >> 1) ^ gen;
|
||||
else
|
||||
key = (key >> 1);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue