vboot: use vb2_crc8 instead of Crc8

No need to have two implementations of this now.

BUG=chromium:611535
BRANCH=none
TEST=make runtests; emerge-kevin coreboot depthcharge

Change-Id: Id3348eae80c5d85451981a44729164ff59f88648
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/399121
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
diff --git a/Makefile b/Makefile
index 0c5c210..53bc4f8 100644
--- a/Makefile
+++ b/Makefile
@@ -317,7 +317,6 @@
 
 # Firmware library sources needed by VbInit() call
 VBINIT_SRCS = \
-	firmware/lib/crc8.c \
 	firmware/lib/utility.c \
 	firmware/lib/vboot_common_init.c \
 	firmware/lib/vboot_nvstorage.c \
@@ -498,9 +497,9 @@
 	cgpt/cgpt_common.c \
 	cgpt/cgpt_create.c \
 	cgpt/cgpt_prioritize.c \
+	firmware/2lib/2crc8.c \
 	firmware/lib/cgptlib/cgptlib_internal.c \
 	firmware/lib/cgptlib/crc32.c \
-	firmware/lib/crc8.c \
 	firmware/lib/gpt_misc.c \
 	${TLCL_SRCS} \
 	firmware/lib/utility_string.c \
@@ -531,6 +530,7 @@
 	cgpt/cgpt_common.c \
 	cgpt/cgpt_create.c \
 	cgpt/cgpt_prioritize.c \
+	firmware/2lib/2crc8.c \
 	firmware/lib/cgptlib/cgptlib_internal.c \
 	firmware/lib/cgptlib/crc32.c \
 	firmware/lib/gpt_misc.c \
diff --git a/firmware/lib/crc8.c b/firmware/lib/crc8.c
deleted file mode 100644
index 0e38e1e..0000000
--- a/firmware/lib/crc8.c
+++ /dev/null
@@ -1,30 +0,0 @@
-/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "sysincludes.h"
-
-#include "crc8.h"
-
-/**
- * Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial.  A table-based
- * algorithm would be faster, but for only a few bytes it isn't worth the code
- * size. */
-uint8_t Crc8(const void *vptr, int len)
-{
-	const uint8_t *data = vptr;
-	unsigned crc = 0;
-	int i, j;
-
-	for (j = len; j; j--, data++) {
-		crc ^= (*data << 8);
-		for(i = 8; i; i--) {
-			if (crc & 0x8000)
-				crc ^= (0x1070 << 3);
-			crc <<= 1;
-		}
-	}
-
-	return (uint8_t)(crc >> 8);
-}
diff --git a/firmware/lib/include/crc8.h b/firmware/lib/include/crc8.h
deleted file mode 100644
index 1434144..0000000
--- a/firmware/lib/include/crc8.h
+++ /dev/null
@@ -1,13 +0,0 @@
-/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- *
- * Very simple 8-bit CRC function.
- */
-#ifndef VBOOT_REFERENCE_CRC8_H_
-#define VBOOT_REFERENCE_CRC8_H_
-#include "sysincludes.h"
-
-uint8_t Crc8(const void *data, int len);
-
-#endif /* VBOOT_REFERENCE_CRC8_H_ */
diff --git a/firmware/lib/rollback_index.c b/firmware/lib/rollback_index.c
index 2ba44ec..84eee00 100644
--- a/firmware/lib/rollback_index.c
+++ b/firmware/lib/rollback_index.c
@@ -8,7 +8,7 @@
 
 #include "sysincludes.h"
 
-#include "crc8.h"
+#include "2crc8.h"
 #include "rollback_index.h"
 #include "tlcl.h"
 #include "tss_constants.h"
@@ -99,7 +99,7 @@
 		 * more times to see if it gets better before we give up. It
 		 * could just be noise.
 		 */
