Identify the checksum algorithm

Hey Guys,

I've been asked to write a technical document describing a protocol. Although I wrote most of it, the underlying structure ie header, and checksum was already in existence. In the technical documentation, I would like to describe the type of checksum used but from the code, I can't determine what it is. It looks like it might be a variant of Fletcher-16 using a modulus of 255, but I'm not sure.

Any ideas? The code is below and the processor is big-endian.

Thanks!

unsigned short calc_checksum(void *data, int count) {

unsigned char *checksum_ptr; unsigned short tx_sum1 = 0; unsigned short tx_sum2 = 0; unsigned char csum1; unsigned char csum2;

for (checksum_ptr = data; checksum_ptr < data + count; checksum_ptr++) { tx_sum1 += *checksum_ptr; if (tx_sum1 >= 255) { tx_sum1 -= 255; } tx_sum2 += tx_sum1; }

tx_sum2 %= 255; csum1 = (255 - ((tx_sum1 + tx_sum2) % 255)); csum2 = (255 - ((tx_sum1 + csum1) % 255)); return (unsigned short)csum1

Reply to
jeholza
Loading thread data ...

d

checksum_ptr++) =A0 =A0 =A0

It does look like Fletcher-16 with a modulus of 255 - although that's pretty common, and somewhat stronger than mod 256.

But why not include a code sample in your documentation and/or a test vector or two?

Reply to
robertwessel2

d

checksum_ptr++) =A0 =A0 =A0

Wait - I wasn't paying enough attention. While the accumulate phase is Fletcher, the way the values are combined is odd, and that shift on the return has to be wrong (csum1 is getting shift completely off the edge of the world).

Reply to
robertwessel2

woul=

Fletcher-16

=

Sorry that's my bad. The checksum is actually calculated in three different functions and I combined them into one in the post to make it easier to read, but I wrote the last line wrong.

Should be: return (unsigned short)csum1

Reply to
jeholza

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.