Identify the checksum algorithm

Jan 03, 2011 3 Replies

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


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?

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).

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

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required