Version 3 of Internet Checksum

Updated 2007-11-01 04:53:12 by Stu

Stu 2007-10-01 Created page.

The Internet Checksum is a checksum used in (nearly?) every IP packet crossing the Internet or any IP-based network, therefore the speed of of the calculation is very important; oftentimes it is performed in hardware.

Many RFC's described it thusly:

The checksum field is the 16 bit one's complement of the one's complement sum of all 16 bit words in the header.

The description of the checksum and how to calculate it are found in RFC 1071 [L1 ] Computing the Internet Checksum, with additional notes in RFC 1141 [L2 ] Incremental Updating of the Internet Checksum, and RFC 1624 [L3 ] Computation of the Internet Checksum via Incremental Update.

I've written a few Tcl procs to calculate the checksum using 8,16,32 or 64 bit quantities.

# inet_cksum8 --
#
#       Compute Internet Checksum
#       using 8 bit operations
#
# Arguments:
#       data    binary data to be checksummed
#
# Results:
#       cksum   16 bit Internet Checksum
#
proc inet_cksum8 {data} { 
        set cksum 0
        set x {}
        set shift 8
        binary scan $data c* x
        foreach v $x {
                set cksum [expr {$cksum + (($v & 0xff) << $shift)}]
                if {$shift == 8} { set shift 0 } else { set shift 8 }
        }
        return [Inet_cksum_fold_and_complement $cksum]
}
###