lib: Move IP checksum to commonlib

This patch moves the IP checksum algorithm into commonlib to prepare for
it being shared with libpayload. The current implementation is ancient
and pretty hard to read (and does some unnecessary questionable things
like the type-punning stuff which leads to suboptimal code generation),
so this reimplements it from scratch (that also helps with the
licensing).

This algorithm is prepared to take in a pre-calculated "wide" checksum
in a machine-register-sized data type which is then narrowed down to 16
bits (see RFC 1071 for why that's valid). This isn't used yet (and the
code will get optimized out), but will be used later in this patch
series for architecture-specific optimization.

Change-Id: Ic04c714c00439a17fc04a8a6e730cc2aa19b8e68
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/80251
Reviewed-by: Yidi Lin <yidilin@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Jakub Czapiga <czapiga@google.com>
diff --git a/src/arch/x86/boot.c b/src/arch/x86/boot.c
index c50ec0e..90af84f 100644
--- a/src/arch/x86/boot.c
+++ b/src/arch/x86/boot.c
@@ -5,7 +5,6 @@
 #include <commonlib/helpers.h>
 #include <console/console.h>
 #include <program_loading.h>
-#include <ip_checksum.h>
 #include <symbols.h>
 #include <assert.h>
 
diff --git a/src/commonlib/Makefile.mk b/src/commonlib/Makefile.mk
index 70e731d..7ec4de9 100644
--- a/src/commonlib/Makefile.mk
+++ b/src/commonlib/Makefile.mk
@@ -61,3 +61,5 @@
 
 decompressor-y += bsd/gcd.c
 all-y += bsd/gcd.c
+
+all-y += bsd/ipchksum.c
diff --git a/src/commonlib/bsd/include/commonlib/bsd/ipchksum.h b/src/commonlib/bsd/include/commonlib/bsd/ipchksum.h
new file mode 100644
index 0000000..91d6872
--- /dev/null
+++ b/src/commonlib/bsd/include/commonlib/bsd/ipchksum.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+
+#ifndef _COMMONLIB_BSD_IPCHKSUM_H_
+#define _COMMONLIB_BSD_IPCHKSUM_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+uint16_t ipchksum(const void *data, size_t size);
+uint16_t ipchksum_add(size_t offset, uint16_t first, uint16_t second);
+
+#endif /* _COMMONLIB_BSD_IPCHKSUM_H_ */
diff --git a/src/commonlib/bsd/ipchksum.c b/src/commonlib/bsd/ipchksum.c
new file mode 100644
index 0000000..a40b86c
--- /dev/null
+++ b/src/commonlib/bsd/ipchksum.c
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later */
+
+#include <commonlib/bsd/ipchksum.h>
+
+/* See RFC 1071 for mathematical explanations of why we can first sum in a larger register and
+   then narrow down, why we don't need to worry about endianness, etc. */
+uint16_t ipchksum(const void *data, size_t size)
+{
+	const uint8_t *p1 = data;
+	unsigned long wide_sum = 0;
+	uint32_t sum = 0;
+	size_t i = 0;
+
+	while (wide_sum) {
+		sum += wide_sum & 0xFFFF;
+		wide_sum >>= 16;
+	}
+	sum = (sum & 0xFFFF) + (sum >> 16);
+
+	for (; i < size; i++) {
+		uint32_t v = p1[i];
+		if (i % 2)
+			v <<= 8;
+		sum += v;
+
+		/* Doing this unconditionally seems to be faster. */
+		sum = (sum & 0xFFFF) + (sum >> 16);
+	}
+
+	return (uint16_t)~sum;
+}
+
+uint16_t ipchksum_add(size_t offset, uint16_t first, uint16_t second)
+{
+	first = ~first;
+	second = ~second;
+
+	/*
+	 * Since the checksum is calculated in 16-bit chunks, if the offset at which
+	 * the data covered by the second checksum would start (if both data streams
+	 * came one after the other) is odd, that means the second stream starts in
+	 * the middle of a 16-bit chunk. This means the second checksum is byte
+	 * swapped compared to what we need it to be, and we must swap it back.
+	 */
+	if (offset % 2)
+		second = (second >> 8) | (second << 8);
+
+	uint32_t sum = first + second;
+	sum = (sum & 0xFFFF) + (sum >> 16);
+
+	return (uint16_t)~sum;
+}
diff --git a/src/drivers/elog/boot_count.c b/src/drivers/elog/boot_count.c
index 17d928a..9d618f9 100644
--- a/src/drivers/elog/boot_count.c
+++ b/src/drivers/elog/boot_count.c
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <commonlib/bsd/ipchksum.h>
 #include <console/console.h>
-#include <ip_checksum.h>
 #include <pc80/mc146818rtc.h>
 #include <stdint.h>
 #include <elog.h>
@@ -47,7 +47,7 @@
 	}
 
 	/* Verify checksum over signature and counter only */
