utils: Add API for CRC validation

- Added the validation functions for `pldm_edac_crc32_validate`.

Motivation:
Checksum validation is a hazard for fuzzing. Provide an API that
allows us to adjust the implementation of the test depending on
whether we are building to fuzz the library implementation.

Change-Id: Iffd245fe5cbcadb1037cd1f5065ccc1da6502562
Signed-off-by: Carter Chen <carter.chen.wiwynn@gmail.com>
diff --git a/src/dsp/firmware_update.c b/src/dsp/firmware_update.c
index ada373b..d733d0a 100644
--- a/src/dsp/firmware_update.c
+++ b/src/dsp/firmware_update.c
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
 #include "api.h"
 #include "array.h"
+#include "utils.h"
 #include "compiler.h"
 #include "dsp/base.h"
 #include "msgbuf.h"
@@ -515,13 +516,13 @@
 		return rc;
 	}
 
-	// TODO: pldm_edac_crc32_test(uint32_t expected, const void *data, size_t len)
-	if (package_header_checksum !=
-	    pldm_edac_crc32(data, package_header_payload_size)) {
+	rc = pldm_edac_crc32_validate(package_header_checksum, data,
+				      package_header_payload_size);
+	if (rc) {
 #if 0
-		printf("checksum failure, expected: %#08" PRIx32 ", found: %#08" PRIx32 "\n", package_header_checksum, pldm_edac_crc32(data, package_header_payload_size));
+		printf("header checksum failure, expected: %#08" PRIx32 ", found: %#08" PRIx32 "\n", package_header_checksum, pldm_edac_crc32(data, package_header_payload_size));
 #endif
-		return -EUCLEAN;
+		return rc;
 	}
 
 	/* We stash these to resolve component images later */
diff --git a/src/utils.c b/src/utils.c
index ca0b848..53ed4f7 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
 #include <libpldm/base.h>
 #include <libpldm/utils.h>
+#include "utils.h"
 
 #include <limits.h>
 #include <stdio.h>
@@ -96,6 +97,15 @@
 	return crc ^ ~0U;
 }
 
+int pldm_edac_crc32_validate(uint32_t expected, const void *data, size_t size)
+{
+	if (!data && size) { /* data is NULL but size is not zero */
+		return -EINVAL;
+	}
+	uint32_t actual = pldm_edac_crc32(data, size);
+	return (expected == actual) ? 0 : -EUCLEAN;
+}
+
 LIBPLDM_ABI_STABLE
 uint8_t pldm_edac_crc8(const void *data, size_t size)
 {
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..6a027ca
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+#ifndef LIBPLDM_SRC_UTILS_H
+#define LIBPLDM_SRC_UTILS_H
+
+#include <errno.h>
+#include <stdint.h>
+#include <stddef.h>
+
+/**
+ * @brief Validate the CRC32 checksum of the given data.
+ *
+ * @param[in]   expected    The expected CRC32 value.
+ * @param[in]   data        Pointer to the data to validate.
+ * @param[in]   size        Size of the data in bytes.
+ * @return      0           if the checksum matches,
+ *              -EUCLEAN    if the checksum mismatches,
+ *              -EINVAL     if the arguments are invalid
+ */
+int pldm_edac_crc32_validate(uint32_t expected, const void *data, size_t size);
+#endif