r/embedded • u/tarsiospettro • Feb 23 '25
Reverse Engineering a 16-bit checksum on UART protocol
I'm trying to reverse engineer the UART communication protocol for the diagnostic of a burner controller (SIEMENS LME39). There is no documentation available, and I am working on captured data. I am not still sure about how the protocol works exactly, but it looks a lot like PROFIBUS. Messages have variable length and seems to be structured in the following way:
0x68 LE LEr 0x68 DA SA FC PDU FCS 0x16
Where:
LEis the length of the messageLEris the length of the message repeatedDAis the destination addressSAis the source addressFCis the function codePDUis the payload of variable lengthFCSis a 16-bit checksum
Examples of messages are (I have isolated the checksum and the 0x68 header parts):
HEADER DA SA PAYLOAD CHECKSUM END DELIMITER
68 0B 0B 68 4A 01 26 07 02 01 50 00 00 00 1E 6E DC 16
68 0E 0E 68 5A 01 71 07 01 31 00 00 00 00 00 72 01 02 0E E5 16So, I am really struggling trying to find out the checksum algorithm. Here are my thoughts:
- The checksum is 16-bit and it is applied to the part of the message starting from the destination address (included) to the end of the payload. This seems reasonable in accordance with the PROFIBUS standard and how most of the checksums work.
The checksum is probably not a CRC-16 because:
I have some examples where little changes in the payload result in little changes in the checksum. This is not typical of CRCs.I changed my mind, it really depends on the generator used.- I have made a script to test against all the possible CRC-16 parameters I know (I mean any choice of generator polynomial, initial value, XOR out, bit reversion and bytes reversion. If anyone has any other idea of parameter to test, please let me know) and I have not found any match.
- EDIT: someone proposed that the checksum is maybe not processed on all the message. This does not affect my approach, as my script worked on xored combinations of messages and checksum. If the same header or footer is added to all messages, the xor is just 0 and it does not affect the result
- Checksum seems to be XOR-linear (i.e. Checksum(A XOR B) = Checksum(A) XOR Checksum(B)) on all the examples I have (so apparently this seems to exclude the Fletcher algorithm or other binary sum based algorithms).
Here a pastebin with some examples of messages I have captured: https://pastebin.com/TM8QTtge
Any help or hint would be really appreciated. Thanks in advance.
EDIT:
just xoring with an initial value does not work. For example I have the following couples:
68 21 21 68 08 01 71 01 01 72 1A 02 00 00 B7 B0 B3 30 31 20 20 2D 00 00 00 00 01 00 03 00 00 00 00 00 00 02 00 22 38 16
68 21 21 68 08 01 71 01 01 72 1A 02 00 00 B7 B0 B4 30 31 20 20 2D 00 00 00 00 01 00 03 00 00 00 00 00 00 02 00 22 24 16Where B3 -> B4 produce the checksum change 22 38 -> 22 24
and
68 21 21 68 08 01 71 01 01 72 1A 02 00 00 B9 B5 B1 20 20 32 34 33 00 00 00 00 03 00 00 00 00 00 00 00 00 02 00 BD F7 16
68 21 21 68 08 01 71 01 01 72 1A 02 00 00 B9 B5 B1 20 20 32 34 34 00 00 00 00 03 00 00 00 00 00 00 00 00 02 00 AC 77 16where 33->34 (so the same bits are modified, but in a different byte) results in the checksum change BD F7 -> AC 77.
So any checksum is applied, it seems to depend on the byte position
EDIT2: Following u/ACCount82 suggestion, think it really could be something like:
crc = INIT_VALUEfor b in body:crc = shift_left_modified(crc)crc ^= b
where each b is a couple of bytes of the payload, and shift_left_modified is a shift left which acts in some non standard way on the leftest bit of each byte. Still working on this
UPDATE 1: working on the above hypothesis, I have been able to simplify the messages, removing bits where the checksum calculation make sense. Here the updated list https://pastebin.com/DZdDZt81
UPDATE 2: I have been able to find what seems to me a piece of the algorithm:
- The payload is chunked in words of 2 bytes, from left to right
- to each couple of bytes, the shift_left_modified is applied. It acts as:
- a shift to non-leftmost bits, i.e.: shift_left_modified(0bcdefgh 0ijklmno) = bcdefgh0 ijklmno0
- add (using xor) to the result a different term for the leftest bits of the right byte: shift_left_modified(00000000 10000000) = 0000 0101 1000 0000 0000 0101 1000 0000
- seems to work in different ways depending on the position of the byte for the leftest bits of the right byte. Different choices seem to work in different cases
9
u/150c_vapour Feb 23 '25
Can you dump the flash and decompile the code? Try common checksum algos. Try to find a message with only a few bits set and mostly zero for some starting points.
4
u/robotlasagna Feb 23 '25
its definitely multiplication of some sort since the decrease in column 5 causes an increase in CS1 by 8. That would indicate the poster who brought up fletcher is on the right track (although fletcher doesnt work)
4
u/Old_Budget_4151 Feb 23 '25
FYI, I found a software download for OCI417.10 here, which includes a firmware file with suggestive strings, may be some clues in there:
3
u/Old_Budget_4151 Feb 23 '25
/u/tarsiospettro my approach is stochastic, but I'm seeing patterns that seem very suggestive. Are you able to capture more examples easily?
https://i.imgur.com/KDkEXFx.png
this is a bitmap between message bits and checksum bits
2
u/ACCount82 Feb 23 '25
Looks like a variant of:
But off the top of my head, I can't figure out what the "funky bit shuffle" is. Doesn't look like a barrel shift to me.
2
u/FrenchOempaloempa Feb 23 '25
I've only just got started with the book "understanding checksums and cyclic redundancy checks" by Philip Koopman, but it's really interesting. So I'm certainly no expert, but 2 things I think are worth keeping in mind with reverse engineering:
But I first would check if the checksum is an "adjusted check value". What this means is:
Now in order to make it easier in hardware to determine whether the supplied check value is the same as the recently calculated one, the check value is adjusted, so that when it's added to the calculated check value, and then done Mod, it will be exactly 0, which in programming is not such a big deal, but when done with a hardware decoder is a much easier check.
So say our Modulo value is 10, and our calculated check value is 3. We don't add the 3 as the check value, but we adjust it to 7. This way, after re-calculating the checksum, the receiver only has to add the provided checksum to his calculated checksum: 3+7=10, do the mod operation once more 10 mod 10 = 0, and check for the 0 at the end.
In your case, this might mean that whatever checksum you've calculated so far will never be the same as the one you received. But if you add the value received at the end to your check value, it might end up at 0 if you do modulo once more.
2
u/Old_Budget_4151 Feb 23 '25
Have you seen this btw? https://en.wikipedia.org/wiki/IEC_60870-5
Check out IEC 101 frame format.
13
u/fb39ca4 friendship ended with C++ ❌; rust is my new friend ✅ Feb 23 '25 edited Feb 23 '25
Worst case you can send a message with every possible checksum until it is accepted. Does the device tell you when a message is accepted?
2
u/duane11583 Feb 23 '25
there are many varients of crc16…
just one
1
u/IAmTarkaDaal Feb 23 '25
How much control do you have over the data? Can you generate a checksum for all zeroes, for just 1, for 2, etc, and see if that suggests a pattern?
3
u/Old_Budget_4151 Feb 23 '25
For future use, https://reveng.sourceforge.io/readme.htm may be helpful. But I tried it and yeah, doesn't find any matching CRC-16.
1
u/fb39ca4 friendship ended with C++ ❌; rust is my new friend ✅ Feb 23 '25
Have you tried XORing bytes together?
2
u/ComradeGibbon Feb 23 '25
Fletcher 16 checksum? This doc describes something that uses it.
https://cache.industry.siemens.com/dl/files/292/17515292/att_74771/v1/545-8103-3.pdf
2
u/Old_Budget_4151 Feb 23 '25
I have found a way to XOR subsets of bits that works for the 135 examples of 14-byte payloads. not yet checked the rest.
2
u/SacheonBigChris Feb 23 '25
Here’s an idea, maybe a long shot. Can you determine what microcontroller / processor is being used inside this LME39? This could give you a clue, if for example, the chip has certain checksum / CRC instructions or peripherals built-in.
1
u/madvlad666 Feb 23 '25
Maybe only a portion of the message e.g. only the payload is protected by checksum?
Can you accomplish what you want by brute force by sweeping it or commanding it to every single possible value of position, at least to the resolution necessary, and then just blindly replicate the whole command message?
1
u/i_haz_redditz Feb 25 '25
Maybe it’s PROFIsafe with 24 bit CRC
1
u/NegativeWedding2936 Sep 24 '25
Did you understand the algorithm? I have the same problem:
16.09.2025 - 09:55:42:664(44 byte): DC 05 05 10 03 05 5D 65 16 68 17 17 68 05 03 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 30 16 10 70 05 49 BE 16
16.09.2025 - 09:55:42:738(44 byte): DC 05 05 10 03 05 7D 85 16 68 17 17 68 05 03 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 30 16 10 71 05 49 BF 16
16.09.2025 - 09:55:42:811(44 byte): DC 05 05 10 03 05 5D 65 16 68 17 17 68 05 03 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 30 16 10 72 05 49 C0 16
16.09.2025 - 09:55:42:885(44 byte): DC 05 05 10 03 05 7D 85 16 68 17 17 68 05 03 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 30 16 10 73 05 49 C1 16
16.09.2025 - 09:55:42:959(44 byte): DC 05 05 10 03 05 5D 65 16 68 17 17 68 05 03 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 30 16 10 74 05 49 C2 16
16.09.2025 - 09:55:43:033(44 byte): DC 05 05 10 03 05 7D 85 16 68 17 17 68 05 03 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 30 16 10 75 05 49 C3 16
16.09.2025 - 09:55:43:105(44 byte): DC 05 05 10 03 05 5D 65 16 68 17 17 68 05 03 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 00 00 00 00 08 30 16 10 76 05 49 C4 16