Manuel
Manuel

Reputation: 382

Reverse engineering "checksum" generation

I've been analyzing some 230V communication and trying to figure out the checksum algorithm of the protocol. From what I've learned the communication structure is the following:

I'm not sure if the checksum is only calculated over the data or the start structure as well (which is 910h in HEX)

I've attached some communication messages, each row is one message. I've tried algorithms like CheckSum8 Xor, CheckSum8 Modulo 256 and CheckSum8 2s Complement; unfortunately without any luck.

| Start structure |          Data          | ChkSum |
 00000100100010000 1010 1010 1010 1010 1011 10001100
 00000100100010000 1010 1010 1010 1010 1001 01111100
 00000100100010000 1010 1010 1010 1010 1101 01110010
 00000100100010000 1010 1010 1010 1010 0101 01101110
 00000100100010000 1010 1010 1010 1011 0101 01010110
 00000100100010000 1010 1010 1010 1001 0101 00100110
 00000100100010000 1010 1010 1010 1101 0101 11000110
 00000100100010000 1010 1010 1010 0101 0101 11101000
 00000100100010000 1010 1010 1011 0101 0101 10110100
 00000100100010000 1010 1010 1001 0101 0101 00001100
 00000100100010000 1010 1010 1101 0101 0101 10010010
 00000100100010000 1010 1010 0101 0101 0101 01000000
 00000100100010000 1010 1011 0101 0101 0101 00001010
 00000100100010000 1010 1001 0101 0101 0101 10011110
 00000100100010000 1010 1101 0101 0101 0101 01011000
 00000100100010000 1010 0101 0101 0101 0101 00111010
 00000100100010000 1011 0101 0101 0101 0101 11111110
 00000100100010000 1001 0101 0101 0101 0101 10011000
 00000100100010000 1101 0101 0101 0101 0101 01010100
 00000100100010000 0101 0101 0101 0101 0101 00100010

UPDATE: I've made a much further progress now, and I'm currently no longer sure if it's a checksum or a CRC at all. I've generated and sent the data value from 0 to 34 and the system generated the following "checksums"

What I've learned so far:

The HEX-Column at the end is the Checksum value in HEX

     | Start structure |          Data           | ?ChkSum?   | HEX |
00    00000100100010000 0000 0000 0000 0000 0000   1011 1000    B8
01    00000100100010000 0000 0000 0000 0000 0001   1011 0010    B2
02    00000100100010000 0000 0000 0000 0000 0010   0100 1000    48
03    00000100100010000 0000 0000 0000 0000 0011   0100 0010    42
04    00000100100010000 0000 0000 0000 0000 0100   1011 0110    B6
05    00000100100010000 0000 0000 0000 0000 0101   1011 1100    BC
06    00000100100010000 0000 0000 0000 0000 0110   0100 0110    46
07    00000100100010000 0000 0000 0000 0000 0111   0100 1100    4C
08    00000100100010000 0000 0000 0000 0000 1000   1010 0100    A4
09    00000100100010000 0000 0000 0000 0000 1001   1010 1110    AE
10    00000100100010000 0000 0000 0000 0000 1010   0101 0100    54
11    00000100100010000 0000 0000 0000 0000 1011   0101 1110    5E
12    00000100100010000 0000 0000 0000 0000 1100   1010 1010    AA
13    00000100100010000 0000 0000 0000 0000 1101   1010 0000    A0
14    00000100100010000 0000 0000 0000 0000 1110   0101 1010    5A
15    00000100100010000 0000 0000 0000 0000 1111   0101 0000    50
16    00000100100010000 0000 0000 0000 0001 0000   1000 0000    80
17    00000100100010000 0000 0000 0000 0001 0001   1000 1010    8A
18    00000100100010000 0000 0000 0000 0001 0010   0111 0000    70
19    00000100100010000 0000 0000 0000 0001 0011   0111 1010    7A
20    00000100100010000 0000 0000 0000 0001 0100   1000 1110    8E
21    00000100100010000 0000 0000 0000 0001 0101   1000 0100    84
22    00000100100010000 0000 0000 0000 0001 0110   0111 1110    7E
23    00000100100010000 0000 0000 0000 0001 0111   0111 0100    74
24    00000100100010000 0000 0000 0000 0001 1000   1001 1100    9C
25    00000100100010000 0000 0000 0000 0001 1001   1001 0110    96
26    00000100100010000 0000 0000 0000 0001 1010   0110 1100    6C
27    00000100100010000 0000 0000 0000 0001 1011   0110 0110    66
28    00000100100010000 0000 0000 0000 0001 1100   1001 0010    92
29    00000100100010000 0000 0000 0000 0001 1101   1001 1000    98
30    00000100100010000 0000 0000 0000 0001 1110   0110 0010    62
31    00000100100010000 0000 0000 0000 0001 1111   0110 1000    68
32    00000100100010000 0000 0000 0000 0010 0000   1100 1000    C8
33    00000100100010000 0000 0000 0000 0010 0001   1100 0010    C2
34    00000100100010000 0000 0000 0000 0010 0010   0011 1000    38