-	csum = compute_ip_checksum(bc, offsetof(struct boot_count, checksum));
+	csum = ipchksum(bc, offsetof(struct boot_count, checksum));
 
 	if (csum != bc->checksum) {
 		printk(BIOS_DEBUG, "Boot Count checksum mismatch\n");
@@ -63,7 +63,7 @@
 	u8 i, *p;
 
 	/* Checksum over signature and counter only */
-	bc->checksum = compute_ip_checksum(
+	bc->checksum = ipchksum(
 		bc, offsetof(struct boot_count, checksum));
 
 	for (p = (u8 *)bc, i = 0; i < sizeof(*bc); i++, p++)
diff --git a/src/drivers/intel/fsp1_1/hob.c b/src/drivers/intel/fsp1_1/hob.c
index e7c5fb4..7522df1 100644
--- a/src/drivers/intel/fsp1_1/hob.c
+++ b/src/drivers/intel/fsp1_1/hob.c
@@ -3,7 +3,6 @@
 #include <arch/hlt.h>
 #include <console/console.h>
 #include <fsp/util.h>
-#include <ip_checksum.h>
 #include <string.h>
 
 /* Compares two EFI GUIDs. Returns true of the GUIDs match, false otherwise. */
diff --git a/src/drivers/net/ne2k.c b/src/drivers/net/ne2k.c
index 4479a76..70ef0ef 100644
--- a/src/drivers/net/ne2k.c
+++ b/src/drivers/net/ne2k.c
@@ -28,11 +28,11 @@
 */
 
 #include <arch/io.h>
+#include <commonlib/bsd/ipchksum.h>
 #include <console/ne2k.h>
 #include <device/device.h>
 #include <device/pci.h>
 #include <device/pci_ops.h>
-#include <ip_checksum.h>
 
 #include "ns8390.h"
 
@@ -184,7 +184,7 @@
 	hdr[38] = (8 + pktlen) >> 8;
 	hdr[39] = 8 + pktlen;
 
-	chksum = compute_ip_checksum(&hdr[14], 20);
+	chksum = ipchksum(&hdr[14], 20);
 
 	hdr[25] = chksum >> 8;
 	hdr[24] = chksum;
diff --git a/src/include/ip_checksum.h b/src/include/ip_checksum.h
deleted file mode 100644
index 74a2f48..0000000
--- a/src/include/ip_checksum.h
+++ /dev/null
@@ -1,8 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-
-#ifndef IP_CHECKSUM_H
-#define IP_CHECKSUM_H
-unsigned long compute_ip_checksum(const void *addr, unsigned long length);
-unsigned long add_ip_checksums(unsigned long offset, unsigned long sum,
-	unsigned long new);
-#endif /* IP_CHECKSUM_H */
diff --git a/src/lib/Makefile.mk b/src/lib/Makefile.mk
index fd3f464..2a95be9 100644
--- a/src/lib/Makefile.mk
+++ b/src/lib/Makefile.mk
@@ -116,8 +116,6 @@
 romstage-$(CONFIG_COLLECT_TIMESTAMPS) += timestamp.c
 romstage-$(CONFIG_CONSOLE_CBMEM) += cbmem_console.c
 
-bootblock-y += compute_ip_checksum.c
-romstage-y += compute_ip_checksum.c
 romstage-y += dimm_info_util.c
 ifeq ($(CONFIG_COMPILER_GCC),y)
 bootblock-$(CONFIG_ARCH_BOOTBLOCK_X86_32) += gcc.c
@@ -145,7 +143,6 @@
 ramstage-y += dimm_info_util.c
 ramstage-y += delay.c
 ramstage-y += fallback_boot.c
-ramstage-y += compute_ip_checksum.c
 ramstage-y += cbfs.c
 ramstage-y += lzma.c lzmadecode.c
 ramstage-y += stack.c
diff --git a/src/lib/compute_ip_checksum.c b/src/lib/compute_ip_checksum.c
deleted file mode 100644
index 913f25c..0000000
--- a/src/lib/compute_ip_checksum.c
+++ /dev/null
@@ -1,53 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-
-#include <stdint.h>
-#include <ip_checksum.h>
-
-unsigned long compute_ip_checksum(const void *addr, unsigned long length)
-{
-	const uint8_t *ptr;
-	volatile union {
-		uint8_t  byte[2];
-		uint16_t word;
-	} value;
-	unsigned long sum;
-	unsigned long i;
-	/* In the most straight forward way possible,
-	 * compute an ip style checksum.
-	 */
-	sum = 0;
-	ptr = addr;
-	for (i = 0; i < length; i++) {
-		unsigned long v;
-		v = ptr[i];
-		if (i & 1)
-			v <<= 8;
-		/* Add the new value */
-		sum += v;
-		/* Wrap around the carry */
-		if (sum > 0xFFFF)
-			sum = (sum + (sum >> 16)) & 0xFFFF;
-	}
-	value.byte[0] = sum & 0xff;
-	value.byte[1] = (sum >> 8) & 0xff;
-	return (~value.word) & 0xFFFF;
-}
-
-unsigned long add_ip_checksums(unsigned long offset, unsigned long sum,
-	unsigned long new)
-{
-	unsigned long checksum;
-	sum = ~sum & 0xFFFF;
-	new = ~new & 0xFFFF;
-	if (offset & 1) {
-		/* byte swap the sum if it came from an odd offset
-		 * since the computation is endian independent this
-		 * works.
-		 */
-		new = ((new >> 8) & 0xff) | ((new << 8) & 0xff00);
-	}
-	checksum = sum + new;
-	if (checksum > 0xFFFF)
-		checksum -= 0xFFFF;
-	return (~checksum) & 0xFFFF;
-}
diff --git a/src/lib/coreboot_table.c b/src/lib/coreboot_table.c
index 800d2d4..d93ba01 100644
--- a/src/lib/coreboot_table.c
+++ b/src/lib/coreboot_table.c
@@ -2,10 +2,10 @@
 
 #include <acpi/acpi.h>
 #include <arch/cbconfig.h>
+#include <commonlib/bsd/ipchksum.h>
 #include <console/console.h>
 #include <console/uart.h>
 #include <identity.h>
-#include <ip_checksum.h>
 #include <boot/coreboot_tables.h>
 #include <boot/tables.h>
 #include <boot_device.h>
@@ -432,10 +432,9 @@
 	}
 
 	first_rec = lb_first_record(head);
-	head->table_checksum = compute_ip_checksum(first_rec,
-		head->table_bytes);
+	head->table_checksum = ipchksum(first_rec, head->table_bytes);
 	head->header_checksum = 0;
-	head->header_checksum = compute_ip_checksum(head, sizeof(*head));
+	head->header_checksum = ipchksum(head, sizeof(*head));
 	printk(BIOS_DEBUG,
 	       "Wrote coreboot table at: %p, 0x%x bytes, checksum %x\n",
 	       head, head->table_bytes, head->table_checksum);
diff --git a/src/northbridge/intel/haswell/broadwell_mrc/raminit.c b/src/northbridge/intel/haswell/broadwell_mrc/raminit.c
index c00e59a..b8a9416 100644
--- a/src/northbridge/intel/haswell/broadwell_mrc/raminit.c
+++ b/src/northbridge/intel/haswell/broadwell_mrc/raminit.c
@@ -8,7 +8,6 @@
 #include <cbmem.h>
 #include <cbfs.h>
 #include <cf9_reset.h>
-#include <ip_checksum.h>
 #include <memory_info.h>
 #include <mrc_cache.h>
 #include <device/device.h>
diff --git a/src/northbridge/intel/haswell/haswell_mrc/raminit.c b/src/northbridge/intel/haswell/haswell_mrc/raminit.c
index bf072fa..f23e40d 100644
--- a/src/northbridge/intel/haswell/haswell_mrc/raminit.c
+++ b/src/northbridge/intel/haswell/haswell_mrc/raminit.c
@@ -7,7 +7,6 @@
 #include <cbmem.h>
 #include <cbfs.h>
 #include <cf9_reset.h>
-#include <ip_checksum.h>
 #include <memory_info.h>
 #include <mrc_cache.h>
 #include <device/device.h>
diff --git a/src/northbridge/intel/ironlake/raminit.c b/src/northbridge/intel/ironlake/raminit.c
index 8a934ed..b84461a 100644
--- a/src/northbridge/intel/ironlake/raminit.c
+++ b/src/northbridge/intel/ironlake/raminit.c
@@ -11,7 +11,6 @@
 #include <cpu/x86/cache.h>
 #include <cbmem.h>
 #include <cf9_reset.h>
-#include <ip_checksum.h>
 #include <option.h>
 #include <device/pci_def.h>
 #include <device/device.h>
diff --git a/src/northbridge/intel/sandybridge/raminit_mrc.c b/src/northbridge/intel/sandybridge/raminit_mrc.c
index dde5742..82e3e82 100644
--- a/src/northbridge/intel/sandybridge/raminit_mrc.c
+++ b/src/northbridge/intel/sandybridge/raminit_mrc.c
@@ -11,7 +11,7 @@
 #include <arch/cpu.h>
 #include <cbmem.h>
 #include <cbfs.h>
-#include <ip_checksum.h>
+#include <commonlib/bsd/ipchksum.h>
 #include <pc80/mc146818rtc.h>
 #include <device/pci_def.h>
 #include <lib.h>
@@ -73,9 +73,9 @@
 	       pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
 
 	/* Save a simple checksum of the seed values */
-	c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed,    sizeof(u32));
-	c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
-	checksum = add_ip_checksums(sizeof(u32), c1, c2);
+	c1 = ipchksum((u8 *)&pei_data->scrambler_seed,    sizeof(u32));
+	c2 = ipchksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
+	checksum = ipchksum_add(sizeof(u32), c1, c2);
 
 	cmos_write((checksum >> 0) & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
 	cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK + 1);
