Appendix A. Checksum Algorithms (from ISO/IEC 8473)
Symbols used in algorithms:
c0, c1 variables used in the algorithms
i position of octet in header (first
octet is i=1)
Bi value of octet i in the header
n position of first octet of checksum (n=8)
L Length of header in octets
X Value of octet one of the checksum parameter
Y Value of octet two of the checksum parameter
Addition is performed in one of the two following modes:
* modulo 255 arithmetic;
* eight-bit one's complement arithmetic;
The algorithm for Generating the Checksum Parameter Value is as follows:
A. Construct the complete header with the value of the
checksum parameter field set to zero; i.e., c0 <- c1 <- 0;
B. Process each octet of the header sequentially from i=1 to L
by:
* c0 <- c0 + Bi
* c1 <- c1 + c0
C. Calculate X, Y as follows:
* X <- (L - 8)(c0 - c1) modulo 255
* Y <- (L - 7)(-C0) + c1
D. If X = 0, then X <- 255
E. If Y = 0, then Y <- 255
F. place the values of X and Y in octets 8 and 9 of the
header, respectively
The algorithm for checking the value of the checksum parameter is as follows:
A. If octets 8 and 9 of the header both contain zero, then the
checksum calculation has succeeded; else if either but not
both of these octets contains the value zero then the
checksum is incorrect; otherwise, initialize: c0 <- c1 <- 0
B. Process each octet of the header sequentially from i = 1 to
L by:
* c0 <- c0 + Bi
* c1 <- c1 + c0
C. When all the octets have been processed, if c0 = c1 = 0,
then the checksum calculation has succeeded, else it has
failed.
There is a separate algorithm to adjust the checksum parameter value when a octet has been modified (such as the TTL). Suppose the value in octet k is changed by Z = newvalue - oldvalue. If X and Y denote the checksum values held in octets n and n+1 respectively, then adjust X and Y as follows:
If X = 0 and Y = 0 then do nothing, else if X = 0 or Y = 0 then the checksum is incorrect, else:
X <- (k - n - 1)Z + X modulo 255
Y <- (n - k)Z + Y modulo 255
If X = 0, then X <- 255; if Y = 0, then Y <- 255.
In the example, n = 89; if the octet altered is the TTL (octet 4), then k = 4. For the case where the lifetime is decreased by one unit (Z = -1), the assignment statements for the new values of X and Y in the immediately preceeding algorithm simplify to:
X <- X + 5 Modulo 255
Y <- Y - 4 Modulo 255