UPADTE 2: I've generated now the data, where only one bit of the data structure is 1

| Dec  | Start structure |          Data           | ?ChkSum?   | HEX |
00      00000100100010000 0000 0000 0000 0000 0000   1011 1000    B8
01      00000100100010000 0000 0000 0000 0000 0001   1011 0010    B2
02      00000100100010000 0000 0000 0000 0000 0010   0100 1000    48
04      00000100100010000 0000 0000 0000 0000 0100   1011 0110    B6
08      00000100100010000 0000 0000 0000 0000 1000   1010 0100    A4
16      00000100100010000 0000 0000 0000 0001 0000   1000 0000    80
32      00000100100010000 0000 0000 0000 0010 0000   1100 1000    C8
64      00000100100010000 0000 0000 0000 0100 0000   0101 1000    58
128     00000100100010000 0000 0000 0000 1000 0000   1001 0110    96
256     00000100100010000 0000 0000 0001 0000 0000   1110 0100    E4
512     00000100100010000 0000 0000 0010 0000 0000   0000 0000    00
1024    00000100100010000 0000 0000 0100 0000 0000   0010 0110    26
2048    00000100100010000 0000 0000 1000 0000 0000   0110 1010    6A
4096    00000100100010000 0000 0001 0000 0000 0000   1111 0010    F2
8192    00000100100010000 0000 0010 0000 0000 0000   0010 1100    2C
16384   00000100100010000 0000 0100 0000 0000 0000   0111 1110    7E
32768   00000100100010000 0000 1000 0000 0000 0000   1101 1010    DA
65536   00000100100010000 0001 0000 0000 0000 0000   0111 1100    7C
131072  00000100100010000 0010 0000 0000 0000 0000   1101 1110    DE
262144  00000100100010000 0100 0000 0000 0000 0000   0111 0100    74
524288  00000100100010000 1000 0000 0000 0000 0000   1100 1110    CE

Upvotes: 2

Views: 904

Answers (1)

DaveHowell
DaveHowell

Reputation: 1264

I think I've cracked this one. Here's how it works:

  1. Start with your 20 input bits and an initial running result of 0b10111000 (0x8B).

  2. There is a constant 8-bit mask for each of the 20 input bits. For each input bits with a 1 value, you want to XOR the current running result with the 8-bit mask for that bit.

The solution in C:

const UINT8 bitMasks[20] = {
  0b00001010,
  0b11110000,
  0b00001110,
  0b00011100,
  0b00111000,
  0b01110000,
  0b11100000,
  0b00101110,
  0b01011100,
  0b10111000,
  0b10011110,
  0b11010010,
  0b01001010,
  0b10010100,
  0b11000110,
  0b01100010,
  0b11000100,
  0b01100110,
  0b11011100,
  0b01110110
};

UINT8 ComputeWeirdHash(UINT32 inBits)
{
  UINT8 result = 0b10111000;
  for (i=0; i<20; i++)
  {
    if (inBits & (1L << i))
      result ^= bitMasks[i];
  }
  return result;
}

Now, to find each bitMask value, use an input where only one bit is 1. Then run it through your test system to find the 8-bit checksum. XOR that result with our initial running result value: 0b10111000, to find the bitMask value.

Upvotes: 2

Related Questions