@@ -100,9 +100,9 @@
 	       pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
 
 	/* Compute seed checksum and compare */
-	c1 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed,    sizeof(u32));
-	c2 = compute_ip_checksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
-	checksum = add_ip_checksums(sizeof(u32), c1, c2);
+	c1 = ipchksum((u8 *)&pei_data->scrambler_seed,    sizeof(u32));
+	c2 = ipchksum((u8 *)&pei_data->scrambler_seed_s3, sizeof(u32));
+	checksum = ipchksum_add(sizeof(u32), c1, c2);
 
 	seed_checksum  = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
 	seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK + 1) << 8;
diff --git a/src/soc/intel/common/basecode/ramtop/ramtop.c b/src/soc/intel/common/basecode/ramtop/ramtop.c
index 90717e0..ec326bb 100644
--- a/src/soc/intel/common/basecode/ramtop/ramtop.c
+++ b/src/soc/intel/common/basecode/ramtop/ramtop.c
@@ -1,8 +1,8 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
 
+#include <commonlib/bsd/ipchksum.h>
 #include <console/console.h>
 #include <cpu/x86/mtrr.h>
-#include <ip_checksum.h>
 #include <intelbasecode/ramtop.h>
 #include <pc80/mc146818rtc.h>
 #include <stdint.h>
