Skip to content

Commit

Permalink
minor: Add lfsr_digest8_reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Dec 15, 2024
1 parent 5483757 commit 5f17123
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
19 changes: 14 additions & 5 deletions include/bit_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,29 @@ uint16_t crc16lsb(uint8_t const message[], unsigned nBytes, uint16_t polynomial,
/// @return CRC value
uint16_t crc16(uint8_t const message[], unsigned nBytes, uint16_t polynomial, uint16_t init);

/// Digest-8 by "LFSR-based Toeplitz hash".
/// Digest-8 by "LFSR-based Toeplitz hash", bits MSB to LSB.
///
/// @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 gen key stream generator, needs to includes the MSB for ROR if the LFSR is rolling
/// @param key initial key
/// @return digest value
uint8_t lfsr_digest8(uint8_t const message[], unsigned bytes, uint8_t gen, uint8_t key);

/// Digest-8 by "LFSR-based Toeplitz hash", byte reflect, bit reflect.
/// Digest-8 by "LFSR-based Toeplitz hash", byte reversed, bits MSB to LSB.
///
/// @param message bytes of message data
/// @param message bytes of message data, read in reverse
/// @param bytes number of bytes to digest
/// @param gen key stream generator, needs to includes the MSB if the LFSR is rolling
/// @param gen key stream generator, needs to includes the MSB for ROR if the LFSR is rolling
/// @param key initial key
/// @return digest value
uint8_t lfsr_digest8_reverse(uint8_t const message[], int bytes, uint8_t gen, uint8_t key);

/// Digest-8 by "LFSR-based Toeplitz hash", byte reversed, bit reflect (LSB to MSB).
///
/// @param message bytes of message data, read in reverse
/// @param bytes number of bytes to digest
/// @param gen key stream generator, needs to includes the LSB for ROL if the LFSR is rolling
/// @param key initial key
/// @return digest value
uint8_t lfsr_digest8_reflect(uint8_t const message[], int bytes, uint8_t gen, uint8_t key);
Expand Down
35 changes: 31 additions & 4 deletions src/bit_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,12 @@ uint16_t crc16(uint8_t const message[], unsigned nBytes, uint16_t polynomial, ui
uint8_t lfsr_digest8(uint8_t const message[], unsigned bytes, uint8_t gen, uint8_t key)
{
uint8_t sum = 0;
// Process message from first byte to last byte
for (unsigned k = 0; k < bytes; ++k) {
uint8_t data = message[k];
// Process individual bits of each byte (MSB to LSB)
for (int i = 7; i >= 0; --i) {
// fprintf(stderr, "key is %02x\n", key);
// fprintf(stderr, "key at %d.%d : %02x\n", k, i, key);
// XOR key into sum if data bit is set
if ((data >> i) & 1)
sum ^= key;
Expand All @@ -322,6 +324,31 @@ uint8_t lfsr_digest8(uint8_t const message[], unsigned bytes, uint8_t gen, uint8
return sum;
}

uint8_t lfsr_digest8_reverse(uint8_t const *message, int bytes, uint8_t gen, uint8_t key)
{
uint8_t sum = 0;
// Process message from last byte to first byte (reflected)
for (int k = bytes - 1; k >= 0; --k) {
uint8_t data = message[k];
// Process individual bits of each byte (MSB to LSB)
for (int i = 7; i >= 0; --i) {
// fprintf(stderr, "key at %d.%d : %02x\n", k, i, key);
// XOR key into sum if data bit is set
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);
}
}
return sum;
}

uint8_t lfsr_digest8_reflect(uint8_t const message[], int bytes, uint8_t gen, uint8_t key)
{
uint8_t sum = 0;
Expand All @@ -330,14 +357,14 @@ uint8_t lfsr_digest8_reflect(uint8_t const message[], int bytes, uint8_t gen, ui
uint8_t data = message[k];
// Process individual bits of each byte (reflected)
for (int i = 0; i < 8; ++i) {
// fprintf(stderr, "key is %02x\n", key);
// fprintf(stderr, "key at %d.%d : %02x\n", k, i, key);
// XOR key into sum if data bit is set
if ((data >> i) & 1) {
sum ^= key;
}

// roll the key left (actually the lsb is dropped here)
// and apply the gen (needs to include the dropped lsb as msb)
// roll the key left (actually the msb is dropped here)
// and apply the gen (needs to include the dropped msb as lsb)
if (key & 0x80)
key = (key << 1) ^ gen;
else
Expand Down

0 comments on commit 5f17123

Please sign in to comment.