Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Wireshark-dev: Re: [Wireshark-dev] Calculating CRC5 of 11-bit data

From: John Sullivan <jsethdev@xxxxxxxxxxxxxx>
Date: Sun, 21 Jul 2019 19:05:35 +0100
On Sunday, July 21, 2019, 6:37:48 AM, Tomasz Mon wrote:
> On Sat, Jul 20, 2019 at 8:42 PM John Sullivan <jsethdev@xxxxxxxxxxxxxx> wrote:
>> But here we have a fixed 11 bit input

> I am sorry, but I have completely missed out there there are two
> places where CRC5 is calculated:
> * In SETUP, OUT, IN, PING and SOF the CRC5 is calculated on 11 bits
> * In SPLIT the CRC5 is calculated on 19 bits

Ok. That's still only two shortish options.

>> static const char ival = 0x08;

> This looks like swapped bit order to me. The CRC5 value for 11-bits of
> 0 is 0x02.

Yes, I was using the USB docs convention which always lists values in
that order (lsbit first, wire transmission order).

If the internal representation is in the sane order, then it's a
simple matter to flip the table. (It simplifies the processing a tiny
bit too.) Here is a version which handles 11 or 19 bit input and
returns the value in opposite order (it also assumes the input is
presented in the opposite order compared to the first version):

int crc5(int v, int vl)
{
  // Only 0x02 and 0x1d really required, for 11 or 19 bit input
  static const char ivals[20] = {
    0x00, 0x04, 0x06, 0x07, 0x13, 0x19, 0x1c, 0x0a,
    0x01, 0x10, 0x0c, 0x02, 0x05, 0x12, 0x0d, 0x16,
    0x0f, 0x17, 0x1b, 0x1d
  };
  static const char bvals[19] = {
    0x1e, 0x15, 0x03, 0x06, 0x0c, 0x18, 0x19, 0x1b,
    0x1f, 0x17, 0x07, 0x0e, 0x1c, 0x11, 0x0b, 0x16,
    0x05, 0x0a, 0x14
  };

  int rv = ivals[vl];
  for ( int i=0 ; i<vl ; i++ ) {
    if (v & (1<<i)) rv ^= bvals[19-vl+i];
  }
  return rv;
}

Again, I've cross-checked the output with the examples/code at:

https://www.usb.org/sites/default/files/crcdes.pdf


John
-- 
Dead stars still burn