Skip to content

Instantly share code, notes, and snippets.

@CaglayanDokme
Last active February 14, 2024 06:59
Show Gist options
  • Save CaglayanDokme/98afcf4219c3aa3ca4c8490f09856fb0 to your computer and use it in GitHub Desktop.
Save CaglayanDokme/98afcf4219c3aa3ca4c8490f09856fb0 to your computer and use it in GitHub Desktop.

Checksum Calculation for MIL-STD-1760 in C++

MIL-STD-1760, short for Military Standard 1760, is a widely recognized and utilized interface standard within the aerospace and defense industries. This standard defines crucial protocols for data transmission between aircraft and munitions. One such protocol is the use of checksums, which play a vital role in ensuring the integrity of data during transmission.

In the context of MIL-STD-1760, a checksum acts as a digital fingerprint for the transmitted data. Its primary purpose is to detect any potential errors, corruption, or tampering that might occur during transmission. This is especially important to prevent disastrous consequences in mission-critical scenarios. Essentially, the checksum serves as a verification mechanism that allows the receiving end to confirm whether the received data matches what was initially sent by the transmitting end.

At first encounter, the documentation of this standard might seem complex and challenging to understand. The paragraph below, describing the checksum algorithm, can easily leave one bewildered:

All checksummed messages shall include a checksum word that satisfies the following algorithm: When each data word (including the checksum word) of a message is cyclically rotated to the right by a number of bits equal to the count of preceding data words in the message, the resultant rotated data words are summed using modulo 2 arithmetic for each bit (without generating carries). This summation across all bits shall yield a sum of zero.

To demystify this intricate process, the following C++ code snippet demonstrates the calculation of the checksum as outlined in the standard documentation. This code snippet simplifies the algorithm and provides a practical example of its implementation.

#include <cstdint>

namespace MIL_STD_1760 {
    using WORD = uint16_t;

    /**
     * @brief  Calculate checksum of a word array
     * @param  msg    Word array containing the actual message
     * @param  length Amount of words in the given array
     * @return WORD   Calculated checksum value
     */
    WORD calculateChecksum(const WORD *msg, const size_t length)
    {
        if(!msg || (0 == length)) {
            return 0;
        }

        auto cyclicRightShift = [](const WORD value, size_t uShiftAmount) -> WORD
        {
            uShiftAmount = uShiftAmount % (sizeof(WORD) * 8);

            return (value >> uShiftAmount) | (value << ((sizeof(WORD) * 8) - uShiftAmount));
        };

        auto cyclicLeftShift = [](const WORD value, size_t uShiftAmount) -> WORD
        {
            uShiftAmount = uShiftAmount % (sizeof(WORD) * 8);

            return (value << uShiftAmount) | (value >> ((sizeof(WORD) * 8) - uShiftAmount));
        };

        WORD checksum = 0;

        for(size_t uWordIdx = 0; uWordIdx < length; ++uWordIdx) {
            checksum ^= cyclicRightShift(msg[uWordIdx], uWordIdx);
        }

        return cyclicLeftShift(checksum, length);
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment