What kind of CRC16 is this?

77 Views Asked by At

I'm currently processing an AX.25 packet. When I try to calculate the CRC/FCS myself, my result and the packet's are always different.

bitstream: 1001101010000010100010001000101010101010101001100110000010000010100001101001111010011010011000100110110001101001000000111111000001110100011010000110100101110011001000000110100101110011001000000110000100100000011101000110010101110011011101000010000001101101011001010111001101110011011000010110011101100101001011100000101011000101

Hex: 9A 82 88 8A AA A6 60 82 86 9E 9A 62 6C 69 03 F0 74 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6D 65 73 73 61 67 65 2E 0A C5

The CRC is the last 16 bits: 0000101011000101

However, I'm a bit confused about whether to reverse each byte or not since AX.25 packets are sent LSB first.

  1. I've tried to calculate the CRC using an online calculator. None of them fits.

input: 9A 82 88 8A AA A6 60 82 86 9E 9A 62 6C 69 03 F0 74 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6D 65 73 73 61 67 65 2E

  1. I've tried using python's crcmod.
import crcmod

input = b"\x9A\x82\x88\x8A\xAA\xA6\x60\x82\x86\x9E\x9A\x62\x6C\x69\x03\xF0\x74\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74\x20\x6D\x65\x73\x73\x61\x67\x65\x2E"
print(input)

ploy_coeff = 0b10001000000100001
k = crcmod.Crc(poly=ploy_coeff, initCrc=0xFFFF, rev=True, xorOut=0xFFFF)
k.digest_size = 4

k.update(input)
print(hex(k.crcValue))

output is 0x9565

I'd like to know how to get the correct CRC value, which bytes need to be reversed.

2

There are 2 best solutions below

0
Mark Adler On

Python's crcmod defines the initial value differently than the usual convention. You need initCrc=0. Then you get the CRC you are expecting.

1
quamrana On

There might be a mistake in the online calculator giving the wrong init value for the crc for X-25. On the crcmod website for pre-defined algorithms it give the init value as 0:

import crcmod

input = b"\x9A\x82\x88\x8A\xAA\xA6\x60\x82\x86\x9E\x9A\x62\x6C\x69\x03\xF0\x74\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74\x20\x6D\x65\x73\x73\x61\x67\x65\x2E"
print(input)

poly_coeff=0x11021
k = crcmod.Crc(poly=poly_coeff, initCrc=0x0, rev=True, xorOut=0xFFFF)

k.update(input)
print(hex(k.crcValue))

Output: 0xc50a