# Few Things about CRC

...that you might need to know or maybe just forgot

02-Nov-2010 09:01

This will be a bit long, longer then I initially expected.

First let's have a look at typical table-based CRC-32 implementation:

- uint32_t calcCrc32(const uint8_t *buf, size_t len) {
- uint32_t rCrc = 0xffffffff;
- size_t n;
- for (n = 0; n < len; ++n) {
- rCrc = crc_table[(rCrc ^ buf[n]) & 0xff] ^ (rCrc >> 8);
- }
- return rCrc ^ 0xffffffff;
- }

This is similar to the implementation that can be found in PNG Specification

But for a few experiments the following implementation will be more suitable. The only difference, is
that instead of using constant values of 0xffffffff, we'll pass them as function arguments.

- uint32_t crc32(const void* buf, size_t bufLen, uint32_t initial, uint32_t final) {
- const uint8_t* data = (const uint8_t*)buf;
- uint32_t rCrc = initial;
- for (size_t i = 0; i < bufLen; ++i) {
- uint32_t bytesCrc = crcTable[(rCrc ^ data[i]) & 0xff];
- rCrc = (rCrc >> 8) ^ bytesCrc;
- }
- return rCrc ^ final;
- }

As you probably know CRC is the *remainder* polynomial resulting from 'dividing' shifted message's polynomial by some chosen generetor polynomial.

