1

Is it possible to have a crc16 implementation that accept different polynomial ? I mean by different polynomial the same function that can calculate crc16 with, one time, polynomial 0xA001 and an other time polynomial 0x1D0F (for exemple).

I have this code, in C, that work fine for little endian, with modbus polynomial (0xA001). I choose to initialise crc 16 at 0xFFFF :

uint16_t _crc16_update(uint8_t *Trame, uint32_t NbOctets, uint16_t Polynome) 
{
  uint16_t u16CRC;

  int_t i;
  int_t j;

  u16CRC = 0xFFFF;

  for (i = NbOctets - 1; i >= 0; i--)
  {
    u16CRC = u16CRC ^ Trame[i]; 
    for (j = 0; j < 8; ++j) 
    { 
      if (u16CRC & 1) 
      u16CRC = (u16CRC >> 1) ^ Polynome; 
      else 
      u16CRC = (u16CRC >> 1); 
    } 
  }

  return u16CRC; 
} 

But when I execute this code with other polynomial, as 0x1D0F, result is wrong regards on this crc 16 online calculator.

Am I trying to do something impossible ?

romaric crailox
  • 564
  • 1
  • 3
  • 15

1 Answers1

1

Is it possible to have a crc16 implementation that accept different polynomial ?

No. The CRC generators must use the same bits at every step of the algorithm or the outputs will not match. See https://en.wikipedia.org/wiki/Cyclic_redundancy_check for a description.

jwdonahue
  • 6,199
  • 2
  • 21
  • 43
  • this is more of a comment than an answer. – Ahmed Masud Dec 05 '17 at 18:12
  • 1
    Edited. Any better? – jwdonahue Dec 05 '17 at 18:18
  • yes.. :-) but a more explanation would be helpful, like *why* it's not possible for different polynomials to be used. – Ahmed Masud Dec 05 '17 at 18:23
  • 1
    If the guzintas don't match, the comzoutas won't either. – jwdonahue Dec 05 '17 at 18:28
  • @jwdonahue I am not sure to understand : I don't want to change polynomial during crc16 calculation but I want to to calculate crc16 one time with one polynomial and an other time with an other polynomial. Because when I read link you share, I understand that polynomial can be a crc16 calculation function variable. So that the answer to "Is it possible to have a crc16 implementation that accept different polynomial" is yes. Can you help me to understand why your answer to this question is no please ? – romaric crailox Dec 06 '17 at 10:51
  • 1
    @romariccrailox, of course. You can use any polynomial you like, they aren't all equally strong, but go for it, just don't expect anybody else's CRC16 to come up with the same check code. Different polynomials, different outputs. That's why there's only a handful of them in common use today. Your reference to your result not being the same as the online crc16 calculator was part of the question right? – jwdonahue Dec 07 '17 at 05:30
  • Ok I understand. But when I say that I have not the same result with crc16 online calculator I mean at equal polynomial. I understand that polynomial are not all equally strong, but I have to do a function that can calculate crc16 with user defined polynomial. And my question is if it is possible, with the same function implementation and a polynomial as a entry parameter, to calculate a crc16 that will able to be use by another device to check validity of data. – romaric crailox Dec 07 '17 at 08:58
  • 1
    @romariccrailox, yes, but they have to be using the exact same polynomial and the accumulator has to be initialized to exactly the same value on both ends. – jwdonahue Dec 14 '17 at 17:25
  • Thx, I understand better with these precisions – romaric crailox Dec 15 '17 at 15:37