Skip to content

[math] Fix crc8_ccitt discarding the MSB of every input byte - #1371

Open
TudorFanaru18 wants to merge 1 commit into
modm-io:developfrom
TudorFanaru18:fix/crc8-ccitt-msb
Open

[math] Fix crc8_ccitt discarding the MSB of every input byte#1371
TudorFanaru18 wants to merge 1 commit into
modm-io:developfrom
TudorFanaru18:fix/crc8-ccitt-msb

Conversation

@TudorFanaru18

Copy link
Copy Markdown

Draft: CI fails (by design). The 5 failing assertions in communication/amnb/message_test.cpp are the subject of #1370 : AMNB's test fixtures encode the uncorrected CRC values, so fixing the CRC changes AMNB header CRCs on the wire.
Whether to update the fixtures, note it in the changelog, or bump a protocol version is the open question.

Fixes #1370

The non-AVR path transliterates the avr-libc inline assembly literally:

    1:  lsl    %0        ; shift left, MSB -> CARRY
        brcc   2f        ; branch on CARRY, i.e. the *pre-shift* MSB
        eor    %0, %2

`lsl` preserves the departing bit in the carry flag, so `brcc` tests bit 7
of the value before the shift. C has no carry flag: `data <<= 1` on a
uint8_t discards that bit, so the following `if (data & 0x80)` tests the
pre-shift bit 6 instead and bit 7 is never examined.

Over all 32768 (crc, data) pairs this made

    crc8_ccitt_update(crc, data) == crc8_ccitt_update(crc, data ^ 0x80)

hold universally, left only 128 of 256 output values reachable, and made
single-bit errors in bit 7 of any byte undetectable.

Adds crc_test, which the module picks up by glob. There were no tests for
this header, which is why the fault went unnoticed; the new suite pins the
published check values for all three functions and asserts that flipping
the MSB of an input byte changes the CRC-8.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

crc8_ccitt discards the MSB of every input byte on non-AVR targets

1 participant