-		if (rsf->crc8 == Crc8(rsf,
+		if (rsf->crc8 == vb2_crc8(rsf,
 				      offsetof(RollbackSpaceFirmware, crc8)))
 			return TPM_SUCCESS;
 
@@ -119,7 +119,7 @@
 	/* All writes should use struct_version 2 or greater. */
 	if (rsf->struct_version < 2)
 		rsf->struct_version = 2;
-	rsf->crc8 = Crc8(rsf, offsetof(RollbackSpaceFirmware, crc8));
+	rsf->crc8 = vb2_crc8(rsf, offsetof(RollbackSpaceFirmware, crc8));
 
 	while (attempts--) {
 		r = SafeWrite(FIRMWARE_NV_INDEX, rsf,
@@ -195,7 +195,8 @@
 		 * more times to see if it gets better before we give up. It
 		 * could just be noise.
 		 */
-		if (rsk->crc8 == Crc8(rsk, offsetof(RollbackSpaceKernel, crc8)))
+		if (rsk->crc8 ==
+		    vb2_crc8(rsk, offsetof(RollbackSpaceKernel, crc8)))
 			return TPM_SUCCESS;
 
 		VBDEBUG(("TPM: %s() - bad CRC\n", __func__));
@@ -214,7 +215,7 @@
 	/* All writes should use struct_version 2 or greater. */
 	if (rsk->struct_version < 2)
 		rsk->struct_version = 2;
-	rsk->crc8 = Crc8(rsk, offsetof(RollbackSpaceKernel, crc8));
+	rsk->crc8 = vb2_crc8(rsk, offsetof(RollbackSpaceKernel, crc8));
 
 	while (attempts--) {
 		r = SafeWrite(KERNEL_NV_INDEX, rsk,
@@ -371,7 +372,7 @@
 		}
 
 		/* Verify CRC */
-		if (u.bf.crc != Crc8(u.buf + 2, u.bf.struct_size - 2)) {
+		if (u.bf.crc != vb2_crc8(u.buf + 2, u.bf.struct_size - 2)) {
 			VBDEBUG(("TPM: %s() - bad CRC\n", __func__));
 			continue;
 		}
diff --git a/firmware/lib/vboot_nvstorage.c b/firmware/lib/vboot_nvstorage.c
index 69feef2..3d6a65d 100644
--- a/firmware/lib/vboot_nvstorage.c
+++ b/firmware/lib/vboot_nvstorage.c
@@ -7,7 +7,7 @@
  */
 #include "sysincludes.h"
 
-#include "crc8.h"
+#include "2crc8.h"
 #include "utility.h"
 #include "vboot_common.h"
 #include "vboot_nvstorage.h"
@@ -79,7 +79,7 @@
 
 	/* Check data for consistency */
 	if ((HEADER_SIGNATURE != (raw[HEADER_OFFSET] & HEADER_MASK))
-	    || (Crc8(raw, CRC_OFFSET) != raw[CRC_OFFSET])) {
+	    || (vb2_crc8(raw, CRC_OFFSET) != raw[CRC_OFFSET])) {
 		/* Data is inconsistent (bad CRC or header); reset defaults */
 		memset(raw, 0, VBNV_BLOCK_SIZE);
 		raw[HEADER_OFFSET] = (HEADER_SIGNATURE |
@@ -96,7 +96,7 @@
 int VbNvTeardown(VbNvContext *context)
 {
 	if (context->regenerate_crc) {
-		context->raw[CRC_OFFSET] = Crc8(context->raw, CRC_OFFSET);
+		context->raw[CRC_OFFSET] = vb2_crc8(context->raw, CRC_OFFSET);
 		context->regenerate_crc = 0;
 		context->raw_changed = 1;
 	}
diff --git a/tests/rollback_index2_tests.c b/tests/rollback_index2_tests.c
index ec36d24..a1fdf0c 100644
--- a/tests/rollback_index2_tests.c
+++ b/tests/rollback_index2_tests.c
@@ -12,7 +12,7 @@
 
 #define _STUB_IMPLEMENTATION_  /* So we can use memset() ourselves */
 
-#include "crc8.h"
+#include "2crc8.h"
 #include "rollback_index.h"
 #include "test_common.h"
 #include "tlcl.h"
@@ -64,7 +64,7 @@
 /* Recalculate CRC of FWMP data */
 static void RecalcFwmpCrc(void)
 {
-	mock_fwmp.fwmp.crc = Crc8(mock_fwmp.buf + 2,
+	mock_fwmp.fwmp.crc = vb2_crc8(mock_fwmp.buf + 2,
 				  mock_fwmp.fwmp.struct_size - 2);
 }
 
@@ -296,7 +296,8 @@
 	/* If the CRC is good and some noise happens, it should recover. */
 	ResetMocks(0, 0);
 	mock_rsf.struct_version = 2;
-	mock_rsf.crc8 = Crc8(&mock_rsf, offsetof(RollbackSpaceFirmware, crc8));
+	mock_rsf.crc8 = vb2_crc8(&mock_rsf,
+				 offsetof(RollbackSpaceFirmware, crc8));
 	noise_on[0] = 1;
 	TEST_EQ(ReadSpaceFirmware(&rsf), 0,
 		"ReadSpaceFirmware(), v2, good CRC");
@@ -399,7 +400,8 @@
 	/* If the CRC is good and some noise happens, it should recover. */
 	ResetMocks(0, 0);
 	mock_rsk.struct_version = 2;
-	mock_rsk.crc8 = Crc8(&mock_rsk, offsetof(RollbackSpaceKernel, crc8));
+	mock_rsk.crc8 = vb2_crc8(&mock_rsk,
+				 offsetof(RollbackSpaceKernel, crc8));
 	noise_on[0] = 1;
 	TEST_EQ(ReadSpaceKernel(&rsk), 0, "ReadSpaceKernel(), v2, good CRC");
 	TEST_STR_EQ(mock_calls,