From 5f17123f3574629c8211ceed46674958db9eea17 Mon Sep 17 00:00:00 2001 From: "Christian W. Zuckschwerdt" Date: Sun, 15 Dec 2024 15:36:39 +0100 Subject: [PATCH] minor: Add lfsr_digest8_reverse --- include/bit_util.h | 19 ++++++++++++++----- src/bit_util.c | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/include/bit_util.h b/include/bit_util.h index 550967c14..518f76f0d 100644 --- a/include/bit_util.h +++ b/include/bit_util.h @@ -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); diff --git a/src/bit_util.c b/src/bit_util.c index fad3b4af3..a1b9f5eb9 100644 --- a/src/bit_util.c +++ b/src/bit_util.c @@ -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; @@ -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; @@ -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