@@ -57,7 +57,7 @@
 	}
 
 	/* Verify checksum over signature and counter only */
-	csum = compute_ip_checksum(ramtop, offsetof(struct ramtop_table, checksum));
+	csum = ipchksum(ramtop, offsetof(struct ramtop_table, checksum));
 
 	if (csum != ramtop->checksum) {
 		printk(BIOS_DEBUG, "ramtop_table checksum mismatch\n");
@@ -73,8 +73,7 @@
 	u8 i, *p;
 
 	/* Checksum over signature and counter only */
-	ramtop->checksum = compute_ip_checksum(
-		ramtop, offsetof(struct ramtop_table, checksum));
+	ramtop->checksum = ipchksum(ramtop, offsetof(struct ramtop_table, checksum));
 
 	for (p = (u8 *)ramtop, i = 0; i < sizeof(*ramtop); i++, p++)
 		cmos_write(*p, (CMOS_VSTART_ramtop / 8) + i);
diff --git a/src/soc/intel/common/block/cse/cse_lite_cmos.c b/src/soc/intel/common/block/cse/cse_lite_cmos.c
index 02d0232..5c26c01 100644
--- a/src/soc/intel/common/block/cse/cse_lite_cmos.c
+++ b/src/soc/intel/common/block/cse/cse_lite_cmos.c
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <commonlib/bsd/ipchksum.h>
 #include <console/console.h>
-#include <ip_checksum.h>
 #include <pc80/mc146818rtc.h>
 
 #include "cse_lite_cmos.h"
@@ -66,7 +66,7 @@
 	}
 
 	/* Verify checksum over signature and backup_status only */