(I'll be talking about 32-bit CRC and I was using the same polynomial that png uses 0xedb88320, but the polynomial itself doesn't change anything that's discussed below.)

### The need of preset

Normally to get the *remainder* crc would be called like: crc32(buffer, buLen, 0, 0). But this causes two problems.

First problem is that if you *prepend the message* with any number of *zeroes* it won't change the resulting reminder,
because this will prepend message polynomial with some 0 coefficients, and that obviously won't change the reminder itself.

Below is an example. It creates such messages, calculates crcs and prints them.

- uint8_t mesg[12] = { 0 };
- size_t len;
- for (len = 0; len < 10; ++len) {
- if (len) mesg[len-1] = 0;
- mesg[len] = 0xa5;
- c1 = crc32 (mesg, len+1, 0, 0);
- printf ("%2d %#08x\n", len+1, c1);
- }

And the results:

- 1 0xa6bc5767
- 2 0xa6bc5767
- 3 0xa6bc5767
- 4 0xa6bc5767
- 5 0xa6bc5767
- 6 0xa6bc5767
- 7 0xa6bc5767
- 8 0xa6bc5767
- 9 0xa6bc5767
- 10 0xa6bc5767

This is solved by preseting the CRC, but before we'll get to that, let's discuss the second problem.

### The need of post-invert

Instead of calculating the CRC on some buffer and then verifying it, it's easier to append CRC after the message, calculate CRC of this new message, the result should be*0*.

(It's quite easy to understand why it's so, but guys at this wiki page already did that, so I'm not going to duplicate it :))

Here's an example:

- uint8_t mesg[12] = { 0 };
- size_t len;
- /* fill the buffer with some dummy data */
- for (len = 0; len < 6; ++len)
- mesg[len] = len;
- /* appending crc */
- c1 = crc32 (mesg, 6, 0, 0);
- *(uint32_t*)(mesg+6) = c1;
- printf ("10 %#08x\n", crc32(mesg, 10, 0, 0));

This brings us to the second problem, if we *append* any zeroes to message constructed as above, the reminder will still be 0.

And because mesg above has size of 12, and it's initially filled with zeroes, the following two lines, will also print 0x00000000 as a resulting CRC

- printf ("11 %#08x\n", crc32(poly, 11, 0, 0));
- printf ("12 %#08x\n", crc32(poly, 12, 0, 0));

That's also quite *easy to understand*. If the resulting reminder is 0 it means that the message polynomial was multiple of our generator,
so appending any number of zero coefficients will give a new message that is still mutiple of a generator

### Post-invert

The second problem is usually 'fixed' by negating CRC result. In this case that will be calling: crc32(buffer, buLen, 0, 0xffffffff)

So let's fix last example by appending negated CRC:

- c1 = crc32 (mesg, 6, 0, 0xffffffff);
- *(uint32_t*)(mesg+6) = c1;
- printf ("inverted crc: %08x\n", c1);

Of course now the calculation of CRC for that new message won't return 0, so we should calculate the CRC for the first 6 bytes and compare it with appended CRC
or... we could calculate so called *reciprocal polynomial* and as before calculate the CRC for whole the message and *compare it with our reciprocal*

- /* calculate reciprocal polynomial */
- uint32_t test = 0xffffffff;
- c1 = crc32 (&test, sizeof(test), 0, 0xffffffff);
- printf ("reciprocal: %08x\n", c1);
- printf ("10 %#08x\n", crc32(poly, 10, 0, 0xffffffff));
- printf ("11 %#08x\n", crc32(poly, 11, 0, 0xffffffff));
- printf ("12 %#08x\n", crc32(poly, 12, 0, 0xffffffff));

And here are the results, as you can see the calculated CRCs are no longer the same.

- reciprocal: 0x2144df1c
- 10 0x2144df1c
- 11 0xc622f71d
- 12 0xb1c2a1a3

### Preset to -1

Now back to the first problem which is usually 'fixed' by preseting CRC to -1 (0xffffffff) instead of 0.

This is equivalent to the calling: crc32(buffer, bufLen, 0xffffffff, 0). The following code is almost identical to the
first one presented, the only difference is using the preset value in call to crc32.

- uint8_t mesg[12] = { 0 };
- size_t len;
- for (len = 0; len < 10; ++len) {
- if (len) mesg[len-1] = 0;
- mesg[len] = 0xa5;
- c1 = crc32 (mesg, len+1, 0xffffffff, 0);
- printf ("%2d %#08x\n", len+1, c1);
- }

And coresponding results:

- 1 0x8b414715
- 2 0x189aba67
- 3 0xa602718a
- 4 0x78077784
- 5 0x9f615f85
- 6 0xe881093b
- 7 0xc42f77e6
- 8 0x3c6177f1
- 9 0xbf4abc36
- 10 0xbac9c0ee

### Preset and post-invert

Preset and post-invert are usually used together. First important observation is that 0xffffffff becomes fixed point of a crc32

- test = 0xffffffff;
- c1 = crc32(&test, sizeof(test), 0xffffffff, 0xffffffff);
- printf ("fp : %#08x\n", c1);

0xffffffff is printed to the screen. As you've probably noted this is also reciprocal.

Now take a look at crc32 function, now back here, now back at crc32 function, now back here. Just before returning the value it's xored with 0xffffffff value (or you can say inverted).

That tells us, that after finishing the loop, the value of *rCrc* variable had to be 0.

That isn't too good, because it basically means, that we can again *append any numbers of zeroes*, after that value and the result won't change

- uint8_t mesg[12] = { 0 };
- size_t len;
- *(uint32_t*)(mesg) = 0xffffffff;
- for (len = 4; len < 10; ++len) {
- if (len > 4) mesg[len-1] = 0;
- mesg[len] = 0xa5;
- c1 = crc32 (mesg, len+1, 0xffffffff, 0xffffffff);
- printf ("%2d %08x\n", len+1, c1);
- }

### Final stuff

...or because we know that reciprocal has the same value, we can append calculated CRC after the message. We need to negate the calculated CRC first. Here's the thing:

- memset(poly, 0, sizeof(poly));
- /* prepare dummy buffer */
- for (len = 0; len < 5; ++len)
- poly[len] = len+66;
- /* add NEGATED crc */
- c1 = crc32 (poly, 5, 0xffffffff, 0xffffffff);
- *(uint32_t*)(poly+5) = ~c1;
- printf ("09 %#08x\n", crc32(poly, 9, 0xffffffff, 0xffffffff));
- printf ("10 %#08x\n", crc32(poly, 10, 0xffffffff, 0xffffffff));
- printf ("11 %#08x\n", crc32(poly, 11, 0xffffffff, 0xffffffff));
- printf ("12 %#08x\n", crc32(poly, 12, 0xffffffff, 0xffffffff));
- printf ("\n");
- for (len = 9; len < 12; ++len) {
- if (len > 9) poly[len-1] = 0;
- poly[len] = 0xa5;
- c1 = crc32 (poly, len+1, 0xffffffff, 0xffffffff);
- printf ("%2d %08x\n", len+1, c1);
- }

...and the results:

- 09 0xffffffff
- 10 0xffffffff
- 11 0xffffffff
- 12 0xffffffff
- 10 0x5943a898
- 11 0x5943a898
- 12 0x5943a898