-	uint16_t csum = compute_ip_checksum(psr, offsetof(struct psr_backup_status, checksum));
+	uint16_t csum = ipchksum(psr, offsetof(struct psr_backup_status, checksum));
 
 	if (csum != psr->checksum) {
 		printk(BIOS_ERR, "PSR backup status checksum mismatch\n");
@@ -80,8 +80,7 @@
 static void psr_backup_status_cmos_write(struct psr_backup_status *psr)
 {
 	/* Checksum over signature and backup_status only */
-	psr->checksum = compute_ip_checksum(
-		psr, offsetof(struct psr_backup_status, checksum));
+	psr->checksum = ipchksum(psr, offsetof(struct psr_backup_status, checksum));
 
 	for (uint8_t *p = (uint8_t *)psr, i = 0; i < sizeof(*psr); i++, p++)
 		cmos_write(*p, PARTITION_FW_CMOS_OFFSET + sizeof(struct cse_specific_info) + i);
diff --git a/src/soc/mediatek/common/memory.c b/src/soc/mediatek/common/memory.c
index 1a627dd..be72023 100644
--- a/src/soc/mediatek/common/memory.c
+++ b/src/soc/mediatek/common/memory.c
@@ -6,7 +6,6 @@
 #include <commonlib/bsd/mem_chip_info.h>
 #include <console/console.h>
 #include <soc/dramc_common.h>
-#include <ip_checksum.h>
 #include <mrc_cache.h>
 #include <soc/dramc_param.h>
 #include <soc/emi.h>
diff --git a/src/soc/mediatek/mt8183/memory.c b/src/soc/mediatek/mt8183/memory.c
index a43cdea..ca6f7a1 100644
--- a/src/soc/mediatek/mt8183/memory.c
+++ b/src/soc/mediatek/mt8183/memory.c
@@ -3,8 +3,8 @@
 #include <assert.h>
 #include <bootmode.h>
 #include <cbfs.h>
+#include <commonlib/bsd/ipchksum.h>
 #include <console/console.h>
-#include <ip_checksum.h>
 #include <security/vboot/vboot_common.h>
 #include <soc/dramc_param.h>
 #include <soc/dramc_pi_api.h>
@@ -63,8 +63,7 @@
 
 static u32 compute_checksum(const struct dramc_param *dparam)
 {
-	return (u32)compute_ip_checksum(dparam->freq_params,
-					sizeof(dparam->freq_params));
+	return (u32)ipchksum(dparam->freq_params, sizeof(dparam->freq_params));
 }
 
 static int dram_run_fast_calibration(const struct dramc_param *dparam,
diff --git a/src/southbridge/intel/bd82x6x/early_pch.c b/src/southbridge/intel/bd82x6x/early_pch.c
index 8433bb2..414b238 100644
--- a/src/southbridge/intel/bd82x6x/early_pch.c
+++ b/src/southbridge/intel/bd82x6x/early_pch.c
@@ -3,7 +3,6 @@
 #include <device/mmio.h>
 #include <device/pci_ops.h>
 #include <cf9_reset.h>
-#include <ip_checksum.h>
 #include <device/pci_def.h>
 #include <device/smbus_host.h>
 #include <southbridge/intel/common/gpio.h>
diff --git a/tests/commonlib/bsd/Makefile.mk b/tests/commonlib/bsd/Makefile.mk
index bf17b6d..3de223e 100644
--- a/tests/commonlib/bsd/Makefile.mk
+++ b/tests/commonlib/bsd/Makefile.mk
@@ -2,8 +2,12 @@
 
 tests-y += helpers-test
 tests-y += gcd-test
+tests-y += ipchksum-test
 
 helpers-test-srcs += tests/commonlib/bsd/helpers-test.c
 
 gcd-test-srcs += tests/commonlib/bsd/gcd-test.c
 gcd-test-srcs += src/commonlib/bsd/gcd.c
+
+ipchksum-test-srcs += tests/commonlib/bsd/ipchksum-test.c
+ipchksum-test-srcs += src/commonlib/bsd/ipchksum.c
diff --git a/tests/commonlib/bsd/ipchksum-test.c b/tests/commonlib/bsd/ipchksum-test.c
new file mode 100644
index 0000000..2aed94c
--- /dev/null
+++ b/tests/commonlib/bsd/ipchksum-test.c
@@ -0,0 +1,99 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <commonlib/bsd/ipchksum.h>
+#include <tests/test.h>
+#include <string.h>
+#include <stdlib.h>
+#include <types.h>
+
+static const uint8_t test_data_simple[] = {
+	0x64, 0x3b, 0x33, 0x17, 0x34, 0x74, 0x62, 0x30, 0x75, 0x73, 0xf3, 0x11, 0x30, 0x2c,
+	0x34, 0x35, 0x6d, 0x39, 0x69, 0x32, 0x23, 0x24, 0x76, 0x71, 0x77, 0x30, 0x39, 0x75,
+	0x76, 0x35, 0x71, 0x32, 0x40, 0x46, 0x34, 0x34, 0xBB, 0x03, 0x66, 0x52};
+static const size_t test_data_simple_sz = ARRAY_SIZE(test_data_simple);
+static const uint16_t test_data_simple_checksum = 0x4267;
+
+static uint8_t test_data_zeros[1024];
+static const size_t test_data_zeros_sz = ARRAY_SIZE(test_data_zeros);
+static const uint16_t test_data_zeros_checksum = 0xFFFF;
+
+static int setup_test_group(void **state)
+{
+	memset(test_data_zeros, 0, test_data_zeros_sz);
+
+	return 0;
+}
+
+static void test_ipchksum_zero_length(void **state)
+{
+	uint16_t res = ipchksum(test_data_simple, 0);
+
+	/* Expect checksum to be in initial state as there are were no data provided. */
+	assert_int_equal(0xFFFF, res);
+}
+
+static void test_ipchksum_zero_buffer(void **state)
+{
+	uint16_t res = ipchksum(test_data_zeros, test_data_zeros_sz);
+	assert_int_equal(test_data_zeros_checksum, res);
+}
+
+static void test_ipchksum_simple_data(void **state)
+{
+	uint16_t res;
+	uint16_t check_res;
+	const size_t helper_buffer_size = sizeof(uint8_t) * (test_data_simple_sz + 2);
+	char *helper_buffer = malloc(helper_buffer_size);
+
+	/* Self test */
+	assert_non_null(helper_buffer);
+
+	/* Expect function to generate the same checksum as stored in */
+	res = ipchksum(test_data_simple, test_data_simple_sz);
+	assert_int_equal(test_data_simple_checksum, res);
+
+	/* Copy test data and checksum to new buffer. Expect computed checksum to be zero,
+	   as it proves the data and the checksum are correct. */
+	memcpy(helper_buffer, test_data_simple, test_data_simple_sz);
+	helper_buffer[helper_buffer_size - 2] = res & 0xFF;
+	helper_buffer[helper_buffer_size - 1] = (res >> 8) & 0xFF;
+	check_res = ipchksum(helper_buffer, helper_buffer_size);
+	assert_int_equal(0, check_res);
+
+	free(helper_buffer);
+}
+
+static void test_ipchksum_add_empty_values(void **state)
+{
+	uint16_t res;
+
+	res = ipchksum_add(0, 0xFFFF, 0xFFFF);
+	assert_int_equal(0xFFFF, res);
+
+	res = ipchksum_add(1, 0xFFFF, 0xFFFF);
+	assert_int_equal(0xFFFF, res);
+}
+
+static void test_ipchksum_add(void **state)
+{
+	uint16_t res_1 = ipchksum(test_data_simple, test_data_simple_sz / 2);
+	uint16_t res_2 = ipchksum(test_data_simple + test_data_simple_sz / 2,
+						  test_data_simple_sz / 2);
+	uint16_t res_sum = ipchksum_add(test_data_simple_sz / 2, res_1, res_2);
+
+	assert_int_equal(test_data_simple_checksum, res_sum);
+}
+
+int main(void)
+{
+	const struct CMUnitTest tests[] = {
+		cmocka_unit_test(test_ipchksum_zero_length),
+		cmocka_unit_test(test_ipchksum_zero_buffer),
+		cmocka_unit_test(test_ipchksum_simple_data),
+
+		cmocka_unit_test(test_ipchksum_add_empty_values),
+		cmocka_unit_test(test_ipchksum_add),
+	};
+
+	return cb_run_group_tests(tests, setup_test_group, NULL);
+}
diff --git a/tests/lib/Makefile.mk b/tests/lib/Makefile.mk
index 0cab1ba..b8c4c71 100644
--- a/tests/lib/Makefile.mk
+++ b/tests/lib/Makefile.mk
@@ -22,7 +22,6 @@
 tests-y += malloc-test
 tests-y += memmove-test
 tests-y += crc_byte-test
-tests-y += compute_ip_checksum-test
 tests-y += memrange-test
 tests-y += uuid-test
 tests-y += bootmem-test
@@ -123,9 +122,6 @@
 crc_byte-test-srcs += tests/lib/crc_byte-test.c
 crc_byte-test-srcs += src/lib/crc_byte.c
 
-compute_ip_checksum-test-srcs += tests/lib/compute_ip_checksum-test.c
-compute_ip_checksum-test-srcs += src/lib/compute_ip_checksum.c
-
 memrange-test-srcs += tests/lib/memrange-test.c
 memrange-test-srcs += src/lib/memrange.c
 memrange-test-srcs += tests/stubs/console.c
@@ -148,7 +144,7 @@
 
 coreboot_table-test-srcs += tests/lib/coreboot_table-test.c
 coreboot_table-test-srcs += tests/stubs/console.c
-coreboot_table-test-srcs += src/lib/compute_ip_checksum.c
+coreboot_table-test-srcs += src/commonlib/bsd/ipchksum.c
 coreboot_table-test-srcs += src/lib/coreboot_table.c
 coreboot_table-test-srcs += src/lib/imd_cbmem.c
 coreboot_table-test-srcs += src/lib/imd.c
diff --git a/tests/lib/compute_ip_checksum-test.c b/tests/lib/compute_ip_checksum-test.c
deleted file mode 100644
index d465bfc..0000000
--- a/tests/lib/compute_ip_checksum-test.c
+++ /dev/null
@@ -1,99 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-
-#include <tests/test.h>
-#include <string.h>
-#include <stdlib.h>
-#include <types.h>
-#include <ip_checksum.h>
-
-static const uint8_t test_data_simple[] = {
-	0x64, 0x3b, 0x33, 0x17, 0x34, 0x74, 0x62, 0x30, 0x75, 0x73, 0xf3, 0x11, 0x30, 0x2c,
-	0x34, 0x35, 0x6d, 0x39, 0x69, 0x32, 0x23, 0x24, 0x76, 0x71, 0x77, 0x30, 0x39, 0x75,
-	0x76, 0x35, 0x71, 0x32, 0x40, 0x46, 0x34, 0x34, 0xBB, 0x03, 0x66, 0x52};
-static const size_t test_data_simple_sz = ARRAY_SIZE(test_data_simple);
-static const unsigned long test_data_simple_checksum = 0x4267;
-
-static uint8_t test_data_zeros[1024];
-static const size_t test_data_zeros_sz = ARRAY_SIZE(test_data_zeros);
-static const unsigned long test_data_zeros_checksum = 0xFFFF;
-
-static int setup_test_group(void **state)
-{
-	memset(test_data_zeros, 0, test_data_zeros_sz);
-
-	return 0;
-}
-
-static void test_compute_ip_checksum_zero_length(void **state)
-{
-	unsigned long res = compute_ip_checksum(test_data_simple, 0);
-
-	/* Expect checksum to be in initial state as there are were no data provided. */
-	assert_int_equal(0xFFFF, res);
-}
-
-static void test_compute_ip_checksum_zero_buffer(void **state)
-{
-	unsigned long res = compute_ip_checksum(test_data_zeros, test_data_zeros_sz);
-	assert_int_equal(test_data_zeros_checksum, res);
-}
-
-static void test_compute_ip_checksum_simple_data(void **state)
-{
-	unsigned long res;
-	unsigned long check_res;
-	const size_t helper_buffer_size = sizeof(uint8_t) * (test_data_simple_sz + 2);
-	char *helper_buffer = malloc(helper_buffer_size);
-
-	/* Self test */
-	assert_non_null(helper_buffer);
-
-	/* Expect function to generate the same checksum as stored in */
-	res = compute_ip_checksum(test_data_simple, test_data_simple_sz);
-	assert_int_equal(test_data_simple_checksum, res);
-
-	/* Copy test data and checksum to new buffer. Expect computed checksum to be zero,
-	   as it proves the data and the checksum are correct. */
-	memcpy(helper_buffer, test_data_simple, test_data_simple_sz);
-	helper_buffer[helper_buffer_size - 2] = res & 0xFF;
-	helper_buffer[helper_buffer_size - 1] = (res >> 8) & 0xFF;
-	check_res = compute_ip_checksum(helper_buffer, helper_buffer_size);
-	assert_int_equal(0, check_res);
-
-	free(helper_buffer);
-}
-
-static void test_add_ip_checksums_empty_values(void **state)
-{
-	unsigned long res;
-
-	res = add_ip_checksums(0, 0xFFFF, 0xFFFF);
-	assert_int_equal(0xFFFF, res);
-
-	res = add_ip_checksums(1, 0xFFFF, 0xFFFF);
-	assert_int_equal(0xFFFF, res);
-}
-
-static void test_add_ip_checksums(void **state)
-{
-	unsigned long res_1 = compute_ip_checksum(test_data_simple, test_data_simple_sz / 2);
-	unsigned long res_2 = compute_ip_checksum(test_data_simple + test_data_simple_sz / 2,
-						  test_data_simple_sz / 2);
-	unsigned long res_sum = add_ip_checksums(test_data_simple_sz / 2, res_1, res_2);
-
-	assert_int_equal(test_data_simple_checksum, res_sum);
-}
-
-int main(void)
-{
-	const struct CMUnitTest tests[] = {
-		cmocka_unit_test(test_compute_ip_checksum_zero_length),
-		cmocka_unit_test(test_compute_ip_checksum_zero_buffer),
-		cmocka_unit_test(test_compute_ip_checksum_simple_data),
-
-		cmocka_unit_test(test_add_ip_checksums_empty_values),
-		cmocka_unit_test(test_add_ip_checksums),
-	};
-
-	return cb_run_group_tests(tests, setup_test_group, NULL);
-}